From: Rico Tzschichholz Date: Sat, 8 Jul 2017 11:15:50 +0000 (+0200) Subject: codegen: Use array_length_cexpr to support fixed-arrays for return-values X-Git-Tag: 0.39.1~67 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a948a3f3b8b3f3f187706b17422f96c410224805;p=thirdparty%2Fvala.git codegen: Use array_length_cexpr to support fixed-arrays for return-values Based on patch by David Lechner https://bugzilla.gnome.org/show_bug.cgi?id=784691 --- diff --git a/codegen/valaccodemethodcallmodule.vala b/codegen/valaccodemethodcallmodule.vala index 67a54c92b..c911b91f3 100644 --- a/codegen/valaccodemethodcallmodule.vala +++ b/codegen/valaccodemethodcallmodule.vala @@ -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")); } diff --git a/tests/Makefile.am b/tests/Makefile.am index b3e5f7cc8..7f5433292 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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 \ diff --git a/tests/gir/array-fixed-length.test b/tests/gir/array-fixed-length.test index 0d7e70ee5..d4ea0175d 100644 --- a/tests/gir/array-fixed-length.test +++ b/tests/gir/array-fixed-length.test @@ -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 index 000000000..7f11eee43 --- /dev/null +++ b/tests/methods/bug784691.vala @@ -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); +} diff --git a/vala/valagirparser.vala b/vala/valagirparser.vala index f030c08ca..177339a67 100644 --- a/vala/valagirparser.vala +++ b/vala/valagirparser.vala @@ -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"]);