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