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/AST/DeclObjC.h"
18#include "clang/AST/DeclarationName.h"
19#include "clang/AST/TemplateBase.h"
20#include "clang/Basic/Diagnostic.h"
21#include "clang/Basic/FileManager.h"
22#include "clang/Basic/FileSystemOptions.h"
23#include "clang/Basic/IdentifierTable.h"
24#include "clang/Basic/SourceManager.h"
25#include "clang/Basic/Version.h"
26#include "clang/Lex/ExternalPreprocessorSource.h"
27#include "clang/Lex/HeaderSearch.h"
28#include "clang/Lex/PreprocessingRecord.h"
29#include "clang/Sema/ExternalSemaSource.h"
30#include "clang/Serialization/ASTBitCodes.h"
31#include "clang/Serialization/ContinuousRangeMap.h"
32#include "clang/Serialization/Module.h"
33#include "clang/Serialization/ModuleManager.h"
34#include "llvm/ADT/APFloat.h"
35#include "llvm/ADT/APInt.h"
36#include "llvm/ADT/APSInt.h"
37#include "llvm/ADT/DenseSet.h"
38#include "llvm/ADT/MapVector.h"
39#include "llvm/ADT/OwningPtr.h"
40#include "llvm/ADT/SmallPtrSet.h"
41#include "llvm/ADT/SmallSet.h"
42#include "llvm/ADT/SmallVector.h"
43#include "llvm/ADT/StringRef.h"
44#include "llvm/Bitcode/BitstreamReader.h"
45#include "llvm/Support/DataTypes.h"
46#include <deque>
47#include <map>
48#include <string>
49#include <utility>
50#include <vector>
51#include <sys/stat.h>
52
53namespace llvm {
54  class MemoryBuffer;
55}
56
57namespace clang {
58
59class AddrLabelExpr;
60class ASTConsumer;
61class ASTContext;
62class ASTIdentifierIterator;
63class ASTUnit; // FIXME: Layering violation and egregious hack.
64class Attr;
65class Decl;
66class DeclContext;
67class DiagnosticOptions;
68class NestedNameSpecifier;
69class CXXBaseSpecifier;
70class CXXConstructorDecl;
71class CXXCtorInitializer;
72class GlobalModuleIndex;
73class GotoStmt;
74class MacroDefinition;
75class MacroDirective;
76class NamedDecl;
77class OpaqueValueExpr;
78class Preprocessor;
79class PreprocessorOptions;
80class Sema;
81class SwitchCase;
82class ASTDeserializationListener;
83class ASTWriter;
84class ASTReader;
85class ASTDeclReader;
86class ASTStmtReader;
87class TypeLocReader;
88struct HeaderFileInfo;
89class VersionTuple;
90class TargetOptions;
91class LazyASTUnresolvedSet;
92
93/// \brief Abstract interface for callback invocations by the ASTReader.
94///
95/// While reading an AST file, the ASTReader will call the methods of the
96/// listener to pass on specific information. Some of the listener methods can
97/// return true to indicate to the ASTReader that the information (and
98/// consequently the AST file) is invalid.
99class ASTReaderListener {
100public:
101  virtual ~ASTReaderListener();
102
103  /// \brief Receives the full Clang version information.
104  ///
105  /// \returns true to indicate that the version is invalid. Subclasses should
106  /// generally defer to this implementation.
107  virtual bool ReadFullVersionInformation(StringRef FullVersion) {
108    return FullVersion != getClangFullRepositoryVersion();
109  }
110
111  /// \brief Receives the language options.
112  ///
113  /// \returns true to indicate the options are invalid or false otherwise.
114  virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
115                                   bool Complain) {
116    return false;
117  }
118
119  /// \brief Receives the target options.
120  ///
121  /// \returns true to indicate the target options are invalid, or false
122  /// otherwise.
123  virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
124                                 bool Complain) {
125    return false;
126  }
127
128  /// \brief Receives the diagnostic options.
129  ///
130  /// \returns true to indicate the diagnostic options are invalid, or false
131  /// otherwise.
132  virtual bool ReadDiagnosticOptions(const DiagnosticOptions &DiagOpts,
133                                     bool Complain) {
134    return false;
135  }
136
137  /// \brief Receives the file system options.
138  ///
139  /// \returns true to indicate the file system options are invalid, or false
140  /// otherwise.
141  virtual bool ReadFileSystemOptions(const FileSystemOptions &FSOpts,
142                                     bool Complain) {
143    return false;
144  }
145
146  /// \brief Receives the header search options.
147  ///
148  /// \returns true to indicate the header search options are invalid, or false
149  /// otherwise.
150  virtual bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
151                                       bool Complain) {
152    return false;
153  }
154
155  /// \brief Receives the preprocessor options.
156  ///
157  /// \param SuggestedPredefines Can be filled in with the set of predefines
158  /// that are suggested by the preprocessor options. Typically only used when
159  /// loading a precompiled header.
160  ///
161  /// \returns true to indicate the preprocessor options are invalid, or false
162  /// otherwise.
163  virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
164                                       bool Complain,
165                                       std::string &SuggestedPredefines) {
166    return false;
167  }
168
169  /// \brief Receives __COUNTER__ value.
170  virtual void ReadCounter(const serialization::ModuleFile &M,
171                           unsigned Value) {}
172
173  /// \brief Returns true if this \c ASTReaderListener wants to receive the
174  /// input files of the AST file via \c visitInputFile, false otherwise.
175  virtual bool needsInputFileVisitation() { return false; }
176
177  /// \brief if \c needsInputFileVisitation returns true, this is called for each
178  /// input file of the AST file.
179  ///
180  /// \returns true to continue receiving the next input file, false to stop.
181  virtual bool visitInputFile(StringRef Filename, bool isSystem) { return true;}
182};
183
184/// \brief ASTReaderListener implementation to validate the information of
185/// the PCH file against an initialized Preprocessor.
186class PCHValidator : public ASTReaderListener {
187  Preprocessor &PP;
188  ASTReader &Reader;
189
190public:
191  PCHValidator(Preprocessor &PP, ASTReader &Reader)
192    : PP(PP), Reader(Reader) {}
193
194  virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
195                                   bool Complain);
196  virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
197                                 bool Complain);
198  virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
199                                       bool Complain,
200                                       std::string &SuggestedPredefines);
201  virtual void ReadCounter(const serialization::ModuleFile &M, unsigned Value);
202
203private:
204  void Error(const char *Msg);
205};
206
207namespace serialization {
208
209class ReadMethodPoolVisitor;
210
211namespace reader {
212  class ASTIdentifierLookupTrait;
213  /// \brief The on-disk hash table used for the DeclContext's Name lookup table.
214  typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
215    ASTDeclContextNameLookupTable;
216}
217
218} // end namespace serialization
219
220/// \brief Reads an AST files chain containing the contents of a translation
221/// unit.
222///
223/// The ASTReader class reads bitstreams (produced by the ASTWriter
224/// class) containing the serialized representation of a given
225/// abstract syntax tree and its supporting data structures. An
226/// instance of the ASTReader can be attached to an ASTContext object,
227/// which will provide access to the contents of the AST files.
228///
229/// The AST reader provides lazy de-serialization of declarations, as
230/// required when traversing the AST. Only those AST nodes that are
231/// actually required will be de-serialized.
232class ASTReader
233  : public ExternalPreprocessorSource,
234    public ExternalPreprocessingRecordSource,
235    public ExternalHeaderFileInfoSource,
236    public ExternalSemaSource,
237    public IdentifierInfoLookup,
238    public ExternalIdentifierLookup,
239    public ExternalSLocEntrySource
240{
241public:
242  typedef SmallVector<uint64_t, 64> RecordData;
243  typedef SmallVectorImpl<uint64_t> RecordDataImpl;
244
245  /// \brief The result of reading the control block of an AST file, which
246  /// can fail for various reasons.
247  enum ASTReadResult {
248    /// \brief The control block was read successfully. Aside from failures,
249    /// the AST file is safe to read into the current context.
250    Success,
251    /// \brief The AST file itself appears corrupted.
252    Failure,
253    /// \brief The AST file was missing.
254    Missing,
255    /// \brief The AST file is out-of-date relative to its input files,
256    /// and needs to be regenerated.
257    OutOfDate,
258    /// \brief The AST file was written by a different version of Clang.
259    VersionMismatch,
260    /// \brief The AST file was writtten with a different language/target
261    /// configuration.
262    ConfigurationMismatch,
263    /// \brief The AST file has errors.
264    HadErrors
265  };
266
267  /// \brief Types of AST files.
268  friend class PCHValidator;
269  friend class ASTDeclReader;
270  friend class ASTStmtReader;
271  friend class ASTIdentifierIterator;
272  friend class serialization::reader::ASTIdentifierLookupTrait;
273  friend class TypeLocReader;
274  friend class ASTWriter;
275  friend class ASTUnit; // ASTUnit needs to remap source locations.
276  friend class serialization::ReadMethodPoolVisitor;
277
278  typedef serialization::ModuleFile ModuleFile;
279  typedef serialization::ModuleKind ModuleKind;
280  typedef serialization::ModuleManager ModuleManager;
281
282  typedef ModuleManager::ModuleIterator ModuleIterator;
283  typedef ModuleManager::ModuleConstIterator ModuleConstIterator;
284  typedef ModuleManager::ModuleReverseIterator ModuleReverseIterator;
285
286private:
287  /// \brief The receiver of some callbacks invoked by ASTReader.
288  OwningPtr<ASTReaderListener> Listener;
289
290  /// \brief The receiver of deserialization events.
291  ASTDeserializationListener *DeserializationListener;
292
293  SourceManager &SourceMgr;
294  FileManager &FileMgr;
295  DiagnosticsEngine &Diags;
296
297  /// \brief The semantic analysis object that will be processing the
298  /// AST files and the translation unit that uses it.
299  Sema *SemaObj;
300
301  /// \brief The preprocessor that will be loading the source file.
302  Preprocessor &PP;
303
304  /// \brief The AST context into which we'll read the AST files.
305  ASTContext &Context;
306
307  /// \brief The AST consumer.
308  ASTConsumer *Consumer;
309
310  /// \brief The module manager which manages modules and their dependencies
311  ModuleManager ModuleMgr;
312
313  /// \brief The location where the module file will be considered as
314  /// imported from. For non-module AST types it should be invalid.
315  SourceLocation CurrentImportLoc;
316
317  /// \brief The global module index, if loaded.
318  llvm::OwningPtr<GlobalModuleIndex> GlobalIndex;
319
320  /// \brief A map of global bit offsets to the module that stores entities
321  /// at those bit offsets.
322  ContinuousRangeMap<uint64_t, ModuleFile*, 4> GlobalBitOffsetsMap;
323
324  /// \brief A map of negated SLocEntryIDs to the modules containing them.
325  ContinuousRangeMap<unsigned, ModuleFile*, 64> GlobalSLocEntryMap;
326
327  typedef ContinuousRangeMap<unsigned, ModuleFile*, 64> GlobalSLocOffsetMapType;
328
329  /// \brief A map of reversed (SourceManager::MaxLoadedOffset - SLocOffset)
330  /// SourceLocation offsets to the modules containing them.
331  GlobalSLocOffsetMapType GlobalSLocOffsetMap;
332
333  /// \brief Types that have already been loaded from the chain.
334  ///
335  /// When the pointer at index I is non-NULL, the type with
336  /// ID = (I + 1) << FastQual::Width has already been loaded
337  std::vector<QualType> TypesLoaded;
338
339  typedef ContinuousRangeMap<serialization::TypeID, ModuleFile *, 4>
340    GlobalTypeMapType;
341
342  /// \brief Mapping from global type IDs to the module in which the
343  /// type resides along with the offset that should be added to the
344  /// global type ID to produce a local ID.
345  GlobalTypeMapType GlobalTypeMap;
346
347  /// \brief Declarations that have already been loaded from the chain.
348  ///
349  /// When the pointer at index I is non-NULL, the declaration with ID
350  /// = I + 1 has already been loaded.
351  std::vector<Decl *> DeclsLoaded;
352
353  typedef ContinuousRangeMap<serialization::DeclID, ModuleFile *, 4>
354    GlobalDeclMapType;
355
356  /// \brief Mapping from global declaration IDs to the module in which the
357  /// declaration resides.
358  GlobalDeclMapType GlobalDeclMap;
359
360  typedef std::pair<ModuleFile *, uint64_t> FileOffset;
361  typedef SmallVector<FileOffset, 2> FileOffsetsTy;
362  typedef llvm::DenseMap<serialization::DeclID, FileOffsetsTy>
363      DeclUpdateOffsetsMap;
364
365  /// \brief Declarations that have modifications residing in a later file
366  /// in the chain.
367  DeclUpdateOffsetsMap DeclUpdateOffsets;
368
369  struct ReplacedDeclInfo {
370    ModuleFile *Mod;
371    uint64_t Offset;
372    unsigned RawLoc;
373
374    ReplacedDeclInfo() : Mod(0), Offset(0), RawLoc(0) {}
375    ReplacedDeclInfo(ModuleFile *Mod, uint64_t Offset, unsigned RawLoc)
376      : Mod(Mod), Offset(Offset), RawLoc(RawLoc) {}
377  };
378
379  typedef llvm::DenseMap<serialization::DeclID, ReplacedDeclInfo>
380      DeclReplacementMap;
381  /// \brief Declarations that have been replaced in a later file in the chain.
382  DeclReplacementMap ReplacedDecls;
383
384  struct FileDeclsInfo {
385    ModuleFile *Mod;
386    ArrayRef<serialization::LocalDeclID> Decls;
387
388    FileDeclsInfo() : Mod(0) {}
389    FileDeclsInfo(ModuleFile *Mod, ArrayRef<serialization::LocalDeclID> Decls)
390      : Mod(Mod), Decls(Decls) {}
391  };
392
393  /// \brief Map from a FileID to the file-level declarations that it contains.
394  llvm::DenseMap<FileID, FileDeclsInfo> FileDeclIDs;
395
396  // Updates for visible decls can occur for other contexts than just the
397  // TU, and when we read those update records, the actual context will not
398  // be available yet (unless it's the TU), so have this pending map using the
399  // ID as a key. It will be realized when the context is actually loaded.
400  typedef
401    SmallVector<std::pair<serialization::reader::ASTDeclContextNameLookupTable *,
402                          ModuleFile*>, 1> DeclContextVisibleUpdates;
403  typedef llvm::DenseMap<serialization::DeclID, DeclContextVisibleUpdates>
404      DeclContextVisibleUpdatesPending;
405
406  /// \brief Updates to the visible declarations of declaration contexts that
407  /// haven't been loaded yet.
408  DeclContextVisibleUpdatesPending PendingVisibleUpdates;
409
410  /// \brief The set of C++ or Objective-C classes that have forward
411  /// declarations that have not yet been linked to their definitions.
412  llvm::SmallPtrSet<Decl *, 4> PendingDefinitions;
413
414  typedef llvm::MapVector<Decl *, uint64_t,
415                          llvm::SmallDenseMap<Decl *, unsigned, 4>,
416                          SmallVector<std::pair<Decl *, uint64_t>, 4> >
417    PendingBodiesMap;
418
419  /// \brief Functions or methods that have bodies that will be attached.
420  PendingBodiesMap PendingBodies;
421
422  /// \brief Read the records that describe the contents of declcontexts.
423  bool ReadDeclContextStorage(ModuleFile &M,
424                              llvm::BitstreamCursor &Cursor,
425                              const std::pair<uint64_t, uint64_t> &Offsets,
426                              serialization::DeclContextInfo &Info);
427
428  /// \brief A vector containing identifiers that have already been
429  /// loaded.
430  ///
431  /// If the pointer at index I is non-NULL, then it refers to the
432  /// IdentifierInfo for the identifier with ID=I+1 that has already
433  /// been loaded.
434  std::vector<IdentifierInfo *> IdentifiersLoaded;
435
436  typedef ContinuousRangeMap<serialization::IdentID, ModuleFile *, 4>
437    GlobalIdentifierMapType;
438
439  /// \brief Mapping from global identifier IDs to the module in which the
440  /// identifier resides along with the offset that should be added to the
441  /// global identifier ID to produce a local ID.
442  GlobalIdentifierMapType GlobalIdentifierMap;
443
444  /// \brief A vector containing macros that have already been
445  /// loaded.
446  ///
447  /// If the pointer at index I is non-NULL, then it refers to the
448  /// MacroInfo for the identifier with ID=I+1 that has already
449  /// been loaded.
450  std::vector<MacroInfo *> MacrosLoaded;
451
452  typedef ContinuousRangeMap<serialization::MacroID, ModuleFile *, 4>
453    GlobalMacroMapType;
454
455  /// \brief Mapping from global macro IDs to the module in which the
456  /// macro resides along with the offset that should be added to the
457  /// global macro ID to produce a local ID.
458  GlobalMacroMapType GlobalMacroMap;
459
460  /// \brief A vector containing submodules that have already been loaded.
461  ///
462  /// This vector is indexed by the Submodule ID (-1). NULL submodule entries
463  /// indicate that the particular submodule ID has not yet been loaded.
464  SmallVector<Module *, 2> SubmodulesLoaded;
465
466  typedef ContinuousRangeMap<serialization::SubmoduleID, ModuleFile *, 4>
467    GlobalSubmoduleMapType;
468
469  /// \brief Mapping from global submodule IDs to the module file in which the
470  /// submodule resides along with the offset that should be added to the
471  /// global submodule ID to produce a local ID.
472  GlobalSubmoduleMapType GlobalSubmoduleMap;
473
474  /// \brief An entity that has been hidden.
475  class HiddenName {
476  public:
477    enum NameKind {
478      Declaration,
479      MacroVisibility
480    } Kind;
481
482  private:
483    union {
484      Decl *D;
485      MacroDirective *MD;
486    };
487
488    IdentifierInfo *Id;
489
490  public:
491    HiddenName(Decl *D) : Kind(Declaration), D(D), Id() { }
492
493    HiddenName(IdentifierInfo *II, MacroDirective *MD)
494      : Kind(MacroVisibility), MD(MD), Id(II) { }
495
496    NameKind getKind() const { return Kind; }
497
498    Decl *getDecl() const {
499      assert(getKind() == Declaration && "Hidden name is not a declaration");
500      return D;
501    }
502
503    std::pair<IdentifierInfo *, MacroDirective *> getMacro() const {
504      assert(getKind() == MacroVisibility && "Hidden name is not a macro!");
505      return std::make_pair(Id, MD);
506    }
507};
508
509  /// \brief A set of hidden declarations.
510  typedef SmallVector<HiddenName, 2> HiddenNames;
511
512  typedef llvm::DenseMap<Module *, HiddenNames> HiddenNamesMapType;
513
514  /// \brief A mapping from each of the hidden submodules to the deserialized
515  /// declarations in that submodule that could be made visible.
516  HiddenNamesMapType HiddenNamesMap;
517
518
519  /// \brief A module import, export, or conflict that hasn't yet been resolved.
520  struct UnresolvedModuleRef {
521    /// \brief The file in which this module resides.
522    ModuleFile *File;
523
524    /// \brief The module that is importing or exporting.
525    Module *Mod;
526
527    /// \brief The kind of module reference.
528    enum { Import, Export, Conflict } Kind;
529
530    /// \brief The local ID of the module that is being exported.
531    unsigned ID;
532
533    /// \brief Whether this is a wildcard export.
534    unsigned IsWildcard : 1;
535
536    /// \brief String data.
537    StringRef String;
538  };
539
540  /// \brief The set of module imports and exports that still need to be
541  /// resolved.
542  SmallVector<UnresolvedModuleRef, 2> UnresolvedModuleRefs;
543
544  /// \brief A vector containing selectors that have already been loaded.
545  ///
546  /// This vector is indexed by the Selector ID (-1). NULL selector
547  /// entries indicate that the particular selector ID has not yet
548  /// been loaded.
549  SmallVector<Selector, 16> SelectorsLoaded;
550
551  typedef ContinuousRangeMap<serialization::SelectorID, ModuleFile *, 4>
552    GlobalSelectorMapType;
553
554  /// \brief Mapping from global selector IDs to the module in which the
555
556  /// global selector ID to produce a local ID.
557  GlobalSelectorMapType GlobalSelectorMap;
558
559  /// \brief The generation number of the last time we loaded data from the
560  /// global method pool for this selector.
561  llvm::DenseMap<Selector, unsigned> SelectorGeneration;
562
563  struct PendingMacroInfo {
564    ModuleFile *M;
565
566    struct ModuleMacroDataTy {
567      serialization::GlobalMacroID GMacID;
568      unsigned ImportLoc;
569    };
570    struct PCHMacroDataTy {
571      uint64_t MacroDirectivesOffset;
572    };
573
574    union {
575      ModuleMacroDataTy ModuleMacroData;
576      PCHMacroDataTy PCHMacroData;
577    };
578
579    PendingMacroInfo(ModuleFile *M,
580                     serialization::GlobalMacroID GMacID,
581                     SourceLocation ImportLoc) : M(M) {
582      ModuleMacroData.GMacID = GMacID;
583      ModuleMacroData.ImportLoc = ImportLoc.getRawEncoding();
584    }
585
586    PendingMacroInfo(ModuleFile *M, uint64_t MacroDirectivesOffset) : M(M) {
587      PCHMacroData.MacroDirectivesOffset = MacroDirectivesOffset;
588    }
589  };
590
591  typedef llvm::MapVector<IdentifierInfo *, SmallVector<PendingMacroInfo, 2> >
592    PendingMacroIDsMap;
593
594  /// \brief Mapping from identifiers that have a macro history to the global
595  /// IDs have not yet been deserialized to the global IDs of those macros.
596  PendingMacroIDsMap PendingMacroIDs;
597
598  typedef ContinuousRangeMap<unsigned, ModuleFile *, 4>
599    GlobalPreprocessedEntityMapType;
600
601  /// \brief Mapping from global preprocessing entity IDs to the module in
602  /// which the preprocessed entity resides along with the offset that should be
603  /// added to the global preprocessing entitiy ID to produce a local ID.
604  GlobalPreprocessedEntityMapType GlobalPreprocessedEntityMap;
605
606  /// \name CodeGen-relevant special data
607  /// \brief Fields containing data that is relevant to CodeGen.
608  //@{
609
610  /// \brief The IDs of all declarations that fulfill the criteria of
611  /// "interesting" decls.
612  ///
613  /// This contains the data loaded from all EXTERNAL_DEFINITIONS blocks in the
614  /// chain. The referenced declarations are deserialized and passed to the
615  /// consumer eagerly.
616  SmallVector<uint64_t, 16> ExternalDefinitions;
617
618  /// \brief The IDs of all tentative definitions stored in the chain.
619  ///
620  /// Sema keeps track of all tentative definitions in a TU because it has to
621  /// complete them and pass them on to CodeGen. Thus, tentative definitions in
622  /// the PCH chain must be eagerly deserialized.
623  SmallVector<uint64_t, 16> TentativeDefinitions;
624
625  /// \brief The IDs of all CXXRecordDecls stored in the chain whose VTables are
626  /// used.
627  ///
628  /// CodeGen has to emit VTables for these records, so they have to be eagerly
629  /// deserialized.
630  SmallVector<uint64_t, 64> VTableUses;
631
632  /// \brief A snapshot of the pending instantiations in the chain.
633  ///
634  /// This record tracks the instantiations that Sema has to perform at the
635  /// end of the TU. It consists of a pair of values for every pending
636  /// instantiation where the first value is the ID of the decl and the second
637  /// is the instantiation location.
638  SmallVector<uint64_t, 64> PendingInstantiations;
639
640  //@}
641
642  /// \name DiagnosticsEngine-relevant special data
643  /// \brief Fields containing data that is used for generating diagnostics
644  //@{
645
646  /// \brief A snapshot of Sema's unused file-scoped variable tracking, for
647  /// generating warnings.
648  SmallVector<uint64_t, 16> UnusedFileScopedDecls;
649
650  /// \brief A list of all the delegating constructors we've seen, to diagnose
651  /// cycles.
652  SmallVector<uint64_t, 4> DelegatingCtorDecls;
653
654  /// \brief Method selectors used in a @selector expression. Used for
655  /// implementation of -Wselector.
656  SmallVector<uint64_t, 64> ReferencedSelectorsData;
657
658  /// \brief A snapshot of Sema's weak undeclared identifier tracking, for
659  /// generating warnings.
660  SmallVector<uint64_t, 64> WeakUndeclaredIdentifiers;
661
662  /// \brief The IDs of type aliases for ext_vectors that exist in the chain.
663  ///
664  /// Used by Sema for finding sugared names for ext_vectors in diagnostics.
665  SmallVector<uint64_t, 4> ExtVectorDecls;
666
667  //@}
668
669  /// \name Sema-relevant special data
670  /// \brief Fields containing data that is used for semantic analysis
671  //@{
672
673  /// \brief The IDs of all locally scoped extern "C" decls in the chain.
674  ///
675  /// Sema tracks these to validate that the types are consistent across all
676  /// local extern "C" declarations.
677  SmallVector<uint64_t, 16> LocallyScopedExternCDecls;
678
679  /// \brief The IDs of all dynamic class declarations in the chain.
680  ///
681  /// Sema tracks these because it checks for the key functions being defined
682  /// at the end of the TU, in which case it directs CodeGen to emit the VTable.
683  SmallVector<uint64_t, 16> DynamicClasses;
684
685  /// \brief The IDs of the declarations Sema stores directly.
686  ///
687  /// Sema tracks a few important decls, such as namespace std, directly.
688  SmallVector<uint64_t, 4> SemaDeclRefs;
689
690  /// \brief The IDs of the types ASTContext stores directly.
691  ///
692  /// The AST context tracks a few important types, such as va_list, directly.
693  SmallVector<uint64_t, 16> SpecialTypes;
694
695  /// \brief The IDs of CUDA-specific declarations ASTContext stores directly.
696  ///
697  /// The AST context tracks a few important decls, currently cudaConfigureCall,
698  /// directly.
699  SmallVector<uint64_t, 2> CUDASpecialDeclRefs;
700
701  /// \brief The floating point pragma option settings.
702  SmallVector<uint64_t, 1> FPPragmaOptions;
703
704  /// \brief The OpenCL extension settings.
705  SmallVector<uint64_t, 1> OpenCLExtensions;
706
707  /// \brief A list of the namespaces we've seen.
708  SmallVector<uint64_t, 4> KnownNamespaces;
709
710  /// \brief A list of undefined decls with internal linkage followed by the
711  /// SourceLocation of a matching ODR-use.
712  SmallVector<uint64_t, 8> UndefinedButUsed;
713
714  // \brief A list of late parsed template function data.
715  SmallVector<uint64_t, 1> LateParsedTemplates;
716
717  /// \brief A list of modules that were imported by precompiled headers or
718  /// any other non-module AST file.
719  SmallVector<serialization::SubmoduleID, 2> ImportedModules;
720  //@}
721
722  /// \brief The directory that the PCH we are reading is stored in.
723  std::string CurrentDir;
724
725  /// \brief The system include root to be used when loading the
726  /// precompiled header.
727  std::string isysroot;
728
729  /// \brief Whether to disable the normal validation performed on precompiled
730  /// headers when they are loaded.
731  bool DisableValidation;
732
733  /// \brief Whether to accept an AST file with compiler errors.
734  bool AllowASTWithCompilerErrors;
735
736  /// \brief Whether we are allowed to use the global module index.
737  bool UseGlobalIndex;
738
739  /// \brief Whether we have tried loading the global module index yet.
740  bool TriedLoadingGlobalIndex;
741
742  /// \brief The current "generation" of the module file import stack, which
743  /// indicates how many separate module file load operations have occurred.
744  unsigned CurrentGeneration;
745
746  typedef llvm::DenseMap<unsigned, SwitchCase *> SwitchCaseMapTy;
747  /// \brief Mapping from switch-case IDs in the chain to switch-case statements
748  ///
749  /// Statements usually don't have IDs, but switch cases need them, so that the
750  /// switch statement can refer to them.
751  SwitchCaseMapTy SwitchCaseStmts;
752
753  SwitchCaseMapTy *CurrSwitchCaseStmts;
754
755  /// \brief The number of source location entries de-serialized from
756  /// the PCH file.
757  unsigned NumSLocEntriesRead;
758
759  /// \brief The number of source location entries in the chain.
760  unsigned TotalNumSLocEntries;
761
762  /// \brief The number of statements (and expressions) de-serialized
763  /// from the chain.
764  unsigned NumStatementsRead;
765
766  /// \brief The total number of statements (and expressions) stored
767  /// in the chain.
768  unsigned TotalNumStatements;
769
770  /// \brief The number of macros de-serialized from the chain.
771  unsigned NumMacrosRead;
772
773  /// \brief The total number of macros stored in the chain.
774  unsigned TotalNumMacros;
775
776  /// \brief The number of lookups into identifier tables.
777  unsigned NumIdentifierLookups;
778
779  /// \brief The number of lookups into identifier tables that succeed.
780  unsigned NumIdentifierLookupHits;
781
782  /// \brief The number of selectors that have been read.
783  unsigned NumSelectorsRead;
784
785  /// \brief The number of method pool entries that have been read.
786  unsigned NumMethodPoolEntriesRead;
787
788  /// \brief The number of times we have looked up a selector in the method
789  /// pool.
790  unsigned NumMethodPoolLookups;
791
792  /// \brief The number of times we have looked up a selector in the method
793  /// pool and found something.
794  unsigned NumMethodPoolHits;
795
796  /// \brief The number of times we have looked up a selector in the method
797  /// pool within a specific module.
798  unsigned NumMethodPoolTableLookups;
799
800  /// \brief The number of times we have looked up a selector in the method
801  /// pool within a specific module and found something.
802  unsigned NumMethodPoolTableHits;
803
804  /// \brief The total number of method pool entries in the selector table.
805  unsigned TotalNumMethodPoolEntries;
806
807  /// Number of lexical decl contexts read/total.
808  unsigned NumLexicalDeclContextsRead, TotalLexicalDeclContexts;
809
810  /// Number of visible decl contexts read/total.
811  unsigned NumVisibleDeclContextsRead, TotalVisibleDeclContexts;
812
813  /// Total size of modules, in bits, currently loaded
814  uint64_t TotalModulesSizeInBits;
815
816  /// \brief Number of Decl/types that are currently deserializing.
817  unsigned NumCurrentElementsDeserializing;
818
819  /// \brief Set true while we are in the process of passing deserialized
820  /// "interesting" decls to consumer inside FinishedDeserializing().
821  /// This is used as a guard to avoid recursively repeating the process of
822  /// passing decls to consumer.
823  bool PassingDeclsToConsumer;
824
825  /// Number of CXX base specifiers currently loaded
826  unsigned NumCXXBaseSpecifiersLoaded;
827
828  /// \brief The set of identifiers that were read while the AST reader was
829  /// (recursively) loading declarations.
830  ///
831  /// The declarations on the identifier chain for these identifiers will be
832  /// loaded once the recursive loading has completed.
833  llvm::MapVector<IdentifierInfo *, SmallVector<uint32_t, 4> >
834    PendingIdentifierInfos;
835
836  /// \brief The generation number of each identifier, which keeps track of
837  /// the last time we loaded information about this identifier.
838  llvm::DenseMap<IdentifierInfo *, unsigned> IdentifierGeneration;
839
840  /// \brief Contains declarations and definitions that will be
841  /// "interesting" to the ASTConsumer, when we get that AST consumer.
842  ///
843  /// "Interesting" declarations are those that have data that may
844  /// need to be emitted, such as inline function definitions or
845  /// Objective-C protocols.
846  std::deque<Decl *> InterestingDecls;
847
848  /// \brief The set of redeclarable declarations that have been deserialized
849  /// since the last time the declaration chains were linked.
850  llvm::SmallPtrSet<Decl *, 16> RedeclsDeserialized;
851
852  /// \brief The list of redeclaration chains that still need to be
853  /// reconstructed.
854  ///
855  /// Each element is the global declaration ID of the first declaration in
856  /// the chain. Elements in this vector should be unique; use
857  /// PendingDeclChainsKnown to ensure uniqueness.
858  SmallVector<serialization::DeclID, 16> PendingDeclChains;
859
860  /// \brief Keeps track of the elements added to PendingDeclChains.
861  llvm::SmallSet<serialization::DeclID, 16> PendingDeclChainsKnown;
862
863  /// \brief The Decl IDs for the Sema/Lexical DeclContext of a Decl that has
864  /// been loaded but its DeclContext was not set yet.
865  struct PendingDeclContextInfo {
866    Decl *D;
867    serialization::GlobalDeclID SemaDC;
868    serialization::GlobalDeclID LexicalDC;
869  };
870
871  /// \brief The set of Decls that have been loaded but their DeclContexts are
872  /// not set yet.
873  ///
874  /// The DeclContexts for these Decls will be set once recursive loading has
875  /// been completed.
876  std::deque<PendingDeclContextInfo> PendingDeclContextInfos;
877
878  /// \brief The set of NamedDecls that have been loaded, but are members of a
879  /// context that has been merged into another context where the corresponding
880  /// declaration is either missing or has not yet been loaded.
881  ///
882  /// We will check whether the corresponding declaration is in fact missing
883  /// once recursing loading has been completed.
884  llvm::SmallVector<NamedDecl *, 16> PendingOdrMergeChecks;
885
886  /// \brief The set of Objective-C categories that have been deserialized
887  /// since the last time the declaration chains were linked.
888  llvm::SmallPtrSet<ObjCCategoryDecl *, 16> CategoriesDeserialized;
889
890  /// \brief The set of Objective-C class definitions that have already been
891  /// loaded, for which we will need to check for categories whenever a new
892  /// module is loaded.
893  SmallVector<ObjCInterfaceDecl *, 16> ObjCClassesLoaded;
894
895  typedef llvm::DenseMap<Decl *, SmallVector<serialization::DeclID, 2> >
896    MergedDeclsMap;
897
898  /// \brief A mapping from canonical declarations to the set of additional
899  /// (global, previously-canonical) declaration IDs that have been merged with
900  /// that canonical declaration.
901  MergedDeclsMap MergedDecls;
902
903  typedef llvm::DenseMap<serialization::GlobalDeclID,
904                         SmallVector<serialization::DeclID, 2> >
905    StoredMergedDeclsMap;
906
907  /// \brief A mapping from canonical declaration IDs to the set of additional
908  /// declaration IDs that have been merged with that canonical declaration.
909  ///
910  /// This is the deserialized representation of the entries in MergedDecls.
911  /// When we query entries in MergedDecls, they will be augmented with entries
912  /// from StoredMergedDecls.
913  StoredMergedDeclsMap StoredMergedDecls;
914
915  /// \brief Combine the stored merged declarations for the given canonical
916  /// declaration into the set of merged declarations.
917  ///
918  /// \returns An iterator into MergedDecls that corresponds to the position of
919  /// the given canonical declaration.
920  MergedDeclsMap::iterator
921  combineStoredMergedDecls(Decl *Canon, serialization::GlobalDeclID CanonID);
922
923  /// \brief A mapping from DeclContexts to the semantic DeclContext that we
924  /// are treating as the definition of the entity. This is used, for instance,
925  /// when merging implicit instantiations of class templates across modules.
926  llvm::DenseMap<DeclContext *, DeclContext *> MergedDeclContexts;
927
928  /// \brief A mapping from canonical declarations of enums to their canonical
929  /// definitions. Only populated when using modules in C++.
930  llvm::DenseMap<EnumDecl *, EnumDecl *> EnumDefinitions;
931
932  /// \brief When reading a Stmt tree, Stmt operands are placed in this stack.
933  SmallVector<Stmt *, 16> StmtStack;
934
935  /// \brief What kind of records we are reading.
936  enum ReadingKind {
937    Read_None, Read_Decl, Read_Type, Read_Stmt
938  };
939
940  /// \brief What kind of records we are reading.
941  ReadingKind ReadingKind;
942
943  /// \brief RAII object to change the reading kind.
944  class ReadingKindTracker {
945    ASTReader &Reader;
946    enum ReadingKind PrevKind;
947
948    ReadingKindTracker(const ReadingKindTracker &) LLVM_DELETED_FUNCTION;
949    void operator=(const ReadingKindTracker &) LLVM_DELETED_FUNCTION;
950
951  public:
952    ReadingKindTracker(enum ReadingKind newKind, ASTReader &reader)
953      : Reader(reader), PrevKind(Reader.ReadingKind) {
954      Reader.ReadingKind = newKind;
955    }
956
957    ~ReadingKindTracker() { Reader.ReadingKind = PrevKind; }
958  };
959
960  /// \brief Suggested contents of the predefines buffer, after this
961  /// PCH file has been processed.
962  ///
963  /// In most cases, this string will be empty, because the predefines
964  /// buffer computed to build the PCH file will be identical to the
965  /// predefines buffer computed from the command line. However, when
966  /// there are differences that the PCH reader can work around, this
967  /// predefines buffer may contain additional definitions.
968  std::string SuggestedPredefines;
969
970  /// \brief Reads a statement from the specified cursor.
971  Stmt *ReadStmtFromStream(ModuleFile &F);
972
973  /// \brief Retrieve the file entry and 'overridden' bit for an input
974  /// file in the given module file.
975  serialization::InputFile getInputFile(ModuleFile &F, unsigned ID,
976                                        bool Complain = true);
977
978  /// \brief Get a FileEntry out of stored-in-PCH filename, making sure we take
979  /// into account all the necessary relocations.
980  const FileEntry *getFileEntry(StringRef filename);
981
982  void MaybeAddSystemRootToFilename(ModuleFile &M, std::string &Filename);
983
984  struct ImportedModule {
985    ModuleFile *Mod;
986    ModuleFile *ImportedBy;
987    SourceLocation ImportLoc;
988
989    ImportedModule(ModuleFile *Mod,
990                   ModuleFile *ImportedBy,
991                   SourceLocation ImportLoc)
992      : Mod(Mod), ImportedBy(ImportedBy), ImportLoc(ImportLoc) { }
993  };
994
995  ASTReadResult ReadASTCore(StringRef FileName, ModuleKind Type,
996                            SourceLocation ImportLoc, ModuleFile *ImportedBy,
997                            SmallVectorImpl<ImportedModule> &Loaded,
998                            off_t ExpectedSize, time_t ExpectedModTime,
999                            unsigned ClientLoadCapabilities);
1000  ASTReadResult ReadControlBlock(ModuleFile &F,
1001                                 SmallVectorImpl<ImportedModule> &Loaded,
1002                                 unsigned ClientLoadCapabilities);
1003  bool ReadASTBlock(ModuleFile &F);
1004  bool ParseLineTable(ModuleFile &F, SmallVectorImpl<uint64_t> &Record);
1005  bool ReadSourceManagerBlock(ModuleFile &F);
1006  llvm::BitstreamCursor &SLocCursorForID(int ID);
1007  SourceLocation getImportLocation(ModuleFile *F);
1008  bool ReadSubmoduleBlock(ModuleFile &F);
1009  static bool ParseLanguageOptions(const RecordData &Record, bool Complain,
1010                                   ASTReaderListener &Listener);
1011  static bool ParseTargetOptions(const RecordData &Record, bool Complain,
1012                                 ASTReaderListener &Listener);
1013  static bool ParseDiagnosticOptions(const RecordData &Record, bool Complain,
1014                                     ASTReaderListener &Listener);
1015  static bool ParseFileSystemOptions(const RecordData &Record, bool Complain,
1016                                     ASTReaderListener &Listener);
1017  static bool ParseHeaderSearchOptions(const RecordData &Record, bool Complain,
1018                                       ASTReaderListener &Listener);
1019  static bool ParsePreprocessorOptions(const RecordData &Record, bool Complain,
1020                                       ASTReaderListener &Listener,
1021                                       std::string &SuggestedPredefines);
1022
1023  struct RecordLocation {
1024    RecordLocation(ModuleFile *M, uint64_t O)
1025      : F(M), Offset(O) {}
1026    ModuleFile *F;
1027    uint64_t Offset;
1028  };
1029
1030  QualType readTypeRecord(unsigned Index);
1031  RecordLocation TypeCursorForIndex(unsigned Index);
1032  void LoadedDecl(unsigned Index, Decl *D);
1033  Decl *ReadDeclRecord(serialization::DeclID ID);
1034  RecordLocation DeclCursorForID(serialization::DeclID ID,
1035                                 unsigned &RawLocation);
1036  void loadDeclUpdateRecords(serialization::DeclID ID, Decl *D);
1037  void loadPendingDeclChain(serialization::GlobalDeclID ID);
1038  void loadObjCCategories(serialization::GlobalDeclID ID, ObjCInterfaceDecl *D,
1039                          unsigned PreviousGeneration = 0);
1040
1041  RecordLocation getLocalBitOffset(uint64_t GlobalOffset);
1042  uint64_t getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset);
1043
1044  /// \brief Returns the first preprocessed entity ID that ends after BLoc.
1045  serialization::PreprocessedEntityID
1046    findBeginPreprocessedEntity(SourceLocation BLoc) const;
1047
1048  /// \brief Returns the first preprocessed entity ID that begins after ELoc.
1049  serialization::PreprocessedEntityID
1050    findEndPreprocessedEntity(SourceLocation ELoc) const;
1051
1052  /// \brief Find the next module that contains entities and return the ID
1053  /// of the first entry.
1054  ///
1055  /// \param SLocMapI points at a chunk of a module that contains no
1056  /// preprocessed entities or the entities it contains are not the
1057  /// ones we are looking for.
1058  serialization::PreprocessedEntityID
1059    findNextPreprocessedEntity(
1060                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const;
1061
1062  /// \brief Returns (ModuleFile, Local index) pair for \p GlobalIndex of a
1063  /// preprocessed entity.
1064  std::pair<ModuleFile *, unsigned>
1065    getModulePreprocessedEntity(unsigned GlobalIndex);
1066
1067  /// \brief Returns (begin, end) pair for the preprocessed entities of a
1068  /// particular module.
1069  std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
1070    getModulePreprocessedEntities(ModuleFile &Mod) const;
1071
1072  class ModuleDeclIterator {
1073    ASTReader *Reader;
1074    ModuleFile *Mod;
1075    const serialization::LocalDeclID *Pos;
1076
1077  public:
1078    typedef const Decl *value_type;
1079    typedef value_type&         reference;
1080    typedef value_type*         pointer;
1081
1082    ModuleDeclIterator() : Reader(0), Mod(0), Pos(0) { }
1083
1084    ModuleDeclIterator(ASTReader *Reader, ModuleFile *Mod,
1085                       const serialization::LocalDeclID *Pos)
1086      : Reader(Reader), Mod(Mod), Pos(Pos) { }
1087
1088    value_type operator*() const {
1089      return Reader->GetDecl(Reader->getGlobalDeclID(*Mod, *Pos));
1090    }
1091
1092    ModuleDeclIterator &operator++() {
1093      ++Pos;
1094      return *this;
1095    }
1096
1097    ModuleDeclIterator operator++(int) {
1098      ModuleDeclIterator Prev(*this);
1099      ++Pos;
1100      return Prev;
1101    }
1102
1103    ModuleDeclIterator &operator--() {
1104      --Pos;
1105      return *this;
1106    }
1107
1108    ModuleDeclIterator operator--(int) {
1109      ModuleDeclIterator Prev(*this);
1110      --Pos;
1111      return Prev;
1112    }
1113
1114    friend bool operator==(const ModuleDeclIterator &LHS,
1115                           const ModuleDeclIterator &RHS) {
1116      assert(LHS.Reader == RHS.Reader && LHS.Mod == RHS.Mod);
1117      return LHS.Pos == RHS.Pos;
1118    }
1119
1120    friend bool operator!=(const ModuleDeclIterator &LHS,
1121                           const ModuleDeclIterator &RHS) {
1122      assert(LHS.Reader == RHS.Reader && LHS.Mod == RHS.Mod);
1123      return LHS.Pos != RHS.Pos;
1124    }
1125  };
1126
1127  std::pair<ModuleDeclIterator, ModuleDeclIterator>
1128    getModuleFileLevelDecls(ModuleFile &Mod);
1129
1130  void PassInterestingDeclsToConsumer();
1131  void PassInterestingDeclToConsumer(Decl *D);
1132
1133  void finishPendingActions();
1134
1135  void pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name);
1136
1137  void addPendingDeclContextInfo(Decl *D,
1138                                 serialization::GlobalDeclID SemaDC,
1139                                 serialization::GlobalDeclID LexicalDC) {
1140    assert(D);
1141    PendingDeclContextInfo Info = { D, SemaDC, LexicalDC };
1142    PendingDeclContextInfos.push_back(Info);
1143  }
1144
1145  /// \brief Produce an error diagnostic and return true.
1146  ///
1147  /// This routine should only be used for fatal errors that have to
1148  /// do with non-routine failures (e.g., corrupted AST file).
1149  void Error(StringRef Msg);
1150  void Error(unsigned DiagID, StringRef Arg1 = StringRef(),
1151             StringRef Arg2 = StringRef());
1152
1153  ASTReader(const ASTReader &) LLVM_DELETED_FUNCTION;
1154  void operator=(const ASTReader &) LLVM_DELETED_FUNCTION;
1155public:
1156  /// \brief Load the AST file and validate its contents against the given
1157  /// Preprocessor.
1158  ///
1159  /// \param PP the preprocessor associated with the context in which this
1160  /// precompiled header will be loaded.
1161  ///
1162  /// \param Context the AST context that this precompiled header will be
1163  /// loaded into.
1164  ///
1165  /// \param isysroot If non-NULL, the system include path specified by the
1166  /// user. This is only used with relocatable PCH files. If non-NULL,
1167  /// a relocatable PCH file will use the default path "/".
1168  ///
1169  /// \param DisableValidation If true, the AST reader will suppress most
1170  /// of its regular consistency checking, allowing the use of precompiled
1171  /// headers that cannot be determined to be compatible.
1172  ///
1173  /// \param AllowASTWithCompilerErrors If true, the AST reader will accept an
1174  /// AST file the was created out of an AST with compiler errors,
1175  /// otherwise it will reject it.
1176  ///
1177  /// \param UseGlobalIndex If true, the AST reader will try to load and use
1178  /// the global module index.
1179  ASTReader(Preprocessor &PP, ASTContext &Context, StringRef isysroot = "",
1180            bool DisableValidation = false,
1181            bool AllowASTWithCompilerErrors = false,
1182            bool UseGlobalIndex = true);
1183
1184  ~ASTReader();
1185
1186  SourceManager &getSourceManager() const { return SourceMgr; }
1187  FileManager &getFileManager() const { return FileMgr; }
1188
1189  /// \brief Flags that indicate what kind of AST loading failures the client
1190  /// of the AST reader can directly handle.
1191  ///
1192  /// When a client states that it can handle a particular kind of failure,
1193  /// the AST reader will not emit errors when producing that kind of failure.
1194  enum LoadFailureCapabilities {
1195    /// \brief The client can't handle any AST loading failures.
1196    ARR_None = 0,
1197    /// \brief The client can handle an AST file that cannot load because it
1198    /// is missing.
1199    ARR_Missing = 0x1,
1200    /// \brief The client can handle an AST file that cannot load because it
1201    /// is out-of-date relative to its input files.
1202    ARR_OutOfDate = 0x2,
1203    /// \brief The client can handle an AST file that cannot load because it
1204    /// was built with a different version of Clang.
1205    ARR_VersionMismatch = 0x4,
1206    /// \brief The client can handle an AST file that cannot load because it's
1207    /// compiled configuration doesn't match that of the context it was
1208    /// loaded into.
1209    ARR_ConfigurationMismatch = 0x8
1210  };
1211
1212  /// \brief Load the AST file designated by the given file name.
1213  ///
1214  /// \param FileName The name of the AST file to load.
1215  ///
1216  /// \param Type The kind of AST being loaded, e.g., PCH, module, main file,
1217  /// or preamble.
1218  ///
1219  /// \param ImportLoc the location where the module file will be considered as
1220  /// imported from. For non-module AST types it should be invalid.
1221  ///
1222  /// \param ClientLoadCapabilities The set of client load-failure
1223  /// capabilities, represented as a bitset of the enumerators of
1224  /// LoadFailureCapabilities.
1225  ASTReadResult ReadAST(const std::string &FileName, ModuleKind Type,
1226                        SourceLocation ImportLoc,
1227                        unsigned ClientLoadCapabilities);
1228
1229  /// \brief Make the entities in the given module and any of its (non-explicit)
1230  /// submodules visible to name lookup.
1231  ///
1232  /// \param Mod The module whose names should be made visible.
1233  ///
1234  /// \param NameVisibility The level of visibility to give the names in the
1235  /// module.  Visibility can only be increased over time.
1236  ///
1237  /// \param ImportLoc The location at which the import occurs.
1238  ///
1239  /// \param Complain Whether to complain about conflicting module imports.
1240  void makeModuleVisible(Module *Mod,
1241                         Module::NameVisibilityKind NameVisibility,
1242                         SourceLocation ImportLoc,
1243                         bool Complain);
1244
1245  /// \brief Make the names within this set of hidden names visible.
1246  void makeNamesVisible(const HiddenNames &Names, Module *Owner);
1247
1248  /// \brief Set the AST callbacks listener.
1249  void setListener(ASTReaderListener *listener) {
1250    Listener.reset(listener);
1251  }
1252
1253  /// \brief Set the AST deserialization listener.
1254  void setDeserializationListener(ASTDeserializationListener *Listener);
1255
1256  /// \brief Determine whether this AST reader has a global index.
1257  bool hasGlobalIndex() const { return GlobalIndex.isValid(); }
1258
1259  /// \brief Attempts to load the global index.
1260  ///
1261  /// \returns true if loading the global index has failed for any reason.
1262  bool loadGlobalIndex();
1263
1264  /// \brief Determine whether we tried to load the global index, but failed,
1265  /// e.g., because it is out-of-date or does not exist.
1266  bool isGlobalIndexUnavailable() const;
1267
1268  /// \brief Initializes the ASTContext
1269  void InitializeContext();
1270
1271  /// \brief Update the state of Sema after loading some additional modules.
1272  void UpdateSema();
1273
1274  /// \brief Add in-memory (virtual file) buffer.
1275  void addInMemoryBuffer(StringRef &FileName, llvm::MemoryBuffer *Buffer) {
1276    ModuleMgr.addInMemoryBuffer(FileName, Buffer);
1277  }
1278
1279  /// \brief Finalizes the AST reader's state before writing an AST file to
1280  /// disk.
1281  ///
1282  /// This operation may undo temporary state in the AST that should not be
1283  /// emitted.
1284  void finalizeForWriting();
1285
1286  /// \brief Retrieve the module manager.
1287  ModuleManager &getModuleManager() { return ModuleMgr; }
1288
1289  /// \brief Retrieve the preprocessor.
1290  Preprocessor &getPreprocessor() const { return PP; }
1291
1292  /// \brief Retrieve the name of the original source file name for the primary
1293  /// module file.
1294  StringRef getOriginalSourceFile() {
1295    return ModuleMgr.getPrimaryModule().OriginalSourceFileName;
1296  }
1297
1298  /// \brief Retrieve the name of the original source file name directly from
1299  /// the AST file, without actually loading the AST file.
1300  static std::string getOriginalSourceFile(const std::string &ASTFileName,
1301                                           FileManager &FileMgr,
1302                                           DiagnosticsEngine &Diags);
1303
1304  /// \brief Read the control block for the named AST file.
1305  ///
1306  /// \returns true if an error occurred, false otherwise.
1307  static bool readASTFileControlBlock(StringRef Filename,
1308                                      FileManager &FileMgr,
1309                                      ASTReaderListener &Listener);
1310
1311  /// \brief Determine whether the given AST file is acceptable to load into a
1312  /// translation unit with the given language and target options.
1313  static bool isAcceptableASTFile(StringRef Filename,
1314                                  FileManager &FileMgr,
1315                                  const LangOptions &LangOpts,
1316                                  const TargetOptions &TargetOpts,
1317                                  const PreprocessorOptions &PPOpts);
1318
1319  /// \brief Returns the suggested contents of the predefines buffer,
1320  /// which contains a (typically-empty) subset of the predefines
1321  /// build prior to including the precompiled header.
1322  const std::string &getSuggestedPredefines() { return SuggestedPredefines; }
1323
1324  /// \brief Read a preallocated preprocessed entity from the external source.
1325  ///
1326  /// \returns null if an error occurred that prevented the preprocessed
1327  /// entity from being loaded.
1328  virtual PreprocessedEntity *ReadPreprocessedEntity(unsigned Index);
1329
1330  /// \brief Returns a pair of [Begin, End) indices of preallocated
1331  /// preprocessed entities that \p Range encompasses.
1332  virtual std::pair<unsigned, unsigned>
1333      findPreprocessedEntitiesInRange(SourceRange Range);
1334
1335  /// \brief Optionally returns true or false if the preallocated preprocessed
1336  /// entity with index \p Index came from file \p FID.
1337  virtual Optional<bool> isPreprocessedEntityInFileID(unsigned Index,
1338                                                      FileID FID);
1339
1340  /// \brief Read the header file information for the given file entry.
1341  virtual HeaderFileInfo GetHeaderFileInfo(const FileEntry *FE);
1342
1343  void ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag);
1344
1345  /// \brief Returns the number of source locations found in the chain.
1346  unsigned getTotalNumSLocs() const {
1347    return TotalNumSLocEntries;
1348  }
1349
1350  /// \brief Returns the number of identifiers found in the chain.
1351  unsigned getTotalNumIdentifiers() const {
1352    return static_cast<unsigned>(IdentifiersLoaded.size());
1353  }
1354
1355  /// \brief Returns the number of macros found in the chain.
1356  unsigned getTotalNumMacros() const {
1357    return static_cast<unsigned>(MacrosLoaded.size());
1358  }
1359
1360  /// \brief Returns the number of types found in the chain.
1361  unsigned getTotalNumTypes() const {
1362    return static_cast<unsigned>(TypesLoaded.size());
1363  }
1364
1365  /// \brief Returns the number of declarations found in the chain.
1366  unsigned getTotalNumDecls() const {
1367    return static_cast<unsigned>(DeclsLoaded.size());
1368  }
1369
1370  /// \brief Returns the number of submodules known.
1371  unsigned getTotalNumSubmodules() const {
1372    return static_cast<unsigned>(SubmodulesLoaded.size());
1373  }
1374
1375  /// \brief Returns the number of selectors found in the chain.
1376  unsigned getTotalNumSelectors() const {
1377    return static_cast<unsigned>(SelectorsLoaded.size());
1378  }
1379
1380  /// \brief Returns the number of preprocessed entities known to the AST
1381  /// reader.
1382  unsigned getTotalNumPreprocessedEntities() const {
1383    unsigned Result = 0;
1384    for (ModuleConstIterator I = ModuleMgr.begin(),
1385        E = ModuleMgr.end(); I != E; ++I) {
1386      Result += (*I)->NumPreprocessedEntities;
1387    }
1388
1389    return Result;
1390  }
1391
1392  /// \brief Returns the number of C++ base specifiers found in the chain.
1393  unsigned getTotalNumCXXBaseSpecifiers() const {
1394    return NumCXXBaseSpecifiersLoaded;
1395  }
1396
1397  /// \brief Reads a TemplateArgumentLocInfo appropriate for the
1398  /// given TemplateArgument kind.
1399  TemplateArgumentLocInfo
1400  GetTemplateArgumentLocInfo(ModuleFile &F, TemplateArgument::ArgKind Kind,
1401                             const RecordData &Record, unsigned &Idx);
1402
1403  /// \brief Reads a TemplateArgumentLoc.
1404  TemplateArgumentLoc
1405  ReadTemplateArgumentLoc(ModuleFile &F,
1406                          const RecordData &Record, unsigned &Idx);
1407
1408  const ASTTemplateArgumentListInfo*
1409  ReadASTTemplateArgumentListInfo(ModuleFile &F,
1410                                  const RecordData &Record, unsigned &Index);
1411
1412  /// \brief Reads a declarator info from the given record.
1413  TypeSourceInfo *GetTypeSourceInfo(ModuleFile &F,
1414                                    const RecordData &Record, unsigned &Idx);
1415
1416  /// \brief Resolve a type ID into a type, potentially building a new
1417  /// type.
1418  QualType GetType(serialization::TypeID ID);
1419
1420  /// \brief Resolve a local type ID within a given AST file into a type.
1421  QualType getLocalType(ModuleFile &F, unsigned LocalID);
1422
1423  /// \brief Map a local type ID within a given AST file into a global type ID.
1424  serialization::TypeID getGlobalTypeID(ModuleFile &F, unsigned LocalID) const;
1425
1426  /// \brief Read a type from the current position in the given record, which
1427  /// was read from the given AST file.
1428  QualType readType(ModuleFile &F, const RecordData &Record, unsigned &Idx) {
1429    if (Idx >= Record.size())
1430      return QualType();
1431
1432    return getLocalType(F, Record[Idx++]);
1433  }
1434
1435  /// \brief Map from a local declaration ID within a given module to a
1436  /// global declaration ID.
1437  serialization::DeclID getGlobalDeclID(ModuleFile &F,
1438                                      serialization::LocalDeclID LocalID) const;
1439
1440  /// \brief Returns true if global DeclID \p ID originated from module \p M.
1441  bool isDeclIDFromModule(serialization::GlobalDeclID ID, ModuleFile &M) const;
1442
1443  /// \brief Retrieve the module file that owns the given declaration, or NULL
1444  /// if the declaration is not from a module file.
1445  ModuleFile *getOwningModuleFile(const Decl *D);
1446
1447  /// \brief Returns the source location for the decl \p ID.
1448  SourceLocation getSourceLocationForDeclID(serialization::GlobalDeclID ID);
1449
1450  /// \brief Resolve a declaration ID into a declaration, potentially
1451  /// building a new declaration.
1452  Decl *GetDecl(serialization::DeclID ID);
1453  virtual Decl *GetExternalDecl(uint32_t ID);
1454
1455  /// \brief Reads a declaration with the given local ID in the given module.
1456  Decl *GetLocalDecl(ModuleFile &F, uint32_t LocalID) {
1457    return GetDecl(getGlobalDeclID(F, LocalID));
1458  }
1459
1460  /// \brief Reads a declaration with the given local ID in the given module.
1461  ///
1462  /// \returns The requested declaration, casted to the given return type.
1463  template<typename T>
1464  T *GetLocalDeclAs(ModuleFile &F, uint32_t LocalID) {
1465    return cast_or_null<T>(GetLocalDecl(F, LocalID));
1466  }
1467
1468  /// \brief Map a global declaration ID into the declaration ID used to
1469  /// refer to this declaration within the given module fule.
1470  ///
1471  /// \returns the global ID of the given declaration as known in the given
1472  /// module file.
1473  serialization::DeclID
1474  mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
1475                                  serialization::DeclID GlobalID);
1476
1477  /// \brief Reads a declaration ID from the given position in a record in the
1478  /// given module.
1479  ///
1480  /// \returns The declaration ID read from the record, adjusted to a global ID.
1481  serialization::DeclID ReadDeclID(ModuleFile &F, const RecordData &Record,
1482                                   unsigned &Idx);
1483
1484  /// \brief Reads a declaration from the given position in a record in the
1485  /// given module.
1486  Decl *ReadDecl(ModuleFile &F, const RecordData &R, unsigned &I) {
1487    return GetDecl(ReadDeclID(F, R, I));
1488  }
1489
1490  /// \brief Reads a declaration from the given position in a record in the
1491  /// given module.
1492  ///
1493  /// \returns The declaration read from this location, casted to the given
1494  /// result type.
1495  template<typename T>
1496  T *ReadDeclAs(ModuleFile &F, const RecordData &R, unsigned &I) {
1497    return cast_or_null<T>(GetDecl(ReadDeclID(F, R, I)));
1498  }
1499
1500  /// \brief Read a CXXBaseSpecifiers ID form the given record and
1501  /// return its global bit offset.
1502  uint64_t readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
1503                                 unsigned &Idx);
1504
1505  virtual CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset);
1506
1507  /// \brief Resolve the offset of a statement into a statement.
1508  ///
1509  /// This operation will read a new statement from the external
1510  /// source each time it is called, and is meant to be used via a
1511  /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
1512  virtual Stmt *GetExternalDeclStmt(uint64_t Offset);
1513
1514  /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1515  /// specified cursor.  Read the abbreviations that are at the top of the block
1516  /// and then leave the cursor pointing into the block.
1517  bool ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor, unsigned BlockID);
1518
1519  /// \brief Finds all the visible declarations with a given name.
1520  /// The current implementation of this method just loads the entire
1521  /// lookup table as unmaterialized references.
1522  virtual bool
1523  FindExternalVisibleDeclsByName(const DeclContext *DC,
1524                                 DeclarationName Name);
1525
1526  /// \brief Read all of the declarations lexically stored in a
1527  /// declaration context.
1528  ///
1529  /// \param DC The declaration context whose declarations will be
1530  /// read.
1531  ///
1532  /// \param Decls Vector that will contain the declarations loaded
1533  /// from the external source. The caller is responsible for merging
1534  /// these declarations with any declarations already stored in the
1535  /// declaration context.
1536  ///
1537  /// \returns true if there was an error while reading the
1538  /// declarations for this declaration context.
1539  virtual ExternalLoadResult FindExternalLexicalDecls(const DeclContext *DC,
1540                                        bool (*isKindWeWant)(Decl::Kind),
1541                                        SmallVectorImpl<Decl*> &Decls);
1542
1543  /// \brief Get the decls that are contained in a file in the Offset/Length
1544  /// range. \p Length can be 0 to indicate a point at \p Offset instead of
1545  /// a range.
1546  virtual void FindFileRegionDecls(FileID File, unsigned Offset,unsigned Length,
1547                                   SmallVectorImpl<Decl *> &Decls);
1548
1549  /// \brief Notify ASTReader that we started deserialization of
1550  /// a decl or type so until FinishedDeserializing is called there may be
1551  /// decls that are initializing. Must be paired with FinishedDeserializing.
1552  virtual void StartedDeserializing() { ++NumCurrentElementsDeserializing; }
1553
1554  /// \brief Notify ASTReader that we finished the deserialization of
1555  /// a decl or type. Must be paired with StartedDeserializing.
1556  virtual void FinishedDeserializing();
1557
1558  /// \brief Function that will be invoked when we begin parsing a new
1559  /// translation unit involving this external AST source.
1560  ///
1561  /// This function will provide all of the external definitions to
1562  /// the ASTConsumer.
1563  virtual void StartTranslationUnit(ASTConsumer *Consumer);
1564
1565  /// \brief Print some statistics about AST usage.
1566  virtual void PrintStats();
1567
1568  /// \brief Dump information about the AST reader to standard error.
1569  void dump();
1570
1571  /// Return the amount of memory used by memory buffers, breaking down
1572  /// by heap-backed versus mmap'ed memory.
1573  virtual void getMemoryBufferSizes(MemoryBufferSizes &sizes) const;
1574
1575  /// \brief Initialize the semantic source with the Sema instance
1576  /// being used to perform semantic analysis on the abstract syntax
1577  /// tree.
1578  virtual void InitializeSema(Sema &S);
1579
1580  /// \brief Inform the semantic consumer that Sema is no longer available.
1581  virtual void ForgetSema() { SemaObj = 0; }
1582
1583  /// \brief Retrieve the IdentifierInfo for the named identifier.
1584  ///
1585  /// This routine builds a new IdentifierInfo for the given identifier. If any
1586  /// declarations with this name are visible from translation unit scope, their
1587  /// declarations will be deserialized and introduced into the declaration
1588  /// chain of the identifier.
1589  virtual IdentifierInfo *get(const char *NameStart, const char *NameEnd);
1590  IdentifierInfo *get(StringRef Name) {
1591    return get(Name.begin(), Name.end());
1592  }
1593
1594  /// \brief Retrieve an iterator into the set of all identifiers
1595  /// in all loaded AST files.
1596  virtual IdentifierIterator *getIdentifiers();
1597
1598  /// \brief Load the contents of the global method pool for a given
1599  /// selector.
1600  virtual void ReadMethodPool(Selector Sel);
1601
1602  /// \brief Load the set of namespaces that are known to the external source,
1603  /// which will be used during typo correction.
1604  virtual void ReadKnownNamespaces(
1605                           SmallVectorImpl<NamespaceDecl *> &Namespaces);
1606
1607  virtual void ReadUndefinedButUsed(
1608                        llvm::DenseMap<NamedDecl *, SourceLocation> &Undefined);
1609
1610  virtual void ReadTentativeDefinitions(
1611                 SmallVectorImpl<VarDecl *> &TentativeDefs);
1612
1613  virtual void ReadUnusedFileScopedDecls(
1614                 SmallVectorImpl<const DeclaratorDecl *> &Decls);
1615
1616  virtual void ReadDelegatingConstructors(
1617                 SmallVectorImpl<CXXConstructorDecl *> &Decls);
1618
1619  virtual void ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls);
1620
1621  virtual void ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls);
1622
1623  virtual void ReadLocallyScopedExternCDecls(
1624                 SmallVectorImpl<NamedDecl *> &Decls);
1625
1626  virtual void ReadReferencedSelectors(
1627                 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels);
1628
1629  virtual void ReadWeakUndeclaredIdentifiers(
1630                 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WI);
1631
1632  virtual void ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables);
1633
1634  virtual void ReadPendingInstantiations(
1635                 SmallVectorImpl<std::pair<ValueDecl *,
1636                                           SourceLocation> > &Pending);
1637
1638  virtual void ReadLateParsedTemplates(
1639      llvm::DenseMap<const FunctionDecl *, LateParsedTemplate *> &LPTMap);
1640
1641  /// \brief Load a selector from disk, registering its ID if it exists.
1642  void LoadSelector(Selector Sel);
1643
1644  void SetIdentifierInfo(unsigned ID, IdentifierInfo *II);
1645  void SetGloballyVisibleDecls(IdentifierInfo *II,
1646                               const SmallVectorImpl<uint32_t> &DeclIDs,
1647                               SmallVectorImpl<Decl *> *Decls = 0);
1648
1649  /// \brief Report a diagnostic.
1650  DiagnosticBuilder Diag(unsigned DiagID);
1651
1652  /// \brief Report a diagnostic.
1653  DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID);
1654
1655  IdentifierInfo *DecodeIdentifierInfo(serialization::IdentifierID ID);
1656
1657  IdentifierInfo *GetIdentifierInfo(ModuleFile &M, const RecordData &Record,
1658                                    unsigned &Idx) {
1659    return DecodeIdentifierInfo(getGlobalIdentifierID(M, Record[Idx++]));
1660  }
1661
1662  virtual IdentifierInfo *GetIdentifier(serialization::IdentifierID ID) {
1663    // Note that we are loading an identifier.
1664    Deserializing AnIdentifier(this);
1665
1666    return DecodeIdentifierInfo(ID);
1667  }
1668
1669  IdentifierInfo *getLocalIdentifier(ModuleFile &M, unsigned LocalID);
1670
1671  serialization::IdentifierID getGlobalIdentifierID(ModuleFile &M,
1672                                                    unsigned LocalID);
1673
1674  void resolvePendingMacro(IdentifierInfo *II, const PendingMacroInfo &PMInfo);
1675
1676  void installPCHMacroDirectives(IdentifierInfo *II,
1677                                 ModuleFile &M, uint64_t Offset);
1678
1679  void installImportedMacro(IdentifierInfo *II, MacroDirective *MD,
1680                            Module *Owner);
1681
1682  /// \brief Retrieve the macro with the given ID.
1683  MacroInfo *getMacro(serialization::MacroID ID);
1684
1685  /// \brief Retrieve the global macro ID corresponding to the given local
1686  /// ID within the given module file.
1687  serialization::MacroID getGlobalMacroID(ModuleFile &M, unsigned LocalID);
1688
1689  /// \brief Read the source location entry with index ID.
1690  virtual bool ReadSLocEntry(int ID);
1691
1692  /// \brief Retrieve the module import location and module name for the
1693  /// given source manager entry ID.
1694  virtual std::pair<SourceLocation, StringRef> getModuleImportLoc(int ID);
1695
1696  /// \brief Retrieve the global submodule ID given a module and its local ID
1697  /// number.
1698  serialization::SubmoduleID
1699  getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID);
1700
1701  /// \brief Retrieve the submodule that corresponds to a global submodule ID.
1702  ///
1703  Module *getSubmodule(serialization::SubmoduleID GlobalID);
1704
1705  /// \brief Retrieve the module that corresponds to the given module ID.
1706  ///
1707  /// Note: overrides method in ExternalASTSource
1708  virtual Module *getModule(unsigned ID);
1709
1710  /// \brief Retrieve a selector from the given module with its local ID
1711  /// number.
1712  Selector getLocalSelector(ModuleFile &M, unsigned LocalID);
1713
1714  Selector DecodeSelector(serialization::SelectorID Idx);
1715
1716  virtual Selector GetExternalSelector(serialization::SelectorID ID);
1717  uint32_t GetNumExternalSelectors();
1718
1719  Selector ReadSelector(ModuleFile &M, const RecordData &Record, unsigned &Idx) {
1720    return getLocalSelector(M, Record[Idx++]);
1721  }
1722
1723  /// \brief Retrieve the global selector ID that corresponds to this
1724  /// the local selector ID in a given module.
1725  serialization::SelectorID getGlobalSelectorID(ModuleFile &F,
1726                                                unsigned LocalID) const;
1727
1728  /// \brief Read a declaration name.
1729  DeclarationName ReadDeclarationName(ModuleFile &F,
1730                                      const RecordData &Record, unsigned &Idx);
1731  void ReadDeclarationNameLoc(ModuleFile &F,
1732                              DeclarationNameLoc &DNLoc, DeclarationName Name,
1733                              const RecordData &Record, unsigned &Idx);
1734  void ReadDeclarationNameInfo(ModuleFile &F, DeclarationNameInfo &NameInfo,
1735                               const RecordData &Record, unsigned &Idx);
1736
1737  void ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
1738                         const RecordData &Record, unsigned &Idx);
1739
1740  NestedNameSpecifier *ReadNestedNameSpecifier(ModuleFile &F,
1741                                               const RecordData &Record,
1742                                               unsigned &Idx);
1743
1744  NestedNameSpecifierLoc ReadNestedNameSpecifierLoc(ModuleFile &F,
1745                                                    const RecordData &Record,
1746                                                    unsigned &Idx);
1747
1748  /// \brief Read a template name.
1749  TemplateName ReadTemplateName(ModuleFile &F, const RecordData &Record,
1750                                unsigned &Idx);
1751
1752  /// \brief Read a template argument.
1753  TemplateArgument ReadTemplateArgument(ModuleFile &F,
1754                                        const RecordData &Record,unsigned &Idx);
1755
1756  /// \brief Read a template parameter list.
1757  TemplateParameterList *ReadTemplateParameterList(ModuleFile &F,
1758                                                   const RecordData &Record,
1759                                                   unsigned &Idx);
1760
1761  /// \brief Read a template argument array.
1762  void
1763  ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
1764                           ModuleFile &F, const RecordData &Record,
1765                           unsigned &Idx);
1766
1767  /// \brief Read a UnresolvedSet structure.
1768  void ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
1769                         const RecordData &Record, unsigned &Idx);
1770
1771  /// \brief Read a C++ base specifier.
1772  CXXBaseSpecifier ReadCXXBaseSpecifier(ModuleFile &F,
1773                                        const RecordData &Record,unsigned &Idx);
1774
1775  /// \brief Read a CXXCtorInitializer array.
1776  std::pair<CXXCtorInitializer **, unsigned>
1777  ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
1778                          unsigned &Idx);
1779
1780  /// \brief Read a source location from raw form.
1781  SourceLocation ReadSourceLocation(ModuleFile &ModuleFile, unsigned Raw) const {
1782    SourceLocation Loc = SourceLocation::getFromRawEncoding(Raw);
1783    assert(ModuleFile.SLocRemap.find(Loc.getOffset()) != ModuleFile.SLocRemap.end() &&
1784           "Cannot find offset to remap.");
1785    int Remap = ModuleFile.SLocRemap.find(Loc.getOffset())->second;
1786    return Loc.getLocWithOffset(Remap);
1787  }
1788
1789  /// \brief Read a source location.
1790  SourceLocation ReadSourceLocation(ModuleFile &ModuleFile,
1791                                    const RecordDataImpl &Record,
1792                                    unsigned &Idx) {
1793    return ReadSourceLocation(ModuleFile, Record[Idx++]);
1794  }
1795
1796  /// \brief Read a source range.
1797  SourceRange ReadSourceRange(ModuleFile &F,
1798                              const RecordData &Record, unsigned &Idx);
1799
1800  /// \brief Read an integral value
1801  llvm::APInt ReadAPInt(const RecordData &Record, unsigned &Idx);
1802
1803  /// \brief Read a signed integral value
1804  llvm::APSInt ReadAPSInt(const RecordData &Record, unsigned &Idx);
1805
1806  /// \brief Read a floating-point value
1807  llvm::APFloat ReadAPFloat(const RecordData &Record,
1808                            const llvm::fltSemantics &Sem, unsigned &Idx);
1809
1810  // \brief Read a string
1811  static std::string ReadString(const RecordData &Record, unsigned &Idx);
1812
1813  /// \brief Read a version tuple.
1814  static VersionTuple ReadVersionTuple(const RecordData &Record, unsigned &Idx);
1815
1816  CXXTemporary *ReadCXXTemporary(ModuleFile &F, const RecordData &Record,
1817                                 unsigned &Idx);
1818
1819  /// \brief Reads attributes from the current stream position.
1820  void ReadAttributes(ModuleFile &F, AttrVec &Attrs,
1821                      const RecordData &Record, unsigned &Idx);
1822
1823  /// \brief Reads a statement.
1824  Stmt *ReadStmt(ModuleFile &F);
1825
1826  /// \brief Reads an expression.
1827  Expr *ReadExpr(ModuleFile &F);
1828
1829  /// \brief Reads a sub-statement operand during statement reading.
1830  Stmt *ReadSubStmt() {
1831    assert(ReadingKind == Read_Stmt &&
1832           "Should be called only during statement reading!");
1833    // Subexpressions are stored from last to first, so the next Stmt we need
1834    // is at the back of the stack.
1835    assert(!StmtStack.empty() && "Read too many sub statements!");
1836    return StmtStack.pop_back_val();
1837  }
1838
1839  /// \brief Reads a sub-expression operand during statement reading.
1840  Expr *ReadSubExpr();
1841
1842  /// \brief Reads a token out of a record.
1843  Token ReadToken(ModuleFile &M, const RecordDataImpl &Record, unsigned &Idx);
1844
1845  /// \brief Reads the macro record located at the given offset.
1846  MacroInfo *ReadMacroRecord(ModuleFile &F, uint64_t Offset);
1847
1848  /// \brief Determine the global preprocessed entity ID that corresponds to
1849  /// the given local ID within the given module.
1850  serialization::PreprocessedEntityID
1851  getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const;
1852
1853  /// \brief Add a macro to resolve imported from a module.
1854  ///
1855  /// \param II The name of the macro.
1856  /// \param M The module file.
1857  /// \param GMacID The global macro ID that is associated with this identifier.
1858  /// \param ImportLoc The location where the module is imported.
1859  void addPendingMacroFromModule(IdentifierInfo *II,
1860                                 ModuleFile *M,
1861                                 serialization::GlobalMacroID GMacID,
1862                                 SourceLocation ImportLoc);
1863
1864  /// \brief Add a macro to deserialize its macro directive history from a PCH.
1865  ///
1866  /// \param II The name of the macro.
1867  /// \param M The module file.
1868  /// \param MacroDirectivesOffset Offset of the serialized macro directive
1869  /// history.
1870  void addPendingMacroFromPCH(IdentifierInfo *II,
1871                              ModuleFile *M, uint64_t MacroDirectivesOffset);
1872
1873  /// \brief Read the set of macros defined by this external macro source.
1874  virtual void ReadDefinedMacros();
1875
1876  /// \brief Update an out-of-date identifier.
1877  virtual void updateOutOfDateIdentifier(IdentifierInfo &II);
1878
1879  /// \brief Note that this identifier is up-to-date.
1880  void markIdentifierUpToDate(IdentifierInfo *II);
1881
1882  /// \brief Load all external visible decls in the given DeclContext.
1883  void completeVisibleDeclsMap(const DeclContext *DC);
1884
1885  /// \brief Retrieve the AST context that this AST reader supplements.
1886  ASTContext &getContext() { return Context; }
1887
1888  // \brief Contains declarations that were loaded before we have
1889  // access to a Sema object.
1890  SmallVector<NamedDecl *, 16> PreloadedDecls;
1891
1892  /// \brief Retrieve the semantic analysis object used to analyze the
1893  /// translation unit in which the precompiled header is being
1894  /// imported.
1895  Sema *getSema() { return SemaObj; }
1896
1897  /// \brief Retrieve the identifier table associated with the
1898  /// preprocessor.
1899  IdentifierTable &getIdentifierTable();
1900
1901  /// \brief Record that the given ID maps to the given switch-case
1902  /// statement.
1903  void RecordSwitchCaseID(SwitchCase *SC, unsigned ID);
1904
1905  /// \brief Retrieve the switch-case statement with the given ID.
1906  SwitchCase *getSwitchCaseWithID(unsigned ID);
1907
1908  void ClearSwitchCaseIDs();
1909
1910  /// \brief Cursors for comments blocks.
1911  SmallVector<std::pair<llvm::BitstreamCursor,
1912                        serialization::ModuleFile *>, 8> CommentsCursors;
1913
1914  /// \brief Loads comments ranges.
1915  void ReadComments();
1916};
1917
1918/// \brief Helper class that saves the current stream position and
1919/// then restores it when destroyed.
1920struct SavedStreamPosition {
1921  explicit SavedStreamPosition(llvm::BitstreamCursor &Cursor)
1922    : Cursor(Cursor), Offset(Cursor.GetCurrentBitNo()) { }
1923
1924  ~SavedStreamPosition() {
1925    Cursor.JumpToBit(Offset);
1926  }
1927
1928private:
1929  llvm::BitstreamCursor &Cursor;
1930  uint64_t Offset;
1931};
1932
1933inline void PCHValidator::Error(const char *Msg) {
1934  Reader.Error(Msg);
1935}
1936
1937} // end namespace clang
1938
1939#endif
1940