]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Use array_length_cexpr to support fixed-arrays for return-values
authorRico Tzschichholz <ricotz@ubuntu.com>
Sat, 8 Jul 2017 11:15:50 +0000 (13:15 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Fri, 15 Sep 2017 12:57:52 +0000 (14:57 +0200)
Based on patch by David Lechner

https://bugzilla.gnome.org/show_bug.cgi?id=784691

codegen/valaccodemethodcallmodule.vala
tests/Makefile.am
tests/gir/array-fixed-length.test
tests/methods/bug784691.vala [new file with mode: 0644]
vala/valagirparser.vala

index 67a54c92b10cfebe68bfd633e4053a0ae82c09ce..c911b91f302056def464dd07313c45169422eab9 100644 (file)
@@ -518,6 +518,8 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
                                        out_arg_map.set (get_param_pos (get_ccode_array_length_pos (m) + 0.01 * dim), new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, temp_ref));
 
                                        append_array_length (expr, temp_ref);
+                               } else if (get_ccode_array_length_expr (m) != null) {
+                                       append_array_length (expr, new CCodeConstant (get_ccode_array_length_expr (m)));
                                } else {
                                        append_array_length (expr, new CCodeConstant ("-1"));
                                }
index b3e5f7cc85cdfcb14c2f6a4c0f9a99a3ea0c915f..7f54332924573f96333482bbef33a7586a42655e 100644 (file)
@@ -96,6 +96,7 @@ TESTS = \
        methods/bug774060.vala \
        methods/bug775466.test \
        methods/bug781061.vala \
+       methods/bug784691.vala \
        methods/generics.vala \
        methods/printf-invalid.test \
        methods/printf-constructor.vala \
index 0d7e70ee5059a201e56c69f1788caf86bc272e88..d4ea0175d6cbdfa150f714b44873324cf59b35e8 100644 (file)
@@ -62,7 +62,7 @@ Output:
 public static void change_array ([CCode (array_length = false)] ref uint8 array[2]);
 [CCode (cheader_filename = "test.h")]
 public static void get_array_out ([CCode (array_length = false)] out uint8 array[8]);
-[CCode (array_length = false, cheader_filename = "test.h")]
+[CCode (array_length = false, array_length_cexpr = "16", cheader_filename = "test.h")]
 public static unowned uint8[] get_array_return ();
 [CCode (cheader_filename = "test.h")]
 public static void set_array ([CCode (array_length = false)] uint8 array[4]);
diff --git a/tests/methods/bug784691.vala b/tests/methods/bug784691.vala
new file mode 100644 (file)
index 0000000..7f11eee
--- /dev/null
@@ -0,0 +1,11 @@
+const int[] FOO = { 0, 1, 2 };
+
+[CCode (array_length = false, array_length_cexpr = "3")]
+unowned int[] get_foo () {
+       assert (FOO.length == 3);
+       return FOO;
+}
+
+void main () {
+       assert (get_foo ().length == 3);
+}
index f030c08cae7f3710f7529ba34683e3206f8160ba..177339a67d886f4c16e8058ab3835cbeffd9d5e6 100644 (file)
@@ -2411,13 +2411,6 @@ public class Vala.GirParser : CodeVisitor {
                }
                type = element_get_type (type, true, ref no_array_length, ref array_null_terminated);
 
-               // FIXME No support for fixed-size array as return-value
-               var array_type = type as ArrayType;
-               if (array_type != null && array_type.fixed_length) {
-                       array_type.fixed_length = false;
-                       array_type.length = null;
-               }
-
                end_element ("return-value");
                return type;
        }
@@ -3098,6 +3091,14 @@ public class Vala.GirParser : CodeVisitor {
                s.comment = comment;
                s.external = true;
 
+               // Transform fixed-array properties of return-type into ccode-attribute
+               var array_type = return_type as ArrayType;
+               if (array_type != null && array_type.fixed_length) {
+                       s.set_attribute_string ("CCode", "array_length_cexpr", ((IntegerLiteral) array_type.length).value);
+                       array_type.fixed_length = false;
+                       array_type.length = null;
+               }
+
                if (s is Signal) {
                        if (current.girdata["name"] != name.replace ("_", "-")) {
                                s.set_attribute_string ("CCode", "cname", current.girdata["name"]);