MLIR  21.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"
18 #include "mlir/TableGen/Builder.h"
20 #include "mlir/TableGen/Trait.h"
21 
22 namespace llvm {
23 class DagInit;
24 class Record;
25 class SMLoc;
26 } // namespace llvm
27 
28 namespace mlir {
29 namespace tblgen {
30 class Dialect;
31 
32 //===----------------------------------------------------------------------===//
33 // AttrOrTypeBuilder
34 //===----------------------------------------------------------------------===//
35 
36 /// Wrapper class that represents a Tablegen AttrOrTypeBuilder.
37 class AttrOrTypeBuilder : public Builder {
38 public:
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.
55 public:
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 
122 private:
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.
141 public:
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.
152 public:
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  /// Get the code location (for error printing).
220  ArrayRef<SMLoc> getLoc() const;
221 
222  /// Returns true if the default get/getChecked methods should be skipped
223  /// during generation.
224  bool skipDefaultBuilders() const;
225 
226  /// Returns the builders of this def.
228 
229  /// Returns the traits of this def.
230  ArrayRef<Trait> getTraits() const { return traits; }
231 
232  /// Returns whether two AttrOrTypeDefs are equal by checking the equality of
233  /// the underlying record.
234  bool operator==(const AttrOrTypeDef &other) const;
235 
236  /// Compares two AttrOrTypeDefs by comparing the names of the dialects.
237  bool operator<(const AttrOrTypeDef &other) const;
238 
239  /// Returns whether the AttrOrTypeDef is defined.
240  operator bool() const { return def != nullptr; }
241 
242  /// Return the underlying def.
243  const llvm::Record *getDef() const { return def; }
244 
245 protected:
246  const llvm::Record *def;
247 
248  /// The builders of this definition.
250 
251  /// The traits of this definition.
253 
254  /// The parameters of this attribute or type.
256 };
257 
258 //===----------------------------------------------------------------------===//
259 // AttrDef
260 //===----------------------------------------------------------------------===//
261 
262 /// This class represents a wrapper around a tablegen AttrDef record.
263 class AttrDef : public AttrOrTypeDef {
264 public:
266 
267  /// Returns the attributes value type builder code block, or std::nullopt if
268  /// it doesn't have one.
269  std::optional<StringRef> getTypeBuilder() const;
270 
271  static bool classof(const AttrOrTypeDef *def);
272 
273  /// Get the unique attribute name "dialect.attrname".
274  StringRef getAttrName() const;
275 };
276 
277 //===----------------------------------------------------------------------===//
278 // TypeDef
279 //===----------------------------------------------------------------------===//
280 
281 /// This class represents a wrapper around a tablegen TypeDef record.
282 class TypeDef : public AttrOrTypeDef {
283 public:
285 
286  static bool classof(const AttrOrTypeDef *def);
287 
288  /// Get the unique type name "dialect.typename".
289  StringRef getTypeName() const;
290 };
291 
292 } // namespace tblgen
293 } // namespace mlir
294 
295 #endif // MLIR_TABLEGEN_ATTRORTYPEDEF_H
This class represents a wrapper around a tablegen AttrDef record.
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.
Definition: AttrOrTypeDef.h:37
bool hasInferredContextParameter() const
Returns true if this builder is able to infer the MLIRContext parameter.
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...
const llvm::Record * getDef() const
Return the underlying def.
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)
const llvm::Record * def
StringRef getStorageNamespace() const
Returns the C++ namespace for this def's storage class.
ArrayRef< AttrOrTypeBuilder > getBuilders() const
Returns the builders of this def.
bool genVerifyDecl() const
Return true if we need to generate the verify declaration and getChecked method.
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.
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.
StringRef getSummary() const
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.
A wrapper class for tblgen AttrOrTypeParameter, arrays of which belong to AttrOrTypeDefs to parameter...
Definition: AttrOrTypeDef.h:54
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)
Definition: AttrOrTypeDef.h:56
static bool classof(const AttrOrTypeParameter *param)
Wrapper class with helper methods for accessing Builders defined in TableGen.
Definition: Builder.h:33
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.
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.