clang 19.0.0git
ASTRecordWriter.h
Go to the documentation of this file.
1//===- ASTRecordWriter.h - Helper classes for writing AST -------*- 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 ASTRecordWriter class, a helper class useful
10// when serializing AST.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_SERIALIZATION_ASTRECORDWRITER_H
15#define LLVM_CLANG_SERIALIZATION_ASTRECORDWRITER_H
16
21
22namespace clang {
23
24class TypeLoc;
25
26/// An object for streaming information to a record.
28 : public serialization::DataStreamBasicWriter<ASTRecordWriter> {
30
31 ASTWriter *Writer;
33
34 /// Statements that we've encountered while serializing a
35 /// declaration or type.
36 SmallVector<Stmt *, 16> StmtsToEmit;
37
38 /// Indices of record elements that describe offsets within the
39 /// bitcode. These will be converted to offsets relative to the current
40 /// record when emitted.
41 SmallVector<unsigned, 8> OffsetIndices;
42
43 /// Flush all of the statements and expressions that have
44 /// been added to the queue via AddStmt().
45 void FlushStmts();
46 void FlushSubStmts();
47
48 void PrepareToEmit(uint64_t MyOffset) {
49 // Convert offsets into relative form.
50 for (unsigned I : OffsetIndices) {
51 auto &StoredOffset = (*Record)[I];
52 assert(StoredOffset < MyOffset && "invalid offset");
53 if (StoredOffset)
54 StoredOffset = MyOffset - StoredOffset;
55 }
56 OffsetIndices.clear();
57 }
58
59public:
60 /// Construct a ASTRecordWriter that uses the default encoding scheme.
62 : DataStreamBasicWriter(W.getASTContext()), Writer(&W), Record(&Record) {}
63
64 /// Construct a ASTRecordWriter that uses the same encoding scheme as another
65 /// ASTRecordWriter.
67 : DataStreamBasicWriter(Parent.getASTContext()), Writer(Parent.Writer),
68 Record(&Record) {}
69
70 /// Copying an ASTRecordWriter is almost certainly a bug.
73
74 /// Extract the underlying record storage.
75 ASTWriter::RecordDataImpl &getRecordData() const { return *Record; }
76
77 /// Minimal vector-like interface.
78 /// @{
79 void push_back(uint64_t N) { Record->push_back(N); }
80 template<typename InputIterator>
81 void append(InputIterator begin, InputIterator end) {
82 Record->append(begin, end);
83 }
84 bool empty() const { return Record->empty(); }
85 size_t size() const { return Record->size(); }
86 uint64_t &operator[](size_t N) { return (*Record)[N]; }
87 /// @}
88
89 /// Emit the record to the stream, followed by its substatements, and
90 /// return its offset.
91 // FIXME: Allow record producers to suggest Abbrevs.
92 uint64_t Emit(unsigned Code, unsigned Abbrev = 0) {
93 uint64_t Offset = Writer->Stream.GetCurrentBitNo();
94 PrepareToEmit(Offset);
95 Writer->Stream.EmitRecord(Code, *Record, Abbrev);
96 FlushStmts();
97 return Offset;
98 }
99
100 /// Emit the record to the stream, preceded by its substatements.
101 uint64_t EmitStmt(unsigned Code, unsigned Abbrev = 0) {
102 FlushSubStmts();
103 PrepareToEmit(Writer->Stream.GetCurrentBitNo());
104 Writer->Stream.EmitRecord(Code, *Record, Abbrev);
105 return Writer->Stream.GetCurrentBitNo();
106 }
107
108 /// Add a bit offset into the record. This will be converted into an
109 /// offset relative to the current record when emitted.
110 void AddOffset(uint64_t BitOffset) {
111 OffsetIndices.push_back(Record->size());
112 Record->push_back(BitOffset);
113 }
114
115 /// Add the given statement or expression to the queue of
116 /// statements to emit.
117 ///
118 /// This routine should be used when emitting types and declarations
119 /// that have expressions as part of their formulation. Once the
120 /// type or declaration has been written, Emit() will write
121 /// the corresponding statements just after the record.
122 void AddStmt(Stmt *S) {
123 StmtsToEmit.push_back(S);
124 }
125 void writeStmtRef(const Stmt *S) {
126 AddStmt(const_cast<Stmt*>(S));
127 }
128
129 /// Write an BTFTypeTagAttr object.
130 void writeBTFTypeTagAttr(const BTFTypeTagAttr *A) { AddAttr(A); }
131
132 /// Add a definition for the given function to the queue of statements
133 /// to emit.
134 void AddFunctionDefinition(const FunctionDecl *FD);
135
136 /// Emit a source location.
138 return Writer->AddSourceLocation(Loc, *Record, Seq);
139 }
142 }
143
144 /// Emit a source range.
145 void AddSourceRange(SourceRange Range, LocSeq *Seq = nullptr) {
146 return Writer->AddSourceRange(Range, *Record, Seq);
147 }
148
149 void writeBool(bool Value) {
150 Record->push_back(Value);
151 }
152
153 void writeUInt32(uint32_t Value) {
154 Record->push_back(Value);
155 }
156
157 void writeUInt64(uint64_t Value) {
158 Record->push_back(Value);
159 }
160
161 /// Emit an integral value.
162 void AddAPInt(const llvm::APInt &Value) {
164 }
165
166 /// Emit a signed integral value.
167 void AddAPSInt(const llvm::APSInt &Value) {
169 }
170
171 /// Emit a floating-point value.
172 void AddAPFloat(const llvm::APFloat &Value);
173
174 /// Emit an APvalue.
175 void AddAPValue(const APValue &Value) { writeAPValue(Value); }
176
177 /// Emit a reference to an identifier.
179 return Writer->AddIdentifierRef(II, *Record);
180 }
183 }
184
185 /// Emit a Selector (which is a smart pointer reference).
186 void AddSelectorRef(Selector S);
188 AddSelectorRef(sel);
189 }
190
191 /// Emit a CXXTemporary.
192 void AddCXXTemporary(const CXXTemporary *Temp);
193
194 /// Emit a C++ base specifier.
196
197 /// Emit a set of C++ base specifiers.
199
200 /// Emit a reference to a type.
202 return Writer->AddTypeRef(T, *Record);
203 }
205 AddTypeRef(T);
206 }
207
208 /// Emits a reference to a declarator info.
210
211 /// Emits source location information for a type. Does not emit the type.
212 void AddTypeLoc(TypeLoc TL, LocSeq *Seq = nullptr);
213
214 /// Emits a template argument location info.
216 const TemplateArgumentLocInfo &Arg);
217
218 /// Emits a template argument location.
220
221 /// Emits an AST template argument list info.
223 const ASTTemplateArgumentListInfo *ASTTemplArgList);
224
225 // Emits a concept reference.
227
228 /// Emit a reference to a declaration.
229 void AddDeclRef(const Decl *D) {
230 return Writer->AddDeclRef(D, *Record);
231 }
232 void writeDeclRef(const Decl *D) {
233 AddDeclRef(D);
234 }
235
236 /// Emit a declaration name.
238 writeDeclarationName(Name);
239 }
240
242 DeclarationName Name);
243 void AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo);
244
245 void AddQualifierInfo(const QualifierInfo &Info);
246
247 /// Emit a nested name specifier.
250 }
251
252 /// Emit a nested name specifier with source-location information.
254
255 /// Emit a template name.
257 writeTemplateName(Name);
258 }
259
260 /// Emit a template argument.
262 writeTemplateArgument(Arg);
263 }
264
265 /// Emit a template parameter list.
266 void AddTemplateParameterList(const TemplateParameterList *TemplateParams);
267
268 /// Emit a template argument list.
269 void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs);
270
271 /// Emit a UnresolvedSet structure.
273
274 /// Emit a CXXCtorInitializer array.
276
277 void AddCXXDefinitionData(const CXXRecordDecl *D);
278
279 /// Emit information about the initializer of a VarDecl.
280 void AddVarDeclInit(const VarDecl *VD);
281
282 /// Write an OMPTraitInfo object.
283 void writeOMPTraitInfo(const OMPTraitInfo *TI);
284
286
287 /// Writes data related to the OpenMP directives.
289
290 /// Emit a string.
291 void AddString(StringRef Str) {
292 return Writer->AddString(Str, *Record);
293 }
294
295 /// Emit a path.
296 void AddPath(StringRef Path) {
297 return Writer->AddPath(Path, *Record);
298 }
299
300 /// Emit a version tuple.
301 void AddVersionTuple(const VersionTuple &Version) {
302 return Writer->AddVersionTuple(Version, *Record);
303 }
304
305 // Emit an attribute.
306 void AddAttr(const Attr *A);
307
308 /// Emit a list of attributes.
310};
311
312} // end namespace clang
313
314#endif
NodeId Parent
Definition: ASTDiff.cpp:191
This file defines OpenMP AST classes for clauses.
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition: APValue.h:122
An object for streaming information to a record.
void AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo)
Definition: ASTWriter.cpp:5867
ASTRecordWriter & operator=(const ASTRecordWriter &)=delete
void AddCXXBaseSpecifiers(ArrayRef< CXXBaseSpecifier > Bases)
Emit a set of C++ base specifiers.
Definition: ASTWriter.cpp:6007
void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs)
Emit a template argument list.
Definition: ASTWriter.cpp:5954
void AddFunctionDefinition(const FunctionDecl *FD)
Add a definition for the given function to the queue of statements to emit.
uint64_t Emit(unsigned Code, unsigned Abbrev=0)
Emit the record to the stream, followed by its substatements, and return its offset.
void AddCXXTemporary(const CXXTemporary *Temp)
Emit a CXXTemporary.
Definition: ASTWriter.cpp:5632
void writeOMPTraitInfo(const OMPTraitInfo *TI)
Write an OMPTraitInfo object.
Definition: ASTWriter.cpp:7346
void AddCXXBaseSpecifier(const CXXBaseSpecifier &Base)
Emit a C++ base specifier.
Definition: ASTWriter.cpp:5983
ASTWriter::RecordDataImpl & getRecordData() const
Extract the underlying record storage.
void writeOMPClause(OMPClause *C)
Definition: ASTWriter.cpp:6552
void writeBool(bool Value)
void AddAPValue(const APValue &Value)
Emit an APvalue.
void AddNestedNameSpecifier(NestedNameSpecifier *NNS)
Emit a nested name specifier.
void AddUnresolvedSet(const ASTUnresolvedSet &Set)
Emit a UnresolvedSet structure.
Definition: ASTWriter.cpp:5973
void AddIdentifierRef(const IdentifierInfo *II)
Emit a reference to an identifier.
void AddStmt(Stmt *S)
Add the given statement or expression to the queue of statements to emit.
void AddDeclarationName(DeclarationName Name)
Emit a declaration name.
void AddSelectorRef(Selector S)
Emit a Selector (which is a smart pointer reference).
Definition: ASTWriter.cpp:5609
void writeUInt64(uint64_t Value)
uint64_t EmitStmt(unsigned Code, unsigned Abbrev=0)
Emit the record to the stream, preceded by its substatements.
void AddSourceRange(SourceRange Range, LocSeq *Seq=nullptr)
Emit a source range.
void AddPath(StringRef Path)
Emit a path.
void writeSourceLocation(SourceLocation Loc)
void AddOffset(uint64_t BitOffset)
Add a bit offset into the record.
void AddTypeRef(QualType T)
Emit a reference to a type.
void writeQualType(QualType T)
void AddSourceLocation(SourceLocation Loc, LocSeq *Seq=nullptr)
Emit a source location.
void push_back(uint64_t N)
Minimal vector-like interface.
void append(InputIterator begin, InputIterator end)
void AddTypeLoc(TypeLoc TL, LocSeq *Seq=nullptr)
Emits source location information for a type. Does not emit the type.
Definition: ASTWriter.cpp:5688
void AddCXXCtorInitializers(ArrayRef< CXXCtorInitializer * > CtorInits)
Emit a CXXCtorInitializer array.
Definition: ASTWriter.cpp:6047
void AddTemplateParameterList(const TemplateParameterList *TemplateParams)
Emit a template parameter list.
Definition: ASTWriter.cpp:5935
void AddTemplateArgument(const TemplateArgument &Arg)
Emit a template argument.
void AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc, DeclarationName Name)
Definition: ASTWriter.cpp:5840
void writeSelector(Selector sel)
void AddTemplateName(TemplateName Name)
Emit a template name.
void AddAPFloat(const llvm::APFloat &Value)
Emit a floating-point value.
Definition: ASTWriter.cpp:5563
void AddTypeSourceInfo(TypeSourceInfo *TInfo)
Emits a reference to a declarator info.
Definition: ASTWriter.cpp:5678
ASTRecordWriter(ASTWriter &W, ASTWriter::RecordDataImpl &Record)
Construct a ASTRecordWriter that uses the default encoding scheme.
void AddQualifierInfo(const QualifierInfo &Info)
Definition: ASTWriter.cpp:5874
void writeUInt32(uint32_t Value)
void AddDeclRef(const Decl *D)
Emit a reference to a declaration.
void writeOMPChildren(OMPChildren *Data)
Writes data related to the OpenMP directives.
Definition: ASTWriter.cpp:7363
ASTRecordWriter(const ASTRecordWriter &)=delete
Copying an ASTRecordWriter is almost certainly a bug.
void AddAPSInt(const llvm::APSInt &Value)
Emit a signed integral value.
void AddConceptReference(const ConceptReference *CR)
Definition: ASTWriter.cpp:473
void AddVersionTuple(const VersionTuple &Version)
Emit a version tuple.
void AddString(StringRef Str)
Emit a string.
void writeIdentifier(const IdentifierInfo *II)
void AddAPInt(const llvm::APInt &Value)
Emit an integral value.
void AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg)
Emits a template argument location info.
Definition: ASTWriter.cpp:5636
ASTRecordWriter(ASTRecordWriter &Parent, ASTWriter::RecordDataImpl &Record)
Construct a ASTRecordWriter that uses the same encoding scheme as another ASTRecordWriter.
void AddAttributes(ArrayRef< const Attr * > Attrs)
Emit a list of attributes.
Definition: ASTWriter.cpp:4495
void AddASTTemplateArgumentListInfo(const ASTTemplateArgumentListInfo *ASTTemplArgList)
Emits an AST template argument list info.
Definition: ASTWriter.cpp:5962
void AddCXXDefinitionData(const CXXRecordDecl *D)
Definition: ASTWriter.cpp:6052
void AddVarDeclInit(const VarDecl *VD)
Emit information about the initializer of a VarDecl.
Definition: ASTWriter.cpp:6149
void writeStmtRef(const Stmt *S)
uint64_t & operator[](size_t N)
void AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg)
Emits a template argument location.
Definition: ASTWriter.cpp:5665
void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS)
Emit a nested name specifier with source-location information.
Definition: ASTWriter.cpp:5881
void writeBTFTypeTagAttr(const BTFTypeTagAttr *A)
Write an BTFTypeTagAttr object.
void AddAttr(const Attr *A)
Definition: ASTWriter.cpp:4471
void writeDeclRef(const Decl *D)
An UnresolvedSet-like class which uses the ASTContext's allocator.
Writes an AST file containing the contents of a translation unit.
Definition: ASTWriter.h:86
void AddSourceRange(SourceRange Range, RecordDataImpl &Record, LocSeq *Seq=nullptr)
Emit a source range.
Definition: ASTWriter.cpp:5557
void AddPath(StringRef Path, RecordDataImpl &Record)
Add a path to the given record.
Definition: ASTWriter.cpp:4574
void AddVersionTuple(const VersionTuple &Version, RecordDataImpl &Record)
Add a version tuple to the given record.
Definition: ASTWriter.cpp:4587
void AddString(StringRef Str, RecordDataImpl &Record)
Add a string to the given record.
Definition: ASTWriter.cpp:4546
void AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record)
Emit a reference to an identifier.
Definition: ASTWriter.cpp:5567
void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record, LocSeq *Seq=nullptr)
Emit a source location.
Definition: ASTWriter.cpp:5551
void AddTypeRef(QualType T, RecordDataImpl &Record)
Emit a reference to a type.
Definition: ASTWriter.cpp:5695
void AddDeclRef(const Decl *D, RecordDataImpl &Record)
Emit a reference to a declaration.
Definition: ASTWriter.cpp:5735
Attr - This represents one attribute.
Definition: Attr.h:42
Represents a base class of a C++ class.
Definition: DeclCXX.h:146
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
Represents a C++ temporary.
Definition: ExprCXX.h:1443
A reference to a concept and its template args, as it appears in the code.
Definition: ASTConcept.h:128
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:85
DeclarationNameLoc - Additional source/type location info for a declaration name.
The name of a declaration.
Represents a function declaration or definition.
Definition: Decl.h:1959
One of these records is kept for each identifier that is lexed.
A C++ nested-name-specifier augmented with source location information.
Represents a C++ nested name specifier, such as "\::std::vector<int>::".
Contains data for OpenMP directives: clauses, children expressions/statements (helpers for codegen) a...
This is a basic class for representing single OpenMP clause.
Definition: OpenMPClause.h:55
Helper data structure representing the traits in a match clause of an declare variant or metadirectiv...
A (possibly-)qualified type.
Definition: Type.h:737
Smart pointer class that efficiently represents Objective-C method names.
Serialized encoding of a sequence of SourceLocations.
Encodes a location in the source.
A trivial tuple used to represent a source range.
Stmt - This represents one statement.
Definition: Stmt.h:84
A template argument list.
Definition: DeclTemplate.h:244
Location wrapper for a TemplateArgument.
Definition: TemplateBase.h:524
Represents a template argument.
Definition: TemplateBase.h:61
ArgKind
The kind of template argument we're storing.
Definition: TemplateBase.h:64
Represents a C++ template name within the type system.
Definition: TemplateName.h:202
Stores a list of template parameters for a TemplateDecl and its derived classes.
Definition: DeclTemplate.h:73
Base wrapper for a particular "section" of type source info.
Definition: TypeLoc.h:59
A container of type source information.
Definition: Type.h:6872
Represents a variable declaration or definition.
Definition: Decl.h:918
DataStreamBasicWriter provides convenience implementations for many BasicWriter methods based on the ...
The JSON file list parser is used to communicate input to InstallAPI.
@ Seq
'seq' clause, allowed on 'loop' and 'routine' directives.
Represents an explicit template argument list in C++, e.g., the "<int>" in "sort<int>".
Definition: TemplateBase.h:676
DeclarationNameInfo - A collector data type for bundling together a DeclarationName and the correspon...
A struct with extended info about a syntactic name qualifier, to be used for the case of out-of-line ...
Definition: Decl.h:743
Location information for a TemplateArgument.
Definition: TemplateBase.h:472