[vala/0.40] codegen: Improve ccode for fixed-length array parameters



commit f969792a8c5a66a0dc746370e09fa0147204035f
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Tue Oct 11 11:05:23 2016 +0200

    codegen: Improve ccode for fixed-length array parameters
    
    https://gitlab.gnome.org/GNOME/vala/issues/163

 codegen/valaccodearraymodule.vala      | 12 ++++++++----
 codegen/valaccodemethodcallmodule.vala |  4 ++--
 codegen/valaccodemethodmodule.vala     |  2 +-
 tests/basic-types/arrays.vala          | 32 ++++++++++++++++++++++++++++++++
 4 files changed, 43 insertions(+), 7 deletions(-)
---
diff --git a/codegen/valaccodearraymodule.vala b/codegen/valaccodearraymodule.vala
index 240d7851f..b99c45d38 100644
--- a/codegen/valaccodearraymodule.vala
+++ b/codegen/valaccodearraymodule.vala
@@ -711,14 +711,18 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
                }
 
                string ctypename = get_ccode_name (param.variable_type);
+               string name = get_variable_cname (param.name);
+               var array_type = (ArrayType) param.variable_type;
 
-               if (param.direction != ParameterDirection.IN) {
+               if (array_type.fixed_length) {
                        ctypename += "*";
                }
 
-               var main_cparam = new CCodeParameter (get_variable_cname (param.name), ctypename);
+               if (param.direction != ParameterDirection.IN) {
+                       ctypename += "*";
+               }
 
-               var array_type = (ArrayType) param.variable_type;
+               var main_cparam = new CCodeParameter (name, ctypename);
 
                generate_type_declaration (array_type.element_type, decl_space);
 
@@ -727,7 +731,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
                        carg_map.set (get_param_pos (get_ccode_pos (param)), get_variable_cexpression 
(param.name));
                }
 
-               if (get_ccode_array_length (param)) {
+               if (!array_type.fixed_length && get_ccode_array_length (param)) {
                        string length_ctype = "int";
                        if (get_ccode_array_length_type (param) != null) {
                                length_ctype = get_ccode_array_length_type (param);
diff --git a/codegen/valaccodemethodcallmodule.vala b/codegen/valaccodemethodcallmodule.vala
index eb65929bd..f94b4029c 100644
--- a/codegen/valaccodemethodcallmodule.vala
+++ b/codegen/valaccodemethodcallmodule.vala
@@ -358,7 +358,7 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
 
                                        var unary = arg as UnaryExpression;
                                        if (unary == null || unary.operator != UnaryOperator.OUT) {
-                                               if (get_ccode_array_length (param) && param.variable_type is 
ArrayType) {
+                                               if (get_ccode_array_length (param) && param.variable_type is 
ArrayType && !((ArrayType) param.variable_type).fixed_length) {
                                                        var array_type = (ArrayType) param.variable_type;
                                                        for (int dim = 1; dim <= array_type.rank; dim++) {
                                                                CCodeExpression? array_length_expr = null;
@@ -424,7 +424,7 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
 
                                                cexpr = new CCodeUnaryExpression 
(CCodeUnaryOperator.ADDRESS_OF, get_cvalue (arg));
 
-                                               if (get_ccode_array_length (param) && param.variable_type is 
ArrayType) {
+                                               if (get_ccode_array_length (param) && param.variable_type is 
ArrayType && !((ArrayType) param.variable_type).fixed_length) {
                                                        var array_type = (ArrayType) param.variable_type;
                                                        var array_length_type = int_type;
                                                        if (get_ccode_array_length_type (param) != null) {
diff --git a/codegen/valaccodemethodmodule.vala b/codegen/valaccodemethodmodule.vala
index baaf49ee0..373736d11 100644
--- a/codegen/valaccodemethodmodule.vala
+++ b/codegen/valaccodemethodmodule.vala
@@ -570,7 +570,7 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
                                                }
                                        } else if (!m.coroutine) {
                                                // declare local variable for out parameter to allow 
assignment even when caller passes NULL
-                                               var vardecl = new CCodeVariableDeclarator.zero 
(get_variable_cname ("_vala_%s".printf (param.name)), default_value_for_type (param.variable_type, true));
+                                               var vardecl = new CCodeVariableDeclarator.zero 
(get_variable_cname ("_vala_%s".printf (param.name)), default_value_for_type (param.variable_type, true), 
get_ccode_declarator_suffix (param.variable_type));
                                                ccode.add_declaration (get_ccode_name (param.variable_type), 
vardecl);
 
                                                if (param.variable_type is ArrayType) {
diff --git a/tests/basic-types/arrays.vala b/tests/basic-types/arrays.vala
index b749bdcb2..085f22f06 100644
--- a/tests/basic-types/arrays.vala
+++ b/tests/basic-types/arrays.vala
@@ -254,6 +254,37 @@ void test_struct_array () {
        assert (FOO_ARRAY_CONST[0].bar == 42);
 }
 
+void give_fixed_array (out int i[3]) {
+       i = { 3, 4, 5 };
+}
+
+void take_fixed_array (int i[3]) {
+       assert (i.length == 3);
+       assert (i[1] == 2);
+}
+
+void change_fixed_array (ref int i[3]) {
+       assert (i.length == 3);
+       //FIXME assert (i[1] == 7);
+       //FIXME i[1] = 9;
+}
+
+void test_fixed_array () {
+       int i[3] = { 1, 2, 3 };
+       assert (i.length == 3);
+       take_fixed_array (i);
+
+       int k[3] = { 6, 7, 8 };
+       change_fixed_array (ref k);
+       assert (k.length == 3);
+       //FIXME assert (k[1] == 9);
+
+       int j[3];
+       give_fixed_array (out j);
+       assert (j.length == 3);
+       //FIXME assert (j[1] == 4);
+}
+
 void main () {
        test_integer_array ();
        test_string_array ();
@@ -270,4 +301,5 @@ void main () {
        test_array_move ();
        test_array_resize ();
        test_struct_array ();
+       test_fixed_array ();
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]