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