Commit 2c1bf578 authored by Jürg Billeter's avatar Jürg Billeter Committed by Jürg Billeter

Support initializers for multi-dimensional arrays, fixes bug 541256

2009-01-04  Jürg Billeter  <j@bitron.ch>

	* vala/valaarraycreationexpression.vala:
	* vala/valainitializerlist.vala:
	* gobject/valaccodearraymodule.vala:

	Support initializers for multi-dimensional arrays, fixes bug 541256

svn path=/trunk/; revision=2272
parent ea9ca5af
2009-01-04 Jürg Billeter <j@bitron.ch>
* vala/valaarraycreationexpression.vala:
* vala/valainitializerlist.vala:
* gobject/valaccodearraymodule.vala:
Support initializers for multi-dimensional arrays, fixes bug 541256
2009-01-04 Jürg Billeter <j@bitron.ch>
* vapi/curses.vapi:
......
......@@ -32,6 +32,17 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
base (codegen, next);
}
void append_initializer_list (CCodeCommaExpression ce, CCodeExpression name_cnode, InitializerList initializer_list, int rank, ref int i) {
foreach (Expression e in initializer_list.get_initializers ()) {
if (rank > 1) {
append_initializer_list (ce, name_cnode, (InitializerList) e, rank - 1, ref i);
} else {
ce.append_expression (new CCodeAssignment (new CCodeElementAccess (name_cnode, new CCodeConstant (i.to_string ())), (CCodeExpression) e.ccodenode));
i++;
}
}
}
public override void visit_array_creation_expression (ArrayCreationExpression expr) {
expr.accept_children (codegen);
......@@ -70,12 +81,6 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
gnew.add_argument (cexpr);
if (expr.initializer_list != null) {
// FIXME rank > 1 not supported yet
if (expr.rank > 1) {
expr.error = true;
Report.error (expr.source_reference, "Creating arrays with rank greater than 1 with initializers is not supported yet");
}
var ce = new CCodeCommaExpression ();
var temp_var = get_temp_variable (expr.value_type, true, expr);
var name_cnode = new CCodeIdentifier (temp_var.name);
......@@ -84,11 +89,8 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
temp_vars.insert (0, temp_var);
ce.append_expression (new CCodeAssignment (name_cnode, gnew));
foreach (Expression e in expr.initializer_list.get_initializers ()) {
ce.append_expression (new CCodeAssignment (new CCodeElementAccess (name_cnode, new CCodeConstant (i.to_string ())), (CCodeExpression) e.ccodenode));
i++;
}
append_initializer_list (ce, name_cnode, expr.initializer_list, expr.rank, ref i);
ce.append_expression (name_cnode);
......
......@@ -118,9 +118,12 @@ public class Vala.ArrayCreationExpression : Expression {
}
private int create_sizes_from_initializer_list (SemanticAnalyzer analyzer, InitializerList il, int rank, Gee.List<Literal> sl) {
var init = new IntegerLiteral (il.size.to_string (), il.source_reference);
init.check (analyzer);
sl.add (init);
if (sl.size == (this.rank - rank)) {
// only add size if this is the first initializer list of the current dimension
var init = new IntegerLiteral (il.size.to_string (), il.source_reference);
init.check (analyzer);
sl.add (init);
}
int subsize = -1;
foreach (Expression e in il.get_initializers ()) {
......
......@@ -110,7 +110,8 @@ public class Vala.InitializerList : Expression {
var array_type = (ArrayType) target_type;
if (!(parent_node is ArrayCreationExpression)
&& !(parent_node is Constant)) {
&& !(parent_node is Constant)
&& !(parent_node is InitializerList)) {
// transform shorthand form
// int[] array = { 42 };
// into
......@@ -126,8 +127,18 @@ public class Vala.InitializerList : Expression {
return array_creation.check (analyzer);
}
DataType inner_target_type;
if (array_type.rank > 1) {
// allow initialization of multi-dimensional arrays
var inner_array_type = (ArrayType) array_type.copy ();
inner_array_type.rank--;
inner_target_type = inner_array_type;
} else {
inner_target_type = array_type.element_type.copy ();
}
foreach (Expression e in get_initializers ()) {
e.target_type = array_type.element_type.copy ();
e.target_type = inner_target_type;
}
} else if (target_type.data_type is Struct) {
/* initializer is used as struct initializer */
......
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