]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
girparser: Create correct output in get_default_lower_case_suffix()
authorRico Tzschichholz <ricotz@ubuntu.com>
Wed, 27 Mar 2019 23:02:59 +0000 (00:02 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Wed, 27 Mar 2019 23:02:59 +0000 (00:02 +0100)
It must match the output of CCodeAttribute.get_default_lower_case_suffix()

Add metadata support to set lower_case_csuffix

See https://gitlab.gnome.org/GNOME/vala/issues/778

codegen/valaccodeattribute.vala
tests/Makefile.am
tests/gir/symbol-type-csuffix.test [new file with mode: 0644]
vala/valagirparser.vala

index 5615eb618c6511091d55a86b650296ee6933ac16..5728fb46995ba0347466ae36ffd9fff05377e3b0 100644 (file)
@@ -890,6 +890,7 @@ public class Vala.CCodeAttribute : AttributeCache {
                if (sym is ObjectTypeSymbol) {
                        var csuffix = Symbol.camel_case_to_lower_case (sym.name);
 
+                       // FIXME Code duplication with GirParser.Node.get_default_lower_case_suffix()
                        // remove underscores in some cases to avoid conflicts of type macros
                        if (csuffix.has_prefix ("type_")) {
                                csuffix = "type" + csuffix.substring ("type_".length);
index abd7cb85c1aea51d4877666b92c1de96df291810..0e1472ffbd8ccb2e8b07423e36baddc78c278454 100644 (file)
@@ -502,6 +502,7 @@ TESTS = \
        gir/parameter-array-length-type.test \
        gir/parameter-nullable-out-simple-type.test \
        gir/property-non-readable.test \
+       gir/symbol-type-csuffix.test \
        annotations/deprecated.vala \
        annotations/description.vala \
        annotations/noaccessormethod.test \
diff --git a/tests/gir/symbol-type-csuffix.test b/tests/gir/symbol-type-csuffix.test
new file mode 100644 (file)
index 0000000..8039d4a
--- /dev/null
@@ -0,0 +1,26 @@
+GIR
+
+Input:
+
+<class name="TypeFoo" c:type="TestTypeFoo" glib:type-name="TestTypeFoo" glib:get-type="test_foo_get_type" glib:type-struct="TypeFooClass" parent="GObject.Object">
+  <constructor name="new" c:identifier="test_type_foo_new">
+    <return-value transfer-ownership="full">
+      <type name="Test.TypeFoo" c:type="TestTypeFoo*"/>
+    </return-value>
+  </constructor>
+  <method name="bar" c:identifier="test_type_foo_bar">
+    <return-value transfer-ownership="none">
+      <type name="none"/>
+    </return-value>
+  </method>
+</class>
+
+Output:
+
+[CCode (cheader_filename = "test.h", type_id = "test_foo_get_type ()")]
+public class TypeFoo : GLib.Object {
+       [CCode (cname = "test_type_foo_new", has_construct_function = false)]
+       public TypeFoo ();
+       [CCode (cname = "test_type_foo_bar")]
+       public void bar ();
+}
index 0210f6fde001231373347f80dad13d84c9451c6e..0a93cecce65d883fea7d91ac00e05d9a10f7c18f 100644 (file)
@@ -72,6 +72,7 @@ public class Vala.GirParser : CodeVisitor {
                DESTROY,
                CPREFIX,
                LOWER_CASE_CPREFIX,
+               LOWER_CASE_CSUFFIX,
                ERRORDOMAIN,
                DESTROYS_INSTANCE,
                BASE_TYPE,
@@ -698,6 +699,9 @@ public class Vala.GirParser : CodeVisitor {
 
                public string get_lower_case_csuffix () {
                        var suffix = symbol.get_attribute_string ("CCode", "lower_case_csuffix");
+                       if (metadata.has_argument (ArgumentType.LOWER_CASE_CSUFFIX)) {
+                               suffix = metadata.get_string (ArgumentType.LOWER_CASE_CSUFFIX);
+                       }
 
                        // we can't rely on gir suffix if metadata changed the name
                        if (suffix == null && girdata != null && girdata["c:symbol-prefix"] != null && !metadata.has_argument (ArgumentType.NAME)) {
@@ -710,7 +714,19 @@ public class Vala.GirParser : CodeVisitor {
                }
 
                public string get_default_lower_case_csuffix () {
-                       return Symbol.camel_case_to_lower_case (name);
+                       var csuffix = Symbol.camel_case_to_lower_case (name);
+
+                       // FIXME Code duplication with CCodeAttribute.get_default_lower_case_suffix()
+                       // remove underscores in some cases to avoid conflicts of type macros
+                       if (csuffix.has_prefix ("type_")) {
+                               csuffix = "type" + csuffix.substring ("type_".length);
+                       } else if (csuffix.has_prefix ("is_")) {
+                               csuffix = "is" + csuffix.substring ("is_".length);
+                       }
+                       if (csuffix.has_suffix ("_class")) {
+                               csuffix = csuffix.substring (0, csuffix.length - "_class".length) + "class";
+                       }
+                       return csuffix;
                }
 
                public string get_cprefix () {