Commit 7a429688 authored by Jürg Billeter's avatar Jürg Billeter

codegen: Use builder API

parent d60501fc
......@@ -48,7 +48,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
var name_cnode = get_variable_cexpression (temp_var.name);
int i = 0;
temp_vars.add (temp_var);
emit_temp_var (temp_var);
append_initializer_list (ce, name_cnode, expr.initializer_list, expr.rank, ref i);
......@@ -80,7 +80,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
var name_cnode = get_variable_cexpression (temp_var.name);
size.ccodenode = name_cnode;
temp_vars.add (temp_var);
emit_temp_var (temp_var);
csize = new CCodeAssignment (name_cnode, csize);
}
......@@ -112,7 +112,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
var name_cnode = get_variable_cexpression (temp_var.name);
int i = 0;
temp_vars.add (temp_var);
emit_temp_var (temp_var);
ce.append_expression (new CCodeAssignment (name_cnode, gnew));
......@@ -428,15 +428,15 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
var len_var = get_temp_variable (int_type);
len_var.source_reference = expr.source_reference;
temp_vars.add (len_var);
emit_temp_var (len_var);
var slice_var = get_temp_variable (expr.value_type, true, expr);
temp_vars.add (slice_var);
emit_temp_var (slice_var);
if (!is_pure_ccode_expression (cstart)) {
// avoid double evaluation of start
var start_var = get_temp_variable (int_type);
temp_vars.add (start_var);
emit_temp_var (start_var);
var start_assignment = new CCodeAssignment (get_variable_cexpression (start_var.name), cstart);
ccomma.append_expression (start_assignment);
......@@ -674,7 +674,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
}
var decl = get_temp_variable (expression_type, false, node);
temp_vars.add (decl);
emit_temp_var (decl);
var ctemp = get_variable_cexpression (decl.name);
......@@ -752,14 +752,11 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
// definition
var block = new CCodeBlock ();
push_context (new EmitContext ());
push_function (function);
if (requires_copy (array_type.element_type)) {
push_context (new EmitContext ());
var cdecl = new CCodeDeclaration (array_type.get_cname ());
var cvardecl = new CCodeVariableDeclarator ("result");
cdecl.add_declarator (cvardecl);
var gnew = new CCodeFunctionCall (new CCodeIdentifier ("g_new0"));
gnew.add_argument (new CCodeIdentifier (array_type.element_type.get_cname ()));
......@@ -770,28 +767,19 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
}
gnew.add_argument (length_expr);
cvardecl.initializer = gnew;
block.add_statement (cdecl);
var idx_decl = new CCodeDeclaration ("int");
idx_decl.add_declarator (new CCodeVariableDeclarator ("i"));
block.add_statement (idx_decl);
var loop_body = new CCodeBlock ();
loop_body.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeElementAccess (new CCodeIdentifier ("result"), new CCodeIdentifier ("i")), get_ref_cexpression (array_type.element_type, new CCodeElementAccess (new CCodeIdentifier ("self"), new CCodeIdentifier ("i")), null, array_type))));
ccode.add_declaration (array_type.get_cname (), cvardecl);
ccode.add_expression (new CCodeAssignment (new CCodeIdentifier ("result"), gnew));
var cfor = new CCodeForStatement (new CCodeBinaryExpression (CCodeBinaryOperator.LESS_THAN, new CCodeIdentifier ("i"), new CCodeIdentifier ("length")), loop_body);
cfor.add_initializer (new CCodeAssignment (new CCodeIdentifier ("i"), new CCodeConstant ("0")));
cfor.add_iterator (new CCodeUnaryExpression (CCodeUnaryOperator.POSTFIX_INCREMENT, new CCodeIdentifier ("i")));
block.add_statement (cfor);
ccode.add_declaration ("int", new CCodeVariableDeclarator ("i"));
block.add_statement (new CCodeReturnStatement (new CCodeIdentifier ("result")));
ccode.open_for (new CCodeAssignment (new CCodeIdentifier ("i"), new CCodeConstant ("0")),
new CCodeBinaryExpression (CCodeBinaryOperator.LESS_THAN, new CCodeIdentifier ("i"), new CCodeIdentifier ("length")),
new CCodeUnaryExpression (CCodeUnaryOperator.POSTFIX_INCREMENT, new CCodeIdentifier ("i")));
var cfrag = new CCodeFragment ();
append_temp_decl (cfrag, temp_vars);
block.add_statement (cfrag);
ccode.add_expression (new CCodeAssignment (new CCodeElementAccess (new CCodeIdentifier ("result"), new CCodeIdentifier ("i")), get_ref_cexpression (array_type.element_type, new CCodeElementAccess (new CCodeIdentifier ("self"), new CCodeIdentifier ("i")), null, array_type)));
ccode.close ();
pop_context ();
ccode.add_return (new CCodeIdentifier ("result"));
} else {
var dup_call = new CCodeFunctionCall (new CCodeIdentifier ("g_memdup"));
dup_call.add_argument (new CCodeIdentifier ("self"));
......@@ -800,16 +788,16 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
sizeof_call.add_argument (new CCodeIdentifier (array_type.element_type.get_cname ()));
dup_call.add_argument (new CCodeBinaryExpression (CCodeBinaryOperator.MUL, new CCodeIdentifier ("length"), sizeof_call));
block.add_statement (new CCodeReturnStatement (dup_call));
ccode.add_return (dup_call);
}
// append to file
cfile.add_function_declaration (function);
function.block = block;
cfile.add_function (function);
pop_context ();
return dup_func;
}
......@@ -831,28 +819,18 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
// definition
var block = new CCodeBlock ();
push_context (new EmitContext ());
push_function (function);
if (requires_copy (array_type.element_type)) {
push_context (new EmitContext ());
var idx_decl = new CCodeDeclaration ("int");
idx_decl.add_declarator (new CCodeVariableDeclarator ("i"));
block.add_statement (idx_decl);
var loop_body = new CCodeBlock ();
loop_body.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeElementAccess (new CCodeIdentifier ("dest"), new CCodeIdentifier ("i")), get_ref_cexpression (array_type.element_type, new CCodeElementAccess (new CCodeIdentifier ("self"), new CCodeIdentifier ("i")), null, array_type))));
ccode.add_declaration ("int", new CCodeVariableDeclarator ("i"));
var cfor = new CCodeForStatement (new CCodeBinaryExpression (CCodeBinaryOperator.LESS_THAN, new CCodeIdentifier ("i"), new CCodeConstant ("%d".printf (array_type.length))), loop_body);
cfor.add_initializer (new CCodeAssignment (new CCodeIdentifier ("i"), new CCodeConstant ("0")));
cfor.add_iterator (new CCodeUnaryExpression (CCodeUnaryOperator.POSTFIX_INCREMENT, new CCodeIdentifier ("i")));
block.add_statement (cfor);
ccode.open_for (new CCodeAssignment (new CCodeIdentifier ("i"), new CCodeConstant ("0")),
new CCodeBinaryExpression (CCodeBinaryOperator.LESS_THAN, new CCodeIdentifier ("i"), new CCodeConstant ("%d".printf (array_type.length))),
new CCodeUnaryExpression (CCodeUnaryOperator.POSTFIX_INCREMENT, new CCodeIdentifier ("i")));
var cfrag = new CCodeFragment ();
append_temp_decl (cfrag, temp_vars);
block.add_statement (cfrag);
pop_context ();
ccode.add_expression (new CCodeAssignment (new CCodeElementAccess (new CCodeIdentifier ("dest"), new CCodeIdentifier ("i")), get_ref_cexpression (array_type.element_type, new CCodeElementAccess (new CCodeIdentifier ("self"), new CCodeIdentifier ("i")), null, array_type)));
} else {
cfile.add_include ("string.h");
......@@ -864,16 +842,16 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
sizeof_call.add_argument (new CCodeIdentifier (array_type.element_type.get_cname ()));
dup_call.add_argument (new CCodeBinaryExpression (CCodeBinaryOperator.MUL, new CCodeConstant ("%d".printf (array_type.length)), sizeof_call));
block.add_statement (new CCodeExpressionStatement (dup_call));
ccode.add_expression (dup_call);
}
// append to file
cfile.add_function_declaration (function);
function.block = block;
cfile.add_function (function);
pop_context ();
return dup_func;
}
......
......@@ -126,13 +126,13 @@ public class Vala.CCodeAssignmentModule : CCodeMemberAccessModule {
var lhs_value_type = assignment.left.value_type.copy ();
string lhs_temp_name = "_tmp%d_".printf (next_temp_var_id++);
var lhs_temp = new LocalVariable (lhs_value_type, "*" + lhs_temp_name);
temp_vars.add (lhs_temp);
emit_temp_var (lhs_temp);
outer_ccomma.append_expression (new CCodeAssignment (get_variable_cexpression (lhs_temp_name), new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, lhs)));
lhs = new CCodeParenthesizedExpression (new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, get_variable_cexpression (lhs_temp_name)));
}
var temp_decl = get_temp_variable (assignment.left.value_type, true, null, false);
temp_vars.add (temp_decl);
emit_temp_var (temp_decl);
ccomma.append_expression (new CCodeAssignment (get_variable_cexpression (temp_decl.name), rhs));
if (unref_old) {
/* unref old value */
......
This diff is collapsed.
This diff is collapsed.
......@@ -69,7 +69,7 @@ public class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
// instance expression has side-effects
// store in temp. variable
var temp_var = get_temp_variable (expr.inner.value_type, true, null, false);
temp_vars.add (temp_var);
emit_temp_var (temp_var);
var ctemp = get_variable_cexpression (temp_var.name);
inst = new CCodeAssignment (ctemp, pub_inst);
expr.inner.ccodenode = ctemp;
......@@ -264,7 +264,7 @@ public class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
var ccomma = new CCodeCommaExpression ();
var temp_var = get_temp_variable (expr.inner.target_type, true, null, false);
temp_vars.add (temp_var);
emit_temp_var (temp_var);
ccomma.append_expression (new CCodeAssignment (get_variable_cexpression (temp_var.name), pub_inst));
ccomma.append_expression (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, get_variable_cexpression (temp_var.name)));
......@@ -281,7 +281,7 @@ public class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
var ccomma = new CCodeCommaExpression ();
var temp_var = get_temp_variable (base_property.get_accessor.value_type);
var ctemp = get_variable_cexpression (temp_var.name);
temp_vars.add (temp_var);
emit_temp_var (temp_var);
ccall.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, ctemp));
ccomma.append_expression (ccall);
ccomma.append_expression (ctemp);
......@@ -292,7 +292,7 @@ public class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
for (int dim = 1; dim <= array_type.rank; dim++) {
var temp_var = get_temp_variable (int_type);
var ctemp = get_variable_cexpression (temp_var.name);
temp_vars.add (temp_var);
emit_temp_var (temp_var);
ccall.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, ctemp));
expr.append_array_size (ctemp);
}
......@@ -301,7 +301,7 @@ public class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
if (delegate_type != null && delegate_type.delegate_symbol.has_target) {
var temp_var = get_temp_variable (new PointerType (new VoidType ()));
var ctemp = get_variable_cexpression (temp_var.name);
temp_vars.add (temp_var);
emit_temp_var (temp_var);
ccall.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, ctemp));
expr.delegate_target = ctemp;
}
......@@ -332,7 +332,7 @@ public class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
var ccomma = new CCodeCommaExpression ();
var temp_var = get_temp_variable (expr.value_type);
var ctemp = get_variable_cexpression (temp_var.name);
temp_vars.add (temp_var);
emit_temp_var (temp_var);
ccall.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, ctemp));
ccall.add_argument (new CCodeConstant ("NULL"));
ccomma.append_expression (ccall);
......
......@@ -200,7 +200,7 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
var ccomma = new CCodeCommaExpression ();
var temp_var = get_temp_variable (ma.inner.target_type, true, null, false);
temp_vars.add (temp_var);
emit_temp_var (temp_var);
ccomma.append_expression (new CCodeAssignment (get_variable_cexpression (temp_var.name), instance));
ccomma.append_expression (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, get_variable_cexpression (temp_var.name)));
......@@ -353,14 +353,14 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
if (param.array_length_type != null) {
if (param.direction == ParameterDirection.OUT) {
var temp_array_length = get_temp_variable (new CType (param.array_length_type));
temp_vars.add (temp_array_length);
emit_temp_var (temp_array_length);
array_length_expr = new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier (temp_array_length.name));
var comma = new CCodeCommaExpression ();
LocalVariable? temp_result = null;
if (!(m.return_type is VoidType)) {
temp_result = get_temp_variable (m.return_type);
temp_vars.add (temp_result);
emit_temp_var (temp_result);
ccall_expr = new CCodeAssignment (get_variable_cexpression (temp_result.name), ccall_expr);
}
......@@ -430,7 +430,7 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
var ccomma = new CCodeCommaExpression ();
var temp_decl = get_temp_variable (arg.value_type, true, null, false);
temp_vars.add (temp_decl);
emit_temp_var (temp_decl);
ccomma.append_expression (new CCodeAssignment (get_variable_cexpression (temp_decl.name), cexpr));
cexpr = get_variable_cexpression (temp_decl.name);
......@@ -451,7 +451,7 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
var ccomma = new CCodeCommaExpression ();
var temp_var = get_temp_variable (param.variable_type, param.variable_type.value_owned);
temp_vars.add (temp_var);
emit_temp_var (temp_var);
cexpr = new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, get_variable_cexpression (temp_var.name));
if (param.direction == ParameterDirection.REF) {
......@@ -468,14 +468,14 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
ccomma.append_expression (ccall_expr);
} else {
ret_temp_var = get_temp_variable (itype.get_return_type (), true, null, false);
temp_vars.add (ret_temp_var);
emit_temp_var (ret_temp_var);
ccomma.append_expression (new CCodeAssignment (get_variable_cexpression (ret_temp_var.name), ccall_expr));
}
var cassign_comma = new CCodeCommaExpression ();
var assign_temp_var = get_temp_variable (unary.inner.value_type, unary.inner.value_type.value_owned, null, false);
temp_vars.add (assign_temp_var);
emit_temp_var (assign_temp_var);
cassign_comma.append_expression (new CCodeAssignment (get_variable_cexpression (assign_temp_var.name), transform_expression (get_variable_cexpression (temp_var.name), param.variable_type, unary.inner.value_type, arg)));
......@@ -538,7 +538,7 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
var temp_var = get_temp_variable (itype.get_return_type (), true, null, false);
var temp_ref = get_variable_cexpression (temp_var.name);
temp_vars.add (temp_var);
emit_temp_var (temp_var);
ccall_expr = new CCodeAssignment (temp_ref, ccall_expr);
......@@ -557,7 +557,7 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
}
var temp_ref = get_variable_cexpression (temp_var.name);
temp_vars.add (temp_var);
emit_temp_var (temp_var);
out_arg_map.set (get_param_pos (m.carray_length_parameter_position + 0.01 * dim), new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, temp_ref));
......@@ -573,7 +573,7 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
var temp_var = get_temp_variable (new PointerType (new VoidType ()));
var temp_ref = get_variable_cexpression (temp_var.name);
temp_vars.add (temp_var);
emit_temp_var (temp_var);
out_arg_map.set (get_param_pos (m.cdelegate_target_parameter_position), new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, temp_ref));
......@@ -583,7 +583,7 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
temp_var = get_temp_variable (new DelegateType ((Delegate) context.root.scope.lookup ("GLib").scope.lookup ("DestroyNotify")));
temp_ref = get_variable_cexpression (temp_var.name);
temp_vars.add (temp_var);
emit_temp_var (temp_var);
out_arg_map.set (get_param_pos (m.cdelegate_target_parameter_position + 0.01), new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, temp_ref));
......@@ -602,7 +602,7 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
var temp_var = get_temp_variable (itype.get_return_type (), true, null, false);
var temp_ref = get_variable_cexpression (temp_var.name);
temp_vars.add (temp_var);
emit_temp_var (temp_var);
ccall_expr = new CCodeAssignment (temp_ref, ccall_expr);
......@@ -615,7 +615,7 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
var temp_var = get_temp_variable (int_type);
var temp_ref = get_variable_cexpression (temp_var.name);
temp_vars.add (temp_var);
emit_temp_var (temp_var);
out_arg_map.set (get_param_pos (deleg.carray_length_parameter_position + 0.01 * dim), new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, temp_ref));
......@@ -631,7 +631,7 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
var temp_var = get_temp_variable (new PointerType (new VoidType ()));
var temp_ref = get_variable_cexpression (temp_var.name);
temp_vars.add (temp_var);
emit_temp_var (temp_var);
out_arg_map.set (get_param_pos (deleg.cdelegate_target_parameter_position), new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, temp_ref));
......@@ -688,7 +688,7 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
var temp_var = get_temp_variable (itype.get_return_type ());
var temp_ref = get_variable_cexpression (temp_var.name);
temp_vars.add (temp_var);
emit_temp_var (temp_var);
out_arg_map.set (get_param_pos (-3), new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, temp_ref));
......@@ -746,20 +746,13 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
}
if (expr.is_yield_expression) {
if (pre_statement_fragment == null) {
pre_statement_fragment = new CCodeFragment ();
}
// set state before calling async function to support immediate callbacks
int state = next_coroutine_state++;
state_switch_statement.add_statement (new CCodeCaseStatement (new CCodeConstant (state.to_string ())));
state_switch_statement.add_statement (new CCodeGotoStatement ("_state_%d".printf (state)));
pre_statement_fragment.append (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), "_state_"), new CCodeConstant (state.to_string ()))));
pre_statement_fragment.append (new CCodeExpressionStatement (async_call));
pre_statement_fragment.append (new CCodeReturnStatement (new CCodeConstant ("FALSE")));
pre_statement_fragment.append (new CCodeLabel ("_state_%d".printf (state)));
ccode.add_expression (new CCodeAssignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), "_state_"), new CCodeConstant (state.to_string ())));
ccode.add_expression (async_call);
ccode.add_return (new CCodeConstant ("FALSE"));
ccode.add_label ("_state_%d".printf (state));
}
if (m is ArrayResizeMethod) {
......@@ -771,7 +764,7 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
var temp_decl = get_temp_variable (int_type);
var temp_ref = get_variable_cexpression (temp_decl.name);
temp_vars.add (temp_decl);
emit_temp_var (temp_decl);
/* memset needs string.h */
cfile.add_include ("string.h");
......
This diff is collapsed.
......@@ -142,8 +142,8 @@ public class Vala.CCodeStructModule : CCodeBaseModule {
public override void visit_struct (Struct st) {
push_context (new EmitContext (st));
var old_instance_finalize_fragment = instance_finalize_fragment;
instance_finalize_fragment = new CCodeFragment ();
var old_instance_finalize_context = instance_finalize_context;
instance_finalize_context = new EmitContext ();
generate_struct_declaration (st, cfile);
......@@ -154,6 +154,8 @@ public class Vala.CCodeStructModule : CCodeBaseModule {
generate_struct_declaration (st, internal_header_file);
}
begin_struct_destroy_function (st);
st.accept_children (this);
if (context.profile == Profile.GOBJECT && !st.is_boolean_type () && !st.is_integer_type () && !st.is_floating_type ()) {
......@@ -166,7 +168,7 @@ public class Vala.CCodeStructModule : CCodeBaseModule {
add_struct_free_function (st);
}
instance_finalize_fragment = old_instance_finalize_fragment;
instance_finalize_context = old_instance_finalize_context;
pop_context ();
}
......@@ -294,15 +296,14 @@ public class Vala.CCodeStructModule : CCodeBaseModule {
}
}
append_temp_decl (cfrag, temp_vars);
temp_vars.clear ();
function.block = cblock;
cfile.add_function (function);
}
void add_struct_destroy_function (Struct st) {
void begin_struct_destroy_function (Struct st) {
push_context (instance_finalize_context);
var function = new CCodeFunction (st.get_destroy_function (), "void");
if (st.access == SymbolAccessibility.PRIVATE) {
function.modifiers = CCodeModifiers.STATIC;
......@@ -310,13 +311,13 @@ public class Vala.CCodeStructModule : CCodeBaseModule {
function.add_parameter (new CCodeFormalParameter ("self", st.get_cname () + "*"));
var cblock = new CCodeBlock ();
cblock.add_statement (instance_finalize_fragment);
push_function (function);
function.block = cblock;
pop_context ();
}
cfile.add_function (function);
void add_struct_destroy_function (Struct st) {
cfile.add_function (instance_finalize_context.ccode);
}
}
......@@ -38,7 +38,7 @@ public class Vala.DovaArrayModule : DovaMethodCallModule {
var name_cnode = new CCodeIdentifier (temp_var.name);
int i = 0;
temp_vars.add (temp_var);
emit_temp_var (temp_var);
append_initializer_list (ce, name_cnode, expr.initializer_list, ref i);
......
......@@ -102,13 +102,13 @@ public class Vala.DovaAssignmentModule : DovaMemberAccessModule {
var lhs_value_type = assignment.left.value_type.copy ();
string lhs_temp_name = "_tmp%d_".printf (next_temp_var_id++);
var lhs_temp = new LocalVariable (lhs_value_type, "*" + lhs_temp_name);
temp_vars.add (lhs_temp);
emit_temp_var (lhs_temp);
outer_ccomma.append_expression (new CCodeAssignment (get_variable_cexpression (lhs_temp_name), new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, lhs)));
lhs = new CCodeParenthesizedExpression (new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, get_variable_cexpression (lhs_temp_name)));
}
var temp_decl = get_temp_variable (assignment.left.value_type);
temp_vars.add (temp_decl);
emit_temp_var (temp_decl);
ccomma.append_expression (new CCodeAssignment (get_variable_cexpression (temp_decl.name), rhs));
if (unref_old) {
/* unref old value */
......
This diff is collapsed.
......@@ -22,51 +22,29 @@
public class Vala.DovaControlFlowModule : DovaMethodModule {
public override void visit_if_statement (IfStatement stmt) {
ccode.open_if ((CCodeExpression) stmt.condition.ccodenode);
stmt.true_statement.emit (this);
if (stmt.false_statement != null) {
stmt.false_statement.emit (this);
}
if (stmt.false_statement != null) {
stmt.ccodenode = new CCodeIfStatement ((CCodeExpression) stmt.condition.ccodenode, (CCodeStatement) stmt.true_statement.ccodenode, (CCodeStatement) stmt.false_statement.ccodenode);
} else {
stmt.ccodenode = new CCodeIfStatement ((CCodeExpression) stmt.condition.ccodenode, (CCodeStatement) stmt.true_statement.ccodenode);
ccode.add_else ();
stmt.false_statement.emit (this);
}
create_temp_decl (stmt, stmt.condition.temp_vars);
ccode.close ();
}
public override void visit_switch_statement (SwitchStatement stmt) {
foreach (SwitchSection section in stmt.get_sections ()) {
section.emit (this);
}
var cswitch = new CCodeSwitchStatement ((CCodeExpression) stmt.expression.ccodenode);
stmt.ccodenode = cswitch;
ccode.open_switch ((CCodeExpression) stmt.expression.ccodenode);
foreach (SwitchSection section in stmt.get_sections ()) {
if (section.has_default_label ()) {
cswitch.add_statement (new CCodeLabel ("default"));
var cdefaultblock = new CCodeBlock ();
cswitch.add_statement (cdefaultblock);
foreach (CodeNode default_stmt in section.get_statements ()) {
cdefaultblock.add_statement (default_stmt.ccodenode);
}
continue;
}
foreach (SwitchLabel label in section.get_labels ()) {
cswitch.add_statement (new CCodeCaseStatement ((CCodeExpression) label.expression.ccodenode));
}
var cblock = new CCodeBlock ();
cswitch.add_statement (cblock);
foreach (CodeNode body_stmt in section.get_statements ()) {
cblock.add_statement (body_stmt.ccodenode);
ccode.add_default ();
}
section.emit (this);
}
create_temp_decl (stmt, stmt.expression.temp_vars);
ccode.close ();
}
public override void visit_switch_label (SwitchLabel label) {
......@@ -74,25 +52,29 @@ public class Vala.DovaControlFlowModule : DovaMethodModule {
label.expression.emit (this);
visit_end_full_expression (label.expression);
ccode.add_case ((CCodeExpression) label.expression.ccodenode);
}
}
public override void visit_loop (Loop stmt) {
ccode.open_while (new CCodeConstant ("true"));
stmt.body.emit (this);
stmt.ccodenode = new CCodeWhileStatement (new CCodeConstant ("true"), (CCodeStatement) stmt.body.ccodenode);
ccode.close ();
}
public override void visit_break_statement (BreakStatement stmt) {
stmt.ccodenode = new CCodeBreakStatement ();
append_local_free (current_symbol, true);
create_local_free (stmt, true);
ccode.add_break ();
}
public override void visit_continue_statement (ContinueStatement stmt) {
stmt.ccodenode = new CCodeContinueStatement ();
append_local_free (current_symbol, true);
create_local_free (stmt, true);
ccode.add_continue ();
}
}
......@@ -27,67 +27,44 @@ public class Vala.DovaErrorModule : DovaDelegateModule {
private bool is_in_catch = false;
public override void visit_throw_statement (ThrowStatement stmt) {
var cfrag = new CCodeFragment ();
ccode.add_expression (new CCodeAssignment (new CCodeIdentifier ("dova_error"), (CCodeExpression) stmt.error_expression.ccodenode));
// method will fail
var cassign = new CCodeAssignment (new CCodeIdentifier ("dova_error"), (CCodeExpression) stmt.error_expression.ccodenode);
cfrag.append (new CCodeExpressionStatement (cassign));
add_simple_check (stmt, cfrag, true);
stmt.ccodenode = cfrag;
create_temp_decl (stmt, stmt.error_expression.temp_vars);
add_simple_check (stmt, true);
}
public virtual CCodeStatement return_with_exception () {
var cerror_block = new CCodeBlock ();
public void return_with_exception () {
// propagate error
// nothing to do
// free local variables
var free_frag = new CCodeFragment ();
append_local_free (current_symbol, free_frag, false);
cerror_block.add_statement (free_frag);
append_local_free (current_symbol, false);
if (current_method is CreationMethod && current_method.parent_symbol is Class) {
var cl = current_method.parent_symbol as Class;
var unref_call = new CCodeFunctionCall (new CCodeIdentifier (cl.get_unref_function ()));
unref_call.add_argument (new CCodeIdentifier ("this"));
cerror_block.add_statement (new CCodeExpressionStatement (unref_call));
cerror_block.add_statement (new CCodeReturnStatement ());
ccode.add_expression (unref_call);
ccode.add_return ();
} else if (current_return_type is VoidType) {
cerror_block.add_statement (new CCodeReturnStatement ());
ccode.add_return ();
} else {
cerror_block.add_statement (new CCodeReturnStatement (default_value_for_type (current_return_type, false)));
ccode.add_return (default_value_for_type (current_return_type, false));
}
return cerror_block;
}
CCodeStatement uncaught_error_statement (CCodeBlock? block = null, bool unexpected = false) {
var cerror_block = block;
if (cerror_block == null) {
cerror_block = new CCodeBlock ();
}
void uncaught_error_statement () {
// free local variables
var free_frag = new CCodeFragment ();
append_local_free (current_symbol, free_frag, false);
cerror_block.add_statement (free_frag);
append_local_free (current_symbol, false);
// TODO log uncaught error as critical warning
if (current_method is CreationMethod) {
cerror_block.add_statement (new CCodeReturnStatement ());
ccode.add_return ();
} else if (current_return_type is VoidType) {
cerror_block.add_statement (new CCodeReturnStatement ());
ccode.add_return ();
} else if (current_return_type != null) {
cerror_block.add_statement (new CCodeReturnStatement (default_value_for_type (current_return_type, false)));
ccode.add_return (default_value_for_type (current_return_type, false));
}
return cerror_block;
}
bool in_finally_block (CodeNode node) {
......@@ -102,17 +79,20 @@ public class Vala.DovaErrorModule : DovaDelegateModule {
return false;
}