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