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