ASTReader.h revision 321369
1212795Sdim//===--- ASTReader.h - AST File Reader --------------------------*- C++ -*-===//
2212795Sdim//
3212795Sdim//                     The LLVM Compiler Infrastructure
4212795Sdim//
5212795Sdim// This file is distributed under the University of Illinois Open Source
6212795Sdim// License. See LICENSE.TXT for details.
7212795Sdim//
8212795Sdim//===----------------------------------------------------------------------===//
9212795Sdim//
10212795Sdim//  This file defines the ASTReader class, which reads AST files.
11212795Sdim//
12212795Sdim//===----------------------------------------------------------------------===//
13212795Sdim
14280031Sdim#ifndef LLVM_CLANG_SERIALIZATION_ASTREADER_H
15280031Sdim#define LLVM_CLANG_SERIALIZATION_ASTREADER_H
16212795Sdim
17249423Sdim#include "clang/AST/DeclObjC.h"
18212795Sdim#include "clang/AST/DeclarationName.h"
19212795Sdim#include "clang/AST/TemplateBase.h"
20212795Sdim#include "clang/Basic/Diagnostic.h"
21226633Sdim#include "clang/Basic/FileSystemOptions.h"
22212795Sdim#include "clang/Basic/IdentifierTable.h"
23249423Sdim#include "clang/Basic/Version.h"
24249423Sdim#include "clang/Lex/ExternalPreprocessorSource.h"
25249423Sdim#include "clang/Lex/HeaderSearch.h"
26249423Sdim#include "clang/Lex/PreprocessingRecord.h"
27249423Sdim#include "clang/Sema/ExternalSemaSource.h"
28309124Sdim#include "clang/Sema/IdentifierResolver.h"
29249423Sdim#include "clang/Serialization/ASTBitCodes.h"
30249423Sdim#include "clang/Serialization/ContinuousRangeMap.h"
31249423Sdim#include "clang/Serialization/Module.h"
32296417Sdim#include "clang/Serialization/ModuleFileExtension.h"
33249423Sdim#include "clang/Serialization/ModuleManager.h"
34243830Sdim#include "llvm/ADT/MapVector.h"
35234353Sdim#include "llvm/ADT/SmallPtrSet.h"
36234353Sdim#include "llvm/ADT/SmallSet.h"
37212795Sdim#include "llvm/ADT/SmallVector.h"
38296417Sdim#include "llvm/ADT/StringMap.h"
39212795Sdim#include "llvm/ADT/StringRef.h"
40276479Sdim#include "llvm/ADT/TinyPtrVector.h"
41218893Sdim#include "llvm/Support/DataTypes.h"
42288943Sdim#include "llvm/Support/Timer.h"
43212795Sdim#include <deque>
44276479Sdim#include <memory>
45212795Sdim#include <string>
46212795Sdim#include <utility>
47212795Sdim#include <vector>
48212795Sdim
49212795Sdimnamespace llvm {
50314564Sdim  class BitstreamCursor;
51212795Sdim  class MemoryBuffer;
52314564Sdim  class APInt;
53314564Sdim  class APSInt;
54314564Sdim  class APFloat;
55212795Sdim}
56212795Sdim
57212795Sdimnamespace clang {
58212795Sdim
59314564Sdimclass SourceManager;
60314564Sdimclass HeaderSearchOptions;
61314564Sdimclass FileManager;
62212795Sdimclass AddrLabelExpr;
63212795Sdimclass ASTConsumer;
64212795Sdimclass ASTContext;
65218893Sdimclass ASTIdentifierIterator;
66226633Sdimclass ASTUnit; // FIXME: Layering violation and egregious hack.
67212795Sdimclass Attr;
68212795Sdimclass Decl;
69212795Sdimclass DeclContext;
70276479Sdimclass DefMacroDirective;
71243830Sdimclass DiagnosticOptions;
72212795Sdimclass NestedNameSpecifier;
73212795Sdimclass CXXBaseSpecifier;
74223017Sdimclass CXXConstructorDecl;
75218893Sdimclass CXXCtorInitializer;
76249423Sdimclass GlobalModuleIndex;
77212795Sdimclass GotoStmt;
78212795Sdimclass MacroDefinition;
79249423Sdimclass MacroDirective;
80288943Sdimclass ModuleMacro;
81212795Sdimclass NamedDecl;
82218893Sdimclass OpaqueValueExpr;
83212795Sdimclass Preprocessor;
84243830Sdimclass PreprocessorOptions;
85212795Sdimclass Sema;
86212795Sdimclass SwitchCase;
87218893Sdimclass ASTDeserializationListener;
88226633Sdimclass ASTWriter;
89212795Sdimclass ASTReader;
90212795Sdimclass ASTDeclReader;
91218893Sdimclass ASTStmtReader;
92314564Sdimclass ASTRecordReader;
93218893Sdimclass TypeLocReader;
94212795Sdimstruct HeaderFileInfo;
95221345Sdimclass VersionTuple;
96243830Sdimclass TargetOptions;
97261991Sdimclass LazyASTUnresolvedSet;
98212795Sdim
99212795Sdim/// \brief Abstract interface for callback invocations by the ASTReader.
100212795Sdim///
101212795Sdim/// While reading an AST file, the ASTReader will call the methods of the
102212795Sdim/// listener to pass on specific information. Some of the listener methods can
103212795Sdim/// return true to indicate to the ASTReader that the information (and
104212795Sdim/// consequently the AST file) is invalid.
105212795Sdimclass ASTReaderListener {
106212795Sdimpublic:
107212795Sdim  virtual ~ASTReaderListener();
108212795Sdim
109249423Sdim  /// \brief Receives the full Clang version information.
110249423Sdim  ///
111249423Sdim  /// \returns true to indicate that the version is invalid. Subclasses should
112249423Sdim  /// generally defer to this implementation.
113249423Sdim  virtual bool ReadFullVersionInformation(StringRef FullVersion) {
114249423Sdim    return FullVersion != getClangFullRepositoryVersion();
115249423Sdim  }
116249423Sdim
117276479Sdim  virtual void ReadModuleName(StringRef ModuleName) {}
118276479Sdim  virtual void ReadModuleMapFile(StringRef ModuleMapPath) {}
119276479Sdim
120212795Sdim  /// \brief Receives the language options.
121212795Sdim  ///
122212795Sdim  /// \returns true to indicate the options are invalid or false otherwise.
123243830Sdim  virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
124280031Sdim                                   bool Complain,
125280031Sdim                                   bool AllowCompatibleDifferences) {
126212795Sdim    return false;
127212795Sdim  }
128212795Sdim
129243830Sdim  /// \brief Receives the target options.
130212795Sdim  ///
131243830Sdim  /// \returns true to indicate the target options are invalid, or false
132243830Sdim  /// otherwise.
133288943Sdim  virtual bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
134288943Sdim                                 bool AllowCompatibleDifferences) {
135212795Sdim    return false;
136212795Sdim  }
137212795Sdim
138243830Sdim  /// \brief Receives the diagnostic options.
139212795Sdim  ///
140243830Sdim  /// \returns true to indicate the diagnostic options are invalid, or false
141243830Sdim  /// otherwise.
142276479Sdim  virtual bool
143276479Sdim  ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,
144276479Sdim                        bool Complain) {
145243830Sdim    return false;
146243830Sdim  }
147243830Sdim
148243830Sdim  /// \brief Receives the file system options.
149212795Sdim  ///
150243830Sdim  /// \returns true to indicate the file system options are invalid, or false
151243830Sdim  /// otherwise.
152243830Sdim  virtual bool ReadFileSystemOptions(const FileSystemOptions &FSOpts,
153243830Sdim                                     bool Complain) {
154243830Sdim    return false;
155243830Sdim  }
156243830Sdim
157243830Sdim  /// \brief Receives the header search options.
158212795Sdim  ///
159243830Sdim  /// \returns true to indicate the header search options are invalid, or false
160243830Sdim  /// otherwise.
161243830Sdim  virtual bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
162288943Sdim                                       StringRef SpecificModuleCachePath,
163243830Sdim                                       bool Complain) {
164243830Sdim    return false;
165243830Sdim  }
166243830Sdim
167243830Sdim  /// \brief Receives the preprocessor options.
168212795Sdim  ///
169243830Sdim  /// \param SuggestedPredefines Can be filled in with the set of predefines
170243830Sdim  /// that are suggested by the preprocessor options. Typically only used when
171243830Sdim  /// loading a precompiled header.
172243830Sdim  ///
173243830Sdim  /// \returns true to indicate the preprocessor options are invalid, or false
174243830Sdim  /// otherwise.
175243830Sdim  virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
176243830Sdim                                       bool Complain,
177243830Sdim                                       std::string &SuggestedPredefines) {
178212795Sdim    return false;
179212795Sdim  }
180212795Sdim
181212795Sdim  /// \brief Receives __COUNTER__ value.
182243830Sdim  virtual void ReadCounter(const serialization::ModuleFile &M,
183243830Sdim                           unsigned Value) {}
184251662Sdim
185276479Sdim  /// This is called for each AST file loaded.
186296417Sdim  virtual void visitModuleFile(StringRef Filename,
187296417Sdim                               serialization::ModuleKind Kind) {}
188276479Sdim
189251662Sdim  /// \brief Returns true if this \c ASTReaderListener wants to receive the
190251662Sdim  /// input files of the AST file via \c visitInputFile, false otherwise.
191251662Sdim  virtual bool needsInputFileVisitation() { return false; }
192276479Sdim  /// \brief Returns true if this \c ASTReaderListener wants to receive the
193276479Sdim  /// system input files of the AST file via \c visitInputFile, false otherwise.
194276479Sdim  virtual bool needsSystemInputFileVisitation() { return false; }
195276479Sdim  /// \brief if \c needsInputFileVisitation returns true, this is called for
196276479Sdim  /// each non-system input file of the AST File. If
197276479Sdim  /// \c needsSystemInputFileVisitation is true, then it is called for all
198276479Sdim  /// system input files as well.
199251662Sdim  ///
200251662Sdim  /// \returns true to continue receiving the next input file, false to stop.
201276479Sdim  virtual bool visitInputFile(StringRef Filename, bool isSystem,
202296417Sdim                              bool isOverridden, bool isExplicitModule) {
203276479Sdim    return true;
204276479Sdim  }
205280031Sdim
206280031Sdim  /// \brief Returns true if this \c ASTReaderListener wants to receive the
207280031Sdim  /// imports of the AST file via \c visitImport, false otherwise.
208280031Sdim  virtual bool needsImportVisitation() const { return false; }
209280031Sdim  /// \brief If needsImportVisitation returns \c true, this is called for each
210280031Sdim  /// AST file imported by this AST file.
211280031Sdim  virtual void visitImport(StringRef Filename) {}
212296417Sdim
213296417Sdim  /// Indicates that a particular module file extension has been read.
214296417Sdim  virtual void readModuleFileExtension(
215296417Sdim                 const ModuleFileExtensionMetadata &Metadata) {}
216212795Sdim};
217212795Sdim
218276479Sdim/// \brief Simple wrapper class for chaining listeners.
219276479Sdimclass ChainedASTReaderListener : public ASTReaderListener {
220276479Sdim  std::unique_ptr<ASTReaderListener> First;
221276479Sdim  std::unique_ptr<ASTReaderListener> Second;
222276479Sdim
223276479Sdimpublic:
224276479Sdim  /// Takes ownership of \p First and \p Second.
225280031Sdim  ChainedASTReaderListener(std::unique_ptr<ASTReaderListener> First,
226280031Sdim                           std::unique_ptr<ASTReaderListener> Second)
227280031Sdim      : First(std::move(First)), Second(std::move(Second)) {}
228276479Sdim
229280031Sdim  std::unique_ptr<ASTReaderListener> takeFirst() { return std::move(First); }
230280031Sdim  std::unique_ptr<ASTReaderListener> takeSecond() { return std::move(Second); }
231280031Sdim
232276479Sdim  bool ReadFullVersionInformation(StringRef FullVersion) override;
233276479Sdim  void ReadModuleName(StringRef ModuleName) override;
234276479Sdim  void ReadModuleMapFile(StringRef ModuleMapPath) override;
235280031Sdim  bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
236280031Sdim                           bool AllowCompatibleDifferences) override;
237288943Sdim  bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
238288943Sdim                         bool AllowCompatibleDifferences) override;
239276479Sdim  bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,
240276479Sdim                             bool Complain) override;
241276479Sdim  bool ReadFileSystemOptions(const FileSystemOptions &FSOpts,
242276479Sdim                             bool Complain) override;
243276479Sdim
244276479Sdim  bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
245288943Sdim                               StringRef SpecificModuleCachePath,
246276479Sdim                               bool Complain) override;
247276479Sdim  bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
248276479Sdim                               bool Complain,
249276479Sdim                               std::string &SuggestedPredefines) override;
250276479Sdim
251276479Sdim  void ReadCounter(const serialization::ModuleFile &M, unsigned Value) override;
252276479Sdim  bool needsInputFileVisitation() override;
253276479Sdim  bool needsSystemInputFileVisitation() override;
254296417Sdim  void visitModuleFile(StringRef Filename,
255296417Sdim                       serialization::ModuleKind Kind) override;
256276479Sdim  bool visitInputFile(StringRef Filename, bool isSystem,
257296417Sdim                      bool isOverridden, bool isExplicitModule) override;
258296417Sdim  void readModuleFileExtension(
259296417Sdim         const ModuleFileExtensionMetadata &Metadata) override;
260276479Sdim};
261276479Sdim
262212795Sdim/// \brief ASTReaderListener implementation to validate the information of
263212795Sdim/// the PCH file against an initialized Preprocessor.
264212795Sdimclass PCHValidator : public ASTReaderListener {
265212795Sdim  Preprocessor &PP;
266212795Sdim  ASTReader &Reader;
267212795Sdim
268212795Sdimpublic:
269212795Sdim  PCHValidator(Preprocessor &PP, ASTReader &Reader)
270261991Sdim    : PP(PP), Reader(Reader) {}
271212795Sdim
272280031Sdim  bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
273280031Sdim                           bool AllowCompatibleDifferences) override;
274288943Sdim  bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
275288943Sdim                         bool AllowCompatibleDifferences) override;
276276479Sdim  bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,
277276479Sdim                             bool Complain) override;
278276479Sdim  bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, bool Complain,
279276479Sdim                               std::string &SuggestedPredefines) override;
280288943Sdim  bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
281288943Sdim                               StringRef SpecificModuleCachePath,
282288943Sdim                               bool Complain) override;
283276479Sdim  void ReadCounter(const serialization::ModuleFile &M, unsigned Value) override;
284212795Sdim
285212795Sdimprivate:
286212795Sdim  void Error(const char *Msg);
287212795Sdim};
288212795Sdim
289314564Sdim/// \brief ASTReaderListenter implementation to set SuggestedPredefines of
290314564Sdim/// ASTReader which is required to use a pch file. This is the replacement
291314564Sdim/// of PCHValidator or SimplePCHValidator when using a pch file without
292314564Sdim/// validating it.
293314564Sdimclass SimpleASTReaderListener : public ASTReaderListener {
294314564Sdim  Preprocessor &PP;
295314564Sdim
296314564Sdimpublic:
297314564Sdim  SimpleASTReaderListener(Preprocessor &PP)
298314564Sdim    : PP(PP) {}
299314564Sdim
300314564Sdim  bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, bool Complain,
301314564Sdim                               std::string &SuggestedPredefines) override;
302314564Sdim};
303314564Sdim
304234353Sdimnamespace serialization {
305226633Sdim
306226633Sdimclass ReadMethodPoolVisitor;
307234353Sdim
308226633Sdimnamespace reader {
309226633Sdim  class ASTIdentifierLookupTrait;
310296417Sdim  /// \brief The on-disk hash table(s) used for DeclContext name lookup.
311296417Sdim  struct DeclContextLookupTable;
312226633Sdim}
313234353Sdim
314226633Sdim} // end namespace serialization
315234353Sdim
316212795Sdim/// \brief Reads an AST files chain containing the contents of a translation
317212795Sdim/// unit.
318212795Sdim///
319212795Sdim/// The ASTReader class reads bitstreams (produced by the ASTWriter
320212795Sdim/// class) containing the serialized representation of a given
321212795Sdim/// abstract syntax tree and its supporting data structures. An
322212795Sdim/// instance of the ASTReader can be attached to an ASTContext object,
323212795Sdim/// which will provide access to the contents of the AST files.
324212795Sdim///
325212795Sdim/// The AST reader provides lazy de-serialization of declarations, as
326212795Sdim/// required when traversing the AST. Only those AST nodes that are
327212795Sdim/// actually required will be de-serialized.
328212795Sdimclass ASTReader
329212795Sdim  : public ExternalPreprocessorSource,
330212795Sdim    public ExternalPreprocessingRecordSource,
331218893Sdim    public ExternalHeaderFileInfoSource,
332212795Sdim    public ExternalSemaSource,
333212795Sdim    public IdentifierInfoLookup,
334234353Sdim    public ExternalSLocEntrySource
335218893Sdim{
336212795Sdimpublic:
337239462Sdim  typedef SmallVector<uint64_t, 64> RecordData;
338261991Sdim  typedef SmallVectorImpl<uint64_t> RecordDataImpl;
339239462Sdim
340243830Sdim  /// \brief The result of reading the control block of an AST file, which
341243830Sdim  /// can fail for various reasons.
342243830Sdim  enum ASTReadResult {
343243830Sdim    /// \brief The control block was read successfully. Aside from failures,
344243830Sdim    /// the AST file is safe to read into the current context.
345243830Sdim    Success,
346243830Sdim    /// \brief The AST file itself appears corrupted.
347243830Sdim    Failure,
348249423Sdim    /// \brief The AST file was missing.
349249423Sdim    Missing,
350243830Sdim    /// \brief The AST file is out-of-date relative to its input files,
351243830Sdim    /// and needs to be regenerated.
352243830Sdim    OutOfDate,
353243830Sdim    /// \brief The AST file was written by a different version of Clang.
354243830Sdim    VersionMismatch,
355243830Sdim    /// \brief The AST file was writtten with a different language/target
356243830Sdim    /// configuration.
357243830Sdim    ConfigurationMismatch,
358243830Sdim    /// \brief The AST file has errors.
359243830Sdim    HadErrors
360243830Sdim  };
361314564Sdim
362218893Sdim  /// \brief Types of AST files.
363212795Sdim  friend class PCHValidator;
364212795Sdim  friend class ASTDeclReader;
365218893Sdim  friend class ASTStmtReader;
366218893Sdim  friend class ASTIdentifierIterator;
367226633Sdim  friend class serialization::reader::ASTIdentifierLookupTrait;
368218893Sdim  friend class TypeLocReader;
369314564Sdim  friend class ASTRecordReader;
370226633Sdim  friend class ASTWriter;
371226633Sdim  friend class ASTUnit; // ASTUnit needs to remap source locations.
372226633Sdim  friend class serialization::ReadMethodPoolVisitor;
373234353Sdim
374234353Sdim  typedef serialization::ModuleFile ModuleFile;
375226633Sdim  typedef serialization::ModuleKind ModuleKind;
376226633Sdim  typedef serialization::ModuleManager ModuleManager;
377234353Sdim
378226633Sdim  typedef ModuleManager::ModuleIterator ModuleIterator;
379226633Sdim  typedef ModuleManager::ModuleConstIterator ModuleConstIterator;
380226633Sdim  typedef ModuleManager::ModuleReverseIterator ModuleReverseIterator;
381226633Sdim
382212795Sdimprivate:
383212795Sdim  /// \brief The receiver of some callbacks invoked by ASTReader.
384276479Sdim  std::unique_ptr<ASTReaderListener> Listener;
385212795Sdim
386212795Sdim  /// \brief The receiver of deserialization events.
387314564Sdim  ASTDeserializationListener *DeserializationListener = nullptr;
388314564Sdim  bool OwnsDeserializationListener = false;
389212795Sdim
390212795Sdim  SourceManager &SourceMgr;
391212795Sdim  FileManager &FileMgr;
392288943Sdim  const PCHContainerReader &PCHContainerRdr;
393226633Sdim  DiagnosticsEngine &Diags;
394234353Sdim
395212795Sdim  /// \brief The semantic analysis object that will be processing the
396212795Sdim  /// AST files and the translation unit that uses it.
397314564Sdim  Sema *SemaObj = nullptr;
398212795Sdim
399212795Sdim  /// \brief The preprocessor that will be loading the source file.
400226633Sdim  Preprocessor &PP;
401212795Sdim
402212795Sdim  /// \brief The AST context into which we'll read the AST files.
403321369Sdim  ASTContext *ContextObj = nullptr;
404234353Sdim
405212795Sdim  /// \brief The AST consumer.
406314564Sdim  ASTConsumer *Consumer = nullptr;
407212795Sdim
408226633Sdim  /// \brief The module manager which manages modules and their dependencies
409226633Sdim  ModuleManager ModuleMgr;
410221345Sdim
411321369Sdim  /// The cache that manages memory buffers for PCM files.
412321369Sdim  MemoryBufferCache &PCMCache;
413321369Sdim
414309124Sdim  /// \brief A dummy identifier resolver used to merge TU-scope declarations in
415309124Sdim  /// C, for the cases where we don't have a Sema object to provide a real
416309124Sdim  /// identifier resolver.
417309124Sdim  IdentifierResolver DummyIdResolver;
418309124Sdim
419296417Sdim  /// A mapping from extension block names to module file extensions.
420314564Sdim  llvm::StringMap<std::shared_ptr<ModuleFileExtension>> ModuleFileExtensions;
421296417Sdim
422288943Sdim  /// \brief A timer used to track the time spent deserializing.
423288943Sdim  std::unique_ptr<llvm::Timer> ReadTimer;
424288943Sdim
425261991Sdim  /// \brief The location where the module file will be considered as
426261991Sdim  /// imported from. For non-module AST types it should be invalid.
427261991Sdim  SourceLocation CurrentImportLoc;
428261991Sdim
429249423Sdim  /// \brief The global module index, if loaded.
430276479Sdim  std::unique_ptr<GlobalModuleIndex> GlobalIndex;
431249423Sdim
432226633Sdim  /// \brief A map of global bit offsets to the module that stores entities
433226633Sdim  /// at those bit offsets.
434234353Sdim  ContinuousRangeMap<uint64_t, ModuleFile*, 4> GlobalBitOffsetsMap;
435212795Sdim
436226633Sdim  /// \brief A map of negated SLocEntryIDs to the modules containing them.
437234353Sdim  ContinuousRangeMap<unsigned, ModuleFile*, 64> GlobalSLocEntryMap;
438212795Sdim
439234353Sdim  typedef ContinuousRangeMap<unsigned, ModuleFile*, 64> GlobalSLocOffsetMapType;
440234353Sdim
441226633Sdim  /// \brief A map of reversed (SourceManager::MaxLoadedOffset - SLocOffset)
442226633Sdim  /// SourceLocation offsets to the modules containing them.
443226633Sdim  GlobalSLocOffsetMapType GlobalSLocOffsetMap;
444234353Sdim
445212795Sdim  /// \brief Types that have already been loaded from the chain.
446212795Sdim  ///
447212795Sdim  /// When the pointer at index I is non-NULL, the type with
448212795Sdim  /// ID = (I + 1) << FastQual::Width has already been loaded
449212795Sdim  std::vector<QualType> TypesLoaded;
450212795Sdim
451234353Sdim  typedef ContinuousRangeMap<serialization::TypeID, ModuleFile *, 4>
452226633Sdim    GlobalTypeMapType;
453212795Sdim
454226633Sdim  /// \brief Mapping from global type IDs to the module in which the
455226633Sdim  /// type resides along with the offset that should be added to the
456226633Sdim  /// global type ID to produce a local ID.
457226633Sdim  GlobalTypeMapType GlobalTypeMap;
458226633Sdim
459212795Sdim  /// \brief Declarations that have already been loaded from the chain.
460212795Sdim  ///
461212795Sdim  /// When the pointer at index I is non-NULL, the declaration with ID
462212795Sdim  /// = I + 1 has already been loaded.
463212795Sdim  std::vector<Decl *> DeclsLoaded;
464212795Sdim
465234353Sdim  typedef ContinuousRangeMap<serialization::DeclID, ModuleFile *, 4>
466226633Sdim    GlobalDeclMapType;
467234353Sdim
468226633Sdim  /// \brief Mapping from global declaration IDs to the module in which the
469226633Sdim  /// declaration resides.
470226633Sdim  GlobalDeclMapType GlobalDeclMap;
471234353Sdim
472234353Sdim  typedef std::pair<ModuleFile *, uint64_t> FileOffset;
473226633Sdim  typedef SmallVector<FileOffset, 2> FileOffsetsTy;
474218893Sdim  typedef llvm::DenseMap<serialization::DeclID, FileOffsetsTy>
475218893Sdim      DeclUpdateOffsetsMap;
476234353Sdim
477218893Sdim  /// \brief Declarations that have modifications residing in a later file
478218893Sdim  /// in the chain.
479218893Sdim  DeclUpdateOffsetsMap DeclUpdateOffsets;
480218893Sdim
481321369Sdim  struct PendingUpdateRecord {
482321369Sdim    Decl *D;
483321369Sdim    serialization::GlobalDeclID ID;
484321369Sdim    // Whether the declaration was just deserialized.
485321369Sdim    bool JustLoaded;
486321369Sdim    PendingUpdateRecord(serialization::GlobalDeclID ID, Decl *D,
487321369Sdim                        bool JustLoaded)
488321369Sdim        : D(D), ID(ID), JustLoaded(JustLoaded) {}
489321369Sdim  };
490276479Sdim  /// \brief Declaration updates for already-loaded declarations that we need
491276479Sdim  /// to apply once we finish processing an import.
492321369Sdim  llvm::SmallVector<PendingUpdateRecord, 16> PendingUpdateRecords;
493276479Sdim
494288943Sdim  enum class PendingFakeDefinitionKind { NotFake, Fake, FakeLoaded };
495288943Sdim
496288943Sdim  /// \brief The DefinitionData pointers that we faked up for class definitions
497288943Sdim  /// that we needed but hadn't loaded yet.
498288943Sdim  llvm::DenseMap<void *, PendingFakeDefinitionKind> PendingFakeDefinitionData;
499288943Sdim
500288943Sdim  /// \brief Exception specification updates that have been loaded but not yet
501288943Sdim  /// propagated across the relevant redeclaration chain. The map key is the
502288943Sdim  /// canonical declaration (used only for deduplication) and the value is a
503288943Sdim  /// declaration that has an exception specification.
504288943Sdim  llvm::SmallMapVector<Decl *, FunctionDecl *, 4> PendingExceptionSpecUpdates;
505288943Sdim
506280031Sdim  /// \brief Declarations that have been imported and have typedef names for
507280031Sdim  /// linkage purposes.
508280031Sdim  llvm::DenseMap<std::pair<DeclContext*, IdentifierInfo*>, NamedDecl*>
509280031Sdim      ImportedTypedefNamesForLinkage;
510280031Sdim
511280031Sdim  /// \brief Mergeable declaration contexts that have anonymous declarations
512280031Sdim  /// within them, and those anonymous declarations.
513280031Sdim  llvm::DenseMap<DeclContext*, llvm::SmallVector<NamedDecl*, 2>>
514280031Sdim    AnonymousDeclarationsForMerging;
515280031Sdim
516234353Sdim  struct FileDeclsInfo {
517234353Sdim    ModuleFile *Mod;
518234353Sdim    ArrayRef<serialization::LocalDeclID> Decls;
519234353Sdim
520276479Sdim    FileDeclsInfo() : Mod(nullptr) {}
521234353Sdim    FileDeclsInfo(ModuleFile *Mod, ArrayRef<serialization::LocalDeclID> Decls)
522234353Sdim      : Mod(Mod), Decls(Decls) {}
523234353Sdim  };
524234353Sdim
525234353Sdim  /// \brief Map from a FileID to the file-level declarations that it contains.
526234353Sdim  llvm::DenseMap<FileID, FileDeclsInfo> FileDeclIDs;
527234353Sdim
528296417Sdim  /// \brief An array of lexical contents of a declaration context, as a sequence of
529296417Sdim  /// Decl::Kind, DeclID pairs.
530296417Sdim  typedef ArrayRef<llvm::support::unaligned_uint32_t> LexicalContents;
531296417Sdim
532296417Sdim  /// \brief Map from a DeclContext to its lexical contents.
533296417Sdim  llvm::DenseMap<const DeclContext*, std::pair<ModuleFile*, LexicalContents>>
534296417Sdim      LexicalDecls;
535296417Sdim
536296417Sdim  /// \brief Map from the TU to its lexical contents from each module file.
537296417Sdim  std::vector<std::pair<ModuleFile*, LexicalContents>> TULexicalDecls;
538296417Sdim
539296417Sdim  /// \brief Map from a DeclContext to its lookup tables.
540296417Sdim  llvm::DenseMap<const DeclContext *,
541296417Sdim                 serialization::reader::DeclContextLookupTable> Lookups;
542296417Sdim
543212795Sdim  // Updates for visible decls can occur for other contexts than just the
544296417Sdim  // TU, and when we read those update records, the actual context may not
545296417Sdim  // be available yet, so have this pending map using the ID as a key. It
546296417Sdim  // will be realized when the context is actually loaded.
547296417Sdim  struct PendingVisibleUpdate {
548296417Sdim    ModuleFile *Mod;
549296417Sdim    const unsigned char *Data;
550296417Sdim  };
551296417Sdim  typedef SmallVector<PendingVisibleUpdate, 1> DeclContextVisibleUpdates;
552212795Sdim
553212795Sdim  /// \brief Updates to the visible declarations of declaration contexts that
554212795Sdim  /// haven't been loaded yet.
555296417Sdim  llvm::DenseMap<serialization::DeclID, DeclContextVisibleUpdates>
556296417Sdim      PendingVisibleUpdates;
557296417Sdim
558234353Sdim  /// \brief The set of C++ or Objective-C classes that have forward
559234353Sdim  /// declarations that have not yet been linked to their definitions.
560234353Sdim  llvm::SmallPtrSet<Decl *, 4> PendingDefinitions;
561243830Sdim
562243830Sdim  typedef llvm::MapVector<Decl *, uint64_t,
563243830Sdim                          llvm::SmallDenseMap<Decl *, unsigned, 4>,
564249423Sdim                          SmallVector<std::pair<Decl *, uint64_t>, 4> >
565243830Sdim    PendingBodiesMap;
566243830Sdim
567243830Sdim  /// \brief Functions or methods that have bodies that will be attached.
568243830Sdim  PendingBodiesMap PendingBodies;
569243830Sdim
570288943Sdim  /// \brief Definitions for which we have added merged definitions but not yet
571288943Sdim  /// performed deduplication.
572288943Sdim  llvm::SetVector<NamedDecl*> PendingMergedDefinitionsToDeduplicate;
573288943Sdim
574296417Sdim  /// \brief Read the record that describes the lexical contents of a DC.
575296417Sdim  bool ReadLexicalDeclContextStorage(ModuleFile &M,
576296417Sdim                                     llvm::BitstreamCursor &Cursor,
577296417Sdim                                     uint64_t Offset, DeclContext *DC);
578296417Sdim  /// \brief Read the record that describes the visible contents of a DC.
579296417Sdim  bool ReadVisibleDeclContextStorage(ModuleFile &M,
580296417Sdim                                     llvm::BitstreamCursor &Cursor,
581296417Sdim                                     uint64_t Offset, serialization::DeclID ID);
582212795Sdim
583212795Sdim  /// \brief A vector containing identifiers that have already been
584212795Sdim  /// loaded.
585212795Sdim  ///
586212795Sdim  /// If the pointer at index I is non-NULL, then it refers to the
587212795Sdim  /// IdentifierInfo for the identifier with ID=I+1 that has already
588212795Sdim  /// been loaded.
589212795Sdim  std::vector<IdentifierInfo *> IdentifiersLoaded;
590212795Sdim
591234353Sdim  typedef ContinuousRangeMap<serialization::IdentID, ModuleFile *, 4>
592226633Sdim    GlobalIdentifierMapType;
593234353Sdim
594243830Sdim  /// \brief Mapping from global identifier IDs to the module in which the
595226633Sdim  /// identifier resides along with the offset that should be added to the
596226633Sdim  /// global identifier ID to produce a local ID.
597226633Sdim  GlobalIdentifierMapType GlobalIdentifierMap;
598226633Sdim
599243830Sdim  /// \brief A vector containing macros that have already been
600243830Sdim  /// loaded.
601243830Sdim  ///
602243830Sdim  /// If the pointer at index I is non-NULL, then it refers to the
603243830Sdim  /// MacroInfo for the identifier with ID=I+1 that has already
604243830Sdim  /// been loaded.
605243830Sdim  std::vector<MacroInfo *> MacrosLoaded;
606243830Sdim
607288943Sdim  typedef std::pair<IdentifierInfo *, serialization::SubmoduleID>
608288943Sdim      LoadedMacroInfo;
609288943Sdim
610288943Sdim  /// \brief A set of #undef directives that we have loaded; used to
611288943Sdim  /// deduplicate the same #undef information coming from multiple module
612288943Sdim  /// files.
613288943Sdim  llvm::DenseSet<LoadedMacroInfo> LoadedUndefs;
614288943Sdim
615243830Sdim  typedef ContinuousRangeMap<serialization::MacroID, ModuleFile *, 4>
616243830Sdim    GlobalMacroMapType;
617243830Sdim
618243830Sdim  /// \brief Mapping from global macro IDs to the module in which the
619243830Sdim  /// macro resides along with the offset that should be added to the
620243830Sdim  /// global macro ID to produce a local ID.
621243830Sdim  GlobalMacroMapType GlobalMacroMap;
622243830Sdim
623234353Sdim  /// \brief A vector containing submodules that have already been loaded.
624234353Sdim  ///
625234353Sdim  /// This vector is indexed by the Submodule ID (-1). NULL submodule entries
626234353Sdim  /// indicate that the particular submodule ID has not yet been loaded.
627234353Sdim  SmallVector<Module *, 2> SubmodulesLoaded;
628234353Sdim
629234353Sdim  typedef ContinuousRangeMap<serialization::SubmoduleID, ModuleFile *, 4>
630234353Sdim    GlobalSubmoduleMapType;
631234353Sdim
632234353Sdim  /// \brief Mapping from global submodule IDs to the module file in which the
633234353Sdim  /// submodule resides along with the offset that should be added to the
634234353Sdim  /// global submodule ID to produce a local ID.
635234353Sdim  GlobalSubmoduleMapType GlobalSubmoduleMap;
636234353Sdim
637234353Sdim  /// \brief A set of hidden declarations.
638288943Sdim  typedef SmallVector<Decl*, 2> HiddenNames;
639234353Sdim  typedef llvm::DenseMap<Module *, HiddenNames> HiddenNamesMapType;
640234353Sdim
641234353Sdim  /// \brief A mapping from each of the hidden submodules to the deserialized
642234353Sdim  /// declarations in that submodule that could be made visible.
643234353Sdim  HiddenNamesMapType HiddenNamesMap;
644234353Sdim
645234353Sdim
646249423Sdim  /// \brief A module import, export, or conflict that hasn't yet been resolved.
647249423Sdim  struct UnresolvedModuleRef {
648234353Sdim    /// \brief The file in which this module resides.
649234353Sdim    ModuleFile *File;
650234353Sdim
651234353Sdim    /// \brief The module that is importing or exporting.
652234353Sdim    Module *Mod;
653249423Sdim
654249423Sdim    /// \brief The kind of module reference.
655249423Sdim    enum { Import, Export, Conflict } Kind;
656249423Sdim
657234353Sdim    /// \brief The local ID of the module that is being exported.
658234353Sdim    unsigned ID;
659249423Sdim
660234353Sdim    /// \brief Whether this is a wildcard export.
661234353Sdim    unsigned IsWildcard : 1;
662249423Sdim
663249423Sdim    /// \brief String data.
664249423Sdim    StringRef String;
665234353Sdim  };
666234353Sdim
667234353Sdim  /// \brief The set of module imports and exports that still need to be
668234353Sdim  /// resolved.
669249423Sdim  SmallVector<UnresolvedModuleRef, 2> UnresolvedModuleRefs;
670234353Sdim
671212795Sdim  /// \brief A vector containing selectors that have already been loaded.
672212795Sdim  ///
673212795Sdim  /// This vector is indexed by the Selector ID (-1). NULL selector
674212795Sdim  /// entries indicate that the particular selector ID has not yet
675212795Sdim  /// been loaded.
676226633Sdim  SmallVector<Selector, 16> SelectorsLoaded;
677212795Sdim
678234353Sdim  typedef ContinuousRangeMap<serialization::SelectorID, ModuleFile *, 4>
679226633Sdim    GlobalSelectorMapType;
680234353Sdim
681226633Sdim  /// \brief Mapping from global selector IDs to the module in which the
682249423Sdim
683226633Sdim  /// global selector ID to produce a local ID.
684226633Sdim  GlobalSelectorMapType GlobalSelectorMap;
685212795Sdim
686234353Sdim  /// \brief The generation number of the last time we loaded data from the
687234353Sdim  /// global method pool for this selector.
688234353Sdim  llvm::DenseMap<Selector, unsigned> SelectorGeneration;
689234353Sdim
690309124Sdim  /// Whether a selector is out of date. We mark a selector as out of date
691309124Sdim  /// if we load another module after the method pool entry was pulled in.
692309124Sdim  llvm::DenseMap<Selector, bool> SelectorOutOfDate;
693309124Sdim
694249423Sdim  struct PendingMacroInfo {
695249423Sdim    ModuleFile *M;
696288943Sdim    uint64_t MacroDirectivesOffset;
697249423Sdim
698288943Sdim    PendingMacroInfo(ModuleFile *M, uint64_t MacroDirectivesOffset)
699288943Sdim        : M(M), MacroDirectivesOffset(MacroDirectivesOffset) {}
700249423Sdim  };
701249423Sdim
702249423Sdim  typedef llvm::MapVector<IdentifierInfo *, SmallVector<PendingMacroInfo, 2> >
703243830Sdim    PendingMacroIDsMap;
704226633Sdim
705243830Sdim  /// \brief Mapping from identifiers that have a macro history to the global
706243830Sdim  /// IDs have not yet been deserialized to the global IDs of those macros.
707243830Sdim  PendingMacroIDsMap PendingMacroIDs;
708243830Sdim
709234353Sdim  typedef ContinuousRangeMap<unsigned, ModuleFile *, 4>
710226633Sdim    GlobalPreprocessedEntityMapType;
711234353Sdim
712226633Sdim  /// \brief Mapping from global preprocessing entity IDs to the module in
713226633Sdim  /// which the preprocessed entity resides along with the offset that should be
714321369Sdim  /// added to the global preprocessing entity ID to produce a local ID.
715226633Sdim  GlobalPreprocessedEntityMapType GlobalPreprocessedEntityMap;
716234353Sdim
717212795Sdim  /// \name CodeGen-relevant special data
718212795Sdim  /// \brief Fields containing data that is relevant to CodeGen.
719212795Sdim  //@{
720212795Sdim
721212795Sdim  /// \brief The IDs of all declarations that fulfill the criteria of
722212795Sdim  /// "interesting" decls.
723212795Sdim  ///
724276479Sdim  /// This contains the data loaded from all EAGERLY_DESERIALIZED_DECLS blocks
725276479Sdim  /// in the chain. The referenced declarations are deserialized and passed to
726276479Sdim  /// the consumer eagerly.
727276479Sdim  SmallVector<uint64_t, 16> EagerlyDeserializedDecls;
728212795Sdim
729239462Sdim  /// \brief The IDs of all tentative definitions stored in the chain.
730212795Sdim  ///
731212795Sdim  /// Sema keeps track of all tentative definitions in a TU because it has to
732212795Sdim  /// complete them and pass them on to CodeGen. Thus, tentative definitions in
733212795Sdim  /// the PCH chain must be eagerly deserialized.
734226633Sdim  SmallVector<uint64_t, 16> TentativeDefinitions;
735212795Sdim
736212795Sdim  /// \brief The IDs of all CXXRecordDecls stored in the chain whose VTables are
737212795Sdim  /// used.
738212795Sdim  ///
739212795Sdim  /// CodeGen has to emit VTables for these records, so they have to be eagerly
740212795Sdim  /// deserialized.
741226633Sdim  SmallVector<uint64_t, 64> VTableUses;
742212795Sdim
743226633Sdim  /// \brief A snapshot of the pending instantiations in the chain.
744226633Sdim  ///
745226633Sdim  /// This record tracks the instantiations that Sema has to perform at the
746226633Sdim  /// end of the TU. It consists of a pair of values for every pending
747226633Sdim  /// instantiation where the first value is the ID of the decl and the second
748226633Sdim  /// is the instantiation location.
749226633Sdim  SmallVector<uint64_t, 64> PendingInstantiations;
750226633Sdim
751212795Sdim  //@}
752212795Sdim
753226633Sdim  /// \name DiagnosticsEngine-relevant special data
754212795Sdim  /// \brief Fields containing data that is used for generating diagnostics
755212795Sdim  //@{
756212795Sdim
757212795Sdim  /// \brief A snapshot of Sema's unused file-scoped variable tracking, for
758212795Sdim  /// generating warnings.
759226633Sdim  SmallVector<uint64_t, 16> UnusedFileScopedDecls;
760212795Sdim
761223017Sdim  /// \brief A list of all the delegating constructors we've seen, to diagnose
762223017Sdim  /// cycles.
763226633Sdim  SmallVector<uint64_t, 4> DelegatingCtorDecls;
764234353Sdim
765226633Sdim  /// \brief Method selectors used in a @selector expression. Used for
766226633Sdim  /// implementation of -Wselector.
767226633Sdim  SmallVector<uint64_t, 64> ReferencedSelectorsData;
768223017Sdim
769212795Sdim  /// \brief A snapshot of Sema's weak undeclared identifier tracking, for
770212795Sdim  /// generating warnings.
771226633Sdim  SmallVector<uint64_t, 64> WeakUndeclaredIdentifiers;
772212795Sdim
773212795Sdim  /// \brief The IDs of type aliases for ext_vectors that exist in the chain.
774212795Sdim  ///
775212795Sdim  /// Used by Sema for finding sugared names for ext_vectors in diagnostics.
776226633Sdim  SmallVector<uint64_t, 4> ExtVectorDecls;
777212795Sdim
778212795Sdim  //@}
779212795Sdim
780212795Sdim  /// \name Sema-relevant special data
781212795Sdim  /// \brief Fields containing data that is used for semantic analysis
782212795Sdim  //@{
783212795Sdim
784280031Sdim  /// \brief The IDs of all potentially unused typedef names in the chain.
785280031Sdim  ///
786280031Sdim  /// Sema tracks these to emit warnings.
787280031Sdim  SmallVector<uint64_t, 16> UnusedLocalTypedefNameCandidates;
788280031Sdim
789314564Sdim  /// \brief Our current depth in #pragma cuda force_host_device begin/end
790314564Sdim  /// macros.
791314564Sdim  unsigned ForceCUDAHostDeviceDepth = 0;
792314564Sdim
793212795Sdim  /// \brief The IDs of the declarations Sema stores directly.
794212795Sdim  ///
795212795Sdim  /// Sema tracks a few important decls, such as namespace std, directly.
796226633Sdim  SmallVector<uint64_t, 4> SemaDeclRefs;
797212795Sdim
798212795Sdim  /// \brief The IDs of the types ASTContext stores directly.
799212795Sdim  ///
800212795Sdim  /// The AST context tracks a few important types, such as va_list, directly.
801226633Sdim  SmallVector<uint64_t, 16> SpecialTypes;
802212795Sdim
803218893Sdim  /// \brief The IDs of CUDA-specific declarations ASTContext stores directly.
804218893Sdim  ///
805218893Sdim  /// The AST context tracks a few important decls, currently cudaConfigureCall,
806218893Sdim  /// directly.
807226633Sdim  SmallVector<uint64_t, 2> CUDASpecialDeclRefs;
808218893Sdim
809218893Sdim  /// \brief The floating point pragma option settings.
810226633Sdim  SmallVector<uint64_t, 1> FPPragmaOptions;
811218893Sdim
812276479Sdim  /// \brief The pragma clang optimize location (if the pragma state is "off").
813276479Sdim  SourceLocation OptimizeOffPragmaLocation;
814276479Sdim
815309124Sdim  /// \brief The PragmaMSStructKind pragma ms_struct state if set, or -1.
816314564Sdim  int PragmaMSStructState = -1;
817309124Sdim
818309124Sdim  /// \brief The PragmaMSPointersToMembersKind pragma pointers_to_members state.
819314564Sdim  int PragmaMSPointersToMembersState = -1;
820309124Sdim  SourceLocation PointersToMembersPragmaLocation;
821309124Sdim
822321369Sdim  /// \brief The pragma pack state.
823321369Sdim  Optional<unsigned> PragmaPackCurrentValue;
824321369Sdim  SourceLocation PragmaPackCurrentLocation;
825321369Sdim  struct PragmaPackStackEntry {
826321369Sdim    unsigned Value;
827321369Sdim    SourceLocation Location;
828321369Sdim    StringRef SlotLabel;
829321369Sdim  };
830321369Sdim  llvm::SmallVector<PragmaPackStackEntry, 2> PragmaPackStack;
831321369Sdim  llvm::SmallVector<std::string, 2> PragmaPackStrings;
832321369Sdim
833218893Sdim  /// \brief The OpenCL extension settings.
834314564Sdim  OpenCLOptions OpenCLExtensions;
835218893Sdim
836314564Sdim  /// \brief Extensions required by an OpenCL type.
837314564Sdim  llvm::DenseMap<const Type *, std::set<std::string>> OpenCLTypeExtMap;
838314564Sdim
839314564Sdim  /// \brief Extensions required by an OpenCL declaration.
840314564Sdim  llvm::DenseMap<const Decl *, std::set<std::string>> OpenCLDeclExtMap;
841314564Sdim
842224145Sdim  /// \brief A list of the namespaces we've seen.
843226633Sdim  SmallVector<uint64_t, 4> KnownNamespaces;
844224145Sdim
845249423Sdim  /// \brief A list of undefined decls with internal linkage followed by the
846249423Sdim  /// SourceLocation of a matching ODR-use.
847249423Sdim  SmallVector<uint64_t, 8> UndefinedButUsed;
848249423Sdim
849288943Sdim  /// \brief Delete expressions to analyze at the end of translation unit.
850288943Sdim  SmallVector<uint64_t, 8> DelayedDeleteExprs;
851288943Sdim
852261991Sdim  // \brief A list of late parsed template function data.
853261991Sdim  SmallVector<uint64_t, 1> LateParsedTemplates;
854261991Sdim
855314564Sdimpublic:
856276479Sdim  struct ImportedSubmodule {
857276479Sdim    serialization::SubmoduleID ID;
858276479Sdim    SourceLocation ImportLoc;
859276479Sdim
860276479Sdim    ImportedSubmodule(serialization::SubmoduleID ID, SourceLocation ImportLoc)
861276479Sdim      : ID(ID), ImportLoc(ImportLoc) {}
862276479Sdim  };
863276479Sdim
864314564Sdimprivate:
865234353Sdim  /// \brief A list of modules that were imported by precompiled headers or
866234353Sdim  /// any other non-module AST file.
867276479Sdim  SmallVector<ImportedSubmodule, 2> ImportedModules;
868212795Sdim  //@}
869212795Sdim
870218893Sdim  /// \brief The directory that the PCH we are reading is stored in.
871218893Sdim  std::string CurrentDir;
872218893Sdim
873212795Sdim  /// \brief The system include root to be used when loading the
874212795Sdim  /// precompiled header.
875226633Sdim  std::string isysroot;
876212795Sdim
877212795Sdim  /// \brief Whether to disable the normal validation performed on precompiled
878212795Sdim  /// headers when they are loaded.
879212795Sdim  bool DisableValidation;
880234353Sdim
881234353Sdim  /// \brief Whether to accept an AST file with compiler errors.
882234353Sdim  bool AllowASTWithCompilerErrors;
883234353Sdim
884276479Sdim  /// \brief Whether to accept an AST file that has a different configuration
885276479Sdim  /// from the current compiler instance.
886276479Sdim  bool AllowConfigurationMismatch;
887276479Sdim
888276479Sdim  /// \brief Whether validate system input files.
889276479Sdim  bool ValidateSystemInputs;
890276479Sdim
891249423Sdim  /// \brief Whether we are allowed to use the global module index.
892249423Sdim  bool UseGlobalIndex;
893249423Sdim
894249423Sdim  /// \brief Whether we have tried loading the global module index yet.
895314564Sdim  bool TriedLoadingGlobalIndex = false;
896249423Sdim
897309124Sdim  ///\brief Whether we are currently processing update records.
898314564Sdim  bool ProcessingUpdateRecords = false;
899309124Sdim
900239462Sdim  typedef llvm::DenseMap<unsigned, SwitchCase *> SwitchCaseMapTy;
901212795Sdim  /// \brief Mapping from switch-case IDs in the chain to switch-case statements
902212795Sdim  ///
903212795Sdim  /// Statements usually don't have IDs, but switch cases need them, so that the
904212795Sdim  /// switch statement can refer to them.
905239462Sdim  SwitchCaseMapTy SwitchCaseStmts;
906212795Sdim
907239462Sdim  SwitchCaseMapTy *CurrSwitchCaseStmts;
908239462Sdim
909212795Sdim  /// \brief The number of source location entries de-serialized from
910212795Sdim  /// the PCH file.
911314564Sdim  unsigned NumSLocEntriesRead = 0;
912212795Sdim
913212795Sdim  /// \brief The number of source location entries in the chain.
914314564Sdim  unsigned TotalNumSLocEntries = 0;
915212795Sdim
916212795Sdim  /// \brief The number of statements (and expressions) de-serialized
917212795Sdim  /// from the chain.
918314564Sdim  unsigned NumStatementsRead = 0;
919212795Sdim
920212795Sdim  /// \brief The total number of statements (and expressions) stored
921212795Sdim  /// in the chain.
922314564Sdim  unsigned TotalNumStatements = 0;
923212795Sdim
924212795Sdim  /// \brief The number of macros de-serialized from the chain.
925314564Sdim  unsigned NumMacrosRead = 0;
926212795Sdim
927212795Sdim  /// \brief The total number of macros stored in the chain.
928314564Sdim  unsigned TotalNumMacros = 0;
929212795Sdim
930249423Sdim  /// \brief The number of lookups into identifier tables.
931314564Sdim  unsigned NumIdentifierLookups = 0;
932249423Sdim
933249423Sdim  /// \brief The number of lookups into identifier tables that succeed.
934314564Sdim  unsigned NumIdentifierLookupHits = 0;
935249423Sdim
936212795Sdim  /// \brief The number of selectors that have been read.
937314564Sdim  unsigned NumSelectorsRead = 0;
938212795Sdim
939212795Sdim  /// \brief The number of method pool entries that have been read.
940314564Sdim  unsigned NumMethodPoolEntriesRead = 0;
941212795Sdim
942212795Sdim  /// \brief The number of times we have looked up a selector in the method
943249423Sdim  /// pool.
944314564Sdim  unsigned NumMethodPoolLookups = 0;
945212795Sdim
946249423Sdim  /// \brief The number of times we have looked up a selector in the method
947249423Sdim  /// pool and found something.
948314564Sdim  unsigned NumMethodPoolHits = 0;
949249423Sdim
950249423Sdim  /// \brief The number of times we have looked up a selector in the method
951249423Sdim  /// pool within a specific module.
952314564Sdim  unsigned NumMethodPoolTableLookups = 0;
953249423Sdim
954249423Sdim  /// \brief The number of times we have looked up a selector in the method
955249423Sdim  /// pool within a specific module and found something.
956314564Sdim  unsigned NumMethodPoolTableHits = 0;
957249423Sdim
958212795Sdim  /// \brief The total number of method pool entries in the selector table.
959314564Sdim  unsigned TotalNumMethodPoolEntries = 0;
960212795Sdim
961212795Sdim  /// Number of lexical decl contexts read/total.
962314564Sdim  unsigned NumLexicalDeclContextsRead = 0, TotalLexicalDeclContexts = 0;
963212795Sdim
964212795Sdim  /// Number of visible decl contexts read/total.
965314564Sdim  unsigned NumVisibleDeclContextsRead = 0, TotalVisibleDeclContexts = 0;
966234353Sdim
967226633Sdim  /// Total size of modules, in bits, currently loaded
968314564Sdim  uint64_t TotalModulesSizeInBits = 0;
969226633Sdim
970212795Sdim  /// \brief Number of Decl/types that are currently deserializing.
971314564Sdim  unsigned NumCurrentElementsDeserializing = 0;
972212795Sdim
973234353Sdim  /// \brief Set true while we are in the process of passing deserialized
974234353Sdim  /// "interesting" decls to consumer inside FinishedDeserializing().
975234353Sdim  /// This is used as a guard to avoid recursively repeating the process of
976234353Sdim  /// passing decls to consumer.
977314564Sdim  bool PassingDeclsToConsumer = false;
978234353Sdim
979212795Sdim  /// \brief The set of identifiers that were read while the AST reader was
980212795Sdim  /// (recursively) loading declarations.
981212795Sdim  ///
982212795Sdim  /// The declarations on the identifier chain for these identifiers will be
983212795Sdim  /// loaded once the recursive loading has completed.
984249423Sdim  llvm::MapVector<IdentifierInfo *, SmallVector<uint32_t, 4> >
985249423Sdim    PendingIdentifierInfos;
986212795Sdim
987288943Sdim  /// \brief The set of lookup results that we have faked in order to support
988288943Sdim  /// merging of partially deserialized decls but that we have not yet removed.
989288943Sdim  llvm::SmallMapVector<IdentifierInfo *, SmallVector<NamedDecl*, 2>, 16>
990288943Sdim    PendingFakeLookupResults;
991288943Sdim
992234353Sdim  /// \brief The generation number of each identifier, which keeps track of
993234353Sdim  /// the last time we loaded information about this identifier.
994234353Sdim  llvm::DenseMap<IdentifierInfo *, unsigned> IdentifierGeneration;
995321369Sdim
996321369Sdim  class InterestingDecl {
997321369Sdim    Decl *D;
998321369Sdim    bool DeclHasPendingBody;
999321369Sdim
1000321369Sdim  public:
1001321369Sdim    InterestingDecl(Decl *D, bool HasBody)
1002321369Sdim        : D(D), DeclHasPendingBody(HasBody) {}
1003321369Sdim    Decl *getDecl() { return D; }
1004321369Sdim    /// Whether the declaration has a pending body.
1005321369Sdim    bool hasPendingBody() { return DeclHasPendingBody; }
1006321369Sdim  };
1007321369Sdim
1008321369Sdim  /// \brief Contains declarations and definitions that could be
1009212795Sdim  /// "interesting" to the ASTConsumer, when we get that AST consumer.
1010212795Sdim  ///
1011212795Sdim  /// "Interesting" declarations are those that have data that may
1012212795Sdim  /// need to be emitted, such as inline function definitions or
1013212795Sdim  /// Objective-C protocols.
1014321369Sdim  std::deque<InterestingDecl> PotentiallyInterestingDecls;
1015212795Sdim
1016234353Sdim  /// \brief The list of redeclaration chains that still need to be
1017296417Sdim  /// reconstructed, and the local offset to the corresponding list
1018296417Sdim  /// of redeclarations.
1019296417Sdim  SmallVector<std::pair<Decl *, uint64_t>, 16> PendingDeclChains;
1020218893Sdim
1021276479Sdim  /// \brief The list of canonical declarations whose redeclaration chains
1022276479Sdim  /// need to be marked as incomplete once we're done deserializing things.
1023276479Sdim  SmallVector<Decl *, 16> PendingIncompleteDeclChains;
1024276479Sdim
1025249423Sdim  /// \brief The Decl IDs for the Sema/Lexical DeclContext of a Decl that has
1026249423Sdim  /// been loaded but its DeclContext was not set yet.
1027249423Sdim  struct PendingDeclContextInfo {
1028249423Sdim    Decl *D;
1029249423Sdim    serialization::GlobalDeclID SemaDC;
1030249423Sdim    serialization::GlobalDeclID LexicalDC;
1031249423Sdim  };
1032249423Sdim
1033249423Sdim  /// \brief The set of Decls that have been loaded but their DeclContexts are
1034249423Sdim  /// not set yet.
1035249423Sdim  ///
1036249423Sdim  /// The DeclContexts for these Decls will be set once recursive loading has
1037249423Sdim  /// been completed.
1038249423Sdim  std::deque<PendingDeclContextInfo> PendingDeclContextInfos;
1039249423Sdim
1040261991Sdim  /// \brief The set of NamedDecls that have been loaded, but are members of a
1041261991Sdim  /// context that has been merged into another context where the corresponding
1042261991Sdim  /// declaration is either missing or has not yet been loaded.
1043261991Sdim  ///
1044261991Sdim  /// We will check whether the corresponding declaration is in fact missing
1045261991Sdim  /// once recursing loading has been completed.
1046261991Sdim  llvm::SmallVector<NamedDecl *, 16> PendingOdrMergeChecks;
1047261991Sdim
1048276479Sdim  /// \brief Record definitions in which we found an ODR violation.
1049276479Sdim  llvm::SmallDenseMap<CXXRecordDecl *, llvm::TinyPtrVector<CXXRecordDecl *>, 2>
1050276479Sdim      PendingOdrMergeFailures;
1051276479Sdim
1052276479Sdim  /// \brief DeclContexts in which we have diagnosed an ODR violation.
1053276479Sdim  llvm::SmallPtrSet<DeclContext*, 2> DiagnosedOdrMergeFailures;
1054276479Sdim
1055234353Sdim  /// \brief The set of Objective-C categories that have been deserialized
1056234353Sdim  /// since the last time the declaration chains were linked.
1057234353Sdim  llvm::SmallPtrSet<ObjCCategoryDecl *, 16> CategoriesDeserialized;
1058234353Sdim
1059234353Sdim  /// \brief The set of Objective-C class definitions that have already been
1060234353Sdim  /// loaded, for which we will need to check for categories whenever a new
1061234353Sdim  /// module is loaded.
1062249423Sdim  SmallVector<ObjCInterfaceDecl *, 16> ObjCClassesLoaded;
1063276479Sdim
1064249423Sdim  typedef llvm::DenseMap<Decl *, SmallVector<serialization::DeclID, 2> >
1065288943Sdim    KeyDeclsMap;
1066234353Sdim
1067288943Sdim  /// \brief A mapping from canonical declarations to the set of global
1068288943Sdim  /// declaration IDs for key declaration that have been merged with that
1069288943Sdim  /// canonical declaration. A key declaration is a formerly-canonical
1070288943Sdim  /// declaration whose module did not import any other key declaration for that
1071288943Sdim  /// entity. These are the IDs that we use as keys when finding redecl chains.
1072288943Sdim  KeyDeclsMap KeyDecls;
1073234353Sdim
1074261991Sdim  /// \brief A mapping from DeclContexts to the semantic DeclContext that we
1075261991Sdim  /// are treating as the definition of the entity. This is used, for instance,
1076261991Sdim  /// when merging implicit instantiations of class templates across modules.
1077261991Sdim  llvm::DenseMap<DeclContext *, DeclContext *> MergedDeclContexts;
1078261991Sdim
1079261991Sdim  /// \brief A mapping from canonical declarations of enums to their canonical
1080261991Sdim  /// definitions. Only populated when using modules in C++.
1081261991Sdim  llvm::DenseMap<EnumDecl *, EnumDecl *> EnumDefinitions;
1082261991Sdim
1083212795Sdim  /// \brief When reading a Stmt tree, Stmt operands are placed in this stack.
1084226633Sdim  SmallVector<Stmt *, 16> StmtStack;
1085212795Sdim
1086212795Sdim  /// \brief What kind of records we are reading.
1087212795Sdim  enum ReadingKind {
1088261991Sdim    Read_None, Read_Decl, Read_Type, Read_Stmt
1089212795Sdim  };
1090212795Sdim
1091234353Sdim  /// \brief What kind of records we are reading.
1092314564Sdim  ReadingKind ReadingKind = Read_None;
1093212795Sdim
1094212795Sdim  /// \brief RAII object to change the reading kind.
1095212795Sdim  class ReadingKindTracker {
1096212795Sdim    ASTReader &Reader;
1097212795Sdim    enum ReadingKind PrevKind;
1098212795Sdim
1099288943Sdim    ReadingKindTracker(const ReadingKindTracker &) = delete;
1100288943Sdim    void operator=(const ReadingKindTracker &) = delete;
1101212795Sdim
1102212795Sdim  public:
1103212795Sdim    ReadingKindTracker(enum ReadingKind newKind, ASTReader &reader)
1104212795Sdim      : Reader(reader), PrevKind(Reader.ReadingKind) {
1105212795Sdim      Reader.ReadingKind = newKind;
1106212795Sdim    }
1107212795Sdim
1108212795Sdim    ~ReadingKindTracker() { Reader.ReadingKind = PrevKind; }
1109212795Sdim  };
1110212795Sdim
1111309124Sdim  /// \brief RAII object to mark the start of processing updates.
1112309124Sdim  class ProcessingUpdatesRAIIObj {
1113309124Sdim    ASTReader &Reader;
1114309124Sdim    bool PrevState;
1115309124Sdim
1116309124Sdim    ProcessingUpdatesRAIIObj(const ProcessingUpdatesRAIIObj &) = delete;
1117309124Sdim    void operator=(const ProcessingUpdatesRAIIObj &) = delete;
1118309124Sdim
1119309124Sdim  public:
1120309124Sdim    ProcessingUpdatesRAIIObj(ASTReader &reader)
1121309124Sdim      : Reader(reader), PrevState(Reader.ProcessingUpdateRecords) {
1122309124Sdim      Reader.ProcessingUpdateRecords = true;
1123309124Sdim    }
1124309124Sdim
1125309124Sdim    ~ProcessingUpdatesRAIIObj() { Reader.ProcessingUpdateRecords = PrevState; }
1126309124Sdim  };
1127309124Sdim
1128212795Sdim  /// \brief Suggested contents of the predefines buffer, after this
1129212795Sdim  /// PCH file has been processed.
1130212795Sdim  ///
1131212795Sdim  /// In most cases, this string will be empty, because the predefines
1132212795Sdim  /// buffer computed to build the PCH file will be identical to the
1133212795Sdim  /// predefines buffer computed from the command line. However, when
1134212795Sdim  /// there are differences that the PCH reader can work around, this
1135212795Sdim  /// predefines buffer may contain additional definitions.
1136212795Sdim  std::string SuggestedPredefines;
1137212795Sdim
1138321369Sdim  llvm::DenseMap<const Decl *, bool> BodySource;
1139321369Sdim
1140212795Sdim  /// \brief Reads a statement from the specified cursor.
1141234353Sdim  Stmt *ReadStmtFromStream(ModuleFile &F);
1142212795Sdim
1143276479Sdim  struct InputFileInfo {
1144276479Sdim    std::string Filename;
1145276479Sdim    off_t StoredSize;
1146276479Sdim    time_t StoredTime;
1147276479Sdim    bool Overridden;
1148296417Sdim    bool Transient;
1149321369Sdim    bool TopLevelModuleMap;
1150276479Sdim  };
1151276479Sdim
1152276479Sdim  /// \brief Reads the stored information about an input file.
1153276479Sdim  InputFileInfo readInputFileInfo(ModuleFile &F, unsigned ID);
1154276479Sdim
1155243830Sdim  /// \brief Retrieve the file entry and 'overridden' bit for an input
1156243830Sdim  /// file in the given module file.
1157249423Sdim  serialization::InputFile getInputFile(ModuleFile &F, unsigned ID,
1158249423Sdim                                        bool Complain = true);
1159243830Sdim
1160280031Sdimpublic:
1161280031Sdim  void ResolveImportedPath(ModuleFile &M, std::string &Filename);
1162280031Sdim  static void ResolveImportedPath(std::string &Filename, StringRef Prefix);
1163223017Sdim
1164288943Sdim  /// \brief Returns the first key declaration for the given declaration. This
1165288943Sdim  /// is one that is formerly-canonical (or still canonical) and whose module
1166288943Sdim  /// did not import any other key declaration of the entity.
1167288943Sdim  Decl *getKeyDeclaration(Decl *D) {
1168288943Sdim    D = D->getCanonicalDecl();
1169288943Sdim    if (D->isFromASTFile())
1170288943Sdim      return D;
1171288943Sdim
1172288943Sdim    auto I = KeyDecls.find(D);
1173288943Sdim    if (I == KeyDecls.end() || I->second.empty())
1174288943Sdim      return D;
1175288943Sdim    return GetExistingDecl(I->second[0]);
1176288943Sdim  }
1177288943Sdim  const Decl *getKeyDeclaration(const Decl *D) {
1178288943Sdim    return getKeyDeclaration(const_cast<Decl*>(D));
1179288943Sdim  }
1180288943Sdim
1181288943Sdim  /// \brief Run a callback on each imported key declaration of \p D.
1182288943Sdim  template <typename Fn>
1183288943Sdim  void forEachImportedKeyDecl(const Decl *D, Fn Visit) {
1184288943Sdim    D = D->getCanonicalDecl();
1185288943Sdim    if (D->isFromASTFile())
1186288943Sdim      Visit(D);
1187288943Sdim
1188288943Sdim    auto It = KeyDecls.find(const_cast<Decl*>(D));
1189288943Sdim    if (It != KeyDecls.end())
1190288943Sdim      for (auto ID : It->second)
1191288943Sdim        Visit(GetExistingDecl(ID));
1192288943Sdim  }
1193288943Sdim
1194296417Sdim  /// \brief Get the loaded lookup tables for \p Primary, if any.
1195296417Sdim  const serialization::reader::DeclContextLookupTable *
1196296417Sdim  getLoadedLookupTables(DeclContext *Primary) const;
1197296417Sdim
1198280031Sdimprivate:
1199249423Sdim  struct ImportedModule {
1200249423Sdim    ModuleFile *Mod;
1201249423Sdim    ModuleFile *ImportedBy;
1202249423Sdim    SourceLocation ImportLoc;
1203249423Sdim
1204249423Sdim    ImportedModule(ModuleFile *Mod,
1205249423Sdim                   ModuleFile *ImportedBy,
1206249423Sdim                   SourceLocation ImportLoc)
1207249423Sdim      : Mod(Mod), ImportedBy(ImportedBy), ImportLoc(ImportLoc) { }
1208249423Sdim  };
1209249423Sdim
1210226633Sdim  ASTReadResult ReadASTCore(StringRef FileName, ModuleKind Type,
1211249423Sdim                            SourceLocation ImportLoc, ModuleFile *ImportedBy,
1212249423Sdim                            SmallVectorImpl<ImportedModule> &Loaded,
1213249423Sdim                            off_t ExpectedSize, time_t ExpectedModTime,
1214321369Sdim                            ASTFileSignature ExpectedSignature,
1215243830Sdim                            unsigned ClientLoadCapabilities);
1216243830Sdim  ASTReadResult ReadControlBlock(ModuleFile &F,
1217249423Sdim                                 SmallVectorImpl<ImportedModule> &Loaded,
1218276479Sdim                                 const ModuleFile *ImportedBy,
1219243830Sdim                                 unsigned ClientLoadCapabilities);
1220296417Sdim  static ASTReadResult ReadOptionsBlock(
1221296417Sdim      llvm::BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
1222296417Sdim      bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
1223321369Sdim      std::string &SuggestedPredefines);
1224321369Sdim
1225321369Sdim  /// Read the unhashed control block.
1226321369Sdim  ///
1227321369Sdim  /// This has no effect on \c F.Stream, instead creating a fresh cursor from
1228321369Sdim  /// \c F.Data and reading ahead.
1229321369Sdim  ASTReadResult readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
1230321369Sdim                                         unsigned ClientLoadCapabilities);
1231321369Sdim
1232321369Sdim  static ASTReadResult
1233321369Sdim  readUnhashedControlBlockImpl(ModuleFile *F, llvm::StringRef StreamData,
1234321369Sdim                               unsigned ClientLoadCapabilities,
1235321369Sdim                               bool AllowCompatibleConfigurationMismatch,
1236321369Sdim                               ASTReaderListener *Listener,
1237321369Sdim                               bool ValidateDiagnosticOptions);
1238321369Sdim
1239276479Sdim  ASTReadResult ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities);
1240296417Sdim  ASTReadResult ReadExtensionBlock(ModuleFile &F);
1241321369Sdim  void ReadModuleOffsetMap(ModuleFile &F) const;
1242280031Sdim  bool ParseLineTable(ModuleFile &F, const RecordData &Record);
1243243830Sdim  bool ReadSourceManagerBlock(ModuleFile &F);
1244226633Sdim  llvm::BitstreamCursor &SLocCursorForID(int ID);
1245234353Sdim  SourceLocation getImportLocation(ModuleFile *F);
1246280031Sdim  ASTReadResult ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
1247280031Sdim                                       const ModuleFile *ImportedBy,
1248280031Sdim                                       unsigned ClientLoadCapabilities);
1249276479Sdim  ASTReadResult ReadSubmoduleBlock(ModuleFile &F,
1250276479Sdim                                   unsigned ClientLoadCapabilities);
1251243830Sdim  static bool ParseLanguageOptions(const RecordData &Record, bool Complain,
1252280031Sdim                                   ASTReaderListener &Listener,
1253280031Sdim                                   bool AllowCompatibleDifferences);
1254243830Sdim  static bool ParseTargetOptions(const RecordData &Record, bool Complain,
1255288943Sdim                                 ASTReaderListener &Listener,
1256288943Sdim                                 bool AllowCompatibleDifferences);
1257243830Sdim  static bool ParseDiagnosticOptions(const RecordData &Record, bool Complain,
1258243830Sdim                                     ASTReaderListener &Listener);
1259243830Sdim  static bool ParseFileSystemOptions(const RecordData &Record, bool Complain,
1260243830Sdim                                     ASTReaderListener &Listener);
1261243830Sdim  static bool ParseHeaderSearchOptions(const RecordData &Record, bool Complain,
1262243830Sdim                                       ASTReaderListener &Listener);
1263243830Sdim  static bool ParsePreprocessorOptions(const RecordData &Record, bool Complain,
1264243830Sdim                                       ASTReaderListener &Listener,
1265243830Sdim                                       std::string &SuggestedPredefines);
1266243830Sdim
1267218893Sdim  struct RecordLocation {
1268234353Sdim    RecordLocation(ModuleFile *M, uint64_t O)
1269218893Sdim      : F(M), Offset(O) {}
1270234353Sdim    ModuleFile *F;
1271218893Sdim    uint64_t Offset;
1272218893Sdim  };
1273212795Sdim
1274226633Sdim  QualType readTypeRecord(unsigned Index);
1275276479Sdim  void readExceptionSpec(ModuleFile &ModuleFile,
1276276479Sdim                         SmallVectorImpl<QualType> &ExceptionStorage,
1277280031Sdim                         FunctionProtoType::ExceptionSpecInfo &ESI,
1278276479Sdim                         const RecordData &Record, unsigned &Index);
1279212795Sdim  RecordLocation TypeCursorForIndex(unsigned Index);
1280212795Sdim  void LoadedDecl(unsigned Index, Decl *D);
1281226633Sdim  Decl *ReadDeclRecord(serialization::DeclID ID);
1282276479Sdim  void markIncompleteDeclChain(Decl *Canon);
1283288943Sdim
1284288943Sdim  /// \brief Returns the most recent declaration of a declaration (which must be
1285288943Sdim  /// of a redeclarable kind) that is either local or has already been loaded
1286288943Sdim  /// merged into its redecl chain.
1287288943Sdim  Decl *getMostRecentExistingDecl(Decl *D);
1288288943Sdim
1289234353Sdim  RecordLocation DeclCursorForID(serialization::DeclID ID,
1290309124Sdim                                 SourceLocation &Location);
1291321369Sdim  void loadDeclUpdateRecords(PendingUpdateRecord &Record);
1292296417Sdim  void loadPendingDeclChain(Decl *D, uint64_t LocalOffset);
1293234353Sdim  void loadObjCCategories(serialization::GlobalDeclID ID, ObjCInterfaceDecl *D,
1294234353Sdim                          unsigned PreviousGeneration = 0);
1295234353Sdim
1296226633Sdim  RecordLocation getLocalBitOffset(uint64_t GlobalOffset);
1297234353Sdim  uint64_t getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset);
1298212795Sdim
1299276479Sdim  /// \brief Returns the first preprocessed entity ID that begins or ends after
1300276479Sdim  /// \arg Loc.
1301226633Sdim  serialization::PreprocessedEntityID
1302276479Sdim  findPreprocessedEntity(SourceLocation Loc, bool EndsAfter) const;
1303226633Sdim
1304239462Sdim  /// \brief Find the next module that contains entities and return the ID
1305226633Sdim  /// of the first entry.
1306243830Sdim  ///
1307243830Sdim  /// \param SLocMapI points at a chunk of a module that contains no
1308239462Sdim  /// preprocessed entities or the entities it contains are not the
1309239462Sdim  /// ones we are looking for.
1310226633Sdim  serialization::PreprocessedEntityID
1311226633Sdim    findNextPreprocessedEntity(
1312226633Sdim                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const;
1313226633Sdim
1314243830Sdim  /// \brief Returns (ModuleFile, Local index) pair for \p GlobalIndex of a
1315234353Sdim  /// preprocessed entity.
1316234353Sdim  std::pair<ModuleFile *, unsigned>
1317234353Sdim    getModulePreprocessedEntity(unsigned GlobalIndex);
1318234353Sdim
1319243830Sdim  /// \brief Returns (begin, end) pair for the preprocessed entities of a
1320243830Sdim  /// particular module.
1321288943Sdim  llvm::iterator_range<PreprocessingRecord::iterator>
1322288943Sdim  getModulePreprocessedEntities(ModuleFile &Mod) const;
1323243830Sdim
1324321369Sdimpublic:
1325288943Sdim  class ModuleDeclIterator
1326288943Sdim      : public llvm::iterator_adaptor_base<
1327288943Sdim            ModuleDeclIterator, const serialization::LocalDeclID *,
1328288943Sdim            std::random_access_iterator_tag, const Decl *, ptrdiff_t,
1329288943Sdim            const Decl *, const Decl *> {
1330243830Sdim    ASTReader *Reader;
1331243830Sdim    ModuleFile *Mod;
1332243830Sdim
1333243830Sdim  public:
1334288943Sdim    ModuleDeclIterator()
1335288943Sdim        : iterator_adaptor_base(nullptr), Reader(nullptr), Mod(nullptr) {}
1336243830Sdim
1337243830Sdim    ModuleDeclIterator(ASTReader *Reader, ModuleFile *Mod,
1338243830Sdim                       const serialization::LocalDeclID *Pos)
1339288943Sdim        : iterator_adaptor_base(Pos), Reader(Reader), Mod(Mod) {}
1340243830Sdim
1341243830Sdim    value_type operator*() const {
1342288943Sdim      return Reader->GetDecl(Reader->getGlobalDeclID(*Mod, *I));
1343243830Sdim    }
1344288943Sdim    value_type operator->() const { return **this; }
1345243830Sdim
1346288943Sdim    bool operator==(const ModuleDeclIterator &RHS) const {
1347288943Sdim      assert(Reader == RHS.Reader && Mod == RHS.Mod);
1348288943Sdim      return I == RHS.I;
1349243830Sdim    }
1350243830Sdim  };
1351243830Sdim
1352288943Sdim  llvm::iterator_range<ModuleDeclIterator>
1353288943Sdim  getModuleFileLevelDecls(ModuleFile &Mod);
1354243830Sdim
1355321369Sdimprivate:
1356212795Sdim  void PassInterestingDeclsToConsumer();
1357234353Sdim  void PassInterestingDeclToConsumer(Decl *D);
1358212795Sdim
1359234353Sdim  void finishPendingActions();
1360280031Sdim  void diagnoseOdrViolations();
1361234353Sdim
1362251662Sdim  void pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name);
1363251662Sdim
1364249423Sdim  void addPendingDeclContextInfo(Decl *D,
1365249423Sdim                                 serialization::GlobalDeclID SemaDC,
1366249423Sdim                                 serialization::GlobalDeclID LexicalDC) {
1367249423Sdim    assert(D);
1368249423Sdim    PendingDeclContextInfo Info = { D, SemaDC, LexicalDC };
1369249423Sdim    PendingDeclContextInfos.push_back(Info);
1370249423Sdim  }
1371249423Sdim
1372212795Sdim  /// \brief Produce an error diagnostic and return true.
1373212795Sdim  ///
1374212795Sdim  /// This routine should only be used for fatal errors that have to
1375212795Sdim  /// do with non-routine failures (e.g., corrupted AST file).
1376321369Sdim  void Error(StringRef Msg) const;
1377226633Sdim  void Error(unsigned DiagID, StringRef Arg1 = StringRef(),
1378321369Sdim             StringRef Arg2 = StringRef()) const;
1379212795Sdim
1380288943Sdim  ASTReader(const ASTReader &) = delete;
1381288943Sdim  void operator=(const ASTReader &) = delete;
1382212795Sdimpublic:
1383212795Sdim  /// \brief Load the AST file and validate its contents against the given
1384212795Sdim  /// Preprocessor.
1385212795Sdim  ///
1386212795Sdim  /// \param PP the preprocessor associated with the context in which this
1387212795Sdim  /// precompiled header will be loaded.
1388212795Sdim  ///
1389212795Sdim  /// \param Context the AST context that this precompiled header will be
1390321369Sdim  /// loaded into, if any.
1391212795Sdim  ///
1392296417Sdim  /// \param PCHContainerRdr the PCHContainerOperations to use for loading and
1393288943Sdim  /// creating modules.
1394288943Sdim  ///
1395296417Sdim  /// \param Extensions the list of module file extensions that can be loaded
1396296417Sdim  /// from the AST files.
1397296417Sdim  ///
1398212795Sdim  /// \param isysroot If non-NULL, the system include path specified by the
1399212795Sdim  /// user. This is only used with relocatable PCH files. If non-NULL,
1400212795Sdim  /// a relocatable PCH file will use the default path "/".
1401212795Sdim  ///
1402212795Sdim  /// \param DisableValidation If true, the AST reader will suppress most
1403212795Sdim  /// of its regular consistency checking, allowing the use of precompiled
1404212795Sdim  /// headers that cannot be determined to be compatible.
1405218893Sdim  ///
1406234353Sdim  /// \param AllowASTWithCompilerErrors If true, the AST reader will accept an
1407234353Sdim  /// AST file the was created out of an AST with compiler errors,
1408234353Sdim  /// otherwise it will reject it.
1409249423Sdim  ///
1410276479Sdim  /// \param AllowConfigurationMismatch If true, the AST reader will not check
1411276479Sdim  /// for configuration differences between the AST file and the invocation.
1412276479Sdim  ///
1413276479Sdim  /// \param ValidateSystemInputs If true, the AST reader will validate
1414276479Sdim  /// system input files in addition to user input files. This is only
1415276479Sdim  /// meaningful if \p DisableValidation is false.
1416276479Sdim  ///
1417249423Sdim  /// \param UseGlobalIndex If true, the AST reader will try to load and use
1418249423Sdim  /// the global module index.
1419288943Sdim  ///
1420288943Sdim  /// \param ReadTimer If non-null, a timer used to track the time spent
1421288943Sdim  /// deserializing.
1422321369Sdim  ASTReader(Preprocessor &PP, ASTContext *Context,
1423288943Sdim            const PCHContainerReader &PCHContainerRdr,
1424314564Sdim            ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
1425288943Sdim            StringRef isysroot = "", bool DisableValidation = false,
1426249423Sdim            bool AllowASTWithCompilerErrors = false,
1427276479Sdim            bool AllowConfigurationMismatch = false,
1428288943Sdim            bool ValidateSystemInputs = false, bool UseGlobalIndex = true,
1429288943Sdim            std::unique_ptr<llvm::Timer> ReadTimer = {});
1430212795Sdim
1431288943Sdim  ~ASTReader() override;
1432212795Sdim
1433226633Sdim  SourceManager &getSourceManager() const { return SourceMgr; }
1434249423Sdim  FileManager &getFileManager() const { return FileMgr; }
1435296417Sdim  DiagnosticsEngine &getDiags() const { return Diags; }
1436234353Sdim
1437243830Sdim  /// \brief Flags that indicate what kind of AST loading failures the client
1438243830Sdim  /// of the AST reader can directly handle.
1439243830Sdim  ///
1440243830Sdim  /// When a client states that it can handle a particular kind of failure,
1441243830Sdim  /// the AST reader will not emit errors when producing that kind of failure.
1442243830Sdim  enum LoadFailureCapabilities {
1443243830Sdim    /// \brief The client can't handle any AST loading failures.
1444243830Sdim    ARR_None = 0,
1445243830Sdim    /// \brief The client can handle an AST file that cannot load because it
1446249423Sdim    /// is missing.
1447249423Sdim    ARR_Missing = 0x1,
1448249423Sdim    /// \brief The client can handle an AST file that cannot load because it
1449243830Sdim    /// is out-of-date relative to its input files.
1450249423Sdim    ARR_OutOfDate = 0x2,
1451243830Sdim    /// \brief The client can handle an AST file that cannot load because it
1452243830Sdim    /// was built with a different version of Clang.
1453249423Sdim    ARR_VersionMismatch = 0x4,
1454243830Sdim    /// \brief The client can handle an AST file that cannot load because it's
1455243830Sdim    /// compiled configuration doesn't match that of the context it was
1456243830Sdim    /// loaded into.
1457249423Sdim    ARR_ConfigurationMismatch = 0x8
1458243830Sdim  };
1459243830Sdim
1460226633Sdim  /// \brief Load the AST file designated by the given file name.
1461243830Sdim  ///
1462243830Sdim  /// \param FileName The name of the AST file to load.
1463243830Sdim  ///
1464243830Sdim  /// \param Type The kind of AST being loaded, e.g., PCH, module, main file,
1465243830Sdim  /// or preamble.
1466243830Sdim  ///
1467249423Sdim  /// \param ImportLoc the location where the module file will be considered as
1468249423Sdim  /// imported from. For non-module AST types it should be invalid.
1469249423Sdim  ///
1470243830Sdim  /// \param ClientLoadCapabilities The set of client load-failure
1471243830Sdim  /// capabilities, represented as a bitset of the enumerators of
1472243830Sdim  /// LoadFailureCapabilities.
1473314564Sdim  ///
1474314564Sdim  /// \param Imported optional out-parameter to append the list of modules
1475314564Sdim  /// that were imported by precompiled headers or any other non-module AST file
1476309124Sdim  ASTReadResult ReadAST(StringRef FileName, ModuleKind Type,
1477249423Sdim                        SourceLocation ImportLoc,
1478314564Sdim                        unsigned ClientLoadCapabilities,
1479314564Sdim                        SmallVectorImpl<ImportedSubmodule> *Imported = nullptr);
1480212795Sdim
1481234353Sdim  /// \brief Make the entities in the given module and any of its (non-explicit)
1482234353Sdim  /// submodules visible to name lookup.
1483234353Sdim  ///
1484234353Sdim  /// \param Mod The module whose names should be made visible.
1485234353Sdim  ///
1486239462Sdim  /// \param NameVisibility The level of visibility to give the names in the
1487239462Sdim  /// module.  Visibility can only be increased over time.
1488249423Sdim  ///
1489249423Sdim  /// \param ImportLoc The location at which the import occurs.
1490276479Sdim  void makeModuleVisible(Module *Mod,
1491249423Sdim                         Module::NameVisibilityKind NameVisibility,
1492288943Sdim                         SourceLocation ImportLoc);
1493276479Sdim
1494234353Sdim  /// \brief Make the names within this set of hidden names visible.
1495288943Sdim  void makeNamesVisible(const HiddenNames &Names, Module *Owner);
1496276479Sdim
1497314564Sdim  /// \brief Note that MergedDef is a redefinition of the canonical definition
1498314564Sdim  /// Def, so Def should be visible whenever MergedDef is.
1499314564Sdim  void mergeDefinitionVisibility(NamedDecl *Def, NamedDecl *MergedDef);
1500314564Sdim
1501280031Sdim  /// \brief Take the AST callbacks listener.
1502280031Sdim  std::unique_ptr<ASTReaderListener> takeListener() {
1503280031Sdim    return std::move(Listener);
1504280031Sdim  }
1505280031Sdim
1506212795Sdim  /// \brief Set the AST callbacks listener.
1507280031Sdim  void setListener(std::unique_ptr<ASTReaderListener> Listener) {
1508280031Sdim    this->Listener = std::move(Listener);
1509212795Sdim  }
1510212795Sdim
1511280031Sdim  /// \brief Add an AST callback listener.
1512276479Sdim  ///
1513276479Sdim  /// Takes ownership of \p L.
1514280031Sdim  void addListener(std::unique_ptr<ASTReaderListener> L) {
1515276479Sdim    if (Listener)
1516280031Sdim      L = llvm::make_unique<ChainedASTReaderListener>(std::move(L),
1517280031Sdim                                                      std::move(Listener));
1518280031Sdim    Listener = std::move(L);
1519276479Sdim  }
1520276479Sdim
1521280031Sdim  /// RAII object to temporarily add an AST callback listener.
1522280031Sdim  class ListenerScope {
1523280031Sdim    ASTReader &Reader;
1524280031Sdim    bool Chained;
1525280031Sdim
1526280031Sdim  public:
1527280031Sdim    ListenerScope(ASTReader &Reader, std::unique_ptr<ASTReaderListener> L)
1528280031Sdim        : Reader(Reader), Chained(false) {
1529280031Sdim      auto Old = Reader.takeListener();
1530280031Sdim      if (Old) {
1531280031Sdim        Chained = true;
1532280031Sdim        L = llvm::make_unique<ChainedASTReaderListener>(std::move(L),
1533280031Sdim                                                        std::move(Old));
1534280031Sdim      }
1535280031Sdim      Reader.setListener(std::move(L));
1536280031Sdim    }
1537280031Sdim    ~ListenerScope() {
1538280031Sdim      auto New = Reader.takeListener();
1539280031Sdim      if (Chained)
1540280031Sdim        Reader.setListener(static_cast<ChainedASTReaderListener *>(New.get())
1541280031Sdim                               ->takeSecond());
1542280031Sdim    }
1543280031Sdim  };
1544280031Sdim
1545212795Sdim  /// \brief Set the AST deserialization listener.
1546276479Sdim  void setDeserializationListener(ASTDeserializationListener *Listener,
1547276479Sdim                                  bool TakeOwnership = false);
1548212795Sdim
1549249423Sdim  /// \brief Determine whether this AST reader has a global index.
1550276479Sdim  bool hasGlobalIndex() const { return (bool)GlobalIndex; }
1551249423Sdim
1552276479Sdim  /// \brief Return global module index.
1553276479Sdim  GlobalModuleIndex *getGlobalIndex() { return GlobalIndex.get(); }
1554276479Sdim
1555276479Sdim  /// \brief Reset reader for a reload try.
1556276479Sdim  void resetForReload() { TriedLoadingGlobalIndex = false; }
1557276479Sdim
1558249423Sdim  /// \brief Attempts to load the global index.
1559249423Sdim  ///
1560249423Sdim  /// \returns true if loading the global index has failed for any reason.
1561249423Sdim  bool loadGlobalIndex();
1562249423Sdim
1563249423Sdim  /// \brief Determine whether we tried to load the global index, but failed,
1564249423Sdim  /// e.g., because it is out-of-date or does not exist.
1565249423Sdim  bool isGlobalIndexUnavailable() const;
1566249423Sdim
1567226633Sdim  /// \brief Initializes the ASTContext
1568226633Sdim  void InitializeContext();
1569212795Sdim
1570261991Sdim  /// \brief Update the state of Sema after loading some additional modules.
1571261991Sdim  void UpdateSema();
1572261991Sdim
1573226633Sdim  /// \brief Add in-memory (virtual file) buffer.
1574280031Sdim  void addInMemoryBuffer(StringRef &FileName,
1575280031Sdim                         std::unique_ptr<llvm::MemoryBuffer> Buffer) {
1576280031Sdim    ModuleMgr.addInMemoryBuffer(FileName, std::move(Buffer));
1577221345Sdim  }
1578221345Sdim
1579234353Sdim  /// \brief Finalizes the AST reader's state before writing an AST file to
1580234353Sdim  /// disk.
1581234353Sdim  ///
1582234353Sdim  /// This operation may undo temporary state in the AST that should not be
1583234353Sdim  /// emitted.
1584234353Sdim  void finalizeForWriting();
1585234353Sdim
1586226633Sdim  /// \brief Retrieve the module manager.
1587226633Sdim  ModuleManager &getModuleManager() { return ModuleMgr; }
1588212795Sdim
1589226633Sdim  /// \brief Retrieve the preprocessor.
1590226633Sdim  Preprocessor &getPreprocessor() const { return PP; }
1591234353Sdim
1592243830Sdim  /// \brief Retrieve the name of the original source file name for the primary
1593243830Sdim  /// module file.
1594243830Sdim  StringRef getOriginalSourceFile() {
1595243830Sdim    return ModuleMgr.getPrimaryModule().OriginalSourceFileName;
1596243830Sdim  }
1597212795Sdim
1598212795Sdim  /// \brief Retrieve the name of the original source file name directly from
1599212795Sdim  /// the AST file, without actually loading the AST file.
1600288943Sdim  static std::string
1601288943Sdim  getOriginalSourceFile(const std::string &ASTFileName, FileManager &FileMgr,
1602288943Sdim                        const PCHContainerReader &PCHContainerRdr,
1603288943Sdim                        DiagnosticsEngine &Diags);
1604212795Sdim
1605243830Sdim  /// \brief Read the control block for the named AST file.
1606243830Sdim  ///
1607243830Sdim  /// \returns true if an error occurred, false otherwise.
1608288943Sdim  static bool
1609288943Sdim  readASTFileControlBlock(StringRef Filename, FileManager &FileMgr,
1610288943Sdim                          const PCHContainerReader &PCHContainerRdr,
1611296417Sdim                          bool FindModuleFileExtensions,
1612314564Sdim                          ASTReaderListener &Listener,
1613314564Sdim                          bool ValidateDiagnosticOptions);
1614243830Sdim
1615243830Sdim  /// \brief Determine whether the given AST file is acceptable to load into a
1616243830Sdim  /// translation unit with the given language and target options.
1617288943Sdim  static bool isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
1618288943Sdim                                  const PCHContainerReader &PCHContainerRdr,
1619243830Sdim                                  const LangOptions &LangOpts,
1620243830Sdim                                  const TargetOptions &TargetOpts,
1621288943Sdim                                  const PreprocessorOptions &PPOpts,
1622321369Sdim                                  StringRef ExistingModuleCachePath);
1623243830Sdim
1624212795Sdim  /// \brief Returns the suggested contents of the predefines buffer,
1625212795Sdim  /// which contains a (typically-empty) subset of the predefines
1626212795Sdim  /// build prior to including the precompiled header.
1627212795Sdim  const std::string &getSuggestedPredefines() { return SuggestedPredefines; }
1628212795Sdim
1629226633Sdim  /// \brief Read a preallocated preprocessed entity from the external source.
1630226633Sdim  ///
1631226633Sdim  /// \returns null if an error occurred that prevented the preprocessed
1632226633Sdim  /// entity from being loaded.
1633276479Sdim  PreprocessedEntity *ReadPreprocessedEntity(unsigned Index) override;
1634218893Sdim
1635226633Sdim  /// \brief Returns a pair of [Begin, End) indices of preallocated
1636243830Sdim  /// preprocessed entities that \p Range encompasses.
1637276479Sdim  std::pair<unsigned, unsigned>
1638276479Sdim      findPreprocessedEntitiesInRange(SourceRange Range) override;
1639226633Sdim
1640234353Sdim  /// \brief Optionally returns true or false if the preallocated preprocessed
1641243830Sdim  /// entity with index \p Index came from file \p FID.
1642276479Sdim  Optional<bool> isPreprocessedEntityInFileID(unsigned Index,
1643276479Sdim                                              FileID FID) override;
1644234353Sdim
1645218893Sdim  /// \brief Read the header file information for the given file entry.
1646276479Sdim  HeaderFileInfo GetHeaderFileInfo(const FileEntry *FE) override;
1647218893Sdim
1648226633Sdim  void ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag);
1649218893Sdim
1650212795Sdim  /// \brief Returns the number of source locations found in the chain.
1651212795Sdim  unsigned getTotalNumSLocs() const {
1652212795Sdim    return TotalNumSLocEntries;
1653212795Sdim  }
1654212795Sdim
1655212795Sdim  /// \brief Returns the number of identifiers found in the chain.
1656212795Sdim  unsigned getTotalNumIdentifiers() const {
1657212795Sdim    return static_cast<unsigned>(IdentifiersLoaded.size());
1658212795Sdim  }
1659212795Sdim
1660243830Sdim  /// \brief Returns the number of macros found in the chain.
1661243830Sdim  unsigned getTotalNumMacros() const {
1662243830Sdim    return static_cast<unsigned>(MacrosLoaded.size());
1663243830Sdim  }
1664243830Sdim
1665212795Sdim  /// \brief Returns the number of types found in the chain.
1666212795Sdim  unsigned getTotalNumTypes() const {
1667212795Sdim    return static_cast<unsigned>(TypesLoaded.size());
1668212795Sdim  }
1669212795Sdim
1670212795Sdim  /// \brief Returns the number of declarations found in the chain.
1671212795Sdim  unsigned getTotalNumDecls() const {
1672212795Sdim    return static_cast<unsigned>(DeclsLoaded.size());
1673212795Sdim  }
1674212795Sdim
1675234353Sdim  /// \brief Returns the number of submodules known.
1676234353Sdim  unsigned getTotalNumSubmodules() const {
1677234353Sdim    return static_cast<unsigned>(SubmodulesLoaded.size());
1678234353Sdim  }
1679234353Sdim
1680212795Sdim  /// \brief Returns the number of selectors found in the chain.
1681212795Sdim  unsigned getTotalNumSelectors() const {
1682212795Sdim    return static_cast<unsigned>(SelectorsLoaded.size());
1683212795Sdim  }
1684212795Sdim
1685226633Sdim  /// \brief Returns the number of preprocessed entities known to the AST
1686226633Sdim  /// reader.
1687226633Sdim  unsigned getTotalNumPreprocessedEntities() const {
1688226633Sdim    unsigned Result = 0;
1689321369Sdim    for (const auto &M : ModuleMgr)
1690321369Sdim      Result += M.NumPreprocessedEntities;
1691226633Sdim    return Result;
1692218893Sdim  }
1693234353Sdim
1694212795Sdim  /// \brief Reads a TemplateArgumentLocInfo appropriate for the
1695212795Sdim  /// given TemplateArgument kind.
1696212795Sdim  TemplateArgumentLocInfo
1697234353Sdim  GetTemplateArgumentLocInfo(ModuleFile &F, TemplateArgument::ArgKind Kind,
1698212795Sdim                             const RecordData &Record, unsigned &Idx);
1699212795Sdim
1700212795Sdim  /// \brief Reads a TemplateArgumentLoc.
1701212795Sdim  TemplateArgumentLoc
1702234353Sdim  ReadTemplateArgumentLoc(ModuleFile &F,
1703212795Sdim                          const RecordData &Record, unsigned &Idx);
1704212795Sdim
1705261991Sdim  const ASTTemplateArgumentListInfo*
1706261991Sdim  ReadASTTemplateArgumentListInfo(ModuleFile &F,
1707261991Sdim                                  const RecordData &Record, unsigned &Index);
1708261991Sdim
1709212795Sdim  /// \brief Reads a declarator info from the given record.
1710234353Sdim  TypeSourceInfo *GetTypeSourceInfo(ModuleFile &F,
1711212795Sdim                                    const RecordData &Record, unsigned &Idx);
1712212795Sdim
1713212795Sdim  /// \brief Resolve a type ID into a type, potentially building a new
1714212795Sdim  /// type.
1715212795Sdim  QualType GetType(serialization::TypeID ID);
1716212795Sdim
1717226633Sdim  /// \brief Resolve a local type ID within a given AST file into a type.
1718234353Sdim  QualType getLocalType(ModuleFile &F, unsigned LocalID);
1719234353Sdim
1720226633Sdim  /// \brief Map a local type ID within a given AST file into a global type ID.
1721234353Sdim  serialization::TypeID getGlobalTypeID(ModuleFile &F, unsigned LocalID) const;
1722234353Sdim
1723234353Sdim  /// \brief Read a type from the current position in the given record, which
1724226633Sdim  /// was read from the given AST file.
1725234353Sdim  QualType readType(ModuleFile &F, const RecordData &Record, unsigned &Idx) {
1726226633Sdim    if (Idx >= Record.size())
1727226633Sdim      return QualType();
1728234353Sdim
1729226633Sdim    return getLocalType(F, Record[Idx++]);
1730226633Sdim  }
1731234353Sdim
1732234353Sdim  /// \brief Map from a local declaration ID within a given module to a
1733226633Sdim  /// global declaration ID.
1734243830Sdim  serialization::DeclID getGlobalDeclID(ModuleFile &F,
1735243830Sdim                                      serialization::LocalDeclID LocalID) const;
1736212795Sdim
1737243830Sdim  /// \brief Returns true if global DeclID \p ID originated from module \p M.
1738234353Sdim  bool isDeclIDFromModule(serialization::GlobalDeclID ID, ModuleFile &M) const;
1739234353Sdim
1740234353Sdim  /// \brief Retrieve the module file that owns the given declaration, or NULL
1741234353Sdim  /// if the declaration is not from a module file.
1742249423Sdim  ModuleFile *getOwningModuleFile(const Decl *D);
1743276479Sdim
1744276479Sdim  /// \brief Get the best name we know for the module that owns the given
1745276479Sdim  /// declaration, or an empty string if the declaration is not from a module.
1746276479Sdim  std::string getOwningModuleNameForDiagnostic(const Decl *D);
1747276479Sdim
1748243830Sdim  /// \brief Returns the source location for the decl \p ID.
1749234353Sdim  SourceLocation getSourceLocationForDeclID(serialization::GlobalDeclID ID);
1750234353Sdim
1751212795Sdim  /// \brief Resolve a declaration ID into a declaration, potentially
1752212795Sdim  /// building a new declaration.
1753212795Sdim  Decl *GetDecl(serialization::DeclID ID);
1754276479Sdim  Decl *GetExternalDecl(uint32_t ID) override;
1755212795Sdim
1756276479Sdim  /// \brief Resolve a declaration ID into a declaration. Return 0 if it's not
1757276479Sdim  /// been loaded yet.
1758276479Sdim  Decl *GetExistingDecl(serialization::DeclID ID);
1759276479Sdim
1760226633Sdim  /// \brief Reads a declaration with the given local ID in the given module.
1761234353Sdim  Decl *GetLocalDecl(ModuleFile &F, uint32_t LocalID) {
1762226633Sdim    return GetDecl(getGlobalDeclID(F, LocalID));
1763226633Sdim  }
1764226633Sdim
1765226633Sdim  /// \brief Reads a declaration with the given local ID in the given module.
1766226633Sdim  ///
1767226633Sdim  /// \returns The requested declaration, casted to the given return type.
1768226633Sdim  template<typename T>
1769234353Sdim  T *GetLocalDeclAs(ModuleFile &F, uint32_t LocalID) {
1770226633Sdim    return cast_or_null<T>(GetLocalDecl(F, LocalID));
1771226633Sdim  }
1772226633Sdim
1773234353Sdim  /// \brief Map a global declaration ID into the declaration ID used to
1774234353Sdim  /// refer to this declaration within the given module fule.
1775234353Sdim  ///
1776234353Sdim  /// \returns the global ID of the given declaration as known in the given
1777234353Sdim  /// module file.
1778234353Sdim  serialization::DeclID
1779234353Sdim  mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
1780234353Sdim                                  serialization::DeclID GlobalID);
1781234353Sdim
1782234353Sdim  /// \brief Reads a declaration ID from the given position in a record in the
1783226633Sdim  /// given module.
1784226633Sdim  ///
1785226633Sdim  /// \returns The declaration ID read from the record, adjusted to a global ID.
1786234353Sdim  serialization::DeclID ReadDeclID(ModuleFile &F, const RecordData &Record,
1787226633Sdim                                   unsigned &Idx);
1788234353Sdim
1789226633Sdim  /// \brief Reads a declaration from the given position in a record in the
1790226633Sdim  /// given module.
1791234353Sdim  Decl *ReadDecl(ModuleFile &F, const RecordData &R, unsigned &I) {
1792226633Sdim    return GetDecl(ReadDeclID(F, R, I));
1793226633Sdim  }
1794234353Sdim
1795226633Sdim  /// \brief Reads a declaration from the given position in a record in the
1796226633Sdim  /// given module.
1797226633Sdim  ///
1798226633Sdim  /// \returns The declaration read from this location, casted to the given
1799226633Sdim  /// result type.
1800226633Sdim  template<typename T>
1801234353Sdim  T *ReadDeclAs(ModuleFile &F, const RecordData &R, unsigned &I) {
1802226633Sdim    return cast_or_null<T>(GetDecl(ReadDeclID(F, R, I)));
1803226633Sdim  }
1804226633Sdim
1805276479Sdim  /// \brief If any redeclarations of \p D have been imported since it was
1806276479Sdim  /// last checked, this digs out those redeclarations and adds them to the
1807276479Sdim  /// redeclaration chain for \p D.
1808276479Sdim  void CompleteRedeclChain(const Decl *D) override;
1809276479Sdim
1810276479Sdim  CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset) override;
1811234353Sdim
1812212795Sdim  /// \brief Resolve the offset of a statement into a statement.
1813212795Sdim  ///
1814212795Sdim  /// This operation will read a new statement from the external
1815212795Sdim  /// source each time it is called, and is meant to be used via a
1816212795Sdim  /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
1817276479Sdim  Stmt *GetExternalDeclStmt(uint64_t Offset) override;
1818212795Sdim
1819212795Sdim  /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1820212795Sdim  /// specified cursor.  Read the abbreviations that are at the top of the block
1821212795Sdim  /// and then leave the cursor pointing into the block.
1822296417Sdim  static bool ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor, unsigned BlockID);
1823212795Sdim
1824212795Sdim  /// \brief Finds all the visible declarations with a given name.
1825212795Sdim  /// The current implementation of this method just loads the entire
1826212795Sdim  /// lookup table as unmaterialized references.
1827276479Sdim  bool FindExternalVisibleDeclsByName(const DeclContext *DC,
1828276479Sdim                                      DeclarationName Name) override;
1829212795Sdim
1830212795Sdim  /// \brief Read all of the declarations lexically stored in a
1831212795Sdim  /// declaration context.
1832212795Sdim  ///
1833212795Sdim  /// \param DC The declaration context whose declarations will be
1834212795Sdim  /// read.
1835212795Sdim  ///
1836296417Sdim  /// \param IsKindWeWant A predicate indicating which declaration kinds
1837296417Sdim  /// we are interested in.
1838296417Sdim  ///
1839212795Sdim  /// \param Decls Vector that will contain the declarations loaded
1840212795Sdim  /// from the external source. The caller is responsible for merging
1841212795Sdim  /// these declarations with any declarations already stored in the
1842212795Sdim  /// declaration context.
1843296417Sdim  void
1844296417Sdim  FindExternalLexicalDecls(const DeclContext *DC,
1845296417Sdim                           llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
1846296417Sdim                           SmallVectorImpl<Decl *> &Decls) override;
1847212795Sdim
1848234353Sdim  /// \brief Get the decls that are contained in a file in the Offset/Length
1849243830Sdim  /// range. \p Length can be 0 to indicate a point at \p Offset instead of
1850234353Sdim  /// a range.
1851276479Sdim  void FindFileRegionDecls(FileID File, unsigned Offset, unsigned Length,
1852276479Sdim                           SmallVectorImpl<Decl *> &Decls) override;
1853234353Sdim
1854212795Sdim  /// \brief Notify ASTReader that we started deserialization of
1855212795Sdim  /// a decl or type so until FinishedDeserializing is called there may be
1856212795Sdim  /// decls that are initializing. Must be paired with FinishedDeserializing.
1857288943Sdim  void StartedDeserializing() override;
1858212795Sdim
1859212795Sdim  /// \brief Notify ASTReader that we finished the deserialization of
1860212795Sdim  /// a decl or type. Must be paired with StartedDeserializing.
1861276479Sdim  void FinishedDeserializing() override;
1862212795Sdim
1863212795Sdim  /// \brief Function that will be invoked when we begin parsing a new
1864212795Sdim  /// translation unit involving this external AST source.
1865212795Sdim  ///
1866212795Sdim  /// This function will provide all of the external definitions to
1867212795Sdim  /// the ASTConsumer.
1868276479Sdim  void StartTranslationUnit(ASTConsumer *Consumer) override;
1869212795Sdim
1870212795Sdim  /// \brief Print some statistics about AST usage.
1871276479Sdim  void PrintStats() override;
1872212795Sdim
1873226633Sdim  /// \brief Dump information about the AST reader to standard error.
1874226633Sdim  void dump();
1875234353Sdim
1876221345Sdim  /// Return the amount of memory used by memory buffers, breaking down
1877221345Sdim  /// by heap-backed versus mmap'ed memory.
1878276479Sdim  void getMemoryBufferSizes(MemoryBufferSizes &sizes) const override;
1879221345Sdim
1880212795Sdim  /// \brief Initialize the semantic source with the Sema instance
1881212795Sdim  /// being used to perform semantic analysis on the abstract syntax
1882212795Sdim  /// tree.
1883276479Sdim  void InitializeSema(Sema &S) override;
1884212795Sdim
1885212795Sdim  /// \brief Inform the semantic consumer that Sema is no longer available.
1886276479Sdim  void ForgetSema() override { SemaObj = nullptr; }
1887212795Sdim
1888212795Sdim  /// \brief Retrieve the IdentifierInfo for the named identifier.
1889212795Sdim  ///
1890212795Sdim  /// This routine builds a new IdentifierInfo for the given identifier. If any
1891212795Sdim  /// declarations with this name are visible from translation unit scope, their
1892212795Sdim  /// declarations will be deserialized and introduced into the declaration
1893212795Sdim  /// chain of the identifier.
1894296417Sdim  IdentifierInfo *get(StringRef Name) override;
1895212795Sdim
1896218893Sdim  /// \brief Retrieve an iterator into the set of all identifiers
1897218893Sdim  /// in all loaded AST files.
1898276479Sdim  IdentifierIterator *getIdentifiers() override;
1899218893Sdim
1900212795Sdim  /// \brief Load the contents of the global method pool for a given
1901212795Sdim  /// selector.
1902276479Sdim  void ReadMethodPool(Selector Sel) override;
1903212795Sdim
1904309124Sdim  /// Load the contents of the global method pool for a given
1905309124Sdim  /// selector if necessary.
1906309124Sdim  void updateOutOfDateSelector(Selector Sel) override;
1907309124Sdim
1908224145Sdim  /// \brief Load the set of namespaces that are known to the external source,
1909224145Sdim  /// which will be used during typo correction.
1910276479Sdim  void ReadKnownNamespaces(
1911276479Sdim                         SmallVectorImpl<NamespaceDecl *> &Namespaces) override;
1912224145Sdim
1913276479Sdim  void ReadUndefinedButUsed(
1914309124Sdim      llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) override;
1915249423Sdim
1916288943Sdim  void ReadMismatchingDeleteExpressions(llvm::MapVector<
1917288943Sdim      FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
1918288943Sdim                                            Exprs) override;
1919288943Sdim
1920276479Sdim  void ReadTentativeDefinitions(
1921276479Sdim                            SmallVectorImpl<VarDecl *> &TentativeDefs) override;
1922226633Sdim
1923276479Sdim  void ReadUnusedFileScopedDecls(
1924276479Sdim                       SmallVectorImpl<const DeclaratorDecl *> &Decls) override;
1925226633Sdim
1926276479Sdim  void ReadDelegatingConstructors(
1927276479Sdim                         SmallVectorImpl<CXXConstructorDecl *> &Decls) override;
1928226633Sdim
1929276479Sdim  void ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) override;
1930226633Sdim
1931280031Sdim  void ReadUnusedLocalTypedefNameCandidates(
1932280031Sdim      llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) override;
1933280031Sdim
1934276479Sdim  void ReadReferencedSelectors(
1935276479Sdim          SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) override;
1936226633Sdim
1937276479Sdim  void ReadWeakUndeclaredIdentifiers(
1938276479Sdim          SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WI) override;
1939226633Sdim
1940276479Sdim  void ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) override;
1941226633Sdim
1942276479Sdim  void ReadPendingInstantiations(
1943234353Sdim                 SmallVectorImpl<std::pair<ValueDecl *,
1944276479Sdim                                           SourceLocation> > &Pending) override;
1945226633Sdim
1946276479Sdim  void ReadLateParsedTemplates(
1947314564Sdim      llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
1948314564Sdim          &LPTMap) override;
1949261991Sdim
1950212795Sdim  /// \brief Load a selector from disk, registering its ID if it exists.
1951212795Sdim  void LoadSelector(Selector Sel);
1952212795Sdim
1953212795Sdim  void SetIdentifierInfo(unsigned ID, IdentifierInfo *II);
1954212795Sdim  void SetGloballyVisibleDecls(IdentifierInfo *II,
1955226633Sdim                               const SmallVectorImpl<uint32_t> &DeclIDs,
1956276479Sdim                               SmallVectorImpl<Decl *> *Decls = nullptr);
1957212795Sdim
1958212795Sdim  /// \brief Report a diagnostic.
1959321369Sdim  DiagnosticBuilder Diag(unsigned DiagID) const;
1960212795Sdim
1961212795Sdim  /// \brief Report a diagnostic.
1962321369Sdim  DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const;
1963212795Sdim
1964226633Sdim  IdentifierInfo *DecodeIdentifierInfo(serialization::IdentifierID ID);
1965212795Sdim
1966234353Sdim  IdentifierInfo *GetIdentifierInfo(ModuleFile &M, const RecordData &Record,
1967226633Sdim                                    unsigned &Idx) {
1968226633Sdim    return DecodeIdentifierInfo(getGlobalIdentifierID(M, Record[Idx++]));
1969212795Sdim  }
1970212795Sdim
1971276479Sdim  IdentifierInfo *GetIdentifier(serialization::IdentifierID ID) override {
1972243830Sdim    // Note that we are loading an identifier.
1973243830Sdim    Deserializing AnIdentifier(this);
1974243830Sdim
1975212795Sdim    return DecodeIdentifierInfo(ID);
1976212795Sdim  }
1977212795Sdim
1978234353Sdim  IdentifierInfo *getLocalIdentifier(ModuleFile &M, unsigned LocalID);
1979234353Sdim
1980234353Sdim  serialization::IdentifierID getGlobalIdentifierID(ModuleFile &M,
1981226633Sdim                                                    unsigned LocalID);
1982234353Sdim
1983249423Sdim  void resolvePendingMacro(IdentifierInfo *II, const PendingMacroInfo &PMInfo);
1984249423Sdim
1985243830Sdim  /// \brief Retrieve the macro with the given ID.
1986249423Sdim  MacroInfo *getMacro(serialization::MacroID ID);
1987243830Sdim
1988243830Sdim  /// \brief Retrieve the global macro ID corresponding to the given local
1989243830Sdim  /// ID within the given module file.
1990243830Sdim  serialization::MacroID getGlobalMacroID(ModuleFile &M, unsigned LocalID);
1991243830Sdim
1992212795Sdim  /// \brief Read the source location entry with index ID.
1993276479Sdim  bool ReadSLocEntry(int ID) override;
1994212795Sdim
1995249423Sdim  /// \brief Retrieve the module import location and module name for the
1996249423Sdim  /// given source manager entry ID.
1997276479Sdim  std::pair<SourceLocation, StringRef> getModuleImportLoc(int ID) override;
1998249423Sdim
1999234353Sdim  /// \brief Retrieve the global submodule ID given a module and its local ID
2000234353Sdim  /// number.
2001234353Sdim  serialization::SubmoduleID
2002234353Sdim  getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID);
2003314564Sdim
2004234353Sdim  /// \brief Retrieve the submodule that corresponds to a global submodule ID.
2005234353Sdim  ///
2006234353Sdim  Module *getSubmodule(serialization::SubmoduleID GlobalID);
2007249423Sdim
2008249423Sdim  /// \brief Retrieve the module that corresponds to the given module ID.
2009249423Sdim  ///
2010249423Sdim  /// Note: overrides method in ExternalASTSource
2011276479Sdim  Module *getModule(unsigned ID) override;
2012249423Sdim
2013296417Sdim  /// \brief Retrieve the module file with a given local ID within the specified
2014296417Sdim  /// ModuleFile.
2015296417Sdim  ModuleFile *getLocalModuleFile(ModuleFile &M, unsigned ID);
2016296417Sdim
2017296417Sdim  /// \brief Get an ID for the given module file.
2018296417Sdim  unsigned getModuleFileID(ModuleFile *M);
2019296417Sdim
2020288943Sdim  /// \brief Return a descriptor for the corresponding module.
2021288943Sdim  llvm::Optional<ASTSourceDescriptor> getSourceDescriptor(unsigned ID) override;
2022288943Sdim
2023321369Sdim  ExtKind hasExternalDefinitions(const Decl *D) override;
2024321369Sdim
2025226633Sdim  /// \brief Retrieve a selector from the given module with its local ID
2026226633Sdim  /// number.
2027234353Sdim  Selector getLocalSelector(ModuleFile &M, unsigned LocalID);
2028212795Sdim
2029226633Sdim  Selector DecodeSelector(serialization::SelectorID Idx);
2030226633Sdim
2031276479Sdim  Selector GetExternalSelector(serialization::SelectorID ID) override;
2032276479Sdim  uint32_t GetNumExternalSelectors() override;
2033212795Sdim
2034234353Sdim  Selector ReadSelector(ModuleFile &M, const RecordData &Record, unsigned &Idx) {
2035226633Sdim    return getLocalSelector(M, Record[Idx++]);
2036212795Sdim  }
2037234353Sdim
2038226633Sdim  /// \brief Retrieve the global selector ID that corresponds to this
2039226633Sdim  /// the local selector ID in a given module.
2040234353Sdim  serialization::SelectorID getGlobalSelectorID(ModuleFile &F,
2041226633Sdim                                                unsigned LocalID) const;
2042212795Sdim
2043212795Sdim  /// \brief Read a declaration name.
2044234353Sdim  DeclarationName ReadDeclarationName(ModuleFile &F,
2045226633Sdim                                      const RecordData &Record, unsigned &Idx);
2046234353Sdim  void ReadDeclarationNameLoc(ModuleFile &F,
2047218893Sdim                              DeclarationNameLoc &DNLoc, DeclarationName Name,
2048218893Sdim                              const RecordData &Record, unsigned &Idx);
2049234353Sdim  void ReadDeclarationNameInfo(ModuleFile &F, DeclarationNameInfo &NameInfo,
2050218893Sdim                               const RecordData &Record, unsigned &Idx);
2051212795Sdim
2052234353Sdim  void ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
2053218893Sdim                         const RecordData &Record, unsigned &Idx);
2054218893Sdim
2055234353Sdim  NestedNameSpecifier *ReadNestedNameSpecifier(ModuleFile &F,
2056226633Sdim                                               const RecordData &Record,
2057212795Sdim                                               unsigned &Idx);
2058212795Sdim
2059234353Sdim  NestedNameSpecifierLoc ReadNestedNameSpecifierLoc(ModuleFile &F,
2060219077Sdim                                                    const RecordData &Record,
2061219077Sdim                                                    unsigned &Idx);
2062219077Sdim
2063212795Sdim  /// \brief Read a template name.
2064234353Sdim  TemplateName ReadTemplateName(ModuleFile &F, const RecordData &Record,
2065218893Sdim                                unsigned &Idx);
2066212795Sdim
2067212795Sdim  /// \brief Read a template argument.
2068296417Sdim  TemplateArgument ReadTemplateArgument(ModuleFile &F, const RecordData &Record,
2069296417Sdim                                        unsigned &Idx,
2070296417Sdim                                        bool Canonicalize = false);
2071234353Sdim
2072212795Sdim  /// \brief Read a template parameter list.
2073234353Sdim  TemplateParameterList *ReadTemplateParameterList(ModuleFile &F,
2074218893Sdim                                                   const RecordData &Record,
2075212795Sdim                                                   unsigned &Idx);
2076234353Sdim
2077212795Sdim  /// \brief Read a template argument array.
2078296417Sdim  void ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
2079296417Sdim                                ModuleFile &F, const RecordData &Record,
2080296417Sdim                                unsigned &Idx, bool Canonicalize = false);
2081212795Sdim
2082212795Sdim  /// \brief Read a UnresolvedSet structure.
2083261991Sdim  void ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
2084212795Sdim                         const RecordData &Record, unsigned &Idx);
2085212795Sdim
2086212795Sdim  /// \brief Read a C++ base specifier.
2087234353Sdim  CXXBaseSpecifier ReadCXXBaseSpecifier(ModuleFile &F,
2088212795Sdim                                        const RecordData &Record,unsigned &Idx);
2089212795Sdim
2090218893Sdim  /// \brief Read a CXXCtorInitializer array.
2091288943Sdim  CXXCtorInitializer **
2092234353Sdim  ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
2093218893Sdim                          unsigned &Idx);
2094212795Sdim
2095288943Sdim  /// \brief Read the contents of a CXXCtorInitializer array.
2096288943Sdim  CXXCtorInitializer **GetExternalCXXCtorInitializers(uint64_t Offset) override;
2097288943Sdim
2098309124Sdim  /// \brief Read a source location from raw form and return it in its
2099309124Sdim  /// originating module file's source location space.
2100309124Sdim  SourceLocation ReadUntranslatedSourceLocation(uint32_t Raw) const {
2101309124Sdim    return SourceLocation::getFromRawEncoding((Raw >> 1) | (Raw << 31));
2102309124Sdim  }
2103309124Sdim
2104218893Sdim  /// \brief Read a source location from raw form.
2105309124Sdim  SourceLocation ReadSourceLocation(ModuleFile &ModuleFile, uint32_t Raw) const {
2106309124Sdim    SourceLocation Loc = ReadUntranslatedSourceLocation(Raw);
2107309124Sdim    return TranslateSourceLocation(ModuleFile, Loc);
2108309124Sdim  }
2109309124Sdim
2110309124Sdim  /// \brief Translate a source location from another module file's source
2111309124Sdim  /// location space into ours.
2112309124Sdim  SourceLocation TranslateSourceLocation(ModuleFile &ModuleFile,
2113309124Sdim                                         SourceLocation Loc) const {
2114321369Sdim    if (!ModuleFile.ModuleOffsetMap.empty())
2115321369Sdim      ReadModuleOffsetMap(ModuleFile);
2116309124Sdim    assert(ModuleFile.SLocRemap.find(Loc.getOffset()) !=
2117309124Sdim               ModuleFile.SLocRemap.end() &&
2118226633Sdim           "Cannot find offset to remap.");
2119234353Sdim    int Remap = ModuleFile.SLocRemap.find(Loc.getOffset())->second;
2120226633Sdim    return Loc.getLocWithOffset(Remap);
2121218893Sdim  }
2122218893Sdim
2123212795Sdim  /// \brief Read a source location.
2124234353Sdim  SourceLocation ReadSourceLocation(ModuleFile &ModuleFile,
2125261991Sdim                                    const RecordDataImpl &Record,
2126261991Sdim                                    unsigned &Idx) {
2127234353Sdim    return ReadSourceLocation(ModuleFile, Record[Idx++]);
2128212795Sdim  }
2129212795Sdim
2130212795Sdim  /// \brief Read a source range.
2131234353Sdim  SourceRange ReadSourceRange(ModuleFile &F,
2132249423Sdim                              const RecordData &Record, unsigned &Idx);
2133212795Sdim
2134212795Sdim  /// \brief Read an integral value
2135212795Sdim  llvm::APInt ReadAPInt(const RecordData &Record, unsigned &Idx);
2136212795Sdim
2137212795Sdim  /// \brief Read a signed integral value
2138212795Sdim  llvm::APSInt ReadAPSInt(const RecordData &Record, unsigned &Idx);
2139212795Sdim
2140212795Sdim  /// \brief Read a floating-point value
2141249423Sdim  llvm::APFloat ReadAPFloat(const RecordData &Record,
2142249423Sdim                            const llvm::fltSemantics &Sem, unsigned &Idx);
2143212795Sdim
2144212795Sdim  // \brief Read a string
2145243830Sdim  static std::string ReadString(const RecordData &Record, unsigned &Idx);
2146212795Sdim
2147280031Sdim  // \brief Read a path
2148280031Sdim  std::string ReadPath(ModuleFile &F, const RecordData &Record, unsigned &Idx);
2149280031Sdim
2150221345Sdim  /// \brief Read a version tuple.
2151243830Sdim  static VersionTuple ReadVersionTuple(const RecordData &Record, unsigned &Idx);
2152221345Sdim
2153234353Sdim  CXXTemporary *ReadCXXTemporary(ModuleFile &F, const RecordData &Record,
2154226633Sdim                                 unsigned &Idx);
2155234353Sdim
2156212795Sdim  /// \brief Reads attributes from the current stream position.
2157321369Sdim  void ReadAttributes(ASTRecordReader &Record, AttrVec &Attrs);
2158212795Sdim
2159212795Sdim  /// \brief Reads a statement.
2160234353Sdim  Stmt *ReadStmt(ModuleFile &F);
2161212795Sdim
2162212795Sdim  /// \brief Reads an expression.
2163234353Sdim  Expr *ReadExpr(ModuleFile &F);
2164212795Sdim
2165212795Sdim  /// \brief Reads a sub-statement operand during statement reading.
2166212795Sdim  Stmt *ReadSubStmt() {
2167212795Sdim    assert(ReadingKind == Read_Stmt &&
2168212795Sdim           "Should be called only during statement reading!");
2169212795Sdim    // Subexpressions are stored from last to first, so the next Stmt we need
2170212795Sdim    // is at the back of the stack.
2171276479Sdim    assert(!StmtStack.empty() && "Read too many sub-statements!");
2172212795Sdim    return StmtStack.pop_back_val();
2173212795Sdim  }
2174212795Sdim
2175212795Sdim  /// \brief Reads a sub-expression operand during statement reading.
2176212795Sdim  Expr *ReadSubExpr();
2177212795Sdim
2178251662Sdim  /// \brief Reads a token out of a record.
2179261991Sdim  Token ReadToken(ModuleFile &M, const RecordDataImpl &Record, unsigned &Idx);
2180251662Sdim
2181212795Sdim  /// \brief Reads the macro record located at the given offset.
2182249423Sdim  MacroInfo *ReadMacroRecord(ModuleFile &F, uint64_t Offset);
2183234353Sdim
2184226633Sdim  /// \brief Determine the global preprocessed entity ID that corresponds to
2185226633Sdim  /// the given local ID within the given module.
2186234353Sdim  serialization::PreprocessedEntityID
2187234353Sdim  getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const;
2188234353Sdim
2189288943Sdim  /// \brief Add a macro to deserialize its macro directive history.
2190234353Sdim  ///
2191234353Sdim  /// \param II The name of the macro.
2192249423Sdim  /// \param M The module file.
2193249423Sdim  /// \param MacroDirectivesOffset Offset of the serialized macro directive
2194249423Sdim  /// history.
2195288943Sdim  void addPendingMacro(IdentifierInfo *II, ModuleFile *M,
2196288943Sdim                       uint64_t MacroDirectivesOffset);
2197234353Sdim
2198212795Sdim  /// \brief Read the set of macros defined by this external macro source.
2199276479Sdim  void ReadDefinedMacros() override;
2200212795Sdim
2201234353Sdim  /// \brief Update an out-of-date identifier.
2202276479Sdim  void updateOutOfDateIdentifier(IdentifierInfo &II) override;
2203234353Sdim
2204234353Sdim  /// \brief Note that this identifier is up-to-date.
2205234353Sdim  void markIdentifierUpToDate(IdentifierInfo *II);
2206234353Sdim
2207234353Sdim  /// \brief Load all external visible decls in the given DeclContext.
2208276479Sdim  void completeVisibleDeclsMap(const DeclContext *DC) override;
2209234353Sdim
2210212795Sdim  /// \brief Retrieve the AST context that this AST reader supplements.
2211321369Sdim  ASTContext &getContext() {
2212321369Sdim    assert(ContextObj && "requested AST context when not loading AST");
2213321369Sdim    return *ContextObj;
2214321369Sdim  }
2215212795Sdim
2216280031Sdim  // \brief Contains the IDs for declarations that were requested before we have
2217212795Sdim  // access to a Sema object.
2218280031Sdim  SmallVector<uint64_t, 16> PreloadedDeclIDs;
2219212795Sdim
2220212795Sdim  /// \brief Retrieve the semantic analysis object used to analyze the
2221212795Sdim  /// translation unit in which the precompiled header is being
2222212795Sdim  /// imported.
2223212795Sdim  Sema *getSema() { return SemaObj; }
2224212795Sdim
2225309124Sdim  /// \brief Get the identifier resolver used for name lookup / updates
2226309124Sdim  /// in the translation unit scope. We have one of these even if we don't
2227309124Sdim  /// have a Sema object.
2228309124Sdim  IdentifierResolver &getIdResolver();
2229309124Sdim
2230212795Sdim  /// \brief Retrieve the identifier table associated with the
2231212795Sdim  /// preprocessor.
2232212795Sdim  IdentifierTable &getIdentifierTable();
2233212795Sdim
2234212795Sdim  /// \brief Record that the given ID maps to the given switch-case
2235212795Sdim  /// statement.
2236212795Sdim  void RecordSwitchCaseID(SwitchCase *SC, unsigned ID);
2237212795Sdim
2238212795Sdim  /// \brief Retrieve the switch-case statement with the given ID.
2239212795Sdim  SwitchCase *getSwitchCaseWithID(unsigned ID);
2240212795Sdim
2241218893Sdim  void ClearSwitchCaseIDs();
2242239462Sdim
2243239462Sdim  /// \brief Cursors for comments blocks.
2244239462Sdim  SmallVector<std::pair<llvm::BitstreamCursor,
2245239462Sdim                        serialization::ModuleFile *>, 8> CommentsCursors;
2246239462Sdim
2247296417Sdim  /// \brief Loads comments ranges.
2248276479Sdim  void ReadComments() override;
2249309124Sdim
2250321369Sdim  /// Visit all the input files of the given module file.
2251321369Sdim  void visitInputFiles(serialization::ModuleFile &MF,
2252321369Sdim                       bool IncludeSystem, bool Complain,
2253321369Sdim          llvm::function_ref<void(const serialization::InputFile &IF,
2254321369Sdim                                  bool isSystem)> Visitor);
2255321369Sdim
2256321369Sdim  /// Visit all the top-level module maps loaded when building the given module
2257321369Sdim  /// file.
2258321369Sdim  void visitTopLevelModuleMaps(serialization::ModuleFile &MF,
2259321369Sdim                               llvm::function_ref<
2260321369Sdim                                   void(const FileEntry *)> Visitor);
2261321369Sdim
2262309124Sdim  bool isProcessingUpdateRecords() { return ProcessingUpdateRecords; }
2263212795Sdim};
2264212795Sdim
2265314564Sdim/// \brief An object for streaming information from a record.
2266314564Sdimclass ASTRecordReader {
2267314564Sdim  typedef serialization::ModuleFile ModuleFile;
2268314564Sdim
2269314564Sdim  ASTReader *Reader;
2270314564Sdim  ModuleFile *F;
2271314564Sdim  unsigned Idx = 0;
2272314564Sdim  ASTReader::RecordData Record;
2273314564Sdim
2274314564Sdim  typedef ASTReader::RecordData RecordData;
2275314564Sdim  typedef ASTReader::RecordDataImpl RecordDataImpl;
2276314564Sdim
2277314564Sdimpublic:
2278314564Sdim  /// Construct an ASTRecordReader that uses the default encoding scheme.
2279314564Sdim  ASTRecordReader(ASTReader &Reader, ModuleFile &F)
2280314564Sdim      : Reader(&Reader), F(&F) {}
2281314564Sdim
2282314564Sdim  /// \brief Reads a record with id AbbrevID from Cursor, resetting the
2283314564Sdim  /// internal state.
2284314564Sdim  unsigned readRecord(llvm::BitstreamCursor &Cursor, unsigned AbbrevID);
2285314564Sdim
2286314564Sdim  /// \brief Is this a module file for a module (rather than a PCH or similar).
2287314564Sdim  bool isModule() const { return F->isModule(); }
2288314564Sdim
2289314564Sdim  /// \brief Retrieve the AST context that this AST reader supplements.
2290314564Sdim  ASTContext &getContext() { return Reader->getContext(); }
2291314564Sdim
2292314564Sdim  /// \brief The current position in this record.
2293314564Sdim  unsigned getIdx() const { return Idx; }
2294314564Sdim  /// \brief The length of this record.
2295314564Sdim  size_t size() const { return Record.size(); }
2296314564Sdim
2297314564Sdim  /// \brief An arbitrary index in this record.
2298314564Sdim  const uint64_t &operator[](size_t N) { return Record[N]; }
2299314564Sdim  /// \brief The last element in this record.
2300314564Sdim  const uint64_t &back() const { return Record.back(); }
2301314564Sdim
2302314564Sdim  /// \brief Returns the current value in this record, and advances to the
2303314564Sdim  /// next value.
2304314564Sdim  const uint64_t &readInt() { return Record[Idx++]; }
2305314564Sdim  /// \brief Returns the current value in this record, without advancing.
2306314564Sdim  const uint64_t &peekInt() { return Record[Idx]; }
2307314564Sdim
2308314564Sdim  /// \brief Skips the specified number of values.
2309314564Sdim  void skipInts(unsigned N) { Idx += N; }
2310314564Sdim
2311314564Sdim  /// \brief Retrieve the global submodule ID its local ID number.
2312314564Sdim  serialization::SubmoduleID
2313314564Sdim  getGlobalSubmoduleID(unsigned LocalID) {
2314314564Sdim    return Reader->getGlobalSubmoduleID(*F, LocalID);
2315314564Sdim  }
2316314564Sdim
2317314564Sdim  /// \brief Retrieve the submodule that corresponds to a global submodule ID.
2318314564Sdim  Module *getSubmodule(serialization::SubmoduleID GlobalID) {
2319314564Sdim    return Reader->getSubmodule(GlobalID);
2320314564Sdim  }
2321314564Sdim
2322314564Sdim  /// \brief Read the record that describes the lexical contents of a DC.
2323314564Sdim  bool readLexicalDeclContextStorage(uint64_t Offset, DeclContext *DC) {
2324314564Sdim    return Reader->ReadLexicalDeclContextStorage(*F, F->DeclsCursor, Offset,
2325314564Sdim                                                 DC);
2326314564Sdim  }
2327314564Sdim
2328314564Sdim  /// \brief Read the record that describes the visible contents of a DC.
2329314564Sdim  bool readVisibleDeclContextStorage(uint64_t Offset,
2330314564Sdim                                     serialization::DeclID ID) {
2331314564Sdim    return Reader->ReadVisibleDeclContextStorage(*F, F->DeclsCursor, Offset,
2332314564Sdim                                                 ID);
2333314564Sdim  }
2334314564Sdim
2335314564Sdim  void readExceptionSpec(SmallVectorImpl<QualType> &ExceptionStorage,
2336314564Sdim                         FunctionProtoType::ExceptionSpecInfo &ESI) {
2337314564Sdim    return Reader->readExceptionSpec(*F, ExceptionStorage, ESI, Record, Idx);
2338314564Sdim  }
2339314564Sdim
2340314564Sdim  /// \brief Get the global offset corresponding to a local offset.
2341314564Sdim  uint64_t getGlobalBitOffset(uint32_t LocalOffset) {
2342314564Sdim    return Reader->getGlobalBitOffset(*F, LocalOffset);
2343314564Sdim  }
2344314564Sdim
2345314564Sdim  /// \brief Reads a statement.
2346314564Sdim  Stmt *readStmt() { return Reader->ReadStmt(*F); }
2347314564Sdim
2348314564Sdim  /// \brief Reads an expression.
2349314564Sdim  Expr *readExpr() { return Reader->ReadExpr(*F); }
2350314564Sdim
2351314564Sdim  /// \brief Reads a sub-statement operand during statement reading.
2352314564Sdim  Stmt *readSubStmt() { return Reader->ReadSubStmt(); }
2353314564Sdim
2354314564Sdim  /// \brief Reads a sub-expression operand during statement reading.
2355314564Sdim  Expr *readSubExpr() { return Reader->ReadSubExpr(); }
2356314564Sdim
2357321369Sdim  /// \brief Reads a declaration with the given local ID in the given module.
2358321369Sdim  ///
2359321369Sdim  /// \returns The requested declaration, casted to the given return type.
2360321369Sdim  template<typename T>
2361321369Sdim  T *GetLocalDeclAs(uint32_t LocalID) {
2362321369Sdim    return cast_or_null<T>(Reader->GetLocalDecl(*F, LocalID));
2363321369Sdim  }
2364321369Sdim
2365314564Sdim  /// \brief Reads a TemplateArgumentLocInfo appropriate for the
2366314564Sdim  /// given TemplateArgument kind, advancing Idx.
2367314564Sdim  TemplateArgumentLocInfo
2368314564Sdim  getTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
2369314564Sdim    return Reader->GetTemplateArgumentLocInfo(*F, Kind, Record, Idx);
2370314564Sdim  }
2371314564Sdim
2372314564Sdim  /// \brief Reads a TemplateArgumentLoc, advancing Idx.
2373314564Sdim  TemplateArgumentLoc
2374314564Sdim  readTemplateArgumentLoc() {
2375314564Sdim    return Reader->ReadTemplateArgumentLoc(*F, Record, Idx);
2376314564Sdim  }
2377314564Sdim
2378314564Sdim  const ASTTemplateArgumentListInfo*
2379314564Sdim  readASTTemplateArgumentListInfo() {
2380314564Sdim    return Reader->ReadASTTemplateArgumentListInfo(*F, Record, Idx);
2381314564Sdim  }
2382314564Sdim
2383314564Sdim  /// \brief Reads a declarator info from the given record, advancing Idx.
2384314564Sdim  TypeSourceInfo *getTypeSourceInfo() {
2385314564Sdim    return Reader->GetTypeSourceInfo(*F, Record, Idx);
2386314564Sdim  }
2387314564Sdim
2388314564Sdim  /// \brief Map a local type ID within a given AST file to a global type ID.
2389314564Sdim  serialization::TypeID getGlobalTypeID(unsigned LocalID) const {
2390314564Sdim    return Reader->getGlobalTypeID(*F, LocalID);
2391314564Sdim  }
2392314564Sdim
2393314564Sdim  /// \brief Read a type from the current position in the record.
2394314564Sdim  QualType readType() {
2395314564Sdim    return Reader->readType(*F, Record, Idx);
2396314564Sdim  }
2397314564Sdim
2398314564Sdim  /// \brief Reads a declaration ID from the given position in this record.
2399314564Sdim  ///
2400314564Sdim  /// \returns The declaration ID read from the record, adjusted to a global ID.
2401314564Sdim  serialization::DeclID readDeclID() {
2402314564Sdim    return Reader->ReadDeclID(*F, Record, Idx);
2403314564Sdim  }
2404314564Sdim
2405314564Sdim  /// \brief Reads a declaration from the given position in a record in the
2406314564Sdim  /// given module, advancing Idx.
2407314564Sdim  Decl *readDecl() {
2408314564Sdim    return Reader->ReadDecl(*F, Record, Idx);
2409314564Sdim  }
2410314564Sdim
2411314564Sdim  /// \brief Reads a declaration from the given position in the record,
2412314564Sdim  /// advancing Idx.
2413314564Sdim  ///
2414314564Sdim  /// \returns The declaration read from this location, casted to the given
2415314564Sdim  /// result type.
2416314564Sdim  template<typename T>
2417314564Sdim  T *readDeclAs() {
2418314564Sdim    return Reader->ReadDeclAs<T>(*F, Record, Idx);
2419314564Sdim  }
2420314564Sdim
2421314564Sdim  IdentifierInfo *getIdentifierInfo() {
2422314564Sdim    return Reader->GetIdentifierInfo(*F, Record, Idx);
2423314564Sdim  }
2424314564Sdim
2425314564Sdim  /// \brief Read a selector from the Record, advancing Idx.
2426314564Sdim  Selector readSelector() {
2427314564Sdim    return Reader->ReadSelector(*F, Record, Idx);
2428314564Sdim  }
2429314564Sdim
2430314564Sdim  /// \brief Read a declaration name, advancing Idx.
2431314564Sdim  DeclarationName readDeclarationName() {
2432314564Sdim    return Reader->ReadDeclarationName(*F, Record, Idx);
2433314564Sdim  }
2434314564Sdim  void readDeclarationNameLoc(DeclarationNameLoc &DNLoc, DeclarationName Name) {
2435314564Sdim    return Reader->ReadDeclarationNameLoc(*F, DNLoc, Name, Record, Idx);
2436314564Sdim  }
2437314564Sdim  void readDeclarationNameInfo(DeclarationNameInfo &NameInfo) {
2438314564Sdim    return Reader->ReadDeclarationNameInfo(*F, NameInfo, Record, Idx);
2439314564Sdim  }
2440314564Sdim
2441314564Sdim  void readQualifierInfo(QualifierInfo &Info) {
2442314564Sdim    return Reader->ReadQualifierInfo(*F, Info, Record, Idx);
2443314564Sdim  }
2444314564Sdim
2445314564Sdim  NestedNameSpecifier *readNestedNameSpecifier() {
2446314564Sdim    return Reader->ReadNestedNameSpecifier(*F, Record, Idx);
2447314564Sdim  }
2448314564Sdim
2449314564Sdim  NestedNameSpecifierLoc readNestedNameSpecifierLoc() {
2450314564Sdim    return Reader->ReadNestedNameSpecifierLoc(*F, Record, Idx);
2451314564Sdim  }
2452314564Sdim
2453314564Sdim  /// \brief Read a template name, advancing Idx.
2454314564Sdim  TemplateName readTemplateName() {
2455314564Sdim    return Reader->ReadTemplateName(*F, Record, Idx);
2456314564Sdim  }
2457314564Sdim
2458314564Sdim  /// \brief Read a template argument, advancing Idx.
2459314564Sdim  TemplateArgument readTemplateArgument(bool Canonicalize = false) {
2460314564Sdim    return Reader->ReadTemplateArgument(*F, Record, Idx, Canonicalize);
2461314564Sdim  }
2462314564Sdim
2463314564Sdim  /// \brief Read a template parameter list, advancing Idx.
2464314564Sdim  TemplateParameterList *readTemplateParameterList() {
2465314564Sdim    return Reader->ReadTemplateParameterList(*F, Record, Idx);
2466314564Sdim  }
2467314564Sdim
2468314564Sdim  /// \brief Read a template argument array, advancing Idx.
2469314564Sdim  void readTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
2470314564Sdim                                bool Canonicalize = false) {
2471314564Sdim    return Reader->ReadTemplateArgumentList(TemplArgs, *F, Record, Idx,
2472314564Sdim                                            Canonicalize);
2473314564Sdim  }
2474314564Sdim
2475314564Sdim  /// \brief Read a UnresolvedSet structure, advancing Idx.
2476314564Sdim  void readUnresolvedSet(LazyASTUnresolvedSet &Set) {
2477314564Sdim    return Reader->ReadUnresolvedSet(*F, Set, Record, Idx);
2478314564Sdim  }
2479314564Sdim
2480314564Sdim  /// \brief Read a C++ base specifier, advancing Idx.
2481314564Sdim  CXXBaseSpecifier readCXXBaseSpecifier() {
2482314564Sdim    return Reader->ReadCXXBaseSpecifier(*F, Record, Idx);
2483314564Sdim  }
2484314564Sdim
2485314564Sdim  /// \brief Read a CXXCtorInitializer array, advancing Idx.
2486314564Sdim  CXXCtorInitializer **readCXXCtorInitializers() {
2487314564Sdim    return Reader->ReadCXXCtorInitializers(*F, Record, Idx);
2488314564Sdim  }
2489314564Sdim
2490314564Sdim  CXXTemporary *readCXXTemporary() {
2491314564Sdim    return Reader->ReadCXXTemporary(*F, Record, Idx);
2492314564Sdim  }
2493314564Sdim
2494314564Sdim  /// \brief Read a source location, advancing Idx.
2495314564Sdim  SourceLocation readSourceLocation() {
2496314564Sdim    return Reader->ReadSourceLocation(*F, Record, Idx);
2497314564Sdim  }
2498314564Sdim
2499314564Sdim  /// \brief Read a source range, advancing Idx.
2500314564Sdim  SourceRange readSourceRange() {
2501314564Sdim    return Reader->ReadSourceRange(*F, Record, Idx);
2502314564Sdim  }
2503314564Sdim
2504314564Sdim  /// \brief Read an integral value, advancing Idx.
2505314564Sdim  llvm::APInt readAPInt() {
2506314564Sdim    return Reader->ReadAPInt(Record, Idx);
2507314564Sdim  }
2508314564Sdim
2509314564Sdim  /// \brief Read a signed integral value, advancing Idx.
2510314564Sdim  llvm::APSInt readAPSInt() {
2511314564Sdim    return Reader->ReadAPSInt(Record, Idx);
2512314564Sdim  }
2513314564Sdim
2514314564Sdim  /// \brief Read a floating-point value, advancing Idx.
2515314564Sdim  llvm::APFloat readAPFloat(const llvm::fltSemantics &Sem) {
2516314564Sdim    return Reader->ReadAPFloat(Record, Sem,Idx);
2517314564Sdim  }
2518314564Sdim
2519314564Sdim  /// \brief Read a string, advancing Idx.
2520314564Sdim  std::string readString() {
2521314564Sdim    return Reader->ReadString(Record, Idx);
2522314564Sdim  }
2523314564Sdim
2524314564Sdim  /// \brief Read a path, advancing Idx.
2525314564Sdim  std::string readPath() {
2526314564Sdim    return Reader->ReadPath(*F, Record, Idx);
2527314564Sdim  }
2528314564Sdim
2529314564Sdim  /// \brief Read a version tuple, advancing Idx.
2530314564Sdim  VersionTuple readVersionTuple() {
2531314564Sdim    return ASTReader::ReadVersionTuple(Record, Idx);
2532314564Sdim  }
2533314564Sdim
2534314564Sdim  /// \brief Reads attributes from the current stream position, advancing Idx.
2535314564Sdim  void readAttributes(AttrVec &Attrs) {
2536321369Sdim    return Reader->ReadAttributes(*this, Attrs);
2537314564Sdim  }
2538314564Sdim
2539314564Sdim  /// \brief Reads a token out of a record, advancing Idx.
2540314564Sdim  Token readToken() {
2541314564Sdim    return Reader->ReadToken(*F, Record, Idx);
2542314564Sdim  }
2543314564Sdim
2544314564Sdim  void recordSwitchCaseID(SwitchCase *SC, unsigned ID) {
2545314564Sdim    Reader->RecordSwitchCaseID(SC, ID);
2546314564Sdim  }
2547314564Sdim
2548314564Sdim  /// \brief Retrieve the switch-case statement with the given ID.
2549314564Sdim  SwitchCase *getSwitchCaseWithID(unsigned ID) {
2550314564Sdim    return Reader->getSwitchCaseWithID(ID);
2551314564Sdim  }
2552314564Sdim};
2553314564Sdim
2554212795Sdim/// \brief Helper class that saves the current stream position and
2555212795Sdim/// then restores it when destroyed.
2556212795Sdimstruct SavedStreamPosition {
2557212795Sdim  explicit SavedStreamPosition(llvm::BitstreamCursor &Cursor)
2558249423Sdim    : Cursor(Cursor), Offset(Cursor.GetCurrentBitNo()) { }
2559212795Sdim
2560212795Sdim  ~SavedStreamPosition() {
2561212795Sdim    Cursor.JumpToBit(Offset);
2562212795Sdim  }
2563212795Sdim
2564212795Sdimprivate:
2565212795Sdim  llvm::BitstreamCursor &Cursor;
2566212795Sdim  uint64_t Offset;
2567212795Sdim};
2568212795Sdim
2569212795Sdiminline void PCHValidator::Error(const char *Msg) {
2570212795Sdim  Reader.Error(Msg);
2571212795Sdim}
2572212795Sdim
2573212795Sdim} // end namespace clang
2574212795Sdim
2575212795Sdim#endif
2576