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