clang 23.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
23
24namespace clang {
25
26class OpenACCClause;
27class TypeLoc;
28
29/// An object for streaming information to a record.
31 : 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.
78 ASTWriter::RecordDataImpl &getRecordData() const { return *Record; }
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
119 AddOffset(Offsets.LexicalOffset);
120 AddOffset(Offsets.VisibleOffset);
122 AddOffset(Offsets.TULocalOffset);
123 }
124
125 /// Add the given statement or expression to the queue of
126 /// statements to emit.
127 ///
128 /// This routine should be used when emitting types and declarations
129 /// that have expressions as part of their formulation. Once the
130 /// type or declaration has been written, Emit() will write
131 /// the corresponding statements just after the record.
132 void AddStmt(Stmt *S) {
133 StmtsToEmit.push_back(S);
134 }
135 void writeStmtRef(const Stmt *S) {
136 AddStmt(const_cast<Stmt*>(S));
137 }
138
139 void writeAttr(const Attr *A) { AddAttr(A); }
140
141 /// Write an BTFTypeTagAttr object.
142 void writeBTFTypeTagAttr(const BTFTypeTagAttr *A) { AddAttr(A); }
143
144 /// Add a definition for the given function to the queue of statements
145 /// to emit.
146 void AddFunctionDefinition(const FunctionDecl *FD);
147
148 /// Emit a source location.
150 return Writer->AddSourceLocation(Loc, *Record);
151 }
155
160
161 void writeHLSLSpirvOperand(SpirvOperand Op) {
162 QualType ResultType;
163 llvm::APInt Value;
164
165 if (Op.isConstant() || Op.isType())
166 ResultType = Op.getResultType();
167 if (Op.isConstant() || Op.isLiteral())
168 Value = Op.getValue();
169
170 Record->push_back(Op.getKind());
171 writeQualType(ResultType);
173 }
174
175 /// Emit a source range.
177 return Writer->AddSourceRange(Range, *Record);
178 }
179
180 void writeBool(bool Value) {
181 Record->push_back(Value);
182 }
183
184 void writeUInt32(uint32_t Value) {
185 Record->push_back(Value);
186 }
187
188 void writeUInt64(uint64_t Value) {
189 Record->push_back(Value);
190 }
191
193 Record->push_back(Value.toInternalRepresentation());
194 }
195
196 /// Emit an integral value.
197 void AddAPInt(const llvm::APInt &Value) {
199 }
200
201 /// Emit a signed integral value.
202 void AddAPSInt(const llvm::APSInt &Value) {
204 }
205
206 /// Emit a floating-point value.
207 void AddAPFloat(const llvm::APFloat &Value);
208
209 /// Emit an APvalue.
210 void AddAPValue(const APValue &Value) { writeAPValue(Value); }
211
212 /// Emit a reference to an identifier.
214 return Writer->AddIdentifierRef(II, *Record);
215 }
218 }
219
220 /// Emit a Selector (which is a smart pointer reference).
221 void AddSelectorRef(Selector S);
223 AddSelectorRef(sel);
224 }
225
226 /// Emit a CXXTemporary.
227 void AddCXXTemporary(const CXXTemporary *Temp);
228
229 /// Emit a C++ base specifier.
231
232 /// Emit a set of C++ base specifiers.
234
235 /// Emit a reference to a type.
237 return Writer->AddTypeRef(getASTContext(), T, *Record);
238 }
240 AddTypeRef(T);
241 }
242
243 /// Emits a reference to a declarator info.
245
246 /// Emits source location information for a type. Does not emit the type.
247 void AddTypeLoc(TypeLoc TL);
248
249 /// Emits a template argument location info.
251
252 /// Emits a template argument location.
254
255 /// Emits an AST template argument list info.
257 const ASTTemplateArgumentListInfo *ASTTemplArgList);
258
259 // Emits a concept reference.
261
262 /// Emit a reference to a declaration.
263 void AddDeclRef(const Decl *D) {
264 return Writer->AddDeclRef(D, *Record);
265 }
266 void writeDeclRef(const Decl *D) {
267 AddDeclRef(D);
268 }
269
270 /// Emit a declaration name.
272 writeDeclarationName(Name);
273 }
274
276 DeclarationName Name);
277 void AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo);
278
279 void AddQualifierInfo(const QualifierInfo &Info);
280
281 /// Emit a nested name specifier.
285
286 /// Emit a nested name specifier with source-location information.
288
289 /// Emit a template name.
291 writeTemplateName(Name);
292 }
293
294 /// Emit a template argument.
296 writeTemplateArgument(Arg);
297 }
298
299 /// Emit a template parameter list.
300 void AddTemplateParameterList(const TemplateParameterList *TemplateParams);
301
302 /// Emit a template argument list.
303 void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs);
304
305 /// Emit a UnresolvedSet structure.
307
308 /// Emit a CXXCtorInitializer array.
310
311 void AddCXXDefinitionData(const CXXRecordDecl *D);
312
313 /// Emit information about the initializer of a VarDecl.
314 void AddVarDeclInit(const VarDecl *VD);
315
316 /// Write an OMPTraitInfo object.
317 void writeOMPTraitInfo(const OMPTraitInfo *TI);
318
320
321 /// Writes data related to the OpenMP directives.
323
325
327
328 /// Writes out a single OpenACC Clause.
330
331 /// Writes out a list of OpenACC clauses.
333
334 void AddOpenACCRoutineDeclAttr(const OpenACCRoutineDeclAttr *A);
335
336 /// Emit a string.
337 void AddString(StringRef Str) {
338 return Writer->AddString(Str, *Record);
339 }
340
341 /// Emit a path.
342 void AddPath(StringRef Path) {
343 return Writer->AddPath(Path, *Record);
344 }
345
346 /// Emit a version tuple.
347 void AddVersionTuple(const VersionTuple &Version) {
348 return Writer->AddVersionTuple(Version, *Record);
349 }
350
351 // Emit an attribute.
352 void AddAttr(const Attr *A);
353
354 /// Emit a list of attributes.
356};
357
358} // end namespace clang
359
360#endif
This file defines OpenMP AST classes for clauses.
Contains data for OpenMP directives: clauses, children expressions/statements (helpers for codegen) a...
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:226
void AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo)
ASTRecordWriter & operator=(const ASTRecordWriter &)=delete
void AddCXXBaseSpecifiers(ArrayRef< CXXBaseSpecifier > Bases)
Emit a set of C++ base specifiers.
void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs)
Emit a template argument list.
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.
void writeOMPTraitInfo(const OMPTraitInfo *TI)
Write an OMPTraitInfo object.
void AddCXXBaseSpecifier(const CXXBaseSpecifier &Base)
Emit a C++ base specifier.
ASTWriter::RecordDataImpl & getRecordData() const
Extract the underlying record storage.
void writeOMPClause(OMPClause *C)
void writeBool(bool Value)
void AddAPValue(const APValue &Value)
Emit an APvalue.
void AddUnresolvedSet(const ASTUnresolvedSet &Set)
Emit a UnresolvedSet structure.
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 AddTemplateArgumentLocInfo(const TemplateArgumentLoc &Arg)
Emits a template argument location info.
void AddTypeLoc(TypeLoc TL)
Emits source location information for a type. Does not emit the type.
void AddSelectorRef(Selector S)
Emit a Selector (which is a smart pointer reference).
void writeUInt64(uint64_t Value)
uint64_t EmitStmt(unsigned Code, unsigned Abbrev=0)
Emit the record to the stream, preceded by its substatements.
void AddPath(StringRef Path)
Emit a path.
void writeSourceLocation(SourceLocation Loc)
void AddOffset(uint64_t BitOffset)
Add a bit offset into the record.
void writeUnsignedOrNone(UnsignedOrNone Value)
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.
void push_back(uint64_t N)
Minimal vector-like interface.
void append(InputIterator begin, InputIterator end)
void AddCXXCtorInitializers(ArrayRef< CXXCtorInitializer * > CtorInits)
Emit a CXXCtorInitializer array.
void AddTemplateParameterList(const TemplateParameterList *TemplateParams)
Emit a template parameter list.
void AddTemplateArgument(const TemplateArgument &Arg)
Emit a template argument.
void AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc, DeclarationName Name)
void writeSelector(Selector sel)
void writeOpenACCIntExprList(ArrayRef< Expr * > Exprs)
void AddTemplateName(TemplateName Name)
Emit a template name.
void AddAPFloat(const llvm::APFloat &Value)
Emit a floating-point value.
void AddTypeSourceInfo(TypeSourceInfo *TInfo)
Emits a reference to a declarator info.
void AddQualifierInfo(const QualifierInfo &Info)
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.
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)
void AddNestedNameSpecifier(NestedNameSpecifier NNS)
Emit a nested name specifier.
void AddVersionTuple(const VersionTuple &Version)
Emit a version tuple.
void AddString(StringRef Str)
Emit a string.
void AddSourceRange(SourceRange Range)
Emit a source range.
void writeIdentifier(const IdentifierInfo *II)
void writeAttr(const Attr *A)
void AddAPInt(const llvm::APInt &Value)
Emit an integral value.
void AddSourceLocation(SourceLocation Loc)
Emit a source location.
void writeOpenACCVarList(const OpenACCClauseWithVarList *C)
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.
void AddASTTemplateArgumentListInfo(const ASTTemplateArgumentListInfo *ASTTemplArgList)
Emits an AST template argument list info.
void AddCXXDefinitionData(const CXXRecordDecl *D)
void AddVarDeclInit(const VarDecl *VD)
Emit information about the initializer of a VarDecl.
void writeStmtRef(const Stmt *S)
uint64_t & operator[](size_t N)
void AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg)
Emits a template argument location.
void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS)
Emit a nested name specifier with source-location information.
void AddLookupOffsets(const LookupBlockOffsets &Offsets)
void AddOpenACCRoutineDeclAttr(const OpenACCRoutineDeclAttr *A)
void writeOpenACCClause(const OpenACCClause *C)
Writes out a single OpenACC Clause.
void writeHLSLSpirvOperand(SpirvOperand Op)
void writeBTFTypeTagAttr(const BTFTypeTagAttr *A)
Write an BTFTypeTagAttr object.
void AddAttr(const Attr *A)
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:97
SmallVectorImpl< uint64_t > RecordDataImpl
Definition ASTWriter.h:103
Attr - This represents one attribute.
Definition Attr.h:46
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:1460
A reference to a concept and its template args, as it appears in the code.
Definition ASTConcept.h:130
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:2000
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>::".
This is a basic class for representing single OpenMP clause.
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.
A (possibly-)qualified type.
Definition TypeBase.h:937
Smart pointer class that efficiently represents Objective-C method names.
Encodes a location in the source.
A trivial tuple used to represent a source range.
Stmt - This represents one statement.
Definition Stmt.h:86
A template argument list.
Location wrapper for a TemplateArgument.
Represents a template argument.
Represents a C++ template name within the type system.
Stores a list of template parameters for a TemplateDecl and its derived classes.
[BoundsSafety] Represents information of declarations referenced by the arguments of the counted_by a...
Definition TypeBase.h:3364
ValueDecl * getDecl() const
Definition Type.cpp:4032
Base wrapper for a particular "section" of type source info.
Definition TypeLoc.h:59
A container of type source information.
Definition TypeBase.h:8359
Represents a variable declaration or definition.
Definition Decl.h:926
DataStreamBasicWriter provides convenience implementations for many BasicWriter methods based on the ...
The JSON file list parser is used to communicate input to InstallAPI.
Represents an explicit template argument list in C++, e.g., the "<int>" in "sort<int>".
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:753