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