ASTReader.cpp revision a2c3646c35dd09d21b74826240aa916545b1873f
1//===--- ASTReader.cpp - AST File Reader ----------------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines the ASTReader class, which reads AST files. 11// 12//===----------------------------------------------------------------------===// 13 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" 53#include <algorithm> 54#include <cstdio> 55#include <iterator> 56 57using namespace clang; 58using namespace clang::serialization; 59using namespace clang::serialization::reader; 60using llvm::BitstreamCursor; 61 62//===----------------------------------------------------------------------===// 63// PCH validator implementation 64//===----------------------------------------------------------------------===// 65 66ASTReaderListener::~ASTReaderListener() {} 67 68/// \brief Compare the given set of language options against an existing set of 69/// language options. 70/// 71/// \param Diags If non-NULL, diagnostics will be emitted via this engine. 72/// 73/// \returns true if the languagae options mis-match, false otherwise. 74static 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) { \ 79 if (Diags) \ 80 Diags->Report(diag::err_pch_langopt_mismatch) \ 81 << Description << LangOpts.Name << ExistingLangOpts.Name; \ 82 return true; \ 83 } 84 85#define VALUE_LANGOPT(Name, Bits, Default, Description) \ 86 if (ExistingLangOpts.Name != LangOpts.Name) { \ 87 if (Diags) \ 88 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 89 << Description; \ 90 return true; \ 91 } 92 93#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 94 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \ 95 if (Diags) \ 96 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 97 << Description; \ 98 return true; \ 99 } 100 101#define BENIGN_LANGOPT(Name, Bits, Default, Description) 102#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) 103#include "clang/Basic/LangOptions.def" 104 105 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) { 106 if (Diags) 107 Diags->Report(diag::err_pch_langopt_value_mismatch) 108 << "target Objective-C runtime"; 109 return true; 110 } 111 112 if (ExistingLangOpts.CommentOpts.BlockCommandNames != 113 LangOpts.CommentOpts.BlockCommandNames) { 114 if (Diags) 115 Diags->Report(diag::err_pch_langopt_value_mismatch) 116 << "block command names"; 117 return true; 118 } 119 120 return false; 121} 122 123/// \brief Compare the given set of target options against an existing set of 124/// target options. 125/// 126/// \param Diags If non-NULL, diagnostics will be emitted via this engine. 127/// 128/// \returns true if the target options mis-match, false otherwise. 129static 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) { \ 134 if (Diags) \ 135 Diags->Report(diag::err_pch_targetopt_mismatch) \ 136 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \ 137 return true; \ 138 } 139 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 146 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()); 155 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]) { 160 ++ExistingIdx; 161 ++ReadIdx; 162 continue; 163 } 164 165 if (ReadFeatures[ReadIdx] < ExistingFeatures[ExistingIdx]) { 166 if (Diags) 167 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 168 << false << ReadFeatures[ReadIdx]; 169 return true; 170 } 171 172 if (Diags) 173 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 174 << true << ExistingFeatures[ExistingIdx]; 175 return true; 176 } 177 178 if (ExistingIdx < ExistingN) { 179 if (Diags) 180 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 181 << true << ExistingFeatures[ExistingIdx]; 182 return true; 183 } 184 185 if (ReadIdx < ReadN) { 186 if (Diags) 187 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 188 << false << ReadFeatures[ReadIdx]; 189 return true; 190 } 191 192 return false; 193} 194 195bool 196PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts, 197 bool Complain) { 198 const LangOptions &ExistingLangOpts = PP.getLangOpts(); 199 return checkLanguageOptions(LangOpts, ExistingLangOpts, 200 Complain? &Reader.Diags : 0); 201} 202 203bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts, 204 bool Complain) { 205 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts(); 206 return checkTargetOptions(TargetOpts, ExistingTargetOpts, 207 Complain? &Reader.Diags : 0); 208} 209 210namespace { 211 typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> > 212 MacroDefinitionsMap; 213} 214 215/// \brief Collect the macro definitions provided by the given preprocessor 216/// options. 217static void collectMacroDefinitions(const PreprocessorOptions &PPOpts, 218 MacroDefinitionsMap &Macros, 219 SmallVectorImpl<StringRef> *MacroNames = 0){ 220 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) { 221 StringRef Macro = PPOpts.Macros[I].first; 222 bool IsUndef = PPOpts.Macros[I].second; 223 224 std::pair<StringRef, StringRef> MacroPair = Macro.split('='); 225 StringRef MacroName = MacroPair.first; 226 StringRef MacroBody = MacroPair.second; 227 228 // For an #undef'd macro, we only care about the name. 229 if (IsUndef) { 230 if (MacroNames && !Macros.count(MacroName)) 231 MacroNames->push_back(MacroName); 232 233 Macros[MacroName] = std::make_pair("", true); 234 continue; 235 } 236 237 // For a #define'd macro, figure out the actual definition. 238 if (MacroName.size() == Macro.size()) 239 MacroBody = "1"; 240 else { 241 // Note: GCC drops anything following an end-of-line character. 242 StringRef::size_type End = MacroBody.find_first_of("\n\r"); 243 MacroBody = MacroBody.substr(0, End); 244 } 245 246 if (MacroNames && !Macros.count(MacroName)) 247 MacroNames->push_back(MacroName); 248 Macros[MacroName] = std::make_pair(MacroBody, false); 249 } 250} 251 252/// \brief Check the preprocessor options deserialized from the control block 253/// against the preprocessor options in an existing preprocessor. 254/// 255/// \param Diags If non-null, produce diagnostics for any mismatches incurred. 256static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts, 257 const PreprocessorOptions &ExistingPPOpts, 258 DiagnosticsEngine *Diags, 259 FileManager &FileMgr, 260 std::string &SuggestedPredefines) { 261 // Check macro definitions. 262 MacroDefinitionsMap ASTFileMacros; 263 collectMacroDefinitions(PPOpts, ASTFileMacros); 264 MacroDefinitionsMap ExistingMacros; 265 SmallVector<StringRef, 4> ExistingMacroNames; 266 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames); 267 268 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) { 269 // Dig out the macro definition in the existing preprocessor options. 270 StringRef MacroName = ExistingMacroNames[I]; 271 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName]; 272 273 // Check whether we know anything about this macro name or not. 274 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known 275 = ASTFileMacros.find(MacroName); 276 if (Known == ASTFileMacros.end()) { 277 // FIXME: Check whether this identifier was referenced anywhere in the 278 // AST file. If so, we should reject the AST file. Unfortunately, this 279 // information isn't in the control block. What shall we do about it? 280 281 if (Existing.second) { 282 SuggestedPredefines += "#undef "; 283 SuggestedPredefines += MacroName.str(); 284 SuggestedPredefines += '\n'; 285 } else { 286 SuggestedPredefines += "#define "; 287 SuggestedPredefines += MacroName.str(); 288 SuggestedPredefines += ' '; 289 SuggestedPredefines += Existing.first.str(); 290 SuggestedPredefines += '\n'; 291 } 292 continue; 293 } 294 295 // If the macro was defined in one but undef'd in the other, we have a 296 // conflict. 297 if (Existing.second != Known->second.second) { 298 if (Diags) { 299 Diags->Report(diag::err_pch_macro_def_undef) 300 << MacroName << Known->second.second; 301 } 302 return true; 303 } 304 305 // If the macro was #undef'd in both, or if the macro bodies are identical, 306 // it's fine. 307 if (Existing.second || Existing.first == Known->second.first) 308 continue; 309 310 // The macro bodies differ; complain. 311 if (Diags) { 312 Diags->Report(diag::err_pch_macro_def_conflict) 313 << MacroName << Known->second.first << Existing.first; 314 } 315 return true; 316 } 317 318 // Check whether we're using predefines. 319 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) { 320 if (Diags) { 321 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines; 322 } 323 return true; 324 } 325 326 // Compute the #include and #include_macros lines we need. 327 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) { 328 StringRef File = ExistingPPOpts.Includes[I]; 329 if (File == ExistingPPOpts.ImplicitPCHInclude) 330 continue; 331 332 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File) 333 != PPOpts.Includes.end()) 334 continue; 335 336 SuggestedPredefines += "#include \""; 337 SuggestedPredefines += 338 HeaderSearch::NormalizeDashIncludePath(File, FileMgr); 339 SuggestedPredefines += "\"\n"; 340 } 341 342 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) { 343 StringRef File = ExistingPPOpts.MacroIncludes[I]; 344 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(), 345 File) 346 != PPOpts.MacroIncludes.end()) 347 continue; 348 349 SuggestedPredefines += "#__include_macros \""; 350 SuggestedPredefines += 351 HeaderSearch::NormalizeDashIncludePath(File, FileMgr); 352 SuggestedPredefines += "\"\n##\n"; 353 } 354 355 return false; 356} 357 358bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 359 bool Complain, 360 std::string &SuggestedPredefines) { 361 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts(); 362 363 return checkPreprocessorOptions(PPOpts, ExistingPPOpts, 364 Complain? &Reader.Diags : 0, 365 PP.getFileManager(), 366 SuggestedPredefines); 367} 368 369void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI, 370 unsigned ID) { 371 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID); 372 ++NumHeaderInfos; 373} 374 375void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) { 376 PP.setCounterValue(Value); 377} 378 379//===----------------------------------------------------------------------===// 380// AST reader implementation 381//===----------------------------------------------------------------------===// 382 383void 384ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) { 385 DeserializationListener = Listener; 386} 387 388 389 390unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) { 391 return serialization::ComputeHash(Sel); 392} 393 394 395std::pair<unsigned, unsigned> 396ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) { 397 using namespace clang::io; 398 unsigned KeyLen = ReadUnalignedLE16(d); 399 unsigned DataLen = ReadUnalignedLE16(d); 400 return std::make_pair(KeyLen, DataLen); 401} 402 403ASTSelectorLookupTrait::internal_key_type 404ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) { 405 using namespace clang::io; 406 SelectorTable &SelTable = Reader.getContext().Selectors; 407 unsigned N = ReadUnalignedLE16(d); 408 IdentifierInfo *FirstII 409 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)); 410 if (N == 0) 411 return SelTable.getNullarySelector(FirstII); 412 else if (N == 1) 413 return SelTable.getUnarySelector(FirstII); 414 415 SmallVector<IdentifierInfo *, 16> Args; 416 Args.push_back(FirstII); 417 for (unsigned I = 1; I != N; ++I) 418 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d))); 419 420 return SelTable.getSelector(N, Args.data()); 421} 422 423ASTSelectorLookupTrait::data_type 424ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d, 425 unsigned DataLen) { 426 using namespace clang::io; 427 428 data_type Result; 429 430 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d)); 431 unsigned NumInstanceMethodsAndBits = ReadUnalignedLE16(d); 432 unsigned NumFactoryMethodsAndBits = ReadUnalignedLE16(d); 433 Result.InstanceBits = NumInstanceMethodsAndBits & 0x3; 434 Result.FactoryBits = NumFactoryMethodsAndBits & 0x3; 435 unsigned NumInstanceMethods = NumInstanceMethodsAndBits >> 2; 436 unsigned NumFactoryMethods = NumFactoryMethodsAndBits >> 2; 437 438 // Load instance methods 439 for (unsigned I = 0; I != NumInstanceMethods; ++I) { 440 if (ObjCMethodDecl *Method 441 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d))) 442 Result.Instance.push_back(Method); 443 } 444 445 // Load factory methods 446 for (unsigned I = 0; I != NumFactoryMethods; ++I) { 447 if (ObjCMethodDecl *Method 448 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d))) 449 Result.Factory.push_back(Method); 450 } 451 452 return Result; 453} 454 455unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) { 456 return llvm::HashString(a); 457} 458 459std::pair<unsigned, unsigned> 460ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) { 461 using namespace clang::io; 462 unsigned DataLen = ReadUnalignedLE16(d); 463 unsigned KeyLen = ReadUnalignedLE16(d); 464 return std::make_pair(KeyLen, DataLen); 465} 466 467ASTIdentifierLookupTraitBase::internal_key_type 468ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) { 469 assert(n >= 2 && d[n-1] == '\0'); 470 return StringRef((const char*) d, n-1); 471} 472 473/// \brief Whether the given identifier is "interesting". 474static bool isInterestingIdentifier(IdentifierInfo &II) { 475 return II.isPoisoned() || 476 II.isExtensionToken() || 477 II.getObjCOrBuiltinID() || 478 II.hasRevertedTokenIDToIdentifier() || 479 II.hadMacroDefinition() || 480 II.getFETokenInfo<void>(); 481} 482 483IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k, 484 const unsigned char* d, 485 unsigned DataLen) { 486 using namespace clang::io; 487 unsigned RawID = ReadUnalignedLE32(d); 488 bool IsInteresting = RawID & 0x01; 489 490 // Wipe out the "is interesting" bit. 491 RawID = RawID >> 1; 492 493 IdentID ID = Reader.getGlobalIdentifierID(F, RawID); 494 if (!IsInteresting) { 495 // For uninteresting identifiers, just build the IdentifierInfo 496 // and associate it with the persistent ID. 497 IdentifierInfo *II = KnownII; 498 if (!II) { 499 II = &Reader.getIdentifierTable().getOwn(k); 500 KnownII = II; 501 } 502 Reader.SetIdentifierInfo(ID, II); 503 if (!II->isFromAST()) { 504 bool WasInteresting = isInterestingIdentifier(*II); 505 II->setIsFromAST(); 506 if (WasInteresting) 507 II->setChangedSinceDeserialization(); 508 } 509 Reader.markIdentifierUpToDate(II); 510 return II; 511 } 512 513 unsigned ObjCOrBuiltinID = ReadUnalignedLE16(d); 514 unsigned Bits = ReadUnalignedLE16(d); 515 bool CPlusPlusOperatorKeyword = Bits & 0x01; 516 Bits >>= 1; 517 bool HasRevertedTokenIDToIdentifier = Bits & 0x01; 518 Bits >>= 1; 519 bool Poisoned = Bits & 0x01; 520 Bits >>= 1; 521 bool ExtensionToken = Bits & 0x01; 522 Bits >>= 1; 523 bool hasSubmoduleMacros = Bits & 0x01; 524 Bits >>= 1; 525 bool hadMacroDefinition = Bits & 0x01; 526 Bits >>= 1; 527 528 assert(Bits == 0 && "Extra bits in the identifier?"); 529 DataLen -= 8; 530 531 // Build the IdentifierInfo itself and link the identifier ID with 532 // the new IdentifierInfo. 533 IdentifierInfo *II = KnownII; 534 if (!II) { 535 II = &Reader.getIdentifierTable().getOwn(StringRef(k)); 536 KnownII = II; 537 } 538 Reader.markIdentifierUpToDate(II); 539 if (!II->isFromAST()) { 540 bool WasInteresting = isInterestingIdentifier(*II); 541 II->setIsFromAST(); 542 if (WasInteresting) 543 II->setChangedSinceDeserialization(); 544 } 545 546 // Set or check the various bits in the IdentifierInfo structure. 547 // Token IDs are read-only. 548 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier) 549 II->RevertTokenIDToIdentifier(); 550 II->setObjCOrBuiltinID(ObjCOrBuiltinID); 551 assert(II->isExtensionToken() == ExtensionToken && 552 "Incorrect extension token flag"); 553 (void)ExtensionToken; 554 if (Poisoned) 555 II->setIsPoisoned(true); 556 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword && 557 "Incorrect C++ operator keyword flag"); 558 (void)CPlusPlusOperatorKeyword; 559 560 // If this identifier is a macro, deserialize the macro 561 // definition. 562 if (hadMacroDefinition) { 563 uint32_t MacroDirectivesOffset = ReadUnalignedLE32(d); 564 DataLen -= 4; 565 SmallVector<uint32_t, 8> LocalMacroIDs; 566 if (hasSubmoduleMacros) { 567 while (uint32_t LocalMacroID = ReadUnalignedLE32(d)) { 568 DataLen -= 4; 569 LocalMacroIDs.push_back(LocalMacroID); 570 } 571 DataLen -= 4; 572 } 573 574 if (F.Kind == MK_Module) { 575 for (SmallVectorImpl<uint32_t>::iterator 576 I = LocalMacroIDs.begin(), E = LocalMacroIDs.end(); I != E; ++I) { 577 MacroID MacID = Reader.getGlobalMacroID(F, *I); 578 Reader.addPendingMacroFromModule(II, &F, MacID, F.DirectImportLoc); 579 } 580 } else { 581 Reader.addPendingMacroFromPCH(II, &F, MacroDirectivesOffset); 582 } 583 } 584 585 Reader.SetIdentifierInfo(ID, II); 586 587 // Read all of the declarations visible at global scope with this 588 // name. 589 if (DataLen > 0) { 590 SmallVector<uint32_t, 4> DeclIDs; 591 for (; DataLen > 0; DataLen -= 4) 592 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d))); 593 Reader.SetGloballyVisibleDecls(II, DeclIDs); 594 } 595 596 return II; 597} 598 599unsigned 600ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const { 601 llvm::FoldingSetNodeID ID; 602 ID.AddInteger(Key.Kind); 603 604 switch (Key.Kind) { 605 case DeclarationName::Identifier: 606 case DeclarationName::CXXLiteralOperatorName: 607 ID.AddString(((IdentifierInfo*)Key.Data)->getName()); 608 break; 609 case DeclarationName::ObjCZeroArgSelector: 610 case DeclarationName::ObjCOneArgSelector: 611 case DeclarationName::ObjCMultiArgSelector: 612 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data))); 613 break; 614 case DeclarationName::CXXOperatorName: 615 ID.AddInteger((OverloadedOperatorKind)Key.Data); 616 break; 617 case DeclarationName::CXXConstructorName: 618 case DeclarationName::CXXDestructorName: 619 case DeclarationName::CXXConversionFunctionName: 620 case DeclarationName::CXXUsingDirective: 621 break; 622 } 623 624 return ID.ComputeHash(); 625} 626 627ASTDeclContextNameLookupTrait::internal_key_type 628ASTDeclContextNameLookupTrait::GetInternalKey( 629 const external_key_type& Name) const { 630 DeclNameKey Key; 631 Key.Kind = Name.getNameKind(); 632 switch (Name.getNameKind()) { 633 case DeclarationName::Identifier: 634 Key.Data = (uint64_t)Name.getAsIdentifierInfo(); 635 break; 636 case DeclarationName::ObjCZeroArgSelector: 637 case DeclarationName::ObjCOneArgSelector: 638 case DeclarationName::ObjCMultiArgSelector: 639 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr(); 640 break; 641 case DeclarationName::CXXOperatorName: 642 Key.Data = Name.getCXXOverloadedOperator(); 643 break; 644 case DeclarationName::CXXLiteralOperatorName: 645 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier(); 646 break; 647 case DeclarationName::CXXConstructorName: 648 case DeclarationName::CXXDestructorName: 649 case DeclarationName::CXXConversionFunctionName: 650 case DeclarationName::CXXUsingDirective: 651 Key.Data = 0; 652 break; 653 } 654 655 return Key; 656} 657 658std::pair<unsigned, unsigned> 659ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) { 660 using namespace clang::io; 661 unsigned KeyLen = ReadUnalignedLE16(d); 662 unsigned DataLen = ReadUnalignedLE16(d); 663 return std::make_pair(KeyLen, DataLen); 664} 665 666ASTDeclContextNameLookupTrait::internal_key_type 667ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) { 668 using namespace clang::io; 669 670 DeclNameKey Key; 671 Key.Kind = (DeclarationName::NameKind)*d++; 672 switch (Key.Kind) { 673 case DeclarationName::Identifier: 674 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)); 675 break; 676 case DeclarationName::ObjCZeroArgSelector: 677 case DeclarationName::ObjCOneArgSelector: 678 case DeclarationName::ObjCMultiArgSelector: 679 Key.Data = 680 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d)) 681 .getAsOpaquePtr(); 682 break; 683 case DeclarationName::CXXOperatorName: 684 Key.Data = *d++; // OverloadedOperatorKind 685 break; 686 case DeclarationName::CXXLiteralOperatorName: 687 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)); 688 break; 689 case DeclarationName::CXXConstructorName: 690 case DeclarationName::CXXDestructorName: 691 case DeclarationName::CXXConversionFunctionName: 692 case DeclarationName::CXXUsingDirective: 693 Key.Data = 0; 694 break; 695 } 696 697 return Key; 698} 699 700ASTDeclContextNameLookupTrait::data_type 701ASTDeclContextNameLookupTrait::ReadData(internal_key_type, 702 const unsigned char* d, 703 unsigned DataLen) { 704 using namespace clang::io; 705 unsigned NumDecls = ReadUnalignedLE16(d); 706 LE32DeclID *Start = reinterpret_cast<LE32DeclID *>( 707 const_cast<unsigned char *>(d)); 708 return std::make_pair(Start, Start + NumDecls); 709} 710 711bool ASTReader::ReadDeclContextStorage(ModuleFile &M, 712 BitstreamCursor &Cursor, 713 const std::pair<uint64_t, uint64_t> &Offsets, 714 DeclContextInfo &Info) { 715 SavedStreamPosition SavedPosition(Cursor); 716 // First the lexical decls. 717 if (Offsets.first != 0) { 718 Cursor.JumpToBit(Offsets.first); 719 720 RecordData Record; 721 StringRef Blob; 722 unsigned Code = Cursor.ReadCode(); 723 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob); 724 if (RecCode != DECL_CONTEXT_LEXICAL) { 725 Error("Expected lexical block"); 726 return true; 727 } 728 729 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob.data()); 730 Info.NumLexicalDecls = Blob.size() / sizeof(KindDeclIDPair); 731 } 732 733 // Now the lookup table. 734 if (Offsets.second != 0) { 735 Cursor.JumpToBit(Offsets.second); 736 737 RecordData Record; 738 StringRef Blob; 739 unsigned Code = Cursor.ReadCode(); 740 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob); 741 if (RecCode != DECL_CONTEXT_VISIBLE) { 742 Error("Expected visible lookup table block"); 743 return true; 744 } 745 Info.NameLookupTableData 746 = ASTDeclContextNameLookupTable::Create( 747 (const unsigned char *)Blob.data() + Record[0], 748 (const unsigned char *)Blob.data(), 749 ASTDeclContextNameLookupTrait(*this, M)); 750 } 751 752 return false; 753} 754 755void ASTReader::Error(StringRef Msg) { 756 Error(diag::err_fe_pch_malformed, Msg); 757} 758 759void ASTReader::Error(unsigned DiagID, 760 StringRef Arg1, StringRef Arg2) { 761 if (Diags.isDiagnosticInFlight()) 762 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2); 763 else 764 Diag(DiagID) << Arg1 << Arg2; 765} 766 767//===----------------------------------------------------------------------===// 768// Source Manager Deserialization 769//===----------------------------------------------------------------------===// 770 771/// \brief Read the line table in the source manager block. 772/// \returns true if there was an error. 773bool ASTReader::ParseLineTable(ModuleFile &F, 774 SmallVectorImpl<uint64_t> &Record) { 775 unsigned Idx = 0; 776 LineTableInfo &LineTable = SourceMgr.getLineTable(); 777 778 // Parse the file names 779 std::map<int, int> FileIDs; 780 for (int I = 0, N = Record[Idx++]; I != N; ++I) { 781 // Extract the file name 782 unsigned FilenameLen = Record[Idx++]; 783 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen); 784 Idx += FilenameLen; 785 MaybeAddSystemRootToFilename(F, Filename); 786 FileIDs[I] = LineTable.getLineTableFilenameID(Filename); 787 } 788 789 // Parse the line entries 790 std::vector<LineEntry> Entries; 791 while (Idx < Record.size()) { 792 int FID = Record[Idx++]; 793 assert(FID >= 0 && "Serialized line entries for non-local file."); 794 // Remap FileID from 1-based old view. 795 FID += F.SLocEntryBaseID - 1; 796 797 // Extract the line entries 798 unsigned NumEntries = Record[Idx++]; 799 assert(NumEntries && "Numentries is 00000"); 800 Entries.clear(); 801 Entries.reserve(NumEntries); 802 for (unsigned I = 0; I != NumEntries; ++I) { 803 unsigned FileOffset = Record[Idx++]; 804 unsigned LineNo = Record[Idx++]; 805 int FilenameID = FileIDs[Record[Idx++]]; 806 SrcMgr::CharacteristicKind FileKind 807 = (SrcMgr::CharacteristicKind)Record[Idx++]; 808 unsigned IncludeOffset = Record[Idx++]; 809 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, 810 FileKind, IncludeOffset)); 811 } 812 LineTable.AddEntry(FileID::get(FID), Entries); 813 } 814 815 return false; 816} 817 818/// \brief Read a source manager block 819bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) { 820 using namespace SrcMgr; 821 822 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor; 823 824 // Set the source-location entry cursor to the current position in 825 // the stream. This cursor will be used to read the contents of the 826 // source manager block initially, and then lazily read 827 // source-location entries as needed. 828 SLocEntryCursor = F.Stream; 829 830 // The stream itself is going to skip over the source manager block. 831 if (F.Stream.SkipBlock()) { 832 Error("malformed block record in AST file"); 833 return true; 834 } 835 836 // Enter the source manager block. 837 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) { 838 Error("malformed source manager block record in AST file"); 839 return true; 840 } 841 842 RecordData Record; 843 while (true) { 844 llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks(); 845 846 switch (E.Kind) { 847 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 848 case llvm::BitstreamEntry::Error: 849 Error("malformed block record in AST file"); 850 return true; 851 case llvm::BitstreamEntry::EndBlock: 852 return false; 853 case llvm::BitstreamEntry::Record: 854 // The interesting case. 855 break; 856 } 857 858 // Read a record. 859 Record.clear(); 860 StringRef Blob; 861 switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) { 862 default: // Default behavior: ignore. 863 break; 864 865 case SM_SLOC_FILE_ENTRY: 866 case SM_SLOC_BUFFER_ENTRY: 867 case SM_SLOC_EXPANSION_ENTRY: 868 // Once we hit one of the source location entries, we're done. 869 return false; 870 } 871 } 872} 873 874/// \brief If a header file is not found at the path that we expect it to be 875/// and the PCH file was moved from its original location, try to resolve the 876/// file by assuming that header+PCH were moved together and the header is in 877/// the same place relative to the PCH. 878static std::string 879resolveFileRelativeToOriginalDir(const std::string &Filename, 880 const std::string &OriginalDir, 881 const std::string &CurrDir) { 882 assert(OriginalDir != CurrDir && 883 "No point trying to resolve the file if the PCH dir didn't change"); 884 using namespace llvm::sys; 885 SmallString<128> filePath(Filename); 886 fs::make_absolute(filePath); 887 assert(path::is_absolute(OriginalDir)); 888 SmallString<128> currPCHPath(CurrDir); 889 890 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)), 891 fileDirE = path::end(path::parent_path(filePath)); 892 path::const_iterator origDirI = path::begin(OriginalDir), 893 origDirE = path::end(OriginalDir); 894 // Skip the common path components from filePath and OriginalDir. 895 while (fileDirI != fileDirE && origDirI != origDirE && 896 *fileDirI == *origDirI) { 897 ++fileDirI; 898 ++origDirI; 899 } 900 for (; origDirI != origDirE; ++origDirI) 901 path::append(currPCHPath, ".."); 902 path::append(currPCHPath, fileDirI, fileDirE); 903 path::append(currPCHPath, path::filename(Filename)); 904 return currPCHPath.str(); 905} 906 907bool ASTReader::ReadSLocEntry(int ID) { 908 if (ID == 0) 909 return false; 910 911 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 912 Error("source location entry ID out-of-range for AST file"); 913 return true; 914 } 915 916 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second; 917 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]); 918 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor; 919 unsigned BaseOffset = F->SLocEntryBaseOffset; 920 921 ++NumSLocEntriesRead; 922 llvm::BitstreamEntry Entry = SLocEntryCursor.advance(); 923 if (Entry.Kind != llvm::BitstreamEntry::Record) { 924 Error("incorrectly-formatted source location entry in AST file"); 925 return true; 926 } 927 928 RecordData Record; 929 StringRef Blob; 930 switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) { 931 default: 932 Error("incorrectly-formatted source location entry in AST file"); 933 return true; 934 935 case SM_SLOC_FILE_ENTRY: { 936 // We will detect whether a file changed and return 'Failure' for it, but 937 // we will also try to fail gracefully by setting up the SLocEntry. 938 unsigned InputID = Record[4]; 939 InputFile IF = getInputFile(*F, InputID); 940 const FileEntry *File = IF.getFile(); 941 bool OverriddenBuffer = IF.isOverridden(); 942 943 // Note that we only check if a File was returned. If it was out-of-date 944 // we have complained but we will continue creating a FileID to recover 945 // gracefully. 946 if (!File) 947 return true; 948 949 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 950 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) { 951 // This is the module's main file. 952 IncludeLoc = getImportLocation(F); 953 } 954 SrcMgr::CharacteristicKind 955 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 956 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter, 957 ID, BaseOffset + Record[0]); 958 SrcMgr::FileInfo &FileInfo = 959 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile()); 960 FileInfo.NumCreatedFIDs = Record[5]; 961 if (Record[3]) 962 FileInfo.setHasLineDirectives(); 963 964 const DeclID *FirstDecl = F->FileSortedDecls + Record[6]; 965 unsigned NumFileDecls = Record[7]; 966 if (NumFileDecls) { 967 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?"); 968 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl, 969 NumFileDecls)); 970 } 971 972 const SrcMgr::ContentCache *ContentCache 973 = SourceMgr.getOrCreateContentCache(File, 974 /*isSystemFile=*/FileCharacter != SrcMgr::C_User); 975 if (OverriddenBuffer && !ContentCache->BufferOverridden && 976 ContentCache->ContentsEntry == ContentCache->OrigEntry) { 977 unsigned Code = SLocEntryCursor.ReadCode(); 978 Record.clear(); 979 unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob); 980 981 if (RecCode != SM_SLOC_BUFFER_BLOB) { 982 Error("AST record has invalid code"); 983 return true; 984 } 985 986 llvm::MemoryBuffer *Buffer 987 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), File->getName()); 988 SourceMgr.overrideFileContents(File, Buffer); 989 } 990 991 break; 992 } 993 994 case SM_SLOC_BUFFER_ENTRY: { 995 const char *Name = Blob.data(); 996 unsigned Offset = Record[0]; 997 SrcMgr::CharacteristicKind 998 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 999 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1000 if (IncludeLoc.isInvalid() && F->Kind == MK_Module) { 1001 IncludeLoc = getImportLocation(F); 1002 } 1003 unsigned Code = SLocEntryCursor.ReadCode(); 1004 Record.clear(); 1005 unsigned RecCode 1006 = SLocEntryCursor.readRecord(Code, Record, &Blob); 1007 1008 if (RecCode != SM_SLOC_BUFFER_BLOB) { 1009 Error("AST record has invalid code"); 1010 return true; 1011 } 1012 1013 llvm::MemoryBuffer *Buffer 1014 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name); 1015 SourceMgr.createFileIDForMemBuffer(Buffer, FileCharacter, ID, 1016 BaseOffset + Offset, IncludeLoc); 1017 break; 1018 } 1019 1020 case SM_SLOC_EXPANSION_ENTRY: { 1021 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]); 1022 SourceMgr.createExpansionLoc(SpellingLoc, 1023 ReadSourceLocation(*F, Record[2]), 1024 ReadSourceLocation(*F, Record[3]), 1025 Record[4], 1026 ID, 1027 BaseOffset + Record[0]); 1028 break; 1029 } 1030 } 1031 1032 return false; 1033} 1034 1035std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) { 1036 if (ID == 0) 1037 return std::make_pair(SourceLocation(), ""); 1038 1039 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1040 Error("source location entry ID out-of-range for AST file"); 1041 return std::make_pair(SourceLocation(), ""); 1042 } 1043 1044 // Find which module file this entry lands in. 1045 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second; 1046 if (M->Kind != MK_Module) 1047 return std::make_pair(SourceLocation(), ""); 1048 1049 // FIXME: Can we map this down to a particular submodule? That would be 1050 // ideal. 1051 return std::make_pair(M->ImportLoc, llvm::sys::path::stem(M->FileName)); 1052} 1053 1054/// \brief Find the location where the module F is imported. 1055SourceLocation ASTReader::getImportLocation(ModuleFile *F) { 1056 if (F->ImportLoc.isValid()) 1057 return F->ImportLoc; 1058 1059 // Otherwise we have a PCH. It's considered to be "imported" at the first 1060 // location of its includer. 1061 if (F->ImportedBy.empty() || !F->ImportedBy[0]) { 1062 // Main file is the importer. We assume that it is the first entry in the 1063 // entry table. We can't ask the manager, because at the time of PCH loading 1064 // the main file entry doesn't exist yet. 1065 // The very first entry is the invalid instantiation loc, which takes up 1066 // offsets 0 and 1. 1067 return SourceLocation::getFromRawEncoding(2U); 1068 } 1069 //return F->Loaders[0]->FirstLoc; 1070 return F->ImportedBy[0]->FirstLoc; 1071} 1072 1073/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the 1074/// specified cursor. Read the abbreviations that are at the top of the block 1075/// and then leave the cursor pointing into the block. 1076bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) { 1077 if (Cursor.EnterSubBlock(BlockID)) { 1078 Error("malformed block record in AST file"); 1079 return Failure; 1080 } 1081 1082 while (true) { 1083 uint64_t Offset = Cursor.GetCurrentBitNo(); 1084 unsigned Code = Cursor.ReadCode(); 1085 1086 // We expect all abbrevs to be at the start of the block. 1087 if (Code != llvm::bitc::DEFINE_ABBREV) { 1088 Cursor.JumpToBit(Offset); 1089 return false; 1090 } 1091 Cursor.ReadAbbrevRecord(); 1092 } 1093} 1094 1095MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) { 1096 BitstreamCursor &Stream = F.MacroCursor; 1097 1098 // Keep track of where we are in the stream, then jump back there 1099 // after reading this macro. 1100 SavedStreamPosition SavedPosition(Stream); 1101 1102 Stream.JumpToBit(Offset); 1103 RecordData Record; 1104 SmallVector<IdentifierInfo*, 16> MacroArgs; 1105 MacroInfo *Macro = 0; 1106 1107 while (true) { 1108 // Advance to the next record, but if we get to the end of the block, don't 1109 // pop it (removing all the abbreviations from the cursor) since we want to 1110 // be able to reseek within the block and read entries. 1111 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd; 1112 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags); 1113 1114 switch (Entry.Kind) { 1115 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1116 case llvm::BitstreamEntry::Error: 1117 Error("malformed block record in AST file"); 1118 return Macro; 1119 case llvm::BitstreamEntry::EndBlock: 1120 return Macro; 1121 case llvm::BitstreamEntry::Record: 1122 // The interesting case. 1123 break; 1124 } 1125 1126 // Read a record. 1127 Record.clear(); 1128 PreprocessorRecordTypes RecType = 1129 (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record); 1130 switch (RecType) { 1131 case PP_MACRO_DIRECTIVE_HISTORY: 1132 return Macro; 1133 1134 case PP_MACRO_OBJECT_LIKE: 1135 case PP_MACRO_FUNCTION_LIKE: { 1136 // If we already have a macro, that means that we've hit the end 1137 // of the definition of the macro we were looking for. We're 1138 // done. 1139 if (Macro) 1140 return Macro; 1141 1142 unsigned NextIndex = 1; // Skip identifier ID. 1143 SubmoduleID SubModID = getGlobalSubmoduleID(F, Record[NextIndex++]); 1144 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex); 1145 MacroInfo *MI = PP.AllocateDeserializedMacroInfo(Loc, SubModID); 1146 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex)); 1147 MI->setIsUsed(Record[NextIndex++]); 1148 1149 if (RecType == PP_MACRO_FUNCTION_LIKE) { 1150 // Decode function-like macro info. 1151 bool isC99VarArgs = Record[NextIndex++]; 1152 bool isGNUVarArgs = Record[NextIndex++]; 1153 bool hasCommaPasting = Record[NextIndex++]; 1154 MacroArgs.clear(); 1155 unsigned NumArgs = Record[NextIndex++]; 1156 for (unsigned i = 0; i != NumArgs; ++i) 1157 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++])); 1158 1159 // Install function-like macro info. 1160 MI->setIsFunctionLike(); 1161 if (isC99VarArgs) MI->setIsC99Varargs(); 1162 if (isGNUVarArgs) MI->setIsGNUVarargs(); 1163 if (hasCommaPasting) MI->setHasCommaPasting(); 1164 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(), 1165 PP.getPreprocessorAllocator()); 1166 } 1167 1168 // Remember that we saw this macro last so that we add the tokens that 1169 // form its body to it. 1170 Macro = MI; 1171 1172 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() && 1173 Record[NextIndex]) { 1174 // We have a macro definition. Register the association 1175 PreprocessedEntityID 1176 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]); 1177 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 1178 PreprocessingRecord::PPEntityID 1179 PPID = PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true); 1180 MacroDefinition *PPDef = 1181 cast_or_null<MacroDefinition>(PPRec.getPreprocessedEntity(PPID)); 1182 if (PPDef) 1183 PPRec.RegisterMacroDefinition(Macro, PPDef); 1184 } 1185 1186 ++NumMacrosRead; 1187 break; 1188 } 1189 1190 case PP_TOKEN: { 1191 // If we see a TOKEN before a PP_MACRO_*, then the file is 1192 // erroneous, just pretend we didn't see this. 1193 if (Macro == 0) break; 1194 1195 Token Tok; 1196 Tok.startToken(); 1197 Tok.setLocation(ReadSourceLocation(F, Record[0])); 1198 Tok.setLength(Record[1]); 1199 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2])) 1200 Tok.setIdentifierInfo(II); 1201 Tok.setKind((tok::TokenKind)Record[3]); 1202 Tok.setFlag((Token::TokenFlags)Record[4]); 1203 Macro->AddTokenToBody(Tok); 1204 break; 1205 } 1206 } 1207 } 1208} 1209 1210PreprocessedEntityID 1211ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const { 1212 ContinuousRangeMap<uint32_t, int, 2>::const_iterator 1213 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS); 1214 assert(I != M.PreprocessedEntityRemap.end() 1215 && "Invalid index into preprocessed entity index remap"); 1216 1217 return LocalID + I->second; 1218} 1219 1220unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) { 1221 return llvm::hash_combine(ikey.Size, ikey.ModTime); 1222} 1223 1224HeaderFileInfoTrait::internal_key_type 1225HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) { 1226 internal_key_type ikey = { FE->getSize(), FE->getModificationTime(), 1227 FE->getName() }; 1228 return ikey; 1229} 1230 1231bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) { 1232 if (a.Size != b.Size || a.ModTime != b.ModTime) 1233 return false; 1234 1235 if (strcmp(a.Filename, b.Filename) == 0) 1236 return true; 1237 1238 // Determine whether the actual files are equivalent. 1239 FileManager &FileMgr = Reader.getFileManager(); 1240 const FileEntry *FEA = FileMgr.getFile(a.Filename); 1241 const FileEntry *FEB = FileMgr.getFile(b.Filename); 1242 return (FEA && FEA == FEB); 1243} 1244 1245std::pair<unsigned, unsigned> 1246HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) { 1247 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d); 1248 unsigned DataLen = (unsigned) *d++; 1249 return std::make_pair(KeyLen, DataLen); 1250} 1251 1252HeaderFileInfoTrait::internal_key_type 1253HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) { 1254 internal_key_type ikey; 1255 ikey.Size = off_t(clang::io::ReadUnalignedLE64(d)); 1256 ikey.ModTime = time_t(clang::io::ReadUnalignedLE64(d)); 1257 ikey.Filename = (const char *)d; 1258 return ikey; 1259} 1260 1261HeaderFileInfoTrait::data_type 1262HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d, 1263 unsigned DataLen) { 1264 const unsigned char *End = d + DataLen; 1265 using namespace clang::io; 1266 HeaderFileInfo HFI; 1267 unsigned Flags = *d++; 1268 HFI.isImport = (Flags >> 5) & 0x01; 1269 HFI.isPragmaOnce = (Flags >> 4) & 0x01; 1270 HFI.DirInfo = (Flags >> 2) & 0x03; 1271 HFI.Resolved = (Flags >> 1) & 0x01; 1272 HFI.IndexHeaderMapHeader = Flags & 0x01; 1273 HFI.NumIncludes = ReadUnalignedLE16(d); 1274 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M, 1275 ReadUnalignedLE32(d)); 1276 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) { 1277 // The framework offset is 1 greater than the actual offset, 1278 // since 0 is used as an indicator for "no framework name". 1279 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1); 1280 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName); 1281 } 1282 1283 if (d != End) { 1284 uint32_t LocalSMID = ReadUnalignedLE32(d); 1285 if (LocalSMID) { 1286 // This header is part of a module. Associate it with the module to enable 1287 // implicit module import. 1288 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID); 1289 Module *Mod = Reader.getSubmodule(GlobalSMID); 1290 HFI.isModuleHeader = true; 1291 FileManager &FileMgr = Reader.getFileManager(); 1292 ModuleMap &ModMap = 1293 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap(); 1294 ModMap.addHeader(Mod, FileMgr.getFile(key.Filename), /*Excluded=*/false); 1295 } 1296 } 1297 1298 assert(End == d && "Wrong data length in HeaderFileInfo deserialization"); 1299 (void)End; 1300 1301 // This HeaderFileInfo was externally loaded. 1302 HFI.External = true; 1303 return HFI; 1304} 1305 1306void ASTReader::addPendingMacroFromModule(IdentifierInfo *II, 1307 ModuleFile *M, 1308 GlobalMacroID GMacID, 1309 SourceLocation ImportLoc) { 1310 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard"); 1311 PendingMacroIDs[II].push_back(PendingMacroInfo(M, GMacID, ImportLoc)); 1312} 1313 1314void ASTReader::addPendingMacroFromPCH(IdentifierInfo *II, 1315 ModuleFile *M, 1316 uint64_t MacroDirectivesOffset) { 1317 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard"); 1318 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset)); 1319} 1320 1321void ASTReader::ReadDefinedMacros() { 1322 // Note that we are loading defined macros. 1323 Deserializing Macros(this); 1324 1325 for (ModuleReverseIterator I = ModuleMgr.rbegin(), 1326 E = ModuleMgr.rend(); I != E; ++I) { 1327 BitstreamCursor &MacroCursor = (*I)->MacroCursor; 1328 1329 // If there was no preprocessor block, skip this file. 1330 if (!MacroCursor.getBitStreamReader()) 1331 continue; 1332 1333 BitstreamCursor Cursor = MacroCursor; 1334 Cursor.JumpToBit((*I)->MacroStartOffset); 1335 1336 RecordData Record; 1337 while (true) { 1338 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks(); 1339 1340 switch (E.Kind) { 1341 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1342 case llvm::BitstreamEntry::Error: 1343 Error("malformed block record in AST file"); 1344 return; 1345 case llvm::BitstreamEntry::EndBlock: 1346 goto NextCursor; 1347 1348 case llvm::BitstreamEntry::Record: 1349 Record.clear(); 1350 switch (Cursor.readRecord(E.ID, Record)) { 1351 default: // Default behavior: ignore. 1352 break; 1353 1354 case PP_MACRO_OBJECT_LIKE: 1355 case PP_MACRO_FUNCTION_LIKE: 1356 getLocalIdentifier(**I, Record[0]); 1357 break; 1358 1359 case PP_TOKEN: 1360 // Ignore tokens. 1361 break; 1362 } 1363 break; 1364 } 1365 } 1366 NextCursor: ; 1367 } 1368} 1369 1370namespace { 1371 /// \brief Visitor class used to look up identifirs in an AST file. 1372 class IdentifierLookupVisitor { 1373 StringRef Name; 1374 unsigned PriorGeneration; 1375 unsigned &NumIdentifierLookups; 1376 unsigned &NumIdentifierLookupHits; 1377 IdentifierInfo *Found; 1378 1379 public: 1380 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration, 1381 unsigned &NumIdentifierLookups, 1382 unsigned &NumIdentifierLookupHits) 1383 : Name(Name), PriorGeneration(PriorGeneration), 1384 NumIdentifierLookups(NumIdentifierLookups), 1385 NumIdentifierLookupHits(NumIdentifierLookupHits), 1386 Found() 1387 { 1388 } 1389 1390 static bool visit(ModuleFile &M, void *UserData) { 1391 IdentifierLookupVisitor *This 1392 = static_cast<IdentifierLookupVisitor *>(UserData); 1393 1394 // If we've already searched this module file, skip it now. 1395 if (M.Generation <= This->PriorGeneration) 1396 return true; 1397 1398 ASTIdentifierLookupTable *IdTable 1399 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable; 1400 if (!IdTable) 1401 return false; 1402 1403 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), 1404 M, This->Found); 1405 ++This->NumIdentifierLookups; 1406 ASTIdentifierLookupTable::iterator Pos = IdTable->find(This->Name,&Trait); 1407 if (Pos == IdTable->end()) 1408 return false; 1409 1410 // Dereferencing the iterator has the effect of building the 1411 // IdentifierInfo node and populating it with the various 1412 // declarations it needs. 1413 ++This->NumIdentifierLookupHits; 1414 This->Found = *Pos; 1415 return true; 1416 } 1417 1418 // \brief Retrieve the identifier info found within the module 1419 // files. 1420 IdentifierInfo *getIdentifierInfo() const { return Found; } 1421 }; 1422} 1423 1424void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) { 1425 // Note that we are loading an identifier. 1426 Deserializing AnIdentifier(this); 1427 1428 unsigned PriorGeneration = 0; 1429 if (getContext().getLangOpts().Modules) 1430 PriorGeneration = IdentifierGeneration[&II]; 1431 1432 // If there is a global index, look there first to determine which modules 1433 // provably do not have any results for this identifier. 1434 GlobalModuleIndex::HitSet Hits; 1435 GlobalModuleIndex::HitSet *HitsPtr = 0; 1436 if (!loadGlobalIndex()) { 1437 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) { 1438 HitsPtr = &Hits; 1439 } 1440 } 1441 1442 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration, 1443 NumIdentifierLookups, 1444 NumIdentifierLookupHits); 1445 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr); 1446 markIdentifierUpToDate(&II); 1447} 1448 1449void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) { 1450 if (!II) 1451 return; 1452 1453 II->setOutOfDate(false); 1454 1455 // Update the generation for this identifier. 1456 if (getContext().getLangOpts().Modules) 1457 IdentifierGeneration[II] = CurrentGeneration; 1458} 1459 1460void ASTReader::resolvePendingMacro(IdentifierInfo *II, 1461 const PendingMacroInfo &PMInfo) { 1462 assert(II); 1463 1464 if (PMInfo.M->Kind != MK_Module) { 1465 installPCHMacroDirectives(II, *PMInfo.M, 1466 PMInfo.PCHMacroData.MacroDirectivesOffset); 1467 return; 1468 } 1469 1470 // Module Macro. 1471 1472 GlobalMacroID GMacID = PMInfo.ModuleMacroData.GMacID; 1473 SourceLocation ImportLoc = 1474 SourceLocation::getFromRawEncoding(PMInfo.ModuleMacroData.ImportLoc); 1475 1476 assert(GMacID); 1477 // If this macro has already been loaded, don't do so again. 1478 if (MacrosLoaded[GMacID - NUM_PREDEF_MACRO_IDS]) 1479 return; 1480 1481 MacroInfo *MI = getMacro(GMacID); 1482 SubmoduleID SubModID = MI->getOwningModuleID(); 1483 MacroDirective *MD = PP.AllocateDefMacroDirective(MI, ImportLoc, 1484 /*isImported=*/true); 1485 1486 // Determine whether this macro definition is visible. 1487 bool Hidden = false; 1488 Module *Owner = 0; 1489 if (SubModID) { 1490 if ((Owner = getSubmodule(SubModID))) { 1491 if (Owner->NameVisibility == Module::Hidden) { 1492 // The owning module is not visible, and this macro definition 1493 // should not be, either. 1494 Hidden = true; 1495 1496 // Note that this macro definition was hidden because its owning 1497 // module is not yet visible. 1498 HiddenNamesMap[Owner].push_back(HiddenName(II, MD)); 1499 } 1500 } 1501 } 1502 1503 if (!Hidden) 1504 installImportedMacro(II, MD, Owner); 1505} 1506 1507void ASTReader::installPCHMacroDirectives(IdentifierInfo *II, 1508 ModuleFile &M, uint64_t Offset) { 1509 assert(M.Kind != MK_Module); 1510 1511 BitstreamCursor &Cursor = M.MacroCursor; 1512 SavedStreamPosition SavedPosition(Cursor); 1513 Cursor.JumpToBit(Offset); 1514 1515 llvm::BitstreamEntry Entry = 1516 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 1517 if (Entry.Kind != llvm::BitstreamEntry::Record) { 1518 Error("malformed block record in AST file"); 1519 return; 1520 } 1521 1522 RecordData Record; 1523 PreprocessorRecordTypes RecType = 1524 (PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record); 1525 if (RecType != PP_MACRO_DIRECTIVE_HISTORY) { 1526 Error("malformed block record in AST file"); 1527 return; 1528 } 1529 1530 // Deserialize the macro directives history in reverse source-order. 1531 MacroDirective *Latest = 0, *Earliest = 0; 1532 unsigned Idx = 0, N = Record.size(); 1533 while (Idx < N) { 1534 MacroDirective *MD = 0; 1535 SourceLocation Loc = ReadSourceLocation(M, Record, Idx); 1536 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++]; 1537 switch (K) { 1538 case MacroDirective::MD_Define: { 1539 GlobalMacroID GMacID = getGlobalMacroID(M, Record[Idx++]); 1540 MacroInfo *MI = getMacro(GMacID); 1541 bool isImported = Record[Idx++]; 1542 bool isAmbiguous = Record[Idx++]; 1543 DefMacroDirective *DefMD = 1544 PP.AllocateDefMacroDirective(MI, Loc, isImported); 1545 DefMD->setAmbiguous(isAmbiguous); 1546 MD = DefMD; 1547 break; 1548 } 1549 case MacroDirective::MD_Undefine: 1550 MD = PP.AllocateUndefMacroDirective(Loc); 1551 break; 1552 case MacroDirective::MD_Visibility: { 1553 bool isPublic = Record[Idx++]; 1554 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic); 1555 break; 1556 } 1557 } 1558 1559 if (!Latest) 1560 Latest = MD; 1561 if (Earliest) 1562 Earliest->setPrevious(MD); 1563 Earliest = MD; 1564 } 1565 1566 PP.setLoadedMacroDirective(II, Latest); 1567} 1568 1569/// \brief For the given macro definitions, check if they are both in system 1570/// modules. 1571static bool areDefinedInSystemModules(MacroInfo *PrevMI, MacroInfo *NewMI, 1572 Module *NewOwner, ASTReader &Reader) { 1573 assert(PrevMI && NewMI); 1574 if (!NewOwner) 1575 return false; 1576 Module *PrevOwner = 0; 1577 if (SubmoduleID PrevModID = PrevMI->getOwningModuleID()) 1578 PrevOwner = Reader.getSubmodule(PrevModID); 1579 if (!PrevOwner) 1580 return false; 1581 if (PrevOwner == NewOwner) 1582 return false; 1583 return PrevOwner->IsSystem && NewOwner->IsSystem; 1584} 1585 1586void ASTReader::installImportedMacro(IdentifierInfo *II, MacroDirective *MD, 1587 Module *Owner) { 1588 assert(II && MD); 1589 1590 DefMacroDirective *DefMD = cast<DefMacroDirective>(MD); 1591 MacroDirective *Prev = PP.getMacroDirective(II); 1592 if (Prev) { 1593 MacroDirective::DefInfo PrevDef = Prev->getDefinition(); 1594 MacroInfo *PrevMI = PrevDef.getMacroInfo(); 1595 MacroInfo *NewMI = DefMD->getInfo(); 1596 if (NewMI != PrevMI && !PrevMI->isIdenticalTo(*NewMI, PP, 1597 /*Syntactically=*/true)) { 1598 // Before marking the macros as ambiguous, check if this is a case where 1599 // both macros are in system headers. If so, we trust that the system 1600 // did not get it wrong. This also handles cases where Clang's own 1601 // headers have a different spelling of certain system macros: 1602 // #define LONG_MAX __LONG_MAX__ (clang's limits.h) 1603 // #define LONG_MAX 0x7fffffffffffffffL (system's limits.h) 1604 if (!areDefinedInSystemModules(PrevMI, NewMI, Owner, *this)) { 1605 PrevDef.getDirective()->setAmbiguous(true); 1606 DefMD->setAmbiguous(true); 1607 } 1608 } 1609 } 1610 1611 PP.appendMacroDirective(II, MD); 1612} 1613 1614InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) { 1615 // If this ID is bogus, just return an empty input file. 1616 if (ID == 0 || ID > F.InputFilesLoaded.size()) 1617 return InputFile(); 1618 1619 // If we've already loaded this input file, return it. 1620 if (F.InputFilesLoaded[ID-1].getFile()) 1621 return F.InputFilesLoaded[ID-1]; 1622 1623 // Go find this input file. 1624 BitstreamCursor &Cursor = F.InputFilesCursor; 1625 SavedStreamPosition SavedPosition(Cursor); 1626 Cursor.JumpToBit(F.InputFileOffsets[ID-1]); 1627 1628 unsigned Code = Cursor.ReadCode(); 1629 RecordData Record; 1630 StringRef Blob; 1631 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) { 1632 case INPUT_FILE: { 1633 unsigned StoredID = Record[0]; 1634 assert(ID == StoredID && "Bogus stored ID or offset"); 1635 (void)StoredID; 1636 off_t StoredSize = (off_t)Record[1]; 1637 time_t StoredTime = (time_t)Record[2]; 1638 bool Overridden = (bool)Record[3]; 1639 1640 // Get the file entry for this input file. 1641 StringRef OrigFilename = Blob; 1642 std::string Filename = OrigFilename; 1643 MaybeAddSystemRootToFilename(F, Filename); 1644 const FileEntry *File 1645 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime) 1646 : FileMgr.getFile(Filename, /*OpenFile=*/false); 1647 1648 // If we didn't find the file, resolve it relative to the 1649 // original directory from which this AST file was created. 1650 if (File == 0 && !F.OriginalDir.empty() && !CurrentDir.empty() && 1651 F.OriginalDir != CurrentDir) { 1652 std::string Resolved = resolveFileRelativeToOriginalDir(Filename, 1653 F.OriginalDir, 1654 CurrentDir); 1655 if (!Resolved.empty()) 1656 File = FileMgr.getFile(Resolved); 1657 } 1658 1659 // For an overridden file, create a virtual file with the stored 1660 // size/timestamp. 1661 if (Overridden && File == 0) { 1662 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime); 1663 } 1664 1665 if (File == 0) { 1666 if (Complain) { 1667 std::string ErrorStr = "could not find file '"; 1668 ErrorStr += Filename; 1669 ErrorStr += "' referenced by AST file"; 1670 Error(ErrorStr.c_str()); 1671 } 1672 return InputFile(); 1673 } 1674 1675 // Check if there was a request to override the contents of the file 1676 // that was part of the precompiled header. Overridding such a file 1677 // can lead to problems when lexing using the source locations from the 1678 // PCH. 1679 SourceManager &SM = getSourceManager(); 1680 if (!Overridden && SM.isFileOverridden(File)) { 1681 if (Complain) 1682 Error(diag::err_fe_pch_file_overridden, Filename); 1683 // After emitting the diagnostic, recover by disabling the override so 1684 // that the original file will be used. 1685 SM.disableFileContentsOverride(File); 1686 // The FileEntry is a virtual file entry with the size of the contents 1687 // that would override the original contents. Set it to the original's 1688 // size/time. 1689 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File), 1690 StoredSize, StoredTime); 1691 } 1692 1693 bool IsOutOfDate = false; 1694 1695 // For an overridden file, there is nothing to validate. 1696 if (!Overridden && (StoredSize != File->getSize() 1697#if !defined(LLVM_ON_WIN32) 1698 // In our regression testing, the Windows file system seems to 1699 // have inconsistent modification times that sometimes 1700 // erroneously trigger this error-handling path. 1701 || StoredTime != File->getModificationTime() 1702#endif 1703 )) { 1704 if (Complain) { 1705 Error(diag::err_fe_pch_file_modified, Filename, F.FileName); 1706 } 1707 1708 IsOutOfDate = true; 1709 } 1710 1711 InputFile IF = InputFile(File, Overridden, IsOutOfDate); 1712 1713 // Note that we've loaded this input file. 1714 F.InputFilesLoaded[ID-1] = IF; 1715 return IF; 1716 } 1717 } 1718 1719 return InputFile(); 1720} 1721 1722const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) { 1723 ModuleFile &M = ModuleMgr.getPrimaryModule(); 1724 std::string Filename = filenameStrRef; 1725 MaybeAddSystemRootToFilename(M, Filename); 1726 const FileEntry *File = FileMgr.getFile(Filename); 1727 if (File == 0 && !M.OriginalDir.empty() && !CurrentDir.empty() && 1728 M.OriginalDir != CurrentDir) { 1729 std::string resolved = resolveFileRelativeToOriginalDir(Filename, 1730 M.OriginalDir, 1731 CurrentDir); 1732 if (!resolved.empty()) 1733 File = FileMgr.getFile(resolved); 1734 } 1735 1736 return File; 1737} 1738 1739/// \brief If we are loading a relocatable PCH file, and the filename is 1740/// not an absolute path, add the system root to the beginning of the file 1741/// name. 1742void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M, 1743 std::string &Filename) { 1744 // If this is not a relocatable PCH file, there's nothing to do. 1745 if (!M.RelocatablePCH) 1746 return; 1747 1748 if (Filename.empty() || llvm::sys::path::is_absolute(Filename)) 1749 return; 1750 1751 if (isysroot.empty()) { 1752 // If no system root was given, default to '/' 1753 Filename.insert(Filename.begin(), '/'); 1754 return; 1755 } 1756 1757 unsigned Length = isysroot.size(); 1758 if (isysroot[Length - 1] != '/') 1759 Filename.insert(Filename.begin(), '/'); 1760 1761 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end()); 1762} 1763 1764ASTReader::ASTReadResult 1765ASTReader::ReadControlBlock(ModuleFile &F, 1766 SmallVectorImpl<ImportedModule> &Loaded, 1767 unsigned ClientLoadCapabilities) { 1768 BitstreamCursor &Stream = F.Stream; 1769 1770 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) { 1771 Error("malformed block record in AST file"); 1772 return Failure; 1773 } 1774 1775 // Read all of the records and blocks in the control block. 1776 RecordData Record; 1777 while (1) { 1778 llvm::BitstreamEntry Entry = Stream.advance(); 1779 1780 switch (Entry.Kind) { 1781 case llvm::BitstreamEntry::Error: 1782 Error("malformed block record in AST file"); 1783 return Failure; 1784 case llvm::BitstreamEntry::EndBlock: 1785 // Validate all of the non-system input files. 1786 if (!DisableValidation) { 1787 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 1788 // All user input files reside at the index range [0, Record[1]). 1789 // Record is the one from INPUT_FILE_OFFSETS. 1790 for (unsigned I = 0, N = Record[1]; I < N; ++I) { 1791 InputFile IF = getInputFile(F, I+1, Complain); 1792 if (!IF.getFile() || IF.isOutOfDate()) 1793 return OutOfDate; 1794 } 1795 } 1796 return Success; 1797 1798 case llvm::BitstreamEntry::SubBlock: 1799 switch (Entry.ID) { 1800 case INPUT_FILES_BLOCK_ID: 1801 F.InputFilesCursor = Stream; 1802 if (Stream.SkipBlock() || // Skip with the main cursor 1803 // Read the abbreviations 1804 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) { 1805 Error("malformed block record in AST file"); 1806 return Failure; 1807 } 1808 continue; 1809 1810 default: 1811 if (Stream.SkipBlock()) { 1812 Error("malformed block record in AST file"); 1813 return Failure; 1814 } 1815 continue; 1816 } 1817 1818 case llvm::BitstreamEntry::Record: 1819 // The interesting case. 1820 break; 1821 } 1822 1823 // Read and process a record. 1824 Record.clear(); 1825 StringRef Blob; 1826 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) { 1827 case METADATA: { 1828 if (Record[0] != VERSION_MAJOR && !DisableValidation) { 1829 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 1830 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old 1831 : diag::warn_pch_version_too_new); 1832 return VersionMismatch; 1833 } 1834 1835 bool hasErrors = Record[5]; 1836 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) { 1837 Diag(diag::err_pch_with_compiler_errors); 1838 return HadErrors; 1839 } 1840 1841 F.RelocatablePCH = Record[4]; 1842 1843 const std::string &CurBranch = getClangFullRepositoryVersion(); 1844 StringRef ASTBranch = Blob; 1845 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) { 1846 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 1847 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch; 1848 return VersionMismatch; 1849 } 1850 break; 1851 } 1852 1853 case IMPORTS: { 1854 // Load each of the imported PCH files. 1855 unsigned Idx = 0, N = Record.size(); 1856 while (Idx < N) { 1857 // Read information about the AST file. 1858 ModuleKind ImportedKind = (ModuleKind)Record[Idx++]; 1859 // The import location will be the local one for now; we will adjust 1860 // all import locations of module imports after the global source 1861 // location info are setup. 1862 SourceLocation ImportLoc = 1863 SourceLocation::getFromRawEncoding(Record[Idx++]); 1864 off_t StoredSize = (off_t)Record[Idx++]; 1865 time_t StoredModTime = (time_t)Record[Idx++]; 1866 unsigned Length = Record[Idx++]; 1867 SmallString<128> ImportedFile(Record.begin() + Idx, 1868 Record.begin() + Idx + Length); 1869 Idx += Length; 1870 1871 // Load the AST file. 1872 switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded, 1873 StoredSize, StoredModTime, 1874 ClientLoadCapabilities)) { 1875 case Failure: return Failure; 1876 // If we have to ignore the dependency, we'll have to ignore this too. 1877 case Missing: 1878 case OutOfDate: return OutOfDate; 1879 case VersionMismatch: return VersionMismatch; 1880 case ConfigurationMismatch: return ConfigurationMismatch; 1881 case HadErrors: return HadErrors; 1882 case Success: break; 1883 } 1884 } 1885 break; 1886 } 1887 1888 case LANGUAGE_OPTIONS: { 1889 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 1890 if (Listener && &F == *ModuleMgr.begin() && 1891 ParseLanguageOptions(Record, Complain, *Listener) && 1892 !DisableValidation) 1893 return ConfigurationMismatch; 1894 break; 1895 } 1896 1897 case TARGET_OPTIONS: { 1898 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0; 1899 if (Listener && &F == *ModuleMgr.begin() && 1900 ParseTargetOptions(Record, Complain, *Listener) && 1901 !DisableValidation) 1902 return ConfigurationMismatch; 1903 break; 1904 } 1905 1906 case DIAGNOSTIC_OPTIONS: { 1907 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0; 1908 if (Listener && &F == *ModuleMgr.begin() && 1909 ParseDiagnosticOptions(Record, Complain, *Listener) && 1910 !DisableValidation) 1911 return ConfigurationMismatch; 1912 break; 1913 } 1914 1915 case FILE_SYSTEM_OPTIONS: { 1916 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0; 1917 if (Listener && &F == *ModuleMgr.begin() && 1918 ParseFileSystemOptions(Record, Complain, *Listener) && 1919 !DisableValidation) 1920 return ConfigurationMismatch; 1921 break; 1922 } 1923 1924 case HEADER_SEARCH_OPTIONS: { 1925 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0; 1926 if (Listener && &F == *ModuleMgr.begin() && 1927 ParseHeaderSearchOptions(Record, Complain, *Listener) && 1928 !DisableValidation) 1929 return ConfigurationMismatch; 1930 break; 1931 } 1932 1933 case PREPROCESSOR_OPTIONS: { 1934 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0; 1935 if (Listener && &F == *ModuleMgr.begin() && 1936 ParsePreprocessorOptions(Record, Complain, *Listener, 1937 SuggestedPredefines) && 1938 !DisableValidation) 1939 return ConfigurationMismatch; 1940 break; 1941 } 1942 1943 case ORIGINAL_FILE: 1944 F.OriginalSourceFileID = FileID::get(Record[0]); 1945 F.ActualOriginalSourceFileName = Blob; 1946 F.OriginalSourceFileName = F.ActualOriginalSourceFileName; 1947 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName); 1948 break; 1949 1950 case ORIGINAL_FILE_ID: 1951 F.OriginalSourceFileID = FileID::get(Record[0]); 1952 break; 1953 1954 case ORIGINAL_PCH_DIR: 1955 F.OriginalDir = Blob; 1956 break; 1957 1958 case INPUT_FILE_OFFSETS: 1959 F.InputFileOffsets = (const uint32_t *)Blob.data(); 1960 F.InputFilesLoaded.resize(Record[0]); 1961 break; 1962 } 1963 } 1964} 1965 1966bool ASTReader::ReadASTBlock(ModuleFile &F) { 1967 BitstreamCursor &Stream = F.Stream; 1968 1969 if (Stream.EnterSubBlock(AST_BLOCK_ID)) { 1970 Error("malformed block record in AST file"); 1971 return true; 1972 } 1973 1974 // Read all of the records and blocks for the AST file. 1975 RecordData Record; 1976 while (1) { 1977 llvm::BitstreamEntry Entry = Stream.advance(); 1978 1979 switch (Entry.Kind) { 1980 case llvm::BitstreamEntry::Error: 1981 Error("error at end of module block in AST file"); 1982 return true; 1983 case llvm::BitstreamEntry::EndBlock: { 1984 // Outside of C++, we do not store a lookup map for the translation unit. 1985 // Instead, mark it as needing a lookup map to be built if this module 1986 // contains any declarations lexically within it (which it always does!). 1987 // This usually has no cost, since we very rarely need the lookup map for 1988 // the translation unit outside C++. 1989 DeclContext *DC = Context.getTranslationUnitDecl(); 1990 if (DC->hasExternalLexicalStorage() && 1991 !getContext().getLangOpts().CPlusPlus) 1992 DC->setMustBuildLookupTable(); 1993 1994 return false; 1995 } 1996 case llvm::BitstreamEntry::SubBlock: 1997 switch (Entry.ID) { 1998 case DECLTYPES_BLOCK_ID: 1999 // We lazily load the decls block, but we want to set up the 2000 // DeclsCursor cursor to point into it. Clone our current bitcode 2001 // cursor to it, enter the block and read the abbrevs in that block. 2002 // With the main cursor, we just skip over it. 2003 F.DeclsCursor = Stream; 2004 if (Stream.SkipBlock() || // Skip with the main cursor. 2005 // Read the abbrevs. 2006 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) { 2007 Error("malformed block record in AST file"); 2008 return true; 2009 } 2010 break; 2011 2012 case DECL_UPDATES_BLOCK_ID: 2013 if (Stream.SkipBlock()) { 2014 Error("malformed block record in AST file"); 2015 return true; 2016 } 2017 break; 2018 2019 case PREPROCESSOR_BLOCK_ID: 2020 F.MacroCursor = Stream; 2021 if (!PP.getExternalSource()) 2022 PP.setExternalSource(this); 2023 2024 if (Stream.SkipBlock() || 2025 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) { 2026 Error("malformed block record in AST file"); 2027 return true; 2028 } 2029 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo(); 2030 break; 2031 2032 case PREPROCESSOR_DETAIL_BLOCK_ID: 2033 F.PreprocessorDetailCursor = Stream; 2034 if (Stream.SkipBlock() || 2035 ReadBlockAbbrevs(F.PreprocessorDetailCursor, 2036 PREPROCESSOR_DETAIL_BLOCK_ID)) { 2037 Error("malformed preprocessor detail record in AST file"); 2038 return true; 2039 } 2040 F.PreprocessorDetailStartOffset 2041 = F.PreprocessorDetailCursor.GetCurrentBitNo(); 2042 2043 if (!PP.getPreprocessingRecord()) 2044 PP.createPreprocessingRecord(); 2045 if (!PP.getPreprocessingRecord()->getExternalSource()) 2046 PP.getPreprocessingRecord()->SetExternalSource(*this); 2047 break; 2048 2049 case SOURCE_MANAGER_BLOCK_ID: 2050 if (ReadSourceManagerBlock(F)) 2051 return true; 2052 break; 2053 2054 case SUBMODULE_BLOCK_ID: 2055 if (ReadSubmoduleBlock(F)) 2056 return true; 2057 break; 2058 2059 case COMMENTS_BLOCK_ID: { 2060 BitstreamCursor C = Stream; 2061 if (Stream.SkipBlock() || 2062 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) { 2063 Error("malformed comments block in AST file"); 2064 return true; 2065 } 2066 CommentsCursors.push_back(std::make_pair(C, &F)); 2067 break; 2068 } 2069 2070 default: 2071 if (Stream.SkipBlock()) { 2072 Error("malformed block record in AST file"); 2073 return true; 2074 } 2075 break; 2076 } 2077 continue; 2078 2079 case llvm::BitstreamEntry::Record: 2080 // The interesting case. 2081 break; 2082 } 2083 2084 // Read and process a record. 2085 Record.clear(); 2086 StringRef Blob; 2087 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) { 2088 default: // Default behavior: ignore. 2089 break; 2090 2091 case TYPE_OFFSET: { 2092 if (F.LocalNumTypes != 0) { 2093 Error("duplicate TYPE_OFFSET record in AST file"); 2094 return true; 2095 } 2096 F.TypeOffsets = (const uint32_t *)Blob.data(); 2097 F.LocalNumTypes = Record[0]; 2098 unsigned LocalBaseTypeIndex = Record[1]; 2099 F.BaseTypeIndex = getTotalNumTypes(); 2100 2101 if (F.LocalNumTypes > 0) { 2102 // Introduce the global -> local mapping for types within this module. 2103 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F)); 2104 2105 // Introduce the local -> global mapping for types within this module. 2106 F.TypeRemap.insertOrReplace( 2107 std::make_pair(LocalBaseTypeIndex, 2108 F.BaseTypeIndex - LocalBaseTypeIndex)); 2109 2110 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes); 2111 } 2112 break; 2113 } 2114 2115 case DECL_OFFSET: { 2116 if (F.LocalNumDecls != 0) { 2117 Error("duplicate DECL_OFFSET record in AST file"); 2118 return true; 2119 } 2120 F.DeclOffsets = (const DeclOffset *)Blob.data(); 2121 F.LocalNumDecls = Record[0]; 2122 unsigned LocalBaseDeclID = Record[1]; 2123 F.BaseDeclID = getTotalNumDecls(); 2124 2125 if (F.LocalNumDecls > 0) { 2126 // Introduce the global -> local mapping for declarations within this 2127 // module. 2128 GlobalDeclMap.insert( 2129 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F)); 2130 2131 // Introduce the local -> global mapping for declarations within this 2132 // module. 2133 F.DeclRemap.insertOrReplace( 2134 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID)); 2135 2136 // Introduce the global -> local mapping for declarations within this 2137 // module. 2138 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID; 2139 2140 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls); 2141 } 2142 break; 2143 } 2144 2145 case TU_UPDATE_LEXICAL: { 2146 DeclContext *TU = Context.getTranslationUnitDecl(); 2147 DeclContextInfo &Info = F.DeclContextInfos[TU]; 2148 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(Blob.data()); 2149 Info.NumLexicalDecls 2150 = static_cast<unsigned int>(Blob.size() / sizeof(KindDeclIDPair)); 2151 TU->setHasExternalLexicalStorage(true); 2152 break; 2153 } 2154 2155 case UPDATE_VISIBLE: { 2156 unsigned Idx = 0; 2157 serialization::DeclID ID = ReadDeclID(F, Record, Idx); 2158 ASTDeclContextNameLookupTable *Table = 2159 ASTDeclContextNameLookupTable::Create( 2160 (const unsigned char *)Blob.data() + Record[Idx++], 2161 (const unsigned char *)Blob.data(), 2162 ASTDeclContextNameLookupTrait(*this, F)); 2163 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU? 2164 DeclContext *TU = Context.getTranslationUnitDecl(); 2165 F.DeclContextInfos[TU].NameLookupTableData = Table; 2166 TU->setHasExternalVisibleStorage(true); 2167 } else 2168 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F)); 2169 break; 2170 } 2171 2172 case IDENTIFIER_TABLE: 2173 F.IdentifierTableData = Blob.data(); 2174 if (Record[0]) { 2175 F.IdentifierLookupTable 2176 = ASTIdentifierLookupTable::Create( 2177 (const unsigned char *)F.IdentifierTableData + Record[0], 2178 (const unsigned char *)F.IdentifierTableData, 2179 ASTIdentifierLookupTrait(*this, F)); 2180 2181 PP.getIdentifierTable().setExternalIdentifierLookup(this); 2182 } 2183 break; 2184 2185 case IDENTIFIER_OFFSET: { 2186 if (F.LocalNumIdentifiers != 0) { 2187 Error("duplicate IDENTIFIER_OFFSET record in AST file"); 2188 return true; 2189 } 2190 F.IdentifierOffsets = (const uint32_t *)Blob.data(); 2191 F.LocalNumIdentifiers = Record[0]; 2192 unsigned LocalBaseIdentifierID = Record[1]; 2193 F.BaseIdentifierID = getTotalNumIdentifiers(); 2194 2195 if (F.LocalNumIdentifiers > 0) { 2196 // Introduce the global -> local mapping for identifiers within this 2197 // module. 2198 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1, 2199 &F)); 2200 2201 // Introduce the local -> global mapping for identifiers within this 2202 // module. 2203 F.IdentifierRemap.insertOrReplace( 2204 std::make_pair(LocalBaseIdentifierID, 2205 F.BaseIdentifierID - LocalBaseIdentifierID)); 2206 2207 IdentifiersLoaded.resize(IdentifiersLoaded.size() 2208 + F.LocalNumIdentifiers); 2209 } 2210 break; 2211 } 2212 2213 case EXTERNAL_DEFINITIONS: 2214 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2215 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I])); 2216 break; 2217 2218 case SPECIAL_TYPES: 2219 if (SpecialTypes.empty()) { 2220 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2221 SpecialTypes.push_back(getGlobalTypeID(F, Record[I])); 2222 break; 2223 } 2224 2225 if (SpecialTypes.size() != Record.size()) { 2226 Error("invalid special-types record"); 2227 return true; 2228 } 2229 2230 for (unsigned I = 0, N = Record.size(); I != N; ++I) { 2231 serialization::TypeID ID = getGlobalTypeID(F, Record[I]); 2232 if (!SpecialTypes[I]) 2233 SpecialTypes[I] = ID; 2234 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate 2235 // merge step? 2236 } 2237 break; 2238 2239 case STATISTICS: 2240 TotalNumStatements += Record[0]; 2241 TotalNumMacros += Record[1]; 2242 TotalLexicalDeclContexts += Record[2]; 2243 TotalVisibleDeclContexts += Record[3]; 2244 break; 2245 2246 case UNUSED_FILESCOPED_DECLS: 2247 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2248 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I])); 2249 break; 2250 2251 case DELEGATING_CTORS: 2252 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2253 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I])); 2254 break; 2255 2256 case WEAK_UNDECLARED_IDENTIFIERS: 2257 if (Record.size() % 4 != 0) { 2258 Error("invalid weak identifiers record"); 2259 return true; 2260 } 2261 2262 // FIXME: Ignore weak undeclared identifiers from non-original PCH 2263 // files. This isn't the way to do it :) 2264 WeakUndeclaredIdentifiers.clear(); 2265 2266 // Translate the weak, undeclared identifiers into global IDs. 2267 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) { 2268 WeakUndeclaredIdentifiers.push_back( 2269 getGlobalIdentifierID(F, Record[I++])); 2270 WeakUndeclaredIdentifiers.push_back( 2271 getGlobalIdentifierID(F, Record[I++])); 2272 WeakUndeclaredIdentifiers.push_back( 2273 ReadSourceLocation(F, Record, I).getRawEncoding()); 2274 WeakUndeclaredIdentifiers.push_back(Record[I++]); 2275 } 2276 break; 2277 2278 case LOCALLY_SCOPED_EXTERN_C_DECLS: 2279 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2280 LocallyScopedExternCDecls.push_back(getGlobalDeclID(F, Record[I])); 2281 break; 2282 2283 case SELECTOR_OFFSETS: { 2284 F.SelectorOffsets = (const uint32_t *)Blob.data(); 2285 F.LocalNumSelectors = Record[0]; 2286 unsigned LocalBaseSelectorID = Record[1]; 2287 F.BaseSelectorID = getTotalNumSelectors(); 2288 2289 if (F.LocalNumSelectors > 0) { 2290 // Introduce the global -> local mapping for selectors within this 2291 // module. 2292 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F)); 2293 2294 // Introduce the local -> global mapping for selectors within this 2295 // module. 2296 F.SelectorRemap.insertOrReplace( 2297 std::make_pair(LocalBaseSelectorID, 2298 F.BaseSelectorID - LocalBaseSelectorID)); 2299 2300 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors); 2301 } 2302 break; 2303 } 2304 2305 case METHOD_POOL: 2306 F.SelectorLookupTableData = (const unsigned char *)Blob.data(); 2307 if (Record[0]) 2308 F.SelectorLookupTable 2309 = ASTSelectorLookupTable::Create( 2310 F.SelectorLookupTableData + Record[0], 2311 F.SelectorLookupTableData, 2312 ASTSelectorLookupTrait(*this, F)); 2313 TotalNumMethodPoolEntries += Record[1]; 2314 break; 2315 2316 case REFERENCED_SELECTOR_POOL: 2317 if (!Record.empty()) { 2318 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) { 2319 ReferencedSelectorsData.push_back(getGlobalSelectorID(F, 2320 Record[Idx++])); 2321 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx). 2322 getRawEncoding()); 2323 } 2324 } 2325 break; 2326 2327 case PP_COUNTER_VALUE: 2328 if (!Record.empty() && Listener) 2329 Listener->ReadCounter(F, Record[0]); 2330 break; 2331 2332 case FILE_SORTED_DECLS: 2333 F.FileSortedDecls = (const DeclID *)Blob.data(); 2334 F.NumFileSortedDecls = Record[0]; 2335 break; 2336 2337 case SOURCE_LOCATION_OFFSETS: { 2338 F.SLocEntryOffsets = (const uint32_t *)Blob.data(); 2339 F.LocalNumSLocEntries = Record[0]; 2340 unsigned SLocSpaceSize = Record[1]; 2341 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) = 2342 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, 2343 SLocSpaceSize); 2344 // Make our entry in the range map. BaseID is negative and growing, so 2345 // we invert it. Because we invert it, though, we need the other end of 2346 // the range. 2347 unsigned RangeStart = 2348 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1; 2349 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F)); 2350 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset); 2351 2352 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing. 2353 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0); 2354 GlobalSLocOffsetMap.insert( 2355 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset 2356 - SLocSpaceSize,&F)); 2357 2358 // Initialize the remapping table. 2359 // Invalid stays invalid. 2360 F.SLocRemap.insert(std::make_pair(0U, 0)); 2361 // This module. Base was 2 when being compiled. 2362 F.SLocRemap.insert(std::make_pair(2U, 2363 static_cast<int>(F.SLocEntryBaseOffset - 2))); 2364 2365 TotalNumSLocEntries += F.LocalNumSLocEntries; 2366 break; 2367 } 2368 2369 case MODULE_OFFSET_MAP: { 2370 // Additional remapping information. 2371 const unsigned char *Data = (const unsigned char*)Blob.data(); 2372 const unsigned char *DataEnd = Data + Blob.size(); 2373 2374 // Continuous range maps we may be updating in our module. 2375 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap); 2376 ContinuousRangeMap<uint32_t, int, 2>::Builder 2377 IdentifierRemap(F.IdentifierRemap); 2378 ContinuousRangeMap<uint32_t, int, 2>::Builder 2379 MacroRemap(F.MacroRemap); 2380 ContinuousRangeMap<uint32_t, int, 2>::Builder 2381 PreprocessedEntityRemap(F.PreprocessedEntityRemap); 2382 ContinuousRangeMap<uint32_t, int, 2>::Builder 2383 SubmoduleRemap(F.SubmoduleRemap); 2384 ContinuousRangeMap<uint32_t, int, 2>::Builder 2385 SelectorRemap(F.SelectorRemap); 2386 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap); 2387 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap); 2388 2389 while(Data < DataEnd) { 2390 uint16_t Len = io::ReadUnalignedLE16(Data); 2391 StringRef Name = StringRef((const char*)Data, Len); 2392 Data += Len; 2393 ModuleFile *OM = ModuleMgr.lookup(Name); 2394 if (!OM) { 2395 Error("SourceLocation remap refers to unknown module"); 2396 return true; 2397 } 2398 2399 uint32_t SLocOffset = io::ReadUnalignedLE32(Data); 2400 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data); 2401 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data); 2402 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data); 2403 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data); 2404 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data); 2405 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data); 2406 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data); 2407 2408 // Source location offset is mapped to OM->SLocEntryBaseOffset. 2409 SLocRemap.insert(std::make_pair(SLocOffset, 2410 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset))); 2411 IdentifierRemap.insert( 2412 std::make_pair(IdentifierIDOffset, 2413 OM->BaseIdentifierID - IdentifierIDOffset)); 2414 MacroRemap.insert(std::make_pair(MacroIDOffset, 2415 OM->BaseMacroID - MacroIDOffset)); 2416 PreprocessedEntityRemap.insert( 2417 std::make_pair(PreprocessedEntityIDOffset, 2418 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset)); 2419 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset, 2420 OM->BaseSubmoduleID - SubmoduleIDOffset)); 2421 SelectorRemap.insert(std::make_pair(SelectorIDOffset, 2422 OM->BaseSelectorID - SelectorIDOffset)); 2423 DeclRemap.insert(std::make_pair(DeclIDOffset, 2424 OM->BaseDeclID - DeclIDOffset)); 2425 2426 TypeRemap.insert(std::make_pair(TypeIndexOffset, 2427 OM->BaseTypeIndex - TypeIndexOffset)); 2428 2429 // Global -> local mappings. 2430 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset; 2431 } 2432 break; 2433 } 2434 2435 case SOURCE_MANAGER_LINE_TABLE: 2436 if (ParseLineTable(F, Record)) 2437 return true; 2438 break; 2439 2440 case SOURCE_LOCATION_PRELOADS: { 2441 // Need to transform from the local view (1-based IDs) to the global view, 2442 // which is based off F.SLocEntryBaseID. 2443 if (!F.PreloadSLocEntries.empty()) { 2444 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file"); 2445 return true; 2446 } 2447 2448 F.PreloadSLocEntries.swap(Record); 2449 break; 2450 } 2451 2452 case EXT_VECTOR_DECLS: 2453 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2454 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I])); 2455 break; 2456 2457 case VTABLE_USES: 2458 if (Record.size() % 3 != 0) { 2459 Error("Invalid VTABLE_USES record"); 2460 return true; 2461 } 2462 2463 // Later tables overwrite earlier ones. 2464 // FIXME: Modules will have some trouble with this. This is clearly not 2465 // the right way to do this. 2466 VTableUses.clear(); 2467 2468 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) { 2469 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++])); 2470 VTableUses.push_back( 2471 ReadSourceLocation(F, Record, Idx).getRawEncoding()); 2472 VTableUses.push_back(Record[Idx++]); 2473 } 2474 break; 2475 2476 case DYNAMIC_CLASSES: 2477 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2478 DynamicClasses.push_back(getGlobalDeclID(F, Record[I])); 2479 break; 2480 2481 case PENDING_IMPLICIT_INSTANTIATIONS: 2482 if (PendingInstantiations.size() % 2 != 0) { 2483 Error("Invalid existing PendingInstantiations"); 2484 return true; 2485 } 2486 2487 if (Record.size() % 2 != 0) { 2488 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block"); 2489 return true; 2490 } 2491 2492 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 2493 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++])); 2494 PendingInstantiations.push_back( 2495 ReadSourceLocation(F, Record, I).getRawEncoding()); 2496 } 2497 break; 2498 2499 case SEMA_DECL_REFS: 2500 // Later tables overwrite earlier ones. 2501 // FIXME: Modules will have some trouble with this. 2502 SemaDeclRefs.clear(); 2503 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2504 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 2505 break; 2506 2507 case PPD_ENTITIES_OFFSETS: { 2508 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data(); 2509 assert(Blob.size() % sizeof(PPEntityOffset) == 0); 2510 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset); 2511 2512 unsigned LocalBasePreprocessedEntityID = Record[0]; 2513 2514 unsigned StartingID; 2515 if (!PP.getPreprocessingRecord()) 2516 PP.createPreprocessingRecord(); 2517 if (!PP.getPreprocessingRecord()->getExternalSource()) 2518 PP.getPreprocessingRecord()->SetExternalSource(*this); 2519 StartingID 2520 = PP.getPreprocessingRecord() 2521 ->allocateLoadedEntities(F.NumPreprocessedEntities); 2522 F.BasePreprocessedEntityID = StartingID; 2523 2524 if (F.NumPreprocessedEntities > 0) { 2525 // Introduce the global -> local mapping for preprocessed entities in 2526 // this module. 2527 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F)); 2528 2529 // Introduce the local -> global mapping for preprocessed entities in 2530 // this module. 2531 F.PreprocessedEntityRemap.insertOrReplace( 2532 std::make_pair(LocalBasePreprocessedEntityID, 2533 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID)); 2534 } 2535 2536 break; 2537 } 2538 2539 case DECL_UPDATE_OFFSETS: { 2540 if (Record.size() % 2 != 0) { 2541 Error("invalid DECL_UPDATE_OFFSETS block in AST file"); 2542 return true; 2543 } 2544 for (unsigned I = 0, N = Record.size(); I != N; I += 2) 2545 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])] 2546 .push_back(std::make_pair(&F, Record[I+1])); 2547 break; 2548 } 2549 2550 case DECL_REPLACEMENTS: { 2551 if (Record.size() % 3 != 0) { 2552 Error("invalid DECL_REPLACEMENTS block in AST file"); 2553 return true; 2554 } 2555 for (unsigned I = 0, N = Record.size(); I != N; I += 3) 2556 ReplacedDecls[getGlobalDeclID(F, Record[I])] 2557 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]); 2558 break; 2559 } 2560 2561 case OBJC_CATEGORIES_MAP: { 2562 if (F.LocalNumObjCCategoriesInMap != 0) { 2563 Error("duplicate OBJC_CATEGORIES_MAP record in AST file"); 2564 return true; 2565 } 2566 2567 F.LocalNumObjCCategoriesInMap = Record[0]; 2568 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data(); 2569 break; 2570 } 2571 2572 case OBJC_CATEGORIES: 2573 F.ObjCCategories.swap(Record); 2574 break; 2575 2576 case CXX_BASE_SPECIFIER_OFFSETS: { 2577 if (F.LocalNumCXXBaseSpecifiers != 0) { 2578 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file"); 2579 return true; 2580 } 2581 2582 F.LocalNumCXXBaseSpecifiers = Record[0]; 2583 F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data(); 2584 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers; 2585 break; 2586 } 2587 2588 case DIAG_PRAGMA_MAPPINGS: 2589 if (F.PragmaDiagMappings.empty()) 2590 F.PragmaDiagMappings.swap(Record); 2591 else 2592 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(), 2593 Record.begin(), Record.end()); 2594 break; 2595 2596 case CUDA_SPECIAL_DECL_REFS: 2597 // Later tables overwrite earlier ones. 2598 // FIXME: Modules will have trouble with this. 2599 CUDASpecialDeclRefs.clear(); 2600 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2601 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 2602 break; 2603 2604 case HEADER_SEARCH_TABLE: { 2605 F.HeaderFileInfoTableData = Blob.data(); 2606 F.LocalNumHeaderFileInfos = Record[1]; 2607 if (Record[0]) { 2608 F.HeaderFileInfoTable 2609 = HeaderFileInfoLookupTable::Create( 2610 (const unsigned char *)F.HeaderFileInfoTableData + Record[0], 2611 (const unsigned char *)F.HeaderFileInfoTableData, 2612 HeaderFileInfoTrait(*this, F, 2613 &PP.getHeaderSearchInfo(), 2614 Blob.data() + Record[2])); 2615 2616 PP.getHeaderSearchInfo().SetExternalSource(this); 2617 if (!PP.getHeaderSearchInfo().getExternalLookup()) 2618 PP.getHeaderSearchInfo().SetExternalLookup(this); 2619 } 2620 break; 2621 } 2622 2623 case FP_PRAGMA_OPTIONS: 2624 // Later tables overwrite earlier ones. 2625 FPPragmaOptions.swap(Record); 2626 break; 2627 2628 case OPENCL_EXTENSIONS: 2629 // Later tables overwrite earlier ones. 2630 OpenCLExtensions.swap(Record); 2631 break; 2632 2633 case TENTATIVE_DEFINITIONS: 2634 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2635 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I])); 2636 break; 2637 2638 case KNOWN_NAMESPACES: 2639 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2640 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I])); 2641 break; 2642 2643 case UNDEFINED_BUT_USED: 2644 if (UndefinedButUsed.size() % 2 != 0) { 2645 Error("Invalid existing UndefinedButUsed"); 2646 return true; 2647 } 2648 2649 if (Record.size() % 2 != 0) { 2650 Error("invalid undefined-but-used record"); 2651 return true; 2652 } 2653 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 2654 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++])); 2655 UndefinedButUsed.push_back( 2656 ReadSourceLocation(F, Record, I).getRawEncoding()); 2657 } 2658 break; 2659 2660 case IMPORTED_MODULES: { 2661 if (F.Kind != MK_Module) { 2662 // If we aren't loading a module (which has its own exports), make 2663 // all of the imported modules visible. 2664 // FIXME: Deal with macros-only imports. 2665 for (unsigned I = 0, N = Record.size(); I != N; ++I) { 2666 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I])) 2667 ImportedModules.push_back(GlobalID); 2668 } 2669 } 2670 break; 2671 } 2672 2673 case LOCAL_REDECLARATIONS: { 2674 F.RedeclarationChains.swap(Record); 2675 break; 2676 } 2677 2678 case LOCAL_REDECLARATIONS_MAP: { 2679 if (F.LocalNumRedeclarationsInMap != 0) { 2680 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file"); 2681 return true; 2682 } 2683 2684 F.LocalNumRedeclarationsInMap = Record[0]; 2685 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data(); 2686 break; 2687 } 2688 2689 case MERGED_DECLARATIONS: { 2690 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) { 2691 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]); 2692 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID]; 2693 for (unsigned N = Record[Idx++]; N > 0; --N) 2694 Decls.push_back(getGlobalDeclID(F, Record[Idx++])); 2695 } 2696 break; 2697 } 2698 2699 case MACRO_OFFSET: { 2700 if (F.LocalNumMacros != 0) { 2701 Error("duplicate MACRO_OFFSET record in AST file"); 2702 return true; 2703 } 2704 F.MacroOffsets = (const uint32_t *)Blob.data(); 2705 F.LocalNumMacros = Record[0]; 2706 unsigned LocalBaseMacroID = Record[1]; 2707 F.BaseMacroID = getTotalNumMacros(); 2708 2709 if (F.LocalNumMacros > 0) { 2710 // Introduce the global -> local mapping for macros within this module. 2711 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F)); 2712 2713 // Introduce the local -> global mapping for macros within this module. 2714 F.MacroRemap.insertOrReplace( 2715 std::make_pair(LocalBaseMacroID, 2716 F.BaseMacroID - LocalBaseMacroID)); 2717 2718 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros); 2719 } 2720 break; 2721 } 2722 2723 case MACRO_TABLE: { 2724 // FIXME: Not used yet. 2725 break; 2726 } 2727 } 2728 } 2729} 2730 2731/// \brief Move the given method to the back of the global list of methods. 2732static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) { 2733 // Find the entry for this selector in the method pool. 2734 Sema::GlobalMethodPool::iterator Known 2735 = S.MethodPool.find(Method->getSelector()); 2736 if (Known == S.MethodPool.end()) 2737 return; 2738 2739 // Retrieve the appropriate method list. 2740 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first 2741 : Known->second.second; 2742 bool Found = false; 2743 for (ObjCMethodList *List = &Start; List; List = List->getNext()) { 2744 if (!Found) { 2745 if (List->Method == Method) { 2746 Found = true; 2747 } else { 2748 // Keep searching. 2749 continue; 2750 } 2751 } 2752 2753 if (List->getNext()) 2754 List->Method = List->getNext()->Method; 2755 else 2756 List->Method = Method; 2757 } 2758} 2759 2760void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) { 2761 for (unsigned I = 0, N = Names.size(); I != N; ++I) { 2762 switch (Names[I].getKind()) { 2763 case HiddenName::Declaration: { 2764 Decl *D = Names[I].getDecl(); 2765 bool wasHidden = D->Hidden; 2766 D->Hidden = false; 2767 2768 if (wasHidden && SemaObj) { 2769 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) { 2770 moveMethodToBackOfGlobalList(*SemaObj, Method); 2771 } 2772 } 2773 break; 2774 } 2775 case HiddenName::MacroVisibility: { 2776 std::pair<IdentifierInfo *, MacroDirective *> Macro = Names[I].getMacro(); 2777 installImportedMacro(Macro.first, Macro.second, Owner); 2778 break; 2779 } 2780 } 2781 } 2782} 2783 2784void ASTReader::makeModuleVisible(Module *Mod, 2785 Module::NameVisibilityKind NameVisibility, 2786 SourceLocation ImportLoc, 2787 bool Complain) { 2788 llvm::SmallPtrSet<Module *, 4> Visited; 2789 SmallVector<Module *, 4> Stack; 2790 Stack.push_back(Mod); 2791 while (!Stack.empty()) { 2792 Mod = Stack.back(); 2793 Stack.pop_back(); 2794 2795 if (NameVisibility <= Mod->NameVisibility) { 2796 // This module already has this level of visibility (or greater), so 2797 // there is nothing more to do. 2798 continue; 2799 } 2800 2801 if (!Mod->isAvailable()) { 2802 // Modules that aren't available cannot be made visible. 2803 continue; 2804 } 2805 2806 // Update the module's name visibility. 2807 Mod->NameVisibility = NameVisibility; 2808 2809 // If we've already deserialized any names from this module, 2810 // mark them as visible. 2811 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod); 2812 if (Hidden != HiddenNamesMap.end()) { 2813 makeNamesVisible(Hidden->second, Hidden->first); 2814 HiddenNamesMap.erase(Hidden); 2815 } 2816 2817 // Push any non-explicit submodules onto the stack to be marked as 2818 // visible. 2819 for (Module::submodule_iterator Sub = Mod->submodule_begin(), 2820 SubEnd = Mod->submodule_end(); 2821 Sub != SubEnd; ++Sub) { 2822 if (!(*Sub)->IsExplicit && Visited.insert(*Sub)) 2823 Stack.push_back(*Sub); 2824 } 2825 2826 // Push any exported modules onto the stack to be marked as visible. 2827 SmallVector<Module *, 16> Exports; 2828 Mod->getExportedModules(Exports); 2829 for (SmallVectorImpl<Module *>::iterator 2830 I = Exports.begin(), E = Exports.end(); I != E; ++I) { 2831 Module *Exported = *I; 2832 if (Visited.insert(Exported)) 2833 Stack.push_back(Exported); 2834 } 2835 2836 // Detect any conflicts. 2837 if (Complain) { 2838 assert(ImportLoc.isValid() && "Missing import location"); 2839 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) { 2840 if (Mod->Conflicts[I].Other->NameVisibility >= NameVisibility) { 2841 Diag(ImportLoc, diag::warn_module_conflict) 2842 << Mod->getFullModuleName() 2843 << Mod->Conflicts[I].Other->getFullModuleName() 2844 << Mod->Conflicts[I].Message; 2845 // FIXME: Need note where the other module was imported. 2846 } 2847 } 2848 } 2849 } 2850} 2851 2852bool ASTReader::loadGlobalIndex() { 2853 if (GlobalIndex) 2854 return false; 2855 2856 if (TriedLoadingGlobalIndex || !UseGlobalIndex || 2857 !Context.getLangOpts().Modules) 2858 return true; 2859 2860 // Try to load the global index. 2861 TriedLoadingGlobalIndex = true; 2862 StringRef ModuleCachePath 2863 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath(); 2864 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result 2865 = GlobalModuleIndex::readIndex(ModuleCachePath); 2866 if (!Result.first) 2867 return true; 2868 2869 GlobalIndex.reset(Result.first); 2870 ModuleMgr.setGlobalIndex(GlobalIndex.get()); 2871 return false; 2872} 2873 2874bool ASTReader::isGlobalIndexUnavailable() const { 2875 return Context.getLangOpts().Modules && UseGlobalIndex && 2876 !hasGlobalIndex() && TriedLoadingGlobalIndex; 2877} 2878 2879ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName, 2880 ModuleKind Type, 2881 SourceLocation ImportLoc, 2882 unsigned ClientLoadCapabilities) { 2883 // Bump the generation number. 2884 unsigned PreviousGeneration = CurrentGeneration++; 2885 2886 unsigned NumModules = ModuleMgr.size(); 2887 SmallVector<ImportedModule, 4> Loaded; 2888 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc, 2889 /*ImportedBy=*/0, Loaded, 2890 0, 0, 2891 ClientLoadCapabilities)) { 2892 case Failure: 2893 case Missing: 2894 case OutOfDate: 2895 case VersionMismatch: 2896 case ConfigurationMismatch: 2897 case HadErrors: 2898 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(), 2899 Context.getLangOpts().Modules 2900 ? &PP.getHeaderSearchInfo().getModuleMap() 2901 : 0); 2902 2903 // If we find that any modules are unusable, the global index is going 2904 // to be out-of-date. Just remove it. 2905 GlobalIndex.reset(); 2906 ModuleMgr.setGlobalIndex(0); 2907 return ReadResult; 2908 2909 case Success: 2910 break; 2911 } 2912 2913 // Here comes stuff that we only do once the entire chain is loaded. 2914 2915 // Load the AST blocks of all of the modules that we loaded. 2916 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(), 2917 MEnd = Loaded.end(); 2918 M != MEnd; ++M) { 2919 ModuleFile &F = *M->Mod; 2920 2921 // Read the AST block. 2922 if (ReadASTBlock(F)) 2923 return Failure; 2924 2925 // Once read, set the ModuleFile bit base offset and update the size in 2926 // bits of all files we've seen. 2927 F.GlobalBitOffset = TotalModulesSizeInBits; 2928 TotalModulesSizeInBits += F.SizeInBits; 2929 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F)); 2930 2931 // Preload SLocEntries. 2932 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) { 2933 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID; 2934 // Load it through the SourceManager and don't call ReadSLocEntry() 2935 // directly because the entry may have already been loaded in which case 2936 // calling ReadSLocEntry() directly would trigger an assertion in 2937 // SourceManager. 2938 SourceMgr.getLoadedSLocEntryByID(Index); 2939 } 2940 } 2941 2942 // Setup the import locations and notify the module manager that we've 2943 // committed to these module files. 2944 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(), 2945 MEnd = Loaded.end(); 2946 M != MEnd; ++M) { 2947 ModuleFile &F = *M->Mod; 2948 2949 ModuleMgr.moduleFileAccepted(&F); 2950 2951 // Set the import location. 2952 F.DirectImportLoc = ImportLoc; 2953 if (!M->ImportedBy) 2954 F.ImportLoc = M->ImportLoc; 2955 else 2956 F.ImportLoc = ReadSourceLocation(*M->ImportedBy, 2957 M->ImportLoc.getRawEncoding()); 2958 } 2959 2960 // Mark all of the identifiers in the identifier table as being out of date, 2961 // so that various accessors know to check the loaded modules when the 2962 // identifier is used. 2963 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(), 2964 IdEnd = PP.getIdentifierTable().end(); 2965 Id != IdEnd; ++Id) 2966 Id->second->setOutOfDate(true); 2967 2968 // Resolve any unresolved module exports. 2969 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) { 2970 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I]; 2971 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID); 2972 Module *ResolvedMod = getSubmodule(GlobalID); 2973 2974 switch (Unresolved.Kind) { 2975 case UnresolvedModuleRef::Conflict: 2976 if (ResolvedMod) { 2977 Module::Conflict Conflict; 2978 Conflict.Other = ResolvedMod; 2979 Conflict.Message = Unresolved.String.str(); 2980 Unresolved.Mod->Conflicts.push_back(Conflict); 2981 } 2982 continue; 2983 2984 case UnresolvedModuleRef::Import: 2985 if (ResolvedMod) 2986 Unresolved.Mod->Imports.push_back(ResolvedMod); 2987 continue; 2988 2989 case UnresolvedModuleRef::Export: 2990 if (ResolvedMod || Unresolved.IsWildcard) 2991 Unresolved.Mod->Exports.push_back( 2992 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard)); 2993 continue; 2994 } 2995 } 2996 UnresolvedModuleRefs.clear(); 2997 2998 InitializeContext(); 2999 3000 if (DeserializationListener) 3001 DeserializationListener->ReaderInitialized(this); 3002 3003 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule(); 3004 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) { 3005 PrimaryModule.OriginalSourceFileID 3006 = FileID::get(PrimaryModule.SLocEntryBaseID 3007 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1); 3008 3009 // If this AST file is a precompiled preamble, then set the 3010 // preamble file ID of the source manager to the file source file 3011 // from which the preamble was built. 3012 if (Type == MK_Preamble) { 3013 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID); 3014 } else if (Type == MK_MainFile) { 3015 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID); 3016 } 3017 } 3018 3019 // For any Objective-C class definitions we have already loaded, make sure 3020 // that we load any additional categories. 3021 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) { 3022 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(), 3023 ObjCClassesLoaded[I], 3024 PreviousGeneration); 3025 } 3026 3027 return Success; 3028} 3029 3030ASTReader::ASTReadResult 3031ASTReader::ReadASTCore(StringRef FileName, 3032 ModuleKind Type, 3033 SourceLocation ImportLoc, 3034 ModuleFile *ImportedBy, 3035 SmallVectorImpl<ImportedModule> &Loaded, 3036 off_t ExpectedSize, time_t ExpectedModTime, 3037 unsigned ClientLoadCapabilities) { 3038 ModuleFile *M; 3039 std::string ErrorStr; 3040 ModuleManager::AddModuleResult AddResult 3041 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy, 3042 CurrentGeneration, ExpectedSize, ExpectedModTime, 3043 M, ErrorStr); 3044 3045 switch (AddResult) { 3046 case ModuleManager::AlreadyLoaded: 3047 return Success; 3048 3049 case ModuleManager::NewlyLoaded: 3050 // Load module file below. 3051 break; 3052 3053 case ModuleManager::Missing: 3054 // The module file was missing; if the client handle handle, that, return 3055 // it. 3056 if (ClientLoadCapabilities & ARR_Missing) 3057 return Missing; 3058 3059 // Otherwise, return an error. 3060 { 3061 std::string Msg = "Unable to load module \"" + FileName.str() + "\": " 3062 + ErrorStr; 3063 Error(Msg); 3064 } 3065 return Failure; 3066 3067 case ModuleManager::OutOfDate: 3068 // We couldn't load the module file because it is out-of-date. If the 3069 // client can handle out-of-date, return it. 3070 if (ClientLoadCapabilities & ARR_OutOfDate) 3071 return OutOfDate; 3072 3073 // Otherwise, return an error. 3074 { 3075 std::string Msg = "Unable to load module \"" + FileName.str() + "\": " 3076 + ErrorStr; 3077 Error(Msg); 3078 } 3079 return Failure; 3080 } 3081 3082 assert(M && "Missing module file"); 3083 3084 // FIXME: This seems rather a hack. Should CurrentDir be part of the 3085 // module? 3086 if (FileName != "-") { 3087 CurrentDir = llvm::sys::path::parent_path(FileName); 3088 if (CurrentDir.empty()) CurrentDir = "."; 3089 } 3090 3091 ModuleFile &F = *M; 3092 BitstreamCursor &Stream = F.Stream; 3093 Stream.init(F.StreamFile); 3094 F.SizeInBits = F.Buffer->getBufferSize() * 8; 3095 3096 // Sniff for the signature. 3097 if (Stream.Read(8) != 'C' || 3098 Stream.Read(8) != 'P' || 3099 Stream.Read(8) != 'C' || 3100 Stream.Read(8) != 'H') { 3101 Diag(diag::err_not_a_pch_file) << FileName; 3102 return Failure; 3103 } 3104 3105 // This is used for compatibility with older PCH formats. 3106 bool HaveReadControlBlock = false; 3107 3108 while (1) { 3109 llvm::BitstreamEntry Entry = Stream.advance(); 3110 3111 switch (Entry.Kind) { 3112 case llvm::BitstreamEntry::Error: 3113 case llvm::BitstreamEntry::EndBlock: 3114 case llvm::BitstreamEntry::Record: 3115 Error("invalid record at top-level of AST file"); 3116 return Failure; 3117 3118 case llvm::BitstreamEntry::SubBlock: 3119 break; 3120 } 3121 3122 // We only know the control subblock ID. 3123 switch (Entry.ID) { 3124 case llvm::bitc::BLOCKINFO_BLOCK_ID: 3125 if (Stream.ReadBlockInfoBlock()) { 3126 Error("malformed BlockInfoBlock in AST file"); 3127 return Failure; 3128 } 3129 break; 3130 case CONTROL_BLOCK_ID: 3131 HaveReadControlBlock = true; 3132 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) { 3133 case Success: 3134 break; 3135 3136 case Failure: return Failure; 3137 case Missing: return Missing; 3138 case OutOfDate: return OutOfDate; 3139 case VersionMismatch: return VersionMismatch; 3140 case ConfigurationMismatch: return ConfigurationMismatch; 3141 case HadErrors: return HadErrors; 3142 } 3143 break; 3144 case AST_BLOCK_ID: 3145 if (!HaveReadControlBlock) { 3146 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 3147 Diag(diag::warn_pch_version_too_old); 3148 return VersionMismatch; 3149 } 3150 3151 // Record that we've loaded this module. 3152 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc)); 3153 return Success; 3154 3155 default: 3156 if (Stream.SkipBlock()) { 3157 Error("malformed block record in AST file"); 3158 return Failure; 3159 } 3160 break; 3161 } 3162 } 3163 3164 return Success; 3165} 3166 3167void ASTReader::InitializeContext() { 3168 // If there's a listener, notify them that we "read" the translation unit. 3169 if (DeserializationListener) 3170 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, 3171 Context.getTranslationUnitDecl()); 3172 3173 // Make sure we load the declaration update records for the translation unit, 3174 // if there are any. 3175 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID, 3176 Context.getTranslationUnitDecl()); 3177 3178 // FIXME: Find a better way to deal with collisions between these 3179 // built-in types. Right now, we just ignore the problem. 3180 3181 // Load the special types. 3182 if (SpecialTypes.size() >= NumSpecialTypeIDs) { 3183 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) { 3184 if (!Context.CFConstantStringTypeDecl) 3185 Context.setCFConstantStringType(GetType(String)); 3186 } 3187 3188 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) { 3189 QualType FileType = GetType(File); 3190 if (FileType.isNull()) { 3191 Error("FILE type is NULL"); 3192 return; 3193 } 3194 3195 if (!Context.FILEDecl) { 3196 if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) 3197 Context.setFILEDecl(Typedef->getDecl()); 3198 else { 3199 const TagType *Tag = FileType->getAs<TagType>(); 3200 if (!Tag) { 3201 Error("Invalid FILE type in AST file"); 3202 return; 3203 } 3204 Context.setFILEDecl(Tag->getDecl()); 3205 } 3206 } 3207 } 3208 3209 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) { 3210 QualType Jmp_bufType = GetType(Jmp_buf); 3211 if (Jmp_bufType.isNull()) { 3212 Error("jmp_buf type is NULL"); 3213 return; 3214 } 3215 3216 if (!Context.jmp_bufDecl) { 3217 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) 3218 Context.setjmp_bufDecl(Typedef->getDecl()); 3219 else { 3220 const TagType *Tag = Jmp_bufType->getAs<TagType>(); 3221 if (!Tag) { 3222 Error("Invalid jmp_buf type in AST file"); 3223 return; 3224 } 3225 Context.setjmp_bufDecl(Tag->getDecl()); 3226 } 3227 } 3228 } 3229 3230 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) { 3231 QualType Sigjmp_bufType = GetType(Sigjmp_buf); 3232 if (Sigjmp_bufType.isNull()) { 3233 Error("sigjmp_buf type is NULL"); 3234 return; 3235 } 3236 3237 if (!Context.sigjmp_bufDecl) { 3238 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) 3239 Context.setsigjmp_bufDecl(Typedef->getDecl()); 3240 else { 3241 const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); 3242 assert(Tag && "Invalid sigjmp_buf type in AST file"); 3243 Context.setsigjmp_bufDecl(Tag->getDecl()); 3244 } 3245 } 3246 } 3247 3248 if (unsigned ObjCIdRedef 3249 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) { 3250 if (Context.ObjCIdRedefinitionType.isNull()) 3251 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef); 3252 } 3253 3254 if (unsigned ObjCClassRedef 3255 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) { 3256 if (Context.ObjCClassRedefinitionType.isNull()) 3257 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef); 3258 } 3259 3260 if (unsigned ObjCSelRedef 3261 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) { 3262 if (Context.ObjCSelRedefinitionType.isNull()) 3263 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef); 3264 } 3265 3266 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) { 3267 QualType Ucontext_tType = GetType(Ucontext_t); 3268 if (Ucontext_tType.isNull()) { 3269 Error("ucontext_t type is NULL"); 3270 return; 3271 } 3272 3273 if (!Context.ucontext_tDecl) { 3274 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>()) 3275 Context.setucontext_tDecl(Typedef->getDecl()); 3276 else { 3277 const TagType *Tag = Ucontext_tType->getAs<TagType>(); 3278 assert(Tag && "Invalid ucontext_t type in AST file"); 3279 Context.setucontext_tDecl(Tag->getDecl()); 3280 } 3281 } 3282 } 3283 } 3284 3285 ReadPragmaDiagnosticMappings(Context.getDiagnostics()); 3286 3287 // If there were any CUDA special declarations, deserialize them. 3288 if (!CUDASpecialDeclRefs.empty()) { 3289 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!"); 3290 Context.setcudaConfigureCallDecl( 3291 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0]))); 3292 } 3293 3294 // Re-export any modules that were imported by a non-module AST file. 3295 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) { 3296 if (Module *Imported = getSubmodule(ImportedModules[I])) 3297 makeModuleVisible(Imported, Module::AllVisible, 3298 /*ImportLoc=*/SourceLocation(), 3299 /*Complain=*/false); 3300 } 3301 ImportedModules.clear(); 3302} 3303 3304void ASTReader::finalizeForWriting() { 3305 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(), 3306 HiddenEnd = HiddenNamesMap.end(); 3307 Hidden != HiddenEnd; ++Hidden) { 3308 makeNamesVisible(Hidden->second, Hidden->first); 3309 } 3310 HiddenNamesMap.clear(); 3311} 3312 3313/// SkipCursorToControlBlock - Given a cursor at the start of an AST file, scan 3314/// ahead and drop the cursor into the start of the CONTROL_BLOCK, returning 3315/// false on success and true on failure. 3316static bool SkipCursorToControlBlock(BitstreamCursor &Cursor) { 3317 while (1) { 3318 llvm::BitstreamEntry Entry = Cursor.advance(); 3319 switch (Entry.Kind) { 3320 case llvm::BitstreamEntry::Error: 3321 case llvm::BitstreamEntry::EndBlock: 3322 return true; 3323 3324 case llvm::BitstreamEntry::Record: 3325 // Ignore top-level records. 3326 Cursor.skipRecord(Entry.ID); 3327 break; 3328 3329 case llvm::BitstreamEntry::SubBlock: 3330 if (Entry.ID == CONTROL_BLOCK_ID) { 3331 if (Cursor.EnterSubBlock(CONTROL_BLOCK_ID)) 3332 return true; 3333 // Found it! 3334 return false; 3335 } 3336 3337 if (Cursor.SkipBlock()) 3338 return true; 3339 } 3340 } 3341} 3342 3343/// \brief Retrieve the name of the original source file name 3344/// directly from the AST file, without actually loading the AST 3345/// file. 3346std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName, 3347 FileManager &FileMgr, 3348 DiagnosticsEngine &Diags) { 3349 // Open the AST file. 3350 std::string ErrStr; 3351 OwningPtr<llvm::MemoryBuffer> Buffer; 3352 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr)); 3353 if (!Buffer) { 3354 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr; 3355 return std::string(); 3356 } 3357 3358 // Initialize the stream 3359 llvm::BitstreamReader StreamFile; 3360 BitstreamCursor Stream; 3361 StreamFile.init((const unsigned char *)Buffer->getBufferStart(), 3362 (const unsigned char *)Buffer->getBufferEnd()); 3363 Stream.init(StreamFile); 3364 3365 // Sniff for the signature. 3366 if (Stream.Read(8) != 'C' || 3367 Stream.Read(8) != 'P' || 3368 Stream.Read(8) != 'C' || 3369 Stream.Read(8) != 'H') { 3370 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName; 3371 return std::string(); 3372 } 3373 3374 // Scan for the CONTROL_BLOCK_ID block. 3375 if (SkipCursorToControlBlock(Stream)) { 3376 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 3377 return std::string(); 3378 } 3379 3380 // Scan for ORIGINAL_FILE inside the control block. 3381 RecordData Record; 3382 while (1) { 3383 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 3384 if (Entry.Kind == llvm::BitstreamEntry::EndBlock) 3385 return std::string(); 3386 3387 if (Entry.Kind != llvm::BitstreamEntry::Record) { 3388 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 3389 return std::string(); 3390 } 3391 3392 Record.clear(); 3393 StringRef Blob; 3394 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE) 3395 return Blob.str(); 3396 } 3397} 3398 3399namespace { 3400 class SimplePCHValidator : public ASTReaderListener { 3401 const LangOptions &ExistingLangOpts; 3402 const TargetOptions &ExistingTargetOpts; 3403 const PreprocessorOptions &ExistingPPOpts; 3404 FileManager &FileMgr; 3405 3406 public: 3407 SimplePCHValidator(const LangOptions &ExistingLangOpts, 3408 const TargetOptions &ExistingTargetOpts, 3409 const PreprocessorOptions &ExistingPPOpts, 3410 FileManager &FileMgr) 3411 : ExistingLangOpts(ExistingLangOpts), 3412 ExistingTargetOpts(ExistingTargetOpts), 3413 ExistingPPOpts(ExistingPPOpts), 3414 FileMgr(FileMgr) 3415 { 3416 } 3417 3418 virtual bool ReadLanguageOptions(const LangOptions &LangOpts, 3419 bool Complain) { 3420 return checkLanguageOptions(ExistingLangOpts, LangOpts, 0); 3421 } 3422 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts, 3423 bool Complain) { 3424 return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0); 3425 } 3426 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 3427 bool Complain, 3428 std::string &SuggestedPredefines) { 3429 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, 0, FileMgr, 3430 SuggestedPredefines); 3431 } 3432 }; 3433} 3434 3435bool ASTReader::readASTFileControlBlock(StringRef Filename, 3436 FileManager &FileMgr, 3437 ASTReaderListener &Listener) { 3438 // Open the AST file. 3439 std::string ErrStr; 3440 OwningPtr<llvm::MemoryBuffer> Buffer; 3441 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr)); 3442 if (!Buffer) { 3443 return true; 3444 } 3445 3446 // Initialize the stream 3447 llvm::BitstreamReader StreamFile; 3448 BitstreamCursor Stream; 3449 StreamFile.init((const unsigned char *)Buffer->getBufferStart(), 3450 (const unsigned char *)Buffer->getBufferEnd()); 3451 Stream.init(StreamFile); 3452 3453 // Sniff for the signature. 3454 if (Stream.Read(8) != 'C' || 3455 Stream.Read(8) != 'P' || 3456 Stream.Read(8) != 'C' || 3457 Stream.Read(8) != 'H') { 3458 return true; 3459 } 3460 3461 // Scan for the CONTROL_BLOCK_ID block. 3462 if (SkipCursorToControlBlock(Stream)) 3463 return true; 3464 3465 // Scan for ORIGINAL_FILE inside the control block. 3466 RecordData Record; 3467 while (1) { 3468 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 3469 if (Entry.Kind == llvm::BitstreamEntry::EndBlock) 3470 return false; 3471 3472 if (Entry.Kind != llvm::BitstreamEntry::Record) 3473 return true; 3474 3475 Record.clear(); 3476 StringRef Blob; 3477 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob); 3478 switch ((ControlRecordTypes)RecCode) { 3479 case METADATA: { 3480 if (Record[0] != VERSION_MAJOR) 3481 return true; 3482 3483 if (Listener.ReadFullVersionInformation(Blob)) 3484 return true; 3485 3486 break; 3487 } 3488 case LANGUAGE_OPTIONS: 3489 if (ParseLanguageOptions(Record, false, Listener)) 3490 return true; 3491 break; 3492 3493 case TARGET_OPTIONS: 3494 if (ParseTargetOptions(Record, false, Listener)) 3495 return true; 3496 break; 3497 3498 case DIAGNOSTIC_OPTIONS: 3499 if (ParseDiagnosticOptions(Record, false, Listener)) 3500 return true; 3501 break; 3502 3503 case FILE_SYSTEM_OPTIONS: 3504 if (ParseFileSystemOptions(Record, false, Listener)) 3505 return true; 3506 break; 3507 3508 case HEADER_SEARCH_OPTIONS: 3509 if (ParseHeaderSearchOptions(Record, false, Listener)) 3510 return true; 3511 break; 3512 3513 case PREPROCESSOR_OPTIONS: { 3514 std::string IgnoredSuggestedPredefines; 3515 if (ParsePreprocessorOptions(Record, false, Listener, 3516 IgnoredSuggestedPredefines)) 3517 return true; 3518 break; 3519 } 3520 3521 default: 3522 // No other validation to perform. 3523 break; 3524 } 3525 } 3526} 3527 3528 3529bool ASTReader::isAcceptableASTFile(StringRef Filename, 3530 FileManager &FileMgr, 3531 const LangOptions &LangOpts, 3532 const TargetOptions &TargetOpts, 3533 const PreprocessorOptions &PPOpts) { 3534 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr); 3535 return !readASTFileControlBlock(Filename, FileMgr, validator); 3536} 3537 3538bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) { 3539 // Enter the submodule block. 3540 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) { 3541 Error("malformed submodule block record in AST file"); 3542 return true; 3543 } 3544 3545 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap(); 3546 bool First = true; 3547 Module *CurrentModule = 0; 3548 RecordData Record; 3549 while (true) { 3550 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks(); 3551 3552 switch (Entry.Kind) { 3553 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 3554 case llvm::BitstreamEntry::Error: 3555 Error("malformed block record in AST file"); 3556 return true; 3557 case llvm::BitstreamEntry::EndBlock: 3558 return false; 3559 case llvm::BitstreamEntry::Record: 3560 // The interesting case. 3561 break; 3562 } 3563 3564 // Read a record. 3565 StringRef Blob; 3566 Record.clear(); 3567 switch (F.Stream.readRecord(Entry.ID, Record, &Blob)) { 3568 default: // Default behavior: ignore. 3569 break; 3570 3571 case SUBMODULE_DEFINITION: { 3572 if (First) { 3573 Error("missing submodule metadata record at beginning of block"); 3574 return true; 3575 } 3576 3577 if (Record.size() < 8) { 3578 Error("malformed module definition"); 3579 return true; 3580 } 3581 3582 StringRef Name = Blob; 3583 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]); 3584 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]); 3585 bool IsFramework = Record[2]; 3586 bool IsExplicit = Record[3]; 3587 bool IsSystem = Record[4]; 3588 bool InferSubmodules = Record[5]; 3589 bool InferExplicitSubmodules = Record[6]; 3590 bool InferExportWildcard = Record[7]; 3591 bool ConfigMacrosExhaustive = Record[8]; 3592 3593 Module *ParentModule = 0; 3594 if (Parent) 3595 ParentModule = getSubmodule(Parent); 3596 3597 // Retrieve this (sub)module from the module map, creating it if 3598 // necessary. 3599 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule, 3600 IsFramework, 3601 IsExplicit).first; 3602 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS; 3603 if (GlobalIndex >= SubmodulesLoaded.size() || 3604 SubmodulesLoaded[GlobalIndex]) { 3605 Error("too many submodules"); 3606 return true; 3607 } 3608 3609 if (!ParentModule) { 3610 if (const FileEntry *CurFile = CurrentModule->getASTFile()) { 3611 if (CurFile != F.File) { 3612 if (!Diags.isDiagnosticInFlight()) { 3613 Diag(diag::err_module_file_conflict) 3614 << CurrentModule->getTopLevelModuleName() 3615 << CurFile->getName() 3616 << F.File->getName(); 3617 } 3618 return true; 3619 } 3620 } 3621 3622 CurrentModule->setASTFile(F.File); 3623 } 3624 3625 CurrentModule->IsFromModuleFile = true; 3626 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem; 3627 CurrentModule->InferSubmodules = InferSubmodules; 3628 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules; 3629 CurrentModule->InferExportWildcard = InferExportWildcard; 3630 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive; 3631 if (DeserializationListener) 3632 DeserializationListener->ModuleRead(GlobalID, CurrentModule); 3633 3634 SubmodulesLoaded[GlobalIndex] = CurrentModule; 3635 3636 // Clear out data that will be replaced by what is the module file. 3637 CurrentModule->LinkLibraries.clear(); 3638 CurrentModule->ConfigMacros.clear(); 3639 CurrentModule->UnresolvedConflicts.clear(); 3640 CurrentModule->Conflicts.clear(); 3641 break; 3642 } 3643 3644 case SUBMODULE_UMBRELLA_HEADER: { 3645 if (First) { 3646 Error("missing submodule metadata record at beginning of block"); 3647 return true; 3648 } 3649 3650 if (!CurrentModule) 3651 break; 3652 3653 if (const FileEntry *Umbrella = PP.getFileManager().getFile(Blob)) { 3654 if (!CurrentModule->getUmbrellaHeader()) 3655 ModMap.setUmbrellaHeader(CurrentModule, Umbrella); 3656 else if (CurrentModule->getUmbrellaHeader() != Umbrella) { 3657 Error("mismatched umbrella headers in submodule"); 3658 return true; 3659 } 3660 } 3661 break; 3662 } 3663 3664 case SUBMODULE_HEADER: { 3665 if (First) { 3666 Error("missing submodule metadata record at beginning of block"); 3667 return true; 3668 } 3669 3670 if (!CurrentModule) 3671 break; 3672 3673 // We lazily associate headers with their modules via the HeaderInfoTable. 3674 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead 3675 // of complete filenames or remove it entirely. 3676 break; 3677 } 3678 3679 case SUBMODULE_EXCLUDED_HEADER: { 3680 if (First) { 3681 Error("missing submodule metadata record at beginning of block"); 3682 return true; 3683 } 3684 3685 if (!CurrentModule) 3686 break; 3687 3688 // We lazily associate headers with their modules via the HeaderInfoTable. 3689 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead 3690 // of complete filenames or remove it entirely. 3691 break; 3692 } 3693 3694 case SUBMODULE_TOPHEADER: { 3695 if (First) { 3696 Error("missing submodule metadata record at beginning of block"); 3697 return true; 3698 } 3699 3700 if (!CurrentModule) 3701 break; 3702 3703 CurrentModule->addTopHeaderFilename(Blob); 3704 break; 3705 } 3706 3707 case SUBMODULE_UMBRELLA_DIR: { 3708 if (First) { 3709 Error("missing submodule metadata record at beginning of block"); 3710 return true; 3711 } 3712 3713 if (!CurrentModule) 3714 break; 3715 3716 if (const DirectoryEntry *Umbrella 3717 = PP.getFileManager().getDirectory(Blob)) { 3718 if (!CurrentModule->getUmbrellaDir()) 3719 ModMap.setUmbrellaDir(CurrentModule, Umbrella); 3720 else if (CurrentModule->getUmbrellaDir() != Umbrella) { 3721 Error("mismatched umbrella directories in submodule"); 3722 return true; 3723 } 3724 } 3725 break; 3726 } 3727 3728 case SUBMODULE_METADATA: { 3729 if (!First) { 3730 Error("submodule metadata record not at beginning of block"); 3731 return true; 3732 } 3733 First = false; 3734 3735 F.BaseSubmoduleID = getTotalNumSubmodules(); 3736 F.LocalNumSubmodules = Record[0]; 3737 unsigned LocalBaseSubmoduleID = Record[1]; 3738 if (F.LocalNumSubmodules > 0) { 3739 // Introduce the global -> local mapping for submodules within this 3740 // module. 3741 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F)); 3742 3743 // Introduce the local -> global mapping for submodules within this 3744 // module. 3745 F.SubmoduleRemap.insertOrReplace( 3746 std::make_pair(LocalBaseSubmoduleID, 3747 F.BaseSubmoduleID - LocalBaseSubmoduleID)); 3748 3749 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules); 3750 } 3751 break; 3752 } 3753 3754 case SUBMODULE_IMPORTS: { 3755 if (First) { 3756 Error("missing submodule metadata record at beginning of block"); 3757 return true; 3758 } 3759 3760 if (!CurrentModule) 3761 break; 3762 3763 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) { 3764 UnresolvedModuleRef Unresolved; 3765 Unresolved.File = &F; 3766 Unresolved.Mod = CurrentModule; 3767 Unresolved.ID = Record[Idx]; 3768 Unresolved.Kind = UnresolvedModuleRef::Import; 3769 Unresolved.IsWildcard = false; 3770 UnresolvedModuleRefs.push_back(Unresolved); 3771 } 3772 break; 3773 } 3774 3775 case SUBMODULE_EXPORTS: { 3776 if (First) { 3777 Error("missing submodule metadata record at beginning of block"); 3778 return true; 3779 } 3780 3781 if (!CurrentModule) 3782 break; 3783 3784 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) { 3785 UnresolvedModuleRef Unresolved; 3786 Unresolved.File = &F; 3787 Unresolved.Mod = CurrentModule; 3788 Unresolved.ID = Record[Idx]; 3789 Unresolved.Kind = UnresolvedModuleRef::Export; 3790 Unresolved.IsWildcard = Record[Idx + 1]; 3791 UnresolvedModuleRefs.push_back(Unresolved); 3792 } 3793 3794 // Once we've loaded the set of exports, there's no reason to keep 3795 // the parsed, unresolved exports around. 3796 CurrentModule->UnresolvedExports.clear(); 3797 break; 3798 } 3799 case SUBMODULE_REQUIRES: { 3800 if (First) { 3801 Error("missing submodule metadata record at beginning of block"); 3802 return true; 3803 } 3804 3805 if (!CurrentModule) 3806 break; 3807 3808 CurrentModule->addRequirement(Blob, Context.getLangOpts(), 3809 Context.getTargetInfo()); 3810 break; 3811 } 3812 3813 case SUBMODULE_LINK_LIBRARY: 3814 if (First) { 3815 Error("missing submodule metadata record at beginning of block"); 3816 return true; 3817 } 3818 3819 if (!CurrentModule) 3820 break; 3821 3822 CurrentModule->LinkLibraries.push_back( 3823 Module::LinkLibrary(Blob, Record[0])); 3824 break; 3825 3826 case SUBMODULE_CONFIG_MACRO: 3827 if (First) { 3828 Error("missing submodule metadata record at beginning of block"); 3829 return true; 3830 } 3831 3832 if (!CurrentModule) 3833 break; 3834 3835 CurrentModule->ConfigMacros.push_back(Blob.str()); 3836 break; 3837 3838 case SUBMODULE_CONFLICT: { 3839 if (First) { 3840 Error("missing submodule metadata record at beginning of block"); 3841 return true; 3842 } 3843 3844 if (!CurrentModule) 3845 break; 3846 3847 UnresolvedModuleRef Unresolved; 3848 Unresolved.File = &F; 3849 Unresolved.Mod = CurrentModule; 3850 Unresolved.ID = Record[0]; 3851 Unresolved.Kind = UnresolvedModuleRef::Conflict; 3852 Unresolved.IsWildcard = false; 3853 Unresolved.String = Blob; 3854 UnresolvedModuleRefs.push_back(Unresolved); 3855 break; 3856 } 3857 } 3858 } 3859} 3860 3861/// \brief Parse the record that corresponds to a LangOptions data 3862/// structure. 3863/// 3864/// This routine parses the language options from the AST file and then gives 3865/// them to the AST listener if one is set. 3866/// 3867/// \returns true if the listener deems the file unacceptable, false otherwise. 3868bool ASTReader::ParseLanguageOptions(const RecordData &Record, 3869 bool Complain, 3870 ASTReaderListener &Listener) { 3871 LangOptions LangOpts; 3872 unsigned Idx = 0; 3873#define LANGOPT(Name, Bits, Default, Description) \ 3874 LangOpts.Name = Record[Idx++]; 3875#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 3876 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++])); 3877#include "clang/Basic/LangOptions.def" 3878#define SANITIZER(NAME, ID) LangOpts.Sanitize.ID = Record[Idx++]; 3879#include "clang/Basic/Sanitizers.def" 3880 3881 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++]; 3882 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx); 3883 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion); 3884 3885 unsigned Length = Record[Idx++]; 3886 LangOpts.CurrentModule.assign(Record.begin() + Idx, 3887 Record.begin() + Idx + Length); 3888 3889 Idx += Length; 3890 3891 // Comment options. 3892 for (unsigned N = Record[Idx++]; N; --N) { 3893 LangOpts.CommentOpts.BlockCommandNames.push_back( 3894 ReadString(Record, Idx)); 3895 } 3896 LangOpts.CommentOpts.ParseAllComments = Record[Idx++]; 3897 3898 return Listener.ReadLanguageOptions(LangOpts, Complain); 3899} 3900 3901bool ASTReader::ParseTargetOptions(const RecordData &Record, 3902 bool Complain, 3903 ASTReaderListener &Listener) { 3904 unsigned Idx = 0; 3905 TargetOptions TargetOpts; 3906 TargetOpts.Triple = ReadString(Record, Idx); 3907 TargetOpts.CPU = ReadString(Record, Idx); 3908 TargetOpts.ABI = ReadString(Record, Idx); 3909 TargetOpts.CXXABI = ReadString(Record, Idx); 3910 TargetOpts.LinkerVersion = ReadString(Record, Idx); 3911 for (unsigned N = Record[Idx++]; N; --N) { 3912 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx)); 3913 } 3914 for (unsigned N = Record[Idx++]; N; --N) { 3915 TargetOpts.Features.push_back(ReadString(Record, Idx)); 3916 } 3917 3918 return Listener.ReadTargetOptions(TargetOpts, Complain); 3919} 3920 3921bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain, 3922 ASTReaderListener &Listener) { 3923 DiagnosticOptions DiagOpts; 3924 unsigned Idx = 0; 3925#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++]; 3926#define ENUM_DIAGOPT(Name, Type, Bits, Default) \ 3927 DiagOpts.set##Name(static_cast<Type>(Record[Idx++])); 3928#include "clang/Basic/DiagnosticOptions.def" 3929 3930 for (unsigned N = Record[Idx++]; N; --N) { 3931 DiagOpts.Warnings.push_back(ReadString(Record, Idx)); 3932 } 3933 3934 return Listener.ReadDiagnosticOptions(DiagOpts, Complain); 3935} 3936 3937bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain, 3938 ASTReaderListener &Listener) { 3939 FileSystemOptions FSOpts; 3940 unsigned Idx = 0; 3941 FSOpts.WorkingDir = ReadString(Record, Idx); 3942 return Listener.ReadFileSystemOptions(FSOpts, Complain); 3943} 3944 3945bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record, 3946 bool Complain, 3947 ASTReaderListener &Listener) { 3948 HeaderSearchOptions HSOpts; 3949 unsigned Idx = 0; 3950 HSOpts.Sysroot = ReadString(Record, Idx); 3951 3952 // Include entries. 3953 for (unsigned N = Record[Idx++]; N; --N) { 3954 std::string Path = ReadString(Record, Idx); 3955 frontend::IncludeDirGroup Group 3956 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]); 3957 bool IsFramework = Record[Idx++]; 3958 bool IgnoreSysRoot = Record[Idx++]; 3959 HSOpts.UserEntries.push_back( 3960 HeaderSearchOptions::Entry(Path, Group, IsFramework, IgnoreSysRoot)); 3961 } 3962 3963 // System header prefixes. 3964 for (unsigned N = Record[Idx++]; N; --N) { 3965 std::string Prefix = ReadString(Record, Idx); 3966 bool IsSystemHeader = Record[Idx++]; 3967 HSOpts.SystemHeaderPrefixes.push_back( 3968 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader)); 3969 } 3970 3971 HSOpts.ResourceDir = ReadString(Record, Idx); 3972 HSOpts.ModuleCachePath = ReadString(Record, Idx); 3973 HSOpts.DisableModuleHash = Record[Idx++]; 3974 HSOpts.UseBuiltinIncludes = Record[Idx++]; 3975 HSOpts.UseStandardSystemIncludes = Record[Idx++]; 3976 HSOpts.UseStandardCXXIncludes = Record[Idx++]; 3977 HSOpts.UseLibcxx = Record[Idx++]; 3978 3979 return Listener.ReadHeaderSearchOptions(HSOpts, Complain); 3980} 3981 3982bool ASTReader::ParsePreprocessorOptions(const RecordData &Record, 3983 bool Complain, 3984 ASTReaderListener &Listener, 3985 std::string &SuggestedPredefines) { 3986 PreprocessorOptions PPOpts; 3987 unsigned Idx = 0; 3988 3989 // Macro definitions/undefs 3990 for (unsigned N = Record[Idx++]; N; --N) { 3991 std::string Macro = ReadString(Record, Idx); 3992 bool IsUndef = Record[Idx++]; 3993 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef)); 3994 } 3995 3996 // Includes 3997 for (unsigned N = Record[Idx++]; N; --N) { 3998 PPOpts.Includes.push_back(ReadString(Record, Idx)); 3999 } 4000 4001 // Macro Includes 4002 for (unsigned N = Record[Idx++]; N; --N) { 4003 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx)); 4004 } 4005 4006 PPOpts.UsePredefines = Record[Idx++]; 4007 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx); 4008 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx); 4009 PPOpts.ObjCXXARCStandardLibrary = 4010 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]); 4011 SuggestedPredefines.clear(); 4012 return Listener.ReadPreprocessorOptions(PPOpts, Complain, 4013 SuggestedPredefines); 4014} 4015 4016std::pair<ModuleFile *, unsigned> 4017ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) { 4018 GlobalPreprocessedEntityMapType::iterator 4019 I = GlobalPreprocessedEntityMap.find(GlobalIndex); 4020 assert(I != GlobalPreprocessedEntityMap.end() && 4021 "Corrupted global preprocessed entity map"); 4022 ModuleFile *M = I->second; 4023 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID; 4024 return std::make_pair(M, LocalIndex); 4025} 4026 4027std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator> 4028ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const { 4029 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord()) 4030 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID, 4031 Mod.NumPreprocessedEntities); 4032 4033 return std::make_pair(PreprocessingRecord::iterator(), 4034 PreprocessingRecord::iterator()); 4035} 4036 4037std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator> 4038ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) { 4039 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls), 4040 ModuleDeclIterator(this, &Mod, 4041 Mod.FileSortedDecls + Mod.NumFileSortedDecls)); 4042} 4043 4044PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) { 4045 PreprocessedEntityID PPID = Index+1; 4046 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 4047 ModuleFile &M = *PPInfo.first; 4048 unsigned LocalIndex = PPInfo.second; 4049 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 4050 4051 if (!PP.getPreprocessingRecord()) { 4052 Error("no preprocessing record"); 4053 return 0; 4054 } 4055 4056 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor); 4057 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset); 4058 4059 llvm::BitstreamEntry Entry = 4060 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 4061 if (Entry.Kind != llvm::BitstreamEntry::Record) 4062 return 0; 4063 4064 // Read the record. 4065 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin), 4066 ReadSourceLocation(M, PPOffs.End)); 4067 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 4068 StringRef Blob; 4069 RecordData Record; 4070 PreprocessorDetailRecordTypes RecType = 4071 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord( 4072 Entry.ID, Record, &Blob); 4073 switch (RecType) { 4074 case PPD_MACRO_EXPANSION: { 4075 bool isBuiltin = Record[0]; 4076 IdentifierInfo *Name = 0; 4077 MacroDefinition *Def = 0; 4078 if (isBuiltin) 4079 Name = getLocalIdentifier(M, Record[1]); 4080 else { 4081 PreprocessedEntityID 4082 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]); 4083 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1)); 4084 } 4085 4086 MacroExpansion *ME; 4087 if (isBuiltin) 4088 ME = new (PPRec) MacroExpansion(Name, Range); 4089 else 4090 ME = new (PPRec) MacroExpansion(Def, Range); 4091 4092 return ME; 4093 } 4094 4095 case PPD_MACRO_DEFINITION: { 4096 // Decode the identifier info and then check again; if the macro is 4097 // still defined and associated with the identifier, 4098 IdentifierInfo *II = getLocalIdentifier(M, Record[0]); 4099 MacroDefinition *MD 4100 = new (PPRec) MacroDefinition(II, Range); 4101 4102 if (DeserializationListener) 4103 DeserializationListener->MacroDefinitionRead(PPID, MD); 4104 4105 return MD; 4106 } 4107 4108 case PPD_INCLUSION_DIRECTIVE: { 4109 const char *FullFileNameStart = Blob.data() + Record[0]; 4110 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]); 4111 const FileEntry *File = 0; 4112 if (!FullFileName.empty()) 4113 File = PP.getFileManager().getFile(FullFileName); 4114 4115 // FIXME: Stable encoding 4116 InclusionDirective::InclusionKind Kind 4117 = static_cast<InclusionDirective::InclusionKind>(Record[2]); 4118 InclusionDirective *ID 4119 = new (PPRec) InclusionDirective(PPRec, Kind, 4120 StringRef(Blob.data(), Record[0]), 4121 Record[1], Record[3], 4122 File, 4123 Range); 4124 return ID; 4125 } 4126 } 4127 4128 llvm_unreachable("Invalid PreprocessorDetailRecordTypes"); 4129} 4130 4131/// \brief \arg SLocMapI points at a chunk of a module that contains no 4132/// preprocessed entities or the entities it contains are not the ones we are 4133/// looking for. Find the next module that contains entities and return the ID 4134/// of the first entry. 4135PreprocessedEntityID ASTReader::findNextPreprocessedEntity( 4136 GlobalSLocOffsetMapType::const_iterator SLocMapI) const { 4137 ++SLocMapI; 4138 for (GlobalSLocOffsetMapType::const_iterator 4139 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) { 4140 ModuleFile &M = *SLocMapI->second; 4141 if (M.NumPreprocessedEntities) 4142 return M.BasePreprocessedEntityID; 4143 } 4144 4145 return getTotalNumPreprocessedEntities(); 4146} 4147 4148namespace { 4149 4150template <unsigned PPEntityOffset::*PPLoc> 4151struct PPEntityComp { 4152 const ASTReader &Reader; 4153 ModuleFile &M; 4154 4155 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { } 4156 4157 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const { 4158 SourceLocation LHS = getLoc(L); 4159 SourceLocation RHS = getLoc(R); 4160 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 4161 } 4162 4163 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const { 4164 SourceLocation LHS = getLoc(L); 4165 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 4166 } 4167 4168 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const { 4169 SourceLocation RHS = getLoc(R); 4170 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 4171 } 4172 4173 SourceLocation getLoc(const PPEntityOffset &PPE) const { 4174 return Reader.ReadSourceLocation(M, PPE.*PPLoc); 4175 } 4176}; 4177 4178} 4179 4180/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc. 4181PreprocessedEntityID 4182ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const { 4183 if (SourceMgr.isLocalSourceLocation(BLoc)) 4184 return getTotalNumPreprocessedEntities(); 4185 4186 GlobalSLocOffsetMapType::const_iterator 4187 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset - 4188 BLoc.getOffset() - 1); 4189 assert(SLocMapI != GlobalSLocOffsetMap.end() && 4190 "Corrupted global sloc offset map"); 4191 4192 if (SLocMapI->second->NumPreprocessedEntities == 0) 4193 return findNextPreprocessedEntity(SLocMapI); 4194 4195 ModuleFile &M = *SLocMapI->second; 4196 typedef const PPEntityOffset *pp_iterator; 4197 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 4198 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 4199 4200 size_t Count = M.NumPreprocessedEntities; 4201 size_t Half; 4202 pp_iterator First = pp_begin; 4203 pp_iterator PPI; 4204 4205 // Do a binary search manually instead of using std::lower_bound because 4206 // The end locations of entities may be unordered (when a macro expansion 4207 // is inside another macro argument), but for this case it is not important 4208 // whether we get the first macro expansion or its containing macro. 4209 while (Count > 0) { 4210 Half = Count/2; 4211 PPI = First; 4212 std::advance(PPI, Half); 4213 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End), 4214 BLoc)){ 4215 First = PPI; 4216 ++First; 4217 Count = Count - Half - 1; 4218 } else 4219 Count = Half; 4220 } 4221 4222 if (PPI == pp_end) 4223 return findNextPreprocessedEntity(SLocMapI); 4224 4225 return M.BasePreprocessedEntityID + (PPI - pp_begin); 4226} 4227 4228/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc. 4229PreprocessedEntityID 4230ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const { 4231 if (SourceMgr.isLocalSourceLocation(ELoc)) 4232 return getTotalNumPreprocessedEntities(); 4233 4234 GlobalSLocOffsetMapType::const_iterator 4235 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset - 4236 ELoc.getOffset() - 1); 4237 assert(SLocMapI != GlobalSLocOffsetMap.end() && 4238 "Corrupted global sloc offset map"); 4239 4240 if (SLocMapI->second->NumPreprocessedEntities == 0) 4241 return findNextPreprocessedEntity(SLocMapI); 4242 4243 ModuleFile &M = *SLocMapI->second; 4244 typedef const PPEntityOffset *pp_iterator; 4245 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 4246 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 4247 pp_iterator PPI = 4248 std::upper_bound(pp_begin, pp_end, ELoc, 4249 PPEntityComp<&PPEntityOffset::Begin>(*this, M)); 4250 4251 if (PPI == pp_end) 4252 return findNextPreprocessedEntity(SLocMapI); 4253 4254 return M.BasePreprocessedEntityID + (PPI - pp_begin); 4255} 4256 4257/// \brief Returns a pair of [Begin, End) indices of preallocated 4258/// preprocessed entities that \arg Range encompasses. 4259std::pair<unsigned, unsigned> 4260 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) { 4261 if (Range.isInvalid()) 4262 return std::make_pair(0,0); 4263 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); 4264 4265 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin()); 4266 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd()); 4267 return std::make_pair(BeginID, EndID); 4268} 4269 4270/// \brief Optionally returns true or false if the preallocated preprocessed 4271/// entity with index \arg Index came from file \arg FID. 4272Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index, 4273 FileID FID) { 4274 if (FID.isInvalid()) 4275 return false; 4276 4277 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 4278 ModuleFile &M = *PPInfo.first; 4279 unsigned LocalIndex = PPInfo.second; 4280 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 4281 4282 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin); 4283 if (Loc.isInvalid()) 4284 return false; 4285 4286 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID)) 4287 return true; 4288 else 4289 return false; 4290} 4291 4292namespace { 4293 /// \brief Visitor used to search for information about a header file. 4294 class HeaderFileInfoVisitor { 4295 const FileEntry *FE; 4296 4297 Optional<HeaderFileInfo> HFI; 4298 4299 public: 4300 explicit HeaderFileInfoVisitor(const FileEntry *FE) 4301 : FE(FE) { } 4302 4303 static bool visit(ModuleFile &M, void *UserData) { 4304 HeaderFileInfoVisitor *This 4305 = static_cast<HeaderFileInfoVisitor *>(UserData); 4306 4307 HeaderFileInfoLookupTable *Table 4308 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable); 4309 if (!Table) 4310 return false; 4311 4312 // Look in the on-disk hash table for an entry for this file name. 4313 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE); 4314 if (Pos == Table->end()) 4315 return false; 4316 4317 This->HFI = *Pos; 4318 return true; 4319 } 4320 4321 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; } 4322 }; 4323} 4324 4325HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) { 4326 HeaderFileInfoVisitor Visitor(FE); 4327 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor); 4328 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) { 4329 if (Listener) 4330 Listener->ReadHeaderFileInfo(*HFI, FE->getUID()); 4331 return *HFI; 4332 } 4333 4334 return HeaderFileInfo(); 4335} 4336 4337void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) { 4338 // FIXME: Make it work properly with modules. 4339 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates; 4340 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) { 4341 ModuleFile &F = *(*I); 4342 unsigned Idx = 0; 4343 DiagStates.clear(); 4344 assert(!Diag.DiagStates.empty()); 4345 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one. 4346 while (Idx < F.PragmaDiagMappings.size()) { 4347 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]); 4348 unsigned DiagStateID = F.PragmaDiagMappings[Idx++]; 4349 if (DiagStateID != 0) { 4350 Diag.DiagStatePoints.push_back( 4351 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1], 4352 FullSourceLoc(Loc, SourceMgr))); 4353 continue; 4354 } 4355 4356 assert(DiagStateID == 0); 4357 // A new DiagState was created here. 4358 Diag.DiagStates.push_back(*Diag.GetCurDiagState()); 4359 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back(); 4360 DiagStates.push_back(NewState); 4361 Diag.DiagStatePoints.push_back( 4362 DiagnosticsEngine::DiagStatePoint(NewState, 4363 FullSourceLoc(Loc, SourceMgr))); 4364 while (1) { 4365 assert(Idx < F.PragmaDiagMappings.size() && 4366 "Invalid data, didn't find '-1' marking end of diag/map pairs"); 4367 if (Idx >= F.PragmaDiagMappings.size()) { 4368 break; // Something is messed up but at least avoid infinite loop in 4369 // release build. 4370 } 4371 unsigned DiagID = F.PragmaDiagMappings[Idx++]; 4372 if (DiagID == (unsigned)-1) { 4373 break; // no more diag/map pairs for this location. 4374 } 4375 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++]; 4376 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc); 4377 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo); 4378 } 4379 } 4380 } 4381} 4382 4383/// \brief Get the correct cursor and offset for loading a type. 4384ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) { 4385 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index); 4386 assert(I != GlobalTypeMap.end() && "Corrupted global type map"); 4387 ModuleFile *M = I->second; 4388 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]); 4389} 4390 4391/// \brief Read and return the type with the given index.. 4392/// 4393/// The index is the type ID, shifted and minus the number of predefs. This 4394/// routine actually reads the record corresponding to the type at the given 4395/// location. It is a helper routine for GetType, which deals with reading type 4396/// IDs. 4397QualType ASTReader::readTypeRecord(unsigned Index) { 4398 RecordLocation Loc = TypeCursorForIndex(Index); 4399 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 4400 4401 // Keep track of where we are in the stream, then jump back there 4402 // after reading this type. 4403 SavedStreamPosition SavedPosition(DeclsCursor); 4404 4405 ReadingKindTracker ReadingKind(Read_Type, *this); 4406 4407 // Note that we are loading a type record. 4408 Deserializing AType(this); 4409 4410 unsigned Idx = 0; 4411 DeclsCursor.JumpToBit(Loc.Offset); 4412 RecordData Record; 4413 unsigned Code = DeclsCursor.ReadCode(); 4414 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) { 4415 case TYPE_EXT_QUAL: { 4416 if (Record.size() != 2) { 4417 Error("Incorrect encoding of extended qualifier type"); 4418 return QualType(); 4419 } 4420 QualType Base = readType(*Loc.F, Record, Idx); 4421 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]); 4422 return Context.getQualifiedType(Base, Quals); 4423 } 4424 4425 case TYPE_COMPLEX: { 4426 if (Record.size() != 1) { 4427 Error("Incorrect encoding of complex type"); 4428 return QualType(); 4429 } 4430 QualType ElemType = readType(*Loc.F, Record, Idx); 4431 return Context.getComplexType(ElemType); 4432 } 4433 4434 case TYPE_POINTER: { 4435 if (Record.size() != 1) { 4436 Error("Incorrect encoding of pointer type"); 4437 return QualType(); 4438 } 4439 QualType PointeeType = readType(*Loc.F, Record, Idx); 4440 return Context.getPointerType(PointeeType); 4441 } 4442 4443 case TYPE_BLOCK_POINTER: { 4444 if (Record.size() != 1) { 4445 Error("Incorrect encoding of block pointer type"); 4446 return QualType(); 4447 } 4448 QualType PointeeType = readType(*Loc.F, Record, Idx); 4449 return Context.getBlockPointerType(PointeeType); 4450 } 4451 4452 case TYPE_LVALUE_REFERENCE: { 4453 if (Record.size() != 2) { 4454 Error("Incorrect encoding of lvalue reference type"); 4455 return QualType(); 4456 } 4457 QualType PointeeType = readType(*Loc.F, Record, Idx); 4458 return Context.getLValueReferenceType(PointeeType, Record[1]); 4459 } 4460 4461 case TYPE_RVALUE_REFERENCE: { 4462 if (Record.size() != 1) { 4463 Error("Incorrect encoding of rvalue reference type"); 4464 return QualType(); 4465 } 4466 QualType PointeeType = readType(*Loc.F, Record, Idx); 4467 return Context.getRValueReferenceType(PointeeType); 4468 } 4469 4470 case TYPE_MEMBER_POINTER: { 4471 if (Record.size() != 2) { 4472 Error("Incorrect encoding of member pointer type"); 4473 return QualType(); 4474 } 4475 QualType PointeeType = readType(*Loc.F, Record, Idx); 4476 QualType ClassType = readType(*Loc.F, Record, Idx); 4477 if (PointeeType.isNull() || ClassType.isNull()) 4478 return QualType(); 4479 4480 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr()); 4481 } 4482 4483 case TYPE_CONSTANT_ARRAY: { 4484 QualType ElementType = readType(*Loc.F, Record, Idx); 4485 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 4486 unsigned IndexTypeQuals = Record[2]; 4487 unsigned Idx = 3; 4488 llvm::APInt Size = ReadAPInt(Record, Idx); 4489 return Context.getConstantArrayType(ElementType, Size, 4490 ASM, IndexTypeQuals); 4491 } 4492 4493 case TYPE_INCOMPLETE_ARRAY: { 4494 QualType ElementType = readType(*Loc.F, Record, Idx); 4495 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 4496 unsigned IndexTypeQuals = Record[2]; 4497 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals); 4498 } 4499 4500 case TYPE_VARIABLE_ARRAY: { 4501 QualType ElementType = readType(*Loc.F, Record, Idx); 4502 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 4503 unsigned IndexTypeQuals = Record[2]; 4504 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]); 4505 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]); 4506 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F), 4507 ASM, IndexTypeQuals, 4508 SourceRange(LBLoc, RBLoc)); 4509 } 4510 4511 case TYPE_VECTOR: { 4512 if (Record.size() != 3) { 4513 Error("incorrect encoding of vector type in AST file"); 4514 return QualType(); 4515 } 4516 4517 QualType ElementType = readType(*Loc.F, Record, Idx); 4518 unsigned NumElements = Record[1]; 4519 unsigned VecKind = Record[2]; 4520 return Context.getVectorType(ElementType, NumElements, 4521 (VectorType::VectorKind)VecKind); 4522 } 4523 4524 case TYPE_EXT_VECTOR: { 4525 if (Record.size() != 3) { 4526 Error("incorrect encoding of extended vector type in AST file"); 4527 return QualType(); 4528 } 4529 4530 QualType ElementType = readType(*Loc.F, Record, Idx); 4531 unsigned NumElements = Record[1]; 4532 return Context.getExtVectorType(ElementType, NumElements); 4533 } 4534 4535 case TYPE_FUNCTION_NO_PROTO: { 4536 if (Record.size() != 6) { 4537 Error("incorrect encoding of no-proto function type"); 4538 return QualType(); 4539 } 4540 QualType ResultType = readType(*Loc.F, Record, Idx); 4541 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3], 4542 (CallingConv)Record[4], Record[5]); 4543 return Context.getFunctionNoProtoType(ResultType, Info); 4544 } 4545 4546 case TYPE_FUNCTION_PROTO: { 4547 QualType ResultType = readType(*Loc.F, Record, Idx); 4548 4549 FunctionProtoType::ExtProtoInfo EPI; 4550 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1], 4551 /*hasregparm*/ Record[2], 4552 /*regparm*/ Record[3], 4553 static_cast<CallingConv>(Record[4]), 4554 /*produces*/ Record[5]); 4555 4556 unsigned Idx = 6; 4557 unsigned NumParams = Record[Idx++]; 4558 SmallVector<QualType, 16> ParamTypes; 4559 for (unsigned I = 0; I != NumParams; ++I) 4560 ParamTypes.push_back(readType(*Loc.F, Record, Idx)); 4561 4562 EPI.Variadic = Record[Idx++]; 4563 EPI.HasTrailingReturn = Record[Idx++]; 4564 EPI.TypeQuals = Record[Idx++]; 4565 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]); 4566 ExceptionSpecificationType EST = 4567 static_cast<ExceptionSpecificationType>(Record[Idx++]); 4568 EPI.ExceptionSpecType = EST; 4569 SmallVector<QualType, 2> Exceptions; 4570 if (EST == EST_Dynamic) { 4571 EPI.NumExceptions = Record[Idx++]; 4572 for (unsigned I = 0; I != EPI.NumExceptions; ++I) 4573 Exceptions.push_back(readType(*Loc.F, Record, Idx)); 4574 EPI.Exceptions = Exceptions.data(); 4575 } else if (EST == EST_ComputedNoexcept) { 4576 EPI.NoexceptExpr = ReadExpr(*Loc.F); 4577 } else if (EST == EST_Uninstantiated) { 4578 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx); 4579 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx); 4580 } else if (EST == EST_Unevaluated) { 4581 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx); 4582 } 4583 return Context.getFunctionType(ResultType, ParamTypes, EPI); 4584 } 4585 4586 case TYPE_UNRESOLVED_USING: { 4587 unsigned Idx = 0; 4588 return Context.getTypeDeclType( 4589 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx)); 4590 } 4591 4592 case TYPE_TYPEDEF: { 4593 if (Record.size() != 2) { 4594 Error("incorrect encoding of typedef type"); 4595 return QualType(); 4596 } 4597 unsigned Idx = 0; 4598 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx); 4599 QualType Canonical = readType(*Loc.F, Record, Idx); 4600 if (!Canonical.isNull()) 4601 Canonical = Context.getCanonicalType(Canonical); 4602 return Context.getTypedefType(Decl, Canonical); 4603 } 4604 4605 case TYPE_TYPEOF_EXPR: 4606 return Context.getTypeOfExprType(ReadExpr(*Loc.F)); 4607 4608 case TYPE_TYPEOF: { 4609 if (Record.size() != 1) { 4610 Error("incorrect encoding of typeof(type) in AST file"); 4611 return QualType(); 4612 } 4613 QualType UnderlyingType = readType(*Loc.F, Record, Idx); 4614 return Context.getTypeOfType(UnderlyingType); 4615 } 4616 4617 case TYPE_DECLTYPE: { 4618 QualType UnderlyingType = readType(*Loc.F, Record, Idx); 4619 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType); 4620 } 4621 4622 case TYPE_UNARY_TRANSFORM: { 4623 QualType BaseType = readType(*Loc.F, Record, Idx); 4624 QualType UnderlyingType = readType(*Loc.F, Record, Idx); 4625 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2]; 4626 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind); 4627 } 4628 4629 case TYPE_AUTO: { 4630 QualType Deduced = readType(*Loc.F, Record, Idx); 4631 bool IsDecltypeAuto = Record[Idx++]; 4632 return Context.getAutoType(Deduced, IsDecltypeAuto); 4633 } 4634 4635 case TYPE_RECORD: { 4636 if (Record.size() != 2) { 4637 Error("incorrect encoding of record type"); 4638 return QualType(); 4639 } 4640 unsigned Idx = 0; 4641 bool IsDependent = Record[Idx++]; 4642 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx); 4643 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl()); 4644 QualType T = Context.getRecordType(RD); 4645 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 4646 return T; 4647 } 4648 4649 case TYPE_ENUM: { 4650 if (Record.size() != 2) { 4651 Error("incorrect encoding of enum type"); 4652 return QualType(); 4653 } 4654 unsigned Idx = 0; 4655 bool IsDependent = Record[Idx++]; 4656 QualType T 4657 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx)); 4658 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 4659 return T; 4660 } 4661 4662 case TYPE_ATTRIBUTED: { 4663 if (Record.size() != 3) { 4664 Error("incorrect encoding of attributed type"); 4665 return QualType(); 4666 } 4667 QualType modifiedType = readType(*Loc.F, Record, Idx); 4668 QualType equivalentType = readType(*Loc.F, Record, Idx); 4669 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]); 4670 return Context.getAttributedType(kind, modifiedType, equivalentType); 4671 } 4672 4673 case TYPE_PAREN: { 4674 if (Record.size() != 1) { 4675 Error("incorrect encoding of paren type"); 4676 return QualType(); 4677 } 4678 QualType InnerType = readType(*Loc.F, Record, Idx); 4679 return Context.getParenType(InnerType); 4680 } 4681 4682 case TYPE_PACK_EXPANSION: { 4683 if (Record.size() != 2) { 4684 Error("incorrect encoding of pack expansion type"); 4685 return QualType(); 4686 } 4687 QualType Pattern = readType(*Loc.F, Record, Idx); 4688 if (Pattern.isNull()) 4689 return QualType(); 4690 Optional<unsigned> NumExpansions; 4691 if (Record[1]) 4692 NumExpansions = Record[1] - 1; 4693 return Context.getPackExpansionType(Pattern, NumExpansions); 4694 } 4695 4696 case TYPE_ELABORATED: { 4697 unsigned Idx = 0; 4698 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 4699 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 4700 QualType NamedType = readType(*Loc.F, Record, Idx); 4701 return Context.getElaboratedType(Keyword, NNS, NamedType); 4702 } 4703 4704 case TYPE_OBJC_INTERFACE: { 4705 unsigned Idx = 0; 4706 ObjCInterfaceDecl *ItfD 4707 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx); 4708 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl()); 4709 } 4710 4711 case TYPE_OBJC_OBJECT: { 4712 unsigned Idx = 0; 4713 QualType Base = readType(*Loc.F, Record, Idx); 4714 unsigned NumProtos = Record[Idx++]; 4715 SmallVector<ObjCProtocolDecl*, 4> Protos; 4716 for (unsigned I = 0; I != NumProtos; ++I) 4717 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx)); 4718 return Context.getObjCObjectType(Base, Protos.data(), NumProtos); 4719 } 4720 4721 case TYPE_OBJC_OBJECT_POINTER: { 4722 unsigned Idx = 0; 4723 QualType Pointee = readType(*Loc.F, Record, Idx); 4724 return Context.getObjCObjectPointerType(Pointee); 4725 } 4726 4727 case TYPE_SUBST_TEMPLATE_TYPE_PARM: { 4728 unsigned Idx = 0; 4729 QualType Parm = readType(*Loc.F, Record, Idx); 4730 QualType Replacement = readType(*Loc.F, Record, Idx); 4731 return 4732 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm), 4733 Replacement); 4734 } 4735 4736 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: { 4737 unsigned Idx = 0; 4738 QualType Parm = readType(*Loc.F, Record, Idx); 4739 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx); 4740 return Context.getSubstTemplateTypeParmPackType( 4741 cast<TemplateTypeParmType>(Parm), 4742 ArgPack); 4743 } 4744 4745 case TYPE_INJECTED_CLASS_NAME: { 4746 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx); 4747 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable 4748 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable 4749 // for AST reading, too much interdependencies. 4750 return 4751 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0); 4752 } 4753 4754 case TYPE_TEMPLATE_TYPE_PARM: { 4755 unsigned Idx = 0; 4756 unsigned Depth = Record[Idx++]; 4757 unsigned Index = Record[Idx++]; 4758 bool Pack = Record[Idx++]; 4759 TemplateTypeParmDecl *D 4760 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx); 4761 return Context.getTemplateTypeParmType(Depth, Index, Pack, D); 4762 } 4763 4764 case TYPE_DEPENDENT_NAME: { 4765 unsigned Idx = 0; 4766 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 4767 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 4768 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx); 4769 QualType Canon = readType(*Loc.F, Record, Idx); 4770 if (!Canon.isNull()) 4771 Canon = Context.getCanonicalType(Canon); 4772 return Context.getDependentNameType(Keyword, NNS, Name, Canon); 4773 } 4774 4775 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: { 4776 unsigned Idx = 0; 4777 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 4778 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 4779 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx); 4780 unsigned NumArgs = Record[Idx++]; 4781 SmallVector<TemplateArgument, 8> Args; 4782 Args.reserve(NumArgs); 4783 while (NumArgs--) 4784 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx)); 4785 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name, 4786 Args.size(), Args.data()); 4787 } 4788 4789 case TYPE_DEPENDENT_SIZED_ARRAY: { 4790 unsigned Idx = 0; 4791 4792 // ArrayType 4793 QualType ElementType = readType(*Loc.F, Record, Idx); 4794 ArrayType::ArraySizeModifier ASM 4795 = (ArrayType::ArraySizeModifier)Record[Idx++]; 4796 unsigned IndexTypeQuals = Record[Idx++]; 4797 4798 // DependentSizedArrayType 4799 Expr *NumElts = ReadExpr(*Loc.F); 4800 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx); 4801 4802 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM, 4803 IndexTypeQuals, Brackets); 4804 } 4805 4806 case TYPE_TEMPLATE_SPECIALIZATION: { 4807 unsigned Idx = 0; 4808 bool IsDependent = Record[Idx++]; 4809 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx); 4810 SmallVector<TemplateArgument, 8> Args; 4811 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx); 4812 QualType Underlying = readType(*Loc.F, Record, Idx); 4813 QualType T; 4814 if (Underlying.isNull()) 4815 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(), 4816 Args.size()); 4817 else 4818 T = Context.getTemplateSpecializationType(Name, Args.data(), 4819 Args.size(), Underlying); 4820 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 4821 return T; 4822 } 4823 4824 case TYPE_ATOMIC: { 4825 if (Record.size() != 1) { 4826 Error("Incorrect encoding of atomic type"); 4827 return QualType(); 4828 } 4829 QualType ValueType = readType(*Loc.F, Record, Idx); 4830 return Context.getAtomicType(ValueType); 4831 } 4832 } 4833 llvm_unreachable("Invalid TypeCode!"); 4834} 4835 4836class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> { 4837 ASTReader &Reader; 4838 ModuleFile &F; 4839 const ASTReader::RecordData &Record; 4840 unsigned &Idx; 4841 4842 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R, 4843 unsigned &I) { 4844 return Reader.ReadSourceLocation(F, R, I); 4845 } 4846 4847 template<typename T> 4848 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) { 4849 return Reader.ReadDeclAs<T>(F, Record, Idx); 4850 } 4851 4852public: 4853 TypeLocReader(ASTReader &Reader, ModuleFile &F, 4854 const ASTReader::RecordData &Record, unsigned &Idx) 4855 : Reader(Reader), F(F), Record(Record), Idx(Idx) 4856 { } 4857 4858 // We want compile-time assurance that we've enumerated all of 4859 // these, so unfortunately we have to declare them first, then 4860 // define them out-of-line. 4861#define ABSTRACT_TYPELOC(CLASS, PARENT) 4862#define TYPELOC(CLASS, PARENT) \ 4863 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 4864#include "clang/AST/TypeLocNodes.def" 4865 4866 void VisitFunctionTypeLoc(FunctionTypeLoc); 4867 void VisitArrayTypeLoc(ArrayTypeLoc); 4868}; 4869 4870void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 4871 // nothing to do 4872} 4873void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 4874 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx)); 4875 if (TL.needsExtraLocalData()) { 4876 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++])); 4877 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++])); 4878 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++])); 4879 TL.setModeAttr(Record[Idx++]); 4880 } 4881} 4882void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { 4883 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4884} 4885void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { 4886 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 4887} 4888void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 4889 TL.setCaretLoc(ReadSourceLocation(Record, Idx)); 4890} 4891void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 4892 TL.setAmpLoc(ReadSourceLocation(Record, Idx)); 4893} 4894void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 4895 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx)); 4896} 4897void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 4898 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 4899 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx)); 4900} 4901void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { 4902 TL.setLBracketLoc(ReadSourceLocation(Record, Idx)); 4903 TL.setRBracketLoc(ReadSourceLocation(Record, Idx)); 4904 if (Record[Idx++]) 4905 TL.setSizeExpr(Reader.ReadExpr(F)); 4906 else 4907 TL.setSizeExpr(0); 4908} 4909void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 4910 VisitArrayTypeLoc(TL); 4911} 4912void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 4913 VisitArrayTypeLoc(TL); 4914} 4915void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 4916 VisitArrayTypeLoc(TL); 4917} 4918void TypeLocReader::VisitDependentSizedArrayTypeLoc( 4919 DependentSizedArrayTypeLoc TL) { 4920 VisitArrayTypeLoc(TL); 4921} 4922void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( 4923 DependentSizedExtVectorTypeLoc TL) { 4924 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4925} 4926void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { 4927 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4928} 4929void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 4930 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4931} 4932void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 4933 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx)); 4934 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 4935 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 4936 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx)); 4937 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) { 4938 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx)); 4939 } 4940} 4941void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 4942 VisitFunctionTypeLoc(TL); 4943} 4944void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 4945 VisitFunctionTypeLoc(TL); 4946} 4947void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 4948 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4949} 4950void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 4951 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4952} 4953void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 4954 TL.setTypeofLoc(ReadSourceLocation(Record, Idx)); 4955 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 4956 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 4957} 4958void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 4959 TL.setTypeofLoc(ReadSourceLocation(Record, Idx)); 4960 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 4961 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 4962 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx)); 4963} 4964void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 4965 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4966} 4967void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { 4968 TL.setKWLoc(ReadSourceLocation(Record, Idx)); 4969 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 4970 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 4971 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx)); 4972} 4973void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) { 4974 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4975} 4976void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { 4977 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4978} 4979void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { 4980 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4981} 4982void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 4983 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx)); 4984 if (TL.hasAttrOperand()) { 4985 SourceRange range; 4986 range.setBegin(ReadSourceLocation(Record, Idx)); 4987 range.setEnd(ReadSourceLocation(Record, Idx)); 4988 TL.setAttrOperandParensRange(range); 4989 } 4990 if (TL.hasAttrExprOperand()) { 4991 if (Record[Idx++]) 4992 TL.setAttrExprOperand(Reader.ReadExpr(F)); 4993 else 4994 TL.setAttrExprOperand(0); 4995 } else if (TL.hasAttrEnumOperand()) 4996 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx)); 4997} 4998void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 4999 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5000} 5001void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( 5002 SubstTemplateTypeParmTypeLoc TL) { 5003 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5004} 5005void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc( 5006 SubstTemplateTypeParmPackTypeLoc TL) { 5007 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5008} 5009void TypeLocReader::VisitTemplateSpecializationTypeLoc( 5010 TemplateSpecializationTypeLoc TL) { 5011 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx)); 5012 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx)); 5013 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 5014 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 5015 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 5016 TL.setArgLocInfo(i, 5017 Reader.GetTemplateArgumentLocInfo(F, 5018 TL.getTypePtr()->getArg(i).getKind(), 5019 Record, Idx)); 5020} 5021void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) { 5022 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 5023 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 5024} 5025void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 5026 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx)); 5027 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx)); 5028} 5029void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 5030 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5031} 5032void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 5033 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx)); 5034 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx)); 5035 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5036} 5037void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( 5038 DependentTemplateSpecializationTypeLoc TL) { 5039 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx)); 5040 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx)); 5041 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx)); 5042 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx)); 5043 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 5044 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 5045 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 5046 TL.setArgLocInfo(I, 5047 Reader.GetTemplateArgumentLocInfo(F, 5048 TL.getTypePtr()->getArg(I).getKind(), 5049 Record, Idx)); 5050} 5051void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 5052 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx)); 5053} 5054void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 5055 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5056} 5057void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 5058 TL.setHasBaseTypeAsWritten(Record[Idx++]); 5059 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 5060 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 5061 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 5062 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx)); 5063} 5064void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 5065 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 5066} 5067void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) { 5068 TL.setKWLoc(ReadSourceLocation(Record, Idx)); 5069 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 5070 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 5071} 5072 5073TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F, 5074 const RecordData &Record, 5075 unsigned &Idx) { 5076 QualType InfoTy = readType(F, Record, Idx); 5077 if (InfoTy.isNull()) 5078 return 0; 5079 5080 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy); 5081 TypeLocReader TLR(*this, F, Record, Idx); 5082 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc()) 5083 TLR.Visit(TL); 5084 return TInfo; 5085} 5086 5087QualType ASTReader::GetType(TypeID ID) { 5088 unsigned FastQuals = ID & Qualifiers::FastMask; 5089 unsigned Index = ID >> Qualifiers::FastWidth; 5090 5091 if (Index < NUM_PREDEF_TYPE_IDS) { 5092 QualType T; 5093 switch ((PredefinedTypeIDs)Index) { 5094 case PREDEF_TYPE_NULL_ID: return QualType(); 5095 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break; 5096 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break; 5097 5098 case PREDEF_TYPE_CHAR_U_ID: 5099 case PREDEF_TYPE_CHAR_S_ID: 5100 // FIXME: Check that the signedness of CharTy is correct! 5101 T = Context.CharTy; 5102 break; 5103 5104 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break; 5105 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break; 5106 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break; 5107 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break; 5108 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break; 5109 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break; 5110 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break; 5111 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break; 5112 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break; 5113 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break; 5114 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break; 5115 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break; 5116 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break; 5117 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break; 5118 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break; 5119 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break; 5120 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break; 5121 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break; 5122 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break; 5123 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break; 5124 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break; 5125 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break; 5126 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break; 5127 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break; 5128 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break; 5129 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break; 5130 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break; 5131 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break; 5132 case PREDEF_TYPE_IMAGE1D_ID: T = Context.OCLImage1dTy; break; 5133 case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break; 5134 case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break; 5135 case PREDEF_TYPE_IMAGE2D_ID: T = Context.OCLImage2dTy; break; 5136 case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break; 5137 case PREDEF_TYPE_IMAGE3D_ID: T = Context.OCLImage3dTy; break; 5138 case PREDEF_TYPE_SAMPLER_ID: T = Context.OCLSamplerTy; break; 5139 case PREDEF_TYPE_EVENT_ID: T = Context.OCLEventTy; break; 5140 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break; 5141 5142 case PREDEF_TYPE_AUTO_RREF_DEDUCT: 5143 T = Context.getAutoRRefDeductType(); 5144 break; 5145 5146 case PREDEF_TYPE_ARC_UNBRIDGED_CAST: 5147 T = Context.ARCUnbridgedCastTy; 5148 break; 5149 5150 case PREDEF_TYPE_VA_LIST_TAG: 5151 T = Context.getVaListTagType(); 5152 break; 5153 5154 case PREDEF_TYPE_BUILTIN_FN: 5155 T = Context.BuiltinFnTy; 5156 break; 5157 } 5158 5159 assert(!T.isNull() && "Unknown predefined type"); 5160 return T.withFastQualifiers(FastQuals); 5161 } 5162 5163 Index -= NUM_PREDEF_TYPE_IDS; 5164 assert(Index < TypesLoaded.size() && "Type index out-of-range"); 5165 if (TypesLoaded[Index].isNull()) { 5166 TypesLoaded[Index] = readTypeRecord(Index); 5167 if (TypesLoaded[Index].isNull()) 5168 return QualType(); 5169 5170 TypesLoaded[Index]->setFromAST(); 5171 if (DeserializationListener) 5172 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID), 5173 TypesLoaded[Index]); 5174 } 5175 5176 return TypesLoaded[Index].withFastQualifiers(FastQuals); 5177} 5178 5179QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) { 5180 return GetType(getGlobalTypeID(F, LocalID)); 5181} 5182 5183serialization::TypeID 5184ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const { 5185 unsigned FastQuals = LocalID & Qualifiers::FastMask; 5186 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth; 5187 5188 if (LocalIndex < NUM_PREDEF_TYPE_IDS) 5189 return LocalID; 5190 5191 ContinuousRangeMap<uint32_t, int, 2>::iterator I 5192 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS); 5193 assert(I != F.TypeRemap.end() && "Invalid index into type index remap"); 5194 5195 unsigned GlobalIndex = LocalIndex + I->second; 5196 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals; 5197} 5198 5199TemplateArgumentLocInfo 5200ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F, 5201 TemplateArgument::ArgKind Kind, 5202 const RecordData &Record, 5203 unsigned &Index) { 5204 switch (Kind) { 5205 case TemplateArgument::Expression: 5206 return ReadExpr(F); 5207 case TemplateArgument::Type: 5208 return GetTypeSourceInfo(F, Record, Index); 5209 case TemplateArgument::Template: { 5210 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, 5211 Index); 5212 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index); 5213 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 5214 SourceLocation()); 5215 } 5216 case TemplateArgument::TemplateExpansion: { 5217 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, 5218 Index); 5219 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index); 5220 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index); 5221 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 5222 EllipsisLoc); 5223 } 5224 case TemplateArgument::Null: 5225 case TemplateArgument::Integral: 5226 case TemplateArgument::Declaration: 5227 case TemplateArgument::NullPtr: 5228 case TemplateArgument::Pack: 5229 // FIXME: Is this right? 5230 return TemplateArgumentLocInfo(); 5231 } 5232 llvm_unreachable("unexpected template argument loc"); 5233} 5234 5235TemplateArgumentLoc 5236ASTReader::ReadTemplateArgumentLoc(ModuleFile &F, 5237 const RecordData &Record, unsigned &Index) { 5238 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index); 5239 5240 if (Arg.getKind() == TemplateArgument::Expression) { 5241 if (Record[Index++]) // bool InfoHasSameExpr. 5242 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); 5243 } 5244 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(), 5245 Record, Index)); 5246} 5247 5248Decl *ASTReader::GetExternalDecl(uint32_t ID) { 5249 return GetDecl(ID); 5250} 5251 5252uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record, 5253 unsigned &Idx){ 5254 if (Idx >= Record.size()) 5255 return 0; 5256 5257 unsigned LocalID = Record[Idx++]; 5258 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]); 5259} 5260 5261CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) { 5262 RecordLocation Loc = getLocalBitOffset(Offset); 5263 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 5264 SavedStreamPosition SavedPosition(Cursor); 5265 Cursor.JumpToBit(Loc.Offset); 5266 ReadingKindTracker ReadingKind(Read_Decl, *this); 5267 RecordData Record; 5268 unsigned Code = Cursor.ReadCode(); 5269 unsigned RecCode = Cursor.readRecord(Code, Record); 5270 if (RecCode != DECL_CXX_BASE_SPECIFIERS) { 5271 Error("Malformed AST file: missing C++ base specifiers"); 5272 return 0; 5273 } 5274 5275 unsigned Idx = 0; 5276 unsigned NumBases = Record[Idx++]; 5277 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases); 5278 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases]; 5279 for (unsigned I = 0; I != NumBases; ++I) 5280 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx); 5281 return Bases; 5282} 5283 5284serialization::DeclID 5285ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const { 5286 if (LocalID < NUM_PREDEF_DECL_IDS) 5287 return LocalID; 5288 5289 ContinuousRangeMap<uint32_t, int, 2>::iterator I 5290 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS); 5291 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap"); 5292 5293 return LocalID + I->second; 5294} 5295 5296bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID, 5297 ModuleFile &M) const { 5298 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID); 5299 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 5300 return &M == I->second; 5301} 5302 5303ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) { 5304 if (!D->isFromASTFile()) 5305 return 0; 5306 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID()); 5307 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 5308 return I->second; 5309} 5310 5311SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) { 5312 if (ID < NUM_PREDEF_DECL_IDS) 5313 return SourceLocation(); 5314 5315 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 5316 5317 if (Index > DeclsLoaded.size()) { 5318 Error("declaration ID out-of-range for AST file"); 5319 return SourceLocation(); 5320 } 5321 5322 if (Decl *D = DeclsLoaded[Index]) 5323 return D->getLocation(); 5324 5325 unsigned RawLocation = 0; 5326 RecordLocation Rec = DeclCursorForID(ID, RawLocation); 5327 return ReadSourceLocation(*Rec.F, RawLocation); 5328} 5329 5330Decl *ASTReader::GetDecl(DeclID ID) { 5331 if (ID < NUM_PREDEF_DECL_IDS) { 5332 switch ((PredefinedDeclIDs)ID) { 5333 case PREDEF_DECL_NULL_ID: 5334 return 0; 5335 5336 case PREDEF_DECL_TRANSLATION_UNIT_ID: 5337 return Context.getTranslationUnitDecl(); 5338 5339 case PREDEF_DECL_OBJC_ID_ID: 5340 return Context.getObjCIdDecl(); 5341 5342 case PREDEF_DECL_OBJC_SEL_ID: 5343 return Context.getObjCSelDecl(); 5344 5345 case PREDEF_DECL_OBJC_CLASS_ID: 5346 return Context.getObjCClassDecl(); 5347 5348 case PREDEF_DECL_OBJC_PROTOCOL_ID: 5349 return Context.getObjCProtocolDecl(); 5350 5351 case PREDEF_DECL_INT_128_ID: 5352 return Context.getInt128Decl(); 5353 5354 case PREDEF_DECL_UNSIGNED_INT_128_ID: 5355 return Context.getUInt128Decl(); 5356 5357 case PREDEF_DECL_OBJC_INSTANCETYPE_ID: 5358 return Context.getObjCInstanceTypeDecl(); 5359 5360 case PREDEF_DECL_BUILTIN_VA_LIST_ID: 5361 return Context.getBuiltinVaListDecl(); 5362 } 5363 } 5364 5365 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 5366 5367 if (Index >= DeclsLoaded.size()) { 5368 assert(0 && "declaration ID out-of-range for AST file"); 5369 Error("declaration ID out-of-range for AST file"); 5370 return 0; 5371 } 5372 5373 if (!DeclsLoaded[Index]) { 5374 ReadDeclRecord(ID); 5375 if (DeserializationListener) 5376 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); 5377 } 5378 5379 return DeclsLoaded[Index]; 5380} 5381 5382DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M, 5383 DeclID GlobalID) { 5384 if (GlobalID < NUM_PREDEF_DECL_IDS) 5385 return GlobalID; 5386 5387 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID); 5388 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 5389 ModuleFile *Owner = I->second; 5390 5391 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos 5392 = M.GlobalToLocalDeclIDs.find(Owner); 5393 if (Pos == M.GlobalToLocalDeclIDs.end()) 5394 return 0; 5395 5396 return GlobalID - Owner->BaseDeclID + Pos->second; 5397} 5398 5399serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F, 5400 const RecordData &Record, 5401 unsigned &Idx) { 5402 if (Idx >= Record.size()) { 5403 Error("Corrupted AST file"); 5404 return 0; 5405 } 5406 5407 return getGlobalDeclID(F, Record[Idx++]); 5408} 5409 5410/// \brief Resolve the offset of a statement into a statement. 5411/// 5412/// This operation will read a new statement from the external 5413/// source each time it is called, and is meant to be used via a 5414/// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 5415Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) { 5416 // Switch case IDs are per Decl. 5417 ClearSwitchCaseIDs(); 5418 5419 // Offset here is a global offset across the entire chain. 5420 RecordLocation Loc = getLocalBitOffset(Offset); 5421 Loc.F->DeclsCursor.JumpToBit(Loc.Offset); 5422 return ReadStmtFromStream(*Loc.F); 5423} 5424 5425namespace { 5426 class FindExternalLexicalDeclsVisitor { 5427 ASTReader &Reader; 5428 const DeclContext *DC; 5429 bool (*isKindWeWant)(Decl::Kind); 5430 5431 SmallVectorImpl<Decl*> &Decls; 5432 bool PredefsVisited[NUM_PREDEF_DECL_IDS]; 5433 5434 public: 5435 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC, 5436 bool (*isKindWeWant)(Decl::Kind), 5437 SmallVectorImpl<Decl*> &Decls) 5438 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls) 5439 { 5440 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I) 5441 PredefsVisited[I] = false; 5442 } 5443 5444 static bool visit(ModuleFile &M, bool Preorder, void *UserData) { 5445 if (Preorder) 5446 return false; 5447 5448 FindExternalLexicalDeclsVisitor *This 5449 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData); 5450 5451 ModuleFile::DeclContextInfosMap::iterator Info 5452 = M.DeclContextInfos.find(This->DC); 5453 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls) 5454 return false; 5455 5456 // Load all of the declaration IDs 5457 for (const KindDeclIDPair *ID = Info->second.LexicalDecls, 5458 *IDE = ID + Info->second.NumLexicalDecls; 5459 ID != IDE; ++ID) { 5460 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first)) 5461 continue; 5462 5463 // Don't add predefined declarations to the lexical context more 5464 // than once. 5465 if (ID->second < NUM_PREDEF_DECL_IDS) { 5466 if (This->PredefsVisited[ID->second]) 5467 continue; 5468 5469 This->PredefsVisited[ID->second] = true; 5470 } 5471 5472 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) { 5473 if (!This->DC->isDeclInLexicalTraversal(D)) 5474 This->Decls.push_back(D); 5475 } 5476 } 5477 5478 return false; 5479 } 5480 }; 5481} 5482 5483ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC, 5484 bool (*isKindWeWant)(Decl::Kind), 5485 SmallVectorImpl<Decl*> &Decls) { 5486 // There might be lexical decls in multiple modules, for the TU at 5487 // least. Walk all of the modules in the order they were loaded. 5488 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls); 5489 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor); 5490 ++NumLexicalDeclContextsRead; 5491 return ELR_Success; 5492} 5493 5494namespace { 5495 5496class DeclIDComp { 5497 ASTReader &Reader; 5498 ModuleFile &Mod; 5499 5500public: 5501 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {} 5502 5503 bool operator()(LocalDeclID L, LocalDeclID R) const { 5504 SourceLocation LHS = getLocation(L); 5505 SourceLocation RHS = getLocation(R); 5506 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 5507 } 5508 5509 bool operator()(SourceLocation LHS, LocalDeclID R) const { 5510 SourceLocation RHS = getLocation(R); 5511 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 5512 } 5513 5514 bool operator()(LocalDeclID L, SourceLocation RHS) const { 5515 SourceLocation LHS = getLocation(L); 5516 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 5517 } 5518 5519 SourceLocation getLocation(LocalDeclID ID) const { 5520 return Reader.getSourceManager().getFileLoc( 5521 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID))); 5522 } 5523}; 5524 5525} 5526 5527void ASTReader::FindFileRegionDecls(FileID File, 5528 unsigned Offset, unsigned Length, 5529 SmallVectorImpl<Decl *> &Decls) { 5530 SourceManager &SM = getSourceManager(); 5531 5532 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File); 5533 if (I == FileDeclIDs.end()) 5534 return; 5535 5536 FileDeclsInfo &DInfo = I->second; 5537 if (DInfo.Decls.empty()) 5538 return; 5539 5540 SourceLocation 5541 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset); 5542 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length); 5543 5544 DeclIDComp DIDComp(*this, *DInfo.Mod); 5545 ArrayRef<serialization::LocalDeclID>::iterator 5546 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(), 5547 BeginLoc, DIDComp); 5548 if (BeginIt != DInfo.Decls.begin()) 5549 --BeginIt; 5550 5551 // If we are pointing at a top-level decl inside an objc container, we need 5552 // to backtrack until we find it otherwise we will fail to report that the 5553 // region overlaps with an objc container. 5554 while (BeginIt != DInfo.Decls.begin() && 5555 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt)) 5556 ->isTopLevelDeclInObjCContainer()) 5557 --BeginIt; 5558 5559 ArrayRef<serialization::LocalDeclID>::iterator 5560 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(), 5561 EndLoc, DIDComp); 5562 if (EndIt != DInfo.Decls.end()) 5563 ++EndIt; 5564 5565 for (ArrayRef<serialization::LocalDeclID>::iterator 5566 DIt = BeginIt; DIt != EndIt; ++DIt) 5567 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt))); 5568} 5569 5570namespace { 5571 /// \brief ModuleFile visitor used to perform name lookup into a 5572 /// declaration context. 5573 class DeclContextNameLookupVisitor { 5574 ASTReader &Reader; 5575 SmallVectorImpl<const DeclContext *> &Contexts; 5576 DeclarationName Name; 5577 SmallVectorImpl<NamedDecl *> &Decls; 5578 5579 public: 5580 DeclContextNameLookupVisitor(ASTReader &Reader, 5581 SmallVectorImpl<const DeclContext *> &Contexts, 5582 DeclarationName Name, 5583 SmallVectorImpl<NamedDecl *> &Decls) 5584 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { } 5585 5586 static bool visit(ModuleFile &M, void *UserData) { 5587 DeclContextNameLookupVisitor *This 5588 = static_cast<DeclContextNameLookupVisitor *>(UserData); 5589 5590 // Check whether we have any visible declaration information for 5591 // this context in this module. 5592 ModuleFile::DeclContextInfosMap::iterator Info; 5593 bool FoundInfo = false; 5594 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) { 5595 Info = M.DeclContextInfos.find(This->Contexts[I]); 5596 if (Info != M.DeclContextInfos.end() && 5597 Info->second.NameLookupTableData) { 5598 FoundInfo = true; 5599 break; 5600 } 5601 } 5602 5603 if (!FoundInfo) 5604 return false; 5605 5606 // Look for this name within this module. 5607 ASTDeclContextNameLookupTable *LookupTable = 5608 Info->second.NameLookupTableData; 5609 ASTDeclContextNameLookupTable::iterator Pos 5610 = LookupTable->find(This->Name); 5611 if (Pos == LookupTable->end()) 5612 return false; 5613 5614 bool FoundAnything = false; 5615 ASTDeclContextNameLookupTrait::data_type Data = *Pos; 5616 for (; Data.first != Data.second; ++Data.first) { 5617 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first); 5618 if (!ND) 5619 continue; 5620 5621 if (ND->getDeclName() != This->Name) { 5622 // A name might be null because the decl's redeclarable part is 5623 // currently read before reading its name. The lookup is triggered by 5624 // building that decl (likely indirectly), and so it is later in the 5625 // sense of "already existing" and can be ignored here. 5626 continue; 5627 } 5628 5629 // Record this declaration. 5630 FoundAnything = true; 5631 This->Decls.push_back(ND); 5632 } 5633 5634 return FoundAnything; 5635 } 5636 }; 5637} 5638 5639/// \brief Retrieve the "definitive" module file for the definition of the 5640/// given declaration context, if there is one. 5641/// 5642/// The "definitive" module file is the only place where we need to look to 5643/// find information about the declarations within the given declaration 5644/// context. For example, C++ and Objective-C classes, C structs/unions, and 5645/// Objective-C protocols, categories, and extensions are all defined in a 5646/// single place in the source code, so they have definitive module files 5647/// associated with them. C++ namespaces, on the other hand, can have 5648/// definitions in multiple different module files. 5649/// 5650/// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's 5651/// NDEBUG checking. 5652static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC, 5653 ASTReader &Reader) { 5654 if (const DeclContext *DefDC = getDefinitiveDeclContext(DC)) 5655 return Reader.getOwningModuleFile(cast<Decl>(DefDC)); 5656 5657 return 0; 5658} 5659 5660bool 5661ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC, 5662 DeclarationName Name) { 5663 assert(DC->hasExternalVisibleStorage() && 5664 "DeclContext has no visible decls in storage"); 5665 if (!Name) 5666 return false; 5667 5668 SmallVector<NamedDecl *, 64> Decls; 5669 5670 // Compute the declaration contexts we need to look into. Multiple such 5671 // declaration contexts occur when two declaration contexts from disjoint 5672 // modules get merged, e.g., when two namespaces with the same name are 5673 // independently defined in separate modules. 5674 SmallVector<const DeclContext *, 2> Contexts; 5675 Contexts.push_back(DC); 5676 5677 if (DC->isNamespace()) { 5678 MergedDeclsMap::iterator Merged 5679 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC))); 5680 if (Merged != MergedDecls.end()) { 5681 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I) 5682 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I]))); 5683 } 5684 } 5685 5686 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls); 5687 5688 // If we can definitively determine which module file to look into, 5689 // only look there. Otherwise, look in all module files. 5690 ModuleFile *Definitive; 5691 if (Contexts.size() == 1 && 5692 (Definitive = getDefinitiveModuleFileFor(DC, *this))) { 5693 DeclContextNameLookupVisitor::visit(*Definitive, &Visitor); 5694 } else { 5695 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor); 5696 } 5697 ++NumVisibleDeclContextsRead; 5698 SetExternalVisibleDeclsForName(DC, Name, Decls); 5699 return !Decls.empty(); 5700} 5701 5702namespace { 5703 /// \brief ModuleFile visitor used to retrieve all visible names in a 5704 /// declaration context. 5705 class DeclContextAllNamesVisitor { 5706 ASTReader &Reader; 5707 SmallVectorImpl<const DeclContext *> &Contexts; 5708 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls; 5709 bool VisitAll; 5710 5711 public: 5712 DeclContextAllNamesVisitor(ASTReader &Reader, 5713 SmallVectorImpl<const DeclContext *> &Contexts, 5714 llvm::DenseMap<DeclarationName, 5715 SmallVector<NamedDecl *, 8> > &Decls, 5716 bool VisitAll) 5717 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { } 5718 5719 static bool visit(ModuleFile &M, void *UserData) { 5720 DeclContextAllNamesVisitor *This 5721 = static_cast<DeclContextAllNamesVisitor *>(UserData); 5722 5723 // Check whether we have any visible declaration information for 5724 // this context in this module. 5725 ModuleFile::DeclContextInfosMap::iterator Info; 5726 bool FoundInfo = false; 5727 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) { 5728 Info = M.DeclContextInfos.find(This->Contexts[I]); 5729 if (Info != M.DeclContextInfos.end() && 5730 Info->second.NameLookupTableData) { 5731 FoundInfo = true; 5732 break; 5733 } 5734 } 5735 5736 if (!FoundInfo) 5737 return false; 5738 5739 ASTDeclContextNameLookupTable *LookupTable = 5740 Info->second.NameLookupTableData; 5741 bool FoundAnything = false; 5742 for (ASTDeclContextNameLookupTable::data_iterator 5743 I = LookupTable->data_begin(), E = LookupTable->data_end(); 5744 I != E; 5745 ++I) { 5746 ASTDeclContextNameLookupTrait::data_type Data = *I; 5747 for (; Data.first != Data.second; ++Data.first) { 5748 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, 5749 *Data.first); 5750 if (!ND) 5751 continue; 5752 5753 // Record this declaration. 5754 FoundAnything = true; 5755 This->Decls[ND->getDeclName()].push_back(ND); 5756 } 5757 } 5758 5759 return FoundAnything && !This->VisitAll; 5760 } 5761 }; 5762} 5763 5764void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) { 5765 if (!DC->hasExternalVisibleStorage()) 5766 return; 5767 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > Decls; 5768 5769 // Compute the declaration contexts we need to look into. Multiple such 5770 // declaration contexts occur when two declaration contexts from disjoint 5771 // modules get merged, e.g., when two namespaces with the same name are 5772 // independently defined in separate modules. 5773 SmallVector<const DeclContext *, 2> Contexts; 5774 Contexts.push_back(DC); 5775 5776 if (DC->isNamespace()) { 5777 MergedDeclsMap::iterator Merged 5778 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC))); 5779 if (Merged != MergedDecls.end()) { 5780 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I) 5781 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I]))); 5782 } 5783 } 5784 5785 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls, 5786 /*VisitAll=*/DC->isFileContext()); 5787 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor); 5788 ++NumVisibleDeclContextsRead; 5789 5790 for (llvm::DenseMap<DeclarationName, 5791 SmallVector<NamedDecl *, 8> >::iterator 5792 I = Decls.begin(), E = Decls.end(); I != E; ++I) { 5793 SetExternalVisibleDeclsForName(DC, I->first, I->second); 5794 } 5795 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false); 5796} 5797 5798/// \brief Under non-PCH compilation the consumer receives the objc methods 5799/// before receiving the implementation, and codegen depends on this. 5800/// We simulate this by deserializing and passing to consumer the methods of the 5801/// implementation before passing the deserialized implementation decl. 5802static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD, 5803 ASTConsumer *Consumer) { 5804 assert(ImplD && Consumer); 5805 5806 for (ObjCImplDecl::method_iterator 5807 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I) 5808 Consumer->HandleInterestingDecl(DeclGroupRef(*I)); 5809 5810 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD)); 5811} 5812 5813void ASTReader::PassInterestingDeclsToConsumer() { 5814 assert(Consumer); 5815 while (!InterestingDecls.empty()) { 5816 Decl *D = InterestingDecls.front(); 5817 InterestingDecls.pop_front(); 5818 5819 PassInterestingDeclToConsumer(D); 5820 } 5821} 5822 5823void ASTReader::PassInterestingDeclToConsumer(Decl *D) { 5824 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) 5825 PassObjCImplDeclToConsumer(ImplD, Consumer); 5826 else 5827 Consumer->HandleInterestingDecl(DeclGroupRef(D)); 5828} 5829 5830void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) { 5831 this->Consumer = Consumer; 5832 5833 if (!Consumer) 5834 return; 5835 5836 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) { 5837 // Force deserialization of this decl, which will cause it to be queued for 5838 // passing to the consumer. 5839 GetDecl(ExternalDefinitions[I]); 5840 } 5841 ExternalDefinitions.clear(); 5842 5843 PassInterestingDeclsToConsumer(); 5844} 5845 5846void ASTReader::PrintStats() { 5847 std::fprintf(stderr, "*** AST File Statistics:\n"); 5848 5849 unsigned NumTypesLoaded 5850 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), 5851 QualType()); 5852 unsigned NumDeclsLoaded 5853 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), 5854 (Decl *)0); 5855 unsigned NumIdentifiersLoaded 5856 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), 5857 IdentifiersLoaded.end(), 5858 (IdentifierInfo *)0); 5859 unsigned NumMacrosLoaded 5860 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(), 5861 MacrosLoaded.end(), 5862 (MacroInfo *)0); 5863 unsigned NumSelectorsLoaded 5864 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), 5865 SelectorsLoaded.end(), 5866 Selector()); 5867 5868 if (unsigned TotalNumSLocEntries = getTotalNumSLocs()) 5869 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", 5870 NumSLocEntriesRead, TotalNumSLocEntries, 5871 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); 5872 if (!TypesLoaded.empty()) 5873 std::fprintf(stderr, " %u/%u types read (%f%%)\n", 5874 NumTypesLoaded, (unsigned)TypesLoaded.size(), 5875 ((float)NumTypesLoaded/TypesLoaded.size() * 100)); 5876 if (!DeclsLoaded.empty()) 5877 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", 5878 NumDeclsLoaded, (unsigned)DeclsLoaded.size(), 5879 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); 5880 if (!IdentifiersLoaded.empty()) 5881 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", 5882 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), 5883 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); 5884 if (!MacrosLoaded.empty()) 5885 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 5886 NumMacrosLoaded, (unsigned)MacrosLoaded.size(), 5887 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100)); 5888 if (!SelectorsLoaded.empty()) 5889 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", 5890 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(), 5891 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100)); 5892 if (TotalNumStatements) 5893 std::fprintf(stderr, " %u/%u statements read (%f%%)\n", 5894 NumStatementsRead, TotalNumStatements, 5895 ((float)NumStatementsRead/TotalNumStatements * 100)); 5896 if (TotalNumMacros) 5897 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 5898 NumMacrosRead, TotalNumMacros, 5899 ((float)NumMacrosRead/TotalNumMacros * 100)); 5900 if (TotalLexicalDeclContexts) 5901 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", 5902 NumLexicalDeclContextsRead, TotalLexicalDeclContexts, 5903 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts 5904 * 100)); 5905 if (TotalVisibleDeclContexts) 5906 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", 5907 NumVisibleDeclContextsRead, TotalVisibleDeclContexts, 5908 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts 5909 * 100)); 5910 if (TotalNumMethodPoolEntries) { 5911 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", 5912 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries, 5913 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries 5914 * 100)); 5915 } 5916 if (NumMethodPoolLookups) { 5917 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n", 5918 NumMethodPoolHits, NumMethodPoolLookups, 5919 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0)); 5920 } 5921 if (NumMethodPoolTableLookups) { 5922 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n", 5923 NumMethodPoolTableHits, NumMethodPoolTableLookups, 5924 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups 5925 * 100.0)); 5926 } 5927 5928 if (NumIdentifierLookupHits) { 5929 std::fprintf(stderr, 5930 " %u / %u identifier table lookups succeeded (%f%%)\n", 5931 NumIdentifierLookupHits, NumIdentifierLookups, 5932 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups); 5933 } 5934 5935 if (GlobalIndex) { 5936 std::fprintf(stderr, "\n"); 5937 GlobalIndex->printStats(); 5938 } 5939 5940 std::fprintf(stderr, "\n"); 5941 dump(); 5942 std::fprintf(stderr, "\n"); 5943} 5944 5945template<typename Key, typename ModuleFile, unsigned InitialCapacity> 5946static void 5947dumpModuleIDMap(StringRef Name, 5948 const ContinuousRangeMap<Key, ModuleFile *, 5949 InitialCapacity> &Map) { 5950 if (Map.begin() == Map.end()) 5951 return; 5952 5953 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType; 5954 llvm::errs() << Name << ":\n"; 5955 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end(); 5956 I != IEnd; ++I) { 5957 llvm::errs() << " " << I->first << " -> " << I->second->FileName 5958 << "\n"; 5959 } 5960} 5961 5962void ASTReader::dump() { 5963 llvm::errs() << "*** PCH/ModuleFile Remappings:\n"; 5964 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap); 5965 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap); 5966 dumpModuleIDMap("Global type map", GlobalTypeMap); 5967 dumpModuleIDMap("Global declaration map", GlobalDeclMap); 5968 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap); 5969 dumpModuleIDMap("Global macro map", GlobalMacroMap); 5970 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap); 5971 dumpModuleIDMap("Global selector map", GlobalSelectorMap); 5972 dumpModuleIDMap("Global preprocessed entity map", 5973 GlobalPreprocessedEntityMap); 5974 5975 llvm::errs() << "\n*** PCH/Modules Loaded:"; 5976 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(), 5977 MEnd = ModuleMgr.end(); 5978 M != MEnd; ++M) 5979 (*M)->dump(); 5980} 5981 5982/// Return the amount of memory used by memory buffers, breaking down 5983/// by heap-backed versus mmap'ed memory. 5984void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const { 5985 for (ModuleConstIterator I = ModuleMgr.begin(), 5986 E = ModuleMgr.end(); I != E; ++I) { 5987 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) { 5988 size_t bytes = buf->getBufferSize(); 5989 switch (buf->getBufferKind()) { 5990 case llvm::MemoryBuffer::MemoryBuffer_Malloc: 5991 sizes.malloc_bytes += bytes; 5992 break; 5993 case llvm::MemoryBuffer::MemoryBuffer_MMap: 5994 sizes.mmap_bytes += bytes; 5995 break; 5996 } 5997 } 5998 } 5999} 6000 6001void ASTReader::InitializeSema(Sema &S) { 6002 SemaObj = &S; 6003 S.addExternalSource(this); 6004 6005 // Makes sure any declarations that were deserialized "too early" 6006 // still get added to the identifier's declaration chains. 6007 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) { 6008 NamedDecl *ND = cast<NamedDecl>(PreloadedDecls[I]->getMostRecentDecl()); 6009 SemaObj->pushExternalDeclIntoScope(ND, PreloadedDecls[I]->getDeclName()); 6010 } 6011 PreloadedDecls.clear(); 6012 6013 // Load the offsets of the declarations that Sema references. 6014 // They will be lazily deserialized when needed. 6015 if (!SemaDeclRefs.empty()) { 6016 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!"); 6017 if (!SemaObj->StdNamespace) 6018 SemaObj->StdNamespace = SemaDeclRefs[0]; 6019 if (!SemaObj->StdBadAlloc) 6020 SemaObj->StdBadAlloc = SemaDeclRefs[1]; 6021 } 6022 6023 if (!FPPragmaOptions.empty()) { 6024 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS"); 6025 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0]; 6026 } 6027 6028 if (!OpenCLExtensions.empty()) { 6029 unsigned I = 0; 6030#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++]; 6031#include "clang/Basic/OpenCLExtensions.def" 6032 6033 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS"); 6034 } 6035} 6036 6037IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) { 6038 // Note that we are loading an identifier. 6039 Deserializing AnIdentifier(this); 6040 StringRef Name(NameStart, NameEnd - NameStart); 6041 6042 // If there is a global index, look there first to determine which modules 6043 // provably do not have any results for this identifier. 6044 GlobalModuleIndex::HitSet Hits; 6045 GlobalModuleIndex::HitSet *HitsPtr = 0; 6046 if (!loadGlobalIndex()) { 6047 if (GlobalIndex->lookupIdentifier(Name, Hits)) { 6048 HitsPtr = &Hits; 6049 } 6050 } 6051 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0, 6052 NumIdentifierLookups, 6053 NumIdentifierLookupHits); 6054 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr); 6055 IdentifierInfo *II = Visitor.getIdentifierInfo(); 6056 markIdentifierUpToDate(II); 6057 return II; 6058} 6059 6060namespace clang { 6061 /// \brief An identifier-lookup iterator that enumerates all of the 6062 /// identifiers stored within a set of AST files. 6063 class ASTIdentifierIterator : public IdentifierIterator { 6064 /// \brief The AST reader whose identifiers are being enumerated. 6065 const ASTReader &Reader; 6066 6067 /// \brief The current index into the chain of AST files stored in 6068 /// the AST reader. 6069 unsigned Index; 6070 6071 /// \brief The current position within the identifier lookup table 6072 /// of the current AST file. 6073 ASTIdentifierLookupTable::key_iterator Current; 6074 6075 /// \brief The end position within the identifier lookup table of 6076 /// the current AST file. 6077 ASTIdentifierLookupTable::key_iterator End; 6078 6079 public: 6080 explicit ASTIdentifierIterator(const ASTReader &Reader); 6081 6082 virtual StringRef Next(); 6083 }; 6084} 6085 6086ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader) 6087 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) { 6088 ASTIdentifierLookupTable *IdTable 6089 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable; 6090 Current = IdTable->key_begin(); 6091 End = IdTable->key_end(); 6092} 6093 6094StringRef ASTIdentifierIterator::Next() { 6095 while (Current == End) { 6096 // If we have exhausted all of our AST files, we're done. 6097 if (Index == 0) 6098 return StringRef(); 6099 6100 --Index; 6101 ASTIdentifierLookupTable *IdTable 6102 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index]. 6103 IdentifierLookupTable; 6104 Current = IdTable->key_begin(); 6105 End = IdTable->key_end(); 6106 } 6107 6108 // We have any identifiers remaining in the current AST file; return 6109 // the next one. 6110 StringRef Result = *Current; 6111 ++Current; 6112 return Result; 6113} 6114 6115IdentifierIterator *ASTReader::getIdentifiers() { 6116 if (!loadGlobalIndex()) 6117 return GlobalIndex->createIdentifierIterator(); 6118 6119 return new ASTIdentifierIterator(*this); 6120} 6121 6122namespace clang { namespace serialization { 6123 class ReadMethodPoolVisitor { 6124 ASTReader &Reader; 6125 Selector Sel; 6126 unsigned PriorGeneration; 6127 unsigned InstanceBits; 6128 unsigned FactoryBits; 6129 SmallVector<ObjCMethodDecl *, 4> InstanceMethods; 6130 SmallVector<ObjCMethodDecl *, 4> FactoryMethods; 6131 6132 public: 6133 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel, 6134 unsigned PriorGeneration) 6135 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration), 6136 InstanceBits(0), FactoryBits(0) { } 6137 6138 static bool visit(ModuleFile &M, void *UserData) { 6139 ReadMethodPoolVisitor *This 6140 = static_cast<ReadMethodPoolVisitor *>(UserData); 6141 6142 if (!M.SelectorLookupTable) 6143 return false; 6144 6145 // If we've already searched this module file, skip it now. 6146 if (M.Generation <= This->PriorGeneration) 6147 return true; 6148 6149 ++This->Reader.NumMethodPoolTableLookups; 6150 ASTSelectorLookupTable *PoolTable 6151 = (ASTSelectorLookupTable*)M.SelectorLookupTable; 6152 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel); 6153 if (Pos == PoolTable->end()) 6154 return false; 6155 6156 ++This->Reader.NumMethodPoolTableHits; 6157 ++This->Reader.NumSelectorsRead; 6158 // FIXME: Not quite happy with the statistics here. We probably should 6159 // disable this tracking when called via LoadSelector. 6160 // Also, should entries without methods count as misses? 6161 ++This->Reader.NumMethodPoolEntriesRead; 6162 ASTSelectorLookupTrait::data_type Data = *Pos; 6163 if (This->Reader.DeserializationListener) 6164 This->Reader.DeserializationListener->SelectorRead(Data.ID, 6165 This->Sel); 6166 6167 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end()); 6168 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end()); 6169 This->InstanceBits = Data.InstanceBits; 6170 This->FactoryBits = Data.FactoryBits; 6171 return true; 6172 } 6173 6174 /// \brief Retrieve the instance methods found by this visitor. 6175 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const { 6176 return InstanceMethods; 6177 } 6178 6179 /// \brief Retrieve the instance methods found by this visitor. 6180 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const { 6181 return FactoryMethods; 6182 } 6183 6184 unsigned getInstanceBits() const { return InstanceBits; } 6185 unsigned getFactoryBits() const { return FactoryBits; } 6186 }; 6187} } // end namespace clang::serialization 6188 6189/// \brief Add the given set of methods to the method list. 6190static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods, 6191 ObjCMethodList &List) { 6192 for (unsigned I = 0, N = Methods.size(); I != N; ++I) { 6193 S.addMethodToGlobalList(&List, Methods[I]); 6194 } 6195} 6196 6197void ASTReader::ReadMethodPool(Selector Sel) { 6198 // Get the selector generation and update it to the current generation. 6199 unsigned &Generation = SelectorGeneration[Sel]; 6200 unsigned PriorGeneration = Generation; 6201 Generation = CurrentGeneration; 6202 6203 // Search for methods defined with this selector. 6204 ++NumMethodPoolLookups; 6205 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration); 6206 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor); 6207 6208 if (Visitor.getInstanceMethods().empty() && 6209 Visitor.getFactoryMethods().empty()) 6210 return; 6211 6212 ++NumMethodPoolHits; 6213 6214 if (!getSema()) 6215 return; 6216 6217 Sema &S = *getSema(); 6218 Sema::GlobalMethodPool::iterator Pos 6219 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first; 6220 6221 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first); 6222 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second); 6223 Pos->second.first.setBits(Visitor.getInstanceBits()); 6224 Pos->second.second.setBits(Visitor.getFactoryBits()); 6225} 6226 6227void ASTReader::ReadKnownNamespaces( 6228 SmallVectorImpl<NamespaceDecl *> &Namespaces) { 6229 Namespaces.clear(); 6230 6231 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) { 6232 if (NamespaceDecl *Namespace 6233 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I]))) 6234 Namespaces.push_back(Namespace); 6235 } 6236} 6237 6238void ASTReader::ReadUndefinedButUsed( 6239 llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) { 6240 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) { 6241 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++])); 6242 SourceLocation Loc = 6243 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]); 6244 Undefined.insert(std::make_pair(D, Loc)); 6245 } 6246} 6247 6248void ASTReader::ReadTentativeDefinitions( 6249 SmallVectorImpl<VarDecl *> &TentativeDefs) { 6250 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { 6251 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I])); 6252 if (Var) 6253 TentativeDefs.push_back(Var); 6254 } 6255 TentativeDefinitions.clear(); 6256} 6257 6258void ASTReader::ReadUnusedFileScopedDecls( 6259 SmallVectorImpl<const DeclaratorDecl *> &Decls) { 6260 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) { 6261 DeclaratorDecl *D 6262 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I])); 6263 if (D) 6264 Decls.push_back(D); 6265 } 6266 UnusedFileScopedDecls.clear(); 6267} 6268 6269void ASTReader::ReadDelegatingConstructors( 6270 SmallVectorImpl<CXXConstructorDecl *> &Decls) { 6271 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) { 6272 CXXConstructorDecl *D 6273 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I])); 6274 if (D) 6275 Decls.push_back(D); 6276 } 6277 DelegatingCtorDecls.clear(); 6278} 6279 6280void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) { 6281 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) { 6282 TypedefNameDecl *D 6283 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])); 6284 if (D) 6285 Decls.push_back(D); 6286 } 6287 ExtVectorDecls.clear(); 6288} 6289 6290void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) { 6291 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) { 6292 CXXRecordDecl *D 6293 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I])); 6294 if (D) 6295 Decls.push_back(D); 6296 } 6297 DynamicClasses.clear(); 6298} 6299 6300void 6301ASTReader::ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl *> &Decls) { 6302 for (unsigned I = 0, N = LocallyScopedExternCDecls.size(); I != N; ++I) { 6303 NamedDecl *D 6304 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternCDecls[I])); 6305 if (D) 6306 Decls.push_back(D); 6307 } 6308 LocallyScopedExternCDecls.clear(); 6309} 6310 6311void ASTReader::ReadReferencedSelectors( 6312 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) { 6313 if (ReferencedSelectorsData.empty()) 6314 return; 6315 6316 // If there are @selector references added them to its pool. This is for 6317 // implementation of -Wselector. 6318 unsigned int DataSize = ReferencedSelectorsData.size()-1; 6319 unsigned I = 0; 6320 while (I < DataSize) { 6321 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]); 6322 SourceLocation SelLoc 6323 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]); 6324 Sels.push_back(std::make_pair(Sel, SelLoc)); 6325 } 6326 ReferencedSelectorsData.clear(); 6327} 6328 6329void ASTReader::ReadWeakUndeclaredIdentifiers( 6330 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) { 6331 if (WeakUndeclaredIdentifiers.empty()) 6332 return; 6333 6334 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) { 6335 IdentifierInfo *WeakId 6336 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 6337 IdentifierInfo *AliasId 6338 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 6339 SourceLocation Loc 6340 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]); 6341 bool Used = WeakUndeclaredIdentifiers[I++]; 6342 WeakInfo WI(AliasId, Loc); 6343 WI.setUsed(Used); 6344 WeakIDs.push_back(std::make_pair(WeakId, WI)); 6345 } 6346 WeakUndeclaredIdentifiers.clear(); 6347} 6348 6349void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) { 6350 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) { 6351 ExternalVTableUse VT; 6352 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); 6353 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]); 6354 VT.DefinitionRequired = VTableUses[Idx++]; 6355 VTables.push_back(VT); 6356 } 6357 6358 VTableUses.clear(); 6359} 6360 6361void ASTReader::ReadPendingInstantiations( 6362 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) { 6363 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) { 6364 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++])); 6365 SourceLocation Loc 6366 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]); 6367 6368 Pending.push_back(std::make_pair(D, Loc)); 6369 } 6370 PendingInstantiations.clear(); 6371} 6372 6373void ASTReader::LoadSelector(Selector Sel) { 6374 // It would be complicated to avoid reading the methods anyway. So don't. 6375 ReadMethodPool(Sel); 6376} 6377 6378void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) { 6379 assert(ID && "Non-zero identifier ID required"); 6380 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); 6381 IdentifiersLoaded[ID - 1] = II; 6382 if (DeserializationListener) 6383 DeserializationListener->IdentifierRead(ID, II); 6384} 6385 6386/// \brief Set the globally-visible declarations associated with the given 6387/// identifier. 6388/// 6389/// If the AST reader is currently in a state where the given declaration IDs 6390/// cannot safely be resolved, they are queued until it is safe to resolve 6391/// them. 6392/// 6393/// \param II an IdentifierInfo that refers to one or more globally-visible 6394/// declarations. 6395/// 6396/// \param DeclIDs the set of declaration IDs with the name @p II that are 6397/// visible at global scope. 6398/// 6399/// \param Decls if non-null, this vector will be populated with the set of 6400/// deserialized declarations. These declarations will not be pushed into 6401/// scope. 6402void 6403ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, 6404 const SmallVectorImpl<uint32_t> &DeclIDs, 6405 SmallVectorImpl<Decl *> *Decls) { 6406 if (NumCurrentElementsDeserializing && !Decls) { 6407 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end()); 6408 return; 6409 } 6410 6411 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { 6412 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); 6413 if (SemaObj) { 6414 // If we're simply supposed to record the declarations, do so now. 6415 if (Decls) { 6416 Decls->push_back(D); 6417 continue; 6418 } 6419 6420 // Introduce this declaration into the translation-unit scope 6421 // and add it to the declaration chain for this identifier, so 6422 // that (unqualified) name lookup will find it. 6423 NamedDecl *ND = cast<NamedDecl>(D->getMostRecentDecl()); 6424 SemaObj->pushExternalDeclIntoScope(ND, II); 6425 } else { 6426 // Queue this declaration so that it will be added to the 6427 // translation unit scope and identifier's declaration chain 6428 // once a Sema object is known. 6429 PreloadedDecls.push_back(D); 6430 } 6431 } 6432} 6433 6434IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) { 6435 if (ID == 0) 6436 return 0; 6437 6438 if (IdentifiersLoaded.empty()) { 6439 Error("no identifier table in AST file"); 6440 return 0; 6441 } 6442 6443 ID -= 1; 6444 if (!IdentifiersLoaded[ID]) { 6445 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1); 6446 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map"); 6447 ModuleFile *M = I->second; 6448 unsigned Index = ID - M->BaseIdentifierID; 6449 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index]; 6450 6451 // All of the strings in the AST file are preceded by a 16-bit length. 6452 // Extract that 16-bit length to avoid having to execute strlen(). 6453 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as 6454 // unsigned integers. This is important to avoid integer overflow when 6455 // we cast them to 'unsigned'. 6456 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2; 6457 unsigned StrLen = (((unsigned) StrLenPtr[0]) 6458 | (((unsigned) StrLenPtr[1]) << 8)) - 1; 6459 IdentifiersLoaded[ID] 6460 = &PP.getIdentifierTable().get(StringRef(Str, StrLen)); 6461 if (DeserializationListener) 6462 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]); 6463 } 6464 6465 return IdentifiersLoaded[ID]; 6466} 6467 6468IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) { 6469 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID)); 6470} 6471 6472IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) { 6473 if (LocalID < NUM_PREDEF_IDENT_IDS) 6474 return LocalID; 6475 6476 ContinuousRangeMap<uint32_t, int, 2>::iterator I 6477 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS); 6478 assert(I != M.IdentifierRemap.end() 6479 && "Invalid index into identifier index remap"); 6480 6481 return LocalID + I->second; 6482} 6483 6484MacroInfo *ASTReader::getMacro(MacroID ID) { 6485 if (ID == 0) 6486 return 0; 6487 6488 if (MacrosLoaded.empty()) { 6489 Error("no macro table in AST file"); 6490 return 0; 6491 } 6492 6493 ID -= NUM_PREDEF_MACRO_IDS; 6494 if (!MacrosLoaded[ID]) { 6495 GlobalMacroMapType::iterator I 6496 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS); 6497 assert(I != GlobalMacroMap.end() && "Corrupted global macro map"); 6498 ModuleFile *M = I->second; 6499 unsigned Index = ID - M->BaseMacroID; 6500 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]); 6501 6502 if (DeserializationListener) 6503 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS, 6504 MacrosLoaded[ID]); 6505 } 6506 6507 return MacrosLoaded[ID]; 6508} 6509 6510MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) { 6511 if (LocalID < NUM_PREDEF_MACRO_IDS) 6512 return LocalID; 6513 6514 ContinuousRangeMap<uint32_t, int, 2>::iterator I 6515 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS); 6516 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap"); 6517 6518 return LocalID + I->second; 6519} 6520 6521serialization::SubmoduleID 6522ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) { 6523 if (LocalID < NUM_PREDEF_SUBMODULE_IDS) 6524 return LocalID; 6525 6526 ContinuousRangeMap<uint32_t, int, 2>::iterator I 6527 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS); 6528 assert(I != M.SubmoduleRemap.end() 6529 && "Invalid index into submodule index remap"); 6530 6531 return LocalID + I->second; 6532} 6533 6534Module *ASTReader::getSubmodule(SubmoduleID GlobalID) { 6535 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) { 6536 assert(GlobalID == 0 && "Unhandled global submodule ID"); 6537 return 0; 6538 } 6539 6540 if (GlobalID > SubmodulesLoaded.size()) { 6541 Error("submodule ID out of range in AST file"); 6542 return 0; 6543 } 6544 6545 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS]; 6546} 6547 6548Module *ASTReader::getModule(unsigned ID) { 6549 return getSubmodule(ID); 6550} 6551 6552Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) { 6553 return DecodeSelector(getGlobalSelectorID(M, LocalID)); 6554} 6555 6556Selector ASTReader::DecodeSelector(serialization::SelectorID ID) { 6557 if (ID == 0) 6558 return Selector(); 6559 6560 if (ID > SelectorsLoaded.size()) { 6561 Error("selector ID out of range in AST file"); 6562 return Selector(); 6563 } 6564 6565 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) { 6566 // Load this selector from the selector table. 6567 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID); 6568 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map"); 6569 ModuleFile &M = *I->second; 6570 ASTSelectorLookupTrait Trait(*this, M); 6571 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS; 6572 SelectorsLoaded[ID - 1] = 6573 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0); 6574 if (DeserializationListener) 6575 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); 6576 } 6577 6578 return SelectorsLoaded[ID - 1]; 6579} 6580 6581Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) { 6582 return DecodeSelector(ID); 6583} 6584 6585uint32_t ASTReader::GetNumExternalSelectors() { 6586 // ID 0 (the null selector) is considered an external selector. 6587 return getTotalNumSelectors() + 1; 6588} 6589 6590serialization::SelectorID 6591ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const { 6592 if (LocalID < NUM_PREDEF_SELECTOR_IDS) 6593 return LocalID; 6594 6595 ContinuousRangeMap<uint32_t, int, 2>::iterator I 6596 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS); 6597 assert(I != M.SelectorRemap.end() 6598 && "Invalid index into selector index remap"); 6599 6600 return LocalID + I->second; 6601} 6602 6603DeclarationName 6604ASTReader::ReadDeclarationName(ModuleFile &F, 6605 const RecordData &Record, unsigned &Idx) { 6606 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++]; 6607 switch (Kind) { 6608 case DeclarationName::Identifier: 6609 return DeclarationName(GetIdentifierInfo(F, Record, Idx)); 6610 6611 case DeclarationName::ObjCZeroArgSelector: 6612 case DeclarationName::ObjCOneArgSelector: 6613 case DeclarationName::ObjCMultiArgSelector: 6614 return DeclarationName(ReadSelector(F, Record, Idx)); 6615 6616 case DeclarationName::CXXConstructorName: 6617 return Context.DeclarationNames.getCXXConstructorName( 6618 Context.getCanonicalType(readType(F, Record, Idx))); 6619 6620 case DeclarationName::CXXDestructorName: 6621 return Context.DeclarationNames.getCXXDestructorName( 6622 Context.getCanonicalType(readType(F, Record, Idx))); 6623 6624 case DeclarationName::CXXConversionFunctionName: 6625 return Context.DeclarationNames.getCXXConversionFunctionName( 6626 Context.getCanonicalType(readType(F, Record, Idx))); 6627 6628 case DeclarationName::CXXOperatorName: 6629 return Context.DeclarationNames.getCXXOperatorName( 6630 (OverloadedOperatorKind)Record[Idx++]); 6631 6632 case DeclarationName::CXXLiteralOperatorName: 6633 return Context.DeclarationNames.getCXXLiteralOperatorName( 6634 GetIdentifierInfo(F, Record, Idx)); 6635 6636 case DeclarationName::CXXUsingDirective: 6637 return DeclarationName::getUsingDirectiveName(); 6638 } 6639 6640 llvm_unreachable("Invalid NameKind!"); 6641} 6642 6643void ASTReader::ReadDeclarationNameLoc(ModuleFile &F, 6644 DeclarationNameLoc &DNLoc, 6645 DeclarationName Name, 6646 const RecordData &Record, unsigned &Idx) { 6647 switch (Name.getNameKind()) { 6648 case DeclarationName::CXXConstructorName: 6649 case DeclarationName::CXXDestructorName: 6650 case DeclarationName::CXXConversionFunctionName: 6651 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx); 6652 break; 6653 6654 case DeclarationName::CXXOperatorName: 6655 DNLoc.CXXOperatorName.BeginOpNameLoc 6656 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 6657 DNLoc.CXXOperatorName.EndOpNameLoc 6658 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 6659 break; 6660 6661 case DeclarationName::CXXLiteralOperatorName: 6662 DNLoc.CXXLiteralOperatorName.OpNameLoc 6663 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 6664 break; 6665 6666 case DeclarationName::Identifier: 6667 case DeclarationName::ObjCZeroArgSelector: 6668 case DeclarationName::ObjCOneArgSelector: 6669 case DeclarationName::ObjCMultiArgSelector: 6670 case DeclarationName::CXXUsingDirective: 6671 break; 6672 } 6673} 6674 6675void ASTReader::ReadDeclarationNameInfo(ModuleFile &F, 6676 DeclarationNameInfo &NameInfo, 6677 const RecordData &Record, unsigned &Idx) { 6678 NameInfo.setName(ReadDeclarationName(F, Record, Idx)); 6679 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx)); 6680 DeclarationNameLoc DNLoc; 6681 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx); 6682 NameInfo.setInfo(DNLoc); 6683} 6684 6685void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info, 6686 const RecordData &Record, unsigned &Idx) { 6687 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx); 6688 unsigned NumTPLists = Record[Idx++]; 6689 Info.NumTemplParamLists = NumTPLists; 6690 if (NumTPLists) { 6691 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists]; 6692 for (unsigned i=0; i != NumTPLists; ++i) 6693 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx); 6694 } 6695} 6696 6697TemplateName 6698ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record, 6699 unsigned &Idx) { 6700 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++]; 6701 switch (Kind) { 6702 case TemplateName::Template: 6703 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx)); 6704 6705 case TemplateName::OverloadedTemplate: { 6706 unsigned size = Record[Idx++]; 6707 UnresolvedSet<8> Decls; 6708 while (size--) 6709 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx)); 6710 6711 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end()); 6712 } 6713 6714 case TemplateName::QualifiedTemplate: { 6715 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx); 6716 bool hasTemplKeyword = Record[Idx++]; 6717 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx); 6718 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template); 6719 } 6720 6721 case TemplateName::DependentTemplate: { 6722 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx); 6723 if (Record[Idx++]) // isIdentifier 6724 return Context.getDependentTemplateName(NNS, 6725 GetIdentifierInfo(F, Record, 6726 Idx)); 6727 return Context.getDependentTemplateName(NNS, 6728 (OverloadedOperatorKind)Record[Idx++]); 6729 } 6730 6731 case TemplateName::SubstTemplateTemplateParm: { 6732 TemplateTemplateParmDecl *param 6733 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx); 6734 if (!param) return TemplateName(); 6735 TemplateName replacement = ReadTemplateName(F, Record, Idx); 6736 return Context.getSubstTemplateTemplateParm(param, replacement); 6737 } 6738 6739 case TemplateName::SubstTemplateTemplateParmPack: { 6740 TemplateTemplateParmDecl *Param 6741 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx); 6742 if (!Param) 6743 return TemplateName(); 6744 6745 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx); 6746 if (ArgPack.getKind() != TemplateArgument::Pack) 6747 return TemplateName(); 6748 6749 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack); 6750 } 6751 } 6752 6753 llvm_unreachable("Unhandled template name kind!"); 6754} 6755 6756TemplateArgument 6757ASTReader::ReadTemplateArgument(ModuleFile &F, 6758 const RecordData &Record, unsigned &Idx) { 6759 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++]; 6760 switch (Kind) { 6761 case TemplateArgument::Null: 6762 return TemplateArgument(); 6763 case TemplateArgument::Type: 6764 return TemplateArgument(readType(F, Record, Idx)); 6765 case TemplateArgument::Declaration: { 6766 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx); 6767 bool ForReferenceParam = Record[Idx++]; 6768 return TemplateArgument(D, ForReferenceParam); 6769 } 6770 case TemplateArgument::NullPtr: 6771 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true); 6772 case TemplateArgument::Integral: { 6773 llvm::APSInt Value = ReadAPSInt(Record, Idx); 6774 QualType T = readType(F, Record, Idx); 6775 return TemplateArgument(Context, Value, T); 6776 } 6777 case TemplateArgument::Template: 6778 return TemplateArgument(ReadTemplateName(F, Record, Idx)); 6779 case TemplateArgument::TemplateExpansion: { 6780 TemplateName Name = ReadTemplateName(F, Record, Idx); 6781 Optional<unsigned> NumTemplateExpansions; 6782 if (unsigned NumExpansions = Record[Idx++]) 6783 NumTemplateExpansions = NumExpansions - 1; 6784 return TemplateArgument(Name, NumTemplateExpansions); 6785 } 6786 case TemplateArgument::Expression: 6787 return TemplateArgument(ReadExpr(F)); 6788 case TemplateArgument::Pack: { 6789 unsigned NumArgs = Record[Idx++]; 6790 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs]; 6791 for (unsigned I = 0; I != NumArgs; ++I) 6792 Args[I] = ReadTemplateArgument(F, Record, Idx); 6793 return TemplateArgument(Args, NumArgs); 6794 } 6795 } 6796 6797 llvm_unreachable("Unhandled template argument kind!"); 6798} 6799 6800TemplateParameterList * 6801ASTReader::ReadTemplateParameterList(ModuleFile &F, 6802 const RecordData &Record, unsigned &Idx) { 6803 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx); 6804 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx); 6805 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx); 6806 6807 unsigned NumParams = Record[Idx++]; 6808 SmallVector<NamedDecl *, 16> Params; 6809 Params.reserve(NumParams); 6810 while (NumParams--) 6811 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx)); 6812 6813 TemplateParameterList* TemplateParams = 6814 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, 6815 Params.data(), Params.size(), RAngleLoc); 6816 return TemplateParams; 6817} 6818 6819void 6820ASTReader:: 6821ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs, 6822 ModuleFile &F, const RecordData &Record, 6823 unsigned &Idx) { 6824 unsigned NumTemplateArgs = Record[Idx++]; 6825 TemplArgs.reserve(NumTemplateArgs); 6826 while (NumTemplateArgs--) 6827 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx)); 6828} 6829 6830/// \brief Read a UnresolvedSet structure. 6831void ASTReader::ReadUnresolvedSet(ModuleFile &F, ASTUnresolvedSet &Set, 6832 const RecordData &Record, unsigned &Idx) { 6833 unsigned NumDecls = Record[Idx++]; 6834 Set.reserve(Context, NumDecls); 6835 while (NumDecls--) { 6836 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx); 6837 AccessSpecifier AS = (AccessSpecifier)Record[Idx++]; 6838 Set.addDecl(Context, D, AS); 6839 } 6840} 6841 6842CXXBaseSpecifier 6843ASTReader::ReadCXXBaseSpecifier(ModuleFile &F, 6844 const RecordData &Record, unsigned &Idx) { 6845 bool isVirtual = static_cast<bool>(Record[Idx++]); 6846 bool isBaseOfClass = static_cast<bool>(Record[Idx++]); 6847 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]); 6848 bool inheritConstructors = static_cast<bool>(Record[Idx++]); 6849 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx); 6850 SourceRange Range = ReadSourceRange(F, Record, Idx); 6851 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx); 6852 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo, 6853 EllipsisLoc); 6854 Result.setInheritConstructors(inheritConstructors); 6855 return Result; 6856} 6857 6858std::pair<CXXCtorInitializer **, unsigned> 6859ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record, 6860 unsigned &Idx) { 6861 CXXCtorInitializer **CtorInitializers = 0; 6862 unsigned NumInitializers = Record[Idx++]; 6863 if (NumInitializers) { 6864 CtorInitializers 6865 = new (Context) CXXCtorInitializer*[NumInitializers]; 6866 for (unsigned i=0; i != NumInitializers; ++i) { 6867 TypeSourceInfo *TInfo = 0; 6868 bool IsBaseVirtual = false; 6869 FieldDecl *Member = 0; 6870 IndirectFieldDecl *IndirectMember = 0; 6871 6872 CtorInitializerType Type = (CtorInitializerType)Record[Idx++]; 6873 switch (Type) { 6874 case CTOR_INITIALIZER_BASE: 6875 TInfo = GetTypeSourceInfo(F, Record, Idx); 6876 IsBaseVirtual = Record[Idx++]; 6877 break; 6878 6879 case CTOR_INITIALIZER_DELEGATING: 6880 TInfo = GetTypeSourceInfo(F, Record, Idx); 6881 break; 6882 6883 case CTOR_INITIALIZER_MEMBER: 6884 Member = ReadDeclAs<FieldDecl>(F, Record, Idx); 6885 break; 6886 6887 case CTOR_INITIALIZER_INDIRECT_MEMBER: 6888 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx); 6889 break; 6890 } 6891 6892 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx); 6893 Expr *Init = ReadExpr(F); 6894 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx); 6895 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx); 6896 bool IsWritten = Record[Idx++]; 6897 unsigned SourceOrderOrNumArrayIndices; 6898 SmallVector<VarDecl *, 8> Indices; 6899 if (IsWritten) { 6900 SourceOrderOrNumArrayIndices = Record[Idx++]; 6901 } else { 6902 SourceOrderOrNumArrayIndices = Record[Idx++]; 6903 Indices.reserve(SourceOrderOrNumArrayIndices); 6904 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i) 6905 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx)); 6906 } 6907 6908 CXXCtorInitializer *BOMInit; 6909 if (Type == CTOR_INITIALIZER_BASE) { 6910 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual, 6911 LParenLoc, Init, RParenLoc, 6912 MemberOrEllipsisLoc); 6913 } else if (Type == CTOR_INITIALIZER_DELEGATING) { 6914 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc, 6915 Init, RParenLoc); 6916 } else if (IsWritten) { 6917 if (Member) 6918 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, 6919 LParenLoc, Init, RParenLoc); 6920 else 6921 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember, 6922 MemberOrEllipsisLoc, LParenLoc, 6923 Init, RParenLoc); 6924 } else { 6925 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc, 6926 LParenLoc, Init, RParenLoc, 6927 Indices.data(), Indices.size()); 6928 } 6929 6930 if (IsWritten) 6931 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices); 6932 CtorInitializers[i] = BOMInit; 6933 } 6934 } 6935 6936 return std::make_pair(CtorInitializers, NumInitializers); 6937} 6938 6939NestedNameSpecifier * 6940ASTReader::ReadNestedNameSpecifier(ModuleFile &F, 6941 const RecordData &Record, unsigned &Idx) { 6942 unsigned N = Record[Idx++]; 6943 NestedNameSpecifier *NNS = 0, *Prev = 0; 6944 for (unsigned I = 0; I != N; ++I) { 6945 NestedNameSpecifier::SpecifierKind Kind 6946 = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; 6947 switch (Kind) { 6948 case NestedNameSpecifier::Identifier: { 6949 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx); 6950 NNS = NestedNameSpecifier::Create(Context, Prev, II); 6951 break; 6952 } 6953 6954 case NestedNameSpecifier::Namespace: { 6955 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx); 6956 NNS = NestedNameSpecifier::Create(Context, Prev, NS); 6957 break; 6958 } 6959 6960 case NestedNameSpecifier::NamespaceAlias: { 6961 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx); 6962 NNS = NestedNameSpecifier::Create(Context, Prev, Alias); 6963 break; 6964 } 6965 6966 case NestedNameSpecifier::TypeSpec: 6967 case NestedNameSpecifier::TypeSpecWithTemplate: { 6968 const Type *T = readType(F, Record, Idx).getTypePtrOrNull(); 6969 if (!T) 6970 return 0; 6971 6972 bool Template = Record[Idx++]; 6973 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T); 6974 break; 6975 } 6976 6977 case NestedNameSpecifier::Global: { 6978 NNS = NestedNameSpecifier::GlobalSpecifier(Context); 6979 // No associated value, and there can't be a prefix. 6980 break; 6981 } 6982 } 6983 Prev = NNS; 6984 } 6985 return NNS; 6986} 6987 6988NestedNameSpecifierLoc 6989ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record, 6990 unsigned &Idx) { 6991 unsigned N = Record[Idx++]; 6992 NestedNameSpecifierLocBuilder Builder; 6993 for (unsigned I = 0; I != N; ++I) { 6994 NestedNameSpecifier::SpecifierKind Kind 6995 = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; 6996 switch (Kind) { 6997 case NestedNameSpecifier::Identifier: { 6998 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx); 6999 SourceRange Range = ReadSourceRange(F, Record, Idx); 7000 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd()); 7001 break; 7002 } 7003 7004 case NestedNameSpecifier::Namespace: { 7005 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx); 7006 SourceRange Range = ReadSourceRange(F, Record, Idx); 7007 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd()); 7008 break; 7009 } 7010 7011 case NestedNameSpecifier::NamespaceAlias: { 7012 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx); 7013 SourceRange Range = ReadSourceRange(F, Record, Idx); 7014 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd()); 7015 break; 7016 } 7017 7018 case NestedNameSpecifier::TypeSpec: 7019 case NestedNameSpecifier::TypeSpecWithTemplate: { 7020 bool Template = Record[Idx++]; 7021 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx); 7022 if (!T) 7023 return NestedNameSpecifierLoc(); 7024 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx); 7025 7026 // FIXME: 'template' keyword location not saved anywhere, so we fake it. 7027 Builder.Extend(Context, 7028 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(), 7029 T->getTypeLoc(), ColonColonLoc); 7030 break; 7031 } 7032 7033 case NestedNameSpecifier::Global: { 7034 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx); 7035 Builder.MakeGlobal(Context, ColonColonLoc); 7036 break; 7037 } 7038 } 7039 } 7040 7041 return Builder.getWithLocInContext(Context); 7042} 7043 7044SourceRange 7045ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record, 7046 unsigned &Idx) { 7047 SourceLocation beg = ReadSourceLocation(F, Record, Idx); 7048 SourceLocation end = ReadSourceLocation(F, Record, Idx); 7049 return SourceRange(beg, end); 7050} 7051 7052/// \brief Read an integral value 7053llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) { 7054 unsigned BitWidth = Record[Idx++]; 7055 unsigned NumWords = llvm::APInt::getNumWords(BitWidth); 7056 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]); 7057 Idx += NumWords; 7058 return Result; 7059} 7060 7061/// \brief Read a signed integral value 7062llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) { 7063 bool isUnsigned = Record[Idx++]; 7064 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned); 7065} 7066 7067/// \brief Read a floating-point value 7068llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, 7069 const llvm::fltSemantics &Sem, 7070 unsigned &Idx) { 7071 return llvm::APFloat(Sem, ReadAPInt(Record, Idx)); 7072} 7073 7074// \brief Read a string 7075std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) { 7076 unsigned Len = Record[Idx++]; 7077 std::string Result(Record.data() + Idx, Record.data() + Idx + Len); 7078 Idx += Len; 7079 return Result; 7080} 7081 7082VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record, 7083 unsigned &Idx) { 7084 unsigned Major = Record[Idx++]; 7085 unsigned Minor = Record[Idx++]; 7086 unsigned Subminor = Record[Idx++]; 7087 if (Minor == 0) 7088 return VersionTuple(Major); 7089 if (Subminor == 0) 7090 return VersionTuple(Major, Minor - 1); 7091 return VersionTuple(Major, Minor - 1, Subminor - 1); 7092} 7093 7094CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F, 7095 const RecordData &Record, 7096 unsigned &Idx) { 7097 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx); 7098 return CXXTemporary::Create(Context, Decl); 7099} 7100 7101DiagnosticBuilder ASTReader::Diag(unsigned DiagID) { 7102 return Diag(SourceLocation(), DiagID); 7103} 7104 7105DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) { 7106 return Diags.Report(Loc, DiagID); 7107} 7108 7109/// \brief Retrieve the identifier table associated with the 7110/// preprocessor. 7111IdentifierTable &ASTReader::getIdentifierTable() { 7112 return PP.getIdentifierTable(); 7113} 7114 7115/// \brief Record that the given ID maps to the given switch-case 7116/// statement. 7117void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { 7118 assert((*CurrSwitchCaseStmts)[ID] == 0 && 7119 "Already have a SwitchCase with this ID"); 7120 (*CurrSwitchCaseStmts)[ID] = SC; 7121} 7122 7123/// \brief Retrieve the switch-case statement with the given ID. 7124SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) { 7125 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID"); 7126 return (*CurrSwitchCaseStmts)[ID]; 7127} 7128 7129void ASTReader::ClearSwitchCaseIDs() { 7130 CurrSwitchCaseStmts->clear(); 7131} 7132 7133void ASTReader::ReadComments() { 7134 std::vector<RawComment *> Comments; 7135 for (SmallVectorImpl<std::pair<BitstreamCursor, 7136 serialization::ModuleFile *> >::iterator 7137 I = CommentsCursors.begin(), 7138 E = CommentsCursors.end(); 7139 I != E; ++I) { 7140 BitstreamCursor &Cursor = I->first; 7141 serialization::ModuleFile &F = *I->second; 7142 SavedStreamPosition SavedPosition(Cursor); 7143 7144 RecordData Record; 7145 while (true) { 7146 llvm::BitstreamEntry Entry = 7147 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd); 7148 7149 switch (Entry.Kind) { 7150 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 7151 case llvm::BitstreamEntry::Error: 7152 Error("malformed block record in AST file"); 7153 return; 7154 case llvm::BitstreamEntry::EndBlock: 7155 goto NextCursor; 7156 case llvm::BitstreamEntry::Record: 7157 // The interesting case. 7158 break; 7159 } 7160 7161 // Read a record. 7162 Record.clear(); 7163 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) { 7164 case COMMENTS_RAW_COMMENT: { 7165 unsigned Idx = 0; 7166 SourceRange SR = ReadSourceRange(F, Record, Idx); 7167 RawComment::CommentKind Kind = 7168 (RawComment::CommentKind) Record[Idx++]; 7169 bool IsTrailingComment = Record[Idx++]; 7170 bool IsAlmostTrailingComment = Record[Idx++]; 7171 Comments.push_back(new (Context) RawComment( 7172 SR, Kind, IsTrailingComment, IsAlmostTrailingComment, 7173 Context.getLangOpts().CommentOpts.ParseAllComments)); 7174 break; 7175 } 7176 } 7177 } 7178 NextCursor:; 7179 } 7180 Context.Comments.addCommentsToFront(Comments); 7181} 7182 7183void ASTReader::finishPendingActions() { 7184 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() || 7185 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty()) { 7186 // If any identifiers with corresponding top-level declarations have 7187 // been loaded, load those declarations now. 7188 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> > TopLevelDecls; 7189 while (!PendingIdentifierInfos.empty()) { 7190 // FIXME: std::move 7191 IdentifierInfo *II = PendingIdentifierInfos.back().first; 7192 SmallVector<uint32_t, 4> DeclIDs = PendingIdentifierInfos.back().second; 7193 PendingIdentifierInfos.pop_back(); 7194 7195 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]); 7196 } 7197 7198 // Load pending declaration chains. 7199 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) { 7200 loadPendingDeclChain(PendingDeclChains[I]); 7201 PendingDeclChainsKnown.erase(PendingDeclChains[I]); 7202 } 7203 PendingDeclChains.clear(); 7204 7205 // Make the most recent of the top-level declarations visible. 7206 for (llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >::iterator 7207 TLD = TopLevelDecls.begin(), TLDEnd = TopLevelDecls.end(); 7208 TLD != TLDEnd; ++TLD) { 7209 IdentifierInfo *II = TLD->first; 7210 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) { 7211 NamedDecl *ND = cast<NamedDecl>(TLD->second[I]->getMostRecentDecl()); 7212 SemaObj->pushExternalDeclIntoScope(ND, II); 7213 } 7214 } 7215 7216 // Load any pending macro definitions. 7217 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) { 7218 IdentifierInfo *II = PendingMacroIDs.begin()[I].first; 7219 SmallVector<PendingMacroInfo, 2> GlobalIDs; 7220 GlobalIDs.swap(PendingMacroIDs.begin()[I].second); 7221 // Initialize the macro history from chained-PCHs ahead of module imports. 7222 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 7223 ++IDIdx) { 7224 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 7225 if (Info.M->Kind != MK_Module) 7226 resolvePendingMacro(II, Info); 7227 } 7228 // Handle module imports. 7229 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 7230 ++IDIdx) { 7231 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 7232 if (Info.M->Kind == MK_Module) 7233 resolvePendingMacro(II, Info); 7234 } 7235 } 7236 PendingMacroIDs.clear(); 7237 7238 // Wire up the DeclContexts for Decls that we delayed setting until 7239 // recursive loading is completed. 7240 while (!PendingDeclContextInfos.empty()) { 7241 PendingDeclContextInfo Info = PendingDeclContextInfos.front(); 7242 PendingDeclContextInfos.pop_front(); 7243 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC)); 7244 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC)); 7245 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext()); 7246 } 7247 } 7248 7249 // If we deserialized any C++ or Objective-C class definitions, any 7250 // Objective-C protocol definitions, or any redeclarable templates, make sure 7251 // that all redeclarations point to the definitions. Note that this can only 7252 // happen now, after the redeclaration chains have been fully wired. 7253 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(), 7254 DEnd = PendingDefinitions.end(); 7255 D != DEnd; ++D) { 7256 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) { 7257 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) { 7258 // Make sure that the TagType points at the definition. 7259 const_cast<TagType*>(TagT)->decl = TD; 7260 } 7261 7262 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) { 7263 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(), 7264 REnd = RD->redecls_end(); 7265 R != REnd; ++R) 7266 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData; 7267 7268 } 7269 7270 continue; 7271 } 7272 7273 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) { 7274 // Make sure that the ObjCInterfaceType points at the definition. 7275 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl)) 7276 ->Decl = ID; 7277 7278 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(), 7279 REnd = ID->redecls_end(); 7280 R != REnd; ++R) 7281 R->Data = ID->Data; 7282 7283 continue; 7284 } 7285 7286 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) { 7287 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(), 7288 REnd = PD->redecls_end(); 7289 R != REnd; ++R) 7290 R->Data = PD->Data; 7291 7292 continue; 7293 } 7294 7295 RedeclarableTemplateDecl *RTD 7296 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl(); 7297 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(), 7298 REnd = RTD->redecls_end(); 7299 R != REnd; ++R) 7300 R->Common = RTD->Common; 7301 } 7302 PendingDefinitions.clear(); 7303 7304 // Load the bodies of any functions or methods we've encountered. We do 7305 // this now (delayed) so that we can be sure that the declaration chains 7306 // have been fully wired up. 7307 for (PendingBodiesMap::iterator PB = PendingBodies.begin(), 7308 PBEnd = PendingBodies.end(); 7309 PB != PBEnd; ++PB) { 7310 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) { 7311 // FIXME: Check for =delete/=default? 7312 // FIXME: Complain about ODR violations here? 7313 if (!getContext().getLangOpts().Modules || !FD->hasBody()) 7314 FD->setLazyBody(PB->second); 7315 continue; 7316 } 7317 7318 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first); 7319 if (!getContext().getLangOpts().Modules || !MD->hasBody()) 7320 MD->setLazyBody(PB->second); 7321 } 7322 PendingBodies.clear(); 7323} 7324 7325void ASTReader::FinishedDeserializing() { 7326 assert(NumCurrentElementsDeserializing && 7327 "FinishedDeserializing not paired with StartedDeserializing"); 7328 if (NumCurrentElementsDeserializing == 1) { 7329 // We decrease NumCurrentElementsDeserializing only after pending actions 7330 // are finished, to avoid recursively re-calling finishPendingActions(). 7331 finishPendingActions(); 7332 } 7333 --NumCurrentElementsDeserializing; 7334 7335 if (NumCurrentElementsDeserializing == 0 && 7336 Consumer && !PassingDeclsToConsumer) { 7337 // Guard variable to avoid recursively redoing the process of passing 7338 // decls to consumer. 7339 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer, 7340 true); 7341 7342 while (!InterestingDecls.empty()) { 7343 // We are not in recursive loading, so it's safe to pass the "interesting" 7344 // decls to the consumer. 7345 Decl *D = InterestingDecls.front(); 7346 InterestingDecls.pop_front(); 7347 PassInterestingDeclToConsumer(D); 7348 } 7349 } 7350} 7351 7352ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context, 7353 StringRef isysroot, bool DisableValidation, 7354 bool AllowASTWithCompilerErrors, bool UseGlobalIndex) 7355 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0), 7356 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), 7357 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context), 7358 Consumer(0), ModuleMgr(PP.getFileManager()), 7359 isysroot(isysroot), DisableValidation(DisableValidation), 7360 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors), 7361 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false), 7362 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts), 7363 NumSLocEntriesRead(0), TotalNumSLocEntries(0), 7364 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0), 7365 TotalNumMacros(0), NumIdentifierLookups(0), NumIdentifierLookupHits(0), 7366 NumSelectorsRead(0), NumMethodPoolEntriesRead(0), 7367 NumMethodPoolLookups(0), NumMethodPoolHits(0), 7368 NumMethodPoolTableLookups(0), NumMethodPoolTableHits(0), 7369 TotalNumMethodPoolEntries(0), 7370 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0), 7371 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0), 7372 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0), 7373 PassingDeclsToConsumer(false), 7374 NumCXXBaseSpecifiersLoaded(0) 7375{ 7376 SourceMgr.setExternalSLocEntrySource(this); 7377} 7378 7379ASTReader::~ASTReader() { 7380 for (DeclContextVisibleUpdatesPending::iterator 7381 I = PendingVisibleUpdates.begin(), 7382 E = PendingVisibleUpdates.end(); 7383 I != E; ++I) { 7384 for (DeclContextVisibleUpdates::iterator J = I->second.begin(), 7385 F = I->second.end(); 7386 J != F; ++J) 7387 delete J->first; 7388 } 7389} 7390