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