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