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*/> >
213 typedef llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> >
217 /// \brief Collect the macro definitions provided by the given preprocessor
219 static void collectMacroDefinitions(const PreprocessorOptions &PPOpts,
220 MacroDefinitionsMap &Macros,
221 SmallVectorImpl<StringRef> *MacroNames = 0){
222 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
223 StringRef Macro = PPOpts.Macros[I].first;
224 bool IsUndef = PPOpts.Macros[I].second;
226 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
227 StringRef MacroName = MacroPair.first;
228 StringRef MacroBody = MacroPair.second;
230 // For an #undef'd macro, we only care about the name.
232 if (MacroNames && !Macros.count(MacroName))
233 MacroNames->push_back(MacroName);
235 Macros[MacroName] = std::make_pair("", true);
239 // For a #define'd macro, figure out the actual definition.
240 if (MacroName.size() == Macro.size())
243 // Note: GCC drops anything following an end-of-line character.
244 StringRef::size_type End = MacroBody.find_first_of("\n\r");
245 MacroBody = MacroBody.substr(0, End);
248 if (MacroNames && !Macros.count(MacroName))
249 MacroNames->push_back(MacroName);
250 Macros[MacroName] = std::make_pair(MacroBody, false);
254 /// \brief Check the preprocessor options deserialized from the control block
255 /// against the preprocessor options in an existing preprocessor.
257 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
258 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
259 const PreprocessorOptions &ExistingPPOpts,
260 DiagnosticsEngine *Diags,
261 FileManager &FileMgr,
262 std::string &SuggestedPredefines,
263 const LangOptions &LangOpts) {
264 // Check macro definitions.
265 MacroDefinitionsMap ASTFileMacros;
266 collectMacroDefinitions(PPOpts, ASTFileMacros);
267 MacroDefinitionsMap ExistingMacros;
268 SmallVector<StringRef, 4> ExistingMacroNames;
269 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
271 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
272 // Dig out the macro definition in the existing preprocessor options.
273 StringRef MacroName = ExistingMacroNames[I];
274 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
276 // Check whether we know anything about this macro name or not.
277 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known
278 = ASTFileMacros.find(MacroName);
279 if (Known == ASTFileMacros.end()) {
280 // FIXME: Check whether this identifier was referenced anywhere in the
281 // AST file. If so, we should reject the AST file. Unfortunately, this
282 // information isn't in the control block. What shall we do about it?
284 if (Existing.second) {
285 SuggestedPredefines += "#undef ";
286 SuggestedPredefines += MacroName.str();
287 SuggestedPredefines += '\n';
289 SuggestedPredefines += "#define ";
290 SuggestedPredefines += MacroName.str();
291 SuggestedPredefines += ' ';
292 SuggestedPredefines += Existing.first.str();
293 SuggestedPredefines += '\n';
298 // If the macro was defined in one but undef'd in the other, we have a
300 if (Existing.second != Known->second.second) {
302 Diags->Report(diag::err_pch_macro_def_undef)
303 << MacroName << Known->second.second;
308 // If the macro was #undef'd in both, or if the macro bodies are identical,
310 if (Existing.second || Existing.first == Known->second.first)
313 // The macro bodies differ; complain.
315 Diags->Report(diag::err_pch_macro_def_conflict)
316 << MacroName << Known->second.first << Existing.first;
321 // Check whether we're using predefines.
322 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) {
324 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
329 // Detailed record is important since it is used for the module cache hash.
330 if (LangOpts.Modules &&
331 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord) {
333 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
338 // Compute the #include and #include_macros lines we need.
339 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
340 StringRef File = ExistingPPOpts.Includes[I];
341 if (File == ExistingPPOpts.ImplicitPCHInclude)
344 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
345 != PPOpts.Includes.end())
348 SuggestedPredefines += "#include \"";
349 SuggestedPredefines +=
350 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
351 SuggestedPredefines += "\"\n";
354 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
355 StringRef File = ExistingPPOpts.MacroIncludes[I];
356 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
358 != PPOpts.MacroIncludes.end())
361 SuggestedPredefines += "#__include_macros \"";
362 SuggestedPredefines +=
363 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
364 SuggestedPredefines += "\"\n##\n";
370 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
372 std::string &SuggestedPredefines) {
373 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
375 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
376 Complain? &Reader.Diags : 0,
382 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
383 PP.setCounterValue(Value);
386 //===----------------------------------------------------------------------===//
387 // AST reader implementation
388 //===----------------------------------------------------------------------===//
391 ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
392 DeserializationListener = Listener;
397 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
398 return serialization::ComputeHash(Sel);
402 std::pair<unsigned, unsigned>
403 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
404 using namespace clang::io;
405 unsigned KeyLen = ReadUnalignedLE16(d);
406 unsigned DataLen = ReadUnalignedLE16(d);
407 return std::make_pair(KeyLen, DataLen);
410 ASTSelectorLookupTrait::internal_key_type
411 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
412 using namespace clang::io;
413 SelectorTable &SelTable = Reader.getContext().Selectors;
414 unsigned N = ReadUnalignedLE16(d);
415 IdentifierInfo *FirstII
416 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
418 return SelTable.getNullarySelector(FirstII);
420 return SelTable.getUnarySelector(FirstII);
422 SmallVector<IdentifierInfo *, 16> Args;
423 Args.push_back(FirstII);
424 for (unsigned I = 1; I != N; ++I)
425 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)));
427 return SelTable.getSelector(N, Args.data());
430 ASTSelectorLookupTrait::data_type
431 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
433 using namespace clang::io;
437 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d));
438 unsigned NumInstanceMethodsAndBits = ReadUnalignedLE16(d);
439 unsigned NumFactoryMethodsAndBits = ReadUnalignedLE16(d);
440 Result.InstanceBits = NumInstanceMethodsAndBits & 0x3;
441 Result.FactoryBits = NumFactoryMethodsAndBits & 0x3;
442 unsigned NumInstanceMethods = NumInstanceMethodsAndBits >> 2;
443 unsigned NumFactoryMethods = NumFactoryMethodsAndBits >> 2;
445 // Load instance methods
446 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
447 if (ObjCMethodDecl *Method
448 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
449 Result.Instance.push_back(Method);
452 // Load factory methods
453 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
454 if (ObjCMethodDecl *Method
455 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
456 Result.Factory.push_back(Method);
462 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
463 return llvm::HashString(a);
466 std::pair<unsigned, unsigned>
467 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
468 using namespace clang::io;
469 unsigned DataLen = ReadUnalignedLE16(d);
470 unsigned KeyLen = ReadUnalignedLE16(d);
471 return std::make_pair(KeyLen, DataLen);
474 ASTIdentifierLookupTraitBase::internal_key_type
475 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
476 assert(n >= 2 && d[n-1] == '\0');
477 return StringRef((const char*) d, n-1);
480 /// \brief Whether the given identifier is "interesting".
481 static bool isInterestingIdentifier(IdentifierInfo &II) {
482 return II.isPoisoned() ||
483 II.isExtensionToken() ||
484 II.getObjCOrBuiltinID() ||
485 II.hasRevertedTokenIDToIdentifier() ||
486 II.hadMacroDefinition() ||
487 II.getFETokenInfo<void>();
490 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
491 const unsigned char* d,
493 using namespace clang::io;
494 unsigned RawID = ReadUnalignedLE32(d);
495 bool IsInteresting = RawID & 0x01;
497 // Wipe out the "is interesting" bit.
500 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
501 if (!IsInteresting) {
502 // For uninteresting identifiers, just build the IdentifierInfo
503 // and associate it with the persistent ID.
504 IdentifierInfo *II = KnownII;
506 II = &Reader.getIdentifierTable().getOwn(k);
509 Reader.SetIdentifierInfo(ID, II);
510 if (!II->isFromAST()) {
511 bool WasInteresting = isInterestingIdentifier(*II);
514 II->setChangedSinceDeserialization();
516 Reader.markIdentifierUpToDate(II);
520 unsigned ObjCOrBuiltinID = ReadUnalignedLE16(d);
521 unsigned Bits = ReadUnalignedLE16(d);
522 bool CPlusPlusOperatorKeyword = Bits & 0x01;
524 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
526 bool Poisoned = Bits & 0x01;
528 bool ExtensionToken = Bits & 0x01;
530 bool hasSubmoduleMacros = Bits & 0x01;
532 bool hadMacroDefinition = Bits & 0x01;
535 assert(Bits == 0 && "Extra bits in the identifier?");
538 // Build the IdentifierInfo itself and link the identifier ID with
539 // the new IdentifierInfo.
540 IdentifierInfo *II = KnownII;
542 II = &Reader.getIdentifierTable().getOwn(StringRef(k));
545 Reader.markIdentifierUpToDate(II);
546 if (!II->isFromAST()) {
547 bool WasInteresting = isInterestingIdentifier(*II);
550 II->setChangedSinceDeserialization();
553 // Set or check the various bits in the IdentifierInfo structure.
554 // Token IDs are read-only.
555 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
556 II->RevertTokenIDToIdentifier();
557 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
558 assert(II->isExtensionToken() == ExtensionToken &&
559 "Incorrect extension token flag");
560 (void)ExtensionToken;
562 II->setIsPoisoned(true);
563 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
564 "Incorrect C++ operator keyword flag");
565 (void)CPlusPlusOperatorKeyword;
567 // If this identifier is a macro, deserialize the macro
569 if (hadMacroDefinition) {
570 uint32_t MacroDirectivesOffset = ReadUnalignedLE32(d);
572 SmallVector<uint32_t, 8> LocalMacroIDs;
573 if (hasSubmoduleMacros) {
574 while (uint32_t LocalMacroID = ReadUnalignedLE32(d)) {
576 LocalMacroIDs.push_back(LocalMacroID);
581 if (F.Kind == MK_Module) {
582 for (SmallVectorImpl<uint32_t>::iterator
583 I = LocalMacroIDs.begin(), E = LocalMacroIDs.end(); I != E; ++I) {
584 MacroID MacID = Reader.getGlobalMacroID(F, *I);
585 Reader.addPendingMacroFromModule(II, &F, MacID, F.DirectImportLoc);
588 Reader.addPendingMacroFromPCH(II, &F, MacroDirectivesOffset);
592 Reader.SetIdentifierInfo(ID, II);
594 // Read all of the declarations visible at global scope with this
597 SmallVector<uint32_t, 4> DeclIDs;
598 for (; DataLen > 0; DataLen -= 4)
599 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
600 Reader.SetGloballyVisibleDecls(II, DeclIDs);
607 ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
608 llvm::FoldingSetNodeID ID;
609 ID.AddInteger(Key.Kind);
612 case DeclarationName::Identifier:
613 case DeclarationName::CXXLiteralOperatorName:
614 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
616 case DeclarationName::ObjCZeroArgSelector:
617 case DeclarationName::ObjCOneArgSelector:
618 case DeclarationName::ObjCMultiArgSelector:
619 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
621 case DeclarationName::CXXOperatorName:
622 ID.AddInteger((OverloadedOperatorKind)Key.Data);
624 case DeclarationName::CXXConstructorName:
625 case DeclarationName::CXXDestructorName:
626 case DeclarationName::CXXConversionFunctionName:
627 case DeclarationName::CXXUsingDirective:
631 return ID.ComputeHash();
634 ASTDeclContextNameLookupTrait::internal_key_type
635 ASTDeclContextNameLookupTrait::GetInternalKey(
636 const external_key_type& Name) const {
638 Key.Kind = Name.getNameKind();
639 switch (Name.getNameKind()) {
640 case DeclarationName::Identifier:
641 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
643 case DeclarationName::ObjCZeroArgSelector:
644 case DeclarationName::ObjCOneArgSelector:
645 case DeclarationName::ObjCMultiArgSelector:
646 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
648 case DeclarationName::CXXOperatorName:
649 Key.Data = Name.getCXXOverloadedOperator();
651 case DeclarationName::CXXLiteralOperatorName:
652 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
654 case DeclarationName::CXXConstructorName:
655 case DeclarationName::CXXDestructorName:
656 case DeclarationName::CXXConversionFunctionName:
657 case DeclarationName::CXXUsingDirective:
665 std::pair<unsigned, unsigned>
666 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
667 using namespace clang::io;
668 unsigned KeyLen = ReadUnalignedLE16(d);
669 unsigned DataLen = ReadUnalignedLE16(d);
670 return std::make_pair(KeyLen, DataLen);
673 ASTDeclContextNameLookupTrait::internal_key_type
674 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
675 using namespace clang::io;
678 Key.Kind = (DeclarationName::NameKind)*d++;
680 case DeclarationName::Identifier:
681 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
683 case DeclarationName::ObjCZeroArgSelector:
684 case DeclarationName::ObjCOneArgSelector:
685 case DeclarationName::ObjCMultiArgSelector:
687 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
690 case DeclarationName::CXXOperatorName:
691 Key.Data = *d++; // OverloadedOperatorKind
693 case DeclarationName::CXXLiteralOperatorName:
694 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
696 case DeclarationName::CXXConstructorName:
697 case DeclarationName::CXXDestructorName:
698 case DeclarationName::CXXConversionFunctionName:
699 case DeclarationName::CXXUsingDirective:
707 ASTDeclContextNameLookupTrait::data_type
708 ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
709 const unsigned char* d,
711 using namespace clang::io;
712 unsigned NumDecls = ReadUnalignedLE16(d);
713 LE32DeclID *Start = reinterpret_cast<LE32DeclID *>(
714 const_cast<unsigned char *>(d));
715 return std::make_pair(Start, Start + NumDecls);
718 bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
719 BitstreamCursor &Cursor,
720 const std::pair<uint64_t, uint64_t> &Offsets,
721 DeclContextInfo &Info) {
722 SavedStreamPosition SavedPosition(Cursor);
723 // First the lexical decls.
724 if (Offsets.first != 0) {
725 Cursor.JumpToBit(Offsets.first);
729 unsigned Code = Cursor.ReadCode();
730 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
731 if (RecCode != DECL_CONTEXT_LEXICAL) {
732 Error("Expected lexical block");
736 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob.data());
737 Info.NumLexicalDecls = Blob.size() / sizeof(KindDeclIDPair);
740 // Now the lookup table.
741 if (Offsets.second != 0) {
742 Cursor.JumpToBit(Offsets.second);
746 unsigned Code = Cursor.ReadCode();
747 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
748 if (RecCode != DECL_CONTEXT_VISIBLE) {
749 Error("Expected visible lookup table block");
752 Info.NameLookupTableData
753 = ASTDeclContextNameLookupTable::Create(
754 (const unsigned char *)Blob.data() + Record[0],
755 (const unsigned char *)Blob.data(),
756 ASTDeclContextNameLookupTrait(*this, M));
762 void ASTReader::Error(StringRef Msg) {
763 Error(diag::err_fe_pch_malformed, Msg);
764 if (Context.getLangOpts().Modules && !Diags.isDiagnosticInFlight()) {
765 Diag(diag::note_module_cache_path)
766 << PP.getHeaderSearchInfo().getModuleCachePath();
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 RecordDataImpl &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.HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>
1287 ((Flags >> 6) & 0x03);
1288 HFI.isImport = (Flags >> 5) & 0x01;
1289 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1290 HFI.DirInfo = (Flags >> 2) & 0x03;
1291 HFI.Resolved = (Flags >> 1) & 0x01;
1292 HFI.IndexHeaderMapHeader = Flags & 0x01;
1293 HFI.NumIncludes = ReadUnalignedLE16(d);
1294 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1295 ReadUnalignedLE32(d));
1296 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1297 // The framework offset is 1 greater than the actual offset,
1298 // since 0 is used as an indicator for "no framework name".
1299 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1300 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1304 uint32_t LocalSMID = ReadUnalignedLE32(d);
1306 // This header is part of a module. Associate it with the module to enable
1307 // implicit module import.
1308 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1309 Module *Mod = Reader.getSubmodule(GlobalSMID);
1310 HFI.isModuleHeader = true;
1311 FileManager &FileMgr = Reader.getFileManager();
1313 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1314 ModMap.addHeader(Mod, FileMgr.getFile(key.Filename), HFI.getHeaderRole());
1318 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1321 // This HeaderFileInfo was externally loaded.
1322 HFI.External = true;
1326 void ASTReader::addPendingMacroFromModule(IdentifierInfo *II,
1328 GlobalMacroID GMacID,
1329 SourceLocation ImportLoc) {
1330 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1331 PendingMacroIDs[II].push_back(PendingMacroInfo(M, GMacID, ImportLoc));
1334 void ASTReader::addPendingMacroFromPCH(IdentifierInfo *II,
1336 uint64_t MacroDirectivesOffset) {
1337 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1338 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1341 void ASTReader::ReadDefinedMacros() {
1342 // Note that we are loading defined macros.
1343 Deserializing Macros(this);
1345 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1346 E = ModuleMgr.rend(); I != E; ++I) {
1347 BitstreamCursor &MacroCursor = (*I)->MacroCursor;
1349 // If there was no preprocessor block, skip this file.
1350 if (!MacroCursor.getBitStreamReader())
1353 BitstreamCursor Cursor = MacroCursor;
1354 Cursor.JumpToBit((*I)->MacroStartOffset);
1358 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1361 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1362 case llvm::BitstreamEntry::Error:
1363 Error("malformed block record in AST file");
1365 case llvm::BitstreamEntry::EndBlock:
1368 case llvm::BitstreamEntry::Record:
1370 switch (Cursor.readRecord(E.ID, Record)) {
1371 default: // Default behavior: ignore.
1374 case PP_MACRO_OBJECT_LIKE:
1375 case PP_MACRO_FUNCTION_LIKE:
1376 getLocalIdentifier(**I, Record[0]);
1391 /// \brief Visitor class used to look up identifirs in an AST file.
1392 class IdentifierLookupVisitor {
1394 unsigned PriorGeneration;
1395 unsigned &NumIdentifierLookups;
1396 unsigned &NumIdentifierLookupHits;
1397 IdentifierInfo *Found;
1400 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1401 unsigned &NumIdentifierLookups,
1402 unsigned &NumIdentifierLookupHits)
1403 : Name(Name), PriorGeneration(PriorGeneration),
1404 NumIdentifierLookups(NumIdentifierLookups),
1405 NumIdentifierLookupHits(NumIdentifierLookupHits),
1410 static bool visit(ModuleFile &M, void *UserData) {
1411 IdentifierLookupVisitor *This
1412 = static_cast<IdentifierLookupVisitor *>(UserData);
1414 // If we've already searched this module file, skip it now.
1415 if (M.Generation <= This->PriorGeneration)
1418 ASTIdentifierLookupTable *IdTable
1419 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1423 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1425 ++This->NumIdentifierLookups;
1426 ASTIdentifierLookupTable::iterator Pos = IdTable->find(This->Name,&Trait);
1427 if (Pos == IdTable->end())
1430 // Dereferencing the iterator has the effect of building the
1431 // IdentifierInfo node and populating it with the various
1432 // declarations it needs.
1433 ++This->NumIdentifierLookupHits;
1438 // \brief Retrieve the identifier info found within the module
1440 IdentifierInfo *getIdentifierInfo() const { return Found; }
1444 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1445 // Note that we are loading an identifier.
1446 Deserializing AnIdentifier(this);
1448 unsigned PriorGeneration = 0;
1449 if (getContext().getLangOpts().Modules)
1450 PriorGeneration = IdentifierGeneration[&II];
1452 // If there is a global index, look there first to determine which modules
1453 // provably do not have any results for this identifier.
1454 GlobalModuleIndex::HitSet Hits;
1455 GlobalModuleIndex::HitSet *HitsPtr = 0;
1456 if (!loadGlobalIndex()) {
1457 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1462 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
1463 NumIdentifierLookups,
1464 NumIdentifierLookupHits);
1465 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
1466 markIdentifierUpToDate(&II);
1469 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1473 II->setOutOfDate(false);
1475 // Update the generation for this identifier.
1476 if (getContext().getLangOpts().Modules)
1477 IdentifierGeneration[II] = CurrentGeneration;
1480 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1481 const PendingMacroInfo &PMInfo) {
1484 if (PMInfo.M->Kind != MK_Module) {
1485 installPCHMacroDirectives(II, *PMInfo.M,
1486 PMInfo.PCHMacroData.MacroDirectivesOffset);
1492 GlobalMacroID GMacID = PMInfo.ModuleMacroData.GMacID;
1493 SourceLocation ImportLoc =
1494 SourceLocation::getFromRawEncoding(PMInfo.ModuleMacroData.ImportLoc);
1497 // If this macro has already been loaded, don't do so again.
1498 if (MacrosLoaded[GMacID - NUM_PREDEF_MACRO_IDS])
1501 MacroInfo *MI = getMacro(GMacID);
1502 SubmoduleID SubModID = MI->getOwningModuleID();
1503 MacroDirective *MD = PP.AllocateDefMacroDirective(MI, ImportLoc,
1504 /*isImported=*/true);
1506 // Determine whether this macro definition is visible.
1507 bool Hidden = false;
1510 if ((Owner = getSubmodule(SubModID))) {
1511 if (Owner->NameVisibility == Module::Hidden) {
1512 // The owning module is not visible, and this macro definition
1513 // should not be, either.
1516 // Note that this macro definition was hidden because its owning
1517 // module is not yet visible.
1518 HiddenNamesMap[Owner].push_back(HiddenName(II, MD));
1524 installImportedMacro(II, MD, Owner);
1527 void ASTReader::installPCHMacroDirectives(IdentifierInfo *II,
1528 ModuleFile &M, uint64_t Offset) {
1529 assert(M.Kind != MK_Module);
1531 BitstreamCursor &Cursor = M.MacroCursor;
1532 SavedStreamPosition SavedPosition(Cursor);
1533 Cursor.JumpToBit(Offset);
1535 llvm::BitstreamEntry Entry =
1536 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1537 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1538 Error("malformed block record in AST file");
1543 PreprocessorRecordTypes RecType =
1544 (PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record);
1545 if (RecType != PP_MACRO_DIRECTIVE_HISTORY) {
1546 Error("malformed block record in AST file");
1550 // Deserialize the macro directives history in reverse source-order.
1551 MacroDirective *Latest = 0, *Earliest = 0;
1552 unsigned Idx = 0, N = Record.size();
1554 MacroDirective *MD = 0;
1555 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
1556 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
1558 case MacroDirective::MD_Define: {
1559 GlobalMacroID GMacID = getGlobalMacroID(M, Record[Idx++]);
1560 MacroInfo *MI = getMacro(GMacID);
1561 bool isImported = Record[Idx++];
1562 bool isAmbiguous = Record[Idx++];
1563 DefMacroDirective *DefMD =
1564 PP.AllocateDefMacroDirective(MI, Loc, isImported);
1565 DefMD->setAmbiguous(isAmbiguous);
1569 case MacroDirective::MD_Undefine:
1570 MD = PP.AllocateUndefMacroDirective(Loc);
1572 case MacroDirective::MD_Visibility: {
1573 bool isPublic = Record[Idx++];
1574 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
1582 Earliest->setPrevious(MD);
1586 PP.setLoadedMacroDirective(II, Latest);
1589 /// \brief For the given macro definitions, check if they are both in system
1591 static bool areDefinedInSystemModules(MacroInfo *PrevMI, MacroInfo *NewMI,
1592 Module *NewOwner, ASTReader &Reader) {
1593 assert(PrevMI && NewMI);
1594 Module *PrevOwner = 0;
1595 if (SubmoduleID PrevModID = PrevMI->getOwningModuleID())
1596 PrevOwner = Reader.getSubmodule(PrevModID);
1597 SourceManager &SrcMgr = Reader.getSourceManager();
1599 = PrevOwner? PrevOwner->IsSystem
1600 : SrcMgr.isInSystemHeader(PrevMI->getDefinitionLoc());
1602 = NewOwner? NewOwner->IsSystem
1603 : SrcMgr.isInSystemHeader(NewMI->getDefinitionLoc());
1604 if (PrevOwner && PrevOwner == NewOwner)
1606 return PrevInSystem && NewInSystem;
1609 void ASTReader::installImportedMacro(IdentifierInfo *II, MacroDirective *MD,
1613 DefMacroDirective *DefMD = cast<DefMacroDirective>(MD);
1614 MacroDirective *Prev = PP.getMacroDirective(II);
1616 MacroDirective::DefInfo PrevDef = Prev->getDefinition();
1617 MacroInfo *PrevMI = PrevDef.getMacroInfo();
1618 MacroInfo *NewMI = DefMD->getInfo();
1619 if (NewMI != PrevMI && !PrevMI->isIdenticalTo(*NewMI, PP,
1620 /*Syntactically=*/true)) {
1621 // Before marking the macros as ambiguous, check if this is a case where
1622 // both macros are in system headers. If so, we trust that the system
1623 // did not get it wrong. This also handles cases where Clang's own
1624 // headers have a different spelling of certain system macros:
1625 // #define LONG_MAX __LONG_MAX__ (clang's limits.h)
1626 // #define LONG_MAX 0x7fffffffffffffffL (system's limits.h)
1627 if (!areDefinedInSystemModules(PrevMI, NewMI, Owner, *this)) {
1628 PrevDef.getDirective()->setAmbiguous(true);
1629 DefMD->setAmbiguous(true);
1634 PP.appendMacroDirective(II, MD);
1637 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
1638 // If this ID is bogus, just return an empty input file.
1639 if (ID == 0 || ID > F.InputFilesLoaded.size())
1642 // If we've already loaded this input file, return it.
1643 if (F.InputFilesLoaded[ID-1].getFile())
1644 return F.InputFilesLoaded[ID-1];
1646 // Go find this input file.
1647 BitstreamCursor &Cursor = F.InputFilesCursor;
1648 SavedStreamPosition SavedPosition(Cursor);
1649 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1651 unsigned Code = Cursor.ReadCode();
1654 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
1656 unsigned StoredID = Record[0];
1657 assert(ID == StoredID && "Bogus stored ID or offset");
1659 off_t StoredSize = (off_t)Record[1];
1660 time_t StoredTime = (time_t)Record[2];
1661 bool Overridden = (bool)Record[3];
1663 // Get the file entry for this input file.
1664 StringRef OrigFilename = Blob;
1665 std::string Filename = OrigFilename;
1666 MaybeAddSystemRootToFilename(F, Filename);
1667 const FileEntry *File
1668 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
1669 : FileMgr.getFile(Filename, /*OpenFile=*/false);
1671 // If we didn't find the file, resolve it relative to the
1672 // original directory from which this AST file was created.
1673 if (File == 0 && !F.OriginalDir.empty() && !CurrentDir.empty() &&
1674 F.OriginalDir != CurrentDir) {
1675 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1678 if (!Resolved.empty())
1679 File = FileMgr.getFile(Resolved);
1682 // For an overridden file, create a virtual file with the stored
1684 if (Overridden && File == 0) {
1685 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
1690 std::string ErrorStr = "could not find file '";
1691 ErrorStr += Filename;
1692 ErrorStr += "' referenced by AST file";
1693 Error(ErrorStr.c_str());
1698 // Check if there was a request to override the contents of the file
1699 // that was part of the precompiled header. Overridding such a file
1700 // can lead to problems when lexing using the source locations from the
1702 SourceManager &SM = getSourceManager();
1703 if (!Overridden && SM.isFileOverridden(File)) {
1705 Error(diag::err_fe_pch_file_overridden, Filename);
1706 // After emitting the diagnostic, recover by disabling the override so
1707 // that the original file will be used.
1708 SM.disableFileContentsOverride(File);
1709 // The FileEntry is a virtual file entry with the size of the contents
1710 // that would override the original contents. Set it to the original's
1712 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1713 StoredSize, StoredTime);
1716 bool IsOutOfDate = false;
1718 // For an overridden file, there is nothing to validate.
1719 if (!Overridden && (StoredSize != File->getSize()
1720 #if !defined(LLVM_ON_WIN32)
1721 // In our regression testing, the Windows file system seems to
1722 // have inconsistent modification times that sometimes
1723 // erroneously trigger this error-handling path.
1724 || StoredTime != File->getModificationTime()
1728 Error(diag::err_fe_pch_file_modified, Filename, F.FileName);
1729 if (Context.getLangOpts().Modules && !Diags.isDiagnosticInFlight()) {
1730 Diag(diag::note_module_cache_path)
1731 << PP.getHeaderSearchInfo().getModuleCachePath();
1738 InputFile IF = InputFile(File, Overridden, IsOutOfDate);
1740 // Note that we've loaded this input file.
1741 F.InputFilesLoaded[ID-1] = IF;
1749 const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
1750 ModuleFile &M = ModuleMgr.getPrimaryModule();
1751 std::string Filename = filenameStrRef;
1752 MaybeAddSystemRootToFilename(M, Filename);
1753 const FileEntry *File = FileMgr.getFile(Filename);
1754 if (File == 0 && !M.OriginalDir.empty() && !CurrentDir.empty() &&
1755 M.OriginalDir != CurrentDir) {
1756 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1759 if (!resolved.empty())
1760 File = FileMgr.getFile(resolved);
1766 /// \brief If we are loading a relocatable PCH file, and the filename is
1767 /// not an absolute path, add the system root to the beginning of the file
1769 void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
1770 std::string &Filename) {
1771 // If this is not a relocatable PCH file, there's nothing to do.
1772 if (!M.RelocatablePCH)
1775 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
1778 if (isysroot.empty()) {
1779 // If no system root was given, default to '/'
1780 Filename.insert(Filename.begin(), '/');
1784 unsigned Length = isysroot.size();
1785 if (isysroot[Length - 1] != '/')
1786 Filename.insert(Filename.begin(), '/');
1788 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
1791 ASTReader::ASTReadResult
1792 ASTReader::ReadControlBlock(ModuleFile &F,
1793 SmallVectorImpl<ImportedModule> &Loaded,
1794 unsigned ClientLoadCapabilities) {
1795 BitstreamCursor &Stream = F.Stream;
1797 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
1798 Error("malformed block record in AST file");
1802 // Read all of the records and blocks in the control block.
1805 llvm::BitstreamEntry Entry = Stream.advance();
1807 switch (Entry.Kind) {
1808 case llvm::BitstreamEntry::Error:
1809 Error("malformed block record in AST file");
1811 case llvm::BitstreamEntry::EndBlock:
1812 // Validate all of the non-system input files.
1813 if (!DisableValidation) {
1814 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
1815 // All user input files reside at the index range [0, Record[1]).
1816 // Record is the one from INPUT_FILE_OFFSETS.
1817 for (unsigned I = 0, N = Record[1]; I < N; ++I) {
1818 InputFile IF = getInputFile(F, I+1, Complain);
1819 if (!IF.getFile() || IF.isOutOfDate())
1825 case llvm::BitstreamEntry::SubBlock:
1827 case INPUT_FILES_BLOCK_ID:
1828 F.InputFilesCursor = Stream;
1829 if (Stream.SkipBlock() || // Skip with the main cursor
1830 // Read the abbreviations
1831 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
1832 Error("malformed block record in AST file");
1838 if (Stream.SkipBlock()) {
1839 Error("malformed block record in AST file");
1845 case llvm::BitstreamEntry::Record:
1846 // The interesting case.
1850 // Read and process a record.
1853 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
1855 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1856 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1857 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
1858 : diag::warn_pch_version_too_new);
1859 return VersionMismatch;
1862 bool hasErrors = Record[5];
1863 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1864 Diag(diag::err_pch_with_compiler_errors);
1868 F.RelocatablePCH = Record[4];
1870 const std::string &CurBranch = getClangFullRepositoryVersion();
1871 StringRef ASTBranch = Blob;
1872 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
1873 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1874 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
1875 return VersionMismatch;
1881 // Load each of the imported PCH files.
1882 unsigned Idx = 0, N = Record.size();
1884 // Read information about the AST file.
1885 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1886 // The import location will be the local one for now; we will adjust
1887 // all import locations of module imports after the global source
1888 // location info are setup.
1889 SourceLocation ImportLoc =
1890 SourceLocation::getFromRawEncoding(Record[Idx++]);
1891 off_t StoredSize = (off_t)Record[Idx++];
1892 time_t StoredModTime = (time_t)Record[Idx++];
1893 unsigned Length = Record[Idx++];
1894 SmallString<128> ImportedFile(Record.begin() + Idx,
1895 Record.begin() + Idx + Length);
1898 // Load the AST file.
1899 switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded,
1900 StoredSize, StoredModTime,
1901 ClientLoadCapabilities)) {
1902 case Failure: return Failure;
1903 // If we have to ignore the dependency, we'll have to ignore this too.
1905 case OutOfDate: return OutOfDate;
1906 case VersionMismatch: return VersionMismatch;
1907 case ConfigurationMismatch: return ConfigurationMismatch;
1908 case HadErrors: return HadErrors;
1909 case Success: break;
1915 case LANGUAGE_OPTIONS: {
1916 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
1917 if (Listener && &F == *ModuleMgr.begin() &&
1918 ParseLanguageOptions(Record, Complain, *Listener) &&
1920 return ConfigurationMismatch;
1924 case TARGET_OPTIONS: {
1925 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1926 if (Listener && &F == *ModuleMgr.begin() &&
1927 ParseTargetOptions(Record, Complain, *Listener) &&
1929 return ConfigurationMismatch;
1933 case DIAGNOSTIC_OPTIONS: {
1934 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1935 if (Listener && &F == *ModuleMgr.begin() &&
1936 ParseDiagnosticOptions(Record, Complain, *Listener) &&
1938 return ConfigurationMismatch;
1942 case FILE_SYSTEM_OPTIONS: {
1943 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1944 if (Listener && &F == *ModuleMgr.begin() &&
1945 ParseFileSystemOptions(Record, Complain, *Listener) &&
1947 return ConfigurationMismatch;
1951 case HEADER_SEARCH_OPTIONS: {
1952 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1953 if (Listener && &F == *ModuleMgr.begin() &&
1954 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
1956 return ConfigurationMismatch;
1960 case PREPROCESSOR_OPTIONS: {
1961 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1962 if (Listener && &F == *ModuleMgr.begin() &&
1963 ParsePreprocessorOptions(Record, Complain, *Listener,
1964 SuggestedPredefines) &&
1966 return ConfigurationMismatch;
1971 F.OriginalSourceFileID = FileID::get(Record[0]);
1972 F.ActualOriginalSourceFileName = Blob;
1973 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
1974 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
1977 case ORIGINAL_FILE_ID:
1978 F.OriginalSourceFileID = FileID::get(Record[0]);
1981 case ORIGINAL_PCH_DIR:
1982 F.OriginalDir = Blob;
1985 case INPUT_FILE_OFFSETS:
1986 F.InputFileOffsets = (const uint32_t *)Blob.data();
1987 F.InputFilesLoaded.resize(Record[0]);
1993 bool ASTReader::ReadASTBlock(ModuleFile &F) {
1994 BitstreamCursor &Stream = F.Stream;
1996 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
1997 Error("malformed block record in AST file");
2001 // Read all of the records and blocks for the AST file.
2004 llvm::BitstreamEntry Entry = Stream.advance();
2006 switch (Entry.Kind) {
2007 case llvm::BitstreamEntry::Error:
2008 Error("error at end of module block in AST file");
2010 case llvm::BitstreamEntry::EndBlock: {
2011 // Outside of C++, we do not store a lookup map for the translation unit.
2012 // Instead, mark it as needing a lookup map to be built if this module
2013 // contains any declarations lexically within it (which it always does!).
2014 // This usually has no cost, since we very rarely need the lookup map for
2015 // the translation unit outside C++.
2016 DeclContext *DC = Context.getTranslationUnitDecl();
2017 if (DC->hasExternalLexicalStorage() &&
2018 !getContext().getLangOpts().CPlusPlus)
2019 DC->setMustBuildLookupTable();
2023 case llvm::BitstreamEntry::SubBlock:
2025 case DECLTYPES_BLOCK_ID:
2026 // We lazily load the decls block, but we want to set up the
2027 // DeclsCursor cursor to point into it. Clone our current bitcode
2028 // cursor to it, enter the block and read the abbrevs in that block.
2029 // With the main cursor, we just skip over it.
2030 F.DeclsCursor = Stream;
2031 if (Stream.SkipBlock() || // Skip with the main cursor.
2032 // Read the abbrevs.
2033 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2034 Error("malformed block record in AST file");
2039 case DECL_UPDATES_BLOCK_ID:
2040 if (Stream.SkipBlock()) {
2041 Error("malformed block record in AST file");
2046 case PREPROCESSOR_BLOCK_ID:
2047 F.MacroCursor = Stream;
2048 if (!PP.getExternalSource())
2049 PP.setExternalSource(this);
2051 if (Stream.SkipBlock() ||
2052 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2053 Error("malformed block record in AST file");
2056 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2059 case PREPROCESSOR_DETAIL_BLOCK_ID:
2060 F.PreprocessorDetailCursor = Stream;
2061 if (Stream.SkipBlock() ||
2062 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
2063 PREPROCESSOR_DETAIL_BLOCK_ID)) {
2064 Error("malformed preprocessor detail record in AST file");
2067 F.PreprocessorDetailStartOffset
2068 = F.PreprocessorDetailCursor.GetCurrentBitNo();
2070 if (!PP.getPreprocessingRecord())
2071 PP.createPreprocessingRecord();
2072 if (!PP.getPreprocessingRecord()->getExternalSource())
2073 PP.getPreprocessingRecord()->SetExternalSource(*this);
2076 case SOURCE_MANAGER_BLOCK_ID:
2077 if (ReadSourceManagerBlock(F))
2081 case SUBMODULE_BLOCK_ID:
2082 if (ReadSubmoduleBlock(F))
2086 case COMMENTS_BLOCK_ID: {
2087 BitstreamCursor C = Stream;
2088 if (Stream.SkipBlock() ||
2089 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2090 Error("malformed comments block in AST file");
2093 CommentsCursors.push_back(std::make_pair(C, &F));
2098 if (Stream.SkipBlock()) {
2099 Error("malformed block record in AST file");
2106 case llvm::BitstreamEntry::Record:
2107 // The interesting case.
2111 // Read and process a record.
2114 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
2115 default: // Default behavior: ignore.
2119 if (F.LocalNumTypes != 0) {
2120 Error("duplicate TYPE_OFFSET record in AST file");
2123 F.TypeOffsets = (const uint32_t *)Blob.data();
2124 F.LocalNumTypes = Record[0];
2125 unsigned LocalBaseTypeIndex = Record[1];
2126 F.BaseTypeIndex = getTotalNumTypes();
2128 if (F.LocalNumTypes > 0) {
2129 // Introduce the global -> local mapping for types within this module.
2130 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2132 // Introduce the local -> global mapping for types within this module.
2133 F.TypeRemap.insertOrReplace(
2134 std::make_pair(LocalBaseTypeIndex,
2135 F.BaseTypeIndex - LocalBaseTypeIndex));
2137 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2143 if (F.LocalNumDecls != 0) {
2144 Error("duplicate DECL_OFFSET record in AST file");
2147 F.DeclOffsets = (const DeclOffset *)Blob.data();
2148 F.LocalNumDecls = Record[0];
2149 unsigned LocalBaseDeclID = Record[1];
2150 F.BaseDeclID = getTotalNumDecls();
2152 if (F.LocalNumDecls > 0) {
2153 // Introduce the global -> local mapping for declarations within this
2155 GlobalDeclMap.insert(
2156 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2158 // Introduce the local -> global mapping for declarations within this
2160 F.DeclRemap.insertOrReplace(
2161 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2163 // Introduce the global -> local mapping for declarations within this
2165 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2167 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2172 case TU_UPDATE_LEXICAL: {
2173 DeclContext *TU = Context.getTranslationUnitDecl();
2174 DeclContextInfo &Info = F.DeclContextInfos[TU];
2175 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(Blob.data());
2176 Info.NumLexicalDecls
2177 = static_cast<unsigned int>(Blob.size() / sizeof(KindDeclIDPair));
2178 TU->setHasExternalLexicalStorage(true);
2182 case UPDATE_VISIBLE: {
2184 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2185 ASTDeclContextNameLookupTable *Table =
2186 ASTDeclContextNameLookupTable::Create(
2187 (const unsigned char *)Blob.data() + Record[Idx++],
2188 (const unsigned char *)Blob.data(),
2189 ASTDeclContextNameLookupTrait(*this, F));
2190 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
2191 DeclContext *TU = Context.getTranslationUnitDecl();
2192 F.DeclContextInfos[TU].NameLookupTableData = Table;
2193 TU->setHasExternalVisibleStorage(true);
2195 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
2199 case IDENTIFIER_TABLE:
2200 F.IdentifierTableData = Blob.data();
2202 F.IdentifierLookupTable
2203 = ASTIdentifierLookupTable::Create(
2204 (const unsigned char *)F.IdentifierTableData + Record[0],
2205 (const unsigned char *)F.IdentifierTableData,
2206 ASTIdentifierLookupTrait(*this, F));
2208 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2212 case IDENTIFIER_OFFSET: {
2213 if (F.LocalNumIdentifiers != 0) {
2214 Error("duplicate IDENTIFIER_OFFSET record in AST file");
2217 F.IdentifierOffsets = (const uint32_t *)Blob.data();
2218 F.LocalNumIdentifiers = Record[0];
2219 unsigned LocalBaseIdentifierID = Record[1];
2220 F.BaseIdentifierID = getTotalNumIdentifiers();
2222 if (F.LocalNumIdentifiers > 0) {
2223 // Introduce the global -> local mapping for identifiers within this
2225 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2228 // Introduce the local -> global mapping for identifiers within this
2230 F.IdentifierRemap.insertOrReplace(
2231 std::make_pair(LocalBaseIdentifierID,
2232 F.BaseIdentifierID - LocalBaseIdentifierID));
2234 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2235 + F.LocalNumIdentifiers);
2240 case EXTERNAL_DEFINITIONS:
2241 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2242 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
2246 if (SpecialTypes.empty()) {
2247 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2248 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2252 if (SpecialTypes.size() != Record.size()) {
2253 Error("invalid special-types record");
2257 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2258 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2259 if (!SpecialTypes[I])
2260 SpecialTypes[I] = ID;
2261 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2267 TotalNumStatements += Record[0];
2268 TotalNumMacros += Record[1];
2269 TotalLexicalDeclContexts += Record[2];
2270 TotalVisibleDeclContexts += Record[3];
2273 case UNUSED_FILESCOPED_DECLS:
2274 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2275 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2278 case DELEGATING_CTORS:
2279 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2280 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2283 case WEAK_UNDECLARED_IDENTIFIERS:
2284 if (Record.size() % 4 != 0) {
2285 Error("invalid weak identifiers record");
2289 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2290 // files. This isn't the way to do it :)
2291 WeakUndeclaredIdentifiers.clear();
2293 // Translate the weak, undeclared identifiers into global IDs.
2294 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2295 WeakUndeclaredIdentifiers.push_back(
2296 getGlobalIdentifierID(F, Record[I++]));
2297 WeakUndeclaredIdentifiers.push_back(
2298 getGlobalIdentifierID(F, Record[I++]));
2299 WeakUndeclaredIdentifiers.push_back(
2300 ReadSourceLocation(F, Record, I).getRawEncoding());
2301 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2305 case LOCALLY_SCOPED_EXTERN_C_DECLS:
2306 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2307 LocallyScopedExternCDecls.push_back(getGlobalDeclID(F, Record[I]));
2310 case SELECTOR_OFFSETS: {
2311 F.SelectorOffsets = (const uint32_t *)Blob.data();
2312 F.LocalNumSelectors = Record[0];
2313 unsigned LocalBaseSelectorID = Record[1];
2314 F.BaseSelectorID = getTotalNumSelectors();
2316 if (F.LocalNumSelectors > 0) {
2317 // Introduce the global -> local mapping for selectors within this
2319 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2321 // Introduce the local -> global mapping for selectors within this
2323 F.SelectorRemap.insertOrReplace(
2324 std::make_pair(LocalBaseSelectorID,
2325 F.BaseSelectorID - LocalBaseSelectorID));
2327 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2333 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
2335 F.SelectorLookupTable
2336 = ASTSelectorLookupTable::Create(
2337 F.SelectorLookupTableData + Record[0],
2338 F.SelectorLookupTableData,
2339 ASTSelectorLookupTrait(*this, F));
2340 TotalNumMethodPoolEntries += Record[1];
2343 case REFERENCED_SELECTOR_POOL:
2344 if (!Record.empty()) {
2345 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2346 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2348 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2354 case PP_COUNTER_VALUE:
2355 if (!Record.empty() && Listener)
2356 Listener->ReadCounter(F, Record[0]);
2359 case FILE_SORTED_DECLS:
2360 F.FileSortedDecls = (const DeclID *)Blob.data();
2361 F.NumFileSortedDecls = Record[0];
2364 case SOURCE_LOCATION_OFFSETS: {
2365 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
2366 F.LocalNumSLocEntries = Record[0];
2367 unsigned SLocSpaceSize = Record[1];
2368 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
2369 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2371 // Make our entry in the range map. BaseID is negative and growing, so
2372 // we invert it. Because we invert it, though, we need the other end of
2374 unsigned RangeStart =
2375 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2376 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2377 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2379 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2380 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2381 GlobalSLocOffsetMap.insert(
2382 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2383 - SLocSpaceSize,&F));
2385 // Initialize the remapping table.
2386 // Invalid stays invalid.
2387 F.SLocRemap.insert(std::make_pair(0U, 0));
2388 // This module. Base was 2 when being compiled.
2389 F.SLocRemap.insert(std::make_pair(2U,
2390 static_cast<int>(F.SLocEntryBaseOffset - 2)));
2392 TotalNumSLocEntries += F.LocalNumSLocEntries;
2396 case MODULE_OFFSET_MAP: {
2397 // Additional remapping information.
2398 const unsigned char *Data = (const unsigned char*)Blob.data();
2399 const unsigned char *DataEnd = Data + Blob.size();
2401 // Continuous range maps we may be updating in our module.
2402 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
2403 ContinuousRangeMap<uint32_t, int, 2>::Builder
2404 IdentifierRemap(F.IdentifierRemap);
2405 ContinuousRangeMap<uint32_t, int, 2>::Builder
2406 MacroRemap(F.MacroRemap);
2407 ContinuousRangeMap<uint32_t, int, 2>::Builder
2408 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2409 ContinuousRangeMap<uint32_t, int, 2>::Builder
2410 SubmoduleRemap(F.SubmoduleRemap);
2411 ContinuousRangeMap<uint32_t, int, 2>::Builder
2412 SelectorRemap(F.SelectorRemap);
2413 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
2414 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2416 while(Data < DataEnd) {
2417 uint16_t Len = io::ReadUnalignedLE16(Data);
2418 StringRef Name = StringRef((const char*)Data, Len);
2420 ModuleFile *OM = ModuleMgr.lookup(Name);
2422 Error("SourceLocation remap refers to unknown module");
2426 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2427 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
2428 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
2429 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
2430 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
2431 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2432 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
2433 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
2435 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2436 SLocRemap.insert(std::make_pair(SLocOffset,
2437 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
2438 IdentifierRemap.insert(
2439 std::make_pair(IdentifierIDOffset,
2440 OM->BaseIdentifierID - IdentifierIDOffset));
2441 MacroRemap.insert(std::make_pair(MacroIDOffset,
2442 OM->BaseMacroID - MacroIDOffset));
2443 PreprocessedEntityRemap.insert(
2444 std::make_pair(PreprocessedEntityIDOffset,
2445 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
2446 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2447 OM->BaseSubmoduleID - SubmoduleIDOffset));
2448 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2449 OM->BaseSelectorID - SelectorIDOffset));
2450 DeclRemap.insert(std::make_pair(DeclIDOffset,
2451 OM->BaseDeclID - DeclIDOffset));
2453 TypeRemap.insert(std::make_pair(TypeIndexOffset,
2454 OM->BaseTypeIndex - TypeIndexOffset));
2456 // Global -> local mappings.
2457 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
2462 case SOURCE_MANAGER_LINE_TABLE:
2463 if (ParseLineTable(F, Record))
2467 case SOURCE_LOCATION_PRELOADS: {
2468 // Need to transform from the local view (1-based IDs) to the global view,
2469 // which is based off F.SLocEntryBaseID.
2470 if (!F.PreloadSLocEntries.empty()) {
2471 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2475 F.PreloadSLocEntries.swap(Record);
2479 case EXT_VECTOR_DECLS:
2480 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2481 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
2485 if (Record.size() % 3 != 0) {
2486 Error("Invalid VTABLE_USES record");
2490 // Later tables overwrite earlier ones.
2491 // FIXME: Modules will have some trouble with this. This is clearly not
2492 // the right way to do this.
2495 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2496 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2497 VTableUses.push_back(
2498 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2499 VTableUses.push_back(Record[Idx++]);
2503 case DYNAMIC_CLASSES:
2504 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2505 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
2508 case PENDING_IMPLICIT_INSTANTIATIONS:
2509 if (PendingInstantiations.size() % 2 != 0) {
2510 Error("Invalid existing PendingInstantiations");
2514 if (Record.size() % 2 != 0) {
2515 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2519 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2520 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2521 PendingInstantiations.push_back(
2522 ReadSourceLocation(F, Record, I).getRawEncoding());
2526 case SEMA_DECL_REFS:
2527 if (Record.size() != 2) {
2528 Error("Invalid SEMA_DECL_REFS block");
2531 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2532 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2535 case PPD_ENTITIES_OFFSETS: {
2536 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
2537 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
2538 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
2540 unsigned LocalBasePreprocessedEntityID = Record[0];
2542 unsigned StartingID;
2543 if (!PP.getPreprocessingRecord())
2544 PP.createPreprocessingRecord();
2545 if (!PP.getPreprocessingRecord()->getExternalSource())
2546 PP.getPreprocessingRecord()->SetExternalSource(*this);
2548 = PP.getPreprocessingRecord()
2549 ->allocateLoadedEntities(F.NumPreprocessedEntities);
2550 F.BasePreprocessedEntityID = StartingID;
2552 if (F.NumPreprocessedEntities > 0) {
2553 // Introduce the global -> local mapping for preprocessed entities in
2555 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2557 // Introduce the local -> global mapping for preprocessed entities in
2559 F.PreprocessedEntityRemap.insertOrReplace(
2560 std::make_pair(LocalBasePreprocessedEntityID,
2561 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2567 case DECL_UPDATE_OFFSETS: {
2568 if (Record.size() % 2 != 0) {
2569 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2572 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2573 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2574 .push_back(std::make_pair(&F, Record[I+1]));
2578 case DECL_REPLACEMENTS: {
2579 if (Record.size() % 3 != 0) {
2580 Error("invalid DECL_REPLACEMENTS block in AST file");
2583 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
2584 ReplacedDecls[getGlobalDeclID(F, Record[I])]
2585 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
2589 case OBJC_CATEGORIES_MAP: {
2590 if (F.LocalNumObjCCategoriesInMap != 0) {
2591 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
2595 F.LocalNumObjCCategoriesInMap = Record[0];
2596 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
2600 case OBJC_CATEGORIES:
2601 F.ObjCCategories.swap(Record);
2604 case CXX_BASE_SPECIFIER_OFFSETS: {
2605 if (F.LocalNumCXXBaseSpecifiers != 0) {
2606 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2610 F.LocalNumCXXBaseSpecifiers = Record[0];
2611 F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data();
2612 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
2616 case DIAG_PRAGMA_MAPPINGS:
2617 if (F.PragmaDiagMappings.empty())
2618 F.PragmaDiagMappings.swap(Record);
2620 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2621 Record.begin(), Record.end());
2624 case CUDA_SPECIAL_DECL_REFS:
2625 // Later tables overwrite earlier ones.
2626 // FIXME: Modules will have trouble with this.
2627 CUDASpecialDeclRefs.clear();
2628 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2629 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2632 case HEADER_SEARCH_TABLE: {
2633 F.HeaderFileInfoTableData = Blob.data();
2634 F.LocalNumHeaderFileInfos = Record[1];
2636 F.HeaderFileInfoTable
2637 = HeaderFileInfoLookupTable::Create(
2638 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2639 (const unsigned char *)F.HeaderFileInfoTableData,
2640 HeaderFileInfoTrait(*this, F,
2641 &PP.getHeaderSearchInfo(),
2642 Blob.data() + Record[2]));
2644 PP.getHeaderSearchInfo().SetExternalSource(this);
2645 if (!PP.getHeaderSearchInfo().getExternalLookup())
2646 PP.getHeaderSearchInfo().SetExternalLookup(this);
2651 case FP_PRAGMA_OPTIONS:
2652 // Later tables overwrite earlier ones.
2653 FPPragmaOptions.swap(Record);
2656 case OPENCL_EXTENSIONS:
2657 // Later tables overwrite earlier ones.
2658 OpenCLExtensions.swap(Record);
2661 case TENTATIVE_DEFINITIONS:
2662 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2663 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
2666 case KNOWN_NAMESPACES:
2667 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2668 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
2671 case UNDEFINED_BUT_USED:
2672 if (UndefinedButUsed.size() % 2 != 0) {
2673 Error("Invalid existing UndefinedButUsed");
2677 if (Record.size() % 2 != 0) {
2678 Error("invalid undefined-but-used record");
2681 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2682 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
2683 UndefinedButUsed.push_back(
2684 ReadSourceLocation(F, Record, I).getRawEncoding());
2688 case IMPORTED_MODULES: {
2689 if (F.Kind != MK_Module) {
2690 // If we aren't loading a module (which has its own exports), make
2691 // all of the imported modules visible.
2692 // FIXME: Deal with macros-only imports.
2693 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2694 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2695 ImportedModules.push_back(GlobalID);
2701 case LOCAL_REDECLARATIONS: {
2702 F.RedeclarationChains.swap(Record);
2706 case LOCAL_REDECLARATIONS_MAP: {
2707 if (F.LocalNumRedeclarationsInMap != 0) {
2708 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
2712 F.LocalNumRedeclarationsInMap = Record[0];
2713 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data();
2717 case MERGED_DECLARATIONS: {
2718 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2719 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2720 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2721 for (unsigned N = Record[Idx++]; N > 0; --N)
2722 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2727 case MACRO_OFFSET: {
2728 if (F.LocalNumMacros != 0) {
2729 Error("duplicate MACRO_OFFSET record in AST file");
2732 F.MacroOffsets = (const uint32_t *)Blob.data();
2733 F.LocalNumMacros = Record[0];
2734 unsigned LocalBaseMacroID = Record[1];
2735 F.BaseMacroID = getTotalNumMacros();
2737 if (F.LocalNumMacros > 0) {
2738 // Introduce the global -> local mapping for macros within this module.
2739 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
2741 // Introduce the local -> global mapping for macros within this module.
2742 F.MacroRemap.insertOrReplace(
2743 std::make_pair(LocalBaseMacroID,
2744 F.BaseMacroID - LocalBaseMacroID));
2746 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
2752 // FIXME: Not used yet.
2756 case LATE_PARSED_TEMPLATE: {
2757 LateParsedTemplates.append(Record.begin(), Record.end());
2764 /// \brief Move the given method to the back of the global list of methods.
2765 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
2766 // Find the entry for this selector in the method pool.
2767 Sema::GlobalMethodPool::iterator Known
2768 = S.MethodPool.find(Method->getSelector());
2769 if (Known == S.MethodPool.end())
2772 // Retrieve the appropriate method list.
2773 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
2774 : Known->second.second;
2776 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
2778 if (List->Method == Method) {
2786 if (List->getNext())
2787 List->Method = List->getNext()->Method;
2789 List->Method = Method;
2793 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
2794 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
2795 switch (Names[I].getKind()) {
2796 case HiddenName::Declaration: {
2797 Decl *D = Names[I].getDecl();
2798 bool wasHidden = D->Hidden;
2801 if (wasHidden && SemaObj) {
2802 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
2803 moveMethodToBackOfGlobalList(*SemaObj, Method);
2808 case HiddenName::MacroVisibility: {
2809 std::pair<IdentifierInfo *, MacroDirective *> Macro = Names[I].getMacro();
2810 installImportedMacro(Macro.first, Macro.second, Owner);
2817 void ASTReader::makeModuleVisible(Module *Mod,
2818 Module::NameVisibilityKind NameVisibility,
2819 SourceLocation ImportLoc,
2821 llvm::SmallPtrSet<Module *, 4> Visited;
2822 SmallVector<Module *, 4> Stack;
2823 Stack.push_back(Mod);
2824 while (!Stack.empty()) {
2825 Mod = Stack.pop_back_val();
2827 if (NameVisibility <= Mod->NameVisibility) {
2828 // This module already has this level of visibility (or greater), so
2829 // there is nothing more to do.
2833 if (!Mod->isAvailable()) {
2834 // Modules that aren't available cannot be made visible.
2838 // Update the module's name visibility.
2839 Mod->NameVisibility = NameVisibility;
2841 // If we've already deserialized any names from this module,
2842 // mark them as visible.
2843 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2844 if (Hidden != HiddenNamesMap.end()) {
2845 makeNamesVisible(Hidden->second, Hidden->first);
2846 HiddenNamesMap.erase(Hidden);
2849 // Push any exported modules onto the stack to be marked as visible.
2850 SmallVector<Module *, 16> Exports;
2851 Mod->getExportedModules(Exports);
2852 for (SmallVectorImpl<Module *>::iterator
2853 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
2854 Module *Exported = *I;
2855 if (Visited.insert(Exported))
2856 Stack.push_back(Exported);
2859 // Detect any conflicts.
2861 assert(ImportLoc.isValid() && "Missing import location");
2862 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2863 if (Mod->Conflicts[I].Other->NameVisibility >= NameVisibility) {
2864 Diag(ImportLoc, diag::warn_module_conflict)
2865 << Mod->getFullModuleName()
2866 << Mod->Conflicts[I].Other->getFullModuleName()
2867 << Mod->Conflicts[I].Message;
2868 // FIXME: Need note where the other module was imported.
2875 bool ASTReader::loadGlobalIndex() {
2879 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
2880 !Context.getLangOpts().Modules)
2883 // Try to load the global index.
2884 TriedLoadingGlobalIndex = true;
2885 StringRef ModuleCachePath
2886 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
2887 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
2888 = GlobalModuleIndex::readIndex(ModuleCachePath);
2892 GlobalIndex.reset(Result.first);
2893 ModuleMgr.setGlobalIndex(GlobalIndex.get());
2897 bool ASTReader::isGlobalIndexUnavailable() const {
2898 return Context.getLangOpts().Modules && UseGlobalIndex &&
2899 !hasGlobalIndex() && TriedLoadingGlobalIndex;
2902 ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2904 SourceLocation ImportLoc,
2905 unsigned ClientLoadCapabilities) {
2906 llvm::SaveAndRestore<SourceLocation>
2907 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
2909 // Bump the generation number.
2910 unsigned PreviousGeneration = CurrentGeneration++;
2912 unsigned NumModules = ModuleMgr.size();
2913 SmallVector<ImportedModule, 4> Loaded;
2914 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
2915 /*ImportedBy=*/0, Loaded,
2917 ClientLoadCapabilities)) {
2921 case VersionMismatch:
2922 case ConfigurationMismatch:
2924 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
2925 Context.getLangOpts().Modules
2926 ? &PP.getHeaderSearchInfo().getModuleMap()
2929 // If we find that any modules are unusable, the global index is going
2930 // to be out-of-date. Just remove it.
2931 GlobalIndex.reset();
2932 ModuleMgr.setGlobalIndex(0);
2939 // Here comes stuff that we only do once the entire chain is loaded.
2941 // Load the AST blocks of all of the modules that we loaded.
2942 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
2943 MEnd = Loaded.end();
2945 ModuleFile &F = *M->Mod;
2947 // Read the AST block.
2948 if (ReadASTBlock(F))
2951 // Once read, set the ModuleFile bit base offset and update the size in
2952 // bits of all files we've seen.
2953 F.GlobalBitOffset = TotalModulesSizeInBits;
2954 TotalModulesSizeInBits += F.SizeInBits;
2955 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
2957 // Preload SLocEntries.
2958 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
2959 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
2960 // Load it through the SourceManager and don't call ReadSLocEntry()
2961 // directly because the entry may have already been loaded in which case
2962 // calling ReadSLocEntry() directly would trigger an assertion in
2964 SourceMgr.getLoadedSLocEntryByID(Index);
2968 // Setup the import locations and notify the module manager that we've
2969 // committed to these module files.
2970 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
2971 MEnd = Loaded.end();
2973 ModuleFile &F = *M->Mod;
2975 ModuleMgr.moduleFileAccepted(&F);
2977 // Set the import location.
2978 F.DirectImportLoc = ImportLoc;
2980 F.ImportLoc = M->ImportLoc;
2982 F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
2983 M->ImportLoc.getRawEncoding());
2986 // Mark all of the identifiers in the identifier table as being out of date,
2987 // so that various accessors know to check the loaded modules when the
2988 // identifier is used.
2989 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2990 IdEnd = PP.getIdentifierTable().end();
2992 Id->second->setOutOfDate(true);
2994 // Resolve any unresolved module exports.
2995 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
2996 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
2997 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
2998 Module *ResolvedMod = getSubmodule(GlobalID);
3000 switch (Unresolved.Kind) {
3001 case UnresolvedModuleRef::Conflict:
3003 Module::Conflict Conflict;
3004 Conflict.Other = ResolvedMod;
3005 Conflict.Message = Unresolved.String.str();
3006 Unresolved.Mod->Conflicts.push_back(Conflict);
3010 case UnresolvedModuleRef::Import:
3012 Unresolved.Mod->Imports.push_back(ResolvedMod);
3015 case UnresolvedModuleRef::Export:
3016 if (ResolvedMod || Unresolved.IsWildcard)
3017 Unresolved.Mod->Exports.push_back(
3018 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
3022 UnresolvedModuleRefs.clear();
3024 // FIXME: How do we load the 'use'd modules? They may not be submodules.
3025 // Might be unnecessary as use declarations are only used to build the
3028 InitializeContext();
3033 if (DeserializationListener)
3034 DeserializationListener->ReaderInitialized(this);
3036 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
3037 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
3038 PrimaryModule.OriginalSourceFileID
3039 = FileID::get(PrimaryModule.SLocEntryBaseID
3040 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
3042 // If this AST file is a precompiled preamble, then set the
3043 // preamble file ID of the source manager to the file source file
3044 // from which the preamble was built.
3045 if (Type == MK_Preamble) {
3046 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
3047 } else if (Type == MK_MainFile) {
3048 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
3052 // For any Objective-C class definitions we have already loaded, make sure
3053 // that we load any additional categories.
3054 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3055 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3056 ObjCClassesLoaded[I],
3057 PreviousGeneration);
3063 ASTReader::ASTReadResult
3064 ASTReader::ReadASTCore(StringRef FileName,
3066 SourceLocation ImportLoc,
3067 ModuleFile *ImportedBy,
3068 SmallVectorImpl<ImportedModule> &Loaded,
3069 off_t ExpectedSize, time_t ExpectedModTime,
3070 unsigned ClientLoadCapabilities) {
3072 std::string ErrorStr;
3073 ModuleManager::AddModuleResult AddResult
3074 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
3075 CurrentGeneration, ExpectedSize, ExpectedModTime,
3078 switch (AddResult) {
3079 case ModuleManager::AlreadyLoaded:
3082 case ModuleManager::NewlyLoaded:
3083 // Load module file below.
3086 case ModuleManager::Missing:
3087 // The module file was missing; if the client handle handle, that, return
3089 if (ClientLoadCapabilities & ARR_Missing)
3092 // Otherwise, return an error.
3094 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3100 case ModuleManager::OutOfDate:
3101 // We couldn't load the module file because it is out-of-date. If the
3102 // client can handle out-of-date, return it.
3103 if (ClientLoadCapabilities & ARR_OutOfDate)
3106 // Otherwise, return an error.
3108 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3115 assert(M && "Missing module file");
3117 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3119 if (FileName != "-") {
3120 CurrentDir = llvm::sys::path::parent_path(FileName);
3121 if (CurrentDir.empty()) CurrentDir = ".";
3125 BitstreamCursor &Stream = F.Stream;
3126 Stream.init(F.StreamFile);
3127 F.SizeInBits = F.Buffer->getBufferSize() * 8;
3129 // Sniff for the signature.
3130 if (Stream.Read(8) != 'C' ||
3131 Stream.Read(8) != 'P' ||
3132 Stream.Read(8) != 'C' ||
3133 Stream.Read(8) != 'H') {
3134 Diag(diag::err_not_a_pch_file) << FileName;
3138 // This is used for compatibility with older PCH formats.
3139 bool HaveReadControlBlock = false;
3142 llvm::BitstreamEntry Entry = Stream.advance();
3144 switch (Entry.Kind) {
3145 case llvm::BitstreamEntry::Error:
3146 case llvm::BitstreamEntry::EndBlock:
3147 case llvm::BitstreamEntry::Record:
3148 Error("invalid record at top-level of AST file");
3151 case llvm::BitstreamEntry::SubBlock:
3155 // We only know the control subblock ID.
3157 case llvm::bitc::BLOCKINFO_BLOCK_ID:
3158 if (Stream.ReadBlockInfoBlock()) {
3159 Error("malformed BlockInfoBlock in AST file");
3163 case CONTROL_BLOCK_ID:
3164 HaveReadControlBlock = true;
3165 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) {
3169 case Failure: return Failure;
3170 case Missing: return Missing;
3171 case OutOfDate: return OutOfDate;
3172 case VersionMismatch: return VersionMismatch;
3173 case ConfigurationMismatch: return ConfigurationMismatch;
3174 case HadErrors: return HadErrors;
3178 if (!HaveReadControlBlock) {
3179 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3180 Diag(diag::warn_pch_version_too_old);
3181 return VersionMismatch;
3184 // Record that we've loaded this module.
3185 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
3189 if (Stream.SkipBlock()) {
3190 Error("malformed block record in AST file");
3200 void ASTReader::InitializeContext() {
3201 // If there's a listener, notify them that we "read" the translation unit.
3202 if (DeserializationListener)
3203 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3204 Context.getTranslationUnitDecl());
3206 // Make sure we load the declaration update records for the translation unit,
3207 // if there are any.
3208 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
3209 Context.getTranslationUnitDecl());
3211 // FIXME: Find a better way to deal with collisions between these
3212 // built-in types. Right now, we just ignore the problem.
3214 // Load the special types.
3215 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
3216 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3217 if (!Context.CFConstantStringTypeDecl)
3218 Context.setCFConstantStringType(GetType(String));
3221 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3222 QualType FileType = GetType(File);
3223 if (FileType.isNull()) {
3224 Error("FILE type is NULL");
3228 if (!Context.FILEDecl) {
3229 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3230 Context.setFILEDecl(Typedef->getDecl());
3232 const TagType *Tag = FileType->getAs<TagType>();
3234 Error("Invalid FILE type in AST file");
3237 Context.setFILEDecl(Tag->getDecl());
3242 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
3243 QualType Jmp_bufType = GetType(Jmp_buf);
3244 if (Jmp_bufType.isNull()) {
3245 Error("jmp_buf type is NULL");
3249 if (!Context.jmp_bufDecl) {
3250 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3251 Context.setjmp_bufDecl(Typedef->getDecl());
3253 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3255 Error("Invalid jmp_buf type in AST file");
3258 Context.setjmp_bufDecl(Tag->getDecl());
3263 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
3264 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3265 if (Sigjmp_bufType.isNull()) {
3266 Error("sigjmp_buf type is NULL");
3270 if (!Context.sigjmp_bufDecl) {
3271 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3272 Context.setsigjmp_bufDecl(Typedef->getDecl());
3274 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3275 assert(Tag && "Invalid sigjmp_buf type in AST file");
3276 Context.setsigjmp_bufDecl(Tag->getDecl());
3281 if (unsigned ObjCIdRedef
3282 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3283 if (Context.ObjCIdRedefinitionType.isNull())
3284 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3287 if (unsigned ObjCClassRedef
3288 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3289 if (Context.ObjCClassRedefinitionType.isNull())
3290 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3293 if (unsigned ObjCSelRedef
3294 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3295 if (Context.ObjCSelRedefinitionType.isNull())
3296 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3299 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3300 QualType Ucontext_tType = GetType(Ucontext_t);
3301 if (Ucontext_tType.isNull()) {
3302 Error("ucontext_t type is NULL");
3306 if (!Context.ucontext_tDecl) {
3307 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3308 Context.setucontext_tDecl(Typedef->getDecl());
3310 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3311 assert(Tag && "Invalid ucontext_t type in AST file");
3312 Context.setucontext_tDecl(Tag->getDecl());
3318 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
3320 // If there were any CUDA special declarations, deserialize them.
3321 if (!CUDASpecialDeclRefs.empty()) {
3322 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
3323 Context.setcudaConfigureCallDecl(
3324 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3327 // Re-export any modules that were imported by a non-module AST file.
3328 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3329 if (Module *Imported = getSubmodule(ImportedModules[I]))
3330 makeModuleVisible(Imported, Module::AllVisible,
3331 /*ImportLoc=*/SourceLocation(),
3332 /*Complain=*/false);
3334 ImportedModules.clear();
3337 void ASTReader::finalizeForWriting() {
3338 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3339 HiddenEnd = HiddenNamesMap.end();
3340 Hidden != HiddenEnd; ++Hidden) {
3341 makeNamesVisible(Hidden->second, Hidden->first);
3343 HiddenNamesMap.clear();
3346 /// \brief Given a cursor at the start of an AST file, scan ahead and drop the
3347 /// cursor into the start of the given block ID, returning false on success and
3348 /// true on failure.
3349 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
3351 llvm::BitstreamEntry Entry = Cursor.advance();
3352 switch (Entry.Kind) {
3353 case llvm::BitstreamEntry::Error:
3354 case llvm::BitstreamEntry::EndBlock:
3357 case llvm::BitstreamEntry::Record:
3358 // Ignore top-level records.
3359 Cursor.skipRecord(Entry.ID);
3362 case llvm::BitstreamEntry::SubBlock:
3363 if (Entry.ID == BlockID) {
3364 if (Cursor.EnterSubBlock(BlockID))
3370 if (Cursor.SkipBlock())
3376 /// \brief Retrieve the name of the original source file name
3377 /// directly from the AST file, without actually loading the AST
3379 std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
3380 FileManager &FileMgr,
3381 DiagnosticsEngine &Diags) {
3382 // Open the AST file.
3384 OwningPtr<llvm::MemoryBuffer> Buffer;
3385 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
3387 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
3388 return std::string();
3391 // Initialize the stream
3392 llvm::BitstreamReader StreamFile;
3393 BitstreamCursor Stream;
3394 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3395 (const unsigned char *)Buffer->getBufferEnd());
3396 Stream.init(StreamFile);
3398 // Sniff for the signature.
3399 if (Stream.Read(8) != 'C' ||
3400 Stream.Read(8) != 'P' ||
3401 Stream.Read(8) != 'C' ||
3402 Stream.Read(8) != 'H') {
3403 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
3404 return std::string();
3407 // Scan for the CONTROL_BLOCK_ID block.
3408 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
3409 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3410 return std::string();
3413 // Scan for ORIGINAL_FILE inside the control block.
3416 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
3417 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3418 return std::string();
3420 if (Entry.Kind != llvm::BitstreamEntry::Record) {
3421 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3422 return std::string();
3427 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
3433 class SimplePCHValidator : public ASTReaderListener {
3434 const LangOptions &ExistingLangOpts;
3435 const TargetOptions &ExistingTargetOpts;
3436 const PreprocessorOptions &ExistingPPOpts;
3437 FileManager &FileMgr;
3440 SimplePCHValidator(const LangOptions &ExistingLangOpts,
3441 const TargetOptions &ExistingTargetOpts,
3442 const PreprocessorOptions &ExistingPPOpts,
3443 FileManager &FileMgr)
3444 : ExistingLangOpts(ExistingLangOpts),
3445 ExistingTargetOpts(ExistingTargetOpts),
3446 ExistingPPOpts(ExistingPPOpts),
3451 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
3453 return checkLanguageOptions(ExistingLangOpts, LangOpts, 0);
3455 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
3457 return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0);
3459 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
3461 std::string &SuggestedPredefines) {
3462 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, 0, FileMgr,
3463 SuggestedPredefines, ExistingLangOpts);
3468 bool ASTReader::readASTFileControlBlock(StringRef Filename,
3469 FileManager &FileMgr,
3470 ASTReaderListener &Listener) {
3471 // Open the AST file.
3473 OwningPtr<llvm::MemoryBuffer> Buffer;
3474 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
3479 // Initialize the stream
3480 llvm::BitstreamReader StreamFile;
3481 BitstreamCursor Stream;
3482 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3483 (const unsigned char *)Buffer->getBufferEnd());
3484 Stream.init(StreamFile);
3486 // Sniff for the signature.
3487 if (Stream.Read(8) != 'C' ||
3488 Stream.Read(8) != 'P' ||
3489 Stream.Read(8) != 'C' ||
3490 Stream.Read(8) != 'H') {
3494 // Scan for the CONTROL_BLOCK_ID block.
3495 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
3498 bool NeedsInputFiles = Listener.needsInputFileVisitation();
3499 BitstreamCursor InputFilesCursor;
3500 if (NeedsInputFiles) {
3501 InputFilesCursor = Stream;
3502 if (SkipCursorToBlock(InputFilesCursor, INPUT_FILES_BLOCK_ID))
3505 // Read the abbreviations
3507 uint64_t Offset = InputFilesCursor.GetCurrentBitNo();
3508 unsigned Code = InputFilesCursor.ReadCode();
3510 // We expect all abbrevs to be at the start of the block.
3511 if (Code != llvm::bitc::DEFINE_ABBREV) {
3512 InputFilesCursor.JumpToBit(Offset);
3515 InputFilesCursor.ReadAbbrevRecord();
3519 // Scan for ORIGINAL_FILE inside the control block.
3522 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
3523 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3526 if (Entry.Kind != llvm::BitstreamEntry::Record)
3531 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
3532 switch ((ControlRecordTypes)RecCode) {
3534 if (Record[0] != VERSION_MAJOR)
3537 if (Listener.ReadFullVersionInformation(Blob))
3542 case LANGUAGE_OPTIONS:
3543 if (ParseLanguageOptions(Record, false, Listener))
3547 case TARGET_OPTIONS:
3548 if (ParseTargetOptions(Record, false, Listener))
3552 case DIAGNOSTIC_OPTIONS:
3553 if (ParseDiagnosticOptions(Record, false, Listener))
3557 case FILE_SYSTEM_OPTIONS:
3558 if (ParseFileSystemOptions(Record, false, Listener))
3562 case HEADER_SEARCH_OPTIONS:
3563 if (ParseHeaderSearchOptions(Record, false, Listener))
3567 case PREPROCESSOR_OPTIONS: {
3568 std::string IgnoredSuggestedPredefines;
3569 if (ParsePreprocessorOptions(Record, false, Listener,
3570 IgnoredSuggestedPredefines))
3575 case INPUT_FILE_OFFSETS: {
3576 if (!NeedsInputFiles)
3579 unsigned NumInputFiles = Record[0];
3580 unsigned NumUserFiles = Record[1];
3581 const uint32_t *InputFileOffs = (const uint32_t *)Blob.data();
3582 for (unsigned I = 0; I != NumInputFiles; ++I) {
3583 // Go find this input file.
3584 bool isSystemFile = I >= NumUserFiles;
3585 BitstreamCursor &Cursor = InputFilesCursor;
3586 SavedStreamPosition SavedPosition(Cursor);
3587 Cursor.JumpToBit(InputFileOffs[I]);
3589 unsigned Code = Cursor.ReadCode();
3592 bool shouldContinue = false;
3593 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
3595 shouldContinue = Listener.visitInputFile(Blob, isSystemFile);
3598 if (!shouldContinue)
3605 // No other validation to perform.
3612 bool ASTReader::isAcceptableASTFile(StringRef Filename,
3613 FileManager &FileMgr,
3614 const LangOptions &LangOpts,
3615 const TargetOptions &TargetOpts,
3616 const PreprocessorOptions &PPOpts) {
3617 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr);
3618 return !readASTFileControlBlock(Filename, FileMgr, validator);
3621 bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
3622 // Enter the submodule block.
3623 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3624 Error("malformed submodule block record in AST file");
3628 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
3630 Module *CurrentModule = 0;
3633 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
3635 switch (Entry.Kind) {
3636 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
3637 case llvm::BitstreamEntry::Error:
3638 Error("malformed block record in AST file");
3640 case llvm::BitstreamEntry::EndBlock:
3642 case llvm::BitstreamEntry::Record:
3643 // The interesting case.
3650 switch (F.Stream.readRecord(Entry.ID, Record, &Blob)) {
3651 default: // Default behavior: ignore.
3654 case SUBMODULE_DEFINITION: {
3656 Error("missing submodule metadata record at beginning of block");
3660 if (Record.size() < 8) {
3661 Error("malformed module definition");
3665 StringRef Name = Blob;
3666 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3667 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3668 bool IsFramework = Record[2];
3669 bool IsExplicit = Record[3];
3670 bool IsSystem = Record[4];
3671 bool InferSubmodules = Record[5];
3672 bool InferExplicitSubmodules = Record[6];
3673 bool InferExportWildcard = Record[7];
3674 bool ConfigMacrosExhaustive = Record[8];
3676 Module *ParentModule = 0;
3678 ParentModule = getSubmodule(Parent);
3680 // Retrieve this (sub)module from the module map, creating it if
3682 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3685 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3686 if (GlobalIndex >= SubmodulesLoaded.size() ||
3687 SubmodulesLoaded[GlobalIndex]) {
3688 Error("too many submodules");
3692 if (!ParentModule) {
3693 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
3694 if (CurFile != F.File) {
3695 if (!Diags.isDiagnosticInFlight()) {
3696 Diag(diag::err_module_file_conflict)
3697 << CurrentModule->getTopLevelModuleName()
3698 << CurFile->getName()
3699 << F.File->getName();
3705 CurrentModule->setASTFile(F.File);
3708 CurrentModule->IsFromModuleFile = true;
3709 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
3710 CurrentModule->InferSubmodules = InferSubmodules;
3711 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3712 CurrentModule->InferExportWildcard = InferExportWildcard;
3713 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
3714 if (DeserializationListener)
3715 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
3717 SubmodulesLoaded[GlobalIndex] = CurrentModule;
3719 // Clear out data that will be replaced by what is the module file.
3720 CurrentModule->LinkLibraries.clear();
3721 CurrentModule->ConfigMacros.clear();
3722 CurrentModule->UnresolvedConflicts.clear();
3723 CurrentModule->Conflicts.clear();
3727 case SUBMODULE_UMBRELLA_HEADER: {
3729 Error("missing submodule metadata record at beginning of block");
3736 if (const FileEntry *Umbrella = PP.getFileManager().getFile(Blob)) {
3737 if (!CurrentModule->getUmbrellaHeader())
3738 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
3739 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
3740 Error("mismatched umbrella headers in submodule");
3747 case SUBMODULE_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_EXCLUDED_HEADER: {
3764 Error("missing submodule metadata record at beginning of block");
3771 // We lazily associate headers with their modules via the HeaderInfoTable.
3772 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
3773 // of complete filenames or remove it entirely.
3777 case SUBMODULE_PRIVATE_HEADER: {
3779 Error("missing submodule metadata record at beginning of block");
3786 // We lazily associate headers with their modules via the HeaderInfoTable.
3787 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
3788 // of complete filenames or remove it entirely.
3792 case SUBMODULE_TOPHEADER: {
3794 Error("missing submodule metadata record at beginning of block");
3801 CurrentModule->addTopHeaderFilename(Blob);
3805 case SUBMODULE_UMBRELLA_DIR: {
3807 Error("missing submodule metadata record at beginning of block");
3814 if (const DirectoryEntry *Umbrella
3815 = PP.getFileManager().getDirectory(Blob)) {
3816 if (!CurrentModule->getUmbrellaDir())
3817 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3818 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3819 Error("mismatched umbrella directories in submodule");
3826 case SUBMODULE_METADATA: {
3828 Error("submodule metadata record not at beginning of block");
3833 F.BaseSubmoduleID = getTotalNumSubmodules();
3834 F.LocalNumSubmodules = Record[0];
3835 unsigned LocalBaseSubmoduleID = Record[1];
3836 if (F.LocalNumSubmodules > 0) {
3837 // Introduce the global -> local mapping for submodules within this
3839 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3841 // Introduce the local -> global mapping for submodules within this
3843 F.SubmoduleRemap.insertOrReplace(
3844 std::make_pair(LocalBaseSubmoduleID,
3845 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3847 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3852 case SUBMODULE_IMPORTS: {
3854 Error("missing submodule metadata record at beginning of block");
3861 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
3862 UnresolvedModuleRef Unresolved;
3863 Unresolved.File = &F;
3864 Unresolved.Mod = CurrentModule;
3865 Unresolved.ID = Record[Idx];
3866 Unresolved.Kind = UnresolvedModuleRef::Import;
3867 Unresolved.IsWildcard = false;
3868 UnresolvedModuleRefs.push_back(Unresolved);
3873 case SUBMODULE_EXPORTS: {
3875 Error("missing submodule metadata record at beginning of block");
3882 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
3883 UnresolvedModuleRef Unresolved;
3884 Unresolved.File = &F;
3885 Unresolved.Mod = CurrentModule;
3886 Unresolved.ID = Record[Idx];
3887 Unresolved.Kind = UnresolvedModuleRef::Export;
3888 Unresolved.IsWildcard = Record[Idx + 1];
3889 UnresolvedModuleRefs.push_back(Unresolved);
3892 // Once we've loaded the set of exports, there's no reason to keep
3893 // the parsed, unresolved exports around.
3894 CurrentModule->UnresolvedExports.clear();
3897 case SUBMODULE_REQUIRES: {
3899 Error("missing submodule metadata record at beginning of block");
3906 CurrentModule->addRequirement(Blob, Record[0], Context.getLangOpts(),
3907 Context.getTargetInfo());
3911 case SUBMODULE_LINK_LIBRARY:
3913 Error("missing submodule metadata record at beginning of block");
3920 CurrentModule->LinkLibraries.push_back(
3921 Module::LinkLibrary(Blob, Record[0]));
3924 case SUBMODULE_CONFIG_MACRO:
3926 Error("missing submodule metadata record at beginning of block");
3933 CurrentModule->ConfigMacros.push_back(Blob.str());
3936 case SUBMODULE_CONFLICT: {
3938 Error("missing submodule metadata record at beginning of block");
3945 UnresolvedModuleRef Unresolved;
3946 Unresolved.File = &F;
3947 Unresolved.Mod = CurrentModule;
3948 Unresolved.ID = Record[0];
3949 Unresolved.Kind = UnresolvedModuleRef::Conflict;
3950 Unresolved.IsWildcard = false;
3951 Unresolved.String = Blob;
3952 UnresolvedModuleRefs.push_back(Unresolved);
3959 /// \brief Parse the record that corresponds to a LangOptions data
3962 /// This routine parses the language options from the AST file and then gives
3963 /// them to the AST listener if one is set.
3965 /// \returns true if the listener deems the file unacceptable, false otherwise.
3966 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
3968 ASTReaderListener &Listener) {
3969 LangOptions LangOpts;
3971 #define LANGOPT(Name, Bits, Default, Description) \
3972 LangOpts.Name = Record[Idx++];
3973 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3974 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3975 #include "clang/Basic/LangOptions.def"
3976 #define SANITIZER(NAME, ID) LangOpts.Sanitize.ID = Record[Idx++];
3977 #include "clang/Basic/Sanitizers.def"
3979 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3980 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3981 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
3983 unsigned Length = Record[Idx++];
3984 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3985 Record.begin() + Idx + Length);
3990 for (unsigned N = Record[Idx++]; N; --N) {
3991 LangOpts.CommentOpts.BlockCommandNames.push_back(
3992 ReadString(Record, Idx));
3994 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
3996 return Listener.ReadLanguageOptions(LangOpts, Complain);
3999 bool ASTReader::ParseTargetOptions(const RecordData &Record,
4001 ASTReaderListener &Listener) {
4003 TargetOptions TargetOpts;
4004 TargetOpts.Triple = ReadString(Record, Idx);
4005 TargetOpts.CPU = ReadString(Record, Idx);
4006 TargetOpts.ABI = ReadString(Record, Idx);
4007 TargetOpts.CXXABI = ReadString(Record, Idx);
4008 TargetOpts.LinkerVersion = ReadString(Record, Idx);
4009 for (unsigned N = Record[Idx++]; N; --N) {
4010 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
4012 for (unsigned N = Record[Idx++]; N; --N) {
4013 TargetOpts.Features.push_back(ReadString(Record, Idx));
4016 return Listener.ReadTargetOptions(TargetOpts, Complain);
4019 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
4020 ASTReaderListener &Listener) {
4021 DiagnosticOptions DiagOpts;
4023 #define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
4024 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
4025 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
4026 #include "clang/Basic/DiagnosticOptions.def"
4028 for (unsigned N = Record[Idx++]; N; --N) {
4029 DiagOpts.Warnings.push_back(ReadString(Record, Idx));
4032 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
4035 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
4036 ASTReaderListener &Listener) {
4037 FileSystemOptions FSOpts;
4039 FSOpts.WorkingDir = ReadString(Record, Idx);
4040 return Listener.ReadFileSystemOptions(FSOpts, Complain);
4043 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
4045 ASTReaderListener &Listener) {
4046 HeaderSearchOptions HSOpts;
4048 HSOpts.Sysroot = ReadString(Record, Idx);
4051 for (unsigned N = Record[Idx++]; N; --N) {
4052 std::string Path = ReadString(Record, Idx);
4053 frontend::IncludeDirGroup Group
4054 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
4055 bool IsFramework = Record[Idx++];
4056 bool IgnoreSysRoot = Record[Idx++];
4057 HSOpts.UserEntries.push_back(
4058 HeaderSearchOptions::Entry(Path, Group, IsFramework, IgnoreSysRoot));
4061 // System header prefixes.
4062 for (unsigned N = Record[Idx++]; N; --N) {
4063 std::string Prefix = ReadString(Record, Idx);
4064 bool IsSystemHeader = Record[Idx++];
4065 HSOpts.SystemHeaderPrefixes.push_back(
4066 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
4069 HSOpts.ResourceDir = ReadString(Record, Idx);
4070 HSOpts.ModuleCachePath = ReadString(Record, Idx);
4071 HSOpts.DisableModuleHash = Record[Idx++];
4072 HSOpts.UseBuiltinIncludes = Record[Idx++];
4073 HSOpts.UseStandardSystemIncludes = Record[Idx++];
4074 HSOpts.UseStandardCXXIncludes = Record[Idx++];
4075 HSOpts.UseLibcxx = Record[Idx++];
4077 return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
4080 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
4082 ASTReaderListener &Listener,
4083 std::string &SuggestedPredefines) {
4084 PreprocessorOptions PPOpts;
4087 // Macro definitions/undefs
4088 for (unsigned N = Record[Idx++]; N; --N) {
4089 std::string Macro = ReadString(Record, Idx);
4090 bool IsUndef = Record[Idx++];
4091 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
4095 for (unsigned N = Record[Idx++]; N; --N) {
4096 PPOpts.Includes.push_back(ReadString(Record, Idx));
4100 for (unsigned N = Record[Idx++]; N; --N) {
4101 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
4104 PPOpts.UsePredefines = Record[Idx++];
4105 PPOpts.DetailedRecord = Record[Idx++];
4106 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
4107 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
4108 PPOpts.ObjCXXARCStandardLibrary =
4109 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
4110 SuggestedPredefines.clear();
4111 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
4112 SuggestedPredefines);
4115 std::pair<ModuleFile *, unsigned>
4116 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
4117 GlobalPreprocessedEntityMapType::iterator
4118 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
4119 assert(I != GlobalPreprocessedEntityMap.end() &&
4120 "Corrupted global preprocessed entity map");
4121 ModuleFile *M = I->second;
4122 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
4123 return std::make_pair(M, LocalIndex);
4126 std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
4127 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
4128 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
4129 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
4130 Mod.NumPreprocessedEntities);
4132 return std::make_pair(PreprocessingRecord::iterator(),
4133 PreprocessingRecord::iterator());
4136 std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
4137 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
4138 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4139 ModuleDeclIterator(this, &Mod,
4140 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
4143 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4144 PreprocessedEntityID PPID = Index+1;
4145 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4146 ModuleFile &M = *PPInfo.first;
4147 unsigned LocalIndex = PPInfo.second;
4148 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4150 if (!PP.getPreprocessingRecord()) {
4151 Error("no preprocessing record");
4155 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
4156 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
4158 llvm::BitstreamEntry Entry =
4159 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
4160 if (Entry.Kind != llvm::BitstreamEntry::Record)
4164 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
4165 ReadSourceLocation(M, PPOffs.End));
4166 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
4169 PreprocessorDetailRecordTypes RecType =
4170 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4171 Entry.ID, Record, &Blob);
4173 case PPD_MACRO_EXPANSION: {
4174 bool isBuiltin = Record[0];
4175 IdentifierInfo *Name = 0;
4176 MacroDefinition *Def = 0;
4178 Name = getLocalIdentifier(M, Record[1]);
4180 PreprocessedEntityID
4181 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
4182 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
4187 ME = new (PPRec) MacroExpansion(Name, Range);
4189 ME = new (PPRec) MacroExpansion(Def, Range);
4194 case PPD_MACRO_DEFINITION: {
4195 // Decode the identifier info and then check again; if the macro is
4196 // still defined and associated with the identifier,
4197 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
4199 = new (PPRec) MacroDefinition(II, Range);
4201 if (DeserializationListener)
4202 DeserializationListener->MacroDefinitionRead(PPID, MD);
4207 case PPD_INCLUSION_DIRECTIVE: {
4208 const char *FullFileNameStart = Blob.data() + Record[0];
4209 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
4210 const FileEntry *File = 0;
4211 if (!FullFileName.empty())
4212 File = PP.getFileManager().getFile(FullFileName);
4214 // FIXME: Stable encoding
4215 InclusionDirective::InclusionKind Kind
4216 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4217 InclusionDirective *ID
4218 = new (PPRec) InclusionDirective(PPRec, Kind,
4219 StringRef(Blob.data(), Record[0]),
4220 Record[1], Record[3],
4227 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4230 /// \brief \arg SLocMapI points at a chunk of a module that contains no
4231 /// preprocessed entities or the entities it contains are not the ones we are
4232 /// looking for. Find the next module that contains entities and return the ID
4233 /// of the first entry.
4234 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4235 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4237 for (GlobalSLocOffsetMapType::const_iterator
4238 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4239 ModuleFile &M = *SLocMapI->second;
4240 if (M.NumPreprocessedEntities)
4241 return M.BasePreprocessedEntityID;
4244 return getTotalNumPreprocessedEntities();
4249 template <unsigned PPEntityOffset::*PPLoc>
4250 struct PPEntityComp {
4251 const ASTReader &Reader;
4254 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
4256 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4257 SourceLocation LHS = getLoc(L);
4258 SourceLocation RHS = getLoc(R);
4259 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4262 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
4263 SourceLocation LHS = getLoc(L);
4264 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4267 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
4268 SourceLocation RHS = getLoc(R);
4269 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4272 SourceLocation getLoc(const PPEntityOffset &PPE) const {
4273 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
4279 /// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
4280 PreprocessedEntityID
4281 ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
4282 if (SourceMgr.isLocalSourceLocation(BLoc))
4283 return getTotalNumPreprocessedEntities();
4285 GlobalSLocOffsetMapType::const_iterator
4286 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
4287 BLoc.getOffset() - 1);
4288 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4289 "Corrupted global sloc offset map");
4291 if (SLocMapI->second->NumPreprocessedEntities == 0)
4292 return findNextPreprocessedEntity(SLocMapI);
4294 ModuleFile &M = *SLocMapI->second;
4295 typedef const PPEntityOffset *pp_iterator;
4296 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4297 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4299 size_t Count = M.NumPreprocessedEntities;
4301 pp_iterator First = pp_begin;
4304 // Do a binary search manually instead of using std::lower_bound because
4305 // The end locations of entities may be unordered (when a macro expansion
4306 // is inside another macro argument), but for this case it is not important
4307 // whether we get the first macro expansion or its containing macro.
4311 std::advance(PPI, Half);
4312 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
4316 Count = Count - Half - 1;
4322 return findNextPreprocessedEntity(SLocMapI);
4324 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4327 /// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
4328 PreprocessedEntityID
4329 ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
4330 if (SourceMgr.isLocalSourceLocation(ELoc))
4331 return getTotalNumPreprocessedEntities();
4333 GlobalSLocOffsetMapType::const_iterator
4334 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
4335 ELoc.getOffset() - 1);
4336 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4337 "Corrupted global sloc offset map");
4339 if (SLocMapI->second->NumPreprocessedEntities == 0)
4340 return findNextPreprocessedEntity(SLocMapI);
4342 ModuleFile &M = *SLocMapI->second;
4343 typedef const PPEntityOffset *pp_iterator;
4344 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4345 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4347 std::upper_bound(pp_begin, pp_end, ELoc,
4348 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
4351 return findNextPreprocessedEntity(SLocMapI);
4353 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4356 /// \brief Returns a pair of [Begin, End) indices of preallocated
4357 /// preprocessed entities that \arg Range encompasses.
4358 std::pair<unsigned, unsigned>
4359 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
4360 if (Range.isInvalid())
4361 return std::make_pair(0,0);
4362 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
4364 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
4365 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
4366 return std::make_pair(BeginID, EndID);
4369 /// \brief Optionally returns true or false if the preallocated preprocessed
4370 /// entity with index \arg Index came from file \arg FID.
4371 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
4373 if (FID.isInvalid())
4376 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4377 ModuleFile &M = *PPInfo.first;
4378 unsigned LocalIndex = PPInfo.second;
4379 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4381 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
4382 if (Loc.isInvalid())
4385 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
4392 /// \brief Visitor used to search for information about a header file.
4393 class HeaderFileInfoVisitor {
4394 const FileEntry *FE;
4396 Optional<HeaderFileInfo> HFI;
4399 explicit HeaderFileInfoVisitor(const FileEntry *FE)
4402 static bool visit(ModuleFile &M, void *UserData) {
4403 HeaderFileInfoVisitor *This
4404 = static_cast<HeaderFileInfoVisitor *>(UserData);
4406 HeaderFileInfoLookupTable *Table
4407 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4411 // Look in the on-disk hash table for an entry for this file name.
4412 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE);
4413 if (Pos == Table->end())
4420 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
4424 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
4425 HeaderFileInfoVisitor Visitor(FE);
4426 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
4427 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
4430 return HeaderFileInfo();
4433 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
4434 // FIXME: Make it work properly with modules.
4435 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
4436 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
4437 ModuleFile &F = *(*I);
4440 assert(!Diag.DiagStates.empty());
4441 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
4442 while (Idx < F.PragmaDiagMappings.size()) {
4443 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
4444 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
4445 if (DiagStateID != 0) {
4446 Diag.DiagStatePoints.push_back(
4447 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
4448 FullSourceLoc(Loc, SourceMgr)));
4452 assert(DiagStateID == 0);
4453 // A new DiagState was created here.
4454 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
4455 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
4456 DiagStates.push_back(NewState);
4457 Diag.DiagStatePoints.push_back(
4458 DiagnosticsEngine::DiagStatePoint(NewState,
4459 FullSourceLoc(Loc, SourceMgr)));
4461 assert(Idx < F.PragmaDiagMappings.size() &&
4462 "Invalid data, didn't find '-1' marking end of diag/map pairs");
4463 if (Idx >= F.PragmaDiagMappings.size()) {
4464 break; // Something is messed up but at least avoid infinite loop in
4467 unsigned DiagID = F.PragmaDiagMappings[Idx++];
4468 if (DiagID == (unsigned)-1) {
4469 break; // no more diag/map pairs for this location.
4471 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
4472 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
4473 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
4479 /// \brief Get the correct cursor and offset for loading a type.
4480 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
4481 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
4482 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
4483 ModuleFile *M = I->second;
4484 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
4487 /// \brief Read and return the type with the given index..
4489 /// The index is the type ID, shifted and minus the number of predefs. This
4490 /// routine actually reads the record corresponding to the type at the given
4491 /// location. It is a helper routine for GetType, which deals with reading type
4493 QualType ASTReader::readTypeRecord(unsigned Index) {
4494 RecordLocation Loc = TypeCursorForIndex(Index);
4495 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
4497 // Keep track of where we are in the stream, then jump back there
4498 // after reading this type.
4499 SavedStreamPosition SavedPosition(DeclsCursor);
4501 ReadingKindTracker ReadingKind(Read_Type, *this);
4503 // Note that we are loading a type record.
4504 Deserializing AType(this);
4507 DeclsCursor.JumpToBit(Loc.Offset);
4509 unsigned Code = DeclsCursor.ReadCode();
4510 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
4511 case TYPE_EXT_QUAL: {
4512 if (Record.size() != 2) {
4513 Error("Incorrect encoding of extended qualifier type");
4516 QualType Base = readType(*Loc.F, Record, Idx);
4517 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
4518 return Context.getQualifiedType(Base, Quals);
4521 case TYPE_COMPLEX: {
4522 if (Record.size() != 1) {
4523 Error("Incorrect encoding of complex type");
4526 QualType ElemType = readType(*Loc.F, Record, Idx);
4527 return Context.getComplexType(ElemType);
4530 case TYPE_POINTER: {
4531 if (Record.size() != 1) {
4532 Error("Incorrect encoding of pointer type");
4535 QualType PointeeType = readType(*Loc.F, Record, Idx);
4536 return Context.getPointerType(PointeeType);
4539 case TYPE_DECAYED: {
4540 if (Record.size() != 1) {
4541 Error("Incorrect encoding of decayed type");
4544 QualType OriginalType = readType(*Loc.F, Record, Idx);
4545 QualType DT = Context.getAdjustedParameterType(OriginalType);
4546 if (!isa<DecayedType>(DT))
4547 Error("Decayed type does not decay");
4551 case TYPE_BLOCK_POINTER: {
4552 if (Record.size() != 1) {
4553 Error("Incorrect encoding of block pointer type");
4556 QualType PointeeType = readType(*Loc.F, Record, Idx);
4557 return Context.getBlockPointerType(PointeeType);
4560 case TYPE_LVALUE_REFERENCE: {
4561 if (Record.size() != 2) {
4562 Error("Incorrect encoding of lvalue reference type");
4565 QualType PointeeType = readType(*Loc.F, Record, Idx);
4566 return Context.getLValueReferenceType(PointeeType, Record[1]);
4569 case TYPE_RVALUE_REFERENCE: {
4570 if (Record.size() != 1) {
4571 Error("Incorrect encoding of rvalue reference type");
4574 QualType PointeeType = readType(*Loc.F, Record, Idx);
4575 return Context.getRValueReferenceType(PointeeType);
4578 case TYPE_MEMBER_POINTER: {
4579 if (Record.size() != 2) {
4580 Error("Incorrect encoding of member pointer type");
4583 QualType PointeeType = readType(*Loc.F, Record, Idx);
4584 QualType ClassType = readType(*Loc.F, Record, Idx);
4585 if (PointeeType.isNull() || ClassType.isNull())
4588 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
4591 case TYPE_CONSTANT_ARRAY: {
4592 QualType ElementType = readType(*Loc.F, Record, Idx);
4593 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4594 unsigned IndexTypeQuals = Record[2];
4596 llvm::APInt Size = ReadAPInt(Record, Idx);
4597 return Context.getConstantArrayType(ElementType, Size,
4598 ASM, IndexTypeQuals);
4601 case TYPE_INCOMPLETE_ARRAY: {
4602 QualType ElementType = readType(*Loc.F, Record, Idx);
4603 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4604 unsigned IndexTypeQuals = Record[2];
4605 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
4608 case TYPE_VARIABLE_ARRAY: {
4609 QualType ElementType = readType(*Loc.F, Record, Idx);
4610 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4611 unsigned IndexTypeQuals = Record[2];
4612 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
4613 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
4614 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
4615 ASM, IndexTypeQuals,
4616 SourceRange(LBLoc, RBLoc));
4620 if (Record.size() != 3) {
4621 Error("incorrect encoding of vector type in AST file");
4625 QualType ElementType = readType(*Loc.F, Record, Idx);
4626 unsigned NumElements = Record[1];
4627 unsigned VecKind = Record[2];
4628 return Context.getVectorType(ElementType, NumElements,
4629 (VectorType::VectorKind)VecKind);
4632 case TYPE_EXT_VECTOR: {
4633 if (Record.size() != 3) {
4634 Error("incorrect encoding of extended vector type in AST file");
4638 QualType ElementType = readType(*Loc.F, Record, Idx);
4639 unsigned NumElements = Record[1];
4640 return Context.getExtVectorType(ElementType, NumElements);
4643 case TYPE_FUNCTION_NO_PROTO: {
4644 if (Record.size() != 6) {
4645 Error("incorrect encoding of no-proto function type");
4648 QualType ResultType = readType(*Loc.F, Record, Idx);
4649 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
4650 (CallingConv)Record[4], Record[5]);
4651 return Context.getFunctionNoProtoType(ResultType, Info);
4654 case TYPE_FUNCTION_PROTO: {
4655 QualType ResultType = readType(*Loc.F, Record, Idx);
4657 FunctionProtoType::ExtProtoInfo EPI;
4658 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
4659 /*hasregparm*/ Record[2],
4660 /*regparm*/ Record[3],
4661 static_cast<CallingConv>(Record[4]),
4662 /*produces*/ Record[5]);
4665 unsigned NumParams = Record[Idx++];
4666 SmallVector<QualType, 16> ParamTypes;
4667 for (unsigned I = 0; I != NumParams; ++I)
4668 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
4670 EPI.Variadic = Record[Idx++];
4671 EPI.HasTrailingReturn = Record[Idx++];
4672 EPI.TypeQuals = Record[Idx++];
4673 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
4674 ExceptionSpecificationType EST =
4675 static_cast<ExceptionSpecificationType>(Record[Idx++]);
4676 EPI.ExceptionSpecType = EST;
4677 SmallVector<QualType, 2> Exceptions;
4678 if (EST == EST_Dynamic) {
4679 EPI.NumExceptions = Record[Idx++];
4680 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
4681 Exceptions.push_back(readType(*Loc.F, Record, Idx));
4682 EPI.Exceptions = Exceptions.data();
4683 } else if (EST == EST_ComputedNoexcept) {
4684 EPI.NoexceptExpr = ReadExpr(*Loc.F);
4685 } else if (EST == EST_Uninstantiated) {
4686 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4687 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4688 } else if (EST == EST_Unevaluated) {
4689 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4691 return Context.getFunctionType(ResultType, ParamTypes, EPI);
4694 case TYPE_UNRESOLVED_USING: {
4696 return Context.getTypeDeclType(
4697 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
4700 case TYPE_TYPEDEF: {
4701 if (Record.size() != 2) {
4702 Error("incorrect encoding of typedef type");
4706 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
4707 QualType Canonical = readType(*Loc.F, Record, Idx);
4708 if (!Canonical.isNull())
4709 Canonical = Context.getCanonicalType(Canonical);
4710 return Context.getTypedefType(Decl, Canonical);
4713 case TYPE_TYPEOF_EXPR:
4714 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
4717 if (Record.size() != 1) {
4718 Error("incorrect encoding of typeof(type) in AST file");
4721 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4722 return Context.getTypeOfType(UnderlyingType);
4725 case TYPE_DECLTYPE: {
4726 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4727 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4730 case TYPE_UNARY_TRANSFORM: {
4731 QualType BaseType = readType(*Loc.F, Record, Idx);
4732 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4733 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
4734 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
4738 QualType Deduced = readType(*Loc.F, Record, Idx);
4739 bool IsDecltypeAuto = Record[Idx++];
4740 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
4741 return Context.getAutoType(Deduced, IsDecltypeAuto, IsDependent);
4745 if (Record.size() != 2) {
4746 Error("incorrect encoding of record type");
4750 bool IsDependent = Record[Idx++];
4751 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4752 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4753 QualType T = Context.getRecordType(RD);
4754 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4759 if (Record.size() != 2) {
4760 Error("incorrect encoding of enum type");
4764 bool IsDependent = Record[Idx++];
4766 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
4767 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4771 case TYPE_ATTRIBUTED: {
4772 if (Record.size() != 3) {
4773 Error("incorrect encoding of attributed type");
4776 QualType modifiedType = readType(*Loc.F, Record, Idx);
4777 QualType equivalentType = readType(*Loc.F, Record, Idx);
4778 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
4779 return Context.getAttributedType(kind, modifiedType, equivalentType);
4783 if (Record.size() != 1) {
4784 Error("incorrect encoding of paren type");
4787 QualType InnerType = readType(*Loc.F, Record, Idx);
4788 return Context.getParenType(InnerType);
4791 case TYPE_PACK_EXPANSION: {
4792 if (Record.size() != 2) {
4793 Error("incorrect encoding of pack expansion type");
4796 QualType Pattern = readType(*Loc.F, Record, Idx);
4797 if (Pattern.isNull())
4799 Optional<unsigned> NumExpansions;
4801 NumExpansions = Record[1] - 1;
4802 return Context.getPackExpansionType(Pattern, NumExpansions);
4805 case TYPE_ELABORATED: {
4807 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4808 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4809 QualType NamedType = readType(*Loc.F, Record, Idx);
4810 return Context.getElaboratedType(Keyword, NNS, NamedType);
4813 case TYPE_OBJC_INTERFACE: {
4815 ObjCInterfaceDecl *ItfD
4816 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
4817 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
4820 case TYPE_OBJC_OBJECT: {
4822 QualType Base = readType(*Loc.F, Record, Idx);
4823 unsigned NumProtos = Record[Idx++];
4824 SmallVector<ObjCProtocolDecl*, 4> Protos;
4825 for (unsigned I = 0; I != NumProtos; ++I)
4826 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
4827 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
4830 case TYPE_OBJC_OBJECT_POINTER: {
4832 QualType Pointee = readType(*Loc.F, Record, Idx);
4833 return Context.getObjCObjectPointerType(Pointee);
4836 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
4838 QualType Parm = readType(*Loc.F, Record, Idx);
4839 QualType Replacement = readType(*Loc.F, Record, Idx);
4841 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
4845 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4847 QualType Parm = readType(*Loc.F, Record, Idx);
4848 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
4849 return Context.getSubstTemplateTypeParmPackType(
4850 cast<TemplateTypeParmType>(Parm),
4854 case TYPE_INJECTED_CLASS_NAME: {
4855 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
4856 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
4857 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
4858 // for AST reading, too much interdependencies.
4860 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
4863 case TYPE_TEMPLATE_TYPE_PARM: {
4865 unsigned Depth = Record[Idx++];
4866 unsigned Index = Record[Idx++];
4867 bool Pack = Record[Idx++];
4868 TemplateTypeParmDecl *D
4869 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
4870 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
4873 case TYPE_DEPENDENT_NAME: {
4875 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4876 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4877 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4878 QualType Canon = readType(*Loc.F, Record, Idx);
4879 if (!Canon.isNull())
4880 Canon = Context.getCanonicalType(Canon);
4881 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
4884 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
4886 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4887 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4888 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4889 unsigned NumArgs = Record[Idx++];
4890 SmallVector<TemplateArgument, 8> Args;
4891 Args.reserve(NumArgs);
4893 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
4894 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
4895 Args.size(), Args.data());
4898 case TYPE_DEPENDENT_SIZED_ARRAY: {
4902 QualType ElementType = readType(*Loc.F, Record, Idx);
4903 ArrayType::ArraySizeModifier ASM
4904 = (ArrayType::ArraySizeModifier)Record[Idx++];
4905 unsigned IndexTypeQuals = Record[Idx++];
4907 // DependentSizedArrayType
4908 Expr *NumElts = ReadExpr(*Loc.F);
4909 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
4911 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
4912 IndexTypeQuals, Brackets);
4915 case TYPE_TEMPLATE_SPECIALIZATION: {
4917 bool IsDependent = Record[Idx++];
4918 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
4919 SmallVector<TemplateArgument, 8> Args;
4920 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
4921 QualType Underlying = readType(*Loc.F, Record, Idx);
4923 if (Underlying.isNull())
4924 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
4927 T = Context.getTemplateSpecializationType(Name, Args.data(),
4928 Args.size(), Underlying);
4929 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4934 if (Record.size() != 1) {
4935 Error("Incorrect encoding of atomic type");
4938 QualType ValueType = readType(*Loc.F, Record, Idx);
4939 return Context.getAtomicType(ValueType);
4942 llvm_unreachable("Invalid TypeCode!");
4945 class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
4948 const ASTReader::RecordData &Record;
4951 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4953 return Reader.ReadSourceLocation(F, R, I);
4956 template<typename T>
4957 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4958 return Reader.ReadDeclAs<T>(F, Record, Idx);
4962 TypeLocReader(ASTReader &Reader, ModuleFile &F,
4963 const ASTReader::RecordData &Record, unsigned &Idx)
4964 : Reader(Reader), F(F), Record(Record), Idx(Idx)
4967 // We want compile-time assurance that we've enumerated all of
4968 // these, so unfortunately we have to declare them first, then
4969 // define them out-of-line.
4970 #define ABSTRACT_TYPELOC(CLASS, PARENT)
4971 #define TYPELOC(CLASS, PARENT) \
4972 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
4973 #include "clang/AST/TypeLocNodes.def"
4975 void VisitFunctionTypeLoc(FunctionTypeLoc);
4976 void VisitArrayTypeLoc(ArrayTypeLoc);
4979 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
4982 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
4983 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
4984 if (TL.needsExtraLocalData()) {
4985 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4986 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4987 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4988 TL.setModeAttr(Record[Idx++]);
4991 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
4992 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4994 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
4995 TL.setStarLoc(ReadSourceLocation(Record, Idx));
4997 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
5000 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
5001 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
5003 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
5004 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
5006 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
5007 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
5009 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
5010 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5011 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5013 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
5014 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
5015 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
5017 TL.setSizeExpr(Reader.ReadExpr(F));
5021 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
5022 VisitArrayTypeLoc(TL);
5024 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
5025 VisitArrayTypeLoc(TL);
5027 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
5028 VisitArrayTypeLoc(TL);
5030 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
5031 DependentSizedArrayTypeLoc TL) {
5032 VisitArrayTypeLoc(TL);
5034 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
5035 DependentSizedExtVectorTypeLoc TL) {
5036 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5038 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
5039 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5041 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
5042 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5044 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
5045 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
5046 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5047 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5048 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
5049 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
5050 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
5053 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
5054 VisitFunctionTypeLoc(TL);
5056 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
5057 VisitFunctionTypeLoc(TL);
5059 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
5060 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5062 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
5063 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5065 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
5066 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5067 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5068 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5070 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
5071 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5072 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5073 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5074 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5076 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
5077 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5079 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
5080 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5081 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5082 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5083 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5085 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
5086 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5088 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
5089 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5091 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
5092 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5094 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
5095 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
5096 if (TL.hasAttrOperand()) {
5098 range.setBegin(ReadSourceLocation(Record, Idx));
5099 range.setEnd(ReadSourceLocation(Record, Idx));
5100 TL.setAttrOperandParensRange(range);
5102 if (TL.hasAttrExprOperand()) {
5104 TL.setAttrExprOperand(Reader.ReadExpr(F));
5106 TL.setAttrExprOperand(0);
5107 } else if (TL.hasAttrEnumOperand())
5108 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
5110 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
5111 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5113 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
5114 SubstTemplateTypeParmTypeLoc TL) {
5115 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5117 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
5118 SubstTemplateTypeParmPackTypeLoc TL) {
5119 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5121 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
5122 TemplateSpecializationTypeLoc TL) {
5123 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5124 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5125 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5126 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5127 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
5129 Reader.GetTemplateArgumentLocInfo(F,
5130 TL.getTypePtr()->getArg(i).getKind(),
5133 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
5134 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5135 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5137 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
5138 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5139 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5141 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
5142 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5144 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
5145 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5146 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5147 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5149 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5150 DependentTemplateSpecializationTypeLoc TL) {
5151 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5152 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5153 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5154 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5155 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5156 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5157 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5159 Reader.GetTemplateArgumentLocInfo(F,
5160 TL.getTypePtr()->getArg(I).getKind(),
5163 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5164 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5166 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
5167 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5169 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5170 TL.setHasBaseTypeAsWritten(Record[Idx++]);
5171 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5172 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5173 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
5174 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
5176 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
5177 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5179 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5180 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5181 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5182 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5185 TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5186 const RecordData &Record,
5188 QualType InfoTy = readType(F, Record, Idx);
5189 if (InfoTy.isNull())
5192 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5193 TypeLocReader TLR(*this, F, Record, Idx);
5194 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5199 QualType ASTReader::GetType(TypeID ID) {
5200 unsigned FastQuals = ID & Qualifiers::FastMask;
5201 unsigned Index = ID >> Qualifiers::FastWidth;
5203 if (Index < NUM_PREDEF_TYPE_IDS) {
5205 switch ((PredefinedTypeIDs)Index) {
5206 case PREDEF_TYPE_NULL_ID: return QualType();
5207 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
5208 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
5210 case PREDEF_TYPE_CHAR_U_ID:
5211 case PREDEF_TYPE_CHAR_S_ID:
5212 // FIXME: Check that the signedness of CharTy is correct!
5216 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
5217 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
5218 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
5219 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
5220 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
5221 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
5222 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
5223 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
5224 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
5225 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
5226 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
5227 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
5228 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
5229 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
5230 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
5231 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
5232 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
5233 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
5234 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
5235 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
5236 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
5237 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
5238 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
5239 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
5240 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
5241 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
5242 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
5243 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
5244 case PREDEF_TYPE_IMAGE1D_ID: T = Context.OCLImage1dTy; break;
5245 case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break;
5246 case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break;
5247 case PREDEF_TYPE_IMAGE2D_ID: T = Context.OCLImage2dTy; break;
5248 case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break;
5249 case PREDEF_TYPE_IMAGE3D_ID: T = Context.OCLImage3dTy; break;
5250 case PREDEF_TYPE_SAMPLER_ID: T = Context.OCLSamplerTy; break;
5251 case PREDEF_TYPE_EVENT_ID: T = Context.OCLEventTy; break;
5252 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
5254 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
5255 T = Context.getAutoRRefDeductType();
5258 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
5259 T = Context.ARCUnbridgedCastTy;
5262 case PREDEF_TYPE_VA_LIST_TAG:
5263 T = Context.getVaListTagType();
5266 case PREDEF_TYPE_BUILTIN_FN:
5267 T = Context.BuiltinFnTy;
5271 assert(!T.isNull() && "Unknown predefined type");
5272 return T.withFastQualifiers(FastQuals);
5275 Index -= NUM_PREDEF_TYPE_IDS;
5276 assert(Index < TypesLoaded.size() && "Type index out-of-range");
5277 if (TypesLoaded[Index].isNull()) {
5278 TypesLoaded[Index] = readTypeRecord(Index);
5279 if (TypesLoaded[Index].isNull())
5282 TypesLoaded[Index]->setFromAST();
5283 if (DeserializationListener)
5284 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
5285 TypesLoaded[Index]);
5288 return TypesLoaded[Index].withFastQualifiers(FastQuals);
5291 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
5292 return GetType(getGlobalTypeID(F, LocalID));
5295 serialization::TypeID
5296 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
5297 unsigned FastQuals = LocalID & Qualifiers::FastMask;
5298 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
5300 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
5303 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5304 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
5305 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
5307 unsigned GlobalIndex = LocalIndex + I->second;
5308 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
5311 TemplateArgumentLocInfo
5312 ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
5313 TemplateArgument::ArgKind Kind,
5314 const RecordData &Record,
5317 case TemplateArgument::Expression:
5319 case TemplateArgument::Type:
5320 return GetTypeSourceInfo(F, Record, Index);
5321 case TemplateArgument::Template: {
5322 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5324 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5325 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5328 case TemplateArgument::TemplateExpansion: {
5329 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5331 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5332 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
5333 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5336 case TemplateArgument::Null:
5337 case TemplateArgument::Integral:
5338 case TemplateArgument::Declaration:
5339 case TemplateArgument::NullPtr:
5340 case TemplateArgument::Pack:
5341 // FIXME: Is this right?
5342 return TemplateArgumentLocInfo();
5344 llvm_unreachable("unexpected template argument loc");
5348 ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
5349 const RecordData &Record, unsigned &Index) {
5350 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
5352 if (Arg.getKind() == TemplateArgument::Expression) {
5353 if (Record[Index++]) // bool InfoHasSameExpr.
5354 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
5356 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
5360 const ASTTemplateArgumentListInfo*
5361 ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
5362 const RecordData &Record,
5364 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
5365 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
5366 unsigned NumArgsAsWritten = Record[Index++];
5367 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
5368 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
5369 TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
5370 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
5373 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
5377 uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
5379 if (Idx >= Record.size())
5382 unsigned LocalID = Record[Idx++];
5383 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
5386 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
5387 RecordLocation Loc = getLocalBitOffset(Offset);
5388 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
5389 SavedStreamPosition SavedPosition(Cursor);
5390 Cursor.JumpToBit(Loc.Offset);
5391 ReadingKindTracker ReadingKind(Read_Decl, *this);
5393 unsigned Code = Cursor.ReadCode();
5394 unsigned RecCode = Cursor.readRecord(Code, Record);
5395 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
5396 Error("Malformed AST file: missing C++ base specifiers");
5401 unsigned NumBases = Record[Idx++];
5402 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
5403 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
5404 for (unsigned I = 0; I != NumBases; ++I)
5405 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
5409 serialization::DeclID
5410 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
5411 if (LocalID < NUM_PREDEF_DECL_IDS)
5414 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5415 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
5416 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
5418 return LocalID + I->second;
5421 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
5422 ModuleFile &M) const {
5423 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
5424 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5425 return &M == I->second;
5428 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
5429 if (!D->isFromASTFile())
5431 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
5432 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5436 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
5437 if (ID < NUM_PREDEF_DECL_IDS)
5438 return SourceLocation();
5440 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5442 if (Index > DeclsLoaded.size()) {
5443 Error("declaration ID out-of-range for AST file");
5444 return SourceLocation();
5447 if (Decl *D = DeclsLoaded[Index])
5448 return D->getLocation();
5450 unsigned RawLocation = 0;
5451 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
5452 return ReadSourceLocation(*Rec.F, RawLocation);
5455 Decl *ASTReader::GetDecl(DeclID ID) {
5456 if (ID < NUM_PREDEF_DECL_IDS) {
5457 switch ((PredefinedDeclIDs)ID) {
5458 case PREDEF_DECL_NULL_ID:
5461 case PREDEF_DECL_TRANSLATION_UNIT_ID:
5462 return Context.getTranslationUnitDecl();
5464 case PREDEF_DECL_OBJC_ID_ID:
5465 return Context.getObjCIdDecl();
5467 case PREDEF_DECL_OBJC_SEL_ID:
5468 return Context.getObjCSelDecl();
5470 case PREDEF_DECL_OBJC_CLASS_ID:
5471 return Context.getObjCClassDecl();
5473 case PREDEF_DECL_OBJC_PROTOCOL_ID:
5474 return Context.getObjCProtocolDecl();
5476 case PREDEF_DECL_INT_128_ID:
5477 return Context.getInt128Decl();
5479 case PREDEF_DECL_UNSIGNED_INT_128_ID:
5480 return Context.getUInt128Decl();
5482 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
5483 return Context.getObjCInstanceTypeDecl();
5485 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
5486 return Context.getBuiltinVaListDecl();
5490 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5492 if (Index >= DeclsLoaded.size()) {
5493 assert(0 && "declaration ID out-of-range for AST file");
5494 Error("declaration ID out-of-range for AST file");
5498 if (!DeclsLoaded[Index]) {
5500 if (DeserializationListener)
5501 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
5504 return DeclsLoaded[Index];
5507 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
5509 if (GlobalID < NUM_PREDEF_DECL_IDS)
5512 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
5513 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5514 ModuleFile *Owner = I->second;
5516 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
5517 = M.GlobalToLocalDeclIDs.find(Owner);
5518 if (Pos == M.GlobalToLocalDeclIDs.end())
5521 return GlobalID - Owner->BaseDeclID + Pos->second;
5524 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
5525 const RecordData &Record,
5527 if (Idx >= Record.size()) {
5528 Error("Corrupted AST file");
5532 return getGlobalDeclID(F, Record[Idx++]);
5535 /// \brief Resolve the offset of a statement into a statement.
5537 /// This operation will read a new statement from the external
5538 /// source each time it is called, and is meant to be used via a
5539 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
5540 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
5541 // Switch case IDs are per Decl.
5542 ClearSwitchCaseIDs();
5544 // Offset here is a global offset across the entire chain.
5545 RecordLocation Loc = getLocalBitOffset(Offset);
5546 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
5547 return ReadStmtFromStream(*Loc.F);
5551 class FindExternalLexicalDeclsVisitor {
5553 const DeclContext *DC;
5554 bool (*isKindWeWant)(Decl::Kind);
5556 SmallVectorImpl<Decl*> &Decls;
5557 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
5560 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
5561 bool (*isKindWeWant)(Decl::Kind),
5562 SmallVectorImpl<Decl*> &Decls)
5563 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
5565 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
5566 PredefsVisited[I] = false;
5569 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
5573 FindExternalLexicalDeclsVisitor *This
5574 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
5576 ModuleFile::DeclContextInfosMap::iterator Info
5577 = M.DeclContextInfos.find(This->DC);
5578 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
5581 // Load all of the declaration IDs
5582 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
5583 *IDE = ID + Info->second.NumLexicalDecls;
5585 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
5588 // Don't add predefined declarations to the lexical context more
5590 if (ID->second < NUM_PREDEF_DECL_IDS) {
5591 if (This->PredefsVisited[ID->second])
5594 This->PredefsVisited[ID->second] = true;
5597 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
5598 if (!This->DC->isDeclInLexicalTraversal(D))
5599 This->Decls.push_back(D);
5608 ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
5609 bool (*isKindWeWant)(Decl::Kind),
5610 SmallVectorImpl<Decl*> &Decls) {
5611 // There might be lexical decls in multiple modules, for the TU at
5612 // least. Walk all of the modules in the order they were loaded.
5613 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
5614 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
5615 ++NumLexicalDeclContextsRead;
5626 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
5628 bool operator()(LocalDeclID L, LocalDeclID R) const {
5629 SourceLocation LHS = getLocation(L);
5630 SourceLocation RHS = getLocation(R);
5631 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5634 bool operator()(SourceLocation LHS, LocalDeclID R) const {
5635 SourceLocation RHS = getLocation(R);
5636 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5639 bool operator()(LocalDeclID L, SourceLocation RHS) const {
5640 SourceLocation LHS = getLocation(L);
5641 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5644 SourceLocation getLocation(LocalDeclID ID) const {
5645 return Reader.getSourceManager().getFileLoc(
5646 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
5652 void ASTReader::FindFileRegionDecls(FileID File,
5653 unsigned Offset, unsigned Length,
5654 SmallVectorImpl<Decl *> &Decls) {
5655 SourceManager &SM = getSourceManager();
5657 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
5658 if (I == FileDeclIDs.end())
5661 FileDeclsInfo &DInfo = I->second;
5662 if (DInfo.Decls.empty())
5666 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
5667 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
5669 DeclIDComp DIDComp(*this, *DInfo.Mod);
5670 ArrayRef<serialization::LocalDeclID>::iterator
5671 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5673 if (BeginIt != DInfo.Decls.begin())
5676 // If we are pointing at a top-level decl inside an objc container, we need
5677 // to backtrack until we find it otherwise we will fail to report that the
5678 // region overlaps with an objc container.
5679 while (BeginIt != DInfo.Decls.begin() &&
5680 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
5681 ->isTopLevelDeclInObjCContainer())
5684 ArrayRef<serialization::LocalDeclID>::iterator
5685 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5687 if (EndIt != DInfo.Decls.end())
5690 for (ArrayRef<serialization::LocalDeclID>::iterator
5691 DIt = BeginIt; DIt != EndIt; ++DIt)
5692 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
5696 /// \brief ModuleFile visitor used to perform name lookup into a
5697 /// declaration context.
5698 class DeclContextNameLookupVisitor {
5700 SmallVectorImpl<const DeclContext *> &Contexts;
5701 DeclarationName Name;
5702 SmallVectorImpl<NamedDecl *> &Decls;
5705 DeclContextNameLookupVisitor(ASTReader &Reader,
5706 SmallVectorImpl<const DeclContext *> &Contexts,
5707 DeclarationName Name,
5708 SmallVectorImpl<NamedDecl *> &Decls)
5709 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
5711 static bool visit(ModuleFile &M, void *UserData) {
5712 DeclContextNameLookupVisitor *This
5713 = static_cast<DeclContextNameLookupVisitor *>(UserData);
5715 // Check whether we have any visible declaration information for
5716 // this context in this module.
5717 ModuleFile::DeclContextInfosMap::iterator Info;
5718 bool FoundInfo = false;
5719 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5720 Info = M.DeclContextInfos.find(This->Contexts[I]);
5721 if (Info != M.DeclContextInfos.end() &&
5722 Info->second.NameLookupTableData) {
5731 // Look for this name within this module.
5732 ASTDeclContextNameLookupTable *LookupTable =
5733 Info->second.NameLookupTableData;
5734 ASTDeclContextNameLookupTable::iterator Pos
5735 = LookupTable->find(This->Name);
5736 if (Pos == LookupTable->end())
5739 bool FoundAnything = false;
5740 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5741 for (; Data.first != Data.second; ++Data.first) {
5742 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5746 if (ND->getDeclName() != This->Name) {
5747 // A name might be null because the decl's redeclarable part is
5748 // currently read before reading its name. The lookup is triggered by
5749 // building that decl (likely indirectly), and so it is later in the
5750 // sense of "already existing" and can be ignored here.
5754 // Record this declaration.
5755 FoundAnything = true;
5756 This->Decls.push_back(ND);
5759 return FoundAnything;
5764 /// \brief Retrieve the "definitive" module file for the definition of the
5765 /// given declaration context, if there is one.
5767 /// The "definitive" module file is the only place where we need to look to
5768 /// find information about the declarations within the given declaration
5769 /// context. For example, C++ and Objective-C classes, C structs/unions, and
5770 /// Objective-C protocols, categories, and extensions are all defined in a
5771 /// single place in the source code, so they have definitive module files
5772 /// associated with them. C++ namespaces, on the other hand, can have
5773 /// definitions in multiple different module files.
5775 /// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's
5776 /// NDEBUG checking.
5777 static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC,
5778 ASTReader &Reader) {
5779 if (const DeclContext *DefDC = getDefinitiveDeclContext(DC))
5780 return Reader.getOwningModuleFile(cast<Decl>(DefDC));
5786 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
5787 DeclarationName Name) {
5788 assert(DC->hasExternalVisibleStorage() &&
5789 "DeclContext has no visible decls in storage");
5793 SmallVector<NamedDecl *, 64> Decls;
5795 // Compute the declaration contexts we need to look into. Multiple such
5796 // declaration contexts occur when two declaration contexts from disjoint
5797 // modules get merged, e.g., when two namespaces with the same name are
5798 // independently defined in separate modules.
5799 SmallVector<const DeclContext *, 2> Contexts;
5800 Contexts.push_back(DC);
5802 if (DC->isNamespace()) {
5803 MergedDeclsMap::iterator Merged
5804 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5805 if (Merged != MergedDecls.end()) {
5806 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5807 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5811 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
5813 // If we can definitively determine which module file to look into,
5814 // only look there. Otherwise, look in all module files.
5815 ModuleFile *Definitive;
5816 if (Contexts.size() == 1 &&
5817 (Definitive = getDefinitiveModuleFileFor(DC, *this))) {
5818 DeclContextNameLookupVisitor::visit(*Definitive, &Visitor);
5820 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
5822 ++NumVisibleDeclContextsRead;
5823 SetExternalVisibleDeclsForName(DC, Name, Decls);
5824 return !Decls.empty();
5828 /// \brief ModuleFile visitor used to retrieve all visible names in a
5829 /// declaration context.
5830 class DeclContextAllNamesVisitor {
5832 SmallVectorImpl<const DeclContext *> &Contexts;
5837 DeclContextAllNamesVisitor(ASTReader &Reader,
5838 SmallVectorImpl<const DeclContext *> &Contexts,
5839 DeclsMap &Decls, bool VisitAll)
5840 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { }
5842 static bool visit(ModuleFile &M, void *UserData) {
5843 DeclContextAllNamesVisitor *This
5844 = static_cast<DeclContextAllNamesVisitor *>(UserData);
5846 // Check whether we have any visible declaration information for
5847 // this context in this module.
5848 ModuleFile::DeclContextInfosMap::iterator Info;
5849 bool FoundInfo = false;
5850 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5851 Info = M.DeclContextInfos.find(This->Contexts[I]);
5852 if (Info != M.DeclContextInfos.end() &&
5853 Info->second.NameLookupTableData) {
5862 ASTDeclContextNameLookupTable *LookupTable =
5863 Info->second.NameLookupTableData;
5864 bool FoundAnything = false;
5865 for (ASTDeclContextNameLookupTable::data_iterator
5866 I = LookupTable->data_begin(), E = LookupTable->data_end();
5869 ASTDeclContextNameLookupTrait::data_type Data = *I;
5870 for (; Data.first != Data.second; ++Data.first) {
5871 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5876 // Record this declaration.
5877 FoundAnything = true;
5878 This->Decls[ND->getDeclName()].push_back(ND);
5882 return FoundAnything && !This->VisitAll;
5887 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
5888 if (!DC->hasExternalVisibleStorage())
5892 // Compute the declaration contexts we need to look into. Multiple such
5893 // declaration contexts occur when two declaration contexts from disjoint
5894 // modules get merged, e.g., when two namespaces with the same name are
5895 // independently defined in separate modules.
5896 SmallVector<const DeclContext *, 2> Contexts;
5897 Contexts.push_back(DC);
5899 if (DC->isNamespace()) {
5900 MergedDeclsMap::iterator Merged
5901 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5902 if (Merged != MergedDecls.end()) {
5903 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5904 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5908 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls,
5909 /*VisitAll=*/DC->isFileContext());
5910 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5911 ++NumVisibleDeclContextsRead;
5913 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5914 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5916 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
5919 /// \brief Under non-PCH compilation the consumer receives the objc methods
5920 /// before receiving the implementation, and codegen depends on this.
5921 /// We simulate this by deserializing and passing to consumer the methods of the
5922 /// implementation before passing the deserialized implementation decl.
5923 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5924 ASTConsumer *Consumer) {
5925 assert(ImplD && Consumer);
5927 for (ObjCImplDecl::method_iterator
5928 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
5929 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
5931 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5934 void ASTReader::PassInterestingDeclsToConsumer() {
5936 while (!InterestingDecls.empty()) {
5937 Decl *D = InterestingDecls.front();
5938 InterestingDecls.pop_front();
5940 PassInterestingDeclToConsumer(D);
5944 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5945 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5946 PassObjCImplDeclToConsumer(ImplD, Consumer);
5948 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5951 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
5952 this->Consumer = Consumer;
5957 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
5958 // Force deserialization of this decl, which will cause it to be queued for
5959 // passing to the consumer.
5960 GetDecl(ExternalDefinitions[I]);
5962 ExternalDefinitions.clear();
5964 PassInterestingDeclsToConsumer();
5967 void ASTReader::PrintStats() {
5968 std::fprintf(stderr, "*** AST File Statistics:\n");
5970 unsigned NumTypesLoaded
5971 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
5973 unsigned NumDeclsLoaded
5974 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5976 unsigned NumIdentifiersLoaded
5977 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5978 IdentifiersLoaded.end(),
5979 (IdentifierInfo *)0);
5980 unsigned NumMacrosLoaded
5981 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5984 unsigned NumSelectorsLoaded
5985 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5986 SelectorsLoaded.end(),
5989 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
5990 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5991 NumSLocEntriesRead, TotalNumSLocEntries,
5992 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
5993 if (!TypesLoaded.empty())
5994 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
5995 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5996 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5997 if (!DeclsLoaded.empty())
5998 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
5999 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
6000 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
6001 if (!IdentifiersLoaded.empty())
6002 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
6003 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
6004 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
6005 if (!MacrosLoaded.empty())
6006 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6007 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
6008 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
6009 if (!SelectorsLoaded.empty())
6010 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
6011 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
6012 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
6013 if (TotalNumStatements)
6014 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
6015 NumStatementsRead, TotalNumStatements,
6016 ((float)NumStatementsRead/TotalNumStatements * 100));
6018 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6019 NumMacrosRead, TotalNumMacros,
6020 ((float)NumMacrosRead/TotalNumMacros * 100));
6021 if (TotalLexicalDeclContexts)
6022 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
6023 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
6024 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
6026 if (TotalVisibleDeclContexts)
6027 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
6028 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
6029 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
6031 if (TotalNumMethodPoolEntries) {
6032 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
6033 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
6034 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
6037 if (NumMethodPoolLookups) {
6038 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
6039 NumMethodPoolHits, NumMethodPoolLookups,
6040 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
6042 if (NumMethodPoolTableLookups) {
6043 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
6044 NumMethodPoolTableHits, NumMethodPoolTableLookups,
6045 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
6049 if (NumIdentifierLookupHits) {
6050 std::fprintf(stderr,
6051 " %u / %u identifier table lookups succeeded (%f%%)\n",
6052 NumIdentifierLookupHits, NumIdentifierLookups,
6053 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
6057 std::fprintf(stderr, "\n");
6058 GlobalIndex->printStats();
6061 std::fprintf(stderr, "\n");
6063 std::fprintf(stderr, "\n");
6066 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
6068 dumpModuleIDMap(StringRef Name,
6069 const ContinuousRangeMap<Key, ModuleFile *,
6070 InitialCapacity> &Map) {
6071 if (Map.begin() == Map.end())
6074 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
6075 llvm::errs() << Name << ":\n";
6076 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
6078 llvm::errs() << " " << I->first << " -> " << I->second->FileName
6083 void ASTReader::dump() {
6084 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
6085 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
6086 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
6087 dumpModuleIDMap("Global type map", GlobalTypeMap);
6088 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
6089 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
6090 dumpModuleIDMap("Global macro map", GlobalMacroMap);
6091 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
6092 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
6093 dumpModuleIDMap("Global preprocessed entity map",
6094 GlobalPreprocessedEntityMap);
6096 llvm::errs() << "\n*** PCH/Modules Loaded:";
6097 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
6098 MEnd = ModuleMgr.end();
6103 /// Return the amount of memory used by memory buffers, breaking down
6104 /// by heap-backed versus mmap'ed memory.
6105 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
6106 for (ModuleConstIterator I = ModuleMgr.begin(),
6107 E = ModuleMgr.end(); I != E; ++I) {
6108 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
6109 size_t bytes = buf->getBufferSize();
6110 switch (buf->getBufferKind()) {
6111 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
6112 sizes.malloc_bytes += bytes;
6114 case llvm::MemoryBuffer::MemoryBuffer_MMap:
6115 sizes.mmap_bytes += bytes;
6122 void ASTReader::InitializeSema(Sema &S) {
6124 S.addExternalSource(this);
6126 // Makes sure any declarations that were deserialized "too early"
6127 // still get added to the identifier's declaration chains.
6128 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
6129 pushExternalDeclIntoScope(PreloadedDecls[I],
6130 PreloadedDecls[I]->getDeclName());
6132 PreloadedDecls.clear();
6134 // FIXME: What happens if these are changed by a module import?
6135 if (!FPPragmaOptions.empty()) {
6136 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
6137 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
6140 // FIXME: What happens if these are changed by a module import?
6141 if (!OpenCLExtensions.empty()) {
6143 #define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
6144 #include "clang/Basic/OpenCLExtensions.def"
6146 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
6152 void ASTReader::UpdateSema() {
6153 assert(SemaObj && "no Sema to update");
6155 // Load the offsets of the declarations that Sema references.
6156 // They will be lazily deserialized when needed.
6157 if (!SemaDeclRefs.empty()) {
6158 assert(SemaDeclRefs.size() % 2 == 0);
6159 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 2) {
6160 if (!SemaObj->StdNamespace)
6161 SemaObj->StdNamespace = SemaDeclRefs[I];
6162 if (!SemaObj->StdBadAlloc)
6163 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
6165 SemaDeclRefs.clear();
6169 IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
6170 // Note that we are loading an identifier.
6171 Deserializing AnIdentifier(this);
6172 StringRef Name(NameStart, NameEnd - NameStart);
6174 // If there is a global index, look there first to determine which modules
6175 // provably do not have any results for this identifier.
6176 GlobalModuleIndex::HitSet Hits;
6177 GlobalModuleIndex::HitSet *HitsPtr = 0;
6178 if (!loadGlobalIndex()) {
6179 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
6183 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
6184 NumIdentifierLookups,
6185 NumIdentifierLookupHits);
6186 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
6187 IdentifierInfo *II = Visitor.getIdentifierInfo();
6188 markIdentifierUpToDate(II);
6193 /// \brief An identifier-lookup iterator that enumerates all of the
6194 /// identifiers stored within a set of AST files.
6195 class ASTIdentifierIterator : public IdentifierIterator {
6196 /// \brief The AST reader whose identifiers are being enumerated.
6197 const ASTReader &Reader;
6199 /// \brief The current index into the chain of AST files stored in
6203 /// \brief The current position within the identifier lookup table
6204 /// of the current AST file.
6205 ASTIdentifierLookupTable::key_iterator Current;
6207 /// \brief The end position within the identifier lookup table of
6208 /// the current AST file.
6209 ASTIdentifierLookupTable::key_iterator End;
6212 explicit ASTIdentifierIterator(const ASTReader &Reader);
6214 virtual StringRef Next();
6218 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
6219 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
6220 ASTIdentifierLookupTable *IdTable
6221 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
6222 Current = IdTable->key_begin();
6223 End = IdTable->key_end();
6226 StringRef ASTIdentifierIterator::Next() {
6227 while (Current == End) {
6228 // If we have exhausted all of our AST files, we're done.
6233 ASTIdentifierLookupTable *IdTable
6234 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
6235 IdentifierLookupTable;
6236 Current = IdTable->key_begin();
6237 End = IdTable->key_end();
6240 // We have any identifiers remaining in the current AST file; return
6242 StringRef Result = *Current;
6247 IdentifierIterator *ASTReader::getIdentifiers() {
6248 if (!loadGlobalIndex())
6249 return GlobalIndex->createIdentifierIterator();
6251 return new ASTIdentifierIterator(*this);
6254 namespace clang { namespace serialization {
6255 class ReadMethodPoolVisitor {
6258 unsigned PriorGeneration;
6259 unsigned InstanceBits;
6260 unsigned FactoryBits;
6261 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
6262 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
6265 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
6266 unsigned PriorGeneration)
6267 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration),
6268 InstanceBits(0), FactoryBits(0) { }
6270 static bool visit(ModuleFile &M, void *UserData) {
6271 ReadMethodPoolVisitor *This
6272 = static_cast<ReadMethodPoolVisitor *>(UserData);
6274 if (!M.SelectorLookupTable)
6277 // If we've already searched this module file, skip it now.
6278 if (M.Generation <= This->PriorGeneration)
6281 ++This->Reader.NumMethodPoolTableLookups;
6282 ASTSelectorLookupTable *PoolTable
6283 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
6284 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
6285 if (Pos == PoolTable->end())
6288 ++This->Reader.NumMethodPoolTableHits;
6289 ++This->Reader.NumSelectorsRead;
6290 // FIXME: Not quite happy with the statistics here. We probably should
6291 // disable this tracking when called via LoadSelector.
6292 // Also, should entries without methods count as misses?
6293 ++This->Reader.NumMethodPoolEntriesRead;
6294 ASTSelectorLookupTrait::data_type Data = *Pos;
6295 if (This->Reader.DeserializationListener)
6296 This->Reader.DeserializationListener->SelectorRead(Data.ID,
6299 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
6300 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
6301 This->InstanceBits = Data.InstanceBits;
6302 This->FactoryBits = Data.FactoryBits;
6306 /// \brief Retrieve the instance methods found by this visitor.
6307 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
6308 return InstanceMethods;
6311 /// \brief Retrieve the instance methods found by this visitor.
6312 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
6313 return FactoryMethods;
6316 unsigned getInstanceBits() const { return InstanceBits; }
6317 unsigned getFactoryBits() const { return FactoryBits; }
6319 } } // end namespace clang::serialization
6321 /// \brief Add the given set of methods to the method list.
6322 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
6323 ObjCMethodList &List) {
6324 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
6325 S.addMethodToGlobalList(&List, Methods[I]);
6329 void ASTReader::ReadMethodPool(Selector Sel) {
6330 // Get the selector generation and update it to the current generation.
6331 unsigned &Generation = SelectorGeneration[Sel];
6332 unsigned PriorGeneration = Generation;
6333 Generation = CurrentGeneration;
6335 // Search for methods defined with this selector.
6336 ++NumMethodPoolLookups;
6337 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
6338 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
6340 if (Visitor.getInstanceMethods().empty() &&
6341 Visitor.getFactoryMethods().empty())
6344 ++NumMethodPoolHits;
6349 Sema &S = *getSema();
6350 Sema::GlobalMethodPool::iterator Pos
6351 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
6353 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
6354 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
6355 Pos->second.first.setBits(Visitor.getInstanceBits());
6356 Pos->second.second.setBits(Visitor.getFactoryBits());
6359 void ASTReader::ReadKnownNamespaces(
6360 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
6363 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
6364 if (NamespaceDecl *Namespace
6365 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
6366 Namespaces.push_back(Namespace);
6370 void ASTReader::ReadUndefinedButUsed(
6371 llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) {
6372 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
6373 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
6374 SourceLocation Loc =
6375 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
6376 Undefined.insert(std::make_pair(D, Loc));
6380 void ASTReader::ReadTentativeDefinitions(
6381 SmallVectorImpl<VarDecl *> &TentativeDefs) {
6382 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
6383 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
6385 TentativeDefs.push_back(Var);
6387 TentativeDefinitions.clear();
6390 void ASTReader::ReadUnusedFileScopedDecls(
6391 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
6392 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
6394 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
6398 UnusedFileScopedDecls.clear();
6401 void ASTReader::ReadDelegatingConstructors(
6402 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
6403 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
6404 CXXConstructorDecl *D
6405 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
6409 DelegatingCtorDecls.clear();
6412 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
6413 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
6415 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
6419 ExtVectorDecls.clear();
6422 void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
6423 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
6425 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
6429 DynamicClasses.clear();
6433 ASTReader::ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl *> &Decls) {
6434 for (unsigned I = 0, N = LocallyScopedExternCDecls.size(); I != N; ++I) {
6436 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternCDecls[I]));
6440 LocallyScopedExternCDecls.clear();
6443 void ASTReader::ReadReferencedSelectors(
6444 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
6445 if (ReferencedSelectorsData.empty())
6448 // If there are @selector references added them to its pool. This is for
6449 // implementation of -Wselector.
6450 unsigned int DataSize = ReferencedSelectorsData.size()-1;
6452 while (I < DataSize) {
6453 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
6454 SourceLocation SelLoc
6455 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
6456 Sels.push_back(std::make_pair(Sel, SelLoc));
6458 ReferencedSelectorsData.clear();
6461 void ASTReader::ReadWeakUndeclaredIdentifiers(
6462 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
6463 if (WeakUndeclaredIdentifiers.empty())
6466 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
6467 IdentifierInfo *WeakId
6468 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6469 IdentifierInfo *AliasId
6470 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6472 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
6473 bool Used = WeakUndeclaredIdentifiers[I++];
6474 WeakInfo WI(AliasId, Loc);
6476 WeakIDs.push_back(std::make_pair(WeakId, WI));
6478 WeakUndeclaredIdentifiers.clear();
6481 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
6482 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
6483 ExternalVTableUse VT;
6484 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
6485 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
6486 VT.DefinitionRequired = VTableUses[Idx++];
6487 VTables.push_back(VT);
6493 void ASTReader::ReadPendingInstantiations(
6494 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
6495 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
6496 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
6498 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
6500 Pending.push_back(std::make_pair(D, Loc));
6502 PendingInstantiations.clear();
6505 void ASTReader::ReadLateParsedTemplates(
6506 llvm::DenseMap<const FunctionDecl *, LateParsedTemplate *> &LPTMap) {
6507 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
6509 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
6511 LateParsedTemplate *LT = new LateParsedTemplate;
6512 LT->D = GetDecl(LateParsedTemplates[Idx++]);
6514 ModuleFile *F = getOwningModuleFile(LT->D);
6515 assert(F && "No module");
6517 unsigned TokN = LateParsedTemplates[Idx++];
6518 LT->Toks.reserve(TokN);
6519 for (unsigned T = 0; T < TokN; ++T)
6520 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
6525 LateParsedTemplates.clear();
6528 void ASTReader::LoadSelector(Selector Sel) {
6529 // It would be complicated to avoid reading the methods anyway. So don't.
6530 ReadMethodPool(Sel);
6533 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
6534 assert(ID && "Non-zero identifier ID required");
6535 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
6536 IdentifiersLoaded[ID - 1] = II;
6537 if (DeserializationListener)
6538 DeserializationListener->IdentifierRead(ID, II);
6541 /// \brief Set the globally-visible declarations associated with the given
6544 /// If the AST reader is currently in a state where the given declaration IDs
6545 /// cannot safely be resolved, they are queued until it is safe to resolve
6548 /// \param II an IdentifierInfo that refers to one or more globally-visible
6551 /// \param DeclIDs the set of declaration IDs with the name @p II that are
6552 /// visible at global scope.
6554 /// \param Decls if non-null, this vector will be populated with the set of
6555 /// deserialized declarations. These declarations will not be pushed into
6558 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
6559 const SmallVectorImpl<uint32_t> &DeclIDs,
6560 SmallVectorImpl<Decl *> *Decls) {
6561 if (NumCurrentElementsDeserializing && !Decls) {
6562 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
6566 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
6567 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
6569 // If we're simply supposed to record the declarations, do so now.
6571 Decls->push_back(D);
6575 // Introduce this declaration into the translation-unit scope
6576 // and add it to the declaration chain for this identifier, so
6577 // that (unqualified) name lookup will find it.
6578 pushExternalDeclIntoScope(D, II);
6580 // Queue this declaration so that it will be added to the
6581 // translation unit scope and identifier's declaration chain
6582 // once a Sema object is known.
6583 PreloadedDecls.push_back(D);
6588 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
6592 if (IdentifiersLoaded.empty()) {
6593 Error("no identifier table in AST file");
6598 if (!IdentifiersLoaded[ID]) {
6599 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
6600 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
6601 ModuleFile *M = I->second;
6602 unsigned Index = ID - M->BaseIdentifierID;
6603 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
6605 // All of the strings in the AST file are preceded by a 16-bit length.
6606 // Extract that 16-bit length to avoid having to execute strlen().
6607 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
6608 // unsigned integers. This is important to avoid integer overflow when
6609 // we cast them to 'unsigned'.
6610 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
6611 unsigned StrLen = (((unsigned) StrLenPtr[0])
6612 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
6613 IdentifiersLoaded[ID]
6614 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
6615 if (DeserializationListener)
6616 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
6619 return IdentifiersLoaded[ID];
6622 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
6623 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
6626 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
6627 if (LocalID < NUM_PREDEF_IDENT_IDS)
6630 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6631 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
6632 assert(I != M.IdentifierRemap.end()
6633 && "Invalid index into identifier index remap");
6635 return LocalID + I->second;
6638 MacroInfo *ASTReader::getMacro(MacroID ID) {
6642 if (MacrosLoaded.empty()) {
6643 Error("no macro table in AST file");
6647 ID -= NUM_PREDEF_MACRO_IDS;
6648 if (!MacrosLoaded[ID]) {
6649 GlobalMacroMapType::iterator I
6650 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
6651 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
6652 ModuleFile *M = I->second;
6653 unsigned Index = ID - M->BaseMacroID;
6654 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
6656 if (DeserializationListener)
6657 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
6661 return MacrosLoaded[ID];
6664 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
6665 if (LocalID < NUM_PREDEF_MACRO_IDS)
6668 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6669 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
6670 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
6672 return LocalID + I->second;
6675 serialization::SubmoduleID
6676 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
6677 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
6680 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6681 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
6682 assert(I != M.SubmoduleRemap.end()
6683 && "Invalid index into submodule index remap");
6685 return LocalID + I->second;
6688 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
6689 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6690 assert(GlobalID == 0 && "Unhandled global submodule ID");
6694 if (GlobalID > SubmodulesLoaded.size()) {
6695 Error("submodule ID out of range in AST file");
6699 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
6702 Module *ASTReader::getModule(unsigned ID) {
6703 return getSubmodule(ID);
6706 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
6707 return DecodeSelector(getGlobalSelectorID(M, LocalID));
6710 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
6714 if (ID > SelectorsLoaded.size()) {
6715 Error("selector ID out of range in AST file");
6719 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
6720 // Load this selector from the selector table.
6721 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
6722 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
6723 ModuleFile &M = *I->second;
6724 ASTSelectorLookupTrait Trait(*this, M);
6725 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
6726 SelectorsLoaded[ID - 1] =
6727 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
6728 if (DeserializationListener)
6729 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
6732 return SelectorsLoaded[ID - 1];
6735 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
6736 return DecodeSelector(ID);
6739 uint32_t ASTReader::GetNumExternalSelectors() {
6740 // ID 0 (the null selector) is considered an external selector.
6741 return getTotalNumSelectors() + 1;
6744 serialization::SelectorID
6745 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
6746 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
6749 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6750 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
6751 assert(I != M.SelectorRemap.end()
6752 && "Invalid index into selector index remap");
6754 return LocalID + I->second;
6758 ASTReader::ReadDeclarationName(ModuleFile &F,
6759 const RecordData &Record, unsigned &Idx) {
6760 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
6762 case DeclarationName::Identifier:
6763 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
6765 case DeclarationName::ObjCZeroArgSelector:
6766 case DeclarationName::ObjCOneArgSelector:
6767 case DeclarationName::ObjCMultiArgSelector:
6768 return DeclarationName(ReadSelector(F, Record, Idx));
6770 case DeclarationName::CXXConstructorName:
6771 return Context.DeclarationNames.getCXXConstructorName(
6772 Context.getCanonicalType(readType(F, Record, Idx)));
6774 case DeclarationName::CXXDestructorName:
6775 return Context.DeclarationNames.getCXXDestructorName(
6776 Context.getCanonicalType(readType(F, Record, Idx)));
6778 case DeclarationName::CXXConversionFunctionName:
6779 return Context.DeclarationNames.getCXXConversionFunctionName(
6780 Context.getCanonicalType(readType(F, Record, Idx)));
6782 case DeclarationName::CXXOperatorName:
6783 return Context.DeclarationNames.getCXXOperatorName(
6784 (OverloadedOperatorKind)Record[Idx++]);
6786 case DeclarationName::CXXLiteralOperatorName:
6787 return Context.DeclarationNames.getCXXLiteralOperatorName(
6788 GetIdentifierInfo(F, Record, Idx));
6790 case DeclarationName::CXXUsingDirective:
6791 return DeclarationName::getUsingDirectiveName();
6794 llvm_unreachable("Invalid NameKind!");
6797 void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
6798 DeclarationNameLoc &DNLoc,
6799 DeclarationName Name,
6800 const RecordData &Record, unsigned &Idx) {
6801 switch (Name.getNameKind()) {
6802 case DeclarationName::CXXConstructorName:
6803 case DeclarationName::CXXDestructorName:
6804 case DeclarationName::CXXConversionFunctionName:
6805 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
6808 case DeclarationName::CXXOperatorName:
6809 DNLoc.CXXOperatorName.BeginOpNameLoc
6810 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6811 DNLoc.CXXOperatorName.EndOpNameLoc
6812 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6815 case DeclarationName::CXXLiteralOperatorName:
6816 DNLoc.CXXLiteralOperatorName.OpNameLoc
6817 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6820 case DeclarationName::Identifier:
6821 case DeclarationName::ObjCZeroArgSelector:
6822 case DeclarationName::ObjCOneArgSelector:
6823 case DeclarationName::ObjCMultiArgSelector:
6824 case DeclarationName::CXXUsingDirective:
6829 void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
6830 DeclarationNameInfo &NameInfo,
6831 const RecordData &Record, unsigned &Idx) {
6832 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
6833 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6834 DeclarationNameLoc DNLoc;
6835 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6836 NameInfo.setInfo(DNLoc);
6839 void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
6840 const RecordData &Record, unsigned &Idx) {
6841 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
6842 unsigned NumTPLists = Record[Idx++];
6843 Info.NumTemplParamLists = NumTPLists;
6845 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
6846 for (unsigned i=0; i != NumTPLists; ++i)
6847 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6852 ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
6854 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
6856 case TemplateName::Template:
6857 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
6859 case TemplateName::OverloadedTemplate: {
6860 unsigned size = Record[Idx++];
6861 UnresolvedSet<8> Decls;
6863 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
6865 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
6868 case TemplateName::QualifiedTemplate: {
6869 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6870 bool hasTemplKeyword = Record[Idx++];
6871 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
6872 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
6875 case TemplateName::DependentTemplate: {
6876 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6877 if (Record[Idx++]) // isIdentifier
6878 return Context.getDependentTemplateName(NNS,
6879 GetIdentifierInfo(F, Record,
6881 return Context.getDependentTemplateName(NNS,
6882 (OverloadedOperatorKind)Record[Idx++]);
6885 case TemplateName::SubstTemplateTemplateParm: {
6886 TemplateTemplateParmDecl *param
6887 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6888 if (!param) return TemplateName();
6889 TemplateName replacement = ReadTemplateName(F, Record, Idx);
6890 return Context.getSubstTemplateTemplateParm(param, replacement);
6893 case TemplateName::SubstTemplateTemplateParmPack: {
6894 TemplateTemplateParmDecl *Param
6895 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6897 return TemplateName();
6899 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6900 if (ArgPack.getKind() != TemplateArgument::Pack)
6901 return TemplateName();
6903 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
6907 llvm_unreachable("Unhandled template name kind!");
6911 ASTReader::ReadTemplateArgument(ModuleFile &F,
6912 const RecordData &Record, unsigned &Idx) {
6913 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6915 case TemplateArgument::Null:
6916 return TemplateArgument();
6917 case TemplateArgument::Type:
6918 return TemplateArgument(readType(F, Record, Idx));
6919 case TemplateArgument::Declaration: {
6920 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6921 bool ForReferenceParam = Record[Idx++];
6922 return TemplateArgument(D, ForReferenceParam);
6924 case TemplateArgument::NullPtr:
6925 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
6926 case TemplateArgument::Integral: {
6927 llvm::APSInt Value = ReadAPSInt(Record, Idx);
6928 QualType T = readType(F, Record, Idx);
6929 return TemplateArgument(Context, Value, T);
6931 case TemplateArgument::Template:
6932 return TemplateArgument(ReadTemplateName(F, Record, Idx));
6933 case TemplateArgument::TemplateExpansion: {
6934 TemplateName Name = ReadTemplateName(F, Record, Idx);
6935 Optional<unsigned> NumTemplateExpansions;
6936 if (unsigned NumExpansions = Record[Idx++])
6937 NumTemplateExpansions = NumExpansions - 1;
6938 return TemplateArgument(Name, NumTemplateExpansions);
6940 case TemplateArgument::Expression:
6941 return TemplateArgument(ReadExpr(F));
6942 case TemplateArgument::Pack: {
6943 unsigned NumArgs = Record[Idx++];
6944 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
6945 for (unsigned I = 0; I != NumArgs; ++I)
6946 Args[I] = ReadTemplateArgument(F, Record, Idx);
6947 return TemplateArgument(Args, NumArgs);
6951 llvm_unreachable("Unhandled template argument kind!");
6954 TemplateParameterList *
6955 ASTReader::ReadTemplateParameterList(ModuleFile &F,
6956 const RecordData &Record, unsigned &Idx) {
6957 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6958 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6959 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
6961 unsigned NumParams = Record[Idx++];
6962 SmallVector<NamedDecl *, 16> Params;
6963 Params.reserve(NumParams);
6965 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
6967 TemplateParameterList* TemplateParams =
6968 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
6969 Params.data(), Params.size(), RAngleLoc);
6970 return TemplateParams;
6975 ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
6976 ModuleFile &F, const RecordData &Record,
6978 unsigned NumTemplateArgs = Record[Idx++];
6979 TemplArgs.reserve(NumTemplateArgs);
6980 while (NumTemplateArgs--)
6981 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
6984 /// \brief Read a UnresolvedSet structure.
6985 void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
6986 const RecordData &Record, unsigned &Idx) {
6987 unsigned NumDecls = Record[Idx++];
6988 Set.reserve(Context, NumDecls);
6989 while (NumDecls--) {
6990 DeclID ID = ReadDeclID(F, Record, Idx);
6991 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6992 Set.addLazyDecl(Context, ID, AS);
6997 ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
6998 const RecordData &Record, unsigned &Idx) {
6999 bool isVirtual = static_cast<bool>(Record[Idx++]);
7000 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
7001 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
7002 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
7003 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
7004 SourceRange Range = ReadSourceRange(F, Record, Idx);
7005 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
7006 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
7008 Result.setInheritConstructors(inheritConstructors);
7012 std::pair<CXXCtorInitializer **, unsigned>
7013 ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
7015 CXXCtorInitializer **CtorInitializers = 0;
7016 unsigned NumInitializers = Record[Idx++];
7017 if (NumInitializers) {
7019 = new (Context) CXXCtorInitializer*[NumInitializers];
7020 for (unsigned i=0; i != NumInitializers; ++i) {
7021 TypeSourceInfo *TInfo = 0;
7022 bool IsBaseVirtual = false;
7023 FieldDecl *Member = 0;
7024 IndirectFieldDecl *IndirectMember = 0;
7026 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
7028 case CTOR_INITIALIZER_BASE:
7029 TInfo = GetTypeSourceInfo(F, Record, Idx);
7030 IsBaseVirtual = Record[Idx++];
7033 case CTOR_INITIALIZER_DELEGATING:
7034 TInfo = GetTypeSourceInfo(F, Record, Idx);
7037 case CTOR_INITIALIZER_MEMBER:
7038 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
7041 case CTOR_INITIALIZER_INDIRECT_MEMBER:
7042 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
7046 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
7047 Expr *Init = ReadExpr(F);
7048 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
7049 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
7050 bool IsWritten = Record[Idx++];
7051 unsigned SourceOrderOrNumArrayIndices;
7052 SmallVector<VarDecl *, 8> Indices;
7054 SourceOrderOrNumArrayIndices = Record[Idx++];
7056 SourceOrderOrNumArrayIndices = Record[Idx++];
7057 Indices.reserve(SourceOrderOrNumArrayIndices);
7058 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
7059 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
7062 CXXCtorInitializer *BOMInit;
7063 if (Type == CTOR_INITIALIZER_BASE) {
7064 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
7065 LParenLoc, Init, RParenLoc,
7066 MemberOrEllipsisLoc);
7067 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
7068 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
7070 } else if (IsWritten) {
7072 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
7073 LParenLoc, Init, RParenLoc);
7075 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
7076 MemberOrEllipsisLoc, LParenLoc,
7079 if (IndirectMember) {
7080 assert(Indices.empty() && "Indirect field improperly initialized");
7081 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
7082 MemberOrEllipsisLoc, LParenLoc,
7085 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
7086 LParenLoc, Init, RParenLoc,
7087 Indices.data(), Indices.size());
7092 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
7093 CtorInitializers[i] = BOMInit;
7097 return std::make_pair(CtorInitializers, NumInitializers);
7100 NestedNameSpecifier *
7101 ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
7102 const RecordData &Record, unsigned &Idx) {
7103 unsigned N = Record[Idx++];
7104 NestedNameSpecifier *NNS = 0, *Prev = 0;
7105 for (unsigned I = 0; I != N; ++I) {
7106 NestedNameSpecifier::SpecifierKind Kind
7107 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7109 case NestedNameSpecifier::Identifier: {
7110 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7111 NNS = NestedNameSpecifier::Create(Context, Prev, II);
7115 case NestedNameSpecifier::Namespace: {
7116 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7117 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
7121 case NestedNameSpecifier::NamespaceAlias: {
7122 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7123 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
7127 case NestedNameSpecifier::TypeSpec:
7128 case NestedNameSpecifier::TypeSpecWithTemplate: {
7129 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
7133 bool Template = Record[Idx++];
7134 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
7138 case NestedNameSpecifier::Global: {
7139 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
7140 // No associated value, and there can't be a prefix.
7149 NestedNameSpecifierLoc
7150 ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
7152 unsigned N = Record[Idx++];
7153 NestedNameSpecifierLocBuilder Builder;
7154 for (unsigned I = 0; I != N; ++I) {
7155 NestedNameSpecifier::SpecifierKind Kind
7156 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7158 case NestedNameSpecifier::Identifier: {
7159 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7160 SourceRange Range = ReadSourceRange(F, Record, Idx);
7161 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
7165 case NestedNameSpecifier::Namespace: {
7166 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7167 SourceRange Range = ReadSourceRange(F, Record, Idx);
7168 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
7172 case NestedNameSpecifier::NamespaceAlias: {
7173 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7174 SourceRange Range = ReadSourceRange(F, Record, Idx);
7175 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
7179 case NestedNameSpecifier::TypeSpec:
7180 case NestedNameSpecifier::TypeSpecWithTemplate: {
7181 bool Template = Record[Idx++];
7182 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
7184 return NestedNameSpecifierLoc();
7185 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7187 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
7188 Builder.Extend(Context,
7189 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
7190 T->getTypeLoc(), ColonColonLoc);
7194 case NestedNameSpecifier::Global: {
7195 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7196 Builder.MakeGlobal(Context, ColonColonLoc);
7202 return Builder.getWithLocInContext(Context);
7206 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
7208 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
7209 SourceLocation end = ReadSourceLocation(F, Record, Idx);
7210 return SourceRange(beg, end);
7213 /// \brief Read an integral value
7214 llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
7215 unsigned BitWidth = Record[Idx++];
7216 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
7217 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
7222 /// \brief Read a signed integral value
7223 llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
7224 bool isUnsigned = Record[Idx++];
7225 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
7228 /// \brief Read a floating-point value
7229 llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
7230 const llvm::fltSemantics &Sem,
7232 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
7235 // \brief Read a string
7236 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
7237 unsigned Len = Record[Idx++];
7238 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
7243 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
7245 unsigned Major = Record[Idx++];
7246 unsigned Minor = Record[Idx++];
7247 unsigned Subminor = Record[Idx++];
7249 return VersionTuple(Major);
7251 return VersionTuple(Major, Minor - 1);
7252 return VersionTuple(Major, Minor - 1, Subminor - 1);
7255 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
7256 const RecordData &Record,
7258 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
7259 return CXXTemporary::Create(Context, Decl);
7262 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
7263 return Diag(CurrentImportLoc, DiagID);
7266 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
7267 return Diags.Report(Loc, DiagID);
7270 /// \brief Retrieve the identifier table associated with the
7272 IdentifierTable &ASTReader::getIdentifierTable() {
7273 return PP.getIdentifierTable();
7276 /// \brief Record that the given ID maps to the given switch-case
7278 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
7279 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
7280 "Already have a SwitchCase with this ID");
7281 (*CurrSwitchCaseStmts)[ID] = SC;
7284 /// \brief Retrieve the switch-case statement with the given ID.
7285 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
7286 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
7287 return (*CurrSwitchCaseStmts)[ID];
7290 void ASTReader::ClearSwitchCaseIDs() {
7291 CurrSwitchCaseStmts->clear();
7294 void ASTReader::ReadComments() {
7295 std::vector<RawComment *> Comments;
7296 for (SmallVectorImpl<std::pair<BitstreamCursor,
7297 serialization::ModuleFile *> >::iterator
7298 I = CommentsCursors.begin(),
7299 E = CommentsCursors.end();
7301 BitstreamCursor &Cursor = I->first;
7302 serialization::ModuleFile &F = *I->second;
7303 SavedStreamPosition SavedPosition(Cursor);
7307 llvm::BitstreamEntry Entry =
7308 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
7310 switch (Entry.Kind) {
7311 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
7312 case llvm::BitstreamEntry::Error:
7313 Error("malformed block record in AST file");
7315 case llvm::BitstreamEntry::EndBlock:
7317 case llvm::BitstreamEntry::Record:
7318 // The interesting case.
7324 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
7325 case COMMENTS_RAW_COMMENT: {
7327 SourceRange SR = ReadSourceRange(F, Record, Idx);
7328 RawComment::CommentKind Kind =
7329 (RawComment::CommentKind) Record[Idx++];
7330 bool IsTrailingComment = Record[Idx++];
7331 bool IsAlmostTrailingComment = Record[Idx++];
7332 Comments.push_back(new (Context) RawComment(
7333 SR, Kind, IsTrailingComment, IsAlmostTrailingComment,
7334 Context.getLangOpts().CommentOpts.ParseAllComments));
7341 Context.Comments.addCommentsToFront(Comments);
7344 void ASTReader::finishPendingActions() {
7345 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
7346 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
7347 !PendingOdrMergeChecks.empty()) {
7348 // If any identifiers with corresponding top-level declarations have
7349 // been loaded, load those declarations now.
7350 typedef llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >
7352 TopLevelDeclsMap TopLevelDecls;
7354 while (!PendingIdentifierInfos.empty()) {
7356 IdentifierInfo *II = PendingIdentifierInfos.back().first;
7357 SmallVector<uint32_t, 4> DeclIDs = PendingIdentifierInfos.back().second;
7358 PendingIdentifierInfos.pop_back();
7360 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
7363 // Load pending declaration chains.
7364 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
7365 loadPendingDeclChain(PendingDeclChains[I]);
7366 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
7368 PendingDeclChains.clear();
7370 // Make the most recent of the top-level declarations visible.
7371 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
7372 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
7373 IdentifierInfo *II = TLD->first;
7374 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
7375 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
7379 // Load any pending macro definitions.
7380 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
7381 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
7382 SmallVector<PendingMacroInfo, 2> GlobalIDs;
7383 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
7384 // Initialize the macro history from chained-PCHs ahead of module imports.
7385 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
7387 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
7388 if (Info.M->Kind != MK_Module)
7389 resolvePendingMacro(II, Info);
7391 // Handle module imports.
7392 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
7394 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
7395 if (Info.M->Kind == MK_Module)
7396 resolvePendingMacro(II, Info);
7399 PendingMacroIDs.clear();
7401 // Wire up the DeclContexts for Decls that we delayed setting until
7402 // recursive loading is completed.
7403 while (!PendingDeclContextInfos.empty()) {
7404 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
7405 PendingDeclContextInfos.pop_front();
7406 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
7407 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
7408 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
7411 // For each declaration from a merged context, check that the canonical
7412 // definition of that context also contains a declaration of the same
7414 while (!PendingOdrMergeChecks.empty()) {
7415 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
7417 // FIXME: Skip over implicit declarations for now. This matters for things
7418 // like implicitly-declared special member functions. This isn't entirely
7419 // correct; we can end up with multiple unmerged declarations of the same
7421 if (D->isImplicit())
7424 DeclContext *CanonDef = D->getDeclContext();
7425 DeclContext::lookup_result R = CanonDef->lookup(D->getDeclName());
7428 const Decl *DCanon = D->getCanonicalDecl();
7430 llvm::SmallVector<const NamedDecl*, 4> Candidates;
7431 for (DeclContext::lookup_iterator I = R.begin(), E = R.end();
7432 !Found && I != E; ++I) {
7433 for (Decl::redecl_iterator RI = (*I)->redecls_begin(),
7434 RE = (*I)->redecls_end();
7436 if ((*RI)->getLexicalDeclContext() == CanonDef) {
7437 // This declaration is present in the canonical definition. If it's
7438 // in the same redecl chain, it's the one we're looking for.
7439 if ((*RI)->getCanonicalDecl() == DCanon)
7442 Candidates.push_back(cast<NamedDecl>(*RI));
7449 D->setInvalidDecl();
7451 Module *CanonDefModule = cast<Decl>(CanonDef)->getOwningModule();
7452 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
7453 << D << D->getOwningModule()->getFullModuleName()
7454 << CanonDef << !CanonDefModule
7455 << (CanonDefModule ? CanonDefModule->getFullModuleName() : "");
7457 if (Candidates.empty())
7458 Diag(cast<Decl>(CanonDef)->getLocation(),
7459 diag::note_module_odr_violation_no_possible_decls) << D;
7461 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
7462 Diag(Candidates[I]->getLocation(),
7463 diag::note_module_odr_violation_possible_decl)
7470 // If we deserialized any C++ or Objective-C class definitions, any
7471 // Objective-C protocol definitions, or any redeclarable templates, make sure
7472 // that all redeclarations point to the definitions. Note that this can only
7473 // happen now, after the redeclaration chains have been fully wired.
7474 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
7475 DEnd = PendingDefinitions.end();
7477 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
7478 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
7479 // Make sure that the TagType points at the definition.
7480 const_cast<TagType*>(TagT)->decl = TD;
7483 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
7484 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
7485 REnd = RD->redecls_end();
7487 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
7494 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
7495 // Make sure that the ObjCInterfaceType points at the definition.
7496 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
7499 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
7500 REnd = ID->redecls_end();
7507 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
7508 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
7509 REnd = PD->redecls_end();
7516 RedeclarableTemplateDecl *RTD
7517 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
7518 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
7519 REnd = RTD->redecls_end();
7521 R->Common = RTD->Common;
7523 PendingDefinitions.clear();
7525 // Load the bodies of any functions or methods we've encountered. We do
7526 // this now (delayed) so that we can be sure that the declaration chains
7527 // have been fully wired up.
7528 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
7529 PBEnd = PendingBodies.end();
7530 PB != PBEnd; ++PB) {
7531 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
7532 // FIXME: Check for =delete/=default?
7533 // FIXME: Complain about ODR violations here?
7534 if (!getContext().getLangOpts().Modules || !FD->hasBody())
7535 FD->setLazyBody(PB->second);
7539 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
7540 if (!getContext().getLangOpts().Modules || !MD->hasBody())
7541 MD->setLazyBody(PB->second);
7543 PendingBodies.clear();
7546 void ASTReader::FinishedDeserializing() {
7547 assert(NumCurrentElementsDeserializing &&
7548 "FinishedDeserializing not paired with StartedDeserializing");
7549 if (NumCurrentElementsDeserializing == 1) {
7550 // We decrease NumCurrentElementsDeserializing only after pending actions
7551 // are finished, to avoid recursively re-calling finishPendingActions().
7552 finishPendingActions();
7554 --NumCurrentElementsDeserializing;
7556 if (NumCurrentElementsDeserializing == 0 &&
7557 Consumer && !PassingDeclsToConsumer) {
7558 // Guard variable to avoid recursively redoing the process of passing
7559 // decls to consumer.
7560 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
7563 while (!InterestingDecls.empty()) {
7564 // We are not in recursive loading, so it's safe to pass the "interesting"
7565 // decls to the consumer.
7566 Decl *D = InterestingDecls.front();
7567 InterestingDecls.pop_front();
7568 PassInterestingDeclToConsumer(D);
7573 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
7574 D = D->getMostRecentDecl();
7576 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
7577 SemaObj->TUScope->AddDecl(D);
7578 } else if (SemaObj->TUScope) {
7579 // Adding the decl to IdResolver may have failed because it was already in
7580 // (even though it was not added in scope). If it is already in, make sure
7581 // it gets in the scope as well.
7582 if (std::find(SemaObj->IdResolver.begin(Name),
7583 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
7584 SemaObj->TUScope->AddDecl(D);
7588 ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
7589 StringRef isysroot, bool DisableValidation,
7590 bool AllowASTWithCompilerErrors, bool UseGlobalIndex)
7591 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
7592 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
7593 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
7594 Consumer(0), ModuleMgr(PP.getFileManager()),
7595 isysroot(isysroot), DisableValidation(DisableValidation),
7596 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
7597 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
7598 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
7599 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
7600 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
7601 TotalNumMacros(0), NumIdentifierLookups(0), NumIdentifierLookupHits(0),
7602 NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
7603 NumMethodPoolLookups(0), NumMethodPoolHits(0),
7604 NumMethodPoolTableLookups(0), NumMethodPoolTableHits(0),
7605 TotalNumMethodPoolEntries(0),
7606 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
7607 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
7608 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
7609 PassingDeclsToConsumer(false),
7610 NumCXXBaseSpecifiersLoaded(0), ReadingKind(Read_None)
7612 SourceMgr.setExternalSLocEntrySource(this);
7615 ASTReader::~ASTReader() {
7616 for (DeclContextVisibleUpdatesPending::iterator
7617 I = PendingVisibleUpdates.begin(),
7618 E = PendingVisibleUpdates.end();
7620 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
7621 F = I->second.end();