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