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