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