]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
girparser: Recognize error parameter before delegate target parameter
authorRico Tzschichholz <ricotz@ubuntu.com>
Thu, 10 Jan 2019 10:24:36 +0000 (11:24 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Thu, 10 Jan 2019 12:35:05 +0000 (13:35 +0100)
Fixes https://gitlab.gnome.org/GNOME/vala/issues/265

tests/Makefile.am
tests/gir/delegate-error-pos.test [new file with mode: 0644]
vala/valagirparser.vala

index 3144f9c4bd34cb7b5a13f3f3ac3581fb7a533080..364e3b32c6da0d672263b7467a9c9c86067e2e8f 100644 (file)
@@ -449,6 +449,7 @@ TESTS = \
        gir/class.test \
        gir/delegate-alias-without-target.test \
        gir/delegate-closure-destroy-index-conflict.test \
+       gir/delegate-error-pos.test \
        gir/enum.test \
        gir/errordomain.test \
        gir/parameter-nullable-out-simple-type.test \
diff --git a/tests/gir/delegate-error-pos.test b/tests/gir/delegate-error-pos.test
new file mode 100644 (file)
index 0000000..37f943d
--- /dev/null
@@ -0,0 +1,29 @@
+GIR
+
+Input:
+
+<callback name="Foo" c:type="TestFoo">
+  <return-value transfer-ownership="none">
+    <type name="none" c:type="void"/>
+  </return-value>
+  <parameters>
+    <parameter name="s" transfer-ownership="none">
+      <type name="utf8" c:type="const gchar*"/>
+    </parameter>
+    <parameter name="error" direction="out" transfer-ownership="full">
+      <type name="GLib.Error" c:type="GError**"/>
+    </parameter>
+    <parameter name="user_data"
+               transfer-ownership="none"
+               nullable="1"
+               allow-none="1"
+               closure="2">
+      <type name="gpointer" c:type="gpointer"/>
+    </parameter>
+  </parameters>
+</callback>
+
+Output:
+
+[CCode (cheader_filename = "test.h", error_pos = 1.8, instance_pos = 1.9)]
+public delegate void Foo (string s) throws GLib.Error;
index e522d302987f3537950bc745d93bdd91bf2f7e08..2aed50f2fe7c1fd9d1339861c1eb0854208dc11c 100644 (file)
@@ -3060,6 +3060,7 @@ public class Vala.GirParser : CodeVisitor {
                public int destroy_idx;
                public bool keep;
                public bool is_async;
+               public bool is_error;
        }
 
        void parse_function (string element_name) {
@@ -3201,6 +3202,7 @@ public class Vala.GirParser : CodeVisitor {
                                                ((Delegate) s).add_error_type (error_type);
                                        }
                                }
+                               throws_string = "1";
                        } else if (throws_string == "1") {
                                if (s is Method) {
                                        ((Method) s).add_error_type (new ErrorType (null, null));
@@ -3317,9 +3319,9 @@ public class Vala.GirParser : CodeVisitor {
                                }
 
                                var info = new ParameterInfo (param, array_length_idx, closure_idx, destroy_idx, scope == "async");
+                               unowned UnresolvedType? unresolved_type = param.variable_type as UnresolvedType;
 
                                if (s is Method && scope == "async") {
-                                       var unresolved_type = param.variable_type as UnresolvedType;
                                        if (unresolved_type != null && unresolved_type.unresolved_symbol.name == "AsyncReadyCallback") {
                                                // GAsync-style method
                                                ((Method) s).coroutine = true;
@@ -3327,6 +3329,14 @@ public class Vala.GirParser : CodeVisitor {
                                        }
                                }
 
+                               if (s is Delegate && throws_string != "1" && param.direction == ParameterDirection.OUT) {
+                                       if (unresolved_type != null && unresolved_type.unresolved_symbol.to_string () == "GLib.Error") {
+                                               ((Delegate) s).add_error_type (new ErrorType (null, null));
+                                               info.is_error = true;
+                                               info.keep = false;
+                                       }
+                               }
+
                                parameters.add (info);
                                pop_metadata ();
                        }
@@ -3896,6 +3906,11 @@ public class Vala.GirParser : CodeVisitor {
                                        s.set_attribute_double ("CCode", "async_result_pos", i + shift);
                                }
                        }
+                       if (s is Delegate && info.is_error) {
+                               if (!s.has_attribute_argument ("CCode", "instance_pos")) {
+                                       s.set_attribute_double ("CCode", "error_pos", j - 0.2);
+                               }
+                       }
                        i++;
                }