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