]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
girparser: Handle anonymous delegate not backed by virtual-method or signal
authorRico Tzschichholz <ricotz@ubuntu.com>
Thu, 14 Jan 2021 16:55:34 +0000 (17:55 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Thu, 14 Jan 2021 16:55:34 +0000 (17:55 +0100)
tests/Makefile.am
tests/gir/delegate-anonymous.test [new file with mode: 0644]
vala/valagirparser.vala

index bd4b19daa3e11dcaf68c9fa3f9bca358bfc95757..bca7145211c86ffb55019c7afa7b01a2bfd702a7 100644 (file)
@@ -696,6 +696,7 @@ TESTS = \
        gir/class.test \
        gir/constant.test \
        gir/delegate-alias-without-target.test \
+       gir/delegate-anonymous.test \
        gir/delegate-array-length-type.test \
        gir/delegate-closure-destroy-index-conflict.test \
        gir/delegate-error-pos.test \
diff --git a/tests/gir/delegate-anonymous.test b/tests/gir/delegate-anonymous.test
new file mode 100644 (file)
index 0000000..0c4fd16
--- /dev/null
@@ -0,0 +1,35 @@
+GIR
+
+Input:
+
+<record name="Foo" c:type="TestFoo">
+  <field name="bar_callback">
+    <callback name="bar_callback" throws="1">
+      <return-value transfer-ownership="none">
+        <type name="gpointer" c:type="gpointer"/>
+      </return-value>
+      <parameters>
+        <parameter name="func"
+                   transfer-ownership="none"
+                   closure="1">
+          <type name="GLib.SourceFunc" c:type="GSourceFunc"/>
+        </parameter>
+        <parameter name="user_data"
+                   transfer-ownership="none"
+                   nullable="1"
+                   allow-none="1">
+          <type name="gpointer" c:type="gpointer"/>
+        </parameter>
+      </parameters>
+    </callback>
+  </field>
+</record>
+
+Output:
+
+[CCode (cheader_filename = "test.h", has_type_id = false)]
+public struct Foo {
+       public weak Test.FooBarCallbackFunc bar_callback;
+}
+[CCode (cheader_filename = "test.h", has_target = false, has_typedef = false)]
+public delegate void* FooBarCallbackFunc (GLib.SourceFunc func) throws GLib.Error;
index 65de9ea85878ed642888545e2ffbe87fe867e6dc..cdea30f2ad81def1c85159805c7619094f1688d0 100644 (file)
@@ -1136,6 +1136,17 @@ public class Vala.GirParser : CodeVisitor {
                                                var d = ((DelegateType) field.variable_type).delegate_symbol;
                                                parser.process_virtual_method_field (this, d, parent.gtype_struct_for);
                                                merged = true;
+                                       } else if (field.variable_type is DelegateType) {
+                                               // anonymous delegate
+                                               var d = ((DelegateType) field.variable_type).delegate_symbol;
+                                               if (this.lookup (d.name).parent == this) {
+                                                       d.set_attribute_bool ("CCode", "has_typedef", false);
+                                                       if (d.has_target && !metadata.has_argument (ArgumentType.DELEGATE_TARGET)) {
+                                                               field.set_attribute_bool ("CCode", "delegate_target", false);
+                                                       }
+                                                       d.name = "%s%sFunc".printf (parent.symbol.name, Symbol.lower_case_to_camel_case (d.name));
+                                                       get_parent_namespace (this).add_delegate (d);
+                                               }
                                        } else if (field.variable_type is ArrayType) {
                                                Node array_length;
                                                if (metadata.has_argument (ArgumentType.ARRAY_LENGTH_FIELD)) {
@@ -1598,6 +1609,17 @@ public class Vala.GirParser : CodeVisitor {
                return sym is ObjectTypeSymbol || sym is Struct || sym is Namespace || sym is ErrorDomain || sym is Enum;
        }
 
+       static unowned Namespace get_parent_namespace (Node node) {
+               unowned Node? n = node.parent;
+               while (n != null) {
+                       if (n.symbol is Namespace) {
+                               return (Namespace) n.symbol;
+                       }
+                       n = n.parent;
+               }
+               assert_not_reached ();
+       }
+
        UnresolvedSymbol? parse_symbol_from_string (string symbol_string, SourceReference? source_reference = null) {
                UnresolvedSymbol? sym = null;
                foreach (unowned string s in symbol_string.split (".")) {