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