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