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"> {
226226 let mnemonic = "attr_with_optional_unsigned";
227227}
228228
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+
229235def TestAttrUgly : Test_Attr<"TestAttrUgly"> {
230236 let parameters = (ins "::mlir::Attribute":$attr);
231237
Original file line number Diff line number Diff line change @@ -26,7 +26,13 @@ attributes {
2626 // CHECK: #test.attr_with_optional_signed<-9223372036854775808>
2727 attr9 = #test.attr_with_optional_signed <-9223372036854775808 >,
2828 // 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 >
3036}
3137
3238// 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}> {{
101101 return parser.emitError(loc, "invalid {2} specification: ") << enumKeyword;
102102 }
103103};
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+ };
104125} // namespace mlir
105126
106127namespace llvm {
You can’t perform that action at this time.
0 commit comments