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