Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v: cleanup type resolver #23305

Merged
merged 6 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions vlib/v/checker/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -411,27 +411,6 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
&& c.type_resolver.is_generic_expr(right) {
// mark variable as generic var because its type changes according to fn return generic resolution type
left.obj.ct_type_var = .generic_var
if right.return_type_generic.has_flag(.generic) {
fn_ret_type := c.resolve_return_type(right)
if fn_ret_type != ast.void_type
&& c.table.final_sym(fn_ret_type).kind != .multi_return {
var_type := if right.or_block.kind == .absent {
fn_ret_type
} else {
fn_ret_type.clear_option_and_result()
}
c.type_resolver.type_map['g.${left.name}.${left.obj.pos.pos}'] = var_type
}
} else if right.is_static_method
&& right.left_type.has_flag(.generic) {
fn_ret_type := c.unwrap_generic(c.resolve_return_type(right))
var_type := if right.or_block.kind == .absent {
fn_ret_type
} else {
fn_ret_type.clear_option_and_result()
}
c.type_resolver.type_map['g.${left.name}.${left.obj.pos.pos}'] = var_type
}
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -1672,7 +1672,8 @@ fn (mut c Checker) selector_expr(mut node ast.SelectorExpr) ast.Type {
} else if c.comptime.inside_comptime_for && typ == c.enum_data_type
&& node.field_name == 'value' {
// for comp-time enum.values
node.expr_type = c.type_resolver.type_map['${c.comptime.comptime_for_enum_var}.typ']
node.expr_type = c.type_resolver.get_ct_type_or_default('${c.comptime.comptime_for_enum_var}.typ',
node.expr_type)
node.typ = typ
return node.expr_type
}
Expand Down Expand Up @@ -3056,7 +3057,8 @@ pub fn (mut c Checker) expr(mut node ast.Expr) ast.Type {
if node.expr.ct_expr {
node.expr_type = c.type_resolver.get_type(node.expr as ast.Ident)
} else if (node.expr as ast.Ident).name in c.type_resolver.type_map {
node.expr_type = c.type_resolver.type_map[(node.expr as ast.Ident).name]
node.expr_type = c.type_resolver.get_ct_type_or_default((node.expr as ast.Ident).name,
node.expr_type)
}
}
c.check_expr_option_or_result_call(node.expr, node.expr_type)
Expand Down Expand Up @@ -3348,7 +3350,8 @@ fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
if mut node.expr is ast.ComptimeSelector {
node.expr_type = c.type_resolver.get_comptime_selector_type(node.expr, node.expr_type)
} else if node.expr is ast.Ident && c.comptime.is_comptime_variant_var(node.expr) {
node.expr_type = c.type_resolver.type_map['${c.comptime.comptime_for_variant_var}.typ']
node.expr_type = c.type_resolver.get_ct_type_or_default('${c.comptime.comptime_for_variant_var}.typ',
ast.void_type)
}
mut from_type := c.unwrap_generic(node.expr_type)
from_sym := c.table.sym(from_type)
Expand Down
16 changes: 8 additions & 8 deletions vlib/v/checker/comptime.v
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ fn (mut c Checker) comptime_selector(mut node ast.ComptimeSelector) ast.Type {
}
expr_name := node.field_expr.expr.str()
if expr_name in c.type_resolver.type_map {
return c.type_resolver.type_map[expr_name]
return c.type_resolver.get_ct_type_or_default(expr_name, ast.void_type)
}
c.error('unknown `\$for` variable `${expr_name}`', left_pos)
} else {
Expand Down Expand Up @@ -300,8 +300,8 @@ fn (mut c Checker) comptime_for(mut node ast.ComptimeFor) {
}
c.comptime.comptime_for_field_value = field
c.comptime.comptime_for_field_var = node.val_var
c.type_resolver.type_map[node.val_var] = c.field_data_type
c.type_resolver.type_map['${node.val_var}.typ'] = node.typ
c.type_resolver.update_ct_type(node.val_var, c.field_data_type)
c.type_resolver.update_ct_type('${node.val_var}.typ', node.typ)
c.comptime.comptime_for_field_type = field.typ
c.stmts(mut node.stmts)

Expand Down Expand Up @@ -345,8 +345,8 @@ fn (mut c Checker) comptime_for(mut node ast.ComptimeFor) {
c.enum_data_type = c.table.find_type('EnumData')
}
c.comptime.comptime_for_enum_var = node.val_var
c.type_resolver.type_map[node.val_var] = c.enum_data_type
c.type_resolver.type_map['${node.val_var}.typ'] = node.typ
c.type_resolver.update_ct_type(node.val_var, c.enum_data_type)
c.type_resolver.update_ct_type('${node.val_var}.typ', node.typ)
c.stmts(mut node.stmts)
c.pop_comptime_info()
c.table.used_features.comptime_for = true
Expand All @@ -363,7 +363,7 @@ fn (mut c Checker) comptime_for(mut node ast.ComptimeFor) {
c.comptime.comptime_for_method = unsafe { &method }
c.comptime.comptime_for_method_var = node.val_var
c.comptime.comptime_for_method_ret_type = method.return_type
c.type_resolver.type_map['${node.val_var}.return_type'] = method.return_type
c.type_resolver.update_ct_type('${node.val_var}.return_type', method.return_type)
c.stmts(mut node.stmts)
c.pop_comptime_info()
}
Expand Down Expand Up @@ -398,8 +398,8 @@ fn (mut c Checker) comptime_for(mut node ast.ComptimeFor) {
c.push_new_comptime_info()
c.comptime.inside_comptime_for = true
c.comptime.comptime_for_variant_var = node.val_var
c.type_resolver.type_map[node.val_var] = c.variant_data_type
c.type_resolver.type_map['${node.val_var}.typ'] = variant
c.type_resolver.update_ct_type(node.val_var, c.variant_data_type)
c.type_resolver.update_ct_type('${node.val_var}.typ', variant)
c.stmts(mut node.stmts)
c.pop_comptime_info()
}
Expand Down
23 changes: 0 additions & 23 deletions vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -4005,29 +4005,6 @@ fn (mut c Checker) resolve_fn_return_type(func &ast.Fn, node ast.CallExpr) ast.T
return ret_type
}

// resolve_return_type resolves the generic return type of CallExpr
fn (mut c Checker) resolve_return_type(node ast.CallExpr) ast.Type {
if node.is_method {
left_sym := c.table.sym(c.unwrap_generic(node.left_type))
if method := c.table.find_method(left_sym, node.name) {
return c.resolve_fn_return_type(method, node)
}
} else if node.is_static_method {
if c.table.cur_fn != unsafe { nil } {
_, name := c.table.convert_generic_static_type_name(node.name, c.table.cur_fn.generic_names,
c.table.cur_concrete_types)
if func := c.table.find_fn(name) {
return c.resolve_fn_return_type(func, node)
}
}
} else {
if func := c.table.find_fn(node.name) {
return c.resolve_fn_return_type(func, node)
}
}
return node.return_type
}

fn (mut c Checker) check_must_use_call_result(node &ast.CallExpr, f &ast.Fn, label string) {
if node.is_return_used {
return
Expand Down
10 changes: 5 additions & 5 deletions vlib/v/checker/for.v
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ fn (mut c Checker) for_in_stmt(mut node ast.ForInStmt) {
node.scope.update_var_type(node.val_var, val_type)

if is_comptime {
c.type_resolver.type_map[node.val_var] = val_type
c.type_resolver.update_ct_type(node.val_var, val_type)
node.scope.update_ct_var_kind(node.val_var, .value_var)

defer {
Expand All @@ -182,7 +182,7 @@ fn (mut c Checker) for_in_stmt(mut node ast.ForInStmt) {
node.scope.update_var_type(node.key_var, key_type)

if is_comptime {
c.type_resolver.type_map[node.key_var] = key_type
c.type_resolver.update_ct_type(node.key_var, key_type)
node.scope.update_ct_var_kind(node.key_var, .key_var)

defer {
Expand All @@ -195,7 +195,7 @@ fn (mut c Checker) for_in_stmt(mut node ast.ForInStmt) {
node.scope.update_var_type(node.val_var, value_type)

if is_comptime {
c.type_resolver.type_map[node.val_var] = value_type
c.type_resolver.update_ct_type(node.val_var, value_type)
node.scope.update_ct_var_kind(node.val_var, .value_var)

defer {
Expand All @@ -220,7 +220,7 @@ fn (mut c Checker) for_in_stmt(mut node ast.ForInStmt) {
node.scope.update_var_type(node.key_var, key_type)

if is_comptime {
c.type_resolver.type_map[node.key_var] = key_type
c.type_resolver.update_ct_type(node.key_var, key_type)
node.scope.update_ct_var_kind(node.key_var, .key_var)

defer {
Expand Down Expand Up @@ -279,7 +279,7 @@ fn (mut c Checker) for_in_stmt(mut node ast.ForInStmt) {
node.val_type = value_type
node.scope.update_var_type(node.val_var, value_type)
if is_comptime {
c.type_resolver.type_map[node.val_var] = value_type
c.type_resolver.update_ct_type(node.val_var, value_type)
node.scope.update_ct_var_kind(node.val_var, .value_var)

defer {
Expand Down
11 changes: 6 additions & 5 deletions vlib/v/checker/if.v
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
c.comptime.comptime_for_enum_var,
] {
if left.field_name == 'typ' {
skip_state = c.check_compatible_types(c.type_resolver.type_map['${comptime_field_name}.typ'],
right as ast.TypeNode)
skip_state = c.check_compatible_types(c.type_resolver.get_ct_type_or_default('${comptime_field_name}.typ',
ast.void_type), right as ast.TypeNode)
}
}
} else if left is ast.TypeNode {
Expand Down Expand Up @@ -387,9 +387,9 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
}
if comptime_field_name.len > 0 {
if comptime_field_name == c.comptime.comptime_for_method_var {
c.type_resolver.type_map[comptime_field_name] = c.comptime.comptime_for_method_ret_type
c.type_resolver.update_ct_type(comptime_field_name, c.comptime.comptime_for_method_ret_type)
} else if comptime_field_name == c.comptime.comptime_for_field_var {
c.type_resolver.type_map[comptime_field_name] = c.comptime.comptime_for_field_type
c.type_resolver.update_ct_type(comptime_field_name, c.comptime.comptime_for_field_type)
}
}
c.skip_flags = cur_skip_flags
Expand Down Expand Up @@ -608,7 +608,8 @@ fn (mut c Checker) smartcast_if_conds(mut node ast.Expr, mut scope ast.Scope, co
ast.Ident {
if right_expr.name == c.comptime.comptime_for_variant_var {
is_comptime = true
c.type_resolver.type_map['${c.comptime.comptime_for_variant_var}.typ']
c.type_resolver.get_ct_type_or_default('${c.comptime.comptime_for_variant_var}.typ',
ast.no_type)
} else {
c.error('invalid type `${right_expr}`', right_expr.pos)
ast.no_type
Expand Down
3 changes: 2 additions & 1 deletion vlib/v/checker/infix.v
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,8 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
}
ast.Ident {
if right_expr.name == c.comptime.comptime_for_variant_var {
c.type_resolver.type_map['${c.comptime.comptime_for_variant_var}.typ']
c.type_resolver.get_ct_type_or_default('${c.comptime.comptime_for_variant_var}.typ',
ast.void_type)
} else {
c.error('invalid type `${right_expr}`', right_expr.pos)
ast.no_type
Expand Down
19 changes: 9 additions & 10 deletions vlib/v/gen/c/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -308,17 +308,19 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
key_str := g.comptime.get_comptime_selector_key_type(val)
if key_str != '' {
if is_decl {
var_type = g.type_resolver.type_map[key_str] or { var_type }
var_type = g.type_resolver.get_ct_type_or_default(key_str,
var_type)
val_type = var_type
left.obj.typ = var_type
} else {
val_type = g.type_resolver.type_map[key_str] or { var_type }
val_type = g.type_resolver.get_ct_type_or_default(key_str,
var_type)
}
g.assign_ct_type = var_type
}
} else if val is ast.ComptimeCall {
key_str := '${val.method_name}.return_type'
var_type = g.type_resolver.type_map[key_str] or { var_type }
var_type = g.type_resolver.get_ct_type_or_default(key_str, var_type)
left.obj.typ = var_type
g.assign_ct_type = var_type
} else if is_decl && val is ast.Ident && val.info is ast.IdentVar {
Expand All @@ -331,7 +333,7 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
} else if val is ast.DumpExpr && val.expr is ast.ComptimeSelector {
key_str := g.comptime.get_comptime_selector_key_type(val.expr as ast.ComptimeSelector)
if key_str != '' {
var_type = g.type_resolver.type_map[key_str] or { var_type }
var_type = g.type_resolver.get_ct_type_or_default(key_str, var_type)
val_type = var_type
left.obj.typ = var_type
}
Expand All @@ -353,15 +355,12 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
var_type = fn_ret_type
val_type = var_type
left.obj.typ = var_type
g.type_resolver.type_map['g.${left.name}.${left.obj.pos.pos}'] = var_type
// eprintln('>> ${func.name} > resolve ${left.name}.${left.obj.pos.pos}.generic to ${g.table.type_to_str(var_type)}')
}
} else if val.is_static_method && val.left_type.has_flag(.generic) {
fn_ret_type := g.resolve_return_type(val)
var_type = fn_ret_type
val_type = var_type
left.obj.typ = var_type
g.type_resolver.type_map['g.${left.name}.${left.obj.pos.pos}'] = var_type
g.assign_ct_type = var_type
}
}
Expand All @@ -370,21 +369,21 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
} else if mut left is ast.ComptimeSelector {
key_str := g.comptime.get_comptime_selector_key_type(left)
if key_str != '' {
var_type = g.type_resolver.type_map[key_str] or { var_type }
var_type = g.type_resolver.get_ct_type_or_default(key_str, var_type)
}
g.assign_ct_type = var_type
if val is ast.ComptimeSelector {
key_str_right := g.comptime.get_comptime_selector_key_type(val)
if key_str_right != '' {
val_type = g.type_resolver.type_map[key_str_right] or { var_type }
val_type = g.type_resolver.get_ct_type_or_default(key_str_right, var_type)
}
} else if val is ast.CallExpr {
g.assign_ct_type = g.comptime.comptime_for_field_type
}
} else if mut left is ast.IndexExpr && val is ast.ComptimeSelector {
key_str := g.comptime.get_comptime_selector_key_type(val)
if key_str != '' {
val_type = g.type_resolver.type_map[key_str] or { var_type }
val_type = g.type_resolver.get_ct_type_or_default(key_str, var_type)
}
g.assign_ct_type = val_type
}
Expand Down
11 changes: 7 additions & 4 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -2718,7 +2718,9 @@ fn (mut g Gen) call_cfn_for_casting_expr(fname string, expr ast.Expr, exp_is_ptr
if got_styp == 'none' && !g.cur_fn.return_type.has_flag(.option) {
g.write('(none){EMPTY_STRUCT_INITIALIZATION}')
} else if is_comptime_variant {
g.write(g.type_default(g.type_resolver.type_map['${g.comptime.comptime_for_variant_var}.typ']))
ctyp := g.type_resolver.get_ct_type_or_default('${g.comptime.comptime_for_variant_var}.typ',
ast.void_type)
g.write(g.type_default(ctyp))
} else {
g.expr(expr)
}
Expand Down Expand Up @@ -4337,7 +4339,8 @@ fn (mut g Gen) debugger_stmt(node ast.DebuggerStmt) {

dot := if obj.orig_type.is_ptr() || obj.is_auto_heap { '->' } else { '.' }
if obj.ct_type_var == .smartcast {
cur_variant_sym := g.table.sym(g.unwrap_generic(g.type_resolver.type_map['${g.comptime.comptime_for_variant_var}.typ']))
cur_variant_sym := g.table.sym(g.unwrap_generic(g.type_resolver.get_ct_type_or_default('${g.comptime.comptime_for_variant_var}.typ',
ast.void_type)))
param_var.write_string('${obj.name}${dot}_${cur_variant_sym.cname}')
} else if cast_sym.info is ast.Aggregate {
sym := g.table.sym(cast_sym.info.types[g.aggregate_type_idx])
Expand Down Expand Up @@ -5141,8 +5144,8 @@ fn (mut g Gen) cast_expr(node ast.CastExpr) {
if node_typ_is_option && node.expr is ast.None {
g.gen_option_error(node.typ, node.expr)
} else if node.expr is ast.Ident && g.comptime.is_comptime_variant_var(node.expr) {
g.expr_with_cast(node.expr, g.type_resolver.type_map['${g.comptime.comptime_for_variant_var}.typ'],
node_typ)
g.expr_with_cast(node.expr, g.type_resolver.get_ct_type_or_default('${g.comptime.comptime_for_variant_var}.typ',
ast.void_type), node_typ)
} else if node_typ_is_option {
g.expr_with_opt(node.expr, expr_type, node.typ)
} else {
Expand Down
Loading
Loading