cgen: cleanup in gen_array_contains_methods() (#14274)
parent
146051b231
commit
3732db2bcc
|
@ -700,90 +700,87 @@ fn (mut g Gen) gen_array_contains_methods() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
done << t
|
done << t
|
||||||
if left_final_sym.kind == .array {
|
mut fn_builder := strings.new_builder(512)
|
||||||
mut left_type_str := g.typ(t)
|
mut left_type_str := g.typ(t)
|
||||||
fn_name := '${left_type_str}_contains'
|
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)
|
if left_final_sym.kind == .array {
|
||||||
elem_sym := g.table.sym(left_info.elem_type)
|
elem_type := (left_final_sym.info as ast.Array).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 < 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\treturn true;')
|
||||||
fn_builder.writeln('\t\t}')
|
fn_builder.writeln('\t\t}')
|
||||||
fn_builder.writeln('\t}')
|
fn_builder.writeln('\t}')
|
||||||
|
@ -792,7 +789,6 @@ fn (mut g Gen) gen_array_contains_methods() {
|
||||||
g.auto_fn_definitions << fn_builder.str()
|
g.auto_fn_definitions << fn_builder.str()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// `nums.contains(2)`
|
// `nums.contains(2)`
|
||||||
fn (mut g Gen) gen_array_contains(typ ast.Type, left ast.Expr, right ast.Expr) {
|
fn (mut g Gen) gen_array_contains(typ ast.Type, left ast.Expr, right ast.Expr) {
|
||||||
|
|
Loading…
Reference in New Issue