clang 24.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
185 Record->push_back(Value);
186 }
187
188 void writeUInt64(uint64_t Value) {
189 Record->push_back(Value);
190 }
191
193 Record->push_back(llvm::to_underlying(Value));
194 }
195
197 Record->push_back(Value.toInternalRepresentation());
198 }
199
200 /// Emit an integral value.
201 void AddAPInt(const llvm::APInt &Value) {
203 }
204
205 /// Emit a signed integral value.
206 void AddAPSInt(const llvm::APSInt &Value) {
208 }
209
210 /// Emit a floating-point value.
211 void AddAPFloat(const llvm::APFloat &Value);
212
213 /// Emit an APvalue.
214 void AddAPValue(const APValue &Value) { writeAPValue(Value); }
215
216 /// Emit a reference to an identifier.
218 return Writer->AddIdentifierRef(II, *Record);
219 }
222 }
223
224 /// Emit a Selector (which is a smart pointer reference).
225 void AddSelectorRef(Selector S);
227 AddSelectorRef(sel);
228 }
229
230 /// Emit a CXXTemporary.
231 void AddCXXTemporary(const CXXTemporary *Temp);
232
233 /// Emit a C++ base specifier.
235
236 /// Emit a set of C++ base specifiers.
238
239 /// Emit a reference to a type.
241 return Writer->AddTypeRef(getASTContext(), T, *Record);
242 }
244 AddTypeRef(T);
245 }
246
247 /// Emits a reference to a declarator info.
249
250 /// Emits source location information for a type. Does not emit the type.
251 void AddTypeLoc(TypeLoc TL);
252
253 /// Emits a template argument location info.
255
256 /// Emits a template argument location.
258
259 /// Emits an AST template argument list info.
261 const ASTTemplateArgumentListInfo *ASTTemplArgList);
262
263 // Emits a concept reference.
265
266 /// Emit a reference to a declaration.
267 void AddDeclRef(const Decl *D) {
268 return Writer->AddDeclRef(D, *Record);
269 }
270 void writeDeclRef(const Decl *D) {
271 AddDeclRef(D);
272 }
273
274 /// Emit a declaration name.
276 writeDeclarationName(Name);
277 }
278
280 DeclarationName Name);
281 void AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo);
282
283 void AddQualifierInfo(const QualifierInfo &Info);
284
285 /// Emit a nested name specifier.
289
290 /// Emit a nested name specifier with source-location information.
292
293 /// Emit a template name.
295 writeTemplateName(Name);
296 }
297
298 /// Emit a template argument.
300 writeTemplateArgument(Arg);
301 }
302
303 /// Emit a template parameter list.
304 void AddTemplateParameterList(const TemplateParameterList *TemplateParams);
305
306 /// Emit a template argument list.
307 void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs);
308
309 /// Emit a UnresolvedSet structure.
311
312 /// Emit a CXXCtorInitializer array.
314
315 void AddCXXDefinitionData(const CXXRecordDecl *D);
316
317 /// Emit information about the initializer of a VarDecl.
318 void AddVarDeclInit(const VarDecl *VD);
319
320 /// Write an OMPTraitInfo object.
321 void writeOMPTraitInfo(const OMPTraitInfo *TI);
322
324
325 /// Writes data related to the OpenMP directives.
327
329
331
332 /// Writes out a single OpenACC Clause.
334
335 /// Writes out a list of OpenACC clauses.
337
338 void AddOpenACCRoutineDeclAttr(const OpenACCRoutineDeclAttr *A);
339
340 /// Emit a string.
341 void AddString(StringRef Str) {
342 return Writer->AddString(Str, *Record);
343 }
344
345 /// Emit a path.
346 void AddPath(StringRef Path) {
347 return Writer->AddPath(Path, *Record);
348 }
349
350 /// Emit a version tuple.
351 void AddVersionTuple(const VersionTuple &Version) {
352 return Writer->AddVersionTuple(Version, *Record);
353 }
354
355 // Emit an attribute.
356 void AddAttr(const Attr *A);
357
358 /// Emit a list of attributes.
360};
361
362} // end namespace clang
363
364#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:158
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:245
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 writeDynAllocKind(DynAllocKind 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:98
SmallVectorImpl< uint64_t > RecordDataImpl
Definition ASTWriter.h:104
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:1464
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:2059
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:938
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:85
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:3425
ValueDecl * getDecl() const
Definition Type.cpp:4251
Base wrapper for a particular "section" of type source info.
Definition TypeLoc.h:59
A container of type source information.
Definition TypeBase.h:8417
Represents a variable declaration or definition.
Definition Decl.h:933
DataStreamBasicWriter provides convenience implementations for many BasicWriter methods based on the ...
Top level wrappers for InstallAPI frontend operations.
DynAllocKind
Kind of source for a dynamic allocation.
Definition APValue.h:69
OptionalUnsigned< unsigned > UnsignedOrNone
const FunctionProtoType * T
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:754