ASTReader.cpp revision dca8ee8b7bc86076916a3a80f553f7a4e98c14af
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 1845/// \brief If we are loading a relocatable PCH file, and the filename is 1846/// not an absolute path, add the system root to the beginning of the file 1847/// name. 1848void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) { 1849 // If this is not a relocatable PCH file, there's nothing to do. 1850 if (!RelocatablePCH) 1851 return; 1852 1853 if (Filename.empty() || llvm::sys::path::is_absolute(Filename)) 1854 return; 1855 1856 if (isysroot == 0) { 1857 // If no system root was given, default to '/' 1858 Filename.insert(Filename.begin(), '/'); 1859 return; 1860 } 1861 1862 unsigned Length = strlen(isysroot); 1863 if (isysroot[Length - 1] != '/') 1864 Filename.insert(Filename.begin(), '/'); 1865 1866 Filename.insert(Filename.begin(), isysroot, isysroot + Length); 1867} 1868 1869ASTReader::ASTReadResult 1870ASTReader::ReadASTBlock(PerFileData &F) { 1871 llvm::BitstreamCursor &Stream = F.Stream; 1872 1873 if (Stream.EnterSubBlock(AST_BLOCK_ID)) { 1874 Error("malformed block record in AST file"); 1875 return Failure; 1876 } 1877 1878 // Read all of the records and blocks for the ASt file. 1879 RecordData Record; 1880 bool First = true; 1881 while (!Stream.AtEndOfStream()) { 1882 unsigned Code = Stream.ReadCode(); 1883 if (Code == llvm::bitc::END_BLOCK) { 1884 if (Stream.ReadBlockEnd()) { 1885 Error("error at end of module block in AST file"); 1886 return Failure; 1887 } 1888 1889 return Success; 1890 } 1891 1892 if (Code == llvm::bitc::ENTER_SUBBLOCK) { 1893 switch (Stream.ReadSubBlockID()) { 1894 case DECLTYPES_BLOCK_ID: 1895 // We lazily load the decls block, but we want to set up the 1896 // DeclsCursor cursor to point into it. Clone our current bitcode 1897 // cursor to it, enter the block and read the abbrevs in that block. 1898 // With the main cursor, we just skip over it. 1899 F.DeclsCursor = Stream; 1900 if (Stream.SkipBlock() || // Skip with the main cursor. 1901 // Read the abbrevs. 1902 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) { 1903 Error("malformed block record in AST file"); 1904 return Failure; 1905 } 1906 break; 1907 1908 case DECL_UPDATES_BLOCK_ID: 1909 if (Stream.SkipBlock()) { 1910 Error("malformed block record in AST file"); 1911 return Failure; 1912 } 1913 break; 1914 1915 case PREPROCESSOR_BLOCK_ID: 1916 F.MacroCursor = Stream; 1917 if (PP) 1918 PP->setExternalSource(this); 1919 1920 if (Stream.SkipBlock() || 1921 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) { 1922 Error("malformed block record in AST file"); 1923 return Failure; 1924 } 1925 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo(); 1926 break; 1927 1928 case PREPROCESSOR_DETAIL_BLOCK_ID: 1929 F.PreprocessorDetailCursor = Stream; 1930 if (Stream.SkipBlock() || 1931 ReadBlockAbbrevs(F.PreprocessorDetailCursor, 1932 PREPROCESSOR_DETAIL_BLOCK_ID)) { 1933 Error("malformed preprocessor detail record in AST file"); 1934 return Failure; 1935 } 1936 F.PreprocessorDetailStartOffset 1937 = F.PreprocessorDetailCursor.GetCurrentBitNo(); 1938 break; 1939 1940 case SOURCE_MANAGER_BLOCK_ID: 1941 switch (ReadSourceManagerBlock(F)) { 1942 case Success: 1943 break; 1944 1945 case Failure: 1946 Error("malformed source manager block in AST file"); 1947 return Failure; 1948 1949 case IgnorePCH: 1950 return IgnorePCH; 1951 } 1952 break; 1953 } 1954 First = false; 1955 continue; 1956 } 1957 1958 if (Code == llvm::bitc::DEFINE_ABBREV) { 1959 Stream.ReadAbbrevRecord(); 1960 continue; 1961 } 1962 1963 // Read and process a record. 1964 Record.clear(); 1965 const char *BlobStart = 0; 1966 unsigned BlobLen = 0; 1967 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record, 1968 &BlobStart, &BlobLen)) { 1969 default: // Default behavior: ignore. 1970 break; 1971 1972 case METADATA: { 1973 if (Record[0] != VERSION_MAJOR && !DisableValidation) { 1974 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old 1975 : diag::warn_pch_version_too_new); 1976 return IgnorePCH; 1977 } 1978 1979 RelocatablePCH = Record[4]; 1980 if (Listener) { 1981 std::string TargetTriple(BlobStart, BlobLen); 1982 if (Listener->ReadTargetTriple(TargetTriple)) 1983 return IgnorePCH; 1984 } 1985 break; 1986 } 1987 1988 case CHAINED_METADATA: { 1989 if (!First) { 1990 Error("CHAINED_METADATA is not first record in block"); 1991 return Failure; 1992 } 1993 if (Record[0] != VERSION_MAJOR && !DisableValidation) { 1994 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old 1995 : diag::warn_pch_version_too_new); 1996 return IgnorePCH; 1997 } 1998 1999 // Load the chained file, which is always a PCH file. 2000 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) { 2001 case Failure: return Failure; 2002 // If we have to ignore the dependency, we'll have to ignore this too. 2003 case IgnorePCH: return IgnorePCH; 2004 case Success: break; 2005 } 2006 break; 2007 } 2008 2009 case TYPE_OFFSET: 2010 if (F.LocalNumTypes != 0) { 2011 Error("duplicate TYPE_OFFSET record in AST file"); 2012 return Failure; 2013 } 2014 F.TypeOffsets = (const uint32_t *)BlobStart; 2015 F.LocalNumTypes = Record[0]; 2016 break; 2017 2018 case DECL_OFFSET: 2019 if (F.LocalNumDecls != 0) { 2020 Error("duplicate DECL_OFFSET record in AST file"); 2021 return Failure; 2022 } 2023 F.DeclOffsets = (const uint32_t *)BlobStart; 2024 F.LocalNumDecls = Record[0]; 2025 break; 2026 2027 case TU_UPDATE_LEXICAL: { 2028 DeclContextInfo Info = { 2029 /* No visible information */ 0, 2030 reinterpret_cast<const KindDeclIDPair *>(BlobStart), 2031 BlobLen / sizeof(KindDeclIDPair) 2032 }; 2033 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0] 2034 .push_back(Info); 2035 break; 2036 } 2037 2038 case UPDATE_VISIBLE: { 2039 serialization::DeclID ID = Record[0]; 2040 void *Table = ASTDeclContextNameLookupTable::Create( 2041 (const unsigned char *)BlobStart + Record[1], 2042 (const unsigned char *)BlobStart, 2043 ASTDeclContextNameLookupTrait(*this)); 2044 if (ID == 1 && Context) { // Is it the TU? 2045 DeclContextInfo Info = { 2046 Table, /* No lexical inforamtion */ 0, 0 2047 }; 2048 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info); 2049 } else 2050 PendingVisibleUpdates[ID].push_back(Table); 2051 break; 2052 } 2053 2054 case REDECLS_UPDATE_LATEST: { 2055 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs"); 2056 for (unsigned i = 0, e = Record.size(); i < e; i += 2) { 2057 DeclID First = Record[i], Latest = Record[i+1]; 2058 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() || 2059 Latest > FirstLatestDeclIDs[First]) && 2060 "The new latest is supposed to come after the previous latest"); 2061 FirstLatestDeclIDs[First] = Latest; 2062 } 2063 break; 2064 } 2065 2066 case LANGUAGE_OPTIONS: 2067 if (ParseLanguageOptions(Record) && !DisableValidation) 2068 return IgnorePCH; 2069 break; 2070 2071 case IDENTIFIER_TABLE: 2072 F.IdentifierTableData = BlobStart; 2073 if (Record[0]) { 2074 F.IdentifierLookupTable 2075 = ASTIdentifierLookupTable::Create( 2076 (const unsigned char *)F.IdentifierTableData + Record[0], 2077 (const unsigned char *)F.IdentifierTableData, 2078 ASTIdentifierLookupTrait(*this, F)); 2079 if (PP) 2080 PP->getIdentifierTable().setExternalIdentifierLookup(this); 2081 } 2082 break; 2083 2084 case IDENTIFIER_OFFSET: 2085 if (F.LocalNumIdentifiers != 0) { 2086 Error("duplicate IDENTIFIER_OFFSET record in AST file"); 2087 return Failure; 2088 } 2089 F.IdentifierOffsets = (const uint32_t *)BlobStart; 2090 F.LocalNumIdentifiers = Record[0]; 2091 break; 2092 2093 case EXTERNAL_DEFINITIONS: 2094 // Optimization for the first block. 2095 if (ExternalDefinitions.empty()) 2096 ExternalDefinitions.swap(Record); 2097 else 2098 ExternalDefinitions.insert(ExternalDefinitions.end(), 2099 Record.begin(), Record.end()); 2100 break; 2101 2102 case SPECIAL_TYPES: 2103 // Optimization for the first block 2104 if (SpecialTypes.empty()) 2105 SpecialTypes.swap(Record); 2106 else 2107 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end()); 2108 break; 2109 2110 case STATISTICS: 2111 TotalNumStatements += Record[0]; 2112 TotalNumMacros += Record[1]; 2113 TotalLexicalDeclContexts += Record[2]; 2114 TotalVisibleDeclContexts += Record[3]; 2115 break; 2116 2117 case UNUSED_FILESCOPED_DECLS: 2118 // Optimization for the first block. 2119 if (UnusedFileScopedDecls.empty()) 2120 UnusedFileScopedDecls.swap(Record); 2121 else 2122 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(), 2123 Record.begin(), Record.end()); 2124 break; 2125 2126 case DELEGATING_CTORS: 2127 // Optimization for the first block. 2128 if (DelegatingCtorDecls.empty()) 2129 DelegatingCtorDecls.swap(Record); 2130 else 2131 DelegatingCtorDecls.insert(DelegatingCtorDecls.end(), 2132 Record.begin(), Record.end()); 2133 break; 2134 2135 case WEAK_UNDECLARED_IDENTIFIERS: 2136 // Later blocks overwrite earlier ones. 2137 WeakUndeclaredIdentifiers.swap(Record); 2138 break; 2139 2140 case LOCALLY_SCOPED_EXTERNAL_DECLS: 2141 // Optimization for the first block. 2142 if (LocallyScopedExternalDecls.empty()) 2143 LocallyScopedExternalDecls.swap(Record); 2144 else 2145 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(), 2146 Record.begin(), Record.end()); 2147 break; 2148 2149 case SELECTOR_OFFSETS: 2150 F.SelectorOffsets = (const uint32_t *)BlobStart; 2151 F.LocalNumSelectors = Record[0]; 2152 break; 2153 2154 case METHOD_POOL: 2155 F.SelectorLookupTableData = (const unsigned char *)BlobStart; 2156 if (Record[0]) 2157 F.SelectorLookupTable 2158 = ASTSelectorLookupTable::Create( 2159 F.SelectorLookupTableData + Record[0], 2160 F.SelectorLookupTableData, 2161 ASTSelectorLookupTrait(*this)); 2162 TotalNumMethodPoolEntries += Record[1]; 2163 break; 2164 2165 case REFERENCED_SELECTOR_POOL: 2166 F.ReferencedSelectorsData.swap(Record); 2167 break; 2168 2169 case PP_COUNTER_VALUE: 2170 if (!Record.empty() && Listener) 2171 Listener->ReadCounter(Record[0]); 2172 break; 2173 2174 case SOURCE_LOCATION_OFFSETS: 2175 F.SLocOffsets = (const uint32_t *)BlobStart; 2176 F.LocalNumSLocEntries = Record[0]; 2177 F.LocalSLocSize = Record[1]; 2178 break; 2179 2180 case SOURCE_LOCATION_PRELOADS: 2181 if (PreloadSLocEntries.empty()) 2182 PreloadSLocEntries.swap(Record); 2183 else 2184 PreloadSLocEntries.insert(PreloadSLocEntries.end(), 2185 Record.begin(), Record.end()); 2186 break; 2187 2188 case STAT_CACHE: { 2189 if (!DisableStatCache) { 2190 ASTStatCache *MyStatCache = 2191 new ASTStatCache((const unsigned char *)BlobStart + Record[0], 2192 (const unsigned char *)BlobStart, 2193 NumStatHits, NumStatMisses); 2194 FileMgr.addStatCache(MyStatCache); 2195 F.StatCache = MyStatCache; 2196 } 2197 break; 2198 } 2199 2200 case EXT_VECTOR_DECLS: 2201 // Optimization for the first block. 2202 if (ExtVectorDecls.empty()) 2203 ExtVectorDecls.swap(Record); 2204 else 2205 ExtVectorDecls.insert(ExtVectorDecls.end(), 2206 Record.begin(), Record.end()); 2207 break; 2208 2209 case VTABLE_USES: 2210 // Later tables overwrite earlier ones. 2211 VTableUses.swap(Record); 2212 break; 2213 2214 case DYNAMIC_CLASSES: 2215 // Optimization for the first block. 2216 if (DynamicClasses.empty()) 2217 DynamicClasses.swap(Record); 2218 else 2219 DynamicClasses.insert(DynamicClasses.end(), 2220 Record.begin(), Record.end()); 2221 break; 2222 2223 case PENDING_IMPLICIT_INSTANTIATIONS: 2224 F.PendingInstantiations.swap(Record); 2225 break; 2226 2227 case SEMA_DECL_REFS: 2228 // Later tables overwrite earlier ones. 2229 SemaDeclRefs.swap(Record); 2230 break; 2231 2232 case ORIGINAL_FILE_NAME: 2233 // The primary AST will be the last to get here, so it will be the one 2234 // that's used. 2235 ActualOriginalFileName.assign(BlobStart, BlobLen); 2236 OriginalFileName = ActualOriginalFileName; 2237 MaybeAddSystemRootToFilename(OriginalFileName); 2238 break; 2239 2240 case ORIGINAL_PCH_DIR: 2241 // The primary AST will be the last to get here, so it will be the one 2242 // that's used. 2243 OriginalDir.assign(BlobStart, BlobLen); 2244 break; 2245 2246 case VERSION_CONTROL_BRANCH_REVISION: { 2247 const std::string &CurBranch = getClangFullRepositoryVersion(); 2248 llvm::StringRef ASTBranch(BlobStart, BlobLen); 2249 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) { 2250 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch; 2251 return IgnorePCH; 2252 } 2253 break; 2254 } 2255 2256 case MACRO_DEFINITION_OFFSETS: 2257 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart; 2258 F.NumPreallocatedPreprocessingEntities = Record[0]; 2259 F.LocalNumMacroDefinitions = Record[1]; 2260 break; 2261 2262 case DECL_UPDATE_OFFSETS: { 2263 if (Record.size() % 2 != 0) { 2264 Error("invalid DECL_UPDATE_OFFSETS block in AST file"); 2265 return Failure; 2266 } 2267 for (unsigned I = 0, N = Record.size(); I != N; I += 2) 2268 DeclUpdateOffsets[static_cast<DeclID>(Record[I])] 2269 .push_back(std::make_pair(&F, Record[I+1])); 2270 break; 2271 } 2272 2273 case DECL_REPLACEMENTS: { 2274 if (Record.size() % 2 != 0) { 2275 Error("invalid DECL_REPLACEMENTS block in AST file"); 2276 return Failure; 2277 } 2278 for (unsigned I = 0, N = Record.size(); I != N; I += 2) 2279 ReplacedDecls[static_cast<DeclID>(Record[I])] = 2280 std::make_pair(&F, Record[I+1]); 2281 break; 2282 } 2283 2284 case CXX_BASE_SPECIFIER_OFFSETS: { 2285 if (F.LocalNumCXXBaseSpecifiers != 0) { 2286 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file"); 2287 return Failure; 2288 } 2289 2290 F.LocalNumCXXBaseSpecifiers = Record[0]; 2291 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart; 2292 break; 2293 } 2294 2295 case DIAG_PRAGMA_MAPPINGS: 2296 if (Record.size() % 2 != 0) { 2297 Error("invalid DIAG_USER_MAPPINGS block in AST file"); 2298 return Failure; 2299 } 2300 if (PragmaDiagMappings.empty()) 2301 PragmaDiagMappings.swap(Record); 2302 else 2303 PragmaDiagMappings.insert(PragmaDiagMappings.end(), 2304 Record.begin(), Record.end()); 2305 break; 2306 2307 case CUDA_SPECIAL_DECL_REFS: 2308 // Later tables overwrite earlier ones. 2309 CUDASpecialDeclRefs.swap(Record); 2310 break; 2311 2312 case HEADER_SEARCH_TABLE: 2313 F.HeaderFileInfoTableData = BlobStart; 2314 F.LocalNumHeaderFileInfos = Record[1]; 2315 if (Record[0]) { 2316 F.HeaderFileInfoTable 2317 = HeaderFileInfoLookupTable::Create( 2318 (const unsigned char *)F.HeaderFileInfoTableData + Record[0], 2319 (const unsigned char *)F.HeaderFileInfoTableData); 2320 if (PP) 2321 PP->getHeaderSearchInfo().SetExternalSource(this); 2322 } 2323 break; 2324 2325 case FP_PRAGMA_OPTIONS: 2326 // Later tables overwrite earlier ones. 2327 FPPragmaOptions.swap(Record); 2328 break; 2329 2330 case OPENCL_EXTENSIONS: 2331 // Later tables overwrite earlier ones. 2332 OpenCLExtensions.swap(Record); 2333 break; 2334 2335 case TENTATIVE_DEFINITIONS: 2336 // Optimization for the first block. 2337 if (TentativeDefinitions.empty()) 2338 TentativeDefinitions.swap(Record); 2339 else 2340 TentativeDefinitions.insert(TentativeDefinitions.end(), 2341 Record.begin(), Record.end()); 2342 break; 2343 } 2344 First = false; 2345 } 2346 Error("premature end of bitstream in AST file"); 2347 return Failure; 2348} 2349 2350ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName, 2351 ASTFileType Type) { 2352 switch(ReadASTCore(FileName, Type)) { 2353 case Failure: return Failure; 2354 case IgnorePCH: return IgnorePCH; 2355 case Success: break; 2356 } 2357 2358 // Here comes stuff that we only do once the entire chain is loaded. 2359 2360 // Allocate space for loaded slocentries, identifiers, decls and types. 2361 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0, 2362 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0, 2363 TotalNumSelectors = 0; 2364 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 2365 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries; 2366 NextSLocOffset += Chain[I]->LocalSLocSize; 2367 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers; 2368 TotalNumTypes += Chain[I]->LocalNumTypes; 2369 TotalNumDecls += Chain[I]->LocalNumDecls; 2370 TotalNumPreallocatedPreprocessingEntities += 2371 Chain[I]->NumPreallocatedPreprocessingEntities; 2372 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions; 2373 TotalNumSelectors += Chain[I]->LocalNumSelectors; 2374 } 2375 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset); 2376 IdentifiersLoaded.resize(TotalNumIdentifiers); 2377 TypesLoaded.resize(TotalNumTypes); 2378 DeclsLoaded.resize(TotalNumDecls); 2379 MacroDefinitionsLoaded.resize(TotalNumMacroDefs); 2380 if (PP) { 2381 if (TotalNumIdentifiers > 0) 2382 PP->getHeaderSearchInfo().SetExternalLookup(this); 2383 if (TotalNumPreallocatedPreprocessingEntities > 0) { 2384 if (!PP->getPreprocessingRecord()) 2385 PP->createPreprocessingRecord(true); 2386 PP->getPreprocessingRecord()->SetExternalSource(*this, 2387 TotalNumPreallocatedPreprocessingEntities); 2388 } 2389 } 2390 SelectorsLoaded.resize(TotalNumSelectors); 2391 // Preload SLocEntries. 2392 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) { 2393 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]); 2394 if (Result != Success) 2395 return Result; 2396 } 2397 2398 // Check the predefines buffers. 2399 if (!DisableValidation && CheckPredefinesBuffers()) 2400 return IgnorePCH; 2401 2402 if (PP) { 2403 // Initialization of keywords and pragmas occurs before the 2404 // AST file is read, so there may be some identifiers that were 2405 // loaded into the IdentifierTable before we intercepted the 2406 // creation of identifiers. Iterate through the list of known 2407 // identifiers and determine whether we have to establish 2408 // preprocessor definitions or top-level identifier declaration 2409 // chains for those identifiers. 2410 // 2411 // We copy the IdentifierInfo pointers to a small vector first, 2412 // since de-serializing declarations or macro definitions can add 2413 // new entries into the identifier table, invalidating the 2414 // iterators. 2415 llvm::SmallVector<IdentifierInfo *, 128> Identifiers; 2416 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(), 2417 IdEnd = PP->getIdentifierTable().end(); 2418 Id != IdEnd; ++Id) 2419 Identifiers.push_back(Id->second); 2420 // We need to search the tables in all files. 2421 for (unsigned J = 0, M = Chain.size(); J != M; ++J) { 2422 ASTIdentifierLookupTable *IdTable 2423 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable; 2424 // Not all AST files necessarily have identifier tables, only the useful 2425 // ones. 2426 if (!IdTable) 2427 continue; 2428 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) { 2429 IdentifierInfo *II = Identifiers[I]; 2430 // Look in the on-disk hash tables for an entry for this identifier 2431 ASTIdentifierLookupTrait Info(*this, *Chain[J], II); 2432 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength()); 2433 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info); 2434 if (Pos == IdTable->end()) 2435 continue; 2436 2437 // Dereferencing the iterator has the effect of populating the 2438 // IdentifierInfo node with the various declarations it needs. 2439 (void)*Pos; 2440 } 2441 } 2442 } 2443 2444 if (Context) 2445 InitializeContext(*Context); 2446 2447 if (DeserializationListener) 2448 DeserializationListener->ReaderInitialized(this); 2449 2450 // If this AST file is a precompiled preamble, then set the main file ID of 2451 // the source manager to the file source file from which the preamble was 2452 // built. This is the only valid way to use a precompiled preamble. 2453 if (Type == Preamble) { 2454 SourceLocation Loc 2455 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1); 2456 if (Loc.isValid()) { 2457 std::pair<FileID, unsigned> Decomposed = SourceMgr.getDecomposedLoc(Loc); 2458 SourceMgr.SetPreambleFileID(Decomposed.first); 2459 } 2460 } 2461 2462 return Success; 2463} 2464 2465ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName, 2466 ASTFileType Type) { 2467 PerFileData *Prev = Chain.empty() ? 0 : Chain.back(); 2468 Chain.push_back(new PerFileData(Type)); 2469 PerFileData &F = *Chain.back(); 2470 if (Prev) 2471 Prev->NextInSource = &F; 2472 else 2473 FirstInSource = &F; 2474 F.Loaders.push_back(Prev); 2475 2476 // Set the AST file name. 2477 F.FileName = FileName; 2478 2479 if (FileName != "-") { 2480 CurrentDir = llvm::sys::path::parent_path(FileName); 2481 if (CurrentDir.empty()) CurrentDir = "."; 2482 } 2483 2484 if (!ASTBuffers.empty()) { 2485 F.Buffer.reset(ASTBuffers.back()); 2486 ASTBuffers.pop_back(); 2487 assert(F.Buffer && "Passed null buffer"); 2488 } else { 2489 // Open the AST file. 2490 // 2491 // FIXME: This shouldn't be here, we should just take a raw_ostream. 2492 std::string ErrStr; 2493 llvm::error_code ec; 2494 if (FileName == "-") { 2495 ec = llvm::MemoryBuffer::getSTDIN(F.Buffer); 2496 if (ec) 2497 ErrStr = ec.message(); 2498 } else 2499 F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr)); 2500 if (!F.Buffer) { 2501 Error(ErrStr.c_str()); 2502 return IgnorePCH; 2503 } 2504 } 2505 2506 // Initialize the stream 2507 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(), 2508 (const unsigned char *)F.Buffer->getBufferEnd()); 2509 llvm::BitstreamCursor &Stream = F.Stream; 2510 Stream.init(F.StreamFile); 2511 F.SizeInBits = F.Buffer->getBufferSize() * 8; 2512 2513 // Sniff for the signature. 2514 if (Stream.Read(8) != 'C' || 2515 Stream.Read(8) != 'P' || 2516 Stream.Read(8) != 'C' || 2517 Stream.Read(8) != 'H') { 2518 Diag(diag::err_not_a_pch_file) << FileName; 2519 return Failure; 2520 } 2521 2522 while (!Stream.AtEndOfStream()) { 2523 unsigned Code = Stream.ReadCode(); 2524 2525 if (Code != llvm::bitc::ENTER_SUBBLOCK) { 2526 Error("invalid record at top-level of AST file"); 2527 return Failure; 2528 } 2529 2530 unsigned BlockID = Stream.ReadSubBlockID(); 2531 2532 // We only know the AST subblock ID. 2533 switch (BlockID) { 2534 case llvm::bitc::BLOCKINFO_BLOCK_ID: 2535 if (Stream.ReadBlockInfoBlock()) { 2536 Error("malformed BlockInfoBlock in AST file"); 2537 return Failure; 2538 } 2539 break; 2540 case AST_BLOCK_ID: 2541 switch (ReadASTBlock(F)) { 2542 case Success: 2543 break; 2544 2545 case Failure: 2546 return Failure; 2547 2548 case IgnorePCH: 2549 // FIXME: We could consider reading through to the end of this 2550 // AST block, skipping subblocks, to see if there are other 2551 // AST blocks elsewhere. 2552 2553 // Clear out any preallocated source location entries, so that 2554 // the source manager does not try to resolve them later. 2555 SourceMgr.ClearPreallocatedSLocEntries(); 2556 2557 // Remove the stat cache. 2558 if (F.StatCache) 2559 FileMgr.removeStatCache((ASTStatCache*)F.StatCache); 2560 2561 return IgnorePCH; 2562 } 2563 break; 2564 default: 2565 if (Stream.SkipBlock()) { 2566 Error("malformed block record in AST file"); 2567 return Failure; 2568 } 2569 break; 2570 } 2571 } 2572 2573 return Success; 2574} 2575 2576void ASTReader::setPreprocessor(Preprocessor &pp) { 2577 PP = &pp; 2578 2579 unsigned TotalNum = 0; 2580 for (unsigned I = 0, N = Chain.size(); I != N; ++I) 2581 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities; 2582 if (TotalNum) { 2583 if (!PP->getPreprocessingRecord()) 2584 PP->createPreprocessingRecord(true); 2585 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum); 2586 } 2587} 2588 2589void ASTReader::InitializeContext(ASTContext &Ctx) { 2590 Context = &Ctx; 2591 assert(Context && "Passed null context!"); 2592 2593 assert(PP && "Forgot to set Preprocessor ?"); 2594 PP->getIdentifierTable().setExternalIdentifierLookup(this); 2595 PP->getHeaderSearchInfo().SetExternalLookup(this); 2596 PP->setExternalSource(this); 2597 PP->getHeaderSearchInfo().SetExternalSource(this); 2598 2599 // If we have an update block for the TU waiting, we have to add it before 2600 // deserializing the decl. 2601 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0); 2602 if (DCU != DeclContextOffsets.end()) { 2603 // Insertion could invalidate map, so grab vector. 2604 DeclContextInfos T; 2605 T.swap(DCU->second); 2606 DeclContextOffsets.erase(DCU); 2607 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T); 2608 } 2609 2610 // Load the translation unit declaration 2611 GetTranslationUnitDecl(); 2612 2613 // Load the special types. 2614 Context->setBuiltinVaListType( 2615 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST])); 2616 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID]) 2617 Context->setObjCIdType(GetType(Id)); 2618 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR]) 2619 Context->setObjCSelType(GetType(Sel)); 2620 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL]) 2621 Context->setObjCProtoType(GetType(Proto)); 2622 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS]) 2623 Context->setObjCClassType(GetType(Class)); 2624 2625 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) 2626 Context->setCFConstantStringType(GetType(String)); 2627 if (unsigned FastEnum 2628 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE]) 2629 Context->setObjCFastEnumerationStateType(GetType(FastEnum)); 2630 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) { 2631 QualType FileType = GetType(File); 2632 if (FileType.isNull()) { 2633 Error("FILE type is NULL"); 2634 return; 2635 } 2636 if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) 2637 Context->setFILEDecl(Typedef->getDecl()); 2638 else { 2639 const TagType *Tag = FileType->getAs<TagType>(); 2640 if (!Tag) { 2641 Error("Invalid FILE type in AST file"); 2642 return; 2643 } 2644 Context->setFILEDecl(Tag->getDecl()); 2645 } 2646 } 2647 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) { 2648 QualType Jmp_bufType = GetType(Jmp_buf); 2649 if (Jmp_bufType.isNull()) { 2650 Error("jmp_bug type is NULL"); 2651 return; 2652 } 2653 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) 2654 Context->setjmp_bufDecl(Typedef->getDecl()); 2655 else { 2656 const TagType *Tag = Jmp_bufType->getAs<TagType>(); 2657 if (!Tag) { 2658 Error("Invalid jmp_buf type in AST file"); 2659 return; 2660 } 2661 Context->setjmp_bufDecl(Tag->getDecl()); 2662 } 2663 } 2664 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) { 2665 QualType Sigjmp_bufType = GetType(Sigjmp_buf); 2666 if (Sigjmp_bufType.isNull()) { 2667 Error("sigjmp_buf type is NULL"); 2668 return; 2669 } 2670 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) 2671 Context->setsigjmp_bufDecl(Typedef->getDecl()); 2672 else { 2673 const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); 2674 assert(Tag && "Invalid sigjmp_buf type in AST file"); 2675 Context->setsigjmp_bufDecl(Tag->getDecl()); 2676 } 2677 } 2678 if (unsigned ObjCIdRedef 2679 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) 2680 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef); 2681 if (unsigned ObjCClassRedef 2682 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) 2683 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef); 2684 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR]) 2685 Context->setBlockDescriptorType(GetType(String)); 2686 if (unsigned String 2687 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR]) 2688 Context->setBlockDescriptorExtendedType(GetType(String)); 2689 if (unsigned ObjCSelRedef 2690 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) 2691 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef); 2692 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING]) 2693 Context->setNSConstantStringType(GetType(String)); 2694 2695 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED]) 2696 Context->setInt128Installed(); 2697 2698 if (unsigned AutoDeduct = SpecialTypes[SPECIAL_TYPE_AUTO_DEDUCT]) 2699 Context->AutoDeductTy = GetType(AutoDeduct); 2700 if (unsigned AutoRRefDeduct = SpecialTypes[SPECIAL_TYPE_AUTO_RREF_DEDUCT]) 2701 Context->AutoRRefDeductTy = GetType(AutoRRefDeduct); 2702 2703 ReadPragmaDiagnosticMappings(Context->getDiagnostics()); 2704 2705 // If there were any CUDA special declarations, deserialize them. 2706 if (!CUDASpecialDeclRefs.empty()) { 2707 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!"); 2708 Context->setcudaConfigureCallDecl( 2709 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0]))); 2710 } 2711} 2712 2713/// \brief Retrieve the name of the original source file name 2714/// directly from the AST file, without actually loading the AST 2715/// file. 2716std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName, 2717 FileManager &FileMgr, 2718 Diagnostic &Diags) { 2719 // Open the AST file. 2720 std::string ErrStr; 2721 llvm::OwningPtr<llvm::MemoryBuffer> Buffer; 2722 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr)); 2723 if (!Buffer) { 2724 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr; 2725 return std::string(); 2726 } 2727 2728 // Initialize the stream 2729 llvm::BitstreamReader StreamFile; 2730 llvm::BitstreamCursor Stream; 2731 StreamFile.init((const unsigned char *)Buffer->getBufferStart(), 2732 (const unsigned char *)Buffer->getBufferEnd()); 2733 Stream.init(StreamFile); 2734 2735 // Sniff for the signature. 2736 if (Stream.Read(8) != 'C' || 2737 Stream.Read(8) != 'P' || 2738 Stream.Read(8) != 'C' || 2739 Stream.Read(8) != 'H') { 2740 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName; 2741 return std::string(); 2742 } 2743 2744 RecordData Record; 2745 while (!Stream.AtEndOfStream()) { 2746 unsigned Code = Stream.ReadCode(); 2747 2748 if (Code == llvm::bitc::ENTER_SUBBLOCK) { 2749 unsigned BlockID = Stream.ReadSubBlockID(); 2750 2751 // We only know the AST subblock ID. 2752 switch (BlockID) { 2753 case AST_BLOCK_ID: 2754 if (Stream.EnterSubBlock(AST_BLOCK_ID)) { 2755 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 2756 return std::string(); 2757 } 2758 break; 2759 2760 default: 2761 if (Stream.SkipBlock()) { 2762 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 2763 return std::string(); 2764 } 2765 break; 2766 } 2767 continue; 2768 } 2769 2770 if (Code == llvm::bitc::END_BLOCK) { 2771 if (Stream.ReadBlockEnd()) { 2772 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName; 2773 return std::string(); 2774 } 2775 continue; 2776 } 2777 2778 if (Code == llvm::bitc::DEFINE_ABBREV) { 2779 Stream.ReadAbbrevRecord(); 2780 continue; 2781 } 2782 2783 Record.clear(); 2784 const char *BlobStart = 0; 2785 unsigned BlobLen = 0; 2786 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen) 2787 == ORIGINAL_FILE_NAME) 2788 return std::string(BlobStart, BlobLen); 2789 } 2790 2791 return std::string(); 2792} 2793 2794/// \brief Parse the record that corresponds to a LangOptions data 2795/// structure. 2796/// 2797/// This routine parses the language options from the AST file and then gives 2798/// them to the AST listener if one is set. 2799/// 2800/// \returns true if the listener deems the file unacceptable, false otherwise. 2801bool ASTReader::ParseLanguageOptions( 2802 const llvm::SmallVectorImpl<uint64_t> &Record) { 2803 if (Listener) { 2804 LangOptions LangOpts; 2805 2806 #define PARSE_LANGOPT(Option) \ 2807 LangOpts.Option = Record[Idx]; \ 2808 ++Idx 2809 2810 unsigned Idx = 0; 2811 PARSE_LANGOPT(Trigraphs); 2812 PARSE_LANGOPT(BCPLComment); 2813 PARSE_LANGOPT(DollarIdents); 2814 PARSE_LANGOPT(AsmPreprocessor); 2815 PARSE_LANGOPT(GNUMode); 2816 PARSE_LANGOPT(GNUKeywords); 2817 PARSE_LANGOPT(ImplicitInt); 2818 PARSE_LANGOPT(Digraphs); 2819 PARSE_LANGOPT(HexFloats); 2820 PARSE_LANGOPT(C99); 2821 PARSE_LANGOPT(C1X); 2822 PARSE_LANGOPT(Microsoft); 2823 PARSE_LANGOPT(CPlusPlus); 2824 PARSE_LANGOPT(CPlusPlus0x); 2825 PARSE_LANGOPT(CXXOperatorNames); 2826 PARSE_LANGOPT(ObjC1); 2827 PARSE_LANGOPT(ObjC2); 2828 PARSE_LANGOPT(ObjCNonFragileABI); 2829 PARSE_LANGOPT(ObjCNonFragileABI2); 2830 PARSE_LANGOPT(AppleKext); 2831 PARSE_LANGOPT(ObjCDefaultSynthProperties); 2832 PARSE_LANGOPT(NoConstantCFStrings); 2833 PARSE_LANGOPT(PascalStrings); 2834 PARSE_LANGOPT(WritableStrings); 2835 PARSE_LANGOPT(LaxVectorConversions); 2836 PARSE_LANGOPT(AltiVec); 2837 PARSE_LANGOPT(Exceptions); 2838 PARSE_LANGOPT(ObjCExceptions); 2839 PARSE_LANGOPT(CXXExceptions); 2840 PARSE_LANGOPT(SjLjExceptions); 2841 PARSE_LANGOPT(MSBitfields); 2842 PARSE_LANGOPT(NeXTRuntime); 2843 PARSE_LANGOPT(Freestanding); 2844 PARSE_LANGOPT(NoBuiltin); 2845 PARSE_LANGOPT(ThreadsafeStatics); 2846 PARSE_LANGOPT(POSIXThreads); 2847 PARSE_LANGOPT(Blocks); 2848 PARSE_LANGOPT(EmitAllDecls); 2849 PARSE_LANGOPT(MathErrno); 2850 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy) 2851 Record[Idx++]); 2852 PARSE_LANGOPT(HeinousExtensions); 2853 PARSE_LANGOPT(Optimize); 2854 PARSE_LANGOPT(OptimizeSize); 2855 PARSE_LANGOPT(Static); 2856 PARSE_LANGOPT(PICLevel); 2857 PARSE_LANGOPT(GNUInline); 2858 PARSE_LANGOPT(NoInline); 2859 PARSE_LANGOPT(Deprecated); 2860 PARSE_LANGOPT(AccessControl); 2861 PARSE_LANGOPT(CharIsSigned); 2862 PARSE_LANGOPT(ShortWChar); 2863 PARSE_LANGOPT(ShortEnums); 2864 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]); 2865 LangOpts.setVisibilityMode((Visibility)Record[Idx++]); 2866 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode) 2867 Record[Idx++]); 2868 PARSE_LANGOPT(InstantiationDepth); 2869 PARSE_LANGOPT(OpenCL); 2870 PARSE_LANGOPT(CUDA); 2871 PARSE_LANGOPT(CatchUndefined); 2872 PARSE_LANGOPT(DefaultFPContract); 2873 PARSE_LANGOPT(ElideConstructors); 2874 PARSE_LANGOPT(SpellChecking); 2875 PARSE_LANGOPT(MRTD); 2876 #undef PARSE_LANGOPT 2877 2878 return Listener->ReadLanguageOptions(LangOpts); 2879 } 2880 2881 return false; 2882} 2883 2884void ASTReader::ReadPreprocessedEntities() { 2885 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 2886 PerFileData &F = *Chain[I]; 2887 if (!F.PreprocessorDetailCursor.getBitStreamReader()) 2888 continue; 2889 2890 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor); 2891 F.PreprocessorDetailCursor.JumpToBit(F.PreprocessorDetailStartOffset); 2892 while (LoadPreprocessedEntity(F)) { } 2893 } 2894} 2895 2896PreprocessedEntity *ASTReader::ReadPreprocessedEntityAtOffset(uint64_t Offset) { 2897 PerFileData *F = 0; 2898 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 2899 if (Offset < Chain[I]->SizeInBits) { 2900 F = Chain[I]; 2901 break; 2902 } 2903 2904 Offset -= Chain[I]->SizeInBits; 2905 } 2906 2907 if (!F) { 2908 Error("Malformed preprocessed entity offset"); 2909 return 0; 2910 } 2911 2912 // Keep track of where we are in the stream, then jump back there 2913 // after reading this entity. 2914 SavedStreamPosition SavedPosition(F->PreprocessorDetailCursor); 2915 F->PreprocessorDetailCursor.JumpToBit(Offset); 2916 return LoadPreprocessedEntity(*F); 2917} 2918 2919HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) { 2920 HeaderFileInfoTrait Trait(FE->getName()); 2921 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 2922 PerFileData &F = *Chain[I]; 2923 HeaderFileInfoLookupTable *Table 2924 = static_cast<HeaderFileInfoLookupTable *>(F.HeaderFileInfoTable); 2925 if (!Table) 2926 continue; 2927 2928 // Look in the on-disk hash table for an entry for this file name. 2929 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE->getName(), 2930 &Trait); 2931 if (Pos == Table->end()) 2932 continue; 2933 2934 HeaderFileInfo HFI = *Pos; 2935 if (Listener) 2936 Listener->ReadHeaderFileInfo(HFI, FE->getUID()); 2937 2938 return HFI; 2939 } 2940 2941 return HeaderFileInfo(); 2942} 2943 2944void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) { 2945 unsigned Idx = 0; 2946 while (Idx < PragmaDiagMappings.size()) { 2947 SourceLocation 2948 Loc = SourceLocation::getFromRawEncoding(PragmaDiagMappings[Idx++]); 2949 while (1) { 2950 assert(Idx < PragmaDiagMappings.size() && 2951 "Invalid data, didn't find '-1' marking end of diag/map pairs"); 2952 if (Idx >= PragmaDiagMappings.size()) 2953 break; // Something is messed up but at least avoid infinite loop in 2954 // release build. 2955 unsigned DiagID = PragmaDiagMappings[Idx++]; 2956 if (DiagID == (unsigned)-1) 2957 break; // no more diag/map pairs for this location. 2958 diag::Mapping Map = (diag::Mapping)PragmaDiagMappings[Idx++]; 2959 Diag.setDiagnosticMapping(DiagID, Map, Loc); 2960 } 2961 } 2962} 2963 2964/// \brief Get the correct cursor and offset for loading a type. 2965ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) { 2966 PerFileData *F = 0; 2967 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 2968 F = Chain[N - I - 1]; 2969 if (Index < F->LocalNumTypes) 2970 break; 2971 Index -= F->LocalNumTypes; 2972 } 2973 assert(F && F->LocalNumTypes > Index && "Broken chain"); 2974 return RecordLocation(F, F->TypeOffsets[Index]); 2975} 2976 2977/// \brief Read and return the type with the given index.. 2978/// 2979/// The index is the type ID, shifted and minus the number of predefs. This 2980/// routine actually reads the record corresponding to the type at the given 2981/// location. It is a helper routine for GetType, which deals with reading type 2982/// IDs. 2983QualType ASTReader::ReadTypeRecord(unsigned Index) { 2984 RecordLocation Loc = TypeCursorForIndex(Index); 2985 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 2986 2987 // Keep track of where we are in the stream, then jump back there 2988 // after reading this type. 2989 SavedStreamPosition SavedPosition(DeclsCursor); 2990 2991 ReadingKindTracker ReadingKind(Read_Type, *this); 2992 2993 // Note that we are loading a type record. 2994 Deserializing AType(this); 2995 2996 DeclsCursor.JumpToBit(Loc.Offset); 2997 RecordData Record; 2998 unsigned Code = DeclsCursor.ReadCode(); 2999 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) { 3000 case TYPE_EXT_QUAL: { 3001 if (Record.size() != 2) { 3002 Error("Incorrect encoding of extended qualifier type"); 3003 return QualType(); 3004 } 3005 QualType Base = GetType(Record[0]); 3006 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]); 3007 return Context->getQualifiedType(Base, Quals); 3008 } 3009 3010 case TYPE_COMPLEX: { 3011 if (Record.size() != 1) { 3012 Error("Incorrect encoding of complex type"); 3013 return QualType(); 3014 } 3015 QualType ElemType = GetType(Record[0]); 3016 return Context->getComplexType(ElemType); 3017 } 3018 3019 case TYPE_POINTER: { 3020 if (Record.size() != 1) { 3021 Error("Incorrect encoding of pointer type"); 3022 return QualType(); 3023 } 3024 QualType PointeeType = GetType(Record[0]); 3025 return Context->getPointerType(PointeeType); 3026 } 3027 3028 case TYPE_BLOCK_POINTER: { 3029 if (Record.size() != 1) { 3030 Error("Incorrect encoding of block pointer type"); 3031 return QualType(); 3032 } 3033 QualType PointeeType = GetType(Record[0]); 3034 return Context->getBlockPointerType(PointeeType); 3035 } 3036 3037 case TYPE_LVALUE_REFERENCE: { 3038 if (Record.size() != 2) { 3039 Error("Incorrect encoding of lvalue reference type"); 3040 return QualType(); 3041 } 3042 QualType PointeeType = GetType(Record[0]); 3043 return Context->getLValueReferenceType(PointeeType, Record[1]); 3044 } 3045 3046 case TYPE_RVALUE_REFERENCE: { 3047 if (Record.size() != 1) { 3048 Error("Incorrect encoding of rvalue reference type"); 3049 return QualType(); 3050 } 3051 QualType PointeeType = GetType(Record[0]); 3052 return Context->getRValueReferenceType(PointeeType); 3053 } 3054 3055 case TYPE_MEMBER_POINTER: { 3056 if (Record.size() != 2) { 3057 Error("Incorrect encoding of member pointer type"); 3058 return QualType(); 3059 } 3060 QualType PointeeType = GetType(Record[0]); 3061 QualType ClassType = GetType(Record[1]); 3062 if (PointeeType.isNull() || ClassType.isNull()) 3063 return QualType(); 3064 3065 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr()); 3066 } 3067 3068 case TYPE_CONSTANT_ARRAY: { 3069 QualType ElementType = GetType(Record[0]); 3070 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 3071 unsigned IndexTypeQuals = Record[2]; 3072 unsigned Idx = 3; 3073 llvm::APInt Size = ReadAPInt(Record, Idx); 3074 return Context->getConstantArrayType(ElementType, Size, 3075 ASM, IndexTypeQuals); 3076 } 3077 3078 case TYPE_INCOMPLETE_ARRAY: { 3079 QualType ElementType = GetType(Record[0]); 3080 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 3081 unsigned IndexTypeQuals = Record[2]; 3082 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals); 3083 } 3084 3085 case TYPE_VARIABLE_ARRAY: { 3086 QualType ElementType = GetType(Record[0]); 3087 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 3088 unsigned IndexTypeQuals = Record[2]; 3089 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]); 3090 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]); 3091 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F), 3092 ASM, IndexTypeQuals, 3093 SourceRange(LBLoc, RBLoc)); 3094 } 3095 3096 case TYPE_VECTOR: { 3097 if (Record.size() != 3) { 3098 Error("incorrect encoding of vector type in AST file"); 3099 return QualType(); 3100 } 3101 3102 QualType ElementType = GetType(Record[0]); 3103 unsigned NumElements = Record[1]; 3104 unsigned VecKind = Record[2]; 3105 return Context->getVectorType(ElementType, NumElements, 3106 (VectorType::VectorKind)VecKind); 3107 } 3108 3109 case TYPE_EXT_VECTOR: { 3110 if (Record.size() != 3) { 3111 Error("incorrect encoding of extended vector type in AST file"); 3112 return QualType(); 3113 } 3114 3115 QualType ElementType = GetType(Record[0]); 3116 unsigned NumElements = Record[1]; 3117 return Context->getExtVectorType(ElementType, NumElements); 3118 } 3119 3120 case TYPE_FUNCTION_NO_PROTO: { 3121 if (Record.size() != 5) { 3122 Error("incorrect encoding of no-proto function type"); 3123 return QualType(); 3124 } 3125 QualType ResultType = GetType(Record[0]); 3126 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3], (CallingConv)Record[4]); 3127 return Context->getFunctionNoProtoType(ResultType, Info); 3128 } 3129 3130 case TYPE_FUNCTION_PROTO: { 3131 QualType ResultType = GetType(Record[0]); 3132 3133 FunctionProtoType::ExtProtoInfo EPI; 3134 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1], 3135 /*hasregparm*/ Record[2], 3136 /*regparm*/ Record[3], 3137 static_cast<CallingConv>(Record[4])); 3138 3139 unsigned Idx = 5; 3140 unsigned NumParams = Record[Idx++]; 3141 llvm::SmallVector<QualType, 16> ParamTypes; 3142 for (unsigned I = 0; I != NumParams; ++I) 3143 ParamTypes.push_back(GetType(Record[Idx++])); 3144 3145 EPI.Variadic = Record[Idx++]; 3146 EPI.TypeQuals = Record[Idx++]; 3147 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]); 3148 ExceptionSpecificationType EST = 3149 static_cast<ExceptionSpecificationType>(Record[Idx++]); 3150 EPI.ExceptionSpecType = EST; 3151 if (EST == EST_Dynamic) { 3152 EPI.NumExceptions = Record[Idx++]; 3153 llvm::SmallVector<QualType, 2> Exceptions; 3154 for (unsigned I = 0; I != EPI.NumExceptions; ++I) 3155 Exceptions.push_back(GetType(Record[Idx++])); 3156 EPI.Exceptions = Exceptions.data(); 3157 } else if (EST == EST_ComputedNoexcept) { 3158 EPI.NoexceptExpr = ReadExpr(*Loc.F); 3159 } 3160 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams, 3161 EPI); 3162 } 3163 3164 case TYPE_UNRESOLVED_USING: 3165 return Context->getTypeDeclType( 3166 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0]))); 3167 3168 case TYPE_TYPEDEF: { 3169 if (Record.size() != 2) { 3170 Error("incorrect encoding of typedef type"); 3171 return QualType(); 3172 } 3173 TypedefNameDecl *Decl = cast<TypedefNameDecl>(GetDecl(Record[0])); 3174 QualType Canonical = GetType(Record[1]); 3175 if (!Canonical.isNull()) 3176 Canonical = Context->getCanonicalType(Canonical); 3177 return Context->getTypedefType(Decl, Canonical); 3178 } 3179 3180 case TYPE_TYPEOF_EXPR: 3181 return Context->getTypeOfExprType(ReadExpr(*Loc.F)); 3182 3183 case TYPE_TYPEOF: { 3184 if (Record.size() != 1) { 3185 Error("incorrect encoding of typeof(type) in AST file"); 3186 return QualType(); 3187 } 3188 QualType UnderlyingType = GetType(Record[0]); 3189 return Context->getTypeOfType(UnderlyingType); 3190 } 3191 3192 case TYPE_DECLTYPE: 3193 return Context->getDecltypeType(ReadExpr(*Loc.F)); 3194 3195 case TYPE_AUTO: 3196 return Context->getAutoType(GetType(Record[0])); 3197 3198 case TYPE_RECORD: { 3199 if (Record.size() != 2) { 3200 Error("incorrect encoding of record type"); 3201 return QualType(); 3202 } 3203 bool IsDependent = Record[0]; 3204 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1]))); 3205 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 3206 return T; 3207 } 3208 3209 case TYPE_ENUM: { 3210 if (Record.size() != 2) { 3211 Error("incorrect encoding of enum type"); 3212 return QualType(); 3213 } 3214 bool IsDependent = Record[0]; 3215 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1]))); 3216 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 3217 return T; 3218 } 3219 3220 case TYPE_ATTRIBUTED: { 3221 if (Record.size() != 3) { 3222 Error("incorrect encoding of attributed type"); 3223 return QualType(); 3224 } 3225 QualType modifiedType = GetType(Record[0]); 3226 QualType equivalentType = GetType(Record[1]); 3227 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]); 3228 return Context->getAttributedType(kind, modifiedType, equivalentType); 3229 } 3230 3231 case TYPE_PAREN: { 3232 if (Record.size() != 1) { 3233 Error("incorrect encoding of paren type"); 3234 return QualType(); 3235 } 3236 QualType InnerType = GetType(Record[0]); 3237 return Context->getParenType(InnerType); 3238 } 3239 3240 case TYPE_PACK_EXPANSION: { 3241 if (Record.size() != 2) { 3242 Error("incorrect encoding of pack expansion type"); 3243 return QualType(); 3244 } 3245 QualType Pattern = GetType(Record[0]); 3246 if (Pattern.isNull()) 3247 return QualType(); 3248 llvm::Optional<unsigned> NumExpansions; 3249 if (Record[1]) 3250 NumExpansions = Record[1] - 1; 3251 return Context->getPackExpansionType(Pattern, NumExpansions); 3252 } 3253 3254 case TYPE_ELABORATED: { 3255 unsigned Idx = 0; 3256 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 3257 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); 3258 QualType NamedType = GetType(Record[Idx++]); 3259 return Context->getElaboratedType(Keyword, NNS, NamedType); 3260 } 3261 3262 case TYPE_OBJC_INTERFACE: { 3263 unsigned Idx = 0; 3264 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++])); 3265 return Context->getObjCInterfaceType(ItfD); 3266 } 3267 3268 case TYPE_OBJC_OBJECT: { 3269 unsigned Idx = 0; 3270 QualType Base = GetType(Record[Idx++]); 3271 unsigned NumProtos = Record[Idx++]; 3272 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos; 3273 for (unsigned I = 0; I != NumProtos; ++I) 3274 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++]))); 3275 return Context->getObjCObjectType(Base, Protos.data(), NumProtos); 3276 } 3277 3278 case TYPE_OBJC_OBJECT_POINTER: { 3279 unsigned Idx = 0; 3280 QualType Pointee = GetType(Record[Idx++]); 3281 return Context->getObjCObjectPointerType(Pointee); 3282 } 3283 3284 case TYPE_SUBST_TEMPLATE_TYPE_PARM: { 3285 unsigned Idx = 0; 3286 QualType Parm = GetType(Record[Idx++]); 3287 QualType Replacement = GetType(Record[Idx++]); 3288 return 3289 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm), 3290 Replacement); 3291 } 3292 3293 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: { 3294 unsigned Idx = 0; 3295 QualType Parm = GetType(Record[Idx++]); 3296 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx); 3297 return Context->getSubstTemplateTypeParmPackType( 3298 cast<TemplateTypeParmType>(Parm), 3299 ArgPack); 3300 } 3301 3302 case TYPE_INJECTED_CLASS_NAME: { 3303 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0])); 3304 QualType TST = GetType(Record[1]); // probably derivable 3305 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable 3306 // for AST reading, too much interdependencies. 3307 return 3308 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0); 3309 } 3310 3311 case TYPE_TEMPLATE_TYPE_PARM: { 3312 unsigned Idx = 0; 3313 unsigned Depth = Record[Idx++]; 3314 unsigned Index = Record[Idx++]; 3315 bool Pack = Record[Idx++]; 3316 TemplateTypeParmDecl *D = 3317 cast_or_null<TemplateTypeParmDecl>(GetDecl(Record[Idx++])); 3318 return Context->getTemplateTypeParmType(Depth, Index, Pack, D); 3319 } 3320 3321 case TYPE_DEPENDENT_NAME: { 3322 unsigned Idx = 0; 3323 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 3324 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); 3325 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx); 3326 QualType Canon = GetType(Record[Idx++]); 3327 if (!Canon.isNull()) 3328 Canon = Context->getCanonicalType(Canon); 3329 return Context->getDependentNameType(Keyword, NNS, Name, Canon); 3330 } 3331 3332 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: { 3333 unsigned Idx = 0; 3334 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 3335 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); 3336 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx); 3337 unsigned NumArgs = Record[Idx++]; 3338 llvm::SmallVector<TemplateArgument, 8> Args; 3339 Args.reserve(NumArgs); 3340 while (NumArgs--) 3341 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx)); 3342 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name, 3343 Args.size(), Args.data()); 3344 } 3345 3346 case TYPE_DEPENDENT_SIZED_ARRAY: { 3347 unsigned Idx = 0; 3348 3349 // ArrayType 3350 QualType ElementType = GetType(Record[Idx++]); 3351 ArrayType::ArraySizeModifier ASM 3352 = (ArrayType::ArraySizeModifier)Record[Idx++]; 3353 unsigned IndexTypeQuals = Record[Idx++]; 3354 3355 // DependentSizedArrayType 3356 Expr *NumElts = ReadExpr(*Loc.F); 3357 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx); 3358 3359 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM, 3360 IndexTypeQuals, Brackets); 3361 } 3362 3363 case TYPE_TEMPLATE_SPECIALIZATION: { 3364 unsigned Idx = 0; 3365 bool IsDependent = Record[Idx++]; 3366 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx); 3367 llvm::SmallVector<TemplateArgument, 8> Args; 3368 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx); 3369 QualType Underlying = GetType(Record[Idx++]); 3370 QualType T; 3371 if (Underlying.isNull()) 3372 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(), 3373 Args.size()); 3374 else 3375 T = Context->getTemplateSpecializationType(Name, Args.data(), 3376 Args.size(), Underlying); 3377 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 3378 return T; 3379 } 3380 } 3381 // Suppress a GCC warning 3382 return QualType(); 3383} 3384 3385class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> { 3386 ASTReader &Reader; 3387 ASTReader::PerFileData &F; 3388 llvm::BitstreamCursor &DeclsCursor; 3389 const ASTReader::RecordData &Record; 3390 unsigned &Idx; 3391 3392 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R, 3393 unsigned &I) { 3394 return Reader.ReadSourceLocation(F, R, I); 3395 } 3396 3397public: 3398 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F, 3399 const ASTReader::RecordData &Record, unsigned &Idx) 3400 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx) 3401 { } 3402 3403 // We want compile-time assurance that we've enumerated all of 3404 // these, so unfortunately we have to declare them first, then 3405 // define them out-of-line. 3406#define ABSTRACT_TYPELOC(CLASS, PARENT) 3407#define TYPELOC(CLASS, PARENT) \ 3408 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 3409#include "clang/AST/TypeLocNodes.def" 3410 3411 void VisitFunctionTypeLoc(FunctionTypeLoc); 3412 void VisitArrayTypeLoc(ArrayTypeLoc); 3413}; 3414 3415void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 3416 // nothing to do 3417} 3418void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 3419 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx)); 3420 if (TL.needsExtraLocalData()) { 3421 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++])); 3422 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++])); 3423 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++])); 3424 TL.setModeAttr(Record[Idx++]); 3425 } 3426} 3427void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { 3428 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3429} 3430void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { 3431 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 3432} 3433void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 3434 TL.setCaretLoc(ReadSourceLocation(Record, Idx)); 3435} 3436void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 3437 TL.setAmpLoc(ReadSourceLocation(Record, Idx)); 3438} 3439void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 3440 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx)); 3441} 3442void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 3443 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 3444 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx)); 3445} 3446void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { 3447 TL.setLBracketLoc(ReadSourceLocation(Record, Idx)); 3448 TL.setRBracketLoc(ReadSourceLocation(Record, Idx)); 3449 if (Record[Idx++]) 3450 TL.setSizeExpr(Reader.ReadExpr(F)); 3451 else 3452 TL.setSizeExpr(0); 3453} 3454void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 3455 VisitArrayTypeLoc(TL); 3456} 3457void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 3458 VisitArrayTypeLoc(TL); 3459} 3460void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 3461 VisitArrayTypeLoc(TL); 3462} 3463void TypeLocReader::VisitDependentSizedArrayTypeLoc( 3464 DependentSizedArrayTypeLoc TL) { 3465 VisitArrayTypeLoc(TL); 3466} 3467void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( 3468 DependentSizedExtVectorTypeLoc TL) { 3469 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3470} 3471void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { 3472 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3473} 3474void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 3475 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3476} 3477void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 3478 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx)); 3479 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx)); 3480 TL.setTrailingReturn(Record[Idx++]); 3481 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) { 3482 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++]))); 3483 } 3484} 3485void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 3486 VisitFunctionTypeLoc(TL); 3487} 3488void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 3489 VisitFunctionTypeLoc(TL); 3490} 3491void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 3492 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3493} 3494void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 3495 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3496} 3497void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 3498 TL.setTypeofLoc(ReadSourceLocation(Record, Idx)); 3499 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 3500 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 3501} 3502void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 3503 TL.setTypeofLoc(ReadSourceLocation(Record, Idx)); 3504 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 3505 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 3506 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx)); 3507} 3508void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 3509 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3510} 3511void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) { 3512 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3513} 3514void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { 3515 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3516} 3517void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { 3518 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3519} 3520void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 3521 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx)); 3522 if (TL.hasAttrOperand()) { 3523 SourceRange range; 3524 range.setBegin(ReadSourceLocation(Record, Idx)); 3525 range.setEnd(ReadSourceLocation(Record, Idx)); 3526 TL.setAttrOperandParensRange(range); 3527 } 3528 if (TL.hasAttrExprOperand()) { 3529 if (Record[Idx++]) 3530 TL.setAttrExprOperand(Reader.ReadExpr(F)); 3531 else 3532 TL.setAttrExprOperand(0); 3533 } else if (TL.hasAttrEnumOperand()) 3534 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx)); 3535} 3536void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 3537 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3538} 3539void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( 3540 SubstTemplateTypeParmTypeLoc TL) { 3541 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3542} 3543void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc( 3544 SubstTemplateTypeParmPackTypeLoc TL) { 3545 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3546} 3547void TypeLocReader::VisitTemplateSpecializationTypeLoc( 3548 TemplateSpecializationTypeLoc TL) { 3549 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx)); 3550 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 3551 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 3552 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 3553 TL.setArgLocInfo(i, 3554 Reader.GetTemplateArgumentLocInfo(F, 3555 TL.getTypePtr()->getArg(i).getKind(), 3556 Record, Idx)); 3557} 3558void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) { 3559 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 3560 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 3561} 3562void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 3563 TL.setKeywordLoc(ReadSourceLocation(Record, Idx)); 3564 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx)); 3565} 3566void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 3567 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3568} 3569void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 3570 TL.setKeywordLoc(ReadSourceLocation(Record, Idx)); 3571 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx)); 3572 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3573} 3574void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( 3575 DependentTemplateSpecializationTypeLoc TL) { 3576 TL.setKeywordLoc(ReadSourceLocation(Record, Idx)); 3577 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx)); 3578 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3579 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 3580 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 3581 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 3582 TL.setArgLocInfo(I, 3583 Reader.GetTemplateArgumentLocInfo(F, 3584 TL.getTypePtr()->getArg(I).getKind(), 3585 Record, Idx)); 3586} 3587void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 3588 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx)); 3589} 3590void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 3591 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3592} 3593void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 3594 TL.setHasBaseTypeAsWritten(Record[Idx++]); 3595 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 3596 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 3597 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 3598 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx)); 3599} 3600void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 3601 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 3602} 3603 3604TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F, 3605 const RecordData &Record, 3606 unsigned &Idx) { 3607 QualType InfoTy = GetType(Record[Idx++]); 3608 if (InfoTy.isNull()) 3609 return 0; 3610 3611 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy); 3612 TypeLocReader TLR(*this, F, Record, Idx); 3613 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc()) 3614 TLR.Visit(TL); 3615 return TInfo; 3616} 3617 3618QualType ASTReader::GetType(TypeID ID) { 3619 unsigned FastQuals = ID & Qualifiers::FastMask; 3620 unsigned Index = ID >> Qualifiers::FastWidth; 3621 3622 if (Index < NUM_PREDEF_TYPE_IDS) { 3623 QualType T; 3624 switch ((PredefinedTypeIDs)Index) { 3625 case PREDEF_TYPE_NULL_ID: return QualType(); 3626 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break; 3627 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break; 3628 3629 case PREDEF_TYPE_CHAR_U_ID: 3630 case PREDEF_TYPE_CHAR_S_ID: 3631 // FIXME: Check that the signedness of CharTy is correct! 3632 T = Context->CharTy; 3633 break; 3634 3635 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break; 3636 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break; 3637 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break; 3638 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break; 3639 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break; 3640 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break; 3641 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break; 3642 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break; 3643 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break; 3644 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break; 3645 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break; 3646 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break; 3647 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break; 3648 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break; 3649 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break; 3650 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break; 3651 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break; 3652 case PREDEF_TYPE_BOUND_MEMBER: T = Context->BoundMemberTy; break; 3653 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break; 3654 case PREDEF_TYPE_UNKNOWN_ANY: T = Context->UnknownAnyTy; break; 3655 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break; 3656 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break; 3657 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break; 3658 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break; 3659 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break; 3660 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break; 3661 } 3662 3663 assert(!T.isNull() && "Unknown predefined type"); 3664 return T.withFastQualifiers(FastQuals); 3665 } 3666 3667 Index -= NUM_PREDEF_TYPE_IDS; 3668 assert(Index < TypesLoaded.size() && "Type index out-of-range"); 3669 if (TypesLoaded[Index].isNull()) { 3670 TypesLoaded[Index] = ReadTypeRecord(Index); 3671 if (TypesLoaded[Index].isNull()) 3672 return QualType(); 3673 3674 TypesLoaded[Index]->setFromAST(); 3675 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID); 3676 if (DeserializationListener) 3677 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID), 3678 TypesLoaded[Index]); 3679 } 3680 3681 return TypesLoaded[Index].withFastQualifiers(FastQuals); 3682} 3683 3684TypeID ASTReader::GetTypeID(QualType T) const { 3685 return MakeTypeID(T, 3686 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this)); 3687} 3688 3689TypeIdx ASTReader::GetTypeIdx(QualType T) const { 3690 if (T.isNull()) 3691 return TypeIdx(); 3692 assert(!T.getLocalFastQualifiers()); 3693 3694 TypeIdxMap::const_iterator I = TypeIdxs.find(T); 3695 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and 3696 // comparing keys of ASTDeclContextNameLookupTable. 3697 // If the type didn't come from the AST file use a specially marked index 3698 // so that any hash/key comparison fail since no such index is stored 3699 // in a AST file. 3700 if (I == TypeIdxs.end()) 3701 return TypeIdx(-1); 3702 return I->second; 3703} 3704 3705unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const { 3706 unsigned Result = 0; 3707 for (unsigned I = 0, N = Chain.size(); I != N; ++I) 3708 Result += Chain[I]->LocalNumCXXBaseSpecifiers; 3709 3710 return Result; 3711} 3712 3713TemplateArgumentLocInfo 3714ASTReader::GetTemplateArgumentLocInfo(PerFileData &F, 3715 TemplateArgument::ArgKind Kind, 3716 const RecordData &Record, 3717 unsigned &Index) { 3718 switch (Kind) { 3719 case TemplateArgument::Expression: 3720 return ReadExpr(F); 3721 case TemplateArgument::Type: 3722 return GetTypeSourceInfo(F, Record, Index); 3723 case TemplateArgument::Template: { 3724 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, 3725 Index); 3726 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index); 3727 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 3728 SourceLocation()); 3729 } 3730 case TemplateArgument::TemplateExpansion: { 3731 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, 3732 Index); 3733 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index); 3734 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index); 3735 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 3736 EllipsisLoc); 3737 } 3738 case TemplateArgument::Null: 3739 case TemplateArgument::Integral: 3740 case TemplateArgument::Declaration: 3741 case TemplateArgument::Pack: 3742 return TemplateArgumentLocInfo(); 3743 } 3744 llvm_unreachable("unexpected template argument loc"); 3745 return TemplateArgumentLocInfo(); 3746} 3747 3748TemplateArgumentLoc 3749ASTReader::ReadTemplateArgumentLoc(PerFileData &F, 3750 const RecordData &Record, unsigned &Index) { 3751 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index); 3752 3753 if (Arg.getKind() == TemplateArgument::Expression) { 3754 if (Record[Index++]) // bool InfoHasSameExpr. 3755 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); 3756 } 3757 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(), 3758 Record, Index)); 3759} 3760 3761Decl *ASTReader::GetExternalDecl(uint32_t ID) { 3762 return GetDecl(ID); 3763} 3764 3765uint64_t 3766ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) { 3767 if (ID == 0) 3768 return 0; 3769 3770 --ID; 3771 uint64_t Offset = 0; 3772 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 3773 PerFileData &F = *Chain[N - I - 1]; 3774 3775 if (ID < F.LocalNumCXXBaseSpecifiers) 3776 return Offset + F.CXXBaseSpecifiersOffsets[ID]; 3777 3778 ID -= F.LocalNumCXXBaseSpecifiers; 3779 Offset += F.SizeInBits; 3780 } 3781 3782 assert(false && "CXXBaseSpecifiers not found"); 3783 return 0; 3784} 3785 3786CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) { 3787 // Figure out which AST file contains this offset. 3788 PerFileData *F = 0; 3789 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 3790 if (Offset < Chain[N - I - 1]->SizeInBits) { 3791 F = Chain[N - I - 1]; 3792 break; 3793 } 3794 3795 Offset -= Chain[N - I - 1]->SizeInBits; 3796 } 3797 3798 if (!F) { 3799 Error("Malformed AST file: C++ base specifiers at impossible offset"); 3800 return 0; 3801 } 3802 3803 llvm::BitstreamCursor &Cursor = F->DeclsCursor; 3804 SavedStreamPosition SavedPosition(Cursor); 3805 Cursor.JumpToBit(Offset); 3806 ReadingKindTracker ReadingKind(Read_Decl, *this); 3807 RecordData Record; 3808 unsigned Code = Cursor.ReadCode(); 3809 unsigned RecCode = Cursor.ReadRecord(Code, Record); 3810 if (RecCode != DECL_CXX_BASE_SPECIFIERS) { 3811 Error("Malformed AST file: missing C++ base specifiers"); 3812 return 0; 3813 } 3814 3815 unsigned Idx = 0; 3816 unsigned NumBases = Record[Idx++]; 3817 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases); 3818 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases]; 3819 for (unsigned I = 0; I != NumBases; ++I) 3820 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx); 3821 return Bases; 3822} 3823 3824TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() { 3825 if (!DeclsLoaded[0]) { 3826 ReadDeclRecord(0, 1); 3827 if (DeserializationListener) 3828 DeserializationListener->DeclRead(1, DeclsLoaded[0]); 3829 } 3830 3831 return cast<TranslationUnitDecl>(DeclsLoaded[0]); 3832} 3833 3834Decl *ASTReader::GetDecl(DeclID ID) { 3835 if (ID == 0) 3836 return 0; 3837 3838 if (ID > DeclsLoaded.size()) { 3839 Error("declaration ID out-of-range for AST file"); 3840 return 0; 3841 } 3842 3843 unsigned Index = ID - 1; 3844 if (!DeclsLoaded[Index]) { 3845 ReadDeclRecord(Index, ID); 3846 if (DeserializationListener) 3847 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); 3848 } 3849 3850 return DeclsLoaded[Index]; 3851} 3852 3853/// \brief Resolve the offset of a statement into a statement. 3854/// 3855/// This operation will read a new statement from the external 3856/// source each time it is called, and is meant to be used via a 3857/// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 3858Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) { 3859 // Switch case IDs are per Decl. 3860 ClearSwitchCaseIDs(); 3861 3862 // Offset here is a global offset across the entire chain. 3863 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 3864 PerFileData &F = *Chain[N - I - 1]; 3865 if (Offset < F.SizeInBits) { 3866 // Since we know that this statement is part of a decl, make sure to use 3867 // the decl cursor to read it. 3868 F.DeclsCursor.JumpToBit(Offset); 3869 return ReadStmtFromStream(F); 3870 } 3871 Offset -= F.SizeInBits; 3872 } 3873 llvm_unreachable("Broken chain"); 3874} 3875 3876bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC, 3877 bool (*isKindWeWant)(Decl::Kind), 3878 llvm::SmallVectorImpl<Decl*> &Decls) { 3879 assert(DC->hasExternalLexicalStorage() && 3880 "DeclContext has no lexical decls in storage"); 3881 3882 // There might be lexical decls in multiple parts of the chain, for the TU 3883 // at least. 3884 // DeclContextOffsets might reallocate as we load additional decls below, 3885 // so make a copy of the vector. 3886 DeclContextInfos Infos = DeclContextOffsets[DC]; 3887 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end(); 3888 I != E; ++I) { 3889 // IDs can be 0 if this context doesn't contain declarations. 3890 if (!I->LexicalDecls) 3891 continue; 3892 3893 // Load all of the declaration IDs 3894 for (const KindDeclIDPair *ID = I->LexicalDecls, 3895 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) { 3896 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first)) 3897 continue; 3898 3899 Decl *D = GetDecl(ID->second); 3900 assert(D && "Null decl in lexical decls"); 3901 Decls.push_back(D); 3902 } 3903 } 3904 3905 ++NumLexicalDeclContextsRead; 3906 return false; 3907} 3908 3909DeclContext::lookup_result 3910ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC, 3911 DeclarationName Name) { 3912 assert(DC->hasExternalVisibleStorage() && 3913 "DeclContext has no visible decls in storage"); 3914 if (!Name) 3915 return DeclContext::lookup_result(DeclContext::lookup_iterator(0), 3916 DeclContext::lookup_iterator(0)); 3917 3918 llvm::SmallVector<NamedDecl *, 64> Decls; 3919 // There might be visible decls in multiple parts of the chain, for the TU 3920 // and namespaces. For any given name, the last available results replace 3921 // all earlier ones. For this reason, we walk in reverse. 3922 DeclContextInfos &Infos = DeclContextOffsets[DC]; 3923 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend(); 3924 I != E; ++I) { 3925 if (!I->NameLookupTableData) 3926 continue; 3927 3928 ASTDeclContextNameLookupTable *LookupTable = 3929 (ASTDeclContextNameLookupTable*)I->NameLookupTableData; 3930 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name); 3931 if (Pos == LookupTable->end()) 3932 continue; 3933 3934 ASTDeclContextNameLookupTrait::data_type Data = *Pos; 3935 for (; Data.first != Data.second; ++Data.first) 3936 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first))); 3937 break; 3938 } 3939 3940 ++NumVisibleDeclContextsRead; 3941 3942 SetExternalVisibleDeclsForName(DC, Name, Decls); 3943 return const_cast<DeclContext*>(DC)->lookup(Name); 3944} 3945 3946void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) { 3947 assert(DC->hasExternalVisibleStorage() && 3948 "DeclContext has no visible decls in storage"); 3949 3950 llvm::SmallVector<NamedDecl *, 64> Decls; 3951 // There might be visible decls in multiple parts of the chain, for the TU 3952 // and namespaces. 3953 DeclContextInfos &Infos = DeclContextOffsets[DC]; 3954 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end(); 3955 I != E; ++I) { 3956 if (!I->NameLookupTableData) 3957 continue; 3958 3959 ASTDeclContextNameLookupTable *LookupTable = 3960 (ASTDeclContextNameLookupTable*)I->NameLookupTableData; 3961 for (ASTDeclContextNameLookupTable::item_iterator 3962 ItemI = LookupTable->item_begin(), 3963 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) { 3964 ASTDeclContextNameLookupTable::item_iterator::value_type Val 3965 = *ItemI; 3966 ASTDeclContextNameLookupTrait::data_type Data = Val.second; 3967 Decls.clear(); 3968 for (; Data.first != Data.second; ++Data.first) 3969 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first))); 3970 MaterializeVisibleDeclsForName(DC, Val.first, Decls); 3971 } 3972 } 3973} 3974 3975void ASTReader::PassInterestingDeclsToConsumer() { 3976 assert(Consumer); 3977 while (!InterestingDecls.empty()) { 3978 DeclGroupRef DG(InterestingDecls.front()); 3979 InterestingDecls.pop_front(); 3980 Consumer->HandleInterestingDecl(DG); 3981 } 3982} 3983 3984void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) { 3985 this->Consumer = Consumer; 3986 3987 if (!Consumer) 3988 return; 3989 3990 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) { 3991 // Force deserialization of this decl, which will cause it to be queued for 3992 // passing to the consumer. 3993 GetDecl(ExternalDefinitions[I]); 3994 } 3995 3996 PassInterestingDeclsToConsumer(); 3997} 3998 3999void ASTReader::PrintStats() { 4000 std::fprintf(stderr, "*** AST File Statistics:\n"); 4001 4002 unsigned NumTypesLoaded 4003 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), 4004 QualType()); 4005 unsigned NumDeclsLoaded 4006 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), 4007 (Decl *)0); 4008 unsigned NumIdentifiersLoaded 4009 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), 4010 IdentifiersLoaded.end(), 4011 (IdentifierInfo *)0); 4012 unsigned NumSelectorsLoaded 4013 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), 4014 SelectorsLoaded.end(), 4015 Selector()); 4016 4017 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits); 4018 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses); 4019 if (TotalNumSLocEntries) 4020 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", 4021 NumSLocEntriesRead, TotalNumSLocEntries, 4022 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); 4023 if (!TypesLoaded.empty()) 4024 std::fprintf(stderr, " %u/%u types read (%f%%)\n", 4025 NumTypesLoaded, (unsigned)TypesLoaded.size(), 4026 ((float)NumTypesLoaded/TypesLoaded.size() * 100)); 4027 if (!DeclsLoaded.empty()) 4028 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", 4029 NumDeclsLoaded, (unsigned)DeclsLoaded.size(), 4030 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); 4031 if (!IdentifiersLoaded.empty()) 4032 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", 4033 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), 4034 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); 4035 if (!SelectorsLoaded.empty()) 4036 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", 4037 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(), 4038 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100)); 4039 if (TotalNumStatements) 4040 std::fprintf(stderr, " %u/%u statements read (%f%%)\n", 4041 NumStatementsRead, TotalNumStatements, 4042 ((float)NumStatementsRead/TotalNumStatements * 100)); 4043 if (TotalNumMacros) 4044 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 4045 NumMacrosRead, TotalNumMacros, 4046 ((float)NumMacrosRead/TotalNumMacros * 100)); 4047 if (TotalLexicalDeclContexts) 4048 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", 4049 NumLexicalDeclContextsRead, TotalLexicalDeclContexts, 4050 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts 4051 * 100)); 4052 if (TotalVisibleDeclContexts) 4053 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", 4054 NumVisibleDeclContextsRead, TotalVisibleDeclContexts, 4055 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts 4056 * 100)); 4057 if (TotalNumMethodPoolEntries) { 4058 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", 4059 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries, 4060 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries 4061 * 100)); 4062 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses); 4063 } 4064 std::fprintf(stderr, "\n"); 4065} 4066 4067/// Return the amount of memory used by memory buffers, breaking down 4068/// by heap-backed versus mmap'ed memory. 4069void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const { 4070 for (unsigned i = 0, e = Chain.size(); i != e; ++i) 4071 if (llvm::MemoryBuffer *buf = Chain[i]->Buffer.get()) { 4072 size_t bytes = buf->getBufferSize(); 4073 switch (buf->getBufferKind()) { 4074 case llvm::MemoryBuffer::MemoryBuffer_Malloc: 4075 sizes.malloc_bytes += bytes; 4076 break; 4077 case llvm::MemoryBuffer::MemoryBuffer_MMap: 4078 sizes.mmap_bytes += bytes; 4079 break; 4080 } 4081 } 4082} 4083 4084void ASTReader::InitializeSema(Sema &S) { 4085 SemaObj = &S; 4086 S.ExternalSource = this; 4087 4088 // Makes sure any declarations that were deserialized "too early" 4089 // still get added to the identifier's declaration chains. 4090 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) { 4091 if (SemaObj->TUScope) 4092 SemaObj->TUScope->AddDecl(PreloadedDecls[I]); 4093 4094 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]); 4095 } 4096 PreloadedDecls.clear(); 4097 4098 // If there were any tentative definitions, deserialize them and add 4099 // them to Sema's list of tentative definitions. 4100 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { 4101 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I])); 4102 SemaObj->TentativeDefinitions.push_back(Var); 4103 } 4104 4105 // If there were any unused file scoped decls, deserialize them and add to 4106 // Sema's list of unused file scoped decls. 4107 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) { 4108 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I])); 4109 SemaObj->UnusedFileScopedDecls.push_back(D); 4110 } 4111 4112 // If there were any delegating constructors, add them to Sema's list 4113 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) { 4114 CXXConstructorDecl *D 4115 = cast<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I])); 4116 SemaObj->DelegatingCtorDecls.push_back(D); 4117 } 4118 4119 // If there were any locally-scoped external declarations, 4120 // deserialize them and add them to Sema's table of locally-scoped 4121 // external declarations. 4122 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) { 4123 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I])); 4124 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D; 4125 } 4126 4127 // If there were any ext_vector type declarations, deserialize them 4128 // and add them to Sema's vector of such declarations. 4129 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) 4130 SemaObj->ExtVectorDecls.push_back( 4131 cast<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]))); 4132 4133 // FIXME: Do VTable uses and dynamic classes deserialize too much ? 4134 // Can we cut them down before writing them ? 4135 4136 // If there were any dynamic classes declarations, deserialize them 4137 // and add them to Sema's vector of such declarations. 4138 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) 4139 SemaObj->DynamicClasses.push_back( 4140 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I]))); 4141 4142 // Load the offsets of the declarations that Sema references. 4143 // They will be lazily deserialized when needed. 4144 if (!SemaDeclRefs.empty()) { 4145 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!"); 4146 SemaObj->StdNamespace = SemaDeclRefs[0]; 4147 SemaObj->StdBadAlloc = SemaDeclRefs[1]; 4148 } 4149 4150 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) { 4151 4152 // If there are @selector references added them to its pool. This is for 4153 // implementation of -Wselector. 4154 if (!F->ReferencedSelectorsData.empty()) { 4155 unsigned int DataSize = F->ReferencedSelectorsData.size()-1; 4156 unsigned I = 0; 4157 while (I < DataSize) { 4158 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]); 4159 SourceLocation SelLoc = ReadSourceLocation( 4160 *F, F->ReferencedSelectorsData, I); 4161 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc)); 4162 } 4163 } 4164 } 4165 4166 // The special data sets below always come from the most recent PCH, 4167 // which is at the front of the chain. 4168 PerFileData &F = *Chain.front(); 4169 4170 // If there were any pending implicit instantiations, deserialize them 4171 // and add them to Sema's queue of such instantiations. 4172 assert(F.PendingInstantiations.size() % 2 == 0 && 4173 "Expected pairs of entries"); 4174 for (unsigned Idx = 0, N = F.PendingInstantiations.size(); Idx < N;) { 4175 ValueDecl *D=cast<ValueDecl>(GetDecl(F.PendingInstantiations[Idx++])); 4176 SourceLocation Loc = ReadSourceLocation(F, F.PendingInstantiations,Idx); 4177 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc)); 4178 } 4179 4180 // If there were any weak undeclared identifiers, deserialize them and add to 4181 // Sema's list of weak undeclared identifiers. 4182 if (!WeakUndeclaredIdentifiers.empty()) { 4183 unsigned Idx = 0; 4184 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) { 4185 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx); 4186 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx); 4187 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx); 4188 bool Used = WeakUndeclaredIdentifiers[Idx++]; 4189 Sema::WeakInfo WI(AliasId, Loc); 4190 WI.setUsed(Used); 4191 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI)); 4192 } 4193 } 4194 4195 // If there were any VTable uses, deserialize the information and add it 4196 // to Sema's vector and map of VTable uses. 4197 if (!VTableUses.empty()) { 4198 unsigned Idx = 0; 4199 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) { 4200 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); 4201 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx); 4202 bool DefinitionRequired = VTableUses[Idx++]; 4203 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc)); 4204 SemaObj->VTablesUsed[Class] = DefinitionRequired; 4205 } 4206 } 4207 4208 if (!FPPragmaOptions.empty()) { 4209 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS"); 4210 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0]; 4211 } 4212 4213 if (!OpenCLExtensions.empty()) { 4214 unsigned I = 0; 4215#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++]; 4216#include "clang/Basic/OpenCLExtensions.def" 4217 4218 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS"); 4219 } 4220} 4221 4222IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) { 4223 // Try to find this name within our on-disk hash tables. We start with the 4224 // most recent one, since that one contains the most up-to-date info. 4225 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 4226 ASTIdentifierLookupTable *IdTable 4227 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable; 4228 if (!IdTable) 4229 continue; 4230 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart); 4231 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key); 4232 if (Pos == IdTable->end()) 4233 continue; 4234 4235 // Dereferencing the iterator has the effect of building the 4236 // IdentifierInfo node and populating it with the various 4237 // declarations it needs. 4238 return *Pos; 4239 } 4240 return 0; 4241} 4242 4243namespace clang { 4244 /// \brief An identifier-lookup iterator that enumerates all of the 4245 /// identifiers stored within a set of AST files. 4246 class ASTIdentifierIterator : public IdentifierIterator { 4247 /// \brief The AST reader whose identifiers are being enumerated. 4248 const ASTReader &Reader; 4249 4250 /// \brief The current index into the chain of AST files stored in 4251 /// the AST reader. 4252 unsigned Index; 4253 4254 /// \brief The current position within the identifier lookup table 4255 /// of the current AST file. 4256 ASTIdentifierLookupTable::key_iterator Current; 4257 4258 /// \brief The end position within the identifier lookup table of 4259 /// the current AST file. 4260 ASTIdentifierLookupTable::key_iterator End; 4261 4262 public: 4263 explicit ASTIdentifierIterator(const ASTReader &Reader); 4264 4265 virtual llvm::StringRef Next(); 4266 }; 4267} 4268 4269ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader) 4270 : Reader(Reader), Index(Reader.Chain.size() - 1) { 4271 ASTIdentifierLookupTable *IdTable 4272 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable; 4273 Current = IdTable->key_begin(); 4274 End = IdTable->key_end(); 4275} 4276 4277llvm::StringRef ASTIdentifierIterator::Next() { 4278 while (Current == End) { 4279 // If we have exhausted all of our AST files, we're done. 4280 if (Index == 0) 4281 return llvm::StringRef(); 4282 4283 --Index; 4284 ASTIdentifierLookupTable *IdTable 4285 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable; 4286 Current = IdTable->key_begin(); 4287 End = IdTable->key_end(); 4288 } 4289 4290 // We have any identifiers remaining in the current AST file; return 4291 // the next one. 4292 std::pair<const char*, unsigned> Key = *Current; 4293 ++Current; 4294 return llvm::StringRef(Key.first, Key.second); 4295} 4296 4297IdentifierIterator *ASTReader::getIdentifiers() const { 4298 return new ASTIdentifierIterator(*this); 4299} 4300 4301std::pair<ObjCMethodList, ObjCMethodList> 4302ASTReader::ReadMethodPool(Selector Sel) { 4303 // Find this selector in a hash table. We want to find the most recent entry. 4304 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 4305 PerFileData &F = *Chain[I]; 4306 if (!F.SelectorLookupTable) 4307 continue; 4308 4309 ASTSelectorLookupTable *PoolTable 4310 = (ASTSelectorLookupTable*)F.SelectorLookupTable; 4311 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel); 4312 if (Pos != PoolTable->end()) { 4313 ++NumSelectorsRead; 4314 // FIXME: Not quite happy with the statistics here. We probably should 4315 // disable this tracking when called via LoadSelector. 4316 // Also, should entries without methods count as misses? 4317 ++NumMethodPoolEntriesRead; 4318 ASTSelectorLookupTrait::data_type Data = *Pos; 4319 if (DeserializationListener) 4320 DeserializationListener->SelectorRead(Data.ID, Sel); 4321 return std::make_pair(Data.Instance, Data.Factory); 4322 } 4323 } 4324 4325 ++NumMethodPoolMisses; 4326 return std::pair<ObjCMethodList, ObjCMethodList>(); 4327} 4328 4329void ASTReader::LoadSelector(Selector Sel) { 4330 // It would be complicated to avoid reading the methods anyway. So don't. 4331 ReadMethodPool(Sel); 4332} 4333 4334void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) { 4335 assert(ID && "Non-zero identifier ID required"); 4336 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); 4337 IdentifiersLoaded[ID - 1] = II; 4338 if (DeserializationListener) 4339 DeserializationListener->IdentifierRead(ID, II); 4340} 4341 4342/// \brief Set the globally-visible declarations associated with the given 4343/// identifier. 4344/// 4345/// If the AST reader is currently in a state where the given declaration IDs 4346/// cannot safely be resolved, they are queued until it is safe to resolve 4347/// them. 4348/// 4349/// \param II an IdentifierInfo that refers to one or more globally-visible 4350/// declarations. 4351/// 4352/// \param DeclIDs the set of declaration IDs with the name @p II that are 4353/// visible at global scope. 4354/// 4355/// \param Nonrecursive should be true to indicate that the caller knows that 4356/// this call is non-recursive, and therefore the globally-visible declarations 4357/// will not be placed onto the pending queue. 4358void 4359ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, 4360 const llvm::SmallVectorImpl<uint32_t> &DeclIDs, 4361 bool Nonrecursive) { 4362 if (NumCurrentElementsDeserializing && !Nonrecursive) { 4363 PendingIdentifierInfos.push_back(PendingIdentifierInfo()); 4364 PendingIdentifierInfo &PII = PendingIdentifierInfos.back(); 4365 PII.II = II; 4366 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end()); 4367 return; 4368 } 4369 4370 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { 4371 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); 4372 if (SemaObj) { 4373 if (SemaObj->TUScope) { 4374 // Introduce this declaration into the translation-unit scope 4375 // and add it to the declaration chain for this identifier, so 4376 // that (unqualified) name lookup will find it. 4377 SemaObj->TUScope->AddDecl(D); 4378 } 4379 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D); 4380 } else { 4381 // Queue this declaration so that it will be added to the 4382 // translation unit scope and identifier's declaration chain 4383 // once a Sema object is known. 4384 PreloadedDecls.push_back(D); 4385 } 4386 } 4387} 4388 4389IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) { 4390 if (ID == 0) 4391 return 0; 4392 4393 if (IdentifiersLoaded.empty()) { 4394 Error("no identifier table in AST file"); 4395 return 0; 4396 } 4397 4398 assert(PP && "Forgot to set Preprocessor ?"); 4399 ID -= 1; 4400 if (!IdentifiersLoaded[ID]) { 4401 unsigned Index = ID; 4402 const char *Str = 0; 4403 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 4404 PerFileData *F = Chain[N - I - 1]; 4405 if (Index < F->LocalNumIdentifiers) { 4406 uint32_t Offset = F->IdentifierOffsets[Index]; 4407 Str = F->IdentifierTableData + Offset; 4408 break; 4409 } 4410 Index -= F->LocalNumIdentifiers; 4411 } 4412 assert(Str && "Broken Chain"); 4413 4414 // All of the strings in the AST file are preceded by a 16-bit length. 4415 // Extract that 16-bit length to avoid having to execute strlen(). 4416 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as 4417 // unsigned integers. This is important to avoid integer overflow when 4418 // we cast them to 'unsigned'. 4419 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2; 4420 unsigned StrLen = (((unsigned) StrLenPtr[0]) 4421 | (((unsigned) StrLenPtr[1]) << 8)) - 1; 4422 IdentifiersLoaded[ID] 4423 = &PP->getIdentifierTable().get(Str, StrLen); 4424 if (DeserializationListener) 4425 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]); 4426 } 4427 4428 return IdentifiersLoaded[ID]; 4429} 4430 4431bool ASTReader::ReadSLocEntry(unsigned ID) { 4432 return ReadSLocEntryRecord(ID) != Success; 4433} 4434 4435Selector ASTReader::DecodeSelector(unsigned ID) { 4436 if (ID == 0) 4437 return Selector(); 4438 4439 if (ID > SelectorsLoaded.size()) { 4440 Error("selector ID out of range in AST file"); 4441 return Selector(); 4442 } 4443 4444 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) { 4445 // Load this selector from the selector table. 4446 unsigned Idx = ID - 1; 4447 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 4448 PerFileData &F = *Chain[N - I - 1]; 4449 if (Idx < F.LocalNumSelectors) { 4450 ASTSelectorLookupTrait Trait(*this); 4451 SelectorsLoaded[ID - 1] = 4452 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0); 4453 if (DeserializationListener) 4454 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); 4455 break; 4456 } 4457 Idx -= F.LocalNumSelectors; 4458 } 4459 } 4460 4461 return SelectorsLoaded[ID - 1]; 4462} 4463 4464Selector ASTReader::GetExternalSelector(uint32_t ID) { 4465 return DecodeSelector(ID); 4466} 4467 4468uint32_t ASTReader::GetNumExternalSelectors() { 4469 // ID 0 (the null selector) is considered an external selector. 4470 return getTotalNumSelectors() + 1; 4471} 4472 4473DeclarationName 4474ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) { 4475 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++]; 4476 switch (Kind) { 4477 case DeclarationName::Identifier: 4478 return DeclarationName(GetIdentifierInfo(Record, Idx)); 4479 4480 case DeclarationName::ObjCZeroArgSelector: 4481 case DeclarationName::ObjCOneArgSelector: 4482 case DeclarationName::ObjCMultiArgSelector: 4483 return DeclarationName(GetSelector(Record, Idx)); 4484 4485 case DeclarationName::CXXConstructorName: 4486 return Context->DeclarationNames.getCXXConstructorName( 4487 Context->getCanonicalType(GetType(Record[Idx++]))); 4488 4489 case DeclarationName::CXXDestructorName: 4490 return Context->DeclarationNames.getCXXDestructorName( 4491 Context->getCanonicalType(GetType(Record[Idx++]))); 4492 4493 case DeclarationName::CXXConversionFunctionName: 4494 return Context->DeclarationNames.getCXXConversionFunctionName( 4495 Context->getCanonicalType(GetType(Record[Idx++]))); 4496 4497 case DeclarationName::CXXOperatorName: 4498 return Context->DeclarationNames.getCXXOperatorName( 4499 (OverloadedOperatorKind)Record[Idx++]); 4500 4501 case DeclarationName::CXXLiteralOperatorName: 4502 return Context->DeclarationNames.getCXXLiteralOperatorName( 4503 GetIdentifierInfo(Record, Idx)); 4504 4505 case DeclarationName::CXXUsingDirective: 4506 return DeclarationName::getUsingDirectiveName(); 4507 } 4508 4509 // Required to silence GCC warning 4510 return DeclarationName(); 4511} 4512 4513void ASTReader::ReadDeclarationNameLoc(PerFileData &F, 4514 DeclarationNameLoc &DNLoc, 4515 DeclarationName Name, 4516 const RecordData &Record, unsigned &Idx) { 4517 switch (Name.getNameKind()) { 4518 case DeclarationName::CXXConstructorName: 4519 case DeclarationName::CXXDestructorName: 4520 case DeclarationName::CXXConversionFunctionName: 4521 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx); 4522 break; 4523 4524 case DeclarationName::CXXOperatorName: 4525 DNLoc.CXXOperatorName.BeginOpNameLoc 4526 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 4527 DNLoc.CXXOperatorName.EndOpNameLoc 4528 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 4529 break; 4530 4531 case DeclarationName::CXXLiteralOperatorName: 4532 DNLoc.CXXLiteralOperatorName.OpNameLoc 4533 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 4534 break; 4535 4536 case DeclarationName::Identifier: 4537 case DeclarationName::ObjCZeroArgSelector: 4538 case DeclarationName::ObjCOneArgSelector: 4539 case DeclarationName::ObjCMultiArgSelector: 4540 case DeclarationName::CXXUsingDirective: 4541 break; 4542 } 4543} 4544 4545void ASTReader::ReadDeclarationNameInfo(PerFileData &F, 4546 DeclarationNameInfo &NameInfo, 4547 const RecordData &Record, unsigned &Idx) { 4548 NameInfo.setName(ReadDeclarationName(Record, Idx)); 4549 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx)); 4550 DeclarationNameLoc DNLoc; 4551 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx); 4552 NameInfo.setInfo(DNLoc); 4553} 4554 4555void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info, 4556 const RecordData &Record, unsigned &Idx) { 4557 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx); 4558 unsigned NumTPLists = Record[Idx++]; 4559 Info.NumTemplParamLists = NumTPLists; 4560 if (NumTPLists) { 4561 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists]; 4562 for (unsigned i=0; i != NumTPLists; ++i) 4563 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx); 4564 } 4565} 4566 4567TemplateName 4568ASTReader::ReadTemplateName(PerFileData &F, const RecordData &Record, 4569 unsigned &Idx) { 4570 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++]; 4571 switch (Kind) { 4572 case TemplateName::Template: 4573 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++]))); 4574 4575 case TemplateName::OverloadedTemplate: { 4576 unsigned size = Record[Idx++]; 4577 UnresolvedSet<8> Decls; 4578 while (size--) 4579 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++]))); 4580 4581 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end()); 4582 } 4583 4584 case TemplateName::QualifiedTemplate: { 4585 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); 4586 bool hasTemplKeyword = Record[Idx++]; 4587 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++])); 4588 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template); 4589 } 4590 4591 case TemplateName::DependentTemplate: { 4592 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); 4593 if (Record[Idx++]) // isIdentifier 4594 return Context->getDependentTemplateName(NNS, 4595 GetIdentifierInfo(Record, Idx)); 4596 return Context->getDependentTemplateName(NNS, 4597 (OverloadedOperatorKind)Record[Idx++]); 4598 } 4599 4600 case TemplateName::SubstTemplateTemplateParmPack: { 4601 TemplateTemplateParmDecl *Param 4602 = cast_or_null<TemplateTemplateParmDecl>(GetDecl(Record[Idx++])); 4603 if (!Param) 4604 return TemplateName(); 4605 4606 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx); 4607 if (ArgPack.getKind() != TemplateArgument::Pack) 4608 return TemplateName(); 4609 4610 return Context->getSubstTemplateTemplateParmPack(Param, ArgPack); 4611 } 4612 } 4613 4614 assert(0 && "Unhandled template name kind!"); 4615 return TemplateName(); 4616} 4617 4618TemplateArgument 4619ASTReader::ReadTemplateArgument(PerFileData &F, 4620 const RecordData &Record, unsigned &Idx) { 4621 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++]; 4622 switch (Kind) { 4623 case TemplateArgument::Null: 4624 return TemplateArgument(); 4625 case TemplateArgument::Type: 4626 return TemplateArgument(GetType(Record[Idx++])); 4627 case TemplateArgument::Declaration: 4628 return TemplateArgument(GetDecl(Record[Idx++])); 4629 case TemplateArgument::Integral: { 4630 llvm::APSInt Value = ReadAPSInt(Record, Idx); 4631 QualType T = GetType(Record[Idx++]); 4632 return TemplateArgument(Value, T); 4633 } 4634 case TemplateArgument::Template: 4635 return TemplateArgument(ReadTemplateName(F, Record, Idx)); 4636 case TemplateArgument::TemplateExpansion: { 4637 TemplateName Name = ReadTemplateName(F, Record, Idx); 4638 llvm::Optional<unsigned> NumTemplateExpansions; 4639 if (unsigned NumExpansions = Record[Idx++]) 4640 NumTemplateExpansions = NumExpansions - 1; 4641 return TemplateArgument(Name, NumTemplateExpansions); 4642 } 4643 case TemplateArgument::Expression: 4644 return TemplateArgument(ReadExpr(F)); 4645 case TemplateArgument::Pack: { 4646 unsigned NumArgs = Record[Idx++]; 4647 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs]; 4648 for (unsigned I = 0; I != NumArgs; ++I) 4649 Args[I] = ReadTemplateArgument(F, Record, Idx); 4650 return TemplateArgument(Args, NumArgs); 4651 } 4652 } 4653 4654 assert(0 && "Unhandled template argument kind!"); 4655 return TemplateArgument(); 4656} 4657 4658TemplateParameterList * 4659ASTReader::ReadTemplateParameterList(PerFileData &F, 4660 const RecordData &Record, unsigned &Idx) { 4661 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx); 4662 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx); 4663 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx); 4664 4665 unsigned NumParams = Record[Idx++]; 4666 llvm::SmallVector<NamedDecl *, 16> Params; 4667 Params.reserve(NumParams); 4668 while (NumParams--) 4669 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++]))); 4670 4671 TemplateParameterList* TemplateParams = 4672 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc, 4673 Params.data(), Params.size(), RAngleLoc); 4674 return TemplateParams; 4675} 4676 4677void 4678ASTReader:: 4679ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs, 4680 PerFileData &F, const RecordData &Record, 4681 unsigned &Idx) { 4682 unsigned NumTemplateArgs = Record[Idx++]; 4683 TemplArgs.reserve(NumTemplateArgs); 4684 while (NumTemplateArgs--) 4685 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx)); 4686} 4687 4688/// \brief Read a UnresolvedSet structure. 4689void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set, 4690 const RecordData &Record, unsigned &Idx) { 4691 unsigned NumDecls = Record[Idx++]; 4692 while (NumDecls--) { 4693 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++])); 4694 AccessSpecifier AS = (AccessSpecifier)Record[Idx++]; 4695 Set.addDecl(D, AS); 4696 } 4697} 4698 4699CXXBaseSpecifier 4700ASTReader::ReadCXXBaseSpecifier(PerFileData &F, 4701 const RecordData &Record, unsigned &Idx) { 4702 bool isVirtual = static_cast<bool>(Record[Idx++]); 4703 bool isBaseOfClass = static_cast<bool>(Record[Idx++]); 4704 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]); 4705 bool inheritConstructors = static_cast<bool>(Record[Idx++]); 4706 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx); 4707 SourceRange Range = ReadSourceRange(F, Record, Idx); 4708 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx); 4709 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo, 4710 EllipsisLoc); 4711 Result.setInheritConstructors(inheritConstructors); 4712 return Result; 4713} 4714 4715std::pair<CXXCtorInitializer **, unsigned> 4716ASTReader::ReadCXXCtorInitializers(PerFileData &F, const RecordData &Record, 4717 unsigned &Idx) { 4718 CXXCtorInitializer **CtorInitializers = 0; 4719 unsigned NumInitializers = Record[Idx++]; 4720 if (NumInitializers) { 4721 ASTContext &C = *getContext(); 4722 4723 CtorInitializers 4724 = new (C) CXXCtorInitializer*[NumInitializers]; 4725 for (unsigned i=0; i != NumInitializers; ++i) { 4726 TypeSourceInfo *BaseClassInfo = 0; 4727 bool IsBaseVirtual = false; 4728 FieldDecl *Member = 0; 4729 IndirectFieldDecl *IndirectMember = 0; 4730 CXXConstructorDecl *Target = 0; 4731 4732 CtorInitializerType Type = (CtorInitializerType)Record[Idx++]; 4733 switch (Type) { 4734 case CTOR_INITIALIZER_BASE: 4735 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx); 4736 IsBaseVirtual = Record[Idx++]; 4737 break; 4738 4739 case CTOR_INITIALIZER_DELEGATING: 4740 Target = cast<CXXConstructorDecl>(GetDecl(Record[Idx++])); 4741 break; 4742 4743 case CTOR_INITIALIZER_MEMBER: 4744 Member = cast<FieldDecl>(GetDecl(Record[Idx++])); 4745 break; 4746 4747 case CTOR_INITIALIZER_INDIRECT_MEMBER: 4748 IndirectMember = cast<IndirectFieldDecl>(GetDecl(Record[Idx++])); 4749 break; 4750 } 4751 4752 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx); 4753 Expr *Init = ReadExpr(F); 4754 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx); 4755 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx); 4756 bool IsWritten = Record[Idx++]; 4757 unsigned SourceOrderOrNumArrayIndices; 4758 llvm::SmallVector<VarDecl *, 8> Indices; 4759 if (IsWritten) { 4760 SourceOrderOrNumArrayIndices = Record[Idx++]; 4761 } else { 4762 SourceOrderOrNumArrayIndices = Record[Idx++]; 4763 Indices.reserve(SourceOrderOrNumArrayIndices); 4764 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i) 4765 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++]))); 4766 } 4767 4768 CXXCtorInitializer *BOMInit; 4769 if (Type == CTOR_INITIALIZER_BASE) { 4770 BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual, 4771 LParenLoc, Init, RParenLoc, 4772 MemberOrEllipsisLoc); 4773 } else if (Type == CTOR_INITIALIZER_DELEGATING) { 4774 BOMInit = new (C) CXXCtorInitializer(C, MemberOrEllipsisLoc, LParenLoc, 4775 Target, Init, RParenLoc); 4776 } else if (IsWritten) { 4777 if (Member) 4778 BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc, 4779 LParenLoc, Init, RParenLoc); 4780 else 4781 BOMInit = new (C) CXXCtorInitializer(C, IndirectMember, 4782 MemberOrEllipsisLoc, LParenLoc, 4783 Init, RParenLoc); 4784 } else { 4785 BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc, 4786 LParenLoc, Init, RParenLoc, 4787 Indices.data(), Indices.size()); 4788 } 4789 4790 if (IsWritten) 4791 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices); 4792 CtorInitializers[i] = BOMInit; 4793 } 4794 } 4795 4796 return std::make_pair(CtorInitializers, NumInitializers); 4797} 4798 4799NestedNameSpecifier * 4800ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) { 4801 unsigned N = Record[Idx++]; 4802 NestedNameSpecifier *NNS = 0, *Prev = 0; 4803 for (unsigned I = 0; I != N; ++I) { 4804 NestedNameSpecifier::SpecifierKind Kind 4805 = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; 4806 switch (Kind) { 4807 case NestedNameSpecifier::Identifier: { 4808 IdentifierInfo *II = GetIdentifierInfo(Record, Idx); 4809 NNS = NestedNameSpecifier::Create(*Context, Prev, II); 4810 break; 4811 } 4812 4813 case NestedNameSpecifier::Namespace: { 4814 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++])); 4815 NNS = NestedNameSpecifier::Create(*Context, Prev, NS); 4816 break; 4817 } 4818 4819 case NestedNameSpecifier::NamespaceAlias: { 4820 NamespaceAliasDecl *Alias 4821 = cast<NamespaceAliasDecl>(GetDecl(Record[Idx++])); 4822 NNS = NestedNameSpecifier::Create(*Context, Prev, Alias); 4823 break; 4824 } 4825 4826 case NestedNameSpecifier::TypeSpec: 4827 case NestedNameSpecifier::TypeSpecWithTemplate: { 4828 const Type *T = GetType(Record[Idx++]).getTypePtrOrNull(); 4829 if (!T) 4830 return 0; 4831 4832 bool Template = Record[Idx++]; 4833 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T); 4834 break; 4835 } 4836 4837 case NestedNameSpecifier::Global: { 4838 NNS = NestedNameSpecifier::GlobalSpecifier(*Context); 4839 // No associated value, and there can't be a prefix. 4840 break; 4841 } 4842 } 4843 Prev = NNS; 4844 } 4845 return NNS; 4846} 4847 4848NestedNameSpecifierLoc 4849ASTReader::ReadNestedNameSpecifierLoc(PerFileData &F, const RecordData &Record, 4850 unsigned &Idx) { 4851 unsigned N = Record[Idx++]; 4852 NestedNameSpecifierLocBuilder Builder; 4853 for (unsigned I = 0; I != N; ++I) { 4854 NestedNameSpecifier::SpecifierKind Kind 4855 = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; 4856 switch (Kind) { 4857 case NestedNameSpecifier::Identifier: { 4858 IdentifierInfo *II = GetIdentifierInfo(Record, Idx); 4859 SourceRange Range = ReadSourceRange(F, Record, Idx); 4860 Builder.Extend(*Context, II, Range.getBegin(), Range.getEnd()); 4861 break; 4862 } 4863 4864 case NestedNameSpecifier::Namespace: { 4865 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++])); 4866 SourceRange Range = ReadSourceRange(F, Record, Idx); 4867 Builder.Extend(*Context, NS, Range.getBegin(), Range.getEnd()); 4868 break; 4869 } 4870 4871 case NestedNameSpecifier::NamespaceAlias: { 4872 NamespaceAliasDecl *Alias 4873 = cast<NamespaceAliasDecl>(GetDecl(Record[Idx++])); 4874 SourceRange Range = ReadSourceRange(F, Record, Idx); 4875 Builder.Extend(*Context, Alias, Range.getBegin(), Range.getEnd()); 4876 break; 4877 } 4878 4879 case NestedNameSpecifier::TypeSpec: 4880 case NestedNameSpecifier::TypeSpecWithTemplate: { 4881 bool Template = Record[Idx++]; 4882 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx); 4883 if (!T) 4884 return NestedNameSpecifierLoc(); 4885 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx); 4886 4887 // FIXME: 'template' keyword location not saved anywhere, so we fake it. 4888 Builder.Extend(*Context, 4889 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(), 4890 T->getTypeLoc(), ColonColonLoc); 4891 break; 4892 } 4893 4894 case NestedNameSpecifier::Global: { 4895 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx); 4896 Builder.MakeGlobal(*Context, ColonColonLoc); 4897 break; 4898 } 4899 } 4900 } 4901 4902 return Builder.getWithLocInContext(*Context); 4903} 4904 4905SourceRange 4906ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record, 4907 unsigned &Idx) { 4908 SourceLocation beg = ReadSourceLocation(F, Record, Idx); 4909 SourceLocation end = ReadSourceLocation(F, Record, Idx); 4910 return SourceRange(beg, end); 4911} 4912 4913/// \brief Read an integral value 4914llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) { 4915 unsigned BitWidth = Record[Idx++]; 4916 unsigned NumWords = llvm::APInt::getNumWords(BitWidth); 4917 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]); 4918 Idx += NumWords; 4919 return Result; 4920} 4921 4922/// \brief Read a signed integral value 4923llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) { 4924 bool isUnsigned = Record[Idx++]; 4925 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned); 4926} 4927 4928/// \brief Read a floating-point value 4929llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) { 4930 return llvm::APFloat(ReadAPInt(Record, Idx)); 4931} 4932 4933// \brief Read a string 4934std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) { 4935 unsigned Len = Record[Idx++]; 4936 std::string Result(Record.data() + Idx, Record.data() + Idx + Len); 4937 Idx += Len; 4938 return Result; 4939} 4940 4941VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record, 4942 unsigned &Idx) { 4943 unsigned Major = Record[Idx++]; 4944 unsigned Minor = Record[Idx++]; 4945 unsigned Subminor = Record[Idx++]; 4946 if (Minor == 0) 4947 return VersionTuple(Major); 4948 if (Subminor == 0) 4949 return VersionTuple(Major, Minor - 1); 4950 return VersionTuple(Major, Minor - 1, Subminor - 1); 4951} 4952 4953CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record, 4954 unsigned &Idx) { 4955 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++])); 4956 return CXXTemporary::Create(*Context, Decl); 4957} 4958 4959DiagnosticBuilder ASTReader::Diag(unsigned DiagID) { 4960 return Diag(SourceLocation(), DiagID); 4961} 4962 4963DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) { 4964 return Diags.Report(Loc, DiagID); 4965} 4966 4967/// \brief Retrieve the identifier table associated with the 4968/// preprocessor. 4969IdentifierTable &ASTReader::getIdentifierTable() { 4970 assert(PP && "Forgot to set Preprocessor ?"); 4971 return PP->getIdentifierTable(); 4972} 4973 4974/// \brief Record that the given ID maps to the given switch-case 4975/// statement. 4976void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { 4977 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID"); 4978 SwitchCaseStmts[ID] = SC; 4979} 4980 4981/// \brief Retrieve the switch-case statement with the given ID. 4982SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) { 4983 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID"); 4984 return SwitchCaseStmts[ID]; 4985} 4986 4987void ASTReader::ClearSwitchCaseIDs() { 4988 SwitchCaseStmts.clear(); 4989} 4990 4991void ASTReader::FinishedDeserializing() { 4992 assert(NumCurrentElementsDeserializing && 4993 "FinishedDeserializing not paired with StartedDeserializing"); 4994 if (NumCurrentElementsDeserializing == 1) { 4995 // If any identifiers with corresponding top-level declarations have 4996 // been loaded, load those declarations now. 4997 while (!PendingIdentifierInfos.empty()) { 4998 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II, 4999 PendingIdentifierInfos.front().DeclIDs, true); 5000 PendingIdentifierInfos.pop_front(); 5001 } 5002 5003 // Ready to load previous declarations of Decls that were delayed. 5004 while (!PendingPreviousDecls.empty()) { 5005 loadAndAttachPreviousDecl(PendingPreviousDecls.front().first, 5006 PendingPreviousDecls.front().second); 5007 PendingPreviousDecls.pop_front(); 5008 } 5009 5010 // We are not in recursive loading, so it's safe to pass the "interesting" 5011 // decls to the consumer. 5012 if (Consumer) 5013 PassInterestingDeclsToConsumer(); 5014 5015 assert(PendingForwardRefs.size() == 0 && 5016 "Some forward refs did not get linked to the definition!"); 5017 } 5018 --NumCurrentElementsDeserializing; 5019} 5020 5021ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context, 5022 const char *isysroot, bool DisableValidation, 5023 bool DisableStatCache) 5024 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0), 5025 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), 5026 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context), 5027 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation), 5028 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0), 5029 NumSLocEntriesRead(0), TotalNumSLocEntries(0), NextSLocOffset(0), 5030 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0), 5031 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0), 5032 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0), 5033 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0), 5034 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0), 5035 NumCurrentElementsDeserializing(0) 5036{ 5037 RelocatablePCH = false; 5038} 5039 5040ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr, 5041 Diagnostic &Diags, const char *isysroot, 5042 bool DisableValidation, bool DisableStatCache) 5043 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr), 5044 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0), 5045 isysroot(isysroot), DisableValidation(DisableValidation), 5046 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0), 5047 NumSLocEntriesRead(0), TotalNumSLocEntries(0), 5048 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0), 5049 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0), 5050 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0), 5051 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0), 5052 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0), 5053 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) { 5054 RelocatablePCH = false; 5055} 5056 5057ASTReader::~ASTReader() { 5058 for (unsigned i = 0, e = Chain.size(); i != e; ++i) 5059 delete Chain[e - i - 1]; 5060 // Delete all visible decl lookup tables 5061 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(), 5062 E = DeclContextOffsets.end(); 5063 I != E; ++I) { 5064 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end(); 5065 J != F; ++J) { 5066 if (J->NameLookupTableData) 5067 delete static_cast<ASTDeclContextNameLookupTable*>( 5068 J->NameLookupTableData); 5069 } 5070 } 5071 for (DeclContextVisibleUpdatesPending::iterator 5072 I = PendingVisibleUpdates.begin(), 5073 E = PendingVisibleUpdates.end(); 5074 I != E; ++I) { 5075 for (DeclContextVisibleUpdates::iterator J = I->second.begin(), 5076 F = I->second.end(); 5077 J != F; ++J) 5078 delete static_cast<ASTDeclContextNameLookupTable*>(*J); 5079 } 5080} 5081 5082ASTReader::PerFileData::PerFileData(ASTFileType Ty) 5083 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0), 5084 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0), 5085 IdentifierLookupTable(0), LocalNumMacroDefinitions(0), 5086 MacroDefinitionOffsets(0), 5087 LocalNumHeaderFileInfos(0), HeaderFileInfoTableData(0), 5088 HeaderFileInfoTable(0), 5089 LocalNumSelectors(0), SelectorOffsets(0), 5090 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0), 5091 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0), 5092 LocalNumTypes(0), TypeOffsets(0), StatCache(0), 5093 NumPreallocatedPreprocessingEntities(0), NextInSource(0) 5094{} 5095 5096ASTReader::PerFileData::~PerFileData() { 5097 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable); 5098 delete static_cast<HeaderFileInfoLookupTable *>(HeaderFileInfoTable); 5099 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable); 5100} 5101