Introduce a new kind of TemplateName that captures a substituted
[clang.git] / include / clang / Serialization / ASTReader.h
blob38b0c57d221c768dbe4c7094d5c8d2a01260bf4a
1 //===--- ASTReader.h - AST File Reader --------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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/Support/DataTypes.h"
35 #include <deque>
36 #include <map>
37 #include <string>
38 #include <utility>
39 #include <vector>
41 namespace llvm {
42 class MemoryBuffer;
45 namespace clang {
47 class AddrLabelExpr;
48 class ASTConsumer;
49 class ASTContext;
50 class ASTIdentifierIterator;
51 class Attr;
52 class Decl;
53 class DeclContext;
54 class NestedNameSpecifier;
55 class CXXBaseSpecifier;
56 class CXXCtorInitializer;
57 class GotoStmt;
58 class LabelStmt;
59 class MacroDefinition;
60 class NamedDecl;
61 class Preprocessor;
62 class Sema;
63 class SwitchCase;
64 class ASTDeserializationListener;
65 class ASTReader;
66 class ASTDeclReader;
67 class ASTStmtReader;
68 class ASTIdentifierLookupTrait;
69 class TypeLocReader;
70 struct HeaderFileInfo;
72 struct PCHPredefinesBlock {
73 /// \brief The file ID for this predefines buffer in a PCH file.
74 FileID BufferID;
76 /// \brief This predefines buffer in a PCH file.
77 llvm::StringRef Data;
79 typedef llvm::SmallVector<PCHPredefinesBlock, 2> PCHPredefinesBlocks;
81 /// \brief Abstract interface for callback invocations by the ASTReader.
82 ///
83 /// While reading an AST file, the ASTReader will call the methods of the
84 /// listener to pass on specific information. Some of the listener methods can
85 /// return true to indicate to the ASTReader that the information (and
86 /// consequently the AST file) is invalid.
87 class ASTReaderListener {
88 public:
89 virtual ~ASTReaderListener();
91 /// \brief Receives the language options.
92 ///
93 /// \returns true to indicate the options are invalid or false otherwise.
94 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
95 return false;
98 /// \brief Receives the target triple.
99 ///
100 /// \returns true to indicate the target triple is invalid or false otherwise.
101 virtual bool ReadTargetTriple(llvm::StringRef Triple) {
102 return false;
105 /// \brief Receives the contents of the predefines buffer.
107 /// \param Buffers Information about the predefines buffers.
109 /// \param OriginalFileName The original file name for the AST file, which
110 /// will appear as an entry in the predefines buffer.
112 /// \param SuggestedPredefines If necessary, additional definitions are added
113 /// here.
115 /// \returns true to indicate the predefines are invalid or false otherwise.
116 virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
117 llvm::StringRef OriginalFileName,
118 std::string &SuggestedPredefines) {
119 return false;
122 /// \brief Receives a HeaderFileInfo entry.
123 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) {}
125 /// \brief Receives __COUNTER__ value.
126 virtual void ReadCounter(unsigned Value) {}
129 /// \brief ASTReaderListener implementation to validate the information of
130 /// the PCH file against an initialized Preprocessor.
131 class PCHValidator : public ASTReaderListener {
132 Preprocessor &PP;
133 ASTReader &Reader;
135 unsigned NumHeaderInfos;
137 public:
138 PCHValidator(Preprocessor &PP, ASTReader &Reader)
139 : PP(PP), Reader(Reader), NumHeaderInfos(0) {}
141 virtual bool ReadLanguageOptions(const LangOptions &LangOpts);
142 virtual bool ReadTargetTriple(llvm::StringRef Triple);
143 virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
144 llvm::StringRef OriginalFileName,
145 std::string &SuggestedPredefines);
146 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID);
147 virtual void ReadCounter(unsigned Value);
149 private:
150 void Error(const char *Msg);
153 /// \brief Reads an AST files chain containing the contents of a translation
154 /// unit.
156 /// The ASTReader class reads bitstreams (produced by the ASTWriter
157 /// class) containing the serialized representation of a given
158 /// abstract syntax tree and its supporting data structures. An
159 /// instance of the ASTReader can be attached to an ASTContext object,
160 /// which will provide access to the contents of the AST files.
162 /// The AST reader provides lazy de-serialization of declarations, as
163 /// required when traversing the AST. Only those AST nodes that are
164 /// actually required will be de-serialized.
165 class ASTReader
166 : public ExternalPreprocessorSource,
167 public ExternalPreprocessingRecordSource,
168 public ExternalSemaSource,
169 public IdentifierInfoLookup,
170 public ExternalIdentifierLookup,
171 public ExternalSLocEntrySource {
172 public:
173 enum ASTReadResult { Success, Failure, IgnorePCH };
174 /// \brief Types of AST files.
175 enum ASTFileType {
176 Module, ///< File is a module proper.
177 PCH, ///< File is a PCH file treated as such.
178 Preamble, ///< File is a PCH file treated as the preamble.
179 MainFile ///< File is a PCH file treated as the actual main file.
181 friend class PCHValidator;
182 friend class ASTDeclReader;
183 friend class ASTStmtReader;
184 friend class ASTIdentifierIterator;
185 friend class ASTIdentifierLookupTrait;
186 friend class TypeLocReader;
187 private:
188 /// \brief The receiver of some callbacks invoked by ASTReader.
189 llvm::OwningPtr<ASTReaderListener> Listener;
191 /// \brief The receiver of deserialization events.
192 ASTDeserializationListener *DeserializationListener;
194 SourceManager &SourceMgr;
195 FileManager &FileMgr;
196 Diagnostic &Diags;
198 /// \brief The semantic analysis object that will be processing the
199 /// AST files and the translation unit that uses it.
200 Sema *SemaObj;
202 /// \brief The preprocessor that will be loading the source file.
203 Preprocessor *PP;
205 /// \brief The AST context into which we'll read the AST files.
206 ASTContext *Context;
208 /// \brief The AST consumer.
209 ASTConsumer *Consumer;
211 /// \brief Information that is needed for every module.
212 struct PerFileData {
213 PerFileData(ASTFileType Ty);
214 ~PerFileData();
216 // === General information ===
218 /// \brief The type of this AST file.
219 ASTFileType Type;
221 /// \brief The file name of the AST file.
222 std::string FileName;
224 /// \brief The memory buffer that stores the data associated with
225 /// this AST file.
226 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
228 /// \brief The size of this file, in bits.
229 uint64_t SizeInBits;
231 /// \brief The bitstream reader from which we'll read the AST file.
232 llvm::BitstreamReader StreamFile;
234 /// \brief The main bitstream cursor for the main block.
235 llvm::BitstreamCursor Stream;
237 // === Source Locations ===
239 /// \brief Cursor used to read source location entries.
240 llvm::BitstreamCursor SLocEntryCursor;
242 /// \brief The number of source location entries in this AST file.
243 unsigned LocalNumSLocEntries;
245 /// \brief Offsets for all of the source location entries in the
246 /// AST file.
247 const uint32_t *SLocOffsets;
249 /// \brief The entire size of this module's source location offset range.
250 unsigned LocalSLocSize;
252 // === Identifiers ===
254 /// \brief The number of identifiers in this AST file.
255 unsigned LocalNumIdentifiers;
257 /// \brief Offsets into the identifier table data.
259 /// This array is indexed by the identifier ID (-1), and provides
260 /// the offset into IdentifierTableData where the string data is
261 /// stored.
262 const uint32_t *IdentifierOffsets;
264 /// \brief Actual data for the on-disk hash table.
266 /// This pointer points into a memory buffer, where the on-disk hash
267 /// table for identifiers actually lives.
268 const char *IdentifierTableData;
270 /// \brief A pointer to an on-disk hash table of opaque type
271 /// IdentifierHashTable.
272 void *IdentifierLookupTable;
274 // === Macros ===
276 /// \brief The cursor to the start of the preprocessor block, which stores
277 /// all of the macro definitions.
278 llvm::BitstreamCursor MacroCursor;
280 /// \brief The offset of the start of the set of defined macros.
281 uint64_t MacroStartOffset;
283 /// \brief The number of macro definitions in this file.
284 unsigned LocalNumMacroDefinitions;
286 /// \brief Offsets of all of the macro definitions in the preprocessing
287 /// record in the AST file.
288 const uint32_t *MacroDefinitionOffsets;
290 // === Selectors ===
292 /// \brief The number of selectors new to this file.
294 /// This is the number of entries in SelectorOffsets.
295 unsigned LocalNumSelectors;
297 /// \brief Offsets into the selector lookup table's data array
298 /// where each selector resides.
299 const uint32_t *SelectorOffsets;
301 /// \brief A pointer to the character data that comprises the selector table
303 /// The SelectorOffsets table refers into this memory.
304 const unsigned char *SelectorLookupTableData;
306 /// \brief A pointer to an on-disk hash table of opaque type
307 /// ASTSelectorLookupTable.
309 /// This hash table provides the IDs of all selectors, and the associated
310 /// instance and factory methods.
311 void *SelectorLookupTable;
313 /// \brief Method selectors used in a @selector expression. Used for
314 /// implementation of -Wselector.
315 llvm::SmallVector<uint64_t, 64> ReferencedSelectorsData;
317 // === Declarations ===
319 /// DeclsCursor - This is a cursor to the start of the DECLS_BLOCK block. It
320 /// has read all the abbreviations at the start of the block and is ready to
321 /// jump around with these in context.
322 llvm::BitstreamCursor DeclsCursor;
324 /// \brief The number of declarations in this AST file.
325 unsigned LocalNumDecls;
327 /// \brief Offset of each declaration within the bitstream, indexed
328 /// by the declaration ID (-1).
329 const uint32_t *DeclOffsets;
331 /// \brief A snapshot of the pending instantiations in the chain.
333 /// This record tracks the instantiations that Sema has to perform at the
334 /// end of the TU. It consists of a pair of values for every pending
335 /// instantiation where the first value is the ID of the decl and the second
336 /// is the instantiation location.
337 llvm::SmallVector<uint64_t, 64> PendingInstantiations;
339 /// \brief The number of C++ base specifier sets in this AST file.
340 unsigned LocalNumCXXBaseSpecifiers;
342 /// \brief Offset of each C++ base specifier set within the bitstream,
343 /// indexed by the C++ base specifier set ID (-1).
344 const uint32_t *CXXBaseSpecifiersOffsets;
346 // === Types ===
348 /// \brief The number of types in this AST file.
349 unsigned LocalNumTypes;
351 /// \brief Offset of each type within the bitstream, indexed by the
352 /// type ID, or the representation of a Type*.
353 const uint32_t *TypeOffsets;
355 // === Miscellaneous ===
357 /// \brief The AST stat cache installed for this file, if any.
359 /// The dynamic type of this stat cache is always ASTStatCache
360 void *StatCache;
362 /// \brief The number of preallocated preprocessing entities in the
363 /// preprocessing record.
364 unsigned NumPreallocatedPreprocessingEntities;
366 /// \brief The next module in source order.
367 PerFileData *NextInSource;
369 /// \brief All the modules that loaded this one. Can contain NULL for
370 /// directly loaded modules.
371 llvm::SmallVector<PerFileData *, 1> Loaders;
374 /// \brief All loaded modules, indexed by name.
375 llvm::StringMap<PerFileData*> Modules;
377 /// \brief The first module in source order.
378 PerFileData *FirstInSource;
380 /// \brief The chain of AST files. The first entry is the one named by the
381 /// user, the last one is the one that doesn't depend on anything further.
382 /// That is, the entry I was created with -include-pch I+1.
383 llvm::SmallVector<PerFileData*, 2> Chain;
385 /// \brief SLocEntries that we're going to preload.
386 llvm::SmallVector<uint64_t, 64> PreloadSLocEntries;
388 /// \brief Types that have already been loaded from the chain.
390 /// When the pointer at index I is non-NULL, the type with
391 /// ID = (I + 1) << FastQual::Width has already been loaded
392 std::vector<QualType> TypesLoaded;
394 /// \brief Map that provides the ID numbers of each type within the
395 /// output stream, plus those deserialized from a chained PCH.
397 /// The ID numbers of types are consecutive (in order of discovery)
398 /// and start at 1. 0 is reserved for NULL. When types are actually
399 /// stored in the stream, the ID number is shifted by 2 bits to
400 /// allow for the const/volatile qualifiers.
402 /// Keys in the map never have const/volatile qualifiers.
403 serialization::TypeIdxMap TypeIdxs;
405 /// \brief Declarations that have already been loaded from the chain.
407 /// When the pointer at index I is non-NULL, the declaration with ID
408 /// = I + 1 has already been loaded.
409 std::vector<Decl *> DeclsLoaded;
411 typedef std::pair<PerFileData *, uint64_t> FileOffset;
412 typedef llvm::SmallVector<FileOffset, 2> FileOffsetsTy;
413 typedef llvm::DenseMap<serialization::DeclID, FileOffsetsTy>
414 DeclUpdateOffsetsMap;
415 /// \brief Declarations that have modifications residing in a later file
416 /// in the chain.
417 DeclUpdateOffsetsMap DeclUpdateOffsets;
419 typedef llvm::DenseMap<serialization::DeclID,
420 std::pair<PerFileData *, uint64_t> >
421 DeclReplacementMap;
422 /// \brief Declarations that have been replaced in a later file in the chain.
423 DeclReplacementMap ReplacedDecls;
425 /// \brief Information about the contents of a DeclContext.
426 struct DeclContextInfo {
427 void *NameLookupTableData; // a ASTDeclContextNameLookupTable.
428 const serialization::KindDeclIDPair *LexicalDecls;
429 unsigned NumLexicalDecls;
431 // In a full chain, there could be multiple updates to every decl context,
432 // so this is a vector. However, typically a chain is only two elements long,
433 // with only one file containing updates, so there will be only one update
434 // per decl context.
435 typedef llvm::SmallVector<DeclContextInfo, 1> DeclContextInfos;
436 typedef llvm::DenseMap<const DeclContext *, DeclContextInfos>
437 DeclContextOffsetsMap;
438 // Updates for visible decls can occur for other contexts than just the
439 // TU, and when we read those update records, the actual context will not
440 // be available yet (unless it's the TU), so have this pending map using the
441 // ID as a key. It will be realized when the context is actually loaded.
442 typedef llvm::SmallVector<void *, 1> DeclContextVisibleUpdates;
443 typedef llvm::DenseMap<serialization::DeclID, DeclContextVisibleUpdates>
444 DeclContextVisibleUpdatesPending;
446 /// \brief Offsets of the lexical and visible declarations for each
447 /// DeclContext.
448 DeclContextOffsetsMap DeclContextOffsets;
450 /// \brief Updates to the visible declarations of declaration contexts that
451 /// haven't been loaded yet.
452 DeclContextVisibleUpdatesPending PendingVisibleUpdates;
454 typedef llvm::SmallVector<CXXRecordDecl *, 4> ForwardRefs;
455 typedef llvm::DenseMap<const CXXRecordDecl *, ForwardRefs>
456 PendingForwardRefsMap;
457 /// \brief Forward references that have a definition but the definition decl
458 /// is still initializing. When the definition gets read it will update
459 /// the DefinitionData pointer of all pending references.
460 PendingForwardRefsMap PendingForwardRefs;
462 typedef llvm::DenseMap<serialization::DeclID, serialization::DeclID>
463 FirstLatestDeclIDMap;
464 /// \brief Map of first declarations from a chained PCH that point to the
465 /// most recent declarations in another AST file.
466 FirstLatestDeclIDMap FirstLatestDeclIDs;
468 /// \brief Read the records that describe the contents of declcontexts.
469 bool ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
470 const std::pair<uint64_t, uint64_t> &Offsets,
471 DeclContextInfo &Info);
473 /// \brief A vector containing identifiers that have already been
474 /// loaded.
476 /// If the pointer at index I is non-NULL, then it refers to the
477 /// IdentifierInfo for the identifier with ID=I+1 that has already
478 /// been loaded.
479 std::vector<IdentifierInfo *> IdentifiersLoaded;
481 /// \brief A vector containing selectors that have already been loaded.
483 /// This vector is indexed by the Selector ID (-1). NULL selector
484 /// entries indicate that the particular selector ID has not yet
485 /// been loaded.
486 llvm::SmallVector<Selector, 16> SelectorsLoaded;
488 /// \brief The macro definitions we have already loaded.
489 llvm::SmallVector<MacroDefinition *, 16> MacroDefinitionsLoaded;
491 /// \brief Mapping from identifiers that represent macros whose definitions
492 /// have not yet been deserialized to the global offset where the macro
493 /// record resides.
494 llvm::DenseMap<IdentifierInfo *, uint64_t> UnreadMacroRecordOffsets;
496 /// \name CodeGen-relevant special data
497 /// \brief Fields containing data that is relevant to CodeGen.
498 //@{
500 /// \brief The IDs of all declarations that fulfill the criteria of
501 /// "interesting" decls.
503 /// This contains the data loaded from all EXTERNAL_DEFINITIONS blocks in the
504 /// chain. The referenced declarations are deserialized and passed to the
505 /// consumer eagerly.
506 llvm::SmallVector<uint64_t, 16> ExternalDefinitions;
508 /// \brief The IDs of all tentative definitions stored in the the chain.
510 /// Sema keeps track of all tentative definitions in a TU because it has to
511 /// complete them and pass them on to CodeGen. Thus, tentative definitions in
512 /// the PCH chain must be eagerly deserialized.
513 llvm::SmallVector<uint64_t, 16> TentativeDefinitions;
515 /// \brief The IDs of all CXXRecordDecls stored in the chain whose VTables are
516 /// used.
518 /// CodeGen has to emit VTables for these records, so they have to be eagerly
519 /// deserialized.
520 llvm::SmallVector<uint64_t, 64> VTableUses;
522 //@}
524 /// \name Diagnostic-relevant special data
525 /// \brief Fields containing data that is used for generating diagnostics
526 //@{
528 /// \brief A snapshot of Sema's unused file-scoped variable tracking, for
529 /// generating warnings.
530 llvm::SmallVector<uint64_t, 16> UnusedFileScopedDecls;
532 /// \brief A snapshot of Sema's weak undeclared identifier tracking, for
533 /// generating warnings.
534 llvm::SmallVector<uint64_t, 64> WeakUndeclaredIdentifiers;
536 /// \brief The IDs of type aliases for ext_vectors that exist in the chain.
538 /// Used by Sema for finding sugared names for ext_vectors in diagnostics.
539 llvm::SmallVector<uint64_t, 4> ExtVectorDecls;
541 //@}
543 /// \name Sema-relevant special data
544 /// \brief Fields containing data that is used for semantic analysis
545 //@{
547 /// \brief The IDs of all locally scoped external decls in the chain.
549 /// Sema tracks these to validate that the types are consistent across all
550 /// local external declarations.
551 llvm::SmallVector<uint64_t, 16> LocallyScopedExternalDecls;
553 /// \brief The IDs of all dynamic class declarations in the chain.
555 /// Sema tracks these because it checks for the key functions being defined
556 /// at the end of the TU, in which case it directs CodeGen to emit the VTable.
557 llvm::SmallVector<uint64_t, 16> DynamicClasses;
559 /// \brief The IDs of the declarations Sema stores directly.
561 /// Sema tracks a few important decls, such as namespace std, directly.
562 llvm::SmallVector<uint64_t, 4> SemaDeclRefs;
564 /// \brief The IDs of the types ASTContext stores directly.
566 /// The AST context tracks a few important types, such as va_list, directly.
567 llvm::SmallVector<uint64_t, 16> SpecialTypes;
569 //@}
571 /// \brief Diagnostic IDs and their mappings that the user changed.
572 llvm::SmallVector<uint64_t, 8> PragmaDiagMappings;
574 /// \brief The original file name that was used to build the primary AST file,
575 /// which may have been modified for relocatable-pch support.
576 std::string OriginalFileName;
578 /// \brief The actual original file name that was used to build the primary
579 /// AST file.
580 std::string ActualOriginalFileName;
582 /// \brief Whether this precompiled header is a relocatable PCH file.
583 bool RelocatablePCH;
585 /// \brief The system include root to be used when loading the
586 /// precompiled header.
587 const char *isysroot;
589 /// \brief Whether to disable the normal validation performed on precompiled
590 /// headers when they are loaded.
591 bool DisableValidation;
593 /// \brief Mapping from switch-case IDs in the chain to switch-case statements
595 /// Statements usually don't have IDs, but switch cases need them, so that the
596 /// switch statement can refer to them.
597 std::map<unsigned, SwitchCase *> SwitchCaseStmts;
599 /// \brief Mapping from label statement IDs in the chain to label statements.
601 /// Statements usually don't have IDs, but labeled statements need them, so
602 /// that goto statements and address-of-label expressions can refer to them.
603 std::map<unsigned, LabelStmt *> LabelStmts;
605 /// \brief Mapping from label IDs to the set of "goto" statements
606 /// that point to that label before the label itself has been
607 /// de-serialized.
608 std::multimap<unsigned, GotoStmt *> UnresolvedGotoStmts;
610 /// \brief Mapping from label IDs to the set of address label
611 /// expressions that point to that label before the label itself has
612 /// been de-serialized.
613 std::multimap<unsigned, AddrLabelExpr *> UnresolvedAddrLabelExprs;
615 /// \brief The number of stat() calls that hit/missed the stat
616 /// cache.
617 unsigned NumStatHits, NumStatMisses;
619 /// \brief The number of source location entries de-serialized from
620 /// the PCH file.
621 unsigned NumSLocEntriesRead;
623 /// \brief The number of source location entries in the chain.
624 unsigned TotalNumSLocEntries;
626 /// \brief The next offset for a SLocEntry after everything in this reader.
627 unsigned NextSLocOffset;
629 /// \brief The number of statements (and expressions) de-serialized
630 /// from the chain.
631 unsigned NumStatementsRead;
633 /// \brief The total number of statements (and expressions) stored
634 /// in the chain.
635 unsigned TotalNumStatements;
637 /// \brief The number of macros de-serialized from the chain.
638 unsigned NumMacrosRead;
640 /// \brief The total number of macros stored in the chain.
641 unsigned TotalNumMacros;
643 /// \brief The number of selectors that have been read.
644 unsigned NumSelectorsRead;
646 /// \brief The number of method pool entries that have been read.
647 unsigned NumMethodPoolEntriesRead;
649 /// \brief The number of times we have looked up a selector in the method
650 /// pool and not found anything interesting.
651 unsigned NumMethodPoolMisses;
653 /// \brief The total number of method pool entries in the selector table.
654 unsigned TotalNumMethodPoolEntries;
656 /// Number of lexical decl contexts read/total.
657 unsigned NumLexicalDeclContextsRead, TotalLexicalDeclContexts;
659 /// Number of visible decl contexts read/total.
660 unsigned NumVisibleDeclContextsRead, TotalVisibleDeclContexts;
662 /// \brief Number of Decl/types that are currently deserializing.
663 unsigned NumCurrentElementsDeserializing;
665 /// \brief An IdentifierInfo that has been loaded but whose top-level
666 /// declarations of the same name have not (yet) been loaded.
667 struct PendingIdentifierInfo {
668 IdentifierInfo *II;
669 llvm::SmallVector<uint32_t, 4> DeclIDs;
672 /// \brief The set of identifiers that were read while the AST reader was
673 /// (recursively) loading declarations.
675 /// The declarations on the identifier chain for these identifiers will be
676 /// loaded once the recursive loading has completed.
677 std::deque<PendingIdentifierInfo> PendingIdentifierInfos;
679 /// \brief Contains declarations and definitions that will be
680 /// "interesting" to the ASTConsumer, when we get that AST consumer.
682 /// "Interesting" declarations are those that have data that may
683 /// need to be emitted, such as inline function definitions or
684 /// Objective-C protocols.
685 std::deque<Decl *> InterestingDecls;
687 /// \brief When reading a Stmt tree, Stmt operands are placed in this stack.
688 llvm::SmallVector<Stmt *, 16> StmtStack;
690 /// \brief What kind of records we are reading.
691 enum ReadingKind {
692 Read_Decl, Read_Type, Read_Stmt
695 /// \brief What kind of records we are reading.
696 ReadingKind ReadingKind;
698 /// \brief RAII object to change the reading kind.
699 class ReadingKindTracker {
700 ASTReader &Reader;
701 enum ReadingKind PrevKind;
703 ReadingKindTracker(const ReadingKindTracker&); // do not implement
704 ReadingKindTracker &operator=(const ReadingKindTracker&);// do not implement
706 public:
707 ReadingKindTracker(enum ReadingKind newKind, ASTReader &reader)
708 : Reader(reader), PrevKind(Reader.ReadingKind) {
709 Reader.ReadingKind = newKind;
712 ~ReadingKindTracker() { Reader.ReadingKind = PrevKind; }
715 /// \brief All predefines buffers in the chain, to be treated as if
716 /// concatenated.
717 PCHPredefinesBlocks PCHPredefinesBuffers;
719 /// \brief Suggested contents of the predefines buffer, after this
720 /// PCH file has been processed.
722 /// In most cases, this string will be empty, because the predefines
723 /// buffer computed to build the PCH file will be identical to the
724 /// predefines buffer computed from the command line. However, when
725 /// there are differences that the PCH reader can work around, this
726 /// predefines buffer may contain additional definitions.
727 std::string SuggestedPredefines;
729 /// \brief Reads a statement from the specified cursor.
730 Stmt *ReadStmtFromStream(PerFileData &F);
732 void MaybeAddSystemRootToFilename(std::string &Filename);
734 ASTReadResult ReadASTCore(llvm::StringRef FileName, ASTFileType Type);
735 ASTReadResult ReadASTBlock(PerFileData &F);
736 bool CheckPredefinesBuffers();
737 bool ParseLineTable(PerFileData &F, llvm::SmallVectorImpl<uint64_t> &Record);
738 ASTReadResult ReadSourceManagerBlock(PerFileData &F);
739 ASTReadResult ReadSLocEntryRecord(unsigned ID);
740 PerFileData *SLocCursorForID(unsigned ID);
741 SourceLocation getImportLocation(PerFileData *F);
742 bool ParseLanguageOptions(const llvm::SmallVectorImpl<uint64_t> &Record);
744 struct RecordLocation {
745 RecordLocation(PerFileData *M, uint64_t O)
746 : F(M), Offset(O) {}
747 PerFileData *F;
748 uint64_t Offset;
751 QualType ReadTypeRecord(unsigned Index);
752 RecordLocation TypeCursorForIndex(unsigned Index);
753 void LoadedDecl(unsigned Index, Decl *D);
754 Decl *ReadDeclRecord(unsigned Index, serialization::DeclID ID);
755 RecordLocation DeclCursorForIndex(unsigned Index, serialization::DeclID ID);
757 void PassInterestingDeclsToConsumer();
759 /// \brief Produce an error diagnostic and return true.
761 /// This routine should only be used for fatal errors that have to
762 /// do with non-routine failures (e.g., corrupted AST file).
763 void Error(const char *Msg);
765 ASTReader(const ASTReader&); // do not implement
766 ASTReader &operator=(const ASTReader &); // do not implement
767 public:
768 typedef llvm::SmallVector<uint64_t, 64> RecordData;
770 /// \brief Load the AST file and validate its contents against the given
771 /// Preprocessor.
773 /// \param PP the preprocessor associated with the context in which this
774 /// precompiled header will be loaded.
776 /// \param Context the AST context that this precompiled header will be
777 /// loaded into.
779 /// \param isysroot If non-NULL, the system include path specified by the
780 /// user. This is only used with relocatable PCH files. If non-NULL,
781 /// a relocatable PCH file will use the default path "/".
783 /// \param DisableValidation If true, the AST reader will suppress most
784 /// of its regular consistency checking, allowing the use of precompiled
785 /// headers that cannot be determined to be compatible.
786 ASTReader(Preprocessor &PP, ASTContext *Context, const char *isysroot = 0,
787 bool DisableValidation = false);
789 /// \brief Load the AST file without using any pre-initialized Preprocessor.
791 /// The necessary information to initialize a Preprocessor later can be
792 /// obtained by setting a ASTReaderListener.
794 /// \param SourceMgr the source manager into which the AST file will be loaded
796 /// \param FileMgr the file manager into which the AST file will be loaded.
798 /// \param Diags the diagnostics system to use for reporting errors and
799 /// warnings relevant to loading the AST file.
801 /// \param isysroot If non-NULL, the system include path specified by the
802 /// user. This is only used with relocatable PCH files. If non-NULL,
803 /// a relocatable PCH file will use the default path "/".
805 /// \param DisableValidation If true, the AST reader will suppress most
806 /// of its regular consistency checking, allowing the use of precompiled
807 /// headers that cannot be determined to be compatible.
808 ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
809 Diagnostic &Diags, const char *isysroot = 0,
810 bool DisableValidation = false);
811 ~ASTReader();
813 /// \brief Load the precompiled header designated by the given file
814 /// name.
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 Diagnostic &Diags);
843 /// \brief Returns the suggested contents of the predefines buffer,
844 /// which contains a (typically-empty) subset of the predefines
845 /// build prior to including the precompiled header.
846 const std::string &getSuggestedPredefines() { return SuggestedPredefines; }
848 /// \brief Read preprocessed entities into the preprocessing record.
849 virtual void ReadPreprocessedEntities();
851 /// \brief Read the preprocessed entity at the given offset.
852 virtual PreprocessedEntity *ReadPreprocessedEntity(uint64_t Offset);
854 void ReadPragmaDiagnosticMappings(Diagnostic &Diag);
856 /// \brief Returns the number of source locations found in the chain.
857 unsigned getTotalNumSLocs() const {
858 return TotalNumSLocEntries;
861 /// \brief Returns the next SLocEntry offset after the chain.
862 unsigned getNextSLocOffset() const {
863 return NextSLocOffset;
866 /// \brief Returns the number of identifiers found in the chain.
867 unsigned getTotalNumIdentifiers() const {
868 return static_cast<unsigned>(IdentifiersLoaded.size());
871 /// \brief Returns the number of types found in the chain.
872 unsigned getTotalNumTypes() const {
873 return static_cast<unsigned>(TypesLoaded.size());
876 /// \brief Returns the number of declarations found in the chain.
877 unsigned getTotalNumDecls() const {
878 return static_cast<unsigned>(DeclsLoaded.size());
881 /// \brief Returns the number of selectors found in the chain.
882 unsigned getTotalNumSelectors() const {
883 return static_cast<unsigned>(SelectorsLoaded.size());
886 /// \brief Returns the number of macro definitions found in the chain.
887 unsigned getTotalNumMacroDefinitions() const {
888 return static_cast<unsigned>(MacroDefinitionsLoaded.size());
891 /// \brief Returns the number of C++ base specifiers found in the chain.
892 unsigned getTotalNumCXXBaseSpecifiers() const;
894 /// \brief Reads a TemplateArgumentLocInfo appropriate for the
895 /// given TemplateArgument kind.
896 TemplateArgumentLocInfo
897 GetTemplateArgumentLocInfo(PerFileData &F, TemplateArgument::ArgKind Kind,
898 const RecordData &Record, unsigned &Idx);
900 /// \brief Reads a TemplateArgumentLoc.
901 TemplateArgumentLoc
902 ReadTemplateArgumentLoc(PerFileData &F,
903 const RecordData &Record, unsigned &Idx);
905 /// \brief Reads a declarator info from the given record.
906 TypeSourceInfo *GetTypeSourceInfo(PerFileData &F,
907 const RecordData &Record, unsigned &Idx);
909 /// \brief Resolve and return the translation unit declaration.
910 TranslationUnitDecl *GetTranslationUnitDecl();
912 /// \brief Resolve a type ID into a type, potentially building a new
913 /// type.
914 QualType GetType(serialization::TypeID ID);
916 /// \brief Returns the type ID associated with the given type.
917 /// If the type didn't come from the AST file the ID that is returned is
918 /// marked as "doesn't exist in AST".
919 serialization::TypeID GetTypeID(QualType T) const;
921 /// \brief Returns the type index associated with the given type.
922 /// If the type didn't come from the AST file the index that is returned is
923 /// marked as "doesn't exist in AST".
924 serialization::TypeIdx GetTypeIdx(QualType T) const;
926 /// \brief Resolve a declaration ID into a declaration, potentially
927 /// building a new declaration.
928 Decl *GetDecl(serialization::DeclID ID);
929 virtual Decl *GetExternalDecl(uint32_t ID);
931 /// \brief Resolve a CXXBaseSpecifiers ID into an offset into the chain
932 /// of loaded AST files.
933 uint64_t GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID);
935 virtual CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset);
937 /// \brief Resolve the offset of a statement into a statement.
939 /// This operation will read a new statement from the external
940 /// source each time it is called, and is meant to be used via a
941 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
942 virtual Stmt *GetExternalDeclStmt(uint64_t Offset);
944 /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
945 /// specified cursor. Read the abbreviations that are at the top of the block
946 /// and then leave the cursor pointing into the block.
947 bool ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor, unsigned BlockID);
949 /// \brief Finds all the visible declarations with a given name.
950 /// The current implementation of this method just loads the entire
951 /// lookup table as unmaterialized references.
952 virtual DeclContext::lookup_result
953 FindExternalVisibleDeclsByName(const DeclContext *DC,
954 DeclarationName Name);
956 virtual void MaterializeVisibleDecls(const DeclContext *DC);
958 /// \brief Read all of the declarations lexically stored in a
959 /// declaration context.
961 /// \param DC The declaration context whose declarations will be
962 /// read.
964 /// \param Decls Vector that will contain the declarations loaded
965 /// from the external source. The caller is responsible for merging
966 /// these declarations with any declarations already stored in the
967 /// declaration context.
969 /// \returns true if there was an error while reading the
970 /// declarations for this declaration context.
971 virtual bool FindExternalLexicalDecls(const DeclContext *DC,
972 bool (*isKindWeWant)(Decl::Kind),
973 llvm::SmallVectorImpl<Decl*> &Decls);
975 /// \brief Notify ASTReader that we started deserialization of
976 /// a decl or type so until FinishedDeserializing is called there may be
977 /// decls that are initializing. Must be paired with FinishedDeserializing.
978 virtual void StartedDeserializing() { ++NumCurrentElementsDeserializing; }
980 /// \brief Notify ASTReader that we finished the deserialization of
981 /// a decl or type. Must be paired with StartedDeserializing.
982 virtual void FinishedDeserializing();
984 /// \brief Function that will be invoked when we begin parsing a new
985 /// translation unit involving this external AST source.
987 /// This function will provide all of the external definitions to
988 /// the ASTConsumer.
989 virtual void StartTranslationUnit(ASTConsumer *Consumer);
991 /// \brief Print some statistics about AST usage.
992 virtual void PrintStats();
994 /// \brief Initialize the semantic source with the Sema instance
995 /// being used to perform semantic analysis on the abstract syntax
996 /// tree.
997 virtual void InitializeSema(Sema &S);
999 /// \brief Inform the semantic consumer that Sema is no longer available.
1000 virtual void ForgetSema() { SemaObj = 0; }
1002 /// \brief Retrieve the IdentifierInfo for the named identifier.
1004 /// This routine builds a new IdentifierInfo for the given identifier. If any
1005 /// declarations with this name are visible from translation unit scope, their
1006 /// declarations will be deserialized and introduced into the declaration
1007 /// chain of the identifier.
1008 virtual IdentifierInfo *get(const char *NameStart, const char *NameEnd);
1009 IdentifierInfo *get(llvm::StringRef Name) {
1010 return get(Name.begin(), Name.end());
1013 /// \brief Retrieve an iterator into the set of all identifiers
1014 /// in all loaded AST files.
1015 virtual IdentifierIterator *getIdentifiers() const;
1017 /// \brief Load the contents of the global method pool for a given
1018 /// selector.
1020 /// \returns a pair of Objective-C methods lists containing the
1021 /// instance and factory methods, respectively, with this selector.
1022 virtual std::pair<ObjCMethodList, ObjCMethodList>
1023 ReadMethodPool(Selector Sel);
1025 /// \brief Load a selector from disk, registering its ID if it exists.
1026 void LoadSelector(Selector Sel);
1028 void SetIdentifierInfo(unsigned ID, IdentifierInfo *II);
1029 void SetGloballyVisibleDecls(IdentifierInfo *II,
1030 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
1031 bool Nonrecursive = false);
1033 /// \brief Report a diagnostic.
1034 DiagnosticBuilder Diag(unsigned DiagID);
1036 /// \brief Report a diagnostic.
1037 DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID);
1039 IdentifierInfo *DecodeIdentifierInfo(unsigned Idx);
1041 IdentifierInfo *GetIdentifierInfo(const RecordData &Record, unsigned &Idx) {
1042 return DecodeIdentifierInfo(Record[Idx++]);
1045 virtual IdentifierInfo *GetIdentifier(unsigned ID) {
1046 return DecodeIdentifierInfo(ID);
1049 /// \brief Read the source location entry with index ID.
1050 virtual void ReadSLocEntry(unsigned ID);
1052 Selector DecodeSelector(unsigned Idx);
1054 virtual Selector GetExternalSelector(uint32_t ID);
1055 uint32_t GetNumExternalSelectors();
1057 Selector GetSelector(const RecordData &Record, unsigned &Idx) {
1058 return DecodeSelector(Record[Idx++]);
1061 /// \brief Read a declaration name.
1062 DeclarationName ReadDeclarationName(const RecordData &Record, unsigned &Idx);
1063 void ReadDeclarationNameLoc(PerFileData &F,
1064 DeclarationNameLoc &DNLoc, DeclarationName Name,
1065 const RecordData &Record, unsigned &Idx);
1066 void ReadDeclarationNameInfo(PerFileData &F, DeclarationNameInfo &NameInfo,
1067 const RecordData &Record, unsigned &Idx);
1069 void ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
1070 const RecordData &Record, unsigned &Idx);
1072 NestedNameSpecifier *ReadNestedNameSpecifier(const RecordData &Record,
1073 unsigned &Idx);
1075 /// \brief Read a template name.
1076 TemplateName ReadTemplateName(PerFileData &F, const RecordData &Record,
1077 unsigned &Idx);
1079 /// \brief Read a template argument.
1080 TemplateArgument ReadTemplateArgument(PerFileData &F,
1081 const RecordData &Record,unsigned &Idx);
1083 /// \brief Read a template parameter list.
1084 TemplateParameterList *ReadTemplateParameterList(PerFileData &F,
1085 const RecordData &Record,
1086 unsigned &Idx);
1088 /// \brief Read a template argument array.
1089 void
1090 ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
1091 PerFileData &F, const RecordData &Record,
1092 unsigned &Idx);
1094 /// \brief Read a UnresolvedSet structure.
1095 void ReadUnresolvedSet(UnresolvedSetImpl &Set,
1096 const RecordData &Record, unsigned &Idx);
1098 /// \brief Read a C++ base specifier.
1099 CXXBaseSpecifier ReadCXXBaseSpecifier(PerFileData &F,
1100 const RecordData &Record,unsigned &Idx);
1102 /// \brief Read a CXXCtorInitializer array.
1103 std::pair<CXXCtorInitializer **, unsigned>
1104 ReadCXXCtorInitializers(PerFileData &F, const RecordData &Record,
1105 unsigned &Idx);
1107 /// \brief Read a source location from raw form.
1108 SourceLocation ReadSourceLocation(PerFileData &Module, unsigned Raw) {
1109 (void)Module; // No remapping yet
1110 return SourceLocation::getFromRawEncoding(Raw);
1113 /// \brief Read a source location.
1114 SourceLocation ReadSourceLocation(PerFileData &Module,
1115 const RecordData &Record, unsigned& Idx) {
1116 return ReadSourceLocation(Module, Record[Idx++]);
1119 /// \brief Read a source range.
1120 SourceRange ReadSourceRange(PerFileData &F,
1121 const RecordData &Record, unsigned& Idx);
1123 /// \brief Read an integral value
1124 llvm::APInt ReadAPInt(const RecordData &Record, unsigned &Idx);
1126 /// \brief Read a signed integral value
1127 llvm::APSInt ReadAPSInt(const RecordData &Record, unsigned &Idx);
1129 /// \brief Read a floating-point value
1130 llvm::APFloat ReadAPFloat(const RecordData &Record, unsigned &Idx);
1132 // \brief Read a string
1133 std::string ReadString(const RecordData &Record, unsigned &Idx);
1135 CXXTemporary *ReadCXXTemporary(const RecordData &Record, unsigned &Idx);
1137 /// \brief Reads attributes from the current stream position.
1138 void ReadAttributes(PerFileData &F, AttrVec &Attrs,
1139 const RecordData &Record, unsigned &Idx);
1141 /// \brief Reads a statement.
1142 Stmt *ReadStmt(PerFileData &F);
1144 /// \brief Reads an expression.
1145 Expr *ReadExpr(PerFileData &F);
1147 /// \brief Reads a sub-statement operand during statement reading.
1148 Stmt *ReadSubStmt() {
1149 assert(ReadingKind == Read_Stmt &&
1150 "Should be called only during statement reading!");
1151 // Subexpressions are stored from last to first, so the next Stmt we need
1152 // is at the back of the stack.
1153 assert(!StmtStack.empty() && "Read too many sub statements!");
1154 return StmtStack.pop_back_val();
1157 /// \brief Reads a sub-expression operand during statement reading.
1158 Expr *ReadSubExpr();
1160 /// \brief Reads the macro record located at the given offset.
1161 PreprocessedEntity *ReadMacroRecord(PerFileData &F, uint64_t Offset);
1163 /// \brief Note that the identifier is a macro whose record will be loaded
1164 /// from the given AST file at the given (file-local) offset.
1165 void SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1166 uint64_t Offset);
1168 /// \brief Read the set of macros defined by this external macro source.
1169 virtual void ReadDefinedMacros();
1171 /// \brief Read the macro definition for this identifier.
1172 virtual void LoadMacroDefinition(IdentifierInfo *II);
1174 /// \brief Read the macro definition corresponding to this iterator
1175 /// into the unread macro record offsets table.
1176 void LoadMacroDefinition(
1177 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos);
1179 /// \brief Retrieve the macro definition with the given ID.
1180 MacroDefinition *getMacroDefinition(serialization::MacroID ID);
1182 /// \brief Retrieve the AST context that this AST reader supplements.
1183 ASTContext *getContext() { return Context; }
1185 // \brief Contains declarations that were loaded before we have
1186 // access to a Sema object.
1187 llvm::SmallVector<NamedDecl *, 16> PreloadedDecls;
1189 /// \brief Retrieve the semantic analysis object used to analyze the
1190 /// translation unit in which the precompiled header is being
1191 /// imported.
1192 Sema *getSema() { return SemaObj; }
1194 /// \brief Retrieve the identifier table associated with the
1195 /// preprocessor.
1196 IdentifierTable &getIdentifierTable();
1198 /// \brief Record that the given ID maps to the given switch-case
1199 /// statement.
1200 void RecordSwitchCaseID(SwitchCase *SC, unsigned ID);
1202 /// \brief Retrieve the switch-case statement with the given ID.
1203 SwitchCase *getSwitchCaseWithID(unsigned ID);
1205 /// \brief Record that the given label statement has been
1206 /// deserialized and has the given ID.
1207 void RecordLabelStmt(LabelStmt *S, unsigned ID);
1209 void ClearSwitchCaseIDs();
1211 /// \brief Set the label of the given statement to the label
1212 /// identified by ID.
1214 /// Depending on the order in which the label and other statements
1215 /// referencing that label occur, this operation may complete
1216 /// immediately (updating the statement) or it may queue the
1217 /// statement to be back-patched later.
1218 void SetLabelOf(GotoStmt *S, unsigned ID);
1220 /// \brief Set the label of the given expression to the label
1221 /// identified by ID.
1223 /// Depending on the order in which the label and other statements
1224 /// referencing that label occur, this operation may complete
1225 /// immediately (updating the statement) or it may queue the
1226 /// statement to be back-patched later.
1227 void SetLabelOf(AddrLabelExpr *S, unsigned ID);
1230 /// \brief Helper class that saves the current stream position and
1231 /// then restores it when destroyed.
1232 struct SavedStreamPosition {
1233 explicit SavedStreamPosition(llvm::BitstreamCursor &Cursor)
1234 : Cursor(Cursor), Offset(Cursor.GetCurrentBitNo()) { }
1236 ~SavedStreamPosition() {
1237 Cursor.JumpToBit(Offset);
1240 private:
1241 llvm::BitstreamCursor &Cursor;
1242 uint64_t Offset;
1245 inline void PCHValidator::Error(const char *Msg) {
1246 Reader.Error(Msg);
1249 } // end namespace clang
1251 #endif