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     LLVM_PREFERRED_TYPE(bool)
725     unsigned IsWildcard : 1;
726 
727     /// String data.
728     StringRef String;
729   };
730 
731   /// The set of module imports and exports that still need to be
732   /// resolved.
733   SmallVector<UnresolvedModuleRef, 2> UnresolvedModuleRefs;
734 
735   /// A vector containing selectors that have already been loaded.
736   ///
737   /// This vector is indexed by the Selector ID (-1). NULL selector
738   /// entries indicate that the particular selector ID has not yet
739   /// been loaded.
740   SmallVector<Selector, 16> SelectorsLoaded;
741 
742   using GlobalSelectorMapType =
743       ContinuousRangeMap<serialization::SelectorID, ModuleFile *, 4>;
744 
745   /// Mapping from global selector IDs to the module in which the
746   /// global selector ID to produce a local ID.
747   GlobalSelectorMapType GlobalSelectorMap;
748 
749   /// The generation number of the last time we loaded data from the
750   /// global method pool for this selector.
751   llvm::DenseMap<Selector, unsigned> SelectorGeneration;
752 
753   /// Whether a selector is out of date. We mark a selector as out of date
754   /// if we load another module after the method pool entry was pulled in.
755   llvm::DenseMap<Selector, bool> SelectorOutOfDate;
756 
757   struct PendingMacroInfo {
758     ModuleFile *M;
759     /// Offset relative to ModuleFile::MacroOffsetsBase.
760     uint32_t MacroDirectivesOffset;
761 
PendingMacroInfoPendingMacroInfo762     PendingMacroInfo(ModuleFile *M, uint32_t MacroDirectivesOffset)
763         : M(M), MacroDirectivesOffset(MacroDirectivesOffset) {}
764   };
765 
766   using PendingMacroIDsMap =
767       llvm::MapVector<IdentifierInfo *, SmallVector<PendingMacroInfo, 2>>;
768 
769   /// Mapping from identifiers that have a macro history to the global
770   /// IDs have not yet been deserialized to the global IDs of those macros.
771   PendingMacroIDsMap PendingMacroIDs;
772 
773   using GlobalPreprocessedEntityMapType =
774       ContinuousRangeMap<unsigned, ModuleFile *, 4>;
775 
776   /// Mapping from global preprocessing entity IDs to the module in
777   /// which the preprocessed entity resides along with the offset that should be
778   /// added to the global preprocessing entity ID to produce a local ID.
779   GlobalPreprocessedEntityMapType GlobalPreprocessedEntityMap;
780 
781   using GlobalSkippedRangeMapType =
782       ContinuousRangeMap<unsigned, ModuleFile *, 4>;
783 
784   /// Mapping from global skipped range base IDs to the module in which
785   /// the skipped ranges reside.
786   GlobalSkippedRangeMapType GlobalSkippedRangeMap;
787 
788   /// \name CodeGen-relevant special data
789   /// Fields containing data that is relevant to CodeGen.
790   //@{
791 
792   /// The IDs of all declarations that fulfill the criteria of
793   /// "interesting" decls.
794   ///
795   /// This contains the data loaded from all EAGERLY_DESERIALIZED_DECLS blocks
796   /// in the chain. The referenced declarations are deserialized and passed to
797   /// the consumer eagerly.
798   SmallVector<serialization::DeclID, 16> EagerlyDeserializedDecls;
799 
800   /// The IDs of all tentative definitions stored in the chain.
801   ///
802   /// Sema keeps track of all tentative definitions in a TU because it has to
803   /// complete them and pass them on to CodeGen. Thus, tentative definitions in
804   /// the PCH chain must be eagerly deserialized.
805   SmallVector<serialization::DeclID, 16> TentativeDefinitions;
806 
807   /// The IDs of all CXXRecordDecls stored in the chain whose VTables are
808   /// used.
809   ///
810   /// CodeGen has to emit VTables for these records, so they have to be eagerly
811   /// deserialized.
812   SmallVector<serialization::DeclID, 64> VTableUses;
813 
814   /// A snapshot of the pending instantiations in the chain.
815   ///
816   /// This record tracks the instantiations that Sema has to perform at the
817   /// end of the TU. It consists of a pair of values for every pending
818   /// instantiation where the first value is the ID of the decl and the second
819   /// is the instantiation location.
820   SmallVector<serialization::DeclID, 64> PendingInstantiations;
821 
822   //@}
823 
824   /// \name DiagnosticsEngine-relevant special data
825   /// Fields containing data that is used for generating diagnostics
826   //@{
827 
828   /// A snapshot of Sema's unused file-scoped variable tracking, for
829   /// generating warnings.
830   SmallVector<serialization::DeclID, 16> UnusedFileScopedDecls;
831 
832   /// A list of all the delegating constructors we've seen, to diagnose
833   /// cycles.
834   SmallVector<serialization::DeclID, 4> DelegatingCtorDecls;
835 
836   /// Method selectors used in a @selector expression. Used for
837   /// implementation of -Wselector.
838   SmallVector<serialization::SelectorID, 64> ReferencedSelectorsData;
839 
840   /// A snapshot of Sema's weak undeclared identifier tracking, for
841   /// generating warnings.
842   SmallVector<serialization::IdentifierID, 64> WeakUndeclaredIdentifiers;
843 
844   /// The IDs of type aliases for ext_vectors that exist in the chain.
845   ///
846   /// Used by Sema for finding sugared names for ext_vectors in diagnostics.
847   SmallVector<serialization::DeclID, 4> ExtVectorDecls;
848 
849   //@}
850 
851   /// \name Sema-relevant special data
852   /// Fields containing data that is used for semantic analysis
853   //@{
854 
855   /// The IDs of all potentially unused typedef names in the chain.
856   ///
857   /// Sema tracks these to emit warnings.
858   SmallVector<serialization::DeclID, 16> UnusedLocalTypedefNameCandidates;
859 
860   /// Our current depth in #pragma cuda force_host_device begin/end
861   /// macros.
862   unsigned ForceCUDAHostDeviceDepth = 0;
863 
864   /// The IDs of the declarations Sema stores directly.
865   ///
866   /// Sema tracks a few important decls, such as namespace std, directly.
867   SmallVector<serialization::DeclID, 4> SemaDeclRefs;
868 
869   /// The IDs of the types ASTContext stores directly.
870   ///
871   /// The AST context tracks a few important types, such as va_list, directly.
872   SmallVector<serialization::TypeID, 16> SpecialTypes;
873 
874   /// The IDs of CUDA-specific declarations ASTContext stores directly.
875   ///
876   /// The AST context tracks a few important decls, currently cudaConfigureCall,
877   /// directly.
878   SmallVector<serialization::DeclID, 2> CUDASpecialDeclRefs;
879 
880   /// The floating point pragma option settings.
881   SmallVector<uint64_t, 1> FPPragmaOptions;
882 
883   /// The pragma clang optimize location (if the pragma state is "off").
884   SourceLocation OptimizeOffPragmaLocation;
885 
886   /// The PragmaMSStructKind pragma ms_struct state if set, or -1.
887   int PragmaMSStructState = -1;
888 
889   /// The PragmaMSPointersToMembersKind pragma pointers_to_members state.
890   int PragmaMSPointersToMembersState = -1;
891   SourceLocation PointersToMembersPragmaLocation;
892 
893   /// The pragma float_control state.
894   std::optional<FPOptionsOverride> FpPragmaCurrentValue;
895   SourceLocation FpPragmaCurrentLocation;
896   struct FpPragmaStackEntry {
897     FPOptionsOverride Value;
898     SourceLocation Location;
899     SourceLocation PushLocation;
900     StringRef SlotLabel;
901   };
902   llvm::SmallVector<FpPragmaStackEntry, 2> FpPragmaStack;
903   llvm::SmallVector<std::string, 2> FpPragmaStrings;
904 
905   /// The pragma align/pack state.
906   std::optional<Sema::AlignPackInfo> PragmaAlignPackCurrentValue;
907   SourceLocation PragmaAlignPackCurrentLocation;
908   struct PragmaAlignPackStackEntry {
909     Sema::AlignPackInfo Value;
910     SourceLocation Location;
911     SourceLocation PushLocation;
912     StringRef SlotLabel;
913   };
914   llvm::SmallVector<PragmaAlignPackStackEntry, 2> PragmaAlignPackStack;
915   llvm::SmallVector<std::string, 2> PragmaAlignPackStrings;
916 
917   /// The OpenCL extension settings.
918   OpenCLOptions OpenCLExtensions;
919 
920   /// Extensions required by an OpenCL type.
921   llvm::DenseMap<const Type *, std::set<std::string>> OpenCLTypeExtMap;
922 
923   /// Extensions required by an OpenCL declaration.
924   llvm::DenseMap<const Decl *, std::set<std::string>> OpenCLDeclExtMap;
925 
926   /// A list of the namespaces we've seen.
927   SmallVector<serialization::DeclID, 4> KnownNamespaces;
928 
929   /// A list of undefined decls with internal linkage followed by the
930   /// SourceLocation of a matching ODR-use.
931   SmallVector<serialization::DeclID, 8> UndefinedButUsed;
932 
933   /// Delete expressions to analyze at the end of translation unit.
934   SmallVector<uint64_t, 8> DelayedDeleteExprs;
935 
936   // A list of late parsed template function data with their module files.
937   SmallVector<std::pair<ModuleFile *, SmallVector<uint64_t, 1>>, 4>
938       LateParsedTemplates;
939 
940   /// The IDs of all decls to be checked for deferred diags.
941   ///
942   /// Sema tracks these to emit deferred diags.
943   llvm::SmallSetVector<serialization::DeclID, 4> DeclsToCheckForDeferredDiags;
944 
945 private:
946   struct ImportedSubmodule {
947     serialization::SubmoduleID ID;
948     SourceLocation ImportLoc;
949 
ImportedSubmoduleImportedSubmodule950     ImportedSubmodule(serialization::SubmoduleID ID, SourceLocation ImportLoc)
951         : ID(ID), ImportLoc(ImportLoc) {}
952   };
953 
954   /// A list of modules that were imported by precompiled headers or
955   /// any other non-module AST file and have not yet been made visible. If a
956   /// module is made visible in the ASTReader, it will be transfered to
957   /// \c PendingImportedModulesSema.
958   SmallVector<ImportedSubmodule, 2> PendingImportedModules;
959 
960   /// A list of modules that were imported by precompiled headers or
961   /// any other non-module AST file and have not yet been made visible for Sema.
962   SmallVector<ImportedSubmodule, 2> PendingImportedModulesSema;
963   //@}
964 
965   /// The system include root to be used when loading the
966   /// precompiled header.
967   std::string isysroot;
968 
969   /// Whether to disable the normal validation performed on precompiled
970   /// headers and module files when they are loaded.
971   DisableValidationForModuleKind DisableValidationKind;
972 
973   /// Whether to accept an AST file with compiler errors.
974   bool AllowASTWithCompilerErrors;
975 
976   /// Whether to accept an AST file that has a different configuration
977   /// from the current compiler instance.
978   bool AllowConfigurationMismatch;
979 
980   /// Whether validate system input files.
981   bool ValidateSystemInputs;
982 
983   /// Whether validate headers and module maps using hash based on contents.
984   bool ValidateASTInputFilesContent;
985 
986   /// Whether we are allowed to use the global module index.
987   bool UseGlobalIndex;
988 
989   /// Whether we have tried loading the global module index yet.
990   bool TriedLoadingGlobalIndex = false;
991 
992   ///Whether we are currently processing update records.
993   bool ProcessingUpdateRecords = false;
994 
995   using SwitchCaseMapTy = llvm::DenseMap<unsigned, SwitchCase *>;
996 
997   /// Mapping from switch-case IDs in the chain to switch-case statements
998   ///
999   /// Statements usually don't have IDs, but switch cases need them, so that the
1000   /// switch statement can refer to them.
1001   SwitchCaseMapTy SwitchCaseStmts;
1002 
1003   SwitchCaseMapTy *CurrSwitchCaseStmts;
1004 
1005   /// The number of source location entries de-serialized from
1006   /// the PCH file.
1007   unsigned NumSLocEntriesRead = 0;
1008 
1009   /// The number of source location entries in the chain.
1010   unsigned TotalNumSLocEntries = 0;
1011 
1012   /// The number of statements (and expressions) de-serialized
1013   /// from the chain.
1014   unsigned NumStatementsRead = 0;
1015 
1016   /// The total number of statements (and expressions) stored
1017   /// in the chain.
1018   unsigned TotalNumStatements = 0;
1019 
1020   /// The number of macros de-serialized from the chain.
1021   unsigned NumMacrosRead = 0;
1022 
1023   /// The total number of macros stored in the chain.
1024   unsigned TotalNumMacros = 0;
1025 
1026   /// The number of lookups into identifier tables.
1027   unsigned NumIdentifierLookups = 0;
1028 
1029   /// The number of lookups into identifier tables that succeed.
1030   unsigned NumIdentifierLookupHits = 0;
1031 
1032   /// The number of selectors that have been read.
1033   unsigned NumSelectorsRead = 0;
1034 
1035   /// The number of method pool entries that have been read.
1036   unsigned NumMethodPoolEntriesRead = 0;
1037 
1038   /// The number of times we have looked up a selector in the method
1039   /// pool.
1040   unsigned NumMethodPoolLookups = 0;
1041 
1042   /// The number of times we have looked up a selector in the method
1043   /// pool and found something.
1044   unsigned NumMethodPoolHits = 0;
1045 
1046   /// The number of times we have looked up a selector in the method
1047   /// pool within a specific module.
1048   unsigned NumMethodPoolTableLookups = 0;
1049 
1050   /// The number of times we have looked up a selector in the method
1051   /// pool within a specific module and found something.
1052   unsigned NumMethodPoolTableHits = 0;
1053 
1054   /// The total number of method pool entries in the selector table.
1055   unsigned TotalNumMethodPoolEntries = 0;
1056 
1057   /// Number of lexical decl contexts read/total.
1058   unsigned NumLexicalDeclContextsRead = 0, TotalLexicalDeclContexts = 0;
1059 
1060   /// Number of visible decl contexts read/total.
1061   unsigned NumVisibleDeclContextsRead = 0, TotalVisibleDeclContexts = 0;
1062 
1063   /// Total size of modules, in bits, currently loaded
1064   uint64_t TotalModulesSizeInBits = 0;
1065 
1066   /// Number of Decl/types that are currently deserializing.
1067   unsigned NumCurrentElementsDeserializing = 0;
1068 
1069   /// Set true while we are in the process of passing deserialized
1070   /// "interesting" decls to consumer inside FinishedDeserializing().
1071   /// This is used as a guard to avoid recursively repeating the process of
1072   /// passing decls to consumer.
1073   bool PassingDeclsToConsumer = false;
1074 
1075   /// The set of identifiers that were read while the AST reader was
1076   /// (recursively) loading declarations.
1077   ///
1078   /// The declarations on the identifier chain for these identifiers will be
1079   /// loaded once the recursive loading has completed.
1080   llvm::MapVector<IdentifierInfo *, SmallVector<uint32_t, 4>>
1081     PendingIdentifierInfos;
1082 
1083   /// The set of lookup results that we have faked in order to support
1084   /// merging of partially deserialized decls but that we have not yet removed.
1085   llvm::SmallMapVector<IdentifierInfo *, SmallVector<NamedDecl*, 2>, 16>
1086     PendingFakeLookupResults;
1087 
1088   /// The generation number of each identifier, which keeps track of
1089   /// the last time we loaded information about this identifier.
1090   llvm::DenseMap<IdentifierInfo *, unsigned> IdentifierGeneration;
1091 
1092   class InterestingDecl {
1093     Decl *D;
1094     bool DeclHasPendingBody;
1095 
1096   public:
InterestingDecl(Decl * D,bool HasBody)1097     InterestingDecl(Decl *D, bool HasBody)
1098         : D(D), DeclHasPendingBody(HasBody) {}
1099 
getDecl()1100     Decl *getDecl() { return D; }
1101 
1102     /// Whether the declaration has a pending body.
hasPendingBody()1103     bool hasPendingBody() { return DeclHasPendingBody; }
1104   };
1105 
1106   /// Contains declarations and definitions that could be
1107   /// "interesting" to the ASTConsumer, when we get that AST consumer.
1108   ///
1109   /// "Interesting" declarations are those that have data that may
1110   /// need to be emitted, such as inline function definitions or
1111   /// Objective-C protocols.
1112   std::deque<InterestingDecl> PotentiallyInterestingDecls;
1113 
1114   /// The list of deduced function types that we have not yet read, because
1115   /// they might contain a deduced return type that refers to a local type
1116   /// declared within the function.
1117   SmallVector<std::pair<FunctionDecl *, serialization::TypeID>, 16>
1118       PendingDeducedFunctionTypes;
1119 
1120   /// The list of deduced variable types that we have not yet read, because
1121   /// they might contain a deduced type that refers to a local type declared
1122   /// within the variable.
1123   SmallVector<std::pair<VarDecl *, serialization::TypeID>, 16>
1124       PendingDeducedVarTypes;
1125 
1126   /// The list of redeclaration chains that still need to be
1127   /// reconstructed, and the local offset to the corresponding list
1128   /// of redeclarations.
1129   SmallVector<std::pair<Decl *, uint64_t>, 16> PendingDeclChains;
1130 
1131   /// The list of canonical declarations whose redeclaration chains
1132   /// need to be marked as incomplete once we're done deserializing things.
1133   SmallVector<Decl *, 16> PendingIncompleteDeclChains;
1134 
1135   /// The Decl IDs for the Sema/Lexical DeclContext of a Decl that has
1136   /// been loaded but its DeclContext was not set yet.
1137   struct PendingDeclContextInfo {
1138     Decl *D;
1139     serialization::GlobalDeclID SemaDC;
1140     serialization::GlobalDeclID LexicalDC;
1141   };
1142 
1143   /// The set of Decls that have been loaded but their DeclContexts are
1144   /// not set yet.
1145   ///
1146   /// The DeclContexts for these Decls will be set once recursive loading has
1147   /// been completed.
1148   std::deque<PendingDeclContextInfo> PendingDeclContextInfos;
1149 
1150   template <typename DeclTy>
1151   using DuplicateObjCDecls = std::pair<DeclTy *, DeclTy *>;
1152 
1153   /// When resolving duplicate ivars from Objective-C extensions we don't error
1154   /// out immediately but check if can merge identical extensions. Not checking
1155   /// extensions for equality immediately because ivar deserialization isn't
1156   /// over yet at that point.
1157   llvm::SmallMapVector<DuplicateObjCDecls<ObjCCategoryDecl>,
1158                        llvm::SmallVector<DuplicateObjCDecls<ObjCIvarDecl>, 4>,
1159                        2>
1160       PendingObjCExtensionIvarRedeclarations;
1161 
1162   /// Members that have been added to classes, for which the class has not yet
1163   /// been notified. CXXRecordDecl::addedMember will be called for each of
1164   /// these once recursive deserialization is complete.
1165   SmallVector<std::pair<CXXRecordDecl*, Decl*>, 4> PendingAddedClassMembers;
1166 
1167   /// The set of NamedDecls that have been loaded, but are members of a
1168   /// context that has been merged into another context where the corresponding
1169   /// declaration is either missing or has not yet been loaded.
1170   ///
1171   /// We will check whether the corresponding declaration is in fact missing
1172   /// once recursing loading has been completed.
1173   llvm::SmallVector<NamedDecl *, 16> PendingOdrMergeChecks;
1174 
1175   using DataPointers =
1176       std::pair<CXXRecordDecl *, struct CXXRecordDecl::DefinitionData *>;
1177   using ObjCInterfaceDataPointers =
1178       std::pair<ObjCInterfaceDecl *,
1179                 struct ObjCInterfaceDecl::DefinitionData *>;
1180   using ObjCProtocolDataPointers =
1181       std::pair<ObjCProtocolDecl *, struct ObjCProtocolDecl::DefinitionData *>;
1182 
1183   /// Record definitions in which we found an ODR violation.
1184   llvm::SmallDenseMap<CXXRecordDecl *, llvm::SmallVector<DataPointers, 2>, 2>
1185       PendingOdrMergeFailures;
1186 
1187   /// C/ObjC record definitions in which we found an ODR violation.
1188   llvm::SmallDenseMap<RecordDecl *, llvm::SmallVector<RecordDecl *, 2>, 2>
1189       PendingRecordOdrMergeFailures;
1190 
1191   /// Function definitions in which we found an ODR violation.
1192   llvm::SmallDenseMap<FunctionDecl *, llvm::SmallVector<FunctionDecl *, 2>, 2>
1193       PendingFunctionOdrMergeFailures;
1194 
1195   /// Enum definitions in which we found an ODR violation.
1196   llvm::SmallDenseMap<EnumDecl *, llvm::SmallVector<EnumDecl *, 2>, 2>
1197       PendingEnumOdrMergeFailures;
1198 
1199   /// ObjCInterfaceDecl in which we found an ODR violation.
1200   llvm::SmallDenseMap<ObjCInterfaceDecl *,
1201                       llvm::SmallVector<ObjCInterfaceDataPointers, 2>, 2>
1202       PendingObjCInterfaceOdrMergeFailures;
1203 
1204   /// ObjCProtocolDecl in which we found an ODR violation.
1205   llvm::SmallDenseMap<ObjCProtocolDecl *,
1206                       llvm::SmallVector<ObjCProtocolDataPointers, 2>, 2>
1207       PendingObjCProtocolOdrMergeFailures;
1208 
1209   /// DeclContexts in which we have diagnosed an ODR violation.
1210   llvm::SmallPtrSet<DeclContext*, 2> DiagnosedOdrMergeFailures;
1211 
1212   /// The set of Objective-C categories that have been deserialized
1213   /// since the last time the declaration chains were linked.
1214   llvm::SmallPtrSet<ObjCCategoryDecl *, 16> CategoriesDeserialized;
1215 
1216   /// The set of Objective-C class definitions that have already been
1217   /// loaded, for which we will need to check for categories whenever a new
1218   /// module is loaded.
1219   SmallVector<ObjCInterfaceDecl *, 16> ObjCClassesLoaded;
1220 
1221   using KeyDeclsMap =
1222       llvm::DenseMap<Decl *, SmallVector<serialization::DeclID, 2>>;
1223 
1224   /// A mapping from canonical declarations to the set of global
1225   /// declaration IDs for key declaration that have been merged with that
1226   /// canonical declaration. A key declaration is a formerly-canonical
1227   /// declaration whose module did not import any other key declaration for that
1228   /// entity. These are the IDs that we use as keys when finding redecl chains.
1229   KeyDeclsMap KeyDecls;
1230 
1231   /// A mapping from DeclContexts to the semantic DeclContext that we
1232   /// are treating as the definition of the entity. This is used, for instance,
1233   /// when merging implicit instantiations of class templates across modules.
1234   llvm::DenseMap<DeclContext *, DeclContext *> MergedDeclContexts;
1235 
1236   /// A mapping from canonical declarations of enums to their canonical
1237   /// definitions. Only populated when using modules in C++.
1238   llvm::DenseMap<EnumDecl *, EnumDecl *> EnumDefinitions;
1239 
1240   /// A mapping from canonical declarations of records to their canonical
1241   /// definitions. Doesn't cover CXXRecordDecl.
1242   llvm::DenseMap<RecordDecl *, RecordDecl *> RecordDefinitions;
1243 
1244   /// When reading a Stmt tree, Stmt operands are placed in this stack.
1245   SmallVector<Stmt *, 16> StmtStack;
1246 
1247   /// What kind of records we are reading.
1248   enum ReadingKind {
1249     Read_None, Read_Decl, Read_Type, Read_Stmt
1250   };
1251 
1252   /// What kind of records we are reading.
1253   ReadingKind ReadingKind = Read_None;
1254 
1255   /// RAII object to change the reading kind.
1256   class ReadingKindTracker {
1257     ASTReader &Reader;
1258     enum ReadingKind PrevKind;
1259 
1260   public:
ReadingKindTracker(enum ReadingKind newKind,ASTReader & reader)1261     ReadingKindTracker(enum ReadingKind newKind, ASTReader &reader)
1262         : Reader(reader), PrevKind(Reader.ReadingKind) {
1263       Reader.ReadingKind = newKind;
1264     }
1265 
1266     ReadingKindTracker(const ReadingKindTracker &) = delete;
1267     ReadingKindTracker &operator=(const ReadingKindTracker &) = delete;
~ReadingKindTracker()1268     ~ReadingKindTracker() { Reader.ReadingKind = PrevKind; }
1269   };
1270 
1271   /// RAII object to mark the start of processing updates.
1272   class ProcessingUpdatesRAIIObj {
1273     ASTReader &Reader;
1274     bool PrevState;
1275 
1276   public:
ProcessingUpdatesRAIIObj(ASTReader & reader)1277     ProcessingUpdatesRAIIObj(ASTReader &reader)
1278         : Reader(reader), PrevState(Reader.ProcessingUpdateRecords) {
1279       Reader.ProcessingUpdateRecords = true;
1280     }
1281 
1282     ProcessingUpdatesRAIIObj(const ProcessingUpdatesRAIIObj &) = delete;
1283     ProcessingUpdatesRAIIObj &
1284     operator=(const ProcessingUpdatesRAIIObj &) = delete;
~ProcessingUpdatesRAIIObj()1285     ~ProcessingUpdatesRAIIObj() { Reader.ProcessingUpdateRecords = PrevState; }
1286   };
1287 
1288   /// Suggested contents of the predefines buffer, after this
1289   /// PCH file has been processed.
1290   ///
1291   /// In most cases, this string will be empty, because the predefines
1292   /// buffer computed to build the PCH file will be identical to the
1293   /// predefines buffer computed from the command line. However, when
1294   /// there are differences that the PCH reader can work around, this
1295   /// predefines buffer may contain additional definitions.
1296   std::string SuggestedPredefines;
1297 
1298   llvm::DenseMap<const Decl *, bool> DefinitionSource;
1299 
1300   bool shouldDisableValidationForFile(const serialization::ModuleFile &M) const;
1301 
1302   /// Reads a statement from the specified cursor.
1303   Stmt *ReadStmtFromStream(ModuleFile &F);
1304 
1305   /// Retrieve the stored information about an input file.
1306   serialization::InputFileInfo getInputFileInfo(ModuleFile &F, unsigned ID);
1307 
1308   /// Retrieve the file entry and 'overridden' bit for an input
1309   /// file in the given module file.
1310   serialization::InputFile getInputFile(ModuleFile &F, unsigned ID,
1311                                         bool Complain = true);
1312 
1313 public:
1314   void ResolveImportedPath(ModuleFile &M, std::string &Filename);
1315   static void ResolveImportedPath(std::string &Filename, StringRef Prefix);
1316 
1317   /// Returns the first key declaration for the given declaration. This
1318   /// is one that is formerly-canonical (or still canonical) and whose module
1319   /// did not import any other key declaration of the entity.
getKeyDeclaration(Decl * D)1320   Decl *getKeyDeclaration(Decl *D) {
1321     D = D->getCanonicalDecl();
1322     if (D->isFromASTFile())
1323       return D;
1324 
1325     auto I = KeyDecls.find(D);
1326     if (I == KeyDecls.end() || I->second.empty())
1327       return D;
1328     return GetExistingDecl(I->second[0]);
1329   }
getKeyDeclaration(const Decl * D)1330   const Decl *getKeyDeclaration(const Decl *D) {
1331     return getKeyDeclaration(const_cast<Decl*>(D));
1332   }
1333 
1334   /// Run a callback on each imported key declaration of \p D.
1335   template <typename Fn>
forEachImportedKeyDecl(const Decl * D,Fn Visit)1336   void forEachImportedKeyDecl(const Decl *D, Fn Visit) {
1337     D = D->getCanonicalDecl();
1338     if (D->isFromASTFile())
1339       Visit(D);
1340 
1341     auto It = KeyDecls.find(const_cast<Decl*>(D));
1342     if (It != KeyDecls.end())
1343       for (auto ID : It->second)
1344         Visit(GetExistingDecl(ID));
1345   }
1346 
1347   /// Get the loaded lookup tables for \p Primary, if any.
1348   const serialization::reader::DeclContextLookupTable *
1349   getLoadedLookupTables(DeclContext *Primary) const;
1350 
1351 private:
1352   struct ImportedModule {
1353     ModuleFile *Mod;
1354     ModuleFile *ImportedBy;
1355     SourceLocation ImportLoc;
1356 
ImportedModuleImportedModule1357     ImportedModule(ModuleFile *Mod,
1358                    ModuleFile *ImportedBy,
1359                    SourceLocation ImportLoc)
1360         : Mod(Mod), ImportedBy(ImportedBy), ImportLoc(ImportLoc) {}
1361   };
1362 
1363   ASTReadResult ReadASTCore(StringRef FileName, ModuleKind Type,
1364                             SourceLocation ImportLoc, ModuleFile *ImportedBy,
1365                             SmallVectorImpl<ImportedModule> &Loaded,
1366                             off_t ExpectedSize, time_t ExpectedModTime,
1367                             ASTFileSignature ExpectedSignature,
1368                             unsigned ClientLoadCapabilities);
1369   ASTReadResult ReadControlBlock(ModuleFile &F,
1370                                  SmallVectorImpl<ImportedModule> &Loaded,
1371                                  const ModuleFile *ImportedBy,
1372                                  unsigned ClientLoadCapabilities);
1373   static ASTReadResult ReadOptionsBlock(
1374       llvm::BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
1375       bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
1376       std::string &SuggestedPredefines);
1377 
1378   /// Read the unhashed control block.
1379   ///
1380   /// This has no effect on \c F.Stream, instead creating a fresh cursor from
1381   /// \c F.Data and reading ahead.
1382   ASTReadResult readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
1383                                          unsigned ClientLoadCapabilities);
1384 
1385   static ASTReadResult
1386   readUnhashedControlBlockImpl(ModuleFile *F, llvm::StringRef StreamData,
1387                                unsigned ClientLoadCapabilities,
1388                                bool AllowCompatibleConfigurationMismatch,
1389                                ASTReaderListener *Listener,
1390                                bool ValidateDiagnosticOptions);
1391 
1392   llvm::Error ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities);
1393   llvm::Error ReadExtensionBlock(ModuleFile &F);
1394   void ReadModuleOffsetMap(ModuleFile &F) const;
1395   void ParseLineTable(ModuleFile &F, const RecordData &Record);
1396   llvm::Error ReadSourceManagerBlock(ModuleFile &F);
1397   SourceLocation getImportLocation(ModuleFile *F);
1398   ASTReadResult ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
1399                                        const ModuleFile *ImportedBy,
1400                                        unsigned ClientLoadCapabilities);
1401   llvm::Error ReadSubmoduleBlock(ModuleFile &F,
1402                                  unsigned ClientLoadCapabilities);
1403   static bool ParseLanguageOptions(const RecordData &Record, bool Complain,
1404                                    ASTReaderListener &Listener,
1405                                    bool AllowCompatibleDifferences);
1406   static bool ParseTargetOptions(const RecordData &Record, bool Complain,
1407                                  ASTReaderListener &Listener,
1408                                  bool AllowCompatibleDifferences);
1409   static bool ParseDiagnosticOptions(const RecordData &Record, bool Complain,
1410                                      ASTReaderListener &Listener);
1411   static bool ParseFileSystemOptions(const RecordData &Record, bool Complain,
1412                                      ASTReaderListener &Listener);
1413   static bool ParseHeaderSearchOptions(const RecordData &Record, bool Complain,
1414                                        ASTReaderListener &Listener);
1415   static bool ParseHeaderSearchPaths(const RecordData &Record, bool Complain,
1416                                      ASTReaderListener &Listener);
1417   static bool ParsePreprocessorOptions(const RecordData &Record, bool Complain,
1418                                        ASTReaderListener &Listener,
1419                                        std::string &SuggestedPredefines);
1420 
1421   struct RecordLocation {
1422     ModuleFile *F;
1423     uint64_t Offset;
1424 
RecordLocationRecordLocation1425     RecordLocation(ModuleFile *M, uint64_t O) : F(M), Offset(O) {}
1426   };
1427 
1428   QualType readTypeRecord(unsigned Index);
1429   RecordLocation TypeCursorForIndex(unsigned Index);
1430   void LoadedDecl(unsigned Index, Decl *D);
1431   Decl *ReadDeclRecord(serialization::DeclID ID);
1432   void markIncompleteDeclChain(Decl *D);
1433 
1434   /// Returns the most recent declaration of a declaration (which must be
1435   /// of a redeclarable kind) that is either local or has already been loaded
1436   /// merged into its redecl chain.
1437   Decl *getMostRecentExistingDecl(Decl *D);
1438 
1439   RecordLocation DeclCursorForID(serialization::DeclID ID,
1440                                  SourceLocation &Location);
1441   void loadDeclUpdateRecords(PendingUpdateRecord &Record);
1442   void loadPendingDeclChain(Decl *D, uint64_t LocalOffset);
1443   void loadObjCCategories(serialization::GlobalDeclID ID, ObjCInterfaceDecl *D,
1444                           unsigned PreviousGeneration = 0);
1445 
1446   RecordLocation getLocalBitOffset(uint64_t GlobalOffset);
1447   uint64_t getGlobalBitOffset(ModuleFile &M, uint64_t LocalOffset);
1448 
1449   /// Returns the first preprocessed entity ID that begins or ends after
1450   /// \arg Loc.
1451   serialization::PreprocessedEntityID
1452   findPreprocessedEntity(SourceLocation Loc, bool EndsAfter) const;
1453 
1454   /// Find the next module that contains entities and return the ID
1455   /// of the first entry.
1456   ///
1457   /// \param SLocMapI points at a chunk of a module that contains no
1458   /// preprocessed entities or the entities it contains are not the
1459   /// ones we are looking for.
1460   serialization::PreprocessedEntityID
1461     findNextPreprocessedEntity(
1462                         GlobalSLocOffsetMapType::const_iterator SLocMapI) const;
1463 
1464   /// Returns (ModuleFile, Local index) pair for \p GlobalIndex of a
1465   /// preprocessed entity.
1466   std::pair<ModuleFile *, unsigned>
1467     getModulePreprocessedEntity(unsigned GlobalIndex);
1468 
1469   /// Returns (begin, end) pair for the preprocessed entities of a
1470   /// particular module.
1471   llvm::iterator_range<PreprocessingRecord::iterator>
1472   getModulePreprocessedEntities(ModuleFile &Mod) const;
1473 
1474   bool canRecoverFromOutOfDate(StringRef ModuleFileName,
1475                                unsigned ClientLoadCapabilities);
1476 
1477 public:
1478   class ModuleDeclIterator
1479       : public llvm::iterator_adaptor_base<
1480             ModuleDeclIterator, const serialization::LocalDeclID *,
1481             std::random_access_iterator_tag, const Decl *, ptrdiff_t,
1482             const Decl *, const Decl *> {
1483     ASTReader *Reader = nullptr;
1484     ModuleFile *Mod = nullptr;
1485 
1486   public:
ModuleDeclIterator()1487     ModuleDeclIterator() : iterator_adaptor_base(nullptr) {}
1488 
ModuleDeclIterator(ASTReader * Reader,ModuleFile * Mod,const serialization::LocalDeclID * Pos)1489     ModuleDeclIterator(ASTReader *Reader, ModuleFile *Mod,
1490                        const serialization::LocalDeclID *Pos)
1491         : iterator_adaptor_base(Pos), Reader(Reader), Mod(Mod) {}
1492 
1493     value_type operator*() const {
1494       return Reader->GetDecl(Reader->getGlobalDeclID(*Mod, *I));
1495     }
1496 
1497     value_type operator->() const { return **this; }
1498 
1499     bool operator==(const ModuleDeclIterator &RHS) const {
1500       assert(Reader == RHS.Reader && Mod == RHS.Mod);
1501       return I == RHS.I;
1502     }
1503   };
1504 
1505   llvm::iterator_range<ModuleDeclIterator>
1506   getModuleFileLevelDecls(ModuleFile &Mod);
1507 
1508 private:
1509   void PassInterestingDeclsToConsumer();
1510   void PassInterestingDeclToConsumer(Decl *D);
1511 
1512   void finishPendingActions();
1513   void diagnoseOdrViolations();
1514 
1515   void pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name);
1516 
addPendingDeclContextInfo(Decl * D,serialization::GlobalDeclID SemaDC,serialization::GlobalDeclID LexicalDC)1517   void addPendingDeclContextInfo(Decl *D,
1518                                  serialization::GlobalDeclID SemaDC,
1519                                  serialization::GlobalDeclID LexicalDC) {
1520     assert(D);
1521     PendingDeclContextInfo Info = { D, SemaDC, LexicalDC };
1522     PendingDeclContextInfos.push_back(Info);
1523   }
1524 
1525   /// Produce an error diagnostic and return true.
1526   ///
1527   /// This routine should only be used for fatal errors that have to
1528   /// do with non-routine failures (e.g., corrupted AST file).
1529   void Error(StringRef Msg) const;
1530   void Error(unsigned DiagID, StringRef Arg1 = StringRef(),
1531              StringRef Arg2 = StringRef(), StringRef Arg3 = StringRef()) const;
1532   void Error(llvm::Error &&Err) const;
1533 
1534 public:
1535   /// Load the AST file and validate its contents against the given
1536   /// Preprocessor.
1537   ///
1538   /// \param PP the preprocessor associated with the context in which this
1539   /// precompiled header will be loaded.
1540   ///
1541   /// \param Context the AST context that this precompiled header will be
1542   /// loaded into, if any.
1543   ///
1544   /// \param PCHContainerRdr the PCHContainerOperations to use for loading and
1545   /// creating modules.
1546   ///
1547   /// \param Extensions the list of module file extensions that can be loaded
1548   /// from the AST files.
1549   ///
1550   /// \param isysroot If non-NULL, the system include path specified by the
1551   /// user. This is only used with relocatable PCH files. If non-NULL,
1552   /// a relocatable PCH file will use the default path "/".
1553   ///
1554   /// \param DisableValidationKind If set, the AST reader will suppress most
1555   /// of its regular consistency checking, allowing the use of precompiled
1556   /// headers and module files that cannot be determined to be compatible.
1557   ///
1558   /// \param AllowASTWithCompilerErrors If true, the AST reader will accept an
1559   /// AST file the was created out of an AST with compiler errors,
1560   /// otherwise it will reject it.
1561   ///
1562   /// \param AllowConfigurationMismatch If true, the AST reader will not check
1563   /// for configuration differences between the AST file and the invocation.
1564   ///
1565   /// \param ValidateSystemInputs If true, the AST reader will validate
1566   /// system input files in addition to user input files. This is only
1567   /// meaningful if \p DisableValidation is false.
1568   ///
1569   /// \param UseGlobalIndex If true, the AST reader will try to load and use
1570   /// the global module index.
1571   ///
1572   /// \param ReadTimer If non-null, a timer used to track the time spent
1573   /// deserializing.
1574   ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
1575             ASTContext *Context, const PCHContainerReader &PCHContainerRdr,
1576             ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
1577             StringRef isysroot = "",
1578             DisableValidationForModuleKind DisableValidationKind =
1579                 DisableValidationForModuleKind::None,
1580             bool AllowASTWithCompilerErrors = false,
1581             bool AllowConfigurationMismatch = false,
1582             bool ValidateSystemInputs = false,
1583             bool ValidateASTInputFilesContent = false,
1584             bool UseGlobalIndex = true,
1585             std::unique_ptr<llvm::Timer> ReadTimer = {});
1586   ASTReader(const ASTReader &) = delete;
1587   ASTReader &operator=(const ASTReader &) = delete;
1588   ~ASTReader() override;
1589 
getSourceManager()1590   SourceManager &getSourceManager() const { return SourceMgr; }
getFileManager()1591   FileManager &getFileManager() const { return FileMgr; }
getDiags()1592   DiagnosticsEngine &getDiags() const { return Diags; }
1593 
1594   /// Flags that indicate what kind of AST loading failures the client
1595   /// of the AST reader can directly handle.
1596   ///
1597   /// When a client states that it can handle a particular kind of failure,
1598   /// the AST reader will not emit errors when producing that kind of failure.
1599   enum LoadFailureCapabilities {
1600     /// The client can't handle any AST loading failures.
1601     ARR_None = 0,
1602 
1603     /// The client can handle an AST file that cannot load because it
1604     /// is missing.
1605     ARR_Missing = 0x1,
1606 
1607     /// The client can handle an AST file that cannot load because it
1608     /// is out-of-date relative to its input files.
1609     ARR_OutOfDate = 0x2,
1610 
1611     /// The client can handle an AST file that cannot load because it
1612     /// was built with a different version of Clang.
1613     ARR_VersionMismatch = 0x4,
1614 
1615     /// The client can handle an AST file that cannot load because it's
1616     /// compiled configuration doesn't match that of the context it was
1617     /// loaded into.
1618     ARR_ConfigurationMismatch = 0x8,
1619 
1620     /// If a module file is marked with errors treat it as out-of-date so the
1621     /// caller can rebuild it.
1622     ARR_TreatModuleWithErrorsAsOutOfDate = 0x10
1623   };
1624 
1625   /// Load the AST file designated by the given file name.
1626   ///
1627   /// \param FileName The name of the AST file to load.
1628   ///
1629   /// \param Type The kind of AST being loaded, e.g., PCH, module, main file,
1630   /// or preamble.
1631   ///
1632   /// \param ImportLoc the location where the module file will be considered as
1633   /// imported from. For non-module AST types it should be invalid.
1634   ///
1635   /// \param ClientLoadCapabilities The set of client load-failure
1636   /// capabilities, represented as a bitset of the enumerators of
1637   /// LoadFailureCapabilities.
1638   ///
1639   /// \param LoadedModuleFile The optional out-parameter refers to the new
1640   /// loaded modules. In case the module specified by FileName is already
1641   /// loaded, the module file pointer referred by NewLoadedModuleFile wouldn't
1642   /// change. Otherwise if the AST file get loaded successfully,
1643   /// NewLoadedModuleFile would refer to the address of the new loaded top level
1644   /// module. The state of NewLoadedModuleFile is unspecified if the AST file
1645   /// isn't loaded successfully.
1646   ASTReadResult ReadAST(StringRef FileName, ModuleKind Type,
1647                         SourceLocation ImportLoc,
1648                         unsigned ClientLoadCapabilities,
1649                         ModuleFile **NewLoadedModuleFile = nullptr);
1650 
1651   /// Make the entities in the given module and any of its (non-explicit)
1652   /// submodules visible to name lookup.
1653   ///
1654   /// \param Mod The module whose names should be made visible.
1655   ///
1656   /// \param NameVisibility The level of visibility to give the names in the
1657   /// module.  Visibility can only be increased over time.
1658   ///
1659   /// \param ImportLoc The location at which the import occurs.
1660   void makeModuleVisible(Module *Mod,
1661                          Module::NameVisibilityKind NameVisibility,
1662                          SourceLocation ImportLoc);
1663 
1664   /// Make the names within this set of hidden names visible.
1665   void makeNamesVisible(const HiddenNames &Names, Module *Owner);
1666 
1667   /// Note that MergedDef is a redefinition of the canonical definition
1668   /// Def, so Def should be visible whenever MergedDef is.
1669   void mergeDefinitionVisibility(NamedDecl *Def, NamedDecl *MergedDef);
1670 
1671   /// Take the AST callbacks listener.
takeListener()1672   std::unique_ptr<ASTReaderListener> takeListener() {
1673     return std::move(Listener);
1674   }
1675 
1676   /// Set the AST callbacks listener.
setListener(std::unique_ptr<ASTReaderListener> Listener)1677   void setListener(std::unique_ptr<ASTReaderListener> Listener) {
1678     this->Listener = std::move(Listener);
1679   }
1680 
1681   /// Add an AST callback listener.
1682   ///
1683   /// Takes ownership of \p L.
addListener(std::unique_ptr<ASTReaderListener> L)1684   void addListener(std::unique_ptr<ASTReaderListener> L) {
1685     if (Listener)
1686       L = std::make_unique<ChainedASTReaderListener>(std::move(L),
1687                                                       std::move(Listener));
1688     Listener = std::move(L);
1689   }
1690 
1691   /// RAII object to temporarily add an AST callback listener.
1692   class ListenerScope {
1693     ASTReader &Reader;
1694     bool Chained = false;
1695 
1696   public:
ListenerScope(ASTReader & Reader,std::unique_ptr<ASTReaderListener> L)1697     ListenerScope(ASTReader &Reader, std::unique_ptr<ASTReaderListener> L)
1698         : Reader(Reader) {
1699       auto Old = Reader.takeListener();
1700       if (Old) {
1701         Chained = true;
1702         L = std::make_unique<ChainedASTReaderListener>(std::move(L),
1703                                                         std::move(Old));
1704       }
1705       Reader.setListener(std::move(L));
1706     }
1707 
~ListenerScope()1708     ~ListenerScope() {
1709       auto New = Reader.takeListener();
1710       if (Chained)
1711         Reader.setListener(static_cast<ChainedASTReaderListener *>(New.get())
1712                                ->takeSecond());
1713     }
1714   };
1715 
1716   /// Set the AST deserialization listener.
1717   void setDeserializationListener(ASTDeserializationListener *Listener,
1718                                   bool TakeOwnership = false);
1719 
1720   /// Get the AST deserialization listener.
getDeserializationListener()1721   ASTDeserializationListener *getDeserializationListener() {
1722     return DeserializationListener;
1723   }
1724 
1725   /// Determine whether this AST reader has a global index.
hasGlobalIndex()1726   bool hasGlobalIndex() const { return (bool)GlobalIndex; }
1727 
1728   /// Return global module index.
getGlobalIndex()1729   GlobalModuleIndex *getGlobalIndex() { return GlobalIndex.get(); }
1730 
1731   /// Reset reader for a reload try.
resetForReload()1732   void resetForReload() { TriedLoadingGlobalIndex = false; }
1733 
1734   /// Attempts to load the global index.
1735   ///
1736   /// \returns true if loading the global index has failed for any reason.
1737   bool loadGlobalIndex();
1738 
1739   /// Determine whether we tried to load the global index, but failed,
1740   /// e.g., because it is out-of-date or does not exist.
1741   bool isGlobalIndexUnavailable() const;
1742 
1743   /// Initializes the ASTContext
1744   void InitializeContext();
1745 
1746   /// Update the state of Sema after loading some additional modules.
1747   void UpdateSema();
1748 
1749   /// Add in-memory (virtual file) buffer.
addInMemoryBuffer(StringRef & FileName,std::unique_ptr<llvm::MemoryBuffer> Buffer)1750   void addInMemoryBuffer(StringRef &FileName,
1751                          std::unique_ptr<llvm::MemoryBuffer> Buffer) {
1752     ModuleMgr.addInMemoryBuffer(FileName, std::move(Buffer));
1753   }
1754 
1755   /// Finalizes the AST reader's state before writing an AST file to
1756   /// disk.
1757   ///
1758   /// This operation may undo temporary state in the AST that should not be
1759   /// emitted.
1760   void finalizeForWriting();
1761 
1762   /// Retrieve the module manager.
getModuleManager()1763   ModuleManager &getModuleManager() { return ModuleMgr; }
1764 
1765   /// Retrieve the preprocessor.
getPreprocessor()1766   Preprocessor &getPreprocessor() const { return PP; }
1767 
1768   /// Retrieve the name of the original source file name for the primary
1769   /// module file.
getOriginalSourceFile()1770   StringRef getOriginalSourceFile() {
1771     return ModuleMgr.getPrimaryModule().OriginalSourceFileName;
1772   }
1773 
1774   /// Retrieve the name of the original source file name directly from
1775   /// the AST file, without actually loading the AST file.
1776   static std::string
1777   getOriginalSourceFile(const std::string &ASTFileName, FileManager &FileMgr,
1778                         const PCHContainerReader &PCHContainerRdr,
1779                         DiagnosticsEngine &Diags);
1780 
1781   /// Read the control block for the named AST file.
1782   ///
1783   /// \returns true if an error occurred, false otherwise.
1784   static bool readASTFileControlBlock(
1785       StringRef Filename, FileManager &FileMgr,
1786       const InMemoryModuleCache &ModuleCache,
1787       const PCHContainerReader &PCHContainerRdr, bool FindModuleFileExtensions,
1788       ASTReaderListener &Listener, bool ValidateDiagnosticOptions,
1789       unsigned ClientLoadCapabilities = ARR_ConfigurationMismatch |
1790                                         ARR_OutOfDate);
1791 
1792   /// Determine whether the given AST file is acceptable to load into a
1793   /// translation unit with the given language and target options.
1794   static bool isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
1795                                   const InMemoryModuleCache &ModuleCache,
1796                                   const PCHContainerReader &PCHContainerRdr,
1797                                   const LangOptions &LangOpts,
1798                                   const TargetOptions &TargetOpts,
1799                                   const PreprocessorOptions &PPOpts,
1800                                   StringRef ExistingModuleCachePath,
1801                                   bool RequireStrictOptionMatches = false);
1802 
1803   /// Returns the suggested contents of the predefines buffer,
1804   /// which contains a (typically-empty) subset of the predefines
1805   /// build prior to including the precompiled header.
getSuggestedPredefines()1806   const std::string &getSuggestedPredefines() { return SuggestedPredefines; }
1807 
1808   /// Read a preallocated preprocessed entity from the external source.
1809   ///
1810   /// \returns null if an error occurred that prevented the preprocessed
1811   /// entity from being loaded.
1812   PreprocessedEntity *ReadPreprocessedEntity(unsigned Index) override;
1813 
1814   /// Returns a pair of [Begin, End) indices of preallocated
1815   /// preprocessed entities that \p Range encompasses.
1816   std::pair<unsigned, unsigned>
1817       findPreprocessedEntitiesInRange(SourceRange Range) override;
1818 
1819   /// Optionally returns true or false if the preallocated preprocessed
1820   /// entity with index \p Index came from file \p FID.
1821   std::optional<bool> isPreprocessedEntityInFileID(unsigned Index,
1822                                                    FileID FID) override;
1823 
1824   /// Read a preallocated skipped range from the external source.
1825   SourceRange ReadSkippedRange(unsigned Index) override;
1826 
1827   /// Read the header file information for the given file entry.
1828   HeaderFileInfo GetHeaderFileInfo(FileEntryRef FE) override;
1829 
1830   void ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag);
1831 
1832   /// Returns the number of source locations found in the chain.
getTotalNumSLocs()1833   unsigned getTotalNumSLocs() const {
1834     return TotalNumSLocEntries;
1835   }
1836 
1837   /// Returns the number of identifiers found in the chain.
getTotalNumIdentifiers()1838   unsigned getTotalNumIdentifiers() const {
1839     return static_cast<unsigned>(IdentifiersLoaded.size());
1840   }
1841 
1842   /// Returns the number of macros found in the chain.
getTotalNumMacros()1843   unsigned getTotalNumMacros() const {
1844     return static_cast<unsigned>(MacrosLoaded.size());
1845   }
1846 
1847   /// Returns the number of types found in the chain.
getTotalNumTypes()1848   unsigned getTotalNumTypes() const {
1849     return static_cast<unsigned>(TypesLoaded.size());
1850   }
1851 
1852   /// Returns the number of declarations found in the chain.
getTotalNumDecls()1853   unsigned getTotalNumDecls() const {
1854     return static_cast<unsigned>(DeclsLoaded.size());
1855   }
1856 
1857   /// Returns the number of submodules known.
getTotalNumSubmodules()1858   unsigned getTotalNumSubmodules() const {
1859     return static_cast<unsigned>(SubmodulesLoaded.size());
1860   }
1861 
1862   /// Returns the number of selectors found in the chain.
getTotalNumSelectors()1863   unsigned getTotalNumSelectors() const {
1864     return static_cast<unsigned>(SelectorsLoaded.size());
1865   }
1866 
1867   /// Returns the number of preprocessed entities known to the AST
1868   /// reader.
getTotalNumPreprocessedEntities()1869   unsigned getTotalNumPreprocessedEntities() const {
1870     unsigned Result = 0;
1871     for (const auto &M : ModuleMgr)
1872       Result += M.NumPreprocessedEntities;
1873     return Result;
1874   }
1875 
1876   /// Resolve a type ID into a type, potentially building a new
1877   /// type.
1878   QualType GetType(serialization::TypeID ID);
1879 
1880   /// Resolve a local type ID within a given AST file into a type.
1881   QualType getLocalType(ModuleFile &F, unsigned LocalID);
1882 
1883   /// Map a local type ID within a given AST file into a global type ID.
1884   serialization::TypeID getGlobalTypeID(ModuleFile &F, unsigned LocalID) const;
1885 
1886   /// Read a type from the current position in the given record, which
1887   /// was read from the given AST file.
readType(ModuleFile & F,const RecordData & Record,unsigned & Idx)1888   QualType readType(ModuleFile &F, const RecordData &Record, unsigned &Idx) {
1889     if (Idx >= Record.size())
1890       return {};
1891 
1892     return getLocalType(F, Record[Idx++]);
1893   }
1894 
1895   /// Map from a local declaration ID within a given module to a
1896   /// global declaration ID.
1897   serialization::DeclID getGlobalDeclID(ModuleFile &F,
1898                                       serialization::LocalDeclID LocalID) const;
1899 
1900   /// Returns true if global DeclID \p ID originated from module \p M.
1901   bool isDeclIDFromModule(serialization::GlobalDeclID ID, ModuleFile &M) const;
1902 
1903   /// Retrieve the module file that owns the given declaration, or NULL
1904   /// if the declaration is not from a module file.
1905   ModuleFile *getOwningModuleFile(const Decl *D);
1906 
1907   /// Returns the source location for the decl \p ID.
1908   SourceLocation getSourceLocationForDeclID(serialization::GlobalDeclID ID);
1909 
1910   /// Resolve a declaration ID into a declaration, potentially
1911   /// building a new declaration.
1912   Decl *GetDecl(serialization::DeclID ID);
1913   Decl *GetExternalDecl(uint32_t ID) override;
1914 
1915   /// Resolve a declaration ID into a declaration. Return 0 if it's not
1916   /// been loaded yet.
1917   Decl *GetExistingDecl(serialization::DeclID ID);
1918 
1919   /// Reads a declaration with the given local ID in the given module.
GetLocalDecl(ModuleFile & F,uint32_t LocalID)1920   Decl *GetLocalDecl(ModuleFile &F, uint32_t LocalID) {
1921     return GetDecl(getGlobalDeclID(F, LocalID));
1922   }
1923 
1924   /// Reads a declaration with the given local ID in the given module.
1925   ///
1926   /// \returns The requested declaration, casted to the given return type.
1927   template<typename T>
GetLocalDeclAs(ModuleFile & F,uint32_t LocalID)1928   T *GetLocalDeclAs(ModuleFile &F, uint32_t LocalID) {
1929     return cast_or_null<T>(GetLocalDecl(F, LocalID));
1930   }
1931 
1932   /// Map a global declaration ID into the declaration ID used to
1933   /// refer to this declaration within the given module fule.
1934   ///
1935   /// \returns the global ID of the given declaration as known in the given
1936   /// module file.
1937   serialization::DeclID
1938   mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
1939                                   serialization::DeclID GlobalID);
1940 
1941   /// Reads a declaration ID from the given position in a record in the
1942   /// given module.
1943   ///
1944   /// \returns The declaration ID read from the record, adjusted to a global ID.
1945   serialization::DeclID ReadDeclID(ModuleFile &F, const RecordData &Record,
1946                                    unsigned &Idx);
1947 
1948   /// Reads a declaration from the given position in a record in the
1949   /// given module.
ReadDecl(ModuleFile & F,const RecordData & R,unsigned & I)1950   Decl *ReadDecl(ModuleFile &F, const RecordData &R, unsigned &I) {
1951     return GetDecl(ReadDeclID(F, R, I));
1952   }
1953 
1954   /// Reads a declaration from the given position in a record in the
1955   /// given module.
1956   ///
1957   /// \returns The declaration read from this location, casted to the given
1958   /// result type.
1959   template<typename T>
ReadDeclAs(ModuleFile & F,const RecordData & R,unsigned & I)1960   T *ReadDeclAs(ModuleFile &F, const RecordData &R, unsigned &I) {
1961     return cast_or_null<T>(GetDecl(ReadDeclID(F, R, I)));
1962   }
1963 
1964   /// If any redeclarations of \p D have been imported since it was
1965   /// last checked, this digs out those redeclarations and adds them to the
1966   /// redeclaration chain for \p D.
1967   void CompleteRedeclChain(const Decl *D) override;
1968 
1969   CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset) override;
1970 
1971   /// Resolve the offset of a statement into a statement.
1972   ///
1973   /// This operation will read a new statement from the external
1974   /// source each time it is called, and is meant to be used via a
1975   /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
1976   Stmt *GetExternalDeclStmt(uint64_t Offset) override;
1977 
1978   /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1979   /// specified cursor.  Read the abbreviations that are at the top of the block
1980   /// and then leave the cursor pointing into the block.
1981   static llvm::Error ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
1982                                       unsigned BlockID,
1983                                       uint64_t *StartOfBlockOffset = nullptr);
1984 
1985   /// Finds all the visible declarations with a given name.
1986   /// The current implementation of this method just loads the entire
1987   /// lookup table as unmaterialized references.
1988   bool FindExternalVisibleDeclsByName(const DeclContext *DC,
1989                                       DeclarationName Name) override;
1990 
1991   /// Read all of the declarations lexically stored in a
1992   /// declaration context.
1993   ///
1994   /// \param DC The declaration context whose declarations will be
1995   /// read.
1996   ///
1997   /// \param IsKindWeWant A predicate indicating which declaration kinds
1998   /// we are interested in.
1999   ///
2000   /// \param Decls Vector that will contain the declarations loaded
2001   /// from the external source. The caller is responsible for merging
2002   /// these declarations with any declarations already stored in the
2003   /// declaration context.
2004   void
2005   FindExternalLexicalDecls(const DeclContext *DC,
2006                            llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
2007                            SmallVectorImpl<Decl *> &Decls) override;
2008 
2009   /// Get the decls that are contained in a file in the Offset/Length
2010   /// range. \p Length can be 0 to indicate a point at \p Offset instead of
2011   /// a range.
2012   void FindFileRegionDecls(FileID File, unsigned Offset, unsigned Length,
2013                            SmallVectorImpl<Decl *> &Decls) override;
2014 
2015   /// Notify ASTReader that we started deserialization of
2016   /// a decl or type so until FinishedDeserializing is called there may be
2017   /// decls that are initializing. Must be paired with FinishedDeserializing.
2018   void StartedDeserializing() override;
2019 
2020   /// Notify ASTReader that we finished the deserialization of
2021   /// a decl or type. Must be paired with StartedDeserializing.
2022   void FinishedDeserializing() override;
2023 
2024   /// Function that will be invoked when we begin parsing a new
2025   /// translation unit involving this external AST source.
2026   ///
2027   /// This function will provide all of the external definitions to
2028   /// the ASTConsumer.
2029   void StartTranslationUnit(ASTConsumer *Consumer) override;
2030 
2031   /// Print some statistics about AST usage.
2032   void PrintStats() override;
2033 
2034   /// Dump information about the AST reader to standard error.
2035   void dump();
2036 
2037   /// Return the amount of memory used by memory buffers, breaking down
2038   /// by heap-backed versus mmap'ed memory.
2039   void getMemoryBufferSizes(MemoryBufferSizes &sizes) const override;
2040 
2041   /// Initialize the semantic source with the Sema instance
2042   /// being used to perform semantic analysis on the abstract syntax
2043   /// tree.
2044   void InitializeSema(Sema &S) override;
2045 
2046   /// Inform the semantic consumer that Sema is no longer available.
ForgetSema()2047   void ForgetSema() override { SemaObj = nullptr; }
2048 
2049   /// Retrieve the IdentifierInfo for the named identifier.
2050   ///
2051   /// This routine builds a new IdentifierInfo for the given identifier. If any
2052   /// declarations with this name are visible from translation unit scope, their
2053   /// declarations will be deserialized and introduced into the declaration
2054   /// chain of the identifier.
2055   IdentifierInfo *get(StringRef Name) override;
2056 
2057   /// Retrieve an iterator into the set of all identifiers
2058   /// in all loaded AST files.
2059   IdentifierIterator *getIdentifiers() override;
2060 
2061   /// Load the contents of the global method pool for a given
2062   /// selector.
2063   void ReadMethodPool(Selector Sel) override;
2064 
2065   /// Load the contents of the global method pool for a given
2066   /// selector if necessary.
2067   void updateOutOfDateSelector(Selector Sel) override;
2068 
2069   /// Load the set of namespaces that are known to the external source,
2070   /// which will be used during typo correction.
2071   void ReadKnownNamespaces(
2072                          SmallVectorImpl<NamespaceDecl *> &Namespaces) override;
2073 
2074   void ReadUndefinedButUsed(
2075       llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) override;
2076 
2077   void ReadMismatchingDeleteExpressions(llvm::MapVector<
2078       FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
2079                                             Exprs) override;
2080 
2081   void ReadTentativeDefinitions(
2082                             SmallVectorImpl<VarDecl *> &TentativeDefs) override;
2083 
2084   void ReadUnusedFileScopedDecls(
2085                        SmallVectorImpl<const DeclaratorDecl *> &Decls) override;
2086 
2087   void ReadDelegatingConstructors(
2088                          SmallVectorImpl<CXXConstructorDecl *> &Decls) override;
2089 
2090   void ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) override;
2091 
2092   void ReadUnusedLocalTypedefNameCandidates(
2093       llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) override;
2094 
2095   void ReadDeclsToCheckForDeferredDiags(
2096       llvm::SmallSetVector<Decl *, 4> &Decls) override;
2097 
2098   void ReadReferencedSelectors(
2099            SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) override;
2100 
2101   void ReadWeakUndeclaredIdentifiers(
2102       SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) override;
2103 
2104   void ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) override;
2105 
2106   void ReadPendingInstantiations(
2107                   SmallVectorImpl<std::pair<ValueDecl *,
2108                                             SourceLocation>> &Pending) override;
2109 
2110   void ReadLateParsedTemplates(
2111       llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
2112           &LPTMap) override;
2113 
2114   void AssignedLambdaNumbering(const CXXRecordDecl *Lambda) override;
2115 
2116   /// Load a selector from disk, registering its ID if it exists.
2117   void LoadSelector(Selector Sel);
2118 
2119   void SetIdentifierInfo(unsigned ID, IdentifierInfo *II);
2120   void SetGloballyVisibleDecls(IdentifierInfo *II,
2121                                const SmallVectorImpl<uint32_t> &DeclIDs,
2122                                SmallVectorImpl<Decl *> *Decls = nullptr);
2123 
2124   /// Report a diagnostic.
2125   DiagnosticBuilder Diag(unsigned DiagID) const;
2126 
2127   /// Report a diagnostic.
2128   DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const;
2129 
2130   IdentifierInfo *DecodeIdentifierInfo(serialization::IdentifierID ID);
2131 
readIdentifier(ModuleFile & M,const RecordData & Record,unsigned & Idx)2132   IdentifierInfo *readIdentifier(ModuleFile &M, const RecordData &Record,
2133                                  unsigned &Idx) {
2134     return DecodeIdentifierInfo(getGlobalIdentifierID(M, Record[Idx++]));
2135   }
2136 
GetIdentifier(serialization::IdentifierID ID)2137   IdentifierInfo *GetIdentifier(serialization::IdentifierID ID) override {
2138     // Note that we are loading an identifier.
2139     Deserializing AnIdentifier(this);
2140 
2141     return DecodeIdentifierInfo(ID);
2142   }
2143 
2144   IdentifierInfo *getLocalIdentifier(ModuleFile &M, unsigned LocalID);
2145 
2146   serialization::IdentifierID getGlobalIdentifierID(ModuleFile &M,
2147                                                     unsigned LocalID);
2148 
2149   void resolvePendingMacro(IdentifierInfo *II, const PendingMacroInfo &PMInfo);
2150 
2151   /// Retrieve the macro with the given ID.
2152   MacroInfo *getMacro(serialization::MacroID ID);
2153 
2154   /// Retrieve the global macro ID corresponding to the given local
2155   /// ID within the given module file.
2156   serialization::MacroID getGlobalMacroID(ModuleFile &M, unsigned LocalID);
2157 
2158   /// Read the source location entry with index ID.
2159   bool ReadSLocEntry(int ID) override;
2160   /// Get the index ID for the loaded SourceLocation offset.
2161   int getSLocEntryID(SourceLocation::UIntTy SLocOffset) override;
2162   /// Try to read the offset of the SLocEntry at the given index in the given
2163   /// module file.
2164   llvm::Expected<SourceLocation::UIntTy> readSLocOffset(ModuleFile *F,
2165                                                         unsigned Index);
2166 
2167   /// Retrieve the module import location and module name for the
2168   /// given source manager entry ID.
2169   std::pair<SourceLocation, StringRef> getModuleImportLoc(int ID) override;
2170 
2171   /// Retrieve the global submodule ID given a module and its local ID
2172   /// number.
2173   serialization::SubmoduleID
2174   getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID);
2175 
2176   /// Retrieve the submodule that corresponds to a global submodule ID.
2177   ///
2178   Module *getSubmodule(serialization::SubmoduleID GlobalID);
2179 
2180   /// Retrieve the module that corresponds to the given module ID.
2181   ///
2182   /// Note: overrides method in ExternalASTSource
2183   Module *getModule(unsigned ID) override;
2184 
2185   /// Retrieve the module file with a given local ID within the specified
2186   /// ModuleFile.
2187   ModuleFile *getLocalModuleFile(ModuleFile &M, unsigned ID);
2188 
2189   /// Get an ID for the given module file.
2190   unsigned getModuleFileID(ModuleFile *M);
2191 
2192   /// Return a descriptor for the corresponding module.
2193   std::optional<ASTSourceDescriptor> getSourceDescriptor(unsigned ID) override;
2194 
2195   ExtKind hasExternalDefinitions(const Decl *D) override;
2196 
2197   /// Retrieve a selector from the given module with its local ID
2198   /// number.
2199   Selector getLocalSelector(ModuleFile &M, unsigned LocalID);
2200 
2201   Selector DecodeSelector(serialization::SelectorID Idx);
2202 
2203   Selector GetExternalSelector(serialization::SelectorID ID) override;
2204   uint32_t GetNumExternalSelectors() override;
2205 
ReadSelector(ModuleFile & M,const RecordData & Record,unsigned & Idx)2206   Selector ReadSelector(ModuleFile &M, const RecordData &Record, unsigned &Idx) {
2207     return getLocalSelector(M, Record[Idx++]);
2208   }
2209 
2210   /// Retrieve the global selector ID that corresponds to this
2211   /// the local selector ID in a given module.
2212   serialization::SelectorID getGlobalSelectorID(ModuleFile &M,
2213                                                 unsigned LocalID) const;
2214 
2215   /// Read the contents of a CXXCtorInitializer array.
2216   CXXCtorInitializer **GetExternalCXXCtorInitializers(uint64_t Offset) override;
2217 
2218   /// Read a AlignPackInfo from raw form.
ReadAlignPackInfo(uint32_t Raw)2219   Sema::AlignPackInfo ReadAlignPackInfo(uint32_t Raw) const {
2220     return Sema::AlignPackInfo::getFromRawEncoding(Raw);
2221   }
2222 
2223   /// Read a source location from raw form and return it in its
2224   /// originating module file's source location space.
2225   SourceLocation ReadUntranslatedSourceLocation(SourceLocation::UIntTy Raw,
2226                                                 LocSeq *Seq = nullptr) const {
2227     return SourceLocationEncoding::decode(Raw, Seq);
2228   }
2229 
2230   /// Read a source location from raw form.
2231   SourceLocation ReadSourceLocation(ModuleFile &ModuleFile,
2232                                     SourceLocation::UIntTy Raw,
2233                                     LocSeq *Seq = nullptr) const {
2234     SourceLocation Loc = ReadUntranslatedSourceLocation(Raw, Seq);
2235     return TranslateSourceLocation(ModuleFile, Loc);
2236   }
2237 
2238   /// Translate a source location from another module file's source
2239   /// location space into ours.
TranslateSourceLocation(ModuleFile & ModuleFile,SourceLocation Loc)2240   SourceLocation TranslateSourceLocation(ModuleFile &ModuleFile,
2241                                          SourceLocation Loc) const {
2242     if (!ModuleFile.ModuleOffsetMap.empty())
2243       ReadModuleOffsetMap(ModuleFile);
2244     assert(ModuleFile.SLocRemap.find(Loc.getOffset()) !=
2245                ModuleFile.SLocRemap.end() &&
2246            "Cannot find offset to remap.");
2247     SourceLocation::IntTy Remap =
2248         ModuleFile.SLocRemap.find(Loc.getOffset())->second;
2249     return Loc.getLocWithOffset(Remap);
2250   }
2251 
2252   /// Read a source location.
2253   SourceLocation ReadSourceLocation(ModuleFile &ModuleFile,
2254                                     const RecordDataImpl &Record, unsigned &Idx,
2255                                     LocSeq *Seq = nullptr) {
2256     return ReadSourceLocation(ModuleFile, Record[Idx++], Seq);
2257   }
2258 
2259   /// Read a FileID.
ReadFileID(ModuleFile & F,const RecordDataImpl & Record,unsigned & Idx)2260   FileID ReadFileID(ModuleFile &F, const RecordDataImpl &Record,
2261                     unsigned &Idx) const {
2262     return TranslateFileID(F, FileID::get(Record[Idx++]));
2263   }
2264 
2265   /// Translate a FileID from another module file's FileID space into ours.
TranslateFileID(ModuleFile & F,FileID FID)2266   FileID TranslateFileID(ModuleFile &F, FileID FID) const {
2267     assert(FID.ID >= 0 && "Reading non-local FileID.");
2268     return FileID::get(F.SLocEntryBaseID + FID.ID - 1);
2269   }
2270 
2271   /// Read a source range.
2272   SourceRange ReadSourceRange(ModuleFile &F, const RecordData &Record,
2273                               unsigned &Idx, LocSeq *Seq = nullptr);
2274 
2275   static llvm::BitVector ReadBitVector(const RecordData &Record,
2276                                        const StringRef Blob);
2277 
2278   // Read a string
2279   static std::string ReadString(const RecordDataImpl &Record, unsigned &Idx);
2280 
2281   // Skip a string
SkipString(const RecordData & Record,unsigned & Idx)2282   static void SkipString(const RecordData &Record, unsigned &Idx) {
2283     Idx += Record[Idx] + 1;
2284   }
2285 
2286   // Read a path
2287   std::string ReadPath(ModuleFile &F, const RecordData &Record, unsigned &Idx);
2288 
2289   // Read a path
2290   std::string ReadPath(StringRef BaseDirectory, const RecordData &Record,
2291                        unsigned &Idx);
2292 
2293   // Skip a path
SkipPath(const RecordData & Record,unsigned & Idx)2294   static void SkipPath(const RecordData &Record, unsigned &Idx) {
2295     SkipString(Record, Idx);
2296   }
2297 
2298   /// Read a version tuple.
2299   static VersionTuple ReadVersionTuple(const RecordData &Record, unsigned &Idx);
2300 
2301   CXXTemporary *ReadCXXTemporary(ModuleFile &F, const RecordData &Record,
2302                                  unsigned &Idx);
2303 
2304   /// Reads a statement.
2305   Stmt *ReadStmt(ModuleFile &F);
2306 
2307   /// Reads an expression.
2308   Expr *ReadExpr(ModuleFile &F);
2309 
2310   /// Reads a sub-statement operand during statement reading.
ReadSubStmt()2311   Stmt *ReadSubStmt() {
2312     assert(ReadingKind == Read_Stmt &&
2313            "Should be called only during statement reading!");
2314     // Subexpressions are stored from last to first, so the next Stmt we need
2315     // is at the back of the stack.
2316     assert(!StmtStack.empty() && "Read too many sub-statements!");
2317     return StmtStack.pop_back_val();
2318   }
2319 
2320   /// Reads a sub-expression operand during statement reading.
2321   Expr *ReadSubExpr();
2322 
2323   /// Reads a token out of a record.
2324   Token ReadToken(ModuleFile &M, const RecordDataImpl &Record, unsigned &Idx);
2325 
2326   /// Reads the macro record located at the given offset.
2327   MacroInfo *ReadMacroRecord(ModuleFile &F, uint64_t Offset);
2328 
2329   /// Determine the global preprocessed entity ID that corresponds to
2330   /// the given local ID within the given module.
2331   serialization::PreprocessedEntityID
2332   getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const;
2333 
2334   /// Add a macro to deserialize its macro directive history.
2335   ///
2336   /// \param II The name of the macro.
2337   /// \param M The module file.
2338   /// \param MacroDirectivesOffset Offset of the serialized macro directive
2339   /// history.
2340   void addPendingMacro(IdentifierInfo *II, ModuleFile *M,
2341                        uint32_t MacroDirectivesOffset);
2342 
2343   /// Read the set of macros defined by this external macro source.
2344   void ReadDefinedMacros() override;
2345 
2346   /// Update an out-of-date identifier.
2347   void updateOutOfDateIdentifier(IdentifierInfo &II) override;
2348 
2349   /// Note that this identifier is up-to-date.
2350   void markIdentifierUpToDate(IdentifierInfo *II);
2351 
2352   /// Load all external visible decls in the given DeclContext.
2353   void completeVisibleDeclsMap(const DeclContext *DC) override;
2354 
2355   /// Retrieve the AST context that this AST reader supplements.
getContext()2356   ASTContext &getContext() {
2357     assert(ContextObj && "requested AST context when not loading AST");
2358     return *ContextObj;
2359   }
2360 
2361   // Contains the IDs for declarations that were requested before we have
2362   // access to a Sema object.
2363   SmallVector<uint64_t, 16> PreloadedDeclIDs;
2364 
2365   /// Retrieve the semantic analysis object used to analyze the
2366   /// translation unit in which the precompiled header is being
2367   /// imported.
getSema()2368   Sema *getSema() { return SemaObj; }
2369 
2370   /// Get the identifier resolver used for name lookup / updates
2371   /// in the translation unit scope. We have one of these even if we don't
2372   /// have a Sema object.
2373   IdentifierResolver &getIdResolver();
2374 
2375   /// Retrieve the identifier table associated with the
2376   /// preprocessor.
2377   IdentifierTable &getIdentifierTable();
2378 
2379   /// Record that the given ID maps to the given switch-case
2380   /// statement.
2381   void RecordSwitchCaseID(SwitchCase *SC, unsigned ID);
2382 
2383   /// Retrieve the switch-case statement with the given ID.
2384   SwitchCase *getSwitchCaseWithID(unsigned ID);
2385 
2386   void ClearSwitchCaseIDs();
2387 
2388   /// Cursors for comments blocks.
2389   SmallVector<std::pair<llvm::BitstreamCursor,
2390                         serialization::ModuleFile *>, 8> CommentsCursors;
2391 
2392   /// Loads comments ranges.
2393   void ReadComments() override;
2394 
2395   /// Visit all the input file infos of the given module file.
2396   void visitInputFileInfos(
2397       serialization::ModuleFile &MF, bool IncludeSystem,
2398       llvm::function_ref<void(const serialization::InputFileInfo &IFI,
2399                               bool IsSystem)>
2400           Visitor);
2401 
2402   /// Visit all the input files of the given module file.
2403   void visitInputFiles(serialization::ModuleFile &MF,
2404                        bool IncludeSystem, bool Complain,
2405           llvm::function_ref<void(const serialization::InputFile &IF,
2406                                   bool isSystem)> Visitor);
2407 
2408   /// Visit all the top-level module maps loaded when building the given module
2409   /// file.
2410   void visitTopLevelModuleMaps(serialization::ModuleFile &MF,
2411                                llvm::function_ref<void(FileEntryRef)> Visitor);
2412 
isProcessingUpdateRecords()2413   bool isProcessingUpdateRecords() { return ProcessingUpdateRecords; }
2414 };
2415 
2416 /// A simple helper class to unpack an integer to bits and consuming
2417 /// the bits in order.
2418 class BitsUnpacker {
2419   constexpr static uint32_t BitsIndexUpbound = 32;
2420 
2421 public:
BitsUnpacker(uint32_t V)2422   BitsUnpacker(uint32_t V) { updateValue(V); }
2423   BitsUnpacker(const BitsUnpacker &) = delete;
2424   BitsUnpacker(BitsUnpacker &&) = delete;
2425   BitsUnpacker operator=(const BitsUnpacker &) = delete;
2426   BitsUnpacker operator=(BitsUnpacker &&) = delete;
2427   ~BitsUnpacker() = default;
2428 
updateValue(uint32_t V)2429   void updateValue(uint32_t V) {
2430     Value = V;
2431     CurrentBitsIndex = 0;
2432   }
2433 
advance(uint32_t BitsWidth)2434   void advance(uint32_t BitsWidth) { CurrentBitsIndex += BitsWidth; }
2435 
getNextBit()2436   bool getNextBit() {
2437     assert(isValid());
2438     return Value & (1 << CurrentBitsIndex++);
2439   }
2440 
getNextBits(uint32_t Width)2441   uint32_t getNextBits(uint32_t Width) {
2442     assert(isValid());
2443     assert(Width < BitsIndexUpbound);
2444     uint32_t Ret = (Value >> CurrentBitsIndex) & ((1 << Width) - 1);
2445     CurrentBitsIndex += Width;
2446     return Ret;
2447   }
2448 
canGetNextNBits(uint32_t Width)2449   bool canGetNextNBits(uint32_t Width) const {
2450     return CurrentBitsIndex + Width < BitsIndexUpbound;
2451   }
2452 
2453 private:
isValid()2454   bool isValid() const { return CurrentBitsIndex < BitsIndexUpbound; }
2455 
2456   uint32_t Value;
2457   uint32_t CurrentBitsIndex = ~0;
2458 };
2459 } // namespace clang
2460 
2461 #endif // LLVM_CLANG_SERIALIZATION_ASTREADER_H
2462