1 //===-- ASTReader.cpp - AST File Reader ----------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ASTReader class, which reads AST files.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Serialization/ASTReader.h"
15 #include "ASTCommon.h"
16 #include "ASTReaderInternals.h"
17 #include "clang/AST/ASTConsumer.h"
18 #include "clang/AST/ASTContext.h"
19 #include "clang/AST/DeclTemplate.h"
20 #include "clang/AST/Expr.h"
21 #include "clang/AST/ExprCXX.h"
22 #include "clang/Frontend/PCHContainerOperations.h"
23 #include "clang/AST/NestedNameSpecifier.h"
24 #include "clang/AST/Type.h"
25 #include "clang/AST/TypeLocVisitor.h"
26 #include "clang/Basic/DiagnosticOptions.h"
27 #include "clang/Basic/FileManager.h"
28 #include "clang/Basic/SourceManager.h"
29 #include "clang/Basic/SourceManagerInternals.h"
30 #include "clang/Basic/TargetInfo.h"
31 #include "clang/Basic/TargetOptions.h"
32 #include "clang/Basic/Version.h"
33 #include "clang/Basic/VersionTuple.h"
34 #include "clang/Frontend/Utils.h"
35 #include "clang/Lex/HeaderSearch.h"
36 #include "clang/Lex/HeaderSearchOptions.h"
37 #include "clang/Lex/MacroInfo.h"
38 #include "clang/Lex/PreprocessingRecord.h"
39 #include "clang/Lex/Preprocessor.h"
40 #include "clang/Lex/PreprocessorOptions.h"
41 #include "clang/Sema/Scope.h"
42 #include "clang/Sema/Sema.h"
43 #include "clang/Serialization/ASTDeserializationListener.h"
44 #include "clang/Serialization/GlobalModuleIndex.h"
45 #include "clang/Serialization/ModuleManager.h"
46 #include "clang/Serialization/SerializationDiagnostic.h"
47 #include "llvm/ADT/Hashing.h"
48 #include "llvm/ADT/StringExtras.h"
49 #include "llvm/Bitcode/BitstreamReader.h"
50 #include "llvm/Support/ErrorHandling.h"
51 #include "llvm/Support/FileSystem.h"
52 #include "llvm/Support/MemoryBuffer.h"
53 #include "llvm/Support/Path.h"
54 #include "llvm/Support/SaveAndRestore.h"
55 #include "llvm/Support/raw_ostream.h"
59 #include <system_error>
61 using namespace clang;
62 using namespace clang::serialization;
63 using namespace clang::serialization::reader;
64 using llvm::BitstreamCursor;
67 //===----------------------------------------------------------------------===//
68 // ChainedASTReaderListener implementation
69 //===----------------------------------------------------------------------===//
72 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
73 return First->ReadFullVersionInformation(FullVersion) ||
74 Second->ReadFullVersionInformation(FullVersion);
76 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
77 First->ReadModuleName(ModuleName);
78 Second->ReadModuleName(ModuleName);
80 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
81 First->ReadModuleMapFile(ModuleMapPath);
82 Second->ReadModuleMapFile(ModuleMapPath);
85 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
87 bool AllowCompatibleDifferences) {
88 return First->ReadLanguageOptions(LangOpts, Complain,
89 AllowCompatibleDifferences) ||
90 Second->ReadLanguageOptions(LangOpts, Complain,
91 AllowCompatibleDifferences);
93 bool ChainedASTReaderListener::ReadTargetOptions(
94 const TargetOptions &TargetOpts, bool Complain,
95 bool AllowCompatibleDifferences) {
96 return First->ReadTargetOptions(TargetOpts, Complain,
97 AllowCompatibleDifferences) ||
98 Second->ReadTargetOptions(TargetOpts, Complain,
99 AllowCompatibleDifferences);
101 bool ChainedASTReaderListener::ReadDiagnosticOptions(
102 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
103 return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
104 Second->ReadDiagnosticOptions(DiagOpts, Complain);
107 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
109 return First->ReadFileSystemOptions(FSOpts, Complain) ||
110 Second->ReadFileSystemOptions(FSOpts, Complain);
113 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
114 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
116 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
118 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
121 bool ChainedASTReaderListener::ReadPreprocessorOptions(
122 const PreprocessorOptions &PPOpts, bool Complain,
123 std::string &SuggestedPredefines) {
124 return First->ReadPreprocessorOptions(PPOpts, Complain,
125 SuggestedPredefines) ||
126 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
128 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
130 First->ReadCounter(M, Value);
131 Second->ReadCounter(M, Value);
133 bool ChainedASTReaderListener::needsInputFileVisitation() {
134 return First->needsInputFileVisitation() ||
135 Second->needsInputFileVisitation();
137 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
138 return First->needsSystemInputFileVisitation() ||
139 Second->needsSystemInputFileVisitation();
141 void ChainedASTReaderListener::visitModuleFile(StringRef Filename) {
142 First->visitModuleFile(Filename);
143 Second->visitModuleFile(Filename);
145 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
148 bool Continue = false;
149 if (First->needsInputFileVisitation() &&
150 (!isSystem || First->needsSystemInputFileVisitation()))
151 Continue |= First->visitInputFile(Filename, isSystem, isOverridden);
152 if (Second->needsInputFileVisitation() &&
153 (!isSystem || Second->needsSystemInputFileVisitation()))
154 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden);
158 //===----------------------------------------------------------------------===//
159 // PCH validator implementation
160 //===----------------------------------------------------------------------===//
162 ASTReaderListener::~ASTReaderListener() {}
164 /// \brief Compare the given set of language options against an existing set of
165 /// language options.
167 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
168 /// \param AllowCompatibleDifferences If true, differences between compatible
169 /// language options will be permitted.
171 /// \returns true if the languagae options mis-match, false otherwise.
172 static bool checkLanguageOptions(const LangOptions &LangOpts,
173 const LangOptions &ExistingLangOpts,
174 DiagnosticsEngine *Diags,
175 bool AllowCompatibleDifferences = true) {
176 #define LANGOPT(Name, Bits, Default, Description) \
177 if (ExistingLangOpts.Name != LangOpts.Name) { \
179 Diags->Report(diag::err_pch_langopt_mismatch) \
180 << Description << LangOpts.Name << ExistingLangOpts.Name; \
184 #define VALUE_LANGOPT(Name, Bits, Default, Description) \
185 if (ExistingLangOpts.Name != LangOpts.Name) { \
187 Diags->Report(diag::err_pch_langopt_value_mismatch) \
192 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
193 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
195 Diags->Report(diag::err_pch_langopt_value_mismatch) \
200 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \
201 if (!AllowCompatibleDifferences) \
202 LANGOPT(Name, Bits, Default, Description)
204 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \
205 if (!AllowCompatibleDifferences) \
206 ENUM_LANGOPT(Name, Bits, Default, Description)
208 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
209 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
210 #include "clang/Basic/LangOptions.def"
212 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
214 Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
218 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
220 Diags->Report(diag::err_pch_langopt_value_mismatch)
221 << "target Objective-C runtime";
225 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
226 LangOpts.CommentOpts.BlockCommandNames) {
228 Diags->Report(diag::err_pch_langopt_value_mismatch)
229 << "block command names";
236 /// \brief Compare the given set of target options against an existing set of
239 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
241 /// \returns true if the target options mis-match, false otherwise.
242 static bool checkTargetOptions(const TargetOptions &TargetOpts,
243 const TargetOptions &ExistingTargetOpts,
244 DiagnosticsEngine *Diags,
245 bool AllowCompatibleDifferences = true) {
246 #define CHECK_TARGET_OPT(Field, Name) \
247 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
249 Diags->Report(diag::err_pch_targetopt_mismatch) \
250 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
254 // The triple and ABI must match exactly.
255 CHECK_TARGET_OPT(Triple, "target");
256 CHECK_TARGET_OPT(ABI, "target ABI");
258 // We can tolerate different CPUs in many cases, notably when one CPU
259 // supports a strict superset of another. When allowing compatible
260 // differences skip this check.
261 if (!AllowCompatibleDifferences)
262 CHECK_TARGET_OPT(CPU, "target CPU");
264 #undef CHECK_TARGET_OPT
266 // Compare feature sets.
267 SmallVector<StringRef, 4> ExistingFeatures(
268 ExistingTargetOpts.FeaturesAsWritten.begin(),
269 ExistingTargetOpts.FeaturesAsWritten.end());
270 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
271 TargetOpts.FeaturesAsWritten.end());
272 std::sort(ExistingFeatures.begin(), ExistingFeatures.end());
273 std::sort(ReadFeatures.begin(), ReadFeatures.end());
275 // We compute the set difference in both directions explicitly so that we can
276 // diagnose the differences differently.
277 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
279 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
280 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
281 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
282 ExistingFeatures.begin(), ExistingFeatures.end(),
283 std::back_inserter(UnmatchedReadFeatures));
285 // If we are allowing compatible differences and the read feature set is
286 // a strict subset of the existing feature set, there is nothing to diagnose.
287 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
291 for (StringRef Feature : UnmatchedReadFeatures)
292 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
293 << /* is-existing-feature */ false << Feature;
294 for (StringRef Feature : UnmatchedExistingFeatures)
295 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
296 << /* is-existing-feature */ true << Feature;
299 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
303 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
305 bool AllowCompatibleDifferences) {
306 const LangOptions &ExistingLangOpts = PP.getLangOpts();
307 return checkLanguageOptions(LangOpts, ExistingLangOpts,
308 Complain ? &Reader.Diags : nullptr,
309 AllowCompatibleDifferences);
312 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
314 bool AllowCompatibleDifferences) {
315 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
316 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
317 Complain ? &Reader.Diags : nullptr,
318 AllowCompatibleDifferences);
322 typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >
324 typedef llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> >
328 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
329 DiagnosticsEngine &Diags,
331 typedef DiagnosticsEngine::Level Level;
333 // Check current mappings for new -Werror mappings, and the stored mappings
334 // for cases that were explicitly mapped to *not* be errors that are now
335 // errors because of options like -Werror.
336 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
338 for (DiagnosticsEngine *MappingSource : MappingSources) {
339 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
340 diag::kind DiagID = DiagIDMappingPair.first;
341 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
342 if (CurLevel < DiagnosticsEngine::Error)
343 continue; // not significant
345 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
346 if (StoredLevel < DiagnosticsEngine::Error) {
348 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
349 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
358 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
359 diag::Severity Ext = Diags.getExtensionHandlingBehavior();
360 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
362 return Ext >= diag::Severity::Error;
365 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
366 DiagnosticsEngine &Diags,
367 bool IsSystem, bool Complain) {
370 if (Diags.getSuppressSystemWarnings())
372 // If -Wsystem-headers was not enabled before, be conservative
373 if (StoredDiags.getSuppressSystemWarnings()) {
375 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
380 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
382 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
386 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
387 !StoredDiags.getEnableAllWarnings()) {
389 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
393 if (isExtHandlingFromDiagsError(Diags) &&
394 !isExtHandlingFromDiagsError(StoredDiags)) {
396 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
400 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
403 bool PCHValidator::ReadDiagnosticOptions(
404 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
405 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
406 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
407 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
408 new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
409 // This should never fail, because we would have processed these options
410 // before writing them to an ASTFile.
411 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
413 ModuleManager &ModuleMgr = Reader.getModuleManager();
414 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
416 // If the original import came from a file explicitly generated by the user,
417 // don't check the diagnostic mappings.
418 // FIXME: currently this is approximated by checking whether this is not a
419 // module import of an implicitly-loaded module file.
420 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
421 // the transitive closure of its imports, since unrelated modules cannot be
422 // imported until after this module finishes validation.
423 ModuleFile *TopImport = *ModuleMgr.rbegin();
424 while (!TopImport->ImportedBy.empty())
425 TopImport = TopImport->ImportedBy[0];
426 if (TopImport->Kind != MK_ImplicitModule)
429 StringRef ModuleName = TopImport->ModuleName;
430 assert(!ModuleName.empty() && "diagnostic options read before module name");
432 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
433 assert(M && "missing module");
435 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
436 // contains the union of their flags.
437 return checkDiagnosticMappings(*Diags, ExistingDiags, M->IsSystem, Complain);
440 /// \brief Collect the macro definitions provided by the given preprocessor
443 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
444 MacroDefinitionsMap &Macros,
445 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
446 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
447 StringRef Macro = PPOpts.Macros[I].first;
448 bool IsUndef = PPOpts.Macros[I].second;
450 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
451 StringRef MacroName = MacroPair.first;
452 StringRef MacroBody = MacroPair.second;
454 // For an #undef'd macro, we only care about the name.
456 if (MacroNames && !Macros.count(MacroName))
457 MacroNames->push_back(MacroName);
459 Macros[MacroName] = std::make_pair("", true);
463 // For a #define'd macro, figure out the actual definition.
464 if (MacroName.size() == Macro.size())
467 // Note: GCC drops anything following an end-of-line character.
468 StringRef::size_type End = MacroBody.find_first_of("\n\r");
469 MacroBody = MacroBody.substr(0, End);
472 if (MacroNames && !Macros.count(MacroName))
473 MacroNames->push_back(MacroName);
474 Macros[MacroName] = std::make_pair(MacroBody, false);
478 /// \brief Check the preprocessor options deserialized from the control block
479 /// against the preprocessor options in an existing preprocessor.
481 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
482 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
483 const PreprocessorOptions &ExistingPPOpts,
484 DiagnosticsEngine *Diags,
485 FileManager &FileMgr,
486 std::string &SuggestedPredefines,
487 const LangOptions &LangOpts) {
488 // Check macro definitions.
489 MacroDefinitionsMap ASTFileMacros;
490 collectMacroDefinitions(PPOpts, ASTFileMacros);
491 MacroDefinitionsMap ExistingMacros;
492 SmallVector<StringRef, 4> ExistingMacroNames;
493 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
495 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
496 // Dig out the macro definition in the existing preprocessor options.
497 StringRef MacroName = ExistingMacroNames[I];
498 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
500 // Check whether we know anything about this macro name or not.
501 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known
502 = ASTFileMacros.find(MacroName);
503 if (Known == ASTFileMacros.end()) {
504 // FIXME: Check whether this identifier was referenced anywhere in the
505 // AST file. If so, we should reject the AST file. Unfortunately, this
506 // information isn't in the control block. What shall we do about it?
508 if (Existing.second) {
509 SuggestedPredefines += "#undef ";
510 SuggestedPredefines += MacroName.str();
511 SuggestedPredefines += '\n';
513 SuggestedPredefines += "#define ";
514 SuggestedPredefines += MacroName.str();
515 SuggestedPredefines += ' ';
516 SuggestedPredefines += Existing.first.str();
517 SuggestedPredefines += '\n';
522 // If the macro was defined in one but undef'd in the other, we have a
524 if (Existing.second != Known->second.second) {
526 Diags->Report(diag::err_pch_macro_def_undef)
527 << MacroName << Known->second.second;
532 // If the macro was #undef'd in both, or if the macro bodies are identical,
534 if (Existing.second || Existing.first == Known->second.first)
537 // The macro bodies differ; complain.
539 Diags->Report(diag::err_pch_macro_def_conflict)
540 << MacroName << Known->second.first << Existing.first;
545 // Check whether we're using predefines.
546 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) {
548 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
553 // Detailed record is important since it is used for the module cache hash.
554 if (LangOpts.Modules &&
555 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord) {
557 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
562 // Compute the #include and #include_macros lines we need.
563 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
564 StringRef File = ExistingPPOpts.Includes[I];
565 if (File == ExistingPPOpts.ImplicitPCHInclude)
568 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
569 != PPOpts.Includes.end())
572 SuggestedPredefines += "#include \"";
573 SuggestedPredefines += File;
574 SuggestedPredefines += "\"\n";
577 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
578 StringRef File = ExistingPPOpts.MacroIncludes[I];
579 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
581 != PPOpts.MacroIncludes.end())
584 SuggestedPredefines += "#__include_macros \"";
585 SuggestedPredefines += File;
586 SuggestedPredefines += "\"\n##\n";
592 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
594 std::string &SuggestedPredefines) {
595 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
597 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
598 Complain? &Reader.Diags : nullptr,
604 /// Check the header search options deserialized from the control block
605 /// against the header search options in an existing preprocessor.
607 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
608 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
609 StringRef SpecificModuleCachePath,
610 StringRef ExistingModuleCachePath,
611 DiagnosticsEngine *Diags,
612 const LangOptions &LangOpts) {
613 if (LangOpts.Modules) {
614 if (SpecificModuleCachePath != ExistingModuleCachePath) {
616 Diags->Report(diag::err_pch_modulecache_mismatch)
617 << SpecificModuleCachePath << ExistingModuleCachePath;
625 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
626 StringRef SpecificModuleCachePath,
628 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
629 PP.getHeaderSearchInfo().getModuleCachePath(),
630 Complain ? &Reader.Diags : nullptr,
634 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
635 PP.setCounterValue(Value);
638 //===----------------------------------------------------------------------===//
639 // AST reader implementation
640 //===----------------------------------------------------------------------===//
642 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
643 bool TakeOwnership) {
644 DeserializationListener = Listener;
645 OwnsDeserializationListener = TakeOwnership;
650 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
651 return serialization::ComputeHash(Sel);
655 std::pair<unsigned, unsigned>
656 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
657 using namespace llvm::support;
658 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
659 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
660 return std::make_pair(KeyLen, DataLen);
663 ASTSelectorLookupTrait::internal_key_type
664 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
665 using namespace llvm::support;
666 SelectorTable &SelTable = Reader.getContext().Selectors;
667 unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
668 IdentifierInfo *FirstII = Reader.getLocalIdentifier(
669 F, endian::readNext<uint32_t, little, unaligned>(d));
671 return SelTable.getNullarySelector(FirstII);
673 return SelTable.getUnarySelector(FirstII);
675 SmallVector<IdentifierInfo *, 16> Args;
676 Args.push_back(FirstII);
677 for (unsigned I = 1; I != N; ++I)
678 Args.push_back(Reader.getLocalIdentifier(
679 F, endian::readNext<uint32_t, little, unaligned>(d)));
681 return SelTable.getSelector(N, Args.data());
684 ASTSelectorLookupTrait::data_type
685 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
687 using namespace llvm::support;
691 Result.ID = Reader.getGlobalSelectorID(
692 F, endian::readNext<uint32_t, little, unaligned>(d));
693 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
694 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
695 Result.InstanceBits = FullInstanceBits & 0x3;
696 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
697 Result.FactoryBits = FullFactoryBits & 0x3;
698 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
699 unsigned NumInstanceMethods = FullInstanceBits >> 3;
700 unsigned NumFactoryMethods = FullFactoryBits >> 3;
702 // Load instance methods
703 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
704 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
705 F, endian::readNext<uint32_t, little, unaligned>(d)))
706 Result.Instance.push_back(Method);
709 // Load factory methods
710 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
711 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
712 F, endian::readNext<uint32_t, little, unaligned>(d)))
713 Result.Factory.push_back(Method);
719 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
720 return llvm::HashString(a);
723 std::pair<unsigned, unsigned>
724 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
725 using namespace llvm::support;
726 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
727 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
728 return std::make_pair(KeyLen, DataLen);
731 ASTIdentifierLookupTraitBase::internal_key_type
732 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
733 assert(n >= 2 && d[n-1] == '\0');
734 return StringRef((const char*) d, n-1);
737 /// \brief Whether the given identifier is "interesting".
738 static bool isInterestingIdentifier(IdentifierInfo &II) {
739 return II.isPoisoned() ||
740 II.isExtensionToken() ||
741 II.getObjCOrBuiltinID() ||
742 II.hasRevertedTokenIDToIdentifier() ||
743 II.hadMacroDefinition() ||
744 II.getFETokenInfo<void>();
747 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
748 const unsigned char* d,
750 using namespace llvm::support;
751 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
752 bool IsInteresting = RawID & 0x01;
754 // Wipe out the "is interesting" bit.
757 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
758 if (!IsInteresting) {
759 // For uninteresting identifiers, just build the IdentifierInfo
760 // and associate it with the persistent ID.
761 IdentifierInfo *II = KnownII;
763 II = &Reader.getIdentifierTable().getOwn(k);
766 Reader.SetIdentifierInfo(ID, II);
767 if (!II->isFromAST()) {
768 bool WasInteresting = isInterestingIdentifier(*II);
771 II->setChangedSinceDeserialization();
773 Reader.markIdentifierUpToDate(II);
777 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
778 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
779 bool CPlusPlusOperatorKeyword = Bits & 0x01;
781 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
783 bool Poisoned = Bits & 0x01;
785 bool ExtensionToken = Bits & 0x01;
787 bool hadMacroDefinition = Bits & 0x01;
790 assert(Bits == 0 && "Extra bits in the identifier?");
793 // Build the IdentifierInfo itself and link the identifier ID with
794 // the new IdentifierInfo.
795 IdentifierInfo *II = KnownII;
797 II = &Reader.getIdentifierTable().getOwn(StringRef(k));
800 Reader.markIdentifierUpToDate(II);
801 if (!II->isFromAST()) {
802 bool WasInteresting = isInterestingIdentifier(*II);
805 II->setChangedSinceDeserialization();
808 // Set or check the various bits in the IdentifierInfo structure.
809 // Token IDs are read-only.
810 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
811 II->RevertTokenIDToIdentifier();
812 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
813 assert(II->isExtensionToken() == ExtensionToken &&
814 "Incorrect extension token flag");
815 (void)ExtensionToken;
817 II->setIsPoisoned(true);
818 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
819 "Incorrect C++ operator keyword flag");
820 (void)CPlusPlusOperatorKeyword;
822 // If this identifier is a macro, deserialize the macro
824 if (hadMacroDefinition) {
825 uint32_t MacroDirectivesOffset =
826 endian::readNext<uint32_t, little, unaligned>(d);
829 Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
832 Reader.SetIdentifierInfo(ID, II);
834 // Read all of the declarations visible at global scope with this
837 SmallVector<uint32_t, 4> DeclIDs;
838 for (; DataLen > 0; DataLen -= 4)
839 DeclIDs.push_back(Reader.getGlobalDeclID(
840 F, endian::readNext<uint32_t, little, unaligned>(d)));
841 Reader.SetGloballyVisibleDecls(II, DeclIDs);
848 ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) {
849 llvm::FoldingSetNodeID ID;
850 ID.AddInteger(Key.Kind);
853 case DeclarationName::Identifier:
854 case DeclarationName::CXXLiteralOperatorName:
855 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
857 case DeclarationName::ObjCZeroArgSelector:
858 case DeclarationName::ObjCOneArgSelector:
859 case DeclarationName::ObjCMultiArgSelector:
860 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
862 case DeclarationName::CXXOperatorName:
863 ID.AddInteger((OverloadedOperatorKind)Key.Data);
865 case DeclarationName::CXXConstructorName:
866 case DeclarationName::CXXDestructorName:
867 case DeclarationName::CXXConversionFunctionName:
868 case DeclarationName::CXXUsingDirective:
872 return ID.ComputeHash();
875 ASTDeclContextNameLookupTrait::internal_key_type
876 ASTDeclContextNameLookupTrait::GetInternalKey(
877 const external_key_type& Name) {
879 Key.Kind = Name.getNameKind();
880 switch (Name.getNameKind()) {
881 case DeclarationName::Identifier:
882 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
884 case DeclarationName::ObjCZeroArgSelector:
885 case DeclarationName::ObjCOneArgSelector:
886 case DeclarationName::ObjCMultiArgSelector:
887 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
889 case DeclarationName::CXXOperatorName:
890 Key.Data = Name.getCXXOverloadedOperator();
892 case DeclarationName::CXXLiteralOperatorName:
893 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
895 case DeclarationName::CXXConstructorName:
896 case DeclarationName::CXXDestructorName:
897 case DeclarationName::CXXConversionFunctionName:
898 case DeclarationName::CXXUsingDirective:
906 std::pair<unsigned, unsigned>
907 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
908 using namespace llvm::support;
909 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
910 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
911 return std::make_pair(KeyLen, DataLen);
914 ASTDeclContextNameLookupTrait::internal_key_type
915 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
916 using namespace llvm::support;
919 Key.Kind = (DeclarationName::NameKind)*d++;
921 case DeclarationName::Identifier:
922 Key.Data = (uint64_t)Reader.getLocalIdentifier(
923 F, endian::readNext<uint32_t, little, unaligned>(d));
925 case DeclarationName::ObjCZeroArgSelector:
926 case DeclarationName::ObjCOneArgSelector:
927 case DeclarationName::ObjCMultiArgSelector:
929 (uint64_t)Reader.getLocalSelector(
930 F, endian::readNext<uint32_t, little, unaligned>(
931 d)).getAsOpaquePtr();
933 case DeclarationName::CXXOperatorName:
934 Key.Data = *d++; // OverloadedOperatorKind
936 case DeclarationName::CXXLiteralOperatorName:
937 Key.Data = (uint64_t)Reader.getLocalIdentifier(
938 F, endian::readNext<uint32_t, little, unaligned>(d));
940 case DeclarationName::CXXConstructorName:
941 case DeclarationName::CXXDestructorName:
942 case DeclarationName::CXXConversionFunctionName:
943 case DeclarationName::CXXUsingDirective:
951 ASTDeclContextNameLookupTrait::data_type
952 ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
953 const unsigned char* d,
955 using namespace llvm::support;
956 unsigned NumDecls = endian::readNext<uint16_t, little, unaligned>(d);
957 LE32DeclID *Start = reinterpret_cast<LE32DeclID *>(
958 const_cast<unsigned char *>(d));
959 return std::make_pair(Start, Start + NumDecls);
962 bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
963 BitstreamCursor &Cursor,
964 const std::pair<uint64_t, uint64_t> &Offsets,
965 DeclContextInfo &Info) {
966 SavedStreamPosition SavedPosition(Cursor);
967 // First the lexical decls.
968 if (Offsets.first != 0) {
969 Cursor.JumpToBit(Offsets.first);
973 unsigned Code = Cursor.ReadCode();
974 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
975 if (RecCode != DECL_CONTEXT_LEXICAL) {
976 Error("Expected lexical block");
980 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob.data());
981 Info.NumLexicalDecls = Blob.size() / sizeof(KindDeclIDPair);
984 // Now the lookup table.
985 if (Offsets.second != 0) {
986 Cursor.JumpToBit(Offsets.second);
990 unsigned Code = Cursor.ReadCode();
991 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
992 if (RecCode != DECL_CONTEXT_VISIBLE) {
993 Error("Expected visible lookup table block");
996 Info.NameLookupTableData = ASTDeclContextNameLookupTable::Create(
997 (const unsigned char *)Blob.data() + Record[0],
998 (const unsigned char *)Blob.data() + sizeof(uint32_t),
999 (const unsigned char *)Blob.data(),
1000 ASTDeclContextNameLookupTrait(*this, M));
1006 void ASTReader::Error(StringRef Msg) {
1007 Error(diag::err_fe_pch_malformed, Msg);
1008 if (Context.getLangOpts().Modules && !Diags.isDiagnosticInFlight()) {
1009 Diag(diag::note_module_cache_path)
1010 << PP.getHeaderSearchInfo().getModuleCachePath();
1014 void ASTReader::Error(unsigned DiagID,
1015 StringRef Arg1, StringRef Arg2) {
1016 if (Diags.isDiagnosticInFlight())
1017 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
1019 Diag(DiagID) << Arg1 << Arg2;
1022 //===----------------------------------------------------------------------===//
1023 // Source Manager Deserialization
1024 //===----------------------------------------------------------------------===//
1026 /// \brief Read the line table in the source manager block.
1027 /// \returns true if there was an error.
1028 bool ASTReader::ParseLineTable(ModuleFile &F,
1029 const RecordData &Record) {
1031 LineTableInfo &LineTable = SourceMgr.getLineTable();
1033 // Parse the file names
1034 std::map<int, int> FileIDs;
1035 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
1036 // Extract the file name
1037 auto Filename = ReadPath(F, Record, Idx);
1038 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1041 // Parse the line entries
1042 std::vector<LineEntry> Entries;
1043 while (Idx < Record.size()) {
1044 int FID = Record[Idx++];
1045 assert(FID >= 0 && "Serialized line entries for non-local file.");
1046 // Remap FileID from 1-based old view.
1047 FID += F.SLocEntryBaseID - 1;
1049 // Extract the line entries
1050 unsigned NumEntries = Record[Idx++];
1051 assert(NumEntries && "Numentries is 00000");
1053 Entries.reserve(NumEntries);
1054 for (unsigned I = 0; I != NumEntries; ++I) {
1055 unsigned FileOffset = Record[Idx++];
1056 unsigned LineNo = Record[Idx++];
1057 int FilenameID = FileIDs[Record[Idx++]];
1058 SrcMgr::CharacteristicKind FileKind
1059 = (SrcMgr::CharacteristicKind)Record[Idx++];
1060 unsigned IncludeOffset = Record[Idx++];
1061 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1062 FileKind, IncludeOffset));
1064 LineTable.AddEntry(FileID::get(FID), Entries);
1070 /// \brief Read a source manager block
1071 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1072 using namespace SrcMgr;
1074 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1076 // Set the source-location entry cursor to the current position in
1077 // the stream. This cursor will be used to read the contents of the
1078 // source manager block initially, and then lazily read
1079 // source-location entries as needed.
1080 SLocEntryCursor = F.Stream;
1082 // The stream itself is going to skip over the source manager block.
1083 if (F.Stream.SkipBlock()) {
1084 Error("malformed block record in AST file");
1088 // Enter the source manager block.
1089 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1090 Error("malformed source manager block record in AST file");
1096 llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
1099 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1100 case llvm::BitstreamEntry::Error:
1101 Error("malformed block record in AST file");
1103 case llvm::BitstreamEntry::EndBlock:
1105 case llvm::BitstreamEntry::Record:
1106 // The interesting case.
1113 switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
1114 default: // Default behavior: ignore.
1117 case SM_SLOC_FILE_ENTRY:
1118 case SM_SLOC_BUFFER_ENTRY:
1119 case SM_SLOC_EXPANSION_ENTRY:
1120 // Once we hit one of the source location entries, we're done.
1126 /// \brief If a header file is not found at the path that we expect it to be
1127 /// and the PCH file was moved from its original location, try to resolve the
1128 /// file by assuming that header+PCH were moved together and the header is in
1129 /// the same place relative to the PCH.
1131 resolveFileRelativeToOriginalDir(const std::string &Filename,
1132 const std::string &OriginalDir,
1133 const std::string &CurrDir) {
1134 assert(OriginalDir != CurrDir &&
1135 "No point trying to resolve the file if the PCH dir didn't change");
1136 using namespace llvm::sys;
1137 SmallString<128> filePath(Filename);
1138 fs::make_absolute(filePath);
1139 assert(path::is_absolute(OriginalDir));
1140 SmallString<128> currPCHPath(CurrDir);
1142 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1143 fileDirE = path::end(path::parent_path(filePath));
1144 path::const_iterator origDirI = path::begin(OriginalDir),
1145 origDirE = path::end(OriginalDir);
1146 // Skip the common path components from filePath and OriginalDir.
1147 while (fileDirI != fileDirE && origDirI != origDirE &&
1148 *fileDirI == *origDirI) {
1152 for (; origDirI != origDirE; ++origDirI)
1153 path::append(currPCHPath, "..");
1154 path::append(currPCHPath, fileDirI, fileDirE);
1155 path::append(currPCHPath, path::filename(Filename));
1156 return currPCHPath.str();
1159 bool ASTReader::ReadSLocEntry(int ID) {
1163 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1164 Error("source location entry ID out-of-range for AST file");
1168 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1169 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
1170 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1171 unsigned BaseOffset = F->SLocEntryBaseOffset;
1173 ++NumSLocEntriesRead;
1174 llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
1175 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1176 Error("incorrectly-formatted source location entry in AST file");
1182 switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
1184 Error("incorrectly-formatted source location entry in AST file");
1187 case SM_SLOC_FILE_ENTRY: {
1188 // We will detect whether a file changed and return 'Failure' for it, but
1189 // we will also try to fail gracefully by setting up the SLocEntry.
1190 unsigned InputID = Record[4];
1191 InputFile IF = getInputFile(*F, InputID);
1192 const FileEntry *File = IF.getFile();
1193 bool OverriddenBuffer = IF.isOverridden();
1195 // Note that we only check if a File was returned. If it was out-of-date
1196 // we have complained but we will continue creating a FileID to recover
1201 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1202 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1203 // This is the module's main file.
1204 IncludeLoc = getImportLocation(F);
1206 SrcMgr::CharacteristicKind
1207 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1208 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1209 ID, BaseOffset + Record[0]);
1210 SrcMgr::FileInfo &FileInfo =
1211 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1212 FileInfo.NumCreatedFIDs = Record[5];
1214 FileInfo.setHasLineDirectives();
1216 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1217 unsigned NumFileDecls = Record[7];
1219 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1220 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1224 const SrcMgr::ContentCache *ContentCache
1225 = SourceMgr.getOrCreateContentCache(File,
1226 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
1227 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1228 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
1229 unsigned Code = SLocEntryCursor.ReadCode();
1231 unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
1233 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1234 Error("AST record has invalid code");
1238 std::unique_ptr<llvm::MemoryBuffer> Buffer
1239 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), File->getName());
1240 SourceMgr.overrideFileContents(File, std::move(Buffer));
1246 case SM_SLOC_BUFFER_ENTRY: {
1247 const char *Name = Blob.data();
1248 unsigned Offset = Record[0];
1249 SrcMgr::CharacteristicKind
1250 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1251 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1252 if (IncludeLoc.isInvalid() &&
1253 (F->Kind == MK_ImplicitModule || F->Kind == MK_ExplicitModule)) {
1254 IncludeLoc = getImportLocation(F);
1256 unsigned Code = SLocEntryCursor.ReadCode();
1259 = SLocEntryCursor.readRecord(Code, Record, &Blob);
1261 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1262 Error("AST record has invalid code");
1266 std::unique_ptr<llvm::MemoryBuffer> Buffer =
1267 llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name);
1268 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1269 BaseOffset + Offset, IncludeLoc);
1273 case SM_SLOC_EXPANSION_ENTRY: {
1274 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1275 SourceMgr.createExpansionLoc(SpellingLoc,
1276 ReadSourceLocation(*F, Record[2]),
1277 ReadSourceLocation(*F, Record[3]),
1280 BaseOffset + Record[0]);
1288 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1290 return std::make_pair(SourceLocation(), "");
1292 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1293 Error("source location entry ID out-of-range for AST file");
1294 return std::make_pair(SourceLocation(), "");
1297 // Find which module file this entry lands in.
1298 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1299 if (M->Kind != MK_ImplicitModule && M->Kind != MK_ExplicitModule)
1300 return std::make_pair(SourceLocation(), "");
1302 // FIXME: Can we map this down to a particular submodule? That would be
1304 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1307 /// \brief Find the location where the module F is imported.
1308 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1309 if (F->ImportLoc.isValid())
1310 return F->ImportLoc;
1312 // Otherwise we have a PCH. It's considered to be "imported" at the first
1313 // location of its includer.
1314 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1315 // Main file is the importer.
1316 assert(!SourceMgr.getMainFileID().isInvalid() && "missing main file");
1317 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1319 return F->ImportedBy[0]->FirstLoc;
1322 /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1323 /// specified cursor. Read the abbreviations that are at the top of the block
1324 /// and then leave the cursor pointing into the block.
1325 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
1326 if (Cursor.EnterSubBlock(BlockID)) {
1327 Error("malformed block record in AST file");
1332 uint64_t Offset = Cursor.GetCurrentBitNo();
1333 unsigned Code = Cursor.ReadCode();
1335 // We expect all abbrevs to be at the start of the block.
1336 if (Code != llvm::bitc::DEFINE_ABBREV) {
1337 Cursor.JumpToBit(Offset);
1340 Cursor.ReadAbbrevRecord();
1344 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1348 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1349 Tok.setLength(Record[Idx++]);
1350 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1351 Tok.setIdentifierInfo(II);
1352 Tok.setKind((tok::TokenKind)Record[Idx++]);
1353 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1357 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1358 BitstreamCursor &Stream = F.MacroCursor;
1360 // Keep track of where we are in the stream, then jump back there
1361 // after reading this macro.
1362 SavedStreamPosition SavedPosition(Stream);
1364 Stream.JumpToBit(Offset);
1366 SmallVector<IdentifierInfo*, 16> MacroArgs;
1367 MacroInfo *Macro = nullptr;
1370 // Advance to the next record, but if we get to the end of the block, don't
1371 // pop it (removing all the abbreviations from the cursor) since we want to
1372 // be able to reseek within the block and read entries.
1373 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1374 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1376 switch (Entry.Kind) {
1377 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1378 case llvm::BitstreamEntry::Error:
1379 Error("malformed block record in AST file");
1381 case llvm::BitstreamEntry::EndBlock:
1383 case llvm::BitstreamEntry::Record:
1384 // The interesting case.
1390 PreprocessorRecordTypes RecType =
1391 (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
1393 case PP_MODULE_MACRO:
1394 case PP_MACRO_DIRECTIVE_HISTORY:
1397 case PP_MACRO_OBJECT_LIKE:
1398 case PP_MACRO_FUNCTION_LIKE: {
1399 // If we already have a macro, that means that we've hit the end
1400 // of the definition of the macro we were looking for. We're
1405 unsigned NextIndex = 1; // Skip identifier ID.
1406 SubmoduleID SubModID = getGlobalSubmoduleID(F, Record[NextIndex++]);
1407 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1408 MacroInfo *MI = PP.AllocateDeserializedMacroInfo(Loc, SubModID);
1409 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1410 MI->setIsUsed(Record[NextIndex++]);
1411 MI->setUsedForHeaderGuard(Record[NextIndex++]);
1413 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1414 // Decode function-like macro info.
1415 bool isC99VarArgs = Record[NextIndex++];
1416 bool isGNUVarArgs = Record[NextIndex++];
1417 bool hasCommaPasting = Record[NextIndex++];
1419 unsigned NumArgs = Record[NextIndex++];
1420 for (unsigned i = 0; i != NumArgs; ++i)
1421 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1423 // Install function-like macro info.
1424 MI->setIsFunctionLike();
1425 if (isC99VarArgs) MI->setIsC99Varargs();
1426 if (isGNUVarArgs) MI->setIsGNUVarargs();
1427 if (hasCommaPasting) MI->setHasCommaPasting();
1428 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
1429 PP.getPreprocessorAllocator());
1432 // Remember that we saw this macro last so that we add the tokens that
1433 // form its body to it.
1436 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1437 Record[NextIndex]) {
1438 // We have a macro definition. Register the association
1439 PreprocessedEntityID
1440 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1441 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1442 PreprocessingRecord::PPEntityID PPID =
1443 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1444 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1445 PPRec.getPreprocessedEntity(PPID));
1447 PPRec.RegisterMacroDefinition(Macro, PPDef);
1455 // If we see a TOKEN before a PP_MACRO_*, then the file is
1456 // erroneous, just pretend we didn't see this.
1460 Token Tok = ReadToken(F, Record, Idx);
1461 Macro->AddTokenToBody(Tok);
1468 PreprocessedEntityID
1469 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
1470 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1471 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1472 assert(I != M.PreprocessedEntityRemap.end()
1473 && "Invalid index into preprocessed entity index remap");
1475 return LocalID + I->second;
1478 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1479 return llvm::hash_combine(ikey.Size, ikey.ModTime);
1482 HeaderFileInfoTrait::internal_key_type
1483 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1484 internal_key_type ikey = { FE->getSize(), FE->getModificationTime(),
1485 FE->getName(), /*Imported*/false };
1489 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1490 if (a.Size != b.Size || a.ModTime != b.ModTime)
1493 if (llvm::sys::path::is_absolute(a.Filename) &&
1494 strcmp(a.Filename, b.Filename) == 0)
1497 // Determine whether the actual files are equivalent.
1498 FileManager &FileMgr = Reader.getFileManager();
1499 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1501 return FileMgr.getFile(Key.Filename);
1503 std::string Resolved = Key.Filename;
1504 Reader.ResolveImportedPath(M, Resolved);
1505 return FileMgr.getFile(Resolved);
1508 const FileEntry *FEA = GetFile(a);
1509 const FileEntry *FEB = GetFile(b);
1510 return FEA && FEA == FEB;
1513 std::pair<unsigned, unsigned>
1514 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1515 using namespace llvm::support;
1516 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1517 unsigned DataLen = (unsigned) *d++;
1518 return std::make_pair(KeyLen, DataLen);
1521 HeaderFileInfoTrait::internal_key_type
1522 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1523 using namespace llvm::support;
1524 internal_key_type ikey;
1525 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1526 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1527 ikey.Filename = (const char *)d;
1528 ikey.Imported = true;
1532 HeaderFileInfoTrait::data_type
1533 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1535 const unsigned char *End = d + DataLen;
1536 using namespace llvm::support;
1538 unsigned Flags = *d++;
1539 HFI.HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>
1540 ((Flags >> 6) & 0x03);
1541 HFI.isImport = (Flags >> 5) & 0x01;
1542 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1543 HFI.DirInfo = (Flags >> 2) & 0x03;
1544 HFI.Resolved = (Flags >> 1) & 0x01;
1545 HFI.IndexHeaderMapHeader = Flags & 0x01;
1546 HFI.NumIncludes = endian::readNext<uint16_t, little, unaligned>(d);
1547 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1548 M, endian::readNext<uint32_t, little, unaligned>(d));
1549 if (unsigned FrameworkOffset =
1550 endian::readNext<uint32_t, little, unaligned>(d)) {
1551 // The framework offset is 1 greater than the actual offset,
1552 // since 0 is used as an indicator for "no framework name".
1553 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1554 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1558 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1560 // This header is part of a module. Associate it with the module to enable
1561 // implicit module import.
1562 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1563 Module *Mod = Reader.getSubmodule(GlobalSMID);
1564 HFI.isModuleHeader = true;
1565 FileManager &FileMgr = Reader.getFileManager();
1567 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1568 // FIXME: This information should be propagated through the
1569 // SUBMODULE_HEADER etc records rather than from here.
1570 // FIXME: We don't ever mark excluded headers.
1571 std::string Filename = key.Filename;
1573 Reader.ResolveImportedPath(M, Filename);
1574 Module::Header H = { key.Filename, FileMgr.getFile(Filename) };
1575 ModMap.addHeader(Mod, H, HFI.getHeaderRole());
1579 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1582 // This HeaderFileInfo was externally loaded.
1583 HFI.External = true;
1587 void ASTReader::addPendingMacro(IdentifierInfo *II,
1589 uint64_t MacroDirectivesOffset) {
1590 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1591 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1594 void ASTReader::ReadDefinedMacros() {
1595 // Note that we are loading defined macros.
1596 Deserializing Macros(this);
1598 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1599 E = ModuleMgr.rend(); I != E; ++I) {
1600 BitstreamCursor &MacroCursor = (*I)->MacroCursor;
1602 // If there was no preprocessor block, skip this file.
1603 if (!MacroCursor.getBitStreamReader())
1606 BitstreamCursor Cursor = MacroCursor;
1607 Cursor.JumpToBit((*I)->MacroStartOffset);
1611 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1614 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1615 case llvm::BitstreamEntry::Error:
1616 Error("malformed block record in AST file");
1618 case llvm::BitstreamEntry::EndBlock:
1621 case llvm::BitstreamEntry::Record:
1623 switch (Cursor.readRecord(E.ID, Record)) {
1624 default: // Default behavior: ignore.
1627 case PP_MACRO_OBJECT_LIKE:
1628 case PP_MACRO_FUNCTION_LIKE:
1629 getLocalIdentifier(**I, Record[0]);
1644 /// \brief Visitor class used to look up identifirs in an AST file.
1645 class IdentifierLookupVisitor {
1648 unsigned PriorGeneration;
1649 unsigned &NumIdentifierLookups;
1650 unsigned &NumIdentifierLookupHits;
1651 IdentifierInfo *Found;
1654 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1655 unsigned &NumIdentifierLookups,
1656 unsigned &NumIdentifierLookupHits)
1657 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
1658 PriorGeneration(PriorGeneration),
1659 NumIdentifierLookups(NumIdentifierLookups),
1660 NumIdentifierLookupHits(NumIdentifierLookupHits),
1665 static bool visit(ModuleFile &M, void *UserData) {
1666 IdentifierLookupVisitor *This
1667 = static_cast<IdentifierLookupVisitor *>(UserData);
1669 // If we've already searched this module file, skip it now.
1670 if (M.Generation <= This->PriorGeneration)
1673 ASTIdentifierLookupTable *IdTable
1674 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1678 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1680 ++This->NumIdentifierLookups;
1681 ASTIdentifierLookupTable::iterator Pos =
1682 IdTable->find_hashed(This->Name, This->NameHash, &Trait);
1683 if (Pos == IdTable->end())
1686 // Dereferencing the iterator has the effect of building the
1687 // IdentifierInfo node and populating it with the various
1688 // declarations it needs.
1689 ++This->NumIdentifierLookupHits;
1694 // \brief Retrieve the identifier info found within the module
1696 IdentifierInfo *getIdentifierInfo() const { return Found; }
1700 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1701 // Note that we are loading an identifier.
1702 Deserializing AnIdentifier(this);
1704 unsigned PriorGeneration = 0;
1705 if (getContext().getLangOpts().Modules)
1706 PriorGeneration = IdentifierGeneration[&II];
1708 // If there is a global index, look there first to determine which modules
1709 // provably do not have any results for this identifier.
1710 GlobalModuleIndex::HitSet Hits;
1711 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
1712 if (!loadGlobalIndex()) {
1713 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1718 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
1719 NumIdentifierLookups,
1720 NumIdentifierLookupHits);
1721 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
1722 markIdentifierUpToDate(&II);
1725 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1729 II->setOutOfDate(false);
1731 // Update the generation for this identifier.
1732 if (getContext().getLangOpts().Modules)
1733 IdentifierGeneration[II] = getGeneration();
1736 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1737 const PendingMacroInfo &PMInfo) {
1738 ModuleFile &M = *PMInfo.M;
1740 BitstreamCursor &Cursor = M.MacroCursor;
1741 SavedStreamPosition SavedPosition(Cursor);
1742 Cursor.JumpToBit(PMInfo.MacroDirectivesOffset);
1744 struct ModuleMacroRecord {
1745 SubmoduleID SubModID;
1747 SmallVector<SubmoduleID, 8> Overrides;
1749 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
1751 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
1752 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
1756 llvm::BitstreamEntry Entry =
1757 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1758 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1759 Error("malformed block record in AST file");
1764 switch ((PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
1765 case PP_MACRO_DIRECTIVE_HISTORY:
1768 case PP_MODULE_MACRO: {
1769 ModuleMacros.push_back(ModuleMacroRecord());
1770 auto &Info = ModuleMacros.back();
1771 Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
1772 Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
1773 for (int I = 2, N = Record.size(); I != N; ++I)
1774 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
1779 Error("malformed block record in AST file");
1783 // We found the macro directive history; that's the last record
1788 // Module macros are listed in reverse dependency order.
1790 std::reverse(ModuleMacros.begin(), ModuleMacros.end());
1791 llvm::SmallVector<ModuleMacro*, 8> Overrides;
1792 for (auto &MMR : ModuleMacros) {
1794 for (unsigned ModID : MMR.Overrides) {
1795 Module *Mod = getSubmodule(ModID);
1796 auto *Macro = PP.getModuleMacro(Mod, II);
1797 assert(Macro && "missing definition for overridden macro");
1798 Overrides.push_back(Macro);
1801 bool Inserted = false;
1802 Module *Owner = getSubmodule(MMR.SubModID);
1803 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
1807 // Don't read the directive history for a module; we don't have anywhere
1809 if (M.Kind == MK_ImplicitModule || M.Kind == MK_ExplicitModule)
1812 // Deserialize the macro directives history in reverse source-order.
1813 MacroDirective *Latest = nullptr, *Earliest = nullptr;
1814 unsigned Idx = 0, N = Record.size();
1816 MacroDirective *MD = nullptr;
1817 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
1818 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
1820 case MacroDirective::MD_Define: {
1821 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
1822 MD = PP.AllocateDefMacroDirective(MI, Loc);
1825 case MacroDirective::MD_Undefine: {
1826 MD = PP.AllocateUndefMacroDirective(Loc);
1829 case MacroDirective::MD_Visibility:
1830 bool isPublic = Record[Idx++];
1831 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
1838 Earliest->setPrevious(MD);
1843 PP.setLoadedMacroDirective(II, Latest);
1846 ASTReader::InputFileInfo
1847 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
1848 // Go find this input file.
1849 BitstreamCursor &Cursor = F.InputFilesCursor;
1850 SavedStreamPosition SavedPosition(Cursor);
1851 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1853 unsigned Code = Cursor.ReadCode();
1857 unsigned Result = Cursor.readRecord(Code, Record, &Blob);
1858 assert(static_cast<InputFileRecordTypes>(Result) == INPUT_FILE &&
1859 "invalid record type for input file");
1862 std::string Filename;
1867 assert(Record[0] == ID && "Bogus stored ID or offset");
1868 StoredSize = static_cast<off_t>(Record[1]);
1869 StoredTime = static_cast<time_t>(Record[2]);
1870 Overridden = static_cast<bool>(Record[3]);
1872 ResolveImportedPath(F, Filename);
1874 InputFileInfo R = { std::move(Filename), StoredSize, StoredTime, Overridden };
1878 std::string ASTReader::getInputFileName(ModuleFile &F, unsigned int ID) {
1879 return readInputFileInfo(F, ID).Filename;
1882 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
1883 // If this ID is bogus, just return an empty input file.
1884 if (ID == 0 || ID > F.InputFilesLoaded.size())
1887 // If we've already loaded this input file, return it.
1888 if (F.InputFilesLoaded[ID-1].getFile())
1889 return F.InputFilesLoaded[ID-1];
1891 if (F.InputFilesLoaded[ID-1].isNotFound())
1894 // Go find this input file.
1895 BitstreamCursor &Cursor = F.InputFilesCursor;
1896 SavedStreamPosition SavedPosition(Cursor);
1897 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1899 InputFileInfo FI = readInputFileInfo(F, ID);
1900 off_t StoredSize = FI.StoredSize;
1901 time_t StoredTime = FI.StoredTime;
1902 bool Overridden = FI.Overridden;
1903 StringRef Filename = FI.Filename;
1905 const FileEntry *File
1906 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
1907 : FileMgr.getFile(Filename, /*OpenFile=*/false);
1909 // If we didn't find the file, resolve it relative to the
1910 // original directory from which this AST file was created.
1911 if (File == nullptr && !F.OriginalDir.empty() && !CurrentDir.empty() &&
1912 F.OriginalDir != CurrentDir) {
1913 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1916 if (!Resolved.empty())
1917 File = FileMgr.getFile(Resolved);
1920 // For an overridden file, create a virtual file with the stored
1922 if (Overridden && File == nullptr) {
1923 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
1926 if (File == nullptr) {
1928 std::string ErrorStr = "could not find file '";
1929 ErrorStr += Filename;
1930 ErrorStr += "' referenced by AST file";
1931 Error(ErrorStr.c_str());
1933 // Record that we didn't find the file.
1934 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
1938 // Check if there was a request to override the contents of the file
1939 // that was part of the precompiled header. Overridding such a file
1940 // can lead to problems when lexing using the source locations from the
1942 SourceManager &SM = getSourceManager();
1943 if (!Overridden && SM.isFileOverridden(File)) {
1945 Error(diag::err_fe_pch_file_overridden, Filename);
1946 // After emitting the diagnostic, recover by disabling the override so
1947 // that the original file will be used.
1948 SM.disableFileContentsOverride(File);
1949 // The FileEntry is a virtual file entry with the size of the contents
1950 // that would override the original contents. Set it to the original's
1952 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1953 StoredSize, StoredTime);
1956 bool IsOutOfDate = false;
1958 // For an overridden file, there is nothing to validate.
1959 if (!Overridden && //
1960 (StoredSize != File->getSize() ||
1961 #if defined(LLVM_ON_WIN32)
1964 // In our regression testing, the Windows file system seems to
1965 // have inconsistent modification times that sometimes
1966 // erroneously trigger this error-handling path.
1968 // This also happens in networked file systems, so disable this
1969 // check if validation is disabled or if we have an explicitly
1972 // FIXME: Should we also do this for PCH files? They could also
1973 // reasonably get shared across a network during a distributed build.
1974 (StoredTime != File->getModificationTime() && !DisableValidation &&
1975 F.Kind != MK_ExplicitModule)
1979 // Build a list of the PCH imports that got us here (in reverse).
1980 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
1981 while (ImportStack.back()->ImportedBy.size() > 0)
1982 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
1984 // The top-level PCH is stale.
1985 StringRef TopLevelPCHName(ImportStack.back()->FileName);
1986 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
1988 // Print the import stack.
1989 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
1990 Diag(diag::note_pch_required_by)
1991 << Filename << ImportStack[0]->FileName;
1992 for (unsigned I = 1; I < ImportStack.size(); ++I)
1993 Diag(diag::note_pch_required_by)
1994 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
1997 if (!Diags.isDiagnosticInFlight())
1998 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2004 InputFile IF = InputFile(File, Overridden, IsOutOfDate);
2006 // Note that we've loaded this input file.
2007 F.InputFilesLoaded[ID-1] = IF;
2011 /// \brief If we are loading a relocatable PCH or module file, and the filename
2012 /// is not an absolute path, add the system or module root to the beginning of
2014 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2015 // Resolve relative to the base directory, if we have one.
2016 if (!M.BaseDirectory.empty())
2017 return ResolveImportedPath(Filename, M.BaseDirectory);
2020 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2021 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2024 SmallString<128> Buffer;
2025 llvm::sys::path::append(Buffer, Prefix, Filename);
2026 Filename.assign(Buffer.begin(), Buffer.end());
2029 ASTReader::ASTReadResult
2030 ASTReader::ReadControlBlock(ModuleFile &F,
2031 SmallVectorImpl<ImportedModule> &Loaded,
2032 const ModuleFile *ImportedBy,
2033 unsigned ClientLoadCapabilities) {
2034 BitstreamCursor &Stream = F.Stream;
2036 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2037 Error("malformed block record in AST file");
2041 // Should we allow the configuration of the module file to differ from the
2042 // configuration of the current translation unit in a compatible way?
2044 // FIXME: Allow this for files explicitly specified with -include-pch too.
2045 bool AllowCompatibleConfigurationMismatch = F.Kind == MK_ExplicitModule;
2047 // Read all of the records and blocks in the control block.
2049 unsigned NumInputs = 0;
2050 unsigned NumUserInputs = 0;
2052 llvm::BitstreamEntry Entry = Stream.advance();
2054 switch (Entry.Kind) {
2055 case llvm::BitstreamEntry::Error:
2056 Error("malformed block record in AST file");
2058 case llvm::BitstreamEntry::EndBlock: {
2059 // Validate input files.
2060 const HeaderSearchOptions &HSOpts =
2061 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2063 // All user input files reside at the index range [0, NumUserInputs), and
2064 // system input files reside at [NumUserInputs, NumInputs).
2065 if (!DisableValidation) {
2066 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2068 // If we are reading a module, we will create a verification timestamp,
2069 // so we verify all input files. Otherwise, verify only user input
2072 unsigned N = NumUserInputs;
2073 if (ValidateSystemInputs ||
2074 (HSOpts.ModulesValidateOncePerBuildSession &&
2075 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2076 F.Kind == MK_ImplicitModule))
2079 for (unsigned I = 0; I < N; ++I) {
2080 InputFile IF = getInputFile(F, I+1, Complain);
2081 if (!IF.getFile() || IF.isOutOfDate())
2087 Listener->visitModuleFile(F.FileName);
2089 if (Listener && Listener->needsInputFileVisitation()) {
2090 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2092 for (unsigned I = 0; I < N; ++I) {
2093 bool IsSystem = I >= NumUserInputs;
2094 InputFileInfo FI = readInputFileInfo(F, I+1);
2095 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden);
2102 case llvm::BitstreamEntry::SubBlock:
2104 case INPUT_FILES_BLOCK_ID:
2105 F.InputFilesCursor = Stream;
2106 if (Stream.SkipBlock() || // Skip with the main cursor
2107 // Read the abbreviations
2108 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2109 Error("malformed block record in AST file");
2115 if (Stream.SkipBlock()) {
2116 Error("malformed block record in AST file");
2122 case llvm::BitstreamEntry::Record:
2123 // The interesting case.
2127 // Read and process a record.
2130 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
2132 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2133 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2134 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2135 : diag::err_pch_version_too_new);
2136 return VersionMismatch;
2139 bool hasErrors = Record[5];
2140 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2141 Diag(diag::err_pch_with_compiler_errors);
2145 F.RelocatablePCH = Record[4];
2146 // Relative paths in a relocatable PCH are relative to our sysroot.
2147 if (F.RelocatablePCH)
2148 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2150 const std::string &CurBranch = getClangFullRepositoryVersion();
2151 StringRef ASTBranch = Blob;
2152 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2153 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2154 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2155 return VersionMismatch;
2161 assert((!F.Signature || F.Signature == Record[0]) && "signature changed");
2162 F.Signature = Record[0];
2166 // Load each of the imported PCH files.
2167 unsigned Idx = 0, N = Record.size();
2169 // Read information about the AST file.
2170 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2171 // The import location will be the local one for now; we will adjust
2172 // all import locations of module imports after the global source
2173 // location info are setup.
2174 SourceLocation ImportLoc =
2175 SourceLocation::getFromRawEncoding(Record[Idx++]);
2176 off_t StoredSize = (off_t)Record[Idx++];
2177 time_t StoredModTime = (time_t)Record[Idx++];
2178 ASTFileSignature StoredSignature = Record[Idx++];
2179 auto ImportedFile = ReadPath(F, Record, Idx);
2181 // Load the AST file.
2182 switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded,
2183 StoredSize, StoredModTime, StoredSignature,
2184 ClientLoadCapabilities)) {
2185 case Failure: return Failure;
2186 // If we have to ignore the dependency, we'll have to ignore this too.
2188 case OutOfDate: return OutOfDate;
2189 case VersionMismatch: return VersionMismatch;
2190 case ConfigurationMismatch: return ConfigurationMismatch;
2191 case HadErrors: return HadErrors;
2192 case Success: break;
2198 case KNOWN_MODULE_FILES:
2201 case LANGUAGE_OPTIONS: {
2202 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2203 // FIXME: The &F == *ModuleMgr.begin() check is wrong for modules.
2204 if (Listener && &F == *ModuleMgr.begin() &&
2205 ParseLanguageOptions(Record, Complain, *Listener,
2206 AllowCompatibleConfigurationMismatch) &&
2207 !DisableValidation && !AllowConfigurationMismatch)
2208 return ConfigurationMismatch;
2212 case TARGET_OPTIONS: {
2213 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2214 if (Listener && &F == *ModuleMgr.begin() &&
2215 ParseTargetOptions(Record, Complain, *Listener,
2216 AllowCompatibleConfigurationMismatch) &&
2217 !DisableValidation && !AllowConfigurationMismatch)
2218 return ConfigurationMismatch;
2222 case DIAGNOSTIC_OPTIONS: {
2223 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate)==0;
2224 if (Listener && &F == *ModuleMgr.begin() &&
2225 !AllowCompatibleConfigurationMismatch &&
2226 ParseDiagnosticOptions(Record, Complain, *Listener) &&
2232 case FILE_SYSTEM_OPTIONS: {
2233 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2234 if (Listener && &F == *ModuleMgr.begin() &&
2235 !AllowCompatibleConfigurationMismatch &&
2236 ParseFileSystemOptions(Record, Complain, *Listener) &&
2237 !DisableValidation && !AllowConfigurationMismatch)
2238 return ConfigurationMismatch;
2242 case HEADER_SEARCH_OPTIONS: {
2243 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2244 if (Listener && &F == *ModuleMgr.begin() &&
2245 !AllowCompatibleConfigurationMismatch &&
2246 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
2247 !DisableValidation && !AllowConfigurationMismatch)
2248 return ConfigurationMismatch;
2252 case PREPROCESSOR_OPTIONS: {
2253 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2254 if (Listener && &F == *ModuleMgr.begin() &&
2255 !AllowCompatibleConfigurationMismatch &&
2256 ParsePreprocessorOptions(Record, Complain, *Listener,
2257 SuggestedPredefines) &&
2258 !DisableValidation && !AllowConfigurationMismatch)
2259 return ConfigurationMismatch;
2264 F.OriginalSourceFileID = FileID::get(Record[0]);
2265 F.ActualOriginalSourceFileName = Blob;
2266 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2267 ResolveImportedPath(F, F.OriginalSourceFileName);
2270 case ORIGINAL_FILE_ID:
2271 F.OriginalSourceFileID = FileID::get(Record[0]);
2274 case ORIGINAL_PCH_DIR:
2275 F.OriginalDir = Blob;
2279 F.ModuleName = Blob;
2281 Listener->ReadModuleName(F.ModuleName);
2284 case MODULE_DIRECTORY: {
2285 assert(!F.ModuleName.empty() &&
2286 "MODULE_DIRECTORY found before MODULE_NAME");
2287 // If we've already loaded a module map file covering this module, we may
2288 // have a better path for it (relative to the current build).
2289 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
2290 if (M && M->Directory) {
2291 // If we're implicitly loading a module, the base directory can't
2292 // change between the build and use.
2293 if (F.Kind != MK_ExplicitModule) {
2294 const DirectoryEntry *BuildDir =
2295 PP.getFileManager().getDirectory(Blob);
2296 if (!BuildDir || BuildDir != M->Directory) {
2297 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2298 Diag(diag::err_imported_module_relocated)
2299 << F.ModuleName << Blob << M->Directory->getName();
2303 F.BaseDirectory = M->Directory->getName();
2305 F.BaseDirectory = Blob;
2310 case MODULE_MAP_FILE:
2311 if (ASTReadResult Result =
2312 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2316 case INPUT_FILE_OFFSETS:
2317 NumInputs = Record[0];
2318 NumUserInputs = Record[1];
2319 F.InputFileOffsets =
2320 (const llvm::support::unaligned_uint64_t *)Blob.data();
2321 F.InputFilesLoaded.resize(NumInputs);
2327 ASTReader::ASTReadResult
2328 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2329 BitstreamCursor &Stream = F.Stream;
2331 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2332 Error("malformed block record in AST file");
2336 // Read all of the records and blocks for the AST file.
2339 llvm::BitstreamEntry Entry = Stream.advance();
2341 switch (Entry.Kind) {
2342 case llvm::BitstreamEntry::Error:
2343 Error("error at end of module block in AST file");
2345 case llvm::BitstreamEntry::EndBlock: {
2346 // Outside of C++, we do not store a lookup map for the translation unit.
2347 // Instead, mark it as needing a lookup map to be built if this module
2348 // contains any declarations lexically within it (which it always does!).
2349 // This usually has no cost, since we very rarely need the lookup map for
2350 // the translation unit outside C++.
2351 DeclContext *DC = Context.getTranslationUnitDecl();
2352 if (DC->hasExternalLexicalStorage() &&
2353 !getContext().getLangOpts().CPlusPlus)
2354 DC->setMustBuildLookupTable();
2358 case llvm::BitstreamEntry::SubBlock:
2360 case DECLTYPES_BLOCK_ID:
2361 // We lazily load the decls block, but we want to set up the
2362 // DeclsCursor cursor to point into it. Clone our current bitcode
2363 // cursor to it, enter the block and read the abbrevs in that block.
2364 // With the main cursor, we just skip over it.
2365 F.DeclsCursor = Stream;
2366 if (Stream.SkipBlock() || // Skip with the main cursor.
2367 // Read the abbrevs.
2368 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2369 Error("malformed block record in AST file");
2374 case PREPROCESSOR_BLOCK_ID:
2375 F.MacroCursor = Stream;
2376 if (!PP.getExternalSource())
2377 PP.setExternalSource(this);
2379 if (Stream.SkipBlock() ||
2380 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2381 Error("malformed block record in AST file");
2384 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2387 case PREPROCESSOR_DETAIL_BLOCK_ID:
2388 F.PreprocessorDetailCursor = Stream;
2389 if (Stream.SkipBlock() ||
2390 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
2391 PREPROCESSOR_DETAIL_BLOCK_ID)) {
2392 Error("malformed preprocessor detail record in AST file");
2395 F.PreprocessorDetailStartOffset
2396 = F.PreprocessorDetailCursor.GetCurrentBitNo();
2398 if (!PP.getPreprocessingRecord())
2399 PP.createPreprocessingRecord();
2400 if (!PP.getPreprocessingRecord()->getExternalSource())
2401 PP.getPreprocessingRecord()->SetExternalSource(*this);
2404 case SOURCE_MANAGER_BLOCK_ID:
2405 if (ReadSourceManagerBlock(F))
2409 case SUBMODULE_BLOCK_ID:
2410 if (ASTReadResult Result = ReadSubmoduleBlock(F, ClientLoadCapabilities))
2414 case COMMENTS_BLOCK_ID: {
2415 BitstreamCursor C = Stream;
2416 if (Stream.SkipBlock() ||
2417 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2418 Error("malformed comments block in AST file");
2421 CommentsCursors.push_back(std::make_pair(C, &F));
2426 if (Stream.SkipBlock()) {
2427 Error("malformed block record in AST file");
2434 case llvm::BitstreamEntry::Record:
2435 // The interesting case.
2439 // Read and process a record.
2442 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
2443 default: // Default behavior: ignore.
2447 if (F.LocalNumTypes != 0) {
2448 Error("duplicate TYPE_OFFSET record in AST file");
2451 F.TypeOffsets = (const uint32_t *)Blob.data();
2452 F.LocalNumTypes = Record[0];
2453 unsigned LocalBaseTypeIndex = Record[1];
2454 F.BaseTypeIndex = getTotalNumTypes();
2456 if (F.LocalNumTypes > 0) {
2457 // Introduce the global -> local mapping for types within this module.
2458 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2460 // Introduce the local -> global mapping for types within this module.
2461 F.TypeRemap.insertOrReplace(
2462 std::make_pair(LocalBaseTypeIndex,
2463 F.BaseTypeIndex - LocalBaseTypeIndex));
2465 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2471 if (F.LocalNumDecls != 0) {
2472 Error("duplicate DECL_OFFSET record in AST file");
2475 F.DeclOffsets = (const DeclOffset *)Blob.data();
2476 F.LocalNumDecls = Record[0];
2477 unsigned LocalBaseDeclID = Record[1];
2478 F.BaseDeclID = getTotalNumDecls();
2480 if (F.LocalNumDecls > 0) {
2481 // Introduce the global -> local mapping for declarations within this
2483 GlobalDeclMap.insert(
2484 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2486 // Introduce the local -> global mapping for declarations within this
2488 F.DeclRemap.insertOrReplace(
2489 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2491 // Introduce the global -> local mapping for declarations within this
2493 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2495 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2500 case TU_UPDATE_LEXICAL: {
2501 DeclContext *TU = Context.getTranslationUnitDecl();
2502 DeclContextInfo &Info = F.DeclContextInfos[TU];
2503 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(Blob.data());
2504 Info.NumLexicalDecls
2505 = static_cast<unsigned int>(Blob.size() / sizeof(KindDeclIDPair));
2506 TU->setHasExternalLexicalStorage(true);
2510 case UPDATE_VISIBLE: {
2512 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2513 ASTDeclContextNameLookupTable *Table =
2514 ASTDeclContextNameLookupTable::Create(
2515 (const unsigned char *)Blob.data() + Record[Idx++],
2516 (const unsigned char *)Blob.data() + sizeof(uint32_t),
2517 (const unsigned char *)Blob.data(),
2518 ASTDeclContextNameLookupTrait(*this, F));
2519 if (Decl *D = GetExistingDecl(ID)) {
2520 auto *DC = cast<DeclContext>(D);
2521 DC->getPrimaryContext()->setHasExternalVisibleStorage(true);
2522 auto *&LookupTable = F.DeclContextInfos[DC].NameLookupTableData;
2524 LookupTable = Table;
2526 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
2530 case IDENTIFIER_TABLE:
2531 F.IdentifierTableData = Blob.data();
2533 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
2534 (const unsigned char *)F.IdentifierTableData + Record[0],
2535 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
2536 (const unsigned char *)F.IdentifierTableData,
2537 ASTIdentifierLookupTrait(*this, F));
2539 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2543 case IDENTIFIER_OFFSET: {
2544 if (F.LocalNumIdentifiers != 0) {
2545 Error("duplicate IDENTIFIER_OFFSET record in AST file");
2548 F.IdentifierOffsets = (const uint32_t *)Blob.data();
2549 F.LocalNumIdentifiers = Record[0];
2550 unsigned LocalBaseIdentifierID = Record[1];
2551 F.BaseIdentifierID = getTotalNumIdentifiers();
2553 if (F.LocalNumIdentifiers > 0) {
2554 // Introduce the global -> local mapping for identifiers within this
2556 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2559 // Introduce the local -> global mapping for identifiers within this
2561 F.IdentifierRemap.insertOrReplace(
2562 std::make_pair(LocalBaseIdentifierID,
2563 F.BaseIdentifierID - LocalBaseIdentifierID));
2565 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2566 + F.LocalNumIdentifiers);
2571 case EAGERLY_DESERIALIZED_DECLS:
2572 // FIXME: Skip reading this record if our ASTConsumer doesn't care
2573 // about "interesting" decls (for instance, if we're building a module).
2574 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2575 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
2579 if (SpecialTypes.empty()) {
2580 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2581 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2585 if (SpecialTypes.size() != Record.size()) {
2586 Error("invalid special-types record");
2590 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2591 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2592 if (!SpecialTypes[I])
2593 SpecialTypes[I] = ID;
2594 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2600 TotalNumStatements += Record[0];
2601 TotalNumMacros += Record[1];
2602 TotalLexicalDeclContexts += Record[2];
2603 TotalVisibleDeclContexts += Record[3];
2606 case UNUSED_FILESCOPED_DECLS:
2607 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2608 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2611 case DELEGATING_CTORS:
2612 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2613 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2616 case WEAK_UNDECLARED_IDENTIFIERS:
2617 if (Record.size() % 4 != 0) {
2618 Error("invalid weak identifiers record");
2622 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2623 // files. This isn't the way to do it :)
2624 WeakUndeclaredIdentifiers.clear();
2626 // Translate the weak, undeclared identifiers into global IDs.
2627 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2628 WeakUndeclaredIdentifiers.push_back(
2629 getGlobalIdentifierID(F, Record[I++]));
2630 WeakUndeclaredIdentifiers.push_back(
2631 getGlobalIdentifierID(F, Record[I++]));
2632 WeakUndeclaredIdentifiers.push_back(
2633 ReadSourceLocation(F, Record, I).getRawEncoding());
2634 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2638 case SELECTOR_OFFSETS: {
2639 F.SelectorOffsets = (const uint32_t *)Blob.data();
2640 F.LocalNumSelectors = Record[0];
2641 unsigned LocalBaseSelectorID = Record[1];
2642 F.BaseSelectorID = getTotalNumSelectors();
2644 if (F.LocalNumSelectors > 0) {
2645 // Introduce the global -> local mapping for selectors within this
2647 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2649 // Introduce the local -> global mapping for selectors within this
2651 F.SelectorRemap.insertOrReplace(
2652 std::make_pair(LocalBaseSelectorID,
2653 F.BaseSelectorID - LocalBaseSelectorID));
2655 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2661 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
2663 F.SelectorLookupTable
2664 = ASTSelectorLookupTable::Create(
2665 F.SelectorLookupTableData + Record[0],
2666 F.SelectorLookupTableData,
2667 ASTSelectorLookupTrait(*this, F));
2668 TotalNumMethodPoolEntries += Record[1];
2671 case REFERENCED_SELECTOR_POOL:
2672 if (!Record.empty()) {
2673 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2674 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2676 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2682 case PP_COUNTER_VALUE:
2683 if (!Record.empty() && Listener)
2684 Listener->ReadCounter(F, Record[0]);
2687 case FILE_SORTED_DECLS:
2688 F.FileSortedDecls = (const DeclID *)Blob.data();
2689 F.NumFileSortedDecls = Record[0];
2692 case SOURCE_LOCATION_OFFSETS: {
2693 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
2694 F.LocalNumSLocEntries = Record[0];
2695 unsigned SLocSpaceSize = Record[1];
2696 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
2697 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2699 // Make our entry in the range map. BaseID is negative and growing, so
2700 // we invert it. Because we invert it, though, we need the other end of
2702 unsigned RangeStart =
2703 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2704 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2705 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2707 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2708 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2709 GlobalSLocOffsetMap.insert(
2710 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2711 - SLocSpaceSize,&F));
2713 // Initialize the remapping table.
2714 // Invalid stays invalid.
2715 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
2716 // This module. Base was 2 when being compiled.
2717 F.SLocRemap.insertOrReplace(std::make_pair(2U,
2718 static_cast<int>(F.SLocEntryBaseOffset - 2)));
2720 TotalNumSLocEntries += F.LocalNumSLocEntries;
2724 case MODULE_OFFSET_MAP: {
2725 // Additional remapping information.
2726 const unsigned char *Data = (const unsigned char*)Blob.data();
2727 const unsigned char *DataEnd = Data + Blob.size();
2729 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
2730 if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
2731 F.SLocRemap.insert(std::make_pair(0U, 0));
2732 F.SLocRemap.insert(std::make_pair(2U, 1));
2735 // Continuous range maps we may be updating in our module.
2736 typedef ContinuousRangeMap<uint32_t, int, 2>::Builder
2738 RemapBuilder SLocRemap(F.SLocRemap);
2739 RemapBuilder IdentifierRemap(F.IdentifierRemap);
2740 RemapBuilder MacroRemap(F.MacroRemap);
2741 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2742 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
2743 RemapBuilder SelectorRemap(F.SelectorRemap);
2744 RemapBuilder DeclRemap(F.DeclRemap);
2745 RemapBuilder TypeRemap(F.TypeRemap);
2747 while(Data < DataEnd) {
2748 using namespace llvm::support;
2749 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
2750 StringRef Name = StringRef((const char*)Data, Len);
2752 ModuleFile *OM = ModuleMgr.lookup(Name);
2754 Error("SourceLocation remap refers to unknown module");
2758 uint32_t SLocOffset =
2759 endian::readNext<uint32_t, little, unaligned>(Data);
2760 uint32_t IdentifierIDOffset =
2761 endian::readNext<uint32_t, little, unaligned>(Data);
2762 uint32_t MacroIDOffset =
2763 endian::readNext<uint32_t, little, unaligned>(Data);
2764 uint32_t PreprocessedEntityIDOffset =
2765 endian::readNext<uint32_t, little, unaligned>(Data);
2766 uint32_t SubmoduleIDOffset =
2767 endian::readNext<uint32_t, little, unaligned>(Data);
2768 uint32_t SelectorIDOffset =
2769 endian::readNext<uint32_t, little, unaligned>(Data);
2770 uint32_t DeclIDOffset =
2771 endian::readNext<uint32_t, little, unaligned>(Data);
2772 uint32_t TypeIndexOffset =
2773 endian::readNext<uint32_t, little, unaligned>(Data);
2775 uint32_t None = std::numeric_limits<uint32_t>::max();
2777 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
2778 RemapBuilder &Remap) {
2780 Remap.insert(std::make_pair(Offset,
2781 static_cast<int>(BaseOffset - Offset)));
2783 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
2784 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
2785 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
2786 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
2787 PreprocessedEntityRemap);
2788 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
2789 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
2790 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
2791 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
2793 // Global -> local mappings.
2794 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
2799 case SOURCE_MANAGER_LINE_TABLE:
2800 if (ParseLineTable(F, Record))
2804 case SOURCE_LOCATION_PRELOADS: {
2805 // Need to transform from the local view (1-based IDs) to the global view,
2806 // which is based off F.SLocEntryBaseID.
2807 if (!F.PreloadSLocEntries.empty()) {
2808 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2812 F.PreloadSLocEntries.swap(Record);
2816 case EXT_VECTOR_DECLS:
2817 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2818 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
2822 if (Record.size() % 3 != 0) {
2823 Error("Invalid VTABLE_USES record");
2827 // Later tables overwrite earlier ones.
2828 // FIXME: Modules will have some trouble with this. This is clearly not
2829 // the right way to do this.
2832 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2833 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2834 VTableUses.push_back(
2835 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2836 VTableUses.push_back(Record[Idx++]);
2840 case PENDING_IMPLICIT_INSTANTIATIONS:
2841 if (PendingInstantiations.size() % 2 != 0) {
2842 Error("Invalid existing PendingInstantiations");
2846 if (Record.size() % 2 != 0) {
2847 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2851 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2852 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2853 PendingInstantiations.push_back(
2854 ReadSourceLocation(F, Record, I).getRawEncoding());
2858 case SEMA_DECL_REFS:
2859 if (Record.size() != 2) {
2860 Error("Invalid SEMA_DECL_REFS block");
2863 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2864 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2867 case PPD_ENTITIES_OFFSETS: {
2868 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
2869 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
2870 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
2872 unsigned LocalBasePreprocessedEntityID = Record[0];
2874 unsigned StartingID;
2875 if (!PP.getPreprocessingRecord())
2876 PP.createPreprocessingRecord();
2877 if (!PP.getPreprocessingRecord()->getExternalSource())
2878 PP.getPreprocessingRecord()->SetExternalSource(*this);
2880 = PP.getPreprocessingRecord()
2881 ->allocateLoadedEntities(F.NumPreprocessedEntities);
2882 F.BasePreprocessedEntityID = StartingID;
2884 if (F.NumPreprocessedEntities > 0) {
2885 // Introduce the global -> local mapping for preprocessed entities in
2887 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2889 // Introduce the local -> global mapping for preprocessed entities in
2891 F.PreprocessedEntityRemap.insertOrReplace(
2892 std::make_pair(LocalBasePreprocessedEntityID,
2893 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2899 case DECL_UPDATE_OFFSETS: {
2900 if (Record.size() % 2 != 0) {
2901 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2904 for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
2905 GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
2906 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
2908 // If we've already loaded the decl, perform the updates when we finish
2909 // loading this block.
2910 if (Decl *D = GetExistingDecl(ID))
2911 PendingUpdateRecords.push_back(std::make_pair(ID, D));
2916 case DECL_REPLACEMENTS: {
2917 if (Record.size() % 3 != 0) {
2918 Error("invalid DECL_REPLACEMENTS block in AST file");
2921 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
2922 ReplacedDecls[getGlobalDeclID(F, Record[I])]
2923 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
2927 case OBJC_CATEGORIES_MAP: {
2928 if (F.LocalNumObjCCategoriesInMap != 0) {
2929 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
2933 F.LocalNumObjCCategoriesInMap = Record[0];
2934 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
2938 case OBJC_CATEGORIES:
2939 F.ObjCCategories.swap(Record);
2942 case CXX_BASE_SPECIFIER_OFFSETS: {
2943 if (F.LocalNumCXXBaseSpecifiers != 0) {
2944 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2948 F.LocalNumCXXBaseSpecifiers = Record[0];
2949 F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data();
2953 case CXX_CTOR_INITIALIZERS_OFFSETS: {
2954 if (F.LocalNumCXXCtorInitializers != 0) {
2955 Error("duplicate CXX_CTOR_INITIALIZERS_OFFSETS record in AST file");
2959 F.LocalNumCXXCtorInitializers = Record[0];
2960 F.CXXCtorInitializersOffsets = (const uint32_t *)Blob.data();
2964 case DIAG_PRAGMA_MAPPINGS:
2965 if (F.PragmaDiagMappings.empty())
2966 F.PragmaDiagMappings.swap(Record);
2968 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2969 Record.begin(), Record.end());
2972 case CUDA_SPECIAL_DECL_REFS:
2973 // Later tables overwrite earlier ones.
2974 // FIXME: Modules will have trouble with this.
2975 CUDASpecialDeclRefs.clear();
2976 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2977 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2980 case HEADER_SEARCH_TABLE: {
2981 F.HeaderFileInfoTableData = Blob.data();
2982 F.LocalNumHeaderFileInfos = Record[1];
2984 F.HeaderFileInfoTable
2985 = HeaderFileInfoLookupTable::Create(
2986 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2987 (const unsigned char *)F.HeaderFileInfoTableData,
2988 HeaderFileInfoTrait(*this, F,
2989 &PP.getHeaderSearchInfo(),
2990 Blob.data() + Record[2]));
2992 PP.getHeaderSearchInfo().SetExternalSource(this);
2993 if (!PP.getHeaderSearchInfo().getExternalLookup())
2994 PP.getHeaderSearchInfo().SetExternalLookup(this);
2999 case FP_PRAGMA_OPTIONS:
3000 // Later tables overwrite earlier ones.
3001 FPPragmaOptions.swap(Record);
3004 case OPENCL_EXTENSIONS:
3005 // Later tables overwrite earlier ones.
3006 OpenCLExtensions.swap(Record);
3009 case TENTATIVE_DEFINITIONS:
3010 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3011 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3014 case KNOWN_NAMESPACES:
3015 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3016 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3019 case UNDEFINED_BUT_USED:
3020 if (UndefinedButUsed.size() % 2 != 0) {
3021 Error("Invalid existing UndefinedButUsed");
3025 if (Record.size() % 2 != 0) {
3026 Error("invalid undefined-but-used record");
3029 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3030 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3031 UndefinedButUsed.push_back(
3032 ReadSourceLocation(F, Record, I).getRawEncoding());
3035 case DELETE_EXPRS_TO_ANALYZE:
3036 for (unsigned I = 0, N = Record.size(); I != N;) {
3037 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3038 const uint64_t Count = Record[I++];
3039 DelayedDeleteExprs.push_back(Count);
3040 for (uint64_t C = 0; C < Count; ++C) {
3041 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3042 bool IsArrayForm = Record[I++] == 1;
3043 DelayedDeleteExprs.push_back(IsArrayForm);
3048 case IMPORTED_MODULES: {
3049 if (F.Kind != MK_ImplicitModule && F.Kind != MK_ExplicitModule) {
3050 // If we aren't loading a module (which has its own exports), make
3051 // all of the imported modules visible.
3052 // FIXME: Deal with macros-only imports.
3053 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3054 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3055 SourceLocation Loc = ReadSourceLocation(F, Record, I);
3057 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3063 case LOCAL_REDECLARATIONS: {
3064 F.RedeclarationChains.swap(Record);
3068 case LOCAL_REDECLARATIONS_MAP: {
3069 if (F.LocalNumRedeclarationsInMap != 0) {
3070 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
3074 F.LocalNumRedeclarationsInMap = Record[0];
3075 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data();
3079 case MACRO_OFFSET: {
3080 if (F.LocalNumMacros != 0) {
3081 Error("duplicate MACRO_OFFSET record in AST file");
3084 F.MacroOffsets = (const uint32_t *)Blob.data();
3085 F.LocalNumMacros = Record[0];
3086 unsigned LocalBaseMacroID = Record[1];
3087 F.BaseMacroID = getTotalNumMacros();
3089 if (F.LocalNumMacros > 0) {
3090 // Introduce the global -> local mapping for macros within this module.
3091 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3093 // Introduce the local -> global mapping for macros within this module.
3094 F.MacroRemap.insertOrReplace(
3095 std::make_pair(LocalBaseMacroID,
3096 F.BaseMacroID - LocalBaseMacroID));
3098 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3103 case LATE_PARSED_TEMPLATE: {
3104 LateParsedTemplates.append(Record.begin(), Record.end());
3108 case OPTIMIZE_PRAGMA_OPTIONS:
3109 if (Record.size() != 1) {
3110 Error("invalid pragma optimize record");
3113 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3116 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3117 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3118 UnusedLocalTypedefNameCandidates.push_back(
3119 getGlobalDeclID(F, Record[I]));
3125 ASTReader::ASTReadResult
3126 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3127 const ModuleFile *ImportedBy,
3128 unsigned ClientLoadCapabilities) {
3130 F.ModuleMapPath = ReadPath(F, Record, Idx);
3132 if (F.Kind == MK_ExplicitModule) {
3133 // For an explicitly-loaded module, we don't care whether the original
3134 // module map file exists or matches.
3138 // Try to resolve ModuleName in the current header search context and
3139 // verify that it is found in the same module map file as we saved. If the
3140 // top-level AST file is a main file, skip this check because there is no
3141 // usable header search context.
3142 assert(!F.ModuleName.empty() &&
3143 "MODULE_NAME should come before MODULE_MAP_FILE");
3144 if (F.Kind == MK_ImplicitModule &&
3145 (*ModuleMgr.begin())->Kind != MK_MainFile) {
3146 // An implicitly-loaded module file should have its module listed in some
3147 // module map file that we've already loaded.
3148 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3149 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3150 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3152 assert(ImportedBy && "top-level import should be verified");
3153 if ((ClientLoadCapabilities & ARR_Missing) == 0)
3154 Diag(diag::err_imported_module_not_found) << F.ModuleName << F.FileName
3155 << ImportedBy->FileName
3160 assert(M->Name == F.ModuleName && "found module with different name");
3162 // Check the primary module map file.
3163 const FileEntry *StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3164 if (StoredModMap == nullptr || StoredModMap != ModMap) {
3165 assert(ModMap && "found module is missing module map file");
3166 assert(ImportedBy && "top-level import should be verified");
3167 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3168 Diag(diag::err_imported_module_modmap_changed)
3169 << F.ModuleName << ImportedBy->FileName
3170 << ModMap->getName() << F.ModuleMapPath;
3174 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3175 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3176 // FIXME: we should use input files rather than storing names.
3177 std::string Filename = ReadPath(F, Record, Idx);
3178 const FileEntry *F =
3179 FileMgr.getFile(Filename, false, false);
3181 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3182 Error("could not find file '" + Filename +"' referenced by AST file");
3185 AdditionalStoredMaps.insert(F);
3188 // Check any additional module map files (e.g. module.private.modulemap)
3189 // that are not in the pcm.
3190 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3191 for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3192 // Remove files that match
3193 // Note: SmallPtrSet::erase is really remove
3194 if (!AdditionalStoredMaps.erase(ModMap)) {
3195 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3196 Diag(diag::err_module_different_modmap)
3197 << F.ModuleName << /*new*/0 << ModMap->getName();
3203 // Check any additional module map files that are in the pcm, but not
3204 // found in header search. Cases that match are already removed.
3205 for (const FileEntry *ModMap : AdditionalStoredMaps) {
3206 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3207 Diag(diag::err_module_different_modmap)
3208 << F.ModuleName << /*not new*/1 << ModMap->getName();
3214 Listener->ReadModuleMapFile(F.ModuleMapPath);
3219 /// \brief Move the given method to the back of the global list of methods.
3220 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3221 // Find the entry for this selector in the method pool.
3222 Sema::GlobalMethodPool::iterator Known
3223 = S.MethodPool.find(Method->getSelector());
3224 if (Known == S.MethodPool.end())
3227 // Retrieve the appropriate method list.
3228 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3229 : Known->second.second;
3231 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
3233 if (List->getMethod() == Method) {
3241 if (List->getNext())
3242 List->setMethod(List->getNext()->getMethod());
3244 List->setMethod(Method);
3248 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
3249 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
3250 for (Decl *D : Names) {
3251 bool wasHidden = D->Hidden;
3254 if (wasHidden && SemaObj) {
3255 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
3256 moveMethodToBackOfGlobalList(*SemaObj, Method);
3262 void ASTReader::makeModuleVisible(Module *Mod,
3263 Module::NameVisibilityKind NameVisibility,
3264 SourceLocation ImportLoc) {
3265 llvm::SmallPtrSet<Module *, 4> Visited;
3266 SmallVector<Module *, 4> Stack;
3267 Stack.push_back(Mod);
3268 while (!Stack.empty()) {
3269 Mod = Stack.pop_back_val();
3271 if (NameVisibility <= Mod->NameVisibility) {
3272 // This module already has this level of visibility (or greater), so
3273 // there is nothing more to do.
3277 if (!Mod->isAvailable()) {
3278 // Modules that aren't available cannot be made visible.
3282 // Update the module's name visibility.
3283 Mod->NameVisibility = NameVisibility;
3285 // If we've already deserialized any names from this module,
3286 // mark them as visible.
3287 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3288 if (Hidden != HiddenNamesMap.end()) {
3289 auto HiddenNames = std::move(*Hidden);
3290 HiddenNamesMap.erase(Hidden);
3291 makeNamesVisible(HiddenNames.second, HiddenNames.first);
3292 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
3293 "making names visible added hidden names");
3296 // Push any exported modules onto the stack to be marked as visible.
3297 SmallVector<Module *, 16> Exports;
3298 Mod->getExportedModules(Exports);
3299 for (SmallVectorImpl<Module *>::iterator
3300 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
3301 Module *Exported = *I;
3302 if (Visited.insert(Exported).second)
3303 Stack.push_back(Exported);
3308 bool ASTReader::loadGlobalIndex() {
3312 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
3313 !Context.getLangOpts().Modules)
3316 // Try to load the global index.
3317 TriedLoadingGlobalIndex = true;
3318 StringRef ModuleCachePath
3319 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
3320 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
3321 = GlobalModuleIndex::readIndex(ModuleCachePath);
3325 GlobalIndex.reset(Result.first);
3326 ModuleMgr.setGlobalIndex(GlobalIndex.get());
3330 bool ASTReader::isGlobalIndexUnavailable() const {
3331 return Context.getLangOpts().Modules && UseGlobalIndex &&
3332 !hasGlobalIndex() && TriedLoadingGlobalIndex;
3335 static void updateModuleTimestamp(ModuleFile &MF) {
3336 // Overwrite the timestamp file contents so that file's mtime changes.
3337 std::string TimestampFilename = MF.getTimestampFilename();
3339 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::F_Text);
3342 OS << "Timestamp file\n";
3345 ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
3347 SourceLocation ImportLoc,
3348 unsigned ClientLoadCapabilities) {
3349 llvm::SaveAndRestore<SourceLocation>
3350 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
3352 // Defer any pending actions until we get to the end of reading the AST file.
3353 Deserializing AnASTFile(this);
3355 // Bump the generation number.
3356 unsigned PreviousGeneration = incrementGeneration(Context);
3358 unsigned NumModules = ModuleMgr.size();
3359 SmallVector<ImportedModule, 4> Loaded;
3360 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
3361 /*ImportedBy=*/nullptr, Loaded,
3363 ClientLoadCapabilities)) {
3367 case VersionMismatch:
3368 case ConfigurationMismatch:
3370 llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet;
3371 for (const ImportedModule &IM : Loaded)
3372 LoadedSet.insert(IM.Mod);
3374 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
3376 Context.getLangOpts().Modules
3377 ? &PP.getHeaderSearchInfo().getModuleMap()
3380 // If we find that any modules are unusable, the global index is going
3381 // to be out-of-date. Just remove it.
3382 GlobalIndex.reset();
3383 ModuleMgr.setGlobalIndex(nullptr);
3390 // Here comes stuff that we only do once the entire chain is loaded.
3392 // Load the AST blocks of all of the modules that we loaded.
3393 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3394 MEnd = Loaded.end();
3396 ModuleFile &F = *M->Mod;
3398 // Read the AST block.
3399 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
3402 // Once read, set the ModuleFile bit base offset and update the size in
3403 // bits of all files we've seen.
3404 F.GlobalBitOffset = TotalModulesSizeInBits;
3405 TotalModulesSizeInBits += F.SizeInBits;
3406 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
3408 // Preload SLocEntries.
3409 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
3410 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
3411 // Load it through the SourceManager and don't call ReadSLocEntry()
3412 // directly because the entry may have already been loaded in which case
3413 // calling ReadSLocEntry() directly would trigger an assertion in
3415 SourceMgr.getLoadedSLocEntryByID(Index);
3419 // Setup the import locations and notify the module manager that we've
3420 // committed to these module files.
3421 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3422 MEnd = Loaded.end();
3424 ModuleFile &F = *M->Mod;
3426 ModuleMgr.moduleFileAccepted(&F);
3428 // Set the import location.
3429 F.DirectImportLoc = ImportLoc;
3431 F.ImportLoc = M->ImportLoc;
3433 F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
3434 M->ImportLoc.getRawEncoding());
3437 // Mark all of the identifiers in the identifier table as being out of date,
3438 // so that various accessors know to check the loaded modules when the
3439 // identifier is used.
3440 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
3441 IdEnd = PP.getIdentifierTable().end();
3443 Id->second->setOutOfDate(true);
3445 // Resolve any unresolved module exports.
3446 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
3447 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
3448 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
3449 Module *ResolvedMod = getSubmodule(GlobalID);
3451 switch (Unresolved.Kind) {
3452 case UnresolvedModuleRef::Conflict:
3454 Module::Conflict Conflict;
3455 Conflict.Other = ResolvedMod;
3456 Conflict.Message = Unresolved.String.str();
3457 Unresolved.Mod->Conflicts.push_back(Conflict);
3461 case UnresolvedModuleRef::Import:
3463 Unresolved.Mod->Imports.insert(ResolvedMod);
3466 case UnresolvedModuleRef::Export:
3467 if (ResolvedMod || Unresolved.IsWildcard)
3468 Unresolved.Mod->Exports.push_back(
3469 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
3473 UnresolvedModuleRefs.clear();
3475 // FIXME: How do we load the 'use'd modules? They may not be submodules.
3476 // Might be unnecessary as use declarations are only used to build the
3479 InitializeContext();
3484 if (DeserializationListener)
3485 DeserializationListener->ReaderInitialized(this);
3487 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
3488 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
3489 PrimaryModule.OriginalSourceFileID
3490 = FileID::get(PrimaryModule.SLocEntryBaseID
3491 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
3493 // If this AST file is a precompiled preamble, then set the
3494 // preamble file ID of the source manager to the file source file
3495 // from which the preamble was built.
3496 if (Type == MK_Preamble) {
3497 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
3498 } else if (Type == MK_MainFile) {
3499 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
3503 // For any Objective-C class definitions we have already loaded, make sure
3504 // that we load any additional categories.
3505 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3506 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3507 ObjCClassesLoaded[I],
3508 PreviousGeneration);
3511 if (PP.getHeaderSearchInfo()
3512 .getHeaderSearchOpts()
3513 .ModulesValidateOncePerBuildSession) {
3514 // Now we are certain that the module and all modules it depends on are
3515 // up to date. Create or update timestamp files for modules that are
3516 // located in the module cache (not for PCH files that could be anywhere
3517 // in the filesystem).
3518 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
3519 ImportedModule &M = Loaded[I];
3520 if (M.Mod->Kind == MK_ImplicitModule) {
3521 updateModuleTimestamp(*M.Mod);
3529 static ASTFileSignature readASTFileSignature(llvm::BitstreamReader &StreamFile);
3531 /// \brief Whether \p Stream starts with the AST/PCH file magic number 'CPCH'.
3532 static bool startsWithASTFileMagic(BitstreamCursor &Stream) {
3533 return Stream.Read(8) == 'C' &&
3534 Stream.Read(8) == 'P' &&
3535 Stream.Read(8) == 'C' &&
3536 Stream.Read(8) == 'H';
3539 ASTReader::ASTReadResult
3540 ASTReader::ReadASTCore(StringRef FileName,
3542 SourceLocation ImportLoc,
3543 ModuleFile *ImportedBy,
3544 SmallVectorImpl<ImportedModule> &Loaded,
3545 off_t ExpectedSize, time_t ExpectedModTime,
3546 ASTFileSignature ExpectedSignature,
3547 unsigned ClientLoadCapabilities) {
3549 std::string ErrorStr;
3550 ModuleManager::AddModuleResult AddResult
3551 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
3552 getGeneration(), ExpectedSize, ExpectedModTime,
3553 ExpectedSignature, readASTFileSignature,
3556 switch (AddResult) {
3557 case ModuleManager::AlreadyLoaded:
3560 case ModuleManager::NewlyLoaded:
3561 // Load module file below.
3564 case ModuleManager::Missing:
3565 // The module file was missing; if the client can handle that, return
3567 if (ClientLoadCapabilities & ARR_Missing)
3570 // Otherwise, return an error.
3572 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3578 case ModuleManager::OutOfDate:
3579 // We couldn't load the module file because it is out-of-date. If the
3580 // client can handle out-of-date, return it.
3581 if (ClientLoadCapabilities & ARR_OutOfDate)
3584 // Otherwise, return an error.
3586 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3593 assert(M && "Missing module file");
3595 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3597 if (FileName != "-") {
3598 CurrentDir = llvm::sys::path::parent_path(FileName);
3599 if (CurrentDir.empty()) CurrentDir = ".";
3603 BitstreamCursor &Stream = F.Stream;
3604 PCHContainerRdr.ExtractPCH(F.Buffer->getMemBufferRef(), F.StreamFile);
3605 Stream.init(&F.StreamFile);
3606 F.SizeInBits = F.Buffer->getBufferSize() * 8;
3608 // Sniff for the signature.
3609 if (!startsWithASTFileMagic(Stream)) {
3610 Diag(diag::err_not_a_pch_file) << FileName;
3614 // This is used for compatibility with older PCH formats.
3615 bool HaveReadControlBlock = false;
3618 llvm::BitstreamEntry Entry = Stream.advance();
3620 switch (Entry.Kind) {
3621 case llvm::BitstreamEntry::Error:
3622 case llvm::BitstreamEntry::EndBlock:
3623 case llvm::BitstreamEntry::Record:
3624 Error("invalid record at top-level of AST file");
3627 case llvm::BitstreamEntry::SubBlock:
3631 // We only know the control subblock ID.
3633 case llvm::bitc::BLOCKINFO_BLOCK_ID:
3634 if (Stream.ReadBlockInfoBlock()) {
3635 Error("malformed BlockInfoBlock in AST file");
3639 case CONTROL_BLOCK_ID:
3640 HaveReadControlBlock = true;
3641 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
3645 case Failure: return Failure;
3646 case Missing: return Missing;
3647 case OutOfDate: return OutOfDate;
3648 case VersionMismatch: return VersionMismatch;
3649 case ConfigurationMismatch: return ConfigurationMismatch;
3650 case HadErrors: return HadErrors;
3654 if (!HaveReadControlBlock) {
3655 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3656 Diag(diag::err_pch_version_too_old);
3657 return VersionMismatch;
3660 // Record that we've loaded this module.
3661 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
3665 if (Stream.SkipBlock()) {
3666 Error("malformed block record in AST file");
3676 void ASTReader::InitializeContext() {
3677 // If there's a listener, notify them that we "read" the translation unit.
3678 if (DeserializationListener)
3679 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3680 Context.getTranslationUnitDecl());
3682 // FIXME: Find a better way to deal with collisions between these
3683 // built-in types. Right now, we just ignore the problem.
3685 // Load the special types.
3686 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
3687 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3688 if (!Context.CFConstantStringTypeDecl)
3689 Context.setCFConstantStringType(GetType(String));
3692 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3693 QualType FileType = GetType(File);
3694 if (FileType.isNull()) {
3695 Error("FILE type is NULL");
3699 if (!Context.FILEDecl) {
3700 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3701 Context.setFILEDecl(Typedef->getDecl());
3703 const TagType *Tag = FileType->getAs<TagType>();
3705 Error("Invalid FILE type in AST file");
3708 Context.setFILEDecl(Tag->getDecl());
3713 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
3714 QualType Jmp_bufType = GetType(Jmp_buf);
3715 if (Jmp_bufType.isNull()) {
3716 Error("jmp_buf type is NULL");
3720 if (!Context.jmp_bufDecl) {
3721 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3722 Context.setjmp_bufDecl(Typedef->getDecl());
3724 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3726 Error("Invalid jmp_buf type in AST file");
3729 Context.setjmp_bufDecl(Tag->getDecl());
3734 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
3735 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3736 if (Sigjmp_bufType.isNull()) {
3737 Error("sigjmp_buf type is NULL");
3741 if (!Context.sigjmp_bufDecl) {
3742 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3743 Context.setsigjmp_bufDecl(Typedef->getDecl());
3745 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3746 assert(Tag && "Invalid sigjmp_buf type in AST file");
3747 Context.setsigjmp_bufDecl(Tag->getDecl());
3752 if (unsigned ObjCIdRedef
3753 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3754 if (Context.ObjCIdRedefinitionType.isNull())
3755 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3758 if (unsigned ObjCClassRedef
3759 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3760 if (Context.ObjCClassRedefinitionType.isNull())
3761 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3764 if (unsigned ObjCSelRedef
3765 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3766 if (Context.ObjCSelRedefinitionType.isNull())
3767 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3770 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3771 QualType Ucontext_tType = GetType(Ucontext_t);
3772 if (Ucontext_tType.isNull()) {
3773 Error("ucontext_t type is NULL");
3777 if (!Context.ucontext_tDecl) {
3778 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3779 Context.setucontext_tDecl(Typedef->getDecl());
3781 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3782 assert(Tag && "Invalid ucontext_t type in AST file");
3783 Context.setucontext_tDecl(Tag->getDecl());
3789 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
3791 // If there were any CUDA special declarations, deserialize them.
3792 if (!CUDASpecialDeclRefs.empty()) {
3793 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
3794 Context.setcudaConfigureCallDecl(
3795 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3798 // Re-export any modules that were imported by a non-module AST file.
3799 // FIXME: This does not make macro-only imports visible again.
3800 for (auto &Import : ImportedModules) {
3801 if (Module *Imported = getSubmodule(Import.ID)) {
3802 makeModuleVisible(Imported, Module::AllVisible,
3803 /*ImportLoc=*/Import.ImportLoc);
3804 PP.makeModuleVisible(Imported, Import.ImportLoc);
3807 ImportedModules.clear();
3810 void ASTReader::finalizeForWriting() {
3811 // Nothing to do for now.
3814 /// \brief Given a cursor at the start of an AST file, scan ahead and drop the
3815 /// cursor into the start of the given block ID, returning false on success and
3816 /// true on failure.
3817 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
3819 llvm::BitstreamEntry Entry = Cursor.advance();
3820 switch (Entry.Kind) {
3821 case llvm::BitstreamEntry::Error:
3822 case llvm::BitstreamEntry::EndBlock:
3825 case llvm::BitstreamEntry::Record:
3826 // Ignore top-level records.
3827 Cursor.skipRecord(Entry.ID);
3830 case llvm::BitstreamEntry::SubBlock:
3831 if (Entry.ID == BlockID) {
3832 if (Cursor.EnterSubBlock(BlockID))
3838 if (Cursor.SkipBlock())
3844 /// \brief Reads and return the signature record from \p StreamFile's control
3845 /// block, or else returns 0.
3846 static ASTFileSignature readASTFileSignature(llvm::BitstreamReader &StreamFile){
3847 BitstreamCursor Stream(StreamFile);
3848 if (!startsWithASTFileMagic(Stream))
3851 // Scan for the CONTROL_BLOCK_ID block.
3852 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
3855 // Scan for SIGNATURE inside the control block.
3856 ASTReader::RecordData Record;
3858 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
3859 if (Entry.Kind == llvm::BitstreamEntry::EndBlock ||
3860 Entry.Kind != llvm::BitstreamEntry::Record)
3865 if (SIGNATURE == Stream.readRecord(Entry.ID, Record, &Blob))
3870 /// \brief Retrieve the name of the original source file name
3871 /// directly from the AST file, without actually loading the AST
3873 std::string ASTReader::getOriginalSourceFile(
3874 const std::string &ASTFileName, FileManager &FileMgr,
3875 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
3876 // Open the AST file.
3877 auto Buffer = FileMgr.getBufferForFile(ASTFileName);
3879 Diags.Report(diag::err_fe_unable_to_read_pch_file)
3880 << ASTFileName << Buffer.getError().message();
3881 return std::string();
3884 // Initialize the stream
3885 llvm::BitstreamReader StreamFile;
3886 PCHContainerRdr.ExtractPCH((*Buffer)->getMemBufferRef(), StreamFile);
3887 BitstreamCursor Stream(StreamFile);
3889 // Sniff for the signature.
3890 if (!startsWithASTFileMagic(Stream)) {
3891 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
3892 return std::string();
3895 // Scan for the CONTROL_BLOCK_ID block.
3896 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
3897 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3898 return std::string();
3901 // Scan for ORIGINAL_FILE inside the control block.
3904 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
3905 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3906 return std::string();
3908 if (Entry.Kind != llvm::BitstreamEntry::Record) {
3909 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3910 return std::string();
3915 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
3921 class SimplePCHValidator : public ASTReaderListener {
3922 const LangOptions &ExistingLangOpts;
3923 const TargetOptions &ExistingTargetOpts;
3924 const PreprocessorOptions &ExistingPPOpts;
3925 std::string ExistingModuleCachePath;
3926 FileManager &FileMgr;
3929 SimplePCHValidator(const LangOptions &ExistingLangOpts,
3930 const TargetOptions &ExistingTargetOpts,
3931 const PreprocessorOptions &ExistingPPOpts,
3932 StringRef ExistingModuleCachePath,
3933 FileManager &FileMgr)
3934 : ExistingLangOpts(ExistingLangOpts),
3935 ExistingTargetOpts(ExistingTargetOpts),
3936 ExistingPPOpts(ExistingPPOpts),
3937 ExistingModuleCachePath(ExistingModuleCachePath),
3942 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
3943 bool AllowCompatibleDifferences) override {
3944 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
3945 AllowCompatibleDifferences);
3947 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
3948 bool AllowCompatibleDifferences) override {
3949 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
3950 AllowCompatibleDifferences);
3952 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
3953 StringRef SpecificModuleCachePath,
3954 bool Complain) override {
3955 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
3956 ExistingModuleCachePath,
3957 nullptr, ExistingLangOpts);
3959 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
3961 std::string &SuggestedPredefines) override {
3962 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
3963 SuggestedPredefines, ExistingLangOpts);
3968 bool ASTReader::readASTFileControlBlock(
3969 StringRef Filename, FileManager &FileMgr,
3970 const PCHContainerReader &PCHContainerRdr,
3971 ASTReaderListener &Listener) {
3972 // Open the AST file.
3973 // FIXME: This allows use of the VFS; we do not allow use of the
3974 // VFS when actually loading a module.
3975 auto Buffer = FileMgr.getBufferForFile(Filename);
3980 // Initialize the stream
3981 llvm::BitstreamReader StreamFile;
3982 PCHContainerRdr.ExtractPCH((*Buffer)->getMemBufferRef(), StreamFile);
3983 BitstreamCursor Stream(StreamFile);
3985 // Sniff for the signature.
3986 if (!startsWithASTFileMagic(Stream))
3989 // Scan for the CONTROL_BLOCK_ID block.
3990 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
3993 bool NeedsInputFiles = Listener.needsInputFileVisitation();
3994 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
3995 bool NeedsImports = Listener.needsImportVisitation();
3996 BitstreamCursor InputFilesCursor;
3997 if (NeedsInputFiles) {
3998 InputFilesCursor = Stream;
3999 if (SkipCursorToBlock(InputFilesCursor, INPUT_FILES_BLOCK_ID))
4002 // Read the abbreviations
4004 uint64_t Offset = InputFilesCursor.GetCurrentBitNo();
4005 unsigned Code = InputFilesCursor.ReadCode();
4007 // We expect all abbrevs to be at the start of the block.
4008 if (Code != llvm::bitc::DEFINE_ABBREV) {
4009 InputFilesCursor.JumpToBit(Offset);
4012 InputFilesCursor.ReadAbbrevRecord();
4016 // Scan for ORIGINAL_FILE inside the control block.
4018 std::string ModuleDir;
4020 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4021 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
4024 if (Entry.Kind != llvm::BitstreamEntry::Record)
4029 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
4030 switch ((ControlRecordTypes)RecCode) {
4032 if (Record[0] != VERSION_MAJOR)
4035 if (Listener.ReadFullVersionInformation(Blob))
4041 Listener.ReadModuleName(Blob);
4043 case MODULE_DIRECTORY:
4046 case MODULE_MAP_FILE: {
4048 auto Path = ReadString(Record, Idx);
4049 ResolveImportedPath(Path, ModuleDir);
4050 Listener.ReadModuleMapFile(Path);
4053 case LANGUAGE_OPTIONS:
4054 if (ParseLanguageOptions(Record, false, Listener,
4055 /*AllowCompatibleConfigurationMismatch*/false))
4059 case TARGET_OPTIONS:
4060 if (ParseTargetOptions(Record, false, Listener,
4061 /*AllowCompatibleConfigurationMismatch*/ false))
4065 case DIAGNOSTIC_OPTIONS:
4066 if (ParseDiagnosticOptions(Record, false, Listener))
4070 case FILE_SYSTEM_OPTIONS:
4071 if (ParseFileSystemOptions(Record, false, Listener))
4075 case HEADER_SEARCH_OPTIONS:
4076 if (ParseHeaderSearchOptions(Record, false, Listener))
4080 case PREPROCESSOR_OPTIONS: {
4081 std::string IgnoredSuggestedPredefines;
4082 if (ParsePreprocessorOptions(Record, false, Listener,
4083 IgnoredSuggestedPredefines))
4088 case INPUT_FILE_OFFSETS: {
4089 if (!NeedsInputFiles)
4092 unsigned NumInputFiles = Record[0];
4093 unsigned NumUserFiles = Record[1];
4094 const uint64_t *InputFileOffs = (const uint64_t *)Blob.data();
4095 for (unsigned I = 0; I != NumInputFiles; ++I) {
4096 // Go find this input file.
4097 bool isSystemFile = I >= NumUserFiles;
4099 if (isSystemFile && !NeedsSystemInputFiles)
4100 break; // the rest are system input files
4102 BitstreamCursor &Cursor = InputFilesCursor;
4103 SavedStreamPosition SavedPosition(Cursor);
4104 Cursor.JumpToBit(InputFileOffs[I]);
4106 unsigned Code = Cursor.ReadCode();
4109 bool shouldContinue = false;
4110 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
4112 bool Overridden = static_cast<bool>(Record[3]);
4113 std::string Filename = Blob;
4114 ResolveImportedPath(Filename, ModuleDir);
4116 Listener.visitInputFile(Filename, isSystemFile, Overridden);
4119 if (!shouldContinue)
4129 unsigned Idx = 0, N = Record.size();
4131 // Read information about the AST file.
4132 Idx += 5; // ImportLoc, Size, ModTime, Signature
4133 std::string Filename = ReadString(Record, Idx);
4134 ResolveImportedPath(Filename, ModuleDir);
4135 Listener.visitImport(Filename);
4140 case KNOWN_MODULE_FILES: {
4141 // Known-but-not-technically-used module files are treated as imports.
4145 unsigned Idx = 0, N = Record.size();
4147 std::string Filename = ReadString(Record, Idx);
4148 ResolveImportedPath(Filename, ModuleDir);
4149 Listener.visitImport(Filename);
4155 // No other validation to perform.
4161 bool ASTReader::isAcceptableASTFile(
4162 StringRef Filename, FileManager &FileMgr,
4163 const PCHContainerReader &PCHContainerRdr, const LangOptions &LangOpts,
4164 const TargetOptions &TargetOpts, const PreprocessorOptions &PPOpts,
4165 std::string ExistingModuleCachePath) {
4166 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
4167 ExistingModuleCachePath, FileMgr);
4168 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
4172 ASTReader::ASTReadResult
4173 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
4174 // Enter the submodule block.
4175 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
4176 Error("malformed submodule block record in AST file");
4180 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
4182 Module *CurrentModule = nullptr;
4185 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
4187 switch (Entry.Kind) {
4188 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
4189 case llvm::BitstreamEntry::Error:
4190 Error("malformed block record in AST file");
4192 case llvm::BitstreamEntry::EndBlock:
4194 case llvm::BitstreamEntry::Record:
4195 // The interesting case.
4202 auto Kind = F.Stream.readRecord(Entry.ID, Record, &Blob);
4204 if ((Kind == SUBMODULE_METADATA) != First) {
4205 Error("submodule metadata record should be at beginning of block");
4210 // Submodule information is only valid if we have a current module.
4211 // FIXME: Should we error on these cases?
4212 if (!CurrentModule && Kind != SUBMODULE_METADATA &&
4213 Kind != SUBMODULE_DEFINITION)
4217 default: // Default behavior: ignore.
4220 case SUBMODULE_DEFINITION: {
4221 if (Record.size() < 8) {
4222 Error("malformed module definition");
4226 StringRef Name = Blob;
4228 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
4229 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
4230 bool IsFramework = Record[Idx++];
4231 bool IsExplicit = Record[Idx++];
4232 bool IsSystem = Record[Idx++];
4233 bool IsExternC = Record[Idx++];
4234 bool InferSubmodules = Record[Idx++];
4235 bool InferExplicitSubmodules = Record[Idx++];
4236 bool InferExportWildcard = Record[Idx++];
4237 bool ConfigMacrosExhaustive = Record[Idx++];
4239 Module *ParentModule = nullptr;
4241 ParentModule = getSubmodule(Parent);
4243 // Retrieve this (sub)module from the module map, creating it if
4245 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule, IsFramework,
4248 // FIXME: set the definition loc for CurrentModule, or call
4249 // ModMap.setInferredModuleAllowedBy()
4251 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
4252 if (GlobalIndex >= SubmodulesLoaded.size() ||
4253 SubmodulesLoaded[GlobalIndex]) {
4254 Error("too many submodules");
4258 if (!ParentModule) {
4259 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
4260 if (CurFile != F.File) {
4261 if (!Diags.isDiagnosticInFlight()) {
4262 Diag(diag::err_module_file_conflict)
4263 << CurrentModule->getTopLevelModuleName()
4264 << CurFile->getName()
4265 << F.File->getName();
4271 CurrentModule->setASTFile(F.File);
4274 CurrentModule->Signature = F.Signature;
4275 CurrentModule->IsFromModuleFile = true;
4276 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
4277 CurrentModule->IsExternC = IsExternC;
4278 CurrentModule->InferSubmodules = InferSubmodules;
4279 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
4280 CurrentModule->InferExportWildcard = InferExportWildcard;
4281 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
4282 if (DeserializationListener)
4283 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
4285 SubmodulesLoaded[GlobalIndex] = CurrentModule;
4287 // Clear out data that will be replaced by what is the module file.
4288 CurrentModule->LinkLibraries.clear();
4289 CurrentModule->ConfigMacros.clear();
4290 CurrentModule->UnresolvedConflicts.clear();
4291 CurrentModule->Conflicts.clear();
4295 case SUBMODULE_UMBRELLA_HEADER: {
4296 std::string Filename = Blob;
4297 ResolveImportedPath(F, Filename);
4298 if (auto *Umbrella = PP.getFileManager().getFile(Filename)) {
4299 if (!CurrentModule->getUmbrellaHeader())
4300 ModMap.setUmbrellaHeader(CurrentModule, Umbrella, Blob);
4301 else if (CurrentModule->getUmbrellaHeader().Entry != Umbrella) {
4302 // This can be a spurious difference caused by changing the VFS to
4303 // point to a different copy of the file, and it is too late to
4304 // to rebuild safely.
4305 // FIXME: If we wrote the virtual paths instead of the 'real' paths,
4306 // after input file validation only real problems would remain and we
4307 // could just error. For now, assume it's okay.
4314 case SUBMODULE_HEADER:
4315 case SUBMODULE_EXCLUDED_HEADER:
4316 case SUBMODULE_PRIVATE_HEADER:
4317 // We lazily associate headers with their modules via the HeaderInfo table.
4318 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4319 // of complete filenames or remove it entirely.
4322 case SUBMODULE_TEXTUAL_HEADER:
4323 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
4324 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
4328 case SUBMODULE_TOPHEADER: {
4329 CurrentModule->addTopHeaderFilename(Blob);
4333 case SUBMODULE_UMBRELLA_DIR: {
4334 std::string Dirname = Blob;
4335 ResolveImportedPath(F, Dirname);
4336 if (auto *Umbrella = PP.getFileManager().getDirectory(Dirname)) {
4337 if (!CurrentModule->getUmbrellaDir())
4338 ModMap.setUmbrellaDir(CurrentModule, Umbrella, Blob);
4339 else if (CurrentModule->getUmbrellaDir().Entry != Umbrella) {
4340 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
4341 Error("mismatched umbrella directories in submodule");
4348 case SUBMODULE_METADATA: {
4349 F.BaseSubmoduleID = getTotalNumSubmodules();
4350 F.LocalNumSubmodules = Record[0];
4351 unsigned LocalBaseSubmoduleID = Record[1];
4352 if (F.LocalNumSubmodules > 0) {
4353 // Introduce the global -> local mapping for submodules within this
4355 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
4357 // Introduce the local -> global mapping for submodules within this
4359 F.SubmoduleRemap.insertOrReplace(
4360 std::make_pair(LocalBaseSubmoduleID,
4361 F.BaseSubmoduleID - LocalBaseSubmoduleID));
4363 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
4368 case SUBMODULE_IMPORTS: {
4369 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
4370 UnresolvedModuleRef Unresolved;
4371 Unresolved.File = &F;
4372 Unresolved.Mod = CurrentModule;
4373 Unresolved.ID = Record[Idx];
4374 Unresolved.Kind = UnresolvedModuleRef::Import;
4375 Unresolved.IsWildcard = false;
4376 UnresolvedModuleRefs.push_back(Unresolved);
4381 case SUBMODULE_EXPORTS: {
4382 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
4383 UnresolvedModuleRef Unresolved;
4384 Unresolved.File = &F;
4385 Unresolved.Mod = CurrentModule;
4386 Unresolved.ID = Record[Idx];
4387 Unresolved.Kind = UnresolvedModuleRef::Export;
4388 Unresolved.IsWildcard = Record[Idx + 1];
4389 UnresolvedModuleRefs.push_back(Unresolved);
4392 // Once we've loaded the set of exports, there's no reason to keep
4393 // the parsed, unresolved exports around.
4394 CurrentModule->UnresolvedExports.clear();
4397 case SUBMODULE_REQUIRES: {
4398 CurrentModule->addRequirement(Blob, Record[0], Context.getLangOpts(),
4399 Context.getTargetInfo());
4403 case SUBMODULE_LINK_LIBRARY:
4404 CurrentModule->LinkLibraries.push_back(
4405 Module::LinkLibrary(Blob, Record[0]));
4408 case SUBMODULE_CONFIG_MACRO:
4409 CurrentModule->ConfigMacros.push_back(Blob.str());
4412 case SUBMODULE_CONFLICT: {
4413 UnresolvedModuleRef Unresolved;
4414 Unresolved.File = &F;
4415 Unresolved.Mod = CurrentModule;
4416 Unresolved.ID = Record[0];
4417 Unresolved.Kind = UnresolvedModuleRef::Conflict;
4418 Unresolved.IsWildcard = false;
4419 Unresolved.String = Blob;
4420 UnresolvedModuleRefs.push_back(Unresolved);
4427 /// \brief Parse the record that corresponds to a LangOptions data
4430 /// This routine parses the language options from the AST file and then gives
4431 /// them to the AST listener if one is set.
4433 /// \returns true if the listener deems the file unacceptable, false otherwise.
4434 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
4436 ASTReaderListener &Listener,
4437 bool AllowCompatibleDifferences) {
4438 LangOptions LangOpts;
4440 #define LANGOPT(Name, Bits, Default, Description) \
4441 LangOpts.Name = Record[Idx++];
4442 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
4443 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
4444 #include "clang/Basic/LangOptions.def"
4445 #define SANITIZER(NAME, ID) \
4446 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
4447 #include "clang/Basic/Sanitizers.def"
4449 for (unsigned N = Record[Idx++]; N; --N)
4450 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
4452 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
4453 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
4454 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
4456 LangOpts.CurrentModule = ReadString(Record, Idx);
4459 for (unsigned N = Record[Idx++]; N; --N) {
4460 LangOpts.CommentOpts.BlockCommandNames.push_back(
4461 ReadString(Record, Idx));
4463 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
4465 return Listener.ReadLanguageOptions(LangOpts, Complain,
4466 AllowCompatibleDifferences);
4469 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
4470 ASTReaderListener &Listener,
4471 bool AllowCompatibleDifferences) {
4473 TargetOptions TargetOpts;
4474 TargetOpts.Triple = ReadString(Record, Idx);
4475 TargetOpts.CPU = ReadString(Record, Idx);
4476 TargetOpts.ABI = ReadString(Record, Idx);
4477 for (unsigned N = Record[Idx++]; N; --N) {
4478 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
4480 for (unsigned N = Record[Idx++]; N; --N) {
4481 TargetOpts.Features.push_back(ReadString(Record, Idx));
4484 return Listener.ReadTargetOptions(TargetOpts, Complain,
4485 AllowCompatibleDifferences);
4488 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
4489 ASTReaderListener &Listener) {
4490 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
4492 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
4493 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
4494 DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
4495 #include "clang/Basic/DiagnosticOptions.def"
4497 for (unsigned N = Record[Idx++]; N; --N)
4498 DiagOpts->Warnings.push_back(ReadString(Record, Idx));
4499 for (unsigned N = Record[Idx++]; N; --N)
4500 DiagOpts->Remarks.push_back(ReadString(Record, Idx));
4502 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
4505 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
4506 ASTReaderListener &Listener) {
4507 FileSystemOptions FSOpts;
4509 FSOpts.WorkingDir = ReadString(Record, Idx);
4510 return Listener.ReadFileSystemOptions(FSOpts, Complain);
4513 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
4515 ASTReaderListener &Listener) {
4516 HeaderSearchOptions HSOpts;
4518 HSOpts.Sysroot = ReadString(Record, Idx);
4521 for (unsigned N = Record[Idx++]; N; --N) {
4522 std::string Path = ReadString(Record, Idx);
4523 frontend::IncludeDirGroup Group
4524 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
4525 bool IsFramework = Record[Idx++];
4526 bool IgnoreSysRoot = Record[Idx++];
4527 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
4531 // System header prefixes.
4532 for (unsigned N = Record[Idx++]; N; --N) {
4533 std::string Prefix = ReadString(Record, Idx);
4534 bool IsSystemHeader = Record[Idx++];
4535 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
4538 HSOpts.ResourceDir = ReadString(Record, Idx);
4539 HSOpts.ModuleCachePath = ReadString(Record, Idx);
4540 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
4541 HSOpts.DisableModuleHash = Record[Idx++];
4542 HSOpts.UseBuiltinIncludes = Record[Idx++];
4543 HSOpts.UseStandardSystemIncludes = Record[Idx++];
4544 HSOpts.UseStandardCXXIncludes = Record[Idx++];
4545 HSOpts.UseLibcxx = Record[Idx++];
4546 std::string SpecificModuleCachePath = ReadString(Record, Idx);
4548 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
4552 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
4554 ASTReaderListener &Listener,
4555 std::string &SuggestedPredefines) {
4556 PreprocessorOptions PPOpts;
4559 // Macro definitions/undefs
4560 for (unsigned N = Record[Idx++]; N; --N) {
4561 std::string Macro = ReadString(Record, Idx);
4562 bool IsUndef = Record[Idx++];
4563 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
4567 for (unsigned N = Record[Idx++]; N; --N) {
4568 PPOpts.Includes.push_back(ReadString(Record, Idx));
4572 for (unsigned N = Record[Idx++]; N; --N) {
4573 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
4576 PPOpts.UsePredefines = Record[Idx++];
4577 PPOpts.DetailedRecord = Record[Idx++];
4578 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
4579 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
4580 PPOpts.ObjCXXARCStandardLibrary =
4581 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
4582 SuggestedPredefines.clear();
4583 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
4584 SuggestedPredefines);
4587 std::pair<ModuleFile *, unsigned>
4588 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
4589 GlobalPreprocessedEntityMapType::iterator
4590 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
4591 assert(I != GlobalPreprocessedEntityMap.end() &&
4592 "Corrupted global preprocessed entity map");
4593 ModuleFile *M = I->second;
4594 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
4595 return std::make_pair(M, LocalIndex);
4598 llvm::iterator_range<PreprocessingRecord::iterator>
4599 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
4600 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
4601 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
4602 Mod.NumPreprocessedEntities);
4604 return llvm::make_range(PreprocessingRecord::iterator(),
4605 PreprocessingRecord::iterator());
4608 llvm::iterator_range<ASTReader::ModuleDeclIterator>
4609 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
4610 return llvm::make_range(
4611 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4612 ModuleDeclIterator(this, &Mod,
4613 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
4616 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4617 PreprocessedEntityID PPID = Index+1;
4618 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4619 ModuleFile &M = *PPInfo.first;
4620 unsigned LocalIndex = PPInfo.second;
4621 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4623 if (!PP.getPreprocessingRecord()) {
4624 Error("no preprocessing record");
4628 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
4629 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
4631 llvm::BitstreamEntry Entry =
4632 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
4633 if (Entry.Kind != llvm::BitstreamEntry::Record)
4637 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
4638 ReadSourceLocation(M, PPOffs.End));
4639 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
4642 PreprocessorDetailRecordTypes RecType =
4643 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4644 Entry.ID, Record, &Blob);
4646 case PPD_MACRO_EXPANSION: {
4647 bool isBuiltin = Record[0];
4648 IdentifierInfo *Name = nullptr;
4649 MacroDefinitionRecord *Def = nullptr;
4651 Name = getLocalIdentifier(M, Record[1]);
4653 PreprocessedEntityID GlobalID =
4654 getGlobalPreprocessedEntityID(M, Record[1]);
4655 Def = cast<MacroDefinitionRecord>(
4656 PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
4661 ME = new (PPRec) MacroExpansion(Name, Range);
4663 ME = new (PPRec) MacroExpansion(Def, Range);
4668 case PPD_MACRO_DEFINITION: {
4669 // Decode the identifier info and then check again; if the macro is
4670 // still defined and associated with the identifier,
4671 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
4672 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
4674 if (DeserializationListener)
4675 DeserializationListener->MacroDefinitionRead(PPID, MD);
4680 case PPD_INCLUSION_DIRECTIVE: {
4681 const char *FullFileNameStart = Blob.data() + Record[0];
4682 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
4683 const FileEntry *File = nullptr;
4684 if (!FullFileName.empty())
4685 File = PP.getFileManager().getFile(FullFileName);
4687 // FIXME: Stable encoding
4688 InclusionDirective::InclusionKind Kind
4689 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4690 InclusionDirective *ID
4691 = new (PPRec) InclusionDirective(PPRec, Kind,
4692 StringRef(Blob.data(), Record[0]),
4693 Record[1], Record[3],
4700 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4703 /// \brief \arg SLocMapI points at a chunk of a module that contains no
4704 /// preprocessed entities or the entities it contains are not the ones we are
4705 /// looking for. Find the next module that contains entities and return the ID
4706 /// of the first entry.
4707 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4708 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4710 for (GlobalSLocOffsetMapType::const_iterator
4711 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4712 ModuleFile &M = *SLocMapI->second;
4713 if (M.NumPreprocessedEntities)
4714 return M.BasePreprocessedEntityID;
4717 return getTotalNumPreprocessedEntities();
4722 template <unsigned PPEntityOffset::*PPLoc>
4723 struct PPEntityComp {
4724 const ASTReader &Reader;
4727 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
4729 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4730 SourceLocation LHS = getLoc(L);
4731 SourceLocation RHS = getLoc(R);
4732 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4735 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
4736 SourceLocation LHS = getLoc(L);
4737 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4740 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
4741 SourceLocation RHS = getLoc(R);
4742 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4745 SourceLocation getLoc(const PPEntityOffset &PPE) const {
4746 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
4752 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
4753 bool EndsAfter) const {
4754 if (SourceMgr.isLocalSourceLocation(Loc))
4755 return getTotalNumPreprocessedEntities();
4757 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
4758 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
4759 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4760 "Corrupted global sloc offset map");
4762 if (SLocMapI->second->NumPreprocessedEntities == 0)
4763 return findNextPreprocessedEntity(SLocMapI);
4765 ModuleFile &M = *SLocMapI->second;
4766 typedef const PPEntityOffset *pp_iterator;
4767 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4768 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4770 size_t Count = M.NumPreprocessedEntities;
4772 pp_iterator First = pp_begin;
4776 PPI = std::upper_bound(pp_begin, pp_end, Loc,
4777 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
4779 // Do a binary search manually instead of using std::lower_bound because
4780 // The end locations of entities may be unordered (when a macro expansion
4781 // is inside another macro argument), but for this case it is not important
4782 // whether we get the first macro expansion or its containing macro.
4786 std::advance(PPI, Half);
4787 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
4791 Count = Count - Half - 1;
4798 return findNextPreprocessedEntity(SLocMapI);
4800 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4803 /// \brief Returns a pair of [Begin, End) indices of preallocated
4804 /// preprocessed entities that \arg Range encompasses.
4805 std::pair<unsigned, unsigned>
4806 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
4807 if (Range.isInvalid())
4808 return std::make_pair(0,0);
4809 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
4811 PreprocessedEntityID BeginID =
4812 findPreprocessedEntity(Range.getBegin(), false);
4813 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
4814 return std::make_pair(BeginID, EndID);
4817 /// \brief Optionally returns true or false if the preallocated preprocessed
4818 /// entity with index \arg Index came from file \arg FID.
4819 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
4821 if (FID.isInvalid())
4824 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4825 ModuleFile &M = *PPInfo.first;
4826 unsigned LocalIndex = PPInfo.second;
4827 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4829 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
4830 if (Loc.isInvalid())
4833 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
4840 /// \brief Visitor used to search for information about a header file.
4841 class HeaderFileInfoVisitor {
4842 const FileEntry *FE;
4844 Optional<HeaderFileInfo> HFI;
4847 explicit HeaderFileInfoVisitor(const FileEntry *FE)
4850 static bool visit(ModuleFile &M, void *UserData) {
4851 HeaderFileInfoVisitor *This
4852 = static_cast<HeaderFileInfoVisitor *>(UserData);
4854 HeaderFileInfoLookupTable *Table
4855 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4859 // Look in the on-disk hash table for an entry for this file name.
4860 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE);
4861 if (Pos == Table->end())
4868 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
4872 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
4873 HeaderFileInfoVisitor Visitor(FE);
4874 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
4875 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
4878 return HeaderFileInfo();
4881 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
4882 // FIXME: Make it work properly with modules.
4883 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
4884 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
4885 ModuleFile &F = *(*I);
4888 assert(!Diag.DiagStates.empty());
4889 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
4890 while (Idx < F.PragmaDiagMappings.size()) {
4891 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
4892 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
4893 if (DiagStateID != 0) {
4894 Diag.DiagStatePoints.push_back(
4895 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
4896 FullSourceLoc(Loc, SourceMgr)));
4900 assert(DiagStateID == 0);
4901 // A new DiagState was created here.
4902 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
4903 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
4904 DiagStates.push_back(NewState);
4905 Diag.DiagStatePoints.push_back(
4906 DiagnosticsEngine::DiagStatePoint(NewState,
4907 FullSourceLoc(Loc, SourceMgr)));
4909 assert(Idx < F.PragmaDiagMappings.size() &&
4910 "Invalid data, didn't find '-1' marking end of diag/map pairs");
4911 if (Idx >= F.PragmaDiagMappings.size()) {
4912 break; // Something is messed up but at least avoid infinite loop in
4915 unsigned DiagID = F.PragmaDiagMappings[Idx++];
4916 if (DiagID == (unsigned)-1) {
4917 break; // no more diag/map pairs for this location.
4919 diag::Severity Map = (diag::Severity)F.PragmaDiagMappings[Idx++];
4920 DiagnosticMapping Mapping = Diag.makeUserMapping(Map, Loc);
4921 Diag.GetCurDiagState()->setMapping(DiagID, Mapping);
4927 /// \brief Get the correct cursor and offset for loading a type.
4928 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
4929 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
4930 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
4931 ModuleFile *M = I->second;
4932 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
4935 /// \brief Read and return the type with the given index..
4937 /// The index is the type ID, shifted and minus the number of predefs. This
4938 /// routine actually reads the record corresponding to the type at the given
4939 /// location. It is a helper routine for GetType, which deals with reading type
4941 QualType ASTReader::readTypeRecord(unsigned Index) {
4942 RecordLocation Loc = TypeCursorForIndex(Index);
4943 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
4945 // Keep track of where we are in the stream, then jump back there
4946 // after reading this type.
4947 SavedStreamPosition SavedPosition(DeclsCursor);
4949 ReadingKindTracker ReadingKind(Read_Type, *this);
4951 // Note that we are loading a type record.
4952 Deserializing AType(this);
4955 DeclsCursor.JumpToBit(Loc.Offset);
4957 unsigned Code = DeclsCursor.ReadCode();
4958 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
4959 case TYPE_EXT_QUAL: {
4960 if (Record.size() != 2) {
4961 Error("Incorrect encoding of extended qualifier type");
4964 QualType Base = readType(*Loc.F, Record, Idx);
4965 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
4966 return Context.getQualifiedType(Base, Quals);
4969 case TYPE_COMPLEX: {
4970 if (Record.size() != 1) {
4971 Error("Incorrect encoding of complex type");
4974 QualType ElemType = readType(*Loc.F, Record, Idx);
4975 return Context.getComplexType(ElemType);
4978 case TYPE_POINTER: {
4979 if (Record.size() != 1) {
4980 Error("Incorrect encoding of pointer type");
4983 QualType PointeeType = readType(*Loc.F, Record, Idx);
4984 return Context.getPointerType(PointeeType);
4987 case TYPE_DECAYED: {
4988 if (Record.size() != 1) {
4989 Error("Incorrect encoding of decayed type");
4992 QualType OriginalType = readType(*Loc.F, Record, Idx);
4993 QualType DT = Context.getAdjustedParameterType(OriginalType);
4994 if (!isa<DecayedType>(DT))
4995 Error("Decayed type does not decay");
4999 case TYPE_ADJUSTED: {
5000 if (Record.size() != 2) {
5001 Error("Incorrect encoding of adjusted type");
5004 QualType OriginalTy = readType(*Loc.F, Record, Idx);
5005 QualType AdjustedTy = readType(*Loc.F, Record, Idx);
5006 return Context.getAdjustedType(OriginalTy, AdjustedTy);
5009 case TYPE_BLOCK_POINTER: {
5010 if (Record.size() != 1) {
5011 Error("Incorrect encoding of block pointer type");
5014 QualType PointeeType = readType(*Loc.F, Record, Idx);
5015 return Context.getBlockPointerType(PointeeType);
5018 case TYPE_LVALUE_REFERENCE: {
5019 if (Record.size() != 2) {
5020 Error("Incorrect encoding of lvalue reference type");
5023 QualType PointeeType = readType(*Loc.F, Record, Idx);
5024 return Context.getLValueReferenceType(PointeeType, Record[1]);
5027 case TYPE_RVALUE_REFERENCE: {
5028 if (Record.size() != 1) {
5029 Error("Incorrect encoding of rvalue reference type");
5032 QualType PointeeType = readType(*Loc.F, Record, Idx);
5033 return Context.getRValueReferenceType(PointeeType);
5036 case TYPE_MEMBER_POINTER: {
5037 if (Record.size() != 2) {
5038 Error("Incorrect encoding of member pointer type");
5041 QualType PointeeType = readType(*Loc.F, Record, Idx);
5042 QualType ClassType = readType(*Loc.F, Record, Idx);
5043 if (PointeeType.isNull() || ClassType.isNull())
5046 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
5049 case TYPE_CONSTANT_ARRAY: {
5050 QualType ElementType = readType(*Loc.F, Record, Idx);
5051 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5052 unsigned IndexTypeQuals = Record[2];
5054 llvm::APInt Size = ReadAPInt(Record, Idx);
5055 return Context.getConstantArrayType(ElementType, Size,
5056 ASM, IndexTypeQuals);
5059 case TYPE_INCOMPLETE_ARRAY: {
5060 QualType ElementType = readType(*Loc.F, Record, Idx);
5061 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5062 unsigned IndexTypeQuals = Record[2];
5063 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
5066 case TYPE_VARIABLE_ARRAY: {
5067 QualType ElementType = readType(*Loc.F, Record, Idx);
5068 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5069 unsigned IndexTypeQuals = Record[2];
5070 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
5071 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
5072 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
5073 ASM, IndexTypeQuals,
5074 SourceRange(LBLoc, RBLoc));
5078 if (Record.size() != 3) {
5079 Error("incorrect encoding of vector type in AST file");
5083 QualType ElementType = readType(*Loc.F, Record, Idx);
5084 unsigned NumElements = Record[1];
5085 unsigned VecKind = Record[2];
5086 return Context.getVectorType(ElementType, NumElements,
5087 (VectorType::VectorKind)VecKind);
5090 case TYPE_EXT_VECTOR: {
5091 if (Record.size() != 3) {
5092 Error("incorrect encoding of extended vector type in AST file");
5096 QualType ElementType = readType(*Loc.F, Record, Idx);
5097 unsigned NumElements = Record[1];
5098 return Context.getExtVectorType(ElementType, NumElements);
5101 case TYPE_FUNCTION_NO_PROTO: {
5102 if (Record.size() != 6) {
5103 Error("incorrect encoding of no-proto function type");
5106 QualType ResultType = readType(*Loc.F, Record, Idx);
5107 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
5108 (CallingConv)Record[4], Record[5]);
5109 return Context.getFunctionNoProtoType(ResultType, Info);
5112 case TYPE_FUNCTION_PROTO: {
5113 QualType ResultType = readType(*Loc.F, Record, Idx);
5115 FunctionProtoType::ExtProtoInfo EPI;
5116 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
5117 /*hasregparm*/ Record[2],
5118 /*regparm*/ Record[3],
5119 static_cast<CallingConv>(Record[4]),
5120 /*produces*/ Record[5]);
5124 EPI.Variadic = Record[Idx++];
5125 EPI.HasTrailingReturn = Record[Idx++];
5126 EPI.TypeQuals = Record[Idx++];
5127 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
5128 SmallVector<QualType, 8> ExceptionStorage;
5129 readExceptionSpec(*Loc.F, ExceptionStorage, EPI.ExceptionSpec, Record, Idx);
5131 unsigned NumParams = Record[Idx++];
5132 SmallVector<QualType, 16> ParamTypes;
5133 for (unsigned I = 0; I != NumParams; ++I)
5134 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
5136 return Context.getFunctionType(ResultType, ParamTypes, EPI);
5139 case TYPE_UNRESOLVED_USING: {
5141 return Context.getTypeDeclType(
5142 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
5145 case TYPE_TYPEDEF: {
5146 if (Record.size() != 2) {
5147 Error("incorrect encoding of typedef type");
5151 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
5152 QualType Canonical = readType(*Loc.F, Record, Idx);
5153 if (!Canonical.isNull())
5154 Canonical = Context.getCanonicalType(Canonical);
5155 return Context.getTypedefType(Decl, Canonical);
5158 case TYPE_TYPEOF_EXPR:
5159 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
5162 if (Record.size() != 1) {
5163 Error("incorrect encoding of typeof(type) in AST file");
5166 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5167 return Context.getTypeOfType(UnderlyingType);
5170 case TYPE_DECLTYPE: {
5171 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5172 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
5175 case TYPE_UNARY_TRANSFORM: {
5176 QualType BaseType = readType(*Loc.F, Record, Idx);
5177 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5178 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
5179 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
5183 QualType Deduced = readType(*Loc.F, Record, Idx);
5184 bool IsDecltypeAuto = Record[Idx++];
5185 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
5186 return Context.getAutoType(Deduced, IsDecltypeAuto, IsDependent);
5190 if (Record.size() != 2) {
5191 Error("incorrect encoding of record type");
5195 bool IsDependent = Record[Idx++];
5196 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
5197 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
5198 QualType T = Context.getRecordType(RD);
5199 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5204 if (Record.size() != 2) {
5205 Error("incorrect encoding of enum type");
5209 bool IsDependent = Record[Idx++];
5211 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
5212 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5216 case TYPE_ATTRIBUTED: {
5217 if (Record.size() != 3) {
5218 Error("incorrect encoding of attributed type");
5221 QualType modifiedType = readType(*Loc.F, Record, Idx);
5222 QualType equivalentType = readType(*Loc.F, Record, Idx);
5223 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
5224 return Context.getAttributedType(kind, modifiedType, equivalentType);
5228 if (Record.size() != 1) {
5229 Error("incorrect encoding of paren type");
5232 QualType InnerType = readType(*Loc.F, Record, Idx);
5233 return Context.getParenType(InnerType);
5236 case TYPE_PACK_EXPANSION: {
5237 if (Record.size() != 2) {
5238 Error("incorrect encoding of pack expansion type");
5241 QualType Pattern = readType(*Loc.F, Record, Idx);
5242 if (Pattern.isNull())
5244 Optional<unsigned> NumExpansions;
5246 NumExpansions = Record[1] - 1;
5247 return Context.getPackExpansionType(Pattern, NumExpansions);
5250 case TYPE_ELABORATED: {
5252 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5253 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5254 QualType NamedType = readType(*Loc.F, Record, Idx);
5255 return Context.getElaboratedType(Keyword, NNS, NamedType);
5258 case TYPE_OBJC_INTERFACE: {
5260 ObjCInterfaceDecl *ItfD
5261 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
5262 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
5265 case TYPE_OBJC_OBJECT: {
5267 QualType Base = readType(*Loc.F, Record, Idx);
5268 unsigned NumTypeArgs = Record[Idx++];
5269 SmallVector<QualType, 4> TypeArgs;
5270 for (unsigned I = 0; I != NumTypeArgs; ++I)
5271 TypeArgs.push_back(readType(*Loc.F, Record, Idx));
5272 unsigned NumProtos = Record[Idx++];
5273 SmallVector<ObjCProtocolDecl*, 4> Protos;
5274 for (unsigned I = 0; I != NumProtos; ++I)
5275 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
5276 bool IsKindOf = Record[Idx++];
5277 return Context.getObjCObjectType(Base, TypeArgs, Protos, IsKindOf);
5280 case TYPE_OBJC_OBJECT_POINTER: {
5282 QualType Pointee = readType(*Loc.F, Record, Idx);
5283 return Context.getObjCObjectPointerType(Pointee);
5286 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
5288 QualType Parm = readType(*Loc.F, Record, Idx);
5289 QualType Replacement = readType(*Loc.F, Record, Idx);
5290 return Context.getSubstTemplateTypeParmType(
5291 cast<TemplateTypeParmType>(Parm),
5292 Context.getCanonicalType(Replacement));
5295 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
5297 QualType Parm = readType(*Loc.F, Record, Idx);
5298 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
5299 return Context.getSubstTemplateTypeParmPackType(
5300 cast<TemplateTypeParmType>(Parm),
5304 case TYPE_INJECTED_CLASS_NAME: {
5305 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
5306 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
5307 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
5308 // for AST reading, too much interdependencies.
5309 const Type *T = nullptr;
5310 for (auto *DI = D; DI; DI = DI->getPreviousDecl()) {
5311 if (const Type *Existing = DI->getTypeForDecl()) {
5317 T = new (Context, TypeAlignment) InjectedClassNameType(D, TST);
5318 for (auto *DI = D; DI; DI = DI->getPreviousDecl())
5319 DI->setTypeForDecl(T);
5321 return QualType(T, 0);
5324 case TYPE_TEMPLATE_TYPE_PARM: {
5326 unsigned Depth = Record[Idx++];
5327 unsigned Index = Record[Idx++];
5328 bool Pack = Record[Idx++];
5329 TemplateTypeParmDecl *D
5330 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
5331 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
5334 case TYPE_DEPENDENT_NAME: {
5336 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5337 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5338 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
5339 QualType Canon = readType(*Loc.F, Record, Idx);
5340 if (!Canon.isNull())
5341 Canon = Context.getCanonicalType(Canon);
5342 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
5345 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
5347 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5348 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5349 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
5350 unsigned NumArgs = Record[Idx++];
5351 SmallVector<TemplateArgument, 8> Args;
5352 Args.reserve(NumArgs);
5354 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
5355 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
5356 Args.size(), Args.data());
5359 case TYPE_DEPENDENT_SIZED_ARRAY: {
5363 QualType ElementType = readType(*Loc.F, Record, Idx);
5364 ArrayType::ArraySizeModifier ASM
5365 = (ArrayType::ArraySizeModifier)Record[Idx++];
5366 unsigned IndexTypeQuals = Record[Idx++];
5368 // DependentSizedArrayType
5369 Expr *NumElts = ReadExpr(*Loc.F);
5370 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
5372 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
5373 IndexTypeQuals, Brackets);
5376 case TYPE_TEMPLATE_SPECIALIZATION: {
5378 bool IsDependent = Record[Idx++];
5379 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
5380 SmallVector<TemplateArgument, 8> Args;
5381 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
5382 QualType Underlying = readType(*Loc.F, Record, Idx);
5384 if (Underlying.isNull())
5385 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
5388 T = Context.getTemplateSpecializationType(Name, Args.data(),
5389 Args.size(), Underlying);
5390 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5395 if (Record.size() != 1) {
5396 Error("Incorrect encoding of atomic type");
5399 QualType ValueType = readType(*Loc.F, Record, Idx);
5400 return Context.getAtomicType(ValueType);
5403 llvm_unreachable("Invalid TypeCode!");
5406 void ASTReader::readExceptionSpec(ModuleFile &ModuleFile,
5407 SmallVectorImpl<QualType> &Exceptions,
5408 FunctionProtoType::ExceptionSpecInfo &ESI,
5409 const RecordData &Record, unsigned &Idx) {
5410 ExceptionSpecificationType EST =
5411 static_cast<ExceptionSpecificationType>(Record[Idx++]);
5413 if (EST == EST_Dynamic) {
5414 for (unsigned I = 0, N = Record[Idx++]; I != N; ++I)
5415 Exceptions.push_back(readType(ModuleFile, Record, Idx));
5416 ESI.Exceptions = Exceptions;
5417 } else if (EST == EST_ComputedNoexcept) {
5418 ESI.NoexceptExpr = ReadExpr(ModuleFile);
5419 } else if (EST == EST_Uninstantiated) {
5420 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5421 ESI.SourceTemplate = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5422 } else if (EST == EST_Unevaluated) {
5423 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5427 class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
5430 const ASTReader::RecordData &Record;
5433 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
5435 return Reader.ReadSourceLocation(F, R, I);
5438 template<typename T>
5439 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
5440 return Reader.ReadDeclAs<T>(F, Record, Idx);
5444 TypeLocReader(ASTReader &Reader, ModuleFile &F,
5445 const ASTReader::RecordData &Record, unsigned &Idx)
5446 : Reader(Reader), F(F), Record(Record), Idx(Idx)
5449 // We want compile-time assurance that we've enumerated all of
5450 // these, so unfortunately we have to declare them first, then
5451 // define them out-of-line.
5452 #define ABSTRACT_TYPELOC(CLASS, PARENT)
5453 #define TYPELOC(CLASS, PARENT) \
5454 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
5455 #include "clang/AST/TypeLocNodes.def"
5457 void VisitFunctionTypeLoc(FunctionTypeLoc);
5458 void VisitArrayTypeLoc(ArrayTypeLoc);
5461 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
5464 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
5465 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
5466 if (TL.needsExtraLocalData()) {
5467 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
5468 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
5469 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
5470 TL.setModeAttr(Record[Idx++]);
5473 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
5474 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5476 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
5477 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5479 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
5482 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
5485 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
5486 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
5488 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
5489 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
5491 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
5492 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
5494 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
5495 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5496 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5498 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
5499 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
5500 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
5502 TL.setSizeExpr(Reader.ReadExpr(F));
5504 TL.setSizeExpr(nullptr);
5506 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
5507 VisitArrayTypeLoc(TL);
5509 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
5510 VisitArrayTypeLoc(TL);
5512 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
5513 VisitArrayTypeLoc(TL);
5515 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
5516 DependentSizedArrayTypeLoc TL) {
5517 VisitArrayTypeLoc(TL);
5519 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
5520 DependentSizedExtVectorTypeLoc TL) {
5521 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5523 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
5524 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5526 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
5527 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5529 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
5530 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
5531 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5532 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5533 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
5534 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
5535 TL.setParam(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
5538 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
5539 VisitFunctionTypeLoc(TL);
5541 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
5542 VisitFunctionTypeLoc(TL);
5544 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
5545 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5547 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
5548 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5550 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
5551 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5552 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5553 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5555 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
5556 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5557 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5558 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5559 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5561 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
5562 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5564 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
5565 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5566 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5567 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5568 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5570 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
5571 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5573 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
5574 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5576 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
5577 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5579 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
5580 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
5581 if (TL.hasAttrOperand()) {
5583 range.setBegin(ReadSourceLocation(Record, Idx));
5584 range.setEnd(ReadSourceLocation(Record, Idx));
5585 TL.setAttrOperandParensRange(range);
5587 if (TL.hasAttrExprOperand()) {
5589 TL.setAttrExprOperand(Reader.ReadExpr(F));
5591 TL.setAttrExprOperand(nullptr);
5592 } else if (TL.hasAttrEnumOperand())
5593 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
5595 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
5596 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5598 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
5599 SubstTemplateTypeParmTypeLoc TL) {
5600 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5602 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
5603 SubstTemplateTypeParmPackTypeLoc TL) {
5604 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5606 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
5607 TemplateSpecializationTypeLoc TL) {
5608 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5609 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5610 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5611 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5612 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
5614 Reader.GetTemplateArgumentLocInfo(F,
5615 TL.getTypePtr()->getArg(i).getKind(),
5618 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
5619 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5620 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5622 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
5623 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5624 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5626 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
5627 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5629 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
5630 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5631 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5632 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5634 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5635 DependentTemplateSpecializationTypeLoc TL) {
5636 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5637 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5638 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5639 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5640 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5641 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5642 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5644 Reader.GetTemplateArgumentLocInfo(F,
5645 TL.getTypePtr()->getArg(I).getKind(),
5648 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5649 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5651 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
5652 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5654 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5655 TL.setHasBaseTypeAsWritten(Record[Idx++]);
5656 TL.setTypeArgsLAngleLoc(ReadSourceLocation(Record, Idx));
5657 TL.setTypeArgsRAngleLoc(ReadSourceLocation(Record, Idx));
5658 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
5659 TL.setTypeArgTInfo(i, Reader.GetTypeSourceInfo(F, Record, Idx));
5660 TL.setProtocolLAngleLoc(ReadSourceLocation(Record, Idx));
5661 TL.setProtocolRAngleLoc(ReadSourceLocation(Record, Idx));
5662 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
5663 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
5665 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
5666 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5668 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5669 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5670 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5671 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5674 TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5675 const RecordData &Record,
5677 QualType InfoTy = readType(F, Record, Idx);
5678 if (InfoTy.isNull())
5681 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5682 TypeLocReader TLR(*this, F, Record, Idx);
5683 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5688 QualType ASTReader::GetType(TypeID ID) {
5689 unsigned FastQuals = ID & Qualifiers::FastMask;
5690 unsigned Index = ID >> Qualifiers::FastWidth;
5692 if (Index < NUM_PREDEF_TYPE_IDS) {
5694 switch ((PredefinedTypeIDs)Index) {
5695 case PREDEF_TYPE_NULL_ID: return QualType();
5696 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
5697 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
5699 case PREDEF_TYPE_CHAR_U_ID:
5700 case PREDEF_TYPE_CHAR_S_ID:
5701 // FIXME: Check that the signedness of CharTy is correct!
5705 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
5706 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
5707 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
5708 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
5709 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
5710 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
5711 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
5712 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
5713 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
5714 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
5715 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
5716 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
5717 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
5718 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
5719 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
5720 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
5721 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
5722 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
5723 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
5724 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
5725 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
5726 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
5727 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
5728 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
5729 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
5730 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
5731 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
5732 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
5733 case PREDEF_TYPE_IMAGE1D_ID: T = Context.OCLImage1dTy; break;
5734 case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break;
5735 case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break;
5736 case PREDEF_TYPE_IMAGE2D_ID: T = Context.OCLImage2dTy; break;
5737 case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break;
5738 case PREDEF_TYPE_IMAGE3D_ID: T = Context.OCLImage3dTy; break;
5739 case PREDEF_TYPE_SAMPLER_ID: T = Context.OCLSamplerTy; break;
5740 case PREDEF_TYPE_EVENT_ID: T = Context.OCLEventTy; break;
5741 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
5743 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
5744 T = Context.getAutoRRefDeductType();
5747 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
5748 T = Context.ARCUnbridgedCastTy;
5751 case PREDEF_TYPE_VA_LIST_TAG:
5752 T = Context.getVaListTagType();
5755 case PREDEF_TYPE_BUILTIN_FN:
5756 T = Context.BuiltinFnTy;
5760 assert(!T.isNull() && "Unknown predefined type");
5761 return T.withFastQualifiers(FastQuals);
5764 Index -= NUM_PREDEF_TYPE_IDS;
5765 assert(Index < TypesLoaded.size() && "Type index out-of-range");
5766 if (TypesLoaded[Index].isNull()) {
5767 TypesLoaded[Index] = readTypeRecord(Index);
5768 if (TypesLoaded[Index].isNull())
5771 TypesLoaded[Index]->setFromAST();
5772 if (DeserializationListener)
5773 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
5774 TypesLoaded[Index]);
5777 return TypesLoaded[Index].withFastQualifiers(FastQuals);
5780 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
5781 return GetType(getGlobalTypeID(F, LocalID));
5784 serialization::TypeID
5785 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
5786 unsigned FastQuals = LocalID & Qualifiers::FastMask;
5787 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
5789 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
5792 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5793 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
5794 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
5796 unsigned GlobalIndex = LocalIndex + I->second;
5797 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
5800 TemplateArgumentLocInfo
5801 ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
5802 TemplateArgument::ArgKind Kind,
5803 const RecordData &Record,
5806 case TemplateArgument::Expression:
5808 case TemplateArgument::Type:
5809 return GetTypeSourceInfo(F, Record, Index);
5810 case TemplateArgument::Template: {
5811 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5813 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5814 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5817 case TemplateArgument::TemplateExpansion: {
5818 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5820 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5821 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
5822 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5825 case TemplateArgument::Null:
5826 case TemplateArgument::Integral:
5827 case TemplateArgument::Declaration:
5828 case TemplateArgument::NullPtr:
5829 case TemplateArgument::Pack:
5830 // FIXME: Is this right?
5831 return TemplateArgumentLocInfo();
5833 llvm_unreachable("unexpected template argument loc");
5837 ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
5838 const RecordData &Record, unsigned &Index) {
5839 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
5841 if (Arg.getKind() == TemplateArgument::Expression) {
5842 if (Record[Index++]) // bool InfoHasSameExpr.
5843 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
5845 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
5849 const ASTTemplateArgumentListInfo*
5850 ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
5851 const RecordData &Record,
5853 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
5854 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
5855 unsigned NumArgsAsWritten = Record[Index++];
5856 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
5857 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
5858 TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
5859 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
5862 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
5866 template<typename TemplateSpecializationDecl>
5867 static void completeRedeclChainForTemplateSpecialization(Decl *D) {
5868 if (auto *TSD = dyn_cast<TemplateSpecializationDecl>(D))
5869 TSD->getSpecializedTemplate()->LoadLazySpecializations();
5872 void ASTReader::CompleteRedeclChain(const Decl *D) {
5873 if (NumCurrentElementsDeserializing) {
5874 // We arrange to not care about the complete redeclaration chain while we're
5875 // deserializing. Just remember that the AST has marked this one as complete
5876 // but that it's not actually complete yet, so we know we still need to
5877 // complete it later.
5878 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
5882 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
5884 // If this is a named declaration, complete it by looking it up
5885 // within its context.
5887 // FIXME: Merging a function definition should merge
5888 // all mergeable entities within it.
5889 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
5890 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
5891 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
5892 auto *II = Name.getAsIdentifierInfo();
5893 if (isa<TranslationUnitDecl>(DC) && II) {
5894 // Outside of C++, we don't have a lookup table for the TU, so update
5895 // the identifier instead. In C++, either way should work fine.
5896 if (II->isOutOfDate())
5897 updateOutOfDateIdentifier(*II);
5900 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
5901 // FIXME: It'd be nice to do something a bit more targeted here.
5902 D->getDeclContext()->decls_begin();
5906 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
5907 CTSD->getSpecializedTemplate()->LoadLazySpecializations();
5908 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
5909 VTSD->getSpecializedTemplate()->LoadLazySpecializations();
5910 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
5911 if (auto *Template = FD->getPrimaryTemplate())
5912 Template->LoadLazySpecializations();
5916 uint64_t ASTReader::ReadCXXCtorInitializersRef(ModuleFile &M,
5917 const RecordData &Record,
5919 if (Idx >= Record.size() || Record[Idx] > M.LocalNumCXXCtorInitializers) {
5920 Error("malformed AST file: missing C++ ctor initializers");
5924 unsigned LocalID = Record[Idx++];
5925 return getGlobalBitOffset(M, M.CXXCtorInitializersOffsets[LocalID - 1]);
5928 CXXCtorInitializer **
5929 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
5930 RecordLocation Loc = getLocalBitOffset(Offset);
5931 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
5932 SavedStreamPosition SavedPosition(Cursor);
5933 Cursor.JumpToBit(Loc.Offset);
5934 ReadingKindTracker ReadingKind(Read_Decl, *this);
5937 unsigned Code = Cursor.ReadCode();
5938 unsigned RecCode = Cursor.readRecord(Code, Record);
5939 if (RecCode != DECL_CXX_CTOR_INITIALIZERS) {
5940 Error("malformed AST file: missing C++ ctor initializers");
5945 return ReadCXXCtorInitializers(*Loc.F, Record, Idx);
5948 uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M,
5949 const RecordData &Record,
5951 if (Idx >= Record.size() || Record[Idx] > M.LocalNumCXXBaseSpecifiers) {
5952 Error("malformed AST file: missing C++ base specifier");
5956 unsigned LocalID = Record[Idx++];
5957 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
5960 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
5961 RecordLocation Loc = getLocalBitOffset(Offset);
5962 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
5963 SavedStreamPosition SavedPosition(Cursor);
5964 Cursor.JumpToBit(Loc.Offset);
5965 ReadingKindTracker ReadingKind(Read_Decl, *this);
5967 unsigned Code = Cursor.ReadCode();
5968 unsigned RecCode = Cursor.readRecord(Code, Record);
5969 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
5970 Error("malformed AST file: missing C++ base specifiers");
5975 unsigned NumBases = Record[Idx++];
5976 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
5977 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
5978 for (unsigned I = 0; I != NumBases; ++I)
5979 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
5983 serialization::DeclID
5984 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
5985 if (LocalID < NUM_PREDEF_DECL_IDS)
5988 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5989 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
5990 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
5992 return LocalID + I->second;
5995 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
5996 ModuleFile &M) const {
5997 // Predefined decls aren't from any module.
5998 if (ID < NUM_PREDEF_DECL_IDS)
6001 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
6002 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
6005 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
6006 if (!D->isFromASTFile())
6008 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
6009 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6013 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
6014 if (ID < NUM_PREDEF_DECL_IDS)
6015 return SourceLocation();
6017 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6019 if (Index > DeclsLoaded.size()) {
6020 Error("declaration ID out-of-range for AST file");
6021 return SourceLocation();
6024 if (Decl *D = DeclsLoaded[Index])
6025 return D->getLocation();
6027 unsigned RawLocation = 0;
6028 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
6029 return ReadSourceLocation(*Rec.F, RawLocation);
6032 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
6034 case PREDEF_DECL_NULL_ID:
6037 case PREDEF_DECL_TRANSLATION_UNIT_ID:
6038 return Context.getTranslationUnitDecl();
6040 case PREDEF_DECL_OBJC_ID_ID:
6041 return Context.getObjCIdDecl();
6043 case PREDEF_DECL_OBJC_SEL_ID:
6044 return Context.getObjCSelDecl();
6046 case PREDEF_DECL_OBJC_CLASS_ID:
6047 return Context.getObjCClassDecl();
6049 case PREDEF_DECL_OBJC_PROTOCOL_ID:
6050 return Context.getObjCProtocolDecl();
6052 case PREDEF_DECL_INT_128_ID:
6053 return Context.getInt128Decl();
6055 case PREDEF_DECL_UNSIGNED_INT_128_ID:
6056 return Context.getUInt128Decl();
6058 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
6059 return Context.getObjCInstanceTypeDecl();
6061 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
6062 return Context.getBuiltinVaListDecl();
6064 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
6065 return Context.getExternCContextDecl();
6067 llvm_unreachable("PredefinedDeclIDs unknown enum value");
6070 Decl *ASTReader::GetExistingDecl(DeclID ID) {
6071 if (ID < NUM_PREDEF_DECL_IDS) {
6072 Decl *D = getPredefinedDecl(Context, (PredefinedDeclIDs)ID);
6074 // Track that we have merged the declaration with ID \p ID into the
6075 // pre-existing predefined declaration \p D.
6076 auto &Merged = KeyDecls[D->getCanonicalDecl()];
6078 Merged.push_back(ID);
6083 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6085 if (Index >= DeclsLoaded.size()) {
6086 assert(0 && "declaration ID out-of-range for AST file");
6087 Error("declaration ID out-of-range for AST file");
6091 return DeclsLoaded[Index];
6094 Decl *ASTReader::GetDecl(DeclID ID) {
6095 if (ID < NUM_PREDEF_DECL_IDS)
6096 return GetExistingDecl(ID);
6098 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6100 if (Index >= DeclsLoaded.size()) {
6101 assert(0 && "declaration ID out-of-range for AST file");
6102 Error("declaration ID out-of-range for AST file");
6106 if (!DeclsLoaded[Index]) {
6108 if (DeserializationListener)
6109 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
6112 return DeclsLoaded[Index];
6115 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
6117 if (GlobalID < NUM_PREDEF_DECL_IDS)
6120 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
6121 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6122 ModuleFile *Owner = I->second;
6124 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
6125 = M.GlobalToLocalDeclIDs.find(Owner);
6126 if (Pos == M.GlobalToLocalDeclIDs.end())
6129 return GlobalID - Owner->BaseDeclID + Pos->second;
6132 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
6133 const RecordData &Record,
6135 if (Idx >= Record.size()) {
6136 Error("Corrupted AST file");
6140 return getGlobalDeclID(F, Record[Idx++]);
6143 /// \brief Resolve the offset of a statement into a statement.
6145 /// This operation will read a new statement from the external
6146 /// source each time it is called, and is meant to be used via a
6147 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
6148 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
6149 // Switch case IDs are per Decl.
6150 ClearSwitchCaseIDs();
6152 // Offset here is a global offset across the entire chain.
6153 RecordLocation Loc = getLocalBitOffset(Offset);
6154 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
6155 return ReadStmtFromStream(*Loc.F);
6159 class FindExternalLexicalDeclsVisitor {
6161 const DeclContext *DC;
6162 bool (*isKindWeWant)(Decl::Kind);
6164 SmallVectorImpl<Decl*> &Decls;
6165 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
6168 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
6169 bool (*isKindWeWant)(Decl::Kind),
6170 SmallVectorImpl<Decl*> &Decls)
6171 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
6173 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
6174 PredefsVisited[I] = false;
6177 static bool visitPostorder(ModuleFile &M, void *UserData) {
6178 FindExternalLexicalDeclsVisitor *This
6179 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
6181 ModuleFile::DeclContextInfosMap::iterator Info
6182 = M.DeclContextInfos.find(This->DC);
6183 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
6186 // Load all of the declaration IDs
6187 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
6188 *IDE = ID + Info->second.NumLexicalDecls;
6190 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
6193 // Don't add predefined declarations to the lexical context more
6195 if (ID->second < NUM_PREDEF_DECL_IDS) {
6196 if (This->PredefsVisited[ID->second])
6199 This->PredefsVisited[ID->second] = true;
6202 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
6203 if (!This->DC->isDeclInLexicalTraversal(D))
6204 This->Decls.push_back(D);
6213 ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
6214 bool (*isKindWeWant)(Decl::Kind),
6215 SmallVectorImpl<Decl*> &Decls) {
6216 // There might be lexical decls in multiple modules, for the TU at
6217 // least. Walk all of the modules in the order they were loaded.
6218 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
6219 ModuleMgr.visitDepthFirst(
6220 nullptr, &FindExternalLexicalDeclsVisitor::visitPostorder, &Visitor);
6221 ++NumLexicalDeclContextsRead;
6232 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
6234 bool operator()(LocalDeclID L, LocalDeclID R) const {
6235 SourceLocation LHS = getLocation(L);
6236 SourceLocation RHS = getLocation(R);
6237 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6240 bool operator()(SourceLocation LHS, LocalDeclID R) const {
6241 SourceLocation RHS = getLocation(R);
6242 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6245 bool operator()(LocalDeclID L, SourceLocation RHS) const {
6246 SourceLocation LHS = getLocation(L);
6247 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6250 SourceLocation getLocation(LocalDeclID ID) const {
6251 return Reader.getSourceManager().getFileLoc(
6252 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
6258 void ASTReader::FindFileRegionDecls(FileID File,
6259 unsigned Offset, unsigned Length,
6260 SmallVectorImpl<Decl *> &Decls) {
6261 SourceManager &SM = getSourceManager();
6263 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
6264 if (I == FileDeclIDs.end())
6267 FileDeclsInfo &DInfo = I->second;
6268 if (DInfo.Decls.empty())
6272 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
6273 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
6275 DeclIDComp DIDComp(*this, *DInfo.Mod);
6276 ArrayRef<serialization::LocalDeclID>::iterator
6277 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6279 if (BeginIt != DInfo.Decls.begin())
6282 // If we are pointing at a top-level decl inside an objc container, we need
6283 // to backtrack until we find it otherwise we will fail to report that the
6284 // region overlaps with an objc container.
6285 while (BeginIt != DInfo.Decls.begin() &&
6286 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
6287 ->isTopLevelDeclInObjCContainer())
6290 ArrayRef<serialization::LocalDeclID>::iterator
6291 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6293 if (EndIt != DInfo.Decls.end())
6296 for (ArrayRef<serialization::LocalDeclID>::iterator
6297 DIt = BeginIt; DIt != EndIt; ++DIt)
6298 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
6301 /// \brief Retrieve the "definitive" module file for the definition of the
6302 /// given declaration context, if there is one.
6304 /// The "definitive" module file is the only place where we need to look to
6305 /// find information about the declarations within the given declaration
6306 /// context. For example, C++ and Objective-C classes, C structs/unions, and
6307 /// Objective-C protocols, categories, and extensions are all defined in a
6308 /// single place in the source code, so they have definitive module files
6309 /// associated with them. C++ namespaces, on the other hand, can have
6310 /// definitions in multiple different module files.
6312 /// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's
6313 /// NDEBUG checking.
6314 static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC,
6315 ASTReader &Reader) {
6316 if (const DeclContext *DefDC = getDefinitiveDeclContext(DC))
6317 return Reader.getOwningModuleFile(cast<Decl>(DefDC));
6323 /// \brief ModuleFile visitor used to perform name lookup into a
6324 /// declaration context.
6325 class DeclContextNameLookupVisitor {
6327 ArrayRef<const DeclContext *> Contexts;
6328 DeclarationName Name;
6329 ASTDeclContextNameLookupTrait::DeclNameKey NameKey;
6331 SmallVectorImpl<NamedDecl *> &Decls;
6332 llvm::SmallPtrSetImpl<NamedDecl *> &DeclSet;
6335 DeclContextNameLookupVisitor(ASTReader &Reader,
6336 DeclarationName Name,
6337 SmallVectorImpl<NamedDecl *> &Decls,
6338 llvm::SmallPtrSetImpl<NamedDecl *> &DeclSet)
6339 : Reader(Reader), Name(Name),
6340 NameKey(ASTDeclContextNameLookupTrait::GetInternalKey(Name)),
6341 NameHash(ASTDeclContextNameLookupTrait::ComputeHash(NameKey)),
6342 Decls(Decls), DeclSet(DeclSet) {}
6344 void visitContexts(ArrayRef<const DeclContext*> Contexts) {
6345 if (Contexts.empty())
6347 this->Contexts = Contexts;
6349 // If we can definitively determine which module file to look into,
6350 // only look there. Otherwise, look in all module files.
6351 ModuleFile *Definitive;
6352 if (Contexts.size() == 1 &&
6353 (Definitive = getDefinitiveModuleFileFor(Contexts[0], Reader))) {
6354 visit(*Definitive, this);
6356 Reader.getModuleManager().visit(&visit, this);
6361 static bool visit(ModuleFile &M, void *UserData) {
6362 DeclContextNameLookupVisitor *This
6363 = static_cast<DeclContextNameLookupVisitor *>(UserData);
6365 // Check whether we have any visible declaration information for
6366 // this context in this module.
6367 ModuleFile::DeclContextInfosMap::iterator Info;
6368 bool FoundInfo = false;
6369 for (auto *DC : This->Contexts) {
6370 Info = M.DeclContextInfos.find(DC);
6371 if (Info != M.DeclContextInfos.end() &&
6372 Info->second.NameLookupTableData) {
6381 // Look for this name within this module.
6382 ASTDeclContextNameLookupTable *LookupTable =
6383 Info->second.NameLookupTableData;
6384 ASTDeclContextNameLookupTable::iterator Pos
6385 = LookupTable->find_hashed(This->NameKey, This->NameHash);
6386 if (Pos == LookupTable->end())
6389 bool FoundAnything = false;
6390 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
6391 for (; Data.first != Data.second; ++Data.first) {
6392 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
6396 if (ND->getDeclName() != This->Name) {
6397 // A name might be null because the decl's redeclarable part is
6398 // currently read before reading its name. The lookup is triggered by
6399 // building that decl (likely indirectly), and so it is later in the
6400 // sense of "already existing" and can be ignored here.
6401 // FIXME: This should not happen; deserializing declarations should
6402 // not perform lookups since that can lead to deserialization cycles.
6406 // Record this declaration.
6407 FoundAnything = true;
6408 if (This->DeclSet.insert(ND).second)
6409 This->Decls.push_back(ND);
6412 return FoundAnything;
6418 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
6419 DeclarationName Name) {
6420 assert(DC->hasExternalVisibleStorage() &&
6421 "DeclContext has no visible decls in storage");
6425 Deserializing LookupResults(this);
6427 SmallVector<NamedDecl *, 64> Decls;
6428 llvm::SmallPtrSet<NamedDecl*, 64> DeclSet;
6430 // Compute the declaration contexts we need to look into. Multiple such
6431 // declaration contexts occur when two declaration contexts from disjoint
6432 // modules get merged, e.g., when two namespaces with the same name are
6433 // independently defined in separate modules.
6434 SmallVector<const DeclContext *, 2> Contexts;
6435 Contexts.push_back(DC);
6437 if (DC->isNamespace()) {
6438 auto Key = KeyDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
6439 if (Key != KeyDecls.end()) {
6440 for (unsigned I = 0, N = Key->second.size(); I != N; ++I)
6441 Contexts.push_back(cast<DeclContext>(GetDecl(Key->second[I])));
6445 DeclContextNameLookupVisitor Visitor(*this, Name, Decls, DeclSet);
6446 Visitor.visitContexts(Contexts);
6448 // If this might be an implicit special member function, then also search
6449 // all merged definitions of the surrounding class. We need to search them
6450 // individually, because finding an entity in one of them doesn't imply that
6451 // we can't find a different entity in another one.
6452 if (isa<CXXRecordDecl>(DC)) {
6453 auto Merged = MergedLookups.find(DC);
6454 if (Merged != MergedLookups.end()) {
6455 for (unsigned I = 0; I != Merged->second.size(); ++I) {
6456 const DeclContext *Context = Merged->second[I];
6457 Visitor.visitContexts(Context);
6458 // We might have just added some more merged lookups. If so, our
6459 // iterator is now invalid, so grab a fresh one before continuing.
6460 Merged = MergedLookups.find(DC);
6465 ++NumVisibleDeclContextsRead;
6466 SetExternalVisibleDeclsForName(DC, Name, Decls);
6467 return !Decls.empty();
6471 /// \brief ModuleFile visitor used to retrieve all visible names in a
6472 /// declaration context.
6473 class DeclContextAllNamesVisitor {
6475 SmallVectorImpl<const DeclContext *> &Contexts;
6477 llvm::SmallPtrSet<NamedDecl *, 256> DeclSet;
6481 DeclContextAllNamesVisitor(ASTReader &Reader,
6482 SmallVectorImpl<const DeclContext *> &Contexts,
6483 DeclsMap &Decls, bool VisitAll)
6484 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { }
6486 static bool visit(ModuleFile &M, void *UserData) {
6487 DeclContextAllNamesVisitor *This
6488 = static_cast<DeclContextAllNamesVisitor *>(UserData);
6490 // Check whether we have any visible declaration information for
6491 // this context in this module.
6492 ModuleFile::DeclContextInfosMap::iterator Info;
6493 bool FoundInfo = false;
6494 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
6495 Info = M.DeclContextInfos.find(This->Contexts[I]);
6496 if (Info != M.DeclContextInfos.end() &&
6497 Info->second.NameLookupTableData) {
6506 ASTDeclContextNameLookupTable *LookupTable =
6507 Info->second.NameLookupTableData;
6508 bool FoundAnything = false;
6509 for (ASTDeclContextNameLookupTable::data_iterator
6510 I = LookupTable->data_begin(), E = LookupTable->data_end();
6513 ASTDeclContextNameLookupTrait::data_type Data = *I;
6514 for (; Data.first != Data.second; ++Data.first) {
6515 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
6520 // Record this declaration.
6521 FoundAnything = true;
6522 if (This->DeclSet.insert(ND).second)
6523 This->Decls[ND->getDeclName()].push_back(ND);
6527 return FoundAnything && !This->VisitAll;
6532 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
6533 if (!DC->hasExternalVisibleStorage())
6537 // Compute the declaration contexts we need to look into. Multiple such
6538 // declaration contexts occur when two declaration contexts from disjoint
6539 // modules get merged, e.g., when two namespaces with the same name are
6540 // independently defined in separate modules.
6541 SmallVector<const DeclContext *, 2> Contexts;
6542 Contexts.push_back(DC);
6544 if (DC->isNamespace()) {
6545 KeyDeclsMap::iterator Key =
6546 KeyDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
6547 if (Key != KeyDecls.end()) {
6548 for (unsigned I = 0, N = Key->second.size(); I != N; ++I)
6549 Contexts.push_back(cast<DeclContext>(GetDecl(Key->second[I])));
6553 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls,
6554 /*VisitAll=*/DC->isFileContext());
6555 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
6556 ++NumVisibleDeclContextsRead;
6558 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
6559 SetExternalVisibleDeclsForName(DC, I->first, I->second);
6561 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
6564 /// \brief Under non-PCH compilation the consumer receives the objc methods
6565 /// before receiving the implementation, and codegen depends on this.
6566 /// We simulate this by deserializing and passing to consumer the methods of the
6567 /// implementation before passing the deserialized implementation decl.
6568 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
6569 ASTConsumer *Consumer) {
6570 assert(ImplD && Consumer);
6572 for (auto *I : ImplD->methods())
6573 Consumer->HandleInterestingDecl(DeclGroupRef(I));
6575 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
6578 void ASTReader::PassInterestingDeclsToConsumer() {
6581 if (PassingDeclsToConsumer)
6584 // Guard variable to avoid recursively redoing the process of passing
6585 // decls to consumer.
6586 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6589 // Ensure that we've loaded all potentially-interesting declarations
6590 // that need to be eagerly loaded.
6591 for (auto ID : EagerlyDeserializedDecls)
6593 EagerlyDeserializedDecls.clear();
6595 while (!InterestingDecls.empty()) {
6596 Decl *D = InterestingDecls.front();
6597 InterestingDecls.pop_front();
6599 PassInterestingDeclToConsumer(D);
6603 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
6604 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
6605 PassObjCImplDeclToConsumer(ImplD, Consumer);
6607 Consumer->HandleInterestingDecl(DeclGroupRef(D));
6610 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
6611 this->Consumer = Consumer;
6614 PassInterestingDeclsToConsumer();
6616 if (DeserializationListener)
6617 DeserializationListener->ReaderInitialized(this);
6620 void ASTReader::PrintStats() {
6621 std::fprintf(stderr, "*** AST File Statistics:\n");
6623 unsigned NumTypesLoaded
6624 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
6626 unsigned NumDeclsLoaded
6627 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
6629 unsigned NumIdentifiersLoaded
6630 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
6631 IdentifiersLoaded.end(),
6632 (IdentifierInfo *)nullptr);
6633 unsigned NumMacrosLoaded
6634 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
6636 (MacroInfo *)nullptr);
6637 unsigned NumSelectorsLoaded
6638 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
6639 SelectorsLoaded.end(),
6642 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
6643 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
6644 NumSLocEntriesRead, TotalNumSLocEntries,
6645 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
6646 if (!TypesLoaded.empty())
6647 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
6648 NumTypesLoaded, (unsigned)TypesLoaded.size(),
6649 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
6650 if (!DeclsLoaded.empty())
6651 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
6652 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
6653 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
6654 if (!IdentifiersLoaded.empty())
6655 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
6656 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
6657 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
6658 if (!MacrosLoaded.empty())
6659 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6660 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
6661 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
6662 if (!SelectorsLoaded.empty())
6663 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
6664 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
6665 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
6666 if (TotalNumStatements)
6667 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
6668 NumStatementsRead, TotalNumStatements,
6669 ((float)NumStatementsRead/TotalNumStatements * 100));
6671 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6672 NumMacrosRead, TotalNumMacros,
6673 ((float)NumMacrosRead/TotalNumMacros * 100));
6674 if (TotalLexicalDeclContexts)
6675 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
6676 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
6677 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
6679 if (TotalVisibleDeclContexts)
6680 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
6681 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
6682 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
6684 if (TotalNumMethodPoolEntries) {
6685 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
6686 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
6687 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
6690 if (NumMethodPoolLookups) {
6691 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
6692 NumMethodPoolHits, NumMethodPoolLookups,
6693 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
6695 if (NumMethodPoolTableLookups) {
6696 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
6697 NumMethodPoolTableHits, NumMethodPoolTableLookups,
6698 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
6702 if (NumIdentifierLookupHits) {
6703 std::fprintf(stderr,
6704 " %u / %u identifier table lookups succeeded (%f%%)\n",
6705 NumIdentifierLookupHits, NumIdentifierLookups,
6706 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
6710 std::fprintf(stderr, "\n");
6711 GlobalIndex->printStats();
6714 std::fprintf(stderr, "\n");
6716 std::fprintf(stderr, "\n");
6719 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
6721 dumpModuleIDMap(StringRef Name,
6722 const ContinuousRangeMap<Key, ModuleFile *,
6723 InitialCapacity> &Map) {
6724 if (Map.begin() == Map.end())
6727 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
6728 llvm::errs() << Name << ":\n";
6729 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
6731 llvm::errs() << " " << I->first << " -> " << I->second->FileName
6736 void ASTReader::dump() {
6737 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
6738 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
6739 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
6740 dumpModuleIDMap("Global type map", GlobalTypeMap);
6741 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
6742 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
6743 dumpModuleIDMap("Global macro map", GlobalMacroMap);
6744 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
6745 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
6746 dumpModuleIDMap("Global preprocessed entity map",
6747 GlobalPreprocessedEntityMap);
6749 llvm::errs() << "\n*** PCH/Modules Loaded:";
6750 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
6751 MEnd = ModuleMgr.end();
6756 /// Return the amount of memory used by memory buffers, breaking down
6757 /// by heap-backed versus mmap'ed memory.
6758 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
6759 for (ModuleConstIterator I = ModuleMgr.begin(),
6760 E = ModuleMgr.end(); I != E; ++I) {
6761 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
6762 size_t bytes = buf->getBufferSize();
6763 switch (buf->getBufferKind()) {
6764 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
6765 sizes.malloc_bytes += bytes;
6767 case llvm::MemoryBuffer::MemoryBuffer_MMap:
6768 sizes.mmap_bytes += bytes;
6775 void ASTReader::InitializeSema(Sema &S) {
6777 S.addExternalSource(this);
6779 // Makes sure any declarations that were deserialized "too early"
6780 // still get added to the identifier's declaration chains.
6781 for (uint64_t ID : PreloadedDeclIDs) {
6782 NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
6783 pushExternalDeclIntoScope(D, D->getDeclName());
6785 PreloadedDeclIDs.clear();
6787 // FIXME: What happens if these are changed by a module import?
6788 if (!FPPragmaOptions.empty()) {
6789 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
6790 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
6793 // FIXME: What happens if these are changed by a module import?
6794 if (!OpenCLExtensions.empty()) {
6796 #define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
6797 #include "clang/Basic/OpenCLExtensions.def"
6799 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
6805 void ASTReader::UpdateSema() {
6806 assert(SemaObj && "no Sema to update");
6808 // Load the offsets of the declarations that Sema references.
6809 // They will be lazily deserialized when needed.
6810 if (!SemaDeclRefs.empty()) {
6811 assert(SemaDeclRefs.size() % 2 == 0);
6812 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 2) {
6813 if (!SemaObj->StdNamespace)
6814 SemaObj->StdNamespace = SemaDeclRefs[I];
6815 if (!SemaObj->StdBadAlloc)
6816 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
6818 SemaDeclRefs.clear();
6821 // Update the state of 'pragma clang optimize'. Use the same API as if we had
6822 // encountered the pragma in the source.
6823 if(OptimizeOffPragmaLocation.isValid())
6824 SemaObj->ActOnPragmaOptimize(/* IsOn = */ false, OptimizeOffPragmaLocation);
6827 IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
6828 // Note that we are loading an identifier.
6829 Deserializing AnIdentifier(this);
6830 StringRef Name(NameStart, NameEnd - NameStart);
6832 // If there is a global index, look there first to determine which modules
6833 // provably do not have any results for this identifier.
6834 GlobalModuleIndex::HitSet Hits;
6835 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
6836 if (!loadGlobalIndex()) {
6837 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
6841 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
6842 NumIdentifierLookups,
6843 NumIdentifierLookupHits);
6844 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
6845 IdentifierInfo *II = Visitor.getIdentifierInfo();
6846 markIdentifierUpToDate(II);
6851 /// \brief An identifier-lookup iterator that enumerates all of the
6852 /// identifiers stored within a set of AST files.
6853 class ASTIdentifierIterator : public IdentifierIterator {
6854 /// \brief The AST reader whose identifiers are being enumerated.
6855 const ASTReader &Reader;
6857 /// \brief The current index into the chain of AST files stored in
6861 /// \brief The current position within the identifier lookup table
6862 /// of the current AST file.
6863 ASTIdentifierLookupTable::key_iterator Current;
6865 /// \brief The end position within the identifier lookup table of
6866 /// the current AST file.
6867 ASTIdentifierLookupTable::key_iterator End;
6870 explicit ASTIdentifierIterator(const ASTReader &Reader);
6872 StringRef Next() override;
6876 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
6877 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
6878 ASTIdentifierLookupTable *IdTable
6879 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
6880 Current = IdTable->key_begin();
6881 End = IdTable->key_end();
6884 StringRef ASTIdentifierIterator::Next() {
6885 while (Current == End) {
6886 // If we have exhausted all of our AST files, we're done.
6891 ASTIdentifierLookupTable *IdTable
6892 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
6893 IdentifierLookupTable;
6894 Current = IdTable->key_begin();
6895 End = IdTable->key_end();
6898 // We have any identifiers remaining in the current AST file; return
6900 StringRef Result = *Current;
6905 IdentifierIterator *ASTReader::getIdentifiers() {
6906 if (!loadGlobalIndex())
6907 return GlobalIndex->createIdentifierIterator();
6909 return new ASTIdentifierIterator(*this);
6912 namespace clang { namespace serialization {
6913 class ReadMethodPoolVisitor {
6916 unsigned PriorGeneration;
6917 unsigned InstanceBits;
6918 unsigned FactoryBits;
6919 bool InstanceHasMoreThanOneDecl;
6920 bool FactoryHasMoreThanOneDecl;
6921 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
6922 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
6925 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
6926 unsigned PriorGeneration)
6927 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration),
6928 InstanceBits(0), FactoryBits(0), InstanceHasMoreThanOneDecl(false),
6929 FactoryHasMoreThanOneDecl(false) {}
6931 static bool visit(ModuleFile &M, void *UserData) {
6932 ReadMethodPoolVisitor *This
6933 = static_cast<ReadMethodPoolVisitor *>(UserData);
6935 if (!M.SelectorLookupTable)
6938 // If we've already searched this module file, skip it now.
6939 if (M.Generation <= This->PriorGeneration)
6942 ++This->Reader.NumMethodPoolTableLookups;
6943 ASTSelectorLookupTable *PoolTable
6944 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
6945 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
6946 if (Pos == PoolTable->end())
6949 ++This->Reader.NumMethodPoolTableHits;
6950 ++This->Reader.NumSelectorsRead;
6951 // FIXME: Not quite happy with the statistics here. We probably should
6952 // disable this tracking when called via LoadSelector.
6953 // Also, should entries without methods count as misses?
6954 ++This->Reader.NumMethodPoolEntriesRead;
6955 ASTSelectorLookupTrait::data_type Data = *Pos;
6956 if (This->Reader.DeserializationListener)
6957 This->Reader.DeserializationListener->SelectorRead(Data.ID,
6960 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
6961 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
6962 This->InstanceBits = Data.InstanceBits;
6963 This->FactoryBits = Data.FactoryBits;
6964 This->InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
6965 This->FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
6969 /// \brief Retrieve the instance methods found by this visitor.
6970 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
6971 return InstanceMethods;
6974 /// \brief Retrieve the instance methods found by this visitor.
6975 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
6976 return FactoryMethods;
6979 unsigned getInstanceBits() const { return InstanceBits; }
6980 unsigned getFactoryBits() const { return FactoryBits; }
6981 bool instanceHasMoreThanOneDecl() const {
6982 return InstanceHasMoreThanOneDecl;
6984 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
6986 } } // end namespace clang::serialization
6988 /// \brief Add the given set of methods to the method list.
6989 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
6990 ObjCMethodList &List) {
6991 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
6992 S.addMethodToGlobalList(&List, Methods[I]);
6996 void ASTReader::ReadMethodPool(Selector Sel) {
6997 // Get the selector generation and update it to the current generation.
6998 unsigned &Generation = SelectorGeneration[Sel];
6999 unsigned PriorGeneration = Generation;
7000 Generation = getGeneration();
7002 // Search for methods defined with this selector.
7003 ++NumMethodPoolLookups;
7004 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
7005 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
7007 if (Visitor.getInstanceMethods().empty() &&
7008 Visitor.getFactoryMethods().empty())
7011 ++NumMethodPoolHits;
7016 Sema &S = *getSema();
7017 Sema::GlobalMethodPool::iterator Pos
7018 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
7020 Pos->second.first.setBits(Visitor.getInstanceBits());
7021 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
7022 Pos->second.second.setBits(Visitor.getFactoryBits());
7023 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
7025 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
7026 // when building a module we keep every method individually and may need to
7027 // update hasMoreThanOneDecl as we add the methods.
7028 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
7029 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
7032 void ASTReader::ReadKnownNamespaces(
7033 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
7036 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
7037 if (NamespaceDecl *Namespace
7038 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
7039 Namespaces.push_back(Namespace);
7043 void ASTReader::ReadUndefinedButUsed(
7044 llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) {
7045 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
7046 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
7047 SourceLocation Loc =
7048 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
7049 Undefined.insert(std::make_pair(D, Loc));
7053 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
7054 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
7056 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
7057 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
7058 uint64_t Count = DelayedDeleteExprs[Idx++];
7059 for (uint64_t C = 0; C < Count; ++C) {
7060 SourceLocation DeleteLoc =
7061 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
7062 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
7063 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
7068 void ASTReader::ReadTentativeDefinitions(
7069 SmallVectorImpl<VarDecl *> &TentativeDefs) {
7070 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
7071 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
7073 TentativeDefs.push_back(Var);
7075 TentativeDefinitions.clear();
7078 void ASTReader::ReadUnusedFileScopedDecls(
7079 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
7080 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
7082 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
7086 UnusedFileScopedDecls.clear();
7089 void ASTReader::ReadDelegatingConstructors(
7090 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
7091 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
7092 CXXConstructorDecl *D
7093 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
7097 DelegatingCtorDecls.clear();
7100 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
7101 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
7103 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
7107 ExtVectorDecls.clear();
7110 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
7111 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
7112 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
7114 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
7115 GetDecl(UnusedLocalTypedefNameCandidates[I]));
7119 UnusedLocalTypedefNameCandidates.clear();
7122 void ASTReader::ReadReferencedSelectors(
7123 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
7124 if (ReferencedSelectorsData.empty())
7127 // If there are @selector references added them to its pool. This is for
7128 // implementation of -Wselector.
7129 unsigned int DataSize = ReferencedSelectorsData.size()-1;
7131 while (I < DataSize) {
7132 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
7133 SourceLocation SelLoc
7134 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
7135 Sels.push_back(std::make_pair(Sel, SelLoc));
7137 ReferencedSelectorsData.clear();
7140 void ASTReader::ReadWeakUndeclaredIdentifiers(
7141 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
7142 if (WeakUndeclaredIdentifiers.empty())
7145 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
7146 IdentifierInfo *WeakId
7147 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7148 IdentifierInfo *AliasId
7149 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7151 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
7152 bool Used = WeakUndeclaredIdentifiers[I++];
7153 WeakInfo WI(AliasId, Loc);
7155 WeakIDs.push_back(std::make_pair(WeakId, WI));
7157 WeakUndeclaredIdentifiers.clear();
7160 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
7161 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
7162 ExternalVTableUse VT;
7163 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
7164 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
7165 VT.DefinitionRequired = VTableUses[Idx++];
7166 VTables.push_back(VT);
7172 void ASTReader::ReadPendingInstantiations(
7173 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
7174 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
7175 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
7177 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
7179 Pending.push_back(std::make_pair(D, Loc));
7181 PendingInstantiations.clear();
7184 void ASTReader::ReadLateParsedTemplates(
7185 llvm::MapVector<const FunctionDecl *, LateParsedTemplate *> &LPTMap) {
7186 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
7188 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
7190 LateParsedTemplate *LT = new LateParsedTemplate;
7191 LT->D = GetDecl(LateParsedTemplates[Idx++]);
7193 ModuleFile *F = getOwningModuleFile(LT->D);
7194 assert(F && "No module");
7196 unsigned TokN = LateParsedTemplates[Idx++];
7197 LT->Toks.reserve(TokN);
7198 for (unsigned T = 0; T < TokN; ++T)
7199 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
7201 LPTMap.insert(std::make_pair(FD, LT));
7204 LateParsedTemplates.clear();
7207 void ASTReader::LoadSelector(Selector Sel) {
7208 // It would be complicated to avoid reading the methods anyway. So don't.
7209 ReadMethodPool(Sel);
7212 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
7213 assert(ID && "Non-zero identifier ID required");
7214 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
7215 IdentifiersLoaded[ID - 1] = II;
7216 if (DeserializationListener)
7217 DeserializationListener->IdentifierRead(ID, II);
7220 /// \brief Set the globally-visible declarations associated with the given
7223 /// If the AST reader is currently in a state where the given declaration IDs
7224 /// cannot safely be resolved, they are queued until it is safe to resolve
7227 /// \param II an IdentifierInfo that refers to one or more globally-visible
7230 /// \param DeclIDs the set of declaration IDs with the name @p II that are
7231 /// visible at global scope.
7233 /// \param Decls if non-null, this vector will be populated with the set of
7234 /// deserialized declarations. These declarations will not be pushed into
7237 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
7238 const SmallVectorImpl<uint32_t> &DeclIDs,
7239 SmallVectorImpl<Decl *> *Decls) {
7240 if (NumCurrentElementsDeserializing && !Decls) {
7241 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
7245 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
7247 // Queue this declaration so that it will be added to the
7248 // translation unit scope and identifier's declaration chain
7249 // once a Sema object is known.
7250 PreloadedDeclIDs.push_back(DeclIDs[I]);
7254 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
7256 // If we're simply supposed to record the declarations, do so now.
7258 Decls->push_back(D);
7262 // Introduce this declaration into the translation-unit scope
7263 // and add it to the declaration chain for this identifier, so
7264 // that (unqualified) name lookup will find it.
7265 pushExternalDeclIntoScope(D, II);
7269 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
7273 if (IdentifiersLoaded.empty()) {
7274 Error("no identifier table in AST file");
7279 if (!IdentifiersLoaded[ID]) {
7280 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
7281 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
7282 ModuleFile *M = I->second;
7283 unsigned Index = ID - M->BaseIdentifierID;
7284 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
7286 // All of the strings in the AST file are preceded by a 16-bit length.
7287 // Extract that 16-bit length to avoid having to execute strlen().
7288 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
7289 // unsigned integers. This is important to avoid integer overflow when
7290 // we cast them to 'unsigned'.
7291 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
7292 unsigned StrLen = (((unsigned) StrLenPtr[0])
7293 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
7294 IdentifiersLoaded[ID]
7295 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
7296 if (DeserializationListener)
7297 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
7300 return IdentifiersLoaded[ID];
7303 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
7304 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
7307 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
7308 if (LocalID < NUM_PREDEF_IDENT_IDS)
7311 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7312 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
7313 assert(I != M.IdentifierRemap.end()
7314 && "Invalid index into identifier index remap");
7316 return LocalID + I->second;
7319 MacroInfo *ASTReader::getMacro(MacroID ID) {
7323 if (MacrosLoaded.empty()) {
7324 Error("no macro table in AST file");
7328 ID -= NUM_PREDEF_MACRO_IDS;
7329 if (!MacrosLoaded[ID]) {
7330 GlobalMacroMapType::iterator I
7331 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
7332 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
7333 ModuleFile *M = I->second;
7334 unsigned Index = ID - M->BaseMacroID;
7335 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
7337 if (DeserializationListener)
7338 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
7342 return MacrosLoaded[ID];
7345 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
7346 if (LocalID < NUM_PREDEF_MACRO_IDS)
7349 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7350 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
7351 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
7353 return LocalID + I->second;
7356 serialization::SubmoduleID
7357 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
7358 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
7361 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7362 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
7363 assert(I != M.SubmoduleRemap.end()
7364 && "Invalid index into submodule index remap");
7366 return LocalID + I->second;
7369 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
7370 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
7371 assert(GlobalID == 0 && "Unhandled global submodule ID");
7375 if (GlobalID > SubmodulesLoaded.size()) {
7376 Error("submodule ID out of range in AST file");
7380 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
7383 Module *ASTReader::getModule(unsigned ID) {
7384 return getSubmodule(ID);
7387 ExternalASTSource::ASTSourceDescriptor
7388 ASTReader::getSourceDescriptor(const Module &M) {
7389 StringRef Dir, Filename;
7391 Dir = M.Directory->getName();
7392 if (auto *File = M.getASTFile())
7393 Filename = File->getName();
7394 return ASTReader::ASTSourceDescriptor{
7395 M.getFullModuleName(), Dir, Filename,
7400 llvm::Optional<ExternalASTSource::ASTSourceDescriptor>
7401 ASTReader::getSourceDescriptor(unsigned ID) {
7402 if (const Module *M = getSubmodule(ID))
7403 return getSourceDescriptor(*M);
7405 // If there is only a single PCH, return it instead.
7406 // Chained PCH are not suported.
7407 if (ModuleMgr.size() == 1) {
7408 ModuleFile &MF = ModuleMgr.getPrimaryModule();
7409 return ASTReader::ASTSourceDescriptor{
7410 MF.OriginalSourceFileName, MF.OriginalDir,
7418 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
7419 return DecodeSelector(getGlobalSelectorID(M, LocalID));
7422 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
7426 if (ID > SelectorsLoaded.size()) {
7427 Error("selector ID out of range in AST file");
7431 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
7432 // Load this selector from the selector table.
7433 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
7434 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
7435 ModuleFile &M = *I->second;
7436 ASTSelectorLookupTrait Trait(*this, M);
7437 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
7438 SelectorsLoaded[ID - 1] =
7439 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
7440 if (DeserializationListener)
7441 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
7444 return SelectorsLoaded[ID - 1];
7447 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
7448 return DecodeSelector(ID);
7451 uint32_t ASTReader::GetNumExternalSelectors() {
7452 // ID 0 (the null selector) is considered an external selector.
7453 return getTotalNumSelectors() + 1;
7456 serialization::SelectorID
7457 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
7458 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
7461 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7462 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
7463 assert(I != M.SelectorRemap.end()
7464 && "Invalid index into selector index remap");
7466 return LocalID + I->second;
7470 ASTReader::ReadDeclarationName(ModuleFile &F,
7471 const RecordData &Record, unsigned &Idx) {
7472 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
7474 case DeclarationName::Identifier:
7475 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
7477 case DeclarationName::ObjCZeroArgSelector:
7478 case DeclarationName::ObjCOneArgSelector:
7479 case DeclarationName::ObjCMultiArgSelector:
7480 return DeclarationName(ReadSelector(F, Record, Idx));
7482 case DeclarationName::CXXConstructorName:
7483 return Context.DeclarationNames.getCXXConstructorName(
7484 Context.getCanonicalType(readType(F, Record, Idx)));
7486 case DeclarationName::CXXDestructorName:
7487 return Context.DeclarationNames.getCXXDestructorName(
7488 Context.getCanonicalType(readType(F, Record, Idx)));
7490 case DeclarationName::CXXConversionFunctionName:
7491 return Context.DeclarationNames.getCXXConversionFunctionName(
7492 Context.getCanonicalType(readType(F, Record, Idx)));
7494 case DeclarationName::CXXOperatorName:
7495 return Context.DeclarationNames.getCXXOperatorName(
7496 (OverloadedOperatorKind)Record[Idx++]);
7498 case DeclarationName::CXXLiteralOperatorName:
7499 return Context.DeclarationNames.getCXXLiteralOperatorName(
7500 GetIdentifierInfo(F, Record, Idx));
7502 case DeclarationName::CXXUsingDirective:
7503 return DeclarationName::getUsingDirectiveName();
7506 llvm_unreachable("Invalid NameKind!");
7509 void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
7510 DeclarationNameLoc &DNLoc,
7511 DeclarationName Name,
7512 const RecordData &Record, unsigned &Idx) {
7513 switch (Name.getNameKind()) {
7514 case DeclarationName::CXXConstructorName:
7515 case DeclarationName::CXXDestructorName:
7516 case DeclarationName::CXXConversionFunctionName:
7517 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
7520 case DeclarationName::CXXOperatorName:
7521 DNLoc.CXXOperatorName.BeginOpNameLoc
7522 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7523 DNLoc.CXXOperatorName.EndOpNameLoc
7524 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7527 case DeclarationName::CXXLiteralOperatorName:
7528 DNLoc.CXXLiteralOperatorName.OpNameLoc
7529 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7532 case DeclarationName::Identifier:
7533 case DeclarationName::ObjCZeroArgSelector:
7534 case DeclarationName::ObjCOneArgSelector:
7535 case DeclarationName::ObjCMultiArgSelector:
7536 case DeclarationName::CXXUsingDirective:
7541 void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
7542 DeclarationNameInfo &NameInfo,
7543 const RecordData &Record, unsigned &Idx) {
7544 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
7545 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
7546 DeclarationNameLoc DNLoc;
7547 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
7548 NameInfo.setInfo(DNLoc);
7551 void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
7552 const RecordData &Record, unsigned &Idx) {
7553 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
7554 unsigned NumTPLists = Record[Idx++];
7555 Info.NumTemplParamLists = NumTPLists;
7557 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
7558 for (unsigned i=0; i != NumTPLists; ++i)
7559 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
7564 ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
7566 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
7568 case TemplateName::Template:
7569 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
7571 case TemplateName::OverloadedTemplate: {
7572 unsigned size = Record[Idx++];
7573 UnresolvedSet<8> Decls;
7575 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
7577 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
7580 case TemplateName::QualifiedTemplate: {
7581 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7582 bool hasTemplKeyword = Record[Idx++];
7583 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
7584 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
7587 case TemplateName::DependentTemplate: {
7588 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7589 if (Record[Idx++]) // isIdentifier
7590 return Context.getDependentTemplateName(NNS,
7591 GetIdentifierInfo(F, Record,
7593 return Context.getDependentTemplateName(NNS,
7594 (OverloadedOperatorKind)Record[Idx++]);
7597 case TemplateName::SubstTemplateTemplateParm: {
7598 TemplateTemplateParmDecl *param
7599 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7600 if (!param) return TemplateName();
7601 TemplateName replacement = ReadTemplateName(F, Record, Idx);
7602 return Context.getSubstTemplateTemplateParm(param, replacement);
7605 case TemplateName::SubstTemplateTemplateParmPack: {
7606 TemplateTemplateParmDecl *Param
7607 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7609 return TemplateName();
7611 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
7612 if (ArgPack.getKind() != TemplateArgument::Pack)
7613 return TemplateName();
7615 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
7619 llvm_unreachable("Unhandled template name kind!");
7623 ASTReader::ReadTemplateArgument(ModuleFile &F,
7624 const RecordData &Record, unsigned &Idx) {
7625 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
7627 case TemplateArgument::Null:
7628 return TemplateArgument();
7629 case TemplateArgument::Type:
7630 return TemplateArgument(readType(F, Record, Idx));
7631 case TemplateArgument::Declaration: {
7632 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
7633 return TemplateArgument(D, readType(F, Record, Idx));
7635 case TemplateArgument::NullPtr:
7636 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
7637 case TemplateArgument::Integral: {
7638 llvm::APSInt Value = ReadAPSInt(Record, Idx);
7639 QualType T = readType(F, Record, Idx);
7640 return TemplateArgument(Context, Value, T);
7642 case TemplateArgument::Template:
7643 return TemplateArgument(ReadTemplateName(F, Record, Idx));
7644 case TemplateArgument::TemplateExpansion: {
7645 TemplateName Name = ReadTemplateName(F, Record, Idx);
7646 Optional<unsigned> NumTemplateExpansions;
7647 if (unsigned NumExpansions = Record[Idx++])
7648 NumTemplateExpansions = NumExpansions - 1;
7649 return TemplateArgument(Name, NumTemplateExpansions);
7651 case TemplateArgument::Expression:
7652 return TemplateArgument(ReadExpr(F));
7653 case TemplateArgument::Pack: {
7654 unsigned NumArgs = Record[Idx++];
7655 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
7656 for (unsigned I = 0; I != NumArgs; ++I)
7657 Args[I] = ReadTemplateArgument(F, Record, Idx);
7658 return TemplateArgument(Args, NumArgs);
7662 llvm_unreachable("Unhandled template argument kind!");
7665 TemplateParameterList *
7666 ASTReader::ReadTemplateParameterList(ModuleFile &F,
7667 const RecordData &Record, unsigned &Idx) {
7668 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
7669 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
7670 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
7672 unsigned NumParams = Record[Idx++];
7673 SmallVector<NamedDecl *, 16> Params;
7674 Params.reserve(NumParams);
7676 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
7678 TemplateParameterList* TemplateParams =
7679 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
7680 Params.data(), Params.size(), RAngleLoc);
7681 return TemplateParams;
7686 ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
7687 ModuleFile &F, const RecordData &Record,
7689 unsigned NumTemplateArgs = Record[Idx++];
7690 TemplArgs.reserve(NumTemplateArgs);
7691 while (NumTemplateArgs--)
7692 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
7695 /// \brief Read a UnresolvedSet structure.
7696 void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
7697 const RecordData &Record, unsigned &Idx) {
7698 unsigned NumDecls = Record[Idx++];
7699 Set.reserve(Context, NumDecls);
7700 while (NumDecls--) {
7701 DeclID ID = ReadDeclID(F, Record, Idx);
7702 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
7703 Set.addLazyDecl(Context, ID, AS);
7708 ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
7709 const RecordData &Record, unsigned &Idx) {
7710 bool isVirtual = static_cast<bool>(Record[Idx++]);
7711 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
7712 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
7713 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
7714 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
7715 SourceRange Range = ReadSourceRange(F, Record, Idx);
7716 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
7717 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
7719 Result.setInheritConstructors(inheritConstructors);
7723 CXXCtorInitializer **
7724 ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
7726 unsigned NumInitializers = Record[Idx++];
7727 assert(NumInitializers && "wrote ctor initializers but have no inits");
7728 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
7729 for (unsigned i = 0; i != NumInitializers; ++i) {
7730 TypeSourceInfo *TInfo = nullptr;
7731 bool IsBaseVirtual = false;
7732 FieldDecl *Member = nullptr;
7733 IndirectFieldDecl *IndirectMember = nullptr;
7735 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
7737 case CTOR_INITIALIZER_BASE:
7738 TInfo = GetTypeSourceInfo(F, Record, Idx);
7739 IsBaseVirtual = Record[Idx++];
7742 case CTOR_INITIALIZER_DELEGATING:
7743 TInfo = GetTypeSourceInfo(F, Record, Idx);
7746 case CTOR_INITIALIZER_MEMBER:
7747 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
7750 case CTOR_INITIALIZER_INDIRECT_MEMBER:
7751 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
7755 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
7756 Expr *Init = ReadExpr(F);
7757 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
7758 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
7759 bool IsWritten = Record[Idx++];
7760 unsigned SourceOrderOrNumArrayIndices;
7761 SmallVector<VarDecl *, 8> Indices;
7763 SourceOrderOrNumArrayIndices = Record[Idx++];
7765 SourceOrderOrNumArrayIndices = Record[Idx++];
7766 Indices.reserve(SourceOrderOrNumArrayIndices);
7767 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
7768 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
7771 CXXCtorInitializer *BOMInit;
7772 if (Type == CTOR_INITIALIZER_BASE) {
7773 BOMInit = new (Context)
7774 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
7775 RParenLoc, MemberOrEllipsisLoc);
7776 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
7777 BOMInit = new (Context)
7778 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
7779 } else if (IsWritten) {
7781 BOMInit = new (Context) CXXCtorInitializer(
7782 Context, Member, MemberOrEllipsisLoc, LParenLoc, Init, RParenLoc);
7784 BOMInit = new (Context)
7785 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
7786 LParenLoc, Init, RParenLoc);
7788 if (IndirectMember) {
7789 assert(Indices.empty() && "Indirect field improperly initialized");
7790 BOMInit = new (Context)
7791 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
7792 LParenLoc, Init, RParenLoc);
7794 BOMInit = CXXCtorInitializer::Create(
7795 Context, Member, MemberOrEllipsisLoc, LParenLoc, Init, RParenLoc,
7796 Indices.data(), Indices.size());
7801 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
7802 CtorInitializers[i] = BOMInit;
7805 return CtorInitializers;
7808 NestedNameSpecifier *
7809 ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
7810 const RecordData &Record, unsigned &Idx) {
7811 unsigned N = Record[Idx++];
7812 NestedNameSpecifier *NNS = nullptr, *Prev = nullptr;
7813 for (unsigned I = 0; I != N; ++I) {
7814 NestedNameSpecifier::SpecifierKind Kind
7815 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7817 case NestedNameSpecifier::Identifier: {
7818 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7819 NNS = NestedNameSpecifier::Create(Context, Prev, II);
7823 case NestedNameSpecifier::Namespace: {
7824 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7825 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
7829 case NestedNameSpecifier::NamespaceAlias: {
7830 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7831 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
7835 case NestedNameSpecifier::TypeSpec:
7836 case NestedNameSpecifier::TypeSpecWithTemplate: {
7837 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
7841 bool Template = Record[Idx++];
7842 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
7846 case NestedNameSpecifier::Global: {
7847 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
7848 // No associated value, and there can't be a prefix.
7852 case NestedNameSpecifier::Super: {
7853 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
7854 NNS = NestedNameSpecifier::SuperSpecifier(Context, RD);
7863 NestedNameSpecifierLoc
7864 ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
7866 unsigned N = Record[Idx++];
7867 NestedNameSpecifierLocBuilder Builder;
7868 for (unsigned I = 0; I != N; ++I) {
7869 NestedNameSpecifier::SpecifierKind Kind
7870 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7872 case NestedNameSpecifier::Identifier: {
7873 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7874 SourceRange Range = ReadSourceRange(F, Record, Idx);
7875 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
7879 case NestedNameSpecifier::Namespace: {
7880 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7881 SourceRange Range = ReadSourceRange(F, Record, Idx);
7882 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
7886 case NestedNameSpecifier::NamespaceAlias: {
7887 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7888 SourceRange Range = ReadSourceRange(F, Record, Idx);
7889 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
7893 case NestedNameSpecifier::TypeSpec:
7894 case NestedNameSpecifier::TypeSpecWithTemplate: {
7895 bool Template = Record[Idx++];
7896 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
7898 return NestedNameSpecifierLoc();
7899 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7901 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
7902 Builder.Extend(Context,
7903 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
7904 T->getTypeLoc(), ColonColonLoc);
7908 case NestedNameSpecifier::Global: {
7909 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7910 Builder.MakeGlobal(Context, ColonColonLoc);
7914 case NestedNameSpecifier::Super: {
7915 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
7916 SourceRange Range = ReadSourceRange(F, Record, Idx);
7917 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
7923 return Builder.getWithLocInContext(Context);
7927 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
7929 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
7930 SourceLocation end = ReadSourceLocation(F, Record, Idx);
7931 return SourceRange(beg, end);
7934 /// \brief Read an integral value
7935 llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
7936 unsigned BitWidth = Record[Idx++];
7937 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
7938 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
7943 /// \brief Read a signed integral value
7944 llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
7945 bool isUnsigned = Record[Idx++];
7946 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
7949 /// \brief Read a floating-point value
7950 llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
7951 const llvm::fltSemantics &Sem,
7953 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
7956 // \brief Read a string
7957 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
7958 unsigned Len = Record[Idx++];
7959 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
7964 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
7966 std::string Filename = ReadString(Record, Idx);
7967 ResolveImportedPath(F, Filename);
7971 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
7973 unsigned Major = Record[Idx++];
7974 unsigned Minor = Record[Idx++];
7975 unsigned Subminor = Record[Idx++];
7977 return VersionTuple(Major);
7979 return VersionTuple(Major, Minor - 1);
7980 return VersionTuple(Major, Minor - 1, Subminor - 1);
7983 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
7984 const RecordData &Record,
7986 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
7987 return CXXTemporary::Create(Context, Decl);
7990 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
7991 return Diag(CurrentImportLoc, DiagID);
7994 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
7995 return Diags.Report(Loc, DiagID);
7998 /// \brief Retrieve the identifier table associated with the
8000 IdentifierTable &ASTReader::getIdentifierTable() {
8001 return PP.getIdentifierTable();
8004 /// \brief Record that the given ID maps to the given switch-case
8006 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
8007 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
8008 "Already have a SwitchCase with this ID");
8009 (*CurrSwitchCaseStmts)[ID] = SC;
8012 /// \brief Retrieve the switch-case statement with the given ID.
8013 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
8014 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
8015 return (*CurrSwitchCaseStmts)[ID];
8018 void ASTReader::ClearSwitchCaseIDs() {
8019 CurrSwitchCaseStmts->clear();
8022 void ASTReader::ReadComments() {
8023 std::vector<RawComment *> Comments;
8024 for (SmallVectorImpl<std::pair<BitstreamCursor,
8025 serialization::ModuleFile *> >::iterator
8026 I = CommentsCursors.begin(),
8027 E = CommentsCursors.end();
8030 BitstreamCursor &Cursor = I->first;
8031 serialization::ModuleFile &F = *I->second;
8032 SavedStreamPosition SavedPosition(Cursor);
8036 llvm::BitstreamEntry Entry =
8037 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
8039 switch (Entry.Kind) {
8040 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
8041 case llvm::BitstreamEntry::Error:
8042 Error("malformed block record in AST file");
8044 case llvm::BitstreamEntry::EndBlock:
8046 case llvm::BitstreamEntry::Record:
8047 // The interesting case.
8053 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
8054 case COMMENTS_RAW_COMMENT: {
8056 SourceRange SR = ReadSourceRange(F, Record, Idx);
8057 RawComment::CommentKind Kind =
8058 (RawComment::CommentKind) Record[Idx++];
8059 bool IsTrailingComment = Record[Idx++];
8060 bool IsAlmostTrailingComment = Record[Idx++];
8061 Comments.push_back(new (Context) RawComment(
8062 SR, Kind, IsTrailingComment, IsAlmostTrailingComment,
8063 Context.getLangOpts().CommentOpts.ParseAllComments));
8069 Context.Comments.addDeserializedComments(Comments);
8073 void ASTReader::getInputFiles(ModuleFile &F,
8074 SmallVectorImpl<serialization::InputFile> &Files) {
8075 for (unsigned I = 0, E = F.InputFilesLoaded.size(); I != E; ++I) {
8077 Files.push_back(getInputFile(F, ID));
8081 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
8082 // If we know the owning module, use it.
8083 if (Module *M = D->getImportedOwningModule())
8084 return M->getFullModuleName();
8086 // Otherwise, use the name of the top-level module the decl is within.
8087 if (ModuleFile *M = getOwningModuleFile(D))
8088 return M->ModuleName;
8090 // Not from a module.
8094 void ASTReader::finishPendingActions() {
8095 while (!PendingIdentifierInfos.empty() ||
8096 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
8097 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
8098 !PendingUpdateRecords.empty()) {
8099 // If any identifiers with corresponding top-level declarations have
8100 // been loaded, load those declarations now.
8101 typedef llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >
8103 TopLevelDeclsMap TopLevelDecls;
8105 while (!PendingIdentifierInfos.empty()) {
8106 IdentifierInfo *II = PendingIdentifierInfos.back().first;
8107 SmallVector<uint32_t, 4> DeclIDs =
8108 std::move(PendingIdentifierInfos.back().second);
8109 PendingIdentifierInfos.pop_back();
8111 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
8114 // For each decl chain that we wanted to complete while deserializing, mark
8115 // it as "still needs to be completed".
8116 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
8117 markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
8119 PendingIncompleteDeclChains.clear();
8121 // Load pending declaration chains.
8122 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
8123 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
8124 loadPendingDeclChain(PendingDeclChains[I]);
8126 assert(PendingDeclChainsKnown.empty());
8127 PendingDeclChains.clear();
8129 // Make the most recent of the top-level declarations visible.
8130 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
8131 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
8132 IdentifierInfo *II = TLD->first;
8133 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
8134 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
8138 // Load any pending macro definitions.
8139 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
8140 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
8141 SmallVector<PendingMacroInfo, 2> GlobalIDs;
8142 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
8143 // Initialize the macro history from chained-PCHs ahead of module imports.
8144 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
8146 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
8147 if (Info.M->Kind != MK_ImplicitModule &&
8148 Info.M->Kind != MK_ExplicitModule)
8149 resolvePendingMacro(II, Info);
8151 // Handle module imports.
8152 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
8154 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
8155 if (Info.M->Kind == MK_ImplicitModule ||
8156 Info.M->Kind == MK_ExplicitModule)
8157 resolvePendingMacro(II, Info);
8160 PendingMacroIDs.clear();
8162 // Wire up the DeclContexts for Decls that we delayed setting until
8163 // recursive loading is completed.
8164 while (!PendingDeclContextInfos.empty()) {
8165 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
8166 PendingDeclContextInfos.pop_front();
8167 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
8168 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
8169 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
8172 // Perform any pending declaration updates.
8173 while (!PendingUpdateRecords.empty()) {
8174 auto Update = PendingUpdateRecords.pop_back_val();
8175 ReadingKindTracker ReadingKind(Read_Decl, *this);
8176 loadDeclUpdateRecords(Update.first, Update.second);
8180 // At this point, all update records for loaded decls are in place, so any
8181 // fake class definitions should have become real.
8182 assert(PendingFakeDefinitionData.empty() &&
8183 "faked up a class definition but never saw the real one");
8185 // If we deserialized any C++ or Objective-C class definitions, any
8186 // Objective-C protocol definitions, or any redeclarable templates, make sure
8187 // that all redeclarations point to the definitions. Note that this can only
8188 // happen now, after the redeclaration chains have been fully wired.
8189 for (Decl *D : PendingDefinitions) {
8190 if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
8191 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
8192 // Make sure that the TagType points at the definition.
8193 const_cast<TagType*>(TagT)->decl = TD;
8196 if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
8197 for (auto *R = getMostRecentExistingDecl(RD); R;
8198 R = R->getPreviousDecl()) {
8200 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
8201 "declaration thinks it's the definition but it isn't");
8202 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
8209 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
8210 // Make sure that the ObjCInterfaceType points at the definition.
8211 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
8214 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
8215 cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
8220 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
8221 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
8222 cast<ObjCProtocolDecl>(R)->Data = PD->Data;
8227 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
8228 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
8229 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
8231 PendingDefinitions.clear();
8233 // Load the bodies of any functions or methods we've encountered. We do
8234 // this now (delayed) so that we can be sure that the declaration chains
8235 // have been fully wired up.
8236 // FIXME: There seems to be no point in delaying this, it does not depend
8237 // on the redecl chains having been wired up.
8238 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
8239 PBEnd = PendingBodies.end();
8240 PB != PBEnd; ++PB) {
8241 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
8242 // FIXME: Check for =delete/=default?
8243 // FIXME: Complain about ODR violations here?
8244 if (!getContext().getLangOpts().Modules || !FD->hasBody())
8245 FD->setLazyBody(PB->second);
8249 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
8250 if (!getContext().getLangOpts().Modules || !MD->hasBody())
8251 MD->setLazyBody(PB->second);
8253 PendingBodies.clear();
8256 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
8257 getContext().deduplicateMergedDefinitonsFor(ND);
8258 PendingMergedDefinitionsToDeduplicate.clear();
8261 void ASTReader::diagnoseOdrViolations() {
8262 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty())
8265 // Trigger the import of the full definition of each class that had any
8266 // odr-merging problems, so we can produce better diagnostics for them.
8267 // These updates may in turn find and diagnose some ODR failures, so take
8268 // ownership of the set first.
8269 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
8270 PendingOdrMergeFailures.clear();
8271 for (auto &Merge : OdrMergeFailures) {
8272 Merge.first->buildLookup();
8273 Merge.first->decls_begin();
8274 Merge.first->bases_begin();
8275 Merge.first->vbases_begin();
8276 for (auto *RD : Merge.second) {
8283 // For each declaration from a merged context, check that the canonical
8284 // definition of that context also contains a declaration of the same
8287 // Caution: this loop does things that might invalidate iterators into
8288 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
8289 while (!PendingOdrMergeChecks.empty()) {
8290 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
8292 // FIXME: Skip over implicit declarations for now. This matters for things
8293 // like implicitly-declared special member functions. This isn't entirely
8294 // correct; we can end up with multiple unmerged declarations of the same
8296 if (D->isImplicit())
8299 DeclContext *CanonDef = D->getDeclContext();
8302 const Decl *DCanon = D->getCanonicalDecl();
8304 for (auto RI : D->redecls()) {
8305 if (RI->getLexicalDeclContext() == CanonDef) {
8313 llvm::SmallVector<const NamedDecl*, 4> Candidates;
8314 DeclContext::lookup_result R = CanonDef->lookup(D->getDeclName());
8315 for (DeclContext::lookup_iterator I = R.begin(), E = R.end();
8316 !Found && I != E; ++I) {
8317 for (auto RI : (*I)->redecls()) {
8318 if (RI->getLexicalDeclContext() == CanonDef) {
8319 // This declaration is present in the canonical definition. If it's
8320 // in the same redecl chain, it's the one we're looking for.
8321 if (RI->getCanonicalDecl() == DCanon)
8324 Candidates.push_back(cast<NamedDecl>(RI));
8331 // The AST doesn't like TagDecls becoming invalid after they've been
8332 // completed. We only really need to mark FieldDecls as invalid here.
8333 if (!isa<TagDecl>(D))
8334 D->setInvalidDecl();
8336 // Ensure we don't accidentally recursively enter deserialization while
8337 // we're producing our diagnostic.
8338 Deserializing RecursionGuard(this);
8340 std::string CanonDefModule =
8341 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
8342 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
8343 << D << getOwningModuleNameForDiagnostic(D)
8344 << CanonDef << CanonDefModule.empty() << CanonDefModule;
8346 if (Candidates.empty())
8347 Diag(cast<Decl>(CanonDef)->getLocation(),
8348 diag::note_module_odr_violation_no_possible_decls) << D;
8350 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
8351 Diag(Candidates[I]->getLocation(),
8352 diag::note_module_odr_violation_possible_decl)
8356 DiagnosedOdrMergeFailures.insert(CanonDef);
8360 if (OdrMergeFailures.empty())
8363 // Ensure we don't accidentally recursively enter deserialization while
8364 // we're producing our diagnostics.
8365 Deserializing RecursionGuard(this);
8367 // Issue any pending ODR-failure diagnostics.
8368 for (auto &Merge : OdrMergeFailures) {
8369 // If we've already pointed out a specific problem with this class, don't
8370 // bother issuing a general "something's different" diagnostic.
8371 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
8374 bool Diagnosed = false;
8375 for (auto *RD : Merge.second) {
8376 // Multiple different declarations got merged together; tell the user
8377 // where they came from.
8378 if (Merge.first != RD) {
8379 // FIXME: Walk the definition, figure out what's different,
8380 // and diagnose that.
8382 std::string Module = getOwningModuleNameForDiagnostic(Merge.first);
8383 Diag(Merge.first->getLocation(),
8384 diag::err_module_odr_violation_different_definitions)
8385 << Merge.first << Module.empty() << Module;
8389 Diag(RD->getLocation(),
8390 diag::note_module_odr_violation_different_definitions)
8391 << getOwningModuleNameForDiagnostic(RD);
8396 // All definitions are updates to the same declaration. This happens if a
8397 // module instantiates the declaration of a class template specialization
8398 // and two or more other modules instantiate its definition.
8400 // FIXME: Indicate which modules had instantiations of this definition.
8401 // FIXME: How can this even happen?
8402 Diag(Merge.first->getLocation(),
8403 diag::err_module_odr_violation_different_instantiations)
8409 void ASTReader::StartedDeserializing() {
8410 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
8411 ReadTimer->startTimer();
8414 void ASTReader::FinishedDeserializing() {
8415 assert(NumCurrentElementsDeserializing &&
8416 "FinishedDeserializing not paired with StartedDeserializing");
8417 if (NumCurrentElementsDeserializing == 1) {
8418 // We decrease NumCurrentElementsDeserializing only after pending actions
8419 // are finished, to avoid recursively re-calling finishPendingActions().
8420 finishPendingActions();
8422 --NumCurrentElementsDeserializing;
8424 if (NumCurrentElementsDeserializing == 0) {
8425 // Propagate exception specification updates along redeclaration chains.
8426 while (!PendingExceptionSpecUpdates.empty()) {
8427 auto Updates = std::move(PendingExceptionSpecUpdates);
8428 PendingExceptionSpecUpdates.clear();
8429 for (auto Update : Updates) {
8430 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
8431 SemaObj->UpdateExceptionSpec(Update.second,
8432 FPT->getExtProtoInfo().ExceptionSpec);
8436 diagnoseOdrViolations();
8439 ReadTimer->stopTimer();
8441 // We are not in recursive loading, so it's safe to pass the "interesting"
8442 // decls to the consumer.
8444 PassInterestingDeclsToConsumer();
8448 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
8449 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
8450 // Remove any fake results before adding any real ones.
8451 auto It = PendingFakeLookupResults.find(II);
8452 if (It != PendingFakeLookupResults.end()) {
8453 for (auto *ND : PendingFakeLookupResults[II])
8454 SemaObj->IdResolver.RemoveDecl(ND);
8455 // FIXME: this works around module+PCH performance issue.
8456 // Rather than erase the result from the map, which is O(n), just clear
8457 // the vector of NamedDecls.
8462 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
8463 SemaObj->TUScope->AddDecl(D);
8464 } else if (SemaObj->TUScope) {
8465 // Adding the decl to IdResolver may have failed because it was already in
8466 // (even though it was not added in scope). If it is already in, make sure
8467 // it gets in the scope as well.
8468 if (std::find(SemaObj->IdResolver.begin(Name),
8469 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
8470 SemaObj->TUScope->AddDecl(D);
8474 ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
8475 const PCHContainerReader &PCHContainerRdr,
8476 StringRef isysroot, bool DisableValidation,
8477 bool AllowASTWithCompilerErrors,
8478 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
8479 bool UseGlobalIndex,
8480 std::unique_ptr<llvm::Timer> ReadTimer)
8481 : Listener(new PCHValidator(PP, *this)), DeserializationListener(nullptr),
8482 OwnsDeserializationListener(false), SourceMgr(PP.getSourceManager()),
8483 FileMgr(PP.getFileManager()), PCHContainerRdr(PCHContainerRdr),
8484 Diags(PP.getDiagnostics()), SemaObj(nullptr), PP(PP), Context(Context),
8485 Consumer(nullptr), ModuleMgr(PP.getFileManager(), PCHContainerRdr),
8486 ReadTimer(std::move(ReadTimer)),
8487 isysroot(isysroot), DisableValidation(DisableValidation),
8488 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
8489 AllowConfigurationMismatch(AllowConfigurationMismatch),
8490 ValidateSystemInputs(ValidateSystemInputs),
8491 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
8492 CurrSwitchCaseStmts(&SwitchCaseStmts), NumSLocEntriesRead(0),
8493 TotalNumSLocEntries(0), NumStatementsRead(0), TotalNumStatements(0),
8494 NumMacrosRead(0), TotalNumMacros(0), NumIdentifierLookups(0),
8495 NumIdentifierLookupHits(0), NumSelectorsRead(0),
8496 NumMethodPoolEntriesRead(0), NumMethodPoolLookups(0),
8497 NumMethodPoolHits(0), NumMethodPoolTableLookups(0),
8498 NumMethodPoolTableHits(0), TotalNumMethodPoolEntries(0),
8499 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
8500 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
8501 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
8502 PassingDeclsToConsumer(false), ReadingKind(Read_None) {
8503 SourceMgr.setExternalSLocEntrySource(this);
8506 ASTReader::~ASTReader() {
8507 if (OwnsDeserializationListener)
8508 delete DeserializationListener;
8510 for (DeclContextVisibleUpdatesPending::iterator
8511 I = PendingVisibleUpdates.begin(),
8512 E = PendingVisibleUpdates.end();
8514 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
8515 F = I->second.end();