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