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