1 //===- ASTWriter.h - AST File Writer ----------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file defines the ASTWriter class, which writes an AST file
10 //  containing a serialized representation of a translation unit.
11 //
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/Type.h"
20 #include "clang/Basic/LLVM.h"
21 #include "clang/Basic/SourceLocation.h"
22 #include "clang/Sema/SemaConsumer.h"
23 #include "clang/Serialization/ASTBitCodes.h"
24 #include "clang/Serialization/ASTDeserializationListener.h"
25 #include "clang/Serialization/PCHContainerOperations.h"
26 #include "llvm/ADT/ArrayRef.h"
27 #include "llvm/ADT/DenseMap.h"
28 #include "llvm/ADT/DenseSet.h"
29 #include "llvm/ADT/MapVector.h"
30 #include "llvm/ADT/STLExtras.h"
31 #include "llvm/ADT/SetVector.h"
32 #include "llvm/ADT/SmallVector.h"
33 #include "llvm/ADT/StringRef.h"
34 #include "llvm/Bitstream/BitstreamWriter.h"
35 #include <cassert>
36 #include <cstddef>
37 #include <cstdint>
38 #include <ctime>
39 #include <memory>
40 #include <queue>
41 #include <string>
42 #include <utility>
43 #include <vector>
44 
45 namespace llvm {
46 
47 class APFloat;
48 class APInt;
49 class APSInt;
50 
51 } // namespace llvm
52 
53 namespace clang {
54 
55 class ASTContext;
56 class ASTReader;
57 class ASTUnresolvedSet;
58 class Attr;
59 class CXXBaseSpecifier;
60 class CXXCtorInitializer;
61 class CXXRecordDecl;
62 class CXXTemporary;
63 class FileEntry;
64 class FPOptions;
65 class FPOptionsOverride;
66 class FunctionDecl;
67 class HeaderSearch;
68 class HeaderSearchOptions;
69 class IdentifierResolver;
70 class LangOptions;
71 class MacroDefinitionRecord;
72 class MacroInfo;
73 class Module;
74 class InMemoryModuleCache;
75 class ModuleFileExtension;
76 class ModuleFileExtensionWriter;
77 class NamedDecl;
78 class ObjCInterfaceDecl;
79 class PreprocessingRecord;
80 class Preprocessor;
81 struct QualifierInfo;
82 class RecordDecl;
83 class Sema;
84 class SourceManager;
85 class Stmt;
86 struct StoredDeclsList;
87 class SwitchCase;
88 class TemplateParameterList;
89 class Token;
90 class TypeSourceInfo;
91 
92 /// Writes an AST file containing the contents of a translation unit.
93 ///
94 /// The ASTWriter class produces a bitstream containing the serialized
95 /// representation of a given abstract syntax tree and its supporting
96 /// data structures. This bitstream can be de-serialized via an
97 /// instance of the ASTReader class.
98 class ASTWriter : public ASTDeserializationListener,
99                   public ASTMutationListener {
100 public:
101   friend class ASTDeclWriter;
102   friend class ASTRecordWriter;
103 
104   using RecordData = SmallVector<uint64_t, 64>;
105   using RecordDataImpl = SmallVectorImpl<uint64_t>;
106   using RecordDataRef = ArrayRef<uint64_t>;
107 
108 private:
109   /// Map that provides the ID numbers of each type within the
110   /// output stream, plus those deserialized from a chained PCH.
111   ///
112   /// The ID numbers of types are consecutive (in order of discovery)
113   /// and start at 1. 0 is reserved for NULL. When types are actually
114   /// stored in the stream, the ID number is shifted by 2 bits to
115   /// allow for the const/volatile qualifiers.
116   ///
117   /// Keys in the map never have const/volatile qualifiers.
118   using TypeIdxMap = llvm::DenseMap<QualType, serialization::TypeIdx,
119                                     serialization::UnsafeQualTypeDenseMapInfo>;
120 
121   /// The bitstream writer used to emit this precompiled header.
122   llvm::BitstreamWriter &Stream;
123 
124   /// The buffer associated with the bitstream.
125   const SmallVectorImpl<char> &Buffer;
126 
127   /// The PCM manager which manages memory buffers for pcm files.
128   InMemoryModuleCache &ModuleCache;
129 
130   /// The ASTContext we're writing.
131   ASTContext *Context = nullptr;
132 
133   /// The preprocessor we're writing.
134   Preprocessor *PP = nullptr;
135 
136   /// The reader of existing AST files, if we're chaining.
137   ASTReader *Chain = nullptr;
138 
139   /// The module we're currently writing, if any.
140   Module *WritingModule = nullptr;
141 
142   /// The offset of the first bit inside the AST_BLOCK.
143   uint64_t ASTBlockStartOffset = 0;
144 
145   /// The range representing all the AST_BLOCK.
146   std::pair<uint64_t, uint64_t> ASTBlockRange;
147 
148   /// The base directory for any relative paths we emit.
149   std::string BaseDirectory;
150 
151   /// Indicates whether timestamps should be written to the produced
152   /// module file. This is the case for files implicitly written to the
153   /// module cache, where we need the timestamps to determine if the module
154   /// file is up to date, but not otherwise.
155   bool IncludeTimestamps;
156 
157   /// Indicates when the AST writing is actively performing
158   /// serialization, rather than just queueing updates.
159   bool WritingAST = false;
160 
161   /// Indicates that we are done serializing the collection of decls
162   /// and types to emit.
163   bool DoneWritingDeclsAndTypes = false;
164 
165   /// Indicates that the AST contained compiler errors.
166   bool ASTHasCompilerErrors = false;
167 
168   /// Mapping from input file entries to the index into the
169   /// offset table where information about that input file is stored.
170   llvm::DenseMap<const FileEntry *, uint32_t> InputFileIDs;
171 
172   /// Stores a declaration or a type to be written to the AST file.
173   class DeclOrType {
174   public:
DeclOrType(Decl * D)175     DeclOrType(Decl *D) : Stored(D), IsType(false) {}
DeclOrType(QualType T)176     DeclOrType(QualType T) : Stored(T.getAsOpaquePtr()), IsType(true) {}
177 
isType()178     bool isType() const { return IsType; }
isDecl()179     bool isDecl() const { return !IsType; }
180 
getType()181     QualType getType() const {
182       assert(isType() && "Not a type!");
183       return QualType::getFromOpaquePtr(Stored);
184     }
185 
getDecl()186     Decl *getDecl() const {
187       assert(isDecl() && "Not a decl!");
188       return static_cast<Decl *>(Stored);
189     }
190 
191   private:
192     void *Stored;
193     bool IsType;
194   };
195 
196   /// The declarations and types to emit.
197   std::queue<DeclOrType> DeclTypesToEmit;
198 
199   /// The first ID number we can use for our own declarations.
200   serialization::DeclID FirstDeclID = serialization::NUM_PREDEF_DECL_IDS;
201 
202   /// The decl ID that will be assigned to the next new decl.
203   serialization::DeclID NextDeclID = FirstDeclID;
204 
205   /// Map that provides the ID numbers of each declaration within
206   /// the output stream, as well as those deserialized from a chained PCH.
207   ///
208   /// The ID numbers of declarations are consecutive (in order of
209   /// discovery) and start at 2. 1 is reserved for the translation
210   /// unit, while 0 is reserved for NULL.
211   llvm::DenseMap<const Decl *, serialization::DeclID> DeclIDs;
212 
213   /// Offset of each declaration in the bitstream, indexed by
214   /// the declaration's ID.
215   std::vector<serialization::DeclOffset> DeclOffsets;
216 
217   /// The offset of the DECLTYPES_BLOCK. The offsets in DeclOffsets
218   /// are relative to this value.
219   uint64_t DeclTypesBlockStartOffset = 0;
220 
221   /// Sorted (by file offset) vector of pairs of file offset/DeclID.
222   using LocDeclIDsTy =
223       SmallVector<std::pair<unsigned, serialization::DeclID>, 64>;
224   struct DeclIDInFileInfo {
225     LocDeclIDsTy DeclIDs;
226 
227     /// Set when the DeclIDs vectors from all files are joined, this
228     /// indicates the index that this particular vector has in the global one.
229     unsigned FirstDeclIndex;
230   };
231   using FileDeclIDsTy =
232       llvm::DenseMap<FileID, std::unique_ptr<DeclIDInFileInfo>>;
233 
234   /// Map from file SLocEntries to info about the file-level declarations
235   /// that it contains.
236   FileDeclIDsTy FileDeclIDs;
237 
238   void associateDeclWithFile(const Decl *D, serialization::DeclID);
239 
240   /// The first ID number we can use for our own types.
241   serialization::TypeID FirstTypeID = serialization::NUM_PREDEF_TYPE_IDS;
242 
243   /// The type ID that will be assigned to the next new type.
244   serialization::TypeID NextTypeID = FirstTypeID;
245 
246   /// Map that provides the ID numbers of each type within the
247   /// output stream, plus those deserialized from a chained PCH.
248   ///
249   /// The ID numbers of types are consecutive (in order of discovery)
250   /// and start at 1. 0 is reserved for NULL. When types are actually
251   /// stored in the stream, the ID number is shifted by 2 bits to
252   /// allow for the const/volatile qualifiers.
253   ///
254   /// Keys in the map never have const/volatile qualifiers.
255   TypeIdxMap TypeIdxs;
256 
257   /// Offset of each type in the bitstream, indexed by
258   /// the type's ID.
259   std::vector<serialization::UnderalignedInt64> TypeOffsets;
260 
261   /// The first ID number we can use for our own identifiers.
262   serialization::IdentID FirstIdentID = serialization::NUM_PREDEF_IDENT_IDS;
263 
264   /// The identifier ID that will be assigned to the next new identifier.
265   serialization::IdentID NextIdentID = FirstIdentID;
266 
267   /// Map that provides the ID numbers of each identifier in
268   /// the output stream.
269   ///
270   /// The ID numbers for identifiers are consecutive (in order of
271   /// discovery), starting at 1. An ID of zero refers to a NULL
272   /// IdentifierInfo.
273   llvm::MapVector<const IdentifierInfo *, serialization::IdentID> IdentifierIDs;
274 
275   /// The first ID number we can use for our own macros.
276   serialization::MacroID FirstMacroID = serialization::NUM_PREDEF_MACRO_IDS;
277 
278   /// The identifier ID that will be assigned to the next new identifier.
279   serialization::MacroID NextMacroID = FirstMacroID;
280 
281   /// Map that provides the ID numbers of each macro.
282   llvm::DenseMap<MacroInfo *, serialization::MacroID> MacroIDs;
283 
284   struct MacroInfoToEmitData {
285     const IdentifierInfo *Name;
286     MacroInfo *MI;
287     serialization::MacroID ID;
288   };
289 
290   /// The macro infos to emit.
291   std::vector<MacroInfoToEmitData> MacroInfosToEmit;
292 
293   llvm::DenseMap<const IdentifierInfo *, uint32_t>
294       IdentMacroDirectivesOffsetMap;
295 
296   /// @name FlushStmt Caches
297   /// @{
298 
299   /// Set of parent Stmts for the currently serializing sub-stmt.
300   llvm::DenseSet<Stmt *> ParentStmts;
301 
302   /// Offsets of sub-stmts already serialized. The offset points
303   /// just after the stmt record.
304   llvm::DenseMap<Stmt *, uint64_t> SubStmtEntries;
305 
306   /// @}
307 
308   /// Offsets of each of the identifier IDs into the identifier
309   /// table.
310   std::vector<uint32_t> IdentifierOffsets;
311 
312   /// The first ID number we can use for our own submodules.
313   serialization::SubmoduleID FirstSubmoduleID =
314       serialization::NUM_PREDEF_SUBMODULE_IDS;
315 
316   /// The submodule ID that will be assigned to the next new submodule.
317   serialization::SubmoduleID NextSubmoduleID = FirstSubmoduleID;
318 
319   /// The first ID number we can use for our own selectors.
320   serialization::SelectorID FirstSelectorID =
321       serialization::NUM_PREDEF_SELECTOR_IDS;
322 
323   /// The selector ID that will be assigned to the next new selector.
324   serialization::SelectorID NextSelectorID = FirstSelectorID;
325 
326   /// Map that provides the ID numbers of each Selector.
327   llvm::MapVector<Selector, serialization::SelectorID> SelectorIDs;
328 
329   /// Offset of each selector within the method pool/selector
330   /// table, indexed by the Selector ID (-1).
331   std::vector<uint32_t> SelectorOffsets;
332 
333   /// Mapping from macro definitions (as they occur in the preprocessing
334   /// record) to the macro IDs.
335   llvm::DenseMap<const MacroDefinitionRecord *,
336                  serialization::PreprocessedEntityID> MacroDefinitions;
337 
338   /// Cache of indices of anonymous declarations within their lexical
339   /// contexts.
340   llvm::DenseMap<const Decl *, unsigned> AnonymousDeclarationNumbers;
341 
342   /// An update to a Decl.
343   class DeclUpdate {
344     /// A DeclUpdateKind.
345     unsigned Kind;
346     union {
347       const Decl *Dcl;
348       void *Type;
349       unsigned Loc;
350       unsigned Val;
351       Module *Mod;
352       const Attr *Attribute;
353     };
354 
355   public:
DeclUpdate(unsigned Kind)356     DeclUpdate(unsigned Kind) : Kind(Kind), Dcl(nullptr) {}
DeclUpdate(unsigned Kind,const Decl * Dcl)357     DeclUpdate(unsigned Kind, const Decl *Dcl) : Kind(Kind), Dcl(Dcl) {}
DeclUpdate(unsigned Kind,QualType Type)358     DeclUpdate(unsigned Kind, QualType Type)
359         : Kind(Kind), Type(Type.getAsOpaquePtr()) {}
DeclUpdate(unsigned Kind,SourceLocation Loc)360     DeclUpdate(unsigned Kind, SourceLocation Loc)
361         : Kind(Kind), Loc(Loc.getRawEncoding()) {}
DeclUpdate(unsigned Kind,unsigned Val)362     DeclUpdate(unsigned Kind, unsigned Val) : Kind(Kind), Val(Val) {}
DeclUpdate(unsigned Kind,Module * M)363     DeclUpdate(unsigned Kind, Module *M) : Kind(Kind), Mod(M) {}
DeclUpdate(unsigned Kind,const Attr * Attribute)364     DeclUpdate(unsigned Kind, const Attr *Attribute)
365           : Kind(Kind), Attribute(Attribute) {}
366 
getKind()367     unsigned getKind() const { return Kind; }
getDecl()368     const Decl *getDecl() const { return Dcl; }
getType()369     QualType getType() const { return QualType::getFromOpaquePtr(Type); }
370 
getLoc()371     SourceLocation getLoc() const {
372       return SourceLocation::getFromRawEncoding(Loc);
373     }
374 
getNumber()375     unsigned getNumber() const { return Val; }
getModule()376     Module *getModule() const { return Mod; }
getAttr()377     const Attr *getAttr() const { return Attribute; }
378   };
379 
380   using UpdateRecord = SmallVector<DeclUpdate, 1>;
381   using DeclUpdateMap = llvm::MapVector<const Decl *, UpdateRecord>;
382 
383   /// Mapping from declarations that came from a chained PCH to the
384   /// record containing modifications to them.
385   DeclUpdateMap DeclUpdates;
386 
387   using FirstLatestDeclMap = llvm::DenseMap<Decl *, Decl *>;
388 
389   /// Map of first declarations from a chained PCH that point to the
390   /// most recent declarations in another PCH.
391   FirstLatestDeclMap FirstLatestDecls;
392 
393   /// Declarations encountered that might be external
394   /// definitions.
395   ///
396   /// We keep track of external definitions and other 'interesting' declarations
397   /// as we are emitting declarations to the AST file. The AST file contains a
398   /// separate record for these declarations, which are provided to the AST
399   /// consumer by the AST reader. This is behavior is required to properly cope with,
400   /// e.g., tentative variable definitions that occur within
401   /// headers. The declarations themselves are stored as declaration
402   /// IDs, since they will be written out to an EAGERLY_DESERIALIZED_DECLS
403   /// record.
404   SmallVector<uint64_t, 16> EagerlyDeserializedDecls;
405   SmallVector<uint64_t, 16> ModularCodegenDecls;
406 
407   /// DeclContexts that have received extensions since their serialized
408   /// form.
409   ///
410   /// For namespaces, when we're chaining and encountering a namespace, we check
411   /// if its primary namespace comes from the chain. If it does, we add the
412   /// primary to this set, so that we can write out lexical content updates for
413   /// it.
414   llvm::SmallSetVector<const DeclContext *, 16> UpdatedDeclContexts;
415 
416   /// Keeps track of declarations that we must emit, even though we're
417   /// not guaranteed to be able to find them by walking the AST starting at the
418   /// translation unit.
419   SmallVector<const Decl *, 16> DeclsToEmitEvenIfUnreferenced;
420 
421   /// The set of Objective-C class that have categories we
422   /// should serialize.
423   llvm::SetVector<ObjCInterfaceDecl *> ObjCClassesWithCategories;
424 
425   /// The set of declarations that may have redeclaration chains that
426   /// need to be serialized.
427   llvm::SmallVector<const Decl *, 16> Redeclarations;
428 
429   /// A cache of the first local declaration for "interesting"
430   /// redeclaration chains.
431   llvm::DenseMap<const Decl *, const Decl *> FirstLocalDeclCache;
432 
433   /// Mapping from SwitchCase statements to IDs.
434   llvm::DenseMap<SwitchCase *, unsigned> SwitchCaseIDs;
435 
436   /// The number of statements written to the AST file.
437   unsigned NumStatements = 0;
438 
439   /// The number of macros written to the AST file.
440   unsigned NumMacros = 0;
441 
442   /// The number of lexical declcontexts written to the AST
443   /// file.
444   unsigned NumLexicalDeclContexts = 0;
445 
446   /// The number of visible declcontexts written to the AST
447   /// file.
448   unsigned NumVisibleDeclContexts = 0;
449 
450   /// A mapping from each known submodule to its ID number, which will
451   /// be a positive integer.
452   llvm::DenseMap<Module *, unsigned> SubmoduleIDs;
453 
454   /// A list of the module file extension writers.
455   std::vector<std::unique_ptr<ModuleFileExtensionWriter>>
456       ModuleFileExtensionWriters;
457 
458   /// Retrieve or create a submodule ID for this module.
459   unsigned getSubmoduleID(Module *Mod);
460 
461   /// Write the given subexpression to the bitstream.
462   void WriteSubStmt(Stmt *S);
463 
464   void WriteBlockInfoBlock();
465   void WriteControlBlock(Preprocessor &PP, ASTContext &Context,
466                          StringRef isysroot, const std::string &OutputFile);
467 
468   /// Write out the signature and diagnostic options, and return the signature.
469   ASTFileSignature writeUnhashedControlBlock(Preprocessor &PP,
470                                              ASTContext &Context);
471 
472   /// Calculate hash of the pcm content.
473   static std::pair<ASTFileSignature, ASTFileSignature>
474   createSignature(StringRef AllBytes, StringRef ASTBlockBytes);
475 
476   void WriteInputFiles(SourceManager &SourceMgr, HeaderSearchOptions &HSOpts,
477                        bool Modules);
478   void WriteSourceManagerBlock(SourceManager &SourceMgr,
479                                const Preprocessor &PP);
480   void WritePreprocessor(const Preprocessor &PP, bool IsModule);
481   void WriteHeaderSearch(const HeaderSearch &HS);
482   void WritePreprocessorDetail(PreprocessingRecord &PPRec,
483                                uint64_t MacroOffsetsBase);
484   void WriteSubmodules(Module *WritingModule);
485 
486   void WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
487                                      bool isModule);
488 
489   unsigned TypeExtQualAbbrev = 0;
490   unsigned TypeFunctionProtoAbbrev = 0;
491   void WriteTypeAbbrevs();
492   void WriteType(QualType T);
493 
494   bool isLookupResultExternal(StoredDeclsList &Result, DeclContext *DC);
495   bool isLookupResultEntirelyExternal(StoredDeclsList &Result, DeclContext *DC);
496 
497   void GenerateNameLookupTable(const DeclContext *DC,
498                                llvm::SmallVectorImpl<char> &LookupTable);
499   uint64_t WriteDeclContextLexicalBlock(ASTContext &Context, DeclContext *DC);
500   uint64_t WriteDeclContextVisibleBlock(ASTContext &Context, DeclContext *DC);
501   void WriteTypeDeclOffsets();
502   void WriteFileDeclIDsMap();
503   void WriteComments();
504   void WriteSelectors(Sema &SemaRef);
505   void WriteReferencedSelectorsPool(Sema &SemaRef);
506   void WriteIdentifierTable(Preprocessor &PP, IdentifierResolver &IdResolver,
507                             bool IsModule);
508   void WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord);
509   void WriteDeclContextVisibleUpdate(const DeclContext *DC);
510   void WriteFPPragmaOptions(const FPOptionsOverride &Opts);
511   void WriteOpenCLExtensions(Sema &SemaRef);
512   void WriteOpenCLExtensionTypes(Sema &SemaRef);
513   void WriteOpenCLExtensionDecls(Sema &SemaRef);
514   void WriteCUDAPragmas(Sema &SemaRef);
515   void WriteObjCCategories();
516   void WriteLateParsedTemplates(Sema &SemaRef);
517   void WriteOptimizePragmaOptions(Sema &SemaRef);
518   void WriteMSStructPragmaOptions(Sema &SemaRef);
519   void WriteMSPointersToMembersPragmaOptions(Sema &SemaRef);
520   void WritePackPragmaOptions(Sema &SemaRef);
521   void WriteFloatControlPragmaOptions(Sema &SemaRef);
522   void WriteModuleFileExtension(Sema &SemaRef,
523                                 ModuleFileExtensionWriter &Writer);
524 
525   unsigned DeclParmVarAbbrev = 0;
526   unsigned DeclContextLexicalAbbrev = 0;
527   unsigned DeclContextVisibleLookupAbbrev = 0;
528   unsigned UpdateVisibleAbbrev = 0;
529   unsigned DeclRecordAbbrev = 0;
530   unsigned DeclTypedefAbbrev = 0;
531   unsigned DeclVarAbbrev = 0;
532   unsigned DeclFieldAbbrev = 0;
533   unsigned DeclEnumAbbrev = 0;
534   unsigned DeclObjCIvarAbbrev = 0;
535   unsigned DeclCXXMethodAbbrev = 0;
536 
537   unsigned DeclRefExprAbbrev = 0;
538   unsigned CharacterLiteralAbbrev = 0;
539   unsigned IntegerLiteralAbbrev = 0;
540   unsigned ExprImplicitCastAbbrev = 0;
541 
542   void WriteDeclAbbrevs();
543   void WriteDecl(ASTContext &Context, Decl *D);
544 
545   ASTFileSignature WriteASTCore(Sema &SemaRef, StringRef isysroot,
546                                 const std::string &OutputFile,
547                                 Module *WritingModule);
548 
549 public:
550   /// Create a new precompiled header writer that outputs to
551   /// the given bitstream.
552   ASTWriter(llvm::BitstreamWriter &Stream, SmallVectorImpl<char> &Buffer,
553             InMemoryModuleCache &ModuleCache,
554             ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
555             bool IncludeTimestamps = true);
556   ~ASTWriter() override;
557 
558   const LangOptions &getLangOpts() const;
559 
560   /// Get a timestamp for output into the AST file. The actual timestamp
561   /// of the specified file may be ignored if we have been instructed to not
562   /// include timestamps in the output file.
563   time_t getTimestampForOutput(const FileEntry *E) const;
564 
565   /// Write a precompiled header for the given semantic analysis.
566   ///
567   /// \param SemaRef a reference to the semantic analysis object that processed
568   /// the AST to be written into the precompiled header.
569   ///
570   /// \param WritingModule The module that we are writing. If null, we are
571   /// writing a precompiled header.
572   ///
573   /// \param isysroot if non-empty, write a relocatable file whose headers
574   /// are relative to the given system root. If we're writing a module, its
575   /// build directory will be used in preference to this if both are available.
576   ///
577   /// \return the module signature, which eventually will be a hash of
578   /// the module but currently is merely a random 32-bit number.
579   ASTFileSignature WriteAST(Sema &SemaRef, const std::string &OutputFile,
580                             Module *WritingModule, StringRef isysroot,
581                             bool hasErrors = false,
582                             bool ShouldCacheASTInMemory = false);
583 
584   /// Emit a token.
585   void AddToken(const Token &Tok, RecordDataImpl &Record);
586 
587   /// Emit a source location.
588   void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record);
589 
590   /// Emit a source range.
591   void AddSourceRange(SourceRange Range, RecordDataImpl &Record);
592 
593   /// Emit a reference to an identifier.
594   void AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record);
595 
596   /// Get the unique number used to refer to the given selector.
597   serialization::SelectorID getSelectorRef(Selector Sel);
598 
599   /// Get the unique number used to refer to the given identifier.
600   serialization::IdentID getIdentifierRef(const IdentifierInfo *II);
601 
602   /// Get the unique number used to refer to the given macro.
603   serialization::MacroID getMacroRef(MacroInfo *MI, const IdentifierInfo *Name);
604 
605   /// Determine the ID of an already-emitted macro.
606   serialization::MacroID getMacroID(MacroInfo *MI);
607 
608   uint32_t getMacroDirectivesOffset(const IdentifierInfo *Name);
609 
610   /// Emit a reference to a type.
611   void AddTypeRef(QualType T, RecordDataImpl &Record);
612 
613   /// Force a type to be emitted and get its ID.
614   serialization::TypeID GetOrCreateTypeID(QualType T);
615 
616   /// Determine the type ID of an already-emitted type.
617   serialization::TypeID getTypeID(QualType T) const;
618 
619   /// Find the first local declaration of a given local redeclarable
620   /// decl.
621   const Decl *getFirstLocalDecl(const Decl *D);
622 
623   /// Is this a local declaration (that is, one that will be written to
624   /// our AST file)? This is the case for declarations that are neither imported
625   /// from another AST file nor predefined.
IsLocalDecl(const Decl * D)626   bool IsLocalDecl(const Decl *D) {
627     if (D->isFromASTFile())
628       return false;
629     auto I = DeclIDs.find(D);
630     return (I == DeclIDs.end() ||
631             I->second >= serialization::NUM_PREDEF_DECL_IDS);
632   };
633 
634   /// Emit a reference to a declaration.
635   void AddDeclRef(const Decl *D, RecordDataImpl &Record);
636 
637   /// Force a declaration to be emitted and get its ID.
638   serialization::DeclID GetDeclRef(const Decl *D);
639 
640   /// Determine the declaration ID of an already-emitted
641   /// declaration.
642   serialization::DeclID getDeclID(const Decl *D);
643 
644   unsigned getAnonymousDeclarationNumber(const NamedDecl *D);
645 
646   /// Add a string to the given record.
647   void AddString(StringRef Str, RecordDataImpl &Record);
648 
649   /// Convert a path from this build process into one that is appropriate
650   /// for emission in the module file.
651   bool PreparePathForOutput(SmallVectorImpl<char> &Path);
652 
653   /// Add a path to the given record.
654   void AddPath(StringRef Path, RecordDataImpl &Record);
655 
656   /// Emit the current record with the given path as a blob.
657   void EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
658                           StringRef Path);
659 
660   /// Add a version tuple to the given record
661   void AddVersionTuple(const VersionTuple &Version, RecordDataImpl &Record);
662 
663   /// Retrieve or create a submodule ID for this module, or return 0 if
664   /// the submodule is neither local (a submodle of the currently-written module)
665   /// nor from an imported module.
666   unsigned getLocalOrImportedSubmoduleID(Module *Mod);
667 
668   /// Note that the identifier II occurs at the given offset
669   /// within the identifier table.
670   void SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset);
671 
672   /// Note that the selector Sel occurs at the given offset
673   /// within the method pool/selector table.
674   void SetSelectorOffset(Selector Sel, uint32_t Offset);
675 
676   /// Record an ID for the given switch-case statement.
677   unsigned RecordSwitchCaseID(SwitchCase *S);
678 
679   /// Retrieve the ID for the given switch-case statement.
680   unsigned getSwitchCaseID(SwitchCase *S);
681 
682   void ClearSwitchCaseIDs();
683 
getTypeExtQualAbbrev()684   unsigned getTypeExtQualAbbrev() const {
685     return TypeExtQualAbbrev;
686   }
687 
getTypeFunctionProtoAbbrev()688   unsigned getTypeFunctionProtoAbbrev() const {
689     return TypeFunctionProtoAbbrev;
690   }
691 
getDeclParmVarAbbrev()692   unsigned getDeclParmVarAbbrev() const { return DeclParmVarAbbrev; }
getDeclRecordAbbrev()693   unsigned getDeclRecordAbbrev() const { return DeclRecordAbbrev; }
getDeclTypedefAbbrev()694   unsigned getDeclTypedefAbbrev() const { return DeclTypedefAbbrev; }
getDeclVarAbbrev()695   unsigned getDeclVarAbbrev() const { return DeclVarAbbrev; }
getDeclFieldAbbrev()696   unsigned getDeclFieldAbbrev() const { return DeclFieldAbbrev; }
getDeclEnumAbbrev()697   unsigned getDeclEnumAbbrev() const { return DeclEnumAbbrev; }
getDeclObjCIvarAbbrev()698   unsigned getDeclObjCIvarAbbrev() const { return DeclObjCIvarAbbrev; }
getDeclCXXMethodAbbrev()699   unsigned getDeclCXXMethodAbbrev() const { return DeclCXXMethodAbbrev; }
700 
getDeclRefExprAbbrev()701   unsigned getDeclRefExprAbbrev() const { return DeclRefExprAbbrev; }
getCharacterLiteralAbbrev()702   unsigned getCharacterLiteralAbbrev() const { return CharacterLiteralAbbrev; }
getIntegerLiteralAbbrev()703   unsigned getIntegerLiteralAbbrev() const { return IntegerLiteralAbbrev; }
getExprImplicitCastAbbrev()704   unsigned getExprImplicitCastAbbrev() const { return ExprImplicitCastAbbrev; }
705 
hasChain()706   bool hasChain() const { return Chain; }
getChain()707   ASTReader *getChain() const { return Chain; }
708 
709 private:
710   // ASTDeserializationListener implementation
711   void ReaderInitialized(ASTReader *Reader) override;
712   void IdentifierRead(serialization::IdentID ID, IdentifierInfo *II) override;
713   void MacroRead(serialization::MacroID ID, MacroInfo *MI) override;
714   void TypeRead(serialization::TypeIdx Idx, QualType T) override;
715   void SelectorRead(serialization::SelectorID ID, Selector Sel) override;
716   void MacroDefinitionRead(serialization::PreprocessedEntityID ID,
717                            MacroDefinitionRecord *MD) override;
718   void ModuleRead(serialization::SubmoduleID ID, Module *Mod) override;
719 
720   // ASTMutationListener implementation.
721   void CompletedTagDefinition(const TagDecl *D) override;
722   void AddedVisibleDecl(const DeclContext *DC, const Decl *D) override;
723   void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) override;
724   void AddedCXXTemplateSpecialization(
725       const ClassTemplateDecl *TD,
726       const ClassTemplateSpecializationDecl *D) override;
727   void AddedCXXTemplateSpecialization(
728       const VarTemplateDecl *TD,
729       const VarTemplateSpecializationDecl *D) override;
730   void AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
731                                       const FunctionDecl *D) override;
732   void ResolvedExceptionSpec(const FunctionDecl *FD) override;
733   void DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) override;
734   void ResolvedOperatorDelete(const CXXDestructorDecl *DD,
735                               const FunctionDecl *Delete,
736                               Expr *ThisArg) override;
737   void CompletedImplicitDefinition(const FunctionDecl *D) override;
738   void InstantiationRequested(const ValueDecl *D) override;
739   void VariableDefinitionInstantiated(const VarDecl *D) override;
740   void FunctionDefinitionInstantiated(const FunctionDecl *D) override;
741   void DefaultArgumentInstantiated(const ParmVarDecl *D) override;
742   void DefaultMemberInitializerInstantiated(const FieldDecl *D) override;
743   void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
744                                     const ObjCInterfaceDecl *IFD) override;
745   void DeclarationMarkedUsed(const Decl *D) override;
746   void DeclarationMarkedOpenMPThreadPrivate(const Decl *D) override;
747   void DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
748                                             const Attr *Attr) override;
749   void DeclarationMarkedOpenMPAllocate(const Decl *D, const Attr *A) override;
750   void RedefinedHiddenDefinition(const NamedDecl *D, Module *M) override;
751   void AddedAttributeToRecord(const Attr *Attr,
752                               const RecordDecl *Record) override;
753 };
754 
755 /// AST and semantic-analysis consumer that generates a
756 /// precompiled header from the parsed source code.
757 class PCHGenerator : public SemaConsumer {
758   const Preprocessor &PP;
759   std::string OutputFile;
760   std::string isysroot;
761   Sema *SemaPtr;
762   std::shared_ptr<PCHBuffer> Buffer;
763   llvm::BitstreamWriter Stream;
764   ASTWriter Writer;
765   bool AllowASTWithErrors;
766   bool ShouldCacheASTInMemory;
767 
768 protected:
getWriter()769   ASTWriter &getWriter() { return Writer; }
getWriter()770   const ASTWriter &getWriter() const { return Writer; }
getPCH()771   SmallVectorImpl<char> &getPCH() const { return Buffer->Data; }
772 
773 public:
774   PCHGenerator(const Preprocessor &PP, InMemoryModuleCache &ModuleCache,
775                StringRef OutputFile, StringRef isysroot,
776                std::shared_ptr<PCHBuffer> Buffer,
777                ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
778                bool AllowASTWithErrors = false, bool IncludeTimestamps = true,
779                bool ShouldCacheASTInMemory = false);
780   ~PCHGenerator() override;
781 
InitializeSema(Sema & S)782   void InitializeSema(Sema &S) override { SemaPtr = &S; }
783   void HandleTranslationUnit(ASTContext &Ctx) override;
784   ASTMutationListener *GetASTMutationListener() override;
785   ASTDeserializationListener *GetASTDeserializationListener() override;
hasEmittedPCH()786   bool hasEmittedPCH() const { return Buffer->IsComplete; }
787 };
788 
789 } // namespace clang
790 
791 #endif // LLVM_CLANG_SERIALIZATION_ASTWRITER_H
792