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