ASTReader.cpp revision c3632730cc83ed7b51f0ab5c38997ae5a9439b0c
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. 1750 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen))) { 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 switch(ReadASTCore(FileName)) { 2030 case Failure: return Failure; 2031 case IgnorePCH: return IgnorePCH; 2032 case Success: break; 2033 } 2034 2035 // Here comes stuff that we only do once the entire chain is loaded. 2036 2037 // Allocate space for loaded slocentries, identifiers, decls and types. 2038 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0, 2039 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0, 2040 TotalNumSelectors = 0; 2041 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 2042 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries; 2043 NextSLocOffset += Chain[I]->LocalSLocSize; 2044 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers; 2045 TotalNumTypes += Chain[I]->LocalNumTypes; 2046 TotalNumDecls += Chain[I]->LocalNumDecls; 2047 TotalNumPreallocatedPreprocessingEntities += 2048 Chain[I]->NumPreallocatedPreprocessingEntities; 2049 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions; 2050 TotalNumSelectors += Chain[I]->LocalNumSelectors; 2051 } 2052 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset); 2053 IdentifiersLoaded.resize(TotalNumIdentifiers); 2054 TypesLoaded.resize(TotalNumTypes); 2055 DeclsLoaded.resize(TotalNumDecls); 2056 MacroDefinitionsLoaded.resize(TotalNumMacroDefs); 2057 if (PP) { 2058 if (TotalNumIdentifiers > 0) 2059 PP->getHeaderSearchInfo().SetExternalLookup(this); 2060 if (TotalNumPreallocatedPreprocessingEntities > 0) { 2061 if (!PP->getPreprocessingRecord()) 2062 PP->createPreprocessingRecord(); 2063 PP->getPreprocessingRecord()->SetExternalSource(*this, 2064 TotalNumPreallocatedPreprocessingEntities); 2065 } 2066 } 2067 SelectorsLoaded.resize(TotalNumSelectors); 2068 // Preload SLocEntries. 2069 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) { 2070 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]); 2071 if (Result != Success) 2072 return Result; 2073 } 2074 2075 // Check the predefines buffers. 2076 if (!DisableValidation && CheckPredefinesBuffers()) 2077 return IgnorePCH; 2078 2079 if (PP) { 2080 // Initialization of keywords and pragmas occurs before the 2081 // AST file is read, so there may be some identifiers that were 2082 // loaded into the IdentifierTable before we intercepted the 2083 // creation of identifiers. Iterate through the list of known 2084 // identifiers and determine whether we have to establish 2085 // preprocessor definitions or top-level identifier declaration 2086 // chains for those identifiers. 2087 // 2088 // We copy the IdentifierInfo pointers to a small vector first, 2089 // since de-serializing declarations or macro definitions can add 2090 // new entries into the identifier table, invalidating the 2091 // iterators. 2092 llvm::SmallVector<IdentifierInfo *, 128> Identifiers; 2093 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(), 2094 IdEnd = PP->getIdentifierTable().end(); 2095 Id != IdEnd; ++Id) 2096 Identifiers.push_back(Id->second); 2097 // We need to search the tables in all files. 2098 for (unsigned J = 0, M = Chain.size(); J != M; ++J) { 2099 ASTIdentifierLookupTable *IdTable 2100 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable; 2101 // Not all AST files necessarily have identifier tables, only the useful 2102 // ones. 2103 if (!IdTable) 2104 continue; 2105 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) { 2106 IdentifierInfo *II = Identifiers[I]; 2107 // Look in the on-disk hash tables for an entry for this identifier 2108 ASTIdentifierLookupTrait Info(*this, *Chain[J], II); 2109 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength()); 2110 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info); 2111 if (Pos == IdTable->end()) 2112 continue; 2113 2114 // Dereferencing the iterator has the effect of populating the 2115 // IdentifierInfo node with the various declarations it needs. 2116 (void)*Pos; 2117 } 2118 } 2119 } 2120 2121 if (Context) 2122 InitializeContext(*Context); 2123 2124 return Success; 2125} 2126 2127ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName) { 2128 PerFileData *Prev = Chain.empty() ? 0 : Chain.back(); 2129 Chain.push_back(new PerFileData()); 2130 PerFileData &F = *Chain.back(); 2131 if (Prev) 2132 Prev->NextInSource = &F; 2133 else 2134 FirstInSource = &F; 2135 F.Loaders.push_back(Prev); 2136 2137 // Set the AST file name. 2138 F.FileName = FileName; 2139 2140 // Open the AST file. 2141 // 2142 // FIXME: This shouldn't be here, we should just take a raw_ostream. 2143 std::string ErrStr; 2144 F.Buffer.reset(llvm::MemoryBuffer::getFileOrSTDIN(FileName, &ErrStr)); 2145 if (!F.Buffer) { 2146 Error(ErrStr.c_str()); 2147 return IgnorePCH; 2148 } 2149 2150 // Initialize the stream 2151 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(), 2152 (const unsigned char *)F.Buffer->getBufferEnd()); 2153 llvm::BitstreamCursor &Stream = F.Stream; 2154 Stream.init(F.StreamFile); 2155 F.SizeInBits = F.Buffer->getBufferSize() * 8; 2156 2157 // Sniff for the signature. 2158 if (Stream.Read(8) != 'C' || 2159 Stream.Read(8) != 'P' || 2160 Stream.Read(8) != 'C' || 2161 Stream.Read(8) != 'H') { 2162 Diag(diag::err_not_a_pch_file) << FileName; 2163 return Failure; 2164 } 2165 2166 while (!Stream.AtEndOfStream()) { 2167 unsigned Code = Stream.ReadCode(); 2168 2169 if (Code != llvm::bitc::ENTER_SUBBLOCK) { 2170 Error("invalid record at top-level of AST file"); 2171 return Failure; 2172 } 2173 2174 unsigned BlockID = Stream.ReadSubBlockID(); 2175 2176 // We only know the AST subblock ID. 2177 switch (BlockID) { 2178 case llvm::bitc::BLOCKINFO_BLOCK_ID: 2179 if (Stream.ReadBlockInfoBlock()) { 2180 Error("malformed BlockInfoBlock in AST file"); 2181 return Failure; 2182 } 2183 break; 2184 case AST_BLOCK_ID: 2185 switch (ReadASTBlock(F)) { 2186 case Success: 2187 break; 2188 2189 case Failure: 2190 return Failure; 2191 2192 case IgnorePCH: 2193 // FIXME: We could consider reading through to the end of this 2194 // AST block, skipping subblocks, to see if there are other 2195 // AST blocks elsewhere. 2196 2197 // Clear out any preallocated source location entries, so that 2198 // the source manager does not try to resolve them later. 2199 SourceMgr.ClearPreallocatedSLocEntries(); 2200 2201 // Remove the stat cache. 2202 if (F.StatCache) 2203 FileMgr.removeStatCache((ASTStatCache*)F.StatCache); 2204 2205 return IgnorePCH; 2206 } 2207 break; 2208 default: 2209 if (Stream.SkipBlock()) { 2210 Error("malformed block record in AST file"); 2211 return Failure; 2212 } 2213 break; 2214 } 2215 } 2216 2217 return Success; 2218} 2219 2220void ASTReader::setPreprocessor(Preprocessor &pp) { 2221 PP = &pp; 2222 2223 unsigned TotalNum = 0; 2224 for (unsigned I = 0, N = Chain.size(); I != N; ++I) 2225 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities; 2226 if (TotalNum) { 2227 if (!PP->getPreprocessingRecord()) 2228 PP->createPreprocessingRecord(); 2229 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum); 2230 } 2231} 2232 2233void ASTReader::InitializeContext(ASTContext &Ctx) { 2234 Context = &Ctx; 2235 assert(Context && "Passed null context!"); 2236 2237 assert(PP && "Forgot to set Preprocessor ?"); 2238 PP->getIdentifierTable().setExternalIdentifierLookup(this); 2239 PP->getHeaderSearchInfo().SetExternalLookup(this); 2240 PP->setExternalSource(this); 2241 2242 // If we have an update block for the TU waiting, we have to add it before 2243 // deserializing the decl. 2244 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0); 2245 if (DCU != DeclContextOffsets.end()) { 2246 // Insertion could invalidate map, so grab vector. 2247 DeclContextInfos T; 2248 T.swap(DCU->second); 2249 DeclContextOffsets.erase(DCU); 2250 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T); 2251 } 2252 2253 // Load the translation unit declaration 2254 GetTranslationUnitDecl(); 2255 2256 // Load the special types. 2257 Context->setBuiltinVaListType( 2258 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST])); 2259 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID]) 2260 Context->setObjCIdType(GetType(Id)); 2261 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR]) 2262 Context->setObjCSelType(GetType(Sel)); 2263 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL]) 2264 Context->setObjCProtoType(GetType(Proto)); 2265 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS]) 2266 Context->setObjCClassType(GetType(Class)); 2267 2268 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) 2269 Context->setCFConstantStringType(GetType(String)); 2270 if (unsigned FastEnum 2271 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE]) 2272 Context->setObjCFastEnumerationStateType(GetType(FastEnum)); 2273 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) { 2274 QualType FileType = GetType(File); 2275 if (FileType.isNull()) { 2276 Error("FILE type is NULL"); 2277 return; 2278 } 2279 if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) 2280 Context->setFILEDecl(Typedef->getDecl()); 2281 else { 2282 const TagType *Tag = FileType->getAs<TagType>(); 2283 if (!Tag) { 2284 Error("Invalid FILE type in AST file"); 2285 return; 2286 } 2287 Context->setFILEDecl(Tag->getDecl()); 2288 } 2289 } 2290 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) { 2291 QualType Jmp_bufType = GetType(Jmp_buf); 2292 if (Jmp_bufType.isNull()) { 2293 Error("jmp_bug type is NULL"); 2294 return; 2295 } 2296 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) 2297 Context->setjmp_bufDecl(Typedef->getDecl()); 2298 else { 2299 const TagType *Tag = Jmp_bufType->getAs<TagType>(); 2300 if (!Tag) { 2301 Error("Invalid jmp_buf type in AST file"); 2302 return; 2303 } 2304 Context->setjmp_bufDecl(Tag->getDecl()); 2305 } 2306 } 2307 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) { 2308 QualType Sigjmp_bufType = GetType(Sigjmp_buf); 2309 if (Sigjmp_bufType.isNull()) { 2310 Error("sigjmp_buf type is NULL"); 2311 return; 2312 } 2313 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) 2314 Context->setsigjmp_bufDecl(Typedef->getDecl()); 2315 else { 2316 const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); 2317 assert(Tag && "Invalid sigjmp_buf type in AST file"); 2318 Context->setsigjmp_bufDecl(Tag->getDecl()); 2319 } 2320 } 2321 if (unsigned ObjCIdRedef 2322 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) 2323 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef); 2324 if (unsigned ObjCClassRedef 2325 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) 2326 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef); 2327 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR]) 2328 Context->setBlockDescriptorType(GetType(String)); 2329 if (unsigned String 2330 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR]) 2331 Context->setBlockDescriptorExtendedType(GetType(String)); 2332 if (unsigned ObjCSelRedef 2333 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) 2334 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef); 2335 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING]) 2336 Context->setNSConstantStringType(GetType(String)); 2337 2338 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED]) 2339 Context->setInt128Installed(); 2340} 2341 2342/// \brief Retrieve the name of the original source file name 2343/// directly from the AST file, without actually loading the AST 2344/// file. 2345std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName, 2346 Diagnostic &Diags) { 2347 // Open the AST file. 2348 std::string ErrStr; 2349 llvm::OwningPtr<llvm::MemoryBuffer> Buffer; 2350 Buffer.reset(llvm::MemoryBuffer::getFile(ASTFileName.c_str(), &ErrStr)); 2351 if (!Buffer) { 2352 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr; 2353 return std::string(); 2354 } 2355 2356 // Initialize the stream 2357 llvm::BitstreamReader StreamFile; 2358 llvm::BitstreamCursor Stream; 2359 StreamFile.init((const unsigned char *)Buffer->getBufferStart(), 2360 (const unsigned char *)Buffer->getBufferEnd()); 2361 Stream.init(StreamFile); 2362 2363 // Sniff for the signature. 2364 if (Stream.Read(8) != 'C' || 2365 Stream.Read(8) != 'P' || 2366 Stream.Read(8) != 'C' || 2367 Stream.Read(8) != 'H') { 2368 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName; 2369 return std::string(); 2370 } 2371 2372 RecordData Record; 2373 while (!Stream.AtEndOfStream()) { 2374 unsigned Code = Stream.ReadCode(); 2375 2376 if (Code == llvm::bitc::ENTER_SUBBLOCK) { 2377 unsigned BlockID = Stream.ReadSubBlockID(); 2378 2379 // We only know the AST subblock ID. 2380 switch (BlockID) { 2381 case AST_BLOCK_ID: 2382 if (Stream.EnterSubBlock(AST_BLOCK_ID)) { 2383 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 2384 return std::string(); 2385 } 2386 break; 2387 2388 default: 2389 if (Stream.SkipBlock()) { 2390 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 2391 return std::string(); 2392 } 2393 break; 2394 } 2395 continue; 2396 } 2397 2398 if (Code == llvm::bitc::END_BLOCK) { 2399 if (Stream.ReadBlockEnd()) { 2400 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName; 2401 return std::string(); 2402 } 2403 continue; 2404 } 2405 2406 if (Code == llvm::bitc::DEFINE_ABBREV) { 2407 Stream.ReadAbbrevRecord(); 2408 continue; 2409 } 2410 2411 Record.clear(); 2412 const char *BlobStart = 0; 2413 unsigned BlobLen = 0; 2414 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen) 2415 == ORIGINAL_FILE_NAME) 2416 return std::string(BlobStart, BlobLen); 2417 } 2418 2419 return std::string(); 2420} 2421 2422/// \brief Parse the record that corresponds to a LangOptions data 2423/// structure. 2424/// 2425/// This routine parses the language options from the AST file and then gives 2426/// them to the AST listener if one is set. 2427/// 2428/// \returns true if the listener deems the file unacceptable, false otherwise. 2429bool ASTReader::ParseLanguageOptions( 2430 const llvm::SmallVectorImpl<uint64_t> &Record) { 2431 if (Listener) { 2432 LangOptions LangOpts; 2433 2434 #define PARSE_LANGOPT(Option) \ 2435 LangOpts.Option = Record[Idx]; \ 2436 ++Idx 2437 2438 unsigned Idx = 0; 2439 PARSE_LANGOPT(Trigraphs); 2440 PARSE_LANGOPT(BCPLComment); 2441 PARSE_LANGOPT(DollarIdents); 2442 PARSE_LANGOPT(AsmPreprocessor); 2443 PARSE_LANGOPT(GNUMode); 2444 PARSE_LANGOPT(GNUKeywords); 2445 PARSE_LANGOPT(ImplicitInt); 2446 PARSE_LANGOPT(Digraphs); 2447 PARSE_LANGOPT(HexFloats); 2448 PARSE_LANGOPT(C99); 2449 PARSE_LANGOPT(Microsoft); 2450 PARSE_LANGOPT(CPlusPlus); 2451 PARSE_LANGOPT(CPlusPlus0x); 2452 PARSE_LANGOPT(CXXOperatorNames); 2453 PARSE_LANGOPT(ObjC1); 2454 PARSE_LANGOPT(ObjC2); 2455 PARSE_LANGOPT(ObjCNonFragileABI); 2456 PARSE_LANGOPT(ObjCNonFragileABI2); 2457 PARSE_LANGOPT(NoConstantCFStrings); 2458 PARSE_LANGOPT(PascalStrings); 2459 PARSE_LANGOPT(WritableStrings); 2460 PARSE_LANGOPT(LaxVectorConversions); 2461 PARSE_LANGOPT(AltiVec); 2462 PARSE_LANGOPT(Exceptions); 2463 PARSE_LANGOPT(SjLjExceptions); 2464 PARSE_LANGOPT(NeXTRuntime); 2465 PARSE_LANGOPT(Freestanding); 2466 PARSE_LANGOPT(NoBuiltin); 2467 PARSE_LANGOPT(ThreadsafeStatics); 2468 PARSE_LANGOPT(POSIXThreads); 2469 PARSE_LANGOPT(Blocks); 2470 PARSE_LANGOPT(EmitAllDecls); 2471 PARSE_LANGOPT(MathErrno); 2472 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy) 2473 Record[Idx++]); 2474 PARSE_LANGOPT(HeinousExtensions); 2475 PARSE_LANGOPT(Optimize); 2476 PARSE_LANGOPT(OptimizeSize); 2477 PARSE_LANGOPT(Static); 2478 PARSE_LANGOPT(PICLevel); 2479 PARSE_LANGOPT(GNUInline); 2480 PARSE_LANGOPT(NoInline); 2481 PARSE_LANGOPT(AccessControl); 2482 PARSE_LANGOPT(CharIsSigned); 2483 PARSE_LANGOPT(ShortWChar); 2484 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]); 2485 LangOpts.setVisibilityMode((LangOptions::VisibilityMode)Record[Idx++]); 2486 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode) 2487 Record[Idx++]); 2488 PARSE_LANGOPT(InstantiationDepth); 2489 PARSE_LANGOPT(OpenCL); 2490 PARSE_LANGOPT(CatchUndefined); 2491 // FIXME: Missing ElideConstructors?! 2492 #undef PARSE_LANGOPT 2493 2494 return Listener->ReadLanguageOptions(LangOpts); 2495 } 2496 2497 return false; 2498} 2499 2500void ASTReader::ReadPreprocessedEntities() { 2501 ReadDefinedMacros(); 2502} 2503 2504/// \brief Get the correct cursor and offset for loading a type. 2505ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) { 2506 PerFileData *F = 0; 2507 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 2508 F = Chain[N - I - 1]; 2509 if (Index < F->LocalNumTypes) 2510 break; 2511 Index -= F->LocalNumTypes; 2512 } 2513 assert(F && F->LocalNumTypes > Index && "Broken chain"); 2514 return RecordLocation(F, F->TypeOffsets[Index]); 2515} 2516 2517/// \brief Read and return the type with the given index.. 2518/// 2519/// The index is the type ID, shifted and minus the number of predefs. This 2520/// routine actually reads the record corresponding to the type at the given 2521/// location. It is a helper routine for GetType, which deals with reading type 2522/// IDs. 2523QualType ASTReader::ReadTypeRecord(unsigned Index) { 2524 RecordLocation Loc = TypeCursorForIndex(Index); 2525 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 2526 2527 // Keep track of where we are in the stream, then jump back there 2528 // after reading this type. 2529 SavedStreamPosition SavedPosition(DeclsCursor); 2530 2531 ReadingKindTracker ReadingKind(Read_Type, *this); 2532 2533 // Note that we are loading a type record. 2534 Deserializing AType(this); 2535 2536 DeclsCursor.JumpToBit(Loc.Offset); 2537 RecordData Record; 2538 unsigned Code = DeclsCursor.ReadCode(); 2539 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) { 2540 case TYPE_EXT_QUAL: { 2541 if (Record.size() != 2) { 2542 Error("Incorrect encoding of extended qualifier type"); 2543 return QualType(); 2544 } 2545 QualType Base = GetType(Record[0]); 2546 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]); 2547 return Context->getQualifiedType(Base, Quals); 2548 } 2549 2550 case TYPE_COMPLEX: { 2551 if (Record.size() != 1) { 2552 Error("Incorrect encoding of complex type"); 2553 return QualType(); 2554 } 2555 QualType ElemType = GetType(Record[0]); 2556 return Context->getComplexType(ElemType); 2557 } 2558 2559 case TYPE_POINTER: { 2560 if (Record.size() != 1) { 2561 Error("Incorrect encoding of pointer type"); 2562 return QualType(); 2563 } 2564 QualType PointeeType = GetType(Record[0]); 2565 return Context->getPointerType(PointeeType); 2566 } 2567 2568 case TYPE_BLOCK_POINTER: { 2569 if (Record.size() != 1) { 2570 Error("Incorrect encoding of block pointer type"); 2571 return QualType(); 2572 } 2573 QualType PointeeType = GetType(Record[0]); 2574 return Context->getBlockPointerType(PointeeType); 2575 } 2576 2577 case TYPE_LVALUE_REFERENCE: { 2578 if (Record.size() != 1) { 2579 Error("Incorrect encoding of lvalue reference type"); 2580 return QualType(); 2581 } 2582 QualType PointeeType = GetType(Record[0]); 2583 return Context->getLValueReferenceType(PointeeType); 2584 } 2585 2586 case TYPE_RVALUE_REFERENCE: { 2587 if (Record.size() != 1) { 2588 Error("Incorrect encoding of rvalue reference type"); 2589 return QualType(); 2590 } 2591 QualType PointeeType = GetType(Record[0]); 2592 return Context->getRValueReferenceType(PointeeType); 2593 } 2594 2595 case TYPE_MEMBER_POINTER: { 2596 if (Record.size() != 2) { 2597 Error("Incorrect encoding of member pointer type"); 2598 return QualType(); 2599 } 2600 QualType PointeeType = GetType(Record[0]); 2601 QualType ClassType = GetType(Record[1]); 2602 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr()); 2603 } 2604 2605 case TYPE_CONSTANT_ARRAY: { 2606 QualType ElementType = GetType(Record[0]); 2607 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 2608 unsigned IndexTypeQuals = Record[2]; 2609 unsigned Idx = 3; 2610 llvm::APInt Size = ReadAPInt(Record, Idx); 2611 return Context->getConstantArrayType(ElementType, Size, 2612 ASM, IndexTypeQuals); 2613 } 2614 2615 case TYPE_INCOMPLETE_ARRAY: { 2616 QualType ElementType = GetType(Record[0]); 2617 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 2618 unsigned IndexTypeQuals = Record[2]; 2619 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals); 2620 } 2621 2622 case TYPE_VARIABLE_ARRAY: { 2623 QualType ElementType = GetType(Record[0]); 2624 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 2625 unsigned IndexTypeQuals = Record[2]; 2626 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]); 2627 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]); 2628 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F), 2629 ASM, IndexTypeQuals, 2630 SourceRange(LBLoc, RBLoc)); 2631 } 2632 2633 case TYPE_VECTOR: { 2634 if (Record.size() != 3) { 2635 Error("incorrect encoding of vector type in AST file"); 2636 return QualType(); 2637 } 2638 2639 QualType ElementType = GetType(Record[0]); 2640 unsigned NumElements = Record[1]; 2641 unsigned AltiVecSpec = Record[2]; 2642 return Context->getVectorType(ElementType, NumElements, 2643 (VectorType::AltiVecSpecific)AltiVecSpec); 2644 } 2645 2646 case TYPE_EXT_VECTOR: { 2647 if (Record.size() != 3) { 2648 Error("incorrect encoding of extended vector type in AST file"); 2649 return QualType(); 2650 } 2651 2652 QualType ElementType = GetType(Record[0]); 2653 unsigned NumElements = Record[1]; 2654 return Context->getExtVectorType(ElementType, NumElements); 2655 } 2656 2657 case TYPE_FUNCTION_NO_PROTO: { 2658 if (Record.size() != 4) { 2659 Error("incorrect encoding of no-proto function type"); 2660 return QualType(); 2661 } 2662 QualType ResultType = GetType(Record[0]); 2663 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]); 2664 return Context->getFunctionNoProtoType(ResultType, Info); 2665 } 2666 2667 case TYPE_FUNCTION_PROTO: { 2668 QualType ResultType = GetType(Record[0]); 2669 bool NoReturn = Record[1]; 2670 unsigned RegParm = Record[2]; 2671 CallingConv CallConv = (CallingConv)Record[3]; 2672 unsigned Idx = 4; 2673 unsigned NumParams = Record[Idx++]; 2674 llvm::SmallVector<QualType, 16> ParamTypes; 2675 for (unsigned I = 0; I != NumParams; ++I) 2676 ParamTypes.push_back(GetType(Record[Idx++])); 2677 bool isVariadic = Record[Idx++]; 2678 unsigned Quals = Record[Idx++]; 2679 bool hasExceptionSpec = Record[Idx++]; 2680 bool hasAnyExceptionSpec = Record[Idx++]; 2681 unsigned NumExceptions = Record[Idx++]; 2682 llvm::SmallVector<QualType, 2> Exceptions; 2683 for (unsigned I = 0; I != NumExceptions; ++I) 2684 Exceptions.push_back(GetType(Record[Idx++])); 2685 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams, 2686 isVariadic, Quals, hasExceptionSpec, 2687 hasAnyExceptionSpec, NumExceptions, 2688 Exceptions.data(), 2689 FunctionType::ExtInfo(NoReturn, RegParm, 2690 CallConv)); 2691 } 2692 2693 case TYPE_UNRESOLVED_USING: 2694 return Context->getTypeDeclType( 2695 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0]))); 2696 2697 case TYPE_TYPEDEF: { 2698 if (Record.size() != 2) { 2699 Error("incorrect encoding of typedef type"); 2700 return QualType(); 2701 } 2702 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0])); 2703 QualType Canonical = GetType(Record[1]); 2704 return Context->getTypedefType(Decl, Canonical); 2705 } 2706 2707 case TYPE_TYPEOF_EXPR: 2708 return Context->getTypeOfExprType(ReadExpr(*Loc.F)); 2709 2710 case TYPE_TYPEOF: { 2711 if (Record.size() != 1) { 2712 Error("incorrect encoding of typeof(type) in AST file"); 2713 return QualType(); 2714 } 2715 QualType UnderlyingType = GetType(Record[0]); 2716 return Context->getTypeOfType(UnderlyingType); 2717 } 2718 2719 case TYPE_DECLTYPE: 2720 return Context->getDecltypeType(ReadExpr(*Loc.F)); 2721 2722 case TYPE_RECORD: { 2723 if (Record.size() != 2) { 2724 Error("incorrect encoding of record type"); 2725 return QualType(); 2726 } 2727 bool IsDependent = Record[0]; 2728 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1]))); 2729 T->Dependent = IsDependent; 2730 return T; 2731 } 2732 2733 case TYPE_ENUM: { 2734 if (Record.size() != 2) { 2735 Error("incorrect encoding of enum type"); 2736 return QualType(); 2737 } 2738 bool IsDependent = Record[0]; 2739 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1]))); 2740 T->Dependent = IsDependent; 2741 return T; 2742 } 2743 2744 case TYPE_ELABORATED: { 2745 unsigned Idx = 0; 2746 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 2747 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); 2748 QualType NamedType = GetType(Record[Idx++]); 2749 return Context->getElaboratedType(Keyword, NNS, NamedType); 2750 } 2751 2752 case TYPE_OBJC_INTERFACE: { 2753 unsigned Idx = 0; 2754 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++])); 2755 return Context->getObjCInterfaceType(ItfD); 2756 } 2757 2758 case TYPE_OBJC_OBJECT: { 2759 unsigned Idx = 0; 2760 QualType Base = GetType(Record[Idx++]); 2761 unsigned NumProtos = Record[Idx++]; 2762 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos; 2763 for (unsigned I = 0; I != NumProtos; ++I) 2764 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++]))); 2765 return Context->getObjCObjectType(Base, Protos.data(), NumProtos); 2766 } 2767 2768 case TYPE_OBJC_OBJECT_POINTER: { 2769 unsigned Idx = 0; 2770 QualType Pointee = GetType(Record[Idx++]); 2771 return Context->getObjCObjectPointerType(Pointee); 2772 } 2773 2774 case TYPE_SUBST_TEMPLATE_TYPE_PARM: { 2775 unsigned Idx = 0; 2776 QualType Parm = GetType(Record[Idx++]); 2777 QualType Replacement = GetType(Record[Idx++]); 2778 return 2779 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm), 2780 Replacement); 2781 } 2782 2783 case TYPE_INJECTED_CLASS_NAME: { 2784 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0])); 2785 QualType TST = GetType(Record[1]); // probably derivable 2786 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable 2787 // for AST reading, too much interdependencies. 2788 return 2789 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0); 2790 } 2791 2792 case TYPE_TEMPLATE_TYPE_PARM: { 2793 unsigned Idx = 0; 2794 unsigned Depth = Record[Idx++]; 2795 unsigned Index = Record[Idx++]; 2796 bool Pack = Record[Idx++]; 2797 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx); 2798 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name); 2799 } 2800 2801 case TYPE_DEPENDENT_NAME: { 2802 unsigned Idx = 0; 2803 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 2804 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); 2805 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx); 2806 QualType Canon = GetType(Record[Idx++]); 2807 return Context->getDependentNameType(Keyword, NNS, Name, Canon); 2808 } 2809 2810 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: { 2811 unsigned Idx = 0; 2812 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 2813 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); 2814 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx); 2815 unsigned NumArgs = Record[Idx++]; 2816 llvm::SmallVector<TemplateArgument, 8> Args; 2817 Args.reserve(NumArgs); 2818 while (NumArgs--) 2819 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx)); 2820 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name, 2821 Args.size(), Args.data()); 2822 } 2823 2824 case TYPE_DEPENDENT_SIZED_ARRAY: { 2825 unsigned Idx = 0; 2826 2827 // ArrayType 2828 QualType ElementType = GetType(Record[Idx++]); 2829 ArrayType::ArraySizeModifier ASM 2830 = (ArrayType::ArraySizeModifier)Record[Idx++]; 2831 unsigned IndexTypeQuals = Record[Idx++]; 2832 2833 // DependentSizedArrayType 2834 Expr *NumElts = ReadExpr(*Loc.F); 2835 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx); 2836 2837 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM, 2838 IndexTypeQuals, Brackets); 2839 } 2840 2841 case TYPE_TEMPLATE_SPECIALIZATION: { 2842 unsigned Idx = 0; 2843 bool IsDependent = Record[Idx++]; 2844 TemplateName Name = ReadTemplateName(Record, Idx); 2845 llvm::SmallVector<TemplateArgument, 8> Args; 2846 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx); 2847 QualType Canon = GetType(Record[Idx++]); 2848 QualType T; 2849 if (Canon.isNull()) 2850 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(), 2851 Args.size()); 2852 else 2853 T = Context->getTemplateSpecializationType(Name, Args.data(), 2854 Args.size(), Canon); 2855 T->Dependent = IsDependent; 2856 return T; 2857 } 2858 } 2859 // Suppress a GCC warning 2860 return QualType(); 2861} 2862 2863class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> { 2864 ASTReader &Reader; 2865 ASTReader::PerFileData &F; 2866 llvm::BitstreamCursor &DeclsCursor; 2867 const ASTReader::RecordData &Record; 2868 unsigned &Idx; 2869 2870 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R, 2871 unsigned &I) { 2872 return Reader.ReadSourceLocation(F, R, I); 2873 } 2874 2875public: 2876 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F, 2877 const ASTReader::RecordData &Record, unsigned &Idx) 2878 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx) 2879 { } 2880 2881 // We want compile-time assurance that we've enumerated all of 2882 // these, so unfortunately we have to declare them first, then 2883 // define them out-of-line. 2884#define ABSTRACT_TYPELOC(CLASS, PARENT) 2885#define TYPELOC(CLASS, PARENT) \ 2886 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 2887#include "clang/AST/TypeLocNodes.def" 2888 2889 void VisitFunctionTypeLoc(FunctionTypeLoc); 2890 void VisitArrayTypeLoc(ArrayTypeLoc); 2891}; 2892 2893void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 2894 // nothing to do 2895} 2896void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 2897 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx)); 2898 if (TL.needsExtraLocalData()) { 2899 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++])); 2900 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++])); 2901 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++])); 2902 TL.setModeAttr(Record[Idx++]); 2903 } 2904} 2905void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { 2906 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 2907} 2908void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { 2909 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 2910} 2911void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 2912 TL.setCaretLoc(ReadSourceLocation(Record, Idx)); 2913} 2914void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 2915 TL.setAmpLoc(ReadSourceLocation(Record, Idx)); 2916} 2917void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 2918 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx)); 2919} 2920void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 2921 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 2922} 2923void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { 2924 TL.setLBracketLoc(ReadSourceLocation(Record, Idx)); 2925 TL.setRBracketLoc(ReadSourceLocation(Record, Idx)); 2926 if (Record[Idx++]) 2927 TL.setSizeExpr(Reader.ReadExpr(F)); 2928 else 2929 TL.setSizeExpr(0); 2930} 2931void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 2932 VisitArrayTypeLoc(TL); 2933} 2934void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 2935 VisitArrayTypeLoc(TL); 2936} 2937void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 2938 VisitArrayTypeLoc(TL); 2939} 2940void TypeLocReader::VisitDependentSizedArrayTypeLoc( 2941 DependentSizedArrayTypeLoc TL) { 2942 VisitArrayTypeLoc(TL); 2943} 2944void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( 2945 DependentSizedExtVectorTypeLoc TL) { 2946 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 2947} 2948void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { 2949 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 2950} 2951void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 2952 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 2953} 2954void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 2955 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 2956 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 2957 TL.setTrailingReturn(Record[Idx++]); 2958 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) { 2959 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++]))); 2960 } 2961} 2962void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 2963 VisitFunctionTypeLoc(TL); 2964} 2965void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 2966 VisitFunctionTypeLoc(TL); 2967} 2968void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 2969 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 2970} 2971void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 2972 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 2973} 2974void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 2975 TL.setTypeofLoc(ReadSourceLocation(Record, Idx)); 2976 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 2977 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 2978} 2979void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 2980 TL.setTypeofLoc(ReadSourceLocation(Record, Idx)); 2981 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 2982 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 2983 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx)); 2984} 2985void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 2986 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 2987} 2988void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { 2989 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 2990} 2991void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { 2992 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 2993} 2994void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 2995 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 2996} 2997void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( 2998 SubstTemplateTypeParmTypeLoc TL) { 2999 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3000} 3001void TypeLocReader::VisitTemplateSpecializationTypeLoc( 3002 TemplateSpecializationTypeLoc TL) { 3003 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx)); 3004 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 3005 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 3006 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 3007 TL.setArgLocInfo(i, 3008 Reader.GetTemplateArgumentLocInfo(F, 3009 TL.getTypePtr()->getArg(i).getKind(), 3010 Record, Idx)); 3011} 3012void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 3013 TL.setKeywordLoc(ReadSourceLocation(Record, Idx)); 3014 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx)); 3015} 3016void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 3017 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3018} 3019void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 3020 TL.setKeywordLoc(ReadSourceLocation(Record, Idx)); 3021 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx)); 3022 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3023} 3024void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( 3025 DependentTemplateSpecializationTypeLoc TL) { 3026 TL.setKeywordLoc(ReadSourceLocation(Record, Idx)); 3027 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx)); 3028 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3029 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 3030 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 3031 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 3032 TL.setArgLocInfo(I, 3033 Reader.GetTemplateArgumentLocInfo(F, 3034 TL.getTypePtr()->getArg(I).getKind(), 3035 Record, Idx)); 3036} 3037void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 3038 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3039} 3040void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 3041 TL.setHasBaseTypeAsWritten(Record[Idx++]); 3042 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 3043 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 3044 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 3045 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx)); 3046} 3047void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 3048 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 3049} 3050 3051TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F, 3052 const RecordData &Record, 3053 unsigned &Idx) { 3054 QualType InfoTy = GetType(Record[Idx++]); 3055 if (InfoTy.isNull()) 3056 return 0; 3057 3058 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy); 3059 TypeLocReader TLR(*this, F, Record, Idx); 3060 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc()) 3061 TLR.Visit(TL); 3062 return TInfo; 3063} 3064 3065QualType ASTReader::GetType(TypeID ID) { 3066 unsigned FastQuals = ID & Qualifiers::FastMask; 3067 unsigned Index = ID >> Qualifiers::FastWidth; 3068 3069 if (Index < NUM_PREDEF_TYPE_IDS) { 3070 QualType T; 3071 switch ((PredefinedTypeIDs)Index) { 3072 case PREDEF_TYPE_NULL_ID: return QualType(); 3073 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break; 3074 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break; 3075 3076 case PREDEF_TYPE_CHAR_U_ID: 3077 case PREDEF_TYPE_CHAR_S_ID: 3078 // FIXME: Check that the signedness of CharTy is correct! 3079 T = Context->CharTy; 3080 break; 3081 3082 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break; 3083 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break; 3084 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break; 3085 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break; 3086 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break; 3087 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break; 3088 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break; 3089 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break; 3090 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break; 3091 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break; 3092 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break; 3093 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break; 3094 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break; 3095 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break; 3096 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break; 3097 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break; 3098 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break; 3099 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break; 3100 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break; 3101 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break; 3102 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break; 3103 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break; 3104 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break; 3105 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break; 3106 } 3107 3108 assert(!T.isNull() && "Unknown predefined type"); 3109 return T.withFastQualifiers(FastQuals); 3110 } 3111 3112 Index -= NUM_PREDEF_TYPE_IDS; 3113 assert(Index < TypesLoaded.size() && "Type index out-of-range"); 3114 if (TypesLoaded[Index].isNull()) { 3115 TypesLoaded[Index] = ReadTypeRecord(Index); 3116 TypesLoaded[Index]->setFromAST(); 3117 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID); 3118 if (DeserializationListener) 3119 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID), 3120 TypesLoaded[Index]); 3121 } 3122 3123 return TypesLoaded[Index].withFastQualifiers(FastQuals); 3124} 3125 3126TypeID ASTReader::GetTypeID(QualType T) const { 3127 return MakeTypeID(T, 3128 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this)); 3129} 3130 3131TypeIdx ASTReader::GetTypeIdx(QualType T) const { 3132 if (T.isNull()) 3133 return TypeIdx(); 3134 assert(!T.getLocalFastQualifiers()); 3135 3136 TypeIdxMap::const_iterator I = TypeIdxs.find(T); 3137 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and 3138 // comparing keys of ASTDeclContextNameLookupTable. 3139 // If the type didn't come from the AST file use a specially marked index 3140 // so that any hash/key comparison fail since no such index is stored 3141 // in a AST file. 3142 if (I == TypeIdxs.end()) 3143 return TypeIdx(-1); 3144 return I->second; 3145} 3146 3147TemplateArgumentLocInfo 3148ASTReader::GetTemplateArgumentLocInfo(PerFileData &F, 3149 TemplateArgument::ArgKind Kind, 3150 const RecordData &Record, 3151 unsigned &Index) { 3152 switch (Kind) { 3153 case TemplateArgument::Expression: 3154 return ReadExpr(F); 3155 case TemplateArgument::Type: 3156 return GetTypeSourceInfo(F, Record, Index); 3157 case TemplateArgument::Template: { 3158 SourceRange QualifierRange = ReadSourceRange(F, Record, Index); 3159 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index); 3160 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc); 3161 } 3162 case TemplateArgument::Null: 3163 case TemplateArgument::Integral: 3164 case TemplateArgument::Declaration: 3165 case TemplateArgument::Pack: 3166 return TemplateArgumentLocInfo(); 3167 } 3168 llvm_unreachable("unexpected template argument loc"); 3169 return TemplateArgumentLocInfo(); 3170} 3171 3172TemplateArgumentLoc 3173ASTReader::ReadTemplateArgumentLoc(PerFileData &F, 3174 const RecordData &Record, unsigned &Index) { 3175 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index); 3176 3177 if (Arg.getKind() == TemplateArgument::Expression) { 3178 if (Record[Index++]) // bool InfoHasSameExpr. 3179 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); 3180 } 3181 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(), 3182 Record, Index)); 3183} 3184 3185Decl *ASTReader::GetExternalDecl(uint32_t ID) { 3186 return GetDecl(ID); 3187} 3188 3189TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() { 3190 if (!DeclsLoaded[0]) { 3191 ReadDeclRecord(0, 1); 3192 if (DeserializationListener) 3193 DeserializationListener->DeclRead(1, DeclsLoaded[0]); 3194 } 3195 3196 return cast<TranslationUnitDecl>(DeclsLoaded[0]); 3197} 3198 3199Decl *ASTReader::GetDecl(DeclID ID) { 3200 if (ID == 0) 3201 return 0; 3202 3203 if (ID > DeclsLoaded.size()) { 3204 Error("declaration ID out-of-range for AST file"); 3205 return 0; 3206 } 3207 3208 unsigned Index = ID - 1; 3209 if (!DeclsLoaded[Index]) { 3210 ReadDeclRecord(Index, ID); 3211 if (DeserializationListener) 3212 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); 3213 } 3214 3215 return DeclsLoaded[Index]; 3216} 3217 3218/// \brief Resolve the offset of a statement into a statement. 3219/// 3220/// This operation will read a new statement from the external 3221/// source each time it is called, and is meant to be used via a 3222/// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 3223Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) { 3224 // Offset here is a global offset across the entire chain. 3225 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 3226 PerFileData &F = *Chain[N - I - 1]; 3227 if (Offset < F.SizeInBits) { 3228 // Since we know that this statement is part of a decl, make sure to use 3229 // the decl cursor to read it. 3230 F.DeclsCursor.JumpToBit(Offset); 3231 return ReadStmtFromStream(F); 3232 } 3233 Offset -= F.SizeInBits; 3234 } 3235 llvm_unreachable("Broken chain"); 3236} 3237 3238bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC, 3239 llvm::SmallVectorImpl<Decl*> &Decls) { 3240 assert(DC->hasExternalLexicalStorage() && 3241 "DeclContext has no lexical decls in storage"); 3242 3243 // There might be lexical decls in multiple parts of the chain, for the TU 3244 // at least. 3245 // DeclContextOffsets might reallocate as we load additional decls below, 3246 // so make a copy of the vector. 3247 DeclContextInfos Infos = DeclContextOffsets[DC]; 3248 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end(); 3249 I != E; ++I) { 3250 // IDs can be 0 if this context doesn't contain declarations. 3251 if (!I->LexicalDecls) 3252 continue; 3253 3254 // Load all of the declaration IDs 3255 for (const DeclID *ID = I->LexicalDecls, *IDE = ID + I->NumLexicalDecls; 3256 ID != IDE; ++ID) { 3257 Decl *D = GetDecl(*ID); 3258 assert(D && "Null decl in lexical decls"); 3259 Decls.push_back(D); 3260 } 3261 } 3262 3263 ++NumLexicalDeclContextsRead; 3264 return false; 3265} 3266 3267DeclContext::lookup_result 3268ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC, 3269 DeclarationName Name) { 3270 assert(DC->hasExternalVisibleStorage() && 3271 "DeclContext has no visible decls in storage"); 3272 if (!Name) 3273 return DeclContext::lookup_result(DeclContext::lookup_iterator(0), 3274 DeclContext::lookup_iterator(0)); 3275 3276 llvm::SmallVector<NamedDecl *, 64> Decls; 3277 // There might be visible decls in multiple parts of the chain, for the TU 3278 // and namespaces. For any given name, the last available results replace 3279 // all earlier ones. For this reason, we walk in reverse. 3280 DeclContextInfos &Infos = DeclContextOffsets[DC]; 3281 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend(); 3282 I != E; ++I) { 3283 if (!I->NameLookupTableData) 3284 continue; 3285 3286 ASTDeclContextNameLookupTable *LookupTable = 3287 (ASTDeclContextNameLookupTable*)I->NameLookupTableData; 3288 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name); 3289 if (Pos == LookupTable->end()) 3290 continue; 3291 3292 ASTDeclContextNameLookupTrait::data_type Data = *Pos; 3293 for (; Data.first != Data.second; ++Data.first) 3294 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first))); 3295 break; 3296 } 3297 3298 ++NumVisibleDeclContextsRead; 3299 3300 SetExternalVisibleDeclsForName(DC, Name, Decls); 3301 return const_cast<DeclContext*>(DC)->lookup(Name); 3302} 3303 3304void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) { 3305 assert(DC->hasExternalVisibleStorage() && 3306 "DeclContext has no visible decls in storage"); 3307 3308 llvm::SmallVector<NamedDecl *, 64> Decls; 3309 // There might be visible decls in multiple parts of the chain, for the TU 3310 // and namespaces. 3311 DeclContextInfos &Infos = DeclContextOffsets[DC]; 3312 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end(); 3313 I != E; ++I) { 3314 if (!I->NameLookupTableData) 3315 continue; 3316 3317 ASTDeclContextNameLookupTable *LookupTable = 3318 (ASTDeclContextNameLookupTable*)I->NameLookupTableData; 3319 for (ASTDeclContextNameLookupTable::item_iterator 3320 ItemI = LookupTable->item_begin(), 3321 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) { 3322 ASTDeclContextNameLookupTable::item_iterator::value_type Val 3323 = *ItemI; 3324 ASTDeclContextNameLookupTrait::data_type Data = Val.second; 3325 Decls.clear(); 3326 for (; Data.first != Data.second; ++Data.first) 3327 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first))); 3328 MaterializeVisibleDeclsForName(DC, Val.first, Decls); 3329 } 3330 } 3331} 3332 3333void ASTReader::PassInterestingDeclsToConsumer() { 3334 assert(Consumer); 3335 while (!InterestingDecls.empty()) { 3336 DeclGroupRef DG(InterestingDecls.front()); 3337 InterestingDecls.pop_front(); 3338 Consumer->HandleInterestingDecl(DG); 3339 } 3340} 3341 3342void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) { 3343 this->Consumer = Consumer; 3344 3345 if (!Consumer) 3346 return; 3347 3348 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) { 3349 // Force deserialization of this decl, which will cause it to be queued for 3350 // passing to the consumer. 3351 GetDecl(ExternalDefinitions[I]); 3352 } 3353 3354 PassInterestingDeclsToConsumer(); 3355} 3356 3357void ASTReader::PrintStats() { 3358 std::fprintf(stderr, "*** AST File Statistics:\n"); 3359 3360 unsigned NumTypesLoaded 3361 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), 3362 QualType()); 3363 unsigned NumDeclsLoaded 3364 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), 3365 (Decl *)0); 3366 unsigned NumIdentifiersLoaded 3367 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), 3368 IdentifiersLoaded.end(), 3369 (IdentifierInfo *)0); 3370 unsigned NumSelectorsLoaded 3371 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), 3372 SelectorsLoaded.end(), 3373 Selector()); 3374 3375 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits); 3376 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses); 3377 if (TotalNumSLocEntries) 3378 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", 3379 NumSLocEntriesRead, TotalNumSLocEntries, 3380 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); 3381 if (!TypesLoaded.empty()) 3382 std::fprintf(stderr, " %u/%u types read (%f%%)\n", 3383 NumTypesLoaded, (unsigned)TypesLoaded.size(), 3384 ((float)NumTypesLoaded/TypesLoaded.size() * 100)); 3385 if (!DeclsLoaded.empty()) 3386 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", 3387 NumDeclsLoaded, (unsigned)DeclsLoaded.size(), 3388 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); 3389 if (!IdentifiersLoaded.empty()) 3390 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", 3391 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), 3392 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); 3393 if (!SelectorsLoaded.empty()) 3394 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", 3395 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(), 3396 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100)); 3397 if (TotalNumStatements) 3398 std::fprintf(stderr, " %u/%u statements read (%f%%)\n", 3399 NumStatementsRead, TotalNumStatements, 3400 ((float)NumStatementsRead/TotalNumStatements * 100)); 3401 if (TotalNumMacros) 3402 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 3403 NumMacrosRead, TotalNumMacros, 3404 ((float)NumMacrosRead/TotalNumMacros * 100)); 3405 if (TotalLexicalDeclContexts) 3406 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", 3407 NumLexicalDeclContextsRead, TotalLexicalDeclContexts, 3408 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts 3409 * 100)); 3410 if (TotalVisibleDeclContexts) 3411 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", 3412 NumVisibleDeclContextsRead, TotalVisibleDeclContexts, 3413 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts 3414 * 100)); 3415 if (TotalNumMethodPoolEntries) { 3416 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", 3417 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries, 3418 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries 3419 * 100)); 3420 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses); 3421 } 3422 std::fprintf(stderr, "\n"); 3423} 3424 3425void ASTReader::InitializeSema(Sema &S) { 3426 SemaObj = &S; 3427 S.ExternalSource = this; 3428 3429 // Makes sure any declarations that were deserialized "too early" 3430 // still get added to the identifier's declaration chains. 3431 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) { 3432 if (SemaObj->TUScope) 3433 SemaObj->TUScope->AddDecl(PreloadedDecls[I]); 3434 3435 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]); 3436 } 3437 PreloadedDecls.clear(); 3438 3439 // If there were any tentative definitions, deserialize them and add 3440 // them to Sema's list of tentative definitions. 3441 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { 3442 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I])); 3443 SemaObj->TentativeDefinitions.push_back(Var); 3444 } 3445 3446 // If there were any unused file scoped decls, deserialize them and add to 3447 // Sema's list of unused file scoped decls. 3448 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) { 3449 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I])); 3450 SemaObj->UnusedFileScopedDecls.push_back(D); 3451 } 3452 3453 // If there were any locally-scoped external declarations, 3454 // deserialize them and add them to Sema's table of locally-scoped 3455 // external declarations. 3456 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) { 3457 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I])); 3458 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D; 3459 } 3460 3461 // If there were any ext_vector type declarations, deserialize them 3462 // and add them to Sema's vector of such declarations. 3463 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) 3464 SemaObj->ExtVectorDecls.push_back( 3465 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I]))); 3466 3467 // FIXME: Do VTable uses and dynamic classes deserialize too much ? 3468 // Can we cut them down before writing them ? 3469 3470 // If there were any dynamic classes declarations, deserialize them 3471 // and add them to Sema's vector of such declarations. 3472 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) 3473 SemaObj->DynamicClasses.push_back( 3474 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I]))); 3475 3476 // Load the offsets of the declarations that Sema references. 3477 // They will be lazily deserialized when needed. 3478 if (!SemaDeclRefs.empty()) { 3479 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!"); 3480 SemaObj->StdNamespace = SemaDeclRefs[0]; 3481 SemaObj->StdBadAlloc = SemaDeclRefs[1]; 3482 } 3483 3484 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) { 3485 3486 // If there are @selector references added them to its pool. This is for 3487 // implementation of -Wselector. 3488 if (!F->ReferencedSelectorsData.empty()) { 3489 unsigned int DataSize = F->ReferencedSelectorsData.size()-1; 3490 unsigned I = 0; 3491 while (I < DataSize) { 3492 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]); 3493 SourceLocation SelLoc = ReadSourceLocation( 3494 *F, F->ReferencedSelectorsData, I); 3495 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc)); 3496 } 3497 } 3498 3499 // If there were any pending implicit instantiations, deserialize them 3500 // and add them to Sema's queue of such instantiations. 3501 assert(F->PendingInstantiations.size() % 2 == 0 && 3502 "Expected pairs of entries"); 3503 for (unsigned Idx = 0, N = F->PendingInstantiations.size(); Idx < N;) { 3504 ValueDecl *D=cast<ValueDecl>(GetDecl(F->PendingInstantiations[Idx++])); 3505 SourceLocation Loc = ReadSourceLocation(*F, F->PendingInstantiations,Idx); 3506 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc)); 3507 } 3508 } 3509 3510 // The two special data sets below always come from the most recent PCH, 3511 // which is at the front of the chain. 3512 PerFileData &F = *Chain.front(); 3513 3514 // If there were any weak undeclared identifiers, deserialize them and add to 3515 // Sema's list of weak undeclared identifiers. 3516 if (!WeakUndeclaredIdentifiers.empty()) { 3517 unsigned Idx = 0; 3518 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) { 3519 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx); 3520 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx); 3521 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx); 3522 bool Used = WeakUndeclaredIdentifiers[Idx++]; 3523 Sema::WeakInfo WI(AliasId, Loc); 3524 WI.setUsed(Used); 3525 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI)); 3526 } 3527 } 3528 3529 // If there were any VTable uses, deserialize the information and add it 3530 // to Sema's vector and map of VTable uses. 3531 if (!VTableUses.empty()) { 3532 unsigned Idx = 0; 3533 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) { 3534 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); 3535 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx); 3536 bool DefinitionRequired = VTableUses[Idx++]; 3537 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc)); 3538 SemaObj->VTablesUsed[Class] = DefinitionRequired; 3539 } 3540 } 3541} 3542 3543IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) { 3544 // Try to find this name within our on-disk hash tables. We start with the 3545 // most recent one, since that one contains the most up-to-date info. 3546 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 3547 ASTIdentifierLookupTable *IdTable 3548 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable; 3549 if (!IdTable) 3550 continue; 3551 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart); 3552 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key); 3553 if (Pos == IdTable->end()) 3554 continue; 3555 3556 // Dereferencing the iterator has the effect of building the 3557 // IdentifierInfo node and populating it with the various 3558 // declarations it needs. 3559 return *Pos; 3560 } 3561 return 0; 3562} 3563 3564std::pair<ObjCMethodList, ObjCMethodList> 3565ASTReader::ReadMethodPool(Selector Sel) { 3566 // Find this selector in a hash table. We want to find the most recent entry. 3567 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 3568 PerFileData &F = *Chain[I]; 3569 if (!F.SelectorLookupTable) 3570 continue; 3571 3572 ASTSelectorLookupTable *PoolTable 3573 = (ASTSelectorLookupTable*)F.SelectorLookupTable; 3574 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel); 3575 if (Pos != PoolTable->end()) { 3576 ++NumSelectorsRead; 3577 // FIXME: Not quite happy with the statistics here. We probably should 3578 // disable this tracking when called via LoadSelector. 3579 // Also, should entries without methods count as misses? 3580 ++NumMethodPoolEntriesRead; 3581 ASTSelectorLookupTrait::data_type Data = *Pos; 3582 if (DeserializationListener) 3583 DeserializationListener->SelectorRead(Data.ID, Sel); 3584 return std::make_pair(Data.Instance, Data.Factory); 3585 } 3586 } 3587 3588 ++NumMethodPoolMisses; 3589 return std::pair<ObjCMethodList, ObjCMethodList>(); 3590} 3591 3592void ASTReader::LoadSelector(Selector Sel) { 3593 // It would be complicated to avoid reading the methods anyway. So don't. 3594 ReadMethodPool(Sel); 3595} 3596 3597void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) { 3598 assert(ID && "Non-zero identifier ID required"); 3599 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); 3600 IdentifiersLoaded[ID - 1] = II; 3601 if (DeserializationListener) 3602 DeserializationListener->IdentifierRead(ID, II); 3603} 3604 3605/// \brief Set the globally-visible declarations associated with the given 3606/// identifier. 3607/// 3608/// If the AST reader is currently in a state where the given declaration IDs 3609/// cannot safely be resolved, they are queued until it is safe to resolve 3610/// them. 3611/// 3612/// \param II an IdentifierInfo that refers to one or more globally-visible 3613/// declarations. 3614/// 3615/// \param DeclIDs the set of declaration IDs with the name @p II that are 3616/// visible at global scope. 3617/// 3618/// \param Nonrecursive should be true to indicate that the caller knows that 3619/// this call is non-recursive, and therefore the globally-visible declarations 3620/// will not be placed onto the pending queue. 3621void 3622ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, 3623 const llvm::SmallVectorImpl<uint32_t> &DeclIDs, 3624 bool Nonrecursive) { 3625 if (NumCurrentElementsDeserializing && !Nonrecursive) { 3626 PendingIdentifierInfos.push_back(PendingIdentifierInfo()); 3627 PendingIdentifierInfo &PII = PendingIdentifierInfos.back(); 3628 PII.II = II; 3629 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end()); 3630 return; 3631 } 3632 3633 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { 3634 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); 3635 if (SemaObj) { 3636 if (SemaObj->TUScope) { 3637 // Introduce this declaration into the translation-unit scope 3638 // and add it to the declaration chain for this identifier, so 3639 // that (unqualified) name lookup will find it. 3640 SemaObj->TUScope->AddDecl(D); 3641 } 3642 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D); 3643 } else { 3644 // Queue this declaration so that it will be added to the 3645 // translation unit scope and identifier's declaration chain 3646 // once a Sema object is known. 3647 PreloadedDecls.push_back(D); 3648 } 3649 } 3650} 3651 3652IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) { 3653 if (ID == 0) 3654 return 0; 3655 3656 if (IdentifiersLoaded.empty()) { 3657 Error("no identifier table in AST file"); 3658 return 0; 3659 } 3660 3661 assert(PP && "Forgot to set Preprocessor ?"); 3662 ID -= 1; 3663 if (!IdentifiersLoaded[ID]) { 3664 unsigned Index = ID; 3665 const char *Str = 0; 3666 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 3667 PerFileData *F = Chain[N - I - 1]; 3668 if (Index < F->LocalNumIdentifiers) { 3669 uint32_t Offset = F->IdentifierOffsets[Index]; 3670 Str = F->IdentifierTableData + Offset; 3671 break; 3672 } 3673 Index -= F->LocalNumIdentifiers; 3674 } 3675 assert(Str && "Broken Chain"); 3676 3677 // All of the strings in the AST file are preceded by a 16-bit length. 3678 // Extract that 16-bit length to avoid having to execute strlen(). 3679 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as 3680 // unsigned integers. This is important to avoid integer overflow when 3681 // we cast them to 'unsigned'. 3682 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2; 3683 unsigned StrLen = (((unsigned) StrLenPtr[0]) 3684 | (((unsigned) StrLenPtr[1]) << 8)) - 1; 3685 IdentifiersLoaded[ID] 3686 = &PP->getIdentifierTable().get(Str, StrLen); 3687 if (DeserializationListener) 3688 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]); 3689 } 3690 3691 return IdentifiersLoaded[ID]; 3692} 3693 3694void ASTReader::ReadSLocEntry(unsigned ID) { 3695 ReadSLocEntryRecord(ID); 3696} 3697 3698Selector ASTReader::DecodeSelector(unsigned ID) { 3699 if (ID == 0) 3700 return Selector(); 3701 3702 if (ID > SelectorsLoaded.size()) { 3703 Error("selector ID out of range in AST file"); 3704 return Selector(); 3705 } 3706 3707 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) { 3708 // Load this selector from the selector table. 3709 unsigned Idx = ID - 1; 3710 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 3711 PerFileData &F = *Chain[N - I - 1]; 3712 if (Idx < F.LocalNumSelectors) { 3713 ASTSelectorLookupTrait Trait(*this); 3714 SelectorsLoaded[ID - 1] = 3715 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0); 3716 if (DeserializationListener) 3717 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); 3718 break; 3719 } 3720 Idx -= F.LocalNumSelectors; 3721 } 3722 } 3723 3724 return SelectorsLoaded[ID - 1]; 3725} 3726 3727Selector ASTReader::GetExternalSelector(uint32_t ID) { 3728 return DecodeSelector(ID); 3729} 3730 3731uint32_t ASTReader::GetNumExternalSelectors() { 3732 // ID 0 (the null selector) is considered an external selector. 3733 return getTotalNumSelectors() + 1; 3734} 3735 3736DeclarationName 3737ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) { 3738 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++]; 3739 switch (Kind) { 3740 case DeclarationName::Identifier: 3741 return DeclarationName(GetIdentifierInfo(Record, Idx)); 3742 3743 case DeclarationName::ObjCZeroArgSelector: 3744 case DeclarationName::ObjCOneArgSelector: 3745 case DeclarationName::ObjCMultiArgSelector: 3746 return DeclarationName(GetSelector(Record, Idx)); 3747 3748 case DeclarationName::CXXConstructorName: 3749 return Context->DeclarationNames.getCXXConstructorName( 3750 Context->getCanonicalType(GetType(Record[Idx++]))); 3751 3752 case DeclarationName::CXXDestructorName: 3753 return Context->DeclarationNames.getCXXDestructorName( 3754 Context->getCanonicalType(GetType(Record[Idx++]))); 3755 3756 case DeclarationName::CXXConversionFunctionName: 3757 return Context->DeclarationNames.getCXXConversionFunctionName( 3758 Context->getCanonicalType(GetType(Record[Idx++]))); 3759 3760 case DeclarationName::CXXOperatorName: 3761 return Context->DeclarationNames.getCXXOperatorName( 3762 (OverloadedOperatorKind)Record[Idx++]); 3763 3764 case DeclarationName::CXXLiteralOperatorName: 3765 return Context->DeclarationNames.getCXXLiteralOperatorName( 3766 GetIdentifierInfo(Record, Idx)); 3767 3768 case DeclarationName::CXXUsingDirective: 3769 return DeclarationName::getUsingDirectiveName(); 3770 } 3771 3772 // Required to silence GCC warning 3773 return DeclarationName(); 3774} 3775 3776TemplateName 3777ASTReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) { 3778 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++]; 3779 switch (Kind) { 3780 case TemplateName::Template: 3781 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++]))); 3782 3783 case TemplateName::OverloadedTemplate: { 3784 unsigned size = Record[Idx++]; 3785 UnresolvedSet<8> Decls; 3786 while (size--) 3787 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++]))); 3788 3789 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end()); 3790 } 3791 3792 case TemplateName::QualifiedTemplate: { 3793 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); 3794 bool hasTemplKeyword = Record[Idx++]; 3795 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++])); 3796 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template); 3797 } 3798 3799 case TemplateName::DependentTemplate: { 3800 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); 3801 if (Record[Idx++]) // isIdentifier 3802 return Context->getDependentTemplateName(NNS, 3803 GetIdentifierInfo(Record, Idx)); 3804 return Context->getDependentTemplateName(NNS, 3805 (OverloadedOperatorKind)Record[Idx++]); 3806 } 3807 } 3808 3809 assert(0 && "Unhandled template name kind!"); 3810 return TemplateName(); 3811} 3812 3813TemplateArgument 3814ASTReader::ReadTemplateArgument(PerFileData &F, 3815 const RecordData &Record, unsigned &Idx) { 3816 switch ((TemplateArgument::ArgKind)Record[Idx++]) { 3817 case TemplateArgument::Null: 3818 return TemplateArgument(); 3819 case TemplateArgument::Type: 3820 return TemplateArgument(GetType(Record[Idx++])); 3821 case TemplateArgument::Declaration: 3822 return TemplateArgument(GetDecl(Record[Idx++])); 3823 case TemplateArgument::Integral: { 3824 llvm::APSInt Value = ReadAPSInt(Record, Idx); 3825 QualType T = GetType(Record[Idx++]); 3826 return TemplateArgument(Value, T); 3827 } 3828 case TemplateArgument::Template: 3829 return TemplateArgument(ReadTemplateName(Record, Idx)); 3830 case TemplateArgument::Expression: 3831 return TemplateArgument(ReadExpr(F)); 3832 case TemplateArgument::Pack: { 3833 unsigned NumArgs = Record[Idx++]; 3834 llvm::SmallVector<TemplateArgument, 8> Args; 3835 Args.reserve(NumArgs); 3836 while (NumArgs--) 3837 Args.push_back(ReadTemplateArgument(F, Record, Idx)); 3838 TemplateArgument TemplArg; 3839 TemplArg.setArgumentPack(Args.data(), Args.size(), /*CopyArgs=*/true); 3840 return TemplArg; 3841 } 3842 } 3843 3844 assert(0 && "Unhandled template argument kind!"); 3845 return TemplateArgument(); 3846} 3847 3848TemplateParameterList * 3849ASTReader::ReadTemplateParameterList(PerFileData &F, 3850 const RecordData &Record, unsigned &Idx) { 3851 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx); 3852 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx); 3853 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx); 3854 3855 unsigned NumParams = Record[Idx++]; 3856 llvm::SmallVector<NamedDecl *, 16> Params; 3857 Params.reserve(NumParams); 3858 while (NumParams--) 3859 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++]))); 3860 3861 TemplateParameterList* TemplateParams = 3862 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc, 3863 Params.data(), Params.size(), RAngleLoc); 3864 return TemplateParams; 3865} 3866 3867void 3868ASTReader:: 3869ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs, 3870 PerFileData &F, const RecordData &Record, 3871 unsigned &Idx) { 3872 unsigned NumTemplateArgs = Record[Idx++]; 3873 TemplArgs.reserve(NumTemplateArgs); 3874 while (NumTemplateArgs--) 3875 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx)); 3876} 3877 3878/// \brief Read a UnresolvedSet structure. 3879void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set, 3880 const RecordData &Record, unsigned &Idx) { 3881 unsigned NumDecls = Record[Idx++]; 3882 while (NumDecls--) { 3883 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++])); 3884 AccessSpecifier AS = (AccessSpecifier)Record[Idx++]; 3885 Set.addDecl(D, AS); 3886 } 3887} 3888 3889CXXBaseSpecifier 3890ASTReader::ReadCXXBaseSpecifier(PerFileData &F, 3891 const RecordData &Record, unsigned &Idx) { 3892 bool isVirtual = static_cast<bool>(Record[Idx++]); 3893 bool isBaseOfClass = static_cast<bool>(Record[Idx++]); 3894 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]); 3895 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx); 3896 SourceRange Range = ReadSourceRange(F, Record, Idx); 3897 return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo); 3898} 3899 3900std::pair<CXXBaseOrMemberInitializer **, unsigned> 3901ASTReader::ReadCXXBaseOrMemberInitializers(PerFileData &F, 3902 const RecordData &Record, 3903 unsigned &Idx) { 3904 CXXBaseOrMemberInitializer **BaseOrMemberInitializers = 0; 3905 unsigned NumInitializers = Record[Idx++]; 3906 if (NumInitializers) { 3907 ASTContext &C = *getContext(); 3908 3909 BaseOrMemberInitializers 3910 = new (C) CXXBaseOrMemberInitializer*[NumInitializers]; 3911 for (unsigned i=0; i != NumInitializers; ++i) { 3912 TypeSourceInfo *BaseClassInfo = 0; 3913 bool IsBaseVirtual = false; 3914 FieldDecl *Member = 0; 3915 3916 bool IsBaseInitializer = Record[Idx++]; 3917 if (IsBaseInitializer) { 3918 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx); 3919 IsBaseVirtual = Record[Idx++]; 3920 } else { 3921 Member = cast<FieldDecl>(GetDecl(Record[Idx++])); 3922 } 3923 SourceLocation MemberLoc = ReadSourceLocation(F, Record, Idx); 3924 Expr *Init = ReadExpr(F); 3925 FieldDecl *AnonUnionMember 3926 = cast_or_null<FieldDecl>(GetDecl(Record[Idx++])); 3927 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx); 3928 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx); 3929 bool IsWritten = Record[Idx++]; 3930 unsigned SourceOrderOrNumArrayIndices; 3931 llvm::SmallVector<VarDecl *, 8> Indices; 3932 if (IsWritten) { 3933 SourceOrderOrNumArrayIndices = Record[Idx++]; 3934 } else { 3935 SourceOrderOrNumArrayIndices = Record[Idx++]; 3936 Indices.reserve(SourceOrderOrNumArrayIndices); 3937 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i) 3938 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++]))); 3939 } 3940 3941 CXXBaseOrMemberInitializer *BOMInit; 3942 if (IsBaseInitializer) { 3943 BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo, 3944 IsBaseVirtual, LParenLoc, 3945 Init, RParenLoc); 3946 } else if (IsWritten) { 3947 BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member, MemberLoc, 3948 LParenLoc, Init, RParenLoc); 3949 } else { 3950 BOMInit = CXXBaseOrMemberInitializer::Create(C, Member, MemberLoc, 3951 LParenLoc, Init, RParenLoc, 3952 Indices.data(), 3953 Indices.size()); 3954 } 3955 3956 if (IsWritten) 3957 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices); 3958 BOMInit->setAnonUnionMember(AnonUnionMember); 3959 BaseOrMemberInitializers[i] = BOMInit; 3960 } 3961 } 3962 3963 return std::make_pair(BaseOrMemberInitializers, NumInitializers); 3964} 3965 3966NestedNameSpecifier * 3967ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) { 3968 unsigned N = Record[Idx++]; 3969 NestedNameSpecifier *NNS = 0, *Prev = 0; 3970 for (unsigned I = 0; I != N; ++I) { 3971 NestedNameSpecifier::SpecifierKind Kind 3972 = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; 3973 switch (Kind) { 3974 case NestedNameSpecifier::Identifier: { 3975 IdentifierInfo *II = GetIdentifierInfo(Record, Idx); 3976 NNS = NestedNameSpecifier::Create(*Context, Prev, II); 3977 break; 3978 } 3979 3980 case NestedNameSpecifier::Namespace: { 3981 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++])); 3982 NNS = NestedNameSpecifier::Create(*Context, Prev, NS); 3983 break; 3984 } 3985 3986 case NestedNameSpecifier::TypeSpec: 3987 case NestedNameSpecifier::TypeSpecWithTemplate: { 3988 Type *T = GetType(Record[Idx++]).getTypePtr(); 3989 bool Template = Record[Idx++]; 3990 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T); 3991 break; 3992 } 3993 3994 case NestedNameSpecifier::Global: { 3995 NNS = NestedNameSpecifier::GlobalSpecifier(*Context); 3996 // No associated value, and there can't be a prefix. 3997 break; 3998 } 3999 } 4000 Prev = NNS; 4001 } 4002 return NNS; 4003} 4004 4005SourceRange 4006ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record, 4007 unsigned &Idx) { 4008 SourceLocation beg = ReadSourceLocation(F, Record, Idx); 4009 SourceLocation end = ReadSourceLocation(F, Record, Idx); 4010 return SourceRange(beg, end); 4011} 4012 4013/// \brief Read an integral value 4014llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) { 4015 unsigned BitWidth = Record[Idx++]; 4016 unsigned NumWords = llvm::APInt::getNumWords(BitWidth); 4017 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]); 4018 Idx += NumWords; 4019 return Result; 4020} 4021 4022/// \brief Read a signed integral value 4023llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) { 4024 bool isUnsigned = Record[Idx++]; 4025 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned); 4026} 4027 4028/// \brief Read a floating-point value 4029llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) { 4030 return llvm::APFloat(ReadAPInt(Record, Idx)); 4031} 4032 4033// \brief Read a string 4034std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) { 4035 unsigned Len = Record[Idx++]; 4036 std::string Result(Record.data() + Idx, Record.data() + Idx + Len); 4037 Idx += Len; 4038 return Result; 4039} 4040 4041CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record, 4042 unsigned &Idx) { 4043 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++])); 4044 return CXXTemporary::Create(*Context, Decl); 4045} 4046 4047DiagnosticBuilder ASTReader::Diag(unsigned DiagID) { 4048 return Diag(SourceLocation(), DiagID); 4049} 4050 4051DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) { 4052 return Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID); 4053} 4054 4055/// \brief Retrieve the identifier table associated with the 4056/// preprocessor. 4057IdentifierTable &ASTReader::getIdentifierTable() { 4058 assert(PP && "Forgot to set Preprocessor ?"); 4059 return PP->getIdentifierTable(); 4060} 4061 4062/// \brief Record that the given ID maps to the given switch-case 4063/// statement. 4064void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { 4065 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID"); 4066 SwitchCaseStmts[ID] = SC; 4067} 4068 4069/// \brief Retrieve the switch-case statement with the given ID. 4070SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) { 4071 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID"); 4072 return SwitchCaseStmts[ID]; 4073} 4074 4075/// \brief Record that the given label statement has been 4076/// deserialized and has the given ID. 4077void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) { 4078 assert(LabelStmts.find(ID) == LabelStmts.end() && 4079 "Deserialized label twice"); 4080 LabelStmts[ID] = S; 4081 4082 // If we've already seen any goto statements that point to this 4083 // label, resolve them now. 4084 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter; 4085 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID); 4086 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto) 4087 Goto->second->setLabel(S); 4088 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second); 4089 4090 // If we've already seen any address-label statements that point to 4091 // this label, resolve them now. 4092 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter; 4093 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels 4094 = UnresolvedAddrLabelExprs.equal_range(ID); 4095 for (AddrLabelIter AddrLabel = AddrLabels.first; 4096 AddrLabel != AddrLabels.second; ++AddrLabel) 4097 AddrLabel->second->setLabel(S); 4098 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second); 4099} 4100 4101/// \brief Set the label of the given statement to the label 4102/// identified by ID. 4103/// 4104/// Depending on the order in which the label and other statements 4105/// referencing that label occur, this operation may complete 4106/// immediately (updating the statement) or it may queue the 4107/// statement to be back-patched later. 4108void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) { 4109 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID); 4110 if (Label != LabelStmts.end()) { 4111 // We've already seen this label, so set the label of the goto and 4112 // we're done. 4113 S->setLabel(Label->second); 4114 } else { 4115 // We haven't seen this label yet, so add this goto to the set of 4116 // unresolved goto statements. 4117 UnresolvedGotoStmts.insert(std::make_pair(ID, S)); 4118 } 4119} 4120 4121/// \brief Set the label of the given expression to the label 4122/// identified by ID. 4123/// 4124/// Depending on the order in which the label and other statements 4125/// referencing that label occur, this operation may complete 4126/// immediately (updating the statement) or it may queue the 4127/// statement to be back-patched later. 4128void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) { 4129 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID); 4130 if (Label != LabelStmts.end()) { 4131 // We've already seen this label, so set the label of the 4132 // label-address expression and we're done. 4133 S->setLabel(Label->second); 4134 } else { 4135 // We haven't seen this label yet, so add this label-address 4136 // expression to the set of unresolved label-address expressions. 4137 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S)); 4138 } 4139} 4140 4141void ASTReader::FinishedDeserializing() { 4142 assert(NumCurrentElementsDeserializing && 4143 "FinishedDeserializing not paired with StartedDeserializing"); 4144 if (NumCurrentElementsDeserializing == 1) { 4145 // If any identifiers with corresponding top-level declarations have 4146 // been loaded, load those declarations now. 4147 while (!PendingIdentifierInfos.empty()) { 4148 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II, 4149 PendingIdentifierInfos.front().DeclIDs, true); 4150 PendingIdentifierInfos.pop_front(); 4151 } 4152 4153 // We are not in recursive loading, so it's safe to pass the "interesting" 4154 // decls to the consumer. 4155 if (Consumer) 4156 PassInterestingDeclsToConsumer(); 4157 } 4158 --NumCurrentElementsDeserializing; 4159} 4160 4161ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context, 4162 const char *isysroot, bool DisableValidation) 4163 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0), 4164 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), 4165 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context), 4166 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation), 4167 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0), 4168 TotalNumSLocEntries(0), NextSLocOffset(0), NumStatementsRead(0), 4169 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0), 4170 NumSelectorsRead(0), NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0), 4171 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0), 4172 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0), 4173 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) { 4174 RelocatablePCH = false; 4175} 4176 4177ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr, 4178 Diagnostic &Diags, const char *isysroot, 4179 bool DisableValidation) 4180 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr), 4181 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0), 4182 isysroot(isysroot), DisableValidation(DisableValidation), NumStatHits(0), 4183 NumStatMisses(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0), 4184 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0), 4185 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0), 4186 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0), 4187 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0), 4188 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0), 4189 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) { 4190 RelocatablePCH = false; 4191} 4192 4193ASTReader::~ASTReader() { 4194 for (unsigned i = 0, e = Chain.size(); i != e; ++i) 4195 delete Chain[e - i - 1]; 4196 // Delete all visible decl lookup tables 4197 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(), 4198 E = DeclContextOffsets.end(); 4199 I != E; ++I) { 4200 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end(); 4201 J != F; ++J) { 4202 if (J->NameLookupTableData) 4203 delete static_cast<ASTDeclContextNameLookupTable*>( 4204 J->NameLookupTableData); 4205 } 4206 } 4207 for (DeclContextVisibleUpdatesPending::iterator 4208 I = PendingVisibleUpdates.begin(), 4209 E = PendingVisibleUpdates.end(); 4210 I != E; ++I) { 4211 for (DeclContextVisibleUpdates::iterator J = I->second.begin(), 4212 F = I->second.end(); 4213 J != F; ++J) 4214 delete static_cast<ASTDeclContextNameLookupTable*>(*J); 4215 } 4216} 4217 4218ASTReader::PerFileData::PerFileData() 4219 : SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0), 4220 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0), 4221 IdentifierLookupTable(0), LocalNumMacroDefinitions(0), 4222 MacroDefinitionOffsets(0), LocalNumSelectors(0), SelectorOffsets(0), 4223 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0), 4224 DeclOffsets(0), LocalNumTypes(0), TypeOffsets(0), StatCache(0), 4225 NumPreallocatedPreprocessingEntities(0), NextInSource(0) 4226{} 4227 4228ASTReader::PerFileData::~PerFileData() { 4229 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable); 4230 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable); 4231} 4232 4233