ASTReader.h revision 72a9ae18553bf8b6bdad84d2c54f73741a47e275
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/Serialization/ContinuousRangeMap.h"
19#include "clang/Sema/ExternalSemaSource.h"
20#include "clang/AST/DeclarationName.h"
21#include "clang/AST/DeclObjC.h"
22#include "clang/AST/TemplateBase.h"
23#include "clang/Lex/ExternalPreprocessorSource.h"
24#include "clang/Lex/HeaderSearch.h"
25#include "clang/Lex/PreprocessingRecord.h"
26#include "clang/Basic/Diagnostic.h"
27#include "clang/Basic/IdentifierTable.h"
28#include "clang/Basic/SourceManager.h"
29#include "llvm/ADT/APFloat.h"
30#include "llvm/ADT/APInt.h"
31#include "llvm/ADT/APSInt.h"
32#include "llvm/ADT/OwningPtr.h"
33#include "llvm/ADT/SmallVector.h"
34#include "llvm/ADT/StringRef.h"
35#include "llvm/Bitcode/BitstreamReader.h"
36#include "llvm/Support/DataTypes.h"
37#include <deque>
38#include <map>
39#include <string>
40#include <utility>
41#include <vector>
42
43namespace llvm {
44  class MemoryBuffer;
45}
46
47namespace clang {
48
49class AddrLabelExpr;
50class ASTConsumer;
51class ASTContext;
52class ASTIdentifierIterator;
53class ASTUnit; // FIXME: Layering violation and egregious hack.
54class Attr;
55class Decl;
56class DeclContext;
57class NestedNameSpecifier;
58class CXXBaseSpecifier;
59class CXXConstructorDecl;
60class CXXCtorInitializer;
61class GotoStmt;
62class MacroDefinition;
63class NamedDecl;
64class OpaqueValueExpr;
65class Preprocessor;
66class Sema;
67class SwitchCase;
68class ASTDeserializationListener;
69class ASTWriter;
70class ASTReader;
71class ASTDeclReader;
72class ASTStmtReader;
73class ASTIdentifierLookupTrait;
74class TypeLocReader;
75struct HeaderFileInfo;
76class VersionTuple;
77
78struct PCHPredefinesBlock {
79  /// \brief The file ID for this predefines buffer in a PCH file.
80  FileID BufferID;
81
82  /// \brief This predefines buffer in a PCH file.
83  StringRef Data;
84};
85typedef SmallVector<PCHPredefinesBlock, 2> PCHPredefinesBlocks;
86
87/// \brief Abstract interface for callback invocations by the ASTReader.
88///
89/// While reading an AST file, the ASTReader will call the methods of the
90/// listener to pass on specific information. Some of the listener methods can
91/// return true to indicate to the ASTReader that the information (and
92/// consequently the AST file) is invalid.
93class ASTReaderListener {
94public:
95  virtual ~ASTReaderListener();
96
97  /// \brief Receives the language options.
98  ///
99  /// \returns true to indicate the options are invalid or false otherwise.
100  virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
101    return false;
102  }
103
104  /// \brief Receives the target triple.
105  ///
106  /// \returns true to indicate the target triple is invalid or false otherwise.
107  virtual bool ReadTargetTriple(StringRef Triple) {
108    return false;
109  }
110
111  /// \brief Receives the contents of the predefines buffer.
112  ///
113  /// \param Buffers Information about the predefines buffers.
114  ///
115  /// \param OriginalFileName The original file name for the AST file, which
116  /// will appear as an entry in the predefines buffer.
117  ///
118  /// \param SuggestedPredefines If necessary, additional definitions are added
119  /// here.
120  ///
121  /// \returns true to indicate the predefines are invalid or false otherwise.
122  virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
123                                    StringRef OriginalFileName,
124                                    std::string &SuggestedPredefines,
125                                    FileManager &FileMgr) {
126    return false;
127  }
128
129  /// \brief Receives a HeaderFileInfo entry.
130  virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) {}
131
132  /// \brief Receives __COUNTER__ value.
133  virtual void ReadCounter(unsigned Value) {}
134};
135
136/// \brief ASTReaderListener implementation to validate the information of
137/// the PCH file against an initialized Preprocessor.
138class PCHValidator : public ASTReaderListener {
139  Preprocessor &PP;
140  ASTReader &Reader;
141
142  unsigned NumHeaderInfos;
143
144public:
145  PCHValidator(Preprocessor &PP, ASTReader &Reader)
146    : PP(PP), Reader(Reader), NumHeaderInfos(0) {}
147
148  virtual bool ReadLanguageOptions(const LangOptions &LangOpts);
149  virtual bool ReadTargetTriple(StringRef Triple);
150  virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
151                                    StringRef OriginalFileName,
152                                    std::string &SuggestedPredefines,
153                                    FileManager &FileMgr);
154  virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID);
155  virtual void ReadCounter(unsigned Value);
156
157private:
158  void Error(const char *Msg);
159};
160
161namespace serialization {
162
163/// \brief Specifies the kind of module that has been loaded.
164enum ModuleKind {
165  MK_Module,   ///< File is a module proper.
166  MK_PCH,      ///< File is a PCH file treated as such.
167  MK_Preamble, ///< File is a PCH file treated as the preamble.
168  MK_MainFile  ///< File is a PCH file treated as the actual main file.
169};
170
171/// \brief Information about a module that has been loaded by the ASTReader.
172///
173/// Each instance of the Module class corresponds to a single AST file, which
174/// may be a precompiled header, precompiled preamble, or an AST file of some
175/// sort loaded as the main file, all of which are specific formulations of
176/// the general notion of a "module". A module may depend on another module
177/// (FIXME: or a set of other modules).
178class Module {
179public:
180  Module(ModuleKind Kind);
181  ~Module();
182
183  // === General information ===
184
185  /// \brief The type of this module.
186  ModuleKind Kind;
187
188  /// \brief The file name of the module file.
189  std::string FileName;
190
191  /// \brief The memory buffer that stores the data associated with
192  /// this AST file.
193  llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
194
195  /// \brief The size of this file, in bits.
196  uint64_t SizeInBits;
197
198  /// \brief The global bit offset (or base) of this module
199  uint64_t GlobalBitOffset;
200
201  /// \brief The bitstream reader from which we'll read the AST file.
202  llvm::BitstreamReader StreamFile;
203
204  /// \brief The main bitstream cursor for the main block.
205  llvm::BitstreamCursor Stream;
206
207  /// \brief The source location where this module was first imported.
208  SourceLocation ImportLoc;
209
210  /// \brief The first source location in this module.
211  SourceLocation FirstLoc;
212
213  // === Source Locations ===
214
215  /// \brief Cursor used to read source location entries.
216  llvm::BitstreamCursor SLocEntryCursor;
217
218  /// \brief The number of source location entries in this AST file.
219  unsigned LocalNumSLocEntries;
220
221  /// \brief The base ID in the source manager's view of this module.
222  int SLocEntryBaseID;
223
224  /// \brief The base offset in the source manager's view of this module.
225  unsigned SLocEntryBaseOffset;
226
227  /// \brief Offsets for all of the source location entries in the
228  /// AST file.
229  const uint32_t *SLocEntryOffsets;
230
231  /// \brief The number of source location file entries in this AST file.
232  unsigned LocalNumSLocFileEntries;
233
234  /// \brief Offsets for all of the source location file entries in the
235  /// AST file.
236  const uint32_t *SLocFileOffsets;
237
238  /// \brief Remapping table for source locations in this module.
239  ContinuousRangeMap<uint32_t, int, 2> SLocRemap;
240
241  // === Identifiers ===
242
243  /// \brief The number of identifiers in this AST file.
244  unsigned LocalNumIdentifiers;
245
246  /// \brief Offsets into the identifier table data.
247  ///
248  /// This array is indexed by the identifier ID (-1), and provides
249  /// the offset into IdentifierTableData where the string data is
250  /// stored.
251  const uint32_t *IdentifierOffsets;
252
253  /// \brief Actual data for the on-disk hash table of identifiers.
254  ///
255  /// This pointer points into a memory buffer, where the on-disk hash
256  /// table for identifiers actually lives.
257  const char *IdentifierTableData;
258
259  /// \brief A pointer to an on-disk hash table of opaque type
260  /// IdentifierHashTable.
261  void *IdentifierLookupTable;
262
263  // === Macros ===
264
265  /// \brief The cursor to the start of the preprocessor block, which stores
266  /// all of the macro definitions.
267  llvm::BitstreamCursor MacroCursor;
268
269  /// \brief The offset of the start of the set of defined macros.
270  uint64_t MacroStartOffset;
271
272  // === Detailed PreprocessingRecord ===
273
274  /// \brief The cursor to the start of the (optional) detailed preprocessing
275  /// record block.
276  llvm::BitstreamCursor PreprocessorDetailCursor;
277
278  /// \brief The offset of the start of the preprocessor detail cursor.
279  uint64_t PreprocessorDetailStartOffset;
280
281  /// \brief The number of macro definitions in this file.
282  unsigned LocalNumMacroDefinitions;
283
284  /// \brief Offsets of all of the macro definitions in the preprocessing
285  /// record in the AST file.
286  const uint32_t *MacroDefinitionOffsets;
287
288  // === Header search information ===
289
290  /// \brief The number of local HeaderFileInfo structures.
291  unsigned LocalNumHeaderFileInfos;
292
293  /// \brief Actual data for the on-disk hash table of header file
294  /// information.
295  ///
296  /// This pointer points into a memory buffer, where the on-disk hash
297  /// table for header file information actually lives.
298  const char *HeaderFileInfoTableData;
299
300  /// \brief The on-disk hash table that contains information about each of
301  /// the header files.
302  void *HeaderFileInfoTable;
303
304  // === Selectors ===
305
306  /// \brief The number of selectors new to this file.
307  ///
308  /// This is the number of entries in SelectorOffsets.
309  unsigned LocalNumSelectors;
310
311  /// \brief Offsets into the selector lookup table's data array
312  /// where each selector resides.
313  const uint32_t *SelectorOffsets;
314
315  /// \brief A pointer to the character data that comprises the selector table
316  ///
317  /// The SelectorOffsets table refers into this memory.
318  const unsigned char *SelectorLookupTableData;
319
320  /// \brief A pointer to an on-disk hash table of opaque type
321  /// ASTSelectorLookupTable.
322  ///
323  /// This hash table provides the IDs of all selectors, and the associated
324  /// instance and factory methods.
325  void *SelectorLookupTable;
326
327  /// \brief Method selectors used in a @selector expression. Used for
328  /// implementation of -Wselector.
329  SmallVector<uint64_t, 64> ReferencedSelectorsData;
330
331  // === Declarations ===
332
333  /// DeclsCursor - This is a cursor to the start of the DECLS_BLOCK block. It
334  /// has read all the abbreviations at the start of the block and is ready to
335  /// jump around with these in context.
336  llvm::BitstreamCursor DeclsCursor;
337
338  /// \brief The number of declarations in this AST file.
339  unsigned LocalNumDecls;
340
341  /// \brief Offset of each declaration within the bitstream, indexed
342  /// by the declaration ID (-1).
343  const uint32_t *DeclOffsets;
344
345  /// \brief A snapshot of the pending instantiations in the chain.
346  ///
347  /// This record tracks the instantiations that Sema has to perform at the
348  /// end of the TU. It consists of a pair of values for every pending
349  /// instantiation where the first value is the ID of the decl and the second
350  /// is the instantiation location.
351  SmallVector<uint64_t, 64> PendingInstantiations;
352
353  /// \brief The number of C++ base specifier sets in this AST file.
354  unsigned LocalNumCXXBaseSpecifiers;
355
356  /// \brief Offset of each C++ base specifier set within the bitstream,
357  /// indexed by the C++ base specifier set ID (-1).
358  const uint32_t *CXXBaseSpecifiersOffsets;
359
360  // === Types ===
361
362  /// \brief The number of types in this AST file.
363  unsigned LocalNumTypes;
364
365  /// \brief Offset of each type within the bitstream, indexed by the
366  /// type ID, or the representation of a Type*.
367  const uint32_t *TypeOffsets;
368
369  // === Miscellaneous ===
370
371  /// \brief Diagnostic IDs and their mappings that the user changed.
372  SmallVector<uint64_t, 8> PragmaDiagMappings;
373
374  /// \brief The AST stat cache installed for this file, if any.
375  ///
376  /// The dynamic type of this stat cache is always ASTStatCache
377  void *StatCache;
378
379  /// \brief The number of preallocated preprocessing entities in the
380  /// preprocessing record.
381  unsigned NumPreallocatedPreprocessingEntities;
382
383  /// \brief The next module in source order.
384  Module *NextInSource;
385
386  /// \brief All the modules that loaded this one. Can contain NULL for
387  /// directly loaded modules.
388  SmallVector<Module *, 1> Loaders;
389};
390
391} // end namespace serialization
392
393/// \brief Reads an AST files chain containing the contents of a translation
394/// unit.
395///
396/// The ASTReader class reads bitstreams (produced by the ASTWriter
397/// class) containing the serialized representation of a given
398/// abstract syntax tree and its supporting data structures. An
399/// instance of the ASTReader can be attached to an ASTContext object,
400/// which will provide access to the contents of the AST files.
401///
402/// The AST reader provides lazy de-serialization of declarations, as
403/// required when traversing the AST. Only those AST nodes that are
404/// actually required will be de-serialized.
405class ASTReader
406  : public ExternalPreprocessorSource,
407    public ExternalPreprocessingRecordSource,
408    public ExternalHeaderFileInfoSource,
409    public ExternalSemaSource,
410    public IdentifierInfoLookup,
411    public ExternalIdentifierLookup,
412    public ExternalSLocEntrySource
413{
414public:
415  enum ASTReadResult { Success, Failure, IgnorePCH };
416  /// \brief Types of AST files.
417  friend class PCHValidator;
418  friend class ASTDeclReader;
419  friend class ASTStmtReader;
420  friend class ASTIdentifierIterator;
421  friend class ASTIdentifierLookupTrait;
422  friend class TypeLocReader;
423  friend class ASTWriter;
424  friend class ASTUnit; // ASTUnit needs to remap source locations.
425
426  typedef serialization::Module Module;
427  typedef serialization::ModuleKind ModuleKind;
428
429private:
430  /// \brief The receiver of some callbacks invoked by ASTReader.
431  llvm::OwningPtr<ASTReaderListener> Listener;
432
433  /// \brief The receiver of deserialization events.
434  ASTDeserializationListener *DeserializationListener;
435
436  SourceManager &SourceMgr;
437  FileManager &FileMgr;
438  Diagnostic &Diags;
439
440  /// \brief The semantic analysis object that will be processing the
441  /// AST files and the translation unit that uses it.
442  Sema *SemaObj;
443
444  /// \brief The preprocessor that will be loading the source file.
445  Preprocessor *PP;
446
447  /// \brief The AST context into which we'll read the AST files.
448  ASTContext *Context;
449
450  /// \brief The AST consumer.
451  ASTConsumer *Consumer;
452
453  /// \brief AST buffers for chained PCHs created and stored in memory.
454  /// First (not depending on another) PCH in chain is in front.
455  std::vector<llvm::MemoryBuffer *> ASTBuffers;
456
457  /// \brief All loaded modules, indexed by name.
458  llvm::StringMap<Module*> Modules;
459
460  /// \brief The first module in source order.
461  Module *FirstInSource;
462
463  /// \brief The chain of AST files. The first entry is the one named by the
464  /// user, the last one is the one that doesn't depend on anything further.
465  /// That is, the entry I was created with -include-pch I+1.
466  SmallVector<Module*, 2> Chain;
467
468  /// \brief A map of global bit offsets to the module that stores entities
469  /// at those bit offsets.
470  ContinuousRangeMap<uint64_t, Module*, 4> GlobalBitOffsetsMap;
471
472  /// \brief SLocEntries that we're going to preload.
473  SmallVector<int, 64> PreloadSLocEntries;
474
475  /// \brief A map of negated SLocEntryIDs to the modules containing them.
476  ContinuousRangeMap<unsigned, Module*, 64> GlobalSLocEntryMap;
477
478  /// \brief Types that have already been loaded from the chain.
479  ///
480  /// When the pointer at index I is non-NULL, the type with
481  /// ID = (I + 1) << FastQual::Width has already been loaded
482  std::vector<QualType> TypesLoaded;
483
484  typedef ContinuousRangeMap<serialization::TypeID,
485      std::pair<Module *, int32_t>, 4>
486    GlobalTypeMapType;
487
488  /// \brief Mapping from global type IDs to the module in which the
489  /// type resides along with the offset that should be added to the
490  /// global type ID to produce a local ID.
491  GlobalTypeMapType GlobalTypeMap;
492
493  /// \brief Map that provides the ID numbers of each type within the
494  /// output stream, plus those deserialized from a chained PCH.
495  ///
496  /// The ID numbers of types are consecutive (in order of discovery)
497  /// and start at 1. 0 is reserved for NULL. When types are actually
498  /// stored in the stream, the ID number is shifted by 2 bits to
499  /// allow for the const/volatile qualifiers.
500  ///
501  /// Keys in the map never have const/volatile qualifiers.
502  serialization::TypeIdxMap TypeIdxs;
503
504  /// \brief Declarations that have already been loaded from the chain.
505  ///
506  /// When the pointer at index I is non-NULL, the declaration with ID
507  /// = I + 1 has already been loaded.
508  std::vector<Decl *> DeclsLoaded;
509
510  typedef ContinuousRangeMap<serialization::DeclID,
511                             std::pair<Module *, int32_t>, 4>
512    GlobalDeclMapType;
513
514  /// \brief Mapping from global declaration IDs to the module in which the
515  /// declaration resides along with the offset that should be added to the
516  /// global declaration ID to produce a local ID.
517  GlobalDeclMapType GlobalDeclMap;
518
519  typedef std::pair<Module *, uint64_t> FileOffset;
520  typedef SmallVector<FileOffset, 2> FileOffsetsTy;
521  typedef llvm::DenseMap<serialization::DeclID, FileOffsetsTy>
522      DeclUpdateOffsetsMap;
523
524  /// \brief Declarations that have modifications residing in a later file
525  /// in the chain.
526  DeclUpdateOffsetsMap DeclUpdateOffsets;
527
528  typedef llvm::DenseMap<serialization::DeclID,
529                         std::pair<Module *, uint64_t> >
530      DeclReplacementMap;
531  /// \brief Declarations that have been replaced in a later file in the chain.
532  DeclReplacementMap ReplacedDecls;
533
534  /// \brief Information about the contents of a DeclContext.
535  struct DeclContextInfo {
536    Module *F;
537    void *NameLookupTableData; // a ASTDeclContextNameLookupTable.
538    const serialization::KindDeclIDPair *LexicalDecls;
539    unsigned NumLexicalDecls;
540  };
541  // In a full chain, there could be multiple updates to every decl context,
542  // so this is a vector. However, typically a chain is only two elements long,
543  // with only one file containing updates, so there will be only one update
544  // per decl context.
545  typedef SmallVector<DeclContextInfo, 1> DeclContextInfos;
546  typedef llvm::DenseMap<const DeclContext *, DeclContextInfos>
547      DeclContextOffsetsMap;
548  // Updates for visible decls can occur for other contexts than just the
549  // TU, and when we read those update records, the actual context will not
550  // be available yet (unless it's the TU), so have this pending map using the
551  // ID as a key. It will be realized when the context is actually loaded.
552  typedef SmallVector<void *, 1> DeclContextVisibleUpdates;
553  typedef llvm::DenseMap<serialization::DeclID, DeclContextVisibleUpdates>
554      DeclContextVisibleUpdatesPending;
555
556  /// \brief Offsets of the lexical and visible declarations for each
557  /// DeclContext.
558  DeclContextOffsetsMap DeclContextOffsets;
559
560  /// \brief Updates to the visible declarations of declaration contexts that
561  /// haven't been loaded yet.
562  DeclContextVisibleUpdatesPending PendingVisibleUpdates;
563
564  typedef SmallVector<CXXRecordDecl *, 4> ForwardRefs;
565  typedef llvm::DenseMap<const CXXRecordDecl *, ForwardRefs>
566      PendingForwardRefsMap;
567  /// \brief Forward references that have a definition but the definition decl
568  /// is still initializing. When the definition gets read it will update
569  /// the DefinitionData pointer of all pending references.
570  PendingForwardRefsMap PendingForwardRefs;
571
572  typedef llvm::DenseMap<serialization::DeclID, serialization::DeclID>
573      FirstLatestDeclIDMap;
574  /// \brief Map of first declarations from a chained PCH that point to the
575  /// most recent declarations in another AST file.
576  FirstLatestDeclIDMap FirstLatestDeclIDs;
577
578  /// \brief Read the records that describe the contents of declcontexts.
579  bool ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
580                              const std::pair<uint64_t, uint64_t> &Offsets,
581                              DeclContextInfo &Info);
582
583  /// \brief A vector containing identifiers that have already been
584  /// loaded.
585  ///
586  /// If the pointer at index I is non-NULL, then it refers to the
587  /// IdentifierInfo for the identifier with ID=I+1 that has already
588  /// been loaded.
589  std::vector<IdentifierInfo *> IdentifiersLoaded;
590
591  typedef ContinuousRangeMap<serialization::IdentID,
592                             std::pair<Module *, int32_t>, 4>
593    GlobalIdentifierMapType;
594
595  /// \brief Mapping from global identifer IDs to the module in which the
596  /// identifier resides along with the offset that should be added to the
597  /// global identifier ID to produce a local ID.
598  GlobalIdentifierMapType GlobalIdentifierMap;
599
600  /// \brief A vector containing selectors that have already been loaded.
601  ///
602  /// This vector is indexed by the Selector ID (-1). NULL selector
603  /// entries indicate that the particular selector ID has not yet
604  /// been loaded.
605  SmallVector<Selector, 16> SelectorsLoaded;
606
607  typedef ContinuousRangeMap<serialization::SelectorID,
608                             std::pair<Module *, int32_t>, 4>
609    GlobalSelectorMapType;
610
611  /// \brief Mapping from global selector IDs to the module in which the
612  /// selector resides along with the offset that should be added to the
613  /// global selector ID to produce a local ID.
614  GlobalSelectorMapType GlobalSelectorMap;
615
616  /// \brief The macro definitions we have already loaded.
617  SmallVector<MacroDefinition *, 16> MacroDefinitionsLoaded;
618
619  typedef ContinuousRangeMap<serialization::MacroID,
620                             std::pair<Module *, int32_t>, 4>
621    GlobalMacroDefinitionMapType;
622
623  /// \brief Mapping from global macro definition IDs to the module in which the
624  /// selector resides along with the offset that should be added to the
625  /// global selector ID to produce a local ID.
626  GlobalMacroDefinitionMapType GlobalMacroDefinitionMap;
627
628  /// \brief Mapping from identifiers that represent macros whose definitions
629  /// have not yet been deserialized to the global offset where the macro
630  /// record resides.
631  llvm::DenseMap<IdentifierInfo *, uint64_t> UnreadMacroRecordOffsets;
632
633  typedef ContinuousRangeMap<unsigned, std::pair<Module *, int>, 4>
634    GlobalPreprocessedEntityMapType;
635
636  /// \brief Mapping from global preprocessing entity IDs to the module in
637  /// which the preprocessed entity resides along with the offset that should be
638  /// added to the global preprocessing entitiy ID to produce a local ID.
639  GlobalPreprocessedEntityMapType GlobalPreprocessedEntityMap;
640
641  typedef ContinuousRangeMap<serialization::CXXBaseSpecifiersID,
642                             std::pair<Module *, int32_t>, 4>
643    GlobalCXXBaseSpecifiersMapType;
644
645  /// \brief Mapping from global CXX base specifier IDs to the module in which the
646  /// CXX base specifier resides along with the offset that should be added to the
647  /// global CXX base specifer ID to produce a local ID.
648  GlobalCXXBaseSpecifiersMapType GlobalCXXBaseSpecifiersMap;
649
650  /// \name CodeGen-relevant special data
651  /// \brief Fields containing data that is relevant to CodeGen.
652  //@{
653
654  /// \brief The IDs of all declarations that fulfill the criteria of
655  /// "interesting" decls.
656  ///
657  /// This contains the data loaded from all EXTERNAL_DEFINITIONS blocks in the
658  /// chain. The referenced declarations are deserialized and passed to the
659  /// consumer eagerly.
660  SmallVector<uint64_t, 16> ExternalDefinitions;
661
662  /// \brief The IDs of all tentative definitions stored in the the chain.
663  ///
664  /// Sema keeps track of all tentative definitions in a TU because it has to
665  /// complete them and pass them on to CodeGen. Thus, tentative definitions in
666  /// the PCH chain must be eagerly deserialized.
667  SmallVector<uint64_t, 16> TentativeDefinitions;
668
669  /// \brief The IDs of all CXXRecordDecls stored in the chain whose VTables are
670  /// used.
671  ///
672  /// CodeGen has to emit VTables for these records, so they have to be eagerly
673  /// deserialized.
674  SmallVector<uint64_t, 64> VTableUses;
675
676  //@}
677
678  /// \name Diagnostic-relevant special data
679  /// \brief Fields containing data that is used for generating diagnostics
680  //@{
681
682  /// \brief A snapshot of Sema's unused file-scoped variable tracking, for
683  /// generating warnings.
684  SmallVector<uint64_t, 16> UnusedFileScopedDecls;
685
686  /// \brief A list of all the delegating constructors we've seen, to diagnose
687  /// cycles.
688  SmallVector<uint64_t, 4> DelegatingCtorDecls;
689
690  /// \brief A snapshot of Sema's weak undeclared identifier tracking, for
691  /// generating warnings.
692  SmallVector<uint64_t, 64> WeakUndeclaredIdentifiers;
693
694  /// \brief The IDs of type aliases for ext_vectors that exist in the chain.
695  ///
696  /// Used by Sema for finding sugared names for ext_vectors in diagnostics.
697  SmallVector<uint64_t, 4> ExtVectorDecls;
698
699  //@}
700
701  /// \name Sema-relevant special data
702  /// \brief Fields containing data that is used for semantic analysis
703  //@{
704
705  /// \brief The IDs of all locally scoped external decls in the chain.
706  ///
707  /// Sema tracks these to validate that the types are consistent across all
708  /// local external declarations.
709  SmallVector<uint64_t, 16> LocallyScopedExternalDecls;
710
711  /// \brief The IDs of all dynamic class declarations in the chain.
712  ///
713  /// Sema tracks these because it checks for the key functions being defined
714  /// at the end of the TU, in which case it directs CodeGen to emit the VTable.
715  SmallVector<uint64_t, 16> DynamicClasses;
716
717  /// \brief The IDs of the declarations Sema stores directly.
718  ///
719  /// Sema tracks a few important decls, such as namespace std, directly.
720  SmallVector<uint64_t, 4> SemaDeclRefs;
721
722  /// \brief The IDs of the types ASTContext stores directly.
723  ///
724  /// The AST context tracks a few important types, such as va_list, directly.
725  SmallVector<uint64_t, 16> SpecialTypes;
726
727  /// \brief The IDs of CUDA-specific declarations ASTContext stores directly.
728  ///
729  /// The AST context tracks a few important decls, currently cudaConfigureCall,
730  /// directly.
731  SmallVector<uint64_t, 2> CUDASpecialDeclRefs;
732
733  /// \brief The floating point pragma option settings.
734  SmallVector<uint64_t, 1> FPPragmaOptions;
735
736  /// \brief The OpenCL extension settings.
737  SmallVector<uint64_t, 1> OpenCLExtensions;
738
739  /// \brief A list of the namespaces we've seen.
740  SmallVector<uint64_t, 4> KnownNamespaces;
741
742  //@}
743
744  /// \brief The original file name that was used to build the primary AST file,
745  /// which may have been modified for relocatable-pch support.
746  std::string OriginalFileName;
747
748  /// \brief The actual original file name that was used to build the primary
749  /// AST file.
750  std::string ActualOriginalFileName;
751
752  /// \brief The file ID for the original file that was used to build the
753  /// primary AST file.
754  FileID OriginalFileID;
755
756  /// \brief The directory that the PCH was originally created in. Used to
757  /// allow resolving headers even after headers+PCH was moved to a new path.
758  std::string OriginalDir;
759
760  /// \brief The directory that the PCH we are reading is stored in.
761  std::string CurrentDir;
762
763  /// \brief Whether this precompiled header is a relocatable PCH file.
764  bool RelocatablePCH;
765
766  /// \brief The system include root to be used when loading the
767  /// precompiled header.
768  const char *isysroot;
769
770  /// \brief Whether to disable the normal validation performed on precompiled
771  /// headers when they are loaded.
772  bool DisableValidation;
773
774  /// \brief Whether to disable the use of stat caches in AST files.
775  bool DisableStatCache;
776
777  /// \brief Mapping from switch-case IDs in the chain to switch-case statements
778  ///
779  /// Statements usually don't have IDs, but switch cases need them, so that the
780  /// switch statement can refer to them.
781  std::map<unsigned, SwitchCase *> SwitchCaseStmts;
782
783  /// \brief Mapping from opaque value IDs to OpaqueValueExprs.
784  std::map<unsigned, OpaqueValueExpr*> OpaqueValueExprs;
785
786  /// \brief The number of stat() calls that hit/missed the stat
787  /// cache.
788  unsigned NumStatHits, NumStatMisses;
789
790  /// \brief The number of source location entries de-serialized from
791  /// the PCH file.
792  unsigned NumSLocEntriesRead;
793
794  /// \brief The number of source location entries in the chain.
795  unsigned TotalNumSLocEntries;
796
797  /// \brief The number of statements (and expressions) de-serialized
798  /// from the chain.
799  unsigned NumStatementsRead;
800
801  /// \brief The total number of statements (and expressions) stored
802  /// in the chain.
803  unsigned TotalNumStatements;
804
805  /// \brief The number of macros de-serialized from the chain.
806  unsigned NumMacrosRead;
807
808  /// \brief The total number of macros stored in the chain.
809  unsigned TotalNumMacros;
810
811  /// \brief The number of selectors that have been read.
812  unsigned NumSelectorsRead;
813
814  /// \brief The number of method pool entries that have been read.
815  unsigned NumMethodPoolEntriesRead;
816
817  /// \brief The number of times we have looked up a selector in the method
818  /// pool and not found anything interesting.
819  unsigned NumMethodPoolMisses;
820
821  /// \brief The total number of method pool entries in the selector table.
822  unsigned TotalNumMethodPoolEntries;
823
824  /// Number of lexical decl contexts read/total.
825  unsigned NumLexicalDeclContextsRead, TotalLexicalDeclContexts;
826
827  /// Number of visible decl contexts read/total.
828  unsigned NumVisibleDeclContextsRead, TotalVisibleDeclContexts;
829
830  /// Total size of modules, in bits, currently loaded
831  uint64_t TotalModulesSizeInBits;
832
833  /// \brief Number of Decl/types that are currently deserializing.
834  unsigned NumCurrentElementsDeserializing;
835
836  /// Number of CXX base specifiers currently loaded
837  unsigned NumCXXBaseSpecifiersLoaded;
838
839  /// \brief An IdentifierInfo that has been loaded but whose top-level
840  /// declarations of the same name have not (yet) been loaded.
841  struct PendingIdentifierInfo {
842    IdentifierInfo *II;
843    SmallVector<uint32_t, 4> DeclIDs;
844  };
845
846  /// \brief The set of identifiers that were read while the AST reader was
847  /// (recursively) loading declarations.
848  ///
849  /// The declarations on the identifier chain for these identifiers will be
850  /// loaded once the recursive loading has completed.
851  std::deque<PendingIdentifierInfo> PendingIdentifierInfos;
852
853  /// \brief Contains declarations and definitions that will be
854  /// "interesting" to the ASTConsumer, when we get that AST consumer.
855  ///
856  /// "Interesting" declarations are those that have data that may
857  /// need to be emitted, such as inline function definitions or
858  /// Objective-C protocols.
859  std::deque<Decl *> InterestingDecls;
860
861  /// \brief We delay loading of the previous declaration chain to avoid
862  /// deeply nested calls when there are many redeclarations.
863  std::deque<std::pair<Decl *, serialization::DeclID> > PendingPreviousDecls;
864
865  /// \brief Ready to load the previous declaration of the given Decl.
866  void loadAndAttachPreviousDecl(Decl *D, serialization::DeclID ID);
867
868  /// \brief When reading a Stmt tree, Stmt operands are placed in this stack.
869  SmallVector<Stmt *, 16> StmtStack;
870
871  /// \brief What kind of records we are reading.
872  enum ReadingKind {
873    Read_Decl, Read_Type, Read_Stmt
874  };
875
876  /// \brief What kind of records we are reading.
877  ReadingKind ReadingKind;
878
879  /// \brief RAII object to change the reading kind.
880  class ReadingKindTracker {
881    ASTReader &Reader;
882    enum ReadingKind PrevKind;
883
884    ReadingKindTracker(const ReadingKindTracker&); // do not implement
885    ReadingKindTracker &operator=(const ReadingKindTracker&);// do not implement
886
887  public:
888    ReadingKindTracker(enum ReadingKind newKind, ASTReader &reader)
889      : Reader(reader), PrevKind(Reader.ReadingKind) {
890      Reader.ReadingKind = newKind;
891    }
892
893    ~ReadingKindTracker() { Reader.ReadingKind = PrevKind; }
894  };
895
896  /// \brief All predefines buffers in the chain, to be treated as if
897  /// concatenated.
898  PCHPredefinesBlocks PCHPredefinesBuffers;
899
900  /// \brief Suggested contents of the predefines buffer, after this
901  /// PCH file has been processed.
902  ///
903  /// In most cases, this string will be empty, because the predefines
904  /// buffer computed to build the PCH file will be identical to the
905  /// predefines buffer computed from the command line. However, when
906  /// there are differences that the PCH reader can work around, this
907  /// predefines buffer may contain additional definitions.
908  std::string SuggestedPredefines;
909
910  /// \brief Reads a statement from the specified cursor.
911  Stmt *ReadStmtFromStream(Module &F);
912
913  /// \brief Get a FileEntry out of stored-in-PCH filename, making sure we take
914  /// into account all the necessary relocations.
915  const FileEntry *getFileEntry(StringRef filename);
916
917  void MaybeAddSystemRootToFilename(std::string &Filename);
918
919  ASTReadResult ReadASTCore(StringRef FileName, ModuleKind Type);
920  ASTReadResult ReadASTBlock(Module &F);
921  bool CheckPredefinesBuffers();
922  bool ParseLineTable(Module &F, SmallVectorImpl<uint64_t> &Record);
923  ASTReadResult ReadSourceManagerBlock(Module &F);
924  ASTReadResult ReadSLocEntryRecord(int ID);
925  llvm::BitstreamCursor &SLocCursorForID(int ID);
926  SourceLocation getImportLocation(Module *F);
927  bool ParseLanguageOptions(const SmallVectorImpl<uint64_t> &Record);
928
929  struct RecordLocation {
930    RecordLocation(Module *M, uint64_t O)
931      : F(M), Offset(O) {}
932    Module *F;
933    uint64_t Offset;
934  };
935
936  QualType readTypeRecord(unsigned Index);
937  RecordLocation TypeCursorForIndex(unsigned Index);
938  void LoadedDecl(unsigned Index, Decl *D);
939  Decl *ReadDeclRecord(unsigned Index, serialization::DeclID ID);
940  RecordLocation DeclCursorForIndex(unsigned Index, serialization::DeclID ID);
941  RecordLocation getLocalBitOffset(uint64_t GlobalOffset);
942
943  void PassInterestingDeclsToConsumer();
944
945  /// \brief Produce an error diagnostic and return true.
946  ///
947  /// This routine should only be used for fatal errors that have to
948  /// do with non-routine failures (e.g., corrupted AST file).
949  void Error(StringRef Msg);
950  void Error(unsigned DiagID, StringRef Arg1 = StringRef(),
951             StringRef Arg2 = StringRef());
952
953  ASTReader(const ASTReader&); // do not implement
954  ASTReader &operator=(const ASTReader &); // do not implement
955public:
956  typedef SmallVector<uint64_t, 64> RecordData;
957
958  /// \brief Load the AST file and validate its contents against the given
959  /// Preprocessor.
960  ///
961  /// \param PP the preprocessor associated with the context in which this
962  /// precompiled header will be loaded.
963  ///
964  /// \param Context the AST context that this precompiled header will be
965  /// loaded into.
966  ///
967  /// \param isysroot If non-NULL, the system include path specified by the
968  /// user. This is only used with relocatable PCH files. If non-NULL,
969  /// a relocatable PCH file will use the default path "/".
970  ///
971  /// \param DisableValidation If true, the AST reader will suppress most
972  /// of its regular consistency checking, allowing the use of precompiled
973  /// headers that cannot be determined to be compatible.
974  ///
975  /// \param DisableStatCache If true, the AST reader will ignore the
976  /// stat cache in the AST files. This performance pessimization can
977  /// help when an AST file is being used in cases where the
978  /// underlying files in the file system may have changed, but
979  /// parsing should still continue.
980  ASTReader(Preprocessor &PP, ASTContext *Context, const char *isysroot = 0,
981            bool DisableValidation = false, bool DisableStatCache = false);
982
983  /// \brief Load the AST file without using any pre-initialized Preprocessor.
984  ///
985  /// The necessary information to initialize a Preprocessor later can be
986  /// obtained by setting a ASTReaderListener.
987  ///
988  /// \param SourceMgr the source manager into which the AST file will be loaded
989  ///
990  /// \param FileMgr the file manager into which the AST file will be loaded.
991  ///
992  /// \param Diags the diagnostics system to use for reporting errors and
993  /// warnings relevant to loading the AST file.
994  ///
995  /// \param isysroot If non-NULL, the system include path specified by the
996  /// user. This is only used with relocatable PCH files. If non-NULL,
997  /// a relocatable PCH file will use the default path "/".
998  ///
999  /// \param DisableValidation If true, the AST reader will suppress most
1000  /// of its regular consistency checking, allowing the use of precompiled
1001  /// headers that cannot be determined to be compatible.
1002  ///
1003  /// \param DisableStatCache If true, the AST reader will ignore the
1004  /// stat cache in the AST files. This performance pessimization can
1005  /// help when an AST file is being used in cases where the
1006  /// underlying files in the file system may have changed, but
1007  /// parsing should still continue.
1008  ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
1009            Diagnostic &Diags, const char *isysroot = 0,
1010            bool DisableValidation = false, bool DisableStatCache = false);
1011  ~ASTReader();
1012
1013  /// \brief Load the precompiled header designated by the given file
1014  /// name.
1015  ASTReadResult ReadAST(const std::string &FileName, ModuleKind Type);
1016
1017  /// \brief Checks that no file that is stored in PCH is out-of-sync with
1018  /// the actual file in the file system.
1019  ASTReadResult validateFileEntries();
1020
1021  /// \brief Set the AST callbacks listener.
1022  void setListener(ASTReaderListener *listener) {
1023    Listener.reset(listener);
1024  }
1025
1026  /// \brief Set the AST deserialization listener.
1027  void setDeserializationListener(ASTDeserializationListener *Listener);
1028
1029  /// \brief Set the Preprocessor to use.
1030  void setPreprocessor(Preprocessor &pp);
1031
1032  /// \brief Sets and initializes the given Context.
1033  void InitializeContext(ASTContext &Context);
1034
1035  /// \brief Set AST buffers for chained PCHs created and stored in memory.
1036  /// First (not depending on another) PCH in chain is first in array.
1037  void setASTMemoryBuffers(llvm::MemoryBuffer **bufs, unsigned numBufs) {
1038    ASTBuffers.clear();
1039    ASTBuffers.insert(ASTBuffers.begin(), bufs, bufs + numBufs);
1040  }
1041
1042  /// \brief Retrieve the name of the named (primary) AST file
1043  const std::string &getFileName() const { return Chain[0]->FileName; }
1044
1045  /// \brief Retrieve the name of the original source file name
1046  const std::string &getOriginalSourceFile() { return OriginalFileName; }
1047
1048  /// \brief Retrieve the name of the original source file name directly from
1049  /// the AST file, without actually loading the AST file.
1050  static std::string getOriginalSourceFile(const std::string &ASTFileName,
1051                                           FileManager &FileMgr,
1052                                           Diagnostic &Diags);
1053
1054  /// \brief Returns the suggested contents of the predefines buffer,
1055  /// which contains a (typically-empty) subset of the predefines
1056  /// build prior to including the precompiled header.
1057  const std::string &getSuggestedPredefines() { return SuggestedPredefines; }
1058
1059  /// \brief Read preprocessed entities into the preprocessing record.
1060  virtual void ReadPreprocessedEntities();
1061
1062  /// \brief Read the preprocessed entity at the given offset.
1063  virtual PreprocessedEntity *ReadPreprocessedEntityAtOffset(uint64_t Offset);
1064
1065  /// \brief Read the header file information for the given file entry.
1066  virtual HeaderFileInfo GetHeaderFileInfo(const FileEntry *FE);
1067
1068  void ReadPragmaDiagnosticMappings(Diagnostic &Diag);
1069
1070  /// \brief Returns the number of source locations found in the chain.
1071  unsigned getTotalNumSLocs() const {
1072    return TotalNumSLocEntries;
1073  }
1074
1075  /// \brief Returns the number of identifiers found in the chain.
1076  unsigned getTotalNumIdentifiers() const {
1077    return static_cast<unsigned>(IdentifiersLoaded.size());
1078  }
1079
1080  /// \brief Returns the number of types found in the chain.
1081  unsigned getTotalNumTypes() const {
1082    return static_cast<unsigned>(TypesLoaded.size());
1083  }
1084
1085  /// \brief Returns the number of declarations found in the chain.
1086  unsigned getTotalNumDecls() const {
1087    return static_cast<unsigned>(DeclsLoaded.size());
1088  }
1089
1090  /// \brief Returns the number of selectors found in the chain.
1091  unsigned getTotalNumSelectors() const {
1092    return static_cast<unsigned>(SelectorsLoaded.size());
1093  }
1094
1095  /// \brief Returns the number of preprocessed entities known to the AST
1096  /// reader.
1097  unsigned getTotalNumPreprocessedEntities() const {
1098    unsigned Result = 0;
1099    for (unsigned I = 0, N = Chain.size(); I != N; ++I)
1100      Result += Chain[I]->NumPreallocatedPreprocessingEntities;
1101
1102    return Result;
1103  }
1104
1105  /// \brief Returns the number of macro definitions found in the chain.
1106  unsigned getTotalNumMacroDefinitions() const {
1107    return static_cast<unsigned>(MacroDefinitionsLoaded.size());
1108  }
1109
1110  /// \brief Returns the number of C++ base specifiers found in the chain.
1111  unsigned getTotalNumCXXBaseSpecifiers() const {
1112    return NumCXXBaseSpecifiersLoaded;
1113  }
1114
1115  /// \brief Reads a TemplateArgumentLocInfo appropriate for the
1116  /// given TemplateArgument kind.
1117  TemplateArgumentLocInfo
1118  GetTemplateArgumentLocInfo(Module &F, TemplateArgument::ArgKind Kind,
1119                             const RecordData &Record, unsigned &Idx);
1120
1121  /// \brief Reads a TemplateArgumentLoc.
1122  TemplateArgumentLoc
1123  ReadTemplateArgumentLoc(Module &F,
1124                          const RecordData &Record, unsigned &Idx);
1125
1126  /// \brief Reads a declarator info from the given record.
1127  TypeSourceInfo *GetTypeSourceInfo(Module &F,
1128                                    const RecordData &Record, unsigned &Idx);
1129
1130  /// \brief Resolve and return the translation unit declaration.
1131  TranslationUnitDecl *GetTranslationUnitDecl();
1132
1133  /// \brief Resolve a type ID into a type, potentially building a new
1134  /// type.
1135  QualType GetType(serialization::TypeID ID);
1136
1137  /// \brief Resolve a local type ID within a given AST file into a type.
1138  QualType getLocalType(Module &F, unsigned LocalID);
1139
1140  /// \brief Map a local type ID within a given AST file into a global type ID.
1141  serialization::TypeID getGlobalTypeID(Module &F, unsigned LocalID) const;
1142
1143  /// \brief Read a type from the current position in the given record, which
1144  /// was read from the given AST file.
1145  QualType readType(Module &F, const RecordData &Record, unsigned &Idx) {
1146    if (Idx >= Record.size())
1147      return QualType();
1148
1149    return getLocalType(F, Record[Idx++]);
1150  }
1151
1152  /// \brief Returns the type ID associated with the given type.
1153  /// If the type didn't come from the AST file the ID that is returned is
1154  /// marked as "doesn't exist in AST".
1155  serialization::TypeID GetTypeID(QualType T) const;
1156
1157  /// \brief Returns the type index associated with the given type.
1158  /// If the type didn't come from the AST file the index that is returned is
1159  /// marked as "doesn't exist in AST".
1160  serialization::TypeIdx GetTypeIdx(QualType T) const;
1161
1162  /// \brief Map from a local declaration ID within a given module to a
1163  /// global declaration ID.
1164  serialization::DeclID getGlobalDeclID(Module &F, unsigned LocalID) const;
1165
1166  /// \brief Resolve a declaration ID into a declaration, potentially
1167  /// building a new declaration.
1168  Decl *GetDecl(serialization::DeclID ID);
1169  virtual Decl *GetExternalDecl(uint32_t ID);
1170
1171  /// \brief Reads a declaration with the given local ID in the give module.
1172  Decl *GetLocalDecl(Module &F, uint32_t LocalID) {
1173    return GetDecl(getGlobalDeclID(F, LocalID));
1174  }
1175
1176  /// \brief Reads a declaration with the given local ID in the give module.
1177  ///
1178  /// \returns The requested declaration, casted to the given return type.
1179  template<typename T>
1180  T *GetLocalDeclAs(Module &F, uint32_t LocalID) {
1181    return cast_or_null<T>(GetLocalDecl(F, LocalID));
1182  }
1183
1184  /// \brief Reads a declaration ID from the given position in a record in the
1185  /// given module.
1186  ///
1187  /// \returns The declaration ID read from the record, adjusted to a global ID.
1188  serialization::DeclID ReadDeclID(Module &F, const RecordData &Record,
1189                                   unsigned &Idx);
1190
1191  /// \brief Reads a declaration from the given position in a record in the
1192  /// given module.
1193  Decl *ReadDecl(Module &F, const RecordData &R, unsigned &I) {
1194    return GetDecl(ReadDeclID(F, R, I));
1195  }
1196
1197  /// \brief Reads a declaration from the given position in a record in the
1198  /// given module.
1199  ///
1200  /// \returns The declaration read from this location, casted to the given
1201  /// result type.
1202  template<typename T>
1203  T *ReadDeclAs(Module &F, const RecordData &R, unsigned &I) {
1204    return cast_or_null<T>(GetDecl(ReadDeclID(F, R, I)));
1205  }
1206
1207  /// \brief Resolve a CXXBaseSpecifiers ID into an offset into the chain
1208  /// of loaded AST files.
1209  uint64_t GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID);
1210
1211  virtual CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset);
1212
1213  /// \brief Resolve the offset of a statement into a statement.
1214  ///
1215  /// This operation will read a new statement from the external
1216  /// source each time it is called, and is meant to be used via a
1217  /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
1218  virtual Stmt *GetExternalDeclStmt(uint64_t Offset);
1219
1220  /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1221  /// specified cursor.  Read the abbreviations that are at the top of the block
1222  /// and then leave the cursor pointing into the block.
1223  bool ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor, unsigned BlockID);
1224
1225  /// \brief Finds all the visible declarations with a given name.
1226  /// The current implementation of this method just loads the entire
1227  /// lookup table as unmaterialized references.
1228  virtual DeclContext::lookup_result
1229  FindExternalVisibleDeclsByName(const DeclContext *DC,
1230                                 DeclarationName Name);
1231
1232  virtual void MaterializeVisibleDecls(const DeclContext *DC);
1233
1234  /// \brief Read all of the declarations lexically stored in a
1235  /// declaration context.
1236  ///
1237  /// \param DC The declaration context whose declarations will be
1238  /// read.
1239  ///
1240  /// \param Decls Vector that will contain the declarations loaded
1241  /// from the external source. The caller is responsible for merging
1242  /// these declarations with any declarations already stored in the
1243  /// declaration context.
1244  ///
1245  /// \returns true if there was an error while reading the
1246  /// declarations for this declaration context.
1247  virtual ExternalLoadResult FindExternalLexicalDecls(const DeclContext *DC,
1248                                        bool (*isKindWeWant)(Decl::Kind),
1249                                        SmallVectorImpl<Decl*> &Decls);
1250
1251  /// \brief Notify ASTReader that we started deserialization of
1252  /// a decl or type so until FinishedDeserializing is called there may be
1253  /// decls that are initializing. Must be paired with FinishedDeserializing.
1254  virtual void StartedDeserializing() { ++NumCurrentElementsDeserializing; }
1255
1256  /// \brief Notify ASTReader that we finished the deserialization of
1257  /// a decl or type. Must be paired with StartedDeserializing.
1258  virtual void FinishedDeserializing();
1259
1260  /// \brief Function that will be invoked when we begin parsing a new
1261  /// translation unit involving this external AST source.
1262  ///
1263  /// This function will provide all of the external definitions to
1264  /// the ASTConsumer.
1265  virtual void StartTranslationUnit(ASTConsumer *Consumer);
1266
1267  /// \brief Print some statistics about AST usage.
1268  virtual void PrintStats();
1269
1270  /// \brief Dump information about the AST reader to standard error.
1271  void dump();
1272
1273  /// Return the amount of memory used by memory buffers, breaking down
1274  /// by heap-backed versus mmap'ed memory.
1275  virtual void getMemoryBufferSizes(MemoryBufferSizes &sizes) const;
1276
1277  /// \brief Initialize the semantic source with the Sema instance
1278  /// being used to perform semantic analysis on the abstract syntax
1279  /// tree.
1280  virtual void InitializeSema(Sema &S);
1281
1282  /// \brief Inform the semantic consumer that Sema is no longer available.
1283  virtual void ForgetSema() { SemaObj = 0; }
1284
1285  /// \brief Retrieve the IdentifierInfo for the named identifier.
1286  ///
1287  /// This routine builds a new IdentifierInfo for the given identifier. If any
1288  /// declarations with this name are visible from translation unit scope, their
1289  /// declarations will be deserialized and introduced into the declaration
1290  /// chain of the identifier.
1291  virtual IdentifierInfo *get(const char *NameStart, const char *NameEnd);
1292  IdentifierInfo *get(StringRef Name) {
1293    return get(Name.begin(), Name.end());
1294  }
1295
1296  /// \brief Retrieve an iterator into the set of all identifiers
1297  /// in all loaded AST files.
1298  virtual IdentifierIterator *getIdentifiers() const;
1299
1300  /// \brief Load the contents of the global method pool for a given
1301  /// selector.
1302  ///
1303  /// \returns a pair of Objective-C methods lists containing the
1304  /// instance and factory methods, respectively, with this selector.
1305  virtual std::pair<ObjCMethodList, ObjCMethodList>
1306    ReadMethodPool(Selector Sel);
1307
1308  /// \brief Load the set of namespaces that are known to the external source,
1309  /// which will be used during typo correction.
1310  virtual void ReadKnownNamespaces(
1311                           SmallVectorImpl<NamespaceDecl *> &Namespaces);
1312
1313  /// \brief Load a selector from disk, registering its ID if it exists.
1314  void LoadSelector(Selector Sel);
1315
1316  void SetIdentifierInfo(unsigned ID, IdentifierInfo *II);
1317  void SetGloballyVisibleDecls(IdentifierInfo *II,
1318                               const SmallVectorImpl<uint32_t> &DeclIDs,
1319                               bool Nonrecursive = false);
1320
1321  /// \brief Report a diagnostic.
1322  DiagnosticBuilder Diag(unsigned DiagID);
1323
1324  /// \brief Report a diagnostic.
1325  DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID);
1326
1327  IdentifierInfo *DecodeIdentifierInfo(unsigned Idx);
1328
1329  IdentifierInfo *GetIdentifierInfo(const RecordData &Record, unsigned &Idx) {
1330    return DecodeIdentifierInfo(Record[Idx++]);
1331  }
1332
1333  virtual IdentifierInfo *GetIdentifier(unsigned ID) {
1334    return DecodeIdentifierInfo(ID);
1335  }
1336
1337  /// \brief Read the source location entry with index ID.
1338  virtual bool ReadSLocEntry(int ID);
1339
1340  Selector DecodeSelector(unsigned Idx);
1341
1342  virtual Selector GetExternalSelector(uint32_t ID);
1343  uint32_t GetNumExternalSelectors();
1344
1345  Selector GetSelector(const RecordData &Record, unsigned &Idx) {
1346    return DecodeSelector(Record[Idx++]);
1347  }
1348
1349  /// \brief Read a declaration name.
1350  DeclarationName ReadDeclarationName(Module &F,
1351                                      const RecordData &Record, unsigned &Idx);
1352  void ReadDeclarationNameLoc(Module &F,
1353                              DeclarationNameLoc &DNLoc, DeclarationName Name,
1354                              const RecordData &Record, unsigned &Idx);
1355  void ReadDeclarationNameInfo(Module &F, DeclarationNameInfo &NameInfo,
1356                               const RecordData &Record, unsigned &Idx);
1357
1358  void ReadQualifierInfo(Module &F, QualifierInfo &Info,
1359                         const RecordData &Record, unsigned &Idx);
1360
1361  NestedNameSpecifier *ReadNestedNameSpecifier(Module &F,
1362                                               const RecordData &Record,
1363                                               unsigned &Idx);
1364
1365  NestedNameSpecifierLoc ReadNestedNameSpecifierLoc(Module &F,
1366                                                    const RecordData &Record,
1367                                                    unsigned &Idx);
1368
1369  /// \brief Read a template name.
1370  TemplateName ReadTemplateName(Module &F, const RecordData &Record,
1371                                unsigned &Idx);
1372
1373  /// \brief Read a template argument.
1374  TemplateArgument ReadTemplateArgument(Module &F,
1375                                        const RecordData &Record,unsigned &Idx);
1376
1377  /// \brief Read a template parameter list.
1378  TemplateParameterList *ReadTemplateParameterList(Module &F,
1379                                                   const RecordData &Record,
1380                                                   unsigned &Idx);
1381
1382  /// \brief Read a template argument array.
1383  void
1384  ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
1385                           Module &F, const RecordData &Record,
1386                           unsigned &Idx);
1387
1388  /// \brief Read a UnresolvedSet structure.
1389  void ReadUnresolvedSet(Module &F, UnresolvedSetImpl &Set,
1390                         const RecordData &Record, unsigned &Idx);
1391
1392  /// \brief Read a C++ base specifier.
1393  CXXBaseSpecifier ReadCXXBaseSpecifier(Module &F,
1394                                        const RecordData &Record,unsigned &Idx);
1395
1396  /// \brief Read a CXXCtorInitializer array.
1397  std::pair<CXXCtorInitializer **, unsigned>
1398  ReadCXXCtorInitializers(Module &F, const RecordData &Record,
1399                          unsigned &Idx);
1400
1401  /// \brief Read a source location from raw form.
1402  SourceLocation ReadSourceLocation(Module &Module, unsigned Raw) {
1403    unsigned Flag = Raw & (1U << 31);
1404    unsigned Offset = Raw & ~(1U << 31);
1405    assert(Module.SLocRemap.find(Offset) != Module.SLocRemap.end() &&
1406           "Cannot find offset to remap.");
1407    int Remap = Module.SLocRemap.find(Offset)->second;
1408    Offset += Remap;
1409    assert((Offset & (1U << 31)) == 0 &&
1410           "Bad offset in reading source location");
1411    return SourceLocation::getFromRawEncoding(Offset | Flag);
1412  }
1413
1414  /// \brief Read a source location.
1415  SourceLocation ReadSourceLocation(Module &Module,
1416                                    const RecordData &Record, unsigned& Idx) {
1417    return ReadSourceLocation(Module, Record[Idx++]);
1418  }
1419
1420  /// \brief Read a source range.
1421  SourceRange ReadSourceRange(Module &F,
1422                              const RecordData &Record, unsigned& Idx);
1423
1424  /// \brief Read an integral value
1425  llvm::APInt ReadAPInt(const RecordData &Record, unsigned &Idx);
1426
1427  /// \brief Read a signed integral value
1428  llvm::APSInt ReadAPSInt(const RecordData &Record, unsigned &Idx);
1429
1430  /// \brief Read a floating-point value
1431  llvm::APFloat ReadAPFloat(const RecordData &Record, unsigned &Idx);
1432
1433  // \brief Read a string
1434  std::string ReadString(const RecordData &Record, unsigned &Idx);
1435
1436  /// \brief Read a version tuple.
1437  VersionTuple ReadVersionTuple(const RecordData &Record, unsigned &Idx);
1438
1439  CXXTemporary *ReadCXXTemporary(Module &F, const RecordData &Record,
1440                                 unsigned &Idx);
1441
1442  /// \brief Reads attributes from the current stream position.
1443  void ReadAttributes(Module &F, AttrVec &Attrs,
1444                      const RecordData &Record, unsigned &Idx);
1445
1446  /// \brief Reads a statement.
1447  Stmt *ReadStmt(Module &F);
1448
1449  /// \brief Reads an expression.
1450  Expr *ReadExpr(Module &F);
1451
1452  /// \brief Reads a sub-statement operand during statement reading.
1453  Stmt *ReadSubStmt() {
1454    assert(ReadingKind == Read_Stmt &&
1455           "Should be called only during statement reading!");
1456    // Subexpressions are stored from last to first, so the next Stmt we need
1457    // is at the back of the stack.
1458    assert(!StmtStack.empty() && "Read too many sub statements!");
1459    return StmtStack.pop_back_val();
1460  }
1461
1462  /// \brief Reads a sub-expression operand during statement reading.
1463  Expr *ReadSubExpr();
1464
1465  /// \brief Reads the macro record located at the given offset.
1466  PreprocessedEntity *ReadMacroRecord(Module &F, uint64_t Offset);
1467
1468  /// \brief Reads the preprocessed entity located at the current stream
1469  /// position.
1470  PreprocessedEntity *LoadPreprocessedEntity(Module &F);
1471
1472  /// \brief Note that the identifier is a macro whose record will be loaded
1473  /// from the given AST file at the given (file-local) offset.
1474  void SetIdentifierIsMacro(IdentifierInfo *II, Module &F,
1475                            uint64_t Offset);
1476
1477  /// \brief Read the set of macros defined by this external macro source.
1478  virtual void ReadDefinedMacros();
1479
1480  /// \brief Read the macro definition for this identifier.
1481  virtual void LoadMacroDefinition(IdentifierInfo *II);
1482
1483  /// \brief Read the macro definition corresponding to this iterator
1484  /// into the unread macro record offsets table.
1485  void LoadMacroDefinition(
1486                     llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos);
1487
1488  /// \brief Retrieve the macro definition with the given ID.
1489  MacroDefinition *getMacroDefinition(serialization::MacroID ID);
1490
1491  /// \brief Retrieve the AST context that this AST reader supplements.
1492  ASTContext *getContext() { return Context; }
1493
1494  // \brief Contains declarations that were loaded before we have
1495  // access to a Sema object.
1496  SmallVector<NamedDecl *, 16> PreloadedDecls;
1497
1498  /// \brief Retrieve the semantic analysis object used to analyze the
1499  /// translation unit in which the precompiled header is being
1500  /// imported.
1501  Sema *getSema() { return SemaObj; }
1502
1503  /// \brief Retrieve the identifier table associated with the
1504  /// preprocessor.
1505  IdentifierTable &getIdentifierTable();
1506
1507  /// \brief Record that the given ID maps to the given switch-case
1508  /// statement.
1509  void RecordSwitchCaseID(SwitchCase *SC, unsigned ID);
1510
1511  /// \brief Retrieve the switch-case statement with the given ID.
1512  SwitchCase *getSwitchCaseWithID(unsigned ID);
1513
1514  void ClearSwitchCaseIDs();
1515};
1516
1517/// \brief Helper class that saves the current stream position and
1518/// then restores it when destroyed.
1519struct SavedStreamPosition {
1520  explicit SavedStreamPosition(llvm::BitstreamCursor &Cursor)
1521  : Cursor(Cursor), Offset(Cursor.GetCurrentBitNo()) { }
1522
1523  ~SavedStreamPosition() {
1524    Cursor.JumpToBit(Offset);
1525  }
1526
1527private:
1528  llvm::BitstreamCursor &Cursor;
1529  uint64_t Offset;
1530};
1531
1532inline void PCHValidator::Error(const char *Msg) {
1533  Reader.Error(Msg);
1534}
1535
1536} // end namespace clang
1537
1538#endif
1539