1 //===--- ASTReader.h - AST File Reader --------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ASTReader class, which reads AST files.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_SERIALIZATION_ASTREADER_H
15 #define LLVM_CLANG_SERIALIZATION_ASTREADER_H
17 #include "clang/AST/DeclObjC.h"
18 #include "clang/AST/DeclarationName.h"
19 #include "clang/AST/TemplateBase.h"
20 #include "clang/Basic/Diagnostic.h"
21 #include "clang/Basic/FileManager.h"
22 #include "clang/Basic/FileSystemOptions.h"
23 #include "clang/Basic/IdentifierTable.h"
24 #include "clang/Basic/SourceManager.h"
25 #include "clang/Basic/Version.h"
26 #include "clang/Lex/ExternalPreprocessorSource.h"
27 #include "clang/Lex/HeaderSearch.h"
28 #include "clang/Lex/PreprocessingRecord.h"
29 #include "clang/Sema/ExternalSemaSource.h"
30 #include "clang/Serialization/ASTBitCodes.h"
31 #include "clang/Serialization/ContinuousRangeMap.h"
32 #include "clang/Serialization/Module.h"
33 #include "clang/Serialization/ModuleManager.h"
34 #include "llvm/ADT/APFloat.h"
35 #include "llvm/ADT/APInt.h"
36 #include "llvm/ADT/APSInt.h"
37 #include "llvm/ADT/MapVector.h"
38 #include "llvm/ADT/SmallPtrSet.h"
39 #include "llvm/ADT/SmallSet.h"
40 #include "llvm/ADT/SmallVector.h"
41 #include "llvm/ADT/StringRef.h"
42 #include "llvm/ADT/TinyPtrVector.h"
43 #include "llvm/Bitcode/BitstreamReader.h"
44 #include "llvm/Support/DataTypes.h"
61 class ASTIdentifierIterator;
62 class ASTUnit; // FIXME: Layering violation and egregious hack.
66 class DefMacroDirective;
67 class DiagnosticOptions;
68 class NestedNameSpecifier;
69 class CXXBaseSpecifier;
70 class CXXConstructorDecl;
71 class CXXCtorInitializer;
72 class GlobalModuleIndex;
74 class MacroDefinition;
77 class OpaqueValueExpr;
79 class PreprocessorOptions;
82 class ASTDeserializationListener;
88 struct HeaderFileInfo;
91 class LazyASTUnresolvedSet;
93 /// \brief Abstract interface for callback invocations by the ASTReader.
95 /// While reading an AST file, the ASTReader will call the methods of the
96 /// listener to pass on specific information. Some of the listener methods can
97 /// return true to indicate to the ASTReader that the information (and
98 /// consequently the AST file) is invalid.
99 class ASTReaderListener {
101 virtual ~ASTReaderListener();
103 /// \brief Receives the full Clang version information.
105 /// \returns true to indicate that the version is invalid. Subclasses should
106 /// generally defer to this implementation.
107 virtual bool ReadFullVersionInformation(StringRef FullVersion) {
108 return FullVersion != getClangFullRepositoryVersion();
111 virtual void ReadModuleName(StringRef ModuleName) {}
112 virtual void ReadModuleMapFile(StringRef ModuleMapPath) {}
114 /// \brief Receives the language options.
116 /// \returns true to indicate the options are invalid or false otherwise.
117 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
119 bool AllowCompatibleDifferences) {
123 /// \brief Receives the target options.
125 /// \returns true to indicate the target options are invalid, or false
127 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
132 /// \brief Receives the diagnostic options.
134 /// \returns true to indicate the diagnostic options are invalid, or false
137 ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,
142 /// \brief Receives the file system options.
144 /// \returns true to indicate the file system options are invalid, or false
146 virtual bool ReadFileSystemOptions(const FileSystemOptions &FSOpts,
151 /// \brief Receives the header search options.
153 /// \returns true to indicate the header search options are invalid, or false
155 virtual bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
160 /// \brief Receives the preprocessor options.
162 /// \param SuggestedPredefines Can be filled in with the set of predefines
163 /// that are suggested by the preprocessor options. Typically only used when
164 /// loading a precompiled header.
166 /// \returns true to indicate the preprocessor options are invalid, or false
168 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
170 std::string &SuggestedPredefines) {
174 /// \brief Receives __COUNTER__ value.
175 virtual void ReadCounter(const serialization::ModuleFile &M,
178 /// This is called for each AST file loaded.
179 virtual void visitModuleFile(StringRef Filename) {}
181 /// \brief Returns true if this \c ASTReaderListener wants to receive the
182 /// input files of the AST file via \c visitInputFile, false otherwise.
183 virtual bool needsInputFileVisitation() { return false; }
184 /// \brief Returns true if this \c ASTReaderListener wants to receive the
185 /// system input files of the AST file via \c visitInputFile, false otherwise.
186 virtual bool needsSystemInputFileVisitation() { return false; }
187 /// \brief if \c needsInputFileVisitation returns true, this is called for
188 /// each non-system input file of the AST File. If
189 /// \c needsSystemInputFileVisitation is true, then it is called for all
190 /// system input files as well.
192 /// \returns true to continue receiving the next input file, false to stop.
193 virtual bool visitInputFile(StringRef Filename, bool isSystem,
198 /// \brief Returns true if this \c ASTReaderListener wants to receive the
199 /// imports of the AST file via \c visitImport, false otherwise.
200 virtual bool needsImportVisitation() const { return false; }
201 /// \brief If needsImportVisitation returns \c true, this is called for each
202 /// AST file imported by this AST file.
203 virtual void visitImport(StringRef Filename) {}
206 /// \brief Simple wrapper class for chaining listeners.
207 class ChainedASTReaderListener : public ASTReaderListener {
208 std::unique_ptr<ASTReaderListener> First;
209 std::unique_ptr<ASTReaderListener> Second;
212 /// Takes ownership of \p First and \p Second.
213 ChainedASTReaderListener(std::unique_ptr<ASTReaderListener> First,
214 std::unique_ptr<ASTReaderListener> Second)
215 : First(std::move(First)), Second(std::move(Second)) {}
217 std::unique_ptr<ASTReaderListener> takeFirst() { return std::move(First); }
218 std::unique_ptr<ASTReaderListener> takeSecond() { return std::move(Second); }
220 bool ReadFullVersionInformation(StringRef FullVersion) override;
221 void ReadModuleName(StringRef ModuleName) override;
222 void ReadModuleMapFile(StringRef ModuleMapPath) override;
223 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
224 bool AllowCompatibleDifferences) override;
225 bool ReadTargetOptions(const TargetOptions &TargetOpts,
226 bool Complain) override;
227 bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,
228 bool Complain) override;
229 bool ReadFileSystemOptions(const FileSystemOptions &FSOpts,
230 bool Complain) override;
232 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
233 bool Complain) override;
234 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
236 std::string &SuggestedPredefines) override;
238 void ReadCounter(const serialization::ModuleFile &M, unsigned Value) override;
239 bool needsInputFileVisitation() override;
240 bool needsSystemInputFileVisitation() override;
241 void visitModuleFile(StringRef Filename) override;
242 bool visitInputFile(StringRef Filename, bool isSystem,
243 bool isOverridden) override;
246 /// \brief ASTReaderListener implementation to validate the information of
247 /// the PCH file against an initialized Preprocessor.
248 class PCHValidator : public ASTReaderListener {
253 PCHValidator(Preprocessor &PP, ASTReader &Reader)
254 : PP(PP), Reader(Reader) {}
256 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
257 bool AllowCompatibleDifferences) override;
258 bool ReadTargetOptions(const TargetOptions &TargetOpts,
259 bool Complain) override;
260 bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,
261 bool Complain) override;
262 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, bool Complain,
263 std::string &SuggestedPredefines) override;
264 void ReadCounter(const serialization::ModuleFile &M, unsigned Value) override;
267 void Error(const char *Msg);
270 namespace serialization {
272 class ReadMethodPoolVisitor;
275 class ASTIdentifierLookupTrait;
276 /// \brief The on-disk hash table used for the DeclContext's Name lookup table.
277 typedef llvm::OnDiskIterableChainedHashTable<ASTDeclContextNameLookupTrait>
278 ASTDeclContextNameLookupTable;
281 } // end namespace serialization
283 /// \brief Reads an AST files chain containing the contents of a translation
286 /// The ASTReader class reads bitstreams (produced by the ASTWriter
287 /// class) containing the serialized representation of a given
288 /// abstract syntax tree and its supporting data structures. An
289 /// instance of the ASTReader can be attached to an ASTContext object,
290 /// which will provide access to the contents of the AST files.
292 /// The AST reader provides lazy de-serialization of declarations, as
293 /// required when traversing the AST. Only those AST nodes that are
294 /// actually required will be de-serialized.
296 : public ExternalPreprocessorSource,
297 public ExternalPreprocessingRecordSource,
298 public ExternalHeaderFileInfoSource,
299 public ExternalSemaSource,
300 public IdentifierInfoLookup,
301 public ExternalIdentifierLookup,
302 public ExternalSLocEntrySource
305 typedef SmallVector<uint64_t, 64> RecordData;
306 typedef SmallVectorImpl<uint64_t> RecordDataImpl;
308 /// \brief The result of reading the control block of an AST file, which
309 /// can fail for various reasons.
311 /// \brief The control block was read successfully. Aside from failures,
312 /// the AST file is safe to read into the current context.
314 /// \brief The AST file itself appears corrupted.
316 /// \brief The AST file was missing.
318 /// \brief The AST file is out-of-date relative to its input files,
319 /// and needs to be regenerated.
321 /// \brief The AST file was written by a different version of Clang.
323 /// \brief The AST file was writtten with a different language/target
325 ConfigurationMismatch,
326 /// \brief The AST file has errors.
330 /// \brief Types of AST files.
331 friend class PCHValidator;
332 friend class ASTDeclReader;
333 friend class ASTStmtReader;
334 friend class ASTIdentifierIterator;
335 friend class serialization::reader::ASTIdentifierLookupTrait;
336 friend class TypeLocReader;
337 friend class ASTWriter;
338 friend class ASTUnit; // ASTUnit needs to remap source locations.
339 friend class serialization::ReadMethodPoolVisitor;
341 typedef serialization::ModuleFile ModuleFile;
342 typedef serialization::ModuleKind ModuleKind;
343 typedef serialization::ModuleManager ModuleManager;
345 typedef ModuleManager::ModuleIterator ModuleIterator;
346 typedef ModuleManager::ModuleConstIterator ModuleConstIterator;
347 typedef ModuleManager::ModuleReverseIterator ModuleReverseIterator;
350 /// \brief The receiver of some callbacks invoked by ASTReader.
351 std::unique_ptr<ASTReaderListener> Listener;
353 /// \brief The receiver of deserialization events.
354 ASTDeserializationListener *DeserializationListener;
355 bool OwnsDeserializationListener;
357 SourceManager &SourceMgr;
358 FileManager &FileMgr;
359 DiagnosticsEngine &Diags;
361 /// \brief The semantic analysis object that will be processing the
362 /// AST files and the translation unit that uses it.
365 /// \brief The preprocessor that will be loading the source file.
368 /// \brief The AST context into which we'll read the AST files.
371 /// \brief The AST consumer.
372 ASTConsumer *Consumer;
374 /// \brief The module manager which manages modules and their dependencies
375 ModuleManager ModuleMgr;
377 /// \brief The location where the module file will be considered as
378 /// imported from. For non-module AST types it should be invalid.
379 SourceLocation CurrentImportLoc;
381 /// \brief The global module index, if loaded.
382 std::unique_ptr<GlobalModuleIndex> GlobalIndex;
384 /// \brief A map of global bit offsets to the module that stores entities
385 /// at those bit offsets.
386 ContinuousRangeMap<uint64_t, ModuleFile*, 4> GlobalBitOffsetsMap;
388 /// \brief A map of negated SLocEntryIDs to the modules containing them.
389 ContinuousRangeMap<unsigned, ModuleFile*, 64> GlobalSLocEntryMap;
391 typedef ContinuousRangeMap<unsigned, ModuleFile*, 64> GlobalSLocOffsetMapType;
393 /// \brief A map of reversed (SourceManager::MaxLoadedOffset - SLocOffset)
394 /// SourceLocation offsets to the modules containing them.
395 GlobalSLocOffsetMapType GlobalSLocOffsetMap;
397 /// \brief Types that have already been loaded from the chain.
399 /// When the pointer at index I is non-NULL, the type with
400 /// ID = (I + 1) << FastQual::Width has already been loaded
401 std::vector<QualType> TypesLoaded;
403 typedef ContinuousRangeMap<serialization::TypeID, ModuleFile *, 4>
406 /// \brief Mapping from global type IDs to the module in which the
407 /// type resides along with the offset that should be added to the
408 /// global type ID to produce a local ID.
409 GlobalTypeMapType GlobalTypeMap;
411 /// \brief Declarations that have already been loaded from the chain.
413 /// When the pointer at index I is non-NULL, the declaration with ID
414 /// = I + 1 has already been loaded.
415 std::vector<Decl *> DeclsLoaded;
417 typedef ContinuousRangeMap<serialization::DeclID, ModuleFile *, 4>
420 /// \brief Mapping from global declaration IDs to the module in which the
421 /// declaration resides.
422 GlobalDeclMapType GlobalDeclMap;
424 typedef std::pair<ModuleFile *, uint64_t> FileOffset;
425 typedef SmallVector<FileOffset, 2> FileOffsetsTy;
426 typedef llvm::DenseMap<serialization::DeclID, FileOffsetsTy>
427 DeclUpdateOffsetsMap;
429 /// \brief Declarations that have modifications residing in a later file
431 DeclUpdateOffsetsMap DeclUpdateOffsets;
433 /// \brief Declaration updates for already-loaded declarations that we need
434 /// to apply once we finish processing an import.
435 llvm::SmallVector<std::pair<serialization::GlobalDeclID, Decl*>, 16>
436 PendingUpdateRecords;
438 struct ReplacedDeclInfo {
443 ReplacedDeclInfo() : Mod(nullptr), Offset(0), RawLoc(0) {}
444 ReplacedDeclInfo(ModuleFile *Mod, uint64_t Offset, unsigned RawLoc)
445 : Mod(Mod), Offset(Offset), RawLoc(RawLoc) {}
448 typedef llvm::DenseMap<serialization::DeclID, ReplacedDeclInfo>
450 /// \brief Declarations that have been replaced in a later file in the chain.
451 DeclReplacementMap ReplacedDecls;
453 /// \brief Declarations that have been imported and have typedef names for
454 /// linkage purposes.
455 llvm::DenseMap<std::pair<DeclContext*, IdentifierInfo*>, NamedDecl*>
456 ImportedTypedefNamesForLinkage;
458 /// \brief Mergeable declaration contexts that have anonymous declarations
459 /// within them, and those anonymous declarations.
460 llvm::DenseMap<DeclContext*, llvm::SmallVector<NamedDecl*, 2>>
461 AnonymousDeclarationsForMerging;
463 struct FileDeclsInfo {
465 ArrayRef<serialization::LocalDeclID> Decls;
467 FileDeclsInfo() : Mod(nullptr) {}
468 FileDeclsInfo(ModuleFile *Mod, ArrayRef<serialization::LocalDeclID> Decls)
469 : Mod(Mod), Decls(Decls) {}
472 /// \brief Map from a FileID to the file-level declarations that it contains.
473 llvm::DenseMap<FileID, FileDeclsInfo> FileDeclIDs;
475 // Updates for visible decls can occur for other contexts than just the
476 // TU, and when we read those update records, the actual context will not
477 // be available yet (unless it's the TU), so have this pending map using the
478 // ID as a key. It will be realized when the context is actually loaded.
480 SmallVector<std::pair<serialization::reader::ASTDeclContextNameLookupTable *,
481 ModuleFile*>, 1> DeclContextVisibleUpdates;
482 typedef llvm::DenseMap<serialization::DeclID, DeclContextVisibleUpdates>
483 DeclContextVisibleUpdatesPending;
485 /// \brief Updates to the visible declarations of declaration contexts that
486 /// haven't been loaded yet.
487 DeclContextVisibleUpdatesPending PendingVisibleUpdates;
489 /// \brief The set of C++ or Objective-C classes that have forward
490 /// declarations that have not yet been linked to their definitions.
491 llvm::SmallPtrSet<Decl *, 4> PendingDefinitions;
493 typedef llvm::MapVector<Decl *, uint64_t,
494 llvm::SmallDenseMap<Decl *, unsigned, 4>,
495 SmallVector<std::pair<Decl *, uint64_t>, 4> >
498 /// \brief Functions or methods that have bodies that will be attached.
499 PendingBodiesMap PendingBodies;
501 /// \brief Read the records that describe the contents of declcontexts.
502 bool ReadDeclContextStorage(ModuleFile &M,
503 llvm::BitstreamCursor &Cursor,
504 const std::pair<uint64_t, uint64_t> &Offsets,
505 serialization::DeclContextInfo &Info);
507 /// \brief A vector containing identifiers that have already been
510 /// If the pointer at index I is non-NULL, then it refers to the
511 /// IdentifierInfo for the identifier with ID=I+1 that has already
513 std::vector<IdentifierInfo *> IdentifiersLoaded;
515 typedef ContinuousRangeMap<serialization::IdentID, ModuleFile *, 4>
516 GlobalIdentifierMapType;
518 /// \brief Mapping from global identifier IDs to the module in which the
519 /// identifier resides along with the offset that should be added to the
520 /// global identifier ID to produce a local ID.
521 GlobalIdentifierMapType GlobalIdentifierMap;
523 /// \brief A vector containing macros that have already been
526 /// If the pointer at index I is non-NULL, then it refers to the
527 /// MacroInfo for the identifier with ID=I+1 that has already
529 std::vector<MacroInfo *> MacrosLoaded;
531 typedef ContinuousRangeMap<serialization::MacroID, ModuleFile *, 4>
534 /// \brief Mapping from global macro IDs to the module in which the
535 /// macro resides along with the offset that should be added to the
536 /// global macro ID to produce a local ID.
537 GlobalMacroMapType GlobalMacroMap;
539 /// \brief A vector containing submodules that have already been loaded.
541 /// This vector is indexed by the Submodule ID (-1). NULL submodule entries
542 /// indicate that the particular submodule ID has not yet been loaded.
543 SmallVector<Module *, 2> SubmodulesLoaded;
545 typedef ContinuousRangeMap<serialization::SubmoduleID, ModuleFile *, 4>
546 GlobalSubmoduleMapType;
548 /// \brief Mapping from global submodule IDs to the module file in which the
549 /// submodule resides along with the offset that should be added to the
550 /// global submodule ID to produce a local ID.
551 GlobalSubmoduleMapType GlobalSubmoduleMap;
553 /// \brief Information on a macro definition or undefinition that is visible
554 /// at the end of a submodule.
555 struct ModuleMacroInfo;
557 /// \brief An entity that has been hidden.
568 ModuleMacroInfo *MMI;
574 HiddenName(Decl *D) : Kind(Declaration), D(D), Id() { }
576 HiddenName(IdentifierInfo *II, ModuleMacroInfo *MMI)
577 : Kind(Macro), MMI(MMI), Id(II) { }
579 NameKind getKind() const { return Kind; }
581 Decl *getDecl() const {
582 assert(getKind() == Declaration && "Hidden name is not a declaration");
586 std::pair<IdentifierInfo *, ModuleMacroInfo *> getMacro() const {
587 assert(getKind() == Macro && "Hidden name is not a macro!");
588 return std::make_pair(Id, MMI);
592 typedef llvm::SmallDenseMap<IdentifierInfo*,
593 ModuleMacroInfo*> HiddenMacrosMap;
595 /// \brief A set of hidden declarations.
597 SmallVector<Decl*, 2> HiddenDecls;
598 HiddenMacrosMap HiddenMacros;
601 typedef llvm::DenseMap<Module *, HiddenNames> HiddenNamesMapType;
603 /// \brief A mapping from each of the hidden submodules to the deserialized
604 /// declarations in that submodule that could be made visible.
605 HiddenNamesMapType HiddenNamesMap;
608 /// \brief A module import, export, or conflict that hasn't yet been resolved.
609 struct UnresolvedModuleRef {
610 /// \brief The file in which this module resides.
613 /// \brief The module that is importing or exporting.
616 /// \brief The kind of module reference.
617 enum { Import, Export, Conflict } Kind;
619 /// \brief The local ID of the module that is being exported.
622 /// \brief Whether this is a wildcard export.
623 unsigned IsWildcard : 1;
625 /// \brief String data.
629 /// \brief The set of module imports and exports that still need to be
631 SmallVector<UnresolvedModuleRef, 2> UnresolvedModuleRefs;
633 /// \brief A vector containing selectors that have already been loaded.
635 /// This vector is indexed by the Selector ID (-1). NULL selector
636 /// entries indicate that the particular selector ID has not yet
638 SmallVector<Selector, 16> SelectorsLoaded;
640 typedef ContinuousRangeMap<serialization::SelectorID, ModuleFile *, 4>
641 GlobalSelectorMapType;
643 /// \brief Mapping from global selector IDs to the module in which the
645 /// global selector ID to produce a local ID.
646 GlobalSelectorMapType GlobalSelectorMap;
648 /// \brief The generation number of the last time we loaded data from the
649 /// global method pool for this selector.
650 llvm::DenseMap<Selector, unsigned> SelectorGeneration;
652 struct PendingMacroInfo {
655 struct ModuleMacroDataTy {
657 serialization::SubmoduleID *Overrides;
659 struct PCHMacroDataTy {
660 uint64_t MacroDirectivesOffset;
664 ModuleMacroDataTy ModuleMacroData;
665 PCHMacroDataTy PCHMacroData;
668 PendingMacroInfo(ModuleFile *M,
670 serialization::SubmoduleID *Overrides) : M(M) {
671 ModuleMacroData.MacID = MacID;
672 ModuleMacroData.Overrides = Overrides;
675 PendingMacroInfo(ModuleFile *M, uint64_t MacroDirectivesOffset) : M(M) {
676 PCHMacroData.MacroDirectivesOffset = MacroDirectivesOffset;
680 typedef llvm::MapVector<IdentifierInfo *, SmallVector<PendingMacroInfo, 2> >
683 /// \brief Mapping from identifiers that have a macro history to the global
684 /// IDs have not yet been deserialized to the global IDs of those macros.
685 PendingMacroIDsMap PendingMacroIDs;
687 typedef ContinuousRangeMap<unsigned, ModuleFile *, 4>
688 GlobalPreprocessedEntityMapType;
690 /// \brief Mapping from global preprocessing entity IDs to the module in
691 /// which the preprocessed entity resides along with the offset that should be
692 /// added to the global preprocessing entitiy ID to produce a local ID.
693 GlobalPreprocessedEntityMapType GlobalPreprocessedEntityMap;
695 /// \name CodeGen-relevant special data
696 /// \brief Fields containing data that is relevant to CodeGen.
699 /// \brief The IDs of all declarations that fulfill the criteria of
700 /// "interesting" decls.
702 /// This contains the data loaded from all EAGERLY_DESERIALIZED_DECLS blocks
703 /// in the chain. The referenced declarations are deserialized and passed to
704 /// the consumer eagerly.
705 SmallVector<uint64_t, 16> EagerlyDeserializedDecls;
707 /// \brief The IDs of all tentative definitions stored in the chain.
709 /// Sema keeps track of all tentative definitions in a TU because it has to
710 /// complete them and pass them on to CodeGen. Thus, tentative definitions in
711 /// the PCH chain must be eagerly deserialized.
712 SmallVector<uint64_t, 16> TentativeDefinitions;
714 /// \brief The IDs of all CXXRecordDecls stored in the chain whose VTables are
717 /// CodeGen has to emit VTables for these records, so they have to be eagerly
719 SmallVector<uint64_t, 64> VTableUses;
721 /// \brief A snapshot of the pending instantiations in the chain.
723 /// This record tracks the instantiations that Sema has to perform at the
724 /// end of the TU. It consists of a pair of values for every pending
725 /// instantiation where the first value is the ID of the decl and the second
726 /// is the instantiation location.
727 SmallVector<uint64_t, 64> PendingInstantiations;
731 /// \name DiagnosticsEngine-relevant special data
732 /// \brief Fields containing data that is used for generating diagnostics
735 /// \brief A snapshot of Sema's unused file-scoped variable tracking, for
736 /// generating warnings.
737 SmallVector<uint64_t, 16> UnusedFileScopedDecls;
739 /// \brief A list of all the delegating constructors we've seen, to diagnose
741 SmallVector<uint64_t, 4> DelegatingCtorDecls;
743 /// \brief Method selectors used in a @selector expression. Used for
744 /// implementation of -Wselector.
745 SmallVector<uint64_t, 64> ReferencedSelectorsData;
747 /// \brief A snapshot of Sema's weak undeclared identifier tracking, for
748 /// generating warnings.
749 SmallVector<uint64_t, 64> WeakUndeclaredIdentifiers;
751 /// \brief The IDs of type aliases for ext_vectors that exist in the chain.
753 /// Used by Sema for finding sugared names for ext_vectors in diagnostics.
754 SmallVector<uint64_t, 4> ExtVectorDecls;
758 /// \name Sema-relevant special data
759 /// \brief Fields containing data that is used for semantic analysis
762 /// \brief The IDs of all locally scoped extern "C" decls in the chain.
764 /// Sema tracks these to validate that the types are consistent across all
765 /// local extern "C" declarations.
766 SmallVector<uint64_t, 16> LocallyScopedExternCDecls;
768 /// \brief The IDs of all dynamic class declarations in the chain.
770 /// Sema tracks these because it checks for the key functions being defined
771 /// at the end of the TU, in which case it directs CodeGen to emit the VTable.
772 SmallVector<uint64_t, 16> DynamicClasses;
774 /// \brief The IDs of all potentially unused typedef names in the chain.
776 /// Sema tracks these to emit warnings.
777 SmallVector<uint64_t, 16> UnusedLocalTypedefNameCandidates;
779 /// \brief The IDs of the declarations Sema stores directly.
781 /// Sema tracks a few important decls, such as namespace std, directly.
782 SmallVector<uint64_t, 4> SemaDeclRefs;
784 /// \brief The IDs of the types ASTContext stores directly.
786 /// The AST context tracks a few important types, such as va_list, directly.
787 SmallVector<uint64_t, 16> SpecialTypes;
789 /// \brief The IDs of CUDA-specific declarations ASTContext stores directly.
791 /// The AST context tracks a few important decls, currently cudaConfigureCall,
793 SmallVector<uint64_t, 2> CUDASpecialDeclRefs;
795 /// \brief The floating point pragma option settings.
796 SmallVector<uint64_t, 1> FPPragmaOptions;
798 /// \brief The pragma clang optimize location (if the pragma state is "off").
799 SourceLocation OptimizeOffPragmaLocation;
801 /// \brief The OpenCL extension settings.
802 SmallVector<uint64_t, 1> OpenCLExtensions;
804 /// \brief A list of the namespaces we've seen.
805 SmallVector<uint64_t, 4> KnownNamespaces;
807 /// \brief A list of undefined decls with internal linkage followed by the
808 /// SourceLocation of a matching ODR-use.
809 SmallVector<uint64_t, 8> UndefinedButUsed;
811 // \brief A list of late parsed template function data.
812 SmallVector<uint64_t, 1> LateParsedTemplates;
814 struct ImportedSubmodule {
815 serialization::SubmoduleID ID;
816 SourceLocation ImportLoc;
818 ImportedSubmodule(serialization::SubmoduleID ID, SourceLocation ImportLoc)
819 : ID(ID), ImportLoc(ImportLoc) {}
822 /// \brief A list of modules that were imported by precompiled headers or
823 /// any other non-module AST file.
824 SmallVector<ImportedSubmodule, 2> ImportedModules;
827 /// \brief The directory that the PCH we are reading is stored in.
828 std::string CurrentDir;
830 /// \brief The system include root to be used when loading the
831 /// precompiled header.
832 std::string isysroot;
834 /// \brief Whether to disable the normal validation performed on precompiled
835 /// headers when they are loaded.
836 bool DisableValidation;
838 /// \brief Whether to accept an AST file with compiler errors.
839 bool AllowASTWithCompilerErrors;
841 /// \brief Whether to accept an AST file that has a different configuration
842 /// from the current compiler instance.
843 bool AllowConfigurationMismatch;
845 /// \brief Whether validate system input files.
846 bool ValidateSystemInputs;
848 /// \brief Whether we are allowed to use the global module index.
851 /// \brief Whether we have tried loading the global module index yet.
852 bool TriedLoadingGlobalIndex;
854 typedef llvm::DenseMap<unsigned, SwitchCase *> SwitchCaseMapTy;
855 /// \brief Mapping from switch-case IDs in the chain to switch-case statements
857 /// Statements usually don't have IDs, but switch cases need them, so that the
858 /// switch statement can refer to them.
859 SwitchCaseMapTy SwitchCaseStmts;
861 SwitchCaseMapTy *CurrSwitchCaseStmts;
863 /// \brief The number of source location entries de-serialized from
865 unsigned NumSLocEntriesRead;
867 /// \brief The number of source location entries in the chain.
868 unsigned TotalNumSLocEntries;
870 /// \brief The number of statements (and expressions) de-serialized
872 unsigned NumStatementsRead;
874 /// \brief The total number of statements (and expressions) stored
876 unsigned TotalNumStatements;
878 /// \brief The number of macros de-serialized from the chain.
879 unsigned NumMacrosRead;
881 /// \brief The total number of macros stored in the chain.
882 unsigned TotalNumMacros;
884 /// \brief The number of lookups into identifier tables.
885 unsigned NumIdentifierLookups;
887 /// \brief The number of lookups into identifier tables that succeed.
888 unsigned NumIdentifierLookupHits;
890 /// \brief The number of selectors that have been read.
891 unsigned NumSelectorsRead;
893 /// \brief The number of method pool entries that have been read.
894 unsigned NumMethodPoolEntriesRead;
896 /// \brief The number of times we have looked up a selector in the method
898 unsigned NumMethodPoolLookups;
900 /// \brief The number of times we have looked up a selector in the method
901 /// pool and found something.
902 unsigned NumMethodPoolHits;
904 /// \brief The number of times we have looked up a selector in the method
905 /// pool within a specific module.
906 unsigned NumMethodPoolTableLookups;
908 /// \brief The number of times we have looked up a selector in the method
909 /// pool within a specific module and found something.
910 unsigned NumMethodPoolTableHits;
912 /// \brief The total number of method pool entries in the selector table.
913 unsigned TotalNumMethodPoolEntries;
915 /// Number of lexical decl contexts read/total.
916 unsigned NumLexicalDeclContextsRead, TotalLexicalDeclContexts;
918 /// Number of visible decl contexts read/total.
919 unsigned NumVisibleDeclContextsRead, TotalVisibleDeclContexts;
921 /// Total size of modules, in bits, currently loaded
922 uint64_t TotalModulesSizeInBits;
924 /// \brief Number of Decl/types that are currently deserializing.
925 unsigned NumCurrentElementsDeserializing;
927 /// \brief Set true while we are in the process of passing deserialized
928 /// "interesting" decls to consumer inside FinishedDeserializing().
929 /// This is used as a guard to avoid recursively repeating the process of
930 /// passing decls to consumer.
931 bool PassingDeclsToConsumer;
933 /// Number of CXX base specifiers currently loaded
934 unsigned NumCXXBaseSpecifiersLoaded;
936 /// \brief The set of identifiers that were read while the AST reader was
937 /// (recursively) loading declarations.
939 /// The declarations on the identifier chain for these identifiers will be
940 /// loaded once the recursive loading has completed.
941 llvm::MapVector<IdentifierInfo *, SmallVector<uint32_t, 4> >
942 PendingIdentifierInfos;
944 /// \brief The generation number of each identifier, which keeps track of
945 /// the last time we loaded information about this identifier.
946 llvm::DenseMap<IdentifierInfo *, unsigned> IdentifierGeneration;
948 /// \brief Contains declarations and definitions that will be
949 /// "interesting" to the ASTConsumer, when we get that AST consumer.
951 /// "Interesting" declarations are those that have data that may
952 /// need to be emitted, such as inline function definitions or
953 /// Objective-C protocols.
954 std::deque<Decl *> InterestingDecls;
956 /// \brief The set of redeclarable declarations that have been deserialized
957 /// since the last time the declaration chains were linked.
958 llvm::SmallPtrSet<Decl *, 16> RedeclsDeserialized;
960 /// \brief The list of redeclaration chains that still need to be
963 /// Each element is the global declaration ID of the first declaration in
964 /// the chain. Elements in this vector should be unique; use
965 /// PendingDeclChainsKnown to ensure uniqueness.
966 SmallVector<serialization::DeclID, 16> PendingDeclChains;
968 /// \brief Keeps track of the elements added to PendingDeclChains.
969 llvm::SmallSet<serialization::DeclID, 16> PendingDeclChainsKnown;
971 /// \brief The list of canonical declarations whose redeclaration chains
972 /// need to be marked as incomplete once we're done deserializing things.
973 SmallVector<Decl *, 16> PendingIncompleteDeclChains;
975 /// \brief The Decl IDs for the Sema/Lexical DeclContext of a Decl that has
976 /// been loaded but its DeclContext was not set yet.
977 struct PendingDeclContextInfo {
979 serialization::GlobalDeclID SemaDC;
980 serialization::GlobalDeclID LexicalDC;
983 /// \brief The set of Decls that have been loaded but their DeclContexts are
986 /// The DeclContexts for these Decls will be set once recursive loading has
988 std::deque<PendingDeclContextInfo> PendingDeclContextInfos;
990 /// \brief The set of NamedDecls that have been loaded, but are members of a
991 /// context that has been merged into another context where the corresponding
992 /// declaration is either missing or has not yet been loaded.
994 /// We will check whether the corresponding declaration is in fact missing
995 /// once recursing loading has been completed.
996 llvm::SmallVector<NamedDecl *, 16> PendingOdrMergeChecks;
998 /// \brief Record definitions in which we found an ODR violation.
999 llvm::SmallDenseMap<CXXRecordDecl *, llvm::TinyPtrVector<CXXRecordDecl *>, 2>
1000 PendingOdrMergeFailures;
1002 /// \brief DeclContexts in which we have diagnosed an ODR violation.
1003 llvm::SmallPtrSet<DeclContext*, 2> DiagnosedOdrMergeFailures;
1005 /// \brief The set of Objective-C categories that have been deserialized
1006 /// since the last time the declaration chains were linked.
1007 llvm::SmallPtrSet<ObjCCategoryDecl *, 16> CategoriesDeserialized;
1009 /// \brief The set of Objective-C class definitions that have already been
1010 /// loaded, for which we will need to check for categories whenever a new
1011 /// module is loaded.
1012 SmallVector<ObjCInterfaceDecl *, 16> ObjCClassesLoaded;
1014 /// \brief A mapping from a primary context for a declaration chain to the
1015 /// other declarations of that entity that also have name lookup tables.
1016 /// Used when we merge together two class definitions that have different
1017 /// sets of declared special member functions.
1018 llvm::DenseMap<const DeclContext*, SmallVector<const DeclContext*, 2>>
1021 typedef llvm::DenseMap<Decl *, SmallVector<serialization::DeclID, 2> >
1024 /// \brief A mapping from canonical declarations to the set of additional
1025 /// (global, previously-canonical) declaration IDs that have been merged with
1026 /// that canonical declaration.
1027 MergedDeclsMap MergedDecls;
1029 typedef llvm::DenseMap<serialization::GlobalDeclID,
1030 SmallVector<serialization::DeclID, 2> >
1031 StoredMergedDeclsMap;
1033 /// \brief A mapping from canonical declaration IDs to the set of additional
1034 /// declaration IDs that have been merged with that canonical declaration.
1036 /// This is the deserialized representation of the entries in MergedDecls.
1037 /// When we query entries in MergedDecls, they will be augmented with entries
1038 /// from StoredMergedDecls.
1039 StoredMergedDeclsMap StoredMergedDecls;
1041 /// \brief Combine the stored merged declarations for the given canonical
1042 /// declaration into the set of merged declarations.
1044 /// \returns An iterator into MergedDecls that corresponds to the position of
1045 /// the given canonical declaration.
1046 MergedDeclsMap::iterator
1047 combineStoredMergedDecls(Decl *Canon, serialization::GlobalDeclID CanonID);
1049 /// \brief A mapping from DeclContexts to the semantic DeclContext that we
1050 /// are treating as the definition of the entity. This is used, for instance,
1051 /// when merging implicit instantiations of class templates across modules.
1052 llvm::DenseMap<DeclContext *, DeclContext *> MergedDeclContexts;
1054 /// \brief A mapping from canonical declarations of enums to their canonical
1055 /// definitions. Only populated when using modules in C++.
1056 llvm::DenseMap<EnumDecl *, EnumDecl *> EnumDefinitions;
1058 /// \brief When reading a Stmt tree, Stmt operands are placed in this stack.
1059 SmallVector<Stmt *, 16> StmtStack;
1061 /// \brief What kind of records we are reading.
1063 Read_None, Read_Decl, Read_Type, Read_Stmt
1066 /// \brief What kind of records we are reading.
1067 ReadingKind ReadingKind;
1069 /// \brief RAII object to change the reading kind.
1070 class ReadingKindTracker {
1072 enum ReadingKind PrevKind;
1074 ReadingKindTracker(const ReadingKindTracker &) LLVM_DELETED_FUNCTION;
1075 void operator=(const ReadingKindTracker &) LLVM_DELETED_FUNCTION;
1078 ReadingKindTracker(enum ReadingKind newKind, ASTReader &reader)
1079 : Reader(reader), PrevKind(Reader.ReadingKind) {
1080 Reader.ReadingKind = newKind;
1083 ~ReadingKindTracker() { Reader.ReadingKind = PrevKind; }
1086 /// \brief Suggested contents of the predefines buffer, after this
1087 /// PCH file has been processed.
1089 /// In most cases, this string will be empty, because the predefines
1090 /// buffer computed to build the PCH file will be identical to the
1091 /// predefines buffer computed from the command line. However, when
1092 /// there are differences that the PCH reader can work around, this
1093 /// predefines buffer may contain additional definitions.
1094 std::string SuggestedPredefines;
1096 /// \brief Reads a statement from the specified cursor.
1097 Stmt *ReadStmtFromStream(ModuleFile &F);
1099 struct InputFileInfo {
1100 std::string Filename;
1106 /// \brief Reads the stored information about an input file.
1107 InputFileInfo readInputFileInfo(ModuleFile &F, unsigned ID);
1108 /// \brief A convenience method to read the filename from an input file.
1109 std::string getInputFileName(ModuleFile &F, unsigned ID);
1111 /// \brief Retrieve the file entry and 'overridden' bit for an input
1112 /// file in the given module file.
1113 serialization::InputFile getInputFile(ModuleFile &F, unsigned ID,
1114 bool Complain = true);
1117 void ResolveImportedPath(ModuleFile &M, std::string &Filename);
1118 static void ResolveImportedPath(std::string &Filename, StringRef Prefix);
1121 struct ImportedModule {
1123 ModuleFile *ImportedBy;
1124 SourceLocation ImportLoc;
1126 ImportedModule(ModuleFile *Mod,
1127 ModuleFile *ImportedBy,
1128 SourceLocation ImportLoc)
1129 : Mod(Mod), ImportedBy(ImportedBy), ImportLoc(ImportLoc) { }
1132 ASTReadResult ReadASTCore(StringRef FileName, ModuleKind Type,
1133 SourceLocation ImportLoc, ModuleFile *ImportedBy,
1134 SmallVectorImpl<ImportedModule> &Loaded,
1135 off_t ExpectedSize, time_t ExpectedModTime,
1136 serialization::ASTFileSignature ExpectedSignature,
1137 unsigned ClientLoadCapabilities);
1138 ASTReadResult ReadControlBlock(ModuleFile &F,
1139 SmallVectorImpl<ImportedModule> &Loaded,
1140 const ModuleFile *ImportedBy,
1141 unsigned ClientLoadCapabilities);
1142 ASTReadResult ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities);
1143 bool ParseLineTable(ModuleFile &F, const RecordData &Record);
1144 bool ReadSourceManagerBlock(ModuleFile &F);
1145 llvm::BitstreamCursor &SLocCursorForID(int ID);
1146 SourceLocation getImportLocation(ModuleFile *F);
1147 ASTReadResult ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
1148 const ModuleFile *ImportedBy,
1149 unsigned ClientLoadCapabilities);
1150 ASTReadResult ReadSubmoduleBlock(ModuleFile &F,
1151 unsigned ClientLoadCapabilities);
1152 static bool ParseLanguageOptions(const RecordData &Record, bool Complain,
1153 ASTReaderListener &Listener,
1154 bool AllowCompatibleDifferences);
1155 static bool ParseTargetOptions(const RecordData &Record, bool Complain,
1156 ASTReaderListener &Listener);
1157 static bool ParseDiagnosticOptions(const RecordData &Record, bool Complain,
1158 ASTReaderListener &Listener);
1159 static bool ParseFileSystemOptions(const RecordData &Record, bool Complain,
1160 ASTReaderListener &Listener);
1161 static bool ParseHeaderSearchOptions(const RecordData &Record, bool Complain,
1162 ASTReaderListener &Listener);
1163 static bool ParsePreprocessorOptions(const RecordData &Record, bool Complain,
1164 ASTReaderListener &Listener,
1165 std::string &SuggestedPredefines);
1167 struct RecordLocation {
1168 RecordLocation(ModuleFile *M, uint64_t O)
1169 : F(M), Offset(O) {}
1174 QualType readTypeRecord(unsigned Index);
1175 void readExceptionSpec(ModuleFile &ModuleFile,
1176 SmallVectorImpl<QualType> &ExceptionStorage,
1177 FunctionProtoType::ExceptionSpecInfo &ESI,
1178 const RecordData &Record, unsigned &Index);
1179 RecordLocation TypeCursorForIndex(unsigned Index);
1180 void LoadedDecl(unsigned Index, Decl *D);
1181 Decl *ReadDeclRecord(serialization::DeclID ID);
1182 void markIncompleteDeclChain(Decl *Canon);
1183 RecordLocation DeclCursorForID(serialization::DeclID ID,
1184 unsigned &RawLocation);
1185 void loadDeclUpdateRecords(serialization::DeclID ID, Decl *D);
1186 void loadPendingDeclChain(serialization::GlobalDeclID ID);
1187 void loadObjCCategories(serialization::GlobalDeclID ID, ObjCInterfaceDecl *D,
1188 unsigned PreviousGeneration = 0);
1190 RecordLocation getLocalBitOffset(uint64_t GlobalOffset);
1191 uint64_t getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset);
1193 /// \brief Returns the first preprocessed entity ID that begins or ends after
1195 serialization::PreprocessedEntityID
1196 findPreprocessedEntity(SourceLocation Loc, bool EndsAfter) const;
1198 /// \brief Find the next module that contains entities and return the ID
1199 /// of the first entry.
1201 /// \param SLocMapI points at a chunk of a module that contains no
1202 /// preprocessed entities or the entities it contains are not the
1203 /// ones we are looking for.
1204 serialization::PreprocessedEntityID
1205 findNextPreprocessedEntity(
1206 GlobalSLocOffsetMapType::const_iterator SLocMapI) const;
1208 /// \brief Returns (ModuleFile, Local index) pair for \p GlobalIndex of a
1209 /// preprocessed entity.
1210 std::pair<ModuleFile *, unsigned>
1211 getModulePreprocessedEntity(unsigned GlobalIndex);
1213 /// \brief Returns (begin, end) pair for the preprocessed entities of a
1214 /// particular module.
1215 std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
1216 getModulePreprocessedEntities(ModuleFile &Mod) const;
1218 class ModuleDeclIterator {
1221 const serialization::LocalDeclID *Pos;
1224 typedef const Decl *value_type;
1225 typedef value_type& reference;
1226 typedef value_type* pointer;
1228 ModuleDeclIterator() : Reader(nullptr), Mod(nullptr), Pos(nullptr) { }
1230 ModuleDeclIterator(ASTReader *Reader, ModuleFile *Mod,
1231 const serialization::LocalDeclID *Pos)
1232 : Reader(Reader), Mod(Mod), Pos(Pos) { }
1234 value_type operator*() const {
1235 return Reader->GetDecl(Reader->getGlobalDeclID(*Mod, *Pos));
1238 ModuleDeclIterator &operator++() {
1243 ModuleDeclIterator operator++(int) {
1244 ModuleDeclIterator Prev(*this);
1249 ModuleDeclIterator &operator--() {
1254 ModuleDeclIterator operator--(int) {
1255 ModuleDeclIterator Prev(*this);
1260 friend bool operator==(const ModuleDeclIterator &LHS,
1261 const ModuleDeclIterator &RHS) {
1262 assert(LHS.Reader == RHS.Reader && LHS.Mod == RHS.Mod);
1263 return LHS.Pos == RHS.Pos;
1266 friend bool operator!=(const ModuleDeclIterator &LHS,
1267 const ModuleDeclIterator &RHS) {
1268 assert(LHS.Reader == RHS.Reader && LHS.Mod == RHS.Mod);
1269 return LHS.Pos != RHS.Pos;
1273 std::pair<ModuleDeclIterator, ModuleDeclIterator>
1274 getModuleFileLevelDecls(ModuleFile &Mod);
1276 void PassInterestingDeclsToConsumer();
1277 void PassInterestingDeclToConsumer(Decl *D);
1279 void finishPendingActions();
1280 void diagnoseOdrViolations();
1282 void pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name);
1284 void addPendingDeclContextInfo(Decl *D,
1285 serialization::GlobalDeclID SemaDC,
1286 serialization::GlobalDeclID LexicalDC) {
1288 PendingDeclContextInfo Info = { D, SemaDC, LexicalDC };
1289 PendingDeclContextInfos.push_back(Info);
1292 /// \brief Produce an error diagnostic and return true.
1294 /// This routine should only be used for fatal errors that have to
1295 /// do with non-routine failures (e.g., corrupted AST file).
1296 void Error(StringRef Msg);
1297 void Error(unsigned DiagID, StringRef Arg1 = StringRef(),
1298 StringRef Arg2 = StringRef());
1300 ASTReader(const ASTReader &) LLVM_DELETED_FUNCTION;
1301 void operator=(const ASTReader &) LLVM_DELETED_FUNCTION;
1303 /// \brief Load the AST file and validate its contents against the given
1306 /// \param PP the preprocessor associated with the context in which this
1307 /// precompiled header will be loaded.
1309 /// \param Context the AST context that this precompiled header will be
1312 /// \param isysroot If non-NULL, the system include path specified by the
1313 /// user. This is only used with relocatable PCH files. If non-NULL,
1314 /// a relocatable PCH file will use the default path "/".
1316 /// \param DisableValidation If true, the AST reader will suppress most
1317 /// of its regular consistency checking, allowing the use of precompiled
1318 /// headers that cannot be determined to be compatible.
1320 /// \param AllowASTWithCompilerErrors If true, the AST reader will accept an
1321 /// AST file the was created out of an AST with compiler errors,
1322 /// otherwise it will reject it.
1324 /// \param AllowConfigurationMismatch If true, the AST reader will not check
1325 /// for configuration differences between the AST file and the invocation.
1327 /// \param ValidateSystemInputs If true, the AST reader will validate
1328 /// system input files in addition to user input files. This is only
1329 /// meaningful if \p DisableValidation is false.
1331 /// \param UseGlobalIndex If true, the AST reader will try to load and use
1332 /// the global module index.
1333 ASTReader(Preprocessor &PP, ASTContext &Context, StringRef isysroot = "",
1334 bool DisableValidation = false,
1335 bool AllowASTWithCompilerErrors = false,
1336 bool AllowConfigurationMismatch = false,
1337 bool ValidateSystemInputs = false,
1338 bool UseGlobalIndex = true);
1342 SourceManager &getSourceManager() const { return SourceMgr; }
1343 FileManager &getFileManager() const { return FileMgr; }
1345 /// \brief Flags that indicate what kind of AST loading failures the client
1346 /// of the AST reader can directly handle.
1348 /// When a client states that it can handle a particular kind of failure,
1349 /// the AST reader will not emit errors when producing that kind of failure.
1350 enum LoadFailureCapabilities {
1351 /// \brief The client can't handle any AST loading failures.
1353 /// \brief The client can handle an AST file that cannot load because it
1356 /// \brief The client can handle an AST file that cannot load because it
1357 /// is out-of-date relative to its input files.
1358 ARR_OutOfDate = 0x2,
1359 /// \brief The client can handle an AST file that cannot load because it
1360 /// was built with a different version of Clang.
1361 ARR_VersionMismatch = 0x4,
1362 /// \brief The client can handle an AST file that cannot load because it's
1363 /// compiled configuration doesn't match that of the context it was
1365 ARR_ConfigurationMismatch = 0x8
1368 /// \brief Load the AST file designated by the given file name.
1370 /// \param FileName The name of the AST file to load.
1372 /// \param Type The kind of AST being loaded, e.g., PCH, module, main file,
1375 /// \param ImportLoc the location where the module file will be considered as
1376 /// imported from. For non-module AST types it should be invalid.
1378 /// \param ClientLoadCapabilities The set of client load-failure
1379 /// capabilities, represented as a bitset of the enumerators of
1380 /// LoadFailureCapabilities.
1381 ASTReadResult ReadAST(const std::string &FileName, ModuleKind Type,
1382 SourceLocation ImportLoc,
1383 unsigned ClientLoadCapabilities);
1385 /// \brief Make the entities in the given module and any of its (non-explicit)
1386 /// submodules visible to name lookup.
1388 /// \param Mod The module whose names should be made visible.
1390 /// \param NameVisibility The level of visibility to give the names in the
1391 /// module. Visibility can only be increased over time.
1393 /// \param ImportLoc The location at which the import occurs.
1395 /// \param Complain Whether to complain about conflicting module imports.
1396 void makeModuleVisible(Module *Mod,
1397 Module::NameVisibilityKind NameVisibility,
1398 SourceLocation ImportLoc,
1401 /// \brief Make the names within this set of hidden names visible.
1402 void makeNamesVisible(const HiddenNames &Names, Module *Owner,
1403 bool FromFinalization);
1405 /// \brief Take the AST callbacks listener.
1406 std::unique_ptr<ASTReaderListener> takeListener() {
1407 return std::move(Listener);
1410 /// \brief Set the AST callbacks listener.
1411 void setListener(std::unique_ptr<ASTReaderListener> Listener) {
1412 this->Listener = std::move(Listener);
1415 /// \brief Add an AST callback listener.
1417 /// Takes ownership of \p L.
1418 void addListener(std::unique_ptr<ASTReaderListener> L) {
1420 L = llvm::make_unique<ChainedASTReaderListener>(std::move(L),
1421 std::move(Listener));
1422 Listener = std::move(L);
1425 /// RAII object to temporarily add an AST callback listener.
1426 class ListenerScope {
1431 ListenerScope(ASTReader &Reader, std::unique_ptr<ASTReaderListener> L)
1432 : Reader(Reader), Chained(false) {
1433 auto Old = Reader.takeListener();
1436 L = llvm::make_unique<ChainedASTReaderListener>(std::move(L),
1439 Reader.setListener(std::move(L));
1442 auto New = Reader.takeListener();
1444 Reader.setListener(static_cast<ChainedASTReaderListener *>(New.get())
1449 /// \brief Set the AST deserialization listener.
1450 void setDeserializationListener(ASTDeserializationListener *Listener,
1451 bool TakeOwnership = false);
1453 /// \brief Determine whether this AST reader has a global index.
1454 bool hasGlobalIndex() const { return (bool)GlobalIndex; }
1456 /// \brief Return global module index.
1457 GlobalModuleIndex *getGlobalIndex() { return GlobalIndex.get(); }
1459 /// \brief Reset reader for a reload try.
1460 void resetForReload() { TriedLoadingGlobalIndex = false; }
1462 /// \brief Attempts to load the global index.
1464 /// \returns true if loading the global index has failed for any reason.
1465 bool loadGlobalIndex();
1467 /// \brief Determine whether we tried to load the global index, but failed,
1468 /// e.g., because it is out-of-date or does not exist.
1469 bool isGlobalIndexUnavailable() const;
1471 /// \brief Initializes the ASTContext
1472 void InitializeContext();
1474 /// \brief Update the state of Sema after loading some additional modules.
1477 /// \brief Add in-memory (virtual file) buffer.
1478 void addInMemoryBuffer(StringRef &FileName,
1479 std::unique_ptr<llvm::MemoryBuffer> Buffer) {
1480 ModuleMgr.addInMemoryBuffer(FileName, std::move(Buffer));
1483 /// \brief Finalizes the AST reader's state before writing an AST file to
1486 /// This operation may undo temporary state in the AST that should not be
1488 void finalizeForWriting();
1490 /// \brief Retrieve the module manager.
1491 ModuleManager &getModuleManager() { return ModuleMgr; }
1493 /// \brief Retrieve the preprocessor.
1494 Preprocessor &getPreprocessor() const { return PP; }
1496 /// \brief Retrieve the name of the original source file name for the primary
1498 StringRef getOriginalSourceFile() {
1499 return ModuleMgr.getPrimaryModule().OriginalSourceFileName;
1502 /// \brief Retrieve the name of the original source file name directly from
1503 /// the AST file, without actually loading the AST file.
1504 static std::string getOriginalSourceFile(const std::string &ASTFileName,
1505 FileManager &FileMgr,
1506 DiagnosticsEngine &Diags);
1508 /// \brief Read the control block for the named AST file.
1510 /// \returns true if an error occurred, false otherwise.
1511 static bool readASTFileControlBlock(StringRef Filename,
1512 FileManager &FileMgr,
1513 ASTReaderListener &Listener);
1515 /// \brief Determine whether the given AST file is acceptable to load into a
1516 /// translation unit with the given language and target options.
1517 static bool isAcceptableASTFile(StringRef Filename,
1518 FileManager &FileMgr,
1519 const LangOptions &LangOpts,
1520 const TargetOptions &TargetOpts,
1521 const PreprocessorOptions &PPOpts);
1523 /// \brief Returns the suggested contents of the predefines buffer,
1524 /// which contains a (typically-empty) subset of the predefines
1525 /// build prior to including the precompiled header.
1526 const std::string &getSuggestedPredefines() { return SuggestedPredefines; }
1528 /// \brief Read a preallocated preprocessed entity from the external source.
1530 /// \returns null if an error occurred that prevented the preprocessed
1531 /// entity from being loaded.
1532 PreprocessedEntity *ReadPreprocessedEntity(unsigned Index) override;
1534 /// \brief Returns a pair of [Begin, End) indices of preallocated
1535 /// preprocessed entities that \p Range encompasses.
1536 std::pair<unsigned, unsigned>
1537 findPreprocessedEntitiesInRange(SourceRange Range) override;
1539 /// \brief Optionally returns true or false if the preallocated preprocessed
1540 /// entity with index \p Index came from file \p FID.
1541 Optional<bool> isPreprocessedEntityInFileID(unsigned Index,
1542 FileID FID) override;
1544 /// \brief Read the header file information for the given file entry.
1545 HeaderFileInfo GetHeaderFileInfo(const FileEntry *FE) override;
1547 void ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag);
1549 /// \brief Returns the number of source locations found in the chain.
1550 unsigned getTotalNumSLocs() const {
1551 return TotalNumSLocEntries;
1554 /// \brief Returns the number of identifiers found in the chain.
1555 unsigned getTotalNumIdentifiers() const {
1556 return static_cast<unsigned>(IdentifiersLoaded.size());
1559 /// \brief Returns the number of macros found in the chain.
1560 unsigned getTotalNumMacros() const {
1561 return static_cast<unsigned>(MacrosLoaded.size());
1564 /// \brief Returns the number of types found in the chain.
1565 unsigned getTotalNumTypes() const {
1566 return static_cast<unsigned>(TypesLoaded.size());
1569 /// \brief Returns the number of declarations found in the chain.
1570 unsigned getTotalNumDecls() const {
1571 return static_cast<unsigned>(DeclsLoaded.size());
1574 /// \brief Returns the number of submodules known.
1575 unsigned getTotalNumSubmodules() const {
1576 return static_cast<unsigned>(SubmodulesLoaded.size());
1579 /// \brief Returns the number of selectors found in the chain.
1580 unsigned getTotalNumSelectors() const {
1581 return static_cast<unsigned>(SelectorsLoaded.size());
1584 /// \brief Returns the number of preprocessed entities known to the AST
1586 unsigned getTotalNumPreprocessedEntities() const {
1587 unsigned Result = 0;
1588 for (ModuleConstIterator I = ModuleMgr.begin(),
1589 E = ModuleMgr.end(); I != E; ++I) {
1590 Result += (*I)->NumPreprocessedEntities;
1596 /// \brief Returns the number of C++ base specifiers found in the chain.
1597 unsigned getTotalNumCXXBaseSpecifiers() const {
1598 return NumCXXBaseSpecifiersLoaded;
1601 /// \brief Reads a TemplateArgumentLocInfo appropriate for the
1602 /// given TemplateArgument kind.
1603 TemplateArgumentLocInfo
1604 GetTemplateArgumentLocInfo(ModuleFile &F, TemplateArgument::ArgKind Kind,
1605 const RecordData &Record, unsigned &Idx);
1607 /// \brief Reads a TemplateArgumentLoc.
1609 ReadTemplateArgumentLoc(ModuleFile &F,
1610 const RecordData &Record, unsigned &Idx);
1612 const ASTTemplateArgumentListInfo*
1613 ReadASTTemplateArgumentListInfo(ModuleFile &F,
1614 const RecordData &Record, unsigned &Index);
1616 /// \brief Reads a declarator info from the given record.
1617 TypeSourceInfo *GetTypeSourceInfo(ModuleFile &F,
1618 const RecordData &Record, unsigned &Idx);
1620 /// \brief Resolve a type ID into a type, potentially building a new
1622 QualType GetType(serialization::TypeID ID);
1624 /// \brief Resolve a local type ID within a given AST file into a type.
1625 QualType getLocalType(ModuleFile &F, unsigned LocalID);
1627 /// \brief Map a local type ID within a given AST file into a global type ID.
1628 serialization::TypeID getGlobalTypeID(ModuleFile &F, unsigned LocalID) const;
1630 /// \brief Read a type from the current position in the given record, which
1631 /// was read from the given AST file.
1632 QualType readType(ModuleFile &F, const RecordData &Record, unsigned &Idx) {
1633 if (Idx >= Record.size())
1636 return getLocalType(F, Record[Idx++]);
1639 /// \brief Map from a local declaration ID within a given module to a
1640 /// global declaration ID.
1641 serialization::DeclID getGlobalDeclID(ModuleFile &F,
1642 serialization::LocalDeclID LocalID) const;
1644 /// \brief Returns true if global DeclID \p ID originated from module \p M.
1645 bool isDeclIDFromModule(serialization::GlobalDeclID ID, ModuleFile &M) const;
1647 /// \brief Retrieve the module file that owns the given declaration, or NULL
1648 /// if the declaration is not from a module file.
1649 ModuleFile *getOwningModuleFile(const Decl *D);
1651 /// \brief Get the best name we know for the module that owns the given
1652 /// declaration, or an empty string if the declaration is not from a module.
1653 std::string getOwningModuleNameForDiagnostic(const Decl *D);
1655 /// \brief Returns the source location for the decl \p ID.
1656 SourceLocation getSourceLocationForDeclID(serialization::GlobalDeclID ID);
1658 /// \brief Resolve a declaration ID into a declaration, potentially
1659 /// building a new declaration.
1660 Decl *GetDecl(serialization::DeclID ID);
1661 Decl *GetExternalDecl(uint32_t ID) override;
1663 /// \brief Resolve a declaration ID into a declaration. Return 0 if it's not
1664 /// been loaded yet.
1665 Decl *GetExistingDecl(serialization::DeclID ID);
1667 /// \brief Reads a declaration with the given local ID in the given module.
1668 Decl *GetLocalDecl(ModuleFile &F, uint32_t LocalID) {
1669 return GetDecl(getGlobalDeclID(F, LocalID));
1672 /// \brief Reads a declaration with the given local ID in the given module.
1674 /// \returns The requested declaration, casted to the given return type.
1675 template<typename T>
1676 T *GetLocalDeclAs(ModuleFile &F, uint32_t LocalID) {
1677 return cast_or_null<T>(GetLocalDecl(F, LocalID));
1680 /// \brief Map a global declaration ID into the declaration ID used to
1681 /// refer to this declaration within the given module fule.
1683 /// \returns the global ID of the given declaration as known in the given
1685 serialization::DeclID
1686 mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
1687 serialization::DeclID GlobalID);
1689 /// \brief Reads a declaration ID from the given position in a record in the
1692 /// \returns The declaration ID read from the record, adjusted to a global ID.
1693 serialization::DeclID ReadDeclID(ModuleFile &F, const RecordData &Record,
1696 /// \brief Reads a declaration from the given position in a record in the
1698 Decl *ReadDecl(ModuleFile &F, const RecordData &R, unsigned &I) {
1699 return GetDecl(ReadDeclID(F, R, I));
1702 /// \brief Reads a declaration from the given position in a record in the
1705 /// \returns The declaration read from this location, casted to the given
1707 template<typename T>
1708 T *ReadDeclAs(ModuleFile &F, const RecordData &R, unsigned &I) {
1709 return cast_or_null<T>(GetDecl(ReadDeclID(F, R, I)));
1712 /// \brief If any redeclarations of \p D have been imported since it was
1713 /// last checked, this digs out those redeclarations and adds them to the
1714 /// redeclaration chain for \p D.
1715 void CompleteRedeclChain(const Decl *D) override;
1717 /// \brief Read a CXXBaseSpecifiers ID form the given record and
1718 /// return its global bit offset.
1719 uint64_t readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
1722 CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset) override;
1724 /// \brief Resolve the offset of a statement into a statement.
1726 /// This operation will read a new statement from the external
1727 /// source each time it is called, and is meant to be used via a
1728 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
1729 Stmt *GetExternalDeclStmt(uint64_t Offset) override;
1731 /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1732 /// specified cursor. Read the abbreviations that are at the top of the block
1733 /// and then leave the cursor pointing into the block.
1734 bool ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor, unsigned BlockID);
1736 /// \brief Finds all the visible declarations with a given name.
1737 /// The current implementation of this method just loads the entire
1738 /// lookup table as unmaterialized references.
1739 bool FindExternalVisibleDeclsByName(const DeclContext *DC,
1740 DeclarationName Name) override;
1742 /// \brief Read all of the declarations lexically stored in a
1743 /// declaration context.
1745 /// \param DC The declaration context whose declarations will be
1748 /// \param Decls Vector that will contain the declarations loaded
1749 /// from the external source. The caller is responsible for merging
1750 /// these declarations with any declarations already stored in the
1751 /// declaration context.
1753 /// \returns true if there was an error while reading the
1754 /// declarations for this declaration context.
1755 ExternalLoadResult FindExternalLexicalDecls(const DeclContext *DC,
1756 bool (*isKindWeWant)(Decl::Kind),
1757 SmallVectorImpl<Decl*> &Decls) override;
1759 /// \brief Get the decls that are contained in a file in the Offset/Length
1760 /// range. \p Length can be 0 to indicate a point at \p Offset instead of
1762 void FindFileRegionDecls(FileID File, unsigned Offset, unsigned Length,
1763 SmallVectorImpl<Decl *> &Decls) override;
1765 /// \brief Notify ASTReader that we started deserialization of
1766 /// a decl or type so until FinishedDeserializing is called there may be
1767 /// decls that are initializing. Must be paired with FinishedDeserializing.
1768 void StartedDeserializing() override { ++NumCurrentElementsDeserializing; }
1770 /// \brief Notify ASTReader that we finished the deserialization of
1771 /// a decl or type. Must be paired with StartedDeserializing.
1772 void FinishedDeserializing() override;
1774 /// \brief Function that will be invoked when we begin parsing a new
1775 /// translation unit involving this external AST source.
1777 /// This function will provide all of the external definitions to
1778 /// the ASTConsumer.
1779 void StartTranslationUnit(ASTConsumer *Consumer) override;
1781 /// \brief Print some statistics about AST usage.
1782 void PrintStats() override;
1784 /// \brief Dump information about the AST reader to standard error.
1787 /// Return the amount of memory used by memory buffers, breaking down
1788 /// by heap-backed versus mmap'ed memory.
1789 void getMemoryBufferSizes(MemoryBufferSizes &sizes) const override;
1791 /// \brief Initialize the semantic source with the Sema instance
1792 /// being used to perform semantic analysis on the abstract syntax
1794 void InitializeSema(Sema &S) override;
1796 /// \brief Inform the semantic consumer that Sema is no longer available.
1797 void ForgetSema() override { SemaObj = nullptr; }
1799 /// \brief Retrieve the IdentifierInfo for the named identifier.
1801 /// This routine builds a new IdentifierInfo for the given identifier. If any
1802 /// declarations with this name are visible from translation unit scope, their
1803 /// declarations will be deserialized and introduced into the declaration
1804 /// chain of the identifier.
1805 virtual IdentifierInfo *get(const char *NameStart, const char *NameEnd);
1806 IdentifierInfo *get(StringRef Name) override {
1807 return get(Name.begin(), Name.end());
1810 /// \brief Retrieve an iterator into the set of all identifiers
1811 /// in all loaded AST files.
1812 IdentifierIterator *getIdentifiers() override;
1814 /// \brief Load the contents of the global method pool for a given
1816 void ReadMethodPool(Selector Sel) override;
1818 /// \brief Load the set of namespaces that are known to the external source,
1819 /// which will be used during typo correction.
1820 void ReadKnownNamespaces(
1821 SmallVectorImpl<NamespaceDecl *> &Namespaces) override;
1823 void ReadUndefinedButUsed(
1824 llvm::DenseMap<NamedDecl *, SourceLocation> &Undefined) override;
1826 void ReadTentativeDefinitions(
1827 SmallVectorImpl<VarDecl *> &TentativeDefs) override;
1829 void ReadUnusedFileScopedDecls(
1830 SmallVectorImpl<const DeclaratorDecl *> &Decls) override;
1832 void ReadDelegatingConstructors(
1833 SmallVectorImpl<CXXConstructorDecl *> &Decls) override;
1835 void ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) override;
1837 void ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) override;
1839 void ReadUnusedLocalTypedefNameCandidates(
1840 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) override;
1842 void ReadLocallyScopedExternCDecls(
1843 SmallVectorImpl<NamedDecl *> &Decls) override;
1845 void ReadReferencedSelectors(
1846 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) override;
1848 void ReadWeakUndeclaredIdentifiers(
1849 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WI) override;
1851 void ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) override;
1853 void ReadPendingInstantiations(
1854 SmallVectorImpl<std::pair<ValueDecl *,
1855 SourceLocation> > &Pending) override;
1857 void ReadLateParsedTemplates(
1858 llvm::DenseMap<const FunctionDecl *,
1859 LateParsedTemplate *> &LPTMap) override;
1861 /// \brief Load a selector from disk, registering its ID if it exists.
1862 void LoadSelector(Selector Sel);
1864 void SetIdentifierInfo(unsigned ID, IdentifierInfo *II);
1865 void SetGloballyVisibleDecls(IdentifierInfo *II,
1866 const SmallVectorImpl<uint32_t> &DeclIDs,
1867 SmallVectorImpl<Decl *> *Decls = nullptr);
1869 /// \brief Report a diagnostic.
1870 DiagnosticBuilder Diag(unsigned DiagID);
1872 /// \brief Report a diagnostic.
1873 DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID);
1875 IdentifierInfo *DecodeIdentifierInfo(serialization::IdentifierID ID);
1877 IdentifierInfo *GetIdentifierInfo(ModuleFile &M, const RecordData &Record,
1879 return DecodeIdentifierInfo(getGlobalIdentifierID(M, Record[Idx++]));
1882 IdentifierInfo *GetIdentifier(serialization::IdentifierID ID) override {
1883 // Note that we are loading an identifier.
1884 Deserializing AnIdentifier(this);
1886 return DecodeIdentifierInfo(ID);
1889 IdentifierInfo *getLocalIdentifier(ModuleFile &M, unsigned LocalID);
1891 serialization::IdentifierID getGlobalIdentifierID(ModuleFile &M,
1894 ModuleMacroInfo *getModuleMacro(const PendingMacroInfo &PMInfo);
1896 void resolvePendingMacro(IdentifierInfo *II, const PendingMacroInfo &PMInfo);
1898 void installPCHMacroDirectives(IdentifierInfo *II,
1899 ModuleFile &M, uint64_t Offset);
1901 void installImportedMacro(IdentifierInfo *II, ModuleMacroInfo *MMI,
1904 typedef llvm::TinyPtrVector<DefMacroDirective *> AmbiguousMacros;
1905 llvm::DenseMap<IdentifierInfo*, AmbiguousMacros> AmbiguousMacroDefs;
1908 removeOverriddenMacros(IdentifierInfo *II, SourceLocation Loc,
1909 AmbiguousMacros &Ambig,
1910 ArrayRef<serialization::SubmoduleID> Overrides);
1913 removeOverriddenMacros(IdentifierInfo *II, SourceLocation Loc,
1914 ArrayRef<serialization::SubmoduleID> Overrides);
1916 /// \brief Retrieve the macro with the given ID.
1917 MacroInfo *getMacro(serialization::MacroID ID);
1919 /// \brief Retrieve the global macro ID corresponding to the given local
1920 /// ID within the given module file.
1921 serialization::MacroID getGlobalMacroID(ModuleFile &M, unsigned LocalID);
1923 /// \brief Read the source location entry with index ID.
1924 bool ReadSLocEntry(int ID) override;
1926 /// \brief Retrieve the module import location and module name for the
1927 /// given source manager entry ID.
1928 std::pair<SourceLocation, StringRef> getModuleImportLoc(int ID) override;
1930 /// \brief Retrieve the global submodule ID given a module and its local ID
1932 serialization::SubmoduleID
1933 getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID);
1935 /// \brief Retrieve the submodule that corresponds to a global submodule ID.
1937 Module *getSubmodule(serialization::SubmoduleID GlobalID);
1939 /// \brief Retrieve the module that corresponds to the given module ID.
1941 /// Note: overrides method in ExternalASTSource
1942 Module *getModule(unsigned ID) override;
1944 /// \brief Retrieve a selector from the given module with its local ID
1946 Selector getLocalSelector(ModuleFile &M, unsigned LocalID);
1948 Selector DecodeSelector(serialization::SelectorID Idx);
1950 Selector GetExternalSelector(serialization::SelectorID ID) override;
1951 uint32_t GetNumExternalSelectors() override;
1953 Selector ReadSelector(ModuleFile &M, const RecordData &Record, unsigned &Idx) {
1954 return getLocalSelector(M, Record[Idx++]);
1957 /// \brief Retrieve the global selector ID that corresponds to this
1958 /// the local selector ID in a given module.
1959 serialization::SelectorID getGlobalSelectorID(ModuleFile &F,
1960 unsigned LocalID) const;
1962 /// \brief Read a declaration name.
1963 DeclarationName ReadDeclarationName(ModuleFile &F,
1964 const RecordData &Record, unsigned &Idx);
1965 void ReadDeclarationNameLoc(ModuleFile &F,
1966 DeclarationNameLoc &DNLoc, DeclarationName Name,
1967 const RecordData &Record, unsigned &Idx);
1968 void ReadDeclarationNameInfo(ModuleFile &F, DeclarationNameInfo &NameInfo,
1969 const RecordData &Record, unsigned &Idx);
1971 void ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
1972 const RecordData &Record, unsigned &Idx);
1974 NestedNameSpecifier *ReadNestedNameSpecifier(ModuleFile &F,
1975 const RecordData &Record,
1978 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc(ModuleFile &F,
1979 const RecordData &Record,
1982 /// \brief Read a template name.
1983 TemplateName ReadTemplateName(ModuleFile &F, const RecordData &Record,
1986 /// \brief Read a template argument.
1987 TemplateArgument ReadTemplateArgument(ModuleFile &F,
1988 const RecordData &Record,unsigned &Idx);
1990 /// \brief Read a template parameter list.
1991 TemplateParameterList *ReadTemplateParameterList(ModuleFile &F,
1992 const RecordData &Record,
1995 /// \brief Read a template argument array.
1997 ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
1998 ModuleFile &F, const RecordData &Record,
2001 /// \brief Read a UnresolvedSet structure.
2002 void ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
2003 const RecordData &Record, unsigned &Idx);
2005 /// \brief Read a C++ base specifier.
2006 CXXBaseSpecifier ReadCXXBaseSpecifier(ModuleFile &F,
2007 const RecordData &Record,unsigned &Idx);
2009 /// \brief Read a CXXCtorInitializer array.
2010 std::pair<CXXCtorInitializer **, unsigned>
2011 ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
2014 /// \brief Read a source location from raw form.
2015 SourceLocation ReadSourceLocation(ModuleFile &ModuleFile, unsigned Raw) const {
2016 SourceLocation Loc = SourceLocation::getFromRawEncoding(Raw);
2017 assert(ModuleFile.SLocRemap.find(Loc.getOffset()) != ModuleFile.SLocRemap.end() &&
2018 "Cannot find offset to remap.");
2019 int Remap = ModuleFile.SLocRemap.find(Loc.getOffset())->second;
2020 return Loc.getLocWithOffset(Remap);
2023 /// \brief Read a source location.
2024 SourceLocation ReadSourceLocation(ModuleFile &ModuleFile,
2025 const RecordDataImpl &Record,
2027 return ReadSourceLocation(ModuleFile, Record[Idx++]);
2030 /// \brief Read a source range.
2031 SourceRange ReadSourceRange(ModuleFile &F,
2032 const RecordData &Record, unsigned &Idx);
2034 /// \brief Read an integral value
2035 llvm::APInt ReadAPInt(const RecordData &Record, unsigned &Idx);
2037 /// \brief Read a signed integral value
2038 llvm::APSInt ReadAPSInt(const RecordData &Record, unsigned &Idx);
2040 /// \brief Read a floating-point value
2041 llvm::APFloat ReadAPFloat(const RecordData &Record,
2042 const llvm::fltSemantics &Sem, unsigned &Idx);
2044 // \brief Read a string
2045 static std::string ReadString(const RecordData &Record, unsigned &Idx);
2047 // \brief Read a path
2048 std::string ReadPath(ModuleFile &F, const RecordData &Record, unsigned &Idx);
2050 /// \brief Read a version tuple.
2051 static VersionTuple ReadVersionTuple(const RecordData &Record, unsigned &Idx);
2053 CXXTemporary *ReadCXXTemporary(ModuleFile &F, const RecordData &Record,
2056 /// \brief Reads attributes from the current stream position.
2057 void ReadAttributes(ModuleFile &F, AttrVec &Attrs,
2058 const RecordData &Record, unsigned &Idx);
2060 /// \brief Reads a statement.
2061 Stmt *ReadStmt(ModuleFile &F);
2063 /// \brief Reads an expression.
2064 Expr *ReadExpr(ModuleFile &F);
2066 /// \brief Reads a sub-statement operand during statement reading.
2067 Stmt *ReadSubStmt() {
2068 assert(ReadingKind == Read_Stmt &&
2069 "Should be called only during statement reading!");
2070 // Subexpressions are stored from last to first, so the next Stmt we need
2071 // is at the back of the stack.
2072 assert(!StmtStack.empty() && "Read too many sub-statements!");
2073 return StmtStack.pop_back_val();
2076 /// \brief Reads a sub-expression operand during statement reading.
2077 Expr *ReadSubExpr();
2079 /// \brief Reads a token out of a record.
2080 Token ReadToken(ModuleFile &M, const RecordDataImpl &Record, unsigned &Idx);
2082 /// \brief Reads the macro record located at the given offset.
2083 MacroInfo *ReadMacroRecord(ModuleFile &F, uint64_t Offset);
2085 /// \brief Determine the global preprocessed entity ID that corresponds to
2086 /// the given local ID within the given module.
2087 serialization::PreprocessedEntityID
2088 getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const;
2090 /// \brief Add a macro to resolve imported from a module.
2092 /// \param II The name of the macro.
2093 /// \param M The module file.
2094 /// \param GMacID The global macro ID that is associated with this identifier.
2095 void addPendingMacroFromModule(IdentifierInfo *II,
2097 serialization::GlobalMacroID GMacID,
2098 ArrayRef<serialization::SubmoduleID>);
2100 /// \brief Add a macro to deserialize its macro directive history from a PCH.
2102 /// \param II The name of the macro.
2103 /// \param M The module file.
2104 /// \param MacroDirectivesOffset Offset of the serialized macro directive
2106 void addPendingMacroFromPCH(IdentifierInfo *II,
2107 ModuleFile *M, uint64_t MacroDirectivesOffset);
2109 /// \brief Read the set of macros defined by this external macro source.
2110 void ReadDefinedMacros() override;
2112 /// \brief Update an out-of-date identifier.
2113 void updateOutOfDateIdentifier(IdentifierInfo &II) override;
2115 /// \brief Note that this identifier is up-to-date.
2116 void markIdentifierUpToDate(IdentifierInfo *II);
2118 /// \brief Load all external visible decls in the given DeclContext.
2119 void completeVisibleDeclsMap(const DeclContext *DC) override;
2121 /// \brief Retrieve the AST context that this AST reader supplements.
2122 ASTContext &getContext() { return Context; }
2124 // \brief Contains the IDs for declarations that were requested before we have
2125 // access to a Sema object.
2126 SmallVector<uint64_t, 16> PreloadedDeclIDs;
2128 /// \brief Retrieve the semantic analysis object used to analyze the
2129 /// translation unit in which the precompiled header is being
2131 Sema *getSema() { return SemaObj; }
2133 /// \brief Retrieve the identifier table associated with the
2135 IdentifierTable &getIdentifierTable();
2137 /// \brief Record that the given ID maps to the given switch-case
2139 void RecordSwitchCaseID(SwitchCase *SC, unsigned ID);
2141 /// \brief Retrieve the switch-case statement with the given ID.
2142 SwitchCase *getSwitchCaseWithID(unsigned ID);
2144 void ClearSwitchCaseIDs();
2146 /// \brief Cursors for comments blocks.
2147 SmallVector<std::pair<llvm::BitstreamCursor,
2148 serialization::ModuleFile *>, 8> CommentsCursors;
2150 //RIDErief Loads comments ranges.
2151 void ReadComments() override;
2153 /// Return all input files for the given module file.
2154 void getInputFiles(ModuleFile &F,
2155 SmallVectorImpl<serialization::InputFile> &Files);
2158 /// \brief Helper class that saves the current stream position and
2159 /// then restores it when destroyed.
2160 struct SavedStreamPosition {
2161 explicit SavedStreamPosition(llvm::BitstreamCursor &Cursor)
2162 : Cursor(Cursor), Offset(Cursor.GetCurrentBitNo()) { }
2164 ~SavedStreamPosition() {
2165 Cursor.JumpToBit(Offset);
2169 llvm::BitstreamCursor &Cursor;
2173 inline void PCHValidator::Error(const char *Msg) {
2177 } // end namespace clang