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