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