Skip to content

Commit

Permalink
cgen: enable autofree for option (#21051)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Mar 20, 2024
1 parent 9894c03 commit e6b43a1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
7 changes: 7 additions & 0 deletions vlib/builtin/string.v
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,13 @@ fn (a string) clone_static() string {
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`.
pub fn (a string) clone() string {
if a.len <= 0 {
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/gen/c/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
mut cloned := false
if g.is_autofree && right_sym.kind in [.array, .string]
&& !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
}
}
Expand Down
28 changes: 20 additions & 8 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -2877,7 +2877,7 @@ fn (mut g Gen) get_ternary_name(name string) string {
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] {
return false
}
Expand Down Expand Up @@ -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 {
// `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.write(')')
}
Expand Down Expand Up @@ -2981,11 +2985,7 @@ fn (mut g Gen) autofree_scope_vars2(scope &ast.Scope, start_pos int, end_pos int
continue
}
is_option := obj.typ.has_flag(.option)
if is_option {
// TODO: free options
continue
}
g.autofree_variable(obj)
g.autofree_variable(obj, is_option)
}
else {}
}
Expand All @@ -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
if v.typ == 0 {
return
Expand Down Expand Up @@ -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('(')
if v.typ.has_flag(.option) {
base_type := g.base_type(v.typ)
af.write_string('(${base_type}*)')
}
if v.typ.share() == .shared_t {
af.write_string('&')
}
Expand All @@ -3101,6 +3105,9 @@ fn (mut g Gen) autofree_var_call(free_fn_name string, v ast.Var) {
if v.typ.share() == .shared_t {
af.write_string('->val')
}
if v.typ.has_flag(.option) {
af.write_string('.data)')
}

af.writeln('); // autofreed ptr var')
} else {
Expand All @@ -3109,6 +3116,11 @@ fn (mut g Gen) autofree_var_call(free_fn_name string, v ast.Var) {
}
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}')
} 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 {
af.writeln('\t${free_fn_name}(&${c_name(v.name)}); // autofreed var ${g.cur_mod.name} ${g.is_builtin_mod}')
}
Expand Down
6 changes: 1 addition & 5 deletions vlib/v/gen/c/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -2220,10 +2220,6 @@ fn (mut g Gen) autofree_call_postgen(node_pos int) {
// continue
// }
is_option := obj.typ.has_flag(.option)
if is_option {
// TODO: free options
continue
}
is_result := obj.typ.has_flag(.result)
if is_result {
// TODO: free results
Expand All @@ -2237,7 +2233,7 @@ fn (mut g Gen) autofree_call_postgen(node_pos int) {
continue
}
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--
}
else {}
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/gen/c/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ fn (mut g Gen) struct_init_field(sfield ast.StructInitField, language ast.Langua
mut cloned := false
if g.is_autofree && !sfield.typ.is_ptr() && field_type_sym.kind in [.array, .string] {
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
}
}
Expand Down

0 comments on commit e6b43a1

Please sign in to comment.