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