| 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/Error.h" |
| 15 | #include "llvm/TableGen/Record.h" |
| 16 | |
| 17 | using namespace mlir; |
| 18 | using namespace mlir::tblgen; |
| 19 | Dialect::Dialect(const llvm::Record *def) : def(def) { |
| 20 | if (def == nullptr) |
| 21 | return; |
| 22 | for (StringRef dialect : def->getValueAsListOfStrings(FieldName: "dependentDialects")) |
| 23 | dependentDialects.push_back(x: dialect); |
| 24 | } |
| 25 | |
| 26 | StringRef Dialect::getName() const { return def->getValueAsString(FieldName: "name"); } |
| 27 | |
| 28 | StringRef Dialect::getCppNamespace() const { |
| 29 | return def->getValueAsString(FieldName: "cppNamespace"); |
| 30 | } |
| 31 | |
| 32 | std::string Dialect::getCppClassName() const { |
| 33 | // Simply use the name and remove any '_' tokens. |
| 34 | std::string cppName = def->getName().str(); |
| 35 | llvm::erase(C&: cppName, V: '_'); |
| 36 | return cppName; |
| 37 | } |
| 38 | |
| 39 | static StringRef getAsStringOrEmpty(const llvm::Record &record, |
| 40 | StringRef fieldName) { |
| 41 | if (auto *valueInit = record.getValueInit(FieldName: fieldName)) { |
| 42 | if (llvm::isa<llvm::StringInit>(Val: valueInit)) |
| 43 | return record.getValueAsString(FieldName: fieldName); |
| 44 | } |
| 45 | return ""; |
| 46 | } |
| 47 | |
| 48 | StringRef Dialect::getSummary() const { |
| 49 | return getAsStringOrEmpty(record: *def, fieldName: "summary"); |
| 50 | } |
| 51 | |
| 52 | StringRef Dialect::getDescription() const { |
| 53 | return getAsStringOrEmpty(record: *def, fieldName: "description"); |
| 54 | } |
| 55 | |
| 56 | ArrayRef<StringRef> Dialect::getDependentDialects() const { |
| 57 | return dependentDialects; |
| 58 | } |
| 59 | |
| 60 | std::optional<StringRef> Dialect::getExtraClassDeclaration() const { |
| 61 | auto value = def->getValueAsString(FieldName: "extraClassDeclaration"); |
| 62 | return value.empty() ? std::optional<StringRef>() : value; |
| 63 | } |
| 64 | |
| 65 | bool Dialect::hasCanonicalizer() const { |
| 66 | return def->getValueAsBit(FieldName: "hasCanonicalizer"); |
| 67 | } |
| 68 | |
| 69 | bool Dialect::hasConstantMaterializer() const { |
| 70 | return def->getValueAsBit(FieldName: "hasConstantMaterializer"); |
| 71 | } |
| 72 | |
| 73 | bool Dialect::hasNonDefaultDestructor() const { |
| 74 | return def->getValueAsBit(FieldName: "hasNonDefaultDestructor"); |
| 75 | } |
| 76 | |
| 77 | bool Dialect::hasOperationAttrVerify() const { |
| 78 | return def->getValueAsBit(FieldName: "hasOperationAttrVerify"); |
| 79 | } |
| 80 | |
| 81 | bool Dialect::hasRegionArgAttrVerify() const { |
| 82 | return def->getValueAsBit(FieldName: "hasRegionArgAttrVerify"); |
| 83 | } |
| 84 | |
| 85 | bool Dialect::hasRegionResultAttrVerify() const { |
| 86 | return def->getValueAsBit(FieldName: "hasRegionResultAttrVerify"); |
| 87 | } |
| 88 | |
| 89 | bool Dialect::hasOperationInterfaceFallback() const { |
| 90 | return def->getValueAsBit(FieldName: "hasOperationInterfaceFallback"); |
| 91 | } |
| 92 | |
| 93 | bool Dialect::useDefaultAttributePrinterParser() const { |
| 94 | return def->getValueAsBit(FieldName: "useDefaultAttributePrinterParser"); |
| 95 | } |
| 96 | |
| 97 | bool Dialect::useDefaultTypePrinterParser() const { |
| 98 | return def->getValueAsBit(FieldName: "useDefaultTypePrinterParser"); |
| 99 | } |
| 100 | |
| 101 | bool Dialect::isExtensible() const { |
| 102 | return def->getValueAsBit(FieldName: "isExtensible"); |
| 103 | } |
| 104 | |
| 105 | bool Dialect::usePropertiesForAttributes() const { |
| 106 | return def->getValueAsBit(FieldName: "usePropertiesForAttributes"); |
| 107 | } |
| 108 | |
| 109 | const llvm::DagInit *Dialect::getDiscardableAttributes() const { |
| 110 | return def->getValueAsDag(FieldName: "discardableAttrs"); |
| 111 | } |
| 112 | |
| 113 | bool Dialect::operator==(const Dialect &other) const { |
| 114 | return def == other.def; |
| 115 | } |
| 116 | |
| 117 | bool Dialect::operator<(const Dialect &other) const { |
| 118 | return getName() < other.getName(); |
| 119 | } |
| 120 |
