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