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