llvm-project
103 строки · 3.8 Кб
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
3
4include "mlir/IR/EnumAttr.td"
5include "mlir/IR/OpBase.td"
6
7// Test bit enums
8def None: I32BitEnumAttrCaseNone<"None", "none">;
9def Bit0: I32BitEnumAttrCaseBit<"Bit0", 0, "tagged">;
10def Bit1: I32BitEnumAttrCaseBit<"Bit1", 1>;
11def Bit2: I32BitEnumAttrCaseBit<"Bit2", 2>;
12def Bit3: I32BitEnumAttrCaseBit<"Bit3", 3>;
13def BitGroup: I32BitEnumAttrCaseGroup<"BitGroup", [
14Bit0, Bit1
15]>;
16
17def MyBitEnum: I32BitEnumAttr<"MyBitEnum", "An example bit enum",
18[None, Bit0, Bit1, Bit2, Bit3, BitGroup]> {
19let genSpecializedAttr = 0;
20}
21
22// DECL-LABEL: enum class MyBitEnum : uint32_t
23// DECL: None = 0,
24// DECL: Bit0 = 1,
25// DECL: Bit1 = 2,
26// DECL: Bit2 = 4,
27// DECL: Bit3 = 8,
28// DECL: }
29
30// DECL: ::std::optional<MyBitEnum> symbolizeMyBitEnum(uint32_t);
31// DECL: std::string stringifyMyBitEnum(MyBitEnum);
32// DECL: ::std::optional<MyBitEnum> symbolizeMyBitEnum(::llvm::StringRef);
33
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;
46// DECL: }
47
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;
53// DECL: default:
54// DECL: break;
55// DECL: }
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;
60
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")
68
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)
73
74// Test enum printer generation for non non-keyword enums.
75
76def NonKeywordBit: I32BitEnumAttrCaseBit<"Bit0", 0, "tag-ged">;
77def MyMixedNonKeywordBitEnum: I32BitEnumAttr<"MyMixedNonKeywordBitEnum", "An example bit enum", [
78NonKeywordBit,
79Bit1
80]> {
81let genSpecializedAttr = 0;
82}
83
84def MyNonKeywordBitEnum: I32BitEnumAttr<"MyNonKeywordBitEnum", "An example bit enum", [
85NonKeywordBit
86]> {
87let genSpecializedAttr = 0;
88}
89
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:
94// DECL: break;
95// DECL: default:
96// DECL: return p << '"' << valueStr << '"';
97// DECL: }
98// DECL: return p << valueStr;
99// DECL: }
100
101// DECL: inline ::llvm::raw_ostream &operator<<(::llvm::raw_ostream &p, ::MyNonKeywordBitEnum value) {
102// DECL: auto valueStr = stringifyEnum(value);
103// DECL: return p << '"' << valueStr << '"';
104