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 
19 #include "clang/AST/OpenMPClause.h"
22 
23 namespace clang {
24 
25 class OpenACCClause;
26 class 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 
61 public:
62  /// Construct a ASTRecordWriter that uses the default encoding scheme.
64  : DataStreamBasicWriter(W.getASTContext()), Writer(&W), Record(&Record) {}
65 
66  /// Construct a ASTRecordWriter that uses the same encoding scheme as another
67  /// ASTRecordWriter.
69  : DataStreamBasicWriter(Parent.getASTContext()), Writer(Parent.Writer),
70  Record(&Record) {}
71 
72  /// Copying an ASTRecordWriter is almost certainly a bug.
73  ASTRecordWriter(const ASTRecordWriter &) = delete;
75 
76  /// Extract the underlying record storage.
78 
79  /// Minimal vector-like interface.
80  /// @{
81  void push_back(uint64_t N) { Record->push_back(N); }
82  template<typename InputIterator>
83  void append(InputIterator begin, InputIterator end) {
84  Record->append(begin, end);
85  }
86  bool empty() const { return Record->empty(); }
87  size_t size() const { return Record->size(); }
88  uint64_t &operator[](size_t N) { return (*Record)[N]; }
89  /// @}
90 
91  /// Emit the record to the stream, followed by its substatements, and
92  /// return its offset.
93  // FIXME: Allow record producers to suggest Abbrevs.
94  uint64_t Emit(unsigned Code, unsigned Abbrev = 0) {
95  uint64_t Offset = Writer->Stream.GetCurrentBitNo();
96  PrepareToEmit(Offset);
97  Writer->Stream.EmitRecord(Code, *Record, Abbrev);
98  FlushStmts();
99  return Offset;
100  }
101 
102  /// Emit the record to the stream, preceded by its substatements.
103  uint64_t EmitStmt(unsigned Code, unsigned Abbrev = 0) {
104  FlushSubStmts();
105  PrepareToEmit(Writer->Stream.GetCurrentBitNo());
106  Writer->Stream.EmitRecord(Code, *Record, Abbrev);
107  return Writer->Stream.GetCurrentBitNo();
108  }
109 
110  /// Add a bit offset into the record. This will be converted into an
111  /// offset relative to the current record when emitted.
112  void AddOffset(uint64_t BitOffset) {
113  OffsetIndices.push_back(Record->size());
114  Record->push_back(BitOffset);
115  }
116 
117  /// Add the given statement or expression to the queue of
118  /// statements to emit.
119  ///
120  /// This routine should be used when emitting types and declarations
121  /// that have expressions as part of their formulation. Once the
122  /// type or declaration has been written, Emit() will write
123  /// the corresponding statements just after the record.
124  void AddStmt(Stmt *S) {
125  StmtsToEmit.push_back(S);
126  }
127  void writeStmtRef(const Stmt *S) {
128  AddStmt(const_cast<Stmt*>(S));
129  }
130 
131  /// Write an BTFTypeTagAttr object.
132  void writeBTFTypeTagAttr(const BTFTypeTagAttr *A) { AddAttr(A); }
133 
134  /// Add a definition for the given function to the queue of statements
135  /// to emit.
136  void AddFunctionDefinition(const FunctionDecl *FD);
137 
138  /// Emit a source location.
139  void AddSourceLocation(SourceLocation Loc, LocSeq *Seq = nullptr) {
140  return Writer->AddSourceLocation(Loc, *Record, Seq);
141  }
144  }
145 
147  writeDeclRef(Info.getDecl());
148  writeBool(Info.isDeref());
149  }
150 
151  /// Emit a source range.
152  void AddSourceRange(SourceRange Range, LocSeq *Seq = nullptr) {
153  return Writer->AddSourceRange(Range, *Record, Seq);
154  }
155 
156  void writeBool(bool Value) {
157  Record->push_back(Value);
158  }
159 
160  void writeUInt32(uint32_t Value) {
161  Record->push_back(Value);
162  }
163 
165  Record->push_back(Value);
166  }
167 
168  /// Emit an integral value.
169  void AddAPInt(const llvm::APInt &Value) {
170  writeAPInt(Value);
171  }
172 
173  /// Emit a signed integral value.
174  void AddAPSInt(const llvm::APSInt &Value) {
176  }
177 
178  /// Emit a floating-point value.
179  void AddAPFloat(const llvm::APFloat &Value);
180 
181  /// Emit an APvalue.
182  void AddAPValue(const APValue &Value) { writeAPValue(Value); }
183 
184  /// Emit a reference to an identifier.
186  return Writer->AddIdentifierRef(II, *Record);
187  }
188  void writeIdentifier(const IdentifierInfo *II) {
189  AddIdentifierRef(II);
190  }
191 
192  /// Emit a Selector (which is a smart pointer reference).
193  void AddSelectorRef(Selector S);
195  AddSelectorRef(sel);
196  }
197 
198  /// Emit a CXXTemporary.
199  void AddCXXTemporary(const CXXTemporary *Temp);
200 
201  /// Emit a C++ base specifier.
203 
204  /// Emit a set of C++ base specifiers.
206 
207  /// Emit a reference to a type.
209  return Writer->AddTypeRef(T, *Record);
210  }
212  AddTypeRef(T);
213  }
214 
215  /// Emits a reference to a declarator info.
216  void AddTypeSourceInfo(TypeSourceInfo *TInfo);
217 
218  /// Emits source location information for a type. Does not emit the type.
219  void AddTypeLoc(TypeLoc TL, LocSeq *Seq = nullptr);
220 
221  /// Emits a template argument location info.
223  const TemplateArgumentLocInfo &Arg);
224 
225  /// Emits a template argument location.
227 
228  /// Emits an AST template argument list info.
230  const ASTTemplateArgumentListInfo *ASTTemplArgList);
231 
232  // Emits a concept reference.
233  void AddConceptReference(const ConceptReference *CR);
234 
235  /// Emit a reference to a declaration.
236  void AddDeclRef(const Decl *D) {
237  return Writer->AddDeclRef(D, *Record);
238  }
239  void writeDeclRef(const Decl *D) {
240  AddDeclRef(D);
241  }
242 
243  /// Emit a declaration name.
245  writeDeclarationName(Name);
246  }
247 
248  void AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
249  DeclarationName Name);
250  void AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo);
251 
252  void AddQualifierInfo(const QualifierInfo &Info);
253 
254  /// Emit a nested name specifier.
257  }
258 
259  /// Emit a nested name specifier with source-location information.
261 
262  /// Emit a template name.
264  writeTemplateName(Name);
265  }
266 
267  /// Emit a template argument.
269  writeTemplateArgument(Arg);
270  }
271 
272  /// Emit a template parameter list.
273  void AddTemplateParameterList(const TemplateParameterList *TemplateParams);
274 
275  /// Emit a template argument list.
276  void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs);
277 
278  /// Emit a UnresolvedSet structure.
279  void AddUnresolvedSet(const ASTUnresolvedSet &Set);
280 
281  /// Emit a CXXCtorInitializer array.
283 
284  void AddCXXDefinitionData(const CXXRecordDecl *D);
285 
286  /// Emit information about the initializer of a VarDecl.
287  void AddVarDeclInit(const VarDecl *VD);
288 
289  /// Write an OMPTraitInfo object.
290  void writeOMPTraitInfo(const OMPTraitInfo *TI);
291 
292  void writeOMPClause(OMPClause *C);
293 
294  /// Writes data related to the OpenMP directives.
295  void writeOMPChildren(OMPChildren *Data);
296 
298 
300 
301  /// Writes out a single OpenACC Clause.
302  void writeOpenACCClause(const OpenACCClause *C);
303 
304  /// Writes out a list of OpenACC clauses.
306 
307  /// Emit a string.
308  void AddString(StringRef Str) {
309  return Writer->AddString(Str, *Record);
310  }
311 
312  /// Emit a path.
313  void AddPath(StringRef Path) {
314  return Writer->AddPath(Path, *Record);
315  }
316 
317  /// Emit a version tuple.
318  void AddVersionTuple(const VersionTuple &Version) {
319  return Writer->AddVersionTuple(Version, *Record);
320  }
321 
322  // Emit an attribute.
323  void AddAttr(const Attr *A);
324 
325  /// Emit a list of attributes.
327 };
328 
329 } // end namespace clang
330 
331 #endif
NodeId Parent
Definition: ASTDiff.cpp:191
llvm::APSInt APSInt
unsigned Offset
Definition: Format.cpp:2978
llvm::MachO::Record Record
Definition: MachO.h:31
This file defines OpenMP AST classes for clauses.
SourceRange Range
Definition: SemaObjC.cpp:754
SourceLocation Loc
Definition: SemaObjC.cpp:755
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:6301
void AddCXXBaseSpecifiers(ArrayRef< CXXBaseSpecifier > Bases)
Emit a set of C++ base specifiers.
Definition: ASTWriter.cpp:6441
void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs)
Emit a template argument list.
Definition: ASTWriter.cpp:6388
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:6019
void writeOMPTraitInfo(const OMPTraitInfo *TI)
Write an OMPTraitInfo object.
Definition: ASTWriter.cpp:7780
void AddCXXBaseSpecifier(const CXXBaseSpecifier &Base)
Emit a C++ base specifier.
Definition: ASTWriter.cpp:6417
void writeOMPClause(OMPClause *C)
Definition: ASTWriter.cpp:6986
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:6407
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:5996
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:8007
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:6075
void AddCXXCtorInitializers(ArrayRef< CXXCtorInitializer * > CtorInits)
Emit a CXXCtorInitializer array.
Definition: ASTWriter.cpp:6481
void AddTemplateParameterList(const TemplateParameterList *TemplateParams)
Emit a template parameter list.
Definition: ASTWriter.cpp:6369
void AddTemplateArgument(const TemplateArgument &Arg)
Emit a template argument.
void AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc, DeclarationName Name)
Definition: ASTWriter.cpp:6274
void writeSelector(Selector sel)
void writeOpenACCIntExprList(ArrayRef< Expr * > Exprs)
Definition: ASTWriter.cpp:7817
void AddTemplateName(TemplateName Name)
Emit a template name.
void AddAPFloat(const llvm::APFloat &Value)
Emit a floating-point value.
Definition: ASTWriter.cpp:5950
void AddTypeSourceInfo(TypeSourceInfo *TInfo)
Emits a reference to a declarator info.
Definition: ASTWriter.cpp:6065
ASTRecordWriter(ASTWriter &W, ASTWriter::RecordDataImpl &Record)
Construct a ASTRecordWriter that uses the default encoding scheme.
void AddQualifierInfo(const QualifierInfo &Info)
Definition: ASTWriter.cpp:6308
void writeUInt32(uint32_t Value)
ASTWriter::RecordDataImpl & getRecordData() const
Extract the underlying record storage.
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:7797
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:510
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:6023
void writeOpenACCVarList(const OpenACCClauseWithVarList *C)
Definition: ASTWriter.cpp:7811
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:4677
void AddASTTemplateArgumentListInfo(const ASTTemplateArgumentListInfo *ASTTemplArgList)
Emits an AST template argument list info.
Definition: ASTWriter.cpp:6396
void AddCXXDefinitionData(const CXXRecordDecl *D)
Definition: ASTWriter.cpp:6486
void AddVarDeclInit(const VarDecl *VD)
Emit information about the initializer of a VarDecl.
Definition: ASTWriter.cpp:6583
void writeStmtRef(const Stmt *S)
void AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg)
Emits a template argument location.
Definition: ASTWriter.cpp:6052
void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS)
Emit a nested name specifier with source-location information.
Definition: ASTWriter.cpp:6315
void writeOpenACCClause(const OpenACCClause *C)
Writes out a single OpenACC Clause.
Definition: ASTWriter.cpp:7823
uint64_t & operator[](size_t N)
void writeBTFTypeTagAttr(const BTFTypeTagAttr *A)
Write an BTFTypeTagAttr object.
ASTRecordWriter & operator=(const ASTRecordWriter &)=delete
void AddAttr(const Attr *A)
Definition: ASTWriter.cpp:4653
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:90
void AddSourceRange(SourceRange Range, RecordDataImpl &Record, LocSeq *Seq=nullptr)
Emit a source range.
Definition: ASTWriter.cpp:5944
void AddPath(StringRef Path, RecordDataImpl &Record)
Add a path to the given record.
Definition: ASTWriter.cpp:4756
void AddVersionTuple(const VersionTuple &Version, RecordDataImpl &Record)
Add a version tuple to the given record.
Definition: ASTWriter.cpp:4769
void AddString(StringRef Str, RecordDataImpl &Record)
Add a string to the given record.
Definition: ASTWriter.cpp:4728
void AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record)
Emit a reference to an identifier.
Definition: ASTWriter.cpp:5954
void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record, LocSeq *Seq=nullptr)
Emit a source location.
Definition: ASTWriter.cpp:5938
void AddTypeRef(QualType T, RecordDataImpl &Record)
Emit a reference to a type.
Definition: ASTWriter.cpp:6082
void AddDeclRef(const Decl *D, RecordDataImpl &Record)
Emit a reference to a declaration.
Definition: ASTWriter.cpp:6141
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:1453
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: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:1972
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:940
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
[BoundsSafety] Represents information of declarations referenced by the arguments of the counted_by a...
Definition: Type.h:3179
ValueDecl * getDecl() const
Definition: Type.cpp:3811
Base wrapper for a particular "section" of type source info.
Definition: TypeLoc.h:59
A container of type source information.
Definition: Type.h:7342
Represents a variable declaration or definition.
Definition: Decl.h:919
DataStreamBasicWriter provides convenience implementations for many BasicWriter methods based on the ...
llvm::APFloat APFloat
Definition: Floating.h:23
llvm::APInt APInt
Definition: Integral.h:29
The JSON file list parser is used to communicate input to InstallAPI.
@ Seq
'seq' clause, allowed on 'loop' and 'routine' directives.
const FunctionProtoType * T
unsigned long uint64_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:744
Location information for a TemplateArgument.
Definition: TemplateBase.h:472