MLIR 24.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 the C++ headers declaring the C++ type of this parameter. Only
91 /// parameters that are attribute or type constraints specify headers.
92 std::vector<StringRef> getCppIncludes() const;
93
94 /// Get an optional C++ parameter parser.
95 std::optional<StringRef> getParser() const;
96
97 /// If this is a type constraint, return it.
98 std::optional<Constraint> getConstraint() const;
99
100 /// Get an optional C++ parameter printer.
101 std::optional<StringRef> getPrinter() const;
102
103 /// Get a description of this parameter for documentation purposes.
104 std::optional<StringRef> getSummary() const;
105
106 /// Get the assembly syntax documentation.
107 StringRef getSyntax() const;
108
109 /// Returns true if the parameter is optional.
110 bool isOptional() const;
111
112 /// Get the default value of the parameter if it has one.
113 std::optional<StringRef> getDefaultValue() const;
114
115 /// Return the underlying def of this parameter.
116 const llvm::Init *getDef() const;
117
118 /// The parameter is pointer-comparable.
119 bool operator==(const AttrOrTypeParameter &other) const {
120 return def == other.def && index == other.index;
121 }
122 bool operator!=(const AttrOrTypeParameter &other) const {
123 return !(*this == other);
124 }
125
126private:
127 /// A parameter can be either a string or a def. Get a potentially null value
128 /// from the def.
129 template <typename InitT>
130 auto getDefValue(StringRef name) const;
131
132 /// The underlying tablegen parameter list this parameter is a part of.
133 const llvm::DagInit *def;
134 /// The index of the parameter within the parameter list (`def`).
135 unsigned index;
136};
137
138//===----------------------------------------------------------------------===//
139// AttributeSelfTypeParameter
140//===----------------------------------------------------------------------===//
141
142// A wrapper class for the AttributeSelfTypeParameter tblgen class. This
143// represents a parameter of mlir::Type that is the value type of an AttrDef.
145public:
146 static bool classof(const AttrOrTypeParameter *param);
147};
148
149//===----------------------------------------------------------------------===//
150// AttrOrTypeDef
151//===----------------------------------------------------------------------===//
152
153/// Wrapper class that contains a TableGen AttrOrTypeDef's record and provides
154/// helper methods for accessing them.
156public:
157 explicit AttrOrTypeDef(const llvm::Record *def);
158
159 /// Get the dialect for which this def belongs.
160 Dialect getDialect() const;
161
162 /// Returns the name of this AttrOrTypeDef record.
163 StringRef getName() const;
164
165 /// Query functions for the documentation of the def.
166 bool hasDescription() const;
167 StringRef getDescription() const;
168 bool hasSummary() const;
169 StringRef getSummary() const;
170
171 /// Returns the name of the C++ class to generate.
172 StringRef getCppClassName() const;
173
174 /// Returns the name of the C++ base class to use when generating this def.
175 StringRef getCppBaseClassName() const;
176
177 /// Returns the name of the storage class for this def.
178 StringRef getStorageClassName() const;
179
180 /// Returns the C++ namespace for this def's storage class.
181 StringRef getStorageNamespace() const;
182
183 /// Returns true if we should generate the storage class.
184 bool genStorageClass() const;
185
186 /// Indicates whether or not to generate the storage class constructor.
187 bool hasStorageCustomConstructor() const;
188
189 /// Get the parameters of this attribute or type.
191
192 /// Return the number of parameters
193 unsigned getNumParameters() const;
194
195 /// Return the keyword/mnemonic to use in the printer/parser methods if we are
196 /// supposed to auto-generate them.
197 std::optional<StringRef> getMnemonic() const;
198
199 /// Returns if the attribute or type has a custom assembly format implemented
200 /// in C++. Corresponds to the `hasCustomAssemblyFormat` field.
201 bool hasCustomAssemblyFormat() const;
202
203 /// Returns the custom assembly format, if one was specified.
204 std::optional<StringRef> getAssemblyFormat() const;
205
206 /// Returns true if the accessors based on the parameters should be generated.
207 bool genAccessors() const;
208
209 /// Return true if we need to generate the verify declaration and getChecked
210 /// method.
211 bool genVerifyDecl() const;
212
213 /// Return true if we need to generate any type constraint verification and
214 /// the getChecked method.
215 bool genVerifyInvariantsImpl() const;
216
217 /// Returns the def's extra class declaration code.
218 std::optional<StringRef> getExtraDecls() const;
219
220 /// Returns the def's extra class definition code.
221 std::optional<StringRef> getExtraDefs() const;
222
223 /// Returns true if we need to generate a default 'getAlias' implementation
224 /// using the mnemonic.
225 bool genMnemonicAlias() const;
226
227 /// Get the code location (for error printing).
228 ArrayRef<SMLoc> getLoc() const;
229
230 /// Returns true if the default get/getChecked methods should be skipped
231 /// during generation.
232 bool skipDefaultBuilders() const;
233
234 /// Returns the builders of this def.
236
237 /// Returns the traits of this def.
238 ArrayRef<Trait> getTraits() const { return traits; }
239
240 /// Returns whether two AttrOrTypeDefs are equal by checking the equality of
241 /// the underlying record.
242 bool operator==(const AttrOrTypeDef &other) const;
243
244 /// Compares two AttrOrTypeDefs by comparing the names of the dialects.
245 bool operator<(const AttrOrTypeDef &other) const;
246
247 /// Returns whether the AttrOrTypeDef is defined.
248 operator bool() const { return def != nullptr; }
249
250 /// Return the underlying def.
251 const llvm::Record *getDef() const { return def; }
252
253protected:
254 const llvm::Record *def;
255
256 /// The builders of this definition.
258
259 /// The traits of this definition.
261
262 /// The parameters of this attribute or type.
264};
265
266//===----------------------------------------------------------------------===//
267// AttrDef
268//===----------------------------------------------------------------------===//
269
270/// This class represents a wrapper around a tablegen AttrDef record.
271class AttrDef : public AttrOrTypeDef {
272public:
274
275 /// Returns the attributes value type builder code block, or std::nullopt if
276 /// it doesn't have one.
277 std::optional<StringRef> getTypeBuilder() const;
278
279 static bool classof(const AttrOrTypeDef *def);
280
281 /// Get the unique attribute name "dialect.attrname".
282 StringRef getAttrName() const;
283};
284
285//===----------------------------------------------------------------------===//
286// TypeDef
287//===----------------------------------------------------------------------===//
288
289/// This class represents a wrapper around a tablegen TypeDef record.
290class TypeDef : public AttrOrTypeDef {
291public:
293
294 static bool classof(const AttrOrTypeDef *def);
295
296 /// Get the unique type name "dialect.typename".
297 StringRef getTypeName() const;
298};
299
300} // namespace tblgen
301} // namespace mlir
302
303#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)
std::vector< StringRef > getCppIncludes() const
Get the C++ headers declaring the C++ type of this parameter.
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.