]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
girwriter: Fix c:type attribute for real struct parameters
authorRico Tzschichholz <ricotz@ubuntu.com>
Mon, 19 Jun 2023 08:05:04 +0000 (10:05 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Mon, 19 Jun 2023 08:05:04 +0000 (10:05 +0200)
Non-simple structs are always by reference

Fixes https://gitlab.gnome.org/GNOME/vala/issues/1444

codegen/valagirwriter.vala
tests/girwriter/GirTest-1.0.gir-expected
tests/girwriter/girtest.vala
tests/girwriter/girtest.vapi-expected
tests/girwriter/girtest.vapigen-expected

index 5bfece82394279b6ec8b291e91b2f55a37d23643..405f8a2e52f9a22041e80f2e8979dff7a2275038 100644 (file)
@@ -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 ("<type name=\"none\" c:type=\"void\"/>\n");
                } else if (type is PointerType) {
                        write_indent ();
-                       buffer.append_printf ("<type name=\"gpointer\" c:type=\"%s%s\"/>\n", get_ccode_name (type), direction == ParameterDirection.IN ? "" : "*");
+                       buffer.append_printf ("<type name=\"gpointer\" c:type=\"%s%s\"/>\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 ("<type name=\"%s\" c:type=\"%s%s\"/>\n", gi_type_name (deleg_type.delegate_symbol), get_ccode_name (type), direction == ParameterDirection.IN ? "" : "*");
+                       buffer.append_printf ("<type name=\"%s\" c:type=\"%s%s\"/>\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<DataType> type_arguments = type.get_type_arguments ();
                        if (type_arguments.size == 0) {
index 5aafe512636071cab06943bed4ef3bbd64240e07..c3da33e4d3630e4201f8f576813b669deaa2617b 100644 (file)
                <field name="field" writable="1">
                        <type name="gint" c:type="gint"/>
                </field>
+               <field name="struct_field" writable="1">
+                       <type name="GirTest.Struct" c:type="GirTestStruct"/>
+               </field>
                <field name="internal_field" writable="1">
                        <type name="gint" c:type="gint"/>
                </field>
                                </parameter>
                        </parameters>
                </method>
+               <method name="set" c:identifier="gir_test_boxed_struct_set">
+                       <return-value transfer-ownership="full">
+                               <type name="none" c:type="void"/>
+                       </return-value>
+                       <parameters>
+                               <instance-parameter name="self" transfer-ownership="none" nullable="1">
+                                       <type name="GirTest.BoxedStruct" c:type="GirTestBoxedStruct*"/>
+                               </instance-parameter>
+                               <parameter name="param1" transfer-ownership="none">
+                                       <type name="GirTest.BoxedStruct" c:type="GirTestBoxedStruct*"/>
+                               </parameter>
+                       </parameters>
+               </method>
        </record>
        <record name="Struct" c:type="GirTestStruct" c:symbol-prefix="struct">
                <field name="field_name" writable="1">
                                </parameter>
                        </parameters>
                </method>
+               <method name="set" c:identifier="gir_test_struct_set">
+                       <return-value transfer-ownership="full">
+                               <type name="none" c:type="void"/>
+                       </return-value>
+                       <parameters>
+                               <instance-parameter name="self" transfer-ownership="none" nullable="1">
+                                       <type name="GirTest.Struct" c:type="GirTestStruct*"/>
+                               </instance-parameter>
+                               <parameter name="param1" transfer-ownership="none">
+                                       <type name="GirTest.Struct" c:type="GirTestStruct*"/>
+                               </parameter>
+                       </parameters>
+               </method>
        </record>
        <record name="SkippedStruct" c:type="GirTestSkippedStruct" c:symbol-prefix="skipped_struct" glib:type-name="GirTestSkippedStruct" glib:get-type="gir_test_skipped_struct_get_type" introspectable="0">
                <field name="field_name" writable="1">
                </return-value>
                <parameters>
                        <parameter name="param" transfer-ownership="none">
-                               <type name="GirTest.RenamedStruct" c:type="GirTestNamedStruct"/>
+                               <type name="GirTest.RenamedStruct" c:type="GirTestNamedStruct*"/>
                        </parameter>
                </parameters>
        </function>
index 1125bfa1cb9a75fca48be794b145f1f625adce57..191f01a1cfb01bef934c04c366bf95865bbe92ae 100644 (file)
@@ -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];
index b63aad4aa7711b1644e5a9e36b3d7434b4cdc6ad..681e4ab7d12895e851c95efb0f79eed0ebb0f6ce 100644 (file)
@@ -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 {
index d559fbea0d6e541481f2749abfffac2e5e3645d4..a3f579324a44c3f2db2c006dcf6fe3722a55a380 100644 (file)
@@ -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 {