| 1 | //===- EnumInfo.cpp - EnumInfo 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 | #include "mlir/TableGen/EnumInfo.h" |
| 10 | #include "mlir/TableGen/Attribute.h" |
| 11 | #include "llvm/TableGen/Record.h" |
| 12 | |
| 13 | using namespace mlir; |
| 14 | using namespace mlir::tblgen; |
| 15 | |
| 16 | using llvm::DefInit; |
| 17 | using llvm::Init; |
| 18 | using llvm::Record; |
| 19 | |
| 20 | EnumCase::EnumCase(const Record *record) : def(record) { |
| 21 | assert(def->isSubClassOf("EnumCase") && |
| 22 | "must be subclass of TableGen 'EnumCase' class"); |
| 23 | } |
| 24 | |
| 25 | EnumCase::EnumCase(const DefInit *init) : EnumCase(init->getDef()) {} |
| 26 | |
| 27 | StringRef EnumCase::getSymbol() const { |
| 28 | return def->getValueAsString(FieldName: "symbol"); |
| 29 | } |
| 30 | |
| 31 | StringRef EnumCase::getStr() const { return def->getValueAsString(FieldName: "str"); } |
| 32 | |
| 33 | int64_t EnumCase::getValue() const { return def->getValueAsInt(FieldName: "value"); } |
| 34 | |
| 35 | const Record &EnumCase::getDef() const { return *def; } |
| 36 | |
| 37 | EnumInfo::EnumInfo(const Record *record) : def(record) { |
| 38 | assert(isSubClassOf("EnumInfo") && |
| 39 | "must be subclass of TableGen 'EnumInfo' class"); |
| 40 | } |
| 41 | |
| 42 | EnumInfo::EnumInfo(const Record &record) : EnumInfo(&record) {} |
| 43 | |
| 44 | EnumInfo::EnumInfo(const DefInit *init) : EnumInfo(init->getDef()) {} |
| 45 | |
| 46 | bool EnumInfo::isSubClassOf(StringRef className) const { |
| 47 | return def->isSubClassOf(Name: className); |
| 48 | } |
| 49 | |
| 50 | bool EnumInfo::isEnumAttr() const { return isSubClassOf(className: "EnumAttrInfo"); } |
| 51 | |
| 52 | std::optional<Attribute> EnumInfo::asEnumAttr() const { |
| 53 | if (isEnumAttr()) |
| 54 | return Attribute(def); |
| 55 | return std::nullopt; |
| 56 | } |
| 57 | |
| 58 | bool EnumInfo::isBitEnum() const { return isSubClassOf(className: "BitEnumBase"); } |
| 59 | |
| 60 | StringRef EnumInfo::getEnumClassName() const { |
| 61 | return def->getValueAsString(FieldName: "className"); |
| 62 | } |
| 63 | |
| 64 | StringRef EnumInfo::getSummary() const { |
| 65 | return def->getValueAsString(FieldName: "summary"); |
| 66 | } |
| 67 | |
| 68 | StringRef EnumInfo::getDescription() const { |
| 69 | return def->getValueAsString(FieldName: "description"); |
| 70 | } |
| 71 | |
| 72 | StringRef EnumInfo::getCppNamespace() const { |
| 73 | return def->getValueAsString(FieldName: "cppNamespace"); |
| 74 | } |
| 75 | |
| 76 | int64_t EnumInfo::getBitwidth() const { return def->getValueAsInt(FieldName: "bitwidth"); } |
| 77 | |
| 78 | StringRef EnumInfo::getUnderlyingType() const { |
| 79 | return def->getValueAsString(FieldName: "underlyingType"); |
| 80 | } |
| 81 | |
| 82 | StringRef EnumInfo::getUnderlyingToSymbolFnName() const { |
| 83 | return def->getValueAsString(FieldName: "underlyingToSymbolFnName"); |
| 84 | } |
| 85 | |
| 86 | StringRef EnumInfo::getStringToSymbolFnName() const { |
| 87 | return def->getValueAsString(FieldName: "stringToSymbolFnName"); |
| 88 | } |
| 89 | |
| 90 | StringRef EnumInfo::getSymbolToStringFnName() const { |
| 91 | return def->getValueAsString(FieldName: "symbolToStringFnName"); |
| 92 | } |
| 93 | |
| 94 | StringRef EnumInfo::getSymbolToStringFnRetType() const { |
| 95 | return def->getValueAsString(FieldName: "symbolToStringFnRetType"); |
| 96 | } |
| 97 | |
| 98 | StringRef EnumInfo::getMaxEnumValFnName() const { |
| 99 | return def->getValueAsString(FieldName: "maxEnumValFnName"); |
| 100 | } |
| 101 | |
| 102 | std::vector<EnumCase> EnumInfo::getAllCases() const { |
| 103 | const auto *inits = def->getValueAsListInit(FieldName: "enumerants"); |
| 104 | |
| 105 | std::vector<EnumCase> cases; |
| 106 | cases.reserve(n: inits->size()); |
| 107 | |
| 108 | for (const Init *init : *inits) { |
| 109 | cases.emplace_back(args: cast<DefInit>(Val: init)); |
| 110 | } |
| 111 | |
| 112 | return cases; |
| 113 | } |
| 114 | |
| 115 | bool EnumInfo::genSpecializedAttr() const { |
| 116 | return isSubClassOf(className: "EnumAttrInfo") && |
| 117 | def->getValueAsBit(FieldName: "genSpecializedAttr"); |
| 118 | } |
| 119 | |
| 120 | const Record *EnumInfo::getBaseAttrClass() const { |
| 121 | return def->getValueAsDef(FieldName: "baseAttrClass"); |
| 122 | } |
| 123 | |
| 124 | StringRef EnumInfo::getSpecializedAttrClassName() const { |
| 125 | return def->getValueAsString(FieldName: "specializedAttrClassName"); |
| 126 | } |
| 127 | |
| 128 | bool EnumInfo::printBitEnumPrimaryGroups() const { |
| 129 | return def->getValueAsBit(FieldName: "printBitEnumPrimaryGroups"); |
| 130 | } |
| 131 | |
| 132 | bool EnumInfo::printBitEnumQuoted() const { |
| 133 | return def->getValueAsBit(FieldName: "printBitEnumQuoted"); |
| 134 | } |
| 135 | |
| 136 | const Record &EnumInfo::getDef() const { return *def; } |
| 137 |
