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/Sema/ExternalSemaSource.h"
19 #include "clang/AST/DeclarationName.h"
20 #include "clang/AST/DeclObjC.h"
21 #include "clang/AST/TemplateBase.h"
22 #include "clang/Lex/ExternalPreprocessorSource.h"
23 #include "clang/Lex/PreprocessingRecord.h"
24 #include "clang/Basic/Diagnostic.h"
25 #include "clang/Basic/IdentifierTable.h"
26 #include "clang/Basic/SourceManager.h"
27 #include "llvm/ADT/APFloat.h"
28 #include "llvm/ADT/APInt.h"
29 #include "llvm/ADT/APSInt.h"
30 #include "llvm/ADT/OwningPtr.h"
31 #include "llvm/ADT/SmallVector.h"
32 #include "llvm/ADT/StringRef.h"
33 #include "llvm/Bitcode/BitstreamReader.h"
34 #include "llvm/System/DataTypes.h"
50 class ASTIdentifierIterator
;
54 class NestedNameSpecifier
;
55 class CXXBaseSpecifier
;
56 class CXXBaseOrMemberInitializer
;
59 class MacroDefinition
;
64 class ASTDeserializationListener
;
68 class ASTIdentifierLookupTrait
;
70 class FileSystemOptions
;
71 struct HeaderFileInfo
;
73 struct PCHPredefinesBlock
{
74 /// \brief The file ID for this predefines buffer in a PCH file.
77 /// \brief This predefines buffer in a PCH file.
80 typedef llvm::SmallVector
<PCHPredefinesBlock
, 2> PCHPredefinesBlocks
;
82 /// \brief Abstract interface for callback invocations by the ASTReader.
84 /// While reading an AST file, the ASTReader will call the methods of the
85 /// listener to pass on specific information. Some of the listener methods can
86 /// return true to indicate to the ASTReader that the information (and
87 /// consequently the AST file) is invalid.
88 class ASTReaderListener
{
90 virtual ~ASTReaderListener();
92 /// \brief Receives the language options.
94 /// \returns true to indicate the options are invalid or false otherwise.
95 virtual bool ReadLanguageOptions(const LangOptions
&LangOpts
) {
99 /// \brief Receives the target triple.
101 /// \returns true to indicate the target triple is invalid or false otherwise.
102 virtual bool ReadTargetTriple(llvm::StringRef Triple
) {
106 /// \brief Receives the contents of the predefines buffer.
108 /// \param Buffers Information about the predefines buffers.
110 /// \param OriginalFileName The original file name for the AST file, which
111 /// will appear as an entry in the predefines buffer.
113 /// \param SuggestedPredefines If necessary, additional definitions are added
116 /// \returns true to indicate the predefines are invalid or false otherwise.
117 virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks
&Buffers
,
118 llvm::StringRef OriginalFileName
,
119 std::string
&SuggestedPredefines
) {
123 /// \brief Receives a HeaderFileInfo entry.
124 virtual void ReadHeaderFileInfo(const HeaderFileInfo
&HFI
, unsigned ID
) {}
126 /// \brief Receives __COUNTER__ value.
127 virtual void ReadCounter(unsigned Value
) {}
130 /// \brief ASTReaderListener implementation to validate the information of
131 /// the PCH file against an initialized Preprocessor.
132 class PCHValidator
: public ASTReaderListener
{
136 unsigned NumHeaderInfos
;
139 PCHValidator(Preprocessor
&PP
, ASTReader
&Reader
)
140 : PP(PP
), Reader(Reader
), NumHeaderInfos(0) {}
142 virtual bool ReadLanguageOptions(const LangOptions
&LangOpts
);
143 virtual bool ReadTargetTriple(llvm::StringRef Triple
);
144 virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks
&Buffers
,
145 llvm::StringRef OriginalFileName
,
146 std::string
&SuggestedPredefines
);
147 virtual void ReadHeaderFileInfo(const HeaderFileInfo
&HFI
, unsigned ID
);
148 virtual void ReadCounter(unsigned Value
);
151 void Error(const char *Msg
);
154 /// \brief Reads an AST files chain containing the contents of a translation
157 /// The ASTReader class reads bitstreams (produced by the ASTWriter
158 /// class) containing the serialized representation of a given
159 /// abstract syntax tree and its supporting data structures. An
160 /// instance of the ASTReader can be attached to an ASTContext object,
161 /// which will provide access to the contents of the AST files.
163 /// The AST reader provides lazy de-serialization of declarations, as
164 /// required when traversing the AST. Only those AST nodes that are
165 /// actually required will be de-serialized.
167 : public ExternalPreprocessorSource
,
168 public ExternalPreprocessingRecordSource
,
169 public ExternalSemaSource
,
170 public IdentifierInfoLookup
,
171 public ExternalIdentifierLookup
,
172 public ExternalSLocEntrySource
{
174 enum ASTReadResult
{ Success
, Failure
, IgnorePCH
};
175 /// \brief Types of AST files.
177 Module
, ///< File is a module proper.
178 PCH
, ///< File is a PCH file treated as such.
179 Preamble
, ///< File is a PCH file treated as the preamble.
180 MainFile
///< File is a PCH file treated as the actual main file.
182 friend class PCHValidator
;
183 friend class ASTDeclReader
;
184 friend class ASTStmtReader
;
185 friend class ASTIdentifierIterator
;
186 friend class ASTIdentifierLookupTrait
;
187 friend class TypeLocReader
;
189 /// \brief The receiver of some callbacks invoked by ASTReader.
190 llvm::OwningPtr
<ASTReaderListener
> Listener
;
192 /// \brief The receiver of deserialization events.
193 ASTDeserializationListener
*DeserializationListener
;
195 SourceManager
&SourceMgr
;
196 FileManager
&FileMgr
;
197 const FileSystemOptions
&FileSystemOpts
;
200 /// \brief The semantic analysis object that will be processing the
201 /// AST files and the translation unit that uses it.
204 /// \brief The preprocessor that will be loading the source file.
207 /// \brief The AST context into which we'll read the AST files.
210 /// \brief The AST consumer.
211 ASTConsumer
*Consumer
;
213 /// \brief Information that is needed for every module.
215 PerFileData(ASTFileType Ty
);
218 // === General information ===
220 /// \brief The type of this AST file.
223 /// \brief The file name of the AST file.
224 std::string FileName
;
226 /// \brief The memory buffer that stores the data associated with
228 llvm::OwningPtr
<llvm::MemoryBuffer
> Buffer
;
230 /// \brief The size of this file, in bits.
233 /// \brief The bitstream reader from which we'll read the AST file.
234 llvm::BitstreamReader StreamFile
;
236 /// \brief The main bitstream cursor for the main block.
237 llvm::BitstreamCursor Stream
;
239 // === Source Locations ===
241 /// \brief Cursor used to read source location entries.
242 llvm::BitstreamCursor SLocEntryCursor
;
244 /// \brief The number of source location entries in this AST file.
245 unsigned LocalNumSLocEntries
;
247 /// \brief Offsets for all of the source location entries in the
249 const uint32_t *SLocOffsets
;
251 /// \brief The entire size of this module's source location offset range.
252 unsigned LocalSLocSize
;
254 // === Identifiers ===
256 /// \brief The number of identifiers in this AST file.
257 unsigned LocalNumIdentifiers
;
259 /// \brief Offsets into the identifier table data.
261 /// This array is indexed by the identifier ID (-1), and provides
262 /// the offset into IdentifierTableData where the string data is
264 const uint32_t *IdentifierOffsets
;
266 /// \brief Actual data for the on-disk hash table.
268 /// This pointer points into a memory buffer, where the on-disk hash
269 /// table for identifiers actually lives.
270 const char *IdentifierTableData
;
272 /// \brief A pointer to an on-disk hash table of opaque type
273 /// IdentifierHashTable.
274 void *IdentifierLookupTable
;
278 /// \brief The cursor to the start of the preprocessor block, which stores
279 /// all of the macro definitions.
280 llvm::BitstreamCursor MacroCursor
;
282 /// \brief The offset of the start of the set of defined macros.
283 uint64_t MacroStartOffset
;
285 /// \brief The number of macro definitions in this file.
286 unsigned LocalNumMacroDefinitions
;
288 /// \brief Offsets of all of the macro definitions in the preprocessing
289 /// record in the AST file.
290 const uint32_t *MacroDefinitionOffsets
;
294 /// \brief The number of selectors new to this file.
296 /// This is the number of entries in SelectorOffsets.
297 unsigned LocalNumSelectors
;
299 /// \brief Offsets into the selector lookup table's data array
300 /// where each selector resides.
301 const uint32_t *SelectorOffsets
;
303 /// \brief A pointer to the character data that comprises the selector table
305 /// The SelectorOffsets table refers into this memory.
306 const unsigned char *SelectorLookupTableData
;
308 /// \brief A pointer to an on-disk hash table of opaque type
309 /// ASTSelectorLookupTable.
311 /// This hash table provides the IDs of all selectors, and the associated
312 /// instance and factory methods.
313 void *SelectorLookupTable
;
315 /// \brief Method selectors used in a @selector expression. Used for
316 /// implementation of -Wselector.
317 llvm::SmallVector
<uint64_t, 64> ReferencedSelectorsData
;
319 // === Declarations ===
321 /// DeclsCursor - This is a cursor to the start of the DECLS_BLOCK block. It
322 /// has read all the abbreviations at the start of the block and is ready to
323 /// jump around with these in context.
324 llvm::BitstreamCursor DeclsCursor
;
326 /// \brief The number of declarations in this AST file.
327 unsigned LocalNumDecls
;
329 /// \brief Offset of each declaration within the bitstream, indexed
330 /// by the declaration ID (-1).
331 const uint32_t *DeclOffsets
;
333 /// \brief A snapshot of the pending instantiations in the chain.
335 /// This record tracks the instantiations that Sema has to perform at the
336 /// end of the TU. It consists of a pair of values for every pending
337 /// instantiation where the first value is the ID of the decl and the second
338 /// is the instantiation location.
339 llvm::SmallVector
<uint64_t, 64> PendingInstantiations
;
341 /// \brief The number of C++ base specifier sets in this AST file.
342 unsigned LocalNumCXXBaseSpecifiers
;
344 /// \brief Offset of each C++ base specifier set within the bitstream,
345 /// indexed by the C++ base specifier set ID (-1).
346 const uint32_t *CXXBaseSpecifiersOffsets
;
350 /// \brief The number of types in this AST file.
351 unsigned LocalNumTypes
;
353 /// \brief Offset of each type within the bitstream, indexed by the
354 /// type ID, or the representation of a Type*.
355 const uint32_t *TypeOffsets
;
357 // === Miscellaneous ===
359 /// \brief The AST stat cache installed for this file, if any.
361 /// The dynamic type of this stat cache is always ASTStatCache
364 /// \brief The number of preallocated preprocessing entities in the
365 /// preprocessing record.
366 unsigned NumPreallocatedPreprocessingEntities
;
368 /// \brief The next module in source order.
369 PerFileData
*NextInSource
;
371 /// \brief All the modules that loaded this one. Can contain NULL for
372 /// directly loaded modules.
373 llvm::SmallVector
<PerFileData
*, 1> Loaders
;
376 /// \brief All loaded modules, indexed by name.
377 llvm::StringMap
<PerFileData
*> Modules
;
379 /// \brief The first module in source order.
380 PerFileData
*FirstInSource
;
382 /// \brief The chain of AST files. The first entry is the one named by the
383 /// user, the last one is the one that doesn't depend on anything further.
384 /// That is, the entry I was created with -include-pch I+1.
385 llvm::SmallVector
<PerFileData
*, 2> Chain
;
387 /// \brief SLocEntries that we're going to preload.
388 llvm::SmallVector
<uint64_t, 64> PreloadSLocEntries
;
390 /// \brief Types that have already been loaded from the chain.
392 /// When the pointer at index I is non-NULL, the type with
393 /// ID = (I + 1) << FastQual::Width has already been loaded
394 std::vector
<QualType
> TypesLoaded
;
396 /// \brief Map that provides the ID numbers of each type within the
397 /// output stream, plus those deserialized from a chained PCH.
399 /// The ID numbers of types are consecutive (in order of discovery)
400 /// and start at 1. 0 is reserved for NULL. When types are actually
401 /// stored in the stream, the ID number is shifted by 2 bits to
402 /// allow for the const/volatile qualifiers.
404 /// Keys in the map never have const/volatile qualifiers.
405 serialization::TypeIdxMap TypeIdxs
;
407 /// \brief Declarations that have already been loaded from the chain.
409 /// When the pointer at index I is non-NULL, the declaration with ID
410 /// = I + 1 has already been loaded.
411 std::vector
<Decl
*> DeclsLoaded
;
413 typedef std::pair
<PerFileData
*, uint64_t> FileOffset
;
414 typedef llvm::SmallVector
<FileOffset
, 2> FileOffsetsTy
;
415 typedef llvm::DenseMap
<serialization::DeclID
, FileOffsetsTy
>
416 DeclUpdateOffsetsMap
;
417 /// \brief Declarations that have modifications residing in a later file
419 DeclUpdateOffsetsMap DeclUpdateOffsets
;
421 typedef llvm::DenseMap
<serialization::DeclID
,
422 std::pair
<PerFileData
*, uint64_t> >
424 /// \brief Declarations that have been replaced in a later file in the chain.
425 DeclReplacementMap ReplacedDecls
;
427 /// \brief Information about the contents of a DeclContext.
428 struct DeclContextInfo
{
429 void *NameLookupTableData
; // a ASTDeclContextNameLookupTable.
430 const serialization::KindDeclIDPair
*LexicalDecls
;
431 unsigned NumLexicalDecls
;
433 // In a full chain, there could be multiple updates to every decl context,
434 // so this is a vector. However, typically a chain is only two elements long,
435 // with only one file containing updates, so there will be only one update
437 typedef llvm::SmallVector
<DeclContextInfo
, 1> DeclContextInfos
;
438 typedef llvm::DenseMap
<const DeclContext
*, DeclContextInfos
>
439 DeclContextOffsetsMap
;
440 // Updates for visible decls can occur for other contexts than just the
441 // TU, and when we read those update records, the actual context will not
442 // be available yet (unless it's the TU), so have this pending map using the
443 // ID as a key. It will be realized when the context is actually loaded.
444 typedef llvm::SmallVector
<void *, 1> DeclContextVisibleUpdates
;
445 typedef llvm::DenseMap
<serialization::DeclID
, DeclContextVisibleUpdates
>
446 DeclContextVisibleUpdatesPending
;
448 /// \brief Offsets of the lexical and visible declarations for each
450 DeclContextOffsetsMap DeclContextOffsets
;
452 /// \brief Updates to the visible declarations of declaration contexts that
453 /// haven't been loaded yet.
454 DeclContextVisibleUpdatesPending PendingVisibleUpdates
;
456 typedef llvm::SmallVector
<CXXRecordDecl
*, 4> ForwardRefs
;
457 typedef llvm::DenseMap
<const CXXRecordDecl
*, ForwardRefs
>
458 PendingForwardRefsMap
;
459 /// \brief Forward references that have a definition but the definition decl
460 /// is still initializing. When the definition gets read it will update
461 /// the DefinitionData pointer of all pending references.
462 PendingForwardRefsMap PendingForwardRefs
;
464 typedef llvm::DenseMap
<serialization::DeclID
, serialization::DeclID
>
465 FirstLatestDeclIDMap
;
466 /// \brief Map of first declarations from a chained PCH that point to the
467 /// most recent declarations in another AST file.
468 FirstLatestDeclIDMap FirstLatestDeclIDs
;
470 /// \brief Read the records that describe the contents of declcontexts.
471 bool ReadDeclContextStorage(llvm::BitstreamCursor
&Cursor
,
472 const std::pair
<uint64_t, uint64_t> &Offsets
,
473 DeclContextInfo
&Info
);
475 /// \brief A vector containing identifiers that have already been
478 /// If the pointer at index I is non-NULL, then it refers to the
479 /// IdentifierInfo for the identifier with ID=I+1 that has already
481 std::vector
<IdentifierInfo
*> IdentifiersLoaded
;
483 /// \brief A vector containing selectors that have already been loaded.
485 /// This vector is indexed by the Selector ID (-1). NULL selector
486 /// entries indicate that the particular selector ID has not yet
488 llvm::SmallVector
<Selector
, 16> SelectorsLoaded
;
490 /// \brief The macro definitions we have already loaded.
491 llvm::SmallVector
<MacroDefinition
*, 16> MacroDefinitionsLoaded
;
493 /// \brief Mapping from identifiers that represent macros whose definitions
494 /// have not yet been deserialized to the global offset where the macro
496 llvm::DenseMap
<IdentifierInfo
*, uint64_t> UnreadMacroRecordOffsets
;
498 /// \name CodeGen-relevant special data
499 /// \brief Fields containing data that is relevant to CodeGen.
502 /// \brief The IDs of all declarations that fulfill the criteria of
503 /// "interesting" decls.
505 /// This contains the data loaded from all EXTERNAL_DEFINITIONS blocks in the
506 /// chain. The referenced declarations are deserialized and passed to the
507 /// consumer eagerly.
508 llvm::SmallVector
<uint64_t, 16> ExternalDefinitions
;
510 /// \brief The IDs of all tentative definitions stored in the the chain.
512 /// Sema keeps track of all tentative definitions in a TU because it has to
513 /// complete them and pass them on to CodeGen. Thus, tentative definitions in
514 /// the PCH chain must be eagerly deserialized.
515 llvm::SmallVector
<uint64_t, 16> TentativeDefinitions
;
517 /// \brief The IDs of all CXXRecordDecls stored in the chain whose VTables are
520 /// CodeGen has to emit VTables for these records, so they have to be eagerly
522 llvm::SmallVector
<uint64_t, 64> VTableUses
;
526 /// \name Diagnostic-relevant special data
527 /// \brief Fields containing data that is used for generating diagnostics
530 /// \brief A snapshot of Sema's unused file-scoped variable tracking, for
531 /// generating warnings.
532 llvm::SmallVector
<uint64_t, 16> UnusedFileScopedDecls
;
534 /// \brief A snapshot of Sema's weak undeclared identifier tracking, for
535 /// generating warnings.
536 llvm::SmallVector
<uint64_t, 64> WeakUndeclaredIdentifiers
;
538 /// \brief The IDs of type aliases for ext_vectors that exist in the chain.
540 /// Used by Sema for finding sugared names for ext_vectors in diagnostics.
541 llvm::SmallVector
<uint64_t, 4> ExtVectorDecls
;
545 /// \name Sema-relevant special data
546 /// \brief Fields containing data that is used for semantic analysis
549 /// \brief The IDs of all locally scoped external decls in the chain.
551 /// Sema tracks these to validate that the types are consistent across all
552 /// local external declarations.
553 llvm::SmallVector
<uint64_t, 16> LocallyScopedExternalDecls
;
555 /// \brief The IDs of all dynamic class declarations in the chain.
557 /// Sema tracks these because it checks for the key functions being defined
558 /// at the end of the TU, in which case it directs CodeGen to emit the VTable.
559 llvm::SmallVector
<uint64_t, 16> DynamicClasses
;
561 /// \brief The IDs of the declarations Sema stores directly.
563 /// Sema tracks a few important decls, such as namespace std, directly.
564 llvm::SmallVector
<uint64_t, 4> SemaDeclRefs
;
566 /// \brief The IDs of the types ASTContext stores directly.
568 /// The AST context tracks a few important types, such as va_list, directly.
569 llvm::SmallVector
<uint64_t, 16> SpecialTypes
;
573 /// \brief The original file name that was used to build the primary AST file,
574 /// which may have been modified for relocatable-pch support.
575 std::string OriginalFileName
;
577 /// \brief The actual original file name that was used to build the primary
579 std::string ActualOriginalFileName
;
581 /// \brief Whether this precompiled header is a relocatable PCH file.
584 /// \brief The system include root to be used when loading the
585 /// precompiled header.
586 const char *isysroot
;
588 /// \brief Whether to disable the normal validation performed on precompiled
589 /// headers when they are loaded.
590 bool DisableValidation
;
592 /// \brief Mapping from switch-case IDs in the chain to switch-case statements
594 /// Statements usually don't have IDs, but switch cases need them, so that the
595 /// switch statement can refer to them.
596 std::map
<unsigned, SwitchCase
*> SwitchCaseStmts
;
598 /// \brief Mapping from label statement IDs in the chain to label statements.
600 /// Statements usually don't have IDs, but labeled statements need them, so
601 /// that goto statements and address-of-label expressions can refer to them.
602 std::map
<unsigned, LabelStmt
*> LabelStmts
;
604 /// \brief Mapping from label IDs to the set of "goto" statements
605 /// that point to that label before the label itself has been
607 std::multimap
<unsigned, GotoStmt
*> UnresolvedGotoStmts
;
609 /// \brief Mapping from label IDs to the set of address label
610 /// expressions that point to that label before the label itself has
611 /// been de-serialized.
612 std::multimap
<unsigned, AddrLabelExpr
*> UnresolvedAddrLabelExprs
;
614 /// \brief The number of stat() calls that hit/missed the stat
616 unsigned NumStatHits
, NumStatMisses
;
618 /// \brief The number of source location entries de-serialized from
620 unsigned NumSLocEntriesRead
;
622 /// \brief The number of source location entries in the chain.
623 unsigned TotalNumSLocEntries
;
625 /// \brief The next offset for a SLocEntry after everything in this reader.
626 unsigned NextSLocOffset
;
628 /// \brief The number of statements (and expressions) de-serialized
630 unsigned NumStatementsRead
;
632 /// \brief The total number of statements (and expressions) stored
634 unsigned TotalNumStatements
;
636 /// \brief The number of macros de-serialized from the chain.
637 unsigned NumMacrosRead
;
639 /// \brief The total number of macros stored in the chain.
640 unsigned TotalNumMacros
;
642 /// \brief The number of selectors that have been read.
643 unsigned NumSelectorsRead
;
645 /// \brief The number of method pool entries that have been read.
646 unsigned NumMethodPoolEntriesRead
;
648 /// \brief The number of times we have looked up a selector in the method
649 /// pool and not found anything interesting.
650 unsigned NumMethodPoolMisses
;
652 /// \brief The total number of method pool entries in the selector table.
653 unsigned TotalNumMethodPoolEntries
;
655 /// Number of lexical decl contexts read/total.
656 unsigned NumLexicalDeclContextsRead
, TotalLexicalDeclContexts
;
658 /// Number of visible decl contexts read/total.
659 unsigned NumVisibleDeclContextsRead
, TotalVisibleDeclContexts
;
661 /// \brief Number of Decl/types that are currently deserializing.
662 unsigned NumCurrentElementsDeserializing
;
664 /// \brief An IdentifierInfo that has been loaded but whose top-level
665 /// declarations of the same name have not (yet) been loaded.
666 struct PendingIdentifierInfo
{
668 llvm::SmallVector
<uint32_t, 4> DeclIDs
;
671 /// \brief The set of identifiers that were read while the AST reader was
672 /// (recursively) loading declarations.
674 /// The declarations on the identifier chain for these identifiers will be
675 /// loaded once the recursive loading has completed.
676 std::deque
<PendingIdentifierInfo
> PendingIdentifierInfos
;
678 /// \brief Contains declarations and definitions that will be
679 /// "interesting" to the ASTConsumer, when we get that AST consumer.
681 /// "Interesting" declarations are those that have data that may
682 /// need to be emitted, such as inline function definitions or
683 /// Objective-C protocols.
684 std::deque
<Decl
*> InterestingDecls
;
686 /// \brief When reading a Stmt tree, Stmt operands are placed in this stack.
687 llvm::SmallVector
<Stmt
*, 16> StmtStack
;
689 /// \brief What kind of records we are reading.
691 Read_Decl
, Read_Type
, Read_Stmt
694 /// \brief What kind of records we are reading.
695 ReadingKind ReadingKind
;
697 /// \brief RAII object to change the reading kind.
698 class ReadingKindTracker
{
700 enum ReadingKind PrevKind
;
702 ReadingKindTracker(const ReadingKindTracker
&); // do not implement
703 ReadingKindTracker
&operator=(const ReadingKindTracker
&);// do not implement
706 ReadingKindTracker(enum ReadingKind newKind
, ASTReader
&reader
)
707 : Reader(reader
), PrevKind(Reader
.ReadingKind
) {
708 Reader
.ReadingKind
= newKind
;
711 ~ReadingKindTracker() { Reader
.ReadingKind
= PrevKind
; }
714 /// \brief All predefines buffers in the chain, to be treated as if
716 PCHPredefinesBlocks PCHPredefinesBuffers
;
718 /// \brief Suggested contents of the predefines buffer, after this
719 /// PCH file has been processed.
721 /// In most cases, this string will be empty, because the predefines
722 /// buffer computed to build the PCH file will be identical to the
723 /// predefines buffer computed from the command line. However, when
724 /// there are differences that the PCH reader can work around, this
725 /// predefines buffer may contain additional definitions.
726 std::string SuggestedPredefines
;
728 /// \brief Reads a statement from the specified cursor.
729 Stmt
*ReadStmtFromStream(PerFileData
&F
);
731 void MaybeAddSystemRootToFilename(std::string
&Filename
);
733 ASTReadResult
ReadASTCore(llvm::StringRef FileName
, ASTFileType Type
);
734 ASTReadResult
ReadASTBlock(PerFileData
&F
);
735 bool CheckPredefinesBuffers();
736 bool ParseLineTable(PerFileData
&F
, llvm::SmallVectorImpl
<uint64_t> &Record
);
737 ASTReadResult
ReadSourceManagerBlock(PerFileData
&F
);
738 ASTReadResult
ReadSLocEntryRecord(unsigned ID
);
739 PerFileData
*SLocCursorForID(unsigned ID
);
740 SourceLocation
getImportLocation(PerFileData
*F
);
741 bool ParseLanguageOptions(const llvm::SmallVectorImpl
<uint64_t> &Record
);
743 struct RecordLocation
{
744 RecordLocation(PerFileData
*M
, uint64_t O
)
750 QualType
ReadTypeRecord(unsigned Index
);
751 RecordLocation
TypeCursorForIndex(unsigned Index
);
752 void LoadedDecl(unsigned Index
, Decl
*D
);
753 Decl
*ReadDeclRecord(unsigned Index
, serialization::DeclID ID
);
754 RecordLocation
DeclCursorForIndex(unsigned Index
, serialization::DeclID ID
);
756 void PassInterestingDeclsToConsumer();
758 /// \brief Produce an error diagnostic and return true.
760 /// This routine should only be used for fatal errors that have to
761 /// do with non-routine failures (e.g., corrupted AST file).
762 void Error(const char *Msg
);
764 ASTReader(const ASTReader
&); // do not implement
765 ASTReader
&operator=(const ASTReader
&); // do not implement
767 typedef llvm::SmallVector
<uint64_t, 64> RecordData
;
769 /// \brief Load the AST file and validate its contents against the given
772 /// \param PP the preprocessor associated with the context in which this
773 /// precompiled header will be loaded.
775 /// \param Context the AST context that this precompiled header will be
778 /// \param isysroot If non-NULL, the system include path specified by the
779 /// user. This is only used with relocatable PCH files. If non-NULL,
780 /// a relocatable PCH file will use the default path "/".
782 /// \param DisableValidation If true, the AST reader will suppress most
783 /// of its regular consistency checking, allowing the use of precompiled
784 /// headers that cannot be determined to be compatible.
785 ASTReader(Preprocessor
&PP
, ASTContext
*Context
, const char *isysroot
= 0,
786 bool DisableValidation
= false);
788 /// \brief Load the AST file without using any pre-initialized Preprocessor.
790 /// The necessary information to initialize a Preprocessor later can be
791 /// obtained by setting a ASTReaderListener.
793 /// \param SourceMgr the source manager into which the AST file will be loaded
795 /// \param FileMgr the file manager into which the AST file will be loaded.
797 /// \param Diags the diagnostics system to use for reporting errors and
798 /// warnings relevant to loading the AST file.
800 /// \param isysroot If non-NULL, the system include path specified by the
801 /// user. This is only used with relocatable PCH files. If non-NULL,
802 /// a relocatable PCH file will use the default path "/".
804 /// \param DisableValidation If true, the AST reader will suppress most
805 /// of its regular consistency checking, allowing the use of precompiled
806 /// headers that cannot be determined to be compatible.
807 ASTReader(SourceManager
&SourceMgr
, FileManager
&FileMgr
,
808 const FileSystemOptions
&FileSystemOpts
,
809 Diagnostic
&Diags
, const char *isysroot
= 0,
810 bool DisableValidation
= false);
813 /// \brief Load the precompiled header designated by the given file
815 ASTReadResult
ReadAST(const std::string
&FileName
, ASTFileType Type
);
817 /// \brief Set the AST callbacks listener.
818 void setListener(ASTReaderListener
*listener
) {
819 Listener
.reset(listener
);
822 /// \brief Set the AST deserialization listener.
823 void setDeserializationListener(ASTDeserializationListener
*Listener
);
825 /// \brief Set the Preprocessor to use.
826 void setPreprocessor(Preprocessor
&pp
);
828 /// \brief Sets and initializes the given Context.
829 void InitializeContext(ASTContext
&Context
);
831 /// \brief Retrieve the name of the named (primary) AST file
832 const std::string
&getFileName() const { return Chain
[0]->FileName
; }
834 /// \brief Retrieve the name of the original source file name
835 const std::string
&getOriginalSourceFile() { return OriginalFileName
; }
837 /// \brief Retrieve the name of the original source file name directly from
838 /// the AST file, without actually loading the AST file.
839 static std::string
getOriginalSourceFile(const std::string
&ASTFileName
,
840 FileManager
&FileMgr
,
841 const FileSystemOptions
&FSOpts
,
844 /// \brief Returns the suggested contents of the predefines buffer,
845 /// which contains a (typically-empty) subset of the predefines
846 /// build prior to including the precompiled header.
847 const std::string
&getSuggestedPredefines() { return SuggestedPredefines
; }
849 /// \brief Read preprocessed entities into the
850 virtual void ReadPreprocessedEntities();
852 /// \brief Returns the number of source locations found in the chain.
853 unsigned getTotalNumSLocs() const {
854 return TotalNumSLocEntries
;
857 /// \brief Returns the next SLocEntry offset after the chain.
858 unsigned getNextSLocOffset() const {
859 return NextSLocOffset
;
862 /// \brief Returns the number of identifiers found in the chain.
863 unsigned getTotalNumIdentifiers() const {
864 return static_cast<unsigned>(IdentifiersLoaded
.size());
867 /// \brief Returns the number of types found in the chain.
868 unsigned getTotalNumTypes() const {
869 return static_cast<unsigned>(TypesLoaded
.size());
872 /// \brief Returns the number of declarations found in the chain.
873 unsigned getTotalNumDecls() const {
874 return static_cast<unsigned>(DeclsLoaded
.size());
877 /// \brief Returns the number of selectors found in the chain.
878 unsigned getTotalNumSelectors() const {
879 return static_cast<unsigned>(SelectorsLoaded
.size());
882 /// \brief Returns the number of macro definitions found in the chain.
883 unsigned getTotalNumMacroDefinitions() const {
884 return static_cast<unsigned>(MacroDefinitionsLoaded
.size());
887 /// \brief Returns the number of C++ base specifiers found in the chain.
888 unsigned getTotalNumCXXBaseSpecifiers() const;
890 /// \brief Reads a TemplateArgumentLocInfo appropriate for the
891 /// given TemplateArgument kind.
892 TemplateArgumentLocInfo
893 GetTemplateArgumentLocInfo(PerFileData
&F
, TemplateArgument::ArgKind Kind
,
894 const RecordData
&Record
, unsigned &Idx
);
896 /// \brief Reads a TemplateArgumentLoc.
898 ReadTemplateArgumentLoc(PerFileData
&F
,
899 const RecordData
&Record
, unsigned &Idx
);
901 /// \brief Reads a declarator info from the given record.
902 TypeSourceInfo
*GetTypeSourceInfo(PerFileData
&F
,
903 const RecordData
&Record
, unsigned &Idx
);
905 /// \brief Resolve and return the translation unit declaration.
906 TranslationUnitDecl
*GetTranslationUnitDecl();
908 /// \brief Resolve a type ID into a type, potentially building a new
910 QualType
GetType(serialization::TypeID ID
);
912 /// \brief Returns the type ID associated with the given type.
913 /// If the type didn't come from the AST file the ID that is returned is
914 /// marked as "doesn't exist in AST".
915 serialization::TypeID
GetTypeID(QualType T
) const;
917 /// \brief Returns the type index associated with the given type.
918 /// If the type didn't come from the AST file the index that is returned is
919 /// marked as "doesn't exist in AST".
920 serialization::TypeIdx
GetTypeIdx(QualType T
) const;
922 /// \brief Resolve a declaration ID into a declaration, potentially
923 /// building a new declaration.
924 Decl
*GetDecl(serialization::DeclID ID
);
925 virtual Decl
*GetExternalDecl(uint32_t ID
);
927 /// \brief Resolve a CXXBaseSpecifiers ID into an offset into the chain
928 /// of loaded AST files.
929 uint64_t GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID
);
931 virtual CXXBaseSpecifier
*GetExternalCXXBaseSpecifiers(uint64_t Offset
);
933 /// \brief Resolve the offset of a statement into a statement.
935 /// This operation will read a new statement from the external
936 /// source each time it is called, and is meant to be used via a
937 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
938 virtual Stmt
*GetExternalDeclStmt(uint64_t Offset
);
940 /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
941 /// specified cursor. Read the abbreviations that are at the top of the block
942 /// and then leave the cursor pointing into the block.
943 bool ReadBlockAbbrevs(llvm::BitstreamCursor
&Cursor
, unsigned BlockID
);
945 /// \brief Finds all the visible declarations with a given name.
946 /// The current implementation of this method just loads the entire
947 /// lookup table as unmaterialized references.
948 virtual DeclContext::lookup_result
949 FindExternalVisibleDeclsByName(const DeclContext
*DC
,
950 DeclarationName Name
);
952 virtual void MaterializeVisibleDecls(const DeclContext
*DC
);
954 /// \brief Read all of the declarations lexically stored in a
955 /// declaration context.
957 /// \param DC The declaration context whose declarations will be
960 /// \param Decls Vector that will contain the declarations loaded
961 /// from the external source. The caller is responsible for merging
962 /// these declarations with any declarations already stored in the
963 /// declaration context.
965 /// \returns true if there was an error while reading the
966 /// declarations for this declaration context.
967 virtual bool FindExternalLexicalDecls(const DeclContext
*DC
,
968 bool (*isKindWeWant
)(Decl::Kind
),
969 llvm::SmallVectorImpl
<Decl
*> &Decls
);
971 /// \brief Notify ASTReader that we started deserialization of
972 /// a decl or type so until FinishedDeserializing is called there may be
973 /// decls that are initializing. Must be paired with FinishedDeserializing.
974 virtual void StartedDeserializing() { ++NumCurrentElementsDeserializing
; }
976 /// \brief Notify ASTReader that we finished the deserialization of
977 /// a decl or type. Must be paired with StartedDeserializing.
978 virtual void FinishedDeserializing();
980 /// \brief Function that will be invoked when we begin parsing a new
981 /// translation unit involving this external AST source.
983 /// This function will provide all of the external definitions to
985 virtual void StartTranslationUnit(ASTConsumer
*Consumer
);
987 /// \brief Print some statistics about AST usage.
988 virtual void PrintStats();
990 /// \brief Initialize the semantic source with the Sema instance
991 /// being used to perform semantic analysis on the abstract syntax
993 virtual void InitializeSema(Sema
&S
);
995 /// \brief Inform the semantic consumer that Sema is no longer available.
996 virtual void ForgetSema() { SemaObj
= 0; }
998 /// \brief Retrieve the IdentifierInfo for the named identifier.
1000 /// This routine builds a new IdentifierInfo for the given identifier. If any
1001 /// declarations with this name are visible from translation unit scope, their
1002 /// declarations will be deserialized and introduced into the declaration
1003 /// chain of the identifier.
1004 virtual IdentifierInfo
*get(const char *NameStart
, const char *NameEnd
);
1005 IdentifierInfo
*get(llvm::StringRef Name
) {
1006 return get(Name
.begin(), Name
.end());
1009 /// \brief Retrieve an iterator into the set of all identifiers
1010 /// in all loaded AST files.
1011 virtual IdentifierIterator
*getIdentifiers() const;
1013 /// \brief Load the contents of the global method pool for a given
1016 /// \returns a pair of Objective-C methods lists containing the
1017 /// instance and factory methods, respectively, with this selector.
1018 virtual std::pair
<ObjCMethodList
, ObjCMethodList
>
1019 ReadMethodPool(Selector Sel
);
1021 /// \brief Load a selector from disk, registering its ID if it exists.
1022 void LoadSelector(Selector Sel
);
1024 void SetIdentifierInfo(unsigned ID
, IdentifierInfo
*II
);
1025 void SetGloballyVisibleDecls(IdentifierInfo
*II
,
1026 const llvm::SmallVectorImpl
<uint32_t> &DeclIDs
,
1027 bool Nonrecursive
= false);
1029 /// \brief Report a diagnostic.
1030 DiagnosticBuilder
Diag(unsigned DiagID
);
1032 /// \brief Report a diagnostic.
1033 DiagnosticBuilder
Diag(SourceLocation Loc
, unsigned DiagID
);
1035 IdentifierInfo
*DecodeIdentifierInfo(unsigned Idx
);
1037 IdentifierInfo
*GetIdentifierInfo(const RecordData
&Record
, unsigned &Idx
) {
1038 return DecodeIdentifierInfo(Record
[Idx
++]);
1041 virtual IdentifierInfo
*GetIdentifier(unsigned ID
) {
1042 return DecodeIdentifierInfo(ID
);
1045 /// \brief Read the source location entry with index ID.
1046 virtual void ReadSLocEntry(unsigned ID
);
1048 Selector
DecodeSelector(unsigned Idx
);
1050 virtual Selector
GetExternalSelector(uint32_t ID
);
1051 uint32_t GetNumExternalSelectors();
1053 Selector
GetSelector(const RecordData
&Record
, unsigned &Idx
) {
1054 return DecodeSelector(Record
[Idx
++]);
1057 /// \brief Read a declaration name.
1058 DeclarationName
ReadDeclarationName(const RecordData
&Record
, unsigned &Idx
);
1059 void ReadDeclarationNameLoc(PerFileData
&F
,
1060 DeclarationNameLoc
&DNLoc
, DeclarationName Name
,
1061 const RecordData
&Record
, unsigned &Idx
);
1062 void ReadDeclarationNameInfo(PerFileData
&F
, DeclarationNameInfo
&NameInfo
,
1063 const RecordData
&Record
, unsigned &Idx
);
1065 void ReadQualifierInfo(PerFileData
&F
, QualifierInfo
&Info
,
1066 const RecordData
&Record
, unsigned &Idx
);
1068 NestedNameSpecifier
*ReadNestedNameSpecifier(const RecordData
&Record
,
1071 /// \brief Read a template name.
1072 TemplateName
ReadTemplateName(const RecordData
&Record
, unsigned &Idx
);
1074 /// \brief Read a template argument.
1075 TemplateArgument
ReadTemplateArgument(PerFileData
&F
,
1076 const RecordData
&Record
,unsigned &Idx
);
1078 /// \brief Read a template parameter list.
1079 TemplateParameterList
*ReadTemplateParameterList(PerFileData
&F
,
1080 const RecordData
&Record
,
1083 /// \brief Read a template argument array.
1085 ReadTemplateArgumentList(llvm::SmallVector
<TemplateArgument
, 8> &TemplArgs
,
1086 PerFileData
&F
, const RecordData
&Record
,
1089 /// \brief Read a UnresolvedSet structure.
1090 void ReadUnresolvedSet(UnresolvedSetImpl
&Set
,
1091 const RecordData
&Record
, unsigned &Idx
);
1093 /// \brief Read a C++ base specifier.
1094 CXXBaseSpecifier
ReadCXXBaseSpecifier(PerFileData
&F
,
1095 const RecordData
&Record
,unsigned &Idx
);
1097 /// \brief Read a CXXBaseOrMemberInitializer array.
1098 std::pair
<CXXBaseOrMemberInitializer
**, unsigned>
1099 ReadCXXBaseOrMemberInitializers(PerFileData
&F
,
1100 const RecordData
&Record
, unsigned &Idx
);
1102 /// \brief Read a source location from raw form.
1103 SourceLocation
ReadSourceLocation(PerFileData
&Module
, unsigned Raw
) {
1104 (void)Module
; // No remapping yet
1105 return SourceLocation::getFromRawEncoding(Raw
);
1108 /// \brief Read a source location.
1109 SourceLocation
ReadSourceLocation(PerFileData
&Module
,
1110 const RecordData
&Record
, unsigned& Idx
) {
1111 return ReadSourceLocation(Module
, Record
[Idx
++]);
1114 /// \brief Read a source range.
1115 SourceRange
ReadSourceRange(PerFileData
&F
,
1116 const RecordData
&Record
, unsigned& Idx
);
1118 /// \brief Read an integral value
1119 llvm::APInt
ReadAPInt(const RecordData
&Record
, unsigned &Idx
);
1121 /// \brief Read a signed integral value
1122 llvm::APSInt
ReadAPSInt(const RecordData
&Record
, unsigned &Idx
);
1124 /// \brief Read a floating-point value
1125 llvm::APFloat
ReadAPFloat(const RecordData
&Record
, unsigned &Idx
);
1127 // \brief Read a string
1128 std::string
ReadString(const RecordData
&Record
, unsigned &Idx
);
1130 CXXTemporary
*ReadCXXTemporary(const RecordData
&Record
, unsigned &Idx
);
1132 /// \brief Reads attributes from the current stream position.
1133 void ReadAttributes(PerFileData
&F
, AttrVec
&Attrs
,
1134 const RecordData
&Record
, unsigned &Idx
);
1136 /// \brief Reads a statement.
1137 Stmt
*ReadStmt(PerFileData
&F
);
1139 /// \brief Reads an expression.
1140 Expr
*ReadExpr(PerFileData
&F
);
1142 /// \brief Reads a sub-statement operand during statement reading.
1143 Stmt
*ReadSubStmt() {
1144 assert(ReadingKind
== Read_Stmt
&&
1145 "Should be called only during statement reading!");
1146 // Subexpressions are stored from last to first, so the next Stmt we need
1147 // is at the back of the stack.
1148 assert(!StmtStack
.empty() && "Read too many sub statements!");
1149 return StmtStack
.pop_back_val();
1152 /// \brief Reads a sub-expression operand during statement reading.
1153 Expr
*ReadSubExpr();
1155 /// \brief Reads the macro record located at the given offset.
1156 void ReadMacroRecord(PerFileData
&F
, uint64_t Offset
);
1158 /// \brief Note that the identifier is a macro whose record will be loaded
1159 /// from the given AST file at the given (file-local) offset.
1160 void SetIdentifierIsMacro(IdentifierInfo
*II
, PerFileData
&F
,
1163 /// \brief Read the set of macros defined by this external macro source.
1164 virtual void ReadDefinedMacros();
1166 /// \brief Read the macro definition for this identifier.
1167 virtual void LoadMacroDefinition(IdentifierInfo
*II
);
1169 /// \brief Read the macro definition corresponding to this iterator
1170 /// into the unread macro record offsets table.
1171 void LoadMacroDefinition(
1172 llvm::DenseMap
<IdentifierInfo
*, uint64_t>::iterator Pos
);
1174 /// \brief Retrieve the macro definition with the given ID.
1175 MacroDefinition
*getMacroDefinition(serialization::MacroID ID
);
1177 /// \brief Retrieve the AST context that this AST reader supplements.
1178 ASTContext
*getContext() { return Context
; }
1180 // \brief Contains declarations that were loaded before we have
1181 // access to a Sema object.
1182 llvm::SmallVector
<NamedDecl
*, 16> PreloadedDecls
;
1184 /// \brief Retrieve the semantic analysis object used to analyze the
1185 /// translation unit in which the precompiled header is being
1187 Sema
*getSema() { return SemaObj
; }
1189 /// \brief Retrieve the identifier table associated with the
1191 IdentifierTable
&getIdentifierTable();
1193 /// \brief Record that the given ID maps to the given switch-case
1195 void RecordSwitchCaseID(SwitchCase
*SC
, unsigned ID
);
1197 /// \brief Retrieve the switch-case statement with the given ID.
1198 SwitchCase
*getSwitchCaseWithID(unsigned ID
);
1200 /// \brief Record that the given label statement has been
1201 /// deserialized and has the given ID.
1202 void RecordLabelStmt(LabelStmt
*S
, unsigned ID
);
1204 void ClearSwitchCaseIDs();
1206 /// \brief Set the label of the given statement to the label
1207 /// identified by ID.
1209 /// Depending on the order in which the label and other statements
1210 /// referencing that label occur, this operation may complete
1211 /// immediately (updating the statement) or it may queue the
1212 /// statement to be back-patched later.
1213 void SetLabelOf(GotoStmt
*S
, unsigned ID
);
1215 /// \brief Set the label of the given expression to the label
1216 /// identified by ID.
1218 /// Depending on the order in which the label and other statements
1219 /// referencing that label occur, this operation may complete
1220 /// immediately (updating the statement) or it may queue the
1221 /// statement to be back-patched later.
1222 void SetLabelOf(AddrLabelExpr
*S
, unsigned ID
);
1225 /// \brief Helper class that saves the current stream position and
1226 /// then restores it when destroyed.
1227 struct SavedStreamPosition
{
1228 explicit SavedStreamPosition(llvm::BitstreamCursor
&Cursor
)
1229 : Cursor(Cursor
), Offset(Cursor
.GetCurrentBitNo()) { }
1231 ~SavedStreamPosition() {
1232 Cursor
.JumpToBit(Offset
);
1236 llvm::BitstreamCursor
&Cursor
;
1240 inline void PCHValidator::Error(const char *Msg
) {
1244 } // end namespace clang