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

cgen: cleanup in gen_array_contains_methods() (#14274)

This commit is contained in:
yuyi 2022-05-04 01:46:07 +08:00 committed by GitHub
parent 146051b231
commit 3732db2bcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -700,97 +700,93 @@ fn (mut g Gen) gen_array_contains_methods() {
continue
}
done << t
mut fn_builder := strings.new_builder(512)
mut left_type_str := g.typ(t)
fn_name := '${left_type_str}_contains'
if left_final_sym.kind == .array {
mut left_type_str := g.typ(t)
fn_name := '${left_type_str}_contains'
left_info := left_final_sym.info as ast.Array
mut elem_type_str := g.typ(left_info.elem_type)
elem_sym := g.table.sym(left_info.elem_type)
if elem_sym.kind == .function {
elem_type := (left_final_sym.info as ast.Array).elem_type
mut elem_type_str := g.typ(elem_type)
elem_kind := g.table.sym(elem_type).kind
elem_is_not_ptr := elem_type.nr_muls() == 0
if elem_kind == .function {
left_type_str = 'Array_voidptr'
elem_type_str = 'voidptr'
}
g.type_definitions.writeln('static bool ${fn_name}($left_type_str a, $elem_type_str v); // auto')
mut fn_builder := strings.new_builder(512)
fn_builder.writeln('static bool ${fn_name}($left_type_str a, $elem_type_str v) {')
fn_builder.writeln('\tfor (int i = 0; i < a.len; ++i) {')
if elem_sym.kind == .string {
if elem_kind == .string {
fn_builder.writeln('\t\tif (fast_string_eq(((string*)a.data)[i], v)) {')
} else if elem_sym.kind == .array && left_info.elem_type.nr_muls() == 0 {
ptr_typ := g.equality_fn(left_info.elem_type)
} else if elem_kind == .array && elem_is_not_ptr {
ptr_typ := g.equality_fn(elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_arr_eq((($elem_type_str*)a.data)[i], v)) {')
} else if elem_sym.kind == .function {
} else if elem_kind == .function {
fn_builder.writeln('\t\tif (((voidptr*)a.data)[i] == v) {')
} else if elem_sym.kind == .map && left_info.elem_type.nr_muls() == 0 {
ptr_typ := g.equality_fn(left_info.elem_type)
} else if elem_kind == .map && elem_is_not_ptr {
ptr_typ := g.equality_fn(elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_map_eq((($elem_type_str*)a.data)[i], v)) {')
} else if elem_sym.kind == .struct_ && left_info.elem_type.nr_muls() == 0 {
ptr_typ := g.equality_fn(left_info.elem_type)
} else if elem_kind == .struct_ && elem_is_not_ptr {
ptr_typ := g.equality_fn(elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_struct_eq((($elem_type_str*)a.data)[i], v)) {')
} else if elem_sym.kind == .interface_ && left_info.elem_type.nr_muls() == 0 {
ptr_typ := g.equality_fn(left_info.elem_type)
} else if elem_kind == .interface_ && elem_is_not_ptr {
ptr_typ := g.equality_fn(elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_interface_eq((($elem_type_str*)a.data)[i], v)) {')
} else if elem_sym.kind == .sum_type && left_info.elem_type.nr_muls() == 0 {
ptr_typ := g.equality_fn(left_info.elem_type)
} else if elem_kind == .sum_type && elem_is_not_ptr {
ptr_typ := g.equality_fn(elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_sumtype_eq((($elem_type_str*)a.data)[i], v)) {')
} else if elem_sym.kind == .alias && left_info.elem_type.nr_muls() == 0 {
ptr_typ := g.equality_fn(left_info.elem_type)
} else if elem_kind == .alias && elem_is_not_ptr {
ptr_typ := g.equality_fn(elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_alias_eq((($elem_type_str*)a.data)[i], v)) {')
} else {
fn_builder.writeln('\t\tif ((($elem_type_str*)a.data)[i] == v) {')
}
fn_builder.writeln('\t\t\treturn true;')
fn_builder.writeln('\t\t}')
fn_builder.writeln('\t}')
fn_builder.writeln('\treturn false;')
fn_builder.writeln('}')
g.auto_fn_definitions << fn_builder.str()
} else if left_final_sym.kind == .array_fixed {
mut left_type_str := g.typ(t)
fn_name := '${left_type_str}_contains'
left_info := left_final_sym.info as ast.ArrayFixed
mut elem_type_str := g.typ(left_info.elem_type)
elem_sym := g.table.sym(left_info.elem_type)
if elem_sym.kind == .function {
size := left_info.size
elem_type := left_info.elem_type
mut elem_type_str := g.typ(elem_type)
elem_kind := g.table.sym(elem_type).kind
elem_is_not_ptr := elem_type.nr_muls() == 0
if elem_kind == .function {
left_type_str = 'Array_voidptr'
elem_type_str = 'voidptr'
}
g.type_definitions.writeln('static bool ${fn_name}($left_type_str a, $elem_type_str v); // auto')
mut fn_builder := strings.new_builder(512)
fn_builder.writeln('static bool ${fn_name}($left_type_str a, $elem_type_str v) {')
fn_builder.writeln('\tfor (int i = 0; i < $left_info.size; ++i) {')
if elem_sym.kind == .string {
fn_builder.writeln('\tfor (int i = 0; i < $size; ++i) {')
if elem_kind == .string {
fn_builder.writeln('\t\tif (fast_string_eq(a[i], v)) {')
} else if elem_sym.kind == .array && left_info.elem_type.nr_muls() == 0 {
} else if elem_kind == .array && elem_is_not_ptr {
ptr_typ := g.equality_fn(left_info.elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_arr_eq(a[i], v)) {')
} else if elem_sym.kind == .function {
} else if elem_kind == .function {
fn_builder.writeln('\t\tif (a[i] == v) {')
} else if elem_sym.kind == .map && left_info.elem_type.nr_muls() == 0 {
ptr_typ := g.equality_fn(left_info.elem_type)
} else if elem_kind == .map && elem_is_not_ptr {
ptr_typ := g.equality_fn(elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_map_eq(a[i], v)) {')
} else if elem_sym.kind == .struct_ && left_info.elem_type.nr_muls() == 0 {
ptr_typ := g.equality_fn(left_info.elem_type)
} else if elem_kind == .struct_ && elem_is_not_ptr {
ptr_typ := g.equality_fn(elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_struct_eq(a[i], v)) {')
} else if elem_sym.kind == .interface_ && left_info.elem_type.nr_muls() == 0 {
ptr_typ := g.equality_fn(left_info.elem_type)
} else if elem_kind == .interface_ && elem_is_not_ptr {
ptr_typ := g.equality_fn(elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_interface_eq(a[i], v)) {')
} else if elem_sym.kind == .sum_type && left_info.elem_type.nr_muls() == 0 {
ptr_typ := g.equality_fn(left_info.elem_type)
} else if elem_kind == .sum_type && elem_is_not_ptr {
ptr_typ := g.equality_fn(elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_sumtype_eq(a[i], v)) {')
} else if elem_sym.kind == .alias && left_info.elem_type.nr_muls() == 0 {
ptr_typ := g.equality_fn(left_info.elem_type)
} else if elem_kind == .alias && elem_is_not_ptr {
ptr_typ := g.equality_fn(elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_alias_eq(a[i], v)) {')
} else {
fn_builder.writeln('\t\tif (a[i] == v) {')
}
fn_builder.writeln('\t\t\treturn true;')
fn_builder.writeln('\t\t}')
fn_builder.writeln('\t}')
fn_builder.writeln('\treturn false;')
fn_builder.writeln('}')
g.auto_fn_definitions << fn_builder.str()
}
fn_builder.writeln('\t\t\treturn true;')
fn_builder.writeln('\t\t}')
fn_builder.writeln('\t}')
fn_builder.writeln('\treturn false;')
fn_builder.writeln('}')
g.auto_fn_definitions << fn_builder.str()
}
}