| 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/Error.h" |
| 12 | #include "llvm/TableGen/Record.h" |
| 13 | |
| 14 | using namespace mlir; |
| 15 | using namespace mlir::tblgen; |
| 16 | |
| 17 | using llvm::DefInit; |
| 18 | using llvm::Init; |
| 19 | using llvm::Record; |
| 20 | |
| 21 | EnumCase::EnumCase(const Record *record) : def(record) { |
| 22 | assert(def->isSubClassOf("EnumCase") && |
| 23 | "must be subclass of TableGen 'EnumCase' class"); |
| 24 | } |
| 25 | |
| 26 | EnumCase::EnumCase(const DefInit *init) : EnumCase(init->getDef()) {} |
| 27 | |
| 28 | StringRef EnumCase::getSymbol() const { |
| 29 | return def->getValueAsString(FieldName: "symbol"); |
| 30 | } |
| 31 | |
| 32 | StringRef EnumCase::getStr() const { return def->getValueAsString(FieldName: "str"); } |
| 33 | |
| 34 | int64_t EnumCase::getValue() const { return def->getValueAsInt(FieldName: "value"); } |
| 35 | |
| 36 | const Record &EnumCase::getDef() const { return *def; } |
| 37 | |
| 38 | EnumInfo::EnumInfo(const Record *record) : def(record) { |
| 39 | assert(isSubClassOf("EnumInfo") && |
| 40 | "must be subclass of TableGen 'EnumInfo' class"); |
| 41 | } |
| 42 | |
| 43 | EnumInfo::EnumInfo(const Record &record) : EnumInfo(&record) {} |
| 44 | |
| 45 | EnumInfo::EnumInfo(const DefInit *init) : EnumInfo(init->getDef()) {} |
| 46 | |
| 47 | bool EnumInfo::isSubClassOf(StringRef className) const { |
| 48 | return def->isSubClassOf(Name: className); |
| 49 | } |
| 50 | |
| 51 | bool EnumInfo::isEnumAttr() const { return isSubClassOf(className: "EnumAttrInfo"); } |
| 52 | |
| 53 | std::optional<Attribute> EnumInfo::asEnumAttr() const { |
| 54 | if (isEnumAttr()) |
| 55 | return Attribute(def); |
| 56 | return std::nullopt; |
| 57 | } |
| 58 | |
| 59 | bool EnumInfo::isBitEnum() const { return isSubClassOf(className: "BitEnumBase"); } |
| 60 | |
| 61 | bool EnumInfo::isNewStyleBitEnum() const { |
| 62 | return isBitEnum() && !printBitEnumQuoted(); |
| 63 | } |
| 64 | |
| 65 | bool EnumInfo::hasParserPrinter() const { |
| 66 | std::optional<Attribute> enumAttrInfo = asEnumAttr(); |
| 67 | return !getUnderlyingType().empty() && |
| 68 | (!enumAttrInfo || !enumAttrInfo->getConstBuilderTemplate().empty()); |
| 69 | } |
| 70 | |
| 71 | StringRef EnumInfo::getEnumClassName() const { |
| 72 | return def->getValueAsString(FieldName: "className"); |
| 73 | } |
| 74 | |
| 75 | StringRef EnumInfo::getSummary() const { |
| 76 | return def->getValueAsString(FieldName: "summary"); |
| 77 | } |
| 78 | |
| 79 | StringRef EnumInfo::getDescription() const { |
| 80 | return def->getValueAsString(FieldName: "description"); |
| 81 | } |
| 82 | |
| 83 | StringRef EnumInfo::getCppNamespace() const { |
| 84 | return def->getValueAsString(FieldName: "cppNamespace"); |
| 85 | } |
| 86 | |
| 87 | std::string EnumInfo::getQualifiedName() const { |
| 88 | return (getCppNamespace() + "::"+ getEnumClassName()).str(); |
| 89 | } |
| 90 | |
| 91 | int64_t EnumInfo::getBitwidth() const { return def->getValueAsInt(FieldName: "bitwidth"); } |
| 92 | |
| 93 | StringRef EnumInfo::getUnderlyingType() const { |
| 94 | return def->getValueAsString(FieldName: "underlyingType"); |
| 95 | } |
| 96 | |
| 97 | StringRef EnumInfo::getUnderlyingToSymbolFnName() const { |
| 98 | return def->getValueAsString(FieldName: "underlyingToSymbolFnName"); |
| 99 | } |
| 100 | |
| 101 | StringRef EnumInfo::getStringToSymbolFnName() const { |
| 102 | return def->getValueAsString(FieldName: "stringToSymbolFnName"); |
| 103 | } |
| 104 | |
| 105 | StringRef EnumInfo::getSymbolToStringFnName() const { |
| 106 | return def->getValueAsString(FieldName: "symbolToStringFnName"); |
| 107 | } |
| 108 | |
| 109 | StringRef EnumInfo::getSymbolToStringFnRetType() const { |
| 110 | return def->getValueAsString(FieldName: "symbolToStringFnRetType"); |
| 111 | } |
| 112 | |
| 113 | StringRef EnumInfo::getMaxEnumValFnName() const { |
| 114 | return def->getValueAsString(FieldName: "maxEnumValFnName"); |
| 115 | } |
| 116 | |
| 117 | std::vector<EnumCase> EnumInfo::getAllCases() const { |
| 118 | const auto *inits = def->getValueAsListInit(FieldName: "enumerants"); |
| 119 | |
| 120 | std::vector<EnumCase> cases; |
| 121 | cases.reserve(n: inits->size()); |
| 122 | |
| 123 | for (const Init *init : *inits) { |
| 124 | cases.emplace_back(args: cast<DefInit>(Val: init)); |
| 125 | } |
| 126 | |
| 127 | return cases; |
| 128 | } |
| 129 | |
| 130 | bool EnumInfo::genSpecializedAttr() const { |
| 131 | return isSubClassOf(className: "EnumAttrInfo") && |
| 132 | def->getValueAsBit(FieldName: "genSpecializedAttr"); |
| 133 | } |
| 134 | |
| 135 | const Record *EnumInfo::getBaseAttrClass() const { |
| 136 | return def->getValueAsDef(FieldName: "baseAttrClass"); |
| 137 | } |
| 138 | |
| 139 | StringRef EnumInfo::getSpecializedAttrClassName() const { |
| 140 | return def->getValueAsString(FieldName: "specializedAttrClassName"); |
| 141 | } |
| 142 | |
| 143 | bool EnumInfo::printBitEnumPrimaryGroups() const { |
| 144 | return def->getValueAsBit(FieldName: "printBitEnumPrimaryGroups"); |
| 145 | } |
| 146 | |
| 147 | bool EnumInfo::printBitEnumQuoted() const { |
| 148 | return def->getValueAsBit(FieldName: "printBitEnumQuoted"); |
| 149 | } |
| 150 | |
| 151 | StringRef EnumInfo::getBitEnumSeparator() const { |
| 152 | return def->getValueAsString(FieldName: "separator"); |
| 153 | } |
| 154 | |
| 155 | EnumInfo::BitEnumSeparatorKind EnumInfo::getBitEnumSeparatorKind() const { |
| 156 | StringRef separator = getBitEnumSeparator().trim(); |
| 157 | if (separator == "|") |
| 158 | return BitEnumSeparatorKind::VerticalBar; |
| 159 | if (separator == ",") |
| 160 | return BitEnumSeparatorKind::Comma; |
| 161 | if (separator.empty()) |
| 162 | return BitEnumSeparatorKind::Space; |
| 163 | PrintFatalError(ErrorLoc: def->getLoc(), |
| 164 | Msg: "bit enum separator must be '|', ',', or a space ' '"); |
| 165 | } |
| 166 | |
| 167 | StringRef EnumInfo::getBitEnumSeparatorParser() const { |
| 168 | switch (getBitEnumSeparatorKind()) { |
| 169 | case BitEnumSeparatorKind::VerticalBar: |
| 170 | return "parseOptionalVerticalBar"; |
| 171 | case BitEnumSeparatorKind::Comma: |
| 172 | return "parseOptionalComma"; |
| 173 | case BitEnumSeparatorKind::Space: |
| 174 | // Whitespace is not a token, so there is nothing to parse between cases. |
| 175 | PrintFatalError(ErrorLoc: def->getLoc(), |
| 176 | Msg: "a bit enum with a space separator has no separator " |
| 177 | "parser"); |
| 178 | } |
| 179 | llvm_unreachable("unknown bit enum separator kind"); |
| 180 | } |
| 181 | |
| 182 | const Record &EnumInfo::getDef() const { return *def; } |
| 183 |