]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
girparser: Support glib:ref-func, glib:unref-func, copy-function and free-function
authorLorenz Wildberg <lorenz@wild-fisch.de>
Tue, 6 Jun 2023 09:00:51 +0000 (11:00 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Fri, 11 Aug 2023 12:32:30 +0000 (14:32 +0200)
tests/Makefile.am
tests/gir/class-ref-unref-function.gir [new file with mode: 0644]
tests/gir/class-ref-unref-function.vapi-expected [new file with mode: 0644]
tests/gir/record-copy-free-func.gir [new file with mode: 0644]
tests/gir/record-copy-free-func.vapi-expected [new file with mode: 0644]
tests/girwriter/girtest.vapigen-expected
vala/valagirparser.vala

index b072ba7e631965c60b6bbcce5d0792435ccb403c..f75a602e92ab829532bd3538f4204913e3f85e1a 100644 (file)
@@ -885,6 +885,7 @@ TESTS = \
        gir/async-sync-out.gir \
        gir/class.gir \
        gir/class-final.gir \
+       gir/class-ref-unref-function.gir \
        gir/constant.gir \
        gir/delegate-alias-without-target.gir \
        gir/delegate-anonymous.gir \
@@ -902,6 +903,7 @@ TESTS = \
        gir/parameter-nullable-out-simple-type.gir \
        gir/property-non-readable.gir \
        gir/signal-virtual.gir \
+       gir/record-copy-free-func.gir \
        gir/symbol-redefined.gir \
        gir/symbol-type-csuffix.gir \
        gir/symbol-without-name.gir \
diff --git a/tests/gir/class-ref-unref-function.gir b/tests/gir/class-ref-unref-function.gir
new file mode 100644 (file)
index 0000000..4d84930
--- /dev/null
@@ -0,0 +1,20 @@
+<?xml version="1.0"?>
+<repository version="1.2" xmlns="http://www.gtk.org/introspection/core/1.0" xmlns:c="http://www.gtk.org/introspection/c/1.0" xmlns:glib="http://www.gtk.org/introspection/glib/1.0">
+<include name="GObject" version="2.0"/>
+<include name="GLib" version="2.0"/>
+<include name="Gio" version="2.0"/>
+<package name="test"/>
+<c:include name="test.h"/>
+<namespace name="Test" version="1.0" shared-library="test" c:prefix="Test" c:identifier-prefixes="Test" c:symbol-prefixes="test">
+<class name="Foo" c:type="TestFoo" c:symbol-prefix="foo" glib:type-name="TestFoo" glib:get-type="test_foo_get_type" glib:type-struct="TestFooClass" glib:fundamental="1" glib:ref-func="foo_custom_ref_function" glib:unref-func="foo_custom_unref_function">
+  <field name="u" writable="1">
+    <type name="guint" c:type="guint"/>
+  </field>
+</class>
+<record name="FooClass" c:type="TestFooClass" glib:is-gtype-struct-for="FooClass">
+  <field name="parent_class" readable="0" private="1">
+    <type name="GObject.TypeClass" c:type="GTypeClass"/>
+  </field>
+</record>
+</namespace>
+</repository>
diff --git a/tests/gir/class-ref-unref-function.vapi-expected b/tests/gir/class-ref-unref-function.vapi-expected
new file mode 100644 (file)
index 0000000..4633301
--- /dev/null
@@ -0,0 +1,9 @@
+[CCode (cprefix = "Test", gir_namespace = "Test", gir_version = "1.0", lower_case_cprefix = "test_")]
+namespace Test {
+       [CCode (cheader_filename = "test.h", ref_function = "foo_custom_ref_function", type_id = "test_foo_get_type ()", unref_function = "foo_custom_unref_function")]
+       public class Foo {
+               public uint u;
+               [CCode (has_construct_function = false)]
+               protected Foo ();
+       }
+}
diff --git a/tests/gir/record-copy-free-func.gir b/tests/gir/record-copy-free-func.gir
new file mode 100644 (file)
index 0000000..4aa1e58
--- /dev/null
@@ -0,0 +1,15 @@
+<?xml version="1.0"?>
+<repository version="1.2" xmlns="http://www.gtk.org/introspection/core/1.0" xmlns:c="http://www.gtk.org/introspection/c/1.0" xmlns:glib="http://www.gtk.org/introspection/glib/1.0">
+<include name="GObject" version="2.0"/>
+<include name="GLib" version="2.0"/>
+<include name="Gio" version="2.0"/>
+<package name="test"/>
+<c:include name="test.h"/>
+<namespace name="Test" version="1.0" shared-library="test" c:prefix="Test" c:identifier-prefixes="Test" c:symbol-prefixes="test">
+<record name="Foo" c:type="TestFoo" c:symbol-prefix="foo" glib:type-name="TestFoo" glib:get-type="test_foo_get_type" copy-function="foo_custom_copy_func" free-function="foo_custom_free_func">
+  <field name="u" writable="1">
+    <type name="guint" c:type="guint"/>
+  </field>
+</record>
+</namespace>
+</repository>
diff --git a/tests/gir/record-copy-free-func.vapi-expected b/tests/gir/record-copy-free-func.vapi-expected
new file mode 100644 (file)
index 0000000..5377957
--- /dev/null
@@ -0,0 +1,8 @@
+[CCode (cprefix = "Test", gir_namespace = "Test", gir_version = "1.0", lower_case_cprefix = "test_")]
+namespace Test {
+       [CCode (cheader_filename = "test.h", copy_function = "foo_custom_copy_func", free_function = "foo_custom_free_func", type_id = "test_foo_get_type ()")]
+       [Compact]
+       public class Foo {
+               public uint u;
+       }
+}
index a3f579324a44c3f2db2c006dcf6fe3722a55a380..00ddb45034a8f7d369d40538f8f294a93a9899b1 100644 (file)
@@ -28,7 +28,7 @@ namespace GirTest {
                [CCode (has_construct_function = false)]
                public CompactClass ();
        }
-       [CCode (cheader_filename = "girtest.h", type_id = "gir_test_deprecated_class_test_get_type ()")]
+       [CCode (cheader_filename = "girtest.h", ref_function = "gir_test_deprecated_class_test_ref", type_id = "gir_test_deprecated_class_test_get_type ()", unref_function = "gir_test_deprecated_class_test_unref")]
        [Version (deprecated = true, deprecated_since = "0.1.2", since = "0.1.0")]
        public class DeprecatedClassTest {
                public int ref_count;
@@ -53,7 +53,7 @@ namespace GirTest {
                [NoAccessorMethod]
                public GLib.Type t_type { get; construct; }
        }
-       [CCode (cheader_filename = "girtest.h", type_id = "gir_test_generics_test_get_type ()")]
+       [CCode (cheader_filename = "girtest.h", ref_function = "gir_test_generics_test_ref", type_id = "gir_test_generics_test_get_type ()", unref_function = "gir_test_generics_test_unref")]
        public class GenericsTest {
                public int ref_count;
                [CCode (has_construct_function = false)]
@@ -142,7 +142,7 @@ namespace GirTest {
                public virtual signal void signal_with_default_handlder (int i1);
                public signal void some_signal (int param);
        }
-       [CCode (cheader_filename = "girtest.h", cname = "GirTestNamedClass", lower_case_csuffix = "namedclass", type_id = "gir_test_namedclass_get_type ()")]
+       [CCode (cheader_filename = "girtest.h", cname = "GirTestNamedClass", lower_case_csuffix = "namedclass", ref_function = "gir_test_namedclass_ref", type_id = "gir_test_namedclass_get_type ()", unref_function = "gir_test_namedclass_unref")]
        public class RenamedClass {
                public int ref_count;
                [CCode (has_construct_function = false)]
@@ -167,7 +167,7 @@ namespace GirTest {
                [CCode (has_construct_function = false)]
                public SubTypeTest ();
        }
-       [CCode (cheader_filename = "girtest.h", type_id = "gir_test_typetest_get_type ()")]
+       [CCode (cheader_filename = "girtest.h", ref_function = "gir_test_typetest_ref", type_id = "gir_test_typetest_get_type ()", unref_function = "gir_test_typetest_unref")]
        public class TypeTest {
                public int ref_count;
                [CCode (has_construct_function = false)]
index fa4337019783a44448ef4776346ed0d1272f63fd..3f5cc8424b89eef62ce3c6cf79efa0c07243d9e1 100644 (file)
@@ -3065,12 +3065,16 @@ public class Vala.GirParser : CodeVisitor {
 
                if (metadata.has_argument (ArgumentType.REF_FUNCTION)) {
                        cl.set_attribute_string ("CCode", "ref_function", metadata.get_string (ArgumentType.REF_FUNCTION));
+               } else if (reader.has_attribute ("glib:ref-func")) {
+                       cl.set_attribute_string ("CCode", "ref_function", reader.get_attribute ("glib:ref-func"));
                }
                if (metadata.has_argument (ArgumentType.REF_SINK_FUNCTION)) {
                        cl.set_attribute_string ("CCode", "ref_sink_function", metadata.get_string (ArgumentType.REF_SINK_FUNCTION));
                }
                if (metadata.has_argument (ArgumentType.UNREF_FUNCTION)) {
                        cl.set_attribute_string ("CCode", "unref_function", metadata.get_string (ArgumentType.UNREF_FUNCTION));
+               } else if (reader.has_attribute ("glib:unref-func")) {
+                       cl.set_attribute_string ("CCode", "unref_function", reader.get_attribute ("glib:unref-func"));
                }
                if (metadata.has_argument (ArgumentType.TYPE_PARAMETERS)) {
                        parse_type_parameters_from_string (cl, metadata.get_string (ArgumentType.TYPE_PARAMETERS), metadata.get_source_reference (ArgumentType.TYPE_PARAMETERS));
@@ -3709,18 +3713,26 @@ public class Vala.GirParser : CodeVisitor {
                }
                if (metadata.has_argument (ArgumentType.COPY_FUNCTION)) {
                        cl.set_attribute_string ("CCode", "copy_function", metadata.get_string (ArgumentType.COPY_FUNCTION));
+               } else if (reader.has_attribute ("copy-function")) {
+                       cl.set_attribute_string ("CCode", "copy_function", reader.get_attribute ("copy-function"));
                }
                if (metadata.has_argument (ArgumentType.FREE_FUNCTION)) {
                        cl.set_attribute_string ("CCode", "free_function", metadata.get_string (ArgumentType.FREE_FUNCTION));
+               } else if (reader.has_attribute ("free-function")) {
+                       cl.set_attribute_string ("CCode", "free_function", reader.get_attribute ("free-function"));
                }
                if (metadata.has_argument (ArgumentType.REF_FUNCTION)) {
                        cl.set_attribute_string ("CCode", "ref_function", metadata.get_string (ArgumentType.REF_FUNCTION));
+               } else if (reader.has_attribute ("glib:ref-func")) {
+                       cl.set_attribute_string ("CCode", "ref_function", reader.get_attribute ("glib:ref-func"));
                }
                if (metadata.has_argument (ArgumentType.REF_SINK_FUNCTION)) {
                        cl.set_attribute_string ("CCode", "ref_sink_function", metadata.get_string (ArgumentType.REF_SINK_FUNCTION));
                }
                if (metadata.has_argument (ArgumentType.UNREF_FUNCTION)) {
                        cl.set_attribute_string ("CCode", "unref_function", metadata.get_string (ArgumentType.UNREF_FUNCTION));
+               } else if (reader.has_attribute ("glib:unref-func")) {
+                       cl.set_attribute_string ("CCode", "unref_function", reader.get_attribute ("glib:unref-func"));
                }
 
                next ();