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