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/Serialization/ASTBitCodes.h"
18 #include "clang/Serialization/ContinuousRangeMap.h"
19 #include "clang/Serialization/Module.h"
20 #include "clang/Serialization/ModuleManager.h"
21 #include "clang/Sema/ExternalSemaSource.h"
22 #include "clang/AST/DeclarationName.h"
23 #include "clang/AST/DeclObjC.h"
24 #include "clang/AST/TemplateBase.h"
25 #include "clang/Lex/ExternalPreprocessorSource.h"
26 #include "clang/Lex/HeaderSearch.h"
27 #include "clang/Lex/PreprocessingRecord.h"
28 #include "clang/Basic/Diagnostic.h"
29 #include "clang/Basic/FileManager.h"
30 #include "clang/Basic/FileSystemOptions.h"
31 #include "clang/Basic/IdentifierTable.h"
32 #include "clang/Basic/SourceManager.h"
33 #include "llvm/ADT/APFloat.h"
34 #include "llvm/ADT/APInt.h"
35 #include "llvm/ADT/APSInt.h"
36 #include "llvm/ADT/OwningPtr.h"
37 #include "llvm/ADT/SmallVector.h"
38 #include "llvm/ADT/StringRef.h"
39 #include "llvm/ADT/DenseSet.h"
40 #include "llvm/Bitcode/BitstreamReader.h"
41 #include "llvm/Support/DataTypes.h"
57 class ASTIdentifierIterator;
58 class ASTUnit; // FIXME: Layering violation and egregious hack.
62 class NestedNameSpecifier;
63 class CXXBaseSpecifier;
64 class CXXConstructorDecl;
65 class CXXCtorInitializer;
67 class MacroDefinition;
69 class OpaqueValueExpr;
73 class ASTDeserializationListener;
79 struct HeaderFileInfo;
82 struct PCHPredefinesBlock {
83 /// \brief The file ID for this predefines buffer in a PCH file.
86 /// \brief This predefines buffer in a PCH file.
89 typedef SmallVector<PCHPredefinesBlock, 2> PCHPredefinesBlocks;
91 /// \brief Abstract interface for callback invocations by the ASTReader.
93 /// While reading an AST file, the ASTReader will call the methods of the
94 /// listener to pass on specific information. Some of the listener methods can
95 /// return true to indicate to the ASTReader that the information (and
96 /// consequently the AST file) is invalid.
97 class ASTReaderListener {
99 virtual ~ASTReaderListener();
101 /// \brief Receives the language options.
103 /// \returns true to indicate the options are invalid or false otherwise.
104 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
108 /// \brief Receives the target triple.
110 /// \returns true to indicate the target triple is invalid or false otherwise.
111 virtual bool ReadTargetTriple(StringRef Triple) {
115 /// \brief Receives the contents of the predefines buffer.
117 /// \param Buffers Information about the predefines buffers.
119 /// \param OriginalFileName The original file name for the AST file, which
120 /// will appear as an entry in the predefines buffer.
122 /// \param SuggestedPredefines If necessary, additional definitions are added
125 /// \returns true to indicate the predefines are invalid or false otherwise.
126 virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
127 StringRef OriginalFileName,
128 std::string &SuggestedPredefines,
129 FileManager &FileMgr) {
133 /// \brief Receives a HeaderFileInfo entry.
134 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) {}
136 /// \brief Receives __COUNTER__ value.
137 virtual void ReadCounter(unsigned Value) {}
140 /// \brief ASTReaderListener implementation to validate the information of
141 /// the PCH file against an initialized Preprocessor.
142 class PCHValidator : public ASTReaderListener {
146 unsigned NumHeaderInfos;
149 PCHValidator(Preprocessor &PP, ASTReader &Reader)
150 : PP(PP), Reader(Reader), NumHeaderInfos(0) {}
152 virtual bool ReadLanguageOptions(const LangOptions &LangOpts);
153 virtual bool ReadTargetTriple(StringRef Triple);
154 virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
155 StringRef OriginalFileName,
156 std::string &SuggestedPredefines,
157 FileManager &FileMgr);
158 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID);
159 virtual void ReadCounter(unsigned Value);
162 void Error(const char *Msg);
165 namespace serialization {
167 class ReadMethodPoolVisitor;
170 class ASTIdentifierLookupTrait;
173 } // end namespace serialization
175 /// \brief Reads an AST files chain containing the contents of a translation
178 /// The ASTReader class reads bitstreams (produced by the ASTWriter
179 /// class) containing the serialized representation of a given
180 /// abstract syntax tree and its supporting data structures. An
181 /// instance of the ASTReader can be attached to an ASTContext object,
182 /// which will provide access to the contents of the AST files.
184 /// The AST reader provides lazy de-serialization of declarations, as
185 /// required when traversing the AST. Only those AST nodes that are
186 /// actually required will be de-serialized.
188 : public ExternalPreprocessorSource,
189 public ExternalPreprocessingRecordSource,
190 public ExternalHeaderFileInfoSource,
191 public ExternalSemaSource,
192 public IdentifierInfoLookup,
193 public ExternalIdentifierLookup,
194 public ExternalSLocEntrySource
197 enum ASTReadResult { Success, Failure, IgnorePCH };
198 /// \brief Types of AST files.
199 friend class PCHValidator;
200 friend class ASTDeclReader;
201 friend class ASTStmtReader;
202 friend class ASTIdentifierIterator;
203 friend class serialization::reader::ASTIdentifierLookupTrait;
204 friend class TypeLocReader;
205 friend class ASTWriter;
206 friend class ASTUnit; // ASTUnit needs to remap source locations.
207 friend class serialization::ReadMethodPoolVisitor;
209 typedef serialization::Module Module;
210 typedef serialization::ModuleKind ModuleKind;
211 typedef serialization::ModuleManager ModuleManager;
213 typedef ModuleManager::ModuleIterator ModuleIterator;
214 typedef ModuleManager::ModuleConstIterator ModuleConstIterator;
215 typedef ModuleManager::ModuleReverseIterator ModuleReverseIterator;
218 /// \brief The receiver of some callbacks invoked by ASTReader.
219 llvm::OwningPtr<ASTReaderListener> Listener;
221 /// \brief The receiver of deserialization events.
222 ASTDeserializationListener *DeserializationListener;
224 SourceManager &SourceMgr;
225 FileManager &FileMgr;
226 DiagnosticsEngine &Diags;
228 /// \brief The semantic analysis object that will be processing the
229 /// AST files and the translation unit that uses it.
232 /// \brief The preprocessor that will be loading the source file.
235 /// \brief The AST context into which we'll read the AST files.
238 /// \brief The AST consumer.
239 ASTConsumer *Consumer;
241 /// \brief The module manager which manages modules and their dependencies
242 ModuleManager ModuleMgr;
244 /// \brief A map of global bit offsets to the module that stores entities
245 /// at those bit offsets.
246 ContinuousRangeMap<uint64_t, Module*, 4> GlobalBitOffsetsMap;
248 /// \brief A map of negated SLocEntryIDs to the modules containing them.
249 ContinuousRangeMap<unsigned, Module*, 64> GlobalSLocEntryMap;
251 typedef ContinuousRangeMap<unsigned, Module*, 64> GlobalSLocOffsetMapType;
253 /// \brief A map of reversed (SourceManager::MaxLoadedOffset - SLocOffset)
254 /// SourceLocation offsets to the modules containing them.
255 GlobalSLocOffsetMapType GlobalSLocOffsetMap;
257 /// \brief Types that have already been loaded from the chain.
259 /// When the pointer at index I is non-NULL, the type with
260 /// ID = (I + 1) << FastQual::Width has already been loaded
261 std::vector<QualType> TypesLoaded;
263 typedef ContinuousRangeMap<serialization::TypeID, Module *, 4>
266 /// \brief Mapping from global type IDs to the module in which the
267 /// type resides along with the offset that should be added to the
268 /// global type ID to produce a local ID.
269 GlobalTypeMapType GlobalTypeMap;
271 /// \brief Declarations that have already been loaded from the chain.
273 /// When the pointer at index I is non-NULL, the declaration with ID
274 /// = I + 1 has already been loaded.
275 std::vector<Decl *> DeclsLoaded;
277 typedef ContinuousRangeMap<serialization::DeclID, Module *, 4>
280 /// \brief Mapping from global declaration IDs to the module in which the
281 /// declaration resides.
282 GlobalDeclMapType GlobalDeclMap;
284 typedef std::pair<Module *, uint64_t> FileOffset;
285 typedef SmallVector<FileOffset, 2> FileOffsetsTy;
286 typedef llvm::DenseMap<serialization::DeclID, FileOffsetsTy>
287 DeclUpdateOffsetsMap;
289 /// \brief Declarations that have modifications residing in a later file
291 DeclUpdateOffsetsMap DeclUpdateOffsets;
293 typedef llvm::DenseMap<serialization::DeclID,
294 std::pair<Module *, uint64_t> >
296 /// \brief Declarations that have been replaced in a later file in the chain.
297 DeclReplacementMap ReplacedDecls;
299 // Updates for visible decls can occur for other contexts than just the
300 // TU, and when we read those update records, the actual context will not
301 // be available yet (unless it's the TU), so have this pending map using the
302 // ID as a key. It will be realized when the context is actually loaded.
303 typedef SmallVector<std::pair<void *, Module*>, 1> DeclContextVisibleUpdates;
304 typedef llvm::DenseMap<serialization::DeclID, DeclContextVisibleUpdates>
305 DeclContextVisibleUpdatesPending;
307 /// \brief Updates to the visible declarations of declaration contexts that
308 /// haven't been loaded yet.
309 DeclContextVisibleUpdatesPending PendingVisibleUpdates;
311 typedef SmallVector<CXXRecordDecl *, 4> ForwardRefs;
312 typedef llvm::DenseMap<const CXXRecordDecl *, ForwardRefs>
313 PendingForwardRefsMap;
314 /// \brief Forward references that have a definition but the definition decl
315 /// is still initializing. When the definition gets read it will update
316 /// the DefinitionData pointer of all pending references.
317 PendingForwardRefsMap PendingForwardRefs;
319 typedef llvm::DenseMap<serialization::DeclID, serialization::DeclID>
320 FirstLatestDeclIDMap;
321 /// \brief Map of first declarations from a chained PCH that point to the
322 /// most recent declarations in another AST file.
323 FirstLatestDeclIDMap FirstLatestDeclIDs;
325 /// \brief Set of ObjC interfaces that have categories chained to them in
327 llvm::DenseSet<serialization::GlobalDeclID> ObjCChainedCategoriesInterfaces;
329 /// \brief Read the records that describe the contents of declcontexts.
330 bool ReadDeclContextStorage(Module &M,
331 llvm::BitstreamCursor &Cursor,
332 const std::pair<uint64_t, uint64_t> &Offsets,
333 serialization::DeclContextInfo &Info);
335 /// \brief A vector containing identifiers that have already been
338 /// If the pointer at index I is non-NULL, then it refers to the
339 /// IdentifierInfo for the identifier with ID=I+1 that has already
341 std::vector<IdentifierInfo *> IdentifiersLoaded;
343 typedef ContinuousRangeMap<serialization::IdentID, Module *, 4>
344 GlobalIdentifierMapType;
346 /// \brief Mapping from global identifer IDs to the module in which the
347 /// identifier resides along with the offset that should be added to the
348 /// global identifier ID to produce a local ID.
349 GlobalIdentifierMapType GlobalIdentifierMap;
351 /// \brief A vector containing selectors that have already been loaded.
353 /// This vector is indexed by the Selector ID (-1). NULL selector
354 /// entries indicate that the particular selector ID has not yet
356 SmallVector<Selector, 16> SelectorsLoaded;
358 typedef ContinuousRangeMap<serialization::SelectorID, Module *, 4>
359 GlobalSelectorMapType;
361 /// \brief Mapping from global selector IDs to the module in which the
362 /// selector resides along with the offset that should be added to the
363 /// global selector ID to produce a local ID.
364 GlobalSelectorMapType GlobalSelectorMap;
366 /// \brief Mapping from identifiers that represent macros whose definitions
367 /// have not yet been deserialized to the global offset where the macro
369 llvm::DenseMap<IdentifierInfo *, uint64_t> UnreadMacroRecordOffsets;
371 typedef ContinuousRangeMap<unsigned, Module *, 4>
372 GlobalPreprocessedEntityMapType;
374 /// \brief Mapping from global preprocessing entity IDs to the module in
375 /// which the preprocessed entity resides along with the offset that should be
376 /// added to the global preprocessing entitiy ID to produce a local ID.
377 GlobalPreprocessedEntityMapType GlobalPreprocessedEntityMap;
379 /// \name CodeGen-relevant special data
380 /// \brief Fields containing data that is relevant to CodeGen.
383 /// \brief The IDs of all declarations that fulfill the criteria of
384 /// "interesting" decls.
386 /// This contains the data loaded from all EXTERNAL_DEFINITIONS blocks in the
387 /// chain. The referenced declarations are deserialized and passed to the
388 /// consumer eagerly.
389 SmallVector<uint64_t, 16> ExternalDefinitions;
391 /// \brief The IDs of all tentative definitions stored in the the chain.
393 /// Sema keeps track of all tentative definitions in a TU because it has to
394 /// complete them and pass them on to CodeGen. Thus, tentative definitions in
395 /// the PCH chain must be eagerly deserialized.
396 SmallVector<uint64_t, 16> TentativeDefinitions;
398 /// \brief The IDs of all CXXRecordDecls stored in the chain whose VTables are
401 /// CodeGen has to emit VTables for these records, so they have to be eagerly
403 SmallVector<uint64_t, 64> VTableUses;
405 /// \brief A snapshot of the pending instantiations in the chain.
407 /// This record tracks the instantiations that Sema has to perform at the
408 /// end of the TU. It consists of a pair of values for every pending
409 /// instantiation where the first value is the ID of the decl and the second
410 /// is the instantiation location.
411 SmallVector<uint64_t, 64> PendingInstantiations;
415 /// \name DiagnosticsEngine-relevant special data
416 /// \brief Fields containing data that is used for generating diagnostics
419 /// \brief A snapshot of Sema's unused file-scoped variable tracking, for
420 /// generating warnings.
421 SmallVector<uint64_t, 16> UnusedFileScopedDecls;
423 /// \brief A list of all the delegating constructors we've seen, to diagnose
425 SmallVector<uint64_t, 4> DelegatingCtorDecls;
427 /// \brief Method selectors used in a @selector expression. Used for
428 /// implementation of -Wselector.
429 SmallVector<uint64_t, 64> ReferencedSelectorsData;
431 /// \brief A snapshot of Sema's weak undeclared identifier tracking, for
432 /// generating warnings.
433 SmallVector<uint64_t, 64> WeakUndeclaredIdentifiers;
435 /// \brief The IDs of type aliases for ext_vectors that exist in the chain.
437 /// Used by Sema for finding sugared names for ext_vectors in diagnostics.
438 SmallVector<uint64_t, 4> ExtVectorDecls;
442 /// \name Sema-relevant special data
443 /// \brief Fields containing data that is used for semantic analysis
446 /// \brief The IDs of all locally scoped external decls in the chain.
448 /// Sema tracks these to validate that the types are consistent across all
449 /// local external declarations.
450 SmallVector<uint64_t, 16> LocallyScopedExternalDecls;
452 /// \brief The IDs of all dynamic class declarations in the chain.
454 /// Sema tracks these because it checks for the key functions being defined
455 /// at the end of the TU, in which case it directs CodeGen to emit the VTable.
456 SmallVector<uint64_t, 16> DynamicClasses;
458 /// \brief The IDs of the declarations Sema stores directly.
460 /// Sema tracks a few important decls, such as namespace std, directly.
461 SmallVector<uint64_t, 4> SemaDeclRefs;
463 /// \brief The IDs of the types ASTContext stores directly.
465 /// The AST context tracks a few important types, such as va_list, directly.
466 SmallVector<uint64_t, 16> SpecialTypes;
468 /// \brief The IDs of CUDA-specific declarations ASTContext stores directly.
470 /// The AST context tracks a few important decls, currently cudaConfigureCall,
472 SmallVector<uint64_t, 2> CUDASpecialDeclRefs;
474 /// \brief The floating point pragma option settings.
475 SmallVector<uint64_t, 1> FPPragmaOptions;
477 /// \brief The OpenCL extension settings.
478 SmallVector<uint64_t, 1> OpenCLExtensions;
480 /// \brief A list of the namespaces we've seen.
481 SmallVector<uint64_t, 4> KnownNamespaces;
485 /// \brief The original file name that was used to build the primary AST file,
486 /// which may have been modified for relocatable-pch support.
487 std::string OriginalFileName;
489 /// \brief The actual original file name that was used to build the primary
491 std::string ActualOriginalFileName;
493 /// \brief The file ID for the original file that was used to build the
494 /// primary AST file.
495 FileID OriginalFileID;
497 /// \brief The directory that the PCH was originally created in. Used to
498 /// allow resolving headers even after headers+PCH was moved to a new path.
499 std::string OriginalDir;
501 /// \brief The directory that the PCH we are reading is stored in.
502 std::string CurrentDir;
504 /// \brief Whether this precompiled header is a relocatable PCH file.
507 /// \brief The system include root to be used when loading the
508 /// precompiled header.
509 std::string isysroot;
511 /// \brief Whether to disable the normal validation performed on precompiled
512 /// headers when they are loaded.
513 bool DisableValidation;
515 /// \brief Whether to disable the use of stat caches in AST files.
516 bool DisableStatCache;
518 /// \brief Mapping from switch-case IDs in the chain to switch-case statements
520 /// Statements usually don't have IDs, but switch cases need them, so that the
521 /// switch statement can refer to them.
522 std::map<unsigned, SwitchCase *> SwitchCaseStmts;
524 /// \brief Mapping from opaque value IDs to OpaqueValueExprs.
525 std::map<unsigned, OpaqueValueExpr*> OpaqueValueExprs;
527 /// \brief The number of stat() calls that hit/missed the stat
529 unsigned NumStatHits, NumStatMisses;
531 /// \brief The number of source location entries de-serialized from
533 unsigned NumSLocEntriesRead;
535 /// \brief The number of source location entries in the chain.
536 unsigned TotalNumSLocEntries;
538 /// \brief The number of statements (and expressions) de-serialized
540 unsigned NumStatementsRead;
542 /// \brief The total number of statements (and expressions) stored
544 unsigned TotalNumStatements;
546 /// \brief The number of macros de-serialized from the chain.
547 unsigned NumMacrosRead;
549 /// \brief The total number of macros stored in the chain.
550 unsigned TotalNumMacros;
552 /// \brief The number of selectors that have been read.
553 unsigned NumSelectorsRead;
555 /// \brief The number of method pool entries that have been read.
556 unsigned NumMethodPoolEntriesRead;
558 /// \brief The number of times we have looked up a selector in the method
559 /// pool and not found anything interesting.
560 unsigned NumMethodPoolMisses;
562 /// \brief The total number of method pool entries in the selector table.
563 unsigned TotalNumMethodPoolEntries;
565 /// Number of lexical decl contexts read/total.
566 unsigned NumLexicalDeclContextsRead, TotalLexicalDeclContexts;
568 /// Number of visible decl contexts read/total.
569 unsigned NumVisibleDeclContextsRead, TotalVisibleDeclContexts;
571 /// Total size of modules, in bits, currently loaded
572 uint64_t TotalModulesSizeInBits;
574 /// \brief Number of Decl/types that are currently deserializing.
575 unsigned NumCurrentElementsDeserializing;
577 /// Number of CXX base specifiers currently loaded
578 unsigned NumCXXBaseSpecifiersLoaded;
580 /// \brief An IdentifierInfo that has been loaded but whose top-level
581 /// declarations of the same name have not (yet) been loaded.
582 struct PendingIdentifierInfo {
584 SmallVector<uint32_t, 4> DeclIDs;
587 /// \brief The set of identifiers that were read while the AST reader was
588 /// (recursively) loading declarations.
590 /// The declarations on the identifier chain for these identifiers will be
591 /// loaded once the recursive loading has completed.
592 std::deque<PendingIdentifierInfo> PendingIdentifierInfos;
594 /// \brief Contains declarations and definitions that will be
595 /// "interesting" to the ASTConsumer, when we get that AST consumer.
597 /// "Interesting" declarations are those that have data that may
598 /// need to be emitted, such as inline function definitions or
599 /// Objective-C protocols.
600 std::deque<Decl *> InterestingDecls;
602 /// \brief We delay loading of the previous declaration chain to avoid
603 /// deeply nested calls when there are many redeclarations.
604 std::deque<std::pair<Decl *, serialization::DeclID> > PendingPreviousDecls;
606 /// \brief Ready to load the previous declaration of the given Decl.
607 void loadAndAttachPreviousDecl(Decl *D, serialization::DeclID ID);
609 /// \brief When reading a Stmt tree, Stmt operands are placed in this stack.
610 SmallVector<Stmt *, 16> StmtStack;
612 /// \brief What kind of records we are reading.
614 Read_Decl, Read_Type, Read_Stmt
617 /// \brief What kind of records we are reading.
618 ReadingKind ReadingKind;
620 /// \brief RAII object to change the reading kind.
621 class ReadingKindTracker {
623 enum ReadingKind PrevKind;
625 ReadingKindTracker(const ReadingKindTracker&); // do not implement
626 ReadingKindTracker &operator=(const ReadingKindTracker&);// do not implement
629 ReadingKindTracker(enum ReadingKind newKind, ASTReader &reader)
630 : Reader(reader), PrevKind(Reader.ReadingKind) {
631 Reader.ReadingKind = newKind;
634 ~ReadingKindTracker() { Reader.ReadingKind = PrevKind; }
637 /// \brief All predefines buffers in the chain, to be treated as if
639 PCHPredefinesBlocks PCHPredefinesBuffers;
641 /// \brief Suggested contents of the predefines buffer, after this
642 /// PCH file has been processed.
644 /// In most cases, this string will be empty, because the predefines
645 /// buffer computed to build the PCH file will be identical to the
646 /// predefines buffer computed from the command line. However, when
647 /// there are differences that the PCH reader can work around, this
648 /// predefines buffer may contain additional definitions.
649 std::string SuggestedPredefines;
651 /// \brief Reads a statement from the specified cursor.
652 Stmt *ReadStmtFromStream(Module &F);
654 /// \brief Get a FileEntry out of stored-in-PCH filename, making sure we take
655 /// into account all the necessary relocations.
656 const FileEntry *getFileEntry(StringRef filename);
658 void MaybeAddSystemRootToFilename(std::string &Filename);
660 ASTReadResult ReadASTCore(StringRef FileName, ModuleKind Type,
662 ASTReadResult ReadASTBlock(Module &F);
663 bool CheckPredefinesBuffers();
664 bool ParseLineTable(Module &F, SmallVectorImpl<uint64_t> &Record);
665 ASTReadResult ReadSourceManagerBlock(Module &F);
666 ASTReadResult ReadSLocEntryRecord(int ID);
667 llvm::BitstreamCursor &SLocCursorForID(int ID);
668 SourceLocation getImportLocation(Module *F);
669 bool ParseLanguageOptions(const SmallVectorImpl<uint64_t> &Record);
671 struct RecordLocation {
672 RecordLocation(Module *M, uint64_t O)
678 QualType readTypeRecord(unsigned Index);
679 RecordLocation TypeCursorForIndex(unsigned Index);
680 void LoadedDecl(unsigned Index, Decl *D);
681 Decl *ReadDeclRecord(serialization::DeclID ID);
682 RecordLocation DeclCursorForID(serialization::DeclID ID);
683 void loadDeclUpdateRecords(serialization::DeclID ID, Decl *D);
684 void loadObjCChainedCategories(serialization::GlobalDeclID ID,
685 ObjCInterfaceDecl *D);
687 RecordLocation getLocalBitOffset(uint64_t GlobalOffset);
688 uint64_t getGlobalBitOffset(Module &M, uint32_t LocalOffset);
690 /// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
691 serialization::PreprocessedEntityID
692 findBeginPreprocessedEntity(SourceLocation BLoc) const;
694 /// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
695 serialization::PreprocessedEntityID
696 findEndPreprocessedEntity(SourceLocation ELoc) const;
698 /// \brief \arg SLocMapI points at a chunk of a module that contains no
699 /// preprocessed entities or the entities it contains are not the ones we are
700 /// looking for. Find the next module that contains entities and return the ID
701 /// of the first entry.
702 serialization::PreprocessedEntityID
703 findNextPreprocessedEntity(
704 GlobalSLocOffsetMapType::const_iterator SLocMapI) const;
706 void PassInterestingDeclsToConsumer();
708 /// \brief Produce an error diagnostic and return true.
710 /// This routine should only be used for fatal errors that have to
711 /// do with non-routine failures (e.g., corrupted AST file).
712 void Error(StringRef Msg);
713 void Error(unsigned DiagID, StringRef Arg1 = StringRef(),
714 StringRef Arg2 = StringRef());
716 ASTReader(const ASTReader&); // do not implement
717 ASTReader &operator=(const ASTReader &); // do not implement
719 typedef SmallVector<uint64_t, 64> RecordData;
721 /// \brief Load the AST file and validate its contents against the given
724 /// \param PP the preprocessor associated with the context in which this
725 /// precompiled header will be loaded.
727 /// \param Context the AST context that this precompiled header will be
730 /// \param isysroot If non-NULL, the system include path specified by the
731 /// user. This is only used with relocatable PCH files. If non-NULL,
732 /// a relocatable PCH file will use the default path "/".
734 /// \param DisableValidation If true, the AST reader will suppress most
735 /// of its regular consistency checking, allowing the use of precompiled
736 /// headers that cannot be determined to be compatible.
738 /// \param DisableStatCache If true, the AST reader will ignore the
739 /// stat cache in the AST files. This performance pessimization can
740 /// help when an AST file is being used in cases where the
741 /// underlying files in the file system may have changed, but
742 /// parsing should still continue.
743 ASTReader(Preprocessor &PP, ASTContext &Context, StringRef isysroot = "",
744 bool DisableValidation = false, bool DisableStatCache = false);
748 SourceManager &getSourceManager() const { return SourceMgr; }
750 /// \brief Load the AST file designated by the given file name.
751 ASTReadResult ReadAST(const std::string &FileName, ModuleKind Type);
753 /// \brief Checks that no file that is stored in PCH is out-of-sync with
754 /// the actual file in the file system.
755 ASTReadResult validateFileEntries(Module &M);
757 /// \brief Set the AST callbacks listener.
758 void setListener(ASTReaderListener *listener) {
759 Listener.reset(listener);
762 /// \brief Set the AST deserialization listener.
763 void setDeserializationListener(ASTDeserializationListener *Listener);
765 /// \brief Initializes the ASTContext
766 void InitializeContext();
768 /// \brief Add in-memory (virtual file) buffer.
769 void addInMemoryBuffer(StringRef &FileName, llvm::MemoryBuffer *Buffer) {
770 ModuleMgr.addInMemoryBuffer(FileName, Buffer);
773 /// \brief Retrieve the module manager.
774 ModuleManager &getModuleManager() { return ModuleMgr; }
776 /// \brief Retrieve the preprocessor.
777 Preprocessor &getPreprocessor() const { return PP; }
779 /// \brief Retrieve the name of the original source file name
780 const std::string &getOriginalSourceFile() { return OriginalFileName; }
782 /// \brief Retrieve the name of the original source file name directly from
783 /// the AST file, without actually loading the AST file.
784 static std::string getOriginalSourceFile(const std::string &ASTFileName,
785 FileManager &FileMgr,
786 DiagnosticsEngine &Diags);
788 /// \brief Returns the suggested contents of the predefines buffer,
789 /// which contains a (typically-empty) subset of the predefines
790 /// build prior to including the precompiled header.
791 const std::string &getSuggestedPredefines() { return SuggestedPredefines; }
793 /// \brief Read a preallocated preprocessed entity from the external source.
795 /// \returns null if an error occurred that prevented the preprocessed
796 /// entity from being loaded.
797 virtual PreprocessedEntity *ReadPreprocessedEntity(unsigned Index);
799 /// \brief Returns a pair of [Begin, End) indices of preallocated
800 /// preprocessed entities that \arg Range encompasses.
801 virtual std::pair<unsigned, unsigned>
802 findPreprocessedEntitiesInRange(SourceRange Range);
804 /// \brief Read the header file information for the given file entry.
805 virtual HeaderFileInfo GetHeaderFileInfo(const FileEntry *FE);
807 void ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag);
809 /// \brief Returns the number of source locations found in the chain.
810 unsigned getTotalNumSLocs() const {
811 return TotalNumSLocEntries;
814 /// \brief Returns the number of identifiers found in the chain.
815 unsigned getTotalNumIdentifiers() const {
816 return static_cast<unsigned>(IdentifiersLoaded.size());
819 /// \brief Returns the number of types found in the chain.
820 unsigned getTotalNumTypes() const {
821 return static_cast<unsigned>(TypesLoaded.size());
824 /// \brief Returns the number of declarations found in the chain.
825 unsigned getTotalNumDecls() const {
826 return static_cast<unsigned>(DeclsLoaded.size());
829 /// \brief Returns the number of selectors found in the chain.
830 unsigned getTotalNumSelectors() const {
831 return static_cast<unsigned>(SelectorsLoaded.size());
834 /// \brief Returns the number of preprocessed entities known to the AST
836 unsigned getTotalNumPreprocessedEntities() const {
838 for (ModuleConstIterator I = ModuleMgr.begin(),
839 E = ModuleMgr.end(); I != E; ++I) {
840 Result += (*I)->NumPreprocessedEntities;
846 /// \brief Returns the number of C++ base specifiers found in the chain.
847 unsigned getTotalNumCXXBaseSpecifiers() const {
848 return NumCXXBaseSpecifiersLoaded;
851 /// \brief Reads a TemplateArgumentLocInfo appropriate for the
852 /// given TemplateArgument kind.
853 TemplateArgumentLocInfo
854 GetTemplateArgumentLocInfo(Module &F, TemplateArgument::ArgKind Kind,
855 const RecordData &Record, unsigned &Idx);
857 /// \brief Reads a TemplateArgumentLoc.
859 ReadTemplateArgumentLoc(Module &F,
860 const RecordData &Record, unsigned &Idx);
862 /// \brief Reads a declarator info from the given record.
863 TypeSourceInfo *GetTypeSourceInfo(Module &F,
864 const RecordData &Record, unsigned &Idx);
866 /// \brief Resolve a type ID into a type, potentially building a new
868 QualType GetType(serialization::TypeID ID);
870 /// \brief Resolve a local type ID within a given AST file into a type.
871 QualType getLocalType(Module &F, unsigned LocalID);
873 /// \brief Map a local type ID within a given AST file into a global type ID.
874 serialization::TypeID getGlobalTypeID(Module &F, unsigned LocalID) const;
876 /// \brief Read a type from the current position in the given record, which
877 /// was read from the given AST file.
878 QualType readType(Module &F, const RecordData &Record, unsigned &Idx) {
879 if (Idx >= Record.size())
882 return getLocalType(F, Record[Idx++]);
885 /// \brief Map from a local declaration ID within a given module to a
886 /// global declaration ID.
887 serialization::DeclID getGlobalDeclID(Module &F, unsigned LocalID) const;
889 /// \brief Returns true if global DeclID \arg ID originated from module
891 bool isDeclIDFromModule(serialization::GlobalDeclID ID, Module &M) const;
893 /// \brief Resolve a declaration ID into a declaration, potentially
894 /// building a new declaration.
895 Decl *GetDecl(serialization::DeclID ID);
896 virtual Decl *GetExternalDecl(uint32_t ID);
898 /// \brief Reads a declaration with the given local ID in the given module.
899 Decl *GetLocalDecl(Module &F, uint32_t LocalID) {
900 return GetDecl(getGlobalDeclID(F, LocalID));
903 /// \brief Reads a declaration with the given local ID in the given module.
905 /// \returns The requested declaration, casted to the given return type.
907 T *GetLocalDeclAs(Module &F, uint32_t LocalID) {
908 return cast_or_null<T>(GetLocalDecl(F, LocalID));
911 /// \brief Reads a declaration ID from the given position in a record in the
914 /// \returns The declaration ID read from the record, adjusted to a global ID.
915 serialization::DeclID ReadDeclID(Module &F, const RecordData &Record,
918 /// \brief Reads a declaration from the given position in a record in the
920 Decl *ReadDecl(Module &F, const RecordData &R, unsigned &I) {
921 return GetDecl(ReadDeclID(F, R, I));
924 /// \brief Reads a declaration from the given position in a record in the
927 /// \returns The declaration read from this location, casted to the given
930 T *ReadDeclAs(Module &F, const RecordData &R, unsigned &I) {
931 return cast_or_null<T>(GetDecl(ReadDeclID(F, R, I)));
934 /// \brief Read a CXXBaseSpecifiers ID form the given record and
935 /// return its global bit offset.
936 uint64_t readCXXBaseSpecifiers(Module &M, const RecordData &Record,
939 virtual CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset);
941 /// \brief Resolve the offset of a statement into a statement.
943 /// This operation will read a new statement from the external
944 /// source each time it is called, and is meant to be used via a
945 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
946 virtual Stmt *GetExternalDeclStmt(uint64_t Offset);
948 /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
949 /// specified cursor. Read the abbreviations that are at the top of the block
950 /// and then leave the cursor pointing into the block.
951 bool ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor, unsigned BlockID);
953 /// \brief Finds all the visible declarations with a given name.
954 /// The current implementation of this method just loads the entire
955 /// lookup table as unmaterialized references.
956 virtual DeclContext::lookup_result
957 FindExternalVisibleDeclsByName(const DeclContext *DC,
958 DeclarationName Name);
960 /// \brief Read all of the declarations lexically stored in a
961 /// declaration context.
963 /// \param DC The declaration context whose declarations will be
966 /// \param Decls Vector that will contain the declarations loaded
967 /// from the external source. The caller is responsible for merging
968 /// these declarations with any declarations already stored in the
969 /// declaration context.
971 /// \returns true if there was an error while reading the
972 /// declarations for this declaration context.
973 virtual ExternalLoadResult FindExternalLexicalDecls(const DeclContext *DC,
974 bool (*isKindWeWant)(Decl::Kind),
975 SmallVectorImpl<Decl*> &Decls);
977 /// \brief Notify ASTReader that we started deserialization of
978 /// a decl or type so until FinishedDeserializing is called there may be
979 /// decls that are initializing. Must be paired with FinishedDeserializing.
980 virtual void StartedDeserializing() { ++NumCurrentElementsDeserializing; }
982 /// \brief Notify ASTReader that we finished the deserialization of
983 /// a decl or type. Must be paired with StartedDeserializing.
984 virtual void FinishedDeserializing();
986 /// \brief Function that will be invoked when we begin parsing a new
987 /// translation unit involving this external AST source.
989 /// This function will provide all of the external definitions to
991 virtual void StartTranslationUnit(ASTConsumer *Consumer);
993 /// \brief Print some statistics about AST usage.
994 virtual void PrintStats();
996 /// \brief Dump information about the AST reader to standard error.
999 /// Return the amount of memory used by memory buffers, breaking down
1000 /// by heap-backed versus mmap'ed memory.
1001 virtual void getMemoryBufferSizes(MemoryBufferSizes &sizes) const;
1003 /// \brief Initialize the semantic source with the Sema instance
1004 /// being used to perform semantic analysis on the abstract syntax
1006 virtual void InitializeSema(Sema &S);
1008 /// \brief Inform the semantic consumer that Sema is no longer available.
1009 virtual void ForgetSema() { SemaObj = 0; }
1011 /// \brief Retrieve the IdentifierInfo for the named identifier.
1013 /// This routine builds a new IdentifierInfo for the given identifier. If any
1014 /// declarations with this name are visible from translation unit scope, their
1015 /// declarations will be deserialized and introduced into the declaration
1016 /// chain of the identifier.
1017 virtual IdentifierInfo *get(const char *NameStart, const char *NameEnd);
1018 IdentifierInfo *get(StringRef Name) {
1019 return get(Name.begin(), Name.end());
1022 /// \brief Retrieve an iterator into the set of all identifiers
1023 /// in all loaded AST files.
1024 virtual IdentifierIterator *getIdentifiers() const;
1026 /// \brief Load the contents of the global method pool for a given
1029 /// \returns a pair of Objective-C methods lists containing the
1030 /// instance and factory methods, respectively, with this selector.
1031 virtual std::pair<ObjCMethodList, ObjCMethodList>
1032 ReadMethodPool(Selector Sel);
1034 /// \brief Load the set of namespaces that are known to the external source,
1035 /// which will be used during typo correction.
1036 virtual void ReadKnownNamespaces(
1037 SmallVectorImpl<NamespaceDecl *> &Namespaces);
1039 virtual void ReadTentativeDefinitions(
1040 SmallVectorImpl<VarDecl *> &TentativeDefs);
1042 virtual void ReadUnusedFileScopedDecls(
1043 SmallVectorImpl<const DeclaratorDecl *> &Decls);
1045 virtual void ReadDelegatingConstructors(
1046 SmallVectorImpl<CXXConstructorDecl *> &Decls);
1048 virtual void ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls);
1050 virtual void ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls);
1052 virtual void ReadLocallyScopedExternalDecls(
1053 SmallVectorImpl<NamedDecl *> &Decls);
1055 virtual void ReadReferencedSelectors(
1056 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels);
1058 virtual void ReadWeakUndeclaredIdentifiers(
1059 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WI);
1061 virtual void ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables);
1063 virtual void ReadPendingInstantiations(
1064 SmallVectorImpl<std::pair<ValueDecl *,
1065 SourceLocation> > &Pending);
1067 /// \brief Load a selector from disk, registering its ID if it exists.
1068 void LoadSelector(Selector Sel);
1070 void SetIdentifierInfo(unsigned ID, IdentifierInfo *II);
1071 void SetGloballyVisibleDecls(IdentifierInfo *II,
1072 const SmallVectorImpl<uint32_t> &DeclIDs,
1073 bool Nonrecursive = false);
1075 /// \brief Report a diagnostic.
1076 DiagnosticBuilder Diag(unsigned DiagID);
1078 /// \brief Report a diagnostic.
1079 DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID);
1081 IdentifierInfo *DecodeIdentifierInfo(serialization::IdentifierID ID);
1083 IdentifierInfo *GetIdentifierInfo(Module &M, const RecordData &Record,
1085 return DecodeIdentifierInfo(getGlobalIdentifierID(M, Record[Idx++]));
1088 virtual IdentifierInfo *GetIdentifier(serialization::IdentifierID ID) {
1089 return DecodeIdentifierInfo(ID);
1092 IdentifierInfo *getLocalIdentifier(Module &M, unsigned LocalID);
1094 serialization::IdentifierID getGlobalIdentifierID(Module &M,
1097 /// \brief Read the source location entry with index ID.
1098 virtual bool ReadSLocEntry(int ID);
1100 /// \brief Retrieve a selector from the given module with its local ID
1102 Selector getLocalSelector(Module &M, unsigned LocalID);
1104 Selector DecodeSelector(serialization::SelectorID Idx);
1106 virtual Selector GetExternalSelector(serialization::SelectorID ID);
1107 uint32_t GetNumExternalSelectors();
1109 Selector ReadSelector(Module &M, const RecordData &Record, unsigned &Idx) {
1110 return getLocalSelector(M, Record[Idx++]);
1113 /// \brief Retrieve the global selector ID that corresponds to this
1114 /// the local selector ID in a given module.
1115 serialization::SelectorID getGlobalSelectorID(Module &F,
1116 unsigned LocalID) const;
1118 /// \brief Read a declaration name.
1119 DeclarationName ReadDeclarationName(Module &F,
1120 const RecordData &Record, unsigned &Idx);
1121 void ReadDeclarationNameLoc(Module &F,
1122 DeclarationNameLoc &DNLoc, DeclarationName Name,
1123 const RecordData &Record, unsigned &Idx);
1124 void ReadDeclarationNameInfo(Module &F, DeclarationNameInfo &NameInfo,
1125 const RecordData &Record, unsigned &Idx);
1127 void ReadQualifierInfo(Module &F, QualifierInfo &Info,
1128 const RecordData &Record, unsigned &Idx);
1130 NestedNameSpecifier *ReadNestedNameSpecifier(Module &F,
1131 const RecordData &Record,
1134 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc(Module &F,
1135 const RecordData &Record,
1138 /// \brief Read a template name.
1139 TemplateName ReadTemplateName(Module &F, const RecordData &Record,
1142 /// \brief Read a template argument.
1143 TemplateArgument ReadTemplateArgument(Module &F,
1144 const RecordData &Record,unsigned &Idx);
1146 /// \brief Read a template parameter list.
1147 TemplateParameterList *ReadTemplateParameterList(Module &F,
1148 const RecordData &Record,
1151 /// \brief Read a template argument array.
1153 ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
1154 Module &F, const RecordData &Record,
1157 /// \brief Read a UnresolvedSet structure.
1158 void ReadUnresolvedSet(Module &F, UnresolvedSetImpl &Set,
1159 const RecordData &Record, unsigned &Idx);
1161 /// \brief Read a C++ base specifier.
1162 CXXBaseSpecifier ReadCXXBaseSpecifier(Module &F,
1163 const RecordData &Record,unsigned &Idx);
1165 /// \brief Read a CXXCtorInitializer array.
1166 std::pair<CXXCtorInitializer **, unsigned>
1167 ReadCXXCtorInitializers(Module &F, const RecordData &Record,
1170 /// \brief Read a source location from raw form.
1171 SourceLocation ReadSourceLocation(Module &Module, unsigned Raw) const {
1172 SourceLocation Loc = SourceLocation::getFromRawEncoding(Raw);
1173 assert(Module.SLocRemap.find(Loc.getOffset()) != Module.SLocRemap.end() &&
1174 "Cannot find offset to remap.");
1175 int Remap = Module.SLocRemap.find(Loc.getOffset())->second;
1176 return Loc.getLocWithOffset(Remap);
1179 /// \brief Read a source location.
1180 SourceLocation ReadSourceLocation(Module &Module,
1181 const RecordData &Record, unsigned& Idx) {
1182 return ReadSourceLocation(Module, Record[Idx++]);
1185 /// \brief Read a source range.
1186 SourceRange ReadSourceRange(Module &F,
1187 const RecordData &Record, unsigned& Idx);
1189 /// \brief Read an integral value
1190 llvm::APInt ReadAPInt(const RecordData &Record, unsigned &Idx);
1192 /// \brief Read a signed integral value
1193 llvm::APSInt ReadAPSInt(const RecordData &Record, unsigned &Idx);
1195 /// \brief Read a floating-point value
1196 llvm::APFloat ReadAPFloat(const RecordData &Record, unsigned &Idx);
1198 // \brief Read a string
1199 std::string ReadString(const RecordData &Record, unsigned &Idx);
1201 /// \brief Read a version tuple.
1202 VersionTuple ReadVersionTuple(const RecordData &Record, unsigned &Idx);
1204 CXXTemporary *ReadCXXTemporary(Module &F, const RecordData &Record,
1207 /// \brief Reads attributes from the current stream position.
1208 void ReadAttributes(Module &F, AttrVec &Attrs,
1209 const RecordData &Record, unsigned &Idx);
1211 /// \brief Reads a statement.
1212 Stmt *ReadStmt(Module &F);
1214 /// \brief Reads an expression.
1215 Expr *ReadExpr(Module &F);
1217 /// \brief Reads a sub-statement operand during statement reading.
1218 Stmt *ReadSubStmt() {
1219 assert(ReadingKind == Read_Stmt &&
1220 "Should be called only during statement reading!");
1221 // Subexpressions are stored from last to first, so the next Stmt we need
1222 // is at the back of the stack.
1223 assert(!StmtStack.empty() && "Read too many sub statements!");
1224 return StmtStack.pop_back_val();
1227 /// \brief Reads a sub-expression operand during statement reading.
1228 Expr *ReadSubExpr();
1230 /// \brief Reads the macro record located at the given offset.
1231 void ReadMacroRecord(Module &F, uint64_t Offset);
1233 /// \brief Determine the global preprocessed entity ID that corresponds to
1234 /// the given local ID within the given module.
1235 serialization::PreprocessedEntityID
1236 getGlobalPreprocessedEntityID(Module &M, unsigned LocalID) const;
1238 /// \brief Note that the identifier is a macro whose record will be loaded
1239 /// from the given AST file at the given (file-local) offset.
1240 void SetIdentifierIsMacro(IdentifierInfo *II, Module &F,
1243 /// \brief Read the set of macros defined by this external macro source.
1244 virtual void ReadDefinedMacros();
1246 /// \brief Read the macro definition for this identifier.
1247 virtual void LoadMacroDefinition(IdentifierInfo *II);
1249 /// \brief Read the macro definition corresponding to this iterator
1250 /// into the unread macro record offsets table.
1251 void LoadMacroDefinition(
1252 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos);
1254 /// \brief Retrieve the AST context that this AST reader supplements.
1255 ASTContext &getContext() { return Context; }
1257 // \brief Contains declarations that were loaded before we have
1258 // access to a Sema object.
1259 SmallVector<NamedDecl *, 16> PreloadedDecls;
1261 /// \brief Retrieve the semantic analysis object used to analyze the
1262 /// translation unit in which the precompiled header is being
1264 Sema *getSema() { return SemaObj; }
1266 /// \brief Retrieve the identifier table associated with the
1268 IdentifierTable &getIdentifierTable();
1270 /// \brief Record that the given ID maps to the given switch-case
1272 void RecordSwitchCaseID(SwitchCase *SC, unsigned ID);
1274 /// \brief Retrieve the switch-case statement with the given ID.
1275 SwitchCase *getSwitchCaseWithID(unsigned ID);
1277 void ClearSwitchCaseIDs();
1280 /// \brief Helper class that saves the current stream position and
1281 /// then restores it when destroyed.
1282 struct SavedStreamPosition {
1283 explicit SavedStreamPosition(llvm::BitstreamCursor &Cursor)
1284 : Cursor(Cursor), Offset(Cursor.GetCurrentBitNo()) { }
1286 ~SavedStreamPosition() {
1287 Cursor.JumpToBit(Offset);
1291 llvm::BitstreamCursor &Cursor;
1295 inline void PCHValidator::Error(const char *Msg) {
1299 } // end namespace clang