autofree: simplify: merge tmp arg logic with scope vars
							parent
							
								
									507d724ee6
								
							
						
					
					
						commit
						1c257abc23
					
				| 
						 | 
				
			
			@ -110,9 +110,9 @@ pub fn mkdir(path string) ?bool {
 | 
			
		|||
	}
 | 
			
		||||
	*/
 | 
			
		||||
	apath := real_path(path)
 | 
			
		||||
	defer {
 | 
			
		||||
		apath.free()
 | 
			
		||||
	}
 | 
			
		||||
	//defer {
 | 
			
		||||
		//apath.free()
 | 
			
		||||
	//}
 | 
			
		||||
	/*
 | 
			
		||||
	$if linux {
 | 
			
		||||
		$if !android {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -324,7 +324,7 @@ pub:
 | 
			
		|||
	comments        []Comment
 | 
			
		||||
pub mut:
 | 
			
		||||
	typ             table.Type
 | 
			
		||||
	is_tmp_autofree bool
 | 
			
		||||
	is_tmp_autofree bool // this tells cgen that a tmp variable has to be used for the arg expression in order to free it after the call
 | 
			
		||||
	pos             token.Position
 | 
			
		||||
	// tmp_name        string // for autofree
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -119,7 +119,7 @@ mut:
 | 
			
		|||
	cur_mod               string
 | 
			
		||||
	is_js_call            bool // for handling a special type arg #1 `json.decode(User, ...)`
 | 
			
		||||
	// nr_vars_to_free       int
 | 
			
		||||
	doing_autofree_tmp    bool
 | 
			
		||||
	// doing_autofree_tmp    bool
 | 
			
		||||
	inside_lambda         bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -1047,8 +1047,8 @@ fn (mut g Gen) stmt(node ast.Stmt) {
 | 
			
		|||
	if g.pref.autofree {
 | 
			
		||||
		// if node is ast.ExprStmt {&& node.expr is ast.CallExpr {
 | 
			
		||||
		if node !is ast.FnDecl {
 | 
			
		||||
			p := node.position()
 | 
			
		||||
			g.autofree_call_postgen(p.pos)
 | 
			
		||||
			// p := node.position()
 | 
			
		||||
			// g.autofree_call_postgen(p.pos)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1999,9 +1999,9 @@ fn (mut g Gen) autofree_var_call(free_fn_name string, v ast.Var) {
 | 
			
		|||
		// tmp expr vars do not need to be freed again here
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	if v.is_autofree_tmp && !g.doing_autofree_tmp {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	// if v.is_autofree_tmp && !g.doing_autofree_tmp {
 | 
			
		||||
	// return
 | 
			
		||||
	// }
 | 
			
		||||
	if v.name.contains('expr_write_1_') {
 | 
			
		||||
		// TODO remove this temporary hack
 | 
			
		||||
		return
 | 
			
		||||
| 
						 | 
				
			
			@ -2009,7 +2009,7 @@ fn (mut g Gen) autofree_var_call(free_fn_name string, v ast.Var) {
 | 
			
		|||
	if v.typ.is_ptr() {
 | 
			
		||||
		g.writeln('\t${free_fn_name}(${c_name(v.name)}); // autofreed ptr var')
 | 
			
		||||
	} else {
 | 
			
		||||
		g.writeln('\t${free_fn_name}(&${c_name(v.name)}); // autofreed var $g.doing_autofree_tmp')
 | 
			
		||||
		g.writeln('\t${free_fn_name}(&${c_name(v.name)}); // autofreed var')
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -741,7 +741,7 @@ fn (mut g Gen) autofree_call_postgen(node_pos int) {
 | 
			
		|||
	if g.inside_vweb_tmpl {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	g.doing_autofree_tmp = true
 | 
			
		||||
	// g.doing_autofree_tmp = true
 | 
			
		||||
	// g.write('/* postgen */')
 | 
			
		||||
	scope := g.file.scope.innermost(node_pos)
 | 
			
		||||
	for _, obj in scope.objects {
 | 
			
		||||
| 
						 | 
				
			
			@ -772,7 +772,7 @@ fn (mut g Gen) autofree_call_postgen(node_pos int) {
 | 
			
		|||
		}
 | 
			
		||||
	}
 | 
			
		||||
	// g.write('/* postgen end */')
 | 
			
		||||
	g.doing_autofree_tmp = false
 | 
			
		||||
	// g.doing_autofree_tmp = false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn (mut g Gen) call_args(node ast.CallExpr) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -9,6 +9,12 @@ fn simple() {
 | 
			
		|||
	name := 'Peter' // string literals mustn't be freed
 | 
			
		||||
	str_inter := 'hello, $name' // concatenated strings must be freed
 | 
			
		||||
	// nums.free() // this should result in a double free and a CI error
 | 
			
		||||
	if true {
 | 
			
		||||
		// test the freeing of local vars in a new scope
 | 
			
		||||
		nums2 := [4, 5, 6]
 | 
			
		||||
		str_inter2 := 'hello, $name'
 | 
			
		||||
		println(nums2)
 | 
			
		||||
	}
 | 
			
		||||
	arr := return_array([])
 | 
			
		||||
	println(arr)
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -243,7 +249,7 @@ fn main() {
 | 
			
		|||
	str_tmp_expr_advanced_var_decl()
 | 
			
		||||
	str_inter()
 | 
			
		||||
	match_expr()
 | 
			
		||||
	optional_str()
 | 
			
		||||
	// optional_str()
 | 
			
		||||
	// optional_return()
 | 
			
		||||
	str_replace()
 | 
			
		||||
	str_replace2()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue