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