From 6dde9f76c617f8b09083d21dc20ae4091d410d15 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 6 Jul 2021 14:40:20 +0300 Subject: [PATCH] cgen: reset g.tmp_count at the start of each fn (minimise v.c commit delta) --- vlib/v/gen/c/cgen.v | 8 +++++++- vlib/v/gen/c/fn.v | 7 ++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 175f75062f..98396b24c5 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -67,8 +67,9 @@ mut: file &ast.File fn_decl &ast.FnDecl // pointer to the FnDecl we are currently inside otherwise 0 last_fn_c_name string - tmp_count int // counter for unique tmp vars (_tmp1, tmp2 etc) + tmp_count int // counter for unique tmp vars (_tmp1, _tmp2 etc); resets at the start of each fn. tmp_count2 int // a separate tmp var counter for autofree fn calls + tmp_count_declarations int // counter for unique tmp names (_d1, _d2 etc); does NOT reset, used for C declarations is_assign_lhs bool // inside left part of assign expr (for array_set(), etc) discard_or_result bool // do not safe last ExprStmt of `or` block in tmp variable to defer ongoing expr usage is_void_expr_stmt bool // ExprStmt whos result is discarded @@ -962,6 +963,11 @@ pub fn (mut g Gen) new_tmp_var() string { return '_t$g.tmp_count' } +pub fn (mut g Gen) new_tmp_declaration_name() string { + g.tmp_count_declarations++ + return '_d$g.tmp_count_declarations' +} + pub fn (mut g Gen) current_tmp_var() string { return '_t$g.tmp_count' } diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index f0097c9829..4b2375e7e5 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -332,6 +332,11 @@ fn (mut g Gen) gen_fn_decl(node &ast.FnDecl, skip bool) { // we could be in an anon fn so save outer fn defer stmts prev_defer_stmts := g.defer_stmts g.defer_stmts = [] + ctmp := g.tmp_count + g.tmp_count = 0 + defer { + g.tmp_count = ctmp + } g.stmts(node.stmts) if node.is_noreturn { g.writeln('\twhile(1);') @@ -409,7 +414,7 @@ fn (mut g Gen) fn_args(args []ast.Param, is_variadic bool, scope &ast.Scope) ([] g.write('void') } for i, arg in args { - mut caname := if arg.name == '_' { g.new_tmp_var() } else { c_name(arg.name) } + mut caname := if arg.name == '_' { g.new_tmp_declaration_name() } else { c_name(arg.name) } typ := g.unwrap_generic(arg.typ) arg_type_sym := g.table.get_type_symbol(typ) mut arg_type_name := g.typ(typ) // util.no_dots(arg_type_sym.name)