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