#include "mlir/TableGen/Type.h"
#include "mlir/TableGen/Dialect.h"
#include "llvm/ADT/Twine.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/TableGen/Record.h"
using namespace mlir;
using namespace mlir::tblgen;
TypeConstraint::TypeConstraint(const llvm::DefInit *init)
: TypeConstraint(init->getDef()) {}
bool TypeConstraint::isOptional() const {
return def->isSubClassOf("Optional");
}
bool TypeConstraint::isVariadic() const {
return def->isSubClassOf("Variadic");
}
bool TypeConstraint::isVariadicOfVariadic() const {
return def->isSubClassOf("VariadicOfVariadic");
}
StringRef TypeConstraint::getVariadicOfVariadicSegmentSizeAttr() const {
assert(isVariadicOfVariadic());
return def->getValueAsString("segmentAttrName");
}
std::optional<StringRef> TypeConstraint::getBuilderCall() const {
const llvm::Record *baseType = def;
if (isVariableLength())
baseType = baseType->getValueAsDef("baseType");
const llvm::RecordVal *builderCall = baseType->getValue("builderCall");
if (!builderCall || !builderCall->getValue())
return std::nullopt;
return TypeSwitch<llvm::Init *, std::optional<StringRef>>(
builderCall->getValue())
.Case<llvm::StringInit>([&](auto *init) {
StringRef value = init->getValue();
return value.empty() ? std::optional<StringRef>() : value;
})
.Default([](auto *) { return std::nullopt; });
}
std::string TypeConstraint::getCPPClassName() const {
StringRef className = def->getValueAsString("cppClassName");
if (className.contains("::"))
return className.str();
if (const llvm::RecordVal *value = def->getValue("dialect")) {
Dialect dialect(cast<const llvm::DefInit>(value->getValue())->getDef());
return (dialect.getCppNamespace() + "::" + className).str();
}
return className.str();
}
Type::Type(const llvm::Record *record) : TypeConstraint(record) {}
Dialect Type::getDialect() const {
return Dialect(def->getValueAsDef("dialect"));
}