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/FileManager.h"
26 #include "clang/Basic/SourceManager.h"
27 #include "clang/Basic/SourceManagerInternals.h"
28 #include "clang/Basic/TargetInfo.h"
29 #include "clang/Basic/TargetOptions.h"
30 #include "clang/Basic/Version.h"
31 #include "clang/Basic/VersionTuple.h"
32 #include "clang/Lex/HeaderSearch.h"
33 #include "clang/Lex/HeaderSearchOptions.h"
34 #include "clang/Lex/MacroInfo.h"
35 #include "clang/Lex/PreprocessingRecord.h"
36 #include "clang/Lex/Preprocessor.h"
37 #include "clang/Lex/PreprocessorOptions.h"
38 #include "clang/Sema/Scope.h"
39 #include "clang/Sema/Sema.h"
40 #include "clang/Serialization/ASTDeserializationListener.h"
41 #include "clang/Serialization/GlobalModuleIndex.h"
42 #include "clang/Serialization/ModuleManager.h"
43 #include "clang/Serialization/SerializationDiagnostic.h"
44 #include "llvm/ADT/Hashing.h"
45 #include "llvm/ADT/StringExtras.h"
46 #include "llvm/Bitcode/BitstreamReader.h"
47 #include "llvm/Support/ErrorHandling.h"
48 #include "llvm/Support/FileSystem.h"
49 #include "llvm/Support/MemoryBuffer.h"
50 #include "llvm/Support/Path.h"
51 #include "llvm/Support/SaveAndRestore.h"
52 #include "llvm/Support/system_error.h"
57 using namespace clang;
58 using namespace clang::serialization;
59 using namespace clang::serialization::reader;
60 using llvm::BitstreamCursor;
62 //===----------------------------------------------------------------------===//
63 // PCH validator implementation
64 //===----------------------------------------------------------------------===//
66 ASTReaderListener::~ASTReaderListener() {}
68 /// \brief Compare the given set of language options against an existing set of
71 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
73 /// \returns true if the languagae options mis-match, false otherwise.
74 static bool checkLanguageOptions(const LangOptions &LangOpts,
75 const LangOptions &ExistingLangOpts,
76 DiagnosticsEngine *Diags) {
77 #define LANGOPT(Name, Bits, Default, Description) \
78 if (ExistingLangOpts.Name != LangOpts.Name) { \
80 Diags->Report(diag::err_pch_langopt_mismatch) \
81 << Description << LangOpts.Name << ExistingLangOpts.Name; \
85 #define VALUE_LANGOPT(Name, Bits, Default, Description) \
86 if (ExistingLangOpts.Name != LangOpts.Name) { \
88 Diags->Report(diag::err_pch_langopt_value_mismatch) \
93 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
94 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
96 Diags->Report(diag::err_pch_langopt_value_mismatch) \
101 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
102 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
103 #include "clang/Basic/LangOptions.def"
105 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
107 Diags->Report(diag::err_pch_langopt_value_mismatch)
108 << "target Objective-C runtime";
112 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
113 LangOpts.CommentOpts.BlockCommandNames) {
115 Diags->Report(diag::err_pch_langopt_value_mismatch)
116 << "block command names";
123 /// \brief Compare the given set of target options against an existing set of
126 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
128 /// \returns true if the target options mis-match, false otherwise.
129 static bool checkTargetOptions(const TargetOptions &TargetOpts,
130 const TargetOptions &ExistingTargetOpts,
131 DiagnosticsEngine *Diags) {
132 #define CHECK_TARGET_OPT(Field, Name) \
133 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
135 Diags->Report(diag::err_pch_targetopt_mismatch) \
136 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
140 CHECK_TARGET_OPT(Triple, "target");
141 CHECK_TARGET_OPT(CPU, "target CPU");
142 CHECK_TARGET_OPT(ABI, "target ABI");
143 CHECK_TARGET_OPT(CXXABI, "target C++ ABI");
144 CHECK_TARGET_OPT(LinkerVersion, "target linker version");
145 #undef CHECK_TARGET_OPT
147 // Compare feature sets.
148 SmallVector<StringRef, 4> ExistingFeatures(
149 ExistingTargetOpts.FeaturesAsWritten.begin(),
150 ExistingTargetOpts.FeaturesAsWritten.end());
151 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
152 TargetOpts.FeaturesAsWritten.end());
153 std::sort(ExistingFeatures.begin(), ExistingFeatures.end());
154 std::sort(ReadFeatures.begin(), ReadFeatures.end());
156 unsigned ExistingIdx = 0, ExistingN = ExistingFeatures.size();
157 unsigned ReadIdx = 0, ReadN = ReadFeatures.size();
158 while (ExistingIdx < ExistingN && ReadIdx < ReadN) {
159 if (ExistingFeatures[ExistingIdx] == ReadFeatures[ReadIdx]) {
165 if (ReadFeatures[ReadIdx] < ExistingFeatures[ExistingIdx]) {
167 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
168 << false << ReadFeatures[ReadIdx];
173 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
174 << true << ExistingFeatures[ExistingIdx];
178 if (ExistingIdx < ExistingN) {
180 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
181 << true << ExistingFeatures[ExistingIdx];
185 if (ReadIdx < ReadN) {
187 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
188 << false << ReadFeatures[ReadIdx];
196 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
198 const LangOptions &ExistingLangOpts = PP.getLangOpts();
199 return checkLanguageOptions(LangOpts, ExistingLangOpts,
200 Complain? &Reader.Diags : 0);
203 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
205 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
206 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
207 Complain? &Reader.Diags : 0);
211 typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >
215 /// \brief Collect the macro definitions provided by the given preprocessor
217 static void collectMacroDefinitions(const PreprocessorOptions &PPOpts,
218 MacroDefinitionsMap &Macros,
219 SmallVectorImpl<StringRef> *MacroNames = 0){
220 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
221 StringRef Macro = PPOpts.Macros[I].first;
222 bool IsUndef = PPOpts.Macros[I].second;
224 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
225 StringRef MacroName = MacroPair.first;
226 StringRef MacroBody = MacroPair.second;
228 // For an #undef'd macro, we only care about the name.
230 if (MacroNames && !Macros.count(MacroName))
231 MacroNames->push_back(MacroName);
233 Macros[MacroName] = std::make_pair("", true);
237 // For a #define'd macro, figure out the actual definition.
238 if (MacroName.size() == Macro.size())
241 // Note: GCC drops anything following an end-of-line character.
242 StringRef::size_type End = MacroBody.find_first_of("\n\r");
243 MacroBody = MacroBody.substr(0, End);
246 if (MacroNames && !Macros.count(MacroName))
247 MacroNames->push_back(MacroName);
248 Macros[MacroName] = std::make_pair(MacroBody, false);
252 /// \brief Check the preprocessor options deserialized from the control block
253 /// against the preprocessor options in an existing preprocessor.
255 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
256 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
257 const PreprocessorOptions &ExistingPPOpts,
258 DiagnosticsEngine *Diags,
259 FileManager &FileMgr,
260 std::string &SuggestedPredefines,
261 const LangOptions &LangOpts) {
262 // Check macro definitions.
263 MacroDefinitionsMap ASTFileMacros;
264 collectMacroDefinitions(PPOpts, ASTFileMacros);
265 MacroDefinitionsMap ExistingMacros;
266 SmallVector<StringRef, 4> ExistingMacroNames;
267 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
269 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
270 // Dig out the macro definition in the existing preprocessor options.
271 StringRef MacroName = ExistingMacroNames[I];
272 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
274 // Check whether we know anything about this macro name or not.
275 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known
276 = ASTFileMacros.find(MacroName);
277 if (Known == ASTFileMacros.end()) {
278 // FIXME: Check whether this identifier was referenced anywhere in the
279 // AST file. If so, we should reject the AST file. Unfortunately, this
280 // information isn't in the control block. What shall we do about it?
282 if (Existing.second) {
283 SuggestedPredefines += "#undef ";
284 SuggestedPredefines += MacroName.str();
285 SuggestedPredefines += '\n';
287 SuggestedPredefines += "#define ";
288 SuggestedPredefines += MacroName.str();
289 SuggestedPredefines += ' ';
290 SuggestedPredefines += Existing.first.str();
291 SuggestedPredefines += '\n';
296 // If the macro was defined in one but undef'd in the other, we have a
298 if (Existing.second != Known->second.second) {
300 Diags->Report(diag::err_pch_macro_def_undef)
301 << MacroName << Known->second.second;
306 // If the macro was #undef'd in both, or if the macro bodies are identical,
308 if (Existing.second || Existing.first == Known->second.first)
311 // The macro bodies differ; complain.
313 Diags->Report(diag::err_pch_macro_def_conflict)
314 << MacroName << Known->second.first << Existing.first;
319 // Check whether we're using predefines.
320 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) {
322 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
327 // Detailed record is important since it is used for the module cache hash.
328 if (LangOpts.Modules &&
329 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord) {
331 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
336 // Compute the #include and #include_macros lines we need.
337 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
338 StringRef File = ExistingPPOpts.Includes[I];
339 if (File == ExistingPPOpts.ImplicitPCHInclude)
342 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
343 != PPOpts.Includes.end())
346 SuggestedPredefines += "#include \"";
347 SuggestedPredefines +=
348 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
349 SuggestedPredefines += "\"\n";
352 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
353 StringRef File = ExistingPPOpts.MacroIncludes[I];
354 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
356 != PPOpts.MacroIncludes.end())
359 SuggestedPredefines += "#__include_macros \"";
360 SuggestedPredefines +=
361 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
362 SuggestedPredefines += "\"\n##\n";
368 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
370 std::string &SuggestedPredefines) {
371 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
373 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
374 Complain? &Reader.Diags : 0,
380 void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
382 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
386 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
387 PP.setCounterValue(Value);
390 //===----------------------------------------------------------------------===//
391 // AST reader implementation
392 //===----------------------------------------------------------------------===//
395 ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
396 DeserializationListener = Listener;
401 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
402 return serialization::ComputeHash(Sel);
406 std::pair<unsigned, unsigned>
407 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
408 using namespace clang::io;
409 unsigned KeyLen = ReadUnalignedLE16(d);
410 unsigned DataLen = ReadUnalignedLE16(d);
411 return std::make_pair(KeyLen, DataLen);
414 ASTSelectorLookupTrait::internal_key_type
415 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
416 using namespace clang::io;
417 SelectorTable &SelTable = Reader.getContext().Selectors;
418 unsigned N = ReadUnalignedLE16(d);
419 IdentifierInfo *FirstII
420 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
422 return SelTable.getNullarySelector(FirstII);
424 return SelTable.getUnarySelector(FirstII);
426 SmallVector<IdentifierInfo *, 16> Args;
427 Args.push_back(FirstII);
428 for (unsigned I = 1; I != N; ++I)
429 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)));
431 return SelTable.getSelector(N, Args.data());
434 ASTSelectorLookupTrait::data_type
435 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
437 using namespace clang::io;
441 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d));
442 unsigned NumInstanceMethodsAndBits = ReadUnalignedLE16(d);
443 unsigned NumFactoryMethodsAndBits = ReadUnalignedLE16(d);
444 Result.InstanceBits = NumInstanceMethodsAndBits & 0x3;
445 Result.FactoryBits = NumFactoryMethodsAndBits & 0x3;
446 unsigned NumInstanceMethods = NumInstanceMethodsAndBits >> 2;
447 unsigned NumFactoryMethods = NumFactoryMethodsAndBits >> 2;
449 // Load instance methods
450 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
451 if (ObjCMethodDecl *Method
452 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
453 Result.Instance.push_back(Method);
456 // Load factory methods
457 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
458 if (ObjCMethodDecl *Method
459 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
460 Result.Factory.push_back(Method);
466 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
467 return llvm::HashString(a);
470 std::pair<unsigned, unsigned>
471 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
472 using namespace clang::io;
473 unsigned DataLen = ReadUnalignedLE16(d);
474 unsigned KeyLen = ReadUnalignedLE16(d);
475 return std::make_pair(KeyLen, DataLen);
478 ASTIdentifierLookupTraitBase::internal_key_type
479 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
480 assert(n >= 2 && d[n-1] == '\0');
481 return StringRef((const char*) d, n-1);
484 /// \brief Whether the given identifier is "interesting".
485 static bool isInterestingIdentifier(IdentifierInfo &II) {
486 return II.isPoisoned() ||
487 II.isExtensionToken() ||
488 II.getObjCOrBuiltinID() ||
489 II.hasRevertedTokenIDToIdentifier() ||
490 II.hadMacroDefinition() ||
491 II.getFETokenInfo<void>();
494 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
495 const unsigned char* d,
497 using namespace clang::io;
498 unsigned RawID = ReadUnalignedLE32(d);
499 bool IsInteresting = RawID & 0x01;
501 // Wipe out the "is interesting" bit.
504 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
505 if (!IsInteresting) {
506 // For uninteresting identifiers, just build the IdentifierInfo
507 // and associate it with the persistent ID.
508 IdentifierInfo *II = KnownII;
510 II = &Reader.getIdentifierTable().getOwn(k);
513 Reader.SetIdentifierInfo(ID, II);
514 if (!II->isFromAST()) {
515 bool WasInteresting = isInterestingIdentifier(*II);
518 II->setChangedSinceDeserialization();
520 Reader.markIdentifierUpToDate(II);
524 unsigned ObjCOrBuiltinID = ReadUnalignedLE16(d);
525 unsigned Bits = ReadUnalignedLE16(d);
526 bool CPlusPlusOperatorKeyword = Bits & 0x01;
528 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
530 bool Poisoned = Bits & 0x01;
532 bool ExtensionToken = Bits & 0x01;
534 bool hasSubmoduleMacros = Bits & 0x01;
536 bool hadMacroDefinition = Bits & 0x01;
539 assert(Bits == 0 && "Extra bits in the identifier?");
542 // Build the IdentifierInfo itself and link the identifier ID with
543 // the new IdentifierInfo.
544 IdentifierInfo *II = KnownII;
546 II = &Reader.getIdentifierTable().getOwn(StringRef(k));
549 Reader.markIdentifierUpToDate(II);
550 if (!II->isFromAST()) {
551 bool WasInteresting = isInterestingIdentifier(*II);
554 II->setChangedSinceDeserialization();
557 // Set or check the various bits in the IdentifierInfo structure.
558 // Token IDs are read-only.
559 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
560 II->RevertTokenIDToIdentifier();
561 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
562 assert(II->isExtensionToken() == ExtensionToken &&
563 "Incorrect extension token flag");
564 (void)ExtensionToken;
566 II->setIsPoisoned(true);
567 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
568 "Incorrect C++ operator keyword flag");
569 (void)CPlusPlusOperatorKeyword;
571 // If this identifier is a macro, deserialize the macro
573 if (hadMacroDefinition) {
574 uint32_t MacroDirectivesOffset = ReadUnalignedLE32(d);
576 SmallVector<uint32_t, 8> LocalMacroIDs;
577 if (hasSubmoduleMacros) {
578 while (uint32_t LocalMacroID = ReadUnalignedLE32(d)) {
580 LocalMacroIDs.push_back(LocalMacroID);
585 if (F.Kind == MK_Module) {
586 for (SmallVectorImpl<uint32_t>::iterator
587 I = LocalMacroIDs.begin(), E = LocalMacroIDs.end(); I != E; ++I) {
588 MacroID MacID = Reader.getGlobalMacroID(F, *I);
589 Reader.addPendingMacroFromModule(II, &F, MacID, F.DirectImportLoc);
592 Reader.addPendingMacroFromPCH(II, &F, MacroDirectivesOffset);
596 Reader.SetIdentifierInfo(ID, II);
598 // Read all of the declarations visible at global scope with this
601 SmallVector<uint32_t, 4> DeclIDs;
602 for (; DataLen > 0; DataLen -= 4)
603 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
604 Reader.SetGloballyVisibleDecls(II, DeclIDs);
611 ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
612 llvm::FoldingSetNodeID ID;
613 ID.AddInteger(Key.Kind);
616 case DeclarationName::Identifier:
617 case DeclarationName::CXXLiteralOperatorName:
618 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
620 case DeclarationName::ObjCZeroArgSelector:
621 case DeclarationName::ObjCOneArgSelector:
622 case DeclarationName::ObjCMultiArgSelector:
623 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
625 case DeclarationName::CXXOperatorName:
626 ID.AddInteger((OverloadedOperatorKind)Key.Data);
628 case DeclarationName::CXXConstructorName:
629 case DeclarationName::CXXDestructorName:
630 case DeclarationName::CXXConversionFunctionName:
631 case DeclarationName::CXXUsingDirective:
635 return ID.ComputeHash();
638 ASTDeclContextNameLookupTrait::internal_key_type
639 ASTDeclContextNameLookupTrait::GetInternalKey(
640 const external_key_type& Name) const {
642 Key.Kind = Name.getNameKind();
643 switch (Name.getNameKind()) {
644 case DeclarationName::Identifier:
645 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
647 case DeclarationName::ObjCZeroArgSelector:
648 case DeclarationName::ObjCOneArgSelector:
649 case DeclarationName::ObjCMultiArgSelector:
650 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
652 case DeclarationName::CXXOperatorName:
653 Key.Data = Name.getCXXOverloadedOperator();
655 case DeclarationName::CXXLiteralOperatorName:
656 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
658 case DeclarationName::CXXConstructorName:
659 case DeclarationName::CXXDestructorName:
660 case DeclarationName::CXXConversionFunctionName:
661 case DeclarationName::CXXUsingDirective:
669 std::pair<unsigned, unsigned>
670 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
671 using namespace clang::io;
672 unsigned KeyLen = ReadUnalignedLE16(d);
673 unsigned DataLen = ReadUnalignedLE16(d);
674 return std::make_pair(KeyLen, DataLen);
677 ASTDeclContextNameLookupTrait::internal_key_type
678 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
679 using namespace clang::io;
682 Key.Kind = (DeclarationName::NameKind)*d++;
684 case DeclarationName::Identifier:
685 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
687 case DeclarationName::ObjCZeroArgSelector:
688 case DeclarationName::ObjCOneArgSelector:
689 case DeclarationName::ObjCMultiArgSelector:
691 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
694 case DeclarationName::CXXOperatorName:
695 Key.Data = *d++; // OverloadedOperatorKind
697 case DeclarationName::CXXLiteralOperatorName:
698 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
700 case DeclarationName::CXXConstructorName:
701 case DeclarationName::CXXDestructorName:
702 case DeclarationName::CXXConversionFunctionName:
703 case DeclarationName::CXXUsingDirective:
711 ASTDeclContextNameLookupTrait::data_type
712 ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
713 const unsigned char* d,
715 using namespace clang::io;
716 unsigned NumDecls = ReadUnalignedLE16(d);
717 LE32DeclID *Start = reinterpret_cast<LE32DeclID *>(
718 const_cast<unsigned char *>(d));
719 return std::make_pair(Start, Start + NumDecls);
722 bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
723 BitstreamCursor &Cursor,
724 const std::pair<uint64_t, uint64_t> &Offsets,
725 DeclContextInfo &Info) {
726 SavedStreamPosition SavedPosition(Cursor);
727 // First the lexical decls.
728 if (Offsets.first != 0) {
729 Cursor.JumpToBit(Offsets.first);
733 unsigned Code = Cursor.ReadCode();
734 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
735 if (RecCode != DECL_CONTEXT_LEXICAL) {
736 Error("Expected lexical block");
740 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob.data());
741 Info.NumLexicalDecls = Blob.size() / sizeof(KindDeclIDPair);
744 // Now the lookup table.
745 if (Offsets.second != 0) {
746 Cursor.JumpToBit(Offsets.second);
750 unsigned Code = Cursor.ReadCode();
751 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
752 if (RecCode != DECL_CONTEXT_VISIBLE) {
753 Error("Expected visible lookup table block");
756 Info.NameLookupTableData
757 = ASTDeclContextNameLookupTable::Create(
758 (const unsigned char *)Blob.data() + Record[0],
759 (const unsigned char *)Blob.data(),
760 ASTDeclContextNameLookupTrait(*this, M));
766 void ASTReader::Error(StringRef Msg) {
767 Error(diag::err_fe_pch_malformed, Msg);
770 void ASTReader::Error(unsigned DiagID,
771 StringRef Arg1, StringRef Arg2) {
772 if (Diags.isDiagnosticInFlight())
773 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
775 Diag(DiagID) << Arg1 << Arg2;
778 //===----------------------------------------------------------------------===//
779 // Source Manager Deserialization
780 //===----------------------------------------------------------------------===//
782 /// \brief Read the line table in the source manager block.
783 /// \returns true if there was an error.
784 bool ASTReader::ParseLineTable(ModuleFile &F,
785 SmallVectorImpl<uint64_t> &Record) {
787 LineTableInfo &LineTable = SourceMgr.getLineTable();
789 // Parse the file names
790 std::map<int, int> FileIDs;
791 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
792 // Extract the file name
793 unsigned FilenameLen = Record[Idx++];
794 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
796 MaybeAddSystemRootToFilename(F, Filename);
797 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
800 // Parse the line entries
801 std::vector<LineEntry> Entries;
802 while (Idx < Record.size()) {
803 int FID = Record[Idx++];
804 assert(FID >= 0 && "Serialized line entries for non-local file.");
805 // Remap FileID from 1-based old view.
806 FID += F.SLocEntryBaseID - 1;
808 // Extract the line entries
809 unsigned NumEntries = Record[Idx++];
810 assert(NumEntries && "Numentries is 00000");
812 Entries.reserve(NumEntries);
813 for (unsigned I = 0; I != NumEntries; ++I) {
814 unsigned FileOffset = Record[Idx++];
815 unsigned LineNo = Record[Idx++];
816 int FilenameID = FileIDs[Record[Idx++]];
817 SrcMgr::CharacteristicKind FileKind
818 = (SrcMgr::CharacteristicKind)Record[Idx++];
819 unsigned IncludeOffset = Record[Idx++];
820 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
821 FileKind, IncludeOffset));
823 LineTable.AddEntry(FileID::get(FID), Entries);
829 /// \brief Read a source manager block
830 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
831 using namespace SrcMgr;
833 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
835 // Set the source-location entry cursor to the current position in
836 // the stream. This cursor will be used to read the contents of the
837 // source manager block initially, and then lazily read
838 // source-location entries as needed.
839 SLocEntryCursor = F.Stream;
841 // The stream itself is going to skip over the source manager block.
842 if (F.Stream.SkipBlock()) {
843 Error("malformed block record in AST file");
847 // Enter the source manager block.
848 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
849 Error("malformed source manager block record in AST file");
855 llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
858 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
859 case llvm::BitstreamEntry::Error:
860 Error("malformed block record in AST file");
862 case llvm::BitstreamEntry::EndBlock:
864 case llvm::BitstreamEntry::Record:
865 // The interesting case.
872 switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
873 default: // Default behavior: ignore.
876 case SM_SLOC_FILE_ENTRY:
877 case SM_SLOC_BUFFER_ENTRY:
878 case SM_SLOC_EXPANSION_ENTRY:
879 // Once we hit one of the source location entries, we're done.
885 /// \brief If a header file is not found at the path that we expect it to be
886 /// and the PCH file was moved from its original location, try to resolve the
887 /// file by assuming that header+PCH were moved together and the header is in
888 /// the same place relative to the PCH.
890 resolveFileRelativeToOriginalDir(const std::string &Filename,
891 const std::string &OriginalDir,
892 const std::string &CurrDir) {
893 assert(OriginalDir != CurrDir &&
894 "No point trying to resolve the file if the PCH dir didn't change");
895 using namespace llvm::sys;
896 SmallString<128> filePath(Filename);
897 fs::make_absolute(filePath);
898 assert(path::is_absolute(OriginalDir));
899 SmallString<128> currPCHPath(CurrDir);
901 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
902 fileDirE = path::end(path::parent_path(filePath));
903 path::const_iterator origDirI = path::begin(OriginalDir),
904 origDirE = path::end(OriginalDir);
905 // Skip the common path components from filePath and OriginalDir.
906 while (fileDirI != fileDirE && origDirI != origDirE &&
907 *fileDirI == *origDirI) {
911 for (; origDirI != origDirE; ++origDirI)
912 path::append(currPCHPath, "..");
913 path::append(currPCHPath, fileDirI, fileDirE);
914 path::append(currPCHPath, path::filename(Filename));
915 return currPCHPath.str();
918 bool ASTReader::ReadSLocEntry(int ID) {
922 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
923 Error("source location entry ID out-of-range for AST file");
927 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
928 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
929 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
930 unsigned BaseOffset = F->SLocEntryBaseOffset;
932 ++NumSLocEntriesRead;
933 llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
934 if (Entry.Kind != llvm::BitstreamEntry::Record) {
935 Error("incorrectly-formatted source location entry in AST file");
941 switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
943 Error("incorrectly-formatted source location entry in AST file");
946 case SM_SLOC_FILE_ENTRY: {
947 // We will detect whether a file changed and return 'Failure' for it, but
948 // we will also try to fail gracefully by setting up the SLocEntry.
949 unsigned InputID = Record[4];
950 InputFile IF = getInputFile(*F, InputID);
951 const FileEntry *File = IF.getFile();
952 bool OverriddenBuffer = IF.isOverridden();
954 // Note that we only check if a File was returned. If it was out-of-date
955 // we have complained but we will continue creating a FileID to recover
960 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
961 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
962 // This is the module's main file.
963 IncludeLoc = getImportLocation(F);
965 SrcMgr::CharacteristicKind
966 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
967 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
968 ID, BaseOffset + Record[0]);
969 SrcMgr::FileInfo &FileInfo =
970 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
971 FileInfo.NumCreatedFIDs = Record[5];
973 FileInfo.setHasLineDirectives();
975 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
976 unsigned NumFileDecls = Record[7];
978 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
979 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
983 const SrcMgr::ContentCache *ContentCache
984 = SourceMgr.getOrCreateContentCache(File,
985 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
986 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
987 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
988 unsigned Code = SLocEntryCursor.ReadCode();
990 unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
992 if (RecCode != SM_SLOC_BUFFER_BLOB) {
993 Error("AST record has invalid code");
997 llvm::MemoryBuffer *Buffer
998 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), File->getName());
999 SourceMgr.overrideFileContents(File, Buffer);
1005 case SM_SLOC_BUFFER_ENTRY: {
1006 const char *Name = Blob.data();
1007 unsigned Offset = Record[0];
1008 SrcMgr::CharacteristicKind
1009 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1010 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1011 if (IncludeLoc.isInvalid() && F->Kind == MK_Module) {
1012 IncludeLoc = getImportLocation(F);
1014 unsigned Code = SLocEntryCursor.ReadCode();
1017 = SLocEntryCursor.readRecord(Code, Record, &Blob);
1019 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1020 Error("AST record has invalid code");
1024 llvm::MemoryBuffer *Buffer
1025 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name);
1026 SourceMgr.createFileIDForMemBuffer(Buffer, FileCharacter, ID,
1027 BaseOffset + Offset, IncludeLoc);
1031 case SM_SLOC_EXPANSION_ENTRY: {
1032 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1033 SourceMgr.createExpansionLoc(SpellingLoc,
1034 ReadSourceLocation(*F, Record[2]),
1035 ReadSourceLocation(*F, Record[3]),
1038 BaseOffset + Record[0]);
1046 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1048 return std::make_pair(SourceLocation(), "");
1050 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1051 Error("source location entry ID out-of-range for AST file");
1052 return std::make_pair(SourceLocation(), "");
1055 // Find which module file this entry lands in.
1056 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1057 if (M->Kind != MK_Module)
1058 return std::make_pair(SourceLocation(), "");
1060 // FIXME: Can we map this down to a particular submodule? That would be
1062 return std::make_pair(M->ImportLoc, llvm::sys::path::stem(M->FileName));
1065 /// \brief Find the location where the module F is imported.
1066 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1067 if (F->ImportLoc.isValid())
1068 return F->ImportLoc;
1070 // Otherwise we have a PCH. It's considered to be "imported" at the first
1071 // location of its includer.
1072 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1073 // Main file is the importer. We assume that it is the first entry in the
1074 // entry table. We can't ask the manager, because at the time of PCH loading
1075 // the main file entry doesn't exist yet.
1076 // The very first entry is the invalid instantiation loc, which takes up
1078 return SourceLocation::getFromRawEncoding(2U);
1080 //return F->Loaders[0]->FirstLoc;
1081 return F->ImportedBy[0]->FirstLoc;
1084 /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1085 /// specified cursor. Read the abbreviations that are at the top of the block
1086 /// and then leave the cursor pointing into the block.
1087 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
1088 if (Cursor.EnterSubBlock(BlockID)) {
1089 Error("malformed block record in AST file");
1094 uint64_t Offset = Cursor.GetCurrentBitNo();
1095 unsigned Code = Cursor.ReadCode();
1097 // We expect all abbrevs to be at the start of the block.
1098 if (Code != llvm::bitc::DEFINE_ABBREV) {
1099 Cursor.JumpToBit(Offset);
1102 Cursor.ReadAbbrevRecord();
1106 Token ASTReader::ReadToken(ModuleFile &F, const RecordData &Record,
1110 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1111 Tok.setLength(Record[Idx++]);
1112 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1113 Tok.setIdentifierInfo(II);
1114 Tok.setKind((tok::TokenKind)Record[Idx++]);
1115 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1119 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1120 BitstreamCursor &Stream = F.MacroCursor;
1122 // Keep track of where we are in the stream, then jump back there
1123 // after reading this macro.
1124 SavedStreamPosition SavedPosition(Stream);
1126 Stream.JumpToBit(Offset);
1128 SmallVector<IdentifierInfo*, 16> MacroArgs;
1129 MacroInfo *Macro = 0;
1132 // Advance to the next record, but if we get to the end of the block, don't
1133 // pop it (removing all the abbreviations from the cursor) since we want to
1134 // be able to reseek within the block and read entries.
1135 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1136 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1138 switch (Entry.Kind) {
1139 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1140 case llvm::BitstreamEntry::Error:
1141 Error("malformed block record in AST file");
1143 case llvm::BitstreamEntry::EndBlock:
1145 case llvm::BitstreamEntry::Record:
1146 // The interesting case.
1152 PreprocessorRecordTypes RecType =
1153 (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
1155 case PP_MACRO_DIRECTIVE_HISTORY:
1158 case PP_MACRO_OBJECT_LIKE:
1159 case PP_MACRO_FUNCTION_LIKE: {
1160 // If we already have a macro, that means that we've hit the end
1161 // of the definition of the macro we were looking for. We're
1166 unsigned NextIndex = 1; // Skip identifier ID.
1167 SubmoduleID SubModID = getGlobalSubmoduleID(F, Record[NextIndex++]);
1168 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1169 MacroInfo *MI = PP.AllocateDeserializedMacroInfo(Loc, SubModID);
1170 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1171 MI->setIsUsed(Record[NextIndex++]);
1173 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1174 // Decode function-like macro info.
1175 bool isC99VarArgs = Record[NextIndex++];
1176 bool isGNUVarArgs = Record[NextIndex++];
1177 bool hasCommaPasting = Record[NextIndex++];
1179 unsigned NumArgs = Record[NextIndex++];
1180 for (unsigned i = 0; i != NumArgs; ++i)
1181 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1183 // Install function-like macro info.
1184 MI->setIsFunctionLike();
1185 if (isC99VarArgs) MI->setIsC99Varargs();
1186 if (isGNUVarArgs) MI->setIsGNUVarargs();
1187 if (hasCommaPasting) MI->setHasCommaPasting();
1188 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
1189 PP.getPreprocessorAllocator());
1192 // Remember that we saw this macro last so that we add the tokens that
1193 // form its body to it.
1196 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1197 Record[NextIndex]) {
1198 // We have a macro definition. Register the association
1199 PreprocessedEntityID
1200 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1201 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1202 PreprocessingRecord::PPEntityID
1203 PPID = PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true);
1204 MacroDefinition *PPDef =
1205 cast_or_null<MacroDefinition>(PPRec.getPreprocessedEntity(PPID));
1207 PPRec.RegisterMacroDefinition(Macro, PPDef);
1215 // If we see a TOKEN before a PP_MACRO_*, then the file is
1216 // erroneous, just pretend we didn't see this.
1217 if (Macro == 0) break;
1220 Token Tok = ReadToken(F, Record, Idx);
1221 Macro->AddTokenToBody(Tok);
1228 PreprocessedEntityID
1229 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
1230 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1231 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1232 assert(I != M.PreprocessedEntityRemap.end()
1233 && "Invalid index into preprocessed entity index remap");
1235 return LocalID + I->second;
1238 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1239 return llvm::hash_combine(ikey.Size, ikey.ModTime);
1242 HeaderFileInfoTrait::internal_key_type
1243 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1244 internal_key_type ikey = { FE->getSize(), FE->getModificationTime(),
1249 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1250 if (a.Size != b.Size || a.ModTime != b.ModTime)
1253 if (strcmp(a.Filename, b.Filename) == 0)
1256 // Determine whether the actual files are equivalent.
1257 FileManager &FileMgr = Reader.getFileManager();
1258 const FileEntry *FEA = FileMgr.getFile(a.Filename);
1259 const FileEntry *FEB = FileMgr.getFile(b.Filename);
1260 return (FEA && FEA == FEB);
1263 std::pair<unsigned, unsigned>
1264 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1265 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1266 unsigned DataLen = (unsigned) *d++;
1267 return std::make_pair(KeyLen, DataLen);
1270 HeaderFileInfoTrait::internal_key_type
1271 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1272 internal_key_type ikey;
1273 ikey.Size = off_t(clang::io::ReadUnalignedLE64(d));
1274 ikey.ModTime = time_t(clang::io::ReadUnalignedLE64(d));
1275 ikey.Filename = (const char *)d;
1279 HeaderFileInfoTrait::data_type
1280 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1282 const unsigned char *End = d + DataLen;
1283 using namespace clang::io;
1285 unsigned Flags = *d++;
1286 HFI.isImport = (Flags >> 5) & 0x01;
1287 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1288 HFI.DirInfo = (Flags >> 2) & 0x03;
1289 HFI.Resolved = (Flags >> 1) & 0x01;
1290 HFI.IndexHeaderMapHeader = Flags & 0x01;
1291 HFI.NumIncludes = ReadUnalignedLE16(d);
1292 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1293 ReadUnalignedLE32(d));
1294 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1295 // The framework offset is 1 greater than the actual offset,
1296 // since 0 is used as an indicator for "no framework name".
1297 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1298 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1302 uint32_t LocalSMID = ReadUnalignedLE32(d);
1304 // This header is part of a module. Associate it with the module to enable
1305 // implicit module import.
1306 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1307 Module *Mod = Reader.getSubmodule(GlobalSMID);
1308 HFI.isModuleHeader = true;
1309 FileManager &FileMgr = Reader.getFileManager();
1311 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1312 ModMap.addHeader(Mod, FileMgr.getFile(key.Filename), /*Excluded=*/false);
1316 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1319 // This HeaderFileInfo was externally loaded.
1320 HFI.External = true;
1324 void ASTReader::addPendingMacroFromModule(IdentifierInfo *II,
1326 GlobalMacroID GMacID,
1327 SourceLocation ImportLoc) {
1328 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1329 PendingMacroIDs[II].push_back(PendingMacroInfo(M, GMacID, ImportLoc));
1332 void ASTReader::addPendingMacroFromPCH(IdentifierInfo *II,
1334 uint64_t MacroDirectivesOffset) {
1335 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1336 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1339 void ASTReader::ReadDefinedMacros() {
1340 // Note that we are loading defined macros.
1341 Deserializing Macros(this);
1343 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1344 E = ModuleMgr.rend(); I != E; ++I) {
1345 BitstreamCursor &MacroCursor = (*I)->MacroCursor;
1347 // If there was no preprocessor block, skip this file.
1348 if (!MacroCursor.getBitStreamReader())
1351 BitstreamCursor Cursor = MacroCursor;
1352 Cursor.JumpToBit((*I)->MacroStartOffset);
1356 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1359 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1360 case llvm::BitstreamEntry::Error:
1361 Error("malformed block record in AST file");
1363 case llvm::BitstreamEntry::EndBlock:
1366 case llvm::BitstreamEntry::Record:
1368 switch (Cursor.readRecord(E.ID, Record)) {
1369 default: // Default behavior: ignore.
1372 case PP_MACRO_OBJECT_LIKE:
1373 case PP_MACRO_FUNCTION_LIKE:
1374 getLocalIdentifier(**I, Record[0]);
1389 /// \brief Visitor class used to look up identifirs in an AST file.
1390 class IdentifierLookupVisitor {
1392 unsigned PriorGeneration;
1393 unsigned &NumIdentifierLookups;
1394 unsigned &NumIdentifierLookupHits;
1395 IdentifierInfo *Found;
1398 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1399 unsigned &NumIdentifierLookups,
1400 unsigned &NumIdentifierLookupHits)
1401 : Name(Name), PriorGeneration(PriorGeneration),
1402 NumIdentifierLookups(NumIdentifierLookups),
1403 NumIdentifierLookupHits(NumIdentifierLookupHits),
1408 static bool visit(ModuleFile &M, void *UserData) {
1409 IdentifierLookupVisitor *This
1410 = static_cast<IdentifierLookupVisitor *>(UserData);
1412 // If we've already searched this module file, skip it now.
1413 if (M.Generation <= This->PriorGeneration)
1416 ASTIdentifierLookupTable *IdTable
1417 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1421 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1423 ++This->NumIdentifierLookups;
1424 ASTIdentifierLookupTable::iterator Pos = IdTable->find(This->Name,&Trait);
1425 if (Pos == IdTable->end())
1428 // Dereferencing the iterator has the effect of building the
1429 // IdentifierInfo node and populating it with the various
1430 // declarations it needs.
1431 ++This->NumIdentifierLookupHits;
1436 // \brief Retrieve the identifier info found within the module
1438 IdentifierInfo *getIdentifierInfo() const { return Found; }
1442 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1443 // Note that we are loading an identifier.
1444 Deserializing AnIdentifier(this);
1446 unsigned PriorGeneration = 0;
1447 if (getContext().getLangOpts().Modules)
1448 PriorGeneration = IdentifierGeneration[&II];
1450 // If there is a global index, look there first to determine which modules
1451 // provably do not have any results for this identifier.
1452 GlobalModuleIndex::HitSet Hits;
1453 GlobalModuleIndex::HitSet *HitsPtr = 0;
1454 if (!loadGlobalIndex()) {
1455 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1460 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
1461 NumIdentifierLookups,
1462 NumIdentifierLookupHits);
1463 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
1464 markIdentifierUpToDate(&II);
1467 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1471 II->setOutOfDate(false);
1473 // Update the generation for this identifier.
1474 if (getContext().getLangOpts().Modules)
1475 IdentifierGeneration[II] = CurrentGeneration;
1478 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1479 const PendingMacroInfo &PMInfo) {
1482 if (PMInfo.M->Kind != MK_Module) {
1483 installPCHMacroDirectives(II, *PMInfo.M,
1484 PMInfo.PCHMacroData.MacroDirectivesOffset);
1490 GlobalMacroID GMacID = PMInfo.ModuleMacroData.GMacID;
1491 SourceLocation ImportLoc =
1492 SourceLocation::getFromRawEncoding(PMInfo.ModuleMacroData.ImportLoc);
1495 // If this macro has already been loaded, don't do so again.
1496 if (MacrosLoaded[GMacID - NUM_PREDEF_MACRO_IDS])
1499 MacroInfo *MI = getMacro(GMacID);
1500 SubmoduleID SubModID = MI->getOwningModuleID();
1501 MacroDirective *MD = PP.AllocateDefMacroDirective(MI, ImportLoc,
1502 /*isImported=*/true);
1504 // Determine whether this macro definition is visible.
1505 bool Hidden = false;
1508 if ((Owner = getSubmodule(SubModID))) {
1509 if (Owner->NameVisibility == Module::Hidden) {
1510 // The owning module is not visible, and this macro definition
1511 // should not be, either.
1514 // Note that this macro definition was hidden because its owning
1515 // module is not yet visible.
1516 HiddenNamesMap[Owner].push_back(HiddenName(II, MD));
1522 installImportedMacro(II, MD, Owner);
1525 void ASTReader::installPCHMacroDirectives(IdentifierInfo *II,
1526 ModuleFile &M, uint64_t Offset) {
1527 assert(M.Kind != MK_Module);
1529 BitstreamCursor &Cursor = M.MacroCursor;
1530 SavedStreamPosition SavedPosition(Cursor);
1531 Cursor.JumpToBit(Offset);
1533 llvm::BitstreamEntry Entry =
1534 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1535 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1536 Error("malformed block record in AST file");
1541 PreprocessorRecordTypes RecType =
1542 (PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record);
1543 if (RecType != PP_MACRO_DIRECTIVE_HISTORY) {
1544 Error("malformed block record in AST file");
1548 // Deserialize the macro directives history in reverse source-order.
1549 MacroDirective *Latest = 0, *Earliest = 0;
1550 unsigned Idx = 0, N = Record.size();
1552 MacroDirective *MD = 0;
1553 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
1554 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
1556 case MacroDirective::MD_Define: {
1557 GlobalMacroID GMacID = getGlobalMacroID(M, Record[Idx++]);
1558 MacroInfo *MI = getMacro(GMacID);
1559 bool isImported = Record[Idx++];
1560 bool isAmbiguous = Record[Idx++];
1561 DefMacroDirective *DefMD =
1562 PP.AllocateDefMacroDirective(MI, Loc, isImported);
1563 DefMD->setAmbiguous(isAmbiguous);
1567 case MacroDirective::MD_Undefine:
1568 MD = PP.AllocateUndefMacroDirective(Loc);
1570 case MacroDirective::MD_Visibility: {
1571 bool isPublic = Record[Idx++];
1572 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
1580 Earliest->setPrevious(MD);
1584 PP.setLoadedMacroDirective(II, Latest);
1587 /// \brief For the given macro definitions, check if they are both in system
1589 static bool areDefinedInSystemModules(MacroInfo *PrevMI, MacroInfo *NewMI,
1590 Module *NewOwner, ASTReader &Reader) {
1591 assert(PrevMI && NewMI);
1594 Module *PrevOwner = 0;
1595 if (SubmoduleID PrevModID = PrevMI->getOwningModuleID())
1596 PrevOwner = Reader.getSubmodule(PrevModID);
1599 if (PrevOwner == NewOwner)
1601 return PrevOwner->IsSystem && NewOwner->IsSystem;
1604 void ASTReader::installImportedMacro(IdentifierInfo *II, MacroDirective *MD,
1608 DefMacroDirective *DefMD = cast<DefMacroDirective>(MD);
1609 MacroDirective *Prev = PP.getMacroDirective(II);
1611 MacroDirective::DefInfo PrevDef = Prev->getDefinition();
1612 MacroInfo *PrevMI = PrevDef.getMacroInfo();
1613 MacroInfo *NewMI = DefMD->getInfo();
1614 if (NewMI != PrevMI && !PrevMI->isIdenticalTo(*NewMI, PP,
1615 /*Syntactically=*/true)) {
1616 // Before marking the macros as ambiguous, check if this is a case where
1617 // both macros are in system headers. If so, we trust that the system
1618 // did not get it wrong. This also handles cases where Clang's own
1619 // headers have a different spelling of certain system macros:
1620 // #define LONG_MAX __LONG_MAX__ (clang's limits.h)
1621 // #define LONG_MAX 0x7fffffffffffffffL (system's limits.h)
1622 if (!areDefinedInSystemModules(PrevMI, NewMI, Owner, *this)) {
1623 PrevDef.getDirective()->setAmbiguous(true);
1624 DefMD->setAmbiguous(true);
1629 PP.appendMacroDirective(II, MD);
1632 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
1633 // If this ID is bogus, just return an empty input file.
1634 if (ID == 0 || ID > F.InputFilesLoaded.size())
1637 // If we've already loaded this input file, return it.
1638 if (F.InputFilesLoaded[ID-1].getFile())
1639 return F.InputFilesLoaded[ID-1];
1641 // Go find this input file.
1642 BitstreamCursor &Cursor = F.InputFilesCursor;
1643 SavedStreamPosition SavedPosition(Cursor);
1644 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1646 unsigned Code = Cursor.ReadCode();
1649 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
1651 unsigned StoredID = Record[0];
1652 assert(ID == StoredID && "Bogus stored ID or offset");
1654 off_t StoredSize = (off_t)Record[1];
1655 time_t StoredTime = (time_t)Record[2];
1656 bool Overridden = (bool)Record[3];
1658 // Get the file entry for this input file.
1659 StringRef OrigFilename = Blob;
1660 std::string Filename = OrigFilename;
1661 MaybeAddSystemRootToFilename(F, Filename);
1662 const FileEntry *File
1663 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
1664 : FileMgr.getFile(Filename, /*OpenFile=*/false);
1666 // If we didn't find the file, resolve it relative to the
1667 // original directory from which this AST file was created.
1668 if (File == 0 && !F.OriginalDir.empty() && !CurrentDir.empty() &&
1669 F.OriginalDir != CurrentDir) {
1670 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1673 if (!Resolved.empty())
1674 File = FileMgr.getFile(Resolved);
1677 // For an overridden file, create a virtual file with the stored
1679 if (Overridden && File == 0) {
1680 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
1685 std::string ErrorStr = "could not find file '";
1686 ErrorStr += Filename;
1687 ErrorStr += "' referenced by AST file";
1688 Error(ErrorStr.c_str());
1693 // Check if there was a request to override the contents of the file
1694 // that was part of the precompiled header. Overridding such a file
1695 // can lead to problems when lexing using the source locations from the
1697 SourceManager &SM = getSourceManager();
1698 if (!Overridden && SM.isFileOverridden(File)) {
1700 Error(diag::err_fe_pch_file_overridden, Filename);
1701 // After emitting the diagnostic, recover by disabling the override so
1702 // that the original file will be used.
1703 SM.disableFileContentsOverride(File);
1704 // The FileEntry is a virtual file entry with the size of the contents
1705 // that would override the original contents. Set it to the original's
1707 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1708 StoredSize, StoredTime);
1711 bool IsOutOfDate = false;
1713 // For an overridden file, there is nothing to validate.
1714 if (!Overridden && (StoredSize != File->getSize()
1715 #if !defined(LLVM_ON_WIN32)
1716 // In our regression testing, the Windows file system seems to
1717 // have inconsistent modification times that sometimes
1718 // erroneously trigger this error-handling path.
1719 || StoredTime != File->getModificationTime()
1723 Error(diag::err_fe_pch_file_modified, Filename, F.FileName);
1729 InputFile IF = InputFile(File, Overridden, IsOutOfDate);
1731 // Note that we've loaded this input file.
1732 F.InputFilesLoaded[ID-1] = IF;
1740 const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
1741 ModuleFile &M = ModuleMgr.getPrimaryModule();
1742 std::string Filename = filenameStrRef;
1743 MaybeAddSystemRootToFilename(M, Filename);
1744 const FileEntry *File = FileMgr.getFile(Filename);
1745 if (File == 0 && !M.OriginalDir.empty() && !CurrentDir.empty() &&
1746 M.OriginalDir != CurrentDir) {
1747 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1750 if (!resolved.empty())
1751 File = FileMgr.getFile(resolved);
1757 /// \brief If we are loading a relocatable PCH file, and the filename is
1758 /// not an absolute path, add the system root to the beginning of the file
1760 void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
1761 std::string &Filename) {
1762 // If this is not a relocatable PCH file, there's nothing to do.
1763 if (!M.RelocatablePCH)
1766 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
1769 if (isysroot.empty()) {
1770 // If no system root was given, default to '/'
1771 Filename.insert(Filename.begin(), '/');
1775 unsigned Length = isysroot.size();
1776 if (isysroot[Length - 1] != '/')
1777 Filename.insert(Filename.begin(), '/');
1779 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
1782 ASTReader::ASTReadResult
1783 ASTReader::ReadControlBlock(ModuleFile &F,
1784 SmallVectorImpl<ImportedModule> &Loaded,
1785 unsigned ClientLoadCapabilities) {
1786 BitstreamCursor &Stream = F.Stream;
1788 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
1789 Error("malformed block record in AST file");
1793 // Read all of the records and blocks in the control block.
1796 llvm::BitstreamEntry Entry = Stream.advance();
1798 switch (Entry.Kind) {
1799 case llvm::BitstreamEntry::Error:
1800 Error("malformed block record in AST file");
1802 case llvm::BitstreamEntry::EndBlock:
1803 // Validate all of the non-system input files.
1804 if (!DisableValidation) {
1805 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
1806 // All user input files reside at the index range [0, Record[1]).
1807 // Record is the one from INPUT_FILE_OFFSETS.
1808 for (unsigned I = 0, N = Record[1]; I < N; ++I) {
1809 InputFile IF = getInputFile(F, I+1, Complain);
1810 if (!IF.getFile() || IF.isOutOfDate())
1816 case llvm::BitstreamEntry::SubBlock:
1818 case INPUT_FILES_BLOCK_ID:
1819 F.InputFilesCursor = Stream;
1820 if (Stream.SkipBlock() || // Skip with the main cursor
1821 // Read the abbreviations
1822 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
1823 Error("malformed block record in AST file");
1829 if (Stream.SkipBlock()) {
1830 Error("malformed block record in AST file");
1836 case llvm::BitstreamEntry::Record:
1837 // The interesting case.
1841 // Read and process a record.
1844 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
1846 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1847 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1848 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
1849 : diag::warn_pch_version_too_new);
1850 return VersionMismatch;
1853 bool hasErrors = Record[5];
1854 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1855 Diag(diag::err_pch_with_compiler_errors);
1859 F.RelocatablePCH = Record[4];
1861 const std::string &CurBranch = getClangFullRepositoryVersion();
1862 StringRef ASTBranch = Blob;
1863 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
1864 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1865 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
1866 return VersionMismatch;
1872 // Load each of the imported PCH files.
1873 unsigned Idx = 0, N = Record.size();
1875 // Read information about the AST file.
1876 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1877 // The import location will be the local one for now; we will adjust
1878 // all import locations of module imports after the global source
1879 // location info are setup.
1880 SourceLocation ImportLoc =
1881 SourceLocation::getFromRawEncoding(Record[Idx++]);
1882 off_t StoredSize = (off_t)Record[Idx++];
1883 time_t StoredModTime = (time_t)Record[Idx++];
1884 unsigned Length = Record[Idx++];
1885 SmallString<128> ImportedFile(Record.begin() + Idx,
1886 Record.begin() + Idx + Length);
1889 // Load the AST file.
1890 switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded,
1891 StoredSize, StoredModTime,
1892 ClientLoadCapabilities)) {
1893 case Failure: return Failure;
1894 // If we have to ignore the dependency, we'll have to ignore this too.
1896 case OutOfDate: return OutOfDate;
1897 case VersionMismatch: return VersionMismatch;
1898 case ConfigurationMismatch: return ConfigurationMismatch;
1899 case HadErrors: return HadErrors;
1900 case Success: break;
1906 case LANGUAGE_OPTIONS: {
1907 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
1908 if (Listener && &F == *ModuleMgr.begin() &&
1909 ParseLanguageOptions(Record, Complain, *Listener) &&
1911 return ConfigurationMismatch;
1915 case TARGET_OPTIONS: {
1916 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1917 if (Listener && &F == *ModuleMgr.begin() &&
1918 ParseTargetOptions(Record, Complain, *Listener) &&
1920 return ConfigurationMismatch;
1924 case DIAGNOSTIC_OPTIONS: {
1925 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1926 if (Listener && &F == *ModuleMgr.begin() &&
1927 ParseDiagnosticOptions(Record, Complain, *Listener) &&
1929 return ConfigurationMismatch;
1933 case FILE_SYSTEM_OPTIONS: {
1934 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1935 if (Listener && &F == *ModuleMgr.begin() &&
1936 ParseFileSystemOptions(Record, Complain, *Listener) &&
1938 return ConfigurationMismatch;
1942 case HEADER_SEARCH_OPTIONS: {
1943 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1944 if (Listener && &F == *ModuleMgr.begin() &&
1945 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
1947 return ConfigurationMismatch;
1951 case PREPROCESSOR_OPTIONS: {
1952 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1953 if (Listener && &F == *ModuleMgr.begin() &&
1954 ParsePreprocessorOptions(Record, Complain, *Listener,
1955 SuggestedPredefines) &&
1957 return ConfigurationMismatch;
1962 F.OriginalSourceFileID = FileID::get(Record[0]);
1963 F.ActualOriginalSourceFileName = Blob;
1964 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
1965 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
1968 case ORIGINAL_FILE_ID:
1969 F.OriginalSourceFileID = FileID::get(Record[0]);
1972 case ORIGINAL_PCH_DIR:
1973 F.OriginalDir = Blob;
1976 case INPUT_FILE_OFFSETS:
1977 F.InputFileOffsets = (const uint32_t *)Blob.data();
1978 F.InputFilesLoaded.resize(Record[0]);
1984 bool ASTReader::ReadASTBlock(ModuleFile &F) {
1985 BitstreamCursor &Stream = F.Stream;
1987 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
1988 Error("malformed block record in AST file");
1992 // Read all of the records and blocks for the AST file.
1995 llvm::BitstreamEntry Entry = Stream.advance();
1997 switch (Entry.Kind) {
1998 case llvm::BitstreamEntry::Error:
1999 Error("error at end of module block in AST file");
2001 case llvm::BitstreamEntry::EndBlock: {
2002 // Outside of C++, we do not store a lookup map for the translation unit.
2003 // Instead, mark it as needing a lookup map to be built if this module
2004 // contains any declarations lexically within it (which it always does!).
2005 // This usually has no cost, since we very rarely need the lookup map for
2006 // the translation unit outside C++.
2007 DeclContext *DC = Context.getTranslationUnitDecl();
2008 if (DC->hasExternalLexicalStorage() &&
2009 !getContext().getLangOpts().CPlusPlus)
2010 DC->setMustBuildLookupTable();
2014 case llvm::BitstreamEntry::SubBlock:
2016 case DECLTYPES_BLOCK_ID:
2017 // We lazily load the decls block, but we want to set up the
2018 // DeclsCursor cursor to point into it. Clone our current bitcode
2019 // cursor to it, enter the block and read the abbrevs in that block.
2020 // With the main cursor, we just skip over it.
2021 F.DeclsCursor = Stream;
2022 if (Stream.SkipBlock() || // Skip with the main cursor.
2023 // Read the abbrevs.
2024 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2025 Error("malformed block record in AST file");
2030 case DECL_UPDATES_BLOCK_ID:
2031 if (Stream.SkipBlock()) {
2032 Error("malformed block record in AST file");
2037 case PREPROCESSOR_BLOCK_ID:
2038 F.MacroCursor = Stream;
2039 if (!PP.getExternalSource())
2040 PP.setExternalSource(this);
2042 if (Stream.SkipBlock() ||
2043 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2044 Error("malformed block record in AST file");
2047 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2050 case PREPROCESSOR_DETAIL_BLOCK_ID:
2051 F.PreprocessorDetailCursor = Stream;
2052 if (Stream.SkipBlock() ||
2053 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
2054 PREPROCESSOR_DETAIL_BLOCK_ID)) {
2055 Error("malformed preprocessor detail record in AST file");
2058 F.PreprocessorDetailStartOffset
2059 = F.PreprocessorDetailCursor.GetCurrentBitNo();
2061 if (!PP.getPreprocessingRecord())
2062 PP.createPreprocessingRecord();
2063 if (!PP.getPreprocessingRecord()->getExternalSource())
2064 PP.getPreprocessingRecord()->SetExternalSource(*this);
2067 case SOURCE_MANAGER_BLOCK_ID:
2068 if (ReadSourceManagerBlock(F))
2072 case SUBMODULE_BLOCK_ID:
2073 if (ReadSubmoduleBlock(F))
2077 case COMMENTS_BLOCK_ID: {
2078 BitstreamCursor C = Stream;
2079 if (Stream.SkipBlock() ||
2080 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2081 Error("malformed comments block in AST file");
2084 CommentsCursors.push_back(std::make_pair(C, &F));
2089 if (Stream.SkipBlock()) {
2090 Error("malformed block record in AST file");
2097 case llvm::BitstreamEntry::Record:
2098 // The interesting case.
2102 // Read and process a record.
2105 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
2106 default: // Default behavior: ignore.
2110 if (F.LocalNumTypes != 0) {
2111 Error("duplicate TYPE_OFFSET record in AST file");
2114 F.TypeOffsets = (const uint32_t *)Blob.data();
2115 F.LocalNumTypes = Record[0];
2116 unsigned LocalBaseTypeIndex = Record[1];
2117 F.BaseTypeIndex = getTotalNumTypes();
2119 if (F.LocalNumTypes > 0) {
2120 // Introduce the global -> local mapping for types within this module.
2121 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2123 // Introduce the local -> global mapping for types within this module.
2124 F.TypeRemap.insertOrReplace(
2125 std::make_pair(LocalBaseTypeIndex,
2126 F.BaseTypeIndex - LocalBaseTypeIndex));
2128 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2134 if (F.LocalNumDecls != 0) {
2135 Error("duplicate DECL_OFFSET record in AST file");
2138 F.DeclOffsets = (const DeclOffset *)Blob.data();
2139 F.LocalNumDecls = Record[0];
2140 unsigned LocalBaseDeclID = Record[1];
2141 F.BaseDeclID = getTotalNumDecls();
2143 if (F.LocalNumDecls > 0) {
2144 // Introduce the global -> local mapping for declarations within this
2146 GlobalDeclMap.insert(
2147 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2149 // Introduce the local -> global mapping for declarations within this
2151 F.DeclRemap.insertOrReplace(
2152 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2154 // Introduce the global -> local mapping for declarations within this
2156 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2158 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2163 case TU_UPDATE_LEXICAL: {
2164 DeclContext *TU = Context.getTranslationUnitDecl();
2165 DeclContextInfo &Info = F.DeclContextInfos[TU];
2166 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(Blob.data());
2167 Info.NumLexicalDecls
2168 = static_cast<unsigned int>(Blob.size() / sizeof(KindDeclIDPair));
2169 TU->setHasExternalLexicalStorage(true);
2173 case UPDATE_VISIBLE: {
2175 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2176 ASTDeclContextNameLookupTable *Table =
2177 ASTDeclContextNameLookupTable::Create(
2178 (const unsigned char *)Blob.data() + Record[Idx++],
2179 (const unsigned char *)Blob.data(),
2180 ASTDeclContextNameLookupTrait(*this, F));
2181 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
2182 DeclContext *TU = Context.getTranslationUnitDecl();
2183 F.DeclContextInfos[TU].NameLookupTableData = Table;
2184 TU->setHasExternalVisibleStorage(true);
2186 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
2190 case IDENTIFIER_TABLE:
2191 F.IdentifierTableData = Blob.data();
2193 F.IdentifierLookupTable
2194 = ASTIdentifierLookupTable::Create(
2195 (const unsigned char *)F.IdentifierTableData + Record[0],
2196 (const unsigned char *)F.IdentifierTableData,
2197 ASTIdentifierLookupTrait(*this, F));
2199 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2203 case IDENTIFIER_OFFSET: {
2204 if (F.LocalNumIdentifiers != 0) {
2205 Error("duplicate IDENTIFIER_OFFSET record in AST file");
2208 F.IdentifierOffsets = (const uint32_t *)Blob.data();
2209 F.LocalNumIdentifiers = Record[0];
2210 unsigned LocalBaseIdentifierID = Record[1];
2211 F.BaseIdentifierID = getTotalNumIdentifiers();
2213 if (F.LocalNumIdentifiers > 0) {
2214 // Introduce the global -> local mapping for identifiers within this
2216 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2219 // Introduce the local -> global mapping for identifiers within this
2221 F.IdentifierRemap.insertOrReplace(
2222 std::make_pair(LocalBaseIdentifierID,
2223 F.BaseIdentifierID - LocalBaseIdentifierID));
2225 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2226 + F.LocalNumIdentifiers);
2231 case EXTERNAL_DEFINITIONS:
2232 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2233 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
2237 if (SpecialTypes.empty()) {
2238 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2239 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2243 if (SpecialTypes.size() != Record.size()) {
2244 Error("invalid special-types record");
2248 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2249 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2250 if (!SpecialTypes[I])
2251 SpecialTypes[I] = ID;
2252 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2258 TotalNumStatements += Record[0];
2259 TotalNumMacros += Record[1];
2260 TotalLexicalDeclContexts += Record[2];
2261 TotalVisibleDeclContexts += Record[3];
2264 case UNUSED_FILESCOPED_DECLS:
2265 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2266 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2269 case DELEGATING_CTORS:
2270 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2271 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2274 case WEAK_UNDECLARED_IDENTIFIERS:
2275 if (Record.size() % 4 != 0) {
2276 Error("invalid weak identifiers record");
2280 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2281 // files. This isn't the way to do it :)
2282 WeakUndeclaredIdentifiers.clear();
2284 // Translate the weak, undeclared identifiers into global IDs.
2285 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2286 WeakUndeclaredIdentifiers.push_back(
2287 getGlobalIdentifierID(F, Record[I++]));
2288 WeakUndeclaredIdentifiers.push_back(
2289 getGlobalIdentifierID(F, Record[I++]));
2290 WeakUndeclaredIdentifiers.push_back(
2291 ReadSourceLocation(F, Record, I).getRawEncoding());
2292 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2296 case LOCALLY_SCOPED_EXTERN_C_DECLS:
2297 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2298 LocallyScopedExternCDecls.push_back(getGlobalDeclID(F, Record[I]));
2301 case SELECTOR_OFFSETS: {
2302 F.SelectorOffsets = (const uint32_t *)Blob.data();
2303 F.LocalNumSelectors = Record[0];
2304 unsigned LocalBaseSelectorID = Record[1];
2305 F.BaseSelectorID = getTotalNumSelectors();
2307 if (F.LocalNumSelectors > 0) {
2308 // Introduce the global -> local mapping for selectors within this
2310 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2312 // Introduce the local -> global mapping for selectors within this
2314 F.SelectorRemap.insertOrReplace(
2315 std::make_pair(LocalBaseSelectorID,
2316 F.BaseSelectorID - LocalBaseSelectorID));
2318 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2324 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
2326 F.SelectorLookupTable
2327 = ASTSelectorLookupTable::Create(
2328 F.SelectorLookupTableData + Record[0],
2329 F.SelectorLookupTableData,
2330 ASTSelectorLookupTrait(*this, F));
2331 TotalNumMethodPoolEntries += Record[1];
2334 case REFERENCED_SELECTOR_POOL:
2335 if (!Record.empty()) {
2336 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2337 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2339 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2345 case PP_COUNTER_VALUE:
2346 if (!Record.empty() && Listener)
2347 Listener->ReadCounter(F, Record[0]);
2350 case FILE_SORTED_DECLS:
2351 F.FileSortedDecls = (const DeclID *)Blob.data();
2352 F.NumFileSortedDecls = Record[0];
2355 case SOURCE_LOCATION_OFFSETS: {
2356 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
2357 F.LocalNumSLocEntries = Record[0];
2358 unsigned SLocSpaceSize = Record[1];
2359 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
2360 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2362 // Make our entry in the range map. BaseID is negative and growing, so
2363 // we invert it. Because we invert it, though, we need the other end of
2365 unsigned RangeStart =
2366 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2367 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2368 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2370 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2371 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2372 GlobalSLocOffsetMap.insert(
2373 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2374 - SLocSpaceSize,&F));
2376 // Initialize the remapping table.
2377 // Invalid stays invalid.
2378 F.SLocRemap.insert(std::make_pair(0U, 0));
2379 // This module. Base was 2 when being compiled.
2380 F.SLocRemap.insert(std::make_pair(2U,
2381 static_cast<int>(F.SLocEntryBaseOffset - 2)));
2383 TotalNumSLocEntries += F.LocalNumSLocEntries;
2387 case MODULE_OFFSET_MAP: {
2388 // Additional remapping information.
2389 const unsigned char *Data = (const unsigned char*)Blob.data();
2390 const unsigned char *DataEnd = Data + Blob.size();
2392 // Continuous range maps we may be updating in our module.
2393 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
2394 ContinuousRangeMap<uint32_t, int, 2>::Builder
2395 IdentifierRemap(F.IdentifierRemap);
2396 ContinuousRangeMap<uint32_t, int, 2>::Builder
2397 MacroRemap(F.MacroRemap);
2398 ContinuousRangeMap<uint32_t, int, 2>::Builder
2399 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2400 ContinuousRangeMap<uint32_t, int, 2>::Builder
2401 SubmoduleRemap(F.SubmoduleRemap);
2402 ContinuousRangeMap<uint32_t, int, 2>::Builder
2403 SelectorRemap(F.SelectorRemap);
2404 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
2405 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2407 while(Data < DataEnd) {
2408 uint16_t Len = io::ReadUnalignedLE16(Data);
2409 StringRef Name = StringRef((const char*)Data, Len);
2411 ModuleFile *OM = ModuleMgr.lookup(Name);
2413 Error("SourceLocation remap refers to unknown module");
2417 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2418 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
2419 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
2420 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
2421 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
2422 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2423 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
2424 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
2426 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2427 SLocRemap.insert(std::make_pair(SLocOffset,
2428 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
2429 IdentifierRemap.insert(
2430 std::make_pair(IdentifierIDOffset,
2431 OM->BaseIdentifierID - IdentifierIDOffset));
2432 MacroRemap.insert(std::make_pair(MacroIDOffset,
2433 OM->BaseMacroID - MacroIDOffset));
2434 PreprocessedEntityRemap.insert(
2435 std::make_pair(PreprocessedEntityIDOffset,
2436 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
2437 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2438 OM->BaseSubmoduleID - SubmoduleIDOffset));
2439 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2440 OM->BaseSelectorID - SelectorIDOffset));
2441 DeclRemap.insert(std::make_pair(DeclIDOffset,
2442 OM->BaseDeclID - DeclIDOffset));
2444 TypeRemap.insert(std::make_pair(TypeIndexOffset,
2445 OM->BaseTypeIndex - TypeIndexOffset));
2447 // Global -> local mappings.
2448 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
2453 case SOURCE_MANAGER_LINE_TABLE:
2454 if (ParseLineTable(F, Record))
2458 case SOURCE_LOCATION_PRELOADS: {
2459 // Need to transform from the local view (1-based IDs) to the global view,
2460 // which is based off F.SLocEntryBaseID.
2461 if (!F.PreloadSLocEntries.empty()) {
2462 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2466 F.PreloadSLocEntries.swap(Record);
2470 case EXT_VECTOR_DECLS:
2471 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2472 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
2476 if (Record.size() % 3 != 0) {
2477 Error("Invalid VTABLE_USES record");
2481 // Later tables overwrite earlier ones.
2482 // FIXME: Modules will have some trouble with this. This is clearly not
2483 // the right way to do this.
2486 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2487 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2488 VTableUses.push_back(
2489 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2490 VTableUses.push_back(Record[Idx++]);
2494 case DYNAMIC_CLASSES:
2495 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2496 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
2499 case PENDING_IMPLICIT_INSTANTIATIONS:
2500 if (PendingInstantiations.size() % 2 != 0) {
2501 Error("Invalid existing PendingInstantiations");
2505 if (Record.size() % 2 != 0) {
2506 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2510 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2511 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2512 PendingInstantiations.push_back(
2513 ReadSourceLocation(F, Record, I).getRawEncoding());
2517 case SEMA_DECL_REFS:
2518 // Later tables overwrite earlier ones.
2519 // FIXME: Modules will have some trouble with this.
2520 SemaDeclRefs.clear();
2521 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2522 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2525 case PPD_ENTITIES_OFFSETS: {
2526 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
2527 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
2528 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
2530 unsigned LocalBasePreprocessedEntityID = Record[0];
2532 unsigned StartingID;
2533 if (!PP.getPreprocessingRecord())
2534 PP.createPreprocessingRecord();
2535 if (!PP.getPreprocessingRecord()->getExternalSource())
2536 PP.getPreprocessingRecord()->SetExternalSource(*this);
2538 = PP.getPreprocessingRecord()
2539 ->allocateLoadedEntities(F.NumPreprocessedEntities);
2540 F.BasePreprocessedEntityID = StartingID;
2542 if (F.NumPreprocessedEntities > 0) {
2543 // Introduce the global -> local mapping for preprocessed entities in
2545 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2547 // Introduce the local -> global mapping for preprocessed entities in
2549 F.PreprocessedEntityRemap.insertOrReplace(
2550 std::make_pair(LocalBasePreprocessedEntityID,
2551 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2557 case DECL_UPDATE_OFFSETS: {
2558 if (Record.size() % 2 != 0) {
2559 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2562 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2563 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2564 .push_back(std::make_pair(&F, Record[I+1]));
2568 case DECL_REPLACEMENTS: {
2569 if (Record.size() % 3 != 0) {
2570 Error("invalid DECL_REPLACEMENTS block in AST file");
2573 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
2574 ReplacedDecls[getGlobalDeclID(F, Record[I])]
2575 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
2579 case OBJC_CATEGORIES_MAP: {
2580 if (F.LocalNumObjCCategoriesInMap != 0) {
2581 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
2585 F.LocalNumObjCCategoriesInMap = Record[0];
2586 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
2590 case OBJC_CATEGORIES:
2591 F.ObjCCategories.swap(Record);
2594 case CXX_BASE_SPECIFIER_OFFSETS: {
2595 if (F.LocalNumCXXBaseSpecifiers != 0) {
2596 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2600 F.LocalNumCXXBaseSpecifiers = Record[0];
2601 F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data();
2602 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
2606 case DIAG_PRAGMA_MAPPINGS:
2607 if (F.PragmaDiagMappings.empty())
2608 F.PragmaDiagMappings.swap(Record);
2610 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2611 Record.begin(), Record.end());
2614 case CUDA_SPECIAL_DECL_REFS:
2615 // Later tables overwrite earlier ones.
2616 // FIXME: Modules will have trouble with this.
2617 CUDASpecialDeclRefs.clear();
2618 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2619 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2622 case HEADER_SEARCH_TABLE: {
2623 F.HeaderFileInfoTableData = Blob.data();
2624 F.LocalNumHeaderFileInfos = Record[1];
2626 F.HeaderFileInfoTable
2627 = HeaderFileInfoLookupTable::Create(
2628 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2629 (const unsigned char *)F.HeaderFileInfoTableData,
2630 HeaderFileInfoTrait(*this, F,
2631 &PP.getHeaderSearchInfo(),
2632 Blob.data() + Record[2]));
2634 PP.getHeaderSearchInfo().SetExternalSource(this);
2635 if (!PP.getHeaderSearchInfo().getExternalLookup())
2636 PP.getHeaderSearchInfo().SetExternalLookup(this);
2641 case FP_PRAGMA_OPTIONS:
2642 // Later tables overwrite earlier ones.
2643 FPPragmaOptions.swap(Record);
2646 case OPENCL_EXTENSIONS:
2647 // Later tables overwrite earlier ones.
2648 OpenCLExtensions.swap(Record);
2651 case TENTATIVE_DEFINITIONS:
2652 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2653 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
2656 case KNOWN_NAMESPACES:
2657 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2658 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
2661 case UNDEFINED_BUT_USED:
2662 if (UndefinedButUsed.size() % 2 != 0) {
2663 Error("Invalid existing UndefinedButUsed");
2667 if (Record.size() % 2 != 0) {
2668 Error("invalid undefined-but-used record");
2671 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2672 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
2673 UndefinedButUsed.push_back(
2674 ReadSourceLocation(F, Record, I).getRawEncoding());
2678 case IMPORTED_MODULES: {
2679 if (F.Kind != MK_Module) {
2680 // If we aren't loading a module (which has its own exports), make
2681 // all of the imported modules visible.
2682 // FIXME: Deal with macros-only imports.
2683 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2684 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2685 ImportedModules.push_back(GlobalID);
2691 case LOCAL_REDECLARATIONS: {
2692 F.RedeclarationChains.swap(Record);
2696 case LOCAL_REDECLARATIONS_MAP: {
2697 if (F.LocalNumRedeclarationsInMap != 0) {
2698 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
2702 F.LocalNumRedeclarationsInMap = Record[0];
2703 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data();
2707 case MERGED_DECLARATIONS: {
2708 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2709 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2710 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2711 for (unsigned N = Record[Idx++]; N > 0; --N)
2712 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2717 case MACRO_OFFSET: {
2718 if (F.LocalNumMacros != 0) {
2719 Error("duplicate MACRO_OFFSET record in AST file");
2722 F.MacroOffsets = (const uint32_t *)Blob.data();
2723 F.LocalNumMacros = Record[0];
2724 unsigned LocalBaseMacroID = Record[1];
2725 F.BaseMacroID = getTotalNumMacros();
2727 if (F.LocalNumMacros > 0) {
2728 // Introduce the global -> local mapping for macros within this module.
2729 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
2731 // Introduce the local -> global mapping for macros within this module.
2732 F.MacroRemap.insertOrReplace(
2733 std::make_pair(LocalBaseMacroID,
2734 F.BaseMacroID - LocalBaseMacroID));
2736 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
2742 // FIXME: Not used yet.
2749 /// \brief Move the given method to the back of the global list of methods.
2750 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
2751 // Find the entry for this selector in the method pool.
2752 Sema::GlobalMethodPool::iterator Known
2753 = S.MethodPool.find(Method->getSelector());
2754 if (Known == S.MethodPool.end())
2757 // Retrieve the appropriate method list.
2758 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
2759 : Known->second.second;
2761 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
2763 if (List->Method == Method) {
2771 if (List->getNext())
2772 List->Method = List->getNext()->Method;
2774 List->Method = Method;
2778 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
2779 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
2780 switch (Names[I].getKind()) {
2781 case HiddenName::Declaration: {
2782 Decl *D = Names[I].getDecl();
2783 bool wasHidden = D->Hidden;
2786 if (wasHidden && SemaObj) {
2787 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
2788 moveMethodToBackOfGlobalList(*SemaObj, Method);
2793 case HiddenName::MacroVisibility: {
2794 std::pair<IdentifierInfo *, MacroDirective *> Macro = Names[I].getMacro();
2795 installImportedMacro(Macro.first, Macro.second, Owner);
2802 void ASTReader::makeModuleVisible(Module *Mod,
2803 Module::NameVisibilityKind NameVisibility,
2804 SourceLocation ImportLoc,
2806 llvm::SmallPtrSet<Module *, 4> Visited;
2807 SmallVector<Module *, 4> Stack;
2808 Stack.push_back(Mod);
2809 while (!Stack.empty()) {
2813 if (NameVisibility <= Mod->NameVisibility) {
2814 // This module already has this level of visibility (or greater), so
2815 // there is nothing more to do.
2819 if (!Mod->isAvailable()) {
2820 // Modules that aren't available cannot be made visible.
2824 // Update the module's name visibility.
2825 Mod->NameVisibility = NameVisibility;
2827 // If we've already deserialized any names from this module,
2828 // mark them as visible.
2829 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2830 if (Hidden != HiddenNamesMap.end()) {
2831 makeNamesVisible(Hidden->second, Hidden->first);
2832 HiddenNamesMap.erase(Hidden);
2835 // Push any non-explicit submodules onto the stack to be marked as
2837 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2838 SubEnd = Mod->submodule_end();
2839 Sub != SubEnd; ++Sub) {
2840 if (!(*Sub)->IsExplicit && Visited.insert(*Sub))
2841 Stack.push_back(*Sub);
2844 // Push any exported modules onto the stack to be marked as visible.
2845 SmallVector<Module *, 16> Exports;
2846 Mod->getExportedModules(Exports);
2847 for (SmallVectorImpl<Module *>::iterator
2848 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
2849 Module *Exported = *I;
2850 if (Visited.insert(Exported))
2851 Stack.push_back(Exported);
2854 // Detect any conflicts.
2856 assert(ImportLoc.isValid() && "Missing import location");
2857 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2858 if (Mod->Conflicts[I].Other->NameVisibility >= NameVisibility) {
2859 Diag(ImportLoc, diag::warn_module_conflict)
2860 << Mod->getFullModuleName()
2861 << Mod->Conflicts[I].Other->getFullModuleName()
2862 << Mod->Conflicts[I].Message;
2863 // FIXME: Need note where the other module was imported.
2870 bool ASTReader::loadGlobalIndex() {
2874 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
2875 !Context.getLangOpts().Modules)
2878 // Try to load the global index.
2879 TriedLoadingGlobalIndex = true;
2880 StringRef ModuleCachePath
2881 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
2882 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
2883 = GlobalModuleIndex::readIndex(ModuleCachePath);
2887 GlobalIndex.reset(Result.first);
2888 ModuleMgr.setGlobalIndex(GlobalIndex.get());
2892 bool ASTReader::isGlobalIndexUnavailable() const {
2893 return Context.getLangOpts().Modules && UseGlobalIndex &&
2894 !hasGlobalIndex() && TriedLoadingGlobalIndex;
2897 ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2899 SourceLocation ImportLoc,
2900 unsigned ClientLoadCapabilities) {
2901 // Bump the generation number.
2902 unsigned PreviousGeneration = CurrentGeneration++;
2904 unsigned NumModules = ModuleMgr.size();
2905 SmallVector<ImportedModule, 4> Loaded;
2906 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
2907 /*ImportedBy=*/0, Loaded,
2909 ClientLoadCapabilities)) {
2913 case VersionMismatch:
2914 case ConfigurationMismatch:
2916 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
2917 Context.getLangOpts().Modules
2918 ? &PP.getHeaderSearchInfo().getModuleMap()
2921 // If we find that any modules are unusable, the global index is going
2922 // to be out-of-date. Just remove it.
2923 GlobalIndex.reset();
2924 ModuleMgr.setGlobalIndex(0);
2931 // Here comes stuff that we only do once the entire chain is loaded.
2933 // Load the AST blocks of all of the modules that we loaded.
2934 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
2935 MEnd = Loaded.end();
2937 ModuleFile &F = *M->Mod;
2939 // Read the AST block.
2940 if (ReadASTBlock(F))
2943 // Once read, set the ModuleFile bit base offset and update the size in
2944 // bits of all files we've seen.
2945 F.GlobalBitOffset = TotalModulesSizeInBits;
2946 TotalModulesSizeInBits += F.SizeInBits;
2947 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
2949 // Preload SLocEntries.
2950 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
2951 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
2952 // Load it through the SourceManager and don't call ReadSLocEntry()
2953 // directly because the entry may have already been loaded in which case
2954 // calling ReadSLocEntry() directly would trigger an assertion in
2956 SourceMgr.getLoadedSLocEntryByID(Index);
2960 // Setup the import locations and notify the module manager that we've
2961 // committed to these module files.
2962 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
2963 MEnd = Loaded.end();
2965 ModuleFile &F = *M->Mod;
2967 ModuleMgr.moduleFileAccepted(&F);
2969 // Set the import location.
2970 F.DirectImportLoc = ImportLoc;
2972 F.ImportLoc = M->ImportLoc;
2974 F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
2975 M->ImportLoc.getRawEncoding());
2978 // Mark all of the identifiers in the identifier table as being out of date,
2979 // so that various accessors know to check the loaded modules when the
2980 // identifier is used.
2981 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2982 IdEnd = PP.getIdentifierTable().end();
2984 Id->second->setOutOfDate(true);
2986 // Resolve any unresolved module exports.
2987 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
2988 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
2989 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
2990 Module *ResolvedMod = getSubmodule(GlobalID);
2992 switch (Unresolved.Kind) {
2993 case UnresolvedModuleRef::Conflict:
2995 Module::Conflict Conflict;
2996 Conflict.Other = ResolvedMod;
2997 Conflict.Message = Unresolved.String.str();
2998 Unresolved.Mod->Conflicts.push_back(Conflict);
3002 case UnresolvedModuleRef::Import:
3004 Unresolved.Mod->Imports.push_back(ResolvedMod);
3007 case UnresolvedModuleRef::Export:
3008 if (ResolvedMod || Unresolved.IsWildcard)
3009 Unresolved.Mod->Exports.push_back(
3010 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
3014 UnresolvedModuleRefs.clear();
3016 InitializeContext();
3018 if (DeserializationListener)
3019 DeserializationListener->ReaderInitialized(this);
3021 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
3022 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
3023 PrimaryModule.OriginalSourceFileID
3024 = FileID::get(PrimaryModule.SLocEntryBaseID
3025 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
3027 // If this AST file is a precompiled preamble, then set the
3028 // preamble file ID of the source manager to the file source file
3029 // from which the preamble was built.
3030 if (Type == MK_Preamble) {
3031 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
3032 } else if (Type == MK_MainFile) {
3033 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
3037 // For any Objective-C class definitions we have already loaded, make sure
3038 // that we load any additional categories.
3039 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3040 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3041 ObjCClassesLoaded[I],
3042 PreviousGeneration);
3048 ASTReader::ASTReadResult
3049 ASTReader::ReadASTCore(StringRef FileName,
3051 SourceLocation ImportLoc,
3052 ModuleFile *ImportedBy,
3053 SmallVectorImpl<ImportedModule> &Loaded,
3054 off_t ExpectedSize, time_t ExpectedModTime,
3055 unsigned ClientLoadCapabilities) {
3057 std::string ErrorStr;
3058 ModuleManager::AddModuleResult AddResult
3059 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
3060 CurrentGeneration, ExpectedSize, ExpectedModTime,
3063 switch (AddResult) {
3064 case ModuleManager::AlreadyLoaded:
3067 case ModuleManager::NewlyLoaded:
3068 // Load module file below.
3071 case ModuleManager::Missing:
3072 // The module file was missing; if the client handle handle, that, return
3074 if (ClientLoadCapabilities & ARR_Missing)
3077 // Otherwise, return an error.
3079 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3085 case ModuleManager::OutOfDate:
3086 // We couldn't load the module file because it is out-of-date. If the
3087 // client can handle out-of-date, return it.
3088 if (ClientLoadCapabilities & ARR_OutOfDate)
3091 // Otherwise, return an error.
3093 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3100 assert(M && "Missing module file");
3102 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3104 if (FileName != "-") {
3105 CurrentDir = llvm::sys::path::parent_path(FileName);
3106 if (CurrentDir.empty()) CurrentDir = ".";
3110 BitstreamCursor &Stream = F.Stream;
3111 Stream.init(F.StreamFile);
3112 F.SizeInBits = F.Buffer->getBufferSize() * 8;
3114 // Sniff for the signature.
3115 if (Stream.Read(8) != 'C' ||
3116 Stream.Read(8) != 'P' ||
3117 Stream.Read(8) != 'C' ||
3118 Stream.Read(8) != 'H') {
3119 Diag(diag::err_not_a_pch_file) << FileName;
3123 // This is used for compatibility with older PCH formats.
3124 bool HaveReadControlBlock = false;
3127 llvm::BitstreamEntry Entry = Stream.advance();
3129 switch (Entry.Kind) {
3130 case llvm::BitstreamEntry::Error:
3131 case llvm::BitstreamEntry::EndBlock:
3132 case llvm::BitstreamEntry::Record:
3133 Error("invalid record at top-level of AST file");
3136 case llvm::BitstreamEntry::SubBlock:
3140 // We only know the control subblock ID.
3142 case llvm::bitc::BLOCKINFO_BLOCK_ID:
3143 if (Stream.ReadBlockInfoBlock()) {
3144 Error("malformed BlockInfoBlock in AST file");
3148 case CONTROL_BLOCK_ID:
3149 HaveReadControlBlock = true;
3150 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) {
3154 case Failure: return Failure;
3155 case Missing: return Missing;
3156 case OutOfDate: return OutOfDate;
3157 case VersionMismatch: return VersionMismatch;
3158 case ConfigurationMismatch: return ConfigurationMismatch;
3159 case HadErrors: return HadErrors;
3163 if (!HaveReadControlBlock) {
3164 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3165 Diag(diag::warn_pch_version_too_old);
3166 return VersionMismatch;
3169 // Record that we've loaded this module.
3170 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
3174 if (Stream.SkipBlock()) {
3175 Error("malformed block record in AST file");
3185 void ASTReader::InitializeContext() {
3186 // If there's a listener, notify them that we "read" the translation unit.
3187 if (DeserializationListener)
3188 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3189 Context.getTranslationUnitDecl());
3191 // Make sure we load the declaration update records for the translation unit,
3192 // if there are any.
3193 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
3194 Context.getTranslationUnitDecl());
3196 // FIXME: Find a better way to deal with collisions between these
3197 // built-in types. Right now, we just ignore the problem.
3199 // Load the special types.
3200 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
3201 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3202 if (!Context.CFConstantStringTypeDecl)
3203 Context.setCFConstantStringType(GetType(String));
3206 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3207 QualType FileType = GetType(File);
3208 if (FileType.isNull()) {
3209 Error("FILE type is NULL");
3213 if (!Context.FILEDecl) {
3214 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3215 Context.setFILEDecl(Typedef->getDecl());
3217 const TagType *Tag = FileType->getAs<TagType>();
3219 Error("Invalid FILE type in AST file");
3222 Context.setFILEDecl(Tag->getDecl());
3227 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
3228 QualType Jmp_bufType = GetType(Jmp_buf);
3229 if (Jmp_bufType.isNull()) {
3230 Error("jmp_buf type is NULL");
3234 if (!Context.jmp_bufDecl) {
3235 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3236 Context.setjmp_bufDecl(Typedef->getDecl());
3238 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3240 Error("Invalid jmp_buf type in AST file");
3243 Context.setjmp_bufDecl(Tag->getDecl());
3248 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
3249 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3250 if (Sigjmp_bufType.isNull()) {
3251 Error("sigjmp_buf type is NULL");
3255 if (!Context.sigjmp_bufDecl) {
3256 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3257 Context.setsigjmp_bufDecl(Typedef->getDecl());
3259 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3260 assert(Tag && "Invalid sigjmp_buf type in AST file");
3261 Context.setsigjmp_bufDecl(Tag->getDecl());
3266 if (unsigned ObjCIdRedef
3267 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3268 if (Context.ObjCIdRedefinitionType.isNull())
3269 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3272 if (unsigned ObjCClassRedef
3273 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3274 if (Context.ObjCClassRedefinitionType.isNull())
3275 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3278 if (unsigned ObjCSelRedef
3279 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3280 if (Context.ObjCSelRedefinitionType.isNull())
3281 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3284 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3285 QualType Ucontext_tType = GetType(Ucontext_t);
3286 if (Ucontext_tType.isNull()) {
3287 Error("ucontext_t type is NULL");
3291 if (!Context.ucontext_tDecl) {
3292 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3293 Context.setucontext_tDecl(Typedef->getDecl());
3295 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3296 assert(Tag && "Invalid ucontext_t type in AST file");
3297 Context.setucontext_tDecl(Tag->getDecl());
3303 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
3305 // If there were any CUDA special declarations, deserialize them.
3306 if (!CUDASpecialDeclRefs.empty()) {
3307 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
3308 Context.setcudaConfigureCallDecl(
3309 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3312 // Re-export any modules that were imported by a non-module AST file.
3313 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3314 if (Module *Imported = getSubmodule(ImportedModules[I]))
3315 makeModuleVisible(Imported, Module::AllVisible,
3316 /*ImportLoc=*/SourceLocation(),
3317 /*Complain=*/false);
3319 ImportedModules.clear();
3322 void ASTReader::finalizeForWriting() {
3323 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3324 HiddenEnd = HiddenNamesMap.end();
3325 Hidden != HiddenEnd; ++Hidden) {
3326 makeNamesVisible(Hidden->second, Hidden->first);
3328 HiddenNamesMap.clear();
3331 /// \brief Given a cursor at the start of an AST file, scan ahead and drop the
3332 /// cursor into the start of the given block ID, returning false on success and
3333 /// true on failure.
3334 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
3336 llvm::BitstreamEntry Entry = Cursor.advance();
3337 switch (Entry.Kind) {
3338 case llvm::BitstreamEntry::Error:
3339 case llvm::BitstreamEntry::EndBlock:
3342 case llvm::BitstreamEntry::Record:
3343 // Ignore top-level records.
3344 Cursor.skipRecord(Entry.ID);
3347 case llvm::BitstreamEntry::SubBlock:
3348 if (Entry.ID == BlockID) {
3349 if (Cursor.EnterSubBlock(BlockID))
3355 if (Cursor.SkipBlock())
3361 /// \brief Retrieve the name of the original source file name
3362 /// directly from the AST file, without actually loading the AST
3364 std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
3365 FileManager &FileMgr,
3366 DiagnosticsEngine &Diags) {
3367 // Open the AST file.
3369 OwningPtr<llvm::MemoryBuffer> Buffer;
3370 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
3372 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
3373 return std::string();
3376 // Initialize the stream
3377 llvm::BitstreamReader StreamFile;
3378 BitstreamCursor Stream;
3379 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3380 (const unsigned char *)Buffer->getBufferEnd());
3381 Stream.init(StreamFile);
3383 // Sniff for the signature.
3384 if (Stream.Read(8) != 'C' ||
3385 Stream.Read(8) != 'P' ||
3386 Stream.Read(8) != 'C' ||
3387 Stream.Read(8) != 'H') {
3388 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
3389 return std::string();
3392 // Scan for the CONTROL_BLOCK_ID block.
3393 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
3394 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3395 return std::string();
3398 // Scan for ORIGINAL_FILE inside the control block.
3401 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
3402 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3403 return std::string();
3405 if (Entry.Kind != llvm::BitstreamEntry::Record) {
3406 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3407 return std::string();
3412 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
3418 class SimplePCHValidator : public ASTReaderListener {
3419 const LangOptions &ExistingLangOpts;
3420 const TargetOptions &ExistingTargetOpts;
3421 const PreprocessorOptions &ExistingPPOpts;
3422 FileManager &FileMgr;
3425 SimplePCHValidator(const LangOptions &ExistingLangOpts,
3426 const TargetOptions &ExistingTargetOpts,
3427 const PreprocessorOptions &ExistingPPOpts,
3428 FileManager &FileMgr)
3429 : ExistingLangOpts(ExistingLangOpts),
3430 ExistingTargetOpts(ExistingTargetOpts),
3431 ExistingPPOpts(ExistingPPOpts),
3436 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
3438 return checkLanguageOptions(ExistingLangOpts, LangOpts, 0);
3440 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
3442 return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0);
3444 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
3446 std::string &SuggestedPredefines) {
3447 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, 0, FileMgr,
3448 SuggestedPredefines, ExistingLangOpts);
3453 bool ASTReader::readASTFileControlBlock(StringRef Filename,
3454 FileManager &FileMgr,
3455 ASTReaderListener &Listener) {
3456 // Open the AST file.
3458 OwningPtr<llvm::MemoryBuffer> Buffer;
3459 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
3464 // Initialize the stream
3465 llvm::BitstreamReader StreamFile;
3466 BitstreamCursor Stream;
3467 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3468 (const unsigned char *)Buffer->getBufferEnd());
3469 Stream.init(StreamFile);
3471 // Sniff for the signature.
3472 if (Stream.Read(8) != 'C' ||
3473 Stream.Read(8) != 'P' ||
3474 Stream.Read(8) != 'C' ||
3475 Stream.Read(8) != 'H') {
3479 // Scan for the CONTROL_BLOCK_ID block.
3480 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
3483 bool NeedsInputFiles = Listener.needsInputFileVisitation();
3484 BitstreamCursor InputFilesCursor;
3485 if (NeedsInputFiles) {
3486 InputFilesCursor = Stream;
3487 if (SkipCursorToBlock(InputFilesCursor, INPUT_FILES_BLOCK_ID))
3490 // Read the abbreviations
3492 uint64_t Offset = InputFilesCursor.GetCurrentBitNo();
3493 unsigned Code = InputFilesCursor.ReadCode();
3495 // We expect all abbrevs to be at the start of the block.
3496 if (Code != llvm::bitc::DEFINE_ABBREV) {
3497 InputFilesCursor.JumpToBit(Offset);
3500 InputFilesCursor.ReadAbbrevRecord();
3504 // Scan for ORIGINAL_FILE inside the control block.
3507 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
3508 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3511 if (Entry.Kind != llvm::BitstreamEntry::Record)
3516 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
3517 switch ((ControlRecordTypes)RecCode) {
3519 if (Record[0] != VERSION_MAJOR)
3522 if (Listener.ReadFullVersionInformation(Blob))
3527 case LANGUAGE_OPTIONS:
3528 if (ParseLanguageOptions(Record, false, Listener))
3532 case TARGET_OPTIONS:
3533 if (ParseTargetOptions(Record, false, Listener))
3537 case DIAGNOSTIC_OPTIONS:
3538 if (ParseDiagnosticOptions(Record, false, Listener))
3542 case FILE_SYSTEM_OPTIONS:
3543 if (ParseFileSystemOptions(Record, false, Listener))
3547 case HEADER_SEARCH_OPTIONS:
3548 if (ParseHeaderSearchOptions(Record, false, Listener))
3552 case PREPROCESSOR_OPTIONS: {
3553 std::string IgnoredSuggestedPredefines;
3554 if (ParsePreprocessorOptions(Record, false, Listener,
3555 IgnoredSuggestedPredefines))
3560 case INPUT_FILE_OFFSETS: {
3561 if (!NeedsInputFiles)
3564 unsigned NumInputFiles = Record[0];
3565 unsigned NumUserFiles = Record[1];
3566 const uint32_t *InputFileOffs = (const uint32_t *)Blob.data();
3567 for (unsigned I = 0; I != NumInputFiles; ++I) {
3568 // Go find this input file.
3569 bool isSystemFile = I >= NumUserFiles;
3570 BitstreamCursor &Cursor = InputFilesCursor;
3571 SavedStreamPosition SavedPosition(Cursor);
3572 Cursor.JumpToBit(InputFileOffs[I]);
3574 unsigned Code = Cursor.ReadCode();
3577 bool shouldContinue = false;
3578 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
3580 shouldContinue = Listener.visitInputFile(Blob, isSystemFile);
3583 if (!shouldContinue)
3590 // No other validation to perform.
3597 bool ASTReader::isAcceptableASTFile(StringRef Filename,
3598 FileManager &FileMgr,
3599 const LangOptions &LangOpts,
3600 const TargetOptions &TargetOpts,
3601 const PreprocessorOptions &PPOpts) {
3602 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr);
3603 return !readASTFileControlBlock(Filename, FileMgr, validator);
3606 bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
3607 // Enter the submodule block.
3608 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3609 Error("malformed submodule block record in AST file");
3613 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
3615 Module *CurrentModule = 0;
3618 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
3620 switch (Entry.Kind) {
3621 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
3622 case llvm::BitstreamEntry::Error:
3623 Error("malformed block record in AST file");
3625 case llvm::BitstreamEntry::EndBlock:
3627 case llvm::BitstreamEntry::Record:
3628 // The interesting case.
3635 switch (F.Stream.readRecord(Entry.ID, Record, &Blob)) {
3636 default: // Default behavior: ignore.
3639 case SUBMODULE_DEFINITION: {
3641 Error("missing submodule metadata record at beginning of block");
3645 if (Record.size() < 8) {
3646 Error("malformed module definition");
3650 StringRef Name = Blob;
3651 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3652 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3653 bool IsFramework = Record[2];
3654 bool IsExplicit = Record[3];
3655 bool IsSystem = Record[4];
3656 bool InferSubmodules = Record[5];
3657 bool InferExplicitSubmodules = Record[6];
3658 bool InferExportWildcard = Record[7];
3659 bool ConfigMacrosExhaustive = Record[8];
3661 Module *ParentModule = 0;
3663 ParentModule = getSubmodule(Parent);
3665 // Retrieve this (sub)module from the module map, creating it if
3667 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3670 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3671 if (GlobalIndex >= SubmodulesLoaded.size() ||
3672 SubmodulesLoaded[GlobalIndex]) {
3673 Error("too many submodules");
3677 if (!ParentModule) {
3678 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
3679 if (CurFile != F.File) {
3680 if (!Diags.isDiagnosticInFlight()) {
3681 Diag(diag::err_module_file_conflict)
3682 << CurrentModule->getTopLevelModuleName()
3683 << CurFile->getName()
3684 << F.File->getName();
3690 CurrentModule->setASTFile(F.File);
3693 CurrentModule->IsFromModuleFile = true;
3694 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
3695 CurrentModule->InferSubmodules = InferSubmodules;
3696 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3697 CurrentModule->InferExportWildcard = InferExportWildcard;
3698 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
3699 if (DeserializationListener)
3700 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
3702 SubmodulesLoaded[GlobalIndex] = CurrentModule;
3704 // Clear out data that will be replaced by what is the module file.
3705 CurrentModule->LinkLibraries.clear();
3706 CurrentModule->ConfigMacros.clear();
3707 CurrentModule->UnresolvedConflicts.clear();
3708 CurrentModule->Conflicts.clear();
3712 case SUBMODULE_UMBRELLA_HEADER: {
3714 Error("missing submodule metadata record at beginning of block");
3721 if (const FileEntry *Umbrella = PP.getFileManager().getFile(Blob)) {
3722 if (!CurrentModule->getUmbrellaHeader())
3723 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
3724 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
3725 Error("mismatched umbrella headers in submodule");
3732 case SUBMODULE_HEADER: {
3734 Error("missing submodule metadata record at beginning of block");
3741 // We lazily associate headers with their modules via the HeaderInfoTable.
3742 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
3743 // of complete filenames or remove it entirely.
3747 case SUBMODULE_EXCLUDED_HEADER: {
3749 Error("missing submodule metadata record at beginning of block");
3756 // We lazily associate headers with their modules via the HeaderInfoTable.
3757 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
3758 // of complete filenames or remove it entirely.
3762 case SUBMODULE_TOPHEADER: {
3764 Error("missing submodule metadata record at beginning of block");
3771 CurrentModule->addTopHeaderFilename(Blob);
3775 case SUBMODULE_UMBRELLA_DIR: {
3777 Error("missing submodule metadata record at beginning of block");
3784 if (const DirectoryEntry *Umbrella
3785 = PP.getFileManager().getDirectory(Blob)) {
3786 if (!CurrentModule->getUmbrellaDir())
3787 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3788 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3789 Error("mismatched umbrella directories in submodule");
3796 case SUBMODULE_METADATA: {
3798 Error("submodule metadata record not at beginning of block");
3803 F.BaseSubmoduleID = getTotalNumSubmodules();
3804 F.LocalNumSubmodules = Record[0];
3805 unsigned LocalBaseSubmoduleID = Record[1];
3806 if (F.LocalNumSubmodules > 0) {
3807 // Introduce the global -> local mapping for submodules within this
3809 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3811 // Introduce the local -> global mapping for submodules within this
3813 F.SubmoduleRemap.insertOrReplace(
3814 std::make_pair(LocalBaseSubmoduleID,
3815 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3817 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3822 case SUBMODULE_IMPORTS: {
3824 Error("missing submodule metadata record at beginning of block");
3831 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
3832 UnresolvedModuleRef Unresolved;
3833 Unresolved.File = &F;
3834 Unresolved.Mod = CurrentModule;
3835 Unresolved.ID = Record[Idx];
3836 Unresolved.Kind = UnresolvedModuleRef::Import;
3837 Unresolved.IsWildcard = false;
3838 UnresolvedModuleRefs.push_back(Unresolved);
3843 case SUBMODULE_EXPORTS: {
3845 Error("missing submodule metadata record at beginning of block");
3852 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
3853 UnresolvedModuleRef Unresolved;
3854 Unresolved.File = &F;
3855 Unresolved.Mod = CurrentModule;
3856 Unresolved.ID = Record[Idx];
3857 Unresolved.Kind = UnresolvedModuleRef::Export;
3858 Unresolved.IsWildcard = Record[Idx + 1];
3859 UnresolvedModuleRefs.push_back(Unresolved);
3862 // Once we've loaded the set of exports, there's no reason to keep
3863 // the parsed, unresolved exports around.
3864 CurrentModule->UnresolvedExports.clear();
3867 case SUBMODULE_REQUIRES: {
3869 Error("missing submodule metadata record at beginning of block");
3876 CurrentModule->addRequirement(Blob, Context.getLangOpts(),
3877 Context.getTargetInfo());
3881 case SUBMODULE_LINK_LIBRARY:
3883 Error("missing submodule metadata record at beginning of block");
3890 CurrentModule->LinkLibraries.push_back(
3891 Module::LinkLibrary(Blob, Record[0]));
3894 case SUBMODULE_CONFIG_MACRO:
3896 Error("missing submodule metadata record at beginning of block");
3903 CurrentModule->ConfigMacros.push_back(Blob.str());
3906 case SUBMODULE_CONFLICT: {
3908 Error("missing submodule metadata record at beginning of block");
3915 UnresolvedModuleRef Unresolved;
3916 Unresolved.File = &F;
3917 Unresolved.Mod = CurrentModule;
3918 Unresolved.ID = Record[0];
3919 Unresolved.Kind = UnresolvedModuleRef::Conflict;
3920 Unresolved.IsWildcard = false;
3921 Unresolved.String = Blob;
3922 UnresolvedModuleRefs.push_back(Unresolved);
3929 /// \brief Parse the record that corresponds to a LangOptions data
3932 /// This routine parses the language options from the AST file and then gives
3933 /// them to the AST listener if one is set.
3935 /// \returns true if the listener deems the file unacceptable, false otherwise.
3936 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
3938 ASTReaderListener &Listener) {
3939 LangOptions LangOpts;
3941 #define LANGOPT(Name, Bits, Default, Description) \
3942 LangOpts.Name = Record[Idx++];
3943 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3944 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3945 #include "clang/Basic/LangOptions.def"
3946 #define SANITIZER(NAME, ID) LangOpts.Sanitize.ID = Record[Idx++];
3947 #include "clang/Basic/Sanitizers.def"
3949 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3950 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3951 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
3953 unsigned Length = Record[Idx++];
3954 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3955 Record.begin() + Idx + Length);
3960 for (unsigned N = Record[Idx++]; N; --N) {
3961 LangOpts.CommentOpts.BlockCommandNames.push_back(
3962 ReadString(Record, Idx));
3964 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
3966 return Listener.ReadLanguageOptions(LangOpts, Complain);
3969 bool ASTReader::ParseTargetOptions(const RecordData &Record,
3971 ASTReaderListener &Listener) {
3973 TargetOptions TargetOpts;
3974 TargetOpts.Triple = ReadString(Record, Idx);
3975 TargetOpts.CPU = ReadString(Record, Idx);
3976 TargetOpts.ABI = ReadString(Record, Idx);
3977 TargetOpts.CXXABI = ReadString(Record, Idx);
3978 TargetOpts.LinkerVersion = ReadString(Record, Idx);
3979 for (unsigned N = Record[Idx++]; N; --N) {
3980 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
3982 for (unsigned N = Record[Idx++]; N; --N) {
3983 TargetOpts.Features.push_back(ReadString(Record, Idx));
3986 return Listener.ReadTargetOptions(TargetOpts, Complain);
3989 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
3990 ASTReaderListener &Listener) {
3991 DiagnosticOptions DiagOpts;
3993 #define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
3994 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
3995 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
3996 #include "clang/Basic/DiagnosticOptions.def"
3998 for (unsigned N = Record[Idx++]; N; --N) {
3999 DiagOpts.Warnings.push_back(ReadString(Record, Idx));
4002 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
4005 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
4006 ASTReaderListener &Listener) {
4007 FileSystemOptions FSOpts;
4009 FSOpts.WorkingDir = ReadString(Record, Idx);
4010 return Listener.ReadFileSystemOptions(FSOpts, Complain);
4013 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
4015 ASTReaderListener &Listener) {
4016 HeaderSearchOptions HSOpts;
4018 HSOpts.Sysroot = ReadString(Record, Idx);
4021 for (unsigned N = Record[Idx++]; N; --N) {
4022 std::string Path = ReadString(Record, Idx);
4023 frontend::IncludeDirGroup Group
4024 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
4025 bool IsFramework = Record[Idx++];
4026 bool IgnoreSysRoot = Record[Idx++];
4027 HSOpts.UserEntries.push_back(
4028 HeaderSearchOptions::Entry(Path, Group, IsFramework, IgnoreSysRoot));
4031 // System header prefixes.
4032 for (unsigned N = Record[Idx++]; N; --N) {
4033 std::string Prefix = ReadString(Record, Idx);
4034 bool IsSystemHeader = Record[Idx++];
4035 HSOpts.SystemHeaderPrefixes.push_back(
4036 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
4039 HSOpts.ResourceDir = ReadString(Record, Idx);
4040 HSOpts.ModuleCachePath = ReadString(Record, Idx);
4041 HSOpts.DisableModuleHash = Record[Idx++];
4042 HSOpts.UseBuiltinIncludes = Record[Idx++];
4043 HSOpts.UseStandardSystemIncludes = Record[Idx++];
4044 HSOpts.UseStandardCXXIncludes = Record[Idx++];
4045 HSOpts.UseLibcxx = Record[Idx++];
4047 return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
4050 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
4052 ASTReaderListener &Listener,
4053 std::string &SuggestedPredefines) {
4054 PreprocessorOptions PPOpts;
4057 // Macro definitions/undefs
4058 for (unsigned N = Record[Idx++]; N; --N) {
4059 std::string Macro = ReadString(Record, Idx);
4060 bool IsUndef = Record[Idx++];
4061 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
4065 for (unsigned N = Record[Idx++]; N; --N) {
4066 PPOpts.Includes.push_back(ReadString(Record, Idx));
4070 for (unsigned N = Record[Idx++]; N; --N) {
4071 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
4074 PPOpts.UsePredefines = Record[Idx++];
4075 PPOpts.DetailedRecord = Record[Idx++];
4076 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
4077 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
4078 PPOpts.ObjCXXARCStandardLibrary =
4079 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
4080 SuggestedPredefines.clear();
4081 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
4082 SuggestedPredefines);
4085 std::pair<ModuleFile *, unsigned>
4086 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
4087 GlobalPreprocessedEntityMapType::iterator
4088 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
4089 assert(I != GlobalPreprocessedEntityMap.end() &&
4090 "Corrupted global preprocessed entity map");
4091 ModuleFile *M = I->second;
4092 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
4093 return std::make_pair(M, LocalIndex);
4096 std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
4097 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
4098 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
4099 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
4100 Mod.NumPreprocessedEntities);
4102 return std::make_pair(PreprocessingRecord::iterator(),
4103 PreprocessingRecord::iterator());
4106 std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
4107 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
4108 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4109 ModuleDeclIterator(this, &Mod,
4110 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
4113 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4114 PreprocessedEntityID PPID = Index+1;
4115 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4116 ModuleFile &M = *PPInfo.first;
4117 unsigned LocalIndex = PPInfo.second;
4118 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4120 if (!PP.getPreprocessingRecord()) {
4121 Error("no preprocessing record");
4125 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
4126 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
4128 llvm::BitstreamEntry Entry =
4129 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
4130 if (Entry.Kind != llvm::BitstreamEntry::Record)
4134 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
4135 ReadSourceLocation(M, PPOffs.End));
4136 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
4139 PreprocessorDetailRecordTypes RecType =
4140 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4141 Entry.ID, Record, &Blob);
4143 case PPD_MACRO_EXPANSION: {
4144 bool isBuiltin = Record[0];
4145 IdentifierInfo *Name = 0;
4146 MacroDefinition *Def = 0;
4148 Name = getLocalIdentifier(M, Record[1]);
4150 PreprocessedEntityID
4151 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
4152 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
4157 ME = new (PPRec) MacroExpansion(Name, Range);
4159 ME = new (PPRec) MacroExpansion(Def, Range);
4164 case PPD_MACRO_DEFINITION: {
4165 // Decode the identifier info and then check again; if the macro is
4166 // still defined and associated with the identifier,
4167 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
4169 = new (PPRec) MacroDefinition(II, Range);
4171 if (DeserializationListener)
4172 DeserializationListener->MacroDefinitionRead(PPID, MD);
4177 case PPD_INCLUSION_DIRECTIVE: {
4178 const char *FullFileNameStart = Blob.data() + Record[0];
4179 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
4180 const FileEntry *File = 0;
4181 if (!FullFileName.empty())
4182 File = PP.getFileManager().getFile(FullFileName);
4184 // FIXME: Stable encoding
4185 InclusionDirective::InclusionKind Kind
4186 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4187 InclusionDirective *ID
4188 = new (PPRec) InclusionDirective(PPRec, Kind,
4189 StringRef(Blob.data(), Record[0]),
4190 Record[1], Record[3],
4197 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4200 /// \brief \arg SLocMapI points at a chunk of a module that contains no
4201 /// preprocessed entities or the entities it contains are not the ones we are
4202 /// looking for. Find the next module that contains entities and return the ID
4203 /// of the first entry.
4204 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4205 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4207 for (GlobalSLocOffsetMapType::const_iterator
4208 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4209 ModuleFile &M = *SLocMapI->second;
4210 if (M.NumPreprocessedEntities)
4211 return M.BasePreprocessedEntityID;
4214 return getTotalNumPreprocessedEntities();
4219 template <unsigned PPEntityOffset::*PPLoc>
4220 struct PPEntityComp {
4221 const ASTReader &Reader;
4224 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
4226 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4227 SourceLocation LHS = getLoc(L);
4228 SourceLocation RHS = getLoc(R);
4229 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4232 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
4233 SourceLocation LHS = getLoc(L);
4234 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4237 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
4238 SourceLocation RHS = getLoc(R);
4239 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4242 SourceLocation getLoc(const PPEntityOffset &PPE) const {
4243 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
4249 /// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
4250 PreprocessedEntityID
4251 ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
4252 if (SourceMgr.isLocalSourceLocation(BLoc))
4253 return getTotalNumPreprocessedEntities();
4255 GlobalSLocOffsetMapType::const_iterator
4256 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
4257 BLoc.getOffset() - 1);
4258 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4259 "Corrupted global sloc offset map");
4261 if (SLocMapI->second->NumPreprocessedEntities == 0)
4262 return findNextPreprocessedEntity(SLocMapI);
4264 ModuleFile &M = *SLocMapI->second;
4265 typedef const PPEntityOffset *pp_iterator;
4266 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4267 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4269 size_t Count = M.NumPreprocessedEntities;
4271 pp_iterator First = pp_begin;
4274 // Do a binary search manually instead of using std::lower_bound because
4275 // The end locations of entities may be unordered (when a macro expansion
4276 // is inside another macro argument), but for this case it is not important
4277 // whether we get the first macro expansion or its containing macro.
4281 std::advance(PPI, Half);
4282 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
4286 Count = Count - Half - 1;
4292 return findNextPreprocessedEntity(SLocMapI);
4294 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4297 /// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
4298 PreprocessedEntityID
4299 ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
4300 if (SourceMgr.isLocalSourceLocation(ELoc))
4301 return getTotalNumPreprocessedEntities();
4303 GlobalSLocOffsetMapType::const_iterator
4304 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
4305 ELoc.getOffset() - 1);
4306 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4307 "Corrupted global sloc offset map");
4309 if (SLocMapI->second->NumPreprocessedEntities == 0)
4310 return findNextPreprocessedEntity(SLocMapI);
4312 ModuleFile &M = *SLocMapI->second;
4313 typedef const PPEntityOffset *pp_iterator;
4314 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4315 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4317 std::upper_bound(pp_begin, pp_end, ELoc,
4318 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
4321 return findNextPreprocessedEntity(SLocMapI);
4323 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4326 /// \brief Returns a pair of [Begin, End) indices of preallocated
4327 /// preprocessed entities that \arg Range encompasses.
4328 std::pair<unsigned, unsigned>
4329 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
4330 if (Range.isInvalid())
4331 return std::make_pair(0,0);
4332 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
4334 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
4335 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
4336 return std::make_pair(BeginID, EndID);
4339 /// \brief Optionally returns true or false if the preallocated preprocessed
4340 /// entity with index \arg Index came from file \arg FID.
4341 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
4343 if (FID.isInvalid())
4346 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4347 ModuleFile &M = *PPInfo.first;
4348 unsigned LocalIndex = PPInfo.second;
4349 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4351 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
4352 if (Loc.isInvalid())
4355 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
4362 /// \brief Visitor used to search for information about a header file.
4363 class HeaderFileInfoVisitor {
4364 const FileEntry *FE;
4366 Optional<HeaderFileInfo> HFI;
4369 explicit HeaderFileInfoVisitor(const FileEntry *FE)
4372 static bool visit(ModuleFile &M, void *UserData) {
4373 HeaderFileInfoVisitor *This
4374 = static_cast<HeaderFileInfoVisitor *>(UserData);
4376 HeaderFileInfoLookupTable *Table
4377 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4381 // Look in the on-disk hash table for an entry for this file name.
4382 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE);
4383 if (Pos == Table->end())
4390 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
4394 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
4395 HeaderFileInfoVisitor Visitor(FE);
4396 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
4397 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
4399 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
4403 return HeaderFileInfo();
4406 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
4407 // FIXME: Make it work properly with modules.
4408 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
4409 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
4410 ModuleFile &F = *(*I);
4413 assert(!Diag.DiagStates.empty());
4414 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
4415 while (Idx < F.PragmaDiagMappings.size()) {
4416 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
4417 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
4418 if (DiagStateID != 0) {
4419 Diag.DiagStatePoints.push_back(
4420 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
4421 FullSourceLoc(Loc, SourceMgr)));
4425 assert(DiagStateID == 0);
4426 // A new DiagState was created here.
4427 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
4428 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
4429 DiagStates.push_back(NewState);
4430 Diag.DiagStatePoints.push_back(
4431 DiagnosticsEngine::DiagStatePoint(NewState,
4432 FullSourceLoc(Loc, SourceMgr)));
4434 assert(Idx < F.PragmaDiagMappings.size() &&
4435 "Invalid data, didn't find '-1' marking end of diag/map pairs");
4436 if (Idx >= F.PragmaDiagMappings.size()) {
4437 break; // Something is messed up but at least avoid infinite loop in
4440 unsigned DiagID = F.PragmaDiagMappings[Idx++];
4441 if (DiagID == (unsigned)-1) {
4442 break; // no more diag/map pairs for this location.
4444 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
4445 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
4446 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
4452 /// \brief Get the correct cursor and offset for loading a type.
4453 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
4454 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
4455 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
4456 ModuleFile *M = I->second;
4457 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
4460 /// \brief Read and return the type with the given index..
4462 /// The index is the type ID, shifted and minus the number of predefs. This
4463 /// routine actually reads the record corresponding to the type at the given
4464 /// location. It is a helper routine for GetType, which deals with reading type
4466 QualType ASTReader::readTypeRecord(unsigned Index) {
4467 RecordLocation Loc = TypeCursorForIndex(Index);
4468 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
4470 // Keep track of where we are in the stream, then jump back there
4471 // after reading this type.
4472 SavedStreamPosition SavedPosition(DeclsCursor);
4474 ReadingKindTracker ReadingKind(Read_Type, *this);
4476 // Note that we are loading a type record.
4477 Deserializing AType(this);
4480 DeclsCursor.JumpToBit(Loc.Offset);
4482 unsigned Code = DeclsCursor.ReadCode();
4483 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
4484 case TYPE_EXT_QUAL: {
4485 if (Record.size() != 2) {
4486 Error("Incorrect encoding of extended qualifier type");
4489 QualType Base = readType(*Loc.F, Record, Idx);
4490 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
4491 return Context.getQualifiedType(Base, Quals);
4494 case TYPE_COMPLEX: {
4495 if (Record.size() != 1) {
4496 Error("Incorrect encoding of complex type");
4499 QualType ElemType = readType(*Loc.F, Record, Idx);
4500 return Context.getComplexType(ElemType);
4503 case TYPE_POINTER: {
4504 if (Record.size() != 1) {
4505 Error("Incorrect encoding of pointer type");
4508 QualType PointeeType = readType(*Loc.F, Record, Idx);
4509 return Context.getPointerType(PointeeType);
4512 case TYPE_BLOCK_POINTER: {
4513 if (Record.size() != 1) {
4514 Error("Incorrect encoding of block pointer type");
4517 QualType PointeeType = readType(*Loc.F, Record, Idx);
4518 return Context.getBlockPointerType(PointeeType);
4521 case TYPE_LVALUE_REFERENCE: {
4522 if (Record.size() != 2) {
4523 Error("Incorrect encoding of lvalue reference type");
4526 QualType PointeeType = readType(*Loc.F, Record, Idx);
4527 return Context.getLValueReferenceType(PointeeType, Record[1]);
4530 case TYPE_RVALUE_REFERENCE: {
4531 if (Record.size() != 1) {
4532 Error("Incorrect encoding of rvalue reference type");
4535 QualType PointeeType = readType(*Loc.F, Record, Idx);
4536 return Context.getRValueReferenceType(PointeeType);
4539 case TYPE_MEMBER_POINTER: {
4540 if (Record.size() != 2) {
4541 Error("Incorrect encoding of member pointer type");
4544 QualType PointeeType = readType(*Loc.F, Record, Idx);
4545 QualType ClassType = readType(*Loc.F, Record, Idx);
4546 if (PointeeType.isNull() || ClassType.isNull())
4549 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
4552 case TYPE_CONSTANT_ARRAY: {
4553 QualType ElementType = readType(*Loc.F, Record, Idx);
4554 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4555 unsigned IndexTypeQuals = Record[2];
4557 llvm::APInt Size = ReadAPInt(Record, Idx);
4558 return Context.getConstantArrayType(ElementType, Size,
4559 ASM, IndexTypeQuals);
4562 case TYPE_INCOMPLETE_ARRAY: {
4563 QualType ElementType = readType(*Loc.F, Record, Idx);
4564 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4565 unsigned IndexTypeQuals = Record[2];
4566 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
4569 case TYPE_VARIABLE_ARRAY: {
4570 QualType ElementType = readType(*Loc.F, Record, Idx);
4571 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4572 unsigned IndexTypeQuals = Record[2];
4573 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
4574 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
4575 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
4576 ASM, IndexTypeQuals,
4577 SourceRange(LBLoc, RBLoc));
4581 if (Record.size() != 3) {
4582 Error("incorrect encoding of vector type in AST file");
4586 QualType ElementType = readType(*Loc.F, Record, Idx);
4587 unsigned NumElements = Record[1];
4588 unsigned VecKind = Record[2];
4589 return Context.getVectorType(ElementType, NumElements,
4590 (VectorType::VectorKind)VecKind);
4593 case TYPE_EXT_VECTOR: {
4594 if (Record.size() != 3) {
4595 Error("incorrect encoding of extended vector type in AST file");
4599 QualType ElementType = readType(*Loc.F, Record, Idx);
4600 unsigned NumElements = Record[1];
4601 return Context.getExtVectorType(ElementType, NumElements);
4604 case TYPE_FUNCTION_NO_PROTO: {
4605 if (Record.size() != 6) {
4606 Error("incorrect encoding of no-proto function type");
4609 QualType ResultType = readType(*Loc.F, Record, Idx);
4610 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
4611 (CallingConv)Record[4], Record[5]);
4612 return Context.getFunctionNoProtoType(ResultType, Info);
4615 case TYPE_FUNCTION_PROTO: {
4616 QualType ResultType = readType(*Loc.F, Record, Idx);
4618 FunctionProtoType::ExtProtoInfo EPI;
4619 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
4620 /*hasregparm*/ Record[2],
4621 /*regparm*/ Record[3],
4622 static_cast<CallingConv>(Record[4]),
4623 /*produces*/ Record[5]);
4626 unsigned NumParams = Record[Idx++];
4627 SmallVector<QualType, 16> ParamTypes;
4628 for (unsigned I = 0; I != NumParams; ++I)
4629 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
4631 EPI.Variadic = Record[Idx++];
4632 EPI.HasTrailingReturn = Record[Idx++];
4633 EPI.TypeQuals = Record[Idx++];
4634 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
4635 ExceptionSpecificationType EST =
4636 static_cast<ExceptionSpecificationType>(Record[Idx++]);
4637 EPI.ExceptionSpecType = EST;
4638 SmallVector<QualType, 2> Exceptions;
4639 if (EST == EST_Dynamic) {
4640 EPI.NumExceptions = Record[Idx++];
4641 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
4642 Exceptions.push_back(readType(*Loc.F, Record, Idx));
4643 EPI.Exceptions = Exceptions.data();
4644 } else if (EST == EST_ComputedNoexcept) {
4645 EPI.NoexceptExpr = ReadExpr(*Loc.F);
4646 } else if (EST == EST_Uninstantiated) {
4647 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4648 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4649 } else if (EST == EST_Unevaluated) {
4650 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4652 return Context.getFunctionType(ResultType, ParamTypes, EPI);
4655 case TYPE_UNRESOLVED_USING: {
4657 return Context.getTypeDeclType(
4658 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
4661 case TYPE_TYPEDEF: {
4662 if (Record.size() != 2) {
4663 Error("incorrect encoding of typedef type");
4667 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
4668 QualType Canonical = readType(*Loc.F, Record, Idx);
4669 if (!Canonical.isNull())
4670 Canonical = Context.getCanonicalType(Canonical);
4671 return Context.getTypedefType(Decl, Canonical);
4674 case TYPE_TYPEOF_EXPR:
4675 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
4678 if (Record.size() != 1) {
4679 Error("incorrect encoding of typeof(type) in AST file");
4682 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4683 return Context.getTypeOfType(UnderlyingType);
4686 case TYPE_DECLTYPE: {
4687 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4688 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4691 case TYPE_UNARY_TRANSFORM: {
4692 QualType BaseType = readType(*Loc.F, Record, Idx);
4693 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4694 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
4695 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
4699 QualType Deduced = readType(*Loc.F, Record, Idx);
4700 bool IsDecltypeAuto = Record[Idx++];
4701 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
4702 return Context.getAutoType(Deduced, IsDecltypeAuto, IsDependent);
4706 if (Record.size() != 2) {
4707 Error("incorrect encoding of record type");
4711 bool IsDependent = Record[Idx++];
4712 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4713 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4714 QualType T = Context.getRecordType(RD);
4715 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4720 if (Record.size() != 2) {
4721 Error("incorrect encoding of enum type");
4725 bool IsDependent = Record[Idx++];
4727 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
4728 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4732 case TYPE_ATTRIBUTED: {
4733 if (Record.size() != 3) {
4734 Error("incorrect encoding of attributed type");
4737 QualType modifiedType = readType(*Loc.F, Record, Idx);
4738 QualType equivalentType = readType(*Loc.F, Record, Idx);
4739 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
4740 return Context.getAttributedType(kind, modifiedType, equivalentType);
4744 if (Record.size() != 1) {
4745 Error("incorrect encoding of paren type");
4748 QualType InnerType = readType(*Loc.F, Record, Idx);
4749 return Context.getParenType(InnerType);
4752 case TYPE_PACK_EXPANSION: {
4753 if (Record.size() != 2) {
4754 Error("incorrect encoding of pack expansion type");
4757 QualType Pattern = readType(*Loc.F, Record, Idx);
4758 if (Pattern.isNull())
4760 Optional<unsigned> NumExpansions;
4762 NumExpansions = Record[1] - 1;
4763 return Context.getPackExpansionType(Pattern, NumExpansions);
4766 case TYPE_ELABORATED: {
4768 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4769 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4770 QualType NamedType = readType(*Loc.F, Record, Idx);
4771 return Context.getElaboratedType(Keyword, NNS, NamedType);
4774 case TYPE_OBJC_INTERFACE: {
4776 ObjCInterfaceDecl *ItfD
4777 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
4778 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
4781 case TYPE_OBJC_OBJECT: {
4783 QualType Base = readType(*Loc.F, Record, Idx);
4784 unsigned NumProtos = Record[Idx++];
4785 SmallVector<ObjCProtocolDecl*, 4> Protos;
4786 for (unsigned I = 0; I != NumProtos; ++I)
4787 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
4788 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
4791 case TYPE_OBJC_OBJECT_POINTER: {
4793 QualType Pointee = readType(*Loc.F, Record, Idx);
4794 return Context.getObjCObjectPointerType(Pointee);
4797 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
4799 QualType Parm = readType(*Loc.F, Record, Idx);
4800 QualType Replacement = readType(*Loc.F, Record, Idx);
4802 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
4806 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4808 QualType Parm = readType(*Loc.F, Record, Idx);
4809 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
4810 return Context.getSubstTemplateTypeParmPackType(
4811 cast<TemplateTypeParmType>(Parm),
4815 case TYPE_INJECTED_CLASS_NAME: {
4816 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
4817 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
4818 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
4819 // for AST reading, too much interdependencies.
4821 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
4824 case TYPE_TEMPLATE_TYPE_PARM: {
4826 unsigned Depth = Record[Idx++];
4827 unsigned Index = Record[Idx++];
4828 bool Pack = Record[Idx++];
4829 TemplateTypeParmDecl *D
4830 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
4831 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
4834 case TYPE_DEPENDENT_NAME: {
4836 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4837 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4838 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4839 QualType Canon = readType(*Loc.F, Record, Idx);
4840 if (!Canon.isNull())
4841 Canon = Context.getCanonicalType(Canon);
4842 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
4845 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
4847 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4848 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4849 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4850 unsigned NumArgs = Record[Idx++];
4851 SmallVector<TemplateArgument, 8> Args;
4852 Args.reserve(NumArgs);
4854 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
4855 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
4856 Args.size(), Args.data());
4859 case TYPE_DEPENDENT_SIZED_ARRAY: {
4863 QualType ElementType = readType(*Loc.F, Record, Idx);
4864 ArrayType::ArraySizeModifier ASM
4865 = (ArrayType::ArraySizeModifier)Record[Idx++];
4866 unsigned IndexTypeQuals = Record[Idx++];
4868 // DependentSizedArrayType
4869 Expr *NumElts = ReadExpr(*Loc.F);
4870 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
4872 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
4873 IndexTypeQuals, Brackets);
4876 case TYPE_TEMPLATE_SPECIALIZATION: {
4878 bool IsDependent = Record[Idx++];
4879 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
4880 SmallVector<TemplateArgument, 8> Args;
4881 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
4882 QualType Underlying = readType(*Loc.F, Record, Idx);
4884 if (Underlying.isNull())
4885 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
4888 T = Context.getTemplateSpecializationType(Name, Args.data(),
4889 Args.size(), Underlying);
4890 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4895 if (Record.size() != 1) {
4896 Error("Incorrect encoding of atomic type");
4899 QualType ValueType = readType(*Loc.F, Record, Idx);
4900 return Context.getAtomicType(ValueType);
4903 llvm_unreachable("Invalid TypeCode!");
4906 class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
4909 const ASTReader::RecordData &Record;
4912 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4914 return Reader.ReadSourceLocation(F, R, I);
4917 template<typename T>
4918 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4919 return Reader.ReadDeclAs<T>(F, Record, Idx);
4923 TypeLocReader(ASTReader &Reader, ModuleFile &F,
4924 const ASTReader::RecordData &Record, unsigned &Idx)
4925 : Reader(Reader), F(F), Record(Record), Idx(Idx)
4928 // We want compile-time assurance that we've enumerated all of
4929 // these, so unfortunately we have to declare them first, then
4930 // define them out-of-line.
4931 #define ABSTRACT_TYPELOC(CLASS, PARENT)
4932 #define TYPELOC(CLASS, PARENT) \
4933 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
4934 #include "clang/AST/TypeLocNodes.def"
4936 void VisitFunctionTypeLoc(FunctionTypeLoc);
4937 void VisitArrayTypeLoc(ArrayTypeLoc);
4940 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
4943 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
4944 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
4945 if (TL.needsExtraLocalData()) {
4946 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4947 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4948 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4949 TL.setModeAttr(Record[Idx++]);
4952 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
4953 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4955 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
4956 TL.setStarLoc(ReadSourceLocation(Record, Idx));
4958 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
4959 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
4961 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
4962 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
4964 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
4965 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
4967 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
4968 TL.setStarLoc(ReadSourceLocation(Record, Idx));
4969 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4971 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
4972 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4973 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
4975 TL.setSizeExpr(Reader.ReadExpr(F));
4979 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4980 VisitArrayTypeLoc(TL);
4982 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4983 VisitArrayTypeLoc(TL);
4985 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4986 VisitArrayTypeLoc(TL);
4988 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4989 DependentSizedArrayTypeLoc TL) {
4990 VisitArrayTypeLoc(TL);
4992 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4993 DependentSizedExtVectorTypeLoc TL) {
4994 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4996 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
4997 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4999 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
5000 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5002 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
5003 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
5004 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5005 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5006 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
5007 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
5008 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
5011 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
5012 VisitFunctionTypeLoc(TL);
5014 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
5015 VisitFunctionTypeLoc(TL);
5017 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
5018 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5020 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
5021 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5023 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
5024 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5025 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5026 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5028 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
5029 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5030 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5031 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5032 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5034 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
5035 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5037 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
5038 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5039 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5040 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5041 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5043 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
5044 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5046 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
5047 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5049 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
5050 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5052 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
5053 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
5054 if (TL.hasAttrOperand()) {
5056 range.setBegin(ReadSourceLocation(Record, Idx));
5057 range.setEnd(ReadSourceLocation(Record, Idx));
5058 TL.setAttrOperandParensRange(range);
5060 if (TL.hasAttrExprOperand()) {
5062 TL.setAttrExprOperand(Reader.ReadExpr(F));
5064 TL.setAttrExprOperand(0);
5065 } else if (TL.hasAttrEnumOperand())
5066 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
5068 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
5069 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5071 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
5072 SubstTemplateTypeParmTypeLoc TL) {
5073 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5075 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
5076 SubstTemplateTypeParmPackTypeLoc TL) {
5077 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5079 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
5080 TemplateSpecializationTypeLoc TL) {
5081 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5082 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5083 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5084 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5085 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
5087 Reader.GetTemplateArgumentLocInfo(F,
5088 TL.getTypePtr()->getArg(i).getKind(),
5091 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
5092 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5093 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5095 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
5096 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5097 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5099 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
5100 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5102 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
5103 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5104 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5105 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5107 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5108 DependentTemplateSpecializationTypeLoc TL) {
5109 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5110 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5111 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5112 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5113 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5114 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5115 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5117 Reader.GetTemplateArgumentLocInfo(F,
5118 TL.getTypePtr()->getArg(I).getKind(),
5121 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5122 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5124 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
5125 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5127 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5128 TL.setHasBaseTypeAsWritten(Record[Idx++]);
5129 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5130 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5131 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
5132 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
5134 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
5135 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5137 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5138 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5139 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5140 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5143 TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5144 const RecordData &Record,
5146 QualType InfoTy = readType(F, Record, Idx);
5147 if (InfoTy.isNull())
5150 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5151 TypeLocReader TLR(*this, F, Record, Idx);
5152 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5157 QualType ASTReader::GetType(TypeID ID) {
5158 unsigned FastQuals = ID & Qualifiers::FastMask;
5159 unsigned Index = ID >> Qualifiers::FastWidth;
5161 if (Index < NUM_PREDEF_TYPE_IDS) {
5163 switch ((PredefinedTypeIDs)Index) {
5164 case PREDEF_TYPE_NULL_ID: return QualType();
5165 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
5166 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
5168 case PREDEF_TYPE_CHAR_U_ID:
5169 case PREDEF_TYPE_CHAR_S_ID:
5170 // FIXME: Check that the signedness of CharTy is correct!
5174 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
5175 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
5176 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
5177 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
5178 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
5179 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
5180 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
5181 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
5182 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
5183 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
5184 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
5185 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
5186 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
5187 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
5188 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
5189 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
5190 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
5191 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
5192 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
5193 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
5194 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
5195 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
5196 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
5197 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
5198 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
5199 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
5200 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
5201 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
5202 case PREDEF_TYPE_IMAGE1D_ID: T = Context.OCLImage1dTy; break;
5203 case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break;
5204 case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break;
5205 case PREDEF_TYPE_IMAGE2D_ID: T = Context.OCLImage2dTy; break;
5206 case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break;
5207 case PREDEF_TYPE_IMAGE3D_ID: T = Context.OCLImage3dTy; break;
5208 case PREDEF_TYPE_SAMPLER_ID: T = Context.OCLSamplerTy; break;
5209 case PREDEF_TYPE_EVENT_ID: T = Context.OCLEventTy; break;
5210 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
5212 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
5213 T = Context.getAutoRRefDeductType();
5216 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
5217 T = Context.ARCUnbridgedCastTy;
5220 case PREDEF_TYPE_VA_LIST_TAG:
5221 T = Context.getVaListTagType();
5224 case PREDEF_TYPE_BUILTIN_FN:
5225 T = Context.BuiltinFnTy;
5229 assert(!T.isNull() && "Unknown predefined type");
5230 return T.withFastQualifiers(FastQuals);
5233 Index -= NUM_PREDEF_TYPE_IDS;
5234 assert(Index < TypesLoaded.size() && "Type index out-of-range");
5235 if (TypesLoaded[Index].isNull()) {
5236 TypesLoaded[Index] = readTypeRecord(Index);
5237 if (TypesLoaded[Index].isNull())
5240 TypesLoaded[Index]->setFromAST();
5241 if (DeserializationListener)
5242 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
5243 TypesLoaded[Index]);
5246 return TypesLoaded[Index].withFastQualifiers(FastQuals);
5249 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
5250 return GetType(getGlobalTypeID(F, LocalID));
5253 serialization::TypeID
5254 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
5255 unsigned FastQuals = LocalID & Qualifiers::FastMask;
5256 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
5258 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
5261 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5262 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
5263 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
5265 unsigned GlobalIndex = LocalIndex + I->second;
5266 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
5269 TemplateArgumentLocInfo
5270 ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
5271 TemplateArgument::ArgKind Kind,
5272 const RecordData &Record,
5275 case TemplateArgument::Expression:
5277 case TemplateArgument::Type:
5278 return GetTypeSourceInfo(F, Record, Index);
5279 case TemplateArgument::Template: {
5280 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5282 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5283 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5286 case TemplateArgument::TemplateExpansion: {
5287 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5289 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5290 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
5291 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5294 case TemplateArgument::Null:
5295 case TemplateArgument::Integral:
5296 case TemplateArgument::Declaration:
5297 case TemplateArgument::NullPtr:
5298 case TemplateArgument::Pack:
5299 // FIXME: Is this right?
5300 return TemplateArgumentLocInfo();
5302 llvm_unreachable("unexpected template argument loc");
5306 ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
5307 const RecordData &Record, unsigned &Index) {
5308 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
5310 if (Arg.getKind() == TemplateArgument::Expression) {
5311 if (Record[Index++]) // bool InfoHasSameExpr.
5312 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
5314 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
5318 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
5322 uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
5324 if (Idx >= Record.size())
5327 unsigned LocalID = Record[Idx++];
5328 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
5331 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
5332 RecordLocation Loc = getLocalBitOffset(Offset);
5333 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
5334 SavedStreamPosition SavedPosition(Cursor);
5335 Cursor.JumpToBit(Loc.Offset);
5336 ReadingKindTracker ReadingKind(Read_Decl, *this);
5338 unsigned Code = Cursor.ReadCode();
5339 unsigned RecCode = Cursor.readRecord(Code, Record);
5340 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
5341 Error("Malformed AST file: missing C++ base specifiers");
5346 unsigned NumBases = Record[Idx++];
5347 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
5348 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
5349 for (unsigned I = 0; I != NumBases; ++I)
5350 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
5354 serialization::DeclID
5355 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
5356 if (LocalID < NUM_PREDEF_DECL_IDS)
5359 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5360 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
5361 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
5363 return LocalID + I->second;
5366 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
5367 ModuleFile &M) const {
5368 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
5369 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5370 return &M == I->second;
5373 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
5374 if (!D->isFromASTFile())
5376 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
5377 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5381 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
5382 if (ID < NUM_PREDEF_DECL_IDS)
5383 return SourceLocation();
5385 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5387 if (Index > DeclsLoaded.size()) {
5388 Error("declaration ID out-of-range for AST file");
5389 return SourceLocation();
5392 if (Decl *D = DeclsLoaded[Index])
5393 return D->getLocation();
5395 unsigned RawLocation = 0;
5396 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
5397 return ReadSourceLocation(*Rec.F, RawLocation);
5400 Decl *ASTReader::GetDecl(DeclID ID) {
5401 if (ID < NUM_PREDEF_DECL_IDS) {
5402 switch ((PredefinedDeclIDs)ID) {
5403 case PREDEF_DECL_NULL_ID:
5406 case PREDEF_DECL_TRANSLATION_UNIT_ID:
5407 return Context.getTranslationUnitDecl();
5409 case PREDEF_DECL_OBJC_ID_ID:
5410 return Context.getObjCIdDecl();
5412 case PREDEF_DECL_OBJC_SEL_ID:
5413 return Context.getObjCSelDecl();
5415 case PREDEF_DECL_OBJC_CLASS_ID:
5416 return Context.getObjCClassDecl();
5418 case PREDEF_DECL_OBJC_PROTOCOL_ID:
5419 return Context.getObjCProtocolDecl();
5421 case PREDEF_DECL_INT_128_ID:
5422 return Context.getInt128Decl();
5424 case PREDEF_DECL_UNSIGNED_INT_128_ID:
5425 return Context.getUInt128Decl();
5427 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
5428 return Context.getObjCInstanceTypeDecl();
5430 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
5431 return Context.getBuiltinVaListDecl();
5435 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5437 if (Index >= DeclsLoaded.size()) {
5438 assert(0 && "declaration ID out-of-range for AST file");
5439 Error("declaration ID out-of-range for AST file");
5443 if (!DeclsLoaded[Index]) {
5445 if (DeserializationListener)
5446 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
5449 return DeclsLoaded[Index];
5452 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
5454 if (GlobalID < NUM_PREDEF_DECL_IDS)
5457 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
5458 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5459 ModuleFile *Owner = I->second;
5461 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
5462 = M.GlobalToLocalDeclIDs.find(Owner);
5463 if (Pos == M.GlobalToLocalDeclIDs.end())
5466 return GlobalID - Owner->BaseDeclID + Pos->second;
5469 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
5470 const RecordData &Record,
5472 if (Idx >= Record.size()) {
5473 Error("Corrupted AST file");
5477 return getGlobalDeclID(F, Record[Idx++]);
5480 /// \brief Resolve the offset of a statement into a statement.
5482 /// This operation will read a new statement from the external
5483 /// source each time it is called, and is meant to be used via a
5484 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
5485 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
5486 // Switch case IDs are per Decl.
5487 ClearSwitchCaseIDs();
5489 // Offset here is a global offset across the entire chain.
5490 RecordLocation Loc = getLocalBitOffset(Offset);
5491 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
5492 return ReadStmtFromStream(*Loc.F);
5496 class FindExternalLexicalDeclsVisitor {
5498 const DeclContext *DC;
5499 bool (*isKindWeWant)(Decl::Kind);
5501 SmallVectorImpl<Decl*> &Decls;
5502 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
5505 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
5506 bool (*isKindWeWant)(Decl::Kind),
5507 SmallVectorImpl<Decl*> &Decls)
5508 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
5510 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
5511 PredefsVisited[I] = false;
5514 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
5518 FindExternalLexicalDeclsVisitor *This
5519 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
5521 ModuleFile::DeclContextInfosMap::iterator Info
5522 = M.DeclContextInfos.find(This->DC);
5523 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
5526 // Load all of the declaration IDs
5527 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
5528 *IDE = ID + Info->second.NumLexicalDecls;
5530 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
5533 // Don't add predefined declarations to the lexical context more
5535 if (ID->second < NUM_PREDEF_DECL_IDS) {
5536 if (This->PredefsVisited[ID->second])
5539 This->PredefsVisited[ID->second] = true;
5542 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
5543 if (!This->DC->isDeclInLexicalTraversal(D))
5544 This->Decls.push_back(D);
5553 ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
5554 bool (*isKindWeWant)(Decl::Kind),
5555 SmallVectorImpl<Decl*> &Decls) {
5556 // There might be lexical decls in multiple modules, for the TU at
5557 // least. Walk all of the modules in the order they were loaded.
5558 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
5559 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
5560 ++NumLexicalDeclContextsRead;
5571 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
5573 bool operator()(LocalDeclID L, LocalDeclID R) const {
5574 SourceLocation LHS = getLocation(L);
5575 SourceLocation RHS = getLocation(R);
5576 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5579 bool operator()(SourceLocation LHS, LocalDeclID R) const {
5580 SourceLocation RHS = getLocation(R);
5581 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5584 bool operator()(LocalDeclID L, SourceLocation RHS) const {
5585 SourceLocation LHS = getLocation(L);
5586 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5589 SourceLocation getLocation(LocalDeclID ID) const {
5590 return Reader.getSourceManager().getFileLoc(
5591 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
5597 void ASTReader::FindFileRegionDecls(FileID File,
5598 unsigned Offset, unsigned Length,
5599 SmallVectorImpl<Decl *> &Decls) {
5600 SourceManager &SM = getSourceManager();
5602 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
5603 if (I == FileDeclIDs.end())
5606 FileDeclsInfo &DInfo = I->second;
5607 if (DInfo.Decls.empty())
5611 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
5612 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
5614 DeclIDComp DIDComp(*this, *DInfo.Mod);
5615 ArrayRef<serialization::LocalDeclID>::iterator
5616 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5618 if (BeginIt != DInfo.Decls.begin())
5621 // If we are pointing at a top-level decl inside an objc container, we need
5622 // to backtrack until we find it otherwise we will fail to report that the
5623 // region overlaps with an objc container.
5624 while (BeginIt != DInfo.Decls.begin() &&
5625 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
5626 ->isTopLevelDeclInObjCContainer())
5629 ArrayRef<serialization::LocalDeclID>::iterator
5630 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5632 if (EndIt != DInfo.Decls.end())
5635 for (ArrayRef<serialization::LocalDeclID>::iterator
5636 DIt = BeginIt; DIt != EndIt; ++DIt)
5637 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
5641 /// \brief ModuleFile visitor used to perform name lookup into a
5642 /// declaration context.
5643 class DeclContextNameLookupVisitor {
5645 SmallVectorImpl<const DeclContext *> &Contexts;
5646 DeclarationName Name;
5647 SmallVectorImpl<NamedDecl *> &Decls;
5650 DeclContextNameLookupVisitor(ASTReader &Reader,
5651 SmallVectorImpl<const DeclContext *> &Contexts,
5652 DeclarationName Name,
5653 SmallVectorImpl<NamedDecl *> &Decls)
5654 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
5656 static bool visit(ModuleFile &M, void *UserData) {
5657 DeclContextNameLookupVisitor *This
5658 = static_cast<DeclContextNameLookupVisitor *>(UserData);
5660 // Check whether we have any visible declaration information for
5661 // this context in this module.
5662 ModuleFile::DeclContextInfosMap::iterator Info;
5663 bool FoundInfo = false;
5664 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5665 Info = M.DeclContextInfos.find(This->Contexts[I]);
5666 if (Info != M.DeclContextInfos.end() &&
5667 Info->second.NameLookupTableData) {
5676 // Look for this name within this module.
5677 ASTDeclContextNameLookupTable *LookupTable =
5678 Info->second.NameLookupTableData;
5679 ASTDeclContextNameLookupTable::iterator Pos
5680 = LookupTable->find(This->Name);
5681 if (Pos == LookupTable->end())
5684 bool FoundAnything = false;
5685 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5686 for (; Data.first != Data.second; ++Data.first) {
5687 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5691 if (ND->getDeclName() != This->Name) {
5692 // A name might be null because the decl's redeclarable part is
5693 // currently read before reading its name. The lookup is triggered by
5694 // building that decl (likely indirectly), and so it is later in the
5695 // sense of "already existing" and can be ignored here.
5699 // Record this declaration.
5700 FoundAnything = true;
5701 This->Decls.push_back(ND);
5704 return FoundAnything;
5709 /// \brief Retrieve the "definitive" module file for the definition of the
5710 /// given declaration context, if there is one.
5712 /// The "definitive" module file is the only place where we need to look to
5713 /// find information about the declarations within the given declaration
5714 /// context. For example, C++ and Objective-C classes, C structs/unions, and
5715 /// Objective-C protocols, categories, and extensions are all defined in a
5716 /// single place in the source code, so they have definitive module files
5717 /// associated with them. C++ namespaces, on the other hand, can have
5718 /// definitions in multiple different module files.
5720 /// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's
5721 /// NDEBUG checking.
5722 static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC,
5723 ASTReader &Reader) {
5724 if (const DeclContext *DefDC = getDefinitiveDeclContext(DC))
5725 return Reader.getOwningModuleFile(cast<Decl>(DefDC));
5731 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
5732 DeclarationName Name) {
5733 assert(DC->hasExternalVisibleStorage() &&
5734 "DeclContext has no visible decls in storage");
5738 SmallVector<NamedDecl *, 64> Decls;
5740 // Compute the declaration contexts we need to look into. Multiple such
5741 // declaration contexts occur when two declaration contexts from disjoint
5742 // modules get merged, e.g., when two namespaces with the same name are
5743 // independently defined in separate modules.
5744 SmallVector<const DeclContext *, 2> Contexts;
5745 Contexts.push_back(DC);
5747 if (DC->isNamespace()) {
5748 MergedDeclsMap::iterator Merged
5749 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5750 if (Merged != MergedDecls.end()) {
5751 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5752 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5756 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
5758 // If we can definitively determine which module file to look into,
5759 // only look there. Otherwise, look in all module files.
5760 ModuleFile *Definitive;
5761 if (Contexts.size() == 1 &&
5762 (Definitive = getDefinitiveModuleFileFor(DC, *this))) {
5763 DeclContextNameLookupVisitor::visit(*Definitive, &Visitor);
5765 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
5767 ++NumVisibleDeclContextsRead;
5768 SetExternalVisibleDeclsForName(DC, Name, Decls);
5769 return !Decls.empty();
5773 /// \brief ModuleFile visitor used to retrieve all visible names in a
5774 /// declaration context.
5775 class DeclContextAllNamesVisitor {
5777 SmallVectorImpl<const DeclContext *> &Contexts;
5778 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
5782 DeclContextAllNamesVisitor(ASTReader &Reader,
5783 SmallVectorImpl<const DeclContext *> &Contexts,
5784 llvm::DenseMap<DeclarationName,
5785 SmallVector<NamedDecl *, 8> > &Decls,
5787 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { }
5789 static bool visit(ModuleFile &M, void *UserData) {
5790 DeclContextAllNamesVisitor *This
5791 = static_cast<DeclContextAllNamesVisitor *>(UserData);
5793 // Check whether we have any visible declaration information for
5794 // this context in this module.
5795 ModuleFile::DeclContextInfosMap::iterator Info;
5796 bool FoundInfo = false;
5797 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5798 Info = M.DeclContextInfos.find(This->Contexts[I]);
5799 if (Info != M.DeclContextInfos.end() &&
5800 Info->second.NameLookupTableData) {
5809 ASTDeclContextNameLookupTable *LookupTable =
5810 Info->second.NameLookupTableData;
5811 bool FoundAnything = false;
5812 for (ASTDeclContextNameLookupTable::data_iterator
5813 I = LookupTable->data_begin(), E = LookupTable->data_end();
5816 ASTDeclContextNameLookupTrait::data_type Data = *I;
5817 for (; Data.first != Data.second; ++Data.first) {
5818 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5823 // Record this declaration.
5824 FoundAnything = true;
5825 This->Decls[ND->getDeclName()].push_back(ND);
5829 return FoundAnything && !This->VisitAll;
5834 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
5835 if (!DC->hasExternalVisibleStorage())
5837 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > Decls;
5839 // Compute the declaration contexts we need to look into. Multiple such
5840 // declaration contexts occur when two declaration contexts from disjoint
5841 // modules get merged, e.g., when two namespaces with the same name are
5842 // independently defined in separate modules.
5843 SmallVector<const DeclContext *, 2> Contexts;
5844 Contexts.push_back(DC);
5846 if (DC->isNamespace()) {
5847 MergedDeclsMap::iterator Merged
5848 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5849 if (Merged != MergedDecls.end()) {
5850 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5851 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5855 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls,
5856 /*VisitAll=*/DC->isFileContext());
5857 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5858 ++NumVisibleDeclContextsRead;
5860 for (llvm::DenseMap<DeclarationName,
5861 SmallVector<NamedDecl *, 8> >::iterator
5862 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5863 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5865 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
5868 /// \brief Under non-PCH compilation the consumer receives the objc methods
5869 /// before receiving the implementation, and codegen depends on this.
5870 /// We simulate this by deserializing and passing to consumer the methods of the
5871 /// implementation before passing the deserialized implementation decl.
5872 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5873 ASTConsumer *Consumer) {
5874 assert(ImplD && Consumer);
5876 for (ObjCImplDecl::method_iterator
5877 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
5878 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
5880 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5883 void ASTReader::PassInterestingDeclsToConsumer() {
5885 while (!InterestingDecls.empty()) {
5886 Decl *D = InterestingDecls.front();
5887 InterestingDecls.pop_front();
5889 PassInterestingDeclToConsumer(D);
5893 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5894 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5895 PassObjCImplDeclToConsumer(ImplD, Consumer);
5897 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5900 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
5901 this->Consumer = Consumer;
5906 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
5907 // Force deserialization of this decl, which will cause it to be queued for
5908 // passing to the consumer.
5909 GetDecl(ExternalDefinitions[I]);
5911 ExternalDefinitions.clear();
5913 PassInterestingDeclsToConsumer();
5916 void ASTReader::PrintStats() {
5917 std::fprintf(stderr, "*** AST File Statistics:\n");
5919 unsigned NumTypesLoaded
5920 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
5922 unsigned NumDeclsLoaded
5923 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5925 unsigned NumIdentifiersLoaded
5926 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5927 IdentifiersLoaded.end(),
5928 (IdentifierInfo *)0);
5929 unsigned NumMacrosLoaded
5930 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5933 unsigned NumSelectorsLoaded
5934 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5935 SelectorsLoaded.end(),
5938 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
5939 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5940 NumSLocEntriesRead, TotalNumSLocEntries,
5941 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
5942 if (!TypesLoaded.empty())
5943 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
5944 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5945 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5946 if (!DeclsLoaded.empty())
5947 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
5948 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5949 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
5950 if (!IdentifiersLoaded.empty())
5951 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
5952 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5953 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
5954 if (!MacrosLoaded.empty())
5955 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5956 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
5957 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
5958 if (!SelectorsLoaded.empty())
5959 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
5960 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5961 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
5962 if (TotalNumStatements)
5963 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5964 NumStatementsRead, TotalNumStatements,
5965 ((float)NumStatementsRead/TotalNumStatements * 100));
5967 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5968 NumMacrosRead, TotalNumMacros,
5969 ((float)NumMacrosRead/TotalNumMacros * 100));
5970 if (TotalLexicalDeclContexts)
5971 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5972 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5973 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5975 if (TotalVisibleDeclContexts)
5976 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5977 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5978 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5980 if (TotalNumMethodPoolEntries) {
5981 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
5982 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5983 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
5986 if (NumMethodPoolLookups) {
5987 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
5988 NumMethodPoolHits, NumMethodPoolLookups,
5989 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
5991 if (NumMethodPoolTableLookups) {
5992 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
5993 NumMethodPoolTableHits, NumMethodPoolTableLookups,
5994 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
5998 if (NumIdentifierLookupHits) {
5999 std::fprintf(stderr,
6000 " %u / %u identifier table lookups succeeded (%f%%)\n",
6001 NumIdentifierLookupHits, NumIdentifierLookups,
6002 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
6006 std::fprintf(stderr, "\n");
6007 GlobalIndex->printStats();
6010 std::fprintf(stderr, "\n");
6012 std::fprintf(stderr, "\n");
6015 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
6017 dumpModuleIDMap(StringRef Name,
6018 const ContinuousRangeMap<Key, ModuleFile *,
6019 InitialCapacity> &Map) {
6020 if (Map.begin() == Map.end())
6023 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
6024 llvm::errs() << Name << ":\n";
6025 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
6027 llvm::errs() << " " << I->first << " -> " << I->second->FileName
6032 void ASTReader::dump() {
6033 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
6034 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
6035 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
6036 dumpModuleIDMap("Global type map", GlobalTypeMap);
6037 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
6038 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
6039 dumpModuleIDMap("Global macro map", GlobalMacroMap);
6040 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
6041 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
6042 dumpModuleIDMap("Global preprocessed entity map",
6043 GlobalPreprocessedEntityMap);
6045 llvm::errs() << "\n*** PCH/Modules Loaded:";
6046 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
6047 MEnd = ModuleMgr.end();
6052 /// Return the amount of memory used by memory buffers, breaking down
6053 /// by heap-backed versus mmap'ed memory.
6054 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
6055 for (ModuleConstIterator I = ModuleMgr.begin(),
6056 E = ModuleMgr.end(); I != E; ++I) {
6057 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
6058 size_t bytes = buf->getBufferSize();
6059 switch (buf->getBufferKind()) {
6060 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
6061 sizes.malloc_bytes += bytes;
6063 case llvm::MemoryBuffer::MemoryBuffer_MMap:
6064 sizes.mmap_bytes += bytes;
6071 void ASTReader::InitializeSema(Sema &S) {
6073 S.addExternalSource(this);
6075 // Makes sure any declarations that were deserialized "too early"
6076 // still get added to the identifier's declaration chains.
6077 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
6078 pushExternalDeclIntoScope(PreloadedDecls[I],
6079 PreloadedDecls[I]->getDeclName());
6081 PreloadedDecls.clear();
6083 // Load the offsets of the declarations that Sema references.
6084 // They will be lazily deserialized when needed.
6085 if (!SemaDeclRefs.empty()) {
6086 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
6087 if (!SemaObj->StdNamespace)
6088 SemaObj->StdNamespace = SemaDeclRefs[0];
6089 if (!SemaObj->StdBadAlloc)
6090 SemaObj->StdBadAlloc = SemaDeclRefs[1];
6093 if (!FPPragmaOptions.empty()) {
6094 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
6095 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
6098 if (!OpenCLExtensions.empty()) {
6100 #define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
6101 #include "clang/Basic/OpenCLExtensions.def"
6103 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
6107 IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
6108 // Note that we are loading an identifier.
6109 Deserializing AnIdentifier(this);
6110 StringRef Name(NameStart, NameEnd - NameStart);
6112 // If there is a global index, look there first to determine which modules
6113 // provably do not have any results for this identifier.
6114 GlobalModuleIndex::HitSet Hits;
6115 GlobalModuleIndex::HitSet *HitsPtr = 0;
6116 if (!loadGlobalIndex()) {
6117 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
6121 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
6122 NumIdentifierLookups,
6123 NumIdentifierLookupHits);
6124 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
6125 IdentifierInfo *II = Visitor.getIdentifierInfo();
6126 markIdentifierUpToDate(II);
6131 /// \brief An identifier-lookup iterator that enumerates all of the
6132 /// identifiers stored within a set of AST files.
6133 class ASTIdentifierIterator : public IdentifierIterator {
6134 /// \brief The AST reader whose identifiers are being enumerated.
6135 const ASTReader &Reader;
6137 /// \brief The current index into the chain of AST files stored in
6141 /// \brief The current position within the identifier lookup table
6142 /// of the current AST file.
6143 ASTIdentifierLookupTable::key_iterator Current;
6145 /// \brief The end position within the identifier lookup table of
6146 /// the current AST file.
6147 ASTIdentifierLookupTable::key_iterator End;
6150 explicit ASTIdentifierIterator(const ASTReader &Reader);
6152 virtual StringRef Next();
6156 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
6157 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
6158 ASTIdentifierLookupTable *IdTable
6159 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
6160 Current = IdTable->key_begin();
6161 End = IdTable->key_end();
6164 StringRef ASTIdentifierIterator::Next() {
6165 while (Current == End) {
6166 // If we have exhausted all of our AST files, we're done.
6171 ASTIdentifierLookupTable *IdTable
6172 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
6173 IdentifierLookupTable;
6174 Current = IdTable->key_begin();
6175 End = IdTable->key_end();
6178 // We have any identifiers remaining in the current AST file; return
6180 StringRef Result = *Current;
6185 IdentifierIterator *ASTReader::getIdentifiers() {
6186 if (!loadGlobalIndex())
6187 return GlobalIndex->createIdentifierIterator();
6189 return new ASTIdentifierIterator(*this);
6192 namespace clang { namespace serialization {
6193 class ReadMethodPoolVisitor {
6196 unsigned PriorGeneration;
6197 unsigned InstanceBits;
6198 unsigned FactoryBits;
6199 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
6200 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
6203 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
6204 unsigned PriorGeneration)
6205 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration),
6206 InstanceBits(0), FactoryBits(0) { }
6208 static bool visit(ModuleFile &M, void *UserData) {
6209 ReadMethodPoolVisitor *This
6210 = static_cast<ReadMethodPoolVisitor *>(UserData);
6212 if (!M.SelectorLookupTable)
6215 // If we've already searched this module file, skip it now.
6216 if (M.Generation <= This->PriorGeneration)
6219 ++This->Reader.NumMethodPoolTableLookups;
6220 ASTSelectorLookupTable *PoolTable
6221 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
6222 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
6223 if (Pos == PoolTable->end())
6226 ++This->Reader.NumMethodPoolTableHits;
6227 ++This->Reader.NumSelectorsRead;
6228 // FIXME: Not quite happy with the statistics here. We probably should
6229 // disable this tracking when called via LoadSelector.
6230 // Also, should entries without methods count as misses?
6231 ++This->Reader.NumMethodPoolEntriesRead;
6232 ASTSelectorLookupTrait::data_type Data = *Pos;
6233 if (This->Reader.DeserializationListener)
6234 This->Reader.DeserializationListener->SelectorRead(Data.ID,
6237 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
6238 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
6239 This->InstanceBits = Data.InstanceBits;
6240 This->FactoryBits = Data.FactoryBits;
6244 /// \brief Retrieve the instance methods found by this visitor.
6245 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
6246 return InstanceMethods;
6249 /// \brief Retrieve the instance methods found by this visitor.
6250 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
6251 return FactoryMethods;
6254 unsigned getInstanceBits() const { return InstanceBits; }
6255 unsigned getFactoryBits() const { return FactoryBits; }
6257 } } // end namespace clang::serialization
6259 /// \brief Add the given set of methods to the method list.
6260 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
6261 ObjCMethodList &List) {
6262 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
6263 S.addMethodToGlobalList(&List, Methods[I]);
6267 void ASTReader::ReadMethodPool(Selector Sel) {
6268 // Get the selector generation and update it to the current generation.
6269 unsigned &Generation = SelectorGeneration[Sel];
6270 unsigned PriorGeneration = Generation;
6271 Generation = CurrentGeneration;
6273 // Search for methods defined with this selector.
6274 ++NumMethodPoolLookups;
6275 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
6276 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
6278 if (Visitor.getInstanceMethods().empty() &&
6279 Visitor.getFactoryMethods().empty())
6282 ++NumMethodPoolHits;
6287 Sema &S = *getSema();
6288 Sema::GlobalMethodPool::iterator Pos
6289 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
6291 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
6292 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
6293 Pos->second.first.setBits(Visitor.getInstanceBits());
6294 Pos->second.second.setBits(Visitor.getFactoryBits());
6297 void ASTReader::ReadKnownNamespaces(
6298 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
6301 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
6302 if (NamespaceDecl *Namespace
6303 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
6304 Namespaces.push_back(Namespace);
6308 void ASTReader::ReadUndefinedButUsed(
6309 llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) {
6310 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
6311 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
6312 SourceLocation Loc =
6313 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
6314 Undefined.insert(std::make_pair(D, Loc));
6318 void ASTReader::ReadTentativeDefinitions(
6319 SmallVectorImpl<VarDecl *> &TentativeDefs) {
6320 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
6321 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
6323 TentativeDefs.push_back(Var);
6325 TentativeDefinitions.clear();
6328 void ASTReader::ReadUnusedFileScopedDecls(
6329 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
6330 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
6332 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
6336 UnusedFileScopedDecls.clear();
6339 void ASTReader::ReadDelegatingConstructors(
6340 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
6341 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
6342 CXXConstructorDecl *D
6343 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
6347 DelegatingCtorDecls.clear();
6350 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
6351 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
6353 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
6357 ExtVectorDecls.clear();
6360 void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
6361 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
6363 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
6367 DynamicClasses.clear();
6371 ASTReader::ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl *> &Decls) {
6372 for (unsigned I = 0, N = LocallyScopedExternCDecls.size(); I != N; ++I) {
6374 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternCDecls[I]));
6378 LocallyScopedExternCDecls.clear();
6381 void ASTReader::ReadReferencedSelectors(
6382 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
6383 if (ReferencedSelectorsData.empty())
6386 // If there are @selector references added them to its pool. This is for
6387 // implementation of -Wselector.
6388 unsigned int DataSize = ReferencedSelectorsData.size()-1;
6390 while (I < DataSize) {
6391 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
6392 SourceLocation SelLoc
6393 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
6394 Sels.push_back(std::make_pair(Sel, SelLoc));
6396 ReferencedSelectorsData.clear();
6399 void ASTReader::ReadWeakUndeclaredIdentifiers(
6400 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
6401 if (WeakUndeclaredIdentifiers.empty())
6404 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
6405 IdentifierInfo *WeakId
6406 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6407 IdentifierInfo *AliasId
6408 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6410 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
6411 bool Used = WeakUndeclaredIdentifiers[I++];
6412 WeakInfo WI(AliasId, Loc);
6414 WeakIDs.push_back(std::make_pair(WeakId, WI));
6416 WeakUndeclaredIdentifiers.clear();
6419 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
6420 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
6421 ExternalVTableUse VT;
6422 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
6423 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
6424 VT.DefinitionRequired = VTableUses[Idx++];
6425 VTables.push_back(VT);
6431 void ASTReader::ReadPendingInstantiations(
6432 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
6433 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
6434 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
6436 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
6438 Pending.push_back(std::make_pair(D, Loc));
6440 PendingInstantiations.clear();
6443 void ASTReader::LoadSelector(Selector Sel) {
6444 // It would be complicated to avoid reading the methods anyway. So don't.
6445 ReadMethodPool(Sel);
6448 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
6449 assert(ID && "Non-zero identifier ID required");
6450 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
6451 IdentifiersLoaded[ID - 1] = II;
6452 if (DeserializationListener)
6453 DeserializationListener->IdentifierRead(ID, II);
6456 /// \brief Set the globally-visible declarations associated with the given
6459 /// If the AST reader is currently in a state where the given declaration IDs
6460 /// cannot safely be resolved, they are queued until it is safe to resolve
6463 /// \param II an IdentifierInfo that refers to one or more globally-visible
6466 /// \param DeclIDs the set of declaration IDs with the name @p II that are
6467 /// visible at global scope.
6469 /// \param Decls if non-null, this vector will be populated with the set of
6470 /// deserialized declarations. These declarations will not be pushed into
6473 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
6474 const SmallVectorImpl<uint32_t> &DeclIDs,
6475 SmallVectorImpl<Decl *> *Decls) {
6476 if (NumCurrentElementsDeserializing && !Decls) {
6477 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
6481 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
6482 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
6484 // If we're simply supposed to record the declarations, do so now.
6486 Decls->push_back(D);
6490 // Introduce this declaration into the translation-unit scope
6491 // and add it to the declaration chain for this identifier, so
6492 // that (unqualified) name lookup will find it.
6493 pushExternalDeclIntoScope(D, II);
6495 // Queue this declaration so that it will be added to the
6496 // translation unit scope and identifier's declaration chain
6497 // once a Sema object is known.
6498 PreloadedDecls.push_back(D);
6503 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
6507 if (IdentifiersLoaded.empty()) {
6508 Error("no identifier table in AST file");
6513 if (!IdentifiersLoaded[ID]) {
6514 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
6515 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
6516 ModuleFile *M = I->second;
6517 unsigned Index = ID - M->BaseIdentifierID;
6518 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
6520 // All of the strings in the AST file are preceded by a 16-bit length.
6521 // Extract that 16-bit length to avoid having to execute strlen().
6522 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
6523 // unsigned integers. This is important to avoid integer overflow when
6524 // we cast them to 'unsigned'.
6525 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
6526 unsigned StrLen = (((unsigned) StrLenPtr[0])
6527 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
6528 IdentifiersLoaded[ID]
6529 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
6530 if (DeserializationListener)
6531 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
6534 return IdentifiersLoaded[ID];
6537 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
6538 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
6541 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
6542 if (LocalID < NUM_PREDEF_IDENT_IDS)
6545 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6546 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
6547 assert(I != M.IdentifierRemap.end()
6548 && "Invalid index into identifier index remap");
6550 return LocalID + I->second;
6553 MacroInfo *ASTReader::getMacro(MacroID ID) {
6557 if (MacrosLoaded.empty()) {
6558 Error("no macro table in AST file");
6562 ID -= NUM_PREDEF_MACRO_IDS;
6563 if (!MacrosLoaded[ID]) {
6564 GlobalMacroMapType::iterator I
6565 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
6566 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
6567 ModuleFile *M = I->second;
6568 unsigned Index = ID - M->BaseMacroID;
6569 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
6571 if (DeserializationListener)
6572 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
6576 return MacrosLoaded[ID];
6579 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
6580 if (LocalID < NUM_PREDEF_MACRO_IDS)
6583 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6584 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
6585 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
6587 return LocalID + I->second;
6590 serialization::SubmoduleID
6591 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
6592 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
6595 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6596 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
6597 assert(I != M.SubmoduleRemap.end()
6598 && "Invalid index into submodule index remap");
6600 return LocalID + I->second;
6603 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
6604 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6605 assert(GlobalID == 0 && "Unhandled global submodule ID");
6609 if (GlobalID > SubmodulesLoaded.size()) {
6610 Error("submodule ID out of range in AST file");
6614 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
6617 Module *ASTReader::getModule(unsigned ID) {
6618 return getSubmodule(ID);
6621 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
6622 return DecodeSelector(getGlobalSelectorID(M, LocalID));
6625 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
6629 if (ID > SelectorsLoaded.size()) {
6630 Error("selector ID out of range in AST file");
6634 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
6635 // Load this selector from the selector table.
6636 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
6637 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
6638 ModuleFile &M = *I->second;
6639 ASTSelectorLookupTrait Trait(*this, M);
6640 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
6641 SelectorsLoaded[ID - 1] =
6642 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
6643 if (DeserializationListener)
6644 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
6647 return SelectorsLoaded[ID - 1];
6650 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
6651 return DecodeSelector(ID);
6654 uint32_t ASTReader::GetNumExternalSelectors() {
6655 // ID 0 (the null selector) is considered an external selector.
6656 return getTotalNumSelectors() + 1;
6659 serialization::SelectorID
6660 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
6661 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
6664 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6665 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
6666 assert(I != M.SelectorRemap.end()
6667 && "Invalid index into selector index remap");
6669 return LocalID + I->second;
6673 ASTReader::ReadDeclarationName(ModuleFile &F,
6674 const RecordData &Record, unsigned &Idx) {
6675 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
6677 case DeclarationName::Identifier:
6678 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
6680 case DeclarationName::ObjCZeroArgSelector:
6681 case DeclarationName::ObjCOneArgSelector:
6682 case DeclarationName::ObjCMultiArgSelector:
6683 return DeclarationName(ReadSelector(F, Record, Idx));
6685 case DeclarationName::CXXConstructorName:
6686 return Context.DeclarationNames.getCXXConstructorName(
6687 Context.getCanonicalType(readType(F, Record, Idx)));
6689 case DeclarationName::CXXDestructorName:
6690 return Context.DeclarationNames.getCXXDestructorName(
6691 Context.getCanonicalType(readType(F, Record, Idx)));
6693 case DeclarationName::CXXConversionFunctionName:
6694 return Context.DeclarationNames.getCXXConversionFunctionName(
6695 Context.getCanonicalType(readType(F, Record, Idx)));
6697 case DeclarationName::CXXOperatorName:
6698 return Context.DeclarationNames.getCXXOperatorName(
6699 (OverloadedOperatorKind)Record[Idx++]);
6701 case DeclarationName::CXXLiteralOperatorName:
6702 return Context.DeclarationNames.getCXXLiteralOperatorName(
6703 GetIdentifierInfo(F, Record, Idx));
6705 case DeclarationName::CXXUsingDirective:
6706 return DeclarationName::getUsingDirectiveName();
6709 llvm_unreachable("Invalid NameKind!");
6712 void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
6713 DeclarationNameLoc &DNLoc,
6714 DeclarationName Name,
6715 const RecordData &Record, unsigned &Idx) {
6716 switch (Name.getNameKind()) {
6717 case DeclarationName::CXXConstructorName:
6718 case DeclarationName::CXXDestructorName:
6719 case DeclarationName::CXXConversionFunctionName:
6720 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
6723 case DeclarationName::CXXOperatorName:
6724 DNLoc.CXXOperatorName.BeginOpNameLoc
6725 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6726 DNLoc.CXXOperatorName.EndOpNameLoc
6727 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6730 case DeclarationName::CXXLiteralOperatorName:
6731 DNLoc.CXXLiteralOperatorName.OpNameLoc
6732 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6735 case DeclarationName::Identifier:
6736 case DeclarationName::ObjCZeroArgSelector:
6737 case DeclarationName::ObjCOneArgSelector:
6738 case DeclarationName::ObjCMultiArgSelector:
6739 case DeclarationName::CXXUsingDirective:
6744 void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
6745 DeclarationNameInfo &NameInfo,
6746 const RecordData &Record, unsigned &Idx) {
6747 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
6748 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6749 DeclarationNameLoc DNLoc;
6750 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6751 NameInfo.setInfo(DNLoc);
6754 void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
6755 const RecordData &Record, unsigned &Idx) {
6756 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
6757 unsigned NumTPLists = Record[Idx++];
6758 Info.NumTemplParamLists = NumTPLists;
6760 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
6761 for (unsigned i=0; i != NumTPLists; ++i)
6762 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6767 ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
6769 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
6771 case TemplateName::Template:
6772 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
6774 case TemplateName::OverloadedTemplate: {
6775 unsigned size = Record[Idx++];
6776 UnresolvedSet<8> Decls;
6778 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
6780 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
6783 case TemplateName::QualifiedTemplate: {
6784 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6785 bool hasTemplKeyword = Record[Idx++];
6786 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
6787 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
6790 case TemplateName::DependentTemplate: {
6791 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6792 if (Record[Idx++]) // isIdentifier
6793 return Context.getDependentTemplateName(NNS,
6794 GetIdentifierInfo(F, Record,
6796 return Context.getDependentTemplateName(NNS,
6797 (OverloadedOperatorKind)Record[Idx++]);
6800 case TemplateName::SubstTemplateTemplateParm: {
6801 TemplateTemplateParmDecl *param
6802 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6803 if (!param) return TemplateName();
6804 TemplateName replacement = ReadTemplateName(F, Record, Idx);
6805 return Context.getSubstTemplateTemplateParm(param, replacement);
6808 case TemplateName::SubstTemplateTemplateParmPack: {
6809 TemplateTemplateParmDecl *Param
6810 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6812 return TemplateName();
6814 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6815 if (ArgPack.getKind() != TemplateArgument::Pack)
6816 return TemplateName();
6818 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
6822 llvm_unreachable("Unhandled template name kind!");
6826 ASTReader::ReadTemplateArgument(ModuleFile &F,
6827 const RecordData &Record, unsigned &Idx) {
6828 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6830 case TemplateArgument::Null:
6831 return TemplateArgument();
6832 case TemplateArgument::Type:
6833 return TemplateArgument(readType(F, Record, Idx));
6834 case TemplateArgument::Declaration: {
6835 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6836 bool ForReferenceParam = Record[Idx++];
6837 return TemplateArgument(D, ForReferenceParam);
6839 case TemplateArgument::NullPtr:
6840 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
6841 case TemplateArgument::Integral: {
6842 llvm::APSInt Value = ReadAPSInt(Record, Idx);
6843 QualType T = readType(F, Record, Idx);
6844 return TemplateArgument(Context, Value, T);
6846 case TemplateArgument::Template:
6847 return TemplateArgument(ReadTemplateName(F, Record, Idx));
6848 case TemplateArgument::TemplateExpansion: {
6849 TemplateName Name = ReadTemplateName(F, Record, Idx);
6850 Optional<unsigned> NumTemplateExpansions;
6851 if (unsigned NumExpansions = Record[Idx++])
6852 NumTemplateExpansions = NumExpansions - 1;
6853 return TemplateArgument(Name, NumTemplateExpansions);
6855 case TemplateArgument::Expression:
6856 return TemplateArgument(ReadExpr(F));
6857 case TemplateArgument::Pack: {
6858 unsigned NumArgs = Record[Idx++];
6859 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
6860 for (unsigned I = 0; I != NumArgs; ++I)
6861 Args[I] = ReadTemplateArgument(F, Record, Idx);
6862 return TemplateArgument(Args, NumArgs);
6866 llvm_unreachable("Unhandled template argument kind!");
6869 TemplateParameterList *
6870 ASTReader::ReadTemplateParameterList(ModuleFile &F,
6871 const RecordData &Record, unsigned &Idx) {
6872 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6873 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6874 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
6876 unsigned NumParams = Record[Idx++];
6877 SmallVector<NamedDecl *, 16> Params;
6878 Params.reserve(NumParams);
6880 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
6882 TemplateParameterList* TemplateParams =
6883 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
6884 Params.data(), Params.size(), RAngleLoc);
6885 return TemplateParams;
6890 ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
6891 ModuleFile &F, const RecordData &Record,
6893 unsigned NumTemplateArgs = Record[Idx++];
6894 TemplArgs.reserve(NumTemplateArgs);
6895 while (NumTemplateArgs--)
6896 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
6899 /// \brief Read a UnresolvedSet structure.
6900 void ASTReader::ReadUnresolvedSet(ModuleFile &F, ASTUnresolvedSet &Set,
6901 const RecordData &Record, unsigned &Idx) {
6902 unsigned NumDecls = Record[Idx++];
6903 Set.reserve(Context, NumDecls);
6904 while (NumDecls--) {
6905 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
6906 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6907 Set.addDecl(Context, D, AS);
6912 ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
6913 const RecordData &Record, unsigned &Idx) {
6914 bool isVirtual = static_cast<bool>(Record[Idx++]);
6915 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6916 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
6917 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
6918 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6919 SourceRange Range = ReadSourceRange(F, Record, Idx);
6920 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
6921 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
6923 Result.setInheritConstructors(inheritConstructors);
6927 std::pair<CXXCtorInitializer **, unsigned>
6928 ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
6930 CXXCtorInitializer **CtorInitializers = 0;
6931 unsigned NumInitializers = Record[Idx++];
6932 if (NumInitializers) {
6934 = new (Context) CXXCtorInitializer*[NumInitializers];
6935 for (unsigned i=0; i != NumInitializers; ++i) {
6936 TypeSourceInfo *TInfo = 0;
6937 bool IsBaseVirtual = false;
6938 FieldDecl *Member = 0;
6939 IndirectFieldDecl *IndirectMember = 0;
6941 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6943 case CTOR_INITIALIZER_BASE:
6944 TInfo = GetTypeSourceInfo(F, Record, Idx);
6945 IsBaseVirtual = Record[Idx++];
6948 case CTOR_INITIALIZER_DELEGATING:
6949 TInfo = GetTypeSourceInfo(F, Record, Idx);
6952 case CTOR_INITIALIZER_MEMBER:
6953 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
6956 case CTOR_INITIALIZER_INDIRECT_MEMBER:
6957 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
6961 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
6962 Expr *Init = ReadExpr(F);
6963 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6964 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
6965 bool IsWritten = Record[Idx++];
6966 unsigned SourceOrderOrNumArrayIndices;
6967 SmallVector<VarDecl *, 8> Indices;
6969 SourceOrderOrNumArrayIndices = Record[Idx++];
6971 SourceOrderOrNumArrayIndices = Record[Idx++];
6972 Indices.reserve(SourceOrderOrNumArrayIndices);
6973 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
6974 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
6977 CXXCtorInitializer *BOMInit;
6978 if (Type == CTOR_INITIALIZER_BASE) {
6979 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
6980 LParenLoc, Init, RParenLoc,
6981 MemberOrEllipsisLoc);
6982 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
6983 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6985 } else if (IsWritten) {
6987 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
6988 LParenLoc, Init, RParenLoc);
6990 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
6991 MemberOrEllipsisLoc, LParenLoc,
6994 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
6995 LParenLoc, Init, RParenLoc,
6996 Indices.data(), Indices.size());
7000 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
7001 CtorInitializers[i] = BOMInit;
7005 return std::make_pair(CtorInitializers, NumInitializers);
7008 NestedNameSpecifier *
7009 ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
7010 const RecordData &Record, unsigned &Idx) {
7011 unsigned N = Record[Idx++];
7012 NestedNameSpecifier *NNS = 0, *Prev = 0;
7013 for (unsigned I = 0; I != N; ++I) {
7014 NestedNameSpecifier::SpecifierKind Kind
7015 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7017 case NestedNameSpecifier::Identifier: {
7018 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7019 NNS = NestedNameSpecifier::Create(Context, Prev, II);
7023 case NestedNameSpecifier::Namespace: {
7024 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7025 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
7029 case NestedNameSpecifier::NamespaceAlias: {
7030 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7031 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
7035 case NestedNameSpecifier::TypeSpec:
7036 case NestedNameSpecifier::TypeSpecWithTemplate: {
7037 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
7041 bool Template = Record[Idx++];
7042 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
7046 case NestedNameSpecifier::Global: {
7047 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
7048 // No associated value, and there can't be a prefix.
7057 NestedNameSpecifierLoc
7058 ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
7060 unsigned N = Record[Idx++];
7061 NestedNameSpecifierLocBuilder Builder;
7062 for (unsigned I = 0; I != N; ++I) {
7063 NestedNameSpecifier::SpecifierKind Kind
7064 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7066 case NestedNameSpecifier::Identifier: {
7067 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7068 SourceRange Range = ReadSourceRange(F, Record, Idx);
7069 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
7073 case NestedNameSpecifier::Namespace: {
7074 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7075 SourceRange Range = ReadSourceRange(F, Record, Idx);
7076 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
7080 case NestedNameSpecifier::NamespaceAlias: {
7081 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7082 SourceRange Range = ReadSourceRange(F, Record, Idx);
7083 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
7087 case NestedNameSpecifier::TypeSpec:
7088 case NestedNameSpecifier::TypeSpecWithTemplate: {
7089 bool Template = Record[Idx++];
7090 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
7092 return NestedNameSpecifierLoc();
7093 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7095 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
7096 Builder.Extend(Context,
7097 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
7098 T->getTypeLoc(), ColonColonLoc);
7102 case NestedNameSpecifier::Global: {
7103 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7104 Builder.MakeGlobal(Context, ColonColonLoc);
7110 return Builder.getWithLocInContext(Context);
7114 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
7116 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
7117 SourceLocation end = ReadSourceLocation(F, Record, Idx);
7118 return SourceRange(beg, end);
7121 /// \brief Read an integral value
7122 llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
7123 unsigned BitWidth = Record[Idx++];
7124 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
7125 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
7130 /// \brief Read a signed integral value
7131 llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
7132 bool isUnsigned = Record[Idx++];
7133 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
7136 /// \brief Read a floating-point value
7137 llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
7138 const llvm::fltSemantics &Sem,
7140 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
7143 // \brief Read a string
7144 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
7145 unsigned Len = Record[Idx++];
7146 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
7151 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
7153 unsigned Major = Record[Idx++];
7154 unsigned Minor = Record[Idx++];
7155 unsigned Subminor = Record[Idx++];
7157 return VersionTuple(Major);
7159 return VersionTuple(Major, Minor - 1);
7160 return VersionTuple(Major, Minor - 1, Subminor - 1);
7163 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
7164 const RecordData &Record,
7166 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
7167 return CXXTemporary::Create(Context, Decl);
7170 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
7171 return Diag(SourceLocation(), DiagID);
7174 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
7175 return Diags.Report(Loc, DiagID);
7178 /// \brief Retrieve the identifier table associated with the
7180 IdentifierTable &ASTReader::getIdentifierTable() {
7181 return PP.getIdentifierTable();
7184 /// \brief Record that the given ID maps to the given switch-case
7186 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
7187 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
7188 "Already have a SwitchCase with this ID");
7189 (*CurrSwitchCaseStmts)[ID] = SC;
7192 /// \brief Retrieve the switch-case statement with the given ID.
7193 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
7194 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
7195 return (*CurrSwitchCaseStmts)[ID];
7198 void ASTReader::ClearSwitchCaseIDs() {
7199 CurrSwitchCaseStmts->clear();
7202 void ASTReader::ReadComments() {
7203 std::vector<RawComment *> Comments;
7204 for (SmallVectorImpl<std::pair<BitstreamCursor,
7205 serialization::ModuleFile *> >::iterator
7206 I = CommentsCursors.begin(),
7207 E = CommentsCursors.end();
7209 BitstreamCursor &Cursor = I->first;
7210 serialization::ModuleFile &F = *I->second;
7211 SavedStreamPosition SavedPosition(Cursor);
7215 llvm::BitstreamEntry Entry =
7216 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
7218 switch (Entry.Kind) {
7219 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
7220 case llvm::BitstreamEntry::Error:
7221 Error("malformed block record in AST file");
7223 case llvm::BitstreamEntry::EndBlock:
7225 case llvm::BitstreamEntry::Record:
7226 // The interesting case.
7232 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
7233 case COMMENTS_RAW_COMMENT: {
7235 SourceRange SR = ReadSourceRange(F, Record, Idx);
7236 RawComment::CommentKind Kind =
7237 (RawComment::CommentKind) Record[Idx++];
7238 bool IsTrailingComment = Record[Idx++];
7239 bool IsAlmostTrailingComment = Record[Idx++];
7240 Comments.push_back(new (Context) RawComment(
7241 SR, Kind, IsTrailingComment, IsAlmostTrailingComment,
7242 Context.getLangOpts().CommentOpts.ParseAllComments));
7249 Context.Comments.addCommentsToFront(Comments);
7252 void ASTReader::finishPendingActions() {
7253 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
7254 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty()) {
7255 // If any identifiers with corresponding top-level declarations have
7256 // been loaded, load those declarations now.
7257 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> > TopLevelDecls;
7258 while (!PendingIdentifierInfos.empty()) {
7260 IdentifierInfo *II = PendingIdentifierInfos.back().first;
7261 SmallVector<uint32_t, 4> DeclIDs = PendingIdentifierInfos.back().second;
7262 PendingIdentifierInfos.pop_back();
7264 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
7267 // Load pending declaration chains.
7268 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
7269 loadPendingDeclChain(PendingDeclChains[I]);
7270 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
7272 PendingDeclChains.clear();
7274 // Make the most recent of the top-level declarations visible.
7275 for (llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >::iterator
7276 TLD = TopLevelDecls.begin(), TLDEnd = TopLevelDecls.end();
7277 TLD != TLDEnd; ++TLD) {
7278 IdentifierInfo *II = TLD->first;
7279 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
7280 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
7284 // Load any pending macro definitions.
7285 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
7286 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
7287 SmallVector<PendingMacroInfo, 2> GlobalIDs;
7288 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
7289 // Initialize the macro history from chained-PCHs ahead of module imports.
7290 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
7292 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
7293 if (Info.M->Kind != MK_Module)
7294 resolvePendingMacro(II, Info);
7296 // Handle module imports.
7297 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
7299 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
7300 if (Info.M->Kind == MK_Module)
7301 resolvePendingMacro(II, Info);
7304 PendingMacroIDs.clear();
7306 // Wire up the DeclContexts for Decls that we delayed setting until
7307 // recursive loading is completed.
7308 while (!PendingDeclContextInfos.empty()) {
7309 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
7310 PendingDeclContextInfos.pop_front();
7311 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
7312 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
7313 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
7317 // If we deserialized any C++ or Objective-C class definitions, any
7318 // Objective-C protocol definitions, or any redeclarable templates, make sure
7319 // that all redeclarations point to the definitions. Note that this can only
7320 // happen now, after the redeclaration chains have been fully wired.
7321 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
7322 DEnd = PendingDefinitions.end();
7324 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
7325 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
7326 // Make sure that the TagType points at the definition.
7327 const_cast<TagType*>(TagT)->decl = TD;
7330 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
7331 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
7332 REnd = RD->redecls_end();
7334 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
7341 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
7342 // Make sure that the ObjCInterfaceType points at the definition.
7343 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
7346 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
7347 REnd = ID->redecls_end();
7354 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
7355 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
7356 REnd = PD->redecls_end();
7363 RedeclarableTemplateDecl *RTD
7364 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
7365 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
7366 REnd = RTD->redecls_end();
7368 R->Common = RTD->Common;
7370 PendingDefinitions.clear();
7372 // Load the bodies of any functions or methods we've encountered. We do
7373 // this now (delayed) so that we can be sure that the declaration chains
7374 // have been fully wired up.
7375 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
7376 PBEnd = PendingBodies.end();
7377 PB != PBEnd; ++PB) {
7378 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
7379 // FIXME: Check for =delete/=default?
7380 // FIXME: Complain about ODR violations here?
7381 if (!getContext().getLangOpts().Modules || !FD->hasBody())
7382 FD->setLazyBody(PB->second);
7386 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
7387 if (!getContext().getLangOpts().Modules || !MD->hasBody())
7388 MD->setLazyBody(PB->second);
7390 PendingBodies.clear();
7393 void ASTReader::FinishedDeserializing() {
7394 assert(NumCurrentElementsDeserializing &&
7395 "FinishedDeserializing not paired with StartedDeserializing");
7396 if (NumCurrentElementsDeserializing == 1) {
7397 // We decrease NumCurrentElementsDeserializing only after pending actions
7398 // are finished, to avoid recursively re-calling finishPendingActions().
7399 finishPendingActions();
7401 --NumCurrentElementsDeserializing;
7403 if (NumCurrentElementsDeserializing == 0 &&
7404 Consumer && !PassingDeclsToConsumer) {
7405 // Guard variable to avoid recursively redoing the process of passing
7406 // decls to consumer.
7407 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
7410 while (!InterestingDecls.empty()) {
7411 // We are not in recursive loading, so it's safe to pass the "interesting"
7412 // decls to the consumer.
7413 Decl *D = InterestingDecls.front();
7414 InterestingDecls.pop_front();
7415 PassInterestingDeclToConsumer(D);
7420 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
7421 D = cast<NamedDecl>(D->getMostRecentDecl());
7423 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
7424 SemaObj->TUScope->AddDecl(D);
7425 } else if (SemaObj->TUScope) {
7426 // Adding the decl to IdResolver may have failed because it was already in
7427 // (even though it was not added in scope). If it is already in, make sure
7428 // it gets in the scope as well.
7429 if (std::find(SemaObj->IdResolver.begin(Name),
7430 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
7431 SemaObj->TUScope->AddDecl(D);
7435 ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
7436 StringRef isysroot, bool DisableValidation,
7437 bool AllowASTWithCompilerErrors, bool UseGlobalIndex)
7438 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
7439 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
7440 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
7441 Consumer(0), ModuleMgr(PP.getFileManager()),
7442 isysroot(isysroot), DisableValidation(DisableValidation),
7443 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
7444 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
7445 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
7446 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
7447 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
7448 TotalNumMacros(0), NumIdentifierLookups(0), NumIdentifierLookupHits(0),
7449 NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
7450 NumMethodPoolLookups(0), NumMethodPoolHits(0),
7451 NumMethodPoolTableLookups(0), NumMethodPoolTableHits(0),
7452 TotalNumMethodPoolEntries(0),
7453 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
7454 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
7455 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
7456 PassingDeclsToConsumer(false),
7457 NumCXXBaseSpecifiersLoaded(0)
7459 SourceMgr.setExternalSLocEntrySource(this);
7462 ASTReader::~ASTReader() {
7463 for (DeclContextVisibleUpdatesPending::iterator
7464 I = PendingVisibleUpdates.begin(),
7465 E = PendingVisibleUpdates.end();
7467 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
7468 F = I->second.end();