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