ASTReader.h revision 1d9f1fe7173e3084325f43c78af812a36d8a2a7c
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.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_FRONTEND_AST_READER_H
15#define LLVM_CLANG_FRONTEND_AST_READER_H
16
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"
35#include <deque>
36#include <map>
37#include <string>
38#include <utility>
39#include <vector>
40
41namespace llvm {
42  class MemoryBuffer;
43}
44
45namespace clang {
46
47class AddrLabelExpr;
48class ASTConsumer;
49class ASTContext;
50class Attr;
51class Decl;
52class DeclContext;
53class NestedNameSpecifier;
54class CXXBaseSpecifier;
55class CXXBaseOrMemberInitializer;
56class GotoStmt;
57class LabelStmt;
58class MacroDefinition;
59class NamedDecl;
60class Preprocessor;
61class Sema;
62class SwitchCase;
63class ASTDeserializationListener;
64class ASTReader;
65class ASTDeclReader;
66class ASTStmtReader;
67class ASTIdentifierLookupTrait;
68class TypeLocReader;
69struct HeaderFileInfo;
70
71struct PCHPredefinesBlock {
72  /// \brief The file ID for this predefines buffer in a PCH file.
73  FileID BufferID;
74
75  /// \brief This predefines buffer in a PCH file.
76  llvm::StringRef Data;
77};
78typedef llvm::SmallVector<PCHPredefinesBlock, 2> PCHPredefinesBlocks;
79
80/// \brief Abstract interface for callback invocations by the ASTReader.
81///
82/// While reading an AST file, the ASTReader will call the methods of the
83/// listener to pass on specific information. Some of the listener methods can
84/// return true to indicate to the ASTReader that the information (and
85/// consequently the AST file) is invalid.
86class ASTReaderListener {
87public:
88  virtual ~ASTReaderListener();
89
90  /// \brief Receives the language options.
91  ///
92  /// \returns true to indicate the options are invalid or false otherwise.
93  virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
94    return false;
95  }
96
97  /// \brief Receives the target triple.
98  ///
99  /// \returns true to indicate the target triple is invalid or false otherwise.
100  virtual bool ReadTargetTriple(llvm::StringRef Triple) {
101    return false;
102  }
103
104  /// \brief Receives the contents of the predefines buffer.
105  ///
106  /// \param Buffers Information about the predefines buffers.
107  ///
108  /// \param OriginalFileName The original file name for the AST file, which
109  /// will appear as an entry in the predefines buffer.
110  ///
111  /// \param SuggestedPredefines If necessary, additional definitions are added
112  /// here.
113  ///
114  /// \returns true to indicate the predefines are invalid or false otherwise.
115  virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
116                                    llvm::StringRef OriginalFileName,
117                                    std::string &SuggestedPredefines) {
118    return false;
119  }
120
121  /// \brief Receives a HeaderFileInfo entry.
122  virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) {}
123
124  /// \brief Receives __COUNTER__ value.
125  virtual void ReadCounter(unsigned Value) {}
126};
127
128/// \brief ASTReaderListener implementation to validate the information of
129/// the PCH file against an initialized Preprocessor.
130class PCHValidator : public ASTReaderListener {
131  Preprocessor &PP;
132  ASTReader &Reader;
133
134  unsigned NumHeaderInfos;
135
136public:
137  PCHValidator(Preprocessor &PP, ASTReader &Reader)
138    : PP(PP), Reader(Reader), NumHeaderInfos(0) {}
139
140  virtual bool ReadLanguageOptions(const LangOptions &LangOpts);
141  virtual bool ReadTargetTriple(llvm::StringRef Triple);
142  virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
143                                    llvm::StringRef OriginalFileName,
144                                    std::string &SuggestedPredefines);
145  virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID);
146  virtual void ReadCounter(unsigned Value);
147
148private:
149  void Error(const char *Msg);
150};
151
152/// \brief Reads an AST files chain containing the contents of a translation
153/// unit.
154///
155/// The ASTReader class reads bitstreams (produced by the ASTWriter
156/// class) containing the serialized representation of a given
157/// abstract syntax tree and its supporting data structures. An
158/// instance of the ASTReader can be attached to an ASTContext object,
159/// which will provide access to the contents of the AST files.
160///
161/// The AST reader provides lazy de-serialization of declarations, as
162/// required when traversing the AST. Only those AST nodes that are
163/// actually required will be de-serialized.
164class ASTReader
165  : public ExternalPreprocessorSource,
166    public ExternalPreprocessingRecordSource,
167    public ExternalSemaSource,
168    public IdentifierInfoLookup,
169    public ExternalIdentifierLookup,
170    public ExternalSLocEntrySource {
171public:
172  enum ASTReadResult { Success, Failure, IgnorePCH };
173  /// \brief Types of AST files.
174  enum ASTFileType {
175    Module,   ///< File is a module proper.
176    PCH,      ///< File is a PCH file treated as such.
177    Preamble, ///< File is a PCH file treated as the preamble.
178    MainFile  ///< File is a PCH file treated as the actual main file.
179  };
180  friend class PCHValidator;
181  friend class ASTDeclReader;
182  friend class ASTStmtReader;
183  friend class ASTIdentifierLookupTrait;
184  friend class TypeLocReader;
185private:
186  /// \brief The receiver of some callbacks invoked by ASTReader.
187  llvm::OwningPtr<ASTReaderListener> Listener;
188
189  /// \brief The receiver of deserialization events.
190  ASTDeserializationListener *DeserializationListener;
191
192  SourceManager &SourceMgr;
193  FileManager &FileMgr;
194  Diagnostic &Diags;
195
196  /// \brief The semantic analysis object that will be processing the
197  /// AST files and the translation unit that uses it.
198  Sema *SemaObj;
199
200  /// \brief The preprocessor that will be loading the source file.
201  Preprocessor *PP;
202
203  /// \brief The AST context into which we'll read the AST files.
204  ASTContext *Context;
205
206  /// \brief The AST consumer.
207  ASTConsumer *Consumer;
208
209  /// \brief Information that is needed for every module.
210  struct PerFileData {
211    PerFileData(ASTFileType Ty);
212    ~PerFileData();
213
214    // === General information ===
215
216    /// \brief The type of this AST file.
217    ASTFileType Type;
218
219    /// \brief The file name of the AST file.
220    std::string FileName;
221
222    /// \brief The memory buffer that stores the data associated with
223    /// this AST file.
224    llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
225
226    /// \brief The size of this file, in bits.
227    uint64_t SizeInBits;
228
229    /// \brief The bitstream reader from which we'll read the AST file.
230    llvm::BitstreamReader StreamFile;
231
232    /// \brief The main bitstream cursor for the main block.
233    llvm::BitstreamCursor Stream;
234
235    // === Source Locations ===
236
237    /// \brief Cursor used to read source location entries.
238    llvm::BitstreamCursor SLocEntryCursor;
239
240    /// \brief The number of source location entries in this AST file.
241    unsigned LocalNumSLocEntries;
242
243    /// \brief Offsets for all of the source location entries in the
244    /// AST file.
245    const uint32_t *SLocOffsets;
246
247    /// \brief The entire size of this module's source location offset range.
248    unsigned LocalSLocSize;
249
250    // === Identifiers ===
251
252    /// \brief The number of identifiers in this AST file.
253    unsigned LocalNumIdentifiers;
254
255    /// \brief Offsets into the identifier table data.
256    ///
257    /// This array is indexed by the identifier ID (-1), and provides
258    /// the offset into IdentifierTableData where the string data is
259    /// stored.
260    const uint32_t *IdentifierOffsets;
261
262    /// \brief Actual data for the on-disk hash table.
263    ///
264    /// This pointer points into a memory buffer, where the on-disk hash
265    /// table for identifiers actually lives.
266    const char *IdentifierTableData;
267
268    /// \brief A pointer to an on-disk hash table of opaque type
269    /// IdentifierHashTable.
270    void *IdentifierLookupTable;
271
272    // === Macros ===
273
274    /// \brief The cursor to the start of the preprocessor block, which stores
275    /// all of the macro definitions.
276    llvm::BitstreamCursor MacroCursor;
277
278    /// \brief The number of macro definitions in this file.
279    unsigned LocalNumMacroDefinitions;
280
281    /// \brief Offsets of all of the macro definitions in the preprocessing
282    /// record in the AST file.
283    const uint32_t *MacroDefinitionOffsets;
284
285    // === Selectors ===
286
287    /// \brief The number of selectors new to this file.
288    ///
289    /// This is the number of entries in SelectorOffsets.
290    unsigned LocalNumSelectors;
291
292    /// \brief Offsets into the selector lookup table's data array
293    /// where each selector resides.
294    const uint32_t *SelectorOffsets;
295
296    /// \brief A pointer to the character data that comprises the selector table
297    ///
298    /// The SelectorOffsets table refers into this memory.
299    const unsigned char *SelectorLookupTableData;
300
301    /// \brief A pointer to an on-disk hash table of opaque type
302    /// ASTSelectorLookupTable.
303    ///
304    /// This hash table provides the IDs of all selectors, and the associated
305    /// instance and factory methods.
306    void *SelectorLookupTable;
307
308    /// \brief Method selectors used in a @selector expression. Used for
309    /// implementation of -Wselector.
310    llvm::SmallVector<uint64_t, 64> ReferencedSelectorsData;
311
312    // === Declarations ===
313
314    /// DeclsCursor - This is a cursor to the start of the DECLS_BLOCK block. It
315    /// has read all the abbreviations at the start of the block and is ready to
316    /// jump around with these in context.
317    llvm::BitstreamCursor DeclsCursor;
318
319    /// \brief The number of declarations in this AST file.
320    unsigned LocalNumDecls;
321
322    /// \brief Offset of each declaration within the bitstream, indexed
323    /// by the declaration ID (-1).
324    const uint32_t *DeclOffsets;
325
326    /// \brief A snapshot of the pending instantiations in the chain.
327    ///
328    /// This record tracks the instantiations that Sema has to perform at the
329    /// end of the TU. It consists of a pair of values for every pending
330    /// instantiation where the first value is the ID of the decl and the second
331    /// is the instantiation location.
332    llvm::SmallVector<uint64_t, 64> PendingInstantiations;
333
334    // === Types ===
335
336    /// \brief The number of types in this AST file.
337    unsigned LocalNumTypes;
338
339    /// \brief Offset of each type within the bitstream, indexed by the
340    /// type ID, or the representation of a Type*.
341    const uint32_t *TypeOffsets;
342
343    // === Miscellaneous ===
344
345    /// \brief The AST stat cache installed for this file, if any.
346    ///
347    /// The dynamic type of this stat cache is always ASTStatCache
348    void *StatCache;
349
350    /// \brief The number of preallocated preprocessing entities in the
351    /// preprocessing record.
352    unsigned NumPreallocatedPreprocessingEntities;
353
354    /// \brief The next module in source order.
355    PerFileData *NextInSource;
356
357    /// \brief All the modules that loaded this one. Can contain NULL for
358    /// directly loaded modules.
359    llvm::SmallVector<PerFileData *, 1> Loaders;
360  };
361
362  /// \brief All loaded modules, indexed by name.
363  llvm::StringMap<PerFileData*> Modules;
364
365  /// \brief The first module in source order.
366  PerFileData *FirstInSource;
367
368  /// \brief The chain of AST files. The first entry is the one named by the
369  /// user, the last one is the one that doesn't depend on anything further.
370  /// That is, the entry I was created with -include-pch I+1.
371  llvm::SmallVector<PerFileData*, 2> Chain;
372
373  /// \brief SLocEntries that we're going to preload.
374  llvm::SmallVector<uint64_t, 64> PreloadSLocEntries;
375
376  /// \brief Types that have already been loaded from the chain.
377  ///
378  /// When the pointer at index I is non-NULL, the type with
379  /// ID = (I + 1) << FastQual::Width has already been loaded
380  std::vector<QualType> TypesLoaded;
381
382  /// \brief Map that provides the ID numbers of each type within the
383  /// output stream, plus those deserialized from a chained PCH.
384  ///
385  /// The ID numbers of types are consecutive (in order of discovery)
386  /// and start at 1. 0 is reserved for NULL. When types are actually
387  /// stored in the stream, the ID number is shifted by 2 bits to
388  /// allow for the const/volatile qualifiers.
389  ///
390  /// Keys in the map never have const/volatile qualifiers.
391  serialization::TypeIdxMap TypeIdxs;
392
393  /// \brief Declarations that have already been loaded from the chain.
394  ///
395  /// When the pointer at index I is non-NULL, the declaration with ID
396  /// = I + 1 has already been loaded.
397  std::vector<Decl *> DeclsLoaded;
398
399  typedef llvm::DenseMap<serialization::DeclID,
400                         std::pair<PerFileData *, uint64_t> >
401      DeclReplacementMap;
402  /// \brief Declarations that have been replaced in a later file in the chain.
403  DeclReplacementMap ReplacedDecls;
404
405  /// \brief Information about the contents of a DeclContext.
406  struct DeclContextInfo {
407    void *NameLookupTableData; // a ASTDeclContextNameLookupTable.
408    const serialization::DeclID *LexicalDecls;
409    unsigned NumLexicalDecls;
410  };
411  // In a full chain, there could be multiple updates to every decl context,
412  // so this is a vector. However, typically a chain is only two elements long,
413  // with only one file containing updates, so there will be only one update
414  // per decl context.
415  typedef llvm::SmallVector<DeclContextInfo, 1> DeclContextInfos;
416  typedef llvm::DenseMap<const DeclContext *, DeclContextInfos>
417      DeclContextOffsetsMap;
418  // Updates for visible decls can occur for other contexts than just the
419  // TU, and when we read those update records, the actual context will not
420  // be available yet (unless it's the TU), so have this pending map using the
421  // ID as a key. It will be realized when the context is actually loaded.
422  typedef llvm::SmallVector<void *, 1> DeclContextVisibleUpdates;
423  typedef llvm::DenseMap<serialization::DeclID, DeclContextVisibleUpdates>
424      DeclContextVisibleUpdatesPending;
425
426  /// \brief Offsets of the lexical and visible declarations for each
427  /// DeclContext.
428  DeclContextOffsetsMap DeclContextOffsets;
429
430  /// \brief Updates to the visible declarations of declaration contexts that
431  /// haven't been loaded yet.
432  DeclContextVisibleUpdatesPending PendingVisibleUpdates;
433
434  typedef llvm::DenseMap<serialization::DeclID, serialization::DeclID>
435      FirstLatestDeclIDMap;
436  /// \brief Map of first declarations from a chained PCH that point to the
437  /// most recent declarations in another AST file.
438  FirstLatestDeclIDMap FirstLatestDeclIDs;
439
440  typedef llvm::SmallVector<serialization::DeclID, 4>
441      AdditionalTemplateSpecializations;
442  typedef llvm::DenseMap<serialization::DeclID,
443                         AdditionalTemplateSpecializations>
444      AdditionalTemplateSpecializationsMap;
445
446  /// \brief Additional specializations (including partial) of templates that
447  /// were introduced after the template was serialized.
448  AdditionalTemplateSpecializationsMap AdditionalTemplateSpecializationsPending;
449
450  /// \brief Read the records that describe the contents of declcontexts.
451  bool ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
452                              const std::pair<uint64_t, uint64_t> &Offsets,
453                              DeclContextInfo &Info);
454
455  /// \brief A vector containing identifiers that have already been
456  /// loaded.
457  ///
458  /// If the pointer at index I is non-NULL, then it refers to the
459  /// IdentifierInfo for the identifier with ID=I+1 that has already
460  /// been loaded.
461  std::vector<IdentifierInfo *> IdentifiersLoaded;
462
463  /// \brief A vector containing selectors that have already been loaded.
464  ///
465  /// This vector is indexed by the Selector ID (-1). NULL selector
466  /// entries indicate that the particular selector ID has not yet
467  /// been loaded.
468  llvm::SmallVector<Selector, 16> SelectorsLoaded;
469
470  /// \brief The macro definitions we have already loaded.
471  llvm::SmallVector<MacroDefinition *, 16> MacroDefinitionsLoaded;
472
473  /// \name CodeGen-relevant special data
474  /// \brief Fields containing data that is relevant to CodeGen.
475  //@{
476
477  /// \brief The IDs of all declarations that fulfill the criteria of
478  /// "interesting" decls.
479  ///
480  /// This contains the data loaded from all EXTERNAL_DEFINITIONS blocks in the
481  /// chain. The referenced declarations are deserialized and passed to the
482  /// consumer eagerly.
483  llvm::SmallVector<uint64_t, 16> ExternalDefinitions;
484
485  /// \brief The IDs of all tentative definitions stored in the the chain.
486  ///
487  /// Sema keeps track of all tentative definitions in a TU because it has to
488  /// complete them and pass them on to CodeGen. Thus, tentative definitions in
489  /// the PCH chain must be eagerly deserialized.
490  llvm::SmallVector<uint64_t, 16> TentativeDefinitions;
491
492  /// \brief The IDs of all CXXRecordDecls stored in the chain whose VTables are
493  /// used.
494  ///
495  /// CodeGen has to emit VTables for these records, so they have to be eagerly
496  /// deserialized.
497  llvm::SmallVector<uint64_t, 64> VTableUses;
498
499  //@}
500
501  /// \name Diagnostic-relevant special data
502  /// \brief Fields containing data that is used for generating diagnostics
503  //@{
504
505  /// \brief A snapshot of Sema's unused file-scoped variable tracking, for
506  /// generating warnings.
507  llvm::SmallVector<uint64_t, 16> UnusedFileScopedDecls;
508
509  /// \brief A snapshot of Sema's weak undeclared identifier tracking, for
510  /// generating warnings.
511  llvm::SmallVector<uint64_t, 64> WeakUndeclaredIdentifiers;
512
513  /// \brief The IDs of type aliases for ext_vectors that exist in the chain.
514  ///
515  /// Used by Sema for finding sugared names for ext_vectors in diagnostics.
516  llvm::SmallVector<uint64_t, 4> ExtVectorDecls;
517
518  //@}
519
520  /// \name Sema-relevant special data
521  /// \brief Fields containing data that is used for semantic analysis
522  //@{
523
524  /// \brief The IDs of all locally scoped external decls in the chain.
525  ///
526  /// Sema tracks these to validate that the types are consistent across all
527  /// local external declarations.
528  llvm::SmallVector<uint64_t, 16> LocallyScopedExternalDecls;
529
530  /// \brief The IDs of all dynamic class declarations in the chain.
531  ///
532  /// Sema tracks these because it checks for the key functions being defined
533  /// at the end of the TU, in which case it directs CodeGen to emit the VTable.
534  llvm::SmallVector<uint64_t, 16> DynamicClasses;
535
536  /// \brief The IDs of the declarations Sema stores directly.
537  ///
538  /// Sema tracks a few important decls, such as namespace std, directly.
539  llvm::SmallVector<uint64_t, 4> SemaDeclRefs;
540
541  /// \brief The IDs of the types ASTContext stores directly.
542  ///
543  /// The AST context tracks a few important types, such as va_list, directly.
544  llvm::SmallVector<uint64_t, 16> SpecialTypes;
545
546  //@}
547
548  /// \brief The original file name that was used to build the primary AST file,
549  /// which may have been modified for relocatable-pch support.
550  std::string OriginalFileName;
551
552  /// \brief The actual original file name that was used to build the primary
553  /// AST file.
554  std::string ActualOriginalFileName;
555
556  /// \brief Whether this precompiled header is a relocatable PCH file.
557  bool RelocatablePCH;
558
559  /// \brief The system include root to be used when loading the
560  /// precompiled header.
561  const char *isysroot;
562
563  /// \brief Whether to disable the normal validation performed on precompiled
564  /// headers when they are loaded.
565  bool DisableValidation;
566
567  /// \brief Mapping from switch-case IDs in the chain to switch-case statements
568  ///
569  /// Statements usually don't have IDs, but switch cases need them, so that the
570  /// switch statement can refer to them.
571  std::map<unsigned, SwitchCase *> SwitchCaseStmts;
572
573  /// \brief Mapping from label statement IDs in the chain to label statements.
574  ///
575  /// Statements usually don't have IDs, but labeled statements need them, so
576  /// that goto statements and address-of-label expressions can refer to them.
577  std::map<unsigned, LabelStmt *> LabelStmts;
578
579  /// \brief Mapping from label IDs to the set of "goto" statements
580  /// that point to that label before the label itself has been
581  /// de-serialized.
582  std::multimap<unsigned, GotoStmt *> UnresolvedGotoStmts;
583
584  /// \brief Mapping from label IDs to the set of address label
585  /// expressions that point to that label before the label itself has
586  /// been de-serialized.
587  std::multimap<unsigned, AddrLabelExpr *> UnresolvedAddrLabelExprs;
588
589  /// \brief The number of stat() calls that hit/missed the stat
590  /// cache.
591  unsigned NumStatHits, NumStatMisses;
592
593  /// \brief The number of source location entries de-serialized from
594  /// the PCH file.
595  unsigned NumSLocEntriesRead;
596
597  /// \brief The number of source location entries in the chain.
598  unsigned TotalNumSLocEntries;
599
600  /// \brief The next offset for a SLocEntry after everything in this reader.
601  unsigned NextSLocOffset;
602
603  /// \brief The number of statements (and expressions) de-serialized
604  /// from the chain.
605  unsigned NumStatementsRead;
606
607  /// \brief The total number of statements (and expressions) stored
608  /// in the chain.
609  unsigned TotalNumStatements;
610
611  /// \brief The number of macros de-serialized from the chain.
612  unsigned NumMacrosRead;
613
614  /// \brief The total number of macros stored in the chain.
615  unsigned TotalNumMacros;
616
617  /// \brief The number of selectors that have been read.
618  unsigned NumSelectorsRead;
619
620  /// \brief The number of method pool entries that have been read.
621  unsigned NumMethodPoolEntriesRead;
622
623  /// \brief The number of times we have looked up a selector in the method
624  /// pool and not found anything interesting.
625  unsigned NumMethodPoolMisses;
626
627  /// \brief The total number of method pool entries in the selector table.
628  unsigned TotalNumMethodPoolEntries;
629
630  /// Number of lexical decl contexts read/total.
631  unsigned NumLexicalDeclContextsRead, TotalLexicalDeclContexts;
632
633  /// Number of visible decl contexts read/total.
634  unsigned NumVisibleDeclContextsRead, TotalVisibleDeclContexts;
635
636  /// \brief Number of Decl/types that are currently deserializing.
637  unsigned NumCurrentElementsDeserializing;
638
639  /// \brief An IdentifierInfo that has been loaded but whose top-level
640  /// declarations of the same name have not (yet) been loaded.
641  struct PendingIdentifierInfo {
642    IdentifierInfo *II;
643    llvm::SmallVector<uint32_t, 4> DeclIDs;
644  };
645
646  /// \brief The set of identifiers that were read while the AST reader was
647  /// (recursively) loading declarations.
648  ///
649  /// The declarations on the identifier chain for these identifiers will be
650  /// loaded once the recursive loading has completed.
651  std::deque<PendingIdentifierInfo> PendingIdentifierInfos;
652
653  /// \brief Contains declarations and definitions that will be
654  /// "interesting" to the ASTConsumer, when we get that AST consumer.
655  ///
656  /// "Interesting" declarations are those that have data that may
657  /// need to be emitted, such as inline function definitions or
658  /// Objective-C protocols.
659  std::deque<Decl *> InterestingDecls;
660
661  /// \brief When reading a Stmt tree, Stmt operands are placed in this stack.
662  llvm::SmallVector<Stmt *, 16> StmtStack;
663
664  /// \brief What kind of records we are reading.
665  enum ReadingKind {
666    Read_Decl, Read_Type, Read_Stmt
667  };
668
669  /// \brief What kind of records we are reading.
670  ReadingKind ReadingKind;
671
672  /// \brief RAII object to change the reading kind.
673  class ReadingKindTracker {
674    ASTReader &Reader;
675    enum ReadingKind PrevKind;
676
677    ReadingKindTracker(const ReadingKindTracker&); // do not implement
678    ReadingKindTracker &operator=(const ReadingKindTracker&);// do not implement
679
680  public:
681    ReadingKindTracker(enum ReadingKind newKind, ASTReader &reader)
682      : Reader(reader), PrevKind(Reader.ReadingKind) {
683      Reader.ReadingKind = newKind;
684    }
685
686    ~ReadingKindTracker() { Reader.ReadingKind = PrevKind; }
687  };
688
689  /// \brief All predefines buffers in the chain, to be treated as if
690  /// concatenated.
691  PCHPredefinesBlocks PCHPredefinesBuffers;
692
693  /// \brief Suggested contents of the predefines buffer, after this
694  /// PCH file has been processed.
695  ///
696  /// In most cases, this string will be empty, because the predefines
697  /// buffer computed to build the PCH file will be identical to the
698  /// predefines buffer computed from the command line. However, when
699  /// there are differences that the PCH reader can work around, this
700  /// predefines buffer may contain additional definitions.
701  std::string SuggestedPredefines;
702
703  /// \brief Reads a statement from the specified cursor.
704  Stmt *ReadStmtFromStream(PerFileData &F);
705
706  void MaybeAddSystemRootToFilename(std::string &Filename);
707
708  ASTReadResult ReadASTCore(llvm::StringRef FileName, ASTFileType Type);
709  ASTReadResult ReadASTBlock(PerFileData &F);
710  bool CheckPredefinesBuffers();
711  bool ParseLineTable(PerFileData &F, llvm::SmallVectorImpl<uint64_t> &Record);
712  ASTReadResult ReadSourceManagerBlock(PerFileData &F);
713  ASTReadResult ReadSLocEntryRecord(unsigned ID);
714  PerFileData *SLocCursorForID(unsigned ID);
715  SourceLocation getImportLocation(PerFileData *F);
716  bool ParseLanguageOptions(const llvm::SmallVectorImpl<uint64_t> &Record);
717
718  struct RecordLocation {
719    RecordLocation(PerFileData *M, uint64_t O)
720      : F(M), Offset(O) {}
721    PerFileData *F;
722    uint64_t Offset;
723  };
724
725  QualType ReadTypeRecord(unsigned Index);
726  RecordLocation TypeCursorForIndex(unsigned Index);
727  void LoadedDecl(unsigned Index, Decl *D);
728  Decl *ReadDeclRecord(unsigned Index, serialization::DeclID ID);
729  RecordLocation DeclCursorForIndex(unsigned Index, serialization::DeclID ID);
730
731  void PassInterestingDeclsToConsumer();
732
733  /// \brief Produce an error diagnostic and return true.
734  ///
735  /// This routine should only be used for fatal errors that have to
736  /// do with non-routine failures (e.g., corrupted AST file).
737  void Error(const char *Msg);
738
739  ASTReader(const ASTReader&); // do not implement
740  ASTReader &operator=(const ASTReader &); // do not implement
741public:
742  typedef llvm::SmallVector<uint64_t, 64> RecordData;
743
744  /// \brief Load the AST file and validate its contents against the given
745  /// Preprocessor.
746  ///
747  /// \param PP the preprocessor associated with the context in which this
748  /// precompiled header will be loaded.
749  ///
750  /// \param Context the AST context that this precompiled header will be
751  /// loaded into.
752  ///
753  /// \param isysroot If non-NULL, the system include path specified by the
754  /// user. This is only used with relocatable PCH files. If non-NULL,
755  /// a relocatable PCH file will use the default path "/".
756  ///
757  /// \param DisableValidation If true, the AST reader will suppress most
758  /// of its regular consistency checking, allowing the use of precompiled
759  /// headers that cannot be determined to be compatible.
760  ASTReader(Preprocessor &PP, ASTContext *Context, const char *isysroot = 0,
761            bool DisableValidation = false);
762
763  /// \brief Load the AST file without using any pre-initialized Preprocessor.
764  ///
765  /// The necessary information to initialize a Preprocessor later can be
766  /// obtained by setting a ASTReaderListener.
767  ///
768  /// \param SourceMgr the source manager into which the AST file will be loaded
769  ///
770  /// \param FileMgr the file manager into which the AST file will be loaded.
771  ///
772  /// \param Diags the diagnostics system to use for reporting errors and
773  /// warnings relevant to loading the AST file.
774  ///
775  /// \param isysroot If non-NULL, the system include path specified by the
776  /// user. This is only used with relocatable PCH files. If non-NULL,
777  /// a relocatable PCH file will use the default path "/".
778  ///
779  /// \param DisableValidation If true, the AST reader will suppress most
780  /// of its regular consistency checking, allowing the use of precompiled
781  /// headers that cannot be determined to be compatible.
782      ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
783            Diagnostic &Diags, const char *isysroot = 0,
784            bool DisableValidation = false);
785  ~ASTReader();
786
787  /// \brief Load the precompiled header designated by the given file
788  /// name.
789  ASTReadResult ReadAST(const std::string &FileName, ASTFileType Type);
790
791  /// \brief Set the AST callbacks listener.
792  void setListener(ASTReaderListener *listener) {
793    Listener.reset(listener);
794  }
795
796  /// \brief Set the AST deserialization listener.
797  void setDeserializationListener(ASTDeserializationListener *Listener);
798
799  /// \brief Set the Preprocessor to use.
800  void setPreprocessor(Preprocessor &pp);
801
802  /// \brief Sets and initializes the given Context.
803  void InitializeContext(ASTContext &Context);
804
805  /// \brief Retrieve the name of the named (primary) AST file
806  const std::string &getFileName() const { return Chain[0]->FileName; }
807
808  /// \brief Retrieve the name of the original source file name
809  const std::string &getOriginalSourceFile() { return OriginalFileName; }
810
811  /// \brief Retrieve the name of the original source file name directly from
812  /// the AST file, without actually loading the AST file.
813  static std::string getOriginalSourceFile(const std::string &ASTFileName,
814                                           Diagnostic &Diags);
815
816  /// \brief Returns the suggested contents of the predefines buffer,
817  /// which contains a (typically-empty) subset of the predefines
818  /// build prior to including the precompiled header.
819  const std::string &getSuggestedPredefines() { return SuggestedPredefines; }
820
821  /// \brief Read preprocessed entities into the
822  virtual void ReadPreprocessedEntities();
823
824  /// \brief Returns the number of source locations found in the chain.
825  unsigned getTotalNumSLocs() const {
826    return TotalNumSLocEntries;
827  }
828
829  /// \brief Returns the next SLocEntry offset after the chain.
830  unsigned getNextSLocOffset() const {
831    return NextSLocOffset;
832  }
833
834  /// \brief Returns the number of identifiers found in the chain.
835  unsigned getTotalNumIdentifiers() const {
836    return static_cast<unsigned>(IdentifiersLoaded.size());
837  }
838
839  /// \brief Returns the number of types found in the chain.
840  unsigned getTotalNumTypes() const {
841    return static_cast<unsigned>(TypesLoaded.size());
842  }
843
844  /// \brief Returns the number of declarations found in the chain.
845  unsigned getTotalNumDecls() const {
846    return static_cast<unsigned>(DeclsLoaded.size());
847  }
848
849  /// \brief Returns the number of selectors found in the chain.
850  unsigned getTotalNumSelectors() const {
851    return static_cast<unsigned>(SelectorsLoaded.size());
852  }
853
854  /// \brief Returns the number of macro definitions found in the chain.
855  unsigned getTotalNumMacroDefinitions() const {
856    return static_cast<unsigned>(MacroDefinitionsLoaded.size());
857  }
858
859  /// \brief Reads a TemplateArgumentLocInfo appropriate for the
860  /// given TemplateArgument kind.
861  TemplateArgumentLocInfo
862  GetTemplateArgumentLocInfo(PerFileData &F, TemplateArgument::ArgKind Kind,
863                             const RecordData &Record, unsigned &Idx);
864
865  /// \brief Reads a TemplateArgumentLoc.
866  TemplateArgumentLoc
867  ReadTemplateArgumentLoc(PerFileData &F,
868                          const RecordData &Record, unsigned &Idx);
869
870  /// \brief Reads a declarator info from the given record.
871  TypeSourceInfo *GetTypeSourceInfo(PerFileData &F,
872                                    const RecordData &Record, unsigned &Idx);
873
874  /// \brief Resolve and return the translation unit declaration.
875  TranslationUnitDecl *GetTranslationUnitDecl();
876
877  /// \brief Resolve a type ID into a type, potentially building a new
878  /// type.
879  QualType GetType(serialization::TypeID ID);
880
881  /// \brief Returns the type ID associated with the given type.
882  /// If the type didn't come from the AST file the ID that is returned is
883  /// marked as "doesn't exist in AST".
884  serialization::TypeID GetTypeID(QualType T) const;
885
886  /// \brief Returns the type index associated with the given type.
887  /// If the type didn't come from the AST file the index that is returned is
888  /// marked as "doesn't exist in AST".
889  serialization::TypeIdx GetTypeIdx(QualType T) const;
890
891  /// \brief Resolve a declaration ID into a declaration, potentially
892  /// building a new declaration.
893  Decl *GetDecl(serialization::DeclID ID);
894  virtual Decl *GetExternalDecl(uint32_t ID);
895
896  /// \brief Resolve the offset of a statement into a statement.
897  ///
898  /// This operation will read a new statement from the external
899  /// source each time it is called, and is meant to be used via a
900  /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
901  virtual Stmt *GetExternalDeclStmt(uint64_t Offset);
902
903  /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
904  /// specified cursor.  Read the abbreviations that are at the top of the block
905  /// and then leave the cursor pointing into the block.
906  bool ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor, unsigned BlockID);
907
908  /// \brief Finds all the visible declarations with a given name.
909  /// The current implementation of this method just loads the entire
910  /// lookup table as unmaterialized references.
911  virtual DeclContext::lookup_result
912  FindExternalVisibleDeclsByName(const DeclContext *DC,
913                                 DeclarationName Name);
914
915  virtual void MaterializeVisibleDecls(const DeclContext *DC);
916
917  /// \brief Read all of the declarations lexically stored in a
918  /// declaration context.
919  ///
920  /// \param DC The declaration context whose declarations will be
921  /// read.
922  ///
923  /// \param Decls Vector that will contain the declarations loaded
924  /// from the external source. The caller is responsible for merging
925  /// these declarations with any declarations already stored in the
926  /// declaration context.
927  ///
928  /// \returns true if there was an error while reading the
929  /// declarations for this declaration context.
930  virtual bool FindExternalLexicalDecls(const DeclContext *DC,
931                                        llvm::SmallVectorImpl<Decl*> &Decls);
932
933  /// \brief Notify ASTReader that we started deserialization of
934  /// a decl or type so until FinishedDeserializing is called there may be
935  /// decls that are initializing. Must be paired with FinishedDeserializing.
936  virtual void StartedDeserializing() { ++NumCurrentElementsDeserializing; }
937
938  /// \brief Notify ASTReader that we finished the deserialization of
939  /// a decl or type. Must be paired with StartedDeserializing.
940  virtual void FinishedDeserializing();
941
942  /// \brief Function that will be invoked when we begin parsing a new
943  /// translation unit involving this external AST source.
944  ///
945  /// This function will provide all of the external definitions to
946  /// the ASTConsumer.
947  virtual void StartTranslationUnit(ASTConsumer *Consumer);
948
949  /// \brief Print some statistics about AST usage.
950  virtual void PrintStats();
951
952  /// \brief Initialize the semantic source with the Sema instance
953  /// being used to perform semantic analysis on the abstract syntax
954  /// tree.
955  virtual void InitializeSema(Sema &S);
956
957  /// \brief Inform the semantic consumer that Sema is no longer available.
958  virtual void ForgetSema() { SemaObj = 0; }
959
960  /// \brief Retrieve the IdentifierInfo for the named identifier.
961  ///
962  /// This routine builds a new IdentifierInfo for the given identifier. If any
963  /// declarations with this name are visible from translation unit scope, their
964  /// declarations will be deserialized and introduced into the declaration
965  /// chain of the identifier.
966  virtual IdentifierInfo *get(const char *NameStart, const char *NameEnd);
967  IdentifierInfo *get(llvm::StringRef Name) {
968    return get(Name.begin(), Name.end());
969  }
970
971  /// \brief Load the contents of the global method pool for a given
972  /// selector.
973  ///
974  /// \returns a pair of Objective-C methods lists containing the
975  /// instance and factory methods, respectively, with this selector.
976  virtual std::pair<ObjCMethodList, ObjCMethodList>
977    ReadMethodPool(Selector Sel);
978
979  /// \brief Load a selector from disk, registering its ID if it exists.
980  void LoadSelector(Selector Sel);
981
982  void SetIdentifierInfo(unsigned ID, IdentifierInfo *II);
983  void SetGloballyVisibleDecls(IdentifierInfo *II,
984                               const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
985                               bool Nonrecursive = false);
986
987  /// \brief Report a diagnostic.
988  DiagnosticBuilder Diag(unsigned DiagID);
989
990  /// \brief Report a diagnostic.
991  DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID);
992
993  IdentifierInfo *DecodeIdentifierInfo(unsigned Idx);
994
995  IdentifierInfo *GetIdentifierInfo(const RecordData &Record, unsigned &Idx) {
996    return DecodeIdentifierInfo(Record[Idx++]);
997  }
998
999  virtual IdentifierInfo *GetIdentifier(unsigned ID) {
1000    return DecodeIdentifierInfo(ID);
1001  }
1002
1003  /// \brief Read the source location entry with index ID.
1004  virtual void ReadSLocEntry(unsigned ID);
1005
1006  Selector DecodeSelector(unsigned Idx);
1007
1008  virtual Selector GetExternalSelector(uint32_t ID);
1009  uint32_t GetNumExternalSelectors();
1010
1011  Selector GetSelector(const RecordData &Record, unsigned &Idx) {
1012    return DecodeSelector(Record[Idx++]);
1013  }
1014
1015  /// \brief Read a declaration name.
1016  DeclarationName ReadDeclarationName(const RecordData &Record, unsigned &Idx);
1017
1018  NestedNameSpecifier *ReadNestedNameSpecifier(const RecordData &Record,
1019                                               unsigned &Idx);
1020
1021  /// \brief Read a template name.
1022  TemplateName ReadTemplateName(const RecordData &Record, unsigned &Idx);
1023
1024  /// \brief Read a template argument.
1025  TemplateArgument ReadTemplateArgument(PerFileData &F,
1026                                        const RecordData &Record,unsigned &Idx);
1027
1028  /// \brief Read a template parameter list.
1029  TemplateParameterList *ReadTemplateParameterList(PerFileData &F,
1030                                                   const RecordData &Record,
1031                                                   unsigned &Idx);
1032
1033  /// \brief Read a template argument array.
1034  void
1035  ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
1036                           PerFileData &F, const RecordData &Record,
1037                           unsigned &Idx);
1038
1039  /// \brief Read a UnresolvedSet structure.
1040  void ReadUnresolvedSet(UnresolvedSetImpl &Set,
1041                         const RecordData &Record, unsigned &Idx);
1042
1043  /// \brief Read a C++ base specifier.
1044  CXXBaseSpecifier ReadCXXBaseSpecifier(PerFileData &F,
1045                                        const RecordData &Record,unsigned &Idx);
1046
1047  /// \brief Read a CXXBaseOrMemberInitializer array.
1048  std::pair<CXXBaseOrMemberInitializer **, unsigned>
1049  ReadCXXBaseOrMemberInitializers(PerFileData &F,
1050                                  const RecordData &Record, unsigned &Idx);
1051
1052  /// \brief Read a source location from raw form.
1053  SourceLocation ReadSourceLocation(PerFileData &Module, unsigned Raw) {
1054    (void)Module; // No remapping yet
1055    return SourceLocation::getFromRawEncoding(Raw);
1056  }
1057
1058  /// \brief Read a source location.
1059  SourceLocation ReadSourceLocation(PerFileData &Module,
1060                                    const RecordData &Record, unsigned& Idx) {
1061    return ReadSourceLocation(Module, Record[Idx++]);
1062  }
1063
1064  /// \brief Read a source range.
1065  SourceRange ReadSourceRange(PerFileData &F,
1066                              const RecordData &Record, unsigned& Idx);
1067
1068  /// \brief Read an integral value
1069  llvm::APInt ReadAPInt(const RecordData &Record, unsigned &Idx);
1070
1071  /// \brief Read a signed integral value
1072  llvm::APSInt ReadAPSInt(const RecordData &Record, unsigned &Idx);
1073
1074  /// \brief Read a floating-point value
1075  llvm::APFloat ReadAPFloat(const RecordData &Record, unsigned &Idx);
1076
1077  // \brief Read a string
1078  std::string ReadString(const RecordData &Record, unsigned &Idx);
1079
1080  CXXTemporary *ReadCXXTemporary(const RecordData &Record, unsigned &Idx);
1081
1082  /// \brief Reads attributes from the current stream position.
1083  void ReadAttributes(PerFileData &F, AttrVec &Attrs);
1084
1085  /// \brief Reads a statement.
1086  Stmt *ReadStmt(PerFileData &F);
1087
1088  /// \brief Reads an expression.
1089  Expr *ReadExpr(PerFileData &F);
1090
1091  /// \brief Reads a sub-statement operand during statement reading.
1092  Stmt *ReadSubStmt() {
1093    assert(ReadingKind == Read_Stmt &&
1094           "Should be called only during statement reading!");
1095    // Subexpressions are stored from last to first, so the next Stmt we need
1096    // is at the back of the stack.
1097    assert(!StmtStack.empty() && "Read too many sub statements!");
1098    return StmtStack.pop_back_val();
1099  }
1100
1101  /// \brief Reads a sub-expression operand during statement reading.
1102  Expr *ReadSubExpr();
1103
1104  /// \brief Reads the macro record located at the given offset.
1105  void ReadMacroRecord(PerFileData &F, uint64_t Offset);
1106
1107  /// \brief Read the set of macros defined by this external macro source.
1108  virtual void ReadDefinedMacros();
1109
1110  /// \brief Retrieve the macro definition with the given ID.
1111  MacroDefinition *getMacroDefinition(serialization::MacroID ID);
1112
1113  /// \brief Retrieve the AST context that this AST reader supplements.
1114  ASTContext *getContext() { return Context; }
1115
1116  // \brief Contains declarations that were loaded before we have
1117  // access to a Sema object.
1118  llvm::SmallVector<NamedDecl *, 16> PreloadedDecls;
1119
1120  /// \brief Retrieve the semantic analysis object used to analyze the
1121  /// translation unit in which the precompiled header is being
1122  /// imported.
1123  Sema *getSema() { return SemaObj; }
1124
1125  /// \brief Retrieve the identifier table associated with the
1126  /// preprocessor.
1127  IdentifierTable &getIdentifierTable();
1128
1129  /// \brief Record that the given ID maps to the given switch-case
1130  /// statement.
1131  void RecordSwitchCaseID(SwitchCase *SC, unsigned ID);
1132
1133  /// \brief Retrieve the switch-case statement with the given ID.
1134  SwitchCase *getSwitchCaseWithID(unsigned ID);
1135
1136  /// \brief Record that the given label statement has been
1137  /// deserialized and has the given ID.
1138  void RecordLabelStmt(LabelStmt *S, unsigned ID);
1139
1140  /// \brief Set the label of the given statement to the label
1141  /// identified by ID.
1142  ///
1143  /// Depending on the order in which the label and other statements
1144  /// referencing that label occur, this operation may complete
1145  /// immediately (updating the statement) or it may queue the
1146  /// statement to be back-patched later.
1147  void SetLabelOf(GotoStmt *S, unsigned ID);
1148
1149  /// \brief Set the label of the given expression to the label
1150  /// identified by ID.
1151  ///
1152  /// Depending on the order in which the label and other statements
1153  /// referencing that label occur, this operation may complete
1154  /// immediately (updating the statement) or it may queue the
1155  /// statement to be back-patched later.
1156  void SetLabelOf(AddrLabelExpr *S, unsigned ID);
1157};
1158
1159/// \brief Helper class that saves the current stream position and
1160/// then restores it when destroyed.
1161struct SavedStreamPosition {
1162  explicit SavedStreamPosition(llvm::BitstreamCursor &Cursor)
1163  : Cursor(Cursor), Offset(Cursor.GetCurrentBitNo()) { }
1164
1165  ~SavedStreamPosition() {
1166    Cursor.JumpToBit(Offset);
1167  }
1168
1169private:
1170  llvm::BitstreamCursor &Cursor;
1171  uint64_t Offset;
1172};
1173
1174inline void PCHValidator::Error(const char *Msg) {
1175  Reader.Error(Msg);
1176}
1177
1178} // end namespace clang
1179
1180#endif
1181