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