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