diff options
author | Rico Tzschichholz <ricotz@ubuntu.com> | 2020-11-13 17:15:37 +0100 |
---|---|---|
committer | Rico Tzschichholz <ricotz@ubuntu.com> | 2020-11-14 15:57:06 +0100 |
commit | 11660b4ea676e6c576b14f657293fd86dfa4ad98 (patch) | |
tree | 2794e2157a7d000ddcab7cf62d7264d2810c6c1f | |
parent | 4769941b14462ec366ef0b5a5cb934254d20ee02 (diff) | |
download | vala-11660b4ea676e6c576b14f657293fd86dfa4ad98.tar.gz |
codegen: Improve GValueModule.visit_cast_expression()
Re-use the result of the GValue-getter invocation
-rw-r--r-- | codegen/valagvaluemodule.vala | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/codegen/valagvaluemodule.vala b/codegen/valagvaluemodule.vala index a6109fd40..0429085e6 100644 --- a/codegen/valagvaluemodule.vala +++ b/codegen/valagvaluemodule.vala @@ -44,29 +44,41 @@ public class Vala.GValueModule : GAsyncModule { } ccall.add_argument (gvalue); - CCodeExpression rv = ccall; + CCodeExpression rv; + if (target_type is ArrayType) { + var temp_var = get_temp_variable (target_type, true, expr, false); + emit_temp_var (temp_var); + var temp_ref = get_variable_cexpression (temp_var.name); + ccode.add_assignment (temp_ref, ccall); + rv = temp_ref; - if (expr != null && target_type is ArrayType) { // null-terminated string array var len_call = new CCodeFunctionCall (new CCodeIdentifier ("g_strv_length")); len_call.add_argument (rv); append_array_length (expr, len_call); } else if (target_type is StructValueType) { - CodeNode node = expr != null ? (CodeNode) expr : target_type; - var temp_value = create_temp_value (target_type, true, node, true); + var temp_var = get_temp_variable (new PointerType (target_type), true, expr, false); + emit_temp_var (temp_var); + var temp_ref = get_variable_cexpression (temp_var.name); + ccode.add_assignment (temp_ref, ccall); + rv = temp_ref; + + // default value to fallback to + var temp_value = create_temp_value (target_type, true, expr, true); var ctemp = get_cvalue_ (temp_value); - rv = new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, new CCodeCastExpression (rv, get_ccode_name (new PointerType (target_type)))); var holds = new CCodeFunctionCall (new CCodeIdentifier ("G_VALUE_HOLDS")); holds.add_argument (gvalue); holds.add_argument (new CCodeIdentifier (get_ccode_type_id (target_type))); - var cond = new CCodeBinaryExpression (CCodeBinaryOperator.AND, holds, ccall); + var cond = new CCodeBinaryExpression (CCodeBinaryOperator.AND, holds, rv); var warn = new CCodeFunctionCall (new CCodeIdentifier ("g_warning")); warn.add_argument (new CCodeConstant ("\"Invalid GValue unboxing (wrong type or NULL)\"")); var fail = new CCodeCommaExpression (); fail.append_expression (warn); fail.append_expression (ctemp); - rv = new CCodeConditionalExpression (cond, rv, fail); + rv = new CCodeConditionalExpression (cond, new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, rv), fail); + } else { + rv = ccall; } set_cvalue (expr, rv); |