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 
19 Dialect::Dialect(const llvm::Record *def) : def(def) {
20   for (StringRef dialect : def->getValueAsListOfStrings("dependentDialects"))
21     dependentDialects.push_back(dialect);
22 }
23 
24 StringRef Dialect::getName() const { return def->getValueAsString("name"); }
25 
26 StringRef Dialect::getCppNamespace() const {
27   return def->getValueAsString("cppNamespace");
28 }
29 
30 std::string Dialect::getCppClassName() const {
31   // Simply use the name and remove any '_' tokens.
32   std::string cppName = def->getName().str();
33   llvm::erase_if(cppName, [](char c) { return c == '_'; });
34   return cppName;
35 }
36 
37 static StringRef getAsStringOrEmpty(const llvm::Record &record,
38                                     StringRef fieldName) {
39   if (auto valueInit = record.getValueInit(fieldName)) {
40     if (llvm::isa<llvm::CodeInit, llvm::StringInit>(valueInit))
41       return record.getValueAsString(fieldName);
42   }
43   return "";
44 }
45 
46 StringRef Dialect::getSummary() const {
47   return getAsStringOrEmpty(*def, "summary");
48 }
49 
50 StringRef Dialect::getDescription() const {
51   return getAsStringOrEmpty(*def, "description");
52 }
53 
54 ArrayRef<StringRef> Dialect::getDependentDialects() const {
55   return dependentDialects;
56 }
57 
58 llvm::Optional<StringRef> Dialect::getExtraClassDeclaration() const {
59   auto value = def->getValueAsString("extraClassDeclaration");
60   return value.empty() ? llvm::Optional<StringRef>() : value;
61 }
62 
63 bool Dialect::hasConstantMaterializer() const {
64   return def->getValueAsBit("hasConstantMaterializer");
65 }
66 
67 bool Dialect::hasOperationAttrVerify() const {
68   return def->getValueAsBit("hasOperationAttrVerify");
69 }
70 
71 bool Dialect::hasRegionArgAttrVerify() const {
72   return def->getValueAsBit("hasRegionArgAttrVerify");
73 }
74 
75 bool Dialect::hasRegionResultAttrVerify() const {
76   return def->getValueAsBit("hasRegionResultAttrVerify");
77 }
78 
79 bool Dialect::operator==(const Dialect &other) const {
80   return def == other.def;
81 }
82 
83 bool Dialect::operator<(const Dialect &other) const {
84   return getName() < other.getName();
85 }
86