clang 20.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
22
23namespace clang {
24
25class OpenACCClause;
26class TypeLoc;
27
28/// An object for streaming information to a record.
30 : public serialization::DataStreamBasicWriter<ASTRecordWriter> {
32
33 ASTWriter *Writer;
35
36 /// Statements that we've encountered while serializing a
37 /// declaration or type.
38 SmallVector<Stmt *, 16> StmtsToEmit;
39
40 /// Indices of record elements that describe offsets within the
41 /// bitcode. These will be converted to offsets relative to the current
42 /// record when emitted.
43 SmallVector<unsigned, 8> OffsetIndices;
44
45 /// Flush all of the statements and expressions that have
46 /// been added to the queue via AddStmt().
47 void FlushStmts();
48 void FlushSubStmts();
49
50 void PrepareToEmit(uint64_t MyOffset) {
51 // Convert offsets into relative form.
52 for (unsigned I : OffsetIndices) {
53 auto &StoredOffset = (*Record)[I];
54 assert(StoredOffset < MyOffset && "invalid offset");
55 if (StoredOffset)
56 StoredOffset = MyOffset - StoredOffset;
57 }
58 OffsetIndices.clear();
59 }
60
61public:
62 /// Construct a ASTRecordWriter that uses the default encoding scheme.
65 : DataStreamBasicWriter(Context), Writer(&W), Record(&Record) {}
66
67 /// Construct a ASTRecordWriter that uses the same encoding scheme as another
68 /// ASTRecordWriter.
70 : DataStreamBasicWriter(Parent.getASTContext()), Writer(Parent.Writer),
71 Record(&Record) {}
72
73 /// Copying an ASTRecordWriter is almost certainly a bug.
76
77 /// Extract the underlying record storage.
79
80 /// Minimal vector-like interface.
81 /// @{
82 void push_back(uint64_t N) { Record->push_back(N); }
83 template<typename InputIterator>
84 void append(InputIterator begin, InputIterator end) {
85 Record->append(begin, end);
86 }
87 bool empty() const { return Record->empty(); }
88 size_t size() const { return Record->size(); }
89 uint64_t &operator[](size_t N) { return (*Record)[N]; }
90 /// @}
91
92 /// Emit the record to the stream, followed by its substatements, and
93 /// return its offset.
94 // FIXME: Allow record producers to suggest Abbrevs.
95 uint64_t Emit(unsigned Code, unsigned Abbrev = 0) {
96 uint64_t Offset = Writer->Stream.GetCurrentBitNo();
97 PrepareToEmit(Offset);
98 Writer->Stream.EmitRecord(Code, *Record, Abbrev);
99 FlushStmts();
100 return Offset;
101 }
102
103 /// Emit the record to the stream, preceded by its substatements.
104 uint64_t EmitStmt(unsigned Code, unsigned Abbrev = 0) {
105 FlushSubStmts();
106 PrepareToEmit(Writer->Stream.GetCurrentBitNo());
107 Writer->Stream.EmitRecord(Code, *Record, Abbrev);
108 return Writer->Stream.GetCurrentBitNo();
109 }
110
111 /// Add a bit offset into the record. This will be converted into an
112 /// offset relative to the current record when emitted.
113 void AddOffset(uint64_t BitOffset) {
114 OffsetIndices.push_back(Record->size());
115 Record->push_back(BitOffset);
116 }
117
118 /// Add the given statement or expression to the queue of
119 /// statements to emit.
120 ///
121 /// This routine should be used when emitting types and declarations
122 /// that have expressions as part of their formulation. Once the
123 /// type or declaration has been written, Emit() will write
124 /// the corresponding statements just after the record.
125 void AddStmt(Stmt *S) {
126 StmtsToEmit.push_back(S);
127 }
128 void writeStmtRef(const Stmt *S) {
129 AddStmt(const_cast<Stmt*>(S));
130 }
131
132 void writeAttr(const Attr *A) { AddAttr(A); }
133
134 /// Write an BTFTypeTagAttr object.
135 void writeBTFTypeTagAttr(const BTFTypeTagAttr *A) { AddAttr(A); }
136
137 /// Add a definition for the given function to the queue of statements
138 /// to emit.
139 void AddFunctionDefinition(const FunctionDecl *FD);
140
141 /// Emit a source location.
143 return Writer->AddSourceLocation(Loc, *Record, Seq);
144 }
147 }
148
150 writeDeclRef(Info.getDecl());
151 writeBool(Info.isDeref());
152 }
153
154 /// Emit a source range.
156 return Writer->AddSourceRange(Range, *Record, Seq);
157 }
158
159 void writeBool(bool Value) {
160 Record->push_back(Value);
161 }
162
163 void writeUInt32(uint32_t Value) {
164 Record->push_back(Value);
165 }
166
167 void writeUInt64(uint64_t Value) {
168 Record->push_back(Value);
169 }
170
171 /// Emit an integral value.
172 void AddAPInt(const llvm::APInt &Value) {
174 }
175
176 /// Emit a signed integral value.
177 void AddAPSInt(const llvm::APSInt &Value) {
179 }
180
181 /// Emit a floating-point value.
182 void AddAPFloat(const llvm::APFloat &Value);
183
184 /// Emit an APvalue.
185 void AddAPValue(const APValue &Value) { writeAPValue(Value); }
186
187 /// Emit a reference to an identifier.
189 return Writer->AddIdentifierRef(II, *Record);
190 }
193 }
194
195 /// Emit a Selector (which is a smart pointer reference).
196 void AddSelectorRef(Selector S);
198 AddSelectorRef(sel);
199 }
200
201 /// Emit a CXXTemporary.
202 void AddCXXTemporary(const CXXTemporary *Temp);
203
204 /// Emit a C++ base specifier.
206
207 /// Emit a set of C++ base specifiers.
209
210 /// Emit a reference to a type.
212 return Writer->AddTypeRef(getASTContext(), T, *Record);
213 }
215 AddTypeRef(T);
216 }
217
218 /// Emits a reference to a declarator info.
220
221 /// Emits source location information for a type. Does not emit the type.
222 void AddTypeLoc(TypeLoc TL, LocSeq *Seq = nullptr);
223
224 /// Emits a template argument location info.
226 const TemplateArgumentLocInfo &Arg);
227
228 /// Emits a template argument location.
230
231 /// Emits an AST template argument list info.
233 const ASTTemplateArgumentListInfo *ASTTemplArgList);
234
235 // Emits a concept reference.
237
238 /// Emit a reference to a declaration.
239 void AddDeclRef(const Decl *D) {
240 return Writer->AddDeclRef(D, *Record);
241 }
242 void writeDeclRef(const Decl *D) {
243 AddDeclRef(D);
244 }
245
246 /// Emit a declaration name.
248 writeDeclarationName(Name);
249 }
250
252 DeclarationName Name);
253 void AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo);
254
255 void AddQualifierInfo(const QualifierInfo &Info);
256
257 /// Emit a nested name specifier.
260 }
261
262 /// Emit a nested name specifier with source-location information.
264
265 /// Emit a template name.
267 writeTemplateName(Name);
268 }
269
270 /// Emit a template argument.
272 writeTemplateArgument(Arg);
273 }
274
275 /// Emit a template parameter list.
276 void AddTemplateParameterList(const TemplateParameterList *TemplateParams);
277
278 /// Emit a template argument list.
279 void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs);
280
281 /// Emit a UnresolvedSet structure.
283
284 /// Emit a CXXCtorInitializer array.
286
288
289 /// Emit information about the initializer of a VarDecl.
290 void AddVarDeclInit(const VarDecl *VD);
291
292 /// Write an OMPTraitInfo object.
293 void writeOMPTraitInfo(const OMPTraitInfo *TI);
294
296
297 /// Writes data related to the OpenMP directives.
299
301
303
304 /// Writes out a single OpenACC Clause.
306
307 /// Writes out a list of OpenACC clauses.
309
310 /// Emit a string.
311 void AddString(StringRef Str) {
312 return Writer->AddString(Str, *Record);
313 }
314
315 /// Emit a path.
316 void AddPath(StringRef Path) {
317 return Writer->AddPath(Path, *Record);
318 }
319
320 /// Emit a version tuple.
321 void AddVersionTuple(const VersionTuple &Version) {
322 return Writer->AddVersionTuple(Version, *Record);
323 }
324
325 // Emit an attribute.
326 void AddAttr(const Attr *A);
327
328 /// Emit a list of attributes.
330};
331
332} // end namespace clang
333
334#endif
NodeId Parent
Definition: ASTDiff.cpp:191
const Decl * D
IndirectLocalPath & Path
enum clang::sema::@1718::IndirectLocalPathEntry::EntryKind Kind
llvm::MachO::Record Record
Definition: MachO.h:31
This file defines OpenMP AST classes for clauses.
SourceRange Range
Definition: SemaObjC.cpp:758
SourceLocation Loc
Definition: SemaObjC.cpp:759
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition: APValue.h:122
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:188
An object for streaming information to a record.
void AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo)
Definition: ASTWriter.cpp:6708
ASTRecordWriter & operator=(const ASTRecordWriter &)=delete
void AddCXXBaseSpecifiers(ArrayRef< CXXBaseSpecifier > Bases)
Emit a set of C++ base specifiers.
Definition: ASTWriter.cpp:6848
void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs)
Emit a template argument list.
Definition: ASTWriter.cpp:6795
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:6426
void writeOMPTraitInfo(const OMPTraitInfo *TI)
Write an OMPTraitInfo object.
Definition: ASTWriter.cpp:8252
void AddCXXBaseSpecifier(const CXXBaseSpecifier &Base)
Emit a C++ base specifier.
Definition: ASTWriter.cpp:6824
ASTWriter::RecordDataImpl & getRecordData() const
Extract the underlying record storage.
void writeOMPClause(OMPClause *C)
Definition: ASTWriter.cpp:7418
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:6814
ASTRecordWriter(ASTContext &Context, ASTWriter &W, ASTWriter::RecordDataImpl &Record)
Construct a ASTRecordWriter that uses the default encoding scheme.
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:6403
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 writeOpenACCClauseList(ArrayRef< const OpenACCClause * > Clauses)
Writes out a list of OpenACC clauses.
Definition: ASTWriter.cpp:8538
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:6482
void AddCXXCtorInitializers(ArrayRef< CXXCtorInitializer * > CtorInits)
Emit a CXXCtorInitializer array.
Definition: ASTWriter.cpp:6888
void AddTemplateParameterList(const TemplateParameterList *TemplateParams)
Emit a template parameter list.
Definition: ASTWriter.cpp:6776
void AddTemplateArgument(const TemplateArgument &Arg)
Emit a template argument.
void AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc, DeclarationName Name)
Definition: ASTWriter.cpp:6681
void writeSelector(Selector sel)
void writeOpenACCIntExprList(ArrayRef< Expr * > Exprs)
Definition: ASTWriter.cpp:8289
void AddTemplateName(TemplateName Name)
Emit a template name.
void AddAPFloat(const llvm::APFloat &Value)
Emit a floating-point value.
Definition: ASTWriter.cpp:6357
void AddTypeSourceInfo(TypeSourceInfo *TInfo)
Emits a reference to a declarator info.
Definition: ASTWriter.cpp:6472
void AddQualifierInfo(const QualifierInfo &Info)
Definition: ASTWriter.cpp:6715
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:8269
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:549
void AddVersionTuple(const VersionTuple &Version)
Emit a version tuple.
void AddString(StringRef Str)
Emit a string.
void writeIdentifier(const IdentifierInfo *II)
void writeAttr(const Attr *A)
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:6430
void writeOpenACCVarList(const OpenACCClauseWithVarList *C)
Definition: ASTWriter.cpp:8283
void writeTypeCoupledDeclRefInfo(TypeCoupledDeclRefInfo Info)
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:4931
void AddASTTemplateArgumentListInfo(const ASTTemplateArgumentListInfo *ASTTemplArgList)
Emits an AST template argument list info.
Definition: ASTWriter.cpp:6803
void AddCXXDefinitionData(const CXXRecordDecl *D)
Definition: ASTWriter.cpp:6893
void AddVarDeclInit(const VarDecl *VD)
Emit information about the initializer of a VarDecl.
Definition: ASTWriter.cpp:6986
void writeStmtRef(const Stmt *S)
uint64_t & operator[](size_t N)
void AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg)
Emits a template argument location.
Definition: ASTWriter.cpp:6459
void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS)
Emit a nested name specifier with source-location information.
Definition: ASTWriter.cpp:6722
void writeOpenACCClause(const OpenACCClause *C)
Writes out a single OpenACC Clause.
Definition: ASTWriter.cpp:8295
void writeBTFTypeTagAttr(const BTFTypeTagAttr *A)
Write an BTFTypeTagAttr object.
void AddAttr(const Attr *A)
Definition: ASTWriter.cpp:4907
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:89
void AddSourceRange(SourceRange Range, RecordDataImpl &Record, LocSeq *Seq=nullptr)
Emit a source range.
Definition: ASTWriter.cpp:6351
void AddPath(StringRef Path, RecordDataImpl &Record)
Add a path to the given record.
Definition: ASTWriter.cpp:5016
void AddVersionTuple(const VersionTuple &Version, RecordDataImpl &Record)
Add a version tuple to the given record.
Definition: ASTWriter.cpp:5036
void AddTypeRef(ASTContext &Context, QualType T, RecordDataImpl &Record)
Emit a reference to a type.
Definition: ASTWriter.cpp:6489
void AddString(StringRef Str, RecordDataImpl &Record)
Add a string to the given record.
Definition: ASTWriter.cpp:4983
void AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record)
Emit a reference to an identifier.
Definition: ASTWriter.cpp:6361
void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record, LocSeq *Seq=nullptr)
Emit a source location.
Definition: ASTWriter.cpp:6345
void AddDeclRef(const Decl *D, RecordDataImpl &Record)
Emit a reference to a declaration.
Definition: ASTWriter.cpp:6548
Attr - This represents one attribute.
Definition: Attr.h:43
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:1457
A reference to a concept and its template args, as it appears in the code.
Definition: ASTConcept.h:124
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
DeclarationNameLoc - Additional source/type location info for a declaration name.
The name of a declaration.
Represents a function declaration or definition.
Definition: Decl.h:1935
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...
Represents a clause with one or more 'var' objects, represented as an expr, as its arguments.
This is the base type for all OpenACC Clauses.
Definition: OpenACCClause.h:24
A (possibly-)qualified type.
Definition: Type.h:929
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:250
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:220
Stores a list of template parameters for a TemplateDecl and its derived classes.
Definition: DeclTemplate.h:73
[BoundsSafety] Represents information of declarations referenced by the arguments of the counted_by a...
Definition: Type.h:3226
ValueDecl * getDecl() const
Definition: Type.cpp:3879
Base wrapper for a particular "section" of type source info.
Definition: TypeLoc.h:59
A container of type source information.
Definition: Type.h:7902
Represents a variable declaration or definition.
Definition: Decl.h:882
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.
const FunctionProtoType * T
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:708
Location information for a TemplateArgument.
Definition: TemplateBase.h:472