Commit 20842807 authored by Jürg Billeter's avatar Jürg Billeter Committed by Jürg Billeter

call visit_end_full_expression for collection expression return array

2007-07-11  Juerg Billeter  <j@bitron.ch>

	* vala/valaforeachstatement.vala: call visit_end_full_expression for
	  collection expression
	* gobject/valacodegenerator.vala, gobject/valacodegeneratormethod.vala:
	  return array length if appropriate
	* tests/test-032.vala, tests/test-032.out: test array as return value
	* tests/Makefile.am: update

	Fixes bug 453676

svn path=/trunk/; revision=344
parent e41d9f6d
2007-07-11 Jürg Billeter <j@bitron.ch>
* vala/valaforeachstatement.vala: call visit_end_full_expression for
collection expression
* gobject/valacodegenerator.vala, gobject/valacodegeneratormethod.vala:
return array length if appropriate
* tests/test-032.vala, tests/test-032.out: test array as return value
* tests/Makefile.am: update
Fixes bug 453676
2007-07-11 Jürg Billeter <j@bitron.ch> 2007-07-11 Jürg Billeter <j@bitron.ch>
* vala/valasemanticanalyzer.vala: any reference or array type or pointer * vala/valasemanticanalyzer.vala: any reference or array type or pointer
......
...@@ -38,6 +38,7 @@ public class Vala.CodeGenerator : CodeVisitor { ...@@ -38,6 +38,7 @@ public class Vala.CodeGenerator : CodeVisitor {
Symbol current_symbol; Symbol current_symbol;
Symbol current_type_symbol; Symbol current_type_symbol;
Class current_class; Class current_class;
Method current_method;
TypeReference current_return_type; TypeReference current_return_type;
CCodeFragment header_begin; CCodeFragment header_begin;
...@@ -1398,7 +1399,28 @@ public class Vala.CodeGenerator : CodeVisitor { ...@@ -1398,7 +1399,28 @@ public class Vala.CodeGenerator : CodeVisitor {
return_expression_symbol.active = false; return_expression_symbol.active = false;
} }
} }
// return array length if appropriate
if (current_method != null && !current_method.no_array_length && current_return_type.data_type is Array) {
var return_expr_decl = get_temp_variable_declarator (stmt.return_expression.static_type);
var ccomma = new CCodeCommaExpression ();
ccomma.append_expression (new CCodeAssignment (new CCodeIdentifier (return_expr_decl.name), (CCodeExpression) stmt.return_expression.ccodenode));
var arr = (Array) current_return_type.data_type;
for (int dim = 1; dim <= arr.rank; dim++) {
var len_l = new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, new CCodeIdentifier (get_array_length_cname ("result", dim)));
var len_r = get_array_length_cexpression (stmt.return_expression, dim);
ccomma.append_expression (new CCodeAssignment (len_l, len_r));
}
ccomma.append_expression (new CCodeIdentifier (return_expr_decl.name));
stmt.return_expression.ccodenode = ccomma;
stmt.return_expression.temp_vars.append (return_expr_decl);
}
create_local_free_expr (stmt.return_expression); create_local_free_expr (stmt.return_expression);
if (stmt.return_expression.static_type != null && if (stmt.return_expression.static_type != null &&
......
...@@ -25,7 +25,10 @@ using GLib; ...@@ -25,7 +25,10 @@ using GLib;
public class Vala.CodeGenerator { public class Vala.CodeGenerator {
public override void visit_method (Method! m) { public override void visit_method (Method! m) {
Method old_method = current_method;
TypeReference old_return_type = current_return_type;
current_symbol = m.symbol; current_symbol = m.symbol;
current_method = m;
current_return_type = m.return_type; current_return_type = m.return_type;
if (m is CreationMethod) { if (m is CreationMethod) {
...@@ -43,7 +46,8 @@ public class Vala.CodeGenerator { ...@@ -43,7 +46,8 @@ public class Vala.CodeGenerator {
} }
current_symbol = current_symbol.parent_symbol; current_symbol = current_symbol.parent_symbol;
current_return_type = null; current_method = current_method;
current_return_type = old_return_type;
if (current_type_symbol != null && current_type_symbol.node is Interface) { if (current_type_symbol != null && current_type_symbol.node is Interface) {
var iface = (Interface) current_type_symbol.node; var iface = (Interface) current_type_symbol.node;
...@@ -52,13 +56,6 @@ public class Vala.CodeGenerator { ...@@ -52,13 +56,6 @@ public class Vala.CodeGenerator {
} }
} }
if (current_symbol.parent_symbol != null &&
current_symbol.parent_symbol.node is Method) {
/* lambda expressions produce nested methods */
var up_method = (Method) current_symbol.parent_symbol.node;
current_return_type = up_method.return_type;
}
function = new CCodeFunction (m.get_real_cname (), m.return_type.get_cname ()); function = new CCodeFunction (m.get_real_cname (), m.return_type.get_cname ());
CCodeFunctionDeclarator vdeclarator = null; CCodeFunctionDeclarator vdeclarator = null;
...@@ -128,7 +125,20 @@ public class Vala.CodeGenerator { ...@@ -128,7 +125,20 @@ public class Vala.CodeGenerator {
vdeclarator.add_parameter ((CCodeFormalParameter) param.ccodenode); vdeclarator.add_parameter ((CCodeFormalParameter) param.ccodenode);
} }
} }
// return array length if appropriate
if (!m.no_array_length && m.return_type.data_type is Array) {
var arr = (Array) m.return_type.data_type;
for (int dim = 1; dim <= arr.rank; dim++) {
var cparam = new CCodeFormalParameter (get_array_length_cname ("result", dim), "int*");
function.add_parameter (cparam);
if (vdeclarator != null) {
vdeclarator.add_parameter (cparam);
}
}
}
if (m.instance && m.instance_last) { if (m.instance && m.instance_last) {
function.add_parameter (instance_param); function.add_parameter (instance_param);
} }
......
...@@ -34,6 +34,7 @@ TESTS = \ ...@@ -34,6 +34,7 @@ TESTS = \
test-029.vala \ test-029.vala \
test-030.vala \ test-030.vala \
test-031.vala \ test-031.vala \
test-032.vala \
$(NULL) $(NULL)
EXTRA_DIST = \ EXTRA_DIST = \
...@@ -100,4 +101,5 @@ EXTRA_DIST = \ ...@@ -100,4 +101,5 @@ EXTRA_DIST = \
test-029.out \ test-029.out \
test-030.out \ test-030.out \
test-031.out \ test-031.out \
test-032.out \
$(NULL) $(NULL)
Array Test: 1 2 3 4 5
using GLib;
class Maman.Bar {
public int[] foo_numbers () {
return new int[3] { 2, 3, 4 };
}
public void run () {
foreach (int i in foo_numbers ()) {
stdout.printf (" %d", i);
}
}
static int main (string[] args) {
stdout.printf ("Array Test: 1");
var bar = new Bar ();
bar.run ();
stdout.printf (" 5\n");
return 0;
}
}
/* valaforeachstatement.vala /* valaforeachstatement.vala
* *
* Copyright (C) 2006 Jürg Billeter * Copyright (C) 2006-2007 Jürg Billeter
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
...@@ -83,7 +83,10 @@ public class Vala.ForeachStatement : Statement { ...@@ -83,7 +83,10 @@ public class Vala.ForeachStatement : Statement {
visitor.visit_begin_foreach_statement (this); visitor.visit_begin_foreach_statement (this);
type_reference.accept (visitor); type_reference.accept (visitor);
collection.accept (visitor); collection.accept (visitor);
visitor.visit_end_full_expression (collection);
body.accept (visitor); body.accept (visitor);
visitor.visit_end_foreach_statement (this); visitor.visit_end_foreach_statement (this);
......
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