From f8a82080dfbc97f23709aa7d76c0e851fe9746b8 Mon Sep 17 00:00:00 2001 From: Rico Tzschichholz Date: Tue, 11 Oct 2016 11:05:23 +0200 Subject: codegen: Generate correct ccode for fixed-length array parameters https://bugzilla.gnome.org/show_bug.cgi?id=641308 --- codegen/valaccodearraymodule.vala | 14 +++++++++++--- codegen/valaccodemethodcallmodule.vala | 4 ++-- codegen/valaccodemethodmodule.vala | 2 +- tests/basic-types/arrays.vala | 18 ++++++++++++++++++ 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/codegen/valaccodearraymodule.vala b/codegen/valaccodearraymodule.vala index 2b78bbc7d..ea227f285 100644 --- a/codegen/valaccodearraymodule.vala +++ b/codegen/valaccodearraymodule.vala @@ -704,14 +704,22 @@ 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) { ctypename += "*"; } - var main_cparam = new CCodeParameter (get_variable_cname (param.name), ctypename); + if (array_type.inline_allocated) { + if (param.direction != ParameterDirection.IN) { + ctypename += "*"; + } else { + name += "[]"; + } + } - var array_type = (ArrayType) param.variable_type; + var main_cparam = new CCodeParameter (name, ctypename); generate_type_declaration (array_type.element_type, decl_space); @@ -720,7 +728,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 31e1839e6..f5b7f2d49 100644 --- a/codegen/valaccodemethodcallmodule.vala +++ b/codegen/valaccodemethodcallmodule.vala @@ -346,7 +346,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; @@ -409,7 +409,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 3a89c7e41..0086db509 100644 --- a/codegen/valaccodemethodmodule.vala +++ b/codegen/valaccodemethodmodule.vala @@ -592,7 +592,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 7685304a6..9b8e70b1b 100644 --- a/tests/basic-types/arrays.vala +++ b/tests/basic-types/arrays.vala @@ -198,6 +198,23 @@ void test_explicit_copying () { assert (a0[1] == a1[1]); } +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 test_fixed_array () { + int i[3] = { 1, 2, 3 }; + take_fixed_array (i); + int j[3]; + give_fixed_array (out j); + assert (j[1] == 4); +} + void main () { test_integer_array (); test_string_array (); @@ -210,4 +227,5 @@ void main () { test_generics_array (); test_void_array (); test_explicit_copying (); + test_fixed_array (); } -- cgit v1.2.1