1 // RUN: mlir-tblgen -gen-enum-decls -I %S/../../include %s | FileCheck %s --check-prefix=DECL
2 // RUN: mlir-tblgen -gen-enum-defs -I %S/../../include %s | FileCheck %s --check-prefix=DEF
4 include "mlir/IR/EnumAttr.td"
5 include "mlir/IR/OpBase.td"
8 def None: I32BitEnumAttrCaseNone<"None", "none">;
9 def Bit0: I32BitEnumAttrCaseBit<"Bit0", 0, "tagged">;
10 def Bit1: I32BitEnumAttrCaseBit<"Bit1", 1>;
11 def Bit2: I32BitEnumAttrCaseBit<"Bit2", 2>;
12 def Bit3: I32BitEnumAttrCaseBit<"Bit3", 3>;
13 def BitGroup: I32BitEnumAttrCaseGroup<"BitGroup", [
17 def MyBitEnum: I32BitEnumAttr<"MyBitEnum", "An example bit enum",
18 [None, Bit0, Bit1, Bit2, Bit3, BitGroup]> {
19 let genSpecializedAttr = 0;
22 // DECL-LABEL: enum class MyBitEnum : uint32_t
30 // DECL: ::std::optional<MyBitEnum> symbolizeMyBitEnum(uint32_t);
31 // DECL: std::string stringifyMyBitEnum(MyBitEnum);
32 // DECL: ::std::optional<MyBitEnum> symbolizeMyBitEnum(::llvm::StringRef);
34 // DECL: struct FieldParser<::MyBitEnum, ::MyBitEnum> {
35 // DECL: template <typename ParserT>
36 // DECL: static FailureOr<::MyBitEnum> parse(ParserT &parser) {
37 // DECL: // Parse the keyword/string containing the enum.
38 // DECL: std::string enumKeyword;
39 // DECL: auto loc = parser.getCurrentLocation();
40 // DECL: if (failed(parser.parseOptionalKeywordOrString(&enumKeyword)))
41 // DECL: return parser.emitError(loc, "expected keyword for An example bit enum");
42 // DECL: // Symbolize the keyword.
43 // DECL: if (::std::optional<::MyBitEnum> attr = ::symbolizeEnum<::MyBitEnum>(enumKeyword))
44 // DECL: return *attr;
45 // DECL: return parser.emitError(loc, "invalid An example bit enum specification: ") << enumKeyword;
48 // DECL: inline ::llvm::raw_ostream &operator<<(::llvm::raw_ostream &p, ::MyBitEnum value) {
49 // DECL: auto valueStr = stringifyEnum(value);
50 // DECL: switch (value) {
51 // DECL: case ::MyBitEnum::BitGroup:
52 // DECL: return p << valueStr;
56 // DECL: auto underlyingValue = static_cast<std::make_unsigned_t<::MyBitEnum>>(value);
57 // DECL: if (underlyingValue && !llvm::has_single_bit(underlyingValue))
58 // DECL: return p << '"' << valueStr << '"';
59 // DECL: return p << valueStr;
61 // DEF-LABEL: std::string stringifyMyBitEnum
62 // DEF: auto val = static_cast<uint32_t>
63 // DEF: if (val == 0) return "none";
64 // DEF: if (1u == (1u & val))
65 // DEF-NEXT: push_back("tagged")
66 // DEF: if (2u == (2u & val))
67 // DEF-NEXT: push_back("Bit1")
69 // DEF-LABEL: ::std::optional<MyBitEnum> symbolizeMyBitEnum(::llvm::StringRef str)
70 // DEF: if (str == "none") return MyBitEnum::None;
71 // DEF: .Case("tagged", 1)
72 // DEF: .Case("Bit1", 2)
74 // Test enum printer generation for non non-keyword enums.
76 def NonKeywordBit: I32BitEnumAttrCaseBit<"Bit0", 0, "tag-ged">;
77 def MyMixedNonKeywordBitEnum: I32BitEnumAttr<"MyMixedNonKeywordBitEnum", "An example bit enum", [
81 let genSpecializedAttr = 0;
84 def MyNonKeywordBitEnum: I32BitEnumAttr<"MyNonKeywordBitEnum", "An example bit enum", [
87 let genSpecializedAttr = 0;
90 // DECL: inline ::llvm::raw_ostream &operator<<(::llvm::raw_ostream &p, ::MyMixedNonKeywordBitEnum value) {
91 // DECL: auto valueStr = stringifyEnum(value);
92 // DECL: switch (value) {
93 // DECL: case ::MyMixedNonKeywordBitEnum::Bit1:
96 // DECL: return p << '"' << valueStr << '"';
98 // DECL: return p << valueStr;
101 // DECL: inline ::llvm::raw_ostream &operator<<(::llvm::raw_ostream &p, ::MyNonKeywordBitEnum value) {
102 // DECL: auto valueStr = stringifyEnum(value);
103 // DECL: return p << '"' << valueStr << '"';