ASTReader.cpp revision 99a5af0088b6340540912921cd0008ac75051a33
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 = reinterpret_cast<LE32DeclID *>( 659 const_cast<unsigned char *>(d)); 660 return std::make_pair(Start, Start + NumDecls); 661} 662 663bool ASTReader::ReadDeclContextStorage(ModuleFile &M, 664 llvm::BitstreamCursor &Cursor, 665 const std::pair<uint64_t, uint64_t> &Offsets, 666 DeclContextInfo &Info) { 667 SavedStreamPosition SavedPosition(Cursor); 668 // First the lexical decls. 669 if (Offsets.first != 0) { 670 Cursor.JumpToBit(Offsets.first); 671 672 RecordData Record; 673 const char *Blob; 674 unsigned BlobLen; 675 unsigned Code = Cursor.ReadCode(); 676 unsigned RecCode = Cursor.ReadRecord(Code, Record, Blob, BlobLen); 677 if (RecCode != DECL_CONTEXT_LEXICAL) { 678 Error("Expected lexical block"); 679 return true; 680 } 681 682 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob); 683 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair); 684 } 685 686 // Now the lookup table. 687 if (Offsets.second != 0) { 688 Cursor.JumpToBit(Offsets.second); 689 690 RecordData Record; 691 const char *Blob; 692 unsigned BlobLen; 693 unsigned Code = Cursor.ReadCode(); 694 unsigned RecCode = Cursor.ReadRecord(Code, Record, Blob, BlobLen); 695 if (RecCode != DECL_CONTEXT_VISIBLE) { 696 Error("Expected visible lookup table block"); 697 return true; 698 } 699 Info.NameLookupTableData 700 = ASTDeclContextNameLookupTable::Create( 701 (const unsigned char *)Blob + Record[0], 702 (const unsigned char *)Blob, 703 ASTDeclContextNameLookupTrait(*this, M)); 704 } 705 706 return false; 707} 708 709void ASTReader::Error(StringRef Msg) { 710 Error(diag::err_fe_pch_malformed, Msg); 711} 712 713void ASTReader::Error(unsigned DiagID, 714 StringRef Arg1, StringRef Arg2) { 715 if (Diags.isDiagnosticInFlight()) 716 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2); 717 else 718 Diag(DiagID) << Arg1 << Arg2; 719} 720 721//===----------------------------------------------------------------------===// 722// Source Manager Deserialization 723//===----------------------------------------------------------------------===// 724 725/// \brief Read the line table in the source manager block. 726/// \returns true if there was an error. 727bool ASTReader::ParseLineTable(ModuleFile &F, 728 SmallVectorImpl<uint64_t> &Record) { 729 unsigned Idx = 0; 730 LineTableInfo &LineTable = SourceMgr.getLineTable(); 731 732 // Parse the file names 733 std::map<int, int> FileIDs; 734 for (int I = 0, N = Record[Idx++]; I != N; ++I) { 735 // Extract the file name 736 unsigned FilenameLen = Record[Idx++]; 737 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen); 738 Idx += FilenameLen; 739 MaybeAddSystemRootToFilename(F, Filename); 740 FileIDs[I] = LineTable.getLineTableFilenameID(Filename); 741 } 742 743 // Parse the line entries 744 std::vector<LineEntry> Entries; 745 while (Idx < Record.size()) { 746 int FID = Record[Idx++]; 747 assert(FID >= 0 && "Serialized line entries for non-local file."); 748 // Remap FileID from 1-based old view. 749 FID += F.SLocEntryBaseID - 1; 750 751 // Extract the line entries 752 unsigned NumEntries = Record[Idx++]; 753 assert(NumEntries && "Numentries is 00000"); 754 Entries.clear(); 755 Entries.reserve(NumEntries); 756 for (unsigned I = 0; I != NumEntries; ++I) { 757 unsigned FileOffset = Record[Idx++]; 758 unsigned LineNo = Record[Idx++]; 759 int FilenameID = FileIDs[Record[Idx++]]; 760 SrcMgr::CharacteristicKind FileKind 761 = (SrcMgr::CharacteristicKind)Record[Idx++]; 762 unsigned IncludeOffset = Record[Idx++]; 763 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, 764 FileKind, IncludeOffset)); 765 } 766 LineTable.AddEntry(FileID::get(FID), Entries); 767 } 768 769 return false; 770} 771 772/// \brief Read a source manager block 773bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) { 774 using namespace SrcMgr; 775 776 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor; 777 778 // Set the source-location entry cursor to the current position in 779 // the stream. This cursor will be used to read the contents of the 780 // source manager block initially, and then lazily read 781 // source-location entries as needed. 782 SLocEntryCursor = F.Stream; 783 784 // The stream itself is going to skip over the source manager block. 785 if (F.Stream.SkipBlock()) { 786 Error("malformed block record in AST file"); 787 return true; 788 } 789 790 // Enter the source manager block. 791 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) { 792 Error("malformed source manager block record in AST file"); 793 return true; 794 } 795 796 RecordData Record; 797 while (true) { 798 llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks(); 799 800 switch (E.Kind) { 801 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 802 case llvm::BitstreamEntry::Error: 803 Error("malformed block record in AST file"); 804 return true; 805 case llvm::BitstreamEntry::EndBlock: 806 return false; 807 case llvm::BitstreamEntry::Record: 808 // The interesting case. 809 break; 810 } 811 812 // Read a record. 813 const char *BlobStart; 814 unsigned BlobLen; 815 Record.clear(); 816 switch (SLocEntryCursor.ReadRecord(E.ID, Record, BlobStart, BlobLen)) { 817 default: // Default behavior: ignore. 818 break; 819 820 case SM_SLOC_FILE_ENTRY: 821 case SM_SLOC_BUFFER_ENTRY: 822 case SM_SLOC_EXPANSION_ENTRY: 823 // Once we hit one of the source location entries, we're done. 824 return false; 825 } 826 } 827} 828 829/// \brief If a header file is not found at the path that we expect it to be 830/// and the PCH file was moved from its original location, try to resolve the 831/// file by assuming that header+PCH were moved together and the header is in 832/// the same place relative to the PCH. 833static std::string 834resolveFileRelativeToOriginalDir(const std::string &Filename, 835 const std::string &OriginalDir, 836 const std::string &CurrDir) { 837 assert(OriginalDir != CurrDir && 838 "No point trying to resolve the file if the PCH dir didn't change"); 839 using namespace llvm::sys; 840 SmallString<128> filePath(Filename); 841 fs::make_absolute(filePath); 842 assert(path::is_absolute(OriginalDir)); 843 SmallString<128> currPCHPath(CurrDir); 844 845 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)), 846 fileDirE = path::end(path::parent_path(filePath)); 847 path::const_iterator origDirI = path::begin(OriginalDir), 848 origDirE = path::end(OriginalDir); 849 // Skip the common path components from filePath and OriginalDir. 850 while (fileDirI != fileDirE && origDirI != origDirE && 851 *fileDirI == *origDirI) { 852 ++fileDirI; 853 ++origDirI; 854 } 855 for (; origDirI != origDirE; ++origDirI) 856 path::append(currPCHPath, ".."); 857 path::append(currPCHPath, fileDirI, fileDirE); 858 path::append(currPCHPath, path::filename(Filename)); 859 return currPCHPath.str(); 860} 861 862bool ASTReader::ReadSLocEntry(int ID) { 863 if (ID == 0) 864 return false; 865 866 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 867 Error("source location entry ID out-of-range for AST file"); 868 return true; 869 } 870 871 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second; 872 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]); 873 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor; 874 unsigned BaseOffset = F->SLocEntryBaseOffset; 875 876 ++NumSLocEntriesRead; 877 llvm::BitstreamEntry Entry = SLocEntryCursor.advance(); 878 if (Entry.Kind != llvm::BitstreamEntry::Record) { 879 Error("incorrectly-formatted source location entry in AST file"); 880 return true; 881 } 882 883 RecordData Record; 884 const char *BlobStart; 885 unsigned BlobLen; 886 switch (SLocEntryCursor.ReadRecord(Entry.ID, Record, BlobStart, BlobLen)) { 887 default: 888 Error("incorrectly-formatted source location entry in AST file"); 889 return true; 890 891 case SM_SLOC_FILE_ENTRY: { 892 // We will detect whether a file changed and return 'Failure' for it, but 893 // we will also try to fail gracefully by setting up the SLocEntry. 894 unsigned InputID = Record[4]; 895 InputFile IF = getInputFile(*F, InputID); 896 const FileEntry *File = IF.getPointer(); 897 bool OverriddenBuffer = IF.getInt(); 898 899 if (!IF.getPointer()) 900 return true; 901 902 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 903 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) { 904 // This is the module's main file. 905 IncludeLoc = getImportLocation(F); 906 } 907 SrcMgr::CharacteristicKind 908 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 909 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter, 910 ID, BaseOffset + Record[0]); 911 SrcMgr::FileInfo &FileInfo = 912 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile()); 913 FileInfo.NumCreatedFIDs = Record[5]; 914 if (Record[3]) 915 FileInfo.setHasLineDirectives(); 916 917 const DeclID *FirstDecl = F->FileSortedDecls + Record[6]; 918 unsigned NumFileDecls = Record[7]; 919 if (NumFileDecls) { 920 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?"); 921 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl, 922 NumFileDecls)); 923 } 924 925 const SrcMgr::ContentCache *ContentCache 926 = SourceMgr.getOrCreateContentCache(File, 927 /*isSystemFile=*/FileCharacter != SrcMgr::C_User); 928 if (OverriddenBuffer && !ContentCache->BufferOverridden && 929 ContentCache->ContentsEntry == ContentCache->OrigEntry) { 930 unsigned Code = SLocEntryCursor.ReadCode(); 931 Record.clear(); 932 unsigned RecCode 933 = SLocEntryCursor.ReadRecord(Code, Record, BlobStart, BlobLen); 934 935 if (RecCode != SM_SLOC_BUFFER_BLOB) { 936 Error("AST record has invalid code"); 937 return true; 938 } 939 940 llvm::MemoryBuffer *Buffer 941 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1), 942 File->getName()); 943 SourceMgr.overrideFileContents(File, Buffer); 944 } 945 946 break; 947 } 948 949 case SM_SLOC_BUFFER_ENTRY: { 950 const char *Name = BlobStart; 951 unsigned Offset = Record[0]; 952 SrcMgr::CharacteristicKind 953 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 954 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 955 if (IncludeLoc.isInvalid() && F->Kind == MK_Module) { 956 IncludeLoc = getImportLocation(F); 957 } 958 unsigned Code = SLocEntryCursor.ReadCode(); 959 Record.clear(); 960 unsigned RecCode 961 = SLocEntryCursor.ReadRecord(Code, Record, BlobStart, BlobLen); 962 963 if (RecCode != SM_SLOC_BUFFER_BLOB) { 964 Error("AST record has invalid code"); 965 return true; 966 } 967 968 llvm::MemoryBuffer *Buffer 969 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1), 970 Name); 971 SourceMgr.createFileIDForMemBuffer(Buffer, FileCharacter, ID, 972 BaseOffset + Offset, IncludeLoc); 973 break; 974 } 975 976 case SM_SLOC_EXPANSION_ENTRY: { 977 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]); 978 SourceMgr.createExpansionLoc(SpellingLoc, 979 ReadSourceLocation(*F, Record[2]), 980 ReadSourceLocation(*F, Record[3]), 981 Record[4], 982 ID, 983 BaseOffset + Record[0]); 984 break; 985 } 986 } 987 988 return false; 989} 990 991std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) { 992 if (ID == 0) 993 return std::make_pair(SourceLocation(), ""); 994 995 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 996 Error("source location entry ID out-of-range for AST file"); 997 return std::make_pair(SourceLocation(), ""); 998 } 999 1000 // Find which module file this entry lands in. 1001 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second; 1002 if (M->Kind != MK_Module) 1003 return std::make_pair(SourceLocation(), ""); 1004 1005 // FIXME: Can we map this down to a particular submodule? That would be 1006 // ideal. 1007 return std::make_pair(M->ImportLoc, llvm::sys::path::stem(M->FileName)); 1008} 1009 1010/// \brief Find the location where the module F is imported. 1011SourceLocation ASTReader::getImportLocation(ModuleFile *F) { 1012 if (F->ImportLoc.isValid()) 1013 return F->ImportLoc; 1014 1015 // Otherwise we have a PCH. It's considered to be "imported" at the first 1016 // location of its includer. 1017 if (F->ImportedBy.empty() || !F->ImportedBy[0]) { 1018 // Main file is the importer. We assume that it is the first entry in the 1019 // entry table. We can't ask the manager, because at the time of PCH loading 1020 // the main file entry doesn't exist yet. 1021 // The very first entry is the invalid instantiation loc, which takes up 1022 // offsets 0 and 1. 1023 return SourceLocation::getFromRawEncoding(2U); 1024 } 1025 //return F->Loaders[0]->FirstLoc; 1026 return F->ImportedBy[0]->FirstLoc; 1027} 1028 1029/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the 1030/// specified cursor. Read the abbreviations that are at the top of the block 1031/// and then leave the cursor pointing into the block. 1032bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor, 1033 unsigned BlockID) { 1034 if (Cursor.EnterSubBlock(BlockID)) { 1035 Error("malformed block record in AST file"); 1036 return Failure; 1037 } 1038 1039 while (true) { 1040 uint64_t Offset = Cursor.GetCurrentBitNo(); 1041 unsigned Code = Cursor.ReadCode(); 1042 1043 // We expect all abbrevs to be at the start of the block. 1044 if (Code != llvm::bitc::DEFINE_ABBREV) { 1045 Cursor.JumpToBit(Offset); 1046 return false; 1047 } 1048 Cursor.ReadAbbrevRecord(); 1049 } 1050} 1051 1052void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset, 1053 MacroInfo *Hint) { 1054 llvm::BitstreamCursor &Stream = F.MacroCursor; 1055 1056 // Keep track of where we are in the stream, then jump back there 1057 // after reading this macro. 1058 SavedStreamPosition SavedPosition(Stream); 1059 1060 Stream.JumpToBit(Offset); 1061 RecordData Record; 1062 SmallVector<IdentifierInfo*, 16> MacroArgs; 1063 MacroInfo *Macro = 0; 1064 1065 // RAII object to add the loaded macro information once we're done 1066 // adding tokens. 1067 struct AddLoadedMacroInfoRAII { 1068 Preprocessor &PP; 1069 MacroInfo *Hint; 1070 MacroInfo *MI; 1071 IdentifierInfo *II; 1072 1073 AddLoadedMacroInfoRAII(Preprocessor &PP, MacroInfo *Hint) 1074 : PP(PP), Hint(Hint), MI(), II() { } 1075 ~AddLoadedMacroInfoRAII( ) { 1076 if (MI) { 1077 // Finally, install the macro. 1078 PP.addLoadedMacroInfo(II, MI, Hint); 1079 } 1080 } 1081 } AddLoadedMacroInfo(PP, Hint); 1082 1083 while (true) { 1084 // Advance to the next record, but if we get to the end of the block, don't 1085 // pop it (removing all the abbreviations from the cursor) since we want to 1086 // be able to reseek within the block and read entries. 1087 unsigned Flags = llvm::BitstreamCursor::AF_DontPopBlockAtEnd; 1088 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags); 1089 1090 switch (Entry.Kind) { 1091 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1092 case llvm::BitstreamEntry::Error: 1093 Error("malformed block record in AST file"); 1094 return; 1095 case llvm::BitstreamEntry::EndBlock: 1096 return; 1097 case llvm::BitstreamEntry::Record: 1098 // The interesting case. 1099 break; 1100 } 1101 1102 // Read a record. 1103 const char *BlobStart = 0; 1104 unsigned BlobLen = 0; 1105 Record.clear(); 1106 PreprocessorRecordTypes RecType = 1107 (PreprocessorRecordTypes)Stream.ReadRecord(Entry.ID, Record, BlobStart, 1108 BlobLen); 1109 switch (RecType) { 1110 case PP_MACRO_OBJECT_LIKE: 1111 case PP_MACRO_FUNCTION_LIKE: { 1112 // If we already have a macro, that means that we've hit the end 1113 // of the definition of the macro we were looking for. We're 1114 // done. 1115 if (Macro) 1116 return; 1117 1118 IdentifierInfo *II = getLocalIdentifier(F, Record[0]); 1119 if (II == 0) { 1120 Error("macro must have a name in AST file"); 1121 return; 1122 } 1123 1124 unsigned GlobalID = getGlobalMacroID(F, Record[1]); 1125 1126 // If this macro has already been loaded, don't do so again. 1127 if (MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS]) 1128 return; 1129 1130 SubmoduleID GlobalSubmoduleID = getGlobalSubmoduleID(F, Record[2]); 1131 unsigned NextIndex = 3; 1132 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex); 1133 MacroInfo *MI = PP.AllocateMacroInfo(Loc); 1134 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex)); 1135 1136 // Record this macro. 1137 MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS] = MI; 1138 1139 SourceLocation UndefLoc = ReadSourceLocation(F, Record, NextIndex); 1140 if (UndefLoc.isValid()) 1141 MI->setUndefLoc(UndefLoc); 1142 1143 MI->setIsUsed(Record[NextIndex++]); 1144 MI->setIsFromAST(); 1145 1146 bool IsPublic = Record[NextIndex++]; 1147 MI->setVisibility(IsPublic, ReadSourceLocation(F, Record, NextIndex)); 1148 1149 if (RecType == PP_MACRO_FUNCTION_LIKE) { 1150 // Decode function-like macro info. 1151 bool isC99VarArgs = Record[NextIndex++]; 1152 bool isGNUVarArgs = Record[NextIndex++]; 1153 bool hasCommaPasting = Record[NextIndex++]; 1154 MacroArgs.clear(); 1155 unsigned NumArgs = Record[NextIndex++]; 1156 for (unsigned i = 0; i != NumArgs; ++i) 1157 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++])); 1158 1159 // Install function-like macro info. 1160 MI->setIsFunctionLike(); 1161 if (isC99VarArgs) MI->setIsC99Varargs(); 1162 if (isGNUVarArgs) MI->setIsGNUVarargs(); 1163 if (hasCommaPasting) MI->setHasCommaPasting(); 1164 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(), 1165 PP.getPreprocessorAllocator()); 1166 } 1167 1168 if (DeserializationListener) 1169 DeserializationListener->MacroRead(GlobalID, MI); 1170 1171 // If an update record marked this as undefined, do so now. 1172 // FIXME: Only if the submodule this update came from is visible? 1173 MacroUpdatesMap::iterator Update = MacroUpdates.find(GlobalID); 1174 if (Update != MacroUpdates.end()) { 1175 if (MI->getUndefLoc().isInvalid()) { 1176 for (unsigned I = 0, N = Update->second.size(); I != N; ++I) { 1177 bool Hidden = false; 1178 if (unsigned SubmoduleID = Update->second[I].first) { 1179 if (Module *Owner = getSubmodule(SubmoduleID)) { 1180 if (Owner->NameVisibility == Module::Hidden) { 1181 // Note that this #undef is hidden. 1182 Hidden = true; 1183 1184 // Record this hiding for later. 1185 HiddenNamesMap[Owner].push_back( 1186 HiddenName(II, MI, Update->second[I].second.UndefLoc)); 1187 } 1188 } 1189 } 1190 1191 if (!Hidden) { 1192 MI->setUndefLoc(Update->second[I].second.UndefLoc); 1193 if (PPMutationListener *Listener = PP.getPPMutationListener()) 1194 Listener->UndefinedMacro(MI); 1195 break; 1196 } 1197 } 1198 } 1199 MacroUpdates.erase(Update); 1200 } 1201 1202 // Determine whether this macro definition is visible. 1203 bool Hidden = !MI->isPublic(); 1204 if (!Hidden && GlobalSubmoduleID) { 1205 if (Module *Owner = getSubmodule(GlobalSubmoduleID)) { 1206 if (Owner->NameVisibility == Module::Hidden) { 1207 // The owning module is not visible, and this macro definition 1208 // should not be, either. 1209 Hidden = true; 1210 1211 // Note that this macro definition was hidden because its owning 1212 // module is not yet visible. 1213 HiddenNamesMap[Owner].push_back(HiddenName(II, MI)); 1214 } 1215 } 1216 } 1217 MI->setHidden(Hidden); 1218 1219 // Make sure we install the macro once we're done. 1220 AddLoadedMacroInfo.MI = MI; 1221 AddLoadedMacroInfo.II = II; 1222 1223 // Remember that we saw this macro last so that we add the tokens that 1224 // form its body to it. 1225 Macro = MI; 1226 1227 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() && 1228 Record[NextIndex]) { 1229 // We have a macro definition. Register the association 1230 PreprocessedEntityID 1231 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]); 1232 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 1233 PPRec.RegisterMacroDefinition(Macro, 1234 PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true)); 1235 } 1236 1237 ++NumMacrosRead; 1238 break; 1239 } 1240 1241 case PP_TOKEN: { 1242 // If we see a TOKEN before a PP_MACRO_*, then the file is 1243 // erroneous, just pretend we didn't see this. 1244 if (Macro == 0) break; 1245 1246 Token Tok; 1247 Tok.startToken(); 1248 Tok.setLocation(ReadSourceLocation(F, Record[0])); 1249 Tok.setLength(Record[1]); 1250 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2])) 1251 Tok.setIdentifierInfo(II); 1252 Tok.setKind((tok::TokenKind)Record[3]); 1253 Tok.setFlag((Token::TokenFlags)Record[4]); 1254 Macro->AddTokenToBody(Tok); 1255 break; 1256 } 1257 } 1258 } 1259} 1260 1261PreprocessedEntityID 1262ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const { 1263 ContinuousRangeMap<uint32_t, int, 2>::const_iterator 1264 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS); 1265 assert(I != M.PreprocessedEntityRemap.end() 1266 && "Invalid index into preprocessed entity index remap"); 1267 1268 return LocalID + I->second; 1269} 1270 1271unsigned HeaderFileInfoTrait::ComputeHash(const char *path) { 1272 return llvm::HashString(llvm::sys::path::filename(path)); 1273} 1274 1275HeaderFileInfoTrait::internal_key_type 1276HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; } 1277 1278bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) { 1279 if (strcmp(a, b) == 0) 1280 return true; 1281 1282 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b)) 1283 return false; 1284 1285 // Determine whether the actual files are equivalent. 1286 bool Result = false; 1287 if (llvm::sys::fs::equivalent(a, b, Result)) 1288 return false; 1289 1290 return Result; 1291} 1292 1293std::pair<unsigned, unsigned> 1294HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) { 1295 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d); 1296 unsigned DataLen = (unsigned) *d++; 1297 return std::make_pair(KeyLen + 1, DataLen); 1298} 1299 1300HeaderFileInfoTrait::data_type 1301HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d, 1302 unsigned DataLen) { 1303 const unsigned char *End = d + DataLen; 1304 using namespace clang::io; 1305 HeaderFileInfo HFI; 1306 unsigned Flags = *d++; 1307 HFI.isImport = (Flags >> 5) & 0x01; 1308 HFI.isPragmaOnce = (Flags >> 4) & 0x01; 1309 HFI.DirInfo = (Flags >> 2) & 0x03; 1310 HFI.Resolved = (Flags >> 1) & 0x01; 1311 HFI.IndexHeaderMapHeader = Flags & 0x01; 1312 HFI.NumIncludes = ReadUnalignedLE16(d); 1313 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M, 1314 ReadUnalignedLE32(d)); 1315 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) { 1316 // The framework offset is 1 greater than the actual offset, 1317 // since 0 is used as an indicator for "no framework name". 1318 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1); 1319 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName); 1320 } 1321 1322 assert(End == d && "Wrong data length in HeaderFileInfo deserialization"); 1323 (void)End; 1324 1325 // This HeaderFileInfo was externally loaded. 1326 HFI.External = true; 1327 return HFI; 1328} 1329 1330void ASTReader::setIdentifierIsMacro(IdentifierInfo *II, ArrayRef<MacroID> IDs){ 1331 II->setHadMacroDefinition(true); 1332 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard"); 1333 PendingMacroIDs[II].append(IDs.begin(), IDs.end()); 1334} 1335 1336void ASTReader::ReadDefinedMacros() { 1337 // Note that we are loading defined macros. 1338 Deserializing Macros(this); 1339 1340 for (ModuleReverseIterator I = ModuleMgr.rbegin(), 1341 E = ModuleMgr.rend(); I != E; ++I) { 1342 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor; 1343 1344 // If there was no preprocessor block, skip this file. 1345 if (!MacroCursor.getBitStreamReader()) 1346 continue; 1347 1348 llvm::BitstreamCursor Cursor = MacroCursor; 1349 Cursor.JumpToBit((*I)->MacroStartOffset); 1350 1351 RecordData Record; 1352 while (true) { 1353 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks(); 1354 1355 switch (E.Kind) { 1356 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1357 case llvm::BitstreamEntry::Error: 1358 Error("malformed block record in AST file"); 1359 return; 1360 case llvm::BitstreamEntry::EndBlock: 1361 goto NextCursor; 1362 1363 case llvm::BitstreamEntry::Record: 1364 const char *BlobStart; 1365 unsigned BlobLen; 1366 Record.clear(); 1367 switch (Cursor.ReadRecord(E.ID, Record, BlobStart, BlobLen)) { 1368 default: // Default behavior: ignore. 1369 break; 1370 1371 case PP_MACRO_OBJECT_LIKE: 1372 case PP_MACRO_FUNCTION_LIKE: 1373 getLocalIdentifier(**I, Record[0]); 1374 break; 1375 1376 case PP_TOKEN: 1377 // Ignore tokens. 1378 break; 1379 } 1380 break; 1381 } 1382 } 1383 NextCursor: ; 1384 } 1385} 1386 1387namespace { 1388 /// \brief Visitor class used to look up identifirs in an AST file. 1389 class IdentifierLookupVisitor { 1390 StringRef Name; 1391 unsigned PriorGeneration; 1392 IdentifierInfo *Found; 1393 public: 1394 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration) 1395 : Name(Name), PriorGeneration(PriorGeneration), Found() { } 1396 1397 static bool visit(ModuleFile &M, void *UserData) { 1398 IdentifierLookupVisitor *This 1399 = static_cast<IdentifierLookupVisitor *>(UserData); 1400 1401 // If we've already searched this module file, skip it now. 1402 if (M.Generation <= This->PriorGeneration) 1403 return true; 1404 1405 ASTIdentifierLookupTable *IdTable 1406 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable; 1407 if (!IdTable) 1408 return false; 1409 1410 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), 1411 M, This->Found); 1412 1413 std::pair<const char*, unsigned> Key(This->Name.begin(), 1414 This->Name.size()); 1415 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Trait); 1416 if (Pos == IdTable->end()) 1417 return false; 1418 1419 // Dereferencing the iterator has the effect of building the 1420 // IdentifierInfo node and populating it with the various 1421 // declarations it needs. 1422 This->Found = *Pos; 1423 return true; 1424 } 1425 1426 // \brief Retrieve the identifier info found within the module 1427 // files. 1428 IdentifierInfo *getIdentifierInfo() const { return Found; } 1429 }; 1430} 1431 1432void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) { 1433 // Note that we are loading an identifier. 1434 Deserializing AnIdentifier(this); 1435 1436 unsigned PriorGeneration = 0; 1437 if (getContext().getLangOpts().Modules) 1438 PriorGeneration = IdentifierGeneration[&II]; 1439 1440 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration); 1441 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor); 1442 markIdentifierUpToDate(&II); 1443} 1444 1445void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) { 1446 if (!II) 1447 return; 1448 1449 II->setOutOfDate(false); 1450 1451 // Update the generation for this identifier. 1452 if (getContext().getLangOpts().Modules) 1453 IdentifierGeneration[II] = CurrentGeneration; 1454} 1455 1456llvm::PointerIntPair<const FileEntry *, 1, bool> 1457ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) { 1458 // If this ID is bogus, just return an empty input file. 1459 if (ID == 0 || ID > F.InputFilesLoaded.size()) 1460 return InputFile(); 1461 1462 // If we've already loaded this input file, return it. 1463 if (F.InputFilesLoaded[ID-1].getPointer()) 1464 return F.InputFilesLoaded[ID-1]; 1465 1466 // Go find this input file. 1467 llvm::BitstreamCursor &Cursor = F.InputFilesCursor; 1468 SavedStreamPosition SavedPosition(Cursor); 1469 Cursor.JumpToBit(F.InputFileOffsets[ID-1]); 1470 1471 unsigned Code = Cursor.ReadCode(); 1472 RecordData Record; 1473 const char *BlobStart = 0; 1474 unsigned BlobLen = 0; 1475 switch ((InputFileRecordTypes)Cursor.ReadRecord(Code, Record, 1476 BlobStart, BlobLen)) { 1477 case INPUT_FILE: { 1478 unsigned StoredID = Record[0]; 1479 assert(ID == StoredID && "Bogus stored ID or offset"); 1480 (void)StoredID; 1481 off_t StoredSize = (off_t)Record[1]; 1482 time_t StoredTime = (time_t)Record[2]; 1483 bool Overridden = (bool)Record[3]; 1484 1485 // Get the file entry for this input file. 1486 StringRef OrigFilename(BlobStart, BlobLen); 1487 std::string Filename = OrigFilename; 1488 MaybeAddSystemRootToFilename(F, Filename); 1489 const FileEntry *File 1490 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime) 1491 : FileMgr.getFile(Filename, /*OpenFile=*/false); 1492 1493 // If we didn't find the file, resolve it relative to the 1494 // original directory from which this AST file was created. 1495 if (File == 0 && !F.OriginalDir.empty() && !CurrentDir.empty() && 1496 F.OriginalDir != CurrentDir) { 1497 std::string Resolved = resolveFileRelativeToOriginalDir(Filename, 1498 F.OriginalDir, 1499 CurrentDir); 1500 if (!Resolved.empty()) 1501 File = FileMgr.getFile(Resolved); 1502 } 1503 1504 // For an overridden file, create a virtual file with the stored 1505 // size/timestamp. 1506 if (Overridden && File == 0) { 1507 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime); 1508 } 1509 1510 if (File == 0) { 1511 if (Complain) { 1512 std::string ErrorStr = "could not find file '"; 1513 ErrorStr += Filename; 1514 ErrorStr += "' referenced by AST file"; 1515 Error(ErrorStr.c_str()); 1516 } 1517 return InputFile(); 1518 } 1519 1520 // Note that we've loaded this input file. 1521 F.InputFilesLoaded[ID-1] = InputFile(File, Overridden); 1522 1523 // Check if there was a request to override the contents of the file 1524 // that was part of the precompiled header. Overridding such a file 1525 // can lead to problems when lexing using the source locations from the 1526 // PCH. 1527 SourceManager &SM = getSourceManager(); 1528 if (!Overridden && SM.isFileOverridden(File)) { 1529 Error(diag::err_fe_pch_file_overridden, Filename); 1530 // After emitting the diagnostic, recover by disabling the override so 1531 // that the original file will be used. 1532 SM.disableFileContentsOverride(File); 1533 // The FileEntry is a virtual file entry with the size of the contents 1534 // that would override the original contents. Set it to the original's 1535 // size/time. 1536 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File), 1537 StoredSize, StoredTime); 1538 } 1539 1540 // For an overridden file, there is nothing to validate. 1541 if (Overridden) 1542 return InputFile(File, Overridden); 1543 1544 if ((StoredSize != File->getSize() 1545#if !defined(LLVM_ON_WIN32) 1546 // In our regression testing, the Windows file system seems to 1547 // have inconsistent modification times that sometimes 1548 // erroneously trigger this error-handling path. 1549 || StoredTime != File->getModificationTime() 1550#endif 1551 )) { 1552 if (Complain) 1553 Error(diag::err_fe_pch_file_modified, Filename); 1554 1555 return InputFile(); 1556 } 1557 1558 return InputFile(File, Overridden); 1559 } 1560 } 1561 1562 return InputFile(); 1563} 1564 1565const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) { 1566 ModuleFile &M = ModuleMgr.getPrimaryModule(); 1567 std::string Filename = filenameStrRef; 1568 MaybeAddSystemRootToFilename(M, Filename); 1569 const FileEntry *File = FileMgr.getFile(Filename); 1570 if (File == 0 && !M.OriginalDir.empty() && !CurrentDir.empty() && 1571 M.OriginalDir != CurrentDir) { 1572 std::string resolved = resolveFileRelativeToOriginalDir(Filename, 1573 M.OriginalDir, 1574 CurrentDir); 1575 if (!resolved.empty()) 1576 File = FileMgr.getFile(resolved); 1577 } 1578 1579 return File; 1580} 1581 1582/// \brief If we are loading a relocatable PCH file, and the filename is 1583/// not an absolute path, add the system root to the beginning of the file 1584/// name. 1585void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M, 1586 std::string &Filename) { 1587 // If this is not a relocatable PCH file, there's nothing to do. 1588 if (!M.RelocatablePCH) 1589 return; 1590 1591 if (Filename.empty() || llvm::sys::path::is_absolute(Filename)) 1592 return; 1593 1594 if (isysroot.empty()) { 1595 // If no system root was given, default to '/' 1596 Filename.insert(Filename.begin(), '/'); 1597 return; 1598 } 1599 1600 unsigned Length = isysroot.size(); 1601 if (isysroot[Length - 1] != '/') 1602 Filename.insert(Filename.begin(), '/'); 1603 1604 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end()); 1605} 1606 1607ASTReader::ASTReadResult 1608ASTReader::ReadControlBlock(ModuleFile &F, 1609 SmallVectorImpl<ImportedModule> &Loaded, 1610 unsigned ClientLoadCapabilities) { 1611 llvm::BitstreamCursor &Stream = F.Stream; 1612 1613 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) { 1614 Error("malformed block record in AST file"); 1615 return Failure; 1616 } 1617 1618 // Read all of the records and blocks in the control block. 1619 RecordData Record; 1620 while (1) { 1621 llvm::BitstreamEntry Entry = Stream.advance(); 1622 1623 switch (Entry.Kind) { 1624 case llvm::BitstreamEntry::Error: 1625 Error("malformed block record in AST file"); 1626 return Failure; 1627 case llvm::BitstreamEntry::EndBlock: 1628 // Validate all of the input files. 1629 if (!DisableValidation) { 1630 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 1631 for (unsigned I = 0, N = Record[0]; I < N; ++I) 1632 if (!getInputFile(F, I+1, Complain).getPointer()) 1633 return OutOfDate; 1634 } 1635 return Success; 1636 1637 case llvm::BitstreamEntry::SubBlock: 1638 switch (Entry.ID) { 1639 case INPUT_FILES_BLOCK_ID: 1640 F.InputFilesCursor = Stream; 1641 if (Stream.SkipBlock() || // Skip with the main cursor 1642 // Read the abbreviations 1643 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) { 1644 Error("malformed block record in AST file"); 1645 return Failure; 1646 } 1647 continue; 1648 1649 default: 1650 if (Stream.SkipBlock()) { 1651 Error("malformed block record in AST file"); 1652 return Failure; 1653 } 1654 continue; 1655 } 1656 1657 case llvm::BitstreamEntry::Record: 1658 // The interesting case. 1659 break; 1660 } 1661 1662 // Read and process a record. 1663 Record.clear(); 1664 const char *BlobStart = 0; 1665 unsigned BlobLen = 0; 1666 switch ((ControlRecordTypes)Stream.ReadRecord(Entry.ID, Record, 1667 BlobStart, BlobLen)) { 1668 case METADATA: { 1669 if (Record[0] != VERSION_MAJOR && !DisableValidation) { 1670 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 1671 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old 1672 : diag::warn_pch_version_too_new); 1673 return VersionMismatch; 1674 } 1675 1676 bool hasErrors = Record[5]; 1677 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) { 1678 Diag(diag::err_pch_with_compiler_errors); 1679 return HadErrors; 1680 } 1681 1682 F.RelocatablePCH = Record[4]; 1683 1684 const std::string &CurBranch = getClangFullRepositoryVersion(); 1685 StringRef ASTBranch(BlobStart, BlobLen); 1686 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) { 1687 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 1688 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch; 1689 return VersionMismatch; 1690 } 1691 break; 1692 } 1693 1694 case IMPORTS: { 1695 // Load each of the imported PCH files. 1696 unsigned Idx = 0, N = Record.size(); 1697 while (Idx < N) { 1698 // Read information about the AST file. 1699 ModuleKind ImportedKind = (ModuleKind)Record[Idx++]; 1700 // The import location will be the local one for now; we will adjust 1701 // all import locations of module imports after the global source 1702 // location info are setup. 1703 SourceLocation ImportLoc = 1704 SourceLocation::getFromRawEncoding(Record[Idx++]); 1705 unsigned Length = Record[Idx++]; 1706 SmallString<128> ImportedFile(Record.begin() + Idx, 1707 Record.begin() + Idx + Length); 1708 Idx += Length; 1709 1710 // Load the AST file. 1711 switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded, 1712 ClientLoadCapabilities)) { 1713 case Failure: return Failure; 1714 // If we have to ignore the dependency, we'll have to ignore this too. 1715 case OutOfDate: return OutOfDate; 1716 case VersionMismatch: return VersionMismatch; 1717 case ConfigurationMismatch: return ConfigurationMismatch; 1718 case HadErrors: return HadErrors; 1719 case Success: break; 1720 } 1721 } 1722 break; 1723 } 1724 1725 case LANGUAGE_OPTIONS: { 1726 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 1727 if (Listener && &F == *ModuleMgr.begin() && 1728 ParseLanguageOptions(Record, Complain, *Listener) && 1729 !DisableValidation) 1730 return ConfigurationMismatch; 1731 break; 1732 } 1733 1734 case TARGET_OPTIONS: { 1735 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0; 1736 if (Listener && &F == *ModuleMgr.begin() && 1737 ParseTargetOptions(Record, Complain, *Listener) && 1738 !DisableValidation) 1739 return ConfigurationMismatch; 1740 break; 1741 } 1742 1743 case DIAGNOSTIC_OPTIONS: { 1744 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0; 1745 if (Listener && &F == *ModuleMgr.begin() && 1746 ParseDiagnosticOptions(Record, Complain, *Listener) && 1747 !DisableValidation) 1748 return ConfigurationMismatch; 1749 break; 1750 } 1751 1752 case FILE_SYSTEM_OPTIONS: { 1753 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0; 1754 if (Listener && &F == *ModuleMgr.begin() && 1755 ParseFileSystemOptions(Record, Complain, *Listener) && 1756 !DisableValidation) 1757 return ConfigurationMismatch; 1758 break; 1759 } 1760 1761 case HEADER_SEARCH_OPTIONS: { 1762 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0; 1763 if (Listener && &F == *ModuleMgr.begin() && 1764 ParseHeaderSearchOptions(Record, Complain, *Listener) && 1765 !DisableValidation) 1766 return ConfigurationMismatch; 1767 break; 1768 } 1769 1770 case PREPROCESSOR_OPTIONS: { 1771 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0; 1772 if (Listener && &F == *ModuleMgr.begin() && 1773 ParsePreprocessorOptions(Record, Complain, *Listener, 1774 SuggestedPredefines) && 1775 !DisableValidation) 1776 return ConfigurationMismatch; 1777 break; 1778 } 1779 1780 case ORIGINAL_FILE: 1781 F.OriginalSourceFileID = FileID::get(Record[0]); 1782 F.ActualOriginalSourceFileName.assign(BlobStart, BlobLen); 1783 F.OriginalSourceFileName = F.ActualOriginalSourceFileName; 1784 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName); 1785 break; 1786 1787 case ORIGINAL_FILE_ID: 1788 F.OriginalSourceFileID = FileID::get(Record[0]); 1789 break; 1790 1791 case ORIGINAL_PCH_DIR: 1792 F.OriginalDir.assign(BlobStart, BlobLen); 1793 break; 1794 1795 case INPUT_FILE_OFFSETS: 1796 F.InputFileOffsets = (const uint32_t *)BlobStart; 1797 F.InputFilesLoaded.resize(Record[0]); 1798 break; 1799 } 1800 } 1801} 1802 1803bool ASTReader::ReadASTBlock(ModuleFile &F) { 1804 llvm::BitstreamCursor &Stream = F.Stream; 1805 1806 if (Stream.EnterSubBlock(AST_BLOCK_ID)) { 1807 Error("malformed block record in AST file"); 1808 return true; 1809 } 1810 1811 // Read all of the records and blocks for the AST file. 1812 RecordData Record; 1813 while (1) { 1814 llvm::BitstreamEntry Entry = Stream.advance(); 1815 1816 switch (Entry.Kind) { 1817 case llvm::BitstreamEntry::Error: 1818 Error("error at end of module block in AST file"); 1819 return true; 1820 case llvm::BitstreamEntry::EndBlock: { 1821 DeclContext *DC = Context.getTranslationUnitDecl(); 1822 if (!DC->hasExternalVisibleStorage() && DC->hasExternalLexicalStorage()) 1823 DC->setMustBuildLookupTable(); 1824 1825 return false; 1826 } 1827 case llvm::BitstreamEntry::SubBlock: 1828 switch (Entry.ID) { 1829 case DECLTYPES_BLOCK_ID: 1830 // We lazily load the decls block, but we want to set up the 1831 // DeclsCursor cursor to point into it. Clone our current bitcode 1832 // cursor to it, enter the block and read the abbrevs in that block. 1833 // With the main cursor, we just skip over it. 1834 F.DeclsCursor = Stream; 1835 if (Stream.SkipBlock() || // Skip with the main cursor. 1836 // Read the abbrevs. 1837 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) { 1838 Error("malformed block record in AST file"); 1839 return true; 1840 } 1841 break; 1842 1843 case DECL_UPDATES_BLOCK_ID: 1844 if (Stream.SkipBlock()) { 1845 Error("malformed block record in AST file"); 1846 return true; 1847 } 1848 break; 1849 1850 case PREPROCESSOR_BLOCK_ID: 1851 F.MacroCursor = Stream; 1852 if (!PP.getExternalSource()) 1853 PP.setExternalSource(this); 1854 1855 if (Stream.SkipBlock() || 1856 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) { 1857 Error("malformed block record in AST file"); 1858 return true; 1859 } 1860 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo(); 1861 break; 1862 1863 case PREPROCESSOR_DETAIL_BLOCK_ID: 1864 F.PreprocessorDetailCursor = Stream; 1865 if (Stream.SkipBlock() || 1866 ReadBlockAbbrevs(F.PreprocessorDetailCursor, 1867 PREPROCESSOR_DETAIL_BLOCK_ID)) { 1868 Error("malformed preprocessor detail record in AST file"); 1869 return true; 1870 } 1871 F.PreprocessorDetailStartOffset 1872 = F.PreprocessorDetailCursor.GetCurrentBitNo(); 1873 1874 if (!PP.getPreprocessingRecord()) 1875 PP.createPreprocessingRecord(); 1876 if (!PP.getPreprocessingRecord()->getExternalSource()) 1877 PP.getPreprocessingRecord()->SetExternalSource(*this); 1878 break; 1879 1880 case SOURCE_MANAGER_BLOCK_ID: 1881 if (ReadSourceManagerBlock(F)) 1882 return true; 1883 break; 1884 1885 case SUBMODULE_BLOCK_ID: 1886 if (ReadSubmoduleBlock(F)) 1887 return true; 1888 break; 1889 1890 case COMMENTS_BLOCK_ID: { 1891 llvm::BitstreamCursor C = Stream; 1892 if (Stream.SkipBlock() || 1893 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) { 1894 Error("malformed comments block in AST file"); 1895 return true; 1896 } 1897 CommentsCursors.push_back(std::make_pair(C, &F)); 1898 break; 1899 } 1900 1901 default: 1902 if (Stream.SkipBlock()) { 1903 Error("malformed block record in AST file"); 1904 return true; 1905 } 1906 break; 1907 } 1908 continue; 1909 1910 case llvm::BitstreamEntry::Record: 1911 // The interesting case. 1912 break; 1913 } 1914 1915 // Read and process a record. 1916 Record.clear(); 1917 const char *BlobStart = 0; 1918 unsigned BlobLen = 0; 1919 switch ((ASTRecordTypes)Stream.ReadRecord(Entry.ID, Record, 1920 BlobStart, BlobLen)) { 1921 default: // Default behavior: ignore. 1922 break; 1923 1924 case TYPE_OFFSET: { 1925 if (F.LocalNumTypes != 0) { 1926 Error("duplicate TYPE_OFFSET record in AST file"); 1927 return true; 1928 } 1929 F.TypeOffsets = (const uint32_t *)BlobStart; 1930 F.LocalNumTypes = Record[0]; 1931 unsigned LocalBaseTypeIndex = Record[1]; 1932 F.BaseTypeIndex = getTotalNumTypes(); 1933 1934 if (F.LocalNumTypes > 0) { 1935 // Introduce the global -> local mapping for types within this module. 1936 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F)); 1937 1938 // Introduce the local -> global mapping for types within this module. 1939 F.TypeRemap.insertOrReplace( 1940 std::make_pair(LocalBaseTypeIndex, 1941 F.BaseTypeIndex - LocalBaseTypeIndex)); 1942 1943 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes); 1944 } 1945 break; 1946 } 1947 1948 case DECL_OFFSET: { 1949 if (F.LocalNumDecls != 0) { 1950 Error("duplicate DECL_OFFSET record in AST file"); 1951 return true; 1952 } 1953 F.DeclOffsets = (const DeclOffset *)BlobStart; 1954 F.LocalNumDecls = Record[0]; 1955 unsigned LocalBaseDeclID = Record[1]; 1956 F.BaseDeclID = getTotalNumDecls(); 1957 1958 if (F.LocalNumDecls > 0) { 1959 // Introduce the global -> local mapping for declarations within this 1960 // module. 1961 GlobalDeclMap.insert( 1962 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F)); 1963 1964 // Introduce the local -> global mapping for declarations within this 1965 // module. 1966 F.DeclRemap.insertOrReplace( 1967 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID)); 1968 1969 // Introduce the global -> local mapping for declarations within this 1970 // module. 1971 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID; 1972 1973 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls); 1974 } 1975 break; 1976 } 1977 1978 case TU_UPDATE_LEXICAL: { 1979 DeclContext *TU = Context.getTranslationUnitDecl(); 1980 DeclContextInfo &Info = F.DeclContextInfos[TU]; 1981 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart); 1982 Info.NumLexicalDecls 1983 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair)); 1984 TU->setHasExternalLexicalStorage(true); 1985 break; 1986 } 1987 1988 case UPDATE_VISIBLE: { 1989 unsigned Idx = 0; 1990 serialization::DeclID ID = ReadDeclID(F, Record, Idx); 1991 ASTDeclContextNameLookupTable *Table = 1992 ASTDeclContextNameLookupTable::Create( 1993 (const unsigned char *)BlobStart + Record[Idx++], 1994 (const unsigned char *)BlobStart, 1995 ASTDeclContextNameLookupTrait(*this, F)); 1996 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU? 1997 DeclContext *TU = Context.getTranslationUnitDecl(); 1998 F.DeclContextInfos[TU].NameLookupTableData = Table; 1999 TU->setHasExternalVisibleStorage(true); 2000 } else 2001 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F)); 2002 break; 2003 } 2004 2005 case IDENTIFIER_TABLE: 2006 F.IdentifierTableData = BlobStart; 2007 if (Record[0]) { 2008 F.IdentifierLookupTable 2009 = ASTIdentifierLookupTable::Create( 2010 (const unsigned char *)F.IdentifierTableData + Record[0], 2011 (const unsigned char *)F.IdentifierTableData, 2012 ASTIdentifierLookupTrait(*this, F)); 2013 2014 PP.getIdentifierTable().setExternalIdentifierLookup(this); 2015 } 2016 break; 2017 2018 case IDENTIFIER_OFFSET: { 2019 if (F.LocalNumIdentifiers != 0) { 2020 Error("duplicate IDENTIFIER_OFFSET record in AST file"); 2021 return true; 2022 } 2023 F.IdentifierOffsets = (const uint32_t *)BlobStart; 2024 F.LocalNumIdentifiers = Record[0]; 2025 unsigned LocalBaseIdentifierID = Record[1]; 2026 F.BaseIdentifierID = getTotalNumIdentifiers(); 2027 2028 if (F.LocalNumIdentifiers > 0) { 2029 // Introduce the global -> local mapping for identifiers within this 2030 // module. 2031 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1, 2032 &F)); 2033 2034 // Introduce the local -> global mapping for identifiers within this 2035 // module. 2036 F.IdentifierRemap.insertOrReplace( 2037 std::make_pair(LocalBaseIdentifierID, 2038 F.BaseIdentifierID - LocalBaseIdentifierID)); 2039 2040 IdentifiersLoaded.resize(IdentifiersLoaded.size() 2041 + F.LocalNumIdentifiers); 2042 } 2043 break; 2044 } 2045 2046 case EXTERNAL_DEFINITIONS: 2047 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2048 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I])); 2049 break; 2050 2051 case SPECIAL_TYPES: 2052 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2053 SpecialTypes.push_back(getGlobalTypeID(F, Record[I])); 2054 break; 2055 2056 case STATISTICS: 2057 TotalNumStatements += Record[0]; 2058 TotalNumMacros += Record[1]; 2059 TotalLexicalDeclContexts += Record[2]; 2060 TotalVisibleDeclContexts += Record[3]; 2061 break; 2062 2063 case UNUSED_FILESCOPED_DECLS: 2064 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2065 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I])); 2066 break; 2067 2068 case DELEGATING_CTORS: 2069 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2070 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I])); 2071 break; 2072 2073 case WEAK_UNDECLARED_IDENTIFIERS: 2074 if (Record.size() % 4 != 0) { 2075 Error("invalid weak identifiers record"); 2076 return true; 2077 } 2078 2079 // FIXME: Ignore weak undeclared identifiers from non-original PCH 2080 // files. This isn't the way to do it :) 2081 WeakUndeclaredIdentifiers.clear(); 2082 2083 // Translate the weak, undeclared identifiers into global IDs. 2084 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) { 2085 WeakUndeclaredIdentifiers.push_back( 2086 getGlobalIdentifierID(F, Record[I++])); 2087 WeakUndeclaredIdentifiers.push_back( 2088 getGlobalIdentifierID(F, Record[I++])); 2089 WeakUndeclaredIdentifiers.push_back( 2090 ReadSourceLocation(F, Record, I).getRawEncoding()); 2091 WeakUndeclaredIdentifiers.push_back(Record[I++]); 2092 } 2093 break; 2094 2095 case LOCALLY_SCOPED_EXTERN_C_DECLS: 2096 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2097 LocallyScopedExternCDecls.push_back(getGlobalDeclID(F, Record[I])); 2098 break; 2099 2100 case SELECTOR_OFFSETS: { 2101 F.SelectorOffsets = (const uint32_t *)BlobStart; 2102 F.LocalNumSelectors = Record[0]; 2103 unsigned LocalBaseSelectorID = Record[1]; 2104 F.BaseSelectorID = getTotalNumSelectors(); 2105 2106 if (F.LocalNumSelectors > 0) { 2107 // Introduce the global -> local mapping for selectors within this 2108 // module. 2109 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F)); 2110 2111 // Introduce the local -> global mapping for selectors within this 2112 // module. 2113 F.SelectorRemap.insertOrReplace( 2114 std::make_pair(LocalBaseSelectorID, 2115 F.BaseSelectorID - LocalBaseSelectorID)); 2116 2117 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors); 2118 } 2119 break; 2120 } 2121 2122 case METHOD_POOL: 2123 F.SelectorLookupTableData = (const unsigned char *)BlobStart; 2124 if (Record[0]) 2125 F.SelectorLookupTable 2126 = ASTSelectorLookupTable::Create( 2127 F.SelectorLookupTableData + Record[0], 2128 F.SelectorLookupTableData, 2129 ASTSelectorLookupTrait(*this, F)); 2130 TotalNumMethodPoolEntries += Record[1]; 2131 break; 2132 2133 case REFERENCED_SELECTOR_POOL: 2134 if (!Record.empty()) { 2135 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) { 2136 ReferencedSelectorsData.push_back(getGlobalSelectorID(F, 2137 Record[Idx++])); 2138 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx). 2139 getRawEncoding()); 2140 } 2141 } 2142 break; 2143 2144 case PP_COUNTER_VALUE: 2145 if (!Record.empty() && Listener) 2146 Listener->ReadCounter(F, Record[0]); 2147 break; 2148 2149 case FILE_SORTED_DECLS: 2150 F.FileSortedDecls = (const DeclID *)BlobStart; 2151 F.NumFileSortedDecls = Record[0]; 2152 break; 2153 2154 case SOURCE_LOCATION_OFFSETS: { 2155 F.SLocEntryOffsets = (const uint32_t *)BlobStart; 2156 F.LocalNumSLocEntries = Record[0]; 2157 unsigned SLocSpaceSize = Record[1]; 2158 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) = 2159 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, 2160 SLocSpaceSize); 2161 // Make our entry in the range map. BaseID is negative and growing, so 2162 // we invert it. Because we invert it, though, we need the other end of 2163 // the range. 2164 unsigned RangeStart = 2165 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1; 2166 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F)); 2167 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset); 2168 2169 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing. 2170 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0); 2171 GlobalSLocOffsetMap.insert( 2172 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset 2173 - SLocSpaceSize,&F)); 2174 2175 // Initialize the remapping table. 2176 // Invalid stays invalid. 2177 F.SLocRemap.insert(std::make_pair(0U, 0)); 2178 // This module. Base was 2 when being compiled. 2179 F.SLocRemap.insert(std::make_pair(2U, 2180 static_cast<int>(F.SLocEntryBaseOffset - 2))); 2181 2182 TotalNumSLocEntries += F.LocalNumSLocEntries; 2183 break; 2184 } 2185 2186 case MODULE_OFFSET_MAP: { 2187 // Additional remapping information. 2188 const unsigned char *Data = (const unsigned char*)BlobStart; 2189 const unsigned char *DataEnd = Data + BlobLen; 2190 2191 // Continuous range maps we may be updating in our module. 2192 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap); 2193 ContinuousRangeMap<uint32_t, int, 2>::Builder 2194 IdentifierRemap(F.IdentifierRemap); 2195 ContinuousRangeMap<uint32_t, int, 2>::Builder 2196 MacroRemap(F.MacroRemap); 2197 ContinuousRangeMap<uint32_t, int, 2>::Builder 2198 PreprocessedEntityRemap(F.PreprocessedEntityRemap); 2199 ContinuousRangeMap<uint32_t, int, 2>::Builder 2200 SubmoduleRemap(F.SubmoduleRemap); 2201 ContinuousRangeMap<uint32_t, int, 2>::Builder 2202 SelectorRemap(F.SelectorRemap); 2203 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap); 2204 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap); 2205 2206 while(Data < DataEnd) { 2207 uint16_t Len = io::ReadUnalignedLE16(Data); 2208 StringRef Name = StringRef((const char*)Data, Len); 2209 Data += Len; 2210 ModuleFile *OM = ModuleMgr.lookup(Name); 2211 if (!OM) { 2212 Error("SourceLocation remap refers to unknown module"); 2213 return true; 2214 } 2215 2216 uint32_t SLocOffset = io::ReadUnalignedLE32(Data); 2217 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data); 2218 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data); 2219 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data); 2220 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data); 2221 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data); 2222 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data); 2223 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data); 2224 2225 // Source location offset is mapped to OM->SLocEntryBaseOffset. 2226 SLocRemap.insert(std::make_pair(SLocOffset, 2227 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset))); 2228 IdentifierRemap.insert( 2229 std::make_pair(IdentifierIDOffset, 2230 OM->BaseIdentifierID - IdentifierIDOffset)); 2231 MacroRemap.insert(std::make_pair(MacroIDOffset, 2232 OM->BaseMacroID - MacroIDOffset)); 2233 PreprocessedEntityRemap.insert( 2234 std::make_pair(PreprocessedEntityIDOffset, 2235 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset)); 2236 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset, 2237 OM->BaseSubmoduleID - SubmoduleIDOffset)); 2238 SelectorRemap.insert(std::make_pair(SelectorIDOffset, 2239 OM->BaseSelectorID - SelectorIDOffset)); 2240 DeclRemap.insert(std::make_pair(DeclIDOffset, 2241 OM->BaseDeclID - DeclIDOffset)); 2242 2243 TypeRemap.insert(std::make_pair(TypeIndexOffset, 2244 OM->BaseTypeIndex - TypeIndexOffset)); 2245 2246 // Global -> local mappings. 2247 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset; 2248 } 2249 break; 2250 } 2251 2252 case SOURCE_MANAGER_LINE_TABLE: 2253 if (ParseLineTable(F, Record)) 2254 return true; 2255 break; 2256 2257 case SOURCE_LOCATION_PRELOADS: { 2258 // Need to transform from the local view (1-based IDs) to the global view, 2259 // which is based off F.SLocEntryBaseID. 2260 if (!F.PreloadSLocEntries.empty()) { 2261 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file"); 2262 return true; 2263 } 2264 2265 F.PreloadSLocEntries.swap(Record); 2266 break; 2267 } 2268 2269 case EXT_VECTOR_DECLS: 2270 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2271 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I])); 2272 break; 2273 2274 case VTABLE_USES: 2275 if (Record.size() % 3 != 0) { 2276 Error("Invalid VTABLE_USES record"); 2277 return true; 2278 } 2279 2280 // Later tables overwrite earlier ones. 2281 // FIXME: Modules will have some trouble with this. This is clearly not 2282 // the right way to do this. 2283 VTableUses.clear(); 2284 2285 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) { 2286 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++])); 2287 VTableUses.push_back( 2288 ReadSourceLocation(F, Record, Idx).getRawEncoding()); 2289 VTableUses.push_back(Record[Idx++]); 2290 } 2291 break; 2292 2293 case DYNAMIC_CLASSES: 2294 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2295 DynamicClasses.push_back(getGlobalDeclID(F, Record[I])); 2296 break; 2297 2298 case PENDING_IMPLICIT_INSTANTIATIONS: 2299 if (PendingInstantiations.size() % 2 != 0) { 2300 Error("Invalid existing PendingInstantiations"); 2301 return true; 2302 } 2303 2304 if (Record.size() % 2 != 0) { 2305 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block"); 2306 return true; 2307 } 2308 2309 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 2310 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++])); 2311 PendingInstantiations.push_back( 2312 ReadSourceLocation(F, Record, I).getRawEncoding()); 2313 } 2314 break; 2315 2316 case SEMA_DECL_REFS: 2317 // Later tables overwrite earlier ones. 2318 // FIXME: Modules will have some trouble with this. 2319 SemaDeclRefs.clear(); 2320 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2321 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 2322 break; 2323 2324 case PPD_ENTITIES_OFFSETS: { 2325 F.PreprocessedEntityOffsets = (const PPEntityOffset *)BlobStart; 2326 assert(BlobLen % sizeof(PPEntityOffset) == 0); 2327 F.NumPreprocessedEntities = BlobLen / sizeof(PPEntityOffset); 2328 2329 unsigned LocalBasePreprocessedEntityID = Record[0]; 2330 2331 unsigned StartingID; 2332 if (!PP.getPreprocessingRecord()) 2333 PP.createPreprocessingRecord(); 2334 if (!PP.getPreprocessingRecord()->getExternalSource()) 2335 PP.getPreprocessingRecord()->SetExternalSource(*this); 2336 StartingID 2337 = PP.getPreprocessingRecord() 2338 ->allocateLoadedEntities(F.NumPreprocessedEntities); 2339 F.BasePreprocessedEntityID = StartingID; 2340 2341 if (F.NumPreprocessedEntities > 0) { 2342 // Introduce the global -> local mapping for preprocessed entities in 2343 // this module. 2344 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F)); 2345 2346 // Introduce the local -> global mapping for preprocessed entities in 2347 // this module. 2348 F.PreprocessedEntityRemap.insertOrReplace( 2349 std::make_pair(LocalBasePreprocessedEntityID, 2350 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID)); 2351 } 2352 2353 break; 2354 } 2355 2356 case DECL_UPDATE_OFFSETS: { 2357 if (Record.size() % 2 != 0) { 2358 Error("invalid DECL_UPDATE_OFFSETS block in AST file"); 2359 return true; 2360 } 2361 for (unsigned I = 0, N = Record.size(); I != N; I += 2) 2362 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])] 2363 .push_back(std::make_pair(&F, Record[I+1])); 2364 break; 2365 } 2366 2367 case DECL_REPLACEMENTS: { 2368 if (Record.size() % 3 != 0) { 2369 Error("invalid DECL_REPLACEMENTS block in AST file"); 2370 return true; 2371 } 2372 for (unsigned I = 0, N = Record.size(); I != N; I += 3) 2373 ReplacedDecls[getGlobalDeclID(F, Record[I])] 2374 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]); 2375 break; 2376 } 2377 2378 case OBJC_CATEGORIES_MAP: { 2379 if (F.LocalNumObjCCategoriesInMap != 0) { 2380 Error("duplicate OBJC_CATEGORIES_MAP record in AST file"); 2381 return true; 2382 } 2383 2384 F.LocalNumObjCCategoriesInMap = Record[0]; 2385 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)BlobStart; 2386 break; 2387 } 2388 2389 case OBJC_CATEGORIES: 2390 F.ObjCCategories.swap(Record); 2391 break; 2392 2393 case CXX_BASE_SPECIFIER_OFFSETS: { 2394 if (F.LocalNumCXXBaseSpecifiers != 0) { 2395 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file"); 2396 return true; 2397 } 2398 2399 F.LocalNumCXXBaseSpecifiers = Record[0]; 2400 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart; 2401 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers; 2402 break; 2403 } 2404 2405 case DIAG_PRAGMA_MAPPINGS: 2406 if (F.PragmaDiagMappings.empty()) 2407 F.PragmaDiagMappings.swap(Record); 2408 else 2409 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(), 2410 Record.begin(), Record.end()); 2411 break; 2412 2413 case CUDA_SPECIAL_DECL_REFS: 2414 // Later tables overwrite earlier ones. 2415 // FIXME: Modules will have trouble with this. 2416 CUDASpecialDeclRefs.clear(); 2417 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2418 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 2419 break; 2420 2421 case HEADER_SEARCH_TABLE: { 2422 F.HeaderFileInfoTableData = BlobStart; 2423 F.LocalNumHeaderFileInfos = Record[1]; 2424 F.HeaderFileFrameworkStrings = BlobStart + Record[2]; 2425 if (Record[0]) { 2426 F.HeaderFileInfoTable 2427 = HeaderFileInfoLookupTable::Create( 2428 (const unsigned char *)F.HeaderFileInfoTableData + Record[0], 2429 (const unsigned char *)F.HeaderFileInfoTableData, 2430 HeaderFileInfoTrait(*this, F, 2431 &PP.getHeaderSearchInfo(), 2432 BlobStart + Record[2])); 2433 2434 PP.getHeaderSearchInfo().SetExternalSource(this); 2435 if (!PP.getHeaderSearchInfo().getExternalLookup()) 2436 PP.getHeaderSearchInfo().SetExternalLookup(this); 2437 } 2438 break; 2439 } 2440 2441 case FP_PRAGMA_OPTIONS: 2442 // Later tables overwrite earlier ones. 2443 FPPragmaOptions.swap(Record); 2444 break; 2445 2446 case OPENCL_EXTENSIONS: 2447 // Later tables overwrite earlier ones. 2448 OpenCLExtensions.swap(Record); 2449 break; 2450 2451 case TENTATIVE_DEFINITIONS: 2452 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2453 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I])); 2454 break; 2455 2456 case KNOWN_NAMESPACES: 2457 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2458 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I])); 2459 break; 2460 2461 case IMPORTED_MODULES: { 2462 if (F.Kind != MK_Module) { 2463 // If we aren't loading a module (which has its own exports), make 2464 // all of the imported modules visible. 2465 // FIXME: Deal with macros-only imports. 2466 for (unsigned I = 0, N = Record.size(); I != N; ++I) { 2467 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I])) 2468 ImportedModules.push_back(GlobalID); 2469 } 2470 } 2471 break; 2472 } 2473 2474 case LOCAL_REDECLARATIONS: { 2475 F.RedeclarationChains.swap(Record); 2476 break; 2477 } 2478 2479 case LOCAL_REDECLARATIONS_MAP: { 2480 if (F.LocalNumRedeclarationsInMap != 0) { 2481 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file"); 2482 return true; 2483 } 2484 2485 F.LocalNumRedeclarationsInMap = Record[0]; 2486 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)BlobStart; 2487 break; 2488 } 2489 2490 case MERGED_DECLARATIONS: { 2491 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) { 2492 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]); 2493 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID]; 2494 for (unsigned N = Record[Idx++]; N > 0; --N) 2495 Decls.push_back(getGlobalDeclID(F, Record[Idx++])); 2496 } 2497 break; 2498 } 2499 2500 case MACRO_OFFSET: { 2501 if (F.LocalNumMacros != 0) { 2502 Error("duplicate MACRO_OFFSET record in AST file"); 2503 return true; 2504 } 2505 F.MacroOffsets = (const uint32_t *)BlobStart; 2506 F.LocalNumMacros = Record[0]; 2507 unsigned LocalBaseMacroID = Record[1]; 2508 F.BaseMacroID = getTotalNumMacros(); 2509 2510 if (F.LocalNumMacros > 0) { 2511 // Introduce the global -> local mapping for macros within this module. 2512 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F)); 2513 2514 // Introduce the local -> global mapping for macros within this module. 2515 F.MacroRemap.insertOrReplace( 2516 std::make_pair(LocalBaseMacroID, 2517 F.BaseMacroID - LocalBaseMacroID)); 2518 2519 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros); 2520 } 2521 break; 2522 } 2523 2524 case MACRO_UPDATES: { 2525 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 2526 MacroID ID = getGlobalMacroID(F, Record[I++]); 2527 if (I == N) 2528 break; 2529 2530 SourceLocation UndefLoc = ReadSourceLocation(F, Record, I); 2531 SubmoduleID SubmoduleID = getGlobalSubmoduleID(F, Record[I++]);; 2532 MacroUpdate Update; 2533 Update.UndefLoc = UndefLoc; 2534 MacroUpdates[ID].push_back(std::make_pair(SubmoduleID, Update)); 2535 } 2536 break; 2537 } 2538 } 2539 } 2540} 2541 2542void ASTReader::makeNamesVisible(const HiddenNames &Names) { 2543 for (unsigned I = 0, N = Names.size(); I != N; ++I) { 2544 switch (Names[I].getKind()) { 2545 case HiddenName::Declaration: 2546 Names[I].getDecl()->Hidden = false; 2547 break; 2548 2549 case HiddenName::MacroVisibility: { 2550 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro(); 2551 Macro.second->setHidden(!Macro.second->isPublic()); 2552 if (Macro.second->isDefined()) { 2553 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second); 2554 } 2555 break; 2556 } 2557 2558 case HiddenName::MacroUndef: { 2559 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro(); 2560 if (Macro.second->isDefined()) { 2561 Macro.second->setUndefLoc(Names[I].getMacroUndefLoc()); 2562 if (PPMutationListener *Listener = PP.getPPMutationListener()) 2563 Listener->UndefinedMacro(Macro.second); 2564 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second); 2565 } 2566 break; 2567 } 2568 } 2569 } 2570} 2571 2572void ASTReader::makeModuleVisible(Module *Mod, 2573 Module::NameVisibilityKind NameVisibility) { 2574 llvm::SmallPtrSet<Module *, 4> Visited; 2575 SmallVector<Module *, 4> Stack; 2576 Stack.push_back(Mod); 2577 while (!Stack.empty()) { 2578 Mod = Stack.back(); 2579 Stack.pop_back(); 2580 2581 if (NameVisibility <= Mod->NameVisibility) { 2582 // This module already has this level of visibility (or greater), so 2583 // there is nothing more to do. 2584 continue; 2585 } 2586 2587 if (!Mod->isAvailable()) { 2588 // Modules that aren't available cannot be made visible. 2589 continue; 2590 } 2591 2592 // Update the module's name visibility. 2593 Mod->NameVisibility = NameVisibility; 2594 2595 // If we've already deserialized any names from this module, 2596 // mark them as visible. 2597 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod); 2598 if (Hidden != HiddenNamesMap.end()) { 2599 makeNamesVisible(Hidden->second); 2600 HiddenNamesMap.erase(Hidden); 2601 } 2602 2603 // Push any non-explicit submodules onto the stack to be marked as 2604 // visible. 2605 for (Module::submodule_iterator Sub = Mod->submodule_begin(), 2606 SubEnd = Mod->submodule_end(); 2607 Sub != SubEnd; ++Sub) { 2608 if (!(*Sub)->IsExplicit && Visited.insert(*Sub)) 2609 Stack.push_back(*Sub); 2610 } 2611 2612 // Push any exported modules onto the stack to be marked as visible. 2613 bool AnyWildcard = false; 2614 bool UnrestrictedWildcard = false; 2615 SmallVector<Module *, 4> WildcardRestrictions; 2616 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) { 2617 Module *Exported = Mod->Exports[I].getPointer(); 2618 if (!Mod->Exports[I].getInt()) { 2619 // Export a named module directly; no wildcards involved. 2620 if (Visited.insert(Exported)) 2621 Stack.push_back(Exported); 2622 2623 continue; 2624 } 2625 2626 // Wildcard export: export all of the imported modules that match 2627 // the given pattern. 2628 AnyWildcard = true; 2629 if (UnrestrictedWildcard) 2630 continue; 2631 2632 if (Module *Restriction = Mod->Exports[I].getPointer()) 2633 WildcardRestrictions.push_back(Restriction); 2634 else { 2635 WildcardRestrictions.clear(); 2636 UnrestrictedWildcard = true; 2637 } 2638 } 2639 2640 // If there were any wildcards, push any imported modules that were 2641 // re-exported by the wildcard restriction. 2642 if (!AnyWildcard) 2643 continue; 2644 2645 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) { 2646 Module *Imported = Mod->Imports[I]; 2647 if (!Visited.insert(Imported)) 2648 continue; 2649 2650 bool Acceptable = UnrestrictedWildcard; 2651 if (!Acceptable) { 2652 // Check whether this module meets one of the restrictions. 2653 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) { 2654 Module *Restriction = WildcardRestrictions[R]; 2655 if (Imported == Restriction || Imported->isSubModuleOf(Restriction)) { 2656 Acceptable = true; 2657 break; 2658 } 2659 } 2660 } 2661 2662 if (!Acceptable) 2663 continue; 2664 2665 Stack.push_back(Imported); 2666 } 2667 } 2668} 2669 2670ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName, 2671 ModuleKind Type, 2672 SourceLocation ImportLoc, 2673 unsigned ClientLoadCapabilities) { 2674 // Bump the generation number. 2675 unsigned PreviousGeneration = CurrentGeneration++; 2676 2677 unsigned NumModules = ModuleMgr.size(); 2678 SmallVector<ImportedModule, 4> Loaded; 2679 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc, 2680 /*ImportedBy=*/0, Loaded, 2681 ClientLoadCapabilities)) { 2682 case Failure: 2683 case OutOfDate: 2684 case VersionMismatch: 2685 case ConfigurationMismatch: 2686 case HadErrors: 2687 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end()); 2688 return ReadResult; 2689 2690 case Success: 2691 break; 2692 } 2693 2694 // Here comes stuff that we only do once the entire chain is loaded. 2695 2696 // Load the AST blocks of all of the modules that we loaded. 2697 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(), 2698 MEnd = Loaded.end(); 2699 M != MEnd; ++M) { 2700 ModuleFile &F = *M->Mod; 2701 2702 // Read the AST block. 2703 if (ReadASTBlock(F)) 2704 return Failure; 2705 2706 // Once read, set the ModuleFile bit base offset and update the size in 2707 // bits of all files we've seen. 2708 F.GlobalBitOffset = TotalModulesSizeInBits; 2709 TotalModulesSizeInBits += F.SizeInBits; 2710 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F)); 2711 2712 // Preload SLocEntries. 2713 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) { 2714 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID; 2715 // Load it through the SourceManager and don't call ReadSLocEntry() 2716 // directly because the entry may have already been loaded in which case 2717 // calling ReadSLocEntry() directly would trigger an assertion in 2718 // SourceManager. 2719 SourceMgr.getLoadedSLocEntryByID(Index); 2720 } 2721 } 2722 2723 // Setup the import locations. 2724 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(), 2725 MEnd = Loaded.end(); 2726 M != MEnd; ++M) { 2727 ModuleFile &F = *M->Mod; 2728 if (!M->ImportedBy) 2729 F.ImportLoc = M->ImportLoc; 2730 else 2731 F.ImportLoc = ReadSourceLocation(*M->ImportedBy, 2732 M->ImportLoc.getRawEncoding()); 2733 } 2734 2735 // Mark all of the identifiers in the identifier table as being out of date, 2736 // so that various accessors know to check the loaded modules when the 2737 // identifier is used. 2738 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(), 2739 IdEnd = PP.getIdentifierTable().end(); 2740 Id != IdEnd; ++Id) 2741 Id->second->setOutOfDate(true); 2742 2743 // Resolve any unresolved module exports. 2744 for (unsigned I = 0, N = UnresolvedModuleImportExports.size(); I != N; ++I) { 2745 UnresolvedModuleImportExport &Unresolved = UnresolvedModuleImportExports[I]; 2746 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID); 2747 Module *ResolvedMod = getSubmodule(GlobalID); 2748 2749 if (Unresolved.IsImport) { 2750 if (ResolvedMod) 2751 Unresolved.Mod->Imports.push_back(ResolvedMod); 2752 continue; 2753 } 2754 2755 if (ResolvedMod || Unresolved.IsWildcard) 2756 Unresolved.Mod->Exports.push_back( 2757 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard)); 2758 } 2759 UnresolvedModuleImportExports.clear(); 2760 2761 InitializeContext(); 2762 2763 if (DeserializationListener) 2764 DeserializationListener->ReaderInitialized(this); 2765 2766 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule(); 2767 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) { 2768 PrimaryModule.OriginalSourceFileID 2769 = FileID::get(PrimaryModule.SLocEntryBaseID 2770 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1); 2771 2772 // If this AST file is a precompiled preamble, then set the 2773 // preamble file ID of the source manager to the file source file 2774 // from which the preamble was built. 2775 if (Type == MK_Preamble) { 2776 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID); 2777 } else if (Type == MK_MainFile) { 2778 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID); 2779 } 2780 } 2781 2782 // For any Objective-C class definitions we have already loaded, make sure 2783 // that we load any additional categories. 2784 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) { 2785 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(), 2786 ObjCClassesLoaded[I], 2787 PreviousGeneration); 2788 } 2789 2790 return Success; 2791} 2792 2793ASTReader::ASTReadResult 2794ASTReader::ReadASTCore(StringRef FileName, 2795 ModuleKind Type, 2796 SourceLocation ImportLoc, 2797 ModuleFile *ImportedBy, 2798 SmallVectorImpl<ImportedModule> &Loaded, 2799 unsigned ClientLoadCapabilities) { 2800 ModuleFile *M; 2801 bool NewModule; 2802 std::string ErrorStr; 2803 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportLoc, 2804 ImportedBy, CurrentGeneration, 2805 ErrorStr); 2806 2807 if (!M) { 2808 // We couldn't load the module. 2809 std::string Msg = "Unable to load module \"" + FileName.str() + "\": " 2810 + ErrorStr; 2811 Error(Msg); 2812 return Failure; 2813 } 2814 2815 if (!NewModule) { 2816 // We've already loaded this module. 2817 return Success; 2818 } 2819 2820 // FIXME: This seems rather a hack. Should CurrentDir be part of the 2821 // module? 2822 if (FileName != "-") { 2823 CurrentDir = llvm::sys::path::parent_path(FileName); 2824 if (CurrentDir.empty()) CurrentDir = "."; 2825 } 2826 2827 ModuleFile &F = *M; 2828 llvm::BitstreamCursor &Stream = F.Stream; 2829 Stream.init(F.StreamFile); 2830 F.SizeInBits = F.Buffer->getBufferSize() * 8; 2831 2832 // Sniff for the signature. 2833 if (Stream.Read(8) != 'C' || 2834 Stream.Read(8) != 'P' || 2835 Stream.Read(8) != 'C' || 2836 Stream.Read(8) != 'H') { 2837 Diag(diag::err_not_a_pch_file) << FileName; 2838 return Failure; 2839 } 2840 2841 // This is used for compatibility with older PCH formats. 2842 bool HaveReadControlBlock = false; 2843 2844 while (1) { 2845 llvm::BitstreamEntry Entry = Stream.advance(); 2846 2847 switch (Entry.Kind) { 2848 case llvm::BitstreamEntry::Error: 2849 case llvm::BitstreamEntry::EndBlock: 2850 case llvm::BitstreamEntry::Record: 2851 Error("invalid record at top-level of AST file"); 2852 return Failure; 2853 2854 case llvm::BitstreamEntry::SubBlock: 2855 break; 2856 } 2857 2858 // We only know the control subblock ID. 2859 switch (Entry.ID) { 2860 case llvm::bitc::BLOCKINFO_BLOCK_ID: 2861 if (Stream.ReadBlockInfoBlock()) { 2862 Error("malformed BlockInfoBlock in AST file"); 2863 return Failure; 2864 } 2865 break; 2866 case CONTROL_BLOCK_ID: 2867 HaveReadControlBlock = true; 2868 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) { 2869 case Success: 2870 break; 2871 2872 case Failure: return Failure; 2873 case OutOfDate: return OutOfDate; 2874 case VersionMismatch: return VersionMismatch; 2875 case ConfigurationMismatch: return ConfigurationMismatch; 2876 case HadErrors: return HadErrors; 2877 } 2878 break; 2879 case AST_BLOCK_ID: 2880 if (!HaveReadControlBlock) { 2881 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2882 Diag(diag::warn_pch_version_too_old); 2883 return VersionMismatch; 2884 } 2885 2886 // Record that we've loaded this module. 2887 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc)); 2888 return Success; 2889 2890 default: 2891 if (Stream.SkipBlock()) { 2892 Error("malformed block record in AST file"); 2893 return Failure; 2894 } 2895 break; 2896 } 2897 } 2898 2899 return Success; 2900} 2901 2902void ASTReader::InitializeContext() { 2903 // If there's a listener, notify them that we "read" the translation unit. 2904 if (DeserializationListener) 2905 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, 2906 Context.getTranslationUnitDecl()); 2907 2908 // Make sure we load the declaration update records for the translation unit, 2909 // if there are any. 2910 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID, 2911 Context.getTranslationUnitDecl()); 2912 2913 // FIXME: Find a better way to deal with collisions between these 2914 // built-in types. Right now, we just ignore the problem. 2915 2916 // Load the special types. 2917 if (SpecialTypes.size() >= NumSpecialTypeIDs) { 2918 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) { 2919 if (!Context.CFConstantStringTypeDecl) 2920 Context.setCFConstantStringType(GetType(String)); 2921 } 2922 2923 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) { 2924 QualType FileType = GetType(File); 2925 if (FileType.isNull()) { 2926 Error("FILE type is NULL"); 2927 return; 2928 } 2929 2930 if (!Context.FILEDecl) { 2931 if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) 2932 Context.setFILEDecl(Typedef->getDecl()); 2933 else { 2934 const TagType *Tag = FileType->getAs<TagType>(); 2935 if (!Tag) { 2936 Error("Invalid FILE type in AST file"); 2937 return; 2938 } 2939 Context.setFILEDecl(Tag->getDecl()); 2940 } 2941 } 2942 } 2943 2944 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) { 2945 QualType Jmp_bufType = GetType(Jmp_buf); 2946 if (Jmp_bufType.isNull()) { 2947 Error("jmp_buf type is NULL"); 2948 return; 2949 } 2950 2951 if (!Context.jmp_bufDecl) { 2952 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) 2953 Context.setjmp_bufDecl(Typedef->getDecl()); 2954 else { 2955 const TagType *Tag = Jmp_bufType->getAs<TagType>(); 2956 if (!Tag) { 2957 Error("Invalid jmp_buf type in AST file"); 2958 return; 2959 } 2960 Context.setjmp_bufDecl(Tag->getDecl()); 2961 } 2962 } 2963 } 2964 2965 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) { 2966 QualType Sigjmp_bufType = GetType(Sigjmp_buf); 2967 if (Sigjmp_bufType.isNull()) { 2968 Error("sigjmp_buf type is NULL"); 2969 return; 2970 } 2971 2972 if (!Context.sigjmp_bufDecl) { 2973 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) 2974 Context.setsigjmp_bufDecl(Typedef->getDecl()); 2975 else { 2976 const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); 2977 assert(Tag && "Invalid sigjmp_buf type in AST file"); 2978 Context.setsigjmp_bufDecl(Tag->getDecl()); 2979 } 2980 } 2981 } 2982 2983 if (unsigned ObjCIdRedef 2984 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) { 2985 if (Context.ObjCIdRedefinitionType.isNull()) 2986 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef); 2987 } 2988 2989 if (unsigned ObjCClassRedef 2990 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) { 2991 if (Context.ObjCClassRedefinitionType.isNull()) 2992 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef); 2993 } 2994 2995 if (unsigned ObjCSelRedef 2996 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) { 2997 if (Context.ObjCSelRedefinitionType.isNull()) 2998 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef); 2999 } 3000 3001 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) { 3002 QualType Ucontext_tType = GetType(Ucontext_t); 3003 if (Ucontext_tType.isNull()) { 3004 Error("ucontext_t type is NULL"); 3005 return; 3006 } 3007 3008 if (!Context.ucontext_tDecl) { 3009 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>()) 3010 Context.setucontext_tDecl(Typedef->getDecl()); 3011 else { 3012 const TagType *Tag = Ucontext_tType->getAs<TagType>(); 3013 assert(Tag && "Invalid ucontext_t type in AST file"); 3014 Context.setucontext_tDecl(Tag->getDecl()); 3015 } 3016 } 3017 } 3018 } 3019 3020 ReadPragmaDiagnosticMappings(Context.getDiagnostics()); 3021 3022 // If there were any CUDA special declarations, deserialize them. 3023 if (!CUDASpecialDeclRefs.empty()) { 3024 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!"); 3025 Context.setcudaConfigureCallDecl( 3026 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0]))); 3027 } 3028 3029 // Re-export any modules that were imported by a non-module AST file. 3030 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) { 3031 if (Module *Imported = getSubmodule(ImportedModules[I])) 3032 makeModuleVisible(Imported, Module::AllVisible); 3033 } 3034 ImportedModules.clear(); 3035} 3036 3037void ASTReader::finalizeForWriting() { 3038 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(), 3039 HiddenEnd = HiddenNamesMap.end(); 3040 Hidden != HiddenEnd; ++Hidden) { 3041 makeNamesVisible(Hidden->second); 3042 } 3043 HiddenNamesMap.clear(); 3044} 3045 3046/// \brief Retrieve the name of the original source file name 3047/// directly from the AST file, without actually loading the AST 3048/// file. 3049std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName, 3050 FileManager &FileMgr, 3051 DiagnosticsEngine &Diags) { 3052 // Open the AST file. 3053 std::string ErrStr; 3054 OwningPtr<llvm::MemoryBuffer> Buffer; 3055 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr)); 3056 if (!Buffer) { 3057 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr; 3058 return std::string(); 3059 } 3060 3061 // Initialize the stream 3062 llvm::BitstreamReader StreamFile; 3063 llvm::BitstreamCursor Stream; 3064 StreamFile.init((const unsigned char *)Buffer->getBufferStart(), 3065 (const unsigned char *)Buffer->getBufferEnd()); 3066 Stream.init(StreamFile); 3067 3068 // Sniff for the signature. 3069 if (Stream.Read(8) != 'C' || 3070 Stream.Read(8) != 'P' || 3071 Stream.Read(8) != 'C' || 3072 Stream.Read(8) != 'H') { 3073 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName; 3074 return std::string(); 3075 } 3076 3077 // Scan for the CONTROL_BLOCK_ID block. 3078 llvm::BitstreamEntry Entry; 3079 RecordData Record; 3080 3081 bool FoundControlBlock = false; 3082 while (!FoundControlBlock) { 3083 Entry = Stream.advance(); 3084 switch (Entry.Kind) { 3085 case llvm::BitstreamEntry::Error: 3086 case llvm::BitstreamEntry::EndBlock: 3087 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName; 3088 return std::string(); 3089 3090 case llvm::BitstreamEntry::Record: 3091 // Ignore top-level records. 3092 Stream.skipRecord(Entry.ID); 3093 break; 3094 3095 case llvm::BitstreamEntry::SubBlock: 3096 if (Entry.ID == CONTROL_BLOCK_ID) { 3097 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) { 3098 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 3099 return std::string(); 3100 } 3101 FoundControlBlock = true; 3102 break; 3103 } 3104 3105 if (Stream.SkipBlock()) { 3106 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 3107 return std::string(); 3108 } 3109 } 3110 } 3111 3112 // Scan for ORIGINAL_FILE. 3113 while (1) { 3114 Entry = Stream.advanceSkippingSubblocks(); 3115 if (Entry.Kind == llvm::BitstreamEntry::EndBlock) 3116 return std::string(); 3117 3118 if (Entry.Kind != llvm::BitstreamEntry::Record) { 3119 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 3120 return std::string(); 3121 } 3122 3123 Record.clear(); 3124 const char *BlobStart = 0; 3125 unsigned BlobLen = 0; 3126 if (Stream.ReadRecord(Entry.ID, Record, BlobStart, BlobLen) 3127 == ORIGINAL_FILE) 3128 return std::string(BlobStart, BlobLen); 3129 } 3130} 3131 3132namespace { 3133 class SimplePCHValidator : public ASTReaderListener { 3134 const LangOptions &ExistingLangOpts; 3135 const TargetOptions &ExistingTargetOpts; 3136 const PreprocessorOptions &ExistingPPOpts; 3137 FileManager &FileMgr; 3138 3139 public: 3140 SimplePCHValidator(const LangOptions &ExistingLangOpts, 3141 const TargetOptions &ExistingTargetOpts, 3142 const PreprocessorOptions &ExistingPPOpts, 3143 FileManager &FileMgr) 3144 : ExistingLangOpts(ExistingLangOpts), 3145 ExistingTargetOpts(ExistingTargetOpts), 3146 ExistingPPOpts(ExistingPPOpts), 3147 FileMgr(FileMgr) 3148 { 3149 } 3150 3151 virtual bool ReadLanguageOptions(const LangOptions &LangOpts, 3152 bool Complain) { 3153 return checkLanguageOptions(ExistingLangOpts, LangOpts, 0); 3154 } 3155 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts, 3156 bool Complain) { 3157 return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0); 3158 } 3159 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 3160 bool Complain, 3161 std::string &SuggestedPredefines) { 3162 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, 0, FileMgr, 3163 SuggestedPredefines); 3164 } 3165 }; 3166} 3167 3168bool ASTReader::readASTFileControlBlock(StringRef Filename, 3169 FileManager &FileMgr, 3170 ASTReaderListener &Listener) { 3171 // Open the AST file. 3172 std::string ErrStr; 3173 OwningPtr<llvm::MemoryBuffer> Buffer; 3174 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr)); 3175 if (!Buffer) { 3176 return true; 3177 } 3178 3179 // Initialize the stream 3180 llvm::BitstreamReader StreamFile; 3181 llvm::BitstreamCursor Stream; 3182 StreamFile.init((const unsigned char *)Buffer->getBufferStart(), 3183 (const unsigned char *)Buffer->getBufferEnd()); 3184 Stream.init(StreamFile); 3185 3186 // Sniff for the signature. 3187 if (Stream.Read(8) != 'C' || 3188 Stream.Read(8) != 'P' || 3189 Stream.Read(8) != 'C' || 3190 Stream.Read(8) != 'H') { 3191 return true; 3192 } 3193 3194 RecordData Record; 3195 bool InControlBlock = false; 3196 while (!Stream.AtEndOfStream()) { 3197 unsigned Code = Stream.ReadCode(); 3198 3199 if (Code == llvm::bitc::ENTER_SUBBLOCK) { 3200 unsigned BlockID = Stream.ReadSubBlockID(); 3201 3202 // We only know the control subblock ID. 3203 switch (BlockID) { 3204 case CONTROL_BLOCK_ID: 3205 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) { 3206 return true; 3207 } else { 3208 InControlBlock = true; 3209 } 3210 break; 3211 3212 default: 3213 if (Stream.SkipBlock()) 3214 return true; 3215 break; 3216 } 3217 continue; 3218 } 3219 3220 if (Code == llvm::bitc::END_BLOCK) { 3221 if (Stream.ReadBlockEnd()) { 3222 return true; 3223 } 3224 3225 InControlBlock = false; 3226 continue; 3227 } 3228 3229 if (Code == llvm::bitc::DEFINE_ABBREV) { 3230 Stream.ReadAbbrevRecord(); 3231 continue; 3232 } 3233 3234 Record.clear(); 3235 const char *BlobStart = 0; 3236 unsigned BlobLen = 0; 3237 unsigned RecCode = Stream.ReadRecord(Code, Record, BlobStart, BlobLen); 3238 if (InControlBlock) { 3239 switch ((ControlRecordTypes)RecCode) { 3240 case METADATA: { 3241 if (Record[0] != VERSION_MAJOR) { 3242 return true; 3243 } 3244 3245 const std::string &CurBranch = getClangFullRepositoryVersion(); 3246 StringRef ASTBranch(BlobStart, BlobLen); 3247 if (StringRef(CurBranch) != ASTBranch) 3248 return true; 3249 3250 break; 3251 } 3252 case LANGUAGE_OPTIONS: 3253 if (ParseLanguageOptions(Record, false, Listener)) 3254 return true; 3255 break; 3256 3257 case TARGET_OPTIONS: 3258 if (ParseTargetOptions(Record, false, Listener)) 3259 return true; 3260 break; 3261 3262 case DIAGNOSTIC_OPTIONS: 3263 if (ParseDiagnosticOptions(Record, false, Listener)) 3264 return true; 3265 break; 3266 3267 case FILE_SYSTEM_OPTIONS: 3268 if (ParseFileSystemOptions(Record, false, Listener)) 3269 return true; 3270 break; 3271 3272 case HEADER_SEARCH_OPTIONS: 3273 if (ParseHeaderSearchOptions(Record, false, Listener)) 3274 return true; 3275 break; 3276 3277 case PREPROCESSOR_OPTIONS: { 3278 std::string IgnoredSuggestedPredefines; 3279 if (ParsePreprocessorOptions(Record, false, Listener, 3280 IgnoredSuggestedPredefines)) 3281 return true; 3282 break; 3283 } 3284 3285 default: 3286 // No other validation to perform. 3287 break; 3288 } 3289 } 3290 } 3291 3292 return false; 3293} 3294 3295 3296bool ASTReader::isAcceptableASTFile(StringRef Filename, 3297 FileManager &FileMgr, 3298 const LangOptions &LangOpts, 3299 const TargetOptions &TargetOpts, 3300 const PreprocessorOptions &PPOpts) { 3301 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr); 3302 return !readASTFileControlBlock(Filename, FileMgr, validator); 3303} 3304 3305bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) { 3306 // Enter the submodule block. 3307 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) { 3308 Error("malformed submodule block record in AST file"); 3309 return true; 3310 } 3311 3312 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap(); 3313 bool First = true; 3314 Module *CurrentModule = 0; 3315 RecordData Record; 3316 while (true) { 3317 unsigned Code = F.Stream.ReadCode(); 3318 if (Code == llvm::bitc::END_BLOCK) { 3319 if (F.Stream.ReadBlockEnd()) { 3320 Error("error at end of submodule block in AST file"); 3321 return true; 3322 } 3323 return false; 3324 } 3325 3326 if (Code == llvm::bitc::ENTER_SUBBLOCK) { 3327 // No known subblocks, always skip them. 3328 F.Stream.ReadSubBlockID(); 3329 if (F.Stream.SkipBlock()) { 3330 Error("malformed block record in AST file"); 3331 return true; 3332 } 3333 continue; 3334 } 3335 3336 if (Code == llvm::bitc::DEFINE_ABBREV) { 3337 F.Stream.ReadAbbrevRecord(); 3338 continue; 3339 } 3340 3341 // Read a record. 3342 const char *BlobStart; 3343 unsigned BlobLen; 3344 Record.clear(); 3345 switch (F.Stream.ReadRecord(Code, Record, BlobStart, BlobLen)) { 3346 default: // Default behavior: ignore. 3347 break; 3348 3349 case SUBMODULE_DEFINITION: { 3350 if (First) { 3351 Error("missing submodule metadata record at beginning of block"); 3352 return true; 3353 } 3354 3355 if (Record.size() < 7) { 3356 Error("malformed module definition"); 3357 return true; 3358 } 3359 3360 StringRef Name(BlobStart, BlobLen); 3361 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]); 3362 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]); 3363 bool IsFramework = Record[2]; 3364 bool IsExplicit = Record[3]; 3365 bool IsSystem = Record[4]; 3366 bool InferSubmodules = Record[5]; 3367 bool InferExplicitSubmodules = Record[6]; 3368 bool InferExportWildcard = Record[7]; 3369 3370 Module *ParentModule = 0; 3371 if (Parent) 3372 ParentModule = getSubmodule(Parent); 3373 3374 // Retrieve this (sub)module from the module map, creating it if 3375 // necessary. 3376 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule, 3377 IsFramework, 3378 IsExplicit).first; 3379 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS; 3380 if (GlobalIndex >= SubmodulesLoaded.size() || 3381 SubmodulesLoaded[GlobalIndex]) { 3382 Error("too many submodules"); 3383 return true; 3384 } 3385 3386 CurrentModule->setASTFile(F.File); 3387 CurrentModule->IsFromModuleFile = true; 3388 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem; 3389 CurrentModule->InferSubmodules = InferSubmodules; 3390 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules; 3391 CurrentModule->InferExportWildcard = InferExportWildcard; 3392 if (DeserializationListener) 3393 DeserializationListener->ModuleRead(GlobalID, CurrentModule); 3394 3395 SubmodulesLoaded[GlobalIndex] = CurrentModule; 3396 3397 // Clear out link libraries; the module file has them. 3398 CurrentModule->LinkLibraries.clear(); 3399 break; 3400 } 3401 3402 case SUBMODULE_UMBRELLA_HEADER: { 3403 if (First) { 3404 Error("missing submodule metadata record at beginning of block"); 3405 return true; 3406 } 3407 3408 if (!CurrentModule) 3409 break; 3410 3411 StringRef FileName(BlobStart, BlobLen); 3412 if (const FileEntry *Umbrella = PP.getFileManager().getFile(FileName)) { 3413 if (!CurrentModule->getUmbrellaHeader()) 3414 ModMap.setUmbrellaHeader(CurrentModule, Umbrella); 3415 else if (CurrentModule->getUmbrellaHeader() != Umbrella) { 3416 Error("mismatched umbrella headers in submodule"); 3417 return true; 3418 } 3419 } 3420 break; 3421 } 3422 3423 case SUBMODULE_HEADER: { 3424 if (First) { 3425 Error("missing submodule metadata record at beginning of block"); 3426 return true; 3427 } 3428 3429 if (!CurrentModule) 3430 break; 3431 3432 // FIXME: Be more lazy about this! 3433 StringRef FileName(BlobStart, BlobLen); 3434 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) { 3435 if (std::find(CurrentModule->Headers.begin(), 3436 CurrentModule->Headers.end(), 3437 File) == CurrentModule->Headers.end()) 3438 ModMap.addHeader(CurrentModule, File, false); 3439 } 3440 break; 3441 } 3442 3443 case SUBMODULE_EXCLUDED_HEADER: { 3444 if (First) { 3445 Error("missing submodule metadata record at beginning of block"); 3446 return true; 3447 } 3448 3449 if (!CurrentModule) 3450 break; 3451 3452 // FIXME: Be more lazy about this! 3453 StringRef FileName(BlobStart, BlobLen); 3454 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) { 3455 if (std::find(CurrentModule->Headers.begin(), 3456 CurrentModule->Headers.end(), 3457 File) == CurrentModule->Headers.end()) 3458 ModMap.addHeader(CurrentModule, File, true); 3459 } 3460 break; 3461 } 3462 3463 case SUBMODULE_TOPHEADER: { 3464 if (First) { 3465 Error("missing submodule metadata record at beginning of block"); 3466 return true; 3467 } 3468 3469 if (!CurrentModule) 3470 break; 3471 3472 // FIXME: Be more lazy about this! 3473 StringRef FileName(BlobStart, BlobLen); 3474 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) 3475 CurrentModule->TopHeaders.insert(File); 3476 break; 3477 } 3478 3479 case SUBMODULE_UMBRELLA_DIR: { 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 StringRef DirName(BlobStart, BlobLen); 3489 if (const DirectoryEntry *Umbrella 3490 = PP.getFileManager().getDirectory(DirName)) { 3491 if (!CurrentModule->getUmbrellaDir()) 3492 ModMap.setUmbrellaDir(CurrentModule, Umbrella); 3493 else if (CurrentModule->getUmbrellaDir() != Umbrella) { 3494 Error("mismatched umbrella directories in submodule"); 3495 return true; 3496 } 3497 } 3498 break; 3499 } 3500 3501 case SUBMODULE_METADATA: { 3502 if (!First) { 3503 Error("submodule metadata record not at beginning of block"); 3504 return true; 3505 } 3506 First = false; 3507 3508 F.BaseSubmoduleID = getTotalNumSubmodules(); 3509 F.LocalNumSubmodules = Record[0]; 3510 unsigned LocalBaseSubmoduleID = Record[1]; 3511 if (F.LocalNumSubmodules > 0) { 3512 // Introduce the global -> local mapping for submodules within this 3513 // module. 3514 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F)); 3515 3516 // Introduce the local -> global mapping for submodules within this 3517 // module. 3518 F.SubmoduleRemap.insertOrReplace( 3519 std::make_pair(LocalBaseSubmoduleID, 3520 F.BaseSubmoduleID - LocalBaseSubmoduleID)); 3521 3522 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules); 3523 } 3524 break; 3525 } 3526 3527 case SUBMODULE_IMPORTS: { 3528 if (First) { 3529 Error("missing submodule metadata record at beginning of block"); 3530 return true; 3531 } 3532 3533 if (!CurrentModule) 3534 break; 3535 3536 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) { 3537 UnresolvedModuleImportExport Unresolved; 3538 Unresolved.File = &F; 3539 Unresolved.Mod = CurrentModule; 3540 Unresolved.ID = Record[Idx]; 3541 Unresolved.IsImport = true; 3542 Unresolved.IsWildcard = false; 3543 UnresolvedModuleImportExports.push_back(Unresolved); 3544 } 3545 break; 3546 } 3547 3548 case SUBMODULE_EXPORTS: { 3549 if (First) { 3550 Error("missing submodule metadata record at beginning of block"); 3551 return true; 3552 } 3553 3554 if (!CurrentModule) 3555 break; 3556 3557 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) { 3558 UnresolvedModuleImportExport Unresolved; 3559 Unresolved.File = &F; 3560 Unresolved.Mod = CurrentModule; 3561 Unresolved.ID = Record[Idx]; 3562 Unresolved.IsImport = false; 3563 Unresolved.IsWildcard = Record[Idx + 1]; 3564 UnresolvedModuleImportExports.push_back(Unresolved); 3565 } 3566 3567 // Once we've loaded the set of exports, there's no reason to keep 3568 // the parsed, unresolved exports around. 3569 CurrentModule->UnresolvedExports.clear(); 3570 break; 3571 } 3572 case SUBMODULE_REQUIRES: { 3573 if (First) { 3574 Error("missing submodule metadata record at beginning of block"); 3575 return true; 3576 } 3577 3578 if (!CurrentModule) 3579 break; 3580 3581 CurrentModule->addRequirement(StringRef(BlobStart, BlobLen), 3582 Context.getLangOpts(), 3583 Context.getTargetInfo()); 3584 break; 3585 } 3586 3587 case SUBMODULE_LINK_LIBRARY: 3588 if (First) { 3589 Error("missing submodule metadata record at beginning of block"); 3590 return true; 3591 } 3592 3593 if (!CurrentModule) 3594 break; 3595 3596 CurrentModule->LinkLibraries.push_back( 3597 Module::LinkLibrary(StringRef(BlobStart, BlobLen), 3598 Record[0])); 3599 break; 3600 } 3601 } 3602} 3603 3604/// \brief Parse the record that corresponds to a LangOptions data 3605/// structure. 3606/// 3607/// This routine parses the language options from the AST file and then gives 3608/// them to the AST listener if one is set. 3609/// 3610/// \returns true if the listener deems the file unacceptable, false otherwise. 3611bool ASTReader::ParseLanguageOptions(const RecordData &Record, 3612 bool Complain, 3613 ASTReaderListener &Listener) { 3614 LangOptions LangOpts; 3615 unsigned Idx = 0; 3616#define LANGOPT(Name, Bits, Default, Description) \ 3617 LangOpts.Name = Record[Idx++]; 3618#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 3619 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++])); 3620#include "clang/Basic/LangOptions.def" 3621#define SANITIZER(NAME, ID) LangOpts.Sanitize.ID = Record[Idx++]; 3622#include "clang/Basic/Sanitizers.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 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 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 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, 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 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 SmallVector<ObjCMethodDecl *, 4> InstanceMethods; 5814 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 6203Module *ASTReader::getModule(unsigned ID) { 6204 return getSubmodule(ID); 6205} 6206 6207Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) { 6208 return DecodeSelector(getGlobalSelectorID(M, LocalID)); 6209} 6210 6211Selector ASTReader::DecodeSelector(serialization::SelectorID ID) { 6212 if (ID == 0) 6213 return Selector(); 6214 6215 if (ID > SelectorsLoaded.size()) { 6216 Error("selector ID out of range in AST file"); 6217 return Selector(); 6218 } 6219 6220 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) { 6221 // Load this selector from the selector table. 6222 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID); 6223 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map"); 6224 ModuleFile &M = *I->second; 6225 ASTSelectorLookupTrait Trait(*this, M); 6226 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS; 6227 SelectorsLoaded[ID - 1] = 6228 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0); 6229 if (DeserializationListener) 6230 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); 6231 } 6232 6233 return SelectorsLoaded[ID - 1]; 6234} 6235 6236Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) { 6237 return DecodeSelector(ID); 6238} 6239 6240uint32_t ASTReader::GetNumExternalSelectors() { 6241 // ID 0 (the null selector) is considered an external selector. 6242 return getTotalNumSelectors() + 1; 6243} 6244 6245serialization::SelectorID 6246ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const { 6247 if (LocalID < NUM_PREDEF_SELECTOR_IDS) 6248 return LocalID; 6249 6250 ContinuousRangeMap<uint32_t, int, 2>::iterator I 6251 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS); 6252 assert(I != M.SelectorRemap.end() 6253 && "Invalid index into selector index remap"); 6254 6255 return LocalID + I->second; 6256} 6257 6258DeclarationName 6259ASTReader::ReadDeclarationName(ModuleFile &F, 6260 const RecordData &Record, unsigned &Idx) { 6261 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++]; 6262 switch (Kind) { 6263 case DeclarationName::Identifier: 6264 return DeclarationName(GetIdentifierInfo(F, Record, Idx)); 6265 6266 case DeclarationName::ObjCZeroArgSelector: 6267 case DeclarationName::ObjCOneArgSelector: 6268 case DeclarationName::ObjCMultiArgSelector: 6269 return DeclarationName(ReadSelector(F, Record, Idx)); 6270 6271 case DeclarationName::CXXConstructorName: 6272 return Context.DeclarationNames.getCXXConstructorName( 6273 Context.getCanonicalType(readType(F, Record, Idx))); 6274 6275 case DeclarationName::CXXDestructorName: 6276 return Context.DeclarationNames.getCXXDestructorName( 6277 Context.getCanonicalType(readType(F, Record, Idx))); 6278 6279 case DeclarationName::CXXConversionFunctionName: 6280 return Context.DeclarationNames.getCXXConversionFunctionName( 6281 Context.getCanonicalType(readType(F, Record, Idx))); 6282 6283 case DeclarationName::CXXOperatorName: 6284 return Context.DeclarationNames.getCXXOperatorName( 6285 (OverloadedOperatorKind)Record[Idx++]); 6286 6287 case DeclarationName::CXXLiteralOperatorName: 6288 return Context.DeclarationNames.getCXXLiteralOperatorName( 6289 GetIdentifierInfo(F, Record, Idx)); 6290 6291 case DeclarationName::CXXUsingDirective: 6292 return DeclarationName::getUsingDirectiveName(); 6293 } 6294 6295 llvm_unreachable("Invalid NameKind!"); 6296} 6297 6298void ASTReader::ReadDeclarationNameLoc(ModuleFile &F, 6299 DeclarationNameLoc &DNLoc, 6300 DeclarationName Name, 6301 const RecordData &Record, unsigned &Idx) { 6302 switch (Name.getNameKind()) { 6303 case DeclarationName::CXXConstructorName: 6304 case DeclarationName::CXXDestructorName: 6305 case DeclarationName::CXXConversionFunctionName: 6306 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx); 6307 break; 6308 6309 case DeclarationName::CXXOperatorName: 6310 DNLoc.CXXOperatorName.BeginOpNameLoc 6311 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 6312 DNLoc.CXXOperatorName.EndOpNameLoc 6313 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 6314 break; 6315 6316 case DeclarationName::CXXLiteralOperatorName: 6317 DNLoc.CXXLiteralOperatorName.OpNameLoc 6318 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 6319 break; 6320 6321 case DeclarationName::Identifier: 6322 case DeclarationName::ObjCZeroArgSelector: 6323 case DeclarationName::ObjCOneArgSelector: 6324 case DeclarationName::ObjCMultiArgSelector: 6325 case DeclarationName::CXXUsingDirective: 6326 break; 6327 } 6328} 6329 6330void ASTReader::ReadDeclarationNameInfo(ModuleFile &F, 6331 DeclarationNameInfo &NameInfo, 6332 const RecordData &Record, unsigned &Idx) { 6333 NameInfo.setName(ReadDeclarationName(F, Record, Idx)); 6334 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx)); 6335 DeclarationNameLoc DNLoc; 6336 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx); 6337 NameInfo.setInfo(DNLoc); 6338} 6339 6340void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info, 6341 const RecordData &Record, unsigned &Idx) { 6342 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx); 6343 unsigned NumTPLists = Record[Idx++]; 6344 Info.NumTemplParamLists = NumTPLists; 6345 if (NumTPLists) { 6346 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists]; 6347 for (unsigned i=0; i != NumTPLists; ++i) 6348 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx); 6349 } 6350} 6351 6352TemplateName 6353ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record, 6354 unsigned &Idx) { 6355 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++]; 6356 switch (Kind) { 6357 case TemplateName::Template: 6358 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx)); 6359 6360 case TemplateName::OverloadedTemplate: { 6361 unsigned size = Record[Idx++]; 6362 UnresolvedSet<8> Decls; 6363 while (size--) 6364 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx)); 6365 6366 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end()); 6367 } 6368 6369 case TemplateName::QualifiedTemplate: { 6370 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx); 6371 bool hasTemplKeyword = Record[Idx++]; 6372 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx); 6373 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template); 6374 } 6375 6376 case TemplateName::DependentTemplate: { 6377 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx); 6378 if (Record[Idx++]) // isIdentifier 6379 return Context.getDependentTemplateName(NNS, 6380 GetIdentifierInfo(F, Record, 6381 Idx)); 6382 return Context.getDependentTemplateName(NNS, 6383 (OverloadedOperatorKind)Record[Idx++]); 6384 } 6385 6386 case TemplateName::SubstTemplateTemplateParm: { 6387 TemplateTemplateParmDecl *param 6388 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx); 6389 if (!param) return TemplateName(); 6390 TemplateName replacement = ReadTemplateName(F, Record, Idx); 6391 return Context.getSubstTemplateTemplateParm(param, replacement); 6392 } 6393 6394 case TemplateName::SubstTemplateTemplateParmPack: { 6395 TemplateTemplateParmDecl *Param 6396 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx); 6397 if (!Param) 6398 return TemplateName(); 6399 6400 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx); 6401 if (ArgPack.getKind() != TemplateArgument::Pack) 6402 return TemplateName(); 6403 6404 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack); 6405 } 6406 } 6407 6408 llvm_unreachable("Unhandled template name kind!"); 6409} 6410 6411TemplateArgument 6412ASTReader::ReadTemplateArgument(ModuleFile &F, 6413 const RecordData &Record, unsigned &Idx) { 6414 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++]; 6415 switch (Kind) { 6416 case TemplateArgument::Null: 6417 return TemplateArgument(); 6418 case TemplateArgument::Type: 6419 return TemplateArgument(readType(F, Record, Idx)); 6420 case TemplateArgument::Declaration: { 6421 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx); 6422 bool ForReferenceParam = Record[Idx++]; 6423 return TemplateArgument(D, ForReferenceParam); 6424 } 6425 case TemplateArgument::NullPtr: 6426 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true); 6427 case TemplateArgument::Integral: { 6428 llvm::APSInt Value = ReadAPSInt(Record, Idx); 6429 QualType T = readType(F, Record, Idx); 6430 return TemplateArgument(Context, Value, T); 6431 } 6432 case TemplateArgument::Template: 6433 return TemplateArgument(ReadTemplateName(F, Record, Idx)); 6434 case TemplateArgument::TemplateExpansion: { 6435 TemplateName Name = ReadTemplateName(F, Record, Idx); 6436 llvm::Optional<unsigned> NumTemplateExpansions; 6437 if (unsigned NumExpansions = Record[Idx++]) 6438 NumTemplateExpansions = NumExpansions - 1; 6439 return TemplateArgument(Name, NumTemplateExpansions); 6440 } 6441 case TemplateArgument::Expression: 6442 return TemplateArgument(ReadExpr(F)); 6443 case TemplateArgument::Pack: { 6444 unsigned NumArgs = Record[Idx++]; 6445 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs]; 6446 for (unsigned I = 0; I != NumArgs; ++I) 6447 Args[I] = ReadTemplateArgument(F, Record, Idx); 6448 return TemplateArgument(Args, NumArgs); 6449 } 6450 } 6451 6452 llvm_unreachable("Unhandled template argument kind!"); 6453} 6454 6455TemplateParameterList * 6456ASTReader::ReadTemplateParameterList(ModuleFile &F, 6457 const RecordData &Record, unsigned &Idx) { 6458 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx); 6459 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx); 6460 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx); 6461 6462 unsigned NumParams = Record[Idx++]; 6463 SmallVector<NamedDecl *, 16> Params; 6464 Params.reserve(NumParams); 6465 while (NumParams--) 6466 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx)); 6467 6468 TemplateParameterList* TemplateParams = 6469 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, 6470 Params.data(), Params.size(), RAngleLoc); 6471 return TemplateParams; 6472} 6473 6474void 6475ASTReader:: 6476ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs, 6477 ModuleFile &F, const RecordData &Record, 6478 unsigned &Idx) { 6479 unsigned NumTemplateArgs = Record[Idx++]; 6480 TemplArgs.reserve(NumTemplateArgs); 6481 while (NumTemplateArgs--) 6482 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx)); 6483} 6484 6485/// \brief Read a UnresolvedSet structure. 6486void ASTReader::ReadUnresolvedSet(ModuleFile &F, ASTUnresolvedSet &Set, 6487 const RecordData &Record, unsigned &Idx) { 6488 unsigned NumDecls = Record[Idx++]; 6489 Set.reserve(Context, NumDecls); 6490 while (NumDecls--) { 6491 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx); 6492 AccessSpecifier AS = (AccessSpecifier)Record[Idx++]; 6493 Set.addDecl(Context, D, AS); 6494 } 6495} 6496 6497CXXBaseSpecifier 6498ASTReader::ReadCXXBaseSpecifier(ModuleFile &F, 6499 const RecordData &Record, unsigned &Idx) { 6500 bool isVirtual = static_cast<bool>(Record[Idx++]); 6501 bool isBaseOfClass = static_cast<bool>(Record[Idx++]); 6502 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]); 6503 bool inheritConstructors = static_cast<bool>(Record[Idx++]); 6504 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx); 6505 SourceRange Range = ReadSourceRange(F, Record, Idx); 6506 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx); 6507 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo, 6508 EllipsisLoc); 6509 Result.setInheritConstructors(inheritConstructors); 6510 return Result; 6511} 6512 6513std::pair<CXXCtorInitializer **, unsigned> 6514ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record, 6515 unsigned &Idx) { 6516 CXXCtorInitializer **CtorInitializers = 0; 6517 unsigned NumInitializers = Record[Idx++]; 6518 if (NumInitializers) { 6519 CtorInitializers 6520 = new (Context) CXXCtorInitializer*[NumInitializers]; 6521 for (unsigned i=0; i != NumInitializers; ++i) { 6522 TypeSourceInfo *TInfo = 0; 6523 bool IsBaseVirtual = false; 6524 FieldDecl *Member = 0; 6525 IndirectFieldDecl *IndirectMember = 0; 6526 6527 CtorInitializerType Type = (CtorInitializerType)Record[Idx++]; 6528 switch (Type) { 6529 case CTOR_INITIALIZER_BASE: 6530 TInfo = GetTypeSourceInfo(F, Record, Idx); 6531 IsBaseVirtual = Record[Idx++]; 6532 break; 6533 6534 case CTOR_INITIALIZER_DELEGATING: 6535 TInfo = GetTypeSourceInfo(F, Record, Idx); 6536 break; 6537 6538 case CTOR_INITIALIZER_MEMBER: 6539 Member = ReadDeclAs<FieldDecl>(F, Record, Idx); 6540 break; 6541 6542 case CTOR_INITIALIZER_INDIRECT_MEMBER: 6543 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx); 6544 break; 6545 } 6546 6547 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx); 6548 Expr *Init = ReadExpr(F); 6549 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx); 6550 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx); 6551 bool IsWritten = Record[Idx++]; 6552 unsigned SourceOrderOrNumArrayIndices; 6553 SmallVector<VarDecl *, 8> Indices; 6554 if (IsWritten) { 6555 SourceOrderOrNumArrayIndices = Record[Idx++]; 6556 } else { 6557 SourceOrderOrNumArrayIndices = Record[Idx++]; 6558 Indices.reserve(SourceOrderOrNumArrayIndices); 6559 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i) 6560 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx)); 6561 } 6562 6563 CXXCtorInitializer *BOMInit; 6564 if (Type == CTOR_INITIALIZER_BASE) { 6565 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual, 6566 LParenLoc, Init, RParenLoc, 6567 MemberOrEllipsisLoc); 6568 } else if (Type == CTOR_INITIALIZER_DELEGATING) { 6569 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc, 6570 Init, RParenLoc); 6571 } else if (IsWritten) { 6572 if (Member) 6573 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, 6574 LParenLoc, Init, RParenLoc); 6575 else 6576 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember, 6577 MemberOrEllipsisLoc, LParenLoc, 6578 Init, RParenLoc); 6579 } else { 6580 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc, 6581 LParenLoc, Init, RParenLoc, 6582 Indices.data(), Indices.size()); 6583 } 6584 6585 if (IsWritten) 6586 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices); 6587 CtorInitializers[i] = BOMInit; 6588 } 6589 } 6590 6591 return std::make_pair(CtorInitializers, NumInitializers); 6592} 6593 6594NestedNameSpecifier * 6595ASTReader::ReadNestedNameSpecifier(ModuleFile &F, 6596 const RecordData &Record, unsigned &Idx) { 6597 unsigned N = Record[Idx++]; 6598 NestedNameSpecifier *NNS = 0, *Prev = 0; 6599 for (unsigned I = 0; I != N; ++I) { 6600 NestedNameSpecifier::SpecifierKind Kind 6601 = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; 6602 switch (Kind) { 6603 case NestedNameSpecifier::Identifier: { 6604 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx); 6605 NNS = NestedNameSpecifier::Create(Context, Prev, II); 6606 break; 6607 } 6608 6609 case NestedNameSpecifier::Namespace: { 6610 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx); 6611 NNS = NestedNameSpecifier::Create(Context, Prev, NS); 6612 break; 6613 } 6614 6615 case NestedNameSpecifier::NamespaceAlias: { 6616 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx); 6617 NNS = NestedNameSpecifier::Create(Context, Prev, Alias); 6618 break; 6619 } 6620 6621 case NestedNameSpecifier::TypeSpec: 6622 case NestedNameSpecifier::TypeSpecWithTemplate: { 6623 const Type *T = readType(F, Record, Idx).getTypePtrOrNull(); 6624 if (!T) 6625 return 0; 6626 6627 bool Template = Record[Idx++]; 6628 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T); 6629 break; 6630 } 6631 6632 case NestedNameSpecifier::Global: { 6633 NNS = NestedNameSpecifier::GlobalSpecifier(Context); 6634 // No associated value, and there can't be a prefix. 6635 break; 6636 } 6637 } 6638 Prev = NNS; 6639 } 6640 return NNS; 6641} 6642 6643NestedNameSpecifierLoc 6644ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record, 6645 unsigned &Idx) { 6646 unsigned N = Record[Idx++]; 6647 NestedNameSpecifierLocBuilder Builder; 6648 for (unsigned I = 0; I != N; ++I) { 6649 NestedNameSpecifier::SpecifierKind Kind 6650 = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; 6651 switch (Kind) { 6652 case NestedNameSpecifier::Identifier: { 6653 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx); 6654 SourceRange Range = ReadSourceRange(F, Record, Idx); 6655 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd()); 6656 break; 6657 } 6658 6659 case NestedNameSpecifier::Namespace: { 6660 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx); 6661 SourceRange Range = ReadSourceRange(F, Record, Idx); 6662 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd()); 6663 break; 6664 } 6665 6666 case NestedNameSpecifier::NamespaceAlias: { 6667 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx); 6668 SourceRange Range = ReadSourceRange(F, Record, Idx); 6669 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd()); 6670 break; 6671 } 6672 6673 case NestedNameSpecifier::TypeSpec: 6674 case NestedNameSpecifier::TypeSpecWithTemplate: { 6675 bool Template = Record[Idx++]; 6676 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx); 6677 if (!T) 6678 return NestedNameSpecifierLoc(); 6679 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx); 6680 6681 // FIXME: 'template' keyword location not saved anywhere, so we fake it. 6682 Builder.Extend(Context, 6683 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(), 6684 T->getTypeLoc(), ColonColonLoc); 6685 break; 6686 } 6687 6688 case NestedNameSpecifier::Global: { 6689 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx); 6690 Builder.MakeGlobal(Context, ColonColonLoc); 6691 break; 6692 } 6693 } 6694 } 6695 6696 return Builder.getWithLocInContext(Context); 6697} 6698 6699SourceRange 6700ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record, 6701 unsigned &Idx) { 6702 SourceLocation beg = ReadSourceLocation(F, Record, Idx); 6703 SourceLocation end = ReadSourceLocation(F, Record, Idx); 6704 return SourceRange(beg, end); 6705} 6706 6707/// \brief Read an integral value 6708llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) { 6709 unsigned BitWidth = Record[Idx++]; 6710 unsigned NumWords = llvm::APInt::getNumWords(BitWidth); 6711 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]); 6712 Idx += NumWords; 6713 return Result; 6714} 6715 6716/// \brief Read a signed integral value 6717llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) { 6718 bool isUnsigned = Record[Idx++]; 6719 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned); 6720} 6721 6722/// \brief Read a floating-point value 6723llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) { 6724 return llvm::APFloat(ReadAPInt(Record, Idx)); 6725} 6726 6727// \brief Read a string 6728std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) { 6729 unsigned Len = Record[Idx++]; 6730 std::string Result(Record.data() + Idx, Record.data() + Idx + Len); 6731 Idx += Len; 6732 return Result; 6733} 6734 6735VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record, 6736 unsigned &Idx) { 6737 unsigned Major = Record[Idx++]; 6738 unsigned Minor = Record[Idx++]; 6739 unsigned Subminor = Record[Idx++]; 6740 if (Minor == 0) 6741 return VersionTuple(Major); 6742 if (Subminor == 0) 6743 return VersionTuple(Major, Minor - 1); 6744 return VersionTuple(Major, Minor - 1, Subminor - 1); 6745} 6746 6747CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F, 6748 const RecordData &Record, 6749 unsigned &Idx) { 6750 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx); 6751 return CXXTemporary::Create(Context, Decl); 6752} 6753 6754DiagnosticBuilder ASTReader::Diag(unsigned DiagID) { 6755 return Diag(SourceLocation(), DiagID); 6756} 6757 6758DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) { 6759 return Diags.Report(Loc, DiagID); 6760} 6761 6762/// \brief Retrieve the identifier table associated with the 6763/// preprocessor. 6764IdentifierTable &ASTReader::getIdentifierTable() { 6765 return PP.getIdentifierTable(); 6766} 6767 6768/// \brief Record that the given ID maps to the given switch-case 6769/// statement. 6770void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { 6771 assert((*CurrSwitchCaseStmts)[ID] == 0 && 6772 "Already have a SwitchCase with this ID"); 6773 (*CurrSwitchCaseStmts)[ID] = SC; 6774} 6775 6776/// \brief Retrieve the switch-case statement with the given ID. 6777SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) { 6778 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID"); 6779 return (*CurrSwitchCaseStmts)[ID]; 6780} 6781 6782void ASTReader::ClearSwitchCaseIDs() { 6783 CurrSwitchCaseStmts->clear(); 6784} 6785 6786void ASTReader::ReadComments() { 6787 std::vector<RawComment *> Comments; 6788 for (SmallVectorImpl<std::pair<llvm::BitstreamCursor, 6789 serialization::ModuleFile *> >::iterator 6790 I = CommentsCursors.begin(), 6791 E = CommentsCursors.end(); 6792 I != E; ++I) { 6793 llvm::BitstreamCursor &Cursor = I->first; 6794 serialization::ModuleFile &F = *I->second; 6795 SavedStreamPosition SavedPosition(Cursor); 6796 6797 RecordData Record; 6798 while (true) { 6799 unsigned Code = Cursor.ReadCode(); 6800 if (Code == llvm::bitc::END_BLOCK) 6801 break; 6802 6803 if (Code == llvm::bitc::ENTER_SUBBLOCK) { 6804 // No known subblocks, always skip them. 6805 Cursor.ReadSubBlockID(); 6806 if (Cursor.SkipBlock()) { 6807 Error("malformed block record in AST file"); 6808 return; 6809 } 6810 continue; 6811 } 6812 6813 if (Code == llvm::bitc::DEFINE_ABBREV) { 6814 Cursor.ReadAbbrevRecord(); 6815 continue; 6816 } 6817 6818 // Read a record. 6819 Record.clear(); 6820 switch ((CommentRecordTypes) Cursor.ReadRecord(Code, Record)) { 6821 case COMMENTS_RAW_COMMENT: { 6822 unsigned Idx = 0; 6823 SourceRange SR = ReadSourceRange(F, Record, Idx); 6824 RawComment::CommentKind Kind = 6825 (RawComment::CommentKind) Record[Idx++]; 6826 bool IsTrailingComment = Record[Idx++]; 6827 bool IsAlmostTrailingComment = Record[Idx++]; 6828 Comments.push_back(new (Context) RawComment(SR, Kind, 6829 IsTrailingComment, 6830 IsAlmostTrailingComment)); 6831 break; 6832 } 6833 } 6834 } 6835 } 6836 Context.Comments.addCommentsToFront(Comments); 6837} 6838 6839void ASTReader::finishPendingActions() { 6840 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() || 6841 !PendingMacroIDs.empty()) { 6842 // If any identifiers with corresponding top-level declarations have 6843 // been loaded, load those declarations now. 6844 while (!PendingIdentifierInfos.empty()) { 6845 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II, 6846 PendingIdentifierInfos.front().DeclIDs, true); 6847 PendingIdentifierInfos.pop_front(); 6848 } 6849 6850 // Load pending declaration chains. 6851 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) { 6852 loadPendingDeclChain(PendingDeclChains[I]); 6853 PendingDeclChainsKnown.erase(PendingDeclChains[I]); 6854 } 6855 PendingDeclChains.clear(); 6856 6857 // Load any pending macro definitions. 6858 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) { 6859 // FIXME: std::move here 6860 SmallVector<MacroID, 2> GlobalIDs = PendingMacroIDs.begin()[I].second; 6861 MacroInfo *Hint = 0; 6862 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 6863 ++IDIdx) { 6864 Hint = getMacro(GlobalIDs[IDIdx], Hint); 6865 } 6866 } 6867 PendingMacroIDs.clear(); 6868 } 6869 6870 // If we deserialized any C++ or Objective-C class definitions, any 6871 // Objective-C protocol definitions, or any redeclarable templates, make sure 6872 // that all redeclarations point to the definitions. Note that this can only 6873 // happen now, after the redeclaration chains have been fully wired. 6874 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(), 6875 DEnd = PendingDefinitions.end(); 6876 D != DEnd; ++D) { 6877 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) { 6878 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) { 6879 // Make sure that the TagType points at the definition. 6880 const_cast<TagType*>(TagT)->decl = TD; 6881 } 6882 6883 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) { 6884 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(), 6885 REnd = RD->redecls_end(); 6886 R != REnd; ++R) 6887 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData; 6888 6889 } 6890 6891 continue; 6892 } 6893 6894 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) { 6895 // Make sure that the ObjCInterfaceType points at the definition. 6896 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl)) 6897 ->Decl = ID; 6898 6899 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(), 6900 REnd = ID->redecls_end(); 6901 R != REnd; ++R) 6902 R->Data = ID->Data; 6903 6904 continue; 6905 } 6906 6907 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) { 6908 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(), 6909 REnd = PD->redecls_end(); 6910 R != REnd; ++R) 6911 R->Data = PD->Data; 6912 6913 continue; 6914 } 6915 6916 RedeclarableTemplateDecl *RTD 6917 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl(); 6918 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(), 6919 REnd = RTD->redecls_end(); 6920 R != REnd; ++R) 6921 R->Common = RTD->Common; 6922 } 6923 PendingDefinitions.clear(); 6924 6925 // Load the bodies of any functions or methods we've encountered. We do 6926 // this now (delayed) so that we can be sure that the declaration chains 6927 // have been fully wired up. 6928 for (PendingBodiesMap::iterator PB = PendingBodies.begin(), 6929 PBEnd = PendingBodies.end(); 6930 PB != PBEnd; ++PB) { 6931 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) { 6932 // FIXME: Check for =delete/=default? 6933 // FIXME: Complain about ODR violations here? 6934 if (!getContext().getLangOpts().Modules || !FD->hasBody()) 6935 FD->setLazyBody(PB->second); 6936 continue; 6937 } 6938 6939 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first); 6940 if (!getContext().getLangOpts().Modules || !MD->hasBody()) 6941 MD->setLazyBody(PB->second); 6942 } 6943 PendingBodies.clear(); 6944} 6945 6946void ASTReader::FinishedDeserializing() { 6947 assert(NumCurrentElementsDeserializing && 6948 "FinishedDeserializing not paired with StartedDeserializing"); 6949 if (NumCurrentElementsDeserializing == 1) { 6950 // We decrease NumCurrentElementsDeserializing only after pending actions 6951 // are finished, to avoid recursively re-calling finishPendingActions(). 6952 finishPendingActions(); 6953 } 6954 --NumCurrentElementsDeserializing; 6955 6956 if (NumCurrentElementsDeserializing == 0 && 6957 Consumer && !PassingDeclsToConsumer) { 6958 // Guard variable to avoid recursively redoing the process of passing 6959 // decls to consumer. 6960 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer, 6961 true); 6962 6963 while (!InterestingDecls.empty()) { 6964 // We are not in recursive loading, so it's safe to pass the "interesting" 6965 // decls to the consumer. 6966 Decl *D = InterestingDecls.front(); 6967 InterestingDecls.pop_front(); 6968 PassInterestingDeclToConsumer(D); 6969 } 6970 } 6971} 6972 6973ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context, 6974 StringRef isysroot, bool DisableValidation, 6975 bool AllowASTWithCompilerErrors) 6976 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0), 6977 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), 6978 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context), 6979 Consumer(0), ModuleMgr(PP.getFileManager()), 6980 isysroot(isysroot), DisableValidation(DisableValidation), 6981 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors), 6982 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts), 6983 NumSLocEntriesRead(0), TotalNumSLocEntries(0), 6984 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0), 6985 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0), 6986 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0), 6987 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0), 6988 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0), 6989 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0), 6990 PassingDeclsToConsumer(false), 6991 NumCXXBaseSpecifiersLoaded(0) 6992{ 6993 SourceMgr.setExternalSLocEntrySource(this); 6994} 6995 6996ASTReader::~ASTReader() { 6997 for (DeclContextVisibleUpdatesPending::iterator 6998 I = PendingVisibleUpdates.begin(), 6999 E = PendingVisibleUpdates.end(); 7000 I != E; ++I) { 7001 for (DeclContextVisibleUpdates::iterator J = I->second.begin(), 7002 F = I->second.end(); 7003 J != F; ++J) 7004 delete J->first; 7005 } 7006} 7007