ASTReader.h revision 5456b0fe40714a78cd0ba7c1a5b7dc34eda385af
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_FRONTEND_AST_READER_H
15#define LLVM_CLANG_FRONTEND_AST_READER_H
16
17#include "clang/Serialization/ASTBitCodes.h"
18#include "clang/Serialization/ContinuousRangeMap.h"
19#include "clang/Serialization/Module.h"
20#include "clang/Serialization/ModuleManager.h"
21#include "clang/Sema/ExternalSemaSource.h"
22#include "clang/AST/DeclarationName.h"
23#include "clang/AST/DeclObjC.h"
24#include "clang/AST/TemplateBase.h"
25#include "clang/Lex/ExternalPreprocessorSource.h"
26#include "clang/Lex/HeaderSearch.h"
27#include "clang/Lex/PreprocessingRecord.h"
28#include "clang/Basic/Diagnostic.h"
29#include "clang/Basic/FileManager.h"
30#include "clang/Basic/FileSystemOptions.h"
31#include "clang/Basic/IdentifierTable.h"
32#include "clang/Basic/SourceManager.h"
33#include "llvm/ADT/APFloat.h"
34#include "llvm/ADT/APInt.h"
35#include "llvm/ADT/APSInt.h"
36#include "llvm/ADT/OwningPtr.h"
37#include "llvm/ADT/SmallPtrSet.h"
38#include "llvm/ADT/SmallSet.h"
39#include "llvm/ADT/SmallVector.h"
40#include "llvm/ADT/StringRef.h"
41#include "llvm/ADT/DenseSet.h"
42#include "llvm/Bitcode/BitstreamReader.h"
43#include "llvm/Support/DataTypes.h"
44#include <deque>
45#include <map>
46#include <string>
47#include <utility>
48#include <vector>
49
50namespace llvm {
51  class MemoryBuffer;
52}
53
54namespace clang {
55
56class AddrLabelExpr;
57class ASTConsumer;
58class ASTContext;
59class ASTIdentifierIterator;
60class ASTUnit; // FIXME: Layering violation and egregious hack.
61class Attr;
62class Decl;
63class DeclContext;
64class NestedNameSpecifier;
65class CXXBaseSpecifier;
66class CXXConstructorDecl;
67class CXXCtorInitializer;
68class GotoStmt;
69class MacroDefinition;
70class NamedDecl;
71class OpaqueValueExpr;
72class Preprocessor;
73class Sema;
74class SwitchCase;
75class ASTDeserializationListener;
76class ASTWriter;
77class ASTReader;
78class ASTDeclReader;
79class ASTStmtReader;
80class TypeLocReader;
81struct HeaderFileInfo;
82class VersionTuple;
83
84struct PCHPredefinesBlock {
85  /// \brief The file ID for this predefines buffer in a PCH file.
86  FileID BufferID;
87
88  /// \brief This predefines buffer in a PCH file.
89  StringRef Data;
90};
91typedef SmallVector<PCHPredefinesBlock, 2> PCHPredefinesBlocks;
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.
99class ASTReaderListener {
100public:
101  virtual ~ASTReaderListener();
102
103  /// \brief Receives the language options.
104  ///
105  /// \returns true to indicate the options are invalid or false otherwise.
106  virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
107    return false;
108  }
109
110  /// \brief Receives the target triple.
111  ///
112  /// \returns true to indicate the target triple is invalid or false otherwise.
113  virtual bool ReadTargetTriple(StringRef Triple) {
114    return false;
115  }
116
117  /// \brief Receives the contents of the predefines buffer.
118  ///
119  /// \param Buffers Information about the predefines buffers.
120  ///
121  /// \param OriginalFileName The original file name for the AST file, which
122  /// will appear as an entry in the predefines buffer.
123  ///
124  /// \param SuggestedPredefines If necessary, additional definitions are added
125  /// here.
126  ///
127  /// \returns true to indicate the predefines are invalid or false otherwise.
128  virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
129                                    StringRef OriginalFileName,
130                                    std::string &SuggestedPredefines,
131                                    FileManager &FileMgr) {
132    return false;
133  }
134
135  /// \brief Receives a HeaderFileInfo entry.
136  virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) {}
137
138  /// \brief Receives __COUNTER__ value.
139  virtual void ReadCounter(unsigned Value) {}
140};
141
142/// \brief ASTReaderListener implementation to validate the information of
143/// the PCH file against an initialized Preprocessor.
144class PCHValidator : public ASTReaderListener {
145  Preprocessor &PP;
146  ASTReader &Reader;
147
148  unsigned NumHeaderInfos;
149
150public:
151  PCHValidator(Preprocessor &PP, ASTReader &Reader)
152    : PP(PP), Reader(Reader), NumHeaderInfos(0) {}
153
154  virtual bool ReadLanguageOptions(const LangOptions &LangOpts);
155  virtual bool ReadTargetTriple(StringRef Triple);
156  virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
157                                    StringRef OriginalFileName,
158                                    std::string &SuggestedPredefines,
159                                    FileManager &FileMgr);
160  virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID);
161  virtual void ReadCounter(unsigned Value);
162
163private:
164  void Error(const char *Msg);
165};
166
167namespace serialization {
168
169class ReadMethodPoolVisitor;
170
171namespace reader {
172  class ASTIdentifierLookupTrait;
173  /// \brief The on-disk hash table used for the DeclContext's Name lookup table.
174  typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
175    ASTDeclContextNameLookupTable;
176}
177
178} // end namespace serialization
179
180/// \brief Reads an AST files chain containing the contents of a translation
181/// unit.
182///
183/// The ASTReader class reads bitstreams (produced by the ASTWriter
184/// class) containing the serialized representation of a given
185/// abstract syntax tree and its supporting data structures. An
186/// instance of the ASTReader can be attached to an ASTContext object,
187/// which will provide access to the contents of the AST files.
188///
189/// The AST reader provides lazy de-serialization of declarations, as
190/// required when traversing the AST. Only those AST nodes that are
191/// actually required will be de-serialized.
192class ASTReader
193  : public ExternalPreprocessorSource,
194    public ExternalPreprocessingRecordSource,
195    public ExternalHeaderFileInfoSource,
196    public ExternalSemaSource,
197    public IdentifierInfoLookup,
198    public ExternalIdentifierLookup,
199    public ExternalSLocEntrySource
200{
201public:
202  typedef SmallVector<uint64_t, 64> RecordData;
203
204  enum ASTReadResult { Success, Failure, IgnorePCH };
205  /// \brief Types of AST files.
206  friend class PCHValidator;
207  friend class ASTDeclReader;
208  friend class ASTStmtReader;
209  friend class ASTIdentifierIterator;
210  friend class serialization::reader::ASTIdentifierLookupTrait;
211  friend class TypeLocReader;
212  friend class ASTWriter;
213  friend class ASTUnit; // ASTUnit needs to remap source locations.
214  friend class serialization::ReadMethodPoolVisitor;
215
216  typedef serialization::ModuleFile ModuleFile;
217  typedef serialization::ModuleKind ModuleKind;
218  typedef serialization::ModuleManager ModuleManager;
219
220  typedef ModuleManager::ModuleIterator ModuleIterator;
221  typedef ModuleManager::ModuleConstIterator ModuleConstIterator;
222  typedef ModuleManager::ModuleReverseIterator ModuleReverseIterator;
223
224private:
225  /// \brief The receiver of some callbacks invoked by ASTReader.
226  OwningPtr<ASTReaderListener> Listener;
227
228  /// \brief The receiver of deserialization events.
229  ASTDeserializationListener *DeserializationListener;
230
231  SourceManager &SourceMgr;
232  FileManager &FileMgr;
233  DiagnosticsEngine &Diags;
234
235  /// \brief The semantic analysis object that will be processing the
236  /// AST files and the translation unit that uses it.
237  Sema *SemaObj;
238
239  /// \brief The preprocessor that will be loading the source file.
240  Preprocessor &PP;
241
242  /// \brief The AST context into which we'll read the AST files.
243  ASTContext &Context;
244
245  /// \brief The AST consumer.
246  ASTConsumer *Consumer;
247
248  /// \brief The module manager which manages modules and their dependencies
249  ModuleManager ModuleMgr;
250
251  /// \brief A map of global bit offsets to the module that stores entities
252  /// at those bit offsets.
253  ContinuousRangeMap<uint64_t, ModuleFile*, 4> GlobalBitOffsetsMap;
254
255  /// \brief A map of negated SLocEntryIDs to the modules containing them.
256  ContinuousRangeMap<unsigned, ModuleFile*, 64> GlobalSLocEntryMap;
257
258  typedef ContinuousRangeMap<unsigned, ModuleFile*, 64> GlobalSLocOffsetMapType;
259
260  /// \brief A map of reversed (SourceManager::MaxLoadedOffset - SLocOffset)
261  /// SourceLocation offsets to the modules containing them.
262  GlobalSLocOffsetMapType GlobalSLocOffsetMap;
263
264  /// \brief Types that have already been loaded from the chain.
265  ///
266  /// When the pointer at index I is non-NULL, the type with
267  /// ID = (I + 1) << FastQual::Width has already been loaded
268  std::vector<QualType> TypesLoaded;
269
270  typedef ContinuousRangeMap<serialization::TypeID, ModuleFile *, 4>
271    GlobalTypeMapType;
272
273  /// \brief Mapping from global type IDs to the module in which the
274  /// type resides along with the offset that should be added to the
275  /// global type ID to produce a local ID.
276  GlobalTypeMapType GlobalTypeMap;
277
278  /// \brief Declarations that have already been loaded from the chain.
279  ///
280  /// When the pointer at index I is non-NULL, the declaration with ID
281  /// = I + 1 has already been loaded.
282  std::vector<Decl *> DeclsLoaded;
283
284  typedef ContinuousRangeMap<serialization::DeclID, ModuleFile *, 4>
285    GlobalDeclMapType;
286
287  /// \brief Mapping from global declaration IDs to the module in which the
288  /// declaration resides.
289  GlobalDeclMapType GlobalDeclMap;
290
291  typedef std::pair<ModuleFile *, uint64_t> FileOffset;
292  typedef SmallVector<FileOffset, 2> FileOffsetsTy;
293  typedef llvm::DenseMap<serialization::DeclID, FileOffsetsTy>
294      DeclUpdateOffsetsMap;
295
296  /// \brief Declarations that have modifications residing in a later file
297  /// in the chain.
298  DeclUpdateOffsetsMap DeclUpdateOffsets;
299
300  struct ReplacedDeclInfo {
301    ModuleFile *Mod;
302    uint64_t Offset;
303    unsigned RawLoc;
304
305    ReplacedDeclInfo() : Mod(0), Offset(0), RawLoc(0) {}
306    ReplacedDeclInfo(ModuleFile *Mod, uint64_t Offset, unsigned RawLoc)
307      : Mod(Mod), Offset(Offset), RawLoc(RawLoc) {}
308  };
309
310  typedef llvm::DenseMap<serialization::DeclID, ReplacedDeclInfo>
311      DeclReplacementMap;
312  /// \brief Declarations that have been replaced in a later file in the chain.
313  DeclReplacementMap ReplacedDecls;
314
315  struct FileDeclsInfo {
316    ModuleFile *Mod;
317    ArrayRef<serialization::LocalDeclID> Decls;
318
319    FileDeclsInfo() : Mod(0) {}
320    FileDeclsInfo(ModuleFile *Mod, ArrayRef<serialization::LocalDeclID> Decls)
321      : Mod(Mod), Decls(Decls) {}
322  };
323
324  /// \brief Map from a FileID to the file-level declarations that it contains.
325  llvm::DenseMap<FileID, FileDeclsInfo> FileDeclIDs;
326
327  // Updates for visible decls can occur for other contexts than just the
328  // TU, and when we read those update records, the actual context will not
329  // be available yet (unless it's the TU), so have this pending map using the
330  // ID as a key. It will be realized when the context is actually loaded.
331  typedef
332    SmallVector<std::pair<serialization::reader::ASTDeclContextNameLookupTable *,
333                          ModuleFile*>, 1> DeclContextVisibleUpdates;
334  typedef llvm::DenseMap<serialization::DeclID, DeclContextVisibleUpdates>
335      DeclContextVisibleUpdatesPending;
336
337  /// \brief Updates to the visible declarations of declaration contexts that
338  /// haven't been loaded yet.
339  DeclContextVisibleUpdatesPending PendingVisibleUpdates;
340
341  /// \brief The set of C++ or Objective-C classes that have forward
342  /// declarations that have not yet been linked to their definitions.
343  llvm::SmallPtrSet<Decl *, 4> PendingDefinitions;
344
345  /// \brief Functions or methods that have bodies that will be attached.
346  llvm::SmallDenseMap<Decl *, uint64_t, 4> PendingBodies;
347
348  /// \brief Read the records that describe the contents of declcontexts.
349  bool ReadDeclContextStorage(ModuleFile &M,
350                              llvm::BitstreamCursor &Cursor,
351                              const std::pair<uint64_t, uint64_t> &Offsets,
352                              serialization::DeclContextInfo &Info);
353
354  /// \brief A vector containing identifiers that have already been
355  /// loaded.
356  ///
357  /// If the pointer at index I is non-NULL, then it refers to the
358  /// IdentifierInfo for the identifier with ID=I+1 that has already
359  /// been loaded.
360  std::vector<IdentifierInfo *> IdentifiersLoaded;
361
362  typedef ContinuousRangeMap<serialization::IdentID, ModuleFile *, 4>
363    GlobalIdentifierMapType;
364
365  /// \brief Mapping from global identifer IDs to the module in which the
366  /// identifier resides along with the offset that should be added to the
367  /// global identifier ID to produce a local ID.
368  GlobalIdentifierMapType GlobalIdentifierMap;
369
370  /// \brief A vector containing submodules that have already been loaded.
371  ///
372  /// This vector is indexed by the Submodule ID (-1). NULL submodule entries
373  /// indicate that the particular submodule ID has not yet been loaded.
374  SmallVector<Module *, 2> SubmodulesLoaded;
375
376  typedef ContinuousRangeMap<serialization::SubmoduleID, ModuleFile *, 4>
377    GlobalSubmoduleMapType;
378
379  /// \brief Mapping from global submodule IDs to the module file in which the
380  /// submodule resides along with the offset that should be added to the
381  /// global submodule ID to produce a local ID.
382  GlobalSubmoduleMapType GlobalSubmoduleMap;
383
384  /// \brief A set of hidden declarations.
385  typedef llvm::SmallVector<llvm::PointerUnion<Decl *, IdentifierInfo *>, 2>
386    HiddenNames;
387
388  typedef llvm::DenseMap<Module *, HiddenNames> HiddenNamesMapType;
389
390  /// \brief A mapping from each of the hidden submodules to the deserialized
391  /// declarations in that submodule that could be made visible.
392  HiddenNamesMapType HiddenNamesMap;
393
394
395  /// \brief A module import or export that hasn't yet been resolved.
396  struct UnresolvedModuleImportExport {
397    /// \brief The file in which this module resides.
398    ModuleFile *File;
399
400    /// \brief The module that is importing or exporting.
401    Module *Mod;
402
403    /// \brief The local ID of the module that is being exported.
404    unsigned ID;
405
406    /// \brief Whether this is an import (vs. an export).
407    unsigned IsImport : 1;
408
409    /// \brief Whether this is a wildcard export.
410    unsigned IsWildcard : 1;
411  };
412
413  /// \brief The set of module imports and exports that still need to be
414  /// resolved.
415  llvm::SmallVector<UnresolvedModuleImportExport, 2>
416    UnresolvedModuleImportExports;
417
418  /// \brief A vector containing selectors that have already been loaded.
419  ///
420  /// This vector is indexed by the Selector ID (-1). NULL selector
421  /// entries indicate that the particular selector ID has not yet
422  /// been loaded.
423  SmallVector<Selector, 16> SelectorsLoaded;
424
425  typedef ContinuousRangeMap<serialization::SelectorID, ModuleFile *, 4>
426    GlobalSelectorMapType;
427
428  /// \brief Mapping from global selector IDs to the module in which the
429  /// selector resides along with the offset that should be added to the
430  /// global selector ID to produce a local ID.
431  GlobalSelectorMapType GlobalSelectorMap;
432
433  /// \brief The generation number of the last time we loaded data from the
434  /// global method pool for this selector.
435  llvm::DenseMap<Selector, unsigned> SelectorGeneration;
436
437  /// \brief Mapping from identifiers that represent macros whose definitions
438  /// have not yet been deserialized to the global offset where the macro
439  /// record resides.
440  llvm::DenseMap<IdentifierInfo *, uint64_t> UnreadMacroRecordOffsets;
441
442  typedef ContinuousRangeMap<unsigned, ModuleFile *, 4>
443    GlobalPreprocessedEntityMapType;
444
445  /// \brief Mapping from global preprocessing entity IDs to the module in
446  /// which the preprocessed entity resides along with the offset that should be
447  /// added to the global preprocessing entitiy ID to produce a local ID.
448  GlobalPreprocessedEntityMapType GlobalPreprocessedEntityMap;
449
450  /// \name CodeGen-relevant special data
451  /// \brief Fields containing data that is relevant to CodeGen.
452  //@{
453
454  /// \brief The IDs of all declarations that fulfill the criteria of
455  /// "interesting" decls.
456  ///
457  /// This contains the data loaded from all EXTERNAL_DEFINITIONS blocks in the
458  /// chain. The referenced declarations are deserialized and passed to the
459  /// consumer eagerly.
460  SmallVector<uint64_t, 16> ExternalDefinitions;
461
462  /// \brief The IDs of all tentative definitions stored in the chain.
463  ///
464  /// Sema keeps track of all tentative definitions in a TU because it has to
465  /// complete them and pass them on to CodeGen. Thus, tentative definitions in
466  /// the PCH chain must be eagerly deserialized.
467  SmallVector<uint64_t, 16> TentativeDefinitions;
468
469  /// \brief The IDs of all CXXRecordDecls stored in the chain whose VTables are
470  /// used.
471  ///
472  /// CodeGen has to emit VTables for these records, so they have to be eagerly
473  /// deserialized.
474  SmallVector<uint64_t, 64> VTableUses;
475
476  /// \brief A snapshot of the pending instantiations in the chain.
477  ///
478  /// This record tracks the instantiations that Sema has to perform at the
479  /// end of the TU. It consists of a pair of values for every pending
480  /// instantiation where the first value is the ID of the decl and the second
481  /// is the instantiation location.
482  SmallVector<uint64_t, 64> PendingInstantiations;
483
484  //@}
485
486  /// \name DiagnosticsEngine-relevant special data
487  /// \brief Fields containing data that is used for generating diagnostics
488  //@{
489
490  /// \brief A snapshot of Sema's unused file-scoped variable tracking, for
491  /// generating warnings.
492  SmallVector<uint64_t, 16> UnusedFileScopedDecls;
493
494  /// \brief A list of all the delegating constructors we've seen, to diagnose
495  /// cycles.
496  SmallVector<uint64_t, 4> DelegatingCtorDecls;
497
498  /// \brief Method selectors used in a @selector expression. Used for
499  /// implementation of -Wselector.
500  SmallVector<uint64_t, 64> ReferencedSelectorsData;
501
502  /// \brief A snapshot of Sema's weak undeclared identifier tracking, for
503  /// generating warnings.
504  SmallVector<uint64_t, 64> WeakUndeclaredIdentifiers;
505
506  /// \brief The IDs of type aliases for ext_vectors that exist in the chain.
507  ///
508  /// Used by Sema for finding sugared names for ext_vectors in diagnostics.
509  SmallVector<uint64_t, 4> ExtVectorDecls;
510
511  //@}
512
513  /// \name Sema-relevant special data
514  /// \brief Fields containing data that is used for semantic analysis
515  //@{
516
517  /// \brief The IDs of all locally scoped external decls in the chain.
518  ///
519  /// Sema tracks these to validate that the types are consistent across all
520  /// local external declarations.
521  SmallVector<uint64_t, 16> LocallyScopedExternalDecls;
522
523  /// \brief The IDs of all dynamic class declarations in the chain.
524  ///
525  /// Sema tracks these because it checks for the key functions being defined
526  /// at the end of the TU, in which case it directs CodeGen to emit the VTable.
527  SmallVector<uint64_t, 16> DynamicClasses;
528
529  /// \brief The IDs of the declarations Sema stores directly.
530  ///
531  /// Sema tracks a few important decls, such as namespace std, directly.
532  SmallVector<uint64_t, 4> SemaDeclRefs;
533
534  /// \brief The IDs of the types ASTContext stores directly.
535  ///
536  /// The AST context tracks a few important types, such as va_list, directly.
537  SmallVector<uint64_t, 16> SpecialTypes;
538
539  /// \brief The IDs of CUDA-specific declarations ASTContext stores directly.
540  ///
541  /// The AST context tracks a few important decls, currently cudaConfigureCall,
542  /// directly.
543  SmallVector<uint64_t, 2> CUDASpecialDeclRefs;
544
545  /// \brief The floating point pragma option settings.
546  SmallVector<uint64_t, 1> FPPragmaOptions;
547
548  /// \brief The OpenCL extension settings.
549  SmallVector<uint64_t, 1> OpenCLExtensions;
550
551  /// \brief A list of the namespaces we've seen.
552  SmallVector<uint64_t, 4> KnownNamespaces;
553
554  /// \brief A list of modules that were imported by precompiled headers or
555  /// any other non-module AST file.
556  SmallVector<serialization::SubmoduleID, 2> ImportedModules;
557  //@}
558
559  /// \brief The original file name that was used to build the primary AST file,
560  /// which may have been modified for relocatable-pch support.
561  std::string OriginalFileName;
562
563  /// \brief The actual original file name that was used to build the primary
564  /// AST file.
565  std::string ActualOriginalFileName;
566
567  /// \brief The file ID for the original file that was used to build the
568  /// primary AST file.
569  FileID OriginalFileID;
570
571  /// \brief The directory that the PCH was originally created in. Used to
572  /// allow resolving headers even after headers+PCH was moved to a new path.
573  std::string OriginalDir;
574
575  /// \brief The directory that the PCH we are reading is stored in.
576  std::string CurrentDir;
577
578  /// \brief Whether this precompiled header is a relocatable PCH file.
579  bool RelocatablePCH;
580
581  /// \brief The system include root to be used when loading the
582  /// precompiled header.
583  std::string isysroot;
584
585  /// \brief Whether to disable the normal validation performed on precompiled
586  /// headers when they are loaded.
587  bool DisableValidation;
588
589  /// \brief Whether to disable the use of stat caches in AST files.
590  bool DisableStatCache;
591
592  /// \brief Whether to accept an AST file with compiler errors.
593  bool AllowASTWithCompilerErrors;
594
595  /// \brief The current "generation" of the module file import stack, which
596  /// indicates how many separate module file load operations have occurred.
597  unsigned CurrentGeneration;
598
599  typedef llvm::DenseMap<unsigned, SwitchCase *> SwitchCaseMapTy;
600  /// \brief Mapping from switch-case IDs in the chain to switch-case statements
601  ///
602  /// Statements usually don't have IDs, but switch cases need them, so that the
603  /// switch statement can refer to them.
604  SwitchCaseMapTy SwitchCaseStmts;
605
606  SwitchCaseMapTy *CurrSwitchCaseStmts;
607
608  /// \brief The number of stat() calls that hit/missed the stat
609  /// cache.
610  unsigned NumStatHits, NumStatMisses;
611
612  /// \brief The number of source location entries de-serialized from
613  /// the PCH file.
614  unsigned NumSLocEntriesRead;
615
616  /// \brief The number of source location entries in the chain.
617  unsigned TotalNumSLocEntries;
618
619  /// \brief The number of statements (and expressions) de-serialized
620  /// from the chain.
621  unsigned NumStatementsRead;
622
623  /// \brief The total number of statements (and expressions) stored
624  /// in the chain.
625  unsigned TotalNumStatements;
626
627  /// \brief The number of macros de-serialized from the chain.
628  unsigned NumMacrosRead;
629
630  /// \brief The total number of macros stored in the chain.
631  unsigned TotalNumMacros;
632
633  /// \brief The number of selectors that have been read.
634  unsigned NumSelectorsRead;
635
636  /// \brief The number of method pool entries that have been read.
637  unsigned NumMethodPoolEntriesRead;
638
639  /// \brief The number of times we have looked up a selector in the method
640  /// pool and not found anything interesting.
641  unsigned NumMethodPoolMisses;
642
643  /// \brief The total number of method pool entries in the selector table.
644  unsigned TotalNumMethodPoolEntries;
645
646  /// Number of lexical decl contexts read/total.
647  unsigned NumLexicalDeclContextsRead, TotalLexicalDeclContexts;
648
649  /// Number of visible decl contexts read/total.
650  unsigned NumVisibleDeclContextsRead, TotalVisibleDeclContexts;
651
652  /// Total size of modules, in bits, currently loaded
653  uint64_t TotalModulesSizeInBits;
654
655  /// \brief Number of Decl/types that are currently deserializing.
656  unsigned NumCurrentElementsDeserializing;
657
658  /// \brief Set true while we are in the process of passing deserialized
659  /// "interesting" decls to consumer inside FinishedDeserializing().
660  /// This is used as a guard to avoid recursively repeating the process of
661  /// passing decls to consumer.
662  bool PassingDeclsToConsumer;
663
664  /// Number of CXX base specifiers currently loaded
665  unsigned NumCXXBaseSpecifiersLoaded;
666
667  /// \brief An IdentifierInfo that has been loaded but whose top-level
668  /// declarations of the same name have not (yet) been loaded.
669  struct PendingIdentifierInfo {
670    IdentifierInfo *II;
671    SmallVector<uint32_t, 4> DeclIDs;
672  };
673
674  /// \brief The set of identifiers that were read while the AST reader was
675  /// (recursively) loading declarations.
676  ///
677  /// The declarations on the identifier chain for these identifiers will be
678  /// loaded once the recursive loading has completed.
679  std::deque<PendingIdentifierInfo> PendingIdentifierInfos;
680
681  /// \brief The generation number of each identifier, which keeps track of
682  /// the last time we loaded information about this identifier.
683  llvm::DenseMap<IdentifierInfo *, unsigned> IdentifierGeneration;
684
685  /// \brief Contains declarations and definitions that will be
686  /// "interesting" to the ASTConsumer, when we get that AST consumer.
687  ///
688  /// "Interesting" declarations are those that have data that may
689  /// need to be emitted, such as inline function definitions or
690  /// Objective-C protocols.
691  std::deque<Decl *> InterestingDecls;
692
693  /// \brief The set of redeclarable declarations that have been deserialized
694  /// since the last time the declaration chains were linked.
695  llvm::SmallPtrSet<Decl *, 16> RedeclsDeserialized;
696
697  /// \brief The list of redeclaration chains that still need to be
698  /// reconstructed.
699  ///
700  /// Each element is the global declaration ID of the first declaration in
701  /// the chain. Elements in this vector should be unique; use
702  /// PendingDeclChainsKnown to ensure uniqueness.
703  llvm::SmallVector<serialization::DeclID, 16> PendingDeclChains;
704
705  /// \brief Keeps track of the elements added to PendingDeclChains.
706  llvm::SmallSet<serialization::DeclID, 16> PendingDeclChainsKnown;
707
708  /// \brief The set of Objective-C categories that have been deserialized
709  /// since the last time the declaration chains were linked.
710  llvm::SmallPtrSet<ObjCCategoryDecl *, 16> CategoriesDeserialized;
711
712  /// \brief The set of Objective-C class definitions that have already been
713  /// loaded, for which we will need to check for categories whenever a new
714  /// module is loaded.
715  llvm::SmallVector<ObjCInterfaceDecl *, 16> ObjCClassesLoaded;
716
717  typedef llvm::DenseMap<Decl *, llvm::SmallVector<serialization::DeclID, 2> >
718    MergedDeclsMap;
719
720  /// \brief A mapping from canonical declarations to the set of additional
721  /// (global, previously-canonical) declaration IDs that have been merged with
722  /// that canonical declaration.
723  MergedDeclsMap MergedDecls;
724
725  typedef llvm::DenseMap<serialization::GlobalDeclID,
726                         llvm::SmallVector<serialization::DeclID, 2> >
727    StoredMergedDeclsMap;
728
729  /// \brief A mapping from canonical declaration IDs to the set of additional
730  /// declaration IDs that have been merged with that canonical declaration.
731  ///
732  /// This is the deserialized representation of the entries in MergedDecls.
733  /// When we query entries in MergedDecls, they will be augmented with entries
734  /// from StoredMergedDecls.
735  StoredMergedDeclsMap StoredMergedDecls;
736
737  /// \brief Combine the stored merged declarations for the given canonical
738  /// declaration into the set of merged declarations.
739  ///
740  /// \returns An iterator into MergedDecls that corresponds to the position of
741  /// the given canonical declaration.
742  MergedDeclsMap::iterator
743  combineStoredMergedDecls(Decl *Canon, serialization::GlobalDeclID CanonID);
744
745  /// \brief Ready to load the previous declaration of the given Decl.
746  void loadAndAttachPreviousDecl(Decl *D, serialization::DeclID ID);
747
748  /// \brief When reading a Stmt tree, Stmt operands are placed in this stack.
749  SmallVector<Stmt *, 16> StmtStack;
750
751  /// \brief What kind of records we are reading.
752  enum ReadingKind {
753    Read_Decl, Read_Type, Read_Stmt
754  };
755
756  /// \brief What kind of records we are reading.
757  ReadingKind ReadingKind;
758
759  /// \brief RAII object to change the reading kind.
760  class ReadingKindTracker {
761    ASTReader &Reader;
762    enum ReadingKind PrevKind;
763
764    ReadingKindTracker(const ReadingKindTracker &) LLVM_DELETED_FUNCTION;
765    void operator=(const ReadingKindTracker &) LLVM_DELETED_FUNCTION;
766
767  public:
768    ReadingKindTracker(enum ReadingKind newKind, ASTReader &reader)
769      : Reader(reader), PrevKind(Reader.ReadingKind) {
770      Reader.ReadingKind = newKind;
771    }
772
773    ~ReadingKindTracker() { Reader.ReadingKind = PrevKind; }
774  };
775
776  /// \brief All predefines buffers in the chain, to be treated as if
777  /// concatenated.
778  PCHPredefinesBlocks PCHPredefinesBuffers;
779
780  /// \brief Suggested contents of the predefines buffer, after this
781  /// PCH file has been processed.
782  ///
783  /// In most cases, this string will be empty, because the predefines
784  /// buffer computed to build the PCH file will be identical to the
785  /// predefines buffer computed from the command line. However, when
786  /// there are differences that the PCH reader can work around, this
787  /// predefines buffer may contain additional definitions.
788  std::string SuggestedPredefines;
789
790  /// \brief Reads a statement from the specified cursor.
791  Stmt *ReadStmtFromStream(ModuleFile &F);
792
793  /// \brief Get a FileEntry out of stored-in-PCH filename, making sure we take
794  /// into account all the necessary relocations.
795  const FileEntry *getFileEntry(StringRef filename);
796
797  void MaybeAddSystemRootToFilename(std::string &Filename);
798
799  ASTReadResult ReadASTCore(StringRef FileName, ModuleKind Type,
800                            ModuleFile *ImportedBy);
801  ASTReadResult ReadASTBlock(ModuleFile &F);
802  bool CheckPredefinesBuffers();
803  bool ParseLineTable(ModuleFile &F, SmallVectorImpl<uint64_t> &Record);
804  ASTReadResult ReadSourceManagerBlock(ModuleFile &F);
805  ASTReadResult ReadSLocEntryRecord(int ID);
806  llvm::BitstreamCursor &SLocCursorForID(int ID);
807  SourceLocation getImportLocation(ModuleFile *F);
808  ASTReadResult ReadSubmoduleBlock(ModuleFile &F);
809  bool ParseLanguageOptions(const RecordData &Record);
810
811  struct RecordLocation {
812    RecordLocation(ModuleFile *M, uint64_t O)
813      : F(M), Offset(O) {}
814    ModuleFile *F;
815    uint64_t Offset;
816  };
817
818  QualType readTypeRecord(unsigned Index);
819  RecordLocation TypeCursorForIndex(unsigned Index);
820  void LoadedDecl(unsigned Index, Decl *D);
821  Decl *ReadDeclRecord(serialization::DeclID ID);
822  RecordLocation DeclCursorForID(serialization::DeclID ID,
823                                 unsigned &RawLocation);
824  void loadDeclUpdateRecords(serialization::DeclID ID, Decl *D);
825  void loadPendingDeclChain(serialization::GlobalDeclID ID);
826  void loadObjCCategories(serialization::GlobalDeclID ID, ObjCInterfaceDecl *D,
827                          unsigned PreviousGeneration = 0);
828
829  RecordLocation getLocalBitOffset(uint64_t GlobalOffset);
830  uint64_t getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset);
831
832  /// \brief Returns the first preprocessed entity ID that ends after BLoc.
833  serialization::PreprocessedEntityID
834    findBeginPreprocessedEntity(SourceLocation BLoc) const;
835
836  /// \brief Returns the first preprocessed entity ID that begins after ELoc.
837  serialization::PreprocessedEntityID
838    findEndPreprocessedEntity(SourceLocation ELoc) const;
839
840  /// \brief Find the next module that contains entities and return the ID
841  /// of the first entry.
842  ///
843  /// \param SLocMapI points at a chunk of a module that contains no
844  /// preprocessed entities or the entities it contains are not the
845  /// ones we are looking for.
846  serialization::PreprocessedEntityID
847    findNextPreprocessedEntity(
848                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const;
849
850  /// \brief Returns (ModuleFile, Local index) pair for \p GlobalIndex of a
851  /// preprocessed entity.
852  std::pair<ModuleFile *, unsigned>
853    getModulePreprocessedEntity(unsigned GlobalIndex);
854
855  /// \brief Returns (begin, end) pair for the preprocessed entities of a
856  /// particular module.
857  std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
858    getModulePreprocessedEntities(ModuleFile &Mod) const;
859
860  class ModuleDeclIterator {
861    ASTReader *Reader;
862    ModuleFile *Mod;
863    const serialization::LocalDeclID *Pos;
864
865  public:
866    typedef const Decl *value_type;
867    typedef value_type&         reference;
868    typedef value_type*         pointer;
869
870    ModuleDeclIterator() : Reader(0), Mod(0), Pos(0) { }
871
872    ModuleDeclIterator(ASTReader *Reader, ModuleFile *Mod,
873                       const serialization::LocalDeclID *Pos)
874      : Reader(Reader), Mod(Mod), Pos(Pos) { }
875
876    value_type operator*() const {
877      return Reader->GetDecl(Reader->getGlobalDeclID(*Mod, *Pos));
878    }
879
880    ModuleDeclIterator &operator++() {
881      ++Pos;
882      return *this;
883    }
884
885    ModuleDeclIterator operator++(int) {
886      ModuleDeclIterator Prev(*this);
887      ++Pos;
888      return Prev;
889    }
890
891    ModuleDeclIterator &operator--() {
892      --Pos;
893      return *this;
894    }
895
896    ModuleDeclIterator operator--(int) {
897      ModuleDeclIterator Prev(*this);
898      --Pos;
899      return Prev;
900    }
901
902    friend bool operator==(const ModuleDeclIterator &LHS,
903                           const ModuleDeclIterator &RHS) {
904      assert(LHS.Reader == RHS.Reader && LHS.Mod == RHS.Mod);
905      return LHS.Pos == RHS.Pos;
906    }
907
908    friend bool operator!=(const ModuleDeclIterator &LHS,
909                           const ModuleDeclIterator &RHS) {
910      assert(LHS.Reader == RHS.Reader && LHS.Mod == RHS.Mod);
911      return LHS.Pos != RHS.Pos;
912    }
913  };
914
915  std::pair<ModuleDeclIterator, ModuleDeclIterator>
916    getModuleFileLevelDecls(ModuleFile &Mod);
917
918  void PassInterestingDeclsToConsumer();
919  void PassInterestingDeclToConsumer(Decl *D);
920
921  void finishPendingActions();
922
923  /// \brief Produce an error diagnostic and return true.
924  ///
925  /// This routine should only be used for fatal errors that have to
926  /// do with non-routine failures (e.g., corrupted AST file).
927  void Error(StringRef Msg);
928  void Error(unsigned DiagID, StringRef Arg1 = StringRef(),
929             StringRef Arg2 = StringRef());
930
931  ASTReader(const ASTReader &) LLVM_DELETED_FUNCTION;
932  void operator=(const ASTReader &) LLVM_DELETED_FUNCTION;
933public:
934  /// \brief Load the AST file and validate its contents against the given
935  /// Preprocessor.
936  ///
937  /// \param PP the preprocessor associated with the context in which this
938  /// precompiled header will be loaded.
939  ///
940  /// \param Context the AST context that this precompiled header will be
941  /// loaded into.
942  ///
943  /// \param isysroot If non-NULL, the system include path specified by the
944  /// user. This is only used with relocatable PCH files. If non-NULL,
945  /// a relocatable PCH file will use the default path "/".
946  ///
947  /// \param DisableValidation If true, the AST reader will suppress most
948  /// of its regular consistency checking, allowing the use of precompiled
949  /// headers that cannot be determined to be compatible.
950  ///
951  /// \param DisableStatCache If true, the AST reader will ignore the
952  /// stat cache in the AST files. This performance pessimization can
953  /// help when an AST file is being used in cases where the
954  /// underlying files in the file system may have changed, but
955  /// parsing should still continue.
956  ///
957  /// \param AllowASTWithCompilerErrors If true, the AST reader will accept an
958  /// AST file the was created out of an AST with compiler errors,
959  /// otherwise it will reject it.
960  ASTReader(Preprocessor &PP, ASTContext &Context, StringRef isysroot = "",
961            bool DisableValidation = false, bool DisableStatCache = false,
962            bool AllowASTWithCompilerErrors = false);
963
964  ~ASTReader();
965
966  SourceManager &getSourceManager() const { return SourceMgr; }
967
968  /// \brief Load the AST file designated by the given file name.
969  ASTReadResult ReadAST(const std::string &FileName, ModuleKind Type);
970
971  /// \brief Checks that no file that is stored in PCH is out-of-sync with
972  /// the actual file in the file system.
973  ASTReadResult validateFileEntries(ModuleFile &M);
974
975  /// \brief Make the entities in the given module and any of its (non-explicit)
976  /// submodules visible to name lookup.
977  ///
978  /// \param Mod The module whose names should be made visible.
979  ///
980  /// \param NameVisibility The level of visibility to give the names in the
981  /// module.  Visibility can only be increased over time.
982  void makeModuleVisible(Module *Mod,
983                         Module::NameVisibilityKind NameVisibility);
984
985  /// \brief Make the names within this set of hidden names visible.
986  void makeNamesVisible(const HiddenNames &Names);
987
988  /// \brief Set the AST callbacks listener.
989  void setListener(ASTReaderListener *listener) {
990    Listener.reset(listener);
991  }
992
993  /// \brief Set the AST deserialization listener.
994  void setDeserializationListener(ASTDeserializationListener *Listener);
995
996  /// \brief Initializes the ASTContext
997  void InitializeContext();
998
999  /// \brief Add in-memory (virtual file) buffer.
1000  void addInMemoryBuffer(StringRef &FileName, llvm::MemoryBuffer *Buffer) {
1001    ModuleMgr.addInMemoryBuffer(FileName, Buffer);
1002  }
1003
1004  /// \brief Finalizes the AST reader's state before writing an AST file to
1005  /// disk.
1006  ///
1007  /// This operation may undo temporary state in the AST that should not be
1008  /// emitted.
1009  void finalizeForWriting();
1010
1011  /// \brief Retrieve the module manager.
1012  ModuleManager &getModuleManager() { return ModuleMgr; }
1013
1014  /// \brief Retrieve the preprocessor.
1015  Preprocessor &getPreprocessor() const { return PP; }
1016
1017  /// \brief Retrieve the name of the original source file name
1018  const std::string &getOriginalSourceFile() { return OriginalFileName; }
1019
1020  /// \brief Retrieve the name of the original source file name directly from
1021  /// the AST file, without actually loading the AST file.
1022  static std::string getOriginalSourceFile(const std::string &ASTFileName,
1023                                           FileManager &FileMgr,
1024                                           DiagnosticsEngine &Diags);
1025
1026  /// \brief Returns the suggested contents of the predefines buffer,
1027  /// which contains a (typically-empty) subset of the predefines
1028  /// build prior to including the precompiled header.
1029  const std::string &getSuggestedPredefines() { return SuggestedPredefines; }
1030
1031  /// \brief Read a preallocated preprocessed entity from the external source.
1032  ///
1033  /// \returns null if an error occurred that prevented the preprocessed
1034  /// entity from being loaded.
1035  virtual PreprocessedEntity *ReadPreprocessedEntity(unsigned Index);
1036
1037  /// \brief Returns a pair of [Begin, End) indices of preallocated
1038  /// preprocessed entities that \p Range encompasses.
1039  virtual std::pair<unsigned, unsigned>
1040      findPreprocessedEntitiesInRange(SourceRange Range);
1041
1042  /// \brief Optionally returns true or false if the preallocated preprocessed
1043  /// entity with index \p Index came from file \p FID.
1044  virtual llvm::Optional<bool> isPreprocessedEntityInFileID(unsigned Index,
1045                                                            FileID FID);
1046
1047  /// \brief Read the header file information for the given file entry.
1048  virtual HeaderFileInfo GetHeaderFileInfo(const FileEntry *FE);
1049
1050  void ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag);
1051
1052  /// \brief Returns the number of source locations found in the chain.
1053  unsigned getTotalNumSLocs() const {
1054    return TotalNumSLocEntries;
1055  }
1056
1057  /// \brief Returns the number of identifiers found in the chain.
1058  unsigned getTotalNumIdentifiers() const {
1059    return static_cast<unsigned>(IdentifiersLoaded.size());
1060  }
1061
1062  /// \brief Returns the number of types found in the chain.
1063  unsigned getTotalNumTypes() const {
1064    return static_cast<unsigned>(TypesLoaded.size());
1065  }
1066
1067  /// \brief Returns the number of declarations found in the chain.
1068  unsigned getTotalNumDecls() const {
1069    return static_cast<unsigned>(DeclsLoaded.size());
1070  }
1071
1072  /// \brief Returns the number of submodules known.
1073  unsigned getTotalNumSubmodules() const {
1074    return static_cast<unsigned>(SubmodulesLoaded.size());
1075  }
1076
1077  /// \brief Returns the number of selectors found in the chain.
1078  unsigned getTotalNumSelectors() const {
1079    return static_cast<unsigned>(SelectorsLoaded.size());
1080  }
1081
1082  /// \brief Returns the number of preprocessed entities known to the AST
1083  /// reader.
1084  unsigned getTotalNumPreprocessedEntities() const {
1085    unsigned Result = 0;
1086    for (ModuleConstIterator I = ModuleMgr.begin(),
1087        E = ModuleMgr.end(); I != E; ++I) {
1088      Result += (*I)->NumPreprocessedEntities;
1089    }
1090
1091    return Result;
1092  }
1093
1094  /// \brief Returns the number of C++ base specifiers found in the chain.
1095  unsigned getTotalNumCXXBaseSpecifiers() const {
1096    return NumCXXBaseSpecifiersLoaded;
1097  }
1098
1099  /// \brief Reads a TemplateArgumentLocInfo appropriate for the
1100  /// given TemplateArgument kind.
1101  TemplateArgumentLocInfo
1102  GetTemplateArgumentLocInfo(ModuleFile &F, TemplateArgument::ArgKind Kind,
1103                             const RecordData &Record, unsigned &Idx);
1104
1105  /// \brief Reads a TemplateArgumentLoc.
1106  TemplateArgumentLoc
1107  ReadTemplateArgumentLoc(ModuleFile &F,
1108                          const RecordData &Record, unsigned &Idx);
1109
1110  /// \brief Reads a declarator info from the given record.
1111  TypeSourceInfo *GetTypeSourceInfo(ModuleFile &F,
1112                                    const RecordData &Record, unsigned &Idx);
1113
1114  /// \brief Resolve a type ID into a type, potentially building a new
1115  /// type.
1116  QualType GetType(serialization::TypeID ID);
1117
1118  /// \brief Resolve a local type ID within a given AST file into a type.
1119  QualType getLocalType(ModuleFile &F, unsigned LocalID);
1120
1121  /// \brief Map a local type ID within a given AST file into a global type ID.
1122  serialization::TypeID getGlobalTypeID(ModuleFile &F, unsigned LocalID) const;
1123
1124  /// \brief Read a type from the current position in the given record, which
1125  /// was read from the given AST file.
1126  QualType readType(ModuleFile &F, const RecordData &Record, unsigned &Idx) {
1127    if (Idx >= Record.size())
1128      return QualType();
1129
1130    return getLocalType(F, Record[Idx++]);
1131  }
1132
1133  /// \brief Map from a local declaration ID within a given module to a
1134  /// global declaration ID.
1135  serialization::DeclID getGlobalDeclID(ModuleFile &F,
1136                                      serialization::LocalDeclID LocalID) const;
1137
1138  /// \brief Returns true if global DeclID \p ID originated from module \p M.
1139  bool isDeclIDFromModule(serialization::GlobalDeclID ID, ModuleFile &M) const;
1140
1141  /// \brief Retrieve the module file that owns the given declaration, or NULL
1142  /// if the declaration is not from a module file.
1143  ModuleFile *getOwningModuleFile(Decl *D);
1144
1145  /// \brief Returns the source location for the decl \p ID.
1146  SourceLocation getSourceLocationForDeclID(serialization::GlobalDeclID ID);
1147
1148  /// \brief Resolve a declaration ID into a declaration, potentially
1149  /// building a new declaration.
1150  Decl *GetDecl(serialization::DeclID ID);
1151  virtual Decl *GetExternalDecl(uint32_t ID);
1152
1153  /// \brief Reads a declaration with the given local ID in the given module.
1154  Decl *GetLocalDecl(ModuleFile &F, uint32_t LocalID) {
1155    return GetDecl(getGlobalDeclID(F, LocalID));
1156  }
1157
1158  /// \brief Reads a declaration with the given local ID in the given module.
1159  ///
1160  /// \returns The requested declaration, casted to the given return type.
1161  template<typename T>
1162  T *GetLocalDeclAs(ModuleFile &F, uint32_t LocalID) {
1163    return cast_or_null<T>(GetLocalDecl(F, LocalID));
1164  }
1165
1166  /// \brief Map a global declaration ID into the declaration ID used to
1167  /// refer to this declaration within the given module fule.
1168  ///
1169  /// \returns the global ID of the given declaration as known in the given
1170  /// module file.
1171  serialization::DeclID
1172  mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
1173                                  serialization::DeclID GlobalID);
1174
1175  /// \brief Reads a declaration ID from the given position in a record in the
1176  /// given module.
1177  ///
1178  /// \returns The declaration ID read from the record, adjusted to a global ID.
1179  serialization::DeclID ReadDeclID(ModuleFile &F, const RecordData &Record,
1180                                   unsigned &Idx);
1181
1182  /// \brief Reads a declaration from the given position in a record in the
1183  /// given module.
1184  Decl *ReadDecl(ModuleFile &F, const RecordData &R, unsigned &I) {
1185    return GetDecl(ReadDeclID(F, R, I));
1186  }
1187
1188  /// \brief Reads a declaration from the given position in a record in the
1189  /// given module.
1190  ///
1191  /// \returns The declaration read from this location, casted to the given
1192  /// result type.
1193  template<typename T>
1194  T *ReadDeclAs(ModuleFile &F, const RecordData &R, unsigned &I) {
1195    return cast_or_null<T>(GetDecl(ReadDeclID(F, R, I)));
1196  }
1197
1198  /// \brief Read a CXXBaseSpecifiers ID form the given record and
1199  /// return its global bit offset.
1200  uint64_t readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
1201                                 unsigned &Idx);
1202
1203  virtual CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset);
1204
1205  /// \brief Resolve the offset of a statement into a statement.
1206  ///
1207  /// This operation will read a new statement from the external
1208  /// source each time it is called, and is meant to be used via a
1209  /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
1210  virtual Stmt *GetExternalDeclStmt(uint64_t Offset);
1211
1212  /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1213  /// specified cursor.  Read the abbreviations that are at the top of the block
1214  /// and then leave the cursor pointing into the block.
1215  bool ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor, unsigned BlockID);
1216
1217  /// \brief Finds all the visible declarations with a given name.
1218  /// The current implementation of this method just loads the entire
1219  /// lookup table as unmaterialized references.
1220  virtual DeclContext::lookup_result
1221  FindExternalVisibleDeclsByName(const DeclContext *DC,
1222                                 DeclarationName Name);
1223
1224  /// \brief Read all of the declarations lexically stored in a
1225  /// declaration context.
1226  ///
1227  /// \param DC The declaration context whose declarations will be
1228  /// read.
1229  ///
1230  /// \param Decls Vector that will contain the declarations loaded
1231  /// from the external source. The caller is responsible for merging
1232  /// these declarations with any declarations already stored in the
1233  /// declaration context.
1234  ///
1235  /// \returns true if there was an error while reading the
1236  /// declarations for this declaration context.
1237  virtual ExternalLoadResult FindExternalLexicalDecls(const DeclContext *DC,
1238                                        bool (*isKindWeWant)(Decl::Kind),
1239                                        SmallVectorImpl<Decl*> &Decls);
1240
1241  /// \brief Get the decls that are contained in a file in the Offset/Length
1242  /// range. \p Length can be 0 to indicate a point at \p Offset instead of
1243  /// a range.
1244  virtual void FindFileRegionDecls(FileID File, unsigned Offset,unsigned Length,
1245                                   SmallVectorImpl<Decl *> &Decls);
1246
1247  /// \brief Notify ASTReader that we started deserialization of
1248  /// a decl or type so until FinishedDeserializing is called there may be
1249  /// decls that are initializing. Must be paired with FinishedDeserializing.
1250  virtual void StartedDeserializing() { ++NumCurrentElementsDeserializing; }
1251
1252  /// \brief Notify ASTReader that we finished the deserialization of
1253  /// a decl or type. Must be paired with StartedDeserializing.
1254  virtual void FinishedDeserializing();
1255
1256  /// \brief Function that will be invoked when we begin parsing a new
1257  /// translation unit involving this external AST source.
1258  ///
1259  /// This function will provide all of the external definitions to
1260  /// the ASTConsumer.
1261  virtual void StartTranslationUnit(ASTConsumer *Consumer);
1262
1263  /// \brief Print some statistics about AST usage.
1264  virtual void PrintStats();
1265
1266  /// \brief Dump information about the AST reader to standard error.
1267  void dump();
1268
1269  /// Return the amount of memory used by memory buffers, breaking down
1270  /// by heap-backed versus mmap'ed memory.
1271  virtual void getMemoryBufferSizes(MemoryBufferSizes &sizes) const;
1272
1273  /// \brief Initialize the semantic source with the Sema instance
1274  /// being used to perform semantic analysis on the abstract syntax
1275  /// tree.
1276  virtual void InitializeSema(Sema &S);
1277
1278  /// \brief Inform the semantic consumer that Sema is no longer available.
1279  virtual void ForgetSema() { SemaObj = 0; }
1280
1281  /// \brief Retrieve the IdentifierInfo for the named identifier.
1282  ///
1283  /// This routine builds a new IdentifierInfo for the given identifier. If any
1284  /// declarations with this name are visible from translation unit scope, their
1285  /// declarations will be deserialized and introduced into the declaration
1286  /// chain of the identifier.
1287  virtual IdentifierInfo *get(const char *NameStart, const char *NameEnd);
1288  IdentifierInfo *get(StringRef Name) {
1289    return get(Name.begin(), Name.end());
1290  }
1291
1292  /// \brief Retrieve an iterator into the set of all identifiers
1293  /// in all loaded AST files.
1294  virtual IdentifierIterator *getIdentifiers() const;
1295
1296  /// \brief Load the contents of the global method pool for a given
1297  /// selector.
1298  virtual void ReadMethodPool(Selector Sel);
1299
1300  /// \brief Load the set of namespaces that are known to the external source,
1301  /// which will be used during typo correction.
1302  virtual void ReadKnownNamespaces(
1303                           SmallVectorImpl<NamespaceDecl *> &Namespaces);
1304
1305  virtual void ReadTentativeDefinitions(
1306                 SmallVectorImpl<VarDecl *> &TentativeDefs);
1307
1308  virtual void ReadUnusedFileScopedDecls(
1309                 SmallVectorImpl<const DeclaratorDecl *> &Decls);
1310
1311  virtual void ReadDelegatingConstructors(
1312                 SmallVectorImpl<CXXConstructorDecl *> &Decls);
1313
1314  virtual void ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls);
1315
1316  virtual void ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls);
1317
1318  virtual void ReadLocallyScopedExternalDecls(
1319                 SmallVectorImpl<NamedDecl *> &Decls);
1320
1321  virtual void ReadReferencedSelectors(
1322                 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels);
1323
1324  virtual void ReadWeakUndeclaredIdentifiers(
1325                 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WI);
1326
1327  virtual void ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables);
1328
1329  virtual void ReadPendingInstantiations(
1330                 SmallVectorImpl<std::pair<ValueDecl *,
1331                                           SourceLocation> > &Pending);
1332
1333  /// \brief Load a selector from disk, registering its ID if it exists.
1334  void LoadSelector(Selector Sel);
1335
1336  void SetIdentifierInfo(unsigned ID, IdentifierInfo *II);
1337  void SetGloballyVisibleDecls(IdentifierInfo *II,
1338                               const SmallVectorImpl<uint32_t> &DeclIDs,
1339                               bool Nonrecursive = false);
1340
1341  /// \brief Report a diagnostic.
1342  DiagnosticBuilder Diag(unsigned DiagID);
1343
1344  /// \brief Report a diagnostic.
1345  DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID);
1346
1347  IdentifierInfo *DecodeIdentifierInfo(serialization::IdentifierID ID);
1348
1349  IdentifierInfo *GetIdentifierInfo(ModuleFile &M, const RecordData &Record,
1350                                    unsigned &Idx) {
1351    return DecodeIdentifierInfo(getGlobalIdentifierID(M, Record[Idx++]));
1352  }
1353
1354  virtual IdentifierInfo *GetIdentifier(serialization::IdentifierID ID) {
1355    return DecodeIdentifierInfo(ID);
1356  }
1357
1358  IdentifierInfo *getLocalIdentifier(ModuleFile &M, unsigned LocalID);
1359
1360  serialization::IdentifierID getGlobalIdentifierID(ModuleFile &M,
1361                                                    unsigned LocalID);
1362
1363  /// \brief Read the source location entry with index ID.
1364  virtual bool ReadSLocEntry(int ID);
1365
1366  /// \brief Retrieve the global submodule ID given a module and its local ID
1367  /// number.
1368  serialization::SubmoduleID
1369  getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID);
1370
1371  /// \brief Retrieve the submodule that corresponds to a global submodule ID.
1372  ///
1373  Module *getSubmodule(serialization::SubmoduleID GlobalID);
1374
1375  /// \brief Retrieve a selector from the given module with its local ID
1376  /// number.
1377  Selector getLocalSelector(ModuleFile &M, unsigned LocalID);
1378
1379  Selector DecodeSelector(serialization::SelectorID Idx);
1380
1381  virtual Selector GetExternalSelector(serialization::SelectorID ID);
1382  uint32_t GetNumExternalSelectors();
1383
1384  Selector ReadSelector(ModuleFile &M, const RecordData &Record, unsigned &Idx) {
1385    return getLocalSelector(M, Record[Idx++]);
1386  }
1387
1388  /// \brief Retrieve the global selector ID that corresponds to this
1389  /// the local selector ID in a given module.
1390  serialization::SelectorID getGlobalSelectorID(ModuleFile &F,
1391                                                unsigned LocalID) const;
1392
1393  /// \brief Read a declaration name.
1394  DeclarationName ReadDeclarationName(ModuleFile &F,
1395                                      const RecordData &Record, unsigned &Idx);
1396  void ReadDeclarationNameLoc(ModuleFile &F,
1397                              DeclarationNameLoc &DNLoc, DeclarationName Name,
1398                              const RecordData &Record, unsigned &Idx);
1399  void ReadDeclarationNameInfo(ModuleFile &F, DeclarationNameInfo &NameInfo,
1400                               const RecordData &Record, unsigned &Idx);
1401
1402  void ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
1403                         const RecordData &Record, unsigned &Idx);
1404
1405  NestedNameSpecifier *ReadNestedNameSpecifier(ModuleFile &F,
1406                                               const RecordData &Record,
1407                                               unsigned &Idx);
1408
1409  NestedNameSpecifierLoc ReadNestedNameSpecifierLoc(ModuleFile &F,
1410                                                    const RecordData &Record,
1411                                                    unsigned &Idx);
1412
1413  /// \brief Read a template name.
1414  TemplateName ReadTemplateName(ModuleFile &F, const RecordData &Record,
1415                                unsigned &Idx);
1416
1417  /// \brief Read a template argument.
1418  TemplateArgument ReadTemplateArgument(ModuleFile &F,
1419                                        const RecordData &Record,unsigned &Idx);
1420
1421  /// \brief Read a template parameter list.
1422  TemplateParameterList *ReadTemplateParameterList(ModuleFile &F,
1423                                                   const RecordData &Record,
1424                                                   unsigned &Idx);
1425
1426  /// \brief Read a template argument array.
1427  void
1428  ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
1429                           ModuleFile &F, const RecordData &Record,
1430                           unsigned &Idx);
1431
1432  /// \brief Read a UnresolvedSet structure.
1433  void ReadUnresolvedSet(ModuleFile &F, UnresolvedSetImpl &Set,
1434                         const RecordData &Record, unsigned &Idx);
1435
1436  /// \brief Read a C++ base specifier.
1437  CXXBaseSpecifier ReadCXXBaseSpecifier(ModuleFile &F,
1438                                        const RecordData &Record,unsigned &Idx);
1439
1440  /// \brief Read a CXXCtorInitializer array.
1441  std::pair<CXXCtorInitializer **, unsigned>
1442  ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
1443                          unsigned &Idx);
1444
1445  /// \brief Read a source location from raw form.
1446  SourceLocation ReadSourceLocation(ModuleFile &ModuleFile, unsigned Raw) const {
1447    SourceLocation Loc = SourceLocation::getFromRawEncoding(Raw);
1448    assert(ModuleFile.SLocRemap.find(Loc.getOffset()) != ModuleFile.SLocRemap.end() &&
1449           "Cannot find offset to remap.");
1450    int Remap = ModuleFile.SLocRemap.find(Loc.getOffset())->second;
1451    return Loc.getLocWithOffset(Remap);
1452  }
1453
1454  /// \brief Read a source location.
1455  SourceLocation ReadSourceLocation(ModuleFile &ModuleFile,
1456                                    const RecordData &Record, unsigned& Idx) {
1457    return ReadSourceLocation(ModuleFile, Record[Idx++]);
1458  }
1459
1460  /// \brief Read a source range.
1461  SourceRange ReadSourceRange(ModuleFile &F,
1462                              const RecordData &Record, unsigned& Idx);
1463
1464  /// \brief Read an integral value
1465  llvm::APInt ReadAPInt(const RecordData &Record, unsigned &Idx);
1466
1467  /// \brief Read a signed integral value
1468  llvm::APSInt ReadAPSInt(const RecordData &Record, unsigned &Idx);
1469
1470  /// \brief Read a floating-point value
1471  llvm::APFloat ReadAPFloat(const RecordData &Record, unsigned &Idx);
1472
1473  // \brief Read a string
1474  std::string ReadString(const RecordData &Record, unsigned &Idx);
1475
1476  /// \brief Read a version tuple.
1477  VersionTuple ReadVersionTuple(const RecordData &Record, unsigned &Idx);
1478
1479  CXXTemporary *ReadCXXTemporary(ModuleFile &F, const RecordData &Record,
1480                                 unsigned &Idx);
1481
1482  /// \brief Reads attributes from the current stream position.
1483  void ReadAttributes(ModuleFile &F, AttrVec &Attrs,
1484                      const RecordData &Record, unsigned &Idx);
1485
1486  /// \brief Reads a statement.
1487  Stmt *ReadStmt(ModuleFile &F);
1488
1489  /// \brief Reads an expression.
1490  Expr *ReadExpr(ModuleFile &F);
1491
1492  /// \brief Reads a sub-statement operand during statement reading.
1493  Stmt *ReadSubStmt() {
1494    assert(ReadingKind == Read_Stmt &&
1495           "Should be called only during statement reading!");
1496    // Subexpressions are stored from last to first, so the next Stmt we need
1497    // is at the back of the stack.
1498    assert(!StmtStack.empty() && "Read too many sub statements!");
1499    return StmtStack.pop_back_val();
1500  }
1501
1502  /// \brief Reads a sub-expression operand during statement reading.
1503  Expr *ReadSubExpr();
1504
1505  /// \brief Reads the macro record located at the given offset.
1506  void ReadMacroRecord(ModuleFile &F, uint64_t Offset);
1507
1508  /// \brief Determine the global preprocessed entity ID that corresponds to
1509  /// the given local ID within the given module.
1510  serialization::PreprocessedEntityID
1511  getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const;
1512
1513  /// \brief Note that the identifier is a macro whose record will be loaded
1514  /// from the given AST file at the given (file-local) offset.
1515  ///
1516  /// \param II The name of the macro.
1517  ///
1518  /// \param F The module file from which the macro definition was deserialized.
1519  ///
1520  /// \param Offset The offset into the module file at which the macro
1521  /// definition is located.
1522  ///
1523  /// \param Visible Whether the macro should be made visible.
1524  void setIdentifierIsMacro(IdentifierInfo *II, ModuleFile &F,
1525                            uint64_t Offset, bool Visible);
1526
1527  /// \brief Read the set of macros defined by this external macro source.
1528  virtual void ReadDefinedMacros();
1529
1530  /// \brief Read the macro definition for this identifier.
1531  virtual void LoadMacroDefinition(IdentifierInfo *II);
1532
1533  /// \brief Update an out-of-date identifier.
1534  virtual void updateOutOfDateIdentifier(IdentifierInfo &II);
1535
1536  /// \brief Note that this identifier is up-to-date.
1537  void markIdentifierUpToDate(IdentifierInfo *II);
1538
1539  /// \brief Read the macro definition corresponding to this iterator
1540  /// into the unread macro record offsets table.
1541  void LoadMacroDefinition(
1542                     llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos);
1543
1544  /// \brief Load all external visible decls in the given DeclContext.
1545  void completeVisibleDeclsMap(const DeclContext *DC);
1546
1547  /// \brief Retrieve the AST context that this AST reader supplements.
1548  ASTContext &getContext() { return Context; }
1549
1550  // \brief Contains declarations that were loaded before we have
1551  // access to a Sema object.
1552  SmallVector<NamedDecl *, 16> PreloadedDecls;
1553
1554  /// \brief Retrieve the semantic analysis object used to analyze the
1555  /// translation unit in which the precompiled header is being
1556  /// imported.
1557  Sema *getSema() { return SemaObj; }
1558
1559  /// \brief Retrieve the identifier table associated with the
1560  /// preprocessor.
1561  IdentifierTable &getIdentifierTable();
1562
1563  /// \brief Record that the given ID maps to the given switch-case
1564  /// statement.
1565  void RecordSwitchCaseID(SwitchCase *SC, unsigned ID);
1566
1567  /// \brief Retrieve the switch-case statement with the given ID.
1568  SwitchCase *getSwitchCaseWithID(unsigned ID);
1569
1570  void ClearSwitchCaseIDs();
1571
1572  /// \brief Cursors for comments blocks.
1573  SmallVector<std::pair<llvm::BitstreamCursor,
1574                        serialization::ModuleFile *>, 8> CommentsCursors;
1575
1576  /// \brief Loads comments ranges.
1577  void ReadComments();
1578};
1579
1580/// \brief Helper class that saves the current stream position and
1581/// then restores it when destroyed.
1582struct SavedStreamPosition {
1583  explicit SavedStreamPosition(llvm::BitstreamCursor &Cursor)
1584  : Cursor(Cursor), Offset(Cursor.GetCurrentBitNo()) { }
1585
1586  ~SavedStreamPosition() {
1587    Cursor.JumpToBit(Offset);
1588  }
1589
1590private:
1591  llvm::BitstreamCursor &Cursor;
1592  uint64_t Offset;
1593};
1594
1595inline void PCHValidator::Error(const char *Msg) {
1596  Reader.Error(Msg);
1597}
1598
1599} // end namespace clang
1600
1601#endif
1602