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