]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Improve ccode for fixed-length array parameters
authorRico Tzschichholz <ricotz@ubuntu.com>
Tue, 11 Oct 2016 09:05:23 +0000 (11:05 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Sat, 27 Oct 2018 20:57:57 +0000 (22:57 +0200)
https://gitlab.gnome.org/GNOME/vala/issues/163

codegen/valaccodearraymodule.vala
codegen/valaccodemethodcallmodule.vala
codegen/valaccodemethodmodule.vala
tests/basic-types/arrays.vala

index 5d611122d485c95cb492808e4652374ddad6fef8..1730da7f6030f4e037ed5dd280947ee483e30fee 100644 (file)
@@ -723,14 +723,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);
 
@@ -739,7 +743,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);
index 4c24ba96983629623628c6b01d56f1421b9f6021..1bca80aee7830ee0b69670d8680efb13faee4b91 100644 (file)
@@ -364,7 +364,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;
@@ -430,7 +430,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) {
index 117e7a21d935dbf29bc0d938cf9805cfbae0c599..b536e749f9e9c74858db564bfb7f8e7a4298636b 100644 (file)
@@ -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) {
index b749bdcb27cd8788933b95245491f54b77d7b131..085f22f06533710483d6f34604eabc5382a2831e 100644 (file)
@@ -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 ();
 }