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