mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
ast: clean up resolve_init() (#17663)
This commit is contained in:
parent
2656ce9522
commit
d290f432d1
@ -1,70 +1,72 @@
|
||||
module ast
|
||||
|
||||
pub fn resolve_init(node StructInit, typ Type, t &Table) Expr {
|
||||
type_sym := t.sym(typ)
|
||||
if type_sym.kind == .array {
|
||||
array_info := type_sym.info as Array
|
||||
mut has_len := false
|
||||
mut has_cap := false
|
||||
mut has_default := false
|
||||
mut len_expr := empty_expr
|
||||
mut cap_expr := empty_expr
|
||||
mut default_expr := empty_expr
|
||||
mut exprs := []Expr{}
|
||||
for field in node.fields {
|
||||
match field.name {
|
||||
'len' {
|
||||
has_len = true
|
||||
len_expr = field.expr
|
||||
}
|
||||
'cap' {
|
||||
has_cap = true
|
||||
cap_expr = field.expr
|
||||
}
|
||||
'init' {
|
||||
has_default = true
|
||||
default_expr = field.expr
|
||||
}
|
||||
else {
|
||||
exprs << field.expr
|
||||
pub fn (t &Table) resolve_init(node StructInit, typ Type) Expr {
|
||||
sym := t.sym(typ)
|
||||
match sym.info {
|
||||
Array {
|
||||
mut has_len := false
|
||||
mut has_cap := false
|
||||
mut has_default := false
|
||||
mut len_expr := empty_expr
|
||||
mut cap_expr := empty_expr
|
||||
mut default_expr := empty_expr
|
||||
mut exprs := []Expr{}
|
||||
for field in node.fields {
|
||||
match field.name {
|
||||
'len' {
|
||||
has_len = true
|
||||
len_expr = field.expr
|
||||
}
|
||||
'cap' {
|
||||
has_cap = true
|
||||
cap_expr = field.expr
|
||||
}
|
||||
'init' {
|
||||
has_default = true
|
||||
default_expr = field.expr
|
||||
}
|
||||
else {
|
||||
exprs << field.expr
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ArrayInit{
|
||||
// TODO: mod is not being set for now, we could need this in future
|
||||
// mod: mod
|
||||
pos: node.pos
|
||||
typ: typ
|
||||
elem_type: array_info.elem_type
|
||||
has_len: has_len
|
||||
has_cap: has_cap
|
||||
has_default: has_default
|
||||
len_expr: len_expr
|
||||
cap_expr: cap_expr
|
||||
default_expr: default_expr
|
||||
exprs: exprs
|
||||
}
|
||||
} else if type_sym.kind == .map {
|
||||
map_info := type_sym.info as Map
|
||||
mut keys := []Expr{}
|
||||
mut vals := []Expr{}
|
||||
for field in node.fields {
|
||||
keys << StringLiteral{
|
||||
val: field.name
|
||||
return ArrayInit{
|
||||
// TODO: mod is not being set for now, we could need this in future
|
||||
// mod: mod
|
||||
pos: node.pos
|
||||
typ: typ
|
||||
elem_type: sym.info.elem_type
|
||||
has_len: has_len
|
||||
has_cap: has_cap
|
||||
has_default: has_default
|
||||
len_expr: len_expr
|
||||
cap_expr: cap_expr
|
||||
default_expr: default_expr
|
||||
exprs: exprs
|
||||
}
|
||||
vals << field.expr
|
||||
}
|
||||
return MapInit{
|
||||
typ: typ
|
||||
key_type: map_info.key_type
|
||||
value_type: map_info.value_type
|
||||
keys: keys
|
||||
vals: vals
|
||||
Map {
|
||||
mut keys := []Expr{}
|
||||
mut vals := []Expr{}
|
||||
for field in node.fields {
|
||||
keys << StringLiteral{
|
||||
val: field.name
|
||||
}
|
||||
vals << field.expr
|
||||
}
|
||||
return MapInit{
|
||||
typ: typ
|
||||
key_type: sym.info.key_type
|
||||
value_type: sym.info.value_type
|
||||
keys: keys
|
||||
vals: vals
|
||||
}
|
||||
}
|
||||
else {
|
||||
return StructInit{
|
||||
...node
|
||||
unresolved: false
|
||||
}
|
||||
}
|
||||
}
|
||||
// struct / other (sumtype?)
|
||||
return StructInit{
|
||||
...node
|
||||
unresolved: false
|
||||
}
|
||||
}
|
||||
|
@ -2685,7 +2685,7 @@ pub fn (mut c Checker) expr(node_ ast.Expr) ast.Type {
|
||||
}
|
||||
ast.StructInit {
|
||||
if node.unresolved {
|
||||
return c.expr(ast.resolve_init(node, c.unwrap_generic(node.typ), c.table))
|
||||
return c.expr(c.table.resolve_init(node, c.unwrap_generic(node.typ)))
|
||||
}
|
||||
return c.struct_init(mut node, false)
|
||||
}
|
||||
|
@ -3310,7 +3310,7 @@ fn (mut g Gen) expr(node_ ast.Expr) {
|
||||
}
|
||||
ast.StructInit {
|
||||
if node.unresolved {
|
||||
g.expr(ast.resolve_init(node, g.unwrap_generic(node.typ), g.table))
|
||||
g.expr(g.table.resolve_init(node, g.unwrap_generic(node.typ)))
|
||||
} else {
|
||||
// `user := User{name: 'Bob'}`
|
||||
g.inside_struct_init = true
|
||||
|
@ -1049,7 +1049,7 @@ fn (mut g JsGen) expr(node_ ast.Expr) {
|
||||
}
|
||||
ast.StructInit {
|
||||
if node.unresolved {
|
||||
resolved := ast.resolve_init(node, g.unwrap_generic(node.typ), g.table)
|
||||
resolved := g.table.resolve_init(node, g.unwrap_generic(node.typ))
|
||||
g.expr(resolved)
|
||||
} else {
|
||||
g.gen_struct_init(node)
|
||||
|
Loading…
Reference in New Issue
Block a user