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