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