ASTReader.cpp revision c7782d96c657eeb767bfea5117db49dc40e6356c
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 F.NumFileSortedDecls = Record[0]; 2067 break; 2068 2069 case SOURCE_LOCATION_OFFSETS: { 2070 F.SLocEntryOffsets = (const uint32_t *)BlobStart; 2071 F.LocalNumSLocEntries = Record[0]; 2072 unsigned SLocSpaceSize = Record[1]; 2073 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) = 2074 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, 2075 SLocSpaceSize); 2076 // Make our entry in the range map. BaseID is negative and growing, so 2077 // we invert it. Because we invert it, though, we need the other end of 2078 // the range. 2079 unsigned RangeStart = 2080 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1; 2081 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F)); 2082 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset); 2083 2084 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing. 2085 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0); 2086 GlobalSLocOffsetMap.insert( 2087 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset 2088 - SLocSpaceSize,&F)); 2089 2090 // Initialize the remapping table. 2091 // Invalid stays invalid. 2092 F.SLocRemap.insert(std::make_pair(0U, 0)); 2093 // This module. Base was 2 when being compiled. 2094 F.SLocRemap.insert(std::make_pair(2U, 2095 static_cast<int>(F.SLocEntryBaseOffset - 2))); 2096 2097 TotalNumSLocEntries += F.LocalNumSLocEntries; 2098 break; 2099 } 2100 2101 case MODULE_OFFSET_MAP: { 2102 // Additional remapping information. 2103 const unsigned char *Data = (const unsigned char*)BlobStart; 2104 const unsigned char *DataEnd = Data + BlobLen; 2105 2106 // Continuous range maps we may be updating in our module. 2107 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap); 2108 ContinuousRangeMap<uint32_t, int, 2>::Builder 2109 IdentifierRemap(F.IdentifierRemap); 2110 ContinuousRangeMap<uint32_t, int, 2>::Builder 2111 PreprocessedEntityRemap(F.PreprocessedEntityRemap); 2112 ContinuousRangeMap<uint32_t, int, 2>::Builder 2113 SubmoduleRemap(F.SubmoduleRemap); 2114 ContinuousRangeMap<uint32_t, int, 2>::Builder 2115 SelectorRemap(F.SelectorRemap); 2116 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap); 2117 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap); 2118 2119 while(Data < DataEnd) { 2120 uint16_t Len = io::ReadUnalignedLE16(Data); 2121 StringRef Name = StringRef((const char*)Data, Len); 2122 Data += Len; 2123 ModuleFile *OM = ModuleMgr.lookup(Name); 2124 if (!OM) { 2125 Error("SourceLocation remap refers to unknown module"); 2126 return Failure; 2127 } 2128 2129 uint32_t SLocOffset = io::ReadUnalignedLE32(Data); 2130 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data); 2131 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data); 2132 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data); 2133 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data); 2134 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data); 2135 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data); 2136 2137 // Source location offset is mapped to OM->SLocEntryBaseOffset. 2138 SLocRemap.insert(std::make_pair(SLocOffset, 2139 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset))); 2140 IdentifierRemap.insert( 2141 std::make_pair(IdentifierIDOffset, 2142 OM->BaseIdentifierID - IdentifierIDOffset)); 2143 PreprocessedEntityRemap.insert( 2144 std::make_pair(PreprocessedEntityIDOffset, 2145 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset)); 2146 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset, 2147 OM->BaseSubmoduleID - SubmoduleIDOffset)); 2148 SelectorRemap.insert(std::make_pair(SelectorIDOffset, 2149 OM->BaseSelectorID - SelectorIDOffset)); 2150 DeclRemap.insert(std::make_pair(DeclIDOffset, 2151 OM->BaseDeclID - DeclIDOffset)); 2152 2153 TypeRemap.insert(std::make_pair(TypeIndexOffset, 2154 OM->BaseTypeIndex - TypeIndexOffset)); 2155 2156 // Global -> local mappings. 2157 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset; 2158 } 2159 break; 2160 } 2161 2162 case SOURCE_MANAGER_LINE_TABLE: 2163 if (ParseLineTable(F, Record)) 2164 return Failure; 2165 break; 2166 2167 case FILE_SOURCE_LOCATION_OFFSETS: 2168 F.SLocFileOffsets = (const uint32_t *)BlobStart; 2169 F.LocalNumSLocFileEntries = Record[0]; 2170 break; 2171 2172 case SOURCE_LOCATION_PRELOADS: { 2173 // Need to transform from the local view (1-based IDs) to the global view, 2174 // which is based off F.SLocEntryBaseID. 2175 if (!F.PreloadSLocEntries.empty()) { 2176 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file"); 2177 return Failure; 2178 } 2179 2180 F.PreloadSLocEntries.swap(Record); 2181 break; 2182 } 2183 2184 case STAT_CACHE: { 2185 if (!DisableStatCache) { 2186 ASTStatCache *MyStatCache = 2187 new ASTStatCache((const unsigned char *)BlobStart + Record[0], 2188 (const unsigned char *)BlobStart, 2189 NumStatHits, NumStatMisses); 2190 FileMgr.addStatCache(MyStatCache); 2191 F.StatCache = MyStatCache; 2192 } 2193 break; 2194 } 2195 2196 case EXT_VECTOR_DECLS: 2197 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2198 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I])); 2199 break; 2200 2201 case VTABLE_USES: 2202 if (Record.size() % 3 != 0) { 2203 Error("Invalid VTABLE_USES record"); 2204 return Failure; 2205 } 2206 2207 // Later tables overwrite earlier ones. 2208 // FIXME: Modules will have some trouble with this. This is clearly not 2209 // the right way to do this. 2210 VTableUses.clear(); 2211 2212 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) { 2213 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++])); 2214 VTableUses.push_back( 2215 ReadSourceLocation(F, Record, Idx).getRawEncoding()); 2216 VTableUses.push_back(Record[Idx++]); 2217 } 2218 break; 2219 2220 case DYNAMIC_CLASSES: 2221 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2222 DynamicClasses.push_back(getGlobalDeclID(F, Record[I])); 2223 break; 2224 2225 case PENDING_IMPLICIT_INSTANTIATIONS: 2226 if (PendingInstantiations.size() % 2 != 0) { 2227 Error("Invalid existing PendingInstantiations"); 2228 return Failure; 2229 } 2230 2231 if (Record.size() % 2 != 0) { 2232 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block"); 2233 return Failure; 2234 } 2235 2236 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 2237 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++])); 2238 PendingInstantiations.push_back( 2239 ReadSourceLocation(F, Record, I).getRawEncoding()); 2240 } 2241 break; 2242 2243 case SEMA_DECL_REFS: 2244 // Later tables overwrite earlier ones. 2245 // FIXME: Modules will have some trouble with this. 2246 SemaDeclRefs.clear(); 2247 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2248 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 2249 break; 2250 2251 case ORIGINAL_FILE_NAME: 2252 // The primary AST will be the last to get here, so it will be the one 2253 // that's used. 2254 ActualOriginalFileName.assign(BlobStart, BlobLen); 2255 OriginalFileName = ActualOriginalFileName; 2256 MaybeAddSystemRootToFilename(OriginalFileName); 2257 break; 2258 2259 case ORIGINAL_FILE_ID: 2260 OriginalFileID = FileID::get(Record[0]); 2261 break; 2262 2263 case ORIGINAL_PCH_DIR: 2264 // The primary AST will be the last to get here, so it will be the one 2265 // that's used. 2266 OriginalDir.assign(BlobStart, BlobLen); 2267 break; 2268 2269 case VERSION_CONTROL_BRANCH_REVISION: { 2270 const std::string &CurBranch = getClangFullRepositoryVersion(); 2271 StringRef ASTBranch(BlobStart, BlobLen); 2272 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) { 2273 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch; 2274 return IgnorePCH; 2275 } 2276 break; 2277 } 2278 2279 case PPD_ENTITIES_OFFSETS: { 2280 F.PreprocessedEntityOffsets = (const PPEntityOffset *)BlobStart; 2281 assert(BlobLen % sizeof(PPEntityOffset) == 0); 2282 F.NumPreprocessedEntities = BlobLen / sizeof(PPEntityOffset); 2283 2284 unsigned LocalBasePreprocessedEntityID = Record[0]; 2285 2286 unsigned StartingID; 2287 if (!PP.getPreprocessingRecord()) 2288 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false); 2289 if (!PP.getPreprocessingRecord()->getExternalSource()) 2290 PP.getPreprocessingRecord()->SetExternalSource(*this); 2291 StartingID 2292 = PP.getPreprocessingRecord() 2293 ->allocateLoadedEntities(F.NumPreprocessedEntities); 2294 F.BasePreprocessedEntityID = StartingID; 2295 2296 if (F.NumPreprocessedEntities > 0) { 2297 // Introduce the global -> local mapping for preprocessed entities in 2298 // this module. 2299 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F)); 2300 2301 // Introduce the local -> global mapping for preprocessed entities in 2302 // this module. 2303 F.PreprocessedEntityRemap.insertOrReplace( 2304 std::make_pair(LocalBasePreprocessedEntityID, 2305 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID)); 2306 } 2307 2308 break; 2309 } 2310 2311 case DECL_UPDATE_OFFSETS: { 2312 if (Record.size() % 2 != 0) { 2313 Error("invalid DECL_UPDATE_OFFSETS block in AST file"); 2314 return Failure; 2315 } 2316 for (unsigned I = 0, N = Record.size(); I != N; I += 2) 2317 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])] 2318 .push_back(std::make_pair(&F, Record[I+1])); 2319 break; 2320 } 2321 2322 case DECL_REPLACEMENTS: { 2323 if (Record.size() % 3 != 0) { 2324 Error("invalid DECL_REPLACEMENTS block in AST file"); 2325 return Failure; 2326 } 2327 for (unsigned I = 0, N = Record.size(); I != N; I += 3) 2328 ReplacedDecls[getGlobalDeclID(F, Record[I])] 2329 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]); 2330 break; 2331 } 2332 2333 case OBJC_CATEGORIES_MAP: { 2334 if (F.LocalNumObjCCategoriesInMap != 0) { 2335 Error("duplicate OBJC_CATEGORIES_MAP record in AST file"); 2336 return Failure; 2337 } 2338 2339 F.LocalNumObjCCategoriesInMap = Record[0]; 2340 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)BlobStart; 2341 break; 2342 } 2343 2344 case OBJC_CATEGORIES: 2345 F.ObjCCategories.swap(Record); 2346 break; 2347 2348 case CXX_BASE_SPECIFIER_OFFSETS: { 2349 if (F.LocalNumCXXBaseSpecifiers != 0) { 2350 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file"); 2351 return Failure; 2352 } 2353 2354 F.LocalNumCXXBaseSpecifiers = Record[0]; 2355 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart; 2356 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers; 2357 break; 2358 } 2359 2360 case DIAG_PRAGMA_MAPPINGS: 2361 if (Record.size() % 2 != 0) { 2362 Error("invalid DIAG_USER_MAPPINGS block in AST file"); 2363 return Failure; 2364 } 2365 2366 if (F.PragmaDiagMappings.empty()) 2367 F.PragmaDiagMappings.swap(Record); 2368 else 2369 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(), 2370 Record.begin(), Record.end()); 2371 break; 2372 2373 case CUDA_SPECIAL_DECL_REFS: 2374 // Later tables overwrite earlier ones. 2375 // FIXME: Modules will have trouble with this. 2376 CUDASpecialDeclRefs.clear(); 2377 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2378 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 2379 break; 2380 2381 case HEADER_SEARCH_TABLE: { 2382 F.HeaderFileInfoTableData = BlobStart; 2383 F.LocalNumHeaderFileInfos = Record[1]; 2384 F.HeaderFileFrameworkStrings = BlobStart + Record[2]; 2385 if (Record[0]) { 2386 F.HeaderFileInfoTable 2387 = HeaderFileInfoLookupTable::Create( 2388 (const unsigned char *)F.HeaderFileInfoTableData + Record[0], 2389 (const unsigned char *)F.HeaderFileInfoTableData, 2390 HeaderFileInfoTrait(*this, F, 2391 &PP.getHeaderSearchInfo(), 2392 BlobStart + Record[2])); 2393 2394 PP.getHeaderSearchInfo().SetExternalSource(this); 2395 if (!PP.getHeaderSearchInfo().getExternalLookup()) 2396 PP.getHeaderSearchInfo().SetExternalLookup(this); 2397 } 2398 break; 2399 } 2400 2401 case FP_PRAGMA_OPTIONS: 2402 // Later tables overwrite earlier ones. 2403 FPPragmaOptions.swap(Record); 2404 break; 2405 2406 case OPENCL_EXTENSIONS: 2407 // Later tables overwrite earlier ones. 2408 OpenCLExtensions.swap(Record); 2409 break; 2410 2411 case TENTATIVE_DEFINITIONS: 2412 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2413 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I])); 2414 break; 2415 2416 case KNOWN_NAMESPACES: 2417 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2418 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I])); 2419 break; 2420 2421 case IMPORTED_MODULES: { 2422 if (F.Kind != MK_Module) { 2423 // If we aren't loading a module (which has its own exports), make 2424 // all of the imported modules visible. 2425 // FIXME: Deal with macros-only imports. 2426 for (unsigned I = 0, N = Record.size(); I != N; ++I) { 2427 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I])) 2428 ImportedModules.push_back(GlobalID); 2429 } 2430 } 2431 break; 2432 } 2433 2434 case LOCAL_REDECLARATIONS: { 2435 F.RedeclarationChains.swap(Record); 2436 break; 2437 } 2438 2439 case LOCAL_REDECLARATIONS_MAP: { 2440 if (F.LocalNumRedeclarationsInMap != 0) { 2441 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file"); 2442 return Failure; 2443 } 2444 2445 F.LocalNumRedeclarationsInMap = Record[0]; 2446 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)BlobStart; 2447 break; 2448 } 2449 2450 case MERGED_DECLARATIONS: { 2451 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) { 2452 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]); 2453 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID]; 2454 for (unsigned N = Record[Idx++]; N > 0; --N) 2455 Decls.push_back(getGlobalDeclID(F, Record[Idx++])); 2456 } 2457 break; 2458 } 2459 } 2460 } 2461 Error("premature end of bitstream in AST file"); 2462 return Failure; 2463} 2464 2465ASTReader::ASTReadResult ASTReader::validateFileEntries(ModuleFile &M) { 2466 llvm::BitstreamCursor &SLocEntryCursor = M.SLocEntryCursor; 2467 2468 for (unsigned i = 0, e = M.LocalNumSLocFileEntries; i != e; ++i) { 2469 SLocEntryCursor.JumpToBit(M.SLocFileOffsets[i]); 2470 unsigned Code = SLocEntryCursor.ReadCode(); 2471 if (Code == llvm::bitc::END_BLOCK || 2472 Code == llvm::bitc::ENTER_SUBBLOCK || 2473 Code == llvm::bitc::DEFINE_ABBREV) { 2474 Error("incorrectly-formatted source location entry in AST file"); 2475 return Failure; 2476 } 2477 2478 RecordData Record; 2479 const char *BlobStart; 2480 unsigned BlobLen; 2481 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) { 2482 default: 2483 Error("incorrectly-formatted source location entry in AST file"); 2484 return Failure; 2485 2486 case SM_SLOC_FILE_ENTRY: { 2487 // If the buffer was overridden, the file need not exist. 2488 if (Record[6]) 2489 break; 2490 2491 StringRef Filename(BlobStart, BlobLen); 2492 const FileEntry *File = getFileEntry(Filename); 2493 2494 if (File == 0) { 2495 std::string ErrorStr = "could not find file '"; 2496 ErrorStr += Filename; 2497 ErrorStr += "' referenced by AST file"; 2498 Error(ErrorStr.c_str()); 2499 return IgnorePCH; 2500 } 2501 2502 if (Record.size() < 7) { 2503 Error("source location entry is incorrect"); 2504 return Failure; 2505 } 2506 2507 off_t StoredSize = (off_t)Record[4]; 2508 time_t StoredTime = (time_t)Record[5]; 2509 2510 // Check if there was a request to override the contents of the file 2511 // that was part of the precompiled header. Overridding such a file 2512 // can lead to problems when lexing using the source locations from the 2513 // PCH. 2514 SourceManager &SM = getSourceManager(); 2515 if (SM.isFileOverridden(File)) { 2516 Error(diag::err_fe_pch_file_overridden, Filename); 2517 // After emitting the diagnostic, recover by disabling the override so 2518 // that the original file will be used. 2519 SM.disableFileContentsOverride(File); 2520 // The FileEntry is a virtual file entry with the size of the contents 2521 // that would override the original contents. Set it to the original's 2522 // size/time. 2523 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File), 2524 StoredSize, StoredTime); 2525 } 2526 2527 // The stat info from the FileEntry came from the cached stat 2528 // info of the PCH, so we cannot trust it. 2529 struct stat StatBuf; 2530 if (::stat(File->getName(), &StatBuf) != 0) { 2531 StatBuf.st_size = File->getSize(); 2532 StatBuf.st_mtime = File->getModificationTime(); 2533 } 2534 2535 if ((StoredSize != StatBuf.st_size 2536#if !defined(LLVM_ON_WIN32) 2537 // In our regression testing, the Windows file system seems to 2538 // have inconsistent modification times that sometimes 2539 // erroneously trigger this error-handling path. 2540 || StoredTime != StatBuf.st_mtime 2541#endif 2542 )) { 2543 Error(diag::err_fe_pch_file_modified, Filename); 2544 return IgnorePCH; 2545 } 2546 2547 break; 2548 } 2549 } 2550 } 2551 2552 return Success; 2553} 2554 2555void ASTReader::makeNamesVisible(const HiddenNames &Names) { 2556 for (unsigned I = 0, N = Names.size(); I != N; ++I) { 2557 if (Decl *D = Names[I].dyn_cast<Decl *>()) 2558 D->Hidden = false; 2559 else { 2560 IdentifierInfo *II = Names[I].get<IdentifierInfo *>(); 2561 // FIXME: Check if this works correctly with macro history. 2562 if (!II->hasMacroDefinition()) { 2563 // Make sure that this macro hasn't been #undef'd in the mean-time. 2564 llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator Known 2565 = PP.Macros.find(II); 2566 if (Known == PP.Macros.end() || 2567 Known->second->getUndefLoc().isInvalid()) { 2568 II->setHasMacroDefinition(true); 2569 if (DeserializationListener) 2570 DeserializationListener->MacroVisible(II); 2571 } 2572 } 2573 } 2574 } 2575} 2576 2577void ASTReader::makeModuleVisible(Module *Mod, 2578 Module::NameVisibilityKind NameVisibility) { 2579 llvm::SmallPtrSet<Module *, 4> Visited; 2580 llvm::SmallVector<Module *, 4> Stack; 2581 Stack.push_back(Mod); 2582 while (!Stack.empty()) { 2583 Mod = Stack.back(); 2584 Stack.pop_back(); 2585 2586 if (NameVisibility <= Mod->NameVisibility) { 2587 // This module already has this level of visibility (or greater), so 2588 // there is nothing more to do. 2589 continue; 2590 } 2591 2592 if (!Mod->isAvailable()) { 2593 // Modules that aren't available cannot be made visible. 2594 continue; 2595 } 2596 2597 // Update the module's name visibility. 2598 Mod->NameVisibility = NameVisibility; 2599 2600 // If we've already deserialized any names from this module, 2601 // mark them as visible. 2602 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod); 2603 if (Hidden != HiddenNamesMap.end()) { 2604 makeNamesVisible(Hidden->second); 2605 HiddenNamesMap.erase(Hidden); 2606 } 2607 2608 // Push any non-explicit submodules onto the stack to be marked as 2609 // visible. 2610 for (Module::submodule_iterator Sub = Mod->submodule_begin(), 2611 SubEnd = Mod->submodule_end(); 2612 Sub != SubEnd; ++Sub) { 2613 if (!(*Sub)->IsExplicit && Visited.insert(*Sub)) 2614 Stack.push_back(*Sub); 2615 } 2616 2617 // Push any exported modules onto the stack to be marked as visible. 2618 bool AnyWildcard = false; 2619 bool UnrestrictedWildcard = false; 2620 llvm::SmallVector<Module *, 4> WildcardRestrictions; 2621 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) { 2622 Module *Exported = Mod->Exports[I].getPointer(); 2623 if (!Mod->Exports[I].getInt()) { 2624 // Export a named module directly; no wildcards involved. 2625 if (Visited.insert(Exported)) 2626 Stack.push_back(Exported); 2627 2628 continue; 2629 } 2630 2631 // Wildcard export: export all of the imported modules that match 2632 // the given pattern. 2633 AnyWildcard = true; 2634 if (UnrestrictedWildcard) 2635 continue; 2636 2637 if (Module *Restriction = Mod->Exports[I].getPointer()) 2638 WildcardRestrictions.push_back(Restriction); 2639 else { 2640 WildcardRestrictions.clear(); 2641 UnrestrictedWildcard = true; 2642 } 2643 } 2644 2645 // If there were any wildcards, push any imported modules that were 2646 // re-exported by the wildcard restriction. 2647 if (!AnyWildcard) 2648 continue; 2649 2650 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) { 2651 Module *Imported = Mod->Imports[I]; 2652 if (!Visited.insert(Imported)) 2653 continue; 2654 2655 bool Acceptable = UnrestrictedWildcard; 2656 if (!Acceptable) { 2657 // Check whether this module meets one of the restrictions. 2658 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) { 2659 Module *Restriction = WildcardRestrictions[R]; 2660 if (Imported == Restriction || Imported->isSubModuleOf(Restriction)) { 2661 Acceptable = true; 2662 break; 2663 } 2664 } 2665 } 2666 2667 if (!Acceptable) 2668 continue; 2669 2670 Stack.push_back(Imported); 2671 } 2672 } 2673} 2674 2675ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName, 2676 ModuleKind Type) { 2677 // Bump the generation number. 2678 unsigned PreviousGeneration = CurrentGeneration++; 2679 2680 switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0)) { 2681 case Failure: return Failure; 2682 case IgnorePCH: return IgnorePCH; 2683 case Success: break; 2684 } 2685 2686 // Here comes stuff that we only do once the entire chain is loaded. 2687 2688 // Check the predefines buffers. 2689 if (!DisableValidation && Type == MK_PCH && 2690 // FIXME: CheckPredefinesBuffers also sets the SuggestedPredefines; 2691 // if DisableValidation is true, defines that were set on command-line 2692 // but not in the PCH file will not be added to SuggestedPredefines. 2693 CheckPredefinesBuffers()) 2694 return IgnorePCH; 2695 2696 // Mark all of the identifiers in the identifier table as being out of date, 2697 // so that various accessors know to check the loaded modules when the 2698 // identifier is used. 2699 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(), 2700 IdEnd = PP.getIdentifierTable().end(); 2701 Id != IdEnd; ++Id) 2702 Id->second->setOutOfDate(true); 2703 2704 // Resolve any unresolved module exports. 2705 for (unsigned I = 0, N = UnresolvedModuleImportExports.size(); I != N; ++I) { 2706 UnresolvedModuleImportExport &Unresolved = UnresolvedModuleImportExports[I]; 2707 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID); 2708 Module *ResolvedMod = getSubmodule(GlobalID); 2709 2710 if (Unresolved.IsImport) { 2711 if (ResolvedMod) 2712 Unresolved.Mod->Imports.push_back(ResolvedMod); 2713 continue; 2714 } 2715 2716 if (ResolvedMod || Unresolved.IsWildcard) 2717 Unresolved.Mod->Exports.push_back( 2718 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard)); 2719 } 2720 UnresolvedModuleImportExports.clear(); 2721 2722 InitializeContext(); 2723 2724 if (DeserializationListener) 2725 DeserializationListener->ReaderInitialized(this); 2726 2727 if (!OriginalFileID.isInvalid()) { 2728 OriginalFileID = FileID::get(ModuleMgr.getPrimaryModule().SLocEntryBaseID 2729 + OriginalFileID.getOpaqueValue() - 1); 2730 2731 // If this AST file is a precompiled preamble, then set the preamble file ID 2732 // of the source manager to the file source file from which the preamble was 2733 // built. 2734 if (Type == MK_Preamble) { 2735 SourceMgr.setPreambleFileID(OriginalFileID); 2736 } else if (Type == MK_MainFile) { 2737 SourceMgr.setMainFileID(OriginalFileID); 2738 } 2739 } 2740 2741 // For any Objective-C class definitions we have already loaded, make sure 2742 // that we load any additional categories. 2743 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) { 2744 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(), 2745 ObjCClassesLoaded[I], 2746 PreviousGeneration); 2747 } 2748 2749 return Success; 2750} 2751 2752ASTReader::ASTReadResult ASTReader::ReadASTCore(StringRef FileName, 2753 ModuleKind Type, 2754 ModuleFile *ImportedBy) { 2755 ModuleFile *M; 2756 bool NewModule; 2757 std::string ErrorStr; 2758 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy, 2759 CurrentGeneration, ErrorStr); 2760 2761 if (!M) { 2762 // We couldn't load the module. 2763 std::string Msg = "Unable to load module \"" + FileName.str() + "\": " 2764 + ErrorStr; 2765 Error(Msg); 2766 return Failure; 2767 } 2768 2769 if (!NewModule) { 2770 // We've already loaded this module. 2771 return Success; 2772 } 2773 2774 // FIXME: This seems rather a hack. Should CurrentDir be part of the 2775 // module? 2776 if (FileName != "-") { 2777 CurrentDir = llvm::sys::path::parent_path(FileName); 2778 if (CurrentDir.empty()) CurrentDir = "."; 2779 } 2780 2781 ModuleFile &F = *M; 2782 llvm::BitstreamCursor &Stream = F.Stream; 2783 Stream.init(F.StreamFile); 2784 F.SizeInBits = F.Buffer->getBufferSize() * 8; 2785 2786 // Sniff for the signature. 2787 if (Stream.Read(8) != 'C' || 2788 Stream.Read(8) != 'P' || 2789 Stream.Read(8) != 'C' || 2790 Stream.Read(8) != 'H') { 2791 Diag(diag::err_not_a_pch_file) << FileName; 2792 return Failure; 2793 } 2794 2795 while (!Stream.AtEndOfStream()) { 2796 unsigned Code = Stream.ReadCode(); 2797 2798 if (Code != llvm::bitc::ENTER_SUBBLOCK) { 2799 Error("invalid record at top-level of AST file"); 2800 return Failure; 2801 } 2802 2803 unsigned BlockID = Stream.ReadSubBlockID(); 2804 2805 // We only know the AST subblock ID. 2806 switch (BlockID) { 2807 case llvm::bitc::BLOCKINFO_BLOCK_ID: 2808 if (Stream.ReadBlockInfoBlock()) { 2809 Error("malformed BlockInfoBlock in AST file"); 2810 return Failure; 2811 } 2812 break; 2813 case AST_BLOCK_ID: 2814 switch (ReadASTBlock(F)) { 2815 case Success: 2816 break; 2817 2818 case Failure: 2819 return Failure; 2820 2821 case IgnorePCH: 2822 // FIXME: We could consider reading through to the end of this 2823 // AST block, skipping subblocks, to see if there are other 2824 // AST blocks elsewhere. 2825 2826 // FIXME: We can't clear loaded slocentries anymore. 2827 //SourceMgr.ClearPreallocatedSLocEntries(); 2828 2829 // Remove the stat cache. 2830 if (F.StatCache) 2831 FileMgr.removeStatCache((ASTStatCache*)F.StatCache); 2832 2833 return IgnorePCH; 2834 } 2835 break; 2836 default: 2837 if (Stream.SkipBlock()) { 2838 Error("malformed block record in AST file"); 2839 return Failure; 2840 } 2841 break; 2842 } 2843 } 2844 2845 // Once read, set the ModuleFile bit base offset and update the size in 2846 // bits of all files we've seen. 2847 F.GlobalBitOffset = TotalModulesSizeInBits; 2848 TotalModulesSizeInBits += F.SizeInBits; 2849 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F)); 2850 2851 // Make sure that the files this module was built against are still available. 2852 if (!DisableValidation) { 2853 switch(validateFileEntries(*M)) { 2854 case Failure: return Failure; 2855 case IgnorePCH: return IgnorePCH; 2856 case Success: break; 2857 } 2858 } 2859 2860 // Preload SLocEntries. 2861 for (unsigned I = 0, N = M->PreloadSLocEntries.size(); I != N; ++I) { 2862 int Index = int(M->PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID; 2863 // Load it through the SourceManager and don't call ReadSLocEntryRecord() 2864 // directly because the entry may have already been loaded in which case 2865 // calling ReadSLocEntryRecord() directly would trigger an assertion in 2866 // SourceManager. 2867 SourceMgr.getLoadedSLocEntryByID(Index); 2868 } 2869 2870 2871 return Success; 2872} 2873 2874void ASTReader::InitializeContext() { 2875 // If there's a listener, notify them that we "read" the translation unit. 2876 if (DeserializationListener) 2877 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, 2878 Context.getTranslationUnitDecl()); 2879 2880 // Make sure we load the declaration update records for the translation unit, 2881 // if there are any. 2882 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID, 2883 Context.getTranslationUnitDecl()); 2884 2885 // FIXME: Find a better way to deal with collisions between these 2886 // built-in types. Right now, we just ignore the problem. 2887 2888 // Load the special types. 2889 if (SpecialTypes.size() >= NumSpecialTypeIDs) { 2890 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) { 2891 if (!Context.CFConstantStringTypeDecl) 2892 Context.setCFConstantStringType(GetType(String)); 2893 } 2894 2895 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) { 2896 QualType FileType = GetType(File); 2897 if (FileType.isNull()) { 2898 Error("FILE type is NULL"); 2899 return; 2900 } 2901 2902 if (!Context.FILEDecl) { 2903 if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) 2904 Context.setFILEDecl(Typedef->getDecl()); 2905 else { 2906 const TagType *Tag = FileType->getAs<TagType>(); 2907 if (!Tag) { 2908 Error("Invalid FILE type in AST file"); 2909 return; 2910 } 2911 Context.setFILEDecl(Tag->getDecl()); 2912 } 2913 } 2914 } 2915 2916 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) { 2917 QualType Jmp_bufType = GetType(Jmp_buf); 2918 if (Jmp_bufType.isNull()) { 2919 Error("jmp_buf type is NULL"); 2920 return; 2921 } 2922 2923 if (!Context.jmp_bufDecl) { 2924 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) 2925 Context.setjmp_bufDecl(Typedef->getDecl()); 2926 else { 2927 const TagType *Tag = Jmp_bufType->getAs<TagType>(); 2928 if (!Tag) { 2929 Error("Invalid jmp_buf type in AST file"); 2930 return; 2931 } 2932 Context.setjmp_bufDecl(Tag->getDecl()); 2933 } 2934 } 2935 } 2936 2937 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) { 2938 QualType Sigjmp_bufType = GetType(Sigjmp_buf); 2939 if (Sigjmp_bufType.isNull()) { 2940 Error("sigjmp_buf type is NULL"); 2941 return; 2942 } 2943 2944 if (!Context.sigjmp_bufDecl) { 2945 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) 2946 Context.setsigjmp_bufDecl(Typedef->getDecl()); 2947 else { 2948 const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); 2949 assert(Tag && "Invalid sigjmp_buf type in AST file"); 2950 Context.setsigjmp_bufDecl(Tag->getDecl()); 2951 } 2952 } 2953 } 2954 2955 if (unsigned ObjCIdRedef 2956 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) { 2957 if (Context.ObjCIdRedefinitionType.isNull()) 2958 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef); 2959 } 2960 2961 if (unsigned ObjCClassRedef 2962 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) { 2963 if (Context.ObjCClassRedefinitionType.isNull()) 2964 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef); 2965 } 2966 2967 if (unsigned ObjCSelRedef 2968 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) { 2969 if (Context.ObjCSelRedefinitionType.isNull()) 2970 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef); 2971 } 2972 2973 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) { 2974 QualType Ucontext_tType = GetType(Ucontext_t); 2975 if (Ucontext_tType.isNull()) { 2976 Error("ucontext_t type is NULL"); 2977 return; 2978 } 2979 2980 if (!Context.ucontext_tDecl) { 2981 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>()) 2982 Context.setucontext_tDecl(Typedef->getDecl()); 2983 else { 2984 const TagType *Tag = Ucontext_tType->getAs<TagType>(); 2985 assert(Tag && "Invalid ucontext_t type in AST file"); 2986 Context.setucontext_tDecl(Tag->getDecl()); 2987 } 2988 } 2989 } 2990 } 2991 2992 ReadPragmaDiagnosticMappings(Context.getDiagnostics()); 2993 2994 // If there were any CUDA special declarations, deserialize them. 2995 if (!CUDASpecialDeclRefs.empty()) { 2996 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!"); 2997 Context.setcudaConfigureCallDecl( 2998 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0]))); 2999 } 3000 3001 // Re-export any modules that were imported by a non-module AST file. 3002 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) { 3003 if (Module *Imported = getSubmodule(ImportedModules[I])) 3004 makeModuleVisible(Imported, Module::AllVisible); 3005 } 3006 ImportedModules.clear(); 3007} 3008 3009void ASTReader::finalizeForWriting() { 3010 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(), 3011 HiddenEnd = HiddenNamesMap.end(); 3012 Hidden != HiddenEnd; ++Hidden) { 3013 makeNamesVisible(Hidden->second); 3014 } 3015 HiddenNamesMap.clear(); 3016} 3017 3018/// \brief Retrieve the name of the original source file name 3019/// directly from the AST file, without actually loading the AST 3020/// file. 3021std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName, 3022 FileManager &FileMgr, 3023 DiagnosticsEngine &Diags) { 3024 // Open the AST file. 3025 std::string ErrStr; 3026 OwningPtr<llvm::MemoryBuffer> Buffer; 3027 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr)); 3028 if (!Buffer) { 3029 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr; 3030 return std::string(); 3031 } 3032 3033 // Initialize the stream 3034 llvm::BitstreamReader StreamFile; 3035 llvm::BitstreamCursor Stream; 3036 StreamFile.init((const unsigned char *)Buffer->getBufferStart(), 3037 (const unsigned char *)Buffer->getBufferEnd()); 3038 Stream.init(StreamFile); 3039 3040 // Sniff for the signature. 3041 if (Stream.Read(8) != 'C' || 3042 Stream.Read(8) != 'P' || 3043 Stream.Read(8) != 'C' || 3044 Stream.Read(8) != 'H') { 3045 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName; 3046 return std::string(); 3047 } 3048 3049 RecordData Record; 3050 while (!Stream.AtEndOfStream()) { 3051 unsigned Code = Stream.ReadCode(); 3052 3053 if (Code == llvm::bitc::ENTER_SUBBLOCK) { 3054 unsigned BlockID = Stream.ReadSubBlockID(); 3055 3056 // We only know the AST subblock ID. 3057 switch (BlockID) { 3058 case AST_BLOCK_ID: 3059 if (Stream.EnterSubBlock(AST_BLOCK_ID)) { 3060 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 3061 return std::string(); 3062 } 3063 break; 3064 3065 default: 3066 if (Stream.SkipBlock()) { 3067 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 3068 return std::string(); 3069 } 3070 break; 3071 } 3072 continue; 3073 } 3074 3075 if (Code == llvm::bitc::END_BLOCK) { 3076 if (Stream.ReadBlockEnd()) { 3077 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName; 3078 return std::string(); 3079 } 3080 continue; 3081 } 3082 3083 if (Code == llvm::bitc::DEFINE_ABBREV) { 3084 Stream.ReadAbbrevRecord(); 3085 continue; 3086 } 3087 3088 Record.clear(); 3089 const char *BlobStart = 0; 3090 unsigned BlobLen = 0; 3091 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen) 3092 == ORIGINAL_FILE_NAME) 3093 return std::string(BlobStart, BlobLen); 3094 } 3095 3096 return std::string(); 3097} 3098 3099ASTReader::ASTReadResult ASTReader::ReadSubmoduleBlock(ModuleFile &F) { 3100 // Enter the submodule block. 3101 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) { 3102 Error("malformed submodule block record in AST file"); 3103 return Failure; 3104 } 3105 3106 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap(); 3107 bool First = true; 3108 Module *CurrentModule = 0; 3109 RecordData Record; 3110 while (true) { 3111 unsigned Code = F.Stream.ReadCode(); 3112 if (Code == llvm::bitc::END_BLOCK) { 3113 if (F.Stream.ReadBlockEnd()) { 3114 Error("error at end of submodule block in AST file"); 3115 return Failure; 3116 } 3117 return Success; 3118 } 3119 3120 if (Code == llvm::bitc::ENTER_SUBBLOCK) { 3121 // No known subblocks, always skip them. 3122 F.Stream.ReadSubBlockID(); 3123 if (F.Stream.SkipBlock()) { 3124 Error("malformed block record in AST file"); 3125 return Failure; 3126 } 3127 continue; 3128 } 3129 3130 if (Code == llvm::bitc::DEFINE_ABBREV) { 3131 F.Stream.ReadAbbrevRecord(); 3132 continue; 3133 } 3134 3135 // Read a record. 3136 const char *BlobStart; 3137 unsigned BlobLen; 3138 Record.clear(); 3139 switch (F.Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) { 3140 default: // Default behavior: ignore. 3141 break; 3142 3143 case SUBMODULE_DEFINITION: { 3144 if (First) { 3145 Error("missing submodule metadata record at beginning of block"); 3146 return Failure; 3147 } 3148 3149 if (Record.size() < 7) { 3150 Error("malformed module definition"); 3151 return Failure; 3152 } 3153 3154 StringRef Name(BlobStart, BlobLen); 3155 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]); 3156 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]); 3157 bool IsFramework = Record[2]; 3158 bool IsExplicit = Record[3]; 3159 bool IsSystem = Record[4]; 3160 bool InferSubmodules = Record[5]; 3161 bool InferExplicitSubmodules = Record[6]; 3162 bool InferExportWildcard = Record[7]; 3163 3164 Module *ParentModule = 0; 3165 if (Parent) 3166 ParentModule = getSubmodule(Parent); 3167 3168 // Retrieve this (sub)module from the module map, creating it if 3169 // necessary. 3170 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule, 3171 IsFramework, 3172 IsExplicit).first; 3173 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS; 3174 if (GlobalIndex >= SubmodulesLoaded.size() || 3175 SubmodulesLoaded[GlobalIndex]) { 3176 Error("too many submodules"); 3177 return Failure; 3178 } 3179 3180 CurrentModule->setASTFile(F.File); 3181 CurrentModule->IsFromModuleFile = true; 3182 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem; 3183 CurrentModule->InferSubmodules = InferSubmodules; 3184 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules; 3185 CurrentModule->InferExportWildcard = InferExportWildcard; 3186 if (DeserializationListener) 3187 DeserializationListener->ModuleRead(GlobalID, CurrentModule); 3188 3189 SubmodulesLoaded[GlobalIndex] = CurrentModule; 3190 break; 3191 } 3192 3193 case SUBMODULE_UMBRELLA_HEADER: { 3194 if (First) { 3195 Error("missing submodule metadata record at beginning of block"); 3196 return Failure; 3197 } 3198 3199 if (!CurrentModule) 3200 break; 3201 3202 StringRef FileName(BlobStart, BlobLen); 3203 if (const FileEntry *Umbrella = PP.getFileManager().getFile(FileName)) { 3204 if (!CurrentModule->getUmbrellaHeader()) 3205 ModMap.setUmbrellaHeader(CurrentModule, Umbrella); 3206 else if (CurrentModule->getUmbrellaHeader() != Umbrella) { 3207 Error("mismatched umbrella headers in submodule"); 3208 return Failure; 3209 } 3210 } 3211 break; 3212 } 3213 3214 case SUBMODULE_HEADER: { 3215 if (First) { 3216 Error("missing submodule metadata record at beginning of block"); 3217 return Failure; 3218 } 3219 3220 if (!CurrentModule) 3221 break; 3222 3223 // FIXME: Be more lazy about this! 3224 StringRef FileName(BlobStart, BlobLen); 3225 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) { 3226 if (std::find(CurrentModule->Headers.begin(), 3227 CurrentModule->Headers.end(), 3228 File) == CurrentModule->Headers.end()) 3229 ModMap.addHeader(CurrentModule, File); 3230 } 3231 break; 3232 } 3233 3234 case SUBMODULE_TOPHEADER: { 3235 if (First) { 3236 Error("missing submodule metadata record at beginning of block"); 3237 return Failure; 3238 } 3239 3240 if (!CurrentModule) 3241 break; 3242 3243 // FIXME: Be more lazy about this! 3244 StringRef FileName(BlobStart, BlobLen); 3245 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) 3246 CurrentModule->TopHeaders.insert(File); 3247 break; 3248 } 3249 3250 case SUBMODULE_UMBRELLA_DIR: { 3251 if (First) { 3252 Error("missing submodule metadata record at beginning of block"); 3253 return Failure; 3254 } 3255 3256 if (!CurrentModule) 3257 break; 3258 3259 StringRef DirName(BlobStart, BlobLen); 3260 if (const DirectoryEntry *Umbrella 3261 = PP.getFileManager().getDirectory(DirName)) { 3262 if (!CurrentModule->getUmbrellaDir()) 3263 ModMap.setUmbrellaDir(CurrentModule, Umbrella); 3264 else if (CurrentModule->getUmbrellaDir() != Umbrella) { 3265 Error("mismatched umbrella directories in submodule"); 3266 return Failure; 3267 } 3268 } 3269 break; 3270 } 3271 3272 case SUBMODULE_METADATA: { 3273 if (!First) { 3274 Error("submodule metadata record not at beginning of block"); 3275 return Failure; 3276 } 3277 First = false; 3278 3279 F.BaseSubmoduleID = getTotalNumSubmodules(); 3280 F.LocalNumSubmodules = Record[0]; 3281 unsigned LocalBaseSubmoduleID = Record[1]; 3282 if (F.LocalNumSubmodules > 0) { 3283 // Introduce the global -> local mapping for submodules within this 3284 // module. 3285 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F)); 3286 3287 // Introduce the local -> global mapping for submodules within this 3288 // module. 3289 F.SubmoduleRemap.insertOrReplace( 3290 std::make_pair(LocalBaseSubmoduleID, 3291 F.BaseSubmoduleID - LocalBaseSubmoduleID)); 3292 3293 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules); 3294 } 3295 break; 3296 } 3297 3298 case SUBMODULE_IMPORTS: { 3299 if (First) { 3300 Error("missing submodule metadata record at beginning of block"); 3301 return Failure; 3302 } 3303 3304 if (!CurrentModule) 3305 break; 3306 3307 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) { 3308 UnresolvedModuleImportExport Unresolved; 3309 Unresolved.File = &F; 3310 Unresolved.Mod = CurrentModule; 3311 Unresolved.ID = Record[Idx]; 3312 Unresolved.IsImport = true; 3313 Unresolved.IsWildcard = false; 3314 UnresolvedModuleImportExports.push_back(Unresolved); 3315 } 3316 break; 3317 } 3318 3319 case SUBMODULE_EXPORTS: { 3320 if (First) { 3321 Error("missing submodule metadata record at beginning of block"); 3322 return Failure; 3323 } 3324 3325 if (!CurrentModule) 3326 break; 3327 3328 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) { 3329 UnresolvedModuleImportExport Unresolved; 3330 Unresolved.File = &F; 3331 Unresolved.Mod = CurrentModule; 3332 Unresolved.ID = Record[Idx]; 3333 Unresolved.IsImport = false; 3334 Unresolved.IsWildcard = Record[Idx + 1]; 3335 UnresolvedModuleImportExports.push_back(Unresolved); 3336 } 3337 3338 // Once we've loaded the set of exports, there's no reason to keep 3339 // the parsed, unresolved exports around. 3340 CurrentModule->UnresolvedExports.clear(); 3341 break; 3342 } 3343 case SUBMODULE_REQUIRES: { 3344 if (First) { 3345 Error("missing submodule metadata record at beginning of block"); 3346 return Failure; 3347 } 3348 3349 if (!CurrentModule) 3350 break; 3351 3352 CurrentModule->addRequirement(StringRef(BlobStart, BlobLen), 3353 Context.getLangOpts(), 3354 Context.getTargetInfo()); 3355 break; 3356 } 3357 } 3358 } 3359} 3360 3361/// \brief Parse the record that corresponds to a LangOptions data 3362/// structure. 3363/// 3364/// This routine parses the language options from the AST file and then gives 3365/// them to the AST listener if one is set. 3366/// 3367/// \returns true if the listener deems the file unacceptable, false otherwise. 3368bool ASTReader::ParseLanguageOptions(const RecordData &Record) { 3369 if (Listener) { 3370 LangOptions LangOpts; 3371 unsigned Idx = 0; 3372#define LANGOPT(Name, Bits, Default, Description) \ 3373 LangOpts.Name = Record[Idx++]; 3374#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 3375 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++])); 3376#include "clang/Basic/LangOptions.def" 3377 3378 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++]; 3379 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx); 3380 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion); 3381 3382 unsigned Length = Record[Idx++]; 3383 LangOpts.CurrentModule.assign(Record.begin() + Idx, 3384 Record.begin() + Idx + Length); 3385 return Listener->ReadLanguageOptions(LangOpts); 3386 } 3387 3388 return false; 3389} 3390 3391std::pair<ModuleFile *, unsigned> 3392ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) { 3393 GlobalPreprocessedEntityMapType::iterator 3394 I = GlobalPreprocessedEntityMap.find(GlobalIndex); 3395 assert(I != GlobalPreprocessedEntityMap.end() && 3396 "Corrupted global preprocessed entity map"); 3397 ModuleFile *M = I->second; 3398 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID; 3399 return std::make_pair(M, LocalIndex); 3400} 3401 3402std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator> 3403ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const { 3404 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord()) 3405 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID, 3406 Mod.NumPreprocessedEntities); 3407 3408 return std::make_pair(PreprocessingRecord::iterator(), 3409 PreprocessingRecord::iterator()); 3410} 3411 3412std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator> 3413ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) { 3414 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls), 3415 ModuleDeclIterator(this, &Mod, 3416 Mod.FileSortedDecls + Mod.NumFileSortedDecls)); 3417} 3418 3419PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) { 3420 PreprocessedEntityID PPID = Index+1; 3421 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 3422 ModuleFile &M = *PPInfo.first; 3423 unsigned LocalIndex = PPInfo.second; 3424 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 3425 3426 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor); 3427 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset); 3428 3429 unsigned Code = M.PreprocessorDetailCursor.ReadCode(); 3430 switch (Code) { 3431 case llvm::bitc::END_BLOCK: 3432 return 0; 3433 3434 case llvm::bitc::ENTER_SUBBLOCK: 3435 Error("unexpected subblock record in preprocessor detail block"); 3436 return 0; 3437 3438 case llvm::bitc::DEFINE_ABBREV: 3439 Error("unexpected abbrevation record in preprocessor detail block"); 3440 return 0; 3441 3442 default: 3443 break; 3444 } 3445 3446 if (!PP.getPreprocessingRecord()) { 3447 Error("no preprocessing record"); 3448 return 0; 3449 } 3450 3451 // Read the record. 3452 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin), 3453 ReadSourceLocation(M, PPOffs.End)); 3454 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 3455 const char *BlobStart = 0; 3456 unsigned BlobLen = 0; 3457 RecordData Record; 3458 PreprocessorDetailRecordTypes RecType = 3459 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.ReadRecord( 3460 Code, Record, BlobStart, BlobLen); 3461 switch (RecType) { 3462 case PPD_MACRO_EXPANSION: { 3463 bool isBuiltin = Record[0]; 3464 IdentifierInfo *Name = 0; 3465 MacroDefinition *Def = 0; 3466 if (isBuiltin) 3467 Name = getLocalIdentifier(M, Record[1]); 3468 else { 3469 PreprocessedEntityID 3470 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]); 3471 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1)); 3472 } 3473 3474 MacroExpansion *ME; 3475 if (isBuiltin) 3476 ME = new (PPRec) MacroExpansion(Name, Range); 3477 else 3478 ME = new (PPRec) MacroExpansion(Def, Range); 3479 3480 return ME; 3481 } 3482 3483 case PPD_MACRO_DEFINITION: { 3484 // Decode the identifier info and then check again; if the macro is 3485 // still defined and associated with the identifier, 3486 IdentifierInfo *II = getLocalIdentifier(M, Record[0]); 3487 MacroDefinition *MD 3488 = new (PPRec) MacroDefinition(II, Range); 3489 3490 if (DeserializationListener) 3491 DeserializationListener->MacroDefinitionRead(PPID, MD); 3492 3493 return MD; 3494 } 3495 3496 case PPD_INCLUSION_DIRECTIVE: { 3497 const char *FullFileNameStart = BlobStart + Record[0]; 3498 StringRef FullFileName(FullFileNameStart, BlobLen - Record[0]); 3499 const FileEntry *File = 0; 3500 if (!FullFileName.empty()) 3501 File = PP.getFileManager().getFile(FullFileName); 3502 3503 // FIXME: Stable encoding 3504 InclusionDirective::InclusionKind Kind 3505 = static_cast<InclusionDirective::InclusionKind>(Record[2]); 3506 InclusionDirective *ID 3507 = new (PPRec) InclusionDirective(PPRec, Kind, 3508 StringRef(BlobStart, Record[0]), 3509 Record[1], Record[3], 3510 File, 3511 Range); 3512 return ID; 3513 } 3514 } 3515 3516 llvm_unreachable("Invalid PreprocessorDetailRecordTypes"); 3517} 3518 3519/// \brief \arg SLocMapI points at a chunk of a module that contains no 3520/// preprocessed entities or the entities it contains are not the ones we are 3521/// looking for. Find the next module that contains entities and return the ID 3522/// of the first entry. 3523PreprocessedEntityID ASTReader::findNextPreprocessedEntity( 3524 GlobalSLocOffsetMapType::const_iterator SLocMapI) const { 3525 ++SLocMapI; 3526 for (GlobalSLocOffsetMapType::const_iterator 3527 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) { 3528 ModuleFile &M = *SLocMapI->second; 3529 if (M.NumPreprocessedEntities) 3530 return getGlobalPreprocessedEntityID(M, M.BasePreprocessedEntityID); 3531 } 3532 3533 return getTotalNumPreprocessedEntities(); 3534} 3535 3536namespace { 3537 3538template <unsigned PPEntityOffset::*PPLoc> 3539struct PPEntityComp { 3540 const ASTReader &Reader; 3541 ModuleFile &M; 3542 3543 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { } 3544 3545 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const { 3546 SourceLocation LHS = getLoc(L); 3547 SourceLocation RHS = getLoc(R); 3548 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 3549 } 3550 3551 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const { 3552 SourceLocation LHS = getLoc(L); 3553 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 3554 } 3555 3556 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const { 3557 SourceLocation RHS = getLoc(R); 3558 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 3559 } 3560 3561 SourceLocation getLoc(const PPEntityOffset &PPE) const { 3562 return Reader.ReadSourceLocation(M, PPE.*PPLoc); 3563 } 3564}; 3565 3566} 3567 3568/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc. 3569PreprocessedEntityID 3570ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const { 3571 if (SourceMgr.isLocalSourceLocation(BLoc)) 3572 return getTotalNumPreprocessedEntities(); 3573 3574 GlobalSLocOffsetMapType::const_iterator 3575 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset - 3576 BLoc.getOffset()); 3577 assert(SLocMapI != GlobalSLocOffsetMap.end() && 3578 "Corrupted global sloc offset map"); 3579 3580 if (SLocMapI->second->NumPreprocessedEntities == 0) 3581 return findNextPreprocessedEntity(SLocMapI); 3582 3583 ModuleFile &M = *SLocMapI->second; 3584 typedef const PPEntityOffset *pp_iterator; 3585 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 3586 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 3587 3588 size_t Count = M.NumPreprocessedEntities; 3589 size_t Half; 3590 pp_iterator First = pp_begin; 3591 pp_iterator PPI; 3592 3593 // Do a binary search manually instead of using std::lower_bound because 3594 // The end locations of entities may be unordered (when a macro expansion 3595 // is inside another macro argument), but for this case it is not important 3596 // whether we get the first macro expansion or its containing macro. 3597 while (Count > 0) { 3598 Half = Count/2; 3599 PPI = First; 3600 std::advance(PPI, Half); 3601 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End), 3602 BLoc)){ 3603 First = PPI; 3604 ++First; 3605 Count = Count - Half - 1; 3606 } else 3607 Count = Half; 3608 } 3609 3610 if (PPI == pp_end) 3611 return findNextPreprocessedEntity(SLocMapI); 3612 3613 return getGlobalPreprocessedEntityID(M, 3614 M.BasePreprocessedEntityID + (PPI - pp_begin)); 3615} 3616 3617/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc. 3618PreprocessedEntityID 3619ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const { 3620 if (SourceMgr.isLocalSourceLocation(ELoc)) 3621 return getTotalNumPreprocessedEntities(); 3622 3623 GlobalSLocOffsetMapType::const_iterator 3624 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset - 3625 ELoc.getOffset()); 3626 assert(SLocMapI != GlobalSLocOffsetMap.end() && 3627 "Corrupted global sloc offset map"); 3628 3629 if (SLocMapI->second->NumPreprocessedEntities == 0) 3630 return findNextPreprocessedEntity(SLocMapI); 3631 3632 ModuleFile &M = *SLocMapI->second; 3633 typedef const PPEntityOffset *pp_iterator; 3634 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 3635 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 3636 pp_iterator PPI = 3637 std::upper_bound(pp_begin, pp_end, ELoc, 3638 PPEntityComp<&PPEntityOffset::Begin>(*this, M)); 3639 3640 if (PPI == pp_end) 3641 return findNextPreprocessedEntity(SLocMapI); 3642 3643 return getGlobalPreprocessedEntityID(M, 3644 M.BasePreprocessedEntityID + (PPI - pp_begin)); 3645} 3646 3647/// \brief Returns a pair of [Begin, End) indices of preallocated 3648/// preprocessed entities that \arg Range encompasses. 3649std::pair<unsigned, unsigned> 3650 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) { 3651 if (Range.isInvalid()) 3652 return std::make_pair(0,0); 3653 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); 3654 3655 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin()); 3656 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd()); 3657 return std::make_pair(BeginID, EndID); 3658} 3659 3660/// \brief Optionally returns true or false if the preallocated preprocessed 3661/// entity with index \arg Index came from file \arg FID. 3662llvm::Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index, 3663 FileID FID) { 3664 if (FID.isInvalid()) 3665 return false; 3666 3667 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 3668 ModuleFile &M = *PPInfo.first; 3669 unsigned LocalIndex = PPInfo.second; 3670 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 3671 3672 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin); 3673 if (Loc.isInvalid()) 3674 return false; 3675 3676 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID)) 3677 return true; 3678 else 3679 return false; 3680} 3681 3682namespace { 3683 /// \brief Visitor used to search for information about a header file. 3684 class HeaderFileInfoVisitor { 3685 ASTReader &Reader; 3686 const FileEntry *FE; 3687 3688 llvm::Optional<HeaderFileInfo> HFI; 3689 3690 public: 3691 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE) 3692 : Reader(Reader), FE(FE) { } 3693 3694 static bool visit(ModuleFile &M, void *UserData) { 3695 HeaderFileInfoVisitor *This 3696 = static_cast<HeaderFileInfoVisitor *>(UserData); 3697 3698 HeaderFileInfoTrait Trait(This->Reader, M, 3699 &This->Reader.getPreprocessor().getHeaderSearchInfo(), 3700 M.HeaderFileFrameworkStrings, 3701 This->FE->getName()); 3702 3703 HeaderFileInfoLookupTable *Table 3704 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable); 3705 if (!Table) 3706 return false; 3707 3708 // Look in the on-disk hash table for an entry for this file name. 3709 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(), 3710 &Trait); 3711 if (Pos == Table->end()) 3712 return false; 3713 3714 This->HFI = *Pos; 3715 return true; 3716 } 3717 3718 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; } 3719 }; 3720} 3721 3722HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) { 3723 HeaderFileInfoVisitor Visitor(*this, FE); 3724 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor); 3725 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) { 3726 if (Listener) 3727 Listener->ReadHeaderFileInfo(*HFI, FE->getUID()); 3728 return *HFI; 3729 } 3730 3731 return HeaderFileInfo(); 3732} 3733 3734void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) { 3735 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) { 3736 ModuleFile &F = *(*I); 3737 unsigned Idx = 0; 3738 while (Idx < F.PragmaDiagMappings.size()) { 3739 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]); 3740 Diag.DiagStates.push_back(*Diag.GetCurDiagState()); 3741 Diag.DiagStatePoints.push_back( 3742 DiagnosticsEngine::DiagStatePoint(&Diag.DiagStates.back(), 3743 FullSourceLoc(Loc, SourceMgr))); 3744 while (1) { 3745 assert(Idx < F.PragmaDiagMappings.size() && 3746 "Invalid data, didn't find '-1' marking end of diag/map pairs"); 3747 if (Idx >= F.PragmaDiagMappings.size()) { 3748 break; // Something is messed up but at least avoid infinite loop in 3749 // release build. 3750 } 3751 unsigned DiagID = F.PragmaDiagMappings[Idx++]; 3752 if (DiagID == (unsigned)-1) { 3753 break; // no more diag/map pairs for this location. 3754 } 3755 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++]; 3756 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc); 3757 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo); 3758 } 3759 } 3760 } 3761} 3762 3763/// \brief Get the correct cursor and offset for loading a type. 3764ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) { 3765 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index); 3766 assert(I != GlobalTypeMap.end() && "Corrupted global type map"); 3767 ModuleFile *M = I->second; 3768 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]); 3769} 3770 3771/// \brief Read and return the type with the given index.. 3772/// 3773/// The index is the type ID, shifted and minus the number of predefs. This 3774/// routine actually reads the record corresponding to the type at the given 3775/// location. It is a helper routine for GetType, which deals with reading type 3776/// IDs. 3777QualType ASTReader::readTypeRecord(unsigned Index) { 3778 RecordLocation Loc = TypeCursorForIndex(Index); 3779 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 3780 3781 // Keep track of where we are in the stream, then jump back there 3782 // after reading this type. 3783 SavedStreamPosition SavedPosition(DeclsCursor); 3784 3785 ReadingKindTracker ReadingKind(Read_Type, *this); 3786 3787 // Note that we are loading a type record. 3788 Deserializing AType(this); 3789 3790 unsigned Idx = 0; 3791 DeclsCursor.JumpToBit(Loc.Offset); 3792 RecordData Record; 3793 unsigned Code = DeclsCursor.ReadCode(); 3794 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) { 3795 case TYPE_EXT_QUAL: { 3796 if (Record.size() != 2) { 3797 Error("Incorrect encoding of extended qualifier type"); 3798 return QualType(); 3799 } 3800 QualType Base = readType(*Loc.F, Record, Idx); 3801 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]); 3802 return Context.getQualifiedType(Base, Quals); 3803 } 3804 3805 case TYPE_COMPLEX: { 3806 if (Record.size() != 1) { 3807 Error("Incorrect encoding of complex type"); 3808 return QualType(); 3809 } 3810 QualType ElemType = readType(*Loc.F, Record, Idx); 3811 return Context.getComplexType(ElemType); 3812 } 3813 3814 case TYPE_POINTER: { 3815 if (Record.size() != 1) { 3816 Error("Incorrect encoding of pointer type"); 3817 return QualType(); 3818 } 3819 QualType PointeeType = readType(*Loc.F, Record, Idx); 3820 return Context.getPointerType(PointeeType); 3821 } 3822 3823 case TYPE_BLOCK_POINTER: { 3824 if (Record.size() != 1) { 3825 Error("Incorrect encoding of block pointer type"); 3826 return QualType(); 3827 } 3828 QualType PointeeType = readType(*Loc.F, Record, Idx); 3829 return Context.getBlockPointerType(PointeeType); 3830 } 3831 3832 case TYPE_LVALUE_REFERENCE: { 3833 if (Record.size() != 2) { 3834 Error("Incorrect encoding of lvalue reference type"); 3835 return QualType(); 3836 } 3837 QualType PointeeType = readType(*Loc.F, Record, Idx); 3838 return Context.getLValueReferenceType(PointeeType, Record[1]); 3839 } 3840 3841 case TYPE_RVALUE_REFERENCE: { 3842 if (Record.size() != 1) { 3843 Error("Incorrect encoding of rvalue reference type"); 3844 return QualType(); 3845 } 3846 QualType PointeeType = readType(*Loc.F, Record, Idx); 3847 return Context.getRValueReferenceType(PointeeType); 3848 } 3849 3850 case TYPE_MEMBER_POINTER: { 3851 if (Record.size() != 2) { 3852 Error("Incorrect encoding of member pointer type"); 3853 return QualType(); 3854 } 3855 QualType PointeeType = readType(*Loc.F, Record, Idx); 3856 QualType ClassType = readType(*Loc.F, Record, Idx); 3857 if (PointeeType.isNull() || ClassType.isNull()) 3858 return QualType(); 3859 3860 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr()); 3861 } 3862 3863 case TYPE_CONSTANT_ARRAY: { 3864 QualType ElementType = readType(*Loc.F, Record, Idx); 3865 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 3866 unsigned IndexTypeQuals = Record[2]; 3867 unsigned Idx = 3; 3868 llvm::APInt Size = ReadAPInt(Record, Idx); 3869 return Context.getConstantArrayType(ElementType, Size, 3870 ASM, IndexTypeQuals); 3871 } 3872 3873 case TYPE_INCOMPLETE_ARRAY: { 3874 QualType ElementType = readType(*Loc.F, Record, Idx); 3875 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 3876 unsigned IndexTypeQuals = Record[2]; 3877 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals); 3878 } 3879 3880 case TYPE_VARIABLE_ARRAY: { 3881 QualType ElementType = readType(*Loc.F, Record, Idx); 3882 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 3883 unsigned IndexTypeQuals = Record[2]; 3884 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]); 3885 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]); 3886 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F), 3887 ASM, IndexTypeQuals, 3888 SourceRange(LBLoc, RBLoc)); 3889 } 3890 3891 case TYPE_VECTOR: { 3892 if (Record.size() != 3) { 3893 Error("incorrect encoding of vector type in AST file"); 3894 return QualType(); 3895 } 3896 3897 QualType ElementType = readType(*Loc.F, Record, Idx); 3898 unsigned NumElements = Record[1]; 3899 unsigned VecKind = Record[2]; 3900 return Context.getVectorType(ElementType, NumElements, 3901 (VectorType::VectorKind)VecKind); 3902 } 3903 3904 case TYPE_EXT_VECTOR: { 3905 if (Record.size() != 3) { 3906 Error("incorrect encoding of extended vector type in AST file"); 3907 return QualType(); 3908 } 3909 3910 QualType ElementType = readType(*Loc.F, Record, Idx); 3911 unsigned NumElements = Record[1]; 3912 return Context.getExtVectorType(ElementType, NumElements); 3913 } 3914 3915 case TYPE_FUNCTION_NO_PROTO: { 3916 if (Record.size() != 6) { 3917 Error("incorrect encoding of no-proto function type"); 3918 return QualType(); 3919 } 3920 QualType ResultType = readType(*Loc.F, Record, Idx); 3921 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3], 3922 (CallingConv)Record[4], Record[5]); 3923 return Context.getFunctionNoProtoType(ResultType, Info); 3924 } 3925 3926 case TYPE_FUNCTION_PROTO: { 3927 QualType ResultType = readType(*Loc.F, Record, Idx); 3928 3929 FunctionProtoType::ExtProtoInfo EPI; 3930 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1], 3931 /*hasregparm*/ Record[2], 3932 /*regparm*/ Record[3], 3933 static_cast<CallingConv>(Record[4]), 3934 /*produces*/ Record[5]); 3935 3936 unsigned Idx = 6; 3937 unsigned NumParams = Record[Idx++]; 3938 SmallVector<QualType, 16> ParamTypes; 3939 for (unsigned I = 0; I != NumParams; ++I) 3940 ParamTypes.push_back(readType(*Loc.F, Record, Idx)); 3941 3942 EPI.Variadic = Record[Idx++]; 3943 EPI.HasTrailingReturn = Record[Idx++]; 3944 EPI.TypeQuals = Record[Idx++]; 3945 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]); 3946 ExceptionSpecificationType EST = 3947 static_cast<ExceptionSpecificationType>(Record[Idx++]); 3948 EPI.ExceptionSpecType = EST; 3949 SmallVector<QualType, 2> Exceptions; 3950 if (EST == EST_Dynamic) { 3951 EPI.NumExceptions = Record[Idx++]; 3952 for (unsigned I = 0; I != EPI.NumExceptions; ++I) 3953 Exceptions.push_back(readType(*Loc.F, Record, Idx)); 3954 EPI.Exceptions = Exceptions.data(); 3955 } else if (EST == EST_ComputedNoexcept) { 3956 EPI.NoexceptExpr = ReadExpr(*Loc.F); 3957 } else if (EST == EST_Uninstantiated) { 3958 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx); 3959 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx); 3960 } else if (EST == EST_Unevaluated) { 3961 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx); 3962 } 3963 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams, 3964 EPI); 3965 } 3966 3967 case TYPE_UNRESOLVED_USING: { 3968 unsigned Idx = 0; 3969 return Context.getTypeDeclType( 3970 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx)); 3971 } 3972 3973 case TYPE_TYPEDEF: { 3974 if (Record.size() != 2) { 3975 Error("incorrect encoding of typedef type"); 3976 return QualType(); 3977 } 3978 unsigned Idx = 0; 3979 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx); 3980 QualType Canonical = readType(*Loc.F, Record, Idx); 3981 if (!Canonical.isNull()) 3982 Canonical = Context.getCanonicalType(Canonical); 3983 return Context.getTypedefType(Decl, Canonical); 3984 } 3985 3986 case TYPE_TYPEOF_EXPR: 3987 return Context.getTypeOfExprType(ReadExpr(*Loc.F)); 3988 3989 case TYPE_TYPEOF: { 3990 if (Record.size() != 1) { 3991 Error("incorrect encoding of typeof(type) in AST file"); 3992 return QualType(); 3993 } 3994 QualType UnderlyingType = readType(*Loc.F, Record, Idx); 3995 return Context.getTypeOfType(UnderlyingType); 3996 } 3997 3998 case TYPE_DECLTYPE: { 3999 QualType UnderlyingType = readType(*Loc.F, Record, Idx); 4000 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType); 4001 } 4002 4003 case TYPE_UNARY_TRANSFORM: { 4004 QualType BaseType = readType(*Loc.F, Record, Idx); 4005 QualType UnderlyingType = readType(*Loc.F, Record, Idx); 4006 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2]; 4007 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind); 4008 } 4009 4010 case TYPE_AUTO: 4011 return Context.getAutoType(readType(*Loc.F, Record, Idx)); 4012 4013 case TYPE_RECORD: { 4014 if (Record.size() != 2) { 4015 Error("incorrect encoding of record type"); 4016 return QualType(); 4017 } 4018 unsigned Idx = 0; 4019 bool IsDependent = Record[Idx++]; 4020 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx); 4021 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl()); 4022 QualType T = Context.getRecordType(RD); 4023 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 4024 return T; 4025 } 4026 4027 case TYPE_ENUM: { 4028 if (Record.size() != 2) { 4029 Error("incorrect encoding of enum type"); 4030 return QualType(); 4031 } 4032 unsigned Idx = 0; 4033 bool IsDependent = Record[Idx++]; 4034 QualType T 4035 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx)); 4036 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 4037 return T; 4038 } 4039 4040 case TYPE_ATTRIBUTED: { 4041 if (Record.size() != 3) { 4042 Error("incorrect encoding of attributed type"); 4043 return QualType(); 4044 } 4045 QualType modifiedType = readType(*Loc.F, Record, Idx); 4046 QualType equivalentType = readType(*Loc.F, Record, Idx); 4047 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]); 4048 return Context.getAttributedType(kind, modifiedType, equivalentType); 4049 } 4050 4051 case TYPE_PAREN: { 4052 if (Record.size() != 1) { 4053 Error("incorrect encoding of paren type"); 4054 return QualType(); 4055 } 4056 QualType InnerType = readType(*Loc.F, Record, Idx); 4057 return Context.getParenType(InnerType); 4058 } 4059 4060 case TYPE_PACK_EXPANSION: { 4061 if (Record.size() != 2) { 4062 Error("incorrect encoding of pack expansion type"); 4063 return QualType(); 4064 } 4065 QualType Pattern = readType(*Loc.F, Record, Idx); 4066 if (Pattern.isNull()) 4067 return QualType(); 4068 llvm::Optional<unsigned> NumExpansions; 4069 if (Record[1]) 4070 NumExpansions = Record[1] - 1; 4071 return Context.getPackExpansionType(Pattern, NumExpansions); 4072 } 4073 4074 case TYPE_ELABORATED: { 4075 unsigned Idx = 0; 4076 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 4077 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 4078 QualType NamedType = readType(*Loc.F, Record, Idx); 4079 return Context.getElaboratedType(Keyword, NNS, NamedType); 4080 } 4081 4082 case TYPE_OBJC_INTERFACE: { 4083 unsigned Idx = 0; 4084 ObjCInterfaceDecl *ItfD 4085 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx); 4086 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl()); 4087 } 4088 4089 case TYPE_OBJC_OBJECT: { 4090 unsigned Idx = 0; 4091 QualType Base = readType(*Loc.F, Record, Idx); 4092 unsigned NumProtos = Record[Idx++]; 4093 SmallVector<ObjCProtocolDecl*, 4> Protos; 4094 for (unsigned I = 0; I != NumProtos; ++I) 4095 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx)); 4096 return Context.getObjCObjectType(Base, Protos.data(), NumProtos); 4097 } 4098 4099 case TYPE_OBJC_OBJECT_POINTER: { 4100 unsigned Idx = 0; 4101 QualType Pointee = readType(*Loc.F, Record, Idx); 4102 return Context.getObjCObjectPointerType(Pointee); 4103 } 4104 4105 case TYPE_SUBST_TEMPLATE_TYPE_PARM: { 4106 unsigned Idx = 0; 4107 QualType Parm = readType(*Loc.F, Record, Idx); 4108 QualType Replacement = readType(*Loc.F, Record, Idx); 4109 return 4110 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm), 4111 Replacement); 4112 } 4113 4114 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: { 4115 unsigned Idx = 0; 4116 QualType Parm = readType(*Loc.F, Record, Idx); 4117 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx); 4118 return Context.getSubstTemplateTypeParmPackType( 4119 cast<TemplateTypeParmType>(Parm), 4120 ArgPack); 4121 } 4122 4123 case TYPE_INJECTED_CLASS_NAME: { 4124 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx); 4125 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable 4126 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable 4127 // for AST reading, too much interdependencies. 4128 return 4129 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0); 4130 } 4131 4132 case TYPE_TEMPLATE_TYPE_PARM: { 4133 unsigned Idx = 0; 4134 unsigned Depth = Record[Idx++]; 4135 unsigned Index = Record[Idx++]; 4136 bool Pack = Record[Idx++]; 4137 TemplateTypeParmDecl *D 4138 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx); 4139 return Context.getTemplateTypeParmType(Depth, Index, Pack, D); 4140 } 4141 4142 case TYPE_DEPENDENT_NAME: { 4143 unsigned Idx = 0; 4144 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 4145 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 4146 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx); 4147 QualType Canon = readType(*Loc.F, Record, Idx); 4148 if (!Canon.isNull()) 4149 Canon = Context.getCanonicalType(Canon); 4150 return Context.getDependentNameType(Keyword, NNS, Name, Canon); 4151 } 4152 4153 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: { 4154 unsigned Idx = 0; 4155 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 4156 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 4157 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx); 4158 unsigned NumArgs = Record[Idx++]; 4159 SmallVector<TemplateArgument, 8> Args; 4160 Args.reserve(NumArgs); 4161 while (NumArgs--) 4162 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx)); 4163 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name, 4164 Args.size(), Args.data()); 4165 } 4166 4167 case TYPE_DEPENDENT_SIZED_ARRAY: { 4168 unsigned Idx = 0; 4169 4170 // ArrayType 4171 QualType ElementType = readType(*Loc.F, Record, Idx); 4172 ArrayType::ArraySizeModifier ASM 4173 = (ArrayType::ArraySizeModifier)Record[Idx++]; 4174 unsigned IndexTypeQuals = Record[Idx++]; 4175 4176 // DependentSizedArrayType 4177 Expr *NumElts = ReadExpr(*Loc.F); 4178 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx); 4179 4180 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM, 4181 IndexTypeQuals, Brackets); 4182 } 4183 4184 case TYPE_TEMPLATE_SPECIALIZATION: { 4185 unsigned Idx = 0; 4186 bool IsDependent = Record[Idx++]; 4187 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx); 4188 SmallVector<TemplateArgument, 8> Args; 4189 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx); 4190 QualType Underlying = readType(*Loc.F, Record, Idx); 4191 QualType T; 4192 if (Underlying.isNull()) 4193 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(), 4194 Args.size()); 4195 else 4196 T = Context.getTemplateSpecializationType(Name, Args.data(), 4197 Args.size(), Underlying); 4198 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 4199 return T; 4200 } 4201 4202 case TYPE_ATOMIC: { 4203 if (Record.size() != 1) { 4204 Error("Incorrect encoding of atomic type"); 4205 return QualType(); 4206 } 4207 QualType ValueType = readType(*Loc.F, Record, Idx); 4208 return Context.getAtomicType(ValueType); 4209 } 4210 } 4211 llvm_unreachable("Invalid TypeCode!"); 4212} 4213 4214class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> { 4215 ASTReader &Reader; 4216 ModuleFile &F; 4217 const ASTReader::RecordData &Record; 4218 unsigned &Idx; 4219 4220 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R, 4221 unsigned &I) { 4222 return Reader.ReadSourceLocation(F, R, I); 4223 } 4224 4225 template<typename T> 4226 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) { 4227 return Reader.ReadDeclAs<T>(F, Record, Idx); 4228 } 4229 4230public: 4231 TypeLocReader(ASTReader &Reader, ModuleFile &F, 4232 const ASTReader::RecordData &Record, unsigned &Idx) 4233 : Reader(Reader), F(F), Record(Record), Idx(Idx) 4234 { } 4235 4236 // We want compile-time assurance that we've enumerated all of 4237 // these, so unfortunately we have to declare them first, then 4238 // define them out-of-line. 4239#define ABSTRACT_TYPELOC(CLASS, PARENT) 4240#define TYPELOC(CLASS, PARENT) \ 4241 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 4242#include "clang/AST/TypeLocNodes.def" 4243 4244 void VisitFunctionTypeLoc(FunctionTypeLoc); 4245 void VisitArrayTypeLoc(ArrayTypeLoc); 4246}; 4247 4248void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 4249 // nothing to do 4250} 4251void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 4252 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx)); 4253 if (TL.needsExtraLocalData()) { 4254 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++])); 4255 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++])); 4256 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++])); 4257 TL.setModeAttr(Record[Idx++]); 4258 } 4259} 4260void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { 4261 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4262} 4263void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { 4264 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 4265} 4266void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 4267 TL.setCaretLoc(ReadSourceLocation(Record, Idx)); 4268} 4269void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 4270 TL.setAmpLoc(ReadSourceLocation(Record, Idx)); 4271} 4272void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 4273 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx)); 4274} 4275void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 4276 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 4277 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx)); 4278} 4279void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { 4280 TL.setLBracketLoc(ReadSourceLocation(Record, Idx)); 4281 TL.setRBracketLoc(ReadSourceLocation(Record, Idx)); 4282 if (Record[Idx++]) 4283 TL.setSizeExpr(Reader.ReadExpr(F)); 4284 else 4285 TL.setSizeExpr(0); 4286} 4287void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 4288 VisitArrayTypeLoc(TL); 4289} 4290void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 4291 VisitArrayTypeLoc(TL); 4292} 4293void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 4294 VisitArrayTypeLoc(TL); 4295} 4296void TypeLocReader::VisitDependentSizedArrayTypeLoc( 4297 DependentSizedArrayTypeLoc TL) { 4298 VisitArrayTypeLoc(TL); 4299} 4300void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( 4301 DependentSizedExtVectorTypeLoc TL) { 4302 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4303} 4304void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { 4305 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4306} 4307void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 4308 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4309} 4310void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 4311 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx)); 4312 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 4313 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 4314 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx)); 4315 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) { 4316 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx)); 4317 } 4318} 4319void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 4320 VisitFunctionTypeLoc(TL); 4321} 4322void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 4323 VisitFunctionTypeLoc(TL); 4324} 4325void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 4326 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4327} 4328void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 4329 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4330} 4331void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 4332 TL.setTypeofLoc(ReadSourceLocation(Record, Idx)); 4333 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 4334 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 4335} 4336void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 4337 TL.setTypeofLoc(ReadSourceLocation(Record, Idx)); 4338 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 4339 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 4340 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx)); 4341} 4342void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 4343 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4344} 4345void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { 4346 TL.setKWLoc(ReadSourceLocation(Record, Idx)); 4347 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 4348 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 4349 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx)); 4350} 4351void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) { 4352 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4353} 4354void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { 4355 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4356} 4357void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { 4358 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4359} 4360void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 4361 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx)); 4362 if (TL.hasAttrOperand()) { 4363 SourceRange range; 4364 range.setBegin(ReadSourceLocation(Record, Idx)); 4365 range.setEnd(ReadSourceLocation(Record, Idx)); 4366 TL.setAttrOperandParensRange(range); 4367 } 4368 if (TL.hasAttrExprOperand()) { 4369 if (Record[Idx++]) 4370 TL.setAttrExprOperand(Reader.ReadExpr(F)); 4371 else 4372 TL.setAttrExprOperand(0); 4373 } else if (TL.hasAttrEnumOperand()) 4374 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx)); 4375} 4376void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 4377 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4378} 4379void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( 4380 SubstTemplateTypeParmTypeLoc TL) { 4381 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4382} 4383void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc( 4384 SubstTemplateTypeParmPackTypeLoc TL) { 4385 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4386} 4387void TypeLocReader::VisitTemplateSpecializationTypeLoc( 4388 TemplateSpecializationTypeLoc TL) { 4389 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx)); 4390 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx)); 4391 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 4392 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 4393 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 4394 TL.setArgLocInfo(i, 4395 Reader.GetTemplateArgumentLocInfo(F, 4396 TL.getTypePtr()->getArg(i).getKind(), 4397 Record, Idx)); 4398} 4399void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) { 4400 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 4401 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 4402} 4403void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 4404 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx)); 4405 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx)); 4406} 4407void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 4408 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4409} 4410void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 4411 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx)); 4412 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx)); 4413 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4414} 4415void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( 4416 DependentTemplateSpecializationTypeLoc TL) { 4417 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx)); 4418 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx)); 4419 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx)); 4420 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx)); 4421 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 4422 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 4423 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 4424 TL.setArgLocInfo(I, 4425 Reader.GetTemplateArgumentLocInfo(F, 4426 TL.getTypePtr()->getArg(I).getKind(), 4427 Record, Idx)); 4428} 4429void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 4430 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx)); 4431} 4432void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 4433 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4434} 4435void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 4436 TL.setHasBaseTypeAsWritten(Record[Idx++]); 4437 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 4438 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 4439 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 4440 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx)); 4441} 4442void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 4443 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 4444} 4445void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) { 4446 TL.setKWLoc(ReadSourceLocation(Record, Idx)); 4447 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 4448 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 4449} 4450 4451TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F, 4452 const RecordData &Record, 4453 unsigned &Idx) { 4454 QualType InfoTy = readType(F, Record, Idx); 4455 if (InfoTy.isNull()) 4456 return 0; 4457 4458 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy); 4459 TypeLocReader TLR(*this, F, Record, Idx); 4460 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc()) 4461 TLR.Visit(TL); 4462 return TInfo; 4463} 4464 4465QualType ASTReader::GetType(TypeID ID) { 4466 unsigned FastQuals = ID & Qualifiers::FastMask; 4467 unsigned Index = ID >> Qualifiers::FastWidth; 4468 4469 if (Index < NUM_PREDEF_TYPE_IDS) { 4470 QualType T; 4471 switch ((PredefinedTypeIDs)Index) { 4472 case PREDEF_TYPE_NULL_ID: return QualType(); 4473 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break; 4474 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break; 4475 4476 case PREDEF_TYPE_CHAR_U_ID: 4477 case PREDEF_TYPE_CHAR_S_ID: 4478 // FIXME: Check that the signedness of CharTy is correct! 4479 T = Context.CharTy; 4480 break; 4481 4482 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break; 4483 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break; 4484 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break; 4485 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break; 4486 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break; 4487 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break; 4488 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break; 4489 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break; 4490 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break; 4491 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break; 4492 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break; 4493 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break; 4494 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break; 4495 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break; 4496 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break; 4497 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break; 4498 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break; 4499 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break; 4500 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break; 4501 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break; 4502 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break; 4503 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break; 4504 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break; 4505 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break; 4506 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break; 4507 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break; 4508 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break; 4509 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break; 4510 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break; 4511 4512 case PREDEF_TYPE_AUTO_RREF_DEDUCT: 4513 T = Context.getAutoRRefDeductType(); 4514 break; 4515 4516 case PREDEF_TYPE_ARC_UNBRIDGED_CAST: 4517 T = Context.ARCUnbridgedCastTy; 4518 break; 4519 4520 case PREDEF_TYPE_VA_LIST_TAG: 4521 T = Context.getVaListTagType(); 4522 break; 4523 4524 case PREDEF_TYPE_BUILTIN_FN: 4525 T = Context.BuiltinFnTy; 4526 break; 4527 } 4528 4529 assert(!T.isNull() && "Unknown predefined type"); 4530 return T.withFastQualifiers(FastQuals); 4531 } 4532 4533 Index -= NUM_PREDEF_TYPE_IDS; 4534 assert(Index < TypesLoaded.size() && "Type index out-of-range"); 4535 if (TypesLoaded[Index].isNull()) { 4536 TypesLoaded[Index] = readTypeRecord(Index); 4537 if (TypesLoaded[Index].isNull()) 4538 return QualType(); 4539 4540 TypesLoaded[Index]->setFromAST(); 4541 if (DeserializationListener) 4542 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID), 4543 TypesLoaded[Index]); 4544 } 4545 4546 return TypesLoaded[Index].withFastQualifiers(FastQuals); 4547} 4548 4549QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) { 4550 return GetType(getGlobalTypeID(F, LocalID)); 4551} 4552 4553serialization::TypeID 4554ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const { 4555 unsigned FastQuals = LocalID & Qualifiers::FastMask; 4556 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth; 4557 4558 if (LocalIndex < NUM_PREDEF_TYPE_IDS) 4559 return LocalID; 4560 4561 ContinuousRangeMap<uint32_t, int, 2>::iterator I 4562 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS); 4563 assert(I != F.TypeRemap.end() && "Invalid index into type index remap"); 4564 4565 unsigned GlobalIndex = LocalIndex + I->second; 4566 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals; 4567} 4568 4569TemplateArgumentLocInfo 4570ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F, 4571 TemplateArgument::ArgKind Kind, 4572 const RecordData &Record, 4573 unsigned &Index) { 4574 switch (Kind) { 4575 case TemplateArgument::Expression: 4576 return ReadExpr(F); 4577 case TemplateArgument::Type: 4578 return GetTypeSourceInfo(F, Record, Index); 4579 case TemplateArgument::Template: { 4580 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, 4581 Index); 4582 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index); 4583 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 4584 SourceLocation()); 4585 } 4586 case TemplateArgument::TemplateExpansion: { 4587 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, 4588 Index); 4589 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index); 4590 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index); 4591 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 4592 EllipsisLoc); 4593 } 4594 case TemplateArgument::Null: 4595 case TemplateArgument::Integral: 4596 case TemplateArgument::Declaration: 4597 case TemplateArgument::NullPtr: 4598 case TemplateArgument::Pack: 4599 // FIXME: Is this right? 4600 return TemplateArgumentLocInfo(); 4601 } 4602 llvm_unreachable("unexpected template argument loc"); 4603} 4604 4605TemplateArgumentLoc 4606ASTReader::ReadTemplateArgumentLoc(ModuleFile &F, 4607 const RecordData &Record, unsigned &Index) { 4608 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index); 4609 4610 if (Arg.getKind() == TemplateArgument::Expression) { 4611 if (Record[Index++]) // bool InfoHasSameExpr. 4612 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); 4613 } 4614 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(), 4615 Record, Index)); 4616} 4617 4618Decl *ASTReader::GetExternalDecl(uint32_t ID) { 4619 return GetDecl(ID); 4620} 4621 4622uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record, 4623 unsigned &Idx){ 4624 if (Idx >= Record.size()) 4625 return 0; 4626 4627 unsigned LocalID = Record[Idx++]; 4628 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]); 4629} 4630 4631CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) { 4632 RecordLocation Loc = getLocalBitOffset(Offset); 4633 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor; 4634 SavedStreamPosition SavedPosition(Cursor); 4635 Cursor.JumpToBit(Loc.Offset); 4636 ReadingKindTracker ReadingKind(Read_Decl, *this); 4637 RecordData Record; 4638 unsigned Code = Cursor.ReadCode(); 4639 unsigned RecCode = Cursor.ReadRecord(Code, Record); 4640 if (RecCode != DECL_CXX_BASE_SPECIFIERS) { 4641 Error("Malformed AST file: missing C++ base specifiers"); 4642 return 0; 4643 } 4644 4645 unsigned Idx = 0; 4646 unsigned NumBases = Record[Idx++]; 4647 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases); 4648 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases]; 4649 for (unsigned I = 0; I != NumBases; ++I) 4650 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx); 4651 return Bases; 4652} 4653 4654serialization::DeclID 4655ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const { 4656 if (LocalID < NUM_PREDEF_DECL_IDS) 4657 return LocalID; 4658 4659 ContinuousRangeMap<uint32_t, int, 2>::iterator I 4660 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS); 4661 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap"); 4662 4663 return LocalID + I->second; 4664} 4665 4666bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID, 4667 ModuleFile &M) const { 4668 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID); 4669 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 4670 return &M == I->second; 4671} 4672 4673ModuleFile *ASTReader::getOwningModuleFile(Decl *D) { 4674 if (!D->isFromASTFile()) 4675 return 0; 4676 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID()); 4677 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 4678 return I->second; 4679} 4680 4681SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) { 4682 if (ID < NUM_PREDEF_DECL_IDS) 4683 return SourceLocation(); 4684 4685 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 4686 4687 if (Index > DeclsLoaded.size()) { 4688 Error("declaration ID out-of-range for AST file"); 4689 return SourceLocation(); 4690 } 4691 4692 if (Decl *D = DeclsLoaded[Index]) 4693 return D->getLocation(); 4694 4695 unsigned RawLocation = 0; 4696 RecordLocation Rec = DeclCursorForID(ID, RawLocation); 4697 return ReadSourceLocation(*Rec.F, RawLocation); 4698} 4699 4700Decl *ASTReader::GetDecl(DeclID ID) { 4701 if (ID < NUM_PREDEF_DECL_IDS) { 4702 switch ((PredefinedDeclIDs)ID) { 4703 case PREDEF_DECL_NULL_ID: 4704 return 0; 4705 4706 case PREDEF_DECL_TRANSLATION_UNIT_ID: 4707 return Context.getTranslationUnitDecl(); 4708 4709 case PREDEF_DECL_OBJC_ID_ID: 4710 return Context.getObjCIdDecl(); 4711 4712 case PREDEF_DECL_OBJC_SEL_ID: 4713 return Context.getObjCSelDecl(); 4714 4715 case PREDEF_DECL_OBJC_CLASS_ID: 4716 return Context.getObjCClassDecl(); 4717 4718 case PREDEF_DECL_OBJC_PROTOCOL_ID: 4719 return Context.getObjCProtocolDecl(); 4720 4721 case PREDEF_DECL_INT_128_ID: 4722 return Context.getInt128Decl(); 4723 4724 case PREDEF_DECL_UNSIGNED_INT_128_ID: 4725 return Context.getUInt128Decl(); 4726 4727 case PREDEF_DECL_OBJC_INSTANCETYPE_ID: 4728 return Context.getObjCInstanceTypeDecl(); 4729 4730 case PREDEF_DECL_BUILTIN_VA_LIST_ID: 4731 return Context.getBuiltinVaListDecl(); 4732 } 4733 } 4734 4735 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 4736 4737 if (Index >= DeclsLoaded.size()) { 4738 assert(0 && "declaration ID out-of-range for AST file"); 4739 Error("declaration ID out-of-range for AST file"); 4740 return 0; 4741 } 4742 4743 if (!DeclsLoaded[Index]) { 4744 ReadDeclRecord(ID); 4745 if (DeserializationListener) 4746 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); 4747 } 4748 4749 return DeclsLoaded[Index]; 4750} 4751 4752DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M, 4753 DeclID GlobalID) { 4754 if (GlobalID < NUM_PREDEF_DECL_IDS) 4755 return GlobalID; 4756 4757 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID); 4758 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 4759 ModuleFile *Owner = I->second; 4760 4761 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos 4762 = M.GlobalToLocalDeclIDs.find(Owner); 4763 if (Pos == M.GlobalToLocalDeclIDs.end()) 4764 return 0; 4765 4766 return GlobalID - Owner->BaseDeclID + Pos->second; 4767} 4768 4769serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F, 4770 const RecordData &Record, 4771 unsigned &Idx) { 4772 if (Idx >= Record.size()) { 4773 Error("Corrupted AST file"); 4774 return 0; 4775 } 4776 4777 return getGlobalDeclID(F, Record[Idx++]); 4778} 4779 4780/// \brief Resolve the offset of a statement into a statement. 4781/// 4782/// This operation will read a new statement from the external 4783/// source each time it is called, and is meant to be used via a 4784/// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 4785Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) { 4786 // Switch case IDs are per Decl. 4787 ClearSwitchCaseIDs(); 4788 4789 // Offset here is a global offset across the entire chain. 4790 RecordLocation Loc = getLocalBitOffset(Offset); 4791 Loc.F->DeclsCursor.JumpToBit(Loc.Offset); 4792 return ReadStmtFromStream(*Loc.F); 4793} 4794 4795namespace { 4796 class FindExternalLexicalDeclsVisitor { 4797 ASTReader &Reader; 4798 const DeclContext *DC; 4799 bool (*isKindWeWant)(Decl::Kind); 4800 4801 SmallVectorImpl<Decl*> &Decls; 4802 bool PredefsVisited[NUM_PREDEF_DECL_IDS]; 4803 4804 public: 4805 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC, 4806 bool (*isKindWeWant)(Decl::Kind), 4807 SmallVectorImpl<Decl*> &Decls) 4808 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls) 4809 { 4810 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I) 4811 PredefsVisited[I] = false; 4812 } 4813 4814 static bool visit(ModuleFile &M, bool Preorder, void *UserData) { 4815 if (Preorder) 4816 return false; 4817 4818 FindExternalLexicalDeclsVisitor *This 4819 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData); 4820 4821 ModuleFile::DeclContextInfosMap::iterator Info 4822 = M.DeclContextInfos.find(This->DC); 4823 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls) 4824 return false; 4825 4826 // Load all of the declaration IDs 4827 for (const KindDeclIDPair *ID = Info->second.LexicalDecls, 4828 *IDE = ID + Info->second.NumLexicalDecls; 4829 ID != IDE; ++ID) { 4830 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first)) 4831 continue; 4832 4833 // Don't add predefined declarations to the lexical context more 4834 // than once. 4835 if (ID->second < NUM_PREDEF_DECL_IDS) { 4836 if (This->PredefsVisited[ID->second]) 4837 continue; 4838 4839 This->PredefsVisited[ID->second] = true; 4840 } 4841 4842 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) { 4843 if (!This->DC->isDeclInLexicalTraversal(D)) 4844 This->Decls.push_back(D); 4845 } 4846 } 4847 4848 return false; 4849 } 4850 }; 4851} 4852 4853ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC, 4854 bool (*isKindWeWant)(Decl::Kind), 4855 SmallVectorImpl<Decl*> &Decls) { 4856 // There might be lexical decls in multiple modules, for the TU at 4857 // least. Walk all of the modules in the order they were loaded. 4858 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls); 4859 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor); 4860 ++NumLexicalDeclContextsRead; 4861 return ELR_Success; 4862} 4863 4864namespace { 4865 4866class DeclIDComp { 4867 ASTReader &Reader; 4868 ModuleFile &Mod; 4869 4870public: 4871 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {} 4872 4873 bool operator()(LocalDeclID L, LocalDeclID R) const { 4874 SourceLocation LHS = getLocation(L); 4875 SourceLocation RHS = getLocation(R); 4876 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 4877 } 4878 4879 bool operator()(SourceLocation LHS, LocalDeclID R) const { 4880 SourceLocation RHS = getLocation(R); 4881 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 4882 } 4883 4884 bool operator()(LocalDeclID L, SourceLocation RHS) const { 4885 SourceLocation LHS = getLocation(L); 4886 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 4887 } 4888 4889 SourceLocation getLocation(LocalDeclID ID) const { 4890 return Reader.getSourceManager().getFileLoc( 4891 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID))); 4892 } 4893}; 4894 4895} 4896 4897void ASTReader::FindFileRegionDecls(FileID File, 4898 unsigned Offset, unsigned Length, 4899 SmallVectorImpl<Decl *> &Decls) { 4900 SourceManager &SM = getSourceManager(); 4901 4902 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File); 4903 if (I == FileDeclIDs.end()) 4904 return; 4905 4906 FileDeclsInfo &DInfo = I->second; 4907 if (DInfo.Decls.empty()) 4908 return; 4909 4910 SourceLocation 4911 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset); 4912 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length); 4913 4914 DeclIDComp DIDComp(*this, *DInfo.Mod); 4915 ArrayRef<serialization::LocalDeclID>::iterator 4916 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(), 4917 BeginLoc, DIDComp); 4918 if (BeginIt != DInfo.Decls.begin()) 4919 --BeginIt; 4920 4921 // If we are pointing at a top-level decl inside an objc container, we need 4922 // to backtrack until we find it otherwise we will fail to report that the 4923 // region overlaps with an objc container. 4924 while (BeginIt != DInfo.Decls.begin() && 4925 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt)) 4926 ->isTopLevelDeclInObjCContainer()) 4927 --BeginIt; 4928 4929 ArrayRef<serialization::LocalDeclID>::iterator 4930 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(), 4931 EndLoc, DIDComp); 4932 if (EndIt != DInfo.Decls.end()) 4933 ++EndIt; 4934 4935 for (ArrayRef<serialization::LocalDeclID>::iterator 4936 DIt = BeginIt; DIt != EndIt; ++DIt) 4937 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt))); 4938} 4939 4940namespace { 4941 /// \brief ModuleFile visitor used to perform name lookup into a 4942 /// declaration context. 4943 class DeclContextNameLookupVisitor { 4944 ASTReader &Reader; 4945 llvm::SmallVectorImpl<const DeclContext *> &Contexts; 4946 DeclarationName Name; 4947 SmallVectorImpl<NamedDecl *> &Decls; 4948 4949 public: 4950 DeclContextNameLookupVisitor(ASTReader &Reader, 4951 SmallVectorImpl<const DeclContext *> &Contexts, 4952 DeclarationName Name, 4953 SmallVectorImpl<NamedDecl *> &Decls) 4954 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { } 4955 4956 static bool visit(ModuleFile &M, void *UserData) { 4957 DeclContextNameLookupVisitor *This 4958 = static_cast<DeclContextNameLookupVisitor *>(UserData); 4959 4960 // Check whether we have any visible declaration information for 4961 // this context in this module. 4962 ModuleFile::DeclContextInfosMap::iterator Info; 4963 bool FoundInfo = false; 4964 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) { 4965 Info = M.DeclContextInfos.find(This->Contexts[I]); 4966 if (Info != M.DeclContextInfos.end() && 4967 Info->second.NameLookupTableData) { 4968 FoundInfo = true; 4969 break; 4970 } 4971 } 4972 4973 if (!FoundInfo) 4974 return false; 4975 4976 // Look for this name within this module. 4977 ASTDeclContextNameLookupTable *LookupTable = 4978 Info->second.NameLookupTableData; 4979 ASTDeclContextNameLookupTable::iterator Pos 4980 = LookupTable->find(This->Name); 4981 if (Pos == LookupTable->end()) 4982 return false; 4983 4984 bool FoundAnything = false; 4985 ASTDeclContextNameLookupTrait::data_type Data = *Pos; 4986 for (; Data.first != Data.second; ++Data.first) { 4987 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first); 4988 if (!ND) 4989 continue; 4990 4991 if (ND->getDeclName() != This->Name) { 4992 // A name might be null because the decl's redeclarable part is 4993 // currently read before reading its name. The lookup is triggered by 4994 // building that decl (likely indirectly), and so it is later in the 4995 // sense of "already existing" and can be ignored here. 4996 continue; 4997 } 4998 4999 // Record this declaration. 5000 FoundAnything = true; 5001 This->Decls.push_back(ND); 5002 } 5003 5004 return FoundAnything; 5005 } 5006 }; 5007} 5008 5009DeclContext::lookup_result 5010ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC, 5011 DeclarationName Name) { 5012 assert(DC->hasExternalVisibleStorage() && 5013 "DeclContext has no visible decls in storage"); 5014 if (!Name) 5015 return DeclContext::lookup_result(DeclContext::lookup_iterator(0), 5016 DeclContext::lookup_iterator(0)); 5017 5018 SmallVector<NamedDecl *, 64> Decls; 5019 5020 // Compute the declaration contexts we need to look into. Multiple such 5021 // declaration contexts occur when two declaration contexts from disjoint 5022 // modules get merged, e.g., when two namespaces with the same name are 5023 // independently defined in separate modules. 5024 SmallVector<const DeclContext *, 2> Contexts; 5025 Contexts.push_back(DC); 5026 5027 if (DC->isNamespace()) { 5028 MergedDeclsMap::iterator Merged 5029 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC))); 5030 if (Merged != MergedDecls.end()) { 5031 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I) 5032 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I]))); 5033 } 5034 } 5035 5036 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls); 5037 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor); 5038 ++NumVisibleDeclContextsRead; 5039 SetExternalVisibleDeclsForName(DC, Name, Decls); 5040 return const_cast<DeclContext*>(DC)->lookup(Name); 5041} 5042 5043namespace { 5044 /// \brief ModuleFile visitor used to retrieve all visible names in a 5045 /// declaration context. 5046 class DeclContextAllNamesVisitor { 5047 ASTReader &Reader; 5048 llvm::SmallVectorImpl<const DeclContext *> &Contexts; 5049 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls; 5050 5051 public: 5052 DeclContextAllNamesVisitor(ASTReader &Reader, 5053 SmallVectorImpl<const DeclContext *> &Contexts, 5054 llvm::DenseMap<DeclarationName, 5055 SmallVector<NamedDecl *, 8> > &Decls) 5056 : Reader(Reader), Contexts(Contexts), Decls(Decls) { } 5057 5058 static bool visit(ModuleFile &M, void *UserData) { 5059 DeclContextAllNamesVisitor *This 5060 = static_cast<DeclContextAllNamesVisitor *>(UserData); 5061 5062 // Check whether we have any visible declaration information for 5063 // this context in this module. 5064 ModuleFile::DeclContextInfosMap::iterator Info; 5065 bool FoundInfo = false; 5066 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) { 5067 Info = M.DeclContextInfos.find(This->Contexts[I]); 5068 if (Info != M.DeclContextInfos.end() && 5069 Info->second.NameLookupTableData) { 5070 FoundInfo = true; 5071 break; 5072 } 5073 } 5074 5075 if (!FoundInfo) 5076 return false; 5077 5078 ASTDeclContextNameLookupTable *LookupTable = 5079 Info->second.NameLookupTableData; 5080 bool FoundAnything = false; 5081 for (ASTDeclContextNameLookupTable::data_iterator 5082 I = LookupTable->data_begin(), E = LookupTable->data_end(); 5083 I != E; ++I) { 5084 ASTDeclContextNameLookupTrait::data_type Data = *I; 5085 for (; Data.first != Data.second; ++Data.first) { 5086 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, 5087 *Data.first); 5088 if (!ND) 5089 continue; 5090 5091 // Record this declaration. 5092 FoundAnything = true; 5093 This->Decls[ND->getDeclName()].push_back(ND); 5094 } 5095 } 5096 5097 return FoundAnything; 5098 } 5099 }; 5100} 5101 5102void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) { 5103 if (!DC->hasExternalVisibleStorage()) 5104 return; 5105 llvm::DenseMap<DeclarationName, llvm::SmallVector<NamedDecl*, 8> > Decls; 5106 5107 // Compute the declaration contexts we need to look into. Multiple such 5108 // declaration contexts occur when two declaration contexts from disjoint 5109 // modules get merged, e.g., when two namespaces with the same name are 5110 // independently defined in separate modules. 5111 SmallVector<const DeclContext *, 2> Contexts; 5112 Contexts.push_back(DC); 5113 5114 if (DC->isNamespace()) { 5115 MergedDeclsMap::iterator Merged 5116 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC))); 5117 if (Merged != MergedDecls.end()) { 5118 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I) 5119 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I]))); 5120 } 5121 } 5122 5123 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls); 5124 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor); 5125 ++NumVisibleDeclContextsRead; 5126 5127 for (llvm::DenseMap<DeclarationName, 5128 llvm::SmallVector<NamedDecl*, 8> >::iterator 5129 I = Decls.begin(), E = Decls.end(); I != E; ++I) { 5130 SetExternalVisibleDeclsForName(DC, I->first, I->second); 5131 } 5132 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false); 5133} 5134 5135/// \brief Under non-PCH compilation the consumer receives the objc methods 5136/// before receiving the implementation, and codegen depends on this. 5137/// We simulate this by deserializing and passing to consumer the methods of the 5138/// implementation before passing the deserialized implementation decl. 5139static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD, 5140 ASTConsumer *Consumer) { 5141 assert(ImplD && Consumer); 5142 5143 for (ObjCImplDecl::method_iterator 5144 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I) 5145 Consumer->HandleInterestingDecl(DeclGroupRef(*I)); 5146 5147 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD)); 5148} 5149 5150void ASTReader::PassInterestingDeclsToConsumer() { 5151 assert(Consumer); 5152 while (!InterestingDecls.empty()) { 5153 Decl *D = InterestingDecls.front(); 5154 InterestingDecls.pop_front(); 5155 5156 PassInterestingDeclToConsumer(D); 5157 } 5158} 5159 5160void ASTReader::PassInterestingDeclToConsumer(Decl *D) { 5161 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) 5162 PassObjCImplDeclToConsumer(ImplD, Consumer); 5163 else 5164 Consumer->HandleInterestingDecl(DeclGroupRef(D)); 5165} 5166 5167void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) { 5168 this->Consumer = Consumer; 5169 5170 if (!Consumer) 5171 return; 5172 5173 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) { 5174 // Force deserialization of this decl, which will cause it to be queued for 5175 // passing to the consumer. 5176 GetDecl(ExternalDefinitions[I]); 5177 } 5178 ExternalDefinitions.clear(); 5179 5180 PassInterestingDeclsToConsumer(); 5181} 5182 5183void ASTReader::PrintStats() { 5184 std::fprintf(stderr, "*** AST File Statistics:\n"); 5185 5186 unsigned NumTypesLoaded 5187 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), 5188 QualType()); 5189 unsigned NumDeclsLoaded 5190 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), 5191 (Decl *)0); 5192 unsigned NumIdentifiersLoaded 5193 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), 5194 IdentifiersLoaded.end(), 5195 (IdentifierInfo *)0); 5196 unsigned NumSelectorsLoaded 5197 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), 5198 SelectorsLoaded.end(), 5199 Selector()); 5200 5201 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits); 5202 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses); 5203 if (unsigned TotalNumSLocEntries = getTotalNumSLocs()) 5204 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", 5205 NumSLocEntriesRead, TotalNumSLocEntries, 5206 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); 5207 if (!TypesLoaded.empty()) 5208 std::fprintf(stderr, " %u/%u types read (%f%%)\n", 5209 NumTypesLoaded, (unsigned)TypesLoaded.size(), 5210 ((float)NumTypesLoaded/TypesLoaded.size() * 100)); 5211 if (!DeclsLoaded.empty()) 5212 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", 5213 NumDeclsLoaded, (unsigned)DeclsLoaded.size(), 5214 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); 5215 if (!IdentifiersLoaded.empty()) 5216 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", 5217 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), 5218 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); 5219 if (!SelectorsLoaded.empty()) 5220 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", 5221 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(), 5222 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100)); 5223 if (TotalNumStatements) 5224 std::fprintf(stderr, " %u/%u statements read (%f%%)\n", 5225 NumStatementsRead, TotalNumStatements, 5226 ((float)NumStatementsRead/TotalNumStatements * 100)); 5227 if (TotalNumMacros) 5228 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 5229 NumMacrosRead, TotalNumMacros, 5230 ((float)NumMacrosRead/TotalNumMacros * 100)); 5231 if (TotalLexicalDeclContexts) 5232 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", 5233 NumLexicalDeclContextsRead, TotalLexicalDeclContexts, 5234 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts 5235 * 100)); 5236 if (TotalVisibleDeclContexts) 5237 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", 5238 NumVisibleDeclContextsRead, TotalVisibleDeclContexts, 5239 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts 5240 * 100)); 5241 if (TotalNumMethodPoolEntries) { 5242 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", 5243 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries, 5244 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries 5245 * 100)); 5246 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses); 5247 } 5248 std::fprintf(stderr, "\n"); 5249 dump(); 5250 std::fprintf(stderr, "\n"); 5251} 5252 5253template<typename Key, typename ModuleFile, unsigned InitialCapacity> 5254static void 5255dumpModuleIDMap(StringRef Name, 5256 const ContinuousRangeMap<Key, ModuleFile *, 5257 InitialCapacity> &Map) { 5258 if (Map.begin() == Map.end()) 5259 return; 5260 5261 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType; 5262 llvm::errs() << Name << ":\n"; 5263 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end(); 5264 I != IEnd; ++I) { 5265 llvm::errs() << " " << I->first << " -> " << I->second->FileName 5266 << "\n"; 5267 } 5268} 5269 5270void ASTReader::dump() { 5271 llvm::errs() << "*** PCH/ModuleFile Remappings:\n"; 5272 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap); 5273 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap); 5274 dumpModuleIDMap("Global type map", GlobalTypeMap); 5275 dumpModuleIDMap("Global declaration map", GlobalDeclMap); 5276 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap); 5277 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap); 5278 dumpModuleIDMap("Global selector map", GlobalSelectorMap); 5279 dumpModuleIDMap("Global preprocessed entity map", 5280 GlobalPreprocessedEntityMap); 5281 5282 llvm::errs() << "\n*** PCH/Modules Loaded:"; 5283 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(), 5284 MEnd = ModuleMgr.end(); 5285 M != MEnd; ++M) 5286 (*M)->dump(); 5287} 5288 5289/// Return the amount of memory used by memory buffers, breaking down 5290/// by heap-backed versus mmap'ed memory. 5291void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const { 5292 for (ModuleConstIterator I = ModuleMgr.begin(), 5293 E = ModuleMgr.end(); I != E; ++I) { 5294 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) { 5295 size_t bytes = buf->getBufferSize(); 5296 switch (buf->getBufferKind()) { 5297 case llvm::MemoryBuffer::MemoryBuffer_Malloc: 5298 sizes.malloc_bytes += bytes; 5299 break; 5300 case llvm::MemoryBuffer::MemoryBuffer_MMap: 5301 sizes.mmap_bytes += bytes; 5302 break; 5303 } 5304 } 5305 } 5306} 5307 5308void ASTReader::InitializeSema(Sema &S) { 5309 SemaObj = &S; 5310 S.ExternalSource = this; 5311 5312 // Makes sure any declarations that were deserialized "too early" 5313 // still get added to the identifier's declaration chains. 5314 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) { 5315 SemaObj->pushExternalDeclIntoScope(PreloadedDecls[I], 5316 PreloadedDecls[I]->getDeclName()); 5317 } 5318 PreloadedDecls.clear(); 5319 5320 // Load the offsets of the declarations that Sema references. 5321 // They will be lazily deserialized when needed. 5322 if (!SemaDeclRefs.empty()) { 5323 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!"); 5324 if (!SemaObj->StdNamespace) 5325 SemaObj->StdNamespace = SemaDeclRefs[0]; 5326 if (!SemaObj->StdBadAlloc) 5327 SemaObj->StdBadAlloc = SemaDeclRefs[1]; 5328 } 5329 5330 if (!FPPragmaOptions.empty()) { 5331 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS"); 5332 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0]; 5333 } 5334 5335 if (!OpenCLExtensions.empty()) { 5336 unsigned I = 0; 5337#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++]; 5338#include "clang/Basic/OpenCLExtensions.def" 5339 5340 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS"); 5341 } 5342} 5343 5344IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) { 5345 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart), 5346 /*PriorGeneration=*/0); 5347 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor); 5348 IdentifierInfo *II = Visitor.getIdentifierInfo(); 5349 markIdentifierUpToDate(II); 5350 return II; 5351} 5352 5353namespace clang { 5354 /// \brief An identifier-lookup iterator that enumerates all of the 5355 /// identifiers stored within a set of AST files. 5356 class ASTIdentifierIterator : public IdentifierIterator { 5357 /// \brief The AST reader whose identifiers are being enumerated. 5358 const ASTReader &Reader; 5359 5360 /// \brief The current index into the chain of AST files stored in 5361 /// the AST reader. 5362 unsigned Index; 5363 5364 /// \brief The current position within the identifier lookup table 5365 /// of the current AST file. 5366 ASTIdentifierLookupTable::key_iterator Current; 5367 5368 /// \brief The end position within the identifier lookup table of 5369 /// the current AST file. 5370 ASTIdentifierLookupTable::key_iterator End; 5371 5372 public: 5373 explicit ASTIdentifierIterator(const ASTReader &Reader); 5374 5375 virtual StringRef Next(); 5376 }; 5377} 5378 5379ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader) 5380 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) { 5381 ASTIdentifierLookupTable *IdTable 5382 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable; 5383 Current = IdTable->key_begin(); 5384 End = IdTable->key_end(); 5385} 5386 5387StringRef ASTIdentifierIterator::Next() { 5388 while (Current == End) { 5389 // If we have exhausted all of our AST files, we're done. 5390 if (Index == 0) 5391 return StringRef(); 5392 5393 --Index; 5394 ASTIdentifierLookupTable *IdTable 5395 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index]. 5396 IdentifierLookupTable; 5397 Current = IdTable->key_begin(); 5398 End = IdTable->key_end(); 5399 } 5400 5401 // We have any identifiers remaining in the current AST file; return 5402 // the next one. 5403 std::pair<const char*, unsigned> Key = *Current; 5404 ++Current; 5405 return StringRef(Key.first, Key.second); 5406} 5407 5408IdentifierIterator *ASTReader::getIdentifiers() const { 5409 return new ASTIdentifierIterator(*this); 5410} 5411 5412namespace clang { namespace serialization { 5413 class ReadMethodPoolVisitor { 5414 ASTReader &Reader; 5415 Selector Sel; 5416 unsigned PriorGeneration; 5417 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods; 5418 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods; 5419 5420 public: 5421 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel, 5422 unsigned PriorGeneration) 5423 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) { } 5424 5425 static bool visit(ModuleFile &M, void *UserData) { 5426 ReadMethodPoolVisitor *This 5427 = static_cast<ReadMethodPoolVisitor *>(UserData); 5428 5429 if (!M.SelectorLookupTable) 5430 return false; 5431 5432 // If we've already searched this module file, skip it now. 5433 if (M.Generation <= This->PriorGeneration) 5434 return true; 5435 5436 ASTSelectorLookupTable *PoolTable 5437 = (ASTSelectorLookupTable*)M.SelectorLookupTable; 5438 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel); 5439 if (Pos == PoolTable->end()) 5440 return false; 5441 5442 ++This->Reader.NumSelectorsRead; 5443 // FIXME: Not quite happy with the statistics here. We probably should 5444 // disable this tracking when called via LoadSelector. 5445 // Also, should entries without methods count as misses? 5446 ++This->Reader.NumMethodPoolEntriesRead; 5447 ASTSelectorLookupTrait::data_type Data = *Pos; 5448 if (This->Reader.DeserializationListener) 5449 This->Reader.DeserializationListener->SelectorRead(Data.ID, 5450 This->Sel); 5451 5452 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end()); 5453 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end()); 5454 return true; 5455 } 5456 5457 /// \brief Retrieve the instance methods found by this visitor. 5458 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const { 5459 return InstanceMethods; 5460 } 5461 5462 /// \brief Retrieve the instance methods found by this visitor. 5463 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const { 5464 return FactoryMethods; 5465 } 5466 }; 5467} } // end namespace clang::serialization 5468 5469/// \brief Add the given set of methods to the method list. 5470static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods, 5471 ObjCMethodList &List) { 5472 for (unsigned I = 0, N = Methods.size(); I != N; ++I) { 5473 S.addMethodToGlobalList(&List, Methods[I]); 5474 } 5475} 5476 5477void ASTReader::ReadMethodPool(Selector Sel) { 5478 // Get the selector generation and update it to the current generation. 5479 unsigned &Generation = SelectorGeneration[Sel]; 5480 unsigned PriorGeneration = Generation; 5481 Generation = CurrentGeneration; 5482 5483 // Search for methods defined with this selector. 5484 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration); 5485 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor); 5486 5487 if (Visitor.getInstanceMethods().empty() && 5488 Visitor.getFactoryMethods().empty()) { 5489 ++NumMethodPoolMisses; 5490 return; 5491 } 5492 5493 if (!getSema()) 5494 return; 5495 5496 Sema &S = *getSema(); 5497 Sema::GlobalMethodPool::iterator Pos 5498 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first; 5499 5500 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first); 5501 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second); 5502} 5503 5504void ASTReader::ReadKnownNamespaces( 5505 SmallVectorImpl<NamespaceDecl *> &Namespaces) { 5506 Namespaces.clear(); 5507 5508 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) { 5509 if (NamespaceDecl *Namespace 5510 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I]))) 5511 Namespaces.push_back(Namespace); 5512 } 5513} 5514 5515void ASTReader::ReadTentativeDefinitions( 5516 SmallVectorImpl<VarDecl *> &TentativeDefs) { 5517 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { 5518 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I])); 5519 if (Var) 5520 TentativeDefs.push_back(Var); 5521 } 5522 TentativeDefinitions.clear(); 5523} 5524 5525void ASTReader::ReadUnusedFileScopedDecls( 5526 SmallVectorImpl<const DeclaratorDecl *> &Decls) { 5527 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) { 5528 DeclaratorDecl *D 5529 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I])); 5530 if (D) 5531 Decls.push_back(D); 5532 } 5533 UnusedFileScopedDecls.clear(); 5534} 5535 5536void ASTReader::ReadDelegatingConstructors( 5537 SmallVectorImpl<CXXConstructorDecl *> &Decls) { 5538 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) { 5539 CXXConstructorDecl *D 5540 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I])); 5541 if (D) 5542 Decls.push_back(D); 5543 } 5544 DelegatingCtorDecls.clear(); 5545} 5546 5547void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) { 5548 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) { 5549 TypedefNameDecl *D 5550 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])); 5551 if (D) 5552 Decls.push_back(D); 5553 } 5554 ExtVectorDecls.clear(); 5555} 5556 5557void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) { 5558 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) { 5559 CXXRecordDecl *D 5560 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I])); 5561 if (D) 5562 Decls.push_back(D); 5563 } 5564 DynamicClasses.clear(); 5565} 5566 5567void 5568ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) { 5569 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) { 5570 NamedDecl *D 5571 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I])); 5572 if (D) 5573 Decls.push_back(D); 5574 } 5575 LocallyScopedExternalDecls.clear(); 5576} 5577 5578void ASTReader::ReadReferencedSelectors( 5579 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) { 5580 if (ReferencedSelectorsData.empty()) 5581 return; 5582 5583 // If there are @selector references added them to its pool. This is for 5584 // implementation of -Wselector. 5585 unsigned int DataSize = ReferencedSelectorsData.size()-1; 5586 unsigned I = 0; 5587 while (I < DataSize) { 5588 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]); 5589 SourceLocation SelLoc 5590 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]); 5591 Sels.push_back(std::make_pair(Sel, SelLoc)); 5592 } 5593 ReferencedSelectorsData.clear(); 5594} 5595 5596void ASTReader::ReadWeakUndeclaredIdentifiers( 5597 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) { 5598 if (WeakUndeclaredIdentifiers.empty()) 5599 return; 5600 5601 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) { 5602 IdentifierInfo *WeakId 5603 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 5604 IdentifierInfo *AliasId 5605 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 5606 SourceLocation Loc 5607 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]); 5608 bool Used = WeakUndeclaredIdentifiers[I++]; 5609 WeakInfo WI(AliasId, Loc); 5610 WI.setUsed(Used); 5611 WeakIDs.push_back(std::make_pair(WeakId, WI)); 5612 } 5613 WeakUndeclaredIdentifiers.clear(); 5614} 5615 5616void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) { 5617 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) { 5618 ExternalVTableUse VT; 5619 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); 5620 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]); 5621 VT.DefinitionRequired = VTableUses[Idx++]; 5622 VTables.push_back(VT); 5623 } 5624 5625 VTableUses.clear(); 5626} 5627 5628void ASTReader::ReadPendingInstantiations( 5629 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) { 5630 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) { 5631 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++])); 5632 SourceLocation Loc 5633 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]); 5634 5635 Pending.push_back(std::make_pair(D, Loc)); 5636 } 5637 PendingInstantiations.clear(); 5638} 5639 5640void ASTReader::LoadSelector(Selector Sel) { 5641 // It would be complicated to avoid reading the methods anyway. So don't. 5642 ReadMethodPool(Sel); 5643} 5644 5645void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) { 5646 assert(ID && "Non-zero identifier ID required"); 5647 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); 5648 IdentifiersLoaded[ID - 1] = II; 5649 if (DeserializationListener) 5650 DeserializationListener->IdentifierRead(ID, II); 5651} 5652 5653/// \brief Set the globally-visible declarations associated with the given 5654/// identifier. 5655/// 5656/// If the AST reader is currently in a state where the given declaration IDs 5657/// cannot safely be resolved, they are queued until it is safe to resolve 5658/// them. 5659/// 5660/// \param II an IdentifierInfo that refers to one or more globally-visible 5661/// declarations. 5662/// 5663/// \param DeclIDs the set of declaration IDs with the name @p II that are 5664/// visible at global scope. 5665/// 5666/// \param Nonrecursive should be true to indicate that the caller knows that 5667/// this call is non-recursive, and therefore the globally-visible declarations 5668/// will not be placed onto the pending queue. 5669void 5670ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, 5671 const SmallVectorImpl<uint32_t> &DeclIDs, 5672 bool Nonrecursive) { 5673 if (NumCurrentElementsDeserializing && !Nonrecursive) { 5674 PendingIdentifierInfos.push_back(PendingIdentifierInfo()); 5675 PendingIdentifierInfo &PII = PendingIdentifierInfos.back(); 5676 PII.II = II; 5677 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end()); 5678 return; 5679 } 5680 5681 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { 5682 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); 5683 if (SemaObj) { 5684 // Introduce this declaration into the translation-unit scope 5685 // and add it to the declaration chain for this identifier, so 5686 // that (unqualified) name lookup will find it. 5687 SemaObj->pushExternalDeclIntoScope(D, II); 5688 } else { 5689 // Queue this declaration so that it will be added to the 5690 // translation unit scope and identifier's declaration chain 5691 // once a Sema object is known. 5692 PreloadedDecls.push_back(D); 5693 } 5694 } 5695} 5696 5697IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) { 5698 if (ID == 0) 5699 return 0; 5700 5701 if (IdentifiersLoaded.empty()) { 5702 Error("no identifier table in AST file"); 5703 return 0; 5704 } 5705 5706 ID -= 1; 5707 if (!IdentifiersLoaded[ID]) { 5708 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1); 5709 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map"); 5710 ModuleFile *M = I->second; 5711 unsigned Index = ID - M->BaseIdentifierID; 5712 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index]; 5713 5714 // All of the strings in the AST file are preceded by a 16-bit length. 5715 // Extract that 16-bit length to avoid having to execute strlen(). 5716 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as 5717 // unsigned integers. This is important to avoid integer overflow when 5718 // we cast them to 'unsigned'. 5719 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2; 5720 unsigned StrLen = (((unsigned) StrLenPtr[0]) 5721 | (((unsigned) StrLenPtr[1]) << 8)) - 1; 5722 IdentifiersLoaded[ID] 5723 = &PP.getIdentifierTable().get(StringRef(Str, StrLen)); 5724 if (DeserializationListener) 5725 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]); 5726 } 5727 5728 return IdentifiersLoaded[ID]; 5729} 5730 5731IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) { 5732 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID)); 5733} 5734 5735IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) { 5736 if (LocalID < NUM_PREDEF_IDENT_IDS) 5737 return LocalID; 5738 5739 ContinuousRangeMap<uint32_t, int, 2>::iterator I 5740 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS); 5741 assert(I != M.IdentifierRemap.end() 5742 && "Invalid index into identifier index remap"); 5743 5744 return LocalID + I->second; 5745} 5746 5747bool ASTReader::ReadSLocEntry(int ID) { 5748 return ReadSLocEntryRecord(ID) != Success; 5749} 5750 5751serialization::SubmoduleID 5752ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) { 5753 if (LocalID < NUM_PREDEF_SUBMODULE_IDS) 5754 return LocalID; 5755 5756 ContinuousRangeMap<uint32_t, int, 2>::iterator I 5757 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS); 5758 assert(I != M.SubmoduleRemap.end() 5759 && "Invalid index into identifier index remap"); 5760 5761 return LocalID + I->second; 5762} 5763 5764Module *ASTReader::getSubmodule(SubmoduleID GlobalID) { 5765 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) { 5766 assert(GlobalID == 0 && "Unhandled global submodule ID"); 5767 return 0; 5768 } 5769 5770 if (GlobalID > SubmodulesLoaded.size()) { 5771 Error("submodule ID out of range in AST file"); 5772 return 0; 5773 } 5774 5775 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS]; 5776} 5777 5778Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) { 5779 return DecodeSelector(getGlobalSelectorID(M, LocalID)); 5780} 5781 5782Selector ASTReader::DecodeSelector(serialization::SelectorID ID) { 5783 if (ID == 0) 5784 return Selector(); 5785 5786 if (ID > SelectorsLoaded.size()) { 5787 Error("selector ID out of range in AST file"); 5788 return Selector(); 5789 } 5790 5791 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) { 5792 // Load this selector from the selector table. 5793 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID); 5794 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map"); 5795 ModuleFile &M = *I->second; 5796 ASTSelectorLookupTrait Trait(*this, M); 5797 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS; 5798 SelectorsLoaded[ID - 1] = 5799 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0); 5800 if (DeserializationListener) 5801 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); 5802 } 5803 5804 return SelectorsLoaded[ID - 1]; 5805} 5806 5807Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) { 5808 return DecodeSelector(ID); 5809} 5810 5811uint32_t ASTReader::GetNumExternalSelectors() { 5812 // ID 0 (the null selector) is considered an external selector. 5813 return getTotalNumSelectors() + 1; 5814} 5815 5816serialization::SelectorID 5817ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const { 5818 if (LocalID < NUM_PREDEF_SELECTOR_IDS) 5819 return LocalID; 5820 5821 ContinuousRangeMap<uint32_t, int, 2>::iterator I 5822 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS); 5823 assert(I != M.SelectorRemap.end() 5824 && "Invalid index into identifier index remap"); 5825 5826 return LocalID + I->second; 5827} 5828 5829DeclarationName 5830ASTReader::ReadDeclarationName(ModuleFile &F, 5831 const RecordData &Record, unsigned &Idx) { 5832 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++]; 5833 switch (Kind) { 5834 case DeclarationName::Identifier: 5835 return DeclarationName(GetIdentifierInfo(F, Record, Idx)); 5836 5837 case DeclarationName::ObjCZeroArgSelector: 5838 case DeclarationName::ObjCOneArgSelector: 5839 case DeclarationName::ObjCMultiArgSelector: 5840 return DeclarationName(ReadSelector(F, Record, Idx)); 5841 5842 case DeclarationName::CXXConstructorName: 5843 return Context.DeclarationNames.getCXXConstructorName( 5844 Context.getCanonicalType(readType(F, Record, Idx))); 5845 5846 case DeclarationName::CXXDestructorName: 5847 return Context.DeclarationNames.getCXXDestructorName( 5848 Context.getCanonicalType(readType(F, Record, Idx))); 5849 5850 case DeclarationName::CXXConversionFunctionName: 5851 return Context.DeclarationNames.getCXXConversionFunctionName( 5852 Context.getCanonicalType(readType(F, Record, Idx))); 5853 5854 case DeclarationName::CXXOperatorName: 5855 return Context.DeclarationNames.getCXXOperatorName( 5856 (OverloadedOperatorKind)Record[Idx++]); 5857 5858 case DeclarationName::CXXLiteralOperatorName: 5859 return Context.DeclarationNames.getCXXLiteralOperatorName( 5860 GetIdentifierInfo(F, Record, Idx)); 5861 5862 case DeclarationName::CXXUsingDirective: 5863 return DeclarationName::getUsingDirectiveName(); 5864 } 5865 5866 llvm_unreachable("Invalid NameKind!"); 5867} 5868 5869void ASTReader::ReadDeclarationNameLoc(ModuleFile &F, 5870 DeclarationNameLoc &DNLoc, 5871 DeclarationName Name, 5872 const RecordData &Record, unsigned &Idx) { 5873 switch (Name.getNameKind()) { 5874 case DeclarationName::CXXConstructorName: 5875 case DeclarationName::CXXDestructorName: 5876 case DeclarationName::CXXConversionFunctionName: 5877 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx); 5878 break; 5879 5880 case DeclarationName::CXXOperatorName: 5881 DNLoc.CXXOperatorName.BeginOpNameLoc 5882 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 5883 DNLoc.CXXOperatorName.EndOpNameLoc 5884 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 5885 break; 5886 5887 case DeclarationName::CXXLiteralOperatorName: 5888 DNLoc.CXXLiteralOperatorName.OpNameLoc 5889 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 5890 break; 5891 5892 case DeclarationName::Identifier: 5893 case DeclarationName::ObjCZeroArgSelector: 5894 case DeclarationName::ObjCOneArgSelector: 5895 case DeclarationName::ObjCMultiArgSelector: 5896 case DeclarationName::CXXUsingDirective: 5897 break; 5898 } 5899} 5900 5901void ASTReader::ReadDeclarationNameInfo(ModuleFile &F, 5902 DeclarationNameInfo &NameInfo, 5903 const RecordData &Record, unsigned &Idx) { 5904 NameInfo.setName(ReadDeclarationName(F, Record, Idx)); 5905 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx)); 5906 DeclarationNameLoc DNLoc; 5907 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx); 5908 NameInfo.setInfo(DNLoc); 5909} 5910 5911void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info, 5912 const RecordData &Record, unsigned &Idx) { 5913 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx); 5914 unsigned NumTPLists = Record[Idx++]; 5915 Info.NumTemplParamLists = NumTPLists; 5916 if (NumTPLists) { 5917 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists]; 5918 for (unsigned i=0; i != NumTPLists; ++i) 5919 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx); 5920 } 5921} 5922 5923TemplateName 5924ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record, 5925 unsigned &Idx) { 5926 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++]; 5927 switch (Kind) { 5928 case TemplateName::Template: 5929 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx)); 5930 5931 case TemplateName::OverloadedTemplate: { 5932 unsigned size = Record[Idx++]; 5933 UnresolvedSet<8> Decls; 5934 while (size--) 5935 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx)); 5936 5937 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end()); 5938 } 5939 5940 case TemplateName::QualifiedTemplate: { 5941 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx); 5942 bool hasTemplKeyword = Record[Idx++]; 5943 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx); 5944 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template); 5945 } 5946 5947 case TemplateName::DependentTemplate: { 5948 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx); 5949 if (Record[Idx++]) // isIdentifier 5950 return Context.getDependentTemplateName(NNS, 5951 GetIdentifierInfo(F, Record, 5952 Idx)); 5953 return Context.getDependentTemplateName(NNS, 5954 (OverloadedOperatorKind)Record[Idx++]); 5955 } 5956 5957 case TemplateName::SubstTemplateTemplateParm: { 5958 TemplateTemplateParmDecl *param 5959 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx); 5960 if (!param) return TemplateName(); 5961 TemplateName replacement = ReadTemplateName(F, Record, Idx); 5962 return Context.getSubstTemplateTemplateParm(param, replacement); 5963 } 5964 5965 case TemplateName::SubstTemplateTemplateParmPack: { 5966 TemplateTemplateParmDecl *Param 5967 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx); 5968 if (!Param) 5969 return TemplateName(); 5970 5971 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx); 5972 if (ArgPack.getKind() != TemplateArgument::Pack) 5973 return TemplateName(); 5974 5975 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack); 5976 } 5977 } 5978 5979 llvm_unreachable("Unhandled template name kind!"); 5980} 5981 5982TemplateArgument 5983ASTReader::ReadTemplateArgument(ModuleFile &F, 5984 const RecordData &Record, unsigned &Idx) { 5985 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++]; 5986 switch (Kind) { 5987 case TemplateArgument::Null: 5988 return TemplateArgument(); 5989 case TemplateArgument::Type: 5990 return TemplateArgument(readType(F, Record, Idx)); 5991 case TemplateArgument::Declaration: { 5992 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx); 5993 bool ForReferenceParam = Record[Idx++]; 5994 return TemplateArgument(D, ForReferenceParam); 5995 } 5996 case TemplateArgument::NullPtr: 5997 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true); 5998 case TemplateArgument::Integral: { 5999 llvm::APSInt Value = ReadAPSInt(Record, Idx); 6000 QualType T = readType(F, Record, Idx); 6001 return TemplateArgument(Context, Value, T); 6002 } 6003 case TemplateArgument::Template: 6004 return TemplateArgument(ReadTemplateName(F, Record, Idx)); 6005 case TemplateArgument::TemplateExpansion: { 6006 TemplateName Name = ReadTemplateName(F, Record, Idx); 6007 llvm::Optional<unsigned> NumTemplateExpansions; 6008 if (unsigned NumExpansions = Record[Idx++]) 6009 NumTemplateExpansions = NumExpansions - 1; 6010 return TemplateArgument(Name, NumTemplateExpansions); 6011 } 6012 case TemplateArgument::Expression: 6013 return TemplateArgument(ReadExpr(F)); 6014 case TemplateArgument::Pack: { 6015 unsigned NumArgs = Record[Idx++]; 6016 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs]; 6017 for (unsigned I = 0; I != NumArgs; ++I) 6018 Args[I] = ReadTemplateArgument(F, Record, Idx); 6019 return TemplateArgument(Args, NumArgs); 6020 } 6021 } 6022 6023 llvm_unreachable("Unhandled template argument kind!"); 6024} 6025 6026TemplateParameterList * 6027ASTReader::ReadTemplateParameterList(ModuleFile &F, 6028 const RecordData &Record, unsigned &Idx) { 6029 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx); 6030 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx); 6031 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx); 6032 6033 unsigned NumParams = Record[Idx++]; 6034 SmallVector<NamedDecl *, 16> Params; 6035 Params.reserve(NumParams); 6036 while (NumParams--) 6037 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx)); 6038 6039 TemplateParameterList* TemplateParams = 6040 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, 6041 Params.data(), Params.size(), RAngleLoc); 6042 return TemplateParams; 6043} 6044 6045void 6046ASTReader:: 6047ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs, 6048 ModuleFile &F, const RecordData &Record, 6049 unsigned &Idx) { 6050 unsigned NumTemplateArgs = Record[Idx++]; 6051 TemplArgs.reserve(NumTemplateArgs); 6052 while (NumTemplateArgs--) 6053 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx)); 6054} 6055 6056/// \brief Read a UnresolvedSet structure. 6057void ASTReader::ReadUnresolvedSet(ModuleFile &F, UnresolvedSetImpl &Set, 6058 const RecordData &Record, unsigned &Idx) { 6059 unsigned NumDecls = Record[Idx++]; 6060 while (NumDecls--) { 6061 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx); 6062 AccessSpecifier AS = (AccessSpecifier)Record[Idx++]; 6063 Set.addDecl(D, AS); 6064 } 6065} 6066 6067CXXBaseSpecifier 6068ASTReader::ReadCXXBaseSpecifier(ModuleFile &F, 6069 const RecordData &Record, unsigned &Idx) { 6070 bool isVirtual = static_cast<bool>(Record[Idx++]); 6071 bool isBaseOfClass = static_cast<bool>(Record[Idx++]); 6072 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]); 6073 bool inheritConstructors = static_cast<bool>(Record[Idx++]); 6074 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx); 6075 SourceRange Range = ReadSourceRange(F, Record, Idx); 6076 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx); 6077 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo, 6078 EllipsisLoc); 6079 Result.setInheritConstructors(inheritConstructors); 6080 return Result; 6081} 6082 6083std::pair<CXXCtorInitializer **, unsigned> 6084ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record, 6085 unsigned &Idx) { 6086 CXXCtorInitializer **CtorInitializers = 0; 6087 unsigned NumInitializers = Record[Idx++]; 6088 if (NumInitializers) { 6089 CtorInitializers 6090 = new (Context) CXXCtorInitializer*[NumInitializers]; 6091 for (unsigned i=0; i != NumInitializers; ++i) { 6092 TypeSourceInfo *TInfo = 0; 6093 bool IsBaseVirtual = false; 6094 FieldDecl *Member = 0; 6095 IndirectFieldDecl *IndirectMember = 0; 6096 6097 CtorInitializerType Type = (CtorInitializerType)Record[Idx++]; 6098 switch (Type) { 6099 case CTOR_INITIALIZER_BASE: 6100 TInfo = GetTypeSourceInfo(F, Record, Idx); 6101 IsBaseVirtual = Record[Idx++]; 6102 break; 6103 6104 case CTOR_INITIALIZER_DELEGATING: 6105 TInfo = GetTypeSourceInfo(F, Record, Idx); 6106 break; 6107 6108 case CTOR_INITIALIZER_MEMBER: 6109 Member = ReadDeclAs<FieldDecl>(F, Record, Idx); 6110 break; 6111 6112 case CTOR_INITIALIZER_INDIRECT_MEMBER: 6113 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx); 6114 break; 6115 } 6116 6117 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx); 6118 Expr *Init = ReadExpr(F); 6119 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx); 6120 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx); 6121 bool IsWritten = Record[Idx++]; 6122 unsigned SourceOrderOrNumArrayIndices; 6123 SmallVector<VarDecl *, 8> Indices; 6124 if (IsWritten) { 6125 SourceOrderOrNumArrayIndices = Record[Idx++]; 6126 } else { 6127 SourceOrderOrNumArrayIndices = Record[Idx++]; 6128 Indices.reserve(SourceOrderOrNumArrayIndices); 6129 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i) 6130 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx)); 6131 } 6132 6133 CXXCtorInitializer *BOMInit; 6134 if (Type == CTOR_INITIALIZER_BASE) { 6135 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual, 6136 LParenLoc, Init, RParenLoc, 6137 MemberOrEllipsisLoc); 6138 } else if (Type == CTOR_INITIALIZER_DELEGATING) { 6139 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc, 6140 Init, RParenLoc); 6141 } else if (IsWritten) { 6142 if (Member) 6143 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, 6144 LParenLoc, Init, RParenLoc); 6145 else 6146 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember, 6147 MemberOrEllipsisLoc, LParenLoc, 6148 Init, RParenLoc); 6149 } else { 6150 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc, 6151 LParenLoc, Init, RParenLoc, 6152 Indices.data(), Indices.size()); 6153 } 6154 6155 if (IsWritten) 6156 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices); 6157 CtorInitializers[i] = BOMInit; 6158 } 6159 } 6160 6161 return std::make_pair(CtorInitializers, NumInitializers); 6162} 6163 6164NestedNameSpecifier * 6165ASTReader::ReadNestedNameSpecifier(ModuleFile &F, 6166 const RecordData &Record, unsigned &Idx) { 6167 unsigned N = Record[Idx++]; 6168 NestedNameSpecifier *NNS = 0, *Prev = 0; 6169 for (unsigned I = 0; I != N; ++I) { 6170 NestedNameSpecifier::SpecifierKind Kind 6171 = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; 6172 switch (Kind) { 6173 case NestedNameSpecifier::Identifier: { 6174 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx); 6175 NNS = NestedNameSpecifier::Create(Context, Prev, II); 6176 break; 6177 } 6178 6179 case NestedNameSpecifier::Namespace: { 6180 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx); 6181 NNS = NestedNameSpecifier::Create(Context, Prev, NS); 6182 break; 6183 } 6184 6185 case NestedNameSpecifier::NamespaceAlias: { 6186 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx); 6187 NNS = NestedNameSpecifier::Create(Context, Prev, Alias); 6188 break; 6189 } 6190 6191 case NestedNameSpecifier::TypeSpec: 6192 case NestedNameSpecifier::TypeSpecWithTemplate: { 6193 const Type *T = readType(F, Record, Idx).getTypePtrOrNull(); 6194 if (!T) 6195 return 0; 6196 6197 bool Template = Record[Idx++]; 6198 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T); 6199 break; 6200 } 6201 6202 case NestedNameSpecifier::Global: { 6203 NNS = NestedNameSpecifier::GlobalSpecifier(Context); 6204 // No associated value, and there can't be a prefix. 6205 break; 6206 } 6207 } 6208 Prev = NNS; 6209 } 6210 return NNS; 6211} 6212 6213NestedNameSpecifierLoc 6214ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record, 6215 unsigned &Idx) { 6216 unsigned N = Record[Idx++]; 6217 NestedNameSpecifierLocBuilder Builder; 6218 for (unsigned I = 0; I != N; ++I) { 6219 NestedNameSpecifier::SpecifierKind Kind 6220 = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; 6221 switch (Kind) { 6222 case NestedNameSpecifier::Identifier: { 6223 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx); 6224 SourceRange Range = ReadSourceRange(F, Record, Idx); 6225 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd()); 6226 break; 6227 } 6228 6229 case NestedNameSpecifier::Namespace: { 6230 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx); 6231 SourceRange Range = ReadSourceRange(F, Record, Idx); 6232 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd()); 6233 break; 6234 } 6235 6236 case NestedNameSpecifier::NamespaceAlias: { 6237 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx); 6238 SourceRange Range = ReadSourceRange(F, Record, Idx); 6239 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd()); 6240 break; 6241 } 6242 6243 case NestedNameSpecifier::TypeSpec: 6244 case NestedNameSpecifier::TypeSpecWithTemplate: { 6245 bool Template = Record[Idx++]; 6246 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx); 6247 if (!T) 6248 return NestedNameSpecifierLoc(); 6249 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx); 6250 6251 // FIXME: 'template' keyword location not saved anywhere, so we fake it. 6252 Builder.Extend(Context, 6253 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(), 6254 T->getTypeLoc(), ColonColonLoc); 6255 break; 6256 } 6257 6258 case NestedNameSpecifier::Global: { 6259 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx); 6260 Builder.MakeGlobal(Context, ColonColonLoc); 6261 break; 6262 } 6263 } 6264 } 6265 6266 return Builder.getWithLocInContext(Context); 6267} 6268 6269SourceRange 6270ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record, 6271 unsigned &Idx) { 6272 SourceLocation beg = ReadSourceLocation(F, Record, Idx); 6273 SourceLocation end = ReadSourceLocation(F, Record, Idx); 6274 return SourceRange(beg, end); 6275} 6276 6277/// \brief Read an integral value 6278llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) { 6279 unsigned BitWidth = Record[Idx++]; 6280 unsigned NumWords = llvm::APInt::getNumWords(BitWidth); 6281 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]); 6282 Idx += NumWords; 6283 return Result; 6284} 6285 6286/// \brief Read a signed integral value 6287llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) { 6288 bool isUnsigned = Record[Idx++]; 6289 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned); 6290} 6291 6292/// \brief Read a floating-point value 6293llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) { 6294 return llvm::APFloat(ReadAPInt(Record, Idx)); 6295} 6296 6297// \brief Read a string 6298std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) { 6299 unsigned Len = Record[Idx++]; 6300 std::string Result(Record.data() + Idx, Record.data() + Idx + Len); 6301 Idx += Len; 6302 return Result; 6303} 6304 6305VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record, 6306 unsigned &Idx) { 6307 unsigned Major = Record[Idx++]; 6308 unsigned Minor = Record[Idx++]; 6309 unsigned Subminor = Record[Idx++]; 6310 if (Minor == 0) 6311 return VersionTuple(Major); 6312 if (Subminor == 0) 6313 return VersionTuple(Major, Minor - 1); 6314 return VersionTuple(Major, Minor - 1, Subminor - 1); 6315} 6316 6317CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F, 6318 const RecordData &Record, 6319 unsigned &Idx) { 6320 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx); 6321 return CXXTemporary::Create(Context, Decl); 6322} 6323 6324DiagnosticBuilder ASTReader::Diag(unsigned DiagID) { 6325 return Diag(SourceLocation(), DiagID); 6326} 6327 6328DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) { 6329 return Diags.Report(Loc, DiagID); 6330} 6331 6332/// \brief Retrieve the identifier table associated with the 6333/// preprocessor. 6334IdentifierTable &ASTReader::getIdentifierTable() { 6335 return PP.getIdentifierTable(); 6336} 6337 6338/// \brief Record that the given ID maps to the given switch-case 6339/// statement. 6340void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { 6341 assert((*CurrSwitchCaseStmts)[ID] == 0 && 6342 "Already have a SwitchCase with this ID"); 6343 (*CurrSwitchCaseStmts)[ID] = SC; 6344} 6345 6346/// \brief Retrieve the switch-case statement with the given ID. 6347SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) { 6348 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID"); 6349 return (*CurrSwitchCaseStmts)[ID]; 6350} 6351 6352void ASTReader::ClearSwitchCaseIDs() { 6353 CurrSwitchCaseStmts->clear(); 6354} 6355 6356void ASTReader::ReadComments() { 6357 std::vector<RawComment *> Comments; 6358 for (SmallVectorImpl<std::pair<llvm::BitstreamCursor, 6359 serialization::ModuleFile *> >::iterator 6360 I = CommentsCursors.begin(), 6361 E = CommentsCursors.end(); 6362 I != E; ++I) { 6363 llvm::BitstreamCursor &Cursor = I->first; 6364 serialization::ModuleFile &F = *I->second; 6365 SavedStreamPosition SavedPosition(Cursor); 6366 6367 RecordData Record; 6368 while (true) { 6369 unsigned Code = Cursor.ReadCode(); 6370 if (Code == llvm::bitc::END_BLOCK) 6371 break; 6372 6373 if (Code == llvm::bitc::ENTER_SUBBLOCK) { 6374 // No known subblocks, always skip them. 6375 Cursor.ReadSubBlockID(); 6376 if (Cursor.SkipBlock()) { 6377 Error("malformed block record in AST file"); 6378 return; 6379 } 6380 continue; 6381 } 6382 6383 if (Code == llvm::bitc::DEFINE_ABBREV) { 6384 Cursor.ReadAbbrevRecord(); 6385 continue; 6386 } 6387 6388 // Read a record. 6389 Record.clear(); 6390 switch ((CommentRecordTypes) Cursor.ReadRecord(Code, Record)) { 6391 case COMMENTS_RAW_COMMENT: { 6392 unsigned Idx = 0; 6393 SourceRange SR = ReadSourceRange(F, Record, Idx); 6394 RawComment::CommentKind Kind = 6395 (RawComment::CommentKind) Record[Idx++]; 6396 bool IsTrailingComment = Record[Idx++]; 6397 bool IsAlmostTrailingComment = Record[Idx++]; 6398 Comments.push_back(new (Context) RawComment(SR, Kind, 6399 IsTrailingComment, 6400 IsAlmostTrailingComment)); 6401 break; 6402 } 6403 } 6404 } 6405 } 6406 Context.Comments.addCommentsToFront(Comments); 6407} 6408 6409void ASTReader::finishPendingActions() { 6410 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty()) { 6411 // If any identifiers with corresponding top-level declarations have 6412 // been loaded, load those declarations now. 6413 while (!PendingIdentifierInfos.empty()) { 6414 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II, 6415 PendingIdentifierInfos.front().DeclIDs, true); 6416 PendingIdentifierInfos.pop_front(); 6417 } 6418 6419 // Load pending declaration chains. 6420 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) { 6421 loadPendingDeclChain(PendingDeclChains[I]); 6422 PendingDeclChainsKnown.erase(PendingDeclChains[I]); 6423 } 6424 PendingDeclChains.clear(); 6425 } 6426 6427 // If we deserialized any C++ or Objective-C class definitions, any 6428 // Objective-C protocol definitions, or any redeclarable templates, make sure 6429 // that all redeclarations point to the definitions. Note that this can only 6430 // happen now, after the redeclaration chains have been fully wired. 6431 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(), 6432 DEnd = PendingDefinitions.end(); 6433 D != DEnd; ++D) { 6434 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) { 6435 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) { 6436 // Make sure that the TagType points at the definition. 6437 const_cast<TagType*>(TagT)->decl = TD; 6438 } 6439 6440 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) { 6441 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(), 6442 REnd = RD->redecls_end(); 6443 R != REnd; ++R) 6444 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData; 6445 6446 } 6447 6448 continue; 6449 } 6450 6451 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) { 6452 // Make sure that the ObjCInterfaceType points at the definition. 6453 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl)) 6454 ->Decl = ID; 6455 6456 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(), 6457 REnd = ID->redecls_end(); 6458 R != REnd; ++R) 6459 R->Data = ID->Data; 6460 6461 continue; 6462 } 6463 6464 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) { 6465 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(), 6466 REnd = PD->redecls_end(); 6467 R != REnd; ++R) 6468 R->Data = PD->Data; 6469 6470 continue; 6471 } 6472 6473 RedeclarableTemplateDecl *RTD 6474 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl(); 6475 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(), 6476 REnd = RTD->redecls_end(); 6477 R != REnd; ++R) 6478 R->Common = RTD->Common; 6479 } 6480 PendingDefinitions.clear(); 6481} 6482 6483void ASTReader::FinishedDeserializing() { 6484 assert(NumCurrentElementsDeserializing && 6485 "FinishedDeserializing not paired with StartedDeserializing"); 6486 if (NumCurrentElementsDeserializing == 1) { 6487 // We decrease NumCurrentElementsDeserializing only after pending actions 6488 // are finished, to avoid recursively re-calling finishPendingActions(). 6489 finishPendingActions(); 6490 } 6491 --NumCurrentElementsDeserializing; 6492 6493 if (NumCurrentElementsDeserializing == 0 && 6494 Consumer && !PassingDeclsToConsumer) { 6495 // Guard variable to avoid recursively redoing the process of passing 6496 // decls to consumer. 6497 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer, 6498 true); 6499 6500 while (!InterestingDecls.empty()) { 6501 // We are not in recursive loading, so it's safe to pass the "interesting" 6502 // decls to the consumer. 6503 Decl *D = InterestingDecls.front(); 6504 InterestingDecls.pop_front(); 6505 PassInterestingDeclToConsumer(D); 6506 } 6507 } 6508} 6509 6510ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context, 6511 StringRef isysroot, bool DisableValidation, 6512 bool DisableStatCache, bool AllowASTWithCompilerErrors) 6513 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0), 6514 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), 6515 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context), 6516 Consumer(0), ModuleMgr(PP.getFileManager()), 6517 RelocatablePCH(false), isysroot(isysroot), 6518 DisableValidation(DisableValidation), 6519 DisableStatCache(DisableStatCache), 6520 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors), 6521 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts), 6522 NumStatHits(0), NumStatMisses(0), 6523 NumSLocEntriesRead(0), TotalNumSLocEntries(0), 6524 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0), 6525 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0), 6526 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0), 6527 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0), 6528 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0), 6529 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0), 6530 PassingDeclsToConsumer(false), 6531 NumCXXBaseSpecifiersLoaded(0) 6532{ 6533 SourceMgr.setExternalSLocEntrySource(this); 6534} 6535 6536ASTReader::~ASTReader() { 6537 for (DeclContextVisibleUpdatesPending::iterator 6538 I = PendingVisibleUpdates.begin(), 6539 E = PendingVisibleUpdates.end(); 6540 I != E; ++I) { 6541 for (DeclContextVisibleUpdates::iterator J = I->second.begin(), 6542 F = I->second.end(); 6543 J != F; ++J) 6544 delete J->first; 6545 } 6546} 6547