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