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