]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/tools/clang/include/clang/Serialization/ASTWriter.h
MFC r244628:
[FreeBSD/stable/9.git] / contrib / llvm / tools / clang / include / clang / Serialization / ASTWriter.h
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/Lex/PPMutationListener.h"
22 #include "clang/Serialization/ASTBitCodes.h"
23 #include "clang/Serialization/ASTDeserializationListener.h"
24 #include "clang/Sema/SemaConsumer.h"
25 #include "llvm/ADT/SmallPtrSet.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/ADT/DenseMap.h"
28 #include "llvm/ADT/DenseSet.h"
29 #include "llvm/ADT/MapVector.h"
30 #include "llvm/ADT/SetVector.h"
31 #include "llvm/Bitcode/BitstreamWriter.h"
32 #include <map>
33 #include <queue>
34 #include <vector>
35
36 namespace llvm {
37   class APFloat;
38   class APInt;
39   class BitstreamWriter;
40 }
41
42 namespace clang {
43
44 class ASTContext;
45 class NestedNameSpecifier;
46 class CXXBaseSpecifier;
47 class CXXCtorInitializer;
48 class FileEntry;
49 class FPOptions;
50 class HeaderSearch;
51 class IdentifierResolver;
52 class MacroDefinition;
53 class OpaqueValueExpr;
54 class OpenCLOptions;
55 class ASTReader;
56 class MacroInfo;
57 class Module;
58 class PreprocessedEntity;
59 class PreprocessingRecord;
60 class Preprocessor;
61 class Sema;
62 class SourceManager;
63 class SwitchCase;
64 class TargetInfo;
65 class VersionTuple;
66
67 namespace SrcMgr { class SLocEntry; }
68
69 /// \brief Writes an AST file containing the contents of a translation unit.
70 ///
71 /// The ASTWriter class produces a bitstream containing the serialized
72 /// representation of a given abstract syntax tree and its supporting
73 /// data structures. This bitstream can be de-serialized via an
74 /// instance of the ASTReader class.
75 class ASTWriter : public ASTDeserializationListener,
76                   public PPMutationListener,
77                   public ASTMutationListener {
78 public:
79   typedef SmallVector<uint64_t, 64> RecordData;
80   typedef SmallVectorImpl<uint64_t> RecordDataImpl;
81
82   friend class ASTDeclWriter;
83   friend class ASTStmtWriter;
84 private:
85   /// \brief Map that provides the ID numbers of each type within the
86   /// output stream, plus those deserialized from a chained PCH.
87   ///
88   /// The ID numbers of types are consecutive (in order of discovery)
89   /// and start at 1. 0 is reserved for NULL. When types are actually
90   /// stored in the stream, the ID number is shifted by 2 bits to
91   /// allow for the const/volatile qualifiers.
92   ///
93   /// Keys in the map never have const/volatile qualifiers.
94   typedef llvm::DenseMap<QualType, serialization::TypeIdx,
95                          serialization::UnsafeQualTypeDenseMapInfo>
96     TypeIdxMap;
97
98   /// \brief The bitstream writer used to emit this precompiled header.
99   llvm::BitstreamWriter &Stream;
100
101   /// \brief The ASTContext we're writing.
102   ASTContext *Context;
103
104   /// \brief The preprocessor we're writing.
105   Preprocessor *PP;
106
107   /// \brief The reader of existing AST files, if we're chaining.
108   ASTReader *Chain;
109
110   /// \brief The module we're currently writing, if any.
111   Module *WritingModule;
112                     
113   /// \brief Indicates when the AST writing is actively performing
114   /// serialization, rather than just queueing updates.
115   bool WritingAST;
116
117   /// \brief Indicates that we are done serializing the collection of decls
118   /// and types to emit.
119   bool DoneWritingDeclsAndTypes;
120
121   /// \brief Indicates that the AST contained compiler errors.
122   bool ASTHasCompilerErrors;
123
124   /// \brief Mapping from input file entries to the index into the
125   /// offset table where information about that input file is stored.
126   llvm::DenseMap<const FileEntry *, uint32_t> InputFileIDs;
127
128   /// \brief Stores a declaration or a type to be written to the AST file.
129   class DeclOrType {
130   public:
131     DeclOrType(Decl *D) : Stored(D), IsType(false) { }
132     DeclOrType(QualType T) : Stored(T.getAsOpaquePtr()), IsType(true) { }
133
134     bool isType() const { return IsType; }
135     bool isDecl() const { return !IsType; }
136
137     QualType getType() const {
138       assert(isType() && "Not a type!");
139       return QualType::getFromOpaquePtr(Stored);
140     }
141
142     Decl *getDecl() const {
143       assert(isDecl() && "Not a decl!");
144       return static_cast<Decl *>(Stored);
145     }
146
147   private:
148     void *Stored;
149     bool IsType;
150   };
151
152   /// \brief The declarations and types to emit.
153   std::queue<DeclOrType> DeclTypesToEmit;
154
155   /// \brief The first ID number we can use for our own declarations.
156   serialization::DeclID FirstDeclID;
157
158   /// \brief The decl ID that will be assigned to the next new decl.
159   serialization::DeclID NextDeclID;
160
161   /// \brief Map that provides the ID numbers of each declaration within
162   /// the output stream, as well as those deserialized from a chained PCH.
163   ///
164   /// The ID numbers of declarations are consecutive (in order of
165   /// discovery) and start at 2. 1 is reserved for the translation
166   /// unit, while 0 is reserved for NULL.
167   llvm::DenseMap<const Decl *, serialization::DeclID> DeclIDs;
168
169   /// \brief Offset of each declaration in the bitstream, indexed by
170   /// the declaration's ID.
171   std::vector<serialization::DeclOffset> DeclOffsets;
172
173   /// \brief Sorted (by file offset) vector of pairs of file offset/DeclID.
174   typedef SmallVector<std::pair<unsigned, serialization::DeclID>, 64>
175     LocDeclIDsTy;
176   struct DeclIDInFileInfo {
177     LocDeclIDsTy DeclIDs;
178     /// \brief Set when the DeclIDs vectors from all files are joined, this
179     /// indicates the index that this particular vector has in the global one.
180     unsigned FirstDeclIndex;
181   };
182   typedef llvm::DenseMap<FileID, DeclIDInFileInfo *> FileDeclIDsTy;
183
184   /// \brief Map from file SLocEntries to info about the file-level declarations
185   /// that it contains.
186   FileDeclIDsTy FileDeclIDs;
187
188   void associateDeclWithFile(const Decl *D, serialization::DeclID);
189
190   /// \brief The first ID number we can use for our own types.
191   serialization::TypeID FirstTypeID;
192
193   /// \brief The type ID that will be assigned to the next new type.
194   serialization::TypeID NextTypeID;
195
196   /// \brief Map that provides the ID numbers of each type within the
197   /// output stream, plus those deserialized from a chained PCH.
198   ///
199   /// The ID numbers of types are consecutive (in order of discovery)
200   /// and start at 1. 0 is reserved for NULL. When types are actually
201   /// stored in the stream, the ID number is shifted by 2 bits to
202   /// allow for the const/volatile qualifiers.
203   ///
204   /// Keys in the map never have const/volatile qualifiers.
205   TypeIdxMap TypeIdxs;
206
207   /// \brief Offset of each type in the bitstream, indexed by
208   /// the type's ID.
209   std::vector<uint32_t> TypeOffsets;
210
211   /// \brief The first ID number we can use for our own identifiers.
212   serialization::IdentID FirstIdentID;
213
214   /// \brief The identifier ID that will be assigned to the next new identifier.
215   serialization::IdentID NextIdentID;
216
217   /// \brief Map that provides the ID numbers of each identifier in
218   /// the output stream.
219   ///
220   /// The ID numbers for identifiers are consecutive (in order of
221   /// discovery), starting at 1. An ID of zero refers to a NULL
222   /// IdentifierInfo.
223   llvm::DenseMap<const IdentifierInfo *, serialization::IdentID> IdentifierIDs;
224
225   /// \brief The first ID number we can use for our own macros.
226   serialization::MacroID FirstMacroID;
227
228   /// \brief The identifier ID that will be assigned to the next new identifier.
229   serialization::MacroID NextMacroID;
230
231   /// \brief Map that provides the ID numbers of each macro.
232   llvm::DenseMap<MacroInfo *, serialization::MacroID> MacroIDs;
233
234   /// @name FlushStmt Caches
235   /// @{
236
237   /// \brief Set of parent Stmts for the currently serializing sub stmt.
238   llvm::DenseSet<Stmt *> ParentStmts;
239
240   /// \brief Offsets of sub stmts already serialized. The offset points
241   /// just after the stmt record.
242   llvm::DenseMap<Stmt *, uint64_t> SubStmtEntries;
243
244   /// @}
245
246   /// \brief Offsets of each of the identifier IDs into the identifier
247   /// table.
248   std::vector<uint32_t> IdentifierOffsets;
249
250   /// \brief The first ID number we can use for our own submodules.
251   serialization::SubmoduleID FirstSubmoduleID;
252   
253   /// \brief The submodule ID that will be assigned to the next new submodule.
254   serialization::SubmoduleID NextSubmoduleID;
255
256   /// \brief The first ID number we can use for our own selectors.
257   serialization::SelectorID FirstSelectorID;
258
259   /// \brief The selector ID that will be assigned to the next new selector.
260   serialization::SelectorID NextSelectorID;
261
262   /// \brief Map that provides the ID numbers of each Selector.
263   llvm::DenseMap<Selector, serialization::SelectorID> SelectorIDs;
264
265   /// \brief Offset of each selector within the method pool/selector
266   /// table, indexed by the Selector ID (-1).
267   std::vector<uint32_t> SelectorOffsets;
268
269   typedef llvm::MapVector<MacroInfo *, MacroUpdate> MacroUpdatesMap;
270
271   /// \brief Updates to macro definitions that were loaded from an AST file.
272   MacroUpdatesMap MacroUpdates;
273
274   /// \brief Mapping from macro definitions (as they occur in the preprocessing
275   /// record) to the macro IDs.
276   llvm::DenseMap<const MacroDefinition *, serialization::PreprocessedEntityID>
277       MacroDefinitions;
278
279   typedef SmallVector<uint64_t, 2> UpdateRecord;
280   typedef llvm::DenseMap<const Decl *, UpdateRecord> DeclUpdateMap;
281   /// \brief Mapping from declarations that came from a chained PCH to the
282   /// record containing modifications to them.
283   DeclUpdateMap DeclUpdates;
284
285   typedef llvm::DenseMap<Decl *, Decl *> FirstLatestDeclMap;
286   /// \brief Map of first declarations from a chained PCH that point to the
287   /// most recent declarations in another PCH.
288   FirstLatestDeclMap FirstLatestDecls;
289
290   /// \brief Declarations encountered that might be external
291   /// definitions.
292   ///
293   /// We keep track of external definitions (as well as tentative
294   /// definitions) as we are emitting declarations to the AST
295   /// file. The AST file contains a separate record for these external
296   /// definitions, which are provided to the AST consumer by the AST
297   /// reader. This is behavior is required to properly cope with,
298   /// e.g., tentative variable definitions that occur within
299   /// headers. The declarations themselves are stored as declaration
300   /// IDs, since they will be written out to an EXTERNAL_DEFINITIONS
301   /// record.
302   SmallVector<uint64_t, 16> ExternalDefinitions;
303
304   /// \brief DeclContexts that have received extensions since their serialized
305   /// form.
306   ///
307   /// For namespaces, when we're chaining and encountering a namespace, we check
308   /// if its primary namespace comes from the chain. If it does, we add the
309   /// primary to this set, so that we can write out lexical content updates for
310   /// it.
311   llvm::SmallPtrSet<const DeclContext *, 16> UpdatedDeclContexts;
312
313   /// \brief Keeps track of visible decls that were added in DeclContexts
314   /// coming from another AST file.
315   SmallVector<const Decl *, 16> UpdatingVisibleDecls;
316
317   typedef llvm::SmallPtrSet<const Decl *, 16> DeclsToRewriteTy;
318   /// \brief Decls that will be replaced in the current dependent AST file.
319   DeclsToRewriteTy DeclsToRewrite;
320
321   /// \brief The set of Objective-C class that have categories we
322   /// should serialize.
323   llvm::SetVector<ObjCInterfaceDecl *> ObjCClassesWithCategories;
324                     
325   struct ReplacedDeclInfo {
326     serialization::DeclID ID;
327     uint64_t Offset;
328     unsigned Loc;
329
330     ReplacedDeclInfo() : ID(0), Offset(0), Loc(0) {}
331     ReplacedDeclInfo(serialization::DeclID ID, uint64_t Offset,
332                      SourceLocation Loc)
333       : ID(ID), Offset(Offset), Loc(Loc.getRawEncoding()) {}
334   };
335
336   /// \brief Decls that have been replaced in the current dependent AST file.
337   ///
338   /// When a decl changes fundamentally after being deserialized (this shouldn't
339   /// happen, but the ObjC AST nodes are designed this way), it will be
340   /// serialized again. In this case, it is registered here, so that the reader
341   /// knows to read the updated version.
342   SmallVector<ReplacedDeclInfo, 16> ReplacedDecls;
343                  
344   /// \brief The set of declarations that may have redeclaration chains that
345   /// need to be serialized.
346   llvm::SetVector<Decl *, llvm::SmallVector<Decl *, 4>, 
347                   llvm::SmallPtrSet<Decl *, 4> > Redeclarations;
348                                       
349   /// \brief Statements that we've encountered while serializing a
350   /// declaration or type.
351   SmallVector<Stmt *, 16> StmtsToEmit;
352
353   /// \brief Statements collection to use for ASTWriter::AddStmt().
354   /// It will point to StmtsToEmit unless it is overriden.
355   SmallVector<Stmt *, 16> *CollectedStmts;
356
357   /// \brief Mapping from SwitchCase statements to IDs.
358   llvm::DenseMap<SwitchCase *, unsigned> SwitchCaseIDs;
359
360   /// \brief The number of statements written to the AST file.
361   unsigned NumStatements;
362
363   /// \brief The number of macros written to the AST file.
364   unsigned NumMacros;
365
366   /// \brief The number of lexical declcontexts written to the AST
367   /// file.
368   unsigned NumLexicalDeclContexts;
369
370   /// \brief The number of visible declcontexts written to the AST
371   /// file.
372   unsigned NumVisibleDeclContexts;
373
374   /// \brief The offset of each CXXBaseSpecifier set within the AST.
375   SmallVector<uint32_t, 4> CXXBaseSpecifiersOffsets;
376
377   /// \brief The first ID number we can use for our own base specifiers.
378   serialization::CXXBaseSpecifiersID FirstCXXBaseSpecifiersID;
379
380   /// \brief The base specifiers ID that will be assigned to the next new
381   /// set of C++ base specifiers.
382   serialization::CXXBaseSpecifiersID NextCXXBaseSpecifiersID;
383
384   /// \brief A set of C++ base specifiers that is queued to be written into the
385   /// AST file.
386   struct QueuedCXXBaseSpecifiers {
387     QueuedCXXBaseSpecifiers() : ID(), Bases(), BasesEnd() { }
388
389     QueuedCXXBaseSpecifiers(serialization::CXXBaseSpecifiersID ID,
390                             CXXBaseSpecifier const *Bases,
391                             CXXBaseSpecifier const *BasesEnd)
392       : ID(ID), Bases(Bases), BasesEnd(BasesEnd) { }
393
394     serialization::CXXBaseSpecifiersID ID;
395     CXXBaseSpecifier const * Bases;
396     CXXBaseSpecifier const * BasesEnd;
397   };
398
399   /// \brief Queue of C++ base specifiers to be written to the AST file,
400   /// in the order they should be written.
401   SmallVector<QueuedCXXBaseSpecifiers, 2> CXXBaseSpecifiersToWrite;
402
403   /// \brief A mapping from each known submodule to its ID number, which will
404   /// be a positive integer.
405   llvm::DenseMap<Module *, unsigned> SubmoduleIDs;
406                     
407   /// \brief Retrieve or create a submodule ID for this module.
408   unsigned getSubmoduleID(Module *Mod);
409                     
410   /// \brief Write the given subexpression to the bitstream.
411   void WriteSubStmt(Stmt *S,
412                     llvm::DenseMap<Stmt *, uint64_t> &SubStmtEntries,
413                     llvm::DenseSet<Stmt *> &ParentStmts);
414
415   void WriteBlockInfoBlock();
416   void WriteControlBlock(Preprocessor &PP, ASTContext &Context,
417                          StringRef isysroot, const std::string &OutputFile);
418   void WriteInputFiles(SourceManager &SourceMgr, StringRef isysroot);
419   void WriteSourceManagerBlock(SourceManager &SourceMgr,
420                                const Preprocessor &PP,
421                                StringRef isysroot);
422   void WritePreprocessor(const Preprocessor &PP, bool IsModule);
423   void WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot);
424   void WritePreprocessorDetail(PreprocessingRecord &PPRec);
425   void WriteSubmodules(Module *WritingModule);
426                                         
427   void WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag);
428   void WriteCXXBaseSpecifiersOffsets();
429   void WriteType(QualType T);
430   uint64_t WriteDeclContextLexicalBlock(ASTContext &Context, DeclContext *DC);
431   uint64_t WriteDeclContextVisibleBlock(ASTContext &Context, DeclContext *DC);
432   void WriteTypeDeclOffsets();
433   void WriteFileDeclIDsMap();
434   void WriteComments();
435   void WriteSelectors(Sema &SemaRef);
436   void WriteReferencedSelectorsPool(Sema &SemaRef);
437   void WriteIdentifierTable(Preprocessor &PP, IdentifierResolver &IdResolver,
438                             bool IsModule);
439   void WriteAttributes(ArrayRef<const Attr*> Attrs, RecordDataImpl &Record);
440   void WriteMacroUpdates();
441   void ResolveDeclUpdatesBlocks();
442   void WriteDeclUpdatesBlocks();
443   void WriteDeclReplacementsBlock();
444   void WriteDeclContextVisibleUpdate(const DeclContext *DC);
445   void WriteFPPragmaOptions(const FPOptions &Opts);
446   void WriteOpenCLExtensions(Sema &SemaRef);
447   void WriteObjCCategories();
448   void WriteRedeclarations();
449   void WriteMergedDecls();
450                         
451   unsigned DeclParmVarAbbrev;
452   unsigned DeclContextLexicalAbbrev;
453   unsigned DeclContextVisibleLookupAbbrev;
454   unsigned UpdateVisibleAbbrev;
455   unsigned DeclRefExprAbbrev;
456   unsigned CharacterLiteralAbbrev;
457   unsigned DeclRecordAbbrev;
458   unsigned IntegerLiteralAbbrev;
459   unsigned DeclTypedefAbbrev;
460   unsigned DeclVarAbbrev;
461   unsigned DeclFieldAbbrev;
462   unsigned DeclEnumAbbrev;
463   unsigned DeclObjCIvarAbbrev;
464
465   void WriteDeclsBlockAbbrevs();
466   void WriteDecl(ASTContext &Context, Decl *D);
467
468   void WriteASTCore(Sema &SemaRef,
469                     StringRef isysroot, const std::string &OutputFile,
470                     Module *WritingModule);
471
472 public:
473   /// \brief Create a new precompiled header writer that outputs to
474   /// the given bitstream.
475   ASTWriter(llvm::BitstreamWriter &Stream);
476   ~ASTWriter();
477
478   /// \brief Write a precompiled header for the given semantic analysis.
479   ///
480   /// \param SemaRef a reference to the semantic analysis object that processed
481   /// the AST to be written into the precompiled header.
482   ///
483   /// \param WritingModule The module that we are writing. If null, we are
484   /// writing a precompiled header.
485   ///
486   /// \param isysroot if non-empty, write a relocatable file whose headers
487   /// are relative to the given system root.
488   void WriteAST(Sema &SemaRef,
489                 const std::string &OutputFile,
490                 Module *WritingModule, StringRef isysroot,
491                 bool hasErrors = false);
492
493   /// \brief Emit a source location.
494   void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record);
495
496   /// \brief Emit a source range.
497   void AddSourceRange(SourceRange Range, RecordDataImpl &Record);
498
499   /// \brief Emit an integral value.
500   void AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record);
501
502   /// \brief Emit a signed integral value.
503   void AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record);
504
505   /// \brief Emit a floating-point value.
506   void AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record);
507
508   /// \brief Emit a reference to an identifier.
509   void AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record);
510
511   /// \brief Emit a reference to a macro.
512   void addMacroRef(MacroInfo *MI, RecordDataImpl &Record);
513
514   /// \brief Emit a Selector (which is a smart pointer reference).
515   void AddSelectorRef(Selector, RecordDataImpl &Record);
516
517   /// \brief Emit a CXXTemporary.
518   void AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record);
519
520   /// \brief Emit a set of C++ base specifiers to the record.
521   void AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
522                                CXXBaseSpecifier const *BasesEnd,
523                                RecordDataImpl &Record);
524
525   /// \brief Get the unique number used to refer to the given selector.
526   serialization::SelectorID getSelectorRef(Selector Sel);
527
528   /// \brief Get the unique number used to refer to the given identifier.
529   serialization::IdentID getIdentifierRef(const IdentifierInfo *II);
530
531   /// \brief Get the unique number used to refer to the given macro.
532   serialization::MacroID getMacroRef(MacroInfo *MI);
533
534   /// \brief Emit a reference to a type.
535   void AddTypeRef(QualType T, RecordDataImpl &Record);
536
537   /// \brief Force a type to be emitted and get its ID.
538   serialization::TypeID GetOrCreateTypeID(QualType T);
539
540   /// \brief Determine the type ID of an already-emitted type.
541   serialization::TypeID getTypeID(QualType T) const;
542
543   /// \brief Force a type to be emitted and get its index.
544   serialization::TypeIdx GetOrCreateTypeIdx( QualType T);
545
546   /// \brief Determine the type index of an already-emitted type.
547   serialization::TypeIdx getTypeIdx(QualType T) const;
548
549   /// \brief Emits a reference to a declarator info.
550   void AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record);
551
552   /// \brief Emits a type with source-location information.
553   void AddTypeLoc(TypeLoc TL, RecordDataImpl &Record);
554
555   /// \brief Emits a template argument location info.
556   void AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
557                                   const TemplateArgumentLocInfo &Arg,
558                                   RecordDataImpl &Record);
559
560   /// \brief Emits a template argument location.
561   void AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
562                               RecordDataImpl &Record);
563
564   /// \brief Emit a reference to a declaration.
565   void AddDeclRef(const Decl *D, RecordDataImpl &Record);
566
567
568   /// \brief Force a declaration to be emitted and get its ID.
569   serialization::DeclID GetDeclRef(const Decl *D);
570
571   /// \brief Determine the declaration ID of an already-emitted
572   /// declaration.
573   serialization::DeclID getDeclID(const Decl *D);
574
575   /// \brief Emit a declaration name.
576   void AddDeclarationName(DeclarationName Name, RecordDataImpl &Record);
577   void AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
578                              DeclarationName Name, RecordDataImpl &Record);
579   void AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
580                               RecordDataImpl &Record);
581
582   void AddQualifierInfo(const QualifierInfo &Info, RecordDataImpl &Record);
583
584   /// \brief Emit a nested name specifier.
585   void AddNestedNameSpecifier(NestedNameSpecifier *NNS, RecordDataImpl &Record);
586
587   /// \brief Emit a nested name specifier with source-location information.
588   void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
589                                  RecordDataImpl &Record);
590
591   /// \brief Emit a template name.
592   void AddTemplateName(TemplateName Name, RecordDataImpl &Record);
593
594   /// \brief Emit a template argument.
595   void AddTemplateArgument(const TemplateArgument &Arg, RecordDataImpl &Record);
596
597   /// \brief Emit a template parameter list.
598   void AddTemplateParameterList(const TemplateParameterList *TemplateParams,
599                                 RecordDataImpl &Record);
600
601   /// \brief Emit a template argument list.
602   void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
603                                 RecordDataImpl &Record);
604
605   /// \brief Emit a UnresolvedSet structure.
606   void AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record);
607
608   /// \brief Emit a C++ base specifier.
609   void AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
610                            RecordDataImpl &Record);
611
612   /// \brief Emit a CXXCtorInitializer array.
613   void AddCXXCtorInitializers(
614                              const CXXCtorInitializer * const *CtorInitializers,
615                              unsigned NumCtorInitializers,
616                              RecordDataImpl &Record);
617
618   void AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record);
619
620   /// \brief Add a string to the given record.
621   void AddString(StringRef Str, RecordDataImpl &Record);
622
623   /// \brief Add a version tuple to the given record
624   void AddVersionTuple(const VersionTuple &Version, RecordDataImpl &Record);
625
626   /// \brief Mark a declaration context as needing an update.
627   void AddUpdatedDeclContext(const DeclContext *DC) {
628     UpdatedDeclContexts.insert(DC);
629   }
630
631   void RewriteDecl(const Decl *D) {
632     DeclsToRewrite.insert(D);
633   }
634
635   bool isRewritten(const Decl *D) const {
636     return DeclsToRewrite.count(D);
637   }
638
639   /// \brief Infer the submodule ID that contains an entity at the given
640   /// source location.
641   serialization::SubmoduleID inferSubmoduleIDFromLocation(SourceLocation Loc);
642
643   /// \brief Note that the identifier II occurs at the given offset
644   /// within the identifier table.
645   void SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset);
646
647   /// \brief Note that the selector Sel occurs at the given offset
648   /// within the method pool/selector table.
649   void SetSelectorOffset(Selector Sel, uint32_t Offset);
650
651   /// \brief Add the given statement or expression to the queue of
652   /// statements to emit.
653   ///
654   /// This routine should be used when emitting types and declarations
655   /// that have expressions as part of their formulation. Once the
656   /// type or declaration has been written, call FlushStmts() to write
657   /// the corresponding statements just after the type or
658   /// declaration.
659   void AddStmt(Stmt *S) {
660       CollectedStmts->push_back(S);
661   }
662
663   /// \brief Flush all of the statements and expressions that have
664   /// been added to the queue via AddStmt().
665   void FlushStmts();
666
667   /// \brief Flush all of the C++ base specifier sets that have been added
668   /// via \c AddCXXBaseSpecifiersRef().
669   void FlushCXXBaseSpecifiers();
670
671   /// \brief Record an ID for the given switch-case statement.
672   unsigned RecordSwitchCaseID(SwitchCase *S);
673
674   /// \brief Retrieve the ID for the given switch-case statement.
675   unsigned getSwitchCaseID(SwitchCase *S);
676
677   void ClearSwitchCaseIDs();
678
679   unsigned getDeclParmVarAbbrev() const { return DeclParmVarAbbrev; }
680   unsigned getDeclRefExprAbbrev() const { return DeclRefExprAbbrev; }
681   unsigned getCharacterLiteralAbbrev() const { return CharacterLiteralAbbrev; }
682   unsigned getDeclRecordAbbrev() const { return DeclRecordAbbrev; }
683   unsigned getIntegerLiteralAbbrev() const { return IntegerLiteralAbbrev; }
684   unsigned getDeclTypedefAbbrev() const { return DeclTypedefAbbrev; }
685   unsigned getDeclVarAbbrev() const { return DeclVarAbbrev; }
686   unsigned getDeclFieldAbbrev() const { return DeclFieldAbbrev; }
687   unsigned getDeclEnumAbbrev() const { return DeclEnumAbbrev; }
688   unsigned getDeclObjCIvarAbbrev() const { return DeclObjCIvarAbbrev; }
689
690   bool hasChain() const { return Chain; }
691
692   // ASTDeserializationListener implementation
693   void ReaderInitialized(ASTReader *Reader);
694   void IdentifierRead(serialization::IdentID ID, IdentifierInfo *II);
695   void MacroRead(serialization::MacroID ID, MacroInfo *MI);
696   void TypeRead(serialization::TypeIdx Idx, QualType T);
697   void SelectorRead(serialization::SelectorID ID, Selector Sel);
698   void MacroDefinitionRead(serialization::PreprocessedEntityID ID,
699                            MacroDefinition *MD);
700   void ModuleRead(serialization::SubmoduleID ID, Module *Mod);
701
702   // PPMutationListener implementation.
703   virtual void UndefinedMacro(MacroInfo *MI);
704
705   // ASTMutationListener implementation.
706   virtual void CompletedTagDefinition(const TagDecl *D);
707   virtual void AddedVisibleDecl(const DeclContext *DC, const Decl *D);
708   virtual void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D);
709   virtual void AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
710                                     const ClassTemplateSpecializationDecl *D);
711   virtual void AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
712                                               const FunctionDecl *D);
713   virtual void CompletedImplicitDefinition(const FunctionDecl *D);
714   virtual void StaticDataMemberInstantiated(const VarDecl *D);
715   virtual void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
716                                             const ObjCInterfaceDecl *IFD);
717   virtual void AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
718                                             const ObjCPropertyDecl *OrigProp,
719                                             const ObjCCategoryDecl *ClassExt);
720 };
721
722 /// \brief AST and semantic-analysis consumer that generates a
723 /// precompiled header from the parsed source code.
724 class PCHGenerator : public SemaConsumer {
725   const Preprocessor &PP;
726   std::string OutputFile;
727   clang::Module *Module;
728   std::string isysroot;
729   raw_ostream *Out;
730   Sema *SemaPtr;
731   llvm::SmallVector<char, 128> Buffer;
732   llvm::BitstreamWriter Stream;
733   ASTWriter Writer;
734
735 protected:
736   ASTWriter &getWriter() { return Writer; }
737   const ASTWriter &getWriter() const { return Writer; }
738
739 public:
740   PCHGenerator(const Preprocessor &PP, StringRef OutputFile,
741                clang::Module *Module,
742                StringRef isysroot, raw_ostream *Out);
743   ~PCHGenerator();
744   virtual void InitializeSema(Sema &S) { SemaPtr = &S; }
745   virtual void HandleTranslationUnit(ASTContext &Ctx);
746   virtual PPMutationListener *GetPPMutationListener();
747   virtual ASTMutationListener *GetASTMutationListener();
748   virtual ASTDeserializationListener *GetASTDeserializationListener();
749 };
750
751 } // end namespace clang
752
753 #endif