Skip to content

Commit

Permalink
ast,checker,cgen: do some minor optimizations (#23306)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Dec 29, 2024
1 parent 7b9414a commit 1607579
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion vlib/v/ast/types.v
Original file line number Diff line number Diff line change
Expand Up @@ -1827,10 +1827,10 @@ pub fn (t &TypeSymbol) str_method_info() (bool, bool, int) {

pub fn (t &TypeSymbol) find_field(name string) ?StructField {
match t.info {
Aggregate { return t.info.find_field(name) }
Struct { return t.info.find_field(name) }
Interface { return t.info.find_field(name) }
SumType { return t.info.find_sum_type_field(name) }
Aggregate { return t.info.find_field(name) }
else { return none }
}
}
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -1706,8 +1706,8 @@ fn (mut c Checker) selector_expr(mut node ast.SelectorExpr) ast.Type {
mut unknown_field_msg := ''
mut has_field := false
mut field := ast.StructField{}
if field_name.len > 0 && field_name[0].is_capital() && sym.info is ast.Struct
&& sym.language == .v {
if field_name.len > 0 && sym.info is ast.Struct && sym.language == .v
&& field_name[0].is_capital() {
// x.Foo.y => access the embedded struct
for embed in sym.info.embeds {
embed_sym := c.table.sym(embed)
Expand Down
16 changes: 8 additions & 8 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -3895,7 +3895,8 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
g.checker_bug('unexpected SelectorExpr.expr_type = 0', node.pos)
}

sym := g.table.sym(g.unwrap_generic(node.expr_type))
unwrapped_expr_type := g.unwrap_generic(node.expr_type)
sym := g.table.sym(unwrapped_expr_type)
field_name := if sym.language == .v { c_name(node.field_name) } else { node.field_name }
is_as_cast := node.expr is ast.AsCast
if is_as_cast {
Expand Down Expand Up @@ -3950,7 +3951,7 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
opt_base_typ := g.base_type(node.expr_type)
g.write('(*(${opt_base_typ}*)')
}
final_sym := g.table.final_sym(g.unwrap_generic(node.expr_type))
final_sym := g.table.final_sym(unwrapped_expr_type)
if final_sym.kind == .array_fixed {
if node.field_name != 'len' {
g.error('field_name should be `len`', node.pos)
Expand All @@ -3972,9 +3973,6 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
mut sum_type_dot := '.'
mut field_typ := ast.void_type
mut is_option_unwrap := false
mut is_dereferenced := node.expr is ast.SelectorExpr && node.expr.expr_type.is_ptr()
&& !node.expr.typ.is_ptr()
&& g.table.final_sym(node.expr.expr_type).kind in [.interface, .sum_type]
if f := g.table.find_field_with_embeds(sym, node.field_name) {
field_sym := g.table.sym(f.typ)
field_typ = f.typ
Expand Down Expand Up @@ -4140,8 +4138,9 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
}
}
alias_to_ptr := sym.info is ast.Alias && sym.info.parent_type.is_ptr()
if field_is_opt
|| (((!is_dereferenced && g.unwrap_generic(node.expr_type).is_ptr())
is_dereferenced := node.expr is ast.SelectorExpr && node.expr.expr_type.is_ptr()
&& !node.expr.typ.is_ptr() && final_sym.kind in [.interface, .sum_type]
if field_is_opt || (((!is_dereferenced && unwrapped_expr_type.is_ptr())
|| sym.kind == .chan || alias_to_ptr) && node.from_embed_types.len == 0)
|| (node.expr.is_as_cast() && g.inside_smartcast) {
g.write('->')
Expand All @@ -4164,7 +4163,8 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
g.write('data))')
}
if sum_type_deref_field != '' {
g.write('${sum_type_dot}${sum_type_deref_field})')
g.write2(sum_type_dot, sum_type_deref_field)
g.write(')')
}
if sym.kind in [.interface, .sum_type] {
g.write('))')
Expand Down

0 comments on commit 1607579

Please sign in to comment.