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