1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: add gen_free_for_type_array/map() (#11530)

This commit is contained in:
yuyi 2021-09-18 20:18:32 +08:00 committed by GitHub
parent f5efa93570
commit 8501217a4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 7 deletions

View File

@ -14,11 +14,6 @@ fn (mut g Gen) gen_free_method_for_type(typ ast.Type) string {
sym = g.table.get_type_symbol(sym.info.parent_type)
}
}
if sym.kind == .map {
return 'map_free'
} else if sym.kind == .array {
return 'array_free'
}
if sym.has_method('free') {
return fn_name
@ -27,6 +22,12 @@ fn (mut g Gen) gen_free_method_for_type(typ ast.Type) string {
ast.Struct {
g.gen_free_for_struct(sym.info, styp, fn_name)
}
ast.Array {
g.gen_free_for_array(sym.info, styp, fn_name)
}
ast.Map {
g.gen_free_for_map(sym.info, styp, fn_name)
}
else {
println(g.table.type_str(typ))
verror("could not generate free method '$fn_name' for type '$styp'")
@ -37,7 +38,7 @@ fn (mut g Gen) gen_free_method_for_type(typ ast.Type) string {
fn (mut g Gen) gen_free_for_struct(info ast.Struct, styp string, fn_name string) {
g.type_definitions.writeln('void ${fn_name}($styp* it); // auto')
mut fn_builder := strings.new_builder(512)
mut fn_builder := strings.new_builder(128)
defer {
g.auto_fn_definitions << fn_builder.str()
}
@ -59,6 +60,43 @@ fn (mut g Gen) gen_free_for_struct(info ast.Struct, styp string, fn_name string)
fn_builder.writeln('}')
}
fn (mut g Gen) gen_free_for_array(info ast.Array, styp string, fn_name string) {
g.type_definitions.writeln('void ${fn_name}($styp* it); // auto')
mut fn_builder := strings.new_builder(128)
defer {
g.auto_fn_definitions << fn_builder.str()
}
fn_builder.writeln('void ${fn_name}($styp* it) {')
sym := g.table.get_type_symbol(g.unwrap_generic(info.elem_type))
if sym.kind in [.string, .array, .map, .struct_] {
fn_builder.writeln('\tfor (int i = 0; i < it->len; i++) {')
mut elem_styp := g.typ(info.elem_type).replace('*', '')
elem_styp_fn_name := if sym.has_method('free') {
'${elem_styp}_free'
} else {
g.gen_free_method_for_type(info.elem_type)
}
fn_builder.writeln('\t\t${elem_styp_fn_name}(&((($elem_styp*)it->data)[i]));')
fn_builder.writeln('\t}')
}
fn_builder.writeln('\tarray_free(it);')
fn_builder.writeln('}')
}
fn (mut g Gen) gen_free_for_map(info ast.Map, styp string, fn_name string) {
g.type_definitions.writeln('void ${fn_name}($styp* it); // auto')
mut fn_builder := strings.new_builder(128)
defer {
g.auto_fn_definitions << fn_builder.str()
}
fn_builder.writeln('void ${fn_name}($styp* it) {')
fn_builder.writeln('\tmap_free(it);')
fn_builder.writeln('}')
}
[inline]
fn styp_to_free_fn_name(styp string) string {
return styp.replace_each(['*', '', '.', '__', ' ', '__']) + '_free'

View File

@ -2,7 +2,7 @@ struct Info {
name string
notes []string
maps map[int]int
info SubInfo
info []SubInfo
}
struct SubInfo {