Skip to content

Commit

Permalink
cgen: reduce code for returning and extra whitespaces on return (vl…
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Nov 25, 2024
1 parent b801083 commit 1931811
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 15 deletions.
12 changes: 7 additions & 5 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)`
Expand All @@ -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)
}
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/gen/c/comptime.v
Original file line number Diff line number Diff line change
Expand Up @@ -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}')
}
}

Expand Down
2 changes: 1 addition & 1 deletion vlib/v/gen/c/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -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}')
}
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/gen/c/if.v
Original file line number Diff line number Diff line change
Expand Up @@ -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}')
}
}
3 changes: 3 additions & 0 deletions vlib/v/gen/c/past_tmp_var.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion vlib/v/gen/c/str.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/gen/c/str_intp.v
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 2 additions & 4 deletions vlib/v/gen/c/testdata/alias_char_ptr_to_ptr.c.must_have
Original file line number Diff line number Diff line change
@@ -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);
}
1 change: 0 additions & 1 deletion vlib/v/gen/c/testdata/if_else_return.c.must_have
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 1931811

Please sign in to comment.