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