ASTReader.cpp revision 1c1508b37bc2a9419b2e22beef9e788eb74203f4
1bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch//===--- ASTReader.cpp - AST File Reader ----------------------------------===// 2bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch// 3bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch// The LLVM Compiler Infrastructure 4bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch// 5bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch// This file is distributed under the University of Illinois Open Source 6bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch// License. See LICENSE.TXT for details. 7bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch// 8bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch//===----------------------------------------------------------------------===// 9bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch// 10bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch// This file defines the ASTReader class, which reads AST files. 11bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch// 12bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch//===----------------------------------------------------------------------===// 13bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch 14bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch#include "clang/Serialization/ASTReader.h" 15bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch#include "ASTCommon.h" 16bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch#include "ASTReaderInternals.h" 17bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch#include "clang/AST/ASTConsumer.h" 18bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch#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 F.HeaderFileFrameworkStrings = Blob.data() + Record[2]; 2480 if (Record[0]) { 2481 F.HeaderFileInfoTable 2482 = HeaderFileInfoLookupTable::Create( 2483 (const unsigned char *)F.HeaderFileInfoTableData + Record[0], 2484 (const unsigned char *)F.HeaderFileInfoTableData, 2485 HeaderFileInfoTrait(*this, F, 2486 &PP.getHeaderSearchInfo(), 2487 Blob.data() + Record[2])); 2488 2489 PP.getHeaderSearchInfo().SetExternalSource(this); 2490 if (!PP.getHeaderSearchInfo().getExternalLookup()) 2491 PP.getHeaderSearchInfo().SetExternalLookup(this); 2492 } 2493 break; 2494 } 2495 2496 case FP_PRAGMA_OPTIONS: 2497 // Later tables overwrite earlier ones. 2498 FPPragmaOptions.swap(Record); 2499 break; 2500 2501 case OPENCL_EXTENSIONS: 2502 // Later tables overwrite earlier ones. 2503 OpenCLExtensions.swap(Record); 2504 break; 2505 2506 case TENTATIVE_DEFINITIONS: 2507 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2508 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I])); 2509 break; 2510 2511 case KNOWN_NAMESPACES: 2512 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2513 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I])); 2514 break; 2515 2516 case UNDEFINED_BUT_USED: 2517 if (UndefinedButUsed.size() % 2 != 0) { 2518 Error("Invalid existing UndefinedButUsed"); 2519 return true; 2520 } 2521 2522 if (Record.size() % 2 != 0) { 2523 Error("invalid undefined-but-used record"); 2524 return true; 2525 } 2526 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 2527 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++])); 2528 UndefinedButUsed.push_back( 2529 ReadSourceLocation(F, Record, I).getRawEncoding()); 2530 } 2531 break; 2532 2533 case IMPORTED_MODULES: { 2534 if (F.Kind != MK_Module) { 2535 // If we aren't loading a module (which has its own exports), make 2536 // all of the imported modules visible. 2537 // FIXME: Deal with macros-only imports. 2538 for (unsigned I = 0, N = Record.size(); I != N; ++I) { 2539 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I])) 2540 ImportedModules.push_back(GlobalID); 2541 } 2542 } 2543 break; 2544 } 2545 2546 case LOCAL_REDECLARATIONS: { 2547 F.RedeclarationChains.swap(Record); 2548 break; 2549 } 2550 2551 case LOCAL_REDECLARATIONS_MAP: { 2552 if (F.LocalNumRedeclarationsInMap != 0) { 2553 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file"); 2554 return true; 2555 } 2556 2557 F.LocalNumRedeclarationsInMap = Record[0]; 2558 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data(); 2559 break; 2560 } 2561 2562 case MERGED_DECLARATIONS: { 2563 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) { 2564 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]); 2565 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID]; 2566 for (unsigned N = Record[Idx++]; N > 0; --N) 2567 Decls.push_back(getGlobalDeclID(F, Record[Idx++])); 2568 } 2569 break; 2570 } 2571 2572 case MACRO_OFFSET: { 2573 if (F.LocalNumMacros != 0) { 2574 Error("duplicate MACRO_OFFSET record in AST file"); 2575 return true; 2576 } 2577 F.MacroOffsets = (const uint32_t *)Blob.data(); 2578 F.LocalNumMacros = Record[0]; 2579 unsigned LocalBaseMacroID = Record[1]; 2580 F.BaseMacroID = getTotalNumMacros(); 2581 2582 if (F.LocalNumMacros > 0) { 2583 // Introduce the global -> local mapping for macros within this module. 2584 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F)); 2585 2586 // Introduce the local -> global mapping for macros within this module. 2587 F.MacroRemap.insertOrReplace( 2588 std::make_pair(LocalBaseMacroID, 2589 F.BaseMacroID - LocalBaseMacroID)); 2590 2591 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros); 2592 } 2593 break; 2594 } 2595 2596 case MACRO_UPDATES: { 2597 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 2598 MacroID ID = getGlobalMacroID(F, Record[I++]); 2599 if (I == N) 2600 break; 2601 2602 SourceLocation UndefLoc = ReadSourceLocation(F, Record, I); 2603 SubmoduleID SubmoduleID = getGlobalSubmoduleID(F, Record[I++]);; 2604 MacroUpdate Update; 2605 Update.UndefLoc = UndefLoc; 2606 MacroUpdates[ID].push_back(std::make_pair(SubmoduleID, Update)); 2607 } 2608 break; 2609 } 2610 } 2611 } 2612} 2613 2614/// \brief Move the given method to the back of the global list of methods. 2615static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) { 2616 // Find the entry for this selector in the method pool. 2617 Sema::GlobalMethodPool::iterator Known 2618 = S.MethodPool.find(Method->getSelector()); 2619 if (Known == S.MethodPool.end()) 2620 return; 2621 2622 // Retrieve the appropriate method list. 2623 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first 2624 : Known->second.second; 2625 bool Found = false; 2626 for (ObjCMethodList *List = &Start; List; List = List->Next) { 2627 if (!Found) { 2628 if (List->Method == Method) { 2629 Found = true; 2630 } else { 2631 // Keep searching. 2632 continue; 2633 } 2634 } 2635 2636 if (List->Next) 2637 List->Method = List->Next->Method; 2638 else 2639 List->Method = Method; 2640 } 2641} 2642 2643void ASTReader::makeNamesVisible(const HiddenNames &Names) { 2644 for (unsigned I = 0, N = Names.size(); I != N; ++I) { 2645 switch (Names[I].getKind()) { 2646 case HiddenName::Declaration: { 2647 Decl *D = Names[I].getDecl(); 2648 bool wasHidden = D->Hidden; 2649 D->Hidden = false; 2650 2651 if (wasHidden && SemaObj) { 2652 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) { 2653 moveMethodToBackOfGlobalList(*SemaObj, Method); 2654 } 2655 } 2656 break; 2657 } 2658 case HiddenName::MacroVisibility: { 2659 std::pair<IdentifierInfo *, MacroDirective *> Macro = Names[I].getMacro(); 2660 Macro.second->setHidden(!Macro.second->isPublic()); 2661 if (Macro.second->isDefined()) { 2662 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second); 2663 } 2664 break; 2665 } 2666 2667 case HiddenName::MacroUndef: { 2668 std::pair<IdentifierInfo *, MacroDirective *> Macro = Names[I].getMacro(); 2669 if (Macro.second->isDefined()) { 2670 Macro.second->setUndefLoc(Names[I].getMacroUndefLoc()); 2671 if (PPMutationListener *Listener = PP.getPPMutationListener()) 2672 Listener->UndefinedMacro(Macro.second); 2673 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second); 2674 } 2675 break; 2676 } 2677 } 2678 } 2679} 2680 2681void ASTReader::makeModuleVisible(Module *Mod, 2682 Module::NameVisibilityKind NameVisibility, 2683 SourceLocation ImportLoc) { 2684 llvm::SmallPtrSet<Module *, 4> Visited; 2685 SmallVector<Module *, 4> Stack; 2686 Stack.push_back(Mod); 2687 while (!Stack.empty()) { 2688 Mod = Stack.back(); 2689 Stack.pop_back(); 2690 2691 if (NameVisibility <= Mod->NameVisibility) { 2692 // This module already has this level of visibility (or greater), so 2693 // there is nothing more to do. 2694 continue; 2695 } 2696 2697 if (!Mod->isAvailable()) { 2698 // Modules that aren't available cannot be made visible. 2699 continue; 2700 } 2701 2702 // Update the module's name visibility. 2703 Mod->NameVisibility = NameVisibility; 2704 2705 // If we've already deserialized any names from this module, 2706 // mark them as visible. 2707 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod); 2708 if (Hidden != HiddenNamesMap.end()) { 2709 makeNamesVisible(Hidden->second); 2710 HiddenNamesMap.erase(Hidden); 2711 } 2712 2713 // Push any non-explicit submodules onto the stack to be marked as 2714 // visible. 2715 for (Module::submodule_iterator Sub = Mod->submodule_begin(), 2716 SubEnd = Mod->submodule_end(); 2717 Sub != SubEnd; ++Sub) { 2718 if (!(*Sub)->IsExplicit && Visited.insert(*Sub)) 2719 Stack.push_back(*Sub); 2720 } 2721 2722 // Push any exported modules onto the stack to be marked as visible. 2723 SmallVector<Module *, 16> Exports; 2724 Mod->getExportedModules(Exports); 2725 for (SmallVectorImpl<Module *>::iterator 2726 I = Exports.begin(), E = Exports.end(); I != E; ++I) { 2727 Module *Exported = *I; 2728 if (Visited.insert(Exported)) 2729 Stack.push_back(Exported); 2730 } 2731 } 2732} 2733 2734bool ASTReader::loadGlobalIndex() { 2735 if (GlobalIndex) 2736 return false; 2737 2738 if (TriedLoadingGlobalIndex || !UseGlobalIndex || 2739 !Context.getLangOpts().Modules) 2740 return true; 2741 2742 // Try to load the global index. 2743 TriedLoadingGlobalIndex = true; 2744 StringRef ModuleCachePath 2745 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath(); 2746 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result 2747 = GlobalModuleIndex::readIndex(FileMgr, ModuleCachePath); 2748 if (!Result.first) 2749 return true; 2750 2751 GlobalIndex.reset(Result.first); 2752 ModuleMgr.setGlobalIndex(GlobalIndex.get()); 2753 return false; 2754} 2755 2756bool ASTReader::isGlobalIndexUnavailable() const { 2757 return Context.getLangOpts().Modules && UseGlobalIndex && 2758 !hasGlobalIndex() && TriedLoadingGlobalIndex; 2759} 2760 2761ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName, 2762 ModuleKind Type, 2763 SourceLocation ImportLoc, 2764 unsigned ClientLoadCapabilities) { 2765 // Bump the generation number. 2766 unsigned PreviousGeneration = CurrentGeneration++; 2767 2768 unsigned NumModules = ModuleMgr.size(); 2769 SmallVector<ImportedModule, 4> Loaded; 2770 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc, 2771 /*ImportedBy=*/0, Loaded, 2772 ClientLoadCapabilities)) { 2773 case Failure: 2774 case OutOfDate: 2775 case VersionMismatch: 2776 case ConfigurationMismatch: 2777 case HadErrors: 2778 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end()); 2779 2780 // If we find that any modules are unusable, the global index is going 2781 // to be out-of-date. Just remove it. 2782 GlobalIndex.reset(); 2783 ModuleMgr.setGlobalIndex(0); 2784 return ReadResult; 2785 2786 case Success: 2787 break; 2788 } 2789 2790 // Here comes stuff that we only do once the entire chain is loaded. 2791 2792 // Load the AST blocks of all of the modules that we loaded. 2793 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(), 2794 MEnd = Loaded.end(); 2795 M != MEnd; ++M) { 2796 ModuleFile &F = *M->Mod; 2797 2798 // Read the AST block. 2799 if (ReadASTBlock(F)) 2800 return Failure; 2801 2802 // Once read, set the ModuleFile bit base offset and update the size in 2803 // bits of all files we've seen. 2804 F.GlobalBitOffset = TotalModulesSizeInBits; 2805 TotalModulesSizeInBits += F.SizeInBits; 2806 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F)); 2807 2808 // Preload SLocEntries. 2809 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) { 2810 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID; 2811 // Load it through the SourceManager and don't call ReadSLocEntry() 2812 // directly because the entry may have already been loaded in which case 2813 // calling ReadSLocEntry() directly would trigger an assertion in 2814 // SourceManager. 2815 SourceMgr.getLoadedSLocEntryByID(Index); 2816 } 2817 } 2818 2819 // Setup the import locations. 2820 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(), 2821 MEnd = Loaded.end(); 2822 M != MEnd; ++M) { 2823 ModuleFile &F = *M->Mod; 2824 F.DirectImportLoc = ImportLoc; 2825 if (!M->ImportedBy) 2826 F.ImportLoc = M->ImportLoc; 2827 else 2828 F.ImportLoc = ReadSourceLocation(*M->ImportedBy, 2829 M->ImportLoc.getRawEncoding()); 2830 } 2831 2832 // Mark all of the identifiers in the identifier table as being out of date, 2833 // so that various accessors know to check the loaded modules when the 2834 // identifier is used. 2835 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(), 2836 IdEnd = PP.getIdentifierTable().end(); 2837 Id != IdEnd; ++Id) 2838 Id->second->setOutOfDate(true); 2839 2840 // Resolve any unresolved module exports. 2841 for (unsigned I = 0, N = UnresolvedModuleImportExports.size(); I != N; ++I) { 2842 UnresolvedModuleImportExport &Unresolved = UnresolvedModuleImportExports[I]; 2843 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID); 2844 Module *ResolvedMod = getSubmodule(GlobalID); 2845 2846 if (Unresolved.IsImport) { 2847 if (ResolvedMod) 2848 Unresolved.Mod->Imports.push_back(ResolvedMod); 2849 continue; 2850 } 2851 2852 if (ResolvedMod || Unresolved.IsWildcard) 2853 Unresolved.Mod->Exports.push_back( 2854 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard)); 2855 } 2856 UnresolvedModuleImportExports.clear(); 2857 2858 InitializeContext(); 2859 2860 if (DeserializationListener) 2861 DeserializationListener->ReaderInitialized(this); 2862 2863 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule(); 2864 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) { 2865 PrimaryModule.OriginalSourceFileID 2866 = FileID::get(PrimaryModule.SLocEntryBaseID 2867 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1); 2868 2869 // If this AST file is a precompiled preamble, then set the 2870 // preamble file ID of the source manager to the file source file 2871 // from which the preamble was built. 2872 if (Type == MK_Preamble) { 2873 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID); 2874 } else if (Type == MK_MainFile) { 2875 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID); 2876 } 2877 } 2878 2879 // For any Objective-C class definitions we have already loaded, make sure 2880 // that we load any additional categories. 2881 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) { 2882 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(), 2883 ObjCClassesLoaded[I], 2884 PreviousGeneration); 2885 } 2886 2887 return Success; 2888} 2889 2890ASTReader::ASTReadResult 2891ASTReader::ReadASTCore(StringRef FileName, 2892 ModuleKind Type, 2893 SourceLocation ImportLoc, 2894 ModuleFile *ImportedBy, 2895 SmallVectorImpl<ImportedModule> &Loaded, 2896 unsigned ClientLoadCapabilities) { 2897 ModuleFile *M; 2898 bool NewModule; 2899 std::string ErrorStr; 2900 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportLoc, 2901 ImportedBy, CurrentGeneration, 2902 ErrorStr); 2903 2904 if (!M) { 2905 // We couldn't load the module. 2906 std::string Msg = "Unable to load module \"" + FileName.str() + "\": " 2907 + ErrorStr; 2908 Error(Msg); 2909 return Failure; 2910 } 2911 2912 if (!NewModule) { 2913 // We've already loaded this module. 2914 return Success; 2915 } 2916 2917 // FIXME: This seems rather a hack. Should CurrentDir be part of the 2918 // module? 2919 if (FileName != "-") { 2920 CurrentDir = llvm::sys::path::parent_path(FileName); 2921 if (CurrentDir.empty()) CurrentDir = "."; 2922 } 2923 2924 ModuleFile &F = *M; 2925 BitstreamCursor &Stream = F.Stream; 2926 Stream.init(F.StreamFile); 2927 F.SizeInBits = F.Buffer->getBufferSize() * 8; 2928 2929 // Sniff for the signature. 2930 if (Stream.Read(8) != 'C' || 2931 Stream.Read(8) != 'P' || 2932 Stream.Read(8) != 'C' || 2933 Stream.Read(8) != 'H') { 2934 Diag(diag::err_not_a_pch_file) << FileName; 2935 return Failure; 2936 } 2937 2938 // This is used for compatibility with older PCH formats. 2939 bool HaveReadControlBlock = false; 2940 2941 while (1) { 2942 llvm::BitstreamEntry Entry = Stream.advance(); 2943 2944 switch (Entry.Kind) { 2945 case llvm::BitstreamEntry::Error: 2946 case llvm::BitstreamEntry::EndBlock: 2947 case llvm::BitstreamEntry::Record: 2948 Error("invalid record at top-level of AST file"); 2949 return Failure; 2950 2951 case llvm::BitstreamEntry::SubBlock: 2952 break; 2953 } 2954 2955 // We only know the control subblock ID. 2956 switch (Entry.ID) { 2957 case llvm::bitc::BLOCKINFO_BLOCK_ID: 2958 if (Stream.ReadBlockInfoBlock()) { 2959 Error("malformed BlockInfoBlock in AST file"); 2960 return Failure; 2961 } 2962 break; 2963 case CONTROL_BLOCK_ID: 2964 HaveReadControlBlock = true; 2965 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) { 2966 case Success: 2967 break; 2968 2969 case Failure: return Failure; 2970 case OutOfDate: return OutOfDate; 2971 case VersionMismatch: return VersionMismatch; 2972 case ConfigurationMismatch: return ConfigurationMismatch; 2973 case HadErrors: return HadErrors; 2974 } 2975 break; 2976 case AST_BLOCK_ID: 2977 if (!HaveReadControlBlock) { 2978 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2979 Diag(diag::warn_pch_version_too_old); 2980 return VersionMismatch; 2981 } 2982 2983 // Record that we've loaded this module. 2984 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc)); 2985 return Success; 2986 2987 default: 2988 if (Stream.SkipBlock()) { 2989 Error("malformed block record in AST file"); 2990 return Failure; 2991 } 2992 break; 2993 } 2994 } 2995 2996 return Success; 2997} 2998 2999void ASTReader::InitializeContext() { 3000 // If there's a listener, notify them that we "read" the translation unit. 3001 if (DeserializationListener) 3002 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, 3003 Context.getTranslationUnitDecl()); 3004 3005 // Make sure we load the declaration update records for the translation unit, 3006 // if there are any. 3007 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID, 3008 Context.getTranslationUnitDecl()); 3009 3010 // FIXME: Find a better way to deal with collisions between these 3011 // built-in types. Right now, we just ignore the problem. 3012 3013 // Load the special types. 3014 if (SpecialTypes.size() >= NumSpecialTypeIDs) { 3015 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) { 3016 if (!Context.CFConstantStringTypeDecl) 3017 Context.setCFConstantStringType(GetType(String)); 3018 } 3019 3020 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) { 3021 QualType FileType = GetType(File); 3022 if (FileType.isNull()) { 3023 Error("FILE type is NULL"); 3024 return; 3025 } 3026 3027 if (!Context.FILEDecl) { 3028 if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) 3029 Context.setFILEDecl(Typedef->getDecl()); 3030 else { 3031 const TagType *Tag = FileType->getAs<TagType>(); 3032 if (!Tag) { 3033 Error("Invalid FILE type in AST file"); 3034 return; 3035 } 3036 Context.setFILEDecl(Tag->getDecl()); 3037 } 3038 } 3039 } 3040 3041 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) { 3042 QualType Jmp_bufType = GetType(Jmp_buf); 3043 if (Jmp_bufType.isNull()) { 3044 Error("jmp_buf type is NULL"); 3045 return; 3046 } 3047 3048 if (!Context.jmp_bufDecl) { 3049 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) 3050 Context.setjmp_bufDecl(Typedef->getDecl()); 3051 else { 3052 const TagType *Tag = Jmp_bufType->getAs<TagType>(); 3053 if (!Tag) { 3054 Error("Invalid jmp_buf type in AST file"); 3055 return; 3056 } 3057 Context.setjmp_bufDecl(Tag->getDecl()); 3058 } 3059 } 3060 } 3061 3062 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) { 3063 QualType Sigjmp_bufType = GetType(Sigjmp_buf); 3064 if (Sigjmp_bufType.isNull()) { 3065 Error("sigjmp_buf type is NULL"); 3066 return; 3067 } 3068 3069 if (!Context.sigjmp_bufDecl) { 3070 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) 3071 Context.setsigjmp_bufDecl(Typedef->getDecl()); 3072 else { 3073 const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); 3074 assert(Tag && "Invalid sigjmp_buf type in AST file"); 3075 Context.setsigjmp_bufDecl(Tag->getDecl()); 3076 } 3077 } 3078 } 3079 3080 if (unsigned ObjCIdRedef 3081 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) { 3082 if (Context.ObjCIdRedefinitionType.isNull()) 3083 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef); 3084 } 3085 3086 if (unsigned ObjCClassRedef 3087 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) { 3088 if (Context.ObjCClassRedefinitionType.isNull()) 3089 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef); 3090 } 3091 3092 if (unsigned ObjCSelRedef 3093 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) { 3094 if (Context.ObjCSelRedefinitionType.isNull()) 3095 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef); 3096 } 3097 3098 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) { 3099 QualType Ucontext_tType = GetType(Ucontext_t); 3100 if (Ucontext_tType.isNull()) { 3101 Error("ucontext_t type is NULL"); 3102 return; 3103 } 3104 3105 if (!Context.ucontext_tDecl) { 3106 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>()) 3107 Context.setucontext_tDecl(Typedef->getDecl()); 3108 else { 3109 const TagType *Tag = Ucontext_tType->getAs<TagType>(); 3110 assert(Tag && "Invalid ucontext_t type in AST file"); 3111 Context.setucontext_tDecl(Tag->getDecl()); 3112 } 3113 } 3114 } 3115 } 3116 3117 ReadPragmaDiagnosticMappings(Context.getDiagnostics()); 3118 3119 // If there were any CUDA special declarations, deserialize them. 3120 if (!CUDASpecialDeclRefs.empty()) { 3121 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!"); 3122 Context.setcudaConfigureCallDecl( 3123 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0]))); 3124 } 3125 3126 // Re-export any modules that were imported by a non-module AST file. 3127 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) { 3128 if (Module *Imported = getSubmodule(ImportedModules[I])) 3129 makeModuleVisible(Imported, Module::AllVisible, 3130 /*ImportLoc=*/SourceLocation()); 3131 } 3132 ImportedModules.clear(); 3133} 3134 3135void ASTReader::finalizeForWriting() { 3136 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(), 3137 HiddenEnd = HiddenNamesMap.end(); 3138 Hidden != HiddenEnd; ++Hidden) { 3139 makeNamesVisible(Hidden->second); 3140 } 3141 HiddenNamesMap.clear(); 3142} 3143 3144/// SkipCursorToControlBlock - Given a cursor at the start of an AST file, scan 3145/// ahead and drop the cursor into the start of the CONTROL_BLOCK, returning 3146/// false on success and true on failure. 3147static bool SkipCursorToControlBlock(BitstreamCursor &Cursor) { 3148 while (1) { 3149 llvm::BitstreamEntry Entry = Cursor.advance(); 3150 switch (Entry.Kind) { 3151 case llvm::BitstreamEntry::Error: 3152 case llvm::BitstreamEntry::EndBlock: 3153 return true; 3154 3155 case llvm::BitstreamEntry::Record: 3156 // Ignore top-level records. 3157 Cursor.skipRecord(Entry.ID); 3158 break; 3159 3160 case llvm::BitstreamEntry::SubBlock: 3161 if (Entry.ID == CONTROL_BLOCK_ID) { 3162 if (Cursor.EnterSubBlock(CONTROL_BLOCK_ID)) 3163 return true; 3164 // Found it! 3165 return false; 3166 } 3167 3168 if (Cursor.SkipBlock()) 3169 return true; 3170 } 3171 } 3172} 3173 3174/// \brief Retrieve the name of the original source file name 3175/// directly from the AST file, without actually loading the AST 3176/// file. 3177std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName, 3178 FileManager &FileMgr, 3179 DiagnosticsEngine &Diags) { 3180 // Open the AST file. 3181 std::string ErrStr; 3182 OwningPtr<llvm::MemoryBuffer> Buffer; 3183 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr)); 3184 if (!Buffer) { 3185 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr; 3186 return std::string(); 3187 } 3188 3189 // Initialize the stream 3190 llvm::BitstreamReader StreamFile; 3191 BitstreamCursor Stream; 3192 StreamFile.init((const unsigned char *)Buffer->getBufferStart(), 3193 (const unsigned char *)Buffer->getBufferEnd()); 3194 Stream.init(StreamFile); 3195 3196 // Sniff for the signature. 3197 if (Stream.Read(8) != 'C' || 3198 Stream.Read(8) != 'P' || 3199 Stream.Read(8) != 'C' || 3200 Stream.Read(8) != 'H') { 3201 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName; 3202 return std::string(); 3203 } 3204 3205 // Scan for the CONTROL_BLOCK_ID block. 3206 if (SkipCursorToControlBlock(Stream)) { 3207 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 3208 return std::string(); 3209 } 3210 3211 // Scan for ORIGINAL_FILE inside the control block. 3212 RecordData Record; 3213 while (1) { 3214 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 3215 if (Entry.Kind == llvm::BitstreamEntry::EndBlock) 3216 return std::string(); 3217 3218 if (Entry.Kind != llvm::BitstreamEntry::Record) { 3219 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 3220 return std::string(); 3221 } 3222 3223 Record.clear(); 3224 StringRef Blob; 3225 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE) 3226 return Blob.str(); 3227 } 3228} 3229 3230namespace { 3231 class SimplePCHValidator : public ASTReaderListener { 3232 const LangOptions &ExistingLangOpts; 3233 const TargetOptions &ExistingTargetOpts; 3234 const PreprocessorOptions &ExistingPPOpts; 3235 FileManager &FileMgr; 3236 3237 public: 3238 SimplePCHValidator(const LangOptions &ExistingLangOpts, 3239 const TargetOptions &ExistingTargetOpts, 3240 const PreprocessorOptions &ExistingPPOpts, 3241 FileManager &FileMgr) 3242 : ExistingLangOpts(ExistingLangOpts), 3243 ExistingTargetOpts(ExistingTargetOpts), 3244 ExistingPPOpts(ExistingPPOpts), 3245 FileMgr(FileMgr) 3246 { 3247 } 3248 3249 virtual bool ReadLanguageOptions(const LangOptions &LangOpts, 3250 bool Complain) { 3251 return checkLanguageOptions(ExistingLangOpts, LangOpts, 0); 3252 } 3253 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts, 3254 bool Complain) { 3255 return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0); 3256 } 3257 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 3258 bool Complain, 3259 std::string &SuggestedPredefines) { 3260 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, 0, FileMgr, 3261 SuggestedPredefines); 3262 } 3263 }; 3264} 3265 3266bool ASTReader::readASTFileControlBlock(StringRef Filename, 3267 FileManager &FileMgr, 3268 ASTReaderListener &Listener) { 3269 // Open the AST file. 3270 std::string ErrStr; 3271 OwningPtr<llvm::MemoryBuffer> Buffer; 3272 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr)); 3273 if (!Buffer) { 3274 return true; 3275 } 3276 3277 // Initialize the stream 3278 llvm::BitstreamReader StreamFile; 3279 BitstreamCursor Stream; 3280 StreamFile.init((const unsigned char *)Buffer->getBufferStart(), 3281 (const unsigned char *)Buffer->getBufferEnd()); 3282 Stream.init(StreamFile); 3283 3284 // Sniff for the signature. 3285 if (Stream.Read(8) != 'C' || 3286 Stream.Read(8) != 'P' || 3287 Stream.Read(8) != 'C' || 3288 Stream.Read(8) != 'H') { 3289 return true; 3290 } 3291 3292 // Scan for the CONTROL_BLOCK_ID block. 3293 if (SkipCursorToControlBlock(Stream)) 3294 return true; 3295 3296 // Scan for ORIGINAL_FILE inside the control block. 3297 RecordData Record; 3298 while (1) { 3299 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 3300 if (Entry.Kind == llvm::BitstreamEntry::EndBlock) 3301 return false; 3302 3303 if (Entry.Kind != llvm::BitstreamEntry::Record) 3304 return true; 3305 3306 Record.clear(); 3307 StringRef Blob; 3308 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob); 3309 switch ((ControlRecordTypes)RecCode) { 3310 case METADATA: { 3311 if (Record[0] != VERSION_MAJOR) 3312 return true; 3313 3314 const std::string &CurBranch = getClangFullRepositoryVersion(); 3315 if (StringRef(CurBranch) != Blob) 3316 return true; 3317 3318 break; 3319 } 3320 case LANGUAGE_OPTIONS: 3321 if (ParseLanguageOptions(Record, false, Listener)) 3322 return true; 3323 break; 3324 3325 case TARGET_OPTIONS: 3326 if (ParseTargetOptions(Record, false, Listener)) 3327 return true; 3328 break; 3329 3330 case DIAGNOSTIC_OPTIONS: 3331 if (ParseDiagnosticOptions(Record, false, Listener)) 3332 return true; 3333 break; 3334 3335 case FILE_SYSTEM_OPTIONS: 3336 if (ParseFileSystemOptions(Record, false, Listener)) 3337 return true; 3338 break; 3339 3340 case HEADER_SEARCH_OPTIONS: 3341 if (ParseHeaderSearchOptions(Record, false, Listener)) 3342 return true; 3343 break; 3344 3345 case PREPROCESSOR_OPTIONS: { 3346 std::string IgnoredSuggestedPredefines; 3347 if (ParsePreprocessorOptions(Record, false, Listener, 3348 IgnoredSuggestedPredefines)) 3349 return true; 3350 break; 3351 } 3352 3353 default: 3354 // No other validation to perform. 3355 break; 3356 } 3357 } 3358} 3359 3360 3361bool ASTReader::isAcceptableASTFile(StringRef Filename, 3362 FileManager &FileMgr, 3363 const LangOptions &LangOpts, 3364 const TargetOptions &TargetOpts, 3365 const PreprocessorOptions &PPOpts) { 3366 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr); 3367 return !readASTFileControlBlock(Filename, FileMgr, validator); 3368} 3369 3370bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) { 3371 // Enter the submodule block. 3372 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) { 3373 Error("malformed submodule block record in AST file"); 3374 return true; 3375 } 3376 3377 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap(); 3378 bool First = true; 3379 Module *CurrentModule = 0; 3380 RecordData Record; 3381 while (true) { 3382 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks(); 3383 3384 switch (Entry.Kind) { 3385 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 3386 case llvm::BitstreamEntry::Error: 3387 Error("malformed block record in AST file"); 3388 return true; 3389 case llvm::BitstreamEntry::EndBlock: 3390 return false; 3391 case llvm::BitstreamEntry::Record: 3392 // The interesting case. 3393 break; 3394 } 3395 3396 // Read a record. 3397 StringRef Blob; 3398 Record.clear(); 3399 switch (F.Stream.readRecord(Entry.ID, Record, &Blob)) { 3400 default: // Default behavior: ignore. 3401 break; 3402 3403 case SUBMODULE_DEFINITION: { 3404 if (First) { 3405 Error("missing submodule metadata record at beginning of block"); 3406 return true; 3407 } 3408 3409 if (Record.size() < 7) { 3410 Error("malformed module definition"); 3411 return true; 3412 } 3413 3414 StringRef Name = Blob; 3415 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]); 3416 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]); 3417 bool IsFramework = Record[2]; 3418 bool IsExplicit = Record[3]; 3419 bool IsSystem = Record[4]; 3420 bool InferSubmodules = Record[5]; 3421 bool InferExplicitSubmodules = Record[6]; 3422 bool InferExportWildcard = Record[7]; 3423 3424 Module *ParentModule = 0; 3425 if (Parent) 3426 ParentModule = getSubmodule(Parent); 3427 3428 // Retrieve this (sub)module from the module map, creating it if 3429 // necessary. 3430 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule, 3431 IsFramework, 3432 IsExplicit).first; 3433 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS; 3434 if (GlobalIndex >= SubmodulesLoaded.size() || 3435 SubmodulesLoaded[GlobalIndex]) { 3436 Error("too many submodules"); 3437 return true; 3438 } 3439 3440 if (const FileEntry *CurFile = CurrentModule->getASTFile()) { 3441 if (CurFile != F.File) { 3442 if (!Diags.isDiagnosticInFlight()) { 3443 Diag(diag::err_module_file_conflict) 3444 << CurrentModule->getTopLevelModuleName() 3445 << CurFile->getName() 3446 << F.File->getName(); 3447 } 3448 return true; 3449 } 3450 } 3451 CurrentModule->setASTFile(F.File); 3452 CurrentModule->IsFromModuleFile = true; 3453 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem; 3454 CurrentModule->InferSubmodules = InferSubmodules; 3455 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules; 3456 CurrentModule->InferExportWildcard = InferExportWildcard; 3457 if (DeserializationListener) 3458 DeserializationListener->ModuleRead(GlobalID, CurrentModule); 3459 3460 SubmodulesLoaded[GlobalIndex] = CurrentModule; 3461 3462 // Clear out link libraries; the module file has them. 3463 CurrentModule->LinkLibraries.clear(); 3464 break; 3465 } 3466 3467 case SUBMODULE_UMBRELLA_HEADER: { 3468 if (First) { 3469 Error("missing submodule metadata record at beginning of block"); 3470 return true; 3471 } 3472 3473 if (!CurrentModule) 3474 break; 3475 3476 if (const FileEntry *Umbrella = PP.getFileManager().getFile(Blob)) { 3477 if (!CurrentModule->getUmbrellaHeader()) 3478 ModMap.setUmbrellaHeader(CurrentModule, Umbrella); 3479 else if (CurrentModule->getUmbrellaHeader() != Umbrella) { 3480 Error("mismatched umbrella headers in submodule"); 3481 return true; 3482 } 3483 } 3484 break; 3485 } 3486 3487 case SUBMODULE_HEADER: { 3488 if (First) { 3489 Error("missing submodule metadata record at beginning of block"); 3490 return true; 3491 } 3492 3493 if (!CurrentModule) 3494 break; 3495 3496 // FIXME: Be more lazy about this! 3497 if (const FileEntry *File = PP.getFileManager().getFile(Blob)) { 3498 if (std::find(CurrentModule->Headers.begin(), 3499 CurrentModule->Headers.end(), 3500 File) == CurrentModule->Headers.end()) 3501 ModMap.addHeader(CurrentModule, File, false); 3502 } 3503 break; 3504 } 3505 3506 case SUBMODULE_EXCLUDED_HEADER: { 3507 if (First) { 3508 Error("missing submodule metadata record at beginning of block"); 3509 return true; 3510 } 3511 3512 if (!CurrentModule) 3513 break; 3514 3515 // FIXME: Be more lazy about this! 3516 if (const FileEntry *File = PP.getFileManager().getFile(Blob)) { 3517 if (std::find(CurrentModule->Headers.begin(), 3518 CurrentModule->Headers.end(), 3519 File) == CurrentModule->Headers.end()) 3520 ModMap.addHeader(CurrentModule, File, true); 3521 } 3522 break; 3523 } 3524 3525 case SUBMODULE_TOPHEADER: { 3526 if (First) { 3527 Error("missing submodule metadata record at beginning of block"); 3528 return true; 3529 } 3530 3531 if (!CurrentModule) 3532 break; 3533 3534 // FIXME: Be more lazy about this! 3535 if (const FileEntry *File = PP.getFileManager().getFile(Blob)) 3536 CurrentModule->TopHeaders.insert(File); 3537 break; 3538 } 3539 3540 case SUBMODULE_UMBRELLA_DIR: { 3541 if (First) { 3542 Error("missing submodule metadata record at beginning of block"); 3543 return true; 3544 } 3545 3546 if (!CurrentModule) 3547 break; 3548 3549 if (const DirectoryEntry *Umbrella 3550 = PP.getFileManager().getDirectory(Blob)) { 3551 if (!CurrentModule->getUmbrellaDir()) 3552 ModMap.setUmbrellaDir(CurrentModule, Umbrella); 3553 else if (CurrentModule->getUmbrellaDir() != Umbrella) { 3554 Error("mismatched umbrella directories in submodule"); 3555 return true; 3556 } 3557 } 3558 break; 3559 } 3560 3561 case SUBMODULE_METADATA: { 3562 if (!First) { 3563 Error("submodule metadata record not at beginning of block"); 3564 return true; 3565 } 3566 First = false; 3567 3568 F.BaseSubmoduleID = getTotalNumSubmodules(); 3569 F.LocalNumSubmodules = Record[0]; 3570 unsigned LocalBaseSubmoduleID = Record[1]; 3571 if (F.LocalNumSubmodules > 0) { 3572 // Introduce the global -> local mapping for submodules within this 3573 // module. 3574 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F)); 3575 3576 // Introduce the local -> global mapping for submodules within this 3577 // module. 3578 F.SubmoduleRemap.insertOrReplace( 3579 std::make_pair(LocalBaseSubmoduleID, 3580 F.BaseSubmoduleID - LocalBaseSubmoduleID)); 3581 3582 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules); 3583 } 3584 break; 3585 } 3586 3587 case SUBMODULE_IMPORTS: { 3588 if (First) { 3589 Error("missing submodule metadata record at beginning of block"); 3590 return true; 3591 } 3592 3593 if (!CurrentModule) 3594 break; 3595 3596 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) { 3597 UnresolvedModuleImportExport Unresolved; 3598 Unresolved.File = &F; 3599 Unresolved.Mod = CurrentModule; 3600 Unresolved.ID = Record[Idx]; 3601 Unresolved.IsImport = true; 3602 Unresolved.IsWildcard = false; 3603 UnresolvedModuleImportExports.push_back(Unresolved); 3604 } 3605 break; 3606 } 3607 3608 case SUBMODULE_EXPORTS: { 3609 if (First) { 3610 Error("missing submodule metadata record at beginning of block"); 3611 return true; 3612 } 3613 3614 if (!CurrentModule) 3615 break; 3616 3617 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) { 3618 UnresolvedModuleImportExport Unresolved; 3619 Unresolved.File = &F; 3620 Unresolved.Mod = CurrentModule; 3621 Unresolved.ID = Record[Idx]; 3622 Unresolved.IsImport = false; 3623 Unresolved.IsWildcard = Record[Idx + 1]; 3624 UnresolvedModuleImportExports.push_back(Unresolved); 3625 } 3626 3627 // Once we've loaded the set of exports, there's no reason to keep 3628 // the parsed, unresolved exports around. 3629 CurrentModule->UnresolvedExports.clear(); 3630 break; 3631 } 3632 case SUBMODULE_REQUIRES: { 3633 if (First) { 3634 Error("missing submodule metadata record at beginning of block"); 3635 return true; 3636 } 3637 3638 if (!CurrentModule) 3639 break; 3640 3641 CurrentModule->addRequirement(Blob, Context.getLangOpts(), 3642 Context.getTargetInfo()); 3643 break; 3644 } 3645 3646 case SUBMODULE_LINK_LIBRARY: 3647 if (First) { 3648 Error("missing submodule metadata record at beginning of block"); 3649 return true; 3650 } 3651 3652 if (!CurrentModule) 3653 break; 3654 3655 CurrentModule->LinkLibraries.push_back( 3656 Module::LinkLibrary(Blob, Record[0])); 3657 break; 3658 } 3659 } 3660} 3661 3662/// \brief Parse the record that corresponds to a LangOptions data 3663/// structure. 3664/// 3665/// This routine parses the language options from the AST file and then gives 3666/// them to the AST listener if one is set. 3667/// 3668/// \returns true if the listener deems the file unacceptable, false otherwise. 3669bool ASTReader::ParseLanguageOptions(const RecordData &Record, 3670 bool Complain, 3671 ASTReaderListener &Listener) { 3672 LangOptions LangOpts; 3673 unsigned Idx = 0; 3674#define LANGOPT(Name, Bits, Default, Description) \ 3675 LangOpts.Name = Record[Idx++]; 3676#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 3677 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++])); 3678#include "clang/Basic/LangOptions.def" 3679#define SANITIZER(NAME, ID) LangOpts.Sanitize.ID = Record[Idx++]; 3680#include "clang/Basic/Sanitizers.def" 3681 3682 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++]; 3683 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx); 3684 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion); 3685 3686 unsigned Length = Record[Idx++]; 3687 LangOpts.CurrentModule.assign(Record.begin() + Idx, 3688 Record.begin() + Idx + Length); 3689 3690 Idx += Length; 3691 3692 // Comment options. 3693 for (unsigned N = Record[Idx++]; N; --N) { 3694 LangOpts.CommentOpts.BlockCommandNames.push_back( 3695 ReadString(Record, Idx)); 3696 } 3697 3698 return Listener.ReadLanguageOptions(LangOpts, Complain); 3699} 3700 3701bool ASTReader::ParseTargetOptions(const RecordData &Record, 3702 bool Complain, 3703 ASTReaderListener &Listener) { 3704 unsigned Idx = 0; 3705 TargetOptions TargetOpts; 3706 TargetOpts.Triple = ReadString(Record, Idx); 3707 TargetOpts.CPU = ReadString(Record, Idx); 3708 TargetOpts.ABI = ReadString(Record, Idx); 3709 TargetOpts.CXXABI = ReadString(Record, Idx); 3710 TargetOpts.LinkerVersion = ReadString(Record, Idx); 3711 for (unsigned N = Record[Idx++]; N; --N) { 3712 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx)); 3713 } 3714 for (unsigned N = Record[Idx++]; N; --N) { 3715 TargetOpts.Features.push_back(ReadString(Record, Idx)); 3716 } 3717 3718 return Listener.ReadTargetOptions(TargetOpts, Complain); 3719} 3720 3721bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain, 3722 ASTReaderListener &Listener) { 3723 DiagnosticOptions DiagOpts; 3724 unsigned Idx = 0; 3725#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++]; 3726#define ENUM_DIAGOPT(Name, Type, Bits, Default) \ 3727 DiagOpts.set##Name(static_cast<Type>(Record[Idx++])); 3728#include "clang/Basic/DiagnosticOptions.def" 3729 3730 for (unsigned N = Record[Idx++]; N; --N) { 3731 DiagOpts.Warnings.push_back(ReadString(Record, Idx)); 3732 } 3733 3734 return Listener.ReadDiagnosticOptions(DiagOpts, Complain); 3735} 3736 3737bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain, 3738 ASTReaderListener &Listener) { 3739 FileSystemOptions FSOpts; 3740 unsigned Idx = 0; 3741 FSOpts.WorkingDir = ReadString(Record, Idx); 3742 return Listener.ReadFileSystemOptions(FSOpts, Complain); 3743} 3744 3745bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record, 3746 bool Complain, 3747 ASTReaderListener &Listener) { 3748 HeaderSearchOptions HSOpts; 3749 unsigned Idx = 0; 3750 HSOpts.Sysroot = ReadString(Record, Idx); 3751 3752 // Include entries. 3753 for (unsigned N = Record[Idx++]; N; --N) { 3754 std::string Path = ReadString(Record, Idx); 3755 frontend::IncludeDirGroup Group 3756 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]); 3757 bool IsFramework = Record[Idx++]; 3758 bool IgnoreSysRoot = Record[Idx++]; 3759 HSOpts.UserEntries.push_back( 3760 HeaderSearchOptions::Entry(Path, Group, IsFramework, IgnoreSysRoot)); 3761 } 3762 3763 // System header prefixes. 3764 for (unsigned N = Record[Idx++]; N; --N) { 3765 std::string Prefix = ReadString(Record, Idx); 3766 bool IsSystemHeader = Record[Idx++]; 3767 HSOpts.SystemHeaderPrefixes.push_back( 3768 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader)); 3769 } 3770 3771 HSOpts.ResourceDir = ReadString(Record, Idx); 3772 HSOpts.ModuleCachePath = ReadString(Record, Idx); 3773 HSOpts.DisableModuleHash = Record[Idx++]; 3774 HSOpts.UseBuiltinIncludes = Record[Idx++]; 3775 HSOpts.UseStandardSystemIncludes = Record[Idx++]; 3776 HSOpts.UseStandardCXXIncludes = Record[Idx++]; 3777 HSOpts.UseLibcxx = Record[Idx++]; 3778 3779 return Listener.ReadHeaderSearchOptions(HSOpts, Complain); 3780} 3781 3782bool ASTReader::ParsePreprocessorOptions(const RecordData &Record, 3783 bool Complain, 3784 ASTReaderListener &Listener, 3785 std::string &SuggestedPredefines) { 3786 PreprocessorOptions PPOpts; 3787 unsigned Idx = 0; 3788 3789 // Macro definitions/undefs 3790 for (unsigned N = Record[Idx++]; N; --N) { 3791 std::string Macro = ReadString(Record, Idx); 3792 bool IsUndef = Record[Idx++]; 3793 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef)); 3794 } 3795 3796 // Includes 3797 for (unsigned N = Record[Idx++]; N; --N) { 3798 PPOpts.Includes.push_back(ReadString(Record, Idx)); 3799 } 3800 3801 // Macro Includes 3802 for (unsigned N = Record[Idx++]; N; --N) { 3803 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx)); 3804 } 3805 3806 PPOpts.UsePredefines = Record[Idx++]; 3807 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx); 3808 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx); 3809 PPOpts.ObjCXXARCStandardLibrary = 3810 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]); 3811 SuggestedPredefines.clear(); 3812 return Listener.ReadPreprocessorOptions(PPOpts, Complain, 3813 SuggestedPredefines); 3814} 3815 3816std::pair<ModuleFile *, unsigned> 3817ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) { 3818 GlobalPreprocessedEntityMapType::iterator 3819 I = GlobalPreprocessedEntityMap.find(GlobalIndex); 3820 assert(I != GlobalPreprocessedEntityMap.end() && 3821 "Corrupted global preprocessed entity map"); 3822 ModuleFile *M = I->second; 3823 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID; 3824 return std::make_pair(M, LocalIndex); 3825} 3826 3827std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator> 3828ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const { 3829 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord()) 3830 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID, 3831 Mod.NumPreprocessedEntities); 3832 3833 return std::make_pair(PreprocessingRecord::iterator(), 3834 PreprocessingRecord::iterator()); 3835} 3836 3837std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator> 3838ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) { 3839 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls), 3840 ModuleDeclIterator(this, &Mod, 3841 Mod.FileSortedDecls + Mod.NumFileSortedDecls)); 3842} 3843 3844PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) { 3845 PreprocessedEntityID PPID = Index+1; 3846 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 3847 ModuleFile &M = *PPInfo.first; 3848 unsigned LocalIndex = PPInfo.second; 3849 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 3850 3851 if (!PP.getPreprocessingRecord()) { 3852 Error("no preprocessing record"); 3853 return 0; 3854 } 3855 3856 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor); 3857 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset); 3858 3859 llvm::BitstreamEntry Entry = 3860 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 3861 if (Entry.Kind != llvm::BitstreamEntry::Record) 3862 return 0; 3863 3864 // Read the record. 3865 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin), 3866 ReadSourceLocation(M, PPOffs.End)); 3867 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 3868 StringRef Blob; 3869 RecordData Record; 3870 PreprocessorDetailRecordTypes RecType = 3871 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord( 3872 Entry.ID, Record, &Blob); 3873 switch (RecType) { 3874 case PPD_MACRO_EXPANSION: { 3875 bool isBuiltin = Record[0]; 3876 IdentifierInfo *Name = 0; 3877 MacroDefinition *Def = 0; 3878 if (isBuiltin) 3879 Name = getLocalIdentifier(M, Record[1]); 3880 else { 3881 PreprocessedEntityID 3882 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]); 3883 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1)); 3884 } 3885 3886 MacroExpansion *ME; 3887 if (isBuiltin) 3888 ME = new (PPRec) MacroExpansion(Name, Range); 3889 else 3890 ME = new (PPRec) MacroExpansion(Def, Range); 3891 3892 return ME; 3893 } 3894 3895 case PPD_MACRO_DEFINITION: { 3896 // Decode the identifier info and then check again; if the macro is 3897 // still defined and associated with the identifier, 3898 IdentifierInfo *II = getLocalIdentifier(M, Record[0]); 3899 MacroDefinition *MD 3900 = new (PPRec) MacroDefinition(II, Range); 3901 3902 if (DeserializationListener) 3903 DeserializationListener->MacroDefinitionRead(PPID, MD); 3904 3905 return MD; 3906 } 3907 3908 case PPD_INCLUSION_DIRECTIVE: { 3909 const char *FullFileNameStart = Blob.data() + Record[0]; 3910 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]); 3911 const FileEntry *File = 0; 3912 if (!FullFileName.empty()) 3913 File = PP.getFileManager().getFile(FullFileName); 3914 3915 // FIXME: Stable encoding 3916 InclusionDirective::InclusionKind Kind 3917 = static_cast<InclusionDirective::InclusionKind>(Record[2]); 3918 InclusionDirective *ID 3919 = new (PPRec) InclusionDirective(PPRec, Kind, 3920 StringRef(Blob.data(), Record[0]), 3921 Record[1], Record[3], 3922 File, 3923 Range); 3924 return ID; 3925 } 3926 } 3927 3928 llvm_unreachable("Invalid PreprocessorDetailRecordTypes"); 3929} 3930 3931/// \brief \arg SLocMapI points at a chunk of a module that contains no 3932/// preprocessed entities or the entities it contains are not the ones we are 3933/// looking for. Find the next module that contains entities and return the ID 3934/// of the first entry. 3935PreprocessedEntityID ASTReader::findNextPreprocessedEntity( 3936 GlobalSLocOffsetMapType::const_iterator SLocMapI) const { 3937 ++SLocMapI; 3938 for (GlobalSLocOffsetMapType::const_iterator 3939 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) { 3940 ModuleFile &M = *SLocMapI->second; 3941 if (M.NumPreprocessedEntities) 3942 return M.BasePreprocessedEntityID; 3943 } 3944 3945 return getTotalNumPreprocessedEntities(); 3946} 3947 3948namespace { 3949 3950template <unsigned PPEntityOffset::*PPLoc> 3951struct PPEntityComp { 3952 const ASTReader &Reader; 3953 ModuleFile &M; 3954 3955 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { } 3956 3957 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const { 3958 SourceLocation LHS = getLoc(L); 3959 SourceLocation RHS = getLoc(R); 3960 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 3961 } 3962 3963 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const { 3964 SourceLocation LHS = getLoc(L); 3965 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 3966 } 3967 3968 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const { 3969 SourceLocation RHS = getLoc(R); 3970 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 3971 } 3972 3973 SourceLocation getLoc(const PPEntityOffset &PPE) const { 3974 return Reader.ReadSourceLocation(M, PPE.*PPLoc); 3975 } 3976}; 3977 3978} 3979 3980/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc. 3981PreprocessedEntityID 3982ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const { 3983 if (SourceMgr.isLocalSourceLocation(BLoc)) 3984 return getTotalNumPreprocessedEntities(); 3985 3986 GlobalSLocOffsetMapType::const_iterator 3987 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset - 3988 BLoc.getOffset()); 3989 assert(SLocMapI != GlobalSLocOffsetMap.end() && 3990 "Corrupted global sloc offset map"); 3991 3992 if (SLocMapI->second->NumPreprocessedEntities == 0) 3993 return findNextPreprocessedEntity(SLocMapI); 3994 3995 ModuleFile &M = *SLocMapI->second; 3996 typedef const PPEntityOffset *pp_iterator; 3997 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 3998 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 3999 4000 size_t Count = M.NumPreprocessedEntities; 4001 size_t Half; 4002 pp_iterator First = pp_begin; 4003 pp_iterator PPI; 4004 4005 // Do a binary search manually instead of using std::lower_bound because 4006 // The end locations of entities may be unordered (when a macro expansion 4007 // is inside another macro argument), but for this case it is not important 4008 // whether we get the first macro expansion or its containing macro. 4009 while (Count > 0) { 4010 Half = Count/2; 4011 PPI = First; 4012 std::advance(PPI, Half); 4013 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End), 4014 BLoc)){ 4015 First = PPI; 4016 ++First; 4017 Count = Count - Half - 1; 4018 } else 4019 Count = Half; 4020 } 4021 4022 if (PPI == pp_end) 4023 return findNextPreprocessedEntity(SLocMapI); 4024 4025 return M.BasePreprocessedEntityID + (PPI - pp_begin); 4026} 4027 4028/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc. 4029PreprocessedEntityID 4030ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const { 4031 if (SourceMgr.isLocalSourceLocation(ELoc)) 4032 return getTotalNumPreprocessedEntities(); 4033 4034 GlobalSLocOffsetMapType::const_iterator 4035 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset - 4036 ELoc.getOffset()); 4037 assert(SLocMapI != GlobalSLocOffsetMap.end() && 4038 "Corrupted global sloc offset map"); 4039 4040 if (SLocMapI->second->NumPreprocessedEntities == 0) 4041 return findNextPreprocessedEntity(SLocMapI); 4042 4043 ModuleFile &M = *SLocMapI->second; 4044 typedef const PPEntityOffset *pp_iterator; 4045 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 4046 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 4047 pp_iterator PPI = 4048 std::upper_bound(pp_begin, pp_end, ELoc, 4049 PPEntityComp<&PPEntityOffset::Begin>(*this, M)); 4050 4051 if (PPI == pp_end) 4052 return findNextPreprocessedEntity(SLocMapI); 4053 4054 return M.BasePreprocessedEntityID + (PPI - pp_begin); 4055} 4056 4057/// \brief Returns a pair of [Begin, End) indices of preallocated 4058/// preprocessed entities that \arg Range encompasses. 4059std::pair<unsigned, unsigned> 4060 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) { 4061 if (Range.isInvalid()) 4062 return std::make_pair(0,0); 4063 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); 4064 4065 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin()); 4066 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd()); 4067 return std::make_pair(BeginID, EndID); 4068} 4069 4070/// \brief Optionally returns true or false if the preallocated preprocessed 4071/// entity with index \arg Index came from file \arg FID. 4072Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index, 4073 FileID FID) { 4074 if (FID.isInvalid()) 4075 return false; 4076 4077 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 4078 ModuleFile &M = *PPInfo.first; 4079 unsigned LocalIndex = PPInfo.second; 4080 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 4081 4082 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin); 4083 if (Loc.isInvalid()) 4084 return false; 4085 4086 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID)) 4087 return true; 4088 else 4089 return false; 4090} 4091 4092namespace { 4093 /// \brief Visitor used to search for information about a header file. 4094 class HeaderFileInfoVisitor { 4095 ASTReader &Reader; 4096 const FileEntry *FE; 4097 4098 Optional<HeaderFileInfo> HFI; 4099 4100 public: 4101 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE) 4102 : Reader(Reader), FE(FE) { } 4103 4104 static bool visit(ModuleFile &M, void *UserData) { 4105 HeaderFileInfoVisitor *This 4106 = static_cast<HeaderFileInfoVisitor *>(UserData); 4107 4108 HeaderFileInfoTrait Trait(This->Reader, M, 4109 &This->Reader.getPreprocessor().getHeaderSearchInfo(), 4110 M.HeaderFileFrameworkStrings, 4111 This->FE->getName()); 4112 4113 HeaderFileInfoLookupTable *Table 4114 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable); 4115 if (!Table) 4116 return false; 4117 4118 // Look in the on-disk hash table for an entry for this file name. 4119 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(), 4120 &Trait); 4121 if (Pos == Table->end()) 4122 return false; 4123 4124 This->HFI = *Pos; 4125 return true; 4126 } 4127 4128 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; } 4129 }; 4130} 4131 4132HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) { 4133 HeaderFileInfoVisitor Visitor(*this, FE); 4134 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor); 4135 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) { 4136 if (Listener) 4137 Listener->ReadHeaderFileInfo(*HFI, FE->getUID()); 4138 return *HFI; 4139 } 4140 4141 return HeaderFileInfo(); 4142} 4143 4144void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) { 4145 // FIXME: Make it work properly with modules. 4146 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates; 4147 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) { 4148 ModuleFile &F = *(*I); 4149 unsigned Idx = 0; 4150 DiagStates.clear(); 4151 assert(!Diag.DiagStates.empty()); 4152 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one. 4153 while (Idx < F.PragmaDiagMappings.size()) { 4154 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]); 4155 unsigned DiagStateID = F.PragmaDiagMappings[Idx++]; 4156 if (DiagStateID != 0) { 4157 Diag.DiagStatePoints.push_back( 4158 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1], 4159 FullSourceLoc(Loc, SourceMgr))); 4160 continue; 4161 } 4162 4163 assert(DiagStateID == 0); 4164 // A new DiagState was created here. 4165 Diag.DiagStates.push_back(*Diag.GetCurDiagState()); 4166 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back(); 4167 DiagStates.push_back(NewState); 4168 Diag.DiagStatePoints.push_back( 4169 DiagnosticsEngine::DiagStatePoint(NewState, 4170 FullSourceLoc(Loc, SourceMgr))); 4171 while (1) { 4172 assert(Idx < F.PragmaDiagMappings.size() && 4173 "Invalid data, didn't find '-1' marking end of diag/map pairs"); 4174 if (Idx >= F.PragmaDiagMappings.size()) { 4175 break; // Something is messed up but at least avoid infinite loop in 4176 // release build. 4177 } 4178 unsigned DiagID = F.PragmaDiagMappings[Idx++]; 4179 if (DiagID == (unsigned)-1) { 4180 break; // no more diag/map pairs for this location. 4181 } 4182 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++]; 4183 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc); 4184 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo); 4185 } 4186 } 4187 } 4188} 4189 4190/// \brief Get the correct cursor and offset for loading a type. 4191ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) { 4192 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index); 4193 assert(I != GlobalTypeMap.end() && "Corrupted global type map"); 4194 ModuleFile *M = I->second; 4195 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]); 4196} 4197 4198/// \brief Read and return the type with the given index.. 4199/// 4200/// The index is the type ID, shifted and minus the number of predefs. This 4201/// routine actually reads the record corresponding to the type at the given 4202/// location. It is a helper routine for GetType, which deals with reading type 4203/// IDs. 4204QualType ASTReader::readTypeRecord(unsigned Index) { 4205 RecordLocation Loc = TypeCursorForIndex(Index); 4206 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 4207 4208 // Keep track of where we are in the stream, then jump back there 4209 // after reading this type. 4210 SavedStreamPosition SavedPosition(DeclsCursor); 4211 4212 ReadingKindTracker ReadingKind(Read_Type, *this); 4213 4214 // Note that we are loading a type record. 4215 Deserializing AType(this); 4216 4217 unsigned Idx = 0; 4218 DeclsCursor.JumpToBit(Loc.Offset); 4219 RecordData Record; 4220 unsigned Code = DeclsCursor.ReadCode(); 4221 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) { 4222 case TYPE_EXT_QUAL: { 4223 if (Record.size() != 2) { 4224 Error("Incorrect encoding of extended qualifier type"); 4225 return QualType(); 4226 } 4227 QualType Base = readType(*Loc.F, Record, Idx); 4228 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]); 4229 return Context.getQualifiedType(Base, Quals); 4230 } 4231 4232 case TYPE_COMPLEX: { 4233 if (Record.size() != 1) { 4234 Error("Incorrect encoding of complex type"); 4235 return QualType(); 4236 } 4237 QualType ElemType = readType(*Loc.F, Record, Idx); 4238 return Context.getComplexType(ElemType); 4239 } 4240 4241 case TYPE_POINTER: { 4242 if (Record.size() != 1) { 4243 Error("Incorrect encoding of pointer type"); 4244 return QualType(); 4245 } 4246 QualType PointeeType = readType(*Loc.F, Record, Idx); 4247 return Context.getPointerType(PointeeType); 4248 } 4249 4250 case TYPE_BLOCK_POINTER: { 4251 if (Record.size() != 1) { 4252 Error("Incorrect encoding of block pointer type"); 4253 return QualType(); 4254 } 4255 QualType PointeeType = readType(*Loc.F, Record, Idx); 4256 return Context.getBlockPointerType(PointeeType); 4257 } 4258 4259 case TYPE_LVALUE_REFERENCE: { 4260 if (Record.size() != 2) { 4261 Error("Incorrect encoding of lvalue reference type"); 4262 return QualType(); 4263 } 4264 QualType PointeeType = readType(*Loc.F, Record, Idx); 4265 return Context.getLValueReferenceType(PointeeType, Record[1]); 4266 } 4267 4268 case TYPE_RVALUE_REFERENCE: { 4269 if (Record.size() != 1) { 4270 Error("Incorrect encoding of rvalue reference type"); 4271 return QualType(); 4272 } 4273 QualType PointeeType = readType(*Loc.F, Record, Idx); 4274 return Context.getRValueReferenceType(PointeeType); 4275 } 4276 4277 case TYPE_MEMBER_POINTER: { 4278 if (Record.size() != 2) { 4279 Error("Incorrect encoding of member pointer type"); 4280 return QualType(); 4281 } 4282 QualType PointeeType = readType(*Loc.F, Record, Idx); 4283 QualType ClassType = readType(*Loc.F, Record, Idx); 4284 if (PointeeType.isNull() || ClassType.isNull()) 4285 return QualType(); 4286 4287 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr()); 4288 } 4289 4290 case TYPE_CONSTANT_ARRAY: { 4291 QualType ElementType = readType(*Loc.F, Record, Idx); 4292 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 4293 unsigned IndexTypeQuals = Record[2]; 4294 unsigned Idx = 3; 4295 llvm::APInt Size = ReadAPInt(Record, Idx); 4296 return Context.getConstantArrayType(ElementType, Size, 4297 ASM, IndexTypeQuals); 4298 } 4299 4300 case TYPE_INCOMPLETE_ARRAY: { 4301 QualType ElementType = readType(*Loc.F, Record, Idx); 4302 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 4303 unsigned IndexTypeQuals = Record[2]; 4304 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals); 4305 } 4306 4307 case TYPE_VARIABLE_ARRAY: { 4308 QualType ElementType = readType(*Loc.F, Record, Idx); 4309 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 4310 unsigned IndexTypeQuals = Record[2]; 4311 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]); 4312 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]); 4313 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F), 4314 ASM, IndexTypeQuals, 4315 SourceRange(LBLoc, RBLoc)); 4316 } 4317 4318 case TYPE_VECTOR: { 4319 if (Record.size() != 3) { 4320 Error("incorrect encoding of vector type in AST file"); 4321 return QualType(); 4322 } 4323 4324 QualType ElementType = readType(*Loc.F, Record, Idx); 4325 unsigned NumElements = Record[1]; 4326 unsigned VecKind = Record[2]; 4327 return Context.getVectorType(ElementType, NumElements, 4328 (VectorType::VectorKind)VecKind); 4329 } 4330 4331 case TYPE_EXT_VECTOR: { 4332 if (Record.size() != 3) { 4333 Error("incorrect encoding of extended vector type in AST file"); 4334 return QualType(); 4335 } 4336 4337 QualType ElementType = readType(*Loc.F, Record, Idx); 4338 unsigned NumElements = Record[1]; 4339 return Context.getExtVectorType(ElementType, NumElements); 4340 } 4341 4342 case TYPE_FUNCTION_NO_PROTO: { 4343 if (Record.size() != 6) { 4344 Error("incorrect encoding of no-proto function type"); 4345 return QualType(); 4346 } 4347 QualType ResultType = readType(*Loc.F, Record, Idx); 4348 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3], 4349 (CallingConv)Record[4], Record[5]); 4350 return Context.getFunctionNoProtoType(ResultType, Info); 4351 } 4352 4353 case TYPE_FUNCTION_PROTO: { 4354 QualType ResultType = readType(*Loc.F, Record, Idx); 4355 4356 FunctionProtoType::ExtProtoInfo EPI; 4357 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1], 4358 /*hasregparm*/ Record[2], 4359 /*regparm*/ Record[3], 4360 static_cast<CallingConv>(Record[4]), 4361 /*produces*/ Record[5]); 4362 4363 unsigned Idx = 6; 4364 unsigned NumParams = Record[Idx++]; 4365 SmallVector<QualType, 16> ParamTypes; 4366 for (unsigned I = 0; I != NumParams; ++I) 4367 ParamTypes.push_back(readType(*Loc.F, Record, Idx)); 4368 4369 EPI.Variadic = Record[Idx++]; 4370 EPI.HasTrailingReturn = Record[Idx++]; 4371 EPI.TypeQuals = Record[Idx++]; 4372 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]); 4373 ExceptionSpecificationType EST = 4374 static_cast<ExceptionSpecificationType>(Record[Idx++]); 4375 EPI.ExceptionSpecType = EST; 4376 SmallVector<QualType, 2> Exceptions; 4377 if (EST == EST_Dynamic) { 4378 EPI.NumExceptions = Record[Idx++]; 4379 for (unsigned I = 0; I != EPI.NumExceptions; ++I) 4380 Exceptions.push_back(readType(*Loc.F, Record, Idx)); 4381 EPI.Exceptions = Exceptions.data(); 4382 } else if (EST == EST_ComputedNoexcept) { 4383 EPI.NoexceptExpr = ReadExpr(*Loc.F); 4384 } else if (EST == EST_Uninstantiated) { 4385 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx); 4386 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx); 4387 } else if (EST == EST_Unevaluated) { 4388 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx); 4389 } 4390 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams, 4391 EPI); 4392 } 4393 4394 case TYPE_UNRESOLVED_USING: { 4395 unsigned Idx = 0; 4396 return Context.getTypeDeclType( 4397 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx)); 4398 } 4399 4400 case TYPE_TYPEDEF: { 4401 if (Record.size() != 2) { 4402 Error("incorrect encoding of typedef type"); 4403 return QualType(); 4404 } 4405 unsigned Idx = 0; 4406 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx); 4407 QualType Canonical = readType(*Loc.F, Record, Idx); 4408 if (!Canonical.isNull()) 4409 Canonical = Context.getCanonicalType(Canonical); 4410 return Context.getTypedefType(Decl, Canonical); 4411 } 4412 4413 case TYPE_TYPEOF_EXPR: 4414 return Context.getTypeOfExprType(ReadExpr(*Loc.F)); 4415 4416 case TYPE_TYPEOF: { 4417 if (Record.size() != 1) { 4418 Error("incorrect encoding of typeof(type) in AST file"); 4419 return QualType(); 4420 } 4421 QualType UnderlyingType = readType(*Loc.F, Record, Idx); 4422 return Context.getTypeOfType(UnderlyingType); 4423 } 4424 4425 case TYPE_DECLTYPE: { 4426 QualType UnderlyingType = readType(*Loc.F, Record, Idx); 4427 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType); 4428 } 4429 4430 case TYPE_UNARY_TRANSFORM: { 4431 QualType BaseType = readType(*Loc.F, Record, Idx); 4432 QualType UnderlyingType = readType(*Loc.F, Record, Idx); 4433 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2]; 4434 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind); 4435 } 4436 4437 case TYPE_AUTO: 4438 return Context.getAutoType(readType(*Loc.F, Record, Idx)); 4439 4440 case TYPE_RECORD: { 4441 if (Record.size() != 2) { 4442 Error("incorrect encoding of record type"); 4443 return QualType(); 4444 } 4445 unsigned Idx = 0; 4446 bool IsDependent = Record[Idx++]; 4447 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx); 4448 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl()); 4449 QualType T = Context.getRecordType(RD); 4450 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 4451 return T; 4452 } 4453 4454 case TYPE_ENUM: { 4455 if (Record.size() != 2) { 4456 Error("incorrect encoding of enum type"); 4457 return QualType(); 4458 } 4459 unsigned Idx = 0; 4460 bool IsDependent = Record[Idx++]; 4461 QualType T 4462 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx)); 4463 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 4464 return T; 4465 } 4466 4467 case TYPE_ATTRIBUTED: { 4468 if (Record.size() != 3) { 4469 Error("incorrect encoding of attributed type"); 4470 return QualType(); 4471 } 4472 QualType modifiedType = readType(*Loc.F, Record, Idx); 4473 QualType equivalentType = readType(*Loc.F, Record, Idx); 4474 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]); 4475 return Context.getAttributedType(kind, modifiedType, equivalentType); 4476 } 4477 4478 case TYPE_PAREN: { 4479 if (Record.size() != 1) { 4480 Error("incorrect encoding of paren type"); 4481 return QualType(); 4482 } 4483 QualType InnerType = readType(*Loc.F, Record, Idx); 4484 return Context.getParenType(InnerType); 4485 } 4486 4487 case TYPE_PACK_EXPANSION: { 4488 if (Record.size() != 2) { 4489 Error("incorrect encoding of pack expansion type"); 4490 return QualType(); 4491 } 4492 QualType Pattern = readType(*Loc.F, Record, Idx); 4493 if (Pattern.isNull()) 4494 return QualType(); 4495 Optional<unsigned> NumExpansions; 4496 if (Record[1]) 4497 NumExpansions = Record[1] - 1; 4498 return Context.getPackExpansionType(Pattern, NumExpansions); 4499 } 4500 4501 case TYPE_ELABORATED: { 4502 unsigned Idx = 0; 4503 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 4504 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 4505 QualType NamedType = readType(*Loc.F, Record, Idx); 4506 return Context.getElaboratedType(Keyword, NNS, NamedType); 4507 } 4508 4509 case TYPE_OBJC_INTERFACE: { 4510 unsigned Idx = 0; 4511 ObjCInterfaceDecl *ItfD 4512 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx); 4513 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl()); 4514 } 4515 4516 case TYPE_OBJC_OBJECT: { 4517 unsigned Idx = 0; 4518 QualType Base = readType(*Loc.F, Record, Idx); 4519 unsigned NumProtos = Record[Idx++]; 4520 SmallVector<ObjCProtocolDecl*, 4> Protos; 4521 for (unsigned I = 0; I != NumProtos; ++I) 4522 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx)); 4523 return Context.getObjCObjectType(Base, Protos.data(), NumProtos); 4524 } 4525 4526 case TYPE_OBJC_OBJECT_POINTER: { 4527 unsigned Idx = 0; 4528 QualType Pointee = readType(*Loc.F, Record, Idx); 4529 return Context.getObjCObjectPointerType(Pointee); 4530 } 4531 4532 case TYPE_SUBST_TEMPLATE_TYPE_PARM: { 4533 unsigned Idx = 0; 4534 QualType Parm = readType(*Loc.F, Record, Idx); 4535 QualType Replacement = readType(*Loc.F, Record, Idx); 4536 return 4537 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm), 4538 Replacement); 4539 } 4540 4541 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: { 4542 unsigned Idx = 0; 4543 QualType Parm = readType(*Loc.F, Record, Idx); 4544 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx); 4545 return Context.getSubstTemplateTypeParmPackType( 4546 cast<TemplateTypeParmType>(Parm), 4547 ArgPack); 4548 } 4549 4550 case TYPE_INJECTED_CLASS_NAME: { 4551 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx); 4552 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable 4553 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable 4554 // for AST reading, too much interdependencies. 4555 return 4556 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0); 4557 } 4558 4559 case TYPE_TEMPLATE_TYPE_PARM: { 4560 unsigned Idx = 0; 4561 unsigned Depth = Record[Idx++]; 4562 unsigned Index = Record[Idx++]; 4563 bool Pack = Record[Idx++]; 4564 TemplateTypeParmDecl *D 4565 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx); 4566 return Context.getTemplateTypeParmType(Depth, Index, Pack, D); 4567 } 4568 4569 case TYPE_DEPENDENT_NAME: { 4570 unsigned Idx = 0; 4571 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 4572 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 4573 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx); 4574 QualType Canon = readType(*Loc.F, Record, Idx); 4575 if (!Canon.isNull()) 4576 Canon = Context.getCanonicalType(Canon); 4577 return Context.getDependentNameType(Keyword, NNS, Name, Canon); 4578 } 4579 4580 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: { 4581 unsigned Idx = 0; 4582 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 4583 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 4584 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx); 4585 unsigned NumArgs = Record[Idx++]; 4586 SmallVector<TemplateArgument, 8> Args; 4587 Args.reserve(NumArgs); 4588 while (NumArgs--) 4589 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx)); 4590 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name, 4591 Args.size(), Args.data()); 4592 } 4593 4594 case TYPE_DEPENDENT_SIZED_ARRAY: { 4595 unsigned Idx = 0; 4596 4597 // ArrayType 4598 QualType ElementType = readType(*Loc.F, Record, Idx); 4599 ArrayType::ArraySizeModifier ASM 4600 = (ArrayType::ArraySizeModifier)Record[Idx++]; 4601 unsigned IndexTypeQuals = Record[Idx++]; 4602 4603 // DependentSizedArrayType 4604 Expr *NumElts = ReadExpr(*Loc.F); 4605 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx); 4606 4607 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM, 4608 IndexTypeQuals, Brackets); 4609 } 4610 4611 case TYPE_TEMPLATE_SPECIALIZATION: { 4612 unsigned Idx = 0; 4613 bool IsDependent = Record[Idx++]; 4614 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx); 4615 SmallVector<TemplateArgument, 8> Args; 4616 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx); 4617 QualType Underlying = readType(*Loc.F, Record, Idx); 4618 QualType T; 4619 if (Underlying.isNull()) 4620 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(), 4621 Args.size()); 4622 else 4623 T = Context.getTemplateSpecializationType(Name, Args.data(), 4624 Args.size(), Underlying); 4625 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 4626 return T; 4627 } 4628 4629 case TYPE_ATOMIC: { 4630 if (Record.size() != 1) { 4631 Error("Incorrect encoding of atomic type"); 4632 return QualType(); 4633 } 4634 QualType ValueType = readType(*Loc.F, Record, Idx); 4635 return Context.getAtomicType(ValueType); 4636 } 4637 } 4638 llvm_unreachable("Invalid TypeCode!"); 4639} 4640 4641class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> { 4642 ASTReader &Reader; 4643 ModuleFile &F; 4644 const ASTReader::RecordData &Record; 4645 unsigned &Idx; 4646 4647 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R, 4648 unsigned &I) { 4649 return Reader.ReadSourceLocation(F, R, I); 4650 } 4651 4652 template<typename T> 4653 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) { 4654 return Reader.ReadDeclAs<T>(F, Record, Idx); 4655 } 4656 4657public: 4658 TypeLocReader(ASTReader &Reader, ModuleFile &F, 4659 const ASTReader::RecordData &Record, unsigned &Idx) 4660 : Reader(Reader), F(F), Record(Record), Idx(Idx) 4661 { } 4662 4663 // We want compile-time assurance that we've enumerated all of 4664 // these, so unfortunately we have to declare them first, then 4665 // define them out-of-line. 4666#define ABSTRACT_TYPELOC(CLASS, PARENT) 4667#define TYPELOC(CLASS, PARENT) \ 4668 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 4669#include "clang/AST/TypeLocNodes.def" 4670 4671 void VisitFunctionTypeLoc(FunctionTypeLoc); 4672 void VisitArrayTypeLoc(ArrayTypeLoc); 4673}; 4674 4675void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 4676 // nothing to do 4677} 4678void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 4679 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx)); 4680 if (TL.needsExtraLocalData()) { 4681 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++])); 4682 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++])); 4683 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++])); 4684 TL.setModeAttr(Record[Idx++]); 4685 } 4686} 4687void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { 4688 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4689} 4690void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { 4691 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 4692} 4693void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 4694 TL.setCaretLoc(ReadSourceLocation(Record, Idx)); 4695} 4696void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 4697 TL.setAmpLoc(ReadSourceLocation(Record, Idx)); 4698} 4699void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 4700 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx)); 4701} 4702void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 4703 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 4704 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx)); 4705} 4706void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { 4707 TL.setLBracketLoc(ReadSourceLocation(Record, Idx)); 4708 TL.setRBracketLoc(ReadSourceLocation(Record, Idx)); 4709 if (Record[Idx++]) 4710 TL.setSizeExpr(Reader.ReadExpr(F)); 4711 else 4712 TL.setSizeExpr(0); 4713} 4714void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 4715 VisitArrayTypeLoc(TL); 4716} 4717void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 4718 VisitArrayTypeLoc(TL); 4719} 4720void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 4721 VisitArrayTypeLoc(TL); 4722} 4723void TypeLocReader::VisitDependentSizedArrayTypeLoc( 4724 DependentSizedArrayTypeLoc TL) { 4725 VisitArrayTypeLoc(TL); 4726} 4727void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( 4728 DependentSizedExtVectorTypeLoc TL) { 4729 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4730} 4731void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { 4732 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4733} 4734void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 4735 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4736} 4737void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 4738 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx)); 4739 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 4740 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 4741 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx)); 4742 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) { 4743 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx)); 4744 } 4745} 4746void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 4747 VisitFunctionTypeLoc(TL); 4748} 4749void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 4750 VisitFunctionTypeLoc(TL); 4751} 4752void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 4753 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4754} 4755void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 4756 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4757} 4758void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 4759 TL.setTypeofLoc(ReadSourceLocation(Record, Idx)); 4760 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 4761 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 4762} 4763void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 4764 TL.setTypeofLoc(ReadSourceLocation(Record, Idx)); 4765 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 4766 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 4767 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx)); 4768} 4769void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 4770 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4771} 4772void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { 4773 TL.setKWLoc(ReadSourceLocation(Record, Idx)); 4774 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 4775 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 4776 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx)); 4777} 4778void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) { 4779 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4780} 4781void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { 4782 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4783} 4784void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { 4785 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4786} 4787void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 4788 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx)); 4789 if (TL.hasAttrOperand()) { 4790 SourceRange range; 4791 range.setBegin(ReadSourceLocation(Record, Idx)); 4792 range.setEnd(ReadSourceLocation(Record, Idx)); 4793 TL.setAttrOperandParensRange(range); 4794 } 4795 if (TL.hasAttrExprOperand()) { 4796 if (Record[Idx++]) 4797 TL.setAttrExprOperand(Reader.ReadExpr(F)); 4798 else 4799 TL.setAttrExprOperand(0); 4800 } else if (TL.hasAttrEnumOperand()) 4801 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx)); 4802} 4803void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 4804 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4805} 4806void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( 4807 SubstTemplateTypeParmTypeLoc TL) { 4808 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4809} 4810void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc( 4811 SubstTemplateTypeParmPackTypeLoc TL) { 4812 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4813} 4814void TypeLocReader::VisitTemplateSpecializationTypeLoc( 4815 TemplateSpecializationTypeLoc TL) { 4816 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx)); 4817 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx)); 4818 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 4819 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 4820 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 4821 TL.setArgLocInfo(i, 4822 Reader.GetTemplateArgumentLocInfo(F, 4823 TL.getTypePtr()->getArg(i).getKind(), 4824 Record, Idx)); 4825} 4826void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) { 4827 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 4828 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 4829} 4830void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 4831 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx)); 4832 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx)); 4833} 4834void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 4835 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4836} 4837void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 4838 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx)); 4839 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx)); 4840 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4841} 4842void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( 4843 DependentTemplateSpecializationTypeLoc TL) { 4844 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx)); 4845 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx)); 4846 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx)); 4847 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx)); 4848 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 4849 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 4850 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 4851 TL.setArgLocInfo(I, 4852 Reader.GetTemplateArgumentLocInfo(F, 4853 TL.getTypePtr()->getArg(I).getKind(), 4854 Record, Idx)); 4855} 4856void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 4857 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx)); 4858} 4859void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 4860 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4861} 4862void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 4863 TL.setHasBaseTypeAsWritten(Record[Idx++]); 4864 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 4865 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 4866 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 4867 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx)); 4868} 4869void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 4870 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 4871} 4872void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) { 4873 TL.setKWLoc(ReadSourceLocation(Record, Idx)); 4874 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 4875 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 4876} 4877 4878TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F, 4879 const RecordData &Record, 4880 unsigned &Idx) { 4881 QualType InfoTy = readType(F, Record, Idx); 4882 if (InfoTy.isNull()) 4883 return 0; 4884 4885 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy); 4886 TypeLocReader TLR(*this, F, Record, Idx); 4887 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc()) 4888 TLR.Visit(TL); 4889 return TInfo; 4890} 4891 4892QualType ASTReader::GetType(TypeID ID) { 4893 unsigned FastQuals = ID & Qualifiers::FastMask; 4894 unsigned Index = ID >> Qualifiers::FastWidth; 4895 4896 if (Index < NUM_PREDEF_TYPE_IDS) { 4897 QualType T; 4898 switch ((PredefinedTypeIDs)Index) { 4899 case PREDEF_TYPE_NULL_ID: return QualType(); 4900 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break; 4901 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break; 4902 4903 case PREDEF_TYPE_CHAR_U_ID: 4904 case PREDEF_TYPE_CHAR_S_ID: 4905 // FIXME: Check that the signedness of CharTy is correct! 4906 T = Context.CharTy; 4907 break; 4908 4909 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break; 4910 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break; 4911 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break; 4912 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break; 4913 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break; 4914 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break; 4915 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break; 4916 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break; 4917 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break; 4918 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break; 4919 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break; 4920 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break; 4921 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break; 4922 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break; 4923 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break; 4924 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break; 4925 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break; 4926 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break; 4927 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break; 4928 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break; 4929 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break; 4930 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break; 4931 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break; 4932 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break; 4933 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break; 4934 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break; 4935 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break; 4936 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break; 4937 case PREDEF_TYPE_IMAGE1D_ID: T = Context.OCLImage1dTy; break; 4938 case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break; 4939 case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break; 4940 case PREDEF_TYPE_IMAGE2D_ID: T = Context.OCLImage2dTy; break; 4941 case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break; 4942 case PREDEF_TYPE_IMAGE3D_ID: T = Context.OCLImage3dTy; break; 4943 case PREDEF_TYPE_SAMPLER_ID: T = Context.OCLSamplerTy; break; 4944 case PREDEF_TYPE_EVENT_ID: T = Context.OCLEventTy; break; 4945 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break; 4946 4947 case PREDEF_TYPE_AUTO_RREF_DEDUCT: 4948 T = Context.getAutoRRefDeductType(); 4949 break; 4950 4951 case PREDEF_TYPE_ARC_UNBRIDGED_CAST: 4952 T = Context.ARCUnbridgedCastTy; 4953 break; 4954 4955 case PREDEF_TYPE_VA_LIST_TAG: 4956 T = Context.getVaListTagType(); 4957 break; 4958 4959 case PREDEF_TYPE_BUILTIN_FN: 4960 T = Context.BuiltinFnTy; 4961 break; 4962 } 4963 4964 assert(!T.isNull() && "Unknown predefined type"); 4965 return T.withFastQualifiers(FastQuals); 4966 } 4967 4968 Index -= NUM_PREDEF_TYPE_IDS; 4969 assert(Index < TypesLoaded.size() && "Type index out-of-range"); 4970 if (TypesLoaded[Index].isNull()) { 4971 TypesLoaded[Index] = readTypeRecord(Index); 4972 if (TypesLoaded[Index].isNull()) 4973 return QualType(); 4974 4975 TypesLoaded[Index]->setFromAST(); 4976 if (DeserializationListener) 4977 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID), 4978 TypesLoaded[Index]); 4979 } 4980 4981 return TypesLoaded[Index].withFastQualifiers(FastQuals); 4982} 4983 4984QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) { 4985 return GetType(getGlobalTypeID(F, LocalID)); 4986} 4987 4988serialization::TypeID 4989ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const { 4990 unsigned FastQuals = LocalID & Qualifiers::FastMask; 4991 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth; 4992 4993 if (LocalIndex < NUM_PREDEF_TYPE_IDS) 4994 return LocalID; 4995 4996 ContinuousRangeMap<uint32_t, int, 2>::iterator I 4997 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS); 4998 assert(I != F.TypeRemap.end() && "Invalid index into type index remap"); 4999 5000 unsigned GlobalIndex = LocalIndex + I->second; 5001 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals; 5002} 5003 5004TemplateArgumentLocInfo 5005ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F, 5006 TemplateArgument::ArgKind Kind, 5007 const RecordData &Record, 5008 unsigned &Index) { 5009 switch (Kind) { 5010 case TemplateArgument::Expression: 5011 return ReadExpr(F); 5012 case TemplateArgument::Type: 5013 return GetTypeSourceInfo(F, Record, Index); 5014 case TemplateArgument::Template: { 5015 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, 5016 Index); 5017 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index); 5018 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 5019 SourceLocation()); 5020 } 5021 case TemplateArgument::TemplateExpansion: { 5022 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, 5023 Index); 5024 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index); 5025 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index); 5026 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 5027 EllipsisLoc); 5028 } 5029 case TemplateArgument::Null: 5030 case TemplateArgument::Integral: 5031 case TemplateArgument::Declaration: 5032 case TemplateArgument::NullPtr: 5033 case TemplateArgument::Pack: 5034 // FIXME: Is this right? 5035 return TemplateArgumentLocInfo(); 5036 } 5037 llvm_unreachable("unexpected template argument loc"); 5038} 5039 5040TemplateArgumentLoc 5041ASTReader::ReadTemplateArgumentLoc(ModuleFile &F, 5042 const RecordData &Record, unsigned &Index) { 5043 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index); 5044 5045 if (Arg.getKind() == TemplateArgument::Expression) { 5046 if (Record[Index++]) // bool InfoHasSameExpr. 5047 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); 5048 } 5049 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(), 5050 Record, Index)); 5051} 5052 5053Decl *ASTReader::GetExternalDecl(uint32_t ID) { 5054 return GetDecl(ID); 5055} 5056 5057uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record, 5058 unsigned &Idx){ 5059 if (Idx >= Record.size()) 5060 return 0; 5061 5062 unsigned LocalID = Record[Idx++]; 5063 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]); 5064} 5065 5066CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) { 5067 RecordLocation Loc = getLocalBitOffset(Offset); 5068 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 5069 SavedStreamPosition SavedPosition(Cursor); 5070 Cursor.JumpToBit(Loc.Offset); 5071 ReadingKindTracker ReadingKind(Read_Decl, *this); 5072 RecordData Record; 5073 unsigned Code = Cursor.ReadCode(); 5074 unsigned RecCode = Cursor.readRecord(Code, Record); 5075 if (RecCode != DECL_CXX_BASE_SPECIFIERS) { 5076 Error("Malformed AST file: missing C++ base specifiers"); 5077 return 0; 5078 } 5079 5080 unsigned Idx = 0; 5081 unsigned NumBases = Record[Idx++]; 5082 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases); 5083 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases]; 5084 for (unsigned I = 0; I != NumBases; ++I) 5085 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx); 5086 return Bases; 5087} 5088 5089serialization::DeclID 5090ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const { 5091 if (LocalID < NUM_PREDEF_DECL_IDS) 5092 return LocalID; 5093 5094 ContinuousRangeMap<uint32_t, int, 2>::iterator I 5095 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS); 5096 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap"); 5097 5098 return LocalID + I->second; 5099} 5100 5101bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID, 5102 ModuleFile &M) const { 5103 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID); 5104 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 5105 return &M == I->second; 5106} 5107 5108ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) { 5109 if (!D->isFromASTFile()) 5110 return 0; 5111 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID()); 5112 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 5113 return I->second; 5114} 5115 5116SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) { 5117 if (ID < NUM_PREDEF_DECL_IDS) 5118 return SourceLocation(); 5119 5120 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 5121 5122 if (Index > DeclsLoaded.size()) { 5123 Error("declaration ID out-of-range for AST file"); 5124 return SourceLocation(); 5125 } 5126 5127 if (Decl *D = DeclsLoaded[Index]) 5128 return D->getLocation(); 5129 5130 unsigned RawLocation = 0; 5131 RecordLocation Rec = DeclCursorForID(ID, RawLocation); 5132 return ReadSourceLocation(*Rec.F, RawLocation); 5133} 5134 5135Decl *ASTReader::GetDecl(DeclID ID) { 5136 if (ID < NUM_PREDEF_DECL_IDS) { 5137 switch ((PredefinedDeclIDs)ID) { 5138 case PREDEF_DECL_NULL_ID: 5139 return 0; 5140 5141 case PREDEF_DECL_TRANSLATION_UNIT_ID: 5142 return Context.getTranslationUnitDecl(); 5143 5144 case PREDEF_DECL_OBJC_ID_ID: 5145 return Context.getObjCIdDecl(); 5146 5147 case PREDEF_DECL_OBJC_SEL_ID: 5148 return Context.getObjCSelDecl(); 5149 5150 case PREDEF_DECL_OBJC_CLASS_ID: 5151 return Context.getObjCClassDecl(); 5152 5153 case PREDEF_DECL_OBJC_PROTOCOL_ID: 5154 return Context.getObjCProtocolDecl(); 5155 5156 case PREDEF_DECL_INT_128_ID: 5157 return Context.getInt128Decl(); 5158 5159 case PREDEF_DECL_UNSIGNED_INT_128_ID: 5160 return Context.getUInt128Decl(); 5161 5162 case PREDEF_DECL_OBJC_INSTANCETYPE_ID: 5163 return Context.getObjCInstanceTypeDecl(); 5164 5165 case PREDEF_DECL_BUILTIN_VA_LIST_ID: 5166 return Context.getBuiltinVaListDecl(); 5167 } 5168 } 5169 5170 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 5171 5172 if (Index >= DeclsLoaded.size()) { 5173 assert(0 && "declaration ID out-of-range for AST file"); 5174 Error("declaration ID out-of-range for AST file"); 5175 return 0; 5176 } 5177 5178 if (!DeclsLoaded[Index]) { 5179 ReadDeclRecord(ID); 5180 if (DeserializationListener) 5181 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); 5182 } 5183 5184 return DeclsLoaded[Index]; 5185} 5186 5187DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M, 5188 DeclID GlobalID) { 5189 if (GlobalID < NUM_PREDEF_DECL_IDS) 5190 return GlobalID; 5191 5192 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID); 5193 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 5194 ModuleFile *Owner = I->second; 5195 5196 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos 5197 = M.GlobalToLocalDeclIDs.find(Owner); 5198 if (Pos == M.GlobalToLocalDeclIDs.end()) 5199 return 0; 5200 5201 return GlobalID - Owner->BaseDeclID + Pos->second; 5202} 5203 5204serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F, 5205 const RecordData &Record, 5206 unsigned &Idx) { 5207 if (Idx >= Record.size()) { 5208 Error("Corrupted AST file"); 5209 return 0; 5210 } 5211 5212 return getGlobalDeclID(F, Record[Idx++]); 5213} 5214 5215/// \brief Resolve the offset of a statement into a statement. 5216/// 5217/// This operation will read a new statement from the external 5218/// source each time it is called, and is meant to be used via a 5219/// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 5220Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) { 5221 // Switch case IDs are per Decl. 5222 ClearSwitchCaseIDs(); 5223 5224 // Offset here is a global offset across the entire chain. 5225 RecordLocation Loc = getLocalBitOffset(Offset); 5226 Loc.F->DeclsCursor.JumpToBit(Loc.Offset); 5227 return ReadStmtFromStream(*Loc.F); 5228} 5229 5230namespace { 5231 class FindExternalLexicalDeclsVisitor { 5232 ASTReader &Reader; 5233 const DeclContext *DC; 5234 bool (*isKindWeWant)(Decl::Kind); 5235 5236 SmallVectorImpl<Decl*> &Decls; 5237 bool PredefsVisited[NUM_PREDEF_DECL_IDS]; 5238 5239 public: 5240 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC, 5241 bool (*isKindWeWant)(Decl::Kind), 5242 SmallVectorImpl<Decl*> &Decls) 5243 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls) 5244 { 5245 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I) 5246 PredefsVisited[I] = false; 5247 } 5248 5249 static bool visit(ModuleFile &M, bool Preorder, void *UserData) { 5250 if (Preorder) 5251 return false; 5252 5253 FindExternalLexicalDeclsVisitor *This 5254 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData); 5255 5256 ModuleFile::DeclContextInfosMap::iterator Info 5257 = M.DeclContextInfos.find(This->DC); 5258 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls) 5259 return false; 5260 5261 // Load all of the declaration IDs 5262 for (const KindDeclIDPair *ID = Info->second.LexicalDecls, 5263 *IDE = ID + Info->second.NumLexicalDecls; 5264 ID != IDE; ++ID) { 5265 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first)) 5266 continue; 5267 5268 // Don't add predefined declarations to the lexical context more 5269 // than once. 5270 if (ID->second < NUM_PREDEF_DECL_IDS) { 5271 if (This->PredefsVisited[ID->second]) 5272 continue; 5273 5274 This->PredefsVisited[ID->second] = true; 5275 } 5276 5277 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) { 5278 if (!This->DC->isDeclInLexicalTraversal(D)) 5279 This->Decls.push_back(D); 5280 } 5281 } 5282 5283 return false; 5284 } 5285 }; 5286} 5287 5288ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC, 5289 bool (*isKindWeWant)(Decl::Kind), 5290 SmallVectorImpl<Decl*> &Decls) { 5291 // There might be lexical decls in multiple modules, for the TU at 5292 // least. Walk all of the modules in the order they were loaded. 5293 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls); 5294 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor); 5295 ++NumLexicalDeclContextsRead; 5296 return ELR_Success; 5297} 5298 5299namespace { 5300 5301class DeclIDComp { 5302 ASTReader &Reader; 5303 ModuleFile &Mod; 5304 5305public: 5306 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {} 5307 5308 bool operator()(LocalDeclID L, LocalDeclID R) const { 5309 SourceLocation LHS = getLocation(L); 5310 SourceLocation RHS = getLocation(R); 5311 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 5312 } 5313 5314 bool operator()(SourceLocation LHS, LocalDeclID R) const { 5315 SourceLocation RHS = getLocation(R); 5316 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 5317 } 5318 5319 bool operator()(LocalDeclID L, SourceLocation RHS) const { 5320 SourceLocation LHS = getLocation(L); 5321 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 5322 } 5323 5324 SourceLocation getLocation(LocalDeclID ID) const { 5325 return Reader.getSourceManager().getFileLoc( 5326 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID))); 5327 } 5328}; 5329 5330} 5331 5332void ASTReader::FindFileRegionDecls(FileID File, 5333 unsigned Offset, unsigned Length, 5334 SmallVectorImpl<Decl *> &Decls) { 5335 SourceManager &SM = getSourceManager(); 5336 5337 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File); 5338 if (I == FileDeclIDs.end()) 5339 return; 5340 5341 FileDeclsInfo &DInfo = I->second; 5342 if (DInfo.Decls.empty()) 5343 return; 5344 5345 SourceLocation 5346 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset); 5347 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length); 5348 5349 DeclIDComp DIDComp(*this, *DInfo.Mod); 5350 ArrayRef<serialization::LocalDeclID>::iterator 5351 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(), 5352 BeginLoc, DIDComp); 5353 if (BeginIt != DInfo.Decls.begin()) 5354 --BeginIt; 5355 5356 // If we are pointing at a top-level decl inside an objc container, we need 5357 // to backtrack until we find it otherwise we will fail to report that the 5358 // region overlaps with an objc container. 5359 while (BeginIt != DInfo.Decls.begin() && 5360 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt)) 5361 ->isTopLevelDeclInObjCContainer()) 5362 --BeginIt; 5363 5364 ArrayRef<serialization::LocalDeclID>::iterator 5365 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(), 5366 EndLoc, DIDComp); 5367 if (EndIt != DInfo.Decls.end()) 5368 ++EndIt; 5369 5370 for (ArrayRef<serialization::LocalDeclID>::iterator 5371 DIt = BeginIt; DIt != EndIt; ++DIt) 5372 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt))); 5373} 5374 5375namespace { 5376 /// \brief ModuleFile visitor used to perform name lookup into a 5377 /// declaration context. 5378 class DeclContextNameLookupVisitor { 5379 ASTReader &Reader; 5380 SmallVectorImpl<const DeclContext *> &Contexts; 5381 DeclarationName Name; 5382 SmallVectorImpl<NamedDecl *> &Decls; 5383 5384 public: 5385 DeclContextNameLookupVisitor(ASTReader &Reader, 5386 SmallVectorImpl<const DeclContext *> &Contexts, 5387 DeclarationName Name, 5388 SmallVectorImpl<NamedDecl *> &Decls) 5389 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { } 5390 5391 static bool visit(ModuleFile &M, void *UserData) { 5392 DeclContextNameLookupVisitor *This 5393 = static_cast<DeclContextNameLookupVisitor *>(UserData); 5394 5395 // Check whether we have any visible declaration information for 5396 // this context in this module. 5397 ModuleFile::DeclContextInfosMap::iterator Info; 5398 bool FoundInfo = false; 5399 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) { 5400 Info = M.DeclContextInfos.find(This->Contexts[I]); 5401 if (Info != M.DeclContextInfos.end() && 5402 Info->second.NameLookupTableData) { 5403 FoundInfo = true; 5404 break; 5405 } 5406 } 5407 5408 if (!FoundInfo) 5409 return false; 5410 5411 // Look for this name within this module. 5412 ASTDeclContextNameLookupTable *LookupTable = 5413 Info->second.NameLookupTableData; 5414 ASTDeclContextNameLookupTable::iterator Pos 5415 = LookupTable->find(This->Name); 5416 if (Pos == LookupTable->end()) 5417 return false; 5418 5419 bool FoundAnything = false; 5420 ASTDeclContextNameLookupTrait::data_type Data = *Pos; 5421 for (; Data.first != Data.second; ++Data.first) { 5422 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first); 5423 if (!ND) 5424 continue; 5425 5426 if (ND->getDeclName() != This->Name) { 5427 // A name might be null because the decl's redeclarable part is 5428 // currently read before reading its name. The lookup is triggered by 5429 // building that decl (likely indirectly), and so it is later in the 5430 // sense of "already existing" and can be ignored here. 5431 continue; 5432 } 5433 5434 // Record this declaration. 5435 FoundAnything = true; 5436 This->Decls.push_back(ND); 5437 } 5438 5439 return FoundAnything; 5440 } 5441 }; 5442} 5443 5444/// \brief Retrieve the "definitive" module file for the definition of the 5445/// given declaration context, if there is one. 5446/// 5447/// The "definitive" module file is the only place where we need to look to 5448/// find information about the declarations within the given declaration 5449/// context. For example, C++ and Objective-C classes, C structs/unions, and 5450/// Objective-C protocols, categories, and extensions are all defined in a 5451/// single place in the source code, so they have definitive module files 5452/// associated with them. C++ namespaces, on the other hand, can have 5453/// definitions in multiple different module files. 5454/// 5455/// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's 5456/// NDEBUG checking. 5457static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC, 5458 ASTReader &Reader) { 5459 if (const DeclContext *DefDC = getDefinitiveDeclContext(DC)) 5460 return Reader.getOwningModuleFile(cast<Decl>(DefDC)); 5461 5462 return 0; 5463} 5464 5465bool 5466ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC, 5467 DeclarationName Name) { 5468 assert(DC->hasExternalVisibleStorage() && 5469 "DeclContext has no visible decls in storage"); 5470 if (!Name) 5471 return false; 5472 5473 SmallVector<NamedDecl *, 64> Decls; 5474 5475 // Compute the declaration contexts we need to look into. Multiple such 5476 // declaration contexts occur when two declaration contexts from disjoint 5477 // modules get merged, e.g., when two namespaces with the same name are 5478 // independently defined in separate modules. 5479 SmallVector<const DeclContext *, 2> Contexts; 5480 Contexts.push_back(DC); 5481 5482 if (DC->isNamespace()) { 5483 MergedDeclsMap::iterator Merged 5484 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC))); 5485 if (Merged != MergedDecls.end()) { 5486 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I) 5487 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I]))); 5488 } 5489 } 5490 5491 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls); 5492 5493 // If we can definitively determine which module file to look into, 5494 // only look there. Otherwise, look in all module files. 5495 ModuleFile *Definitive; 5496 if (Contexts.size() == 1 && 5497 (Definitive = getDefinitiveModuleFileFor(DC, *this))) { 5498 DeclContextNameLookupVisitor::visit(*Definitive, &Visitor); 5499 } else { 5500 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor); 5501 } 5502 ++NumVisibleDeclContextsRead; 5503 SetExternalVisibleDeclsForName(DC, Name, Decls); 5504 return !Decls.empty(); 5505} 5506 5507namespace { 5508 /// \brief ModuleFile visitor used to retrieve all visible names in a 5509 /// declaration context. 5510 class DeclContextAllNamesVisitor { 5511 ASTReader &Reader; 5512 SmallVectorImpl<const DeclContext *> &Contexts; 5513 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls; 5514 bool VisitAll; 5515 5516 public: 5517 DeclContextAllNamesVisitor(ASTReader &Reader, 5518 SmallVectorImpl<const DeclContext *> &Contexts, 5519 llvm::DenseMap<DeclarationName, 5520 SmallVector<NamedDecl *, 8> > &Decls, 5521 bool VisitAll) 5522 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { } 5523 5524 static bool visit(ModuleFile &M, void *UserData) { 5525 DeclContextAllNamesVisitor *This 5526 = static_cast<DeclContextAllNamesVisitor *>(UserData); 5527 5528 // Check whether we have any visible declaration information for 5529 // this context in this module. 5530 ModuleFile::DeclContextInfosMap::iterator Info; 5531 bool FoundInfo = false; 5532 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) { 5533 Info = M.DeclContextInfos.find(This->Contexts[I]); 5534 if (Info != M.DeclContextInfos.end() && 5535 Info->second.NameLookupTableData) { 5536 FoundInfo = true; 5537 break; 5538 } 5539 } 5540 5541 if (!FoundInfo) 5542 return false; 5543 5544 ASTDeclContextNameLookupTable *LookupTable = 5545 Info->second.NameLookupTableData; 5546 bool FoundAnything = false; 5547 for (ASTDeclContextNameLookupTable::data_iterator 5548 I = LookupTable->data_begin(), E = LookupTable->data_end(); 5549 I != E; 5550 ++I) { 5551 ASTDeclContextNameLookupTrait::data_type Data = *I; 5552 for (; Data.first != Data.second; ++Data.first) { 5553 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, 5554 *Data.first); 5555 if (!ND) 5556 continue; 5557 5558 // Record this declaration. 5559 FoundAnything = true; 5560 This->Decls[ND->getDeclName()].push_back(ND); 5561 } 5562 } 5563 5564 return FoundAnything && !This->VisitAll; 5565 } 5566 }; 5567} 5568 5569void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) { 5570 if (!DC->hasExternalVisibleStorage()) 5571 return; 5572 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > Decls; 5573 5574 // Compute the declaration contexts we need to look into. Multiple such 5575 // declaration contexts occur when two declaration contexts from disjoint 5576 // modules get merged, e.g., when two namespaces with the same name are 5577 // independently defined in separate modules. 5578 SmallVector<const DeclContext *, 2> Contexts; 5579 Contexts.push_back(DC); 5580 5581 if (DC->isNamespace()) { 5582 MergedDeclsMap::iterator Merged 5583 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC))); 5584 if (Merged != MergedDecls.end()) { 5585 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I) 5586 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I]))); 5587 } 5588 } 5589 5590 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls, 5591 /*VisitAll=*/DC->isFileContext()); 5592 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor); 5593 ++NumVisibleDeclContextsRead; 5594 5595 for (llvm::DenseMap<DeclarationName, 5596 SmallVector<NamedDecl *, 8> >::iterator 5597 I = Decls.begin(), E = Decls.end(); I != E; ++I) { 5598 SetExternalVisibleDeclsForName(DC, I->first, I->second); 5599 } 5600 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false); 5601} 5602 5603/// \brief Under non-PCH compilation the consumer receives the objc methods 5604/// before receiving the implementation, and codegen depends on this. 5605/// We simulate this by deserializing and passing to consumer the methods of the 5606/// implementation before passing the deserialized implementation decl. 5607static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD, 5608 ASTConsumer *Consumer) { 5609 assert(ImplD && Consumer); 5610 5611 for (ObjCImplDecl::method_iterator 5612 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I) 5613 Consumer->HandleInterestingDecl(DeclGroupRef(*I)); 5614 5615 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD)); 5616} 5617 5618void ASTReader::PassInterestingDeclsToConsumer() { 5619 assert(Consumer); 5620 while (!InterestingDecls.empty()) { 5621 Decl *D = InterestingDecls.front(); 5622 InterestingDecls.pop_front(); 5623 5624 PassInterestingDeclToConsumer(D); 5625 } 5626} 5627 5628void ASTReader::PassInterestingDeclToConsumer(Decl *D) { 5629 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) 5630 PassObjCImplDeclToConsumer(ImplD, Consumer); 5631 else 5632 Consumer->HandleInterestingDecl(DeclGroupRef(D)); 5633} 5634 5635void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) { 5636 this->Consumer = Consumer; 5637 5638 if (!Consumer) 5639 return; 5640 5641 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) { 5642 // Force deserialization of this decl, which will cause it to be queued for 5643 // passing to the consumer. 5644 GetDecl(ExternalDefinitions[I]); 5645 } 5646 ExternalDefinitions.clear(); 5647 5648 PassInterestingDeclsToConsumer(); 5649} 5650 5651void ASTReader::PrintStats() { 5652 std::fprintf(stderr, "*** AST File Statistics:\n"); 5653 5654 unsigned NumTypesLoaded 5655 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), 5656 QualType()); 5657 unsigned NumDeclsLoaded 5658 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), 5659 (Decl *)0); 5660 unsigned NumIdentifiersLoaded 5661 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), 5662 IdentifiersLoaded.end(), 5663 (IdentifierInfo *)0); 5664 unsigned NumMacrosLoaded 5665 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(), 5666 MacrosLoaded.end(), 5667 (MacroDirective *)0); 5668 unsigned NumSelectorsLoaded 5669 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), 5670 SelectorsLoaded.end(), 5671 Selector()); 5672 5673 if (unsigned TotalNumSLocEntries = getTotalNumSLocs()) 5674 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", 5675 NumSLocEntriesRead, TotalNumSLocEntries, 5676 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); 5677 if (!TypesLoaded.empty()) 5678 std::fprintf(stderr, " %u/%u types read (%f%%)\n", 5679 NumTypesLoaded, (unsigned)TypesLoaded.size(), 5680 ((float)NumTypesLoaded/TypesLoaded.size() * 100)); 5681 if (!DeclsLoaded.empty()) 5682 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", 5683 NumDeclsLoaded, (unsigned)DeclsLoaded.size(), 5684 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); 5685 if (!IdentifiersLoaded.empty()) 5686 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", 5687 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), 5688 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); 5689 if (!MacrosLoaded.empty()) 5690 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 5691 NumMacrosLoaded, (unsigned)MacrosLoaded.size(), 5692 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100)); 5693 if (!SelectorsLoaded.empty()) 5694 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", 5695 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(), 5696 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100)); 5697 if (TotalNumStatements) 5698 std::fprintf(stderr, " %u/%u statements read (%f%%)\n", 5699 NumStatementsRead, TotalNumStatements, 5700 ((float)NumStatementsRead/TotalNumStatements * 100)); 5701 if (TotalNumMacros) 5702 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 5703 NumMacrosRead, TotalNumMacros, 5704 ((float)NumMacrosRead/TotalNumMacros * 100)); 5705 if (TotalLexicalDeclContexts) 5706 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", 5707 NumLexicalDeclContextsRead, TotalLexicalDeclContexts, 5708 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts 5709 * 100)); 5710 if (TotalVisibleDeclContexts) 5711 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", 5712 NumVisibleDeclContextsRead, TotalVisibleDeclContexts, 5713 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts 5714 * 100)); 5715 if (TotalNumMethodPoolEntries) { 5716 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", 5717 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries, 5718 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries 5719 * 100)); 5720 } 5721 if (NumMethodPoolLookups) { 5722 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n", 5723 NumMethodPoolHits, NumMethodPoolLookups, 5724 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0)); 5725 } 5726 if (NumMethodPoolTableLookups) { 5727 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n", 5728 NumMethodPoolTableHits, NumMethodPoolTableLookups, 5729 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups 5730 * 100.0)); 5731 } 5732 5733 if (NumIdentifierLookupHits) { 5734 std::fprintf(stderr, 5735 " %u / %u identifier table lookups succeeded (%f%%)\n", 5736 NumIdentifierLookupHits, NumIdentifierLookups, 5737 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups); 5738 } 5739 5740 if (GlobalIndex) { 5741 std::fprintf(stderr, "\n"); 5742 GlobalIndex->printStats(); 5743 } 5744 5745 std::fprintf(stderr, "\n"); 5746 dump(); 5747 std::fprintf(stderr, "\n"); 5748} 5749 5750template<typename Key, typename ModuleFile, unsigned InitialCapacity> 5751static void 5752dumpModuleIDMap(StringRef Name, 5753 const ContinuousRangeMap<Key, ModuleFile *, 5754 InitialCapacity> &Map) { 5755 if (Map.begin() == Map.end()) 5756 return; 5757 5758 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType; 5759 llvm::errs() << Name << ":\n"; 5760 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end(); 5761 I != IEnd; ++I) { 5762 llvm::errs() << " " << I->first << " -> " << I->second->FileName 5763 << "\n"; 5764 } 5765} 5766 5767void ASTReader::dump() { 5768 llvm::errs() << "*** PCH/ModuleFile Remappings:\n"; 5769 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap); 5770 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap); 5771 dumpModuleIDMap("Global type map", GlobalTypeMap); 5772 dumpModuleIDMap("Global declaration map", GlobalDeclMap); 5773 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap); 5774 dumpModuleIDMap("Global macro map", GlobalMacroMap); 5775 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap); 5776 dumpModuleIDMap("Global selector map", GlobalSelectorMap); 5777 dumpModuleIDMap("Global preprocessed entity map", 5778 GlobalPreprocessedEntityMap); 5779 5780 llvm::errs() << "\n*** PCH/Modules Loaded:"; 5781 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(), 5782 MEnd = ModuleMgr.end(); 5783 M != MEnd; ++M) 5784 (*M)->dump(); 5785} 5786 5787/// Return the amount of memory used by memory buffers, breaking down 5788/// by heap-backed versus mmap'ed memory. 5789void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const { 5790 for (ModuleConstIterator I = ModuleMgr.begin(), 5791 E = ModuleMgr.end(); I != E; ++I) { 5792 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) { 5793 size_t bytes = buf->getBufferSize(); 5794 switch (buf->getBufferKind()) { 5795 case llvm::MemoryBuffer::MemoryBuffer_Malloc: 5796 sizes.malloc_bytes += bytes; 5797 break; 5798 case llvm::MemoryBuffer::MemoryBuffer_MMap: 5799 sizes.mmap_bytes += bytes; 5800 break; 5801 } 5802 } 5803 } 5804} 5805 5806void ASTReader::InitializeSema(Sema &S) { 5807 SemaObj = &S; 5808 S.addExternalSource(this); 5809 5810 // Makes sure any declarations that were deserialized "too early" 5811 // still get added to the identifier's declaration chains. 5812 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) { 5813 NamedDecl *ND = cast<NamedDecl>(PreloadedDecls[I]->getMostRecentDecl()); 5814 SemaObj->pushExternalDeclIntoScope(ND, PreloadedDecls[I]->getDeclName()); 5815 } 5816 PreloadedDecls.clear(); 5817 5818 // Load the offsets of the declarations that Sema references. 5819 // They will be lazily deserialized when needed. 5820 if (!SemaDeclRefs.empty()) { 5821 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!"); 5822 if (!SemaObj->StdNamespace) 5823 SemaObj->StdNamespace = SemaDeclRefs[0]; 5824 if (!SemaObj->StdBadAlloc) 5825 SemaObj->StdBadAlloc = SemaDeclRefs[1]; 5826 } 5827 5828 if (!FPPragmaOptions.empty()) { 5829 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS"); 5830 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0]; 5831 } 5832 5833 if (!OpenCLExtensions.empty()) { 5834 unsigned I = 0; 5835#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++]; 5836#include "clang/Basic/OpenCLExtensions.def" 5837 5838 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS"); 5839 } 5840} 5841 5842IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) { 5843 // Note that we are loading an identifier. 5844 Deserializing AnIdentifier(this); 5845 StringRef Name(NameStart, NameEnd - NameStart); 5846 5847 // If there is a global index, look there first to determine which modules 5848 // provably do not have any results for this identifier. 5849 GlobalModuleIndex::HitSet Hits; 5850 GlobalModuleIndex::HitSet *HitsPtr = 0; 5851 if (!loadGlobalIndex()) { 5852 if (GlobalIndex->lookupIdentifier(Name, Hits)) { 5853 HitsPtr = &Hits; 5854 } 5855 } 5856 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0, 5857 NumIdentifierLookups, 5858 NumIdentifierLookupHits); 5859 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr); 5860 IdentifierInfo *II = Visitor.getIdentifierInfo(); 5861 markIdentifierUpToDate(II); 5862 return II; 5863} 5864 5865namespace clang { 5866 /// \brief An identifier-lookup iterator that enumerates all of the 5867 /// identifiers stored within a set of AST files. 5868 class ASTIdentifierIterator : public IdentifierIterator { 5869 /// \brief The AST reader whose identifiers are being enumerated. 5870 const ASTReader &Reader; 5871 5872 /// \brief The current index into the chain of AST files stored in 5873 /// the AST reader. 5874 unsigned Index; 5875 5876 /// \brief The current position within the identifier lookup table 5877 /// of the current AST file. 5878 ASTIdentifierLookupTable::key_iterator Current; 5879 5880 /// \brief The end position within the identifier lookup table of 5881 /// the current AST file. 5882 ASTIdentifierLookupTable::key_iterator End; 5883 5884 public: 5885 explicit ASTIdentifierIterator(const ASTReader &Reader); 5886 5887 virtual StringRef Next(); 5888 }; 5889} 5890 5891ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader) 5892 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) { 5893 ASTIdentifierLookupTable *IdTable 5894 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable; 5895 Current = IdTable->key_begin(); 5896 End = IdTable->key_end(); 5897} 5898 5899StringRef ASTIdentifierIterator::Next() { 5900 while (Current == End) { 5901 // If we have exhausted all of our AST files, we're done. 5902 if (Index == 0) 5903 return StringRef(); 5904 5905 --Index; 5906 ASTIdentifierLookupTable *IdTable 5907 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index]. 5908 IdentifierLookupTable; 5909 Current = IdTable->key_begin(); 5910 End = IdTable->key_end(); 5911 } 5912 5913 // We have any identifiers remaining in the current AST file; return 5914 // the next one. 5915 StringRef Result = *Current; 5916 ++Current; 5917 return Result; 5918} 5919 5920IdentifierIterator *ASTReader::getIdentifiers() const { 5921 return new ASTIdentifierIterator(*this); 5922} 5923 5924namespace clang { namespace serialization { 5925 class ReadMethodPoolVisitor { 5926 ASTReader &Reader; 5927 Selector Sel; 5928 unsigned PriorGeneration; 5929 SmallVector<ObjCMethodDecl *, 4> InstanceMethods; 5930 SmallVector<ObjCMethodDecl *, 4> FactoryMethods; 5931 5932 public: 5933 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel, 5934 unsigned PriorGeneration) 5935 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) { } 5936 5937 static bool visit(ModuleFile &M, void *UserData) { 5938 ReadMethodPoolVisitor *This 5939 = static_cast<ReadMethodPoolVisitor *>(UserData); 5940 5941 if (!M.SelectorLookupTable) 5942 return false; 5943 5944 // If we've already searched this module file, skip it now. 5945 if (M.Generation <= This->PriorGeneration) 5946 return true; 5947 5948 ++This->Reader.NumMethodPoolTableLookups; 5949 ASTSelectorLookupTable *PoolTable 5950 = (ASTSelectorLookupTable*)M.SelectorLookupTable; 5951 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel); 5952 if (Pos == PoolTable->end()) 5953 return false; 5954 5955 ++This->Reader.NumMethodPoolTableHits; 5956 ++This->Reader.NumSelectorsRead; 5957 // FIXME: Not quite happy with the statistics here. We probably should 5958 // disable this tracking when called via LoadSelector. 5959 // Also, should entries without methods count as misses? 5960 ++This->Reader.NumMethodPoolEntriesRead; 5961 ASTSelectorLookupTrait::data_type Data = *Pos; 5962 if (This->Reader.DeserializationListener) 5963 This->Reader.DeserializationListener->SelectorRead(Data.ID, 5964 This->Sel); 5965 5966 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end()); 5967 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end()); 5968 return true; 5969 } 5970 5971 /// \brief Retrieve the instance methods found by this visitor. 5972 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const { 5973 return InstanceMethods; 5974 } 5975 5976 /// \brief Retrieve the instance methods found by this visitor. 5977 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const { 5978 return FactoryMethods; 5979 } 5980 }; 5981} } // end namespace clang::serialization 5982 5983/// \brief Add the given set of methods to the method list. 5984static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods, 5985 ObjCMethodList &List) { 5986 for (unsigned I = 0, N = Methods.size(); I != N; ++I) { 5987 S.addMethodToGlobalList(&List, Methods[I]); 5988 } 5989} 5990 5991void ASTReader::ReadMethodPool(Selector Sel) { 5992 // Get the selector generation and update it to the current generation. 5993 unsigned &Generation = SelectorGeneration[Sel]; 5994 unsigned PriorGeneration = Generation; 5995 Generation = CurrentGeneration; 5996 5997 // Search for methods defined with this selector. 5998 ++NumMethodPoolLookups; 5999 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration); 6000 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor); 6001 6002 if (Visitor.getInstanceMethods().empty() && 6003 Visitor.getFactoryMethods().empty()) 6004 return; 6005 6006 ++NumMethodPoolHits; 6007 6008 if (!getSema()) 6009 return; 6010 6011 Sema &S = *getSema(); 6012 Sema::GlobalMethodPool::iterator Pos 6013 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first; 6014 6015 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first); 6016 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second); 6017} 6018 6019void ASTReader::ReadKnownNamespaces( 6020 SmallVectorImpl<NamespaceDecl *> &Namespaces) { 6021 Namespaces.clear(); 6022 6023 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) { 6024 if (NamespaceDecl *Namespace 6025 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I]))) 6026 Namespaces.push_back(Namespace); 6027 } 6028} 6029 6030void ASTReader::ReadUndefinedButUsed( 6031 llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) { 6032 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) { 6033 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++])); 6034 SourceLocation Loc = 6035 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]); 6036 Undefined.insert(std::make_pair(D, Loc)); 6037 } 6038} 6039 6040void ASTReader::ReadTentativeDefinitions( 6041 SmallVectorImpl<VarDecl *> &TentativeDefs) { 6042 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { 6043 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I])); 6044 if (Var) 6045 TentativeDefs.push_back(Var); 6046 } 6047 TentativeDefinitions.clear(); 6048} 6049 6050void ASTReader::ReadUnusedFileScopedDecls( 6051 SmallVectorImpl<const DeclaratorDecl *> &Decls) { 6052 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) { 6053 DeclaratorDecl *D 6054 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I])); 6055 if (D) 6056 Decls.push_back(D); 6057 } 6058 UnusedFileScopedDecls.clear(); 6059} 6060 6061void ASTReader::ReadDelegatingConstructors( 6062 SmallVectorImpl<CXXConstructorDecl *> &Decls) { 6063 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) { 6064 CXXConstructorDecl *D 6065 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I])); 6066 if (D) 6067 Decls.push_back(D); 6068 } 6069 DelegatingCtorDecls.clear(); 6070} 6071 6072void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) { 6073 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) { 6074 TypedefNameDecl *D 6075 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])); 6076 if (D) 6077 Decls.push_back(D); 6078 } 6079 ExtVectorDecls.clear(); 6080} 6081 6082void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) { 6083 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) { 6084 CXXRecordDecl *D 6085 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I])); 6086 if (D) 6087 Decls.push_back(D); 6088 } 6089 DynamicClasses.clear(); 6090} 6091 6092void 6093ASTReader::ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl *> &Decls) { 6094 for (unsigned I = 0, N = LocallyScopedExternCDecls.size(); I != N; ++I) { 6095 NamedDecl *D 6096 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternCDecls[I])); 6097 if (D) 6098 Decls.push_back(D); 6099 } 6100 LocallyScopedExternCDecls.clear(); 6101} 6102 6103void ASTReader::ReadReferencedSelectors( 6104 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) { 6105 if (ReferencedSelectorsData.empty()) 6106 return; 6107 6108 // If there are @selector references added them to its pool. This is for 6109 // implementation of -Wselector. 6110 unsigned int DataSize = ReferencedSelectorsData.size()-1; 6111 unsigned I = 0; 6112 while (I < DataSize) { 6113 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]); 6114 SourceLocation SelLoc 6115 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]); 6116 Sels.push_back(std::make_pair(Sel, SelLoc)); 6117 } 6118 ReferencedSelectorsData.clear(); 6119} 6120 6121void ASTReader::ReadWeakUndeclaredIdentifiers( 6122 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) { 6123 if (WeakUndeclaredIdentifiers.empty()) 6124 return; 6125 6126 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) { 6127 IdentifierInfo *WeakId 6128 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 6129 IdentifierInfo *AliasId 6130 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 6131 SourceLocation Loc 6132 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]); 6133 bool Used = WeakUndeclaredIdentifiers[I++]; 6134 WeakInfo WI(AliasId, Loc); 6135 WI.setUsed(Used); 6136 WeakIDs.push_back(std::make_pair(WeakId, WI)); 6137 } 6138 WeakUndeclaredIdentifiers.clear(); 6139} 6140 6141void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) { 6142 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) { 6143 ExternalVTableUse VT; 6144 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); 6145 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]); 6146 VT.DefinitionRequired = VTableUses[Idx++]; 6147 VTables.push_back(VT); 6148 } 6149 6150 VTableUses.clear(); 6151} 6152 6153void ASTReader::ReadPendingInstantiations( 6154 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) { 6155 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) { 6156 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++])); 6157 SourceLocation Loc 6158 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]); 6159 6160 Pending.push_back(std::make_pair(D, Loc)); 6161 } 6162 PendingInstantiations.clear(); 6163} 6164 6165void ASTReader::LoadSelector(Selector Sel) { 6166 // It would be complicated to avoid reading the methods anyway. So don't. 6167 ReadMethodPool(Sel); 6168} 6169 6170void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) { 6171 assert(ID && "Non-zero identifier ID required"); 6172 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); 6173 IdentifiersLoaded[ID - 1] = II; 6174 if (DeserializationListener) 6175 DeserializationListener->IdentifierRead(ID, II); 6176} 6177 6178/// \brief Set the globally-visible declarations associated with the given 6179/// identifier. 6180/// 6181/// If the AST reader is currently in a state where the given declaration IDs 6182/// cannot safely be resolved, they are queued until it is safe to resolve 6183/// them. 6184/// 6185/// \param II an IdentifierInfo that refers to one or more globally-visible 6186/// declarations. 6187/// 6188/// \param DeclIDs the set of declaration IDs with the name @p II that are 6189/// visible at global scope. 6190/// 6191/// \param Decls if non-null, this vector will be populated with the set of 6192/// deserialized declarations. These declarations will not be pushed into 6193/// scope. 6194void 6195ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, 6196 const SmallVectorImpl<uint32_t> &DeclIDs, 6197 SmallVectorImpl<Decl *> *Decls) { 6198 if (NumCurrentElementsDeserializing && !Decls) { 6199 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end()); 6200 return; 6201 } 6202 6203 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { 6204 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); 6205 if (SemaObj) { 6206 // If we're simply supposed to record the declarations, do so now. 6207 if (Decls) { 6208 Decls->push_back(D); 6209 continue; 6210 } 6211 6212 // Introduce this declaration into the translation-unit scope 6213 // and add it to the declaration chain for this identifier, so 6214 // that (unqualified) name lookup will find it. 6215 NamedDecl *ND = cast<NamedDecl>(D->getMostRecentDecl()); 6216 SemaObj->pushExternalDeclIntoScope(ND, II); 6217 } else { 6218 // Queue this declaration so that it will be added to the 6219 // translation unit scope and identifier's declaration chain 6220 // once a Sema object is known. 6221 PreloadedDecls.push_back(D); 6222 } 6223 } 6224} 6225 6226IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) { 6227 if (ID == 0) 6228 return 0; 6229 6230 if (IdentifiersLoaded.empty()) { 6231 Error("no identifier table in AST file"); 6232 return 0; 6233 } 6234 6235 ID -= 1; 6236 if (!IdentifiersLoaded[ID]) { 6237 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1); 6238 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map"); 6239 ModuleFile *M = I->second; 6240 unsigned Index = ID - M->BaseIdentifierID; 6241 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index]; 6242 6243 // All of the strings in the AST file are preceded by a 16-bit length. 6244 // Extract that 16-bit length to avoid having to execute strlen(). 6245 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as 6246 // unsigned integers. This is important to avoid integer overflow when 6247 // we cast them to 'unsigned'. 6248 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2; 6249 unsigned StrLen = (((unsigned) StrLenPtr[0]) 6250 | (((unsigned) StrLenPtr[1]) << 8)) - 1; 6251 IdentifiersLoaded[ID] 6252 = &PP.getIdentifierTable().get(StringRef(Str, StrLen)); 6253 if (DeserializationListener) 6254 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]); 6255 } 6256 6257 return IdentifiersLoaded[ID]; 6258} 6259 6260IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) { 6261 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID)); 6262} 6263 6264IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) { 6265 if (LocalID < NUM_PREDEF_IDENT_IDS) 6266 return LocalID; 6267 6268 ContinuousRangeMap<uint32_t, int, 2>::iterator I 6269 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS); 6270 assert(I != M.IdentifierRemap.end() 6271 && "Invalid index into identifier index remap"); 6272 6273 return LocalID + I->second; 6274} 6275 6276MacroDirective *ASTReader::getMacro(MacroID ID, MacroDirective *Hint) { 6277 if (ID == 0) 6278 return 0; 6279 6280 if (MacrosLoaded.empty()) { 6281 Error("no macro table in AST file"); 6282 return 0; 6283 } 6284 6285 ID -= NUM_PREDEF_MACRO_IDS; 6286 if (!MacrosLoaded[ID]) { 6287 GlobalMacroMapType::iterator I 6288 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS); 6289 assert(I != GlobalMacroMap.end() && "Corrupted global macro map"); 6290 ModuleFile *M = I->second; 6291 unsigned Index = ID - M->BaseMacroID; 6292 ReadMacroRecord(*M, M->MacroOffsets[Index], Hint); 6293 } 6294 6295 return MacrosLoaded[ID]; 6296} 6297 6298MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) { 6299 if (LocalID < NUM_PREDEF_MACRO_IDS) 6300 return LocalID; 6301 6302 ContinuousRangeMap<uint32_t, int, 2>::iterator I 6303 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS); 6304 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap"); 6305 6306 return LocalID + I->second; 6307} 6308 6309serialization::SubmoduleID 6310ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) { 6311 if (LocalID < NUM_PREDEF_SUBMODULE_IDS) 6312 return LocalID; 6313 6314 ContinuousRangeMap<uint32_t, int, 2>::iterator I 6315 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS); 6316 assert(I != M.SubmoduleRemap.end() 6317 && "Invalid index into submodule index remap"); 6318 6319 return LocalID + I->second; 6320} 6321 6322Module *ASTReader::getSubmodule(SubmoduleID GlobalID) { 6323 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) { 6324 assert(GlobalID == 0 && "Unhandled global submodule ID"); 6325 return 0; 6326 } 6327 6328 if (GlobalID > SubmodulesLoaded.size()) { 6329 Error("submodule ID out of range in AST file"); 6330 return 0; 6331 } 6332 6333 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS]; 6334} 6335 6336Module *ASTReader::getModule(unsigned ID) { 6337 return getSubmodule(ID); 6338} 6339 6340Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) { 6341 return DecodeSelector(getGlobalSelectorID(M, LocalID)); 6342} 6343 6344Selector ASTReader::DecodeSelector(serialization::SelectorID ID) { 6345 if (ID == 0) 6346 return Selector(); 6347 6348 if (ID > SelectorsLoaded.size()) { 6349 Error("selector ID out of range in AST file"); 6350 return Selector(); 6351 } 6352 6353 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) { 6354 // Load this selector from the selector table. 6355 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID); 6356 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map"); 6357 ModuleFile &M = *I->second; 6358 ASTSelectorLookupTrait Trait(*this, M); 6359 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS; 6360 SelectorsLoaded[ID - 1] = 6361 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0); 6362 if (DeserializationListener) 6363 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); 6364 } 6365 6366 return SelectorsLoaded[ID - 1]; 6367} 6368 6369Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) { 6370 return DecodeSelector(ID); 6371} 6372 6373uint32_t ASTReader::GetNumExternalSelectors() { 6374 // ID 0 (the null selector) is considered an external selector. 6375 return getTotalNumSelectors() + 1; 6376} 6377 6378serialization::SelectorID 6379ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const { 6380 if (LocalID < NUM_PREDEF_SELECTOR_IDS) 6381 return LocalID; 6382 6383 ContinuousRangeMap<uint32_t, int, 2>::iterator I 6384 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS); 6385 assert(I != M.SelectorRemap.end() 6386 && "Invalid index into selector index remap"); 6387 6388 return LocalID + I->second; 6389} 6390 6391DeclarationName 6392ASTReader::ReadDeclarationName(ModuleFile &F, 6393 const RecordData &Record, unsigned &Idx) { 6394 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++]; 6395 switch (Kind) { 6396 case DeclarationName::Identifier: 6397 return DeclarationName(GetIdentifierInfo(F, Record, Idx)); 6398 6399 case DeclarationName::ObjCZeroArgSelector: 6400 case DeclarationName::ObjCOneArgSelector: 6401 case DeclarationName::ObjCMultiArgSelector: 6402 return DeclarationName(ReadSelector(F, Record, Idx)); 6403 6404 case DeclarationName::CXXConstructorName: 6405 return Context.DeclarationNames.getCXXConstructorName( 6406 Context.getCanonicalType(readType(F, Record, Idx))); 6407 6408 case DeclarationName::CXXDestructorName: 6409 return Context.DeclarationNames.getCXXDestructorName( 6410 Context.getCanonicalType(readType(F, Record, Idx))); 6411 6412 case DeclarationName::CXXConversionFunctionName: 6413 return Context.DeclarationNames.getCXXConversionFunctionName( 6414 Context.getCanonicalType(readType(F, Record, Idx))); 6415 6416 case DeclarationName::CXXOperatorName: 6417 return Context.DeclarationNames.getCXXOperatorName( 6418 (OverloadedOperatorKind)Record[Idx++]); 6419 6420 case DeclarationName::CXXLiteralOperatorName: 6421 return Context.DeclarationNames.getCXXLiteralOperatorName( 6422 GetIdentifierInfo(F, Record, Idx)); 6423 6424 case DeclarationName::CXXUsingDirective: 6425 return DeclarationName::getUsingDirectiveName(); 6426 } 6427 6428 llvm_unreachable("Invalid NameKind!"); 6429} 6430 6431void ASTReader::ReadDeclarationNameLoc(ModuleFile &F, 6432 DeclarationNameLoc &DNLoc, 6433 DeclarationName Name, 6434 const RecordData &Record, unsigned &Idx) { 6435 switch (Name.getNameKind()) { 6436 case DeclarationName::CXXConstructorName: 6437 case DeclarationName::CXXDestructorName: 6438 case DeclarationName::CXXConversionFunctionName: 6439 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx); 6440 break; 6441 6442 case DeclarationName::CXXOperatorName: 6443 DNLoc.CXXOperatorName.BeginOpNameLoc 6444 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 6445 DNLoc.CXXOperatorName.EndOpNameLoc 6446 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 6447 break; 6448 6449 case DeclarationName::CXXLiteralOperatorName: 6450 DNLoc.CXXLiteralOperatorName.OpNameLoc 6451 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 6452 break; 6453 6454 case DeclarationName::Identifier: 6455 case DeclarationName::ObjCZeroArgSelector: 6456 case DeclarationName::ObjCOneArgSelector: 6457 case DeclarationName::ObjCMultiArgSelector: 6458 case DeclarationName::CXXUsingDirective: 6459 break; 6460 } 6461} 6462 6463void ASTReader::ReadDeclarationNameInfo(ModuleFile &F, 6464 DeclarationNameInfo &NameInfo, 6465 const RecordData &Record, unsigned &Idx) { 6466 NameInfo.setName(ReadDeclarationName(F, Record, Idx)); 6467 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx)); 6468 DeclarationNameLoc DNLoc; 6469 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx); 6470 NameInfo.setInfo(DNLoc); 6471} 6472 6473void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info, 6474 const RecordData &Record, unsigned &Idx) { 6475 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx); 6476 unsigned NumTPLists = Record[Idx++]; 6477 Info.NumTemplParamLists = NumTPLists; 6478 if (NumTPLists) { 6479 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists]; 6480 for (unsigned i=0; i != NumTPLists; ++i) 6481 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx); 6482 } 6483} 6484 6485TemplateName 6486ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record, 6487 unsigned &Idx) { 6488 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++]; 6489 switch (Kind) { 6490 case TemplateName::Template: 6491 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx)); 6492 6493 case TemplateName::OverloadedTemplate: { 6494 unsigned size = Record[Idx++]; 6495 UnresolvedSet<8> Decls; 6496 while (size--) 6497 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx)); 6498 6499 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end()); 6500 } 6501 6502 case TemplateName::QualifiedTemplate: { 6503 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx); 6504 bool hasTemplKeyword = Record[Idx++]; 6505 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx); 6506 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template); 6507 } 6508 6509 case TemplateName::DependentTemplate: { 6510 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx); 6511 if (Record[Idx++]) // isIdentifier 6512 return Context.getDependentTemplateName(NNS, 6513 GetIdentifierInfo(F, Record, 6514 Idx)); 6515 return Context.getDependentTemplateName(NNS, 6516 (OverloadedOperatorKind)Record[Idx++]); 6517 } 6518 6519 case TemplateName::SubstTemplateTemplateParm: { 6520 TemplateTemplateParmDecl *param 6521 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx); 6522 if (!param) return TemplateName(); 6523 TemplateName replacement = ReadTemplateName(F, Record, Idx); 6524 return Context.getSubstTemplateTemplateParm(param, replacement); 6525 } 6526 6527 case TemplateName::SubstTemplateTemplateParmPack: { 6528 TemplateTemplateParmDecl *Param 6529 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx); 6530 if (!Param) 6531 return TemplateName(); 6532 6533 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx); 6534 if (ArgPack.getKind() != TemplateArgument::Pack) 6535 return TemplateName(); 6536 6537 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack); 6538 } 6539 } 6540 6541 llvm_unreachable("Unhandled template name kind!"); 6542} 6543 6544TemplateArgument 6545ASTReader::ReadTemplateArgument(ModuleFile &F, 6546 const RecordData &Record, unsigned &Idx) { 6547 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++]; 6548 switch (Kind) { 6549 case TemplateArgument::Null: 6550 return TemplateArgument(); 6551 case TemplateArgument::Type: 6552 return TemplateArgument(readType(F, Record, Idx)); 6553 case TemplateArgument::Declaration: { 6554 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx); 6555 bool ForReferenceParam = Record[Idx++]; 6556 return TemplateArgument(D, ForReferenceParam); 6557 } 6558 case TemplateArgument::NullPtr: 6559 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true); 6560 case TemplateArgument::Integral: { 6561 llvm::APSInt Value = ReadAPSInt(Record, Idx); 6562 QualType T = readType(F, Record, Idx); 6563 return TemplateArgument(Context, Value, T); 6564 } 6565 case TemplateArgument::Template: 6566 return TemplateArgument(ReadTemplateName(F, Record, Idx)); 6567 case TemplateArgument::TemplateExpansion: { 6568 TemplateName Name = ReadTemplateName(F, Record, Idx); 6569 Optional<unsigned> NumTemplateExpansions; 6570 if (unsigned NumExpansions = Record[Idx++]) 6571 NumTemplateExpansions = NumExpansions - 1; 6572 return TemplateArgument(Name, NumTemplateExpansions); 6573 } 6574 case TemplateArgument::Expression: 6575 return TemplateArgument(ReadExpr(F)); 6576 case TemplateArgument::Pack: { 6577 unsigned NumArgs = Record[Idx++]; 6578 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs]; 6579 for (unsigned I = 0; I != NumArgs; ++I) 6580 Args[I] = ReadTemplateArgument(F, Record, Idx); 6581 return TemplateArgument(Args, NumArgs); 6582 } 6583 } 6584 6585 llvm_unreachable("Unhandled template argument kind!"); 6586} 6587 6588TemplateParameterList * 6589ASTReader::ReadTemplateParameterList(ModuleFile &F, 6590 const RecordData &Record, unsigned &Idx) { 6591 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx); 6592 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx); 6593 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx); 6594 6595 unsigned NumParams = Record[Idx++]; 6596 SmallVector<NamedDecl *, 16> Params; 6597 Params.reserve(NumParams); 6598 while (NumParams--) 6599 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx)); 6600 6601 TemplateParameterList* TemplateParams = 6602 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, 6603 Params.data(), Params.size(), RAngleLoc); 6604 return TemplateParams; 6605} 6606 6607void 6608ASTReader:: 6609ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs, 6610 ModuleFile &F, const RecordData &Record, 6611 unsigned &Idx) { 6612 unsigned NumTemplateArgs = Record[Idx++]; 6613 TemplArgs.reserve(NumTemplateArgs); 6614 while (NumTemplateArgs--) 6615 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx)); 6616} 6617 6618/// \brief Read a UnresolvedSet structure. 6619void ASTReader::ReadUnresolvedSet(ModuleFile &F, ASTUnresolvedSet &Set, 6620 const RecordData &Record, unsigned &Idx) { 6621 unsigned NumDecls = Record[Idx++]; 6622 Set.reserve(Context, NumDecls); 6623 while (NumDecls--) { 6624 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx); 6625 AccessSpecifier AS = (AccessSpecifier)Record[Idx++]; 6626 Set.addDecl(Context, D, AS); 6627 } 6628} 6629 6630CXXBaseSpecifier 6631ASTReader::ReadCXXBaseSpecifier(ModuleFile &F, 6632 const RecordData &Record, unsigned &Idx) { 6633 bool isVirtual = static_cast<bool>(Record[Idx++]); 6634 bool isBaseOfClass = static_cast<bool>(Record[Idx++]); 6635 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]); 6636 bool inheritConstructors = static_cast<bool>(Record[Idx++]); 6637 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx); 6638 SourceRange Range = ReadSourceRange(F, Record, Idx); 6639 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx); 6640 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo, 6641 EllipsisLoc); 6642 Result.setInheritConstructors(inheritConstructors); 6643 return Result; 6644} 6645 6646std::pair<CXXCtorInitializer **, unsigned> 6647ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record, 6648 unsigned &Idx) { 6649 CXXCtorInitializer **CtorInitializers = 0; 6650 unsigned NumInitializers = Record[Idx++]; 6651 if (NumInitializers) { 6652 CtorInitializers 6653 = new (Context) CXXCtorInitializer*[NumInitializers]; 6654 for (unsigned i=0; i != NumInitializers; ++i) { 6655 TypeSourceInfo *TInfo = 0; 6656 bool IsBaseVirtual = false; 6657 FieldDecl *Member = 0; 6658 IndirectFieldDecl *IndirectMember = 0; 6659 6660 CtorInitializerType Type = (CtorInitializerType)Record[Idx++]; 6661 switch (Type) { 6662 case CTOR_INITIALIZER_BASE: 6663 TInfo = GetTypeSourceInfo(F, Record, Idx); 6664 IsBaseVirtual = Record[Idx++]; 6665 break; 6666 6667 case CTOR_INITIALIZER_DELEGATING: 6668 TInfo = GetTypeSourceInfo(F, Record, Idx); 6669 break; 6670 6671 case CTOR_INITIALIZER_MEMBER: 6672 Member = ReadDeclAs<FieldDecl>(F, Record, Idx); 6673 break; 6674 6675 case CTOR_INITIALIZER_INDIRECT_MEMBER: 6676 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx); 6677 break; 6678 } 6679 6680 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx); 6681 Expr *Init = ReadExpr(F); 6682 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx); 6683 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx); 6684 bool IsWritten = Record[Idx++]; 6685 unsigned SourceOrderOrNumArrayIndices; 6686 SmallVector<VarDecl *, 8> Indices; 6687 if (IsWritten) { 6688 SourceOrderOrNumArrayIndices = Record[Idx++]; 6689 } else { 6690 SourceOrderOrNumArrayIndices = Record[Idx++]; 6691 Indices.reserve(SourceOrderOrNumArrayIndices); 6692 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i) 6693 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx)); 6694 } 6695 6696 CXXCtorInitializer *BOMInit; 6697 if (Type == CTOR_INITIALIZER_BASE) { 6698 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual, 6699 LParenLoc, Init, RParenLoc, 6700 MemberOrEllipsisLoc); 6701 } else if (Type == CTOR_INITIALIZER_DELEGATING) { 6702 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc, 6703 Init, RParenLoc); 6704 } else if (IsWritten) { 6705 if (Member) 6706 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, 6707 LParenLoc, Init, RParenLoc); 6708 else 6709 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember, 6710 MemberOrEllipsisLoc, LParenLoc, 6711 Init, RParenLoc); 6712 } else { 6713 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc, 6714 LParenLoc, Init, RParenLoc, 6715 Indices.data(), Indices.size()); 6716 } 6717 6718 if (IsWritten) 6719 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices); 6720 CtorInitializers[i] = BOMInit; 6721 } 6722 } 6723 6724 return std::make_pair(CtorInitializers, NumInitializers); 6725} 6726 6727NestedNameSpecifier * 6728ASTReader::ReadNestedNameSpecifier(ModuleFile &F, 6729 const RecordData &Record, unsigned &Idx) { 6730 unsigned N = Record[Idx++]; 6731 NestedNameSpecifier *NNS = 0, *Prev = 0; 6732 for (unsigned I = 0; I != N; ++I) { 6733 NestedNameSpecifier::SpecifierKind Kind 6734 = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; 6735 switch (Kind) { 6736 case NestedNameSpecifier::Identifier: { 6737 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx); 6738 NNS = NestedNameSpecifier::Create(Context, Prev, II); 6739 break; 6740 } 6741 6742 case NestedNameSpecifier::Namespace: { 6743 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx); 6744 NNS = NestedNameSpecifier::Create(Context, Prev, NS); 6745 break; 6746 } 6747 6748 case NestedNameSpecifier::NamespaceAlias: { 6749 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx); 6750 NNS = NestedNameSpecifier::Create(Context, Prev, Alias); 6751 break; 6752 } 6753 6754 case NestedNameSpecifier::TypeSpec: 6755 case NestedNameSpecifier::TypeSpecWithTemplate: { 6756 const Type *T = readType(F, Record, Idx).getTypePtrOrNull(); 6757 if (!T) 6758 return 0; 6759 6760 bool Template = Record[Idx++]; 6761 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T); 6762 break; 6763 } 6764 6765 case NestedNameSpecifier::Global: { 6766 NNS = NestedNameSpecifier::GlobalSpecifier(Context); 6767 // No associated value, and there can't be a prefix. 6768 break; 6769 } 6770 } 6771 Prev = NNS; 6772 } 6773 return NNS; 6774} 6775 6776NestedNameSpecifierLoc 6777ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record, 6778 unsigned &Idx) { 6779 unsigned N = Record[Idx++]; 6780 NestedNameSpecifierLocBuilder Builder; 6781 for (unsigned I = 0; I != N; ++I) { 6782 NestedNameSpecifier::SpecifierKind Kind 6783 = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; 6784 switch (Kind) { 6785 case NestedNameSpecifier::Identifier: { 6786 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx); 6787 SourceRange Range = ReadSourceRange(F, Record, Idx); 6788 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd()); 6789 break; 6790 } 6791 6792 case NestedNameSpecifier::Namespace: { 6793 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx); 6794 SourceRange Range = ReadSourceRange(F, Record, Idx); 6795 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd()); 6796 break; 6797 } 6798 6799 case NestedNameSpecifier::NamespaceAlias: { 6800 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx); 6801 SourceRange Range = ReadSourceRange(F, Record, Idx); 6802 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd()); 6803 break; 6804 } 6805 6806 case NestedNameSpecifier::TypeSpec: 6807 case NestedNameSpecifier::TypeSpecWithTemplate: { 6808 bool Template = Record[Idx++]; 6809 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx); 6810 if (!T) 6811 return NestedNameSpecifierLoc(); 6812 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx); 6813 6814 // FIXME: 'template' keyword location not saved anywhere, so we fake it. 6815 Builder.Extend(Context, 6816 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(), 6817 T->getTypeLoc(), ColonColonLoc); 6818 break; 6819 } 6820 6821 case NestedNameSpecifier::Global: { 6822 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx); 6823 Builder.MakeGlobal(Context, ColonColonLoc); 6824 break; 6825 } 6826 } 6827 } 6828 6829 return Builder.getWithLocInContext(Context); 6830} 6831 6832SourceRange 6833ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record, 6834 unsigned &Idx) { 6835 SourceLocation beg = ReadSourceLocation(F, Record, Idx); 6836 SourceLocation end = ReadSourceLocation(F, Record, Idx); 6837 return SourceRange(beg, end); 6838} 6839 6840/// \brief Read an integral value 6841llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) { 6842 unsigned BitWidth = Record[Idx++]; 6843 unsigned NumWords = llvm::APInt::getNumWords(BitWidth); 6844 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]); 6845 Idx += NumWords; 6846 return Result; 6847} 6848 6849/// \brief Read a signed integral value 6850llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) { 6851 bool isUnsigned = Record[Idx++]; 6852 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned); 6853} 6854 6855/// \brief Read a floating-point value 6856llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, 6857 const llvm::fltSemantics &Sem, 6858 unsigned &Idx) { 6859 return llvm::APFloat(Sem, ReadAPInt(Record, Idx)); 6860} 6861 6862// \brief Read a string 6863std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) { 6864 unsigned Len = Record[Idx++]; 6865 std::string Result(Record.data() + Idx, Record.data() + Idx + Len); 6866 Idx += Len; 6867 return Result; 6868} 6869 6870VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record, 6871 unsigned &Idx) { 6872 unsigned Major = Record[Idx++]; 6873 unsigned Minor = Record[Idx++]; 6874 unsigned Subminor = Record[Idx++]; 6875 if (Minor == 0) 6876 return VersionTuple(Major); 6877 if (Subminor == 0) 6878 return VersionTuple(Major, Minor - 1); 6879 return VersionTuple(Major, Minor - 1, Subminor - 1); 6880} 6881 6882CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F, 6883 const RecordData &Record, 6884 unsigned &Idx) { 6885 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx); 6886 return CXXTemporary::Create(Context, Decl); 6887} 6888 6889DiagnosticBuilder ASTReader::Diag(unsigned DiagID) { 6890 return Diag(SourceLocation(), DiagID); 6891} 6892 6893DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) { 6894 return Diags.Report(Loc, DiagID); 6895} 6896 6897/// \brief Retrieve the identifier table associated with the 6898/// preprocessor. 6899IdentifierTable &ASTReader::getIdentifierTable() { 6900 return PP.getIdentifierTable(); 6901} 6902 6903/// \brief Record that the given ID maps to the given switch-case 6904/// statement. 6905void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { 6906 assert((*CurrSwitchCaseStmts)[ID] == 0 && 6907 "Already have a SwitchCase with this ID"); 6908 (*CurrSwitchCaseStmts)[ID] = SC; 6909} 6910 6911/// \brief Retrieve the switch-case statement with the given ID. 6912SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) { 6913 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID"); 6914 return (*CurrSwitchCaseStmts)[ID]; 6915} 6916 6917void ASTReader::ClearSwitchCaseIDs() { 6918 CurrSwitchCaseStmts->clear(); 6919} 6920 6921void ASTReader::ReadComments() { 6922 std::vector<RawComment *> Comments; 6923 for (SmallVectorImpl<std::pair<BitstreamCursor, 6924 serialization::ModuleFile *> >::iterator 6925 I = CommentsCursors.begin(), 6926 E = CommentsCursors.end(); 6927 I != E; ++I) { 6928 BitstreamCursor &Cursor = I->first; 6929 serialization::ModuleFile &F = *I->second; 6930 SavedStreamPosition SavedPosition(Cursor); 6931 6932 RecordData Record; 6933 while (true) { 6934 llvm::BitstreamEntry Entry = 6935 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd); 6936 6937 switch (Entry.Kind) { 6938 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 6939 case llvm::BitstreamEntry::Error: 6940 Error("malformed block record in AST file"); 6941 return; 6942 case llvm::BitstreamEntry::EndBlock: 6943 goto NextCursor; 6944 case llvm::BitstreamEntry::Record: 6945 // The interesting case. 6946 break; 6947 } 6948 6949 // Read a record. 6950 Record.clear(); 6951 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) { 6952 case COMMENTS_RAW_COMMENT: { 6953 unsigned Idx = 0; 6954 SourceRange SR = ReadSourceRange(F, Record, Idx); 6955 RawComment::CommentKind Kind = 6956 (RawComment::CommentKind) Record[Idx++]; 6957 bool IsTrailingComment = Record[Idx++]; 6958 bool IsAlmostTrailingComment = Record[Idx++]; 6959 Comments.push_back(new (Context) RawComment(SR, Kind, 6960 IsTrailingComment, 6961 IsAlmostTrailingComment)); 6962 break; 6963 } 6964 } 6965 } 6966 NextCursor:; 6967 } 6968 Context.Comments.addCommentsToFront(Comments); 6969} 6970 6971void ASTReader::finishPendingActions() { 6972 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() || 6973 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty()) { 6974 // If any identifiers with corresponding top-level declarations have 6975 // been loaded, load those declarations now. 6976 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> > TopLevelDecls; 6977 while (!PendingIdentifierInfos.empty()) { 6978 // FIXME: std::move 6979 IdentifierInfo *II = PendingIdentifierInfos.back().first; 6980 SmallVector<uint32_t, 4> DeclIDs = PendingIdentifierInfos.back().second; 6981 PendingIdentifierInfos.pop_back(); 6982 6983 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]); 6984 } 6985 6986 // Load pending declaration chains. 6987 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) { 6988 loadPendingDeclChain(PendingDeclChains[I]); 6989 PendingDeclChainsKnown.erase(PendingDeclChains[I]); 6990 } 6991 PendingDeclChains.clear(); 6992 6993 // Make the most recent of the top-level declarations visible. 6994 for (llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >::iterator 6995 TLD = TopLevelDecls.begin(), TLDEnd = TopLevelDecls.end(); 6996 TLD != TLDEnd; ++TLD) { 6997 IdentifierInfo *II = TLD->first; 6998 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) { 6999 NamedDecl *ND = cast<NamedDecl>(TLD->second[I]->getMostRecentDecl()); 7000 SemaObj->pushExternalDeclIntoScope(ND, II); 7001 } 7002 } 7003 7004 // Load any pending macro definitions. 7005 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) { 7006 // FIXME: std::move here 7007 SmallVector<MacroID, 2> GlobalIDs = PendingMacroIDs.begin()[I].second; 7008 MacroDirective *Hint = 0; 7009 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 7010 ++IDIdx) { 7011 Hint = getMacro(GlobalIDs[IDIdx], Hint); 7012 } 7013 } 7014 PendingMacroIDs.clear(); 7015 7016 // Wire up the DeclContexts for Decls that we delayed setting until 7017 // recursive loading is completed. 7018 while (!PendingDeclContextInfos.empty()) { 7019 PendingDeclContextInfo Info = PendingDeclContextInfos.front(); 7020 PendingDeclContextInfos.pop_front(); 7021 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC)); 7022 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC)); 7023 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext()); 7024 } 7025 } 7026 7027 // If we deserialized any C++ or Objective-C class definitions, any 7028 // Objective-C protocol definitions, or any redeclarable templates, make sure 7029 // that all redeclarations point to the definitions. Note that this can only 7030 // happen now, after the redeclaration chains have been fully wired. 7031 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(), 7032 DEnd = PendingDefinitions.end(); 7033 D != DEnd; ++D) { 7034 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) { 7035 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) { 7036 // Make sure that the TagType points at the definition. 7037 const_cast<TagType*>(TagT)->decl = TD; 7038 } 7039 7040 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) { 7041 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(), 7042 REnd = RD->redecls_end(); 7043 R != REnd; ++R) 7044 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData; 7045 7046 } 7047 7048 continue; 7049 } 7050 7051 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) { 7052 // Make sure that the ObjCInterfaceType points at the definition. 7053 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl)) 7054 ->Decl = ID; 7055 7056 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(), 7057 REnd = ID->redecls_end(); 7058 R != REnd; ++R) 7059 R->Data = ID->Data; 7060 7061 continue; 7062 } 7063 7064 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) { 7065 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(), 7066 REnd = PD->redecls_end(); 7067 R != REnd; ++R) 7068 R->Data = PD->Data; 7069 7070 continue; 7071 } 7072 7073 RedeclarableTemplateDecl *RTD 7074 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl(); 7075 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(), 7076 REnd = RTD->redecls_end(); 7077 R != REnd; ++R) 7078 R->Common = RTD->Common; 7079 } 7080 PendingDefinitions.clear(); 7081 7082 // Load the bodies of any functions or methods we've encountered. We do 7083 // this now (delayed) so that we can be sure that the declaration chains 7084 // have been fully wired up. 7085 for (PendingBodiesMap::iterator PB = PendingBodies.begin(), 7086 PBEnd = PendingBodies.end(); 7087 PB != PBEnd; ++PB) { 7088 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) { 7089 // FIXME: Check for =delete/=default? 7090 // FIXME: Complain about ODR violations here? 7091 if (!getContext().getLangOpts().Modules || !FD->hasBody()) 7092 FD->setLazyBody(PB->second); 7093 continue; 7094 } 7095 7096 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first); 7097 if (!getContext().getLangOpts().Modules || !MD->hasBody()) 7098 MD->setLazyBody(PB->second); 7099 } 7100 PendingBodies.clear(); 7101} 7102 7103void ASTReader::FinishedDeserializing() { 7104 assert(NumCurrentElementsDeserializing && 7105 "FinishedDeserializing not paired with StartedDeserializing"); 7106 if (NumCurrentElementsDeserializing == 1) { 7107 // We decrease NumCurrentElementsDeserializing only after pending actions 7108 // are finished, to avoid recursively re-calling finishPendingActions(). 7109 finishPendingActions(); 7110 } 7111 --NumCurrentElementsDeserializing; 7112 7113 if (NumCurrentElementsDeserializing == 0 && 7114 Consumer && !PassingDeclsToConsumer) { 7115 // Guard variable to avoid recursively redoing the process of passing 7116 // decls to consumer. 7117 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer, 7118 true); 7119 7120 while (!InterestingDecls.empty()) { 7121 // We are not in recursive loading, so it's safe to pass the "interesting" 7122 // decls to the consumer. 7123 Decl *D = InterestingDecls.front(); 7124 InterestingDecls.pop_front(); 7125 PassInterestingDeclToConsumer(D); 7126 } 7127 } 7128} 7129 7130ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context, 7131 StringRef isysroot, bool DisableValidation, 7132 bool AllowASTWithCompilerErrors, bool UseGlobalIndex) 7133 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0), 7134 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), 7135 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context), 7136 Consumer(0), ModuleMgr(PP.getFileManager()), 7137 isysroot(isysroot), DisableValidation(DisableValidation), 7138 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors), 7139 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false), 7140 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts), 7141 NumSLocEntriesRead(0), TotalNumSLocEntries(0), 7142 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0), 7143 TotalNumMacros(0), NumIdentifierLookups(0), NumIdentifierLookupHits(0), 7144 NumSelectorsRead(0), NumMethodPoolEntriesRead(0), 7145 NumMethodPoolLookups(0), NumMethodPoolHits(0), 7146 NumMethodPoolTableLookups(0), NumMethodPoolTableHits(0), 7147 TotalNumMethodPoolEntries(0), 7148 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0), 7149 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0), 7150 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0), 7151 PassingDeclsToConsumer(false), 7152 NumCXXBaseSpecifiersLoaded(0) 7153{ 7154 SourceMgr.setExternalSLocEntrySource(this); 7155} 7156 7157ASTReader::~ASTReader() { 7158 for (DeclContextVisibleUpdatesPending::iterator 7159 I = PendingVisibleUpdates.begin(), 7160 E = PendingVisibleUpdates.end(); 7161 I != E; ++I) { 7162 for (DeclContextVisibleUpdates::iterator J = I->second.begin(), 7163 F = I->second.end(); 7164 J != F; ++J) 7165 delete J->first; 7166 } 7167} 7168