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