Files
clang-p2996/mlir/lib/TableGen/EnumInfo.cpp
Krzysztof Drewniak d7c53a91c2 [mlir] Decouple enum generation from attributes, adding EnumInfo and EnumCase (#132148)
This commit pulls apart the inherent attribute dependence of classes
like EnumAttrInfo and EnumAttrCase, factoring them out into simpler
EnumCase and EnumInfo variants. This allows specifying the cases of an
enum without needing to make the cases, or the EnumInfo itself, a
subclass of SignlessIntegerAttrBase.

The existing classes are retained as subclasses of the new ones, both
for backwards compatibility and to allow attribute-specific information.

In addition, the new BitEnum class changes its default printer/parser
behavior: cases when multiple keywords appear, like having both nuw and
nsw in overflow flags, will no longer be quoted by the operator<<, and
the FieldParser instance will now expect multiple keywords. All
instances of BitEnumAttr retain the old behavior.
2025-03-27 19:40:06 -05:00

137 lines
3.7 KiB
C++

//===- EnumInfo.cpp - EnumInfo wrapper class ----------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "mlir/TableGen/EnumInfo.h"
#include "mlir/TableGen/Attribute.h"
#include "llvm/TableGen/Record.h"
using namespace mlir;
using namespace mlir::tblgen;
using llvm::DefInit;
using llvm::Init;
using llvm::Record;
EnumCase::EnumCase(const Record *record) : def(record) {
assert(def->isSubClassOf("EnumCase") &&
"must be subclass of TableGen 'EnumCase' class");
}
EnumCase::EnumCase(const DefInit *init) : EnumCase(init->getDef()) {}
StringRef EnumCase::getSymbol() const {
return def->getValueAsString("symbol");
}
StringRef EnumCase::getStr() const { return def->getValueAsString("str"); }
int64_t EnumCase::getValue() const { return def->getValueAsInt("value"); }
const Record &EnumCase::getDef() const { return *def; }
EnumInfo::EnumInfo(const Record *record) : def(record) {
assert(isSubClassOf("EnumInfo") &&
"must be subclass of TableGen 'EnumInfo' class");
}
EnumInfo::EnumInfo(const Record &record) : EnumInfo(&record) {}
EnumInfo::EnumInfo(const DefInit *init) : EnumInfo(init->getDef()) {}
bool EnumInfo::isSubClassOf(StringRef className) const {
return def->isSubClassOf(className);
}
bool EnumInfo::isEnumAttr() const { return isSubClassOf("EnumAttrInfo"); }
std::optional<Attribute> EnumInfo::asEnumAttr() const {
if (isEnumAttr())
return Attribute(def);
return std::nullopt;
}
bool EnumInfo::isBitEnum() const { return isSubClassOf("BitEnumBase"); }
StringRef EnumInfo::getEnumClassName() const {
return def->getValueAsString("className");
}
StringRef EnumInfo::getSummary() const {
return def->getValueAsString("summary");
}
StringRef EnumInfo::getDescription() const {
return def->getValueAsString("description");
}
StringRef EnumInfo::getCppNamespace() const {
return def->getValueAsString("cppNamespace");
}
int64_t EnumInfo::getBitwidth() const { return def->getValueAsInt("bitwidth"); }
StringRef EnumInfo::getUnderlyingType() const {
return def->getValueAsString("underlyingType");
}
StringRef EnumInfo::getUnderlyingToSymbolFnName() const {
return def->getValueAsString("underlyingToSymbolFnName");
}
StringRef EnumInfo::getStringToSymbolFnName() const {
return def->getValueAsString("stringToSymbolFnName");
}
StringRef EnumInfo::getSymbolToStringFnName() const {
return def->getValueAsString("symbolToStringFnName");
}
StringRef EnumInfo::getSymbolToStringFnRetType() const {
return def->getValueAsString("symbolToStringFnRetType");
}
StringRef EnumInfo::getMaxEnumValFnName() const {
return def->getValueAsString("maxEnumValFnName");
}
std::vector<EnumCase> EnumInfo::getAllCases() const {
const auto *inits = def->getValueAsListInit("enumerants");
std::vector<EnumCase> cases;
cases.reserve(inits->size());
for (const Init *init : *inits) {
cases.emplace_back(cast<DefInit>(init));
}
return cases;
}
bool EnumInfo::genSpecializedAttr() const {
return isSubClassOf("EnumAttrInfo") &&
def->getValueAsBit("genSpecializedAttr");
}
const Record *EnumInfo::getBaseAttrClass() const {
return def->getValueAsDef("baseAttrClass");
}
StringRef EnumInfo::getSpecializedAttrClassName() const {
return def->getValueAsString("specializedAttrClassName");
}
bool EnumInfo::printBitEnumPrimaryGroups() const {
return def->getValueAsBit("printBitEnumPrimaryGroups");
}
bool EnumInfo::printBitEnumQuoted() const {
return def->getValueAsBit("printBitEnumQuoted");
}
const Record &EnumInfo::getDef() const { return *def; }