From: Jürg Billeter Date: Sat, 20 Mar 2010 20:12:28 +0000 (+0100) Subject: Avoid temporary variable on struct initialization X-Git-Tag: 0.8.0~133 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f29d9bcc65f1eef9886e8cdb62c1dc191f27c5db;p=thirdparty%2Fvala.git Avoid temporary variable on struct initialization --- diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala index 72b4aa2c9..32938d2ef 100644 --- a/codegen/valaccodebasemodule.vala +++ b/codegen/valaccodebasemodule.vala @@ -2170,6 +2170,13 @@ internal class Vala.CCodeBaseModule : CCodeModule { cfrag.append (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), get_variable_cname (local.name)), rhs))); } } else { + CCodeStatement post_stmt = null; + var st = local.variable_type.data_type as Struct; + if (st != null && !st.is_simple_type () && !local.variable_type.nullable && local.initializer is ObjectCreationExpression) { + post_stmt = new CCodeExpressionStatement (rhs); + rhs = null; + } + var cvar = new CCodeVariableDeclarator (get_variable_cname (local.name), rhs, local.variable_type.get_cdeclarator_suffix ()); if (rhs != null) { cvar.line = rhs.line; @@ -2185,6 +2192,10 @@ internal class Vala.CCodeBaseModule : CCodeModule { cvar.initializer = default_value_for_type (local.variable_type, true); cvar.init0 = true; } + + if (post_stmt != null) { + cfrag.append (post_stmt); + } } if (local.initializer != null && local.variable_type is ArrayType) { @@ -3943,10 +3954,16 @@ internal class Vala.CCodeBaseModule : CCodeModule { var st = expr.type_reference.data_type as Struct; if ((st != null && !st.is_simple_type ()) || expr.get_object_initializer ().size > 0) { // value-type initialization or object creation expression with object initializer - var temp_decl = get_temp_variable (expr.type_reference, false, expr); - temp_vars.add (temp_decl); - instance = get_variable_cexpression (get_variable_cname (temp_decl.name)); + var local = expr.parent_node as LocalVariable; + if (local != null && !local.variable_type.nullable) { + instance = get_variable_cexpression (get_variable_cname (local.name)); + } else { + var temp_decl = get_temp_variable (expr.type_reference, false, expr); + temp_vars.add (temp_decl); + + instance = get_variable_cexpression (get_variable_cname (temp_decl.name)); + } } if (expr.symbol_reference == null) { @@ -4131,8 +4148,12 @@ internal class Vala.CCodeBaseModule : CCodeModule { } else { assert (false); } - - if (instance != null) { + + var local = expr.parent_node as LocalVariable; + if (st != null && !st.is_simple_type () && local != null && !local.variable_type.nullable) { + // no comma expression necessary + expr.ccodenode = creation_expr; + } else if (instance != null) { var ccomma = new CCodeCommaExpression (); if (expr.type_reference.data_type is Struct) {