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