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