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