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