1 //===- ASTReader.h - AST File Reader ----------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the ASTReader class, which reads AST files. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_CLANG_SERIALIZATION_ASTREADER_H 14 #define LLVM_CLANG_SERIALIZATION_ASTREADER_H 15 16 #include "clang/AST/Type.h" 17 #include "clang/Basic/Diagnostic.h" 18 #include "clang/Basic/DiagnosticOptions.h" 19 #include "clang/Basic/IdentifierTable.h" 20 #include "clang/Basic/OpenCLOptions.h" 21 #include "clang/Basic/SourceLocation.h" 22 #include "clang/Basic/StackExhaustionHandler.h" 23 #include "clang/Basic/Version.h" 24 #include "clang/Lex/ExternalPreprocessorSource.h" 25 #include "clang/Lex/HeaderSearch.h" 26 #include "clang/Lex/PreprocessingRecord.h" 27 #include "clang/Lex/PreprocessorOptions.h" 28 #include "clang/Sema/ExternalSemaSource.h" 29 #include "clang/Sema/IdentifierResolver.h" 30 #include "clang/Sema/Sema.h" 31 #include "clang/Serialization/ASTBitCodes.h" 32 #include "clang/Serialization/ContinuousRangeMap.h" 33 #include "clang/Serialization/ModuleFile.h" 34 #include "clang/Serialization/ModuleFileExtension.h" 35 #include "clang/Serialization/ModuleManager.h" 36 #include "clang/Serialization/SourceLocationEncoding.h" 37 #include "llvm/ADT/ArrayRef.h" 38 #include "llvm/ADT/DenseMap.h" 39 #include "llvm/ADT/DenseSet.h" 40 #include "llvm/ADT/IntrusiveRefCntPtr.h" 41 #include "llvm/ADT/MapVector.h" 42 #include "llvm/ADT/PagedVector.h" 43 #include "llvm/ADT/STLExtras.h" 44 #include "llvm/ADT/SetVector.h" 45 #include "llvm/ADT/SmallPtrSet.h" 46 #include "llvm/ADT/SmallVector.h" 47 #include "llvm/ADT/StringMap.h" 48 #include "llvm/ADT/StringRef.h" 49 #include "llvm/ADT/iterator.h" 50 #include "llvm/ADT/iterator_range.h" 51 #include "llvm/Bitstream/BitstreamReader.h" 52 #include "llvm/Support/MemoryBuffer.h" 53 #include "llvm/Support/SaveAndRestore.h" 54 #include "llvm/Support/Timer.h" 55 #include "llvm/Support/VersionTuple.h" 56 #include <cassert> 57 #include <cstddef> 58 #include <cstdint> 59 #include <ctime> 60 #include <deque> 61 #include <memory> 62 #include <optional> 63 #include <set> 64 #include <string> 65 #include <utility> 66 #include <vector> 67 68 namespace clang { 69 70 class ASTConsumer; 71 class ASTContext; 72 class ASTDeserializationListener; 73 class ASTReader; 74 class ASTRecordReader; 75 class CXXTemporary; 76 class Decl; 77 class DeclarationName; 78 class DeclaratorDecl; 79 class DeclContext; 80 class EnumDecl; 81 class Expr; 82 class FieldDecl; 83 class FileEntry; 84 class FileManager; 85 class FileSystemOptions; 86 class FunctionDecl; 87 class GlobalModuleIndex; 88 struct HeaderFileInfo; 89 class HeaderSearchOptions; 90 class LangOptions; 91 class MacroInfo; 92 class InMemoryModuleCache; 93 class NamedDecl; 94 class NamespaceDecl; 95 class ObjCCategoryDecl; 96 class ObjCInterfaceDecl; 97 class PCHContainerReader; 98 class Preprocessor; 99 class PreprocessorOptions; 100 class Sema; 101 class SourceManager; 102 class Stmt; 103 class SwitchCase; 104 class TargetOptions; 105 class Token; 106 class TypedefNameDecl; 107 class ValueDecl; 108 class VarDecl; 109 110 /// Abstract interface for callback invocations by the ASTReader. 111 /// 112 /// While reading an AST file, the ASTReader will call the methods of the 113 /// listener to pass on specific information. Some of the listener methods can 114 /// return true to indicate to the ASTReader that the information (and 115 /// consequently the AST file) is invalid. 116 class ASTReaderListener { 117 public: 118 virtual ~ASTReaderListener(); 119 120 /// Receives the full Clang version information. 121 /// 122 /// \returns true to indicate that the version is invalid. Subclasses should 123 /// generally defer to this implementation. 124 virtual bool ReadFullVersionInformation(StringRef FullVersion) { 125 return FullVersion != getClangFullRepositoryVersion(); 126 } 127 128 virtual void ReadModuleName(StringRef ModuleName) {} 129 virtual void ReadModuleMapFile(StringRef ModuleMapPath) {} 130 131 /// Receives the language options. 132 /// 133 /// \returns true to indicate the options are invalid or false otherwise. 134 virtual bool ReadLanguageOptions(const LangOptions &LangOpts, 135 StringRef ModuleFilename, bool Complain, 136 bool AllowCompatibleDifferences) { 137 return false; 138 } 139 140 /// Receives the target options. 141 /// 142 /// \returns true to indicate the target options are invalid, or false 143 /// otherwise. 144 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts, 145 StringRef ModuleFilename, bool Complain, 146 bool AllowCompatibleDifferences) { 147 return false; 148 } 149 150 /// Receives the diagnostic options. 151 /// 152 /// \returns true to indicate the diagnostic options are invalid, or false 153 /// otherwise. 154 virtual bool 155 ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, 156 StringRef ModuleFilename, bool Complain) { 157 return false; 158 } 159 160 /// Receives the file system options. 161 /// 162 /// \returns true to indicate the file system options are invalid, or false 163 /// otherwise. 164 virtual bool ReadFileSystemOptions(const FileSystemOptions &FSOpts, 165 bool Complain) { 166 return false; 167 } 168 169 /// Receives the header search options. 170 /// 171 /// \param HSOpts The read header search options. The following fields are 172 /// missing and are reported in ReadHeaderSearchPaths(): 173 /// UserEntries, SystemHeaderPrefixes, VFSOverlayFiles. 174 /// 175 /// \returns true to indicate the header search options are invalid, or false 176 /// otherwise. 177 virtual bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 178 StringRef ModuleFilename, 179 StringRef SpecificModuleCachePath, 180 bool Complain) { 181 return false; 182 } 183 184 /// Receives the header search paths. 185 /// 186 /// \param HSOpts The read header search paths. Only the following fields are 187 /// initialized: UserEntries, SystemHeaderPrefixes, 188 /// VFSOverlayFiles. The rest is reported in 189 /// ReadHeaderSearchOptions(). 190 /// 191 /// \returns true to indicate the header search paths are invalid, or false 192 /// otherwise. 193 virtual bool ReadHeaderSearchPaths(const HeaderSearchOptions &HSOpts, 194 bool Complain) { 195 return false; 196 } 197 198 /// Receives the preprocessor options. 199 /// 200 /// \param SuggestedPredefines Can be filled in with the set of predefines 201 /// that are suggested by the preprocessor options. Typically only used when 202 /// loading a precompiled header. 203 /// 204 /// \returns true to indicate the preprocessor options are invalid, or false 205 /// otherwise. 206 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 207 StringRef ModuleFilename, 208 bool ReadMacros, bool Complain, 209 std::string &SuggestedPredefines) { 210 return false; 211 } 212 213 /// Receives __COUNTER__ value. 214 virtual void ReadCounter(const serialization::ModuleFile &M, 215 unsigned Value) {} 216 217 /// This is called for each AST file loaded. 218 virtual void visitModuleFile(StringRef Filename, 219 serialization::ModuleKind Kind) {} 220 221 /// Returns true if this \c ASTReaderListener wants to receive the 222 /// input files of the AST file via \c visitInputFile, false otherwise. 223 virtual bool needsInputFileVisitation() { return false; } 224 225 /// Returns true if this \c ASTReaderListener wants to receive the 226 /// system input files of the AST file via \c visitInputFile, false otherwise. 227 virtual bool needsSystemInputFileVisitation() { return false; } 228 229 /// if \c needsInputFileVisitation returns true, this is called for 230 /// each non-system input file of the AST File. If 231 /// \c needsSystemInputFileVisitation is true, then it is called for all 232 /// system input files as well. 233 /// 234 /// \returns true to continue receiving the next input file, false to stop. 235 virtual bool visitInputFile(StringRef Filename, bool isSystem, 236 bool isOverridden, bool isExplicitModule) { 237 return true; 238 } 239 240 /// Returns true if this \c ASTReaderListener wants to receive the 241 /// imports of the AST file via \c visitImport, false otherwise. 242 virtual bool needsImportVisitation() const { return false; } 243 244 /// If needsImportVisitation returns \c true, this is called for each 245 /// AST file imported by this AST file. 246 virtual void visitImport(StringRef ModuleName, StringRef Filename) {} 247 248 /// Indicates that a particular module file extension has been read. 249 virtual void readModuleFileExtension( 250 const ModuleFileExtensionMetadata &Metadata) {} 251 }; 252 253 /// Simple wrapper class for chaining listeners. 254 class ChainedASTReaderListener : public ASTReaderListener { 255 std::unique_ptr<ASTReaderListener> First; 256 std::unique_ptr<ASTReaderListener> Second; 257 258 public: 259 /// Takes ownership of \p First and \p Second. 260 ChainedASTReaderListener(std::unique_ptr<ASTReaderListener> First, 261 std::unique_ptr<ASTReaderListener> Second) 262 : First(std::move(First)), Second(std::move(Second)) {} 263 264 std::unique_ptr<ASTReaderListener> takeFirst() { return std::move(First); } 265 std::unique_ptr<ASTReaderListener> takeSecond() { return std::move(Second); } 266 267 bool ReadFullVersionInformation(StringRef FullVersion) override; 268 void ReadModuleName(StringRef ModuleName) override; 269 void ReadModuleMapFile(StringRef ModuleMapPath) override; 270 bool ReadLanguageOptions(const LangOptions &LangOpts, 271 StringRef ModuleFilename, bool Complain, 272 bool AllowCompatibleDifferences) override; 273 bool ReadTargetOptions(const TargetOptions &TargetOpts, 274 StringRef ModuleFilename, bool Complain, 275 bool AllowCompatibleDifferences) override; 276 bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, 277 StringRef ModuleFilename, bool Complain) override; 278 bool ReadFileSystemOptions(const FileSystemOptions &FSOpts, 279 bool Complain) override; 280 281 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 282 StringRef ModuleFilename, 283 StringRef SpecificModuleCachePath, 284 bool Complain) override; 285 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 286 StringRef ModuleFilename, bool ReadMacros, 287 bool Complain, 288 std::string &SuggestedPredefines) override; 289 290 void ReadCounter(const serialization::ModuleFile &M, unsigned Value) override; 291 bool needsInputFileVisitation() override; 292 bool needsSystemInputFileVisitation() override; 293 void visitModuleFile(StringRef Filename, 294 serialization::ModuleKind Kind) override; 295 bool visitInputFile(StringRef Filename, bool isSystem, 296 bool isOverridden, bool isExplicitModule) override; 297 void readModuleFileExtension( 298 const ModuleFileExtensionMetadata &Metadata) override; 299 }; 300 301 /// ASTReaderListener implementation to validate the information of 302 /// the PCH file against an initialized Preprocessor. 303 class PCHValidator : public ASTReaderListener { 304 Preprocessor &PP; 305 ASTReader &Reader; 306 307 public: 308 PCHValidator(Preprocessor &PP, ASTReader &Reader) 309 : PP(PP), Reader(Reader) {} 310 311 bool ReadLanguageOptions(const LangOptions &LangOpts, 312 StringRef ModuleFilename, bool Complain, 313 bool AllowCompatibleDifferences) override; 314 bool ReadTargetOptions(const TargetOptions &TargetOpts, 315 StringRef ModuleFilename, bool Complain, 316 bool AllowCompatibleDifferences) override; 317 bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, 318 StringRef ModuleFilename, bool Complain) override; 319 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 320 StringRef ModuleFilename, bool ReadMacros, 321 bool Complain, 322 std::string &SuggestedPredefines) override; 323 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 324 StringRef ModuleFilename, 325 StringRef SpecificModuleCachePath, 326 bool Complain) override; 327 void ReadCounter(const serialization::ModuleFile &M, unsigned Value) override; 328 }; 329 330 /// ASTReaderListenter implementation to set SuggestedPredefines of 331 /// ASTReader which is required to use a pch file. This is the replacement 332 /// of PCHValidator or SimplePCHValidator when using a pch file without 333 /// validating it. 334 class SimpleASTReaderListener : public ASTReaderListener { 335 Preprocessor &PP; 336 337 public: 338 SimpleASTReaderListener(Preprocessor &PP) : PP(PP) {} 339 340 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 341 StringRef ModuleFilename, bool ReadMacros, 342 bool Complain, 343 std::string &SuggestedPredefines) override; 344 }; 345 346 namespace serialization { 347 348 class ReadMethodPoolVisitor; 349 350 namespace reader { 351 352 class ASTIdentifierLookupTrait; 353 354 /// The on-disk hash table(s) used for DeclContext name lookup. 355 struct DeclContextLookupTable; 356 struct ModuleLocalLookupTable; 357 358 /// The on-disk hash table(s) used for specialization decls. 359 struct LazySpecializationInfoLookupTable; 360 361 } // namespace reader 362 363 } // namespace serialization 364 365 /// Reads an AST files chain containing the contents of a translation 366 /// unit. 367 /// 368 /// The ASTReader class reads bitstreams (produced by the ASTWriter 369 /// class) containing the serialized representation of a given 370 /// abstract syntax tree and its supporting data structures. An 371 /// instance of the ASTReader can be attached to an ASTContext object, 372 /// which will provide access to the contents of the AST files. 373 /// 374 /// The AST reader provides lazy de-serialization of declarations, as 375 /// required when traversing the AST. Only those AST nodes that are 376 /// actually required will be de-serialized. 377 class ASTReader 378 : public ExternalPreprocessorSource, 379 public ExternalPreprocessingRecordSource, 380 public ExternalHeaderFileInfoSource, 381 public ExternalSemaSource, 382 public IdentifierInfoLookup, 383 public ExternalSLocEntrySource 384 { 385 public: 386 /// Types of AST files. 387 friend class ASTDeclMerger; 388 friend class ASTDeclReader; 389 friend class ASTIdentifierIterator; 390 friend class ASTRecordReader; 391 friend class ASTUnit; // ASTUnit needs to remap source locations. 392 friend class ASTWriter; 393 friend class PCHValidator; 394 friend class serialization::reader::ASTIdentifierLookupTrait; 395 friend class serialization::ReadMethodPoolVisitor; 396 friend class TypeLocReader; 397 friend class LocalDeclID; 398 399 using RecordData = SmallVector<uint64_t, 64>; 400 using RecordDataImpl = SmallVectorImpl<uint64_t>; 401 402 /// The result of reading the control block of an AST file, which 403 /// can fail for various reasons. 404 enum ASTReadResult { 405 /// The control block was read successfully. Aside from failures, 406 /// the AST file is safe to read into the current context. 407 Success, 408 409 /// The AST file itself appears corrupted. 410 Failure, 411 412 /// The AST file was missing. 413 Missing, 414 415 /// The AST file is out-of-date relative to its input files, 416 /// and needs to be regenerated. 417 OutOfDate, 418 419 /// The AST file was written by a different version of Clang. 420 VersionMismatch, 421 422 /// The AST file was written with a different language/target 423 /// configuration. 424 ConfigurationMismatch, 425 426 /// The AST file has errors. 427 HadErrors 428 }; 429 430 using ModuleFile = serialization::ModuleFile; 431 using ModuleKind = serialization::ModuleKind; 432 using ModuleManager = serialization::ModuleManager; 433 using ModuleIterator = ModuleManager::ModuleIterator; 434 using ModuleConstIterator = ModuleManager::ModuleConstIterator; 435 using ModuleReverseIterator = ModuleManager::ModuleReverseIterator; 436 437 private: 438 using LocSeq = SourceLocationSequence; 439 440 /// The receiver of some callbacks invoked by ASTReader. 441 std::unique_ptr<ASTReaderListener> Listener; 442 443 /// The receiver of deserialization events. 444 ASTDeserializationListener *DeserializationListener = nullptr; 445 446 bool OwnsDeserializationListener = false; 447 448 SourceManager &SourceMgr; 449 FileManager &FileMgr; 450 const PCHContainerReader &PCHContainerRdr; 451 DiagnosticsEngine &Diags; 452 // Sema has duplicate logic, but SemaObj can sometimes be null so ASTReader 453 // has its own version. 454 StackExhaustionHandler StackHandler; 455 456 /// The semantic analysis object that will be processing the 457 /// AST files and the translation unit that uses it. 458 Sema *SemaObj = nullptr; 459 460 /// The preprocessor that will be loading the source file. 461 Preprocessor &PP; 462 463 /// The AST context into which we'll read the AST files. 464 ASTContext *ContextObj = nullptr; 465 466 /// The AST consumer. 467 ASTConsumer *Consumer = nullptr; 468 469 /// The module manager which manages modules and their dependencies 470 ModuleManager ModuleMgr; 471 472 /// A dummy identifier resolver used to merge TU-scope declarations in 473 /// C, for the cases where we don't have a Sema object to provide a real 474 /// identifier resolver. 475 IdentifierResolver DummyIdResolver; 476 477 /// A mapping from extension block names to module file extensions. 478 llvm::StringMap<std::shared_ptr<ModuleFileExtension>> ModuleFileExtensions; 479 480 /// A timer used to track the time spent deserializing. 481 std::unique_ptr<llvm::Timer> ReadTimer; 482 483 /// The location where the module file will be considered as 484 /// imported from. For non-module AST types it should be invalid. 485 SourceLocation CurrentImportLoc; 486 487 /// The module kind that is currently deserializing. 488 std::optional<ModuleKind> CurrentDeserializingModuleKind; 489 490 /// The global module index, if loaded. 491 std::unique_ptr<GlobalModuleIndex> GlobalIndex; 492 493 /// A map of global bit offsets to the module that stores entities 494 /// at those bit offsets. 495 ContinuousRangeMap<uint64_t, ModuleFile*, 4> GlobalBitOffsetsMap; 496 497 /// A map of negated SLocEntryIDs to the modules containing them. 498 ContinuousRangeMap<unsigned, ModuleFile*, 64> GlobalSLocEntryMap; 499 500 using GlobalSLocOffsetMapType = 501 ContinuousRangeMap<unsigned, ModuleFile *, 64>; 502 503 /// A map of reversed (SourceManager::MaxLoadedOffset - SLocOffset) 504 /// SourceLocation offsets to the modules containing them. 505 GlobalSLocOffsetMapType GlobalSLocOffsetMap; 506 507 /// Types that have already been loaded from the chain. 508 /// 509 /// When the pointer at index I is non-NULL, the type with 510 /// ID = (I + 1) << FastQual::Width has already been loaded 511 llvm::PagedVector<QualType> TypesLoaded; 512 513 /// Declarations that have already been loaded from the chain. 514 /// 515 /// When the pointer at index I is non-NULL, the declaration with ID 516 /// = I + 1 has already been loaded. 517 llvm::PagedVector<Decl *> DeclsLoaded; 518 519 using FileOffset = std::pair<ModuleFile *, uint64_t>; 520 using FileOffsetsTy = SmallVector<FileOffset, 2>; 521 using DeclUpdateOffsetsMap = llvm::DenseMap<GlobalDeclID, FileOffsetsTy>; 522 523 /// Declarations that have modifications residing in a later file 524 /// in the chain. 525 DeclUpdateOffsetsMap DeclUpdateOffsets; 526 527 struct LookupBlockOffsets { 528 uint64_t LexicalOffset; 529 uint64_t VisibleOffset; 530 uint64_t ModuleLocalOffset; 531 uint64_t TULocalOffset; 532 }; 533 534 using DelayedNamespaceOffsetMapTy = 535 llvm::DenseMap<GlobalDeclID, LookupBlockOffsets>; 536 537 /// Mapping from global declaration IDs to the lexical and visible block 538 /// offset for delayed namespace in reduced BMI. 539 /// 540 /// We can't use the existing DeclUpdate mechanism since the DeclUpdate 541 /// may only be applied in an outer most read. However, we need to know 542 /// whether or not a DeclContext has external storage during the recursive 543 /// reading. So we need to apply the offset immediately after we read the 544 /// namespace as if it is not delayed. 545 DelayedNamespaceOffsetMapTy DelayedNamespaceOffsetMap; 546 547 /// Mapping from main decl ID to the related decls IDs. 548 /// 549 /// The key is the main decl ID, and the value is a vector of related decls 550 /// that must be loaded immediately after the main decl. This is necessary 551 /// to ensure that the definition for related decls comes from the same module 552 /// as the enclosing main decl. Without this, due to lazy deserialization, 553 /// the definition for the main decl and related decls may come from different 554 /// modules. It is used for the following cases: 555 /// - Lambda inside a template function definition: The main declaration is 556 /// the enclosing function, and the related declarations are the lambda 557 /// declarations. 558 /// - Friend function defined inside a template CXXRecord declaration: The 559 /// main declaration is the enclosing record, and the related declarations 560 /// are the friend functions. 561 llvm::DenseMap<GlobalDeclID, SmallVector<GlobalDeclID, 4>> RelatedDeclsMap; 562 563 struct PendingUpdateRecord { 564 Decl *D; 565 GlobalDeclID ID; 566 567 // Whether the declaration was just deserialized. 568 bool JustLoaded; 569 570 PendingUpdateRecord(GlobalDeclID ID, Decl *D, bool JustLoaded) 571 : D(D), ID(ID), JustLoaded(JustLoaded) {} 572 }; 573 574 /// Declaration updates for already-loaded declarations that we need 575 /// to apply once we finish processing an import. 576 llvm::SmallVector<PendingUpdateRecord, 16> PendingUpdateRecords; 577 578 enum class PendingFakeDefinitionKind { NotFake, Fake, FakeLoaded }; 579 580 /// The DefinitionData pointers that we faked up for class definitions 581 /// that we needed but hadn't loaded yet. 582 llvm::DenseMap<void *, PendingFakeDefinitionKind> PendingFakeDefinitionData; 583 584 /// Exception specification updates that have been loaded but not yet 585 /// propagated across the relevant redeclaration chain. The map key is the 586 /// canonical declaration (used only for deduplication) and the value is a 587 /// declaration that has an exception specification. 588 llvm::SmallMapVector<Decl *, FunctionDecl *, 4> PendingExceptionSpecUpdates; 589 590 /// Deduced return type updates that have been loaded but not yet propagated 591 /// across the relevant redeclaration chain. The map key is the canonical 592 /// declaration and the value is the deduced return type. 593 llvm::SmallMapVector<FunctionDecl *, QualType, 4> PendingDeducedTypeUpdates; 594 595 /// Functions has undededuced return type and we wish we can find the deduced 596 /// return type by iterating the redecls in other modules. 597 llvm::SmallVector<FunctionDecl *, 4> PendingUndeducedFunctionDecls; 598 599 /// Declarations that have been imported and have typedef names for 600 /// linkage purposes. 601 llvm::DenseMap<std::pair<DeclContext *, IdentifierInfo *>, NamedDecl *> 602 ImportedTypedefNamesForLinkage; 603 604 /// Mergeable declaration contexts that have anonymous declarations 605 /// within them, and those anonymous declarations. 606 llvm::DenseMap<Decl*, llvm::SmallVector<NamedDecl*, 2>> 607 AnonymousDeclarationsForMerging; 608 609 /// Map from numbering information for lambdas to the corresponding lambdas. 610 llvm::DenseMap<std::pair<const Decl *, unsigned>, NamedDecl *> 611 LambdaDeclarationsForMerging; 612 613 /// Key used to identify LifetimeExtendedTemporaryDecl for merging, 614 /// containing the lifetime-extending declaration and the mangling number. 615 using LETemporaryKey = std::pair<Decl *, unsigned>; 616 617 /// Map of already deserialiazed temporaries. 618 llvm::DenseMap<LETemporaryKey, LifetimeExtendedTemporaryDecl *> 619 LETemporaryForMerging; 620 621 struct FileDeclsInfo { 622 ModuleFile *Mod = nullptr; 623 ArrayRef<serialization::unaligned_decl_id_t> Decls; 624 625 FileDeclsInfo() = default; 626 FileDeclsInfo(ModuleFile *Mod, 627 ArrayRef<serialization::unaligned_decl_id_t> Decls) 628 : Mod(Mod), Decls(Decls) {} 629 }; 630 631 /// Map from a FileID to the file-level declarations that it contains. 632 llvm::DenseMap<FileID, FileDeclsInfo> FileDeclIDs; 633 634 /// An array of lexical contents of a declaration context, as a sequence of 635 /// Decl::Kind, DeclID pairs. 636 using LexicalContents = ArrayRef<serialization::unaligned_decl_id_t>; 637 638 /// Map from a DeclContext to its lexical contents. 639 llvm::DenseMap<const DeclContext*, std::pair<ModuleFile*, LexicalContents>> 640 LexicalDecls; 641 642 /// Map from the TU to its lexical contents from each module file. 643 std::vector<std::pair<ModuleFile*, LexicalContents>> TULexicalDecls; 644 645 /// Map from a DeclContext to its lookup tables. 646 llvm::DenseMap<const DeclContext *, 647 serialization::reader::DeclContextLookupTable> Lookups; 648 llvm::DenseMap<const DeclContext *, 649 serialization::reader::ModuleLocalLookupTable> 650 ModuleLocalLookups; 651 llvm::DenseMap<const DeclContext *, 652 serialization::reader::DeclContextLookupTable> 653 TULocalLookups; 654 655 using SpecLookupTableTy = 656 llvm::DenseMap<const Decl *, 657 serialization::reader::LazySpecializationInfoLookupTable>; 658 /// Map from decls to specialized decls. 659 SpecLookupTableTy SpecializationsLookups; 660 /// Split partial specialization from specialization to speed up lookups. 661 SpecLookupTableTy PartialSpecializationsLookups; 662 663 bool LoadExternalSpecializationsImpl(SpecLookupTableTy &SpecLookups, 664 const Decl *D); 665 bool LoadExternalSpecializationsImpl(SpecLookupTableTy &SpecLookups, 666 const Decl *D, 667 ArrayRef<TemplateArgument> TemplateArgs); 668 669 // Updates for visible decls can occur for other contexts than just the 670 // TU, and when we read those update records, the actual context may not 671 // be available yet, so have this pending map using the ID as a key. It 672 // will be realized when the data is actually loaded. 673 struct UpdateData { 674 ModuleFile *Mod; 675 const unsigned char *Data; 676 }; 677 using DeclContextVisibleUpdates = SmallVector<UpdateData, 1>; 678 679 /// Updates to the visible declarations of declaration contexts that 680 /// haven't been loaded yet. 681 llvm::DenseMap<GlobalDeclID, DeclContextVisibleUpdates> PendingVisibleUpdates; 682 llvm::DenseMap<GlobalDeclID, DeclContextVisibleUpdates> 683 PendingModuleLocalVisibleUpdates; 684 llvm::DenseMap<GlobalDeclID, DeclContextVisibleUpdates> TULocalUpdates; 685 686 using SpecializationsUpdate = SmallVector<UpdateData, 1>; 687 using SpecializationsUpdateMap = 688 llvm::DenseMap<GlobalDeclID, SpecializationsUpdate>; 689 SpecializationsUpdateMap PendingSpecializationsUpdates; 690 SpecializationsUpdateMap PendingPartialSpecializationsUpdates; 691 692 /// The set of C++ or Objective-C classes that have forward 693 /// declarations that have not yet been linked to their definitions. 694 llvm::SmallPtrSet<Decl *, 4> PendingDefinitions; 695 696 using PendingBodiesMap = 697 llvm::MapVector<Decl *, uint64_t, 698 llvm::SmallDenseMap<Decl *, unsigned, 4>, 699 SmallVector<std::pair<Decl *, uint64_t>, 4>>; 700 701 /// Functions or methods that have bodies that will be attached. 702 PendingBodiesMap PendingBodies; 703 704 /// Definitions for which we have added merged definitions but not yet 705 /// performed deduplication. 706 llvm::SetVector<NamedDecl *> PendingMergedDefinitionsToDeduplicate; 707 708 /// The duplicated definitions in module units which are pending to be warned. 709 /// We need to delay it to wait for the loading of definitions since we don't 710 /// want to warn for forward declarations. 711 llvm::SmallVector<std::pair<Decl *, Decl *>> 712 PendingWarningForDuplicatedDefsInModuleUnits; 713 714 /// Read the record that describes the lexical contents of a DC. 715 bool ReadLexicalDeclContextStorage(ModuleFile &M, 716 llvm::BitstreamCursor &Cursor, 717 uint64_t Offset, DeclContext *DC); 718 719 enum class VisibleDeclContextStorageKind { 720 GenerallyVisible, 721 ModuleLocalVisible, 722 TULocalVisible, 723 }; 724 725 /// Read the record that describes the visible contents of a DC. 726 bool ReadVisibleDeclContextStorage(ModuleFile &M, 727 llvm::BitstreamCursor &Cursor, 728 uint64_t Offset, GlobalDeclID ID, 729 VisibleDeclContextStorageKind VisibleKind); 730 731 bool ReadSpecializations(ModuleFile &M, llvm::BitstreamCursor &Cursor, 732 uint64_t Offset, Decl *D, bool IsPartial); 733 void AddSpecializations(const Decl *D, const unsigned char *Data, 734 ModuleFile &M, bool IsPartial); 735 736 /// A vector containing identifiers that have already been 737 /// loaded. 738 /// 739 /// If the pointer at index I is non-NULL, then it refers to the 740 /// IdentifierInfo for the identifier with ID=I+1 that has already 741 /// been loaded. 742 std::vector<IdentifierInfo *> IdentifiersLoaded; 743 744 /// A vector containing macros that have already been 745 /// loaded. 746 /// 747 /// If the pointer at index I is non-NULL, then it refers to the 748 /// MacroInfo for the identifier with ID=I+1 that has already 749 /// been loaded. 750 std::vector<MacroInfo *> MacrosLoaded; 751 752 using LoadedMacroInfo = 753 std::pair<IdentifierInfo *, serialization::SubmoduleID>; 754 755 /// A set of #undef directives that we have loaded; used to 756 /// deduplicate the same #undef information coming from multiple module 757 /// files. 758 llvm::DenseSet<LoadedMacroInfo> LoadedUndefs; 759 760 using GlobalMacroMapType = 761 ContinuousRangeMap<serialization::MacroID, ModuleFile *, 4>; 762 763 /// Mapping from global macro IDs to the module in which the 764 /// macro resides along with the offset that should be added to the 765 /// global macro ID to produce a local ID. 766 GlobalMacroMapType GlobalMacroMap; 767 768 /// A vector containing submodules that have already been loaded. 769 /// 770 /// This vector is indexed by the Submodule ID (-1). NULL submodule entries 771 /// indicate that the particular submodule ID has not yet been loaded. 772 SmallVector<Module *, 2> SubmodulesLoaded; 773 774 using GlobalSubmoduleMapType = 775 ContinuousRangeMap<serialization::SubmoduleID, ModuleFile *, 4>; 776 777 /// Mapping from global submodule IDs to the module file in which the 778 /// submodule resides along with the offset that should be added to the 779 /// global submodule ID to produce a local ID. 780 GlobalSubmoduleMapType GlobalSubmoduleMap; 781 782 /// A set of hidden declarations. 783 using HiddenNames = SmallVector<Decl *, 2>; 784 using HiddenNamesMapType = llvm::DenseMap<Module *, HiddenNames>; 785 786 /// A mapping from each of the hidden submodules to the deserialized 787 /// declarations in that submodule that could be made visible. 788 HiddenNamesMapType HiddenNamesMap; 789 790 /// A module import, export, or conflict that hasn't yet been resolved. 791 struct UnresolvedModuleRef { 792 /// The file in which this module resides. 793 ModuleFile *File; 794 795 /// The module that is importing or exporting. 796 Module *Mod; 797 798 /// The kind of module reference. 799 enum { Import, Export, Conflict, Affecting } Kind; 800 801 /// The local ID of the module that is being exported. 802 unsigned ID; 803 804 /// Whether this is a wildcard export. 805 LLVM_PREFERRED_TYPE(bool) 806 unsigned IsWildcard : 1; 807 808 /// String data. 809 StringRef String; 810 }; 811 812 /// The set of module imports and exports that still need to be 813 /// resolved. 814 SmallVector<UnresolvedModuleRef, 2> UnresolvedModuleRefs; 815 816 /// A vector containing selectors that have already been loaded. 817 /// 818 /// This vector is indexed by the Selector ID (-1). NULL selector 819 /// entries indicate that the particular selector ID has not yet 820 /// been loaded. 821 SmallVector<Selector, 16> SelectorsLoaded; 822 823 using GlobalSelectorMapType = 824 ContinuousRangeMap<serialization::SelectorID, ModuleFile *, 4>; 825 826 /// Mapping from global selector IDs to the module in which the 827 /// global selector ID to produce a local ID. 828 GlobalSelectorMapType GlobalSelectorMap; 829 830 /// The generation number of the last time we loaded data from the 831 /// global method pool for this selector. 832 llvm::DenseMap<Selector, unsigned> SelectorGeneration; 833 834 /// Whether a selector is out of date. We mark a selector as out of date 835 /// if we load another module after the method pool entry was pulled in. 836 llvm::DenseMap<Selector, bool> SelectorOutOfDate; 837 838 struct PendingMacroInfo { 839 ModuleFile *M; 840 /// Offset relative to ModuleFile::MacroOffsetsBase. 841 uint32_t MacroDirectivesOffset; 842 843 PendingMacroInfo(ModuleFile *M, uint32_t MacroDirectivesOffset) 844 : M(M), MacroDirectivesOffset(MacroDirectivesOffset) {} 845 }; 846 847 using PendingMacroIDsMap = 848 llvm::MapVector<IdentifierInfo *, SmallVector<PendingMacroInfo, 2>>; 849 850 /// Mapping from identifiers that have a macro history to the global 851 /// IDs have not yet been deserialized to the global IDs of those macros. 852 PendingMacroIDsMap PendingMacroIDs; 853 854 using GlobalPreprocessedEntityMapType = 855 ContinuousRangeMap<unsigned, ModuleFile *, 4>; 856 857 /// Mapping from global preprocessing entity IDs to the module in 858 /// which the preprocessed entity resides along with the offset that should be 859 /// added to the global preprocessing entity ID to produce a local ID. 860 GlobalPreprocessedEntityMapType GlobalPreprocessedEntityMap; 861 862 using GlobalSkippedRangeMapType = 863 ContinuousRangeMap<unsigned, ModuleFile *, 4>; 864 865 /// Mapping from global skipped range base IDs to the module in which 866 /// the skipped ranges reside. 867 GlobalSkippedRangeMapType GlobalSkippedRangeMap; 868 869 /// \name CodeGen-relevant special data 870 /// Fields containing data that is relevant to CodeGen. 871 //@{ 872 873 /// The IDs of all declarations that fulfill the criteria of 874 /// "interesting" decls. 875 /// 876 /// This contains the data loaded from all EAGERLY_DESERIALIZED_DECLS blocks 877 /// in the chain. The referenced declarations are deserialized and passed to 878 /// the consumer eagerly. 879 SmallVector<GlobalDeclID, 16> EagerlyDeserializedDecls; 880 881 /// The IDs of all vtables to emit. The referenced declarations are passed 882 /// to the consumers' HandleVTable eagerly after passing 883 /// EagerlyDeserializedDecls. 884 SmallVector<GlobalDeclID, 16> VTablesToEmit; 885 886 /// The IDs of all tentative definitions stored in the chain. 887 /// 888 /// Sema keeps track of all tentative definitions in a TU because it has to 889 /// complete them and pass them on to CodeGen. Thus, tentative definitions in 890 /// the PCH chain must be eagerly deserialized. 891 SmallVector<GlobalDeclID, 16> TentativeDefinitions; 892 893 /// The IDs of all CXXRecordDecls stored in the chain whose VTables are 894 /// used. 895 /// 896 /// CodeGen has to emit VTables for these records, so they have to be eagerly 897 /// deserialized. 898 struct VTableUse { 899 GlobalDeclID ID; 900 SourceLocation::UIntTy RawLoc; 901 bool Used; 902 }; 903 SmallVector<VTableUse> VTableUses; 904 905 /// A snapshot of the pending instantiations in the chain. 906 /// 907 /// This record tracks the instantiations that Sema has to perform at the 908 /// end of the TU. It consists of a pair of values for every pending 909 /// instantiation where the first value is the ID of the decl and the second 910 /// is the instantiation location. 911 struct PendingInstantiation { 912 GlobalDeclID ID; 913 SourceLocation::UIntTy RawLoc; 914 }; 915 SmallVector<PendingInstantiation, 64> PendingInstantiations; 916 917 //@} 918 919 /// \name DiagnosticsEngine-relevant special data 920 /// Fields containing data that is used for generating diagnostics 921 //@{ 922 923 /// A snapshot of Sema's unused file-scoped variable tracking, for 924 /// generating warnings. 925 SmallVector<GlobalDeclID, 16> UnusedFileScopedDecls; 926 927 /// A list of all the delegating constructors we've seen, to diagnose 928 /// cycles. 929 SmallVector<GlobalDeclID, 4> DelegatingCtorDecls; 930 931 /// Method selectors used in a @selector expression. Used for 932 /// implementation of -Wselector. 933 SmallVector<serialization::SelectorID, 64> ReferencedSelectorsData; 934 935 /// A snapshot of Sema's weak undeclared identifier tracking, for 936 /// generating warnings. 937 SmallVector<serialization::IdentifierID, 64> WeakUndeclaredIdentifiers; 938 939 /// The IDs of type aliases for ext_vectors that exist in the chain. 940 /// 941 /// Used by Sema for finding sugared names for ext_vectors in diagnostics. 942 SmallVector<GlobalDeclID, 4> ExtVectorDecls; 943 944 //@} 945 946 /// \name Sema-relevant special data 947 /// Fields containing data that is used for semantic analysis 948 //@{ 949 950 /// The IDs of all potentially unused typedef names in the chain. 951 /// 952 /// Sema tracks these to emit warnings. 953 SmallVector<GlobalDeclID, 16> UnusedLocalTypedefNameCandidates; 954 955 /// Our current depth in #pragma cuda force_host_device begin/end 956 /// macros. 957 unsigned ForceHostDeviceDepth = 0; 958 959 /// The IDs of the declarations Sema stores directly. 960 /// 961 /// Sema tracks a few important decls, such as namespace std, directly. 962 SmallVector<GlobalDeclID, 4> SemaDeclRefs; 963 964 /// The IDs of the types ASTContext stores directly. 965 /// 966 /// The AST context tracks a few important types, such as va_list, directly. 967 SmallVector<serialization::TypeID, 16> SpecialTypes; 968 969 /// The IDs of CUDA-specific declarations ASTContext stores directly. 970 /// 971 /// The AST context tracks a few important decls, currently cudaConfigureCall, 972 /// directly. 973 SmallVector<GlobalDeclID, 2> CUDASpecialDeclRefs; 974 975 /// The floating point pragma option settings. 976 SmallVector<uint64_t, 1> FPPragmaOptions; 977 978 /// The pragma clang optimize location (if the pragma state is "off"). 979 SourceLocation OptimizeOffPragmaLocation; 980 981 /// The PragmaMSStructKind pragma ms_struct state if set, or -1. 982 int PragmaMSStructState = -1; 983 984 /// The PragmaMSPointersToMembersKind pragma pointers_to_members state. 985 int PragmaMSPointersToMembersState = -1; 986 SourceLocation PointersToMembersPragmaLocation; 987 988 /// The pragma float_control state. 989 std::optional<FPOptionsOverride> FpPragmaCurrentValue; 990 SourceLocation FpPragmaCurrentLocation; 991 struct FpPragmaStackEntry { 992 FPOptionsOverride Value; 993 SourceLocation Location; 994 SourceLocation PushLocation; 995 StringRef SlotLabel; 996 }; 997 llvm::SmallVector<FpPragmaStackEntry, 2> FpPragmaStack; 998 llvm::SmallVector<std::string, 2> FpPragmaStrings; 999 1000 /// The pragma align/pack state. 1001 std::optional<Sema::AlignPackInfo> PragmaAlignPackCurrentValue; 1002 SourceLocation PragmaAlignPackCurrentLocation; 1003 struct PragmaAlignPackStackEntry { 1004 Sema::AlignPackInfo Value; 1005 SourceLocation Location; 1006 SourceLocation PushLocation; 1007 StringRef SlotLabel; 1008 }; 1009 llvm::SmallVector<PragmaAlignPackStackEntry, 2> PragmaAlignPackStack; 1010 llvm::SmallVector<std::string, 2> PragmaAlignPackStrings; 1011 1012 /// The OpenCL extension settings. 1013 OpenCLOptions OpenCLExtensions; 1014 1015 /// Extensions required by an OpenCL type. 1016 llvm::DenseMap<const Type *, std::set<std::string>> OpenCLTypeExtMap; 1017 1018 /// Extensions required by an OpenCL declaration. 1019 llvm::DenseMap<const Decl *, std::set<std::string>> OpenCLDeclExtMap; 1020 1021 /// A list of the namespaces we've seen. 1022 SmallVector<GlobalDeclID, 4> KnownNamespaces; 1023 1024 /// A list of undefined decls with internal linkage followed by the 1025 /// SourceLocation of a matching ODR-use. 1026 struct UndefinedButUsedDecl { 1027 GlobalDeclID ID; 1028 SourceLocation::UIntTy RawLoc; 1029 }; 1030 SmallVector<UndefinedButUsedDecl, 8> UndefinedButUsed; 1031 1032 /// Delete expressions to analyze at the end of translation unit. 1033 SmallVector<uint64_t, 8> DelayedDeleteExprs; 1034 1035 // A list of late parsed template function data with their module files. 1036 SmallVector<std::pair<ModuleFile *, SmallVector<uint64_t, 1>>, 4> 1037 LateParsedTemplates; 1038 1039 /// The IDs of all decls to be checked for deferred diags. 1040 /// 1041 /// Sema tracks these to emit deferred diags. 1042 llvm::SmallSetVector<GlobalDeclID, 4> DeclsToCheckForDeferredDiags; 1043 1044 /// The IDs of all decls with function effects to be checked. 1045 SmallVector<GlobalDeclID> DeclsWithEffectsToVerify; 1046 1047 private: 1048 struct ImportedSubmodule { 1049 serialization::SubmoduleID ID; 1050 SourceLocation ImportLoc; 1051 1052 ImportedSubmodule(serialization::SubmoduleID ID, SourceLocation ImportLoc) 1053 : ID(ID), ImportLoc(ImportLoc) {} 1054 }; 1055 1056 /// A list of modules that were imported by precompiled headers or 1057 /// any other non-module AST file and have not yet been made visible. If a 1058 /// module is made visible in the ASTReader, it will be transfered to 1059 /// \c PendingImportedModulesSema. 1060 SmallVector<ImportedSubmodule, 2> PendingImportedModules; 1061 1062 /// A list of modules that were imported by precompiled headers or 1063 /// any other non-module AST file and have not yet been made visible for Sema. 1064 SmallVector<ImportedSubmodule, 2> PendingImportedModulesSema; 1065 //@} 1066 1067 /// The system include root to be used when loading the 1068 /// precompiled header. 1069 std::string isysroot; 1070 1071 /// Whether to disable the normal validation performed on precompiled 1072 /// headers and module files when they are loaded. 1073 DisableValidationForModuleKind DisableValidationKind; 1074 1075 /// Whether to accept an AST file with compiler errors. 1076 bool AllowASTWithCompilerErrors; 1077 1078 /// Whether to accept an AST file that has a different configuration 1079 /// from the current compiler instance. 1080 bool AllowConfigurationMismatch; 1081 1082 /// Whether validate system input files. 1083 bool ValidateSystemInputs; 1084 1085 /// Whether validate headers and module maps using hash based on contents. 1086 bool ValidateASTInputFilesContent; 1087 1088 /// Whether we are allowed to use the global module index. 1089 bool UseGlobalIndex; 1090 1091 /// Whether we have tried loading the global module index yet. 1092 bool TriedLoadingGlobalIndex = false; 1093 1094 ///Whether we are currently processing update records. 1095 bool ProcessingUpdateRecords = false; 1096 1097 using SwitchCaseMapTy = llvm::DenseMap<unsigned, SwitchCase *>; 1098 1099 /// Mapping from switch-case IDs in the chain to switch-case statements 1100 /// 1101 /// Statements usually don't have IDs, but switch cases need them, so that the 1102 /// switch statement can refer to them. 1103 SwitchCaseMapTy SwitchCaseStmts; 1104 1105 SwitchCaseMapTy *CurrSwitchCaseStmts; 1106 1107 /// The number of source location entries de-serialized from 1108 /// the PCH file. 1109 unsigned NumSLocEntriesRead = 0; 1110 1111 /// The number of source location entries in the chain. 1112 unsigned TotalNumSLocEntries = 0; 1113 1114 /// The number of statements (and expressions) de-serialized 1115 /// from the chain. 1116 unsigned NumStatementsRead = 0; 1117 1118 /// The total number of statements (and expressions) stored 1119 /// in the chain. 1120 unsigned TotalNumStatements = 0; 1121 1122 /// The number of macros de-serialized from the chain. 1123 unsigned NumMacrosRead = 0; 1124 1125 /// The total number of macros stored in the chain. 1126 unsigned TotalNumMacros = 0; 1127 1128 /// The number of lookups into identifier tables. 1129 unsigned NumIdentifierLookups = 0; 1130 1131 /// The number of lookups into identifier tables that succeed. 1132 unsigned NumIdentifierLookupHits = 0; 1133 1134 /// The number of selectors that have been read. 1135 unsigned NumSelectorsRead = 0; 1136 1137 /// The number of method pool entries that have been read. 1138 unsigned NumMethodPoolEntriesRead = 0; 1139 1140 /// The number of times we have looked up a selector in the method 1141 /// pool. 1142 unsigned NumMethodPoolLookups = 0; 1143 1144 /// The number of times we have looked up a selector in the method 1145 /// pool and found something. 1146 unsigned NumMethodPoolHits = 0; 1147 1148 /// The number of times we have looked up a selector in the method 1149 /// pool within a specific module. 1150 unsigned NumMethodPoolTableLookups = 0; 1151 1152 /// The number of times we have looked up a selector in the method 1153 /// pool within a specific module and found something. 1154 unsigned NumMethodPoolTableHits = 0; 1155 1156 /// The total number of method pool entries in the selector table. 1157 unsigned TotalNumMethodPoolEntries = 0; 1158 1159 /// Number of lexical decl contexts read/total. 1160 unsigned NumLexicalDeclContextsRead = 0, TotalLexicalDeclContexts = 0; 1161 1162 /// Number of visible decl contexts read/total. 1163 unsigned NumVisibleDeclContextsRead = 0, TotalVisibleDeclContexts = 0; 1164 1165 /// Number of module local visible decl contexts read/total. 1166 unsigned NumModuleLocalVisibleDeclContexts = 0, 1167 TotalModuleLocalVisibleDeclContexts = 0; 1168 1169 /// Number of TU Local decl contexts read/total 1170 unsigned NumTULocalVisibleDeclContexts = 0, 1171 TotalTULocalVisibleDeclContexts = 0; 1172 1173 /// Total size of modules, in bits, currently loaded 1174 uint64_t TotalModulesSizeInBits = 0; 1175 1176 /// Number of Decl/types that are currently deserializing. 1177 unsigned NumCurrentElementsDeserializing = 0; 1178 1179 /// Set true while we are in the process of passing deserialized 1180 /// "interesting" decls to consumer inside FinishedDeserializing(). 1181 /// This is used as a guard to avoid recursively repeating the process of 1182 /// passing decls to consumer. 1183 bool PassingDeclsToConsumer = false; 1184 1185 /// The set of identifiers that were read while the AST reader was 1186 /// (recursively) loading declarations. 1187 /// 1188 /// The declarations on the identifier chain for these identifiers will be 1189 /// loaded once the recursive loading has completed. 1190 llvm::MapVector<IdentifierInfo *, SmallVector<GlobalDeclID, 4>> 1191 PendingIdentifierInfos; 1192 1193 /// The set of lookup results that we have faked in order to support 1194 /// merging of partially deserialized decls but that we have not yet removed. 1195 llvm::SmallMapVector<const IdentifierInfo *, SmallVector<NamedDecl *, 2>, 16> 1196 PendingFakeLookupResults; 1197 1198 /// The generation number of each identifier, which keeps track of 1199 /// the last time we loaded information about this identifier. 1200 llvm::DenseMap<const IdentifierInfo *, unsigned> IdentifierGeneration; 1201 1202 /// Contains declarations and definitions that could be 1203 /// "interesting" to the ASTConsumer, when we get that AST consumer. 1204 /// 1205 /// "Interesting" declarations are those that have data that may 1206 /// need to be emitted, such as inline function definitions or 1207 /// Objective-C protocols. 1208 std::deque<Decl *> PotentiallyInterestingDecls; 1209 1210 /// The list of deduced function types that we have not yet read, because 1211 /// they might contain a deduced return type that refers to a local type 1212 /// declared within the function. 1213 SmallVector<std::pair<FunctionDecl *, serialization::TypeID>, 16> 1214 PendingDeducedFunctionTypes; 1215 1216 /// The list of deduced variable types that we have not yet read, because 1217 /// they might contain a deduced type that refers to a local type declared 1218 /// within the variable. 1219 SmallVector<std::pair<VarDecl *, serialization::TypeID>, 16> 1220 PendingDeducedVarTypes; 1221 1222 /// The list of redeclaration chains that still need to be 1223 /// reconstructed, and the local offset to the corresponding list 1224 /// of redeclarations. 1225 SmallVector<std::pair<Decl *, uint64_t>, 16> PendingDeclChains; 1226 1227 /// The list of canonical declarations whose redeclaration chains 1228 /// need to be marked as incomplete once we're done deserializing things. 1229 SmallVector<Decl *, 16> PendingIncompleteDeclChains; 1230 1231 /// The Decl IDs for the Sema/Lexical DeclContext of a Decl that has 1232 /// been loaded but its DeclContext was not set yet. 1233 struct PendingDeclContextInfo { 1234 Decl *D; 1235 GlobalDeclID SemaDC; 1236 GlobalDeclID LexicalDC; 1237 }; 1238 1239 /// The set of Decls that have been loaded but their DeclContexts are 1240 /// not set yet. 1241 /// 1242 /// The DeclContexts for these Decls will be set once recursive loading has 1243 /// been completed. 1244 std::deque<PendingDeclContextInfo> PendingDeclContextInfos; 1245 1246 template <typename DeclTy> 1247 using DuplicateObjCDecls = std::pair<DeclTy *, DeclTy *>; 1248 1249 /// When resolving duplicate ivars from Objective-C extensions we don't error 1250 /// out immediately but check if can merge identical extensions. Not checking 1251 /// extensions for equality immediately because ivar deserialization isn't 1252 /// over yet at that point. 1253 llvm::SmallMapVector<DuplicateObjCDecls<ObjCCategoryDecl>, 1254 llvm::SmallVector<DuplicateObjCDecls<ObjCIvarDecl>, 4>, 1255 2> 1256 PendingObjCExtensionIvarRedeclarations; 1257 1258 /// Members that have been added to classes, for which the class has not yet 1259 /// been notified. CXXRecordDecl::addedMember will be called for each of 1260 /// these once recursive deserialization is complete. 1261 SmallVector<std::pair<CXXRecordDecl*, Decl*>, 4> PendingAddedClassMembers; 1262 1263 /// The set of NamedDecls that have been loaded, but are members of a 1264 /// context that has been merged into another context where the corresponding 1265 /// declaration is either missing or has not yet been loaded. 1266 /// 1267 /// We will check whether the corresponding declaration is in fact missing 1268 /// once recursing loading has been completed. 1269 llvm::SmallVector<NamedDecl *, 16> PendingOdrMergeChecks; 1270 1271 using DataPointers = 1272 std::pair<CXXRecordDecl *, struct CXXRecordDecl::DefinitionData *>; 1273 using ObjCInterfaceDataPointers = 1274 std::pair<ObjCInterfaceDecl *, 1275 struct ObjCInterfaceDecl::DefinitionData *>; 1276 using ObjCProtocolDataPointers = 1277 std::pair<ObjCProtocolDecl *, struct ObjCProtocolDecl::DefinitionData *>; 1278 1279 /// Record definitions in which we found an ODR violation. 1280 llvm::SmallDenseMap<CXXRecordDecl *, llvm::SmallVector<DataPointers, 2>, 2> 1281 PendingOdrMergeFailures; 1282 1283 /// C/ObjC record definitions in which we found an ODR violation. 1284 llvm::SmallDenseMap<RecordDecl *, llvm::SmallVector<RecordDecl *, 2>, 2> 1285 PendingRecordOdrMergeFailures; 1286 1287 /// Function definitions in which we found an ODR violation. 1288 llvm::SmallDenseMap<FunctionDecl *, llvm::SmallVector<FunctionDecl *, 2>, 2> 1289 PendingFunctionOdrMergeFailures; 1290 1291 /// Enum definitions in which we found an ODR violation. 1292 llvm::SmallDenseMap<EnumDecl *, llvm::SmallVector<EnumDecl *, 2>, 2> 1293 PendingEnumOdrMergeFailures; 1294 1295 /// ObjCInterfaceDecl in which we found an ODR violation. 1296 llvm::SmallDenseMap<ObjCInterfaceDecl *, 1297 llvm::SmallVector<ObjCInterfaceDataPointers, 2>, 2> 1298 PendingObjCInterfaceOdrMergeFailures; 1299 1300 /// ObjCProtocolDecl in which we found an ODR violation. 1301 llvm::SmallDenseMap<ObjCProtocolDecl *, 1302 llvm::SmallVector<ObjCProtocolDataPointers, 2>, 2> 1303 PendingObjCProtocolOdrMergeFailures; 1304 1305 /// DeclContexts in which we have diagnosed an ODR violation. 1306 llvm::SmallPtrSet<DeclContext*, 2> DiagnosedOdrMergeFailures; 1307 1308 /// The set of Objective-C categories that have been deserialized 1309 /// since the last time the declaration chains were linked. 1310 llvm::SmallPtrSet<ObjCCategoryDecl *, 16> CategoriesDeserialized; 1311 1312 /// The set of Objective-C class definitions that have already been 1313 /// loaded, for which we will need to check for categories whenever a new 1314 /// module is loaded. 1315 SmallVector<ObjCInterfaceDecl *, 16> ObjCClassesLoaded; 1316 1317 using KeyDeclsMap = llvm::DenseMap<Decl *, SmallVector<GlobalDeclID, 2>>; 1318 1319 /// A mapping from canonical declarations to the set of global 1320 /// declaration IDs for key declaration that have been merged with that 1321 /// canonical declaration. A key declaration is a formerly-canonical 1322 /// declaration whose module did not import any other key declaration for that 1323 /// entity. These are the IDs that we use as keys when finding redecl chains. 1324 KeyDeclsMap KeyDecls; 1325 1326 /// A mapping from DeclContexts to the semantic DeclContext that we 1327 /// are treating as the definition of the entity. This is used, for instance, 1328 /// when merging implicit instantiations of class templates across modules. 1329 llvm::DenseMap<DeclContext *, DeclContext *> MergedDeclContexts; 1330 1331 /// A mapping from canonical declarations of enums to their canonical 1332 /// definitions. Only populated when using modules in C++. 1333 llvm::DenseMap<EnumDecl *, EnumDecl *> EnumDefinitions; 1334 1335 /// A mapping from canonical declarations of records to their canonical 1336 /// definitions. Doesn't cover CXXRecordDecl. 1337 llvm::DenseMap<RecordDecl *, RecordDecl *> RecordDefinitions; 1338 1339 /// When reading a Stmt tree, Stmt operands are placed in this stack. 1340 SmallVector<Stmt *, 16> StmtStack; 1341 1342 /// What kind of records we are reading. 1343 enum ReadingKind { 1344 Read_None, Read_Decl, Read_Type, Read_Stmt 1345 }; 1346 1347 /// What kind of records we are reading. 1348 ReadingKind ReadingKind = Read_None; 1349 1350 /// RAII object to change the reading kind. 1351 class ReadingKindTracker { 1352 ASTReader &Reader; 1353 enum ReadingKind PrevKind; 1354 1355 public: 1356 ReadingKindTracker(enum ReadingKind newKind, ASTReader &reader) 1357 : Reader(reader), PrevKind(Reader.ReadingKind) { 1358 Reader.ReadingKind = newKind; 1359 } 1360 1361 ReadingKindTracker(const ReadingKindTracker &) = delete; 1362 ReadingKindTracker &operator=(const ReadingKindTracker &) = delete; 1363 ~ReadingKindTracker() { Reader.ReadingKind = PrevKind; } 1364 }; 1365 1366 /// RAII object to mark the start of processing updates. 1367 class ProcessingUpdatesRAIIObj { 1368 ASTReader &Reader; 1369 bool PrevState; 1370 1371 public: 1372 ProcessingUpdatesRAIIObj(ASTReader &reader) 1373 : Reader(reader), PrevState(Reader.ProcessingUpdateRecords) { 1374 Reader.ProcessingUpdateRecords = true; 1375 } 1376 1377 ProcessingUpdatesRAIIObj(const ProcessingUpdatesRAIIObj &) = delete; 1378 ProcessingUpdatesRAIIObj & 1379 operator=(const ProcessingUpdatesRAIIObj &) = delete; 1380 ~ProcessingUpdatesRAIIObj() { Reader.ProcessingUpdateRecords = PrevState; } 1381 }; 1382 1383 /// Suggested contents of the predefines buffer, after this 1384 /// PCH file has been processed. 1385 /// 1386 /// In most cases, this string will be empty, because the predefines 1387 /// buffer computed to build the PCH file will be identical to the 1388 /// predefines buffer computed from the command line. However, when 1389 /// there are differences that the PCH reader can work around, this 1390 /// predefines buffer may contain additional definitions. 1391 std::string SuggestedPredefines; 1392 1393 llvm::DenseMap<const Decl *, bool> DefinitionSource; 1394 1395 bool shouldDisableValidationForFile(const serialization::ModuleFile &M) const; 1396 1397 /// Reads a statement from the specified cursor. 1398 Stmt *ReadStmtFromStream(ModuleFile &F); 1399 1400 /// Retrieve the stored information about an input file. 1401 serialization::InputFileInfo getInputFileInfo(ModuleFile &F, unsigned ID); 1402 1403 /// Retrieve the file entry and 'overridden' bit for an input 1404 /// file in the given module file. 1405 serialization::InputFile getInputFile(ModuleFile &F, unsigned ID, 1406 bool Complain = true); 1407 1408 /// The buffer used as the temporary backing storage for resolved paths. 1409 SmallString<0> PathBuf; 1410 1411 /// A wrapper around StringRef that temporarily borrows the underlying buffer. 1412 class TemporarilyOwnedStringRef { 1413 StringRef String; 1414 llvm::SaveAndRestore<SmallString<0>> UnderlyingBuffer; 1415 1416 public: 1417 TemporarilyOwnedStringRef(StringRef S, SmallString<0> &UnderlyingBuffer) 1418 : String(S), UnderlyingBuffer(UnderlyingBuffer, {}) {} 1419 1420 /// Return the wrapped \c StringRef that must be outlived by \c this. 1421 const StringRef *operator->() const & { return &String; } 1422 const StringRef &operator*() const & { return String; } 1423 1424 /// Make it harder to get a \c StringRef that outlives \c this. 1425 const StringRef *operator->() && = delete; 1426 const StringRef &operator*() && = delete; 1427 }; 1428 1429 public: 1430 /// Get the buffer for resolving paths. 1431 SmallString<0> &getPathBuf() { return PathBuf; } 1432 1433 /// Resolve \c Path in the context of module file \c M. The return value 1434 /// must go out of scope before the next call to \c ResolveImportedPath. 1435 static TemporarilyOwnedStringRef 1436 ResolveImportedPath(SmallString<0> &Buf, StringRef Path, ModuleFile &ModF); 1437 /// Resolve \c Path in the context of the \c Prefix directory. The return 1438 /// value must go out of scope before the next call to \c ResolveImportedPath. 1439 static TemporarilyOwnedStringRef 1440 ResolveImportedPath(SmallString<0> &Buf, StringRef Path, StringRef Prefix); 1441 1442 /// Resolve \c Path in the context of module file \c M. 1443 static std::string ResolveImportedPathAndAllocate(SmallString<0> &Buf, 1444 StringRef Path, 1445 ModuleFile &ModF); 1446 /// Resolve \c Path in the context of the \c Prefix directory. 1447 static std::string ResolveImportedPathAndAllocate(SmallString<0> &Buf, 1448 StringRef Path, 1449 StringRef Prefix); 1450 1451 /// Returns the first key declaration for the given declaration. This 1452 /// is one that is formerly-canonical (or still canonical) and whose module 1453 /// did not import any other key declaration of the entity. 1454 Decl *getKeyDeclaration(Decl *D) { 1455 D = D->getCanonicalDecl(); 1456 if (D->isFromASTFile()) 1457 return D; 1458 1459 auto I = KeyDecls.find(D); 1460 if (I == KeyDecls.end() || I->second.empty()) 1461 return D; 1462 return GetExistingDecl(I->second[0]); 1463 } 1464 const Decl *getKeyDeclaration(const Decl *D) { 1465 return getKeyDeclaration(const_cast<Decl*>(D)); 1466 } 1467 1468 /// Run a callback on each imported key declaration of \p D. 1469 template <typename Fn> 1470 void forEachImportedKeyDecl(const Decl *D, Fn Visit) { 1471 D = D->getCanonicalDecl(); 1472 if (D->isFromASTFile()) 1473 Visit(D); 1474 1475 auto It = KeyDecls.find(const_cast<Decl*>(D)); 1476 if (It != KeyDecls.end()) 1477 for (auto ID : It->second) 1478 Visit(GetExistingDecl(ID)); 1479 } 1480 1481 /// Get the loaded lookup tables for \p Primary, if any. 1482 const serialization::reader::DeclContextLookupTable * 1483 getLoadedLookupTables(DeclContext *Primary) const; 1484 1485 const serialization::reader::ModuleLocalLookupTable * 1486 getModuleLocalLookupTables(DeclContext *Primary) const; 1487 1488 const serialization::reader::DeclContextLookupTable * 1489 getTULocalLookupTables(DeclContext *Primary) const; 1490 1491 /// Get the loaded specializations lookup tables for \p D, 1492 /// if any. 1493 serialization::reader::LazySpecializationInfoLookupTable * 1494 getLoadedSpecializationsLookupTables(const Decl *D, bool IsPartial); 1495 1496 /// If we have any unloaded specialization for \p D 1497 bool haveUnloadedSpecializations(const Decl *D) const; 1498 1499 private: 1500 struct ImportedModule { 1501 ModuleFile *Mod; 1502 ModuleFile *ImportedBy; 1503 SourceLocation ImportLoc; 1504 1505 ImportedModule(ModuleFile *Mod, 1506 ModuleFile *ImportedBy, 1507 SourceLocation ImportLoc) 1508 : Mod(Mod), ImportedBy(ImportedBy), ImportLoc(ImportLoc) {} 1509 }; 1510 1511 ASTReadResult ReadASTCore(StringRef FileName, ModuleKind Type, 1512 SourceLocation ImportLoc, ModuleFile *ImportedBy, 1513 SmallVectorImpl<ImportedModule> &Loaded, 1514 off_t ExpectedSize, time_t ExpectedModTime, 1515 ASTFileSignature ExpectedSignature, 1516 unsigned ClientLoadCapabilities); 1517 ASTReadResult ReadControlBlock(ModuleFile &F, 1518 SmallVectorImpl<ImportedModule> &Loaded, 1519 const ModuleFile *ImportedBy, 1520 unsigned ClientLoadCapabilities); 1521 static ASTReadResult 1522 ReadOptionsBlock(llvm::BitstreamCursor &Stream, StringRef Filename, 1523 unsigned ClientLoadCapabilities, 1524 bool AllowCompatibleConfigurationMismatch, 1525 ASTReaderListener &Listener, 1526 std::string &SuggestedPredefines); 1527 1528 /// Read the unhashed control block. 1529 /// 1530 /// This has no effect on \c F.Stream, instead creating a fresh cursor from 1531 /// \c F.Data and reading ahead. 1532 ASTReadResult readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy, 1533 unsigned ClientLoadCapabilities); 1534 1535 static ASTReadResult readUnhashedControlBlockImpl( 1536 ModuleFile *F, llvm::StringRef StreamData, StringRef Filename, 1537 unsigned ClientLoadCapabilities, 1538 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener, 1539 bool ValidateDiagnosticOptions); 1540 1541 llvm::Error ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities); 1542 llvm::Error ReadExtensionBlock(ModuleFile &F); 1543 void ReadModuleOffsetMap(ModuleFile &F) const; 1544 void ParseLineTable(ModuleFile &F, const RecordData &Record); 1545 llvm::Error ReadSourceManagerBlock(ModuleFile &F); 1546 SourceLocation getImportLocation(ModuleFile *F); 1547 ASTReadResult ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F, 1548 const ModuleFile *ImportedBy, 1549 unsigned ClientLoadCapabilities); 1550 llvm::Error ReadSubmoduleBlock(ModuleFile &F, 1551 unsigned ClientLoadCapabilities); 1552 static bool ParseLanguageOptions(const RecordData &Record, 1553 StringRef ModuleFilename, bool Complain, 1554 ASTReaderListener &Listener, 1555 bool AllowCompatibleDifferences); 1556 static bool ParseTargetOptions(const RecordData &Record, 1557 StringRef ModuleFilename, bool Complain, 1558 ASTReaderListener &Listener, 1559 bool AllowCompatibleDifferences); 1560 static bool ParseDiagnosticOptions(const RecordData &Record, 1561 StringRef ModuleFilename, bool Complain, 1562 ASTReaderListener &Listener); 1563 static bool ParseFileSystemOptions(const RecordData &Record, bool Complain, 1564 ASTReaderListener &Listener); 1565 static bool ParseHeaderSearchOptions(const RecordData &Record, 1566 StringRef ModuleFilename, bool Complain, 1567 ASTReaderListener &Listener); 1568 static bool ParseHeaderSearchPaths(const RecordData &Record, bool Complain, 1569 ASTReaderListener &Listener); 1570 static bool ParsePreprocessorOptions(const RecordData &Record, 1571 StringRef ModuleFilename, bool Complain, 1572 ASTReaderListener &Listener, 1573 std::string &SuggestedPredefines); 1574 1575 struct RecordLocation { 1576 ModuleFile *F; 1577 uint64_t Offset; 1578 1579 RecordLocation(ModuleFile *M, uint64_t O) : F(M), Offset(O) {} 1580 }; 1581 1582 QualType readTypeRecord(serialization::TypeID ID); 1583 RecordLocation TypeCursorForIndex(serialization::TypeID ID); 1584 void LoadedDecl(unsigned Index, Decl *D); 1585 Decl *ReadDeclRecord(GlobalDeclID ID); 1586 void markIncompleteDeclChain(Decl *D); 1587 1588 /// Returns the most recent declaration of a declaration (which must be 1589 /// of a redeclarable kind) that is either local or has already been loaded 1590 /// merged into its redecl chain. 1591 Decl *getMostRecentExistingDecl(Decl *D); 1592 1593 RecordLocation DeclCursorForID(GlobalDeclID ID, SourceLocation &Location); 1594 void loadDeclUpdateRecords(PendingUpdateRecord &Record); 1595 void loadPendingDeclChain(Decl *D, uint64_t LocalOffset); 1596 void loadObjCCategories(GlobalDeclID ID, ObjCInterfaceDecl *D, 1597 unsigned PreviousGeneration = 0); 1598 1599 RecordLocation getLocalBitOffset(uint64_t GlobalOffset); 1600 uint64_t getGlobalBitOffset(ModuleFile &M, uint64_t LocalOffset); 1601 1602 /// Returns the first preprocessed entity ID that begins or ends after 1603 /// \arg Loc. 1604 serialization::PreprocessedEntityID 1605 findPreprocessedEntity(SourceLocation Loc, bool EndsAfter) const; 1606 1607 /// Find the next module that contains entities and return the ID 1608 /// of the first entry. 1609 /// 1610 /// \param SLocMapI points at a chunk of a module that contains no 1611 /// preprocessed entities or the entities it contains are not the 1612 /// ones we are looking for. 1613 serialization::PreprocessedEntityID 1614 findNextPreprocessedEntity( 1615 GlobalSLocOffsetMapType::const_iterator SLocMapI) const; 1616 1617 /// Returns (ModuleFile, Local index) pair for \p GlobalIndex of a 1618 /// preprocessed entity. 1619 std::pair<ModuleFile *, unsigned> 1620 getModulePreprocessedEntity(unsigned GlobalIndex); 1621 1622 /// Returns (begin, end) pair for the preprocessed entities of a 1623 /// particular module. 1624 llvm::iterator_range<PreprocessingRecord::iterator> 1625 getModulePreprocessedEntities(ModuleFile &Mod) const; 1626 1627 bool canRecoverFromOutOfDate(StringRef ModuleFileName, 1628 unsigned ClientLoadCapabilities); 1629 1630 public: 1631 class ModuleDeclIterator 1632 : public llvm::iterator_adaptor_base< 1633 ModuleDeclIterator, const serialization::unaligned_decl_id_t *, 1634 std::random_access_iterator_tag, const Decl *, ptrdiff_t, 1635 const Decl *, const Decl *> { 1636 ASTReader *Reader = nullptr; 1637 ModuleFile *Mod = nullptr; 1638 1639 public: 1640 ModuleDeclIterator() : iterator_adaptor_base(nullptr) {} 1641 1642 ModuleDeclIterator(ASTReader *Reader, ModuleFile *Mod, 1643 const serialization::unaligned_decl_id_t *Pos) 1644 : iterator_adaptor_base(Pos), Reader(Reader), Mod(Mod) {} 1645 1646 value_type operator*() const { 1647 LocalDeclID ID = LocalDeclID::get(*Reader, *Mod, *I); 1648 return Reader->GetDecl(Reader->getGlobalDeclID(*Mod, ID)); 1649 } 1650 1651 value_type operator->() const { return **this; } 1652 1653 bool operator==(const ModuleDeclIterator &RHS) const { 1654 assert(Reader == RHS.Reader && Mod == RHS.Mod); 1655 return I == RHS.I; 1656 } 1657 }; 1658 1659 llvm::iterator_range<ModuleDeclIterator> 1660 getModuleFileLevelDecls(ModuleFile &Mod); 1661 1662 private: 1663 bool isConsumerInterestedIn(Decl *D); 1664 void PassInterestingDeclsToConsumer(); 1665 void PassInterestingDeclToConsumer(Decl *D); 1666 void PassVTableToConsumer(CXXRecordDecl *RD); 1667 1668 void finishPendingActions(); 1669 void diagnoseOdrViolations(); 1670 1671 void pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name); 1672 1673 void addPendingDeclContextInfo(Decl *D, GlobalDeclID SemaDC, 1674 GlobalDeclID LexicalDC) { 1675 assert(D); 1676 PendingDeclContextInfo Info = { D, SemaDC, LexicalDC }; 1677 PendingDeclContextInfos.push_back(Info); 1678 } 1679 1680 /// Produce an error diagnostic and return true. 1681 /// 1682 /// This routine should only be used for fatal errors that have to 1683 /// do with non-routine failures (e.g., corrupted AST file). 1684 void Error(StringRef Msg) const; 1685 void Error(unsigned DiagID, StringRef Arg1 = StringRef(), 1686 StringRef Arg2 = StringRef(), StringRef Arg3 = StringRef()) const; 1687 void Error(llvm::Error &&Err) const; 1688 1689 /// Translate a \param GlobalDeclID to the index of DeclsLoaded array. 1690 unsigned translateGlobalDeclIDToIndex(GlobalDeclID ID) const; 1691 1692 /// Translate an \param IdentifierID ID to the index of IdentifiersLoaded 1693 /// array and the corresponding module file. 1694 std::pair<ModuleFile *, unsigned> 1695 translateIdentifierIDToIndex(serialization::IdentifierID ID) const; 1696 1697 /// Translate an \param TypeID ID to the index of TypesLoaded 1698 /// array and the corresponding module file. 1699 std::pair<ModuleFile *, unsigned> 1700 translateTypeIDToIndex(serialization::TypeID ID) const; 1701 1702 /// Get a predefined Decl from ASTContext. 1703 Decl *getPredefinedDecl(PredefinedDeclIDs ID); 1704 1705 public: 1706 /// Load the AST file and validate its contents against the given 1707 /// Preprocessor. 1708 /// 1709 /// \param PP the preprocessor associated with the context in which this 1710 /// precompiled header will be loaded. 1711 /// 1712 /// \param Context the AST context that this precompiled header will be 1713 /// loaded into, if any. 1714 /// 1715 /// \param PCHContainerRdr the PCHContainerOperations to use for loading and 1716 /// creating modules. 1717 /// 1718 /// \param Extensions the list of module file extensions that can be loaded 1719 /// from the AST files. 1720 /// 1721 /// \param isysroot If non-NULL, the system include path specified by the 1722 /// user. This is only used with relocatable PCH files. If non-NULL, 1723 /// a relocatable PCH file will use the default path "/". 1724 /// 1725 /// \param DisableValidationKind If set, the AST reader will suppress most 1726 /// of its regular consistency checking, allowing the use of precompiled 1727 /// headers and module files that cannot be determined to be compatible. 1728 /// 1729 /// \param AllowASTWithCompilerErrors If true, the AST reader will accept an 1730 /// AST file the was created out of an AST with compiler errors, 1731 /// otherwise it will reject it. 1732 /// 1733 /// \param AllowConfigurationMismatch If true, the AST reader will not check 1734 /// for configuration differences between the AST file and the invocation. 1735 /// 1736 /// \param ValidateSystemInputs If true, the AST reader will validate 1737 /// system input files in addition to user input files. This is only 1738 /// meaningful if \p DisableValidation is false. 1739 /// 1740 /// \param UseGlobalIndex If true, the AST reader will try to load and use 1741 /// the global module index. 1742 /// 1743 /// \param ReadTimer If non-null, a timer used to track the time spent 1744 /// deserializing. 1745 ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache, 1746 ASTContext *Context, const PCHContainerReader &PCHContainerRdr, 1747 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, 1748 StringRef isysroot = "", 1749 DisableValidationForModuleKind DisableValidationKind = 1750 DisableValidationForModuleKind::None, 1751 bool AllowASTWithCompilerErrors = false, 1752 bool AllowConfigurationMismatch = false, 1753 bool ValidateSystemInputs = false, 1754 bool ValidateASTInputFilesContent = false, 1755 bool UseGlobalIndex = true, 1756 std::unique_ptr<llvm::Timer> ReadTimer = {}); 1757 ASTReader(const ASTReader &) = delete; 1758 ASTReader &operator=(const ASTReader &) = delete; 1759 ~ASTReader() override; 1760 1761 SourceManager &getSourceManager() const { return SourceMgr; } 1762 FileManager &getFileManager() const { return FileMgr; } 1763 DiagnosticsEngine &getDiags() const { return Diags; } 1764 1765 /// Flags that indicate what kind of AST loading failures the client 1766 /// of the AST reader can directly handle. 1767 /// 1768 /// When a client states that it can handle a particular kind of failure, 1769 /// the AST reader will not emit errors when producing that kind of failure. 1770 enum LoadFailureCapabilities { 1771 /// The client can't handle any AST loading failures. 1772 ARR_None = 0, 1773 1774 /// The client can handle an AST file that cannot load because it 1775 /// is missing. 1776 ARR_Missing = 0x1, 1777 1778 /// The client can handle an AST file that cannot load because it 1779 /// is out-of-date relative to its input files. 1780 ARR_OutOfDate = 0x2, 1781 1782 /// The client can handle an AST file that cannot load because it 1783 /// was built with a different version of Clang. 1784 ARR_VersionMismatch = 0x4, 1785 1786 /// The client can handle an AST file that cannot load because it's 1787 /// compiled configuration doesn't match that of the context it was 1788 /// loaded into. 1789 ARR_ConfigurationMismatch = 0x8, 1790 1791 /// If a module file is marked with errors treat it as out-of-date so the 1792 /// caller can rebuild it. 1793 ARR_TreatModuleWithErrorsAsOutOfDate = 0x10 1794 }; 1795 1796 /// Load the AST file designated by the given file name. 1797 /// 1798 /// \param FileName The name of the AST file to load. 1799 /// 1800 /// \param Type The kind of AST being loaded, e.g., PCH, module, main file, 1801 /// or preamble. 1802 /// 1803 /// \param ImportLoc the location where the module file will be considered as 1804 /// imported from. For non-module AST types it should be invalid. 1805 /// 1806 /// \param ClientLoadCapabilities The set of client load-failure 1807 /// capabilities, represented as a bitset of the enumerators of 1808 /// LoadFailureCapabilities. 1809 /// 1810 /// \param LoadedModuleFile The optional out-parameter refers to the new 1811 /// loaded modules. In case the module specified by FileName is already 1812 /// loaded, the module file pointer referred by NewLoadedModuleFile wouldn't 1813 /// change. Otherwise if the AST file get loaded successfully, 1814 /// NewLoadedModuleFile would refer to the address of the new loaded top level 1815 /// module. The state of NewLoadedModuleFile is unspecified if the AST file 1816 /// isn't loaded successfully. 1817 ASTReadResult ReadAST(StringRef FileName, ModuleKind Type, 1818 SourceLocation ImportLoc, 1819 unsigned ClientLoadCapabilities, 1820 ModuleFile **NewLoadedModuleFile = nullptr); 1821 1822 /// Make the entities in the given module and any of its (non-explicit) 1823 /// submodules visible to name lookup. 1824 /// 1825 /// \param Mod The module whose names should be made visible. 1826 /// 1827 /// \param NameVisibility The level of visibility to give the names in the 1828 /// module. Visibility can only be increased over time. 1829 /// 1830 /// \param ImportLoc The location at which the import occurs. 1831 void makeModuleVisible(Module *Mod, 1832 Module::NameVisibilityKind NameVisibility, 1833 SourceLocation ImportLoc); 1834 1835 /// Make the names within this set of hidden names visible. 1836 void makeNamesVisible(const HiddenNames &Names, Module *Owner); 1837 1838 /// Note that MergedDef is a redefinition of the canonical definition 1839 /// Def, so Def should be visible whenever MergedDef is. 1840 void mergeDefinitionVisibility(NamedDecl *Def, NamedDecl *MergedDef); 1841 1842 /// Take the AST callbacks listener. 1843 std::unique_ptr<ASTReaderListener> takeListener() { 1844 return std::move(Listener); 1845 } 1846 1847 /// Set the AST callbacks listener. 1848 void setListener(std::unique_ptr<ASTReaderListener> Listener) { 1849 this->Listener = std::move(Listener); 1850 } 1851 1852 /// Add an AST callback listener. 1853 /// 1854 /// Takes ownership of \p L. 1855 void addListener(std::unique_ptr<ASTReaderListener> L) { 1856 if (Listener) 1857 L = std::make_unique<ChainedASTReaderListener>(std::move(L), 1858 std::move(Listener)); 1859 Listener = std::move(L); 1860 } 1861 1862 /// RAII object to temporarily add an AST callback listener. 1863 class ListenerScope { 1864 ASTReader &Reader; 1865 bool Chained = false; 1866 1867 public: 1868 ListenerScope(ASTReader &Reader, std::unique_ptr<ASTReaderListener> L) 1869 : Reader(Reader) { 1870 auto Old = Reader.takeListener(); 1871 if (Old) { 1872 Chained = true; 1873 L = std::make_unique<ChainedASTReaderListener>(std::move(L), 1874 std::move(Old)); 1875 } 1876 Reader.setListener(std::move(L)); 1877 } 1878 1879 ~ListenerScope() { 1880 auto New = Reader.takeListener(); 1881 if (Chained) 1882 Reader.setListener(static_cast<ChainedASTReaderListener *>(New.get()) 1883 ->takeSecond()); 1884 } 1885 }; 1886 1887 /// Set the AST deserialization listener. 1888 void setDeserializationListener(ASTDeserializationListener *Listener, 1889 bool TakeOwnership = false); 1890 1891 /// Get the AST deserialization listener. 1892 ASTDeserializationListener *getDeserializationListener() { 1893 return DeserializationListener; 1894 } 1895 1896 /// Determine whether this AST reader has a global index. 1897 bool hasGlobalIndex() const { return (bool)GlobalIndex; } 1898 1899 /// Return global module index. 1900 GlobalModuleIndex *getGlobalIndex() { return GlobalIndex.get(); } 1901 1902 /// Reset reader for a reload try. 1903 void resetForReload() { TriedLoadingGlobalIndex = false; } 1904 1905 /// Attempts to load the global index. 1906 /// 1907 /// \returns true if loading the global index has failed for any reason. 1908 bool loadGlobalIndex(); 1909 1910 /// Determine whether we tried to load the global index, but failed, 1911 /// e.g., because it is out-of-date or does not exist. 1912 bool isGlobalIndexUnavailable() const; 1913 1914 /// Initializes the ASTContext 1915 void InitializeContext(); 1916 1917 /// Update the state of Sema after loading some additional modules. 1918 void UpdateSema(); 1919 1920 /// Add in-memory (virtual file) buffer. 1921 void addInMemoryBuffer(StringRef &FileName, 1922 std::unique_ptr<llvm::MemoryBuffer> Buffer) { 1923 ModuleMgr.addInMemoryBuffer(FileName, std::move(Buffer)); 1924 } 1925 1926 /// Finalizes the AST reader's state before writing an AST file to 1927 /// disk. 1928 /// 1929 /// This operation may undo temporary state in the AST that should not be 1930 /// emitted. 1931 void finalizeForWriting(); 1932 1933 /// Retrieve the module manager. 1934 ModuleManager &getModuleManager() { return ModuleMgr; } 1935 const ModuleManager &getModuleManager() const { return ModuleMgr; } 1936 1937 /// Retrieve the preprocessor. 1938 Preprocessor &getPreprocessor() const { return PP; } 1939 1940 /// Retrieve the name of the original source file name for the primary 1941 /// module file. 1942 StringRef getOriginalSourceFile() { 1943 return ModuleMgr.getPrimaryModule().OriginalSourceFileName; 1944 } 1945 1946 /// Retrieve the name of the original source file name directly from 1947 /// the AST file, without actually loading the AST file. 1948 static std::string 1949 getOriginalSourceFile(const std::string &ASTFileName, FileManager &FileMgr, 1950 const PCHContainerReader &PCHContainerRdr, 1951 DiagnosticsEngine &Diags); 1952 1953 /// Read the control block for the named AST file. 1954 /// 1955 /// \returns true if an error occurred, false otherwise. 1956 static bool readASTFileControlBlock( 1957 StringRef Filename, FileManager &FileMgr, 1958 const InMemoryModuleCache &ModuleCache, 1959 const PCHContainerReader &PCHContainerRdr, bool FindModuleFileExtensions, 1960 ASTReaderListener &Listener, bool ValidateDiagnosticOptions, 1961 unsigned ClientLoadCapabilities = ARR_ConfigurationMismatch | 1962 ARR_OutOfDate); 1963 1964 /// Determine whether the given AST file is acceptable to load into a 1965 /// translation unit with the given language and target options. 1966 static bool isAcceptableASTFile(StringRef Filename, FileManager &FileMgr, 1967 const InMemoryModuleCache &ModuleCache, 1968 const PCHContainerReader &PCHContainerRdr, 1969 const LangOptions &LangOpts, 1970 const TargetOptions &TargetOpts, 1971 const PreprocessorOptions &PPOpts, 1972 StringRef ExistingModuleCachePath, 1973 bool RequireStrictOptionMatches = false); 1974 1975 /// Returns the suggested contents of the predefines buffer, 1976 /// which contains a (typically-empty) subset of the predefines 1977 /// build prior to including the precompiled header. 1978 const std::string &getSuggestedPredefines() { return SuggestedPredefines; } 1979 1980 /// Read a preallocated preprocessed entity from the external source. 1981 /// 1982 /// \returns null if an error occurred that prevented the preprocessed 1983 /// entity from being loaded. 1984 PreprocessedEntity *ReadPreprocessedEntity(unsigned Index) override; 1985 1986 /// Returns a pair of [Begin, End) indices of preallocated 1987 /// preprocessed entities that \p Range encompasses. 1988 std::pair<unsigned, unsigned> 1989 findPreprocessedEntitiesInRange(SourceRange Range) override; 1990 1991 /// Optionally returns true or false if the preallocated preprocessed 1992 /// entity with index \p Index came from file \p FID. 1993 std::optional<bool> isPreprocessedEntityInFileID(unsigned Index, 1994 FileID FID) override; 1995 1996 /// Read a preallocated skipped range from the external source. 1997 SourceRange ReadSkippedRange(unsigned Index) override; 1998 1999 /// Read the header file information for the given file entry. 2000 HeaderFileInfo GetHeaderFileInfo(FileEntryRef FE) override; 2001 2002 void ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag); 2003 2004 /// Returns the number of source locations found in the chain. 2005 unsigned getTotalNumSLocs() const { 2006 return TotalNumSLocEntries; 2007 } 2008 2009 /// Returns the number of identifiers found in the chain. 2010 unsigned getTotalNumIdentifiers() const { 2011 return static_cast<unsigned>(IdentifiersLoaded.size()); 2012 } 2013 2014 /// Returns the number of macros found in the chain. 2015 unsigned getTotalNumMacros() const { 2016 return static_cast<unsigned>(MacrosLoaded.size()); 2017 } 2018 2019 /// Returns the number of types found in the chain. 2020 unsigned getTotalNumTypes() const { 2021 return static_cast<unsigned>(TypesLoaded.size()); 2022 } 2023 2024 /// Returns the number of declarations found in the chain. 2025 unsigned getTotalNumDecls() const { 2026 return static_cast<unsigned>(DeclsLoaded.size()); 2027 } 2028 2029 /// Returns the number of submodules known. 2030 unsigned getTotalNumSubmodules() const { 2031 return static_cast<unsigned>(SubmodulesLoaded.size()); 2032 } 2033 2034 /// Returns the number of selectors found in the chain. 2035 unsigned getTotalNumSelectors() const { 2036 return static_cast<unsigned>(SelectorsLoaded.size()); 2037 } 2038 2039 /// Returns the number of preprocessed entities known to the AST 2040 /// reader. 2041 unsigned getTotalNumPreprocessedEntities() const { 2042 unsigned Result = 0; 2043 for (const auto &M : ModuleMgr) 2044 Result += M.NumPreprocessedEntities; 2045 return Result; 2046 } 2047 2048 /// Resolve a type ID into a type, potentially building a new 2049 /// type. 2050 QualType GetType(serialization::TypeID ID); 2051 2052 /// Resolve a local type ID within a given AST file into a type. 2053 QualType getLocalType(ModuleFile &F, serialization::LocalTypeID LocalID); 2054 2055 /// Map a local type ID within a given AST file into a global type ID. 2056 serialization::TypeID 2057 getGlobalTypeID(ModuleFile &F, serialization::LocalTypeID LocalID) const; 2058 2059 /// Read a type from the current position in the given record, which 2060 /// was read from the given AST file. 2061 QualType readType(ModuleFile &F, const RecordData &Record, unsigned &Idx) { 2062 if (Idx >= Record.size()) 2063 return {}; 2064 2065 return getLocalType(F, Record[Idx++]); 2066 } 2067 2068 /// Map from a local declaration ID within a given module to a 2069 /// global declaration ID. 2070 GlobalDeclID getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const; 2071 2072 /// Returns true if global DeclID \p ID originated from module \p M. 2073 bool isDeclIDFromModule(GlobalDeclID ID, ModuleFile &M) const; 2074 2075 /// Retrieve the module file that owns the given declaration, or NULL 2076 /// if the declaration is not from a module file. 2077 ModuleFile *getOwningModuleFile(const Decl *D) const; 2078 ModuleFile *getOwningModuleFile(GlobalDeclID ID) const; 2079 2080 /// Returns the source location for the decl \p ID. 2081 SourceLocation getSourceLocationForDeclID(GlobalDeclID ID); 2082 2083 /// Resolve a declaration ID into a declaration, potentially 2084 /// building a new declaration. 2085 Decl *GetDecl(GlobalDeclID ID); 2086 Decl *GetExternalDecl(GlobalDeclID ID) override; 2087 2088 /// Resolve a declaration ID into a declaration. Return 0 if it's not 2089 /// been loaded yet. 2090 Decl *GetExistingDecl(GlobalDeclID ID); 2091 2092 /// Reads a declaration with the given local ID in the given module. 2093 Decl *GetLocalDecl(ModuleFile &F, LocalDeclID LocalID) { 2094 return GetDecl(getGlobalDeclID(F, LocalID)); 2095 } 2096 2097 /// Reads a declaration with the given local ID in the given module. 2098 /// 2099 /// \returns The requested declaration, casted to the given return type. 2100 template <typename T> T *GetLocalDeclAs(ModuleFile &F, LocalDeclID LocalID) { 2101 return cast_or_null<T>(GetLocalDecl(F, LocalID)); 2102 } 2103 2104 /// Map a global declaration ID into the declaration ID used to 2105 /// refer to this declaration within the given module fule. 2106 /// 2107 /// \returns the global ID of the given declaration as known in the given 2108 /// module file. 2109 LocalDeclID mapGlobalIDToModuleFileGlobalID(ModuleFile &M, 2110 GlobalDeclID GlobalID); 2111 2112 /// Reads a declaration ID from the given position in a record in the 2113 /// given module. 2114 /// 2115 /// \returns The declaration ID read from the record, adjusted to a global ID. 2116 GlobalDeclID ReadDeclID(ModuleFile &F, const RecordDataImpl &Record, 2117 unsigned &Idx); 2118 2119 /// Reads a declaration from the given position in a record in the 2120 /// given module. 2121 Decl *ReadDecl(ModuleFile &F, const RecordDataImpl &R, unsigned &I) { 2122 return GetDecl(ReadDeclID(F, R, I)); 2123 } 2124 2125 /// Reads a declaration from the given position in a record in the 2126 /// given module. 2127 /// 2128 /// \returns The declaration read from this location, casted to the given 2129 /// result type. 2130 template <typename T> 2131 T *ReadDeclAs(ModuleFile &F, const RecordDataImpl &R, unsigned &I) { 2132 return cast_or_null<T>(GetDecl(ReadDeclID(F, R, I))); 2133 } 2134 2135 /// If any redeclarations of \p D have been imported since it was 2136 /// last checked, this digs out those redeclarations and adds them to the 2137 /// redeclaration chain for \p D. 2138 void CompleteRedeclChain(const Decl *D) override; 2139 2140 CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset) override; 2141 2142 /// Resolve the offset of a statement into a statement. 2143 /// 2144 /// This operation will read a new statement from the external 2145 /// source each time it is called, and is meant to be used via a 2146 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 2147 Stmt *GetExternalDeclStmt(uint64_t Offset) override; 2148 2149 /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the 2150 /// specified cursor. Read the abbreviations that are at the top of the block 2151 /// and then leave the cursor pointing into the block. 2152 static llvm::Error ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor, 2153 unsigned BlockID, 2154 uint64_t *StartOfBlockOffset = nullptr); 2155 2156 bool LoadExternalSpecializations(const Decl *D, bool OnlyPartial) override; 2157 2158 bool 2159 LoadExternalSpecializations(const Decl *D, 2160 ArrayRef<TemplateArgument> TemplateArgs) override; 2161 2162 /// Finds all the visible declarations with a given name. 2163 /// The current implementation of this method just loads the entire 2164 /// lookup table as unmaterialized references. 2165 bool FindExternalVisibleDeclsByName(const DeclContext *DC, 2166 DeclarationName Name, 2167 const DeclContext *OriginalDC) override; 2168 2169 /// Read all of the declarations lexically stored in a 2170 /// declaration context. 2171 /// 2172 /// \param DC The declaration context whose declarations will be 2173 /// read. 2174 /// 2175 /// \param IsKindWeWant A predicate indicating which declaration kinds 2176 /// we are interested in. 2177 /// 2178 /// \param Decls Vector that will contain the declarations loaded 2179 /// from the external source. The caller is responsible for merging 2180 /// these declarations with any declarations already stored in the 2181 /// declaration context. 2182 void 2183 FindExternalLexicalDecls(const DeclContext *DC, 2184 llvm::function_ref<bool(Decl::Kind)> IsKindWeWant, 2185 SmallVectorImpl<Decl *> &Decls) override; 2186 2187 /// Get the decls that are contained in a file in the Offset/Length 2188 /// range. \p Length can be 0 to indicate a point at \p Offset instead of 2189 /// a range. 2190 void FindFileRegionDecls(FileID File, unsigned Offset, unsigned Length, 2191 SmallVectorImpl<Decl *> &Decls) override; 2192 2193 /// Notify ASTReader that we started deserialization of 2194 /// a decl or type so until FinishedDeserializing is called there may be 2195 /// decls that are initializing. Must be paired with FinishedDeserializing. 2196 void StartedDeserializing() override; 2197 2198 /// Notify ASTReader that we finished the deserialization of 2199 /// a decl or type. Must be paired with StartedDeserializing. 2200 void FinishedDeserializing() override; 2201 2202 /// Function that will be invoked when we begin parsing a new 2203 /// translation unit involving this external AST source. 2204 /// 2205 /// This function will provide all of the external definitions to 2206 /// the ASTConsumer. 2207 void StartTranslationUnit(ASTConsumer *Consumer) override; 2208 2209 /// Print some statistics about AST usage. 2210 void PrintStats() override; 2211 2212 /// Dump information about the AST reader to standard error. 2213 void dump(); 2214 2215 /// Return the amount of memory used by memory buffers, breaking down 2216 /// by heap-backed versus mmap'ed memory. 2217 void getMemoryBufferSizes(MemoryBufferSizes &sizes) const override; 2218 2219 /// Initialize the semantic source with the Sema instance 2220 /// being used to perform semantic analysis on the abstract syntax 2221 /// tree. 2222 void InitializeSema(Sema &S) override; 2223 2224 /// Inform the semantic consumer that Sema is no longer available. 2225 void ForgetSema() override { SemaObj = nullptr; } 2226 2227 /// Retrieve the IdentifierInfo for the named identifier. 2228 /// 2229 /// This routine builds a new IdentifierInfo for the given identifier. If any 2230 /// declarations with this name are visible from translation unit scope, their 2231 /// declarations will be deserialized and introduced into the declaration 2232 /// chain of the identifier. 2233 IdentifierInfo *get(StringRef Name) override; 2234 2235 /// Retrieve an iterator into the set of all identifiers 2236 /// in all loaded AST files. 2237 IdentifierIterator *getIdentifiers() override; 2238 2239 /// Load the contents of the global method pool for a given 2240 /// selector. 2241 void ReadMethodPool(Selector Sel) override; 2242 2243 /// Load the contents of the global method pool for a given 2244 /// selector if necessary. 2245 void updateOutOfDateSelector(Selector Sel) override; 2246 2247 /// Load the set of namespaces that are known to the external source, 2248 /// which will be used during typo correction. 2249 void ReadKnownNamespaces( 2250 SmallVectorImpl<NamespaceDecl *> &Namespaces) override; 2251 2252 void ReadUndefinedButUsed( 2253 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) override; 2254 2255 void ReadMismatchingDeleteExpressions(llvm::MapVector< 2256 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> & 2257 Exprs) override; 2258 2259 void ReadTentativeDefinitions( 2260 SmallVectorImpl<VarDecl *> &TentativeDefs) override; 2261 2262 void ReadUnusedFileScopedDecls( 2263 SmallVectorImpl<const DeclaratorDecl *> &Decls) override; 2264 2265 void ReadDelegatingConstructors( 2266 SmallVectorImpl<CXXConstructorDecl *> &Decls) override; 2267 2268 void ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) override; 2269 2270 void ReadUnusedLocalTypedefNameCandidates( 2271 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) override; 2272 2273 void ReadDeclsToCheckForDeferredDiags( 2274 llvm::SmallSetVector<Decl *, 4> &Decls) override; 2275 2276 void ReadReferencedSelectors( 2277 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) override; 2278 2279 void ReadWeakUndeclaredIdentifiers( 2280 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) override; 2281 2282 void ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) override; 2283 2284 void ReadPendingInstantiations( 2285 SmallVectorImpl<std::pair<ValueDecl *, 2286 SourceLocation>> &Pending) override; 2287 2288 void ReadLateParsedTemplates( 2289 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>> 2290 &LPTMap) override; 2291 2292 void AssignedLambdaNumbering(CXXRecordDecl *Lambda) override; 2293 2294 /// Load a selector from disk, registering its ID if it exists. 2295 void LoadSelector(Selector Sel); 2296 2297 void SetIdentifierInfo(serialization::IdentifierID ID, IdentifierInfo *II); 2298 void SetGloballyVisibleDecls(IdentifierInfo *II, 2299 const SmallVectorImpl<GlobalDeclID> &DeclIDs, 2300 SmallVectorImpl<Decl *> *Decls = nullptr); 2301 2302 /// Report a diagnostic. 2303 DiagnosticBuilder Diag(unsigned DiagID) const; 2304 2305 /// Report a diagnostic. 2306 DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const; 2307 2308 void runWithSufficientStackSpace(SourceLocation Loc, 2309 llvm::function_ref<void()> Fn); 2310 2311 IdentifierInfo *DecodeIdentifierInfo(serialization::IdentifierID ID); 2312 2313 IdentifierInfo *readIdentifier(ModuleFile &M, const RecordData &Record, 2314 unsigned &Idx) { 2315 return DecodeIdentifierInfo(getGlobalIdentifierID(M, Record[Idx++])); 2316 } 2317 2318 IdentifierInfo *GetIdentifier(serialization::IdentifierID ID) override { 2319 // Note that we are loading an identifier. 2320 Deserializing AnIdentifier(this); 2321 2322 return DecodeIdentifierInfo(ID); 2323 } 2324 2325 IdentifierInfo *getLocalIdentifier(ModuleFile &M, uint64_t LocalID); 2326 2327 serialization::IdentifierID getGlobalIdentifierID(ModuleFile &M, 2328 uint64_t LocalID); 2329 2330 void resolvePendingMacro(IdentifierInfo *II, const PendingMacroInfo &PMInfo); 2331 2332 /// Retrieve the macro with the given ID. 2333 MacroInfo *getMacro(serialization::MacroID ID); 2334 2335 /// Retrieve the global macro ID corresponding to the given local 2336 /// ID within the given module file. 2337 serialization::MacroID getGlobalMacroID(ModuleFile &M, unsigned LocalID); 2338 2339 /// Read the source location entry with index ID. 2340 bool ReadSLocEntry(int ID) override; 2341 /// Get the index ID for the loaded SourceLocation offset. 2342 int getSLocEntryID(SourceLocation::UIntTy SLocOffset) override; 2343 /// Try to read the offset of the SLocEntry at the given index in the given 2344 /// module file. 2345 llvm::Expected<SourceLocation::UIntTy> readSLocOffset(ModuleFile *F, 2346 unsigned Index); 2347 2348 /// Retrieve the module import location and module name for the 2349 /// given source manager entry ID. 2350 std::pair<SourceLocation, StringRef> getModuleImportLoc(int ID) override; 2351 2352 /// Retrieve the global submodule ID given a module and its local ID 2353 /// number. 2354 serialization::SubmoduleID getGlobalSubmoduleID(ModuleFile &M, 2355 unsigned LocalID) const; 2356 2357 /// Retrieve the submodule that corresponds to a global submodule ID. 2358 /// 2359 Module *getSubmodule(serialization::SubmoduleID GlobalID); 2360 2361 /// Retrieve the module that corresponds to the given module ID. 2362 /// 2363 /// Note: overrides method in ExternalASTSource 2364 Module *getModule(unsigned ID) override; 2365 2366 /// Retrieve the module file with a given local ID within the specified 2367 /// ModuleFile. 2368 ModuleFile *getLocalModuleFile(ModuleFile &M, unsigned ID) const; 2369 2370 /// Get an ID for the given module file. 2371 unsigned getModuleFileID(ModuleFile *M); 2372 2373 /// Return a descriptor for the corresponding module. 2374 std::optional<ASTSourceDescriptor> getSourceDescriptor(unsigned ID) override; 2375 2376 ExtKind hasExternalDefinitions(const Decl *D) override; 2377 2378 /// Retrieve a selector from the given module with its local ID 2379 /// number. 2380 Selector getLocalSelector(ModuleFile &M, unsigned LocalID); 2381 2382 Selector DecodeSelector(serialization::SelectorID Idx); 2383 2384 Selector GetExternalSelector(serialization::SelectorID ID) override; 2385 uint32_t GetNumExternalSelectors() override; 2386 2387 Selector ReadSelector(ModuleFile &M, const RecordData &Record, unsigned &Idx) { 2388 return getLocalSelector(M, Record[Idx++]); 2389 } 2390 2391 /// Retrieve the global selector ID that corresponds to this 2392 /// the local selector ID in a given module. 2393 serialization::SelectorID getGlobalSelectorID(ModuleFile &M, 2394 unsigned LocalID) const; 2395 2396 /// Read the contents of a CXXCtorInitializer array. 2397 CXXCtorInitializer **GetExternalCXXCtorInitializers(uint64_t Offset) override; 2398 2399 /// Read a AlignPackInfo from raw form. 2400 Sema::AlignPackInfo ReadAlignPackInfo(uint32_t Raw) const { 2401 return Sema::AlignPackInfo::getFromRawEncoding(Raw); 2402 } 2403 2404 using RawLocEncoding = SourceLocationEncoding::RawLocEncoding; 2405 2406 /// Read a source location from raw form and return it in its 2407 /// originating module file's source location space. 2408 std::pair<SourceLocation, unsigned> 2409 ReadUntranslatedSourceLocation(RawLocEncoding Raw, 2410 LocSeq *Seq = nullptr) const { 2411 return SourceLocationEncoding::decode(Raw, Seq); 2412 } 2413 2414 /// Read a source location from raw form. 2415 SourceLocation ReadSourceLocation(ModuleFile &MF, RawLocEncoding Raw, 2416 LocSeq *Seq = nullptr) const { 2417 if (!MF.ModuleOffsetMap.empty()) 2418 ReadModuleOffsetMap(MF); 2419 2420 auto [Loc, ModuleFileIndex] = ReadUntranslatedSourceLocation(Raw, Seq); 2421 ModuleFile *OwningModuleFile = 2422 ModuleFileIndex == 0 ? &MF : MF.TransitiveImports[ModuleFileIndex - 1]; 2423 2424 assert(!SourceMgr.isLoadedSourceLocation(Loc) && 2425 "Run out source location space"); 2426 2427 return TranslateSourceLocation(*OwningModuleFile, Loc); 2428 } 2429 2430 /// Translate a source location from another module file's source 2431 /// location space into ours. 2432 SourceLocation TranslateSourceLocation(ModuleFile &ModuleFile, 2433 SourceLocation Loc) const { 2434 if (Loc.isInvalid()) 2435 return Loc; 2436 2437 // FIXME: TranslateSourceLocation is not re-enterable. It is problematic 2438 // to call TranslateSourceLocation on a translated source location. 2439 // We either need a method to know whether or not a source location is 2440 // translated or refactor the code to make it clear that 2441 // TranslateSourceLocation won't be called with translated source location. 2442 2443 return Loc.getLocWithOffset(ModuleFile.SLocEntryBaseOffset - 2); 2444 } 2445 2446 /// Read a source location. 2447 SourceLocation ReadSourceLocation(ModuleFile &ModuleFile, 2448 const RecordDataImpl &Record, unsigned &Idx, 2449 LocSeq *Seq = nullptr) { 2450 return ReadSourceLocation(ModuleFile, Record[Idx++], Seq); 2451 } 2452 2453 /// Read a FileID. 2454 FileID ReadFileID(ModuleFile &F, const RecordDataImpl &Record, 2455 unsigned &Idx) const { 2456 return TranslateFileID(F, FileID::get(Record[Idx++])); 2457 } 2458 2459 /// Translate a FileID from another module file's FileID space into ours. 2460 FileID TranslateFileID(ModuleFile &F, FileID FID) const { 2461 assert(FID.ID >= 0 && "Reading non-local FileID."); 2462 if (FID.isInvalid()) 2463 return FID; 2464 return FileID::get(F.SLocEntryBaseID + FID.ID - 1); 2465 } 2466 2467 /// Read a source range. 2468 SourceRange ReadSourceRange(ModuleFile &F, const RecordData &Record, 2469 unsigned &Idx, LocSeq *Seq = nullptr); 2470 2471 static llvm::BitVector ReadBitVector(const RecordData &Record, 2472 const StringRef Blob); 2473 2474 // Read a string 2475 static std::string ReadString(const RecordDataImpl &Record, unsigned &Idx); 2476 static StringRef ReadStringBlob(const RecordDataImpl &Record, unsigned &Idx, 2477 StringRef &Blob); 2478 2479 // Read a path 2480 std::string ReadPath(ModuleFile &F, const RecordData &Record, unsigned &Idx); 2481 2482 // Read a path 2483 std::string ReadPath(StringRef BaseDirectory, const RecordData &Record, 2484 unsigned &Idx); 2485 std::string ReadPathBlob(StringRef BaseDirectory, const RecordData &Record, 2486 unsigned &Idx, StringRef &Blob); 2487 2488 /// Read a version tuple. 2489 static VersionTuple ReadVersionTuple(const RecordData &Record, unsigned &Idx); 2490 2491 CXXTemporary *ReadCXXTemporary(ModuleFile &F, const RecordData &Record, 2492 unsigned &Idx); 2493 2494 /// Reads a statement. 2495 Stmt *ReadStmt(ModuleFile &F); 2496 2497 /// Reads an expression. 2498 Expr *ReadExpr(ModuleFile &F); 2499 2500 /// Reads a sub-statement operand during statement reading. 2501 Stmt *ReadSubStmt() { 2502 assert(ReadingKind == Read_Stmt && 2503 "Should be called only during statement reading!"); 2504 // Subexpressions are stored from last to first, so the next Stmt we need 2505 // is at the back of the stack. 2506 assert(!StmtStack.empty() && "Read too many sub-statements!"); 2507 return StmtStack.pop_back_val(); 2508 } 2509 2510 /// Reads a sub-expression operand during statement reading. 2511 Expr *ReadSubExpr(); 2512 2513 /// Reads a token out of a record. 2514 Token ReadToken(ModuleFile &M, const RecordDataImpl &Record, unsigned &Idx); 2515 2516 /// Reads the macro record located at the given offset. 2517 MacroInfo *ReadMacroRecord(ModuleFile &F, uint64_t Offset); 2518 2519 /// Determine the global preprocessed entity ID that corresponds to 2520 /// the given local ID within the given module. 2521 serialization::PreprocessedEntityID 2522 getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const; 2523 2524 /// Add a macro to deserialize its macro directive history. 2525 /// 2526 /// \param II The name of the macro. 2527 /// \param M The module file. 2528 /// \param MacroDirectivesOffset Offset of the serialized macro directive 2529 /// history. 2530 void addPendingMacro(IdentifierInfo *II, ModuleFile *M, 2531 uint32_t MacroDirectivesOffset); 2532 2533 /// Read the set of macros defined by this external macro source. 2534 void ReadDefinedMacros() override; 2535 2536 /// Update an out-of-date identifier. 2537 void updateOutOfDateIdentifier(const IdentifierInfo &II) override; 2538 2539 /// Note that this identifier is up-to-date. 2540 void markIdentifierUpToDate(const IdentifierInfo *II); 2541 2542 /// Load all external visible decls in the given DeclContext. 2543 void completeVisibleDeclsMap(const DeclContext *DC) override; 2544 2545 /// Retrieve the AST context that this AST reader supplements. 2546 ASTContext &getContext() { 2547 assert(ContextObj && "requested AST context when not loading AST"); 2548 return *ContextObj; 2549 } 2550 2551 // Contains the IDs for declarations that were requested before we have 2552 // access to a Sema object. 2553 SmallVector<GlobalDeclID, 16> PreloadedDeclIDs; 2554 2555 /// Retrieve the semantic analysis object used to analyze the 2556 /// translation unit in which the precompiled header is being 2557 /// imported. 2558 Sema *getSema() { return SemaObj; } 2559 2560 /// Get the identifier resolver used for name lookup / updates 2561 /// in the translation unit scope. We have one of these even if we don't 2562 /// have a Sema object. 2563 IdentifierResolver &getIdResolver(); 2564 2565 /// Retrieve the identifier table associated with the 2566 /// preprocessor. 2567 IdentifierTable &getIdentifierTable(); 2568 2569 /// Record that the given ID maps to the given switch-case 2570 /// statement. 2571 void RecordSwitchCaseID(SwitchCase *SC, unsigned ID); 2572 2573 /// Retrieve the switch-case statement with the given ID. 2574 SwitchCase *getSwitchCaseWithID(unsigned ID); 2575 2576 void ClearSwitchCaseIDs(); 2577 2578 /// Cursors for comments blocks. 2579 SmallVector<std::pair<llvm::BitstreamCursor, 2580 serialization::ModuleFile *>, 8> CommentsCursors; 2581 2582 /// Loads comments ranges. 2583 void ReadComments() override; 2584 2585 /// Visit all the input file infos of the given module file. 2586 void visitInputFileInfos( 2587 serialization::ModuleFile &MF, bool IncludeSystem, 2588 llvm::function_ref<void(const serialization::InputFileInfo &IFI, 2589 bool IsSystem)> 2590 Visitor); 2591 2592 /// Visit all the input files of the given module file. 2593 void visitInputFiles(serialization::ModuleFile &MF, 2594 bool IncludeSystem, bool Complain, 2595 llvm::function_ref<void(const serialization::InputFile &IF, 2596 bool isSystem)> Visitor); 2597 2598 /// Visit all the top-level module maps loaded when building the given module 2599 /// file. 2600 void visitTopLevelModuleMaps(serialization::ModuleFile &MF, 2601 llvm::function_ref<void(FileEntryRef)> Visitor); 2602 2603 bool isProcessingUpdateRecords() { return ProcessingUpdateRecords; } 2604 }; 2605 2606 /// A simple helper class to unpack an integer to bits and consuming 2607 /// the bits in order. 2608 class BitsUnpacker { 2609 constexpr static uint32_t BitsIndexUpbound = 32; 2610 2611 public: 2612 BitsUnpacker(uint32_t V) { updateValue(V); } 2613 BitsUnpacker(const BitsUnpacker &) = delete; 2614 BitsUnpacker(BitsUnpacker &&) = delete; 2615 BitsUnpacker operator=(const BitsUnpacker &) = delete; 2616 BitsUnpacker operator=(BitsUnpacker &&) = delete; 2617 ~BitsUnpacker() = default; 2618 2619 void updateValue(uint32_t V) { 2620 Value = V; 2621 CurrentBitsIndex = 0; 2622 } 2623 2624 void advance(uint32_t BitsWidth) { CurrentBitsIndex += BitsWidth; } 2625 2626 bool getNextBit() { 2627 assert(isValid()); 2628 return Value & (1 << CurrentBitsIndex++); 2629 } 2630 2631 uint32_t getNextBits(uint32_t Width) { 2632 assert(isValid()); 2633 assert(Width < BitsIndexUpbound); 2634 uint32_t Ret = (Value >> CurrentBitsIndex) & ((1 << Width) - 1); 2635 CurrentBitsIndex += Width; 2636 return Ret; 2637 } 2638 2639 bool canGetNextNBits(uint32_t Width) const { 2640 return CurrentBitsIndex + Width < BitsIndexUpbound; 2641 } 2642 2643 private: 2644 bool isValid() const { return CurrentBitsIndex < BitsIndexUpbound; } 2645 2646 uint32_t Value; 2647 uint32_t CurrentBitsIndex = ~0; 2648 }; 2649 2650 inline bool shouldSkipCheckingODR(const Decl *D) { 2651 return D->getASTContext().getLangOpts().SkipODRCheckInGMF && 2652 (D->isFromGlobalModule() || D->isFromHeaderUnit()); 2653 } 2654 2655 /// Calculate a hash value for the primary module name of the given module. 2656 /// \returns std::nullopt if M is not a C++ standard module. 2657 std::optional<unsigned> getPrimaryModuleHash(const Module *M); 2658 2659 } // namespace clang 2660 2661 #endif // LLVM_CLANG_SERIALIZATION_ASTREADER_H 2662