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