Skip to content

Commit

Permalink
v: cleanup ComptimeSelector .typ key generation (#23308)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Dec 30, 2024
1 parent 9b31800 commit 9f11638
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 46 deletions.
2 changes: 2 additions & 0 deletions vlib/v/ast/ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -1994,6 +1994,8 @@ pub mut:
left_type Type
field_expr Expr
typ Type
is_name bool // true if f.$(field.name)
typ_key string // `f.typ` cached key for type resolver
}

@[minify]
Expand Down
4 changes: 4 additions & 0 deletions vlib/v/checker/comptime.v
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ fn (mut c Checker) comptime_selector(mut node ast.ComptimeSelector) ast.Type {
c.error('compile time field access can only be used when iterating over `T.fields`',
left_pos)
}
node.is_name = node.field_expr.field_name == 'name'
if mut node.field_expr.expr is ast.Ident {
node.typ_key = '${node.field_expr.expr.name}.typ'
}
expr_type = c.type_resolver.get_comptime_selector_type(node, ast.void_type)
if expr_type != ast.void_type {
return expr_type
Expand Down
38 changes: 18 additions & 20 deletions vlib/v/gen/c/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -305,15 +305,14 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
g.assign_ct_type = var_type
}
} else if val is ast.ComptimeSelector {
key_str := g.comptime.get_comptime_selector_key_type(val)
if key_str != '' {
if val.typ_key != '' {
if is_decl {
var_type = g.type_resolver.get_ct_type_or_default(key_str,
var_type = g.type_resolver.get_ct_type_or_default(val.typ_key,
var_type)
val_type = var_type
left.obj.typ = var_type
} else {
val_type = g.type_resolver.get_ct_type_or_default(key_str,
val_type = g.type_resolver.get_ct_type_or_default(val.typ_key,
var_type)
}
g.assign_ct_type = var_type
Expand All @@ -330,14 +329,16 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
var_type = val_type.clear_flag(.option)
left.obj.typ = var_type
}
} 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.get_ct_type_or_default(key_str, var_type)
val_type = var_type
left.obj.typ = var_type
} else if val is ast.DumpExpr {
if val.expr is ast.ComptimeSelector {
if val.expr.typ_key != '' {
var_type = g.type_resolver.get_ct_type_or_default(val.expr.typ_key,
var_type)
val_type = var_type
left.obj.typ = var_type
}
g.assign_ct_type = var_type
}
g.assign_ct_type = var_type
} else if val is ast.IndexExpr {
if val.left is ast.Ident && g.type_resolver.is_generic_param_var(val.left) {
ctyp := g.unwrap_generic(g.get_gn_var_type(val.left))
Expand Down Expand Up @@ -374,23 +375,20 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
is_auto_heap = left.obj.is_auto_heap
}
} 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.get_ct_type_or_default(key_str, var_type)
if left.typ_key != '' {
var_type = g.type_resolver.get_ct_type_or_default(left.typ_key, 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.get_ct_type_or_default(key_str_right, var_type)
if val.typ_key != '' {
val_type = g.type_resolver.get_ct_type_or_default(val.typ_key, 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.get_ct_type_or_default(key_str, var_type)
if val.typ_key != '' {
val_type = g.type_resolver.get_ct_type_or_default(val.typ_key, var_type)
}
g.assign_ct_type = val_type
}
Expand Down
5 changes: 2 additions & 3 deletions vlib/v/gen/c/comptime.v
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ fn (mut g Gen) comptime_selector(node ast.ComptimeSelector) {
g.write('.')
}
// check for field.name
if node.field_expr is ast.SelectorExpr {
if node.is_name && node.field_expr is ast.SelectorExpr {
if node.field_expr.expr is ast.Ident {
if node.field_expr.expr.name == g.comptime.comptime_for_field_var
&& node.field_expr.field_name == 'name' {
if node.field_expr.expr.name == g.comptime.comptime_for_field_var {
_, field_name := g.type_resolver.get_comptime_selector_var_type(node)
g.write(c_name(field_name))
if is_interface_field {
Expand Down
7 changes: 3 additions & 4 deletions vlib/v/gen/c/dumpexpr.v
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ fn (mut g Gen) dump_expr(node ast.DumpExpr) {
}
}
}
// var.${field.name}
if node.expr is ast.ComptimeSelector {
// var.$(field.name)
if node.expr is ast.ComptimeSelector && node.expr.is_name {
if node.expr.field_expr is ast.SelectorExpr {
if node.expr.field_expr.expr is ast.Ident {
if node.expr.field_expr.expr.name == g.comptime.comptime_for_field_var
&& node.expr.field_expr.field_name == 'name' {
if node.expr.field_expr.expr.name == g.comptime.comptime_for_field_var {
field, _ := g.type_resolver.get_comptime_selector_var_type(node.expr)
name = g.styp(g.unwrap_generic(field.typ.clear_flags(.shared_f, .result)))
expr_type = field.typ
Expand Down
10 changes: 4 additions & 6 deletions vlib/v/gen/c/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -1302,9 +1302,8 @@ fn (mut g Gen) gen_to_str_method_call(node ast.CallExpr) bool {
}
left_node := node.left
if left_node is ast.ComptimeSelector {
key_str := g.comptime.get_comptime_selector_key_type(left_node)
if key_str != '' {
rec_type = g.type_resolver.get_ct_type_or_default(key_str, rec_type)
if left_node.typ_key != '' {
rec_type = g.type_resolver.get_ct_type_or_default(left_node.typ_key, rec_type)
g.gen_expr_to_string(left_node, rec_type)
return true
}
Expand Down Expand Up @@ -2274,9 +2273,8 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
} else {
g.write('${c_fn_name(print_method)}(')
if expr is ast.ComptimeSelector {
key_str := g.comptime.get_comptime_selector_key_type(expr)
if key_str != '' {
typ = g.type_resolver.get_ct_type_or_default(key_str, typ)
if expr.typ_key != '' {
typ = g.type_resolver.get_ct_type_or_default(expr.typ_key, typ)
}
} else if expr is ast.ComptimeCall {
if expr.method_name == 'method' {
Expand Down
15 changes: 2 additions & 13 deletions vlib/v/type_resolver/comptime_resolver.v
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@ pub fn (mut t TypeResolver) get_comptime_selector_var_type(node ast.ComptimeSele
return field, field_name
}

@[inline]
pub fn (t &ResolverInfo) get_comptime_selector_key_type(val ast.ComptimeSelector) string {
if val.field_expr is ast.SelectorExpr {
if val.field_expr.expr is ast.Ident {
return '${val.field_expr.expr.name}.typ'
}
}
return ''
}

// is_comptime_expr checks if the node is related to a comptime expr
@[inline]
pub fn (t &ResolverInfo) is_comptime_expr(node ast.Expr) bool {
Expand Down Expand Up @@ -130,9 +120,8 @@ pub fn (t &TypeResolver) get_type_from_comptime_var(var ast.Ident) ast.Type {
// get_comptime_selector_type retrieves the var.$(field.name) type when field_name is 'name' otherwise default_type is returned
@[inline]
pub fn (mut t TypeResolver) get_comptime_selector_type(node ast.ComptimeSelector, default_type ast.Type) ast.Type {
if node.field_expr is ast.SelectorExpr
&& t.info.check_comptime_is_field_selector(node.field_expr)
&& node.field_expr.field_name == 'name' {
if node.is_name && node.field_expr is ast.SelectorExpr
&& t.info.check_comptime_is_field_selector(node.field_expr) {
return t.resolver.unwrap_generic(t.info.comptime_for_field_type)
}
return default_type
Expand Down

0 comments on commit 9f11638

Please sign in to comment.