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: reduce table.sym() consecutive calls by using new table.sym2() #22882

Closed
wants to merge 2 commits into from
Closed
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
25 changes: 19 additions & 6 deletions vlib/v/ast/table.v
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,7 @@ pub fn (t &Table) is_same_method(f &Fn, func &Fn) string {
// don't check receiver for `.typ`
has_unexpected_type := i > 0 && f.params[i].typ != func.params[i].typ
// temporary hack for JS ifaces
lsym := t.sym(f.params[i].typ)
rsym := t.sym(func.params[i].typ)
lsym, rsym := t.sym2(f.params[i].typ, func.params[i].typ)
if lsym.language == .js && rsym.language == .js {
return ''
}
Expand Down Expand Up @@ -738,6 +737,22 @@ pub fn (t &Table) sym(typ Type) &TypeSymbol {
return invalid_type_symbol
}

@[direct_array_access]
pub fn (t &Table) sym2(typ Type, typ2 Type) (&TypeSymbol, &TypeSymbol) {
idx, idx2 := typ.idx(), typ2.idx()
if idx > 0 && idx2 > 0 && idx < t.type_symbols.len && idx2 < t.type_symbols.len {
return t.type_symbols[idx], t.type_symbols[idx2]
}
// this should never happen
if idx <= 0 {
t.panic('table.sym2: invalid type (typ=${typ} idx=${idx}). Compiler bug. This should never happen. Please report the bug using `v bug file.v`.')
}
if idx2 <= 0 {
t.panic('table.sym2: invalid type (typ2=${typ} idx2=${idx}). Compiler bug. This should never happen. Please report the bug using `v bug file.v`.')
}
return invalid_type_symbol, invalid_type_symbol
}

// final_sym follows aliases until it gets to a "real" Type
@[direct_array_access]
pub fn (t &Table) final_sym(typ Type) &TypeSymbol {
Expand Down Expand Up @@ -1055,8 +1070,7 @@ pub fn (t &Table) thread_cname(return_type Type) string {
// e. g. map[string]int
@[inline]
pub fn (t &Table) map_name(key_type Type, value_type Type) string {
key_type_sym := t.sym(key_type)
value_type_sym := t.sym(value_type)
key_type_sym, value_type_sym := t.sym2(key_type, value_type)
ptr := if value_type.is_ptr() { '&'.repeat(value_type.nr_muls()) } else { '' }
opt := if value_type.has_flag(.option) { '?' } else { '' }
res := if value_type.has_flag(.result) { '!' } else { '' }
Expand All @@ -1065,8 +1079,7 @@ pub fn (t &Table) map_name(key_type Type, value_type Type) string {

@[inline]
pub fn (t &Table) map_cname(key_type Type, value_type Type) string {
key_type_sym := t.sym(key_type)
value_type_sym := t.sym(value_type)
key_type_sym, value_type_sym := t.sym2(key_type, value_type)
suffix := if value_type.is_ptr() { '_ptr'.repeat(value_type.nr_muls()) } else { '' }
opt := if value_type.has_flag(.option) { '_option_' } else { '' }
res := if value_type.has_flag(.result) { '_result_' } else { '' }
Expand Down
4 changes: 1 addition & 3 deletions vlib/v/checker/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -542,9 +542,7 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
if left_type_unwrapped == 0 {
continue
}
left_sym := c.table.sym(left_type_unwrapped)
right_sym := c.table.sym(right_type_unwrapped)

left_sym, right_sym := c.table.sym2(left_type_unwrapped, right_type_unwrapped)
if left_sym.kind == .array && !c.inside_unsafe && right_sym.kind == .array
&& left is ast.Ident && !left.is_blank_ident() && right in [ast.Ident, ast.SelectorExpr]
&& ((node.op == .decl_assign && left.is_mut) || node.op == .assign) {
Expand Down
38 changes: 21 additions & 17 deletions vlib/v/checker/check_types.v
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ fn (mut c Checker) check_types(got ast.Type, expected ast.Type) bool {
if (expected == ast.rune_type && got_is_int) || (got == ast.rune_type && exp_is_int) {
return true
}
got_sym := c.table.sym(got)
expected_sym := c.table.sym(expected)
got_sym, expected_sym := c.table.sym2(got, expected)

// Allow `[N]anyptr` as `[N]anyptr`
if got_sym.info is ast.Array && expected_sym.info is ast.Array {
Expand Down Expand Up @@ -132,8 +131,7 @@ fn (mut c Checker) check_types(got ast.Type, expected ast.Type) bool {

if (exp_idx == ast.nil_type_idx && got_idx == ast.string_type_idx)
|| (got_idx == ast.nil_type_idx && exp_idx == ast.string_type_idx) {
got_sym := c.table.sym(got)
exp_sym := c.table.sym(expected)
got_sym, exp_sym := c.table.sym2(got, expected)

if expected.is_ptr() || got.is_ptr() {
return true
Expand Down Expand Up @@ -340,8 +338,7 @@ fn (mut c Checker) check_expected_call_arg(got_ ast.Type, expected_ ast.Type, la
return
}
} else {
got_typ_sym := c.table.sym(c.unwrap_generic(got))
expected_typ_sym := c.table.sym(c.unwrap_generic(expected))
got_typ_sym, expected_typ_sym := c.table.sym2(c.unwrap_generic(got), c.unwrap_generic(expected))
if expected_typ_sym.kind == .interface && c.type_implements(got, expected, token.Pos{}) {
return
}
Expand Down Expand Up @@ -418,7 +415,7 @@ fn (mut c Checker) check_basic(got ast.Type, expected ast.Type) bool {
|| (got.idx() == ast.array_type_idx && c.table.final_sym(expected).kind == .array) {
return true
}
got_sym, exp_sym := c.table.sym(got), c.table.sym(expected)
got_sym, exp_sym := c.table.sym2(got, expected)
// multi return
if exp_sym.kind == .multi_return && got_sym.kind == .multi_return {
exp_types := exp_sym.mr_info().types
Expand Down Expand Up @@ -570,8 +567,7 @@ fn (mut c Checker) check_shift(mut node ast.InfixExpr, left_type_ ast.Type, righ
return ast.void_type
}
if !right_type.is_int() && !c.pref.translated {
left_sym := c.table.sym(left_type)
right_sym := c.table.sym(right_type)
left_sym, right_sym := c.table.sym2(left_type, right_type)
c.error('cannot shift non-integer type `${right_sym.name}` into type `${left_sym.name}`',
node.right.pos())
return ast.void_type
Expand Down Expand Up @@ -795,12 +791,14 @@ fn (mut c Checker) infer_struct_generic_types(typ ast.Type, node ast.StructInit)
init_sym := c.table.sym(t.typ)
if init_sym.info is ast.Array {
mut init_elem_typ, mut field_elem_typ := init_sym.info.elem_type, field_sym.info.elem_type
mut init_elem_sym, mut field_elem_sym := c.table.sym(init_elem_typ), c.table.sym(field_elem_typ)
mut init_elem_sym, mut field_elem_sym := c.table.sym2(init_elem_typ,
field_elem_typ)
for {
if mut init_elem_sym.info is ast.Array
&& mut field_elem_sym.info is ast.Array {
init_elem_typ, field_elem_typ = init_elem_sym.info.elem_type, field_elem_sym.info.elem_type
init_elem_sym, field_elem_sym = c.table.sym(init_elem_typ), c.table.sym(field_elem_typ)
init_elem_sym, field_elem_sym = c.table.sym2(init_elem_typ,
field_elem_typ)
} else {
if field_elem_sym.name == gt_name {
mut elem_typ := init_elem_typ
Expand All @@ -823,12 +821,14 @@ fn (mut c Checker) infer_struct_generic_types(typ ast.Type, node ast.StructInit)
init_sym := c.table.sym(t.typ)
if init_sym.info is ast.ArrayFixed {
mut init_elem_typ, mut field_elem_typ := init_sym.info.elem_type, field_sym.info.elem_type
mut init_elem_sym, mut field_elem_sym := c.table.sym(init_elem_typ), c.table.sym(field_elem_typ)
mut init_elem_sym, mut field_elem_sym := c.table.sym2(init_elem_typ,
field_elem_typ)
for {
if mut init_elem_sym.info is ast.ArrayFixed
&& mut field_elem_sym.info is ast.ArrayFixed {
init_elem_typ, field_elem_typ = init_elem_sym.info.elem_type, field_elem_sym.info.elem_type
init_elem_sym, field_elem_sym = c.table.sym(init_elem_typ), c.table.sym(field_elem_typ)
init_elem_sym, field_elem_sym = c.table.sym2(init_elem_typ,
field_elem_typ)
} else {
if field_elem_sym.name == gt_name {
mut elem_typ := init_elem_typ
Expand Down Expand Up @@ -1036,14 +1036,16 @@ fn (mut c Checker) infer_fn_generic_types(func &ast.Fn, mut node ast.CallExpr) {
typ = ast.mktyp(arg_typ)
} else if arg_sym.info is ast.Array && param_sym.info is ast.Array {
mut arg_elem_typ, mut param_elem_typ := arg_sym.info.elem_type, param_sym.info.elem_type
mut arg_elem_sym, mut param_elem_sym := c.table.sym(arg_elem_typ), c.table.sym(param_elem_typ)
mut arg_elem_sym, mut param_elem_sym := c.table.sym2(arg_elem_typ,
param_elem_typ)
for {
if mut arg_elem_sym.info is ast.Array
&& mut param_elem_sym.info is ast.Array
&& c.table.cur_fn != unsafe { nil }
&& param_elem_sym.name !in c.table.cur_fn.generic_names {
arg_elem_typ, param_elem_typ = arg_elem_sym.info.elem_type, param_elem_sym.info.elem_type
arg_elem_sym, param_elem_sym = c.table.sym(arg_elem_typ), c.table.sym(param_elem_typ)
arg_elem_sym, param_elem_sym = c.table.sym2(arg_elem_typ,
param_elem_typ)
} else {
if param_elem_sym.name == gt_name {
typ = arg_elem_typ
Expand All @@ -1056,14 +1058,16 @@ fn (mut c Checker) infer_fn_generic_types(func &ast.Fn, mut node ast.CallExpr) {
}
} else if arg_sym.info is ast.ArrayFixed && param_sym.info is ast.ArrayFixed {
mut arg_elem_typ, mut param_elem_typ := arg_sym.info.elem_type, param_sym.info.elem_type
mut arg_elem_sym, mut param_elem_sym := c.table.sym(arg_elem_typ), c.table.sym(param_elem_typ)
mut arg_elem_sym, mut param_elem_sym := c.table.sym2(arg_elem_typ,
param_elem_typ)
for {
if mut arg_elem_sym.info is ast.ArrayFixed
&& mut param_elem_sym.info is ast.ArrayFixed
&& c.table.cur_fn != unsafe { nil }
&& param_elem_sym.name !in c.table.cur_fn.generic_names {
arg_elem_typ, param_elem_typ = arg_elem_sym.info.elem_type, param_elem_sym.info.elem_type
arg_elem_sym, param_elem_sym = c.table.sym(arg_elem_typ), c.table.sym(param_elem_typ)
arg_elem_sym, param_elem_sym = c.table.sym2(arg_elem_typ,
param_elem_typ)
} else {
if param_elem_sym.name == gt_name {
typ = arg_elem_typ
Expand Down
9 changes: 3 additions & 6 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -1041,8 +1041,7 @@ fn (mut c Checker) type_implements(typ ast.Type, interface_type ast.Type, pos to
}
utyp := c.unwrap_generic(typ)
styp := c.table.type_to_str(utyp)
typ_sym := c.table.sym(utyp)
mut inter_sym := c.table.sym(interface_type)
typ_sym, mut inter_sym := c.table.sym2(utyp, interface_type)
if !inter_sym.is_pub && inter_sym.mod !in [typ_sym.mod, c.mod] && typ_sym.mod != 'builtin' {
c.error('`${styp}` cannot implement private interface `${inter_sym.name}` of other module',
pos)
Expand Down Expand Up @@ -2859,8 +2858,7 @@ pub fn (mut c Checker) expr(mut node ast.Expr) ast.Type {
}
ast.AsCast {
node.expr_type = c.expr(mut node.expr)
expr_type_sym := c.table.sym(node.expr_type)
type_sym := c.table.sym(c.unwrap_generic(node.typ))
expr_type_sym, type_sym := c.table.sym2(node.expr_type, c.unwrap_generic(node.typ))
if !c.is_builtin_mod {
c.table.used_features.as_cast = true
}
Expand Down Expand Up @@ -4554,8 +4552,7 @@ fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) ast.Type {
c.error('should not create object instance on the heap to simply access a member',
node.pos.extend(node.right.pos))
}
right_sym := c.table.sym(right_type)
expr_sym := c.table.sym(node.right.expr_type)
right_sym, expr_sym := c.table.sym2(right_type, node.right.expr_type)
if expr_sym.kind == .struct && (expr_sym.info as ast.Struct).is_minify
&& (node.right.typ == ast.bool_type_idx || (right_sym.kind == .enum
&& !(right_sym.info as ast.Enum).is_flag
Expand Down
7 changes: 3 additions & 4 deletions vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -1669,8 +1669,8 @@ 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() {
continue
}
unaliased_param_sym := c.table.sym(c.table.unaliased_type(param_type))
unaliased_arg_sym := c.table.sym(c.table.unaliased_type(arg_typ))
unaliased_param_sym, unaliased_arg_sym := c.table.sym2(c.table.unaliased_type(param_type),
c.table.unaliased_type(arg_typ))
// Allow `[32]i8` as `&i8` etc
if ((unaliased_arg_sym.kind == .array_fixed || unaliased_arg_sym.kind == .array)
&& (param_is_number
Expand Down Expand Up @@ -2722,8 +2722,7 @@ fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
// if arg_typ_sym.kind == .string && typ_sym.has_method('str') {
// continue
// }
param_typ_sym := c.table.sym(exp_arg_typ)
arg_typ_sym := c.table.sym(got_arg_typ)
param_typ_sym, arg_typ_sym := c.table.sym2(exp_arg_typ, got_arg_typ)
if param_typ_sym.info is ast.Array && arg_typ_sym.info is ast.Array {
param_elem_type := c.table.unaliased_type(param_typ_sym.info.elem_type)
arg_elem_type := c.table.unaliased_type(arg_typ_sym.info.elem_type)
Expand Down
3 changes: 1 addition & 2 deletions vlib/v/checker/infix.v
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,7 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
c.check_div_mod_by_zero(node.right, node.op)
}

left_sym = c.table.sym(unwrapped_left_type)
right_sym = c.table.sym(unwrapped_right_type)
left_sym, right_sym = c.table.sym2(unwrapped_left_type, unwrapped_right_type)
if left_sym.info is ast.Alias && left_final_sym.is_primitive() {
if left_sym.has_method(node.op.str()) {
if method := left_sym.find_method(node.op.str()) {
Expand Down
22 changes: 11 additions & 11 deletions vlib/v/checker/interface.v
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,7 @@ fn (mut c Checker) interface_decl(mut node ast.InterfaceDecl) {

fn (mut c Checker) unwrap_generic_interface(typ ast.Type, interface_type ast.Type, pos token.Pos) ast.Type {
utyp := c.unwrap_generic(typ)
typ_sym := c.table.sym(utyp)
mut inter_sym := c.table.sym(interface_type)

typ_sym, mut inter_sym := c.table.sym2(utyp, interface_type)
if mut inter_sym.info is ast.Interface {
if inter_sym.info.is_generic {
mut inferred_types := []ast.Type{}
Expand All @@ -262,8 +260,7 @@ fn (mut c Checker) unwrap_generic_interface(typ ast.Type, interface_type ast.Typ
return 0
}
if imethod.return_type.has_flag(.generic) {
imret_sym := c.table.sym(imethod.return_type)
mret_sym := c.table.sym(method.return_type)
imret_sym, mret_sym := c.table.sym2(imethod.return_type, method.return_type)
if method.return_type == ast.void_type
&& imethod.return_type != method.return_type {
c.error('interface method `${imethod.name}` returns `${imret_sym.name}`, but implementation method `${method.name}` returns no value',
Expand Down Expand Up @@ -323,18 +320,19 @@ fn (mut c Checker) unwrap_generic_interface(typ ast.Type, interface_type ast.Typ
need_inferred_type = true
}
if iparam.typ.has_flag(.generic) || need_inferred_type {
param_sym := c.table.sym(iparam.typ)
arg_sym := c.table.sym(param.typ)
param_sym, arg_sym := c.table.sym2(iparam.typ, param.typ)
if c.table.get_type_name(iparam.typ) == gt_name || need_inferred_type {
inferred_type = param.typ
} else if arg_sym.info is ast.Array && param_sym.info is ast.Array {
mut arg_elem_typ, mut param_elem_typ := arg_sym.info.elem_type, param_sym.info.elem_type
mut arg_elem_sym, mut param_elem_sym := c.table.sym(arg_elem_typ), c.table.sym(param_elem_typ)
mut arg_elem_sym, mut param_elem_sym := c.table.sym2(arg_elem_typ,
param_elem_typ)
for {
if mut arg_elem_sym.info is ast.Array
&& mut param_elem_sym.info is ast.Array {
arg_elem_typ, param_elem_typ = arg_elem_sym.info.elem_type, param_elem_sym.info.elem_type
arg_elem_sym, param_elem_sym = c.table.sym(arg_elem_typ), c.table.sym(param_elem_typ)
arg_elem_sym, param_elem_sym = c.table.sym2(arg_elem_typ,
param_elem_typ)
} else {
if param_elem_sym.name == gt_name {
inferred_type = arg_elem_typ
Expand All @@ -345,12 +343,14 @@ fn (mut c Checker) unwrap_generic_interface(typ ast.Type, interface_type ast.Typ
} else if arg_sym.info is ast.ArrayFixed
&& param_sym.info is ast.ArrayFixed {
mut arg_elem_typ, mut param_elem_typ := arg_sym.info.elem_type, param_sym.info.elem_type
mut arg_elem_sym, mut param_elem_sym := c.table.sym(arg_elem_typ), c.table.sym(param_elem_typ)
mut arg_elem_sym, mut param_elem_sym := c.table.sym2(arg_elem_typ,
param_elem_typ)
for {
if mut arg_elem_sym.info is ast.ArrayFixed
&& mut param_elem_sym.info is ast.ArrayFixed {
arg_elem_typ, param_elem_typ = arg_elem_sym.info.elem_type, param_elem_sym.info.elem_type
arg_elem_sym, param_elem_sym = c.table.sym(arg_elem_typ), c.table.sym(param_elem_typ)
arg_elem_sym, param_elem_sym = c.table.sym2(arg_elem_typ,
param_elem_typ)
} else {
if param_elem_sym.name == gt_name {
inferred_type = arg_elem_typ
Expand Down
3 changes: 1 addition & 2 deletions vlib/v/checker/match.v
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type {
}
}
if stmt.typ != ast.error_type {
ret_sym := c.table.sym(ret_type)
stmt_sym := c.table.sym(stmt.typ)
ret_sym, stmt_sym := c.table.sym2(ret_type, stmt.typ)
if ret_sym.kind !in [.sum_type, .interface]
&& stmt_sym.kind in [.sum_type, .interface] {
c.error('return type mismatch, it should be `${ret_sym.name}`',
Expand Down
3 changes: 1 addition & 2 deletions vlib/v/checker/return.v
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,7 @@ fn (mut c Checker) return_stmt(mut node ast.Return) {
exprv.pos)
}
if node.exprs[expr_idxs[i]] !is ast.ComptimeCall {
got_type_sym := c.table.sym(got_type)
exp_type_sym := c.table.sym(exp_type)
got_type_sym, exp_type_sym := c.table.sym2(got_type, exp_type)
pos := node.exprs[expr_idxs[i]].pos()
if c.check_types(got_type, exp_type) {
if exp_type.is_unsigned() && got_type.is_int_literal() {
Expand Down
9 changes: 3 additions & 6 deletions vlib/v/checker/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
}

// disallow map `mut a = b`
field_sym := c.table.sym(field.typ)
expr_sym := c.table.sym(field.default_expr_typ)
field_sym, expr_sym := c.table.sym2(field.typ, field.default_expr_typ)
if field_sym.kind == .map && expr_sym.kind == .map && field.default_expr.is_lvalue()
&& field.is_mut
&& (!field.default_expr_typ.is_ptr() || field.default_expr is ast.Ident) {
Expand Down Expand Up @@ -389,8 +388,7 @@ fn minify_sort_fn(a &ast.StructField, b &ast.StructField) int {
}

mut t := global_table
a_sym := t.sym(a.typ)
b_sym := t.sym(b.typ)
a_sym, b_sym := t.sym2(a.typ, b.typ)

// push all non-flag enums to the end too, just before the bool fields
// TODO: support enums with custom field values as well
Expand Down Expand Up @@ -853,8 +851,7 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
s := c.table.type_to_str(update_type)
c.error('expected struct, found `${s}`', node.update_expr.pos())
} else if update_type != node.typ {
from_sym := c.table.sym(update_type)
to_sym := c.table.sym(node.typ)
from_sym, to_sym := c.table.sym2(update_type, node.typ)
from_info := from_sym.info as ast.Struct
to_info := to_sym.info as ast.Struct
// TODO: this check is too strict
Expand Down
Loading
Loading