From 19318110d95519a9b6aeaa90f00b2d83eaa395e1 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Mon, 25 Nov 2024 13:25:26 -0300 Subject: [PATCH] cgen: reduce code for returning and extra whitespaces on `return` (#22967) --- vlib/v/gen/c/cgen.v | 12 +++++++----- vlib/v/gen/c/comptime.v | 2 +- vlib/v/gen/c/fn.v | 2 +- vlib/v/gen/c/if.v | 2 +- vlib/v/gen/c/past_tmp_var.v | 3 +++ vlib/v/gen/c/str.v | 2 +- vlib/v/gen/c/str_intp.v | 2 +- .../gen/c/testdata/alias_char_ptr_to_ptr.c.must_have | 6 ++---- vlib/v/gen/c/testdata/if_else_return.c.must_have | 1 - 9 files changed, 17 insertions(+), 15 deletions(-) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 9b5346fdb8c99c..043bed1d096707 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -5515,9 +5515,10 @@ fn (mut g Gen) return_stmt(node ast.Return) { g.set_current_pos_as_last_stmt_pos() g.write_v_source_line_info_stmt(node) + old_inside_return := g.inside_return g.inside_return = true defer { - g.inside_return = false + g.inside_return = old_inside_return } if node.exprs.len > 0 { @@ -5896,8 +5897,10 @@ fn (mut g Gen) return_stmt(node ast.Return) { if expr is ast.Ident { g.returned_var_name = expr.name } + if !use_tmp_var && !g.is_builtin_mod { + use_tmp_var = expr is ast.CallExpr + } } - // free := g.is_autofree && !g.is_builtin_mod // node.exprs[0] is ast.CallExpr // Create a temporary variable for the return expression if use_tmp_var || !g.is_builtin_mod { // `return foo(a, b, c)` @@ -5906,11 +5909,10 @@ fn (mut g Gen) return_stmt(node ast.Return) { // Don't use a tmp var if a variable is simply returned: `return x` // Just in case of defer statements exists, that the return values cannot // be modified. - if use_tmp_var || !(node.exprs[0].is_literal() || node.exprs[0] is ast.Ident) { + if use_tmp_var { use_tmp_var = true g.write('${ret_typ} ${tmpvar} = ') } else { - use_tmp_var = false if !g.is_builtin_mod { g.autofree_scope_vars(node.pos.pos - 1, node.pos.line_nr, true) } @@ -6893,7 +6895,7 @@ fn (mut g Gen) write_types(symbols []&ast.TypeSymbol) { if variant in idxs { continue } - g.type_definitions.writeln('// | ${variant:4d} = ${g.styp(variant.idx_type()):-20s}') + g.type_definitions.writeln('// | ${variant:4d} = ${g.styp(variant.idx_type())}') idxs << variant } idxs.clear() diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index 4623968e822d49..4e734e34fcd99d 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -452,7 +452,7 @@ fn (mut g Gen) comptime_if(node ast.IfExpr) { g.defer_ifdef = '' g.writeln('#endif') if node.is_expr { - g.write('${line} ${tmp_var}') + g.write('${line}${tmp_var}') } } diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index af44903645f694..a70a3cfd13bdf8 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -1021,7 +1021,7 @@ fn (mut g Gen) call_expr(node ast.CallExpr) { } if node.is_return_used { // return value is used, so we need to write the unwrapped temporary var - g.write('\n ${cur_line} (*(${unwrapped_styp}*)${tmp_opt}.data)') + g.write('\n ${cur_line}(*(${unwrapped_styp}*)${tmp_opt}.data)') } else { g.write('\n ${cur_line}') } diff --git a/vlib/v/gen/c/if.v b/vlib/v/gen/c/if.v index d209e3a2348f1e..d820b5f27b4856 100644 --- a/vlib/v/gen/c/if.v +++ b/vlib/v/gen/c/if.v @@ -427,6 +427,6 @@ fn (mut g Gen) if_expr(node ast.IfExpr) { g.set_current_pos_as_last_stmt_pos() } g.empty_line = false - g.write('${cur_line} ${tmp}') + g.write('${cur_line}${tmp}') } } diff --git a/vlib/v/gen/c/past_tmp_var.v b/vlib/v/gen/c/past_tmp_var.v index 8c3555f2354722..a0b7f44d598e72 100644 --- a/vlib/v/gen/c/past_tmp_var.v +++ b/vlib/v/gen/c/past_tmp_var.v @@ -47,6 +47,9 @@ fn (mut g Gen) past_tmp_var_done(p &PastTmpVar) { } else { g.write(p.s) } + if g.inside_return { + g.write(' ') + } g.write(p.tmp_var) } } diff --git a/vlib/v/gen/c/str.v b/vlib/v/gen/c/str.v index 13a493e8bc80f6..6f275e0d3bd3d0 100644 --- a/vlib/v/gen/c/str.v +++ b/vlib/v/gen/c/str.v @@ -140,7 +140,7 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) { } else { g.expr(expr) } - g.write2('.data', ') ? _SLIT("Option(&nil)") : ') + g.write('.data) ? _SLIT("Option(&nil)") : ') } else { inside_interface_deref_old := g.inside_interface_deref g.inside_interface_deref = false diff --git a/vlib/v/gen/c/str_intp.v b/vlib/v/gen/c/str_intp.v index 178b5f3dcd6321..bd75ce00d524a4 100644 --- a/vlib/v/gen/c/str_intp.v +++ b/vlib/v/gen/c/str_intp.v @@ -256,7 +256,7 @@ fn (mut g Gen) string_inter_literal(node ast.StringInterLiteral) { } } } - g.write2(' str_intp(', node.vals.len.str()) + g.write2('str_intp(', node.vals.len.str()) g.write(', _MOV((StrIntpData[]){') for i, val in node.vals { mut escaped_val := cescape_nonascii(util.smart_quote(val, false)) diff --git a/vlib/v/gen/c/testdata/alias_char_ptr_to_ptr.c.must_have b/vlib/v/gen/c/testdata/alias_char_ptr_to_ptr.c.must_have index a5258371c1fde2..e4ea955d3d0da6 100644 --- a/vlib/v/gen/c/testdata/alias_char_ptr_to_ptr.c.must_have +++ b/vlib/v/gen/c/testdata/alias_char_ptr_to_ptr.c.must_have @@ -1,10 +1,8 @@ VV_LOCAL_SYMBOL string main__get_two(void) { char* s = main__return_text_two(); -string _t1 = cstring_to_vstring(s); -return _t1; +return cstring_to_vstring(s); } VV_LOCAL_SYMBOL string main__get_one(void) { main__ALcharptr s = main__return_text_one(); -string _t1 = cstring_to_vstring(s); -return _t1; +return cstring_to_vstring(s); } \ No newline at end of file diff --git a/vlib/v/gen/c/testdata/if_else_return.c.must_have b/vlib/v/gen/c/testdata/if_else_return.c.must_have index 03926cc7337b1d..4dfd5ec3ca7233 100644 --- a/vlib/v/gen/c/testdata/if_else_return.c.must_have +++ b/vlib/v/gen/c/testdata/if_else_return.c.must_have @@ -5,6 +5,5 @@ _result_ok(&(string[]) { s }, (_result*)(&_t2), sizeof(string)); } else { return (_result_string){ .is_error=true, .err=_v_error(_SLIT("empty")), .data={EMPTY_STRUCT_INITIALIZATION} }; } -_result_string _t1 = _t2; return _t1; } \ No newline at end of file