Skip to content
Snippets Groups Projects
Commit d259594b authored by Mogball's avatar Mogball
Browse files

[mlir][ods] AttrOrTypeDef format: parse types

Add template specialization to `FieldParser` for parsing types.

Reviewed By: mehdi_amini

Differential Revision: https://reviews.llvm.org/D113867
parent 0b158c6c
No related branches found
No related tags found
No related merge requests found
......@@ -70,6 +70,18 @@ struct FieldParser<
}
};
/// 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,
......
......@@ -161,4 +161,12 @@ def TestAttrParams: Test_Attr<"TestAttrParams"> {
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
......@@ -12,7 +12,9 @@ attributes {
// 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
......
......@@ -125,3 +125,11 @@ func private @test_verifier_fails() -> () attributes {
// 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>>
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment