Add template specialization to `FieldParser` for parsing types.
Reviewed By: mehdi_amini
Differential Revision: https://reviews.llvm.org/D113867
}
};
+/// Parse a type.
+template <typename TypeT>
+struct FieldParser<
+ TypeT, std::enable_if_t<std::is_base_of<Type, TypeT>::value, TypeT>> {
+ static FailureOr<TypeT> parse(AsmParser &parser) {
+ TypeT value;
+ if (parser.parseType(value))
+ return failure();
+ return value;
+ }
+};
+
/// Parse any integer.
template <typename IntT>
struct FieldParser<IntT,
let assemblyFormat = "`<` params `>`";
}
+// Test types can be parsed/printed.
+def TestAttrWithTypeParam : Test_Attr<"TestAttrWithTypeParam"> {
+ let parameters = (ins "::mlir::IntegerType":$int_type,
+ "::mlir::Type":$any_type);
+ let mnemonic = "attr_with_type";
+ let assemblyFormat = "`<` $int_type `,` $any_type `>`";
+}
+
#endif // TEST_ATTRDEFS
// CHECK: #test<"attr_ugly begin 5 : index end">
attr2 = #test<"attr_ugly begin 5 : index end">,
// CHECK: #test.attr_params<42, 24>
- attr3 = #test.attr_params<42, 24>
+ attr3 = #test.attr_params<42, 24>,
+ // CHECK: #test.attr_with_type<i32, vector<4xi32>>
+ attr4 = #test.attr_with_type<i32, vector<4xi32>>
}
// CHECK-LABEL: @test_roundtrip_default_parsers_struct
// expected-error@+1 {{expected 'one' to equal 'four.size()'}}
attr = #test.attr_with_format<42 : two = "hello", four = [1, 2, 3] : 42 : i64>
}
+
+// -----
+
+func private @test_attr_with_type_failed_to_parse_type() -> () attributes {
+ // expected-error@+2 {{invalid kind of type specified}}
+ // expected-error@+1 {{failed to parse TestAttrWithTypeParam parameter 'int_type'}}
+ attr = #test.attr_with_type<vector<4xi32>, vector<4xi32>>
+}