From: Rico Tzschichholz Date: Sat, 2 Nov 2019 06:37:33 +0000 (+0100) Subject: codegen: Make type-parameter properties readable X-Git-Tag: 0.44.10~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6b4137560b3f207bb3fa3c1c7ac3b57f540c593e;p=thirdparty%2Fvala.git codegen: Make type-parameter properties readable Those are immutable while being construct-only properties. See https://gitlab.gnome.org/GNOME/vala/issues/190 --- diff --git a/codegen/valagobjectmodule.vala b/codegen/valagobjectmodule.vala index 8ad676ab9..61eb59645 100644 --- a/codegen/valagobjectmodule.vala +++ b/codegen/valagobjectmodule.vala @@ -93,7 +93,7 @@ public class Vala.GObjectModule : GTypeModule { cspec.add_argument (new CCodeConstant ("\"type\"")); cspec.add_argument (new CCodeConstant ("\"type\"")); cspec.add_argument (new CCodeIdentifier ("G_TYPE_NONE")); - cspec.add_argument (new CCodeConstant ("G_PARAM_STATIC_STRINGS | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY")); + cspec.add_argument (new CCodeConstant ("G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY")); cinst.add_argument (cspec); ccode.add_expression (cinst); prop_enum.add_value (new CCodeEnumValue (enum_value)); @@ -109,7 +109,7 @@ public class Vala.GObjectModule : GTypeModule { cspec.add_argument (func_name_constant); cspec.add_argument (new CCodeConstant ("\"dup func\"")); cspec.add_argument (new CCodeConstant ("\"dup func\"")); - cspec.add_argument (new CCodeConstant ("G_PARAM_STATIC_STRINGS | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY")); + cspec.add_argument (new CCodeConstant ("G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY")); cinst.add_argument (cspec); ccode.add_expression (cinst); prop_enum.add_value (new CCodeEnumValue (enum_value)); @@ -125,7 +125,7 @@ public class Vala.GObjectModule : GTypeModule { cspec.add_argument (func_name_constant); cspec.add_argument (new CCodeConstant ("\"destroy func\"")); cspec.add_argument (new CCodeConstant ("\"destroy func\"")); - cspec.add_argument (new CCodeConstant ("G_PARAM_STATIC_STRINGS | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY")); + cspec.add_argument (new CCodeConstant ("G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY")); cinst.add_argument (cspec); ccode.add_expression (cinst); prop_enum.add_value (new CCodeEnumValue (enum_value)); @@ -279,6 +279,43 @@ public class Vala.GObjectModule : GTypeModule { } ccode.add_break (); } + + /* type, dup func, and destroy func properties for generic types */ + foreach (TypeParameter type_param in cl.get_type_parameters ()) { + string func_name, enum_value; + CCodeMemberAccess cfield; + CCodeFunctionCall csetcall; + + func_name = "%s_type".printf (type_param.name.ascii_down ()); + enum_value = "%s_%s".printf (get_ccode_lower_case_name (cl, null), func_name).ascii_up (); + ccode.add_case (new CCodeIdentifier (enum_value)); + cfield = new CCodeMemberAccess.pointer (new CCodeMemberAccess.pointer (new CCodeIdentifier ("self"), "priv"), func_name); + csetcall = new CCodeFunctionCall (new CCodeIdentifier ("g_value_set_gtype")); + csetcall.add_argument (new CCodeIdentifier ("value")); + csetcall.add_argument (cfield); + ccode.add_expression (csetcall); + ccode.add_break (); + + func_name = "%s_dup_func".printf (type_param.name.ascii_down ()); + enum_value = "%s_%s".printf (get_ccode_lower_case_name (cl, null), func_name).ascii_up (); + ccode.add_case (new CCodeIdentifier (enum_value)); + cfield = new CCodeMemberAccess.pointer (new CCodeMemberAccess.pointer (new CCodeIdentifier ("self"), "priv"), func_name); + csetcall = new CCodeFunctionCall (new CCodeIdentifier ("g_value_set_pointer")); + csetcall.add_argument (new CCodeIdentifier ("value")); + csetcall.add_argument (cfield); + ccode.add_expression (csetcall); + ccode.add_break (); + + func_name = "%s_destroy_func".printf (type_param.name.ascii_down ()); + enum_value = "%s_%s".printf (get_ccode_lower_case_name (cl, null), func_name).ascii_up (); + ccode.add_case (new CCodeIdentifier (enum_value)); + cfield = new CCodeMemberAccess.pointer (new CCodeMemberAccess.pointer (new CCodeIdentifier ("self"), "priv"), func_name); + csetcall = new CCodeFunctionCall (new CCodeIdentifier ("g_value_set_pointer")); + csetcall.add_argument (new CCodeIdentifier ("value")); + csetcall.add_argument (cfield); + ccode.add_expression (csetcall); + ccode.add_break (); + } ccode.add_default (); emit_invalid_property_id_warn (); ccode.add_break (); diff --git a/tests/Makefile.am b/tests/Makefile.am index c5e3f319f..cb6b69912 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -488,6 +488,7 @@ TESTS = \ generics/constructor-chain-up.vala \ generics/inference-static-function.vala \ generics/parameter-sizeof-initializer.vala \ + generics/type-parameter-properties.vala \ generics/bug640330.test \ generics/bug640330.vala \ generics/bug694765-1.vala \ diff --git a/tests/generics/type-parameter-properties.vala b/tests/generics/type-parameter-properties.vala new file mode 100644 index 000000000..3004d198a --- /dev/null +++ b/tests/generics/type-parameter-properties.vala @@ -0,0 +1,29 @@ +class Foo : Object { +} + +class Bar : Foo { +} + +void main () { + var bar = new Bar (); + { + Type type; + BoxedCopyFunc dup_func; + DestroyNotify destroy_func; + + bar.get ("t-type", out type, "t-dup-func", out dup_func, "t-destroy-func", out destroy_func); + assert (type == typeof(string)); + assert (dup_func == (BoxedCopyFunc) string.dup); + assert (destroy_func == (DestroyNotify) free); + } + { + Type type; + BoxedCopyFunc dup_func; + DestroyNotify destroy_func; + + bar.get ("g-type", out type, "g-dup-func", out dup_func, "g-destroy-func", out destroy_func); + assert (type == typeof(string)); + assert (dup_func == (BoxedCopyFunc) string.dup); + assert (destroy_func == (DestroyNotify) free); + } +}