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