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