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