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