parser/cgen: autofree - move branch_parent_pos to cgen.

pull/7162/head
joe-conigliaro 2020-12-06 18:02:36 +11:00
parent d7c057840a
commit 7733834751
No known key found for this signature in database
GPG Key ID: C12F7136C08206F1
4 changed files with 8 additions and 12 deletions

View File

@ -286,10 +286,9 @@ pub mut:
// break, continue
pub struct BranchStmt {
pub:
kind token.Kind
label string
pos token.Position
parent_pos int
kind token.Kind
label string
pos token.Position
}
pub struct CallExpr {

View File

@ -126,6 +126,7 @@ mut:
// sum type deref needs to know which index to deref because unions take care of the correct field
aggregate_type_idx int
returned_var_name string // to detect that a var doesn't need to be freed since it's being returned
branch_parent_pos int // used in BranchStmt (continue/break) for autofree stop position
}
const (
@ -853,7 +854,7 @@ fn (mut g Gen) stmt(node ast.Stmt) {
if g.pref.autofree && !g.is_builtin_mod {
g.writeln('// free before continue/break')
g.autofree_scope_vars_stop(node.pos.pos - 1, node.pos.line_nr, true,
node.parent_pos)
g.branch_parent_pos)
}
g.writeln('$node.kind;')
}
@ -1008,6 +1009,8 @@ fn (mut g Gen) stmt(node ast.Stmt) {
g.for_in(node)
}
ast.ForStmt {
prev_branch_parent_pos := g.branch_parent_pos
g.branch_parent_pos = node.pos.pos
g.write_v_source_line_info(node.pos)
g.is_vlines_enabled = false
if node.label.len > 0 {
@ -1031,6 +1034,7 @@ fn (mut g Gen) stmt(node ast.Stmt) {
if node.label.len > 0 {
g.writeln('${node.label}__break: {}')
}
g.branch_parent_pos = prev_branch_parent_pos
}
ast.GlobalDecl {
g.global_decl(node)

View File

@ -9,11 +9,6 @@ import v.table
fn (mut p Parser) for_stmt() ast.Stmt {
p.check(.key_for)
pos := p.tok.position()
prev_branch_parent_pos := p.branch_parent_pos
p.branch_parent_pos = pos.pos
defer {
p.branch_parent_pos = prev_branch_parent_pos
}
p.open_scope()
p.inside_for = true
if p.tok.kind == .key_match {

View File

@ -60,7 +60,6 @@ mut:
vet_errors []string
cur_fn_name string
in_generic_params bool // indicates if parsing between `<` and `>` of a method/function
branch_parent_pos int // used in BranchStmt (continue/break) autofree stop position
}
// for tests
@ -691,7 +690,6 @@ pub fn (mut p Parser) stmt(is_top_level bool) ast.Stmt {
kind: tok.kind
label: label
pos: tok.position()
parent_pos: p.branch_parent_pos
}
}
.key_unsafe {