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/Module.h"
22#include "clang/Basic/SourceLocation.h"
23#include "clang/Sema/Sema.h"
24#include "clang/Sema/SemaConsumer.h"
25#include "clang/Serialization/ASTBitCodes.h"
26#include "clang/Serialization/ASTDeserializationListener.h"
27#include "clang/Serialization/PCHContainerOperations.h"
28#include "clang/Serialization/SourceLocationEncoding.h"
29#include "llvm/ADT/ArrayRef.h"
30#include "llvm/ADT/DenseMap.h"
31#include "llvm/ADT/DenseSet.h"
32#include "llvm/ADT/MapVector.h"
33#include "llvm/ADT/STLExtras.h"
34#include "llvm/ADT/SetVector.h"
35#include "llvm/ADT/SmallVector.h"
36#include "llvm/ADT/StringRef.h"
37#include "llvm/Bitstream/BitstreamWriter.h"
38#include <cassert>
39#include <cstddef>
40#include <cstdint>
41#include <ctime>
42#include <memory>
43#include <queue>
44#include <string>
45#include <utility>
46#include <vector>
47
48namespace clang {
49
50class ASTContext;
51class ASTReader;
52class Attr;
53class CXXRecordDecl;
54class FileEntry;
55class FPOptionsOverride;
56class FunctionDecl;
57class HeaderSearch;
58class HeaderSearchOptions;
59class IdentifierResolver;
60class LangOptions;
61class MacroDefinitionRecord;
62class MacroInfo;
63class Module;
64class InMemoryModuleCache;
65class ModuleFileExtension;
66class ModuleFileExtensionWriter;
67class NamedDecl;
68class ObjCInterfaceDecl;
69class PreprocessingRecord;
70class Preprocessor;
71class RecordDecl;
72class Sema;
73class SourceManager;
74class Stmt;
75class StoredDeclsList;
76class SwitchCase;
77class Token;
78
79/// Writes an AST file containing the contents of a translation unit.
80///
81/// The ASTWriter class produces a bitstream containing the serialized
82/// representation of a given abstract syntax tree and its supporting
83/// data structures. This bitstream can be de-serialized via an
84/// instance of the ASTReader class.
85class ASTWriter : public ASTDeserializationListener,
86 public ASTMutationListener {
87public:
88 friend class ASTDeclWriter;
89 friend class ASTRecordWriter;
90
91 using RecordData = SmallVector<uint64_t, 64>;
92 using RecordDataImpl = SmallVectorImpl<uint64_t>;
93 using RecordDataRef = ArrayRef<uint64_t>;
94
95private:
96 /// 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 using TypeIdxMap = llvm::DenseMap<QualType, serialization::TypeIdx,
106 serialization::UnsafeQualTypeDenseMapInfo>;
107
108 using LocSeq = SourceLocationSequence;
109
110 /// The bitstream writer used to emit this precompiled header.
111 llvm::BitstreamWriter &Stream;
112
113 /// The buffer associated with the bitstream.
114 const SmallVectorImpl<char> &Buffer;
115
116 /// The PCM manager which manages memory buffers for pcm files.
117 InMemoryModuleCache &ModuleCache;
118
119 /// The ASTContext we're writing.
120 ASTContext *Context = nullptr;
121
122 /// The preprocessor we're writing.
123 Preprocessor *PP = nullptr;
124
125 /// The reader of existing AST files, if we're chaining.
126 ASTReader *Chain = nullptr;
127
128 /// The module we're currently writing, if any.
129 Module *WritingModule = nullptr;
130
131 /// The byte range representing all the UNHASHED_CONTROL_BLOCK.
132 std::pair<uint64_t, uint64_t> UnhashedControlBlockRange;
133 /// The bit offset of the AST block hash blob.
134 uint64_t ASTBlockHashOffset = 0;
135 /// The bit offset of the signature blob.
136 uint64_t SignatureOffset = 0;
137
138 /// The bit offset of the first bit inside the AST_BLOCK.
139 uint64_t ASTBlockStartOffset = 0;
140
141 /// The byte range representing all the AST_BLOCK.
142 std::pair<uint64_t, uint64_t> ASTBlockRange;
143
144 /// The base directory for any relative paths we emit.
145 std::string BaseDirectory;
146
147 /// Indicates whether timestamps should be written to the produced
148 /// module file. This is the case for files implicitly written to the
149 /// module cache, where we need the timestamps to determine if the module
150 /// file is up to date, but not otherwise.
151 bool IncludeTimestamps;
152
153 /// Indicates whether the AST file being written is an implicit module.
154 /// If that's the case, we may be able to skip writing some information that
155 /// are guaranteed to be the same in the importer by the context hash.
156 bool BuildingImplicitModule = false;
157
158 /// Indicates when the AST writing is actively performing
159 /// serialization, rather than just queueing updates.
160 bool WritingAST = false;
161
162 /// Indicates that we are done serializing the collection of decls
163 /// and types to emit.
164 bool DoneWritingDeclsAndTypes = false;
165
166 /// Indicates that the AST contained compiler errors.
167 bool ASTHasCompilerErrors = false;
168
169 /// Indicates that we're going to generate the reduced BMI for C++20
170 /// named modules.
171 bool GeneratingReducedBMI = false;
172
173 /// Mapping from input file entries to the index into the
174 /// offset table where information about that input file is stored.
175 llvm::DenseMap<const FileEntry *, uint32_t> InputFileIDs;
176
177 /// Stores a declaration or a type to be written to the AST file.
178 class DeclOrType {
179 public:
180 DeclOrType(Decl *D) : Stored(D), IsType(false) {}
181 DeclOrType(QualType T) : Stored(T.getAsOpaquePtr()), IsType(true) {}
182
183 bool isType() const { return IsType; }
184 bool isDecl() const { return !IsType; }
185
186 QualType getType() const {
187 assert(isType() && "Not a type!");
188 return QualType::getFromOpaquePtr(Ptr: Stored);
189 }
190
191 Decl *getDecl() const {
192 assert(isDecl() && "Not a decl!");
193 return static_cast<Decl *>(Stored);
194 }
195
196 private:
197 void *Stored;
198 bool IsType;
199 };
200
201 /// The declarations and types to emit.
202 std::queue<DeclOrType> DeclTypesToEmit;
203
204 /// The delayed namespace to emit. Only meaningful for reduced BMI.
205 ///
206 /// In reduced BMI, we want to elide the unreachable declarations in
207 /// the global module fragment. However, in ASTWriterDecl, when we see
208 /// a namespace, all the declarations in the namespace would be emitted.
209 /// So the optimization become meaningless. To solve the issue, we
210 /// delay recording all the declarations until we emit all the declarations.
211 /// Then we can safely record the reached declarations only.
212 llvm::SmallVector<NamespaceDecl *, 16> DelayedNamespace;
213
214 /// The first ID number we can use for our own declarations.
215 serialization::DeclID FirstDeclID = serialization::NUM_PREDEF_DECL_IDS;
216
217 /// The decl ID that will be assigned to the next new decl.
218 serialization::DeclID NextDeclID = FirstDeclID;
219
220 /// Map that provides the ID numbers of each declaration within
221 /// the output stream, as well as those deserialized from a chained PCH.
222 ///
223 /// The ID numbers of declarations are consecutive (in order of
224 /// discovery) and start at 2. 1 is reserved for the translation
225 /// unit, while 0 is reserved for NULL.
226 llvm::DenseMap<const Decl *, serialization::DeclID> DeclIDs;
227
228 /// Offset of each declaration in the bitstream, indexed by
229 /// the declaration's ID.
230 std::vector<serialization::DeclOffset> DeclOffsets;
231
232 /// The offset of the DECLTYPES_BLOCK. The offsets in DeclOffsets
233 /// are relative to this value.
234 uint64_t DeclTypesBlockStartOffset = 0;
235
236 /// Sorted (by file offset) vector of pairs of file offset/DeclID.
237 using LocDeclIDsTy =
238 SmallVector<std::pair<unsigned, serialization::DeclID>, 64>;
239 struct DeclIDInFileInfo {
240 LocDeclIDsTy DeclIDs;
241
242 /// Set when the DeclIDs vectors from all files are joined, this
243 /// indicates the index that this particular vector has in the global one.
244 unsigned FirstDeclIndex;
245 };
246 using FileDeclIDsTy =
247 llvm::DenseMap<FileID, std::unique_ptr<DeclIDInFileInfo>>;
248
249 /// Map from file SLocEntries to info about the file-level declarations
250 /// that it contains.
251 FileDeclIDsTy FileDeclIDs;
252
253 void associateDeclWithFile(const Decl *D, serialization::DeclID);
254
255 /// The first ID number we can use for our own types.
256 serialization::TypeID FirstTypeID = serialization::NUM_PREDEF_TYPE_IDS;
257
258 /// The type ID that will be assigned to the next new type.
259 serialization::TypeID NextTypeID = FirstTypeID;
260
261 /// Map that provides the ID numbers of each type within the
262 /// output stream, plus those deserialized from a chained PCH.
263 ///
264 /// The ID numbers of types are consecutive (in order of discovery)
265 /// and start at 1. 0 is reserved for NULL. When types are actually
266 /// stored in the stream, the ID number is shifted by 2 bits to
267 /// allow for the const/volatile qualifiers.
268 ///
269 /// Keys in the map never have const/volatile qualifiers.
270 TypeIdxMap TypeIdxs;
271
272 /// Offset of each type in the bitstream, indexed by
273 /// the type's ID.
274 std::vector<serialization::UnderalignedInt64> TypeOffsets;
275
276 /// The first ID number we can use for our own identifiers.
277 serialization::IdentID FirstIdentID = serialization::NUM_PREDEF_IDENT_IDS;
278
279 /// The identifier ID that will be assigned to the next new identifier.
280 serialization::IdentID NextIdentID = FirstIdentID;
281
282 /// Map that provides the ID numbers of each identifier in
283 /// the output stream.
284 ///
285 /// The ID numbers for identifiers are consecutive (in order of
286 /// discovery), starting at 1. An ID of zero refers to a NULL
287 /// IdentifierInfo.
288 llvm::MapVector<const IdentifierInfo *, serialization::IdentID> IdentifierIDs;
289
290 /// The first ID number we can use for our own macros.
291 serialization::MacroID FirstMacroID = serialization::NUM_PREDEF_MACRO_IDS;
292
293 /// The identifier ID that will be assigned to the next new identifier.
294 serialization::MacroID NextMacroID = FirstMacroID;
295
296 /// Map that provides the ID numbers of each macro.
297 llvm::DenseMap<MacroInfo *, serialization::MacroID> MacroIDs;
298
299 struct MacroInfoToEmitData {
300 const IdentifierInfo *Name;
301 MacroInfo *MI;
302 serialization::MacroID ID;
303 };
304
305 /// The macro infos to emit.
306 std::vector<MacroInfoToEmitData> MacroInfosToEmit;
307
308 llvm::DenseMap<const IdentifierInfo *, uint32_t>
309 IdentMacroDirectivesOffsetMap;
310
311 /// @name FlushStmt Caches
312 /// @{
313
314 /// Set of parent Stmts for the currently serializing sub-stmt.
315 llvm::DenseSet<Stmt *> ParentStmts;
316
317 /// Offsets of sub-stmts already serialized. The offset points
318 /// just after the stmt record.
319 llvm::DenseMap<Stmt *, uint64_t> SubStmtEntries;
320
321 /// @}
322
323 /// Offsets of each of the identifier IDs into the identifier
324 /// table.
325 std::vector<uint32_t> IdentifierOffsets;
326
327 /// The first ID number we can use for our own submodules.
328 serialization::SubmoduleID FirstSubmoduleID =
329 serialization::NUM_PREDEF_SUBMODULE_IDS;
330
331 /// The submodule ID that will be assigned to the next new submodule.
332 serialization::SubmoduleID NextSubmoduleID = FirstSubmoduleID;
333
334 /// The first ID number we can use for our own selectors.
335 serialization::SelectorID FirstSelectorID =
336 serialization::NUM_PREDEF_SELECTOR_IDS;
337
338 /// The selector ID that will be assigned to the next new selector.
339 serialization::SelectorID NextSelectorID = FirstSelectorID;
340
341 /// Map that provides the ID numbers of each Selector.
342 llvm::MapVector<Selector, serialization::SelectorID> SelectorIDs;
343
344 /// Offset of each selector within the method pool/selector
345 /// table, indexed by the Selector ID (-1).
346 std::vector<uint32_t> SelectorOffsets;
347
348 /// Mapping from macro definitions (as they occur in the preprocessing
349 /// record) to the macro IDs.
350 llvm::DenseMap<const MacroDefinitionRecord *,
351 serialization::PreprocessedEntityID> MacroDefinitions;
352
353 /// Cache of indices of anonymous declarations within their lexical
354 /// contexts.
355 llvm::DenseMap<const Decl *, unsigned> AnonymousDeclarationNumbers;
356
357 /// An update to a Decl.
358 class DeclUpdate {
359 /// A DeclUpdateKind.
360 unsigned Kind;
361 union {
362 const Decl *Dcl;
363 void *Type;
364 SourceLocation::UIntTy Loc;
365 unsigned Val;
366 Module *Mod;
367 const Attr *Attribute;
368 };
369
370 public:
371 DeclUpdate(unsigned Kind) : Kind(Kind), Dcl(nullptr) {}
372 DeclUpdate(unsigned Kind, const Decl *Dcl) : Kind(Kind), Dcl(Dcl) {}
373 DeclUpdate(unsigned Kind, QualType Type)
374 : Kind(Kind), Type(Type.getAsOpaquePtr()) {}
375 DeclUpdate(unsigned Kind, SourceLocation Loc)
376 : Kind(Kind), Loc(Loc.getRawEncoding()) {}
377 DeclUpdate(unsigned Kind, unsigned Val) : Kind(Kind), Val(Val) {}
378 DeclUpdate(unsigned Kind, Module *M) : Kind(Kind), Mod(M) {}
379 DeclUpdate(unsigned Kind, const Attr *Attribute)
380 : Kind(Kind), Attribute(Attribute) {}
381
382 unsigned getKind() const { return Kind; }
383 const Decl *getDecl() const { return Dcl; }
384 QualType getType() const { return QualType::getFromOpaquePtr(Ptr: Type); }
385
386 SourceLocation getLoc() const {
387 return SourceLocation::getFromRawEncoding(Encoding: Loc);
388 }
389
390 unsigned getNumber() const { return Val; }
391 Module *getModule() const { return Mod; }
392 const Attr *getAttr() const { return Attribute; }
393 };
394
395 using UpdateRecord = SmallVector<DeclUpdate, 1>;
396 using DeclUpdateMap = llvm::MapVector<const Decl *, UpdateRecord>;
397
398 /// Mapping from declarations that came from a chained PCH to the
399 /// record containing modifications to them.
400 DeclUpdateMap DeclUpdates;
401
402 /// DeclUpdates added during parsing the GMF. We split these from
403 /// DeclUpdates since we want to add these updates in GMF on need.
404 /// Only meaningful for reduced BMI.
405 DeclUpdateMap DeclUpdatesFromGMF;
406
407 using FirstLatestDeclMap = llvm::DenseMap<Decl *, Decl *>;
408
409 /// Map of first declarations from a chained PCH that point to the
410 /// most recent declarations in another PCH.
411 FirstLatestDeclMap FirstLatestDecls;
412
413 /// Declarations encountered that might be external
414 /// definitions.
415 ///
416 /// We keep track of external definitions and other 'interesting' declarations
417 /// as we are emitting declarations to the AST file. The AST file contains a
418 /// separate record for these declarations, which are provided to the AST
419 /// consumer by the AST reader. This is behavior is required to properly cope with,
420 /// e.g., tentative variable definitions that occur within
421 /// headers. The declarations themselves are stored as declaration
422 /// IDs, since they will be written out to an EAGERLY_DESERIALIZED_DECLS
423 /// record.
424 SmallVector<serialization::DeclID, 16> EagerlyDeserializedDecls;
425 SmallVector<serialization::DeclID, 16> ModularCodegenDecls;
426
427 /// DeclContexts that have received extensions since their serialized
428 /// form.
429 ///
430 /// For namespaces, when we're chaining and encountering a namespace, we check
431 /// if its primary namespace comes from the chain. If it does, we add the
432 /// primary to this set, so that we can write out lexical content updates for
433 /// it.
434 llvm::SmallSetVector<const DeclContext *, 16> UpdatedDeclContexts;
435
436 /// Keeps track of declarations that we must emit, even though we're
437 /// not guaranteed to be able to find them by walking the AST starting at the
438 /// translation unit.
439 SmallVector<const Decl *, 16> DeclsToEmitEvenIfUnreferenced;
440
441 /// The set of Objective-C class that have categories we
442 /// should serialize.
443 llvm::SetVector<ObjCInterfaceDecl *> ObjCClassesWithCategories;
444
445 /// The set of declarations that may have redeclaration chains that
446 /// need to be serialized.
447 llvm::SmallVector<const Decl *, 16> Redeclarations;
448
449 /// A cache of the first local declaration for "interesting"
450 /// redeclaration chains.
451 llvm::DenseMap<const Decl *, const Decl *> FirstLocalDeclCache;
452
453 /// Mapping from SwitchCase statements to IDs.
454 llvm::DenseMap<SwitchCase *, unsigned> SwitchCaseIDs;
455
456 /// The number of statements written to the AST file.
457 unsigned NumStatements = 0;
458
459 /// The number of macros written to the AST file.
460 unsigned NumMacros = 0;
461
462 /// The number of lexical declcontexts written to the AST
463 /// file.
464 unsigned NumLexicalDeclContexts = 0;
465
466 /// The number of visible declcontexts written to the AST
467 /// file.
468 unsigned NumVisibleDeclContexts = 0;
469
470 /// A mapping from each known submodule to its ID number, which will
471 /// be a positive integer.
472 llvm::DenseMap<const Module *, unsigned> SubmoduleIDs;
473
474 /// A list of the module file extension writers.
475 std::vector<std::unique_ptr<ModuleFileExtensionWriter>>
476 ModuleFileExtensionWriters;
477
478 /// Mapping from a source location entry to whether it is affecting or not.
479 llvm::BitVector IsSLocAffecting;
480
481 /// Mapping from \c FileID to an index into the FileID adjustment table.
482 std::vector<FileID> NonAffectingFileIDs;
483 std::vector<unsigned> NonAffectingFileIDAdjustments;
484
485 /// Mapping from an offset to an index into the offset adjustment table.
486 std::vector<SourceRange> NonAffectingRanges;
487 std::vector<SourceLocation::UIntTy> NonAffectingOffsetAdjustments;
488
489 /// Computes input files that didn't affect compilation of the current module,
490 /// and initializes data structures necessary for leaving those files out
491 /// during \c SourceManager serialization.
492 void computeNonAffectingInputFiles();
493
494 /// Returns an adjusted \c FileID, accounting for any non-affecting input
495 /// files.
496 FileID getAdjustedFileID(FileID FID) const;
497 /// Returns an adjusted number of \c FileIDs created within the specified \c
498 /// FileID, accounting for any non-affecting input files.
499 unsigned getAdjustedNumCreatedFIDs(FileID FID) const;
500 /// Returns an adjusted \c SourceLocation, accounting for any non-affecting
501 /// input files.
502 SourceLocation getAdjustedLocation(SourceLocation Loc) const;
503 /// Returns an adjusted \c SourceRange, accounting for any non-affecting input
504 /// files.
505 SourceRange getAdjustedRange(SourceRange Range) const;
506 /// Returns an adjusted \c SourceLocation offset, accounting for any
507 /// non-affecting input files.
508 SourceLocation::UIntTy getAdjustedOffset(SourceLocation::UIntTy Offset) const;
509 /// Returns an adjustment for offset into SourceManager, accounting for any
510 /// non-affecting input files.
511 SourceLocation::UIntTy getAdjustment(SourceLocation::UIntTy Offset) const;
512
513 /// Retrieve or create a submodule ID for this module.
514 unsigned getSubmoduleID(Module *Mod);
515
516 /// Write the given subexpression to the bitstream.
517 void WriteSubStmt(Stmt *S);
518
519 void WriteBlockInfoBlock();
520 void WriteControlBlock(Preprocessor &PP, ASTContext &Context,
521 StringRef isysroot);
522
523 /// Write out the signature and diagnostic options, and return the signature.
524 void writeUnhashedControlBlock(Preprocessor &PP, ASTContext &Context);
525 ASTFileSignature backpatchSignature();
526
527 /// Calculate hash of the pcm content.
528 std::pair<ASTFileSignature, ASTFileSignature> createSignature() const;
529
530 void WriteInputFiles(SourceManager &SourceMgr, HeaderSearchOptions &HSOpts);
531 void WriteSourceManagerBlock(SourceManager &SourceMgr,
532 const Preprocessor &PP);
533 void WritePreprocessor(const Preprocessor &PP, bool IsModule);
534 void WriteHeaderSearch(const HeaderSearch &HS);
535 void WritePreprocessorDetail(PreprocessingRecord &PPRec,
536 uint64_t MacroOffsetsBase);
537 void WriteSubmodules(Module *WritingModule);
538
539 void WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
540 bool isModule);
541
542 unsigned TypeExtQualAbbrev = 0;
543 void WriteTypeAbbrevs();
544 void WriteType(QualType T);
545
546 bool isLookupResultExternal(StoredDeclsList &Result, DeclContext *DC);
547 bool isLookupResultEntirelyExternalOrUnreachable(StoredDeclsList &Result,
548 DeclContext *DC);
549
550 void GenerateNameLookupTable(const DeclContext *DC,
551 llvm::SmallVectorImpl<char> &LookupTable);
552 uint64_t WriteDeclContextLexicalBlock(ASTContext &Context, DeclContext *DC);
553 uint64_t WriteDeclContextVisibleBlock(ASTContext &Context, DeclContext *DC);
554 void WriteTypeDeclOffsets();
555 void WriteFileDeclIDsMap();
556 void WriteComments();
557 void WriteSelectors(Sema &SemaRef);
558 void WriteReferencedSelectorsPool(Sema &SemaRef);
559 void WriteIdentifierTable(Preprocessor &PP, IdentifierResolver &IdResolver,
560 bool IsModule);
561 void WriteDeclAndTypes(ASTContext &Context);
562 void PrepareWritingSpecialDecls(Sema &SemaRef);
563 void WriteSpecialDeclRecords(Sema &SemaRef);
564 void WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord);
565 void WriteDeclContextVisibleUpdate(const DeclContext *DC);
566 void WriteFPPragmaOptions(const FPOptionsOverride &Opts);
567 void WriteOpenCLExtensions(Sema &SemaRef);
568 void WriteCUDAPragmas(Sema &SemaRef);
569 void WriteObjCCategories();
570 void WriteLateParsedTemplates(Sema &SemaRef);
571 void WriteOptimizePragmaOptions(Sema &SemaRef);
572 void WriteMSStructPragmaOptions(Sema &SemaRef);
573 void WriteMSPointersToMembersPragmaOptions(Sema &SemaRef);
574 void WritePackPragmaOptions(Sema &SemaRef);
575 void WriteFloatControlPragmaOptions(Sema &SemaRef);
576 void WriteModuleFileExtension(Sema &SemaRef,
577 ModuleFileExtensionWriter &Writer);
578
579 unsigned DeclParmVarAbbrev = 0;
580 unsigned DeclContextLexicalAbbrev = 0;
581 unsigned DeclContextVisibleLookupAbbrev = 0;
582 unsigned UpdateVisibleAbbrev = 0;
583 unsigned DeclRecordAbbrev = 0;
584 unsigned DeclTypedefAbbrev = 0;
585 unsigned DeclVarAbbrev = 0;
586 unsigned DeclFieldAbbrev = 0;
587 unsigned DeclEnumAbbrev = 0;
588 unsigned DeclObjCIvarAbbrev = 0;
589 unsigned DeclCXXMethodAbbrev = 0;
590 unsigned DeclDependentNonTemplateCXXMethodAbbrev = 0;
591 unsigned DeclTemplateCXXMethodAbbrev = 0;
592 unsigned DeclMemberSpecializedCXXMethodAbbrev = 0;
593 unsigned DeclTemplateSpecializedCXXMethodAbbrev = 0;
594 unsigned DeclDependentSpecializationCXXMethodAbbrev = 0;
595 unsigned DeclTemplateTypeParmAbbrev = 0;
596 unsigned DeclUsingShadowAbbrev = 0;
597
598 unsigned DeclRefExprAbbrev = 0;
599 unsigned CharacterLiteralAbbrev = 0;
600 unsigned IntegerLiteralAbbrev = 0;
601 unsigned ExprImplicitCastAbbrev = 0;
602 unsigned BinaryOperatorAbbrev = 0;
603 unsigned CompoundAssignOperatorAbbrev = 0;
604 unsigned CallExprAbbrev = 0;
605 unsigned CXXOperatorCallExprAbbrev = 0;
606 unsigned CXXMemberCallExprAbbrev = 0;
607
608 unsigned CompoundStmtAbbrev = 0;
609
610 void WriteDeclAbbrevs();
611 void WriteDecl(ASTContext &Context, Decl *D);
612
613 ASTFileSignature WriteASTCore(Sema &SemaRef, StringRef isysroot,
614 Module *WritingModule);
615
616public:
617 /// Create a new precompiled header writer that outputs to
618 /// the given bitstream.
619 ASTWriter(llvm::BitstreamWriter &Stream, SmallVectorImpl<char> &Buffer,
620 InMemoryModuleCache &ModuleCache,
621 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
622 bool IncludeTimestamps = true, bool BuildingImplicitModule = false,
623 bool GeneratingReducedBMI = false);
624 ~ASTWriter() override;
625
626 ASTContext &getASTContext() const {
627 assert(Context && "requested AST context when not writing AST");
628 return *Context;
629 }
630
631 const LangOptions &getLangOpts() const;
632
633 /// Get a timestamp for output into the AST file. The actual timestamp
634 /// of the specified file may be ignored if we have been instructed to not
635 /// include timestamps in the output file.
636 time_t getTimestampForOutput(const FileEntry *E) const;
637
638 /// Write a precompiled header for the given semantic analysis.
639 ///
640 /// \param SemaRef a reference to the semantic analysis object that processed
641 /// the AST to be written into the precompiled header.
642 ///
643 /// \param WritingModule The module that we are writing. If null, we are
644 /// writing a precompiled header.
645 ///
646 /// \param isysroot if non-empty, write a relocatable file whose headers
647 /// are relative to the given system root. If we're writing a module, its
648 /// build directory will be used in preference to this if both are available.
649 ///
650 /// \return the module signature, which eventually will be a hash of
651 /// the module but currently is merely a random 32-bit number.
652 ASTFileSignature WriteAST(Sema &SemaRef, StringRef OutputFile,
653 Module *WritingModule, StringRef isysroot,
654 bool ShouldCacheASTInMemory = false);
655
656 /// Emit a token.
657 void AddToken(const Token &Tok, RecordDataImpl &Record);
658
659 /// Emit a AlignPackInfo.
660 void AddAlignPackInfo(const Sema::AlignPackInfo &Info,
661 RecordDataImpl &Record);
662
663 /// Emit a FileID.
664 void AddFileID(FileID FID, RecordDataImpl &Record);
665
666 /// Emit a source location.
667 void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record,
668 LocSeq *Seq = nullptr);
669
670 /// Emit a source range.
671 void AddSourceRange(SourceRange Range, RecordDataImpl &Record,
672 LocSeq *Seq = nullptr);
673
674 /// Emit a reference to an identifier.
675 void AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record);
676
677 /// Get the unique number used to refer to the given selector.
678 serialization::SelectorID getSelectorRef(Selector Sel);
679
680 /// Get the unique number used to refer to the given identifier.
681 serialization::IdentID getIdentifierRef(const IdentifierInfo *II);
682
683 /// Get the unique number used to refer to the given macro.
684 serialization::MacroID getMacroRef(MacroInfo *MI, const IdentifierInfo *Name);
685
686 /// Determine the ID of an already-emitted macro.
687 serialization::MacroID getMacroID(MacroInfo *MI);
688
689 uint32_t getMacroDirectivesOffset(const IdentifierInfo *Name);
690
691 /// Emit a reference to a type.
692 void AddTypeRef(QualType T, RecordDataImpl &Record);
693
694 /// Force a type to be emitted and get its ID.
695 serialization::TypeID GetOrCreateTypeID(QualType T);
696
697 /// Determine the type ID of an already-emitted type.
698 serialization::TypeID getTypeID(QualType T) const;
699
700 /// Find the first local declaration of a given local redeclarable
701 /// decl.
702 const Decl *getFirstLocalDecl(const Decl *D);
703
704 /// Is this a local declaration (that is, one that will be written to
705 /// our AST file)? This is the case for declarations that are neither imported
706 /// from another AST file nor predefined.
707 bool IsLocalDecl(const Decl *D) {
708 if (D->isFromASTFile())
709 return false;
710 auto I = DeclIDs.find(Val: D);
711 return (I == DeclIDs.end() ||
712 I->second >= serialization::NUM_PREDEF_DECL_IDS);
713 };
714
715 /// Emit a reference to a declaration.
716 void AddDeclRef(const Decl *D, RecordDataImpl &Record);
717 // Emit a reference to a declaration if the declaration was emitted.
718 void AddEmittedDeclRef(const Decl *D, RecordDataImpl &Record);
719
720 /// Force a declaration to be emitted and get its ID.
721 serialization::DeclID GetDeclRef(const Decl *D);
722
723 /// Determine the declaration ID of an already-emitted
724 /// declaration.
725 serialization::DeclID getDeclID(const Decl *D);
726
727 /// Whether or not the declaration got emitted. If not, it wouldn't be
728 /// emitted.
729 ///
730 /// This may only be called after we've done the job to write the
731 /// declarations (marked by DoneWritingDeclsAndTypes).
732 ///
733 /// A declaration may only be omitted in reduced BMI.
734 bool wasDeclEmitted(const Decl *D) const;
735
736 unsigned getAnonymousDeclarationNumber(const NamedDecl *D);
737
738 /// Add a string to the given record.
739 void AddString(StringRef Str, RecordDataImpl &Record);
740
741 /// Convert a path from this build process into one that is appropriate
742 /// for emission in the module file.
743 bool PreparePathForOutput(SmallVectorImpl<char> &Path);
744
745 /// Add a path to the given record.
746 void AddPath(StringRef Path, RecordDataImpl &Record);
747
748 /// Emit the current record with the given path as a blob.
749 void EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
750 StringRef Path);
751
752 /// Add a version tuple to the given record
753 void AddVersionTuple(const VersionTuple &Version, RecordDataImpl &Record);
754
755 /// Retrieve or create a submodule ID for this module, or return 0 if
756 /// the submodule is neither local (a submodle of the currently-written module)
757 /// nor from an imported module.
758 unsigned getLocalOrImportedSubmoduleID(const Module *Mod);
759
760 /// Note that the identifier II occurs at the given offset
761 /// within the identifier table.
762 void SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset);
763
764 /// Note that the selector Sel occurs at the given offset
765 /// within the method pool/selector table.
766 void SetSelectorOffset(Selector Sel, uint32_t Offset);
767
768 /// Record an ID for the given switch-case statement.
769 unsigned RecordSwitchCaseID(SwitchCase *S);
770
771 /// Retrieve the ID for the given switch-case statement.
772 unsigned getSwitchCaseID(SwitchCase *S);
773
774 void ClearSwitchCaseIDs();
775
776 unsigned getTypeExtQualAbbrev() const {
777 return TypeExtQualAbbrev;
778 }
779
780 unsigned getDeclParmVarAbbrev() const { return DeclParmVarAbbrev; }
781 unsigned getDeclRecordAbbrev() const { return DeclRecordAbbrev; }
782 unsigned getDeclTypedefAbbrev() const { return DeclTypedefAbbrev; }
783 unsigned getDeclVarAbbrev() const { return DeclVarAbbrev; }
784 unsigned getDeclFieldAbbrev() const { return DeclFieldAbbrev; }
785 unsigned getDeclEnumAbbrev() const { return DeclEnumAbbrev; }
786 unsigned getDeclObjCIvarAbbrev() const { return DeclObjCIvarAbbrev; }
787 unsigned getDeclCXXMethodAbbrev(FunctionDecl::TemplatedKind Kind) const {
788 switch (Kind) {
789 case FunctionDecl::TK_NonTemplate:
790 return DeclCXXMethodAbbrev;
791 case FunctionDecl::TK_FunctionTemplate:
792 return DeclTemplateCXXMethodAbbrev;
793 case FunctionDecl::TK_MemberSpecialization:
794 return DeclMemberSpecializedCXXMethodAbbrev;
795 case FunctionDecl::TK_FunctionTemplateSpecialization:
796 return DeclTemplateSpecializedCXXMethodAbbrev;
797 case FunctionDecl::TK_DependentNonTemplate:
798 return DeclDependentNonTemplateCXXMethodAbbrev;
799 case FunctionDecl::TK_DependentFunctionTemplateSpecialization:
800 return DeclDependentSpecializationCXXMethodAbbrev;
801 }
802 llvm_unreachable("Unknwon Template Kind!");
803 }
804 unsigned getDeclTemplateTypeParmAbbrev() const {
805 return DeclTemplateTypeParmAbbrev;
806 }
807 unsigned getDeclUsingShadowAbbrev() const { return DeclUsingShadowAbbrev; }
808
809 unsigned getDeclRefExprAbbrev() const { return DeclRefExprAbbrev; }
810 unsigned getCharacterLiteralAbbrev() const { return CharacterLiteralAbbrev; }
811 unsigned getIntegerLiteralAbbrev() const { return IntegerLiteralAbbrev; }
812 unsigned getExprImplicitCastAbbrev() const { return ExprImplicitCastAbbrev; }
813 unsigned getBinaryOperatorAbbrev() const { return BinaryOperatorAbbrev; }
814 unsigned getCompoundAssignOperatorAbbrev() const {
815 return CompoundAssignOperatorAbbrev;
816 }
817 unsigned getCallExprAbbrev() const { return CallExprAbbrev; }
818 unsigned getCXXOperatorCallExprAbbrev() { return CXXOperatorCallExprAbbrev; }
819 unsigned getCXXMemberCallExprAbbrev() { return CXXMemberCallExprAbbrev; }
820
821 unsigned getCompoundStmtAbbrev() const { return CompoundStmtAbbrev; }
822
823 bool hasChain() const { return Chain; }
824 ASTReader *getChain() const { return Chain; }
825
826 bool isWritingStdCXXNamedModules() const {
827 return WritingModule && WritingModule->isNamedModule();
828 }
829
830 bool isGeneratingReducedBMI() const { return GeneratingReducedBMI; }
831
832 bool getDoneWritingDeclsAndTypes() const { return DoneWritingDeclsAndTypes; }
833
834private:
835 // ASTDeserializationListener implementation
836 void ReaderInitialized(ASTReader *Reader) override;
837 void IdentifierRead(serialization::IdentID ID, IdentifierInfo *II) override;
838 void MacroRead(serialization::MacroID ID, MacroInfo *MI) override;
839 void TypeRead(serialization::TypeIdx Idx, QualType T) override;
840 void SelectorRead(serialization::SelectorID ID, Selector Sel) override;
841 void MacroDefinitionRead(serialization::PreprocessedEntityID ID,
842 MacroDefinitionRecord *MD) override;
843 void ModuleRead(serialization::SubmoduleID ID, Module *Mod) override;
844
845 // ASTMutationListener implementation.
846 void CompletedTagDefinition(const TagDecl *D) override;
847 void AddedVisibleDecl(const DeclContext *DC, const Decl *D) override;
848 void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) override;
849 void AddedCXXTemplateSpecialization(
850 const ClassTemplateDecl *TD,
851 const ClassTemplateSpecializationDecl *D) override;
852 void AddedCXXTemplateSpecialization(
853 const VarTemplateDecl *TD,
854 const VarTemplateSpecializationDecl *D) override;
855 void AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
856 const FunctionDecl *D) override;
857 void ResolvedExceptionSpec(const FunctionDecl *FD) override;
858 void DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) override;
859 void ResolvedOperatorDelete(const CXXDestructorDecl *DD,
860 const FunctionDecl *Delete,
861 Expr *ThisArg) override;
862 void CompletedImplicitDefinition(const FunctionDecl *D) override;
863 void InstantiationRequested(const ValueDecl *D) override;
864 void VariableDefinitionInstantiated(const VarDecl *D) override;
865 void FunctionDefinitionInstantiated(const FunctionDecl *D) override;
866 void DefaultArgumentInstantiated(const ParmVarDecl *D) override;
867 void DefaultMemberInitializerInstantiated(const FieldDecl *D) override;
868 void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
869 const ObjCInterfaceDecl *IFD) override;
870 void DeclarationMarkedUsed(const Decl *D) override;
871 void DeclarationMarkedOpenMPThreadPrivate(const Decl *D) override;
872 void DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
873 const Attr *Attr) override;
874 void DeclarationMarkedOpenMPAllocate(const Decl *D, const Attr *A) override;
875 void RedefinedHiddenDefinition(const NamedDecl *D, Module *M) override;
876 void AddedAttributeToRecord(const Attr *Attr,
877 const RecordDecl *Record) override;
878 void EnteringModulePurview() override;
879 void AddedManglingNumber(const Decl *D, unsigned) override;
880 void AddedStaticLocalNumbers(const Decl *D, unsigned) override;
881 void AddedAnonymousNamespace(const TranslationUnitDecl *,
882 NamespaceDecl *AnonNamespace) override;
883};
884
885/// AST and semantic-analysis consumer that generates a
886/// precompiled header from the parsed source code.
887class PCHGenerator : public SemaConsumer {
888 Preprocessor &PP;
889 std::string OutputFile;
890 std::string isysroot;
891 Sema *SemaPtr;
892 std::shared_ptr<PCHBuffer> Buffer;
893 llvm::BitstreamWriter Stream;
894 ASTWriter Writer;
895 bool AllowASTWithErrors;
896 bool ShouldCacheASTInMemory;
897
898protected:
899 ASTWriter &getWriter() { return Writer; }
900 const ASTWriter &getWriter() const { return Writer; }
901 SmallVectorImpl<char> &getPCH() const { return Buffer->Data; }
902
903 bool isComplete() const { return Buffer->IsComplete; }
904 PCHBuffer *getBufferPtr() { return Buffer.get(); }
905 StringRef getOutputFile() const { return OutputFile; }
906 DiagnosticsEngine &getDiagnostics() const {
907 return SemaPtr->getDiagnostics();
908 }
909 Preprocessor &getPreprocessor() { return PP; }
910
911 virtual Module *getEmittingModule(ASTContext &Ctx);
912
913public:
914 PCHGenerator(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
915 StringRef OutputFile, StringRef isysroot,
916 std::shared_ptr<PCHBuffer> Buffer,
917 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
918 bool AllowASTWithErrors = false, bool IncludeTimestamps = true,
919 bool BuildingImplicitModule = false,
920 bool ShouldCacheASTInMemory = false,
921 bool GeneratingReducedBMI = false);
922 ~PCHGenerator() override;
923
924 void InitializeSema(Sema &S) override { SemaPtr = &S; }
925 void HandleTranslationUnit(ASTContext &Ctx) override;
926 ASTMutationListener *GetASTMutationListener() override;
927 ASTDeserializationListener *GetASTDeserializationListener() override;
928 bool hasEmittedPCH() const { return Buffer->IsComplete; }
929};
930
931class ReducedBMIGenerator : public PCHGenerator {
932protected:
933 virtual Module *getEmittingModule(ASTContext &Ctx) override;
934
935public:
936 ReducedBMIGenerator(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
937 StringRef OutputFile);
938
939 void HandleTranslationUnit(ASTContext &Ctx) override;
940};
941
942/// If we can elide the definition of \param D in reduced BMI.
943///
944/// Generally, we can elide the definition of a declaration if it won't affect
945/// the ABI. e.g., the non-inline function bodies.
946bool CanElideDeclDef(const Decl *D);
947
948/// A simple helper class to pack several bits in order into (a) 32 bit
949/// integer(s).
950class BitsPacker {
951 constexpr static uint32_t BitIndexUpbound = 32u;
952
953public:
954 BitsPacker() = default;
955 BitsPacker(const BitsPacker &) = delete;
956 BitsPacker(BitsPacker &&) = delete;
957 BitsPacker operator=(const BitsPacker &) = delete;
958 BitsPacker operator=(BitsPacker &&) = delete;
959 ~BitsPacker() = default;
960
961 bool canWriteNextNBits(uint32_t BitsWidth) const {
962 return CurrentBitIndex + BitsWidth < BitIndexUpbound;
963 }
964
965 void reset(uint32_t Value) {
966 UnderlyingValue = Value;
967 CurrentBitIndex = 0;
968 }
969
970 void addBit(bool Value) { addBits(Value, BitsWidth: 1); }
971 void addBits(uint32_t Value, uint32_t BitsWidth) {
972 assert(BitsWidth < BitIndexUpbound);
973 assert((Value < (1u << BitsWidth)) && "Passing narrower bit width!");
974 assert(canWriteNextNBits(BitsWidth) &&
975 "Inserting too much bits into a value!");
976
977 UnderlyingValue |= Value << CurrentBitIndex;
978 CurrentBitIndex += BitsWidth;
979 }
980
981 operator uint32_t() { return UnderlyingValue; }
982
983private:
984 uint32_t UnderlyingValue = 0;
985 uint32_t CurrentBitIndex = 0;
986};
987
988} // namespace clang
989
990#endif // LLVM_CLANG_SERIALIZATION_ASTWRITER_H
991

source code of clang/include/clang/Serialization/ASTWriter.h