From: Jürg Billeter Date: Sat, 20 Oct 2012 07:50:54 +0000 (+0200) Subject: codegen: Fix fields initialized by functions returning an array X-Git-Tag: 0.18.1~19 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=24a62c812dc10add23263e4ba8d22380d3e4983d;p=thirdparty%2Fvala.git codegen: Fix fields initialized by functions returning an array Fixes bug 686336. --- diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala index 8b2a35940..62d1d8a0a 100644 --- a/codegen/valaccodebasemodule.vala +++ b/codegen/valaccodebasemodule.vala @@ -1088,16 +1088,26 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator { ccode.add_assignment (lhs, rhs); - if (f.variable_type is ArrayType && get_ccode_array_length (f) && - f.initializer is ArrayCreationExpression) { + if (f.variable_type is ArrayType && get_ccode_array_length (f)) { var array_type = (ArrayType) f.variable_type; var field_value = get_field_cvalue (f, load_this_parameter ((TypeSymbol) f.parent_symbol)); - List sizes = ((ArrayCreationExpression) f.initializer).get_sizes (); - for (int dim = 1; dim <= array_type.rank; dim++) { - var array_len_lhs = get_array_length_cvalue (field_value, dim); - var size = sizes[dim - 1]; - ccode.add_assignment (array_len_lhs, get_cvalue (size)); + var glib_value = (GLibValue) f.initializer.target_value; + if (glib_value.array_length_cvalues != null) { + for (int dim = 1; dim <= array_type.rank; dim++) { + var array_len_lhs = get_array_length_cvalue (field_value, dim); + ccode.add_assignment (array_len_lhs, get_array_length_cvalue (glib_value, dim)); + } + } else if (glib_value.array_null_terminated) { + requires_array_length = true; + var len_call = new CCodeFunctionCall (new CCodeIdentifier ("_vala_array_length")); + len_call.add_argument (get_cvalue_ (glib_value)); + + ccode.add_assignment (get_array_length_cvalue (field_value, 1), len_call); + } else { + for (int dim = 1; dim <= array_type.rank; dim++) { + ccode.add_assignment (get_array_length_cvalue (field_value, dim), new CCodeConstant ("-1")); + } } if (array_type.rank == 1 && f.is_internal_symbol ()) { @@ -1267,16 +1277,26 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator { ccode.add_assignment (lhs, rhs); } - if (f.variable_type is ArrayType && get_ccode_array_length (f) && - f.initializer is ArrayCreationExpression) { + if (f.variable_type is ArrayType && get_ccode_array_length (f)) { var array_type = (ArrayType) f.variable_type; var field_value = get_field_cvalue (f, null); - List sizes = ((ArrayCreationExpression) f.initializer).get_sizes (); - for (int dim = 1; dim <= array_type.rank; dim++) { - var array_len_lhs = get_array_length_cvalue (field_value, dim); - var size = sizes[dim - 1]; - ccode.add_assignment (array_len_lhs, get_cvalue (size)); + var glib_value = (GLibValue) f.initializer.target_value; + if (glib_value.array_length_cvalues != null) { + for (int dim = 1; dim <= array_type.rank; dim++) { + var array_len_lhs = get_array_length_cvalue (field_value, dim); + ccode.add_assignment (array_len_lhs, get_array_length_cvalue (glib_value, dim)); + } + } else if (glib_value.array_null_terminated) { + requires_array_length = true; + var len_call = new CCodeFunctionCall (new CCodeIdentifier ("_vala_array_length")); + len_call.add_argument (get_cvalue_ (glib_value)); + + ccode.add_assignment (get_array_length_cvalue (field_value, 1), len_call); + } else { + for (int dim = 1; dim <= array_type.rank; dim++) { + ccode.add_assignment (get_array_length_cvalue (field_value, dim), new CCodeConstant ("-1")); + } } } } else { diff --git a/tests/Makefile.am b/tests/Makefile.am index c1a8296fd..13457eb45 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -32,6 +32,7 @@ TESTS = \ basic-types/bug652380.vala \ basic-types/bug655908.vala \ basic-types/bug659975.vala \ + basic-types/bug686336.vala \ namespaces.vala \ methods/lambda.vala \ methods/closures.vala \ diff --git a/tests/basic-types/bug686336.vala b/tests/basic-types/bug686336.vala new file mode 100644 index 000000000..3aceef842 --- /dev/null +++ b/tests/basic-types/bug686336.vala @@ -0,0 +1,25 @@ +class Foo { + static int[] static_bar = { 1, 2, 3 }; + static int[] static_baz = create_array (); + + int[] bar = { 1, 2, 3 }; + int[] baz = create_array (); + + static int[] create_array () { + return { 1, 2, 3 }; + } + + public void test () { + assert (static_bar.length == 3); + assert (static_baz.length == 3); + + assert (bar.length == 3); + assert (baz.length == 3); + } +} + +void main () { + var foo = new Foo (); + foo.test (); +} +