autofree: use scopes to avoid dups

pull/6631/head
Alexander Medvednikov 2020-10-15 16:26:59 +02:00
parent df82ef6bc7
commit 0dfd51408e
2 changed files with 11 additions and 4 deletions

View File

@ -111,7 +111,7 @@ mut:
match_sumtype_syms []table.TypeSymbol match_sumtype_syms []table.TypeSymbol
// tmp_arg_vars_to_free []string // tmp_arg_vars_to_free []string
// autofree_pregen map[string]string // autofree_pregen map[string]string
autofree_tmp_vars []string // to avoid redefining the same tmp vars in a single function // autofree_tmp_vars []string // to avoid redefining the same tmp vars in a single function
called_fn_name string called_fn_name string
cur_mod string cur_mod string
is_js_call bool // for handling a special type arg #1 `json.decode(User, ...)` is_js_call bool // for handling a special type arg #1 `json.decode(User, ...)`

View File

@ -17,7 +17,7 @@ fn (mut g Gen) gen_fn_decl(it ast.FnDecl, skip bool) {
} }
if g.pref.autofree { if g.pref.autofree {
defer { defer {
g.autofree_tmp_vars = [] // g.autofree_tmp_vars = []
} }
} }
// if g.fileis('vweb.v') { // if g.fileis('vweb.v') {
@ -630,6 +630,7 @@ fn (mut g Gen) autofree_call_pregen(node ast.CallExpr) {
} }
free_tmp_arg_vars = false // set the flag to true only if we have at least one arg to free free_tmp_arg_vars = false // set the flag to true only if we have at least one arg to free
g.tmp_count2++ g.tmp_count2++
mut scope := g.file.scope.innermost(node.pos.pos)
for i, arg in node.args { for i, arg in node.args {
if !arg.is_tmp_autofree { if !arg.is_tmp_autofree {
continue continue
@ -645,12 +646,18 @@ fn (mut g Gen) autofree_call_pregen(node ast.CallExpr) {
// t := '_tt${g.tmp_count2}_arg_expr_${fn_name}_$i' // t := '_tt${g.tmp_count2}_arg_expr_${fn_name}_$i'
t := '_arg_expr_${fn_name}_$i' t := '_arg_expr_${fn_name}_$i'
// g.called_fn_name = name // g.called_fn_name = name
used := t in g.autofree_tmp_vars // used := t in g.autofree_tmp_vars
used := scope.known_var(t)
if used { if used {
g.write('$t = ') g.write('$t = ')
} else { } else {
g.write('string $t = ') g.write('string $t = ')
g.autofree_tmp_vars << t scope.register(t, ast.Var{
name: t
typ: table.string_type
is_arg: true // TODO hack so that it's not freed twice when out of scope. it's probably better to use one model
})
// g.autofree_tmp_vars << t
} }
g.expr(arg.expr) g.expr(arg.expr)
g.writeln(';// new af pre') g.writeln(';// new af pre')