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