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