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

checker: cleanup in fn.v (#18958)

This commit is contained in:
yuyi 2023-07-25 05:12:43 +08:00 committed by GitHub
parent c6ddbd308e
commit b29f3caeec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -893,16 +893,14 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
if typ != 0 { if typ != 0 {
generic_vts := c.table.final_sym(typ) generic_vts := c.table.final_sym(typ)
if generic_vts.kind == .function { if generic_vts.info is ast.FnType {
info := generic_vts.info as ast.FnType func = generic_vts.info.func
func = info.func
found = true found = true
found_in_args = true found_in_args = true
} else { } else {
vts := c.table.sym(c.unwrap_generic(typ)) vts := c.table.sym(c.unwrap_generic(typ))
if vts.kind == .function { if vts.info is ast.FnType {
info := vts.info as ast.FnType func = vts.info.func
func = info.func
found = true found = true
found_in_args = true found_in_args = true
} }
@ -914,8 +912,8 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
if obj := c.file.global_scope.find(fn_name) { if obj := c.file.global_scope.find(fn_name) {
if obj.typ != 0 { if obj.typ != 0 {
sym := c.table.sym(obj.typ) sym := c.table.sym(obj.typ)
if sym.kind == .function { if sym.info is ast.FnType {
func = (sym.info as ast.FnType).func func = sym.info.func
found = true found = true
} }
} }
@ -931,13 +929,13 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
} }
if obj.typ != 0 { if obj.typ != 0 {
sym := c.table.sym(obj.typ) sym := c.table.sym(obj.typ)
if sym.kind == .function { if sym.info is ast.FnType {
// at this point, the const metadata should be already known, // at this point, the const metadata should be already known,
// and we are sure that it is just a function // and we are sure that it is just a function
c.table.fns[qualified_const_name].usages++ c.table.fns[qualified_const_name].usages++
c.table.fns[func.name].usages++ c.table.fns[func.name].usages++
found = true found = true
func = (sym.info as ast.FnType).func func = sym.info.func
node.is_fn_a_const = true node.is_fn_a_const = true
node.fn_var_type = obj.typ node.fn_var_type = obj.typ
node.const_name = qualified_const_name node.const_name = qualified_const_name
@ -1053,9 +1051,8 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
if func.is_variadic && i >= func.params.len - 1 { if func.is_variadic && i >= func.params.len - 1 {
param_sym := c.table.sym(param.typ) param_sym := c.table.sym(param.typ)
mut expected_type := param.typ mut expected_type := param.typ
if param_sym.kind == .array { if param_sym.info is ast.Array {
info := param_sym.array_info() expected_type = param_sym.info.elem_type
expected_type = info.elem_type
c.expected_type = expected_type c.expected_type = expected_type
} }
typ := c.expr(mut call_arg.expr) typ := c.expr(mut call_arg.expr)
@ -1196,18 +1193,17 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
if param.typ.has_flag(.generic) { if param.typ.has_flag(.generic) {
continue continue
} }
if param_typ_sym.kind == .array && arg_typ_sym.kind == .array { if param_typ_sym.info is ast.Array && arg_typ_sym.info is ast.Array {
param_info := param_typ_sym.info as ast.Array param_elem_type := c.table.unaliased_type(param_typ_sym.info.elem_type)
param_elem_type := c.table.unaliased_type(param_info.elem_type) arg_elem_type := c.table.unaliased_type(arg_typ_sym.info.elem_type)
arg_info := arg_typ_sym.info as ast.Array
arg_elem_type := c.table.unaliased_type(arg_info.elem_type)
param_nr_muls := param.typ.nr_muls() param_nr_muls := param.typ.nr_muls()
arg_nr_muls := if call_arg.is_mut { arg_nr_muls := if call_arg.is_mut {
arg_typ.nr_muls() + 1 arg_typ.nr_muls() + 1
} else { } else {
arg_typ.nr_muls() arg_typ.nr_muls()
} }
if param_nr_muls == arg_nr_muls && param_info.nr_dims == arg_info.nr_dims if param_nr_muls == arg_nr_muls
&& param_typ_sym.info.nr_dims == arg_typ_sym.info.nr_dims
&& param_elem_type == arg_elem_type { && param_elem_type == arg_elem_type {
continue continue
} }
@ -1255,24 +1251,27 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
if param_type.is_any_kind_of_pointer() && arg_typ.is_any_kind_of_pointer() { if param_type.is_any_kind_of_pointer() && arg_typ.is_any_kind_of_pointer() {
continue continue
} }
param_typ_sym_ := c.table.sym(c.table.unaliased_type(param_type)) unaliased_param_sym := c.table.sym(c.table.unaliased_type(param_type))
arg_typ_sym_ := c.table.sym(c.table.unaliased_type(arg_typ)) unaliased_arg_sym := c.table.sym(c.table.unaliased_type(arg_typ))
// Allow `[32]i8` as `&i8` etc // Allow `[32]i8` as `&i8` etc
if ((arg_typ_sym_.kind == .array_fixed || arg_typ_sym_.kind == .array) if ((unaliased_arg_sym.kind == .array_fixed || unaliased_arg_sym.kind == .array)
&& (param_is_number && (param_is_number
|| c.table.unaliased_type(param_type).is_any_kind_of_pointer())) || c.table.unaliased_type(param_type).is_any_kind_of_pointer()))
|| ((param_typ_sym_.kind == .array_fixed || param_typ_sym_.kind == .array) || ((unaliased_param_sym.kind == .array_fixed
|| unaliased_param_sym.kind == .array)
&& (typ_is_number || c.table.unaliased_type(arg_typ).is_any_kind_of_pointer())) { && (typ_is_number || c.table.unaliased_type(arg_typ).is_any_kind_of_pointer())) {
continue continue
} }
// Allow `[N]anyptr` as `[N]anyptr` // Allow `[N]anyptr` as `[N]anyptr`
if arg_typ_sym_.kind == .array && param_typ_sym_.kind == .array { if unaliased_arg_sym.info is ast.Array && unaliased_param_sym.info is ast.Array {
if (arg_typ_sym_.info as ast.Array).elem_type.is_any_kind_of_pointer() if unaliased_arg_sym.info.elem_type.is_any_kind_of_pointer()
&& (param_typ_sym_.info as ast.Array).elem_type.is_any_kind_of_pointer() { && unaliased_param_sym.info.elem_type.is_any_kind_of_pointer() {
continue continue
} }
} else if arg_typ_sym_.kind == .array_fixed && param_typ_sym_.kind == .array_fixed { } else if unaliased_arg_sym.info is ast.ArrayFixed
if (arg_typ_sym_.info as ast.ArrayFixed).elem_type.is_any_kind_of_pointer()&& (param_typ_sym_.info as ast.ArrayFixed).elem_type.is_any_kind_of_pointer() { && unaliased_param_sym.info is ast.ArrayFixed {
if unaliased_arg_sym.info.elem_type.is_any_kind_of_pointer()
&& unaliased_param_sym.info.elem_type.is_any_kind_of_pointer() {
continue continue
} }
} }
@ -1417,9 +1416,9 @@ fn (mut c Checker) get_comptime_args(func ast.Fn, node_ ast.CallExpr, concrete_t
mut ctyp := c.get_comptime_var_type(call_arg.expr) mut ctyp := c.get_comptime_var_type(call_arg.expr)
if ctyp != ast.void_type { if ctyp != ast.void_type {
arg_sym := c.table.sym(ctyp) arg_sym := c.table.sym(ctyp)
if arg_sym.kind == .array && param_typ.has_flag(.generic) if arg_sym.info is ast.Array && param_typ.has_flag(.generic)
&& c.table.final_sym(param_typ).kind == .array { && c.table.final_sym(param_typ).kind == .array {
ctyp = (arg_sym.info as ast.Array).elem_type ctyp = arg_sym.info.elem_type
} }
comptime_args[i] = ctyp comptime_args[i] = ctyp
} }
@ -1432,9 +1431,9 @@ fn (mut c Checker) get_comptime_args(func ast.Fn, node_ ast.CallExpr, concrete_t
if param_typ.has_flag(.variadic) { if param_typ.has_flag(.variadic) {
ctyp = ast.mktyp(ctyp) ctyp = ast.mktyp(ctyp)
comptime_args[i] = ctyp comptime_args[i] = ctyp
} else if arg_sym.kind == .array && param_typ.has_flag(.generic) } else if arg_sym.info is ast.Array && param_typ.has_flag(.generic)
&& param_typ_sym.kind == .array { && param_typ_sym.kind == .array {
ctyp = c.get_generic_array_element_type(arg_sym.info as ast.Array) ctyp = c.get_generic_array_element_type(arg_sym.info)
comptime_args[i] = ctyp comptime_args[i] = ctyp
} else if arg_sym.kind in [.struct_, .interface_, .sum_type] { } else if arg_sym.kind in [.struct_, .interface_, .sum_type] {
mut generic_types := []ast.Type{} mut generic_types := []ast.Type{}
@ -1458,9 +1457,9 @@ fn (mut c Checker) get_comptime_args(func ast.Fn, node_ ast.CallExpr, concrete_t
} }
} }
} else if arg_sym.kind == .any { } else if arg_sym.kind == .any {
mut cparam_type_sym := c.table.sym(c.unwrap_generic(ctyp)) cparam_type_sym := c.table.sym(c.unwrap_generic(ctyp))
if param_typ_sym.kind == .array && cparam_type_sym.kind == .array { if param_typ_sym.kind == .array && cparam_type_sym.info is ast.Array {
ctyp = (cparam_type_sym.info as ast.Array).elem_type ctyp = cparam_type_sym.info.elem_type
comptime_args[i] = ctyp comptime_args[i] = ctyp
} else { } else {
if node_.args[i].expr.is_auto_deref_var() { if node_.args[i].expr.is_auto_deref_var() {
@ -1528,20 +1527,18 @@ fn (mut c Checker) resolve_fn_generic_args(func ast.Fn, mut node ast.CallExpr) [
// cast_fixed_array_ret casts a ArrayFixed type created to return to a non returning one // cast_fixed_array_ret casts a ArrayFixed type created to return to a non returning one
fn (mut c Checker) cast_fixed_array_ret(typ ast.Type, sym ast.TypeSymbol) ast.Type { fn (mut c Checker) cast_fixed_array_ret(typ ast.Type, sym ast.TypeSymbol) ast.Type {
if sym.kind == .array_fixed && (sym.info as ast.ArrayFixed).is_fn_ret { if sym.info is ast.ArrayFixed && sym.info.is_fn_ret {
info := sym.info as ast.ArrayFixed return c.table.find_or_register_array_fixed(sym.info.elem_type, sym.info.size,
return c.table.find_or_register_array_fixed(info.elem_type, info.size, info.size_expr, sym.info.size_expr, false)
false)
} }
return typ return typ
} }
// cast_to_fixed_array_ret casts a ArrayFixed type created to do not return to a returning one // cast_to_fixed_array_ret casts a ArrayFixed type created to do not return to a returning one
fn (mut c Checker) cast_to_fixed_array_ret(typ ast.Type, sym ast.TypeSymbol) ast.Type { fn (mut c Checker) cast_to_fixed_array_ret(typ ast.Type, sym ast.TypeSymbol) ast.Type {
if sym.kind == .array_fixed && !(sym.info as ast.ArrayFixed).is_fn_ret { if sym.info is ast.ArrayFixed && !sym.info.is_fn_ret {
info := sym.info as ast.ArrayFixed return c.table.find_or_register_array_fixed(sym.info.elem_type, sym.info.size,
return c.table.find_or_register_array_fixed(info.elem_type, info.size, info.size_expr, sym.info.size_expr, true)
true)
} }
return typ return typ
} }
@ -1634,7 +1631,7 @@ fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
if !c.check_types(arg_type, info.elem_type) && !c.check_types(left_type, arg_type) { if !c.check_types(arg_type, info.elem_type) && !c.check_types(left_type, arg_type) {
c.error('cannot ${method_name} `${arg_sym.name}` to `${left_sym.name}`', arg_expr.pos()) c.error('cannot ${method_name} `${arg_sym.name}` to `${left_sym.name}`', arg_expr.pos())
} }
} else if final_left_sym.info is ast.Array && method_name in ['first', 'last', 'pop'] { } else if final_left_sym.kind == .array && method_name in ['first', 'last', 'pop'] {
return c.array_builtin_method_call(mut node, left_type, final_left_sym) return c.array_builtin_method_call(mut node, left_type, final_left_sym)
} else if c.pref.backend.is_js() && left_sym.name.starts_with('Promise[') } else if c.pref.backend.is_js() && left_sym.name.starts_with('Promise[')
&& method_name == 'wait' { && method_name == 'wait' {
@ -1648,13 +1645,12 @@ fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
node.return_type = info.concrete_types[0] node.return_type = info.concrete_types[0]
node.return_type.set_flag(.option) node.return_type.set_flag(.option)
return node.return_type return node.return_type
} else if left_sym.kind == .thread && method_name == 'wait' { } else if left_sym.info is ast.Thread && method_name == 'wait' {
info := left_sym.info as ast.Thread
if node.args.len > 0 { if node.args.len > 0 {
c.error('wait() does not have any arguments', node.args[0].pos) c.error('wait() does not have any arguments', node.args[0].pos)
} }
node.return_type = info.return_type node.return_type = left_sym.info.return_type
return info.return_type return left_sym.info.return_type
} else if left_sym.kind == .char && left_type.nr_muls() == 0 && method_name == 'str' { } else if left_sym.kind == .char && left_type.nr_muls() == 0 && method_name == 'str' {
c.error('calling `.str()` on type `char` is not allowed, use its address or cast it to an integer instead', c.error('calling `.str()` on type `char` is not allowed, use its address or cast it to an integer instead',
node.left.pos().extend(node.pos)) node.left.pos().extend(node.pos))