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