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