cgen: cleanup in gen_array_contains_methods() (#14274)

yuyi 2022-05-04 01:46:07 +08:00 committed by Jef Roosens
parent ee6d819f22
commit 4b48bece20
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
1 changed files with 49 additions and 53 deletions

View File

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