ASTWriter.h revision 58a2cd8c0d52e710cbcc57a67eac7b51b0b831c4
1//===--- ASTWriter.h - AST File Writer --------------------------*- 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 ASTWriter class, which writes an AST file
11//  containing a serialized representation of a translation unit.
12//
13//===----------------------------------------------------------------------===//
14#ifndef LLVM_CLANG_FRONTEND_AST_WRITER_H
15#define LLVM_CLANG_FRONTEND_AST_WRITER_H
16
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclarationName.h"
19#include "clang/AST/TemplateBase.h"
20#include "clang/AST/ASTMutationListener.h"
21#include "clang/Serialization/ASTBitCodes.h"
22#include "clang/Serialization/ASTDeserializationListener.h"
23#include "clang/Sema/SemaConsumer.h"
24#include "llvm/ADT/SmallPtrSet.h"
25#include "llvm/ADT/SmallVector.h"
26#include "llvm/ADT/DenseMap.h"
27#include "llvm/Bitcode/BitstreamWriter.h"
28#include <map>
29#include <queue>
30#include <vector>
31
32namespace llvm {
33  class APFloat;
34  class APInt;
35  class BitstreamWriter;
36}
37
38namespace clang {
39
40class ASTContext;
41class ASTSerializationListener;
42class NestedNameSpecifier;
43class CXXBaseSpecifier;
44class CXXCtorInitializer;
45class FPOptions;
46class HeaderSearch;
47class MacroDefinition;
48class MemorizeStatCalls;
49class OpaqueValueExpr;
50class OpenCLOptions;
51class ASTReader;
52class PreprocessedEntity;
53class PreprocessingRecord;
54class Preprocessor;
55class Sema;
56class SourceManager;
57class SwitchCase;
58class TargetInfo;
59class VersionTuple;
60
61/// \brief Writes an AST file containing the contents of a translation unit.
62///
63/// The ASTWriter class produces a bitstream containing the serialized
64/// representation of a given abstract syntax tree and its supporting
65/// data structures. This bitstream can be de-serialized via an
66/// instance of the ASTReader class.
67class ASTWriter : public ASTDeserializationListener,
68                  public ASTMutationListener {
69public:
70  typedef llvm::SmallVector<uint64_t, 64> RecordData;
71  typedef llvm::SmallVectorImpl<uint64_t> RecordDataImpl;
72
73  friend class ASTDeclWriter;
74private:
75  /// \brief The bitstream writer used to emit this precompiled header.
76  llvm::BitstreamWriter &Stream;
77
78  /// \brief The reader of existing AST files, if we're chaining.
79  ASTReader *Chain;
80
81  /// \brief A listener object that receives notifications when certain
82  /// entities are serialized.
83  ASTSerializationListener *SerializationListener;
84
85  /// \brief Stores a declaration or a type to be written to the AST file.
86  class DeclOrType {
87  public:
88    DeclOrType(Decl *D) : Stored(D), IsType(false) { }
89    DeclOrType(QualType T) : Stored(T.getAsOpaquePtr()), IsType(true) { }
90
91    bool isType() const { return IsType; }
92    bool isDecl() const { return !IsType; }
93
94    QualType getType() const {
95      assert(isType() && "Not a type!");
96      return QualType::getFromOpaquePtr(Stored);
97    }
98
99    Decl *getDecl() const {
100      assert(isDecl() && "Not a decl!");
101      return static_cast<Decl *>(Stored);
102    }
103
104  private:
105    void *Stored;
106    bool IsType;
107  };
108
109  /// \brief The declarations and types to emit.
110  std::queue<DeclOrType> DeclTypesToEmit;
111
112  /// \brief The first ID number we can use for our own declarations.
113  serialization::DeclID FirstDeclID;
114
115  /// \brief The decl ID that will be assigned to the next new decl.
116  serialization::DeclID NextDeclID;
117
118  /// \brief Map that provides the ID numbers of each declaration within
119  /// the output stream, as well as those deserialized from a chained PCH.
120  ///
121  /// The ID numbers of declarations are consecutive (in order of
122  /// discovery) and start at 2. 1 is reserved for the translation
123  /// unit, while 0 is reserved for NULL.
124  llvm::DenseMap<const Decl *, serialization::DeclID> DeclIDs;
125
126  /// \brief Offset of each declaration in the bitstream, indexed by
127  /// the declaration's ID.
128  std::vector<uint32_t> DeclOffsets;
129
130  /// \brief The first ID number we can use for our own types.
131  serialization::TypeID FirstTypeID;
132
133  /// \brief The type ID that will be assigned to the next new type.
134  serialization::TypeID NextTypeID;
135
136  /// \brief Map that provides the ID numbers of each type within the
137  /// output stream, plus those deserialized from a chained PCH.
138  ///
139  /// The ID numbers of types are consecutive (in order of discovery)
140  /// and start at 1. 0 is reserved for NULL. When types are actually
141  /// stored in the stream, the ID number is shifted by 2 bits to
142  /// allow for the const/volatile qualifiers.
143  ///
144  /// Keys in the map never have const/volatile qualifiers.
145  serialization::TypeIdxMap TypeIdxs;
146
147  /// \brief Offset of each type in the bitstream, indexed by
148  /// the type's ID.
149  std::vector<uint32_t> TypeOffsets;
150
151  /// \brief The first ID number we can use for our own identifiers.
152  serialization::IdentID FirstIdentID;
153
154  /// \brief The identifier ID that will be assigned to the next new identifier.
155  serialization::IdentID NextIdentID;
156
157  /// \brief Map that provides the ID numbers of each identifier in
158  /// the output stream.
159  ///
160  /// The ID numbers for identifiers are consecutive (in order of
161  /// discovery), starting at 1. An ID of zero refers to a NULL
162  /// IdentifierInfo.
163  llvm::DenseMap<const IdentifierInfo *, serialization::IdentID> IdentifierIDs;
164
165  /// \brief Offsets of each of the identifier IDs into the identifier
166  /// table.
167  std::vector<uint32_t> IdentifierOffsets;
168
169  /// \brief The first ID number we can use for our own selectors.
170  serialization::SelectorID FirstSelectorID;
171
172  /// \brief The selector ID that will be assigned to the next new identifier.
173  serialization::SelectorID NextSelectorID;
174
175  /// \brief Map that provides the ID numbers of each Selector.
176  llvm::DenseMap<Selector, serialization::SelectorID> SelectorIDs;
177
178  /// \brief Offset of each selector within the method pool/selector
179  /// table, indexed by the Selector ID (-1).
180  std::vector<uint32_t> SelectorOffsets;
181
182  /// \brief Offsets of each of the macro identifiers into the
183  /// bitstream.
184  ///
185  /// For each identifier that is associated with a macro, this map
186  /// provides the offset into the bitstream where that macro is
187  /// defined.
188  llvm::DenseMap<const IdentifierInfo *, uint64_t> MacroOffsets;
189
190  /// \brief The set of identifiers that had macro definitions at some point.
191  std::vector<const IdentifierInfo *> DeserializedMacroNames;
192
193  /// \brief The first ID number we can use for our own macro definitions.
194  serialization::MacroID FirstMacroID;
195
196  /// \brief The decl ID that will be assigned to the next new macro definition.
197  serialization::MacroID NextMacroID;
198
199  /// \brief Mapping from macro definitions (as they occur in the preprocessing
200  /// record) to the macro IDs.
201  llvm::DenseMap<const MacroDefinition *, serialization::MacroID>
202      MacroDefinitions;
203
204  /// \brief Mapping from the macro definition indices in \c MacroDefinitions
205  /// to the corresponding offsets within the preprocessor block.
206  std::vector<uint32_t> MacroDefinitionOffsets;
207
208  typedef llvm::SmallVector<uint64_t, 2> UpdateRecord;
209  typedef llvm::DenseMap<const Decl *, UpdateRecord> DeclUpdateMap;
210  /// \brief Mapping from declarations that came from a chained PCH to the
211  /// record containing modifications to them.
212  DeclUpdateMap DeclUpdates;
213
214  typedef llvm::DenseMap<Decl *, Decl *> FirstLatestDeclMap;
215  /// \brief Map of first declarations from a chained PCH that point to the
216  /// most recent declarations in another PCH.
217  FirstLatestDeclMap FirstLatestDecls;
218
219  /// \brief Declarations encountered that might be external
220  /// definitions.
221  ///
222  /// We keep track of external definitions (as well as tentative
223  /// definitions) as we are emitting declarations to the AST
224  /// file. The AST file contains a separate record for these external
225  /// definitions, which are provided to the AST consumer by the AST
226  /// reader. This is behavior is required to properly cope with,
227  /// e.g., tentative variable definitions that occur within
228  /// headers. The declarations themselves are stored as declaration
229  /// IDs, since they will be written out to an EXTERNAL_DEFINITIONS
230  /// record.
231  llvm::SmallVector<uint64_t, 16> ExternalDefinitions;
232
233  /// \brief DeclContexts that have received extensions since their serialized
234  /// form.
235  ///
236  /// For namespaces, when we're chaining and encountering a namespace, we check if
237  /// its primary namespace comes from the chain. If it does, we add the primary
238  /// to this set, so that we can write out lexical content updates for it.
239  llvm::SmallPtrSet<const DeclContext *, 16> UpdatedDeclContexts;
240
241  typedef llvm::SmallPtrSet<const Decl *, 16> DeclsToRewriteTy;
242  /// \brief Decls that will be replaced in the current dependent AST file.
243  DeclsToRewriteTy DeclsToRewrite;
244
245  /// \brief Decls that have been replaced in the current dependent AST file.
246  ///
247  /// When a decl changes fundamentally after being deserialized (this shouldn't
248  /// happen, but the ObjC AST nodes are designed this way), it will be
249  /// serialized again. In this case, it is registered here, so that the reader
250  /// knows to read the updated version.
251  llvm::SmallVector<std::pair<serialization::DeclID, uint64_t>, 16>
252      ReplacedDecls;
253
254  /// \brief Statements that we've encountered while serializing a
255  /// declaration or type.
256  llvm::SmallVector<Stmt *, 16> StmtsToEmit;
257
258  /// \brief Statements collection to use for ASTWriter::AddStmt().
259  /// It will point to StmtsToEmit unless it is overriden.
260  llvm::SmallVector<Stmt *, 16> *CollectedStmts;
261
262  /// \brief Mapping from SwitchCase statements to IDs.
263  std::map<SwitchCase *, unsigned> SwitchCaseIDs;
264
265  /// \brief Mapping from OpaqueValueExpr expressions to IDs.
266  llvm::DenseMap<OpaqueValueExpr *, unsigned> OpaqueValues;
267
268  /// \brief The number of statements written to the AST file.
269  unsigned NumStatements;
270
271  /// \brief The number of macros written to the AST file.
272  unsigned NumMacros;
273
274  /// \brief The number of lexical declcontexts written to the AST
275  /// file.
276  unsigned NumLexicalDeclContexts;
277
278  /// \brief The number of visible declcontexts written to the AST
279  /// file.
280  unsigned NumVisibleDeclContexts;
281
282  /// \brief The offset of each CXXBaseSpecifier set within the AST.
283  llvm::SmallVector<uint32_t, 4> CXXBaseSpecifiersOffsets;
284
285  /// \brief The first ID number we can use for our own base specifiers.
286  serialization::CXXBaseSpecifiersID FirstCXXBaseSpecifiersID;
287
288  /// \brief The base specifiers ID that will be assigned to the next new
289  /// set of C++ base specifiers.
290  serialization::CXXBaseSpecifiersID NextCXXBaseSpecifiersID;
291
292  /// \brief A set of C++ base specifiers that is queued to be written into the
293  /// AST file.
294  struct QueuedCXXBaseSpecifiers {
295    QueuedCXXBaseSpecifiers() : ID(), Bases(), BasesEnd() { }
296
297    QueuedCXXBaseSpecifiers(serialization::CXXBaseSpecifiersID ID,
298                            CXXBaseSpecifier const *Bases,
299                            CXXBaseSpecifier const *BasesEnd)
300      : ID(ID), Bases(Bases), BasesEnd(BasesEnd) { }
301
302    serialization::CXXBaseSpecifiersID ID;
303    CXXBaseSpecifier const * Bases;
304    CXXBaseSpecifier const * BasesEnd;
305  };
306
307  /// \brief Queue of C++ base specifiers to be written to the AST file,
308  /// in the order they should be written.
309  llvm::SmallVector<QueuedCXXBaseSpecifiers, 2> CXXBaseSpecifiersToWrite;
310
311  /// \brief Write the given subexpression to the bitstream.
312  void WriteSubStmt(Stmt *S);
313
314  void WriteBlockInfoBlock();
315  void WriteMetadata(ASTContext &Context, const char *isysroot,
316                     const std::string &OutputFile);
317  void WriteLanguageOptions(const LangOptions &LangOpts);
318  void WriteStatCache(MemorizeStatCalls &StatCalls);
319  void WriteSourceManagerBlock(SourceManager &SourceMgr,
320                               const Preprocessor &PP,
321                               const char* isysroot);
322  void WritePreprocessor(const Preprocessor &PP);
323  void WriteHeaderSearch(HeaderSearch &HS, const char* isysroot);
324  void WritePreprocessorDetail(PreprocessingRecord &PPRec);
325  void WritePragmaDiagnosticMappings(const Diagnostic &Diag);
326  void WriteCXXBaseSpecifiersOffsets();
327  void WriteType(QualType T);
328  uint64_t WriteDeclContextLexicalBlock(ASTContext &Context, DeclContext *DC);
329  uint64_t WriteDeclContextVisibleBlock(ASTContext &Context, DeclContext *DC);
330  void WriteTypeDeclOffsets();
331  void WriteSelectors(Sema &SemaRef);
332  void WriteReferencedSelectorsPool(Sema &SemaRef);
333  void WriteIdentifierTable(Preprocessor &PP);
334  void WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record);
335  void WriteDeclUpdatesBlocks();
336  void WriteDeclReplacementsBlock();
337  void WriteDeclContextVisibleUpdate(const DeclContext *DC);
338  void WriteFPPragmaOptions(const FPOptions &Opts);
339  void WriteOpenCLExtensions(Sema &SemaRef);
340
341  unsigned ParmVarDeclAbbrev;
342  unsigned DeclContextLexicalAbbrev;
343  unsigned DeclContextVisibleLookupAbbrev;
344  unsigned UpdateVisibleAbbrev;
345  void WriteDeclsBlockAbbrevs();
346  void WriteDecl(ASTContext &Context, Decl *D);
347
348  void WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
349                    const char* isysroot, const std::string &OutputFile);
350  void WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
351                     const char* isysroot);
352
353public:
354  /// \brief Create a new precompiled header writer that outputs to
355  /// the given bitstream.
356  ASTWriter(llvm::BitstreamWriter &Stream);
357
358  /// \brief Set the listener that will receive notification of serialization
359  /// events.
360  void SetSerializationListener(ASTSerializationListener *Listener) {
361    SerializationListener = Listener;
362  }
363
364  /// \brief Write a precompiled header for the given semantic analysis.
365  ///
366  /// \param SemaRef a reference to the semantic analysis object that processed
367  /// the AST to be written into the precompiled header.
368  ///
369  /// \param StatCalls the object that cached all of the stat() calls made while
370  /// searching for source files and headers.
371  ///
372  /// \param isysroot if non-NULL, write a relocatable PCH file whose headers
373  /// are relative to the given system root.
374  ///
375  /// \param PPRec Record of the preprocessing actions that occurred while
376  /// preprocessing this file, e.g., macro instantiations
377  void WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
378                const std::string &OutputFile,
379                const char* isysroot);
380
381  /// \brief Emit a source location.
382  void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record);
383
384  /// \brief Emit a source range.
385  void AddSourceRange(SourceRange Range, RecordDataImpl &Record);
386
387  /// \brief Emit an integral value.
388  void AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record);
389
390  /// \brief Emit a signed integral value.
391  void AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record);
392
393  /// \brief Emit a floating-point value.
394  void AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record);
395
396  /// \brief Emit a reference to an identifier.
397  void AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record);
398
399  /// \brief Emit a Selector (which is a smart pointer reference).
400  void AddSelectorRef(Selector, RecordDataImpl &Record);
401
402  /// \brief Emit a CXXTemporary.
403  void AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record);
404
405  /// \brief Emit a set of C++ base specifiers to the record.
406  void AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
407                               CXXBaseSpecifier const *BasesEnd,
408                               RecordDataImpl &Record);
409
410  /// \brief Get the unique number used to refer to the given selector.
411  serialization::SelectorID getSelectorRef(Selector Sel);
412
413  /// \brief Get the unique number used to refer to the given identifier.
414  serialization::IdentID getIdentifierRef(const IdentifierInfo *II);
415
416  /// \brief Retrieve the offset of the macro definition for the given
417  /// identifier.
418  ///
419  /// The identifier must refer to a macro.
420  uint64_t getMacroOffset(const IdentifierInfo *II) {
421    assert(MacroOffsets.find(II) != MacroOffsets.end() &&
422           "Identifier does not name a macro");
423    return MacroOffsets[II];
424  }
425
426  /// \brief Retrieve the ID number corresponding to the given macro
427  /// definition.
428  serialization::MacroID getMacroDefinitionID(MacroDefinition *MD);
429
430  /// \brief Emit a reference to a type.
431  void AddTypeRef(QualType T, RecordDataImpl &Record);
432
433  /// \brief Force a type to be emitted and get its ID.
434  serialization::TypeID GetOrCreateTypeID(QualType T);
435
436  /// \brief Determine the type ID of an already-emitted type.
437  serialization::TypeID getTypeID(QualType T) const;
438
439  /// \brief Force a type to be emitted and get its index.
440  serialization::TypeIdx GetOrCreateTypeIdx(QualType T);
441
442  /// \brief Determine the type index of an already-emitted type.
443  serialization::TypeIdx getTypeIdx(QualType T) const;
444
445  /// \brief Emits a reference to a declarator info.
446  void AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record);
447
448  /// \brief Emits a type with source-location information.
449  void AddTypeLoc(TypeLoc TL, RecordDataImpl &Record);
450
451  /// \brief Emits a template argument location info.
452  void AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
453                                  const TemplateArgumentLocInfo &Arg,
454                                  RecordDataImpl &Record);
455
456  /// \brief Emits a template argument location.
457  void AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
458                              RecordDataImpl &Record);
459
460  /// \brief Emit a reference to a declaration.
461  void AddDeclRef(const Decl *D, RecordDataImpl &Record);
462
463
464  /// \brief Force a declaration to be emitted and get its ID.
465  serialization::DeclID GetDeclRef(const Decl *D);
466
467  /// \brief Determine the declaration ID of an already-emitted
468  /// declaration.
469  serialization::DeclID getDeclID(const Decl *D);
470
471  /// \brief Emit a declaration name.
472  void AddDeclarationName(DeclarationName Name, RecordDataImpl &Record);
473  void AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
474                             DeclarationName Name, RecordDataImpl &Record);
475  void AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
476                              RecordDataImpl &Record);
477
478  void AddQualifierInfo(const QualifierInfo &Info, RecordDataImpl &Record);
479
480  /// \brief Emit a nested name specifier.
481  void AddNestedNameSpecifier(NestedNameSpecifier *NNS, RecordDataImpl &Record);
482
483  /// \brief Emit a nested name specifier with source-location information.
484  void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
485                                 RecordDataImpl &Record);
486
487  /// \brief Emit a template name.
488  void AddTemplateName(TemplateName Name, RecordDataImpl &Record);
489
490  /// \brief Emit a template argument.
491  void AddTemplateArgument(const TemplateArgument &Arg, RecordDataImpl &Record);
492
493  /// \brief Emit a template parameter list.
494  void AddTemplateParameterList(const TemplateParameterList *TemplateParams,
495                                RecordDataImpl &Record);
496
497  /// \brief Emit a template argument list.
498  void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
499                                RecordDataImpl &Record);
500
501  /// \brief Emit a UnresolvedSet structure.
502  void AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record);
503
504  /// \brief Emit a C++ base specifier.
505  void AddCXXBaseSpecifier(const CXXBaseSpecifier &Base, RecordDataImpl &Record);
506
507  /// \brief Emit a CXXCtorInitializer array.
508  void AddCXXCtorInitializers(
509                             const CXXCtorInitializer * const *CtorInitializers,
510                             unsigned NumCtorInitializers,
511                             RecordDataImpl &Record);
512
513  void AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record);
514
515  /// \brief Add a string to the given record.
516  void AddString(llvm::StringRef Str, RecordDataImpl &Record);
517
518  /// \brief Add a version tuple to the given record
519  void AddVersionTuple(const VersionTuple &Version, RecordDataImpl &Record);
520
521  /// \brief Mark a declaration context as needing an update.
522  void AddUpdatedDeclContext(const DeclContext *DC) {
523    UpdatedDeclContexts.insert(DC);
524  }
525
526  void RewriteDecl(const Decl *D) {
527    DeclsToRewrite.insert(D);
528    // Reset the flag, so that we don't add this decl multiple times.
529    const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
530  }
531
532  /// \brief Note that the identifier II occurs at the given offset
533  /// within the identifier table.
534  void SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset);
535
536  /// \brief Note that the selector Sel occurs at the given offset
537  /// within the method pool/selector table.
538  void SetSelectorOffset(Selector Sel, uint32_t Offset);
539
540  /// \brief Add the given statement or expression to the queue of
541  /// statements to emit.
542  ///
543  /// This routine should be used when emitting types and declarations
544  /// that have expressions as part of their formulation. Once the
545  /// type or declaration has been written, call FlushStmts() to write
546  /// the corresponding statements just after the type or
547  /// declaration.
548  void AddStmt(Stmt *S) {
549      CollectedStmts->push_back(S);
550  }
551
552  /// \brief Flush all of the statements and expressions that have
553  /// been added to the queue via AddStmt().
554  void FlushStmts();
555
556  /// \brief Flush all of the C++ base specifier sets that have been added
557  /// via \c AddCXXBaseSpecifiersRef().
558  void FlushCXXBaseSpecifiers();
559
560  /// \brief Record an ID for the given switch-case statement.
561  unsigned RecordSwitchCaseID(SwitchCase *S);
562
563  /// \brief Retrieve the ID for the given switch-case statement.
564  unsigned getSwitchCaseID(SwitchCase *S);
565
566  void ClearSwitchCaseIDs();
567
568  /// \brief Retrieve the ID for the given opaque value expression.
569  unsigned getOpaqueValueID(OpaqueValueExpr *e);
570
571  unsigned getParmVarDeclAbbrev() const { return ParmVarDeclAbbrev; }
572
573  bool hasChain() const { return Chain; }
574
575  // ASTDeserializationListener implementation
576  void ReaderInitialized(ASTReader *Reader);
577  void IdentifierRead(serialization::IdentID ID, IdentifierInfo *II);
578  void TypeRead(serialization::TypeIdx Idx, QualType T);
579  void DeclRead(serialization::DeclID ID, const Decl *D);
580  void SelectorRead(serialization::SelectorID ID, Selector Sel);
581  void MacroDefinitionRead(serialization::MacroID ID, MacroDefinition *MD);
582
583  // ASTMutationListener implementation.
584  virtual void CompletedTagDefinition(const TagDecl *D);
585  virtual void AddedVisibleDecl(const DeclContext *DC, const Decl *D);
586  virtual void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D);
587  virtual void AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
588                                    const ClassTemplateSpecializationDecl *D);
589  virtual void AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
590                                              const FunctionDecl *D);
591  virtual void CompletedImplicitDefinition(const FunctionDecl *D);
592};
593
594/// \brief AST and semantic-analysis consumer that generates a
595/// precompiled header from the parsed source code.
596class PCHGenerator : public SemaConsumer {
597  const Preprocessor &PP;
598  std::string OutputFile;
599  const char *isysroot;
600  llvm::raw_ostream *Out;
601  Sema *SemaPtr;
602  MemorizeStatCalls *StatCalls; // owned by the FileManager
603  std::vector<unsigned char> Buffer;
604  llvm::BitstreamWriter Stream;
605  ASTWriter Writer;
606  bool Chaining;
607
608protected:
609  ASTWriter &getWriter() { return Writer; }
610  const ASTWriter &getWriter() const { return Writer; }
611
612public:
613  PCHGenerator(const Preprocessor &PP, const std::string &OutputFile, bool Chaining,
614               const char *isysroot, llvm::raw_ostream *Out);
615  virtual void InitializeSema(Sema &S) { SemaPtr = &S; }
616  virtual void HandleTranslationUnit(ASTContext &Ctx);
617  virtual ASTMutationListener *GetASTMutationListener();
618  virtual ASTSerializationListener *GetASTSerializationListener();
619  virtual ASTDeserializationListener *GetASTDeserializationListener();
620};
621
622} // end namespace clang
623
624#endif
625