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