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