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