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