From: Rico Tzschichholz Date: Thu, 2 May 2019 16:09:19 +0000 (+0200) Subject: girwriter: Write implicit parameters and properties for generics X-Git-Tag: 0.45.1~18 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0d0e39bf7737b96367cef8507fd875350536d2db;p=thirdparty%2Fvala.git girwriter: Write implicit parameters and properties for generics --- diff --git a/codegen/valagirwriter.vala b/codegen/valagirwriter.vala index e3684c003..590a887c3 100644 --- a/codegen/valagirwriter.vala +++ b/codegen/valagirwriter.vala @@ -454,6 +454,12 @@ public class Vala.GIRWriter : CodeVisitor { write_indent (); buffer.append_printf("\n"); + if (cl.base_class != null && cl.base_class.is_subtype_of (gobject_type)) { + foreach (var p in cl.get_type_parameters ()) { + write_type_parameter (p, "property"); + } + } + hierarchy.insert (0, cl); cl.accept_children (this); hierarchy.remove_at (0); @@ -968,7 +974,46 @@ public class Vala.GIRWriter : CodeVisitor { } } - private void write_params_and_return (List params, DataType? return_type, bool return_array_length, string? return_comment = null, bool constructor = false, DataType? instance_type = null, bool user_data = false) { + private void write_type_parameter (TypeParameter type_parameter, string tag_type) { + write_indent (); + if (tag_type == "property") { + buffer.append_printf ("<%s name=\"%s-type\" writable=\"1\" construct-only=\"1\">\n", tag_type, type_parameter.name.down ()); + } else { + buffer.append_printf ("<%s name=\"%s_type\" transfer-ownership=\"none\">\n", tag_type, type_parameter.name.down ()); + } + indent++; + write_indent (); + buffer.append_printf ("\n"); + indent--; + write_indent (); + buffer.append_printf ("\n", tag_type); + write_indent (); + if (tag_type == "property") { + buffer.append_printf ("<%s name=\"%s-dup-func\" writable=\"1\" construct-only=\"1\">\n", tag_type, type_parameter.name.down ()); + } else { + buffer.append_printf ("<%s name=\"%s_dup_func\" transfer-ownership=\"none\">\n", tag_type, type_parameter.name.down ()); + } + indent++; + write_indent (); + buffer.append_printf ("\n"); + indent--; + write_indent (); + buffer.append_printf ("\n", tag_type); + write_indent (); + if (tag_type == "property") { + buffer.append_printf ("<%s name=\"%s-destroy-func\" writable=\"1\" construct-only=\"1\">\n", tag_type, type_parameter.name.down ()); + } else { + buffer.append_printf ("<%s name=\"%s_destroy_func\" transfer-ownership=\"none\">\n", tag_type, type_parameter.name.down ()); + } + indent++; + write_indent (); + buffer.append_printf ("\n"); + indent--; + write_indent (); + buffer.append_printf ("\n", tag_type); + } + + private void write_params_and_return (List params, List? type_params, DataType? return_type, bool return_array_length, string? return_comment = null, bool constructor = false, DataType? instance_type = null, bool user_data = false) { int last_index = 0; bool ret_is_struct = return_type != null && return_type.is_real_non_null_struct_type (); @@ -1003,7 +1048,7 @@ public class Vala.GIRWriter : CodeVisitor { write_param_or_return (new VoidType (), false, ref last_index, false, null, return_comment, ParameterDirection.IN); } - if (params.size != 0 || instance_type != null || (return_type is ArrayType && return_array_length) || (return_type is DelegateType) || ret_is_struct) { + if (params.size != 0 || (type_params != null && type_params.size > 0) || instance_type != null || (return_type is ArrayType && return_array_length) || (return_type is DelegateType) || ret_is_struct) { write_indent (); buffer.append_printf ("\n"); indent++; @@ -1013,6 +1058,13 @@ public class Vala.GIRWriter : CodeVisitor { write_param_or_return (instance_type, true, ref index, false, "self"); } + if (type_params != null) { + foreach (var p in type_params) { + write_type_parameter (p, "parameter"); + index += 3; + } + } + foreach (Parameter param in params) { write_param_or_return (param.variable_type, true, ref index, get_ccode_array_length (param), param.name, get_parameter_comment (param), param.direction, false, false, param.ellipsis); @@ -1064,7 +1116,7 @@ public class Vala.GIRWriter : CodeVisitor { write_doc (get_delegate_comment (cb)); - write_params_and_return (cb.get_parameters (), cb.return_type, get_ccode_array_length (cb), get_delegate_return_comment (cb), false, null, cb.has_target); + write_params_and_return (cb.get_parameters (), cb.get_type_parameters (), cb.return_type, get_ccode_array_length (cb), get_delegate_return_comment (cb), false, null, cb.has_target); indent--; write_indent (); @@ -1184,7 +1236,7 @@ public class Vala.GIRWriter : CodeVisitor { instance_type = CCodeBaseModule.get_data_type_for_symbol ((TypeSymbol) m.parent_symbol); } - write_params_and_return (params, return_type, get_ccode_array_length (m), return_comment, false, instance_type); + write_params_and_return (params, m.get_type_parameters (), return_type, get_ccode_array_length (m), return_comment, false, instance_type); indent--; write_indent (); @@ -1228,7 +1280,11 @@ public class Vala.GIRWriter : CodeVisitor { write_doc (get_method_comment (m)); var datatype = CCodeBaseModule.get_data_type_for_symbol ((TypeSymbol) m.parent_symbol); - write_params_and_return (m.get_parameters (), datatype, false, get_method_return_comment (m), true); + List? type_params = null; + if (m.parent_symbol is Class) { + type_params = ((Class) m.parent_symbol).get_type_parameters (); + } + write_params_and_return (m.get_parameters (), type_params, datatype, false, get_method_return_comment (m), true); indent--; write_indent (); @@ -1301,7 +1357,7 @@ public class Vala.GIRWriter : CodeVisitor { write_doc (get_signal_comment (sig)); - write_params_and_return (sig.get_parameters (), sig.return_type, false, get_signal_return_comment (sig)); + write_params_and_return (sig.get_parameters (), null, sig.return_type, false, get_signal_return_comment (sig)); indent--; write_indent (); diff --git a/tests/girwriter/GirTest-1.0.gir-expected b/tests/girwriter/GirTest-1.0.gir-expected index a8bf8181d..9aba2a5f8 100644 --- a/tests/girwriter/GirTest-1.0.gir-expected +++ b/tests/girwriter/GirTest-1.0.gir-expected @@ -960,6 +960,148 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/girwriter/girtest.vala b/tests/girwriter/girtest.vala index ef35e8ec8..1164ab1da 100644 --- a/tests/girwriter/girtest.vala +++ b/tests/girwriter/girtest.vala @@ -272,4 +272,17 @@ namespace GirTest { [Version (deprecated = true, deprecated_since = "0.1.2", since = "0.1.0")] public class DeprecatedClassTest { } + + public class GenericsTest { + public GenericsTest (owned DelegateTest cb) { + } + + public void method (T param) { + } + } + + public class GenericsObjectTest : Object { + public void method (K[] param) { + } + } } diff --git a/tests/girwriter/girtest.vapi-expected b/tests/girwriter/girtest.vapi-expected index 4a0709bc2..8e3b3b697 100644 --- a/tests/girwriter/girtest.vapi-expected +++ b/tests/girwriter/girtest.vapi-expected @@ -14,6 +14,16 @@ namespace GirTest { public DeprecatedClassTest (); } [CCode (cheader_filename = "girtest.h")] + public class GenericsObjectTest : GLib.Object { + public GenericsObjectTest (); + public void method (K[] param); + } + [CCode (cheader_filename = "girtest.h")] + public class GenericsTest { + public GenericsTest (owned GirTest.DelegateTest cb); + public void method (T param); + } + [CCode (cheader_filename = "girtest.h")] public class ImplementionTest : GLib.Object, GirTest.InterfaceTest { public ImplementionTest (); }