MLIR 22.0.0git
AttrOrTypeDef.h
Go to the documentation of this file.
1//===-- AttrOrTypeDef.h - Wrapper for attr and type definitions -*- 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// AttrOrTypeDef, AttrDef, and TypeDef wrappers to simplify using TableGen
10// Record defining a MLIR attributes and types.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef MLIR_TABLEGEN_ATTRORTYPEDEF_H
15#define MLIR_TABLEGEN_ATTRORTYPEDEF_H
16
17#include "mlir/Support/LLVM.h"
20#include "mlir/TableGen/Trait.h"
21
22namespace llvm {
23class DagInit;
24class Record;
25class SMLoc;
26} // namespace llvm
27
28namespace mlir {
29namespace tblgen {
30class Dialect;
31
32//===----------------------------------------------------------------------===//
33// AttrOrTypeBuilder
34//===----------------------------------------------------------------------===//
35
36/// Wrapper class that represents a Tablegen AttrOrTypeBuilder.
37class AttrOrTypeBuilder : public Builder {
38public:
39 using Builder::Builder;
40
41 /// Returns an optional builder return type.
42 std::optional<StringRef> getReturnType() const;
43
44 /// Returns true if this builder is able to infer the MLIRContext parameter.
45 bool hasInferredContextParameter() const;
46};
47
48//===----------------------------------------------------------------------===//
49// AttrOrTypeParameter
50//===----------------------------------------------------------------------===//
51
52/// A wrapper class for tblgen AttrOrTypeParameter, arrays of which belong to
53/// AttrOrTypeDefs to parameterize them.
55public:
56 explicit AttrOrTypeParameter(const llvm::DagInit *def, unsigned index)
57 : def(def), index(index) {}
58
59 /// Returns true if the parameter is anonymous (has no name).
60 bool isAnonymous() const;
61
62 /// Get the parameter name.
63 StringRef getName() const;
64
65 /// Get the parameter accessor name.
66 std::string getAccessorName() const;
67
68 /// If specified, get the custom allocator code for this parameter.
69 std::optional<StringRef> getAllocator() const;
70
71 /// Return true if user defined comparator is specified.
72 bool hasCustomComparator() const;
73
74 /// Get the custom comparator code for this parameter or fallback to the
75 /// default.
76 StringRef getComparator() const;
77
78 /// Get the C++ type of this parameter.
79 StringRef getCppType() const;
80
81 /// Get the C++ accessor type of this parameter.
82 StringRef getCppAccessorType() const;
83
84 /// Get the C++ storage type of this parameter.
85 StringRef getCppStorageType() const;
86
87 /// Get the C++ code to convert from the storage type to the parameter type.
88 StringRef getConvertFromStorage() const;
89
90 /// Get an optional C++ parameter parser.
91 std::optional<StringRef> getParser() const;
92
93 /// If this is a type constraint, return it.
94 std::optional<Constraint> getConstraint() const;
95
96 /// Get an optional C++ parameter printer.
97 std::optional<StringRef> getPrinter() const;
98
99 /// Get a description of this parameter for documentation purposes.
100 std::optional<StringRef> getSummary() const;
101
102 /// Get the assembly syntax documentation.
103 StringRef getSyntax() const;
104
105 /// Returns true if the parameter is optional.
106 bool isOptional() const;
107
108 /// Get the default value of the parameter if it has one.
109 std::optional<StringRef> getDefaultValue() const;
110
111 /// Return the underlying def of this parameter.
112 const llvm::Init *getDef() const;
113
114 /// The parameter is pointer-comparable.
115 bool operator==(const AttrOrTypeParameter &other) const {
116 return def == other.def && index == other.index;
117 }
118 bool operator!=(const AttrOrTypeParameter &other) const {
119 return !(*this == other);
120 }
121
122private:
123 /// A parameter can be either a string or a def. Get a potentially null value
124 /// from the def.
125 template <typename InitT>
126 auto getDefValue(StringRef name) const;
127
128 /// The underlying tablegen parameter list this parameter is a part of.
129 const llvm::DagInit *def;
130 /// The index of the parameter within the parameter list (`def`).
131 unsigned index;
132};
133
134//===----------------------------------------------------------------------===//
135// AttributeSelfTypeParameter
136//===----------------------------------------------------------------------===//
137
138// A wrapper class for the AttributeSelfTypeParameter tblgen class. This
139// represents a parameter of mlir::Type that is the value type of an AttrDef.
141public:
142 static bool classof(const AttrOrTypeParameter *param);
143};
144
145//===----------------------------------------------------------------------===//
146// AttrOrTypeDef
147//===----------------------------------------------------------------------===//
148
149/// Wrapper class that contains a TableGen AttrOrTypeDef's record and provides
150/// helper methods for accessing them.
152public:
153 explicit AttrOrTypeDef(const llvm::Record *def);
154
155 /// Get the dialect for which this def belongs.
156 Dialect getDialect() const;
157
158 /// Returns the name of this AttrOrTypeDef record.
159 StringRef getName() const;
160
161 /// Query functions for the documentation of the def.
162 bool hasDescription() const;
163 StringRef getDescription() const;
164 bool hasSummary() const;
165 StringRef getSummary() const;
166
167 /// Returns the name of the C++ class to generate.
168 StringRef getCppClassName() const;
169
170 /// Returns the name of the C++ base class to use when generating this def.
171 StringRef getCppBaseClassName() const;
172
173 /// Returns the name of the storage class for this def.
174 StringRef getStorageClassName() const;
175
176 /// Returns the C++ namespace for this def's storage class.
177 StringRef getStorageNamespace() const;
178
179 /// Returns true if we should generate the storage class.
180 bool genStorageClass() const;
181
182 /// Indicates whether or not to generate the storage class constructor.
183 bool hasStorageCustomConstructor() const;
184
185 /// Get the parameters of this attribute or type.
187
188 /// Return the number of parameters
189 unsigned getNumParameters() const;
190
191 /// Return the keyword/mnemonic to use in the printer/parser methods if we are
192 /// supposed to auto-generate them.
193 std::optional<StringRef> getMnemonic() const;
194
195 /// Returns if the attribute or type has a custom assembly format implemented
196 /// in C++. Corresponds to the `hasCustomAssemblyFormat` field.
197 bool hasCustomAssemblyFormat() const;
198
199 /// Returns the custom assembly format, if one was specified.
200 std::optional<StringRef> getAssemblyFormat() const;
201
202 /// Returns true if the accessors based on the parameters should be generated.
203 bool genAccessors() const;
204
205 /// Return true if we need to generate the verify declaration and getChecked
206 /// method.
207 bool genVerifyDecl() const;
208
209 /// Return true if we need to generate any type constraint verification and
210 /// the getChecked method.
211 bool genVerifyInvariantsImpl() const;
212
213 /// Returns the def's extra class declaration code.
214 std::optional<StringRef> getExtraDecls() const;
215
216 /// Returns the def's extra class definition code.
217 std::optional<StringRef> getExtraDefs() const;
218
219 /// Returns true if we need to generate a default 'getAlias' implementation
220 /// using the mnemonic.
221 bool genMnemonicAlias() const;
222
223 /// Get the code location (for error printing).
224 ArrayRef<SMLoc> getLoc() const;
225
226 /// Returns true if the default get/getChecked methods should be skipped
227 /// during generation.
228 bool skipDefaultBuilders() const;
229
230 /// Returns the builders of this def.
232
233 /// Returns the traits of this def.
234 ArrayRef<Trait> getTraits() const { return traits; }
235
236 /// Returns whether two AttrOrTypeDefs are equal by checking the equality of
237 /// the underlying record.
238 bool operator==(const AttrOrTypeDef &other) const;
239
240 /// Compares two AttrOrTypeDefs by comparing the names of the dialects.
241 bool operator<(const AttrOrTypeDef &other) const;
242
243 /// Returns whether the AttrOrTypeDef is defined.
244 operator bool() const { return def != nullptr; }
245
246 /// Return the underlying def.
247 const llvm::Record *getDef() const { return def; }
248
249protected:
250 const llvm::Record *def;
251
252 /// The builders of this definition.
254
255 /// The traits of this definition.
257
258 /// The parameters of this attribute or type.
260};
261
262//===----------------------------------------------------------------------===//
263// AttrDef
264//===----------------------------------------------------------------------===//
265
266/// This class represents a wrapper around a tablegen AttrDef record.
267class AttrDef : public AttrOrTypeDef {
268public:
270
271 /// Returns the attributes value type builder code block, or std::nullopt if
272 /// it doesn't have one.
273 std::optional<StringRef> getTypeBuilder() const;
274
275 static bool classof(const AttrOrTypeDef *def);
276
277 /// Get the unique attribute name "dialect.attrname".
278 StringRef getAttrName() const;
279};
280
281//===----------------------------------------------------------------------===//
282// TypeDef
283//===----------------------------------------------------------------------===//
284
285/// This class represents a wrapper around a tablegen TypeDef record.
286class TypeDef : public AttrOrTypeDef {
287public:
289
290 static bool classof(const AttrOrTypeDef *def);
291
292 /// Get the unique type name "dialect.typename".
293 StringRef getTypeName() const;
294};
295
296} // namespace tblgen
297} // namespace mlir
298
299#endif // MLIR_TABLEGEN_ATTRORTYPEDEF_H
This class represents a wrapper around a tablegen AttrDef record.
AttrOrTypeDef(const llvm::Record *def)
StringRef getAttrName() const
Get the unique attribute name "dialect.attrname".
std::optional< StringRef > getTypeBuilder() const
Returns the attributes value type builder code block, or std::nullopt if it doesn't have one.
static bool classof(const AttrOrTypeDef *def)
Wrapper class that represents a Tablegen AttrOrTypeBuilder.
bool hasInferredContextParameter() const
Returns true if this builder is able to infer the MLIRContext parameter.
Builder(const llvm::Record *record, ArrayRef< SMLoc > loc)
Construct a builder from the given Record instance.
std::optional< StringRef > getReturnType() const
Returns an optional builder return type.
Wrapper class that contains a TableGen AttrOrTypeDef's record and provides helper methods for accessi...
SmallVector< AttrOrTypeParameter > parameters
The parameters of this attribute or type.
bool hasCustomAssemblyFormat() const
Returns if the attribute or type has a custom assembly format implemented in C++.
std::optional< StringRef > getMnemonic() const
Return the keyword/mnemonic to use in the printer/parser methods if we are supposed to auto-generate ...
bool operator==(const AttrOrTypeDef &other) const
Returns whether two AttrOrTypeDefs are equal by checking the equality of the underlying record.
StringRef getCppClassName() const
Returns the name of the C++ class to generate.
AttrOrTypeDef(const llvm::Record *def)
ArrayRef< AttrOrTypeBuilder > getBuilders() const
Returns the builders of this def.
const llvm::Record * def
StringRef getStorageNamespace() const
Returns the C++ namespace for this def's storage class.
bool genVerifyDecl() const
Return true if we need to generate the verify declaration and getChecked method.
bool genMnemonicAlias() const
Returns true if we need to generate a default 'getAlias' implementation using the mnemonic.
ArrayRef< SMLoc > getLoc() const
Get the code location (for error printing).
bool hasDescription() const
Query functions for the documentation of the def.
StringRef getCppBaseClassName() const
Returns the name of the C++ base class to use when generating this def.
const llvm::Record * getDef() const
Return the underlying def.
Dialect getDialect() const
Get the dialect for which this def belongs.
SmallVector< Trait > traits
The traits of this definition.
std::optional< StringRef > getExtraDefs() const
Returns the def's extra class definition code.
ArrayRef< Trait > getTraits() const
Returns the traits of this def.
bool genStorageClass() const
Returns true if we should generate the storage class.
StringRef getDescription() const
std::optional< StringRef > getExtraDecls() const
Returns the def's extra class declaration code.
bool genVerifyInvariantsImpl() const
Return true if we need to generate any type constraint verification and the getChecked method.
StringRef getStorageClassName() const
Returns the name of the storage class for this def.
SmallVector< AttrOrTypeBuilder > builders
The builders of this definition.
bool hasStorageCustomConstructor() const
Indicates whether or not to generate the storage class constructor.
bool skipDefaultBuilders() const
Returns true if the default get/getChecked methods should be skipped during generation.
ArrayRef< AttrOrTypeParameter > getParameters() const
Get the parameters of this attribute or type.
unsigned getNumParameters() const
Return the number of parameters.
std::optional< StringRef > getAssemblyFormat() const
Returns the custom assembly format, if one was specified.
bool operator<(const AttrOrTypeDef &other) const
Compares two AttrOrTypeDefs by comparing the names of the dialects.
StringRef getName() const
Returns the name of this AttrOrTypeDef record.
bool genAccessors() const
Returns true if the accessors based on the parameters should be generated.
bool hasCustomComparator() const
Return true if user defined comparator is specified.
std::optional< StringRef > getParser() const
Get an optional C++ parameter parser.
StringRef getSyntax() const
Get the assembly syntax documentation.
std::optional< Constraint > getConstraint() const
If this is a type constraint, return it.
StringRef getComparator() const
Get the custom comparator code for this parameter or fallback to the default.
StringRef getName() const
Get the parameter name.
const llvm::Init * getDef() const
Return the underlying def of this parameter.
std::optional< StringRef > getPrinter() const
Get an optional C++ parameter printer.
bool operator!=(const AttrOrTypeParameter &other) const
StringRef getConvertFromStorage() const
Get the C++ code to convert from the storage type to the parameter type.
bool operator==(const AttrOrTypeParameter &other) const
The parameter is pointer-comparable.
std::optional< StringRef > getDefaultValue() const
Get the default value of the parameter if it has one.
bool isAnonymous() const
Returns true if the parameter is anonymous (has no name).
std::string getAccessorName() const
Get the parameter accessor name.
StringRef getCppType() const
Get the C++ type of this parameter.
std::optional< StringRef > getSummary() const
Get a description of this parameter for documentation purposes.
StringRef getCppStorageType() const
Get the C++ storage type of this parameter.
bool isOptional() const
Returns true if the parameter is optional.
std::optional< StringRef > getAllocator() const
If specified, get the custom allocator code for this parameter.
StringRef getCppAccessorType() const
Get the C++ accessor type of this parameter.
AttrOrTypeParameter(const llvm::DagInit *def, unsigned index)
static bool classof(const AttrOrTypeParameter *param)
Builder(const llvm::Record *record, ArrayRef< SMLoc > loc)
Construct a builder from the given Record instance.
This class represents a wrapper around a tablegen TypeDef record.
AttrOrTypeDef(const llvm::Record *def)
StringRef getTypeName() const
Get the unique type name "dialect.typename".
static bool classof(const AttrOrTypeDef *def)
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition CallGraph.h:229
Include the generated interface declarations.