Bug Summary

File:clang/lib/Serialization/ASTReader.cpp
Warning:line 4914, column 37
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name ASTReader.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -relaxed-aliasing -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/tools/clang/lib/Serialization -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/tools/clang/lib/Serialization -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/clang/lib/Serialization -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/clang/include -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/include -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/llvm/include -D NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/tools/clang/lib/Serialization -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e=. -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2021-09-04-040900-46481-1 -x c++ /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/clang/lib/Serialization/ASTReader.cpp

/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/clang/lib/Serialization/ASTReader.cpp

1//===- ASTReader.cpp - AST File Reader ------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the ASTReader class, which reads AST files.
10//
11//===----------------------------------------------------------------------===//
12
13#include "ASTCommon.h"
14#include "ASTReaderInternals.h"
15#include "clang/AST/ASTConsumer.h"
16#include "clang/AST/ASTContext.h"
17#include "clang/AST/ASTMutationListener.h"
18#include "clang/AST/ASTUnresolvedSet.h"
19#include "clang/AST/AbstractTypeReader.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclBase.h"
22#include "clang/AST/DeclCXX.h"
23#include "clang/AST/DeclFriend.h"
24#include "clang/AST/DeclGroup.h"
25#include "clang/AST/DeclObjC.h"
26#include "clang/AST/DeclTemplate.h"
27#include "clang/AST/DeclarationName.h"
28#include "clang/AST/Expr.h"
29#include "clang/AST/ExprCXX.h"
30#include "clang/AST/ExternalASTSource.h"
31#include "clang/AST/NestedNameSpecifier.h"
32#include "clang/AST/ODRHash.h"
33#include "clang/AST/OpenMPClause.h"
34#include "clang/AST/RawCommentList.h"
35#include "clang/AST/TemplateBase.h"
36#include "clang/AST/TemplateName.h"
37#include "clang/AST/Type.h"
38#include "clang/AST/TypeLoc.h"
39#include "clang/AST/TypeLocVisitor.h"
40#include "clang/AST/UnresolvedSet.h"
41#include "clang/Basic/CommentOptions.h"
42#include "clang/Basic/Diagnostic.h"
43#include "clang/Basic/DiagnosticError.h"
44#include "clang/Basic/DiagnosticOptions.h"
45#include "clang/Basic/ExceptionSpecificationType.h"
46#include "clang/Basic/FileManager.h"
47#include "clang/Basic/FileSystemOptions.h"
48#include "clang/Basic/IdentifierTable.h"
49#include "clang/Basic/LLVM.h"
50#include "clang/Basic/LangOptions.h"
51#include "clang/Basic/Module.h"
52#include "clang/Basic/ObjCRuntime.h"
53#include "clang/Basic/OpenMPKinds.h"
54#include "clang/Basic/OperatorKinds.h"
55#include "clang/Basic/PragmaKinds.h"
56#include "clang/Basic/Sanitizers.h"
57#include "clang/Basic/SourceLocation.h"
58#include "clang/Basic/SourceManager.h"
59#include "clang/Basic/SourceManagerInternals.h"
60#include "clang/Basic/Specifiers.h"
61#include "clang/Basic/TargetInfo.h"
62#include "clang/Basic/TargetOptions.h"
63#include "clang/Basic/TokenKinds.h"
64#include "clang/Basic/Version.h"
65#include "clang/Lex/HeaderSearch.h"
66#include "clang/Lex/HeaderSearchOptions.h"
67#include "clang/Lex/MacroInfo.h"
68#include "clang/Lex/ModuleMap.h"
69#include "clang/Lex/PreprocessingRecord.h"
70#include "clang/Lex/Preprocessor.h"
71#include "clang/Lex/PreprocessorOptions.h"
72#include "clang/Lex/Token.h"
73#include "clang/Sema/ObjCMethodList.h"
74#include "clang/Sema/Scope.h"
75#include "clang/Sema/Sema.h"
76#include "clang/Sema/Weak.h"
77#include "clang/Serialization/ASTBitCodes.h"
78#include "clang/Serialization/ASTDeserializationListener.h"
79#include "clang/Serialization/ASTRecordReader.h"
80#include "clang/Serialization/ContinuousRangeMap.h"
81#include "clang/Serialization/GlobalModuleIndex.h"
82#include "clang/Serialization/InMemoryModuleCache.h"
83#include "clang/Serialization/ModuleFile.h"
84#include "clang/Serialization/ModuleFileExtension.h"
85#include "clang/Serialization/ModuleManager.h"
86#include "clang/Serialization/PCHContainerOperations.h"
87#include "clang/Serialization/SerializationDiagnostic.h"
88#include "llvm/ADT/APFloat.h"
89#include "llvm/ADT/APInt.h"
90#include "llvm/ADT/APSInt.h"
91#include "llvm/ADT/ArrayRef.h"
92#include "llvm/ADT/DenseMap.h"
93#include "llvm/ADT/FloatingPointMode.h"
94#include "llvm/ADT/FoldingSet.h"
95#include "llvm/ADT/Hashing.h"
96#include "llvm/ADT/IntrusiveRefCntPtr.h"
97#include "llvm/ADT/None.h"
98#include "llvm/ADT/Optional.h"
99#include "llvm/ADT/STLExtras.h"
100#include "llvm/ADT/ScopeExit.h"
101#include "llvm/ADT/SmallPtrSet.h"
102#include "llvm/ADT/SmallString.h"
103#include "llvm/ADT/SmallVector.h"
104#include "llvm/ADT/StringExtras.h"
105#include "llvm/ADT/StringMap.h"
106#include "llvm/ADT/StringRef.h"
107#include "llvm/ADT/Triple.h"
108#include "llvm/ADT/iterator_range.h"
109#include "llvm/Bitstream/BitstreamReader.h"
110#include "llvm/Support/Casting.h"
111#include "llvm/Support/Compiler.h"
112#include "llvm/Support/Compression.h"
113#include "llvm/Support/DJB.h"
114#include "llvm/Support/Endian.h"
115#include "llvm/Support/Error.h"
116#include "llvm/Support/ErrorHandling.h"
117#include "llvm/Support/FileSystem.h"
118#include "llvm/Support/LEB128.h"
119#include "llvm/Support/MemoryBuffer.h"
120#include "llvm/Support/Path.h"
121#include "llvm/Support/SaveAndRestore.h"
122#include "llvm/Support/Timer.h"
123#include "llvm/Support/VersionTuple.h"
124#include "llvm/Support/raw_ostream.h"
125#include <algorithm>
126#include <cassert>
127#include <cstddef>
128#include <cstdint>
129#include <cstdio>
130#include <ctime>
131#include <iterator>
132#include <limits>
133#include <map>
134#include <memory>
135#include <string>
136#include <system_error>
137#include <tuple>
138#include <utility>
139#include <vector>
140
141using namespace clang;
142using namespace clang::serialization;
143using namespace clang::serialization::reader;
144using llvm::BitstreamCursor;
145using llvm::RoundingMode;
146
147//===----------------------------------------------------------------------===//
148// ChainedASTReaderListener implementation
149//===----------------------------------------------------------------------===//
150
151bool
152ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
153 return First->ReadFullVersionInformation(FullVersion) ||
154 Second->ReadFullVersionInformation(FullVersion);
155}
156
157void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
158 First->ReadModuleName(ModuleName);
159 Second->ReadModuleName(ModuleName);
160}
161
162void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
163 First->ReadModuleMapFile(ModuleMapPath);
164 Second->ReadModuleMapFile(ModuleMapPath);
165}
166
167bool
168ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
169 bool Complain,
170 bool AllowCompatibleDifferences) {
171 return First->ReadLanguageOptions(LangOpts, Complain,
172 AllowCompatibleDifferences) ||
173 Second->ReadLanguageOptions(LangOpts, Complain,
174 AllowCompatibleDifferences);
175}
176
177bool ChainedASTReaderListener::ReadTargetOptions(
178 const TargetOptions &TargetOpts, bool Complain,
179 bool AllowCompatibleDifferences) {
180 return First->ReadTargetOptions(TargetOpts, Complain,
181 AllowCompatibleDifferences) ||
182 Second->ReadTargetOptions(TargetOpts, Complain,
183 AllowCompatibleDifferences);
184}
185
186bool ChainedASTReaderListener::ReadDiagnosticOptions(
187 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
188 return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
189 Second->ReadDiagnosticOptions(DiagOpts, Complain);
190}
191
192bool
193ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
194 bool Complain) {
195 return First->ReadFileSystemOptions(FSOpts, Complain) ||
196 Second->ReadFileSystemOptions(FSOpts, Complain);
197}
198
199bool ChainedASTReaderListener::ReadHeaderSearchOptions(
200 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
201 bool Complain) {
202 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
203 Complain) ||
204 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
205 Complain);
206}
207
208bool ChainedASTReaderListener::ReadPreprocessorOptions(
209 const PreprocessorOptions &PPOpts, bool Complain,
210 std::string &SuggestedPredefines) {
211 return First->ReadPreprocessorOptions(PPOpts, Complain,
212 SuggestedPredefines) ||
213 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
214}
215
216void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
217 unsigned Value) {
218 First->ReadCounter(M, Value);
219 Second->ReadCounter(M, Value);
220}
221
222bool ChainedASTReaderListener::needsInputFileVisitation() {
223 return First->needsInputFileVisitation() ||
224 Second->needsInputFileVisitation();
225}
226
227bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
228 return First->needsSystemInputFileVisitation() ||
229 Second->needsSystemInputFileVisitation();
230}
231
232void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
233 ModuleKind Kind) {
234 First->visitModuleFile(Filename, Kind);
235 Second->visitModuleFile(Filename, Kind);
236}
237
238bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
239 bool isSystem,
240 bool isOverridden,
241 bool isExplicitModule) {
242 bool Continue = false;
243 if (First->needsInputFileVisitation() &&
244 (!isSystem || First->needsSystemInputFileVisitation()))
245 Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
246 isExplicitModule);
247 if (Second->needsInputFileVisitation() &&
248 (!isSystem || Second->needsSystemInputFileVisitation()))
249 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
250 isExplicitModule);
251 return Continue;
252}
253
254void ChainedASTReaderListener::readModuleFileExtension(
255 const ModuleFileExtensionMetadata &Metadata) {
256 First->readModuleFileExtension(Metadata);
257 Second->readModuleFileExtension(Metadata);
258}
259
260//===----------------------------------------------------------------------===//
261// PCH validator implementation
262//===----------------------------------------------------------------------===//
263
264ASTReaderListener::~ASTReaderListener() = default;
265
266/// Compare the given set of language options against an existing set of
267/// language options.
268///
269/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
270/// \param AllowCompatibleDifferences If true, differences between compatible
271/// language options will be permitted.
272///
273/// \returns true if the languagae options mis-match, false otherwise.
274static bool checkLanguageOptions(const LangOptions &LangOpts,
275 const LangOptions &ExistingLangOpts,
276 DiagnosticsEngine *Diags,
277 bool AllowCompatibleDifferences = true) {
278#define LANGOPT(Name, Bits, Default, Description) \
279 if (ExistingLangOpts.Name != LangOpts.Name) { \
280 if (Diags) \
281 Diags->Report(diag::err_pch_langopt_mismatch) \
282 << Description << LangOpts.Name << ExistingLangOpts.Name; \
283 return true; \
284 }
285
286#define VALUE_LANGOPT(Name, Bits, Default, Description) \
287 if (ExistingLangOpts.Name != LangOpts.Name) { \
288 if (Diags) \
289 Diags->Report(diag::err_pch_langopt_value_mismatch) \
290 << Description; \
291 return true; \
292 }
293
294#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
295 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
296 if (Diags) \
297 Diags->Report(diag::err_pch_langopt_value_mismatch) \
298 << Description; \
299 return true; \
300 }
301
302#define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \
303 if (!AllowCompatibleDifferences) \
304 LANGOPT(Name, Bits, Default, Description)
305
306#define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \
307 if (!AllowCompatibleDifferences) \
308 ENUM_LANGOPT(Name, Bits, Default, Description)
309
310#define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
311 if (!AllowCompatibleDifferences) \
312 VALUE_LANGOPT(Name, Bits, Default, Description)
313
314#define BENIGN_LANGOPT(Name, Bits, Default, Description)
315#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
316#define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
317#include "clang/Basic/LangOptions.def"
318
319 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
320 if (Diags)
321 Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
322 return true;
323 }
324
325 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
326 if (Diags)
327 Diags->Report(diag::err_pch_langopt_value_mismatch)
328 << "target Objective-C runtime";
329 return true;
330 }
331
332 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
333 LangOpts.CommentOpts.BlockCommandNames) {
334 if (Diags)
335 Diags->Report(diag::err_pch_langopt_value_mismatch)
336 << "block command names";
337 return true;
338 }
339
340 // Sanitizer feature mismatches are treated as compatible differences. If
341 // compatible differences aren't allowed, we still only want to check for
342 // mismatches of non-modular sanitizers (the only ones which can affect AST
343 // generation).
344 if (!AllowCompatibleDifferences) {
345 SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
346 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
347 SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
348 ExistingSanitizers.clear(ModularSanitizers);
349 ImportedSanitizers.clear(ModularSanitizers);
350 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
351 const std::string Flag = "-fsanitize=";
352 if (Diags) {
353#define SANITIZER(NAME, ID) \
354 { \
355 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \
356 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \
357 if (InExistingModule != InImportedModule) \
358 Diags->Report(diag::err_pch_targetopt_feature_mismatch) \
359 << InExistingModule << (Flag + NAME); \
360 }
361#include "clang/Basic/Sanitizers.def"
362 }
363 return true;
364 }
365 }
366
367 return false;
368}
369
370/// Compare the given set of target options against an existing set of
371/// target options.
372///
373/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
374///
375/// \returns true if the target options mis-match, false otherwise.
376static bool checkTargetOptions(const TargetOptions &TargetOpts,
377 const TargetOptions &ExistingTargetOpts,
378 DiagnosticsEngine *Diags,
379 bool AllowCompatibleDifferences = true) {
380#define CHECK_TARGET_OPT(Field, Name) \
381 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
382 if (Diags) \
383 Diags->Report(diag::err_pch_targetopt_mismatch) \
384 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
385 return true; \
386 }
387
388 // The triple and ABI must match exactly.
389 CHECK_TARGET_OPT(Triple, "target");
390 CHECK_TARGET_OPT(ABI, "target ABI");
391
392 // We can tolerate different CPUs in many cases, notably when one CPU
393 // supports a strict superset of another. When allowing compatible
394 // differences skip this check.
395 if (!AllowCompatibleDifferences) {
396 CHECK_TARGET_OPT(CPU, "target CPU");
397 CHECK_TARGET_OPT(TuneCPU, "tune CPU");
398 }
399
400#undef CHECK_TARGET_OPT
401
402 // Compare feature sets.
403 SmallVector<StringRef, 4> ExistingFeatures(
404 ExistingTargetOpts.FeaturesAsWritten.begin(),
405 ExistingTargetOpts.FeaturesAsWritten.end());
406 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
407 TargetOpts.FeaturesAsWritten.end());
408 llvm::sort(ExistingFeatures);
409 llvm::sort(ReadFeatures);
410
411 // We compute the set difference in both directions explicitly so that we can
412 // diagnose the differences differently.
413 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
414 std::set_difference(
415 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
416 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
417 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
418 ExistingFeatures.begin(), ExistingFeatures.end(),
419 std::back_inserter(UnmatchedReadFeatures));
420
421 // If we are allowing compatible differences and the read feature set is
422 // a strict subset of the existing feature set, there is nothing to diagnose.
423 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
424 return false;
425
426 if (Diags) {
427 for (StringRef Feature : UnmatchedReadFeatures)
428 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
429 << /* is-existing-feature */ false << Feature;
430 for (StringRef Feature : UnmatchedExistingFeatures)
431 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
432 << /* is-existing-feature */ true << Feature;
433 }
434
435 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
436}
437
438bool
439PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
440 bool Complain,
441 bool AllowCompatibleDifferences) {
442 const LangOptions &ExistingLangOpts = PP.getLangOpts();
443 return checkLanguageOptions(LangOpts, ExistingLangOpts,
444 Complain ? &Reader.Diags : nullptr,
445 AllowCompatibleDifferences);
446}
447
448bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
449 bool Complain,
450 bool AllowCompatibleDifferences) {
451 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
452 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
453 Complain ? &Reader.Diags : nullptr,
454 AllowCompatibleDifferences);
455}
456
457namespace {
458
459using MacroDefinitionsMap =
460 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
461using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
462
463} // namespace
464
465static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
466 DiagnosticsEngine &Diags,
467 bool Complain) {
468 using Level = DiagnosticsEngine::Level;
469
470 // Check current mappings for new -Werror mappings, and the stored mappings
471 // for cases that were explicitly mapped to *not* be errors that are now
472 // errors because of options like -Werror.
473 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
474
475 for (DiagnosticsEngine *MappingSource : MappingSources) {
476 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
477 diag::kind DiagID = DiagIDMappingPair.first;
478 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
479 if (CurLevel < DiagnosticsEngine::Error)
480 continue; // not significant
481 Level StoredLevel =
482 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
483 if (StoredLevel < DiagnosticsEngine::Error) {
484 if (Complain)
485 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
486 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
487 return true;
488 }
489 }
490 }
491
492 return false;
493}
494
495static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
496 diag::Severity Ext = Diags.getExtensionHandlingBehavior();
497 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
498 return true;
499 return Ext >= diag::Severity::Error;
500}
501
502static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
503 DiagnosticsEngine &Diags,
504 bool IsSystem, bool Complain) {
505 // Top-level options
506 if (IsSystem) {
507 if (Diags.getSuppressSystemWarnings())
508 return false;
509 // If -Wsystem-headers was not enabled before, be conservative
510 if (StoredDiags.getSuppressSystemWarnings()) {
511 if (Complain)
512 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
513 return true;
514 }
515 }
516
517 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
518 if (Complain)
519 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
520 return true;
521 }
522
523 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
524 !StoredDiags.getEnableAllWarnings()) {
525 if (Complain)
526 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
527 return true;
528 }
529
530 if (isExtHandlingFromDiagsError(Diags) &&
531 !isExtHandlingFromDiagsError(StoredDiags)) {
532 if (Complain)
533 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
534 return true;
535 }
536
537 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
538}
539
540/// Return the top import module if it is implicit, nullptr otherwise.
541static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
542 Preprocessor &PP) {
543 // If the original import came from a file explicitly generated by the user,
544 // don't check the diagnostic mappings.
545 // FIXME: currently this is approximated by checking whether this is not a
546 // module import of an implicitly-loaded module file.
547 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
548 // the transitive closure of its imports, since unrelated modules cannot be
549 // imported until after this module finishes validation.
550 ModuleFile *TopImport = &*ModuleMgr.rbegin();
551 while (!TopImport->ImportedBy.empty())
552 TopImport = TopImport->ImportedBy[0];
553 if (TopImport->Kind != MK_ImplicitModule)
554 return nullptr;
555
556 StringRef ModuleName = TopImport->ModuleName;
557 assert(!ModuleName.empty() && "diagnostic options read before module name")(static_cast<void> (0));
558
559 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
560 assert(M && "missing module")(static_cast<void> (0));
561 return M;
562}
563
564bool PCHValidator::ReadDiagnosticOptions(
565 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
566 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
567 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
568 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
569 new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
570 // This should never fail, because we would have processed these options
571 // before writing them to an ASTFile.
572 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
573
574 ModuleManager &ModuleMgr = Reader.getModuleManager();
575 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then")(static_cast<void> (0));
576
577 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
578 if (!TopM)
579 return false;
580
581 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
582 // contains the union of their flags.
583 return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
584 Complain);
585}
586
587/// Collect the macro definitions provided by the given preprocessor
588/// options.
589static void
590collectMacroDefinitions(const PreprocessorOptions &PPOpts,
591 MacroDefinitionsMap &Macros,
592 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
593 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
594 StringRef Macro = PPOpts.Macros[I].first;
595 bool IsUndef = PPOpts.Macros[I].second;
596
597 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
598 StringRef MacroName = MacroPair.first;
599 StringRef MacroBody = MacroPair.second;
600
601 // For an #undef'd macro, we only care about the name.
602 if (IsUndef) {
603 if (MacroNames && !Macros.count(MacroName))
604 MacroNames->push_back(MacroName);
605
606 Macros[MacroName] = std::make_pair("", true);
607 continue;
608 }
609
610 // For a #define'd macro, figure out the actual definition.
611 if (MacroName.size() == Macro.size())
612 MacroBody = "1";
613 else {
614 // Note: GCC drops anything following an end-of-line character.
615 StringRef::size_type End = MacroBody.find_first_of("\n\r");
616 MacroBody = MacroBody.substr(0, End);
617 }
618
619 if (MacroNames && !Macros.count(MacroName))
620 MacroNames->push_back(MacroName);
621 Macros[MacroName] = std::make_pair(MacroBody, false);
622 }
623}
624
625/// Check the preprocessor options deserialized from the control block
626/// against the preprocessor options in an existing preprocessor.
627///
628/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
629/// \param Validate If true, validate preprocessor options. If false, allow
630/// macros defined by \p ExistingPPOpts to override those defined by
631/// \p PPOpts in SuggestedPredefines.
632static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
633 const PreprocessorOptions &ExistingPPOpts,
634 DiagnosticsEngine *Diags,
635 FileManager &FileMgr,
636 std::string &SuggestedPredefines,
637 const LangOptions &LangOpts,
638 bool Validate = true) {
639 // Check macro definitions.
640 MacroDefinitionsMap ASTFileMacros;
641 collectMacroDefinitions(PPOpts, ASTFileMacros);
642 MacroDefinitionsMap ExistingMacros;
643 SmallVector<StringRef, 4> ExistingMacroNames;
644 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
645
646 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
647 // Dig out the macro definition in the existing preprocessor options.
648 StringRef MacroName = ExistingMacroNames[I];
649 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
650
651 // Check whether we know anything about this macro name or not.
652 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
653 ASTFileMacros.find(MacroName);
654 if (!Validate || Known == ASTFileMacros.end()) {
655 // FIXME: Check whether this identifier was referenced anywhere in the
656 // AST file. If so, we should reject the AST file. Unfortunately, this
657 // information isn't in the control block. What shall we do about it?
658
659 if (Existing.second) {
660 SuggestedPredefines += "#undef ";
661 SuggestedPredefines += MacroName.str();
662 SuggestedPredefines += '\n';
663 } else {
664 SuggestedPredefines += "#define ";
665 SuggestedPredefines += MacroName.str();
666 SuggestedPredefines += ' ';
667 SuggestedPredefines += Existing.first.str();
668 SuggestedPredefines += '\n';
669 }
670 continue;
671 }
672
673 // If the macro was defined in one but undef'd in the other, we have a
674 // conflict.
675 if (Existing.second != Known->second.second) {
676 if (Diags) {
677 Diags->Report(diag::err_pch_macro_def_undef)
678 << MacroName << Known->second.second;
679 }
680 return true;
681 }
682
683 // If the macro was #undef'd in both, or if the macro bodies are identical,
684 // it's fine.
685 if (Existing.second || Existing.first == Known->second.first)
686 continue;
687
688 // The macro bodies differ; complain.
689 if (Diags) {
690 Diags->Report(diag::err_pch_macro_def_conflict)
691 << MacroName << Known->second.first << Existing.first;
692 }
693 return true;
694 }
695
696 // Check whether we're using predefines.
697 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
698 if (Diags) {
699 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
700 }
701 return true;
702 }
703
704 // Detailed record is important since it is used for the module cache hash.
705 if (LangOpts.Modules &&
706 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
707 if (Diags) {
708 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
709 }
710 return true;
711 }
712
713 // Compute the #include and #include_macros lines we need.
714 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
715 StringRef File = ExistingPPOpts.Includes[I];
716
717 if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
718 !ExistingPPOpts.PCHThroughHeader.empty()) {
719 // In case the through header is an include, we must add all the includes
720 // to the predefines so the start point can be determined.
721 SuggestedPredefines += "#include \"";
722 SuggestedPredefines += File;
723 SuggestedPredefines += "\"\n";
724 continue;
725 }
726
727 if (File == ExistingPPOpts.ImplicitPCHInclude)
728 continue;
729
730 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
731 != PPOpts.Includes.end())
732 continue;
733
734 SuggestedPredefines += "#include \"";
735 SuggestedPredefines += File;
736 SuggestedPredefines += "\"\n";
737 }
738
739 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
740 StringRef File = ExistingPPOpts.MacroIncludes[I];
741 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
742 File)
743 != PPOpts.MacroIncludes.end())
744 continue;
745
746 SuggestedPredefines += "#__include_macros \"";
747 SuggestedPredefines += File;
748 SuggestedPredefines += "\"\n##\n";
749 }
750
751 return false;
752}
753
754bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
755 bool Complain,
756 std::string &SuggestedPredefines) {
757 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
758
759 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
760 Complain? &Reader.Diags : nullptr,
761 PP.getFileManager(),
762 SuggestedPredefines,
763 PP.getLangOpts());
764}
765
766bool SimpleASTReaderListener::ReadPreprocessorOptions(
767 const PreprocessorOptions &PPOpts,
768 bool Complain,
769 std::string &SuggestedPredefines) {
770 return checkPreprocessorOptions(PPOpts,
771 PP.getPreprocessorOpts(),
772 nullptr,
773 PP.getFileManager(),
774 SuggestedPredefines,
775 PP.getLangOpts(),
776 false);
777}
778
779/// Check the header search options deserialized from the control block
780/// against the header search options in an existing preprocessor.
781///
782/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
783static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
784 StringRef SpecificModuleCachePath,
785 StringRef ExistingModuleCachePath,
786 DiagnosticsEngine *Diags,
787 const LangOptions &LangOpts,
788 const PreprocessorOptions &PPOpts) {
789 if (LangOpts.Modules) {
790 if (SpecificModuleCachePath != ExistingModuleCachePath &&
791 !PPOpts.AllowPCHWithDifferentModulesCachePath) {
792 if (Diags)
793 Diags->Report(diag::err_pch_modulecache_mismatch)
794 << SpecificModuleCachePath << ExistingModuleCachePath;
795 return true;
796 }
797 }
798
799 return false;
800}
801
802bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
803 StringRef SpecificModuleCachePath,
804 bool Complain) {
805 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
806 PP.getHeaderSearchInfo().getModuleCachePath(),
807 Complain ? &Reader.Diags : nullptr,
808 PP.getLangOpts(), PP.getPreprocessorOpts());
809}
810
811void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
812 PP.setCounterValue(Value);
813}
814
815//===----------------------------------------------------------------------===//
816// AST reader implementation
817//===----------------------------------------------------------------------===//
818
819static uint64_t readULEB(const unsigned char *&P) {
820 unsigned Length = 0;
821 const char *Error = nullptr;
822
823 uint64_t Val = llvm::decodeULEB128(P, &Length, nullptr, &Error);
824 if (Error)
825 llvm::report_fatal_error(Error);
826 P += Length;
827 return Val;
828}
829
830/// Read ULEB-encoded key length and data length.
831static std::pair<unsigned, unsigned>
832readULEBKeyDataLength(const unsigned char *&P) {
833 unsigned KeyLen = readULEB(P);
834 if ((unsigned)KeyLen != KeyLen)
835 llvm::report_fatal_error("key too large");
836
837 unsigned DataLen = readULEB(P);
838 if ((unsigned)DataLen != DataLen)
839 llvm::report_fatal_error("data too large");
840
841 return std::make_pair(KeyLen, DataLen);
842}
843
844void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
845 bool TakeOwnership) {
846 DeserializationListener = Listener;
847 OwnsDeserializationListener = TakeOwnership;
848}
849
850unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
851 return serialization::ComputeHash(Sel);
852}
853
854std::pair<unsigned, unsigned>
855ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
856 return readULEBKeyDataLength(d);
857}
858
859ASTSelectorLookupTrait::internal_key_type
860ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
861 using namespace llvm::support;
862
863 SelectorTable &SelTable = Reader.getContext().Selectors;
864 unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
865 IdentifierInfo *FirstII = Reader.getLocalIdentifier(
866 F, endian::readNext<uint32_t, little, unaligned>(d));
867 if (N == 0)
868 return SelTable.getNullarySelector(FirstII);
869 else if (N == 1)
870 return SelTable.getUnarySelector(FirstII);
871
872 SmallVector<IdentifierInfo *, 16> Args;
873 Args.push_back(FirstII);
874 for (unsigned I = 1; I != N; ++I)
875 Args.push_back(Reader.getLocalIdentifier(
876 F, endian::readNext<uint32_t, little, unaligned>(d)));
877
878 return SelTable.getSelector(N, Args.data());
879}
880
881ASTSelectorLookupTrait::data_type
882ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
883 unsigned DataLen) {
884 using namespace llvm::support;
885
886 data_type Result;
887
888 Result.ID = Reader.getGlobalSelectorID(
889 F, endian::readNext<uint32_t, little, unaligned>(d));
890 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
891 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
892 Result.InstanceBits = FullInstanceBits & 0x3;
893 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
894 Result.FactoryBits = FullFactoryBits & 0x3;
895 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
896 unsigned NumInstanceMethods = FullInstanceBits >> 3;
897 unsigned NumFactoryMethods = FullFactoryBits >> 3;
898
899 // Load instance methods
900 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
901 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
902 F, endian::readNext<uint32_t, little, unaligned>(d)))
903 Result.Instance.push_back(Method);
904 }
905
906 // Load factory methods
907 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
908 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
909 F, endian::readNext<uint32_t, little, unaligned>(d)))
910 Result.Factory.push_back(Method);
911 }
912
913 return Result;
914}
915
916unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
917 return llvm::djbHash(a);
918}
919
920std::pair<unsigned, unsigned>
921ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
922 return readULEBKeyDataLength(d);
923}
924
925ASTIdentifierLookupTraitBase::internal_key_type
926ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
927 assert(n >= 2 && d[n-1] == '\0')(static_cast<void> (0));
928 return StringRef((const char*) d, n-1);
929}
930
931/// Whether the given identifier is "interesting".
932static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
933 bool IsModule) {
934 return II.hadMacroDefinition() || II.isPoisoned() ||
935 (!IsModule && II.getObjCOrBuiltinID()) ||
936 II.hasRevertedTokenIDToIdentifier() ||
937 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
938 II.getFETokenInfo());
939}
940
941static bool readBit(unsigned &Bits) {
942 bool Value = Bits & 0x1;
943 Bits >>= 1;
944 return Value;
945}
946
947IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
948 using namespace llvm::support;
949
950 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
951 return Reader.getGlobalIdentifierID(F, RawID >> 1);
952}
953
954static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
955 if (!II.isFromAST()) {
956 II.setIsFromAST();
957 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
958 if (isInterestingIdentifier(Reader, II, IsModule))
959 II.setChangedSinceDeserialization();
960 }
961}
962
963IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
964 const unsigned char* d,
965 unsigned DataLen) {
966 using namespace llvm::support;
967
968 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
969 bool IsInteresting = RawID & 0x01;
970
971 // Wipe out the "is interesting" bit.
972 RawID = RawID >> 1;
973
974 // Build the IdentifierInfo and link the identifier ID with it.
975 IdentifierInfo *II = KnownII;
976 if (!II) {
977 II = &Reader.getIdentifierTable().getOwn(k);
978 KnownII = II;
979 }
980 markIdentifierFromAST(Reader, *II);
981 Reader.markIdentifierUpToDate(II);
982
983 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
984 if (!IsInteresting) {
985 // For uninteresting identifiers, there's nothing else to do. Just notify
986 // the reader that we've finished loading this identifier.
987 Reader.SetIdentifierInfo(ID, II);
988 return II;
989 }
990
991 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
992 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
993 bool CPlusPlusOperatorKeyword = readBit(Bits);
994 bool HasRevertedTokenIDToIdentifier = readBit(Bits);
995 bool Poisoned = readBit(Bits);
996 bool ExtensionToken = readBit(Bits);
997 bool HadMacroDefinition = readBit(Bits);
998
999 assert(Bits == 0 && "Extra bits in the identifier?")(static_cast<void> (0));
1000 DataLen -= 8;
1001
1002 // Set or check the various bits in the IdentifierInfo structure.
1003 // Token IDs are read-only.
1004 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
1005 II->revertTokenIDToIdentifier();
1006 if (!F.isModule())
1007 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1008 assert(II->isExtensionToken() == ExtensionToken &&(static_cast<void> (0))
1009 "Incorrect extension token flag")(static_cast<void> (0));
1010 (void)ExtensionToken;
1011 if (Poisoned)
1012 II->setIsPoisoned(true);
1013 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&(static_cast<void> (0))
1014 "Incorrect C++ operator keyword flag")(static_cast<void> (0));
1015 (void)CPlusPlusOperatorKeyword;
1016
1017 // If this identifier is a macro, deserialize the macro
1018 // definition.
1019 if (HadMacroDefinition) {
1020 uint32_t MacroDirectivesOffset =
1021 endian::readNext<uint32_t, little, unaligned>(d);
1022 DataLen -= 4;
1023
1024 Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1025 }
1026
1027 Reader.SetIdentifierInfo(ID, II);
1028
1029 // Read all of the declarations visible at global scope with this
1030 // name.
1031 if (DataLen > 0) {
1032 SmallVector<uint32_t, 4> DeclIDs;
1033 for (; DataLen > 0; DataLen -= 4)
1034 DeclIDs.push_back(Reader.getGlobalDeclID(
1035 F, endian::readNext<uint32_t, little, unaligned>(d)));
1036 Reader.SetGloballyVisibleDecls(II, DeclIDs);
1037 }
1038
1039 return II;
1040}
1041
1042DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1043 : Kind(Name.getNameKind()) {
1044 switch (Kind) {
1045 case DeclarationName::Identifier:
1046 Data = (uint64_t)Name.getAsIdentifierInfo();
1047 break;
1048 case DeclarationName::ObjCZeroArgSelector:
1049 case DeclarationName::ObjCOneArgSelector:
1050 case DeclarationName::ObjCMultiArgSelector:
1051 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1052 break;
1053 case DeclarationName::CXXOperatorName:
1054 Data = Name.getCXXOverloadedOperator();
1055 break;
1056 case DeclarationName::CXXLiteralOperatorName:
1057 Data = (uint64_t)Name.getCXXLiteralIdentifier();
1058 break;
1059 case DeclarationName::CXXDeductionGuideName:
1060 Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1061 ->getDeclName().getAsIdentifierInfo();
1062 break;
1063 case DeclarationName::CXXConstructorName:
1064 case DeclarationName::CXXDestructorName:
1065 case DeclarationName::CXXConversionFunctionName:
1066 case DeclarationName::CXXUsingDirective:
1067 Data = 0;
1068 break;
1069 }
1070}
1071
1072unsigned DeclarationNameKey::getHash() const {
1073 llvm::FoldingSetNodeID ID;
1074 ID.AddInteger(Kind);
1075
1076 switch (Kind) {
1077 case DeclarationName::Identifier:
1078 case DeclarationName::CXXLiteralOperatorName:
1079 case DeclarationName::CXXDeductionGuideName:
1080 ID.AddString(((IdentifierInfo*)Data)->getName());
1081 break;
1082 case DeclarationName::ObjCZeroArgSelector:
1083 case DeclarationName::ObjCOneArgSelector:
1084 case DeclarationName::ObjCMultiArgSelector:
1085 ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1086 break;
1087 case DeclarationName::CXXOperatorName:
1088 ID.AddInteger((OverloadedOperatorKind)Data);
1089 break;
1090 case DeclarationName::CXXConstructorName:
1091 case DeclarationName::CXXDestructorName:
1092 case DeclarationName::CXXConversionFunctionName:
1093 case DeclarationName::CXXUsingDirective:
1094 break;
1095 }
1096
1097 return ID.ComputeHash();
1098}
1099
1100ModuleFile *
1101ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1102 using namespace llvm::support;
1103
1104 uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1105 return Reader.getLocalModuleFile(F, ModuleFileID);
1106}
1107
1108std::pair<unsigned, unsigned>
1109ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1110 return readULEBKeyDataLength(d);
1111}
1112
1113ASTDeclContextNameLookupTrait::internal_key_type
1114ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1115 using namespace llvm::support;
1116
1117 auto Kind = (DeclarationName::NameKind)*d++;
1118 uint64_t Data;
1119 switch (Kind) {
1120 case DeclarationName::Identifier:
1121 case DeclarationName::CXXLiteralOperatorName:
1122 case DeclarationName::CXXDeductionGuideName:
1123 Data = (uint64_t)Reader.getLocalIdentifier(
1124 F, endian::readNext<uint32_t, little, unaligned>(d));
1125 break;
1126 case DeclarationName::ObjCZeroArgSelector:
1127 case DeclarationName::ObjCOneArgSelector:
1128 case DeclarationName::ObjCMultiArgSelector:
1129 Data =
1130 (uint64_t)Reader.getLocalSelector(
1131 F, endian::readNext<uint32_t, little, unaligned>(
1132 d)).getAsOpaquePtr();
1133 break;
1134 case DeclarationName::CXXOperatorName:
1135 Data = *d++; // OverloadedOperatorKind
1136 break;
1137 case DeclarationName::CXXConstructorName:
1138 case DeclarationName::CXXDestructorName:
1139 case DeclarationName::CXXConversionFunctionName:
1140 case DeclarationName::CXXUsingDirective:
1141 Data = 0;
1142 break;
1143 }
1144
1145 return DeclarationNameKey(Kind, Data);
1146}
1147
1148void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1149 const unsigned char *d,
1150 unsigned DataLen,
1151 data_type_builder &Val) {
1152 using namespace llvm::support;
1153
1154 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1155 uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1156 Val.insert(Reader.getGlobalDeclID(F, LocalID));
1157 }
1158}
1159
1160bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1161 BitstreamCursor &Cursor,
1162 uint64_t Offset,
1163 DeclContext *DC) {
1164 assert(Offset != 0)(static_cast<void> (0));
1165
1166 SavedStreamPosition SavedPosition(Cursor);
1167 if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1168 Error(std::move(Err));
1169 return true;
1170 }
1171
1172 RecordData Record;
1173 StringRef Blob;
1174 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1175 if (!MaybeCode) {
1176 Error(MaybeCode.takeError());
1177 return true;
1178 }
1179 unsigned Code = MaybeCode.get();
1180
1181 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1182 if (!MaybeRecCode) {
1183 Error(MaybeRecCode.takeError());
1184 return true;
1185 }
1186 unsigned RecCode = MaybeRecCode.get();
1187 if (RecCode != DECL_CONTEXT_LEXICAL) {
1188 Error("Expected lexical block");
1189 return true;
1190 }
1191
1192 assert(!isa<TranslationUnitDecl>(DC) &&(static_cast<void> (0))
1193 "expected a TU_UPDATE_LEXICAL record for TU")(static_cast<void> (0));
1194 // If we are handling a C++ class template instantiation, we can see multiple
1195 // lexical updates for the same record. It's important that we select only one
1196 // of them, so that field numbering works properly. Just pick the first one we
1197 // see.
1198 auto &Lex = LexicalDecls[DC];
1199 if (!Lex.first) {
1200 Lex = std::make_pair(
1201 &M, llvm::makeArrayRef(
1202 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1203 Blob.data()),
1204 Blob.size() / 4));
1205 }
1206 DC->setHasExternalLexicalStorage(true);
1207 return false;
1208}
1209
1210bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1211 BitstreamCursor &Cursor,
1212 uint64_t Offset,
1213 DeclID ID) {
1214 assert(Offset != 0)(static_cast<void> (0));
1215
1216 SavedStreamPosition SavedPosition(Cursor);
1217 if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1218 Error(std::move(Err));
1219 return true;
1220 }
1221
1222 RecordData Record;
1223 StringRef Blob;
1224 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1225 if (!MaybeCode) {
1226 Error(MaybeCode.takeError());
1227 return true;
1228 }
1229 unsigned Code = MaybeCode.get();
1230
1231 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1232 if (!MaybeRecCode) {
1233 Error(MaybeRecCode.takeError());
1234 return true;
1235 }
1236 unsigned RecCode = MaybeRecCode.get();
1237 if (RecCode != DECL_CONTEXT_VISIBLE) {
1238 Error("Expected visible lookup table block");
1239 return true;
1240 }
1241
1242 // We can't safely determine the primary context yet, so delay attaching the
1243 // lookup table until we're done with recursive deserialization.
1244 auto *Data = (const unsigned char*)Blob.data();
1245 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1246 return false;
1247}
1248
1249void ASTReader::Error(StringRef Msg) const {
1250 Error(diag::err_fe_pch_malformed, Msg);
1251 if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1252 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1253 Diag(diag::note_module_cache_path)
1254 << PP.getHeaderSearchInfo().getModuleCachePath();
1255 }
1256}
1257
1258void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1259 StringRef Arg3) const {
1260 if (Diags.isDiagnosticInFlight())
1261 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
1262 else
1263 Diag(DiagID) << Arg1 << Arg2 << Arg3;
1264}
1265
1266void ASTReader::Error(llvm::Error &&Err) const {
1267 llvm::Error RemainingErr =
1268 handleErrors(std::move(Err), [this](const DiagnosticError &E) {
1269 auto Diag = E.getDiagnostic().second;
1270
1271 // Ideally we'd just emit it, but have to handle a possible in-flight
1272 // diagnostic. Note that the location is currently ignored as well.
1273 auto NumArgs = Diag.getStorage()->NumDiagArgs;
1274 assert(NumArgs <= 3 && "Can only have up to 3 arguments")(static_cast<void> (0));
1275 StringRef Arg1, Arg2, Arg3;
1276 switch (NumArgs) {
1277 case 3:
1278 Arg3 = Diag.getStringArg(2);
1279 LLVM_FALLTHROUGH[[gnu::fallthrough]];
1280 case 2:
1281 Arg2 = Diag.getStringArg(1);
1282 LLVM_FALLTHROUGH[[gnu::fallthrough]];
1283 case 1:
1284 Arg1 = Diag.getStringArg(0);
1285 }
1286 Error(Diag.getDiagID(), Arg1, Arg2, Arg3);
1287 });
1288 if (RemainingErr)
1289 Error(toString(std::move(RemainingErr)));
1290}
1291
1292//===----------------------------------------------------------------------===//
1293// Source Manager Deserialization
1294//===----------------------------------------------------------------------===//
1295
1296/// Read the line table in the source manager block.
1297void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) {
1298 unsigned Idx = 0;
1299 LineTableInfo &LineTable = SourceMgr.getLineTable();
1300
1301 // Parse the file names
1302 std::map<int, int> FileIDs;
1303 FileIDs[-1] = -1; // For unspecified filenames.
1304 for (unsigned I = 0; Record[Idx]; ++I) {
1305 // Extract the file name
1306 auto Filename = ReadPath(F, Record, Idx);
1307 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1308 }
1309 ++Idx;
1310
1311 // Parse the line entries
1312 std::vector<LineEntry> Entries;
1313 while (Idx < Record.size()) {
1314 int FID = Record[Idx++];
1315 assert(FID >= 0 && "Serialized line entries for non-local file.")(static_cast<void> (0));
1316 // Remap FileID from 1-based old view.
1317 FID += F.SLocEntryBaseID - 1;
1318
1319 // Extract the line entries
1320 unsigned NumEntries = Record[Idx++];
1321 assert(NumEntries && "no line entries for file ID")(static_cast<void> (0));
1322 Entries.clear();
1323 Entries.reserve(NumEntries);
1324 for (unsigned I = 0; I != NumEntries; ++I) {
1325 unsigned FileOffset = Record[Idx++];
1326 unsigned LineNo = Record[Idx++];
1327 int FilenameID = FileIDs[Record[Idx++]];
1328 SrcMgr::CharacteristicKind FileKind
1329 = (SrcMgr::CharacteristicKind)Record[Idx++];
1330 unsigned IncludeOffset = Record[Idx++];
1331 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1332 FileKind, IncludeOffset));
1333 }
1334 LineTable.AddEntry(FileID::get(FID), Entries);
1335 }
1336}
1337
1338/// Read a source manager block
1339llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1340 using namespace SrcMgr;
1341
1342 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1343
1344 // Set the source-location entry cursor to the current position in
1345 // the stream. This cursor will be used to read the contents of the
1346 // source manager block initially, and then lazily read
1347 // source-location entries as needed.
1348 SLocEntryCursor = F.Stream;
1349
1350 // The stream itself is going to skip over the source manager block.
1351 if (llvm::Error Err = F.Stream.SkipBlock())
1352 return Err;
1353
1354 // Enter the source manager block.
1355 if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID))
1356 return Err;
1357 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1358
1359 RecordData Record;
1360 while (true) {
1361 Expected<llvm::BitstreamEntry> MaybeE =
1362 SLocEntryCursor.advanceSkippingSubblocks();
1363 if (!MaybeE)
1364 return MaybeE.takeError();
1365 llvm::BitstreamEntry E = MaybeE.get();
1366
1367 switch (E.Kind) {
1368 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1369 case llvm::BitstreamEntry::Error:
1370 return llvm::createStringError(std::errc::illegal_byte_sequence,
1371 "malformed block record in AST file");
1372 case llvm::BitstreamEntry::EndBlock:
1373 return llvm::Error::success();
1374 case llvm::BitstreamEntry::Record:
1375 // The interesting case.
1376 break;
1377 }
1378
1379 // Read a record.
1380 Record.clear();
1381 StringRef Blob;
1382 Expected<unsigned> MaybeRecord =
1383 SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1384 if (!MaybeRecord)
1385 return MaybeRecord.takeError();
1386 switch (MaybeRecord.get()) {
1387 default: // Default behavior: ignore.
1388 break;
1389
1390 case SM_SLOC_FILE_ENTRY:
1391 case SM_SLOC_BUFFER_ENTRY:
1392 case SM_SLOC_EXPANSION_ENTRY:
1393 // Once we hit one of the source location entries, we're done.
1394 return llvm::Error::success();
1395 }
1396 }
1397}
1398
1399/// If a header file is not found at the path that we expect it to be
1400/// and the PCH file was moved from its original location, try to resolve the
1401/// file by assuming that header+PCH were moved together and the header is in
1402/// the same place relative to the PCH.
1403static std::string
1404resolveFileRelativeToOriginalDir(const std::string &Filename,
1405 const std::string &OriginalDir,
1406 const std::string &CurrDir) {
1407 assert(OriginalDir != CurrDir &&(static_cast<void> (0))
1408 "No point trying to resolve the file if the PCH dir didn't change")(static_cast<void> (0));
1409
1410 using namespace llvm::sys;
1411
1412 SmallString<128> filePath(Filename);
1413 fs::make_absolute(filePath);
1414 assert(path::is_absolute(OriginalDir))(static_cast<void> (0));
1415 SmallString<128> currPCHPath(CurrDir);
1416
1417 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1418 fileDirE = path::end(path::parent_path(filePath));
1419 path::const_iterator origDirI = path::begin(OriginalDir),
1420 origDirE = path::end(OriginalDir);
1421 // Skip the common path components from filePath and OriginalDir.
1422 while (fileDirI != fileDirE && origDirI != origDirE &&
1423 *fileDirI == *origDirI) {
1424 ++fileDirI;
1425 ++origDirI;
1426 }
1427 for (; origDirI != origDirE; ++origDirI)
1428 path::append(currPCHPath, "..");
1429 path::append(currPCHPath, fileDirI, fileDirE);
1430 path::append(currPCHPath, path::filename(Filename));
1431 return std::string(currPCHPath.str());
1432}
1433
1434bool ASTReader::ReadSLocEntry(int ID) {
1435 if (ID == 0)
1436 return false;
1437
1438 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1439 Error("source location entry ID out-of-range for AST file");
1440 return true;
1441 }
1442
1443 // Local helper to read the (possibly-compressed) buffer data following the
1444 // entry record.
1445 auto ReadBuffer = [this](
1446 BitstreamCursor &SLocEntryCursor,
1447 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1448 RecordData Record;
1449 StringRef Blob;
1450 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1451 if (!MaybeCode) {
1452 Error(MaybeCode.takeError());
1453 return nullptr;
1454 }
1455 unsigned Code = MaybeCode.get();
1456
1457 Expected<unsigned> MaybeRecCode =
1458 SLocEntryCursor.readRecord(Code, Record, &Blob);
1459 if (!MaybeRecCode) {
1460 Error(MaybeRecCode.takeError());
1461 return nullptr;
1462 }
1463 unsigned RecCode = MaybeRecCode.get();
1464
1465 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1466 if (!llvm::zlib::isAvailable()) {
1467 Error("zlib is not available");
1468 return nullptr;
1469 }
1470 SmallString<0> Uncompressed;
1471 if (llvm::Error E =
1472 llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1473 Error("could not decompress embedded file contents: " +
1474 llvm::toString(std::move(E)));
1475 return nullptr;
1476 }
1477 return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1478 } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1479 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1480 } else {
1481 Error("AST record has invalid code");
1482 return nullptr;
1483 }
1484 };
1485
1486 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1487 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1488 F->SLocEntryOffsetsBase +
1489 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1490 Error(std::move(Err));
1491 return true;
1492 }
1493
1494 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1495 SourceLocation::UIntTy BaseOffset = F->SLocEntryBaseOffset;
1496
1497 ++NumSLocEntriesRead;
1498 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1499 if (!MaybeEntry) {
1500 Error(MaybeEntry.takeError());
1501 return true;
1502 }
1503 llvm::BitstreamEntry Entry = MaybeEntry.get();
1504
1505 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1506 Error("incorrectly-formatted source location entry in AST file");
1507 return true;
1508 }
1509
1510 RecordData Record;
1511 StringRef Blob;
1512 Expected<unsigned> MaybeSLOC =
1513 SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1514 if (!MaybeSLOC) {
1515 Error(MaybeSLOC.takeError());
1516 return true;
1517 }
1518 switch (MaybeSLOC.get()) {
1519 default:
1520 Error("incorrectly-formatted source location entry in AST file");
1521 return true;
1522
1523 case SM_SLOC_FILE_ENTRY: {
1524 // We will detect whether a file changed and return 'Failure' for it, but
1525 // we will also try to fail gracefully by setting up the SLocEntry.
1526 unsigned InputID = Record[4];
1527 InputFile IF = getInputFile(*F, InputID);
1528 Optional<FileEntryRef> File = IF.getFile();
1529 bool OverriddenBuffer = IF.isOverridden();
1530
1531 // Note that we only check if a File was returned. If it was out-of-date
1532 // we have complained but we will continue creating a FileID to recover
1533 // gracefully.
1534 if (!File)
1535 return true;
1536
1537 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1538 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1539 // This is the module's main file.
1540 IncludeLoc = getImportLocation(F);
1541 }
1542 SrcMgr::CharacteristicKind
1543 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1544 FileID FID = SourceMgr.createFileID(*File, IncludeLoc, FileCharacter, ID,
1545 BaseOffset + Record[0]);
1546 SrcMgr::FileInfo &FileInfo =
1547 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1548 FileInfo.NumCreatedFIDs = Record[5];
1549 if (Record[3])
1550 FileInfo.setHasLineDirectives();
1551
1552 unsigned NumFileDecls = Record[7];
1553 if (NumFileDecls && ContextObj) {
1554 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1555 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?")(static_cast<void> (0));
1556 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1557 NumFileDecls));
1558 }
1559
1560 const SrcMgr::ContentCache &ContentCache =
1561 SourceMgr.getOrCreateContentCache(*File, isSystem(FileCharacter));
1562 if (OverriddenBuffer && !ContentCache.BufferOverridden &&
1563 ContentCache.ContentsEntry == ContentCache.OrigEntry &&
1564 !ContentCache.getBufferIfLoaded()) {
1565 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1566 if (!Buffer)
1567 return true;
1568 SourceMgr.overrideFileContents(*File, std::move(Buffer));
1569 }
1570
1571 break;
1572 }
1573
1574 case SM_SLOC_BUFFER_ENTRY: {
1575 const char *Name = Blob.data();
1576 unsigned Offset = Record[0];
1577 SrcMgr::CharacteristicKind
1578 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1579 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1580 if (IncludeLoc.isInvalid() && F->isModule()) {
1581 IncludeLoc = getImportLocation(F);
1582 }
1583
1584 auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1585 if (!Buffer)
1586 return true;
1587 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1588 BaseOffset + Offset, IncludeLoc);
1589 break;
1590 }
1591
1592 case SM_SLOC_EXPANSION_ENTRY: {
1593 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1594 SourceMgr.createExpansionLoc(SpellingLoc,
1595 ReadSourceLocation(*F, Record[2]),
1596 ReadSourceLocation(*F, Record[3]),
1597 Record[5],
1598 Record[4],
1599 ID,
1600 BaseOffset + Record[0]);
1601 break;
1602 }
1603 }
1604
1605 return false;
1606}
1607
1608std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1609 if (ID == 0)
1610 return std::make_pair(SourceLocation(), "");
1611
1612 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1613 Error("source location entry ID out-of-range for AST file");
1614 return std::make_pair(SourceLocation(), "");
1615 }
1616
1617 // Find which module file this entry lands in.
1618 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1619 if (!M->isModule())
1620 return std::make_pair(SourceLocation(), "");
1621
1622 // FIXME: Can we map this down to a particular submodule? That would be
1623 // ideal.
1624 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1625}
1626
1627/// Find the location where the module F is imported.
1628SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1629 if (F->ImportLoc.isValid())
1630 return F->ImportLoc;
1631
1632 // Otherwise we have a PCH. It's considered to be "imported" at the first
1633 // location of its includer.
1634 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1635 // Main file is the importer.
1636 assert(SourceMgr.getMainFileID().isValid() && "missing main file")(static_cast<void> (0));
1637 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1638 }
1639 return F->ImportedBy[0]->FirstLoc;
1640}
1641
1642/// Enter a subblock of the specified BlockID with the specified cursor. Read
1643/// the abbreviations that are at the top of the block and then leave the cursor
1644/// pointing into the block.
1645llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor,
1646 unsigned BlockID,
1647 uint64_t *StartOfBlockOffset) {
1648 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID))
1649 return Err;
1650
1651 if (StartOfBlockOffset)
1652 *StartOfBlockOffset = Cursor.GetCurrentBitNo();
1653
1654 while (true) {
1655 uint64_t Offset = Cursor.GetCurrentBitNo();
1656 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1657 if (!MaybeCode)
1658 return MaybeCode.takeError();
1659 unsigned Code = MaybeCode.get();
1660
1661 // We expect all abbrevs to be at the start of the block.
1662 if (Code != llvm::bitc::DEFINE_ABBREV) {
1663 if (llvm::Error Err = Cursor.JumpToBit(Offset))
1664 return Err;
1665 return llvm::Error::success();
1666 }
1667 if (llvm::Error Err = Cursor.ReadAbbrevRecord())
1668 return Err;
1669 }
1670}
1671
1672Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1673 unsigned &Idx) {
1674 Token Tok;
1675 Tok.startToken();
1676 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1677 Tok.setLength(Record[Idx++]);
1678 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1679 Tok.setIdentifierInfo(II);
1680 Tok.setKind((tok::TokenKind)Record[Idx++]);
1681 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1682 return Tok;
1683}
1684
1685MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1686 BitstreamCursor &Stream = F.MacroCursor;
1687
1688 // Keep track of where we are in the stream, then jump back there
1689 // after reading this macro.
1690 SavedStreamPosition SavedPosition(Stream);
1691
1692 if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1693 // FIXME this drops errors on the floor.
1694 consumeError(std::move(Err));
1695 return nullptr;
1696 }
1697 RecordData Record;
1698 SmallVector<IdentifierInfo*, 16> MacroParams;
1699 MacroInfo *Macro = nullptr;
1700
1701 while (true) {
1702 // Advance to the next record, but if we get to the end of the block, don't
1703 // pop it (removing all the abbreviations from the cursor) since we want to
1704 // be able to reseek within the block and read entries.
1705 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1706 Expected<llvm::BitstreamEntry> MaybeEntry =
1707 Stream.advanceSkippingSubblocks(Flags);
1708 if (!MaybeEntry) {
1709 Error(MaybeEntry.takeError());
1710 return Macro;
1711 }
1712 llvm::BitstreamEntry Entry = MaybeEntry.get();
1713
1714 switch (Entry.Kind) {
1715 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1716 case llvm::BitstreamEntry::Error:
1717 Error("malformed block record in AST file");
1718 return Macro;
1719 case llvm::BitstreamEntry::EndBlock:
1720 return Macro;
1721 case llvm::BitstreamEntry::Record:
1722 // The interesting case.
1723 break;
1724 }
1725
1726 // Read a record.
1727 Record.clear();
1728 PreprocessorRecordTypes RecType;
1729 if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1730 RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1731 else {
1732 Error(MaybeRecType.takeError());
1733 return Macro;
1734 }
1735 switch (RecType) {
1736 case PP_MODULE_MACRO:
1737 case PP_MACRO_DIRECTIVE_HISTORY:
1738 return Macro;
1739
1740 case PP_MACRO_OBJECT_LIKE:
1741 case PP_MACRO_FUNCTION_LIKE: {
1742 // If we already have a macro, that means that we've hit the end
1743 // of the definition of the macro we were looking for. We're
1744 // done.
1745 if (Macro)
1746 return Macro;
1747
1748 unsigned NextIndex = 1; // Skip identifier ID.
1749 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1750 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1751 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1752 MI->setIsUsed(Record[NextIndex++]);
1753 MI->setUsedForHeaderGuard(Record[NextIndex++]);
1754
1755 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1756 // Decode function-like macro info.
1757 bool isC99VarArgs = Record[NextIndex++];
1758 bool isGNUVarArgs = Record[NextIndex++];
1759 bool hasCommaPasting = Record[NextIndex++];
1760 MacroParams.clear();
1761 unsigned NumArgs = Record[NextIndex++];
1762 for (unsigned i = 0; i != NumArgs; ++i)
1763 MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1764
1765 // Install function-like macro info.
1766 MI->setIsFunctionLike();
1767 if (isC99VarArgs) MI->setIsC99Varargs();
1768 if (isGNUVarArgs) MI->setIsGNUVarargs();
1769 if (hasCommaPasting) MI->setHasCommaPasting();
1770 MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1771 }
1772
1773 // Remember that we saw this macro last so that we add the tokens that
1774 // form its body to it.
1775 Macro = MI;
1776
1777 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1778 Record[NextIndex]) {
1779 // We have a macro definition. Register the association
1780 PreprocessedEntityID
1781 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1782 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1783 PreprocessingRecord::PPEntityID PPID =
1784 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1785 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1786 PPRec.getPreprocessedEntity(PPID));
1787 if (PPDef)
1788 PPRec.RegisterMacroDefinition(Macro, PPDef);
1789 }
1790
1791 ++NumMacrosRead;
1792 break;
1793 }
1794
1795 case PP_TOKEN: {
1796 // If we see a TOKEN before a PP_MACRO_*, then the file is
1797 // erroneous, just pretend we didn't see this.
1798 if (!Macro) break;
1799
1800 unsigned Idx = 0;
1801 Token Tok = ReadToken(F, Record, Idx);
1802 Macro->AddTokenToBody(Tok);
1803 break;
1804 }
1805 }
1806 }
1807}
1808
1809PreprocessedEntityID
1810ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1811 unsigned LocalID) const {
1812 if (!M.ModuleOffsetMap.empty())
1813 ReadModuleOffsetMap(M);
1814
1815 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1816 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1817 assert(I != M.PreprocessedEntityRemap.end()(static_cast<void> (0))
1818 && "Invalid index into preprocessed entity index remap")(static_cast<void> (0));
1819
1820 return LocalID + I->second;
1821}
1822
1823unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1824 return llvm::hash_combine(ikey.Size, ikey.ModTime);
1825}
1826
1827HeaderFileInfoTrait::internal_key_type
1828HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1829 internal_key_type ikey = {FE->getSize(),
1830 M.HasTimestamps ? FE->getModificationTime() : 0,
1831 FE->getName(), /*Imported*/ false};
1832 return ikey;
1833}
1834
1835bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1836 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1837 return false;
1838
1839 if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1840 return true;
1841
1842 // Determine whether the actual files are equivalent.
1843 FileManager &FileMgr = Reader.getFileManager();
1844 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1845 if (!Key.Imported) {
1846 if (auto File = FileMgr.getFile(Key.Filename))
1847 return *File;
1848 return nullptr;
1849 }
1850
1851 std::string Resolved = std::string(Key.Filename);
1852 Reader.ResolveImportedPath(M, Resolved);
1853 if (auto File = FileMgr.getFile(Resolved))
1854 return *File;
1855 return nullptr;
1856 };
1857
1858 const FileEntry *FEA = GetFile(a);
1859 const FileEntry *FEB = GetFile(b);
1860 return FEA && FEA == FEB;
1861}
1862
1863std::pair<unsigned, unsigned>
1864HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1865 return readULEBKeyDataLength(d);
1866}
1867
1868HeaderFileInfoTrait::internal_key_type
1869HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1870 using namespace llvm::support;
1871
1872 internal_key_type ikey;
1873 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1874 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1875 ikey.Filename = (const char *)d;
1876 ikey.Imported = true;
1877 return ikey;
1878}
1879
1880HeaderFileInfoTrait::data_type
1881HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1882 unsigned DataLen) {
1883 using namespace llvm::support;
1884
1885 const unsigned char *End = d + DataLen;
1886 HeaderFileInfo HFI;
1887 unsigned Flags = *d++;
1888 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1889 HFI.isImport |= (Flags >> 5) & 0x01;
1890 HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1891 HFI.DirInfo = (Flags >> 1) & 0x07;
1892 HFI.IndexHeaderMapHeader = Flags & 0x01;
1893 // FIXME: Find a better way to handle this. Maybe just store a
1894 // "has been included" flag?
1895 HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1896 HFI.NumIncludes);
1897 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1898 M, endian::readNext<uint32_t, little, unaligned>(d));
1899 if (unsigned FrameworkOffset =
1900 endian::readNext<uint32_t, little, unaligned>(d)) {
1901 // The framework offset is 1 greater than the actual offset,
1902 // since 0 is used as an indicator for "no framework name".
1903 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1904 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1905 }
1906
1907 assert((End - d) % 4 == 0 &&(static_cast<void> (0))
1908 "Wrong data length in HeaderFileInfo deserialization")(static_cast<void> (0));
1909 while (d != End) {
1910 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1911 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1912 LocalSMID >>= 2;
1913
1914 // This header is part of a module. Associate it with the module to enable
1915 // implicit module import.
1916 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1917 Module *Mod = Reader.getSubmodule(GlobalSMID);
1918 FileManager &FileMgr = Reader.getFileManager();
1919 ModuleMap &ModMap =
1920 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1921
1922 std::string Filename = std::string(key.Filename);
1923 if (key.Imported)
1924 Reader.ResolveImportedPath(M, Filename);
1925 // FIXME: NameAsWritten
1926 Module::Header H = {std::string(key.Filename), "",
1927 *FileMgr.getFile(Filename)};
1928 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1929 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1930 }
1931
1932 // This HeaderFileInfo was externally loaded.
1933 HFI.External = true;
1934 HFI.IsValid = true;
1935 return HFI;
1936}
1937
1938void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
1939 uint32_t MacroDirectivesOffset) {
1940 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard")(static_cast<void> (0));
1941 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1942}
1943
1944void ASTReader::ReadDefinedMacros() {
1945 // Note that we are loading defined macros.
1946 Deserializing Macros(this);
1947
1948 for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1949 BitstreamCursor &MacroCursor = I.MacroCursor;
1950
1951 // If there was no preprocessor block, skip this file.
1952 if (MacroCursor.getBitcodeBytes().empty())
1953 continue;
1954
1955 BitstreamCursor Cursor = MacroCursor;
1956 if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1957 Error(std::move(Err));
1958 return;
1959 }
1960
1961 RecordData Record;
1962 while (true) {
1963 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1964 if (!MaybeE) {
1965 Error(MaybeE.takeError());
1966 return;
1967 }
1968 llvm::BitstreamEntry E = MaybeE.get();
1969
1970 switch (E.Kind) {
1971 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1972 case llvm::BitstreamEntry::Error:
1973 Error("malformed block record in AST file");
1974 return;
1975 case llvm::BitstreamEntry::EndBlock:
1976 goto NextCursor;
1977
1978 case llvm::BitstreamEntry::Record: {
1979 Record.clear();
1980 Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1981 if (!MaybeRecord) {
1982 Error(MaybeRecord.takeError());
1983 return;
1984 }
1985 switch (MaybeRecord.get()) {
1986 default: // Default behavior: ignore.
1987 break;
1988
1989 case PP_MACRO_OBJECT_LIKE:
1990 case PP_MACRO_FUNCTION_LIKE: {
1991 IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1992 if (II->isOutOfDate())
1993 updateOutOfDateIdentifier(*II);
1994 break;
1995 }
1996
1997 case PP_TOKEN:
1998 // Ignore tokens.
1999 break;
2000 }
2001 break;
2002 }
2003 }
2004 }
2005 NextCursor: ;
2006 }
2007}
2008
2009namespace {
2010
2011 /// Visitor class used to look up identifirs in an AST file.
2012 class IdentifierLookupVisitor {
2013 StringRef Name;
2014 unsigned NameHash;
2015 unsigned PriorGeneration;
2016 unsigned &NumIdentifierLookups;
2017 unsigned &NumIdentifierLookupHits;
2018 IdentifierInfo *Found = nullptr;
2019
2020 public:
2021 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2022 unsigned &NumIdentifierLookups,
2023 unsigned &NumIdentifierLookupHits)
2024 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2025 PriorGeneration(PriorGeneration),
2026 NumIdentifierLookups(NumIdentifierLookups),
2027 NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2028
2029 bool operator()(ModuleFile &M) {
2030 // If we've already searched this module file, skip it now.
2031 if (M.Generation <= PriorGeneration)
2032 return true;
2033
2034 ASTIdentifierLookupTable *IdTable
2035 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2036 if (!IdTable)
2037 return false;
2038
2039 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2040 Found);
2041 ++NumIdentifierLookups;
2042 ASTIdentifierLookupTable::iterator Pos =
2043 IdTable->find_hashed(Name, NameHash, &Trait);
2044 if (Pos == IdTable->end())
2045 return false;
2046
2047 // Dereferencing the iterator has the effect of building the
2048 // IdentifierInfo node and populating it with the various
2049 // declarations it needs.
2050 ++NumIdentifierLookupHits;
2051 Found = *Pos;
2052 return true;
2053 }
2054
2055 // Retrieve the identifier info found within the module
2056 // files.
2057 IdentifierInfo *getIdentifierInfo() const { return Found; }
2058 };
2059
2060} // namespace
2061
2062void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2063 // Note that we are loading an identifier.
2064 Deserializing AnIdentifier(this);
2065
2066 unsigned PriorGeneration = 0;
2067 if (getContext().getLangOpts().Modules)
2068 PriorGeneration = IdentifierGeneration[&II];
2069
2070 // If there is a global index, look there first to determine which modules
2071 // provably do not have any results for this identifier.
2072 GlobalModuleIndex::HitSet Hits;
2073 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2074 if (!loadGlobalIndex()) {
2075 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2076 HitsPtr = &Hits;
2077 }
2078 }
2079
2080 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2081 NumIdentifierLookups,
2082 NumIdentifierLookupHits);
2083 ModuleMgr.visit(Visitor, HitsPtr);
2084 markIdentifierUpToDate(&II);
2085}
2086
2087void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2088 if (!II)
2089 return;
2090
2091 II->setOutOfDate(false);
2092
2093 // Update the generation for this identifier.
2094 if (getContext().getLangOpts().Modules)
2095 IdentifierGeneration[II] = getGeneration();
2096}
2097
2098void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2099 const PendingMacroInfo &PMInfo) {
2100 ModuleFile &M = *PMInfo.M;
2101
2102 BitstreamCursor &Cursor = M.MacroCursor;
2103 SavedStreamPosition SavedPosition(Cursor);
2104 if (llvm::Error Err =
2105 Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2106 Error(std::move(Err));
2107 return;
2108 }
2109
2110 struct ModuleMacroRecord {
2111 SubmoduleID SubModID;
2112 MacroInfo *MI;
2113 SmallVector<SubmoduleID, 8> Overrides;
2114 };
2115 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2116
2117 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2118 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2119 // macro histroy.
2120 RecordData Record;
2121 while (true) {
2122 Expected<llvm::BitstreamEntry> MaybeEntry =
2123 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2124 if (!MaybeEntry) {
2125 Error(MaybeEntry.takeError());
2126 return;
2127 }
2128 llvm::BitstreamEntry Entry = MaybeEntry.get();
2129
2130 if (Entry.Kind != llvm::BitstreamEntry::Record) {
2131 Error("malformed block record in AST file");
2132 return;
2133 }
2134
2135 Record.clear();
2136 Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2137 if (!MaybePP) {
2138 Error(MaybePP.takeError());
2139 return;
2140 }
2141 switch ((PreprocessorRecordTypes)MaybePP.get()) {
2142 case PP_MACRO_DIRECTIVE_HISTORY:
2143 break;
2144
2145 case PP_MODULE_MACRO: {
2146 ModuleMacros.push_back(ModuleMacroRecord());
2147 auto &Info = ModuleMacros.back();
2148 Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2149 Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2150 for (int I = 2, N = Record.size(); I != N; ++I)
2151 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2152 continue;
2153 }
2154
2155 default:
2156 Error("malformed block record in AST file");
2157 return;
2158 }
2159
2160 // We found the macro directive history; that's the last record
2161 // for this macro.
2162 break;
2163 }
2164
2165 // Module macros are listed in reverse dependency order.
2166 {
2167 std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2168 llvm::SmallVector<ModuleMacro*, 8> Overrides;
2169 for (auto &MMR : ModuleMacros) {
2170 Overrides.clear();
2171 for (unsigned ModID : MMR.Overrides) {
2172 Module *Mod = getSubmodule(ModID);
2173 auto *Macro = PP.getModuleMacro(Mod, II);
2174 assert(Macro && "missing definition for overridden macro")(static_cast<void> (0));
2175 Overrides.push_back(Macro);
2176 }
2177
2178 bool Inserted = false;
2179 Module *Owner = getSubmodule(MMR.SubModID);
2180 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2181 }
2182 }
2183
2184 // Don't read the directive history for a module; we don't have anywhere
2185 // to put it.
2186 if (M.isModule())
2187 return;
2188
2189 // Deserialize the macro directives history in reverse source-order.
2190 MacroDirective *Latest = nullptr, *Earliest = nullptr;
2191 unsigned Idx = 0, N = Record.size();
2192 while (Idx < N) {
2193 MacroDirective *MD = nullptr;
2194 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2195 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2196 switch (K) {
2197 case MacroDirective::MD_Define: {
2198 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2199 MD = PP.AllocateDefMacroDirective(MI, Loc);
2200 break;
2201 }
2202 case MacroDirective::MD_Undefine:
2203 MD = PP.AllocateUndefMacroDirective(Loc);
2204 break;
2205 case MacroDirective::MD_Visibility:
2206 bool isPublic = Record[Idx++];
2207 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2208 break;
2209 }
2210
2211 if (!Latest)
2212 Latest = MD;
2213 if (Earliest)
2214 Earliest->setPrevious(MD);
2215 Earliest = MD;
2216 }
2217
2218 if (Latest)
2219 PP.setLoadedMacroDirective(II, Earliest, Latest);
2220}
2221
2222bool ASTReader::shouldDisableValidationForFile(
2223 const serialization::ModuleFile &M) const {
2224 if (DisableValidationKind == DisableValidationForModuleKind::None)
2225 return false;
2226
2227 // If a PCH is loaded and validation is disabled for PCH then disable
2228 // validation for the PCH and the modules it loads.
2229 ModuleKind K = CurrentDeserializingModuleKind.getValueOr(M.Kind);
2230
2231 switch (K) {
2232 case MK_MainFile:
2233 case MK_Preamble:
2234 case MK_PCH:
2235 return bool(DisableValidationKind & DisableValidationForModuleKind::PCH);
2236 case MK_ImplicitModule:
2237 case MK_ExplicitModule:
2238 case MK_PrebuiltModule:
2239 return bool(DisableValidationKind & DisableValidationForModuleKind::Module);
2240 }
2241
2242 return false;
2243}
2244
2245ASTReader::InputFileInfo
2246ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2247 // Go find this input file.
2248 BitstreamCursor &Cursor = F.InputFilesCursor;
2249 SavedStreamPosition SavedPosition(Cursor);
2250 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2251 // FIXME this drops errors on the floor.
2252 consumeError(std::move(Err));
2253 }
2254
2255 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2256 if (!MaybeCode) {
2257 // FIXME this drops errors on the floor.
2258 consumeError(MaybeCode.takeError());
2259 }
2260 unsigned Code = MaybeCode.get();
2261 RecordData Record;
2262 StringRef Blob;
2263
2264 if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2265 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&(static_cast<void> (0))
2266 "invalid record type for input file")(static_cast<void> (0));
2267 else {
2268 // FIXME this drops errors on the floor.
2269 consumeError(Maybe.takeError());
2270 }
2271
2272 assert(Record[0] == ID && "Bogus stored ID or offset")(static_cast<void> (0));
2273 InputFileInfo R;
2274 R.StoredSize = static_cast<off_t>(Record[1]);
2275 R.StoredTime = static_cast<time_t>(Record[2]);
2276 R.Overridden = static_cast<bool>(Record[3]);
2277 R.Transient = static_cast<bool>(Record[4]);
2278 R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2279 R.Filename = std::string(Blob);
2280 ResolveImportedPath(F, R.Filename);
2281
2282 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2283 if (!MaybeEntry) // FIXME this drops errors on the floor.
2284 consumeError(MaybeEntry.takeError());
2285 llvm::BitstreamEntry Entry = MaybeEntry.get();
2286 assert(Entry.Kind == llvm::BitstreamEntry::Record &&(static_cast<void> (0))
2287 "expected record type for input file hash")(static_cast<void> (0));
2288
2289 Record.clear();
2290 if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2291 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&(static_cast<void> (0))
2292 "invalid record type for input file hash")(static_cast<void> (0));
2293 else {
2294 // FIXME this drops errors on the floor.
2295 consumeError(Maybe.takeError());
2296 }
2297 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2298 static_cast<uint64_t>(Record[0]);
2299 return R;
2300}
2301
2302static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2303InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2304 // If this ID is bogus, just return an empty input file.
2305 if (ID == 0 || ID > F.InputFilesLoaded.size())
2306 return InputFile();
2307
2308 // If we've already loaded this input file, return it.
2309 if (F.InputFilesLoaded[ID-1].getFile())
2310 return F.InputFilesLoaded[ID-1];
2311
2312 if (F.InputFilesLoaded[ID-1].isNotFound())
2313 return InputFile();
2314
2315 // Go find this input file.
2316 BitstreamCursor &Cursor = F.InputFilesCursor;
2317 SavedStreamPosition SavedPosition(Cursor);
2318 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2319 // FIXME this drops errors on the floor.
2320 consumeError(std::move(Err));
2321 }
2322
2323 InputFileInfo FI = readInputFileInfo(F, ID);
2324 off_t StoredSize = FI.StoredSize;
2325 time_t StoredTime = FI.StoredTime;
2326 bool Overridden = FI.Overridden;
2327 bool Transient = FI.Transient;
2328 StringRef Filename = FI.Filename;
2329 uint64_t StoredContentHash = FI.ContentHash;
2330
2331 OptionalFileEntryRefDegradesToFileEntryPtr File =
2332 expectedToOptional(FileMgr.getFileRef(Filename, /*OpenFile=*/false));
2333
2334 // If we didn't find the file, resolve it relative to the
2335 // original directory from which this AST file was created.
2336 if (!File && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2337 F.OriginalDir != F.BaseDirectory) {
2338 std::string Resolved = resolveFileRelativeToOriginalDir(
2339 std::string(Filename), F.OriginalDir, F.BaseDirectory);
2340 if (!Resolved.empty())
2341 File = expectedToOptional(FileMgr.getFileRef(Resolved));
2342 }
2343
2344 // For an overridden file, create a virtual file with the stored
2345 // size/timestamp.
2346 if ((Overridden || Transient) && !File)
2347 File = FileMgr.getVirtualFileRef(Filename, StoredSize, StoredTime);
2348
2349 if (!File) {
2350 if (Complain) {
2351 std::string ErrorStr = "could not find file '";
2352 ErrorStr += Filename;
2353 ErrorStr += "' referenced by AST file '";
2354 ErrorStr += F.FileName;
2355 ErrorStr += "'";
2356 Error(ErrorStr);
2357 }
2358 // Record that we didn't find the file.
2359 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2360 return InputFile();
2361 }
2362
2363 // Check if there was a request to override the contents of the file
2364 // that was part of the precompiled header. Overriding such a file
2365 // can lead to problems when lexing using the source locations from the
2366 // PCH.
2367 SourceManager &SM = getSourceManager();
2368 // FIXME: Reject if the overrides are different.
2369 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2370 if (Complain)
2371 Error(diag::err_fe_pch_file_overridden, Filename);
2372
2373 // After emitting the diagnostic, bypass the overriding file to recover
2374 // (this creates a separate FileEntry).
2375 File = SM.bypassFileContentsOverride(*File);
2376 if (!File) {
2377 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2378 return InputFile();
2379 }
2380 }
2381
2382 struct Change {
2383 enum ModificationKind {
2384 Size,
2385 ModTime,
2386 Content,
2387 None,
2388 } Kind;
2389 llvm::Optional<int64_t> Old = llvm::None;
2390 llvm::Optional<int64_t> New = llvm::None;
2391 };
2392 auto HasInputFileChanged = [&]() {
2393 if (StoredSize != File->getSize())
2394 return Change{Change::Size, StoredSize, File->getSize()};
2395 if (!shouldDisableValidationForFile(F) && StoredTime &&
2396 StoredTime != File->getModificationTime()) {
2397 Change MTimeChange = {Change::ModTime, StoredTime,
2398 File->getModificationTime()};
2399
2400 // In case the modification time changes but not the content,
2401 // accept the cached file as legit.
2402 if (ValidateASTInputFilesContent &&
2403 StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) {
2404 auto MemBuffOrError = FileMgr.getBufferForFile(File);
2405 if (!MemBuffOrError) {
2406 if (!Complain)
2407 return MTimeChange;
2408 std::string ErrorStr = "could not get buffer for file '";
2409 ErrorStr += File->getName();
2410 ErrorStr += "'";
2411 Error(ErrorStr);
2412 return MTimeChange;
2413 }
2414
2415 // FIXME: hash_value is not guaranteed to be stable!
2416 auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2417 if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2418 return Change{Change::None};
2419
2420 return Change{Change::Content};
2421 }
2422 return MTimeChange;
2423 }
2424 return Change{Change::None};
2425 };
2426
2427 bool IsOutOfDate = false;
2428 auto FileChange = HasInputFileChanged();
2429 // For an overridden file, there is nothing to validate.
2430 if (!Overridden && FileChange.Kind != Change::None) {
2431 if (Complain && !Diags.isDiagnosticInFlight()) {
2432 // Build a list of the PCH imports that got us here (in reverse).
2433 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2434 while (!ImportStack.back()->ImportedBy.empty())
2435 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2436
2437 // The top-level PCH is stale.
2438 StringRef TopLevelPCHName(ImportStack.back()->FileName);
2439 Diag(diag::err_fe_ast_file_modified)
2440 << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind)
2441 << TopLevelPCHName << FileChange.Kind
2442 << (FileChange.Old && FileChange.New)
2443 << llvm::itostr(FileChange.Old.getValueOr(0))
2444 << llvm::itostr(FileChange.New.getValueOr(0));
2445
2446 // Print the import stack.
2447 if (ImportStack.size() > 1) {
2448 Diag(diag::note_pch_required_by)
2449 << Filename << ImportStack[0]->FileName;
2450 for (unsigned I = 1; I < ImportStack.size(); ++I)
2451 Diag(diag::note_pch_required_by)
2452 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2453 }
2454
2455 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2456 }
2457
2458 IsOutOfDate = true;
2459 }
2460 // FIXME: If the file is overridden and we've already opened it,
2461 // issue an error (or split it into a separate FileEntry).
2462
2463 InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate);
2464
2465 // Note that we've loaded this input file.
2466 F.InputFilesLoaded[ID-1] = IF;
2467 return IF;
2468}
2469
2470/// If we are loading a relocatable PCH or module file, and the filename
2471/// is not an absolute path, add the system or module root to the beginning of
2472/// the file name.
2473void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2474 // Resolve relative to the base directory, if we have one.
2475 if (!M.BaseDirectory.empty())
2476 return ResolveImportedPath(Filename, M.BaseDirectory);
2477}
2478
2479void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2480 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2481 return;
2482
2483 SmallString<128> Buffer;
2484 llvm::sys::path::append(Buffer, Prefix, Filename);
2485 Filename.assign(Buffer.begin(), Buffer.end());
2486}
2487
2488static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2489 switch (ARR) {
2490 case ASTReader::Failure: return true;
2491 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2492 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2493 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2494 case ASTReader::ConfigurationMismatch:
2495 return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2496 case ASTReader::HadErrors: return true;
2497 case ASTReader::Success: return false;
2498 }
2499
2500 llvm_unreachable("unknown ASTReadResult")__builtin_unreachable();
2501}
2502
2503ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2504 BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2505 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2506 std::string &SuggestedPredefines) {
2507 if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2508 // FIXME this drops errors on the floor.
2509 consumeError(std::move(Err));
2510 return Failure;
2511 }
2512
2513 // Read all of the records in the options block.
2514 RecordData Record;
2515 ASTReadResult Result = Success;
2516 while (true) {
2517 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2518 if (!MaybeEntry) {
2519 // FIXME this drops errors on the floor.
2520 consumeError(MaybeEntry.takeError());
2521 return Failure;
2522 }
2523 llvm::BitstreamEntry Entry = MaybeEntry.get();
2524
2525 switch (Entry.Kind) {
2526 case llvm::BitstreamEntry::Error:
2527 case llvm::BitstreamEntry::SubBlock:
2528 return Failure;
2529
2530 case llvm::BitstreamEntry::EndBlock:
2531 return Result;
2532
2533 case llvm::BitstreamEntry::Record:
2534 // The interesting case.
2535 break;
2536 }
2537
2538 // Read and process a record.
2539 Record.clear();
2540 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2541 if (!MaybeRecordType) {
2542 // FIXME this drops errors on the floor.
2543 consumeError(MaybeRecordType.takeError());
2544 return Failure;
2545 }
2546 switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2547 case LANGUAGE_OPTIONS: {
2548 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2549 if (ParseLanguageOptions(Record, Complain, Listener,
2550 AllowCompatibleConfigurationMismatch))
2551 Result = ConfigurationMismatch;
2552 break;
2553 }
2554
2555 case TARGET_OPTIONS: {
2556 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2557 if (ParseTargetOptions(Record, Complain, Listener,
2558 AllowCompatibleConfigurationMismatch))
2559 Result = ConfigurationMismatch;
2560 break;
2561 }
2562
2563 case FILE_SYSTEM_OPTIONS: {
2564 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2565 if (!AllowCompatibleConfigurationMismatch &&
2566 ParseFileSystemOptions(Record, Complain, Listener))
2567 Result = ConfigurationMismatch;
2568 break;
2569 }
2570
2571 case HEADER_SEARCH_OPTIONS: {
2572 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2573 if (!AllowCompatibleConfigurationMismatch &&
2574 ParseHeaderSearchOptions(Record, Complain, Listener))
2575 Result = ConfigurationMismatch;
2576 break;
2577 }
2578
2579 case PREPROCESSOR_OPTIONS:
2580 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2581 if (!AllowCompatibleConfigurationMismatch &&
2582 ParsePreprocessorOptions(Record, Complain, Listener,
2583 SuggestedPredefines))
2584 Result = ConfigurationMismatch;
2585 break;
2586 }
2587 }
2588}
2589
2590ASTReader::ASTReadResult
2591ASTReader::ReadControlBlock(ModuleFile &F,
2592 SmallVectorImpl<ImportedModule> &Loaded,
2593 const ModuleFile *ImportedBy,
2594 unsigned ClientLoadCapabilities) {
2595 BitstreamCursor &Stream = F.Stream;
2596
2597 if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2598 Error(std::move(Err));
2599 return Failure;
2600 }
2601
2602 // Lambda to read the unhashed control block the first time it's called.
2603 //
2604 // For PCM files, the unhashed control block cannot be read until after the
2605 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still
2606 // need to look ahead before reading the IMPORTS record. For consistency,
2607 // this block is always read somehow (see BitstreamEntry::EndBlock).
2608 bool HasReadUnhashedControlBlock = false;
2609 auto readUnhashedControlBlockOnce = [&]() {
2610 if (!HasReadUnhashedControlBlock) {
2611 HasReadUnhashedControlBlock = true;
2612 if (ASTReadResult Result =
2613 readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2614 return Result;
2615 }
2616 return Success;
2617 };
2618
2619 bool DisableValidation = shouldDisableValidationForFile(F);
2620
2621 // Read all of the records and blocks in the control block.
2622 RecordData Record;
2623 unsigned NumInputs = 0;
2624 unsigned NumUserInputs = 0;
2625 StringRef BaseDirectoryAsWritten;
2626 while (true) {
2627 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2628 if (!MaybeEntry) {
2629 Error(MaybeEntry.takeError());
2630 return Failure;
2631 }
2632 llvm::BitstreamEntry Entry = MaybeEntry.get();
2633
2634 switch (Entry.Kind) {
2635 case llvm::BitstreamEntry::Error:
2636 Error("malformed block record in AST file");
2637 return Failure;
2638 case llvm::BitstreamEntry::EndBlock: {
2639 // Validate the module before returning. This call catches an AST with
2640 // no module name and no imports.
2641 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2642 return Result;
2643
2644 // Validate input files.
2645 const HeaderSearchOptions &HSOpts =
2646 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2647
2648 // All user input files reside at the index range [0, NumUserInputs), and
2649 // system input files reside at [NumUserInputs, NumInputs). For explicitly
2650 // loaded module files, ignore missing inputs.
2651 if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2652 F.Kind != MK_PrebuiltModule) {
2653 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2654
2655 // If we are reading a module, we will create a verification timestamp,
2656 // so we verify all input files. Otherwise, verify only user input
2657 // files.
2658
2659 unsigned N = NumUserInputs;
2660 if (ValidateSystemInputs ||
2661 (HSOpts.ModulesValidateOncePerBuildSession &&
2662 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2663 F.Kind == MK_ImplicitModule))
2664 N = NumInputs;
2665
2666 for (unsigned I = 0; I < N; ++I) {
2667 InputFile IF = getInputFile(F, I+1, Complain);
2668 if (!IF.getFile() || IF.isOutOfDate())
2669 return OutOfDate;
2670 }
2671 }
2672
2673 if (Listener)
2674 Listener->visitModuleFile(F.FileName, F.Kind);
2675
2676 if (Listener && Listener->needsInputFileVisitation()) {
2677 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2678 : NumUserInputs;
2679 for (unsigned I = 0; I < N; ++I) {
2680 bool IsSystem = I >= NumUserInputs;
2681 InputFileInfo FI = readInputFileInfo(F, I+1);
2682 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2683 F.Kind == MK_ExplicitModule ||
2684 F.Kind == MK_PrebuiltModule);
2685 }
2686 }
2687
2688 return Success;
2689 }
2690
2691 case llvm::BitstreamEntry::SubBlock:
2692 switch (Entry.ID) {
2693 case INPUT_FILES_BLOCK_ID:
2694 F.InputFilesCursor = Stream;
2695 if (llvm::Error Err = Stream.SkipBlock()) {
2696 Error(std::move(Err));
2697 return Failure;
2698 }
2699 if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2700 Error("malformed block record in AST file");
2701 return Failure;
2702 }
2703 continue;
2704
2705 case OPTIONS_BLOCK_ID:
2706 // If we're reading the first module for this group, check its options
2707 // are compatible with ours. For modules it imports, no further checking
2708 // is required, because we checked them when we built it.
2709 if (Listener && !ImportedBy) {
2710 // Should we allow the configuration of the module file to differ from
2711 // the configuration of the current translation unit in a compatible
2712 // way?
2713 //
2714 // FIXME: Allow this for files explicitly specified with -include-pch.
2715 bool AllowCompatibleConfigurationMismatch =
2716 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2717
2718 ASTReadResult Result =
2719 ReadOptionsBlock(Stream, ClientLoadCapabilities,
2720 AllowCompatibleConfigurationMismatch, *Listener,
2721 SuggestedPredefines);
2722 if (Result == Failure) {
2723 Error("malformed block record in AST file");
2724 return Result;
2725 }
2726
2727 if (DisableValidation ||
2728 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2729 Result = Success;
2730
2731 // If we can't load the module, exit early since we likely
2732 // will rebuild the module anyway. The stream may be in the
2733 // middle of a block.
2734 if (Result != Success)
2735 return Result;
2736 } else if (llvm::Error Err = Stream.SkipBlock()) {
2737 Error(std::move(Err));
2738 return Failure;
2739 }
2740 continue;
2741
2742 default:
2743 if (llvm::Error Err = Stream.SkipBlock()) {
2744 Error(std::move(Err));
2745 return Failure;
2746 }
2747 continue;
2748 }
2749
2750 case llvm::BitstreamEntry::Record:
2751 // The interesting case.
2752 break;
2753 }
2754
2755 // Read and process a record.
2756 Record.clear();
2757 StringRef Blob;
2758 Expected<unsigned> MaybeRecordType =
2759 Stream.readRecord(Entry.ID, Record, &Blob);
2760 if (!MaybeRecordType) {
2761 Error(MaybeRecordType.takeError());
2762 return Failure;
2763 }
2764 switch ((ControlRecordTypes)MaybeRecordType.get()) {
2765 case METADATA: {
2766 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2767 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2768 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2769 : diag::err_pch_version_too_new);
2770 return VersionMismatch;
2771 }
2772
2773 bool hasErrors = Record[6];
2774 if (hasErrors && !DisableValidation) {
2775 // If requested by the caller and the module hasn't already been read
2776 // or compiled, mark modules on error as out-of-date.
2777 if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) &&
2778 canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
2779 return OutOfDate;
2780
2781 if (!AllowASTWithCompilerErrors) {
2782 Diag(diag::err_pch_with_compiler_errors);
2783 return HadErrors;
2784 }
2785 }
2786 if (hasErrors) {
2787 Diags.ErrorOccurred = true;
2788 Diags.UncompilableErrorOccurred = true;
2789 Diags.UnrecoverableErrorOccurred = true;
2790 }
2791
2792 F.RelocatablePCH = Record[4];
2793 // Relative paths in a relocatable PCH are relative to our sysroot.
2794 if (F.RelocatablePCH)
2795 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2796
2797 F.HasTimestamps = Record[5];
2798
2799 const std::string &CurBranch = getClangFullRepositoryVersion();
2800 StringRef ASTBranch = Blob;
2801 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2802 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2803 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2804 return VersionMismatch;
2805 }
2806 break;
2807 }
2808
2809 case IMPORTS: {
2810 // Validate the AST before processing any imports (otherwise, untangling
2811 // them can be error-prone and expensive). A module will have a name and
2812 // will already have been validated, but this catches the PCH case.
2813 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2814 return Result;
2815
2816 // Load each of the imported PCH files.
2817 unsigned Idx = 0, N = Record.size();
2818 while (Idx < N) {
2819 // Read information about the AST file.
2820 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2821 // The import location will be the local one for now; we will adjust
2822 // all import locations of module imports after the global source
2823 // location info are setup, in ReadAST.
2824 SourceLocation ImportLoc =
2825 ReadUntranslatedSourceLocation(Record[Idx++]);
2826 off_t StoredSize = (off_t)Record[Idx++];
2827 time_t StoredModTime = (time_t)Record[Idx++];
2828 auto FirstSignatureByte = Record.begin() + Idx;
2829 ASTFileSignature StoredSignature = ASTFileSignature::create(
2830 FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size);
2831 Idx += ASTFileSignature::size;
2832
2833 std::string ImportedName = ReadString(Record, Idx);
2834 std::string ImportedFile;
2835
2836 // For prebuilt and explicit modules first consult the file map for
2837 // an override. Note that here we don't search prebuilt module
2838 // directories, only the explicit name to file mappings. Also, we will
2839 // still verify the size/signature making sure it is essentially the
2840 // same file but perhaps in a different location.
2841 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2842 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2843 ImportedName, /*FileMapOnly*/ true);
2844
2845 if (ImportedFile.empty())
2846 // Use BaseDirectoryAsWritten to ensure we use the same path in the
2847 // ModuleCache as when writing.
2848 ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2849 else
2850 SkipPath(Record, Idx);
2851
2852 // If our client can't cope with us being out of date, we can't cope with
2853 // our dependency being missing.
2854 unsigned Capabilities = ClientLoadCapabilities;
2855 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2856 Capabilities &= ~ARR_Missing;
2857
2858 // Load the AST file.
2859 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2860 Loaded, StoredSize, StoredModTime,
2861 StoredSignature, Capabilities);
2862
2863 // If we diagnosed a problem, produce a backtrace.
2864 bool recompilingFinalized =
2865 Result == OutOfDate && (Capabilities & ARR_OutOfDate) &&
2866 getModuleManager().getModuleCache().isPCMFinal(F.FileName);
2867 if (isDiagnosedResult(Result, Capabilities) || recompilingFinalized)
2868 Diag(diag::note_module_file_imported_by)
2869 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2870 if (recompilingFinalized)
2871 Diag(diag::note_module_file_conflict);
2872
2873 switch (Result) {
2874 case Failure: return Failure;
2875 // If we have to ignore the dependency, we'll have to ignore this too.
2876 case Missing:
2877 case OutOfDate: return OutOfDate;
2878 case VersionMismatch: return VersionMismatch;
2879 case ConfigurationMismatch: return ConfigurationMismatch;
2880 case HadErrors: return HadErrors;
2881 case Success: break;
2882 }
2883 }
2884 break;
2885 }
2886
2887 case ORIGINAL_FILE:
2888 F.OriginalSourceFileID = FileID::get(Record[0]);
2889 F.ActualOriginalSourceFileName = std::string(Blob);
2890 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2891 ResolveImportedPath(F, F.OriginalSourceFileName);
2892 break;
2893
2894 case ORIGINAL_FILE_ID:
2895 F.OriginalSourceFileID = FileID::get(Record[0]);
2896 break;
2897
2898 case ORIGINAL_PCH_DIR:
2899 F.OriginalDir = std::string(Blob);
2900 break;
2901
2902 case MODULE_NAME:
2903 F.ModuleName = std::string(Blob);
2904 Diag(diag::remark_module_import)
2905 << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2906 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2907 if (Listener)
2908 Listener->ReadModuleName(F.ModuleName);
2909
2910 // Validate the AST as soon as we have a name so we can exit early on
2911 // failure.
2912 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2913 return Result;
2914
2915 break;
2916
2917 case MODULE_DIRECTORY: {
2918 // Save the BaseDirectory as written in the PCM for computing the module
2919 // filename for the ModuleCache.
2920 BaseDirectoryAsWritten = Blob;
2921 assert(!F.ModuleName.empty() &&(static_cast<void> (0))
2922 "MODULE_DIRECTORY found before MODULE_NAME")(static_cast<void> (0));
2923 // If we've already loaded a module map file covering this module, we may
2924 // have a better path for it (relative to the current build).
2925 Module *M = PP.getHeaderSearchInfo().lookupModule(
2926 F.ModuleName, /*AllowSearch*/ true,
2927 /*AllowExtraModuleMapSearch*/ true);
2928 if (M && M->Directory) {
2929 // If we're implicitly loading a module, the base directory can't
2930 // change between the build and use.
2931 // Don't emit module relocation error if we have -fno-validate-pch
2932 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
2933 DisableValidationForModuleKind::Module) &&
2934 F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2935 auto BuildDir = PP.getFileManager().getDirectory(Blob);
2936 if (!BuildDir || *BuildDir != M->Directory) {
2937 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
2938 Diag(diag::err_imported_module_relocated)
2939 << F.ModuleName << Blob << M->Directory->getName();
2940 return OutOfDate;
2941 }
2942 }
2943 F.BaseDirectory = std::string(M->Directory->getName());
2944 } else {
2945 F.BaseDirectory = std::string(Blob);
2946 }
2947 break;
2948 }
2949
2950 case MODULE_MAP_FILE:
2951 if (ASTReadResult Result =
2952 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2953 return Result;
2954 break;
2955
2956 case INPUT_FILE_OFFSETS:
2957 NumInputs = Record[0];
2958 NumUserInputs = Record[1];
2959 F.InputFileOffsets =
2960 (const llvm::support::unaligned_uint64_t *)Blob.data();
2961 F.InputFilesLoaded.resize(NumInputs);
2962 F.NumUserInputFiles = NumUserInputs;
2963 break;
2964 }
2965 }
2966}
2967
2968llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
2969 unsigned ClientLoadCapabilities) {
2970 BitstreamCursor &Stream = F.Stream;
2971
2972 if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID))
2973 return Err;
2974 F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
2975
2976 // Read all of the records and blocks for the AST file.
2977 RecordData Record;
2978 while (true) {
2979 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2980 if (!MaybeEntry)
2981 return MaybeEntry.takeError();
2982 llvm::BitstreamEntry Entry = MaybeEntry.get();
2983
2984 switch (Entry.Kind) {
2985 case llvm::BitstreamEntry::Error:
2986 return llvm::createStringError(
2987 std::errc::illegal_byte_sequence,
2988 "error at end of module block in AST file");
2989 case llvm::BitstreamEntry::EndBlock:
2990 // Outside of C++, we do not store a lookup map for the translation unit.
2991 // Instead, mark it as needing a lookup map to be built if this module
2992 // contains any declarations lexically within it (which it always does!).
2993 // This usually has no cost, since we very rarely need the lookup map for
2994 // the translation unit outside C++.
2995 if (ASTContext *Ctx = ContextObj) {
2996 DeclContext *DC = Ctx->getTranslationUnitDecl();
2997 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2998 DC->setMustBuildLookupTable();
2999 }
3000
3001 return llvm::Error::success();
3002 case llvm::BitstreamEntry::SubBlock:
3003 switch (Entry.ID) {
3004 case DECLTYPES_BLOCK_ID:
3005 // We lazily load the decls block, but we want to set up the
3006 // DeclsCursor cursor to point into it. Clone our current bitcode
3007 // cursor to it, enter the block and read the abbrevs in that block.
3008 // With the main cursor, we just skip over it.
3009 F.DeclsCursor = Stream;
3010 if (llvm::Error Err = Stream.SkipBlock())
3011 return Err;
3012 if (llvm::Error Err = ReadBlockAbbrevs(
3013 F.DeclsCursor, DECLTYPES_BLOCK_ID, &F.DeclsBlockStartOffset))
3014 return Err;
3015 break;
3016
3017 case PREPROCESSOR_BLOCK_ID:
3018 F.MacroCursor = Stream;
3019 if (!PP.getExternalSource())
3020 PP.setExternalSource(this);
3021
3022 if (llvm::Error Err = Stream.SkipBlock())
3023 return Err;
3024 if (llvm::Error Err =
3025 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID))
3026 return Err;
3027 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3028 break;
3029
3030 case PREPROCESSOR_DETAIL_BLOCK_ID:
3031 F.PreprocessorDetailCursor = Stream;
3032
3033 if (llvm::Error Err = Stream.SkipBlock()) {
3034 return Err;
3035 }
3036 if (llvm::Error Err = ReadBlockAbbrevs(F.PreprocessorDetailCursor,
3037 PREPROCESSOR_DETAIL_BLOCK_ID))
3038 return Err;
3039 F.PreprocessorDetailStartOffset
3040 = F.PreprocessorDetailCursor.GetCurrentBitNo();
3041
3042 if (!PP.getPreprocessingRecord())
3043 PP.createPreprocessingRecord();
3044 if (!PP.getPreprocessingRecord()->getExternalSource())
3045 PP.getPreprocessingRecord()->SetExternalSource(*this);
3046 break;
3047
3048 case SOURCE_MANAGER_BLOCK_ID:
3049 if (llvm::Error Err = ReadSourceManagerBlock(F))
3050 return Err;
3051 break;
3052
3053 case SUBMODULE_BLOCK_ID:
3054 if (llvm::Error Err = ReadSubmoduleBlock(F, ClientLoadCapabilities))
3055 return Err;
3056 break;
3057
3058 case COMMENTS_BLOCK_ID: {
3059 BitstreamCursor C = Stream;
3060
3061 if (llvm::Error Err = Stream.SkipBlock())
3062 return Err;
3063 if (llvm::Error Err = ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID))
3064 return Err;
3065 CommentsCursors.push_back(std::make_pair(C, &F));
3066 break;
3067 }
3068
3069 default:
3070 if (llvm::Error Err = Stream.SkipBlock())
3071 return Err;
3072 break;
3073 }
3074 continue;
3075
3076 case llvm::BitstreamEntry::Record:
3077 // The interesting case.
3078 break;
3079 }
3080
3081 // Read and process a record.
3082 Record.clear();
3083 StringRef Blob;
3084 Expected<unsigned> MaybeRecordType =
3085 Stream.readRecord(Entry.ID, Record, &Blob);
3086 if (!MaybeRecordType)
3087 return MaybeRecordType.takeError();
3088 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3089
3090 // If we're not loading an AST context, we don't care about most records.
3091 if (!ContextObj) {
3092 switch (RecordType) {
3093 case IDENTIFIER_TABLE:
3094 case IDENTIFIER_OFFSET:
3095 case INTERESTING_IDENTIFIERS:
3096 case STATISTICS:
3097 case PP_CONDITIONAL_STACK:
3098 case PP_COUNTER_VALUE:
3099 case SOURCE_LOCATION_OFFSETS:
3100 case MODULE_OFFSET_MAP:
3101 case SOURCE_MANAGER_LINE_TABLE:
3102 case SOURCE_LOCATION_PRELOADS:
3103 case PPD_ENTITIES_OFFSETS:
3104 case HEADER_SEARCH_TABLE:
3105 case IMPORTED_MODULES:
3106 case MACRO_OFFSET:
3107 break;
3108 default:
3109 continue;
3110 }
3111 }
3112
3113 switch (RecordType) {
3114 default: // Default behavior: ignore.
3115 break;
3116
3117 case TYPE_OFFSET: {
3118 if (F.LocalNumTypes != 0)
3119 return llvm::createStringError(
3120 std::errc::illegal_byte_sequence,
3121 "duplicate TYPE_OFFSET record in AST file");
3122 F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data());
3123 F.LocalNumTypes = Record[0];
3124 unsigned LocalBaseTypeIndex = Record[1];
3125 F.BaseTypeIndex = getTotalNumTypes();
3126
3127 if (F.LocalNumTypes > 0) {
3128 // Introduce the global -> local mapping for types within this module.
3129 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3130
3131 // Introduce the local -> global mapping for types within this module.
3132 F.TypeRemap.insertOrReplace(
3133 std::make_pair(LocalBaseTypeIndex,
3134 F.BaseTypeIndex - LocalBaseTypeIndex));
3135
3136 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3137 }
3138 break;
3139 }
3140
3141 case DECL_OFFSET: {
3142 if (F.LocalNumDecls != 0)
3143 return llvm::createStringError(
3144 std::errc::illegal_byte_sequence,
3145 "duplicate DECL_OFFSET record in AST file");
3146 F.DeclOffsets = (const DeclOffset *)Blob.data();
3147 F.LocalNumDecls = Record[0];
3148 unsigned LocalBaseDeclID = Record[1];
3149 F.BaseDeclID = getTotalNumDecls();
3150
3151 if (F.LocalNumDecls > 0) {
3152 // Introduce the global -> local mapping for declarations within this
3153 // module.
3154 GlobalDeclMap.insert(
3155 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3156
3157 // Introduce the local -> global mapping for declarations within this
3158 // module.
3159 F.DeclRemap.insertOrReplace(
3160 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3161
3162 // Introduce the global -> local mapping for declarations within this
3163 // module.
3164 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3165
3166 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3167 }
3168 break;
3169 }
3170
3171 case TU_UPDATE_LEXICAL: {
3172 DeclContext *TU = ContextObj->getTranslationUnitDecl();
3173 LexicalContents Contents(
3174 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3175 Blob.data()),
3176 static_cast<unsigned int>(Blob.size() / 4));
3177 TULexicalDecls.push_back(std::make_pair(&F, Contents));
3178 TU->setHasExternalLexicalStorage(true);
3179 break;
3180 }
3181
3182 case UPDATE_VISIBLE: {
3183 unsigned Idx = 0;
3184 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3185 auto *Data = (const unsigned char*)Blob.data();
3186 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3187 // If we've already loaded the decl, perform the updates when we finish
3188 // loading this block.
3189 if (Decl *D = GetExistingDecl(ID))
3190 PendingUpdateRecords.push_back(
3191 PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3192 break;
3193 }
3194
3195 case IDENTIFIER_TABLE:
3196 F.IdentifierTableData =
3197 reinterpret_cast<const unsigned char *>(Blob.data());
3198 if (Record[0]) {
3199 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3200 F.IdentifierTableData + Record[0],
3201 F.IdentifierTableData + sizeof(uint32_t),
3202 F.IdentifierTableData,
3203 ASTIdentifierLookupTrait(*this, F));
3204
3205 PP.getIdentifierTable().setExternalIdentifierLookup(this);
3206 }
3207 break;
3208
3209 case IDENTIFIER_OFFSET: {
3210 if (F.LocalNumIdentifiers != 0)
3211 return llvm::createStringError(
3212 std::errc::illegal_byte_sequence,
3213 "duplicate IDENTIFIER_OFFSET record in AST file");
3214 F.IdentifierOffsets = (const uint32_t *)Blob.data();
3215 F.LocalNumIdentifiers = Record[0];
3216 unsigned LocalBaseIdentifierID = Record[1];
3217 F.BaseIdentifierID = getTotalNumIdentifiers();
3218
3219 if (F.LocalNumIdentifiers > 0) {
3220 // Introduce the global -> local mapping for identifiers within this
3221 // module.
3222 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3223 &F));
3224
3225 // Introduce the local -> global mapping for identifiers within this
3226 // module.
3227 F.IdentifierRemap.insertOrReplace(
3228 std::make_pair(LocalBaseIdentifierID,
3229 F.BaseIdentifierID - LocalBaseIdentifierID));
3230
3231 IdentifiersLoaded.resize(IdentifiersLoaded.size()
3232 + F.LocalNumIdentifiers);
3233 }
3234 break;
3235 }
3236
3237 case INTERESTING_IDENTIFIERS:
3238 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3239 break;
3240
3241 case EAGERLY_DESERIALIZED_DECLS:
3242 // FIXME: Skip reading this record if our ASTConsumer doesn't care
3243 // about "interesting" decls (for instance, if we're building a module).
3244 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3245 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3246 break;
3247
3248 case MODULAR_CODEGEN_DECLS:
3249 // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3250 // them (ie: if we're not codegenerating this module).
3251 if (F.Kind == MK_MainFile ||
3252 getContext().getLangOpts().BuildingPCHWithObjectFile)
3253 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3254 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3255 break;
3256
3257 case SPECIAL_TYPES:
3258 if (SpecialTypes.empty()) {
3259 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3260 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3261 break;
3262 }
3263
3264 if (SpecialTypes.size() != Record.size())
3265 return llvm::createStringError(std::errc::illegal_byte_sequence,
3266 "invalid special-types record");
3267
3268 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3269 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3270 if (!SpecialTypes[I])
3271 SpecialTypes[I] = ID;
3272 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3273 // merge step?
3274 }
3275 break;
3276
3277 case STATISTICS:
3278 TotalNumStatements += Record[0];
3279 TotalNumMacros += Record[1];
3280 TotalLexicalDeclContexts += Record[2];
3281 TotalVisibleDeclContexts += Record[3];
3282 break;
3283
3284 case UNUSED_FILESCOPED_DECLS:
3285 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3286 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3287 break;
3288
3289 case DELEGATING_CTORS:
3290 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3291 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3292 break;
3293
3294 case WEAK_UNDECLARED_IDENTIFIERS:
3295 if (Record.size() % 4 != 0)
3296 return llvm::createStringError(std::errc::illegal_byte_sequence,
3297 "invalid weak identifiers record");
3298
3299 // FIXME: Ignore weak undeclared identifiers from non-original PCH
3300 // files. This isn't the way to do it :)
3301 WeakUndeclaredIdentifiers.clear();
3302
3303 // Translate the weak, undeclared identifiers into global IDs.
3304 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3305 WeakUndeclaredIdentifiers.push_back(
3306 getGlobalIdentifierID(F, Record[I++]));
3307 WeakUndeclaredIdentifiers.push_back(
3308 getGlobalIdentifierID(F, Record[I++]));
3309 WeakUndeclaredIdentifiers.push_back(
3310 ReadSourceLocation(F, Record, I).getRawEncoding());
3311 WeakUndeclaredIdentifiers.push_back(Record[I++]);
3312 }
3313 break;
3314
3315 case SELECTOR_OFFSETS: {
3316 F.SelectorOffsets = (const uint32_t *)Blob.data();
3317 F.LocalNumSelectors = Record[0];
3318 unsigned LocalBaseSelectorID = Record[1];
3319 F.BaseSelectorID = getTotalNumSelectors();
3320
3321 if (F.LocalNumSelectors > 0) {
3322 // Introduce the global -> local mapping for selectors within this
3323 // module.
3324 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3325
3326 // Introduce the local -> global mapping for selectors within this
3327 // module.
3328 F.SelectorRemap.insertOrReplace(
3329 std::make_pair(LocalBaseSelectorID,
3330 F.BaseSelectorID - LocalBaseSelectorID));
3331
3332 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3333 }
3334 break;
3335 }
3336
3337 case METHOD_POOL:
3338 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3339 if (Record[0])
3340 F.SelectorLookupTable
3341 = ASTSelectorLookupTable::Create(
3342 F.SelectorLookupTableData + Record[0],
3343 F.SelectorLookupTableData,
3344 ASTSelectorLookupTrait(*this, F));
3345 TotalNumMethodPoolEntries += Record[1];
3346 break;
3347
3348 case REFERENCED_SELECTOR_POOL:
3349 if (!Record.empty()) {
3350 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3351 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3352 Record[Idx++]));
3353 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3354 getRawEncoding());
3355 }
3356 }
3357 break;
3358
3359 case PP_CONDITIONAL_STACK:
3360 if (!Record.empty()) {
3361 unsigned Idx = 0, End = Record.size() - 1;
3362 bool ReachedEOFWhileSkipping = Record[Idx++];
3363 llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3364 if (ReachedEOFWhileSkipping) {
3365 SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3366 SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3367 bool FoundNonSkipPortion = Record[Idx++];
3368 bool FoundElse = Record[Idx++];
3369 SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3370 SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3371 FoundElse, ElseLoc);
3372 }
3373 SmallVector<PPConditionalInfo, 4> ConditionalStack;
3374 while (Idx < End) {
3375 auto Loc = ReadSourceLocation(F, Record, Idx);
3376 bool WasSkipping = Record[Idx++];
3377 bool FoundNonSkip = Record[Idx++];
3378 bool FoundElse = Record[Idx++];
3379 ConditionalStack.push_back(
3380 {Loc, WasSkipping, FoundNonSkip, FoundElse});
3381 }
3382 PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3383 }
3384 break;
3385
3386 case PP_COUNTER_VALUE:
3387 if (!Record.empty() && Listener)
3388 Listener->ReadCounter(F, Record[0]);
3389 break;
3390
3391 case FILE_SORTED_DECLS:
3392 F.FileSortedDecls = (const DeclID *)Blob.data();
3393 F.NumFileSortedDecls = Record[0];
3394 break;
3395
3396 case SOURCE_LOCATION_OFFSETS: {
3397 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3398 F.LocalNumSLocEntries = Record[0];
3399 SourceLocation::UIntTy SLocSpaceSize = Record[1];
3400 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
3401 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3402 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3403 SLocSpaceSize);
3404 if (!F.SLocEntryBaseID)
3405 return llvm::createStringError(std::errc::invalid_argument,
3406 "ran out of source locations");
3407 // Make our entry in the range map. BaseID is negative and growing, so
3408 // we invert it. Because we invert it, though, we need the other end of
3409 // the range.
3410 unsigned RangeStart =
3411 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3412 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3413 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3414
3415 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3416 assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0)(static_cast<void> (0));
3417 GlobalSLocOffsetMap.insert(
3418 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3419 - SLocSpaceSize,&F));
3420
3421 // Initialize the remapping table.
3422 // Invalid stays invalid.
3423 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3424 // This module. Base was 2 when being compiled.
3425 F.SLocRemap.insertOrReplace(std::make_pair(
3426 2U, static_cast<SourceLocation::IntTy>(F.SLocEntryBaseOffset - 2)));
3427
3428 TotalNumSLocEntries += F.LocalNumSLocEntries;
3429 break;
3430 }
3431
3432 case MODULE_OFFSET_MAP:
3433 F.ModuleOffsetMap = Blob;
3434 break;
3435
3436 case SOURCE_MANAGER_LINE_TABLE:
3437 ParseLineTable(F, Record);
3438 break;
3439
3440 case SOURCE_LOCATION_PRELOADS: {
3441 // Need to transform from the local view (1-based IDs) to the global view,
3442 // which is based off F.SLocEntryBaseID.
3443 if (!F.PreloadSLocEntries.empty())
3444 return llvm::createStringError(
3445 std::errc::illegal_byte_sequence,
3446 "Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3447
3448 F.PreloadSLocEntries.swap(Record);
3449 break;
3450 }
3451
3452 case EXT_VECTOR_DECLS:
3453 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3454 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3455 break;
3456
3457 case VTABLE_USES:
3458 if (Record.size() % 3 != 0)
3459 return llvm::createStringError(std::errc::illegal_byte_sequence,
3460 "Invalid VTABLE_USES record");
3461
3462 // Later tables overwrite earlier ones.
3463 // FIXME: Modules will have some trouble with this. This is clearly not
3464 // the right way to do this.
3465 VTableUses.clear();
3466
3467 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3468 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3469 VTableUses.push_back(
3470 ReadSourceLocation(F, Record, Idx).getRawEncoding());
3471 VTableUses.push_back(Record[Idx++]);
3472 }
3473 break;
3474
3475 case PENDING_IMPLICIT_INSTANTIATIONS:
3476 if (PendingInstantiations.size() % 2 != 0)
3477 return llvm::createStringError(
3478 std::errc::illegal_byte_sequence,
3479 "Invalid existing PendingInstantiations");
3480
3481 if (Record.size() % 2 != 0)
3482 return llvm::createStringError(
3483 std::errc::illegal_byte_sequence,
3484 "Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3485
3486 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3487 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3488 PendingInstantiations.push_back(
3489 ReadSourceLocation(F, Record, I).getRawEncoding());
3490 }
3491 break;
3492
3493 case SEMA_DECL_REFS:
3494 if (Record.size() != 3)
3495 return llvm::createStringError(std::errc::illegal_byte_sequence,
3496 "Invalid SEMA_DECL_REFS block");
3497 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3498 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3499 break;
3500
3501 case PPD_ENTITIES_OFFSETS: {
3502 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3503 assert(Blob.size() % sizeof(PPEntityOffset) == 0)(static_cast<void> (0));
3504 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3505
3506 unsigned LocalBasePreprocessedEntityID = Record[0];
3507
3508 unsigned StartingID;
3509 if (!PP.getPreprocessingRecord())
3510 PP.createPreprocessingRecord();
3511 if (!PP.getPreprocessingRecord()->getExternalSource())
3512 PP.getPreprocessingRecord()->SetExternalSource(*this);
3513 StartingID
3514 = PP.getPreprocessingRecord()
3515 ->allocateLoadedEntities(F.NumPreprocessedEntities);
3516 F.BasePreprocessedEntityID = StartingID;
3517
3518 if (F.NumPreprocessedEntities > 0) {
3519 // Introduce the global -> local mapping for preprocessed entities in
3520 // this module.
3521 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3522
3523 // Introduce the local -> global mapping for preprocessed entities in
3524 // this module.
3525 F.PreprocessedEntityRemap.insertOrReplace(
3526 std::make_pair(LocalBasePreprocessedEntityID,
3527 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3528 }
3529
3530 break;
3531 }
3532
3533 case PPD_SKIPPED_RANGES: {
3534 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3535 assert(Blob.size() % sizeof(PPSkippedRange) == 0)(static_cast<void> (0));
3536 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3537
3538 if (!PP.getPreprocessingRecord())
3539 PP.createPreprocessingRecord();
3540 if (!PP.getPreprocessingRecord()->getExternalSource())
3541 PP.getPreprocessingRecord()->SetExternalSource(*this);
3542 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3543 ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3544
3545 if (F.NumPreprocessedSkippedRanges > 0)
3546 GlobalSkippedRangeMap.insert(
3547 std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3548 break;
3549 }
3550
3551 case DECL_UPDATE_OFFSETS:
3552 if (Record.size() % 2 != 0)
3553 return llvm::createStringError(
3554 std::errc::illegal_byte_sequence,
3555 "invalid DECL_UPDATE_OFFSETS block in AST file");
3556 for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3557 GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3558 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3559
3560 // If we've already loaded the decl, perform the updates when we finish
3561 // loading this block.
3562 if (Decl *D = GetExistingDecl(ID))
3563 PendingUpdateRecords.push_back(
3564 PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3565 }
3566 break;
3567
3568 case OBJC_CATEGORIES_MAP:
3569 if (F.LocalNumObjCCategoriesInMap != 0)
3570 return llvm::createStringError(
3571 std::errc::illegal_byte_sequence,
3572 "duplicate OBJC_CATEGORIES_MAP record in AST file");
3573
3574 F.LocalNumObjCCategoriesInMap = Record[0];
3575 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3576 break;
3577
3578 case OBJC_CATEGORIES:
3579 F.ObjCCategories.swap(Record);
3580 break;
3581
3582 case CUDA_SPECIAL_DECL_REFS:
3583 // Later tables overwrite earlier ones.
3584 // FIXME: Modules will have trouble with this.
3585 CUDASpecialDeclRefs.clear();
3586 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3587 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3588 break;
3589
3590 case HEADER_SEARCH_TABLE:
3591 F.HeaderFileInfoTableData = Blob.data();
3592 F.LocalNumHeaderFileInfos = Record[1];
3593 if (Record[0]) {
3594 F.HeaderFileInfoTable
3595 = HeaderFileInfoLookupTable::Create(
3596 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3597 (const unsigned char *)F.HeaderFileInfoTableData,
3598 HeaderFileInfoTrait(*this, F,
3599 &PP.getHeaderSearchInfo(),
3600 Blob.data() + Record[2]));
3601
3602 PP.getHeaderSearchInfo().SetExternalSource(this);
3603 if (!PP.getHeaderSearchInfo().getExternalLookup())
3604 PP.getHeaderSearchInfo().SetExternalLookup(this);
3605 }
3606 break;
3607
3608 case FP_PRAGMA_OPTIONS:
3609 // Later tables overwrite earlier ones.
3610 FPPragmaOptions.swap(Record);
3611 break;
3612
3613 case OPENCL_EXTENSIONS:
3614 for (unsigned I = 0, E = Record.size(); I != E; ) {
3615 auto Name = ReadString(Record, I);
3616 auto &OptInfo = OpenCLExtensions.OptMap[Name];
3617 OptInfo.Supported = Record[I++] != 0;
3618 OptInfo.Enabled = Record[I++] != 0;
3619 OptInfo.WithPragma = Record[I++] != 0;
3620 OptInfo.Avail = Record[I++];
3621 OptInfo.Core = Record[I++];
3622 OptInfo.Opt = Record[I++];
3623 }
3624 break;
3625
3626 case TENTATIVE_DEFINITIONS:
3627 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3628 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3629 break;
3630
3631 case KNOWN_NAMESPACES:
3632 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3633 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3634 break;
3635
3636 case UNDEFINED_BUT_USED:
3637 if (UndefinedButUsed.size() % 2 != 0)
3638 return llvm::createStringError(std::errc::illegal_byte_sequence,
3639 "Invalid existing UndefinedButUsed");
3640
3641 if (Record.size() % 2 != 0)
3642 return llvm::createStringError(std::errc::illegal_byte_sequence,
3643 "invalid undefined-but-used record");
3644 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3645 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3646 UndefinedButUsed.push_back(
3647 ReadSourceLocation(F, Record, I).getRawEncoding());
3648 }
3649 break;
3650
3651 case DELETE_EXPRS_TO_ANALYZE:
3652 for (unsigned I = 0, N = Record.size(); I != N;) {
3653 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3654 const uint64_t Count = Record[I++];
3655 DelayedDeleteExprs.push_back(Count);
3656 for (uint64_t C = 0; C < Count; ++C) {
3657 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3658 bool IsArrayForm = Record[I++] == 1;
3659 DelayedDeleteExprs.push_back(IsArrayForm);
3660 }
3661 }
3662 break;
3663
3664 case IMPORTED_MODULES:
3665 if (!F.isModule()) {
3666 // If we aren't loading a module (which has its own exports), make
3667 // all of the imported modules visible.
3668 // FIXME: Deal with macros-only imports.
3669 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3670 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3671 SourceLocation Loc = ReadSourceLocation(F, Record, I);
3672 if (GlobalID) {
3673 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3674 if (DeserializationListener)
3675 DeserializationListener->ModuleImportRead(GlobalID, Loc);
3676 }
3677 }
3678 }
3679 break;
3680
3681 case MACRO_OFFSET: {
3682 if (F.LocalNumMacros != 0)
3683 return llvm::createStringError(
3684 std::errc::illegal_byte_sequence,
3685 "duplicate MACRO_OFFSET record in AST file");
3686 F.MacroOffsets = (const uint32_t *)Blob.data();
3687 F.LocalNumMacros = Record[0];
3688 unsigned LocalBaseMacroID = Record[1];
3689 F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset;
3690 F.BaseMacroID = getTotalNumMacros();
3691
3692 if (F.LocalNumMacros > 0) {
3693 // Introduce the global -> local mapping for macros within this module.
3694 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3695
3696 // Introduce the local -> global mapping for macros within this module.
3697 F.MacroRemap.insertOrReplace(
3698 std::make_pair(LocalBaseMacroID,
3699 F.BaseMacroID - LocalBaseMacroID));
3700
3701 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3702 }
3703 break;
3704 }
3705
3706 case LATE_PARSED_TEMPLATE:
3707 LateParsedTemplates.emplace_back(
3708 std::piecewise_construct, std::forward_as_tuple(&F),
3709 std::forward_as_tuple(Record.begin(), Record.end()));
3710 break;
3711
3712 case OPTIMIZE_PRAGMA_OPTIONS:
3713 if (Record.size() != 1)
3714 return llvm::createStringError(std::errc::illegal_byte_sequence,
3715 "invalid pragma optimize record");
3716 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3717 break;
3718
3719 case MSSTRUCT_PRAGMA_OPTIONS:
3720 if (Record.size() != 1)
3721 return llvm::createStringError(std::errc::illegal_byte_sequence,
3722 "invalid pragma ms_struct record");
3723 PragmaMSStructState = Record[0];
3724 break;
3725
3726 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3727 if (Record.size() != 2)
3728 return llvm::createStringError(
3729 std::errc::illegal_byte_sequence,
3730 "invalid pragma pointers to members record");
3731 PragmaMSPointersToMembersState = Record[0];
3732 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3733 break;
3734
3735 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3736 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3737 UnusedLocalTypedefNameCandidates.push_back(
3738 getGlobalDeclID(F, Record[I]));
3739 break;
3740
3741 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3742 if (Record.size() != 1)
3743 return llvm::createStringError(std::errc::illegal_byte_sequence,
3744 "invalid cuda pragma options record");
3745 ForceCUDAHostDeviceDepth = Record[0];
3746 break;
3747
3748 case ALIGN_PACK_PRAGMA_OPTIONS: {
3749 if (Record.size() < 3)
3750 return llvm::createStringError(std::errc::illegal_byte_sequence,
3751 "invalid pragma pack record");
3752 PragmaAlignPackCurrentValue = ReadAlignPackInfo(Record[0]);
3753 PragmaAlignPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3754 unsigned NumStackEntries = Record[2];
3755 unsigned Idx = 3;
3756 // Reset the stack when importing a new module.
3757 PragmaAlignPackStack.clear();
3758 for (unsigned I = 0; I < NumStackEntries; ++I) {
3759 PragmaAlignPackStackEntry Entry;
3760 Entry.Value = ReadAlignPackInfo(Record[Idx++]);
3761 Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3762 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3763 PragmaAlignPackStrings.push_back(ReadString(Record, Idx));
3764 Entry.SlotLabel = PragmaAlignPackStrings.back();
3765 PragmaAlignPackStack.push_back(Entry);
3766 }
3767 break;
3768 }
3769
3770 case FLOAT_CONTROL_PRAGMA_OPTIONS: {
3771 if (Record.size() < 3)
3772 return llvm::createStringError(std::errc::illegal_byte_sequence,
3773 "invalid pragma float control record");
3774 FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]);
3775 FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]);
3776 unsigned NumStackEntries = Record[2];
3777 unsigned Idx = 3;
3778 // Reset the stack when importing a new module.
3779 FpPragmaStack.clear();
3780 for (unsigned I = 0; I < NumStackEntries; ++I) {
3781 FpPragmaStackEntry Entry;
3782 Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]);
3783 Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3784 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3785 FpPragmaStrings.push_back(ReadString(Record, Idx));
3786 Entry.SlotLabel = FpPragmaStrings.back();
3787 FpPragmaStack.push_back(Entry);
3788 }
3789 break;
3790 }
3791
3792 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
3793 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3794 DeclsToCheckForDeferredDiags.insert(getGlobalDeclID(F, Record[I]));
3795 break;
3796 }
3797 }
3798}
3799
3800void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3801 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read")(static_cast<void> (0));
3802
3803 // Additional remapping information.
3804 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3805 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3806 F.ModuleOffsetMap = StringRef();
3807
3808 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3809 if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3810 F.SLocRemap.insert(std::make_pair(0U, 0));
3811 F.SLocRemap.insert(std::make_pair(2U, 1));
3812 }
3813
3814 // Continuous range maps we may be updating in our module.
3815 using SLocRemapBuilder =
3816 ContinuousRangeMap<SourceLocation::UIntTy, SourceLocation::IntTy,
3817 2>::Builder;
3818 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3819 SLocRemapBuilder SLocRemap(F.SLocRemap);
3820 RemapBuilder IdentifierRemap(F.IdentifierRemap);
3821 RemapBuilder MacroRemap(F.MacroRemap);
3822 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3823 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3824 RemapBuilder SelectorRemap(F.SelectorRemap);
3825 RemapBuilder DeclRemap(F.DeclRemap);
3826 RemapBuilder TypeRemap(F.TypeRemap);
3827
3828 while (Data < DataEnd) {
3829 // FIXME: Looking up dependency modules by filename is horrible. Let's
3830 // start fixing this with prebuilt, explicit and implicit modules and see
3831 // how it goes...
3832 using namespace llvm::support;
3833 ModuleKind Kind = static_cast<ModuleKind>(
3834 endian::readNext<uint8_t, little, unaligned>(Data));
3835 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3836 StringRef Name = StringRef((const char*)Data, Len);
3837 Data += Len;
3838 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
3839 Kind == MK_ImplicitModule
3840 ? ModuleMgr.lookupByModuleName(Name)
3841 : ModuleMgr.lookupByFileName(Name));
3842 if (!OM) {
3843 std::string Msg =
3844 "SourceLocation remap refers to unknown module, cannot find ";
3845 Msg.append(std::string(Name));
3846 Error(Msg);
3847 return;
3848 }
3849
3850 SourceLocation::UIntTy SLocOffset =
3851 endian::readNext<uint32_t, little, unaligned>(Data);
3852 uint32_t IdentifierIDOffset =
3853 endian::readNext<uint32_t, little, unaligned>(Data);
3854 uint32_t MacroIDOffset =
3855 endian::readNext<uint32_t, little, unaligned>(Data);
3856 uint32_t PreprocessedEntityIDOffset =
3857 endian::readNext<uint32_t, little, unaligned>(Data);
3858 uint32_t SubmoduleIDOffset =
3859 endian::readNext<uint32_t, little, unaligned>(Data);
3860 uint32_t SelectorIDOffset =
3861 endian::readNext<uint32_t, little, unaligned>(Data);
3862 uint32_t DeclIDOffset =
3863 endian::readNext<uint32_t, little, unaligned>(Data);
3864 uint32_t TypeIndexOffset =
3865 endian::readNext<uint32_t, little, unaligned>(Data);
3866
3867 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3868 RemapBuilder &Remap) {
3869 constexpr uint32_t None = std::numeric_limits<uint32_t>::max();
3870 if (Offset != None)
3871 Remap.insert(std::make_pair(Offset,
3872 static_cast<int>(BaseOffset - Offset)));
3873 };
3874
3875 constexpr SourceLocation::UIntTy SLocNone =
3876 std::numeric_limits<SourceLocation::UIntTy>::max();
3877 if (SLocOffset != SLocNone)
3878 SLocRemap.insert(std::make_pair(
3879 SLocOffset, static_cast<SourceLocation::IntTy>(
3880 OM->SLocEntryBaseOffset - SLocOffset)));
3881
3882 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3883 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3884 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3885 PreprocessedEntityRemap);
3886 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3887 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3888 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3889 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3890
3891 // Global -> local mappings.
3892 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3893 }
3894}
3895
3896ASTReader::ASTReadResult
3897ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3898 const ModuleFile *ImportedBy,
3899 unsigned ClientLoadCapabilities) {
3900 unsigned Idx = 0;
3901 F.ModuleMapPath = ReadPath(F, Record, Idx);
3902
3903 // Try to resolve ModuleName in the current header search context and
3904 // verify that it is found in the same module map file as we saved. If the
3905 // top-level AST file is a main file, skip this check because there is no
3906 // usable header search context.
3907 assert(!F.ModuleName.empty() &&(static_cast<void> (0))
3908 "MODULE_NAME should come before MODULE_MAP_FILE")(static_cast<void> (0));
3909 if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3910 // An implicitly-loaded module file should have its module listed in some
3911 // module map file that we've already loaded.
3912 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3913 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3914 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3915 // Don't emit module relocation error if we have -fno-validate-pch
3916 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
3917 DisableValidationForModuleKind::Module) &&
3918 !ModMap) {
3919 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) {
3920 if (auto ASTFE = M ? M->getASTFile() : None) {
3921 // This module was defined by an imported (explicit) module.
3922 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3923 << ASTFE->getName();
3924 } else {
3925 // This module was built with a different module map.
3926 Diag(diag::err_imported_module_not_found)
3927 << F.ModuleName << F.FileName
3928 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3929 << !ImportedBy;
3930 // In case it was imported by a PCH, there's a chance the user is
3931 // just missing to include the search path to the directory containing
3932 // the modulemap.
3933 if (ImportedBy && ImportedBy->Kind == MK_PCH)
3934 Diag(diag::note_imported_by_pch_module_not_found)
3935 << llvm::sys::path::parent_path(F.ModuleMapPath);
3936 }
3937 }
3938 return OutOfDate;
3939 }
3940
3941 assert(M && M->Name == F.ModuleName && "found module with different name")(static_cast<void> (0));
3942
3943 // Check the primary module map file.
3944 auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3945 if (!StoredModMap || *StoredModMap != ModMap) {
3946 assert(ModMap && "found module is missing module map file")(static_cast<void> (0));
3947 assert((ImportedBy || F.Kind == MK_ImplicitModule) &&(static_cast<void> (0))
3948 "top-level import should be verified")(static_cast<void> (0));
3949 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
3950 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3951 Diag(diag::err_imported_module_modmap_changed)
3952 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3953 << ModMap->getName() << F.ModuleMapPath << NotImported;
3954 return OutOfDate;
3955 }
3956
3957 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3958 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3959 // FIXME: we should use input files rather than storing names.
3960 std::string Filename = ReadPath(F, Record, Idx);
3961 auto SF = FileMgr.getFile(Filename, false, false);
3962 if (!SF) {
3963 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3964 Error("could not find file '" + Filename +"' referenced by AST file");
3965 return OutOfDate;
3966 }
3967 AdditionalStoredMaps.insert(*SF);
3968 }
3969
3970 // Check any additional module map files (e.g. module.private.modulemap)
3971 // that are not in the pcm.
3972 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3973 for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3974 // Remove files that match
3975 // Note: SmallPtrSet::erase is really remove
3976 if (!AdditionalStoredMaps.erase(ModMap)) {
3977 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3978 Diag(diag::err_module_different_modmap)
3979 << F.ModuleName << /*new*/0 << ModMap->getName();
3980 return OutOfDate;
3981 }
3982 }
3983 }
3984
3985 // Check any additional module map files that are in the pcm, but not
3986 // found in header search. Cases that match are already removed.
3987 for (const FileEntry *ModMap : AdditionalStoredMaps) {
3988 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3989 Diag(diag::err_module_different_modmap)
3990 << F.ModuleName << /*not new*/1 << ModMap->getName();
3991 return OutOfDate;
3992 }
3993 }
3994
3995 if (Listener)
3996 Listener->ReadModuleMapFile(F.ModuleMapPath);
3997 return Success;
3998}
3999
4000/// Move the given method to the back of the global list of methods.
4001static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4002 // Find the entry for this selector in the method pool.
4003 Sema::GlobalMethodPool::iterator Known
4004 = S.MethodPool.find(Method->getSelector());
4005 if (Known == S.MethodPool.end())
4006 return;
4007
4008 // Retrieve the appropriate method list.
4009 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4010 : Known->second.second;
4011 bool Found = false;
4012 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4013 if (!Found) {
4014 if (List->getMethod() == Method) {
4015 Found = true;
4016 } else {
4017 // Keep searching.
4018 continue;
4019 }
4020 }
4021
4022 if (List->getNext())
4023 List->setMethod(List->getNext()->getMethod());
4024 else
4025 List->setMethod(Method);
4026 }
4027}
4028
4029void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4030 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?")(static_cast<void> (0));
4031 for (Decl *D : Names) {
4032 bool wasHidden = !D->isUnconditionallyVisible();
4033 D->setVisibleDespiteOwningModule();
4034
4035 if (wasHidden && SemaObj) {
4036 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4037 moveMethodToBackOfGlobalList(*SemaObj, Method);
4038 }
4039 }
4040 }
4041}
4042
4043void ASTReader::makeModuleVisible(Module *Mod,
4044 Module::NameVisibilityKind NameVisibility,
4045 SourceLocation ImportLoc) {
4046 llvm::SmallPtrSet<Module *, 4> Visited;
4047 SmallVector<Module *, 4> Stack;
4048 Stack.push_back(Mod);
4049 while (!Stack.empty()) {
4050 Mod = Stack.pop_back_val();
4051
4052 if (NameVisibility <= Mod->NameVisibility) {
4053 // This module already has this level of visibility (or greater), so
4054 // there is nothing more to do.
4055 continue;
4056 }
4057
4058 if (Mod->isUnimportable()) {
4059 // Modules that aren't importable cannot be made visible.
4060 continue;
4061 }
4062
4063 // Update the module's name visibility.
4064 Mod->NameVisibility = NameVisibility;
4065
4066 // If we've already deserialized any names from this module,
4067 // mark them as visible.
4068 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4069 if (Hidden != HiddenNamesMap.end()) {
4070 auto HiddenNames = std::move(*Hidden);
4071 HiddenNamesMap.erase(Hidden);
4072 makeNamesVisible(HiddenNames.second, HiddenNames.first);
4073 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&(static_cast<void> (0))
4074 "making names visible added hidden names")(static_cast<void> (0));
4075 }
4076
4077 // Push any exported modules onto the stack to be marked as visible.
4078 SmallVector<Module *, 16> Exports;
4079 Mod->getExportedModules(Exports);
4080 for (SmallVectorImpl<Module *>::iterator
4081 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4082 Module *Exported = *I;
4083 if (Visited.insert(Exported).second)
4084 Stack.push_back(Exported);
4085 }
4086 }
4087}
4088
4089/// We've merged the definition \p MergedDef into the existing definition
4090/// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4091/// visible.
4092void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4093 NamedDecl *MergedDef) {
4094 if (!Def->isUnconditionallyVisible()) {
4095 // If MergedDef is visible or becomes visible, make the definition visible.
4096 if (MergedDef->isUnconditionallyVisible())
4097 Def->setVisibleDespiteOwningModule();
4098 else {
4099 getContext().mergeDefinitionIntoModule(
4100 Def, MergedDef->getImportedOwningModule(),
4101 /*NotifyListeners*/ false);
4102 PendingMergedDefinitionsToDeduplicate.insert(Def);
4103 }
4104 }
4105}
4106
4107bool ASTReader::loadGlobalIndex() {
4108 if (GlobalIndex)
4109 return false;
4110
4111 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4112 !PP.getLangOpts().Modules)
4113 return true;
4114
4115 // Try to load the global index.
4116 TriedLoadingGlobalIndex = true;
4117 StringRef ModuleCachePath
4118 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4119 std::pair<GlobalModuleIndex *, llvm::Error> Result =
4120 GlobalModuleIndex::readIndex(ModuleCachePath);
4121 if (llvm::Error Err = std::move(Result.second)) {
4122 assert(!Result.first)(static_cast<void> (0));
4123 consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4124 return true;
4125 }
4126
4127 GlobalIndex.reset(Result.first);
4128 ModuleMgr.setGlobalIndex(GlobalIndex.get());
4129 return false;
4130}
4131
4132bool ASTReader::isGlobalIndexUnavailable() const {
4133 return PP.getLangOpts().Modules && UseGlobalIndex &&
4134 !hasGlobalIndex() && TriedLoadingGlobalIndex;
4135}
4136
4137static void updateModuleTimestamp(ModuleFile &MF) {
4138 // Overwrite the timestamp file contents so that file's mtime changes.
4139 std::string TimestampFilename = MF.getTimestampFilename();
4140 std::error_code EC;
4141 llvm::raw_fd_ostream OS(TimestampFilename, EC,
4142 llvm::sys::fs::OF_TextWithCRLF);
4143 if (EC)
4144 return;
4145 OS << "Timestamp file\n";
4146 OS.close();
4147 OS.clear_error(); // Avoid triggering a fatal error.
4148}
4149
4150/// Given a cursor at the start of an AST file, scan ahead and drop the
4151/// cursor into the start of the given block ID, returning false on success and
4152/// true on failure.
4153static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4154 while (true) {
4155 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4156 if (!MaybeEntry) {
4157 // FIXME this drops errors on the floor.
4158 consumeError(MaybeEntry.takeError());
4159 return true;
4160 }
4161 llvm::BitstreamEntry Entry = MaybeEntry.get();
4162
4163 switch (Entry.Kind) {
4164 case llvm::BitstreamEntry::Error:
4165 case llvm::BitstreamEntry::EndBlock:
4166 return true;
4167
4168 case llvm::BitstreamEntry::Record:
4169 // Ignore top-level records.
4170 if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4171 break;
4172 else {
4173 // FIXME this drops errors on the floor.
4174 consumeError(Skipped.takeError());
4175 return true;
4176 }
4177
4178 case llvm::BitstreamEntry::SubBlock:
4179 if (Entry.ID == BlockID) {
4180 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4181 // FIXME this drops the error on the floor.
4182 consumeError(std::move(Err));
4183 return true;
4184 }
4185 // Found it!
4186 return false;
4187 }
4188
4189 if (llvm::Error Err = Cursor.SkipBlock()) {
4190 // FIXME this drops the error on the floor.
4191 consumeError(std::move(Err));
4192 return true;
4193 }
4194 }
4195 }
4196}
4197
4198ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4199 ModuleKind Type,
4200 SourceLocation ImportLoc,
4201 unsigned ClientLoadCapabilities,
4202 SmallVectorImpl<ImportedSubmodule> *Imported) {
4203 llvm::SaveAndRestore<SourceLocation>
4204 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4205 llvm::SaveAndRestore<Optional<ModuleKind>> SetCurModuleKindRAII(
4206 CurrentDeserializingModuleKind, Type);
4207
4208 // Defer any pending actions until we get to the end of reading the AST file.
4209 Deserializing AnASTFile(this);
4210
4211 // Bump the generation number.
4212 unsigned PreviousGeneration = 0;
4213 if (ContextObj)
1
Assuming field 'ContextObj' is null
2
Taking false branch
4214 PreviousGeneration = incrementGeneration(*ContextObj);
4215
4216 unsigned NumModules = ModuleMgr.size();
4217 SmallVector<ImportedModule, 4> Loaded;
4218 if (ASTReadResult ReadResult
2.1
'ReadResult' is 0
2.1
'ReadResult' is 0
2.1
'ReadResult' is 0
=
3
Taking false branch
4219 ReadASTCore(FileName, Type, ImportLoc,
4220 /*ImportedBy=*/nullptr, Loaded, 0, 0, ASTFileSignature(),
4221 ClientLoadCapabilities)) {
4222 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules,
4223 PP.getLangOpts().Modules
4224 ? &PP.getHeaderSearchInfo().getModuleMap()
4225 : nullptr);
4226
4227 // If we find that any modules are unusable, the global index is going
4228 // to be out-of-date. Just remove it.
4229 GlobalIndex.reset();
4230 ModuleMgr.setGlobalIndex(nullptr);
4231 return ReadResult;
4232 }
4233
4234 // Here comes stuff that we only do once the entire chain is loaded. Do *not*
4235 // remove modules from this point. Various fields are updated during reading
4236 // the AST block and removing the modules would result in dangling pointers.
4237 // They are generally only incidentally dereferenced, ie. a binary search
4238 // runs over `GlobalSLocEntryMap`, which could cause an invalid module to
4239 // be dereferenced but it wouldn't actually be used.
4240
4241 // Load the AST blocks of all of the modules that we loaded. We can still
4242 // hit errors parsing the ASTs at this point.
4243 for (ImportedModule &M : Loaded) {
4
Assuming '__begin1' is equal to '__end1'
4244 ModuleFile &F = *M.Mod;
4245
4246 // Read the AST block.
4247 if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) {
4248 Error(std::move(Err));
4249 return Failure;
4250 }
4251
4252 // The AST block should always have a definition for the main module.
4253 if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4254 Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4255 return Failure;
4256 }
4257
4258 // Read the extension blocks.
4259 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4260 if (llvm::Error Err = ReadExtensionBlock(F)) {
4261 Error(std::move(Err));
4262 return Failure;
4263 }
4264 }
4265
4266 // Once read, set the ModuleFile bit base offset and update the size in
4267 // bits of all files we've seen.
4268 F.GlobalBitOffset = TotalModulesSizeInBits;
4269 TotalModulesSizeInBits += F.SizeInBits;
4270 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4271 }
4272
4273 // Preload source locations and interesting indentifiers.
4274 for (ImportedModule &M : Loaded) {
5
Assuming '__begin1' is equal to '__end1'
4275 ModuleFile &F = *M.Mod;
4276
4277 // Preload SLocEntries.
4278 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4279 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4280 // Load it through the SourceManager and don't call ReadSLocEntry()
4281 // directly because the entry may have already been loaded in which case
4282 // calling ReadSLocEntry() directly would trigger an assertion in
4283 // SourceManager.
4284 SourceMgr.getLoadedSLocEntryByID(Index);
4285 }
4286
4287 // Map the original source file ID into the ID space of the current
4288 // compilation.
4289 if (F.OriginalSourceFileID.isValid()) {
4290 F.OriginalSourceFileID = FileID::get(
4291 F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4292 }
4293
4294 // Preload all the pending interesting identifiers by marking them out of
4295 // date.
4296 for (auto Offset : F.PreloadIdentifierOffsets) {
4297 const unsigned char *Data = F.IdentifierTableData + Offset;
4298
4299 ASTIdentifierLookupTrait Trait(*this, F);
4300 auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4301 auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4302 auto &II = PP.getIdentifierTable().getOwn(Key);
4303 II.setOutOfDate(true);
4304
4305 // Mark this identifier as being from an AST file so that we can track
4306 // whether we need to serialize it.
4307 markIdentifierFromAST(*this, II);
4308
4309 // Associate the ID with the identifier so that the writer can reuse it.
4310 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4311 SetIdentifierInfo(ID, &II);
4312 }
4313 }
4314
4315 // Setup the import locations and notify the module manager that we've
4316 // committed to these module files.
4317 for (ImportedModule &M : Loaded) {
6
Assuming '__begin1' is equal to '__end1'
4318 ModuleFile &F = *M.Mod;
4319
4320 ModuleMgr.moduleFileAccepted(&F);
4321
4322 // Set the import location.
4323 F.DirectImportLoc = ImportLoc;
4324 // FIXME: We assume that locations from PCH / preamble do not need
4325 // any translation.
4326 if (!M.ImportedBy)
4327 F.ImportLoc = M.ImportLoc;
4328 else
4329 F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4330 }
4331
4332 if (!PP.getLangOpts().CPlusPlus ||
7
Assuming field 'CPlusPlus' is not equal to 0
4333 (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
8
Assuming 'Type' is equal to MK_ImplicitModule
4334 Type != MK_PrebuiltModule)) {
4335 // Mark all of the identifiers in the identifier table as being out of date,
4336 // so that various accessors know to check the loaded modules when the
4337 // identifier is used.
4338 //
4339 // For C++ modules, we don't need information on many identifiers (just
4340 // those that provide macros or are poisoned), so we mark all of
4341 // the interesting ones via PreloadIdentifierOffsets.
4342 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4343 IdEnd = PP.getIdentifierTable().end();
4344 Id != IdEnd; ++Id)
4345 Id->second->setOutOfDate(true);
4346 }
4347 // Mark selectors as out of date.
4348 for (auto Sel : SelectorGeneration)
4349 SelectorOutOfDate[Sel.first] = true;
4350
4351 // Resolve any unresolved module exports.
4352 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
9
Assuming 'I' is equal to 'N'
10
Loop condition is false. Execution continues on line 4379
4353 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4354 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4355 Module *ResolvedMod = getSubmodule(GlobalID);
4356
4357 switch (Unresolved.Kind) {
4358 case UnresolvedModuleRef::Conflict:
4359 if (ResolvedMod) {
4360 Module::Conflict Conflict;
4361 Conflict.Other = ResolvedMod;
4362 Conflict.Message = Unresolved.String.str();
4363 Unresolved.Mod->Conflicts.push_back(Conflict);
4364 }
4365 continue;
4366
4367 case UnresolvedModuleRef::Import:
4368 if (ResolvedMod)
4369 Unresolved.Mod->Imports.insert(ResolvedMod);
4370 continue;
4371
4372 case UnresolvedModuleRef::Export:
4373 if (ResolvedMod || Unresolved.IsWildcard)
4374 Unresolved.Mod->Exports.push_back(
4375 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4376 continue;
4377 }
4378 }
4379 UnresolvedModuleRefs.clear();
4380
4381 if (Imported)
11
Assuming 'Imported' is null
12
Taking false branch
4382 Imported->append(ImportedModules.begin(),
4383 ImportedModules.end());
4384
4385 // FIXME: How do we load the 'use'd modules? They may not be submodules.
4386 // Might be unnecessary as use declarations are only used to build the
4387 // module itself.
4388
4389 if (ContextObj)
13
Assuming field 'ContextObj' is non-null
14
Taking true branch
4390 InitializeContext();
15
Calling 'ASTReader::InitializeContext'
4391
4392 if (SemaObj)
4393 UpdateSema();
4394
4395 if (DeserializationListener)
4396 DeserializationListener->ReaderInitialized(this);
4397
4398 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4399 if (PrimaryModule.OriginalSourceFileID.isValid()) {
4400 // If this AST file is a precompiled preamble, then set the
4401 // preamble file ID of the source manager to the file source file
4402 // from which the preamble was built.
4403 if (Type == MK_Preamble) {
4404 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4405 } else if (Type == MK_MainFile) {
4406 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4407 }
4408 }
4409
4410 // For any Objective-C class definitions we have already loaded, make sure
4411 // that we load any additional categories.
4412 if (ContextObj) {
4413 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4414 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4415 ObjCClassesLoaded[I],
4416 PreviousGeneration);
4417 }
4418 }
4419
4420 if (PP.getHeaderSearchInfo()
4421 .getHeaderSearchOpts()
4422 .ModulesValidateOncePerBuildSession) {
4423 // Now we are certain that the module and all modules it depends on are
4424 // up to date. Create or update timestamp files for modules that are
4425 // located in the module cache (not for PCH files that could be anywhere
4426 // in the filesystem).
4427 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4428 ImportedModule &M = Loaded[I];
4429 if (M.Mod->Kind == MK_ImplicitModule) {
4430 updateModuleTimestamp(*M.Mod);
4431 }
4432 }
4433 }
4434
4435 return Success;
4436}
4437
4438static ASTFileSignature readASTFileSignature(StringRef PCH);
4439
4440/// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4441static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4442 // FIXME checking magic headers is done in other places such as
4443 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4444 // always done the same. Unify it all with a helper.
4445 if (!Stream.canSkipToPos(4))
4446 return llvm::createStringError(std::errc::illegal_byte_sequence,
4447 "file too small to contain AST file magic");
4448 for (unsigned C : {'C', 'P', 'C', 'H'})
4449 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4450 if (Res.get() != C)
4451 return llvm::createStringError(
4452 std::errc::illegal_byte_sequence,
4453 "file doesn't start with AST file magic");
4454 } else
4455 return Res.takeError();
4456 return llvm::Error::success();
4457}
4458
4459static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4460 switch (Kind) {
4461 case MK_PCH:
4462 return 0; // PCH
4463 case MK_ImplicitModule:
4464 case MK_ExplicitModule:
4465 case MK_PrebuiltModule:
4466 return 1; // module
4467 case MK_MainFile:
4468 case MK_Preamble:
4469 return 2; // main source file
4470 }
4471 llvm_unreachable("unknown module kind")__builtin_unreachable();
4472}
4473
4474ASTReader::ASTReadResult
4475ASTReader::ReadASTCore(StringRef FileName,
4476 ModuleKind Type,
4477 SourceLocation ImportLoc,
4478 ModuleFile *ImportedBy,
4479 SmallVectorImpl<ImportedModule> &Loaded,
4480 off_t ExpectedSize, time_t ExpectedModTime,
4481 ASTFileSignature ExpectedSignature,
4482 unsigned ClientLoadCapabilities) {
4483 ModuleFile *M;
4484 std::string ErrorStr;
4485 ModuleManager::AddModuleResult AddResult
4486 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4487 getGeneration(), ExpectedSize, ExpectedModTime,
4488 ExpectedSignature, readASTFileSignature,
4489 M, ErrorStr);
4490
4491 switch (AddResult) {
4492 case ModuleManager::AlreadyLoaded:
4493 Diag(diag::remark_module_import)
4494 << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4495 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4496 return Success;
4497
4498 case ModuleManager::NewlyLoaded:
4499 // Load module file below.
4500 break;
4501
4502 case ModuleManager::Missing:
4503 // The module file was missing; if the client can handle that, return
4504 // it.
4505 if (ClientLoadCapabilities & ARR_Missing)
4506 return Missing;
4507
4508 // Otherwise, return an error.
4509 Diag(diag::err_ast_file_not_found)
4510 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4511 << ErrorStr;
4512 return Failure;
4513
4514 case ModuleManager::OutOfDate:
4515 // We couldn't load the module file because it is out-of-date. If the
4516 // client can handle out-of-date, return it.
4517 if (ClientLoadCapabilities & ARR_OutOfDate)
4518 return OutOfDate;
4519
4520 // Otherwise, return an error.
4521 Diag(diag::err_ast_file_out_of_date)
4522 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4523 << ErrorStr;
4524 return Failure;
4525 }
4526
4527 assert(M && "Missing module file")(static_cast<void> (0));
4528
4529 bool ShouldFinalizePCM = false;
4530 auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4531 auto &MC = getModuleManager().getModuleCache();
4532 if (ShouldFinalizePCM)
4533 MC.finalizePCM(FileName);
4534 else
4535 MC.tryToDropPCM(FileName);
4536 });
4537 ModuleFile &F = *M;
4538 BitstreamCursor &Stream = F.Stream;
4539 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4540 F.SizeInBits = F.Buffer->getBufferSize() * 8;
4541
4542 // Sniff for the signature.
4543 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4544 Diag(diag::err_ast_file_invalid)
4545 << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4546 return Failure;
4547 }
4548
4549 // This is used for compatibility with older PCH formats.
4550 bool HaveReadControlBlock = false;
4551 while (true) {
4552 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4553 if (!MaybeEntry) {
4554 Error(MaybeEntry.takeError());
4555 return Failure;
4556 }
4557 llvm::BitstreamEntry Entry = MaybeEntry.get();
4558
4559 switch (Entry.Kind) {
4560 case llvm::BitstreamEntry::Error:
4561 case llvm::BitstreamEntry::Record:
4562 case llvm::BitstreamEntry::EndBlock:
4563 Error("invalid record at top-level of AST file");
4564 return Failure;
4565
4566 case llvm::BitstreamEntry::SubBlock:
4567 break;
4568 }
4569
4570 switch (Entry.ID) {
4571 case CONTROL_BLOCK_ID:
4572 HaveReadControlBlock = true;
4573 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4574 case Success:
4575 // Check that we didn't try to load a non-module AST file as a module.
4576 //
4577 // FIXME: Should we also perform the converse check? Loading a module as
4578 // a PCH file sort of works, but it's a bit wonky.
4579 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4580 Type == MK_PrebuiltModule) &&
4581 F.ModuleName.empty()) {
4582 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4583 if (Result != OutOfDate ||
4584 (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4585 Diag(diag::err_module_file_not_module) << FileName;
4586 return Result;
4587 }
4588 break;
4589
4590 case Failure: return Failure;
4591 case Missing: return Missing;
4592 case OutOfDate: return OutOfDate;
4593 case VersionMismatch: return VersionMismatch;
4594 case ConfigurationMismatch: return ConfigurationMismatch;
4595 case HadErrors: return HadErrors;
4596 }
4597 break;
4598
4599 case AST_BLOCK_ID:
4600 if (!HaveReadControlBlock) {
4601 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4602 Diag(diag::err_pch_version_too_old);
4603 return VersionMismatch;
4604 }
4605
4606 // Record that we've loaded this module.
4607 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4608 ShouldFinalizePCM = true;
4609 return Success;
4610
4611 case UNHASHED_CONTROL_BLOCK_ID:
4612 // This block is handled using look-ahead during ReadControlBlock. We
4613 // shouldn't get here!
4614 Error("malformed block record in AST file");
4615 return Failure;
4616
4617 default:
4618 if (llvm::Error Err = Stream.SkipBlock()) {
4619 Error(std::move(Err));
4620 return Failure;
4621 }
4622 break;
4623 }
4624 }
4625
4626 llvm_unreachable("unexpected break; expected return")__builtin_unreachable();
4627}
4628
4629ASTReader::ASTReadResult
4630ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4631 unsigned ClientLoadCapabilities) {
4632 const HeaderSearchOptions &HSOpts =
4633 PP.getHeaderSearchInfo().getHeaderSearchOpts();
4634 bool AllowCompatibleConfigurationMismatch =
4635 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4636 bool DisableValidation = shouldDisableValidationForFile(F);
4637
4638 ASTReadResult Result = readUnhashedControlBlockImpl(
4639 &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4640 Listener.get(),
4641 WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4642
4643 // If F was directly imported by another module, it's implicitly validated by
4644 // the importing module.
4645 if (DisableValidation || WasImportedBy ||
4646 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4647 return Success;
4648
4649 if (Result == Failure) {
4650 Error("malformed block record in AST file");
4651 return Failure;
4652 }
4653
4654 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4655 // If this module has already been finalized in the ModuleCache, we're stuck
4656 // with it; we can only load a single version of each module.
4657 //
4658 // This can happen when a module is imported in two contexts: in one, as a
4659 // user module; in another, as a system module (due to an import from
4660 // another module marked with the [system] flag). It usually indicates a
4661 // bug in the module map: this module should also be marked with [system].
4662 //
4663 // If -Wno-system-headers (the default), and the first import is as a
4664 // system module, then validation will fail during the as-user import,
4665 // since -Werror flags won't have been validated. However, it's reasonable
4666 // to treat this consistently as a system module.
4667 //
4668 // If -Wsystem-headers, the PCM on disk was built with
4669 // -Wno-system-headers, and the first import is as a user module, then
4670 // validation will fail during the as-system import since the PCM on disk
4671 // doesn't guarantee that -Werror was respected. However, the -Werror
4672 // flags were checked during the initial as-user import.
4673 if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4674 Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4675 return Success;
4676 }
4677 }
4678
4679 return Result;
4680}
4681
4682ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4683 ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4684 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4685 bool ValidateDiagnosticOptions) {
4686 // Initialize a stream.
4687 BitstreamCursor Stream(StreamData);
4688
4689 // Sniff for the signature.
4690 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4691 // FIXME this drops the error on the floor.
4692 consumeError(std::move(Err));
4693 return Failure;
4694 }
4695
4696 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4697 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4698 return Failure;
4699
4700 // Read all of the records in the options block.
4701 RecordData Record;
4702 ASTReadResult Result = Success;
4703 while (true) {
4704 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4705 if (!MaybeEntry) {
4706 // FIXME this drops the error on the floor.
4707 consumeError(MaybeEntry.takeError());
4708 return Failure;
4709 }
4710 llvm::BitstreamEntry Entry = MaybeEntry.get();
4711
4712 switch (Entry.Kind) {
4713 case llvm::BitstreamEntry::Error:
4714 case llvm::BitstreamEntry::SubBlock:
4715 return Failure;
4716
4717 case llvm::BitstreamEntry::EndBlock:
4718 return Result;
4719
4720 case llvm::BitstreamEntry::Record:
4721 // The interesting case.
4722 break;
4723 }
4724
4725 // Read and process a record.
4726 Record.clear();
4727 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
4728 if (!MaybeRecordType) {
4729 // FIXME this drops the error.
4730 return Failure;
4731 }
4732 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4733 case SIGNATURE:
4734 if (F)
4735 F->Signature = ASTFileSignature::create(Record.begin(), Record.end());
4736 break;
4737 case AST_BLOCK_HASH:
4738 if (F)
4739 F->ASTBlockHash =
4740 ASTFileSignature::create(Record.begin(), Record.end());
4741 break;
4742 case DIAGNOSTIC_OPTIONS: {
4743 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4744 if (Listener && ValidateDiagnosticOptions &&
4745 !AllowCompatibleConfigurationMismatch &&
4746 ParseDiagnosticOptions(Record, Complain, *Listener))
4747 Result = OutOfDate; // Don't return early. Read the signature.
4748 break;
4749 }
4750 case DIAG_PRAGMA_MAPPINGS:
4751 if (!F)
4752 break;
4753 if (F->PragmaDiagMappings.empty())
4754 F->PragmaDiagMappings.swap(Record);
4755 else
4756 F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4757 Record.begin(), Record.end());
4758 break;
4759 }
4760 }
4761}
4762
4763/// Parse a record and blob containing module file extension metadata.
4764static bool parseModuleFileExtensionMetadata(
4765 const SmallVectorImpl<uint64_t> &Record,
4766 StringRef Blob,
4767 ModuleFileExtensionMetadata &Metadata) {
4768 if (Record.size() < 4) return true;
4769
4770 Metadata.MajorVersion = Record[0];
4771 Metadata.MinorVersion = Record[1];
4772
4773 unsigned BlockNameLen = Record[2];
4774 unsigned UserInfoLen = Record[3];
4775
4776 if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4777
4778 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4779 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4780 Blob.data() + BlockNameLen + UserInfoLen);
4781 return false;
4782}
4783
4784llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) {
4785 BitstreamCursor &Stream = F.Stream;
4786
4787 RecordData Record;
4788 while (true) {
4789 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4790 if (!MaybeEntry)
4791 return MaybeEntry.takeError();
4792 llvm::BitstreamEntry Entry = MaybeEntry.get();
4793
4794 switch (Entry.Kind) {
4795 case llvm::BitstreamEntry::SubBlock:
4796 if (llvm::Error Err = Stream.SkipBlock())
4797 return Err;
4798 continue;
4799 case llvm::BitstreamEntry::EndBlock:
4800 return llvm::Error::success();
4801 case llvm::BitstreamEntry::Error:
4802 return llvm::createStringError(std::errc::illegal_byte_sequence,
4803 "malformed block record in AST file");
4804 case llvm::BitstreamEntry::Record:
4805 break;
4806 }
4807
4808 Record.clear();
4809 StringRef Blob;
4810 Expected<unsigned> MaybeRecCode =
4811 Stream.readRecord(Entry.ID, Record, &Blob);
4812 if (!MaybeRecCode)
4813 return MaybeRecCode.takeError();
4814 switch (MaybeRecCode.get()) {
4815 case EXTENSION_METADATA: {
4816 ModuleFileExtensionMetadata Metadata;
4817 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
4818 return llvm::createStringError(
4819 std::errc::illegal_byte_sequence,
4820 "malformed EXTENSION_METADATA in AST file");
4821
4822 // Find a module file extension with this block name.
4823 auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4824 if (Known == ModuleFileExtensions.end()) break;
4825
4826 // Form a reader.
4827 if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4828 F, Stream)) {
4829 F.ExtensionReaders.push_back(std::move(Reader));
4830 }
4831
4832 break;
4833 }
4834 }
4835 }
4836
4837 return llvm::Error::success();
4838}
4839
4840void ASTReader::InitializeContext() {
4841 assert(ContextObj && "no context to initialize")(static_cast<void> (0));
4842 ASTContext &Context = *ContextObj;
4843
4844 // If there's a listener, notify them that we "read" the translation unit.
4845 if (DeserializationListener)
16
Assuming field 'DeserializationListener' is null
17
Taking false branch
4846 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4847 Context.getTranslationUnitDecl());
4848
4849 // FIXME: Find a better way to deal with collisions between these
4850 // built-in types. Right now, we just ignore the problem.
4851
4852 // Load the special types.
4853 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
18
Assuming the condition is true
19
Taking true branch
4854 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
20
Assuming 'String' is 0
21
Taking false branch
4855 if (!Context.CFConstantStringTypeDecl)
4856 Context.setCFConstantStringType(GetType(String));
4857 }
4858
4859 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
22
Assuming 'File' is 0
23
Taking false branch
4860 QualType FileType = GetType(File);
4861 if (FileType.isNull()) {
4862 Error("FILE type is NULL");
4863 return;
4864 }
4865
4866 if (!Context.FILEDecl) {
4867 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4868 Context.setFILEDecl(Typedef->getDecl());
4869 else {
4870 const TagType *Tag = FileType->getAs<TagType>();
4871 if (!Tag) {
4872 Error("Invalid FILE type in AST file");
4873 return;
4874 }
4875 Context.setFILEDecl(Tag->getDecl());
4876 }
4877 }
4878 }
4879
4880 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
24
Assuming 'Jmp_buf' is 0
25
Taking false branch
4881 QualType Jmp_bufType = GetType(Jmp_buf);
4882 if (Jmp_bufType.isNull()) {
4883 Error("jmp_buf type is NULL");
4884 return;
4885 }
4886
4887 if (!Context.jmp_bufDecl) {
4888 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4889 Context.setjmp_bufDecl(Typedef->getDecl());
4890 else {
4891 const TagType *Tag = Jmp_bufType->getAs<TagType>();
4892 if (!Tag) {
4893 Error("Invalid jmp_buf type in AST file");
4894 return;
4895 }
4896 Context.setjmp_bufDecl(Tag->getDecl());
4897 }
4898 }
4899 }
4900
4901 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
26
Assuming 'Sigjmp_buf' is not equal to 0
27
Taking true branch
4902 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4903 if (Sigjmp_bufType.isNull()) {
28
Calling 'QualType::isNull'
34
Returning from 'QualType::isNull'
35
Taking false branch
4904 Error("sigjmp_buf type is NULL");
4905 return;
4906 }
4907
4908 if (!Context.sigjmp_bufDecl) {
36
Assuming field 'sigjmp_bufDecl' is null
37
Taking true branch
4909 if (const TypedefType *Typedef
38.1
'Typedef' is null
38.1
'Typedef' is null
38.1
'Typedef' is null
= Sigjmp_bufType->getAs<TypedefType>())
38
Assuming the object is not a 'TypedefType'
39
Taking false branch
4910 Context.setsigjmp_bufDecl(Typedef->getDecl());
4911 else {
4912 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
40
Assuming the object is not a 'TagType'
41
'Tag' initialized to a null pointer value
4913 assert(Tag && "Invalid sigjmp_buf type in AST file")(static_cast<void> (0));
4914 Context.setsigjmp_bufDecl(Tag->getDecl());
42
Called C++ object pointer is null
4915 }
4916 }
4917 }
4918
4919 if (unsigned ObjCIdRedef
4920 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4921 if (Context.ObjCIdRedefinitionType.isNull())
4922 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4923 }
4924
4925 if (unsigned ObjCClassRedef
4926 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4927 if (Context.ObjCClassRedefinitionType.isNull())
4928 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4929 }
4930
4931 if (unsigned ObjCSelRedef
4932 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4933 if (Context.ObjCSelRedefinitionType.isNull())
4934 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4935 }
4936
4937 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4938 QualType Ucontext_tType = GetType(Ucontext_t);
4939 if (Ucontext_tType.isNull()) {
4940 Error("ucontext_t type is NULL");
4941 return;
4942 }
4943
4944 if (!Context.ucontext_tDecl) {
4945 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4946 Context.setucontext_tDecl(Typedef->getDecl());
4947 else {
4948 const TagType *Tag = Ucontext_tType->getAs<TagType>();
4949 assert(Tag && "Invalid ucontext_t type in AST file")(static_cast<void> (0));
4950 Context.setucontext_tDecl(Tag->getDecl());
4951 }
4952 }
4953 }
4954 }
4955
4956 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4957
4958 // If there were any CUDA special declarations, deserialize them.
4959 if (!CUDASpecialDeclRefs.empty()) {
4960 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!")(static_cast<void> (0));
4961 Context.setcudaConfigureCallDecl(
4962 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4963 }
4964
4965 // Re-export any modules that were imported by a non-module AST file.
4966 // FIXME: This does not make macro-only imports visible again.
4967 for (auto &Import : ImportedModules) {
4968 if (Module *Imported = getSubmodule(Import.ID)) {
4969 makeModuleVisible(Imported, Module::AllVisible,
4970 /*ImportLoc=*/Import.ImportLoc);
4971 if (Import.ImportLoc.isValid())
4972 PP.makeModuleVisible(Imported, Import.ImportLoc);
4973 // This updates visibility for Preprocessor only. For Sema, which can be
4974 // nullptr here, we do the same later, in UpdateSema().
4975 }
4976 }
4977}
4978
4979void ASTReader::finalizeForWriting() {
4980 // Nothing to do for now.
4981}
4982
4983/// Reads and return the signature record from \p PCH's control block, or
4984/// else returns 0.
4985static ASTFileSignature readASTFileSignature(StringRef PCH) {
4986 BitstreamCursor Stream(PCH);
4987 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4988 // FIXME this drops the error on the floor.
4989 consumeError(std::move(Err));
4990 return ASTFileSignature();
4991 }
4992
4993 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4994 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4995 return ASTFileSignature();
4996
4997 // Scan for SIGNATURE inside the diagnostic options block.
4998 ASTReader::RecordData Record;
4999 while (true) {
5000 Expected<llvm::BitstreamEntry> MaybeEntry =
5001 Stream.advanceSkippingSubblocks();
5002 if (!MaybeEntry) {
5003 // FIXME this drops the error on the floor.
5004 consumeError(MaybeEntry.takeError());
5005 return ASTFileSignature();
5006 }
5007 llvm::BitstreamEntry Entry = MaybeEntry.get();
5008
5009 if (Entry.Kind != llvm::BitstreamEntry::Record)
5010 return ASTFileSignature();
5011
5012 Record.clear();
5013 StringRef Blob;
5014 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5015 if (!MaybeRecord) {
5016 // FIXME this drops the error on the floor.
5017 consumeError(MaybeRecord.takeError());
5018 return ASTFileSignature();
5019 }
5020 if (SIGNATURE == MaybeRecord.get())
5021 return ASTFileSignature::create(Record.begin(),
5022 Record.begin() + ASTFileSignature::size);
5023 }
5024}
5025
5026/// Retrieve the name of the original source file name
5027/// directly from the AST file, without actually loading the AST
5028/// file.
5029std::string ASTReader::getOriginalSourceFile(
5030 const std::string &ASTFileName, FileManager &FileMgr,
5031 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5032 // Open the AST file.
5033 auto Buffer = FileMgr.getBufferForFile(ASTFileName);
5034 if (!Buffer) {
5035 Diags.Report(diag::err_fe_unable_to_read_pch_file)
5036 << ASTFileName << Buffer.getError().message();
5037 return std::string();
5038 }
5039
5040 // Initialize the stream
5041 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5042
5043 // Sniff for the signature.
5044 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5045 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5046 return std::string();
5047 }
5048
5049 // Scan for the CONTROL_BLOCK_ID block.
5050 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5051 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5052 return std::string();
5053 }
5054
5055 // Scan for ORIGINAL_FILE inside the control block.
5056 RecordData Record;
5057 while (true) {
5058 Expected<llvm::BitstreamEntry> MaybeEntry =
5059 Stream.advanceSkippingSubblocks();
5060 if (!MaybeEntry) {
5061 // FIXME this drops errors on the floor.
5062 consumeError(MaybeEntry.takeError());
5063 return std::string();
5064 }
5065 llvm::BitstreamEntry Entry = MaybeEntry.get();
5066
5067 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5068 return std::string();
5069
5070 if (Entry.Kind != llvm::BitstreamEntry::Record) {
5071 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5072 return std::string();
5073 }
5074
5075 Record.clear();
5076 StringRef Blob;
5077 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5078 if (!MaybeRecord) {
5079 // FIXME this drops the errors on the floor.
5080 consumeError(MaybeRecord.takeError());
5081 return std::string();
5082 }
5083 if (ORIGINAL_FILE == MaybeRecord.get())
5084 return Blob.str();
5085 }
5086}
5087
5088namespace {
5089
5090 class SimplePCHValidator : public ASTReaderListener {
5091 const LangOptions &ExistingLangOpts;
5092 const TargetOptions &ExistingTargetOpts;
5093 const PreprocessorOptions &ExistingPPOpts;
5094 std::string ExistingModuleCachePath;
5095 FileManager &FileMgr;
5096
5097 public:
5098 SimplePCHValidator(const LangOptions &ExistingLangOpts,
5099 const TargetOptions &ExistingTargetOpts,
5100 const PreprocessorOptions &ExistingPPOpts,
5101 StringRef ExistingModuleCachePath, FileManager &FileMgr)
5102 : ExistingLangOpts(ExistingLangOpts),
5103 ExistingTargetOpts(ExistingTargetOpts),
5104 ExistingPPOpts(ExistingPPOpts),
5105 ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {}
5106
5107 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5108 bool AllowCompatibleDifferences) override {
5109 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5110 AllowCompatibleDifferences);
5111 }
5112
5113 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5114 bool AllowCompatibleDifferences) override {
5115 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5116 AllowCompatibleDifferences);
5117 }
5118
5119 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5120 StringRef SpecificModuleCachePath,
5121 bool Complain) override {
5122 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5123 ExistingModuleCachePath, nullptr,
5124 ExistingLangOpts, ExistingPPOpts);
5125 }
5126
5127 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5128 bool Complain,
5129 std::string &SuggestedPredefines) override {
5130 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5131 SuggestedPredefines, ExistingLangOpts);
5132 }
5133 };
5134
5135} // namespace
5136
5137bool ASTReader::readASTFileControlBlock(
5138 StringRef Filename, FileManager &FileMgr,
5139 const PCHContainerReader &PCHContainerRdr,
5140 bool FindModuleFileExtensions,
5141 ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5142 // Open the AST file.
5143 // FIXME: This allows use of the VFS; we do not allow use of the
5144 // VFS when actually loading a module.
5145 auto Buffer = FileMgr.getBufferForFile(Filename);
5146 if (!Buffer) {
5147 return true;
5148 }
5149
5150 // Initialize the stream
5151 StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5152 BitstreamCursor Stream(Bytes);
5153
5154 // Sniff for the signature.
5155 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5156 consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5157 return true;
5158 }
5159
5160 // Scan for the CONTROL_BLOCK_ID block.
5161 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5162 return true;
5163
5164 bool NeedsInputFiles = Listener.needsInputFileVisitation();
5165 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5166 bool NeedsImports = Listener.needsImportVisitation();
5167 BitstreamCursor InputFilesCursor;
5168
5169 RecordData Record;
5170 std::string ModuleDir;
5171 bool DoneWithControlBlock = false;
5172 while (!DoneWithControlBlock) {
5173 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5174 if (!MaybeEntry) {
5175 // FIXME this drops the error on the floor.
5176 consumeError(MaybeEntry.takeError());
5177 return true;
5178 }
5179 llvm::BitstreamEntry Entry = MaybeEntry.get();
5180
5181 switch (Entry.Kind) {
5182 case llvm::BitstreamEntry::SubBlock: {
5183 switch (Entry.ID) {
5184 case OPTIONS_BLOCK_ID: {
5185 std::string IgnoredSuggestedPredefines;
5186 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5187 /*AllowCompatibleConfigurationMismatch*/ false,
5188 Listener, IgnoredSuggestedPredefines) != Success)
5189 return true;
5190 break;
5191 }
5192
5193 case INPUT_FILES_BLOCK_ID:
5194 InputFilesCursor = Stream;
5195 if (llvm::Error Err = Stream.SkipBlock()) {
5196 // FIXME this drops the error on the floor.
5197 consumeError(std::move(Err));
5198 return true;
5199 }
5200 if (NeedsInputFiles &&
5201 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5202 return true;
5203 break;
5204
5205 default:
5206 if (llvm::Error Err = Stream.SkipBlock()) {
5207 // FIXME this drops the error on the floor.
5208 consumeError(std::move(Err));
5209 return true;
5210 }
5211 break;
5212 }
5213
5214 continue;
5215 }
5216
5217 case llvm::BitstreamEntry::EndBlock:
5218 DoneWithControlBlock = true;
5219 break;
5220
5221 case llvm::BitstreamEntry::Error:
5222 return true;
5223
5224 case llvm::BitstreamEntry::Record:
5225 break;
5226 }
5227
5228 if (DoneWithControlBlock) break;
5229
5230 Record.clear();
5231 StringRef Blob;
5232 Expected<unsigned> MaybeRecCode =
5233 Stream.readRecord(Entry.ID, Record, &Blob);
5234 if (!MaybeRecCode) {
5235 // FIXME this drops the error.
5236 return Failure;
5237 }
5238 switch ((ControlRecordTypes)MaybeRecCode.get()) {
5239 case METADATA:
5240 if (Record[0] != VERSION_MAJOR)
5241 return true;
5242 if (Listener.ReadFullVersionInformation(Blob))
5243 return true;
5244 break;
5245 case MODULE_NAME:
5246 Listener.ReadModuleName(Blob);
5247 break;
5248 case MODULE_DIRECTORY:
5249 ModuleDir = std::string(Blob);
5250 break;
5251 case MODULE_MAP_FILE: {
5252 unsigned Idx = 0;
5253 auto Path = ReadString(Record, Idx);
5254 ResolveImportedPath(Path, ModuleDir);
5255 Listener.ReadModuleMapFile(Path);
5256 break;
5257 }
5258 case INPUT_FILE_OFFSETS: {
5259 if (!NeedsInputFiles)
5260 break;
5261
5262 unsigned NumInputFiles = Record[0];
5263 unsigned NumUserFiles = Record[1];
5264 const llvm::support::unaligned_uint64_t *InputFileOffs =
5265 (const llvm::support::unaligned_uint64_t *)Blob.data();
5266 for (unsigned I = 0; I != NumInputFiles; ++I) {
5267 // Go find this input file.
5268 bool isSystemFile = I >= NumUserFiles;
5269
5270 if (isSystemFile && !NeedsSystemInputFiles)
5271 break; // the rest are system input files
5272
5273 BitstreamCursor &Cursor = InputFilesCursor;
5274 SavedStreamPosition SavedPosition(Cursor);
5275 if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5276 // FIXME this drops errors on the floor.
5277 consumeError(std::move(Err));
5278 }
5279
5280 Expected<unsigned> MaybeCode = Cursor.ReadCode();
5281 if (!MaybeCode) {
5282 // FIXME this drops errors on the floor.
5283 consumeError(MaybeCode.takeError());
5284 }
5285 unsigned Code = MaybeCode.get();
5286
5287 RecordData Record;
5288 StringRef Blob;
5289 bool shouldContinue = false;
5290 Expected<unsigned> MaybeRecordType =
5291 Cursor.readRecord(Code, Record, &Blob);
5292 if (!MaybeRecordType) {
5293 // FIXME this drops errors on the floor.
5294 consumeError(MaybeRecordType.takeError());
5295 }
5296 switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5297 case INPUT_FILE_HASH:
5298 break;
5299 case INPUT_FILE:
5300 bool Overridden = static_cast<bool>(Record[3]);
5301 std::string Filename = std::string(Blob);
5302 ResolveImportedPath(Filename, ModuleDir);
5303 shouldContinue = Listener.visitInputFile(
5304 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5305 break;
5306 }
5307 if (!shouldContinue)
5308 break;
5309 }
5310 break;
5311 }
5312
5313 case IMPORTS: {
5314 if (!NeedsImports)
5315 break;
5316
5317 unsigned Idx = 0, N = Record.size();
5318 while (Idx < N) {
5319 // Read information about the AST file.
5320 Idx +=
5321 1 + 1 + 1 + 1 +
5322 ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature
5323 std::string ModuleName = ReadString(Record, Idx);
5324 std::string Filename = ReadString(Record, Idx);
5325 ResolveImportedPath(Filename, ModuleDir);
5326 Listener.visitImport(ModuleName, Filename);
5327 }
5328 break;
5329 }
5330
5331 default:
5332 // No other validation to perform.
5333 break;
5334 }
5335 }
5336
5337 // Look for module file extension blocks, if requested.
5338 if (FindModuleFileExtensions) {
5339 BitstreamCursor SavedStream = Stream;
5340 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5341 bool DoneWithExtensionBlock = false;
5342 while (!DoneWithExtensionBlock) {
5343 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5344 if (!MaybeEntry) {
5345 // FIXME this drops the error.
5346 return true;
5347 }
5348 llvm::BitstreamEntry Entry = MaybeEntry.get();
5349
5350 switch (Entry.Kind) {
5351 case llvm::BitstreamEntry::SubBlock:
5352 if (llvm::Error Err = Stream.SkipBlock()) {
5353 // FIXME this drops the error on the floor.
5354 consumeError(std::move(Err));
5355 return true;
5356 }
5357 continue;
5358
5359 case llvm::BitstreamEntry::EndBlock:
5360 DoneWithExtensionBlock = true;
5361 continue;
5362
5363 case llvm::BitstreamEntry::Error:
5364 return true;
5365
5366 case llvm::BitstreamEntry::Record:
5367 break;
5368 }
5369
5370 Record.clear();
5371 StringRef Blob;
5372 Expected<unsigned> MaybeRecCode =
5373 Stream.readRecord(Entry.ID, Record, &Blob);
5374 if (!MaybeRecCode) {
5375 // FIXME this drops the error.
5376 return true;
5377 }
5378 switch (MaybeRecCode.get()) {
5379 case EXTENSION_METADATA: {
5380 ModuleFileExtensionMetadata Metadata;
5381 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5382 return true;
5383
5384 Listener.readModuleFileExtension(Metadata);
5385 break;
5386 }
5387 }
5388 }
5389 }
5390 Stream = SavedStream;
5391 }
5392
5393 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5394 if (readUnhashedControlBlockImpl(
5395 nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5396 /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5397 ValidateDiagnosticOptions) != Success)
5398 return true;
5399
5400 return false;
5401}
5402
5403bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5404 const PCHContainerReader &PCHContainerRdr,
5405 const LangOptions &LangOpts,
5406 const TargetOptions &TargetOpts,
5407 const PreprocessorOptions &PPOpts,
5408 StringRef ExistingModuleCachePath) {
5409 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5410 ExistingModuleCachePath, FileMgr);
5411 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5412 /*FindModuleFileExtensions=*/false,
5413 validator,
5414 /*ValidateDiagnosticOptions=*/true);
5415}
5416
5417llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
5418 unsigned ClientLoadCapabilities) {
5419 // Enter the submodule block.
5420 if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID))
5421 return Err;
5422
5423 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5424 bool First = true;
5425 Module *CurrentModule = nullptr;
5426 RecordData Record;
5427 while (true) {
5428 Expected<llvm::BitstreamEntry> MaybeEntry =
5429 F.Stream.advanceSkippingSubblocks();
5430 if (!MaybeEntry)
5431 return MaybeEntry.takeError();
5432 llvm::BitstreamEntry Entry = MaybeEntry.get();
5433
5434 switch (Entry.Kind) {
5435 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5436 case llvm::BitstreamEntry::Error:
5437 return llvm::createStringError(std::errc::illegal_byte_sequence,
5438 "malformed block record in AST file");
5439 case llvm::BitstreamEntry::EndBlock:
5440 return llvm::Error::success();
5441 case llvm::BitstreamEntry::Record:
5442 // The interesting case.
5443 break;
5444 }
5445
5446 // Read a record.
5447 StringRef Blob;
5448 Record.clear();
5449 Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5450 if (!MaybeKind)
5451 return MaybeKind.takeError();
5452 unsigned Kind = MaybeKind.get();
5453
5454 if ((Kind == SUBMODULE_METADATA) != First)
5455 return llvm::createStringError(
5456 std::errc::illegal_byte_sequence,
5457 "submodule metadata record should be at beginning of block");
5458 First = false;
5459
5460 // Submodule information is only valid if we have a current module.
5461 // FIXME: Should we error on these cases?
5462 if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5463 Kind != SUBMODULE_DEFINITION)
5464 continue;
5465
5466 switch (Kind) {
5467 default: // Default behavior: ignore.
5468 break;
5469
5470 case SUBMODULE_DEFINITION: {
5471 if (Record.size() < 12)
5472 return llvm::createStringError(std::errc::illegal_byte_sequence,
5473 "malformed module definition");
5474
5475 StringRef Name = Blob;
5476 unsigned Idx = 0;
5477 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5478 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5479 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5480 bool IsFramework = Record[Idx++];
5481 bool IsExplicit = Record[Idx++];
5482 bool IsSystem = Record[Idx++];
5483 bool IsExternC = Record[Idx++];
5484 bool InferSubmodules = Record[Idx++];
5485 bool InferExplicitSubmodules = Record[Idx++];
5486 bool InferExportWildcard = Record[Idx++];
5487 bool ConfigMacrosExhaustive = Record[Idx++];
5488 bool ModuleMapIsPrivate = Record[Idx++];
5489
5490 Module *ParentModule = nullptr;
5491 if (Parent)
5492 ParentModule = getSubmodule(Parent);
5493
5494 // Retrieve this (sub)module from the module map, creating it if
5495 // necessary.
5496 CurrentModule =
5497 ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5498 .first;
5499
5500 // FIXME: set the definition loc for CurrentModule, or call
5501 // ModMap.setInferredModuleAllowedBy()
5502
5503 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5504 if (GlobalIndex >= SubmodulesLoaded.size() ||
5505 SubmodulesLoaded[GlobalIndex])
5506 return llvm::createStringError(std::errc::invalid_argument,
5507 "too many submodules");
5508
5509 if (!ParentModule) {
5510 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5511 // Don't emit module relocation error if we have -fno-validate-pch
5512 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
5513 DisableValidationForModuleKind::Module) &&
5514 CurFile != F.File) {
5515 auto ConflictError =
5516 PartialDiagnostic(diag::err_module_file_conflict,
5517 ContextObj->DiagAllocator)
5518 << CurrentModule->getTopLevelModuleName() << CurFile->getName()
5519 << F.File->getName();
5520 return DiagnosticError::create(CurrentImportLoc, ConflictError);
5521 }
5522 }
5523
5524 F.DidReadTopLevelSubmodule = true;
5525 CurrentModule->setASTFile(F.File);
5526 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5527 }
5528
5529 CurrentModule->Kind = Kind;
5530 CurrentModule->Signature = F.Signature;
5531 CurrentModule->IsFromModuleFile = true;
5532 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5533 CurrentModule->IsExternC = IsExternC;
5534 CurrentModule->InferSubmodules = InferSubmodules;
5535 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5536 CurrentModule->InferExportWildcard = InferExportWildcard;
5537 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5538 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5539 if (DeserializationListener)
5540 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5541
5542 SubmodulesLoaded[GlobalIndex] = CurrentModule;
5543
5544 // Clear out data that will be replaced by what is in the module file.
5545 CurrentModule->LinkLibraries.clear();
5546 CurrentModule->ConfigMacros.clear();
5547 CurrentModule->UnresolvedConflicts.clear();
5548 CurrentModule->Conflicts.clear();
5549
5550 // The module is available unless it's missing a requirement; relevant
5551 // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5552 // Missing headers that were present when the module was built do not
5553 // make it unavailable -- if we got this far, this must be an explicitly
5554 // imported module file.
5555 CurrentModule->Requirements.clear();
5556 CurrentModule->MissingHeaders.clear();
5557 CurrentModule->IsUnimportable =
5558 ParentModule && ParentModule->IsUnimportable;
5559 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
5560 break;
5561 }
5562
5563 case SUBMODULE_UMBRELLA_HEADER: {
5564 // FIXME: This doesn't work for framework modules as `Filename` is the
5565 // name as written in the module file and does not include
5566 // `Headers/`, so this path will never exist.
5567 std::string Filename = std::string(Blob);
5568 ResolveImportedPath(F, Filename);
5569 if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
5570 if (!CurrentModule->getUmbrellaHeader()) {
5571 // FIXME: NameAsWritten
5572 ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob, "");
5573 }
5574 // Note that it's too late at this point to return out of date if the
5575 // name from the PCM doesn't match up with the one in the module map,
5576 // but also quite unlikely since we will have already checked the
5577 // modification time and size of the module map file itself.
5578 }
5579 break;
5580 }
5581
5582 case SUBMODULE_HEADER:
5583 case SUBMODULE_EXCLUDED_HEADER:
5584 case SUBMODULE_PRIVATE_HEADER:
5585 // We lazily associate headers with their modules via the HeaderInfo table.
5586 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5587 // of complete filenames or remove it entirely.
5588 break;
5589
5590 case SUBMODULE_TEXTUAL_HEADER:
5591 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5592 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5593 // them here.
5594 break;
5595
5596 case SUBMODULE_TOPHEADER:
5597 CurrentModule->addTopHeaderFilename(Blob);
5598 break;
5599
5600 case SUBMODULE_UMBRELLA_DIR: {
5601 // See comments in SUBMODULE_UMBRELLA_HEADER
5602 std::string Dirname = std::string(Blob);
5603 ResolveImportedPath(F, Dirname);
5604 if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5605 if (!CurrentModule->getUmbrellaDir()) {
5606 // FIXME: NameAsWritten
5607 ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob, "");
5608 }
5609 }
5610 break;
5611 }
5612
5613 case SUBMODULE_METADATA: {
5614 F.BaseSubmoduleID = getTotalNumSubmodules();
5615 F.LocalNumSubmodules = Record[0];
5616 unsigned LocalBaseSubmoduleID = Record[1];
5617 if (F.LocalNumSubmodules > 0) {
5618 // Introduce the global -> local mapping for submodules within this
5619 // module.
5620 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5621
5622 // Introduce the local -> global mapping for submodules within this
5623 // module.
5624 F.SubmoduleRemap.insertOrReplace(
5625 std::make_pair(LocalBaseSubmoduleID,
5626 F.BaseSubmoduleID - LocalBaseSubmoduleID));
5627
5628 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5629 }
5630 break;
5631 }
5632
5633 case SUBMODULE_IMPORTS:
5634 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5635 UnresolvedModuleRef Unresolved;
5636 Unresolved.File = &F;
5637 Unresolved.Mod = CurrentModule;
5638 Unresolved.ID = Record[Idx];
5639 Unresolved.Kind = UnresolvedModuleRef::Import;
5640 Unresolved.IsWildcard = false;
5641 UnresolvedModuleRefs.push_back(Unresolved);
5642 }
5643 break;
5644
5645 case SUBMODULE_EXPORTS:
5646 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5647 UnresolvedModuleRef Unresolved;
5648 Unresolved.File = &F;
5649 Unresolved.Mod = CurrentModule;
5650 Unresolved.ID = Record[Idx];
5651 Unresolved.Kind = UnresolvedModuleRef::Export;
5652 Unresolved.IsWildcard = Record[Idx + 1];
5653 UnresolvedModuleRefs.push_back(Unresolved);
5654 }
5655
5656 // Once we've loaded the set of exports, there's no reason to keep
5657 // the parsed, unresolved exports around.
5658 CurrentModule->UnresolvedExports.clear();
5659 break;
5660
5661 case SUBMODULE_REQUIRES:
5662 CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5663 PP.getTargetInfo());
5664 break;
5665
5666 case SUBMODULE_LINK_LIBRARY:
5667 ModMap.resolveLinkAsDependencies(CurrentModule);
5668 CurrentModule->LinkLibraries.push_back(
5669 Module::LinkLibrary(std::string(Blob), Record[0]));
5670 break;
5671
5672 case SUBMODULE_CONFIG_MACRO:
5673 CurrentModule->ConfigMacros.push_back(Blob.str());
5674 break;
5675
5676 case SUBMODULE_CONFLICT: {
5677 UnresolvedModuleRef Unresolved;
5678 Unresolved.File = &F;
5679 Unresolved.Mod = CurrentModule;
5680 Unresolved.ID = Record[0];
5681 Unresolved.Kind = UnresolvedModuleRef::Conflict;
5682 Unresolved.IsWildcard = false;
5683 Unresolved.String = Blob;
5684 UnresolvedModuleRefs.push_back(Unresolved);
5685 break;
5686 }
5687
5688 case SUBMODULE_INITIALIZERS: {
5689 if (!ContextObj)
5690 break;
5691 SmallVector<uint32_t, 16> Inits;
5692 for (auto &ID : Record)
5693 Inits.push_back(getGlobalDeclID(F, ID));
5694 ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5695 break;
5696 }
5697
5698 case SUBMODULE_EXPORT_AS:
5699 CurrentModule->ExportAsModule = Blob.str();
5700 ModMap.addLinkAsDependency(CurrentModule);
5701 break;
5702 }
5703 }
5704}
5705
5706/// Parse the record that corresponds to a LangOptions data
5707/// structure.
5708///
5709/// This routine parses the language options from the AST file and then gives
5710/// them to the AST listener if one is set.
5711///
5712/// \returns true if the listener deems the file unacceptable, false otherwise.
5713bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5714 bool Complain,
5715 ASTReaderListener &Listener,
5716 bool AllowCompatibleDifferences) {
5717 LangOptions LangOpts;
5718 unsigned Idx = 0;
5719#define LANGOPT(Name, Bits, Default, Description) \
5720 LangOpts.Name = Record[Idx++];
5721#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5722 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5723#include "clang/Basic/LangOptions.def"
5724#define SANITIZER(NAME, ID) \
5725 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5726#include "clang/Basic/Sanitizers.def"
5727
5728 for (unsigned N = Record[Idx++]; N; --N)
5729 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5730
5731 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5732 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5733 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5734
5735 LangOpts.CurrentModule = ReadString(Record, Idx);
5736
5737 // Comment options.
5738 for (unsigned N = Record[Idx++]; N; --N) {
5739 LangOpts.CommentOpts.BlockCommandNames.push_back(
5740 ReadString(Record, Idx));
5741 }
5742 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5743
5744 // OpenMP offloading options.
5745 for (unsigned N = Record[Idx++]; N; --N) {
5746 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5747 }
5748
5749 LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5750
5751 return Listener.ReadLanguageOptions(LangOpts, Complain,
5752 AllowCompatibleDifferences);
5753}
5754
5755bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5756 ASTReaderListener &Listener,
5757 bool AllowCompatibleDifferences) {
5758 unsigned Idx = 0;
5759 TargetOptions TargetOpts;
5760 TargetOpts.Triple = ReadString(Record, Idx);
5761 TargetOpts.CPU = ReadString(Record, Idx);
5762 TargetOpts.TuneCPU = ReadString(Record, Idx);
5763 TargetOpts.ABI = ReadString(Record, Idx);
5764 for (unsigned N = Record[Idx++]; N; --N) {
5765 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5766 }
5767 for (unsigned N = Record[Idx++]; N; --N) {
5768 TargetOpts.Features.push_back(ReadString(Record, Idx));
5769 }
5770
5771 return Listener.ReadTargetOptions(TargetOpts, Complain,
5772 AllowCompatibleDifferences);
5773}
5774
5775bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5776 ASTReaderListener &Listener) {
5777 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5778 unsigned Idx = 0;
5779#define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5780#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5781 DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5782#include "clang/Basic/DiagnosticOptions.def"
5783
5784 for (unsigned N = Record[Idx++]; N; --N)
5785 DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5786 for (unsigned N = Record[Idx++]; N; --N)
5787 DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5788
5789 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5790}
5791
5792bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5793 ASTReaderListener &Listener) {
5794 FileSystemOptions FSOpts;
5795 unsigned Idx = 0;
5796 FSOpts.WorkingDir = ReadString(Record, Idx);
5797 return Listener.ReadFileSystemOptions(FSOpts, Complain);
5798}
5799
5800bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5801 bool Complain,
5802 ASTReaderListener &Listener) {
5803 HeaderSearchOptions HSOpts;
5804 unsigned Idx = 0;
5805 HSOpts.Sysroot = ReadString(Record, Idx);
5806
5807 // Include entries.
5808 for (unsigned N = Record[Idx++]; N; --N) {
5809 std::string Path = ReadString(Record, Idx);
5810 frontend::IncludeDirGroup Group
5811 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5812 bool IsFramework = Record[Idx++];
5813 bool IgnoreSysRoot = Record[Idx++];
5814 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5815 IgnoreSysRoot);
5816 }
5817
5818 // System header prefixes.
5819 for (unsigned N = Record[Idx++]; N; --N) {
5820 std::string Prefix = ReadString(Record, Idx);
5821 bool IsSystemHeader = Record[Idx++];
5822 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5823 }
5824
5825 HSOpts.ResourceDir = ReadString(Record, Idx);
5826 HSOpts.ModuleCachePath = ReadString(Record, Idx);
5827 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5828 HSOpts.DisableModuleHash = Record[Idx++];
5829 HSOpts.ImplicitModuleMaps = Record[Idx++];
5830 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5831 HSOpts.EnablePrebuiltImplicitModules = Record[Idx++];
5832 HSOpts.UseBuiltinIncludes = Record[Idx++];
5833 HSOpts.UseStandardSystemIncludes = Record[Idx++];
5834 HSOpts.UseStandardCXXIncludes = Record[Idx++];
5835 HSOpts.UseLibcxx = Record[Idx++];
5836 std::string SpecificModuleCachePath = ReadString(Record, Idx);
5837
5838 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5839 Complain);
5840}
5841
5842bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5843 bool Complain,
5844 ASTReaderListener &Listener,
5845 std::string &SuggestedPredefines) {
5846 PreprocessorOptions PPOpts;
5847 unsigned Idx = 0;
5848
5849 // Macro definitions/undefs
5850 for (unsigned N = Record[Idx++]; N; --N) {
5851 std::string Macro = ReadString(Record, Idx);
5852 bool IsUndef = Record[Idx++];
5853 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5854 }
5855
5856 // Includes
5857 for (unsigned N = Record[Idx++]; N; --N) {
5858 PPOpts.Includes.push_back(ReadString(Record, Idx));
5859 }
5860
5861 // Macro Includes
5862 for (unsigned N = Record[Idx++]; N; --N) {
5863 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5864 }
5865
5866 PPOpts.UsePredefines = Record[Idx++];
5867 PPOpts.DetailedRecord = Record[Idx++];
5868 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5869 PPOpts.ObjCXXARCStandardLibrary =
5870 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5871 SuggestedPredefines.clear();
5872 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5873 SuggestedPredefines);
5874}
5875
5876std::pair<ModuleFile *, unsigned>
5877ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5878 GlobalPreprocessedEntityMapType::iterator
5879 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5880 assert(I != GlobalPreprocessedEntityMap.end() &&(static_cast<void> (0))
5881 "Corrupted global preprocessed entity map")(static_cast<void> (0));
5882 ModuleFile *M = I->second;
5883 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5884 return std::make_pair(M, LocalIndex);
5885}
5886
5887llvm::iterator_range<PreprocessingRecord::iterator>
5888ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5889 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5890 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5891 Mod.NumPreprocessedEntities);
5892
5893 return llvm::make_range(PreprocessingRecord::iterator(),
5894 PreprocessingRecord::iterator());
5895}
5896
5897bool ASTReader::canRecoverFromOutOfDate(StringRef ModuleFileName,
5898 unsigned int ClientLoadCapabilities) {
5899 return ClientLoadCapabilities & ARR_OutOfDate &&
5900 !getModuleManager().getModuleCache().isPCMFinal(ModuleFileName);
5901}
5902
5903llvm::iterator_range<ASTReader::ModuleDeclIterator>
5904ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5905 return llvm::make_range(
5906 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5907 ModuleDeclIterator(this, &Mod,
5908 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5909}
5910
5911SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5912 auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5913 assert(I != GlobalSkippedRangeMap.end() &&(static_cast<void> (0))
5914 "Corrupted global skipped range map")(static_cast<void> (0));
5915 ModuleFile *M = I->second;
5916 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5917 assert(LocalIndex < M->NumPreprocessedSkippedRanges)(static_cast<void> (0));
5918 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5919 SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5920 TranslateSourceLocation(*M, RawRange.getEnd()));
5921 assert(Range.isValid())(static_cast<void> (0));
5922 return Range;
5923}
5924
5925PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5926 PreprocessedEntityID PPID = Index+1;
5927 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5928 ModuleFile &M = *PPInfo.first;
5929 unsigned LocalIndex = PPInfo.second;
5930 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5931
5932 if (!PP.getPreprocessingRecord()) {
5933 Error("no preprocessing record");
5934 return nullptr;
5935 }
5936
5937 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5938 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
5939 M.MacroOffsetsBase + PPOffs.BitOffset)) {
5940 Error(std::move(Err));
5941 return nullptr;
5942 }
5943
5944 Expected<llvm::BitstreamEntry> MaybeEntry =
5945 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5946 if (!MaybeEntry) {
5947 Error(MaybeEntry.takeError());
5948 return nullptr;
5949 }
5950 llvm::BitstreamEntry Entry = MaybeEntry.get();
5951
5952 if (Entry.Kind != llvm::BitstreamEntry::Record)
5953 return nullptr;
5954
5955 // Read the record.
5956 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5957 TranslateSourceLocation(M, PPOffs.getEnd()));
5958 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5959 StringRef Blob;
5960 RecordData Record;
5961 Expected<unsigned> MaybeRecType =
5962 M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5963 if (!MaybeRecType) {
5964 Error(MaybeRecType.takeError());
5965 return nullptr;
5966 }
5967 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
5968 case PPD_MACRO_EXPANSION: {
5969 bool isBuiltin = Record[0];
5970 IdentifierInfo *Name = nullptr;
5971 MacroDefinitionRecord *Def = nullptr;
5972 if (isBuiltin)
5973 Name = getLocalIdentifier(M, Record[1]);
5974 else {
5975 PreprocessedEntityID GlobalID =
5976 getGlobalPreprocessedEntityID(M, Record[1]);
5977 Def = cast<MacroDefinitionRecord>(
5978 PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
5979 }
5980
5981 MacroExpansion *ME;
5982 if (isBuiltin)
5983 ME = new (PPRec) MacroExpansion(Name, Range);
5984 else
5985 ME = new (PPRec) MacroExpansion(Def, Range);
5986
5987 return ME;
5988 }
5989
5990 case PPD_MACRO_DEFINITION: {
5991 // Decode the identifier info and then check again; if the macro is
5992 // still defined and associated with the identifier,
5993 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
5994 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
5995
5996 if (DeserializationListener)
5997 DeserializationListener->MacroDefinitionRead(PPID, MD);
5998
5999 return MD;
6000 }
6001
6002 case PPD_INCLUSION_DIRECTIVE: {
6003 const char *FullFileNameStart = Blob.data() + Record[0];
6004 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6005 const FileEntry *File = nullptr;
6006 if (!FullFileName.empty())
6007 if (auto FE = PP.getFileManager().getFile(FullFileName))
6008 File = *FE;
6009
6010 // FIXME: Stable encoding
6011 InclusionDirective::InclusionKind Kind
6012 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6013 InclusionDirective *ID
6014 = new (PPRec) InclusionDirective(PPRec, Kind,
6015 StringRef(Blob.data(), Record[0]),
6016 Record[1], Record[3],
6017 File,
6018 Range);
6019 return ID;
6020 }
6021 }
6022
6023 llvm_unreachable("Invalid PreprocessorDetailRecordTypes")__builtin_unreachable();
6024}
6025
6026/// Find the next module that contains entities and return the ID
6027/// of the first entry.
6028///
6029/// \param SLocMapI points at a chunk of a module that contains no
6030/// preprocessed entities or the entities it contains are not the ones we are
6031/// looking for.
6032PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6033 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6034 ++SLocMapI;
6035 for (GlobalSLocOffsetMapType::const_iterator
6036 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6037 ModuleFile &M = *SLocMapI->second;
6038 if (M.NumPreprocessedEntities)
6039 return M.BasePreprocessedEntityID;
6040 }
6041
6042 return getTotalNumPreprocessedEntities();
6043}
6044
6045namespace {
6046
6047struct PPEntityComp {
6048 const ASTReader &Reader;
6049 ModuleFile &M;
6050
6051 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6052
6053 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6054 SourceLocation LHS = getLoc(L);
6055 SourceLocation RHS = getLoc(R);
6056 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6057 }
6058
6059 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6060 SourceLocation LHS = getLoc(L);
6061 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6062 }
6063
6064 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6065 SourceLocation RHS = getLoc(R);
6066 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6067 }
6068
6069 SourceLocation getLoc(const PPEntityOffset &PPE) const {
6070 return Reader.TranslateSourceLocation(M, PPE.getBegin());
6071 }
6072};
6073
6074} // namespace
6075
6076PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6077 bool EndsAfter) const {
6078 if (SourceMgr.isLocalSourceLocation(Loc))
6079 return getTotalNumPreprocessedEntities();
6080
6081 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6082 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6083 assert(SLocMapI != GlobalSLocOffsetMap.end() &&(static_cast<void> (0))
6084 "Corrupted global sloc offset map")(static_cast<void> (0));
6085
6086 if (SLocMapI->second->NumPreprocessedEntities == 0)
6087 return findNextPreprocessedEntity(SLocMapI);
6088
6089 ModuleFile &M = *SLocMapI->second;
6090
6091 using pp_iterator = const PPEntityOffset *;
6092
6093 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6094 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6095
6096 size_t Count = M.NumPreprocessedEntities;
6097 size_t Half;
6098 pp_iterator First = pp_begin;
6099 pp_iterator PPI;
6100
6101 if (EndsAfter) {
6102 PPI = std::upper_bound(pp_begin, pp_end, Loc,
6103 PPEntityComp(*this, M));
6104 } else {
6105 // Do a binary search manually instead of using std::lower_bound because
6106 // The end locations of entities may be unordered (when a macro expansion
6107 // is inside another macro argument), but for this case it is not important
6108 // whether we get the first macro expansion or its containing macro.
6109 while (Count > 0) {
6110 Half = Count / 2;
6111 PPI = First;
6112 std::advance(PPI, Half);
6113 if (SourceMgr.isBeforeInTranslationUnit(
6114 TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6115 First = PPI;
6116 ++First;
6117 Count = Count - Half - 1;
6118 } else
6119 Count = Half;
6120 }
6121 }
6122
6123 if (PPI == pp_end)
6124 return findNextPreprocessedEntity(SLocMapI);
6125
6126 return M.BasePreprocessedEntityID + (PPI - pp_begin);
6127}
6128
6129/// Returns a pair of [Begin, End) indices of preallocated
6130/// preprocessed entities that \arg Range encompasses.
6131std::pair<unsigned, unsigned>
6132 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6133 if (Range.isInvalid())
6134 return std::make_pair(0,0);
6135 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()))(static_cast<void> (0));
6136
6137 PreprocessedEntityID BeginID =
6138 findPreprocessedEntity(Range.getBegin(), false);
6139 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6140 return std::make_pair(BeginID, EndID);
6141}
6142
6143/// Optionally returns true or false if the preallocated preprocessed
6144/// entity with index \arg Index came from file \arg FID.
6145Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6146 FileID FID) {
6147 if (FID.isInvalid())
6148 return false;
6149
6150 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6151 ModuleFile &M = *PPInfo.first;
6152 unsigned LocalIndex = PPInfo.second;
6153 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6154
6155 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6156 if (Loc.isInvalid())
6157 return false;
6158
6159 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6160 return true;
6161 else
6162 return false;
6163}
6164
6165namespace {
6166
6167 /// Visitor used to search for information about a header file.
6168 class HeaderFileInfoVisitor {
6169 const FileEntry *FE;
6170 Optional<HeaderFileInfo> HFI;
6171
6172 public:
6173 explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6174
6175 bool operator()(ModuleFile &M) {
6176 HeaderFileInfoLookupTable *Table
6177 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6178 if (!Table)
6179 return false;
6180
6181 // Look in the on-disk hash table for an entry for this file name.
6182 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6183 if (Pos == Table->end())
6184 return false;
6185
6186 HFI = *Pos;
6187 return true;
6188 }
6189
6190 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6191 };
6192
6193} // namespace
6194
6195HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6196 HeaderFileInfoVisitor Visitor(FE);
6197 ModuleMgr.visit(Visitor);
6198 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6199 return *HFI;
6200
6201 return HeaderFileInfo();
6202}
6203
6204void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6205 using DiagState = DiagnosticsEngine::DiagState;
6206 SmallVector<DiagState *, 32> DiagStates;
6207
6208 for (ModuleFile &F : ModuleMgr) {
6209 unsigned Idx = 0;
6210 auto &Record = F.PragmaDiagMappings;
6211 if (Record.empty())
6212 continue;
6213
6214 DiagStates.clear();
6215
6216 auto ReadDiagState =
6217 [&](const DiagState &BasedOn, SourceLocation Loc,
6218 bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6219 unsigned BackrefID = Record[Idx++];
6220 if (BackrefID != 0)
6221 return DiagStates[BackrefID - 1];
6222
6223 // A new DiagState was created here.
6224 Diag.DiagStates.push_back(BasedOn);
6225 DiagState *NewState = &Diag.DiagStates.back();
6226 DiagStates.push_back(NewState);
6227 unsigned Size = Record[Idx++];
6228 assert(Idx + Size * 2 <= Record.size() &&(static_cast<void> (0))
6229 "Invalid data, not enough diag/map pairs")(static_cast<void> (0));
6230 while (Size--) {
6231 unsigned DiagID = Record[Idx++];
6232 DiagnosticMapping NewMapping =
6233 DiagnosticMapping::deserialize(Record[Idx++]);
6234 if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6235 continue;
6236
6237 DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6238
6239 // If this mapping was specified as a warning but the severity was
6240 // upgraded due to diagnostic settings, simulate the current diagnostic
6241 // settings (and use a warning).
6242 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6243 NewMapping.setSeverity(diag::Severity::Warning);
6244 NewMapping.setUpgradedFromWarning(false);
6245 }
6246
6247 Mapping = NewMapping;
6248 }
6249 return NewState;
6250 };
6251
6252 // Read the first state.
6253 DiagState *FirstState;
6254 if (F.Kind == MK_ImplicitModule) {
6255 // Implicitly-built modules are reused with different diagnostic
6256 // settings. Use the initial diagnostic state from Diag to simulate this
6257 // compilation's diagnostic settings.
6258 FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6259 DiagStates.push_back(FirstState);
6260
6261 // Skip the initial diagnostic state from the serialized module.
6262 assert(Record[1] == 0 &&(static_cast<void> (0))
6263 "Invalid data, unexpected backref in initial state")(static_cast<void> (0));
6264 Idx = 3 + Record[2] * 2;
6265 assert(Idx < Record.size() &&(static_cast<void> (0))
6266 "Invalid data, not enough state change pairs in initial state")(static_cast<void> (0));
6267 } else if (F.isModule()) {
6268 // For an explicit module, preserve the flags from the module build
6269 // command line (-w, -Weverything, -Werror, ...) along with any explicit
6270 // -Wblah flags.
6271 unsigned Flags = Record[Idx++];
6272 DiagState Initial;
6273 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6274 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6275 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6276 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6277 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6278 Initial.ExtBehavior = (diag::Severity)Flags;
6279 FirstState = ReadDiagState(Initial, SourceLocation(), true);
6280
6281 assert(F.OriginalSourceFileID.isValid())(static_cast<void> (0));
6282
6283 // Set up the root buffer of the module to start with the initial
6284 // diagnostic state of the module itself, to cover files that contain no
6285 // explicit transitions (for which we did not serialize anything).
6286 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6287 .StateTransitions.push_back({FirstState, 0});
6288 } else {
6289 // For prefix ASTs, start with whatever the user configured on the
6290 // command line.
6291 Idx++; // Skip flags.
6292 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6293 SourceLocation(), false);
6294 }
6295
6296 // Read the state transitions.
6297 unsigned NumLocations = Record[Idx++];
6298 while (NumLocations--) {
6299 assert(Idx < Record.size() &&(static_cast<void> (0))
6300 "Invalid data, missing pragma diagnostic states")(static_cast<void> (0));
6301 SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6302 auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6303 assert(IDAndOffset.first.isValid() && "invalid FileID for transition")(static_cast<void> (0));
6304 assert(IDAndOffset.second == 0 && "not a start location for a FileID")(static_cast<void> (0));
6305 unsigned Transitions = Record[Idx++];
6306
6307 // Note that we don't need to set up Parent/ParentOffset here, because
6308 // we won't be changing the diagnostic state within imported FileIDs
6309 // (other than perhaps appending to the main source file, which has no
6310 // parent).
6311 auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6312 F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6313 for (unsigned I = 0; I != Transitions; ++I) {
6314 unsigned Offset = Record[Idx++];
6315 auto *State =
6316 ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6317 F.StateTransitions.push_back({State, Offset});
6318 }
6319 }
6320
6321 // Read the final state.
6322 assert(Idx < Record.size() &&(static_cast<void> (0))
6323 "Invalid data, missing final pragma diagnostic state")(static_cast<void> (0));
6324 SourceLocation CurStateLoc =
6325 ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6326 auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6327
6328 if (!F.isModule()) {
6329 Diag.DiagStatesByLoc.CurDiagState = CurState;
6330 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6331
6332 // Preserve the property that the imaginary root file describes the
6333 // current state.
6334 FileID NullFile;
6335 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6336 if (T.empty())
6337 T.push_back({CurState, 0});
6338 else
6339 T[0].State = CurState;
6340 }
6341
6342 // Don't try to read these mappings again.
6343 Record.clear();
6344 }
6345}
6346
6347/// Get the correct cursor and offset for loading a type.
6348ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6349 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6350 assert(I != GlobalTypeMap.end() && "Corrupted global type map")(static_cast<void> (0));
6351 ModuleFile *M = I->second;
6352 return RecordLocation(
6353 M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() +
6354 M->DeclsBlockStartOffset);
6355}
6356
6357static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6358 switch (code) {
6359#define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6360 case TYPE_##CODE_ID: return Type::CLASS_ID;
6361#include "clang/Serialization/TypeBitCodes.def"
6362 default: return llvm::None;
6363 }
6364}
6365
6366/// Read and return the type with the given index..
6367///
6368/// The index is the type ID, shifted and minus the number of predefs. This
6369/// routine actually reads the record corresponding to the type at the given
6370/// location. It is a helper routine for GetType, which deals with reading type
6371/// IDs.
6372QualType ASTReader::readTypeRecord(unsigned Index) {
6373 assert(ContextObj && "reading type with no AST context")(static_cast<void> (0));
6374 ASTContext &Context = *ContextObj;
6375 RecordLocation Loc = TypeCursorForIndex(Index);
6376 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6377
6378 // Keep track of where we are in the stream, then jump back there
6379 // after reading this type.
6380 SavedStreamPosition SavedPosition(DeclsCursor);
6381
6382 ReadingKindTracker ReadingKind(Read_Type, *this);
6383
6384 // Note that we are loading a type record.
6385 Deserializing AType(this);
6386
6387 if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6388 Error(std::move(Err));
6389 return QualType();
6390 }
6391 Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6392 if (!RawCode) {
6393 Error(RawCode.takeError());
6394 return QualType();
6395 }
6396
6397 ASTRecordReader Record(*this, *Loc.F);
6398 Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6399 if (!Code) {
6400 Error(Code.takeError());
6401 return QualType();
6402 }
6403 if (Code.get() == TYPE_EXT_QUAL) {
6404 QualType baseType = Record.readQualType();
6405 Qualifiers quals = Record.readQualifiers();
6406 return Context.getQualifiedType(baseType, quals);
6407 }
6408
6409 auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6410 if (!maybeClass) {
6411 Error("Unexpected code for type");
6412 return QualType();
6413 }
6414
6415 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6416 return TypeReader.read(*maybeClass);
6417}
6418
6419namespace clang {
6420
6421class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6422 ASTRecordReader &Reader;
6423
6424 SourceLocation readSourceLocation() {
6425 return Reader.readSourceLocation();
6426 }
6427
6428 TypeSourceInfo *GetTypeSourceInfo() {
6429 return Reader.readTypeSourceInfo();
6430 }
6431
6432 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6433 return Reader.readNestedNameSpecifierLoc();
6434 }
6435
6436 Attr *ReadAttr() {
6437 return Reader.readAttr();
6438 }
6439
6440public:
6441 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
6442
6443 // We want compile-time assurance that we've enumerated all of
6444 // these, so unfortunately we have to declare them first, then
6445 // define them out-of-line.
6446#define ABSTRACT_TYPELOC(CLASS, PARENT)
6447#define TYPELOC(CLASS, PARENT) \
6448 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6449#include "clang/AST/TypeLocNodes.def"
6450
6451 void VisitFunctionTypeLoc(FunctionTypeLoc);
6452 void VisitArrayTypeLoc(ArrayTypeLoc);
6453};
6454
6455} // namespace clang
6456
6457void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6458 // nothing to do
6459}
6460
6461void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6462 TL.setBuiltinLoc(readSourceLocation());
6463 if (TL.needsExtraLocalData()) {
6464 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6465 TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt()));
6466 TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt()));
6467 TL.setModeAttr(Reader.readInt());
6468 }
6469}
6470
6471void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6472 TL.setNameLoc(readSourceLocation());
6473}
6474
6475void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6476 TL.setStarLoc(readSourceLocation());
6477}
6478
6479void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6480 // nothing to do
6481}
6482
6483void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6484 // nothing to do
6485}
6486
6487void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6488 TL.setExpansionLoc(readSourceLocation());
6489}
6490
6491void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6492 TL.setCaretLoc(readSourceLocation());
6493}
6494
6495void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6496 TL.setAmpLoc(readSourceLocation());
6497}
6498
6499void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6500 TL.setAmpAmpLoc(readSourceLocation());
6501}
6502
6503void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6504 TL.setStarLoc(readSourceLocation());
6505 TL.setClassTInfo(GetTypeSourceInfo());
6506}
6507
6508void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6509 TL.setLBracketLoc(readSourceLocation());
6510 TL.setRBracketLoc(readSourceLocation());
6511 if (Reader.readBool())
6512 TL.setSizeExpr(Reader.readExpr());
6513 else
6514 TL.setSizeExpr(nullptr);
6515}
6516
6517void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6518 VisitArrayTypeLoc(TL);
6519}
6520
6521void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6522 VisitArrayTypeLoc(TL);
6523}
6524
6525void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6526 VisitArrayTypeLoc(TL);
6527}
6528
6529void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6530 DependentSizedArrayTypeLoc TL) {
6531 VisitArrayTypeLoc(TL);
6532}
6533
6534void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6535 DependentAddressSpaceTypeLoc TL) {
6536
6537 TL.setAttrNameLoc(readSourceLocation());
6538 TL.setAttrOperandParensRange(Reader.readSourceRange());
6539 TL.setAttrExprOperand(Reader.readExpr());
6540}
6541
6542void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6543 DependentSizedExtVectorTypeLoc TL) {
6544 TL.setNameLoc(readSourceLocation());
6545}
6546
6547void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6548 TL.setNameLoc(readSourceLocation());
6549}
6550
6551void TypeLocReader::VisitDependentVectorTypeLoc(
6552 DependentVectorTypeLoc TL) {
6553 TL.setNameLoc(readSourceLocation());
6554}
6555
6556void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6557 TL.setNameLoc(readSourceLocation());
6558}
6559
6560void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
6561 TL.setAttrNameLoc(readSourceLocation());
6562 TL.setAttrOperandParensRange(Reader.readSourceRange());
6563 TL.setAttrRowOperand(Reader.readExpr());
6564 TL.setAttrColumnOperand(Reader.readExpr());
6565}
6566
6567void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
6568 DependentSizedMatrixTypeLoc TL) {
6569 TL.setAttrNameLoc(readSourceLocation());
6570 TL.setAttrOperandParensRange(Reader.readSourceRange());
6571 TL.setAttrRowOperand(Reader.readExpr());
6572 TL.setAttrColumnOperand(Reader.readExpr());
6573}
6574
6575void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6576 TL.setLocalRangeBegin(readSourceLocation());
6577 TL.setLParenLoc(readSourceLocation());
6578 TL.setRParenLoc(readSourceLocation());
6579 TL.setExceptionSpecRange(Reader.readSourceRange());
6580 TL.setLocalRangeEnd(readSourceLocation());
6581 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6582 TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
6583 }
6584}
6585
6586void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6587 VisitFunctionTypeLoc(TL);
6588}
6589
6590void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6591 VisitFunctionTypeLoc(TL);
6592}
6593
6594void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6595 TL.setNameLoc(readSourceLocation());
6596}
6597
6598void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6599 TL.setNameLoc(readSourceLocation());
6600}
6601
6602void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6603 TL.setTypeofLoc(readSourceLocation());
6604 TL.setLParenLoc(readSourceLocation());
6605 TL.setRParenLoc(readSourceLocation());
6606}
6607
6608void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6609 TL.setTypeofLoc(readSourceLocation());
6610 TL.setLParenLoc(readSourceLocation());
6611 TL.setRParenLoc(readSourceLocation());
6612 TL.setUnderlyingTInfo(GetTypeSourceInfo());
6613}
6614
6615void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6616 TL.setNameLoc(readSourceLocation());
6617}
6618
6619void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6620 TL.setKWLoc(readSourceLocation());
6621 TL.setLParenLoc(readSourceLocation());
6622 TL.setRParenLoc(readSourceLocation());
6623 TL.setUnderlyingTInfo(GetTypeSourceInfo());
6624}
6625
6626void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6627 TL.setNameLoc(readSourceLocation());
6628 if (Reader.readBool()) {
6629 TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc());
6630 TL.setTemplateKWLoc(readSourceLocation());
6631 TL.setConceptNameLoc(readSourceLocation());
6632 TL.setFoundDecl(Reader.readDeclAs<NamedDecl>());
6633 TL.setLAngleLoc(readSourceLocation());
6634 TL.setRAngleLoc(readSourceLocation());
6635 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6636 TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo(
6637 TL.getTypePtr()->getArg(i).getKind()));
6638 }
6639}
6640
6641void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6642 DeducedTemplateSpecializationTypeLoc TL) {
6643 TL.setTemplateNameLoc(readSourceLocation());
6644}
6645
6646void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6647 TL.setNameLoc(readSourceLocation());
6648}
6649
6650void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6651 TL.setNameLoc(readSourceLocation());
6652}
6653
6654void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6655 TL.setAttr(ReadAttr());
6656}
6657
6658void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6659 TL.setNameLoc(readSourceLocation());
6660}
6661
6662void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6663 SubstTemplateTypeParmTypeLoc TL) {
6664 TL.setNameLoc(readSourceLocation());
6665}
6666
6667void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6668 SubstTemplateTypeParmPackTypeLoc TL) {
6669 TL.setNameLoc(readSourceLocation());
6670}
6671
6672void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6673 TemplateSpecializationTypeLoc TL) {
6674 TL.setTemplateKeywordLoc(readSourceLocation());
6675 TL.setTemplateNameLoc(readSourceLocation());
6676 TL.setLAngleLoc(readSourceLocation());
6677 TL.setRAngleLoc(readSourceLocation());
6678 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6679 TL.setArgLocInfo(
6680 i,
6681 Reader.readTemplateArgumentLocInfo(
6682 TL.getTypePtr()->getArg(i).getKind()));
6683}
6684
6685void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
6686 TL.setLParenLoc(readSourceLocation());
6687 TL.setRParenLoc(readSourceLocation());
6688}
6689
6690void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
6691 TL.setElaboratedKeywordLoc(readSourceLocation());
6692 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6693}
6694
6695void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
6696 TL.setNameLoc(readSourceLocation());
6697}
6698
6699void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
6700 TL.setElaboratedKeywordLoc(readSourceLocation());
6701 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6702 TL.setNameLoc(readSourceLocation());
6703}
6704
6705void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6706 DependentTemplateSpecializationTypeLoc TL) {
6707 TL.setElaboratedKeywordLoc(readSourceLocation());
6708 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6709 TL.setTemplateKeywordLoc(readSourceLocation());
6710 TL.setTemplateNameLoc(readSourceLocation());
6711 TL.setLAngleLoc(readSourceLocation());
6712 TL.setRAngleLoc(readSourceLocation());
6713 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
6714 TL.setArgLocInfo(
6715 I,
6716 Reader.readTemplateArgumentLocInfo(
6717 TL.getTypePtr()->getArg(I).getKind()));
6718}
6719
6720void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
6721 TL.setEllipsisLoc(readSourceLocation());
6722}
6723
6724void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
6725 TL.setNameLoc(readSourceLocation());
6726}
6727
6728void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6729 if (TL.getNumProtocols()) {
6730 TL.setProtocolLAngleLoc(readSourceLocation());
6731 TL.setProtocolRAngleLoc(readSourceLocation());
6732 }
6733 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6734 TL.setProtocolLoc(i, readSourceLocation());
6735}
6736
6737void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
6738 TL.setHasBaseTypeAsWritten(Reader.readBool());
6739 TL.setTypeArgsLAngleLoc(readSourceLocation());
6740 TL.setTypeArgsRAngleLoc(readSourceLocation());
6741 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
6742 TL.setTypeArgTInfo(i, GetTypeSourceInfo());
6743 TL.setProtocolLAngleLoc(readSourceLocation());
6744 TL.setProtocolRAngleLoc(readSourceLocation());
6745 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6746 TL.setProtocolLoc(i, readSourceLocation());
6747}
6748
6749void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
6750 TL.setStarLoc(readSourceLocation());
6751}
6752
6753void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
6754 TL.setKWLoc(readSourceLocation());
6755 TL.setLParenLoc(readSourceLocation());
6756 TL.setRParenLoc(readSourceLocation());
6757}
6758
6759void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
6760 TL.setKWLoc(readSourceLocation());
6761}
6762
6763void TypeLocReader::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) {
6764 TL.setNameLoc(readSourceLocation());
6765}
6766void TypeLocReader::VisitDependentExtIntTypeLoc(
6767 clang::DependentExtIntTypeLoc TL) {
6768 TL.setNameLoc(readSourceLocation());
6769}
6770
6771
6772void ASTRecordReader::readTypeLoc(TypeLoc TL) {
6773 TypeLocReader TLR(*this);
6774 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6775 TLR.Visit(TL);
6776}
6777
6778TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
6779 QualType InfoTy = readType();
6780 if (InfoTy.isNull())
6781 return nullptr;
6782
6783 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
6784 readTypeLoc(TInfo->getTypeLoc());
6785 return TInfo;
6786}
6787
6788QualType ASTReader::GetType(TypeID ID) {
6789 assert(ContextObj && "reading type with no AST context")(static_cast<void> (0));
6790 ASTContext &Context = *ContextObj;
6791
6792 unsigned FastQuals = ID & Qualifiers::FastMask;
6793 unsigned Index = ID >> Qualifiers::FastWidth;
6794
6795 if (Index < NUM_PREDEF_TYPE_IDS) {
6796 QualType T;
6797 switch ((PredefinedTypeIDs)Index) {
6798 case PREDEF_TYPE_NULL_ID:
6799 return QualType();
6800 case PREDEF_TYPE_VOID_ID:
6801 T = Context.VoidTy;
6802 break;
6803 case PREDEF_TYPE_BOOL_ID:
6804 T = Context.BoolTy;
6805 break;
6806 case PREDEF_TYPE_CHAR_U_ID:
6807 case PREDEF_TYPE_CHAR_S_ID:
6808 // FIXME: Check that the signedness of CharTy is correct!
6809 T = Context.CharTy;
6810 break;
6811 case PREDEF_TYPE_UCHAR_ID:
6812 T = Context.UnsignedCharTy;
6813 break;
6814 case PREDEF_TYPE_USHORT_ID:
6815 T = Context.UnsignedShortTy;
6816 break;
6817 case PREDEF_TYPE_UINT_ID:
6818 T = Context.UnsignedIntTy;
6819 break;
6820 case PREDEF_TYPE_ULONG_ID:
6821 T = Context.UnsignedLongTy;
6822 break;
6823 case PREDEF_TYPE_ULONGLONG_ID:
6824 T = Context.UnsignedLongLongTy;
6825 break;
6826 case PREDEF_TYPE_UINT128_ID:
6827 T = Context.UnsignedInt128Ty;
6828 break;
6829 case PREDEF_TYPE_SCHAR_ID:
6830 T = Context.SignedCharTy;
6831 break;
6832 case PREDEF_TYPE_WCHAR_ID:
6833 T = Context.WCharTy;
6834 break;
6835 case PREDEF_TYPE_SHORT_ID:
6836 T = Context.ShortTy;
6837 break;
6838 case PREDEF_TYPE_INT_ID:
6839 T = Context.IntTy;
6840 break;
6841 case PREDEF_TYPE_LONG_ID:
6842 T = Context.LongTy;
6843 break;
6844 case PREDEF_TYPE_LONGLONG_ID:
6845 T = Context.LongLongTy;
6846 break;
6847 case PREDEF_TYPE_INT128_ID:
6848 T = Context.Int128Ty;
6849 break;
6850 case PREDEF_TYPE_BFLOAT16_ID:
6851 T = Context.BFloat16Ty;
6852 break;
6853 case PREDEF_TYPE_HALF_ID:
6854 T = Context.HalfTy;
6855 break;
6856 case PREDEF_TYPE_FLOAT_ID:
6857 T = Context.FloatTy;
6858 break;
6859 case PREDEF_TYPE_DOUBLE_ID:
6860 T = Context.DoubleTy;
6861 break;
6862 case PREDEF_TYPE_LONGDOUBLE_ID:
6863 T = Context.LongDoubleTy;
6864 break;
6865 case PREDEF_TYPE_SHORT_ACCUM_ID:
6866 T = Context.ShortAccumTy;
6867 break;
6868 case PREDEF_TYPE_ACCUM_ID:
6869 T = Context.AccumTy;
6870 break;
6871 case PREDEF_TYPE_LONG_ACCUM_ID:
6872 T = Context.LongAccumTy;
6873 break;
6874 case PREDEF_TYPE_USHORT_ACCUM_ID:
6875 T = Context.UnsignedShortAccumTy;
6876 break;
6877 case PREDEF_TYPE_UACCUM_ID:
6878 T = Context.UnsignedAccumTy;
6879 break;
6880 case PREDEF_TYPE_ULONG_ACCUM_ID:
6881 T = Context.UnsignedLongAccumTy;
6882 break;
6883 case PREDEF_TYPE_SHORT_FRACT_ID:
6884 T = Context.ShortFractTy;
6885 break;
6886 case PREDEF_TYPE_FRACT_ID:
6887 T = Context.FractTy;
6888 break;
6889 case PREDEF_TYPE_LONG_FRACT_ID:
6890 T = Context.LongFractTy;
6891 break;
6892 case PREDEF_TYPE_USHORT_FRACT_ID:
6893 T = Context.UnsignedShortFractTy;
6894 break;
6895 case PREDEF_TYPE_UFRACT_ID:
6896 T = Context.UnsignedFractTy;
6897 break;
6898 case PREDEF_TYPE_ULONG_FRACT_ID:
6899 T = Context.UnsignedLongFractTy;
6900 break;
6901 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6902 T = Context.SatShortAccumTy;
6903 break;
6904 case PREDEF_TYPE_SAT_ACCUM_ID:
6905 T = Context.SatAccumTy;
6906 break;
6907 case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6908 T = Context.SatLongAccumTy;
6909 break;
6910 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6911 T = Context.SatUnsignedShortAccumTy;
6912 break;
6913 case PREDEF_TYPE_SAT_UACCUM_ID:
6914 T = Context.SatUnsignedAccumTy;
6915 break;
6916 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6917 T = Context.SatUnsignedLongAccumTy;
6918 break;
6919 case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6920 T = Context.SatShortFractTy;
6921 break;
6922 case PREDEF_TYPE_SAT_FRACT_ID:
6923 T = Context.SatFractTy;
6924 break;
6925 case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6926 T = Context.SatLongFractTy;
6927 break;
6928 case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6929 T = Context.SatUnsignedShortFractTy;
6930 break;
6931 case PREDEF_TYPE_SAT_UFRACT_ID:
6932 T = Context.SatUnsignedFractTy;
6933 break;
6934 case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6935 T = Context.SatUnsignedLongFractTy;
6936 break;
6937 case PREDEF_TYPE_FLOAT16_ID:
6938 T = Context.Float16Ty;
6939 break;
6940 case PREDEF_TYPE_FLOAT128_ID:
6941 T = Context.Float128Ty;
6942 break;
6943 case PREDEF_TYPE_OVERLOAD_ID:
6944 T = Context.OverloadTy;
6945 break;
6946 case PREDEF_TYPE_BOUND_MEMBER:
6947 T = Context.BoundMemberTy;
6948 break;
6949 case PREDEF_TYPE_PSEUDO_OBJECT:
6950 T = Context.PseudoObjectTy;
6951 break;
6952 case PREDEF_TYPE_DEPENDENT_ID:
6953 T = Context.DependentTy;
6954 break;
6955 case PREDEF_TYPE_UNKNOWN_ANY:
6956 T = Context.UnknownAnyTy;
6957 break;
6958 case PREDEF_TYPE_NULLPTR_ID:
6959 T = Context.NullPtrTy;
6960 break;
6961 case PREDEF_TYPE_CHAR8_ID:
6962 T = Context.Char8Ty;
6963 break;
6964 case PREDEF_TYPE_CHAR16_ID:
6965 T = Context.Char16Ty;
6966 break;
6967 case PREDEF_TYPE_CHAR32_ID:
6968 T = Context.Char32Ty;
6969 break;
6970 case PREDEF_TYPE_OBJC_ID:
6971 T = Context.ObjCBuiltinIdTy;
6972 break;
6973 case PREDEF_TYPE_OBJC_CLASS:
6974 T = Context.ObjCBuiltinClassTy;
6975 break;
6976 case PREDEF_TYPE_OBJC_SEL:
6977 T = Context.ObjCBuiltinSelTy;
6978 break;
6979#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
6980 case PREDEF_TYPE_##Id##_ID: \
6981 T = Context.SingletonId; \
6982 break;
6983#include "clang/Basic/OpenCLImageTypes.def"
6984#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
6985 case PREDEF_TYPE_##Id##_ID: \
6986 T = Context.Id##Ty; \
6987 break;
6988#include "clang/Basic/OpenCLExtensionTypes.def"
6989 case PREDEF_TYPE_SAMPLER_ID:
6990 T = Context.OCLSamplerTy;
6991 break;
6992 case PREDEF_TYPE_EVENT_ID:
6993 T = Context.OCLEventTy;
6994 break;
6995 case PREDEF_TYPE_CLK_EVENT_ID:
6996 T = Context.OCLClkEventTy;
6997 break;
6998 case PREDEF_TYPE_QUEUE_ID:
6999 T = Context.OCLQueueTy;
7000 break;
7001 case PREDEF_TYPE_RESERVE_ID_ID:
7002 T = Context.OCLReserveIDTy;
7003 break;
7004 case PREDEF_TYPE_AUTO_DEDUCT:
7005 T = Context.getAutoDeductType();
7006 break;
7007 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7008 T = Context.getAutoRRefDeductType();
7009 break;
7010 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7011 T = Context.ARCUnbridgedCastTy;
7012 break;
7013 case PREDEF_TYPE_BUILTIN_FN:
7014 T = Context.BuiltinFnTy;
7015 break;
7016 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7017 T = Context.IncompleteMatrixIdxTy;
7018 break;
7019 case PREDEF_TYPE_OMP_ARRAY_SECTION:
7020 T = Context.OMPArraySectionTy;
7021 break;
7022 case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7023 T = Context.OMPArraySectionTy;
7024 break;
7025 case PREDEF_TYPE_OMP_ITERATOR:
7026 T = Context.OMPIteratorTy;
7027 break;
7028#define SVE_TYPE(Name, Id, SingletonId) \
7029 case PREDEF_TYPE_##Id##_ID: \
7030 T = Context.SingletonId; \
7031 break;
7032#include "clang/Basic/AArch64SVEACLETypes.def"
7033#define PPC_VECTOR_TYPE(Name, Id, Size) \
7034 case PREDEF_TYPE_##Id##_ID: \
7035 T = Context.Id##Ty; \
7036 break;
7037#include "clang/Basic/PPCTypes.def"
7038#define RVV_TYPE(Name, Id, SingletonId) \
7039 case PREDEF_TYPE_##Id##_ID: \
7040 T = Context.SingletonId; \
7041 break;
7042#include "clang/Basic/RISCVVTypes.def"
7043 }
7044
7045 assert(!T.isNull() && "Unknown predefined type")(static_cast<void> (0));
7046 return T.withFastQualifiers(FastQuals);
7047 }
7048
7049 Index -= NUM_PREDEF_TYPE_IDS;
7050 assert(Index < TypesLoaded.size() && "Type index out-of-range")(static_cast<void> (0));
7051 if (TypesLoaded[Index].isNull()) {
7052 TypesLoaded[Index] = readTypeRecord(Index);
7053 if (TypesLoaded[Index].isNull())
7054 return QualType();
7055
7056 TypesLoaded[Index]->setFromAST();
7057 if (DeserializationListener)
7058 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7059 TypesLoaded[Index]);
7060 }
7061
7062 return TypesLoaded[Index].withFastQualifiers(FastQuals);
7063}
7064
7065QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7066 return GetType(getGlobalTypeID(F, LocalID));
7067}
7068
7069serialization::TypeID
7070ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7071 unsigned FastQuals = LocalID & Qualifiers::FastMask;
7072 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7073
7074 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7075 return LocalID;
7076
7077 if (!F.ModuleOffsetMap.empty())
7078 ReadModuleOffsetMap(F);
7079
7080 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7081 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7082 assert(I != F.TypeRemap.end() && "Invalid index into type index remap")(static_cast<void> (0));
7083
7084 unsigned GlobalIndex = LocalIndex + I->second;
7085 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7086}
7087
7088TemplateArgumentLocInfo
7089ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7090 switch (Kind) {
7091 case TemplateArgument::Expression:
7092 return readExpr();
7093 case TemplateArgument::Type:
7094 return readTypeSourceInfo();
7095 case TemplateArgument::Template: {
7096 NestedNameSpecifierLoc QualifierLoc =
7097 readNestedNameSpecifierLoc();
7098 SourceLocation TemplateNameLoc = readSourceLocation();
7099 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7100 TemplateNameLoc, SourceLocation());
7101 }
7102 case TemplateArgument::TemplateExpansion: {
7103 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7104 SourceLocation TemplateNameLoc = readSourceLocation();
7105 SourceLocation EllipsisLoc = readSourceLocation();
7106 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7107 TemplateNameLoc, EllipsisLoc);
7108 }
7109 case TemplateArgument::Null:
7110 case TemplateArgument::Integral:
7111 case TemplateArgument::Declaration:
7112 case TemplateArgument::NullPtr:
7113 case TemplateArgument::Pack:
7114 // FIXME: Is this right?
7115 return TemplateArgumentLocInfo();
7116 }
7117 llvm_unreachable("unexpected template argument loc")__builtin_unreachable();
7118}
7119
7120TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7121 TemplateArgument Arg = readTemplateArgument();
7122
7123 if (Arg.getKind() == TemplateArgument::Expression) {
7124 if (readBool()) // bool InfoHasSameExpr.
7125 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7126 }
7127 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
7128}
7129
7130const ASTTemplateArgumentListInfo *
7131ASTRecordReader::readASTTemplateArgumentListInfo() {
7132 SourceLocation LAngleLoc = readSourceLocation();
7133 SourceLocation RAngleLoc = readSourceLocation();
7134 unsigned NumArgsAsWritten = readInt();
7135 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7136 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7137 TemplArgsInfo.addArgument(readTemplateArgumentLoc());
7138 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7139}
7140
7141Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7142 return GetDecl(ID);
7143}
7144
7145void ASTReader::CompleteRedeclChain(const Decl *D) {
7146 if (NumCurrentElementsDeserializing) {
7147 // We arrange to not care about the complete redeclaration chain while we're
7148 // deserializing. Just remember that the AST has marked this one as complete
7149 // but that it's not actually complete yet, so we know we still need to
7150 // complete it later.
7151 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7152 return;
7153 }
7154
7155 if (!D->getDeclContext()) {
7156 assert(isa<TranslationUnitDecl>(D) && "Not a TU?")(static_cast<void> (0));
7157 return;
7158 }
7159
7160 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7161
7162 // If this is a named declaration, complete it by looking it up
7163 // within its context.
7164 //
7165 // FIXME: Merging a function definition should merge
7166 // all mergeable entities within it.
7167 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7168 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7169 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7170 if (!getContext().getLangOpts().CPlusPlus &&
7171 isa<TranslationUnitDecl>(DC)) {
7172 // Outside of C++, we don't have a lookup table for the TU, so update
7173 // the identifier instead. (For C++ modules, we don't store decls
7174 // in the serialized identifier table, so we do the lookup in the TU.)
7175 auto *II = Name.getAsIdentifierInfo();
7176 assert(II && "non-identifier name in C?")(static_cast<void> (0));
7177 if (II->isOutOfDate())
7178 updateOutOfDateIdentifier(*II);
7179 } else
7180 DC->lookup(Name);
7181 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7182 // Find all declarations of this kind from the relevant context.
7183 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7184 auto *DC = cast<DeclContext>(DCDecl);
7185 SmallVector<Decl*, 8> Decls;
7186 FindExternalLexicalDecls(
7187 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7188 }
7189 }
7190 }
7191
7192 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7193 CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7194 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7195 VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7196 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7197 if (auto *Template = FD->getPrimaryTemplate())
7198 Template->LoadLazySpecializations();
7199 }
7200}
7201
7202CXXCtorInitializer **
7203ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7204 RecordLocation Loc = getLocalBitOffset(Offset);
7205 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7206 SavedStreamPosition SavedPosition(Cursor);
7207 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7208 Error(std::move(Err));
7209 return nullptr;
7210 }
7211 ReadingKindTracker ReadingKind(Read_Decl, *this);
7212
7213 Expected<unsigned> MaybeCode = Cursor.ReadCode();
7214 if (!MaybeCode) {
7215 Error(MaybeCode.takeError());
7216 return nullptr;
7217 }
7218 unsigned Code = MaybeCode.get();
7219
7220 ASTRecordReader Record(*this, *Loc.F);
7221 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7222 if (!MaybeRecCode) {
7223 Error(MaybeRecCode.takeError());
7224 return nullptr;
7225 }
7226 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7227 Error("malformed AST file: missing C++ ctor initializers");
7228 return nullptr;
7229 }
7230
7231 return Record.readCXXCtorInitializers();
7232}
7233
7234CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7235 assert(ContextObj && "reading base specifiers with no AST context")(static_cast<void> (0));
7236 ASTContext &Context = *ContextObj;
7237
7238 RecordLocation Loc = getLocalBitOffset(Offset);
7239 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7240 SavedStreamPosition SavedPosition(Cursor);
7241 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7242 Error(std::move(Err));
7243 return nullptr;
7244 }
7245 ReadingKindTracker ReadingKind(Read_Decl, *this);
7246
7247 Expected<unsigned> MaybeCode = Cursor.ReadCode();
7248 if (!MaybeCode) {
7249 Error(MaybeCode.takeError());
7250 return nullptr;
7251 }
7252 unsigned Code = MaybeCode.get();
7253
7254 ASTRecordReader Record(*this, *Loc.F);
7255 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7256 if (!MaybeRecCode) {
7257 Error(MaybeCode.takeError());
7258 return nullptr;
7259 }
7260 unsigned RecCode = MaybeRecCode.get();
7261
7262 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7263 Error("malformed AST file: missing C++ base specifiers");
7264 return nullptr;
7265 }
7266
7267 unsigned NumBases = Record.readInt();
7268 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7269 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7270 for (unsigned I = 0; I != NumBases; ++I)
7271 Bases[I] = Record.readCXXBaseSpecifier();
7272 return Bases;
7273}
7274
7275serialization::DeclID
7276ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7277 if (LocalID < NUM_PREDEF_DECL_IDS)
7278 return LocalID;
7279
7280 if (!F.ModuleOffsetMap.empty())
7281 ReadModuleOffsetMap(F);
7282
7283 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7284 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7285 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap")(static_cast<void> (0));
7286
7287 return LocalID + I->second;
7288}
7289
7290bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7291 ModuleFile &M) const {
7292 // Predefined decls aren't from any module.
7293 if (ID < NUM_PREDEF_DECL_IDS)
7294 return false;
7295
7296 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7297 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7298}
7299
7300ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7301 if (!D->isFromASTFile())
7302 return nullptr;
7303 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7304 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map")(static_cast<void> (0));
7305 return I->second;
7306}
7307
7308SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7309 if (ID < NUM_PREDEF_DECL_IDS)
7310 return SourceLocation();
7311
7312 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7313
7314 if (Index > DeclsLoaded.size()) {
7315 Error("declaration ID out-of-range for AST file");
7316 return SourceLocation();
7317 }
7318
7319 if (Decl *D = DeclsLoaded[Index])
7320 return D->getLocation();
7321
7322 SourceLocation Loc;
7323 DeclCursorForID(ID, Loc);
7324 return Loc;
7325}
7326
7327static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7328 switch (ID) {
7329 case PREDEF_DECL_NULL_ID:
7330 return nullptr;
7331
7332 case PREDEF_DECL_TRANSLATION_UNIT_ID:
7333 return Context.getTranslationUnitDecl();
7334
7335 case PREDEF_DECL_OBJC_ID_ID:
7336 return Context.getObjCIdDecl();
7337
7338 case PREDEF_DECL_OBJC_SEL_ID:
7339 return Context.getObjCSelDecl();
7340
7341 case PREDEF_DECL_OBJC_CLASS_ID:
7342 return Context.getObjCClassDecl();
7343
7344 case PREDEF_DECL_OBJC_PROTOCOL_ID:
7345 return Context.getObjCProtocolDecl();
7346
7347 case PREDEF_DECL_INT_128_ID:
7348 return Context.getInt128Decl();
7349
7350 case PREDEF_DECL_UNSIGNED_INT_128_ID:
7351 return Context.getUInt128Decl();
7352
7353 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7354 return Context.getObjCInstanceTypeDecl();
7355
7356 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7357 return Context.getBuiltinVaListDecl();
7358
7359 case PREDEF_DECL_VA_LIST_TAG:
7360 return Context.getVaListTagDecl();
7361
7362 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7363 return Context.getBuiltinMSVaListDecl();
7364
7365 case PREDEF_DECL_BUILTIN_MS_GUID_ID:
7366 return Context.getMSGuidTagDecl();
7367
7368 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7369 return Context.getExternCContextDecl();
7370
7371 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7372 return Context.getMakeIntegerSeqDecl();
7373
7374 case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7375 return Context.getCFConstantStringDecl();
7376
7377 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7378 return Context.getCFConstantStringTagDecl();
7379
7380 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7381 return Context.getTypePackElementDecl();
7382 }
7383 llvm_unreachable("PredefinedDeclIDs unknown enum value")__builtin_unreachable();
7384}
7385
7386Decl *ASTReader::GetExistingDecl(DeclID ID) {
7387 assert(ContextObj && "reading decl with no AST context")(static_cast<void> (0));
7388 if (ID < NUM_PREDEF_DECL_IDS) {
7389 Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7390 if (D) {
7391 // Track that we have merged the declaration with ID \p ID into the
7392 // pre-existing predefined declaration \p D.
7393 auto &Merged = KeyDecls[D->getCanonicalDecl()];
7394 if (Merged.empty())
7395 Merged.push_back(ID);
7396 }
7397 return D;
7398 }
7399
7400 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7401
7402 if (Index >= DeclsLoaded.size()) {
7403 assert(0 && "declaration ID out-of-range for AST file")(static_cast<void> (0));
7404 Error("declaration ID out-of-range for AST file");
7405 return nullptr;
7406 }
7407
7408 return DeclsLoaded[Index];
7409}
7410
7411Decl *ASTReader::GetDecl(DeclID ID) {
7412 if (ID < NUM_PREDEF_DECL_IDS)
7413 return GetExistingDecl(ID);
7414
7415 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7416
7417 if (Index >= DeclsLoaded.size()) {
7418 assert(0 && "declaration ID out-of-range for AST file")(static_cast<void> (0));
7419 Error("declaration ID out-of-range for AST file");
7420 return nullptr;
7421 }
7422
7423 if (!DeclsLoaded[Index]) {
7424 ReadDeclRecord(ID);
7425 if (DeserializationListener)
7426 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7427 }
7428
7429 return DeclsLoaded[Index];
7430}
7431
7432DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7433 DeclID GlobalID) {
7434 if (GlobalID < NUM_PREDEF_DECL_IDS)
7435 return GlobalID;
7436
7437 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7438 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map")(static_cast<void> (0));
7439 ModuleFile *Owner = I->second;
7440
7441 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7442 = M.GlobalToLocalDeclIDs.find(Owner);
7443 if (Pos == M.GlobalToLocalDeclIDs.end())
7444 return 0;
7445
7446 return GlobalID - Owner->BaseDeclID + Pos->second;
7447}
7448
7449serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7450 const RecordData &Record,
7451 unsigned &Idx) {
7452 if (Idx >= Record.size()) {
7453 Error("Corrupted AST file");
7454 return 0;
7455 }
7456
7457 return getGlobalDeclID(F, Record[Idx++]);
7458}
7459
7460/// Resolve the offset of a statement into a statement.
7461///
7462/// This operation will read a new statement from the external
7463/// source each time it is called, and is meant to be used via a
7464/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7465Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7466 // Switch case IDs are per Decl.
7467 ClearSwitchCaseIDs();
7468
7469 // Offset here is a global offset across the entire chain.
7470 RecordLocation Loc = getLocalBitOffset(Offset);
7471 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7472 Error(std::move(Err));
7473 return nullptr;
7474 }
7475 assert(NumCurrentElementsDeserializing == 0 &&(static_cast<void> (0))
7476 "should not be called while already deserializing")(static_cast<void> (0));
7477 Deserializing D(this);
7478 return ReadStmtFromStream(*Loc.F);
7479}
7480
7481void ASTReader::FindExternalLexicalDecls(
7482 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7483 SmallVectorImpl<Decl *> &Decls) {
7484 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7485
7486 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7487 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries")(static_cast<void> (0));
7488 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7489 auto K = (Decl::Kind)+LexicalDecls[I];
7490 if (!IsKindWeWant(K))
7491 continue;
7492
7493 auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7494
7495 // Don't add predefined declarations to the lexical context more
7496 // than once.
7497 if (ID < NUM_PREDEF_DECL_IDS) {
7498 if (PredefsVisited[ID])
7499 continue;
7500
7501 PredefsVisited[ID] = true;
7502 }
7503
7504 if (Decl *D = GetLocalDecl(*M, ID)) {
7505 assert(D->getKind() == K && "wrong kind for lexical decl")(static_cast<void> (0));
7506 if (!DC->isDeclInLexicalTraversal(D))
7507 Decls.push_back(D);
7508 }
7509 }
7510 };
7511
7512 if (isa<TranslationUnitDecl>(DC)) {
7513 for (auto Lexical : TULexicalDecls)
7514 Visit(Lexical.first, Lexical.second);
7515 } else {
7516 auto I = LexicalDecls.find(DC);
7517 if (I != LexicalDecls.end())
7518 Visit(I->second.first, I->second.second);
7519 }
7520
7521 ++NumLexicalDeclContextsRead;
7522}
7523
7524namespace {
7525
7526class DeclIDComp {
7527 ASTReader &Reader;
7528 ModuleFile &Mod;
7529
7530public:
7531 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7532
7533 bool operator()(LocalDeclID L, LocalDeclID R) const {
7534 SourceLocation LHS = getLocation(L);
7535 SourceLocation RHS = getLocation(R);
7536 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7537 }
7538
7539 bool operator()(SourceLocation LHS, LocalDeclID R) const {
7540 SourceLocation RHS = getLocation(R);
7541 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7542 }
7543
7544 bool operator()(LocalDeclID L, SourceLocation RHS) const {
7545 SourceLocation LHS = getLocation(L);
7546 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7547 }
7548
7549 SourceLocation getLocation(LocalDeclID ID) const {
7550 return Reader.getSourceManager().getFileLoc(
7551 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7552 }
7553};
7554
7555} // namespace
7556
7557void ASTReader::FindFileRegionDecls(FileID File,
7558 unsigned Offset, unsigned Length,
7559 SmallVectorImpl<Decl *> &Decls) {
7560 SourceManager &SM = getSourceManager();
7561
7562 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7563 if (I == FileDeclIDs.end())
7564 return;
7565
7566 FileDeclsInfo &DInfo = I->second;
7567 if (DInfo.Decls.empty())
7568 return;
7569
7570 SourceLocation
7571 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7572 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7573
7574 DeclIDComp DIDComp(*this, *DInfo.Mod);
7575 ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7576 llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7577 if (BeginIt != DInfo.Decls.begin())
7578 --BeginIt;
7579
7580 // If we are pointing at a top-level decl inside an objc container, we need
7581 // to backtrack until we find it otherwise we will fail to report that the
7582 // region overlaps with an objc container.
7583 while (BeginIt != DInfo.Decls.begin() &&
7584 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7585 ->isTopLevelDeclInObjCContainer())
7586 --BeginIt;
7587
7588 ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7589 llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7590 if (EndIt != DInfo.Decls.end())
7591 ++EndIt;
7592
7593 for (ArrayRef<serialization::LocalDeclID>::iterator
7594 DIt = BeginIt; DIt != EndIt; ++DIt)
7595 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7596}
7597
7598bool
7599ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7600 DeclarationName Name) {
7601 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&(static_cast<void> (0))
7602 "DeclContext has no visible decls in storage")(static_cast<void> (0));
7603 if (!Name)
7604 return false;
7605
7606 auto It = Lookups.find(DC);
7607 if (It == Lookups.end())
7608 return false;
7609
7610 Deserializing LookupResults(this);
7611
7612 // Load the list of declarations.
7613 SmallVector<NamedDecl *, 64> Decls;
7614 llvm::SmallPtrSet<NamedDecl *, 8> Found;
7615 for (DeclID ID : It->second.Table.find(Name)) {
7616 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7617 if (ND->getDeclName() == Name && Found.insert(ND).second)
7618 Decls.push_back(ND);
7619 }
7620
7621 ++NumVisibleDeclContextsRead;
7622 SetExternalVisibleDeclsForName(DC, Name, Decls);
7623 return !Decls.empty();
7624}
7625
7626void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7627 if (!DC->hasExternalVisibleStorage())
7628 return;
7629
7630 auto It = Lookups.find(DC);
7631 assert(It != Lookups.end() &&(static_cast<void> (0))
7632 "have external visible storage but no lookup tables")(static_cast<void> (0));
7633
7634 DeclsMap Decls;
7635
7636 for (DeclID ID : It->second.Table.findAll()) {
7637 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7638 Decls[ND->getDeclName()].push_back(ND);
7639 }
7640
7641 ++NumVisibleDeclContextsRead;
7642
7643 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7644 SetExternalVisibleDeclsForName(DC, I->first, I->second);
7645 }
7646 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7647}
7648
7649const serialization::reader::DeclContextLookupTable *
7650ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7651 auto I = Lookups.find(Primary);
7652 return I == Lookups.end() ? nullptr : &I->second;
7653}
7654
7655/// Under non-PCH compilation the consumer receives the objc methods
7656/// before receiving the implementation, and codegen depends on this.
7657/// We simulate this by deserializing and passing to consumer the methods of the
7658/// implementation before passing the deserialized implementation decl.
7659static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7660 ASTConsumer *Consumer) {
7661 assert(ImplD && Consumer)(static_cast<void> (0));
7662
7663 for (auto *I : ImplD->methods())
7664 Consumer->HandleInterestingDecl(DeclGroupRef(I));
7665
7666 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7667}
7668
7669void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
7670 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
7671 PassObjCImplDeclToConsumer(ImplD, Consumer);
7672 else
7673 Consumer->HandleInterestingDecl(DeclGroupRef(D));
7674}
7675
7676void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7677 this->Consumer = Consumer;
7678
7679 if (Consumer)
7680 PassInterestingDeclsToConsumer();
7681
7682 if (DeserializationListener)
7683 DeserializationListener->ReaderInitialized(this);
7684}
7685
7686void ASTReader::PrintStats() {
7687 std::fprintf(stderrstderr, "*** AST File Statistics:\n");
7688
7689 unsigned NumTypesLoaded
7690 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
7691 QualType());
7692 unsigned NumDeclsLoaded
7693 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
7694 (Decl *)nullptr);
7695 unsigned NumIdentifiersLoaded
7696 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
7697 IdentifiersLoaded.end(),
7698 (IdentifierInfo *)nullptr);
7699 unsigned NumMacrosLoaded
7700 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
7701 MacrosLoaded.end(),
7702 (MacroInfo *)nullptr);
7703 unsigned NumSelectorsLoaded
7704 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
7705 SelectorsLoaded.end(),
7706 Selector());
7707
7708 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7709 std::fprintf(stderrstderr, " %u/%u source location entries read (%f%%)\n",
7710 NumSLocEntriesRead, TotalNumSLocEntries,
7711 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7712 if (!TypesLoaded.empty())
7713 std::fprintf(stderrstderr, " %u/%u types read (%f%%)\n",
7714 NumTypesLoaded, (unsigned)TypesLoaded.size(),
7715 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7716 if (!DeclsLoaded.empty())
7717 std::fprintf(stderrstderr, " %u/%u declarations read (%f%%)\n",
7718 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7719 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7720 if (!IdentifiersLoaded.empty())
7721 std::fprintf(stderrstderr, " %u/%u identifiers read (%f%%)\n",
7722 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7723 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7724 if (!MacrosLoaded.empty())
7725 std::fprintf(stderrstderr, " %u/%u macros read (%f%%)\n",
7726 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7727 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7728 if (!SelectorsLoaded.empty())
7729 std::fprintf(stderrstderr, " %u/%u selectors read (%f%%)\n",
7730 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7731 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7732 if (TotalNumStatements)
7733 std::fprintf(stderrstderr, " %u/%u statements read (%f%%)\n",
7734 NumStatementsRead, TotalNumStatements,
7735 ((float)NumStatementsRead/TotalNumStatements * 100));
7736 if (TotalNumMacros)
7737 std::fprintf(stderrstderr, " %u/%u macros read (%f%%)\n",
7738 NumMacrosRead, TotalNumMacros,
7739 ((float)NumMacrosRead/TotalNumMacros * 100));
7740 if (TotalLexicalDeclContexts)
7741 std::fprintf(stderrstderr, " %u/%u lexical declcontexts read (%f%%)\n",
7742 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7743 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7744 * 100));
7745 if (TotalVisibleDeclContexts)
7746 std::fprintf(stderrstderr, " %u/%u visible declcontexts read (%f%%)\n",
7747 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7748 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7749 * 100));
7750 if (TotalNumMethodPoolEntries)
7751 std::fprintf(stderrstderr, " %u/%u method pool entries read (%f%%)\n",
7752 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7753 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7754 * 100));
7755 if (NumMethodPoolLookups)
7756 std::fprintf(stderrstderr, " %u/%u method pool lookups succeeded (%f%%)\n",
7757 NumMethodPoolHits, NumMethodPoolLookups,
7758 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
7759 if (NumMethodPoolTableLookups)
7760 std::fprintf(stderrstderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
7761 NumMethodPoolTableHits, NumMethodPoolTableLookups,
7762 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7763 * 100.0));
7764 if (NumIdentifierLookupHits)
7765 std::fprintf(stderrstderr,
7766 " %u / %u identifier table lookups succeeded (%f%%)\n",
7767 NumIdentifierLookupHits, NumIdentifierLookups,
7768 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
7769
7770 if (GlobalIndex) {
7771 std::fprintf(stderrstderr, "\n");
7772 GlobalIndex->printStats();
7773 }
7774
7775 std::fprintf(stderrstderr, "\n");
7776 dump();
7777 std::fprintf(stderrstderr, "\n");
7778}
7779
7780template<typename Key, typename ModuleFile, unsigned InitialCapacity>
7781LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) static void
7782dumpModuleIDMap(StringRef Name,
7783 const ContinuousRangeMap<Key, ModuleFile *,
7784 InitialCapacity> &Map) {
7785 if (Map.begin() == Map.end())
7786 return;
7787
7788 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7789
7790 llvm::errs() << Name << ":\n";
7791 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7792 I != IEnd; ++I) {
7793 llvm::errs() << " " << I->first << " -> " << I->second->FileName
7794 << "\n";
7795 }
7796}
7797
7798LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void ASTReader::dump() {
7799 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7800 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7801 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7802 dumpModuleIDMap("Global type map", GlobalTypeMap);
7803 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7804 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7805 dumpModuleIDMap("Global macro map", GlobalMacroMap);
7806 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7807 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
7808 dumpModuleIDMap("Global preprocessed entity map",
7809 GlobalPreprocessedEntityMap);
7810
7811 llvm::errs() << "\n*** PCH/Modules Loaded:";
7812 for (ModuleFile &M : ModuleMgr)
7813 M.dump();
7814}
7815
7816/// Return the amount of memory used by memory buffers, breaking down
7817/// by heap-backed versus mmap'ed memory.
7818void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
7819 for (ModuleFile &I : ModuleMgr) {
7820 if (llvm::MemoryBuffer *buf = I.Buffer) {
7821 size_t bytes = buf->getBufferSize();
7822 switch (buf->getBufferKind()) {
7823 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7824 sizes.malloc_bytes += bytes;
7825 break;
7826 case llvm::MemoryBuffer::MemoryBuffer_MMap:
7827 sizes.mmap_bytes += bytes;
7828 break;
7829 }
7830 }
7831 }
7832}
7833
7834void ASTReader::InitializeSema(Sema &S) {
7835 SemaObj = &S;
7836 S.addExternalSource(this);
7837
7838 // Makes sure any declarations that were deserialized "too early"
7839 // still get added to the identifier's declaration chains.
7840 for (uint64_t ID : PreloadedDeclIDs) {
7841 NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7842 pushExternalDeclIntoScope(D, D->getDeclName());
7843 }
7844 PreloadedDeclIDs.clear();
7845
7846 // FIXME: What happens if these are changed by a module import?
7847 if (!FPPragmaOptions.empty()) {
7848 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS")(static_cast<void> (0));
7849 FPOptionsOverride NewOverrides =
7850 FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]);
7851 SemaObj->CurFPFeatures =
7852 NewOverrides.applyOverrides(SemaObj->getLangOpts());
7853 }
7854
7855 SemaObj->OpenCLFeatures = OpenCLExtensions;
7856
7857 UpdateSema();
7858}
7859
7860void ASTReader::UpdateSema() {
7861 assert(SemaObj && "no Sema to update")(static_cast<void> (0));
7862
7863 // Load the offsets of the declarations that Sema references.
7864 // They will be lazily deserialized when needed.
7865 if (!SemaDeclRefs.empty()) {
7866 assert(SemaDeclRefs.size() % 3 == 0)(static_cast<void> (0));
7867 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
7868 if (!SemaObj->StdNamespace)
7869 SemaObj->StdNamespace = SemaDeclRefs[I];
7870 if (!SemaObj->StdBadAlloc)
7871 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7872 if (!SemaObj->StdAlignValT)
7873 SemaObj->StdAlignValT = SemaDeclRefs[I+2];
7874 }
7875 SemaDeclRefs.clear();
7876 }
7877
7878 // Update the state of pragmas. Use the same API as if we had encountered the
7879 // pragma in the source.
7880 if(OptimizeOffPragmaLocation.isValid())
7881 SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
7882 if (PragmaMSStructState != -1)
7883 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7884 if (PointersToMembersPragmaLocation.isValid()) {
7885 SemaObj->ActOnPragmaMSPointersToMembers(
7886 (LangOptions::PragmaMSPointersToMembersKind)
7887 PragmaMSPointersToMembersState,
7888 PointersToMembersPragmaLocation);
7889 }
7890 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
7891
7892 if (PragmaAlignPackCurrentValue) {
7893 // The bottom of the stack might have a default value. It must be adjusted
7894 // to the current value to ensure that the packing state is preserved after
7895 // popping entries that were included/imported from a PCH/module.
7896 bool DropFirst = false;
7897 if (!PragmaAlignPackStack.empty() &&
7898 PragmaAlignPackStack.front().Location.isInvalid()) {
7899 assert(PragmaAlignPackStack.front().Value ==(static_cast<void> (0))
7900 SemaObj->AlignPackStack.DefaultValue &&(static_cast<void> (0))
7901 "Expected a default alignment value")(static_cast<void> (0));
7902 SemaObj->AlignPackStack.Stack.emplace_back(
7903 PragmaAlignPackStack.front().SlotLabel,
7904 SemaObj->AlignPackStack.CurrentValue,
7905 SemaObj->AlignPackStack.CurrentPragmaLocation,
7906 PragmaAlignPackStack.front().PushLocation);
7907 DropFirst = true;
7908 }
7909 for (const auto &Entry : llvm::makeArrayRef(PragmaAlignPackStack)
7910 .drop_front(DropFirst ? 1 : 0)) {
7911 SemaObj->AlignPackStack.Stack.emplace_back(
7912 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7913 }
7914 if (PragmaAlignPackCurrentLocation.isInvalid()) {
7915 assert(*PragmaAlignPackCurrentValue ==(static_cast<void> (0))
7916 SemaObj->AlignPackStack.DefaultValue &&(static_cast<void> (0))
7917 "Expected a default align and pack value")(static_cast<void> (0));
7918 // Keep the current values.
7919 } else {
7920 SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue;
7921 SemaObj->AlignPackStack.CurrentPragmaLocation =
7922 PragmaAlignPackCurrentLocation;
7923 }
7924 }
7925 if (FpPragmaCurrentValue) {
7926 // The bottom of the stack might have a default value. It must be adjusted
7927 // to the current value to ensure that fp-pragma state is preserved after
7928 // popping entries that were included/imported from a PCH/module.
7929 bool DropFirst = false;
7930 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
7931 assert(FpPragmaStack.front().Value ==(static_cast<void> (0))
7932 SemaObj->FpPragmaStack.DefaultValue &&(static_cast<void> (0))
7933 "Expected a default pragma float_control value")(static_cast<void> (0));
7934 SemaObj->FpPragmaStack.Stack.emplace_back(
7935 FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue,
7936 SemaObj->FpPragmaStack.CurrentPragmaLocation,
7937 FpPragmaStack.front().PushLocation);
7938 DropFirst = true;
7939 }
7940 for (const auto &Entry :
7941 llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0))
7942 SemaObj->FpPragmaStack.Stack.emplace_back(
7943 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7944 if (FpPragmaCurrentLocation.isInvalid()) {
7945 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&(static_cast<void> (0))
7946 "Expected a default pragma float_control value")(static_cast<void> (0));
7947 // Keep the current values.
7948 } else {
7949 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
7950 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
7951 }
7952 }
7953
7954 // For non-modular AST files, restore visiblity of modules.
7955 for (auto &Import : ImportedModules) {
7956 if (Import.ImportLoc.isInvalid())
7957 continue;
7958 if (Module *Imported = getSubmodule(Import.ID)) {
7959 SemaObj->makeModuleVisible(Imported, Import.ImportLoc);
7960 }
7961 }
7962}
7963
7964IdentifierInfo *ASTReader::get(StringRef Name) {
7965 // Note that we are loading an identifier.
7966 Deserializing AnIdentifier(this);
7967
7968 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
7969 NumIdentifierLookups,
7970 NumIdentifierLookupHits);
7971
7972 // We don't need to do identifier table lookups in C++ modules (we preload
7973 // all interesting declarations, and don't need to use the scope for name
7974 // lookups). Perform the lookup in PCH files, though, since we don't build
7975 // a complete initial identifier table if we're carrying on from a PCH.
7976 if (PP.getLangOpts().CPlusPlus) {
7977 for (auto F : ModuleMgr.pch_modules())
7978 if (Visitor(*F))
7979 break;
7980 } else {
7981 // If there is a global index, look there first to determine which modules
7982 // provably do not have any results for this identifier.
7983 GlobalModuleIndex::HitSet Hits;
7984 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
7985 if (!loadGlobalIndex()) {
7986 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
7987 HitsPtr = &Hits;
7988 }
7989 }
7990
7991 ModuleMgr.visit(Visitor, HitsPtr);
7992 }
7993
7994 IdentifierInfo *II = Visitor.getIdentifierInfo();
7995 markIdentifierUpToDate(II);
7996 return II;
7997}
7998
7999namespace clang {
8000
8001 /// An identifier-lookup iterator that enumerates all of the
8002 /// identifiers stored within a set of AST files.
8003 class ASTIdentifierIterator : public IdentifierIterator {
8004 /// The AST reader whose identifiers are being enumerated.
8005 const ASTReader &Reader;
8006
8007 /// The current index into the chain of AST files stored in
8008 /// the AST reader.
8009 unsigned Index;
8010
8011 /// The current position within the identifier lookup table
8012 /// of the current AST file.
8013 ASTIdentifierLookupTable::key_iterator Current;
8014
8015 /// The end position within the identifier lookup table of
8016 /// the current AST file.
8017 ASTIdentifierLookupTable::key_iterator End;
8018
8019 /// Whether to skip any modules in the ASTReader.
8020 bool SkipModules;
8021
8022 public:
8023 explicit ASTIdentifierIterator(const ASTReader &Reader,
8024 bool SkipModules = false);
8025
8026 StringRef Next() override;
8027 };
8028
8029} // namespace clang
8030
8031ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8032 bool SkipModules)
8033 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8034}
8035
8036StringRef ASTIdentifierIterator::Next() {
8037 while (Current == End) {
8038 // If we have exhausted all of our AST files, we're done.
8039 if (Index == 0)
8040 return StringRef();
8041
8042 --Index;
8043 ModuleFile &F = Reader.ModuleMgr[Index];
8044 if (SkipModules && F.isModule())
8045 continue;
8046
8047 ASTIdentifierLookupTable *IdTable =
8048 (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8049 Current = IdTable->key_begin();
8050 End = IdTable->key_end();
8051 }
8052
8053 // We have any identifiers remaining in the current AST file; return
8054 // the next one.
8055 StringRef Result = *Current;
8056 ++Current;
8057 return Result;
8058}
8059
8060namespace {
8061
8062/// A utility for appending two IdentifierIterators.
8063class ChainedIdentifierIterator : public IdentifierIterator {
8064 std::unique_ptr<IdentifierIterator> Current;
8065 std::unique_ptr<IdentifierIterator> Queued;
8066
8067public:
8068 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8069 std::unique_ptr<IdentifierIterator> Second)
8070 : Current(std::move(First)), Queued(std::move(Second)) {}
8071
8072 StringRef Next() override {
8073 if (!Current)
8074 return StringRef();
8075
8076 StringRef result = Current->Next();
8077 if (!result.empty())
8078 return result;
8079
8080 // Try the queued iterator, which may itself be empty.
8081 Current.reset();
8082 std::swap(Current, Queued);
8083 return Next();
8084 }
8085};
8086
8087} // namespace
8088
8089IdentifierIterator *ASTReader::getIdentifiers() {
8090 if (!loadGlobalIndex()) {
8091 std::unique_ptr<IdentifierIterator> ReaderIter(
8092 new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8093 std::unique_ptr<IdentifierIterator> ModulesIter(
8094 GlobalIndex->createIdentifierIterator());
8095 return new ChainedIdentifierIterator(std::move(ReaderIter),
8096 std::move(ModulesIter));
8097 }
8098
8099 return new ASTIdentifierIterator(*this);
8100}
8101
8102namespace clang {
8103namespace serialization {
8104
8105 class ReadMethodPoolVisitor {
8106 ASTReader &Reader;
8107 Selector Sel;
8108 unsigned PriorGeneration;
8109 unsigned InstanceBits = 0;
8110 unsigned FactoryBits = 0;
8111 bool InstanceHasMoreThanOneDecl = false;
8112 bool FactoryHasMoreThanOneDecl = false;
8113 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8114 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8115
8116 public:
8117 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8118 unsigned PriorGeneration)
8119 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8120
8121 bool operator()(ModuleFile &M) {
8122 if (!M.SelectorLookupTable)
8123 return false;
8124
8125 // If we've already searched this module file, skip it now.
8126 if (M.Generation <= PriorGeneration)
8127 return true;
8128
8129 ++Reader.NumMethodPoolTableLookups;
8130 ASTSelectorLookupTable *PoolTable
8131 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8132 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8133 if (Pos == PoolTable->end())
8134 return false;
8135
8136 ++Reader.NumMethodPoolTableHits;
8137 ++Reader.NumSelectorsRead;
8138 // FIXME: Not quite happy with the statistics here. We probably should
8139 // disable this tracking when called via LoadSelector.
8140 // Also, should entries without methods count as misses?
8141 ++Reader.NumMethodPoolEntriesRead;
8142 ASTSelectorLookupTrait::data_type Data = *Pos;
8143 if (Reader.DeserializationListener)
8144 Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8145
8146 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8147 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8148 InstanceBits = Data.InstanceBits;
8149 FactoryBits = Data.FactoryBits;
8150 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8151 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8152 return true;
8153 }
8154
8155 /// Retrieve the instance methods found by this visitor.
8156 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8157 return InstanceMethods;
8158 }
8159
8160 /// Retrieve the instance methods found by this visitor.
8161 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8162 return FactoryMethods;
8163 }
8164
8165 unsigned getInstanceBits() const { return InstanceBits; }
8166 unsigned getFactoryBits() const { return FactoryBits; }
8167
8168 bool instanceHasMoreThanOneDecl() const {
8169 return InstanceHasMoreThanOneDecl;
8170 }
8171
8172 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8173 };
8174
8175} // namespace serialization
8176} // namespace clang
8177
8178/// Add the given set of methods to the method list.
8179static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8180 ObjCMethodList &List) {
8181 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8182 S.addMethodToGlobalList(&List, Methods[I]);
8183 }
8184}
8185
8186void ASTReader::ReadMethodPool(Selector Sel) {
8187 // Get the selector generation and update it to the current generation.
8188 unsigned &Generation = SelectorGeneration[Sel];
8189 unsigned PriorGeneration = Generation;
8190 Generation = getGeneration();
8191 SelectorOutOfDate[Sel] = false;
8192
8193 // Search for methods defined with this selector.
8194 ++NumMethodPoolLookups;
8195 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8196 ModuleMgr.visit(Visitor);
8197
8198 if (Visitor.getInstanceMethods().empty() &&
8199 Visitor.getFactoryMethods().empty())
8200 return;
8201
8202 ++NumMethodPoolHits;
8203
8204 if (!getSema())
8205 return;
8206
8207 Sema &S = *getSema();
8208 Sema::GlobalMethodPool::iterator Pos
8209 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8210
8211 Pos->second.first.setBits(Visitor.getInstanceBits());
8212 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8213 Pos->second.second.setBits(Visitor.getFactoryBits());
8214 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8215
8216 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8217 // when building a module we keep every method individually and may need to
8218 // update hasMoreThanOneDecl as we add the methods.
8219 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8220 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8221}
8222
8223void ASTReader::updateOutOfDateSelector(Selector Sel) {
8224 if (SelectorOutOfDate[Sel])
8225 ReadMethodPool(Sel);
8226}
8227
8228void ASTReader::ReadKnownNamespaces(
8229 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8230 Namespaces.clear();
8231
8232 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8233 if (NamespaceDecl *Namespace
8234 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8235 Namespaces.push_back(Namespace);
8236 }
8237}
8238
8239void ASTReader::ReadUndefinedButUsed(
8240 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8241 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8242 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8243 SourceLocation Loc =
8244 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8245 Undefined.insert(std::make_pair(D, Loc));
8246 }
8247}
8248
8249void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8250 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8251 Exprs) {
8252 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8253 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8254 uint64_t Count = DelayedDeleteExprs[Idx++];
8255 for (uint64_t C = 0; C < Count; ++C) {
8256 SourceLocation DeleteLoc =
8257 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8258 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8259 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8260 }
8261 }
8262}
8263
8264void ASTReader::ReadTentativeDefinitions(
8265 SmallVectorImpl<VarDecl *> &TentativeDefs) {
8266 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8267 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8268 if (Var)
8269 TentativeDefs.push_back(Var);
8270 }
8271 TentativeDefinitions.clear();
8272}
8273
8274void ASTReader::ReadUnusedFileScopedDecls(
8275 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8276 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8277 DeclaratorDecl *D
8278 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8279 if (D)
8280 Decls.push_back(D);
8281 }
8282 UnusedFileScopedDecls.clear();
8283}
8284
8285void ASTReader::ReadDelegatingConstructors(
8286 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8287 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8288 CXXConstructorDecl *D
8289 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8290 if (D)
8291 Decls.push_back(D);
8292 }
8293 DelegatingCtorDecls.clear();
8294}
8295
8296void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8297 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8298 TypedefNameDecl *D
8299 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8300 if (D)
8301 Decls.push_back(D);
8302 }
8303 ExtVectorDecls.clear();
8304}
8305
8306void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8307 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8308 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8309 ++I) {
8310 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8311 GetDecl(UnusedLocalTypedefNameCandidates[I]));
8312 if (D)
8313 Decls.insert(D);
8314 }
8315 UnusedLocalTypedefNameCandidates.clear();
8316}
8317
8318void ASTReader::ReadDeclsToCheckForDeferredDiags(
8319 llvm::SmallSetVector<Decl *, 4> &Decls) {
8320 for (auto I : DeclsToCheckForDeferredDiags) {
8321 auto *D = dyn_cast_or_null<Decl>(GetDecl(I));
8322 if (D)
8323 Decls.insert(D);
8324 }
8325 DeclsToCheckForDeferredDiags.clear();
8326}
8327
8328void ASTReader::ReadReferencedSelectors(
8329 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8330 if (ReferencedSelectorsData.empty())
8331 return;
8332
8333 // If there are @selector references added them to its pool. This is for
8334 // implementation of -Wselector.
8335 unsigned int DataSize = ReferencedSelectorsData.size()-1;
8336 unsigned I = 0;
8337 while (I < DataSize) {
8338 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8339 SourceLocation SelLoc
8340 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8341 Sels.push_back(std::make_pair(Sel, SelLoc));
8342 }
8343 ReferencedSelectorsData.clear();
8344}
8345
8346void ASTReader::ReadWeakUndeclaredIdentifiers(
8347 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8348 if (WeakUndeclaredIdentifiers.empty())
8349 return;
8350
8351 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8352 IdentifierInfo *WeakId
8353 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8354 IdentifierInfo *AliasId
8355 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8356 SourceLocation Loc
8357 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8358 bool Used = WeakUndeclaredIdentifiers[I++];
8359 WeakInfo WI(AliasId, Loc);
8360 WI.setUsed(Used);
8361 WeakIDs.push_back(std::make_pair(WeakId, WI));
8362 }
8363 WeakUndeclaredIdentifiers.clear();
8364}
8365
8366void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8367 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8368 ExternalVTableUse VT;
8369 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8370 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8371 VT.DefinitionRequired = VTableUses[Idx++];
8372 VTables.push_back(VT);
8373 }
8374
8375 VTableUses.clear();
8376}
8377
8378void ASTReader::ReadPendingInstantiations(
8379 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8380 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8381 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8382 SourceLocation Loc
8383 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8384
8385 Pending.push_back(std::make_pair(D, Loc));
8386 }
8387 PendingInstantiations.clear();
8388}
8389
8390void ASTReader::ReadLateParsedTemplates(
8391 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8392 &LPTMap) {
8393 for (auto &LPT : LateParsedTemplates) {
8394 ModuleFile *FMod = LPT.first;
8395 RecordDataImpl &LateParsed = LPT.second;
8396 for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
8397 /* In loop */) {
8398 FunctionDecl *FD =
8399 cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++]));
8400
8401 auto LT = std::make_unique<LateParsedTemplate>();
8402 LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]);
8403
8404 ModuleFile *F = getOwningModuleFile(LT->D);
8405 assert(F && "No module")(static_cast<void> (0));
8406
8407 unsigned TokN = LateParsed[Idx++];
8408 LT->Toks.reserve(TokN);
8409 for (unsigned T = 0; T < TokN; ++T)
8410 LT->Toks.push_back(ReadToken(*F, LateParsed, Idx));
8411
8412 LPTMap.insert(std::make_pair(FD, std::move(LT)));
8413 }
8414 }
8415
8416 LateParsedTemplates.clear();
8417}
8418
8419void ASTReader::LoadSelector(Selector Sel) {
8420 // It would be complicated to avoid reading the methods anyway. So don't.
8421 ReadMethodPool(Sel);
8422}
8423
8424void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8425 assert(ID && "Non-zero identifier ID required")(static_cast<void> (0));
8426 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range")(static_cast<void> (0));
8427 IdentifiersLoaded[ID - 1] = II;
8428 if (DeserializationListener)
8429 DeserializationListener->IdentifierRead(ID, II);
8430}
8431
8432/// Set the globally-visible declarations associated with the given
8433/// identifier.
8434///
8435/// If the AST reader is currently in a state where the given declaration IDs
8436/// cannot safely be resolved, they are queued until it is safe to resolve
8437/// them.
8438///
8439/// \param II an IdentifierInfo that refers to one or more globally-visible
8440/// declarations.
8441///
8442/// \param DeclIDs the set of declaration IDs with the name @p II that are
8443/// visible at global scope.
8444///
8445/// \param Decls if non-null, this vector will be populated with the set of
8446/// deserialized declarations. These declarations will not be pushed into
8447/// scope.
8448void
8449ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8450 const SmallVectorImpl<uint32_t> &DeclIDs,
8451 SmallVectorImpl<Decl *> *Decls) {
8452 if (NumCurrentElementsDeserializing && !Decls) {
8453 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8454 return;
8455 }
8456
8457 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8458 if (!SemaObj) {
8459 // Queue this declaration so that it will be added to the
8460 // translation unit scope and identifier's declaration chain
8461 // once a Sema object is known.
8462 PreloadedDeclIDs.push_back(DeclIDs[I]);
8463 continue;
8464 }
8465
8466 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8467
8468 // If we're simply supposed to record the declarations, do so now.
8469 if (Decls) {
8470 Decls->push_back(D);
8471 continue;
8472 }
8473
8474 // Introduce this declaration into the translation-unit scope
8475 // and add it to the declaration chain for this identifier, so
8476 // that (unqualified) name lookup will find it.
8477 pushExternalDeclIntoScope(D, II);
8478 }
8479}
8480
8481IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8482 if (ID == 0)
8483 return nullptr;
8484
8485 if (IdentifiersLoaded.empty()) {
8486 Error("no identifier table in AST file");
8487 return nullptr;
8488 }
8489
8490 ID -= 1;
8491 if (!IdentifiersLoaded[ID]) {
8492 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8493 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map")(static_cast<void> (0));
8494 ModuleFile *M = I->second;
8495 unsigned Index = ID - M->BaseIdentifierID;
8496 const unsigned char *Data =
8497 M->IdentifierTableData + M->IdentifierOffsets[Index];
8498
8499 ASTIdentifierLookupTrait Trait(*this, *M);
8500 auto KeyDataLen = Trait.ReadKeyDataLength(Data);
8501 auto Key = Trait.ReadKey(Data, KeyDataLen.first);
8502 auto &II = PP.getIdentifierTable().get(Key);
8503 IdentifiersLoaded[ID] = &II;
8504 markIdentifierFromAST(*this, II);
8505 if (DeserializationListener)
8506 DeserializationListener->IdentifierRead(ID + 1, &II);
8507 }
8508
8509 return IdentifiersLoaded[ID];
8510}
8511
8512IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8513 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8514}
8515
8516IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8517 if (LocalID < NUM_PREDEF_IDENT_IDS)
8518 return LocalID;
8519
8520 if (!M.ModuleOffsetMap.empty())
8521 ReadModuleOffsetMap(M);
8522
8523 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8524 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8525 assert(I != M.IdentifierRemap.end()(static_cast<void> (0))
8526 && "Invalid index into identifier index remap")(static_cast<void> (0));
8527
8528 return LocalID + I->second;
8529}
8530
8531MacroInfo *ASTReader::getMacro(MacroID ID) {
8532 if (ID == 0)
8533 return nullptr;
8534
8535 if (MacrosLoaded.empty()) {
8536 Error("no macro table in AST file");
8537 return nullptr;
8538 }
8539
8540 ID -= NUM_PREDEF_MACRO_IDS;
8541 if (!MacrosLoaded[ID]) {
8542 GlobalMacroMapType::iterator I
8543 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8544 assert(I != GlobalMacroMap.end() && "Corrupted global macro map")(static_cast<void> (0));
8545 ModuleFile *M = I->second;
8546 unsigned Index = ID - M->BaseMacroID;
8547 MacrosLoaded[ID] =
8548 ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]);
8549
8550 if (DeserializationListener)
8551 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8552 MacrosLoaded[ID]);
8553 }
8554
8555 return MacrosLoaded[ID];
8556}
8557
8558MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8559 if (LocalID < NUM_PREDEF_MACRO_IDS)
8560 return LocalID;
8561
8562 if (!M.ModuleOffsetMap.empty())
8563 ReadModuleOffsetMap(M);
8564
8565 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8566 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8567 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap")(static_cast<void> (0));
8568
8569 return LocalID + I->second;
8570}
8571
8572serialization::SubmoduleID
8573ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8574 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8575 return LocalID;
8576
8577 if (!M.ModuleOffsetMap.empty())
8578 ReadModuleOffsetMap(M);
8579
8580 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8581 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8582 assert(I != M.SubmoduleRemap.end()(static_cast<void> (0))
8583 && "Invalid index into submodule index remap")(static_cast<void> (0));
8584
8585 return LocalID + I->second;
8586}
8587
8588Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8589 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8590 assert(GlobalID == 0 && "Unhandled global submodule ID")(static_cast<void> (0));
8591 return nullptr;
8592 }
8593
8594 if (GlobalID > SubmodulesLoaded.size()) {
8595 Error("submodule ID out of range in AST file");
8596 return nullptr;
8597 }
8598
8599 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8600}
8601
8602Module *ASTReader::getModule(unsigned ID) {
8603 return getSubmodule(ID);
8604}
8605
8606ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8607 if (ID & 1) {
8608 // It's a module, look it up by submodule ID.
8609 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8610 return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8611 } else {
8612 // It's a prefix (preamble, PCH, ...). Look it up by index.
8613 unsigned IndexFromEnd = ID >> 1;
8614 assert(IndexFromEnd && "got reference to unknown module file")(static_cast<void> (0));
8615 return getModuleManager().pch_modules().end()[-IndexFromEnd];
8616 }
8617}
8618
8619unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8620 if (!F)
8621 return 1;
8622
8623 // For a file representing a module, use the submodule ID of the top-level
8624 // module as the file ID. For any other kind of file, the number of such
8625 // files loaded beforehand will be the same on reload.
8626 // FIXME: Is this true even if we have an explicit module file and a PCH?
8627 if (F->isModule())
8628 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8629
8630 auto PCHModules = getModuleManager().pch_modules();
8631 auto I = llvm::find(PCHModules, F);
8632 assert(I != PCHModules.end() && "emitting reference to unknown file")(static_cast<void> (0));
8633 return (I - PCHModules.end()) << 1;
8634}
8635
8636llvm::Optional<ASTSourceDescriptor>
8637ASTReader::getSourceDescriptor(unsigned ID) {
8638 if (Module *M = getSubmodule(ID))
8639 return ASTSourceDescriptor(*M);
8640
8641 // If there is only a single PCH, return it instead.
8642 // Chained PCH are not supported.
8643 const auto &PCHChain = ModuleMgr.pch_modules();
8644 if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8645 ModuleFile &MF = ModuleMgr.getPrimaryModule();
8646 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8647 StringRef FileName = llvm::sys::path::filename(MF.FileName);
8648 return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8649 MF.Signature);
8650 }
8651 return None;
8652}
8653
8654ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8655 auto I = DefinitionSource.find(FD);
8656 if (I == DefinitionSource.end())
8657 return EK_ReplyHazy;
8658 return I->second ? EK_Never : EK_Always;
8659}
8660
8661Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8662 return DecodeSelector(getGlobalSelectorID(M, LocalID));
8663}
8664
8665Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8666 if (ID == 0)
8667 return Selector();
8668
8669 if (ID > SelectorsLoaded.size()) {
8670 Error("selector ID out of range in AST file");
8671 return Selector();
8672 }
8673
8674 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
8675 // Load this selector from the selector table.
8676 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8677 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map")(static_cast<void> (0));
8678 ModuleFile &M = *I->second;
8679 ASTSelectorLookupTrait Trait(*this, M);
8680 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8681 SelectorsLoaded[ID - 1] =
8682 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8683 if (DeserializationListener)
8684 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8685 }
8686
8687 return SelectorsLoaded[ID - 1];
8688}
8689
8690Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8691 return DecodeSelector(ID);
8692}
8693
8694uint32_t ASTReader::GetNumExternalSelectors() {
8695 // ID 0 (the null selector) is considered an external selector.
8696 return getTotalNumSelectors() + 1;
8697}
8698
8699serialization::SelectorID
8700ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8701 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8702 return LocalID;
8703
8704 if (!M.ModuleOffsetMap.empty())
8705 ReadModuleOffsetMap(M);
8706
8707 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8708 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
8709 assert(I != M.SelectorRemap.end()(static_cast<void> (0))
8710 && "Invalid index into selector index remap")(static_cast<void> (0));
8711
8712 return LocalID + I->second;
8713}
8714
8715DeclarationNameLoc
8716ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8717 switch (Name.getNameKind()) {
8718 case DeclarationName::CXXConstructorName:
8719 case DeclarationName::CXXDestructorName:
8720 case DeclarationName::CXXConversionFunctionName:
8721 return DeclarationNameLoc::makeNamedTypeLoc(readTypeSourceInfo());
8722
8723 case DeclarationName::CXXOperatorName:
8724 return DeclarationNameLoc::makeCXXOperatorNameLoc(readSourceRange());
8725
8726 case DeclarationName::CXXLiteralOperatorName:
8727 return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(
8728 readSourceLocation());
8729
8730 case DeclarationName::Identifier:
8731 case DeclarationName::ObjCZeroArgSelector:
8732 case DeclarationName::ObjCOneArgSelector:
8733 case DeclarationName::ObjCMultiArgSelector:
8734 case DeclarationName::CXXUsingDirective:
8735 case DeclarationName::CXXDeductionGuideName:
8736 break;
8737 }
8738 return DeclarationNameLoc();
8739}
8740
8741DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
8742 DeclarationNameInfo NameInfo;
8743 NameInfo.setName(readDeclarationName());
8744 NameInfo.setLoc(readSourceLocation());
8745 NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
8746 return NameInfo;
8747}
8748
8749void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
8750 Info.QualifierLoc = readNestedNameSpecifierLoc();
8751 unsigned NumTPLists = readInt();
8752 Info.NumTemplParamLists = NumTPLists;
8753 if (NumTPLists) {
8754 Info.TemplParamLists =
8755 new (getContext()) TemplateParameterList *[NumTPLists];
8756 for (unsigned i = 0; i != NumTPLists; ++i)
8757 Info.TemplParamLists[i] = readTemplateParameterList();
8758 }
8759}
8760
8761TemplateParameterList *
8762ASTRecordReader::readTemplateParameterList() {
8763 SourceLocation TemplateLoc = readSourceLocation();
8764 SourceLocation LAngleLoc = readSourceLocation();
8765 SourceLocation RAngleLoc = readSourceLocation();
8766
8767 unsigned NumParams = readInt();
8768 SmallVector<NamedDecl *, 16> Params;
8769 Params.reserve(NumParams);
8770 while (NumParams--)
8771 Params.push_back(readDeclAs<NamedDecl>());
8772
8773 bool HasRequiresClause = readBool();
8774 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
8775
8776 TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8777 getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
8778 return TemplateParams;
8779}
8780
8781void ASTRecordReader::readTemplateArgumentList(
8782 SmallVectorImpl<TemplateArgument> &TemplArgs,
8783 bool Canonicalize) {
8784 unsigned NumTemplateArgs = readInt();
8785 TemplArgs.reserve(NumTemplateArgs);
8786 while (NumTemplateArgs--)
8787 TemplArgs.push_back(readTemplateArgument(Canonicalize));
8788}
8789
8790/// Read a UnresolvedSet structure.
8791void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
8792 unsigned NumDecls = readInt();
8793 Set.reserve(getContext(), NumDecls);
8794 while (NumDecls--) {
8795 DeclID ID = readDeclID();
8796 AccessSpecifier AS = (AccessSpecifier) readInt();
8797 Set.addLazyDecl(getContext(), ID, AS);
8798 }
8799}
8800
8801CXXBaseSpecifier
8802ASTRecordReader::readCXXBaseSpecifier() {
8803 bool isVirtual = readBool();
8804 bool isBaseOfClass = readBool();
8805 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
8806 bool inheritConstructors = readBool();
8807 TypeSourceInfo *TInfo = readTypeSourceInfo();
8808 SourceRange Range = readSourceRange();
8809 SourceLocation EllipsisLoc = readSourceLocation();
8810 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
8811 EllipsisLoc);
8812 Result.setInheritConstructors(inheritConstructors);
8813 return Result;
8814}
8815
8816CXXCtorInitializer **
8817ASTRecordReader::readCXXCtorInitializers() {
8818 ASTContext &Context = getContext();
8819 unsigned NumInitializers = readInt();
8820 assert(NumInitializers && "wrote ctor initializers but have no inits")(static_cast<void> (0));
8821 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
8822 for (unsigned i = 0; i != NumInitializers; ++i) {
8823 TypeSourceInfo *TInfo = nullptr;
8824 bool IsBaseVirtual = false;
8825 FieldDecl *Member = nullptr;
8826 IndirectFieldDecl *IndirectMember = nullptr;
8827
8828 CtorInitializerType Type = (CtorInitializerType) readInt();
8829 switch (Type) {
8830 case CTOR_INITIALIZER_BASE:
8831 TInfo = readTypeSourceInfo();
8832 IsBaseVirtual = readBool();
8833 break;
8834
8835 case CTOR_INITIALIZER_DELEGATING:
8836 TInfo = readTypeSourceInfo();
8837 break;
8838
8839 case CTOR_INITIALIZER_MEMBER:
8840 Member = readDeclAs<FieldDecl>();
8841 break;
8842
8843 case CTOR_INITIALIZER_INDIRECT_MEMBER:
8844 IndirectMember = readDeclAs<IndirectFieldDecl>();
8845 break;
8846 }
8847
8848 SourceLocation MemberOrEllipsisLoc = readSourceLocation();
8849 Expr *Init = readExpr();
8850 SourceLocation LParenLoc = readSourceLocation();
8851 SourceLocation RParenLoc = readSourceLocation();
8852
8853 CXXCtorInitializer *BOMInit;
8854 if (Type == CTOR_INITIALIZER_BASE)
8855 BOMInit = new (Context)
8856 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8857 RParenLoc, MemberOrEllipsisLoc);
8858 else if (Type == CTOR_INITIALIZER_DELEGATING)
8859 BOMInit = new (Context)
8860 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8861 else if (Member)
8862 BOMInit = new (Context)
8863 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8864 Init, RParenLoc);
8865 else
8866 BOMInit = new (Context)
8867 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8868 LParenLoc, Init, RParenLoc);
8869
8870 if (/*IsWritten*/readBool()) {
8871 unsigned SourceOrder = readInt();
8872 BOMInit->setSourceOrder(SourceOrder);
8873 }
8874
8875 CtorInitializers[i] = BOMInit;
8876 }
8877
8878 return CtorInitializers;
8879}
8880
8881NestedNameSpecifierLoc
8882ASTRecordReader::readNestedNameSpecifierLoc() {
8883 ASTContext &Context = getContext();
8884 unsigned N = readInt();
8885 NestedNameSpecifierLocBuilder Builder;
8886 for (unsigned I = 0; I != N; ++I) {
8887 auto Kind = readNestedNameSpecifierKind();
8888 switch (Kind) {
8889 case NestedNameSpecifier::Identifier: {
8890 IdentifierInfo *II = readIdentifier();
8891 SourceRange Range = readSourceRange();
8892 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8893 break;
8894 }
8895
8896 case NestedNameSpecifier::Namespace: {
8897 NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
8898 SourceRange Range = readSourceRange();
8899 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8900 break;
8901 }
8902
8903 case NestedNameSpecifier::NamespaceAlias: {
8904 NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
8905 SourceRange Range = readSourceRange();
8906 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8907 break;
8908 }
8909
8910 case NestedNameSpecifier::TypeSpec:
8911 case NestedNameSpecifier::TypeSpecWithTemplate: {
8912 bool Template = readBool();
8913 TypeSourceInfo *T = readTypeSourceInfo();
8914 if (!T)
8915 return NestedNameSpecifierLoc();
8916 SourceLocation ColonColonLoc = readSourceLocation();
8917
8918 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8919 Builder.Extend(Context,
8920 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8921 T->getTypeLoc(), ColonColonLoc);
8922 break;
8923 }
8924
8925 case NestedNameSpecifier::Global: {
8926 SourceLocation ColonColonLoc = readSourceLocation();
8927 Builder.MakeGlobal(Context, ColonColonLoc);
8928 break;
8929 }
8930
8931 case NestedNameSpecifier::Super: {
8932 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
8933 SourceRange Range = readSourceRange();
8934 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8935 break;
8936 }
8937 }
8938 }
8939
8940 return Builder.getWithLocInContext(Context);
8941}
8942
8943SourceRange
8944ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8945 unsigned &Idx) {
8946 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8947 SourceLocation end = ReadSourceLocation(F, Record, Idx);
8948 return SourceRange(beg, end);
8949}
8950
8951/// Read a floating-point value
8952llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
8953 return llvm::APFloat(Sem, readAPInt());
8954}
8955
8956// Read a string
8957std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
8958 unsigned Len = Record[Idx++];
8959 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
8960 Idx += Len;
8961 return Result;
8962}
8963
8964std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
8965 unsigned &Idx) {
8966 std::string Filename = ReadString(Record, Idx);
8967 ResolveImportedPath(F, Filename);
8968 return Filename;
8969}
8970
8971std::string ASTReader::ReadPath(StringRef BaseDirectory,
8972 const RecordData &Record, unsigned &Idx) {
8973 std::string Filename = ReadString(Record, Idx);
8974 if (!BaseDirectory.empty())
8975 ResolveImportedPath(Filename, BaseDirectory);
8976 return Filename;
8977}
8978
8979VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
8980 unsigned &Idx) {
8981 unsigned Major = Record[Idx++];
8982 unsigned Minor = Record[Idx++];
8983 unsigned Subminor = Record[Idx++];
8984 if (Minor == 0)
8985 return VersionTuple(Major);
8986 if (Subminor == 0)
8987 return VersionTuple(Major, Minor - 1);
8988 return VersionTuple(Major, Minor - 1, Subminor - 1);
8989}
8990
8991CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
8992 const RecordData &Record,
8993 unsigned &Idx) {
8994 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
8995 return CXXTemporary::Create(getContext(), Decl);
8996}
8997
8998DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
8999 return Diag(CurrentImportLoc, DiagID);
9000}
9001
9002DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9003 return Diags.Report(Loc, DiagID);
9004}
9005
9006/// Retrieve the identifier table associated with the
9007/// preprocessor.
9008IdentifierTable &ASTReader::getIdentifierTable() {
9009 return PP.getIdentifierTable();
9010}
9011
9012/// Record that the given ID maps to the given switch-case
9013/// statement.
9014void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9015 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&(static_cast<void> (0))
9016 "Already have a SwitchCase with this ID")(static_cast<void> (0));
9017 (*CurrSwitchCaseStmts)[ID] = SC;
9018}
9019
9020/// Retrieve the switch-case statement with the given ID.
9021SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9022 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID")(static_cast<void> (0));
9023 return (*CurrSwitchCaseStmts)[ID];
9024}
9025
9026void ASTReader::ClearSwitchCaseIDs() {
9027 CurrSwitchCaseStmts->clear();
9028}
9029
9030void ASTReader::ReadComments() {
9031 ASTContext &Context = getContext();
9032 std::vector<RawComment *> Comments;
9033 for (SmallVectorImpl<std::pair<BitstreamCursor,
9034 serialization::ModuleFile *>>::iterator
9035 I = CommentsCursors.begin(),
9036 E = CommentsCursors.end();
9037 I != E; ++I) {
9038 Comments.clear();
9039 BitstreamCursor &Cursor = I->first;
9040 serialization::ModuleFile &F = *I->second;
9041 SavedStreamPosition SavedPosition(Cursor);
9042
9043 RecordData Record;
9044 while (true) {
9045 Expected<llvm::BitstreamEntry> MaybeEntry =
9046 Cursor.advanceSkippingSubblocks(
9047 BitstreamCursor::AF_DontPopBlockAtEnd);
9048 if (!MaybeEntry) {
9049 Error(MaybeEntry.takeError());
9050 return;
9051 }
9052 llvm::BitstreamEntry Entry = MaybeEntry.get();
9053
9054 switch (Entry.Kind) {
9055 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9056 case llvm::BitstreamEntry::Error:
9057 Error("malformed block record in AST file");
9058 return;
9059 case llvm::BitstreamEntry::EndBlock:
9060 goto NextCursor;
9061 case llvm::BitstreamEntry::Record:
9062 // The interesting case.
9063 break;
9064 }
9065
9066 // Read a record.
9067 Record.clear();
9068 Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9069 if (!MaybeComment) {
9070 Error(MaybeComment.takeError());
9071 return;
9072 }
9073 switch ((CommentRecordTypes)MaybeComment.get()) {
9074 case COMMENTS_RAW_COMMENT: {
9075 unsigned Idx = 0;
9076 SourceRange SR = ReadSourceRange(F, Record, Idx);
9077 RawComment::CommentKind Kind =
9078 (RawComment::CommentKind) Record[Idx++];
9079 bool IsTrailingComment = Record[Idx++];
9080 bool IsAlmostTrailingComment = Record[Idx++];
9081 Comments.push_back(new (Context) RawComment(
9082 SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9083 break;
9084 }
9085 }
9086 }
9087 NextCursor:
9088 llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9089 FileToOffsetToComment;
9090 for (RawComment *C : Comments) {
9091 SourceLocation CommentLoc = C->getBeginLoc();
9092 if (CommentLoc.isValid()) {
9093 std::pair<FileID, unsigned> Loc =
9094 SourceMgr.getDecomposedLoc(CommentLoc);
9095 if (Loc.first.isValid())
9096 Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9097 }
9098 }
9099 }
9100}
9101
9102void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9103 bool IncludeSystem, bool Complain,
9104 llvm::function_ref<void(const serialization::InputFile &IF,
9105 bool isSystem)> Visitor) {
9106 unsigned NumUserInputs = MF.NumUserInputFiles;
9107 unsigned NumInputs = MF.InputFilesLoaded.size();
9108 assert(NumUserInputs <= NumInputs)(static_cast<void> (0));
9109 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9110 for (unsigned I = 0; I < N; ++I) {
9111 bool IsSystem = I >= NumUserInputs;
9112 InputFile IF = getInputFile(MF, I+1, Complain);
9113 Visitor(IF, IsSystem);
9114 }
9115}
9116
9117void ASTReader::visitTopLevelModuleMaps(
9118 serialization::ModuleFile &MF,
9119 llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9120 unsigned NumInputs = MF.InputFilesLoaded.size();
9121 for (unsigned I = 0; I < NumInputs; ++I) {
9122 InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9123 if (IFI.TopLevelModuleMap)
9124 // FIXME: This unnecessarily re-reads the InputFileInfo.
9125 if (auto FE = getInputFile(MF, I + 1).getFile())
9126 Visitor(FE);
9127 }
9128}
9129
9130std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9131 // If we know the owning module, use it.
9132 if (Module *M = D->getImportedOwningModule())
9133 return M->getFullModuleName();
9134
9135 // Otherwise, use the name of the top-level module the decl is within.
9136 if (ModuleFile *M = getOwningModuleFile(D))
9137 return M->ModuleName;
9138
9139 // Not from a module.
9140 return {};
9141}
9142
9143void ASTReader::finishPendingActions() {
9144 while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9145 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9146 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9147 !PendingUpdateRecords.empty()) {
9148 // If any identifiers with corresponding top-level declarations have
9149 // been loaded, load those declarations now.
9150 using TopLevelDeclsMap =
9151 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9152 TopLevelDeclsMap TopLevelDecls;
9153
9154 while (!PendingIdentifierInfos.empty()) {
9155 IdentifierInfo *II = PendingIdentifierInfos.back().first;
9156 SmallVector<uint32_t, 4> DeclIDs =
9157 std::move(PendingIdentifierInfos.back().second);
9158 PendingIdentifierInfos.pop_back();
9159
9160 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9161 }
9162
9163 // Load each function type that we deferred loading because it was a
9164 // deduced type that might refer to a local type declared within itself.
9165 for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9166 auto *FD = PendingFunctionTypes[I].first;
9167 FD->setType(GetType(PendingFunctionTypes[I].second));
9168
9169 // If we gave a function a deduced return type, remember that we need to
9170 // propagate that along the redeclaration chain.
9171 auto *DT = FD->getReturnType()->getContainedDeducedType();
9172 if (DT && DT->isDeduced())
9173 PendingDeducedTypeUpdates.insert(
9174 {FD->getCanonicalDecl(), FD->getReturnType()});
9175 }
9176 PendingFunctionTypes.clear();
9177
9178 // For each decl chain that we wanted to complete while deserializing, mark
9179 // it as "still needs to be completed".
9180 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9181 markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9182 }
9183 PendingIncompleteDeclChains.clear();
9184
9185 // Load pending declaration chains.
9186 for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9187 loadPendingDeclChain(PendingDeclChains[I].first,
9188 PendingDeclChains[I].second);
9189 PendingDeclChains.clear();
9190
9191 // Make the most recent of the top-level declarations visible.
9192 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9193 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9194 IdentifierInfo *II = TLD->first;
9195 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9196 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9197 }
9198 }
9199
9200 // Load any pending macro definitions.
9201 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9202 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9203 SmallVector<PendingMacroInfo, 2> GlobalIDs;
9204 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9205 // Initialize the macro history from chained-PCHs ahead of module imports.
9206 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9207 ++IDIdx) {
9208 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9209 if (!Info.M->isModule())
9210 resolvePendingMacro(II, Info);
9211 }
9212 // Handle module imports.
9213 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9214 ++IDIdx) {
9215 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9216 if (Info.M->isModule())
9217 resolvePendingMacro(II, Info);
9218 }
9219 }
9220 PendingMacroIDs.clear();
9221
9222 // Wire up the DeclContexts for Decls that we delayed setting until
9223 // recursive loading is completed.
9224 while (!PendingDeclContextInfos.empty()) {
9225 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9226 PendingDeclContextInfos.pop_front();
9227 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9228 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9229 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9230 }
9231
9232 // Perform any pending declaration updates.
9233 while (!PendingUpdateRecords.empty()) {
9234 auto Update = PendingUpdateRecords.pop_back_val();
9235 ReadingKindTracker ReadingKind(Read_Decl, *this);
9236 loadDeclUpdateRecords(Update);
9237 }
9238 }
9239
9240 // At this point, all update records for loaded decls are in place, so any
9241 // fake class definitions should have become real.
9242 assert(PendingFakeDefinitionData.empty() &&(static_cast<void> (0))
9243 "faked up a class definition but never saw the real one")(static_cast<void> (0));
9244
9245 // If we deserialized any C++ or Objective-C class definitions, any
9246 // Objective-C protocol definitions, or any redeclarable templates, make sure
9247 // that all redeclarations point to the definitions. Note that this can only
9248 // happen now, after the redeclaration chains have been fully wired.
9249 for (Decl *D : PendingDefinitions) {
9250 if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9251 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9252 // Make sure that the TagType points at the definition.
9253 const_cast<TagType*>(TagT)->decl = TD;
9254 }
9255
9256 if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9257 for (auto *R = getMostRecentExistingDecl(RD); R;
9258 R = R->getPreviousDecl()) {
9259 assert((R == D) ==(static_cast<void> (0))
9260 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&(static_cast<void> (0))
9261 "declaration thinks it's the definition but it isn't")(static_cast<void> (0));
9262 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9263 }
9264 }
9265
9266 continue;
9267 }
9268
9269 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9270 // Make sure that the ObjCInterfaceType points at the definition.
9271 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9272 ->Decl = ID;
9273
9274 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9275 cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9276
9277 continue;
9278 }
9279
9280 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9281 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9282 cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9283
9284 continue;
9285 }
9286
9287 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9288 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9289 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9290 }
9291 PendingDefinitions.clear();
9292
9293 // Load the bodies of any functions or methods we've encountered. We do
9294 // this now (delayed) so that we can be sure that the declaration chains
9295 // have been fully wired up (hasBody relies on this).
9296 // FIXME: We shouldn't require complete redeclaration chains here.
9297 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9298 PBEnd = PendingBodies.end();
9299 PB != PBEnd; ++PB) {
9300 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9301 // For a function defined inline within a class template, force the
9302 // canonical definition to be the one inside the canonical definition of
9303 // the template. This ensures that we instantiate from a correct view
9304 // of the template.
9305 //
9306 // Sadly we can't do this more generally: we can't be sure that all
9307 // copies of an arbitrary class definition will have the same members
9308 // defined (eg, some member functions may not be instantiated, and some
9309 // special members may or may not have been implicitly defined).
9310 if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9311 if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9312 continue;
9313
9314 // FIXME: Check for =delete/=default?
9315 // FIXME: Complain about ODR violations here?
9316 const FunctionDecl *Defn = nullptr;
9317 if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9318 FD->setLazyBody(PB->second);
9319 } else {
9320 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9321 mergeDefinitionVisibility(NonConstDefn, FD);
9322
9323 if (!FD->isLateTemplateParsed() &&
9324 !NonConstDefn->isLateTemplateParsed() &&
9325 FD->getODRHash() != NonConstDefn->getODRHash()) {
9326 if (!isa<CXXMethodDecl>(FD)) {
9327 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9328 } else if (FD->getLexicalParent()->isFileContext() &&
9329 NonConstDefn->getLexicalParent()->isFileContext()) {
9330 // Only diagnose out-of-line method definitions. If they are
9331 // in class definitions, then an error will be generated when
9332 // processing the class bodies.
9333 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9334 }
9335 }
9336 }
9337 continue;
9338 }
9339
9340 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9341 if (!getContext().getLangOpts().Modules || !MD->hasBody())
9342 MD->setLazyBody(PB->second);
9343 }
9344 PendingBodies.clear();
9345
9346 // Do some cleanup.
9347 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9348 getContext().deduplicateMergedDefinitonsFor(ND);
9349 PendingMergedDefinitionsToDeduplicate.clear();
9350}
9351
9352void ASTReader::diagnoseOdrViolations() {
9353 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9354 PendingFunctionOdrMergeFailures.empty() &&
9355 PendingEnumOdrMergeFailures.empty())
9356 return;
9357
9358 // Trigger the import of the full definition of each class that had any
9359 // odr-merging problems, so we can produce better diagnostics for them.
9360 // These updates may in turn find and diagnose some ODR failures, so take
9361 // ownership of the set first.
9362 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9363 PendingOdrMergeFailures.clear();
9364 for (auto &Merge : OdrMergeFailures) {
9365 Merge.first->buildLookup();
9366 Merge.first->decls_begin();
9367 Merge.first->bases_begin();
9368 Merge.first->vbases_begin();
9369 for (auto &RecordPair : Merge.second) {
9370 auto *RD = RecordPair.first;
9371 RD->decls_begin();
9372 RD->bases_begin();
9373 RD->vbases_begin();
9374 }
9375 }
9376
9377 // Trigger the import of functions.
9378 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9379 PendingFunctionOdrMergeFailures.clear();
9380 for (auto &Merge : FunctionOdrMergeFailures) {
9381 Merge.first->buildLookup();
9382 Merge.first->decls_begin();
9383 Merge.first->getBody();
9384 for (auto &FD : Merge.second) {
9385 FD->buildLookup();
9386 FD->decls_begin();
9387 FD->getBody();
9388 }
9389 }
9390
9391 // Trigger the import of enums.
9392 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9393 PendingEnumOdrMergeFailures.clear();
9394 for (auto &Merge : EnumOdrMergeFailures) {
9395 Merge.first->decls_begin();
9396 for (auto &Enum : Merge.second) {
9397 Enum->decls_begin();
9398 }
9399 }
9400
9401 // For each declaration from a merged context, check that the canonical
9402 // definition of that context also contains a declaration of the same
9403 // entity.
9404 //
9405 // Caution: this loop does things that might invalidate iterators into
9406 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9407 while (!PendingOdrMergeChecks.empty()) {
9408 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9409
9410 // FIXME: Skip over implicit declarations for now. This matters for things
9411 // like implicitly-declared special member functions. This isn't entirely
9412 // correct; we can end up with multiple unmerged declarations of the same
9413 // implicit entity.
9414 if (D->isImplicit())
9415 continue;
9416
9417 DeclContext *CanonDef = D->getDeclContext();
9418
9419 bool Found = false;
9420 const Decl *DCanon = D->getCanonicalDecl();
9421
9422 for (auto RI : D->redecls()) {
9423 if (RI->getLexicalDeclContext() == CanonDef) {
9424 Found = true;
9425 break;
9426 }
9427 }
9428 if (Found)
9429 continue;
9430
9431 // Quick check failed, time to do the slow thing. Note, we can't just
9432 // look up the name of D in CanonDef here, because the member that is
9433 // in CanonDef might not be found by name lookup (it might have been
9434 // replaced by a more recent declaration in the lookup table), and we
9435 // can't necessarily find it in the redeclaration chain because it might
9436 // be merely mergeable, not redeclarable.
9437 llvm::SmallVector<const NamedDecl*, 4> Candidates;
9438 for (auto *CanonMember : CanonDef->decls()) {
9439 if (CanonMember->getCanonicalDecl() == DCanon) {
9440 // This can happen if the declaration is merely mergeable and not
9441 // actually redeclarable (we looked for redeclarations earlier).
9442 //
9443 // FIXME: We should be able to detect this more efficiently, without
9444 // pulling in all of the members of CanonDef.
9445 Found = true;
9446 break;
9447 }
9448 if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9449 if (ND->getDeclName() == D->getDeclName())
9450 Candidates.push_back(ND);
9451 }
9452
9453 if (!Found) {
9454 // The AST doesn't like TagDecls becoming invalid after they've been
9455 // completed. We only really need to mark FieldDecls as invalid here.
9456 if (!isa<TagDecl>(D))
9457 D->setInvalidDecl();
9458
9459 // Ensure we don't accidentally recursively enter deserialization while
9460 // we're producing our diagnostic.
9461 Deserializing RecursionGuard(this);
9462
9463 std::string CanonDefModule =
9464 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9465 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9466 << D << getOwningModuleNameForDiagnostic(D)
9467 << CanonDef << CanonDefModule.empty() << CanonDefModule;
9468
9469 if (Candidates.empty())
9470 Diag(cast<Decl>(CanonDef)->getLocation(),
9471 diag::note_module_odr_violation_no_possible_decls) << D;
9472 else {
9473 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9474 Diag(Candidates[I]->getLocation(),
9475 diag::note_module_odr_violation_possible_decl)
9476 << Candidates[I];
9477 }
9478
9479 DiagnosedOdrMergeFailures.insert(CanonDef);
9480 }
9481 }
9482
9483 if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9484 EnumOdrMergeFailures.empty())
9485 return;
9486
9487 // Ensure we don't accidentally recursively enter deserialization while
9488 // we're producing our diagnostics.
9489 Deserializing RecursionGuard(this);
9490
9491 // Common code for hashing helpers.
9492 ODRHash Hash;
9493 auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9494 Hash.clear();
9495 Hash.AddQualType(Ty);
9496 return Hash.CalculateHash();
9497 };
9498
9499 auto ComputeODRHash = [&Hash](const Stmt *S) {
9500 assert(S)(static_cast<void> (0));
9501 Hash.clear();
9502 Hash.AddStmt(S);
9503 return Hash.CalculateHash();
9504 };
9505
9506 auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9507 assert(D)(static_cast<void> (0));
9508 Hash.clear();
9509 Hash.AddSubDecl(D);
9510 return Hash.CalculateHash();
9511 };
9512
9513 auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9514 Hash.clear();
9515 Hash.AddTemplateArgument(TA);
9516 return Hash.CalculateHash();
9517 };
9518
9519 auto ComputeTemplateParameterListODRHash =
9520 [&Hash](const TemplateParameterList *TPL) {
9521 assert(TPL)(static_cast<void> (0));
9522 Hash.clear();
9523 Hash.AddTemplateParameterList(TPL);
9524 return Hash.CalculateHash();
9525 };
9526
9527 // Used with err_module_odr_violation_mismatch_decl and
9528 // note_module_odr_violation_mismatch_decl
9529 // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed
9530 enum ODRMismatchDecl {
9531 EndOfClass,
9532 PublicSpecifer,
9533 PrivateSpecifer,
9534 ProtectedSpecifer,
9535 StaticAssert,
9536 Field,
9537 CXXMethod,
9538 TypeAlias,
9539 TypeDef,
9540 Var,
9541 Friend,
9542 FunctionTemplate,
9543 Other
9544 };
9545
9546 // Used with err_module_odr_violation_mismatch_decl_diff and
9547 // note_module_odr_violation_mismatch_decl_diff
9548 enum ODRMismatchDeclDifference {
9549 StaticAssertCondition,
9550 StaticAssertMessage,
9551 StaticAssertOnlyMessage,
9552 FieldName,
9553 FieldTypeName,
9554 FieldSingleBitField,
9555 FieldDifferentWidthBitField,
9556 FieldSingleMutable,
9557 FieldSingleInitializer,
9558 FieldDifferentInitializers,
9559 MethodName,
9560 MethodDeleted,
9561 MethodDefaulted,
9562 MethodVirtual,
9563 MethodStatic,
9564 MethodVolatile,
9565 MethodConst,
9566 MethodInline,
9567 MethodNumberParameters,
9568 MethodParameterType,
9569 MethodParameterName,
9570 MethodParameterSingleDefaultArgument,
9571 MethodParameterDifferentDefaultArgument,
9572 MethodNoTemplateArguments,
9573 MethodDifferentNumberTemplateArguments,
9574 MethodDifferentTemplateArgument,
9575 MethodSingleBody,
9576 MethodDifferentBody,
9577 TypedefName,
9578 TypedefType,
9579 VarName,
9580 VarType,
9581 VarSingleInitializer,
9582 VarDifferentInitializer,
9583 VarConstexpr,
9584 FriendTypeFunction,
9585 FriendType,
9586 FriendFunction,
9587 FunctionTemplateDifferentNumberParameters,
9588 FunctionTemplateParameterDifferentKind,
9589 FunctionTemplateParameterName,
9590 FunctionTemplateParameterSingleDefaultArgument,
9591 FunctionTemplateParameterDifferentDefaultArgument,
9592 FunctionTemplateParameterDifferentType,
9593 FunctionTemplatePackParameter,
9594 };
9595
9596 // These lambdas have the common portions of the ODR diagnostics. This
9597 // has the same return as Diag(), so addition parameters can be passed
9598 // in with operator<<
9599 auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9600 SourceLocation Loc, SourceRange Range,
9601 ODRMismatchDeclDifference DiffType) {
9602 return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
9603 << FirstRecord << FirstModule.empty() << FirstModule << Range
9604 << DiffType;
9605 };
9606 auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc,
9607 SourceRange Range, ODRMismatchDeclDifference DiffType) {
9608 return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
9609 << SecondModule << Range << DiffType;
9610 };
9611
9612 auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote,
9613 &ComputeQualTypeODRHash, &ComputeODRHash](
9614 NamedDecl *FirstRecord, StringRef FirstModule,
9615 StringRef SecondModule, FieldDecl *FirstField,
9616 FieldDecl *SecondField) {
9617 IdentifierInfo *FirstII = FirstField->getIdentifier();
9618 IdentifierInfo *SecondII = SecondField->getIdentifier();
9619 if (FirstII->getName() != SecondII->getName()) {
9620 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9621 FirstField->getSourceRange(), FieldName)
9622 << FirstII;
9623 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9624 SecondField->getSourceRange(), FieldName)
9625 << SecondII;
9626
9627 return true;
9628 }
9629
9630 assert(getContext().hasSameType(FirstField->getType(),(static_cast<void> (0))
9631 SecondField->getType()))(static_cast<void> (0));
9632
9633 QualType FirstType = FirstField->getType();
9634 QualType SecondType = SecondField->getType();
9635 if (ComputeQualTypeODRHash(FirstType) !=
9636 ComputeQualTypeODRHash(SecondType)) {
9637 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9638 FirstField->getSourceRange(), FieldTypeName)
9639 << FirstII << FirstType;
9640 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9641 SecondField->getSourceRange(), FieldTypeName)
9642 << SecondII << SecondType;
9643
9644 return true;
9645 }
9646
9647 const bool IsFirstBitField = FirstField->isBitField();
9648 const bool IsSecondBitField = SecondField->isBitField();
9649 if (IsFirstBitField != IsSecondBitField) {
9650 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9651 FirstField->getSourceRange(), FieldSingleBitField)
9652 << FirstII << IsFirstBitField;
9653 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9654 SecondField->getSourceRange(), FieldSingleBitField)
9655 << SecondII << IsSecondBitField;
9656 return true;
9657 }
9658
9659 if (IsFirstBitField && IsSecondBitField) {
9660 unsigned FirstBitWidthHash =
9661 ComputeODRHash(FirstField->getBitWidth());
9662 unsigned SecondBitWidthHash =
9663 ComputeODRHash(SecondField->getBitWidth());
9664 if (FirstBitWidthHash != SecondBitWidthHash) {
9665 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9666 FirstField->getSourceRange(),
9667 FieldDifferentWidthBitField)
9668 << FirstII << FirstField->getBitWidth()->getSourceRange();
9669 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9670 SecondField->getSourceRange(),
9671 FieldDifferentWidthBitField)
9672 << SecondII << SecondField->getBitWidth()->getSourceRange();
9673 return true;
9674 }
9675 }
9676
9677 if (!PP.getLangOpts().CPlusPlus)
9678 return false;
9679
9680 const bool IsFirstMutable = FirstField->isMutable();
9681 const bool IsSecondMutable = SecondField->isMutable();
9682 if (IsFirstMutable != IsSecondMutable) {
9683 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9684 FirstField->getSourceRange(), FieldSingleMutable)
9685 << FirstII << IsFirstMutable;
9686 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9687 SecondField->getSourceRange(), FieldSingleMutable)
9688 << SecondII << IsSecondMutable;
9689 return true;
9690 }
9691
9692 const Expr *FirstInitializer = FirstField->getInClassInitializer();
9693 const Expr *SecondInitializer = SecondField->getInClassInitializer();
9694 if ((!FirstInitializer && SecondInitializer) ||
9695 (FirstInitializer && !SecondInitializer)) {
9696 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9697 FirstField->getSourceRange(), FieldSingleInitializer)
9698 << FirstII << (FirstInitializer != nullptr);
9699 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9700 SecondField->getSourceRange(), FieldSingleInitializer)
9701 << SecondII << (SecondInitializer != nullptr);
9702 return true;
9703 }
9704
9705 if (FirstInitializer && SecondInitializer) {
9706 unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
9707 unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
9708 if (FirstInitHash != SecondInitHash) {
9709 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9710 FirstField->getSourceRange(),
9711 FieldDifferentInitializers)
9712 << FirstII << FirstInitializer->getSourceRange();
9713 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9714 SecondField->getSourceRange(),
9715 FieldDifferentInitializers)
9716 << SecondII << SecondInitializer->getSourceRange();
9717 return true;
9718 }
9719 }
9720
9721 return false;
9722 };
9723
9724 auto ODRDiagTypeDefOrAlias =
9725 [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash](
9726 NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
9727 TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD,
9728 bool IsTypeAlias) {
9729 auto FirstName = FirstTD->getDeclName();
9730 auto SecondName = SecondTD->getDeclName();
9731 if (FirstName != SecondName) {
9732 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9733 FirstTD->getSourceRange(), TypedefName)
9734 << IsTypeAlias << FirstName;
9735 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9736 SecondTD->getSourceRange(), TypedefName)
9737 << IsTypeAlias << SecondName;
9738 return true;
9739 }
9740
9741 QualType FirstType = FirstTD->getUnderlyingType();
9742 QualType SecondType = SecondTD->getUnderlyingType();
9743 if (ComputeQualTypeODRHash(FirstType) !=
9744 ComputeQualTypeODRHash(SecondType)) {
9745 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9746 FirstTD->getSourceRange(), TypedefType)
9747 << IsTypeAlias << FirstName << FirstType;
9748 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9749 SecondTD->getSourceRange(), TypedefType)
9750 << IsTypeAlias << SecondName << SecondType;
9751 return true;
9752 }
9753
9754 return false;
9755 };
9756
9757 auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote,
9758 &ComputeQualTypeODRHash, &ComputeODRHash,
9759 this](NamedDecl *FirstRecord, StringRef FirstModule,
9760 StringRef SecondModule, VarDecl *FirstVD,
9761 VarDecl *SecondVD) {
9762 auto FirstName = FirstVD->getDeclName();
9763 auto SecondName = SecondVD->getDeclName();
9764 if (FirstName != SecondName) {
9765 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9766 FirstVD->getSourceRange(), VarName)
9767 << FirstName;
9768 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9769 SecondVD->getSourceRange(), VarName)
9770 << SecondName;
9771 return true;
9772 }
9773
9774 QualType FirstType = FirstVD->getType();
9775 QualType SecondType = SecondVD->getType();
9776 if (ComputeQualTypeODRHash(FirstType) !=
9777 ComputeQualTypeODRHash(SecondType)) {
9778 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9779 FirstVD->getSourceRange(), VarType)
9780 << FirstName << FirstType;
9781 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9782 SecondVD->getSourceRange(), VarType)
9783 << SecondName << SecondType;
9784 return true;
9785 }
9786
9787 if (!PP.getLangOpts().CPlusPlus)
9788 return false;
9789
9790 const Expr *FirstInit = FirstVD->getInit();
9791 const Expr *SecondInit = SecondVD->getInit();
9792 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
9793 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9794 FirstVD->getSourceRange(), VarSingleInitializer)
9795 << FirstName << (FirstInit == nullptr)
9796 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
9797 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9798 SecondVD->getSourceRange(), VarSingleInitializer)
9799 << SecondName << (SecondInit == nullptr)
9800 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
9801 return true;
9802 }
9803
9804 if (FirstInit && SecondInit &&
9805 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
9806 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9807 FirstVD->getSourceRange(), VarDifferentInitializer)
9808 << FirstName << FirstInit->getSourceRange();
9809 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9810 SecondVD->getSourceRange(), VarDifferentInitializer)
9811 << SecondName << SecondInit->getSourceRange();
9812 return true;
9813 }
9814
9815 const bool FirstIsConstexpr = FirstVD->isConstexpr();
9816 const bool SecondIsConstexpr = SecondVD->isConstexpr();
9817 if (FirstIsConstexpr != SecondIsConstexpr) {
9818 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9819 FirstVD->getSourceRange(), VarConstexpr)
9820 << FirstName << FirstIsConstexpr;
9821 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9822 SecondVD->getSourceRange(), VarConstexpr)
9823 << SecondName << SecondIsConstexpr;
9824 return true;
9825 }
9826 return false;
9827 };
9828
9829 auto DifferenceSelector = [](Decl *D) {
9830 assert(D && "valid Decl required")(static_cast<void> (0));
9831 switch (D->getKind()) {
9832 default:
9833 return Other;
9834 case Decl::AccessSpec:
9835 switch (D->getAccess()) {
9836 case AS_public:
9837 return PublicSpecifer;
9838 case AS_private:
9839 return PrivateSpecifer;
9840 case AS_protected:
9841 return ProtectedSpecifer;
9842 case AS_none:
9843 break;
9844 }
9845 llvm_unreachable("Invalid access specifier")__builtin_unreachable();
9846 case Decl::StaticAssert:
9847 return StaticAssert;
9848 case Decl::Field:
9849 return Field;
9850 case Decl::CXXMethod:
9851 case Decl::CXXConstructor:
9852 case Decl::CXXDestructor:
9853 return CXXMethod;
9854 case Decl::TypeAlias:
9855 return TypeAlias;
9856 case Decl::Typedef:
9857 return TypeDef;
9858 case Decl::Var:
9859 return Var;
9860 case Decl::Friend:
9861 return Friend;
9862 case Decl::FunctionTemplate:
9863 return FunctionTemplate;
9864 }
9865 };
9866
9867 using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9868 auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9869 RecordDecl *Record,
9870 const DeclContext *DC) {
9871 for (auto *D : Record->decls()) {
9872 if (!ODRHash::isDeclToBeProcessed(D, DC))
9873 continue;
9874 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9875 }
9876 };
9877
9878 struct DiffResult {
9879 Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
9880 ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
9881 };
9882
9883 // If there is a diagnoseable difference, FirstDiffType and
9884 // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
9885 // filled in if not EndOfClass.
9886 auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes,
9887 DeclHashes &SecondHashes) {
9888 DiffResult DR;
9889 auto FirstIt = FirstHashes.begin();
9890 auto SecondIt = SecondHashes.begin();
9891 while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
9892 if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
9893 FirstIt->second == SecondIt->second) {
9894 ++FirstIt;
9895 ++SecondIt;
9896 continue;
9897 }
9898
9899 DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
9900 DR.SecondDecl =
9901 SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
9902
9903 DR.FirstDiffType =
9904 DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
9905 DR.SecondDiffType =
9906 DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
9907 return DR;
9908 }
9909 return DR;
9910 };
9911
9912 // Use this to diagnose that an unexpected Decl was encountered
9913 // or no difference was detected. This causes a generic error
9914 // message to be emitted.
9915 auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord,
9916 StringRef FirstModule,
9917 NamedDecl *SecondRecord,
9918 StringRef SecondModule) {
9919 Diag(FirstRecord->getLocation(),
9920 diag::err_module_odr_violation_different_definitions)
9921 << FirstRecord << FirstModule.empty() << FirstModule;
9922
9923 if (DR.FirstDecl) {
9924 Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
9925 << FirstRecord << DR.FirstDecl->getSourceRange();
9926 }
9927
9928 Diag(SecondRecord->getLocation(),
9929 diag::note_module_odr_violation_different_definitions)
9930 << SecondModule;
9931
9932 if (DR.SecondDecl) {
9933 Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
9934 << DR.SecondDecl->getSourceRange();
9935 }
9936 };
9937
9938 auto DiagnoseODRMismatch =
9939 [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule,
9940 NamedDecl *SecondRecord, StringRef SecondModule) {
9941 SourceLocation FirstLoc;
9942 SourceRange FirstRange;
9943 auto *FirstTag = dyn_cast<TagDecl>(FirstRecord);
9944 if (DR.FirstDiffType == EndOfClass && FirstTag) {
9945 FirstLoc = FirstTag->getBraceRange().getEnd();
9946 } else {
9947 FirstLoc = DR.FirstDecl->getLocation();
9948 FirstRange = DR.FirstDecl->getSourceRange();
9949 }
9950 Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
9951 << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
9952 << DR.FirstDiffType;
9953
9954 SourceLocation SecondLoc;
9955 SourceRange SecondRange;
9956 auto *SecondTag = dyn_cast<TagDecl>(SecondRecord);
9957 if (DR.SecondDiffType == EndOfClass && SecondTag) {
9958 SecondLoc = SecondTag->getBraceRange().getEnd();
9959 } else {
9960 SecondLoc = DR.SecondDecl->getLocation();
9961 SecondRange = DR.SecondDecl->getSourceRange();
9962 }
9963 Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
9964 << SecondModule << SecondRange << DR.SecondDiffType;
9965 };
9966
9967 // Issue any pending ODR-failure diagnostics.
9968 for (auto &Merge : OdrMergeFailures) {
9969 // If we've already pointed out a specific problem with this class, don't
9970 // bother issuing a general "something's different" diagnostic.
9971 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
9972 continue;
9973
9974 bool Diagnosed = false;
9975 CXXRecordDecl *FirstRecord = Merge.first;
9976 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
9977 for (auto &RecordPair : Merge.second) {
9978 CXXRecordDecl *SecondRecord = RecordPair.first;
9979 // Multiple different declarations got merged together; tell the user
9980 // where they came from.
9981 if (FirstRecord == SecondRecord)
9982 continue;
9983
9984 std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
9985
9986 auto *FirstDD = FirstRecord->DefinitionData;
9987 auto *SecondDD = RecordPair.second;
9988
9989 assert(FirstDD && SecondDD && "Definitions without DefinitionData")(static_cast<void> (0));
9990
9991 // Diagnostics from DefinitionData are emitted here.
9992 if (FirstDD != SecondDD) {
9993 enum ODRDefinitionDataDifference {
9994 NumBases,
9995 NumVBases,
9996 BaseType,
9997 BaseVirtual,
9998 BaseAccess,
9999 };
10000 auto ODRDiagBaseError = [FirstRecord, &FirstModule,
10001 this](SourceLocation Loc, SourceRange Range,
10002 ODRDefinitionDataDifference DiffType) {
10003 return Diag(Loc, diag::err_module_odr_violation_definition_data)
10004 << FirstRecord << FirstModule.empty() << FirstModule << Range
10005 << DiffType;
10006 };
10007 auto ODRDiagBaseNote = [&SecondModule,
10008 this](SourceLocation Loc, SourceRange Range,
10009 ODRDefinitionDataDifference DiffType) {
10010 return Diag(Loc, diag::note_module_odr_violation_definition_data)
10011 << SecondModule << Range << DiffType;
10012 };
10013
10014 unsigned FirstNumBases = FirstDD->NumBases;
10015 unsigned FirstNumVBases = FirstDD->NumVBases;
10016 unsigned SecondNumBases = SecondDD->NumBases;
10017 unsigned SecondNumVBases = SecondDD->NumVBases;
10018
10019 auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10020 unsigned NumBases = DD->NumBases;
10021 if (NumBases == 0) return SourceRange();
10022 auto bases = DD->bases();
10023 return SourceRange(bases[0].getBeginLoc(),
10024 bases[NumBases - 1].getEndLoc());
10025 };
10026
10027 if (FirstNumBases != SecondNumBases) {
10028 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10029 NumBases)
10030 << FirstNumBases;
10031 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10032 NumBases)
10033 << SecondNumBases;
10034 Diagnosed = true;
10035 break;
10036 }
10037
10038 if (FirstNumVBases != SecondNumVBases) {
10039 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10040 NumVBases)
10041 << FirstNumVBases;
10042 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10043 NumVBases)
10044 << SecondNumVBases;
10045 Diagnosed = true;
10046 break;
10047 }
10048
10049 auto FirstBases = FirstDD->bases();
10050 auto SecondBases = SecondDD->bases();
10051 unsigned i = 0;
10052 for (i = 0; i < FirstNumBases; ++i) {
10053 auto FirstBase = FirstBases[i];
10054 auto SecondBase = SecondBases[i];
10055 if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10056 ComputeQualTypeODRHash(SecondBase.getType())) {
10057 ODRDiagBaseError(FirstRecord->getLocation(),
10058 FirstBase.getSourceRange(), BaseType)
10059 << (i + 1) << FirstBase.getType();
10060 ODRDiagBaseNote(SecondRecord->getLocation(),
10061 SecondBase.getSourceRange(), BaseType)
10062 << (i + 1) << SecondBase.getType();
10063 break;
10064 }
10065
10066 if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10067 ODRDiagBaseError(FirstRecord->getLocation(),
10068 FirstBase.getSourceRange(), BaseVirtual)
10069 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
10070 ODRDiagBaseNote(SecondRecord->getLocation(),
10071 SecondBase.getSourceRange(), BaseVirtual)
10072 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10073 break;
10074 }
10075
10076 if (FirstBase.getAccessSpecifierAsWritten() !=
10077 SecondBase.getAccessSpecifierAsWritten()) {
10078 ODRDiagBaseError(FirstRecord->getLocation(),
10079 FirstBase.getSourceRange(), BaseAccess)
10080 << (i + 1) << FirstBase.getType()
10081 << (int)FirstBase.getAccessSpecifierAsWritten();
10082 ODRDiagBaseNote(SecondRecord->getLocation(),
10083 SecondBase.getSourceRange(), BaseAccess)
10084 << (i + 1) << SecondBase.getType()
10085 << (int)SecondBase.getAccessSpecifierAsWritten();
10086 break;
10087 }
10088 }
10089
10090 if (i != FirstNumBases) {
10091 Diagnosed = true;
10092 break;
10093 }
10094 }
10095
10096 const ClassTemplateDecl *FirstTemplate =
10097 FirstRecord->getDescribedClassTemplate();
10098 const ClassTemplateDecl *SecondTemplate =
10099 SecondRecord->getDescribedClassTemplate();
10100
10101 assert(!FirstTemplate == !SecondTemplate &&(static_cast<void> (0))
10102 "Both pointers should be null or non-null")(static_cast<void> (0));
10103
10104 enum ODRTemplateDifference {
10105 ParamEmptyName,
10106 ParamName,
10107 ParamSingleDefaultArgument,
10108 ParamDifferentDefaultArgument,
10109 };
10110
10111 if (FirstTemplate && SecondTemplate) {
10112 DeclHashes FirstTemplateHashes;
10113 DeclHashes SecondTemplateHashes;
10114
10115 auto PopulateTemplateParameterHashs =
10116 [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10117 const ClassTemplateDecl *TD) {
10118 for (auto *D : TD->getTemplateParameters()->asArray()) {
10119 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10120 }
10121 };
10122
10123 PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10124 PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10125
10126 assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&(static_cast<void> (0))
10127 "Number of template parameters should be equal.")(static_cast<void> (0));
10128
10129 auto FirstIt = FirstTemplateHashes.begin();
10130 auto FirstEnd = FirstTemplateHashes.end();
10131 auto SecondIt = SecondTemplateHashes.begin();
10132 for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10133 if (FirstIt->second == SecondIt->second)
10134 continue;
10135
10136 auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this](
10137 SourceLocation Loc, SourceRange Range,
10138 ODRTemplateDifference DiffType) {
10139 return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10140 << FirstRecord << FirstModule.empty() << FirstModule << Range
10141 << DiffType;
10142 };
10143 auto ODRDiagTemplateNote = [&SecondModule, this](
10144 SourceLocation Loc, SourceRange Range,
10145 ODRTemplateDifference DiffType) {
10146 return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10147 << SecondModule << Range << DiffType;
10148 };
10149
10150 const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10151 const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10152
10153 assert(FirstDecl->getKind() == SecondDecl->getKind() &&(static_cast<void> (0))
10154 "Parameter Decl's should be the same kind.")(static_cast<void> (0));
10155
10156 DeclarationName FirstName = FirstDecl->getDeclName();
10157 DeclarationName SecondName = SecondDecl->getDeclName();
10158
10159 if (FirstName != SecondName) {
10160 const bool FirstNameEmpty =
10161 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10162 const bool SecondNameEmpty =
10163 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10164 assert((!FirstNameEmpty || !SecondNameEmpty) &&(static_cast<void> (0))
10165 "Both template parameters cannot be unnamed.")(static_cast<void> (0));
10166 ODRDiagTemplateError(FirstDecl->getLocation(),
10167 FirstDecl->getSourceRange(),
10168 FirstNameEmpty ? ParamEmptyName : ParamName)
10169 << FirstName;
10170 ODRDiagTemplateNote(SecondDecl->getLocation(),
10171 SecondDecl->getSourceRange(),
10172 SecondNameEmpty ? ParamEmptyName : ParamName)
10173 << SecondName;
10174 break;
10175 }
10176
10177 switch (FirstDecl->getKind()) {
10178 default:
10179 llvm_unreachable("Invalid template parameter type.")__builtin_unreachable();
10180 case Decl::TemplateTypeParm: {
10181 const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10182 const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10183 const bool HasFirstDefaultArgument =
10184 FirstParam->hasDefaultArgument() &&
10185 !FirstParam->defaultArgumentWasInherited();
10186 const bool HasSecondDefaultArgument =
10187 SecondParam->hasDefaultArgument() &&
10188 !SecondParam->defaultArgumentWasInherited();
10189
10190 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10191 ODRDiagTemplateError(FirstDecl->getLocation(),
10192 FirstDecl->getSourceRange(),
10193 ParamSingleDefaultArgument)
10194 << HasFirstDefaultArgument;
10195 ODRDiagTemplateNote(SecondDecl->getLocation(),
10196 SecondDecl->getSourceRange(),
10197 ParamSingleDefaultArgument)
10198 << HasSecondDefaultArgument;
10199 break;
10200 }
10201
10202 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&(static_cast<void> (0))
10203 "Expecting default arguments.")(static_cast<void> (0));
10204
10205 ODRDiagTemplateError(FirstDecl->getLocation(),
10206 FirstDecl->getSourceRange(),
10207 ParamDifferentDefaultArgument);
10208 ODRDiagTemplateNote(SecondDecl->getLocation(),
10209 SecondDecl->getSourceRange(),
10210 ParamDifferentDefaultArgument);
10211
10212 break;
10213 }
10214 case Decl::NonTypeTemplateParm: {
10215 const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10216 const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10217 const bool HasFirstDefaultArgument =
10218 FirstParam->hasDefaultArgument() &&
10219 !FirstParam->defaultArgumentWasInherited();
10220 const bool HasSecondDefaultArgument =
10221 SecondParam->hasDefaultArgument() &&
10222 !SecondParam->defaultArgumentWasInherited();
10223
10224 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10225 ODRDiagTemplateError(FirstDecl->getLocation(),
10226 FirstDecl->getSourceRange(),
10227 ParamSingleDefaultArgument)
10228 << HasFirstDefaultArgument;
10229 ODRDiagTemplateNote(SecondDecl->getLocation(),
10230 SecondDecl->getSourceRange(),
10231 ParamSingleDefaultArgument)
10232 << HasSecondDefaultArgument;
10233 break;
10234 }
10235
10236 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&(static_cast<void> (0))
10237 "Expecting default arguments.")(static_cast<void> (0));
10238
10239 ODRDiagTemplateError(FirstDecl->getLocation(),
10240 FirstDecl->getSourceRange(),
10241 ParamDifferentDefaultArgument);
10242 ODRDiagTemplateNote(SecondDecl->getLocation(),
10243 SecondDecl->getSourceRange(),
10244 ParamDifferentDefaultArgument);
10245
10246 break;
10247 }
10248 case Decl::TemplateTemplateParm: {
10249 const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10250 const auto *SecondParam =
10251 cast<TemplateTemplateParmDecl>(SecondDecl);
10252 const bool HasFirstDefaultArgument =
10253 FirstParam->hasDefaultArgument() &&
10254 !FirstParam->defaultArgumentWasInherited();
10255 const bool HasSecondDefaultArgument =
10256 SecondParam->hasDefaultArgument() &&
10257 !SecondParam->defaultArgumentWasInherited();
10258
10259 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10260 ODRDiagTemplateError(FirstDecl->getLocation(),
10261 FirstDecl->getSourceRange(),
10262 ParamSingleDefaultArgument)
10263 << HasFirstDefaultArgument;
10264 ODRDiagTemplateNote(SecondDecl->getLocation(),
10265 SecondDecl->getSourceRange(),
10266 ParamSingleDefaultArgument)
10267 << HasSecondDefaultArgument;
10268 break;
10269 }
10270
10271 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&(static_cast<void> (0))
10272 "Expecting default arguments.")(static_cast<void> (0));
10273
10274 ODRDiagTemplateError(FirstDecl->getLocation(),
10275 FirstDecl->getSourceRange(),
10276 ParamDifferentDefaultArgument);
10277 ODRDiagTemplateNote(SecondDecl->getLocation(),
10278 SecondDecl->getSourceRange(),
10279 ParamDifferentDefaultArgument);
10280
10281 break;
10282 }
10283 }
10284
10285 break;
10286 }
10287
10288 if (FirstIt != FirstEnd) {
10289 Diagnosed = true;
10290 break;
10291 }
10292 }
10293
10294 DeclHashes FirstHashes;
10295 DeclHashes SecondHashes;
10296 const DeclContext *DC = FirstRecord;
10297 PopulateHashes(FirstHashes, FirstRecord, DC);
10298 PopulateHashes(SecondHashes, SecondRecord, DC);
10299
10300 auto DR = FindTypeDiffs(FirstHashes, SecondHashes);
10301 ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
10302 ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
10303 Decl *FirstDecl = DR.FirstDecl;
10304 Decl *SecondDecl = DR.SecondDecl;
10305
10306 if (FirstDiffType == Other || SecondDiffType == Other) {
10307 DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
10308 SecondModule);
10309 Diagnosed = true;
10310 break;
10311 }
10312
10313 if (FirstDiffType != SecondDiffType) {
10314 DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord,
10315 SecondModule);
10316 Diagnosed = true;
10317 break;
10318 }
10319
10320 assert(FirstDiffType == SecondDiffType)(static_cast<void> (0));
10321
10322 switch (FirstDiffType) {
10323 case Other:
10324 case EndOfClass:
10325 case PublicSpecifer:
10326 case PrivateSpecifer:
10327 case ProtectedSpecifer:
10328 llvm_unreachable("Invalid diff type")__builtin_unreachable();
10329
10330 case StaticAssert: {
10331 StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10332 StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10333
10334 Expr *FirstExpr = FirstSA->getAssertExpr();
10335 Expr *SecondExpr = SecondSA->getAssertExpr();
10336 unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10337 unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10338 if (FirstODRHash != SecondODRHash) {
10339 ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(),
10340 FirstExpr->getSourceRange(), StaticAssertCondition);
10341 ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(),
10342 SecondExpr->getSourceRange(), StaticAssertCondition);
10343 Diagnosed = true;
10344 break;
10345 }
10346
10347 StringLiteral *FirstStr = FirstSA->getMessage();
10348 StringLiteral *SecondStr = SecondSA->getMessage();
10349 assert((FirstStr || SecondStr) && "Both messages cannot be empty")(static_cast<void> (0));
10350 if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10351 SourceLocation FirstLoc, SecondLoc;
10352 SourceRange FirstRange, SecondRange;
10353 if (FirstStr) {
10354 FirstLoc = FirstStr->getBeginLoc();
10355 FirstRange = FirstStr->getSourceRange();
10356 } else {
10357 FirstLoc = FirstSA->getBeginLoc();
10358 FirstRange = FirstSA->getSourceRange();
10359 }
10360 if (SecondStr) {
10361 SecondLoc = SecondStr->getBeginLoc();
10362 SecondRange = SecondStr->getSourceRange();
10363 } else {
10364 SecondLoc = SecondSA->getBeginLoc();
10365 SecondRange = SecondSA->getSourceRange();
10366 }
10367 ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange,
10368 StaticAssertOnlyMessage)
10369 << (FirstStr == nullptr);
10370 ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange,
10371 StaticAssertOnlyMessage)
10372 << (SecondStr == nullptr);
10373 Diagnosed = true;
10374 break;
10375 }
10376
10377 if (FirstStr && SecondStr &&
10378 FirstStr->getString() != SecondStr->getString()) {
10379 ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(),
10380 FirstStr->getSourceRange(), StaticAssertMessage);
10381 ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(),
10382 SecondStr->getSourceRange(), StaticAssertMessage);
10383 Diagnosed = true;
10384 break;
10385 }
10386 break;
10387 }
10388 case Field: {
10389 Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule,
10390 cast<FieldDecl>(FirstDecl),
10391 cast<FieldDecl>(SecondDecl));
10392 break;
10393 }
10394 case CXXMethod: {
10395 enum {
10396 DiagMethod,
10397 DiagConstructor,
10398 DiagDestructor,
10399 } FirstMethodType,
10400 SecondMethodType;
10401 auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10402 if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10403 if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10404 return DiagMethod;
10405 };
10406 const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10407 const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10408 FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10409 SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10410 auto FirstName = FirstMethod->getDeclName();
10411 auto SecondName = SecondMethod->getDeclName();
10412 if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10413 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10414 FirstMethod->getSourceRange(), MethodName)
10415 << FirstMethodType << FirstName;
10416 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10417 SecondMethod->getSourceRange(), MethodName)
10418 << SecondMethodType << SecondName;
10419
10420 Diagnosed = true;
10421 break;
10422 }
10423
10424 const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10425 const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10426 if (FirstDeleted != SecondDeleted) {
10427 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10428 FirstMethod->getSourceRange(), MethodDeleted)
10429 << FirstMethodType << FirstName << FirstDeleted;
10430
10431 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10432 SecondMethod->getSourceRange(), MethodDeleted)
10433 << SecondMethodType << SecondName << SecondDeleted;
10434 Diagnosed = true;
10435 break;
10436 }
10437
10438 const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10439 const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10440 if (FirstDefaulted != SecondDefaulted) {
10441 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10442 FirstMethod->getSourceRange(), MethodDefaulted)
10443 << FirstMethodType << FirstName << FirstDefaulted;
10444
10445 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10446 SecondMethod->getSourceRange(), MethodDefaulted)
10447 << SecondMethodType << SecondName << SecondDefaulted;
10448 Diagnosed = true;
10449 break;
10450 }
10451
10452 const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10453 const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10454 const bool FirstPure = FirstMethod->isPure();
10455 const bool SecondPure = SecondMethod->isPure();
10456 if ((FirstVirtual || SecondVirtual) &&
10457 (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10458 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10459 FirstMethod->getSourceRange(), MethodVirtual)
10460 << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10461 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10462 SecondMethod->getSourceRange(), MethodVirtual)
10463 << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10464 Diagnosed = true;
10465 break;
10466 }
10467
10468 // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging,
10469 // FirstDecl is the canonical Decl of SecondDecl, so the storage
10470 // class needs to be checked instead.
10471 const auto FirstStorage = FirstMethod->getStorageClass();
10472 const auto SecondStorage = SecondMethod->getStorageClass();
10473 const bool FirstStatic = FirstStorage == SC_Static;
10474 const bool SecondStatic = SecondStorage == SC_Static;
10475 if (FirstStatic != SecondStatic) {
10476 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10477 FirstMethod->getSourceRange(), MethodStatic)
10478 << FirstMethodType << FirstName << FirstStatic;
10479 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10480 SecondMethod->getSourceRange(), MethodStatic)
10481 << SecondMethodType << SecondName << SecondStatic;
10482 Diagnosed = true;
10483 break;
10484 }
10485
10486 const bool FirstVolatile = FirstMethod->isVolatile();
10487 const bool SecondVolatile = SecondMethod->isVolatile();
10488 if (FirstVolatile != SecondVolatile) {
10489 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10490 FirstMethod->getSourceRange(), MethodVolatile)
10491 << FirstMethodType << FirstName << FirstVolatile;
10492 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10493 SecondMethod->getSourceRange(), MethodVolatile)
10494 << SecondMethodType << SecondName << SecondVolatile;
10495 Diagnosed = true;
10496 break;
10497 }
10498
10499 const bool FirstConst = FirstMethod->isConst();
10500 const bool SecondConst = SecondMethod->isConst();
10501 if (FirstConst != SecondConst) {
10502 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10503 FirstMethod->getSourceRange(), MethodConst)
10504 << FirstMethodType << FirstName << FirstConst;
10505 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10506 SecondMethod->getSourceRange(), MethodConst)
10507 << SecondMethodType << SecondName << SecondConst;
10508 Diagnosed = true;
10509 break;
10510 }
10511
10512 const bool FirstInline = FirstMethod->isInlineSpecified();
10513 const bool SecondInline = SecondMethod->isInlineSpecified();
10514 if (FirstInline != SecondInline) {
10515 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10516 FirstMethod->getSourceRange(), MethodInline)
10517 << FirstMethodType << FirstName << FirstInline;
10518 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10519 SecondMethod->getSourceRange(), MethodInline)
10520 << SecondMethodType << SecondName << SecondInline;
10521 Diagnosed = true;
10522 break;
10523 }
10524
10525 const unsigned FirstNumParameters = FirstMethod->param_size();
10526 const unsigned SecondNumParameters = SecondMethod->param_size();
10527 if (FirstNumParameters != SecondNumParameters) {
10528 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10529 FirstMethod->getSourceRange(),
10530 MethodNumberParameters)
10531 << FirstMethodType << FirstName << FirstNumParameters;
10532 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10533 SecondMethod->getSourceRange(),
10534 MethodNumberParameters)
10535 << SecondMethodType << SecondName << SecondNumParameters;
10536 Diagnosed = true;
10537 break;
10538 }
10539
10540 // Need this status boolean to know when break out of the switch.
10541 bool ParameterMismatch = false;
10542 for (unsigned I = 0; I < FirstNumParameters; ++I) {
10543 const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10544 const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10545
10546 QualType FirstParamType = FirstParam->getType();
10547 QualType SecondParamType = SecondParam->getType();
10548 if (FirstParamType != SecondParamType &&
10549 ComputeQualTypeODRHash(FirstParamType) !=
10550 ComputeQualTypeODRHash(SecondParamType)) {
10551 if (const DecayedType *ParamDecayedType =
10552 FirstParamType->getAs<DecayedType>()) {
10553 ODRDiagDeclError(
10554 FirstRecord, FirstModule, FirstMethod->getLocation(),
10555 FirstMethod->getSourceRange(), MethodParameterType)
10556 << FirstMethodType << FirstName << (I + 1) << FirstParamType
10557 << true << ParamDecayedType->getOriginalType();
10558 } else {
10559 ODRDiagDeclError(
10560 FirstRecord, FirstModule, FirstMethod->getLocation(),
10561 FirstMethod->getSourceRange(), MethodParameterType)
10562 << FirstMethodType << FirstName << (I + 1) << FirstParamType
10563 << false;
10564 }
10565
10566 if (const DecayedType *ParamDecayedType =
10567 SecondParamType->getAs<DecayedType>()) {
10568 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10569 SecondMethod->getSourceRange(),
10570 MethodParameterType)
10571 << SecondMethodType << SecondName << (I + 1)
10572 << SecondParamType << true
10573 << ParamDecayedType->getOriginalType();
10574 } else {
10575 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10576 SecondMethod->getSourceRange(),
10577 MethodParameterType)
10578 << SecondMethodType << SecondName << (I + 1)
10579 << SecondParamType << false;
10580 }
10581 ParameterMismatch = true;
10582 break;
10583 }
10584
10585 DeclarationName FirstParamName = FirstParam->getDeclName();
10586 DeclarationName SecondParamName = SecondParam->getDeclName();
10587 if (FirstParamName != SecondParamName) {
10588 ODRDiagDeclError(FirstRecord, FirstModule,
10589 FirstMethod->getLocation(),
10590 FirstMethod->getSourceRange(), MethodParameterName)
10591 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
10592 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10593 SecondMethod->getSourceRange(), MethodParameterName)
10594 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
10595 ParameterMismatch = true;
10596 break;
10597 }
10598
10599 const Expr *FirstInit = FirstParam->getInit();
10600 const Expr *SecondInit = SecondParam->getInit();
10601 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10602 ODRDiagDeclError(FirstRecord, FirstModule,
10603 FirstMethod->getLocation(),
10604 FirstMethod->getSourceRange(),
10605 MethodParameterSingleDefaultArgument)
10606 << FirstMethodType << FirstName << (I + 1)
10607 << (FirstInit == nullptr)
10608 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10609 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10610 SecondMethod->getSourceRange(),
10611 MethodParameterSingleDefaultArgument)
10612 << SecondMethodType << SecondName << (I + 1)
10613 << (SecondInit == nullptr)
10614 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10615 ParameterMismatch = true;
10616 break;
10617 }
10618
10619 if (FirstInit && SecondInit &&
10620 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10621 ODRDiagDeclError(FirstRecord, FirstModule,
10622 FirstMethod->getLocation(),
10623 FirstMethod->getSourceRange(),
10624 MethodParameterDifferentDefaultArgument)
10625 << FirstMethodType << FirstName << (I + 1)
10626 << FirstInit->getSourceRange();
10627 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10628 SecondMethod->getSourceRange(),
10629 MethodParameterDifferentDefaultArgument)
10630 << SecondMethodType << SecondName << (I + 1)
10631 << SecondInit->getSourceRange();
10632 ParameterMismatch = true;
10633 break;
10634
10635 }
10636 }
10637
10638 if (ParameterMismatch) {
10639 Diagnosed = true;
10640 break;
10641 }
10642
10643 const auto *FirstTemplateArgs =
10644 FirstMethod->getTemplateSpecializationArgs();
10645 const auto *SecondTemplateArgs =
10646 SecondMethod->getTemplateSpecializationArgs();
10647
10648 if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10649 (!FirstTemplateArgs && SecondTemplateArgs)) {
10650 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10651 FirstMethod->getSourceRange(),
10652 MethodNoTemplateArguments)
10653 << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10654 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10655 SecondMethod->getSourceRange(),
10656 MethodNoTemplateArguments)
10657 << SecondMethodType << SecondName
10658 << (SecondTemplateArgs != nullptr);
10659
10660 Diagnosed = true;
10661 break;
10662 }
10663
10664 if (FirstTemplateArgs && SecondTemplateArgs) {
10665 // Remove pack expansions from argument list.
10666 auto ExpandTemplateArgumentList =
10667 [](const TemplateArgumentList *TAL) {
10668 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10669 for (const TemplateArgument &TA : TAL->asArray()) {
10670 if (TA.getKind() != TemplateArgument::Pack) {
10671 ExpandedList.push_back(&TA);
10672 continue;
10673 }
10674 for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10675 ExpandedList.push_back(&PackTA);
10676 }
10677 }
10678 return ExpandedList;
10679 };
10680 llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10681 ExpandTemplateArgumentList(FirstTemplateArgs);
10682 llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10683 ExpandTemplateArgumentList(SecondTemplateArgs);
10684
10685 if (FirstExpandedList.size() != SecondExpandedList.size()) {
10686 ODRDiagDeclError(FirstRecord, FirstModule,
10687 FirstMethod->getLocation(),
10688 FirstMethod->getSourceRange(),
10689 MethodDifferentNumberTemplateArguments)
10690 << FirstMethodType << FirstName
10691 << (unsigned)FirstExpandedList.size();
10692 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10693 SecondMethod->getSourceRange(),
10694 MethodDifferentNumberTemplateArguments)
10695 << SecondMethodType << SecondName
10696 << (unsigned)SecondExpandedList.size();
10697
10698 Diagnosed = true;
10699 break;
10700 }
10701
10702 bool TemplateArgumentMismatch = false;
10703 for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10704 const TemplateArgument &FirstTA = *FirstExpandedList[i],
10705 &SecondTA = *SecondExpandedList[i];
10706 if (ComputeTemplateArgumentODRHash(FirstTA) ==
10707 ComputeTemplateArgumentODRHash(SecondTA)) {
10708 continue;
10709 }
10710
10711 ODRDiagDeclError(
10712 FirstRecord, FirstModule, FirstMethod->getLocation(),
10713 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument)
10714 << FirstMethodType << FirstName << FirstTA << i + 1;
10715 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10716 SecondMethod->getSourceRange(),
10717 MethodDifferentTemplateArgument)
10718 << SecondMethodType << SecondName << SecondTA << i + 1;
10719
10720 TemplateArgumentMismatch = true;
10721 break;
10722 }
10723
10724 if (TemplateArgumentMismatch) {
10725 Diagnosed = true;
10726 break;
10727 }
10728 }
10729
10730 // Compute the hash of the method as if it has no body.
10731 auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10732 Hash.clear();
10733 Hash.AddFunctionDecl(D, true /*SkipBody*/);
10734 return Hash.CalculateHash();
10735 };
10736
10737 // Compare the hash generated to the hash stored. A difference means
10738 // that a body was present in the original source. Due to merging,
10739 // the stardard way of detecting a body will not work.
10740 const bool HasFirstBody =
10741 ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10742 const bool HasSecondBody =
10743 ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10744
10745 if (HasFirstBody != HasSecondBody) {
10746 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10747 FirstMethod->getSourceRange(), MethodSingleBody)
10748 << FirstMethodType << FirstName << HasFirstBody;
10749 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10750 SecondMethod->getSourceRange(), MethodSingleBody)
10751 << SecondMethodType << SecondName << HasSecondBody;
10752 Diagnosed = true;
10753 break;
10754 }
10755
10756 if (HasFirstBody && HasSecondBody) {
10757 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10758 FirstMethod->getSourceRange(), MethodDifferentBody)
10759 << FirstMethodType << FirstName;
10760 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10761 SecondMethod->getSourceRange(), MethodDifferentBody)
10762 << SecondMethodType << SecondName;
10763 Diagnosed = true;
10764 break;
10765 }
10766
10767 break;
10768 }
10769 case TypeAlias:
10770 case TypeDef: {
10771 Diagnosed = ODRDiagTypeDefOrAlias(
10772 FirstRecord, FirstModule, SecondModule,
10773 cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl),
10774 FirstDiffType == TypeAlias);
10775 break;
10776 }
10777 case Var: {
10778 Diagnosed =
10779 ODRDiagVar(FirstRecord, FirstModule, SecondModule,
10780 cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl));
10781 break;
10782 }
10783 case Friend: {
10784 FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10785 FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10786
10787 NamedDecl *FirstND = FirstFriend->getFriendDecl();
10788 NamedDecl *SecondND = SecondFriend->getFriendDecl();
10789
10790 TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10791 TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10792
10793 if (FirstND && SecondND) {
10794 ODRDiagDeclError(FirstRecord, FirstModule,
10795 FirstFriend->getFriendLoc(),
10796 FirstFriend->getSourceRange(), FriendFunction)
10797 << FirstND;
10798 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10799 SecondFriend->getSourceRange(), FriendFunction)
10800 << SecondND;
10801
10802 Diagnosed = true;
10803 break;
10804 }
10805
10806 if (FirstTSI && SecondTSI) {
10807 QualType FirstFriendType = FirstTSI->getType();
10808 QualType SecondFriendType = SecondTSI->getType();
10809 assert(ComputeQualTypeODRHash(FirstFriendType) !=(static_cast<void> (0))
10810 ComputeQualTypeODRHash(SecondFriendType))(static_cast<void> (0));
10811 ODRDiagDeclError(FirstRecord, FirstModule,
10812 FirstFriend->getFriendLoc(),
10813 FirstFriend->getSourceRange(), FriendType)
10814 << FirstFriendType;
10815 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10816 SecondFriend->getSourceRange(), FriendType)
10817 << SecondFriendType;
10818 Diagnosed = true;
10819 break;
10820 }
10821
10822 ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(),
10823 FirstFriend->getSourceRange(), FriendTypeFunction)
10824 << (FirstTSI == nullptr);
10825 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10826 SecondFriend->getSourceRange(), FriendTypeFunction)
10827 << (SecondTSI == nullptr);
10828
10829 Diagnosed = true;
10830 break;
10831 }
10832 case FunctionTemplate: {
10833 FunctionTemplateDecl *FirstTemplate =
10834 cast<FunctionTemplateDecl>(FirstDecl);
10835 FunctionTemplateDecl *SecondTemplate =
10836 cast<FunctionTemplateDecl>(SecondDecl);
10837
10838 TemplateParameterList *FirstTPL =
10839 FirstTemplate->getTemplateParameters();
10840 TemplateParameterList *SecondTPL =
10841 SecondTemplate->getTemplateParameters();
10842
10843 if (FirstTPL->size() != SecondTPL->size()) {
10844 ODRDiagDeclError(FirstRecord, FirstModule,
10845 FirstTemplate->getLocation(),
10846 FirstTemplate->getSourceRange(),
10847 FunctionTemplateDifferentNumberParameters)
10848 << FirstTemplate << FirstTPL->size();
10849 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10850 SecondTemplate->getSourceRange(),
10851 FunctionTemplateDifferentNumberParameters)
10852 << SecondTemplate << SecondTPL->size();
10853
10854 Diagnosed = true;
10855 break;
10856 }
10857
10858 bool ParameterMismatch = false;
10859 for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10860 NamedDecl *FirstParam = FirstTPL->getParam(i);
10861 NamedDecl *SecondParam = SecondTPL->getParam(i);
10862
10863 if (FirstParam->getKind() != SecondParam->getKind()) {
10864 enum {
10865 TemplateTypeParameter,
10866 NonTypeTemplateParameter,
10867 TemplateTemplateParameter,
10868 };
10869 auto GetParamType = [](NamedDecl *D) {
10870 switch (D->getKind()) {
10871 default:
10872 llvm_unreachable("Unexpected template parameter type")__builtin_unreachable();
10873 case Decl::TemplateTypeParm:
10874 return TemplateTypeParameter;
10875 case Decl::NonTypeTemplateParm:
10876 return NonTypeTemplateParameter;
10877 case Decl::TemplateTemplateParm:
10878 return TemplateTemplateParameter;
10879 }
10880 };
10881
10882 ODRDiagDeclError(FirstRecord, FirstModule,
10883 FirstTemplate->getLocation(),
10884 FirstTemplate->getSourceRange(),
10885 FunctionTemplateParameterDifferentKind)
10886 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
10887 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10888 SecondTemplate->getSourceRange(),
10889 FunctionTemplateParameterDifferentKind)
10890 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10891
10892 ParameterMismatch = true;
10893 break;
10894 }
10895
10896 if (FirstParam->getName() != SecondParam->getName()) {
10897 ODRDiagDeclError(
10898 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10899 FirstTemplate->getSourceRange(), FunctionTemplateParameterName)
10900 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10901 << FirstParam;
10902 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10903 SecondTemplate->getSourceRange(),
10904 FunctionTemplateParameterName)
10905 << SecondTemplate << (i + 1)
10906 << (bool)SecondParam->getIdentifier() << SecondParam;
10907 ParameterMismatch = true;
10908 break;
10909 }
10910
10911 if (isa<TemplateTypeParmDecl>(FirstParam) &&
10912 isa<TemplateTypeParmDecl>(SecondParam)) {
10913 TemplateTypeParmDecl *FirstTTPD =
10914 cast<TemplateTypeParmDecl>(FirstParam);
10915 TemplateTypeParmDecl *SecondTTPD =
10916 cast<TemplateTypeParmDecl>(SecondParam);
10917 bool HasFirstDefaultArgument =
10918 FirstTTPD->hasDefaultArgument() &&
10919 !FirstTTPD->defaultArgumentWasInherited();
10920 bool HasSecondDefaultArgument =
10921 SecondTTPD->hasDefaultArgument() &&
10922 !SecondTTPD->defaultArgumentWasInherited();
10923 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10924 ODRDiagDeclError(FirstRecord, FirstModule,
10925 FirstTemplate->getLocation(),
10926 FirstTemplate->getSourceRange(),
10927 FunctionTemplateParameterSingleDefaultArgument)
10928 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10929 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10930 SecondTemplate->getSourceRange(),
10931 FunctionTemplateParameterSingleDefaultArgument)
10932 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10933 ParameterMismatch = true;
10934 break;
10935 }
10936
10937 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10938 QualType FirstType = FirstTTPD->getDefaultArgument();
10939 QualType SecondType = SecondTTPD->getDefaultArgument();
10940 if (ComputeQualTypeODRHash(FirstType) !=
10941 ComputeQualTypeODRHash(SecondType)) {
10942 ODRDiagDeclError(
10943 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10944 FirstTemplate->getSourceRange(),
10945 FunctionTemplateParameterDifferentDefaultArgument)
10946 << FirstTemplate << (i + 1) << FirstType;
10947 ODRDiagDeclNote(
10948 SecondModule, SecondTemplate->getLocation(),
10949 SecondTemplate->getSourceRange(),
10950 FunctionTemplateParameterDifferentDefaultArgument)
10951 << SecondTemplate << (i + 1) << SecondType;
10952 ParameterMismatch = true;
10953 break;
10954 }
10955 }
10956
10957 if (FirstTTPD->isParameterPack() !=
10958 SecondTTPD->isParameterPack()) {
10959 ODRDiagDeclError(FirstRecord, FirstModule,
10960 FirstTemplate->getLocation(),
10961 FirstTemplate->getSourceRange(),
10962 FunctionTemplatePackParameter)
10963 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
10964 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10965 SecondTemplate->getSourceRange(),
10966 FunctionTemplatePackParameter)
10967 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
10968 ParameterMismatch = true;
10969 break;
10970 }
10971 }
10972
10973 if (isa<TemplateTemplateParmDecl>(FirstParam) &&
10974 isa<TemplateTemplateParmDecl>(SecondParam)) {
10975 TemplateTemplateParmDecl *FirstTTPD =
10976 cast<TemplateTemplateParmDecl>(FirstParam);
10977 TemplateTemplateParmDecl *SecondTTPD =
10978 cast<TemplateTemplateParmDecl>(SecondParam);
10979
10980 TemplateParameterList *FirstTPL =
10981 FirstTTPD->getTemplateParameters();
10982 TemplateParameterList *SecondTPL =
10983 SecondTTPD->getTemplateParameters();
10984
10985 if (ComputeTemplateParameterListODRHash(FirstTPL) !=
10986 ComputeTemplateParameterListODRHash(SecondTPL)) {
10987 ODRDiagDeclError(FirstRecord, FirstModule,
10988 FirstTemplate->getLocation(),
10989 FirstTemplate->getSourceRange(),
10990 FunctionTemplateParameterDifferentType)
10991 << FirstTemplate << (i + 1);
10992 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10993 SecondTemplate->getSourceRange(),
10994 FunctionTemplateParameterDifferentType)
10995 << SecondTemplate << (i + 1);
10996 ParameterMismatch = true;
10997 break;
10998 }
10999
11000 bool HasFirstDefaultArgument =
11001 FirstTTPD->hasDefaultArgument() &&
11002 !FirstTTPD->defaultArgumentWasInherited();
11003 bool HasSecondDefaultArgument =
11004 SecondTTPD->hasDefaultArgument() &&
11005 !SecondTTPD->defaultArgumentWasInherited();
11006 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11007 ODRDiagDeclError(FirstRecord, FirstModule,
11008 FirstTemplate->getLocation(),
11009 FirstTemplate->getSourceRange(),
11010 FunctionTemplateParameterSingleDefaultArgument)
11011 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11012 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11013 SecondTemplate->getSourceRange(),
11014 FunctionTemplateParameterSingleDefaultArgument)
11015 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11016 ParameterMismatch = true;
11017 break;
11018 }
11019
11020 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11021 TemplateArgument FirstTA =
11022 FirstTTPD->getDefaultArgument().getArgument();
11023 TemplateArgument SecondTA =
11024 SecondTTPD->getDefaultArgument().getArgument();
11025 if (ComputeTemplateArgumentODRHash(FirstTA) !=
11026 ComputeTemplateArgumentODRHash(SecondTA)) {
11027 ODRDiagDeclError(
11028 FirstRecord, FirstModule, FirstTemplate->getLocation(),
11029 FirstTemplate->getSourceRange(),
11030 FunctionTemplateParameterDifferentDefaultArgument)
11031 << FirstTemplate << (i + 1) << FirstTA;
11032 ODRDiagDeclNote(
11033 SecondModule, SecondTemplate->getLocation(),
11034 SecondTemplate->getSourceRange(),
11035 FunctionTemplateParameterDifferentDefaultArgument)
11036 << SecondTemplate << (i + 1) << SecondTA;
11037 ParameterMismatch = true;
11038 break;
11039 }
11040 }
11041
11042 if (FirstTTPD->isParameterPack() !=
11043 SecondTTPD->isParameterPack()) {
11044 ODRDiagDeclError(FirstRecord, FirstModule,
11045 FirstTemplate->getLocation(),
11046 FirstTemplate->getSourceRange(),
11047 FunctionTemplatePackParameter)
11048 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11049 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11050 SecondTemplate->getSourceRange(),
11051 FunctionTemplatePackParameter)
11052 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11053 ParameterMismatch = true;
11054 break;
11055 }
11056 }
11057
11058 if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11059 isa<NonTypeTemplateParmDecl>(SecondParam)) {
11060 NonTypeTemplateParmDecl *FirstNTTPD =
11061 cast<NonTypeTemplateParmDecl>(FirstParam);
11062 NonTypeTemplateParmDecl *SecondNTTPD =
11063 cast<NonTypeTemplateParmDecl>(SecondParam);
11064
11065 QualType FirstType = FirstNTTPD->getType();
11066 QualType SecondType = SecondNTTPD->getType();
11067 if (ComputeQualTypeODRHash(FirstType) !=
11068 ComputeQualTypeODRHash(SecondType)) {
11069 ODRDiagDeclError(FirstRecord, FirstModule,
11070 FirstTemplate->getLocation(),
11071 FirstTemplate->getSourceRange(),
11072 FunctionTemplateParameterDifferentType)
11073 << FirstTemplate << (i + 1);
11074 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11075 SecondTemplate->getSourceRange(),
11076 FunctionTemplateParameterDifferentType)
11077 << SecondTemplate << (i + 1);
11078 ParameterMismatch = true;
11079 break;
11080 }
11081
11082 bool HasFirstDefaultArgument =
11083 FirstNTTPD->hasDefaultArgument() &&
11084 !FirstNTTPD->defaultArgumentWasInherited();
11085 bool HasSecondDefaultArgument =
11086 SecondNTTPD->hasDefaultArgument() &&
11087 !SecondNTTPD->defaultArgumentWasInherited();
11088 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11089 ODRDiagDeclError(FirstRecord, FirstModule,
11090 FirstTemplate->getLocation(),
11091 FirstTemplate->getSourceRange(),
11092 FunctionTemplateParameterSingleDefaultArgument)
11093 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11094 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11095 SecondTemplate->getSourceRange(),
11096 FunctionTemplateParameterSingleDefaultArgument)
11097 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11098 ParameterMismatch = true;
11099 break;
11100 }
11101
11102 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11103 Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11104 Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11105 if (ComputeODRHash(FirstDefaultArgument) !=
11106 ComputeODRHash(SecondDefaultArgument)) {
11107 ODRDiagDeclError(
11108 FirstRecord, FirstModule, FirstTemplate->getLocation(),
11109 FirstTemplate->getSourceRange(),
11110 FunctionTemplateParameterDifferentDefaultArgument)
11111 << FirstTemplate << (i + 1) << FirstDefaultArgument;
11112 ODRDiagDeclNote(
11113 SecondModule, SecondTemplate->getLocation(),
11114 SecondTemplate->getSourceRange(),
11115 FunctionTemplateParameterDifferentDefaultArgument)
11116 << SecondTemplate << (i + 1) << SecondDefaultArgument;
11117 ParameterMismatch = true;
11118 break;
11119 }
11120 }
11121
11122 if (FirstNTTPD->isParameterPack() !=
11123 SecondNTTPD->isParameterPack()) {
11124 ODRDiagDeclError(FirstRecord, FirstModule,
11125 FirstTemplate->getLocation(),
11126 FirstTemplate->getSourceRange(),
11127 FunctionTemplatePackParameter)
11128 << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11129 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11130 SecondTemplate->getSourceRange(),
11131 FunctionTemplatePackParameter)
11132 << SecondTemplate << (i + 1)
11133 << SecondNTTPD->isParameterPack();
11134 ParameterMismatch = true;
11135 break;
11136 }
11137 }
11138 }
11139
11140 if (ParameterMismatch) {
11141 Diagnosed = true;
11142 break;
11143 }
11144
11145 break;
11146 }
11147 }
11148
11149 if (Diagnosed)
11150 continue;
11151
11152 Diag(FirstDecl->getLocation(),
11153 diag::err_module_odr_violation_mismatch_decl_unknown)
11154 << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11155 << FirstDecl->getSourceRange();
11156 Diag(SecondDecl->getLocation(),
11157 diag::note_module_odr_violation_mismatch_decl_unknown)
11158 << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11159 Diagnosed = true;
11160 }
11161
11162 if (!Diagnosed) {
11163 // All definitions are updates to the same declaration. This happens if a
11164 // module instantiates the declaration of a class template specialization
11165 // and two or more other modules instantiate its definition.
11166 //
11167 // FIXME: Indicate which modules had instantiations of this definition.
11168 // FIXME: How can this even happen?
11169 Diag(Merge.first->getLocation(),
11170 diag::err_module_odr_violation_different_instantiations)
11171 << Merge.first;
11172 }
11173 }
11174
11175 // Issue ODR failures diagnostics for functions.
11176 for (auto &Merge : FunctionOdrMergeFailures) {
11177 enum ODRFunctionDifference {
11178 ReturnType,
11179 ParameterName,
11180 ParameterType,
11181 ParameterSingleDefaultArgument,
11182 ParameterDifferentDefaultArgument,
11183 FunctionBody,
11184 };
11185
11186 FunctionDecl *FirstFunction = Merge.first;
11187 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11188
11189 bool Diagnosed = false;
11190 for (auto &SecondFunction : Merge.second) {
11191
11192 if (FirstFunction == SecondFunction)
11193 continue;
11194
11195 std::string SecondModule =
11196 getOwningModuleNameForDiagnostic(SecondFunction);
11197
11198 auto ODRDiagError = [FirstFunction, &FirstModule,
11199 this](SourceLocation Loc, SourceRange Range,
11200 ODRFunctionDifference DiffType) {
11201 return Diag(Loc, diag::err_module_odr_violation_function)
11202 << FirstFunction << FirstModule.empty() << FirstModule << Range
11203 << DiffType;
11204 };
11205 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11206 SourceRange Range,
11207 ODRFunctionDifference DiffType) {
11208 return Diag(Loc, diag::note_module_odr_violation_function)
11209 << SecondModule << Range << DiffType;
11210 };
11211
11212 if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11213 ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11214 ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11215 FirstFunction->getReturnTypeSourceRange(), ReturnType)
11216 << FirstFunction->getReturnType();
11217 ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11218 SecondFunction->getReturnTypeSourceRange(), ReturnType)
11219 << SecondFunction->getReturnType();
11220 Diagnosed = true;
11221 break;
11222 }
11223
11224 assert(FirstFunction->param_size() == SecondFunction->param_size() &&(static_cast<void> (0))
11225 "Merged functions with different number of parameters")(static_cast<void> (0));
11226
11227 auto ParamSize = FirstFunction->param_size();
11228 bool ParameterMismatch = false;
11229 for (unsigned I = 0; I < ParamSize; ++I) {
11230 auto *FirstParam = FirstFunction->getParamDecl(I);
11231 auto *SecondParam = SecondFunction->getParamDecl(I);
11232
11233 assert(getContext().hasSameType(FirstParam->getType(),(static_cast<void> (0))
11234 SecondParam->getType()) &&(static_cast<void> (0))
11235 "Merged function has different parameter types.")(static_cast<void> (0));
11236
11237 if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11238 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11239 ParameterName)
11240 << I + 1 << FirstParam->getDeclName();
11241 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11242 ParameterName)
11243 << I + 1 << SecondParam->getDeclName();
11244 ParameterMismatch = true;
11245 break;
11246 };
11247
11248 QualType FirstParamType = FirstParam->getType();
11249 QualType SecondParamType = SecondParam->getType();
11250 if (FirstParamType != SecondParamType &&
11251 ComputeQualTypeODRHash(FirstParamType) !=
11252 ComputeQualTypeODRHash(SecondParamType)) {
11253 if (const DecayedType *ParamDecayedType =
11254 FirstParamType->getAs<DecayedType>()) {
11255 ODRDiagError(FirstParam->getLocation(),
11256 FirstParam->getSourceRange(), ParameterType)
11257 << (I + 1) << FirstParamType << true
11258 << ParamDecayedType->getOriginalType();
11259 } else {
11260 ODRDiagError(FirstParam->getLocation(),
11261 FirstParam->getSourceRange(), ParameterType)
11262 << (I + 1) << FirstParamType << false;
11263 }
11264
11265 if (const DecayedType *ParamDecayedType =
11266 SecondParamType->getAs<DecayedType>()) {
11267 ODRDiagNote(SecondParam->getLocation(),
11268 SecondParam->getSourceRange(), ParameterType)
11269 << (I + 1) << SecondParamType << true
11270 << ParamDecayedType->getOriginalType();
11271 } else {
11272 ODRDiagNote(SecondParam->getLocation(),
11273 SecondParam->getSourceRange(), ParameterType)
11274 << (I + 1) << SecondParamType << false;
11275 }
11276 ParameterMismatch = true;
11277 break;
11278 }
11279
11280 const Expr *FirstInit = FirstParam->getInit();
11281 const Expr *SecondInit = SecondParam->getInit();
11282 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11283 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11284 ParameterSingleDefaultArgument)
11285 << (I + 1) << (FirstInit == nullptr)
11286 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11287 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11288 ParameterSingleDefaultArgument)
11289 << (I + 1) << (SecondInit == nullptr)
11290 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11291 ParameterMismatch = true;
11292 break;
11293 }
11294
11295 if (FirstInit && SecondInit &&
11296 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11297 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11298 ParameterDifferentDefaultArgument)
11299 << (I + 1) << FirstInit->getSourceRange();
11300 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11301 ParameterDifferentDefaultArgument)
11302 << (I + 1) << SecondInit->getSourceRange();
11303 ParameterMismatch = true;
11304 break;
11305 }
11306
11307 assert(ComputeSubDeclODRHash(FirstParam) ==(static_cast<void> (0))
11308 ComputeSubDeclODRHash(SecondParam) &&(static_cast<void> (0))
11309 "Undiagnosed parameter difference.")(static_cast<void> (0));
11310 }
11311
11312 if (ParameterMismatch) {
11313 Diagnosed = true;
11314 break;
11315 }
11316
11317 // If no error has been generated before now, assume the problem is in
11318 // the body and generate a message.
11319 ODRDiagError(FirstFunction->getLocation(),
11320 FirstFunction->getSourceRange(), FunctionBody);
11321 ODRDiagNote(SecondFunction->getLocation(),
11322 SecondFunction->getSourceRange(), FunctionBody);
11323 Diagnosed = true;
11324 break;
11325 }
11326 (void)Diagnosed;
11327 assert(Diagnosed && "Unable to emit ODR diagnostic.")(static_cast<void> (0));
11328 }
11329
11330 // Issue ODR failures diagnostics for enums.
11331 for (auto &Merge : EnumOdrMergeFailures) {
11332 enum ODREnumDifference {
11333 SingleScopedEnum,
11334 EnumTagKeywordMismatch,
11335 SingleSpecifiedType,
11336 DifferentSpecifiedTypes,
11337 DifferentNumberEnumConstants,
11338 EnumConstantName,
11339 EnumConstantSingleInitilizer,
11340 EnumConstantDifferentInitilizer,
11341 };
11342
11343 // If we've already pointed out a specific problem with this enum, don't
11344 // bother issuing a general "something's different" diagnostic.
11345 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11346 continue;
11347
11348 EnumDecl *FirstEnum = Merge.first;
11349 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11350
11351 using DeclHashes =
11352 llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11353 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11354 DeclHashes &Hashes, EnumDecl *Enum) {
11355 for (auto *D : Enum->decls()) {
11356 // Due to decl merging, the first EnumDecl is the parent of
11357 // Decls in both records.
11358 if (!ODRHash::isDeclToBeProcessed(D, FirstEnum))
11359 continue;
11360 assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind")(static_cast<void> (0));
11361 Hashes.emplace_back(cast<EnumConstantDecl>(D),
11362 ComputeSubDeclODRHash(D));
11363 }
11364 };
11365 DeclHashes FirstHashes;
11366 PopulateHashes(FirstHashes, FirstEnum);
11367 bool Diagnosed = false;
11368 for (auto &SecondEnum : Merge.second) {
11369
11370 if (FirstEnum == SecondEnum)
11371 continue;
11372
11373 std::string SecondModule =
11374 getOwningModuleNameForDiagnostic(SecondEnum);
11375
11376 auto ODRDiagError = [FirstEnum, &FirstModule,
11377 this](SourceLocation Loc, SourceRange Range,
11378 ODREnumDifference DiffType) {
11379 return Diag(Loc, diag::err_module_odr_violation_enum)
11380 << FirstEnum << FirstModule.empty() << FirstModule << Range
11381 << DiffType;
11382 };
11383 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11384 SourceRange Range,
11385 ODREnumDifference DiffType) {
11386 return Diag(Loc, diag::note_module_odr_violation_enum)
11387 << SecondModule << Range << DiffType;
11388 };
11389
11390 if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11391 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11392 SingleScopedEnum)
11393 << FirstEnum->isScoped();
11394 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11395 SingleScopedEnum)
11396 << SecondEnum->isScoped();
11397 Diagnosed = true;
11398 continue;
11399 }
11400
11401 if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11402 if (FirstEnum->isScopedUsingClassTag() !=
11403 SecondEnum->isScopedUsingClassTag()) {
11404 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11405 EnumTagKeywordMismatch)
11406 << FirstEnum->isScopedUsingClassTag();
11407 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11408 EnumTagKeywordMismatch)
11409 << SecondEnum->isScopedUsingClassTag();
11410 Diagnosed = true;
11411 continue;
11412 }
11413 }
11414
11415 QualType FirstUnderlyingType =
11416 FirstEnum->getIntegerTypeSourceInfo()
11417 ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11418 : QualType();
11419 QualType SecondUnderlyingType =
11420 SecondEnum->getIntegerTypeSourceInfo()
11421 ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11422 : QualType();
11423 if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11424 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11425 SingleSpecifiedType)
11426 << !FirstUnderlyingType.isNull();
11427 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11428 SingleSpecifiedType)
11429 << !SecondUnderlyingType.isNull();
11430 Diagnosed = true;
11431 continue;
11432 }
11433
11434 if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11435 if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11436 ComputeQualTypeODRHash(SecondUnderlyingType)) {
11437 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11438 DifferentSpecifiedTypes)
11439 << FirstUnderlyingType;
11440 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11441 DifferentSpecifiedTypes)
11442 << SecondUnderlyingType;
11443 Diagnosed = true;
11444 continue;
11445 }
11446 }
11447
11448 DeclHashes SecondHashes;
11449 PopulateHashes(SecondHashes, SecondEnum);
11450
11451 if (FirstHashes.size() != SecondHashes.size()) {
11452 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11453 DifferentNumberEnumConstants)
11454 << (int)FirstHashes.size();
11455 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11456 DifferentNumberEnumConstants)
11457 << (int)SecondHashes.size();
11458 Diagnosed = true;
11459 continue;
11460 }
11461
11462 for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11463 if (FirstHashes[I].second == SecondHashes[I].second)
11464 continue;
11465 const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11466 const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11467
11468 if (FirstEnumConstant->getDeclName() !=
11469 SecondEnumConstant->getDeclName()) {
11470
11471 ODRDiagError(FirstEnumConstant->getLocation(),
11472 FirstEnumConstant->getSourceRange(), EnumConstantName)
11473 << I + 1 << FirstEnumConstant;
11474 ODRDiagNote(SecondEnumConstant->getLocation(),
11475 SecondEnumConstant->getSourceRange(), EnumConstantName)
11476 << I + 1 << SecondEnumConstant;
11477 Diagnosed = true;
11478 break;
11479 }
11480
11481 const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11482 const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11483 if (!FirstInit && !SecondInit)
11484 continue;
11485
11486 if (!FirstInit || !SecondInit) {
11487 ODRDiagError(FirstEnumConstant->getLocation(),
11488 FirstEnumConstant->getSourceRange(),
11489 EnumConstantSingleInitilizer)
11490 << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11491 ODRDiagNote(SecondEnumConstant->getLocation(),
11492 SecondEnumConstant->getSourceRange(),
11493 EnumConstantSingleInitilizer)
11494 << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11495 Diagnosed = true;
11496 break;
11497 }
11498
11499 if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11500 ODRDiagError(FirstEnumConstant->getLocation(),
11501 FirstEnumConstant->getSourceRange(),
11502 EnumConstantDifferentInitilizer)
11503 << I + 1 << FirstEnumConstant;
11504 ODRDiagNote(SecondEnumConstant->getLocation(),
11505 SecondEnumConstant->getSourceRange(),
11506 EnumConstantDifferentInitilizer)
11507 << I + 1 << SecondEnumConstant;
11508 Diagnosed = true;
11509 break;
11510 }
11511 }
11512 }
11513
11514 (void)Diagnosed;
11515 assert(Diagnosed && "Unable to emit ODR diagnostic.")(static_cast<void> (0));
11516 }
11517}
11518
11519void ASTReader::StartedDeserializing() {
11520 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11521 ReadTimer->startTimer();
11522}
11523
11524void ASTReader::FinishedDeserializing() {
11525 assert(NumCurrentElementsDeserializing &&(static_cast<void> (0))
11526 "FinishedDeserializing not paired with StartedDeserializing")(static_cast<void> (0));
11527 if (NumCurrentElementsDeserializing == 1) {
11528 // We decrease NumCurrentElementsDeserializing only after pending actions
11529 // are finished, to avoid recursively re-calling finishPendingActions().
11530 finishPendingActions();
11531 }
11532 --NumCurrentElementsDeserializing;
11533
11534 if (NumCurrentElementsDeserializing == 0) {
11535 // Propagate exception specification and deduced type updates along
11536 // redeclaration chains.
11537 //
11538 // We do this now rather than in finishPendingActions because we want to
11539 // be able to walk the complete redeclaration chains of the updated decls.
11540 while (!PendingExceptionSpecUpdates.empty() ||
11541 !PendingDeducedTypeUpdates.empty()) {
11542 auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11543 PendingExceptionSpecUpdates.clear();
11544 for (auto Update : ESUpdates) {
11545 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11546 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11547 auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11548 if (auto *Listener = getContext().getASTMutationListener())
11549 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11550 for (auto *Redecl : Update.second->redecls())
11551 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11552 }
11553
11554 auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11555 PendingDeducedTypeUpdates.clear();
11556 for (auto Update : DTUpdates) {
11557 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11558 // FIXME: If the return type is already deduced, check that it matches.
11559 getContext().adjustDeducedFunctionResultType(Update.first,
11560 Update.second);
11561 }
11562 }
11563
11564 if (ReadTimer)
11565 ReadTimer->stopTimer();
11566
11567 diagnoseOdrViolations();
11568
11569 // We are not in recursive loading, so it's safe to pass the "interesting"
11570 // decls to the consumer.
11571 if (Consumer)
11572 PassInterestingDeclsToConsumer();
11573 }
11574}
11575
11576void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11577 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11578 // Remove any fake results before adding any real ones.
11579 auto It = PendingFakeLookupResults.find(II);
11580 if (It != PendingFakeLookupResults.end()) {
11581 for (auto *ND : It->second)
11582 SemaObj->IdResolver.RemoveDecl(ND);
11583 // FIXME: this works around module+PCH performance issue.
11584 // Rather than erase the result from the map, which is O(n), just clear
11585 // the vector of NamedDecls.
11586 It->second.clear();
11587 }
11588 }
11589
11590 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11591 SemaObj->TUScope->AddDecl(D);
11592 } else if (SemaObj->TUScope) {
11593 // Adding the decl to IdResolver may have failed because it was already in
11594 // (even though it was not added in scope). If it is already in, make sure
11595 // it gets in the scope as well.
11596 if (std::find(SemaObj->IdResolver.begin(Name),
11597 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11598 SemaObj->TUScope->AddDecl(D);
11599 }
11600}
11601
11602ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11603 ASTContext *Context,
11604 const PCHContainerReader &PCHContainerRdr,
11605 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11606 StringRef isysroot,
11607 DisableValidationForModuleKind DisableValidationKind,
11608 bool AllowASTWithCompilerErrors,
11609 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11610 bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11611 std::unique_ptr<llvm::Timer> ReadTimer)
11612 : Listener(bool(DisableValidationKind &DisableValidationForModuleKind::PCH)
11613 ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11614 : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11615 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11616 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11617 ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11618 PCHContainerRdr, PP.getHeaderSearchInfo()),
11619 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11620 DisableValidationKind(DisableValidationKind),
11621 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11622 AllowConfigurationMismatch(AllowConfigurationMismatch),
11623 ValidateSystemInputs(ValidateSystemInputs),
11624 ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11625 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11626 SourceMgr.setExternalSLocEntrySource(this);
11627
11628 for (const auto &Ext : Extensions) {
11629 auto BlockName = Ext->getExtensionMetadata().BlockName;
11630 auto Known = ModuleFileExtensions.find(BlockName);
11631 if (Known != ModuleFileExtensions.end()) {
11632 Diags.Report(diag::warn_duplicate_module_file_extension)
11633 << BlockName;
11634 continue;
11635 }
11636
11637 ModuleFileExtensions.insert({BlockName, Ext});
11638 }
11639}
11640
11641ASTReader::~ASTReader() {
11642 if (OwnsDeserializationListener)
11643 delete DeserializationListener;
11644}
11645
11646IdentifierResolver &ASTReader::getIdResolver() {
11647 return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11648}
11649
11650Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11651 unsigned AbbrevID) {
11652 Idx = 0;
11653 Record.clear();
11654 return Cursor.readRecord(AbbrevID, Record);
11655}
11656//===----------------------------------------------------------------------===//
11657//// OMPClauseReader implementation
11658////===----------------------------------------------------------------------===//
11659
11660// This has to be in namespace clang because it's friended by all
11661// of the OMP clauses.
11662namespace clang {
11663
11664class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11665 ASTRecordReader &Record;
11666 ASTContext &Context;
11667
11668public:
11669 OMPClauseReader(ASTRecordReader &Record)
11670 : Record(Record), Context(Record.getContext()) {}
11671#define GEN_CLANG_CLAUSE_CLASS
11672#define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11673#include "llvm/Frontend/OpenMP/OMP.inc"
11674 OMPClause *readClause();
11675 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11676 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11677};
11678
11679} // end namespace clang
11680
11681OMPClause *ASTRecordReader::readOMPClause() {
11682 return OMPClauseReader(*this).readClause();
11683}
11684
11685OMPClause *OMPClauseReader::readClause() {
11686 OMPClause *C = nullptr;
11687 switch (llvm::omp::Clause(Record.readInt())) {
11688 case llvm::omp::OMPC_if:
11689 C = new (Context) OMPIfClause();
11690 break;
11691 case llvm::omp::OMPC_final:
11692 C = new (Context) OMPFinalClause();
11693 break;
11694 case llvm::omp::OMPC_num_threads:
11695 C = new (Context) OMPNumThreadsClause();
11696 break;
11697 case llvm::omp::OMPC_safelen:
11698 C = new (Context) OMPSafelenClause();
11699 break;
11700 case llvm::omp::OMPC_simdlen:
11701 C = new (Context) OMPSimdlenClause();
11702 break;
11703 case llvm::omp::OMPC_sizes: {
11704 unsigned NumSizes = Record.readInt();
11705 C = OMPSizesClause::CreateEmpty(Context, NumSizes);
11706 break;
11707 }
11708 case llvm::omp::OMPC_full:
11709 C = OMPFullClause::CreateEmpty(Context);
11710 break;
11711 case llvm::omp::OMPC_partial:
11712 C = OMPPartialClause::CreateEmpty(Context);
11713 break;
11714 case llvm::omp::OMPC_allocator:
11715 C = new (Context) OMPAllocatorClause();
11716 break;
11717 case llvm::omp::OMPC_collapse:
11718 C = new (Context) OMPCollapseClause();
11719 break;
11720 case llvm::omp::OMPC_default:
11721 C = new (Context) OMPDefaultClause();
11722 break;
11723 case llvm::omp::OMPC_proc_bind:
11724 C = new (Context) OMPProcBindClause();
11725 break;
11726 case llvm::omp::OMPC_schedule:
11727 C = new (Context) OMPScheduleClause();
11728 break;
11729 case llvm::omp::OMPC_ordered:
11730 C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11731 break;
11732 case llvm::omp::OMPC_nowait:
11733 C = new (Context) OMPNowaitClause();
11734 break;
11735 case llvm::omp::OMPC_untied:
11736 C = new (Context) OMPUntiedClause();
11737 break;
11738 case llvm::omp::OMPC_mergeable:
11739 C = new (Context) OMPMergeableClause();
11740 break;
11741 case llvm::omp::OMPC_read:
11742 C = new (Context) OMPReadClause();
11743 break;
11744 case llvm::omp::OMPC_write:
11745 C = new (Context) OMPWriteClause();
11746 break;
11747 case llvm::omp::OMPC_update:
11748 C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
11749 break;
11750 case llvm::omp::OMPC_capture:
11751 C = new (Context) OMPCaptureClause();
11752 break;
11753 case llvm::omp::OMPC_seq_cst:
11754 C = new (Context) OMPSeqCstClause();
11755 break;
11756 case llvm::omp::OMPC_acq_rel:
11757 C = new (Context) OMPAcqRelClause();
11758 break;
11759 case llvm::omp::OMPC_acquire:
11760 C = new (Context) OMPAcquireClause();
11761 break;
11762 case llvm::omp::OMPC_release:
11763 C = new (Context) OMPReleaseClause();
11764 break;
11765 case llvm::omp::OMPC_relaxed:
11766 C = new (Context) OMPRelaxedClause();
11767 break;
11768 case llvm::omp::OMPC_threads:
11769 C = new (Context) OMPThreadsClause();
11770 break;
11771 case llvm::omp::OMPC_simd:
11772 C = new (Context) OMPSIMDClause();
11773 break;
11774 case llvm::omp::OMPC_nogroup:
11775 C = new (Context) OMPNogroupClause();
11776 break;
11777 case llvm::omp::OMPC_unified_address:
11778 C = new (Context) OMPUnifiedAddressClause();
11779 break;
11780 case llvm::omp::OMPC_unified_shared_memory:
11781 C = new (Context) OMPUnifiedSharedMemoryClause();
11782 break;
11783 case llvm::omp::OMPC_reverse_offload:
11784 C = new (Context) OMPReverseOffloadClause();
11785 break;
11786 case llvm::omp::OMPC_dynamic_allocators:
11787 C = new (Context) OMPDynamicAllocatorsClause();
11788 break;
11789 case llvm::omp::OMPC_atomic_default_mem_order:
11790 C = new (Context) OMPAtomicDefaultMemOrderClause();
11791 break;
11792 case llvm::omp::OMPC_private:
11793 C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11794 break;
11795 case llvm::omp::OMPC_firstprivate:
11796 C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11797 break;
11798 case llvm::omp::OMPC_lastprivate:
11799 C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11800 break;
11801 case llvm::omp::OMPC_shared:
11802 C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11803 break;
11804 case llvm::omp::OMPC_reduction: {
11805 unsigned N = Record.readInt();
11806 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11807 C = OMPReductionClause::CreateEmpty(Context, N, Modifier);
11808 break;
11809 }
11810 case llvm::omp::OMPC_task_reduction:
11811 C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11812 break;
11813 case llvm::omp::OMPC_in_reduction:
11814 C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11815 break;
11816 case llvm::omp::OMPC_linear:
11817 C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11818 break;
11819 case llvm::omp::OMPC_aligned:
11820 C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11821 break;
11822 case llvm::omp::OMPC_copyin:
11823 C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11824 break;
11825 case llvm::omp::OMPC_copyprivate:
11826 C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11827 break;
11828 case llvm::omp::OMPC_flush:
11829 C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11830 break;
11831 case llvm::omp::OMPC_depobj:
11832 C = OMPDepobjClause::CreateEmpty(Context);
11833 break;
11834 case llvm::omp::OMPC_depend: {
11835 unsigned NumVars = Record.readInt();
11836 unsigned NumLoops = Record.readInt();
11837 C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11838 break;
11839 }
11840 case llvm::omp::OMPC_device:
11841 C = new (Context) OMPDeviceClause();
11842 break;
11843 case llvm::omp::OMPC_map: {
11844 OMPMappableExprListSizeTy Sizes;
11845 Sizes.NumVars = Record.readInt();
11846 Sizes.NumUniqueDeclarations = Record.readInt();
11847 Sizes.NumComponentLists = Record.readInt();
11848 Sizes.NumComponents = Record.readInt();
11849 C = OMPMapClause::CreateEmpty(Context, Sizes);
11850 break;
11851 }
11852 case llvm::omp::OMPC_num_teams:
11853 C = new (Context) OMPNumTeamsClause();
11854 break;
11855 case llvm::omp::OMPC_thread_limit:
11856 C = new (Context) OMPThreadLimitClause();
11857 break;
11858 case llvm::omp::OMPC_priority:
11859 C = new (Context) OMPPriorityClause();
11860 break;
11861 case llvm::omp::OMPC_grainsize:
11862 C = new (Context) OMPGrainsizeClause();
11863 break;
11864 case llvm::omp::OMPC_num_tasks:
11865 C = new (Context) OMPNumTasksClause();
11866 break;
11867 case llvm::omp::OMPC_hint:
11868 C = new (Context) OMPHintClause();
11869 break;
11870 case llvm::omp::OMPC_dist_schedule:
11871 C = new (Context) OMPDistScheduleClause();
11872 break;
11873 case llvm::omp::OMPC_defaultmap:
11874 C = new (Context) OMPDefaultmapClause();
11875 break;
11876 case llvm::omp::OMPC_to: {
11877 OMPMappableExprListSizeTy Sizes;
11878 Sizes.NumVars = Record.readInt();
11879 Sizes.NumUniqueDeclarations = Record.readInt();
11880 Sizes.NumComponentLists = Record.readInt();
11881 Sizes.NumComponents = Record.readInt();
11882 C = OMPToClause::CreateEmpty(Context, Sizes);
11883 break;
11884 }
11885 case llvm::omp::OMPC_from: {
11886 OMPMappableExprListSizeTy Sizes;
11887 Sizes.NumVars = Record.readInt();
11888 Sizes.NumUniqueDeclarations = Record.readInt();
11889 Sizes.NumComponentLists = Record.readInt();
11890 Sizes.NumComponents = Record.readInt();
11891 C = OMPFromClause::CreateEmpty(Context, Sizes);
11892 break;
11893 }
11894 case llvm::omp::OMPC_use_device_ptr: {
11895 OMPMappableExprListSizeTy Sizes;
11896 Sizes.NumVars = Record.readInt();
11897 Sizes.NumUniqueDeclarations = Record.readInt();
11898 Sizes.NumComponentLists = Record.readInt();
11899 Sizes.NumComponents = Record.readInt();
11900 C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
11901 break;
11902 }
11903 case llvm::omp::OMPC_use_device_addr: {
11904 OMPMappableExprListSizeTy Sizes;
11905 Sizes.NumVars = Record.readInt();
11906 Sizes.NumUniqueDeclarations = Record.readInt();
11907 Sizes.NumComponentLists = Record.readInt();
11908 Sizes.NumComponents = Record.readInt();
11909 C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes);
11910 break;
11911 }
11912 case llvm::omp::OMPC_is_device_ptr: {
11913 OMPMappableExprListSizeTy Sizes;
11914 Sizes.NumVars = Record.readInt();
11915 Sizes.NumUniqueDeclarations = Record.readInt();
11916 Sizes.NumComponentLists = Record.readInt();
11917 Sizes.NumComponents = Record.readInt();
11918 C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
11919 break;
11920 }
11921 case llvm::omp::OMPC_allocate:
11922 C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
11923 break;
11924 case llvm::omp::OMPC_nontemporal:
11925 C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
11926 break;
11927 case llvm::omp::OMPC_inclusive:
11928 C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
11929 break;
11930 case llvm::omp::OMPC_exclusive:
11931 C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
11932 break;
11933 case llvm::omp::OMPC_order:
11934 C = new (Context) OMPOrderClause();
11935 break;
11936 case llvm::omp::OMPC_init:
11937 C = OMPInitClause::CreateEmpty(Context, Record.readInt());
11938 break;
11939 case llvm::omp::OMPC_use:
11940 C = new (Context) OMPUseClause();
11941 break;
11942 case llvm::omp::OMPC_destroy:
11943 C = new (Context) OMPDestroyClause();
11944 break;
11945 case llvm::omp::OMPC_novariants:
11946 C = new (Context) OMPNovariantsClause();
11947 break;
11948 case llvm::omp::OMPC_nocontext:
11949 C = new (Context) OMPNocontextClause();
11950 break;
11951 case llvm::omp::OMPC_detach:
11952 C = new (Context) OMPDetachClause();
11953 break;
11954 case llvm::omp::OMPC_uses_allocators:
11955 C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt());
11956 break;
11957 case llvm::omp::OMPC_affinity:
11958 C = OMPAffinityClause::CreateEmpty(Context, Record.readInt());
11959 break;
11960 case llvm::omp::OMPC_filter:
11961 C = new (Context) OMPFilterClause();
11962 break;
11963#define OMP_CLAUSE_NO_CLASS(Enum, Str)case llvm::omp::Enum: break; \
11964 case llvm::omp::Enum: \
11965 break;
11966#include "llvm/Frontend/OpenMP/OMPKinds.def"
11967 default:
11968 break;
11969 }
11970 assert(C && "Unknown OMPClause type")(static_cast<void> (0));
11971
11972 Visit(C);
11973 C->setLocStart(Record.readSourceLocation());
11974 C->setLocEnd(Record.readSourceLocation());
11975
11976 return C;
11977}
11978
11979void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
11980 C->setPreInitStmt(Record.readSubStmt(),
11981 static_cast<OpenMPDirectiveKind>(Record.readInt()));
11982}
11983
11984void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
11985 VisitOMPClauseWithPreInit(C);
11986 C->setPostUpdateExpr(Record.readSubExpr());
11987}
11988
11989void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
11990 VisitOMPClauseWithPreInit(C);
11991 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
11992 C->setNameModifierLoc(Record.readSourceLocation());
11993 C->setColonLoc(Record.readSourceLocation());
11994 C->setCondition(Record.readSubExpr());
11995 C->setLParenLoc(Record.readSourceLocation());
11996}
11997
11998void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
11999 VisitOMPClauseWithPreInit(C);
12000 C->setCondition(Record.readSubExpr());
12001 C->setLParenLoc(Record.readSourceLocation());
12002}
12003
12004void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12005 VisitOMPClauseWithPreInit(C);
12006 C->setNumThreads(Record.readSubExpr());
12007 C->setLParenLoc(Record.readSourceLocation());
12008}
12009
12010void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12011 C->setSafelen(Record.readSubExpr());
12012 C->setLParenLoc(Record.readSourceLocation());
12013}
12014
12015void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12016 C->setSimdlen(Record.readSubExpr());
12017 C->setLParenLoc(Record.readSourceLocation());
12018}
12019
12020void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) {
12021 for (Expr *&E : C->getSizesRefs())
12022 E = Record.readSubExpr();
12023 C->setLParenLoc(Record.readSourceLocation());
12024}
12025
12026void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {}
12027
12028void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) {
12029 C->setFactor(Record.readSubExpr());
12030 C->setLParenLoc(Record.readSourceLocation());
12031}
12032
12033void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
12034 C->setAllocator(Record.readExpr());
12035 C->setLParenLoc(Record.readSourceLocation());
12036}
12037
12038void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
12039 C->setNumForLoops(Record.readSubExpr());
12040 C->setLParenLoc(Record.readSourceLocation());
12041}
12042
12043void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
12044 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
12045 C->setLParenLoc(Record.readSourceLocation());
12046 C->setDefaultKindKwLoc(Record.readSourceLocation());
12047}
12048
12049void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
12050 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
12051 C->setLParenLoc(Record.readSourceLocation());
12052 C->setProcBindKindKwLoc(Record.readSourceLocation());
12053}
12054
12055void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12056 VisitOMPClauseWithPreInit(C);
12057 C->setScheduleKind(
12058 static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12059 C->setFirstScheduleModifier(
12060 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12061 C->setSecondScheduleModifier(
12062 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12063 C->setChunkSize(Record.readSubExpr());
12064 C->setLParenLoc(Record.readSourceLocation());
12065 C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12066 C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12067 C->setScheduleKindLoc(Record.readSourceLocation());
12068 C->setCommaLoc(Record.readSourceLocation());
12069}
12070
12071void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12072 C->setNumForLoops(Record.readSubExpr());
12073 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12074 C->setLoopNumIterations(I, Record.readSubExpr());
12075 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12076 C->setLoopCounter(I, Record.readSubExpr());
12077 C->setLParenLoc(Record.readSourceLocation());
12078}
12079
12080void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
12081 C->setEventHandler(Record.readSubExpr());
12082 C->setLParenLoc(Record.readSourceLocation());
12083}
12084
12085void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
12086
12087void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12088
12089void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12090
12091void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12092
12093void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12094
12095void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
12096 if (C->isExtended()) {
12097 C->setLParenLoc(Record.readSourceLocation());
12098 C->setArgumentLoc(Record.readSourceLocation());
12099 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
12100 }
12101}
12102
12103void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12104
12105void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12106
12107void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12108
12109void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12110
12111void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12112
12113void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12114
12115void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12116
12117void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12118
12119void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12120
12121void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) {
12122 unsigned NumVars = C->varlist_size();
12123 SmallVector<Expr *, 16> Vars;
12124 Vars.reserve(NumVars);
12125 for (unsigned I = 0; I != NumVars; ++I)
12126 Vars.push_back(Record.readSubExpr());
12127 C->setVarRefs(Vars);
12128 C->setIsTarget(Record.readBool());
12129 C->setIsTargetSync(Record.readBool());
12130 C->setLParenLoc(Record.readSourceLocation());
12131 C->setVarLoc(Record.readSourceLocation());
12132}
12133
12134void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) {
12135 C->setInteropVar(Record.readSubExpr());
12136 C->setLParenLoc(Record.readSourceLocation());
12137 C->setVarLoc(Record.readSourceLocation());
12138}
12139
12140void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) {
12141 C->setInteropVar(Record.readSubExpr());
12142 C->setLParenLoc(Record.readSourceLocation());
12143 C->setVarLoc(Record.readSourceLocation());
12144}
12145
12146void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
12147 VisitOMPClauseWithPreInit(C);
12148 C->setCondition(Record.readSubExpr());
12149 C->setLParenLoc(Record.readSourceLocation());
12150}
12151
12152void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) {
12153 VisitOMPClauseWithPreInit(C);
12154 C->setCondition(Record.readSubExpr());
12155 C->setLParenLoc(Record.readSourceLocation());
12156}
12157
12158void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12159
12160void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12161 OMPUnifiedSharedMemoryClause *) {}
12162
12163void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12164
12165void
12166OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12167}
12168
12169void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12170 OMPAtomicDefaultMemOrderClause *C) {
12171 C->setAtomicDefaultMemOrderKind(
12172 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12173 C->setLParenLoc(Record.readSourceLocation());
12174 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12175}
12176
12177void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12178 C->setLParenLoc(Record.readSourceLocation());
12179 unsigned NumVars = C->varlist_size();
12180 SmallVector<Expr *, 16> Vars;
12181 Vars.reserve(NumVars);
12182 for (unsigned i = 0; i != NumVars; ++i)
12183 Vars.push_back(Record.readSubExpr());
12184 C->setVarRefs(Vars);
12185 Vars.clear();
12186 for (unsigned i = 0; i != NumVars; ++i)
12187 Vars.push_back(Record.readSubExpr());
12188 C->setPrivateCopies(Vars);
12189}
12190
12191void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12192 VisitOMPClauseWithPreInit(C);
12193 C->setLParenLoc(Record.readSourceLocation());
12194 unsigned NumVars = C->varlist_size();
12195 SmallVector<Expr *, 16> Vars;
12196 Vars.reserve(NumVars);
12197 for (unsigned i = 0; i != NumVars; ++i)
12198 Vars.push_back(Record.readSubExpr());
12199 C->setVarRefs(Vars);
12200 Vars.clear();
12201 for (unsigned i = 0; i != NumVars; ++i)
12202 Vars.push_back(Record.readSubExpr());
12203 C->setPrivateCopies(Vars);
12204 Vars.clear();
12205 for (unsigned i = 0; i != NumVars; ++i)
12206 Vars.push_back(Record.readSubExpr());
12207 C->setInits(Vars);
12208}
12209
12210void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12211 VisitOMPClauseWithPostUpdate(C);
12212 C->setLParenLoc(Record.readSourceLocation());
12213 C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12214 C->setKindLoc(Record.readSourceLocation());
12215 C->setColonLoc(Record.readSourceLocation());
12216 unsigned NumVars = C->varlist_size();
12217 SmallVector<Expr *, 16> Vars;
12218 Vars.reserve(NumVars);
12219 for (unsigned i = 0; i != NumVars; ++i)
12220 Vars.push_back(Record.readSubExpr());
12221 C->setVarRefs(Vars);
12222 Vars.clear();
12223 for (unsigned i = 0; i != NumVars; ++i)
12224 Vars.push_back(Record.readSubExpr());
12225 C->setPrivateCopies(Vars);
12226 Vars.clear();
12227 for (unsigned i = 0; i != NumVars; ++i)
12228 Vars.push_back(Record.readSubExpr());
12229 C->setSourceExprs(Vars);
12230 Vars.clear();
12231 for (unsigned i = 0; i != NumVars; ++i)
12232 Vars.push_back(Record.readSubExpr());
12233 C->setDestinationExprs(Vars);
12234 Vars.clear();
12235 for (unsigned i = 0; i != NumVars; ++i)
12236 Vars.push_back(Record.readSubExpr());
12237 C->setAssignmentOps(Vars);
12238}
12239
12240void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12241 C->setLParenLoc(Record.readSourceLocation());
12242 unsigned NumVars = C->varlist_size();
12243 SmallVector<Expr *, 16> Vars;
12244 Vars.reserve(NumVars);
12245 for (unsigned i = 0; i != NumVars; ++i)
12246 Vars.push_back(Record.readSubExpr());
12247 C->setVarRefs(Vars);
12248}
12249
12250void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12251 VisitOMPClauseWithPostUpdate(C);
12252 C->setLParenLoc(Record.readSourceLocation());
12253 C->setModifierLoc(Record.readSourceLocation());
12254 C->setColonLoc(Record.readSourceLocation());
12255 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12256 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12257 C->setQualifierLoc(NNSL);
12258 C->setNameInfo(DNI);
12259
12260 unsigned NumVars = C->varlist_size();
12261 SmallVector<Expr *, 16> Vars;
12262 Vars.reserve(NumVars);
12263 for (unsigned i = 0; i != NumVars; ++i)
12264 Vars.push_back(Record.readSubExpr());
12265 C->setVarRefs(Vars);
12266 Vars.clear();
12267 for (unsigned i = 0; i != NumVars; ++i)
12268 Vars.push_back(Record.readSubExpr());
12269 C->setPrivates(Vars);
12270 Vars.clear();
12271 for (unsigned i = 0; i != NumVars; ++i)
12272 Vars.push_back(Record.readSubExpr());
12273 C->setLHSExprs(Vars);
12274 Vars.clear();
12275 for (unsigned i = 0; i != NumVars; ++i)
12276 Vars.push_back(Record.readSubExpr());
12277 C->setRHSExprs(Vars);
12278 Vars.clear();
12279 for (unsigned i = 0; i != NumVars; ++i)
12280 Vars.push_back(Record.readSubExpr());
12281 C->setReductionOps(Vars);
12282 if (C->getModifier() == OMPC_REDUCTION_inscan) {
12283 Vars.clear();
12284 for (unsigned i = 0; i != NumVars; ++i)
12285 Vars.push_back(Record.readSubExpr());
12286 C->setInscanCopyOps(Vars);
12287 Vars.clear();
12288 for (unsigned i = 0; i != NumVars; ++i)
12289 Vars.push_back(Record.readSubExpr());
12290 C->setInscanCopyArrayTemps(Vars);
12291 Vars.clear();
12292 for (unsigned i = 0; i != NumVars; ++i)
12293 Vars.push_back(Record.readSubExpr());
12294 C->setInscanCopyArrayElems(Vars);
12295 }
12296}
12297
12298void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12299 VisitOMPClauseWithPostUpdate(C);
12300 C->setLParenLoc(Record.readSourceLocation());
12301 C->setColonLoc(Record.readSourceLocation());
12302 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12303 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12304 C->setQualifierLoc(NNSL);
12305 C->setNameInfo(DNI);
12306
12307 unsigned NumVars = C->varlist_size();
12308 SmallVector<Expr *, 16> Vars;
12309 Vars.reserve(NumVars);
12310 for (unsigned I = 0; I != NumVars; ++I)
12311 Vars.push_back(Record.readSubExpr());
12312 C->setVarRefs(Vars);
12313 Vars.clear();
12314 for (unsigned I = 0; I != NumVars; ++I)
12315 Vars.push_back(Record.readSubExpr());
12316 C->setPrivates(Vars);
12317 Vars.clear();
12318 for (unsigned I = 0; I != NumVars; ++I)
12319 Vars.push_back(Record.readSubExpr());
12320 C->setLHSExprs(Vars);
12321 Vars.clear();
12322 for (unsigned I = 0; I != NumVars; ++I)
12323 Vars.push_back(Record.readSubExpr());
12324 C->setRHSExprs(Vars);
12325 Vars.clear();
12326 for (unsigned I = 0; I != NumVars; ++I)
12327 Vars.push_back(Record.readSubExpr());
12328 C->setReductionOps(Vars);
12329}
12330
12331void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12332 VisitOMPClauseWithPostUpdate(C);
12333 C->setLParenLoc(Record.readSourceLocation());
12334 C->setColonLoc(Record.readSourceLocation());
12335 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12336 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12337 C->setQualifierLoc(NNSL);
12338 C->setNameInfo(DNI);
12339
12340 unsigned NumVars = C->varlist_size();
12341 SmallVector<Expr *, 16> Vars;
12342 Vars.reserve(NumVars);
12343 for (unsigned I = 0; I != NumVars; ++I)
12344 Vars.push_back(Record.readSubExpr());
12345 C->setVarRefs(Vars);
12346 Vars.clear();
12347 for (unsigned I = 0; I != NumVars; ++I)
12348 Vars.push_back(Record.readSubExpr());
12349 C->setPrivates(Vars);
12350 Vars.clear();
12351 for (unsigned I = 0; I != NumVars; ++I)
12352 Vars.push_back(Record.readSubExpr());
12353 C->setLHSExprs(Vars);
12354 Vars.clear();
12355 for (unsigned I = 0; I != NumVars; ++I)
12356 Vars.push_back(Record.readSubExpr());
12357 C->setRHSExprs(Vars);
12358 Vars.clear();
12359 for (unsigned I = 0; I != NumVars; ++I)
12360 Vars.push_back(Record.readSubExpr());
12361 C->setReductionOps(Vars);
12362 Vars.clear();
12363 for (unsigned I = 0; I != NumVars; ++I)
12364 Vars.push_back(Record.readSubExpr());
12365 C->setTaskgroupDescriptors(Vars);
12366}
12367
12368void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12369 VisitOMPClauseWithPostUpdate(C);
12370 C->setLParenLoc(Record.readSourceLocation());
12371 C->setColonLoc(Record.readSourceLocation());
12372 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12373 C->setModifierLoc(Record.readSourceLocation());
12374 unsigned NumVars = C->varlist_size();
12375 SmallVector<Expr *, 16> Vars;
12376 Vars.reserve(NumVars);
12377 for (unsigned i = 0; i != NumVars; ++i)
12378 Vars.push_back(Record.readSubExpr());
12379 C->setVarRefs(Vars);
12380 Vars.clear();
12381 for (unsigned i = 0; i != NumVars; ++i)
12382 Vars.push_back(Record.readSubExpr());
12383 C->setPrivates(Vars);
12384 Vars.clear();
12385 for (unsigned i = 0; i != NumVars; ++i)
12386 Vars.push_back(Record.readSubExpr());
12387 C->setInits(Vars);
12388 Vars.clear();
12389 for (unsigned i = 0; i != NumVars; ++i)
12390 Vars.push_back(Record.readSubExpr());
12391 C->setUpdates(Vars);
12392 Vars.clear();
12393 for (unsigned i = 0; i != NumVars; ++i)
12394 Vars.push_back(Record.readSubExpr());
12395 C->setFinals(Vars);
12396 C->setStep(Record.readSubExpr());
12397 C->setCalcStep(Record.readSubExpr());
12398 Vars.clear();
12399 for (unsigned I = 0; I != NumVars + 1; ++I)
12400 Vars.push_back(Record.readSubExpr());
12401 C->setUsedExprs(Vars);
12402}
12403
12404void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12405 C->setLParenLoc(Record.readSourceLocation());
12406 C->setColonLoc(Record.readSourceLocation());
12407 unsigned NumVars = C->varlist_size();
12408 SmallVector<Expr *, 16> Vars;
12409 Vars.reserve(NumVars);
12410 for (unsigned i = 0; i != NumVars; ++i)
12411 Vars.push_back(Record.readSubExpr());
12412 C->setVarRefs(Vars);
12413 C->setAlignment(Record.readSubExpr());
12414}
12415
12416void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12417 C->setLParenLoc(Record.readSourceLocation());
12418 unsigned NumVars = C->varlist_size();
12419 SmallVector<Expr *, 16> Exprs;
12420 Exprs.reserve(NumVars);
12421 for (unsigned i = 0; i != NumVars; ++i)
12422 Exprs.push_back(Record.readSubExpr());
12423 C->setVarRefs(Exprs);
12424 Exprs.clear();
12425 for (unsigned i = 0; i != NumVars; ++i)
12426 Exprs.push_back(Record.readSubExpr());
12427 C->setSourceExprs(Exprs);
12428 Exprs.clear();
12429 for (unsigned i = 0; i != NumVars; ++i)
12430 Exprs.push_back(Record.readSubExpr());
12431 C->setDestinationExprs(Exprs);
12432 Exprs.clear();
12433 for (unsigned i = 0; i != NumVars; ++i)
12434 Exprs.push_back(Record.readSubExpr());
12435 C->setAssignmentOps(Exprs);
12436}
12437
12438void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12439 C->setLParenLoc(Record.readSourceLocation());
12440 unsigned NumVars = C->varlist_size();
12441 SmallVector<Expr *, 16> Exprs;
12442 Exprs.reserve(NumVars);
12443 for (unsigned i = 0; i != NumVars; ++i)
12444 Exprs.push_back(Record.readSubExpr());
12445 C->setVarRefs(Exprs);
12446 Exprs.clear();
12447 for (unsigned i = 0; i != NumVars; ++i)
12448 Exprs.push_back(Record.readSubExpr());
12449 C->setSourceExprs(Exprs);
12450 Exprs.clear();
12451 for (unsigned i = 0; i != NumVars; ++i)
12452 Exprs.push_back(Record.readSubExpr());
12453 C->setDestinationExprs(Exprs);
12454 Exprs.clear();
12455 for (unsigned i = 0; i != NumVars; ++i)
12456 Exprs.push_back(Record.readSubExpr());
12457 C->setAssignmentOps(Exprs);
12458}
12459
12460void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12461 C->setLParenLoc(Record.readSourceLocation());
12462 unsigned NumVars = C->varlist_size();
12463 SmallVector<Expr *, 16> Vars;
12464 Vars.reserve(NumVars);
12465 for (unsigned i = 0; i != NumVars; ++i)
12466 Vars.push_back(Record.readSubExpr());
12467 C->setVarRefs(Vars);
12468}
12469
12470void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12471 C->setDepobj(Record.readSubExpr());
12472 C->setLParenLoc(Record.readSourceLocation());
12473}
12474
12475void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12476 C->setLParenLoc(Record.readSourceLocation());
12477 C->setModifier(Record.readSubExpr());
12478 C->setDependencyKind(
12479 static_cast<OpenMPDependClauseKind>(Record.readInt()));
12480 C->setDependencyLoc(Record.readSourceLocation());
12481 C->setColonLoc(Record.readSourceLocation());
12482 unsigned NumVars = C->varlist_size();
12483 SmallVector<Expr *, 16> Vars;
12484 Vars.reserve(NumVars);
12485 for (unsigned I = 0; I != NumVars; ++I)
12486 Vars.push_back(Record.readSubExpr());
12487 C->setVarRefs(Vars);
12488 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12489 C->setLoopData(I, Record.readSubExpr());
12490}
12491
12492void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12493 VisitOMPClauseWithPreInit(C);
12494 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12495 C->setDevice(Record.readSubExpr());
12496 C->setModifierLoc(Record.readSourceLocation());
12497 C->setLParenLoc(Record.readSourceLocation());
12498}
12499
12500void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12501 C->setLParenLoc(Record.readSourceLocation());
12502 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12503 C->setMapTypeModifier(
12504 I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12505 C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12506 }
12507 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12508 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12509 C->setMapType(
12510 static_cast<OpenMPMapClauseKind>(Record.readInt()));
12511 C->setMapLoc(Record.readSourceLocation());
12512 C->setColonLoc(Record.readSourceLocation());
12513 auto NumVars = C->varlist_size();
12514 auto UniqueDecls = C->getUniqueDeclarationsNum();
12515 auto TotalLists = C->getTotalComponentListNum();
12516 auto TotalComponents = C->getTotalComponentsNum();
12517
12518 SmallVector<Expr *, 16> Vars;
12519 Vars.reserve(NumVars);
12520 for (unsigned i = 0; i != NumVars; ++i)
12521 Vars.push_back(Record.readExpr());
12522 C->setVarRefs(Vars);
12523
12524 SmallVector<Expr *, 16> UDMappers;
12525 UDMappers.reserve(NumVars);
12526 for (unsigned I = 0; I < NumVars; ++I)
12527 UDMappers.push_back(Record.readExpr());
12528 C->setUDMapperRefs(UDMappers);
12529
12530 SmallVector<ValueDecl *, 16> Decls;
12531 Decls.reserve(UniqueDecls);
12532 for (unsigned i = 0; i < UniqueDecls; ++i)
12533 Decls.push_back(Record.readDeclAs<ValueDecl>());
12534 C->setUniqueDecls(Decls);
12535
12536 SmallVector<unsigned, 16> ListsPerDecl;
12537 ListsPerDecl.reserve(UniqueDecls);
12538 for (unsigned i = 0; i < UniqueDecls; ++i)
12539 ListsPerDecl.push_back(Record.readInt());
12540 C->setDeclNumLists(ListsPerDecl);
12541
12542 SmallVector<unsigned, 32> ListSizes;
12543 ListSizes.reserve(TotalLists);
12544 for (unsigned i = 0; i < TotalLists; ++i)
12545 ListSizes.push_back(Record.readInt());
12546 C->setComponentListSizes(ListSizes);
12547
12548 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12549 Components.reserve(TotalComponents);
12550 for (unsigned i = 0; i < TotalComponents; ++i) {
12551 Expr *AssociatedExprPr = Record.readExpr();
12552 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12553 Components.emplace_back(AssociatedExprPr, AssociatedDecl,
12554 /*IsNonContiguous=*/false);
12555 }
12556 C->setComponents(Components, ListSizes);
12557}
12558
12559void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12560 C->setLParenLoc(Record.readSourceLocation());
12561 C->setColonLoc(Record.readSourceLocation());
12562 C->setAllocator(Record.readSubExpr());
12563 unsigned NumVars = C->varlist_size();
12564 SmallVector<Expr *, 16> Vars;
12565 Vars.reserve(NumVars);
12566 for (unsigned i = 0; i != NumVars; ++i)
12567 Vars.push_back(Record.readSubExpr());
12568 C->setVarRefs(Vars);
12569}
12570
12571void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12572 VisitOMPClauseWithPreInit(C);
12573 C->setNumTeams(Record.readSubExpr());
12574 C->setLParenLoc(Record.readSourceLocation());
12575}
12576
12577void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12578 VisitOMPClauseWithPreInit(C);
12579 C->setThreadLimit(Record.readSubExpr());
12580 C->setLParenLoc(Record.readSourceLocation());
12581}
12582
12583void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12584 VisitOMPClauseWithPreInit(C);
12585 C->setPriority(Record.readSubExpr());
12586 C->setLParenLoc(Record.readSourceLocation());
12587}
12588
12589void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12590 VisitOMPClauseWithPreInit(C);
12591 C->setGrainsize(Record.readSubExpr());
12592 C->setLParenLoc(Record.readSourceLocation());
12593}
12594
12595void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12596 VisitOMPClauseWithPreInit(C);
12597 C->setNumTasks(Record.readSubExpr());
12598 C->setLParenLoc(Record.readSourceLocation());
12599}
12600
12601void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12602 C->setHint(Record.readSubExpr());
12603 C->setLParenLoc(Record.readSourceLocation());
12604}
12605
12606void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12607 VisitOMPClauseWithPreInit(C);
12608 C->setDistScheduleKind(
12609 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12610 C->setChunkSize(Record.readSubExpr());
12611 C->setLParenLoc(Record.readSourceLocation());
12612 C->setDistScheduleKindLoc(Record.readSourceLocation());
12613 C->setCommaLoc(Record.readSourceLocation());
12614}
12615
12616void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12617 C->setDefaultmapKind(
12618 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12619 C->setDefaultmapModifier(
12620 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12621 C->setLParenLoc(Record.readSourceLocation());
12622 C->setDefaultmapModifierLoc(Record.readSourceLocation());
12623 C->setDefaultmapKindLoc(Record.readSourceLocation());
12624}
12625
12626void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12627 C->setLParenLoc(Record.readSourceLocation());
12628 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12629 C->setMotionModifier(
12630 I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12631 C->setMotionModifierLoc(I, Record.readSourceLocation());
12632 }
12633 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12634 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12635 C->setColonLoc(Record.readSourceLocation());
12636 auto NumVars = C->varlist_size();
12637 auto UniqueDecls = C->getUniqueDeclarationsNum();
12638 auto TotalLists = C->getTotalComponentListNum();
12639 auto TotalComponents = C->getTotalComponentsNum();
12640
12641 SmallVector<Expr *, 16> Vars;
12642 Vars.reserve(NumVars);
12643 for (unsigned i = 0; i != NumVars; ++i)
12644 Vars.push_back(Record.readSubExpr());
12645 C->setVarRefs(Vars);
12646
12647 SmallVector<Expr *, 16> UDMappers;
12648 UDMappers.reserve(NumVars);
12649 for (unsigned I = 0; I < NumVars; ++I)
12650 UDMappers.push_back(Record.readSubExpr());
12651 C->setUDMapperRefs(UDMappers);
12652
12653 SmallVector<ValueDecl *, 16> Decls;
12654 Decls.reserve(UniqueDecls);
12655 for (unsigned i = 0; i < UniqueDecls; ++i)
12656 Decls.push_back(Record.readDeclAs<ValueDecl>());
12657 C->setUniqueDecls(Decls);
12658
12659 SmallVector<unsigned, 16> ListsPerDecl;
12660 ListsPerDecl.reserve(UniqueDecls);
12661 for (unsigned i = 0; i < UniqueDecls; ++i)
12662 ListsPerDecl.push_back(Record.readInt());
12663 C->setDeclNumLists(ListsPerDecl);
12664
12665 SmallVector<unsigned, 32> ListSizes;
12666 ListSizes.reserve(TotalLists);
12667 for (unsigned i = 0; i < TotalLists; ++i)
12668 ListSizes.push_back(Record.readInt());
12669 C->setComponentListSizes(ListSizes);
12670
12671 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12672 Components.reserve(TotalComponents);
12673 for (unsigned i = 0; i < TotalComponents; ++i) {
12674 Expr *AssociatedExprPr = Record.readSubExpr();
12675 bool IsNonContiguous = Record.readBool();
12676 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12677 Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
12678 }
12679 C->setComponents(Components, ListSizes);
12680}
12681
12682void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12683 C->setLParenLoc(Record.readSourceLocation());
12684 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12685 C->setMotionModifier(
12686 I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12687 C->setMotionModifierLoc(I, Record.readSourceLocation());
12688 }
12689 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12690 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12691 C->setColonLoc(Record.readSourceLocation());
12692 auto NumVars = C->varlist_size();
12693 auto UniqueDecls = C->getUniqueDeclarationsNum();
12694 auto TotalLists = C->getTotalComponentListNum();
12695 auto TotalComponents = C->getTotalComponentsNum();
12696
12697 SmallVector<Expr *, 16> Vars;
12698 Vars.reserve(NumVars);
12699 for (unsigned i = 0; i != NumVars; ++i)
12700 Vars.push_back(Record.readSubExpr());
12701 C->setVarRefs(Vars);
12702
12703 SmallVector<Expr *, 16> UDMappers;
12704 UDMappers.reserve(NumVars);
12705 for (unsigned I = 0; I < NumVars; ++I)
12706 UDMappers.push_back(Record.readSubExpr());
12707 C->setUDMapperRefs(UDMappers);
12708
12709 SmallVector<ValueDecl *, 16> Decls;
12710 Decls.reserve(UniqueDecls);
12711 for (unsigned i = 0; i < UniqueDecls; ++i)
12712 Decls.push_back(Record.readDeclAs<ValueDecl>());
12713 C->setUniqueDecls(Decls);
12714
12715 SmallVector<unsigned, 16> ListsPerDecl;
12716 ListsPerDecl.reserve(UniqueDecls);
12717 for (unsigned i = 0; i < UniqueDecls; ++i)
12718 ListsPerDecl.push_back(Record.readInt());
12719 C->setDeclNumLists(ListsPerDecl);
12720
12721 SmallVector<unsigned, 32> ListSizes;
12722 ListSizes.reserve(TotalLists);
12723 for (unsigned i = 0; i < TotalLists; ++i)
12724 ListSizes.push_back(Record.readInt());
12725 C->setComponentListSizes(ListSizes);
12726
12727 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12728 Components.reserve(TotalComponents);
12729 for (unsigned i = 0; i < TotalComponents; ++i) {
12730 Expr *AssociatedExprPr = Record.readSubExpr();
12731 bool IsNonContiguous = Record.readBool();
12732 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12733 Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
12734 }
12735 C->setComponents(Components, ListSizes);
12736}
12737
12738void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12739 C->setLParenLoc(Record.readSourceLocation());
12740 auto NumVars = C->varlist_size();
12741 auto UniqueDecls = C->getUniqueDeclarationsNum();
12742 auto TotalLists = C->getTotalComponentListNum();
12743 auto TotalComponents = C->getTotalComponentsNum();
12744
12745 SmallVector<Expr *, 16> Vars;
12746 Vars.reserve(NumVars);
12747 for (unsigned i = 0; i != NumVars; ++i)
12748 Vars.push_back(Record.readSubExpr());
12749 C->setVarRefs(Vars);
12750 Vars.clear();
12751 for (unsigned i = 0; i != NumVars; ++i)
12752 Vars.push_back(Record.readSubExpr());
12753 C->setPrivateCopies(Vars);
12754 Vars.clear();
12755 for (unsigned i = 0; i != NumVars; ++i)
12756 Vars.push_back(Record.readSubExpr());
12757 C->setInits(Vars);
12758
12759 SmallVector<ValueDecl *, 16> Decls;
12760 Decls.reserve(UniqueDecls);
12761 for (unsigned i = 0; i < UniqueDecls; ++i)
12762 Decls.push_back(Record.readDeclAs<ValueDecl>());
12763 C->setUniqueDecls(Decls);
12764
12765 SmallVector<unsigned, 16> ListsPerDecl;
12766 ListsPerDecl.reserve(UniqueDecls);
12767 for (unsigned i = 0; i < UniqueDecls; ++i)
12768 ListsPerDecl.push_back(Record.readInt());
12769 C->setDeclNumLists(ListsPerDecl);
12770
12771 SmallVector<unsigned, 32> ListSizes;
12772 ListSizes.reserve(TotalLists);
12773 for (unsigned i = 0; i < TotalLists; ++i)
12774 ListSizes.push_back(Record.readInt());
12775 C->setComponentListSizes(ListSizes);
12776
12777 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12778 Components.reserve(TotalComponents);
12779 for (unsigned i = 0; i < TotalComponents; ++i) {
12780 auto *AssociatedExprPr = Record.readSubExpr();
12781 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12782 Components.emplace_back(AssociatedExprPr, AssociatedDecl,
12783 /*IsNonContiguous=*/false);
12784 }
12785 C->setComponents(Components, ListSizes);
12786}
12787
12788void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12789 C->setLParenLoc(Record.readSourceLocation());
12790 auto NumVars = C->varlist_size();
12791 auto UniqueDecls = C->getUniqueDeclarationsNum();
12792 auto TotalLists = C->getTotalComponentListNum();
12793 auto TotalComponents = C->getTotalComponentsNum();
12794
12795 SmallVector<Expr *, 16> Vars;
12796 Vars.reserve(NumVars);
12797 for (unsigned i = 0; i != NumVars; ++i)
12798 Vars.push_back(Record.readSubExpr());
12799 C->setVarRefs(Vars);
12800
12801 SmallVector<ValueDecl *, 16> Decls;
12802 Decls.reserve(UniqueDecls);
12803 for (unsigned i = 0; i < UniqueDecls; ++i)
12804 Decls.push_back(Record.readDeclAs<ValueDecl>());
12805 C->setUniqueDecls(Decls);
12806
12807 SmallVector<unsigned, 16> ListsPerDecl;
12808 ListsPerDecl.reserve(UniqueDecls);
12809 for (unsigned i = 0; i < UniqueDecls; ++i)
12810 ListsPerDecl.push_back(Record.readInt());
12811 C->setDeclNumLists(ListsPerDecl);
12812
12813 SmallVector<unsigned, 32> ListSizes;
12814 ListSizes.reserve(TotalLists);
12815 for (unsigned i = 0; i < TotalLists; ++i)
12816 ListSizes.push_back(Record.readInt());
12817 C->setComponentListSizes(ListSizes);
12818
12819 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12820 Components.reserve(TotalComponents);
12821 for (unsigned i = 0; i < TotalComponents; ++i) {
12822 Expr *AssociatedExpr = Record.readSubExpr();
12823 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12824 Components.emplace_back(AssociatedExpr, AssociatedDecl,
12825 /*IsNonContiguous*/ false);
12826 }
12827 C->setComponents(Components, ListSizes);
12828}
12829
12830void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12831 C->setLParenLoc(Record.readSourceLocation());
12832 auto NumVars = C->varlist_size();
12833 auto UniqueDecls = C->getUniqueDeclarationsNum();
12834 auto TotalLists = C->getTotalComponentListNum();
12835 auto TotalComponents = C->getTotalComponentsNum();
12836
12837 SmallVector<Expr *, 16> Vars;
12838 Vars.reserve(NumVars);
12839 for (unsigned i = 0; i != NumVars; ++i)
12840 Vars.push_back(Record.readSubExpr());
12841 C->setVarRefs(Vars);
12842 Vars.clear();
12843
12844 SmallVector<ValueDecl *, 16> Decls;
12845 Decls.reserve(UniqueDecls);
12846 for (unsigned i = 0; i < UniqueDecls; ++i)
12847 Decls.push_back(Record.readDeclAs<ValueDecl>());
12848 C->setUniqueDecls(Decls);
12849
12850 SmallVector<unsigned, 16> ListsPerDecl;
12851 ListsPerDecl.reserve(UniqueDecls);
12852 for (unsigned i = 0; i < UniqueDecls; ++i)
12853 ListsPerDecl.push_back(Record.readInt());
12854 C->setDeclNumLists(ListsPerDecl);
12855
12856 SmallVector<unsigned, 32> ListSizes;
12857 ListSizes.reserve(TotalLists);
12858 for (unsigned i = 0; i < TotalLists; ++i)
12859 ListSizes.push_back(Record.readInt());
12860 C->setComponentListSizes(ListSizes);
12861
12862 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12863 Components.reserve(TotalComponents);
12864 for (unsigned i = 0; i < TotalComponents; ++i) {
12865 Expr *AssociatedExpr = Record.readSubExpr();
12866 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12867 Components.emplace_back(AssociatedExpr, AssociatedDecl,
12868 /*IsNonContiguous=*/false);
12869 }
12870 C->setComponents(Components, ListSizes);
12871}
12872
12873void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12874 C->setLParenLoc(Record.readSourceLocation());
12875 unsigned NumVars = C->varlist_size();
12876 SmallVector<Expr *, 16> Vars;
12877 Vars.reserve(NumVars);
12878 for (unsigned i = 0; i != NumVars; ++i)
12879 Vars.push_back(Record.readSubExpr());
12880 C->setVarRefs(Vars);
12881 Vars.clear();
12882 Vars.reserve(NumVars);
12883 for (unsigned i = 0; i != NumVars; ++i)
12884 Vars.push_back(Record.readSubExpr());
12885 C->setPrivateRefs(Vars);
12886}
12887
12888void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12889 C->setLParenLoc(Record.readSourceLocation());
12890 unsigned NumVars = C->varlist_size();
12891 SmallVector<Expr *, 16> Vars;
12892 Vars.reserve(NumVars);
12893 for (unsigned i = 0; i != NumVars; ++i)
12894 Vars.push_back(Record.readSubExpr());
12895 C->setVarRefs(Vars);
12896}
12897
12898void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12899 C->setLParenLoc(Record.readSourceLocation());
12900 unsigned NumVars = C->varlist_size();
12901 SmallVector<Expr *, 16> Vars;
12902 Vars.reserve(NumVars);
12903 for (unsigned i = 0; i != NumVars; ++i)
12904 Vars.push_back(Record.readSubExpr());
12905 C->setVarRefs(Vars);
12906}
12907
12908void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
12909 C->setLParenLoc(Record.readSourceLocation());
12910 unsigned NumOfAllocators = C->getNumberOfAllocators();
12911 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
12912 Data.reserve(NumOfAllocators);
12913 for (unsigned I = 0; I != NumOfAllocators; ++I) {
12914 OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
12915 D.Allocator = Record.readSubExpr();
12916 D.AllocatorTraits = Record.readSubExpr();
12917 D.LParenLoc = Record.readSourceLocation();
12918 D.RParenLoc = Record.readSourceLocation();
12919 }
12920 C->setAllocatorsData(Data);
12921}
12922
12923void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
12924 C->setLParenLoc(Record.readSourceLocation());
12925 C->setModifier(Record.readSubExpr());
12926 C->setColonLoc(Record.readSourceLocation());
12927 unsigned NumOfLocators = C->varlist_size();
12928 SmallVector<Expr *, 4> Locators;
12929 Locators.reserve(NumOfLocators);
12930 for (unsigned I = 0; I != NumOfLocators; ++I)
12931 Locators.push_back(Record.readSubExpr());
12932 C->setVarRefs(Locators);
12933}
12934
12935void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12936 C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12937 C->setLParenLoc(Record.readSourceLocation());
12938 C->setKindKwLoc(Record.readSourceLocation());
12939}
12940
12941void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) {
12942 VisitOMPClauseWithPreInit(C);
12943 C->setThreadID(Record.readSubExpr());
12944 C->setLParenLoc(Record.readSourceLocation());
12945}
12946
12947OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
12948 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
12949 TI.Sets.resize(readUInt32());
12950 for (auto &Set : TI.Sets) {
12951 Set.Kind = readEnum<llvm::omp::TraitSet>();
12952 Set.Selectors.resize(readUInt32());
12953 for (auto &Selector : Set.Selectors) {
12954 Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12955 Selector.ScoreOrCondition = nullptr;
12956 if (readBool())
12957 Selector.ScoreOrCondition = readExprRef();
12958 Selector.Properties.resize(readUInt32());
12959 for (auto &Property : Selector.Properties)
12960 Property.Kind = readEnum<llvm::omp::TraitProperty>();
12961 }
12962 }
12963 return &TI;
12964}
12965
12966void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
12967 if (!Data)
12968 return;
12969 if (Reader->ReadingKind == ASTReader::Read_Stmt) {
12970 // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
12971 skipInts(3);
12972 }
12973 SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
12974 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
12975 Clauses[I] = readOMPClause();
12976 Data->setClauses(Clauses);
12977 if (Data->hasAssociatedStmt())
12978 Data->setAssociatedStmt(readStmt());
12979 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
12980 Data->getChildren()[I] = readStmt();
12981}

/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/clang/include/clang/AST/Type.h

1//===- Type.h - C Language Family Type Representation -----------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9/// \file
10/// C Language Family Type Representation
11///
12/// This file defines the clang::Type interface and subclasses, used to
13/// represent types for languages in the C family.
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_CLANG_AST_TYPE_H
18#define LLVM_CLANG_AST_TYPE_H
19
20#include "clang/AST/DependenceFlags.h"
21#include "clang/AST/NestedNameSpecifier.h"
22#include "clang/AST/TemplateName.h"
23#include "clang/Basic/AddressSpaces.h"
24#include "clang/Basic/AttrKinds.h"
25#include "clang/Basic/Diagnostic.h"
26#include "clang/Basic/ExceptionSpecificationType.h"
27#include "clang/Basic/LLVM.h"
28#include "clang/Basic/Linkage.h"
29#include "clang/Basic/PartialDiagnostic.h"
30#include "clang/Basic/SourceLocation.h"
31#include "clang/Basic/Specifiers.h"
32#include "clang/Basic/Visibility.h"
33#include "llvm/ADT/APInt.h"
34#include "llvm/ADT/APSInt.h"
35#include "llvm/ADT/ArrayRef.h"
36#include "llvm/ADT/FoldingSet.h"
37#include "llvm/ADT/None.h"
38#include "llvm/ADT/Optional.h"
39#include "llvm/ADT/PointerIntPair.h"
40#include "llvm/ADT/PointerUnion.h"
41#include "llvm/ADT/StringRef.h"
42#include "llvm/ADT/Twine.h"
43#include "llvm/ADT/iterator_range.h"
44#include "llvm/Support/Casting.h"
45#include "llvm/Support/Compiler.h"
46#include "llvm/Support/ErrorHandling.h"
47#include "llvm/Support/PointerLikeTypeTraits.h"
48#include "llvm/Support/TrailingObjects.h"
49#include "llvm/Support/type_traits.h"
50#include <cassert>
51#include <cstddef>
52#include <cstdint>
53#include <cstring>
54#include <string>
55#include <type_traits>
56#include <utility>
57
58namespace clang {
59
60class ExtQuals;
61class QualType;
62class ConceptDecl;
63class TagDecl;
64class TemplateParameterList;
65class Type;
66
67enum {
68 TypeAlignmentInBits = 4,
69 TypeAlignment = 1 << TypeAlignmentInBits
70};
71
72namespace serialization {
73 template <class T> class AbstractTypeReader;
74 template <class T> class AbstractTypeWriter;
75}
76
77} // namespace clang
78
79namespace llvm {
80
81 template <typename T>
82 struct PointerLikeTypeTraits;
83 template<>
84 struct PointerLikeTypeTraits< ::clang::Type*> {
85 static inline void *getAsVoidPointer(::clang::Type *P) { return P; }
86
87 static inline ::clang::Type *getFromVoidPointer(void *P) {
88 return static_cast< ::clang::Type*>(P);
89 }
90
91 static constexpr int NumLowBitsAvailable = clang::TypeAlignmentInBits;
92 };
93
94 template<>
95 struct PointerLikeTypeTraits< ::clang::ExtQuals*> {
96 static inline void *getAsVoidPointer(::clang::ExtQuals *P) { return P; }
97
98 static inline ::clang::ExtQuals *getFromVoidPointer(void *P) {
99 return static_cast< ::clang::ExtQuals*>(P);
100 }
101
102 static constexpr int NumLowBitsAvailable = clang::TypeAlignmentInBits;
103 };
104
105} // namespace llvm
106
107namespace clang {
108
109class ASTContext;
110template <typename> class CanQual;
111class CXXRecordDecl;
112class DeclContext;
113class EnumDecl;
114class Expr;
115class ExtQualsTypeCommonBase;
116class FunctionDecl;
117class IdentifierInfo;
118class NamedDecl;
119class ObjCInterfaceDecl;
120class ObjCProtocolDecl;
121class ObjCTypeParamDecl;
122struct PrintingPolicy;
123class RecordDecl;
124class Stmt;
125class TagDecl;
126class TemplateArgument;
127class TemplateArgumentListInfo;
128class TemplateArgumentLoc;
129class TemplateTypeParmDecl;
130class TypedefNameDecl;
131class UnresolvedUsingTypenameDecl;
132
133using CanQualType = CanQual<Type>;
134
135// Provide forward declarations for all of the *Type classes.
136#define TYPE(Class, Base) class Class##Type;
137#include "clang/AST/TypeNodes.inc"
138
139/// The collection of all-type qualifiers we support.
140/// Clang supports five independent qualifiers:
141/// * C99: const, volatile, and restrict
142/// * MS: __unaligned
143/// * Embedded C (TR18037): address spaces
144/// * Objective C: the GC attributes (none, weak, or strong)
145class Qualifiers {
146public:
147 enum TQ { // NOTE: These flags must be kept in sync with DeclSpec::TQ.
148 Const = 0x1,
149 Restrict = 0x2,
150 Volatile = 0x4,
151 CVRMask = Const | Volatile | Restrict
152 };
153
154 enum GC {
155 GCNone = 0,
156 Weak,
157 Strong
158 };
159
160 enum ObjCLifetime {
161 /// There is no lifetime qualification on this type.
162 OCL_None,
163
164 /// This object can be modified without requiring retains or
165 /// releases.
166 OCL_ExplicitNone,
167
168 /// Assigning into this object requires the old value to be
169 /// released and the new value to be retained. The timing of the
170 /// release of the old value is inexact: it may be moved to
171 /// immediately after the last known point where the value is
172 /// live.
173 OCL_Strong,
174
175 /// Reading or writing from this object requires a barrier call.
176 OCL_Weak,
177
178 /// Assigning into this object requires a lifetime extension.
179 OCL_Autoreleasing
180 };
181
182 enum {
183 /// The maximum supported address space number.
184 /// 23 bits should be enough for anyone.
185 MaxAddressSpace = 0x7fffffu,
186
187 /// The width of the "fast" qualifier mask.
188 FastWidth = 3,
189
190 /// The fast qualifier mask.
191 FastMask = (1 << FastWidth) - 1
192 };
193
194 /// Returns the common set of qualifiers while removing them from
195 /// the given sets.
196 static Qualifiers removeCommonQualifiers(Qualifiers &L, Qualifiers &R) {
197 // If both are only CVR-qualified, bit operations are sufficient.
198 if (!(L.Mask & ~CVRMask) && !(R.Mask & ~CVRMask)) {
199 Qualifiers Q;
200 Q.Mask = L.Mask & R.Mask;
201 L.Mask &= ~Q.Mask;
202 R.Mask &= ~Q.Mask;
203 return Q;
204 }
205
206 Qualifiers Q;
207 unsigned CommonCRV = L.getCVRQualifiers() & R.getCVRQualifiers();
208 Q.addCVRQualifiers(CommonCRV);
209 L.removeCVRQualifiers(CommonCRV);
210 R.removeCVRQualifiers(CommonCRV);
211
212 if (L.getObjCGCAttr() == R.getObjCGCAttr()) {
213 Q.setObjCGCAttr(L.getObjCGCAttr());
214 L.removeObjCGCAttr();
215 R.removeObjCGCAttr();
216 }
217
218 if (L.getObjCLifetime() == R.getObjCLifetime()) {
219 Q.setObjCLifetime(L.getObjCLifetime());
220 L.removeObjCLifetime();
221 R.removeObjCLifetime();
222 }
223
224 if (L.getAddressSpace() == R.getAddressSpace()) {
225 Q.setAddressSpace(L.getAddressSpace());
226 L.removeAddressSpace();
227 R.removeAddressSpace();
228 }
229 return Q;
230 }
231
232 static Qualifiers fromFastMask(unsigned Mask) {
233 Qualifiers Qs;
234 Qs.addFastQualifiers(Mask);
235 return Qs;
236 }
237
238 static Qualifiers fromCVRMask(unsigned CVR) {
239 Qualifiers Qs;
240 Qs.addCVRQualifiers(CVR);
241 return Qs;
242 }
243
244 static Qualifiers fromCVRUMask(unsigned CVRU) {
245 Qualifiers Qs;
246 Qs.addCVRUQualifiers(CVRU);
247 return Qs;
248 }
249
250 // Deserialize qualifiers from an opaque representation.
251 static Qualifiers fromOpaqueValue(unsigned opaque) {
252 Qualifiers Qs;
253 Qs.Mask = opaque;
254 return Qs;
255 }
256
257 // Serialize these qualifiers into an opaque representation.
258 unsigned getAsOpaqueValue() const {
259 return Mask;
260 }
261
262 bool hasConst() const { return Mask & Const; }
263 bool hasOnlyConst() const { return Mask == Const; }
264 void removeConst() { Mask &= ~Const; }
265 void addConst() { Mask |= Const; }
266
267 bool hasVolatile() const { return Mask & Volatile; }
268 bool hasOnlyVolatile() const { return Mask == Volatile; }
269 void removeVolatile() { Mask &= ~Volatile; }
270 void addVolatile() { Mask |= Volatile; }
271
272 bool hasRestrict() const { return Mask & Restrict; }
273 bool hasOnlyRestrict() const { return Mask == Restrict; }
274 void removeRestrict() { Mask &= ~Restrict; }
275 void addRestrict() { Mask |= Restrict; }
276
277 bool hasCVRQualifiers() const { return getCVRQualifiers(); }
278 unsigned getCVRQualifiers() const { return Mask & CVRMask; }
279 unsigned getCVRUQualifiers() const { return Mask & (CVRMask | UMask); }
280
281 void setCVRQualifiers(unsigned mask) {
282 assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits")(static_cast<void> (0));
283 Mask = (Mask & ~CVRMask) | mask;
284 }
285 void removeCVRQualifiers(unsigned mask) {
286 assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits")(static_cast<void> (0));
287 Mask &= ~mask;
288 }
289 void removeCVRQualifiers() {
290 removeCVRQualifiers(CVRMask);
291 }
292 void addCVRQualifiers(unsigned mask) {
293 assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits")(static_cast<void> (0));
294 Mask |= mask;
295 }
296 void addCVRUQualifiers(unsigned mask) {
297 assert(!(mask & ~CVRMask & ~UMask) && "bitmask contains non-CVRU bits")(static_cast<void> (0));
298 Mask |= mask;
299 }
300
301 bool hasUnaligned() const { return Mask & UMask; }
302 void setUnaligned(bool flag) {
303 Mask = (Mask & ~UMask) | (flag ? UMask : 0);
304 }
305 void removeUnaligned() { Mask &= ~UMask; }
306 void addUnaligned() { Mask |= UMask; }
307
308 bool hasObjCGCAttr() const { return Mask & GCAttrMask; }
309 GC getObjCGCAttr() const { return GC((Mask & GCAttrMask) >> GCAttrShift); }
310 void setObjCGCAttr(GC type) {
311 Mask = (Mask & ~GCAttrMask) | (type << GCAttrShift);
312 }
313 void removeObjCGCAttr() { setObjCGCAttr(GCNone); }
314 void addObjCGCAttr(GC type) {
315 assert(type)(static_cast<void> (0));
316 setObjCGCAttr(type);
317 }
318 Qualifiers withoutObjCGCAttr() const {
319 Qualifiers qs = *this;
320 qs.removeObjCGCAttr();
321 return qs;
322 }
323 Qualifiers withoutObjCLifetime() const {
324 Qualifiers qs = *this;
325 qs.removeObjCLifetime();
326 return qs;
327 }
328 Qualifiers withoutAddressSpace() const {
329 Qualifiers qs = *this;
330 qs.removeAddressSpace();
331 return qs;
332 }
333
334 bool hasObjCLifetime() const { return Mask & LifetimeMask; }
335 ObjCLifetime getObjCLifetime() const {
336 return ObjCLifetime((Mask & LifetimeMask) >> LifetimeShift);
337 }
338 void setObjCLifetime(ObjCLifetime type) {
339 Mask = (Mask & ~LifetimeMask) | (type << LifetimeShift);
340 }
341 void removeObjCLifetime() { setObjCLifetime(OCL_None); }
342 void addObjCLifetime(ObjCLifetime type) {
343 assert(type)(static_cast<void> (0));
344 assert(!hasObjCLifetime())(static_cast<void> (0));
345 Mask |= (type << LifetimeShift);
346 }
347
348 /// True if the lifetime is neither None or ExplicitNone.
349 bool hasNonTrivialObjCLifetime() const {
350 ObjCLifetime lifetime = getObjCLifetime();
351 return (lifetime > OCL_ExplicitNone);
352 }
353
354 /// True if the lifetime is either strong or weak.
355 bool hasStrongOrWeakObjCLifetime() const {
356 ObjCLifetime lifetime = getObjCLifetime();
357 return (lifetime == OCL_Strong || lifetime == OCL_Weak);
358 }
359
360 bool hasAddressSpace() const { return Mask & AddressSpaceMask; }
361 LangAS getAddressSpace() const {
362 return static_cast<LangAS>(Mask >> AddressSpaceShift);
363 }
364 bool hasTargetSpecificAddressSpace() const {
365 return isTargetAddressSpace(getAddressSpace());
366 }
367 /// Get the address space attribute value to be printed by diagnostics.
368 unsigned getAddressSpaceAttributePrintValue() const {
369 auto Addr = getAddressSpace();
370 // This function is not supposed to be used with language specific
371 // address spaces. If that happens, the diagnostic message should consider
372 // printing the QualType instead of the address space value.
373 assert(Addr == LangAS::Default || hasTargetSpecificAddressSpace())(static_cast<void> (0));
374 if (Addr != LangAS::Default)
375 return toTargetAddressSpace(Addr);
376 // TODO: The diagnostic messages where Addr may be 0 should be fixed
377 // since it cannot differentiate the situation where 0 denotes the default
378 // address space or user specified __attribute__((address_space(0))).
379 return 0;
380 }
381 void setAddressSpace(LangAS space) {
382 assert((unsigned)space <= MaxAddressSpace)(static_cast<void> (0));
383 Mask = (Mask & ~AddressSpaceMask)
384 | (((uint32_t) space) << AddressSpaceShift);
385 }
386 void removeAddressSpace() { setAddressSpace(LangAS::Default); }
387 void addAddressSpace(LangAS space) {
388 assert(space != LangAS::Default)(static_cast<void> (0));
389 setAddressSpace(space);
390 }
391
392 // Fast qualifiers are those that can be allocated directly
393 // on a QualType object.
394 bool hasFastQualifiers() const { return getFastQualifiers(); }
395 unsigned getFastQualifiers() const { return Mask & FastMask; }
396 void setFastQualifiers(unsigned mask) {
397 assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits")(static_cast<void> (0));
398 Mask = (Mask & ~FastMask) | mask;
399 }
400 void removeFastQualifiers(unsigned mask) {
401 assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits")(static_cast<void> (0));
402 Mask &= ~mask;
403 }
404 void removeFastQualifiers() {
405 removeFastQualifiers(FastMask);
406 }
407 void addFastQualifiers(unsigned mask) {
408 assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits")(static_cast<void> (0));
409 Mask |= mask;
410 }
411
412 /// Return true if the set contains any qualifiers which require an ExtQuals
413 /// node to be allocated.
414 bool hasNonFastQualifiers() const { return Mask & ~FastMask; }
415 Qualifiers getNonFastQualifiers() const {
416 Qualifiers Quals = *this;
417 Quals.setFastQualifiers(0);
418 return Quals;
419 }
420
421 /// Return true if the set contains any qualifiers.
422 bool hasQualifiers() const { return Mask; }
423 bool empty() const { return !Mask; }
424
425 /// Add the qualifiers from the given set to this set.
426 void addQualifiers(Qualifiers Q) {
427 // If the other set doesn't have any non-boolean qualifiers, just
428 // bit-or it in.
429 if (!(Q.Mask & ~CVRMask))
430 Mask |= Q.Mask;
431 else {
432 Mask |= (Q.Mask & CVRMask);
433 if (Q.hasAddressSpace())
434 addAddressSpace(Q.getAddressSpace());
435 if (Q.hasObjCGCAttr())
436 addObjCGCAttr(Q.getObjCGCAttr());
437 if (Q.hasObjCLifetime())
438 addObjCLifetime(Q.getObjCLifetime());
439 }
440 }
441
442 /// Remove the qualifiers from the given set from this set.
443 void removeQualifiers(Qualifiers Q) {
444 // If the other set doesn't have any non-boolean qualifiers, just
445 // bit-and the inverse in.
446 if (!(Q.Mask & ~CVRMask))
447 Mask &= ~Q.Mask;
448 else {
449 Mask &= ~(Q.Mask & CVRMask);
450 if (getObjCGCAttr() == Q.getObjCGCAttr())
451 removeObjCGCAttr();
452 if (getObjCLifetime() == Q.getObjCLifetime())
453 removeObjCLifetime();
454 if (getAddressSpace() == Q.getAddressSpace())
455 removeAddressSpace();
456 }
457 }
458
459 /// Add the qualifiers from the given set to this set, given that
460 /// they don't conflict.
461 void addConsistentQualifiers(Qualifiers qs) {
462 assert(getAddressSpace() == qs.getAddressSpace() ||(static_cast<void> (0))
463 !hasAddressSpace() || !qs.hasAddressSpace())(static_cast<void> (0));
464 assert(getObjCGCAttr() == qs.getObjCGCAttr() ||(static_cast<void> (0))
465 !hasObjCGCAttr() || !qs.hasObjCGCAttr())(static_cast<void> (0));
466 assert(getObjCLifetime() == qs.getObjCLifetime() ||(static_cast<void> (0))
467 !hasObjCLifetime() || !qs.hasObjCLifetime())(static_cast<void> (0));
468 Mask |= qs.Mask;
469 }
470
471 /// Returns true if address space A is equal to or a superset of B.
472 /// OpenCL v2.0 defines conversion rules (OpenCLC v2.0 s6.5.5) and notion of
473 /// overlapping address spaces.
474 /// CL1.1 or CL1.2:
475 /// every address space is a superset of itself.
476 /// CL2.0 adds:
477 /// __generic is a superset of any address space except for __constant.
478 static bool isAddressSpaceSupersetOf(LangAS A, LangAS B) {
479 // Address spaces must match exactly.
480 return A == B ||
481 // Otherwise in OpenCLC v2.0 s6.5.5: every address space except
482 // for __constant can be used as __generic.
483 (A == LangAS::opencl_generic && B != LangAS::opencl_constant) ||
484 // We also define global_device and global_host address spaces,
485 // to distinguish global pointers allocated on host from pointers
486 // allocated on device, which are a subset of __global.
487 (A == LangAS::opencl_global && (B == LangAS::opencl_global_device ||
488 B == LangAS::opencl_global_host)) ||
489 (A == LangAS::sycl_global && (B == LangAS::sycl_global_device ||
490 B == LangAS::sycl_global_host)) ||
491 // Consider pointer size address spaces to be equivalent to default.
492 ((isPtrSizeAddressSpace(A) || A == LangAS::Default) &&
493 (isPtrSizeAddressSpace(B) || B == LangAS::Default)) ||
494 // Default is a superset of SYCL address spaces.
495 (A == LangAS::Default &&
496 (B == LangAS::sycl_private || B == LangAS::sycl_local ||
497 B == LangAS::sycl_global || B == LangAS::sycl_global_device ||
498 B == LangAS::sycl_global_host)) ||
499 // In HIP device compilation, any cuda address space is allowed
500 // to implicitly cast into the default address space.
501 (A == LangAS::Default &&
502 (B == LangAS::cuda_constant || B == LangAS::cuda_device ||
503 B == LangAS::cuda_shared));
504 }
505
506 /// Returns true if the address space in these qualifiers is equal to or
507 /// a superset of the address space in the argument qualifiers.
508 bool isAddressSpaceSupersetOf(Qualifiers other) const {
509 return isAddressSpaceSupersetOf(getAddressSpace(), other.getAddressSpace());
510 }
511
512 /// Determines if these qualifiers compatibly include another set.
513 /// Generally this answers the question of whether an object with the other
514 /// qualifiers can be safely used as an object with these qualifiers.
515 bool compatiblyIncludes(Qualifiers other) const {
516 return isAddressSpaceSupersetOf(other) &&
517 // ObjC GC qualifiers can match, be added, or be removed, but can't
518 // be changed.
519 (getObjCGCAttr() == other.getObjCGCAttr() || !hasObjCGCAttr() ||
520 !other.hasObjCGCAttr()) &&
521 // ObjC lifetime qualifiers must match exactly.
522 getObjCLifetime() == other.getObjCLifetime() &&
523 // CVR qualifiers may subset.
524 (((Mask & CVRMask) | (other.Mask & CVRMask)) == (Mask & CVRMask)) &&
525 // U qualifier may superset.
526 (!other.hasUnaligned() || hasUnaligned());
527 }
528
529 /// Determines if these qualifiers compatibly include another set of
530 /// qualifiers from the narrow perspective of Objective-C ARC lifetime.
531 ///
532 /// One set of Objective-C lifetime qualifiers compatibly includes the other
533 /// if the lifetime qualifiers match, or if both are non-__weak and the
534 /// including set also contains the 'const' qualifier, or both are non-__weak
535 /// and one is None (which can only happen in non-ARC modes).
536 bool compatiblyIncludesObjCLifetime(Qualifiers other) const {
537 if (getObjCLifetime() == other.getObjCLifetime())
538 return true;
539
540 if (getObjCLifetime() == OCL_Weak || other.getObjCLifetime() == OCL_Weak)
541 return false;
542
543 if (getObjCLifetime() == OCL_None || other.getObjCLifetime() == OCL_None)
544 return true;
545
546 return hasConst();
547 }
548
549 /// Determine whether this set of qualifiers is a strict superset of
550 /// another set of qualifiers, not considering qualifier compatibility.
551 bool isStrictSupersetOf(Qualifiers Other) const;
552
553 bool operator==(Qualifiers Other) const { return Mask == Other.Mask; }
554 bool operator!=(Qualifiers Other) const { return Mask != Other.Mask; }
555
556 explicit operator bool() const { return hasQualifiers(); }
557
558 Qualifiers &operator+=(Qualifiers R) {
559 addQualifiers(R);
560 return *this;
561 }
562
563 // Union two qualifier sets. If an enumerated qualifier appears
564 // in both sets, use the one from the right.
565 friend Qualifiers operator+(Qualifiers L, Qualifiers R) {
566 L += R;
567 return L;
568 }
569
570 Qualifiers &operator-=(Qualifiers R) {
571 removeQualifiers(R);
572 return *this;
573 }
574
575 /// Compute the difference between two qualifier sets.
576 friend Qualifiers operator-(Qualifiers L, Qualifiers R) {
577 L -= R;
578 return L;
579 }
580
581 std::string getAsString() const;
582 std::string getAsString(const PrintingPolicy &Policy) const;
583
584 static std::string getAddrSpaceAsString(LangAS AS);
585
586 bool isEmptyWhenPrinted(const PrintingPolicy &Policy) const;
587 void print(raw_ostream &OS, const PrintingPolicy &Policy,
588 bool appendSpaceIfNonEmpty = false) const;
589
590 void Profile(llvm::FoldingSetNodeID &ID) const {
591 ID.AddInteger(Mask);
592 }
593
594private:
595 // bits: |0 1 2|3|4 .. 5|6 .. 8|9 ... 31|
596 // |C R V|U|GCAttr|Lifetime|AddressSpace|
597 uint32_t Mask = 0;
598
599 static const uint32_t UMask = 0x8;
600 static const uint32_t UShift = 3;
601 static const uint32_t GCAttrMask = 0x30;
602 static const uint32_t GCAttrShift = 4;
603 static const uint32_t LifetimeMask = 0x1C0;
604 static const uint32_t LifetimeShift = 6;
605 static const uint32_t AddressSpaceMask =
606 ~(CVRMask | UMask | GCAttrMask | LifetimeMask);
607 static const uint32_t AddressSpaceShift = 9;
608};
609
610/// A std::pair-like structure for storing a qualified type split
611/// into its local qualifiers and its locally-unqualified type.
612struct SplitQualType {
613 /// The locally-unqualified type.
614 const Type *Ty = nullptr;
615
616 /// The local qualifiers.
617 Qualifiers Quals;
618
619 SplitQualType() = default;
620 SplitQualType(const Type *ty, Qualifiers qs) : Ty(ty), Quals(qs) {}
621
622 SplitQualType getSingleStepDesugaredType() const; // end of this file
623
624 // Make std::tie work.
625 std::pair<const Type *,Qualifiers> asPair() const {
626 return std::pair<const Type *, Qualifiers>(Ty, Quals);
627 }
628
629 friend bool operator==(SplitQualType a, SplitQualType b) {
630 return a.Ty == b.Ty && a.Quals == b.Quals;
631 }
632 friend bool operator!=(SplitQualType a, SplitQualType b) {
633 return a.Ty != b.Ty || a.Quals != b.Quals;
634 }
635};
636
637/// The kind of type we are substituting Objective-C type arguments into.
638///
639/// The kind of substitution affects the replacement of type parameters when
640/// no concrete type information is provided, e.g., when dealing with an
641/// unspecialized type.
642enum class ObjCSubstitutionContext {
643 /// An ordinary type.
644 Ordinary,
645
646 /// The result type of a method or function.
647 Result,
648
649 /// The parameter type of a method or function.
650 Parameter,
651
652 /// The type of a property.
653 Property,
654
655 /// The superclass of a type.
656 Superclass,
657};
658
659/// A (possibly-)qualified type.
660///
661/// For efficiency, we don't store CV-qualified types as nodes on their
662/// own: instead each reference to a type stores the qualifiers. This
663/// greatly reduces the number of nodes we need to allocate for types (for
664/// example we only need one for 'int', 'const int', 'volatile int',
665/// 'const volatile int', etc).
666///
667/// As an added efficiency bonus, instead of making this a pair, we
668/// just store the two bits we care about in the low bits of the
669/// pointer. To handle the packing/unpacking, we make QualType be a
670/// simple wrapper class that acts like a smart pointer. A third bit
671/// indicates whether there are extended qualifiers present, in which
672/// case the pointer points to a special structure.
673class QualType {
674 friend class QualifierCollector;
675
676 // Thankfully, these are efficiently composable.
677 llvm::PointerIntPair<llvm::PointerUnion<const Type *, const ExtQuals *>,
678 Qualifiers::FastWidth> Value;
679
680 const ExtQuals *getExtQualsUnsafe() const {
681 return Value.getPointer().get<const ExtQuals*>();
682 }
683
684 const Type *getTypePtrUnsafe() const {
685 return Value.getPointer().get<const Type*>();
686 }
687
688 const ExtQualsTypeCommonBase *getCommonPtr() const {
689 assert(!isNull() && "Cannot retrieve a NULL type pointer")(static_cast<void> (0));
690 auto CommonPtrVal = reinterpret_cast<uintptr_t>(Value.getOpaqueValue());
691 CommonPtrVal &= ~(uintptr_t)((1 << TypeAlignmentInBits) - 1);
692 return reinterpret_cast<ExtQualsTypeCommonBase*>(CommonPtrVal);
693 }
694
695public:
696 QualType() = default;
697 QualType(const Type *Ptr, unsigned Quals) : Value(Ptr, Quals) {}
698 QualType(const ExtQuals *Ptr, unsigned Quals) : Value(Ptr, Quals) {}
699
700 unsigned getLocalFastQualifiers() const { return Value.getInt(); }
701 void setLocalFastQualifiers(unsigned Quals) { Value.setInt(Quals); }
702
703 /// Retrieves a pointer to the underlying (unqualified) type.
704 ///
705 /// This function requires that the type not be NULL. If the type might be
706 /// NULL, use the (slightly less efficient) \c getTypePtrOrNull().
707 const Type *getTypePtr() const;
708
709 const Type *getTypePtrOrNull() const;
710
711 /// Retrieves a pointer to the name of the base type.
712 const IdentifierInfo *getBaseTypeIdentifier() const;
713
714 /// Divides a QualType into its unqualified type and a set of local
715 /// qualifiers.
716 SplitQualType split() const;
717
718 void *getAsOpaquePtr() const { return Value.getOpaqueValue(); }
719
720 static QualType getFromOpaquePtr(const void *Ptr) {
721 QualType T;
722 T.Value.setFromOpaqueValue(const_cast<void*>(Ptr));
723 return T;
724 }
725
726 const Type &operator*() const {
727 return *getTypePtr();
728 }
729
730 const Type *operator->() const {
731 return getTypePtr();
732 }
733
734 bool isCanonical() const;
735 bool isCanonicalAsParam() const;
736
737 /// Return true if this QualType doesn't point to a type yet.
738 bool isNull() const {
739 return Value.getPointer().isNull();
29
Calling 'PointerUnion::isNull'
32
Returning from 'PointerUnion::isNull'
33
Returning zero, which participates in a condition later
740 }
741
742 /// Determine whether this particular QualType instance has the
743 /// "const" qualifier set, without looking through typedefs that may have
744 /// added "const" at a different level.
745 bool isLocalConstQualified() const {
746 return (getLocalFastQualifiers() & Qualifiers::Const);
747 }
748
749 /// Determine whether this type is const-qualified.
750 bool isConstQualified() const;
751
752 /// Determine whether this particular QualType instance has the
753 /// "restrict" qualifier set, without looking through typedefs that may have
754 /// added "restrict" at a different level.
755 bool isLocalRestrictQualified() const {
756 return (getLocalFastQualifiers() & Qualifiers::Restrict);
757 }
758
759 /// Determine whether this type is restrict-qualified.
760 bool isRestrictQualified() const;
761
762 /// Determine whether this particular QualType instance has the
763 /// "volatile" qualifier set, without looking through typedefs that may have
764 /// added "volatile" at a different level.
765 bool isLocalVolatileQualified() const {
766 return (getLocalFastQualifiers() & Qualifiers::Volatile);
767 }
768
769 /// Determine whether this type is volatile-qualified.
770 bool isVolatileQualified() const;
771
772 /// Determine whether this particular QualType instance has any
773 /// qualifiers, without looking through any typedefs that might add
774 /// qualifiers at a different level.
775 bool hasLocalQualifiers() const {
776 return getLocalFastQualifiers() || hasLocalNonFastQualifiers();
777 }
778
779 /// Determine whether this type has any qualifiers.
780 bool hasQualifiers() const;
781
782 /// Determine whether this particular QualType instance has any
783 /// "non-fast" qualifiers, e.g., those that are stored in an ExtQualType
784 /// instance.
785 bool hasLocalNonFastQualifiers() const {
786 return Value.getPointer().is<const ExtQuals*>();
787 }
788
789 /// Retrieve the set of qualifiers local to this particular QualType
790 /// instance, not including any qualifiers acquired through typedefs or
791 /// other sugar.
792 Qualifiers getLocalQualifiers() const;
793
794 /// Retrieve the set of qualifiers applied to this type.
795 Qualifiers getQualifiers() const;
796
797 /// Retrieve the set of CVR (const-volatile-restrict) qualifiers
798 /// local to this particular QualType instance, not including any qualifiers
799 /// acquired through typedefs or other sugar.
800 unsigned getLocalCVRQualifiers() const {
801 return getLocalFastQualifiers();
802 }
803
804 /// Retrieve the set of CVR (const-volatile-restrict) qualifiers
805 /// applied to this type.
806 unsigned getCVRQualifiers() const;
807
808 bool isConstant(const ASTContext& Ctx) const {
809 return QualType::isConstant(*this, Ctx);
810 }
811
812 /// Determine whether this is a Plain Old Data (POD) type (C++ 3.9p10).
813 bool isPODType(const ASTContext &Context) const;
814
815 /// Return true if this is a POD type according to the rules of the C++98
816 /// standard, regardless of the current compilation's language.
817 bool isCXX98PODType(const ASTContext &Context) const;
818
819 /// Return true if this is a POD type according to the more relaxed rules
820 /// of the C++11 standard, regardless of the current compilation's language.
821 /// (C++0x [basic.types]p9). Note that, unlike
822 /// CXXRecordDecl::isCXX11StandardLayout, this takes DRs into account.
823 bool isCXX11PODType(const ASTContext &Context) const;
824
825 /// Return true if this is a trivial type per (C++0x [basic.types]p9)
826 bool isTrivialType(const ASTContext &Context) const;
827
828 /// Return true if this is a trivially copyable type (C++0x [basic.types]p9)
829 bool isTriviallyCopyableType(const ASTContext &Context) const;
830
831
832 /// Returns true if it is a class and it might be dynamic.
833 bool mayBeDynamicClass() const;
834
835 /// Returns true if it is not a class or if the class might not be dynamic.
836 bool mayBeNotDynamicClass() const;
837
838 // Don't promise in the API that anything besides 'const' can be
839 // easily added.
840
841 /// Add the `const` type qualifier to this QualType.
842 void addConst() {
843 addFastQualifiers(Qualifiers::Const);
844 }
845 QualType withConst() const {
846 return withFastQualifiers(Qualifiers::Const);
847 }
848
849 /// Add the `volatile` type qualifier to this QualType.
850 void addVolatile() {
851 addFastQualifiers(Qualifiers::Volatile);
852 }
853 QualType withVolatile() const {
854 return withFastQualifiers(Qualifiers::Volatile);
855 }
856
857 /// Add the `restrict` qualifier to this QualType.
858 void addRestrict() {
859 addFastQualifiers(Qualifiers::Restrict);
860 }
861 QualType withRestrict() const {
862 return withFastQualifiers(Qualifiers::Restrict);
863 }
864
865 QualType withCVRQualifiers(unsigned CVR) const {
866 return withFastQualifiers(CVR);
867 }
868
869 void addFastQualifiers(unsigned TQs) {
870 assert(!(TQs & ~Qualifiers::FastMask)(static_cast<void> (0))
871 && "non-fast qualifier bits set in mask!")(static_cast<void> (0));
872 Value.setInt(Value.getInt() | TQs);
873 }
874
875 void removeLocalConst();
876 void removeLocalVolatile();
877 void removeLocalRestrict();
878 void removeLocalCVRQualifiers(unsigned Mask);
879
880 void removeLocalFastQualifiers() { Value.setInt(0); }
881 void removeLocalFastQualifiers(unsigned Mask) {
882 assert(!(Mask & ~Qualifiers::FastMask) && "mask has non-fast qualifiers")(static_cast<void> (0));
883 Value.setInt(Value.getInt() & ~Mask);
884 }
885
886 // Creates a type with the given qualifiers in addition to any
887 // qualifiers already on this type.
888 QualType withFastQualifiers(unsigned TQs) const {
889 QualType T = *this;
890 T.addFastQualifiers(TQs);
891 return T;
892 }
893
894 // Creates a type with exactly the given fast qualifiers, removing
895 // any existing fast qualifiers.
896 QualType withExactLocalFastQualifiers(unsigned TQs) const {
897 return withoutLocalFastQualifiers().withFastQualifiers(TQs);
898 }
899
900 // Removes fast qualifiers, but leaves any extended qualifiers in place.
901 QualType withoutLocalFastQualifiers() const {
902 QualType T = *this;
903 T.removeLocalFastQualifiers();
904 return T;
905 }
906
907 QualType getCanonicalType() const;
908
909 /// Return this type with all of the instance-specific qualifiers
910 /// removed, but without removing any qualifiers that may have been applied
911 /// through typedefs.
912 QualType getLocalUnqualifiedType() const { return QualType(getTypePtr(), 0); }
913
914 /// Retrieve the unqualified variant of the given type,
915 /// removing as little sugar as possible.
916 ///
917 /// This routine looks through various kinds of sugar to find the
918 /// least-desugared type that is unqualified. For example, given:
919 ///
920 /// \code
921 /// typedef int Integer;
922 /// typedef const Integer CInteger;
923 /// typedef CInteger DifferenceType;
924 /// \endcode
925 ///
926 /// Executing \c getUnqualifiedType() on the type \c DifferenceType will
927 /// desugar until we hit the type \c Integer, which has no qualifiers on it.
928 ///
929 /// The resulting type might still be qualified if it's sugar for an array
930 /// type. To strip qualifiers even from within a sugared array type, use
931 /// ASTContext::getUnqualifiedArrayType.
932 inline QualType getUnqualifiedType() const;
933
934 /// Retrieve the unqualified variant of the given type, removing as little
935 /// sugar as possible.
936 ///
937 /// Like getUnqualifiedType(), but also returns the set of
938 /// qualifiers that were built up.
939 ///
940 /// The resulting type might still be qualified if it's sugar for an array
941 /// type. To strip qualifiers even from within a sugared array type, use
942 /// ASTContext::getUnqualifiedArrayType.
943 inline SplitQualType getSplitUnqualifiedType() const;
944
945 /// Determine whether this type is more qualified than the other
946 /// given type, requiring exact equality for non-CVR qualifiers.
947 bool isMoreQualifiedThan(QualType Other) const;
948
949 /// Determine whether this type is at least as qualified as the other
950 /// given type, requiring exact equality for non-CVR qualifiers.
951 bool isAtLeastAsQualifiedAs(QualType Other) const;
952
953 QualType getNonReferenceType() const;
954
955 /// Determine the type of a (typically non-lvalue) expression with the
956 /// specified result type.
957 ///
958 /// This routine should be used for expressions for which the return type is
959 /// explicitly specified (e.g., in a cast or call) and isn't necessarily
960 /// an lvalue. It removes a top-level reference (since there are no
961 /// expressions of reference type) and deletes top-level cvr-qualifiers
962 /// from non-class types (in C++) or all types (in C).
963 QualType getNonLValueExprType(const ASTContext &Context) const;
964
965 /// Remove an outer pack expansion type (if any) from this type. Used as part
966 /// of converting the type of a declaration to the type of an expression that
967 /// references that expression. It's meaningless for an expression to have a
968 /// pack expansion type.
969 QualType getNonPackExpansionType() const;
970
971 /// Return the specified type with any "sugar" removed from
972 /// the type. This takes off typedefs, typeof's etc. If the outer level of
973 /// the type is already concrete, it returns it unmodified. This is similar
974 /// to getting the canonical type, but it doesn't remove *all* typedefs. For
975 /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
976 /// concrete.
977 ///
978 /// Qualifiers are left in place.
979 QualType getDesugaredType(const ASTContext &Context) const {
980 return getDesugaredType(*this, Context);
981 }
982
983 SplitQualType getSplitDesugaredType() const {
984 return getSplitDesugaredType(*this);
985 }
986
987 /// Return the specified type with one level of "sugar" removed from
988 /// the type.
989 ///
990 /// This routine takes off the first typedef, typeof, etc. If the outer level
991 /// of the type is already concrete, it returns it unmodified.
992 QualType getSingleStepDesugaredType(const ASTContext &Context) const {
993 return getSingleStepDesugaredTypeImpl(*this, Context);
994 }
995
996 /// Returns the specified type after dropping any
997 /// outer-level parentheses.
998 QualType IgnoreParens() const {
999 if (isa<ParenType>(*this))
1000 return QualType::IgnoreParens(*this);
1001 return *this;
1002 }
1003
1004 /// Indicate whether the specified types and qualifiers are identical.
1005 friend bool operator==(const QualType &LHS, const QualType &RHS) {
1006 return LHS.Value == RHS.Value;
1007 }
1008 friend bool operator!=(const QualType &LHS, const QualType &RHS) {
1009 return LHS.Value != RHS.Value;
1010 }
1011 friend bool operator<(const QualType &LHS, const QualType &RHS) {
1012 return LHS.Value < RHS.Value;
1013 }
1014
1015 static std::string getAsString(SplitQualType split,
1016 const PrintingPolicy &Policy) {
1017 return getAsString(split.Ty, split.Quals, Policy);
1018 }
1019 static std::string getAsString(const Type *ty, Qualifiers qs,
1020 const PrintingPolicy &Policy);
1021
1022 std::string getAsString() const;
1023 std::string getAsString(const PrintingPolicy &Policy) const;
1024
1025 void print(raw_ostream &OS, const PrintingPolicy &Policy,
1026 const Twine &PlaceHolder = Twine(),
1027 unsigned Indentation = 0) const;
1028
1029 static void print(SplitQualType split, raw_ostream &OS,
1030 const PrintingPolicy &policy, const Twine &PlaceHolder,
1031 unsigned Indentation = 0) {
1032 return print(split.Ty, split.Quals, OS, policy, PlaceHolder, Indentation);
1033 }
1034
1035 static void print(const Type *ty, Qualifiers qs,
1036 raw_ostream &OS, const PrintingPolicy &policy,
1037 const Twine &PlaceHolder,
1038 unsigned Indentation = 0);
1039
1040 void getAsStringInternal(std::string &Str,
1041 const PrintingPolicy &Policy) const;
1042
1043 static void getAsStringInternal(SplitQualType split, std::string &out,
1044 const PrintingPolicy &policy) {
1045 return getAsStringInternal(split.Ty, split.Quals, out, policy);
1046 }
1047
1048 static void getAsStringInternal(const Type *ty, Qualifiers qs,
1049 std::string &out,
1050 const PrintingPolicy &policy);
1051
1052 class StreamedQualTypeHelper {
1053 const QualType &T;
1054 const PrintingPolicy &Policy;
1055 const Twine &PlaceHolder;
1056 unsigned Indentation;
1057
1058 public:
1059 StreamedQualTypeHelper(const QualType &T, const PrintingPolicy &Policy,
1060 const Twine &PlaceHolder, unsigned Indentation)
1061 : T(T), Policy(Policy), PlaceHolder(PlaceHolder),
1062 Indentation(Indentation) {}
1063
1064 friend raw_ostream &operator<<(raw_ostream &OS,
1065 const StreamedQualTypeHelper &SQT) {
1066 SQT.T.print(OS, SQT.Policy, SQT.PlaceHolder, SQT.Indentation);
1067 return OS;
1068 }
1069 };
1070
1071 StreamedQualTypeHelper stream(const PrintingPolicy &Policy,
1072 const Twine &PlaceHolder = Twine(),
1073 unsigned Indentation = 0) const {
1074 return StreamedQualTypeHelper(*this, Policy, PlaceHolder, Indentation);
1075 }
1076
1077 void dump(const char *s) const;
1078 void dump() const;
1079 void dump(llvm::raw_ostream &OS, const ASTContext &Context) const;
1080
1081 void Profile(llvm::FoldingSetNodeID &ID) const {
1082 ID.AddPointer(getAsOpaquePtr());
1083 }
1084
1085 /// Check if this type has any address space qualifier.
1086 inline bool hasAddressSpace() const;
1087
1088 /// Return the address space of this type.
1089 inline LangAS getAddressSpace() const;
1090
1091 /// Returns true if address space qualifiers overlap with T address space
1092 /// qualifiers.
1093 /// OpenCL C defines conversion rules for pointers to different address spaces
1094 /// and notion of overlapping address spaces.
1095 /// CL1.1 or CL1.2:
1096 /// address spaces overlap iff they are they same.
1097 /// OpenCL C v2.0 s6.5.5 adds:
1098 /// __generic overlaps with any address space except for __constant.
1099 bool isAddressSpaceOverlapping(QualType T) const {
1100 Qualifiers Q = getQualifiers();
1101 Qualifiers TQ = T.getQualifiers();
1102 // Address spaces overlap if at least one of them is a superset of another
1103 return Q.isAddressSpaceSupersetOf(TQ) || TQ.isAddressSpaceSupersetOf(Q);
1104 }
1105
1106 /// Returns gc attribute of this type.
1107 inline Qualifiers::GC getObjCGCAttr() const;
1108
1109 /// true when Type is objc's weak.
1110 bool isObjCGCWeak() const {
1111 return getObjCGCAttr() == Qualifiers::Weak;
1112 }
1113
1114 /// true when Type is objc's strong.
1115 bool isObjCGCStrong() const {
1116 return getObjCGCAttr() == Qualifiers::Strong;
1117 }
1118
1119 /// Returns lifetime attribute of this type.
1120 Qualifiers::ObjCLifetime getObjCLifetime() const {
1121 return getQualifiers().getObjCLifetime();
1122 }
1123
1124 bool hasNonTrivialObjCLifetime() const {
1125 return getQualifiers().hasNonTrivialObjCLifetime();
1126 }
1127
1128 bool hasStrongOrWeakObjCLifetime() const {
1129 return getQualifiers().hasStrongOrWeakObjCLifetime();
1130 }
1131
1132 // true when Type is objc's weak and weak is enabled but ARC isn't.
1133 bool isNonWeakInMRRWithObjCWeak(const ASTContext &Context) const;
1134
1135 enum PrimitiveDefaultInitializeKind {
1136 /// The type does not fall into any of the following categories. Note that
1137 /// this case is zero-valued so that values of this enum can be used as a
1138 /// boolean condition for non-triviality.
1139 PDIK_Trivial,
1140
1141 /// The type is an Objective-C retainable pointer type that is qualified
1142 /// with the ARC __strong qualifier.
1143 PDIK_ARCStrong,
1144
1145 /// The type is an Objective-C retainable pointer type that is qualified
1146 /// with the ARC __weak qualifier.
1147 PDIK_ARCWeak,
1148
1149 /// The type is a struct containing a field whose type is not PCK_Trivial.
1150 PDIK_Struct
1151 };
1152
1153 /// Functions to query basic properties of non-trivial C struct types.
1154
1155 /// Check if this is a non-trivial type that would cause a C struct
1156 /// transitively containing this type to be non-trivial to default initialize
1157 /// and return the kind.
1158 PrimitiveDefaultInitializeKind
1159 isNonTrivialToPrimitiveDefaultInitialize() const;
1160
1161 enum PrimitiveCopyKind {
1162 /// The type does not fall into any of the following categories. Note that
1163 /// this case is zero-valued so that values of this enum can be used as a
1164 /// boolean condition for non-triviality.
1165 PCK_Trivial,
1166
1167 /// The type would be trivial except that it is volatile-qualified. Types
1168 /// that fall into one of the other non-trivial cases may additionally be
1169 /// volatile-qualified.
1170 PCK_VolatileTrivial,
1171
1172 /// The type is an Objective-C retainable pointer type that is qualified
1173 /// with the ARC __strong qualifier.
1174 PCK_ARCStrong,
1175
1176 /// The type is an Objective-C retainable pointer type that is qualified
1177 /// with the ARC __weak qualifier.
1178 PCK_ARCWeak,
1179
1180 /// The type is a struct containing a field whose type is neither
1181 /// PCK_Trivial nor PCK_VolatileTrivial.
1182 /// Note that a C++ struct type does not necessarily match this; C++ copying
1183 /// semantics are too complex to express here, in part because they depend
1184 /// on the exact constructor or assignment operator that is chosen by
1185 /// overload resolution to do the copy.
1186 PCK_Struct
1187 };
1188
1189 /// Check if this is a non-trivial type that would cause a C struct
1190 /// transitively containing this type to be non-trivial to copy and return the
1191 /// kind.
1192 PrimitiveCopyKind isNonTrivialToPrimitiveCopy() const;
1193
1194 /// Check if this is a non-trivial type that would cause a C struct
1195 /// transitively containing this type to be non-trivial to destructively
1196 /// move and return the kind. Destructive move in this context is a C++-style
1197 /// move in which the source object is placed in a valid but unspecified state
1198 /// after it is moved, as opposed to a truly destructive move in which the
1199 /// source object is placed in an uninitialized state.
1200 PrimitiveCopyKind isNonTrivialToPrimitiveDestructiveMove() const;
1201
1202 enum DestructionKind {
1203 DK_none,
1204 DK_cxx_destructor,
1205 DK_objc_strong_lifetime,
1206 DK_objc_weak_lifetime,
1207 DK_nontrivial_c_struct
1208 };
1209
1210 /// Returns a nonzero value if objects of this type require
1211 /// non-trivial work to clean up after. Non-zero because it's
1212 /// conceivable that qualifiers (objc_gc(weak)?) could make
1213 /// something require destruction.
1214 DestructionKind isDestructedType() const {
1215 return isDestructedTypeImpl(*this);
1216 }
1217
1218 /// Check if this is or contains a C union that is non-trivial to
1219 /// default-initialize, which is a union that has a member that is non-trivial
1220 /// to default-initialize. If this returns true,
1221 /// isNonTrivialToPrimitiveDefaultInitialize returns PDIK_Struct.
1222 bool hasNonTrivialToPrimitiveDefaultInitializeCUnion() const;
1223
1224 /// Check if this is or contains a C union that is non-trivial to destruct,
1225 /// which is a union that has a member that is non-trivial to destruct. If
1226 /// this returns true, isDestructedType returns DK_nontrivial_c_struct.
1227 bool hasNonTrivialToPrimitiveDestructCUnion() const;
1228
1229 /// Check if this is or contains a C union that is non-trivial to copy, which
1230 /// is a union that has a member that is non-trivial to copy. If this returns
1231 /// true, isNonTrivialToPrimitiveCopy returns PCK_Struct.
1232 bool hasNonTrivialToPrimitiveCopyCUnion() const;
1233
1234 /// Determine whether expressions of the given type are forbidden
1235 /// from being lvalues in C.
1236 ///
1237 /// The expression types that are forbidden to be lvalues are:
1238 /// - 'void', but not qualified void
1239 /// - function types
1240 ///
1241 /// The exact rule here is C99 6.3.2.1:
1242 /// An lvalue is an expression with an object type or an incomplete
1243 /// type other than void.
1244 bool isCForbiddenLValueType() const;
1245
1246 /// Substitute type arguments for the Objective-C type parameters used in the
1247 /// subject type.
1248 ///
1249 /// \param ctx ASTContext in which the type exists.
1250 ///
1251 /// \param typeArgs The type arguments that will be substituted for the
1252 /// Objective-C type parameters in the subject type, which are generally
1253 /// computed via \c Type::getObjCSubstitutions. If empty, the type
1254 /// parameters will be replaced with their bounds or id/Class, as appropriate
1255 /// for the context.
1256 ///
1257 /// \param context The context in which the subject type was written.
1258 ///
1259 /// \returns the resulting type.
1260 QualType substObjCTypeArgs(ASTContext &ctx,
1261 ArrayRef<QualType> typeArgs,
1262 ObjCSubstitutionContext context) const;
1263
1264 /// Substitute type arguments from an object type for the Objective-C type
1265 /// parameters used in the subject type.
1266 ///
1267 /// This operation combines the computation of type arguments for
1268 /// substitution (\c Type::getObjCSubstitutions) with the actual process of
1269 /// substitution (\c QualType::substObjCTypeArgs) for the convenience of
1270 /// callers that need to perform a single substitution in isolation.
1271 ///
1272 /// \param objectType The type of the object whose member type we're
1273 /// substituting into. For example, this might be the receiver of a message
1274 /// or the base of a property access.
1275 ///
1276 /// \param dc The declaration context from which the subject type was
1277 /// retrieved, which indicates (for example) which type parameters should
1278 /// be substituted.
1279 ///
1280 /// \param context The context in which the subject type was written.
1281 ///
1282 /// \returns the subject type after replacing all of the Objective-C type
1283 /// parameters with their corresponding arguments.
1284 QualType substObjCMemberType(QualType objectType,
1285 const DeclContext *dc,
1286 ObjCSubstitutionContext context) const;
1287
1288 /// Strip Objective-C "__kindof" types from the given type.
1289 QualType stripObjCKindOfType(const ASTContext &ctx) const;
1290
1291 /// Remove all qualifiers including _Atomic.
1292 QualType getAtomicUnqualifiedType() const;
1293
1294private:
1295 // These methods are implemented in a separate translation unit;
1296 // "static"-ize them to avoid creating temporary QualTypes in the
1297 // caller.
1298 static bool isConstant(QualType T, const ASTContext& Ctx);
1299 static QualType getDesugaredType(QualType T, const ASTContext &Context);
1300 static SplitQualType getSplitDesugaredType(QualType T);
1301 static SplitQualType getSplitUnqualifiedTypeImpl(QualType type);
1302 static QualType getSingleStepDesugaredTypeImpl(QualType type,
1303 const ASTContext &C);
1304 static QualType IgnoreParens(QualType T);
1305 static DestructionKind isDestructedTypeImpl(QualType type);
1306
1307 /// Check if \param RD is or contains a non-trivial C union.
1308 static bool hasNonTrivialToPrimitiveDefaultInitializeCUnion(const RecordDecl *RD);
1309 static bool hasNonTrivialToPrimitiveDestructCUnion(const RecordDecl *RD);
1310 static bool hasNonTrivialToPrimitiveCopyCUnion(const RecordDecl *RD);
1311};
1312
1313} // namespace clang
1314
1315namespace llvm {
1316
1317/// Implement simplify_type for QualType, so that we can dyn_cast from QualType
1318/// to a specific Type class.
1319template<> struct simplify_type< ::clang::QualType> {
1320 using SimpleType = const ::clang::Type *;
1321
1322 static SimpleType getSimplifiedValue(::clang::QualType Val) {
1323 return Val.getTypePtr();
1324 }
1325};
1326
1327// Teach SmallPtrSet that QualType is "basically a pointer".
1328template<>
1329struct PointerLikeTypeTraits<clang::QualType> {
1330 static inline void *getAsVoidPointer(clang::QualType P) {
1331 return P.getAsOpaquePtr();
1332 }
1333
1334 static inline clang::QualType getFromVoidPointer(void *P) {
1335 return clang::QualType::getFromOpaquePtr(P);
1336 }
1337
1338 // Various qualifiers go in low bits.
1339 static constexpr int NumLowBitsAvailable = 0;
1340};
1341
1342} // namespace llvm
1343
1344namespace clang {
1345
1346/// Base class that is common to both the \c ExtQuals and \c Type
1347/// classes, which allows \c QualType to access the common fields between the
1348/// two.
1349class ExtQualsTypeCommonBase {
1350 friend class ExtQuals;
1351 friend class QualType;
1352 friend class Type;
1353
1354 /// The "base" type of an extended qualifiers type (\c ExtQuals) or
1355 /// a self-referential pointer (for \c Type).
1356 ///
1357 /// This pointer allows an efficient mapping from a QualType to its
1358 /// underlying type pointer.
1359 const Type *const BaseType;
1360
1361 /// The canonical type of this type. A QualType.
1362 QualType CanonicalType;
1363
1364 ExtQualsTypeCommonBase(const Type *baseType, QualType canon)
1365 : BaseType(baseType), CanonicalType(canon) {}
1366};
1367
1368/// We can encode up to four bits in the low bits of a
1369/// type pointer, but there are many more type qualifiers that we want
1370/// to be able to apply to an arbitrary type. Therefore we have this
1371/// struct, intended to be heap-allocated and used by QualType to
1372/// store qualifiers.
1373///
1374/// The current design tags the 'const', 'restrict', and 'volatile' qualifiers
1375/// in three low bits on the QualType pointer; a fourth bit records whether
1376/// the pointer is an ExtQuals node. The extended qualifiers (address spaces,
1377/// Objective-C GC attributes) are much more rare.
1378class ExtQuals : public ExtQualsTypeCommonBase, public llvm::FoldingSetNode {
1379 // NOTE: changing the fast qualifiers should be straightforward as
1380 // long as you don't make 'const' non-fast.
1381 // 1. Qualifiers:
1382 // a) Modify the bitmasks (Qualifiers::TQ and DeclSpec::TQ).
1383 // Fast qualifiers must occupy the low-order bits.
1384 // b) Update Qualifiers::FastWidth and FastMask.
1385 // 2. QualType:
1386 // a) Update is{Volatile,Restrict}Qualified(), defined inline.
1387 // b) Update remove{Volatile,Restrict}, defined near the end of
1388 // this header.
1389 // 3. ASTContext:
1390 // a) Update get{Volatile,Restrict}Type.
1391
1392 /// The immutable set of qualifiers applied by this node. Always contains
1393 /// extended qualifiers.
1394 Qualifiers Quals;
1395
1396 ExtQuals *this_() { return this; }
1397
1398public:
1399 ExtQuals(const Type *baseType, QualType canon, Qualifiers quals)
1400 : ExtQualsTypeCommonBase(baseType,
1401 canon.isNull() ? QualType(this_(), 0) : canon),
1402 Quals(quals) {
1403 assert(Quals.hasNonFastQualifiers()(static_cast<void> (0))
1404 && "ExtQuals created with no fast qualifiers")(static_cast<void> (0));
1405 assert(!Quals.hasFastQualifiers()(static_cast<void> (0))
1406 && "ExtQuals created with fast qualifiers")(static_cast<void> (0));
1407 }
1408
1409 Qualifiers getQualifiers() const { return Quals; }
1410
1411 bool hasObjCGCAttr() const { return Quals.hasObjCGCAttr(); }
1412 Qualifiers::GC getObjCGCAttr() const { return Quals.getObjCGCAttr(); }
1413
1414 bool hasObjCLifetime() const { return Quals.hasObjCLifetime(); }
1415 Qualifiers::ObjCLifetime getObjCLifetime() const {
1416 return Quals.getObjCLifetime();
1417 }
1418
1419 bool hasAddressSpace() const { return Quals.hasAddressSpace(); }
1420 LangAS getAddressSpace() const { return Quals.getAddressSpace(); }
1421
1422 const Type *getBaseType() const { return BaseType; }
1423
1424public:
1425 void Profile(llvm::FoldingSetNodeID &ID) const {
1426 Profile(ID, getBaseType(), Quals);
1427 }
1428
1429 static void Profile(llvm::FoldingSetNodeID &ID,
1430 const Type *BaseType,
1431 Qualifiers Quals) {
1432 assert(!Quals.hasFastQualifiers() && "fast qualifiers in ExtQuals hash!")(static_cast<void> (0));
1433 ID.AddPointer(BaseType);
1434 Quals.Profile(ID);
1435 }
1436};
1437
1438/// The kind of C++11 ref-qualifier associated with a function type.
1439/// This determines whether a member function's "this" object can be an
1440/// lvalue, rvalue, or neither.
1441enum RefQualifierKind {
1442 /// No ref-qualifier was provided.
1443 RQ_None = 0,
1444
1445 /// An lvalue ref-qualifier was provided (\c &).
1446 RQ_LValue,
1447
1448 /// An rvalue ref-qualifier was provided (\c &&).
1449 RQ_RValue
1450};
1451
1452/// Which keyword(s) were used to create an AutoType.
1453enum class AutoTypeKeyword {
1454 /// auto
1455 Auto,
1456
1457 /// decltype(auto)
1458 DecltypeAuto,
1459
1460 /// __auto_type (GNU extension)
1461 GNUAutoType
1462};
1463
1464/// The base class of the type hierarchy.
1465///
1466/// A central concept with types is that each type always has a canonical
1467/// type. A canonical type is the type with any typedef names stripped out
1468/// of it or the types it references. For example, consider:
1469///
1470/// typedef int foo;
1471/// typedef foo* bar;
1472/// 'int *' 'foo *' 'bar'
1473///
1474/// There will be a Type object created for 'int'. Since int is canonical, its
1475/// CanonicalType pointer points to itself. There is also a Type for 'foo' (a
1476/// TypedefType). Its CanonicalType pointer points to the 'int' Type. Next
1477/// there is a PointerType that represents 'int*', which, like 'int', is
1478/// canonical. Finally, there is a PointerType type for 'foo*' whose canonical
1479/// type is 'int*', and there is a TypedefType for 'bar', whose canonical type
1480/// is also 'int*'.
1481///
1482/// Non-canonical types are useful for emitting diagnostics, without losing
1483/// information about typedefs being used. Canonical types are useful for type
1484/// comparisons (they allow by-pointer equality tests) and useful for reasoning
1485/// about whether something has a particular form (e.g. is a function type),
1486/// because they implicitly, recursively, strip all typedefs out of a type.
1487///
1488/// Types, once created, are immutable.
1489///
1490class alignas(8) Type : public ExtQualsTypeCommonBase {
1491public:
1492 enum TypeClass {
1493#define TYPE(Class, Base) Class,
1494#define LAST_TYPE(Class) TypeLast = Class
1495#define ABSTRACT_TYPE(Class, Base)
1496#include "clang/AST/TypeNodes.inc"
1497 };
1498
1499private:
1500 /// Bitfields required by the Type class.
1501 class TypeBitfields {
1502 friend class Type;
1503 template <class T> friend class TypePropertyCache;
1504
1505 /// TypeClass bitfield - Enum that specifies what subclass this belongs to.
1506 unsigned TC : 8;
1507
1508 /// Store information on the type dependency.
1509 unsigned Dependence : llvm::BitWidth<TypeDependence>;
1510
1511 /// True if the cache (i.e. the bitfields here starting with
1512 /// 'Cache') is valid.
1513 mutable unsigned CacheValid : 1;
1514
1515 /// Linkage of this type.
1516 mutable unsigned CachedLinkage : 3;
1517
1518 /// Whether this type involves and local or unnamed types.
1519 mutable unsigned CachedLocalOrUnnamed : 1;
1520
1521 /// Whether this type comes from an AST file.
1522 mutable unsigned FromAST : 1;
1523
1524 bool isCacheValid() const {
1525 return CacheValid;
1526 }
1527
1528 Linkage getLinkage() const {
1529 assert(isCacheValid() && "getting linkage from invalid cache")(static_cast<void> (0));
1530 return static_cast<Linkage>(CachedLinkage);
1531 }
1532
1533 bool hasLocalOrUnnamedType() const {
1534 assert(isCacheValid() && "getting linkage from invalid cache")(static_cast<void> (0));
1535 return CachedLocalOrUnnamed;
1536 }
1537 };
1538 enum { NumTypeBits = 8 + llvm::BitWidth<TypeDependence> + 6 };
1539
1540protected:
1541 // These classes allow subclasses to somewhat cleanly pack bitfields
1542 // into Type.
1543
1544 class ArrayTypeBitfields {
1545 friend class ArrayType;
1546
1547 unsigned : NumTypeBits;
1548
1549 /// CVR qualifiers from declarations like
1550 /// 'int X[static restrict 4]'. For function parameters only.
1551 unsigned IndexTypeQuals : 3;
1552
1553 /// Storage class qualifiers from declarations like
1554 /// 'int X[static restrict 4]'. For function parameters only.
1555 /// Actually an ArrayType::ArraySizeModifier.
1556 unsigned SizeModifier : 3;
1557 };
1558
1559 class ConstantArrayTypeBitfields {
1560 friend class ConstantArrayType;
1561
1562 unsigned : NumTypeBits + 3 + 3;
1563
1564 /// Whether we have a stored size expression.
1565 unsigned HasStoredSizeExpr : 1;
1566 };
1567
1568 class BuiltinTypeBitfields {
1569 friend class BuiltinType;
1570
1571 unsigned : NumTypeBits;
1572
1573 /// The kind (BuiltinType::Kind) of builtin type this is.
1574 unsigned Kind : 8;
1575 };
1576
1577 /// FunctionTypeBitfields store various bits belonging to FunctionProtoType.
1578 /// Only common bits are stored here. Additional uncommon bits are stored
1579 /// in a trailing object after FunctionProtoType.
1580 class FunctionTypeBitfields {
1581 friend class FunctionProtoType;
1582 friend class FunctionType;
1583
1584 unsigned : NumTypeBits;
1585
1586 /// Extra information which affects how the function is called, like
1587 /// regparm and the calling convention.
1588 unsigned ExtInfo : 13;
1589
1590 /// The ref-qualifier associated with a \c FunctionProtoType.
1591 ///
1592 /// This is a value of type \c RefQualifierKind.
1593 unsigned RefQualifier : 2;
1594
1595 /// Used only by FunctionProtoType, put here to pack with the
1596 /// other bitfields.
1597 /// The qualifiers are part of FunctionProtoType because...
1598 ///
1599 /// C++ 8.3.5p4: The return type, the parameter type list and the
1600 /// cv-qualifier-seq, [...], are part of the function type.
1601 unsigned FastTypeQuals : Qualifiers::FastWidth;
1602 /// Whether this function has extended Qualifiers.
1603 unsigned HasExtQuals : 1;
1604
1605 /// The number of parameters this function has, not counting '...'.
1606 /// According to [implimits] 8 bits should be enough here but this is
1607 /// somewhat easy to exceed with metaprogramming and so we would like to
1608 /// keep NumParams as wide as reasonably possible.
1609 unsigned NumParams : 16;
1610
1611 /// The type of exception specification this function has.
1612 unsigned ExceptionSpecType : 4;
1613
1614 /// Whether this function has extended parameter information.
1615 unsigned HasExtParameterInfos : 1;
1616
1617 /// Whether the function is variadic.
1618 unsigned Variadic : 1;
1619
1620 /// Whether this function has a trailing return type.
1621 unsigned HasTrailingReturn : 1;
1622 };
1623
1624 class ObjCObjectTypeBitfields {
1625 friend class ObjCObjectType;
1626
1627 unsigned : NumTypeBits;
1628
1629 /// The number of type arguments stored directly on this object type.
1630 unsigned NumTypeArgs : 7;
1631
1632 /// The number of protocols stored directly on this object type.
1633 unsigned NumProtocols : 6;
1634
1635 /// Whether this is a "kindof" type.
1636 unsigned IsKindOf : 1;
1637 };
1638
1639 class ReferenceTypeBitfields {
1640 friend class ReferenceType;
1641
1642 unsigned : NumTypeBits;
1643
1644 /// True if the type was originally spelled with an lvalue sigil.
1645 /// This is never true of rvalue references but can also be false
1646 /// on lvalue references because of C++0x [dcl.typedef]p9,
1647 /// as follows:
1648 ///
1649 /// typedef int &ref; // lvalue, spelled lvalue
1650 /// typedef int &&rvref; // rvalue
1651 /// ref &a; // lvalue, inner ref, spelled lvalue
1652 /// ref &&a; // lvalue, inner ref
1653 /// rvref &a; // lvalue, inner ref, spelled lvalue
1654 /// rvref &&a; // rvalue, inner ref
1655 unsigned SpelledAsLValue : 1;
1656
1657 /// True if the inner type is a reference type. This only happens
1658 /// in non-canonical forms.
1659 unsigned InnerRef : 1;
1660 };
1661
1662 class TypeWithKeywordBitfields {
1663 friend class TypeWithKeyword;
1664
1665 unsigned : NumTypeBits;
1666
1667 /// An ElaboratedTypeKeyword. 8 bits for efficient access.
1668 unsigned Keyword : 8;
1669 };
1670
1671 enum { NumTypeWithKeywordBits = 8 };
1672
1673 class ElaboratedTypeBitfields {
1674 friend class ElaboratedType;
1675
1676 unsigned : NumTypeBits;
1677 unsigned : NumTypeWithKeywordBits;
1678
1679 /// Whether the ElaboratedType has a trailing OwnedTagDecl.
1680 unsigned HasOwnedTagDecl : 1;
1681 };
1682
1683 class VectorTypeBitfields {
1684 friend class VectorType;
1685 friend class DependentVectorType;
1686
1687 unsigned : NumTypeBits;
1688
1689 /// The kind of vector, either a generic vector type or some
1690 /// target-specific vector type such as for AltiVec or Neon.
1691 unsigned VecKind : 3;
1692 /// The number of elements in the vector.
1693 uint32_t NumElements;
1694 };
1695
1696 class AttributedTypeBitfields {
1697 friend class AttributedType;
1698
1699 unsigned : NumTypeBits;
1700
1701 /// An AttributedType::Kind
1702 unsigned AttrKind : 32 - NumTypeBits;
1703 };
1704
1705 class AutoTypeBitfields {
1706 friend class AutoType;
1707
1708 unsigned : NumTypeBits;
1709
1710 /// Was this placeholder type spelled as 'auto', 'decltype(auto)',
1711 /// or '__auto_type'? AutoTypeKeyword value.
1712 unsigned Keyword : 2;
1713
1714 /// The number of template arguments in the type-constraints, which is
1715 /// expected to be able to hold at least 1024 according to [implimits].
1716 /// However as this limit is somewhat easy to hit with template
1717 /// metaprogramming we'd prefer to keep it as large as possible.
1718 /// At the moment it has been left as a non-bitfield since this type
1719 /// safely fits in 64 bits as an unsigned, so there is no reason to
1720 /// introduce the performance impact of a bitfield.
1721 unsigned NumArgs;
1722 };
1723
1724 class SubstTemplateTypeParmPackTypeBitfields {
1725 friend class SubstTemplateTypeParmPackType;
1726
1727 unsigned : NumTypeBits;
1728
1729 /// The number of template arguments in \c Arguments, which is
1730 /// expected to be able to hold at least 1024 according to [implimits].
1731 /// However as this limit is somewhat easy to hit with template
1732 /// metaprogramming we'd prefer to keep it as large as possible.
1733 /// At the moment it has been left as a non-bitfield since this type
1734 /// safely fits in 64 bits as an unsigned, so there is no reason to
1735 /// introduce the performance impact of a bitfield.
1736 unsigned NumArgs;
1737 };
1738
1739 class TemplateSpecializationTypeBitfields {
1740 friend class TemplateSpecializationType;
1741
1742 unsigned : NumTypeBits;
1743
1744 /// Whether this template specialization type is a substituted type alias.
1745 unsigned TypeAlias : 1;
1746
1747 /// The number of template arguments named in this class template
1748 /// specialization, which is expected to be able to hold at least 1024
1749 /// according to [implimits]. However, as this limit is somewhat easy to
1750 /// hit with template metaprogramming we'd prefer to keep it as large
1751 /// as possible. At the moment it has been left as a non-bitfield since
1752 /// this type safely fits in 64 bits as an unsigned, so there is no reason
1753 /// to introduce the performance impact of a bitfield.
1754 unsigned NumArgs;
1755 };
1756
1757 class DependentTemplateSpecializationTypeBitfields {
1758 friend class DependentTemplateSpecializationType;
1759
1760 unsigned : NumTypeBits;
1761 unsigned : NumTypeWithKeywordBits;
1762
1763 /// The number of template arguments named in this class template
1764 /// specialization, which is expected to be able to hold at least 1024
1765 /// according to [implimits]. However, as this limit is somewhat easy to
1766 /// hit with template metaprogramming we'd prefer to keep it as large
1767 /// as possible. At the moment it has been left as a non-bitfield since
1768 /// this type safely fits in 64 bits as an unsigned, so there is no reason
1769 /// to introduce the performance impact of a bitfield.
1770 unsigned NumArgs;
1771 };
1772
1773 class PackExpansionTypeBitfields {
1774 friend class PackExpansionType;
1775
1776 unsigned : NumTypeBits;
1777
1778 /// The number of expansions that this pack expansion will
1779 /// generate when substituted (+1), which is expected to be able to
1780 /// hold at least 1024 according to [implimits]. However, as this limit
1781 /// is somewhat easy to hit with template metaprogramming we'd prefer to
1782 /// keep it as large as possible. At the moment it has been left as a
1783 /// non-bitfield since this type safely fits in 64 bits as an unsigned, so
1784 /// there is no reason to introduce the performance impact of a bitfield.
1785 ///
1786 /// This field will only have a non-zero value when some of the parameter
1787 /// packs that occur within the pattern have been substituted but others
1788 /// have not.
1789 unsigned NumExpansions;
1790 };
1791
1792 union {
1793 TypeBitfields TypeBits;
1794 ArrayTypeBitfields ArrayTypeBits;
1795 ConstantArrayTypeBitfields ConstantArrayTypeBits;
1796 AttributedTypeBitfields AttributedTypeBits;
1797 AutoTypeBitfields AutoTypeBits;
1798 BuiltinTypeBitfields BuiltinTypeBits;
1799 FunctionTypeBitfields FunctionTypeBits;
1800 ObjCObjectTypeBitfields ObjCObjectTypeBits;
1801 ReferenceTypeBitfields ReferenceTypeBits;
1802 TypeWithKeywordBitfields TypeWithKeywordBits;
1803 ElaboratedTypeBitfields ElaboratedTypeBits;
1804 VectorTypeBitfields VectorTypeBits;
1805 SubstTemplateTypeParmPackTypeBitfields SubstTemplateTypeParmPackTypeBits;
1806 TemplateSpecializationTypeBitfields TemplateSpecializationTypeBits;
1807 DependentTemplateSpecializationTypeBitfields
1808 DependentTemplateSpecializationTypeBits;
1809 PackExpansionTypeBitfields PackExpansionTypeBits;
1810 };
1811
1812private:
1813 template <class T> friend class TypePropertyCache;
1814
1815 /// Set whether this type comes from an AST file.
1816 void setFromAST(bool V = true) const {
1817 TypeBits.FromAST = V;
1818 }
1819
1820protected:
1821 friend class ASTContext;
1822
1823 Type(TypeClass tc, QualType canon, TypeDependence Dependence)
1824 : ExtQualsTypeCommonBase(this,
1825 canon.isNull() ? QualType(this_(), 0) : canon) {
1826 static_assert(sizeof(*this) <= 8 + sizeof(ExtQualsTypeCommonBase),
1827 "changing bitfields changed sizeof(Type)!");
1828 static_assert(alignof(decltype(*this)) % sizeof(void *) == 0,
1829 "Insufficient alignment!");
1830 TypeBits.TC = tc;
1831 TypeBits.Dependence = static_cast<unsigned>(Dependence);
1832 TypeBits.CacheValid = false;
1833 TypeBits.CachedLocalOrUnnamed = false;
1834 TypeBits.CachedLinkage = NoLinkage;
1835 TypeBits.FromAST = false;
1836 }
1837
1838 // silence VC++ warning C4355: 'this' : used in base member initializer list
1839 Type *this_() { return this; }
1840
1841 void setDependence(TypeDependence D) {
1842 TypeBits.Dependence = static_cast<unsigned>(D);
1843 }
1844
1845 void addDependence(TypeDependence D) { setDependence(getDependence() | D); }
1846
1847public:
1848 friend class ASTReader;
1849 friend class ASTWriter;
1850 template <class T> friend class serialization::AbstractTypeReader;
1851 template <class T> friend class serialization::AbstractTypeWriter;
1852
1853 Type(const Type &) = delete;
1854 Type(Type &&) = delete;
1855 Type &operator=(const Type &) = delete;
1856 Type &operator=(Type &&) = delete;
1857
1858 TypeClass getTypeClass() const { return static_cast<TypeClass>(TypeBits.TC); }
1859
1860 /// Whether this type comes from an AST file.
1861 bool isFromAST() const { return TypeBits.FromAST; }
1862
1863 /// Whether this type is or contains an unexpanded parameter
1864 /// pack, used to support C++0x variadic templates.
1865 ///
1866 /// A type that contains a parameter pack shall be expanded by the
1867 /// ellipsis operator at some point. For example, the typedef in the
1868 /// following example contains an unexpanded parameter pack 'T':
1869 ///
1870 /// \code
1871 /// template<typename ...T>
1872 /// struct X {
1873 /// typedef T* pointer_types; // ill-formed; T is a parameter pack.
1874 /// };
1875 /// \endcode
1876 ///
1877 /// Note that this routine does not specify which
1878 bool containsUnexpandedParameterPack() const {
1879 return getDependence() & TypeDependence::UnexpandedPack;
1880 }
1881
1882 /// Determines if this type would be canonical if it had no further
1883 /// qualification.
1884 bool isCanonicalUnqualified() const {
1885 return CanonicalType == QualType(this, 0);
1886 }
1887
1888 /// Pull a single level of sugar off of this locally-unqualified type.
1889 /// Users should generally prefer SplitQualType::getSingleStepDesugaredType()
1890 /// or QualType::getSingleStepDesugaredType(const ASTContext&).
1891 QualType getLocallyUnqualifiedSingleStepDesugaredType() const;
1892
1893 /// As an extension, we classify types as one of "sized" or "sizeless";
1894 /// every type is one or the other. Standard types are all sized;
1895 /// sizeless types are purely an extension.
1896 ///
1897 /// Sizeless types contain data with no specified size, alignment,
1898 /// or layout.
1899 bool isSizelessType() const;
1900 bool isSizelessBuiltinType() const;
1901
1902 /// Determines if this is a sizeless type supported by the
1903 /// 'arm_sve_vector_bits' type attribute, which can be applied to a single
1904 /// SVE vector or predicate, excluding tuple types such as svint32x4_t.
1905 bool isVLSTBuiltinType() const;
1906
1907 /// Returns the representative type for the element of an SVE builtin type.
1908 /// This is used to represent fixed-length SVE vectors created with the
1909 /// 'arm_sve_vector_bits' type attribute as VectorType.
1910 QualType getSveEltType(const ASTContext &Ctx) const;
1911
1912 /// Types are partitioned into 3 broad categories (C99 6.2.5p1):
1913 /// object types, function types, and incomplete types.
1914
1915 /// Return true if this is an incomplete type.
1916 /// A type that can describe objects, but which lacks information needed to
1917 /// determine its size (e.g. void, or a fwd declared struct). Clients of this
1918 /// routine will need to determine if the size is actually required.
1919 ///
1920 /// Def If non-null, and the type refers to some kind of declaration
1921 /// that can be completed (such as a C struct, C++ class, or Objective-C
1922 /// class), will be set to the declaration.
1923 bool isIncompleteType(NamedDecl **Def = nullptr) const;
1924
1925 /// Return true if this is an incomplete or object
1926 /// type, in other words, not a function type.
1927 bool isIncompleteOrObjectType() const {
1928 return !isFunctionType();
1929 }
1930
1931 /// Determine whether this type is an object type.
1932 bool isObjectType() const {
1933 // C++ [basic.types]p8:
1934 // An object type is a (possibly cv-qualified) type that is not a
1935 // function type, not a reference type, and not a void type.
1936 return !isReferenceType() && !isFunctionType() && !isVoidType();
1937 }
1938
1939 /// Return true if this is a literal type
1940 /// (C++11 [basic.types]p10)
1941 bool isLiteralType(const ASTContext &Ctx) const;
1942
1943 /// Determine if this type is a structural type, per C++20 [temp.param]p7.
1944 bool isStructuralType() const;
1945
1946 /// Test if this type is a standard-layout type.
1947 /// (C++0x [basic.type]p9)
1948 bool isStandardLayoutType() const;
1949
1950 /// Helper methods to distinguish type categories. All type predicates
1951 /// operate on the canonical type, ignoring typedefs and qualifiers.
1952
1953 /// Returns true if the type is a builtin type.
1954 bool isBuiltinType() const;
1955
1956 /// Test for a particular builtin type.
1957 bool isSpecificBuiltinType(unsigned K) const;
1958
1959 /// Test for a type which does not represent an actual type-system type but
1960 /// is instead used as a placeholder for various convenient purposes within
1961 /// Clang. All such types are BuiltinTypes.
1962 bool isPlaceholderType() const;
1963 const BuiltinType *getAsPlaceholderType() const;
1964
1965 /// Test for a specific placeholder type.
1966 bool isSpecificPlaceholderType(unsigned K) const;
1967
1968 /// Test for a placeholder type other than Overload; see
1969 /// BuiltinType::isNonOverloadPlaceholderType.
1970 bool isNonOverloadPlaceholderType() const;
1971
1972 /// isIntegerType() does *not* include complex integers (a GCC extension).
1973 /// isComplexIntegerType() can be used to test for complex integers.
1974 bool isIntegerType() const; // C99 6.2.5p17 (int, char, bool, enum)
1975 bool isEnumeralType() const;
1976
1977 /// Determine whether this type is a scoped enumeration type.
1978 bool isScopedEnumeralType() const;
1979 bool isBooleanType() const;
1980 bool isCharType() const;
1981 bool isWideCharType() const;
1982 bool isChar8Type() const;
1983 bool isChar16Type() const;
1984 bool isChar32Type() const;
1985 bool isAnyCharacterType() const;
1986 bool isIntegralType(const ASTContext &Ctx) const;
1987
1988 /// Determine whether this type is an integral or enumeration type.
1989 bool isIntegralOrEnumerationType() const;
1990
1991 /// Determine whether this type is an integral or unscoped enumeration type.
1992 bool isIntegralOrUnscopedEnumerationType() const;
1993 bool isUnscopedEnumerationType() const;
1994
1995 /// Floating point categories.
1996 bool isRealFloatingType() const; // C99 6.2.5p10 (float, double, long double)
1997 /// isComplexType() does *not* include complex integers (a GCC extension).
1998 /// isComplexIntegerType() can be used to test for complex integers.
1999 bool isComplexType() const; // C99 6.2.5p11 (complex)
2000 bool isAnyComplexType() const; // C99 6.2.5p11 (complex) + Complex Int.
2001 bool isFloatingType() const; // C99 6.2.5p11 (real floating + complex)
2002 bool isHalfType() const; // OpenCL 6.1.1.1, NEON (IEEE 754-2008 half)
2003 bool isFloat16Type() const; // C11 extension ISO/IEC TS 18661
2004 bool isBFloat16Type() const;
2005 bool isFloat128Type() const;
2006 bool isRealType() const; // C99 6.2.5p17 (real floating + integer)
2007 bool isArithmeticType() const; // C99 6.2.5p18 (integer + floating)
2008 bool isVoidType() const; // C99 6.2.5p19
2009 bool isScalarType() const; // C99 6.2.5p21 (arithmetic + pointers)
2010 bool isAggregateType() const;
2011 bool isFundamentalType() const;
2012 bool isCompoundType() const;
2013
2014 // Type Predicates: Check to see if this type is structurally the specified
2015 // type, ignoring typedefs and qualifiers.
2016 bool isFunctionType() const;
2017 bool isFunctionNoProtoType() const { return getAs<FunctionNoProtoType>(); }
2018 bool isFunctionProtoType() const { return getAs<FunctionProtoType>(); }
2019 bool isPointerType() const;
2020 bool isAnyPointerType() const; // Any C pointer or ObjC object pointer
2021 bool isBlockPointerType() const;
2022 bool isVoidPointerType() const;
2023 bool isReferenceType() const;
2024 bool isLValueReferenceType() const;
2025 bool isRValueReferenceType() const;
2026 bool isObjectPointerType() const;
2027 bool isFunctionPointerType() const;
2028 bool isFunctionReferenceType() const;
2029 bool isMemberPointerType() const;
2030 bool isMemberFunctionPointerType() const;
2031 bool isMemberDataPointerType() const;
2032 bool isArrayType() const;
2033 bool isConstantArrayType() const;
2034 bool isIncompleteArrayType() const;
2035 bool isVariableArrayType() const;
2036 bool isDependentSizedArrayType() const;
2037 bool isRecordType() const;
2038 bool isClassType() const;
2039 bool isStructureType() const;
2040 bool isObjCBoxableRecordType() const;
2041 bool isInterfaceType() const;
2042 bool isStructureOrClassType() const;
2043 bool isUnionType() const;
2044 bool isComplexIntegerType() const; // GCC _Complex integer type.
2045 bool isVectorType() const; // GCC vector type.
2046 bool isExtVectorType() const; // Extended vector type.
2047 bool isMatrixType() const; // Matrix type.
2048 bool isConstantMatrixType() const; // Constant matrix type.
2049 bool isDependentAddressSpaceType() const; // value-dependent address space qualifier
2050 bool isObjCObjectPointerType() const; // pointer to ObjC object
2051 bool isObjCRetainableType() const; // ObjC object or block pointer
2052 bool isObjCLifetimeType() const; // (array of)* retainable type
2053 bool isObjCIndirectLifetimeType() const; // (pointer to)* lifetime type
2054 bool isObjCNSObjectType() const; // __attribute__((NSObject))
2055 bool isObjCIndependentClassType() const; // __attribute__((objc_independent_class))
2056 // FIXME: change this to 'raw' interface type, so we can used 'interface' type
2057 // for the common case.
2058 bool isObjCObjectType() const; // NSString or typeof(*(id)0)
2059 bool isObjCQualifiedInterfaceType() const; // NSString<foo>
2060 bool isObjCQualifiedIdType() const; // id<foo>
2061 bool isObjCQualifiedClassType() const; // Class<foo>
2062 bool isObjCObjectOrInterfaceType() const;
2063 bool isObjCIdType() const; // id
2064 bool isDecltypeType() const;
2065 /// Was this type written with the special inert-in-ARC __unsafe_unretained
2066 /// qualifier?
2067 ///
2068 /// This approximates the answer to the following question: if this
2069 /// translation unit were compiled in ARC, would this type be qualified
2070 /// with __unsafe_unretained?
2071 bool isObjCInertUnsafeUnretainedType() const {
2072 return hasAttr(attr::ObjCInertUnsafeUnretained);
2073 }
2074
2075 /// Whether the type is Objective-C 'id' or a __kindof type of an
2076 /// object type, e.g., __kindof NSView * or __kindof id
2077 /// <NSCopying>.
2078 ///
2079 /// \param bound Will be set to the bound on non-id subtype types,
2080 /// which will be (possibly specialized) Objective-C class type, or
2081 /// null for 'id.
2082 bool isObjCIdOrObjectKindOfType(const ASTContext &ctx,
2083 const ObjCObjectType *&bound) const;
2084
2085 bool isObjCClassType() const; // Class
2086
2087 /// Whether the type is Objective-C 'Class' or a __kindof type of an
2088 /// Class type, e.g., __kindof Class <NSCopying>.
2089 ///
2090 /// Unlike \c isObjCIdOrObjectKindOfType, there is no relevant bound
2091 /// here because Objective-C's type system cannot express "a class
2092 /// object for a subclass of NSFoo".
2093 bool isObjCClassOrClassKindOfType() const;
2094
2095 bool isBlockCompatibleObjCPointerType(ASTContext &ctx) const;
2096 bool isObjCSelType() const; // Class
2097 bool isObjCBuiltinType() const; // 'id' or 'Class'
2098 bool isObjCARCBridgableType() const;
2099 bool isCARCBridgableType() const;
2100 bool isTemplateTypeParmType() const; // C++ template type parameter
2101 bool isNullPtrType() const; // C++11 std::nullptr_t
2102 bool isNothrowT() const; // C++ std::nothrow_t
2103 bool isAlignValT() const; // C++17 std::align_val_t
2104 bool isStdByteType() const; // C++17 std::byte
2105 bool isAtomicType() const; // C11 _Atomic()
2106 bool isUndeducedAutoType() const; // C++11 auto or
2107 // C++14 decltype(auto)
2108 bool isTypedefNameType() const; // typedef or alias template
2109
2110#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
2111 bool is##Id##Type() const;
2112#include "clang/Basic/OpenCLImageTypes.def"
2113
2114 bool isImageType() const; // Any OpenCL image type
2115
2116 bool isSamplerT() const; // OpenCL sampler_t
2117 bool isEventT() const; // OpenCL event_t
2118 bool isClkEventT() const; // OpenCL clk_event_t
2119 bool isQueueT() const; // OpenCL queue_t
2120 bool isReserveIDT() const; // OpenCL reserve_id_t
2121
2122#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
2123 bool is##Id##Type() const;
2124#include "clang/Basic/OpenCLExtensionTypes.def"
2125 // Type defined in cl_intel_device_side_avc_motion_estimation OpenCL extension
2126 bool isOCLIntelSubgroupAVCType() const;
2127 bool isOCLExtOpaqueType() const; // Any OpenCL extension type
2128
2129 bool isPipeType() const; // OpenCL pipe type
2130 bool isExtIntType() const; // Extended Int Type
2131 bool isOpenCLSpecificType() const; // Any OpenCL specific type
2132
2133 /// Determines if this type, which must satisfy
2134 /// isObjCLifetimeType(), is implicitly __unsafe_unretained rather
2135 /// than implicitly __strong.
2136 bool isObjCARCImplicitlyUnretainedType() const;
2137
2138 /// Check if the type is the CUDA device builtin surface type.
2139 bool isCUDADeviceBuiltinSurfaceType() const;
2140 /// Check if the type is the CUDA device builtin texture type.
2141 bool isCUDADeviceBuiltinTextureType() const;
2142
2143 /// Return the implicit lifetime for this type, which must not be dependent.
2144 Qualifiers::ObjCLifetime getObjCARCImplicitLifetime() const;
2145
2146 enum ScalarTypeKind {
2147 STK_CPointer,
2148 STK_BlockPointer,
2149 STK_ObjCObjectPointer,
2150 STK_MemberPointer,
2151 STK_Bool,
2152 STK_Integral,
2153 STK_Floating,
2154 STK_IntegralComplex,
2155 STK_FloatingComplex,
2156 STK_FixedPoint
2157 };
2158
2159 /// Given that this is a scalar type, classify it.
2160 ScalarTypeKind getScalarTypeKind() const;
2161
2162 TypeDependence getDependence() const {
2163 return static_cast<TypeDependence>(TypeBits.Dependence);
2164 }
2165
2166 /// Whether this type is an error type.
2167 bool containsErrors() const {
2168 return getDependence() & TypeDependence::Error;
2169 }
2170
2171 /// Whether this type is a dependent type, meaning that its definition
2172 /// somehow depends on a template parameter (C++ [temp.dep.type]).
2173 bool isDependentType() const {
2174 return getDependence() & TypeDependence::Dependent;
2175 }
2176
2177 /// Determine whether this type is an instantiation-dependent type,
2178 /// meaning that the type involves a template parameter (even if the
2179 /// definition does not actually depend on the type substituted for that
2180 /// template parameter).
2181 bool isInstantiationDependentType() const {
2182 return getDependence() & TypeDependence::Instantiation;
2183 }
2184
2185 /// Determine whether this type is an undeduced type, meaning that
2186 /// it somehow involves a C++11 'auto' type or similar which has not yet been
2187 /// deduced.
2188 bool isUndeducedType() const;
2189
2190 /// Whether this type is a variably-modified type (C99 6.7.5).
2191 bool isVariablyModifiedType() const {
2192 return getDependence() & TypeDependence::VariablyModified;
2193 }
2194
2195 /// Whether this type involves a variable-length array type
2196 /// with a definite size.
2197 bool hasSizedVLAType() const;
2198
2199 /// Whether this type is or contains a local or unnamed type.
2200 bool hasUnnamedOrLocalType() const;
2201
2202 bool isOverloadableType() const;
2203
2204 /// Determine wither this type is a C++ elaborated-type-specifier.
2205 bool isElaboratedTypeSpecifier() const;
2206
2207 bool canDecayToPointerType() const;
2208
2209 /// Whether this type is represented natively as a pointer. This includes
2210 /// pointers, references, block pointers, and Objective-C interface,
2211 /// qualified id, and qualified interface types, as well as nullptr_t.
2212 bool hasPointerRepresentation() const;
2213
2214 /// Whether this type can represent an objective pointer type for the
2215 /// purpose of GC'ability
2216 bool hasObjCPointerRepresentation() const;
2217
2218 /// Determine whether this type has an integer representation
2219 /// of some sort, e.g., it is an integer type or a vector.
2220 bool hasIntegerRepresentation() const;
2221
2222 /// Determine whether this type has an signed integer representation
2223 /// of some sort, e.g., it is an signed integer type or a vector.
2224 bool hasSignedIntegerRepresentation() const;
2225
2226 /// Determine whether this type has an unsigned integer representation
2227 /// of some sort, e.g., it is an unsigned integer type or a vector.
2228 bool hasUnsignedIntegerRepresentation() const;
2229
2230 /// Determine whether this type has a floating-point representation
2231 /// of some sort, e.g., it is a floating-point type or a vector thereof.
2232 bool hasFloatingRepresentation() const;
2233
2234 // Type Checking Functions: Check to see if this type is structurally the
2235 // specified type, ignoring typedefs and qualifiers, and return a pointer to
2236 // the best type we can.
2237 const RecordType *getAsStructureType() const;
2238 /// NOTE: getAs*ArrayType are methods on ASTContext.
2239 const RecordType *getAsUnionType() const;
2240 const ComplexType *getAsComplexIntegerType() const; // GCC complex int type.
2241 const ObjCObjectType *getAsObjCInterfaceType() const;
2242
2243 // The following is a convenience method that returns an ObjCObjectPointerType
2244 // for object declared using an interface.
2245 const ObjCObjectPointerType *getAsObjCInterfacePointerType() const;
2246 const ObjCObjectPointerType *getAsObjCQualifiedIdType() const;
2247 const ObjCObjectPointerType *getAsObjCQualifiedClassType() const;
2248 const ObjCObjectType *getAsObjCQualifiedInterfaceType() const;
2249
2250 /// Retrieves the CXXRecordDecl that this type refers to, either
2251 /// because the type is a RecordType or because it is the injected-class-name
2252 /// type of a class template or class template partial specialization.
2253 CXXRecordDecl *getAsCXXRecordDecl() const;
2254
2255 /// Retrieves the RecordDecl this type refers to.
2256 RecordDecl *getAsRecordDecl() const;
2257
2258 /// Retrieves the TagDecl that this type refers to, either
2259 /// because the type is a TagType or because it is the injected-class-name
2260 /// type of a class template or class template partial specialization.
2261 TagDecl *getAsTagDecl() const;
2262
2263 /// If this is a pointer or reference to a RecordType, return the
2264 /// CXXRecordDecl that the type refers to.
2265 ///
2266 /// If this is not a pointer or reference, or the type being pointed to does
2267 /// not refer to a CXXRecordDecl, returns NULL.
2268 const CXXRecordDecl *getPointeeCXXRecordDecl() const;
2269
2270 /// Get the DeducedType whose type will be deduced for a variable with
2271 /// an initializer of this type. This looks through declarators like pointer
2272 /// types, but not through decltype or typedefs.
2273 DeducedType *getContainedDeducedType() const;
2274
2275 /// Get the AutoType whose type will be deduced for a variable with
2276 /// an initializer of this type. This looks through declarators like pointer
2277 /// types, but not through decltype or typedefs.
2278 AutoType *getContainedAutoType() const {
2279 return dyn_cast_or_null<AutoType>(getContainedDeducedType());
2280 }
2281
2282 /// Determine whether this type was written with a leading 'auto'
2283 /// corresponding to a trailing return type (possibly for a nested
2284 /// function type within a pointer to function type or similar).
2285 bool hasAutoForTrailingReturnType() const;
2286
2287 /// Member-template getAs<specific type>'. Look through sugar for
2288 /// an instance of \<specific type>. This scheme will eventually
2289 /// replace the specific getAsXXXX methods above.
2290 ///
2291 /// There are some specializations of this member template listed
2292 /// immediately following this class.
2293 template <typename T> const T *getAs() const;
2294
2295 /// Member-template getAsAdjusted<specific type>. Look through specific kinds
2296 /// of sugar (parens, attributes, etc) for an instance of \<specific type>.
2297 /// This is used when you need to walk over sugar nodes that represent some
2298 /// kind of type adjustment from a type that was written as a \<specific type>
2299 /// to another type that is still canonically a \<specific type>.
2300 template <typename T> const T *getAsAdjusted() const;
2301
2302 /// A variant of getAs<> for array types which silently discards
2303 /// qualifiers from the outermost type.
2304 const ArrayType *getAsArrayTypeUnsafe() const;
2305
2306 /// Member-template castAs<specific type>. Look through sugar for
2307 /// the underlying instance of \<specific type>.
2308 ///
2309 /// This method has the same relationship to getAs<T> as cast<T> has
2310 /// to dyn_cast<T>; which is to say, the underlying type *must*
2311 /// have the intended type, and this method will never return null.
2312 template <typename T> const T *castAs() const;
2313
2314 /// A variant of castAs<> for array type which silently discards
2315 /// qualifiers from the outermost type.
2316 const ArrayType *castAsArrayTypeUnsafe() const;
2317
2318 /// Determine whether this type had the specified attribute applied to it
2319 /// (looking through top-level type sugar).
2320 bool hasAttr(attr::Kind AK) const;
2321
2322 /// Get the base element type of this type, potentially discarding type
2323 /// qualifiers. This should never be used when type qualifiers
2324 /// are meaningful.
2325 const Type *getBaseElementTypeUnsafe() const;
2326
2327 /// If this is an array type, return the element type of the array,
2328 /// potentially with type qualifiers missing.
2329 /// This should never be used when type qualifiers are meaningful.
2330 const Type *getArrayElementTypeNoTypeQual() const;
2331
2332 /// If this is a pointer type, return the pointee type.
2333 /// If this is an array type, return the array element type.
2334 /// This should never be used when type qualifiers are meaningful.
2335 const Type *getPointeeOrArrayElementType() const;
2336
2337 /// If this is a pointer, ObjC object pointer, or block
2338 /// pointer, this returns the respective pointee.
2339 QualType getPointeeType() const;
2340
2341 /// Return the specified type with any "sugar" removed from the type,
2342 /// removing any typedefs, typeofs, etc., as well as any qualifiers.
2343 const Type *getUnqualifiedDesugaredType() const;
2344
2345 /// More type predicates useful for type checking/promotion
2346 bool isPromotableIntegerType() const; // C99 6.3.1.1p2
2347
2348 /// Return true if this is an integer type that is
2349 /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
2350 /// or an enum decl which has a signed representation.
2351 bool isSignedIntegerType() const;
2352
2353 /// Return true if this is an integer type that is
2354 /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool],
2355 /// or an enum decl which has an unsigned representation.
2356 bool isUnsignedIntegerType() const;
2357
2358 /// Determines whether this is an integer type that is signed or an
2359 /// enumeration types whose underlying type is a signed integer type.
2360 bool isSignedIntegerOrEnumerationType() const;
2361
2362 /// Determines whether this is an integer type that is unsigned or an
2363 /// enumeration types whose underlying type is a unsigned integer type.
2364 bool isUnsignedIntegerOrEnumerationType() const;
2365
2366 /// Return true if this is a fixed point type according to
2367 /// ISO/IEC JTC1 SC22 WG14 N1169.
2368 bool isFixedPointType() const;
2369
2370 /// Return true if this is a fixed point or integer type.
2371 bool isFixedPointOrIntegerType() const;
2372
2373 /// Return true if this is a saturated fixed point type according to
2374 /// ISO/IEC JTC1 SC22 WG14 N1169. This type can be signed or unsigned.
2375 bool isSaturatedFixedPointType() const;
2376
2377 /// Return true if this is a saturated fixed point type according to
2378 /// ISO/IEC JTC1 SC22 WG14 N1169. This type can be signed or unsigned.
2379 bool isUnsaturatedFixedPointType() const;
2380
2381 /// Return true if this is a fixed point type that is signed according
2382 /// to ISO/IEC JTC1 SC22 WG14 N1169. This type can also be saturated.
2383 bool isSignedFixedPointType() const;
2384
2385 /// Return true if this is a fixed point type that is unsigned according
2386 /// to ISO/IEC JTC1 SC22 WG14 N1169. This type can also be saturated.
2387 bool isUnsignedFixedPointType() const;
2388
2389 /// Return true if this is not a variable sized type,
2390 /// according to the rules of C99 6.7.5p3. It is not legal to call this on
2391 /// incomplete types.
2392 bool isConstantSizeType() const;
2393
2394 /// Returns true if this type can be represented by some
2395 /// set of type specifiers.
2396 bool isSpecifierType() const;
2397
2398 /// Determine the linkage of this type.
2399 Linkage getLinkage() const;
2400
2401 /// Determine the visibility of this type.
2402 Visibility getVisibility() const {
2403 return getLinkageAndVisibility().getVisibility();
2404 }
2405
2406 /// Return true if the visibility was explicitly set is the code.
2407 bool isVisibilityExplicit() const {
2408 return getLinkageAndVisibility().isVisibilityExplicit();
2409 }
2410
2411 /// Determine the linkage and visibility of this type.
2412 LinkageInfo getLinkageAndVisibility() const;
2413
2414 /// True if the computed linkage is valid. Used for consistency
2415 /// checking. Should always return true.
2416 bool isLinkageValid() const;
2417
2418 /// Determine the nullability of the given type.
2419 ///
2420 /// Note that nullability is only captured as sugar within the type
2421 /// system, not as part of the canonical type, so nullability will
2422 /// be lost by canonicalization and desugaring.
2423 Optional<NullabilityKind> getNullability(const ASTContext &context) const;
2424
2425 /// Determine whether the given type can have a nullability
2426 /// specifier applied to it, i.e., if it is any kind of pointer type.
2427 ///
2428 /// \param ResultIfUnknown The value to return if we don't yet know whether
2429 /// this type can have nullability because it is dependent.
2430 bool canHaveNullability(bool ResultIfUnknown = true) const;
2431
2432 /// Retrieve the set of substitutions required when accessing a member
2433 /// of the Objective-C receiver type that is declared in the given context.
2434 ///
2435 /// \c *this is the type of the object we're operating on, e.g., the
2436 /// receiver for a message send or the base of a property access, and is
2437 /// expected to be of some object or object pointer type.
2438 ///
2439 /// \param dc The declaration context for which we are building up a
2440 /// substitution mapping, which should be an Objective-C class, extension,
2441 /// category, or method within.
2442 ///
2443 /// \returns an array of type arguments that can be substituted for
2444 /// the type parameters of the given declaration context in any type described
2445 /// within that context, or an empty optional to indicate that no
2446 /// substitution is required.
2447 Optional<ArrayRef<QualType>>
2448 getObjCSubstitutions(const DeclContext *dc) const;
2449
2450 /// Determines if this is an ObjC interface type that may accept type
2451 /// parameters.
2452 bool acceptsObjCTypeParams() const;
2453
2454 const char *getTypeClassName() const;
2455
2456 QualType getCanonicalTypeInternal() const {
2457 return CanonicalType;
2458 }
2459
2460 CanQualType getCanonicalTypeUnqualified() const; // in CanonicalType.h
2461 void dump() const;
2462 void dump(llvm::raw_ostream &OS, const ASTContext &Context) const;
2463};
2464
2465/// This will check for a TypedefType by removing any existing sugar
2466/// until it reaches a TypedefType or a non-sugared type.
2467template <> const TypedefType *Type::getAs() const;
2468
2469/// This will check for a TemplateSpecializationType by removing any
2470/// existing sugar until it reaches a TemplateSpecializationType or a
2471/// non-sugared type.
2472template <> const TemplateSpecializationType *Type::getAs() const;
2473
2474/// This will check for an AttributedType by removing any existing sugar
2475/// until it reaches an AttributedType or a non-sugared type.
2476template <> const AttributedType *Type::getAs() const;
2477
2478// We can do canonical leaf types faster, because we don't have to
2479// worry about preserving child type decoration.
2480#define TYPE(Class, Base)
2481#define LEAF_TYPE(Class) \
2482template <> inline const Class##Type *Type::getAs() const { \
2483 return dyn_cast<Class##Type>(CanonicalType); \
2484} \
2485template <> inline const Class##Type *Type::castAs() const { \
2486 return cast<Class##Type>(CanonicalType); \
2487}
2488#include "clang/AST/TypeNodes.inc"
2489
2490/// This class is used for builtin types like 'int'. Builtin
2491/// types are always canonical and have a literal name field.
2492class BuiltinType : public Type {
2493public:
2494 enum Kind {
2495// OpenCL image types
2496#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) Id,
2497#include "clang/Basic/OpenCLImageTypes.def"
2498// OpenCL extension types
2499#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) Id,
2500#include "clang/Basic/OpenCLExtensionTypes.def"
2501// SVE Types
2502#define SVE_TYPE(Name, Id, SingletonId) Id,
2503#include "clang/Basic/AArch64SVEACLETypes.def"
2504// PPC MMA Types
2505#define PPC_VECTOR_TYPE(Name, Id, Size) Id,
2506#include "clang/Basic/PPCTypes.def"
2507// RVV Types
2508#define RVV_TYPE(Name, Id, SingletonId) Id,
2509#include "clang/Basic/RISCVVTypes.def"
2510// All other builtin types
2511#define BUILTIN_TYPE(Id, SingletonId) Id,
2512#define LAST_BUILTIN_TYPE(Id) LastKind = Id
2513#include "clang/AST/BuiltinTypes.def"
2514 };
2515
2516private:
2517 friend class ASTContext; // ASTContext creates these.
2518
2519 BuiltinType(Kind K)
2520 : Type(Builtin, QualType(),
2521 K == Dependent ? TypeDependence::DependentInstantiation
2522 : TypeDependence::None) {
2523 BuiltinTypeBits.Kind = K;
2524 }
2525
2526public:
2527 Kind getKind() const { return static_cast<Kind>(BuiltinTypeBits.Kind); }
2528 StringRef getName(const PrintingPolicy &Policy) const;
2529
2530 const char *getNameAsCString(const PrintingPolicy &Policy) const {
2531 // The StringRef is null-terminated.
2532 StringRef str = getName(Policy);
2533 assert(!str.empty() && str.data()[str.size()] == '\0')(static_cast<void> (0));
2534 return str.data();
2535 }
2536
2537 bool isSugared() const { return false; }
2538 QualType desugar() const { return QualType(this, 0); }
2539
2540 bool isInteger() const {
2541 return getKind() >= Bool && getKind() <= Int128;
2542 }
2543
2544 bool isSignedInteger() const {
2545 return getKind() >= Char_S && getKind() <= Int128;
2546 }
2547
2548 bool isUnsignedInteger() const {
2549 return getKind() >= Bool && getKind() <= UInt128;
2550 }
2551
2552 bool isFloatingPoint() const {
2553 return getKind() >= Half && getKind() <= Float128;
2554 }
2555
2556 /// Determines whether the given kind corresponds to a placeholder type.
2557 static bool isPlaceholderTypeKind(Kind K) {
2558 return K >= Overload;
2559 }
2560
2561 /// Determines whether this type is a placeholder type, i.e. a type
2562 /// which cannot appear in arbitrary positions in a fully-formed
2563 /// expression.
2564 bool isPlaceholderType() const {
2565 return isPlaceholderTypeKind(getKind());
2566 }
2567
2568 /// Determines whether this type is a placeholder type other than
2569 /// Overload. Most placeholder types require only syntactic
2570 /// information about their context in order to be resolved (e.g.
2571 /// whether it is a call expression), which means they can (and
2572 /// should) be resolved in an earlier "phase" of analysis.
2573 /// Overload expressions sometimes pick up further information
2574 /// from their context, like whether the context expects a
2575 /// specific function-pointer type, and so frequently need
2576 /// special treatment.
2577 bool isNonOverloadPlaceholderType() const {
2578 return getKind() > Overload;
2579 }
2580
2581 static bool classof(const Type *T) { return T->getTypeClass() == Builtin; }
2582};
2583
2584/// Complex values, per C99 6.2.5p11. This supports the C99 complex
2585/// types (_Complex float etc) as well as the GCC integer complex extensions.
2586class ComplexType : public Type, public llvm::FoldingSetNode {
2587 friend class ASTContext; // ASTContext creates these.
2588
2589 QualType ElementType;
2590
2591 ComplexType(QualType Element, QualType CanonicalPtr)
2592 : Type(Complex, CanonicalPtr, Element->getDependence()),
2593 ElementType(Element) {}
2594
2595public:
2596 QualType getElementType() const { return ElementType; }
2597
2598 bool isSugared() const { return false; }
2599 QualType desugar() const { return QualType(this, 0); }
2600
2601 void Profile(llvm::FoldingSetNodeID &ID) {
2602 Profile(ID, getElementType());
2603 }
2604
2605 static void Profile(llvm::FoldingSetNodeID &ID, QualType Element) {
2606 ID.AddPointer(Element.getAsOpaquePtr());
2607 }
2608
2609 static bool classof(const Type *T) { return T->getTypeClass() == Complex; }
2610};
2611
2612/// Sugar for parentheses used when specifying types.
2613class ParenType : public Type, public llvm::FoldingSetNode {
2614 friend class ASTContext; // ASTContext creates these.
2615
2616 QualType Inner;
2617
2618 ParenType(QualType InnerType, QualType CanonType)
2619 : Type(Paren, CanonType, InnerType->getDependence()), Inner(InnerType) {}
2620
2621public:
2622 QualType getInnerType() const { return Inner; }
2623
2624 bool isSugared() const { return true; }
2625 QualType desugar() const { return getInnerType(); }
2626
2627 void Profile(llvm::FoldingSetNodeID &ID) {
2628 Profile(ID, getInnerType());
2629 }
2630
2631 static void Profile(llvm::FoldingSetNodeID &ID, QualType Inner) {
2632 Inner.Profile(ID);
2633 }
2634
2635 static bool classof(const Type *T) { return T->getTypeClass() == Paren; }
2636};
2637
2638/// PointerType - C99 6.7.5.1 - Pointer Declarators.
2639class PointerType : public Type, public llvm::FoldingSetNode {
2640 friend class ASTContext; // ASTContext creates these.
2641
2642 QualType PointeeType;
2643
2644 PointerType(QualType Pointee, QualType CanonicalPtr)
2645 : Type(Pointer, CanonicalPtr, Pointee->getDependence()),
2646 PointeeType(Pointee) {}
2647
2648public:
2649 QualType getPointeeType() const { return PointeeType; }
2650
2651 bool isSugared() const { return false; }
2652 QualType desugar() const { return QualType(this, 0); }
2653
2654 void Profile(llvm::FoldingSetNodeID &ID) {
2655 Profile(ID, getPointeeType());
2656 }
2657
2658 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) {
2659 ID.AddPointer(Pointee.getAsOpaquePtr());
2660 }
2661
2662 static bool classof(const Type *T) { return T->getTypeClass() == Pointer; }
2663};
2664
2665/// Represents a type which was implicitly adjusted by the semantic
2666/// engine for arbitrary reasons. For example, array and function types can
2667/// decay, and function types can have their calling conventions adjusted.
2668class AdjustedType : public Type, public llvm::FoldingSetNode {
2669 QualType OriginalTy;
2670 QualType AdjustedTy;
2671
2672protected:
2673 friend class ASTContext; // ASTContext creates these.
2674
2675 AdjustedType(TypeClass TC, QualType OriginalTy, QualType AdjustedTy,
2676 QualType CanonicalPtr)
2677 : Type(TC, CanonicalPtr, OriginalTy->getDependence()),
2678 OriginalTy(OriginalTy), AdjustedTy(AdjustedTy) {}
2679
2680public:
2681 QualType getOriginalType() const { return OriginalTy; }
2682 QualType getAdjustedType() const { return AdjustedTy; }
2683
2684 bool isSugared() const { return true; }
2685 QualType desugar() const { return AdjustedTy; }
2686
2687 void Profile(llvm::FoldingSetNodeID &ID) {
2688 Profile(ID, OriginalTy, AdjustedTy);
2689 }
2690
2691 static void Profile(llvm::FoldingSetNodeID &ID, QualType Orig, QualType New) {
2692 ID.AddPointer(Orig.getAsOpaquePtr());
2693 ID.AddPointer(New.getAsOpaquePtr());
2694 }
2695
2696 static bool classof(const Type *T) {
2697 return T->getTypeClass() == Adjusted || T->getTypeClass() == Decayed;
2698 }
2699};
2700
2701/// Represents a pointer type decayed from an array or function type.
2702class DecayedType : public AdjustedType {
2703 friend class ASTContext; // ASTContext creates these.
2704
2705 inline
2706 DecayedType(QualType OriginalType, QualType Decayed, QualType Canonical);
2707
2708public:
2709 QualType getDecayedType() const { return getAdjustedType(); }
2710
2711 inline QualType getPointeeType() const;
2712
2713 static bool classof(const Type *T) { return T->getTypeClass() == Decayed; }
2714};
2715
2716/// Pointer to a block type.
2717/// This type is to represent types syntactically represented as
2718/// "void (^)(int)", etc. Pointee is required to always be a function type.
2719class BlockPointerType : public Type, public llvm::FoldingSetNode {
2720 friend class ASTContext; // ASTContext creates these.
2721
2722 // Block is some kind of pointer type
2723 QualType PointeeType;
2724
2725 BlockPointerType(QualType Pointee, QualType CanonicalCls)
2726 : Type(BlockPointer, CanonicalCls, Pointee->getDependence()),
2727 PointeeType(Pointee) {}
2728
2729public:
2730 // Get the pointee type. Pointee is required to always be a function type.
2731 QualType getPointeeType() const { return PointeeType; }
2732
2733 bool isSugared() const { return false; }
2734 QualType desugar() const { return QualType(this, 0); }
2735
2736 void Profile(llvm::FoldingSetNodeID &ID) {
2737 Profile(ID, getPointeeType());
2738 }
2739
2740 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) {
2741 ID.AddPointer(Pointee.getAsOpaquePtr());
2742 }
2743
2744 static bool classof(const Type *T) {
2745 return T->getTypeClass() == BlockPointer;
2746 }
2747};
2748
2749/// Base for LValueReferenceType and RValueReferenceType
2750class ReferenceType : public Type, public llvm::FoldingSetNode {
2751 QualType PointeeType;
2752
2753protected:
2754 ReferenceType(TypeClass tc, QualType Referencee, QualType CanonicalRef,
2755 bool SpelledAsLValue)
2756 : Type(tc, CanonicalRef, Referencee->getDependence()),
2757 PointeeType(Referencee) {
2758 ReferenceTypeBits.SpelledAsLValue = SpelledAsLValue;
2759 ReferenceTypeBits.InnerRef = Referencee->isReferenceType();
2760 }
2761
2762public:
2763 bool isSpelledAsLValue() const { return ReferenceTypeBits.SpelledAsLValue; }
2764 bool isInnerRef() const { return ReferenceTypeBits.InnerRef; }
2765
2766 QualType getPointeeTypeAsWritten() const { return PointeeType; }
2767
2768 QualType getPointeeType() const {
2769 // FIXME: this might strip inner qualifiers; okay?
2770 const ReferenceType *T = this;
2771 while (T->isInnerRef())
2772 T = T->PointeeType->castAs<ReferenceType>();
2773 return T->PointeeType;
2774 }
2775
2776 void Profile(llvm::FoldingSetNodeID &ID) {
2777 Profile(ID, PointeeType, isSpelledAsLValue());
2778 }
2779
2780 static void Profile(llvm::FoldingSetNodeID &ID,
2781 QualType Referencee,
2782 bool SpelledAsLValue) {
2783 ID.AddPointer(Referencee.getAsOpaquePtr());
2784 ID.AddBoolean(SpelledAsLValue);
2785 }
2786
2787 static bool classof(const Type *T) {
2788 return T->getTypeClass() == LValueReference ||
2789 T->getTypeClass() == RValueReference;
2790 }
2791};
2792
2793/// An lvalue reference type, per C++11 [dcl.ref].
2794class LValueReferenceType : public ReferenceType {
2795 friend class ASTContext; // ASTContext creates these
2796
2797 LValueReferenceType(QualType Referencee, QualType CanonicalRef,
2798 bool SpelledAsLValue)
2799 : ReferenceType(LValueReference, Referencee, CanonicalRef,
2800 SpelledAsLValue) {}
2801
2802public:
2803 bool isSugared() const { return false; }
2804 QualType desugar() const { return QualType(this, 0); }
2805
2806 static bool classof(const Type *T) {
2807 return T->getTypeClass() == LValueReference;
2808 }
2809};
2810
2811/// An rvalue reference type, per C++11 [dcl.ref].
2812class RValueReferenceType : public ReferenceType {
2813 friend class ASTContext; // ASTContext creates these
2814
2815 RValueReferenceType(QualType Referencee, QualType CanonicalRef)
2816 : ReferenceType(RValueReference, Referencee, CanonicalRef, false) {}
2817
2818public:
2819 bool isSugared() const { return false; }
2820 QualType desugar() const { return QualType(this, 0); }
2821
2822 static bool classof(const Type *T) {
2823 return T->getTypeClass() == RValueReference;
2824 }
2825};
2826
2827/// A pointer to member type per C++ 8.3.3 - Pointers to members.
2828///
2829/// This includes both pointers to data members and pointer to member functions.
2830class MemberPointerType : public Type, public llvm::FoldingSetNode {
2831 friend class ASTContext; // ASTContext creates these.
2832
2833 QualType PointeeType;
2834
2835 /// The class of which the pointee is a member. Must ultimately be a
2836 /// RecordType, but could be a typedef or a template parameter too.
2837 const Type *Class;
2838
2839 MemberPointerType(QualType Pointee, const Type *Cls, QualType CanonicalPtr)
2840 : Type(MemberPointer, CanonicalPtr,
2841 (Cls->getDependence() & ~TypeDependence::VariablyModified) |
2842 Pointee->getDependence()),
2843 PointeeType(Pointee), Class(Cls) {}
2844
2845public:
2846 QualType getPointeeType() const { return PointeeType; }
2847
2848 /// Returns true if the member type (i.e. the pointee type) is a
2849 /// function type rather than a data-member type.
2850 bool isMemberFunctionPointer() const {
2851 return PointeeType->isFunctionProtoType();
2852 }
2853
2854 /// Returns true if the member type (i.e. the pointee type) is a
2855 /// data type rather than a function type.
2856 bool isMemberDataPointer() const {
2857 return !PointeeType->isFunctionProtoType();
2858 }
2859
2860 const Type *getClass() const { return Class; }
2861 CXXRecordDecl *getMostRecentCXXRecordDecl() const;
2862
2863 bool isSugared() const { return false; }
2864 QualType desugar() const { return QualType(this, 0); }
2865
2866 void Profile(llvm::FoldingSetNodeID &ID) {
2867 Profile(ID, getPointeeType(), getClass());
2868 }
2869
2870 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee,
2871 const Type *Class) {
2872 ID.AddPointer(Pointee.getAsOpaquePtr());
2873 ID.AddPointer(Class);
2874 }
2875
2876 static bool classof(const Type *T) {
2877 return T->getTypeClass() == MemberPointer;
2878 }
2879};
2880
2881/// Represents an array type, per C99 6.7.5.2 - Array Declarators.
2882class ArrayType : public Type, public llvm::FoldingSetNode {
2883public:
2884 /// Capture whether this is a normal array (e.g. int X[4])
2885 /// an array with a static size (e.g. int X[static 4]), or an array
2886 /// with a star size (e.g. int X[*]).
2887 /// 'static' is only allowed on function parameters.
2888 enum ArraySizeModifier {
2889 Normal, Static, Star
2890 };
2891
2892private:
2893 /// The element type of the array.
2894 QualType ElementType;
2895
2896protected:
2897 friend class ASTContext; // ASTContext creates these.
2898
2899 ArrayType(TypeClass tc, QualType et, QualType can, ArraySizeModifier sm,
2900 unsigned tq, const Expr *sz = nullptr);
2901
2902public:
2903 QualType getElementType() const { return ElementType; }
2904
2905 ArraySizeModifier getSizeModifier() const {
2906 return ArraySizeModifier(ArrayTypeBits.SizeModifier);
2907 }
2908
2909 Qualifiers getIndexTypeQualifiers() const {
2910 return Qualifiers::fromCVRMask(getIndexTypeCVRQualifiers());
2911 }
2912
2913 unsigned getIndexTypeCVRQualifiers() const {
2914 return ArrayTypeBits.IndexTypeQuals;
2915 }
2916
2917 static bool classof(const Type *T) {
2918 return T->getTypeClass() == ConstantArray ||
2919 T->getTypeClass() == VariableArray ||
2920 T->getTypeClass() == IncompleteArray ||
2921 T->getTypeClass() == DependentSizedArray;
2922 }
2923};
2924
2925/// Represents the canonical version of C arrays with a specified constant size.
2926/// For example, the canonical type for 'int A[4 + 4*100]' is a
2927/// ConstantArrayType where the element type is 'int' and the size is 404.
2928class ConstantArrayType final
2929 : public ArrayType,
2930 private llvm::TrailingObjects<ConstantArrayType, const Expr *> {
2931 friend class ASTContext; // ASTContext creates these.
2932 friend TrailingObjects;
2933
2934 llvm::APInt Size; // Allows us to unique the type.
2935
2936 ConstantArrayType(QualType et, QualType can, const llvm::APInt &size,
2937 const Expr *sz, ArraySizeModifier sm, unsigned tq)
2938 : ArrayType(ConstantArray, et, can, sm, tq, sz), Size(size) {
2939 ConstantArrayTypeBits.HasStoredSizeExpr = sz != nullptr;
2940 if (ConstantArrayTypeBits.HasStoredSizeExpr) {
2941 assert(!can.isNull() && "canonical constant array should not have size")(static_cast<void> (0));
2942 *getTrailingObjects<const Expr*>() = sz;
2943 }
2944 }
2945
2946 unsigned numTrailingObjects(OverloadToken<const Expr*>) const {
2947 return ConstantArrayTypeBits.HasStoredSizeExpr;
2948 }
2949
2950public:
2951 const llvm::APInt &getSize() const { return Size; }
2952 const Expr *getSizeExpr() const {
2953 return ConstantArrayTypeBits.HasStoredSizeExpr
2954 ? *getTrailingObjects<const Expr *>()
2955 : nullptr;
2956 }
2957 bool isSugared() const { return false; }
2958 QualType desugar() const { return QualType(this, 0); }
2959
2960 /// Determine the number of bits required to address a member of
2961 // an array with the given element type and number of elements.
2962 static unsigned getNumAddressingBits(const ASTContext &Context,
2963 QualType ElementType,
2964 const llvm::APInt &NumElements);
2965
2966 /// Determine the maximum number of active bits that an array's size
2967 /// can require, which limits the maximum size of the array.
2968 static unsigned getMaxSizeBits(const ASTContext &Context);
2969
2970 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx) {
2971 Profile(ID, Ctx, getElementType(), getSize(), getSizeExpr(),
2972 getSizeModifier(), getIndexTypeCVRQualifiers());
2973 }
2974
2975 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx,
2976 QualType ET, const llvm::APInt &ArraySize,
2977 const Expr *SizeExpr, ArraySizeModifier SizeMod,
2978 unsigned TypeQuals);
2979
2980 static bool classof(const Type *T) {
2981 return T->getTypeClass() == ConstantArray;
2982 }
2983};
2984
2985/// Represents a C array with an unspecified size. For example 'int A[]' has
2986/// an IncompleteArrayType where the element type is 'int' and the size is
2987/// unspecified.
2988class IncompleteArrayType : public ArrayType {
2989 friend class ASTContext; // ASTContext creates these.
2990
2991 IncompleteArrayType(QualType et, QualType can,
2992 ArraySizeModifier sm, unsigned tq)
2993 : ArrayType(IncompleteArray, et, can, sm, tq) {}
2994
2995public:
2996 friend class StmtIteratorBase;
2997
2998 bool isSugared() const { return false; }
2999 QualType desugar() const { return QualType(this, 0); }
3000
3001 static bool classof(const Type *T) {
3002 return T->getTypeClass() == IncompleteArray;
3003 }
3004
3005 void Profile(llvm::FoldingSetNodeID &ID) {
3006 Profile(ID, getElementType(), getSizeModifier(),
3007 getIndexTypeCVRQualifiers());
3008 }
3009
3010 static void Profile(llvm::FoldingSetNodeID &ID, QualType ET,
3011 ArraySizeModifier SizeMod, unsigned TypeQuals) {
3012 ID.AddPointer(ET.getAsOpaquePtr());
3013 ID.AddInteger(SizeMod);
3014 ID.AddInteger(TypeQuals);
3015 }
3016};
3017
3018/// Represents a C array with a specified size that is not an
3019/// integer-constant-expression. For example, 'int s[x+foo()]'.
3020/// Since the size expression is an arbitrary expression, we store it as such.
3021///
3022/// Note: VariableArrayType's aren't uniqued (since the expressions aren't) and
3023/// should not be: two lexically equivalent variable array types could mean
3024/// different things, for example, these variables do not have the same type
3025/// dynamically:
3026///
3027/// void foo(int x) {
3028/// int Y[x];
3029/// ++x;
3030/// int Z[x];
3031/// }
3032class VariableArrayType : public ArrayType {
3033 friend class ASTContext; // ASTContext creates these.
3034
3035 /// An assignment-expression. VLA's are only permitted within
3036 /// a function block.
3037 Stmt *SizeExpr;
3038
3039 /// The range spanned by the left and right array brackets.
3040 SourceRange Brackets;
3041
3042 VariableArrayType(QualType et, QualType can, Expr *e,
3043 ArraySizeModifier sm, unsigned tq,
3044 SourceRange brackets)
3045 : ArrayType(VariableArray, et, can, sm, tq, e),
3046 SizeExpr((Stmt*) e), Brackets(brackets) {}
3047
3048public:
3049 friend class StmtIteratorBase;
3050
3051 Expr *getSizeExpr() const {
3052 // We use C-style casts instead of cast<> here because we do not wish
3053 // to have a dependency of Type.h on Stmt.h/Expr.h.
3054 return (Expr*) SizeExpr;
3055 }
3056
3057 SourceRange getBracketsRange() const { return Brackets; }
3058 SourceLocation getLBracketLoc() const { return Brackets.getBegin(); }
3059 SourceLocation getRBracketLoc() const { return Brackets.getEnd(); }
3060
3061 bool isSugared() const { return false; }
3062 QualType desugar() const { return QualType(this, 0); }
3063
3064 static bool classof(const Type *T) {
3065 return T->getTypeClass() == VariableArray;
3066 }
3067
3068 void Profile(llvm::FoldingSetNodeID &ID) {
3069 llvm_unreachable("Cannot unique VariableArrayTypes.")__builtin_unreachable();
3070 }
3071};
3072
3073/// Represents an array type in C++ whose size is a value-dependent expression.
3074///
3075/// For example:
3076/// \code
3077/// template<typename T, int Size>
3078/// class array {
3079/// T data[Size];
3080/// };
3081/// \endcode
3082///
3083/// For these types, we won't actually know what the array bound is
3084/// until template instantiation occurs, at which point this will
3085/// become either a ConstantArrayType or a VariableArrayType.
3086class DependentSizedArrayType : public ArrayType {
3087 friend class ASTContext; // ASTContext creates these.
3088
3089 const ASTContext &Context;
3090
3091 /// An assignment expression that will instantiate to the
3092 /// size of the array.
3093 ///
3094 /// The expression itself might be null, in which case the array
3095 /// type will have its size deduced from an initializer.
3096 Stmt *SizeExpr;
3097
3098 /// The range spanned by the left and right array brackets.
3099 SourceRange Brackets;
3100
3101 DependentSizedArrayType(const ASTContext &Context, QualType et, QualType can,
3102 Expr *e, ArraySizeModifier sm, unsigned tq,
3103 SourceRange brackets);
3104
3105public:
3106 friend class StmtIteratorBase;
3107
3108 Expr *getSizeExpr() const {
3109 // We use C-style casts instead of cast<> here because we do not wish
3110 // to have a dependency of Type.h on Stmt.h/Expr.h.
3111 return (Expr*) SizeExpr;
3112 }
3113
3114 SourceRange getBracketsRange() const { return Brackets; }
3115 SourceLocation getLBracketLoc() const { return Brackets.getBegin(); }
3116 SourceLocation getRBracketLoc() const { return Brackets.getEnd(); }
3117
3118 bool isSugared() const { return false; }
3119 QualType desugar() const { return QualType(this, 0); }
3120
3121 static bool classof(const Type *T) {
3122 return T->getTypeClass() == DependentSizedArray;
3123 }
3124
3125 void Profile(llvm::FoldingSetNodeID &ID) {
3126 Profile(ID, Context, getElementType(),
3127 getSizeModifier(), getIndexTypeCVRQualifiers(), getSizeExpr());
3128 }
3129
3130 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
3131 QualType ET, ArraySizeModifier SizeMod,
3132 unsigned TypeQuals, Expr *E);
3133};
3134
3135/// Represents an extended address space qualifier where the input address space
3136/// value is dependent. Non-dependent address spaces are not represented with a
3137/// special Type subclass; they are stored on an ExtQuals node as part of a QualType.
3138///
3139/// For example:
3140/// \code
3141/// template<typename T, int AddrSpace>
3142/// class AddressSpace {
3143/// typedef T __attribute__((address_space(AddrSpace))) type;
3144/// }
3145/// \endcode
3146class DependentAddressSpaceType : public Type, public llvm::FoldingSetNode {
3147 friend class ASTContext;
3148
3149 const ASTContext &Context;
3150 Expr *AddrSpaceExpr;
3151 QualType PointeeType;
3152 SourceLocation loc;
3153
3154 DependentAddressSpaceType(const ASTContext &Context, QualType PointeeType,
3155 QualType can, Expr *AddrSpaceExpr,
3156 SourceLocation loc);
3157
3158public:
3159 Expr *getAddrSpaceExpr() const { return AddrSpaceExpr; }
3160 QualType getPointeeType() const { return PointeeType; }
3161 SourceLocation getAttributeLoc() const { return loc; }
3162
3163 bool isSugared() const { return false; }
3164 QualType desugar() const { return QualType(this, 0); }
3165
3166 static bool classof(const Type *T) {
3167 return T->getTypeClass() == DependentAddressSpace;
3168 }
3169
3170 void Profile(llvm::FoldingSetNodeID &ID) {
3171 Profile(ID, Context, getPointeeType(), getAddrSpaceExpr());
3172 }
3173
3174 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
3175 QualType PointeeType, Expr *AddrSpaceExpr);
3176};
3177
3178/// Represents an extended vector type where either the type or size is
3179/// dependent.
3180///
3181/// For example:
3182/// \code
3183/// template<typename T, int Size>
3184/// class vector {
3185/// typedef T __attribute__((ext_vector_type(Size))) type;
3186/// }
3187/// \endcode
3188class DependentSizedExtVectorType : public Type, public llvm::FoldingSetNode {
3189 friend class ASTContext;
3190
3191 const ASTContext &Context;
3192 Expr *SizeExpr;
3193
3194 /// The element type of the array.
3195 QualType ElementType;
3196
3197 SourceLocation loc;
3198
3199 DependentSizedExtVectorType(const ASTContext &Context, QualType ElementType,
3200 QualType can, Expr *SizeExpr, SourceLocation loc);
3201
3202public:
3203 Expr *getSizeExpr() const { return SizeExpr; }
3204 QualType getElementType() const { return ElementType; }
3205 SourceLocation getAttributeLoc() const { return loc; }
3206
3207 bool isSugared() const { return false; }
3208 QualType desugar() const { return QualType(this, 0); }
3209
3210 static bool classof(const Type *T) {
3211 return T->getTypeClass() == DependentSizedExtVector;
3212 }
3213
3214 void Profile(llvm::FoldingSetNodeID &ID) {
3215 Profile(ID, Context, getElementType(), getSizeExpr());
3216 }
3217
3218 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
3219 QualType ElementType, Expr *SizeExpr);
3220};
3221
3222
3223/// Represents a GCC generic vector type. This type is created using
3224/// __attribute__((vector_size(n)), where "n" specifies the vector size in
3225/// bytes; or from an Altivec __vector or vector declaration.
3226/// Since the constructor takes the number of vector elements, the
3227/// client is responsible for converting the size into the number of elements.
3228class VectorType : public Type, public llvm::FoldingSetNode {
3229public:
3230 enum VectorKind {
3231 /// not a target-specific vector type
3232 GenericVector,
3233
3234 /// is AltiVec vector
3235 AltiVecVector,
3236
3237 /// is AltiVec 'vector Pixel'
3238 AltiVecPixel,
3239
3240 /// is AltiVec 'vector bool ...'
3241 AltiVecBool,
3242
3243 /// is ARM Neon vector
3244 NeonVector,
3245
3246 /// is ARM Neon polynomial vector
3247 NeonPolyVector,
3248
3249 /// is AArch64 SVE fixed-length data vector
3250 SveFixedLengthDataVector,
3251
3252 /// is AArch64 SVE fixed-length predicate vector
3253 SveFixedLengthPredicateVector
3254 };
3255
3256protected:
3257 friend class ASTContext; // ASTContext creates these.
3258
3259 /// The element type of the vector.
3260 QualType ElementType;
3261
3262 VectorType(QualType vecType, unsigned nElements, QualType canonType,
3263 VectorKind vecKind);
3264
3265 VectorType(TypeClass tc, QualType vecType, unsigned nElements,
3266 QualType canonType, VectorKind vecKind);
3267
3268public:
3269 QualType getElementType() const { return ElementType; }
3270 unsigned getNumElements() const { return VectorTypeBits.NumElements; }
3271
3272 bool isSugared() const { return false; }
3273 QualType desugar() const { return QualType(this, 0); }
3274
3275 VectorKind getVectorKind() const {
3276 return VectorKind(VectorTypeBits.VecKind);
3277 }
3278
3279 void Profile(llvm::FoldingSetNodeID &ID) {
3280 Profile(ID, getElementType(), getNumElements(),
3281 getTypeClass(), getVectorKind());
3282 }
3283
3284 static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType,
3285 unsigned NumElements, TypeClass TypeClass,
3286 VectorKind VecKind) {
3287 ID.AddPointer(ElementType.getAsOpaquePtr());
3288 ID.AddInteger(NumElements);
3289 ID.AddInteger(TypeClass);
3290 ID.AddInteger(VecKind);
3291 }
3292
3293 static bool classof(const Type *T) {
3294 return T->getTypeClass() == Vector || T->getTypeClass() == ExtVector;
3295 }
3296};
3297
3298/// Represents a vector type where either the type or size is dependent.
3299////
3300/// For example:
3301/// \code
3302/// template<typename T, int Size>
3303/// class vector {
3304/// typedef T __attribute__((vector_size(Size))) type;
3305/// }
3306/// \endcode
3307class DependentVectorType : public Type, public llvm::FoldingSetNode {
3308 friend class ASTContext;
3309
3310 const ASTContext &Context;
3311 QualType ElementType;
3312 Expr *SizeExpr;
3313 SourceLocation Loc;
3314
3315 DependentVectorType(const ASTContext &Context, QualType ElementType,
3316 QualType CanonType, Expr *SizeExpr,
3317 SourceLocation Loc, VectorType::VectorKind vecKind);
3318
3319public:
3320 Expr *getSizeExpr() const { return SizeExpr; }
3321 QualType getElementType() const { return ElementType; }
3322 SourceLocation getAttributeLoc() const { return Loc; }
3323 VectorType::VectorKind getVectorKind() const {
3324 return VectorType::VectorKind(VectorTypeBits.VecKind);
3325 }
3326
3327 bool isSugared() const { return false; }
3328 QualType desugar() const { return QualType(this, 0); }
3329
3330 static bool classof(const Type *T) {
3331 return T->getTypeClass() == DependentVector;
3332 }
3333
3334 void Profile(llvm::FoldingSetNodeID &ID) {
3335 Profile(ID, Context, getElementType(), getSizeExpr(), getVectorKind());
3336 }
3337
3338 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
3339 QualType ElementType, const Expr *SizeExpr,
3340 VectorType::VectorKind VecKind);
3341};
3342
3343/// ExtVectorType - Extended vector type. This type is created using
3344/// __attribute__((ext_vector_type(n)), where "n" is the number of elements.
3345/// Unlike vector_size, ext_vector_type is only allowed on typedef's. This
3346/// class enables syntactic extensions, like Vector Components for accessing
3347/// points (as .xyzw), colors (as .rgba), and textures (modeled after OpenGL
3348/// Shading Language).
3349class ExtVectorType : public VectorType {
3350 friend class ASTContext; // ASTContext creates these.
3351
3352 ExtVectorType(QualType vecType, unsigned nElements, QualType canonType)
3353 : VectorType(ExtVector, vecType, nElements, canonType, GenericVector) {}
3354
3355public:
3356 static int getPointAccessorIdx(char c) {
3357 switch (c) {
3358 default: return -1;
3359 case 'x': case 'r': return 0;
3360 case 'y': case 'g': return 1;
3361 case 'z': case 'b': return 2;
3362 case 'w': case 'a': return 3;
3363 }
3364 }
3365
3366 static int getNumericAccessorIdx(char c) {
3367 switch (c) {
3368 default: return -1;
3369 case '0': return 0;
3370 case '1': return 1;
3371 case '2': return 2;
3372 case '3': return 3;
3373 case '4': return 4;
3374 case '5': return 5;
3375 case '6': return 6;
3376 case '7': return 7;
3377 case '8': return 8;
3378 case '9': return 9;
3379 case 'A':
3380 case 'a': return 10;
3381 case 'B':
3382 case 'b': return 11;
3383 case 'C':
3384 case 'c': return 12;
3385 case 'D':
3386 case 'd': return 13;
3387 case 'E':
3388 case 'e': return 14;
3389 case 'F':
3390 case 'f': return 15;
3391 }
3392 }
3393
3394 static int getAccessorIdx(char c, bool isNumericAccessor) {
3395 if (isNumericAccessor)
3396 return getNumericAccessorIdx(c);
3397 else
3398 return getPointAccessorIdx(c);
3399 }
3400
3401 bool isAccessorWithinNumElements(char c, bool isNumericAccessor) const {
3402 if (int idx = getAccessorIdx(c, isNumericAccessor)+1)
3403 return unsigned(idx-1) < getNumElements();
3404 return false;
3405 }
3406
3407 bool isSugared() const { return false; }
3408 QualType desugar() const { return QualType(this, 0); }
3409
3410 static bool classof(const Type *T) {
3411 return T->getTypeClass() == ExtVector;
3412 }
3413};
3414
3415/// Represents a matrix type, as defined in the Matrix Types clang extensions.
3416/// __attribute__((matrix_type(rows, columns))), where "rows" specifies
3417/// number of rows and "columns" specifies the number of columns.
3418class MatrixType : public Type, public llvm::FoldingSetNode {
3419protected:
3420 friend class ASTContext;
3421
3422 /// The element type of the matrix.
3423 QualType ElementType;
3424
3425 MatrixType(QualType ElementTy, QualType CanonElementTy);
3426
3427 MatrixType(TypeClass TypeClass, QualType ElementTy, QualType CanonElementTy,
3428 const Expr *RowExpr = nullptr, const Expr *ColumnExpr = nullptr);
3429
3430public:
3431 /// Returns type of the elements being stored in the matrix
3432 QualType getElementType() const { return ElementType; }
3433
3434 /// Valid elements types are the following:
3435 /// * an integer type (as in C2x 6.2.5p19), but excluding enumerated types
3436 /// and _Bool
3437 /// * the standard floating types float or double
3438 /// * a half-precision floating point type, if one is supported on the target
3439 static bool isValidElementType(QualType T) {
3440 return T->isDependentType() ||
3441 (T->isRealType() && !T->isBooleanType() && !T->isEnumeralType());
3442 }
3443
3444 bool isSugared() const { return false; }
3445 QualType desugar() const { return QualType(this, 0); }
3446
3447 static bool classof(const Type *T) {
3448 return T->getTypeClass() == ConstantMatrix ||
3449 T->getTypeClass() == DependentSizedMatrix;
3450 }
3451};
3452
3453/// Represents a concrete matrix type with constant number of rows and columns
3454class ConstantMatrixType final : public MatrixType {
3455protected:
3456 friend class ASTContext;
3457
3458 /// Number of rows and columns.
3459 unsigned NumRows;
3460 unsigned NumColumns;
3461
3462 static constexpr unsigned MaxElementsPerDimension = (1 << 20) - 1;
3463
3464 ConstantMatrixType(QualType MatrixElementType, unsigned NRows,
3465 unsigned NColumns, QualType CanonElementType);
3466
3467 ConstantMatrixType(TypeClass typeClass, QualType MatrixType, unsigned NRows,
3468 unsigned NColumns, QualType CanonElementType);
3469
3470public:
3471 /// Returns the number of rows in the matrix.
3472 unsigned getNumRows() const { return NumRows; }
3473
3474 /// Returns the number of columns in the matrix.
3475 unsigned getNumColumns() const { return NumColumns; }
3476
3477 /// Returns the number of elements required to embed the matrix into a vector.
3478 unsigned getNumElementsFlattened() const {
3479 return getNumRows() * getNumColumns();
3480 }
3481
3482 /// Returns true if \p NumElements is a valid matrix dimension.
3483 static constexpr bool isDimensionValid(size_t NumElements) {
3484 return NumElements > 0 && NumElements <= MaxElementsPerDimension;
3485 }
3486
3487 /// Returns the maximum number of elements per dimension.
3488 static constexpr unsigned getMaxElementsPerDimension() {
3489 return MaxElementsPerDimension;
3490 }
3491
3492 void Profile(llvm::FoldingSetNodeID &ID) {
3493 Profile(ID, getElementType(), getNumRows(), getNumColumns(),
3494 getTypeClass());
3495 }
3496
3497 static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType,
3498 unsigned NumRows, unsigned NumColumns,
3499 TypeClass TypeClass) {
3500 ID.AddPointer(ElementType.getAsOpaquePtr());
3501 ID.AddInteger(NumRows);
3502 ID.AddInteger(NumColumns);
3503 ID.AddInteger(TypeClass);
3504 }
3505
3506 static bool classof(const Type *T) {
3507 return T->getTypeClass() == ConstantMatrix;
3508 }
3509};
3510
3511/// Represents a matrix type where the type and the number of rows and columns
3512/// is dependent on a template.
3513class DependentSizedMatrixType final : public MatrixType {
3514 friend class ASTContext;
3515
3516 const ASTContext &Context;
3517 Expr *RowExpr;
3518 Expr *ColumnExpr;
3519
3520 SourceLocation loc;
3521
3522 DependentSizedMatrixType(const ASTContext &Context, QualType ElementType,
3523 QualType CanonicalType, Expr *RowExpr,
3524 Expr *ColumnExpr, SourceLocation loc);
3525
3526public:
3527 Expr *getRowExpr() const { return RowExpr; }
3528 Expr *getColumnExpr() const { return ColumnExpr; }
3529 SourceLocation getAttributeLoc() const { return loc; }
3530
3531 static bool classof(const Type *T) {
3532 return T->getTypeClass() == DependentSizedMatrix;
3533 }
3534
3535 void Profile(llvm::FoldingSetNodeID &ID) {
3536 Profile(ID, Context, getElementType(), getRowExpr(), getColumnExpr());
3537 }
3538
3539 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
3540 QualType ElementType, Expr *RowExpr, Expr *ColumnExpr);
3541};
3542
3543/// FunctionType - C99 6.7.5.3 - Function Declarators. This is the common base
3544/// class of FunctionNoProtoType and FunctionProtoType.
3545class FunctionType : public Type {
3546 // The type returned by the function.
3547 QualType ResultType;
3548
3549public:
3550 /// Interesting information about a specific parameter that can't simply
3551 /// be reflected in parameter's type. This is only used by FunctionProtoType
3552 /// but is in FunctionType to make this class available during the
3553 /// specification of the bases of FunctionProtoType.
3554 ///
3555 /// It makes sense to model language features this way when there's some
3556 /// sort of parameter-specific override (such as an attribute) that
3557 /// affects how the function is called. For example, the ARC ns_consumed
3558 /// attribute changes whether a parameter is passed at +0 (the default)
3559 /// or +1 (ns_consumed). This must be reflected in the function type,
3560 /// but isn't really a change to the parameter type.
3561 ///
3562 /// One serious disadvantage of modelling language features this way is
3563 /// that they generally do not work with language features that attempt
3564 /// to destructure types. For example, template argument deduction will
3565 /// not be able to match a parameter declared as
3566 /// T (*)(U)
3567 /// against an argument of type
3568 /// void (*)(__attribute__((ns_consumed)) id)
3569 /// because the substitution of T=void, U=id into the former will
3570 /// not produce the latter.
3571 class ExtParameterInfo {
3572 enum {
3573 ABIMask = 0x0F,
3574 IsConsumed = 0x10,
3575 HasPassObjSize = 0x20,
3576 IsNoEscape = 0x40,
3577 };
3578 unsigned char Data = 0;
3579
3580 public:
3581 ExtParameterInfo() = default;
3582
3583 /// Return the ABI treatment of this parameter.
3584 ParameterABI getABI() const { return ParameterABI(Data & ABIMask); }
3585 ExtParameterInfo withABI(ParameterABI kind) const {
3586 ExtParameterInfo copy = *this;
3587 copy.Data = (copy.Data & ~ABIMask) | unsigned(kind);
3588 return copy;
3589 }
3590
3591 /// Is this parameter considered "consumed" by Objective-C ARC?
3592 /// Consumed parameters must have retainable object type.
3593 bool isConsumed() const { return (Data & IsConsumed); }
3594 ExtParameterInfo withIsConsumed(bool consumed) const {
3595 ExtParameterInfo copy = *this;
3596 if (consumed)
3597 copy.Data |= IsConsumed;
3598 else
3599 copy.Data &= ~IsConsumed;
3600 return copy;
3601 }
3602
3603 bool hasPassObjectSize() const { return Data & HasPassObjSize; }
3604 ExtParameterInfo withHasPassObjectSize() const {
3605 ExtParameterInfo Copy = *this;
3606 Copy.Data |= HasPassObjSize;
3607 return Copy;
3608 }
3609
3610 bool isNoEscape() const { return Data & IsNoEscape; }
3611 ExtParameterInfo withIsNoEscape(bool NoEscape) const {
3612 ExtParameterInfo Copy = *this;
3613 if (NoEscape)
3614 Copy.Data |= IsNoEscape;
3615 else
3616 Copy.Data &= ~IsNoEscape;
3617 return Copy;
3618 }
3619
3620 unsigned char getOpaqueValue() const { return Data; }
3621 static ExtParameterInfo getFromOpaqueValue(unsigned char data) {
3622 ExtParameterInfo result;
3623 result.Data = data;
3624 return result;
3625 }
3626
3627 friend bool operator==(ExtParameterInfo lhs, ExtParameterInfo rhs) {
3628 return lhs.Data == rhs.Data;
3629 }
3630
3631 friend bool operator!=(ExtParameterInfo lhs, ExtParameterInfo rhs) {
3632 return lhs.Data != rhs.Data;
3633 }
3634 };
3635
3636 /// A class which abstracts out some details necessary for
3637 /// making a call.
3638 ///
3639 /// It is not actually used directly for storing this information in
3640 /// a FunctionType, although FunctionType does currently use the
3641 /// same bit-pattern.
3642 ///
3643 // If you add a field (say Foo), other than the obvious places (both,
3644 // constructors, compile failures), what you need to update is
3645 // * Operator==
3646 // * getFoo
3647 // * withFoo
3648 // * functionType. Add Foo, getFoo.
3649 // * ASTContext::getFooType
3650 // * ASTContext::mergeFunctionTypes
3651 // * FunctionNoProtoType::Profile
3652 // * FunctionProtoType::Profile
3653 // * TypePrinter::PrintFunctionProto
3654 // * AST read and write
3655 // * Codegen
3656 class ExtInfo {
3657 friend class FunctionType;
3658
3659 // Feel free to rearrange or add bits, but if you go over 16, you'll need to
3660 // adjust the Bits field below, and if you add bits, you'll need to adjust
3661 // Type::FunctionTypeBitfields::ExtInfo as well.
3662
3663 // | CC |noreturn|produces|nocallersavedregs|regparm|nocfcheck|cmsenscall|
3664 // |0 .. 4| 5 | 6 | 7 |8 .. 10| 11 | 12 |
3665 //
3666 // regparm is either 0 (no regparm attribute) or the regparm value+1.
3667 enum { CallConvMask = 0x1F };
3668 enum { NoReturnMask = 0x20 };
3669 enum { ProducesResultMask = 0x40 };
3670 enum { NoCallerSavedRegsMask = 0x80 };
3671 enum {
3672 RegParmMask = 0x700,
3673 RegParmOffset = 8
3674 };
3675 enum { NoCfCheckMask = 0x800 };
3676 enum { CmseNSCallMask = 0x1000 };
3677 uint16_t Bits = CC_C;
3678
3679 ExtInfo(unsigned Bits) : Bits(static_cast<uint16_t>(Bits)) {}
3680
3681 public:
3682 // Constructor with no defaults. Use this when you know that you
3683 // have all the elements (when reading an AST file for example).
3684 ExtInfo(bool noReturn, bool hasRegParm, unsigned regParm, CallingConv cc,
3685 bool producesResult, bool noCallerSavedRegs, bool NoCfCheck,
3686 bool cmseNSCall) {
3687 assert((!hasRegParm || regParm < 7) && "Invalid regparm value")(static_cast<void> (0));
3688 Bits = ((unsigned)cc) | (noReturn ? NoReturnMask : 0) |
3689 (producesResult ? ProducesResultMask : 0) |
3690 (noCallerSavedRegs ? NoCallerSavedRegsMask : 0) |
3691 (hasRegParm ? ((regParm + 1) << RegParmOffset) : 0) |
3692 (NoCfCheck ? NoCfCheckMask : 0) |
3693 (cmseNSCall ? CmseNSCallMask : 0);
3694 }
3695
3696 // Constructor with all defaults. Use when for example creating a
3697 // function known to use defaults.
3698 ExtInfo() = default;
3699
3700 // Constructor with just the calling convention, which is an important part
3701 // of the canonical type.
3702 ExtInfo(CallingConv CC) : Bits(CC) {}
3703
3704 bool getNoReturn() const { return Bits & NoReturnMask; }
3705 bool getProducesResult() const { return Bits & ProducesResultMask; }
3706 bool getCmseNSCall() const { return Bits & CmseNSCallMask; }
3707 bool getNoCallerSavedRegs() const { return Bits & NoCallerSavedRegsMask; }
3708 bool getNoCfCheck() const { return Bits & NoCfCheckMask; }
3709 bool getHasRegParm() const { return ((Bits & RegParmMask) >> RegParmOffset) != 0; }
3710
3711 unsigned getRegParm() const {
3712 unsigned RegParm = (Bits & RegParmMask) >> RegParmOffset;
3713 if (RegParm > 0)
3714 --RegParm;
3715 return RegParm;
3716 }
3717
3718 CallingConv getCC() const { return CallingConv(Bits & CallConvMask); }
3719
3720 bool operator==(ExtInfo Other) const {
3721 return Bits == Other.Bits;
3722 }
3723 bool operator!=(ExtInfo Other) const {
3724 return Bits != Other.Bits;
3725 }
3726
3727 // Note that we don't have setters. That is by design, use
3728 // the following with methods instead of mutating these objects.
3729
3730 ExtInfo withNoReturn(bool noReturn) const {
3731 if (noReturn)
3732 return ExtInfo(Bits | NoReturnMask);
3733 else
3734 return ExtInfo(Bits & ~NoReturnMask);
3735 }
3736
3737 ExtInfo withProducesResult(bool producesResult) const {
3738 if (producesResult)
3739 return ExtInfo(Bits | ProducesResultMask);
3740 else
3741 return ExtInfo(Bits & ~ProducesResultMask);
3742 }
3743
3744 ExtInfo withCmseNSCall(bool cmseNSCall) const {
3745 if (cmseNSCall)
3746 return ExtInfo(Bits | CmseNSCallMask);
3747 else
3748 return ExtInfo(Bits & ~CmseNSCallMask);
3749 }
3750
3751 ExtInfo withNoCallerSavedRegs(bool noCallerSavedRegs) const {
3752 if (noCallerSavedRegs)
3753 return ExtInfo(Bits | NoCallerSavedRegsMask);
3754 else
3755 return ExtInfo(Bits & ~NoCallerSavedRegsMask);
3756 }
3757
3758 ExtInfo withNoCfCheck(bool noCfCheck) const {
3759 if (noCfCheck)
3760 return ExtInfo(Bits | NoCfCheckMask);
3761 else
3762 return ExtInfo(Bits & ~NoCfCheckMask);
3763 }
3764
3765 ExtInfo withRegParm(unsigned RegParm) const {
3766 assert(RegParm < 7 && "Invalid regparm value")(static_cast<void> (0));
3767 return ExtInfo((Bits & ~RegParmMask) |
3768 ((RegParm + 1) << RegParmOffset));
3769 }
3770
3771 ExtInfo withCallingConv(CallingConv cc) const {
3772 return ExtInfo((Bits & ~CallConvMask) | (unsigned) cc);
3773 }
3774
3775 void Profile(llvm::FoldingSetNodeID &ID) const {
3776 ID.AddInteger(Bits);
3777 }
3778 };
3779
3780 /// A simple holder for a QualType representing a type in an
3781 /// exception specification. Unfortunately needed by FunctionProtoType
3782 /// because TrailingObjects cannot handle repeated types.
3783 struct ExceptionType { QualType Type; };
3784
3785 /// A simple holder for various uncommon bits which do not fit in
3786 /// FunctionTypeBitfields. Aligned to alignof(void *) to maintain the
3787 /// alignment of subsequent objects in TrailingObjects. You must update
3788 /// hasExtraBitfields in FunctionProtoType after adding extra data here.
3789 struct alignas(void *) FunctionTypeExtraBitfields {
3790 /// The number of types in the exception specification.
3791 /// A whole unsigned is not needed here and according to
3792 /// [implimits] 8 bits would be enough here.
3793 unsigned NumExceptionType;
3794 };
3795
3796protected:
3797 FunctionType(TypeClass tc, QualType res, QualType Canonical,
3798 TypeDependence Dependence, ExtInfo Info)
3799 : Type(tc, Canonical, Dependence), ResultType(res) {
3800 FunctionTypeBits.ExtInfo = Info.Bits;
3801 }
3802
3803 Qualifiers getFastTypeQuals() const {
3804 return Qualifiers::fromFastMask(FunctionTypeBits.FastTypeQuals);
3805 }
3806
3807public:
3808 QualType getReturnType() const { return ResultType; }
3809
3810 bool getHasRegParm() const { return getExtInfo().getHasRegParm(); }
3811 unsigned getRegParmType() const { return getExtInfo().getRegParm(); }
3812
3813 /// Determine whether this function type includes the GNU noreturn
3814 /// attribute. The C++11 [[noreturn]] attribute does not affect the function
3815 /// type.
3816 bool getNoReturnAttr() const { return getExtInfo().getNoReturn(); }
3817
3818 bool getCmseNSCallAttr() const { return getExtInfo().getCmseNSCall(); }
3819 CallingConv getCallConv() const { return getExtInfo().getCC(); }
3820 ExtInfo getExtInfo() const { return ExtInfo(FunctionTypeBits.ExtInfo); }
3821
3822 static_assert((~Qualifiers::FastMask & Qualifiers::CVRMask) == 0,
3823 "Const, volatile and restrict are assumed to be a subset of "
3824 "the fast qualifiers.");
3825
3826 bool isConst() const { return getFastTypeQuals().hasConst(); }
3827 bool isVolatile() const { return getFastTypeQuals().hasVolatile(); }
3828 bool isRestrict() const { return getFastTypeQuals().hasRestrict(); }
3829
3830 /// Determine the type of an expression that calls a function of
3831 /// this type.
3832 QualType getCallResultType(const ASTContext &Context) const {
3833 return getReturnType().getNonLValueExprType(Context);
3834 }
3835
3836 static StringRef getNameForCallConv(CallingConv CC);
3837
3838 static bool classof(const Type *T) {
3839 return T->getTypeClass() == FunctionNoProto ||
3840 T->getTypeClass() == FunctionProto;
3841 }
3842};
3843
3844/// Represents a K&R-style 'int foo()' function, which has
3845/// no information available about its arguments.
3846class FunctionNoProtoType : public FunctionType, public llvm::FoldingSetNode {
3847 friend class ASTContext; // ASTContext creates these.
3848
3849 FunctionNoProtoType(QualType Result, QualType Canonical, ExtInfo Info)
3850 : FunctionType(FunctionNoProto, Result, Canonical,
3851 Result->getDependence() &
3852 ~(TypeDependence::DependentInstantiation |
3853 TypeDependence::UnexpandedPack),
3854 Info) {}
3855
3856public:
3857 // No additional state past what FunctionType provides.
3858
3859 bool isSugared() const { return false; }
3860 QualType desugar() const { return QualType(this, 0); }
3861
3862 void Profile(llvm::FoldingSetNodeID &ID) {
3863 Profile(ID, getReturnType(), getExtInfo());
3864 }
3865
3866 static void Profile(llvm::FoldingSetNodeID &ID, QualType ResultType,
3867 ExtInfo Info) {
3868 Info.Profile(ID);
3869 ID.AddPointer(ResultType.getAsOpaquePtr());
3870 }
3871
3872 static bool classof(const Type *T) {
3873 return T->getTypeClass() == FunctionNoProto;
3874 }
3875};
3876
3877/// Represents a prototype with parameter type info, e.g.
3878/// 'int foo(int)' or 'int foo(void)'. 'void' is represented as having no
3879/// parameters, not as having a single void parameter. Such a type can have
3880/// an exception specification, but this specification is not part of the
3881/// canonical type. FunctionProtoType has several trailing objects, some of
3882/// which optional. For more information about the trailing objects see
3883/// the first comment inside FunctionProtoType.
3884class FunctionProtoType final
3885 : public FunctionType,
3886 public llvm::FoldingSetNode,
3887 private llvm::TrailingObjects<
3888 FunctionProtoType, QualType, SourceLocation,
3889 FunctionType::FunctionTypeExtraBitfields, FunctionType::ExceptionType,
3890 Expr *, FunctionDecl *, FunctionType::ExtParameterInfo, Qualifiers> {
3891 friend class ASTContext; // ASTContext creates these.
3892 friend TrailingObjects;
3893
3894 // FunctionProtoType is followed by several trailing objects, some of
3895 // which optional. They are in order:
3896 //
3897 // * An array of getNumParams() QualType holding the parameter types.
3898 // Always present. Note that for the vast majority of FunctionProtoType,
3899 // these will be the only trailing objects.
3900 //
3901 // * Optionally if the function is variadic, the SourceLocation of the
3902 // ellipsis.
3903 //
3904 // * Optionally if some extra data is stored in FunctionTypeExtraBitfields
3905 // (see FunctionTypeExtraBitfields and FunctionTypeBitfields):
3906 // a single FunctionTypeExtraBitfields. Present if and only if
3907 // hasExtraBitfields() is true.
3908 //
3909 // * Optionally exactly one of:
3910 // * an array of getNumExceptions() ExceptionType,
3911 // * a single Expr *,
3912 // * a pair of FunctionDecl *,
3913 // * a single FunctionDecl *
3914 // used to store information about the various types of exception
3915 // specification. See getExceptionSpecSize for the details.
3916 //
3917 // * Optionally an array of getNumParams() ExtParameterInfo holding
3918 // an ExtParameterInfo for each of the parameters. Present if and
3919 // only if hasExtParameterInfos() is true.
3920 //
3921 // * Optionally a Qualifiers object to represent extra qualifiers that can't
3922 // be represented by FunctionTypeBitfields.FastTypeQuals. Present if and only
3923 // if hasExtQualifiers() is true.
3924 //
3925 // The optional FunctionTypeExtraBitfields has to be before the data
3926 // related to the exception specification since it contains the number
3927 // of exception types.
3928 //
3929 // We put the ExtParameterInfos last. If all were equal, it would make
3930 // more sense to put these before the exception specification, because
3931 // it's much easier to skip past them compared to the elaborate switch
3932 // required to skip the exception specification. However, all is not
3933 // equal; ExtParameterInfos are used to model very uncommon features,
3934 // and it's better not to burden the more common paths.
3935
3936public:
3937 /// Holds information about the various types of exception specification.
3938 /// ExceptionSpecInfo is not stored as such in FunctionProtoType but is
3939 /// used to group together the various bits of information about the
3940 /// exception specification.
3941 struct ExceptionSpecInfo {
3942 /// The kind of exception specification this is.
3943 ExceptionSpecificationType Type = EST_None;
3944
3945 /// Explicitly-specified list of exception types.
3946 ArrayRef<QualType> Exceptions;
3947
3948 /// Noexcept expression, if this is a computed noexcept specification.
3949 Expr *NoexceptExpr = nullptr;
3950
3951 /// The function whose exception specification this is, for
3952 /// EST_Unevaluated and EST_Uninstantiated.
3953 FunctionDecl *SourceDecl = nullptr;
3954
3955 /// The function template whose exception specification this is instantiated
3956 /// from, for EST_Uninstantiated.
3957 FunctionDecl *SourceTemplate = nullptr;
3958
3959 ExceptionSpecInfo() = default;
3960
3961 ExceptionSpecInfo(ExceptionSpecificationType EST) : Type(EST) {}
3962 };
3963
3964 /// Extra information about a function prototype. ExtProtoInfo is not
3965 /// stored as such in FunctionProtoType but is used to group together
3966 /// the various bits of extra information about a function prototype.
3967 struct ExtProtoInfo {
3968 FunctionType::ExtInfo ExtInfo;
3969 bool Variadic : 1;
3970 bool HasTrailingReturn : 1;
3971 Qualifiers TypeQuals;
3972 RefQualifierKind RefQualifier = RQ_None;
3973 ExceptionSpecInfo ExceptionSpec;
3974 const ExtParameterInfo *ExtParameterInfos = nullptr;
3975 SourceLocation EllipsisLoc;
3976
3977 ExtProtoInfo() : Variadic(false), HasTrailingReturn(false) {}
3978
3979 ExtProtoInfo(CallingConv CC)
3980 : ExtInfo(CC), Variadic(false), HasTrailingReturn(false) {}
3981
3982 ExtProtoInfo withExceptionSpec(const ExceptionSpecInfo &ESI) {
3983 ExtProtoInfo Result(*this);
3984 Result.ExceptionSpec = ESI;
3985 return Result;
3986 }
3987 };
3988
3989private:
3990 unsigned numTrailingObjects(OverloadToken<QualType>) const {
3991 return getNumParams();
3992 }
3993
3994 unsigned numTrailingObjects(OverloadToken<SourceLocation>) const {
3995 return isVariadic();
3996 }
3997
3998 unsigned numTrailingObjects(OverloadToken<FunctionTypeExtraBitfields>) const {
3999 return hasExtraBitfields();
4000 }
4001
4002 unsigned numTrailingObjects(OverloadToken<ExceptionType>) const {
4003 return getExceptionSpecSize().NumExceptionType;
4004 }
4005
4006 unsigned numTrailingObjects(OverloadToken<Expr *>) const {
4007 return getExceptionSpecSize().NumExprPtr;
4008 }
4009
4010 unsigned numTrailingObjects(OverloadToken<FunctionDecl *>) const {
4011 return getExceptionSpecSize().NumFunctionDeclPtr;
4012 }
4013
4014 unsigned numTrailingObjects(OverloadToken<ExtParameterInfo>) const {
4015 return hasExtParameterInfos() ? getNumParams() : 0;
4016 }
4017
4018 /// Determine whether there are any argument types that
4019 /// contain an unexpanded parameter pack.
4020 static bool containsAnyUnexpandedParameterPack(const QualType *ArgArray,
4021 unsigned numArgs) {
4022 for (unsigned Idx = 0; Idx < numArgs; ++Idx)
4023 if (ArgArray[Idx]->containsUnexpandedParameterPack())
4024 return true;
4025
4026 return false;
4027 }
4028
4029 FunctionProtoType(QualType result, ArrayRef<QualType> params,
4030 QualType canonical, const ExtProtoInfo &epi);
4031
4032 /// This struct is returned by getExceptionSpecSize and is used to
4033 /// translate an ExceptionSpecificationType to the number and kind
4034 /// of trailing objects related to the exception specification.
4035 struct ExceptionSpecSizeHolder {
4036 unsigned NumExceptionType;
4037 unsigned NumExprPtr;
4038 unsigned NumFunctionDeclPtr;
4039 };
4040
4041 /// Return the number and kind of trailing objects
4042 /// related to the exception specification.
4043 static ExceptionSpecSizeHolder
4044 getExceptionSpecSize(ExceptionSpecificationType EST, unsigned NumExceptions) {
4045 switch (EST) {
4046 case EST_None:
4047 case EST_DynamicNone:
4048 case EST_MSAny:
4049 case EST_BasicNoexcept:
4050 case EST_Unparsed:
4051 case EST_NoThrow:
4052 return {0, 0, 0};
4053
4054 case EST_Dynamic:
4055 return {NumExceptions, 0, 0};
4056
4057 case EST_DependentNoexcept:
4058 case EST_NoexceptFalse:
4059 case EST_NoexceptTrue:
4060 return {0, 1, 0};
4061
4062 case EST_Uninstantiated:
4063 return {0, 0, 2};
4064
4065 case EST_Unevaluated:
4066 return {0, 0, 1};
4067 }
4068 llvm_unreachable("bad exception specification kind")__builtin_unreachable();
4069 }
4070
4071 /// Return the number and kind of trailing objects
4072 /// related to the exception specification.
4073 ExceptionSpecSizeHolder getExceptionSpecSize() const {
4074 return getExceptionSpecSize(getExceptionSpecType(), getNumExceptions());
4075 }
4076
4077 /// Whether the trailing FunctionTypeExtraBitfields is present.
4078 static bool hasExtraBitfields(ExceptionSpecificationType EST) {
4079 // If the exception spec type is EST_Dynamic then we have > 0 exception
4080 // types and the exact number is stored in FunctionTypeExtraBitfields.
4081 return EST == EST_Dynamic;
4082 }
4083
4084 /// Whether the trailing FunctionTypeExtraBitfields is present.
4085 bool hasExtraBitfields() const {
4086 return hasExtraBitfields(getExceptionSpecType());
4087 }
4088
4089 bool hasExtQualifiers() const {
4090 return FunctionTypeBits.HasExtQuals;
4091 }
4092
4093public:
4094 unsigned getNumParams() const { return FunctionTypeBits.NumParams; }
4095
4096 QualType getParamType(unsigned i) const {
4097 assert(i < getNumParams() && "invalid parameter index")(static_cast<void> (0));
4098 return param_type_begin()[i];
4099 }
4100
4101 ArrayRef<QualType> getParamTypes() const {
4102 return llvm::makeArrayRef(param_type_begin(), param_type_end());
4103 }
4104
4105 ExtProtoInfo getExtProtoInfo() const {
4106 ExtProtoInfo EPI;
4107 EPI.ExtInfo = getExtInfo();
4108 EPI.Variadic = isVariadic();
4109 EPI.EllipsisLoc = getEllipsisLoc();
4110 EPI.HasTrailingReturn = hasTrailingReturn();
4111 EPI.ExceptionSpec = getExceptionSpecInfo();
4112 EPI.TypeQuals = getMethodQuals();
4113 EPI.RefQualifier = getRefQualifier();
4114 EPI.ExtParameterInfos = getExtParameterInfosOrNull();
4115 return EPI;
4116 }
4117
4118 /// Get the kind of exception specification on this function.
4119 ExceptionSpecificationType getExceptionSpecType() const {
4120 return static_cast<ExceptionSpecificationType>(
4121 FunctionTypeBits.ExceptionSpecType);
4122 }
4123
4124 /// Return whether this function has any kind of exception spec.
4125 bool hasExceptionSpec() const { return getExceptionSpecType() != EST_None; }
4126
4127 /// Return whether this function has a dynamic (throw) exception spec.
4128 bool hasDynamicExceptionSpec() const {
4129 return isDynamicExceptionSpec(getExceptionSpecType());
4130 }
4131
4132 /// Return whether this function has a noexcept exception spec.
4133 bool hasNoexceptExceptionSpec() const {
4134 return isNoexceptExceptionSpec(getExceptionSpecType());
4135 }
4136
4137 /// Return whether this function has a dependent exception spec.
4138 bool hasDependentExceptionSpec() const;
4139
4140 /// Return whether this function has an instantiation-dependent exception
4141 /// spec.
4142 bool hasInstantiationDependentExceptionSpec() const;
4143
4144 /// Return all the available information about this type's exception spec.
4145 ExceptionSpecInfo getExceptionSpecInfo() const {
4146 ExceptionSpecInfo Result;
4147 Result.Type = getExceptionSpecType();
4148 if (Result.Type == EST_Dynamic) {
4149 Result.Exceptions = exceptions();
4150 } else if (isComputedNoexcept(Result.Type)) {
4151 Result.NoexceptExpr = getNoexceptExpr();
4152 } else if (Result.Type == EST_Uninstantiated) {
4153 Result.SourceDecl = getExceptionSpecDecl();
4154 Result.SourceTemplate = getExceptionSpecTemplate();
4155 } else if (Result.Type == EST_Unevaluated) {
4156 Result.SourceDecl = getExceptionSpecDecl();
4157 }
4158 return Result;
4159 }
4160
4161 /// Return the number of types in the exception specification.
4162 unsigned getNumExceptions() const {
4163 return getExceptionSpecType() == EST_Dynamic
4164 ? getTrailingObjects<FunctionTypeExtraBitfields>()
4165 ->NumExceptionType
4166 : 0;
4167 }
4168
4169 /// Return the ith exception type, where 0 <= i < getNumExceptions().
4170 QualType getExceptionType(unsigned i) const {
4171 assert(i < getNumExceptions() && "Invalid exception number!")(static_cast<void> (0));
4172 return exception_begin()[i];
4173 }
4174
4175 /// Return the expression inside noexcept(expression), or a null pointer
4176 /// if there is none (because the exception spec is not of this form).
4177 Expr *getNoexceptExpr() const {
4178 if (!isComputedNoexcept(getExceptionSpecType()))
4179 return nullptr;
4180 return *getTrailingObjects<Expr *>();
4181 }
4182
4183 /// If this function type has an exception specification which hasn't
4184 /// been determined yet (either because it has not been evaluated or because
4185 /// it has not been instantiated), this is the function whose exception
4186 /// specification is represented by this type.
4187 FunctionDecl *getExceptionSpecDecl() const {
4188 if (getExceptionSpecType() != EST_Uninstantiated &&
4189 getExceptionSpecType() != EST_Unevaluated)
4190 return nullptr;
4191 return getTrailingObjects<FunctionDecl *>()[0];
4192 }
4193
4194 /// If this function type has an uninstantiated exception
4195 /// specification, this is the function whose exception specification
4196 /// should be instantiated to find the exception specification for
4197 /// this type.
4198 FunctionDecl *getExceptionSpecTemplate() const {
4199 if (getExceptionSpecType() != EST_Uninstantiated)
4200 return nullptr;
4201 return getTrailingObjects<FunctionDecl *>()[1];
4202 }
4203
4204 /// Determine whether this function type has a non-throwing exception
4205 /// specification.
4206 CanThrowResult canThrow() const;
4207
4208 /// Determine whether this function type has a non-throwing exception
4209 /// specification. If this depends on template arguments, returns
4210 /// \c ResultIfDependent.
4211 bool isNothrow(bool ResultIfDependent = false) const {
4212 return ResultIfDependent ? canThrow() != CT_Can : canThrow() == CT_Cannot;
4213 }
4214
4215 /// Whether this function prototype is variadic.
4216 bool isVariadic() const { return FunctionTypeBits.Variadic; }
4217
4218 SourceLocation getEllipsisLoc() const {
4219 return isVariadic() ? *getTrailingObjects<SourceLocation>()
4220 : SourceLocation();
4221 }
4222
4223 /// Determines whether this function prototype contains a
4224 /// parameter pack at the end.
4225 ///
4226 /// A function template whose last parameter is a parameter pack can be
4227 /// called with an arbitrary number of arguments, much like a variadic
4228 /// function.
4229 bool isTemplateVariadic() const;
4230
4231 /// Whether this function prototype has a trailing return type.
4232 bool hasTrailingReturn() const { return FunctionTypeBits.HasTrailingReturn; }
4233
4234 Qualifiers getMethodQuals() const {
4235 if (hasExtQualifiers())
4236 return *getTrailingObjects<Qualifiers>();
4237 else
4238 return getFastTypeQuals();
4239 }
4240
4241 /// Retrieve the ref-qualifier associated with this function type.
4242 RefQualifierKind getRefQualifier() const {
4243 return static_cast<RefQualifierKind>(FunctionTypeBits.RefQualifier);
4244 }
4245
4246 using param_type_iterator = const QualType *;
4247 using param_type_range = llvm::iterator_range<param_type_iterator>;
4248
4249 param_type_range param_types() const {
4250 return param_type_range(param_type_begin(), param_type_end());
4251 }
4252
4253 param_type_iterator param_type_begin() const {
4254 return getTrailingObjects<QualType>();
4255 }
4256
4257 param_type_iterator param_type_end() const {
4258 return param_type_begin() + getNumParams();
4259 }
4260
4261 using exception_iterator = const QualType *;
4262
4263 ArrayRef<QualType> exceptions() const {
4264 return llvm::makeArrayRef(exception_begin(), exception_end());
4265 }
4266
4267 exception_iterator exception_begin() const {
4268 return reinterpret_cast<exception_iterator>(
4269 getTrailingObjects<ExceptionType>());
4270 }
4271
4272 exception_iterator exception_end() const {
4273 return exception_begin() + getNumExceptions();
4274 }
4275
4276 /// Is there any interesting extra information for any of the parameters
4277 /// of this function type?
4278 bool hasExtParameterInfos() const {
4279 return FunctionTypeBits.HasExtParameterInfos;
4280 }
4281
4282 ArrayRef<ExtParameterInfo> getExtParameterInfos() const {
4283 assert(hasExtParameterInfos())(static_cast<void> (0));
4284 return ArrayRef<ExtParameterInfo>(getTrailingObjects<ExtParameterInfo>(),
4285 getNumParams());
4286 }
4287
4288 /// Return a pointer to the beginning of the array of extra parameter
4289 /// information, if present, or else null if none of the parameters
4290 /// carry it. This is equivalent to getExtProtoInfo().ExtParameterInfos.
4291 const ExtParameterInfo *getExtParameterInfosOrNull() const {
4292 if (!hasExtParameterInfos())
4293 return nullptr;
4294 return getTrailingObjects<ExtParameterInfo>();
4295 }
4296
4297 ExtParameterInfo getExtParameterInfo(unsigned I) const {
4298 assert(I < getNumParams() && "parameter index out of range")(static_cast<void> (0));
4299 if (hasExtParameterInfos())
4300 return getTrailingObjects<ExtParameterInfo>()[I];
4301 return ExtParameterInfo();
4302 }
4303
4304 ParameterABI getParameterABI(unsigned I) const {
4305 assert(I < getNumParams() && "parameter index out of range")(static_cast<void> (0));
4306 if (hasExtParameterInfos())
4307 return getTrailingObjects<ExtParameterInfo>()[I].getABI();
4308 return ParameterABI::Ordinary;
4309 }
4310
4311 bool isParamConsumed(unsigned I) const {
4312 assert(I < getNumParams() && "parameter index out of range")(static_cast<void> (0));
4313 if (hasExtParameterInfos())
4314 return getTrailingObjects<ExtParameterInfo>()[I].isConsumed();
4315 return false;
4316 }
4317
4318 bool isSugared() const { return false; }
4319 QualType desugar() const { return QualType(this, 0); }
4320
4321 void printExceptionSpecification(raw_ostream &OS,
4322 const PrintingPolicy &Policy) const;
4323
4324 static bool classof(const Type *T) {
4325 return T->getTypeClass() == FunctionProto;
4326 }
4327
4328 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx);
4329 static void Profile(llvm::FoldingSetNodeID &ID, QualType Result,
4330 param_type_iterator ArgTys, unsigned NumArgs,
4331 const ExtProtoInfo &EPI, const ASTContext &Context,
4332 bool Canonical);
4333};
4334
4335/// Represents the dependent type named by a dependently-scoped
4336/// typename using declaration, e.g.
4337/// using typename Base<T>::foo;
4338///
4339/// Template instantiation turns these into the underlying type.
4340class UnresolvedUsingType : public Type {
4341 friend class ASTContext; // ASTContext creates these.
4342
4343 UnresolvedUsingTypenameDecl *Decl;
4344
4345 UnresolvedUsingType(const UnresolvedUsingTypenameDecl *D)
4346 : Type(UnresolvedUsing, QualType(),
4347 TypeDependence::DependentInstantiation),
4348 Decl(const_cast<UnresolvedUsingTypenameDecl *>(D)) {}
4349
4350public:
4351 UnresolvedUsingTypenameDecl *getDecl() const { return Decl; }
4352
4353 bool isSugared() const { return false; }
4354 QualType desugar() const { return QualType(this, 0); }
4355
4356 static bool classof(const Type *T) {
4357 return T->getTypeClass() == UnresolvedUsing;
4358 }
4359
4360 void Profile(llvm::FoldingSetNodeID &ID) {
4361 return Profile(ID, Decl);
4362 }
4363
4364 static void Profile(llvm::FoldingSetNodeID &ID,
4365 UnresolvedUsingTypenameDecl *D) {
4366 ID.AddPointer(D);
4367 }
4368};
4369
4370class TypedefType : public Type {
4371 TypedefNameDecl *Decl;
4372
4373private:
4374 friend class ASTContext; // ASTContext creates these.
4375
4376 TypedefType(TypeClass tc, const TypedefNameDecl *D, QualType underlying,
4377 QualType can);
4378
4379public:
4380 TypedefNameDecl *getDecl() const { return Decl; }
4381
4382 bool isSugared() const { return true; }
4383 QualType desugar() const;
4384
4385 static bool classof(const Type *T) { return T->getTypeClass() == Typedef; }
4386};
4387
4388/// Sugar type that represents a type that was qualified by a qualifier written
4389/// as a macro invocation.
4390class MacroQualifiedType : public Type {
4391 friend class ASTContext; // ASTContext creates these.
4392
4393 QualType UnderlyingTy;
4394 const IdentifierInfo *MacroII;
4395
4396 MacroQualifiedType(QualType UnderlyingTy, QualType CanonTy,
4397 const IdentifierInfo *MacroII)
4398 : Type(MacroQualified, CanonTy, UnderlyingTy->getDependence()),
4399 UnderlyingTy(UnderlyingTy), MacroII(MacroII) {
4400 assert(isa<AttributedType>(UnderlyingTy) &&(static_cast<void> (0))
4401 "Expected a macro qualified type to only wrap attributed types.")(static_cast<void> (0));
4402 }
4403
4404public:
4405 const IdentifierInfo *getMacroIdentifier() const { return MacroII; }
4406 QualType getUnderlyingType() const { return UnderlyingTy; }
4407
4408 /// Return this attributed type's modified type with no qualifiers attached to
4409 /// it.
4410 QualType getModifiedType() const;
4411
4412 bool isSugared() const { return true; }
4413 QualType desugar() const;
4414
4415 static bool classof(const Type *T) {
4416 return T->getTypeClass() == MacroQualified;
4417 }
4418};
4419
4420/// Represents a `typeof` (or __typeof__) expression (a GCC extension).
4421class TypeOfExprType : public Type {
4422 Expr *TOExpr;
4423
4424protected:
4425 friend class ASTContext; // ASTContext creates these.
4426
4427 TypeOfExprType(Expr *E, QualType can = QualType());
4428
4429public:
4430 Expr *getUnderlyingExpr() const { return TOExpr; }
4431
4432 /// Remove a single level of sugar.
4433 QualType desugar() const;
4434
4435 /// Returns whether this type directly provides sugar.
4436 bool isSugared() const;
4437
4438 static bool classof(const Type *T) { return T->getTypeClass() == TypeOfExpr; }
4439};
4440
4441/// Internal representation of canonical, dependent
4442/// `typeof(expr)` types.
4443///
4444/// This class is used internally by the ASTContext to manage
4445/// canonical, dependent types, only. Clients will only see instances
4446/// of this class via TypeOfExprType nodes.
4447class DependentTypeOfExprType
4448 : public TypeOfExprType, public llvm::FoldingSetNode {
4449 const ASTContext &Context;
4450
4451public:
4452 DependentTypeOfExprType(const ASTContext &Context, Expr *E)
4453 : TypeOfExprType(E), Context(Context) {}
4454
4455 void Profile(llvm::FoldingSetNodeID &ID) {
4456 Profile(ID, Context, getUnderlyingExpr());
4457 }
4458
4459 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
4460 Expr *E);
4461};
4462
4463/// Represents `typeof(type)`, a GCC extension.
4464class TypeOfType : public Type {
4465 friend class ASTContext; // ASTContext creates these.
4466
4467 QualType TOType;
4468
4469 TypeOfType(QualType T, QualType can)
4470 : Type(TypeOf, can, T->getDependence()), TOType(T) {
4471 assert(!isa<TypedefType>(can) && "Invalid canonical type")(static_cast<void> (0));
4472 }
4473
4474public:
4475 QualType getUnderlyingType() const { return TOType; }
4476
4477 /// Remove a single level of sugar.
4478 QualType desugar() const { return getUnderlyingType(); }
4479
4480 /// Returns whether this type directly provides sugar.
4481 bool isSugared() const { return true; }
4482
4483 static bool classof(const Type *T) { return T->getTypeClass() == TypeOf; }
4484};
4485
4486/// Represents the type `decltype(expr)` (C++11).
4487class DecltypeType : public Type {
4488 Expr *E;
4489 QualType UnderlyingType;
4490
4491protected:
4492 friend class ASTContext; // ASTContext creates these.
4493
4494 DecltypeType(Expr *E, QualType underlyingType, QualType can = QualType());
4495
4496public:
4497 Expr *getUnderlyingExpr() const { return E; }
4498 QualType getUnderlyingType() const { return UnderlyingType; }
4499
4500 /// Remove a single level of sugar.
4501 QualType desugar() const;
4502
4503 /// Returns whether this type directly provides sugar.
4504 bool isSugared() const;
4505
4506 static bool classof(const Type *T) { return T->getTypeClass() == Decltype; }
4507};
4508
4509/// Internal representation of canonical, dependent
4510/// decltype(expr) types.
4511///
4512/// This class is used internally by the ASTContext to manage
4513/// canonical, dependent types, only. Clients will only see instances
4514/// of this class via DecltypeType nodes.
4515class DependentDecltypeType : public DecltypeType, public llvm::FoldingSetNode {
4516 const ASTContext &Context;
4517
4518public:
4519 DependentDecltypeType(const ASTContext &Context, Expr *E);
4520
4521 void Profile(llvm::FoldingSetNodeID &ID) {
4522 Profile(ID, Context, getUnderlyingExpr());
4523 }
4524
4525 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
4526 Expr *E);
4527};
4528
4529/// A unary type transform, which is a type constructed from another.
4530class UnaryTransformType : public Type {
4531public:
4532 enum UTTKind {
4533 EnumUnderlyingType
4534 };
4535
4536private:
4537 /// The untransformed type.
4538 QualType BaseType;
4539
4540 /// The transformed type if not dependent, otherwise the same as BaseType.
4541 QualType UnderlyingType;
4542
4543 UTTKind UKind;
4544
4545protected:
4546 friend class ASTContext;
4547
4548 UnaryTransformType(QualType BaseTy, QualType UnderlyingTy, UTTKind UKind,
4549 QualType CanonicalTy);
4550
4551public:
4552 bool isSugared() const { return !isDependentType(); }
4553 QualType desugar() const { return UnderlyingType; }
4554
4555 QualType getUnderlyingType() const { return UnderlyingType; }
4556 QualType getBaseType() const { return BaseType; }
4557
4558 UTTKind getUTTKind() const { return UKind; }
4559
4560 static bool classof(const Type *T) {
4561 return T->getTypeClass() == UnaryTransform;
4562 }
4563};
4564
4565/// Internal representation of canonical, dependent
4566/// __underlying_type(type) types.
4567///
4568/// This class is used internally by the ASTContext to manage
4569/// canonical, dependent types, only. Clients will only see instances
4570/// of this class via UnaryTransformType nodes.
4571class DependentUnaryTransformType : public UnaryTransformType,
4572 public llvm::FoldingSetNode {
4573public:
4574 DependentUnaryTransformType(const ASTContext &C, QualType BaseType,
4575 UTTKind UKind);
4576
4577 void Profile(llvm::FoldingSetNodeID &ID) {
4578 Profile(ID, getBaseType(), getUTTKind());
4579 }
4580
4581 static void Profile(llvm::FoldingSetNodeID &ID, QualType BaseType,
4582 UTTKind UKind) {
4583 ID.AddPointer(BaseType.getAsOpaquePtr());
4584 ID.AddInteger((unsigned)UKind);
4585 }
4586};
4587
4588class TagType : public Type {
4589 friend class ASTReader;
4590 template <class T> friend class serialization::AbstractTypeReader;
4591
4592 /// Stores the TagDecl associated with this type. The decl may point to any
4593 /// TagDecl that declares the entity.
4594 TagDecl *decl;
4595
4596protected:
4597 TagType(TypeClass TC, const TagDecl *D, QualType can);
4598
4599public:
4600 TagDecl *getDecl() const;
4601
4602 /// Determines whether this type is in the process of being defined.
4603 bool isBeingDefined() const;
4604
4605 static bool classof(const Type *T) {
4606 return T->getTypeClass() == Enum || T->getTypeClass() == Record;
4607 }
4608};
4609
4610/// A helper class that allows the use of isa/cast/dyncast
4611/// to detect TagType objects of structs/unions/classes.
4612class RecordType : public TagType {
4613protected:
4614 friend class ASTContext; // ASTContext creates these.
4615
4616 explicit RecordType(const RecordDecl *D)
4617 : TagType(Record, reinterpret_cast<const TagDecl*>(D), QualType()) {}
4618 explicit RecordType(TypeClass TC, RecordDecl *D)
4619 : TagType(TC, reinterpret_cast<const TagDecl*>(D), QualType()) {}
4620
4621public:
4622 RecordDecl *getDecl() const {
4623 return reinterpret_cast<RecordDecl*>(TagType::getDecl());
4624 }
4625
4626 /// Recursively check all fields in the record for const-ness. If any field
4627 /// is declared const, return true. Otherwise, return false.
4628 bool hasConstFields() const;
4629
4630 bool isSugared() const { return false; }
4631 QualType desugar() const { return QualType(this, 0); }
4632
4633 static bool classof(const Type *T) { return T->getTypeClass() == Record; }
4634};
4635
4636/// A helper class that allows the use of isa/cast/dyncast
4637/// to detect TagType objects of enums.
4638class EnumType : public TagType {
4639 friend class ASTContext; // ASTContext creates these.
4640
4641 explicit EnumType(const EnumDecl *D)
4642 : TagType(Enum, reinterpret_cast<const TagDecl*>(D), QualType()) {}
4643
4644public:
4645 EnumDecl *getDecl() const {
4646 return reinterpret_cast<EnumDecl*>(TagType::getDecl());
4647 }
4648
4649 bool isSugared() const { return false; }
4650 QualType desugar() const { return QualType(this, 0); }
4651
4652 static bool classof(const Type *T) { return T->getTypeClass() == Enum; }
4653};
4654
4655/// An attributed type is a type to which a type attribute has been applied.
4656///
4657/// The "modified type" is the fully-sugared type to which the attributed
4658/// type was applied; generally it is not canonically equivalent to the
4659/// attributed type. The "equivalent type" is the minimally-desugared type
4660/// which the type is canonically equivalent to.
4661///
4662/// For example, in the following attributed type:
4663/// int32_t __attribute__((vector_size(16)))
4664/// - the modified type is the TypedefType for int32_t
4665/// - the equivalent type is VectorType(16, int32_t)
4666/// - the canonical type is VectorType(16, int)
4667class AttributedType : public Type, public llvm::FoldingSetNode {
4668public:
4669 using Kind = attr::Kind;
4670
4671private:
4672 friend class ASTContext; // ASTContext creates these
4673
4674 QualType ModifiedType;
4675 QualType EquivalentType;
4676
4677 AttributedType(QualType canon, attr::Kind attrKind, QualType modified,
4678 QualType equivalent)
4679 : Type(Attributed, canon, equivalent->getDependence()),
4680 ModifiedType(modified), EquivalentType(equivalent) {
4681 AttributedTypeBits.AttrKind = attrKind;
4682 }
4683
4684public:
4685 Kind getAttrKind() const {
4686 return static_cast<Kind>(AttributedTypeBits.AttrKind);
4687 }
4688
4689 QualType getModifiedType() const { return ModifiedType; }
4690 QualType getEquivalentType() const { return EquivalentType; }
4691
4692 bool isSugared() const { return true; }
4693 QualType desugar() const { return getEquivalentType(); }
4694
4695 /// Does this attribute behave like a type qualifier?
4696 ///
4697 /// A type qualifier adjusts a type to provide specialized rules for
4698 /// a specific object, like the standard const and volatile qualifiers.
4699 /// This includes attributes controlling things like nullability,
4700 /// address spaces, and ARC ownership. The value of the object is still
4701 /// largely described by the modified type.
4702 ///
4703 /// In contrast, many type attributes "rewrite" their modified type to
4704 /// produce a fundamentally different type, not necessarily related in any
4705 /// formalizable way to the original type. For example, calling convention
4706 /// and vector attributes are not simple type qualifiers.
4707 ///
4708 /// Type qualifiers are often, but not always, reflected in the canonical
4709 /// type.
4710 bool isQualifier() const;
4711
4712 bool isMSTypeSpec() const;
4713
4714 bool isCallingConv() const;
4715
4716 llvm::Optional<NullabilityKind> getImmediateNullability() const;
4717
4718 /// Retrieve the attribute kind corresponding to the given
4719 /// nullability kind.
4720 static Kind getNullabilityAttrKind(NullabilityKind kind) {
4721 switch (kind) {
4722 case NullabilityKind::NonNull:
4723 return attr::TypeNonNull;
4724
4725 case NullabilityKind::Nullable:
4726 return attr::TypeNullable;
4727
4728 case NullabilityKind::NullableResult:
4729 return attr::TypeNullableResult;
4730
4731 case NullabilityKind::Unspecified:
4732 return attr::TypeNullUnspecified;
4733 }
4734 llvm_unreachable("Unknown nullability kind.")__builtin_unreachable();
4735 }
4736
4737 /// Strip off the top-level nullability annotation on the given
4738 /// type, if it's there.
4739 ///
4740 /// \param T The type to strip. If the type is exactly an
4741 /// AttributedType specifying nullability (without looking through
4742 /// type sugar), the nullability is returned and this type changed
4743 /// to the underlying modified type.
4744 ///
4745 /// \returns the top-level nullability, if present.
4746 static Optional<NullabilityKind> stripOuterNullability(QualType &T);
4747
4748 void Profile(llvm::FoldingSetNodeID &ID) {
4749 Profile(ID, getAttrKind(), ModifiedType, EquivalentType);
4750 }
4751
4752 static void Profile(llvm::FoldingSetNodeID &ID, Kind attrKind,
4753 QualType modified, QualType equivalent) {
4754 ID.AddInteger(attrKind);
4755 ID.AddPointer(modified.getAsOpaquePtr());
4756 ID.AddPointer(equivalent.getAsOpaquePtr());
4757 }
4758
4759 static bool classof(const Type *T) {
4760 return T->getTypeClass() == Attributed;
4761 }
4762};
4763
4764class TemplateTypeParmType : public Type, public llvm::FoldingSetNode {
4765 friend class ASTContext; // ASTContext creates these
4766
4767 // Helper data collector for canonical types.
4768 struct CanonicalTTPTInfo {
4769 unsigned Depth : 15;
4770 unsigned ParameterPack : 1;
4771 unsigned Index : 16;
4772 };
4773
4774 union {
4775 // Info for the canonical type.
4776 CanonicalTTPTInfo CanTTPTInfo;
4777
4778 // Info for the non-canonical type.
4779 TemplateTypeParmDecl *TTPDecl;
4780 };
4781
4782 /// Build a non-canonical type.
4783 TemplateTypeParmType(TemplateTypeParmDecl *TTPDecl, QualType Canon)
4784 : Type(TemplateTypeParm, Canon,
4785 TypeDependence::DependentInstantiation |
4786 (Canon->getDependence() & TypeDependence::UnexpandedPack)),
4787 TTPDecl(TTPDecl) {}
4788
4789 /// Build the canonical type.
4790 TemplateTypeParmType(unsigned D, unsigned I, bool PP)
4791 : Type(TemplateTypeParm, QualType(this, 0),
4792 TypeDependence::DependentInstantiation |
4793 (PP ? TypeDependence::UnexpandedPack : TypeDependence::None)) {
4794 CanTTPTInfo.Depth = D;
4795 CanTTPTInfo.Index = I;
4796 CanTTPTInfo.ParameterPack = PP;
4797 }
4798
4799 const CanonicalTTPTInfo& getCanTTPTInfo() const {
4800 QualType Can = getCanonicalTypeInternal();
4801 return Can->castAs<TemplateTypeParmType>()->CanTTPTInfo;
4802 }
4803
4804public:
4805 unsigned getDepth() const { return getCanTTPTInfo().Depth; }
4806 unsigned getIndex() const { return getCanTTPTInfo().Index; }
4807 bool isParameterPack() const { return getCanTTPTInfo().ParameterPack; }
4808
4809 TemplateTypeParmDecl *getDecl() const {
4810 return isCanonicalUnqualified() ? nullptr : TTPDecl;
4811 }
4812
4813 IdentifierInfo *getIdentifier() const;
4814
4815 bool isSugared() const { return false; }
4816 QualType desugar() const { return QualType(this, 0); }
4817
4818 void Profile(llvm::FoldingSetNodeID &ID) {
4819 Profile(ID, getDepth(), getIndex(), isParameterPack(), getDecl());
4820 }
4821
4822 static void Profile(llvm::FoldingSetNodeID &ID, unsigned Depth,
4823 unsigned Index, bool ParameterPack,
4824 TemplateTypeParmDecl *TTPDecl) {
4825 ID.AddInteger(Depth);
4826 ID.AddInteger(Index);
4827 ID.AddBoolean(ParameterPack);
4828 ID.AddPointer(TTPDecl);
4829 }
4830
4831 static bool classof(const Type *T) {
4832 return T->getTypeClass() == TemplateTypeParm;
4833 }
4834};
4835
4836/// Represents the result of substituting a type for a template
4837/// type parameter.
4838///
4839/// Within an instantiated template, all template type parameters have
4840/// been replaced with these. They are used solely to record that a
4841/// type was originally written as a template type parameter;
4842/// therefore they are never canonical.
4843class SubstTemplateTypeParmType : public Type, public llvm::FoldingSetNode {
4844 friend class ASTContext;
4845
4846 // The original type parameter.
4847 const TemplateTypeParmType *Replaced;
4848
4849 SubstTemplateTypeParmType(const TemplateTypeParmType *Param, QualType Canon)
4850 : Type(SubstTemplateTypeParm, Canon, Canon->getDependence()),
4851 Replaced(Param) {}
4852
4853public:
4854 /// Gets the template parameter that was substituted for.
4855 const TemplateTypeParmType *getReplacedParameter() const {
4856 return Replaced;
4857 }
4858
4859 /// Gets the type that was substituted for the template
4860 /// parameter.
4861 QualType getReplacementType() const {
4862 return getCanonicalTypeInternal();
4863 }
4864
4865 bool isSugared() const { return true; }
4866 QualType desugar() const { return getReplacementType(); }
4867
4868 void Profile(llvm::FoldingSetNodeID &ID) {
4869 Profile(ID, getReplacedParameter(), getReplacementType());
4870 }
4871
4872 static void Profile(llvm::FoldingSetNodeID &ID,
4873 const TemplateTypeParmType *Replaced,
4874 QualType Replacement) {
4875 ID.AddPointer(Replaced);
4876 ID.AddPointer(Replacement.getAsOpaquePtr());
4877 }
4878
4879 static bool classof(const Type *T) {
4880 return T->getTypeClass() == SubstTemplateTypeParm;
4881 }
4882};
4883
4884/// Represents the result of substituting a set of types for a template
4885/// type parameter pack.
4886///
4887/// When a pack expansion in the source code contains multiple parameter packs
4888/// and those parameter packs correspond to different levels of template
4889/// parameter lists, this type node is used to represent a template type
4890/// parameter pack from an outer level, which has already had its argument pack
4891/// substituted but that still lives within a pack expansion that itself
4892/// could not be instantiated. When actually performing a substitution into
4893/// that pack expansion (e.g., when all template parameters have corresponding
4894/// arguments), this type will be replaced with the \c SubstTemplateTypeParmType
4895/// at the current pack substitution index.
4896class SubstTemplateTypeParmPackType : public Type, public llvm::FoldingSetNode {
4897 friend class ASTContext;
4898
4899 /// The original type parameter.
4900 const TemplateTypeParmType *Replaced;
4901
4902 /// A pointer to the set of template arguments that this
4903 /// parameter pack is instantiated with.
4904 const TemplateArgument *Arguments;
4905
4906 SubstTemplateTypeParmPackType(const TemplateTypeParmType *Param,
4907 QualType Canon,
4908 const TemplateArgument &ArgPack);
4909
4910public:
4911 IdentifierInfo *getIdentifier() const { return Replaced->getIdentifier(); }
4912
4913 /// Gets the template parameter that was substituted for.
4914 const TemplateTypeParmType *getReplacedParameter() const {
4915 return Replaced;
4916 }
4917
4918 unsigned getNumArgs() const {
4919 return SubstTemplateTypeParmPackTypeBits.NumArgs;
4920 }
4921
4922 bool isSugared() const { return false; }
4923 QualType desugar() const { return QualType(this, 0); }
4924
4925 TemplateArgument getArgumentPack() const;
4926
4927 void Profile(llvm::FoldingSetNodeID &ID);
4928 static void Profile(llvm::FoldingSetNodeID &ID,
4929 const TemplateTypeParmType *Replaced,
4930 const TemplateArgument &ArgPack);
4931
4932 static bool classof(const Type *T) {
4933 return T->getTypeClass() == SubstTemplateTypeParmPack;
4934 }
4935};
4936
4937/// Common base class for placeholders for types that get replaced by
4938/// placeholder type deduction: C++11 auto, C++14 decltype(auto), C++17 deduced
4939/// class template types, and constrained type names.
4940///
4941/// These types are usually a placeholder for a deduced type. However, before
4942/// the initializer is attached, or (usually) if the initializer is
4943/// type-dependent, there is no deduced type and the type is canonical. In
4944/// the latter case, it is also a dependent type.
4945class DeducedType : public Type {
4946protected:
4947 DeducedType(TypeClass TC, QualType DeducedAsType,
4948 TypeDependence ExtraDependence)
4949 : Type(TC,
4950 // FIXME: Retain the sugared deduced type?
4951 DeducedAsType.isNull() ? QualType(this, 0)
4952 : DeducedAsType.getCanonicalType(),
4953 ExtraDependence | (DeducedAsType.isNull()
4954 ? TypeDependence::None
4955 : DeducedAsType->getDependence() &
4956 ~TypeDependence::VariablyModified)) {}
4957
4958public:
4959 bool isSugared() const { return !isCanonicalUnqualified(); }
4960 QualType desugar() const { return getCanonicalTypeInternal(); }
4961
4962 /// Get the type deduced for this placeholder type, or null if it's
4963 /// either not been deduced or was deduced to a dependent type.
4964 QualType getDeducedType() const {
4965 return !isCanonicalUnqualified() ? getCanonicalTypeInternal() : QualType();
4966 }
4967 bool isDeduced() const {
4968 return !isCanonicalUnqualified() || isDependentType();
4969 }
4970
4971 static bool classof(const Type *T) {
4972 return T->getTypeClass() == Auto ||
4973 T->getTypeClass() == DeducedTemplateSpecialization;
4974 }
4975};
4976
4977/// Represents a C++11 auto or C++14 decltype(auto) type, possibly constrained
4978/// by a type-constraint.
4979class alignas(8) AutoType : public DeducedType, public llvm::FoldingSetNode {
4980 friend class ASTContext; // ASTContext creates these
4981
4982 ConceptDecl *TypeConstraintConcept;
4983
4984 AutoType(QualType DeducedAsType, AutoTypeKeyword Keyword,
4985 TypeDependence ExtraDependence, ConceptDecl *CD,
4986 ArrayRef<TemplateArgument> TypeConstraintArgs);
4987
4988 const TemplateArgument *getArgBuffer() const {
4989 return reinterpret_cast<const TemplateArgument*>(this+1);
4990 }
4991
4992 TemplateArgument *getArgBuffer() {
4993 return reinterpret_cast<TemplateArgument*>(this+1);
4994 }
4995
4996public:
4997 /// Retrieve the template arguments.
4998 const TemplateArgument *getArgs() const {
4999 return getArgBuffer();
5000 }
5001
5002 /// Retrieve the number of template arguments.
5003 unsigned getNumArgs() const {
5004 return AutoTypeBits.NumArgs;
5005 }
5006
5007 const TemplateArgument &getArg(unsigned Idx) const; // in TemplateBase.h
5008
5009 ArrayRef<TemplateArgument> getTypeConstraintArguments() const {
5010 return {getArgs(), getNumArgs()};
5011 }
5012
5013 ConceptDecl *getTypeConstraintConcept() const {
5014 return TypeConstraintConcept;
5015 }
5016
5017 bool isConstrained() const {
5018 return TypeConstraintConcept != nullptr;
5019 }
5020
5021 bool isDecltypeAuto() const {
5022 return getKeyword() == AutoTypeKeyword::DecltypeAuto;
5023 }
5024
5025 AutoTypeKeyword getKeyword() const {
5026 return (AutoTypeKeyword)AutoTypeBits.Keyword;
5027 }
5028
5029 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) {
5030 Profile(ID, Context, getDeducedType(), getKeyword(), isDependentType(),
5031 getTypeConstraintConcept(), getTypeConstraintArguments());
5032 }
5033
5034 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
5035 QualType Deduced, AutoTypeKeyword Keyword,
5036 bool IsDependent, ConceptDecl *CD,
5037 ArrayRef<TemplateArgument> Arguments);
5038
5039 static bool classof(const Type *T) {
5040 return T->getTypeClass() == Auto;
5041 }
5042};
5043
5044/// Represents a C++17 deduced template specialization type.
5045class DeducedTemplateSpecializationType : public DeducedType,
5046 public llvm::FoldingSetNode {
5047 friend class ASTContext; // ASTContext creates these
5048
5049 /// The name of the template whose arguments will be deduced.
5050 TemplateName Template;
5051
5052 DeducedTemplateSpecializationType(TemplateName Template,
5053 QualType DeducedAsType,
5054 bool IsDeducedAsDependent)
5055 : DeducedType(DeducedTemplateSpecialization, DeducedAsType,
5056 toTypeDependence(Template.getDependence()) |
5057 (IsDeducedAsDependent
5058 ? TypeDependence::DependentInstantiation
5059 : TypeDependence::None)),
5060 Template(Template) {}
5061
5062public:
5063 /// Retrieve the name of the template that we are deducing.
5064 TemplateName getTemplateName() const { return Template;}
5065
5066 void Profile(llvm::FoldingSetNodeID &ID) {
5067 Profile(ID, getTemplateName(), getDeducedType(), isDependentType());
5068 }
5069
5070 static void Profile(llvm::FoldingSetNodeID &ID, TemplateName Template,
5071 QualType Deduced, bool IsDependent) {
5072 Template.Profile(ID);
5073 ID.AddPointer(Deduced.getAsOpaquePtr());
5074 ID.AddBoolean(IsDependent);
5075 }
5076
5077 static bool classof(const Type *T) {
5078 return T->getTypeClass() == DeducedTemplateSpecialization;
5079 }
5080};
5081
5082/// Represents a type template specialization; the template
5083/// must be a class template, a type alias template, or a template
5084/// template parameter. A template which cannot be resolved to one of
5085/// these, e.g. because it is written with a dependent scope
5086/// specifier, is instead represented as a
5087/// @c DependentTemplateSpecializationType.
5088///
5089/// A non-dependent template specialization type is always "sugar",
5090/// typically for a \c RecordType. For example, a class template
5091/// specialization type of \c vector<int> will refer to a tag type for
5092/// the instantiation \c std::vector<int, std::allocator<int>>
5093///
5094/// Template specializations are dependent if either the template or
5095/// any of the template arguments are dependent, in which case the
5096/// type may also be canonical.
5097///
5098/// Instances of this type are allocated with a trailing array of
5099/// TemplateArguments, followed by a QualType representing the
5100/// non-canonical aliased type when the template is a type alias
5101/// template.
5102class alignas(8) TemplateSpecializationType
5103 : public Type,
5104 public llvm::FoldingSetNode {
5105 friend class ASTContext; // ASTContext creates these
5106
5107 /// The name of the template being specialized. This is
5108 /// either a TemplateName::Template (in which case it is a
5109 /// ClassTemplateDecl*, a TemplateTemplateParmDecl*, or a
5110 /// TypeAliasTemplateDecl*), a
5111 /// TemplateName::SubstTemplateTemplateParmPack, or a
5112 /// TemplateName::SubstTemplateTemplateParm (in which case the
5113 /// replacement must, recursively, be one of these).
5114 TemplateName Template;
5115
5116 TemplateSpecializationType(TemplateName T,
5117 ArrayRef<TemplateArgument> Args,
5118 QualType Canon,
5119 QualType Aliased);
5120
5121public:
5122 /// Determine whether any of the given template arguments are dependent.
5123 ///
5124 /// The converted arguments should be supplied when known; whether an
5125 /// argument is dependent can depend on the conversions performed on it
5126 /// (for example, a 'const int' passed as a template argument might be
5127 /// dependent if the parameter is a reference but non-dependent if the
5128 /// parameter is an int).
5129 ///
5130 /// Note that the \p Args parameter is unused: this is intentional, to remind
5131 /// the caller that they need to pass in the converted arguments, not the
5132 /// specified arguments.
5133 static bool
5134 anyDependentTemplateArguments(ArrayRef<TemplateArgumentLoc> Args,
5135 ArrayRef<TemplateArgument> Converted);
5136 static bool
5137 anyDependentTemplateArguments(const TemplateArgumentListInfo &,
5138 ArrayRef<TemplateArgument> Converted);
5139 static bool anyInstantiationDependentTemplateArguments(
5140 ArrayRef<TemplateArgumentLoc> Args);
5141
5142 /// True if this template specialization type matches a current
5143 /// instantiation in the context in which it is found.
5144 bool isCurrentInstantiation() const {
5145 return isa<InjectedClassNameType>(getCanonicalTypeInternal());
5146 }
5147
5148 /// Determine if this template specialization type is for a type alias
5149 /// template that has been substituted.
5150 ///
5151 /// Nearly every template specialization type whose template is an alias
5152 /// template will be substituted. However, this is not the case when
5153 /// the specialization contains a pack expansion but the template alias
5154 /// does not have a corresponding parameter pack, e.g.,
5155 ///
5156 /// \code
5157 /// template<typename T, typename U, typename V> struct S;
5158 /// template<typename T, typename U> using A = S<T, int, U>;
5159 /// template<typename... Ts> struct X {
5160 /// typedef A<Ts...> type; // not a type alias
5161 /// };
5162 /// \endcode
5163 bool isTypeAlias() const { return TemplateSpecializationTypeBits.TypeAlias; }
5164
5165 /// Get the aliased type, if this is a specialization of a type alias
5166 /// template.
5167 QualType getAliasedType() const {
5168 assert(isTypeAlias() && "not a type alias template specialization")(static_cast<void> (0));
5169 return *reinterpret_cast<const QualType*>(end());
5170 }
5171
5172 using iterator = const TemplateArgument *;
5173
5174 iterator begin() const { return getArgs(); }
5175 iterator end() const; // defined inline in TemplateBase.h
5176
5177 /// Retrieve the name of the template that we are specializing.
5178 TemplateName getTemplateName() const { return Template; }
5179
5180 /// Retrieve the template arguments.
5181 const TemplateArgument *getArgs() const {
5182 return reinterpret_cast<const TemplateArgument *>(this + 1);
5183 }
5184
5185 /// Retrieve the number of template arguments.
5186 unsigned getNumArgs() const {
5187 return TemplateSpecializationTypeBits.NumArgs;
5188 }
5189
5190 /// Retrieve a specific template argument as a type.
5191 /// \pre \c isArgType(Arg)
5192 const TemplateArgument &getArg(unsigned Idx) const; // in TemplateBase.h
5193
5194 ArrayRef<TemplateArgument> template_arguments() const {
5195 return {getArgs(), getNumArgs()};
5196 }
5197
5198 bool isSugared() const {
5199 return !isDependentType() || isCurrentInstantiation() || isTypeAlias();
5200 }
5201
5202 QualType desugar() const {
5203 return isTypeAlias() ? getAliasedType() : getCanonicalTypeInternal();
5204 }
5205
5206 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx) {
5207 Profile(ID, Template, template_arguments(), Ctx);
5208 if (isTypeAlias())
5209 getAliasedType().Profile(ID);
5210 }
5211
5212 static void Profile(llvm::FoldingSetNodeID &ID, TemplateName T,
5213 ArrayRef<TemplateArgument> Args,
5214 const ASTContext &Context);
5215
5216 static bool classof(const Type *T) {
5217 return T->getTypeClass() == TemplateSpecialization;
5218 }
5219};
5220
5221/// Print a template argument list, including the '<' and '>'
5222/// enclosing the template arguments.
5223void printTemplateArgumentList(raw_ostream &OS,
5224 ArrayRef<TemplateArgument> Args,
5225 const PrintingPolicy &Policy,
5226 const TemplateParameterList *TPL = nullptr);
5227
5228void printTemplateArgumentList(raw_ostream &OS,
5229 ArrayRef<TemplateArgumentLoc> Args,
5230 const PrintingPolicy &Policy,
5231 const TemplateParameterList *TPL = nullptr);
5232
5233void printTemplateArgumentList(raw_ostream &OS,
5234 const TemplateArgumentListInfo &Args,
5235 const PrintingPolicy &Policy,
5236 const TemplateParameterList *TPL = nullptr);
5237
5238/// The injected class name of a C++ class template or class
5239/// template partial specialization. Used to record that a type was
5240/// spelled with a bare identifier rather than as a template-id; the
5241/// equivalent for non-templated classes is just RecordType.
5242///
5243/// Injected class name types are always dependent. Template
5244/// instantiation turns these into RecordTypes.
5245///
5246/// Injected class name types are always canonical. This works
5247/// because it is impossible to compare an injected class name type
5248/// with the corresponding non-injected template type, for the same
5249/// reason that it is impossible to directly compare template
5250/// parameters from different dependent contexts: injected class name
5251/// types can only occur within the scope of a particular templated
5252/// declaration, and within that scope every template specialization
5253/// will canonicalize to the injected class name (when appropriate
5254/// according to the rules of the language).
5255class InjectedClassNameType : public Type {
5256 friend class ASTContext; // ASTContext creates these.
5257 friend class ASTNodeImporter;
5258 friend class ASTReader; // FIXME: ASTContext::getInjectedClassNameType is not
5259 // currently suitable for AST reading, too much
5260 // interdependencies.
5261 template <class T> friend class serialization::AbstractTypeReader;
5262
5263 CXXRecordDecl *Decl;
5264
5265 /// The template specialization which this type represents.
5266 /// For example, in
5267 /// template <class T> class A { ... };
5268 /// this is A<T>, whereas in
5269 /// template <class X, class Y> class A<B<X,Y> > { ... };
5270 /// this is A<B<X,Y> >.
5271 ///
5272 /// It is always unqualified, always a template specialization type,
5273 /// and always dependent.
5274 QualType InjectedType;
5275
5276 InjectedClassNameType(CXXRecordDecl *D, QualType TST)
5277 : Type(InjectedClassName, QualType(),
5278 TypeDependence::DependentInstantiation),
5279 Decl(D), InjectedType(TST) {
5280 assert(isa<TemplateSpecializationType>(TST))(static_cast<void> (0));
5281 assert(!TST.hasQualifiers())(static_cast<void> (0));
5282 assert(TST->isDependentType())(static_cast<void> (0));
5283 }
5284
5285public:
5286 QualType getInjectedSpecializationType() const { return InjectedType; }
5287
5288 const TemplateSpecializationType *getInjectedTST() const {
5289 return cast<TemplateSpecializationType>(InjectedType.getTypePtr());
5290 }
5291
5292 TemplateName getTemplateName() const {
5293 return getInjectedTST()->getTemplateName();
5294 }
5295
5296 CXXRecordDecl *getDecl() const;
5297
5298 bool isSugared() const { return false; }
5299 QualType desugar() const { return QualType(this, 0); }
5300
5301 static bool classof(const Type *T) {
5302 return T->getTypeClass() == InjectedClassName;
5303 }
5304};
5305
5306/// The kind of a tag type.
5307enum TagTypeKind {
5308 /// The "struct" keyword.
5309 TTK_Struct,
5310
5311 /// The "__interface" keyword.
5312 TTK_Interface,
5313
5314 /// The "union" keyword.
5315 TTK_Union,
5316
5317 /// The "class" keyword.
5318 TTK_Class,
5319
5320 /// The "enum" keyword.
5321 TTK_Enum
5322};
5323
5324/// The elaboration keyword that precedes a qualified type name or
5325/// introduces an elaborated-type-specifier.
5326enum ElaboratedTypeKeyword {
5327 /// The "struct" keyword introduces the elaborated-type-specifier.
5328 ETK_Struct,
5329
5330 /// The "__interface" keyword introduces the elaborated-type-specifier.
5331 ETK_Interface,
5332
5333 /// The "union" keyword introduces the elaborated-type-specifier.
5334 ETK_Union,
5335
5336 /// The "class" keyword introduces the elaborated-type-specifier.
5337 ETK_Class,
5338
5339 /// The "enum" keyword introduces the elaborated-type-specifier.
5340 ETK_Enum,
5341
5342 /// The "typename" keyword precedes the qualified type name, e.g.,
5343 /// \c typename T::type.
5344 ETK_Typename,
5345
5346 /// No keyword precedes the qualified type name.
5347 ETK_None
5348};
5349
5350/// A helper class for Type nodes having an ElaboratedTypeKeyword.
5351/// The keyword in stored in the free bits of the base class.
5352/// Also provides a few static helpers for converting and printing
5353/// elaborated type keyword and tag type kind enumerations.
5354class TypeWithKeyword : public Type {
5355protected:
5356 TypeWithKeyword(ElaboratedTypeKeyword Keyword, TypeClass tc,
5357 QualType Canonical, TypeDependence Dependence)
5358 : Type(tc, Canonical, Dependence) {
5359 TypeWithKeywordBits.Keyword = Keyword;
5360 }
5361
5362public:
5363 ElaboratedTypeKeyword getKeyword() const {
5364 return static_cast<ElaboratedTypeKeyword>(TypeWithKeywordBits.Keyword);
5365 }
5366
5367 /// Converts a type specifier (DeclSpec::TST) into an elaborated type keyword.
5368 static ElaboratedTypeKeyword getKeywordForTypeSpec(unsigned TypeSpec);
5369
5370 /// Converts a type specifier (DeclSpec::TST) into a tag type kind.
5371 /// It is an error to provide a type specifier which *isn't* a tag kind here.
5372 static TagTypeKind getTagTypeKindForTypeSpec(unsigned TypeSpec);
5373
5374 /// Converts a TagTypeKind into an elaborated type keyword.
5375 static ElaboratedTypeKeyword getKeywordForTagTypeKind(TagTypeKind Tag);
5376
5377 /// Converts an elaborated type keyword into a TagTypeKind.
5378 /// It is an error to provide an elaborated type keyword
5379 /// which *isn't* a tag kind here.
5380 static TagTypeKind getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword);
5381
5382 static bool KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword);
5383
5384 static StringRef getKeywordName(ElaboratedTypeKeyword Keyword);
5385
5386 static StringRef getTagTypeKindName(TagTypeKind Kind) {
5387 return getKeywordName(getKeywordForTagTypeKind(Kind));
5388 }
5389
5390 class CannotCastToThisType {};
5391 static CannotCastToThisType classof(const Type *);
5392};
5393
5394/// Represents a type that was referred to using an elaborated type
5395/// keyword, e.g., struct S, or via a qualified name, e.g., N::M::type,
5396/// or both.
5397///
5398/// This type is used to keep track of a type name as written in the
5399/// source code, including tag keywords and any nested-name-specifiers.
5400/// The type itself is always "sugar", used to express what was written
5401/// in the source code but containing no additional semantic information.
5402class ElaboratedType final
5403 : public TypeWithKeyword,
5404 public llvm::FoldingSetNode,
5405 private llvm::TrailingObjects<ElaboratedType, TagDecl *> {
5406 friend class ASTContext; // ASTContext creates these
5407 friend TrailingObjects;
5408
5409 /// The nested name specifier containing the qualifier.
5410 NestedNameSpecifier *NNS;
5411
5412 /// The type that this qualified name refers to.
5413 QualType NamedType;
5414
5415 /// The (re)declaration of this tag type owned by this occurrence is stored
5416 /// as a trailing object if there is one. Use getOwnedTagDecl to obtain
5417 /// it, or obtain a null pointer if there is none.
5418
5419 ElaboratedType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS,
5420 QualType NamedType, QualType CanonType, TagDecl *OwnedTagDecl)
5421 : TypeWithKeyword(Keyword, Elaborated, CanonType,
5422 // Any semantic dependence on the qualifier will have
5423 // been incorporated into NamedType. We still need to
5424 // track syntactic (instantiation / error / pack)
5425 // dependence on the qualifier.
5426 NamedType->getDependence() |
5427 (NNS ? toSyntacticDependence(
5428 toTypeDependence(NNS->getDependence()))
5429 : TypeDependence::None)),
5430 NNS(NNS), NamedType(NamedType) {
5431 ElaboratedTypeBits.HasOwnedTagDecl = false;
5432 if (OwnedTagDecl) {
5433 ElaboratedTypeBits.HasOwnedTagDecl = true;
5434 *getTrailingObjects<TagDecl *>() = OwnedTagDecl;
5435 }
5436 assert(!(Keyword == ETK_None && NNS == nullptr) &&(static_cast<void> (0))
5437 "ElaboratedType cannot have elaborated type keyword "(static_cast<void> (0))
5438 "and name qualifier both null.")(static_cast<void> (0));
5439 }
5440
5441public:
5442 /// Retrieve the qualification on this type.
5443 NestedNameSpecifier *getQualifier() const { return NNS; }
5444
5445 /// Retrieve the type named by the qualified-id.
5446 QualType getNamedType() const { return NamedType; }
5447
5448 /// Remove a single level of sugar.
5449 QualType desugar() const { return getNamedType(); }
5450
5451 /// Returns whether this type directly provides sugar.
5452 bool isSugared() const { return true; }
5453
5454 /// Return the (re)declaration of this type owned by this occurrence of this
5455 /// type, or nullptr if there is none.
5456 TagDecl *getOwnedTagDecl() const {
5457 return ElaboratedTypeBits.HasOwnedTagDecl ? *getTrailingObjects<TagDecl *>()
5458 : nullptr;
5459 }
5460
5461 void Profile(llvm::FoldingSetNodeID &ID) {
5462 Profile(ID, getKeyword(), NNS, NamedType, getOwnedTagDecl());
5463 }
5464
5465 static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword,
5466 NestedNameSpecifier *NNS, QualType NamedType,
5467 TagDecl *OwnedTagDecl) {
5468 ID.AddInteger(Keyword);
5469 ID.AddPointer(NNS);
5470 NamedType.Profile(ID);
5471 ID.AddPointer(OwnedTagDecl);
5472 }
5473
5474 static bool classof(const Type *T) { return T->getTypeClass() == Elaborated; }
5475};
5476
5477/// Represents a qualified type name for which the type name is
5478/// dependent.
5479///
5480/// DependentNameType represents a class of dependent types that involve a
5481/// possibly dependent nested-name-specifier (e.g., "T::") followed by a
5482/// name of a type. The DependentNameType may start with a "typename" (for a
5483/// typename-specifier), "class", "struct", "union", or "enum" (for a
5484/// dependent elaborated-type-specifier), or nothing (in contexts where we
5485/// know that we must be referring to a type, e.g., in a base class specifier).
5486/// Typically the nested-name-specifier is dependent, but in MSVC compatibility
5487/// mode, this type is used with non-dependent names to delay name lookup until
5488/// instantiation.
5489class DependentNameType : public TypeWithKeyword, public llvm::FoldingSetNode {
5490 friend class ASTContext; // ASTContext creates these
5491
5492 /// The nested name specifier containing the qualifier.
5493 NestedNameSpecifier *NNS;
5494
5495 /// The type that this typename specifier refers to.
5496 const IdentifierInfo *Name;
5497
5498 DependentNameType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS,
5499 const IdentifierInfo *Name, QualType CanonType)
5500 : TypeWithKeyword(Keyword, DependentName, CanonType,
5501 TypeDependence::DependentInstantiation |
5502 toTypeDependence(NNS->getDependence())),
5503 NNS(NNS), Name(Name) {}
5504
5505public:
5506 /// Retrieve the qualification on this type.
5507 NestedNameSpecifier *getQualifier() const { return NNS; }
5508
5509 /// Retrieve the type named by the typename specifier as an identifier.
5510 ///
5511 /// This routine will return a non-NULL identifier pointer when the
5512 /// form of the original typename was terminated by an identifier,
5513 /// e.g., "typename T::type".
5514 const IdentifierInfo *getIdentifier() const {
5515 return Name;
5516 }
5517
5518 bool isSugared() const { return false; }
5519 QualType desugar() const { return QualType(this, 0); }
5520
5521 void Profile(llvm::FoldingSetNodeID &ID) {
5522 Profile(ID, getKeyword(), NNS, Name);
5523 }
5524
5525 static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword,
5526 NestedNameSpecifier *NNS, const IdentifierInfo *Name) {
5527 ID.AddInteger(Keyword);
5528 ID.AddPointer(NNS);
5529 ID.AddPointer(Name);
5530 }
5531
5532 static bool classof(const Type *T) {
5533 return T->getTypeClass() == DependentName;
5534 }
5535};
5536
5537/// Represents a template specialization type whose template cannot be
5538/// resolved, e.g.
5539/// A<T>::template B<T>
5540class alignas(8) DependentTemplateSpecializationType
5541 : public TypeWithKeyword,
5542 public llvm::FoldingSetNode {
5543 friend class ASTContext; // ASTContext creates these
5544
5545 /// The nested name specifier containing the qualifier.
5546 NestedNameSpecifier *NNS;
5547
5548 /// The identifier of the template.
5549 const IdentifierInfo *Name;
5550
5551 DependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword,
5552 NestedNameSpecifier *NNS,
5553 const IdentifierInfo *Name,
5554 ArrayRef<TemplateArgument> Args,
5555 QualType Canon);
5556
5557 const TemplateArgument *getArgBuffer() const {
5558 return reinterpret_cast<const TemplateArgument*>(this+1);
5559 }
5560
5561 TemplateArgument *getArgBuffer() {
5562 return reinterpret_cast<TemplateArgument*>(this+1);
5563 }
5564
5565public:
5566 NestedNameSpecifier *getQualifier() const { return NNS; }
5567 const IdentifierInfo *getIdentifier() const { return Name; }
5568
5569 /// Retrieve the template arguments.
5570 const TemplateArgument *getArgs() const {
5571 return getArgBuffer();
5572 }
5573
5574 /// Retrieve the number of template arguments.
5575 unsigned getNumArgs() const {
5576 return DependentTemplateSpecializationTypeBits.NumArgs;
5577 }
5578
5579 const TemplateArgument &getArg(unsigned Idx) const; // in TemplateBase.h
5580
5581 ArrayRef<TemplateArgument> template_arguments() const {
5582 return {getArgs(), getNumArgs()};
5583 }
5584
5585 using iterator = const TemplateArgument *;
5586
5587 iterator begin() const { return getArgs(); }
5588 iterator end() const; // inline in TemplateBase.h
5589
5590 bool isSugared() const { return false; }
5591 QualType desugar() const { return QualType(this, 0); }
5592
5593 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) {
5594 Profile(ID, Context, getKeyword(), NNS, Name, {getArgs(), getNumArgs()});
5595 }
5596
5597 static void Profile(llvm::FoldingSetNodeID &ID,
5598 const ASTContext &Context,
5599 ElaboratedTypeKeyword Keyword,
5600 NestedNameSpecifier *Qualifier,
5601 const IdentifierInfo *Name,
5602 ArrayRef<TemplateArgument> Args);
5603
5604 static bool classof(const Type *T) {
5605 return T->getTypeClass() == DependentTemplateSpecialization;
5606 }
5607};
5608
5609/// Represents a pack expansion of types.
5610///
5611/// Pack expansions are part of C++11 variadic templates. A pack
5612/// expansion contains a pattern, which itself contains one or more
5613/// "unexpanded" parameter packs. When instantiated, a pack expansion
5614/// produces a series of types, each instantiated from the pattern of
5615/// the expansion, where the Ith instantiation of the pattern uses the
5616/// Ith arguments bound to each of the unexpanded parameter packs. The
5617/// pack expansion is considered to "expand" these unexpanded
5618/// parameter packs.
5619///
5620/// \code
5621/// template<typename ...Types> struct tuple;
5622///
5623/// template<typename ...Types>
5624/// struct tuple_of_references {
5625/// typedef tuple<Types&...> type;
5626/// };
5627/// \endcode
5628///
5629/// Here, the pack expansion \c Types&... is represented via a
5630/// PackExpansionType whose pattern is Types&.
5631class PackExpansionType : public Type, public llvm::FoldingSetNode {
5632 friend class ASTContext; // ASTContext creates these
5633
5634 /// The pattern of the pack expansion.
5635 QualType Pattern;
5636
5637 PackExpansionType(QualType Pattern, QualType Canon,
5638 Optional<unsigned> NumExpansions)
5639 : Type(PackExpansion, Canon,
5640 (Pattern->getDependence() | TypeDependence::Dependent |
5641 TypeDependence::Instantiation) &
5642 ~TypeDependence::UnexpandedPack),
5643 Pattern(Pattern) {
5644 PackExpansionTypeBits.NumExpansions =
5645 NumExpansions ? *NumExpansions + 1 : 0;
5646 }
5647
5648public:
5649 /// Retrieve the pattern of this pack expansion, which is the
5650 /// type that will be repeatedly instantiated when instantiating the
5651 /// pack expansion itself.
5652 QualType getPattern() const { return Pattern; }
5653
5654 /// Retrieve the number of expansions that this pack expansion will
5655 /// generate, if known.
5656 Optional<unsigned> getNumExpansions() const {
5657 if (PackExpansionTypeBits.NumExpansions)
5658 return PackExpansionTypeBits.NumExpansions - 1;
5659 return None;
5660 }
5661
5662 bool isSugared() const { return false; }
5663 QualType desugar() const { return QualType(this, 0); }
5664
5665 void Profile(llvm::FoldingSetNodeID &ID) {
5666 Profile(ID, getPattern(), getNumExpansions());
5667 }
5668
5669 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pattern,
5670 Optional<unsigned> NumExpansions) {
5671 ID.AddPointer(Pattern.getAsOpaquePtr());
5672 ID.AddBoolean(NumExpansions.hasValue());
5673 if (NumExpansions)
5674 ID.AddInteger(*NumExpansions);
5675 }
5676
5677 static bool classof(const Type *T) {
5678 return T->getTypeClass() == PackExpansion;
5679 }
5680};
5681
5682/// This class wraps the list of protocol qualifiers. For types that can
5683/// take ObjC protocol qualifers, they can subclass this class.
5684template <class T>
5685class ObjCProtocolQualifiers {
5686protected:
5687 ObjCProtocolQualifiers() = default;
5688
5689 ObjCProtocolDecl * const *getProtocolStorage() const {
5690 return const_cast<ObjCProtocolQualifiers*>(this)->getProtocolStorage();
5691 }
5692
5693 ObjCProtocolDecl **getProtocolStorage() {
5694 return static_cast<T*>(this)->getProtocolStorageImpl();
5695 }
5696
5697 void setNumProtocols(unsigned N) {
5698 static_cast<T*>(this)->setNumProtocolsImpl(N);
5699 }
5700
5701 void initialize(ArrayRef<ObjCProtocolDecl *> protocols) {
5702 setNumProtocols(protocols.size());
5703 assert(getNumProtocols() == protocols.size() &&(static_cast<void> (0))
5704 "bitfield overflow in protocol count")(static_cast<void> (0));
5705 if (!protocols.empty())
5706 memcpy(getProtocolStorage(), protocols.data(),
5707 protocols.size() * sizeof(ObjCProtocolDecl*));
5708 }
5709
5710public:
5711 using qual_iterator = ObjCProtocolDecl * const *;
5712 using qual_range = llvm::iterator_range<qual_iterator>;
5713
5714 qual_range quals() const { return qual_range(qual_begin(), qual_end()); }
5715 qual_iterator qual_begin() const { return getProtocolStorage(); }
5716 qual_iterator qual_end() const { return qual_begin() + getNumProtocols(); }
5717
5718 bool qual_empty() const { return getNumProtocols() == 0; }
5719
5720 /// Return the number of qualifying protocols in this type, or 0 if
5721 /// there are none.
5722 unsigned getNumProtocols() const {
5723 return static_cast<const T*>(this)->getNumProtocolsImpl();
5724 }
5725
5726 /// Fetch a protocol by index.
5727 ObjCProtocolDecl *getProtocol(unsigned I) const {
5728 assert(I < getNumProtocols() && "Out-of-range protocol access")(static_cast<void> (0));
5729 return qual_begin()[I];
5730 }
5731
5732 /// Retrieve all of the protocol qualifiers.
5733 ArrayRef<ObjCProtocolDecl *> getProtocols() const {
5734 return ArrayRef<ObjCProtocolDecl *>(qual_begin(), getNumProtocols());
5735 }
5736};
5737
5738/// Represents a type parameter type in Objective C. It can take
5739/// a list of protocols.
5740class ObjCTypeParamType : public Type,
5741 public ObjCProtocolQualifiers<ObjCTypeParamType>,
5742 public llvm::FoldingSetNode {
5743 friend class ASTContext;
5744 friend class ObjCProtocolQualifiers<ObjCTypeParamType>;
5745
5746 /// The number of protocols stored on this type.
5747 unsigned NumProtocols : 6;
5748
5749 ObjCTypeParamDecl *OTPDecl;
5750
5751 /// The protocols are stored after the ObjCTypeParamType node. In the
5752 /// canonical type, the list of protocols are sorted alphabetically
5753 /// and uniqued.
5754 ObjCProtocolDecl **getProtocolStorageImpl();
5755
5756 /// Return the number of qualifying protocols in this interface type,
5757 /// or 0 if there are none.
5758 unsigned getNumProtocolsImpl() const {
5759 return NumProtocols;
5760 }
5761
5762 void setNumProtocolsImpl(unsigned N) {
5763 NumProtocols = N;
5764 }
5765
5766 ObjCTypeParamType(const ObjCTypeParamDecl *D,
5767 QualType can,
5768 ArrayRef<ObjCProtocolDecl *> protocols);
5769
5770public:
5771 bool isSugared() const { return true; }
5772 QualType desugar() const { return getCanonicalTypeInternal(); }
5773
5774 static bool classof(const Type *T) {
5775 return T->getTypeClass() == ObjCTypeParam;
5776 }
5777
5778 void Profile(llvm::FoldingSetNodeID &ID);
5779 static void Profile(llvm::FoldingSetNodeID &ID,
5780 const ObjCTypeParamDecl *OTPDecl,
5781 QualType CanonicalType,
5782 ArrayRef<ObjCProtocolDecl *> protocols);
5783
5784 ObjCTypeParamDecl *getDecl() const { return OTPDecl; }
5785};
5786
5787/// Represents a class type in Objective C.
5788///
5789/// Every Objective C type is a combination of a base type, a set of
5790/// type arguments (optional, for parameterized classes) and a list of
5791/// protocols.
5792///
5793/// Given the following declarations:
5794/// \code
5795/// \@class C<T>;
5796/// \@protocol P;
5797/// \endcode
5798///
5799/// 'C' is an ObjCInterfaceType C. It is sugar for an ObjCObjectType
5800/// with base C and no protocols.
5801///
5802/// 'C<P>' is an unspecialized ObjCObjectType with base C and protocol list [P].
5803/// 'C<C*>' is a specialized ObjCObjectType with type arguments 'C*' and no
5804/// protocol list.
5805/// 'C<C*><P>' is a specialized ObjCObjectType with base C, type arguments 'C*',
5806/// and protocol list [P].
5807///
5808/// 'id' is a TypedefType which is sugar for an ObjCObjectPointerType whose
5809/// pointee is an ObjCObjectType with base BuiltinType::ObjCIdType
5810/// and no protocols.
5811///
5812/// 'id<P>' is an ObjCObjectPointerType whose pointee is an ObjCObjectType
5813/// with base BuiltinType::ObjCIdType and protocol list [P]. Eventually
5814/// this should get its own sugar class to better represent the source.
5815class ObjCObjectType : public Type,
5816 public ObjCProtocolQualifiers<ObjCObjectType> {
5817 friend class ObjCProtocolQualifiers<ObjCObjectType>;
5818
5819 // ObjCObjectType.NumTypeArgs - the number of type arguments stored
5820 // after the ObjCObjectPointerType node.
5821 // ObjCObjectType.NumProtocols - the number of protocols stored
5822 // after the type arguments of ObjCObjectPointerType node.
5823 //
5824 // These protocols are those written directly on the type. If
5825 // protocol qualifiers ever become additive, the iterators will need
5826 // to get kindof complicated.
5827 //
5828 // In the canonical object type, these are sorted alphabetically
5829 // and uniqued.
5830
5831 /// Either a BuiltinType or an InterfaceType or sugar for either.
5832 QualType BaseType;
5833
5834 /// Cached superclass type.
5835 mutable llvm::PointerIntPair<const ObjCObjectType *, 1, bool>
5836 CachedSuperClassType;
5837
5838 QualType *getTypeArgStorage();
5839 const QualType *getTypeArgStorage() const {
5840 return const_cast<ObjCObjectType *>(this)->getTypeArgStorage();
5841 }
5842
5843 ObjCProtocolDecl **getProtocolStorageImpl();
5844 /// Return the number of qualifying protocols in this interface type,
5845 /// or 0 if there are none.
5846 unsigned getNumProtocolsImpl() const {
5847 return ObjCObjectTypeBits.NumProtocols;
5848 }
5849 void setNumProtocolsImpl(unsigned N) {
5850 ObjCObjectTypeBits.NumProtocols = N;
5851 }
5852
5853protected:
5854 enum Nonce_ObjCInterface { Nonce_ObjCInterface };
5855
5856 ObjCObjectType(QualType Canonical, QualType Base,
5857 ArrayRef<QualType> typeArgs,
5858 ArrayRef<ObjCProtocolDecl *> protocols,
5859 bool isKindOf);
5860
5861 ObjCObjectType(enum Nonce_ObjCInterface)
5862 : Type(ObjCInterface, QualType(), TypeDependence::None),
5863 BaseType(QualType(this_(), 0)) {
5864 ObjCObjectTypeBits.NumProtocols = 0;
5865 ObjCObjectTypeBits.NumTypeArgs = 0;
5866 ObjCObjectTypeBits.IsKindOf = 0;
5867 }
5868
5869 void computeSuperClassTypeSlow() const;
5870
5871public:
5872 /// Gets the base type of this object type. This is always (possibly
5873 /// sugar for) one of:
5874 /// - the 'id' builtin type (as opposed to the 'id' type visible to the
5875 /// user, which is a typedef for an ObjCObjectPointerType)
5876 /// - the 'Class' builtin type (same caveat)
5877 /// - an ObjCObjectType (currently always an ObjCInterfaceType)
5878 QualType getBaseType() const { return BaseType; }
5879
5880 bool isObjCId() const {
5881 return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCId);
5882 }
5883
5884 bool isObjCClass() const {
5885 return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCClass);
5886 }
5887
5888 bool isObjCUnqualifiedId() const { return qual_empty() && isObjCId(); }
5889 bool isObjCUnqualifiedClass() const { return qual_empty() && isObjCClass(); }
5890 bool isObjCUnqualifiedIdOrClass() const {
5891 if (!qual_empty()) return false;
5892 if (const BuiltinType *T = getBaseType()->getAs<BuiltinType>())
5893 return T->getKind() == BuiltinType::ObjCId ||
5894 T->getKind() == BuiltinType::ObjCClass;
5895 return false;
5896 }
5897 bool isObjCQualifiedId() const { return !qual_empty() && isObjCId(); }
5898 bool isObjCQualifiedClass() const { return !qual_empty() && isObjCClass(); }
5899
5900 /// Gets the interface declaration for this object type, if the base type
5901 /// really is an interface.
5902 ObjCInterfaceDecl *getInterface() const;
5903
5904 /// Determine whether this object type is "specialized", meaning
5905 /// that it has type arguments.
5906 bool isSpecialized() const;
5907
5908 /// Determine whether this object type was written with type arguments.
5909 bool isSpecializedAsWritten() const {
5910 return ObjCObjectTypeBits.NumTypeArgs > 0;
5911 }
5912
5913 /// Determine whether this object type is "unspecialized", meaning
5914 /// that it has no type arguments.
5915 bool isUnspecialized() const { return !isSpecialized(); }
5916
5917 /// Determine whether this object type is "unspecialized" as
5918 /// written, meaning that it has no type arguments.
5919 bool isUnspecializedAsWritten() const { return !isSpecializedAsWritten(); }
5920
5921 /// Retrieve the type arguments of this object type (semantically).
5922 ArrayRef<QualType> getTypeArgs() const;
5923
5924 /// Retrieve the type arguments of this object type as they were
5925 /// written.
5926 ArrayRef<QualType> getTypeArgsAsWritten() const {
5927 return llvm::makeArrayRef(getTypeArgStorage(),
5928 ObjCObjectTypeBits.NumTypeArgs);
5929 }
5930
5931 /// Whether this is a "__kindof" type as written.
5932 bool isKindOfTypeAsWritten() const { return ObjCObjectTypeBits.IsKindOf; }
5933
5934 /// Whether this ia a "__kindof" type (semantically).
5935 bool isKindOfType() const;
5936
5937 /// Retrieve the type of the superclass of this object type.
5938 ///
5939 /// This operation substitutes any type arguments into the
5940 /// superclass of the current class type, potentially producing a
5941 /// specialization of the superclass type. Produces a null type if
5942 /// there is no superclass.
5943 QualType getSuperClassType() const {
5944 if (!CachedSuperClassType.getInt())
5945 computeSuperClassTypeSlow();
5946
5947 assert(CachedSuperClassType.getInt() && "Superclass not set?")(static_cast<void> (0));
5948 return QualType(CachedSuperClassType.getPointer(), 0);
5949 }
5950
5951 /// Strip off the Objective-C "kindof" type and (with it) any
5952 /// protocol qualifiers.
5953 QualType stripObjCKindOfTypeAndQuals(const ASTContext &ctx) const;
5954
5955 bool isSugared() const { return false; }
5956 QualType desugar() const { return QualType(this, 0); }
5957
5958 static bool classof(const Type *T) {
5959 return T->getTypeClass() == ObjCObject ||
5960 T->getTypeClass() == ObjCInterface;
5961 }
5962};
5963
5964/// A class providing a concrete implementation
5965/// of ObjCObjectType, so as to not increase the footprint of
5966/// ObjCInterfaceType. Code outside of ASTContext and the core type
5967/// system should not reference this type.
5968class ObjCObjectTypeImpl : public ObjCObjectType, public llvm::FoldingSetNode {
5969 friend class ASTContext;
5970
5971 // If anyone adds fields here, ObjCObjectType::getProtocolStorage()
5972 // will need to be modified.
5973
5974 ObjCObjectTypeImpl(QualType Canonical, QualType Base,
5975 ArrayRef<QualType> typeArgs,
5976 ArrayRef<ObjCProtocolDecl *> protocols,
5977 bool isKindOf)
5978 : ObjCObjectType(Canonical, Base, typeArgs, protocols, isKindOf) {}
5979
5980public:
5981 void Profile(llvm::FoldingSetNodeID &ID);
5982 static void Profile(llvm::FoldingSetNodeID &ID,
5983 QualType Base,
5984 ArrayRef<QualType> typeArgs,
5985 ArrayRef<ObjCProtocolDecl *> protocols,
5986 bool isKindOf);
5987};
5988
5989inline QualType *ObjCObjectType::getTypeArgStorage() {
5990 return reinterpret_cast<QualType *>(static_cast<ObjCObjectTypeImpl*>(this)+1);
5991}
5992
5993inline ObjCProtocolDecl **ObjCObjectType::getProtocolStorageImpl() {
5994 return reinterpret_cast<ObjCProtocolDecl**>(
5995 getTypeArgStorage() + ObjCObjectTypeBits.NumTypeArgs);
5996}
5997
5998inline ObjCProtocolDecl **ObjCTypeParamType::getProtocolStorageImpl() {
5999 return reinterpret_cast<ObjCProtocolDecl**>(
6000 static_cast<ObjCTypeParamType*>(this)+1);
6001}
6002
6003/// Interfaces are the core concept in Objective-C for object oriented design.
6004/// They basically correspond to C++ classes. There are two kinds of interface
6005/// types: normal interfaces like `NSString`, and qualified interfaces, which
6006/// are qualified with a protocol list like `NSString<NSCopyable, NSAmazing>`.
6007///
6008/// ObjCInterfaceType guarantees the following properties when considered
6009/// as a subtype of its superclass, ObjCObjectType:
6010/// - There are no protocol qualifiers. To reinforce this, code which
6011/// tries to invoke the protocol methods via an ObjCInterfaceType will
6012/// fail to compile.
6013/// - It is its own base type. That is, if T is an ObjCInterfaceType*,
6014/// T->getBaseType() == QualType(T, 0).
6015class ObjCInterfaceType : public ObjCObjectType {
6016 friend class ASTContext; // ASTContext creates these.
6017 friend class ASTReader;
6018 friend class ObjCInterfaceDecl;
6019 template <class T> friend class serialization::AbstractTypeReader;
6020
6021 mutable ObjCInterfaceDecl *Decl;
6022
6023 ObjCInterfaceType(const ObjCInterfaceDecl *D)
6024 : ObjCObjectType(Nonce_ObjCInterface),
6025 Decl(const_cast<ObjCInterfaceDecl*>(D)) {}
6026
6027public:
6028 /// Get the declaration of this interface.
6029 ObjCInterfaceDecl *getDecl() const { return Decl; }
6030
6031 bool isSugared() const { return false; }
6032 QualType desugar() const { return QualType(this, 0); }
6033
6034 static bool classof(const Type *T) {
6035 return T->getTypeClass() == ObjCInterface;
6036 }
6037
6038 // Nonsense to "hide" certain members of ObjCObjectType within this
6039 // class. People asking for protocols on an ObjCInterfaceType are
6040 // not going to get what they want: ObjCInterfaceTypes are
6041 // guaranteed to have no protocols.
6042 enum {
6043 qual_iterator,
6044 qual_begin,
6045 qual_end,
6046 getNumProtocols,
6047 getProtocol
6048 };
6049};
6050
6051inline ObjCInterfaceDecl *ObjCObjectType::getInterface() const {
6052 QualType baseType = getBaseType();
6053 while (const auto *ObjT = baseType->getAs<ObjCObjectType>()) {
6054 if (const auto *T = dyn_cast<ObjCInterfaceType>(ObjT))
6055 return T->getDecl();
6056
6057 baseType = ObjT->getBaseType();
6058 }
6059
6060 return nullptr;
6061}
6062
6063/// Represents a pointer to an Objective C object.
6064///
6065/// These are constructed from pointer declarators when the pointee type is
6066/// an ObjCObjectType (or sugar for one). In addition, the 'id' and 'Class'
6067/// types are typedefs for these, and the protocol-qualified types 'id<P>'
6068/// and 'Class<P>' are translated into these.
6069///
6070/// Pointers to pointers to Objective C objects are still PointerTypes;
6071/// only the first level of pointer gets it own type implementation.
6072class ObjCObjectPointerType : public Type, public llvm::FoldingSetNode {
6073 friend class ASTContext; // ASTContext creates these.
6074
6075 QualType PointeeType;
6076
6077 ObjCObjectPointerType(QualType Canonical, QualType Pointee)
6078 : Type(ObjCObjectPointer, Canonical, Pointee->getDependence()),
6079 PointeeType(Pointee) {}
6080
6081public:
6082 /// Gets the type pointed to by this ObjC pointer.
6083 /// The result will always be an ObjCObjectType or sugar thereof.
6084 QualType getPointeeType() const { return PointeeType; }
6085
6086 /// Gets the type pointed to by this ObjC pointer. Always returns non-null.
6087 ///
6088 /// This method is equivalent to getPointeeType() except that
6089 /// it discards any typedefs (or other sugar) between this
6090 /// type and the "outermost" object type. So for:
6091 /// \code
6092 /// \@class A; \@protocol P; \@protocol Q;
6093 /// typedef A<P> AP;
6094 /// typedef A A1;
6095 /// typedef A1<P> A1P;
6096 /// typedef A1P<Q> A1PQ;
6097 /// \endcode
6098 /// For 'A*', getObjectType() will return 'A'.
6099 /// For 'A<P>*', getObjectType() will return 'A<P>'.
6100 /// For 'AP*', getObjectType() will return 'A<P>'.
6101 /// For 'A1*', getObjectType() will return 'A'.
6102 /// For 'A1<P>*', getObjectType() will return 'A1<P>'.
6103 /// For 'A1P*', getObjectType() will return 'A1<P>'.
6104 /// For 'A1PQ*', getObjectType() will return 'A1<Q>', because
6105 /// adding protocols to a protocol-qualified base discards the
6106 /// old qualifiers (for now). But if it didn't, getObjectType()
6107 /// would return 'A1P<Q>' (and we'd have to make iterating over
6108 /// qualifiers more complicated).
6109 const ObjCObjectType *getObjectType() const {
6110 return PointeeType->castAs<ObjCObjectType>();
6111 }
6112
6113 /// If this pointer points to an Objective C
6114 /// \@interface type, gets the type for that interface. Any protocol
6115 /// qualifiers on the interface are ignored.
6116 ///
6117 /// \return null if the base type for this pointer is 'id' or 'Class'
6118 const ObjCInterfaceType *getInterfaceType() const;
6119
6120 /// If this pointer points to an Objective \@interface
6121 /// type, gets the declaration for that interface.
6122 ///
6123 /// \return null if the base type for this pointer is 'id' or 'Class'
6124 ObjCInterfaceDecl *getInterfaceDecl() const {
6125 return getObjectType()->getInterface();
6126 }
6127
6128 /// True if this is equivalent to the 'id' type, i.e. if
6129 /// its object type is the primitive 'id' type with no protocols.
6130 bool isObjCIdType() const {
6131 return getObjectType()->isObjCUnqualifiedId();
6132 }
6133
6134 /// True if this is equivalent to the 'Class' type,
6135 /// i.e. if its object tive is the primitive 'Class' type with no protocols.
6136 bool isObjCClassType() const {
6137 return getObjectType()->isObjCUnqualifiedClass();
6138 }
6139
6140 /// True if this is equivalent to the 'id' or 'Class' type,
6141 bool isObjCIdOrClassType() const {
6142 return getObjectType()->isObjCUnqualifiedIdOrClass();
6143 }
6144
6145 /// True if this is equivalent to 'id<P>' for some non-empty set of
6146 /// protocols.
6147 bool isObjCQualifiedIdType() const {
6148 return getObjectType()->isObjCQualifiedId();
6149 }
6150
6151 /// True if this is equivalent to 'Class<P>' for some non-empty set of
6152 /// protocols.
6153 bool isObjCQualifiedClassType() const {
6154 return getObjectType()->isObjCQualifiedClass();
6155 }
6156
6157 /// Whether this is a "__kindof" type.
6158 bool isKindOfType() const { return getObjectType()->isKindOfType(); }
6159
6160 /// Whether this type is specialized, meaning that it has type arguments.
6161 bool isSpecialized() const { return getObjectType()->isSpecialized(); }
6162
6163 /// Whether this type is specialized, meaning that it has type arguments.
6164 bool isSpecializedAsWritten() const {
6165 return getObjectType()->isSpecializedAsWritten();
6166 }
6167
6168 /// Whether this type is unspecialized, meaning that is has no type arguments.
6169 bool isUnspecialized() const { return getObjectType()->isUnspecialized(); }
6170
6171 /// Determine whether this object type is "unspecialized" as
6172 /// written, meaning that it has no type arguments.
6173 bool isUnspecializedAsWritten() const { return !isSpecializedAsWritten(); }
6174
6175 /// Retrieve the type arguments for this type.
6176 ArrayRef<QualType> getTypeArgs() const {
6177 return getObjectType()->getTypeArgs();
6178 }
6179
6180 /// Retrieve the type arguments for this type.
6181 ArrayRef<QualType> getTypeArgsAsWritten() const {
6182 return getObjectType()->getTypeArgsAsWritten();
6183 }
6184
6185 /// An iterator over the qualifiers on the object type. Provided
6186 /// for convenience. This will always iterate over the full set of
6187 /// protocols on a type, not just those provided directly.
6188 using qual_iterator = ObjCObjectType::qual_iterator;
6189 using qual_range = llvm::iterator_range<qual_iterator>;
6190
6191 qual_range quals() const { return qual_range(qual_begin(), qual_end()); }
6192
6193 qual_iterator qual_begin() const {
6194 return getObjectType()->qual_begin();
6195 }
6196
6197 qual_iterator qual_end() const {
6198 return getObjectType()->qual_end();
6199 }
6200
6201 bool qual_empty() const { return getObjectType()->qual_empty(); }
6202
6203 /// Return the number of qualifying protocols on the object type.
6204 unsigned getNumProtocols() const {
6205 return getObjectType()->getNumProtocols();
6206 }
6207
6208 /// Retrieve a qualifying protocol by index on the object type.
6209 ObjCProtocolDecl *getProtocol(unsigned I) const {
6210 return getObjectType()->getProtocol(I);
6211 }
6212
6213 bool isSugared() const { return false; }
6214 QualType desugar() const { return QualType(this, 0); }
6215
6216 /// Retrieve the type of the superclass of this object pointer type.
6217 ///
6218 /// This operation substitutes any type arguments into the
6219 /// superclass of the current class type, potentially producing a
6220 /// pointer to a specialization of the superclass type. Produces a
6221 /// null type if there is no superclass.
6222 QualType getSuperClassType() const;
6223
6224 /// Strip off the Objective-C "kindof" type and (with it) any
6225 /// protocol qualifiers.
6226 const ObjCObjectPointerType *stripObjCKindOfTypeAndQuals(
6227 const ASTContext &ctx) const;
6228
6229 void Profile(llvm::FoldingSetNodeID &ID) {
6230 Profile(ID, getPointeeType());
6231 }
6232
6233 static void Profile(llvm::FoldingSetNodeID &ID, QualType T) {
6234 ID.AddPointer(T.getAsOpaquePtr());
6235 }
6236
6237 static bool classof(const Type *T) {
6238 return T->getTypeClass() == ObjCObjectPointer;
6239 }
6240};
6241
6242class AtomicType : public Type, public llvm::FoldingSetNode {
6243 friend class ASTContext; // ASTContext creates these.
6244
6245 QualType ValueType;
6246
6247 AtomicType(QualType ValTy, QualType Canonical)
6248 : Type(Atomic, Canonical, ValTy->getDependence()), ValueType(ValTy) {}
6249
6250public:
6251 /// Gets the type contained by this atomic type, i.e.
6252 /// the type returned by performing an atomic load of this atomic type.
6253 QualType getValueType() const { return ValueType; }
6254
6255 bool isSugared() const { return false; }
6256 QualType desugar() const { return QualType(this, 0); }
6257
6258 void Profile(llvm::FoldingSetNodeID &ID) {
6259 Profile(ID, getValueType());
6260 }
6261
6262 static void Profile(llvm::FoldingSetNodeID &ID, QualType T) {
6263 ID.AddPointer(T.getAsOpaquePtr());
6264 }
6265
6266 static bool classof(const Type *T) {
6267 return T->getTypeClass() == Atomic;
6268 }
6269};
6270
6271/// PipeType - OpenCL20.
6272class PipeType : public Type, public llvm::FoldingSetNode {
6273 friend class ASTContext; // ASTContext creates these.
6274
6275 QualType ElementType;
6276 bool isRead;
6277
6278 PipeType(QualType elemType, QualType CanonicalPtr, bool isRead)
6279 : Type(Pipe, CanonicalPtr, elemType->getDependence()),
6280 ElementType(elemType), isRead(isRead) {}
6281
6282public:
6283 QualType getElementType() const { return ElementType; }
6284
6285 bool isSugared() const { return false; }
6286
6287 QualType desugar() const { return QualType(this, 0); }
6288
6289 void Profile(llvm::FoldingSetNodeID &ID) {
6290 Profile(ID, getElementType(), isReadOnly());
6291 }
6292
6293 static void Profile(llvm::FoldingSetNodeID &ID, QualType T, bool isRead) {
6294 ID.AddPointer(T.getAsOpaquePtr());
6295 ID.AddBoolean(isRead);
6296 }
6297
6298 static bool classof(const Type *T) {
6299 return T->getTypeClass() == Pipe;
6300 }
6301
6302 bool isReadOnly() const { return isRead; }
6303};
6304
6305/// A fixed int type of a specified bitwidth.
6306class ExtIntType final : public Type, public llvm::FoldingSetNode {
6307 friend class ASTContext;
6308 unsigned IsUnsigned : 1;
6309 unsigned NumBits : 24;
6310
6311protected:
6312 ExtIntType(bool isUnsigned, unsigned NumBits);
6313
6314public:
6315 bool isUnsigned() const { return IsUnsigned; }
6316 bool isSigned() const { return !IsUnsigned; }
6317 unsigned getNumBits() const { return NumBits; }
6318
6319 bool isSugared() const { return false; }
6320 QualType desugar() const { return QualType(this, 0); }
6321
6322 void Profile(llvm::FoldingSetNodeID &ID) {
6323 Profile(ID, isUnsigned(), getNumBits());
6324 }
6325
6326 static void Profile(llvm::FoldingSetNodeID &ID, bool IsUnsigned,
6327 unsigned NumBits) {
6328 ID.AddBoolean(IsUnsigned);
6329 ID.AddInteger(NumBits);
6330 }
6331
6332 static bool classof(const Type *T) { return T->getTypeClass() == ExtInt; }
6333};
6334
6335class DependentExtIntType final : public Type, public llvm::FoldingSetNode {
6336 friend class ASTContext;
6337 const ASTContext &Context;
6338 llvm::PointerIntPair<Expr*, 1, bool> ExprAndUnsigned;
6339
6340protected:
6341 DependentExtIntType(const ASTContext &Context, bool IsUnsigned,
6342 Expr *NumBits);
6343
6344public:
6345 bool isUnsigned() const;
6346 bool isSigned() const { return !isUnsigned(); }
6347 Expr *getNumBitsExpr() const;
6348
6349 bool isSugared() const { return false; }
6350 QualType desugar() const { return QualType(this, 0); }
6351
6352 void Profile(llvm::FoldingSetNodeID &ID) {
6353 Profile(ID, Context, isUnsigned(), getNumBitsExpr());
6354 }
6355 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
6356 bool IsUnsigned, Expr *NumBitsExpr);
6357
6358 static bool classof(const Type *T) {
6359 return T->getTypeClass() == DependentExtInt;
6360 }
6361};
6362
6363/// A qualifier set is used to build a set of qualifiers.
6364class QualifierCollector : public Qualifiers {
6365public:
6366 QualifierCollector(Qualifiers Qs = Qualifiers()) : Qualifiers(Qs) {}
6367
6368 /// Collect any qualifiers on the given type and return an
6369 /// unqualified type. The qualifiers are assumed to be consistent
6370 /// with those already in the type.
6371 const Type *strip(QualType type) {
6372 addFastQualifiers(type.getLocalFastQualifiers());
6373 if (!type.hasLocalNonFastQualifiers())
6374 return type.getTypePtrUnsafe();
6375
6376 const ExtQuals *extQuals = type.getExtQualsUnsafe();
6377 addConsistentQualifiers(extQuals->getQualifiers());
6378 return extQuals->getBaseType();
6379 }
6380
6381 /// Apply the collected qualifiers to the given type.
6382 QualType apply(const ASTContext &Context, QualType QT) const;
6383
6384 /// Apply the collected qualifiers to the given type.
6385 QualType apply(const ASTContext &Context, const Type* T) const;
6386};
6387
6388/// A container of type source information.
6389///
6390/// A client can read the relevant info using TypeLoc wrappers, e.g:
6391/// @code
6392/// TypeLoc TL = TypeSourceInfo->getTypeLoc();
6393/// TL.getBeginLoc().print(OS, SrcMgr);
6394/// @endcode
6395class alignas(8) TypeSourceInfo {
6396 // Contains a memory block after the class, used for type source information,
6397 // allocated by ASTContext.
6398 friend class ASTContext;
6399
6400 QualType Ty;
6401
6402 TypeSourceInfo(QualType ty) : Ty(ty) {}
6403
6404public:
6405 /// Return the type wrapped by this type source info.
6406 QualType getType() const { return Ty; }
6407
6408 /// Return the TypeLoc wrapper for the type source info.
6409 TypeLoc getTypeLoc() const; // implemented in TypeLoc.h
6410
6411 /// Override the type stored in this TypeSourceInfo. Use with caution!
6412 void overrideType(QualType T) { Ty = T; }
6413};
6414
6415// Inline function definitions.
6416
6417inline SplitQualType SplitQualType::getSingleStepDesugaredType() const {
6418 SplitQualType desugar =
6419 Ty->getLocallyUnqualifiedSingleStepDesugaredType().split();
6420 desugar.Quals.addConsistentQualifiers(Quals);
6421 return desugar;
6422}
6423
6424inline const Type *QualType::getTypePtr() const {
6425 return getCommonPtr()->BaseType;
6426}
6427
6428inline const Type *QualType::getTypePtrOrNull() const {
6429 return (isNull() ? nullptr : getCommonPtr()->BaseType);
6430}
6431
6432inline SplitQualType QualType::split() const {
6433 if (!hasLocalNonFastQualifiers())
6434 return SplitQualType(getTypePtrUnsafe(),
6435 Qualifiers::fromFastMask(getLocalFastQualifiers()));
6436
6437 const ExtQuals *eq = getExtQualsUnsafe();
6438 Qualifiers qs = eq->getQualifiers();
6439 qs.addFastQualifiers(getLocalFastQualifiers());
6440 return SplitQualType(eq->getBaseType(), qs);
6441}
6442
6443inline Qualifiers QualType::getLocalQualifiers() const {
6444 Qualifiers Quals;
6445 if (hasLocalNonFastQualifiers())
6446 Quals = getExtQualsUnsafe()->getQualifiers();
6447 Quals.addFastQualifiers(getLocalFastQualifiers());
6448 return Quals;
6449}
6450
6451inline Qualifiers QualType::getQualifiers() const {
6452 Qualifiers quals = getCommonPtr()->CanonicalType.getLocalQualifiers();
6453 quals.addFastQualifiers(getLocalFastQualifiers());
6454 return quals;
6455}
6456
6457inline unsigned QualType::getCVRQualifiers() const {
6458 unsigned cvr = getCommonPtr()->CanonicalType.getLocalCVRQualifiers();
6459 cvr |= getLocalCVRQualifiers();
6460 return cvr;
6461}
6462
6463inline QualType QualType::getCanonicalType() const {
6464 QualType canon = getCommonPtr()->CanonicalType;
6465 return canon.withFastQualifiers(getLocalFastQualifiers());
6466}
6467
6468inline bool QualType::isCanonical() const {
6469 return getTypePtr()->isCanonicalUnqualified();
6470}
6471
6472inline bool QualType::isCanonicalAsParam() const {
6473 if (!isCanonical()) return false;
6474 if (hasLocalQualifiers()) return false;
6475
6476 const Type *T = getTypePtr();
6477 if (T->isVariablyModifiedType() && T->hasSizedVLAType())
6478 return false;
6479
6480 return !isa<FunctionType>(T) && !isa<ArrayType>(T);
6481}
6482
6483inline bool QualType::isConstQualified() const {
6484 return isLocalConstQualified() ||
6485 getCommonPtr()->CanonicalType.isLocalConstQualified();
6486}
6487
6488inline bool QualType::isRestrictQualified() const {
6489 return isLocalRestrictQualified() ||
6490 getCommonPtr()->CanonicalType.isLocalRestrictQualified();
6491}
6492
6493
6494inline bool QualType::isVolatileQualified() const {
6495 return isLocalVolatileQualified() ||
6496 getCommonPtr()->CanonicalType.isLocalVolatileQualified();
6497}
6498
6499inline bool QualType::hasQualifiers() const {
6500 return hasLocalQualifiers() ||
6501 getCommonPtr()->CanonicalType.hasLocalQualifiers();
6502}
6503
6504inline QualType QualType::getUnqualifiedType() const {
6505 if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers())
6506 return QualType(getTypePtr(), 0);
6507
6508 return QualType(getSplitUnqualifiedTypeImpl(*this).Ty, 0);
6509}
6510
6511inline SplitQualType QualType::getSplitUnqualifiedType() const {
6512 if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers())
6513 return split();
6514
6515 return getSplitUnqualifiedTypeImpl(*this);
6516}
6517
6518inline void QualType::removeLocalConst() {
6519 removeLocalFastQualifiers(Qualifiers::Const);
6520}
6521
6522inline void QualType::removeLocalRestrict() {
6523 removeLocalFastQualifiers(Qualifiers::Restrict);
6524}
6525
6526inline void QualType::removeLocalVolatile() {
6527 removeLocalFastQualifiers(Qualifiers::Volatile);
6528}
6529
6530inline void QualType::removeLocalCVRQualifiers(unsigned Mask) {
6531 assert(!(Mask & ~Qualifiers::CVRMask) && "mask has non-CVR bits")(static_cast<void> (0));
6532 static_assert((int)Qualifiers::CVRMask == (int)Qualifiers::FastMask,
6533 "Fast bits differ from CVR bits!");
6534
6535 // Fast path: we don't need to touch the slow qualifiers.
6536 removeLocalFastQualifiers(Mask);
6537}
6538
6539/// Check if this type has any address space qualifier.
6540inline bool QualType::hasAddressSpace() const {
6541 return getQualifiers().hasAddressSpace();
6542}
6543
6544/// Return the address space of this type.
6545inline LangAS QualType::getAddressSpace() const {
6546 return getQualifiers().getAddressSpace();
6547}
6548
6549/// Return the gc attribute of this type.
6550inline Qualifiers::GC QualType::getObjCGCAttr() const {
6551 return getQualifiers().getObjCGCAttr();
6552}
6553
6554inline bool QualType::hasNonTrivialToPrimitiveDefaultInitializeCUnion() const {
6555 if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl())
6556 return hasNonTrivialToPrimitiveDefaultInitializeCUnion(RD);
6557 return false;
6558}
6559
6560inline bool QualType::hasNonTrivialToPrimitiveDestructCUnion() const {
6561 if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl())
6562 return hasNonTrivialToPrimitiveDestructCUnion(RD);
6563 return false;
6564}
6565
6566inline bool QualType::hasNonTrivialToPrimitiveCopyCUnion() const {
6567 if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl())
6568 return hasNonTrivialToPrimitiveCopyCUnion(RD);
6569 return false;
6570}
6571
6572inline FunctionType::ExtInfo getFunctionExtInfo(const Type &t) {
6573 if (const auto *PT = t.getAs<PointerType>()) {
6574 if (const auto *FT = PT->getPointeeType()->getAs<FunctionType>())
6575 return FT->getExtInfo();
6576 } else if (const auto *FT = t.getAs<FunctionType>())
6577 return FT->getExtInfo();
6578
6579 return FunctionType::ExtInfo();
6580}
6581
6582inline FunctionType::ExtInfo getFunctionExtInfo(QualType t) {
6583 return getFunctionExtInfo(*t);
6584}
6585
6586/// Determine whether this type is more
6587/// qualified than the Other type. For example, "const volatile int"
6588/// is more qualified than "const int", "volatile int", and
6589/// "int". However, it is not more qualified than "const volatile
6590/// int".
6591inline bool QualType::isMoreQualifiedThan(QualType other) const {
6592 Qualifiers MyQuals = getQualifiers();
6593 Qualifiers OtherQuals = other.getQualifiers();
6594 return (MyQuals != OtherQuals && MyQuals.compatiblyIncludes(OtherQuals));
6595}
6596
6597/// Determine whether this type is at last
6598/// as qualified as the Other type. For example, "const volatile
6599/// int" is at least as qualified as "const int", "volatile int",
6600/// "int", and "const volatile int".
6601inline bool QualType::isAtLeastAsQualifiedAs(QualType other) const {
6602 Qualifiers OtherQuals = other.getQualifiers();
6603
6604 // Ignore __unaligned qualifier if this type is a void.
6605 if (getUnqualifiedType()->isVoidType())
6606 OtherQuals.removeUnaligned();
6607
6608 return getQualifiers().compatiblyIncludes(OtherQuals);
6609}
6610
6611/// If Type is a reference type (e.g., const
6612/// int&), returns the type that the reference refers to ("const
6613/// int"). Otherwise, returns the type itself. This routine is used
6614/// throughout Sema to implement C++ 5p6:
6615///
6616/// If an expression initially has the type "reference to T" (8.3.2,
6617/// 8.5.3), the type is adjusted to "T" prior to any further
6618/// analysis, the expression designates the object or function
6619/// denoted by the reference, and the expression is an lvalue.
6620inline QualType QualType::getNonReferenceType() const {
6621 if (const auto *RefType = (*this)->getAs<ReferenceType>())
6622 return RefType->getPointeeType();
6623 else
6624 return *this;
6625}
6626
6627inline bool QualType::isCForbiddenLValueType() const {
6628 return ((getTypePtr()->isVoidType() && !hasQualifiers()) ||
6629 getTypePtr()->isFunctionType());
6630}
6631
6632/// Tests whether the type is categorized as a fundamental type.
6633///
6634/// \returns True for types specified in C++0x [basic.fundamental].
6635inline bool Type::isFundamentalType() const {
6636 return isVoidType() ||
6637 isNullPtrType() ||
6638 // FIXME: It's really annoying that we don't have an
6639 // 'isArithmeticType()' which agrees with the standard definition.
6640 (isArithmeticType() && !isEnumeralType());
6641}
6642
6643/// Tests whether the type is categorized as a compound type.
6644///
6645/// \returns True for types specified in C++0x [basic.compound].
6646inline bool Type::isCompoundType() const {
6647 // C++0x [basic.compound]p1:
6648 // Compound types can be constructed in the following ways:
6649 // -- arrays of objects of a given type [...];
6650 return isArrayType() ||
6651 // -- functions, which have parameters of given types [...];
6652 isFunctionType() ||
6653 // -- pointers to void or objects or functions [...];
6654 isPointerType() ||
6655 // -- references to objects or functions of a given type. [...]
6656 isReferenceType() ||
6657 // -- classes containing a sequence of objects of various types, [...];
6658 isRecordType() ||
6659 // -- unions, which are classes capable of containing objects of different
6660 // types at different times;
6661 isUnionType() ||
6662 // -- enumerations, which comprise a set of named constant values. [...];
6663 isEnumeralType() ||
6664 // -- pointers to non-static class members, [...].
6665 isMemberPointerType();
6666}
6667
6668inline bool Type::isFunctionType() const {
6669 return isa<FunctionType>(CanonicalType);
6670}
6671
6672inline bool Type::isPointerType() const {
6673 return isa<PointerType>(CanonicalType);
6674}
6675
6676inline bool Type::isAnyPointerType() const {
6677 return isPointerType() || isObjCObjectPointerType();
6678}
6679
6680inline bool Type::isBlockPointerType() const {
6681 return isa<BlockPointerType>(CanonicalType);
6682}
6683
6684inline bool Type::isReferenceType() const {
6685 return isa<ReferenceType>(CanonicalType);
6686}
6687
6688inline bool Type::isLValueReferenceType() const {
6689 return isa<LValueReferenceType>(CanonicalType);
6690}
6691
6692inline bool Type::isRValueReferenceType() const {
6693 return isa<RValueReferenceType>(CanonicalType);
6694}
6695
6696inline bool Type::isObjectPointerType() const {
6697 // Note: an "object pointer type" is not the same thing as a pointer to an
6698 // object type; rather, it is a pointer to an object type or a pointer to cv
6699 // void.
6700 if (const auto *T = getAs<PointerType>())
6701 return !T->getPointeeType()->isFunctionType();
6702 else
6703 return false;
6704}
6705
6706inline bool Type::isFunctionPointerType() const {
6707 if (const auto *T = getAs<PointerType>())
6708 return T->getPointeeType()->isFunctionType();
6709 else
6710 return false;
6711}
6712
6713inline bool Type::isFunctionReferenceType() const {
6714 if (const auto *T = getAs<ReferenceType>())
6715 return T->getPointeeType()->isFunctionType();
6716 else
6717 return false;
6718}
6719
6720inline bool Type::isMemberPointerType() const {
6721 return isa<MemberPointerType>(CanonicalType);
6722}
6723
6724inline bool Type::isMemberFunctionPointerType() const {
6725 if (const auto *T = getAs<MemberPointerType>())
6726 return T->isMemberFunctionPointer();
6727 else
6728 return false;
6729}
6730
6731inline bool Type::isMemberDataPointerType() const {
6732 if (const auto *T = getAs<MemberPointerType>())
6733 return T->isMemberDataPointer();
6734 else
6735 return false;
6736}
6737
6738inline bool Type::isArrayType() const {
6739 return isa<ArrayType>(CanonicalType);
6740}
6741
6742inline bool Type::isConstantArrayType() const {
6743 return isa<ConstantArrayType>(CanonicalType);
6744}
6745
6746inline bool Type::isIncompleteArrayType() const {
6747 return isa<IncompleteArrayType>(CanonicalType);
6748}
6749
6750inline bool Type::isVariableArrayType() const {
6751 return isa<VariableArrayType>(CanonicalType);
6752}
6753
6754inline bool Type::isDependentSizedArrayType() const {
6755 return isa<DependentSizedArrayType>(CanonicalType);
6756}
6757
6758inline bool Type::isBuiltinType() const {
6759 return isa<BuiltinType>(CanonicalType);
6760}
6761
6762inline bool Type::isRecordType() const {
6763 return isa<RecordType>(CanonicalType);
6764}
6765
6766inline bool Type::isEnumeralType() const {
6767 return isa<EnumType>(CanonicalType);
6768}
6769
6770inline bool Type::isAnyComplexType() const {
6771 return isa<ComplexType>(CanonicalType);
6772}
6773
6774inline bool Type::isVectorType() const {
6775 return isa<VectorType>(CanonicalType);
6776}
6777
6778inline bool Type::isExtVectorType() const {
6779 return isa<ExtVectorType>(CanonicalType);
6780}
6781
6782inline bool Type::isMatrixType() const {
6783 return isa<MatrixType>(CanonicalType);
6784}
6785
6786inline bool Type::isConstantMatrixType() const {
6787 return isa<ConstantMatrixType>(CanonicalType);
6788}
6789
6790inline bool Type::isDependentAddressSpaceType() const {
6791 return isa<DependentAddressSpaceType>(CanonicalType);
6792}
6793
6794inline bool Type::isObjCObjectPointerType() const {
6795 return isa<ObjCObjectPointerType>(CanonicalType);
6796}
6797
6798inline bool Type::isObjCObjectType() const {
6799 return isa<ObjCObjectType>(CanonicalType);
6800}
6801
6802inline bool Type::isObjCObjectOrInterfaceType() const {
6803 return isa<ObjCInterfaceType>(CanonicalType) ||
6804 isa<ObjCObjectType>(CanonicalType);
6805}
6806
6807inline bool Type::isAtomicType() const {
6808 return isa<AtomicType>(CanonicalType);
6809}
6810
6811inline bool Type::isUndeducedAutoType() const {
6812 return isa<AutoType>(CanonicalType);
6813}
6814
6815inline bool Type::isObjCQualifiedIdType() const {
6816 if (const auto *OPT = getAs<ObjCObjectPointerType>())
6817 return OPT->isObjCQualifiedIdType();
6818 return false;
6819}
6820
6821inline bool Type::isObjCQualifiedClassType() const {
6822 if (const auto *OPT = getAs<ObjCObjectPointerType>())
6823 return OPT->isObjCQualifiedClassType();
6824 return false;
6825}
6826
6827inline bool Type::isObjCIdType() const {
6828 if (const auto *OPT = getAs<ObjCObjectPointerType>())
6829 return OPT->isObjCIdType();
6830 return false;
6831}
6832
6833inline bool Type::isObjCClassType() const {
6834 if (const auto *OPT = getAs<ObjCObjectPointerType>())
6835 return OPT->isObjCClassType();
6836 return false;
6837}
6838
6839inline bool Type::isObjCSelType() const {
6840 if (const auto *OPT = getAs<PointerType>())
6841 return OPT->getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCSel);
6842 return false;
6843}
6844
6845inline bool Type::isObjCBuiltinType() const {
6846 return isObjCIdType() || isObjCClassType() || isObjCSelType();
6847}
6848
6849inline bool Type::isDecltypeType() const {
6850 return isa<DecltypeType>(this);
6851}
6852
6853#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
6854 inline bool Type::is##Id##Type() const { \
6855 return isSpecificBuiltinType(BuiltinType::Id); \
6856 }
6857#include "clang/Basic/OpenCLImageTypes.def"
6858
6859inline bool Type::isSamplerT() const {
6860 return isSpecificBuiltinType(BuiltinType::OCLSampler);
6861}
6862
6863inline bool Type::isEventT() const {
6864 return isSpecificBuiltinType(BuiltinType::OCLEvent);
6865}
6866
6867inline bool Type::isClkEventT() const {
6868 return isSpecificBuiltinType(BuiltinType::OCLClkEvent);
6869}
6870
6871inline bool Type::isQueueT() const {
6872 return isSpecificBuiltinType(BuiltinType::OCLQueue);
6873}
6874
6875inline bool Type::isReserveIDT() const {
6876 return isSpecificBuiltinType(BuiltinType::OCLReserveID);
6877}
6878
6879inline bool Type::isImageType() const {
6880#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) is##Id##Type() ||
6881 return
6882#include "clang/Basic/OpenCLImageTypes.def"
6883 false; // end boolean or operation
6884}
6885
6886inline bool Type::isPipeType() const {
6887 return isa<PipeType>(CanonicalType);
6888}
6889
6890inline bool Type::isExtIntType() const {
6891 return isa<ExtIntType>(CanonicalType);
6892}
6893
6894#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
6895 inline bool Type::is##Id##Type() const { \
6896 return isSpecificBuiltinType(BuiltinType::Id); \
6897 }
6898#include "clang/Basic/OpenCLExtensionTypes.def"
6899
6900inline bool Type::isOCLIntelSubgroupAVCType() const {
6901#define INTEL_SUBGROUP_AVC_TYPE(ExtType, Id) \
6902 isOCLIntelSubgroupAVC##Id##Type() ||
6903 return
6904#include "clang/Basic/OpenCLExtensionTypes.def"
6905 false; // end of boolean or operation
6906}
6907
6908inline bool Type::isOCLExtOpaqueType() const {
6909#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) is##Id##Type() ||
6910 return
6911#include "clang/Basic/OpenCLExtensionTypes.def"
6912 false; // end of boolean or operation
6913}
6914
6915inline bool Type::isOpenCLSpecificType() const {
6916 return isSamplerT() || isEventT() || isImageType() || isClkEventT() ||
6917 isQueueT() || isReserveIDT() || isPipeType() || isOCLExtOpaqueType();
6918}
6919
6920inline bool Type::isTemplateTypeParmType() const {
6921 return isa<TemplateTypeParmType>(CanonicalType);
6922}
6923
6924inline bool Type::isSpecificBuiltinType(unsigned K) const {
6925 if (const BuiltinType *BT = getAs<BuiltinType>()) {
6926 return BT->getKind() == static_cast<BuiltinType::Kind>(K);
6927 }
6928 return false;
6929}
6930
6931inline bool Type::isPlaceholderType() const {
6932 if (const auto *BT = dyn_cast<BuiltinType>(this))
6933 return BT->isPlaceholderType();
6934 return false;
6935}
6936
6937inline const BuiltinType *Type::getAsPlaceholderType() const {
6938 if (const auto *BT = dyn_cast<BuiltinType>(this))
6939 if (BT->isPlaceholderType())
6940 return BT;
6941 return nullptr;
6942}
6943
6944inline bool Type::isSpecificPlaceholderType(unsigned K) const {
6945 assert(BuiltinType::isPlaceholderTypeKind((BuiltinType::Kind) K))(static_cast<void> (0));
6946 return isSpecificBuiltinType(K);
6947}
6948
6949inline bool Type::isNonOverloadPlaceholderType() const {
6950 if (const auto *BT = dyn_cast<BuiltinType>(this))
6951 return BT->isNonOverloadPlaceholderType();
6952 return false;
6953}
6954
6955inline bool Type::isVoidType() const {
6956 return isSpecificBuiltinType(BuiltinType::Void);
6957}
6958
6959inline bool Type::isHalfType() const {
6960 // FIXME: Should we allow complex __fp16? Probably not.
6961 return isSpecificBuiltinType(BuiltinType::Half);
6962}
6963
6964inline bool Type::isFloat16Type() const {
6965 return isSpecificBuiltinType(BuiltinType::Float16);
6966}
6967
6968inline bool Type::isBFloat16Type() const {
6969 return isSpecificBuiltinType(BuiltinType::BFloat16);
6970}
6971
6972inline bool Type::isFloat128Type() const {
6973 return isSpecificBuiltinType(BuiltinType::Float128);
6974}
6975
6976inline bool Type::isNullPtrType() const {
6977 return isSpecificBuiltinType(BuiltinType::NullPtr);
6978}
6979
6980bool IsEnumDeclComplete(EnumDecl *);
6981bool IsEnumDeclScoped(EnumDecl *);
6982
6983inline bool Type::isIntegerType() const {
6984 if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType))
6985 return BT->getKind() >= BuiltinType::Bool &&
6986 BT->getKind() <= BuiltinType::Int128;
6987 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
6988 // Incomplete enum types are not treated as integer types.
6989 // FIXME: In C++, enum types are never integer types.
6990 return IsEnumDeclComplete(ET->getDecl()) &&
6991 !IsEnumDeclScoped(ET->getDecl());
6992 }
6993 return isExtIntType();
6994}
6995
6996inline bool Type::isFixedPointType() const {
6997 if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) {
6998 return BT->getKind() >= BuiltinType::ShortAccum &&
6999 BT->getKind() <= BuiltinType::SatULongFract;
7000 }
7001 return false;
7002}
7003
7004inline bool Type::isFixedPointOrIntegerType() const {
7005 return isFixedPointType() || isIntegerType();
7006}
7007
7008inline bool Type::isSaturatedFixedPointType() const {
7009 if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) {
7010 return BT->getKind() >= BuiltinType::SatShortAccum &&
7011 BT->getKind() <= BuiltinType::SatULongFract;
7012 }
7013 return false;
7014}
7015
7016inline bool Type::isUnsaturatedFixedPointType() const {
7017 return isFixedPointType() && !isSaturatedFixedPointType();
7018}
7019
7020inline bool Type::isSignedFixedPointType() const {
7021 if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) {
7022 return ((BT->getKind() >= BuiltinType::ShortAccum &&
7023 BT->getKind() <= BuiltinType::LongAccum) ||
7024 (BT->getKind() >= BuiltinType::ShortFract &&
7025 BT->getKind() <= BuiltinType::LongFract) ||
7026 (BT->getKind() >= BuiltinType::SatShortAccum &&
7027 BT->getKind() <= BuiltinType::SatLongAccum) ||
7028 (BT->getKind() >= BuiltinType::SatShortFract &&
7029 BT->getKind() <= BuiltinType::SatLongFract));
7030 }
7031 return false;
7032}
7033
7034inline bool Type::isUnsignedFixedPointType() const {
7035 return isFixedPointType() && !isSignedFixedPointType();
7036}
7037
7038inline bool Type::isScalarType() const {
7039 if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType))
7040 return BT->getKind() > BuiltinType::Void &&
7041 BT->getKind() <= BuiltinType::NullPtr;
7042 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
7043 // Enums are scalar types, but only if they are defined. Incomplete enums
7044 // are not treated as scalar types.
7045 return IsEnumDeclComplete(ET->getDecl());
7046 return isa<PointerType>(CanonicalType) ||
7047 isa<BlockPointerType>(CanonicalType) ||
7048 isa<MemberPointerType>(CanonicalType) ||
7049 isa<ComplexType>(CanonicalType) ||
7050 isa<ObjCObjectPointerType>(CanonicalType) ||
7051 isExtIntType();
7052}
7053
7054inline bool Type::isIntegralOrEnumerationType() const {
7055 if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType))
7056 return BT->getKind() >= BuiltinType::Bool &&
7057 BT->getKind() <= BuiltinType::Int128;
7058
7059 // Check for a complete enum type; incomplete enum types are not properly an
7060 // enumeration type in the sense required here.
7061 if (const auto *ET = dyn_cast<EnumType>(CanonicalType))
7062 return IsEnumDeclComplete(ET->getDecl());
7063
7064 return isExtIntType();
7065}
7066
7067inline bool Type::isBooleanType() const {
7068 if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType))
7069 return BT->getKind() == BuiltinType::Bool;
7070 return false;
7071}
7072
7073inline bool Type::isUndeducedType() const {
7074 auto *DT = getContainedDeducedType();
7075 return DT && !DT->isDeduced();
7076}
7077
7078/// Determines whether this is a type for which one can define
7079/// an overloaded operator.
7080inline bool Type::isOverloadableType() const {
7081 return isDependentType() || isRecordType() || isEnumeralType();
7082}
7083
7084/// Determines whether this type is written as a typedef-name.
7085inline bool Type::isTypedefNameType() const {
7086 if (getAs<TypedefType>())
7087 return true;
7088 if (auto *TST = getAs<TemplateSpecializationType>())
7089 return TST->isTypeAlias();
7090 return false;
7091}
7092
7093/// Determines whether this type can decay to a pointer type.
7094inline bool Type::canDecayToPointerType() const {
7095 return isFunctionType() || isArrayType();
7096}
7097
7098inline bool Type::hasPointerRepresentation() const {
7099 return (isPointerType() || isReferenceType() || isBlockPointerType() ||
7100 isObjCObjectPointerType() || isNullPtrType());
7101}
7102
7103inline bool Type::hasObjCPointerRepresentation() const {
7104 return isObjCObjectPointerType();
7105}
7106
7107inline const Type *Type::getBaseElementTypeUnsafe() const {
7108 const Type *type = this;
7109 while (const ArrayType *arrayType = type->getAsArrayTypeUnsafe())
7110 type = arrayType->getElementType().getTypePtr();
7111 return type;
7112}
7113
7114inline const Type *Type::getPointeeOrArrayElementType() const {
7115 const Type *type = this;
7116 if (type->isAnyPointerType())
7117 return type->getPointeeType().getTypePtr();
7118 else if (type->isArrayType())
7119 return type->getBaseElementTypeUnsafe();
7120 return type;
7121}
7122/// Insertion operator for partial diagnostics. This allows sending adress
7123/// spaces into a diagnostic with <<.
7124inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD,
7125 LangAS AS) {
7126 PD.AddTaggedVal(static_cast<std::underlying_type_t<LangAS>>(AS),
7127 DiagnosticsEngine::ArgumentKind::ak_addrspace);
7128 return PD;
7129}
7130
7131/// Insertion operator for partial diagnostics. This allows sending Qualifiers
7132/// into a diagnostic with <<.
7133inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD,
7134 Qualifiers Q) {
7135 PD.AddTaggedVal(Q.getAsOpaqueValue(),
7136 DiagnosticsEngine::ArgumentKind::ak_qual);
7137 return PD;
7138}
7139
7140/// Insertion operator for partial diagnostics. This allows sending QualType's
7141/// into a diagnostic with <<.
7142inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD,
7143 QualType T) {
7144 PD.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()),
7145 DiagnosticsEngine::ak_qualtype);
7146 return PD;
7147}
7148
7149// Helper class template that is used by Type::getAs to ensure that one does
7150// not try to look through a qualified type to get to an array type.
7151template <typename T>
7152using TypeIsArrayType =
7153 std::integral_constant<bool, std::is_same<T, ArrayType>::value ||
7154 std::is_base_of<ArrayType, T>::value>;
7155
7156// Member-template getAs<specific type>'.
7157template <typename T> const T *Type::getAs() const {
7158 static_assert(!TypeIsArrayType<T>::value,
7159 "ArrayType cannot be used with getAs!");
7160
7161 // If this is directly a T type, return it.
7162 if (const auto *Ty = dyn_cast<T>(this))
7163 return Ty;
7164
7165 // If the canonical form of this type isn't the right kind, reject it.
7166 if (!isa<T>(CanonicalType))
7167 return nullptr;
7168
7169 // If this is a typedef for the type, strip the typedef off without
7170 // losing all typedef information.
7171 return cast<T>(getUnqualifiedDesugaredType());
7172}
7173
7174template <typename T> const T *Type::getAsAdjusted() const {
7175 static_assert(!TypeIsArrayType<T>::value, "ArrayType cannot be used with getAsAdjusted!");
7176
7177 // If this is directly a T type, return it.
7178 if (const auto *Ty = dyn_cast<T>(this))
7179 return Ty;
7180
7181 // If the canonical form of this type isn't the right kind, reject it.
7182 if (!isa<T>(CanonicalType))
7183 return nullptr;
7184
7185 // Strip off type adjustments that do not modify the underlying nature of the
7186 // type.
7187 const Type *Ty = this;
7188 while (Ty) {
7189 if (const auto *A = dyn_cast<AttributedType>(Ty))
7190 Ty = A->getModifiedType().getTypePtr();
7191 else if (const auto *E = dyn_cast<ElaboratedType>(Ty))
7192 Ty = E->desugar().getTypePtr();
7193 else if (const auto *P = dyn_cast<ParenType>(Ty))
7194 Ty = P->desugar().getTypePtr();
7195 else if (const auto *A = dyn_cast<AdjustedType>(Ty))
7196 Ty = A->desugar().getTypePtr();
7197 else if (const auto *M = dyn_cast<MacroQualifiedType>(Ty))
7198 Ty = M->desugar().getTypePtr();
7199 else
7200 break;
7201 }
7202
7203 // Just because the canonical type is correct does not mean we can use cast<>,
7204 // since we may not have stripped off all the sugar down to the base type.
7205 return dyn_cast<T>(Ty);
7206}
7207
7208inline const ArrayType *Type::getAsArrayTypeUnsafe() const {
7209 // If this is directly an array type, return it.
7210 if (const auto *arr = dyn_cast<ArrayType>(this))
7211 return arr;
7212
7213 // If the canonical form of this type isn't the right kind, reject it.
7214 if (!isa<ArrayType>(CanonicalType))
7215 return nullptr;
7216
7217 // If this is a typedef for the type, strip the typedef off without
7218 // losing all typedef information.
7219 return cast<ArrayType>(getUnqualifiedDesugaredType());
7220}
7221
7222template <typename T> const T *Type::castAs() const {
7223 static_assert(!TypeIsArrayType<T>::value,
7224 "ArrayType cannot be used with castAs!");
7225
7226 if (const auto *ty = dyn_cast<T>(this)) return ty;
7227 assert(isa<T>(CanonicalType))(static_cast<void> (0));
7228 return cast<T>(getUnqualifiedDesugaredType());
7229}
7230
7231inline const ArrayType *Type::castAsArrayTypeUnsafe() const {
7232 assert(isa<ArrayType>(CanonicalType))(static_cast<void> (0));
7233 if (const auto *arr = dyn_cast<ArrayType>(this)) return arr;
7234 return cast<ArrayType>(getUnqualifiedDesugaredType());
7235}
7236
7237DecayedType::DecayedType(QualType OriginalType, QualType DecayedPtr,
7238 QualType CanonicalPtr)
7239 : AdjustedType(Decayed, OriginalType, DecayedPtr, CanonicalPtr) {
7240#ifndef NDEBUG1
7241 QualType Adjusted = getAdjustedType();
7242 (void)AttributedType::stripOuterNullability(Adjusted);
7243 assert(isa<PointerType>(Adjusted))(static_cast<void> (0));
7244#endif
7245}
7246
7247QualType DecayedType::getPointeeType() const {
7248 QualType Decayed = getDecayedType();
7249 (void)AttributedType::stripOuterNullability(Decayed);
7250 return cast<PointerType>(Decayed)->getPointeeType();
7251}
7252
7253// Get the decimal string representation of a fixed point type, represented
7254// as a scaled integer.
7255// TODO: At some point, we should change the arguments to instead just accept an
7256// APFixedPoint instead of APSInt and scale.
7257void FixedPointValueToString(SmallVectorImpl<char> &Str, llvm::APSInt Val,
7258 unsigned Scale);
7259
7260} // namespace clang
7261
7262#endif // LLVM_CLANG_AST_TYPE_H

/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/llvm/include/llvm/ADT/PointerUnion.h

1//===- llvm/ADT/PointerUnion.h - Discriminated Union of 2 Ptrs --*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the PointerUnion class, which is a discriminated union of
10// pointer types.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ADT_POINTERUNION_H
15#define LLVM_ADT_POINTERUNION_H
16
17#include "llvm/ADT/DenseMapInfo.h"
18#include "llvm/ADT/PointerIntPair.h"
19#include "llvm/Support/PointerLikeTypeTraits.h"
20#include <cassert>
21#include <cstddef>
22#include <cstdint>
23
24namespace llvm {
25
26namespace pointer_union_detail {
27 /// Determine the number of bits required to store integers with values < n.
28 /// This is ceil(log2(n)).
29 constexpr int bitsRequired(unsigned n) {
30 return n > 1 ? 1 + bitsRequired((n + 1) / 2) : 0;
31 }
32
33 template <typename... Ts> constexpr int lowBitsAvailable() {
34 return std::min<int>({PointerLikeTypeTraits<Ts>::NumLowBitsAvailable...});
35 }
36
37 /// Find the index of a type in a list of types. TypeIndex<T, Us...>::Index
38 /// is the index of T in Us, or sizeof...(Us) if T does not appear in the
39 /// list.
40 template <typename T, typename ...Us> struct TypeIndex;
41 template <typename T, typename ...Us> struct TypeIndex<T, T, Us...> {
42 static constexpr int Index = 0;
43 };
44 template <typename T, typename U, typename... Us>
45 struct TypeIndex<T, U, Us...> {
46 static constexpr int Index = 1 + TypeIndex<T, Us...>::Index;
47 };
48 template <typename T> struct TypeIndex<T> {
49 static constexpr int Index = 0;
50 };
51
52 /// Find the first type in a list of types.
53 template <typename T, typename...> struct GetFirstType {
54 using type = T;
55 };
56
57 /// Provide PointerLikeTypeTraits for void* that is used by PointerUnion
58 /// for the template arguments.
59 template <typename ...PTs> class PointerUnionUIntTraits {
60 public:
61 static inline void *getAsVoidPointer(void *P) { return P; }
62 static inline void *getFromVoidPointer(void *P) { return P; }
63 static constexpr int NumLowBitsAvailable = lowBitsAvailable<PTs...>();
64 };
65
66 template <typename Derived, typename ValTy, int I, typename ...Types>
67 class PointerUnionMembers;
68
69 template <typename Derived, typename ValTy, int I>
70 class PointerUnionMembers<Derived, ValTy, I> {
71 protected:
72 ValTy Val;
73 PointerUnionMembers() = default;
74 PointerUnionMembers(ValTy Val) : Val(Val) {}
75
76 friend struct PointerLikeTypeTraits<Derived>;
77 };
78
79 template <typename Derived, typename ValTy, int I, typename Type,
80 typename ...Types>
81 class PointerUnionMembers<Derived, ValTy, I, Type, Types...>
82 : public PointerUnionMembers<Derived, ValTy, I + 1, Types...> {
83 using Base = PointerUnionMembers<Derived, ValTy, I + 1, Types...>;
84 public:
85 using Base::Base;
86 PointerUnionMembers() = default;
87 PointerUnionMembers(Type V)
88 : Base(ValTy(const_cast<void *>(
89 PointerLikeTypeTraits<Type>::getAsVoidPointer(V)),
90 I)) {}
91
92 using Base::operator=;
93 Derived &operator=(Type V) {
94 this->Val = ValTy(
95 const_cast<void *>(PointerLikeTypeTraits<Type>::getAsVoidPointer(V)),
96 I);
97 return static_cast<Derived &>(*this);
98 };
99 };
100}
101
102/// A discriminated union of two or more pointer types, with the discriminator
103/// in the low bit of the pointer.
104///
105/// This implementation is extremely efficient in space due to leveraging the
106/// low bits of the pointer, while exposing a natural and type-safe API.
107///
108/// Common use patterns would be something like this:
109/// PointerUnion<int*, float*> P;
110/// P = (int*)0;
111/// printf("%d %d", P.is<int*>(), P.is<float*>()); // prints "1 0"
112/// X = P.get<int*>(); // ok.
113/// Y = P.get<float*>(); // runtime assertion failure.
114/// Z = P.get<double*>(); // compile time failure.
115/// P = (float*)0;
116/// Y = P.get<float*>(); // ok.
117/// X = P.get<int*>(); // runtime assertion failure.
118template <typename... PTs>
119class PointerUnion
120 : public pointer_union_detail::PointerUnionMembers<
121 PointerUnion<PTs...>,
122 PointerIntPair<
123 void *, pointer_union_detail::bitsRequired(sizeof...(PTs)), int,
124 pointer_union_detail::PointerUnionUIntTraits<PTs...>>,
125 0, PTs...> {
126 // The first type is special because we want to directly cast a pointer to a
127 // default-initialized union to a pointer to the first type. But we don't
128 // want PointerUnion to be a 'template <typename First, typename ...Rest>'
129 // because it's much more convenient to have a name for the whole pack. So
130 // split off the first type here.
131 using First = typename pointer_union_detail::GetFirstType<PTs...>::type;
132 using Base = typename PointerUnion::PointerUnionMembers;
133
134public:
135 PointerUnion() = default;
136
137 PointerUnion(std::nullptr_t) : PointerUnion() {}
138 using Base::Base;
139
140 /// Test if the pointer held in the union is null, regardless of
141 /// which type it is.
142 bool isNull() const { return !this->Val.getPointer(); }
30
Assuming the condition is false
31
Returning zero, which participates in a condition later
143
144 explicit operator bool() const { return !isNull(); }
145
146 /// Test if the Union currently holds the type matching T.
147 template <typename T> bool is() const {
148 constexpr int Index = pointer_union_detail::TypeIndex<T, PTs...>::Index;
149 static_assert(Index < sizeof...(PTs),
150 "PointerUnion::is<T> given type not in the union");
151 return this->Val.getInt() == Index;
152 }
153
154 /// Returns the value of the specified pointer type.
155 ///
156 /// If the specified pointer type is incorrect, assert.
157 template <typename T> T get() const {
158 assert(is<T>() && "Invalid accessor called")(static_cast<void> (0));
159 return PointerLikeTypeTraits<T>::getFromVoidPointer(this->Val.getPointer());
160 }
161
162 /// Returns the current pointer if it is of the specified pointer type,
163 /// otherwise returns null.
164 template <typename T> T dyn_cast() const {
165 if (is<T>())
166 return get<T>();
167 return T();
168 }
169
170 /// If the union is set to the first pointer type get an address pointing to
171 /// it.
172 First const *getAddrOfPtr1() const {
173 return const_cast<PointerUnion *>(this)->getAddrOfPtr1();
174 }
175
176 /// If the union is set to the first pointer type get an address pointing to
177 /// it.
178 First *getAddrOfPtr1() {
179 assert(is<First>() && "Val is not the first pointer")(static_cast<void> (0));
180 assert((static_cast<void> (0))
181 PointerLikeTypeTraits<First>::getAsVoidPointer(get<First>()) ==(static_cast<void> (0))
182 this->Val.getPointer() &&(static_cast<void> (0))
183 "Can't get the address because PointerLikeTypeTraits changes the ptr")(static_cast<void> (0));
184 return const_cast<First *>(
185 reinterpret_cast<const First *>(this->Val.getAddrOfPointer()));
186 }
187
188 /// Assignment from nullptr which just clears the union.
189 const PointerUnion &operator=(std::nullptr_t) {
190 this->Val.initWithPointer(nullptr);
191 return *this;
192 }
193
194 /// Assignment from elements of the union.
195 using Base::operator=;
196
197 void *getOpaqueValue() const { return this->Val.getOpaqueValue(); }
198 static inline PointerUnion getFromOpaqueValue(void *VP) {
199 PointerUnion V;
200 V.Val = decltype(V.Val)::getFromOpaqueValue(VP);
201 return V;
202 }
203};
204
205template <typename ...PTs>
206bool operator==(PointerUnion<PTs...> lhs, PointerUnion<PTs...> rhs) {
207 return lhs.getOpaqueValue() == rhs.getOpaqueValue();
208}
209
210template <typename ...PTs>
211bool operator!=(PointerUnion<PTs...> lhs, PointerUnion<PTs...> rhs) {
212 return lhs.getOpaqueValue() != rhs.getOpaqueValue();
213}
214
215template <typename ...PTs>
216bool operator<(PointerUnion<PTs...> lhs, PointerUnion<PTs...> rhs) {
217 return lhs.getOpaqueValue() < rhs.getOpaqueValue();
218}
219
220// Teach SmallPtrSet that PointerUnion is "basically a pointer", that has
221// # low bits available = min(PT1bits,PT2bits)-1.
222template <typename ...PTs>
223struct PointerLikeTypeTraits<PointerUnion<PTs...>> {
224 static inline void *getAsVoidPointer(const PointerUnion<PTs...> &P) {
225 return P.getOpaqueValue();
226 }
227
228 static inline PointerUnion<PTs...> getFromVoidPointer(void *P) {
229 return PointerUnion<PTs...>::getFromOpaqueValue(P);
230 }
231
232 // The number of bits available are the min of the pointer types minus the
233 // bits needed for the discriminator.
234 static constexpr int NumLowBitsAvailable = PointerLikeTypeTraits<decltype(
235 PointerUnion<PTs...>::Val)>::NumLowBitsAvailable;
236};
237
238// Teach DenseMap how to use PointerUnions as keys.
239template <typename ...PTs> struct DenseMapInfo<PointerUnion<PTs...>> {
240 using Union = PointerUnion<PTs...>;
241 using FirstInfo =
242 DenseMapInfo<typename pointer_union_detail::GetFirstType<PTs...>::type>;
243
244 static inline Union getEmptyKey() { return Union(FirstInfo::getEmptyKey()); }
245
246 static inline Union getTombstoneKey() {
247 return Union(FirstInfo::getTombstoneKey());
248 }
249
250 static unsigned getHashValue(const Union &UnionVal) {
251 intptr_t key = (intptr_t)UnionVal.getOpaqueValue();
252 return DenseMapInfo<intptr_t>::getHashValue(key);
253 }
254
255 static bool isEqual(const Union &LHS, const Union &RHS) {
256 return LHS == RHS;
257 }
258};
259
260} // end namespace llvm
261
262#endif // LLVM_ADT_POINTERUNION_H