File tree Expand file tree Collapse file tree 3 files changed +34
-1
lines changed Expand file tree Collapse file tree 3 files changed +34
-1
lines changed Original file line number Diff line number Diff line change @@ -226,6 +226,12 @@ def TestAttrWithOptionalUnsigned : Test_Attr<"TestAttrWithOptionalUnsigned"> {
226
226
let mnemonic = "attr_with_optional_unsigned";
227
227
}
228
228
229
+ def TestAttrWithOptionalEnum : Test_Attr<"TestAttrWithOptionalEnum"> {
230
+ let parameters = (ins OptionalParameter<"std::optional<SimpleEnum>">:$value);
231
+ let assemblyFormat = "`<` $value `>`";
232
+ let mnemonic = "attr_with_optional_enum";
233
+ }
234
+
229
235
def TestAttrUgly : Test_Attr<"TestAttrUgly"> {
230
236
let parameters = (ins "::mlir::Attribute":$attr);
231
237
Original file line number Diff line number Diff line change @@ -26,7 +26,13 @@ attributes {
26
26
// CHECK: #test.attr_with_optional_signed<-9223372036854775808>
27
27
attr9 = #test.attr_with_optional_signed <-9223372036854775808 >,
28
28
// CHECK: #test.attr_with_optional_unsigned<18446744073709551615>
29
- attr_10 = #test.attr_with_optional_unsigned <18446744073709551615 >
29
+ attr_10 = #test.attr_with_optional_unsigned <18446744073709551615 >,
30
+ // CHECK: #test.attr_with_optional_enum<>
31
+ attr_11 = #test.attr_with_optional_enum <>,
32
+ // CHECK: #test.attr_with_optional_enum<a>
33
+ attr_12 = #test.attr_with_optional_enum <a >,
34
+ // CHECK: #test.attr_with_optional_enum<b>
35
+ attr_13 = #test.attr_with_optional_enum <b >
30
36
}
31
37
32
38
// CHECK-LABEL: @test_roundtrip_default_parsers_struct
Original file line number Diff line number Diff line change @@ -101,6 +101,27 @@ struct FieldParser<{0}, {0}> {{
101
101
return parser.emitError(loc, "invalid {2} specification: ") << enumKeyword;
102
102
}
103
103
};
104
+
105
+ /// Support for std::optional, useful in attribute/type definition where the enum is
106
+ /// used as:
107
+ ///
108
+ /// let parameters = (ins OptionalParameter<"std::optional<TheEnumName>">:$value);
109
+ template<>
110
+ struct FieldParser<std::optional<{0}>, std::optional<{0}>> {{
111
+ template <typename ParserT>
112
+ static FailureOr<std::optional<{0}>> parse(ParserT &parser) {{
113
+ // Parse the keyword/string containing the enum.
114
+ std::string enumKeyword;
115
+ auto loc = parser.getCurrentLocation();
116
+ if (failed(parser.parseOptionalKeywordOrString(&enumKeyword)))
117
+ return std::optional<{0}>{{};
118
+
119
+ // Symbolize the keyword.
120
+ if (::std::optional<{0}> attr = {1}::symbolizeEnum<{0}>(enumKeyword))
121
+ return attr;
122
+ return parser.emitError(loc, "invalid {2} specification: ") << enumKeyword;
123
+ }
124
+ };
104
125
} // namespace mlir
105
126
106
127
namespace llvm {
You can’t perform that action at this time.
0 commit comments