From: Rico Tzschichholz Date: Mon, 19 Jun 2023 08:05:04 +0000 (+0200) Subject: girwriter: Fix c:type attribute for real struct parameters X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0f6eb0dafe912d9550bcd783cd36b7d5dd254690;p=thirdparty%2Fvala.git girwriter: Fix c:type attribute for real struct parameters Non-simple structs are always by reference Fixes https://gitlab.gnome.org/GNOME/vala/issues/1444 --- diff --git a/codegen/valagirwriter.vala b/codegen/valagirwriter.vala index 5bfece823..405f8a2e5 100644 --- a/codegen/valagirwriter.vala +++ b/codegen/valagirwriter.vala @@ -1635,7 +1635,17 @@ public class Vala.GIRWriter : CodeVisitor { if (has_array_length) { length_param_index = tag == "parameter" ? index + 1 : index; } - write_type (type, length_param_index, direction); + + bool additional_indirection = direction != ParameterDirection.IN; + if (!additional_indirection && tag == "parameter" && !type.nullable) { + // pass non-simple structs always by reference + unowned Struct? st = type.type_symbol as Struct; + if (st != null && !st.is_simple_type ()) { + additional_indirection = true; + } + } + + write_type (type, length_param_index, additional_indirection); } indent--; @@ -1657,7 +1667,7 @@ public class Vala.GIRWriter : CodeVisitor { buffer.append_printf (" glib:get-type=\"%sget_type\"", get_ccode_lower_case_prefix (symbol)); } - private void write_type (DataType type, int index = -1, ParameterDirection direction = ParameterDirection.IN) { + private void write_type (DataType type, int index = -1, bool additional_indirection = false) { if (type is ArrayType) { var array_type = (ArrayType) type; @@ -1669,7 +1679,7 @@ public class Vala.GIRWriter : CodeVisitor { } else if (index != -1) { buffer.append_printf (" length=\"%i\"", index); } - buffer.append_printf (" c:type=\"%s%s\"", get_ccode_name (array_type.element_type), direction == ParameterDirection.IN ? "*" : "**"); + buffer.append_printf (" c:type=\"%s%s\"", get_ccode_name (array_type.element_type), !additional_indirection ? "*" : "**"); buffer.append_printf (">\n"); indent++; @@ -1683,7 +1693,7 @@ public class Vala.GIRWriter : CodeVisitor { buffer.append_printf ("\n"); } else if (type is PointerType) { write_indent (); - buffer.append_printf ("\n", get_ccode_name (type), direction == ParameterDirection.IN ? "" : "*"); + buffer.append_printf ("\n", get_ccode_name (type), !additional_indirection ? "" : "*"); } else if (type is GenericType) { // generic type parameters not supported in GIR write_indent (); @@ -1691,7 +1701,7 @@ public class Vala.GIRWriter : CodeVisitor { } else if (type is DelegateType) { var deleg_type = (DelegateType) type; write_indent (); - buffer.append_printf ("\n", gi_type_name (deleg_type.delegate_symbol), get_ccode_name (type), direction == ParameterDirection.IN ? "" : "*"); + buffer.append_printf ("\n", gi_type_name (deleg_type.delegate_symbol), get_ccode_name (type), !additional_indirection ? "" : "*"); } else if (type.type_symbol != null) { write_indent (); string type_name = gi_type_name (type.type_symbol); @@ -1699,7 +1709,7 @@ public class Vala.GIRWriter : CodeVisitor { if ((type_name == "GLib.Array") || (type_name == "GLib.PtrArray")) { is_array = true; } - buffer.append_printf ("<%s name=\"%s\" c:type=\"%s%s\"", is_array ? "array" : "type", gi_type_name (type.type_symbol), get_ccode_name (type), direction == ParameterDirection.IN ? "" : "*"); + buffer.append_printf ("<%s name=\"%s\" c:type=\"%s%s\"", is_array ? "array" : "type", gi_type_name (type.type_symbol), get_ccode_name (type), !additional_indirection ? "" : "*"); List type_arguments = type.get_type_arguments (); if (type_arguments.size == 0) { diff --git a/tests/girwriter/GirTest-1.0.gir-expected b/tests/girwriter/GirTest-1.0.gir-expected index 5aafe5126..c3da33e4d 100644 --- a/tests/girwriter/GirTest-1.0.gir-expected +++ b/tests/girwriter/GirTest-1.0.gir-expected @@ -141,6 +141,9 @@ + + + @@ -2823,6 +2826,19 @@ + + + + + + + + + + + + + @@ -2880,6 +2896,19 @@ + + + + + + + + + + + + + @@ -3019,7 +3048,7 @@ - + diff --git a/tests/girwriter/girtest.vala b/tests/girwriter/girtest.vala index 1125bfa1c..191f01a1c 100644 --- a/tests/girwriter/girtest.vala +++ b/tests/girwriter/girtest.vala @@ -18,6 +18,9 @@ namespace GirTest { public BoxedStruct ret () { return this; } + + public void @set (BoxedStruct param1) { + } } [CCode (has_type_id = false)] @@ -39,6 +42,9 @@ namespace GirTest { public Struct ret () { return this; } + + public void @set (Struct param1) { + } } [GIR (visible = false)] @@ -192,6 +198,8 @@ namespace GirTest { public int field = 42; + public Struct struct_field; + internal int internal_field = 23; public int fixed_array_field[23]; diff --git a/tests/girwriter/girtest.vapi-expected b/tests/girwriter/girtest.vapi-expected index b63aad4aa..681e4ab7d 100644 --- a/tests/girwriter/girtest.vapi-expected +++ b/tests/girwriter/girtest.vapi-expected @@ -65,6 +65,7 @@ namespace GirTest { public int field; public int fixed_array_field[23]; public string? nullable_field; + public GirTest.Struct struct_field; public ObjectTest (); public void array_in (int[] array); public void array_inout (ref int[] array); @@ -177,6 +178,7 @@ namespace GirTest { public BoxedStruct.foo (int param1, int param2); public void inv (); public GirTest.BoxedStruct ret (); + public void @set (GirTest.BoxedStruct param1); } [CCode (cheader_filename = "girtest.h")] [GIR (name = "RenamedStruct")] @@ -196,6 +198,7 @@ namespace GirTest { public Struct.foo (int param1, int param2); public void inv (); public GirTest.Struct ret (); + public void @set (GirTest.Struct param1); } [CCode (cheader_filename = "girtest.h")] public enum EnumTest { diff --git a/tests/girwriter/girtest.vapigen-expected b/tests/girwriter/girtest.vapigen-expected index d559fbea0..a3f579324 100644 --- a/tests/girwriter/girtest.vapigen-expected +++ b/tests/girwriter/girtest.vapigen-expected @@ -74,6 +74,7 @@ namespace GirTest { public int fixed_array_field[23]; public int internal_field; public string? nullable_field; + public GirTest.Struct struct_field; [CCode (has_construct_function = false)] public ObjectTest (); public void array_in ([CCode (array_length_cname = "array_length1", array_length_pos = 1.1)] int[] array); @@ -206,6 +207,7 @@ namespace GirTest { public BoxedStruct.foo (int param1, int param2); public void inv (); public GirTest.BoxedStruct ret (); + public void @set (GirTest.BoxedStruct param1); } [CCode (cheader_filename = "girtest.h", cname = "GirTestNamedCompactClass", has_type_id = false)] public struct RenamedCompactClass { @@ -222,6 +224,7 @@ namespace GirTest { public Struct.foo (int param1, int param2); public void inv (); public GirTest.Struct ret (); + public void @set (GirTest.Struct param1); } [CCode (cheader_filename = "girtest.h", cprefix = "GIR_TEST_ENUM_TEST_", type_id = "gir_test_enum_test_get_type ()")] public enum EnumTest {