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