Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Oct 18, 2024
1 parent cf11ddc commit 9824633
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions vlib/v/gen/c/auto_free_methods.v
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ fn (mut g Gen) gen_free_for_interface(sym ast.TypeSymbol, info ast.Interface, st
if sub_sym.kind !in [.string, .array, .map, .struct] {
continue
}
fn_name_typ := g.get_free_method(typ_)
field_styp := g.gen_type_name_for_free_call(typ_)
fn_name_typ := if sub_sym.has_method('free') {
'${field_styp}_free'
} else {
g.gen_free_method(typ_)
}
fn_builder.writeln('\tif (it->_typ == _${sym.cname}_${sub_sym.cname}_index) { ${fn_name_typ}(it->_${sub_sym.cname}); return; }')
}
fn_builder.writeln('}')
Expand All @@ -107,21 +112,16 @@ fn (mut g Gen) gen_free_for_struct(typ ast.Type, info ast.Struct, styp string, f
if sym.kind !in [.string, .array, .map, .struct] {
continue
}
mut field_styp := g.typ(field.typ.set_nr_muls(0).clear_flag(.option)).replace('*',
'')
is_shared := field_styp.starts_with('__shared')
field_styp := g.gen_type_name_for_free_call(field.typ)
is_struct_option := typ.has_flag(.option)
if is_shared {
field_styp = field_styp.all_after('__shared__')
}
field_styp_fn_name := if sym.has_method('free') {
'${field_styp}_free'
} else {
g.gen_free_method(field.typ)
}
is_field_option := field.typ.has_flag(.option)
expects_opt := field_styp_fn_name.starts_with('_option_')
if is_shared {
if field.typ.has_flag(.shared_f) {
fn_builder.writeln('\t${field_styp_fn_name}(&(it->${field_name}->val));')
} else if is_struct_option {
opt_styp := g.base_type(typ)
Expand Down Expand Up @@ -152,6 +152,14 @@ fn (mut g Gen) gen_free_for_struct(typ ast.Type, info ast.Struct, styp string, f
fn_builder.writeln('}')
}

fn (mut g Gen) gen_type_name_for_free_call(typ ast.Type) string {
mut styp := g.typ(typ.set_nr_muls(0).clear_flag(.option)).replace('*', '')
if styp.starts_with('__shared') {
styp = styp.all_after('__shared__')
}
return styp
}

fn (mut g Gen) gen_free_for_array(info ast.Array, styp string, fn_name string) {
g.definitions.writeln('${g.static_modifier} void ${fn_name}(${styp}* it); // auto')
mut fn_builder := strings.new_builder(128)
Expand Down

0 comments on commit 9824633

Please sign in to comment.