11 #include "llvm/ADT/FunctionExtras.h"
12 #include "llvm/ADT/SmallPtrSet.h"
13 #include "llvm/ADT/StringExtras.h"
14 #include "llvm/Support/ErrorHandling.h"
15 #include "llvm/TableGen/Error.h"
16 #include "llvm/TableGen/Record.h"
26 std::optional<StringRef> type =
def->getValueAsOptionalString(
"returnType");
27 return type && !type->empty() ? type : std::nullopt;
31 return def->getValueAsBit(
"hasInferredContextParam");
41 dyn_cast_or_null<llvm::ListInit>(
def->getValueInit(
"builders"));
42 if (builderList && !builderList->empty()) {
43 for (llvm::Init *init : builderList->getValues()) {
51 PrintFatalError(
def->getLoc(),
"builder parameters must have a name");
58 if (
auto *traitList =
def->getValueAsListInit(
"traits")) {
60 traits.reserve(traitSet.size());
61 llvm::unique_function<void(llvm::ListInit *)> processTraitList =
62 [&](llvm::ListInit *traitList) {
63 for (
auto *traitInit : *traitList) {
64 if (!traitSet.insert(traitInit).second)
68 auto *traitDef = cast<llvm::DefInit>(traitInit)->getDef();
69 if (traitDef->isSubClassOf(
"Interface")) {
70 if (
auto *bases = traitDef->getValueAsListInit(
"baseInterfaces"))
71 processTraitList(bases);
77 processTraitList(traitList);
81 if (
auto *parametersDag =
def->getValueAsDag(
"parameters")) {
82 for (
unsigned i = 0, e = parametersDag->getNumArgs(); i < e; ++i)
90 if (hasCppFormat && hasDeclarativeFormat) {
91 PrintFatalError(
getLoc(),
"cannot specify both 'assemblyFormat' "
92 "and 'hasCustomAssemblyFormat'");
94 if (!
parameters.empty() && !hasCppFormat && !hasDeclarativeFormat) {
96 "must specify either 'assemblyFormat' or "
97 "'hasCustomAssemblyFormat' when 'mnemonic' is set");
99 }
else if (hasCppFormat || hasDeclarativeFormat) {
101 "'assemblyFormat' or 'hasCustomAssemblyFormat' can only be "
102 "used when 'mnemonic' is set");
107 "'assemblyFormat' requires 'genAccessors' to be true");
114 auto *dialect = dyn_cast<llvm::DefInit>(
def->getValue(
"dialect")->getValue());
115 return Dialect(dialect ? dialect->getDef() :
nullptr);
121 return def->getValueAsString(
"cppClassName");
125 return def->getValueAsString(
"cppBaseClassName");
129 const llvm::RecordVal *desc =
def->getValue(
"description");
130 return desc && isa<llvm::StringInit>(desc->getValue());
134 return def->getValueAsString(
"description");
138 const llvm::RecordVal *summary =
def->getValue(
"summary");
139 return summary && isa<llvm::StringInit>(summary->getValue());
143 return def->getValueAsString(
"summary");
147 return def->getValueAsString(
"storageClass");
151 return def->getValueAsString(
"storageNamespace");
155 return def->getValueAsBit(
"genStorageClass");
159 return def->getValueAsBit(
"hasStorageCustomConstructor");
163 auto *parametersDag =
def->getValueAsDag(
"parameters");
164 return parametersDag ? parametersDag->getNumArgs() : 0;
168 return def->getValueAsOptionalString(
"mnemonic");
172 return def->getValueAsBit(
"hasCustomAssemblyFormat");
176 return def->getValueAsOptionalString(
"assemblyFormat");
180 return def->getValueAsBit(
"genAccessors");
184 return def->getValueAsBit(
"genVerifyDecl");
188 auto value =
def->getValueAsString(
"extraClassDeclaration");
189 return value.empty() ? std::optional<StringRef>() : value;
193 auto value =
def->getValueAsString(
"extraClassDefinition");
194 return value.empty() ? std::optional<StringRef>() : value;
200 return def->getValueAsBit(
"skipDefaultBuilders");
216 return def->getValueAsOptionalString(
"typeBuilder");
220 return def->getDef()->isSubClassOf(
"AttrDef");
224 return def->getValueAsString(
"attrName");
232 return def->getDef()->isSubClassOf(
"TypeDef");
236 return def->getValueAsString(
"typeName");
243 template <
typename InitT>
244 auto AttrOrTypeParameter::getDefValue(StringRef name)
const {
245 std::optional<decltype(std::declval<InitT>().getValue())> result;
246 if (
auto *param = dyn_cast<llvm::DefInit>(
getDef()))
247 if (
auto *init = param->getDef()->getValue(name))
248 if (
auto *value = dyn_cast_or_null<InitT>(init->getValue()))
249 result = value->getValue();
254 return !def->getArgName(index);
258 return def->getArgName(index)->getValue();
263 llvm::convertToCamelFromSnakeCase(
getName(),
true);
267 return getDefValue<llvm::StringInit>(
"allocator");
271 return getDefValue<llvm::StringInit>(
"comparator").value_or(
"$_lhs == $_rhs");
275 if (
auto *stringType = dyn_cast<llvm::StringInit>(
getDef()))
276 return stringType->getValue();
277 auto cppType = getDefValue<llvm::StringInit>(
"cppType");
280 if (
auto *init = dyn_cast<llvm::DefInit>(
getDef()))
281 llvm::PrintFatalError(
282 init->getDef()->getLoc(),
283 Twine(
"Missing `cppType` field in Attribute/Type parameter: ") +
284 init->getAsString());
285 llvm::report_fatal_error(
286 Twine(
"Missing `cppType` field in Attribute/Type parameter: ") +
292 return getDefValue<llvm::StringInit>(
"cppAccessorType")
297 return getDefValue<llvm::StringInit>(
"cppStorageType").value_or(
getCppType());
301 return getDefValue<llvm::StringInit>(
"convertFromStorage").value_or(
"$_self");
305 return getDefValue<llvm::StringInit>(
"parser");
309 return getDefValue<llvm::StringInit>(
"printer");
313 return getDefValue<llvm::StringInit>(
"summary");
317 if (
auto *stringType = dyn_cast<llvm::StringInit>(
getDef()))
318 return stringType->getValue();
319 return getDefValue<llvm::StringInit>(
"syntax").value_or(
getCppType());
327 std::optional<StringRef> result =
328 getDefValue<llvm::StringInit>(
"defaultValue");
329 return result && !result->empty() ? result : std::nullopt;
339 llvm::Init *paramDef = param->
getDef();
340 if (
auto *paramDefInit = dyn_cast<llvm::DefInit>(paramDef))
341 return paramDefInit->getDef()->isSubClassOf(
"AttributeSelfTypeParameter");
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.
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)
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.
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
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.
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...
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.
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.
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.
static bool classof(const AttrOrTypeParameter *param)
This class represents a single parameter to a builder method.
const llvm::Record * def
The TableGen definition of this builder.
ArrayRef< Parameter > getParameters() const
Return a list of parameters used in this build method.
static Trait create(const llvm::Init *init)
StringRef getTypeName() const
Get the unique type name "dialect.typename".
static bool classof(const AttrOrTypeDef *def)
Include the generated interface declarations.