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_FRONTEND_AST_READER_H
15 #define LLVM_CLANG_FRONTEND_AST_READER_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/DenseSet.h"
38 #include "llvm/ADT/MapVector.h"
39 #include "llvm/ADT/OwningPtr.h"
40 #include "llvm/ADT/SmallPtrSet.h"
41 #include "llvm/ADT/SmallSet.h"
42 #include "llvm/ADT/SmallVector.h"
43 #include "llvm/ADT/StringRef.h"
44 #include "llvm/Bitcode/BitstreamReader.h"
45 #include "llvm/Support/DataTypes.h"
62 class ASTIdentifierIterator;
63 class ASTUnit; // FIXME: Layering violation and egregious hack.
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 /// \brief Receives the language options.
113 /// \returns true to indicate the options are invalid or false otherwise.
114 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
119 /// \brief Receives the target options.
121 /// \returns true to indicate the target options are invalid, or false
123 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
128 /// \brief Receives the diagnostic options.
130 /// \returns true to indicate the diagnostic options are invalid, or false
132 virtual bool ReadDiagnosticOptions(const DiagnosticOptions &DiagOpts,
137 /// \brief Receives the file system options.
139 /// \returns true to indicate the file system options are invalid, or false
141 virtual bool ReadFileSystemOptions(const FileSystemOptions &FSOpts,
146 /// \brief Receives the header search options.
148 /// \returns true to indicate the header search options are invalid, or false
150 virtual bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
155 /// \brief Receives the preprocessor options.
157 /// \param SuggestedPredefines Can be filled in with the set of predefines
158 /// that are suggested by the preprocessor options. Typically only used when
159 /// loading a precompiled header.
161 /// \returns true to indicate the preprocessor options are invalid, or false
163 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
165 std::string &SuggestedPredefines) {
169 /// \brief Receives __COUNTER__ value.
170 virtual void ReadCounter(const serialization::ModuleFile &M,
173 /// \brief Returns true if this \c ASTReaderListener wants to receive the
174 /// input files of the AST file via \c visitInputFile, false otherwise.
175 virtual bool needsInputFileVisitation() { return false; }
177 /// \brief if \c needsInputFileVisitation returns true, this is called for each
178 /// input file of the AST file.
180 /// \returns true to continue receiving the next input file, false to stop.
181 virtual bool visitInputFile(StringRef Filename, bool isSystem) { return true;}
184 /// \brief ASTReaderListener implementation to validate the information of
185 /// the PCH file against an initialized Preprocessor.
186 class PCHValidator : public ASTReaderListener {
191 PCHValidator(Preprocessor &PP, ASTReader &Reader)
192 : PP(PP), Reader(Reader) {}
194 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
196 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
198 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
200 std::string &SuggestedPredefines);
201 virtual void ReadCounter(const serialization::ModuleFile &M, unsigned Value);
204 void Error(const char *Msg);
207 namespace serialization {
209 class ReadMethodPoolVisitor;
212 class ASTIdentifierLookupTrait;
213 /// \brief The on-disk hash table used for the DeclContext's Name lookup table.
214 typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
215 ASTDeclContextNameLookupTable;
218 } // end namespace serialization
220 /// \brief Reads an AST files chain containing the contents of a translation
223 /// The ASTReader class reads bitstreams (produced by the ASTWriter
224 /// class) containing the serialized representation of a given
225 /// abstract syntax tree and its supporting data structures. An
226 /// instance of the ASTReader can be attached to an ASTContext object,
227 /// which will provide access to the contents of the AST files.
229 /// The AST reader provides lazy de-serialization of declarations, as
230 /// required when traversing the AST. Only those AST nodes that are
231 /// actually required will be de-serialized.
233 : public ExternalPreprocessorSource,
234 public ExternalPreprocessingRecordSource,
235 public ExternalHeaderFileInfoSource,
236 public ExternalSemaSource,
237 public IdentifierInfoLookup,
238 public ExternalIdentifierLookup,
239 public ExternalSLocEntrySource
242 typedef SmallVector<uint64_t, 64> RecordData;
243 typedef SmallVectorImpl<uint64_t> RecordDataImpl;
245 /// \brief The result of reading the control block of an AST file, which
246 /// can fail for various reasons.
248 /// \brief The control block was read successfully. Aside from failures,
249 /// the AST file is safe to read into the current context.
251 /// \brief The AST file itself appears corrupted.
253 /// \brief The AST file was missing.
255 /// \brief The AST file is out-of-date relative to its input files,
256 /// and needs to be regenerated.
258 /// \brief The AST file was written by a different version of Clang.
260 /// \brief The AST file was writtten with a different language/target
262 ConfigurationMismatch,
263 /// \brief The AST file has errors.
267 /// \brief Types of AST files.
268 friend class PCHValidator;
269 friend class ASTDeclReader;
270 friend class ASTStmtReader;
271 friend class ASTIdentifierIterator;
272 friend class serialization::reader::ASTIdentifierLookupTrait;
273 friend class TypeLocReader;
274 friend class ASTWriter;
275 friend class ASTUnit; // ASTUnit needs to remap source locations.
276 friend class serialization::ReadMethodPoolVisitor;
278 typedef serialization::ModuleFile ModuleFile;
279 typedef serialization::ModuleKind ModuleKind;
280 typedef serialization::ModuleManager ModuleManager;
282 typedef ModuleManager::ModuleIterator ModuleIterator;
283 typedef ModuleManager::ModuleConstIterator ModuleConstIterator;
284 typedef ModuleManager::ModuleReverseIterator ModuleReverseIterator;
287 /// \brief The receiver of some callbacks invoked by ASTReader.
288 OwningPtr<ASTReaderListener> Listener;
290 /// \brief The receiver of deserialization events.
291 ASTDeserializationListener *DeserializationListener;
293 SourceManager &SourceMgr;
294 FileManager &FileMgr;
295 DiagnosticsEngine &Diags;
297 /// \brief The semantic analysis object that will be processing the
298 /// AST files and the translation unit that uses it.
301 /// \brief The preprocessor that will be loading the source file.
304 /// \brief The AST context into which we'll read the AST files.
307 /// \brief The AST consumer.
308 ASTConsumer *Consumer;
310 /// \brief The module manager which manages modules and their dependencies
311 ModuleManager ModuleMgr;
313 /// \brief The location where the module file will be considered as
314 /// imported from. For non-module AST types it should be invalid.
315 SourceLocation CurrentImportLoc;
317 /// \brief The global module index, if loaded.
318 llvm::OwningPtr<GlobalModuleIndex> GlobalIndex;
320 /// \brief A map of global bit offsets to the module that stores entities
321 /// at those bit offsets.
322 ContinuousRangeMap<uint64_t, ModuleFile*, 4> GlobalBitOffsetsMap;
324 /// \brief A map of negated SLocEntryIDs to the modules containing them.
325 ContinuousRangeMap<unsigned, ModuleFile*, 64> GlobalSLocEntryMap;
327 typedef ContinuousRangeMap<unsigned, ModuleFile*, 64> GlobalSLocOffsetMapType;
329 /// \brief A map of reversed (SourceManager::MaxLoadedOffset - SLocOffset)
330 /// SourceLocation offsets to the modules containing them.
331 GlobalSLocOffsetMapType GlobalSLocOffsetMap;
333 /// \brief Types that have already been loaded from the chain.
335 /// When the pointer at index I is non-NULL, the type with
336 /// ID = (I + 1) << FastQual::Width has already been loaded
337 std::vector<QualType> TypesLoaded;
339 typedef ContinuousRangeMap<serialization::TypeID, ModuleFile *, 4>
342 /// \brief Mapping from global type IDs to the module in which the
343 /// type resides along with the offset that should be added to the
344 /// global type ID to produce a local ID.
345 GlobalTypeMapType GlobalTypeMap;
347 /// \brief Declarations that have already been loaded from the chain.
349 /// When the pointer at index I is non-NULL, the declaration with ID
350 /// = I + 1 has already been loaded.
351 std::vector<Decl *> DeclsLoaded;
353 typedef ContinuousRangeMap<serialization::DeclID, ModuleFile *, 4>
356 /// \brief Mapping from global declaration IDs to the module in which the
357 /// declaration resides.
358 GlobalDeclMapType GlobalDeclMap;
360 typedef std::pair<ModuleFile *, uint64_t> FileOffset;
361 typedef SmallVector<FileOffset, 2> FileOffsetsTy;
362 typedef llvm::DenseMap<serialization::DeclID, FileOffsetsTy>
363 DeclUpdateOffsetsMap;
365 /// \brief Declarations that have modifications residing in a later file
367 DeclUpdateOffsetsMap DeclUpdateOffsets;
369 struct ReplacedDeclInfo {
374 ReplacedDeclInfo() : Mod(0), Offset(0), RawLoc(0) {}
375 ReplacedDeclInfo(ModuleFile *Mod, uint64_t Offset, unsigned RawLoc)
376 : Mod(Mod), Offset(Offset), RawLoc(RawLoc) {}
379 typedef llvm::DenseMap<serialization::DeclID, ReplacedDeclInfo>
381 /// \brief Declarations that have been replaced in a later file in the chain.
382 DeclReplacementMap ReplacedDecls;
384 struct FileDeclsInfo {
386 ArrayRef<serialization::LocalDeclID> Decls;
388 FileDeclsInfo() : Mod(0) {}
389 FileDeclsInfo(ModuleFile *Mod, ArrayRef<serialization::LocalDeclID> Decls)
390 : Mod(Mod), Decls(Decls) {}
393 /// \brief Map from a FileID to the file-level declarations that it contains.
394 llvm::DenseMap<FileID, FileDeclsInfo> FileDeclIDs;
396 // Updates for visible decls can occur for other contexts than just the
397 // TU, and when we read those update records, the actual context will not
398 // be available yet (unless it's the TU), so have this pending map using the
399 // ID as a key. It will be realized when the context is actually loaded.
401 SmallVector<std::pair<serialization::reader::ASTDeclContextNameLookupTable *,
402 ModuleFile*>, 1> DeclContextVisibleUpdates;
403 typedef llvm::DenseMap<serialization::DeclID, DeclContextVisibleUpdates>
404 DeclContextVisibleUpdatesPending;
406 /// \brief Updates to the visible declarations of declaration contexts that
407 /// haven't been loaded yet.
408 DeclContextVisibleUpdatesPending PendingVisibleUpdates;
410 /// \brief The set of C++ or Objective-C classes that have forward
411 /// declarations that have not yet been linked to their definitions.
412 llvm::SmallPtrSet<Decl *, 4> PendingDefinitions;
414 typedef llvm::MapVector<Decl *, uint64_t,
415 llvm::SmallDenseMap<Decl *, unsigned, 4>,
416 SmallVector<std::pair<Decl *, uint64_t>, 4> >
419 /// \brief Functions or methods that have bodies that will be attached.
420 PendingBodiesMap PendingBodies;
422 /// \brief Read the records that describe the contents of declcontexts.
423 bool ReadDeclContextStorage(ModuleFile &M,
424 llvm::BitstreamCursor &Cursor,
425 const std::pair<uint64_t, uint64_t> &Offsets,
426 serialization::DeclContextInfo &Info);
428 /// \brief A vector containing identifiers that have already been
431 /// If the pointer at index I is non-NULL, then it refers to the
432 /// IdentifierInfo for the identifier with ID=I+1 that has already
434 std::vector<IdentifierInfo *> IdentifiersLoaded;
436 typedef ContinuousRangeMap<serialization::IdentID, ModuleFile *, 4>
437 GlobalIdentifierMapType;
439 /// \brief Mapping from global identifier IDs to the module in which the
440 /// identifier resides along with the offset that should be added to the
441 /// global identifier ID to produce a local ID.
442 GlobalIdentifierMapType GlobalIdentifierMap;
444 /// \brief A vector containing macros that have already been
447 /// If the pointer at index I is non-NULL, then it refers to the
448 /// MacroInfo for the identifier with ID=I+1 that has already
450 std::vector<MacroInfo *> MacrosLoaded;
452 typedef ContinuousRangeMap<serialization::MacroID, ModuleFile *, 4>
455 /// \brief Mapping from global macro IDs to the module in which the
456 /// macro resides along with the offset that should be added to the
457 /// global macro ID to produce a local ID.
458 GlobalMacroMapType GlobalMacroMap;
460 /// \brief A vector containing submodules that have already been loaded.
462 /// This vector is indexed by the Submodule ID (-1). NULL submodule entries
463 /// indicate that the particular submodule ID has not yet been loaded.
464 SmallVector<Module *, 2> SubmodulesLoaded;
466 typedef ContinuousRangeMap<serialization::SubmoduleID, ModuleFile *, 4>
467 GlobalSubmoduleMapType;
469 /// \brief Mapping from global submodule IDs to the module file in which the
470 /// submodule resides along with the offset that should be added to the
471 /// global submodule ID to produce a local ID.
472 GlobalSubmoduleMapType GlobalSubmoduleMap;
474 /// \brief An entity that has been hidden.
491 HiddenName(Decl *D) : Kind(Declaration), D(D), Id() { }
493 HiddenName(IdentifierInfo *II, MacroDirective *MD)
494 : Kind(MacroVisibility), MD(MD), Id(II) { }
496 NameKind getKind() const { return Kind; }
498 Decl *getDecl() const {
499 assert(getKind() == Declaration && "Hidden name is not a declaration");
503 std::pair<IdentifierInfo *, MacroDirective *> getMacro() const {
504 assert(getKind() == MacroVisibility && "Hidden name is not a macro!");
505 return std::make_pair(Id, MD);
509 /// \brief A set of hidden declarations.
510 typedef SmallVector<HiddenName, 2> HiddenNames;
512 typedef llvm::DenseMap<Module *, HiddenNames> HiddenNamesMapType;
514 /// \brief A mapping from each of the hidden submodules to the deserialized
515 /// declarations in that submodule that could be made visible.
516 HiddenNamesMapType HiddenNamesMap;
519 /// \brief A module import, export, or conflict that hasn't yet been resolved.
520 struct UnresolvedModuleRef {
521 /// \brief The file in which this module resides.
524 /// \brief The module that is importing or exporting.
527 /// \brief The kind of module reference.
528 enum { Import, Export, Conflict } Kind;
530 /// \brief The local ID of the module that is being exported.
533 /// \brief Whether this is a wildcard export.
534 unsigned IsWildcard : 1;
536 /// \brief String data.
540 /// \brief The set of module imports and exports that still need to be
542 SmallVector<UnresolvedModuleRef, 2> UnresolvedModuleRefs;
544 /// \brief A vector containing selectors that have already been loaded.
546 /// This vector is indexed by the Selector ID (-1). NULL selector
547 /// entries indicate that the particular selector ID has not yet
549 SmallVector<Selector, 16> SelectorsLoaded;
551 typedef ContinuousRangeMap<serialization::SelectorID, ModuleFile *, 4>
552 GlobalSelectorMapType;
554 /// \brief Mapping from global selector IDs to the module in which the
556 /// global selector ID to produce a local ID.
557 GlobalSelectorMapType GlobalSelectorMap;
559 /// \brief The generation number of the last time we loaded data from the
560 /// global method pool for this selector.
561 llvm::DenseMap<Selector, unsigned> SelectorGeneration;
563 struct PendingMacroInfo {
566 struct ModuleMacroDataTy {
567 serialization::GlobalMacroID GMacID;
570 struct PCHMacroDataTy {
571 uint64_t MacroDirectivesOffset;
575 ModuleMacroDataTy ModuleMacroData;
576 PCHMacroDataTy PCHMacroData;
579 PendingMacroInfo(ModuleFile *M,
580 serialization::GlobalMacroID GMacID,
581 SourceLocation ImportLoc) : M(M) {
582 ModuleMacroData.GMacID = GMacID;
583 ModuleMacroData.ImportLoc = ImportLoc.getRawEncoding();
586 PendingMacroInfo(ModuleFile *M, uint64_t MacroDirectivesOffset) : M(M) {
587 PCHMacroData.MacroDirectivesOffset = MacroDirectivesOffset;
591 typedef llvm::MapVector<IdentifierInfo *, SmallVector<PendingMacroInfo, 2> >
594 /// \brief Mapping from identifiers that have a macro history to the global
595 /// IDs have not yet been deserialized to the global IDs of those macros.
596 PendingMacroIDsMap PendingMacroIDs;
598 typedef ContinuousRangeMap<unsigned, ModuleFile *, 4>
599 GlobalPreprocessedEntityMapType;
601 /// \brief Mapping from global preprocessing entity IDs to the module in
602 /// which the preprocessed entity resides along with the offset that should be
603 /// added to the global preprocessing entitiy ID to produce a local ID.
604 GlobalPreprocessedEntityMapType GlobalPreprocessedEntityMap;
606 /// \name CodeGen-relevant special data
607 /// \brief Fields containing data that is relevant to CodeGen.
610 /// \brief The IDs of all declarations that fulfill the criteria of
611 /// "interesting" decls.
613 /// This contains the data loaded from all EXTERNAL_DEFINITIONS blocks in the
614 /// chain. The referenced declarations are deserialized and passed to the
615 /// consumer eagerly.
616 SmallVector<uint64_t, 16> ExternalDefinitions;
618 /// \brief The IDs of all tentative definitions stored in the chain.
620 /// Sema keeps track of all tentative definitions in a TU because it has to
621 /// complete them and pass them on to CodeGen. Thus, tentative definitions in
622 /// the PCH chain must be eagerly deserialized.
623 SmallVector<uint64_t, 16> TentativeDefinitions;
625 /// \brief The IDs of all CXXRecordDecls stored in the chain whose VTables are
628 /// CodeGen has to emit VTables for these records, so they have to be eagerly
630 SmallVector<uint64_t, 64> VTableUses;
632 /// \brief A snapshot of the pending instantiations in the chain.
634 /// This record tracks the instantiations that Sema has to perform at the
635 /// end of the TU. It consists of a pair of values for every pending
636 /// instantiation where the first value is the ID of the decl and the second
637 /// is the instantiation location.
638 SmallVector<uint64_t, 64> PendingInstantiations;
642 /// \name DiagnosticsEngine-relevant special data
643 /// \brief Fields containing data that is used for generating diagnostics
646 /// \brief A snapshot of Sema's unused file-scoped variable tracking, for
647 /// generating warnings.
648 SmallVector<uint64_t, 16> UnusedFileScopedDecls;
650 /// \brief A list of all the delegating constructors we've seen, to diagnose
652 SmallVector<uint64_t, 4> DelegatingCtorDecls;
654 /// \brief Method selectors used in a @selector expression. Used for
655 /// implementation of -Wselector.
656 SmallVector<uint64_t, 64> ReferencedSelectorsData;
658 /// \brief A snapshot of Sema's weak undeclared identifier tracking, for
659 /// generating warnings.
660 SmallVector<uint64_t, 64> WeakUndeclaredIdentifiers;
662 /// \brief The IDs of type aliases for ext_vectors that exist in the chain.
664 /// Used by Sema for finding sugared names for ext_vectors in diagnostics.
665 SmallVector<uint64_t, 4> ExtVectorDecls;
669 /// \name Sema-relevant special data
670 /// \brief Fields containing data that is used for semantic analysis
673 /// \brief The IDs of all locally scoped extern "C" decls in the chain.
675 /// Sema tracks these to validate that the types are consistent across all
676 /// local extern "C" declarations.
677 SmallVector<uint64_t, 16> LocallyScopedExternCDecls;
679 /// \brief The IDs of all dynamic class declarations in the chain.
681 /// Sema tracks these because it checks for the key functions being defined
682 /// at the end of the TU, in which case it directs CodeGen to emit the VTable.
683 SmallVector<uint64_t, 16> DynamicClasses;
685 /// \brief The IDs of the declarations Sema stores directly.
687 /// Sema tracks a few important decls, such as namespace std, directly.
688 SmallVector<uint64_t, 4> SemaDeclRefs;
690 /// \brief The IDs of the types ASTContext stores directly.
692 /// The AST context tracks a few important types, such as va_list, directly.
693 SmallVector<uint64_t, 16> SpecialTypes;
695 /// \brief The IDs of CUDA-specific declarations ASTContext stores directly.
697 /// The AST context tracks a few important decls, currently cudaConfigureCall,
699 SmallVector<uint64_t, 2> CUDASpecialDeclRefs;
701 /// \brief The floating point pragma option settings.
702 SmallVector<uint64_t, 1> FPPragmaOptions;
704 /// \brief The OpenCL extension settings.
705 SmallVector<uint64_t, 1> OpenCLExtensions;
707 /// \brief A list of the namespaces we've seen.
708 SmallVector<uint64_t, 4> KnownNamespaces;
710 /// \brief A list of undefined decls with internal linkage followed by the
711 /// SourceLocation of a matching ODR-use.
712 SmallVector<uint64_t, 8> UndefinedButUsed;
714 // \brief A list of late parsed template function data.
715 SmallVector<uint64_t, 1> LateParsedTemplates;
717 /// \brief A list of modules that were imported by precompiled headers or
718 /// any other non-module AST file.
719 SmallVector<serialization::SubmoduleID, 2> ImportedModules;
722 /// \brief The directory that the PCH we are reading is stored in.
723 std::string CurrentDir;
725 /// \brief The system include root to be used when loading the
726 /// precompiled header.
727 std::string isysroot;
729 /// \brief Whether to disable the normal validation performed on precompiled
730 /// headers when they are loaded.
731 bool DisableValidation;
733 /// \brief Whether to accept an AST file with compiler errors.
734 bool AllowASTWithCompilerErrors;
736 /// \brief Whether we are allowed to use the global module index.
739 /// \brief Whether we have tried loading the global module index yet.
740 bool TriedLoadingGlobalIndex;
742 /// \brief The current "generation" of the module file import stack, which
743 /// indicates how many separate module file load operations have occurred.
744 unsigned CurrentGeneration;
746 typedef llvm::DenseMap<unsigned, SwitchCase *> SwitchCaseMapTy;
747 /// \brief Mapping from switch-case IDs in the chain to switch-case statements
749 /// Statements usually don't have IDs, but switch cases need them, so that the
750 /// switch statement can refer to them.
751 SwitchCaseMapTy SwitchCaseStmts;
753 SwitchCaseMapTy *CurrSwitchCaseStmts;
755 /// \brief The number of source location entries de-serialized from
757 unsigned NumSLocEntriesRead;
759 /// \brief The number of source location entries in the chain.
760 unsigned TotalNumSLocEntries;
762 /// \brief The number of statements (and expressions) de-serialized
764 unsigned NumStatementsRead;
766 /// \brief The total number of statements (and expressions) stored
768 unsigned TotalNumStatements;
770 /// \brief The number of macros de-serialized from the chain.
771 unsigned NumMacrosRead;
773 /// \brief The total number of macros stored in the chain.
774 unsigned TotalNumMacros;
776 /// \brief The number of lookups into identifier tables.
777 unsigned NumIdentifierLookups;
779 /// \brief The number of lookups into identifier tables that succeed.
780 unsigned NumIdentifierLookupHits;
782 /// \brief The number of selectors that have been read.
783 unsigned NumSelectorsRead;
785 /// \brief The number of method pool entries that have been read.
786 unsigned NumMethodPoolEntriesRead;
788 /// \brief The number of times we have looked up a selector in the method
790 unsigned NumMethodPoolLookups;
792 /// \brief The number of times we have looked up a selector in the method
793 /// pool and found something.
794 unsigned NumMethodPoolHits;
796 /// \brief The number of times we have looked up a selector in the method
797 /// pool within a specific module.
798 unsigned NumMethodPoolTableLookups;
800 /// \brief The number of times we have looked up a selector in the method
801 /// pool within a specific module and found something.
802 unsigned NumMethodPoolTableHits;
804 /// \brief The total number of method pool entries in the selector table.
805 unsigned TotalNumMethodPoolEntries;
807 /// Number of lexical decl contexts read/total.
808 unsigned NumLexicalDeclContextsRead, TotalLexicalDeclContexts;
810 /// Number of visible decl contexts read/total.
811 unsigned NumVisibleDeclContextsRead, TotalVisibleDeclContexts;
813 /// Total size of modules, in bits, currently loaded
814 uint64_t TotalModulesSizeInBits;
816 /// \brief Number of Decl/types that are currently deserializing.
817 unsigned NumCurrentElementsDeserializing;
819 /// \brief Set true while we are in the process of passing deserialized
820 /// "interesting" decls to consumer inside FinishedDeserializing().
821 /// This is used as a guard to avoid recursively repeating the process of
822 /// passing decls to consumer.
823 bool PassingDeclsToConsumer;
825 /// Number of CXX base specifiers currently loaded
826 unsigned NumCXXBaseSpecifiersLoaded;
828 /// \brief The set of identifiers that were read while the AST reader was
829 /// (recursively) loading declarations.
831 /// The declarations on the identifier chain for these identifiers will be
832 /// loaded once the recursive loading has completed.
833 llvm::MapVector<IdentifierInfo *, SmallVector<uint32_t, 4> >
834 PendingIdentifierInfos;
836 /// \brief The generation number of each identifier, which keeps track of
837 /// the last time we loaded information about this identifier.
838 llvm::DenseMap<IdentifierInfo *, unsigned> IdentifierGeneration;
840 /// \brief Contains declarations and definitions that will be
841 /// "interesting" to the ASTConsumer, when we get that AST consumer.
843 /// "Interesting" declarations are those that have data that may
844 /// need to be emitted, such as inline function definitions or
845 /// Objective-C protocols.
846 std::deque<Decl *> InterestingDecls;
848 /// \brief The set of redeclarable declarations that have been deserialized
849 /// since the last time the declaration chains were linked.
850 llvm::SmallPtrSet<Decl *, 16> RedeclsDeserialized;
852 /// \brief The list of redeclaration chains that still need to be
855 /// Each element is the global declaration ID of the first declaration in
856 /// the chain. Elements in this vector should be unique; use
857 /// PendingDeclChainsKnown to ensure uniqueness.
858 SmallVector<serialization::DeclID, 16> PendingDeclChains;
860 /// \brief Keeps track of the elements added to PendingDeclChains.
861 llvm::SmallSet<serialization::DeclID, 16> PendingDeclChainsKnown;
863 /// \brief The Decl IDs for the Sema/Lexical DeclContext of a Decl that has
864 /// been loaded but its DeclContext was not set yet.
865 struct PendingDeclContextInfo {
867 serialization::GlobalDeclID SemaDC;
868 serialization::GlobalDeclID LexicalDC;
871 /// \brief The set of Decls that have been loaded but their DeclContexts are
874 /// The DeclContexts for these Decls will be set once recursive loading has
876 std::deque<PendingDeclContextInfo> PendingDeclContextInfos;
878 /// \brief The set of NamedDecls that have been loaded, but are members of a
879 /// context that has been merged into another context where the corresponding
880 /// declaration is either missing or has not yet been loaded.
882 /// We will check whether the corresponding declaration is in fact missing
883 /// once recursing loading has been completed.
884 llvm::SmallVector<NamedDecl *, 16> PendingOdrMergeChecks;
886 /// \brief The set of Objective-C categories that have been deserialized
887 /// since the last time the declaration chains were linked.
888 llvm::SmallPtrSet<ObjCCategoryDecl *, 16> CategoriesDeserialized;
890 /// \brief The set of Objective-C class definitions that have already been
891 /// loaded, for which we will need to check for categories whenever a new
892 /// module is loaded.
893 SmallVector<ObjCInterfaceDecl *, 16> ObjCClassesLoaded;
895 typedef llvm::DenseMap<Decl *, SmallVector<serialization::DeclID, 2> >
898 /// \brief A mapping from canonical declarations to the set of additional
899 /// (global, previously-canonical) declaration IDs that have been merged with
900 /// that canonical declaration.
901 MergedDeclsMap MergedDecls;
903 typedef llvm::DenseMap<serialization::GlobalDeclID,
904 SmallVector<serialization::DeclID, 2> >
905 StoredMergedDeclsMap;
907 /// \brief A mapping from canonical declaration IDs to the set of additional
908 /// declaration IDs that have been merged with that canonical declaration.
910 /// This is the deserialized representation of the entries in MergedDecls.
911 /// When we query entries in MergedDecls, they will be augmented with entries
912 /// from StoredMergedDecls.
913 StoredMergedDeclsMap StoredMergedDecls;
915 /// \brief Combine the stored merged declarations for the given canonical
916 /// declaration into the set of merged declarations.
918 /// \returns An iterator into MergedDecls that corresponds to the position of
919 /// the given canonical declaration.
920 MergedDeclsMap::iterator
921 combineStoredMergedDecls(Decl *Canon, serialization::GlobalDeclID CanonID);
923 /// \brief A mapping from DeclContexts to the semantic DeclContext that we
924 /// are treating as the definition of the entity. This is used, for instance,
925 /// when merging implicit instantiations of class templates across modules.
926 llvm::DenseMap<DeclContext *, DeclContext *> MergedDeclContexts;
928 /// \brief A mapping from canonical declarations of enums to their canonical
929 /// definitions. Only populated when using modules in C++.
930 llvm::DenseMap<EnumDecl *, EnumDecl *> EnumDefinitions;
932 /// \brief When reading a Stmt tree, Stmt operands are placed in this stack.
933 SmallVector<Stmt *, 16> StmtStack;
935 /// \brief What kind of records we are reading.
937 Read_None, Read_Decl, Read_Type, Read_Stmt
940 /// \brief What kind of records we are reading.
941 ReadingKind ReadingKind;
943 /// \brief RAII object to change the reading kind.
944 class ReadingKindTracker {
946 enum ReadingKind PrevKind;
948 ReadingKindTracker(const ReadingKindTracker &) LLVM_DELETED_FUNCTION;
949 void operator=(const ReadingKindTracker &) LLVM_DELETED_FUNCTION;
952 ReadingKindTracker(enum ReadingKind newKind, ASTReader &reader)
953 : Reader(reader), PrevKind(Reader.ReadingKind) {
954 Reader.ReadingKind = newKind;
957 ~ReadingKindTracker() { Reader.ReadingKind = PrevKind; }
960 /// \brief Suggested contents of the predefines buffer, after this
961 /// PCH file has been processed.
963 /// In most cases, this string will be empty, because the predefines
964 /// buffer computed to build the PCH file will be identical to the
965 /// predefines buffer computed from the command line. However, when
966 /// there are differences that the PCH reader can work around, this
967 /// predefines buffer may contain additional definitions.
968 std::string SuggestedPredefines;
970 /// \brief Reads a statement from the specified cursor.
971 Stmt *ReadStmtFromStream(ModuleFile &F);
973 /// \brief Retrieve the file entry and 'overridden' bit for an input
974 /// file in the given module file.
975 serialization::InputFile getInputFile(ModuleFile &F, unsigned ID,
976 bool Complain = true);
978 /// \brief Get a FileEntry out of stored-in-PCH filename, making sure we take
979 /// into account all the necessary relocations.
980 const FileEntry *getFileEntry(StringRef filename);
982 void MaybeAddSystemRootToFilename(ModuleFile &M, std::string &Filename);
984 struct ImportedModule {
986 ModuleFile *ImportedBy;
987 SourceLocation ImportLoc;
989 ImportedModule(ModuleFile *Mod,
990 ModuleFile *ImportedBy,
991 SourceLocation ImportLoc)
992 : Mod(Mod), ImportedBy(ImportedBy), ImportLoc(ImportLoc) { }
995 ASTReadResult ReadASTCore(StringRef FileName, ModuleKind Type,
996 SourceLocation ImportLoc, ModuleFile *ImportedBy,
997 SmallVectorImpl<ImportedModule> &Loaded,
998 off_t ExpectedSize, time_t ExpectedModTime,
999 unsigned ClientLoadCapabilities);
1000 ASTReadResult ReadControlBlock(ModuleFile &F,
1001 SmallVectorImpl<ImportedModule> &Loaded,
1002 unsigned ClientLoadCapabilities);
1003 bool ReadASTBlock(ModuleFile &F);
1004 bool ParseLineTable(ModuleFile &F, SmallVectorImpl<uint64_t> &Record);
1005 bool ReadSourceManagerBlock(ModuleFile &F);
1006 llvm::BitstreamCursor &SLocCursorForID(int ID);
1007 SourceLocation getImportLocation(ModuleFile *F);
1008 bool ReadSubmoduleBlock(ModuleFile &F);
1009 static bool ParseLanguageOptions(const RecordData &Record, bool Complain,
1010 ASTReaderListener &Listener);
1011 static bool ParseTargetOptions(const RecordData &Record, bool Complain,
1012 ASTReaderListener &Listener);
1013 static bool ParseDiagnosticOptions(const RecordData &Record, bool Complain,
1014 ASTReaderListener &Listener);
1015 static bool ParseFileSystemOptions(const RecordData &Record, bool Complain,
1016 ASTReaderListener &Listener);
1017 static bool ParseHeaderSearchOptions(const RecordData &Record, bool Complain,
1018 ASTReaderListener &Listener);
1019 static bool ParsePreprocessorOptions(const RecordData &Record, bool Complain,
1020 ASTReaderListener &Listener,
1021 std::string &SuggestedPredefines);
1023 struct RecordLocation {
1024 RecordLocation(ModuleFile *M, uint64_t O)
1025 : F(M), Offset(O) {}
1030 QualType readTypeRecord(unsigned Index);
1031 RecordLocation TypeCursorForIndex(unsigned Index);
1032 void LoadedDecl(unsigned Index, Decl *D);
1033 Decl *ReadDeclRecord(serialization::DeclID ID);
1034 RecordLocation DeclCursorForID(serialization::DeclID ID,
1035 unsigned &RawLocation);
1036 void loadDeclUpdateRecords(serialization::DeclID ID, Decl *D);
1037 void loadPendingDeclChain(serialization::GlobalDeclID ID);
1038 void loadObjCCategories(serialization::GlobalDeclID ID, ObjCInterfaceDecl *D,
1039 unsigned PreviousGeneration = 0);
1041 RecordLocation getLocalBitOffset(uint64_t GlobalOffset);
1042 uint64_t getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset);
1044 /// \brief Returns the first preprocessed entity ID that ends after BLoc.
1045 serialization::PreprocessedEntityID
1046 findBeginPreprocessedEntity(SourceLocation BLoc) const;
1048 /// \brief Returns the first preprocessed entity ID that begins after ELoc.
1049 serialization::PreprocessedEntityID
1050 findEndPreprocessedEntity(SourceLocation ELoc) const;
1052 /// \brief Find the next module that contains entities and return the ID
1053 /// of the first entry.
1055 /// \param SLocMapI points at a chunk of a module that contains no
1056 /// preprocessed entities or the entities it contains are not the
1057 /// ones we are looking for.
1058 serialization::PreprocessedEntityID
1059 findNextPreprocessedEntity(
1060 GlobalSLocOffsetMapType::const_iterator SLocMapI) const;
1062 /// \brief Returns (ModuleFile, Local index) pair for \p GlobalIndex of a
1063 /// preprocessed entity.
1064 std::pair<ModuleFile *, unsigned>
1065 getModulePreprocessedEntity(unsigned GlobalIndex);
1067 /// \brief Returns (begin, end) pair for the preprocessed entities of a
1068 /// particular module.
1069 std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
1070 getModulePreprocessedEntities(ModuleFile &Mod) const;
1072 class ModuleDeclIterator {
1075 const serialization::LocalDeclID *Pos;
1078 typedef const Decl *value_type;
1079 typedef value_type& reference;
1080 typedef value_type* pointer;
1082 ModuleDeclIterator() : Reader(0), Mod(0), Pos(0) { }
1084 ModuleDeclIterator(ASTReader *Reader, ModuleFile *Mod,
1085 const serialization::LocalDeclID *Pos)
1086 : Reader(Reader), Mod(Mod), Pos(Pos) { }
1088 value_type operator*() const {
1089 return Reader->GetDecl(Reader->getGlobalDeclID(*Mod, *Pos));
1092 ModuleDeclIterator &operator++() {
1097 ModuleDeclIterator operator++(int) {
1098 ModuleDeclIterator Prev(*this);
1103 ModuleDeclIterator &operator--() {
1108 ModuleDeclIterator operator--(int) {
1109 ModuleDeclIterator Prev(*this);
1114 friend bool operator==(const ModuleDeclIterator &LHS,
1115 const ModuleDeclIterator &RHS) {
1116 assert(LHS.Reader == RHS.Reader && LHS.Mod == RHS.Mod);
1117 return LHS.Pos == RHS.Pos;
1120 friend bool operator!=(const ModuleDeclIterator &LHS,
1121 const ModuleDeclIterator &RHS) {
1122 assert(LHS.Reader == RHS.Reader && LHS.Mod == RHS.Mod);
1123 return LHS.Pos != RHS.Pos;
1127 std::pair<ModuleDeclIterator, ModuleDeclIterator>
1128 getModuleFileLevelDecls(ModuleFile &Mod);
1130 void PassInterestingDeclsToConsumer();
1131 void PassInterestingDeclToConsumer(Decl *D);
1133 void finishPendingActions();
1135 void pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name);
1137 void addPendingDeclContextInfo(Decl *D,
1138 serialization::GlobalDeclID SemaDC,
1139 serialization::GlobalDeclID LexicalDC) {
1141 PendingDeclContextInfo Info = { D, SemaDC, LexicalDC };
1142 PendingDeclContextInfos.push_back(Info);
1145 /// \brief Produce an error diagnostic and return true.
1147 /// This routine should only be used for fatal errors that have to
1148 /// do with non-routine failures (e.g., corrupted AST file).
1149 void Error(StringRef Msg);
1150 void Error(unsigned DiagID, StringRef Arg1 = StringRef(),
1151 StringRef Arg2 = StringRef());
1153 ASTReader(const ASTReader &) LLVM_DELETED_FUNCTION;
1154 void operator=(const ASTReader &) LLVM_DELETED_FUNCTION;
1156 /// \brief Load the AST file and validate its contents against the given
1159 /// \param PP the preprocessor associated with the context in which this
1160 /// precompiled header will be loaded.
1162 /// \param Context the AST context that this precompiled header will be
1165 /// \param isysroot If non-NULL, the system include path specified by the
1166 /// user. This is only used with relocatable PCH files. If non-NULL,
1167 /// a relocatable PCH file will use the default path "/".
1169 /// \param DisableValidation If true, the AST reader will suppress most
1170 /// of its regular consistency checking, allowing the use of precompiled
1171 /// headers that cannot be determined to be compatible.
1173 /// \param AllowASTWithCompilerErrors If true, the AST reader will accept an
1174 /// AST file the was created out of an AST with compiler errors,
1175 /// otherwise it will reject it.
1177 /// \param UseGlobalIndex If true, the AST reader will try to load and use
1178 /// the global module index.
1179 ASTReader(Preprocessor &PP, ASTContext &Context, StringRef isysroot = "",
1180 bool DisableValidation = false,
1181 bool AllowASTWithCompilerErrors = false,
1182 bool UseGlobalIndex = true);
1186 SourceManager &getSourceManager() const { return SourceMgr; }
1187 FileManager &getFileManager() const { return FileMgr; }
1189 /// \brief Flags that indicate what kind of AST loading failures the client
1190 /// of the AST reader can directly handle.
1192 /// When a client states that it can handle a particular kind of failure,
1193 /// the AST reader will not emit errors when producing that kind of failure.
1194 enum LoadFailureCapabilities {
1195 /// \brief The client can't handle any AST loading failures.
1197 /// \brief The client can handle an AST file that cannot load because it
1200 /// \brief The client can handle an AST file that cannot load because it
1201 /// is out-of-date relative to its input files.
1202 ARR_OutOfDate = 0x2,
1203 /// \brief The client can handle an AST file that cannot load because it
1204 /// was built with a different version of Clang.
1205 ARR_VersionMismatch = 0x4,
1206 /// \brief The client can handle an AST file that cannot load because it's
1207 /// compiled configuration doesn't match that of the context it was
1209 ARR_ConfigurationMismatch = 0x8
1212 /// \brief Load the AST file designated by the given file name.
1214 /// \param FileName The name of the AST file to load.
1216 /// \param Type The kind of AST being loaded, e.g., PCH, module, main file,
1219 /// \param ImportLoc the location where the module file will be considered as
1220 /// imported from. For non-module AST types it should be invalid.
1222 /// \param ClientLoadCapabilities The set of client load-failure
1223 /// capabilities, represented as a bitset of the enumerators of
1224 /// LoadFailureCapabilities.
1225 ASTReadResult ReadAST(const std::string &FileName, ModuleKind Type,
1226 SourceLocation ImportLoc,
1227 unsigned ClientLoadCapabilities);
1229 /// \brief Make the entities in the given module and any of its (non-explicit)
1230 /// submodules visible to name lookup.
1232 /// \param Mod The module whose names should be made visible.
1234 /// \param NameVisibility The level of visibility to give the names in the
1235 /// module. Visibility can only be increased over time.
1237 /// \param ImportLoc The location at which the import occurs.
1239 /// \param Complain Whether to complain about conflicting module imports.
1240 void makeModuleVisible(Module *Mod,
1241 Module::NameVisibilityKind NameVisibility,
1242 SourceLocation ImportLoc,
1245 /// \brief Make the names within this set of hidden names visible.
1246 void makeNamesVisible(const HiddenNames &Names, Module *Owner);
1248 /// \brief Set the AST callbacks listener.
1249 void setListener(ASTReaderListener *listener) {
1250 Listener.reset(listener);
1253 /// \brief Set the AST deserialization listener.
1254 void setDeserializationListener(ASTDeserializationListener *Listener);
1256 /// \brief Determine whether this AST reader has a global index.
1257 bool hasGlobalIndex() const { return GlobalIndex.isValid(); }
1259 /// \brief Attempts to load the global index.
1261 /// \returns true if loading the global index has failed for any reason.
1262 bool loadGlobalIndex();
1264 /// \brief Determine whether we tried to load the global index, but failed,
1265 /// e.g., because it is out-of-date or does not exist.
1266 bool isGlobalIndexUnavailable() const;
1268 /// \brief Initializes the ASTContext
1269 void InitializeContext();
1271 /// \brief Update the state of Sema after loading some additional modules.
1274 /// \brief Add in-memory (virtual file) buffer.
1275 void addInMemoryBuffer(StringRef &FileName, llvm::MemoryBuffer *Buffer) {
1276 ModuleMgr.addInMemoryBuffer(FileName, Buffer);
1279 /// \brief Finalizes the AST reader's state before writing an AST file to
1282 /// This operation may undo temporary state in the AST that should not be
1284 void finalizeForWriting();
1286 /// \brief Retrieve the module manager.
1287 ModuleManager &getModuleManager() { return ModuleMgr; }
1289 /// \brief Retrieve the preprocessor.
1290 Preprocessor &getPreprocessor() const { return PP; }
1292 /// \brief Retrieve the name of the original source file name for the primary
1294 StringRef getOriginalSourceFile() {
1295 return ModuleMgr.getPrimaryModule().OriginalSourceFileName;
1298 /// \brief Retrieve the name of the original source file name directly from
1299 /// the AST file, without actually loading the AST file.
1300 static std::string getOriginalSourceFile(const std::string &ASTFileName,
1301 FileManager &FileMgr,
1302 DiagnosticsEngine &Diags);
1304 /// \brief Read the control block for the named AST file.
1306 /// \returns true if an error occurred, false otherwise.
1307 static bool readASTFileControlBlock(StringRef Filename,
1308 FileManager &FileMgr,
1309 ASTReaderListener &Listener);
1311 /// \brief Determine whether the given AST file is acceptable to load into a
1312 /// translation unit with the given language and target options.
1313 static bool isAcceptableASTFile(StringRef Filename,
1314 FileManager &FileMgr,
1315 const LangOptions &LangOpts,
1316 const TargetOptions &TargetOpts,
1317 const PreprocessorOptions &PPOpts);
1319 /// \brief Returns the suggested contents of the predefines buffer,
1320 /// which contains a (typically-empty) subset of the predefines
1321 /// build prior to including the precompiled header.
1322 const std::string &getSuggestedPredefines() { return SuggestedPredefines; }
1324 /// \brief Read a preallocated preprocessed entity from the external source.
1326 /// \returns null if an error occurred that prevented the preprocessed
1327 /// entity from being loaded.
1328 virtual PreprocessedEntity *ReadPreprocessedEntity(unsigned Index);
1330 /// \brief Returns a pair of [Begin, End) indices of preallocated
1331 /// preprocessed entities that \p Range encompasses.
1332 virtual std::pair<unsigned, unsigned>
1333 findPreprocessedEntitiesInRange(SourceRange Range);
1335 /// \brief Optionally returns true or false if the preallocated preprocessed
1336 /// entity with index \p Index came from file \p FID.
1337 virtual Optional<bool> isPreprocessedEntityInFileID(unsigned Index,
1340 /// \brief Read the header file information for the given file entry.
1341 virtual HeaderFileInfo GetHeaderFileInfo(const FileEntry *FE);
1343 void ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag);
1345 /// \brief Returns the number of source locations found in the chain.
1346 unsigned getTotalNumSLocs() const {
1347 return TotalNumSLocEntries;
1350 /// \brief Returns the number of identifiers found in the chain.
1351 unsigned getTotalNumIdentifiers() const {
1352 return static_cast<unsigned>(IdentifiersLoaded.size());
1355 /// \brief Returns the number of macros found in the chain.
1356 unsigned getTotalNumMacros() const {
1357 return static_cast<unsigned>(MacrosLoaded.size());
1360 /// \brief Returns the number of types found in the chain.
1361 unsigned getTotalNumTypes() const {
1362 return static_cast<unsigned>(TypesLoaded.size());
1365 /// \brief Returns the number of declarations found in the chain.
1366 unsigned getTotalNumDecls() const {
1367 return static_cast<unsigned>(DeclsLoaded.size());
1370 /// \brief Returns the number of submodules known.
1371 unsigned getTotalNumSubmodules() const {
1372 return static_cast<unsigned>(SubmodulesLoaded.size());
1375 /// \brief Returns the number of selectors found in the chain.
1376 unsigned getTotalNumSelectors() const {
1377 return static_cast<unsigned>(SelectorsLoaded.size());
1380 /// \brief Returns the number of preprocessed entities known to the AST
1382 unsigned getTotalNumPreprocessedEntities() const {
1383 unsigned Result = 0;
1384 for (ModuleConstIterator I = ModuleMgr.begin(),
1385 E = ModuleMgr.end(); I != E; ++I) {
1386 Result += (*I)->NumPreprocessedEntities;
1392 /// \brief Returns the number of C++ base specifiers found in the chain.
1393 unsigned getTotalNumCXXBaseSpecifiers() const {
1394 return NumCXXBaseSpecifiersLoaded;
1397 /// \brief Reads a TemplateArgumentLocInfo appropriate for the
1398 /// given TemplateArgument kind.
1399 TemplateArgumentLocInfo
1400 GetTemplateArgumentLocInfo(ModuleFile &F, TemplateArgument::ArgKind Kind,
1401 const RecordData &Record, unsigned &Idx);
1403 /// \brief Reads a TemplateArgumentLoc.
1405 ReadTemplateArgumentLoc(ModuleFile &F,
1406 const RecordData &Record, unsigned &Idx);
1408 const ASTTemplateArgumentListInfo*
1409 ReadASTTemplateArgumentListInfo(ModuleFile &F,
1410 const RecordData &Record, unsigned &Index);
1412 /// \brief Reads a declarator info from the given record.
1413 TypeSourceInfo *GetTypeSourceInfo(ModuleFile &F,
1414 const RecordData &Record, unsigned &Idx);
1416 /// \brief Resolve a type ID into a type, potentially building a new
1418 QualType GetType(serialization::TypeID ID);
1420 /// \brief Resolve a local type ID within a given AST file into a type.
1421 QualType getLocalType(ModuleFile &F, unsigned LocalID);
1423 /// \brief Map a local type ID within a given AST file into a global type ID.
1424 serialization::TypeID getGlobalTypeID(ModuleFile &F, unsigned LocalID) const;
1426 /// \brief Read a type from the current position in the given record, which
1427 /// was read from the given AST file.
1428 QualType readType(ModuleFile &F, const RecordData &Record, unsigned &Idx) {
1429 if (Idx >= Record.size())
1432 return getLocalType(F, Record[Idx++]);
1435 /// \brief Map from a local declaration ID within a given module to a
1436 /// global declaration ID.
1437 serialization::DeclID getGlobalDeclID(ModuleFile &F,
1438 serialization::LocalDeclID LocalID) const;
1440 /// \brief Returns true if global DeclID \p ID originated from module \p M.
1441 bool isDeclIDFromModule(serialization::GlobalDeclID ID, ModuleFile &M) const;
1443 /// \brief Retrieve the module file that owns the given declaration, or NULL
1444 /// if the declaration is not from a module file.
1445 ModuleFile *getOwningModuleFile(const Decl *D);
1447 /// \brief Returns the source location for the decl \p ID.
1448 SourceLocation getSourceLocationForDeclID(serialization::GlobalDeclID ID);
1450 /// \brief Resolve a declaration ID into a declaration, potentially
1451 /// building a new declaration.
1452 Decl *GetDecl(serialization::DeclID ID);
1453 virtual Decl *GetExternalDecl(uint32_t ID);
1455 /// \brief Reads a declaration with the given local ID in the given module.
1456 Decl *GetLocalDecl(ModuleFile &F, uint32_t LocalID) {
1457 return GetDecl(getGlobalDeclID(F, LocalID));
1460 /// \brief Reads a declaration with the given local ID in the given module.
1462 /// \returns The requested declaration, casted to the given return type.
1463 template<typename T>
1464 T *GetLocalDeclAs(ModuleFile &F, uint32_t LocalID) {
1465 return cast_or_null<T>(GetLocalDecl(F, LocalID));
1468 /// \brief Map a global declaration ID into the declaration ID used to
1469 /// refer to this declaration within the given module fule.
1471 /// \returns the global ID of the given declaration as known in the given
1473 serialization::DeclID
1474 mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
1475 serialization::DeclID GlobalID);
1477 /// \brief Reads a declaration ID from the given position in a record in the
1480 /// \returns The declaration ID read from the record, adjusted to a global ID.
1481 serialization::DeclID ReadDeclID(ModuleFile &F, const RecordData &Record,
1484 /// \brief Reads a declaration from the given position in a record in the
1486 Decl *ReadDecl(ModuleFile &F, const RecordData &R, unsigned &I) {
1487 return GetDecl(ReadDeclID(F, R, I));
1490 /// \brief Reads a declaration from the given position in a record in the
1493 /// \returns The declaration read from this location, casted to the given
1495 template<typename T>
1496 T *ReadDeclAs(ModuleFile &F, const RecordData &R, unsigned &I) {
1497 return cast_or_null<T>(GetDecl(ReadDeclID(F, R, I)));
1500 /// \brief Read a CXXBaseSpecifiers ID form the given record and
1501 /// return its global bit offset.
1502 uint64_t readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
1505 virtual CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset);
1507 /// \brief Resolve the offset of a statement into a statement.
1509 /// This operation will read a new statement from the external
1510 /// source each time it is called, and is meant to be used via a
1511 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
1512 virtual Stmt *GetExternalDeclStmt(uint64_t Offset);
1514 /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1515 /// specified cursor. Read the abbreviations that are at the top of the block
1516 /// and then leave the cursor pointing into the block.
1517 bool ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor, unsigned BlockID);
1519 /// \brief Finds all the visible declarations with a given name.
1520 /// The current implementation of this method just loads the entire
1521 /// lookup table as unmaterialized references.
1523 FindExternalVisibleDeclsByName(const DeclContext *DC,
1524 DeclarationName Name);
1526 /// \brief Read all of the declarations lexically stored in a
1527 /// declaration context.
1529 /// \param DC The declaration context whose declarations will be
1532 /// \param Decls Vector that will contain the declarations loaded
1533 /// from the external source. The caller is responsible for merging
1534 /// these declarations with any declarations already stored in the
1535 /// declaration context.
1537 /// \returns true if there was an error while reading the
1538 /// declarations for this declaration context.
1539 virtual ExternalLoadResult FindExternalLexicalDecls(const DeclContext *DC,
1540 bool (*isKindWeWant)(Decl::Kind),
1541 SmallVectorImpl<Decl*> &Decls);
1543 /// \brief Get the decls that are contained in a file in the Offset/Length
1544 /// range. \p Length can be 0 to indicate a point at \p Offset instead of
1546 virtual void FindFileRegionDecls(FileID File, unsigned Offset,unsigned Length,
1547 SmallVectorImpl<Decl *> &Decls);
1549 /// \brief Notify ASTReader that we started deserialization of
1550 /// a decl or type so until FinishedDeserializing is called there may be
1551 /// decls that are initializing. Must be paired with FinishedDeserializing.
1552 virtual void StartedDeserializing() { ++NumCurrentElementsDeserializing; }
1554 /// \brief Notify ASTReader that we finished the deserialization of
1555 /// a decl or type. Must be paired with StartedDeserializing.
1556 virtual void FinishedDeserializing();
1558 /// \brief Function that will be invoked when we begin parsing a new
1559 /// translation unit involving this external AST source.
1561 /// This function will provide all of the external definitions to
1562 /// the ASTConsumer.
1563 virtual void StartTranslationUnit(ASTConsumer *Consumer);
1565 /// \brief Print some statistics about AST usage.
1566 virtual void PrintStats();
1568 /// \brief Dump information about the AST reader to standard error.
1571 /// Return the amount of memory used by memory buffers, breaking down
1572 /// by heap-backed versus mmap'ed memory.
1573 virtual void getMemoryBufferSizes(MemoryBufferSizes &sizes) const;
1575 /// \brief Initialize the semantic source with the Sema instance
1576 /// being used to perform semantic analysis on the abstract syntax
1578 virtual void InitializeSema(Sema &S);
1580 /// \brief Inform the semantic consumer that Sema is no longer available.
1581 virtual void ForgetSema() { SemaObj = 0; }
1583 /// \brief Retrieve the IdentifierInfo for the named identifier.
1585 /// This routine builds a new IdentifierInfo for the given identifier. If any
1586 /// declarations with this name are visible from translation unit scope, their
1587 /// declarations will be deserialized and introduced into the declaration
1588 /// chain of the identifier.
1589 virtual IdentifierInfo *get(const char *NameStart, const char *NameEnd);
1590 IdentifierInfo *get(StringRef Name) {
1591 return get(Name.begin(), Name.end());
1594 /// \brief Retrieve an iterator into the set of all identifiers
1595 /// in all loaded AST files.
1596 virtual IdentifierIterator *getIdentifiers();
1598 /// \brief Load the contents of the global method pool for a given
1600 virtual void ReadMethodPool(Selector Sel);
1602 /// \brief Load the set of namespaces that are known to the external source,
1603 /// which will be used during typo correction.
1604 virtual void ReadKnownNamespaces(
1605 SmallVectorImpl<NamespaceDecl *> &Namespaces);
1607 virtual void ReadUndefinedButUsed(
1608 llvm::DenseMap<NamedDecl *, SourceLocation> &Undefined);
1610 virtual void ReadTentativeDefinitions(
1611 SmallVectorImpl<VarDecl *> &TentativeDefs);
1613 virtual void ReadUnusedFileScopedDecls(
1614 SmallVectorImpl<const DeclaratorDecl *> &Decls);
1616 virtual void ReadDelegatingConstructors(
1617 SmallVectorImpl<CXXConstructorDecl *> &Decls);
1619 virtual void ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls);
1621 virtual void ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls);
1623 virtual void ReadLocallyScopedExternCDecls(
1624 SmallVectorImpl<NamedDecl *> &Decls);
1626 virtual void ReadReferencedSelectors(
1627 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels);
1629 virtual void ReadWeakUndeclaredIdentifiers(
1630 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WI);
1632 virtual void ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables);
1634 virtual void ReadPendingInstantiations(
1635 SmallVectorImpl<std::pair<ValueDecl *,
1636 SourceLocation> > &Pending);
1638 virtual void ReadLateParsedTemplates(
1639 llvm::DenseMap<const FunctionDecl *, LateParsedTemplate *> &LPTMap);
1641 /// \brief Load a selector from disk, registering its ID if it exists.
1642 void LoadSelector(Selector Sel);
1644 void SetIdentifierInfo(unsigned ID, IdentifierInfo *II);
1645 void SetGloballyVisibleDecls(IdentifierInfo *II,
1646 const SmallVectorImpl<uint32_t> &DeclIDs,
1647 SmallVectorImpl<Decl *> *Decls = 0);
1649 /// \brief Report a diagnostic.
1650 DiagnosticBuilder Diag(unsigned DiagID);
1652 /// \brief Report a diagnostic.
1653 DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID);
1655 IdentifierInfo *DecodeIdentifierInfo(serialization::IdentifierID ID);
1657 IdentifierInfo *GetIdentifierInfo(ModuleFile &M, const RecordData &Record,
1659 return DecodeIdentifierInfo(getGlobalIdentifierID(M, Record[Idx++]));
1662 virtual IdentifierInfo *GetIdentifier(serialization::IdentifierID ID) {
1663 // Note that we are loading an identifier.
1664 Deserializing AnIdentifier(this);
1666 return DecodeIdentifierInfo(ID);
1669 IdentifierInfo *getLocalIdentifier(ModuleFile &M, unsigned LocalID);
1671 serialization::IdentifierID getGlobalIdentifierID(ModuleFile &M,
1674 void resolvePendingMacro(IdentifierInfo *II, const PendingMacroInfo &PMInfo);
1676 void installPCHMacroDirectives(IdentifierInfo *II,
1677 ModuleFile &M, uint64_t Offset);
1679 void installImportedMacro(IdentifierInfo *II, MacroDirective *MD,
1682 /// \brief Retrieve the macro with the given ID.
1683 MacroInfo *getMacro(serialization::MacroID ID);
1685 /// \brief Retrieve the global macro ID corresponding to the given local
1686 /// ID within the given module file.
1687 serialization::MacroID getGlobalMacroID(ModuleFile &M, unsigned LocalID);
1689 /// \brief Read the source location entry with index ID.
1690 virtual bool ReadSLocEntry(int ID);
1692 /// \brief Retrieve the module import location and module name for the
1693 /// given source manager entry ID.
1694 virtual std::pair<SourceLocation, StringRef> getModuleImportLoc(int ID);
1696 /// \brief Retrieve the global submodule ID given a module and its local ID
1698 serialization::SubmoduleID
1699 getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID);
1701 /// \brief Retrieve the submodule that corresponds to a global submodule ID.
1703 Module *getSubmodule(serialization::SubmoduleID GlobalID);
1705 /// \brief Retrieve the module that corresponds to the given module ID.
1707 /// Note: overrides method in ExternalASTSource
1708 virtual Module *getModule(unsigned ID);
1710 /// \brief Retrieve a selector from the given module with its local ID
1712 Selector getLocalSelector(ModuleFile &M, unsigned LocalID);
1714 Selector DecodeSelector(serialization::SelectorID Idx);
1716 virtual Selector GetExternalSelector(serialization::SelectorID ID);
1717 uint32_t GetNumExternalSelectors();
1719 Selector ReadSelector(ModuleFile &M, const RecordData &Record, unsigned &Idx) {
1720 return getLocalSelector(M, Record[Idx++]);
1723 /// \brief Retrieve the global selector ID that corresponds to this
1724 /// the local selector ID in a given module.
1725 serialization::SelectorID getGlobalSelectorID(ModuleFile &F,
1726 unsigned LocalID) const;
1728 /// \brief Read a declaration name.
1729 DeclarationName ReadDeclarationName(ModuleFile &F,
1730 const RecordData &Record, unsigned &Idx);
1731 void ReadDeclarationNameLoc(ModuleFile &F,
1732 DeclarationNameLoc &DNLoc, DeclarationName Name,
1733 const RecordData &Record, unsigned &Idx);
1734 void ReadDeclarationNameInfo(ModuleFile &F, DeclarationNameInfo &NameInfo,
1735 const RecordData &Record, unsigned &Idx);
1737 void ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
1738 const RecordData &Record, unsigned &Idx);
1740 NestedNameSpecifier *ReadNestedNameSpecifier(ModuleFile &F,
1741 const RecordData &Record,
1744 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc(ModuleFile &F,
1745 const RecordData &Record,
1748 /// \brief Read a template name.
1749 TemplateName ReadTemplateName(ModuleFile &F, const RecordData &Record,
1752 /// \brief Read a template argument.
1753 TemplateArgument ReadTemplateArgument(ModuleFile &F,
1754 const RecordData &Record,unsigned &Idx);
1756 /// \brief Read a template parameter list.
1757 TemplateParameterList *ReadTemplateParameterList(ModuleFile &F,
1758 const RecordData &Record,
1761 /// \brief Read a template argument array.
1763 ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
1764 ModuleFile &F, const RecordData &Record,
1767 /// \brief Read a UnresolvedSet structure.
1768 void ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
1769 const RecordData &Record, unsigned &Idx);
1771 /// \brief Read a C++ base specifier.
1772 CXXBaseSpecifier ReadCXXBaseSpecifier(ModuleFile &F,
1773 const RecordData &Record,unsigned &Idx);
1775 /// \brief Read a CXXCtorInitializer array.
1776 std::pair<CXXCtorInitializer **, unsigned>
1777 ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
1780 /// \brief Read a source location from raw form.
1781 SourceLocation ReadSourceLocation(ModuleFile &ModuleFile, unsigned Raw) const {
1782 SourceLocation Loc = SourceLocation::getFromRawEncoding(Raw);
1783 assert(ModuleFile.SLocRemap.find(Loc.getOffset()) != ModuleFile.SLocRemap.end() &&
1784 "Cannot find offset to remap.");
1785 int Remap = ModuleFile.SLocRemap.find(Loc.getOffset())->second;
1786 return Loc.getLocWithOffset(Remap);
1789 /// \brief Read a source location.
1790 SourceLocation ReadSourceLocation(ModuleFile &ModuleFile,
1791 const RecordDataImpl &Record,
1793 return ReadSourceLocation(ModuleFile, Record[Idx++]);
1796 /// \brief Read a source range.
1797 SourceRange ReadSourceRange(ModuleFile &F,
1798 const RecordData &Record, unsigned &Idx);
1800 /// \brief Read an integral value
1801 llvm::APInt ReadAPInt(const RecordData &Record, unsigned &Idx);
1803 /// \brief Read a signed integral value
1804 llvm::APSInt ReadAPSInt(const RecordData &Record, unsigned &Idx);
1806 /// \brief Read a floating-point value
1807 llvm::APFloat ReadAPFloat(const RecordData &Record,
1808 const llvm::fltSemantics &Sem, unsigned &Idx);
1810 // \brief Read a string
1811 static std::string ReadString(const RecordData &Record, unsigned &Idx);
1813 /// \brief Read a version tuple.
1814 static VersionTuple ReadVersionTuple(const RecordData &Record, unsigned &Idx);
1816 CXXTemporary *ReadCXXTemporary(ModuleFile &F, const RecordData &Record,
1819 /// \brief Reads attributes from the current stream position.
1820 void ReadAttributes(ModuleFile &F, AttrVec &Attrs,
1821 const RecordData &Record, unsigned &Idx);
1823 /// \brief Reads a statement.
1824 Stmt *ReadStmt(ModuleFile &F);
1826 /// \brief Reads an expression.
1827 Expr *ReadExpr(ModuleFile &F);
1829 /// \brief Reads a sub-statement operand during statement reading.
1830 Stmt *ReadSubStmt() {
1831 assert(ReadingKind == Read_Stmt &&
1832 "Should be called only during statement reading!");
1833 // Subexpressions are stored from last to first, so the next Stmt we need
1834 // is at the back of the stack.
1835 assert(!StmtStack.empty() && "Read too many sub statements!");
1836 return StmtStack.pop_back_val();
1839 /// \brief Reads a sub-expression operand during statement reading.
1840 Expr *ReadSubExpr();
1842 /// \brief Reads a token out of a record.
1843 Token ReadToken(ModuleFile &M, const RecordDataImpl &Record, unsigned &Idx);
1845 /// \brief Reads the macro record located at the given offset.
1846 MacroInfo *ReadMacroRecord(ModuleFile &F, uint64_t Offset);
1848 /// \brief Determine the global preprocessed entity ID that corresponds to
1849 /// the given local ID within the given module.
1850 serialization::PreprocessedEntityID
1851 getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const;
1853 /// \brief Add a macro to resolve imported from a module.
1855 /// \param II The name of the macro.
1856 /// \param M The module file.
1857 /// \param GMacID The global macro ID that is associated with this identifier.
1858 /// \param ImportLoc The location where the module is imported.
1859 void addPendingMacroFromModule(IdentifierInfo *II,
1861 serialization::GlobalMacroID GMacID,
1862 SourceLocation ImportLoc);
1864 /// \brief Add a macro to deserialize its macro directive history from a PCH.
1866 /// \param II The name of the macro.
1867 /// \param M The module file.
1868 /// \param MacroDirectivesOffset Offset of the serialized macro directive
1870 void addPendingMacroFromPCH(IdentifierInfo *II,
1871 ModuleFile *M, uint64_t MacroDirectivesOffset);
1873 /// \brief Read the set of macros defined by this external macro source.
1874 virtual void ReadDefinedMacros();
1876 /// \brief Update an out-of-date identifier.
1877 virtual void updateOutOfDateIdentifier(IdentifierInfo &II);
1879 /// \brief Note that this identifier is up-to-date.
1880 void markIdentifierUpToDate(IdentifierInfo *II);
1882 /// \brief Load all external visible decls in the given DeclContext.
1883 void completeVisibleDeclsMap(const DeclContext *DC);
1885 /// \brief Retrieve the AST context that this AST reader supplements.
1886 ASTContext &getContext() { return Context; }
1888 // \brief Contains declarations that were loaded before we have
1889 // access to a Sema object.
1890 SmallVector<NamedDecl *, 16> PreloadedDecls;
1892 /// \brief Retrieve the semantic analysis object used to analyze the
1893 /// translation unit in which the precompiled header is being
1895 Sema *getSema() { return SemaObj; }
1897 /// \brief Retrieve the identifier table associated with the
1899 IdentifierTable &getIdentifierTable();
1901 /// \brief Record that the given ID maps to the given switch-case
1903 void RecordSwitchCaseID(SwitchCase *SC, unsigned ID);
1905 /// \brief Retrieve the switch-case statement with the given ID.
1906 SwitchCase *getSwitchCaseWithID(unsigned ID);
1908 void ClearSwitchCaseIDs();
1910 /// \brief Cursors for comments blocks.
1911 SmallVector<std::pair<llvm::BitstreamCursor,
1912 serialization::ModuleFile *>, 8> CommentsCursors;
1914 /// \brief Loads comments ranges.
1915 void ReadComments();
1918 /// \brief Helper class that saves the current stream position and
1919 /// then restores it when destroyed.
1920 struct SavedStreamPosition {
1921 explicit SavedStreamPosition(llvm::BitstreamCursor &Cursor)
1922 : Cursor(Cursor), Offset(Cursor.GetCurrentBitNo()) { }
1924 ~SavedStreamPosition() {
1925 Cursor.JumpToBit(Offset);
1929 llvm::BitstreamCursor &Cursor;
1933 inline void PCHValidator::Error(const char *Msg) {
1937 } // end namespace clang