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