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