Commit aef2d1d7 authored by Jürg Billeter's avatar Jürg Billeter
Browse files

codegen: Fix fields initialized by functions returning an array

Fixes bug 686336.
parent 72da4d1c
......@@ -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<Expression> 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<Expression> 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 {
......
......@@ -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 \
......
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 ();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment