mirror of
https://github.com/vlang/v.git
synced 2025-09-13 09:25:45 -04:00
cgen: enable autofree for option (#21051)
This commit is contained in:
parent
9894c03e14
commit
e6b43a166e
@ -313,6 +313,13 @@ fn (a string) clone_static() string {
|
|||||||
return a.clone()
|
return a.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// option_clone_static returns an independent copy of a given array when lhs is an option type.
|
||||||
|
// It should be used only in -autofree generated code.
|
||||||
|
@[inline; markused]
|
||||||
|
fn (a string) option_clone_static() ?string {
|
||||||
|
return ?string(a.clone())
|
||||||
|
}
|
||||||
|
|
||||||
// clone returns a copy of the V string `a`.
|
// clone returns a copy of the V string `a`.
|
||||||
pub fn (a string) clone() string {
|
pub fn (a string) clone() string {
|
||||||
if a.len <= 0 {
|
if a.len <= 0 {
|
||||||
|
@ -652,7 +652,7 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
|
|||||||
mut cloned := false
|
mut cloned := false
|
||||||
if g.is_autofree && right_sym.kind in [.array, .string]
|
if g.is_autofree && right_sym.kind in [.array, .string]
|
||||||
&& !unwrapped_val_type.has_flag(.shared_f) {
|
&& !unwrapped_val_type.has_flag(.shared_f) {
|
||||||
if g.gen_clone_assignment(val, unwrapped_val_type, false) {
|
if g.gen_clone_assignment(var_type, val, unwrapped_val_type, false) {
|
||||||
cloned = true
|
cloned = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2877,7 +2877,7 @@ fn (mut g Gen) get_ternary_name(name string) string {
|
|||||||
return g.ternary_names[name]
|
return g.ternary_names[name]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut g Gen) gen_clone_assignment(val ast.Expr, typ ast.Type, add_eq bool) bool {
|
fn (mut g Gen) gen_clone_assignment(var_type ast.Type, val ast.Expr, typ ast.Type, add_eq bool) bool {
|
||||||
if val !in [ast.Ident, ast.SelectorExpr] {
|
if val !in [ast.Ident, ast.SelectorExpr] {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -2905,7 +2905,11 @@ fn (mut g Gen) gen_clone_assignment(val ast.Expr, typ ast.Type, add_eq bool) boo
|
|||||||
}
|
}
|
||||||
} else if right_sym.kind == .string {
|
} else if right_sym.kind == .string {
|
||||||
// `str1 = str2` => `str1 = str2.clone()`
|
// `str1 = str2` => `str1 = str2.clone()`
|
||||||
g.write(' string_clone_static(')
|
if var_type.has_flag(.option) {
|
||||||
|
g.write(' string_option_clone_static(')
|
||||||
|
} else {
|
||||||
|
g.write(' string_clone_static(')
|
||||||
|
}
|
||||||
g.expr(val)
|
g.expr(val)
|
||||||
g.write(')')
|
g.write(')')
|
||||||
}
|
}
|
||||||
@ -2981,11 +2985,7 @@ fn (mut g Gen) autofree_scope_vars2(scope &ast.Scope, start_pos int, end_pos int
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
is_option := obj.typ.has_flag(.option)
|
is_option := obj.typ.has_flag(.option)
|
||||||
if is_option {
|
g.autofree_variable(obj, is_option)
|
||||||
// TODO: free options
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
g.autofree_variable(obj)
|
|
||||||
}
|
}
|
||||||
else {}
|
else {}
|
||||||
}
|
}
|
||||||
@ -3009,7 +3009,7 @@ fn (mut g Gen) autofree_scope_vars2(scope &ast.Scope, start_pos int, end_pos int
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut g Gen) autofree_variable(v ast.Var) {
|
fn (mut g Gen) autofree_variable(v ast.Var, is_option bool) {
|
||||||
// filter out invalid variables
|
// filter out invalid variables
|
||||||
if v.typ == 0 {
|
if v.typ == 0 {
|
||||||
return
|
return
|
||||||
@ -3093,6 +3093,10 @@ fn (mut g Gen) autofree_var_call(free_fn_name string, v ast.Var) {
|
|||||||
af.write_string(free_fn_name)
|
af.write_string(free_fn_name)
|
||||||
}
|
}
|
||||||
af.write_string('(')
|
af.write_string('(')
|
||||||
|
if v.typ.has_flag(.option) {
|
||||||
|
base_type := g.base_type(v.typ)
|
||||||
|
af.write_string('(${base_type}*)')
|
||||||
|
}
|
||||||
if v.typ.share() == .shared_t {
|
if v.typ.share() == .shared_t {
|
||||||
af.write_string('&')
|
af.write_string('&')
|
||||||
}
|
}
|
||||||
@ -3101,6 +3105,9 @@ fn (mut g Gen) autofree_var_call(free_fn_name string, v ast.Var) {
|
|||||||
if v.typ.share() == .shared_t {
|
if v.typ.share() == .shared_t {
|
||||||
af.write_string('->val')
|
af.write_string('->val')
|
||||||
}
|
}
|
||||||
|
if v.typ.has_flag(.option) {
|
||||||
|
af.write_string('.data)')
|
||||||
|
}
|
||||||
|
|
||||||
af.writeln('); // autofreed ptr var')
|
af.writeln('); // autofreed ptr var')
|
||||||
} else {
|
} else {
|
||||||
@ -3109,6 +3116,11 @@ fn (mut g Gen) autofree_var_call(free_fn_name string, v ast.Var) {
|
|||||||
}
|
}
|
||||||
if v.is_auto_heap {
|
if v.is_auto_heap {
|
||||||
af.writeln('\t${free_fn_name}(${c_name(v.name)}); // autofreed heap var ${g.cur_mod.name} ${g.is_builtin_mod}')
|
af.writeln('\t${free_fn_name}(${c_name(v.name)}); // autofreed heap var ${g.cur_mod.name} ${g.is_builtin_mod}')
|
||||||
|
} else if v.typ.has_flag(.option) {
|
||||||
|
base_type := g.base_type(v.typ)
|
||||||
|
af.writeln('\tif (${c_name(v.name)}.state != 2) {')
|
||||||
|
af.writeln('\t\t${free_fn_name}((${base_type}*)${c_name(v.name)}.data); // autofreed option var ${g.cur_mod.name} ${g.is_builtin_mod}')
|
||||||
|
af.writeln('\t}')
|
||||||
} else {
|
} else {
|
||||||
af.writeln('\t${free_fn_name}(&${c_name(v.name)}); // autofreed var ${g.cur_mod.name} ${g.is_builtin_mod}')
|
af.writeln('\t${free_fn_name}(&${c_name(v.name)}); // autofreed var ${g.cur_mod.name} ${g.is_builtin_mod}')
|
||||||
}
|
}
|
||||||
|
@ -2220,10 +2220,6 @@ fn (mut g Gen) autofree_call_postgen(node_pos int) {
|
|||||||
// continue
|
// continue
|
||||||
// }
|
// }
|
||||||
is_option := obj.typ.has_flag(.option)
|
is_option := obj.typ.has_flag(.option)
|
||||||
if is_option {
|
|
||||||
// TODO: free options
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
is_result := obj.typ.has_flag(.result)
|
is_result := obj.typ.has_flag(.result)
|
||||||
if is_result {
|
if is_result {
|
||||||
// TODO: free results
|
// TODO: free results
|
||||||
@ -2237,7 +2233,7 @@ fn (mut g Gen) autofree_call_postgen(node_pos int) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
obj.is_used = true // TODO bug? sets all vars is_used to true
|
obj.is_used = true // TODO bug? sets all vars is_used to true
|
||||||
g.autofree_variable(obj)
|
g.autofree_variable(obj, is_option)
|
||||||
// g.nr_vars_to_free--
|
// g.nr_vars_to_free--
|
||||||
}
|
}
|
||||||
else {}
|
else {}
|
||||||
|
@ -619,7 +619,7 @@ fn (mut g Gen) struct_init_field(sfield ast.StructInitField, language ast.Langua
|
|||||||
mut cloned := false
|
mut cloned := false
|
||||||
if g.is_autofree && !sfield.typ.is_ptr() && field_type_sym.kind in [.array, .string] {
|
if g.is_autofree && !sfield.typ.is_ptr() && field_type_sym.kind in [.array, .string] {
|
||||||
g.write('/*clone1*/')
|
g.write('/*clone1*/')
|
||||||
if g.gen_clone_assignment(sfield.expr, sfield.typ, false) {
|
if g.gen_clone_assignment(sfield.typ, sfield.expr, sfield.typ, false) {
|
||||||
cloned = true
|
cloned = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user