1//===- Attribute.cpp - Attribute wrapper class ----------------------------===//
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// Attribute wrapper to simplify using TableGen Record defining a MLIR
10// Attribute.
11//
12//===----------------------------------------------------------------------===//
13
14#include "mlir/TableGen/Operator.h"
15#include "llvm/TableGen/Record.h"
16
17using namespace mlir;
18using namespace mlir::tblgen;
19
20using llvm::DefInit;
21using llvm::Init;
22using llvm::Record;
23using llvm::StringInit;
24
25// Returns the initializer's value as string if the given TableGen initializer
26// is a code or string initializer. Returns the empty StringRef otherwise.
27static StringRef getValueAsString(const Init *init) {
28 if (const auto *str = dyn_cast<StringInit>(Val: init))
29 return str->getValue().trim();
30 return {};
31}
32
33bool AttrConstraint::isSubClassOf(StringRef className) const {
34 return def->isSubClassOf(Name: className);
35}
36
37Attribute::Attribute(const Record *record) : AttrConstraint(record) {
38 assert(record->isSubClassOf("Attr") &&
39 "must be subclass of TableGen 'Attr' class");
40}
41
42Attribute::Attribute(const DefInit *init) : Attribute(init->getDef()) {}
43
44bool Attribute::isDerivedAttr() const { return isSubClassOf(className: "DerivedAttr"); }
45
46bool Attribute::isTypeAttr() const { return isSubClassOf(className: "TypeAttrBase"); }
47
48bool Attribute::isSymbolRefAttr() const {
49 StringRef defName = def->getName();
50 if (defName == "SymbolRefAttr" || defName == "FlatSymbolRefAttr")
51 return true;
52 return isSubClassOf(className: "SymbolRefAttr") || isSubClassOf(className: "FlatSymbolRefAttr");
53}
54
55bool Attribute::isEnumAttr() const {
56 return isSubClassOf(className: "EnumAttrInfo") || isSubClassOf(className: "EnumAttr");
57}
58
59StringRef Attribute::getStorageType() const {
60 const auto *init = def->getValueInit(FieldName: "storageType");
61 auto type = getValueAsString(init);
62 if (type.empty())
63 return "::mlir::Attribute";
64 return type;
65}
66
67StringRef Attribute::getReturnType() const {
68 const auto *init = def->getValueInit(FieldName: "returnType");
69 return getValueAsString(init);
70}
71
72// Return the type constraint corresponding to the type of this attribute, or
73// std::nullopt if this is not a TypedAttr.
74std::optional<Type> Attribute::getValueType() const {
75 if (const auto *defInit = dyn_cast<DefInit>(Val: def->getValueInit(FieldName: "valueType")))
76 return Type(defInit->getDef());
77 return std::nullopt;
78}
79
80StringRef Attribute::getConvertFromStorageCall() const {
81 const auto *init = def->getValueInit(FieldName: "convertFromStorage");
82 return getValueAsString(init);
83}
84
85bool Attribute::isConstBuildable() const {
86 const auto *init = def->getValueInit(FieldName: "constBuilderCall");
87 return !getValueAsString(init).empty();
88}
89
90StringRef Attribute::getConstBuilderTemplate() const {
91 const auto *init = def->getValueInit(FieldName: "constBuilderCall");
92 return getValueAsString(init);
93}
94
95Attribute Attribute::getBaseAttr() const {
96 if (const auto *defInit = dyn_cast<DefInit>(Val: def->getValueInit(FieldName: "baseAttr"))) {
97 return Attribute(defInit).getBaseAttr();
98 }
99 return *this;
100}
101
102bool Attribute::hasDefaultValue() const {
103 const auto *init = def->getValueInit(FieldName: "defaultValue");
104 return !getValueAsString(init).empty();
105}
106
107StringRef Attribute::getDefaultValue() const {
108 const auto *init = def->getValueInit(FieldName: "defaultValue");
109 return getValueAsString(init);
110}
111
112bool Attribute::isOptional() const { return def->getValueAsBit(FieldName: "isOptional"); }
113
114StringRef Attribute::getAttrDefName() const {
115 if (def->isAnonymous()) {
116 return getBaseAttr().def->getName();
117 }
118 return def->getName();
119}
120
121StringRef Attribute::getDerivedCodeBody() const {
122 assert(isDerivedAttr() && "only derived attribute has 'body' field");
123 return def->getValueAsString(FieldName: "body");
124}
125
126Dialect Attribute::getDialect() const {
127 const llvm::RecordVal *record = def->getValue(Name: "dialect");
128 if (record) {
129 if (const DefInit *init = dyn_cast<DefInit>(Val: record->getValue()))
130 return Dialect(init->getDef());
131 }
132 return Dialect(nullptr);
133}
134
135const Record &Attribute::getDef() const { return *def; }
136
137ConstantAttr::ConstantAttr(const DefInit *init) : def(init->getDef()) {
138 assert(def->isSubClassOf("ConstantAttr") &&
139 "must be subclass of TableGen 'ConstantAttr' class");
140}
141
142Attribute ConstantAttr::getAttribute() const {
143 return Attribute(def->getValueAsDef(FieldName: "attr"));
144}
145
146StringRef ConstantAttr::getConstantValue() const {
147 return def->getValueAsString(FieldName: "value");
148}
149
150const char * ::mlir::tblgen::inferTypeOpInterface = "InferTypeOpInterface";
151