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/AST/NestedNameSpecifier.h"
23 #include "clang/AST/Type.h"
24 #include "clang/AST/TypeLocVisitor.h"
25 #include "clang/Basic/DiagnosticOptions.h"
26 #include "clang/Basic/FileManager.h"
27 #include "clang/Basic/SourceManager.h"
28 #include "clang/Basic/SourceManagerInternals.h"
29 #include "clang/Basic/TargetInfo.h"
30 #include "clang/Basic/TargetOptions.h"
31 #include "clang/Basic/Version.h"
32 #include "clang/Basic/VersionTuple.h"
33 #include "clang/Frontend/Utils.h"
34 #include "clang/Lex/HeaderSearch.h"
35 #include "clang/Lex/HeaderSearchOptions.h"
36 #include "clang/Lex/MacroInfo.h"
37 #include "clang/Lex/PreprocessingRecord.h"
38 #include "clang/Lex/Preprocessor.h"
39 #include "clang/Lex/PreprocessorOptions.h"
40 #include "clang/Sema/Scope.h"
41 #include "clang/Sema/Sema.h"
42 #include "clang/Serialization/ASTDeserializationListener.h"
43 #include "clang/Serialization/GlobalModuleIndex.h"
44 #include "clang/Serialization/ModuleManager.h"
45 #include "clang/Serialization/SerializationDiagnostic.h"
46 #include "llvm/ADT/Hashing.h"
47 #include "llvm/ADT/StringExtras.h"
48 #include "llvm/Bitcode/BitstreamReader.h"
49 #include "llvm/Support/ErrorHandling.h"
50 #include "llvm/Support/FileSystem.h"
51 #include "llvm/Support/MemoryBuffer.h"
52 #include "llvm/Support/Path.h"
53 #include "llvm/Support/SaveAndRestore.h"
54 #include "llvm/Support/raw_ostream.h"
58 #include <system_error>
60 using namespace clang;
61 using namespace clang::serialization;
62 using namespace clang::serialization::reader;
63 using llvm::BitstreamCursor;
66 //===----------------------------------------------------------------------===//
67 // ChainedASTReaderListener implementation
68 //===----------------------------------------------------------------------===//
71 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
72 return First->ReadFullVersionInformation(FullVersion) ||
73 Second->ReadFullVersionInformation(FullVersion);
75 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
76 First->ReadModuleName(ModuleName);
77 Second->ReadModuleName(ModuleName);
79 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
80 First->ReadModuleMapFile(ModuleMapPath);
81 Second->ReadModuleMapFile(ModuleMapPath);
83 bool ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
85 return First->ReadLanguageOptions(LangOpts, Complain) ||
86 Second->ReadLanguageOptions(LangOpts, Complain);
89 ChainedASTReaderListener::ReadTargetOptions(const TargetOptions &TargetOpts,
91 return First->ReadTargetOptions(TargetOpts, Complain) ||
92 Second->ReadTargetOptions(TargetOpts, Complain);
94 bool ChainedASTReaderListener::ReadDiagnosticOptions(
95 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
96 return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
97 Second->ReadDiagnosticOptions(DiagOpts, Complain);
100 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
102 return First->ReadFileSystemOptions(FSOpts, Complain) ||
103 Second->ReadFileSystemOptions(FSOpts, Complain);
106 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
107 const HeaderSearchOptions &HSOpts, bool Complain) {
108 return First->ReadHeaderSearchOptions(HSOpts, Complain) ||
109 Second->ReadHeaderSearchOptions(HSOpts, Complain);
111 bool ChainedASTReaderListener::ReadPreprocessorOptions(
112 const PreprocessorOptions &PPOpts, bool Complain,
113 std::string &SuggestedPredefines) {
114 return First->ReadPreprocessorOptions(PPOpts, Complain,
115 SuggestedPredefines) ||
116 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
118 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
120 First->ReadCounter(M, Value);
121 Second->ReadCounter(M, Value);
123 bool ChainedASTReaderListener::needsInputFileVisitation() {
124 return First->needsInputFileVisitation() ||
125 Second->needsInputFileVisitation();
127 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
128 return First->needsSystemInputFileVisitation() ||
129 Second->needsSystemInputFileVisitation();
131 void ChainedASTReaderListener::visitModuleFile(StringRef Filename) {
132 First->visitModuleFile(Filename);
133 Second->visitModuleFile(Filename);
135 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
138 bool Continue = false;
139 if (First->needsInputFileVisitation() &&
140 (!isSystem || First->needsSystemInputFileVisitation()))
141 Continue |= First->visitInputFile(Filename, isSystem, isOverridden);
142 if (Second->needsInputFileVisitation() &&
143 (!isSystem || Second->needsSystemInputFileVisitation()))
144 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden);
148 //===----------------------------------------------------------------------===//
149 // PCH validator implementation
150 //===----------------------------------------------------------------------===//
152 ASTReaderListener::~ASTReaderListener() {}
154 /// \brief Compare the given set of language options against an existing set of
155 /// language options.
157 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
159 /// \returns true if the languagae options mis-match, false otherwise.
160 static bool checkLanguageOptions(const LangOptions &LangOpts,
161 const LangOptions &ExistingLangOpts,
162 DiagnosticsEngine *Diags) {
163 #define LANGOPT(Name, Bits, Default, Description) \
164 if (ExistingLangOpts.Name != LangOpts.Name) { \
166 Diags->Report(diag::err_pch_langopt_mismatch) \
167 << Description << LangOpts.Name << ExistingLangOpts.Name; \
171 #define VALUE_LANGOPT(Name, Bits, Default, Description) \
172 if (ExistingLangOpts.Name != LangOpts.Name) { \
174 Diags->Report(diag::err_pch_langopt_value_mismatch) \
179 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
180 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
182 Diags->Report(diag::err_pch_langopt_value_mismatch) \
187 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
188 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
189 #include "clang/Basic/LangOptions.def"
191 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
193 Diags->Report(diag::err_pch_langopt_value_mismatch)
194 << "target Objective-C runtime";
198 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
199 LangOpts.CommentOpts.BlockCommandNames) {
201 Diags->Report(diag::err_pch_langopt_value_mismatch)
202 << "block command names";
209 /// \brief Compare the given set of target options against an existing set of
212 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
214 /// \returns true if the target options mis-match, false otherwise.
215 static bool checkTargetOptions(const TargetOptions &TargetOpts,
216 const TargetOptions &ExistingTargetOpts,
217 DiagnosticsEngine *Diags) {
218 #define CHECK_TARGET_OPT(Field, Name) \
219 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
221 Diags->Report(diag::err_pch_targetopt_mismatch) \
222 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
226 CHECK_TARGET_OPT(Triple, "target");
227 CHECK_TARGET_OPT(CPU, "target CPU");
228 CHECK_TARGET_OPT(ABI, "target ABI");
229 #undef CHECK_TARGET_OPT
231 // Compare feature sets.
232 SmallVector<StringRef, 4> ExistingFeatures(
233 ExistingTargetOpts.FeaturesAsWritten.begin(),
234 ExistingTargetOpts.FeaturesAsWritten.end());
235 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
236 TargetOpts.FeaturesAsWritten.end());
237 std::sort(ExistingFeatures.begin(), ExistingFeatures.end());
238 std::sort(ReadFeatures.begin(), ReadFeatures.end());
240 unsigned ExistingIdx = 0, ExistingN = ExistingFeatures.size();
241 unsigned ReadIdx = 0, ReadN = ReadFeatures.size();
242 while (ExistingIdx < ExistingN && ReadIdx < ReadN) {
243 if (ExistingFeatures[ExistingIdx] == ReadFeatures[ReadIdx]) {
249 if (ReadFeatures[ReadIdx] < ExistingFeatures[ExistingIdx]) {
251 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
252 << false << ReadFeatures[ReadIdx];
257 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
258 << true << ExistingFeatures[ExistingIdx];
262 if (ExistingIdx < ExistingN) {
264 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
265 << true << ExistingFeatures[ExistingIdx];
269 if (ReadIdx < ReadN) {
271 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
272 << false << ReadFeatures[ReadIdx];
280 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
282 const LangOptions &ExistingLangOpts = PP.getLangOpts();
283 return checkLanguageOptions(LangOpts, ExistingLangOpts,
284 Complain? &Reader.Diags : nullptr);
287 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
289 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
290 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
291 Complain? &Reader.Diags : nullptr);
295 typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >
297 typedef llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> >
301 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
302 DiagnosticsEngine &Diags,
304 typedef DiagnosticsEngine::Level Level;
306 // Check current mappings for new -Werror mappings, and the stored mappings
307 // for cases that were explicitly mapped to *not* be errors that are now
308 // errors because of options like -Werror.
309 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
311 for (DiagnosticsEngine *MappingSource : MappingSources) {
312 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
313 diag::kind DiagID = DiagIDMappingPair.first;
314 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
315 if (CurLevel < DiagnosticsEngine::Error)
316 continue; // not significant
318 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
319 if (StoredLevel < DiagnosticsEngine::Error) {
321 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
322 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
331 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
332 diag::Severity Ext = Diags.getExtensionHandlingBehavior();
333 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
335 return Ext >= diag::Severity::Error;
338 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
339 DiagnosticsEngine &Diags,
340 bool IsSystem, bool Complain) {
343 if (Diags.getSuppressSystemWarnings())
345 // If -Wsystem-headers was not enabled before, be conservative
346 if (StoredDiags.getSuppressSystemWarnings()) {
348 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
353 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
355 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
359 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
360 !StoredDiags.getEnableAllWarnings()) {
362 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
366 if (isExtHandlingFromDiagsError(Diags) &&
367 !isExtHandlingFromDiagsError(StoredDiags)) {
369 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
373 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
376 bool PCHValidator::ReadDiagnosticOptions(
377 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
378 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
379 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
380 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
381 new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
382 // This should never fail, because we would have processed these options
383 // before writing them to an ASTFile.
384 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
386 ModuleManager &ModuleMgr = Reader.getModuleManager();
387 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
389 // If the original import came from a file explicitly generated by the user,
390 // don't check the diagnostic mappings.
391 // FIXME: currently this is approximated by checking whether this is not a
393 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
394 // the transitive closure of its imports, since unrelated modules cannot be
395 // imported until after this module finishes validation.
396 ModuleFile *TopImport = *ModuleMgr.rbegin();
397 while (!TopImport->ImportedBy.empty())
398 TopImport = TopImport->ImportedBy[0];
399 if (TopImport->Kind != MK_Module)
402 StringRef ModuleName = TopImport->ModuleName;
403 assert(!ModuleName.empty() && "diagnostic options read before module name");
405 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
406 assert(M && "missing module");
408 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
409 // contains the union of their flags.
410 return checkDiagnosticMappings(*Diags, ExistingDiags, M->IsSystem, Complain);
413 /// \brief Collect the macro definitions provided by the given preprocessor
416 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
417 MacroDefinitionsMap &Macros,
418 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
419 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
420 StringRef Macro = PPOpts.Macros[I].first;
421 bool IsUndef = PPOpts.Macros[I].second;
423 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
424 StringRef MacroName = MacroPair.first;
425 StringRef MacroBody = MacroPair.second;
427 // For an #undef'd macro, we only care about the name.
429 if (MacroNames && !Macros.count(MacroName))
430 MacroNames->push_back(MacroName);
432 Macros[MacroName] = std::make_pair("", true);
436 // For a #define'd macro, figure out the actual definition.
437 if (MacroName.size() == Macro.size())
440 // Note: GCC drops anything following an end-of-line character.
441 StringRef::size_type End = MacroBody.find_first_of("\n\r");
442 MacroBody = MacroBody.substr(0, End);
445 if (MacroNames && !Macros.count(MacroName))
446 MacroNames->push_back(MacroName);
447 Macros[MacroName] = std::make_pair(MacroBody, false);
451 /// \brief Check the preprocessor options deserialized from the control block
452 /// against the preprocessor options in an existing preprocessor.
454 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
455 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
456 const PreprocessorOptions &ExistingPPOpts,
457 DiagnosticsEngine *Diags,
458 FileManager &FileMgr,
459 std::string &SuggestedPredefines,
460 const LangOptions &LangOpts) {
461 // Check macro definitions.
462 MacroDefinitionsMap ASTFileMacros;
463 collectMacroDefinitions(PPOpts, ASTFileMacros);
464 MacroDefinitionsMap ExistingMacros;
465 SmallVector<StringRef, 4> ExistingMacroNames;
466 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
468 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
469 // Dig out the macro definition in the existing preprocessor options.
470 StringRef MacroName = ExistingMacroNames[I];
471 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
473 // Check whether we know anything about this macro name or not.
474 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known
475 = ASTFileMacros.find(MacroName);
476 if (Known == ASTFileMacros.end()) {
477 // FIXME: Check whether this identifier was referenced anywhere in the
478 // AST file. If so, we should reject the AST file. Unfortunately, this
479 // information isn't in the control block. What shall we do about it?
481 if (Existing.second) {
482 SuggestedPredefines += "#undef ";
483 SuggestedPredefines += MacroName.str();
484 SuggestedPredefines += '\n';
486 SuggestedPredefines += "#define ";
487 SuggestedPredefines += MacroName.str();
488 SuggestedPredefines += ' ';
489 SuggestedPredefines += Existing.first.str();
490 SuggestedPredefines += '\n';
495 // If the macro was defined in one but undef'd in the other, we have a
497 if (Existing.second != Known->second.second) {
499 Diags->Report(diag::err_pch_macro_def_undef)
500 << MacroName << Known->second.second;
505 // If the macro was #undef'd in both, or if the macro bodies are identical,
507 if (Existing.second || Existing.first == Known->second.first)
510 // The macro bodies differ; complain.
512 Diags->Report(diag::err_pch_macro_def_conflict)
513 << MacroName << Known->second.first << Existing.first;
518 // Check whether we're using predefines.
519 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) {
521 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
526 // Detailed record is important since it is used for the module cache hash.
527 if (LangOpts.Modules &&
528 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord) {
530 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
535 // Compute the #include and #include_macros lines we need.
536 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
537 StringRef File = ExistingPPOpts.Includes[I];
538 if (File == ExistingPPOpts.ImplicitPCHInclude)
541 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
542 != PPOpts.Includes.end())
545 SuggestedPredefines += "#include \"";
546 SuggestedPredefines += File;
547 SuggestedPredefines += "\"\n";
550 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
551 StringRef File = ExistingPPOpts.MacroIncludes[I];
552 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
554 != PPOpts.MacroIncludes.end())
557 SuggestedPredefines += "#__include_macros \"";
558 SuggestedPredefines += File;
559 SuggestedPredefines += "\"\n##\n";
565 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
567 std::string &SuggestedPredefines) {
568 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
570 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
571 Complain? &Reader.Diags : nullptr,
577 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
578 PP.setCounterValue(Value);
581 //===----------------------------------------------------------------------===//
582 // AST reader implementation
583 //===----------------------------------------------------------------------===//
585 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
586 bool TakeOwnership) {
587 DeserializationListener = Listener;
588 OwnsDeserializationListener = TakeOwnership;
593 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
594 return serialization::ComputeHash(Sel);
598 std::pair<unsigned, unsigned>
599 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
600 using namespace llvm::support;
601 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
602 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
603 return std::make_pair(KeyLen, DataLen);
606 ASTSelectorLookupTrait::internal_key_type
607 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
608 using namespace llvm::support;
609 SelectorTable &SelTable = Reader.getContext().Selectors;
610 unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
611 IdentifierInfo *FirstII = Reader.getLocalIdentifier(
612 F, endian::readNext<uint32_t, little, unaligned>(d));
614 return SelTable.getNullarySelector(FirstII);
616 return SelTable.getUnarySelector(FirstII);
618 SmallVector<IdentifierInfo *, 16> Args;
619 Args.push_back(FirstII);
620 for (unsigned I = 1; I != N; ++I)
621 Args.push_back(Reader.getLocalIdentifier(
622 F, endian::readNext<uint32_t, little, unaligned>(d)));
624 return SelTable.getSelector(N, Args.data());
627 ASTSelectorLookupTrait::data_type
628 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
630 using namespace llvm::support;
634 Result.ID = Reader.getGlobalSelectorID(
635 F, endian::readNext<uint32_t, little, unaligned>(d));
636 unsigned NumInstanceMethodsAndBits =
637 endian::readNext<uint16_t, little, unaligned>(d);
638 unsigned NumFactoryMethodsAndBits =
639 endian::readNext<uint16_t, little, unaligned>(d);
640 Result.InstanceBits = NumInstanceMethodsAndBits & 0x3;
641 Result.FactoryBits = NumFactoryMethodsAndBits & 0x3;
642 unsigned NumInstanceMethods = NumInstanceMethodsAndBits >> 2;
643 unsigned NumFactoryMethods = NumFactoryMethodsAndBits >> 2;
645 // Load instance methods
646 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
647 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
648 F, endian::readNext<uint32_t, little, unaligned>(d)))
649 Result.Instance.push_back(Method);
652 // Load factory methods
653 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
654 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
655 F, endian::readNext<uint32_t, little, unaligned>(d)))
656 Result.Factory.push_back(Method);
662 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
663 return llvm::HashString(a);
666 std::pair<unsigned, unsigned>
667 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
668 using namespace llvm::support;
669 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
670 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
671 return std::make_pair(KeyLen, DataLen);
674 ASTIdentifierLookupTraitBase::internal_key_type
675 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
676 assert(n >= 2 && d[n-1] == '\0');
677 return StringRef((const char*) d, n-1);
680 /// \brief Whether the given identifier is "interesting".
681 static bool isInterestingIdentifier(IdentifierInfo &II) {
682 return II.isPoisoned() ||
683 II.isExtensionToken() ||
684 II.getObjCOrBuiltinID() ||
685 II.hasRevertedTokenIDToIdentifier() ||
686 II.hadMacroDefinition() ||
687 II.getFETokenInfo<void>();
690 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
691 const unsigned char* d,
693 using namespace llvm::support;
694 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
695 bool IsInteresting = RawID & 0x01;
697 // Wipe out the "is interesting" bit.
700 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
701 if (!IsInteresting) {
702 // For uninteresting identifiers, just build the IdentifierInfo
703 // and associate it with the persistent ID.
704 IdentifierInfo *II = KnownII;
706 II = &Reader.getIdentifierTable().getOwn(k);
709 Reader.SetIdentifierInfo(ID, II);
710 if (!II->isFromAST()) {
711 bool WasInteresting = isInterestingIdentifier(*II);
714 II->setChangedSinceDeserialization();
716 Reader.markIdentifierUpToDate(II);
720 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
721 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
722 bool CPlusPlusOperatorKeyword = Bits & 0x01;
724 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
726 bool Poisoned = Bits & 0x01;
728 bool ExtensionToken = Bits & 0x01;
730 bool hasSubmoduleMacros = Bits & 0x01;
732 bool hadMacroDefinition = Bits & 0x01;
735 assert(Bits == 0 && "Extra bits in the identifier?");
738 // Build the IdentifierInfo itself and link the identifier ID with
739 // the new IdentifierInfo.
740 IdentifierInfo *II = KnownII;
742 II = &Reader.getIdentifierTable().getOwn(StringRef(k));
745 Reader.markIdentifierUpToDate(II);
746 if (!II->isFromAST()) {
747 bool WasInteresting = isInterestingIdentifier(*II);
750 II->setChangedSinceDeserialization();
753 // Set or check the various bits in the IdentifierInfo structure.
754 // Token IDs are read-only.
755 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
756 II->RevertTokenIDToIdentifier();
757 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
758 assert(II->isExtensionToken() == ExtensionToken &&
759 "Incorrect extension token flag");
760 (void)ExtensionToken;
762 II->setIsPoisoned(true);
763 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
764 "Incorrect C++ operator keyword flag");
765 (void)CPlusPlusOperatorKeyword;
767 // If this identifier is a macro, deserialize the macro
769 if (hadMacroDefinition) {
770 uint32_t MacroDirectivesOffset =
771 endian::readNext<uint32_t, little, unaligned>(d);
773 SmallVector<uint32_t, 8> LocalMacroIDs;
774 if (hasSubmoduleMacros) {
776 uint32_t LocalMacroID =
777 endian::readNext<uint32_t, little, unaligned>(d);
779 if (LocalMacroID == 0xdeadbeef) break;
780 LocalMacroIDs.push_back(LocalMacroID);
784 if (F.Kind == MK_Module) {
785 // Macro definitions are stored from newest to oldest, so reverse them
786 // before registering them.
787 llvm::SmallVector<unsigned, 8> MacroSizes;
788 for (SmallVectorImpl<uint32_t>::iterator
789 I = LocalMacroIDs.begin(), E = LocalMacroIDs.end(); I != E; /**/) {
792 static const uint32_t HasOverridesFlag = 0x80000000U;
793 if (I + 1 != E && (I[1] & HasOverridesFlag))
794 Size += 1 + (I[1] & ~HasOverridesFlag);
796 MacroSizes.push_back(Size);
800 SmallVectorImpl<uint32_t>::iterator I = LocalMacroIDs.end();
801 for (SmallVectorImpl<unsigned>::reverse_iterator SI = MacroSizes.rbegin(),
802 SE = MacroSizes.rend();
806 uint32_t LocalMacroID = *I;
807 ArrayRef<uint32_t> Overrides;
809 Overrides = llvm::makeArrayRef(&I[2], *SI - 2);
810 Reader.addPendingMacroFromModule(II, &F, LocalMacroID, Overrides);
812 assert(I == LocalMacroIDs.begin());
814 Reader.addPendingMacroFromPCH(II, &F, MacroDirectivesOffset);
818 Reader.SetIdentifierInfo(ID, II);
820 // Read all of the declarations visible at global scope with this
823 SmallVector<uint32_t, 4> DeclIDs;
824 for (; DataLen > 0; DataLen -= 4)
825 DeclIDs.push_back(Reader.getGlobalDeclID(
826 F, endian::readNext<uint32_t, little, unaligned>(d)));
827 Reader.SetGloballyVisibleDecls(II, DeclIDs);
834 ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
835 llvm::FoldingSetNodeID ID;
836 ID.AddInteger(Key.Kind);
839 case DeclarationName::Identifier:
840 case DeclarationName::CXXLiteralOperatorName:
841 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
843 case DeclarationName::ObjCZeroArgSelector:
844 case DeclarationName::ObjCOneArgSelector:
845 case DeclarationName::ObjCMultiArgSelector:
846 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
848 case DeclarationName::CXXOperatorName:
849 ID.AddInteger((OverloadedOperatorKind)Key.Data);
851 case DeclarationName::CXXConstructorName:
852 case DeclarationName::CXXDestructorName:
853 case DeclarationName::CXXConversionFunctionName:
854 case DeclarationName::CXXUsingDirective:
858 return ID.ComputeHash();
861 ASTDeclContextNameLookupTrait::internal_key_type
862 ASTDeclContextNameLookupTrait::GetInternalKey(
863 const external_key_type& Name) const {
865 Key.Kind = Name.getNameKind();
866 switch (Name.getNameKind()) {
867 case DeclarationName::Identifier:
868 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
870 case DeclarationName::ObjCZeroArgSelector:
871 case DeclarationName::ObjCOneArgSelector:
872 case DeclarationName::ObjCMultiArgSelector:
873 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
875 case DeclarationName::CXXOperatorName:
876 Key.Data = Name.getCXXOverloadedOperator();
878 case DeclarationName::CXXLiteralOperatorName:
879 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
881 case DeclarationName::CXXConstructorName:
882 case DeclarationName::CXXDestructorName:
883 case DeclarationName::CXXConversionFunctionName:
884 case DeclarationName::CXXUsingDirective:
892 std::pair<unsigned, unsigned>
893 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
894 using namespace llvm::support;
895 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
896 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
897 return std::make_pair(KeyLen, DataLen);
900 ASTDeclContextNameLookupTrait::internal_key_type
901 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
902 using namespace llvm::support;
905 Key.Kind = (DeclarationName::NameKind)*d++;
907 case DeclarationName::Identifier:
908 Key.Data = (uint64_t)Reader.getLocalIdentifier(
909 F, endian::readNext<uint32_t, little, unaligned>(d));
911 case DeclarationName::ObjCZeroArgSelector:
912 case DeclarationName::ObjCOneArgSelector:
913 case DeclarationName::ObjCMultiArgSelector:
915 (uint64_t)Reader.getLocalSelector(
916 F, endian::readNext<uint32_t, little, unaligned>(
917 d)).getAsOpaquePtr();
919 case DeclarationName::CXXOperatorName:
920 Key.Data = *d++; // OverloadedOperatorKind
922 case DeclarationName::CXXLiteralOperatorName:
923 Key.Data = (uint64_t)Reader.getLocalIdentifier(
924 F, endian::readNext<uint32_t, little, unaligned>(d));
926 case DeclarationName::CXXConstructorName:
927 case DeclarationName::CXXDestructorName:
928 case DeclarationName::CXXConversionFunctionName:
929 case DeclarationName::CXXUsingDirective:
937 ASTDeclContextNameLookupTrait::data_type
938 ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
939 const unsigned char* d,
941 using namespace llvm::support;
942 unsigned NumDecls = endian::readNext<uint16_t, little, unaligned>(d);
943 LE32DeclID *Start = reinterpret_cast<LE32DeclID *>(
944 const_cast<unsigned char *>(d));
945 return std::make_pair(Start, Start + NumDecls);
948 bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
949 BitstreamCursor &Cursor,
950 const std::pair<uint64_t, uint64_t> &Offsets,
951 DeclContextInfo &Info) {
952 SavedStreamPosition SavedPosition(Cursor);
953 // First the lexical decls.
954 if (Offsets.first != 0) {
955 Cursor.JumpToBit(Offsets.first);
959 unsigned Code = Cursor.ReadCode();
960 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
961 if (RecCode != DECL_CONTEXT_LEXICAL) {
962 Error("Expected lexical block");
966 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob.data());
967 Info.NumLexicalDecls = Blob.size() / sizeof(KindDeclIDPair);
970 // Now the lookup table.
971 if (Offsets.second != 0) {
972 Cursor.JumpToBit(Offsets.second);
976 unsigned Code = Cursor.ReadCode();
977 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
978 if (RecCode != DECL_CONTEXT_VISIBLE) {
979 Error("Expected visible lookup table block");
982 Info.NameLookupTableData = ASTDeclContextNameLookupTable::Create(
983 (const unsigned char *)Blob.data() + Record[0],
984 (const unsigned char *)Blob.data() + sizeof(uint32_t),
985 (const unsigned char *)Blob.data(),
986 ASTDeclContextNameLookupTrait(*this, M));
992 void ASTReader::Error(StringRef Msg) {
993 Error(diag::err_fe_pch_malformed, Msg);
994 if (Context.getLangOpts().Modules && !Diags.isDiagnosticInFlight()) {
995 Diag(diag::note_module_cache_path)
996 << PP.getHeaderSearchInfo().getModuleCachePath();
1000 void ASTReader::Error(unsigned DiagID,
1001 StringRef Arg1, StringRef Arg2) {
1002 if (Diags.isDiagnosticInFlight())
1003 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
1005 Diag(DiagID) << Arg1 << Arg2;
1008 //===----------------------------------------------------------------------===//
1009 // Source Manager Deserialization
1010 //===----------------------------------------------------------------------===//
1012 /// \brief Read the line table in the source manager block.
1013 /// \returns true if there was an error.
1014 bool ASTReader::ParseLineTable(ModuleFile &F,
1015 SmallVectorImpl<uint64_t> &Record) {
1017 LineTableInfo &LineTable = SourceMgr.getLineTable();
1019 // Parse the file names
1020 std::map<int, int> FileIDs;
1021 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
1022 // Extract the file name
1023 unsigned FilenameLen = Record[Idx++];
1024 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1026 MaybeAddSystemRootToFilename(F, Filename);
1027 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1030 // Parse the line entries
1031 std::vector<LineEntry> Entries;
1032 while (Idx < Record.size()) {
1033 int FID = Record[Idx++];
1034 assert(FID >= 0 && "Serialized line entries for non-local file.");
1035 // Remap FileID from 1-based old view.
1036 FID += F.SLocEntryBaseID - 1;
1038 // Extract the line entries
1039 unsigned NumEntries = Record[Idx++];
1040 assert(NumEntries && "Numentries is 00000");
1042 Entries.reserve(NumEntries);
1043 for (unsigned I = 0; I != NumEntries; ++I) {
1044 unsigned FileOffset = Record[Idx++];
1045 unsigned LineNo = Record[Idx++];
1046 int FilenameID = FileIDs[Record[Idx++]];
1047 SrcMgr::CharacteristicKind FileKind
1048 = (SrcMgr::CharacteristicKind)Record[Idx++];
1049 unsigned IncludeOffset = Record[Idx++];
1050 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1051 FileKind, IncludeOffset));
1053 LineTable.AddEntry(FileID::get(FID), Entries);
1059 /// \brief Read a source manager block
1060 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1061 using namespace SrcMgr;
1063 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1065 // Set the source-location entry cursor to the current position in
1066 // the stream. This cursor will be used to read the contents of the
1067 // source manager block initially, and then lazily read
1068 // source-location entries as needed.
1069 SLocEntryCursor = F.Stream;
1071 // The stream itself is going to skip over the source manager block.
1072 if (F.Stream.SkipBlock()) {
1073 Error("malformed block record in AST file");
1077 // Enter the source manager block.
1078 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1079 Error("malformed source manager block record in AST file");
1085 llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
1088 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1089 case llvm::BitstreamEntry::Error:
1090 Error("malformed block record in AST file");
1092 case llvm::BitstreamEntry::EndBlock:
1094 case llvm::BitstreamEntry::Record:
1095 // The interesting case.
1102 switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
1103 default: // Default behavior: ignore.
1106 case SM_SLOC_FILE_ENTRY:
1107 case SM_SLOC_BUFFER_ENTRY:
1108 case SM_SLOC_EXPANSION_ENTRY:
1109 // Once we hit one of the source location entries, we're done.
1115 /// \brief If a header file is not found at the path that we expect it to be
1116 /// and the PCH file was moved from its original location, try to resolve the
1117 /// file by assuming that header+PCH were moved together and the header is in
1118 /// the same place relative to the PCH.
1120 resolveFileRelativeToOriginalDir(const std::string &Filename,
1121 const std::string &OriginalDir,
1122 const std::string &CurrDir) {
1123 assert(OriginalDir != CurrDir &&
1124 "No point trying to resolve the file if the PCH dir didn't change");
1125 using namespace llvm::sys;
1126 SmallString<128> filePath(Filename);
1127 fs::make_absolute(filePath);
1128 assert(path::is_absolute(OriginalDir));
1129 SmallString<128> currPCHPath(CurrDir);
1131 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1132 fileDirE = path::end(path::parent_path(filePath));
1133 path::const_iterator origDirI = path::begin(OriginalDir),
1134 origDirE = path::end(OriginalDir);
1135 // Skip the common path components from filePath and OriginalDir.
1136 while (fileDirI != fileDirE && origDirI != origDirE &&
1137 *fileDirI == *origDirI) {
1141 for (; origDirI != origDirE; ++origDirI)
1142 path::append(currPCHPath, "..");
1143 path::append(currPCHPath, fileDirI, fileDirE);
1144 path::append(currPCHPath, path::filename(Filename));
1145 return currPCHPath.str();
1148 bool ASTReader::ReadSLocEntry(int ID) {
1152 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1153 Error("source location entry ID out-of-range for AST file");
1157 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1158 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
1159 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1160 unsigned BaseOffset = F->SLocEntryBaseOffset;
1162 ++NumSLocEntriesRead;
1163 llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
1164 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1165 Error("incorrectly-formatted source location entry in AST file");
1171 switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
1173 Error("incorrectly-formatted source location entry in AST file");
1176 case SM_SLOC_FILE_ENTRY: {
1177 // We will detect whether a file changed and return 'Failure' for it, but
1178 // we will also try to fail gracefully by setting up the SLocEntry.
1179 unsigned InputID = Record[4];
1180 InputFile IF = getInputFile(*F, InputID);
1181 const FileEntry *File = IF.getFile();
1182 bool OverriddenBuffer = IF.isOverridden();
1184 // Note that we only check if a File was returned. If it was out-of-date
1185 // we have complained but we will continue creating a FileID to recover
1190 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1191 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1192 // This is the module's main file.
1193 IncludeLoc = getImportLocation(F);
1195 SrcMgr::CharacteristicKind
1196 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1197 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1198 ID, BaseOffset + Record[0]);
1199 SrcMgr::FileInfo &FileInfo =
1200 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1201 FileInfo.NumCreatedFIDs = Record[5];
1203 FileInfo.setHasLineDirectives();
1205 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1206 unsigned NumFileDecls = Record[7];
1208 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1209 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1213 const SrcMgr::ContentCache *ContentCache
1214 = SourceMgr.getOrCreateContentCache(File,
1215 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
1216 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1217 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
1218 unsigned Code = SLocEntryCursor.ReadCode();
1220 unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
1222 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1223 Error("AST record has invalid code");
1227 llvm::MemoryBuffer *Buffer
1228 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), File->getName());
1229 SourceMgr.overrideFileContents(File, Buffer);
1235 case SM_SLOC_BUFFER_ENTRY: {
1236 const char *Name = Blob.data();
1237 unsigned Offset = Record[0];
1238 SrcMgr::CharacteristicKind
1239 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1240 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1241 if (IncludeLoc.isInvalid() && F->Kind == MK_Module) {
1242 IncludeLoc = getImportLocation(F);
1244 unsigned Code = SLocEntryCursor.ReadCode();
1247 = SLocEntryCursor.readRecord(Code, Record, &Blob);
1249 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1250 Error("AST record has invalid code");
1254 llvm::MemoryBuffer *Buffer
1255 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name);
1256 SourceMgr.createFileID(Buffer, FileCharacter, ID, BaseOffset + Offset,
1261 case SM_SLOC_EXPANSION_ENTRY: {
1262 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1263 SourceMgr.createExpansionLoc(SpellingLoc,
1264 ReadSourceLocation(*F, Record[2]),
1265 ReadSourceLocation(*F, Record[3]),
1268 BaseOffset + Record[0]);
1276 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1278 return std::make_pair(SourceLocation(), "");
1280 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1281 Error("source location entry ID out-of-range for AST file");
1282 return std::make_pair(SourceLocation(), "");
1285 // Find which module file this entry lands in.
1286 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1287 if (M->Kind != MK_Module)
1288 return std::make_pair(SourceLocation(), "");
1290 // FIXME: Can we map this down to a particular submodule? That would be
1292 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1295 /// \brief Find the location where the module F is imported.
1296 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1297 if (F->ImportLoc.isValid())
1298 return F->ImportLoc;
1300 // Otherwise we have a PCH. It's considered to be "imported" at the first
1301 // location of its includer.
1302 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1303 // Main file is the importer.
1304 assert(!SourceMgr.getMainFileID().isInvalid() && "missing main file");
1305 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1307 return F->ImportedBy[0]->FirstLoc;
1310 /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1311 /// specified cursor. Read the abbreviations that are at the top of the block
1312 /// and then leave the cursor pointing into the block.
1313 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
1314 if (Cursor.EnterSubBlock(BlockID)) {
1315 Error("malformed block record in AST file");
1320 uint64_t Offset = Cursor.GetCurrentBitNo();
1321 unsigned Code = Cursor.ReadCode();
1323 // We expect all abbrevs to be at the start of the block.
1324 if (Code != llvm::bitc::DEFINE_ABBREV) {
1325 Cursor.JumpToBit(Offset);
1328 Cursor.ReadAbbrevRecord();
1332 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1336 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1337 Tok.setLength(Record[Idx++]);
1338 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1339 Tok.setIdentifierInfo(II);
1340 Tok.setKind((tok::TokenKind)Record[Idx++]);
1341 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1345 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1346 BitstreamCursor &Stream = F.MacroCursor;
1348 // Keep track of where we are in the stream, then jump back there
1349 // after reading this macro.
1350 SavedStreamPosition SavedPosition(Stream);
1352 Stream.JumpToBit(Offset);
1354 SmallVector<IdentifierInfo*, 16> MacroArgs;
1355 MacroInfo *Macro = nullptr;
1358 // Advance to the next record, but if we get to the end of the block, don't
1359 // pop it (removing all the abbreviations from the cursor) since we want to
1360 // be able to reseek within the block and read entries.
1361 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1362 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1364 switch (Entry.Kind) {
1365 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1366 case llvm::BitstreamEntry::Error:
1367 Error("malformed block record in AST file");
1369 case llvm::BitstreamEntry::EndBlock:
1371 case llvm::BitstreamEntry::Record:
1372 // The interesting case.
1378 PreprocessorRecordTypes RecType =
1379 (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
1381 case PP_MACRO_DIRECTIVE_HISTORY:
1384 case PP_MACRO_OBJECT_LIKE:
1385 case PP_MACRO_FUNCTION_LIKE: {
1386 // If we already have a macro, that means that we've hit the end
1387 // of the definition of the macro we were looking for. We're
1392 unsigned NextIndex = 1; // Skip identifier ID.
1393 SubmoduleID SubModID = getGlobalSubmoduleID(F, Record[NextIndex++]);
1394 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1395 MacroInfo *MI = PP.AllocateDeserializedMacroInfo(Loc, SubModID);
1396 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1397 MI->setIsUsed(Record[NextIndex++]);
1398 MI->setUsedForHeaderGuard(Record[NextIndex++]);
1400 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1401 // Decode function-like macro info.
1402 bool isC99VarArgs = Record[NextIndex++];
1403 bool isGNUVarArgs = Record[NextIndex++];
1404 bool hasCommaPasting = Record[NextIndex++];
1406 unsigned NumArgs = Record[NextIndex++];
1407 for (unsigned i = 0; i != NumArgs; ++i)
1408 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1410 // Install function-like macro info.
1411 MI->setIsFunctionLike();
1412 if (isC99VarArgs) MI->setIsC99Varargs();
1413 if (isGNUVarArgs) MI->setIsGNUVarargs();
1414 if (hasCommaPasting) MI->setHasCommaPasting();
1415 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
1416 PP.getPreprocessorAllocator());
1419 // Remember that we saw this macro last so that we add the tokens that
1420 // form its body to it.
1423 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1424 Record[NextIndex]) {
1425 // We have a macro definition. Register the association
1426 PreprocessedEntityID
1427 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1428 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1429 PreprocessingRecord::PPEntityID
1430 PPID = PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true);
1431 MacroDefinition *PPDef =
1432 cast_or_null<MacroDefinition>(PPRec.getPreprocessedEntity(PPID));
1434 PPRec.RegisterMacroDefinition(Macro, PPDef);
1442 // If we see a TOKEN before a PP_MACRO_*, then the file is
1443 // erroneous, just pretend we didn't see this.
1447 Token Tok = ReadToken(F, Record, Idx);
1448 Macro->AddTokenToBody(Tok);
1455 PreprocessedEntityID
1456 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
1457 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1458 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1459 assert(I != M.PreprocessedEntityRemap.end()
1460 && "Invalid index into preprocessed entity index remap");
1462 return LocalID + I->second;
1465 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1466 return llvm::hash_combine(ikey.Size, ikey.ModTime);
1469 HeaderFileInfoTrait::internal_key_type
1470 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1471 internal_key_type ikey = { FE->getSize(), FE->getModificationTime(),
1476 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1477 if (a.Size != b.Size || a.ModTime != b.ModTime)
1480 if (strcmp(a.Filename, b.Filename) == 0)
1483 // Determine whether the actual files are equivalent.
1484 FileManager &FileMgr = Reader.getFileManager();
1485 const FileEntry *FEA = FileMgr.getFile(a.Filename);
1486 const FileEntry *FEB = FileMgr.getFile(b.Filename);
1487 return (FEA && FEA == FEB);
1490 std::pair<unsigned, unsigned>
1491 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1492 using namespace llvm::support;
1493 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1494 unsigned DataLen = (unsigned) *d++;
1495 return std::make_pair(KeyLen, DataLen);
1498 HeaderFileInfoTrait::internal_key_type
1499 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1500 using namespace llvm::support;
1501 internal_key_type ikey;
1502 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1503 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1504 ikey.Filename = (const char *)d;
1508 HeaderFileInfoTrait::data_type
1509 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1511 const unsigned char *End = d + DataLen;
1512 using namespace llvm::support;
1514 unsigned Flags = *d++;
1515 HFI.HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>
1516 ((Flags >> 6) & 0x03);
1517 HFI.isImport = (Flags >> 5) & 0x01;
1518 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1519 HFI.DirInfo = (Flags >> 2) & 0x03;
1520 HFI.Resolved = (Flags >> 1) & 0x01;
1521 HFI.IndexHeaderMapHeader = Flags & 0x01;
1522 HFI.NumIncludes = endian::readNext<uint16_t, little, unaligned>(d);
1523 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1524 M, endian::readNext<uint32_t, little, unaligned>(d));
1525 if (unsigned FrameworkOffset =
1526 endian::readNext<uint32_t, little, unaligned>(d)) {
1527 // The framework offset is 1 greater than the actual offset,
1528 // since 0 is used as an indicator for "no framework name".
1529 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1530 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1534 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1536 // This header is part of a module. Associate it with the module to enable
1537 // implicit module import.
1538 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1539 Module *Mod = Reader.getSubmodule(GlobalSMID);
1540 HFI.isModuleHeader = true;
1541 FileManager &FileMgr = Reader.getFileManager();
1543 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1544 ModMap.addHeader(Mod, FileMgr.getFile(key.Filename), HFI.getHeaderRole());
1548 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1551 // This HeaderFileInfo was externally loaded.
1552 HFI.External = true;
1557 ASTReader::addPendingMacroFromModule(IdentifierInfo *II, ModuleFile *M,
1558 GlobalMacroID GMacID,
1559 ArrayRef<SubmoduleID> Overrides) {
1560 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1561 SubmoduleID *OverrideData = nullptr;
1562 if (!Overrides.empty()) {
1563 OverrideData = new (Context) SubmoduleID[Overrides.size() + 1];
1564 OverrideData[0] = Overrides.size();
1565 for (unsigned I = 0; I != Overrides.size(); ++I)
1566 OverrideData[I + 1] = getGlobalSubmoduleID(*M, Overrides[I]);
1568 PendingMacroIDs[II].push_back(PendingMacroInfo(M, GMacID, OverrideData));
1571 void ASTReader::addPendingMacroFromPCH(IdentifierInfo *II,
1573 uint64_t MacroDirectivesOffset) {
1574 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1575 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1578 void ASTReader::ReadDefinedMacros() {
1579 // Note that we are loading defined macros.
1580 Deserializing Macros(this);
1582 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1583 E = ModuleMgr.rend(); I != E; ++I) {
1584 BitstreamCursor &MacroCursor = (*I)->MacroCursor;
1586 // If there was no preprocessor block, skip this file.
1587 if (!MacroCursor.getBitStreamReader())
1590 BitstreamCursor Cursor = MacroCursor;
1591 Cursor.JumpToBit((*I)->MacroStartOffset);
1595 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1598 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1599 case llvm::BitstreamEntry::Error:
1600 Error("malformed block record in AST file");
1602 case llvm::BitstreamEntry::EndBlock:
1605 case llvm::BitstreamEntry::Record:
1607 switch (Cursor.readRecord(E.ID, Record)) {
1608 default: // Default behavior: ignore.
1611 case PP_MACRO_OBJECT_LIKE:
1612 case PP_MACRO_FUNCTION_LIKE:
1613 getLocalIdentifier(**I, Record[0]);
1628 /// \brief Visitor class used to look up identifirs in an AST file.
1629 class IdentifierLookupVisitor {
1631 unsigned PriorGeneration;
1632 unsigned &NumIdentifierLookups;
1633 unsigned &NumIdentifierLookupHits;
1634 IdentifierInfo *Found;
1637 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1638 unsigned &NumIdentifierLookups,
1639 unsigned &NumIdentifierLookupHits)
1640 : Name(Name), PriorGeneration(PriorGeneration),
1641 NumIdentifierLookups(NumIdentifierLookups),
1642 NumIdentifierLookupHits(NumIdentifierLookupHits),
1647 static bool visit(ModuleFile &M, void *UserData) {
1648 IdentifierLookupVisitor *This
1649 = static_cast<IdentifierLookupVisitor *>(UserData);
1651 // If we've already searched this module file, skip it now.
1652 if (M.Generation <= This->PriorGeneration)
1655 ASTIdentifierLookupTable *IdTable
1656 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1660 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1662 ++This->NumIdentifierLookups;
1663 ASTIdentifierLookupTable::iterator Pos = IdTable->find(This->Name,&Trait);
1664 if (Pos == IdTable->end())
1667 // Dereferencing the iterator has the effect of building the
1668 // IdentifierInfo node and populating it with the various
1669 // declarations it needs.
1670 ++This->NumIdentifierLookupHits;
1675 // \brief Retrieve the identifier info found within the module
1677 IdentifierInfo *getIdentifierInfo() const { return Found; }
1681 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1682 // Note that we are loading an identifier.
1683 Deserializing AnIdentifier(this);
1685 unsigned PriorGeneration = 0;
1686 if (getContext().getLangOpts().Modules)
1687 PriorGeneration = IdentifierGeneration[&II];
1689 // If there is a global index, look there first to determine which modules
1690 // provably do not have any results for this identifier.
1691 GlobalModuleIndex::HitSet Hits;
1692 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
1693 if (!loadGlobalIndex()) {
1694 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1699 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
1700 NumIdentifierLookups,
1701 NumIdentifierLookupHits);
1702 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
1703 markIdentifierUpToDate(&II);
1706 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1710 II->setOutOfDate(false);
1712 // Update the generation for this identifier.
1713 if (getContext().getLangOpts().Modules)
1714 IdentifierGeneration[II] = getGeneration();
1717 struct ASTReader::ModuleMacroInfo {
1718 SubmoduleID SubModID;
1720 SubmoduleID *Overrides;
1721 // FIXME: Remove this.
1724 bool isDefine() const { return MI; }
1726 SubmoduleID getSubmoduleID() const { return SubModID; }
1728 ArrayRef<SubmoduleID> getOverriddenSubmodules() const {
1731 return llvm::makeArrayRef(Overrides + 1, *Overrides);
1734 MacroDirective *import(Preprocessor &PP, SourceLocation ImportLoc) const {
1736 return PP.AllocateUndefMacroDirective(ImportLoc, SubModID,
1737 getOverriddenSubmodules());
1738 return PP.AllocateDefMacroDirective(MI, ImportLoc, SubModID,
1739 getOverriddenSubmodules());
1743 ASTReader::ModuleMacroInfo *
1744 ASTReader::getModuleMacro(const PendingMacroInfo &PMInfo) {
1745 ModuleMacroInfo Info;
1747 uint32_t ID = PMInfo.ModuleMacroData.MacID;
1749 // Macro undefinition.
1750 Info.SubModID = getGlobalSubmoduleID(*PMInfo.M, ID >> 1);
1753 // Macro definition.
1754 GlobalMacroID GMacID = getGlobalMacroID(*PMInfo.M, ID >> 1);
1757 // If this macro has already been loaded, don't do so again.
1758 // FIXME: This is highly dubious. Multiple macro definitions can have the
1759 // same MacroInfo (and hence the same GMacID) due to #pragma push_macro etc.
1760 if (MacrosLoaded[GMacID - NUM_PREDEF_MACRO_IDS])
1763 Info.MI = getMacro(GMacID);
1764 Info.SubModID = Info.MI->getOwningModuleID();
1766 Info.Overrides = PMInfo.ModuleMacroData.Overrides;
1769 return new (Context) ModuleMacroInfo(Info);
1772 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1773 const PendingMacroInfo &PMInfo) {
1776 if (PMInfo.M->Kind != MK_Module) {
1777 installPCHMacroDirectives(II, *PMInfo.M,
1778 PMInfo.PCHMacroData.MacroDirectivesOffset);
1784 ModuleMacroInfo *MMI = getModuleMacro(PMInfo);
1788 Module *Owner = getSubmodule(MMI->getSubmoduleID());
1789 if (Owner && Owner->NameVisibility == Module::Hidden) {
1790 // Macros in the owning module are hidden. Just remember this macro to
1791 // install if we make this module visible.
1792 HiddenNamesMap[Owner].HiddenMacros.insert(std::make_pair(II, MMI));
1794 installImportedMacro(II, MMI, Owner);
1798 void ASTReader::installPCHMacroDirectives(IdentifierInfo *II,
1799 ModuleFile &M, uint64_t Offset) {
1800 assert(M.Kind != MK_Module);
1802 BitstreamCursor &Cursor = M.MacroCursor;
1803 SavedStreamPosition SavedPosition(Cursor);
1804 Cursor.JumpToBit(Offset);
1806 llvm::BitstreamEntry Entry =
1807 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1808 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1809 Error("malformed block record in AST file");
1814 PreprocessorRecordTypes RecType =
1815 (PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record);
1816 if (RecType != PP_MACRO_DIRECTIVE_HISTORY) {
1817 Error("malformed block record in AST file");
1821 // Deserialize the macro directives history in reverse source-order.
1822 MacroDirective *Latest = nullptr, *Earliest = nullptr;
1823 unsigned Idx = 0, N = Record.size();
1825 MacroDirective *MD = nullptr;
1826 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
1827 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
1829 case MacroDirective::MD_Define: {
1830 GlobalMacroID GMacID = getGlobalMacroID(M, Record[Idx++]);
1831 MacroInfo *MI = getMacro(GMacID);
1832 SubmoduleID ImportedFrom = Record[Idx++];
1833 bool IsAmbiguous = Record[Idx++];
1834 llvm::SmallVector<unsigned, 4> Overrides;
1836 Overrides.insert(Overrides.end(),
1837 &Record[Idx] + 1, &Record[Idx] + 1 + Record[Idx]);
1838 Idx += Overrides.size() + 1;
1840 DefMacroDirective *DefMD =
1841 PP.AllocateDefMacroDirective(MI, Loc, ImportedFrom, Overrides);
1842 DefMD->setAmbiguous(IsAmbiguous);
1846 case MacroDirective::MD_Undefine: {
1847 SubmoduleID ImportedFrom = Record[Idx++];
1848 llvm::SmallVector<unsigned, 4> Overrides;
1850 Overrides.insert(Overrides.end(),
1851 &Record[Idx] + 1, &Record[Idx] + 1 + Record[Idx]);
1852 Idx += Overrides.size() + 1;
1854 MD = PP.AllocateUndefMacroDirective(Loc, ImportedFrom, Overrides);
1857 case MacroDirective::MD_Visibility:
1858 bool isPublic = Record[Idx++];
1859 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
1866 Earliest->setPrevious(MD);
1870 PP.setLoadedMacroDirective(II, Latest);
1873 /// \brief For the given macro definitions, check if they are both in system
1875 static bool areDefinedInSystemModules(MacroInfo *PrevMI, MacroInfo *NewMI,
1876 Module *NewOwner, ASTReader &Reader) {
1877 assert(PrevMI && NewMI);
1878 Module *PrevOwner = nullptr;
1879 if (SubmoduleID PrevModID = PrevMI->getOwningModuleID())
1880 PrevOwner = Reader.getSubmodule(PrevModID);
1881 SourceManager &SrcMgr = Reader.getSourceManager();
1883 = PrevOwner? PrevOwner->IsSystem
1884 : SrcMgr.isInSystemHeader(PrevMI->getDefinitionLoc());
1886 = NewOwner? NewOwner->IsSystem
1887 : SrcMgr.isInSystemHeader(NewMI->getDefinitionLoc());
1888 if (PrevOwner && PrevOwner == NewOwner)
1890 return PrevInSystem && NewInSystem;
1893 void ASTReader::removeOverriddenMacros(IdentifierInfo *II,
1894 SourceLocation ImportLoc,
1895 AmbiguousMacros &Ambig,
1896 ArrayRef<SubmoduleID> Overrides) {
1897 for (unsigned OI = 0, ON = Overrides.size(); OI != ON; ++OI) {
1898 SubmoduleID OwnerID = Overrides[OI];
1900 // If this macro is not yet visible, remove it from the hidden names list.
1901 // It won't be there if we're in the middle of making the owner visible.
1902 Module *Owner = getSubmodule(OwnerID);
1903 auto HiddenIt = HiddenNamesMap.find(Owner);
1904 if (HiddenIt != HiddenNamesMap.end()) {
1905 HiddenNames &Hidden = HiddenIt->second;
1906 HiddenMacrosMap::iterator HI = Hidden.HiddenMacros.find(II);
1907 if (HI != Hidden.HiddenMacros.end()) {
1908 // Register the macro now so we don't lose it when we re-export.
1909 PP.appendMacroDirective(II, HI->second->import(PP, ImportLoc));
1911 auto SubOverrides = HI->second->getOverriddenSubmodules();
1912 Hidden.HiddenMacros.erase(HI);
1913 removeOverriddenMacros(II, ImportLoc, Ambig, SubOverrides);
1917 // If this macro is already in our list of conflicts, remove it from there.
1919 std::remove_if(Ambig.begin(), Ambig.end(), [&](DefMacroDirective *MD) {
1920 return MD->getInfo()->getOwningModuleID() == OwnerID;
1926 ASTReader::AmbiguousMacros *
1927 ASTReader::removeOverriddenMacros(IdentifierInfo *II,
1928 SourceLocation ImportLoc,
1929 ArrayRef<SubmoduleID> Overrides) {
1930 MacroDirective *Prev = PP.getMacroDirective(II);
1931 if (!Prev && Overrides.empty())
1934 DefMacroDirective *PrevDef = Prev ? Prev->getDefinition().getDirective()
1936 if (PrevDef && PrevDef->isAmbiguous()) {
1937 // We had a prior ambiguity. Check whether we resolve it (or make it worse).
1938 AmbiguousMacros &Ambig = AmbiguousMacroDefs[II];
1939 Ambig.push_back(PrevDef);
1941 removeOverriddenMacros(II, ImportLoc, Ambig, Overrides);
1946 AmbiguousMacroDefs.erase(II);
1948 // There's no ambiguity yet. Maybe we're introducing one.
1949 AmbiguousMacros Ambig;
1951 Ambig.push_back(PrevDef);
1953 removeOverriddenMacros(II, ImportLoc, Ambig, Overrides);
1955 if (!Ambig.empty()) {
1956 AmbiguousMacros &Result = AmbiguousMacroDefs[II];
1957 std::swap(Result, Ambig);
1962 // We ended up with no ambiguity.
1966 void ASTReader::installImportedMacro(IdentifierInfo *II, ModuleMacroInfo *MMI,
1968 assert(II && Owner);
1970 SourceLocation ImportLoc = Owner->MacroVisibilityLoc;
1971 if (ImportLoc.isInvalid()) {
1972 // FIXME: If we made macros from this module visible but didn't provide a
1973 // source location for the import, we don't have a location for the macro.
1974 // Use the location at which the containing module file was first imported
1976 ImportLoc = MMI->F->DirectImportLoc;
1977 assert(ImportLoc.isValid() && "no import location for a visible macro?");
1980 AmbiguousMacros *Prev =
1981 removeOverriddenMacros(II, ImportLoc, MMI->getOverriddenSubmodules());
1983 // Create a synthetic macro definition corresponding to the import (or null
1984 // if this was an undefinition of the macro).
1985 MacroDirective *Imported = MMI->import(PP, ImportLoc);
1986 DefMacroDirective *MD = dyn_cast<DefMacroDirective>(Imported);
1988 // If there's no ambiguity, just install the macro.
1990 PP.appendMacroDirective(II, Imported);
1993 assert(!Prev->empty());
1996 // We imported a #undef that didn't remove all prior definitions. The most
1997 // recent prior definition remains, and we install it in the place of the
1998 // imported directive, as if by a local #pragma pop_macro.
1999 MacroInfo *NewMI = Prev->back()->getInfo();
2001 MD = PP.AllocateDefMacroDirective(NewMI, ImportLoc);
2003 // Install our #undef first so that we don't lose track of it. We'll replace
2004 // this with whichever macro definition ends up winning.
2005 PP.appendMacroDirective(II, Imported);
2008 // We're introducing a macro definition that creates or adds to an ambiguity.
2009 // We can resolve that ambiguity if this macro is token-for-token identical to
2010 // all of the existing definitions.
2011 MacroInfo *NewMI = MD->getInfo();
2012 assert(NewMI && "macro definition with no MacroInfo?");
2013 while (!Prev->empty()) {
2014 MacroInfo *PrevMI = Prev->back()->getInfo();
2015 assert(PrevMI && "macro definition with no MacroInfo?");
2017 // Before marking the macros as ambiguous, check if this is a case where
2018 // both macros are in system headers. If so, we trust that the system
2019 // did not get it wrong. This also handles cases where Clang's own
2020 // headers have a different spelling of certain system macros:
2021 // #define LONG_MAX __LONG_MAX__ (clang's limits.h)
2022 // #define LONG_MAX 0x7fffffffffffffffL (system's limits.h)
2024 // FIXME: Remove the defined-in-system-headers check. clang's limits.h
2025 // overrides the system limits.h's macros, so there's no conflict here.
2026 if (NewMI != PrevMI &&
2027 !PrevMI->isIdenticalTo(*NewMI, PP, /*Syntactically=*/true) &&
2028 !areDefinedInSystemModules(PrevMI, NewMI, Owner, *this))
2031 // The previous definition is the same as this one (or both are defined in
2032 // system modules so we can assume they're equivalent); we don't need to
2033 // track it any more.
2038 MD->setAmbiguous(true);
2040 PP.appendMacroDirective(II, MD);
2043 ASTReader::InputFileInfo
2044 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2045 // Go find this input file.
2046 BitstreamCursor &Cursor = F.InputFilesCursor;
2047 SavedStreamPosition SavedPosition(Cursor);
2048 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
2050 unsigned Code = Cursor.ReadCode();
2054 unsigned Result = Cursor.readRecord(Code, Record, &Blob);
2055 assert(static_cast<InputFileRecordTypes>(Result) == INPUT_FILE &&
2056 "invalid record type for input file");
2059 std::string Filename;
2064 assert(Record[0] == ID && "Bogus stored ID or offset");
2065 StoredSize = static_cast<off_t>(Record[1]);
2066 StoredTime = static_cast<time_t>(Record[2]);
2067 Overridden = static_cast<bool>(Record[3]);
2069 MaybeAddSystemRootToFilename(F, Filename);
2071 InputFileInfo R = { std::move(Filename), StoredSize, StoredTime, Overridden };
2075 std::string ASTReader::getInputFileName(ModuleFile &F, unsigned int ID) {
2076 return readInputFileInfo(F, ID).Filename;
2079 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2080 // If this ID is bogus, just return an empty input file.
2081 if (ID == 0 || ID > F.InputFilesLoaded.size())
2084 // If we've already loaded this input file, return it.
2085 if (F.InputFilesLoaded[ID-1].getFile())
2086 return F.InputFilesLoaded[ID-1];
2088 if (F.InputFilesLoaded[ID-1].isNotFound())
2091 // Go find this input file.
2092 BitstreamCursor &Cursor = F.InputFilesCursor;
2093 SavedStreamPosition SavedPosition(Cursor);
2094 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
2096 InputFileInfo FI = readInputFileInfo(F, ID);
2097 off_t StoredSize = FI.StoredSize;
2098 time_t StoredTime = FI.StoredTime;
2099 bool Overridden = FI.Overridden;
2100 StringRef Filename = FI.Filename;
2102 const FileEntry *File
2103 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
2104 : FileMgr.getFile(Filename, /*OpenFile=*/false);
2106 // If we didn't find the file, resolve it relative to the
2107 // original directory from which this AST file was created.
2108 if (File == nullptr && !F.OriginalDir.empty() && !CurrentDir.empty() &&
2109 F.OriginalDir != CurrentDir) {
2110 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
2113 if (!Resolved.empty())
2114 File = FileMgr.getFile(Resolved);
2117 // For an overridden file, create a virtual file with the stored
2119 if (Overridden && File == nullptr) {
2120 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
2123 if (File == nullptr) {
2125 std::string ErrorStr = "could not find file '";
2126 ErrorStr += Filename;
2127 ErrorStr += "' referenced by AST file";
2128 Error(ErrorStr.c_str());
2130 // Record that we didn't find the file.
2131 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2135 // Check if there was a request to override the contents of the file
2136 // that was part of the precompiled header. Overridding such a file
2137 // can lead to problems when lexing using the source locations from the
2139 SourceManager &SM = getSourceManager();
2140 if (!Overridden && SM.isFileOverridden(File)) {
2142 Error(diag::err_fe_pch_file_overridden, Filename);
2143 // After emitting the diagnostic, recover by disabling the override so
2144 // that the original file will be used.
2145 SM.disableFileContentsOverride(File);
2146 // The FileEntry is a virtual file entry with the size of the contents
2147 // that would override the original contents. Set it to the original's
2149 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
2150 StoredSize, StoredTime);
2153 bool IsOutOfDate = false;
2155 // For an overridden file, there is nothing to validate.
2156 if (!Overridden && (StoredSize != File->getSize()
2157 #if !defined(LLVM_ON_WIN32)
2158 // In our regression testing, the Windows file system seems to
2159 // have inconsistent modification times that sometimes
2160 // erroneously trigger this error-handling path.
2161 || StoredTime != File->getModificationTime()
2165 // Build a list of the PCH imports that got us here (in reverse).
2166 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2167 while (ImportStack.back()->ImportedBy.size() > 0)
2168 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2170 // The top-level PCH is stale.
2171 StringRef TopLevelPCHName(ImportStack.back()->FileName);
2172 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
2174 // Print the import stack.
2175 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2176 Diag(diag::note_pch_required_by)
2177 << Filename << ImportStack[0]->FileName;
2178 for (unsigned I = 1; I < ImportStack.size(); ++I)
2179 Diag(diag::note_pch_required_by)
2180 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2183 if (!Diags.isDiagnosticInFlight())
2184 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2190 InputFile IF = InputFile(File, Overridden, IsOutOfDate);
2192 // Note that we've loaded this input file.
2193 F.InputFilesLoaded[ID-1] = IF;
2197 const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
2198 ModuleFile &M = ModuleMgr.getPrimaryModule();
2199 std::string Filename = filenameStrRef;
2200 MaybeAddSystemRootToFilename(M, Filename);
2201 const FileEntry *File = FileMgr.getFile(Filename);
2202 if (File == nullptr && !M.OriginalDir.empty() && !CurrentDir.empty() &&
2203 M.OriginalDir != CurrentDir) {
2204 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
2207 if (!resolved.empty())
2208 File = FileMgr.getFile(resolved);
2214 /// \brief If we are loading a relocatable PCH file, and the filename is
2215 /// not an absolute path, add the system root to the beginning of the file
2217 void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
2218 std::string &Filename) {
2219 // If this is not a relocatable PCH file, there's nothing to do.
2220 if (!M.RelocatablePCH)
2223 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2226 if (isysroot.empty()) {
2227 // If no system root was given, default to '/'
2228 Filename.insert(Filename.begin(), '/');
2232 unsigned Length = isysroot.size();
2233 if (isysroot[Length - 1] != '/')
2234 Filename.insert(Filename.begin(), '/');
2236 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
2239 ASTReader::ASTReadResult
2240 ASTReader::ReadControlBlock(ModuleFile &F,
2241 SmallVectorImpl<ImportedModule> &Loaded,
2242 const ModuleFile *ImportedBy,
2243 unsigned ClientLoadCapabilities) {
2244 BitstreamCursor &Stream = F.Stream;
2246 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2247 Error("malformed block record in AST file");
2251 // Read all of the records and blocks in the control block.
2254 llvm::BitstreamEntry Entry = Stream.advance();
2256 switch (Entry.Kind) {
2257 case llvm::BitstreamEntry::Error:
2258 Error("malformed block record in AST file");
2260 case llvm::BitstreamEntry::EndBlock: {
2261 // Validate input files.
2262 const HeaderSearchOptions &HSOpts =
2263 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2265 // All user input files reside at the index range [0, Record[1]), and
2266 // system input files reside at [Record[1], Record[0]).
2267 // Record is the one from INPUT_FILE_OFFSETS.
2268 unsigned NumInputs = Record[0];
2269 unsigned NumUserInputs = Record[1];
2271 if (!DisableValidation &&
2272 (ValidateSystemInputs || !HSOpts.ModulesValidateOncePerBuildSession ||
2273 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp)) {
2274 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2276 // If we are reading a module, we will create a verification timestamp,
2277 // so we verify all input files. Otherwise, verify only user input
2280 unsigned N = NumUserInputs;
2281 if (ValidateSystemInputs ||
2282 (HSOpts.ModulesValidateOncePerBuildSession && F.Kind == MK_Module))
2285 for (unsigned I = 0; I < N; ++I) {
2286 InputFile IF = getInputFile(F, I+1, Complain);
2287 if (!IF.getFile() || IF.isOutOfDate())
2293 Listener->visitModuleFile(F.FileName);
2295 if (Listener && Listener->needsInputFileVisitation()) {
2296 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2298 for (unsigned I = 0; I < N; ++I) {
2299 bool IsSystem = I >= NumUserInputs;
2300 InputFileInfo FI = readInputFileInfo(F, I+1);
2301 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden);
2308 case llvm::BitstreamEntry::SubBlock:
2310 case INPUT_FILES_BLOCK_ID:
2311 F.InputFilesCursor = Stream;
2312 if (Stream.SkipBlock() || // Skip with the main cursor
2313 // Read the abbreviations
2314 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2315 Error("malformed block record in AST file");
2321 if (Stream.SkipBlock()) {
2322 Error("malformed block record in AST file");
2328 case llvm::BitstreamEntry::Record:
2329 // The interesting case.
2333 // Read and process a record.
2336 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
2338 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2339 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2340 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2341 : diag::err_pch_version_too_new);
2342 return VersionMismatch;
2345 bool hasErrors = Record[5];
2346 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2347 Diag(diag::err_pch_with_compiler_errors);
2351 F.RelocatablePCH = Record[4];
2353 const std::string &CurBranch = getClangFullRepositoryVersion();
2354 StringRef ASTBranch = Blob;
2355 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2356 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2357 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2358 return VersionMismatch;
2364 // Load each of the imported PCH files.
2365 unsigned Idx = 0, N = Record.size();
2367 // Read information about the AST file.
2368 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2369 // The import location will be the local one for now; we will adjust
2370 // all import locations of module imports after the global source
2371 // location info are setup.
2372 SourceLocation ImportLoc =
2373 SourceLocation::getFromRawEncoding(Record[Idx++]);
2374 off_t StoredSize = (off_t)Record[Idx++];
2375 time_t StoredModTime = (time_t)Record[Idx++];
2376 unsigned Length = Record[Idx++];
2377 SmallString<128> ImportedFile(Record.begin() + Idx,
2378 Record.begin() + Idx + Length);
2381 // Load the AST file.
2382 switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded,
2383 StoredSize, StoredModTime,
2384 ClientLoadCapabilities)) {
2385 case Failure: return Failure;
2386 // If we have to ignore the dependency, we'll have to ignore this too.
2388 case OutOfDate: return OutOfDate;
2389 case VersionMismatch: return VersionMismatch;
2390 case ConfigurationMismatch: return ConfigurationMismatch;
2391 case HadErrors: return HadErrors;
2392 case Success: break;
2398 case LANGUAGE_OPTIONS: {
2399 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2400 if (Listener && &F == *ModuleMgr.begin() &&
2401 ParseLanguageOptions(Record, Complain, *Listener) &&
2402 !DisableValidation && !AllowConfigurationMismatch)
2403 return ConfigurationMismatch;
2407 case TARGET_OPTIONS: {
2408 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2409 if (Listener && &F == *ModuleMgr.begin() &&
2410 ParseTargetOptions(Record, Complain, *Listener) &&
2411 !DisableValidation && !AllowConfigurationMismatch)
2412 return ConfigurationMismatch;
2416 case DIAGNOSTIC_OPTIONS: {
2417 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate)==0;
2418 if (Listener && &F == *ModuleMgr.begin() &&
2419 ParseDiagnosticOptions(Record, Complain, *Listener) &&
2425 case FILE_SYSTEM_OPTIONS: {
2426 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2427 if (Listener && &F == *ModuleMgr.begin() &&
2428 ParseFileSystemOptions(Record, Complain, *Listener) &&
2429 !DisableValidation && !AllowConfigurationMismatch)
2430 return ConfigurationMismatch;
2434 case HEADER_SEARCH_OPTIONS: {
2435 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2436 if (Listener && &F == *ModuleMgr.begin() &&
2437 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
2438 !DisableValidation && !AllowConfigurationMismatch)
2439 return ConfigurationMismatch;
2443 case PREPROCESSOR_OPTIONS: {
2444 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2445 if (Listener && &F == *ModuleMgr.begin() &&
2446 ParsePreprocessorOptions(Record, Complain, *Listener,
2447 SuggestedPredefines) &&
2448 !DisableValidation && !AllowConfigurationMismatch)
2449 return ConfigurationMismatch;
2454 F.OriginalSourceFileID = FileID::get(Record[0]);
2455 F.ActualOriginalSourceFileName = Blob;
2456 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2457 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
2460 case ORIGINAL_FILE_ID:
2461 F.OriginalSourceFileID = FileID::get(Record[0]);
2464 case ORIGINAL_PCH_DIR:
2465 F.OriginalDir = Blob;
2469 F.ModuleName = Blob;
2471 Listener->ReadModuleName(F.ModuleName);
2474 case MODULE_MAP_FILE:
2475 if (ASTReadResult Result =
2476 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2478 case INPUT_FILE_OFFSETS:
2479 F.InputFileOffsets = (const uint32_t *)Blob.data();
2480 F.InputFilesLoaded.resize(Record[0]);
2486 ASTReader::ASTReadResult
2487 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2488 BitstreamCursor &Stream = F.Stream;
2490 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2491 Error("malformed block record in AST file");
2495 // Read all of the records and blocks for the AST file.
2498 llvm::BitstreamEntry Entry = Stream.advance();
2500 switch (Entry.Kind) {
2501 case llvm::BitstreamEntry::Error:
2502 Error("error at end of module block in AST file");
2504 case llvm::BitstreamEntry::EndBlock: {
2505 // Outside of C++, we do not store a lookup map for the translation unit.
2506 // Instead, mark it as needing a lookup map to be built if this module
2507 // contains any declarations lexically within it (which it always does!).
2508 // This usually has no cost, since we very rarely need the lookup map for
2509 // the translation unit outside C++.
2510 DeclContext *DC = Context.getTranslationUnitDecl();
2511 if (DC->hasExternalLexicalStorage() &&
2512 !getContext().getLangOpts().CPlusPlus)
2513 DC->setMustBuildLookupTable();
2517 case llvm::BitstreamEntry::SubBlock:
2519 case DECLTYPES_BLOCK_ID:
2520 // We lazily load the decls block, but we want to set up the
2521 // DeclsCursor cursor to point into it. Clone our current bitcode
2522 // cursor to it, enter the block and read the abbrevs in that block.
2523 // With the main cursor, we just skip over it.
2524 F.DeclsCursor = Stream;
2525 if (Stream.SkipBlock() || // Skip with the main cursor.
2526 // Read the abbrevs.
2527 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2528 Error("malformed block record in AST file");
2533 case PREPROCESSOR_BLOCK_ID:
2534 F.MacroCursor = Stream;
2535 if (!PP.getExternalSource())
2536 PP.setExternalSource(this);
2538 if (Stream.SkipBlock() ||
2539 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2540 Error("malformed block record in AST file");
2543 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2546 case PREPROCESSOR_DETAIL_BLOCK_ID:
2547 F.PreprocessorDetailCursor = Stream;
2548 if (Stream.SkipBlock() ||
2549 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
2550 PREPROCESSOR_DETAIL_BLOCK_ID)) {
2551 Error("malformed preprocessor detail record in AST file");
2554 F.PreprocessorDetailStartOffset
2555 = F.PreprocessorDetailCursor.GetCurrentBitNo();
2557 if (!PP.getPreprocessingRecord())
2558 PP.createPreprocessingRecord();
2559 if (!PP.getPreprocessingRecord()->getExternalSource())
2560 PP.getPreprocessingRecord()->SetExternalSource(*this);
2563 case SOURCE_MANAGER_BLOCK_ID:
2564 if (ReadSourceManagerBlock(F))
2568 case SUBMODULE_BLOCK_ID:
2569 if (ASTReadResult Result = ReadSubmoduleBlock(F, ClientLoadCapabilities))
2573 case COMMENTS_BLOCK_ID: {
2574 BitstreamCursor C = Stream;
2575 if (Stream.SkipBlock() ||
2576 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2577 Error("malformed comments block in AST file");
2580 CommentsCursors.push_back(std::make_pair(C, &F));
2585 if (Stream.SkipBlock()) {
2586 Error("malformed block record in AST file");
2593 case llvm::BitstreamEntry::Record:
2594 // The interesting case.
2598 // Read and process a record.
2601 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
2602 default: // Default behavior: ignore.
2606 if (F.LocalNumTypes != 0) {
2607 Error("duplicate TYPE_OFFSET record in AST file");
2610 F.TypeOffsets = (const uint32_t *)Blob.data();
2611 F.LocalNumTypes = Record[0];
2612 unsigned LocalBaseTypeIndex = Record[1];
2613 F.BaseTypeIndex = getTotalNumTypes();
2615 if (F.LocalNumTypes > 0) {
2616 // Introduce the global -> local mapping for types within this module.
2617 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2619 // Introduce the local -> global mapping for types within this module.
2620 F.TypeRemap.insertOrReplace(
2621 std::make_pair(LocalBaseTypeIndex,
2622 F.BaseTypeIndex - LocalBaseTypeIndex));
2624 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2630 if (F.LocalNumDecls != 0) {
2631 Error("duplicate DECL_OFFSET record in AST file");
2634 F.DeclOffsets = (const DeclOffset *)Blob.data();
2635 F.LocalNumDecls = Record[0];
2636 unsigned LocalBaseDeclID = Record[1];
2637 F.BaseDeclID = getTotalNumDecls();
2639 if (F.LocalNumDecls > 0) {
2640 // Introduce the global -> local mapping for declarations within this
2642 GlobalDeclMap.insert(
2643 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2645 // Introduce the local -> global mapping for declarations within this
2647 F.DeclRemap.insertOrReplace(
2648 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2650 // Introduce the global -> local mapping for declarations within this
2652 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2654 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2659 case TU_UPDATE_LEXICAL: {
2660 DeclContext *TU = Context.getTranslationUnitDecl();
2661 DeclContextInfo &Info = F.DeclContextInfos[TU];
2662 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(Blob.data());
2663 Info.NumLexicalDecls
2664 = static_cast<unsigned int>(Blob.size() / sizeof(KindDeclIDPair));
2665 TU->setHasExternalLexicalStorage(true);
2669 case UPDATE_VISIBLE: {
2671 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2672 ASTDeclContextNameLookupTable *Table =
2673 ASTDeclContextNameLookupTable::Create(
2674 (const unsigned char *)Blob.data() + Record[Idx++],
2675 (const unsigned char *)Blob.data() + sizeof(uint32_t),
2676 (const unsigned char *)Blob.data(),
2677 ASTDeclContextNameLookupTrait(*this, F));
2678 if (Decl *D = GetExistingDecl(ID)) {
2679 auto *DC = cast<DeclContext>(D);
2680 DC->getPrimaryContext()->setHasExternalVisibleStorage(true);
2681 auto *&LookupTable = F.DeclContextInfos[DC].NameLookupTableData;
2683 LookupTable = Table;
2685 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
2689 case IDENTIFIER_TABLE:
2690 F.IdentifierTableData = Blob.data();
2692 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
2693 (const unsigned char *)F.IdentifierTableData + Record[0],
2694 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
2695 (const unsigned char *)F.IdentifierTableData,
2696 ASTIdentifierLookupTrait(*this, F));
2698 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2702 case IDENTIFIER_OFFSET: {
2703 if (F.LocalNumIdentifiers != 0) {
2704 Error("duplicate IDENTIFIER_OFFSET record in AST file");
2707 F.IdentifierOffsets = (const uint32_t *)Blob.data();
2708 F.LocalNumIdentifiers = Record[0];
2709 unsigned LocalBaseIdentifierID = Record[1];
2710 F.BaseIdentifierID = getTotalNumIdentifiers();
2712 if (F.LocalNumIdentifiers > 0) {
2713 // Introduce the global -> local mapping for identifiers within this
2715 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2718 // Introduce the local -> global mapping for identifiers within this
2720 F.IdentifierRemap.insertOrReplace(
2721 std::make_pair(LocalBaseIdentifierID,
2722 F.BaseIdentifierID - LocalBaseIdentifierID));
2724 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2725 + F.LocalNumIdentifiers);
2730 case EAGERLY_DESERIALIZED_DECLS:
2731 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2732 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
2736 if (SpecialTypes.empty()) {
2737 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2738 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2742 if (SpecialTypes.size() != Record.size()) {
2743 Error("invalid special-types record");
2747 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2748 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2749 if (!SpecialTypes[I])
2750 SpecialTypes[I] = ID;
2751 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2757 TotalNumStatements += Record[0];
2758 TotalNumMacros += Record[1];
2759 TotalLexicalDeclContexts += Record[2];
2760 TotalVisibleDeclContexts += Record[3];
2763 case UNUSED_FILESCOPED_DECLS:
2764 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2765 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2768 case DELEGATING_CTORS:
2769 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2770 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2773 case WEAK_UNDECLARED_IDENTIFIERS:
2774 if (Record.size() % 4 != 0) {
2775 Error("invalid weak identifiers record");
2779 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2780 // files. This isn't the way to do it :)
2781 WeakUndeclaredIdentifiers.clear();
2783 // Translate the weak, undeclared identifiers into global IDs.
2784 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2785 WeakUndeclaredIdentifiers.push_back(
2786 getGlobalIdentifierID(F, Record[I++]));
2787 WeakUndeclaredIdentifiers.push_back(
2788 getGlobalIdentifierID(F, Record[I++]));
2789 WeakUndeclaredIdentifiers.push_back(
2790 ReadSourceLocation(F, Record, I).getRawEncoding());
2791 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2795 case LOCALLY_SCOPED_EXTERN_C_DECLS:
2796 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2797 LocallyScopedExternCDecls.push_back(getGlobalDeclID(F, Record[I]));
2800 case SELECTOR_OFFSETS: {
2801 F.SelectorOffsets = (const uint32_t *)Blob.data();
2802 F.LocalNumSelectors = Record[0];
2803 unsigned LocalBaseSelectorID = Record[1];
2804 F.BaseSelectorID = getTotalNumSelectors();
2806 if (F.LocalNumSelectors > 0) {
2807 // Introduce the global -> local mapping for selectors within this
2809 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2811 // Introduce the local -> global mapping for selectors within this
2813 F.SelectorRemap.insertOrReplace(
2814 std::make_pair(LocalBaseSelectorID,
2815 F.BaseSelectorID - LocalBaseSelectorID));
2817 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2823 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
2825 F.SelectorLookupTable
2826 = ASTSelectorLookupTable::Create(
2827 F.SelectorLookupTableData + Record[0],
2828 F.SelectorLookupTableData,
2829 ASTSelectorLookupTrait(*this, F));
2830 TotalNumMethodPoolEntries += Record[1];
2833 case REFERENCED_SELECTOR_POOL:
2834 if (!Record.empty()) {
2835 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2836 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2838 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2844 case PP_COUNTER_VALUE:
2845 if (!Record.empty() && Listener)
2846 Listener->ReadCounter(F, Record[0]);
2849 case FILE_SORTED_DECLS:
2850 F.FileSortedDecls = (const DeclID *)Blob.data();
2851 F.NumFileSortedDecls = Record[0];
2854 case SOURCE_LOCATION_OFFSETS: {
2855 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
2856 F.LocalNumSLocEntries = Record[0];
2857 unsigned SLocSpaceSize = Record[1];
2858 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
2859 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2861 // Make our entry in the range map. BaseID is negative and growing, so
2862 // we invert it. Because we invert it, though, we need the other end of
2864 unsigned RangeStart =
2865 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2866 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2867 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2869 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2870 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2871 GlobalSLocOffsetMap.insert(
2872 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2873 - SLocSpaceSize,&F));
2875 // Initialize the remapping table.
2876 // Invalid stays invalid.
2877 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
2878 // This module. Base was 2 when being compiled.
2879 F.SLocRemap.insertOrReplace(std::make_pair(2U,
2880 static_cast<int>(F.SLocEntryBaseOffset - 2)));
2882 TotalNumSLocEntries += F.LocalNumSLocEntries;
2886 case MODULE_OFFSET_MAP: {
2887 // Additional remapping information.
2888 const unsigned char *Data = (const unsigned char*)Blob.data();
2889 const unsigned char *DataEnd = Data + Blob.size();
2891 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
2892 if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
2893 F.SLocRemap.insert(std::make_pair(0U, 0));
2894 F.SLocRemap.insert(std::make_pair(2U, 1));
2897 // Continuous range maps we may be updating in our module.
2898 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
2899 ContinuousRangeMap<uint32_t, int, 2>::Builder
2900 IdentifierRemap(F.IdentifierRemap);
2901 ContinuousRangeMap<uint32_t, int, 2>::Builder
2902 MacroRemap(F.MacroRemap);
2903 ContinuousRangeMap<uint32_t, int, 2>::Builder
2904 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2905 ContinuousRangeMap<uint32_t, int, 2>::Builder
2906 SubmoduleRemap(F.SubmoduleRemap);
2907 ContinuousRangeMap<uint32_t, int, 2>::Builder
2908 SelectorRemap(F.SelectorRemap);
2909 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
2910 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2912 while(Data < DataEnd) {
2913 using namespace llvm::support;
2914 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
2915 StringRef Name = StringRef((const char*)Data, Len);
2917 ModuleFile *OM = ModuleMgr.lookup(Name);
2919 Error("SourceLocation remap refers to unknown module");
2923 uint32_t SLocOffset =
2924 endian::readNext<uint32_t, little, unaligned>(Data);
2925 uint32_t IdentifierIDOffset =
2926 endian::readNext<uint32_t, little, unaligned>(Data);
2927 uint32_t MacroIDOffset =
2928 endian::readNext<uint32_t, little, unaligned>(Data);
2929 uint32_t PreprocessedEntityIDOffset =
2930 endian::readNext<uint32_t, little, unaligned>(Data);
2931 uint32_t SubmoduleIDOffset =
2932 endian::readNext<uint32_t, little, unaligned>(Data);
2933 uint32_t SelectorIDOffset =
2934 endian::readNext<uint32_t, little, unaligned>(Data);
2935 uint32_t DeclIDOffset =
2936 endian::readNext<uint32_t, little, unaligned>(Data);
2937 uint32_t TypeIndexOffset =
2938 endian::readNext<uint32_t, little, unaligned>(Data);
2940 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2941 SLocRemap.insert(std::make_pair(SLocOffset,
2942 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
2943 IdentifierRemap.insert(
2944 std::make_pair(IdentifierIDOffset,
2945 OM->BaseIdentifierID - IdentifierIDOffset));
2946 MacroRemap.insert(std::make_pair(MacroIDOffset,
2947 OM->BaseMacroID - MacroIDOffset));
2948 PreprocessedEntityRemap.insert(
2949 std::make_pair(PreprocessedEntityIDOffset,
2950 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
2951 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2952 OM->BaseSubmoduleID - SubmoduleIDOffset));
2953 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2954 OM->BaseSelectorID - SelectorIDOffset));
2955 DeclRemap.insert(std::make_pair(DeclIDOffset,
2956 OM->BaseDeclID - DeclIDOffset));
2958 TypeRemap.insert(std::make_pair(TypeIndexOffset,
2959 OM->BaseTypeIndex - TypeIndexOffset));
2961 // Global -> local mappings.
2962 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
2967 case SOURCE_MANAGER_LINE_TABLE:
2968 if (ParseLineTable(F, Record))
2972 case SOURCE_LOCATION_PRELOADS: {
2973 // Need to transform from the local view (1-based IDs) to the global view,
2974 // which is based off F.SLocEntryBaseID.
2975 if (!F.PreloadSLocEntries.empty()) {
2976 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2980 F.PreloadSLocEntries.swap(Record);
2984 case EXT_VECTOR_DECLS:
2985 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2986 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
2990 if (Record.size() % 3 != 0) {
2991 Error("Invalid VTABLE_USES record");
2995 // Later tables overwrite earlier ones.
2996 // FIXME: Modules will have some trouble with this. This is clearly not
2997 // the right way to do this.
3000 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3001 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3002 VTableUses.push_back(
3003 ReadSourceLocation(F, Record, Idx).getRawEncoding());
3004 VTableUses.push_back(Record[Idx++]);
3008 case DYNAMIC_CLASSES:
3009 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3010 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
3013 case PENDING_IMPLICIT_INSTANTIATIONS:
3014 if (PendingInstantiations.size() % 2 != 0) {
3015 Error("Invalid existing PendingInstantiations");
3019 if (Record.size() % 2 != 0) {
3020 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3024 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3025 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3026 PendingInstantiations.push_back(
3027 ReadSourceLocation(F, Record, I).getRawEncoding());
3031 case SEMA_DECL_REFS:
3032 if (Record.size() != 2) {
3033 Error("Invalid SEMA_DECL_REFS block");
3036 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3037 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3040 case PPD_ENTITIES_OFFSETS: {
3041 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3042 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3043 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3045 unsigned LocalBasePreprocessedEntityID = Record[0];
3047 unsigned StartingID;
3048 if (!PP.getPreprocessingRecord())
3049 PP.createPreprocessingRecord();
3050 if (!PP.getPreprocessingRecord()->getExternalSource())
3051 PP.getPreprocessingRecord()->SetExternalSource(*this);
3053 = PP.getPreprocessingRecord()
3054 ->allocateLoadedEntities(F.NumPreprocessedEntities);
3055 F.BasePreprocessedEntityID = StartingID;
3057 if (F.NumPreprocessedEntities > 0) {
3058 // Introduce the global -> local mapping for preprocessed entities in
3060 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3062 // Introduce the local -> global mapping for preprocessed entities in
3064 F.PreprocessedEntityRemap.insertOrReplace(
3065 std::make_pair(LocalBasePreprocessedEntityID,
3066 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3072 case DECL_UPDATE_OFFSETS: {
3073 if (Record.size() % 2 != 0) {
3074 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3077 for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3078 GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3079 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3081 // If we've already loaded the decl, perform the updates when we finish
3082 // loading this block.
3083 if (Decl *D = GetExistingDecl(ID))
3084 PendingUpdateRecords.push_back(std::make_pair(ID, D));
3089 case DECL_REPLACEMENTS: {
3090 if (Record.size() % 3 != 0) {
3091 Error("invalid DECL_REPLACEMENTS block in AST file");
3094 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
3095 ReplacedDecls[getGlobalDeclID(F, Record[I])]
3096 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
3100 case OBJC_CATEGORIES_MAP: {
3101 if (F.LocalNumObjCCategoriesInMap != 0) {
3102 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3106 F.LocalNumObjCCategoriesInMap = Record[0];
3107 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3111 case OBJC_CATEGORIES:
3112 F.ObjCCategories.swap(Record);
3115 case CXX_BASE_SPECIFIER_OFFSETS: {
3116 if (F.LocalNumCXXBaseSpecifiers != 0) {
3117 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
3121 F.LocalNumCXXBaseSpecifiers = Record[0];
3122 F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data();
3123 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
3127 case DIAG_PRAGMA_MAPPINGS:
3128 if (F.PragmaDiagMappings.empty())
3129 F.PragmaDiagMappings.swap(Record);
3131 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
3132 Record.begin(), Record.end());
3135 case CUDA_SPECIAL_DECL_REFS:
3136 // Later tables overwrite earlier ones.
3137 // FIXME: Modules will have trouble with this.
3138 CUDASpecialDeclRefs.clear();
3139 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3140 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3143 case HEADER_SEARCH_TABLE: {
3144 F.HeaderFileInfoTableData = Blob.data();
3145 F.LocalNumHeaderFileInfos = Record[1];
3147 F.HeaderFileInfoTable
3148 = HeaderFileInfoLookupTable::Create(
3149 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3150 (const unsigned char *)F.HeaderFileInfoTableData,
3151 HeaderFileInfoTrait(*this, F,
3152 &PP.getHeaderSearchInfo(),
3153 Blob.data() + Record[2]));
3155 PP.getHeaderSearchInfo().SetExternalSource(this);
3156 if (!PP.getHeaderSearchInfo().getExternalLookup())
3157 PP.getHeaderSearchInfo().SetExternalLookup(this);
3162 case FP_PRAGMA_OPTIONS:
3163 // Later tables overwrite earlier ones.
3164 FPPragmaOptions.swap(Record);
3167 case OPENCL_EXTENSIONS:
3168 // Later tables overwrite earlier ones.
3169 OpenCLExtensions.swap(Record);
3172 case TENTATIVE_DEFINITIONS:
3173 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3174 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3177 case KNOWN_NAMESPACES:
3178 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3179 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3182 case UNDEFINED_BUT_USED:
3183 if (UndefinedButUsed.size() % 2 != 0) {
3184 Error("Invalid existing UndefinedButUsed");
3188 if (Record.size() % 2 != 0) {
3189 Error("invalid undefined-but-used record");
3192 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3193 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3194 UndefinedButUsed.push_back(
3195 ReadSourceLocation(F, Record, I).getRawEncoding());
3199 case IMPORTED_MODULES: {
3200 if (F.Kind != MK_Module) {
3201 // If we aren't loading a module (which has its own exports), make
3202 // all of the imported modules visible.
3203 // FIXME: Deal with macros-only imports.
3204 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3205 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3206 SourceLocation Loc = ReadSourceLocation(F, Record, I);
3208 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3214 case LOCAL_REDECLARATIONS: {
3215 F.RedeclarationChains.swap(Record);
3219 case LOCAL_REDECLARATIONS_MAP: {
3220 if (F.LocalNumRedeclarationsInMap != 0) {
3221 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
3225 F.LocalNumRedeclarationsInMap = Record[0];
3226 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data();
3230 case MERGED_DECLARATIONS: {
3231 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
3232 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
3233 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
3234 for (unsigned N = Record[Idx++]; N > 0; --N)
3235 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
3240 case MACRO_OFFSET: {
3241 if (F.LocalNumMacros != 0) {
3242 Error("duplicate MACRO_OFFSET record in AST file");
3245 F.MacroOffsets = (const uint32_t *)Blob.data();
3246 F.LocalNumMacros = Record[0];
3247 unsigned LocalBaseMacroID = Record[1];
3248 F.BaseMacroID = getTotalNumMacros();
3250 if (F.LocalNumMacros > 0) {
3251 // Introduce the global -> local mapping for macros within this module.
3252 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3254 // Introduce the local -> global mapping for macros within this module.
3255 F.MacroRemap.insertOrReplace(
3256 std::make_pair(LocalBaseMacroID,
3257 F.BaseMacroID - LocalBaseMacroID));
3259 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3265 // FIXME: Not used yet.
3269 case LATE_PARSED_TEMPLATE: {
3270 LateParsedTemplates.append(Record.begin(), Record.end());
3274 case OPTIMIZE_PRAGMA_OPTIONS:
3275 if (Record.size() != 1) {
3276 Error("invalid pragma optimize record");
3279 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3285 ASTReader::ASTReadResult
3286 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3287 const ModuleFile *ImportedBy,
3288 unsigned ClientLoadCapabilities) {
3290 F.ModuleMapPath = ReadString(Record, Idx);
3292 // Try to resolve ModuleName in the current header search context and
3293 // verify that it is found in the same module map file as we saved. If the
3294 // top-level AST file is a main file, skip this check because there is no
3295 // usable header search context.
3296 assert(!F.ModuleName.empty() &&
3297 "MODULE_NAME should come before MOUDLE_MAP_FILE");
3298 if (F.Kind == MK_Module && (*ModuleMgr.begin())->Kind != MK_MainFile) {
3299 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3301 assert(ImportedBy && "top-level import should be verified");
3302 if ((ClientLoadCapabilities & ARR_Missing) == 0)
3303 Diag(diag::err_imported_module_not_found)
3304 << F.ModuleName << ImportedBy->FileName;
3308 // Check the primary module map file.
3309 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3310 const FileEntry *StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3311 const FileEntry *ModMap = Map.getModuleMapFileForUniquing(M);
3312 if (StoredModMap == nullptr || StoredModMap != ModMap) {
3313 assert(ModMap && "found module is missing module map file");
3314 assert(M->Name == F.ModuleName && "found module with different name");
3315 assert(ImportedBy && "top-level import should be verified");
3316 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3317 Diag(diag::err_imported_module_modmap_changed)
3318 << F.ModuleName << ImportedBy->FileName
3319 << ModMap->getName() << F.ModuleMapPath;
3323 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3324 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3325 // FIXME: we should use input files rather than storing names.
3326 std::string Filename = ReadString(Record, Idx);
3327 const FileEntry *F =
3328 FileMgr.getFile(Filename, false, false);
3330 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3331 Error("could not find file '" + Filename +"' referenced by AST file");
3334 AdditionalStoredMaps.insert(F);
3337 // Check any additional module map files (e.g. module.private.modulemap)
3338 // that are not in the pcm.
3339 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3340 for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3341 // Remove files that match
3342 // Note: SmallPtrSet::erase is really remove
3343 if (!AdditionalStoredMaps.erase(ModMap)) {
3344 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3345 Diag(diag::err_module_different_modmap)
3346 << F.ModuleName << /*new*/0 << ModMap->getName();
3352 // Check any additional module map files that are in the pcm, but not
3353 // found in header search. Cases that match are already removed.
3354 for (const FileEntry *ModMap : AdditionalStoredMaps) {
3355 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3356 Diag(diag::err_module_different_modmap)
3357 << F.ModuleName << /*not new*/1 << ModMap->getName();
3363 Listener->ReadModuleMapFile(F.ModuleMapPath);
3368 /// \brief Move the given method to the back of the global list of methods.
3369 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3370 // Find the entry for this selector in the method pool.
3371 Sema::GlobalMethodPool::iterator Known
3372 = S.MethodPool.find(Method->getSelector());
3373 if (Known == S.MethodPool.end())
3376 // Retrieve the appropriate method list.
3377 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3378 : Known->second.second;
3380 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
3382 if (List->Method == Method) {
3390 if (List->getNext())
3391 List->Method = List->getNext()->Method;
3393 List->Method = Method;
3397 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner,
3398 bool FromFinalization) {
3399 // FIXME: Only do this if Owner->NameVisibility == AllVisible.
3400 for (Decl *D : Names.HiddenDecls) {
3401 bool wasHidden = D->Hidden;
3404 if (wasHidden && SemaObj) {
3405 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
3406 moveMethodToBackOfGlobalList(*SemaObj, Method);
3411 assert((FromFinalization || Owner->NameVisibility >= Module::MacrosVisible) &&
3412 "nothing to make visible?");
3413 for (const auto &Macro : Names.HiddenMacros) {
3414 if (FromFinalization)
3415 PP.appendMacroDirective(Macro.first,
3416 Macro.second->import(PP, SourceLocation()));
3418 installImportedMacro(Macro.first, Macro.second, Owner);
3422 void ASTReader::makeModuleVisible(Module *Mod,
3423 Module::NameVisibilityKind NameVisibility,
3424 SourceLocation ImportLoc,
3426 llvm::SmallPtrSet<Module *, 4> Visited;
3427 SmallVector<Module *, 4> Stack;
3428 Stack.push_back(Mod);
3429 while (!Stack.empty()) {
3430 Mod = Stack.pop_back_val();
3432 if (NameVisibility <= Mod->NameVisibility) {
3433 // This module already has this level of visibility (or greater), so
3434 // there is nothing more to do.
3438 if (!Mod->isAvailable()) {
3439 // Modules that aren't available cannot be made visible.
3443 // Update the module's name visibility.
3444 if (NameVisibility >= Module::MacrosVisible &&
3445 Mod->NameVisibility < Module::MacrosVisible)
3446 Mod->MacroVisibilityLoc = ImportLoc;
3447 Mod->NameVisibility = NameVisibility;
3449 // If we've already deserialized any names from this module,
3450 // mark them as visible.
3451 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3452 if (Hidden != HiddenNamesMap.end()) {
3453 auto HiddenNames = std::move(*Hidden);
3454 HiddenNamesMap.erase(Hidden);
3455 makeNamesVisible(HiddenNames.second, HiddenNames.first,
3456 /*FromFinalization*/false);
3457 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
3458 "making names visible added hidden names");
3461 // Push any exported modules onto the stack to be marked as visible.
3462 SmallVector<Module *, 16> Exports;
3463 Mod->getExportedModules(Exports);
3464 for (SmallVectorImpl<Module *>::iterator
3465 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
3466 Module *Exported = *I;
3467 if (Visited.insert(Exported))
3468 Stack.push_back(Exported);
3471 // Detect any conflicts.
3473 assert(ImportLoc.isValid() && "Missing import location");
3474 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
3475 if (Mod->Conflicts[I].Other->NameVisibility >= NameVisibility) {
3476 Diag(ImportLoc, diag::warn_module_conflict)
3477 << Mod->getFullModuleName()
3478 << Mod->Conflicts[I].Other->getFullModuleName()
3479 << Mod->Conflicts[I].Message;
3480 // FIXME: Need note where the other module was imported.
3487 bool ASTReader::loadGlobalIndex() {
3491 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
3492 !Context.getLangOpts().Modules)
3495 // Try to load the global index.
3496 TriedLoadingGlobalIndex = true;
3497 StringRef ModuleCachePath
3498 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
3499 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
3500 = GlobalModuleIndex::readIndex(ModuleCachePath);
3504 GlobalIndex.reset(Result.first);
3505 ModuleMgr.setGlobalIndex(GlobalIndex.get());
3509 bool ASTReader::isGlobalIndexUnavailable() const {
3510 return Context.getLangOpts().Modules && UseGlobalIndex &&
3511 !hasGlobalIndex() && TriedLoadingGlobalIndex;
3514 static void updateModuleTimestamp(ModuleFile &MF) {
3515 // Overwrite the timestamp file contents so that file's mtime changes.
3516 std::string TimestampFilename = MF.getTimestampFilename();
3517 std::string ErrorInfo;
3518 llvm::raw_fd_ostream OS(TimestampFilename.c_str(), ErrorInfo,
3519 llvm::sys::fs::F_Text);
3520 if (!ErrorInfo.empty())
3522 OS << "Timestamp file\n";
3525 ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
3527 SourceLocation ImportLoc,
3528 unsigned ClientLoadCapabilities) {
3529 llvm::SaveAndRestore<SourceLocation>
3530 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
3532 // Defer any pending actions until we get to the end of reading the AST file.
3533 Deserializing AnASTFile(this);
3535 // Bump the generation number.
3536 unsigned PreviousGeneration = incrementGeneration(Context);
3538 unsigned NumModules = ModuleMgr.size();
3539 SmallVector<ImportedModule, 4> Loaded;
3540 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
3541 /*ImportedBy=*/nullptr, Loaded,
3543 ClientLoadCapabilities)) {
3547 case VersionMismatch:
3548 case ConfigurationMismatch:
3550 llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet;
3551 for (const ImportedModule &IM : Loaded)
3552 LoadedSet.insert(IM.Mod);
3554 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
3556 Context.getLangOpts().Modules
3557 ? &PP.getHeaderSearchInfo().getModuleMap()
3560 // If we find that any modules are unusable, the global index is going
3561 // to be out-of-date. Just remove it.
3562 GlobalIndex.reset();
3563 ModuleMgr.setGlobalIndex(nullptr);
3570 // Here comes stuff that we only do once the entire chain is loaded.
3572 // Load the AST blocks of all of the modules that we loaded.
3573 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3574 MEnd = Loaded.end();
3576 ModuleFile &F = *M->Mod;
3578 // Read the AST block.
3579 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
3582 // Once read, set the ModuleFile bit base offset and update the size in
3583 // bits of all files we've seen.
3584 F.GlobalBitOffset = TotalModulesSizeInBits;
3585 TotalModulesSizeInBits += F.SizeInBits;
3586 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
3588 // Preload SLocEntries.
3589 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
3590 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
3591 // Load it through the SourceManager and don't call ReadSLocEntry()
3592 // directly because the entry may have already been loaded in which case
3593 // calling ReadSLocEntry() directly would trigger an assertion in
3595 SourceMgr.getLoadedSLocEntryByID(Index);
3599 // Setup the import locations and notify the module manager that we've
3600 // committed to these module files.
3601 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3602 MEnd = Loaded.end();
3604 ModuleFile &F = *M->Mod;
3606 ModuleMgr.moduleFileAccepted(&F);
3608 // Set the import location.
3609 F.DirectImportLoc = ImportLoc;
3611 F.ImportLoc = M->ImportLoc;
3613 F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
3614 M->ImportLoc.getRawEncoding());
3617 // Mark all of the identifiers in the identifier table as being out of date,
3618 // so that various accessors know to check the loaded modules when the
3619 // identifier is used.
3620 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
3621 IdEnd = PP.getIdentifierTable().end();
3623 Id->second->setOutOfDate(true);
3625 // Resolve any unresolved module exports.
3626 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
3627 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
3628 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
3629 Module *ResolvedMod = getSubmodule(GlobalID);
3631 switch (Unresolved.Kind) {
3632 case UnresolvedModuleRef::Conflict:
3634 Module::Conflict Conflict;
3635 Conflict.Other = ResolvedMod;
3636 Conflict.Message = Unresolved.String.str();
3637 Unresolved.Mod->Conflicts.push_back(Conflict);
3641 case UnresolvedModuleRef::Import:
3643 Unresolved.Mod->Imports.push_back(ResolvedMod);
3646 case UnresolvedModuleRef::Export:
3647 if (ResolvedMod || Unresolved.IsWildcard)
3648 Unresolved.Mod->Exports.push_back(
3649 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
3653 UnresolvedModuleRefs.clear();
3655 // FIXME: How do we load the 'use'd modules? They may not be submodules.
3656 // Might be unnecessary as use declarations are only used to build the
3659 InitializeContext();
3664 if (DeserializationListener)
3665 DeserializationListener->ReaderInitialized(this);
3667 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
3668 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
3669 PrimaryModule.OriginalSourceFileID
3670 = FileID::get(PrimaryModule.SLocEntryBaseID
3671 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
3673 // If this AST file is a precompiled preamble, then set the
3674 // preamble file ID of the source manager to the file source file
3675 // from which the preamble was built.
3676 if (Type == MK_Preamble) {
3677 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
3678 } else if (Type == MK_MainFile) {
3679 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
3683 // For any Objective-C class definitions we have already loaded, make sure
3684 // that we load any additional categories.
3685 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3686 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3687 ObjCClassesLoaded[I],
3688 PreviousGeneration);
3691 if (PP.getHeaderSearchInfo()
3692 .getHeaderSearchOpts()
3693 .ModulesValidateOncePerBuildSession) {
3694 // Now we are certain that the module and all modules it depends on are
3695 // up to date. Create or update timestamp files for modules that are
3696 // located in the module cache (not for PCH files that could be anywhere
3697 // in the filesystem).
3698 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
3699 ImportedModule &M = Loaded[I];
3700 if (M.Mod->Kind == MK_Module) {
3701 updateModuleTimestamp(*M.Mod);
3709 ASTReader::ASTReadResult
3710 ASTReader::ReadASTCore(StringRef FileName,
3712 SourceLocation ImportLoc,
3713 ModuleFile *ImportedBy,
3714 SmallVectorImpl<ImportedModule> &Loaded,
3715 off_t ExpectedSize, time_t ExpectedModTime,
3716 unsigned ClientLoadCapabilities) {
3718 std::string ErrorStr;
3719 ModuleManager::AddModuleResult AddResult
3720 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
3721 getGeneration(), ExpectedSize, ExpectedModTime,
3724 switch (AddResult) {
3725 case ModuleManager::AlreadyLoaded:
3728 case ModuleManager::NewlyLoaded:
3729 // Load module file below.
3732 case ModuleManager::Missing:
3733 // The module file was missing; if the client handle handle, that, return
3735 if (ClientLoadCapabilities & ARR_Missing)
3738 // Otherwise, return an error.
3740 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3746 case ModuleManager::OutOfDate:
3747 // We couldn't load the module file because it is out-of-date. If the
3748 // client can handle out-of-date, return it.
3749 if (ClientLoadCapabilities & ARR_OutOfDate)
3752 // Otherwise, return an error.
3754 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3761 assert(M && "Missing module file");
3763 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3765 if (FileName != "-") {
3766 CurrentDir = llvm::sys::path::parent_path(FileName);
3767 if (CurrentDir.empty()) CurrentDir = ".";
3771 BitstreamCursor &Stream = F.Stream;
3772 Stream.init(F.StreamFile);
3773 F.SizeInBits = F.Buffer->getBufferSize() * 8;
3775 // Sniff for the signature.
3776 if (Stream.Read(8) != 'C' ||
3777 Stream.Read(8) != 'P' ||
3778 Stream.Read(8) != 'C' ||
3779 Stream.Read(8) != 'H') {
3780 Diag(diag::err_not_a_pch_file) << FileName;
3784 // This is used for compatibility with older PCH formats.
3785 bool HaveReadControlBlock = false;
3788 llvm::BitstreamEntry Entry = Stream.advance();
3790 switch (Entry.Kind) {
3791 case llvm::BitstreamEntry::Error:
3792 case llvm::BitstreamEntry::EndBlock:
3793 case llvm::BitstreamEntry::Record:
3794 Error("invalid record at top-level of AST file");
3797 case llvm::BitstreamEntry::SubBlock:
3801 // We only know the control subblock ID.
3803 case llvm::bitc::BLOCKINFO_BLOCK_ID:
3804 if (Stream.ReadBlockInfoBlock()) {
3805 Error("malformed BlockInfoBlock in AST file");
3809 case CONTROL_BLOCK_ID:
3810 HaveReadControlBlock = true;
3811 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
3815 case Failure: return Failure;
3816 case Missing: return Missing;
3817 case OutOfDate: return OutOfDate;
3818 case VersionMismatch: return VersionMismatch;
3819 case ConfigurationMismatch: return ConfigurationMismatch;
3820 case HadErrors: return HadErrors;
3824 if (!HaveReadControlBlock) {
3825 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3826 Diag(diag::err_pch_version_too_old);
3827 return VersionMismatch;
3830 // Record that we've loaded this module.
3831 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
3835 if (Stream.SkipBlock()) {
3836 Error("malformed block record in AST file");
3846 void ASTReader::InitializeContext() {
3847 // If there's a listener, notify them that we "read" the translation unit.
3848 if (DeserializationListener)
3849 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3850 Context.getTranslationUnitDecl());
3852 // FIXME: Find a better way to deal with collisions between these
3853 // built-in types. Right now, we just ignore the problem.
3855 // Load the special types.
3856 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
3857 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3858 if (!Context.CFConstantStringTypeDecl)
3859 Context.setCFConstantStringType(GetType(String));
3862 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3863 QualType FileType = GetType(File);
3864 if (FileType.isNull()) {
3865 Error("FILE type is NULL");
3869 if (!Context.FILEDecl) {
3870 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3871 Context.setFILEDecl(Typedef->getDecl());
3873 const TagType *Tag = FileType->getAs<TagType>();
3875 Error("Invalid FILE type in AST file");
3878 Context.setFILEDecl(Tag->getDecl());
3883 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
3884 QualType Jmp_bufType = GetType(Jmp_buf);
3885 if (Jmp_bufType.isNull()) {
3886 Error("jmp_buf type is NULL");
3890 if (!Context.jmp_bufDecl) {
3891 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3892 Context.setjmp_bufDecl(Typedef->getDecl());
3894 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3896 Error("Invalid jmp_buf type in AST file");
3899 Context.setjmp_bufDecl(Tag->getDecl());
3904 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
3905 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3906 if (Sigjmp_bufType.isNull()) {
3907 Error("sigjmp_buf type is NULL");
3911 if (!Context.sigjmp_bufDecl) {
3912 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3913 Context.setsigjmp_bufDecl(Typedef->getDecl());
3915 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3916 assert(Tag && "Invalid sigjmp_buf type in AST file");
3917 Context.setsigjmp_bufDecl(Tag->getDecl());
3922 if (unsigned ObjCIdRedef
3923 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3924 if (Context.ObjCIdRedefinitionType.isNull())
3925 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3928 if (unsigned ObjCClassRedef
3929 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3930 if (Context.ObjCClassRedefinitionType.isNull())
3931 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3934 if (unsigned ObjCSelRedef
3935 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3936 if (Context.ObjCSelRedefinitionType.isNull())
3937 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3940 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3941 QualType Ucontext_tType = GetType(Ucontext_t);
3942 if (Ucontext_tType.isNull()) {
3943 Error("ucontext_t type is NULL");
3947 if (!Context.ucontext_tDecl) {
3948 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3949 Context.setucontext_tDecl(Typedef->getDecl());
3951 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3952 assert(Tag && "Invalid ucontext_t type in AST file");
3953 Context.setucontext_tDecl(Tag->getDecl());
3959 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
3961 // If there were any CUDA special declarations, deserialize them.
3962 if (!CUDASpecialDeclRefs.empty()) {
3963 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
3964 Context.setcudaConfigureCallDecl(
3965 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3968 // Re-export any modules that were imported by a non-module AST file.
3969 // FIXME: This does not make macro-only imports visible again. It also doesn't
3970 // make #includes mapped to module imports visible.
3971 for (auto &Import : ImportedModules) {
3972 if (Module *Imported = getSubmodule(Import.ID))
3973 makeModuleVisible(Imported, Module::AllVisible,
3974 /*ImportLoc=*/Import.ImportLoc,
3975 /*Complain=*/false);
3977 ImportedModules.clear();
3980 void ASTReader::finalizeForWriting() {
3981 while (!HiddenNamesMap.empty()) {
3982 auto HiddenNames = std::move(*HiddenNamesMap.begin());
3983 HiddenNamesMap.erase(HiddenNamesMap.begin());
3984 makeNamesVisible(HiddenNames.second, HiddenNames.first,
3985 /*FromFinalization*/true);
3989 /// \brief Given a cursor at the start of an AST file, scan ahead and drop the
3990 /// cursor into the start of the given block ID, returning false on success and
3991 /// true on failure.
3992 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
3994 llvm::BitstreamEntry Entry = Cursor.advance();
3995 switch (Entry.Kind) {
3996 case llvm::BitstreamEntry::Error:
3997 case llvm::BitstreamEntry::EndBlock:
4000 case llvm::BitstreamEntry::Record:
4001 // Ignore top-level records.
4002 Cursor.skipRecord(Entry.ID);
4005 case llvm::BitstreamEntry::SubBlock:
4006 if (Entry.ID == BlockID) {
4007 if (Cursor.EnterSubBlock(BlockID))
4013 if (Cursor.SkipBlock())
4019 /// \brief Retrieve the name of the original source file name
4020 /// directly from the AST file, without actually loading the AST
4022 std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
4023 FileManager &FileMgr,
4024 DiagnosticsEngine &Diags) {
4025 // Open the AST file.
4027 std::unique_ptr<llvm::MemoryBuffer> Buffer;
4028 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
4030 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
4031 return std::string();
4034 // Initialize the stream
4035 llvm::BitstreamReader StreamFile;
4036 BitstreamCursor Stream;
4037 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
4038 (const unsigned char *)Buffer->getBufferEnd());
4039 Stream.init(StreamFile);
4041 // Sniff for the signature.
4042 if (Stream.Read(8) != 'C' ||
4043 Stream.Read(8) != 'P' ||
4044 Stream.Read(8) != 'C' ||
4045 Stream.Read(8) != 'H') {
4046 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
4047 return std::string();
4050 // Scan for the CONTROL_BLOCK_ID block.
4051 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
4052 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4053 return std::string();
4056 // Scan for ORIGINAL_FILE inside the control block.
4059 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4060 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
4061 return std::string();
4063 if (Entry.Kind != llvm::BitstreamEntry::Record) {
4064 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4065 return std::string();
4070 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
4076 class SimplePCHValidator : public ASTReaderListener {
4077 const LangOptions &ExistingLangOpts;
4078 const TargetOptions &ExistingTargetOpts;
4079 const PreprocessorOptions &ExistingPPOpts;
4080 FileManager &FileMgr;
4083 SimplePCHValidator(const LangOptions &ExistingLangOpts,
4084 const TargetOptions &ExistingTargetOpts,
4085 const PreprocessorOptions &ExistingPPOpts,
4086 FileManager &FileMgr)
4087 : ExistingLangOpts(ExistingLangOpts),
4088 ExistingTargetOpts(ExistingTargetOpts),
4089 ExistingPPOpts(ExistingPPOpts),
4094 bool ReadLanguageOptions(const LangOptions &LangOpts,
4095 bool Complain) override {
4096 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr);
4098 bool ReadTargetOptions(const TargetOptions &TargetOpts,
4099 bool Complain) override {
4100 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr);
4102 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
4104 std::string &SuggestedPredefines) override {
4105 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
4106 SuggestedPredefines, ExistingLangOpts);
4111 bool ASTReader::readASTFileControlBlock(StringRef Filename,
4112 FileManager &FileMgr,
4113 ASTReaderListener &Listener) {
4114 // Open the AST file.
4116 std::unique_ptr<llvm::MemoryBuffer> Buffer;
4117 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
4122 // Initialize the stream
4123 llvm::BitstreamReader StreamFile;
4124 BitstreamCursor Stream;
4125 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
4126 (const unsigned char *)Buffer->getBufferEnd());
4127 Stream.init(StreamFile);
4129 // Sniff for the signature.
4130 if (Stream.Read(8) != 'C' ||
4131 Stream.Read(8) != 'P' ||
4132 Stream.Read(8) != 'C' ||
4133 Stream.Read(8) != 'H') {
4137 // Scan for the CONTROL_BLOCK_ID block.
4138 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
4141 bool NeedsInputFiles = Listener.needsInputFileVisitation();
4142 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
4143 BitstreamCursor InputFilesCursor;
4144 if (NeedsInputFiles) {
4145 InputFilesCursor = Stream;
4146 if (SkipCursorToBlock(InputFilesCursor, INPUT_FILES_BLOCK_ID))
4149 // Read the abbreviations
4151 uint64_t Offset = InputFilesCursor.GetCurrentBitNo();
4152 unsigned Code = InputFilesCursor.ReadCode();
4154 // We expect all abbrevs to be at the start of the block.
4155 if (Code != llvm::bitc::DEFINE_ABBREV) {
4156 InputFilesCursor.JumpToBit(Offset);
4159 InputFilesCursor.ReadAbbrevRecord();
4163 // Scan for ORIGINAL_FILE inside the control block.
4166 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4167 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
4170 if (Entry.Kind != llvm::BitstreamEntry::Record)
4175 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
4176 switch ((ControlRecordTypes)RecCode) {
4178 if (Record[0] != VERSION_MAJOR)
4181 if (Listener.ReadFullVersionInformation(Blob))
4187 Listener.ReadModuleName(Blob);
4189 case MODULE_MAP_FILE: {
4191 Listener.ReadModuleMapFile(ReadString(Record, Idx));
4194 case LANGUAGE_OPTIONS:
4195 if (ParseLanguageOptions(Record, false, Listener))
4199 case TARGET_OPTIONS:
4200 if (ParseTargetOptions(Record, false, Listener))
4204 case DIAGNOSTIC_OPTIONS:
4205 if (ParseDiagnosticOptions(Record, false, Listener))
4209 case FILE_SYSTEM_OPTIONS:
4210 if (ParseFileSystemOptions(Record, false, Listener))
4214 case HEADER_SEARCH_OPTIONS:
4215 if (ParseHeaderSearchOptions(Record, false, Listener))
4219 case PREPROCESSOR_OPTIONS: {
4220 std::string IgnoredSuggestedPredefines;
4221 if (ParsePreprocessorOptions(Record, false, Listener,
4222 IgnoredSuggestedPredefines))
4227 case INPUT_FILE_OFFSETS: {
4228 if (!NeedsInputFiles)
4231 unsigned NumInputFiles = Record[0];
4232 unsigned NumUserFiles = Record[1];
4233 const uint32_t *InputFileOffs = (const uint32_t *)Blob.data();
4234 for (unsigned I = 0; I != NumInputFiles; ++I) {
4235 // Go find this input file.
4236 bool isSystemFile = I >= NumUserFiles;
4238 if (isSystemFile && !NeedsSystemInputFiles)
4239 break; // the rest are system input files
4241 BitstreamCursor &Cursor = InputFilesCursor;
4242 SavedStreamPosition SavedPosition(Cursor);
4243 Cursor.JumpToBit(InputFileOffs[I]);
4245 unsigned Code = Cursor.ReadCode();
4248 bool shouldContinue = false;
4249 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
4251 bool Overridden = static_cast<bool>(Record[3]);
4252 shouldContinue = Listener.visitInputFile(Blob, isSystemFile, Overridden);
4255 if (!shouldContinue)
4262 // No other validation to perform.
4269 bool ASTReader::isAcceptableASTFile(StringRef Filename,
4270 FileManager &FileMgr,
4271 const LangOptions &LangOpts,
4272 const TargetOptions &TargetOpts,
4273 const PreprocessorOptions &PPOpts) {
4274 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr);
4275 return !readASTFileControlBlock(Filename, FileMgr, validator);
4278 ASTReader::ASTReadResult
4279 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
4280 // Enter the submodule block.
4281 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
4282 Error("malformed submodule block record in AST file");
4286 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
4288 Module *CurrentModule = nullptr;
4291 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
4293 switch (Entry.Kind) {
4294 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
4295 case llvm::BitstreamEntry::Error:
4296 Error("malformed block record in AST file");
4298 case llvm::BitstreamEntry::EndBlock:
4300 case llvm::BitstreamEntry::Record:
4301 // The interesting case.
4308 switch (F.Stream.readRecord(Entry.ID, Record, &Blob)) {
4309 default: // Default behavior: ignore.
4312 case SUBMODULE_DEFINITION: {
4314 Error("missing submodule metadata record at beginning of block");
4318 if (Record.size() < 8) {
4319 Error("malformed module definition");
4323 StringRef Name = Blob;
4325 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
4326 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
4327 bool IsFramework = Record[Idx++];
4328 bool IsExplicit = Record[Idx++];
4329 bool IsSystem = Record[Idx++];
4330 bool IsExternC = Record[Idx++];
4331 bool InferSubmodules = Record[Idx++];
4332 bool InferExplicitSubmodules = Record[Idx++];
4333 bool InferExportWildcard = Record[Idx++];
4334 bool ConfigMacrosExhaustive = Record[Idx++];
4336 Module *ParentModule = nullptr;
4338 ParentModule = getSubmodule(Parent);
4340 // Retrieve this (sub)module from the module map, creating it if
4342 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule, IsFramework,
4345 // FIXME: set the definition loc for CurrentModule, or call
4346 // ModMap.setInferredModuleAllowedBy()
4348 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
4349 if (GlobalIndex >= SubmodulesLoaded.size() ||
4350 SubmodulesLoaded[GlobalIndex]) {
4351 Error("too many submodules");
4355 if (!ParentModule) {
4356 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
4357 if (CurFile != F.File) {
4358 if (!Diags.isDiagnosticInFlight()) {
4359 Diag(diag::err_module_file_conflict)
4360 << CurrentModule->getTopLevelModuleName()
4361 << CurFile->getName()
4362 << F.File->getName();
4368 CurrentModule->setASTFile(F.File);
4371 CurrentModule->IsFromModuleFile = true;
4372 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
4373 CurrentModule->IsExternC = IsExternC;
4374 CurrentModule->InferSubmodules = InferSubmodules;
4375 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
4376 CurrentModule->InferExportWildcard = InferExportWildcard;
4377 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
4378 if (DeserializationListener)
4379 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
4381 SubmodulesLoaded[GlobalIndex] = CurrentModule;
4383 // Clear out data that will be replaced by what is the module file.
4384 CurrentModule->LinkLibraries.clear();
4385 CurrentModule->ConfigMacros.clear();
4386 CurrentModule->UnresolvedConflicts.clear();
4387 CurrentModule->Conflicts.clear();
4391 case SUBMODULE_UMBRELLA_HEADER: {
4393 Error("missing submodule metadata record at beginning of block");
4400 if (const FileEntry *Umbrella = PP.getFileManager().getFile(Blob)) {
4401 if (!CurrentModule->getUmbrellaHeader())
4402 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
4403 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
4404 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
4405 Error("mismatched umbrella headers in submodule");
4412 case SUBMODULE_HEADER: {
4414 Error("missing submodule metadata record at beginning of block");
4421 // We lazily associate headers with their modules via the HeaderInfoTable.
4422 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4423 // of complete filenames or remove it entirely.
4427 case SUBMODULE_EXCLUDED_HEADER: {
4429 Error("missing submodule metadata record at beginning of block");
4436 // We lazily associate headers with their modules via the HeaderInfoTable.
4437 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4438 // of complete filenames or remove it entirely.
4442 case SUBMODULE_PRIVATE_HEADER: {
4444 Error("missing submodule metadata record at beginning of block");
4451 // We lazily associate headers with their modules via the HeaderInfoTable.
4452 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4453 // of complete filenames or remove it entirely.
4457 case SUBMODULE_TOPHEADER: {
4459 Error("missing submodule metadata record at beginning of block");
4466 CurrentModule->addTopHeaderFilename(Blob);
4470 case SUBMODULE_UMBRELLA_DIR: {
4472 Error("missing submodule metadata record at beginning of block");
4479 if (const DirectoryEntry *Umbrella
4480 = PP.getFileManager().getDirectory(Blob)) {
4481 if (!CurrentModule->getUmbrellaDir())
4482 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
4483 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
4484 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
4485 Error("mismatched umbrella directories in submodule");
4492 case SUBMODULE_METADATA: {
4494 Error("submodule metadata record not at beginning of block");
4499 F.BaseSubmoduleID = getTotalNumSubmodules();
4500 F.LocalNumSubmodules = Record[0];
4501 unsigned LocalBaseSubmoduleID = Record[1];
4502 if (F.LocalNumSubmodules > 0) {
4503 // Introduce the global -> local mapping for submodules within this
4505 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
4507 // Introduce the local -> global mapping for submodules within this
4509 F.SubmoduleRemap.insertOrReplace(
4510 std::make_pair(LocalBaseSubmoduleID,
4511 F.BaseSubmoduleID - LocalBaseSubmoduleID));
4513 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
4518 case SUBMODULE_IMPORTS: {
4520 Error("missing submodule metadata record at beginning of block");
4527 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
4528 UnresolvedModuleRef Unresolved;
4529 Unresolved.File = &F;
4530 Unresolved.Mod = CurrentModule;
4531 Unresolved.ID = Record[Idx];
4532 Unresolved.Kind = UnresolvedModuleRef::Import;
4533 Unresolved.IsWildcard = false;
4534 UnresolvedModuleRefs.push_back(Unresolved);
4539 case SUBMODULE_EXPORTS: {
4541 Error("missing submodule metadata record at beginning of block");
4548 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
4549 UnresolvedModuleRef Unresolved;
4550 Unresolved.File = &F;
4551 Unresolved.Mod = CurrentModule;
4552 Unresolved.ID = Record[Idx];
4553 Unresolved.Kind = UnresolvedModuleRef::Export;
4554 Unresolved.IsWildcard = Record[Idx + 1];
4555 UnresolvedModuleRefs.push_back(Unresolved);
4558 // Once we've loaded the set of exports, there's no reason to keep
4559 // the parsed, unresolved exports around.
4560 CurrentModule->UnresolvedExports.clear();
4563 case SUBMODULE_REQUIRES: {
4565 Error("missing submodule metadata record at beginning of block");
4572 CurrentModule->addRequirement(Blob, Record[0], Context.getLangOpts(),
4573 Context.getTargetInfo());
4577 case SUBMODULE_LINK_LIBRARY:
4579 Error("missing submodule metadata record at beginning of block");
4586 CurrentModule->LinkLibraries.push_back(
4587 Module::LinkLibrary(Blob, Record[0]));
4590 case SUBMODULE_CONFIG_MACRO:
4592 Error("missing submodule metadata record at beginning of block");
4599 CurrentModule->ConfigMacros.push_back(Blob.str());
4602 case SUBMODULE_CONFLICT: {
4604 Error("missing submodule metadata record at beginning of block");
4611 UnresolvedModuleRef Unresolved;
4612 Unresolved.File = &F;
4613 Unresolved.Mod = CurrentModule;
4614 Unresolved.ID = Record[0];
4615 Unresolved.Kind = UnresolvedModuleRef::Conflict;
4616 Unresolved.IsWildcard = false;
4617 Unresolved.String = Blob;
4618 UnresolvedModuleRefs.push_back(Unresolved);
4625 /// \brief Parse the record that corresponds to a LangOptions data
4628 /// This routine parses the language options from the AST file and then gives
4629 /// them to the AST listener if one is set.
4631 /// \returns true if the listener deems the file unacceptable, false otherwise.
4632 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
4634 ASTReaderListener &Listener) {
4635 LangOptions LangOpts;
4637 #define LANGOPT(Name, Bits, Default, Description) \
4638 LangOpts.Name = Record[Idx++];
4639 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
4640 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
4641 #include "clang/Basic/LangOptions.def"
4642 #define SANITIZER(NAME, ID) LangOpts.Sanitize.ID = Record[Idx++];
4643 #include "clang/Basic/Sanitizers.def"
4645 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
4646 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
4647 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
4649 unsigned Length = Record[Idx++];
4650 LangOpts.CurrentModule.assign(Record.begin() + Idx,
4651 Record.begin() + Idx + Length);
4656 for (unsigned N = Record[Idx++]; N; --N) {
4657 LangOpts.CommentOpts.BlockCommandNames.push_back(
4658 ReadString(Record, Idx));
4660 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
4662 return Listener.ReadLanguageOptions(LangOpts, Complain);
4665 bool ASTReader::ParseTargetOptions(const RecordData &Record,
4667 ASTReaderListener &Listener) {
4669 TargetOptions TargetOpts;
4670 TargetOpts.Triple = ReadString(Record, Idx);
4671 TargetOpts.CPU = ReadString(Record, Idx);
4672 TargetOpts.ABI = ReadString(Record, Idx);
4673 for (unsigned N = Record[Idx++]; N; --N) {
4674 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
4676 for (unsigned N = Record[Idx++]; N; --N) {
4677 TargetOpts.Features.push_back(ReadString(Record, Idx));
4680 return Listener.ReadTargetOptions(TargetOpts, Complain);
4683 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
4684 ASTReaderListener &Listener) {
4685 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
4687 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
4688 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
4689 DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
4690 #include "clang/Basic/DiagnosticOptions.def"
4692 for (unsigned N = Record[Idx++]; N; --N)
4693 DiagOpts->Warnings.push_back(ReadString(Record, Idx));
4694 for (unsigned N = Record[Idx++]; N; --N)
4695 DiagOpts->Remarks.push_back(ReadString(Record, Idx));
4697 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
4700 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
4701 ASTReaderListener &Listener) {
4702 FileSystemOptions FSOpts;
4704 FSOpts.WorkingDir = ReadString(Record, Idx);
4705 return Listener.ReadFileSystemOptions(FSOpts, Complain);
4708 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
4710 ASTReaderListener &Listener) {
4711 HeaderSearchOptions HSOpts;
4713 HSOpts.Sysroot = ReadString(Record, Idx);
4716 for (unsigned N = Record[Idx++]; N; --N) {
4717 std::string Path = ReadString(Record, Idx);
4718 frontend::IncludeDirGroup Group
4719 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
4720 bool IsFramework = Record[Idx++];
4721 bool IgnoreSysRoot = Record[Idx++];
4722 HSOpts.UserEntries.push_back(
4723 HeaderSearchOptions::Entry(Path, Group, IsFramework, IgnoreSysRoot));
4726 // System header prefixes.
4727 for (unsigned N = Record[Idx++]; N; --N) {
4728 std::string Prefix = ReadString(Record, Idx);
4729 bool IsSystemHeader = Record[Idx++];
4730 HSOpts.SystemHeaderPrefixes.push_back(
4731 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
4734 HSOpts.ResourceDir = ReadString(Record, Idx);
4735 HSOpts.ModuleCachePath = ReadString(Record, Idx);
4736 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
4737 HSOpts.DisableModuleHash = Record[Idx++];
4738 HSOpts.UseBuiltinIncludes = Record[Idx++];
4739 HSOpts.UseStandardSystemIncludes = Record[Idx++];
4740 HSOpts.UseStandardCXXIncludes = Record[Idx++];
4741 HSOpts.UseLibcxx = Record[Idx++];
4743 return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
4746 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
4748 ASTReaderListener &Listener,
4749 std::string &SuggestedPredefines) {
4750 PreprocessorOptions PPOpts;
4753 // Macro definitions/undefs
4754 for (unsigned N = Record[Idx++]; N; --N) {
4755 std::string Macro = ReadString(Record, Idx);
4756 bool IsUndef = Record[Idx++];
4757 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
4761 for (unsigned N = Record[Idx++]; N; --N) {
4762 PPOpts.Includes.push_back(ReadString(Record, Idx));
4766 for (unsigned N = Record[Idx++]; N; --N) {
4767 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
4770 PPOpts.UsePredefines = Record[Idx++];
4771 PPOpts.DetailedRecord = Record[Idx++];
4772 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
4773 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
4774 PPOpts.ObjCXXARCStandardLibrary =
4775 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
4776 SuggestedPredefines.clear();
4777 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
4778 SuggestedPredefines);
4781 std::pair<ModuleFile *, unsigned>
4782 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
4783 GlobalPreprocessedEntityMapType::iterator
4784 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
4785 assert(I != GlobalPreprocessedEntityMap.end() &&
4786 "Corrupted global preprocessed entity map");
4787 ModuleFile *M = I->second;
4788 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
4789 return std::make_pair(M, LocalIndex);
4792 std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
4793 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
4794 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
4795 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
4796 Mod.NumPreprocessedEntities);
4798 return std::make_pair(PreprocessingRecord::iterator(),
4799 PreprocessingRecord::iterator());
4802 std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
4803 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
4804 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4805 ModuleDeclIterator(this, &Mod,
4806 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
4809 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4810 PreprocessedEntityID PPID = Index+1;
4811 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4812 ModuleFile &M = *PPInfo.first;
4813 unsigned LocalIndex = PPInfo.second;
4814 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4816 if (!PP.getPreprocessingRecord()) {
4817 Error("no preprocessing record");
4821 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
4822 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
4824 llvm::BitstreamEntry Entry =
4825 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
4826 if (Entry.Kind != llvm::BitstreamEntry::Record)
4830 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
4831 ReadSourceLocation(M, PPOffs.End));
4832 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
4835 PreprocessorDetailRecordTypes RecType =
4836 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4837 Entry.ID, Record, &Blob);
4839 case PPD_MACRO_EXPANSION: {
4840 bool isBuiltin = Record[0];
4841 IdentifierInfo *Name = nullptr;
4842 MacroDefinition *Def = nullptr;
4844 Name = getLocalIdentifier(M, Record[1]);
4846 PreprocessedEntityID
4847 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
4848 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
4853 ME = new (PPRec) MacroExpansion(Name, Range);
4855 ME = new (PPRec) MacroExpansion(Def, Range);
4860 case PPD_MACRO_DEFINITION: {
4861 // Decode the identifier info and then check again; if the macro is
4862 // still defined and associated with the identifier,
4863 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
4865 = new (PPRec) MacroDefinition(II, Range);
4867 if (DeserializationListener)
4868 DeserializationListener->MacroDefinitionRead(PPID, MD);
4873 case PPD_INCLUSION_DIRECTIVE: {
4874 const char *FullFileNameStart = Blob.data() + Record[0];
4875 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
4876 const FileEntry *File = nullptr;
4877 if (!FullFileName.empty())
4878 File = PP.getFileManager().getFile(FullFileName);
4880 // FIXME: Stable encoding
4881 InclusionDirective::InclusionKind Kind
4882 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4883 InclusionDirective *ID
4884 = new (PPRec) InclusionDirective(PPRec, Kind,
4885 StringRef(Blob.data(), Record[0]),
4886 Record[1], Record[3],
4893 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4896 /// \brief \arg SLocMapI points at a chunk of a module that contains no
4897 /// preprocessed entities or the entities it contains are not the ones we are
4898 /// looking for. Find the next module that contains entities and return the ID
4899 /// of the first entry.
4900 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4901 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4903 for (GlobalSLocOffsetMapType::const_iterator
4904 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4905 ModuleFile &M = *SLocMapI->second;
4906 if (M.NumPreprocessedEntities)
4907 return M.BasePreprocessedEntityID;
4910 return getTotalNumPreprocessedEntities();
4915 template <unsigned PPEntityOffset::*PPLoc>
4916 struct PPEntityComp {
4917 const ASTReader &Reader;
4920 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
4922 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4923 SourceLocation LHS = getLoc(L);
4924 SourceLocation RHS = getLoc(R);
4925 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4928 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
4929 SourceLocation LHS = getLoc(L);
4930 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4933 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
4934 SourceLocation RHS = getLoc(R);
4935 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4938 SourceLocation getLoc(const PPEntityOffset &PPE) const {
4939 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
4945 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
4946 bool EndsAfter) const {
4947 if (SourceMgr.isLocalSourceLocation(Loc))
4948 return getTotalNumPreprocessedEntities();
4950 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
4951 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
4952 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4953 "Corrupted global sloc offset map");
4955 if (SLocMapI->second->NumPreprocessedEntities == 0)
4956 return findNextPreprocessedEntity(SLocMapI);
4958 ModuleFile &M = *SLocMapI->second;
4959 typedef const PPEntityOffset *pp_iterator;
4960 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4961 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4963 size_t Count = M.NumPreprocessedEntities;
4965 pp_iterator First = pp_begin;
4969 PPI = std::upper_bound(pp_begin, pp_end, Loc,
4970 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
4972 // Do a binary search manually instead of using std::lower_bound because
4973 // The end locations of entities may be unordered (when a macro expansion
4974 // is inside another macro argument), but for this case it is not important
4975 // whether we get the first macro expansion or its containing macro.
4979 std::advance(PPI, Half);
4980 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
4984 Count = Count - Half - 1;
4991 return findNextPreprocessedEntity(SLocMapI);
4993 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4996 /// \brief Returns a pair of [Begin, End) indices of preallocated
4997 /// preprocessed entities that \arg Range encompasses.
4998 std::pair<unsigned, unsigned>
4999 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
5000 if (Range.isInvalid())
5001 return std::make_pair(0,0);
5002 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
5004 PreprocessedEntityID BeginID =
5005 findPreprocessedEntity(Range.getBegin(), false);
5006 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
5007 return std::make_pair(BeginID, EndID);
5010 /// \brief Optionally returns true or false if the preallocated preprocessed
5011 /// entity with index \arg Index came from file \arg FID.
5012 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
5014 if (FID.isInvalid())
5017 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5018 ModuleFile &M = *PPInfo.first;
5019 unsigned LocalIndex = PPInfo.second;
5020 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5022 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
5023 if (Loc.isInvalid())
5026 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
5033 /// \brief Visitor used to search for information about a header file.
5034 class HeaderFileInfoVisitor {
5035 const FileEntry *FE;
5037 Optional<HeaderFileInfo> HFI;
5040 explicit HeaderFileInfoVisitor(const FileEntry *FE)
5043 static bool visit(ModuleFile &M, void *UserData) {
5044 HeaderFileInfoVisitor *This
5045 = static_cast<HeaderFileInfoVisitor *>(UserData);
5047 HeaderFileInfoLookupTable *Table
5048 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
5052 // Look in the on-disk hash table for an entry for this file name.
5053 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE);
5054 if (Pos == Table->end())
5061 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
5065 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
5066 HeaderFileInfoVisitor Visitor(FE);
5067 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
5068 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
5071 return HeaderFileInfo();
5074 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
5075 // FIXME: Make it work properly with modules.
5076 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
5077 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
5078 ModuleFile &F = *(*I);
5081 assert(!Diag.DiagStates.empty());
5082 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
5083 while (Idx < F.PragmaDiagMappings.size()) {
5084 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
5085 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
5086 if (DiagStateID != 0) {
5087 Diag.DiagStatePoints.push_back(
5088 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
5089 FullSourceLoc(Loc, SourceMgr)));
5093 assert(DiagStateID == 0);
5094 // A new DiagState was created here.
5095 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
5096 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
5097 DiagStates.push_back(NewState);
5098 Diag.DiagStatePoints.push_back(
5099 DiagnosticsEngine::DiagStatePoint(NewState,
5100 FullSourceLoc(Loc, SourceMgr)));
5102 assert(Idx < F.PragmaDiagMappings.size() &&
5103 "Invalid data, didn't find '-1' marking end of diag/map pairs");
5104 if (Idx >= F.PragmaDiagMappings.size()) {
5105 break; // Something is messed up but at least avoid infinite loop in
5108 unsigned DiagID = F.PragmaDiagMappings[Idx++];
5109 if (DiagID == (unsigned)-1) {
5110 break; // no more diag/map pairs for this location.
5112 diag::Severity Map = (diag::Severity)F.PragmaDiagMappings[Idx++];
5113 DiagnosticMapping Mapping = Diag.makeUserMapping(Map, Loc);
5114 Diag.GetCurDiagState()->setMapping(DiagID, Mapping);
5120 /// \brief Get the correct cursor and offset for loading a type.
5121 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
5122 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
5123 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
5124 ModuleFile *M = I->second;
5125 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
5128 /// \brief Read and return the type with the given index..
5130 /// The index is the type ID, shifted and minus the number of predefs. This
5131 /// routine actually reads the record corresponding to the type at the given
5132 /// location. It is a helper routine for GetType, which deals with reading type
5134 QualType ASTReader::readTypeRecord(unsigned Index) {
5135 RecordLocation Loc = TypeCursorForIndex(Index);
5136 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
5138 // Keep track of where we are in the stream, then jump back there
5139 // after reading this type.
5140 SavedStreamPosition SavedPosition(DeclsCursor);
5142 ReadingKindTracker ReadingKind(Read_Type, *this);
5144 // Note that we are loading a type record.
5145 Deserializing AType(this);
5148 DeclsCursor.JumpToBit(Loc.Offset);
5150 unsigned Code = DeclsCursor.ReadCode();
5151 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
5152 case TYPE_EXT_QUAL: {
5153 if (Record.size() != 2) {
5154 Error("Incorrect encoding of extended qualifier type");
5157 QualType Base = readType(*Loc.F, Record, Idx);
5158 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
5159 return Context.getQualifiedType(Base, Quals);
5162 case TYPE_COMPLEX: {
5163 if (Record.size() != 1) {
5164 Error("Incorrect encoding of complex type");
5167 QualType ElemType = readType(*Loc.F, Record, Idx);
5168 return Context.getComplexType(ElemType);
5171 case TYPE_POINTER: {
5172 if (Record.size() != 1) {
5173 Error("Incorrect encoding of pointer type");
5176 QualType PointeeType = readType(*Loc.F, Record, Idx);
5177 return Context.getPointerType(PointeeType);
5180 case TYPE_DECAYED: {
5181 if (Record.size() != 1) {
5182 Error("Incorrect encoding of decayed type");
5185 QualType OriginalType = readType(*Loc.F, Record, Idx);
5186 QualType DT = Context.getAdjustedParameterType(OriginalType);
5187 if (!isa<DecayedType>(DT))
5188 Error("Decayed type does not decay");
5192 case TYPE_ADJUSTED: {
5193 if (Record.size() != 2) {
5194 Error("Incorrect encoding of adjusted type");
5197 QualType OriginalTy = readType(*Loc.F, Record, Idx);
5198 QualType AdjustedTy = readType(*Loc.F, Record, Idx);
5199 return Context.getAdjustedType(OriginalTy, AdjustedTy);
5202 case TYPE_BLOCK_POINTER: {
5203 if (Record.size() != 1) {
5204 Error("Incorrect encoding of block pointer type");
5207 QualType PointeeType = readType(*Loc.F, Record, Idx);
5208 return Context.getBlockPointerType(PointeeType);
5211 case TYPE_LVALUE_REFERENCE: {
5212 if (Record.size() != 2) {
5213 Error("Incorrect encoding of lvalue reference type");
5216 QualType PointeeType = readType(*Loc.F, Record, Idx);
5217 return Context.getLValueReferenceType(PointeeType, Record[1]);
5220 case TYPE_RVALUE_REFERENCE: {
5221 if (Record.size() != 1) {
5222 Error("Incorrect encoding of rvalue reference type");
5225 QualType PointeeType = readType(*Loc.F, Record, Idx);
5226 return Context.getRValueReferenceType(PointeeType);
5229 case TYPE_MEMBER_POINTER: {
5230 if (Record.size() != 2) {
5231 Error("Incorrect encoding of member pointer type");
5234 QualType PointeeType = readType(*Loc.F, Record, Idx);
5235 QualType ClassType = readType(*Loc.F, Record, Idx);
5236 if (PointeeType.isNull() || ClassType.isNull())
5239 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
5242 case TYPE_CONSTANT_ARRAY: {
5243 QualType ElementType = readType(*Loc.F, Record, Idx);
5244 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5245 unsigned IndexTypeQuals = Record[2];
5247 llvm::APInt Size = ReadAPInt(Record, Idx);
5248 return Context.getConstantArrayType(ElementType, Size,
5249 ASM, IndexTypeQuals);
5252 case TYPE_INCOMPLETE_ARRAY: {
5253 QualType ElementType = readType(*Loc.F, Record, Idx);
5254 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5255 unsigned IndexTypeQuals = Record[2];
5256 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
5259 case TYPE_VARIABLE_ARRAY: {
5260 QualType ElementType = readType(*Loc.F, Record, Idx);
5261 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5262 unsigned IndexTypeQuals = Record[2];
5263 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
5264 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
5265 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
5266 ASM, IndexTypeQuals,
5267 SourceRange(LBLoc, RBLoc));
5271 if (Record.size() != 3) {
5272 Error("incorrect encoding of vector type in AST file");
5276 QualType ElementType = readType(*Loc.F, Record, Idx);
5277 unsigned NumElements = Record[1];
5278 unsigned VecKind = Record[2];
5279 return Context.getVectorType(ElementType, NumElements,
5280 (VectorType::VectorKind)VecKind);
5283 case TYPE_EXT_VECTOR: {
5284 if (Record.size() != 3) {
5285 Error("incorrect encoding of extended vector type in AST file");
5289 QualType ElementType = readType(*Loc.F, Record, Idx);
5290 unsigned NumElements = Record[1];
5291 return Context.getExtVectorType(ElementType, NumElements);
5294 case TYPE_FUNCTION_NO_PROTO: {
5295 if (Record.size() != 6) {
5296 Error("incorrect encoding of no-proto function type");
5299 QualType ResultType = readType(*Loc.F, Record, Idx);
5300 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
5301 (CallingConv)Record[4], Record[5]);
5302 return Context.getFunctionNoProtoType(ResultType, Info);
5305 case TYPE_FUNCTION_PROTO: {
5306 QualType ResultType = readType(*Loc.F, Record, Idx);
5308 FunctionProtoType::ExtProtoInfo EPI;
5309 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
5310 /*hasregparm*/ Record[2],
5311 /*regparm*/ Record[3],
5312 static_cast<CallingConv>(Record[4]),
5313 /*produces*/ Record[5]);
5317 EPI.Variadic = Record[Idx++];
5318 EPI.HasTrailingReturn = Record[Idx++];
5319 EPI.TypeQuals = Record[Idx++];
5320 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
5321 SmallVector<QualType, 8> ExceptionStorage;
5322 readExceptionSpec(*Loc.F, ExceptionStorage, EPI.ExceptionSpec, Record, Idx);
5324 unsigned NumParams = Record[Idx++];
5325 SmallVector<QualType, 16> ParamTypes;
5326 for (unsigned I = 0; I != NumParams; ++I)
5327 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
5329 return Context.getFunctionType(ResultType, ParamTypes, EPI);
5332 case TYPE_UNRESOLVED_USING: {
5334 return Context.getTypeDeclType(
5335 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
5338 case TYPE_TYPEDEF: {
5339 if (Record.size() != 2) {
5340 Error("incorrect encoding of typedef type");
5344 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
5345 QualType Canonical = readType(*Loc.F, Record, Idx);
5346 if (!Canonical.isNull())
5347 Canonical = Context.getCanonicalType(Canonical);
5348 return Context.getTypedefType(Decl, Canonical);
5351 case TYPE_TYPEOF_EXPR:
5352 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
5355 if (Record.size() != 1) {
5356 Error("incorrect encoding of typeof(type) in AST file");
5359 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5360 return Context.getTypeOfType(UnderlyingType);
5363 case TYPE_DECLTYPE: {
5364 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5365 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
5368 case TYPE_UNARY_TRANSFORM: {
5369 QualType BaseType = readType(*Loc.F, Record, Idx);
5370 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5371 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
5372 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
5376 QualType Deduced = readType(*Loc.F, Record, Idx);
5377 bool IsDecltypeAuto = Record[Idx++];
5378 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
5379 return Context.getAutoType(Deduced, IsDecltypeAuto, IsDependent);
5383 if (Record.size() != 2) {
5384 Error("incorrect encoding of record type");
5388 bool IsDependent = Record[Idx++];
5389 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
5390 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
5391 QualType T = Context.getRecordType(RD);
5392 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5397 if (Record.size() != 2) {
5398 Error("incorrect encoding of enum type");
5402 bool IsDependent = Record[Idx++];
5404 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
5405 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5409 case TYPE_ATTRIBUTED: {
5410 if (Record.size() != 3) {
5411 Error("incorrect encoding of attributed type");
5414 QualType modifiedType = readType(*Loc.F, Record, Idx);
5415 QualType equivalentType = readType(*Loc.F, Record, Idx);
5416 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
5417 return Context.getAttributedType(kind, modifiedType, equivalentType);
5421 if (Record.size() != 1) {
5422 Error("incorrect encoding of paren type");
5425 QualType InnerType = readType(*Loc.F, Record, Idx);
5426 return Context.getParenType(InnerType);
5429 case TYPE_PACK_EXPANSION: {
5430 if (Record.size() != 2) {
5431 Error("incorrect encoding of pack expansion type");
5434 QualType Pattern = readType(*Loc.F, Record, Idx);
5435 if (Pattern.isNull())
5437 Optional<unsigned> NumExpansions;
5439 NumExpansions = Record[1] - 1;
5440 return Context.getPackExpansionType(Pattern, NumExpansions);
5443 case TYPE_ELABORATED: {
5445 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5446 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5447 QualType NamedType = readType(*Loc.F, Record, Idx);
5448 return Context.getElaboratedType(Keyword, NNS, NamedType);
5451 case TYPE_OBJC_INTERFACE: {
5453 ObjCInterfaceDecl *ItfD
5454 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
5455 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
5458 case TYPE_OBJC_OBJECT: {
5460 QualType Base = readType(*Loc.F, Record, Idx);
5461 unsigned NumProtos = Record[Idx++];
5462 SmallVector<ObjCProtocolDecl*, 4> Protos;
5463 for (unsigned I = 0; I != NumProtos; ++I)
5464 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
5465 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
5468 case TYPE_OBJC_OBJECT_POINTER: {
5470 QualType Pointee = readType(*Loc.F, Record, Idx);
5471 return Context.getObjCObjectPointerType(Pointee);
5474 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
5476 QualType Parm = readType(*Loc.F, Record, Idx);
5477 QualType Replacement = readType(*Loc.F, Record, Idx);
5478 return Context.getSubstTemplateTypeParmType(
5479 cast<TemplateTypeParmType>(Parm),
5480 Context.getCanonicalType(Replacement));
5483 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
5485 QualType Parm = readType(*Loc.F, Record, Idx);
5486 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
5487 return Context.getSubstTemplateTypeParmPackType(
5488 cast<TemplateTypeParmType>(Parm),
5492 case TYPE_INJECTED_CLASS_NAME: {
5493 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
5494 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
5495 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
5496 // for AST reading, too much interdependencies.
5498 if (const Type *Existing = D->getTypeForDecl())
5500 else if (auto *Prev = D->getPreviousDecl())
5501 T = Prev->getTypeForDecl();
5503 T = new (Context, TypeAlignment) InjectedClassNameType(D, TST);
5504 return QualType(T, 0);
5507 case TYPE_TEMPLATE_TYPE_PARM: {
5509 unsigned Depth = Record[Idx++];
5510 unsigned Index = Record[Idx++];
5511 bool Pack = Record[Idx++];
5512 TemplateTypeParmDecl *D
5513 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
5514 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
5517 case TYPE_DEPENDENT_NAME: {
5519 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5520 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5521 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
5522 QualType Canon = readType(*Loc.F, Record, Idx);
5523 if (!Canon.isNull())
5524 Canon = Context.getCanonicalType(Canon);
5525 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
5528 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
5530 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5531 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5532 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
5533 unsigned NumArgs = Record[Idx++];
5534 SmallVector<TemplateArgument, 8> Args;
5535 Args.reserve(NumArgs);
5537 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
5538 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
5539 Args.size(), Args.data());
5542 case TYPE_DEPENDENT_SIZED_ARRAY: {
5546 QualType ElementType = readType(*Loc.F, Record, Idx);
5547 ArrayType::ArraySizeModifier ASM
5548 = (ArrayType::ArraySizeModifier)Record[Idx++];
5549 unsigned IndexTypeQuals = Record[Idx++];
5551 // DependentSizedArrayType
5552 Expr *NumElts = ReadExpr(*Loc.F);
5553 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
5555 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
5556 IndexTypeQuals, Brackets);
5559 case TYPE_TEMPLATE_SPECIALIZATION: {
5561 bool IsDependent = Record[Idx++];
5562 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
5563 SmallVector<TemplateArgument, 8> Args;
5564 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
5565 QualType Underlying = readType(*Loc.F, Record, Idx);
5567 if (Underlying.isNull())
5568 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
5571 T = Context.getTemplateSpecializationType(Name, Args.data(),
5572 Args.size(), Underlying);
5573 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5578 if (Record.size() != 1) {
5579 Error("Incorrect encoding of atomic type");
5582 QualType ValueType = readType(*Loc.F, Record, Idx);
5583 return Context.getAtomicType(ValueType);
5586 llvm_unreachable("Invalid TypeCode!");
5589 void ASTReader::readExceptionSpec(ModuleFile &ModuleFile,
5590 SmallVectorImpl<QualType> &Exceptions,
5591 FunctionProtoType::ExceptionSpecInfo &ESI,
5592 const RecordData &Record, unsigned &Idx) {
5593 ExceptionSpecificationType EST =
5594 static_cast<ExceptionSpecificationType>(Record[Idx++]);
5596 if (EST == EST_Dynamic) {
5597 for (unsigned I = 0, N = Record[Idx++]; I != N; ++I)
5598 Exceptions.push_back(readType(ModuleFile, Record, Idx));
5599 ESI.Exceptions = Exceptions;
5600 } else if (EST == EST_ComputedNoexcept) {
5601 ESI.NoexceptExpr = ReadExpr(ModuleFile);
5602 } else if (EST == EST_Uninstantiated) {
5603 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5604 ESI.SourceTemplate = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5605 } else if (EST == EST_Unevaluated) {
5606 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5610 class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
5613 const ASTReader::RecordData &Record;
5616 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
5618 return Reader.ReadSourceLocation(F, R, I);
5621 template<typename T>
5622 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
5623 return Reader.ReadDeclAs<T>(F, Record, Idx);
5627 TypeLocReader(ASTReader &Reader, ModuleFile &F,
5628 const ASTReader::RecordData &Record, unsigned &Idx)
5629 : Reader(Reader), F(F), Record(Record), Idx(Idx)
5632 // We want compile-time assurance that we've enumerated all of
5633 // these, so unfortunately we have to declare them first, then
5634 // define them out-of-line.
5635 #define ABSTRACT_TYPELOC(CLASS, PARENT)
5636 #define TYPELOC(CLASS, PARENT) \
5637 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
5638 #include "clang/AST/TypeLocNodes.def"
5640 void VisitFunctionTypeLoc(FunctionTypeLoc);
5641 void VisitArrayTypeLoc(ArrayTypeLoc);
5644 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
5647 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
5648 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
5649 if (TL.needsExtraLocalData()) {
5650 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
5651 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
5652 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
5653 TL.setModeAttr(Record[Idx++]);
5656 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
5657 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5659 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
5660 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5662 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
5665 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
5668 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
5669 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
5671 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
5672 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
5674 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
5675 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
5677 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
5678 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5679 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5681 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
5682 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
5683 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
5685 TL.setSizeExpr(Reader.ReadExpr(F));
5687 TL.setSizeExpr(nullptr);
5689 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
5690 VisitArrayTypeLoc(TL);
5692 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
5693 VisitArrayTypeLoc(TL);
5695 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
5696 VisitArrayTypeLoc(TL);
5698 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
5699 DependentSizedArrayTypeLoc TL) {
5700 VisitArrayTypeLoc(TL);
5702 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
5703 DependentSizedExtVectorTypeLoc TL) {
5704 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5706 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
5707 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5709 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
5710 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5712 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
5713 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
5714 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5715 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5716 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
5717 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
5718 TL.setParam(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
5721 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
5722 VisitFunctionTypeLoc(TL);
5724 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
5725 VisitFunctionTypeLoc(TL);
5727 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
5728 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5730 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
5731 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5733 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
5734 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5735 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5736 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5738 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
5739 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5740 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5741 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5742 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5744 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
5745 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5747 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
5748 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5749 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5750 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5751 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5753 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
5754 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5756 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
5757 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5759 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
5760 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5762 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
5763 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
5764 if (TL.hasAttrOperand()) {
5766 range.setBegin(ReadSourceLocation(Record, Idx));
5767 range.setEnd(ReadSourceLocation(Record, Idx));
5768 TL.setAttrOperandParensRange(range);
5770 if (TL.hasAttrExprOperand()) {
5772 TL.setAttrExprOperand(Reader.ReadExpr(F));
5774 TL.setAttrExprOperand(nullptr);
5775 } else if (TL.hasAttrEnumOperand())
5776 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
5778 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
5779 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5781 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
5782 SubstTemplateTypeParmTypeLoc TL) {
5783 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5785 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
5786 SubstTemplateTypeParmPackTypeLoc TL) {
5787 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5789 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
5790 TemplateSpecializationTypeLoc TL) {
5791 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5792 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5793 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5794 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5795 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
5797 Reader.GetTemplateArgumentLocInfo(F,
5798 TL.getTypePtr()->getArg(i).getKind(),
5801 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
5802 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5803 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5805 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
5806 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5807 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5809 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
5810 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5812 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
5813 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5814 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5815 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5817 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5818 DependentTemplateSpecializationTypeLoc TL) {
5819 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5820 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5821 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5822 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5823 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5824 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5825 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5827 Reader.GetTemplateArgumentLocInfo(F,
5828 TL.getTypePtr()->getArg(I).getKind(),
5831 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5832 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5834 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
5835 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5837 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5838 TL.setHasBaseTypeAsWritten(Record[Idx++]);
5839 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5840 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5841 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
5842 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
5844 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
5845 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5847 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5848 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5849 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5850 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5853 TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5854 const RecordData &Record,
5856 QualType InfoTy = readType(F, Record, Idx);
5857 if (InfoTy.isNull())
5860 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5861 TypeLocReader TLR(*this, F, Record, Idx);
5862 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5867 QualType ASTReader::GetType(TypeID ID) {
5868 unsigned FastQuals = ID & Qualifiers::FastMask;
5869 unsigned Index = ID >> Qualifiers::FastWidth;
5871 if (Index < NUM_PREDEF_TYPE_IDS) {
5873 switch ((PredefinedTypeIDs)Index) {
5874 case PREDEF_TYPE_NULL_ID: return QualType();
5875 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
5876 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
5878 case PREDEF_TYPE_CHAR_U_ID:
5879 case PREDEF_TYPE_CHAR_S_ID:
5880 // FIXME: Check that the signedness of CharTy is correct!
5884 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
5885 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
5886 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
5887 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
5888 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
5889 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
5890 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
5891 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
5892 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
5893 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
5894 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
5895 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
5896 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
5897 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
5898 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
5899 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
5900 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
5901 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
5902 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
5903 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
5904 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
5905 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
5906 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
5907 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
5908 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
5909 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
5910 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
5911 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
5912 case PREDEF_TYPE_IMAGE1D_ID: T = Context.OCLImage1dTy; break;
5913 case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break;
5914 case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break;
5915 case PREDEF_TYPE_IMAGE2D_ID: T = Context.OCLImage2dTy; break;
5916 case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break;
5917 case PREDEF_TYPE_IMAGE3D_ID: T = Context.OCLImage3dTy; break;
5918 case PREDEF_TYPE_SAMPLER_ID: T = Context.OCLSamplerTy; break;
5919 case PREDEF_TYPE_EVENT_ID: T = Context.OCLEventTy; break;
5920 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
5922 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
5923 T = Context.getAutoRRefDeductType();
5926 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
5927 T = Context.ARCUnbridgedCastTy;
5930 case PREDEF_TYPE_VA_LIST_TAG:
5931 T = Context.getVaListTagType();
5934 case PREDEF_TYPE_BUILTIN_FN:
5935 T = Context.BuiltinFnTy;
5939 assert(!T.isNull() && "Unknown predefined type");
5940 return T.withFastQualifiers(FastQuals);
5943 Index -= NUM_PREDEF_TYPE_IDS;
5944 assert(Index < TypesLoaded.size() && "Type index out-of-range");
5945 if (TypesLoaded[Index].isNull()) {
5946 TypesLoaded[Index] = readTypeRecord(Index);
5947 if (TypesLoaded[Index].isNull())
5950 TypesLoaded[Index]->setFromAST();
5951 if (DeserializationListener)
5952 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
5953 TypesLoaded[Index]);
5956 return TypesLoaded[Index].withFastQualifiers(FastQuals);
5959 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
5960 return GetType(getGlobalTypeID(F, LocalID));
5963 serialization::TypeID
5964 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
5965 unsigned FastQuals = LocalID & Qualifiers::FastMask;
5966 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
5968 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
5971 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5972 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
5973 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
5975 unsigned GlobalIndex = LocalIndex + I->second;
5976 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
5979 TemplateArgumentLocInfo
5980 ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
5981 TemplateArgument::ArgKind Kind,
5982 const RecordData &Record,
5985 case TemplateArgument::Expression:
5987 case TemplateArgument::Type:
5988 return GetTypeSourceInfo(F, Record, Index);
5989 case TemplateArgument::Template: {
5990 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5992 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5993 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5996 case TemplateArgument::TemplateExpansion: {
5997 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5999 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
6000 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
6001 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
6004 case TemplateArgument::Null:
6005 case TemplateArgument::Integral:
6006 case TemplateArgument::Declaration:
6007 case TemplateArgument::NullPtr:
6008 case TemplateArgument::Pack:
6009 // FIXME: Is this right?
6010 return TemplateArgumentLocInfo();
6012 llvm_unreachable("unexpected template argument loc");
6016 ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
6017 const RecordData &Record, unsigned &Index) {
6018 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
6020 if (Arg.getKind() == TemplateArgument::Expression) {
6021 if (Record[Index++]) // bool InfoHasSameExpr.
6022 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
6024 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
6028 const ASTTemplateArgumentListInfo*
6029 ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
6030 const RecordData &Record,
6032 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
6033 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
6034 unsigned NumArgsAsWritten = Record[Index++];
6035 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
6036 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
6037 TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
6038 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
6041 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
6045 void ASTReader::CompleteRedeclChain(const Decl *D) {
6046 if (NumCurrentElementsDeserializing) {
6047 // We arrange to not care about the complete redeclaration chain while we're
6048 // deserializing. Just remember that the AST has marked this one as complete
6049 // but that it's not actually complete yet, so we know we still need to
6050 // complete it later.
6051 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
6055 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
6057 // If this is a named declaration, complete it by looking it up
6058 // within its context.
6060 // FIXME: We don't currently handle the cases where we can't do this;
6061 // merging a class definition that contains unnamed entities should merge
6062 // those entities. Likewise, merging a function definition should merge
6063 // all mergeable entities within it.
6064 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
6065 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
6066 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
6067 auto *II = Name.getAsIdentifierInfo();
6068 if (isa<TranslationUnitDecl>(DC) && II) {
6069 // Outside of C++, we don't have a lookup table for the TU, so update
6070 // the identifier instead. In C++, either way should work fine.
6071 if (II->isOutOfDate())
6072 updateOutOfDateIdentifier(*II);
6079 uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M,
6080 const RecordData &Record,
6082 if (Idx >= Record.size() || Record[Idx] > M.LocalNumCXXBaseSpecifiers) {
6083 Error("malformed AST file: missing C++ base specifier");
6087 unsigned LocalID = Record[Idx++];
6088 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
6091 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
6092 RecordLocation Loc = getLocalBitOffset(Offset);
6093 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
6094 SavedStreamPosition SavedPosition(Cursor);
6095 Cursor.JumpToBit(Loc.Offset);
6096 ReadingKindTracker ReadingKind(Read_Decl, *this);
6098 unsigned Code = Cursor.ReadCode();
6099 unsigned RecCode = Cursor.readRecord(Code, Record);
6100 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
6101 Error("malformed AST file: missing C++ base specifiers");
6106 unsigned NumBases = Record[Idx++];
6107 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
6108 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
6109 for (unsigned I = 0; I != NumBases; ++I)
6110 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
6114 serialization::DeclID
6115 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
6116 if (LocalID < NUM_PREDEF_DECL_IDS)
6119 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6120 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
6121 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
6123 return LocalID + I->second;
6126 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
6127 ModuleFile &M) const {
6128 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
6129 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6130 return &M == I->second;
6133 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
6134 if (!D->isFromASTFile())
6136 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
6137 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6141 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
6142 if (ID < NUM_PREDEF_DECL_IDS)
6143 return SourceLocation();
6145 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6147 if (Index > DeclsLoaded.size()) {
6148 Error("declaration ID out-of-range for AST file");
6149 return SourceLocation();
6152 if (Decl *D = DeclsLoaded[Index])
6153 return D->getLocation();
6155 unsigned RawLocation = 0;
6156 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
6157 return ReadSourceLocation(*Rec.F, RawLocation);
6160 Decl *ASTReader::GetExistingDecl(DeclID ID) {
6161 if (ID < NUM_PREDEF_DECL_IDS) {
6162 switch ((PredefinedDeclIDs)ID) {
6163 case PREDEF_DECL_NULL_ID:
6166 case PREDEF_DECL_TRANSLATION_UNIT_ID:
6167 return Context.getTranslationUnitDecl();
6169 case PREDEF_DECL_OBJC_ID_ID:
6170 return Context.getObjCIdDecl();
6172 case PREDEF_DECL_OBJC_SEL_ID:
6173 return Context.getObjCSelDecl();
6175 case PREDEF_DECL_OBJC_CLASS_ID:
6176 return Context.getObjCClassDecl();
6178 case PREDEF_DECL_OBJC_PROTOCOL_ID:
6179 return Context.getObjCProtocolDecl();
6181 case PREDEF_DECL_INT_128_ID:
6182 return Context.getInt128Decl();
6184 case PREDEF_DECL_UNSIGNED_INT_128_ID:
6185 return Context.getUInt128Decl();
6187 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
6188 return Context.getObjCInstanceTypeDecl();
6190 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
6191 return Context.getBuiltinVaListDecl();
6195 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6197 if (Index >= DeclsLoaded.size()) {
6198 assert(0 && "declaration ID out-of-range for AST file");
6199 Error("declaration ID out-of-range for AST file");
6203 return DeclsLoaded[Index];
6206 Decl *ASTReader::GetDecl(DeclID ID) {
6207 if (ID < NUM_PREDEF_DECL_IDS)
6208 return GetExistingDecl(ID);
6210 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6212 if (Index >= DeclsLoaded.size()) {
6213 assert(0 && "declaration ID out-of-range for AST file");
6214 Error("declaration ID out-of-range for AST file");
6218 if (!DeclsLoaded[Index]) {
6220 if (DeserializationListener)
6221 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
6224 return DeclsLoaded[Index];
6227 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
6229 if (GlobalID < NUM_PREDEF_DECL_IDS)
6232 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
6233 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6234 ModuleFile *Owner = I->second;
6236 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
6237 = M.GlobalToLocalDeclIDs.find(Owner);
6238 if (Pos == M.GlobalToLocalDeclIDs.end())
6241 return GlobalID - Owner->BaseDeclID + Pos->second;
6244 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
6245 const RecordData &Record,
6247 if (Idx >= Record.size()) {
6248 Error("Corrupted AST file");
6252 return getGlobalDeclID(F, Record[Idx++]);
6255 /// \brief Resolve the offset of a statement into a statement.
6257 /// This operation will read a new statement from the external
6258 /// source each time it is called, and is meant to be used via a
6259 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
6260 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
6261 // Switch case IDs are per Decl.
6262 ClearSwitchCaseIDs();
6264 // Offset here is a global offset across the entire chain.
6265 RecordLocation Loc = getLocalBitOffset(Offset);
6266 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
6267 return ReadStmtFromStream(*Loc.F);
6271 class FindExternalLexicalDeclsVisitor {
6273 const DeclContext *DC;
6274 bool (*isKindWeWant)(Decl::Kind);
6276 SmallVectorImpl<Decl*> &Decls;
6277 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
6280 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
6281 bool (*isKindWeWant)(Decl::Kind),
6282 SmallVectorImpl<Decl*> &Decls)
6283 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
6285 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
6286 PredefsVisited[I] = false;
6289 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
6293 FindExternalLexicalDeclsVisitor *This
6294 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
6296 ModuleFile::DeclContextInfosMap::iterator Info
6297 = M.DeclContextInfos.find(This->DC);
6298 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
6301 // Load all of the declaration IDs
6302 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
6303 *IDE = ID + Info->second.NumLexicalDecls;
6305 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
6308 // Don't add predefined declarations to the lexical context more
6310 if (ID->second < NUM_PREDEF_DECL_IDS) {
6311 if (This->PredefsVisited[ID->second])
6314 This->PredefsVisited[ID->second] = true;
6317 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
6318 if (!This->DC->isDeclInLexicalTraversal(D))
6319 This->Decls.push_back(D);
6328 ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
6329 bool (*isKindWeWant)(Decl::Kind),
6330 SmallVectorImpl<Decl*> &Decls) {
6331 // There might be lexical decls in multiple modules, for the TU at
6332 // least. Walk all of the modules in the order they were loaded.
6333 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
6334 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
6335 ++NumLexicalDeclContextsRead;
6346 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
6348 bool operator()(LocalDeclID L, LocalDeclID R) const {
6349 SourceLocation LHS = getLocation(L);
6350 SourceLocation RHS = getLocation(R);
6351 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6354 bool operator()(SourceLocation LHS, LocalDeclID R) const {
6355 SourceLocation RHS = getLocation(R);
6356 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6359 bool operator()(LocalDeclID L, SourceLocation RHS) const {
6360 SourceLocation LHS = getLocation(L);
6361 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6364 SourceLocation getLocation(LocalDeclID ID) const {
6365 return Reader.getSourceManager().getFileLoc(
6366 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
6372 void ASTReader::FindFileRegionDecls(FileID File,
6373 unsigned Offset, unsigned Length,
6374 SmallVectorImpl<Decl *> &Decls) {
6375 SourceManager &SM = getSourceManager();
6377 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
6378 if (I == FileDeclIDs.end())
6381 FileDeclsInfo &DInfo = I->second;
6382 if (DInfo.Decls.empty())
6386 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
6387 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
6389 DeclIDComp DIDComp(*this, *DInfo.Mod);
6390 ArrayRef<serialization::LocalDeclID>::iterator
6391 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6393 if (BeginIt != DInfo.Decls.begin())
6396 // If we are pointing at a top-level decl inside an objc container, we need
6397 // to backtrack until we find it otherwise we will fail to report that the
6398 // region overlaps with an objc container.
6399 while (BeginIt != DInfo.Decls.begin() &&
6400 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
6401 ->isTopLevelDeclInObjCContainer())
6404 ArrayRef<serialization::LocalDeclID>::iterator
6405 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6407 if (EndIt != DInfo.Decls.end())
6410 for (ArrayRef<serialization::LocalDeclID>::iterator
6411 DIt = BeginIt; DIt != EndIt; ++DIt)
6412 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
6416 /// \brief ModuleFile visitor used to perform name lookup into a
6417 /// declaration context.
6418 class DeclContextNameLookupVisitor {
6420 ArrayRef<const DeclContext *> Contexts;
6421 DeclarationName Name;
6422 SmallVectorImpl<NamedDecl *> &Decls;
6425 DeclContextNameLookupVisitor(ASTReader &Reader,
6426 ArrayRef<const DeclContext *> Contexts,
6427 DeclarationName Name,
6428 SmallVectorImpl<NamedDecl *> &Decls)
6429 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
6431 static bool visit(ModuleFile &M, void *UserData) {
6432 DeclContextNameLookupVisitor *This
6433 = static_cast<DeclContextNameLookupVisitor *>(UserData);
6435 // Check whether we have any visible declaration information for
6436 // this context in this module.
6437 ModuleFile::DeclContextInfosMap::iterator Info;
6438 bool FoundInfo = false;
6439 for (auto *DC : This->Contexts) {
6440 Info = M.DeclContextInfos.find(DC);
6441 if (Info != M.DeclContextInfos.end() &&
6442 Info->second.NameLookupTableData) {
6451 // Look for this name within this module.
6452 ASTDeclContextNameLookupTable *LookupTable =
6453 Info->second.NameLookupTableData;
6454 ASTDeclContextNameLookupTable::iterator Pos
6455 = LookupTable->find(This->Name);
6456 if (Pos == LookupTable->end())
6459 bool FoundAnything = false;
6460 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
6461 for (; Data.first != Data.second; ++Data.first) {
6462 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
6466 if (ND->getDeclName() != This->Name) {
6467 // A name might be null because the decl's redeclarable part is
6468 // currently read before reading its name. The lookup is triggered by
6469 // building that decl (likely indirectly), and so it is later in the
6470 // sense of "already existing" and can be ignored here.
6471 // FIXME: This should not happen; deserializing declarations should
6472 // not perform lookups since that can lead to deserialization cycles.
6476 // Record this declaration.
6477 FoundAnything = true;
6478 This->Decls.push_back(ND);
6481 return FoundAnything;
6486 /// \brief Retrieve the "definitive" module file for the definition of the
6487 /// given declaration context, if there is one.
6489 /// The "definitive" module file is the only place where we need to look to
6490 /// find information about the declarations within the given declaration
6491 /// context. For example, C++ and Objective-C classes, C structs/unions, and
6492 /// Objective-C protocols, categories, and extensions are all defined in a
6493 /// single place in the source code, so they have definitive module files
6494 /// associated with them. C++ namespaces, on the other hand, can have
6495 /// definitions in multiple different module files.
6497 /// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's
6498 /// NDEBUG checking.
6499 static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC,
6500 ASTReader &Reader) {
6501 if (const DeclContext *DefDC = getDefinitiveDeclContext(DC))
6502 return Reader.getOwningModuleFile(cast<Decl>(DefDC));
6508 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
6509 DeclarationName Name) {
6510 assert(DC->hasExternalVisibleStorage() &&
6511 "DeclContext has no visible decls in storage");
6515 Deserializing LookupResults(this);
6517 SmallVector<NamedDecl *, 64> Decls;
6519 // Compute the declaration contexts we need to look into. Multiple such
6520 // declaration contexts occur when two declaration contexts from disjoint
6521 // modules get merged, e.g., when two namespaces with the same name are
6522 // independently defined in separate modules.
6523 SmallVector<const DeclContext *, 2> Contexts;
6524 Contexts.push_back(DC);
6526 if (DC->isNamespace()) {
6527 auto Merged = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
6528 if (Merged != MergedDecls.end()) {
6529 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
6530 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
6534 auto LookUpInContexts = [&](ArrayRef<const DeclContext*> Contexts) {
6535 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
6537 // If we can definitively determine which module file to look into,
6538 // only look there. Otherwise, look in all module files.
6539 ModuleFile *Definitive;
6540 if (Contexts.size() == 1 &&
6541 (Definitive = getDefinitiveModuleFileFor(Contexts[0], *this))) {
6542 DeclContextNameLookupVisitor::visit(*Definitive, &Visitor);
6544 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
6548 LookUpInContexts(Contexts);
6550 // If this might be an implicit special member function, then also search
6551 // all merged definitions of the surrounding class. We need to search them
6552 // individually, because finding an entity in one of them doesn't imply that
6553 // we can't find a different entity in another one.
6554 if (isa<CXXRecordDecl>(DC)) {
6555 auto Kind = Name.getNameKind();
6556 if (Kind == DeclarationName::CXXConstructorName ||
6557 Kind == DeclarationName::CXXDestructorName ||
6558 (Kind == DeclarationName::CXXOperatorName &&
6559 Name.getCXXOverloadedOperator() == OO_Equal)) {
6560 auto Merged = MergedLookups.find(DC);
6561 if (Merged != MergedLookups.end())
6562 for (auto *MergedDC : Merged->second)
6563 LookUpInContexts(MergedDC);
6567 ++NumVisibleDeclContextsRead;
6568 SetExternalVisibleDeclsForName(DC, Name, Decls);
6569 return !Decls.empty();
6573 /// \brief ModuleFile visitor used to retrieve all visible names in a
6574 /// declaration context.
6575 class DeclContextAllNamesVisitor {
6577 SmallVectorImpl<const DeclContext *> &Contexts;
6582 DeclContextAllNamesVisitor(ASTReader &Reader,
6583 SmallVectorImpl<const DeclContext *> &Contexts,
6584 DeclsMap &Decls, bool VisitAll)
6585 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { }
6587 static bool visit(ModuleFile &M, void *UserData) {
6588 DeclContextAllNamesVisitor *This
6589 = static_cast<DeclContextAllNamesVisitor *>(UserData);
6591 // Check whether we have any visible declaration information for
6592 // this context in this module.
6593 ModuleFile::DeclContextInfosMap::iterator Info;
6594 bool FoundInfo = false;
6595 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
6596 Info = M.DeclContextInfos.find(This->Contexts[I]);
6597 if (Info != M.DeclContextInfos.end() &&
6598 Info->second.NameLookupTableData) {
6607 ASTDeclContextNameLookupTable *LookupTable =
6608 Info->second.NameLookupTableData;
6609 bool FoundAnything = false;
6610 for (ASTDeclContextNameLookupTable::data_iterator
6611 I = LookupTable->data_begin(), E = LookupTable->data_end();
6614 ASTDeclContextNameLookupTrait::data_type Data = *I;
6615 for (; Data.first != Data.second; ++Data.first) {
6616 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
6621 // Record this declaration.
6622 FoundAnything = true;
6623 This->Decls[ND->getDeclName()].push_back(ND);
6627 return FoundAnything && !This->VisitAll;
6632 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
6633 if (!DC->hasExternalVisibleStorage())
6637 // Compute the declaration contexts we need to look into. Multiple such
6638 // declaration contexts occur when two declaration contexts from disjoint
6639 // modules get merged, e.g., when two namespaces with the same name are
6640 // independently defined in separate modules.
6641 SmallVector<const DeclContext *, 2> Contexts;
6642 Contexts.push_back(DC);
6644 if (DC->isNamespace()) {
6645 MergedDeclsMap::iterator Merged
6646 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
6647 if (Merged != MergedDecls.end()) {
6648 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
6649 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
6653 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls,
6654 /*VisitAll=*/DC->isFileContext());
6655 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
6656 ++NumVisibleDeclContextsRead;
6658 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
6659 SetExternalVisibleDeclsForName(DC, I->first, I->second);
6661 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
6664 /// \brief Under non-PCH compilation the consumer receives the objc methods
6665 /// before receiving the implementation, and codegen depends on this.
6666 /// We simulate this by deserializing and passing to consumer the methods of the
6667 /// implementation before passing the deserialized implementation decl.
6668 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
6669 ASTConsumer *Consumer) {
6670 assert(ImplD && Consumer);
6672 for (auto *I : ImplD->methods())
6673 Consumer->HandleInterestingDecl(DeclGroupRef(I));
6675 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
6678 void ASTReader::PassInterestingDeclsToConsumer() {
6681 if (PassingDeclsToConsumer)
6684 // Guard variable to avoid recursively redoing the process of passing
6685 // decls to consumer.
6686 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6689 while (!InterestingDecls.empty()) {
6690 Decl *D = InterestingDecls.front();
6691 InterestingDecls.pop_front();
6693 PassInterestingDeclToConsumer(D);
6697 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
6698 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
6699 PassObjCImplDeclToConsumer(ImplD, Consumer);
6701 Consumer->HandleInterestingDecl(DeclGroupRef(D));
6704 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
6705 this->Consumer = Consumer;
6710 for (unsigned I = 0, N = EagerlyDeserializedDecls.size(); I != N; ++I) {
6711 // Force deserialization of this decl, which will cause it to be queued for
6712 // passing to the consumer.
6713 GetDecl(EagerlyDeserializedDecls[I]);
6715 EagerlyDeserializedDecls.clear();
6717 PassInterestingDeclsToConsumer();
6720 void ASTReader::PrintStats() {
6721 std::fprintf(stderr, "*** AST File Statistics:\n");
6723 unsigned NumTypesLoaded
6724 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
6726 unsigned NumDeclsLoaded
6727 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
6729 unsigned NumIdentifiersLoaded
6730 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
6731 IdentifiersLoaded.end(),
6732 (IdentifierInfo *)nullptr);
6733 unsigned NumMacrosLoaded
6734 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
6736 (MacroInfo *)nullptr);
6737 unsigned NumSelectorsLoaded
6738 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
6739 SelectorsLoaded.end(),
6742 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
6743 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
6744 NumSLocEntriesRead, TotalNumSLocEntries,
6745 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
6746 if (!TypesLoaded.empty())
6747 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
6748 NumTypesLoaded, (unsigned)TypesLoaded.size(),
6749 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
6750 if (!DeclsLoaded.empty())
6751 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
6752 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
6753 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
6754 if (!IdentifiersLoaded.empty())
6755 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
6756 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
6757 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
6758 if (!MacrosLoaded.empty())
6759 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6760 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
6761 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
6762 if (!SelectorsLoaded.empty())
6763 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
6764 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
6765 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
6766 if (TotalNumStatements)
6767 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
6768 NumStatementsRead, TotalNumStatements,
6769 ((float)NumStatementsRead/TotalNumStatements * 100));
6771 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6772 NumMacrosRead, TotalNumMacros,
6773 ((float)NumMacrosRead/TotalNumMacros * 100));
6774 if (TotalLexicalDeclContexts)
6775 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
6776 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
6777 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
6779 if (TotalVisibleDeclContexts)
6780 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
6781 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
6782 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
6784 if (TotalNumMethodPoolEntries) {
6785 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
6786 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
6787 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
6790 if (NumMethodPoolLookups) {
6791 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
6792 NumMethodPoolHits, NumMethodPoolLookups,
6793 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
6795 if (NumMethodPoolTableLookups) {
6796 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
6797 NumMethodPoolTableHits, NumMethodPoolTableLookups,
6798 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
6802 if (NumIdentifierLookupHits) {
6803 std::fprintf(stderr,
6804 " %u / %u identifier table lookups succeeded (%f%%)\n",
6805 NumIdentifierLookupHits, NumIdentifierLookups,
6806 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
6810 std::fprintf(stderr, "\n");
6811 GlobalIndex->printStats();
6814 std::fprintf(stderr, "\n");
6816 std::fprintf(stderr, "\n");
6819 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
6821 dumpModuleIDMap(StringRef Name,
6822 const ContinuousRangeMap<Key, ModuleFile *,
6823 InitialCapacity> &Map) {
6824 if (Map.begin() == Map.end())
6827 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
6828 llvm::errs() << Name << ":\n";
6829 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
6831 llvm::errs() << " " << I->first << " -> " << I->second->FileName
6836 void ASTReader::dump() {
6837 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
6838 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
6839 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
6840 dumpModuleIDMap("Global type map", GlobalTypeMap);
6841 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
6842 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
6843 dumpModuleIDMap("Global macro map", GlobalMacroMap);
6844 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
6845 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
6846 dumpModuleIDMap("Global preprocessed entity map",
6847 GlobalPreprocessedEntityMap);
6849 llvm::errs() << "\n*** PCH/Modules Loaded:";
6850 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
6851 MEnd = ModuleMgr.end();
6856 /// Return the amount of memory used by memory buffers, breaking down
6857 /// by heap-backed versus mmap'ed memory.
6858 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
6859 for (ModuleConstIterator I = ModuleMgr.begin(),
6860 E = ModuleMgr.end(); I != E; ++I) {
6861 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
6862 size_t bytes = buf->getBufferSize();
6863 switch (buf->getBufferKind()) {
6864 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
6865 sizes.malloc_bytes += bytes;
6867 case llvm::MemoryBuffer::MemoryBuffer_MMap:
6868 sizes.mmap_bytes += bytes;
6875 void ASTReader::InitializeSema(Sema &S) {
6877 S.addExternalSource(this);
6879 // Makes sure any declarations that were deserialized "too early"
6880 // still get added to the identifier's declaration chains.
6881 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
6882 pushExternalDeclIntoScope(PreloadedDecls[I],
6883 PreloadedDecls[I]->getDeclName());
6885 PreloadedDecls.clear();
6887 // FIXME: What happens if these are changed by a module import?
6888 if (!FPPragmaOptions.empty()) {
6889 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
6890 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
6893 // FIXME: What happens if these are changed by a module import?
6894 if (!OpenCLExtensions.empty()) {
6896 #define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
6897 #include "clang/Basic/OpenCLExtensions.def"
6899 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
6905 void ASTReader::UpdateSema() {
6906 assert(SemaObj && "no Sema to update");
6908 // Load the offsets of the declarations that Sema references.
6909 // They will be lazily deserialized when needed.
6910 if (!SemaDeclRefs.empty()) {
6911 assert(SemaDeclRefs.size() % 2 == 0);
6912 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 2) {
6913 if (!SemaObj->StdNamespace)
6914 SemaObj->StdNamespace = SemaDeclRefs[I];
6915 if (!SemaObj->StdBadAlloc)
6916 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
6918 SemaDeclRefs.clear();
6921 // Update the state of 'pragma clang optimize'. Use the same API as if we had
6922 // encountered the pragma in the source.
6923 if(OptimizeOffPragmaLocation.isValid())
6924 SemaObj->ActOnPragmaOptimize(/* IsOn = */ false, OptimizeOffPragmaLocation);
6927 IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
6928 // Note that we are loading an identifier.
6929 Deserializing AnIdentifier(this);
6930 StringRef Name(NameStart, NameEnd - NameStart);
6932 // If there is a global index, look there first to determine which modules
6933 // provably do not have any results for this identifier.
6934 GlobalModuleIndex::HitSet Hits;
6935 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
6936 if (!loadGlobalIndex()) {
6937 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
6941 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
6942 NumIdentifierLookups,
6943 NumIdentifierLookupHits);
6944 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
6945 IdentifierInfo *II = Visitor.getIdentifierInfo();
6946 markIdentifierUpToDate(II);
6951 /// \brief An identifier-lookup iterator that enumerates all of the
6952 /// identifiers stored within a set of AST files.
6953 class ASTIdentifierIterator : public IdentifierIterator {
6954 /// \brief The AST reader whose identifiers are being enumerated.
6955 const ASTReader &Reader;
6957 /// \brief The current index into the chain of AST files stored in
6961 /// \brief The current position within the identifier lookup table
6962 /// of the current AST file.
6963 ASTIdentifierLookupTable::key_iterator Current;
6965 /// \brief The end position within the identifier lookup table of
6966 /// the current AST file.
6967 ASTIdentifierLookupTable::key_iterator End;
6970 explicit ASTIdentifierIterator(const ASTReader &Reader);
6972 StringRef Next() override;
6976 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
6977 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
6978 ASTIdentifierLookupTable *IdTable
6979 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
6980 Current = IdTable->key_begin();
6981 End = IdTable->key_end();
6984 StringRef ASTIdentifierIterator::Next() {
6985 while (Current == End) {
6986 // If we have exhausted all of our AST files, we're done.
6991 ASTIdentifierLookupTable *IdTable
6992 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
6993 IdentifierLookupTable;
6994 Current = IdTable->key_begin();
6995 End = IdTable->key_end();
6998 // We have any identifiers remaining in the current AST file; return
7000 StringRef Result = *Current;
7005 IdentifierIterator *ASTReader::getIdentifiers() {
7006 if (!loadGlobalIndex())
7007 return GlobalIndex->createIdentifierIterator();
7009 return new ASTIdentifierIterator(*this);
7012 namespace clang { namespace serialization {
7013 class ReadMethodPoolVisitor {
7016 unsigned PriorGeneration;
7017 unsigned InstanceBits;
7018 unsigned FactoryBits;
7019 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
7020 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
7023 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
7024 unsigned PriorGeneration)
7025 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration),
7026 InstanceBits(0), FactoryBits(0) { }
7028 static bool visit(ModuleFile &M, void *UserData) {
7029 ReadMethodPoolVisitor *This
7030 = static_cast<ReadMethodPoolVisitor *>(UserData);
7032 if (!M.SelectorLookupTable)
7035 // If we've already searched this module file, skip it now.
7036 if (M.Generation <= This->PriorGeneration)
7039 ++This->Reader.NumMethodPoolTableLookups;
7040 ASTSelectorLookupTable *PoolTable
7041 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
7042 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
7043 if (Pos == PoolTable->end())
7046 ++This->Reader.NumMethodPoolTableHits;
7047 ++This->Reader.NumSelectorsRead;
7048 // FIXME: Not quite happy with the statistics here. We probably should
7049 // disable this tracking when called via LoadSelector.
7050 // Also, should entries without methods count as misses?
7051 ++This->Reader.NumMethodPoolEntriesRead;
7052 ASTSelectorLookupTrait::data_type Data = *Pos;
7053 if (This->Reader.DeserializationListener)
7054 This->Reader.DeserializationListener->SelectorRead(Data.ID,
7057 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
7058 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
7059 This->InstanceBits = Data.InstanceBits;
7060 This->FactoryBits = Data.FactoryBits;
7064 /// \brief Retrieve the instance methods found by this visitor.
7065 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
7066 return InstanceMethods;
7069 /// \brief Retrieve the instance methods found by this visitor.
7070 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
7071 return FactoryMethods;
7074 unsigned getInstanceBits() const { return InstanceBits; }
7075 unsigned getFactoryBits() const { return FactoryBits; }
7077 } } // end namespace clang::serialization
7079 /// \brief Add the given set of methods to the method list.
7080 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
7081 ObjCMethodList &List) {
7082 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
7083 S.addMethodToGlobalList(&List, Methods[I]);
7087 void ASTReader::ReadMethodPool(Selector Sel) {
7088 // Get the selector generation and update it to the current generation.
7089 unsigned &Generation = SelectorGeneration[Sel];
7090 unsigned PriorGeneration = Generation;
7091 Generation = getGeneration();
7093 // Search for methods defined with this selector.
7094 ++NumMethodPoolLookups;
7095 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
7096 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
7098 if (Visitor.getInstanceMethods().empty() &&
7099 Visitor.getFactoryMethods().empty())
7102 ++NumMethodPoolHits;
7107 Sema &S = *getSema();
7108 Sema::GlobalMethodPool::iterator Pos
7109 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
7111 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
7112 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
7113 Pos->second.first.setBits(Visitor.getInstanceBits());
7114 Pos->second.second.setBits(Visitor.getFactoryBits());
7117 void ASTReader::ReadKnownNamespaces(
7118 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
7121 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
7122 if (NamespaceDecl *Namespace
7123 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
7124 Namespaces.push_back(Namespace);
7128 void ASTReader::ReadUndefinedButUsed(
7129 llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) {
7130 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
7131 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
7132 SourceLocation Loc =
7133 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
7134 Undefined.insert(std::make_pair(D, Loc));
7138 void ASTReader::ReadTentativeDefinitions(
7139 SmallVectorImpl<VarDecl *> &TentativeDefs) {
7140 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
7141 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
7143 TentativeDefs.push_back(Var);
7145 TentativeDefinitions.clear();
7148 void ASTReader::ReadUnusedFileScopedDecls(
7149 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
7150 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
7152 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
7156 UnusedFileScopedDecls.clear();
7159 void ASTReader::ReadDelegatingConstructors(
7160 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
7161 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
7162 CXXConstructorDecl *D
7163 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
7167 DelegatingCtorDecls.clear();
7170 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
7171 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
7173 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
7177 ExtVectorDecls.clear();
7180 void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
7181 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
7183 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
7187 DynamicClasses.clear();
7191 ASTReader::ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl *> &Decls) {
7192 for (unsigned I = 0, N = LocallyScopedExternCDecls.size(); I != N; ++I) {
7194 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternCDecls[I]));
7198 LocallyScopedExternCDecls.clear();
7201 void ASTReader::ReadReferencedSelectors(
7202 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
7203 if (ReferencedSelectorsData.empty())
7206 // If there are @selector references added them to its pool. This is for
7207 // implementation of -Wselector.
7208 unsigned int DataSize = ReferencedSelectorsData.size()-1;
7210 while (I < DataSize) {
7211 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
7212 SourceLocation SelLoc
7213 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
7214 Sels.push_back(std::make_pair(Sel, SelLoc));
7216 ReferencedSelectorsData.clear();
7219 void ASTReader::ReadWeakUndeclaredIdentifiers(
7220 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
7221 if (WeakUndeclaredIdentifiers.empty())
7224 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
7225 IdentifierInfo *WeakId
7226 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7227 IdentifierInfo *AliasId
7228 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7230 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
7231 bool Used = WeakUndeclaredIdentifiers[I++];
7232 WeakInfo WI(AliasId, Loc);
7234 WeakIDs.push_back(std::make_pair(WeakId, WI));
7236 WeakUndeclaredIdentifiers.clear();
7239 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
7240 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
7241 ExternalVTableUse VT;
7242 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
7243 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
7244 VT.DefinitionRequired = VTableUses[Idx++];
7245 VTables.push_back(VT);
7251 void ASTReader::ReadPendingInstantiations(
7252 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
7253 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
7254 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
7256 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
7258 Pending.push_back(std::make_pair(D, Loc));
7260 PendingInstantiations.clear();
7263 void ASTReader::ReadLateParsedTemplates(
7264 llvm::DenseMap<const FunctionDecl *, LateParsedTemplate *> &LPTMap) {
7265 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
7267 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
7269 LateParsedTemplate *LT = new LateParsedTemplate;
7270 LT->D = GetDecl(LateParsedTemplates[Idx++]);
7272 ModuleFile *F = getOwningModuleFile(LT->D);
7273 assert(F && "No module");
7275 unsigned TokN = LateParsedTemplates[Idx++];
7276 LT->Toks.reserve(TokN);
7277 for (unsigned T = 0; T < TokN; ++T)
7278 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
7283 LateParsedTemplates.clear();
7286 void ASTReader::LoadSelector(Selector Sel) {
7287 // It would be complicated to avoid reading the methods anyway. So don't.
7288 ReadMethodPool(Sel);
7291 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
7292 assert(ID && "Non-zero identifier ID required");
7293 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
7294 IdentifiersLoaded[ID - 1] = II;
7295 if (DeserializationListener)
7296 DeserializationListener->IdentifierRead(ID, II);
7299 /// \brief Set the globally-visible declarations associated with the given
7302 /// If the AST reader is currently in a state where the given declaration IDs
7303 /// cannot safely be resolved, they are queued until it is safe to resolve
7306 /// \param II an IdentifierInfo that refers to one or more globally-visible
7309 /// \param DeclIDs the set of declaration IDs with the name @p II that are
7310 /// visible at global scope.
7312 /// \param Decls if non-null, this vector will be populated with the set of
7313 /// deserialized declarations. These declarations will not be pushed into
7316 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
7317 const SmallVectorImpl<uint32_t> &DeclIDs,
7318 SmallVectorImpl<Decl *> *Decls) {
7319 if (NumCurrentElementsDeserializing && !Decls) {
7320 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
7324 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
7325 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
7327 // If we're simply supposed to record the declarations, do so now.
7329 Decls->push_back(D);
7333 // Introduce this declaration into the translation-unit scope
7334 // and add it to the declaration chain for this identifier, so
7335 // that (unqualified) name lookup will find it.
7336 pushExternalDeclIntoScope(D, II);
7338 // Queue this declaration so that it will be added to the
7339 // translation unit scope and identifier's declaration chain
7340 // once a Sema object is known.
7341 PreloadedDecls.push_back(D);
7346 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
7350 if (IdentifiersLoaded.empty()) {
7351 Error("no identifier table in AST file");
7356 if (!IdentifiersLoaded[ID]) {
7357 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
7358 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
7359 ModuleFile *M = I->second;
7360 unsigned Index = ID - M->BaseIdentifierID;
7361 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
7363 // All of the strings in the AST file are preceded by a 16-bit length.
7364 // Extract that 16-bit length to avoid having to execute strlen().
7365 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
7366 // unsigned integers. This is important to avoid integer overflow when
7367 // we cast them to 'unsigned'.
7368 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
7369 unsigned StrLen = (((unsigned) StrLenPtr[0])
7370 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
7371 IdentifiersLoaded[ID]
7372 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
7373 if (DeserializationListener)
7374 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
7377 return IdentifiersLoaded[ID];
7380 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
7381 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
7384 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
7385 if (LocalID < NUM_PREDEF_IDENT_IDS)
7388 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7389 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
7390 assert(I != M.IdentifierRemap.end()
7391 && "Invalid index into identifier index remap");
7393 return LocalID + I->second;
7396 MacroInfo *ASTReader::getMacro(MacroID ID) {
7400 if (MacrosLoaded.empty()) {
7401 Error("no macro table in AST file");
7405 ID -= NUM_PREDEF_MACRO_IDS;
7406 if (!MacrosLoaded[ID]) {
7407 GlobalMacroMapType::iterator I
7408 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
7409 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
7410 ModuleFile *M = I->second;
7411 unsigned Index = ID - M->BaseMacroID;
7412 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
7414 if (DeserializationListener)
7415 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
7419 return MacrosLoaded[ID];
7422 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
7423 if (LocalID < NUM_PREDEF_MACRO_IDS)
7426 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7427 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
7428 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
7430 return LocalID + I->second;
7433 serialization::SubmoduleID
7434 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
7435 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
7438 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7439 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
7440 assert(I != M.SubmoduleRemap.end()
7441 && "Invalid index into submodule index remap");
7443 return LocalID + I->second;
7446 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
7447 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
7448 assert(GlobalID == 0 && "Unhandled global submodule ID");
7452 if (GlobalID > SubmodulesLoaded.size()) {
7453 Error("submodule ID out of range in AST file");
7457 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
7460 Module *ASTReader::getModule(unsigned ID) {
7461 return getSubmodule(ID);
7464 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
7465 return DecodeSelector(getGlobalSelectorID(M, LocalID));
7468 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
7472 if (ID > SelectorsLoaded.size()) {
7473 Error("selector ID out of range in AST file");
7477 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
7478 // Load this selector from the selector table.
7479 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
7480 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
7481 ModuleFile &M = *I->second;
7482 ASTSelectorLookupTrait Trait(*this, M);
7483 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
7484 SelectorsLoaded[ID - 1] =
7485 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
7486 if (DeserializationListener)
7487 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
7490 return SelectorsLoaded[ID - 1];
7493 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
7494 return DecodeSelector(ID);
7497 uint32_t ASTReader::GetNumExternalSelectors() {
7498 // ID 0 (the null selector) is considered an external selector.
7499 return getTotalNumSelectors() + 1;
7502 serialization::SelectorID
7503 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
7504 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
7507 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7508 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
7509 assert(I != M.SelectorRemap.end()
7510 && "Invalid index into selector index remap");
7512 return LocalID + I->second;
7516 ASTReader::ReadDeclarationName(ModuleFile &F,
7517 const RecordData &Record, unsigned &Idx) {
7518 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
7520 case DeclarationName::Identifier:
7521 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
7523 case DeclarationName::ObjCZeroArgSelector:
7524 case DeclarationName::ObjCOneArgSelector:
7525 case DeclarationName::ObjCMultiArgSelector:
7526 return DeclarationName(ReadSelector(F, Record, Idx));
7528 case DeclarationName::CXXConstructorName:
7529 return Context.DeclarationNames.getCXXConstructorName(
7530 Context.getCanonicalType(readType(F, Record, Idx)));
7532 case DeclarationName::CXXDestructorName:
7533 return Context.DeclarationNames.getCXXDestructorName(
7534 Context.getCanonicalType(readType(F, Record, Idx)));
7536 case DeclarationName::CXXConversionFunctionName:
7537 return Context.DeclarationNames.getCXXConversionFunctionName(
7538 Context.getCanonicalType(readType(F, Record, Idx)));
7540 case DeclarationName::CXXOperatorName:
7541 return Context.DeclarationNames.getCXXOperatorName(
7542 (OverloadedOperatorKind)Record[Idx++]);
7544 case DeclarationName::CXXLiteralOperatorName:
7545 return Context.DeclarationNames.getCXXLiteralOperatorName(
7546 GetIdentifierInfo(F, Record, Idx));
7548 case DeclarationName::CXXUsingDirective:
7549 return DeclarationName::getUsingDirectiveName();
7552 llvm_unreachable("Invalid NameKind!");
7555 void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
7556 DeclarationNameLoc &DNLoc,
7557 DeclarationName Name,
7558 const RecordData &Record, unsigned &Idx) {
7559 switch (Name.getNameKind()) {
7560 case DeclarationName::CXXConstructorName:
7561 case DeclarationName::CXXDestructorName:
7562 case DeclarationName::CXXConversionFunctionName:
7563 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
7566 case DeclarationName::CXXOperatorName:
7567 DNLoc.CXXOperatorName.BeginOpNameLoc
7568 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7569 DNLoc.CXXOperatorName.EndOpNameLoc
7570 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7573 case DeclarationName::CXXLiteralOperatorName:
7574 DNLoc.CXXLiteralOperatorName.OpNameLoc
7575 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7578 case DeclarationName::Identifier:
7579 case DeclarationName::ObjCZeroArgSelector:
7580 case DeclarationName::ObjCOneArgSelector:
7581 case DeclarationName::ObjCMultiArgSelector:
7582 case DeclarationName::CXXUsingDirective:
7587 void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
7588 DeclarationNameInfo &NameInfo,
7589 const RecordData &Record, unsigned &Idx) {
7590 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
7591 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
7592 DeclarationNameLoc DNLoc;
7593 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
7594 NameInfo.setInfo(DNLoc);
7597 void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
7598 const RecordData &Record, unsigned &Idx) {
7599 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
7600 unsigned NumTPLists = Record[Idx++];
7601 Info.NumTemplParamLists = NumTPLists;
7603 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
7604 for (unsigned i=0; i != NumTPLists; ++i)
7605 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
7610 ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
7612 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
7614 case TemplateName::Template:
7615 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
7617 case TemplateName::OverloadedTemplate: {
7618 unsigned size = Record[Idx++];
7619 UnresolvedSet<8> Decls;
7621 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
7623 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
7626 case TemplateName::QualifiedTemplate: {
7627 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7628 bool hasTemplKeyword = Record[Idx++];
7629 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
7630 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
7633 case TemplateName::DependentTemplate: {
7634 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7635 if (Record[Idx++]) // isIdentifier
7636 return Context.getDependentTemplateName(NNS,
7637 GetIdentifierInfo(F, Record,
7639 return Context.getDependentTemplateName(NNS,
7640 (OverloadedOperatorKind)Record[Idx++]);
7643 case TemplateName::SubstTemplateTemplateParm: {
7644 TemplateTemplateParmDecl *param
7645 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7646 if (!param) return TemplateName();
7647 TemplateName replacement = ReadTemplateName(F, Record, Idx);
7648 return Context.getSubstTemplateTemplateParm(param, replacement);
7651 case TemplateName::SubstTemplateTemplateParmPack: {
7652 TemplateTemplateParmDecl *Param
7653 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7655 return TemplateName();
7657 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
7658 if (ArgPack.getKind() != TemplateArgument::Pack)
7659 return TemplateName();
7661 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
7665 llvm_unreachable("Unhandled template name kind!");
7669 ASTReader::ReadTemplateArgument(ModuleFile &F,
7670 const RecordData &Record, unsigned &Idx) {
7671 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
7673 case TemplateArgument::Null:
7674 return TemplateArgument();
7675 case TemplateArgument::Type:
7676 return TemplateArgument(readType(F, Record, Idx));
7677 case TemplateArgument::Declaration: {
7678 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
7679 bool ForReferenceParam = Record[Idx++];
7680 return TemplateArgument(D, ForReferenceParam);
7682 case TemplateArgument::NullPtr:
7683 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
7684 case TemplateArgument::Integral: {
7685 llvm::APSInt Value = ReadAPSInt(Record, Idx);
7686 QualType T = readType(F, Record, Idx);
7687 return TemplateArgument(Context, Value, T);
7689 case TemplateArgument::Template:
7690 return TemplateArgument(ReadTemplateName(F, Record, Idx));
7691 case TemplateArgument::TemplateExpansion: {
7692 TemplateName Name = ReadTemplateName(F, Record, Idx);
7693 Optional<unsigned> NumTemplateExpansions;
7694 if (unsigned NumExpansions = Record[Idx++])
7695 NumTemplateExpansions = NumExpansions - 1;
7696 return TemplateArgument(Name, NumTemplateExpansions);
7698 case TemplateArgument::Expression:
7699 return TemplateArgument(ReadExpr(F));
7700 case TemplateArgument::Pack: {
7701 unsigned NumArgs = Record[Idx++];
7702 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
7703 for (unsigned I = 0; I != NumArgs; ++I)
7704 Args[I] = ReadTemplateArgument(F, Record, Idx);
7705 return TemplateArgument(Args, NumArgs);
7709 llvm_unreachable("Unhandled template argument kind!");
7712 TemplateParameterList *
7713 ASTReader::ReadTemplateParameterList(ModuleFile &F,
7714 const RecordData &Record, unsigned &Idx) {
7715 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
7716 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
7717 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
7719 unsigned NumParams = Record[Idx++];
7720 SmallVector<NamedDecl *, 16> Params;
7721 Params.reserve(NumParams);
7723 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
7725 TemplateParameterList* TemplateParams =
7726 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
7727 Params.data(), Params.size(), RAngleLoc);
7728 return TemplateParams;
7733 ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
7734 ModuleFile &F, const RecordData &Record,
7736 unsigned NumTemplateArgs = Record[Idx++];
7737 TemplArgs.reserve(NumTemplateArgs);
7738 while (NumTemplateArgs--)
7739 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
7742 /// \brief Read a UnresolvedSet structure.
7743 void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
7744 const RecordData &Record, unsigned &Idx) {
7745 unsigned NumDecls = Record[Idx++];
7746 Set.reserve(Context, NumDecls);
7747 while (NumDecls--) {
7748 DeclID ID = ReadDeclID(F, Record, Idx);
7749 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
7750 Set.addLazyDecl(Context, ID, AS);
7755 ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
7756 const RecordData &Record, unsigned &Idx) {
7757 bool isVirtual = static_cast<bool>(Record[Idx++]);
7758 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
7759 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
7760 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
7761 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
7762 SourceRange Range = ReadSourceRange(F, Record, Idx);
7763 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
7764 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
7766 Result.setInheritConstructors(inheritConstructors);
7770 std::pair<CXXCtorInitializer **, unsigned>
7771 ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
7773 CXXCtorInitializer **CtorInitializers = nullptr;
7774 unsigned NumInitializers = Record[Idx++];
7775 if (NumInitializers) {
7777 = new (Context) CXXCtorInitializer*[NumInitializers];
7778 for (unsigned i=0; i != NumInitializers; ++i) {
7779 TypeSourceInfo *TInfo = nullptr;
7780 bool IsBaseVirtual = false;
7781 FieldDecl *Member = nullptr;
7782 IndirectFieldDecl *IndirectMember = nullptr;
7784 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
7786 case CTOR_INITIALIZER_BASE:
7787 TInfo = GetTypeSourceInfo(F, Record, Idx);
7788 IsBaseVirtual = Record[Idx++];
7791 case CTOR_INITIALIZER_DELEGATING:
7792 TInfo = GetTypeSourceInfo(F, Record, Idx);
7795 case CTOR_INITIALIZER_MEMBER:
7796 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
7799 case CTOR_INITIALIZER_INDIRECT_MEMBER:
7800 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
7804 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
7805 Expr *Init = ReadExpr(F);
7806 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
7807 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
7808 bool IsWritten = Record[Idx++];
7809 unsigned SourceOrderOrNumArrayIndices;
7810 SmallVector<VarDecl *, 8> Indices;
7812 SourceOrderOrNumArrayIndices = Record[Idx++];
7814 SourceOrderOrNumArrayIndices = Record[Idx++];
7815 Indices.reserve(SourceOrderOrNumArrayIndices);
7816 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
7817 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
7820 CXXCtorInitializer *BOMInit;
7821 if (Type == CTOR_INITIALIZER_BASE) {
7822 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
7823 LParenLoc, Init, RParenLoc,
7824 MemberOrEllipsisLoc);
7825 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
7826 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
7828 } else if (IsWritten) {
7830 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
7831 LParenLoc, Init, RParenLoc);
7833 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
7834 MemberOrEllipsisLoc, LParenLoc,
7837 if (IndirectMember) {
7838 assert(Indices.empty() && "Indirect field improperly initialized");
7839 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
7840 MemberOrEllipsisLoc, LParenLoc,
7843 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
7844 LParenLoc, Init, RParenLoc,
7845 Indices.data(), Indices.size());
7850 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
7851 CtorInitializers[i] = BOMInit;
7855 return std::make_pair(CtorInitializers, NumInitializers);
7858 NestedNameSpecifier *
7859 ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
7860 const RecordData &Record, unsigned &Idx) {
7861 unsigned N = Record[Idx++];
7862 NestedNameSpecifier *NNS = nullptr, *Prev = nullptr;
7863 for (unsigned I = 0; I != N; ++I) {
7864 NestedNameSpecifier::SpecifierKind Kind
7865 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7867 case NestedNameSpecifier::Identifier: {
7868 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7869 NNS = NestedNameSpecifier::Create(Context, Prev, II);
7873 case NestedNameSpecifier::Namespace: {
7874 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7875 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
7879 case NestedNameSpecifier::NamespaceAlias: {
7880 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7881 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
7885 case NestedNameSpecifier::TypeSpec:
7886 case NestedNameSpecifier::TypeSpecWithTemplate: {
7887 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
7891 bool Template = Record[Idx++];
7892 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
7896 case NestedNameSpecifier::Global: {
7897 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
7898 // No associated value, and there can't be a prefix.
7907 NestedNameSpecifierLoc
7908 ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
7910 unsigned N = Record[Idx++];
7911 NestedNameSpecifierLocBuilder Builder;
7912 for (unsigned I = 0; I != N; ++I) {
7913 NestedNameSpecifier::SpecifierKind Kind
7914 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7916 case NestedNameSpecifier::Identifier: {
7917 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7918 SourceRange Range = ReadSourceRange(F, Record, Idx);
7919 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
7923 case NestedNameSpecifier::Namespace: {
7924 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7925 SourceRange Range = ReadSourceRange(F, Record, Idx);
7926 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
7930 case NestedNameSpecifier::NamespaceAlias: {
7931 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7932 SourceRange Range = ReadSourceRange(F, Record, Idx);
7933 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
7937 case NestedNameSpecifier::TypeSpec:
7938 case NestedNameSpecifier::TypeSpecWithTemplate: {
7939 bool Template = Record[Idx++];
7940 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
7942 return NestedNameSpecifierLoc();
7943 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7945 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
7946 Builder.Extend(Context,
7947 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
7948 T->getTypeLoc(), ColonColonLoc);
7952 case NestedNameSpecifier::Global: {
7953 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7954 Builder.MakeGlobal(Context, ColonColonLoc);
7960 return Builder.getWithLocInContext(Context);
7964 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
7966 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
7967 SourceLocation end = ReadSourceLocation(F, Record, Idx);
7968 return SourceRange(beg, end);
7971 /// \brief Read an integral value
7972 llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
7973 unsigned BitWidth = Record[Idx++];
7974 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
7975 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
7980 /// \brief Read a signed integral value
7981 llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
7982 bool isUnsigned = Record[Idx++];
7983 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
7986 /// \brief Read a floating-point value
7987 llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
7988 const llvm::fltSemantics &Sem,
7990 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
7993 // \brief Read a string
7994 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
7995 unsigned Len = Record[Idx++];
7996 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
8001 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
8003 unsigned Major = Record[Idx++];
8004 unsigned Minor = Record[Idx++];
8005 unsigned Subminor = Record[Idx++];
8007 return VersionTuple(Major);
8009 return VersionTuple(Major, Minor - 1);
8010 return VersionTuple(Major, Minor - 1, Subminor - 1);
8013 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
8014 const RecordData &Record,
8016 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
8017 return CXXTemporary::Create(Context, Decl);
8020 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
8021 return Diag(CurrentImportLoc, DiagID);
8024 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
8025 return Diags.Report(Loc, DiagID);
8028 /// \brief Retrieve the identifier table associated with the
8030 IdentifierTable &ASTReader::getIdentifierTable() {
8031 return PP.getIdentifierTable();
8034 /// \brief Record that the given ID maps to the given switch-case
8036 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
8037 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
8038 "Already have a SwitchCase with this ID");
8039 (*CurrSwitchCaseStmts)[ID] = SC;
8042 /// \brief Retrieve the switch-case statement with the given ID.
8043 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
8044 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
8045 return (*CurrSwitchCaseStmts)[ID];
8048 void ASTReader::ClearSwitchCaseIDs() {
8049 CurrSwitchCaseStmts->clear();
8052 void ASTReader::ReadComments() {
8053 std::vector<RawComment *> Comments;
8054 for (SmallVectorImpl<std::pair<BitstreamCursor,
8055 serialization::ModuleFile *> >::iterator
8056 I = CommentsCursors.begin(),
8057 E = CommentsCursors.end();
8060 BitstreamCursor &Cursor = I->first;
8061 serialization::ModuleFile &F = *I->second;
8062 SavedStreamPosition SavedPosition(Cursor);
8066 llvm::BitstreamEntry Entry =
8067 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
8069 switch (Entry.Kind) {
8070 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
8071 case llvm::BitstreamEntry::Error:
8072 Error("malformed block record in AST file");
8074 case llvm::BitstreamEntry::EndBlock:
8076 case llvm::BitstreamEntry::Record:
8077 // The interesting case.
8083 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
8084 case COMMENTS_RAW_COMMENT: {
8086 SourceRange SR = ReadSourceRange(F, Record, Idx);
8087 RawComment::CommentKind Kind =
8088 (RawComment::CommentKind) Record[Idx++];
8089 bool IsTrailingComment = Record[Idx++];
8090 bool IsAlmostTrailingComment = Record[Idx++];
8091 Comments.push_back(new (Context) RawComment(
8092 SR, Kind, IsTrailingComment, IsAlmostTrailingComment,
8093 Context.getLangOpts().CommentOpts.ParseAllComments));
8099 Context.Comments.addDeserializedComments(Comments);
8103 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
8104 // If we know the owning module, use it.
8105 if (Module *M = D->getOwningModule())
8106 return M->getFullModuleName();
8108 // Otherwise, use the name of the top-level module the decl is within.
8109 if (ModuleFile *M = getOwningModuleFile(D))
8110 return M->ModuleName;
8112 // Not from a module.
8116 void ASTReader::finishPendingActions() {
8117 while (!PendingIdentifierInfos.empty() ||
8118 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
8119 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
8120 !PendingUpdateRecords.empty()) {
8121 // If any identifiers with corresponding top-level declarations have
8122 // been loaded, load those declarations now.
8123 typedef llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >
8125 TopLevelDeclsMap TopLevelDecls;
8127 while (!PendingIdentifierInfos.empty()) {
8128 IdentifierInfo *II = PendingIdentifierInfos.back().first;
8129 SmallVector<uint32_t, 4> DeclIDs =
8130 std::move(PendingIdentifierInfos.back().second);
8131 PendingIdentifierInfos.pop_back();
8133 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
8136 // For each decl chain that we wanted to complete while deserializing, mark
8137 // it as "still needs to be completed".
8138 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
8139 markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
8141 PendingIncompleteDeclChains.clear();
8143 // Load pending declaration chains.
8144 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
8145 loadPendingDeclChain(PendingDeclChains[I]);
8146 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
8148 PendingDeclChains.clear();
8150 // Make the most recent of the top-level declarations visible.
8151 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
8152 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
8153 IdentifierInfo *II = TLD->first;
8154 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
8155 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
8159 // Load any pending macro definitions.
8160 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
8161 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
8162 SmallVector<PendingMacroInfo, 2> GlobalIDs;
8163 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
8164 // Initialize the macro history from chained-PCHs ahead of module imports.
8165 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
8167 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
8168 if (Info.M->Kind != MK_Module)
8169 resolvePendingMacro(II, Info);
8171 // Handle module imports.
8172 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
8174 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
8175 if (Info.M->Kind == MK_Module)
8176 resolvePendingMacro(II, Info);
8179 PendingMacroIDs.clear();
8181 // Wire up the DeclContexts for Decls that we delayed setting until
8182 // recursive loading is completed.
8183 while (!PendingDeclContextInfos.empty()) {
8184 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
8185 PendingDeclContextInfos.pop_front();
8186 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
8187 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
8188 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
8191 // Perform any pending declaration updates.
8192 while (!PendingUpdateRecords.empty()) {
8193 auto Update = PendingUpdateRecords.pop_back_val();
8194 ReadingKindTracker ReadingKind(Read_Decl, *this);
8195 loadDeclUpdateRecords(Update.first, Update.second);
8199 // If we deserialized any C++ or Objective-C class definitions, any
8200 // Objective-C protocol definitions, or any redeclarable templates, make sure
8201 // that all redeclarations point to the definitions. Note that this can only
8202 // happen now, after the redeclaration chains have been fully wired.
8203 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
8204 DEnd = PendingDefinitions.end();
8206 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
8207 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
8208 // Make sure that the TagType points at the definition.
8209 const_cast<TagType*>(TagT)->decl = TD;
8212 if (auto RD = dyn_cast<CXXRecordDecl>(*D)) {
8213 for (auto R : RD->redecls())
8214 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
8220 if (auto ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
8221 // Make sure that the ObjCInterfaceType points at the definition.
8222 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
8225 for (auto R : ID->redecls())
8231 if (auto PD = dyn_cast<ObjCProtocolDecl>(*D)) {
8232 for (auto R : PD->redecls())
8238 auto RTD = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
8239 for (auto R : RTD->redecls())
8240 R->Common = RTD->Common;
8242 PendingDefinitions.clear();
8244 // Load the bodies of any functions or methods we've encountered. We do
8245 // this now (delayed) so that we can be sure that the declaration chains
8246 // have been fully wired up.
8247 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
8248 PBEnd = PendingBodies.end();
8249 PB != PBEnd; ++PB) {
8250 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
8251 // FIXME: Check for =delete/=default?
8252 // FIXME: Complain about ODR violations here?
8253 if (!getContext().getLangOpts().Modules || !FD->hasBody())
8254 FD->setLazyBody(PB->second);
8258 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
8259 if (!getContext().getLangOpts().Modules || !MD->hasBody())
8260 MD->setLazyBody(PB->second);
8262 PendingBodies.clear();
8265 void ASTReader::diagnoseOdrViolations() {
8266 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty())
8269 // Trigger the import of the full definition of each class that had any
8270 // odr-merging problems, so we can produce better diagnostics for them.
8271 // These updates may in turn find and diagnose some ODR failures, so take
8272 // ownership of the set first.
8273 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
8274 PendingOdrMergeFailures.clear();
8275 for (auto &Merge : OdrMergeFailures) {
8276 Merge.first->buildLookup();
8277 Merge.first->decls_begin();
8278 Merge.first->bases_begin();
8279 Merge.first->vbases_begin();
8280 for (auto *RD : Merge.second) {
8287 // For each declaration from a merged context, check that the canonical
8288 // definition of that context also contains a declaration of the same
8291 // Caution: this loop does things that might invalidate iterators into
8292 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
8293 while (!PendingOdrMergeChecks.empty()) {
8294 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
8296 // FIXME: Skip over implicit declarations for now. This matters for things
8297 // like implicitly-declared special member functions. This isn't entirely
8298 // correct; we can end up with multiple unmerged declarations of the same
8300 if (D->isImplicit())
8303 DeclContext *CanonDef = D->getDeclContext();
8304 DeclContext::lookup_result R = CanonDef->lookup(D->getDeclName());
8307 const Decl *DCanon = D->getCanonicalDecl();
8309 llvm::SmallVector<const NamedDecl*, 4> Candidates;
8310 for (DeclContext::lookup_iterator I = R.begin(), E = R.end();
8311 !Found && I != E; ++I) {
8312 for (auto RI : (*I)->redecls()) {
8313 if (RI->getLexicalDeclContext() == CanonDef) {
8314 // This declaration is present in the canonical definition. If it's
8315 // in the same redecl chain, it's the one we're looking for.
8316 if (RI->getCanonicalDecl() == DCanon)
8319 Candidates.push_back(cast<NamedDecl>(RI));
8326 D->setInvalidDecl();
8328 std::string CanonDefModule =
8329 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
8330 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
8331 << D << getOwningModuleNameForDiagnostic(D)
8332 << CanonDef << CanonDefModule.empty() << CanonDefModule;
8334 if (Candidates.empty())
8335 Diag(cast<Decl>(CanonDef)->getLocation(),
8336 diag::note_module_odr_violation_no_possible_decls) << D;
8338 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
8339 Diag(Candidates[I]->getLocation(),
8340 diag::note_module_odr_violation_possible_decl)
8344 DiagnosedOdrMergeFailures.insert(CanonDef);
8348 // Issue any pending ODR-failure diagnostics.
8349 for (auto &Merge : OdrMergeFailures) {
8350 // If we've already pointed out a specific problem with this class, don't
8351 // bother issuing a general "something's different" diagnostic.
8352 if (!DiagnosedOdrMergeFailures.insert(Merge.first))
8355 bool Diagnosed = false;
8356 for (auto *RD : Merge.second) {
8357 // Multiple different declarations got merged together; tell the user
8358 // where they came from.
8359 if (Merge.first != RD) {
8360 // FIXME: Walk the definition, figure out what's different,
8361 // and diagnose that.
8363 std::string Module = getOwningModuleNameForDiagnostic(Merge.first);
8364 Diag(Merge.first->getLocation(),
8365 diag::err_module_odr_violation_different_definitions)
8366 << Merge.first << Module.empty() << Module;
8370 Diag(RD->getLocation(),
8371 diag::note_module_odr_violation_different_definitions)
8372 << getOwningModuleNameForDiagnostic(RD);
8377 // All definitions are updates to the same declaration. This happens if a
8378 // module instantiates the declaration of a class template specialization
8379 // and two or more other modules instantiate its definition.
8381 // FIXME: Indicate which modules had instantiations of this definition.
8382 // FIXME: How can this even happen?
8383 Diag(Merge.first->getLocation(),
8384 diag::err_module_odr_violation_different_instantiations)
8390 void ASTReader::FinishedDeserializing() {
8391 assert(NumCurrentElementsDeserializing &&
8392 "FinishedDeserializing not paired with StartedDeserializing");
8393 if (NumCurrentElementsDeserializing == 1) {
8394 // We decrease NumCurrentElementsDeserializing only after pending actions
8395 // are finished, to avoid recursively re-calling finishPendingActions().
8396 finishPendingActions();
8398 --NumCurrentElementsDeserializing;
8400 if (NumCurrentElementsDeserializing == 0) {
8401 diagnoseOdrViolations();
8403 // We are not in recursive loading, so it's safe to pass the "interesting"
8404 // decls to the consumer.
8406 PassInterestingDeclsToConsumer();
8410 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
8411 D = D->getMostRecentDecl();
8413 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
8414 SemaObj->TUScope->AddDecl(D);
8415 } else if (SemaObj->TUScope) {
8416 // Adding the decl to IdResolver may have failed because it was already in
8417 // (even though it was not added in scope). If it is already in, make sure
8418 // it gets in the scope as well.
8419 if (std::find(SemaObj->IdResolver.begin(Name),
8420 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
8421 SemaObj->TUScope->AddDecl(D);
8425 ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context, StringRef isysroot,
8426 bool DisableValidation, bool AllowASTWithCompilerErrors,
8427 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
8428 bool UseGlobalIndex)
8429 : Listener(new PCHValidator(PP, *this)), DeserializationListener(nullptr),
8430 OwnsDeserializationListener(false), SourceMgr(PP.getSourceManager()),
8431 FileMgr(PP.getFileManager()), Diags(PP.getDiagnostics()),
8432 SemaObj(nullptr), PP(PP), Context(Context), Consumer(nullptr),
8433 ModuleMgr(PP.getFileManager()), isysroot(isysroot),
8434 DisableValidation(DisableValidation),
8435 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
8436 AllowConfigurationMismatch(AllowConfigurationMismatch),
8437 ValidateSystemInputs(ValidateSystemInputs),
8438 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
8439 CurrSwitchCaseStmts(&SwitchCaseStmts),
8440 NumSLocEntriesRead(0), TotalNumSLocEntries(0), NumStatementsRead(0),
8441 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
8442 NumIdentifierLookups(0), NumIdentifierLookupHits(0), NumSelectorsRead(0),
8443 NumMethodPoolEntriesRead(0), NumMethodPoolLookups(0),
8444 NumMethodPoolHits(0), NumMethodPoolTableLookups(0),
8445 NumMethodPoolTableHits(0), TotalNumMethodPoolEntries(0),
8446 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
8447 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
8448 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
8449 PassingDeclsToConsumer(false), NumCXXBaseSpecifiersLoaded(0),
8450 ReadingKind(Read_None) {
8451 SourceMgr.setExternalSLocEntrySource(this);
8454 ASTReader::~ASTReader() {
8455 if (OwnsDeserializationListener)
8456 delete DeserializationListener;
8458 for (DeclContextVisibleUpdatesPending::iterator
8459 I = PendingVisibleUpdates.begin(),
8460 E = PendingVisibleUpdates.end();
8462 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
8463 F = I->second.end();