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