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