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"
24 using llvm::RecordVal;
25 using llvm::StringInit;
32 std::optional<StringRef> type =
def->getValueAsOptionalString(
"returnType");
33 return type && !type->empty() ? type : std::nullopt;
37 return def->getValueAsBit(
"hasInferredContextParam");
46 const auto *builderList =
47 dyn_cast_or_null<ListInit>(
def->getValueInit(
"builders"));
48 if (builderList && !builderList->empty()) {
49 for (
const Init *init : builderList->getValues()) {
56 PrintFatalError(
def->getLoc(),
"builder parameters must have a name");
63 if (
auto *traitList =
def->getValueAsListInit(
"traits")) {
65 traits.reserve(traitSet.size());
66 llvm::unique_function<void(
const ListInit *)> processTraitList =
67 [&](
const ListInit *traitList) {
68 for (
auto *traitInit : *traitList) {
69 if (!traitSet.insert(traitInit).second)
73 auto *traitDef = cast<DefInit>(traitInit)->getDef();
74 if (traitDef->isSubClassOf(
"Interface")) {
75 if (
auto *bases = traitDef->getValueAsListInit(
"baseInterfaces"))
76 processTraitList(bases);
82 processTraitList(traitList);
86 if (
auto *parametersDag =
def->getValueAsDag(
"parameters")) {
87 for (
unsigned i = 0, e = parametersDag->getNumArgs(); i < e; ++i)
95 if (hasCppFormat && hasDeclarativeFormat) {
96 PrintFatalError(
getLoc(),
"cannot specify both 'assemblyFormat' "
97 "and 'hasCustomAssemblyFormat'");
99 if (!
parameters.empty() && !hasCppFormat && !hasDeclarativeFormat) {
101 "must specify either 'assemblyFormat' or "
102 "'hasCustomAssemblyFormat' when 'mnemonic' is set");
104 }
else if (hasCppFormat || hasDeclarativeFormat) {
106 "'assemblyFormat' or 'hasCustomAssemblyFormat' can only be "
107 "used when 'mnemonic' is set");
112 "'assemblyFormat' requires 'genAccessors' to be true");
119 const auto *dialect = dyn_cast<DefInit>(
def->getValue(
"dialect")->getValue());
120 return Dialect(dialect ? dialect->getDef() :
nullptr);
126 return def->getValueAsString(
"cppClassName");
130 return def->getValueAsString(
"cppBaseClassName");
134 const RecordVal *desc =
def->getValue(
"description");
135 return desc && isa<StringInit>(desc->getValue());
139 return def->getValueAsString(
"description");
143 const RecordVal *summary =
def->getValue(
"summary");
144 return summary && isa<StringInit>(summary->getValue());
148 return def->getValueAsString(
"summary");
152 return def->getValueAsString(
"storageClass");
156 return def->getValueAsString(
"storageNamespace");
160 return def->getValueAsBit(
"genStorageClass");
164 return def->getValueAsBit(
"hasStorageCustomConstructor");
168 auto *parametersDag =
def->getValueAsDag(
"parameters");
169 return parametersDag ? parametersDag->getNumArgs() : 0;
173 return def->getValueAsOptionalString(
"mnemonic");
177 return def->getValueAsBit(
"hasCustomAssemblyFormat");
181 return def->getValueAsOptionalString(
"assemblyFormat");
185 return def->getValueAsBit(
"genAccessors");
189 return def->getValueAsBit(
"genVerifyDecl");
199 auto value =
def->getValueAsString(
"extraClassDeclaration");
200 return value.empty() ? std::optional<StringRef>() : value;
204 auto value =
def->getValueAsString(
"extraClassDefinition");
205 return value.empty() ? std::optional<StringRef>() : value;
211 return def->getValueAsBit(
"skipDefaultBuilders");
227 return def->getValueAsOptionalString(
"typeBuilder");
231 return def->getDef()->isSubClassOf(
"AttrDef");
235 return def->getValueAsString(
"attrName");
243 return def->getDef()->isSubClassOf(
"TypeDef");
247 return def->getValueAsString(
"typeName");
254 template <
typename InitT>
255 auto AttrOrTypeParameter::getDefValue(StringRef name)
const {
256 std::optional<decltype(std::declval<InitT>().getValue())> result;
257 if (
const auto *param = dyn_cast<DefInit>(
getDef()))
258 if (
const auto *init = param->getDef()->getValue(name))
259 if (
const auto *value = dyn_cast_or_null<InitT>(init->getValue()))
260 result = value->getValue();
265 return !def->getArgName(index);
269 return def->getArgName(index)->getValue();
274 llvm::convertToCamelFromSnakeCase(
getName(),
true);
278 return getDefValue<StringInit>(
"allocator");
282 return getDefValue<StringInit>(
"comparator").value_or(
"$_lhs == $_rhs");
286 if (
auto *stringType = dyn_cast<StringInit>(
getDef()))
287 return stringType->getValue();
288 auto cppType = getDefValue<StringInit>(
"cppType");
291 if (
const auto *init = dyn_cast<DefInit>(
getDef()))
292 llvm::PrintFatalError(
293 init->getDef()->getLoc(),
294 Twine(
"Missing `cppType` field in Attribute/Type parameter: ") +
295 init->getAsString());
296 llvm::report_fatal_error(
297 Twine(
"Missing `cppType` field in Attribute/Type parameter: ") +
303 return getDefValue<StringInit>(
"cppAccessorType").value_or(
getCppType());
307 return getDefValue<StringInit>(
"cppStorageType").value_or(
getCppType());
311 return getDefValue<StringInit>(
"convertFromStorage").value_or(
"$_self");
315 return getDefValue<StringInit>(
"parser");
319 return getDefValue<StringInit>(
"printer");
323 return getDefValue<StringInit>(
"summary");
327 if (
auto *stringType = dyn_cast<StringInit>(
getDef()))
328 return stringType->getValue();
329 return getDefValue<StringInit>(
"syntax").value_or(
getCppType());
337 std::optional<StringRef> result = getDefValue<StringInit>(
"defaultValue");
338 return result && !result->empty() ? result : std::nullopt;
344 if (
const auto *param = dyn_cast<DefInit>(
getDef()))
345 if (param->getDef()->isSubClassOf(
"Constraint"))
355 const Init *paramDef = param->
getDef();
356 if (
const auto *paramDefInit = dyn_cast<DefInit>(paramDef))
357 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.
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.
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.
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.
const llvm::Init * getDef() const
Return the underlying def of this parameter.
std::optional< StringRef > getPrinter() const
Get an optional C++ parameter printer.
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.