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