2019-05-14 15:03:48 -07:00
|
|
|
//===- Attribute.cpp - Attribute wrapper class ----------------------------===//
|
2019-01-09 13:50:20 -08:00
|
|
|
//
|
2020-01-26 03:58:30 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-23 09:35:36 -08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-01-09 13:50:20 -08:00
|
|
|
//
|
2019-12-23 09:35:36 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-01-09 13:50:20 -08:00
|
|
|
//
|
|
|
|
|
// Attribute wrapper to simplify using TableGen Record defining a MLIR
|
|
|
|
|
// Attribute.
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2019-04-12 06:05:49 -07:00
|
|
|
#include "mlir/TableGen/Format.h"
|
2019-01-09 13:50:20 -08:00
|
|
|
#include "mlir/TableGen/Operator.h"
|
|
|
|
|
#include "llvm/TableGen/Record.h"
|
|
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
|
|
2019-04-12 06:05:49 -07:00
|
|
|
using llvm::CodeInit;
|
|
|
|
|
using llvm::DefInit;
|
|
|
|
|
using llvm::Init;
|
|
|
|
|
using llvm::Record;
|
|
|
|
|
using llvm::StringInit;
|
|
|
|
|
|
2019-01-09 13:50:20 -08:00
|
|
|
// Returns the initializer's value as string if the given TableGen initializer
|
|
|
|
|
// is a code or string initializer. Returns the empty StringRef otherwise.
|
2019-04-12 06:05:49 -07:00
|
|
|
static StringRef getValueAsString(const Init *init) {
|
|
|
|
|
if (const auto *code = dyn_cast<CodeInit>(init))
|
2019-01-09 13:50:20 -08:00
|
|
|
return code->getValue().trim();
|
2019-04-12 06:05:49 -07:00
|
|
|
else if (const auto *str = dyn_cast<StringInit>(init))
|
2019-01-09 13:50:20 -08:00
|
|
|
return str->getValue().trim();
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-12 06:05:49 -07:00
|
|
|
tblgen::AttrConstraint::AttrConstraint(const Record *record)
|
2019-03-25 06:09:26 -07:00
|
|
|
: Constraint(Constraint::CK_Attr, record) {
|
2019-11-12 11:57:40 -08:00
|
|
|
assert(isSubClassOf("AttrConstraint") &&
|
2019-01-28 07:13:40 -08:00
|
|
|
"must be subclass of TableGen 'AttrConstraint' class");
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-12 11:57:40 -08:00
|
|
|
bool tblgen::AttrConstraint::isSubClassOf(StringRef className) const {
|
|
|
|
|
return def->isSubClassOf(className);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-12 06:05:49 -07:00
|
|
|
tblgen::Attribute::Attribute(const Record *record) : AttrConstraint(record) {
|
2019-01-28 07:13:40 -08:00
|
|
|
assert(record->isSubClassOf("Attr") &&
|
2019-01-09 13:50:20 -08:00
|
|
|
"must be subclass of TableGen 'Attr' class");
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-12 06:05:49 -07:00
|
|
|
tblgen::Attribute::Attribute(const DefInit *init) : Attribute(init->getDef()) {}
|
2019-01-09 13:50:20 -08:00
|
|
|
|
|
|
|
|
bool tblgen::Attribute::isDerivedAttr() const {
|
2019-11-12 11:57:40 -08:00
|
|
|
return isSubClassOf("DerivedAttr");
|
2019-01-09 13:50:20 -08:00
|
|
|
}
|
|
|
|
|
|
2019-03-26 15:31:15 -07:00
|
|
|
bool tblgen::Attribute::isTypeAttr() const {
|
2019-11-12 11:57:40 -08:00
|
|
|
return isSubClassOf("TypeAttrBase");
|
2019-03-26 15:31:15 -07:00
|
|
|
}
|
|
|
|
|
|
2019-07-17 18:41:28 -07:00
|
|
|
bool tblgen::Attribute::isEnumAttr() const {
|
2019-11-12 11:57:40 -08:00
|
|
|
return isSubClassOf("EnumAttrInfo");
|
2019-07-17 18:41:28 -07:00
|
|
|
}
|
|
|
|
|
|
2019-01-09 13:50:20 -08:00
|
|
|
StringRef tblgen::Attribute::getStorageType() const {
|
2019-01-16 10:23:21 -08:00
|
|
|
const auto *init = def->getValueInit("storageType");
|
2019-01-09 13:50:20 -08:00
|
|
|
auto type = getValueAsString(init);
|
|
|
|
|
if (type.empty())
|
|
|
|
|
return "Attribute";
|
|
|
|
|
return type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringRef tblgen::Attribute::getReturnType() const {
|
2019-01-16 10:23:21 -08:00
|
|
|
const auto *init = def->getValueInit("returnType");
|
2019-01-09 13:50:20 -08:00
|
|
|
return getValueAsString(init);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-08 10:01:17 -08:00
|
|
|
// Return the type constraint corresponding to the type of this attribute, or
|
|
|
|
|
// None if this is not a TypedAttr.
|
|
|
|
|
llvm::Optional<tblgen::Type> tblgen::Attribute::getValueType() const {
|
|
|
|
|
if (auto *defInit = dyn_cast<llvm::DefInit>(def->getValueInit("valueType")))
|
|
|
|
|
return tblgen::Type(defInit->getDef());
|
|
|
|
|
return llvm::None;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-09 13:50:20 -08:00
|
|
|
StringRef tblgen::Attribute::getConvertFromStorageCall() const {
|
2019-01-16 10:23:21 -08:00
|
|
|
const auto *init = def->getValueInit("convertFromStorage");
|
2019-01-09 13:50:20 -08:00
|
|
|
return getValueAsString(init);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-16 09:23:14 -08:00
|
|
|
bool tblgen::Attribute::isConstBuildable() const {
|
2019-01-16 10:23:21 -08:00
|
|
|
const auto *init = def->getValueInit("constBuilderCall");
|
2019-01-16 09:23:14 -08:00
|
|
|
return !getValueAsString(init).empty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringRef tblgen::Attribute::getConstBuilderTemplate() const {
|
2019-01-16 10:23:21 -08:00
|
|
|
const auto *init = def->getValueInit("constBuilderCall");
|
2019-01-16 09:23:14 -08:00
|
|
|
return getValueAsString(init);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-16 04:33:54 -07:00
|
|
|
tblgen::Attribute tblgen::Attribute::getBaseAttr() const {
|
|
|
|
|
if (const auto *defInit =
|
|
|
|
|
llvm::dyn_cast<llvm::DefInit>(def->getValueInit("baseAttr"))) {
|
|
|
|
|
return Attribute(defInit).getBaseAttr();
|
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-02 09:33:24 -08:00
|
|
|
bool tblgen::Attribute::hasDefaultValue() const {
|
2019-01-22 10:26:09 -08:00
|
|
|
const auto *init = def->getValueInit("defaultValue");
|
|
|
|
|
return !getValueAsString(init).empty();
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-02 09:33:24 -08:00
|
|
|
StringRef tblgen::Attribute::getDefaultValue() const {
|
2019-04-12 06:05:49 -07:00
|
|
|
const auto *init = def->getValueInit("defaultValue");
|
|
|
|
|
return getValueAsString(init);
|
2019-02-22 15:11:00 -08:00
|
|
|
}
|
|
|
|
|
|
2019-04-12 06:05:49 -07:00
|
|
|
bool tblgen::Attribute::isOptional() const {
|
|
|
|
|
return def->getValueAsBit("isOptional");
|
2019-01-22 10:26:09 -08:00
|
|
|
}
|
|
|
|
|
|
2019-05-10 16:11:02 -07:00
|
|
|
StringRef tblgen::Attribute::getAttrDefName() const {
|
2019-07-16 04:33:54 -07:00
|
|
|
if (def->isAnonymous()) {
|
|
|
|
|
return getBaseAttr().def->getName();
|
|
|
|
|
}
|
2019-01-16 10:23:21 -08:00
|
|
|
return def->getName();
|
2019-01-16 09:23:14 -08:00
|
|
|
}
|
|
|
|
|
|
2019-01-09 13:50:20 -08:00
|
|
|
StringRef tblgen::Attribute::getDerivedCodeBody() const {
|
|
|
|
|
assert(isDerivedAttr() && "only derived attribute has 'body' field");
|
2019-01-16 10:23:21 -08:00
|
|
|
return def->getValueAsString("body");
|
2019-01-09 13:50:20 -08:00
|
|
|
}
|
2019-02-01 15:40:22 -08:00
|
|
|
|
2020-03-14 20:33:53 -07:00
|
|
|
tblgen::Dialect tblgen::Attribute::getDialect() const {
|
|
|
|
|
return Dialect(def->getValueAsDef("dialect"));
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-12 06:05:49 -07:00
|
|
|
tblgen::ConstantAttr::ConstantAttr(const DefInit *init) : def(init->getDef()) {
|
2019-02-01 15:40:22 -08:00
|
|
|
assert(def->isSubClassOf("ConstantAttr") &&
|
|
|
|
|
"must be subclass of TableGen 'ConstantAttr' class");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tblgen::Attribute tblgen::ConstantAttr::getAttribute() const {
|
|
|
|
|
return Attribute(def->getValueAsDef("attr"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringRef tblgen::ConstantAttr::getConstantValue() const {
|
|
|
|
|
return def->getValueAsString("value");
|
|
|
|
|
}
|
2019-04-01 08:58:53 -07:00
|
|
|
|
2019-12-26 12:15:26 -05:00
|
|
|
tblgen::EnumAttrCase::EnumAttrCase(const llvm::Record *record)
|
|
|
|
|
: Attribute(record) {
|
2019-11-12 11:57:40 -08:00
|
|
|
assert(isSubClassOf("EnumAttrCaseInfo") &&
|
2019-07-01 05:26:14 -07:00
|
|
|
"must be subclass of TableGen 'EnumAttrInfo' class");
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-26 12:15:26 -05:00
|
|
|
tblgen::EnumAttrCase::EnumAttrCase(const llvm::DefInit *init)
|
|
|
|
|
: EnumAttrCase(init->getDef()) {}
|
|
|
|
|
|
2019-07-01 05:26:14 -07:00
|
|
|
bool tblgen::EnumAttrCase::isStrCase() const {
|
2019-11-12 11:57:40 -08:00
|
|
|
return isSubClassOf("StrEnumAttrCase");
|
2019-04-01 08:58:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringRef tblgen::EnumAttrCase::getSymbol() const {
|
|
|
|
|
return def->getValueAsString("symbol");
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-24 17:51:10 +01:00
|
|
|
StringRef tblgen::EnumAttrCase::getStr() const {
|
|
|
|
|
return def->getValueAsString("str");
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-08 08:39:07 -07:00
|
|
|
int64_t tblgen::EnumAttrCase::getValue() const {
|
|
|
|
|
return def->getValueAsInt("value");
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-02 13:19:19 -05:00
|
|
|
const llvm::Record &tblgen::EnumAttrCase::getDef() const { return *def; }
|
|
|
|
|
|
2019-04-01 08:58:53 -07:00
|
|
|
tblgen::EnumAttr::EnumAttr(const llvm::Record *record) : Attribute(record) {
|
2019-11-12 11:57:40 -08:00
|
|
|
assert(isSubClassOf("EnumAttrInfo") &&
|
2019-04-01 08:58:53 -07:00
|
|
|
"must be subclass of TableGen 'EnumAttr' class");
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-08 08:39:07 -07:00
|
|
|
tblgen::EnumAttr::EnumAttr(const llvm::Record &record) : Attribute(&record) {}
|
|
|
|
|
|
2019-04-01 08:58:53 -07:00
|
|
|
tblgen::EnumAttr::EnumAttr(const llvm::DefInit *init)
|
|
|
|
|
: EnumAttr(init->getDef()) {}
|
|
|
|
|
|
2020-01-02 13:19:19 -05:00
|
|
|
bool tblgen::EnumAttr::classof(const Attribute *attr) {
|
|
|
|
|
return attr->isSubClassOf("EnumAttrInfo");
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-12 11:57:40 -08:00
|
|
|
bool tblgen::EnumAttr::isBitEnum() const { return isSubClassOf("BitEnumAttr"); }
|
2019-09-16 09:22:43 -07:00
|
|
|
|
2019-04-01 08:58:53 -07:00
|
|
|
StringRef tblgen::EnumAttr::getEnumClassName() const {
|
|
|
|
|
return def->getValueAsString("className");
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-08 08:39:07 -07:00
|
|
|
StringRef tblgen::EnumAttr::getCppNamespace() const {
|
|
|
|
|
return def->getValueAsString("cppNamespace");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringRef tblgen::EnumAttr::getUnderlyingType() const {
|
|
|
|
|
return def->getValueAsString("underlyingType");
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-21 14:51:58 -07:00
|
|
|
StringRef tblgen::EnumAttr::getUnderlyingToSymbolFnName() const {
|
|
|
|
|
return def->getValueAsString("underlyingToSymbolFnName");
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-08 08:39:07 -07:00
|
|
|
StringRef tblgen::EnumAttr::getStringToSymbolFnName() const {
|
|
|
|
|
return def->getValueAsString("stringToSymbolFnName");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringRef tblgen::EnumAttr::getSymbolToStringFnName() const {
|
|
|
|
|
return def->getValueAsString("symbolToStringFnName");
|
|
|
|
|
}
|
2019-09-16 09:22:43 -07:00
|
|
|
|
|
|
|
|
StringRef tblgen::EnumAttr::getSymbolToStringFnRetType() const {
|
|
|
|
|
return def->getValueAsString("symbolToStringFnRetType");
|
|
|
|
|
}
|
2019-06-08 08:39:07 -07:00
|
|
|
|
2019-06-19 16:09:57 -07:00
|
|
|
StringRef tblgen::EnumAttr::getMaxEnumValFnName() const {
|
|
|
|
|
return def->getValueAsString("maxEnumValFnName");
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-01 08:58:53 -07:00
|
|
|
std::vector<tblgen::EnumAttrCase> tblgen::EnumAttr::getAllCases() const {
|
|
|
|
|
const auto *inits = def->getValueAsListInit("enumerants");
|
|
|
|
|
|
|
|
|
|
std::vector<tblgen::EnumAttrCase> cases;
|
|
|
|
|
cases.reserve(inits->size());
|
|
|
|
|
|
|
|
|
|
for (const llvm::Init *init : *inits) {
|
|
|
|
|
cases.push_back(tblgen::EnumAttrCase(cast<llvm::DefInit>(init)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cases;
|
|
|
|
|
}
|
2019-08-30 12:51:31 -07:00
|
|
|
|
|
|
|
|
tblgen::StructFieldAttr::StructFieldAttr(const llvm::Record *record)
|
|
|
|
|
: def(record) {
|
|
|
|
|
assert(def->isSubClassOf("StructFieldAttr") &&
|
|
|
|
|
"must be subclass of TableGen 'StructFieldAttr' class");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tblgen::StructFieldAttr::StructFieldAttr(const llvm::Record &record)
|
|
|
|
|
: StructFieldAttr(&record) {}
|
|
|
|
|
|
|
|
|
|
tblgen::StructFieldAttr::StructFieldAttr(const llvm::DefInit *init)
|
|
|
|
|
: StructFieldAttr(init->getDef()) {}
|
|
|
|
|
|
|
|
|
|
StringRef tblgen::StructFieldAttr::getName() const {
|
|
|
|
|
return def->getValueAsString("name");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tblgen::Attribute tblgen::StructFieldAttr::getType() const {
|
|
|
|
|
auto init = def->getValueInit("type");
|
|
|
|
|
return tblgen::Attribute(cast<llvm::DefInit>(init));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tblgen::StructAttr::StructAttr(const llvm::Record *record) : Attribute(record) {
|
2019-11-12 11:57:40 -08:00
|
|
|
assert(isSubClassOf("StructAttr") &&
|
2019-08-30 12:51:31 -07:00
|
|
|
"must be subclass of TableGen 'StructAttr' class");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tblgen::StructAttr::StructAttr(const llvm::DefInit *init)
|
|
|
|
|
: StructAttr(init->getDef()) {}
|
|
|
|
|
|
|
|
|
|
StringRef tblgen::StructAttr::getStructClassName() const {
|
|
|
|
|
return def->getValueAsString("className");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringRef tblgen::StructAttr::getCppNamespace() const {
|
|
|
|
|
Dialect dialect(def->getValueAsDef("structDialect"));
|
|
|
|
|
return dialect.getCppNamespace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<mlir::tblgen::StructFieldAttr>
|
|
|
|
|
tblgen::StructAttr::getAllFields() const {
|
|
|
|
|
std::vector<mlir::tblgen::StructFieldAttr> attributes;
|
|
|
|
|
|
|
|
|
|
const auto *inits = def->getValueAsListInit("fields");
|
|
|
|
|
attributes.reserve(inits->size());
|
|
|
|
|
|
|
|
|
|
for (const llvm::Init *init : *inits) {
|
|
|
|
|
attributes.emplace_back(cast<llvm::DefInit>(init));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return attributes;
|
|
|
|
|
}
|