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