ASTReader.cpp revision 4c0c7e86645dfa1719d17d70e009ab49347aba62
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 "clang/Serialization/SerializationDiagnostic.h" 18#include "ASTCommon.h" 19#include "ASTReaderInternals.h" 20#include "clang/Sema/Sema.h" 21#include "clang/Sema/Scope.h" 22#include "clang/AST/ASTConsumer.h" 23#include "clang/AST/ASTContext.h" 24#include "clang/AST/DeclTemplate.h" 25#include "clang/AST/Expr.h" 26#include "clang/AST/ExprCXX.h" 27#include "clang/AST/NestedNameSpecifier.h" 28#include "clang/AST/Type.h" 29#include "clang/AST/TypeLocVisitor.h" 30#include "clang/Lex/MacroInfo.h" 31#include "clang/Lex/PreprocessingRecord.h" 32#include "clang/Lex/Preprocessor.h" 33#include "clang/Lex/PreprocessorOptions.h" 34#include "clang/Lex/HeaderSearch.h" 35#include "clang/Lex/HeaderSearchOptions.h" 36#include "clang/Basic/OnDiskHashTable.h" 37#include "clang/Basic/SourceManager.h" 38#include "clang/Basic/SourceManagerInternals.h" 39#include "clang/Basic/FileManager.h" 40#include "clang/Basic/FileSystemStatCache.h" 41#include "clang/Basic/TargetInfo.h" 42#include "clang/Basic/TargetOptions.h" 43#include "clang/Basic/Version.h" 44#include "clang/Basic/VersionTuple.h" 45#include "llvm/ADT/StringExtras.h" 46#include "llvm/Bitcode/BitstreamReader.h" 47#include "llvm/Support/MemoryBuffer.h" 48#include "llvm/Support/ErrorHandling.h" 49#include "llvm/Support/FileSystem.h" 50#include "llvm/Support/Path.h" 51#include "llvm/Support/SaveAndRestore.h" 52#include "llvm/Support/system_error.h" 53#include <algorithm> 54#include <iterator> 55#include <cstdio> 56#include <sys/stat.h> 57 58using namespace clang; 59using namespace clang::serialization; 60using namespace clang::serialization::reader; 61 62//===----------------------------------------------------------------------===// 63// PCH validator implementation 64//===----------------------------------------------------------------------===// 65 66ASTReaderListener::~ASTReaderListener() {} 67 68/// \brief Compare the given set of language options against an existing set of 69/// language options. 70/// 71/// \param Diags If non-NULL, diagnostics will be emitted via this engine. 72/// 73/// \returns true if the languagae options mis-match, false otherwise. 74static bool checkLanguageOptions(const LangOptions &LangOpts, 75 const LangOptions &ExistingLangOpts, 76 DiagnosticsEngine *Diags) { 77#define LANGOPT(Name, Bits, Default, Description) \ 78 if (ExistingLangOpts.Name != LangOpts.Name) { \ 79 if (Diags) \ 80 Diags->Report(diag::err_pch_langopt_mismatch) \ 81 << Description << LangOpts.Name << ExistingLangOpts.Name; \ 82 return true; \ 83 } 84 85#define VALUE_LANGOPT(Name, Bits, Default, Description) \ 86 if (ExistingLangOpts.Name != LangOpts.Name) { \ 87 if (Diags) \ 88 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 89 << Description; \ 90 return true; \ 91 } 92 93#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 94 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \ 95 if (Diags) \ 96 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 97 << Description; \ 98 return true; \ 99 } 100 101#define BENIGN_LANGOPT(Name, Bits, Default, Description) 102#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) 103#include "clang/Basic/LangOptions.def" 104 105 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) { 106 if (Diags) 107 Diags->Report(diag::err_pch_langopt_value_mismatch) 108 << "target Objective-C runtime"; 109 return true; 110 } 111 112 return false; 113} 114 115/// \brief Compare the given set of target options against an existing set of 116/// target options. 117/// 118/// \param Diags If non-NULL, diagnostics will be emitted via this engine. 119/// 120/// \returns true if the target options mis-match, false otherwise. 121static bool checkTargetOptions(const TargetOptions &TargetOpts, 122 const TargetOptions &ExistingTargetOpts, 123 DiagnosticsEngine *Diags) { 124#define CHECK_TARGET_OPT(Field, Name) \ 125 if (TargetOpts.Field != ExistingTargetOpts.Field) { \ 126 if (Diags) \ 127 Diags->Report(diag::err_pch_targetopt_mismatch) \ 128 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \ 129 return true; \ 130 } 131 132 CHECK_TARGET_OPT(Triple, "target"); 133 CHECK_TARGET_OPT(CPU, "target CPU"); 134 CHECK_TARGET_OPT(ABI, "target ABI"); 135 CHECK_TARGET_OPT(CXXABI, "target C++ ABI"); 136 CHECK_TARGET_OPT(LinkerVersion, "target linker version"); 137#undef CHECK_TARGET_OPT 138 139 // Compare feature sets. 140 SmallVector<StringRef, 4> ExistingFeatures( 141 ExistingTargetOpts.FeaturesAsWritten.begin(), 142 ExistingTargetOpts.FeaturesAsWritten.end()); 143 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(), 144 TargetOpts.FeaturesAsWritten.end()); 145 std::sort(ExistingFeatures.begin(), ExistingFeatures.end()); 146 std::sort(ReadFeatures.begin(), ReadFeatures.end()); 147 148 unsigned ExistingIdx = 0, ExistingN = ExistingFeatures.size(); 149 unsigned ReadIdx = 0, ReadN = ReadFeatures.size(); 150 while (ExistingIdx < ExistingN && ReadIdx < ReadN) { 151 if (ExistingFeatures[ExistingIdx] == ReadFeatures[ReadIdx]) { 152 ++ExistingIdx; 153 ++ReadIdx; 154 continue; 155 } 156 157 if (ReadFeatures[ReadIdx] < ExistingFeatures[ExistingIdx]) { 158 if (Diags) 159 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 160 << false << ReadFeatures[ReadIdx]; 161 return true; 162 } 163 164 if (Diags) 165 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 166 << true << ExistingFeatures[ExistingIdx]; 167 return true; 168 } 169 170 if (ExistingIdx < ExistingN) { 171 if (Diags) 172 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 173 << true << ExistingFeatures[ExistingIdx]; 174 return true; 175 } 176 177 if (ReadIdx < ReadN) { 178 if (Diags) 179 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 180 << false << ReadFeatures[ReadIdx]; 181 return true; 182 } 183 184 return false; 185} 186 187bool 188PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts, 189 bool Complain) { 190 const LangOptions &ExistingLangOpts = PP.getLangOpts(); 191 return checkLanguageOptions(LangOpts, ExistingLangOpts, 192 Complain? &Reader.Diags : 0); 193} 194 195bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts, 196 bool Complain) { 197 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts(); 198 return checkTargetOptions(TargetOpts, ExistingTargetOpts, 199 Complain? &Reader.Diags : 0); 200} 201 202namespace { 203 typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> > 204 MacroDefinitionsMap; 205} 206 207/// \brief Collect the macro definitions provided by the given preprocessor 208/// options. 209static void collectMacroDefinitions(const PreprocessorOptions &PPOpts, 210 MacroDefinitionsMap &Macros, 211 SmallVectorImpl<StringRef> *MacroNames = 0){ 212 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) { 213 StringRef Macro = PPOpts.Macros[I].first; 214 bool IsUndef = PPOpts.Macros[I].second; 215 216 std::pair<StringRef, StringRef> MacroPair = Macro.split('='); 217 StringRef MacroName = MacroPair.first; 218 StringRef MacroBody = MacroPair.second; 219 220 // For an #undef'd macro, we only care about the name. 221 if (IsUndef) { 222 if (MacroNames && !Macros.count(MacroName)) 223 MacroNames->push_back(MacroName); 224 225 Macros[MacroName] = std::make_pair("", true); 226 continue; 227 } 228 229 // For a #define'd macro, figure out the actual definition. 230 if (MacroName.size() == Macro.size()) 231 MacroBody = "1"; 232 else { 233 // Note: GCC drops anything following an end-of-line character. 234 StringRef::size_type End = MacroBody.find_first_of("\n\r"); 235 MacroBody = MacroBody.substr(0, End); 236 } 237 238 if (MacroNames && !Macros.count(MacroName)) 239 MacroNames->push_back(MacroName); 240 Macros[MacroName] = std::make_pair(MacroBody, false); 241 } 242} 243 244/// \brief Check the preprocessor options deserialized from the control block 245/// against the preprocessor options in an existing preprocessor. 246/// 247/// \param Diags If non-null, produce diagnostics for any mismatches incurred. 248static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts, 249 const PreprocessorOptions &ExistingPPOpts, 250 DiagnosticsEngine *Diags) { 251 // Check macro definitions. 252 MacroDefinitionsMap ASTFileMacros; 253 collectMacroDefinitions(PPOpts, ASTFileMacros); 254 MacroDefinitionsMap ExistingMacros; 255 SmallVector<StringRef, 4> ExistingMacroNames; 256 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames); 257 258 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) { 259 // Dig out the macro definition in the existing preprocessor options. 260 StringRef MacroName = ExistingMacroNames[I]; 261 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName]; 262 263 // Check whether we know anything about this macro name or not. 264 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known 265 = ASTFileMacros.find(MacroName); 266 if (Known == ASTFileMacros.end()) { 267 // FIXME: Check whether this identifier was referenced anywhere in the 268 // AST file. If so, we should reject the AST file. Unfortunately, this 269 // information isn't in the control block. What shall we do about it? 270 continue; 271 } 272 273 // If the macro was defined in one but undef'd in the other, we have a 274 // conflict. 275 if (Existing.second != Known->second.second) { 276 if (Diags) { 277 Diags->Report(diag::err_pch_macro_def_undef) 278 << MacroName << Known->second.second; 279 } 280 return true; 281 } 282 283 // If the macro was #undef'd in both, or if the macro bodies are identical, 284 // it's fine. 285 if (Existing.second || Existing.first == Known->second.first) 286 continue; 287 288 // The macro bodies differ; complain. 289 if (Diags) { 290 Diags->Report(diag::err_pch_macro_def_conflict) 291 << MacroName << Known->second.first << Existing.first; 292 } 293 return true; 294 } 295 296 // Check whether we're using predefines. 297 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) { 298 if (Diags) { 299 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines; 300 } 301 return true; 302 } 303 304 return false; 305} 306 307bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 308 bool Complain) { 309 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts(); 310 311 return checkPreprocessorOptions(PPOpts, ExistingPPOpts, 312 Complain? &Reader.Diags : 0); 313} 314 315namespace { 316 struct EmptyStringRef { 317 bool operator ()(StringRef r) const { return r.empty(); } 318 }; 319 struct EmptyBlock { 320 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();} 321 }; 322} 323 324static bool EqualConcatenations(SmallVector<StringRef, 2> L, 325 PCHPredefinesBlocks R) { 326 // First, sum up the lengths. 327 unsigned LL = 0, RL = 0; 328 for (unsigned I = 0, N = L.size(); I != N; ++I) { 329 LL += L[I].size(); 330 } 331 for (unsigned I = 0, N = R.size(); I != N; ++I) { 332 RL += R[I].Data.size(); 333 } 334 if (LL != RL) 335 return false; 336 if (LL == 0 && RL == 0) 337 return true; 338 339 // Kick out empty parts, they confuse the algorithm below. 340 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end()); 341 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end()); 342 343 // Do it the hard way. At this point, both vectors must be non-empty. 344 StringRef LR = L[0], RR = R[0].Data; 345 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size(); 346 (void) RN; 347 for (;;) { 348 // Compare the current pieces. 349 if (LR.size() == RR.size()) { 350 // If they're the same length, it's pretty easy. 351 if (LR != RR) 352 return false; 353 // Both pieces are done, advance. 354 ++LI; 355 ++RI; 356 // If either string is done, they're both done, since they're the same 357 // length. 358 if (LI == LN) { 359 assert(RI == RN && "Strings not the same length after all?"); 360 return true; 361 } 362 LR = L[LI]; 363 RR = R[RI].Data; 364 } else if (LR.size() < RR.size()) { 365 // Right piece is longer. 366 if (!RR.startswith(LR)) 367 return false; 368 ++LI; 369 assert(LI != LN && "Strings not the same length after all?"); 370 RR = RR.substr(LR.size()); 371 LR = L[LI]; 372 } else { 373 // Left piece is longer. 374 if (!LR.startswith(RR)) 375 return false; 376 ++RI; 377 assert(RI != RN && "Strings not the same length after all?"); 378 LR = LR.substr(RR.size()); 379 RR = R[RI].Data; 380 } 381 } 382} 383 384static std::pair<FileID, StringRef::size_type> 385FindMacro(const PCHPredefinesBlocks &Buffers, StringRef MacroDef) { 386 std::pair<FileID, StringRef::size_type> Res; 387 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) { 388 Res.second = Buffers[I].Data.find(MacroDef); 389 if (Res.second != StringRef::npos) { 390 Res.first = Buffers[I].BufferID; 391 break; 392 } 393 } 394 return Res; 395} 396 397bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers, 398 StringRef OriginalFileName, 399 std::string &SuggestedPredefines, 400 FileManager &FileMgr, 401 bool Complain) { 402 // We are in the context of an implicit include, so the predefines buffer will 403 // have a #include entry for the PCH file itself (as normalized by the 404 // preprocessor initialization). Find it and skip over it in the checking 405 // below. 406 SmallString<256> PCHInclude; 407 PCHInclude += "#include \""; 408 PCHInclude += HeaderSearch::NormalizeDashIncludePath(OriginalFileName, 409 FileMgr); 410 PCHInclude += "\"\n"; 411 std::pair<StringRef,StringRef> Split = 412 StringRef(PP.getPredefines()).split(PCHInclude.str()); 413 StringRef Left = Split.first, Right = Split.second; 414 if (Left == PP.getPredefines()) { 415 if (Complain) 416 Error("Missing PCH include entry!"); 417 return true; 418 } 419 420 // If the concatenation of all the PCH buffers is equal to the adjusted 421 // command line, we're done. 422 SmallVector<StringRef, 2> CommandLine; 423 CommandLine.push_back(Left); 424 CommandLine.push_back(Right); 425 if (EqualConcatenations(CommandLine, Buffers)) 426 return false; 427 428 SourceManager &SourceMgr = PP.getSourceManager(); 429 430 // The predefines buffers are different. Determine what the differences are, 431 // and whether they require us to reject the PCH file. 432 SmallVector<StringRef, 8> PCHLines; 433 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) 434 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false); 435 436 SmallVector<StringRef, 8> CmdLineLines; 437 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false); 438 439 // Pick out implicit #includes after the PCH and don't consider them for 440 // validation; we will insert them into SuggestedPredefines so that the 441 // preprocessor includes them. 442 std::string IncludesAfterPCH; 443 SmallVector<StringRef, 8> AfterPCHLines; 444 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false); 445 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) { 446 if (AfterPCHLines[i].startswith("#include ")) { 447 IncludesAfterPCH += AfterPCHLines[i]; 448 IncludesAfterPCH += '\n'; 449 } else { 450 CmdLineLines.push_back(AfterPCHLines[i]); 451 } 452 } 453 454 // Make sure we add the includes last into SuggestedPredefines before we 455 // exit this function. 456 struct AddIncludesRAII { 457 std::string &SuggestedPredefines; 458 std::string &IncludesAfterPCH; 459 460 AddIncludesRAII(std::string &SuggestedPredefines, 461 std::string &IncludesAfterPCH) 462 : SuggestedPredefines(SuggestedPredefines), 463 IncludesAfterPCH(IncludesAfterPCH) { } 464 ~AddIncludesRAII() { 465 SuggestedPredefines += IncludesAfterPCH; 466 } 467 } AddIncludes(SuggestedPredefines, IncludesAfterPCH); 468 469 // Sort both sets of predefined buffer lines, since we allow some extra 470 // definitions and they may appear at any point in the output. 471 std::sort(CmdLineLines.begin(), CmdLineLines.end()); 472 std::sort(PCHLines.begin(), PCHLines.end()); 473 474 // Determine which predefines that were used to build the PCH file are missing 475 // from the command line. 476 std::vector<StringRef> MissingPredefines; 477 std::set_difference(PCHLines.begin(), PCHLines.end(), 478 CmdLineLines.begin(), CmdLineLines.end(), 479 std::back_inserter(MissingPredefines)); 480 481 bool MissingDefines = false; 482 bool ConflictingDefines = false; 483 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) { 484 StringRef Missing = MissingPredefines[I]; 485 if (Missing.startswith("#include ")) { 486 // An -include was specified when generating the PCH; it is included in 487 // the PCH, just ignore it. 488 continue; 489 } 490 if (!Missing.startswith("#define ")) { 491 if (Complain) 492 Reader.Diag(diag::warn_pch_compiler_options_mismatch); 493 return true; 494 } 495 496 // This is a macro definition. Determine the name of the macro we're 497 // defining. 498 std::string::size_type StartOfMacroName = strlen("#define "); 499 std::string::size_type EndOfMacroName 500 = Missing.find_first_of("( \n\r", StartOfMacroName); 501 assert(EndOfMacroName != std::string::npos && 502 "Couldn't find the end of the macro name"); 503 StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName); 504 505 // Determine whether this macro was given a different definition on the 506 // command line. 507 std::string MacroDefStart = "#define " + MacroName.str(); 508 std::string::size_type MacroDefLen = MacroDefStart.size(); 509 SmallVector<StringRef, 8>::iterator ConflictPos 510 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(), 511 MacroDefStart); 512 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) { 513 if (!ConflictPos->startswith(MacroDefStart)) { 514 // Different macro; we're done. 515 ConflictPos = CmdLineLines.end(); 516 break; 517 } 518 519 assert(ConflictPos->size() > MacroDefLen && 520 "Invalid #define in predefines buffer?"); 521 if ((*ConflictPos)[MacroDefLen] != ' ' && 522 (*ConflictPos)[MacroDefLen] != '(') 523 continue; // Longer macro name; keep trying. 524 525 // We found a conflicting macro definition. 526 break; 527 } 528 529 if (ConflictPos != CmdLineLines.end()) { 530 if (!Complain) 531 return true; 532 533 Reader.Diag(diag::warn_cmdline_conflicting_macro_def) 534 << MacroName; 535 536 // Show the definition of this macro within the PCH file. 537 std::pair<FileID, StringRef::size_type> MacroLoc = 538 FindMacro(Buffers, Missing); 539 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!"); 540 SourceLocation PCHMissingLoc = 541 SourceMgr.getLocForStartOfFile(MacroLoc.first) 542 .getLocWithOffset(MacroLoc.second); 543 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName; 544 545 ConflictingDefines = true; 546 continue; 547 } 548 549 // If the macro doesn't conflict, then we'll just pick up the macro 550 // definition from the PCH file. Warn the user that they made a mistake. 551 if (ConflictingDefines) 552 continue; // Don't complain if there are already conflicting defs 553 554 if (!MissingDefines) { 555 if (!Complain) 556 return true; 557 558 Reader.Diag(diag::warn_cmdline_missing_macro_defs); 559 MissingDefines = true; 560 } 561 562 if (!Complain) 563 return true; 564 565 // Show the definition of this macro within the PCH file. 566 std::pair<FileID, StringRef::size_type> MacroLoc = 567 FindMacro(Buffers, Missing); 568 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!"); 569 SourceLocation PCHMissingLoc = 570 SourceMgr.getLocForStartOfFile(MacroLoc.first) 571 .getLocWithOffset(MacroLoc.second); 572 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch); 573 } 574 575 if (ConflictingDefines) 576 return true; 577 578 // Determine what predefines were introduced based on command-line 579 // parameters that were not present when building the PCH 580 // file. Extra #defines are okay, so long as the identifiers being 581 // defined were not used within the precompiled header. 582 std::vector<StringRef> ExtraPredefines; 583 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(), 584 PCHLines.begin(), PCHLines.end(), 585 std::back_inserter(ExtraPredefines)); 586 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) { 587 StringRef &Extra = ExtraPredefines[I]; 588 if (!Extra.startswith("#define ")) { 589 if (Complain) 590 Reader.Diag(diag::warn_pch_compiler_options_mismatch); 591 return true; 592 } 593 594 // This is an extra macro definition. Determine the name of the 595 // macro we're defining. 596 std::string::size_type StartOfMacroName = strlen("#define "); 597 std::string::size_type EndOfMacroName 598 = Extra.find_first_of("( \n\r", StartOfMacroName); 599 assert(EndOfMacroName != std::string::npos && 600 "Couldn't find the end of the macro name"); 601 StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName); 602 603 // Check whether this name was used somewhere in the PCH file. If 604 // so, defining it as a macro could change behavior, so we reject 605 // the PCH file. 606 if (IdentifierInfo *II = Reader.get(MacroName)) { 607 if (Complain) 608 Reader.Diag(diag::warn_macro_name_used_in_pch) << II; 609 return true; 610 } 611 612 // Add this definition to the suggested predefines buffer. 613 SuggestedPredefines += Extra; 614 SuggestedPredefines += '\n'; 615 } 616 617 // If we get here, it's because the predefines buffer had compatible 618 // contents. Accept the PCH file. 619 return false; 620} 621 622void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI, 623 unsigned ID) { 624 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID); 625 ++NumHeaderInfos; 626} 627 628void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) { 629 PP.setCounterValue(Value); 630} 631 632//===----------------------------------------------------------------------===// 633// AST reader implementation 634//===----------------------------------------------------------------------===// 635 636void 637ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) { 638 DeserializationListener = Listener; 639} 640 641 642 643unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) { 644 return serialization::ComputeHash(Sel); 645} 646 647 648std::pair<unsigned, unsigned> 649ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) { 650 using namespace clang::io; 651 unsigned KeyLen = ReadUnalignedLE16(d); 652 unsigned DataLen = ReadUnalignedLE16(d); 653 return std::make_pair(KeyLen, DataLen); 654} 655 656ASTSelectorLookupTrait::internal_key_type 657ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) { 658 using namespace clang::io; 659 SelectorTable &SelTable = Reader.getContext().Selectors; 660 unsigned N = ReadUnalignedLE16(d); 661 IdentifierInfo *FirstII 662 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)); 663 if (N == 0) 664 return SelTable.getNullarySelector(FirstII); 665 else if (N == 1) 666 return SelTable.getUnarySelector(FirstII); 667 668 SmallVector<IdentifierInfo *, 16> Args; 669 Args.push_back(FirstII); 670 for (unsigned I = 1; I != N; ++I) 671 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d))); 672 673 return SelTable.getSelector(N, Args.data()); 674} 675 676ASTSelectorLookupTrait::data_type 677ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d, 678 unsigned DataLen) { 679 using namespace clang::io; 680 681 data_type Result; 682 683 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d)); 684 unsigned NumInstanceMethods = ReadUnalignedLE16(d); 685 unsigned NumFactoryMethods = ReadUnalignedLE16(d); 686 687 // Load instance methods 688 for (unsigned I = 0; I != NumInstanceMethods; ++I) { 689 if (ObjCMethodDecl *Method 690 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d))) 691 Result.Instance.push_back(Method); 692 } 693 694 // Load factory methods 695 for (unsigned I = 0; I != NumFactoryMethods; ++I) { 696 if (ObjCMethodDecl *Method 697 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d))) 698 Result.Factory.push_back(Method); 699 } 700 701 return Result; 702} 703 704unsigned ASTIdentifierLookupTrait::ComputeHash(const internal_key_type& a) { 705 return llvm::HashString(StringRef(a.first, a.second)); 706} 707 708std::pair<unsigned, unsigned> 709ASTIdentifierLookupTrait::ReadKeyDataLength(const unsigned char*& d) { 710 using namespace clang::io; 711 unsigned DataLen = ReadUnalignedLE16(d); 712 unsigned KeyLen = ReadUnalignedLE16(d); 713 return std::make_pair(KeyLen, DataLen); 714} 715 716std::pair<const char*, unsigned> 717ASTIdentifierLookupTrait::ReadKey(const unsigned char* d, unsigned n) { 718 assert(n >= 2 && d[n-1] == '\0'); 719 return std::make_pair((const char*) d, n-1); 720} 721 722IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k, 723 const unsigned char* d, 724 unsigned DataLen) { 725 using namespace clang::io; 726 unsigned RawID = ReadUnalignedLE32(d); 727 bool IsInteresting = RawID & 0x01; 728 729 // Wipe out the "is interesting" bit. 730 RawID = RawID >> 1; 731 732 IdentID ID = Reader.getGlobalIdentifierID(F, RawID); 733 if (!IsInteresting) { 734 // For uninteresting identifiers, just build the IdentifierInfo 735 // and associate it with the persistent ID. 736 IdentifierInfo *II = KnownII; 737 if (!II) { 738 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second)); 739 KnownII = II; 740 } 741 Reader.SetIdentifierInfo(ID, II); 742 II->setIsFromAST(); 743 Reader.markIdentifierUpToDate(II); 744 return II; 745 } 746 747 unsigned ObjCOrBuiltinID = ReadUnalignedLE16(d); 748 unsigned Bits = ReadUnalignedLE16(d); 749 bool CPlusPlusOperatorKeyword = Bits & 0x01; 750 Bits >>= 1; 751 bool HasRevertedTokenIDToIdentifier = Bits & 0x01; 752 Bits >>= 1; 753 bool Poisoned = Bits & 0x01; 754 Bits >>= 1; 755 bool ExtensionToken = Bits & 0x01; 756 Bits >>= 1; 757 bool hadMacroDefinition = Bits & 0x01; 758 Bits >>= 1; 759 760 assert(Bits == 0 && "Extra bits in the identifier?"); 761 DataLen -= 8; 762 763 // Build the IdentifierInfo itself and link the identifier ID with 764 // the new IdentifierInfo. 765 IdentifierInfo *II = KnownII; 766 if (!II) { 767 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second)); 768 KnownII = II; 769 } 770 Reader.markIdentifierUpToDate(II); 771 II->setIsFromAST(); 772 773 // Set or check the various bits in the IdentifierInfo structure. 774 // Token IDs are read-only. 775 if (HasRevertedTokenIDToIdentifier) 776 II->RevertTokenIDToIdentifier(); 777 II->setObjCOrBuiltinID(ObjCOrBuiltinID); 778 assert(II->isExtensionToken() == ExtensionToken && 779 "Incorrect extension token flag"); 780 (void)ExtensionToken; 781 if (Poisoned) 782 II->setIsPoisoned(true); 783 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword && 784 "Incorrect C++ operator keyword flag"); 785 (void)CPlusPlusOperatorKeyword; 786 787 // If this identifier is a macro, deserialize the macro 788 // definition. 789 if (hadMacroDefinition) { 790 SmallVector<MacroID, 4> MacroIDs; 791 while (uint32_t LocalID = ReadUnalignedLE32(d)) { 792 MacroIDs.push_back(Reader.getGlobalMacroID(F, LocalID)); 793 DataLen -= 4; 794 } 795 DataLen -= 4; 796 Reader.setIdentifierIsMacro(II, MacroIDs); 797 } 798 799 Reader.SetIdentifierInfo(ID, II); 800 801 // Read all of the declarations visible at global scope with this 802 // name. 803 if (DataLen > 0) { 804 SmallVector<uint32_t, 4> DeclIDs; 805 for (; DataLen > 0; DataLen -= 4) 806 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d))); 807 Reader.SetGloballyVisibleDecls(II, DeclIDs); 808 } 809 810 return II; 811} 812 813unsigned 814ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const { 815 llvm::FoldingSetNodeID ID; 816 ID.AddInteger(Key.Kind); 817 818 switch (Key.Kind) { 819 case DeclarationName::Identifier: 820 case DeclarationName::CXXLiteralOperatorName: 821 ID.AddString(((IdentifierInfo*)Key.Data)->getName()); 822 break; 823 case DeclarationName::ObjCZeroArgSelector: 824 case DeclarationName::ObjCOneArgSelector: 825 case DeclarationName::ObjCMultiArgSelector: 826 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data))); 827 break; 828 case DeclarationName::CXXOperatorName: 829 ID.AddInteger((OverloadedOperatorKind)Key.Data); 830 break; 831 case DeclarationName::CXXConstructorName: 832 case DeclarationName::CXXDestructorName: 833 case DeclarationName::CXXConversionFunctionName: 834 case DeclarationName::CXXUsingDirective: 835 break; 836 } 837 838 return ID.ComputeHash(); 839} 840 841ASTDeclContextNameLookupTrait::internal_key_type 842ASTDeclContextNameLookupTrait::GetInternalKey( 843 const external_key_type& Name) const { 844 DeclNameKey Key; 845 Key.Kind = Name.getNameKind(); 846 switch (Name.getNameKind()) { 847 case DeclarationName::Identifier: 848 Key.Data = (uint64_t)Name.getAsIdentifierInfo(); 849 break; 850 case DeclarationName::ObjCZeroArgSelector: 851 case DeclarationName::ObjCOneArgSelector: 852 case DeclarationName::ObjCMultiArgSelector: 853 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr(); 854 break; 855 case DeclarationName::CXXOperatorName: 856 Key.Data = Name.getCXXOverloadedOperator(); 857 break; 858 case DeclarationName::CXXLiteralOperatorName: 859 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier(); 860 break; 861 case DeclarationName::CXXConstructorName: 862 case DeclarationName::CXXDestructorName: 863 case DeclarationName::CXXConversionFunctionName: 864 case DeclarationName::CXXUsingDirective: 865 Key.Data = 0; 866 break; 867 } 868 869 return Key; 870} 871 872std::pair<unsigned, unsigned> 873ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) { 874 using namespace clang::io; 875 unsigned KeyLen = ReadUnalignedLE16(d); 876 unsigned DataLen = ReadUnalignedLE16(d); 877 return std::make_pair(KeyLen, DataLen); 878} 879 880ASTDeclContextNameLookupTrait::internal_key_type 881ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) { 882 using namespace clang::io; 883 884 DeclNameKey Key; 885 Key.Kind = (DeclarationName::NameKind)*d++; 886 switch (Key.Kind) { 887 case DeclarationName::Identifier: 888 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)); 889 break; 890 case DeclarationName::ObjCZeroArgSelector: 891 case DeclarationName::ObjCOneArgSelector: 892 case DeclarationName::ObjCMultiArgSelector: 893 Key.Data = 894 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d)) 895 .getAsOpaquePtr(); 896 break; 897 case DeclarationName::CXXOperatorName: 898 Key.Data = *d++; // OverloadedOperatorKind 899 break; 900 case DeclarationName::CXXLiteralOperatorName: 901 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)); 902 break; 903 case DeclarationName::CXXConstructorName: 904 case DeclarationName::CXXDestructorName: 905 case DeclarationName::CXXConversionFunctionName: 906 case DeclarationName::CXXUsingDirective: 907 Key.Data = 0; 908 break; 909 } 910 911 return Key; 912} 913 914ASTDeclContextNameLookupTrait::data_type 915ASTDeclContextNameLookupTrait::ReadData(internal_key_type, 916 const unsigned char* d, 917 unsigned DataLen) { 918 using namespace clang::io; 919 unsigned NumDecls = ReadUnalignedLE16(d); 920 LE32DeclID *Start = (LE32DeclID *)d; 921 return std::make_pair(Start, Start + NumDecls); 922} 923 924bool ASTReader::ReadDeclContextStorage(ModuleFile &M, 925 llvm::BitstreamCursor &Cursor, 926 const std::pair<uint64_t, uint64_t> &Offsets, 927 DeclContextInfo &Info) { 928 SavedStreamPosition SavedPosition(Cursor); 929 // First the lexical decls. 930 if (Offsets.first != 0) { 931 Cursor.JumpToBit(Offsets.first); 932 933 RecordData Record; 934 const char *Blob; 935 unsigned BlobLen; 936 unsigned Code = Cursor.ReadCode(); 937 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen); 938 if (RecCode != DECL_CONTEXT_LEXICAL) { 939 Error("Expected lexical block"); 940 return true; 941 } 942 943 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob); 944 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair); 945 } 946 947 // Now the lookup table. 948 if (Offsets.second != 0) { 949 Cursor.JumpToBit(Offsets.second); 950 951 RecordData Record; 952 const char *Blob; 953 unsigned BlobLen; 954 unsigned Code = Cursor.ReadCode(); 955 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen); 956 if (RecCode != DECL_CONTEXT_VISIBLE) { 957 Error("Expected visible lookup table block"); 958 return true; 959 } 960 Info.NameLookupTableData 961 = ASTDeclContextNameLookupTable::Create( 962 (const unsigned char *)Blob + Record[0], 963 (const unsigned char *)Blob, 964 ASTDeclContextNameLookupTrait(*this, M)); 965 } 966 967 return false; 968} 969 970void ASTReader::Error(StringRef Msg) { 971 Error(diag::err_fe_pch_malformed, Msg); 972} 973 974void ASTReader::Error(unsigned DiagID, 975 StringRef Arg1, StringRef Arg2) { 976 if (Diags.isDiagnosticInFlight()) 977 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2); 978 else 979 Diag(DiagID) << Arg1 << Arg2; 980} 981 982/// \brief Tell the AST listener about the predefines buffers in the chain. 983bool ASTReader::CheckPredefinesBuffers(bool Complain) { 984 if (Listener) { 985 // We only care about the primary module. 986 ModuleFile &M = ModuleMgr.getPrimaryModule(); 987 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers, 988 M.ActualOriginalSourceFileName, 989 SuggestedPredefines, 990 FileMgr, 991 Complain); 992 } 993 return false; 994} 995 996//===----------------------------------------------------------------------===// 997// Source Manager Deserialization 998//===----------------------------------------------------------------------===// 999 1000/// \brief Read the line table in the source manager block. 1001/// \returns true if there was an error. 1002bool ASTReader::ParseLineTable(ModuleFile &F, 1003 SmallVectorImpl<uint64_t> &Record) { 1004 unsigned Idx = 0; 1005 LineTableInfo &LineTable = SourceMgr.getLineTable(); 1006 1007 // Parse the file names 1008 std::map<int, int> FileIDs; 1009 for (int I = 0, N = Record[Idx++]; I != N; ++I) { 1010 // Extract the file name 1011 unsigned FilenameLen = Record[Idx++]; 1012 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen); 1013 Idx += FilenameLen; 1014 MaybeAddSystemRootToFilename(F, Filename); 1015 FileIDs[I] = LineTable.getLineTableFilenameID(Filename); 1016 } 1017 1018 // Parse the line entries 1019 std::vector<LineEntry> Entries; 1020 while (Idx < Record.size()) { 1021 int FID = Record[Idx++]; 1022 assert(FID >= 0 && "Serialized line entries for non-local file."); 1023 // Remap FileID from 1-based old view. 1024 FID += F.SLocEntryBaseID - 1; 1025 1026 // Extract the line entries 1027 unsigned NumEntries = Record[Idx++]; 1028 assert(NumEntries && "Numentries is 00000"); 1029 Entries.clear(); 1030 Entries.reserve(NumEntries); 1031 for (unsigned I = 0; I != NumEntries; ++I) { 1032 unsigned FileOffset = Record[Idx++]; 1033 unsigned LineNo = Record[Idx++]; 1034 int FilenameID = FileIDs[Record[Idx++]]; 1035 SrcMgr::CharacteristicKind FileKind 1036 = (SrcMgr::CharacteristicKind)Record[Idx++]; 1037 unsigned IncludeOffset = Record[Idx++]; 1038 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, 1039 FileKind, IncludeOffset)); 1040 } 1041 LineTable.AddEntry(FileID::get(FID), Entries); 1042 } 1043 1044 return false; 1045} 1046 1047namespace { 1048 1049class ASTStatData { 1050public: 1051 const ino_t ino; 1052 const dev_t dev; 1053 const mode_t mode; 1054 const time_t mtime; 1055 const off_t size; 1056 1057 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s) 1058 : ino(i), dev(d), mode(mo), mtime(m), size(s) {} 1059}; 1060 1061class ASTStatLookupTrait { 1062 public: 1063 typedef const char *external_key_type; 1064 typedef const char *internal_key_type; 1065 1066 typedef ASTStatData data_type; 1067 1068 static unsigned ComputeHash(const char *path) { 1069 return llvm::HashString(path); 1070 } 1071 1072 static internal_key_type GetInternalKey(const char *path) { return path; } 1073 1074 static bool EqualKey(internal_key_type a, internal_key_type b) { 1075 return strcmp(a, b) == 0; 1076 } 1077 1078 static std::pair<unsigned, unsigned> 1079 ReadKeyDataLength(const unsigned char*& d) { 1080 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d); 1081 unsigned DataLen = (unsigned) *d++; 1082 return std::make_pair(KeyLen + 1, DataLen); 1083 } 1084 1085 static internal_key_type ReadKey(const unsigned char *d, unsigned) { 1086 return (const char *)d; 1087 } 1088 1089 static data_type ReadData(const internal_key_type, const unsigned char *d, 1090 unsigned /*DataLen*/) { 1091 using namespace clang::io; 1092 1093 ino_t ino = (ino_t) ReadUnalignedLE32(d); 1094 dev_t dev = (dev_t) ReadUnalignedLE32(d); 1095 mode_t mode = (mode_t) ReadUnalignedLE16(d); 1096 time_t mtime = (time_t) ReadUnalignedLE64(d); 1097 off_t size = (off_t) ReadUnalignedLE64(d); 1098 return data_type(ino, dev, mode, mtime, size); 1099 } 1100}; 1101 1102/// \brief stat() cache for precompiled headers. 1103/// 1104/// This cache is very similar to the stat cache used by pretokenized 1105/// headers. 1106class ASTStatCache : public FileSystemStatCache { 1107 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy; 1108 CacheTy *Cache; 1109 1110 unsigned &NumStatHits, &NumStatMisses; 1111public: 1112 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base, 1113 unsigned &NumStatHits, unsigned &NumStatMisses) 1114 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) { 1115 Cache = CacheTy::Create(Buckets, Base); 1116 } 1117 1118 ~ASTStatCache() { delete Cache; } 1119 1120 LookupResult getStat(const char *Path, struct stat &StatBuf, 1121 int *FileDescriptor) { 1122 // Do the lookup for the file's data in the AST file. 1123 CacheTy::iterator I = Cache->find(Path); 1124 1125 // If we don't get a hit in the AST file just forward to 'stat'. 1126 if (I == Cache->end()) { 1127 ++NumStatMisses; 1128 return statChained(Path, StatBuf, FileDescriptor); 1129 } 1130 1131 ++NumStatHits; 1132 ASTStatData Data = *I; 1133 1134 StatBuf.st_ino = Data.ino; 1135 StatBuf.st_dev = Data.dev; 1136 StatBuf.st_mtime = Data.mtime; 1137 StatBuf.st_mode = Data.mode; 1138 StatBuf.st_size = Data.size; 1139 return CacheExists; 1140 } 1141}; 1142} // end anonymous namespace 1143 1144 1145/// \brief Read a source manager block 1146bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) { 1147 using namespace SrcMgr; 1148 1149 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor; 1150 1151 // Set the source-location entry cursor to the current position in 1152 // the stream. This cursor will be used to read the contents of the 1153 // source manager block initially, and then lazily read 1154 // source-location entries as needed. 1155 SLocEntryCursor = F.Stream; 1156 1157 // The stream itself is going to skip over the source manager block. 1158 if (F.Stream.SkipBlock()) { 1159 Error("malformed block record in AST file"); 1160 return true; 1161 } 1162 1163 // Enter the source manager block. 1164 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) { 1165 Error("malformed source manager block record in AST file"); 1166 return true; 1167 } 1168 1169 RecordData Record; 1170 while (true) { 1171 unsigned Code = SLocEntryCursor.ReadCode(); 1172 if (Code == llvm::bitc::END_BLOCK) { 1173 if (SLocEntryCursor.ReadBlockEnd()) { 1174 Error("error at end of Source Manager block in AST file"); 1175 return true; 1176 } 1177 return false; 1178 } 1179 1180 if (Code == llvm::bitc::ENTER_SUBBLOCK) { 1181 // No known subblocks, always skip them. 1182 SLocEntryCursor.ReadSubBlockID(); 1183 if (SLocEntryCursor.SkipBlock()) { 1184 Error("malformed block record in AST file"); 1185 return true; 1186 } 1187 continue; 1188 } 1189 1190 if (Code == llvm::bitc::DEFINE_ABBREV) { 1191 SLocEntryCursor.ReadAbbrevRecord(); 1192 continue; 1193 } 1194 1195 // Read a record. 1196 const char *BlobStart; 1197 unsigned BlobLen; 1198 Record.clear(); 1199 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) { 1200 default: // Default behavior: ignore. 1201 break; 1202 1203 case SM_SLOC_FILE_ENTRY: 1204 case SM_SLOC_BUFFER_ENTRY: 1205 case SM_SLOC_EXPANSION_ENTRY: 1206 // Once we hit one of the source location entries, we're done. 1207 return false; 1208 } 1209 } 1210} 1211 1212/// \brief If a header file is not found at the path that we expect it to be 1213/// and the PCH file was moved from its original location, try to resolve the 1214/// file by assuming that header+PCH were moved together and the header is in 1215/// the same place relative to the PCH. 1216static std::string 1217resolveFileRelativeToOriginalDir(const std::string &Filename, 1218 const std::string &OriginalDir, 1219 const std::string &CurrDir) { 1220 assert(OriginalDir != CurrDir && 1221 "No point trying to resolve the file if the PCH dir didn't change"); 1222 using namespace llvm::sys; 1223 SmallString<128> filePath(Filename); 1224 fs::make_absolute(filePath); 1225 assert(path::is_absolute(OriginalDir)); 1226 SmallString<128> currPCHPath(CurrDir); 1227 1228 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)), 1229 fileDirE = path::end(path::parent_path(filePath)); 1230 path::const_iterator origDirI = path::begin(OriginalDir), 1231 origDirE = path::end(OriginalDir); 1232 // Skip the common path components from filePath and OriginalDir. 1233 while (fileDirI != fileDirE && origDirI != origDirE && 1234 *fileDirI == *origDirI) { 1235 ++fileDirI; 1236 ++origDirI; 1237 } 1238 for (; origDirI != origDirE; ++origDirI) 1239 path::append(currPCHPath, ".."); 1240 path::append(currPCHPath, fileDirI, fileDirE); 1241 path::append(currPCHPath, path::filename(Filename)); 1242 return currPCHPath.str(); 1243} 1244 1245bool ASTReader::ReadSLocEntry(int ID) { 1246 if (ID == 0) 1247 return false; 1248 1249 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1250 Error("source location entry ID out-of-range for AST file"); 1251 return true; 1252 } 1253 1254 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second; 1255 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]); 1256 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor; 1257 unsigned BaseOffset = F->SLocEntryBaseOffset; 1258 1259 ++NumSLocEntriesRead; 1260 unsigned Code = SLocEntryCursor.ReadCode(); 1261 if (Code == llvm::bitc::END_BLOCK || 1262 Code == llvm::bitc::ENTER_SUBBLOCK || 1263 Code == llvm::bitc::DEFINE_ABBREV) { 1264 Error("incorrectly-formatted source location entry in AST file"); 1265 return true; 1266 } 1267 1268 RecordData Record; 1269 const char *BlobStart; 1270 unsigned BlobLen; 1271 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) { 1272 default: 1273 Error("incorrectly-formatted source location entry in AST file"); 1274 return true; 1275 1276 case SM_SLOC_FILE_ENTRY: { 1277 // We will detect whether a file changed and return 'Failure' for it, but 1278 // we will also try to fail gracefully by setting up the SLocEntry. 1279 unsigned InputID = Record[4]; 1280 InputFile IF = getInputFile(*F, InputID); 1281 const FileEntry *File = IF.getPointer(); 1282 bool OverriddenBuffer = IF.getInt(); 1283 1284 if (!IF.getPointer()) 1285 return true; 1286 1287 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1288 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) { 1289 // This is the module's main file. 1290 IncludeLoc = getImportLocation(F); 1291 } 1292 SrcMgr::CharacteristicKind 1293 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1294 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter, 1295 ID, BaseOffset + Record[0]); 1296 SrcMgr::FileInfo &FileInfo = 1297 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile()); 1298 FileInfo.NumCreatedFIDs = Record[5]; 1299 if (Record[3]) 1300 FileInfo.setHasLineDirectives(); 1301 1302 const DeclID *FirstDecl = F->FileSortedDecls + Record[6]; 1303 unsigned NumFileDecls = Record[7]; 1304 if (NumFileDecls) { 1305 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?"); 1306 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl, 1307 NumFileDecls)); 1308 } 1309 1310 const SrcMgr::ContentCache *ContentCache 1311 = SourceMgr.getOrCreateContentCache(File, 1312 /*isSystemFile=*/FileCharacter != SrcMgr::C_User); 1313 if (OverriddenBuffer && !ContentCache->BufferOverridden && 1314 ContentCache->ContentsEntry == ContentCache->OrigEntry) { 1315 unsigned Code = SLocEntryCursor.ReadCode(); 1316 Record.clear(); 1317 unsigned RecCode 1318 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen); 1319 1320 if (RecCode != SM_SLOC_BUFFER_BLOB) { 1321 Error("AST record has invalid code"); 1322 return true; 1323 } 1324 1325 llvm::MemoryBuffer *Buffer 1326 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1), 1327 File->getName()); 1328 SourceMgr.overrideFileContents(File, Buffer); 1329 } 1330 1331 break; 1332 } 1333 1334 case SM_SLOC_BUFFER_ENTRY: { 1335 const char *Name = BlobStart; 1336 unsigned Offset = Record[0]; 1337 unsigned Code = SLocEntryCursor.ReadCode(); 1338 Record.clear(); 1339 unsigned RecCode 1340 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen); 1341 1342 if (RecCode != SM_SLOC_BUFFER_BLOB) { 1343 Error("AST record has invalid code"); 1344 return true; 1345 } 1346 1347 llvm::MemoryBuffer *Buffer 1348 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1), 1349 Name); 1350 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, 1351 BaseOffset + Offset); 1352 1353 if (strcmp(Name, "<built-in>") == 0 && F->Kind == MK_PCH) { 1354 PCHPredefinesBlock Block = { 1355 BufferID, 1356 StringRef(BlobStart, BlobLen - 1) 1357 }; 1358 PCHPredefinesBuffers.push_back(Block); 1359 } 1360 1361 break; 1362 } 1363 1364 case SM_SLOC_EXPANSION_ENTRY: { 1365 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]); 1366 SourceMgr.createExpansionLoc(SpellingLoc, 1367 ReadSourceLocation(*F, Record[2]), 1368 ReadSourceLocation(*F, Record[3]), 1369 Record[4], 1370 ID, 1371 BaseOffset + Record[0]); 1372 break; 1373 } 1374 } 1375 1376 return false; 1377} 1378 1379/// \brief Find the location where the module F is imported. 1380SourceLocation ASTReader::getImportLocation(ModuleFile *F) { 1381 if (F->ImportLoc.isValid()) 1382 return F->ImportLoc; 1383 1384 // Otherwise we have a PCH. It's considered to be "imported" at the first 1385 // location of its includer. 1386 if (F->ImportedBy.empty() || !F->ImportedBy[0]) { 1387 // Main file is the importer. We assume that it is the first entry in the 1388 // entry table. We can't ask the manager, because at the time of PCH loading 1389 // the main file entry doesn't exist yet. 1390 // The very first entry is the invalid instantiation loc, which takes up 1391 // offsets 0 and 1. 1392 return SourceLocation::getFromRawEncoding(2U); 1393 } 1394 //return F->Loaders[0]->FirstLoc; 1395 return F->ImportedBy[0]->FirstLoc; 1396} 1397 1398/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the 1399/// specified cursor. Read the abbreviations that are at the top of the block 1400/// and then leave the cursor pointing into the block. 1401bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor, 1402 unsigned BlockID) { 1403 if (Cursor.EnterSubBlock(BlockID)) { 1404 Error("malformed block record in AST file"); 1405 return Failure; 1406 } 1407 1408 while (true) { 1409 uint64_t Offset = Cursor.GetCurrentBitNo(); 1410 unsigned Code = Cursor.ReadCode(); 1411 1412 // We expect all abbrevs to be at the start of the block. 1413 if (Code != llvm::bitc::DEFINE_ABBREV) { 1414 Cursor.JumpToBit(Offset); 1415 return false; 1416 } 1417 Cursor.ReadAbbrevRecord(); 1418 } 1419} 1420 1421void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset, 1422 MacroInfo *Hint) { 1423 llvm::BitstreamCursor &Stream = F.MacroCursor; 1424 1425 // Keep track of where we are in the stream, then jump back there 1426 // after reading this macro. 1427 SavedStreamPosition SavedPosition(Stream); 1428 1429 Stream.JumpToBit(Offset); 1430 RecordData Record; 1431 SmallVector<IdentifierInfo*, 16> MacroArgs; 1432 MacroInfo *Macro = 0; 1433 1434 // RAII object to add the loaded macro information once we're done 1435 // adding tokens. 1436 struct AddLoadedMacroInfoRAII { 1437 Preprocessor &PP; 1438 MacroInfo *Hint; 1439 MacroInfo *MI; 1440 IdentifierInfo *II; 1441 1442 AddLoadedMacroInfoRAII(Preprocessor &PP, MacroInfo *Hint) 1443 : PP(PP), Hint(Hint), MI(), II() { } 1444 ~AddLoadedMacroInfoRAII( ) { 1445 if (MI) { 1446 // Finally, install the macro. 1447 PP.addLoadedMacroInfo(II, MI, Hint); 1448 } 1449 } 1450 } AddLoadedMacroInfo(PP, Hint); 1451 1452 while (true) { 1453 unsigned Code = Stream.ReadCode(); 1454 switch (Code) { 1455 case llvm::bitc::END_BLOCK: 1456 return; 1457 1458 case llvm::bitc::ENTER_SUBBLOCK: 1459 // No known subblocks, always skip them. 1460 Stream.ReadSubBlockID(); 1461 if (Stream.SkipBlock()) { 1462 Error("malformed block record in AST file"); 1463 return; 1464 } 1465 continue; 1466 1467 case llvm::bitc::DEFINE_ABBREV: 1468 Stream.ReadAbbrevRecord(); 1469 continue; 1470 default: break; 1471 } 1472 1473 // Read a record. 1474 const char *BlobStart = 0; 1475 unsigned BlobLen = 0; 1476 Record.clear(); 1477 PreprocessorRecordTypes RecType = 1478 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart, 1479 BlobLen); 1480 switch (RecType) { 1481 case PP_MACRO_OBJECT_LIKE: 1482 case PP_MACRO_FUNCTION_LIKE: { 1483 // If we already have a macro, that means that we've hit the end 1484 // of the definition of the macro we were looking for. We're 1485 // done. 1486 if (Macro) 1487 return; 1488 1489 IdentifierInfo *II = getLocalIdentifier(F, Record[0]); 1490 if (II == 0) { 1491 Error("macro must have a name in AST file"); 1492 return; 1493 } 1494 1495 unsigned GlobalID = getGlobalMacroID(F, Record[1]); 1496 1497 // If this macro has already been loaded, don't do so again. 1498 if (MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS]) 1499 return; 1500 1501 SubmoduleID GlobalSubmoduleID = getGlobalSubmoduleID(F, Record[2]); 1502 unsigned NextIndex = 3; 1503 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex); 1504 MacroInfo *MI = PP.AllocateMacroInfo(Loc); 1505 1506 // Record this macro. 1507 MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS] = MI; 1508 1509 SourceLocation UndefLoc = ReadSourceLocation(F, Record, NextIndex); 1510 if (UndefLoc.isValid()) 1511 MI->setUndefLoc(UndefLoc); 1512 1513 MI->setIsUsed(Record[NextIndex++]); 1514 MI->setIsFromAST(); 1515 1516 bool IsPublic = Record[NextIndex++]; 1517 MI->setVisibility(IsPublic, ReadSourceLocation(F, Record, NextIndex)); 1518 1519 if (RecType == PP_MACRO_FUNCTION_LIKE) { 1520 // Decode function-like macro info. 1521 bool isC99VarArgs = Record[NextIndex++]; 1522 bool isGNUVarArgs = Record[NextIndex++]; 1523 MacroArgs.clear(); 1524 unsigned NumArgs = Record[NextIndex++]; 1525 for (unsigned i = 0; i != NumArgs; ++i) 1526 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++])); 1527 1528 // Install function-like macro info. 1529 MI->setIsFunctionLike(); 1530 if (isC99VarArgs) MI->setIsC99Varargs(); 1531 if (isGNUVarArgs) MI->setIsGNUVarargs(); 1532 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(), 1533 PP.getPreprocessorAllocator()); 1534 } 1535 1536 if (DeserializationListener) 1537 DeserializationListener->MacroRead(GlobalID, MI); 1538 1539 // If an update record marked this as undefined, do so now. 1540 // FIXME: Only if the submodule this update came from is visible? 1541 MacroUpdatesMap::iterator Update = MacroUpdates.find(GlobalID); 1542 if (Update != MacroUpdates.end()) { 1543 if (MI->getUndefLoc().isInvalid()) { 1544 for (unsigned I = 0, N = Update->second.size(); I != N; ++I) { 1545 bool Hidden = false; 1546 if (unsigned SubmoduleID = Update->second[I].first) { 1547 if (Module *Owner = getSubmodule(SubmoduleID)) { 1548 if (Owner->NameVisibility == Module::Hidden) { 1549 // Note that this #undef is hidden. 1550 Hidden = true; 1551 1552 // Record this hiding for later. 1553 HiddenNamesMap[Owner].push_back( 1554 HiddenName(II, MI, Update->second[I].second.UndefLoc)); 1555 } 1556 } 1557 } 1558 1559 if (!Hidden) { 1560 MI->setUndefLoc(Update->second[I].second.UndefLoc); 1561 if (PPMutationListener *Listener = PP.getPPMutationListener()) 1562 Listener->UndefinedMacro(MI); 1563 break; 1564 } 1565 } 1566 } 1567 MacroUpdates.erase(Update); 1568 } 1569 1570 // Determine whether this macro definition is visible. 1571 bool Hidden = !MI->isPublic(); 1572 if (!Hidden && GlobalSubmoduleID) { 1573 if (Module *Owner = getSubmodule(GlobalSubmoduleID)) { 1574 if (Owner->NameVisibility == Module::Hidden) { 1575 // The owning module is not visible, and this macro definition 1576 // should not be, either. 1577 Hidden = true; 1578 1579 // Note that this macro definition was hidden because its owning 1580 // module is not yet visible. 1581 HiddenNamesMap[Owner].push_back(HiddenName(II, MI)); 1582 } 1583 } 1584 } 1585 MI->setHidden(Hidden); 1586 1587 // Make sure we install the macro once we're done. 1588 AddLoadedMacroInfo.MI = MI; 1589 AddLoadedMacroInfo.II = II; 1590 1591 // Remember that we saw this macro last so that we add the tokens that 1592 // form its body to it. 1593 Macro = MI; 1594 1595 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() && 1596 Record[NextIndex]) { 1597 // We have a macro definition. Register the association 1598 PreprocessedEntityID 1599 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]); 1600 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 1601 PPRec.RegisterMacroDefinition(Macro, 1602 PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true)); 1603 } 1604 1605 ++NumMacrosRead; 1606 break; 1607 } 1608 1609 case PP_TOKEN: { 1610 // If we see a TOKEN before a PP_MACRO_*, then the file is 1611 // erroneous, just pretend we didn't see this. 1612 if (Macro == 0) break; 1613 1614 Token Tok; 1615 Tok.startToken(); 1616 Tok.setLocation(ReadSourceLocation(F, Record[0])); 1617 Tok.setLength(Record[1]); 1618 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2])) 1619 Tok.setIdentifierInfo(II); 1620 Tok.setKind((tok::TokenKind)Record[3]); 1621 Tok.setFlag((Token::TokenFlags)Record[4]); 1622 Macro->AddTokenToBody(Tok); 1623 break; 1624 } 1625 } 1626 } 1627} 1628 1629PreprocessedEntityID 1630ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const { 1631 ContinuousRangeMap<uint32_t, int, 2>::const_iterator 1632 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS); 1633 assert(I != M.PreprocessedEntityRemap.end() 1634 && "Invalid index into preprocessed entity index remap"); 1635 1636 return LocalID + I->second; 1637} 1638 1639unsigned HeaderFileInfoTrait::ComputeHash(const char *path) { 1640 return llvm::HashString(llvm::sys::path::filename(path)); 1641} 1642 1643HeaderFileInfoTrait::internal_key_type 1644HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; } 1645 1646bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) { 1647 if (strcmp(a, b) == 0) 1648 return true; 1649 1650 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b)) 1651 return false; 1652 1653 // Determine whether the actual files are equivalent. 1654 bool Result = false; 1655 if (llvm::sys::fs::equivalent(a, b, Result)) 1656 return false; 1657 1658 return Result; 1659} 1660 1661std::pair<unsigned, unsigned> 1662HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) { 1663 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d); 1664 unsigned DataLen = (unsigned) *d++; 1665 return std::make_pair(KeyLen + 1, DataLen); 1666} 1667 1668HeaderFileInfoTrait::data_type 1669HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d, 1670 unsigned DataLen) { 1671 const unsigned char *End = d + DataLen; 1672 using namespace clang::io; 1673 HeaderFileInfo HFI; 1674 unsigned Flags = *d++; 1675 HFI.isImport = (Flags >> 5) & 0x01; 1676 HFI.isPragmaOnce = (Flags >> 4) & 0x01; 1677 HFI.DirInfo = (Flags >> 2) & 0x03; 1678 HFI.Resolved = (Flags >> 1) & 0x01; 1679 HFI.IndexHeaderMapHeader = Flags & 0x01; 1680 HFI.NumIncludes = ReadUnalignedLE16(d); 1681 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M, 1682 ReadUnalignedLE32(d)); 1683 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) { 1684 // The framework offset is 1 greater than the actual offset, 1685 // since 0 is used as an indicator for "no framework name". 1686 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1); 1687 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName); 1688 } 1689 1690 assert(End == d && "Wrong data length in HeaderFileInfo deserialization"); 1691 (void)End; 1692 1693 // This HeaderFileInfo was externally loaded. 1694 HFI.External = true; 1695 return HFI; 1696} 1697 1698void ASTReader::setIdentifierIsMacro(IdentifierInfo *II, ArrayRef<MacroID> IDs){ 1699 II->setHadMacroDefinition(true); 1700 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard"); 1701 PendingMacroIDs[II].append(IDs.begin(), IDs.end()); 1702} 1703 1704void ASTReader::ReadDefinedMacros() { 1705 // Note that we are loading defined macros. 1706 Deserializing Macros(this); 1707 1708 for (ModuleReverseIterator I = ModuleMgr.rbegin(), 1709 E = ModuleMgr.rend(); I != E; ++I) { 1710 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor; 1711 1712 // If there was no preprocessor block, skip this file. 1713 if (!MacroCursor.getBitStreamReader()) 1714 continue; 1715 1716 llvm::BitstreamCursor Cursor = MacroCursor; 1717 Cursor.JumpToBit((*I)->MacroStartOffset); 1718 1719 RecordData Record; 1720 while (true) { 1721 unsigned Code = Cursor.ReadCode(); 1722 if (Code == llvm::bitc::END_BLOCK) 1723 break; 1724 1725 if (Code == llvm::bitc::ENTER_SUBBLOCK) { 1726 // No known subblocks, always skip them. 1727 Cursor.ReadSubBlockID(); 1728 if (Cursor.SkipBlock()) { 1729 Error("malformed block record in AST file"); 1730 return; 1731 } 1732 continue; 1733 } 1734 1735 if (Code == llvm::bitc::DEFINE_ABBREV) { 1736 Cursor.ReadAbbrevRecord(); 1737 continue; 1738 } 1739 1740 // Read a record. 1741 const char *BlobStart; 1742 unsigned BlobLen; 1743 Record.clear(); 1744 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) { 1745 default: // Default behavior: ignore. 1746 break; 1747 1748 case PP_MACRO_OBJECT_LIKE: 1749 case PP_MACRO_FUNCTION_LIKE: 1750 getLocalIdentifier(**I, Record[0]); 1751 break; 1752 1753 case PP_TOKEN: 1754 // Ignore tokens. 1755 break; 1756 } 1757 } 1758 } 1759} 1760 1761namespace { 1762 /// \brief Visitor class used to look up identifirs in an AST file. 1763 class IdentifierLookupVisitor { 1764 StringRef Name; 1765 unsigned PriorGeneration; 1766 IdentifierInfo *Found; 1767 public: 1768 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration) 1769 : Name(Name), PriorGeneration(PriorGeneration), Found() { } 1770 1771 static bool visit(ModuleFile &M, void *UserData) { 1772 IdentifierLookupVisitor *This 1773 = static_cast<IdentifierLookupVisitor *>(UserData); 1774 1775 // If we've already searched this module file, skip it now. 1776 if (M.Generation <= This->PriorGeneration) 1777 return true; 1778 1779 ASTIdentifierLookupTable *IdTable 1780 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable; 1781 if (!IdTable) 1782 return false; 1783 1784 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), 1785 M, This->Found); 1786 1787 std::pair<const char*, unsigned> Key(This->Name.begin(), 1788 This->Name.size()); 1789 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Trait); 1790 if (Pos == IdTable->end()) 1791 return false; 1792 1793 // Dereferencing the iterator has the effect of building the 1794 // IdentifierInfo node and populating it with the various 1795 // declarations it needs. 1796 This->Found = *Pos; 1797 return true; 1798 } 1799 1800 // \brief Retrieve the identifier info found within the module 1801 // files. 1802 IdentifierInfo *getIdentifierInfo() const { return Found; } 1803 }; 1804} 1805 1806void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) { 1807 // Note that we are loading an identifier. 1808 Deserializing AnIdentifier(this); 1809 1810 unsigned PriorGeneration = 0; 1811 if (getContext().getLangOpts().Modules) 1812 PriorGeneration = IdentifierGeneration[&II]; 1813 1814 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration); 1815 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor); 1816 markIdentifierUpToDate(&II); 1817} 1818 1819void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) { 1820 if (!II) 1821 return; 1822 1823 II->setOutOfDate(false); 1824 1825 // Update the generation for this identifier. 1826 if (getContext().getLangOpts().Modules) 1827 IdentifierGeneration[II] = CurrentGeneration; 1828} 1829 1830llvm::PointerIntPair<const FileEntry *, 1, bool> 1831ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) { 1832 // If this ID is bogus, just return an empty input file. 1833 if (ID == 0 || ID > F.InputFilesLoaded.size()) 1834 return InputFile(); 1835 1836 // If we've already loaded this input file, return it. 1837 if (F.InputFilesLoaded[ID-1].getPointer()) 1838 return F.InputFilesLoaded[ID-1]; 1839 1840 // Go find this input file. 1841 llvm::BitstreamCursor &Cursor = F.InputFilesCursor; 1842 SavedStreamPosition SavedPosition(Cursor); 1843 Cursor.JumpToBit(F.InputFileOffsets[ID-1]); 1844 1845 unsigned Code = Cursor.ReadCode(); 1846 RecordData Record; 1847 const char *BlobStart = 0; 1848 unsigned BlobLen = 0; 1849 switch ((InputFileRecordTypes)Cursor.ReadRecord(Code, Record, 1850 &BlobStart, &BlobLen)) { 1851 case INPUT_FILE: { 1852 unsigned StoredID = Record[0]; 1853 assert(ID == StoredID && "Bogus stored ID or offset"); 1854 (void)StoredID; 1855 off_t StoredSize = (off_t)Record[1]; 1856 time_t StoredTime = (time_t)Record[2]; 1857 bool Overridden = (bool)Record[3]; 1858 1859 // Get the file entry for this input file. 1860 StringRef OrigFilename(BlobStart, BlobLen); 1861 std::string Filename = OrigFilename; 1862 MaybeAddSystemRootToFilename(F, Filename); 1863 const FileEntry *File 1864 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime) 1865 : FileMgr.getFile(Filename, /*OpenFile=*/false); 1866 1867 // If we didn't find the file, resolve it relative to the 1868 // original directory from which this AST file was created. 1869 if (File == 0 && !F.OriginalDir.empty() && !CurrentDir.empty() && 1870 F.OriginalDir != CurrentDir) { 1871 std::string Resolved = resolveFileRelativeToOriginalDir(Filename, 1872 F.OriginalDir, 1873 CurrentDir); 1874 if (!Resolved.empty()) 1875 File = FileMgr.getFile(Resolved); 1876 } 1877 1878 // For an overridden file, create a virtual file with the stored 1879 // size/timestamp. 1880 if (Overridden && File == 0) { 1881 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime); 1882 } 1883 1884 if (File == 0) { 1885 if (Complain) { 1886 std::string ErrorStr = "could not find file '"; 1887 ErrorStr += Filename; 1888 ErrorStr += "' referenced by AST file"; 1889 Error(ErrorStr.c_str()); 1890 } 1891 return InputFile(); 1892 } 1893 1894 // Note that we've loaded this input file. 1895 F.InputFilesLoaded[ID-1] = InputFile(File, Overridden); 1896 1897 // Check if there was a request to override the contents of the file 1898 // that was part of the precompiled header. Overridding such a file 1899 // can lead to problems when lexing using the source locations from the 1900 // PCH. 1901 SourceManager &SM = getSourceManager(); 1902 if (!Overridden && SM.isFileOverridden(File)) { 1903 Error(diag::err_fe_pch_file_overridden, Filename); 1904 // After emitting the diagnostic, recover by disabling the override so 1905 // that the original file will be used. 1906 SM.disableFileContentsOverride(File); 1907 // The FileEntry is a virtual file entry with the size of the contents 1908 // that would override the original contents. Set it to the original's 1909 // size/time. 1910 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File), 1911 StoredSize, StoredTime); 1912 } 1913 1914 // For an overridden file, there is nothing to validate. 1915 if (Overridden) 1916 return InputFile(File, Overridden); 1917 1918 // The stat info from the FileEntry came from the cached stat 1919 // info of the PCH, so we cannot trust it. 1920 struct stat StatBuf; 1921 if (::stat(File->getName(), &StatBuf) != 0) { 1922 StatBuf.st_size = File->getSize(); 1923 StatBuf.st_mtime = File->getModificationTime(); 1924 } 1925 1926 if ((StoredSize != StatBuf.st_size 1927#if !defined(LLVM_ON_WIN32) 1928 // In our regression testing, the Windows file system seems to 1929 // have inconsistent modification times that sometimes 1930 // erroneously trigger this error-handling path. 1931 || StoredTime != StatBuf.st_mtime 1932#endif 1933 )) { 1934 if (Complain) 1935 Error(diag::err_fe_pch_file_modified, Filename); 1936 1937 return InputFile(); 1938 } 1939 1940 return InputFile(File, Overridden); 1941 } 1942 } 1943 1944 return InputFile(); 1945} 1946 1947const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) { 1948 ModuleFile &M = ModuleMgr.getPrimaryModule(); 1949 std::string Filename = filenameStrRef; 1950 MaybeAddSystemRootToFilename(M, Filename); 1951 const FileEntry *File = FileMgr.getFile(Filename); 1952 if (File == 0 && !M.OriginalDir.empty() && !CurrentDir.empty() && 1953 M.OriginalDir != CurrentDir) { 1954 std::string resolved = resolveFileRelativeToOriginalDir(Filename, 1955 M.OriginalDir, 1956 CurrentDir); 1957 if (!resolved.empty()) 1958 File = FileMgr.getFile(resolved); 1959 } 1960 1961 return File; 1962} 1963 1964/// \brief If we are loading a relocatable PCH file, and the filename is 1965/// not an absolute path, add the system root to the beginning of the file 1966/// name. 1967StringRef ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M, 1968 std::string &Filename) { 1969 // If this is not a relocatable PCH file, there's nothing to do. 1970 if (!M.RelocatablePCH) 1971 return Filename; 1972 1973 if (Filename.empty() || llvm::sys::path::is_absolute(Filename)) 1974 return Filename; 1975 1976 if (isysroot.empty()) { 1977 // If no system root was given, default to '/' 1978 Filename.insert(Filename.begin(), '/'); 1979 return Filename; 1980 } 1981 1982 unsigned Length = isysroot.size(); 1983 if (isysroot[Length - 1] != '/') 1984 Filename.insert(Filename.begin(), '/'); 1985 1986 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end()); 1987 return Filename; 1988} 1989 1990ASTReader::ASTReadResult 1991ASTReader::ReadControlBlock(ModuleFile &F, 1992 llvm::SmallVectorImpl<ModuleFile *> &Loaded, 1993 unsigned ClientLoadCapabilities) { 1994 llvm::BitstreamCursor &Stream = F.Stream; 1995 1996 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) { 1997 Error("malformed block record in AST file"); 1998 return Failure; 1999 } 2000 2001 // Read all of the records and blocks in the control block. 2002 RecordData Record; 2003 while (!Stream.AtEndOfStream()) { 2004 unsigned Code = Stream.ReadCode(); 2005 if (Code == llvm::bitc::END_BLOCK) { 2006 if (Stream.ReadBlockEnd()) { 2007 Error("error at end of control block in AST file"); 2008 return Failure; 2009 } 2010 2011 // Validate all of the input files. 2012 if (!DisableValidation) { 2013 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 2014 for (unsigned I = 0, N = Record[0]; I < N; ++I) 2015 if (!getInputFile(F, I+1, Complain).getPointer()) 2016 return OutOfDate; 2017 } 2018 2019 return Success; 2020 } 2021 2022 if (Code == llvm::bitc::ENTER_SUBBLOCK) { 2023 switch (Stream.ReadSubBlockID()) { 2024 case INPUT_FILES_BLOCK_ID: 2025 F.InputFilesCursor = Stream; 2026 if (Stream.SkipBlock() || // Skip with the main cursor 2027 // Read the abbreviations 2028 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) { 2029 Error("malformed block record in AST file"); 2030 return Failure; 2031 } 2032 continue; 2033 2034 default: 2035 if (!Stream.SkipBlock()) 2036 continue; 2037 break; 2038 } 2039 2040 Error("malformed block record in AST file"); 2041 return Failure; 2042 } 2043 2044 if (Code == llvm::bitc::DEFINE_ABBREV) { 2045 Stream.ReadAbbrevRecord(); 2046 continue; 2047 } 2048 2049 // Read and process a record. 2050 Record.clear(); 2051 const char *BlobStart = 0; 2052 unsigned BlobLen = 0; 2053 switch ((ControlRecordTypes)Stream.ReadRecord(Code, Record, 2054 &BlobStart, &BlobLen)) { 2055 case METADATA: { 2056 if (Record[0] != VERSION_MAJOR && !DisableValidation) { 2057 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2058 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old 2059 : diag::warn_pch_version_too_new); 2060 return VersionMismatch; 2061 } 2062 2063 bool hasErrors = Record[5]; 2064 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) { 2065 Diag(diag::err_pch_with_compiler_errors); 2066 return HadErrors; 2067 } 2068 2069 F.RelocatablePCH = Record[4]; 2070 2071 const std::string &CurBranch = getClangFullRepositoryVersion(); 2072 StringRef ASTBranch(BlobStart, BlobLen); 2073 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) { 2074 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2075 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch; 2076 return VersionMismatch; 2077 } 2078 break; 2079 } 2080 2081 case IMPORTS: { 2082 // Load each of the imported PCH files. 2083 unsigned Idx = 0, N = Record.size(); 2084 while (Idx < N) { 2085 // Read information about the AST file. 2086 ModuleKind ImportedKind = (ModuleKind)Record[Idx++]; 2087 unsigned Length = Record[Idx++]; 2088 SmallString<128> ImportedFile(Record.begin() + Idx, 2089 Record.begin() + Idx + Length); 2090 Idx += Length; 2091 2092 // Load the AST file. 2093 switch(ReadASTCore(ImportedFile, ImportedKind, &F, Loaded, 2094 ClientLoadCapabilities)) { 2095 case Failure: return Failure; 2096 // If we have to ignore the dependency, we'll have to ignore this too. 2097 case OutOfDate: return OutOfDate; 2098 case VersionMismatch: return VersionMismatch; 2099 case ConfigurationMismatch: return ConfigurationMismatch; 2100 case HadErrors: return HadErrors; 2101 case Success: break; 2102 } 2103 } 2104 break; 2105 } 2106 2107 case LANGUAGE_OPTIONS: { 2108 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2109 if (Listener && &F == *ModuleMgr.begin() && 2110 ParseLanguageOptions(Record, Complain, *Listener) && 2111 !DisableValidation) 2112 return ConfigurationMismatch; 2113 break; 2114 } 2115 2116 case TARGET_OPTIONS: { 2117 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0; 2118 if (Listener && &F == *ModuleMgr.begin() && 2119 ParseTargetOptions(Record, Complain, *Listener) && 2120 !DisableValidation) 2121 return ConfigurationMismatch; 2122 break; 2123 } 2124 2125 case DIAGNOSTIC_OPTIONS: { 2126 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0; 2127 if (Listener && &F == *ModuleMgr.begin() && 2128 ParseDiagnosticOptions(Record, Complain, *Listener) && 2129 !DisableValidation) 2130 return ConfigurationMismatch; 2131 break; 2132 } 2133 2134 case FILE_SYSTEM_OPTIONS: { 2135 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0; 2136 if (Listener && &F == *ModuleMgr.begin() && 2137 ParseFileSystemOptions(Record, Complain, *Listener) && 2138 !DisableValidation) 2139 return ConfigurationMismatch; 2140 break; 2141 } 2142 2143 case HEADER_SEARCH_OPTIONS: { 2144 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0; 2145 if (Listener && &F == *ModuleMgr.begin() && 2146 ParseHeaderSearchOptions(Record, Complain, *Listener) && 2147 !DisableValidation) 2148 return ConfigurationMismatch; 2149 break; 2150 } 2151 2152 case PREPROCESSOR_OPTIONS: { 2153 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0; 2154 if (Listener && &F == *ModuleMgr.begin() && 2155 ParsePreprocessorOptions(Record, Complain, *Listener) && 2156 !DisableValidation) 2157 return ConfigurationMismatch; 2158 break; 2159 } 2160 2161 case ORIGINAL_FILE: 2162 F.OriginalSourceFileID = FileID::get(Record[0]); 2163 F.ActualOriginalSourceFileName.assign(BlobStart, BlobLen); 2164 F.OriginalSourceFileName = F.ActualOriginalSourceFileName; 2165 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName); 2166 break; 2167 2168 case ORIGINAL_PCH_DIR: 2169 F.OriginalDir.assign(BlobStart, BlobLen); 2170 break; 2171 2172 case INPUT_FILE_OFFSETS: 2173 F.InputFileOffsets = (const uint32_t *)BlobStart; 2174 F.InputFilesLoaded.resize(Record[0]); 2175 break; 2176 } 2177 } 2178 2179 Error("premature end of bitstream in AST file"); 2180 return Failure; 2181} 2182 2183bool ASTReader::ReadASTBlock(ModuleFile &F) { 2184 llvm::BitstreamCursor &Stream = F.Stream; 2185 2186 if (Stream.EnterSubBlock(AST_BLOCK_ID)) { 2187 Error("malformed block record in AST file"); 2188 return true; 2189 } 2190 2191 // Read all of the records and blocks for the AST file. 2192 RecordData Record; 2193 while (!Stream.AtEndOfStream()) { 2194 unsigned Code = Stream.ReadCode(); 2195 if (Code == llvm::bitc::END_BLOCK) { 2196 if (Stream.ReadBlockEnd()) { 2197 Error("error at end of module block in AST file"); 2198 return true; 2199 } 2200 2201 DeclContext *DC = Context.getTranslationUnitDecl(); 2202 if (!DC->hasExternalVisibleStorage() && DC->hasExternalLexicalStorage()) 2203 DC->setMustBuildLookupTable(); 2204 2205 return false; 2206 } 2207 2208 if (Code == llvm::bitc::ENTER_SUBBLOCK) { 2209 switch (Stream.ReadSubBlockID()) { 2210 case DECLTYPES_BLOCK_ID: 2211 // We lazily load the decls block, but we want to set up the 2212 // DeclsCursor cursor to point into it. Clone our current bitcode 2213 // cursor to it, enter the block and read the abbrevs in that block. 2214 // With the main cursor, we just skip over it. 2215 F.DeclsCursor = Stream; 2216 if (Stream.SkipBlock() || // Skip with the main cursor. 2217 // Read the abbrevs. 2218 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) { 2219 Error("malformed block record in AST file"); 2220 return true; 2221 } 2222 break; 2223 2224 case DECL_UPDATES_BLOCK_ID: 2225 if (Stream.SkipBlock()) { 2226 Error("malformed block record in AST file"); 2227 return true; 2228 } 2229 break; 2230 2231 case PREPROCESSOR_BLOCK_ID: 2232 F.MacroCursor = Stream; 2233 if (!PP.getExternalSource()) 2234 PP.setExternalSource(this); 2235 2236 if (Stream.SkipBlock() || 2237 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) { 2238 Error("malformed block record in AST file"); 2239 return true; 2240 } 2241 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo(); 2242 break; 2243 2244 case PREPROCESSOR_DETAIL_BLOCK_ID: 2245 F.PreprocessorDetailCursor = Stream; 2246 if (Stream.SkipBlock() || 2247 ReadBlockAbbrevs(F.PreprocessorDetailCursor, 2248 PREPROCESSOR_DETAIL_BLOCK_ID)) { 2249 Error("malformed preprocessor detail record in AST file"); 2250 return true; 2251 } 2252 F.PreprocessorDetailStartOffset 2253 = F.PreprocessorDetailCursor.GetCurrentBitNo(); 2254 2255 if (!PP.getPreprocessingRecord()) 2256 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false); 2257 if (!PP.getPreprocessingRecord()->getExternalSource()) 2258 PP.getPreprocessingRecord()->SetExternalSource(*this); 2259 break; 2260 2261 case SOURCE_MANAGER_BLOCK_ID: 2262 if (ReadSourceManagerBlock(F)) 2263 return true; 2264 break; 2265 2266 case SUBMODULE_BLOCK_ID: 2267 if (ReadSubmoduleBlock(F)) 2268 return true; 2269 break; 2270 2271 case COMMENTS_BLOCK_ID: { 2272 llvm::BitstreamCursor C = Stream; 2273 if (Stream.SkipBlock() || 2274 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) { 2275 Error("malformed comments block in AST file"); 2276 return true; 2277 } 2278 CommentsCursors.push_back(std::make_pair(C, &F)); 2279 break; 2280 } 2281 2282 default: 2283 if (!Stream.SkipBlock()) 2284 break; 2285 Error("malformed block record in AST file"); 2286 return true; 2287 } 2288 continue; 2289 } 2290 2291 if (Code == llvm::bitc::DEFINE_ABBREV) { 2292 Stream.ReadAbbrevRecord(); 2293 continue; 2294 } 2295 2296 // Read and process a record. 2297 Record.clear(); 2298 const char *BlobStart = 0; 2299 unsigned BlobLen = 0; 2300 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record, 2301 &BlobStart, &BlobLen)) { 2302 default: // Default behavior: ignore. 2303 break; 2304 2305 case TYPE_OFFSET: { 2306 if (F.LocalNumTypes != 0) { 2307 Error("duplicate TYPE_OFFSET record in AST file"); 2308 return true; 2309 } 2310 F.TypeOffsets = (const uint32_t *)BlobStart; 2311 F.LocalNumTypes = Record[0]; 2312 unsigned LocalBaseTypeIndex = Record[1]; 2313 F.BaseTypeIndex = getTotalNumTypes(); 2314 2315 if (F.LocalNumTypes > 0) { 2316 // Introduce the global -> local mapping for types within this module. 2317 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F)); 2318 2319 // Introduce the local -> global mapping for types within this module. 2320 F.TypeRemap.insertOrReplace( 2321 std::make_pair(LocalBaseTypeIndex, 2322 F.BaseTypeIndex - LocalBaseTypeIndex)); 2323 2324 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes); 2325 } 2326 break; 2327 } 2328 2329 case DECL_OFFSET: { 2330 if (F.LocalNumDecls != 0) { 2331 Error("duplicate DECL_OFFSET record in AST file"); 2332 return true; 2333 } 2334 F.DeclOffsets = (const DeclOffset *)BlobStart; 2335 F.LocalNumDecls = Record[0]; 2336 unsigned LocalBaseDeclID = Record[1]; 2337 F.BaseDeclID = getTotalNumDecls(); 2338 2339 if (F.LocalNumDecls > 0) { 2340 // Introduce the global -> local mapping for declarations within this 2341 // module. 2342 GlobalDeclMap.insert( 2343 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F)); 2344 2345 // Introduce the local -> global mapping for declarations within this 2346 // module. 2347 F.DeclRemap.insertOrReplace( 2348 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID)); 2349 2350 // Introduce the global -> local mapping for declarations within this 2351 // module. 2352 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID; 2353 2354 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls); 2355 } 2356 break; 2357 } 2358 2359 case TU_UPDATE_LEXICAL: { 2360 DeclContext *TU = Context.getTranslationUnitDecl(); 2361 DeclContextInfo &Info = F.DeclContextInfos[TU]; 2362 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart); 2363 Info.NumLexicalDecls 2364 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair)); 2365 TU->setHasExternalLexicalStorage(true); 2366 break; 2367 } 2368 2369 case UPDATE_VISIBLE: { 2370 unsigned Idx = 0; 2371 serialization::DeclID ID = ReadDeclID(F, Record, Idx); 2372 ASTDeclContextNameLookupTable *Table = 2373 ASTDeclContextNameLookupTable::Create( 2374 (const unsigned char *)BlobStart + Record[Idx++], 2375 (const unsigned char *)BlobStart, 2376 ASTDeclContextNameLookupTrait(*this, F)); 2377 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU? 2378 DeclContext *TU = Context.getTranslationUnitDecl(); 2379 F.DeclContextInfos[TU].NameLookupTableData = Table; 2380 TU->setHasExternalVisibleStorage(true); 2381 } else 2382 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F)); 2383 break; 2384 } 2385 2386 case IDENTIFIER_TABLE: 2387 F.IdentifierTableData = BlobStart; 2388 if (Record[0]) { 2389 F.IdentifierLookupTable 2390 = ASTIdentifierLookupTable::Create( 2391 (const unsigned char *)F.IdentifierTableData + Record[0], 2392 (const unsigned char *)F.IdentifierTableData, 2393 ASTIdentifierLookupTrait(*this, F)); 2394 2395 PP.getIdentifierTable().setExternalIdentifierLookup(this); 2396 } 2397 break; 2398 2399 case IDENTIFIER_OFFSET: { 2400 if (F.LocalNumIdentifiers != 0) { 2401 Error("duplicate IDENTIFIER_OFFSET record in AST file"); 2402 return true; 2403 } 2404 F.IdentifierOffsets = (const uint32_t *)BlobStart; 2405 F.LocalNumIdentifiers = Record[0]; 2406 unsigned LocalBaseIdentifierID = Record[1]; 2407 F.BaseIdentifierID = getTotalNumIdentifiers(); 2408 2409 if (F.LocalNumIdentifiers > 0) { 2410 // Introduce the global -> local mapping for identifiers within this 2411 // module. 2412 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1, 2413 &F)); 2414 2415 // Introduce the local -> global mapping for identifiers within this 2416 // module. 2417 F.IdentifierRemap.insertOrReplace( 2418 std::make_pair(LocalBaseIdentifierID, 2419 F.BaseIdentifierID - LocalBaseIdentifierID)); 2420 2421 IdentifiersLoaded.resize(IdentifiersLoaded.size() 2422 + F.LocalNumIdentifiers); 2423 } 2424 break; 2425 } 2426 2427 case EXTERNAL_DEFINITIONS: 2428 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2429 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I])); 2430 break; 2431 2432 case SPECIAL_TYPES: 2433 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2434 SpecialTypes.push_back(getGlobalTypeID(F, Record[I])); 2435 break; 2436 2437 case STATISTICS: 2438 TotalNumStatements += Record[0]; 2439 TotalNumMacros += Record[1]; 2440 TotalLexicalDeclContexts += Record[2]; 2441 TotalVisibleDeclContexts += Record[3]; 2442 break; 2443 2444 case UNUSED_FILESCOPED_DECLS: 2445 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2446 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I])); 2447 break; 2448 2449 case DELEGATING_CTORS: 2450 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2451 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I])); 2452 break; 2453 2454 case WEAK_UNDECLARED_IDENTIFIERS: 2455 if (Record.size() % 4 != 0) { 2456 Error("invalid weak identifiers record"); 2457 return true; 2458 } 2459 2460 // FIXME: Ignore weak undeclared identifiers from non-original PCH 2461 // files. This isn't the way to do it :) 2462 WeakUndeclaredIdentifiers.clear(); 2463 2464 // Translate the weak, undeclared identifiers into global IDs. 2465 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) { 2466 WeakUndeclaredIdentifiers.push_back( 2467 getGlobalIdentifierID(F, Record[I++])); 2468 WeakUndeclaredIdentifiers.push_back( 2469 getGlobalIdentifierID(F, Record[I++])); 2470 WeakUndeclaredIdentifiers.push_back( 2471 ReadSourceLocation(F, Record, I).getRawEncoding()); 2472 WeakUndeclaredIdentifiers.push_back(Record[I++]); 2473 } 2474 break; 2475 2476 case LOCALLY_SCOPED_EXTERNAL_DECLS: 2477 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2478 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I])); 2479 break; 2480 2481 case SELECTOR_OFFSETS: { 2482 F.SelectorOffsets = (const uint32_t *)BlobStart; 2483 F.LocalNumSelectors = Record[0]; 2484 unsigned LocalBaseSelectorID = Record[1]; 2485 F.BaseSelectorID = getTotalNumSelectors(); 2486 2487 if (F.LocalNumSelectors > 0) { 2488 // Introduce the global -> local mapping for selectors within this 2489 // module. 2490 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F)); 2491 2492 // Introduce the local -> global mapping for selectors within this 2493 // module. 2494 F.SelectorRemap.insertOrReplace( 2495 std::make_pair(LocalBaseSelectorID, 2496 F.BaseSelectorID - LocalBaseSelectorID)); 2497 2498 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors); 2499 } 2500 break; 2501 } 2502 2503 case METHOD_POOL: 2504 F.SelectorLookupTableData = (const unsigned char *)BlobStart; 2505 if (Record[0]) 2506 F.SelectorLookupTable 2507 = ASTSelectorLookupTable::Create( 2508 F.SelectorLookupTableData + Record[0], 2509 F.SelectorLookupTableData, 2510 ASTSelectorLookupTrait(*this, F)); 2511 TotalNumMethodPoolEntries += Record[1]; 2512 break; 2513 2514 case REFERENCED_SELECTOR_POOL: 2515 if (!Record.empty()) { 2516 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) { 2517 ReferencedSelectorsData.push_back(getGlobalSelectorID(F, 2518 Record[Idx++])); 2519 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx). 2520 getRawEncoding()); 2521 } 2522 } 2523 break; 2524 2525 case PP_COUNTER_VALUE: 2526 if (!Record.empty() && Listener) 2527 Listener->ReadCounter(F, Record[0]); 2528 break; 2529 2530 case FILE_SORTED_DECLS: 2531 F.FileSortedDecls = (const DeclID *)BlobStart; 2532 F.NumFileSortedDecls = Record[0]; 2533 break; 2534 2535 case SOURCE_LOCATION_OFFSETS: { 2536 F.SLocEntryOffsets = (const uint32_t *)BlobStart; 2537 F.LocalNumSLocEntries = Record[0]; 2538 unsigned SLocSpaceSize = Record[1]; 2539 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) = 2540 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, 2541 SLocSpaceSize); 2542 // Make our entry in the range map. BaseID is negative and growing, so 2543 // we invert it. Because we invert it, though, we need the other end of 2544 // the range. 2545 unsigned RangeStart = 2546 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1; 2547 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F)); 2548 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset); 2549 2550 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing. 2551 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0); 2552 GlobalSLocOffsetMap.insert( 2553 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset 2554 - SLocSpaceSize,&F)); 2555 2556 // Initialize the remapping table. 2557 // Invalid stays invalid. 2558 F.SLocRemap.insert(std::make_pair(0U, 0)); 2559 // This module. Base was 2 when being compiled. 2560 F.SLocRemap.insert(std::make_pair(2U, 2561 static_cast<int>(F.SLocEntryBaseOffset - 2))); 2562 2563 TotalNumSLocEntries += F.LocalNumSLocEntries; 2564 break; 2565 } 2566 2567 case MODULE_OFFSET_MAP: { 2568 // Additional remapping information. 2569 const unsigned char *Data = (const unsigned char*)BlobStart; 2570 const unsigned char *DataEnd = Data + BlobLen; 2571 2572 // Continuous range maps we may be updating in our module. 2573 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap); 2574 ContinuousRangeMap<uint32_t, int, 2>::Builder 2575 IdentifierRemap(F.IdentifierRemap); 2576 ContinuousRangeMap<uint32_t, int, 2>::Builder 2577 MacroRemap(F.MacroRemap); 2578 ContinuousRangeMap<uint32_t, int, 2>::Builder 2579 PreprocessedEntityRemap(F.PreprocessedEntityRemap); 2580 ContinuousRangeMap<uint32_t, int, 2>::Builder 2581 SubmoduleRemap(F.SubmoduleRemap); 2582 ContinuousRangeMap<uint32_t, int, 2>::Builder 2583 SelectorRemap(F.SelectorRemap); 2584 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap); 2585 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap); 2586 2587 while(Data < DataEnd) { 2588 uint16_t Len = io::ReadUnalignedLE16(Data); 2589 StringRef Name = StringRef((const char*)Data, Len); 2590 Data += Len; 2591 ModuleFile *OM = ModuleMgr.lookup(Name); 2592 if (!OM) { 2593 Error("SourceLocation remap refers to unknown module"); 2594 return true; 2595 } 2596 2597 uint32_t SLocOffset = io::ReadUnalignedLE32(Data); 2598 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data); 2599 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data); 2600 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data); 2601 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data); 2602 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data); 2603 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data); 2604 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data); 2605 2606 // Source location offset is mapped to OM->SLocEntryBaseOffset. 2607 SLocRemap.insert(std::make_pair(SLocOffset, 2608 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset))); 2609 IdentifierRemap.insert( 2610 std::make_pair(IdentifierIDOffset, 2611 OM->BaseIdentifierID - IdentifierIDOffset)); 2612 MacroRemap.insert(std::make_pair(MacroIDOffset, 2613 OM->BaseMacroID - MacroIDOffset)); 2614 PreprocessedEntityRemap.insert( 2615 std::make_pair(PreprocessedEntityIDOffset, 2616 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset)); 2617 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset, 2618 OM->BaseSubmoduleID - SubmoduleIDOffset)); 2619 SelectorRemap.insert(std::make_pair(SelectorIDOffset, 2620 OM->BaseSelectorID - SelectorIDOffset)); 2621 DeclRemap.insert(std::make_pair(DeclIDOffset, 2622 OM->BaseDeclID - DeclIDOffset)); 2623 2624 TypeRemap.insert(std::make_pair(TypeIndexOffset, 2625 OM->BaseTypeIndex - TypeIndexOffset)); 2626 2627 // Global -> local mappings. 2628 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset; 2629 } 2630 break; 2631 } 2632 2633 case SOURCE_MANAGER_LINE_TABLE: 2634 if (ParseLineTable(F, Record)) 2635 return true; 2636 break; 2637 2638 case SOURCE_LOCATION_PRELOADS: { 2639 // Need to transform from the local view (1-based IDs) to the global view, 2640 // which is based off F.SLocEntryBaseID. 2641 if (!F.PreloadSLocEntries.empty()) { 2642 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file"); 2643 return true; 2644 } 2645 2646 F.PreloadSLocEntries.swap(Record); 2647 break; 2648 } 2649 2650 case STAT_CACHE: { 2651 if (!DisableStatCache) { 2652 ASTStatCache *MyStatCache = 2653 new ASTStatCache((const unsigned char *)BlobStart + Record[0], 2654 (const unsigned char *)BlobStart, 2655 NumStatHits, NumStatMisses); 2656 FileMgr.addStatCache(MyStatCache); 2657 F.StatCache = MyStatCache; 2658 } 2659 break; 2660 } 2661 2662 case EXT_VECTOR_DECLS: 2663 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2664 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I])); 2665 break; 2666 2667 case VTABLE_USES: 2668 if (Record.size() % 3 != 0) { 2669 Error("Invalid VTABLE_USES record"); 2670 return true; 2671 } 2672 2673 // Later tables overwrite earlier ones. 2674 // FIXME: Modules will have some trouble with this. This is clearly not 2675 // the right way to do this. 2676 VTableUses.clear(); 2677 2678 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) { 2679 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++])); 2680 VTableUses.push_back( 2681 ReadSourceLocation(F, Record, Idx).getRawEncoding()); 2682 VTableUses.push_back(Record[Idx++]); 2683 } 2684 break; 2685 2686 case DYNAMIC_CLASSES: 2687 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2688 DynamicClasses.push_back(getGlobalDeclID(F, Record[I])); 2689 break; 2690 2691 case PENDING_IMPLICIT_INSTANTIATIONS: 2692 if (PendingInstantiations.size() % 2 != 0) { 2693 Error("Invalid existing PendingInstantiations"); 2694 return true; 2695 } 2696 2697 if (Record.size() % 2 != 0) { 2698 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block"); 2699 return true; 2700 } 2701 2702 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 2703 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++])); 2704 PendingInstantiations.push_back( 2705 ReadSourceLocation(F, Record, I).getRawEncoding()); 2706 } 2707 break; 2708 2709 case SEMA_DECL_REFS: 2710 // Later tables overwrite earlier ones. 2711 // FIXME: Modules will have some trouble with this. 2712 SemaDeclRefs.clear(); 2713 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2714 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 2715 break; 2716 2717 case PPD_ENTITIES_OFFSETS: { 2718 F.PreprocessedEntityOffsets = (const PPEntityOffset *)BlobStart; 2719 assert(BlobLen % sizeof(PPEntityOffset) == 0); 2720 F.NumPreprocessedEntities = BlobLen / sizeof(PPEntityOffset); 2721 2722 unsigned LocalBasePreprocessedEntityID = Record[0]; 2723 2724 unsigned StartingID; 2725 if (!PP.getPreprocessingRecord()) 2726 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false); 2727 if (!PP.getPreprocessingRecord()->getExternalSource()) 2728 PP.getPreprocessingRecord()->SetExternalSource(*this); 2729 StartingID 2730 = PP.getPreprocessingRecord() 2731 ->allocateLoadedEntities(F.NumPreprocessedEntities); 2732 F.BasePreprocessedEntityID = StartingID; 2733 2734 if (F.NumPreprocessedEntities > 0) { 2735 // Introduce the global -> local mapping for preprocessed entities in 2736 // this module. 2737 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F)); 2738 2739 // Introduce the local -> global mapping for preprocessed entities in 2740 // this module. 2741 F.PreprocessedEntityRemap.insertOrReplace( 2742 std::make_pair(LocalBasePreprocessedEntityID, 2743 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID)); 2744 } 2745 2746 break; 2747 } 2748 2749 case DECL_UPDATE_OFFSETS: { 2750 if (Record.size() % 2 != 0) { 2751 Error("invalid DECL_UPDATE_OFFSETS block in AST file"); 2752 return true; 2753 } 2754 for (unsigned I = 0, N = Record.size(); I != N; I += 2) 2755 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])] 2756 .push_back(std::make_pair(&F, Record[I+1])); 2757 break; 2758 } 2759 2760 case DECL_REPLACEMENTS: { 2761 if (Record.size() % 3 != 0) { 2762 Error("invalid DECL_REPLACEMENTS block in AST file"); 2763 return true; 2764 } 2765 for (unsigned I = 0, N = Record.size(); I != N; I += 3) 2766 ReplacedDecls[getGlobalDeclID(F, Record[I])] 2767 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]); 2768 break; 2769 } 2770 2771 case OBJC_CATEGORIES_MAP: { 2772 if (F.LocalNumObjCCategoriesInMap != 0) { 2773 Error("duplicate OBJC_CATEGORIES_MAP record in AST file"); 2774 return true; 2775 } 2776 2777 F.LocalNumObjCCategoriesInMap = Record[0]; 2778 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)BlobStart; 2779 break; 2780 } 2781 2782 case OBJC_CATEGORIES: 2783 F.ObjCCategories.swap(Record); 2784 break; 2785 2786 case CXX_BASE_SPECIFIER_OFFSETS: { 2787 if (F.LocalNumCXXBaseSpecifiers != 0) { 2788 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file"); 2789 return true; 2790 } 2791 2792 F.LocalNumCXXBaseSpecifiers = Record[0]; 2793 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart; 2794 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers; 2795 break; 2796 } 2797 2798 case DIAG_PRAGMA_MAPPINGS: 2799 if (Record.size() % 2 != 0) { 2800 Error("invalid DIAG_USER_MAPPINGS block in AST file"); 2801 return true; 2802 } 2803 2804 if (F.PragmaDiagMappings.empty()) 2805 F.PragmaDiagMappings.swap(Record); 2806 else 2807 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(), 2808 Record.begin(), Record.end()); 2809 break; 2810 2811 case CUDA_SPECIAL_DECL_REFS: 2812 // Later tables overwrite earlier ones. 2813 // FIXME: Modules will have trouble with this. 2814 CUDASpecialDeclRefs.clear(); 2815 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2816 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 2817 break; 2818 2819 case HEADER_SEARCH_TABLE: { 2820 F.HeaderFileInfoTableData = BlobStart; 2821 F.LocalNumHeaderFileInfos = Record[1]; 2822 F.HeaderFileFrameworkStrings = BlobStart + Record[2]; 2823 if (Record[0]) { 2824 F.HeaderFileInfoTable 2825 = HeaderFileInfoLookupTable::Create( 2826 (const unsigned char *)F.HeaderFileInfoTableData + Record[0], 2827 (const unsigned char *)F.HeaderFileInfoTableData, 2828 HeaderFileInfoTrait(*this, F, 2829 &PP.getHeaderSearchInfo(), 2830 BlobStart + Record[2])); 2831 2832 PP.getHeaderSearchInfo().SetExternalSource(this); 2833 if (!PP.getHeaderSearchInfo().getExternalLookup()) 2834 PP.getHeaderSearchInfo().SetExternalLookup(this); 2835 } 2836 break; 2837 } 2838 2839 case FP_PRAGMA_OPTIONS: 2840 // Later tables overwrite earlier ones. 2841 FPPragmaOptions.swap(Record); 2842 break; 2843 2844 case OPENCL_EXTENSIONS: 2845 // Later tables overwrite earlier ones. 2846 OpenCLExtensions.swap(Record); 2847 break; 2848 2849 case TENTATIVE_DEFINITIONS: 2850 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2851 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I])); 2852 break; 2853 2854 case KNOWN_NAMESPACES: 2855 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2856 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I])); 2857 break; 2858 2859 case IMPORTED_MODULES: { 2860 if (F.Kind != MK_Module) { 2861 // If we aren't loading a module (which has its own exports), make 2862 // all of the imported modules visible. 2863 // FIXME: Deal with macros-only imports. 2864 for (unsigned I = 0, N = Record.size(); I != N; ++I) { 2865 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I])) 2866 ImportedModules.push_back(GlobalID); 2867 } 2868 } 2869 break; 2870 } 2871 2872 case LOCAL_REDECLARATIONS: { 2873 F.RedeclarationChains.swap(Record); 2874 break; 2875 } 2876 2877 case LOCAL_REDECLARATIONS_MAP: { 2878 if (F.LocalNumRedeclarationsInMap != 0) { 2879 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file"); 2880 return true; 2881 } 2882 2883 F.LocalNumRedeclarationsInMap = Record[0]; 2884 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)BlobStart; 2885 break; 2886 } 2887 2888 case MERGED_DECLARATIONS: { 2889 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) { 2890 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]); 2891 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID]; 2892 for (unsigned N = Record[Idx++]; N > 0; --N) 2893 Decls.push_back(getGlobalDeclID(F, Record[Idx++])); 2894 } 2895 break; 2896 } 2897 2898 case MACRO_OFFSET: { 2899 if (F.LocalNumMacros != 0) { 2900 Error("duplicate MACRO_OFFSET record in AST file"); 2901 return true; 2902 } 2903 F.MacroOffsets = (const uint32_t *)BlobStart; 2904 F.LocalNumMacros = Record[0]; 2905 unsigned LocalBaseMacroID = Record[1]; 2906 F.BaseMacroID = getTotalNumMacros(); 2907 2908 if (F.LocalNumMacros > 0) { 2909 // Introduce the global -> local mapping for macros within this module. 2910 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F)); 2911 2912 // Introduce the local -> global mapping for macros within this module. 2913 F.MacroRemap.insertOrReplace( 2914 std::make_pair(LocalBaseMacroID, 2915 F.BaseMacroID - LocalBaseMacroID)); 2916 2917 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros); 2918 } 2919 break; 2920 } 2921 2922 case MACRO_UPDATES: { 2923 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 2924 MacroID ID = getGlobalMacroID(F, Record[I++]); 2925 if (I == N) 2926 break; 2927 2928 SourceLocation UndefLoc = ReadSourceLocation(F, Record, I); 2929 SubmoduleID SubmoduleID = getGlobalSubmoduleID(F, Record[I++]);; 2930 MacroUpdate Update; 2931 Update.UndefLoc = UndefLoc; 2932 MacroUpdates[ID].push_back(std::make_pair(SubmoduleID, Update)); 2933 } 2934 break; 2935 } 2936 } 2937 } 2938 Error("premature end of bitstream in AST file"); 2939 return true; 2940} 2941 2942void ASTReader::makeNamesVisible(const HiddenNames &Names) { 2943 for (unsigned I = 0, N = Names.size(); I != N; ++I) { 2944 switch (Names[I].getKind()) { 2945 case HiddenName::Declaration: 2946 Names[I].getDecl()->Hidden = false; 2947 break; 2948 2949 case HiddenName::MacroVisibility: { 2950 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro(); 2951 Macro.second->setHidden(!Macro.second->isPublic()); 2952 if (Macro.second->isDefined()) { 2953 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second); 2954 } 2955 break; 2956 } 2957 2958 case HiddenName::MacroUndef: { 2959 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro(); 2960 if (Macro.second->isDefined()) { 2961 Macro.second->setUndefLoc(Names[I].getMacroUndefLoc()); 2962 if (PPMutationListener *Listener = PP.getPPMutationListener()) 2963 Listener->UndefinedMacro(Macro.second); 2964 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second); 2965 } 2966 break; 2967 } 2968 } 2969 } 2970} 2971 2972void ASTReader::makeModuleVisible(Module *Mod, 2973 Module::NameVisibilityKind NameVisibility) { 2974 llvm::SmallPtrSet<Module *, 4> Visited; 2975 llvm::SmallVector<Module *, 4> Stack; 2976 Stack.push_back(Mod); 2977 while (!Stack.empty()) { 2978 Mod = Stack.back(); 2979 Stack.pop_back(); 2980 2981 if (NameVisibility <= Mod->NameVisibility) { 2982 // This module already has this level of visibility (or greater), so 2983 // there is nothing more to do. 2984 continue; 2985 } 2986 2987 if (!Mod->isAvailable()) { 2988 // Modules that aren't available cannot be made visible. 2989 continue; 2990 } 2991 2992 // Update the module's name visibility. 2993 Mod->NameVisibility = NameVisibility; 2994 2995 // If we've already deserialized any names from this module, 2996 // mark them as visible. 2997 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod); 2998 if (Hidden != HiddenNamesMap.end()) { 2999 makeNamesVisible(Hidden->second); 3000 HiddenNamesMap.erase(Hidden); 3001 } 3002 3003 // Push any non-explicit submodules onto the stack to be marked as 3004 // visible. 3005 for (Module::submodule_iterator Sub = Mod->submodule_begin(), 3006 SubEnd = Mod->submodule_end(); 3007 Sub != SubEnd; ++Sub) { 3008 if (!(*Sub)->IsExplicit && Visited.insert(*Sub)) 3009 Stack.push_back(*Sub); 3010 } 3011 3012 // Push any exported modules onto the stack to be marked as visible. 3013 bool AnyWildcard = false; 3014 bool UnrestrictedWildcard = false; 3015 llvm::SmallVector<Module *, 4> WildcardRestrictions; 3016 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) { 3017 Module *Exported = Mod->Exports[I].getPointer(); 3018 if (!Mod->Exports[I].getInt()) { 3019 // Export a named module directly; no wildcards involved. 3020 if (Visited.insert(Exported)) 3021 Stack.push_back(Exported); 3022 3023 continue; 3024 } 3025 3026 // Wildcard export: export all of the imported modules that match 3027 // the given pattern. 3028 AnyWildcard = true; 3029 if (UnrestrictedWildcard) 3030 continue; 3031 3032 if (Module *Restriction = Mod->Exports[I].getPointer()) 3033 WildcardRestrictions.push_back(Restriction); 3034 else { 3035 WildcardRestrictions.clear(); 3036 UnrestrictedWildcard = true; 3037 } 3038 } 3039 3040 // If there were any wildcards, push any imported modules that were 3041 // re-exported by the wildcard restriction. 3042 if (!AnyWildcard) 3043 continue; 3044 3045 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) { 3046 Module *Imported = Mod->Imports[I]; 3047 if (!Visited.insert(Imported)) 3048 continue; 3049 3050 bool Acceptable = UnrestrictedWildcard; 3051 if (!Acceptable) { 3052 // Check whether this module meets one of the restrictions. 3053 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) { 3054 Module *Restriction = WildcardRestrictions[R]; 3055 if (Imported == Restriction || Imported->isSubModuleOf(Restriction)) { 3056 Acceptable = true; 3057 break; 3058 } 3059 } 3060 } 3061 3062 if (!Acceptable) 3063 continue; 3064 3065 Stack.push_back(Imported); 3066 } 3067 } 3068} 3069 3070ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName, 3071 ModuleKind Type, 3072 unsigned ClientLoadCapabilities) { 3073 // Bump the generation number. 3074 unsigned PreviousGeneration = CurrentGeneration++; 3075 3076 // Load the core of the AST files. 3077 llvm::SmallVector<ModuleFile *, 4> Loaded; 3078 switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0, Loaded, 3079 ClientLoadCapabilities)) { 3080 case Failure: return Failure; 3081 case OutOfDate: return OutOfDate; 3082 case VersionMismatch: return VersionMismatch; 3083 case ConfigurationMismatch: return ConfigurationMismatch; 3084 case HadErrors: return HadErrors; 3085 case Success: break; 3086 } 3087 3088 // Here comes stuff that we only do once the entire chain is loaded. 3089 3090 // Load the AST blocks of all of the modules that we loaded. 3091 for (llvm::SmallVectorImpl<ModuleFile *>::iterator M = Loaded.begin(), 3092 MEnd = Loaded.end(); 3093 M != MEnd; ++M) { 3094 ModuleFile &F = **M; 3095 3096 // Read the AST block. 3097 if (ReadASTBlock(F)) 3098 return Failure; 3099 3100 // Once read, set the ModuleFile bit base offset and update the size in 3101 // bits of all files we've seen. 3102 F.GlobalBitOffset = TotalModulesSizeInBits; 3103 TotalModulesSizeInBits += F.SizeInBits; 3104 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F)); 3105 3106 // Preload SLocEntries. 3107 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) { 3108 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID; 3109 // Load it through the SourceManager and don't call ReadSLocEntry() 3110 // directly because the entry may have already been loaded in which case 3111 // calling ReadSLocEntry() directly would trigger an assertion in 3112 // SourceManager. 3113 SourceMgr.getLoadedSLocEntryByID(Index); 3114 } 3115 } 3116 3117 // Check the predefines buffers. 3118 bool ConfigComplain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0; 3119 if (!DisableValidation && Type == MK_PCH && 3120 // FIXME: CheckPredefinesBuffers also sets the SuggestedPredefines; 3121 // if DisableValidation is true, defines that were set on command-line 3122 // but not in the PCH file will not be added to SuggestedPredefines. 3123 CheckPredefinesBuffers(ConfigComplain)) 3124 return ConfigurationMismatch; 3125 3126 // Mark all of the identifiers in the identifier table as being out of date, 3127 // so that various accessors know to check the loaded modules when the 3128 // identifier is used. 3129 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(), 3130 IdEnd = PP.getIdentifierTable().end(); 3131 Id != IdEnd; ++Id) 3132 Id->second->setOutOfDate(true); 3133 3134 // Resolve any unresolved module exports. 3135 for (unsigned I = 0, N = UnresolvedModuleImportExports.size(); I != N; ++I) { 3136 UnresolvedModuleImportExport &Unresolved = UnresolvedModuleImportExports[I]; 3137 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID); 3138 Module *ResolvedMod = getSubmodule(GlobalID); 3139 3140 if (Unresolved.IsImport) { 3141 if (ResolvedMod) 3142 Unresolved.Mod->Imports.push_back(ResolvedMod); 3143 continue; 3144 } 3145 3146 if (ResolvedMod || Unresolved.IsWildcard) 3147 Unresolved.Mod->Exports.push_back( 3148 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard)); 3149 } 3150 UnresolvedModuleImportExports.clear(); 3151 3152 InitializeContext(); 3153 3154 if (DeserializationListener) 3155 DeserializationListener->ReaderInitialized(this); 3156 3157 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule(); 3158 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) { 3159 PrimaryModule.OriginalSourceFileID 3160 = FileID::get(PrimaryModule.SLocEntryBaseID 3161 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1); 3162 3163 // If this AST file is a precompiled preamble, then set the 3164 // preamble file ID of the source manager to the file source file 3165 // from which the preamble was built. 3166 if (Type == MK_Preamble) { 3167 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID); 3168 } else if (Type == MK_MainFile) { 3169 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID); 3170 } 3171 } 3172 3173 // For any Objective-C class definitions we have already loaded, make sure 3174 // that we load any additional categories. 3175 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) { 3176 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(), 3177 ObjCClassesLoaded[I], 3178 PreviousGeneration); 3179 } 3180 3181 return Success; 3182} 3183 3184ASTReader::ASTReadResult 3185ASTReader::ReadASTCore(StringRef FileName, 3186 ModuleKind Type, 3187 ModuleFile *ImportedBy, 3188 llvm::SmallVectorImpl<ModuleFile *> &Loaded, 3189 unsigned ClientLoadCapabilities) { 3190 ModuleFile *M; 3191 bool NewModule; 3192 std::string ErrorStr; 3193 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy, 3194 CurrentGeneration, ErrorStr); 3195 3196 if (!M) { 3197 // We couldn't load the module. 3198 std::string Msg = "Unable to load module \"" + FileName.str() + "\": " 3199 + ErrorStr; 3200 Error(Msg); 3201 return Failure; 3202 } 3203 3204 if (!NewModule) { 3205 // We've already loaded this module. 3206 return Success; 3207 } 3208 3209 // FIXME: This seems rather a hack. Should CurrentDir be part of the 3210 // module? 3211 if (FileName != "-") { 3212 CurrentDir = llvm::sys::path::parent_path(FileName); 3213 if (CurrentDir.empty()) CurrentDir = "."; 3214 } 3215 3216 ModuleFile &F = *M; 3217 llvm::BitstreamCursor &Stream = F.Stream; 3218 Stream.init(F.StreamFile); 3219 F.SizeInBits = F.Buffer->getBufferSize() * 8; 3220 3221 // Sniff for the signature. 3222 if (Stream.Read(8) != 'C' || 3223 Stream.Read(8) != 'P' || 3224 Stream.Read(8) != 'C' || 3225 Stream.Read(8) != 'H') { 3226 Diag(diag::err_not_a_pch_file) << FileName; 3227 return Failure; 3228 } 3229 3230 while (!Stream.AtEndOfStream()) { 3231 unsigned Code = Stream.ReadCode(); 3232 3233 if (Code != llvm::bitc::ENTER_SUBBLOCK) { 3234 Error("invalid record at top-level of AST file"); 3235 return Failure; 3236 } 3237 3238 unsigned BlockID = Stream.ReadSubBlockID(); 3239 3240 // We only know the control subblock ID. 3241 switch (BlockID) { 3242 case llvm::bitc::BLOCKINFO_BLOCK_ID: 3243 if (Stream.ReadBlockInfoBlock()) { 3244 Error("malformed BlockInfoBlock in AST file"); 3245 return Failure; 3246 } 3247 break; 3248 case CONTROL_BLOCK_ID: 3249 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) { 3250 case Success: 3251 break; 3252 3253 case Failure: return Failure; 3254 case OutOfDate: return OutOfDate; 3255 case VersionMismatch: return VersionMismatch; 3256 case ConfigurationMismatch: return ConfigurationMismatch; 3257 case HadErrors: return HadErrors; 3258 } 3259 break; 3260 case AST_BLOCK_ID: 3261 // Record that we've loaded this module. 3262 Loaded.push_back(M); 3263 return Success; 3264 3265 default: 3266 if (Stream.SkipBlock()) { 3267 Error("malformed block record in AST file"); 3268 return Failure; 3269 } 3270 break; 3271 } 3272 } 3273 3274 return Success; 3275} 3276 3277void ASTReader::InitializeContext() { 3278 // If there's a listener, notify them that we "read" the translation unit. 3279 if (DeserializationListener) 3280 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, 3281 Context.getTranslationUnitDecl()); 3282 3283 // Make sure we load the declaration update records for the translation unit, 3284 // if there are any. 3285 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID, 3286 Context.getTranslationUnitDecl()); 3287 3288 // FIXME: Find a better way to deal with collisions between these 3289 // built-in types. Right now, we just ignore the problem. 3290 3291 // Load the special types. 3292 if (SpecialTypes.size() >= NumSpecialTypeIDs) { 3293 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) { 3294 if (!Context.CFConstantStringTypeDecl) 3295 Context.setCFConstantStringType(GetType(String)); 3296 } 3297 3298 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) { 3299 QualType FileType = GetType(File); 3300 if (FileType.isNull()) { 3301 Error("FILE type is NULL"); 3302 return; 3303 } 3304 3305 if (!Context.FILEDecl) { 3306 if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) 3307 Context.setFILEDecl(Typedef->getDecl()); 3308 else { 3309 const TagType *Tag = FileType->getAs<TagType>(); 3310 if (!Tag) { 3311 Error("Invalid FILE type in AST file"); 3312 return; 3313 } 3314 Context.setFILEDecl(Tag->getDecl()); 3315 } 3316 } 3317 } 3318 3319 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) { 3320 QualType Jmp_bufType = GetType(Jmp_buf); 3321 if (Jmp_bufType.isNull()) { 3322 Error("jmp_buf type is NULL"); 3323 return; 3324 } 3325 3326 if (!Context.jmp_bufDecl) { 3327 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) 3328 Context.setjmp_bufDecl(Typedef->getDecl()); 3329 else { 3330 const TagType *Tag = Jmp_bufType->getAs<TagType>(); 3331 if (!Tag) { 3332 Error("Invalid jmp_buf type in AST file"); 3333 return; 3334 } 3335 Context.setjmp_bufDecl(Tag->getDecl()); 3336 } 3337 } 3338 } 3339 3340 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) { 3341 QualType Sigjmp_bufType = GetType(Sigjmp_buf); 3342 if (Sigjmp_bufType.isNull()) { 3343 Error("sigjmp_buf type is NULL"); 3344 return; 3345 } 3346 3347 if (!Context.sigjmp_bufDecl) { 3348 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) 3349 Context.setsigjmp_bufDecl(Typedef->getDecl()); 3350 else { 3351 const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); 3352 assert(Tag && "Invalid sigjmp_buf type in AST file"); 3353 Context.setsigjmp_bufDecl(Tag->getDecl()); 3354 } 3355 } 3356 } 3357 3358 if (unsigned ObjCIdRedef 3359 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) { 3360 if (Context.ObjCIdRedefinitionType.isNull()) 3361 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef); 3362 } 3363 3364 if (unsigned ObjCClassRedef 3365 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) { 3366 if (Context.ObjCClassRedefinitionType.isNull()) 3367 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef); 3368 } 3369 3370 if (unsigned ObjCSelRedef 3371 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) { 3372 if (Context.ObjCSelRedefinitionType.isNull()) 3373 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef); 3374 } 3375 3376 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) { 3377 QualType Ucontext_tType = GetType(Ucontext_t); 3378 if (Ucontext_tType.isNull()) { 3379 Error("ucontext_t type is NULL"); 3380 return; 3381 } 3382 3383 if (!Context.ucontext_tDecl) { 3384 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>()) 3385 Context.setucontext_tDecl(Typedef->getDecl()); 3386 else { 3387 const TagType *Tag = Ucontext_tType->getAs<TagType>(); 3388 assert(Tag && "Invalid ucontext_t type in AST file"); 3389 Context.setucontext_tDecl(Tag->getDecl()); 3390 } 3391 } 3392 } 3393 } 3394 3395 ReadPragmaDiagnosticMappings(Context.getDiagnostics()); 3396 3397 // If there were any CUDA special declarations, deserialize them. 3398 if (!CUDASpecialDeclRefs.empty()) { 3399 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!"); 3400 Context.setcudaConfigureCallDecl( 3401 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0]))); 3402 } 3403 3404 // Re-export any modules that were imported by a non-module AST file. 3405 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) { 3406 if (Module *Imported = getSubmodule(ImportedModules[I])) 3407 makeModuleVisible(Imported, Module::AllVisible); 3408 } 3409 ImportedModules.clear(); 3410} 3411 3412void ASTReader::finalizeForWriting() { 3413 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(), 3414 HiddenEnd = HiddenNamesMap.end(); 3415 Hidden != HiddenEnd; ++Hidden) { 3416 makeNamesVisible(Hidden->second); 3417 } 3418 HiddenNamesMap.clear(); 3419} 3420 3421/// \brief Retrieve the name of the original source file name 3422/// directly from the AST file, without actually loading the AST 3423/// file. 3424std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName, 3425 FileManager &FileMgr, 3426 DiagnosticsEngine &Diags) { 3427 // Open the AST file. 3428 std::string ErrStr; 3429 OwningPtr<llvm::MemoryBuffer> Buffer; 3430 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr)); 3431 if (!Buffer) { 3432 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr; 3433 return std::string(); 3434 } 3435 3436 // Initialize the stream 3437 llvm::BitstreamReader StreamFile; 3438 llvm::BitstreamCursor Stream; 3439 StreamFile.init((const unsigned char *)Buffer->getBufferStart(), 3440 (const unsigned char *)Buffer->getBufferEnd()); 3441 Stream.init(StreamFile); 3442 3443 // Sniff for the signature. 3444 if (Stream.Read(8) != 'C' || 3445 Stream.Read(8) != 'P' || 3446 Stream.Read(8) != 'C' || 3447 Stream.Read(8) != 'H') { 3448 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName; 3449 return std::string(); 3450 } 3451 3452 RecordData Record; 3453 while (!Stream.AtEndOfStream()) { 3454 unsigned Code = Stream.ReadCode(); 3455 3456 if (Code == llvm::bitc::ENTER_SUBBLOCK) { 3457 unsigned BlockID = Stream.ReadSubBlockID(); 3458 3459 // We only know the AST subblock ID. 3460 switch (BlockID) { 3461 case CONTROL_BLOCK_ID: 3462 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) { 3463 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 3464 return std::string(); 3465 } 3466 break; 3467 3468 default: 3469 if (Stream.SkipBlock()) { 3470 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 3471 return std::string(); 3472 } 3473 break; 3474 } 3475 continue; 3476 } 3477 3478 if (Code == llvm::bitc::END_BLOCK) { 3479 if (Stream.ReadBlockEnd()) { 3480 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName; 3481 return std::string(); 3482 } 3483 continue; 3484 } 3485 3486 if (Code == llvm::bitc::DEFINE_ABBREV) { 3487 Stream.ReadAbbrevRecord(); 3488 continue; 3489 } 3490 3491 Record.clear(); 3492 const char *BlobStart = 0; 3493 unsigned BlobLen = 0; 3494 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen) == ORIGINAL_FILE) 3495 return std::string(BlobStart, BlobLen); 3496 } 3497 3498 return std::string(); 3499} 3500 3501namespace { 3502 class SimplePCHValidator : public ASTReaderListener { 3503 const LangOptions &ExistingLangOpts; 3504 const TargetOptions &ExistingTargetOpts; 3505 const PreprocessorOptions &ExistingPPOpts; 3506 3507 public: 3508 SimplePCHValidator(const LangOptions &ExistingLangOpts, 3509 const TargetOptions &ExistingTargetOpts, 3510 const PreprocessorOptions &ExistingPPOpts) 3511 : ExistingLangOpts(ExistingLangOpts), 3512 ExistingTargetOpts(ExistingTargetOpts), 3513 ExistingPPOpts(ExistingPPOpts) 3514 { 3515 } 3516 3517 virtual bool ReadLanguageOptions(const LangOptions &LangOpts, 3518 bool Complain) { 3519 return checkLanguageOptions(ExistingLangOpts, LangOpts, 0); 3520 } 3521 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts, 3522 bool Complain) { 3523 return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0); 3524 } 3525 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 3526 bool Complain) { 3527 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, 0); 3528 } 3529 }; 3530} 3531 3532bool ASTReader::isAcceptableASTFile(StringRef Filename, 3533 FileManager &FileMgr, 3534 const LangOptions &LangOpts, 3535 const TargetOptions &TargetOpts, 3536 const PreprocessorOptions &PPOpts) { 3537 // Open the AST file. 3538 std::string ErrStr; 3539 OwningPtr<llvm::MemoryBuffer> Buffer; 3540 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr)); 3541 if (!Buffer) { 3542 return false; 3543 } 3544 3545 // Initialize the stream 3546 llvm::BitstreamReader StreamFile; 3547 llvm::BitstreamCursor Stream; 3548 StreamFile.init((const unsigned char *)Buffer->getBufferStart(), 3549 (const unsigned char *)Buffer->getBufferEnd()); 3550 Stream.init(StreamFile); 3551 3552 // Sniff for the signature. 3553 if (Stream.Read(8) != 'C' || 3554 Stream.Read(8) != 'P' || 3555 Stream.Read(8) != 'C' || 3556 Stream.Read(8) != 'H') { 3557 return false; 3558 } 3559 3560 SimplePCHValidator Validator(LangOpts, TargetOpts, PPOpts); 3561 RecordData Record; 3562 bool InControlBlock = false; 3563 while (!Stream.AtEndOfStream()) { 3564 unsigned Code = Stream.ReadCode(); 3565 3566 if (Code == llvm::bitc::ENTER_SUBBLOCK) { 3567 unsigned BlockID = Stream.ReadSubBlockID(); 3568 3569 // We only know the control subblock ID. 3570 switch (BlockID) { 3571 case CONTROL_BLOCK_ID: 3572 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) { 3573 return false; 3574 } else { 3575 InControlBlock = true; 3576 } 3577 break; 3578 3579 default: 3580 if (Stream.SkipBlock()) 3581 return false; 3582 break; 3583 } 3584 continue; 3585 } 3586 3587 if (Code == llvm::bitc::END_BLOCK) { 3588 if (Stream.ReadBlockEnd()) { 3589 return false; 3590 } 3591 InControlBlock = false; 3592 continue; 3593 } 3594 3595 if (Code == llvm::bitc::DEFINE_ABBREV) { 3596 Stream.ReadAbbrevRecord(); 3597 continue; 3598 } 3599 3600 Record.clear(); 3601 const char *BlobStart = 0; 3602 unsigned BlobLen = 0; 3603 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen); 3604 if (InControlBlock) { 3605 switch ((ControlRecordTypes)RecCode) { 3606 case METADATA: { 3607 if (Record[0] != VERSION_MAJOR) { 3608 return false; 3609 } 3610 3611 const std::string &CurBranch = getClangFullRepositoryVersion(); 3612 StringRef ASTBranch(BlobStart, BlobLen); 3613 if (StringRef(CurBranch) != ASTBranch) 3614 return false; 3615 3616 break; 3617 } 3618 case LANGUAGE_OPTIONS: 3619 if (ParseLanguageOptions(Record, false, Validator)) 3620 return false; 3621 break; 3622 3623 case TARGET_OPTIONS: 3624 if (ParseTargetOptions(Record, false, Validator)) 3625 return false; 3626 break; 3627 3628 case DIAGNOSTIC_OPTIONS: 3629 if (ParseDiagnosticOptions(Record, false, Validator)) 3630 return false; 3631 break; 3632 3633 case FILE_SYSTEM_OPTIONS: 3634 if (ParseFileSystemOptions(Record, false, Validator)) 3635 return false; 3636 break; 3637 3638 case HEADER_SEARCH_OPTIONS: 3639 if (ParseHeaderSearchOptions(Record, false, Validator)) 3640 return false; 3641 break; 3642 3643 case PREPROCESSOR_OPTIONS: 3644 if (ParsePreprocessorOptions(Record, false, Validator)) 3645 return false; 3646 break; 3647 3648 default: 3649 // No other validation to perform. 3650 break; 3651 } 3652 } 3653 } 3654 3655 return true; 3656} 3657 3658bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) { 3659 // Enter the submodule block. 3660 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) { 3661 Error("malformed submodule block record in AST file"); 3662 return true; 3663 } 3664 3665 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap(); 3666 bool First = true; 3667 Module *CurrentModule = 0; 3668 RecordData Record; 3669 while (true) { 3670 unsigned Code = F.Stream.ReadCode(); 3671 if (Code == llvm::bitc::END_BLOCK) { 3672 if (F.Stream.ReadBlockEnd()) { 3673 Error("error at end of submodule block in AST file"); 3674 return true; 3675 } 3676 return false; 3677 } 3678 3679 if (Code == llvm::bitc::ENTER_SUBBLOCK) { 3680 // No known subblocks, always skip them. 3681 F.Stream.ReadSubBlockID(); 3682 if (F.Stream.SkipBlock()) { 3683 Error("malformed block record in AST file"); 3684 return true; 3685 } 3686 continue; 3687 } 3688 3689 if (Code == llvm::bitc::DEFINE_ABBREV) { 3690 F.Stream.ReadAbbrevRecord(); 3691 continue; 3692 } 3693 3694 // Read a record. 3695 const char *BlobStart; 3696 unsigned BlobLen; 3697 Record.clear(); 3698 switch (F.Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) { 3699 default: // Default behavior: ignore. 3700 break; 3701 3702 case SUBMODULE_DEFINITION: { 3703 if (First) { 3704 Error("missing submodule metadata record at beginning of block"); 3705 return true; 3706 } 3707 3708 if (Record.size() < 7) { 3709 Error("malformed module definition"); 3710 return true; 3711 } 3712 3713 StringRef Name(BlobStart, BlobLen); 3714 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]); 3715 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]); 3716 bool IsFramework = Record[2]; 3717 bool IsExplicit = Record[3]; 3718 bool IsSystem = Record[4]; 3719 bool InferSubmodules = Record[5]; 3720 bool InferExplicitSubmodules = Record[6]; 3721 bool InferExportWildcard = Record[7]; 3722 3723 Module *ParentModule = 0; 3724 if (Parent) 3725 ParentModule = getSubmodule(Parent); 3726 3727 // Retrieve this (sub)module from the module map, creating it if 3728 // necessary. 3729 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule, 3730 IsFramework, 3731 IsExplicit).first; 3732 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS; 3733 if (GlobalIndex >= SubmodulesLoaded.size() || 3734 SubmodulesLoaded[GlobalIndex]) { 3735 Error("too many submodules"); 3736 return true; 3737 } 3738 3739 CurrentModule->setASTFile(F.File); 3740 CurrentModule->IsFromModuleFile = true; 3741 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem; 3742 CurrentModule->InferSubmodules = InferSubmodules; 3743 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules; 3744 CurrentModule->InferExportWildcard = InferExportWildcard; 3745 if (DeserializationListener) 3746 DeserializationListener->ModuleRead(GlobalID, CurrentModule); 3747 3748 SubmodulesLoaded[GlobalIndex] = CurrentModule; 3749 break; 3750 } 3751 3752 case SUBMODULE_UMBRELLA_HEADER: { 3753 if (First) { 3754 Error("missing submodule metadata record at beginning of block"); 3755 return true; 3756 } 3757 3758 if (!CurrentModule) 3759 break; 3760 3761 StringRef FileName(BlobStart, BlobLen); 3762 if (const FileEntry *Umbrella = PP.getFileManager().getFile(FileName)) { 3763 if (!CurrentModule->getUmbrellaHeader()) 3764 ModMap.setUmbrellaHeader(CurrentModule, Umbrella); 3765 else if (CurrentModule->getUmbrellaHeader() != Umbrella) { 3766 Error("mismatched umbrella headers in submodule"); 3767 return true; 3768 } 3769 } 3770 break; 3771 } 3772 3773 case SUBMODULE_HEADER: { 3774 if (First) { 3775 Error("missing submodule metadata record at beginning of block"); 3776 return true; 3777 } 3778 3779 if (!CurrentModule) 3780 break; 3781 3782 // FIXME: Be more lazy about this! 3783 StringRef FileName(BlobStart, BlobLen); 3784 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) { 3785 if (std::find(CurrentModule->Headers.begin(), 3786 CurrentModule->Headers.end(), 3787 File) == CurrentModule->Headers.end()) 3788 ModMap.addHeader(CurrentModule, File, false); 3789 } 3790 break; 3791 } 3792 3793 case SUBMODULE_EXCLUDED_HEADER: { 3794 if (First) { 3795 Error("missing submodule metadata record at beginning of block"); 3796 return true; 3797 } 3798 3799 if (!CurrentModule) 3800 break; 3801 3802 // FIXME: Be more lazy about this! 3803 StringRef FileName(BlobStart, BlobLen); 3804 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) { 3805 if (std::find(CurrentModule->Headers.begin(), 3806 CurrentModule->Headers.end(), 3807 File) == CurrentModule->Headers.end()) 3808 ModMap.addHeader(CurrentModule, File, true); 3809 } 3810 break; 3811 } 3812 3813 case SUBMODULE_TOPHEADER: { 3814 if (First) { 3815 Error("missing submodule metadata record at beginning of block"); 3816 return true; 3817 } 3818 3819 if (!CurrentModule) 3820 break; 3821 3822 // FIXME: Be more lazy about this! 3823 StringRef FileName(BlobStart, BlobLen); 3824 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) 3825 CurrentModule->TopHeaders.insert(File); 3826 break; 3827 } 3828 3829 case SUBMODULE_UMBRELLA_DIR: { 3830 if (First) { 3831 Error("missing submodule metadata record at beginning of block"); 3832 return true; 3833 } 3834 3835 if (!CurrentModule) 3836 break; 3837 3838 StringRef DirName(BlobStart, BlobLen); 3839 if (const DirectoryEntry *Umbrella 3840 = PP.getFileManager().getDirectory(DirName)) { 3841 if (!CurrentModule->getUmbrellaDir()) 3842 ModMap.setUmbrellaDir(CurrentModule, Umbrella); 3843 else if (CurrentModule->getUmbrellaDir() != Umbrella) { 3844 Error("mismatched umbrella directories in submodule"); 3845 return true; 3846 } 3847 } 3848 break; 3849 } 3850 3851 case SUBMODULE_METADATA: { 3852 if (!First) { 3853 Error("submodule metadata record not at beginning of block"); 3854 return true; 3855 } 3856 First = false; 3857 3858 F.BaseSubmoduleID = getTotalNumSubmodules(); 3859 F.LocalNumSubmodules = Record[0]; 3860 unsigned LocalBaseSubmoduleID = Record[1]; 3861 if (F.LocalNumSubmodules > 0) { 3862 // Introduce the global -> local mapping for submodules within this 3863 // module. 3864 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F)); 3865 3866 // Introduce the local -> global mapping for submodules within this 3867 // module. 3868 F.SubmoduleRemap.insertOrReplace( 3869 std::make_pair(LocalBaseSubmoduleID, 3870 F.BaseSubmoduleID - LocalBaseSubmoduleID)); 3871 3872 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules); 3873 } 3874 break; 3875 } 3876 3877 case SUBMODULE_IMPORTS: { 3878 if (First) { 3879 Error("missing submodule metadata record at beginning of block"); 3880 return true; 3881 } 3882 3883 if (!CurrentModule) 3884 break; 3885 3886 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) { 3887 UnresolvedModuleImportExport Unresolved; 3888 Unresolved.File = &F; 3889 Unresolved.Mod = CurrentModule; 3890 Unresolved.ID = Record[Idx]; 3891 Unresolved.IsImport = true; 3892 Unresolved.IsWildcard = false; 3893 UnresolvedModuleImportExports.push_back(Unresolved); 3894 } 3895 break; 3896 } 3897 3898 case SUBMODULE_EXPORTS: { 3899 if (First) { 3900 Error("missing submodule metadata record at beginning of block"); 3901 return true; 3902 } 3903 3904 if (!CurrentModule) 3905 break; 3906 3907 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) { 3908 UnresolvedModuleImportExport Unresolved; 3909 Unresolved.File = &F; 3910 Unresolved.Mod = CurrentModule; 3911 Unresolved.ID = Record[Idx]; 3912 Unresolved.IsImport = false; 3913 Unresolved.IsWildcard = Record[Idx + 1]; 3914 UnresolvedModuleImportExports.push_back(Unresolved); 3915 } 3916 3917 // Once we've loaded the set of exports, there's no reason to keep 3918 // the parsed, unresolved exports around. 3919 CurrentModule->UnresolvedExports.clear(); 3920 break; 3921 } 3922 case SUBMODULE_REQUIRES: { 3923 if (First) { 3924 Error("missing submodule metadata record at beginning of block"); 3925 return true; 3926 } 3927 3928 if (!CurrentModule) 3929 break; 3930 3931 CurrentModule->addRequirement(StringRef(BlobStart, BlobLen), 3932 Context.getLangOpts(), 3933 Context.getTargetInfo()); 3934 break; 3935 } 3936 } 3937 } 3938} 3939 3940/// \brief Parse the record that corresponds to a LangOptions data 3941/// structure. 3942/// 3943/// This routine parses the language options from the AST file and then gives 3944/// them to the AST listener if one is set. 3945/// 3946/// \returns true if the listener deems the file unacceptable, false otherwise. 3947bool ASTReader::ParseLanguageOptions(const RecordData &Record, 3948 bool Complain, 3949 ASTReaderListener &Listener) { 3950 LangOptions LangOpts; 3951 unsigned Idx = 0; 3952#define LANGOPT(Name, Bits, Default, Description) \ 3953 LangOpts.Name = Record[Idx++]; 3954#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 3955 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++])); 3956#include "clang/Basic/LangOptions.def" 3957 3958 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++]; 3959 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx); 3960 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion); 3961 3962 unsigned Length = Record[Idx++]; 3963 LangOpts.CurrentModule.assign(Record.begin() + Idx, 3964 Record.begin() + Idx + Length); 3965 return Listener.ReadLanguageOptions(LangOpts, Complain); 3966} 3967 3968bool ASTReader::ParseTargetOptions(const RecordData &Record, 3969 bool Complain, 3970 ASTReaderListener &Listener) { 3971 unsigned Idx = 0; 3972 TargetOptions TargetOpts; 3973 TargetOpts.Triple = ReadString(Record, Idx); 3974 TargetOpts.CPU = ReadString(Record, Idx); 3975 TargetOpts.ABI = ReadString(Record, Idx); 3976 TargetOpts.CXXABI = ReadString(Record, Idx); 3977 TargetOpts.LinkerVersion = ReadString(Record, Idx); 3978 for (unsigned N = Record[Idx++]; N; --N) { 3979 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx)); 3980 } 3981 for (unsigned N = Record[Idx++]; N; --N) { 3982 TargetOpts.Features.push_back(ReadString(Record, Idx)); 3983 } 3984 3985 return Listener.ReadTargetOptions(TargetOpts, Complain); 3986} 3987 3988bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain, 3989 ASTReaderListener &Listener) { 3990 DiagnosticOptions DiagOpts; 3991 unsigned Idx = 0; 3992#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++]; 3993#define ENUM_DIAGOPT(Name, Type, Bits, Default) \ 3994 DiagOpts.set##Name(static_cast<Type>(Record[Idx++])); 3995#include "clang/Basic/DiagnosticOptions.def" 3996 3997 for (unsigned N = Record[Idx++]; N; --N) { 3998 DiagOpts.Warnings.push_back(ReadString(Record, Idx)); 3999 } 4000 4001 return Listener.ReadDiagnosticOptions(DiagOpts, Complain); 4002} 4003 4004bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain, 4005 ASTReaderListener &Listener) { 4006 FileSystemOptions FSOpts; 4007 unsigned Idx = 0; 4008 FSOpts.WorkingDir = ReadString(Record, Idx); 4009 return Listener.ReadFileSystemOptions(FSOpts, Complain); 4010} 4011 4012bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record, 4013 bool Complain, 4014 ASTReaderListener &Listener) { 4015 HeaderSearchOptions HSOpts; 4016 unsigned Idx = 0; 4017 HSOpts.Sysroot = ReadString(Record, Idx); 4018 4019 // Include entries. 4020 for (unsigned N = Record[Idx++]; N; --N) { 4021 std::string Path = ReadString(Record, Idx); 4022 frontend::IncludeDirGroup Group 4023 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]); 4024 bool IsUserSupplied = Record[Idx++]; 4025 bool IsFramework = Record[Idx++]; 4026 bool IgnoreSysRoot = Record[Idx++]; 4027 bool IsInternal = Record[Idx++]; 4028 bool ImplicitExternC = Record[Idx++]; 4029 HSOpts.UserEntries.push_back( 4030 HeaderSearchOptions::Entry(Path, Group, IsUserSupplied, IsFramework, 4031 IgnoreSysRoot, IsInternal, ImplicitExternC)); 4032 } 4033 4034 // System header prefixes. 4035 for (unsigned N = Record[Idx++]; N; --N) { 4036 std::string Prefix = ReadString(Record, Idx); 4037 bool IsSystemHeader = Record[Idx++]; 4038 HSOpts.SystemHeaderPrefixes.push_back( 4039 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader)); 4040 } 4041 4042 HSOpts.ResourceDir = ReadString(Record, Idx); 4043 HSOpts.ModuleCachePath = ReadString(Record, Idx); 4044 HSOpts.DisableModuleHash = Record[Idx++]; 4045 HSOpts.UseBuiltinIncludes = Record[Idx++]; 4046 HSOpts.UseStandardSystemIncludes = Record[Idx++]; 4047 HSOpts.UseStandardCXXIncludes = Record[Idx++]; 4048 HSOpts.UseLibcxx = Record[Idx++]; 4049 4050 return Listener.ReadHeaderSearchOptions(HSOpts, Complain); 4051} 4052 4053bool ASTReader::ParsePreprocessorOptions(const RecordData &Record, 4054 bool Complain, 4055 ASTReaderListener &Listener) { 4056 PreprocessorOptions PPOpts; 4057 unsigned Idx = 0; 4058 4059 // Macro definitions/undefs 4060 for (unsigned N = Record[Idx++]; N; --N) { 4061 std::string Macro = ReadString(Record, Idx); 4062 bool IsUndef = Record[Idx++]; 4063 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef)); 4064 } 4065 4066 // Includes 4067 for (unsigned N = Record[Idx++]; N; --N) { 4068 PPOpts.Includes.push_back(ReadString(Record, Idx)); 4069 } 4070 4071 // Macro Includes 4072 for (unsigned N = Record[Idx++]; N; --N) { 4073 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx)); 4074 } 4075 4076 PPOpts.UsePredefines = Record[Idx++]; 4077 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx); 4078 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx); 4079 PPOpts.ObjCXXARCStandardLibrary = 4080 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]); 4081 return Listener.ReadPreprocessorOptions(PPOpts, Complain); 4082} 4083 4084std::pair<ModuleFile *, unsigned> 4085ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) { 4086 GlobalPreprocessedEntityMapType::iterator 4087 I = GlobalPreprocessedEntityMap.find(GlobalIndex); 4088 assert(I != GlobalPreprocessedEntityMap.end() && 4089 "Corrupted global preprocessed entity map"); 4090 ModuleFile *M = I->second; 4091 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID; 4092 return std::make_pair(M, LocalIndex); 4093} 4094 4095std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator> 4096ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const { 4097 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord()) 4098 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID, 4099 Mod.NumPreprocessedEntities); 4100 4101 return std::make_pair(PreprocessingRecord::iterator(), 4102 PreprocessingRecord::iterator()); 4103} 4104 4105std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator> 4106ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) { 4107 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls), 4108 ModuleDeclIterator(this, &Mod, 4109 Mod.FileSortedDecls + Mod.NumFileSortedDecls)); 4110} 4111 4112PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) { 4113 PreprocessedEntityID PPID = Index+1; 4114 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 4115 ModuleFile &M = *PPInfo.first; 4116 unsigned LocalIndex = PPInfo.second; 4117 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 4118 4119 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor); 4120 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset); 4121 4122 unsigned Code = M.PreprocessorDetailCursor.ReadCode(); 4123 switch (Code) { 4124 case llvm::bitc::END_BLOCK: 4125 return 0; 4126 4127 case llvm::bitc::ENTER_SUBBLOCK: 4128 Error("unexpected subblock record in preprocessor detail block"); 4129 return 0; 4130 4131 case llvm::bitc::DEFINE_ABBREV: 4132 Error("unexpected abbrevation record in preprocessor detail block"); 4133 return 0; 4134 4135 default: 4136 break; 4137 } 4138 4139 if (!PP.getPreprocessingRecord()) { 4140 Error("no preprocessing record"); 4141 return 0; 4142 } 4143 4144 // Read the record. 4145 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin), 4146 ReadSourceLocation(M, PPOffs.End)); 4147 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 4148 const char *BlobStart = 0; 4149 unsigned BlobLen = 0; 4150 RecordData Record; 4151 PreprocessorDetailRecordTypes RecType = 4152 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.ReadRecord( 4153 Code, Record, BlobStart, BlobLen); 4154 switch (RecType) { 4155 case PPD_MACRO_EXPANSION: { 4156 bool isBuiltin = Record[0]; 4157 IdentifierInfo *Name = 0; 4158 MacroDefinition *Def = 0; 4159 if (isBuiltin) 4160 Name = getLocalIdentifier(M, Record[1]); 4161 else { 4162 PreprocessedEntityID 4163 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]); 4164 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1)); 4165 } 4166 4167 MacroExpansion *ME; 4168 if (isBuiltin) 4169 ME = new (PPRec) MacroExpansion(Name, Range); 4170 else 4171 ME = new (PPRec) MacroExpansion(Def, Range); 4172 4173 return ME; 4174 } 4175 4176 case PPD_MACRO_DEFINITION: { 4177 // Decode the identifier info and then check again; if the macro is 4178 // still defined and associated with the identifier, 4179 IdentifierInfo *II = getLocalIdentifier(M, Record[0]); 4180 MacroDefinition *MD 4181 = new (PPRec) MacroDefinition(II, Range); 4182 4183 if (DeserializationListener) 4184 DeserializationListener->MacroDefinitionRead(PPID, MD); 4185 4186 return MD; 4187 } 4188 4189 case PPD_INCLUSION_DIRECTIVE: { 4190 const char *FullFileNameStart = BlobStart + Record[0]; 4191 StringRef FullFileName(FullFileNameStart, BlobLen - Record[0]); 4192 const FileEntry *File = 0; 4193 if (!FullFileName.empty()) 4194 File = PP.getFileManager().getFile(FullFileName); 4195 4196 // FIXME: Stable encoding 4197 InclusionDirective::InclusionKind Kind 4198 = static_cast<InclusionDirective::InclusionKind>(Record[2]); 4199 InclusionDirective *ID 4200 = new (PPRec) InclusionDirective(PPRec, Kind, 4201 StringRef(BlobStart, Record[0]), 4202 Record[1], Record[3], 4203 File, 4204 Range); 4205 return ID; 4206 } 4207 } 4208 4209 llvm_unreachable("Invalid PreprocessorDetailRecordTypes"); 4210} 4211 4212/// \brief \arg SLocMapI points at a chunk of a module that contains no 4213/// preprocessed entities or the entities it contains are not the ones we are 4214/// looking for. Find the next module that contains entities and return the ID 4215/// of the first entry. 4216PreprocessedEntityID ASTReader::findNextPreprocessedEntity( 4217 GlobalSLocOffsetMapType::const_iterator SLocMapI) const { 4218 ++SLocMapI; 4219 for (GlobalSLocOffsetMapType::const_iterator 4220 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) { 4221 ModuleFile &M = *SLocMapI->second; 4222 if (M.NumPreprocessedEntities) 4223 return getGlobalPreprocessedEntityID(M, M.BasePreprocessedEntityID); 4224 } 4225 4226 return getTotalNumPreprocessedEntities(); 4227} 4228 4229namespace { 4230 4231template <unsigned PPEntityOffset::*PPLoc> 4232struct PPEntityComp { 4233 const ASTReader &Reader; 4234 ModuleFile &M; 4235 4236 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { } 4237 4238 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const { 4239 SourceLocation LHS = getLoc(L); 4240 SourceLocation RHS = getLoc(R); 4241 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 4242 } 4243 4244 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const { 4245 SourceLocation LHS = getLoc(L); 4246 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 4247 } 4248 4249 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const { 4250 SourceLocation RHS = getLoc(R); 4251 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 4252 } 4253 4254 SourceLocation getLoc(const PPEntityOffset &PPE) const { 4255 return Reader.ReadSourceLocation(M, PPE.*PPLoc); 4256 } 4257}; 4258 4259} 4260 4261/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc. 4262PreprocessedEntityID 4263ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const { 4264 if (SourceMgr.isLocalSourceLocation(BLoc)) 4265 return getTotalNumPreprocessedEntities(); 4266 4267 GlobalSLocOffsetMapType::const_iterator 4268 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset - 4269 BLoc.getOffset()); 4270 assert(SLocMapI != GlobalSLocOffsetMap.end() && 4271 "Corrupted global sloc offset map"); 4272 4273 if (SLocMapI->second->NumPreprocessedEntities == 0) 4274 return findNextPreprocessedEntity(SLocMapI); 4275 4276 ModuleFile &M = *SLocMapI->second; 4277 typedef const PPEntityOffset *pp_iterator; 4278 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 4279 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 4280 4281 size_t Count = M.NumPreprocessedEntities; 4282 size_t Half; 4283 pp_iterator First = pp_begin; 4284 pp_iterator PPI; 4285 4286 // Do a binary search manually instead of using std::lower_bound because 4287 // The end locations of entities may be unordered (when a macro expansion 4288 // is inside another macro argument), but for this case it is not important 4289 // whether we get the first macro expansion or its containing macro. 4290 while (Count > 0) { 4291 Half = Count/2; 4292 PPI = First; 4293 std::advance(PPI, Half); 4294 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End), 4295 BLoc)){ 4296 First = PPI; 4297 ++First; 4298 Count = Count - Half - 1; 4299 } else 4300 Count = Half; 4301 } 4302 4303 if (PPI == pp_end) 4304 return findNextPreprocessedEntity(SLocMapI); 4305 4306 return getGlobalPreprocessedEntityID(M, 4307 M.BasePreprocessedEntityID + (PPI - pp_begin)); 4308} 4309 4310/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc. 4311PreprocessedEntityID 4312ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const { 4313 if (SourceMgr.isLocalSourceLocation(ELoc)) 4314 return getTotalNumPreprocessedEntities(); 4315 4316 GlobalSLocOffsetMapType::const_iterator 4317 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset - 4318 ELoc.getOffset()); 4319 assert(SLocMapI != GlobalSLocOffsetMap.end() && 4320 "Corrupted global sloc offset map"); 4321 4322 if (SLocMapI->second->NumPreprocessedEntities == 0) 4323 return findNextPreprocessedEntity(SLocMapI); 4324 4325 ModuleFile &M = *SLocMapI->second; 4326 typedef const PPEntityOffset *pp_iterator; 4327 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 4328 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 4329 pp_iterator PPI = 4330 std::upper_bound(pp_begin, pp_end, ELoc, 4331 PPEntityComp<&PPEntityOffset::Begin>(*this, M)); 4332 4333 if (PPI == pp_end) 4334 return findNextPreprocessedEntity(SLocMapI); 4335 4336 return getGlobalPreprocessedEntityID(M, 4337 M.BasePreprocessedEntityID + (PPI - pp_begin)); 4338} 4339 4340/// \brief Returns a pair of [Begin, End) indices of preallocated 4341/// preprocessed entities that \arg Range encompasses. 4342std::pair<unsigned, unsigned> 4343 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) { 4344 if (Range.isInvalid()) 4345 return std::make_pair(0,0); 4346 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); 4347 4348 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin()); 4349 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd()); 4350 return std::make_pair(BeginID, EndID); 4351} 4352 4353/// \brief Optionally returns true or false if the preallocated preprocessed 4354/// entity with index \arg Index came from file \arg FID. 4355llvm::Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index, 4356 FileID FID) { 4357 if (FID.isInvalid()) 4358 return false; 4359 4360 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 4361 ModuleFile &M = *PPInfo.first; 4362 unsigned LocalIndex = PPInfo.second; 4363 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 4364 4365 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin); 4366 if (Loc.isInvalid()) 4367 return false; 4368 4369 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID)) 4370 return true; 4371 else 4372 return false; 4373} 4374 4375namespace { 4376 /// \brief Visitor used to search for information about a header file. 4377 class HeaderFileInfoVisitor { 4378 ASTReader &Reader; 4379 const FileEntry *FE; 4380 4381 llvm::Optional<HeaderFileInfo> HFI; 4382 4383 public: 4384 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE) 4385 : Reader(Reader), FE(FE) { } 4386 4387 static bool visit(ModuleFile &M, void *UserData) { 4388 HeaderFileInfoVisitor *This 4389 = static_cast<HeaderFileInfoVisitor *>(UserData); 4390 4391 HeaderFileInfoTrait Trait(This->Reader, M, 4392 &This->Reader.getPreprocessor().getHeaderSearchInfo(), 4393 M.HeaderFileFrameworkStrings, 4394 This->FE->getName()); 4395 4396 HeaderFileInfoLookupTable *Table 4397 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable); 4398 if (!Table) 4399 return false; 4400 4401 // Look in the on-disk hash table for an entry for this file name. 4402 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(), 4403 &Trait); 4404 if (Pos == Table->end()) 4405 return false; 4406 4407 This->HFI = *Pos; 4408 return true; 4409 } 4410 4411 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; } 4412 }; 4413} 4414 4415HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) { 4416 HeaderFileInfoVisitor Visitor(*this, FE); 4417 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor); 4418 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) { 4419 if (Listener) 4420 Listener->ReadHeaderFileInfo(*HFI, FE->getUID()); 4421 return *HFI; 4422 } 4423 4424 return HeaderFileInfo(); 4425} 4426 4427void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) { 4428 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) { 4429 ModuleFile &F = *(*I); 4430 unsigned Idx = 0; 4431 while (Idx < F.PragmaDiagMappings.size()) { 4432 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]); 4433 Diag.DiagStates.push_back(*Diag.GetCurDiagState()); 4434 Diag.DiagStatePoints.push_back( 4435 DiagnosticsEngine::DiagStatePoint(&Diag.DiagStates.back(), 4436 FullSourceLoc(Loc, SourceMgr))); 4437 while (1) { 4438 assert(Idx < F.PragmaDiagMappings.size() && 4439 "Invalid data, didn't find '-1' marking end of diag/map pairs"); 4440 if (Idx >= F.PragmaDiagMappings.size()) { 4441 break; // Something is messed up but at least avoid infinite loop in 4442 // release build. 4443 } 4444 unsigned DiagID = F.PragmaDiagMappings[Idx++]; 4445 if (DiagID == (unsigned)-1) { 4446 break; // no more diag/map pairs for this location. 4447 } 4448 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++]; 4449 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc); 4450 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo); 4451 } 4452 } 4453 } 4454} 4455 4456/// \brief Get the correct cursor and offset for loading a type. 4457ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) { 4458 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index); 4459 assert(I != GlobalTypeMap.end() && "Corrupted global type map"); 4460 ModuleFile *M = I->second; 4461 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]); 4462} 4463 4464/// \brief Read and return the type with the given index.. 4465/// 4466/// The index is the type ID, shifted and minus the number of predefs. This 4467/// routine actually reads the record corresponding to the type at the given 4468/// location. It is a helper routine for GetType, which deals with reading type 4469/// IDs. 4470QualType ASTReader::readTypeRecord(unsigned Index) { 4471 RecordLocation Loc = TypeCursorForIndex(Index); 4472 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 4473 4474 // Keep track of where we are in the stream, then jump back there 4475 // after reading this type. 4476 SavedStreamPosition SavedPosition(DeclsCursor); 4477 4478 ReadingKindTracker ReadingKind(Read_Type, *this); 4479 4480 // Note that we are loading a type record. 4481 Deserializing AType(this); 4482 4483 unsigned Idx = 0; 4484 DeclsCursor.JumpToBit(Loc.Offset); 4485 RecordData Record; 4486 unsigned Code = DeclsCursor.ReadCode(); 4487 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) { 4488 case TYPE_EXT_QUAL: { 4489 if (Record.size() != 2) { 4490 Error("Incorrect encoding of extended qualifier type"); 4491 return QualType(); 4492 } 4493 QualType Base = readType(*Loc.F, Record, Idx); 4494 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]); 4495 return Context.getQualifiedType(Base, Quals); 4496 } 4497 4498 case TYPE_COMPLEX: { 4499 if (Record.size() != 1) { 4500 Error("Incorrect encoding of complex type"); 4501 return QualType(); 4502 } 4503 QualType ElemType = readType(*Loc.F, Record, Idx); 4504 return Context.getComplexType(ElemType); 4505 } 4506 4507 case TYPE_POINTER: { 4508 if (Record.size() != 1) { 4509 Error("Incorrect encoding of pointer type"); 4510 return QualType(); 4511 } 4512 QualType PointeeType = readType(*Loc.F, Record, Idx); 4513 return Context.getPointerType(PointeeType); 4514 } 4515 4516 case TYPE_BLOCK_POINTER: { 4517 if (Record.size() != 1) { 4518 Error("Incorrect encoding of block pointer type"); 4519 return QualType(); 4520 } 4521 QualType PointeeType = readType(*Loc.F, Record, Idx); 4522 return Context.getBlockPointerType(PointeeType); 4523 } 4524 4525 case TYPE_LVALUE_REFERENCE: { 4526 if (Record.size() != 2) { 4527 Error("Incorrect encoding of lvalue reference type"); 4528 return QualType(); 4529 } 4530 QualType PointeeType = readType(*Loc.F, Record, Idx); 4531 return Context.getLValueReferenceType(PointeeType, Record[1]); 4532 } 4533 4534 case TYPE_RVALUE_REFERENCE: { 4535 if (Record.size() != 1) { 4536 Error("Incorrect encoding of rvalue reference type"); 4537 return QualType(); 4538 } 4539 QualType PointeeType = readType(*Loc.F, Record, Idx); 4540 return Context.getRValueReferenceType(PointeeType); 4541 } 4542 4543 case TYPE_MEMBER_POINTER: { 4544 if (Record.size() != 2) { 4545 Error("Incorrect encoding of member pointer type"); 4546 return QualType(); 4547 } 4548 QualType PointeeType = readType(*Loc.F, Record, Idx); 4549 QualType ClassType = readType(*Loc.F, Record, Idx); 4550 if (PointeeType.isNull() || ClassType.isNull()) 4551 return QualType(); 4552 4553 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr()); 4554 } 4555 4556 case TYPE_CONSTANT_ARRAY: { 4557 QualType ElementType = readType(*Loc.F, Record, Idx); 4558 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 4559 unsigned IndexTypeQuals = Record[2]; 4560 unsigned Idx = 3; 4561 llvm::APInt Size = ReadAPInt(Record, Idx); 4562 return Context.getConstantArrayType(ElementType, Size, 4563 ASM, IndexTypeQuals); 4564 } 4565 4566 case TYPE_INCOMPLETE_ARRAY: { 4567 QualType ElementType = readType(*Loc.F, Record, Idx); 4568 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 4569 unsigned IndexTypeQuals = Record[2]; 4570 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals); 4571 } 4572 4573 case TYPE_VARIABLE_ARRAY: { 4574 QualType ElementType = readType(*Loc.F, Record, Idx); 4575 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 4576 unsigned IndexTypeQuals = Record[2]; 4577 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]); 4578 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]); 4579 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F), 4580 ASM, IndexTypeQuals, 4581 SourceRange(LBLoc, RBLoc)); 4582 } 4583 4584 case TYPE_VECTOR: { 4585 if (Record.size() != 3) { 4586 Error("incorrect encoding of vector type in AST file"); 4587 return QualType(); 4588 } 4589 4590 QualType ElementType = readType(*Loc.F, Record, Idx); 4591 unsigned NumElements = Record[1]; 4592 unsigned VecKind = Record[2]; 4593 return Context.getVectorType(ElementType, NumElements, 4594 (VectorType::VectorKind)VecKind); 4595 } 4596 4597 case TYPE_EXT_VECTOR: { 4598 if (Record.size() != 3) { 4599 Error("incorrect encoding of extended vector type in AST file"); 4600 return QualType(); 4601 } 4602 4603 QualType ElementType = readType(*Loc.F, Record, Idx); 4604 unsigned NumElements = Record[1]; 4605 return Context.getExtVectorType(ElementType, NumElements); 4606 } 4607 4608 case TYPE_FUNCTION_NO_PROTO: { 4609 if (Record.size() != 6) { 4610 Error("incorrect encoding of no-proto function type"); 4611 return QualType(); 4612 } 4613 QualType ResultType = readType(*Loc.F, Record, Idx); 4614 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3], 4615 (CallingConv)Record[4], Record[5]); 4616 return Context.getFunctionNoProtoType(ResultType, Info); 4617 } 4618 4619 case TYPE_FUNCTION_PROTO: { 4620 QualType ResultType = readType(*Loc.F, Record, Idx); 4621 4622 FunctionProtoType::ExtProtoInfo EPI; 4623 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1], 4624 /*hasregparm*/ Record[2], 4625 /*regparm*/ Record[3], 4626 static_cast<CallingConv>(Record[4]), 4627 /*produces*/ Record[5]); 4628 4629 unsigned Idx = 6; 4630 unsigned NumParams = Record[Idx++]; 4631 SmallVector<QualType, 16> ParamTypes; 4632 for (unsigned I = 0; I != NumParams; ++I) 4633 ParamTypes.push_back(readType(*Loc.F, Record, Idx)); 4634 4635 EPI.Variadic = Record[Idx++]; 4636 EPI.HasTrailingReturn = Record[Idx++]; 4637 EPI.TypeQuals = Record[Idx++]; 4638 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]); 4639 ExceptionSpecificationType EST = 4640 static_cast<ExceptionSpecificationType>(Record[Idx++]); 4641 EPI.ExceptionSpecType = EST; 4642 SmallVector<QualType, 2> Exceptions; 4643 if (EST == EST_Dynamic) { 4644 EPI.NumExceptions = Record[Idx++]; 4645 for (unsigned I = 0; I != EPI.NumExceptions; ++I) 4646 Exceptions.push_back(readType(*Loc.F, Record, Idx)); 4647 EPI.Exceptions = Exceptions.data(); 4648 } else if (EST == EST_ComputedNoexcept) { 4649 EPI.NoexceptExpr = ReadExpr(*Loc.F); 4650 } else if (EST == EST_Uninstantiated) { 4651 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx); 4652 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx); 4653 } else if (EST == EST_Unevaluated) { 4654 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx); 4655 } 4656 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams, 4657 EPI); 4658 } 4659 4660 case TYPE_UNRESOLVED_USING: { 4661 unsigned Idx = 0; 4662 return Context.getTypeDeclType( 4663 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx)); 4664 } 4665 4666 case TYPE_TYPEDEF: { 4667 if (Record.size() != 2) { 4668 Error("incorrect encoding of typedef type"); 4669 return QualType(); 4670 } 4671 unsigned Idx = 0; 4672 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx); 4673 QualType Canonical = readType(*Loc.F, Record, Idx); 4674 if (!Canonical.isNull()) 4675 Canonical = Context.getCanonicalType(Canonical); 4676 return Context.getTypedefType(Decl, Canonical); 4677 } 4678 4679 case TYPE_TYPEOF_EXPR: 4680 return Context.getTypeOfExprType(ReadExpr(*Loc.F)); 4681 4682 case TYPE_TYPEOF: { 4683 if (Record.size() != 1) { 4684 Error("incorrect encoding of typeof(type) in AST file"); 4685 return QualType(); 4686 } 4687 QualType UnderlyingType = readType(*Loc.F, Record, Idx); 4688 return Context.getTypeOfType(UnderlyingType); 4689 } 4690 4691 case TYPE_DECLTYPE: { 4692 QualType UnderlyingType = readType(*Loc.F, Record, Idx); 4693 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType); 4694 } 4695 4696 case TYPE_UNARY_TRANSFORM: { 4697 QualType BaseType = readType(*Loc.F, Record, Idx); 4698 QualType UnderlyingType = readType(*Loc.F, Record, Idx); 4699 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2]; 4700 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind); 4701 } 4702 4703 case TYPE_AUTO: 4704 return Context.getAutoType(readType(*Loc.F, Record, Idx)); 4705 4706 case TYPE_RECORD: { 4707 if (Record.size() != 2) { 4708 Error("incorrect encoding of record type"); 4709 return QualType(); 4710 } 4711 unsigned Idx = 0; 4712 bool IsDependent = Record[Idx++]; 4713 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx); 4714 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl()); 4715 QualType T = Context.getRecordType(RD); 4716 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 4717 return T; 4718 } 4719 4720 case TYPE_ENUM: { 4721 if (Record.size() != 2) { 4722 Error("incorrect encoding of enum type"); 4723 return QualType(); 4724 } 4725 unsigned Idx = 0; 4726 bool IsDependent = Record[Idx++]; 4727 QualType T 4728 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx)); 4729 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 4730 return T; 4731 } 4732 4733 case TYPE_ATTRIBUTED: { 4734 if (Record.size() != 3) { 4735 Error("incorrect encoding of attributed type"); 4736 return QualType(); 4737 } 4738 QualType modifiedType = readType(*Loc.F, Record, Idx); 4739 QualType equivalentType = readType(*Loc.F, Record, Idx); 4740 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]); 4741 return Context.getAttributedType(kind, modifiedType, equivalentType); 4742 } 4743 4744 case TYPE_PAREN: { 4745 if (Record.size() != 1) { 4746 Error("incorrect encoding of paren type"); 4747 return QualType(); 4748 } 4749 QualType InnerType = readType(*Loc.F, Record, Idx); 4750 return Context.getParenType(InnerType); 4751 } 4752 4753 case TYPE_PACK_EXPANSION: { 4754 if (Record.size() != 2) { 4755 Error("incorrect encoding of pack expansion type"); 4756 return QualType(); 4757 } 4758 QualType Pattern = readType(*Loc.F, Record, Idx); 4759 if (Pattern.isNull()) 4760 return QualType(); 4761 llvm::Optional<unsigned> NumExpansions; 4762 if (Record[1]) 4763 NumExpansions = Record[1] - 1; 4764 return Context.getPackExpansionType(Pattern, NumExpansions); 4765 } 4766 4767 case TYPE_ELABORATED: { 4768 unsigned Idx = 0; 4769 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 4770 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 4771 QualType NamedType = readType(*Loc.F, Record, Idx); 4772 return Context.getElaboratedType(Keyword, NNS, NamedType); 4773 } 4774 4775 case TYPE_OBJC_INTERFACE: { 4776 unsigned Idx = 0; 4777 ObjCInterfaceDecl *ItfD 4778 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx); 4779 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl()); 4780 } 4781 4782 case TYPE_OBJC_OBJECT: { 4783 unsigned Idx = 0; 4784 QualType Base = readType(*Loc.F, Record, Idx); 4785 unsigned NumProtos = Record[Idx++]; 4786 SmallVector<ObjCProtocolDecl*, 4> Protos; 4787 for (unsigned I = 0; I != NumProtos; ++I) 4788 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx)); 4789 return Context.getObjCObjectType(Base, Protos.data(), NumProtos); 4790 } 4791 4792 case TYPE_OBJC_OBJECT_POINTER: { 4793 unsigned Idx = 0; 4794 QualType Pointee = readType(*Loc.F, Record, Idx); 4795 return Context.getObjCObjectPointerType(Pointee); 4796 } 4797 4798 case TYPE_SUBST_TEMPLATE_TYPE_PARM: { 4799 unsigned Idx = 0; 4800 QualType Parm = readType(*Loc.F, Record, Idx); 4801 QualType Replacement = readType(*Loc.F, Record, Idx); 4802 return 4803 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm), 4804 Replacement); 4805 } 4806 4807 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: { 4808 unsigned Idx = 0; 4809 QualType Parm = readType(*Loc.F, Record, Idx); 4810 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx); 4811 return Context.getSubstTemplateTypeParmPackType( 4812 cast<TemplateTypeParmType>(Parm), 4813 ArgPack); 4814 } 4815 4816 case TYPE_INJECTED_CLASS_NAME: { 4817 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx); 4818 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable 4819 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable 4820 // for AST reading, too much interdependencies. 4821 return 4822 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0); 4823 } 4824 4825 case TYPE_TEMPLATE_TYPE_PARM: { 4826 unsigned Idx = 0; 4827 unsigned Depth = Record[Idx++]; 4828 unsigned Index = Record[Idx++]; 4829 bool Pack = Record[Idx++]; 4830 TemplateTypeParmDecl *D 4831 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx); 4832 return Context.getTemplateTypeParmType(Depth, Index, Pack, D); 4833 } 4834 4835 case TYPE_DEPENDENT_NAME: { 4836 unsigned Idx = 0; 4837 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 4838 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 4839 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx); 4840 QualType Canon = readType(*Loc.F, Record, Idx); 4841 if (!Canon.isNull()) 4842 Canon = Context.getCanonicalType(Canon); 4843 return Context.getDependentNameType(Keyword, NNS, Name, Canon); 4844 } 4845 4846 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: { 4847 unsigned Idx = 0; 4848 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 4849 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 4850 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx); 4851 unsigned NumArgs = Record[Idx++]; 4852 SmallVector<TemplateArgument, 8> Args; 4853 Args.reserve(NumArgs); 4854 while (NumArgs--) 4855 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx)); 4856 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name, 4857 Args.size(), Args.data()); 4858 } 4859 4860 case TYPE_DEPENDENT_SIZED_ARRAY: { 4861 unsigned Idx = 0; 4862 4863 // ArrayType 4864 QualType ElementType = readType(*Loc.F, Record, Idx); 4865 ArrayType::ArraySizeModifier ASM 4866 = (ArrayType::ArraySizeModifier)Record[Idx++]; 4867 unsigned IndexTypeQuals = Record[Idx++]; 4868 4869 // DependentSizedArrayType 4870 Expr *NumElts = ReadExpr(*Loc.F); 4871 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx); 4872 4873 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM, 4874 IndexTypeQuals, Brackets); 4875 } 4876 4877 case TYPE_TEMPLATE_SPECIALIZATION: { 4878 unsigned Idx = 0; 4879 bool IsDependent = Record[Idx++]; 4880 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx); 4881 SmallVector<TemplateArgument, 8> Args; 4882 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx); 4883 QualType Underlying = readType(*Loc.F, Record, Idx); 4884 QualType T; 4885 if (Underlying.isNull()) 4886 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(), 4887 Args.size()); 4888 else 4889 T = Context.getTemplateSpecializationType(Name, Args.data(), 4890 Args.size(), Underlying); 4891 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 4892 return T; 4893 } 4894 4895 case TYPE_ATOMIC: { 4896 if (Record.size() != 1) { 4897 Error("Incorrect encoding of atomic type"); 4898 return QualType(); 4899 } 4900 QualType ValueType = readType(*Loc.F, Record, Idx); 4901 return Context.getAtomicType(ValueType); 4902 } 4903 } 4904 llvm_unreachable("Invalid TypeCode!"); 4905} 4906 4907class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> { 4908 ASTReader &Reader; 4909 ModuleFile &F; 4910 const ASTReader::RecordData &Record; 4911 unsigned &Idx; 4912 4913 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R, 4914 unsigned &I) { 4915 return Reader.ReadSourceLocation(F, R, I); 4916 } 4917 4918 template<typename T> 4919 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) { 4920 return Reader.ReadDeclAs<T>(F, Record, Idx); 4921 } 4922 4923public: 4924 TypeLocReader(ASTReader &Reader, ModuleFile &F, 4925 const ASTReader::RecordData &Record, unsigned &Idx) 4926 : Reader(Reader), F(F), Record(Record), Idx(Idx) 4927 { } 4928 4929 // We want compile-time assurance that we've enumerated all of 4930 // these, so unfortunately we have to declare them first, then 4931 // define them out-of-line. 4932#define ABSTRACT_TYPELOC(CLASS, PARENT) 4933#define TYPELOC(CLASS, PARENT) \ 4934 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 4935#include "clang/AST/TypeLocNodes.def" 4936 4937 void VisitFunctionTypeLoc(FunctionTypeLoc); 4938 void VisitArrayTypeLoc(ArrayTypeLoc); 4939}; 4940 4941void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 4942 // nothing to do 4943} 4944void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 4945 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx)); 4946 if (TL.needsExtraLocalData()) { 4947 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++])); 4948 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++])); 4949 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++])); 4950 TL.setModeAttr(Record[Idx++]); 4951 } 4952} 4953void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { 4954 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4955} 4956void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { 4957 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 4958} 4959void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 4960 TL.setCaretLoc(ReadSourceLocation(Record, Idx)); 4961} 4962void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 4963 TL.setAmpLoc(ReadSourceLocation(Record, Idx)); 4964} 4965void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 4966 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx)); 4967} 4968void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 4969 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 4970 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx)); 4971} 4972void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { 4973 TL.setLBracketLoc(ReadSourceLocation(Record, Idx)); 4974 TL.setRBracketLoc(ReadSourceLocation(Record, Idx)); 4975 if (Record[Idx++]) 4976 TL.setSizeExpr(Reader.ReadExpr(F)); 4977 else 4978 TL.setSizeExpr(0); 4979} 4980void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 4981 VisitArrayTypeLoc(TL); 4982} 4983void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 4984 VisitArrayTypeLoc(TL); 4985} 4986void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 4987 VisitArrayTypeLoc(TL); 4988} 4989void TypeLocReader::VisitDependentSizedArrayTypeLoc( 4990 DependentSizedArrayTypeLoc TL) { 4991 VisitArrayTypeLoc(TL); 4992} 4993void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( 4994 DependentSizedExtVectorTypeLoc TL) { 4995 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4996} 4997void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { 4998 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 4999} 5000void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 5001 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5002} 5003void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 5004 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx)); 5005 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 5006 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 5007 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx)); 5008 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) { 5009 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx)); 5010 } 5011} 5012void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 5013 VisitFunctionTypeLoc(TL); 5014} 5015void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 5016 VisitFunctionTypeLoc(TL); 5017} 5018void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 5019 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5020} 5021void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 5022 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5023} 5024void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 5025 TL.setTypeofLoc(ReadSourceLocation(Record, Idx)); 5026 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 5027 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 5028} 5029void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 5030 TL.setTypeofLoc(ReadSourceLocation(Record, Idx)); 5031 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 5032 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 5033 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx)); 5034} 5035void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 5036 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5037} 5038void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { 5039 TL.setKWLoc(ReadSourceLocation(Record, Idx)); 5040 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 5041 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 5042 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx)); 5043} 5044void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) { 5045 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5046} 5047void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { 5048 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5049} 5050void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { 5051 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5052} 5053void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 5054 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx)); 5055 if (TL.hasAttrOperand()) { 5056 SourceRange range; 5057 range.setBegin(ReadSourceLocation(Record, Idx)); 5058 range.setEnd(ReadSourceLocation(Record, Idx)); 5059 TL.setAttrOperandParensRange(range); 5060 } 5061 if (TL.hasAttrExprOperand()) { 5062 if (Record[Idx++]) 5063 TL.setAttrExprOperand(Reader.ReadExpr(F)); 5064 else 5065 TL.setAttrExprOperand(0); 5066 } else if (TL.hasAttrEnumOperand()) 5067 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx)); 5068} 5069void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 5070 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5071} 5072void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( 5073 SubstTemplateTypeParmTypeLoc TL) { 5074 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5075} 5076void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc( 5077 SubstTemplateTypeParmPackTypeLoc TL) { 5078 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5079} 5080void TypeLocReader::VisitTemplateSpecializationTypeLoc( 5081 TemplateSpecializationTypeLoc TL) { 5082 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx)); 5083 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx)); 5084 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 5085 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 5086 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 5087 TL.setArgLocInfo(i, 5088 Reader.GetTemplateArgumentLocInfo(F, 5089 TL.getTypePtr()->getArg(i).getKind(), 5090 Record, Idx)); 5091} 5092void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) { 5093 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 5094 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 5095} 5096void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 5097 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx)); 5098 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx)); 5099} 5100void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 5101 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5102} 5103void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 5104 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx)); 5105 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx)); 5106 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5107} 5108void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( 5109 DependentTemplateSpecializationTypeLoc TL) { 5110 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx)); 5111 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx)); 5112 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx)); 5113 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx)); 5114 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 5115 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 5116 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 5117 TL.setArgLocInfo(I, 5118 Reader.GetTemplateArgumentLocInfo(F, 5119 TL.getTypePtr()->getArg(I).getKind(), 5120 Record, Idx)); 5121} 5122void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 5123 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx)); 5124} 5125void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 5126 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5127} 5128void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 5129 TL.setHasBaseTypeAsWritten(Record[Idx++]); 5130 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 5131 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 5132 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 5133 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx)); 5134} 5135void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 5136 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 5137} 5138void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) { 5139 TL.setKWLoc(ReadSourceLocation(Record, Idx)); 5140 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 5141 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 5142} 5143 5144TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F, 5145 const RecordData &Record, 5146 unsigned &Idx) { 5147 QualType InfoTy = readType(F, Record, Idx); 5148 if (InfoTy.isNull()) 5149 return 0; 5150 5151 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy); 5152 TypeLocReader TLR(*this, F, Record, Idx); 5153 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc()) 5154 TLR.Visit(TL); 5155 return TInfo; 5156} 5157 5158QualType ASTReader::GetType(TypeID ID) { 5159 unsigned FastQuals = ID & Qualifiers::FastMask; 5160 unsigned Index = ID >> Qualifiers::FastWidth; 5161 5162 if (Index < NUM_PREDEF_TYPE_IDS) { 5163 QualType T; 5164 switch ((PredefinedTypeIDs)Index) { 5165 case PREDEF_TYPE_NULL_ID: return QualType(); 5166 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break; 5167 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break; 5168 5169 case PREDEF_TYPE_CHAR_U_ID: 5170 case PREDEF_TYPE_CHAR_S_ID: 5171 // FIXME: Check that the signedness of CharTy is correct! 5172 T = Context.CharTy; 5173 break; 5174 5175 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break; 5176 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break; 5177 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break; 5178 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break; 5179 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break; 5180 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break; 5181 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break; 5182 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break; 5183 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break; 5184 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break; 5185 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break; 5186 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break; 5187 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break; 5188 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break; 5189 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break; 5190 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break; 5191 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break; 5192 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break; 5193 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break; 5194 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break; 5195 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break; 5196 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break; 5197 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break; 5198 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break; 5199 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break; 5200 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break; 5201 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break; 5202 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break; 5203 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break; 5204 5205 case PREDEF_TYPE_AUTO_RREF_DEDUCT: 5206 T = Context.getAutoRRefDeductType(); 5207 break; 5208 5209 case PREDEF_TYPE_ARC_UNBRIDGED_CAST: 5210 T = Context.ARCUnbridgedCastTy; 5211 break; 5212 5213 case PREDEF_TYPE_VA_LIST_TAG: 5214 T = Context.getVaListTagType(); 5215 break; 5216 5217 case PREDEF_TYPE_BUILTIN_FN: 5218 T = Context.BuiltinFnTy; 5219 break; 5220 } 5221 5222 assert(!T.isNull() && "Unknown predefined type"); 5223 return T.withFastQualifiers(FastQuals); 5224 } 5225 5226 Index -= NUM_PREDEF_TYPE_IDS; 5227 assert(Index < TypesLoaded.size() && "Type index out-of-range"); 5228 if (TypesLoaded[Index].isNull()) { 5229 TypesLoaded[Index] = readTypeRecord(Index); 5230 if (TypesLoaded[Index].isNull()) 5231 return QualType(); 5232 5233 TypesLoaded[Index]->setFromAST(); 5234 if (DeserializationListener) 5235 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID), 5236 TypesLoaded[Index]); 5237 } 5238 5239 return TypesLoaded[Index].withFastQualifiers(FastQuals); 5240} 5241 5242QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) { 5243 return GetType(getGlobalTypeID(F, LocalID)); 5244} 5245 5246serialization::TypeID 5247ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const { 5248 unsigned FastQuals = LocalID & Qualifiers::FastMask; 5249 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth; 5250 5251 if (LocalIndex < NUM_PREDEF_TYPE_IDS) 5252 return LocalID; 5253 5254 ContinuousRangeMap<uint32_t, int, 2>::iterator I 5255 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS); 5256 assert(I != F.TypeRemap.end() && "Invalid index into type index remap"); 5257 5258 unsigned GlobalIndex = LocalIndex + I->second; 5259 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals; 5260} 5261 5262TemplateArgumentLocInfo 5263ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F, 5264 TemplateArgument::ArgKind Kind, 5265 const RecordData &Record, 5266 unsigned &Index) { 5267 switch (Kind) { 5268 case TemplateArgument::Expression: 5269 return ReadExpr(F); 5270 case TemplateArgument::Type: 5271 return GetTypeSourceInfo(F, Record, Index); 5272 case TemplateArgument::Template: { 5273 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, 5274 Index); 5275 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index); 5276 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 5277 SourceLocation()); 5278 } 5279 case TemplateArgument::TemplateExpansion: { 5280 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, 5281 Index); 5282 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index); 5283 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index); 5284 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 5285 EllipsisLoc); 5286 } 5287 case TemplateArgument::Null: 5288 case TemplateArgument::Integral: 5289 case TemplateArgument::Declaration: 5290 case TemplateArgument::NullPtr: 5291 case TemplateArgument::Pack: 5292 // FIXME: Is this right? 5293 return TemplateArgumentLocInfo(); 5294 } 5295 llvm_unreachable("unexpected template argument loc"); 5296} 5297 5298TemplateArgumentLoc 5299ASTReader::ReadTemplateArgumentLoc(ModuleFile &F, 5300 const RecordData &Record, unsigned &Index) { 5301 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index); 5302 5303 if (Arg.getKind() == TemplateArgument::Expression) { 5304 if (Record[Index++]) // bool InfoHasSameExpr. 5305 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); 5306 } 5307 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(), 5308 Record, Index)); 5309} 5310 5311Decl *ASTReader::GetExternalDecl(uint32_t ID) { 5312 return GetDecl(ID); 5313} 5314 5315uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record, 5316 unsigned &Idx){ 5317 if (Idx >= Record.size()) 5318 return 0; 5319 5320 unsigned LocalID = Record[Idx++]; 5321 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]); 5322} 5323 5324CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) { 5325 RecordLocation Loc = getLocalBitOffset(Offset); 5326 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor; 5327 SavedStreamPosition SavedPosition(Cursor); 5328 Cursor.JumpToBit(Loc.Offset); 5329 ReadingKindTracker ReadingKind(Read_Decl, *this); 5330 RecordData Record; 5331 unsigned Code = Cursor.ReadCode(); 5332 unsigned RecCode = Cursor.ReadRecord(Code, Record); 5333 if (RecCode != DECL_CXX_BASE_SPECIFIERS) { 5334 Error("Malformed AST file: missing C++ base specifiers"); 5335 return 0; 5336 } 5337 5338 unsigned Idx = 0; 5339 unsigned NumBases = Record[Idx++]; 5340 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases); 5341 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases]; 5342 for (unsigned I = 0; I != NumBases; ++I) 5343 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx); 5344 return Bases; 5345} 5346 5347serialization::DeclID 5348ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const { 5349 if (LocalID < NUM_PREDEF_DECL_IDS) 5350 return LocalID; 5351 5352 ContinuousRangeMap<uint32_t, int, 2>::iterator I 5353 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS); 5354 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap"); 5355 5356 return LocalID + I->second; 5357} 5358 5359bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID, 5360 ModuleFile &M) const { 5361 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID); 5362 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 5363 return &M == I->second; 5364} 5365 5366ModuleFile *ASTReader::getOwningModuleFile(Decl *D) { 5367 if (!D->isFromASTFile()) 5368 return 0; 5369 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID()); 5370 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 5371 return I->second; 5372} 5373 5374SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) { 5375 if (ID < NUM_PREDEF_DECL_IDS) 5376 return SourceLocation(); 5377 5378 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 5379 5380 if (Index > DeclsLoaded.size()) { 5381 Error("declaration ID out-of-range for AST file"); 5382 return SourceLocation(); 5383 } 5384 5385 if (Decl *D = DeclsLoaded[Index]) 5386 return D->getLocation(); 5387 5388 unsigned RawLocation = 0; 5389 RecordLocation Rec = DeclCursorForID(ID, RawLocation); 5390 return ReadSourceLocation(*Rec.F, RawLocation); 5391} 5392 5393Decl *ASTReader::GetDecl(DeclID ID) { 5394 if (ID < NUM_PREDEF_DECL_IDS) { 5395 switch ((PredefinedDeclIDs)ID) { 5396 case PREDEF_DECL_NULL_ID: 5397 return 0; 5398 5399 case PREDEF_DECL_TRANSLATION_UNIT_ID: 5400 return Context.getTranslationUnitDecl(); 5401 5402 case PREDEF_DECL_OBJC_ID_ID: 5403 return Context.getObjCIdDecl(); 5404 5405 case PREDEF_DECL_OBJC_SEL_ID: 5406 return Context.getObjCSelDecl(); 5407 5408 case PREDEF_DECL_OBJC_CLASS_ID: 5409 return Context.getObjCClassDecl(); 5410 5411 case PREDEF_DECL_OBJC_PROTOCOL_ID: 5412 return Context.getObjCProtocolDecl(); 5413 5414 case PREDEF_DECL_INT_128_ID: 5415 return Context.getInt128Decl(); 5416 5417 case PREDEF_DECL_UNSIGNED_INT_128_ID: 5418 return Context.getUInt128Decl(); 5419 5420 case PREDEF_DECL_OBJC_INSTANCETYPE_ID: 5421 return Context.getObjCInstanceTypeDecl(); 5422 5423 case PREDEF_DECL_BUILTIN_VA_LIST_ID: 5424 return Context.getBuiltinVaListDecl(); 5425 } 5426 } 5427 5428 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 5429 5430 if (Index >= DeclsLoaded.size()) { 5431 assert(0 && "declaration ID out-of-range for AST file"); 5432 Error("declaration ID out-of-range for AST file"); 5433 return 0; 5434 } 5435 5436 if (!DeclsLoaded[Index]) { 5437 ReadDeclRecord(ID); 5438 if (DeserializationListener) 5439 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); 5440 } 5441 5442 return DeclsLoaded[Index]; 5443} 5444 5445DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M, 5446 DeclID GlobalID) { 5447 if (GlobalID < NUM_PREDEF_DECL_IDS) 5448 return GlobalID; 5449 5450 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID); 5451 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 5452 ModuleFile *Owner = I->second; 5453 5454 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos 5455 = M.GlobalToLocalDeclIDs.find(Owner); 5456 if (Pos == M.GlobalToLocalDeclIDs.end()) 5457 return 0; 5458 5459 return GlobalID - Owner->BaseDeclID + Pos->second; 5460} 5461 5462serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F, 5463 const RecordData &Record, 5464 unsigned &Idx) { 5465 if (Idx >= Record.size()) { 5466 Error("Corrupted AST file"); 5467 return 0; 5468 } 5469 5470 return getGlobalDeclID(F, Record[Idx++]); 5471} 5472 5473/// \brief Resolve the offset of a statement into a statement. 5474/// 5475/// This operation will read a new statement from the external 5476/// source each time it is called, and is meant to be used via a 5477/// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 5478Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) { 5479 // Switch case IDs are per Decl. 5480 ClearSwitchCaseIDs(); 5481 5482 // Offset here is a global offset across the entire chain. 5483 RecordLocation Loc = getLocalBitOffset(Offset); 5484 Loc.F->DeclsCursor.JumpToBit(Loc.Offset); 5485 return ReadStmtFromStream(*Loc.F); 5486} 5487 5488namespace { 5489 class FindExternalLexicalDeclsVisitor { 5490 ASTReader &Reader; 5491 const DeclContext *DC; 5492 bool (*isKindWeWant)(Decl::Kind); 5493 5494 SmallVectorImpl<Decl*> &Decls; 5495 bool PredefsVisited[NUM_PREDEF_DECL_IDS]; 5496 5497 public: 5498 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC, 5499 bool (*isKindWeWant)(Decl::Kind), 5500 SmallVectorImpl<Decl*> &Decls) 5501 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls) 5502 { 5503 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I) 5504 PredefsVisited[I] = false; 5505 } 5506 5507 static bool visit(ModuleFile &M, bool Preorder, void *UserData) { 5508 if (Preorder) 5509 return false; 5510 5511 FindExternalLexicalDeclsVisitor *This 5512 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData); 5513 5514 ModuleFile::DeclContextInfosMap::iterator Info 5515 = M.DeclContextInfos.find(This->DC); 5516 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls) 5517 return false; 5518 5519 // Load all of the declaration IDs 5520 for (const KindDeclIDPair *ID = Info->second.LexicalDecls, 5521 *IDE = ID + Info->second.NumLexicalDecls; 5522 ID != IDE; ++ID) { 5523 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first)) 5524 continue; 5525 5526 // Don't add predefined declarations to the lexical context more 5527 // than once. 5528 if (ID->second < NUM_PREDEF_DECL_IDS) { 5529 if (This->PredefsVisited[ID->second]) 5530 continue; 5531 5532 This->PredefsVisited[ID->second] = true; 5533 } 5534 5535 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) { 5536 if (!This->DC->isDeclInLexicalTraversal(D)) 5537 This->Decls.push_back(D); 5538 } 5539 } 5540 5541 return false; 5542 } 5543 }; 5544} 5545 5546ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC, 5547 bool (*isKindWeWant)(Decl::Kind), 5548 SmallVectorImpl<Decl*> &Decls) { 5549 // There might be lexical decls in multiple modules, for the TU at 5550 // least. Walk all of the modules in the order they were loaded. 5551 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls); 5552 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor); 5553 ++NumLexicalDeclContextsRead; 5554 return ELR_Success; 5555} 5556 5557namespace { 5558 5559class DeclIDComp { 5560 ASTReader &Reader; 5561 ModuleFile &Mod; 5562 5563public: 5564 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {} 5565 5566 bool operator()(LocalDeclID L, LocalDeclID R) const { 5567 SourceLocation LHS = getLocation(L); 5568 SourceLocation RHS = getLocation(R); 5569 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 5570 } 5571 5572 bool operator()(SourceLocation LHS, LocalDeclID R) const { 5573 SourceLocation RHS = getLocation(R); 5574 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 5575 } 5576 5577 bool operator()(LocalDeclID L, SourceLocation RHS) const { 5578 SourceLocation LHS = getLocation(L); 5579 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 5580 } 5581 5582 SourceLocation getLocation(LocalDeclID ID) const { 5583 return Reader.getSourceManager().getFileLoc( 5584 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID))); 5585 } 5586}; 5587 5588} 5589 5590void ASTReader::FindFileRegionDecls(FileID File, 5591 unsigned Offset, unsigned Length, 5592 SmallVectorImpl<Decl *> &Decls) { 5593 SourceManager &SM = getSourceManager(); 5594 5595 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File); 5596 if (I == FileDeclIDs.end()) 5597 return; 5598 5599 FileDeclsInfo &DInfo = I->second; 5600 if (DInfo.Decls.empty()) 5601 return; 5602 5603 SourceLocation 5604 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset); 5605 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length); 5606 5607 DeclIDComp DIDComp(*this, *DInfo.Mod); 5608 ArrayRef<serialization::LocalDeclID>::iterator 5609 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(), 5610 BeginLoc, DIDComp); 5611 if (BeginIt != DInfo.Decls.begin()) 5612 --BeginIt; 5613 5614 // If we are pointing at a top-level decl inside an objc container, we need 5615 // to backtrack until we find it otherwise we will fail to report that the 5616 // region overlaps with an objc container. 5617 while (BeginIt != DInfo.Decls.begin() && 5618 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt)) 5619 ->isTopLevelDeclInObjCContainer()) 5620 --BeginIt; 5621 5622 ArrayRef<serialization::LocalDeclID>::iterator 5623 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(), 5624 EndLoc, DIDComp); 5625 if (EndIt != DInfo.Decls.end()) 5626 ++EndIt; 5627 5628 for (ArrayRef<serialization::LocalDeclID>::iterator 5629 DIt = BeginIt; DIt != EndIt; ++DIt) 5630 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt))); 5631} 5632 5633namespace { 5634 /// \brief ModuleFile visitor used to perform name lookup into a 5635 /// declaration context. 5636 class DeclContextNameLookupVisitor { 5637 ASTReader &Reader; 5638 llvm::SmallVectorImpl<const DeclContext *> &Contexts; 5639 DeclarationName Name; 5640 SmallVectorImpl<NamedDecl *> &Decls; 5641 5642 public: 5643 DeclContextNameLookupVisitor(ASTReader &Reader, 5644 SmallVectorImpl<const DeclContext *> &Contexts, 5645 DeclarationName Name, 5646 SmallVectorImpl<NamedDecl *> &Decls) 5647 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { } 5648 5649 static bool visit(ModuleFile &M, void *UserData) { 5650 DeclContextNameLookupVisitor *This 5651 = static_cast<DeclContextNameLookupVisitor *>(UserData); 5652 5653 // Check whether we have any visible declaration information for 5654 // this context in this module. 5655 ModuleFile::DeclContextInfosMap::iterator Info; 5656 bool FoundInfo = false; 5657 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) { 5658 Info = M.DeclContextInfos.find(This->Contexts[I]); 5659 if (Info != M.DeclContextInfos.end() && 5660 Info->second.NameLookupTableData) { 5661 FoundInfo = true; 5662 break; 5663 } 5664 } 5665 5666 if (!FoundInfo) 5667 return false; 5668 5669 // Look for this name within this module. 5670 ASTDeclContextNameLookupTable *LookupTable = 5671 Info->second.NameLookupTableData; 5672 ASTDeclContextNameLookupTable::iterator Pos 5673 = LookupTable->find(This->Name); 5674 if (Pos == LookupTable->end()) 5675 return false; 5676 5677 bool FoundAnything = false; 5678 ASTDeclContextNameLookupTrait::data_type Data = *Pos; 5679 for (; Data.first != Data.second; ++Data.first) { 5680 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first); 5681 if (!ND) 5682 continue; 5683 5684 if (ND->getDeclName() != This->Name) { 5685 // A name might be null because the decl's redeclarable part is 5686 // currently read before reading its name. The lookup is triggered by 5687 // building that decl (likely indirectly), and so it is later in the 5688 // sense of "already existing" and can be ignored here. 5689 continue; 5690 } 5691 5692 // Record this declaration. 5693 FoundAnything = true; 5694 This->Decls.push_back(ND); 5695 } 5696 5697 return FoundAnything; 5698 } 5699 }; 5700} 5701 5702DeclContext::lookup_result 5703ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC, 5704 DeclarationName Name) { 5705 assert(DC->hasExternalVisibleStorage() && 5706 "DeclContext has no visible decls in storage"); 5707 if (!Name) 5708 return DeclContext::lookup_result(DeclContext::lookup_iterator(0), 5709 DeclContext::lookup_iterator(0)); 5710 5711 SmallVector<NamedDecl *, 64> Decls; 5712 5713 // Compute the declaration contexts we need to look into. Multiple such 5714 // declaration contexts occur when two declaration contexts from disjoint 5715 // modules get merged, e.g., when two namespaces with the same name are 5716 // independently defined in separate modules. 5717 SmallVector<const DeclContext *, 2> Contexts; 5718 Contexts.push_back(DC); 5719 5720 if (DC->isNamespace()) { 5721 MergedDeclsMap::iterator Merged 5722 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC))); 5723 if (Merged != MergedDecls.end()) { 5724 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I) 5725 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I]))); 5726 } 5727 } 5728 5729 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls); 5730 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor); 5731 ++NumVisibleDeclContextsRead; 5732 SetExternalVisibleDeclsForName(DC, Name, Decls); 5733 return const_cast<DeclContext*>(DC)->lookup(Name); 5734} 5735 5736namespace { 5737 /// \brief ModuleFile visitor used to retrieve all visible names in a 5738 /// declaration context. 5739 class DeclContextAllNamesVisitor { 5740 ASTReader &Reader; 5741 llvm::SmallVectorImpl<const DeclContext *> &Contexts; 5742 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls; 5743 5744 public: 5745 DeclContextAllNamesVisitor(ASTReader &Reader, 5746 SmallVectorImpl<const DeclContext *> &Contexts, 5747 llvm::DenseMap<DeclarationName, 5748 SmallVector<NamedDecl *, 8> > &Decls) 5749 : Reader(Reader), Contexts(Contexts), Decls(Decls) { } 5750 5751 static bool visit(ModuleFile &M, void *UserData) { 5752 DeclContextAllNamesVisitor *This 5753 = static_cast<DeclContextAllNamesVisitor *>(UserData); 5754 5755 // Check whether we have any visible declaration information for 5756 // this context in this module. 5757 ModuleFile::DeclContextInfosMap::iterator Info; 5758 bool FoundInfo = false; 5759 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) { 5760 Info = M.DeclContextInfos.find(This->Contexts[I]); 5761 if (Info != M.DeclContextInfos.end() && 5762 Info->second.NameLookupTableData) { 5763 FoundInfo = true; 5764 break; 5765 } 5766 } 5767 5768 if (!FoundInfo) 5769 return false; 5770 5771 ASTDeclContextNameLookupTable *LookupTable = 5772 Info->second.NameLookupTableData; 5773 bool FoundAnything = false; 5774 for (ASTDeclContextNameLookupTable::data_iterator 5775 I = LookupTable->data_begin(), E = LookupTable->data_end(); 5776 I != E; ++I) { 5777 ASTDeclContextNameLookupTrait::data_type Data = *I; 5778 for (; Data.first != Data.second; ++Data.first) { 5779 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, 5780 *Data.first); 5781 if (!ND) 5782 continue; 5783 5784 // Record this declaration. 5785 FoundAnything = true; 5786 This->Decls[ND->getDeclName()].push_back(ND); 5787 } 5788 } 5789 5790 return FoundAnything; 5791 } 5792 }; 5793} 5794 5795void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) { 5796 if (!DC->hasExternalVisibleStorage()) 5797 return; 5798 llvm::DenseMap<DeclarationName, llvm::SmallVector<NamedDecl*, 8> > Decls; 5799 5800 // Compute the declaration contexts we need to look into. Multiple such 5801 // declaration contexts occur when two declaration contexts from disjoint 5802 // modules get merged, e.g., when two namespaces with the same name are 5803 // independently defined in separate modules. 5804 SmallVector<const DeclContext *, 2> Contexts; 5805 Contexts.push_back(DC); 5806 5807 if (DC->isNamespace()) { 5808 MergedDeclsMap::iterator Merged 5809 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC))); 5810 if (Merged != MergedDecls.end()) { 5811 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I) 5812 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I]))); 5813 } 5814 } 5815 5816 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls); 5817 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor); 5818 ++NumVisibleDeclContextsRead; 5819 5820 for (llvm::DenseMap<DeclarationName, 5821 llvm::SmallVector<NamedDecl*, 8> >::iterator 5822 I = Decls.begin(), E = Decls.end(); I != E; ++I) { 5823 SetExternalVisibleDeclsForName(DC, I->first, I->second); 5824 } 5825 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false); 5826} 5827 5828/// \brief Under non-PCH compilation the consumer receives the objc methods 5829/// before receiving the implementation, and codegen depends on this. 5830/// We simulate this by deserializing and passing to consumer the methods of the 5831/// implementation before passing the deserialized implementation decl. 5832static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD, 5833 ASTConsumer *Consumer) { 5834 assert(ImplD && Consumer); 5835 5836 for (ObjCImplDecl::method_iterator 5837 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I) 5838 Consumer->HandleInterestingDecl(DeclGroupRef(*I)); 5839 5840 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD)); 5841} 5842 5843void ASTReader::PassInterestingDeclsToConsumer() { 5844 assert(Consumer); 5845 while (!InterestingDecls.empty()) { 5846 Decl *D = InterestingDecls.front(); 5847 InterestingDecls.pop_front(); 5848 5849 PassInterestingDeclToConsumer(D); 5850 } 5851} 5852 5853void ASTReader::PassInterestingDeclToConsumer(Decl *D) { 5854 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) 5855 PassObjCImplDeclToConsumer(ImplD, Consumer); 5856 else 5857 Consumer->HandleInterestingDecl(DeclGroupRef(D)); 5858} 5859 5860void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) { 5861 this->Consumer = Consumer; 5862 5863 if (!Consumer) 5864 return; 5865 5866 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) { 5867 // Force deserialization of this decl, which will cause it to be queued for 5868 // passing to the consumer. 5869 GetDecl(ExternalDefinitions[I]); 5870 } 5871 ExternalDefinitions.clear(); 5872 5873 PassInterestingDeclsToConsumer(); 5874} 5875 5876void ASTReader::PrintStats() { 5877 std::fprintf(stderr, "*** AST File Statistics:\n"); 5878 5879 unsigned NumTypesLoaded 5880 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), 5881 QualType()); 5882 unsigned NumDeclsLoaded 5883 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), 5884 (Decl *)0); 5885 unsigned NumIdentifiersLoaded 5886 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), 5887 IdentifiersLoaded.end(), 5888 (IdentifierInfo *)0); 5889 unsigned NumMacrosLoaded 5890 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(), 5891 MacrosLoaded.end(), 5892 (MacroInfo *)0); 5893 unsigned NumSelectorsLoaded 5894 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), 5895 SelectorsLoaded.end(), 5896 Selector()); 5897 5898 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits); 5899 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses); 5900 if (unsigned TotalNumSLocEntries = getTotalNumSLocs()) 5901 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", 5902 NumSLocEntriesRead, TotalNumSLocEntries, 5903 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); 5904 if (!TypesLoaded.empty()) 5905 std::fprintf(stderr, " %u/%u types read (%f%%)\n", 5906 NumTypesLoaded, (unsigned)TypesLoaded.size(), 5907 ((float)NumTypesLoaded/TypesLoaded.size() * 100)); 5908 if (!DeclsLoaded.empty()) 5909 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", 5910 NumDeclsLoaded, (unsigned)DeclsLoaded.size(), 5911 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); 5912 if (!IdentifiersLoaded.empty()) 5913 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", 5914 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), 5915 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); 5916 if (!MacrosLoaded.empty()) 5917 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 5918 NumMacrosLoaded, (unsigned)MacrosLoaded.size(), 5919 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100)); 5920 if (!SelectorsLoaded.empty()) 5921 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", 5922 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(), 5923 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100)); 5924 if (TotalNumStatements) 5925 std::fprintf(stderr, " %u/%u statements read (%f%%)\n", 5926 NumStatementsRead, TotalNumStatements, 5927 ((float)NumStatementsRead/TotalNumStatements * 100)); 5928 if (TotalNumMacros) 5929 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 5930 NumMacrosRead, TotalNumMacros, 5931 ((float)NumMacrosRead/TotalNumMacros * 100)); 5932 if (TotalLexicalDeclContexts) 5933 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", 5934 NumLexicalDeclContextsRead, TotalLexicalDeclContexts, 5935 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts 5936 * 100)); 5937 if (TotalVisibleDeclContexts) 5938 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", 5939 NumVisibleDeclContextsRead, TotalVisibleDeclContexts, 5940 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts 5941 * 100)); 5942 if (TotalNumMethodPoolEntries) { 5943 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", 5944 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries, 5945 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries 5946 * 100)); 5947 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses); 5948 } 5949 std::fprintf(stderr, "\n"); 5950 dump(); 5951 std::fprintf(stderr, "\n"); 5952} 5953 5954template<typename Key, typename ModuleFile, unsigned InitialCapacity> 5955static void 5956dumpModuleIDMap(StringRef Name, 5957 const ContinuousRangeMap<Key, ModuleFile *, 5958 InitialCapacity> &Map) { 5959 if (Map.begin() == Map.end()) 5960 return; 5961 5962 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType; 5963 llvm::errs() << Name << ":\n"; 5964 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end(); 5965 I != IEnd; ++I) { 5966 llvm::errs() << " " << I->first << " -> " << I->second->FileName 5967 << "\n"; 5968 } 5969} 5970 5971void ASTReader::dump() { 5972 llvm::errs() << "*** PCH/ModuleFile Remappings:\n"; 5973 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap); 5974 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap); 5975 dumpModuleIDMap("Global type map", GlobalTypeMap); 5976 dumpModuleIDMap("Global declaration map", GlobalDeclMap); 5977 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap); 5978 dumpModuleIDMap("Global macro map", GlobalMacroMap); 5979 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap); 5980 dumpModuleIDMap("Global selector map", GlobalSelectorMap); 5981 dumpModuleIDMap("Global preprocessed entity map", 5982 GlobalPreprocessedEntityMap); 5983 5984 llvm::errs() << "\n*** PCH/Modules Loaded:"; 5985 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(), 5986 MEnd = ModuleMgr.end(); 5987 M != MEnd; ++M) 5988 (*M)->dump(); 5989} 5990 5991/// Return the amount of memory used by memory buffers, breaking down 5992/// by heap-backed versus mmap'ed memory. 5993void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const { 5994 for (ModuleConstIterator I = ModuleMgr.begin(), 5995 E = ModuleMgr.end(); I != E; ++I) { 5996 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) { 5997 size_t bytes = buf->getBufferSize(); 5998 switch (buf->getBufferKind()) { 5999 case llvm::MemoryBuffer::MemoryBuffer_Malloc: 6000 sizes.malloc_bytes += bytes; 6001 break; 6002 case llvm::MemoryBuffer::MemoryBuffer_MMap: 6003 sizes.mmap_bytes += bytes; 6004 break; 6005 } 6006 } 6007 } 6008} 6009 6010void ASTReader::InitializeSema(Sema &S) { 6011 SemaObj = &S; 6012 S.addExternalSource(this); 6013 6014 // Makes sure any declarations that were deserialized "too early" 6015 // still get added to the identifier's declaration chains. 6016 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) { 6017 SemaObj->pushExternalDeclIntoScope(PreloadedDecls[I], 6018 PreloadedDecls[I]->getDeclName()); 6019 } 6020 PreloadedDecls.clear(); 6021 6022 // Load the offsets of the declarations that Sema references. 6023 // They will be lazily deserialized when needed. 6024 if (!SemaDeclRefs.empty()) { 6025 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!"); 6026 if (!SemaObj->StdNamespace) 6027 SemaObj->StdNamespace = SemaDeclRefs[0]; 6028 if (!SemaObj->StdBadAlloc) 6029 SemaObj->StdBadAlloc = SemaDeclRefs[1]; 6030 } 6031 6032 if (!FPPragmaOptions.empty()) { 6033 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS"); 6034 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0]; 6035 } 6036 6037 if (!OpenCLExtensions.empty()) { 6038 unsigned I = 0; 6039#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++]; 6040#include "clang/Basic/OpenCLExtensions.def" 6041 6042 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS"); 6043 } 6044} 6045 6046IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) { 6047 // Note that we are loading an identifier. 6048 Deserializing AnIdentifier(this); 6049 6050 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart), 6051 /*PriorGeneration=*/0); 6052 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor); 6053 IdentifierInfo *II = Visitor.getIdentifierInfo(); 6054 markIdentifierUpToDate(II); 6055 return II; 6056} 6057 6058namespace clang { 6059 /// \brief An identifier-lookup iterator that enumerates all of the 6060 /// identifiers stored within a set of AST files. 6061 class ASTIdentifierIterator : public IdentifierIterator { 6062 /// \brief The AST reader whose identifiers are being enumerated. 6063 const ASTReader &Reader; 6064 6065 /// \brief The current index into the chain of AST files stored in 6066 /// the AST reader. 6067 unsigned Index; 6068 6069 /// \brief The current position within the identifier lookup table 6070 /// of the current AST file. 6071 ASTIdentifierLookupTable::key_iterator Current; 6072 6073 /// \brief The end position within the identifier lookup table of 6074 /// the current AST file. 6075 ASTIdentifierLookupTable::key_iterator End; 6076 6077 public: 6078 explicit ASTIdentifierIterator(const ASTReader &Reader); 6079 6080 virtual StringRef Next(); 6081 }; 6082} 6083 6084ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader) 6085 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) { 6086 ASTIdentifierLookupTable *IdTable 6087 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable; 6088 Current = IdTable->key_begin(); 6089 End = IdTable->key_end(); 6090} 6091 6092StringRef ASTIdentifierIterator::Next() { 6093 while (Current == End) { 6094 // If we have exhausted all of our AST files, we're done. 6095 if (Index == 0) 6096 return StringRef(); 6097 6098 --Index; 6099 ASTIdentifierLookupTable *IdTable 6100 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index]. 6101 IdentifierLookupTable; 6102 Current = IdTable->key_begin(); 6103 End = IdTable->key_end(); 6104 } 6105 6106 // We have any identifiers remaining in the current AST file; return 6107 // the next one. 6108 std::pair<const char*, unsigned> Key = *Current; 6109 ++Current; 6110 return StringRef(Key.first, Key.second); 6111} 6112 6113IdentifierIterator *ASTReader::getIdentifiers() const { 6114 return new ASTIdentifierIterator(*this); 6115} 6116 6117namespace clang { namespace serialization { 6118 class ReadMethodPoolVisitor { 6119 ASTReader &Reader; 6120 Selector Sel; 6121 unsigned PriorGeneration; 6122 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods; 6123 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods; 6124 6125 public: 6126 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel, 6127 unsigned PriorGeneration) 6128 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) { } 6129 6130 static bool visit(ModuleFile &M, void *UserData) { 6131 ReadMethodPoolVisitor *This 6132 = static_cast<ReadMethodPoolVisitor *>(UserData); 6133 6134 if (!M.SelectorLookupTable) 6135 return false; 6136 6137 // If we've already searched this module file, skip it now. 6138 if (M.Generation <= This->PriorGeneration) 6139 return true; 6140 6141 ASTSelectorLookupTable *PoolTable 6142 = (ASTSelectorLookupTable*)M.SelectorLookupTable; 6143 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel); 6144 if (Pos == PoolTable->end()) 6145 return false; 6146 6147 ++This->Reader.NumSelectorsRead; 6148 // FIXME: Not quite happy with the statistics here. We probably should 6149 // disable this tracking when called via LoadSelector. 6150 // Also, should entries without methods count as misses? 6151 ++This->Reader.NumMethodPoolEntriesRead; 6152 ASTSelectorLookupTrait::data_type Data = *Pos; 6153 if (This->Reader.DeserializationListener) 6154 This->Reader.DeserializationListener->SelectorRead(Data.ID, 6155 This->Sel); 6156 6157 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end()); 6158 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end()); 6159 return true; 6160 } 6161 6162 /// \brief Retrieve the instance methods found by this visitor. 6163 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const { 6164 return InstanceMethods; 6165 } 6166 6167 /// \brief Retrieve the instance methods found by this visitor. 6168 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const { 6169 return FactoryMethods; 6170 } 6171 }; 6172} } // end namespace clang::serialization 6173 6174/// \brief Add the given set of methods to the method list. 6175static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods, 6176 ObjCMethodList &List) { 6177 for (unsigned I = 0, N = Methods.size(); I != N; ++I) { 6178 S.addMethodToGlobalList(&List, Methods[I]); 6179 } 6180} 6181 6182void ASTReader::ReadMethodPool(Selector Sel) { 6183 // Get the selector generation and update it to the current generation. 6184 unsigned &Generation = SelectorGeneration[Sel]; 6185 unsigned PriorGeneration = Generation; 6186 Generation = CurrentGeneration; 6187 6188 // Search for methods defined with this selector. 6189 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration); 6190 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor); 6191 6192 if (Visitor.getInstanceMethods().empty() && 6193 Visitor.getFactoryMethods().empty()) { 6194 ++NumMethodPoolMisses; 6195 return; 6196 } 6197 6198 if (!getSema()) 6199 return; 6200 6201 Sema &S = *getSema(); 6202 Sema::GlobalMethodPool::iterator Pos 6203 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first; 6204 6205 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first); 6206 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second); 6207} 6208 6209void ASTReader::ReadKnownNamespaces( 6210 SmallVectorImpl<NamespaceDecl *> &Namespaces) { 6211 Namespaces.clear(); 6212 6213 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) { 6214 if (NamespaceDecl *Namespace 6215 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I]))) 6216 Namespaces.push_back(Namespace); 6217 } 6218} 6219 6220void ASTReader::ReadTentativeDefinitions( 6221 SmallVectorImpl<VarDecl *> &TentativeDefs) { 6222 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { 6223 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I])); 6224 if (Var) 6225 TentativeDefs.push_back(Var); 6226 } 6227 TentativeDefinitions.clear(); 6228} 6229 6230void ASTReader::ReadUnusedFileScopedDecls( 6231 SmallVectorImpl<const DeclaratorDecl *> &Decls) { 6232 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) { 6233 DeclaratorDecl *D 6234 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I])); 6235 if (D) 6236 Decls.push_back(D); 6237 } 6238 UnusedFileScopedDecls.clear(); 6239} 6240 6241void ASTReader::ReadDelegatingConstructors( 6242 SmallVectorImpl<CXXConstructorDecl *> &Decls) { 6243 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) { 6244 CXXConstructorDecl *D 6245 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I])); 6246 if (D) 6247 Decls.push_back(D); 6248 } 6249 DelegatingCtorDecls.clear(); 6250} 6251 6252void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) { 6253 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) { 6254 TypedefNameDecl *D 6255 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])); 6256 if (D) 6257 Decls.push_back(D); 6258 } 6259 ExtVectorDecls.clear(); 6260} 6261 6262void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) { 6263 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) { 6264 CXXRecordDecl *D 6265 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I])); 6266 if (D) 6267 Decls.push_back(D); 6268 } 6269 DynamicClasses.clear(); 6270} 6271 6272void 6273ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) { 6274 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) { 6275 NamedDecl *D 6276 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I])); 6277 if (D) 6278 Decls.push_back(D); 6279 } 6280 LocallyScopedExternalDecls.clear(); 6281} 6282 6283void ASTReader::ReadReferencedSelectors( 6284 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) { 6285 if (ReferencedSelectorsData.empty()) 6286 return; 6287 6288 // If there are @selector references added them to its pool. This is for 6289 // implementation of -Wselector. 6290 unsigned int DataSize = ReferencedSelectorsData.size()-1; 6291 unsigned I = 0; 6292 while (I < DataSize) { 6293 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]); 6294 SourceLocation SelLoc 6295 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]); 6296 Sels.push_back(std::make_pair(Sel, SelLoc)); 6297 } 6298 ReferencedSelectorsData.clear(); 6299} 6300 6301void ASTReader::ReadWeakUndeclaredIdentifiers( 6302 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) { 6303 if (WeakUndeclaredIdentifiers.empty()) 6304 return; 6305 6306 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) { 6307 IdentifierInfo *WeakId 6308 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 6309 IdentifierInfo *AliasId 6310 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 6311 SourceLocation Loc 6312 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]); 6313 bool Used = WeakUndeclaredIdentifiers[I++]; 6314 WeakInfo WI(AliasId, Loc); 6315 WI.setUsed(Used); 6316 WeakIDs.push_back(std::make_pair(WeakId, WI)); 6317 } 6318 WeakUndeclaredIdentifiers.clear(); 6319} 6320 6321void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) { 6322 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) { 6323 ExternalVTableUse VT; 6324 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); 6325 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]); 6326 VT.DefinitionRequired = VTableUses[Idx++]; 6327 VTables.push_back(VT); 6328 } 6329 6330 VTableUses.clear(); 6331} 6332 6333void ASTReader::ReadPendingInstantiations( 6334 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) { 6335 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) { 6336 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++])); 6337 SourceLocation Loc 6338 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]); 6339 6340 Pending.push_back(std::make_pair(D, Loc)); 6341 } 6342 PendingInstantiations.clear(); 6343} 6344 6345void ASTReader::LoadSelector(Selector Sel) { 6346 // It would be complicated to avoid reading the methods anyway. So don't. 6347 ReadMethodPool(Sel); 6348} 6349 6350void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) { 6351 assert(ID && "Non-zero identifier ID required"); 6352 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); 6353 IdentifiersLoaded[ID - 1] = II; 6354 if (DeserializationListener) 6355 DeserializationListener->IdentifierRead(ID, II); 6356} 6357 6358/// \brief Set the globally-visible declarations associated with the given 6359/// identifier. 6360/// 6361/// If the AST reader is currently in a state where the given declaration IDs 6362/// cannot safely be resolved, they are queued until it is safe to resolve 6363/// them. 6364/// 6365/// \param II an IdentifierInfo that refers to one or more globally-visible 6366/// declarations. 6367/// 6368/// \param DeclIDs the set of declaration IDs with the name @p II that are 6369/// visible at global scope. 6370/// 6371/// \param Nonrecursive should be true to indicate that the caller knows that 6372/// this call is non-recursive, and therefore the globally-visible declarations 6373/// will not be placed onto the pending queue. 6374void 6375ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, 6376 const SmallVectorImpl<uint32_t> &DeclIDs, 6377 bool Nonrecursive) { 6378 if (NumCurrentElementsDeserializing && !Nonrecursive) { 6379 PendingIdentifierInfos.push_back(PendingIdentifierInfo()); 6380 PendingIdentifierInfo &PII = PendingIdentifierInfos.back(); 6381 PII.II = II; 6382 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end()); 6383 return; 6384 } 6385 6386 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { 6387 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); 6388 if (SemaObj) { 6389 // Introduce this declaration into the translation-unit scope 6390 // and add it to the declaration chain for this identifier, so 6391 // that (unqualified) name lookup will find it. 6392 SemaObj->pushExternalDeclIntoScope(D, II); 6393 } else { 6394 // Queue this declaration so that it will be added to the 6395 // translation unit scope and identifier's declaration chain 6396 // once a Sema object is known. 6397 PreloadedDecls.push_back(D); 6398 } 6399 } 6400} 6401 6402IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) { 6403 if (ID == 0) 6404 return 0; 6405 6406 if (IdentifiersLoaded.empty()) { 6407 Error("no identifier table in AST file"); 6408 return 0; 6409 } 6410 6411 ID -= 1; 6412 if (!IdentifiersLoaded[ID]) { 6413 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1); 6414 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map"); 6415 ModuleFile *M = I->second; 6416 unsigned Index = ID - M->BaseIdentifierID; 6417 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index]; 6418 6419 // All of the strings in the AST file are preceded by a 16-bit length. 6420 // Extract that 16-bit length to avoid having to execute strlen(). 6421 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as 6422 // unsigned integers. This is important to avoid integer overflow when 6423 // we cast them to 'unsigned'. 6424 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2; 6425 unsigned StrLen = (((unsigned) StrLenPtr[0]) 6426 | (((unsigned) StrLenPtr[1]) << 8)) - 1; 6427 IdentifiersLoaded[ID] 6428 = &PP.getIdentifierTable().get(StringRef(Str, StrLen)); 6429 if (DeserializationListener) 6430 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]); 6431 } 6432 6433 return IdentifiersLoaded[ID]; 6434} 6435 6436IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) { 6437 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID)); 6438} 6439 6440IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) { 6441 if (LocalID < NUM_PREDEF_IDENT_IDS) 6442 return LocalID; 6443 6444 ContinuousRangeMap<uint32_t, int, 2>::iterator I 6445 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS); 6446 assert(I != M.IdentifierRemap.end() 6447 && "Invalid index into identifier index remap"); 6448 6449 return LocalID + I->second; 6450} 6451 6452MacroInfo *ASTReader::getMacro(MacroID ID, MacroInfo *Hint) { 6453 if (ID == 0) 6454 return 0; 6455 6456 if (MacrosLoaded.empty()) { 6457 Error("no macro table in AST file"); 6458 return 0; 6459 } 6460 6461 ID -= NUM_PREDEF_MACRO_IDS; 6462 if (!MacrosLoaded[ID]) { 6463 GlobalMacroMapType::iterator I 6464 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS); 6465 assert(I != GlobalMacroMap.end() && "Corrupted global macro map"); 6466 ModuleFile *M = I->second; 6467 unsigned Index = ID - M->BaseMacroID; 6468 ReadMacroRecord(*M, M->MacroOffsets[Index], Hint); 6469 } 6470 6471 return MacrosLoaded[ID]; 6472} 6473 6474MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) { 6475 if (LocalID < NUM_PREDEF_MACRO_IDS) 6476 return LocalID; 6477 6478 ContinuousRangeMap<uint32_t, int, 2>::iterator I 6479 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS); 6480 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap"); 6481 6482 return LocalID + I->second; 6483} 6484 6485serialization::SubmoduleID 6486ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) { 6487 if (LocalID < NUM_PREDEF_SUBMODULE_IDS) 6488 return LocalID; 6489 6490 ContinuousRangeMap<uint32_t, int, 2>::iterator I 6491 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS); 6492 assert(I != M.SubmoduleRemap.end() 6493 && "Invalid index into submodule index remap"); 6494 6495 return LocalID + I->second; 6496} 6497 6498Module *ASTReader::getSubmodule(SubmoduleID GlobalID) { 6499 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) { 6500 assert(GlobalID == 0 && "Unhandled global submodule ID"); 6501 return 0; 6502 } 6503 6504 if (GlobalID > SubmodulesLoaded.size()) { 6505 Error("submodule ID out of range in AST file"); 6506 return 0; 6507 } 6508 6509 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS]; 6510} 6511 6512Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) { 6513 return DecodeSelector(getGlobalSelectorID(M, LocalID)); 6514} 6515 6516Selector ASTReader::DecodeSelector(serialization::SelectorID ID) { 6517 if (ID == 0) 6518 return Selector(); 6519 6520 if (ID > SelectorsLoaded.size()) { 6521 Error("selector ID out of range in AST file"); 6522 return Selector(); 6523 } 6524 6525 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) { 6526 // Load this selector from the selector table. 6527 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID); 6528 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map"); 6529 ModuleFile &M = *I->second; 6530 ASTSelectorLookupTrait Trait(*this, M); 6531 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS; 6532 SelectorsLoaded[ID - 1] = 6533 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0); 6534 if (DeserializationListener) 6535 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); 6536 } 6537 6538 return SelectorsLoaded[ID - 1]; 6539} 6540 6541Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) { 6542 return DecodeSelector(ID); 6543} 6544 6545uint32_t ASTReader::GetNumExternalSelectors() { 6546 // ID 0 (the null selector) is considered an external selector. 6547 return getTotalNumSelectors() + 1; 6548} 6549 6550serialization::SelectorID 6551ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const { 6552 if (LocalID < NUM_PREDEF_SELECTOR_IDS) 6553 return LocalID; 6554 6555 ContinuousRangeMap<uint32_t, int, 2>::iterator I 6556 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS); 6557 assert(I != M.SelectorRemap.end() 6558 && "Invalid index into selector index remap"); 6559 6560 return LocalID + I->second; 6561} 6562 6563DeclarationName 6564ASTReader::ReadDeclarationName(ModuleFile &F, 6565 const RecordData &Record, unsigned &Idx) { 6566 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++]; 6567 switch (Kind) { 6568 case DeclarationName::Identifier: 6569 return DeclarationName(GetIdentifierInfo(F, Record, Idx)); 6570 6571 case DeclarationName::ObjCZeroArgSelector: 6572 case DeclarationName::ObjCOneArgSelector: 6573 case DeclarationName::ObjCMultiArgSelector: 6574 return DeclarationName(ReadSelector(F, Record, Idx)); 6575 6576 case DeclarationName::CXXConstructorName: 6577 return Context.DeclarationNames.getCXXConstructorName( 6578 Context.getCanonicalType(readType(F, Record, Idx))); 6579 6580 case DeclarationName::CXXDestructorName: 6581 return Context.DeclarationNames.getCXXDestructorName( 6582 Context.getCanonicalType(readType(F, Record, Idx))); 6583 6584 case DeclarationName::CXXConversionFunctionName: 6585 return Context.DeclarationNames.getCXXConversionFunctionName( 6586 Context.getCanonicalType(readType(F, Record, Idx))); 6587 6588 case DeclarationName::CXXOperatorName: 6589 return Context.DeclarationNames.getCXXOperatorName( 6590 (OverloadedOperatorKind)Record[Idx++]); 6591 6592 case DeclarationName::CXXLiteralOperatorName: 6593 return Context.DeclarationNames.getCXXLiteralOperatorName( 6594 GetIdentifierInfo(F, Record, Idx)); 6595 6596 case DeclarationName::CXXUsingDirective: 6597 return DeclarationName::getUsingDirectiveName(); 6598 } 6599 6600 llvm_unreachable("Invalid NameKind!"); 6601} 6602 6603void ASTReader::ReadDeclarationNameLoc(ModuleFile &F, 6604 DeclarationNameLoc &DNLoc, 6605 DeclarationName Name, 6606 const RecordData &Record, unsigned &Idx) { 6607 switch (Name.getNameKind()) { 6608 case DeclarationName::CXXConstructorName: 6609 case DeclarationName::CXXDestructorName: 6610 case DeclarationName::CXXConversionFunctionName: 6611 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx); 6612 break; 6613 6614 case DeclarationName::CXXOperatorName: 6615 DNLoc.CXXOperatorName.BeginOpNameLoc 6616 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 6617 DNLoc.CXXOperatorName.EndOpNameLoc 6618 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 6619 break; 6620 6621 case DeclarationName::CXXLiteralOperatorName: 6622 DNLoc.CXXLiteralOperatorName.OpNameLoc 6623 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 6624 break; 6625 6626 case DeclarationName::Identifier: 6627 case DeclarationName::ObjCZeroArgSelector: 6628 case DeclarationName::ObjCOneArgSelector: 6629 case DeclarationName::ObjCMultiArgSelector: 6630 case DeclarationName::CXXUsingDirective: 6631 break; 6632 } 6633} 6634 6635void ASTReader::ReadDeclarationNameInfo(ModuleFile &F, 6636 DeclarationNameInfo &NameInfo, 6637 const RecordData &Record, unsigned &Idx) { 6638 NameInfo.setName(ReadDeclarationName(F, Record, Idx)); 6639 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx)); 6640 DeclarationNameLoc DNLoc; 6641 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx); 6642 NameInfo.setInfo(DNLoc); 6643} 6644 6645void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info, 6646 const RecordData &Record, unsigned &Idx) { 6647 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx); 6648 unsigned NumTPLists = Record[Idx++]; 6649 Info.NumTemplParamLists = NumTPLists; 6650 if (NumTPLists) { 6651 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists]; 6652 for (unsigned i=0; i != NumTPLists; ++i) 6653 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx); 6654 } 6655} 6656 6657TemplateName 6658ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record, 6659 unsigned &Idx) { 6660 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++]; 6661 switch (Kind) { 6662 case TemplateName::Template: 6663 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx)); 6664 6665 case TemplateName::OverloadedTemplate: { 6666 unsigned size = Record[Idx++]; 6667 UnresolvedSet<8> Decls; 6668 while (size--) 6669 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx)); 6670 6671 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end()); 6672 } 6673 6674 case TemplateName::QualifiedTemplate: { 6675 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx); 6676 bool hasTemplKeyword = Record[Idx++]; 6677 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx); 6678 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template); 6679 } 6680 6681 case TemplateName::DependentTemplate: { 6682 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx); 6683 if (Record[Idx++]) // isIdentifier 6684 return Context.getDependentTemplateName(NNS, 6685 GetIdentifierInfo(F, Record, 6686 Idx)); 6687 return Context.getDependentTemplateName(NNS, 6688 (OverloadedOperatorKind)Record[Idx++]); 6689 } 6690 6691 case TemplateName::SubstTemplateTemplateParm: { 6692 TemplateTemplateParmDecl *param 6693 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx); 6694 if (!param) return TemplateName(); 6695 TemplateName replacement = ReadTemplateName(F, Record, Idx); 6696 return Context.getSubstTemplateTemplateParm(param, replacement); 6697 } 6698 6699 case TemplateName::SubstTemplateTemplateParmPack: { 6700 TemplateTemplateParmDecl *Param 6701 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx); 6702 if (!Param) 6703 return TemplateName(); 6704 6705 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx); 6706 if (ArgPack.getKind() != TemplateArgument::Pack) 6707 return TemplateName(); 6708 6709 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack); 6710 } 6711 } 6712 6713 llvm_unreachable("Unhandled template name kind!"); 6714} 6715 6716TemplateArgument 6717ASTReader::ReadTemplateArgument(ModuleFile &F, 6718 const RecordData &Record, unsigned &Idx) { 6719 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++]; 6720 switch (Kind) { 6721 case TemplateArgument::Null: 6722 return TemplateArgument(); 6723 case TemplateArgument::Type: 6724 return TemplateArgument(readType(F, Record, Idx)); 6725 case TemplateArgument::Declaration: { 6726 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx); 6727 bool ForReferenceParam = Record[Idx++]; 6728 return TemplateArgument(D, ForReferenceParam); 6729 } 6730 case TemplateArgument::NullPtr: 6731 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true); 6732 case TemplateArgument::Integral: { 6733 llvm::APSInt Value = ReadAPSInt(Record, Idx); 6734 QualType T = readType(F, Record, Idx); 6735 return TemplateArgument(Context, Value, T); 6736 } 6737 case TemplateArgument::Template: 6738 return TemplateArgument(ReadTemplateName(F, Record, Idx)); 6739 case TemplateArgument::TemplateExpansion: { 6740 TemplateName Name = ReadTemplateName(F, Record, Idx); 6741 llvm::Optional<unsigned> NumTemplateExpansions; 6742 if (unsigned NumExpansions = Record[Idx++]) 6743 NumTemplateExpansions = NumExpansions - 1; 6744 return TemplateArgument(Name, NumTemplateExpansions); 6745 } 6746 case TemplateArgument::Expression: 6747 return TemplateArgument(ReadExpr(F)); 6748 case TemplateArgument::Pack: { 6749 unsigned NumArgs = Record[Idx++]; 6750 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs]; 6751 for (unsigned I = 0; I != NumArgs; ++I) 6752 Args[I] = ReadTemplateArgument(F, Record, Idx); 6753 return TemplateArgument(Args, NumArgs); 6754 } 6755 } 6756 6757 llvm_unreachable("Unhandled template argument kind!"); 6758} 6759 6760TemplateParameterList * 6761ASTReader::ReadTemplateParameterList(ModuleFile &F, 6762 const RecordData &Record, unsigned &Idx) { 6763 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx); 6764 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx); 6765 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx); 6766 6767 unsigned NumParams = Record[Idx++]; 6768 SmallVector<NamedDecl *, 16> Params; 6769 Params.reserve(NumParams); 6770 while (NumParams--) 6771 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx)); 6772 6773 TemplateParameterList* TemplateParams = 6774 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, 6775 Params.data(), Params.size(), RAngleLoc); 6776 return TemplateParams; 6777} 6778 6779void 6780ASTReader:: 6781ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs, 6782 ModuleFile &F, const RecordData &Record, 6783 unsigned &Idx) { 6784 unsigned NumTemplateArgs = Record[Idx++]; 6785 TemplArgs.reserve(NumTemplateArgs); 6786 while (NumTemplateArgs--) 6787 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx)); 6788} 6789 6790/// \brief Read a UnresolvedSet structure. 6791void ASTReader::ReadUnresolvedSet(ModuleFile &F, UnresolvedSetImpl &Set, 6792 const RecordData &Record, unsigned &Idx) { 6793 unsigned NumDecls = Record[Idx++]; 6794 while (NumDecls--) { 6795 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx); 6796 AccessSpecifier AS = (AccessSpecifier)Record[Idx++]; 6797 Set.addDecl(D, AS); 6798 } 6799} 6800 6801CXXBaseSpecifier 6802ASTReader::ReadCXXBaseSpecifier(ModuleFile &F, 6803 const RecordData &Record, unsigned &Idx) { 6804 bool isVirtual = static_cast<bool>(Record[Idx++]); 6805 bool isBaseOfClass = static_cast<bool>(Record[Idx++]); 6806 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]); 6807 bool inheritConstructors = static_cast<bool>(Record[Idx++]); 6808 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx); 6809 SourceRange Range = ReadSourceRange(F, Record, Idx); 6810 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx); 6811 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo, 6812 EllipsisLoc); 6813 Result.setInheritConstructors(inheritConstructors); 6814 return Result; 6815} 6816 6817std::pair<CXXCtorInitializer **, unsigned> 6818ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record, 6819 unsigned &Idx) { 6820 CXXCtorInitializer **CtorInitializers = 0; 6821 unsigned NumInitializers = Record[Idx++]; 6822 if (NumInitializers) { 6823 CtorInitializers 6824 = new (Context) CXXCtorInitializer*[NumInitializers]; 6825 for (unsigned i=0; i != NumInitializers; ++i) { 6826 TypeSourceInfo *TInfo = 0; 6827 bool IsBaseVirtual = false; 6828 FieldDecl *Member = 0; 6829 IndirectFieldDecl *IndirectMember = 0; 6830 6831 CtorInitializerType Type = (CtorInitializerType)Record[Idx++]; 6832 switch (Type) { 6833 case CTOR_INITIALIZER_BASE: 6834 TInfo = GetTypeSourceInfo(F, Record, Idx); 6835 IsBaseVirtual = Record[Idx++]; 6836 break; 6837 6838 case CTOR_INITIALIZER_DELEGATING: 6839 TInfo = GetTypeSourceInfo(F, Record, Idx); 6840 break; 6841 6842 case CTOR_INITIALIZER_MEMBER: 6843 Member = ReadDeclAs<FieldDecl>(F, Record, Idx); 6844 break; 6845 6846 case CTOR_INITIALIZER_INDIRECT_MEMBER: 6847 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx); 6848 break; 6849 } 6850 6851 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx); 6852 Expr *Init = ReadExpr(F); 6853 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx); 6854 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx); 6855 bool IsWritten = Record[Idx++]; 6856 unsigned SourceOrderOrNumArrayIndices; 6857 SmallVector<VarDecl *, 8> Indices; 6858 if (IsWritten) { 6859 SourceOrderOrNumArrayIndices = Record[Idx++]; 6860 } else { 6861 SourceOrderOrNumArrayIndices = Record[Idx++]; 6862 Indices.reserve(SourceOrderOrNumArrayIndices); 6863 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i) 6864 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx)); 6865 } 6866 6867 CXXCtorInitializer *BOMInit; 6868 if (Type == CTOR_INITIALIZER_BASE) { 6869 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual, 6870 LParenLoc, Init, RParenLoc, 6871 MemberOrEllipsisLoc); 6872 } else if (Type == CTOR_INITIALIZER_DELEGATING) { 6873 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc, 6874 Init, RParenLoc); 6875 } else if (IsWritten) { 6876 if (Member) 6877 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, 6878 LParenLoc, Init, RParenLoc); 6879 else 6880 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember, 6881 MemberOrEllipsisLoc, LParenLoc, 6882 Init, RParenLoc); 6883 } else { 6884 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc, 6885 LParenLoc, Init, RParenLoc, 6886 Indices.data(), Indices.size()); 6887 } 6888 6889 if (IsWritten) 6890 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices); 6891 CtorInitializers[i] = BOMInit; 6892 } 6893 } 6894 6895 return std::make_pair(CtorInitializers, NumInitializers); 6896} 6897 6898NestedNameSpecifier * 6899ASTReader::ReadNestedNameSpecifier(ModuleFile &F, 6900 const RecordData &Record, unsigned &Idx) { 6901 unsigned N = Record[Idx++]; 6902 NestedNameSpecifier *NNS = 0, *Prev = 0; 6903 for (unsigned I = 0; I != N; ++I) { 6904 NestedNameSpecifier::SpecifierKind Kind 6905 = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; 6906 switch (Kind) { 6907 case NestedNameSpecifier::Identifier: { 6908 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx); 6909 NNS = NestedNameSpecifier::Create(Context, Prev, II); 6910 break; 6911 } 6912 6913 case NestedNameSpecifier::Namespace: { 6914 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx); 6915 NNS = NestedNameSpecifier::Create(Context, Prev, NS); 6916 break; 6917 } 6918 6919 case NestedNameSpecifier::NamespaceAlias: { 6920 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx); 6921 NNS = NestedNameSpecifier::Create(Context, Prev, Alias); 6922 break; 6923 } 6924 6925 case NestedNameSpecifier::TypeSpec: 6926 case NestedNameSpecifier::TypeSpecWithTemplate: { 6927 const Type *T = readType(F, Record, Idx).getTypePtrOrNull(); 6928 if (!T) 6929 return 0; 6930 6931 bool Template = Record[Idx++]; 6932 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T); 6933 break; 6934 } 6935 6936 case NestedNameSpecifier::Global: { 6937 NNS = NestedNameSpecifier::GlobalSpecifier(Context); 6938 // No associated value, and there can't be a prefix. 6939 break; 6940 } 6941 } 6942 Prev = NNS; 6943 } 6944 return NNS; 6945} 6946 6947NestedNameSpecifierLoc 6948ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record, 6949 unsigned &Idx) { 6950 unsigned N = Record[Idx++]; 6951 NestedNameSpecifierLocBuilder Builder; 6952 for (unsigned I = 0; I != N; ++I) { 6953 NestedNameSpecifier::SpecifierKind Kind 6954 = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; 6955 switch (Kind) { 6956 case NestedNameSpecifier::Identifier: { 6957 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx); 6958 SourceRange Range = ReadSourceRange(F, Record, Idx); 6959 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd()); 6960 break; 6961 } 6962 6963 case NestedNameSpecifier::Namespace: { 6964 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx); 6965 SourceRange Range = ReadSourceRange(F, Record, Idx); 6966 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd()); 6967 break; 6968 } 6969 6970 case NestedNameSpecifier::NamespaceAlias: { 6971 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx); 6972 SourceRange Range = ReadSourceRange(F, Record, Idx); 6973 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd()); 6974 break; 6975 } 6976 6977 case NestedNameSpecifier::TypeSpec: 6978 case NestedNameSpecifier::TypeSpecWithTemplate: { 6979 bool Template = Record[Idx++]; 6980 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx); 6981 if (!T) 6982 return NestedNameSpecifierLoc(); 6983 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx); 6984 6985 // FIXME: 'template' keyword location not saved anywhere, so we fake it. 6986 Builder.Extend(Context, 6987 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(), 6988 T->getTypeLoc(), ColonColonLoc); 6989 break; 6990 } 6991 6992 case NestedNameSpecifier::Global: { 6993 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx); 6994 Builder.MakeGlobal(Context, ColonColonLoc); 6995 break; 6996 } 6997 } 6998 } 6999 7000 return Builder.getWithLocInContext(Context); 7001} 7002 7003SourceRange 7004ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record, 7005 unsigned &Idx) { 7006 SourceLocation beg = ReadSourceLocation(F, Record, Idx); 7007 SourceLocation end = ReadSourceLocation(F, Record, Idx); 7008 return SourceRange(beg, end); 7009} 7010 7011/// \brief Read an integral value 7012llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) { 7013 unsigned BitWidth = Record[Idx++]; 7014 unsigned NumWords = llvm::APInt::getNumWords(BitWidth); 7015 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]); 7016 Idx += NumWords; 7017 return Result; 7018} 7019 7020/// \brief Read a signed integral value 7021llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) { 7022 bool isUnsigned = Record[Idx++]; 7023 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned); 7024} 7025 7026/// \brief Read a floating-point value 7027llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) { 7028 return llvm::APFloat(ReadAPInt(Record, Idx)); 7029} 7030 7031// \brief Read a string 7032std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) { 7033 unsigned Len = Record[Idx++]; 7034 std::string Result(Record.data() + Idx, Record.data() + Idx + Len); 7035 Idx += Len; 7036 return Result; 7037} 7038 7039VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record, 7040 unsigned &Idx) { 7041 unsigned Major = Record[Idx++]; 7042 unsigned Minor = Record[Idx++]; 7043 unsigned Subminor = Record[Idx++]; 7044 if (Minor == 0) 7045 return VersionTuple(Major); 7046 if (Subminor == 0) 7047 return VersionTuple(Major, Minor - 1); 7048 return VersionTuple(Major, Minor - 1, Subminor - 1); 7049} 7050 7051CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F, 7052 const RecordData &Record, 7053 unsigned &Idx) { 7054 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx); 7055 return CXXTemporary::Create(Context, Decl); 7056} 7057 7058DiagnosticBuilder ASTReader::Diag(unsigned DiagID) { 7059 return Diag(SourceLocation(), DiagID); 7060} 7061 7062DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) { 7063 return Diags.Report(Loc, DiagID); 7064} 7065 7066/// \brief Retrieve the identifier table associated with the 7067/// preprocessor. 7068IdentifierTable &ASTReader::getIdentifierTable() { 7069 return PP.getIdentifierTable(); 7070} 7071 7072/// \brief Record that the given ID maps to the given switch-case 7073/// statement. 7074void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { 7075 assert((*CurrSwitchCaseStmts)[ID] == 0 && 7076 "Already have a SwitchCase with this ID"); 7077 (*CurrSwitchCaseStmts)[ID] = SC; 7078} 7079 7080/// \brief Retrieve the switch-case statement with the given ID. 7081SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) { 7082 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID"); 7083 return (*CurrSwitchCaseStmts)[ID]; 7084} 7085 7086void ASTReader::ClearSwitchCaseIDs() { 7087 CurrSwitchCaseStmts->clear(); 7088} 7089 7090void ASTReader::ReadComments() { 7091 std::vector<RawComment *> Comments; 7092 for (SmallVectorImpl<std::pair<llvm::BitstreamCursor, 7093 serialization::ModuleFile *> >::iterator 7094 I = CommentsCursors.begin(), 7095 E = CommentsCursors.end(); 7096 I != E; ++I) { 7097 llvm::BitstreamCursor &Cursor = I->first; 7098 serialization::ModuleFile &F = *I->second; 7099 SavedStreamPosition SavedPosition(Cursor); 7100 7101 RecordData Record; 7102 while (true) { 7103 unsigned Code = Cursor.ReadCode(); 7104 if (Code == llvm::bitc::END_BLOCK) 7105 break; 7106 7107 if (Code == llvm::bitc::ENTER_SUBBLOCK) { 7108 // No known subblocks, always skip them. 7109 Cursor.ReadSubBlockID(); 7110 if (Cursor.SkipBlock()) { 7111 Error("malformed block record in AST file"); 7112 return; 7113 } 7114 continue; 7115 } 7116 7117 if (Code == llvm::bitc::DEFINE_ABBREV) { 7118 Cursor.ReadAbbrevRecord(); 7119 continue; 7120 } 7121 7122 // Read a record. 7123 Record.clear(); 7124 switch ((CommentRecordTypes) Cursor.ReadRecord(Code, Record)) { 7125 case COMMENTS_RAW_COMMENT: { 7126 unsigned Idx = 0; 7127 SourceRange SR = ReadSourceRange(F, Record, Idx); 7128 RawComment::CommentKind Kind = 7129 (RawComment::CommentKind) Record[Idx++]; 7130 bool IsTrailingComment = Record[Idx++]; 7131 bool IsAlmostTrailingComment = Record[Idx++]; 7132 Comments.push_back(new (Context) RawComment(SR, Kind, 7133 IsTrailingComment, 7134 IsAlmostTrailingComment)); 7135 break; 7136 } 7137 } 7138 } 7139 } 7140 Context.Comments.addCommentsToFront(Comments); 7141} 7142 7143void ASTReader::finishPendingActions() { 7144 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() || 7145 !PendingMacroIDs.empty()) { 7146 // If any identifiers with corresponding top-level declarations have 7147 // been loaded, load those declarations now. 7148 while (!PendingIdentifierInfos.empty()) { 7149 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II, 7150 PendingIdentifierInfos.front().DeclIDs, true); 7151 PendingIdentifierInfos.pop_front(); 7152 } 7153 7154 // Load pending declaration chains. 7155 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) { 7156 loadPendingDeclChain(PendingDeclChains[I]); 7157 PendingDeclChainsKnown.erase(PendingDeclChains[I]); 7158 } 7159 PendingDeclChains.clear(); 7160 7161 // Load any pending macro definitions. 7162 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) { 7163 // FIXME: std::move here 7164 SmallVector<MacroID, 2> GlobalIDs = PendingMacroIDs.begin()[I].second; 7165 MacroInfo *Hint = 0; 7166 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 7167 ++IDIdx) { 7168 Hint = getMacro(GlobalIDs[IDIdx], Hint); 7169 } 7170 } 7171 PendingMacroIDs.clear(); 7172 } 7173 7174 // If we deserialized any C++ or Objective-C class definitions, any 7175 // Objective-C protocol definitions, or any redeclarable templates, make sure 7176 // that all redeclarations point to the definitions. Note that this can only 7177 // happen now, after the redeclaration chains have been fully wired. 7178 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(), 7179 DEnd = PendingDefinitions.end(); 7180 D != DEnd; ++D) { 7181 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) { 7182 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) { 7183 // Make sure that the TagType points at the definition. 7184 const_cast<TagType*>(TagT)->decl = TD; 7185 } 7186 7187 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) { 7188 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(), 7189 REnd = RD->redecls_end(); 7190 R != REnd; ++R) 7191 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData; 7192 7193 } 7194 7195 continue; 7196 } 7197 7198 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) { 7199 // Make sure that the ObjCInterfaceType points at the definition. 7200 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl)) 7201 ->Decl = ID; 7202 7203 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(), 7204 REnd = ID->redecls_end(); 7205 R != REnd; ++R) 7206 R->Data = ID->Data; 7207 7208 continue; 7209 } 7210 7211 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) { 7212 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(), 7213 REnd = PD->redecls_end(); 7214 R != REnd; ++R) 7215 R->Data = PD->Data; 7216 7217 continue; 7218 } 7219 7220 RedeclarableTemplateDecl *RTD 7221 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl(); 7222 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(), 7223 REnd = RTD->redecls_end(); 7224 R != REnd; ++R) 7225 R->Common = RTD->Common; 7226 } 7227 PendingDefinitions.clear(); 7228 7229 // Load the bodies of any functions or methods we've encountered. We do 7230 // this now (delayed) so that we can be sure that the declaration chains 7231 // have been fully wired up. 7232 for (PendingBodiesMap::iterator PB = PendingBodies.begin(), 7233 PBEnd = PendingBodies.end(); 7234 PB != PBEnd; ++PB) { 7235 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) { 7236 // FIXME: Check for =delete/=default? 7237 // FIXME: Complain about ODR violations here? 7238 if (!getContext().getLangOpts().Modules || !FD->hasBody()) 7239 FD->setLazyBody(PB->second); 7240 continue; 7241 } 7242 7243 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first); 7244 if (!getContext().getLangOpts().Modules || !MD->hasBody()) 7245 MD->setLazyBody(PB->second); 7246 } 7247 PendingBodies.clear(); 7248} 7249 7250void ASTReader::FinishedDeserializing() { 7251 assert(NumCurrentElementsDeserializing && 7252 "FinishedDeserializing not paired with StartedDeserializing"); 7253 if (NumCurrentElementsDeserializing == 1) { 7254 // We decrease NumCurrentElementsDeserializing only after pending actions 7255 // are finished, to avoid recursively re-calling finishPendingActions(). 7256 finishPendingActions(); 7257 } 7258 --NumCurrentElementsDeserializing; 7259 7260 if (NumCurrentElementsDeserializing == 0 && 7261 Consumer && !PassingDeclsToConsumer) { 7262 // Guard variable to avoid recursively redoing the process of passing 7263 // decls to consumer. 7264 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer, 7265 true); 7266 7267 while (!InterestingDecls.empty()) { 7268 // We are not in recursive loading, so it's safe to pass the "interesting" 7269 // decls to the consumer. 7270 Decl *D = InterestingDecls.front(); 7271 InterestingDecls.pop_front(); 7272 PassInterestingDeclToConsumer(D); 7273 } 7274 } 7275} 7276 7277ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context, 7278 StringRef isysroot, bool DisableValidation, 7279 bool DisableStatCache, bool AllowASTWithCompilerErrors) 7280 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0), 7281 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), 7282 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context), 7283 Consumer(0), ModuleMgr(PP.getFileManager()), 7284 isysroot(isysroot), DisableValidation(DisableValidation), 7285 DisableStatCache(DisableStatCache), 7286 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors), 7287 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts), 7288 NumStatHits(0), NumStatMisses(0), 7289 NumSLocEntriesRead(0), TotalNumSLocEntries(0), 7290 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0), 7291 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0), 7292 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0), 7293 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0), 7294 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0), 7295 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0), 7296 PassingDeclsToConsumer(false), 7297 NumCXXBaseSpecifiersLoaded(0) 7298{ 7299 SourceMgr.setExternalSLocEntrySource(this); 7300} 7301 7302ASTReader::~ASTReader() { 7303 for (DeclContextVisibleUpdatesPending::iterator 7304 I = PendingVisibleUpdates.begin(), 7305 E = PendingVisibleUpdates.end(); 7306 I != E; ++I) { 7307 for (DeclContextVisibleUpdates::iterator J = I->second.begin(), 7308 F = I->second.end(); 7309 J != F; ++J) 7310 delete J->first; 7311 } 7312} 7313