1 //===- AttrOrTypeDef.cpp - AttrOrTypeDef wrapper classes ------------------===//
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 #include "mlir/TableGen/AttrOrTypeDef.h"
10 #include "mlir/TableGen/Dialect.h"
11 #include "llvm/ADT/SmallPtrSet.h"
12 #include "llvm/ADT/StringExtras.h"
13 #include "llvm/TableGen/Error.h"
14 #include "llvm/TableGen/Record.h"
15 
16 using namespace mlir;
17 using namespace mlir::tblgen;
18 
19 //===----------------------------------------------------------------------===//
20 // AttrOrTypeBuilder
21 //===----------------------------------------------------------------------===//
22 
23 /// Returns true if this builder is able to infer the MLIRContext parameter.
24 bool AttrOrTypeBuilder::hasInferredContextParameter() const {
25   return def->getValueAsBit("hasInferredContextParam");
26 }
27 
28 //===----------------------------------------------------------------------===//
29 // AttrOrTypeDef
30 //===----------------------------------------------------------------------===//
31 
32 AttrOrTypeDef::AttrOrTypeDef(const llvm::Record *def) : def(def) {
33   // Populate the builders.
34   auto *builderList =
35       dyn_cast_or_null<llvm::ListInit>(def->getValueInit("builders"));
36   if (builderList && !builderList->empty()) {
37     for (llvm::Init *init : builderList->getValues()) {
38       AttrOrTypeBuilder builder(cast<llvm::DefInit>(init)->getDef(),
39                                 def->getLoc());
40 
41       // Ensure that all parameters have names.
42       for (const AttrOrTypeBuilder::Parameter &param :
43            builder.getParameters()) {
44         if (!param.getName())
45           PrintFatalError(def->getLoc(), "builder parameters must have a name");
46       }
47       builders.emplace_back(builder);
48     }
49   }
50 
51   // Populate the traits.
52   if (auto *traitList = def->getValueAsListInit("traits")) {
53     SmallPtrSet<const llvm::Init *, 32> traitSet;
54     traits.reserve(traitSet.size());
55     for (auto *traitInit : *traitList)
56       if (traitSet.insert(traitInit).second)
57         traits.push_back(Trait::create(traitInit));
58   }
59 
60   // Populate the parameters.
61   if (auto *parametersDag = def->getValueAsDag("parameters")) {
62     for (unsigned i = 0, e = parametersDag->getNumArgs(); i < e; ++i)
63       parameters.push_back(AttrOrTypeParameter(parametersDag, i));
64   }
65 
66   // Verify the use of the mnemonic field.
67   bool hasCppFormat = hasCustomAssemblyFormat();
68   bool hasDeclarativeFormat = getAssemblyFormat().has_value();
69   if (getMnemonic()) {
70     if (hasCppFormat && hasDeclarativeFormat) {
71       PrintFatalError(getLoc(), "cannot specify both 'assemblyFormat' "
72                                 "and 'hasCustomAssemblyFormat'");
73     }
74     if (!parameters.empty() && !hasCppFormat && !hasDeclarativeFormat) {
75       PrintFatalError(getLoc(),
76                       "must specify either 'assemblyFormat' or "
77                       "'hasCustomAssemblyFormat' when 'mnemonic' is set");
78     }
79   } else if (hasCppFormat || hasDeclarativeFormat) {
80     PrintFatalError(getLoc(),
81                     "'assemblyFormat' or 'hasCustomAssemblyFormat' can only be "
82                     "used when 'mnemonic' is set");
83   }
84   // Assembly format requires accessors to be generated.
85   if (hasDeclarativeFormat && !genAccessors()) {
86     PrintFatalError(getLoc(),
87                     "'assemblyFormat' requires 'genAccessors' to be true");
88   }
89 }
90 
91 Dialect AttrOrTypeDef::getDialect() const {
92   auto *dialect = dyn_cast<llvm::DefInit>(def->getValue("dialect")->getValue());
93   return Dialect(dialect ? dialect->getDef() : nullptr);
94 }
95 
96 StringRef AttrOrTypeDef::getName() const { return def->getName(); }
97 
98 StringRef AttrOrTypeDef::getCppClassName() const {
99   return def->getValueAsString("cppClassName");
100 }
101 
102 StringRef AttrOrTypeDef::getCppBaseClassName() const {
103   return def->getValueAsString("cppBaseClassName");
104 }
105 
106 bool AttrOrTypeDef::hasDescription() const {
107   const llvm::RecordVal *desc = def->getValue("description");
108   return desc && isa<llvm::StringInit>(desc->getValue());
109 }
110 
111 StringRef AttrOrTypeDef::getDescription() const {
112   return def->getValueAsString("description");
113 }
114 
115 bool AttrOrTypeDef::hasSummary() const {
116   const llvm::RecordVal *summary = def->getValue("summary");
117   return summary && isa<llvm::StringInit>(summary->getValue());
118 }
119 
120 StringRef AttrOrTypeDef::getSummary() const {
121   return def->getValueAsString("summary");
122 }
123 
124 StringRef AttrOrTypeDef::getStorageClassName() const {
125   return def->getValueAsString("storageClass");
126 }
127 
128 StringRef AttrOrTypeDef::getStorageNamespace() const {
129   return def->getValueAsString("storageNamespace");
130 }
131 
132 bool AttrOrTypeDef::genStorageClass() const {
133   return def->getValueAsBit("genStorageClass");
134 }
135 
136 bool AttrOrTypeDef::hasStorageCustomConstructor() const {
137   return def->getValueAsBit("hasStorageCustomConstructor");
138 }
139 
140 unsigned AttrOrTypeDef::getNumParameters() const {
141   auto *parametersDag = def->getValueAsDag("parameters");
142   return parametersDag ? parametersDag->getNumArgs() : 0;
143 }
144 
145 Optional<StringRef> AttrOrTypeDef::getMnemonic() const {
146   return def->getValueAsOptionalString("mnemonic");
147 }
148 
149 bool AttrOrTypeDef::hasCustomAssemblyFormat() const {
150   return def->getValueAsBit("hasCustomAssemblyFormat");
151 }
152 
153 Optional<StringRef> AttrOrTypeDef::getAssemblyFormat() const {
154   return def->getValueAsOptionalString("assemblyFormat");
155 }
156 
157 bool AttrOrTypeDef::genAccessors() const {
158   return def->getValueAsBit("genAccessors");
159 }
160 
161 bool AttrOrTypeDef::genVerifyDecl() const {
162   return def->getValueAsBit("genVerifyDecl");
163 }
164 
165 Optional<StringRef> AttrOrTypeDef::getExtraDecls() const {
166   auto value = def->getValueAsString("extraClassDeclaration");
167   return value.empty() ? Optional<StringRef>() : value;
168 }
169 
170 ArrayRef<SMLoc> AttrOrTypeDef::getLoc() const { return def->getLoc(); }
171 
172 bool AttrOrTypeDef::skipDefaultBuilders() const {
173   return def->getValueAsBit("skipDefaultBuilders");
174 }
175 
176 bool AttrOrTypeDef::operator==(const AttrOrTypeDef &other) const {
177   return def == other.def;
178 }
179 
180 bool AttrOrTypeDef::operator<(const AttrOrTypeDef &other) const {
181   return getName() < other.getName();
182 }
183 
184 //===----------------------------------------------------------------------===//
185 // AttrDef
186 //===----------------------------------------------------------------------===//
187 
188 Optional<StringRef> AttrDef::getTypeBuilder() const {
189   return def->getValueAsOptionalString("typeBuilder");
190 }
191 
192 bool AttrDef::classof(const AttrOrTypeDef *def) {
193   return def->getDef()->isSubClassOf("AttrDef");
194 }
195 
196 //===----------------------------------------------------------------------===//
197 // AttrOrTypeParameter
198 //===----------------------------------------------------------------------===//
199 
200 template <typename InitT>
201 auto AttrOrTypeParameter::getDefValue(StringRef name) const {
202   Optional<decltype(std::declval<InitT>().getValue())> result;
203   if (auto *param = dyn_cast<llvm::DefInit>(getDef()))
204     if (auto *init = param->getDef()->getValue(name))
205       if (auto *value = dyn_cast_or_null<InitT>(init->getValue()))
206         result = value->getValue();
207   return result;
208 }
209 
210 bool AttrOrTypeParameter::isAnonymous() const {
211   return !def->getArgName(index);
212 }
213 
214 StringRef AttrOrTypeParameter::getName() const {
215   return def->getArgName(index)->getValue();
216 }
217 
218 std::string AttrOrTypeParameter::getAccessorName() const {
219   return "get" +
220          llvm::convertToCamelFromSnakeCase(getName(), /*capitalizeFirst=*/true);
221 }
222 
223 Optional<StringRef> AttrOrTypeParameter::getAllocator() const {
224   return getDefValue<llvm::StringInit>("allocator");
225 }
226 
227 StringRef AttrOrTypeParameter::getComparator() const {
228   return getDefValue<llvm::StringInit>("comparator").value_or("$_lhs == $_rhs");
229 }
230 
231 StringRef AttrOrTypeParameter::getCppType() const {
232   if (auto *stringType = dyn_cast<llvm::StringInit>(getDef()))
233     return stringType->getValue();
234   return getDefValue<llvm::StringInit>("cppType").getValue();
235 }
236 
237 StringRef AttrOrTypeParameter::getCppAccessorType() const {
238   return getDefValue<llvm::StringInit>("cppAccessorType")
239       .value_or(getCppType());
240 }
241 
242 StringRef AttrOrTypeParameter::getCppStorageType() const {
243   return getDefValue<llvm::StringInit>("cppStorageType").value_or(getCppType());
244 }
245 
246 StringRef AttrOrTypeParameter::getConvertFromStorage() const {
247   return getDefValue<llvm::StringInit>("convertFromStorage")
248       .getValueOr("$_self");
249 }
250 
251 Optional<StringRef> AttrOrTypeParameter::getParser() const {
252   return getDefValue<llvm::StringInit>("parser");
253 }
254 
255 Optional<StringRef> AttrOrTypeParameter::getPrinter() const {
256   return getDefValue<llvm::StringInit>("printer");
257 }
258 
259 Optional<StringRef> AttrOrTypeParameter::getSummary() const {
260   return getDefValue<llvm::StringInit>("summary");
261 }
262 
263 StringRef AttrOrTypeParameter::getSyntax() const {
264   if (auto *stringType = dyn_cast<llvm::StringInit>(getDef()))
265     return stringType->getValue();
266   return getDefValue<llvm::StringInit>("syntax").value_or(getCppType());
267 }
268 
269 bool AttrOrTypeParameter::isOptional() const {
270   // Parameters with default values are automatically optional.
271   return getDefValue<llvm::BitInit>("isOptional").value_or(false) ||
272          getDefaultValue();
273 }
274 
275 Optional<StringRef> AttrOrTypeParameter::getDefaultValue() const {
276   return getDefValue<llvm::StringInit>("defaultValue");
277 }
278 
279 llvm::Init *AttrOrTypeParameter::getDef() const { return def->getArg(index); }
280 
281 //===----------------------------------------------------------------------===//
282 // AttributeSelfTypeParameter
283 //===----------------------------------------------------------------------===//
284 
285 bool AttributeSelfTypeParameter::classof(const AttrOrTypeParameter *param) {
286   llvm::Init *paramDef = param->getDef();
287   if (auto *paramDefInit = dyn_cast<llvm::DefInit>(paramDef))
288     return paramDefInit->getDef()->isSubClassOf("AttributeSelfTypeParameter");
289   return false;
290 }
291