Skip to content

Commit

Permalink
ast, parser, checker, cgen: cleanup ast.ArrayInit (#19526)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 authored Oct 8, 2023
1 parent efef5bc commit e3ba197
Show file tree
Hide file tree
Showing 16 changed files with 82 additions and 82 deletions.
4 changes: 2 additions & 2 deletions cmd/tools/vast/vast.v
Original file line number Diff line number Diff line change
Expand Up @@ -1603,10 +1603,10 @@ fn (t Tree) array_init(node ast.ArrayInit) &Node {
obj.add_terse('mod', t.string_node(node.mod))
obj.add_terse('len_expr', t.expr(node.len_expr))
obj.add_terse('cap_expr', t.expr(node.cap_expr))
obj.add_terse('default_expr', t.expr(node.default_expr))
obj.add_terse('init_expr', t.expr(node.init_expr))
obj.add_terse('has_len', t.bool_node(node.has_len))
obj.add_terse('has_cap', t.bool_node(node.has_cap))
obj.add_terse('has_default', t.bool_node(node.has_default))
obj.add_terse('has_init', t.bool_node(node.has_init))
obj.add_terse('has_index', t.bool_node(node.has_index))
obj.add_terse('expr_types', t.array_node_type(node.expr_types))
obj.add('pos', t.pos(node.pos))
Expand Down
18 changes: 9 additions & 9 deletions vlib/v/ast/ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -1436,17 +1436,17 @@ pub:
mod string
has_len bool
has_cap bool
has_default bool
has_init bool
has_index bool // true if temp variable index is used
pub mut:
exprs []Expr // `[expr, expr]` or `[expr]Type{}` for fixed array
len_expr Expr // len: expr
cap_expr Expr // cap: expr
default_expr Expr // init: expr
expr_types []Type // [Dog, Cat] // also used for interface_types
elem_type Type // element type
default_type Type // default value type
typ Type // array type
exprs []Expr // `[expr, expr]` or `[expr]Type{}` for fixed array
len_expr Expr // len: expr
cap_expr Expr // cap: expr
init_expr Expr // init: expr
expr_types []Type // [Dog, Cat] // also used for interface_types
elem_type Type // element type
init_type Type // init: value type
typ Type // array type
}

pub struct ArrayDecompose {
Expand Down
12 changes: 6 additions & 6 deletions vlib/v/ast/init.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ pub fn (t &Table) resolve_init(node StructInit, typ Type) Expr {
Array {
mut has_len := false
mut has_cap := false
mut has_default := false
mut has_init := false
mut len_expr := empty_expr
mut cap_expr := empty_expr
mut default_expr := empty_expr
mut init_expr := empty_expr
mut exprs := []Expr{}
for field in node.init_fields {
match field.name {
Expand All @@ -22,8 +22,8 @@ pub fn (t &Table) resolve_init(node StructInit, typ Type) Expr {
cap_expr = field.expr
}
'init' {
has_default = true
default_expr = field.expr
has_init = true
init_expr = field.expr
}
else {
exprs << field.expr
Expand All @@ -38,10 +38,10 @@ pub fn (t &Table) resolve_init(node StructInit, typ Type) Expr {
elem_type: sym.info.elem_type
has_len: has_len
has_cap: has_cap
has_default: has_default
has_init: has_init
len_expr: len_expr
cap_expr: cap_expr
default_expr: default_expr
init_expr: init_expr
exprs: exprs
}
}
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/ast/str.v
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,8 @@ pub fn (x Expr) str() string {
if x.has_cap {
fields << 'cap: ${x.cap_expr.str()}'
}
if x.has_default {
fields << 'init: ${x.default_expr.str()}'
if x.has_init {
fields << 'init: ${x.init_expr.str()}'
}
typ_str := global_table.type_to_str(x.elem_type)
if fields.len > 0 {
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/ast/table.v
Original file line number Diff line number Diff line change
Expand Up @@ -2339,7 +2339,7 @@ pub fn (t &Table) dependent_names_in_expr(expr Expr) []string {
}
names << t.dependent_names_in_expr(expr.len_expr)
names << t.dependent_names_in_expr(expr.cap_expr)
names << t.dependent_names_in_expr(expr.default_expr)
names << t.dependent_names_in_expr(expr.init_expr)
}
CallExpr {
for arg in expr.args {
Expand Down
20 changes: 10 additions & 10 deletions vlib/v/checker/containers.v
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
c.check_array_init_para_type('len', mut node.len_expr, node.pos)
}
}
if node.has_default {
if node.has_init {
c.check_array_init_default_expr(mut node)
}
if node.has_len {
len_typ := c.check_expr_opt_call(node.len_expr, c.expr(mut node.len_expr))
if len_typ.has_flag(.option) {
c.error('cannot use unwrapped Option as length', node.len_expr.pos())
}
if node.has_len && !node.has_default {
if node.has_len && !node.has_init {
elem_type_sym := c.table.sym(node.elem_type)
if elem_type_sym.kind == .interface_ {
c.error('cannot instantiate an array of interfaces without also giving a default `init:` value',
Expand Down Expand Up @@ -295,21 +295,21 @@ fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
} else {
node.typ = ast.new_type(idx)
}
if node.has_default {
if node.has_init {
c.check_array_init_default_expr(mut node)
}
}
return node.typ
}

fn (mut c Checker) check_array_init_default_expr(mut node ast.ArrayInit) {
mut default_expr := node.default_expr
default_typ := c.check_expr_opt_call(default_expr, c.expr(mut default_expr))
node.default_type = default_typ
if !node.elem_type.has_flag(.option) && default_typ.has_flag(.option) {
c.error('cannot use unwrapped Option as initializer', default_expr.pos())
mut init_expr := node.init_expr
init_typ := c.check_expr_opt_call(init_expr, c.expr(mut init_expr))
node.init_type = init_typ
if !node.elem_type.has_flag(.option) && init_typ.has_flag(.option) {
c.error('cannot use unwrapped Option as initializer', init_expr.pos())
}
c.check_expected(default_typ, node.elem_type) or { c.error(err.msg(), default_expr.pos()) }
c.check_expected(init_typ, node.elem_type) or { c.error(err.msg(), init_expr.pos()) }
}

fn (mut c Checker) check_array_init_para_type(para string, mut expr ast.Expr, pos token.Pos) {
Expand All @@ -321,7 +321,7 @@ fn (mut c Checker) check_array_init_para_type(para string, mut expr ast.Expr, po

fn (mut c Checker) ensure_sumtype_array_has_default_value(node ast.ArrayInit) {
sym := c.table.sym(node.elem_type)
if sym.kind == .sum_type && !node.has_default {
if sym.kind == .sum_type && !node.has_init {
c.error('cannot initialize sum type array without default value', node.pos)
}
}
Expand Down
12 changes: 6 additions & 6 deletions vlib/v/eval/expr.v
Original file line number Diff line number Diff line change
Expand Up @@ -452,22 +452,22 @@ pub fn (mut e Eval) expr(expr ast.Expr, expecting ast.Type) Object {
e.error(exp.str())
}
ast.ArrayInit {
if expr.has_len || expr.has_cap || expr.has_default {
if expr.has_len && !expr.has_cap && expr.has_default {
if expr.has_len || expr.has_cap || expr.has_init {
if expr.has_len && !expr.has_cap && expr.has_init {
return Array{
val: []Object{len: int((e.expr(expr.len_expr, 7) as Int).val), init: e.expr(expr.default_expr,
val: []Object{len: int((e.expr(expr.len_expr, 7) as Int).val), init: e.expr(expr.init_expr,
expr.elem_type)}
}
} else if !expr.has_len && expr.has_cap && !expr.has_default {
} else if !expr.has_len && expr.has_cap && !expr.has_init {
return Array{
val: []Object{cap: int((e.expr(expr.cap_expr, 7) as Int).val)}
}
} else if !expr.has_len && !expr.has_cap && !expr.has_default {
} else if !expr.has_len && !expr.has_cap && !expr.has_init {
return Array{
val: []Object{}
}
} else {
e.error('unknown array init combination; len: ${expr.has_len}, cap: ${expr.has_cap}, init: ${expr.has_default}')
e.error('unknown array init combination; len: ${expr.has_len}, cap: ${expr.has_cap}, init: ${expr.has_init}')
}
}
if expr.is_fixed || expr.has_val {
Expand Down
12 changes: 6 additions & 6 deletions vlib/v/fmt/fmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -1669,20 +1669,20 @@ pub fn (mut f Fmt) array_init(node ast.ArrayInit) {
if node.has_len {
f.write('len: ')
f.expr(node.len_expr)
if node.has_cap || node.has_default {
if node.has_cap || node.has_init {
f.write(', ')
}
}
if node.has_cap {
f.write('cap: ')
f.expr(node.cap_expr)
if node.has_default {
if node.has_init {
f.write(', ')
}
}
if node.has_default {
if node.has_init {
f.write('init: ')
f.expr(node.default_expr)
f.expr(node.init_expr)
}
f.write('}')
return
Expand Down Expand Up @@ -1851,9 +1851,9 @@ pub fn (mut f Fmt) array_init(node ast.ArrayInit) {
return
}
f.write(f.table.type_to_str_using_aliases(node.elem_type, f.mod2alias))
if node.has_default {
if node.has_init {
f.write('{init: ')
f.expr(node.default_expr)
f.expr(node.init_expr)
f.write('}')
} else {
f.write('{}')
Expand Down
32 changes: 16 additions & 16 deletions vlib/v/gen/c/array.v
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
g.write(', ')
}
}
} else if node.has_default {
} else if node.has_init {
info := array_type.unaliased_sym.info as ast.ArrayFixed
for i in 0 .. info.size {
g.write('0')
Expand All @@ -126,7 +126,7 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
g.indent++
g.writeln('int it = index;') // FIXME: Remove this line when it is fully forbidden
g.write('*pelem = ')
g.expr_with_default(node)
g.expr_with_init(node)
g.writeln(';')
g.indent--
g.writeln('}')
Expand Down Expand Up @@ -164,10 +164,10 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
g.write(', ')
}
}
} else if node.has_default {
} else if node.has_init {
info := array_type.unaliased_sym.info as ast.ArrayFixed
for i in 0 .. info.size {
g.expr_with_default(node)
g.expr_with_init(node)
if i != info.size - 1 {
g.write(', ')
}
Expand Down Expand Up @@ -236,11 +236,11 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
}
}

fn (mut g Gen) expr_with_default(node ast.ArrayInit) {
fn (mut g Gen) expr_with_init(node ast.ArrayInit) {
if node.elem_type.has_flag(.option) {
g.expr_with_opt(node.default_expr, node.default_type, node.elem_type)
g.expr_with_opt(node.init_expr, node.init_type, node.elem_type)
} else {
g.expr_with_cast(node.default_expr, node.default_type, node.elem_type)
g.expr_with_cast(node.init_expr, node.init_type, node.elem_type)
}
}

Expand All @@ -267,8 +267,8 @@ fn (mut g Gen) array_init_with_fields(node ast.ArrayInit, elem_type Type, is_amp
}
elem_styp := g.typ(elem_type.typ)
noscan := g.check_noscan(elem_type.typ)
is_default_array := elem_type.unaliased_sym.kind == .array && node.has_default
is_default_map := elem_type.unaliased_sym.kind == .map && node.has_default
is_default_array := elem_type.unaliased_sym.kind == .array && node.has_init
is_default_map := elem_type.unaliased_sym.kind == .map && node.has_init
needs_more_defaults := node.has_len && (g.struct_has_array_or_map_field(elem_type.typ)
|| elem_type.unaliased_sym.kind in [.array, .map])
if node.has_index {
Expand Down Expand Up @@ -342,7 +342,7 @@ fn (mut g Gen) array_init_with_fields(node ast.ArrayInit, elem_type Type, is_amp
g.indent++
g.writeln('int it = index;') // FIXME: Remove this line when it is fully forbidden
g.write('*pelem = ')
g.expr_with_default(node)
g.expr_with_init(node)
g.writeln(';')
g.indent--
g.writeln('}')
Expand Down Expand Up @@ -385,11 +385,11 @@ fn (mut g Gen) array_init_with_fields(node ast.ArrayInit, elem_type Type, is_amp
0
}
g.write('(${elem_styp}[]){')
g.expr(node.default_expr)
g.expr(node.init_expr)
g.write('}[0], ${depth})')
} else if is_default_map {
g.write('(${elem_styp}[]){')
g.expr(node.default_expr)
g.expr(node.init_expr)
g.write('}[0])')
} else if needs_more_defaults {
tmp := g.new_tmp_var()
Expand All @@ -404,8 +404,8 @@ fn (mut g Gen) array_init_with_fields(node ast.ArrayInit, elem_type Type, is_amp
g.expr(node.len_expr)
g.writeln('; ${ind}++) {')
g.write('\t${tmp}[${ind}] = ')
if node.has_default {
g.expr_with_default(node)
if node.has_init {
g.expr_with_init(node)
} else {
if node.elem_type.has_flag(.option) {
g.expr_with_opt(ast.None{}, ast.none_type, node.elem_type)
Expand All @@ -417,9 +417,9 @@ fn (mut g Gen) array_init_with_fields(node ast.ArrayInit, elem_type Type, is_amp
g.writeln('}')
g.write(line)
g.write(' (voidptr)${tmp})')
} else if node.has_default {
} else if node.has_init {
g.write('&(${elem_styp}[]){')
g.expr_with_default(node)
g.expr_with_init(node)
g.write('})')
} else if node.has_len && node.elem_type.has_flag(.option) {
g.write('&')
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/gen/c/if.v
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ fn (mut g Gen) need_tmp_var_in_expr(expr ast.Expr) bool {
if g.need_tmp_var_in_expr(expr.cap_expr) {
return true
}
if g.need_tmp_var_in_expr(expr.default_expr) {
if g.need_tmp_var_in_expr(expr.init_expr) {
return true
}
for elem_expr in expr.exprs {
Expand Down
12 changes: 6 additions & 6 deletions vlib/v/gen/golang/golang.v
Original file line number Diff line number Diff line change
Expand Up @@ -1332,20 +1332,20 @@ pub fn (mut f Gen) array_init(node ast.ArrayInit) {
if node.has_len {
f.write('len: ')
f.expr(node.len_expr)
if node.has_cap || node.has_default {
if node.has_cap || node.has_init {
f.write(', ')
}
}
if node.has_cap {
f.write('cap: ')
f.expr(node.cap_expr)
if node.has_default {
if node.has_init {
f.write(', ')
}
}
if node.has_default {
if node.has_init {
f.write('init: ')
f.expr(node.default_expr)
f.expr(node.init_expr)
}
f.write('}')
return
Expand Down Expand Up @@ -1374,9 +1374,9 @@ pub fn (mut f Gen) array_init(node ast.ArrayInit) {
return
}
f.write(f.table.type_to_str_using_aliases(node.elem_type, f.mod2alias))
if node.has_default {
if node.has_init {
f.write('{init: ')
f.expr(node.default_expr)
f.expr(node.init_expr)
f.write('}')
} else {
f.write('{}')
Expand Down
8 changes: 4 additions & 4 deletions vlib/v/gen/js/js.v
Original file line number Diff line number Diff line change
Expand Up @@ -2122,8 +2122,8 @@ fn (mut g JsGen) gen_array_init_expr(it ast.ArrayInit) {
g.writeln('; it++) {')
g.inc_indent()
g.write('${t1}.push(')
if it.has_default {
g.expr(it.default_expr)
if it.has_init {
g.expr(it.init_expr)
} else {
// Fill the array with the default values for its type
t := g.to_js_typ_val(it.elem_type)
Expand Down Expand Up @@ -2154,8 +2154,8 @@ fn (mut g JsGen) gen_array_init_expr(it ast.ArrayInit) {
g.writeln('; ${t2}++) {')
g.inc_indent()
g.write('${t1}.push(')
if it.has_default {
g.expr(it.default_expr)
if it.has_init {
g.expr(it.init_expr)
} else {
// Fill the array with the default values for its type
t := g.to_js_typ_val(it.elem_type)
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/markused/walker.v
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ fn (mut w Walker) expr(node_ ast.Expr) {
ast.ArrayInit {
w.expr(node.len_expr)
w.expr(node.cap_expr)
w.expr(node.default_expr)
w.expr(node.init_expr)
w.exprs(node.exprs)
}
ast.Assoc {
Expand Down
Loading

0 comments on commit e3ba197

Please sign in to comment.