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