1 //===- Dialect.cpp - Dialect 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 // Dialect wrapper to simplify using TableGen Record defining a MLIR dialect.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/TableGen/Dialect.h"
14 #include "llvm/TableGen/Record.h"
15 
16 using namespace mlir;
17 using namespace mlir::tblgen;
18 Dialect::Dialect(const llvm::Record *def) : def(def) {
19   if (def == nullptr)
20     return;
21   for (StringRef dialect : def->getValueAsListOfStrings("dependentDialects"))
22     dependentDialects.push_back(dialect);
23 }
24 
25 StringRef Dialect::getName() const { return def->getValueAsString("name"); }
26 
27 StringRef Dialect::getCppNamespace() const {
28   return def->getValueAsString("cppNamespace");
29 }
30 
31 std::string Dialect::getCppClassName() const {
32   // Simply use the name and remove any '_' tokens.
33   std::string cppName = def->getName().str();
34   llvm::erase_if(cppName, [](char c) { return c == '_'; });
35   return cppName;
36 }
37 
38 static StringRef getAsStringOrEmpty(const llvm::Record &record,
39                                     StringRef fieldName) {
40   if (auto valueInit = record.getValueInit(fieldName)) {
41     if (llvm::isa<llvm::StringInit>(valueInit))
42       return record.getValueAsString(fieldName);
43   }
44   return "";
45 }
46 
47 StringRef Dialect::getSummary() const {
48   return getAsStringOrEmpty(*def, "summary");
49 }
50 
51 StringRef Dialect::getDescription() const {
52   return getAsStringOrEmpty(*def, "description");
53 }
54 
55 ArrayRef<StringRef> Dialect::getDependentDialects() const {
56   return dependentDialects;
57 }
58 
59 llvm::Optional<StringRef> Dialect::getExtraClassDeclaration() const {
60   auto value = def->getValueAsString("extraClassDeclaration");
61   return value.empty() ? llvm::Optional<StringRef>() : value;
62 }
63 
64 bool Dialect::hasCanonicalizer() const {
65   return def->getValueAsBit("hasCanonicalizer");
66 }
67 
68 bool Dialect::hasConstantMaterializer() const {
69   return def->getValueAsBit("hasConstantMaterializer");
70 }
71 
72 bool Dialect::hasNonDefaultDestructor() const {
73   return def->getValueAsBit("hasNonDefaultDestructor");
74 }
75 
76 bool Dialect::hasOperationAttrVerify() const {
77   return def->getValueAsBit("hasOperationAttrVerify");
78 }
79 
80 bool Dialect::hasRegionArgAttrVerify() const {
81   return def->getValueAsBit("hasRegionArgAttrVerify");
82 }
83 
84 bool Dialect::hasRegionResultAttrVerify() const {
85   return def->getValueAsBit("hasRegionResultAttrVerify");
86 }
87 
88 bool Dialect::hasOperationInterfaceFallback() const {
89   return def->getValueAsBit("hasOperationInterfaceFallback");
90 }
91 
92 bool Dialect::operator==(const Dialect &other) const {
93   return def == other.def;
94 }
95 
96 bool Dialect::operator<(const Dialect &other) const {
97   return getName() < other.getName();
98 }
99