]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
girparser: Improve evalution of instance-parameter information
authorRico Tzschichholz <ricotz@ubuntu.com>
Mon, 26 Aug 2019 11:20:18 +0000 (13:20 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Wed, 28 Aug 2019 06:39:58 +0000 (08:39 +0200)
See https://gitlab.gnome.org/GNOME/vala/issues/836

tests/Makefile.am
tests/gir/instance-parameter-owned.test [new file with mode: 0644]
vala/valagirparser.vala

index 877e71a3014a0ce9271a7d33353825407676cf02..a5bf21ec8dfc5c593cbb10df300f14859bab95dd 100644 (file)
@@ -514,6 +514,7 @@ TESTS = \
        gir/delegate-error-pos.test \
        gir/enum.test \
        gir/errordomain.test \
+       gir/instance-parameter-owned.test \
        gir/method-array-length-type.test \
        gir/parameter-array-length-type.test \
        gir/parameter-nullable-out-simple-type.test \
diff --git a/tests/gir/instance-parameter-owned.test b/tests/gir/instance-parameter-owned.test
new file mode 100644 (file)
index 0000000..27cf75d
--- /dev/null
@@ -0,0 +1,50 @@
+GIR
+
+Input:
+
+<record name="Foo"
+        c:type="TestFoo"
+        glib:type-name="TestFoo"
+        glib:get-type="test_foo_get_type"
+        c:symbol-prefix="foo">
+  <constructor name="new" c:identifier="test_foo_new">
+    <return-value transfer-ownership="full">
+      <type name="Transform" c:type="TestFoo*"/>
+    </return-value>
+  </constructor>
+  <method name="bar" c:identifier="test_foo_bar">
+    <return-value transfer-ownership="full">
+      <type name="Foo" c:type="TestFoo*"/>
+    </return-value>
+    <parameters>
+      <instance-parameter name="self"
+                          transfer-ownership="full">
+        <type name="Foo" c:type="TestFoo*"/>
+      </instance-parameter>
+    </parameters>
+  </method>
+  <method name="baz" c:identifier="test_foo_baz">
+    <return-value transfer-ownership="full">
+      <type name="none"/>
+    </return-value>
+    <parameters>
+      <instance-parameter name="self"
+                          transfer-ownership="full">
+        <type name="Foo" c:type="TestFoo*"/>
+      </instance-parameter>
+    </parameters>
+  </method>
+</record>
+
+Output:
+
+[CCode (cheader_filename = "test.h", copy_function = "g_boxed_copy", free_function = "g_boxed_free", type_id = "test_foo_get_type ()")]
+[Compact]
+public class Foo {
+       [CCode (has_construct_function = false)]
+       public Foo ();
+       [DestroysInstance]
+       public Test.Foo bar ();
+       [DestroysInstance]
+       public void baz ();
+}
index 0392a59f03ebb06bdb165ea3a3d5d13162e44678..57931ee501decabb5c2b3311364da3c5b749ac05 100644 (file)
@@ -3289,11 +3289,8 @@ public class Vala.GirParser : CodeVisitor {
                        while (current_token == MarkupTokenType.START_ELEMENT) {
                                current_parameter_idx++;
 
-                               if (reader.name == "instance-parameter" &&
-                                   !(symbol_type == "function" || symbol_type == "constructor")) {
-                                       skip_element ();
-                                       continue;
-                               }
+                               var is_instance_parameter = (reader.name == "instance-parameter"
+                                       && !(symbol_type == "function" || symbol_type == "constructor"));
 
                                if (instance_idx > -2 && instance_idx == current_parameter_idx) {
                                        skip_element ();
@@ -3311,6 +3308,23 @@ public class Vala.GirParser : CodeVisitor {
                                Comment? param_comment;
                                default_param_name = "arg%d".printf (parameters.size);
                                var param = parse_parameter (out array_length_idx, out closure_idx, out destroy_idx, out scope, out param_comment, default_param_name);
+
+                               if (is_instance_parameter) {
+                                       unowned Method? m = s as Method;
+                                       if (m != null) {
+                                               if (param.direction == ParameterDirection.IN) {
+                                                       if (param.variable_type.value_owned) {
+                                                               m.set_attribute ("DestroysInstance", true);
+                                                       }
+                                                       pop_metadata ();
+                                                       continue;
+                                               } else {
+                                                       //TODO can more be done here?
+                                                       m.binding = MemberBinding.STATIC;
+                                               }
+                                       }
+                               }
+
                                if (array_length_idx != -1) {
                                        if (instance_idx > -2 && instance_idx < array_length_idx) {
                                                array_length_idx--;