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