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
|
module ast
|
||||||
|
|
||||||
pub fn resolve_init(node StructInit, typ Type, t &Table) Expr {
|
pub fn (t &Table) resolve_init(node StructInit, typ Type) Expr {
|
||||||
type_sym := t.sym(typ)
|
sym := t.sym(typ)
|
||||||
if type_sym.kind == .array {
|
match sym.info {
|
||||||
array_info := type_sym.info as Array
|
Array {
|
||||||
mut has_len := false
|
mut has_len := false
|
||||||
mut has_cap := false
|
mut has_cap := false
|
||||||
mut has_default := false
|
mut has_default := false
|
||||||
mut len_expr := empty_expr
|
mut len_expr := empty_expr
|
||||||
mut cap_expr := empty_expr
|
mut cap_expr := empty_expr
|
||||||
mut default_expr := empty_expr
|
mut default_expr := empty_expr
|
||||||
mut exprs := []Expr{}
|
mut exprs := []Expr{}
|
||||||
for field in node.fields {
|
for field in node.fields {
|
||||||
match field.name {
|
match field.name {
|
||||||
'len' {
|
'len' {
|
||||||
has_len = true
|
has_len = true
|
||||||
len_expr = field.expr
|
len_expr = field.expr
|
||||||
}
|
}
|
||||||
'cap' {
|
'cap' {
|
||||||
has_cap = true
|
has_cap = true
|
||||||
cap_expr = field.expr
|
cap_expr = field.expr
|
||||||
}
|
}
|
||||||
'init' {
|
'init' {
|
||||||
has_default = true
|
has_default = true
|
||||||
default_expr = field.expr
|
default_expr = field.expr
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
exprs << field.expr
|
exprs << field.expr
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return ArrayInit{
|
||||||
return ArrayInit{
|
// TODO: mod is not being set for now, we could need this in future
|
||||||
// TODO: mod is not being set for now, we could need this in future
|
// mod: mod
|
||||||
// mod: mod
|
pos: node.pos
|
||||||
pos: node.pos
|
typ: typ
|
||||||
typ: typ
|
elem_type: sym.info.elem_type
|
||||||
elem_type: array_info.elem_type
|
has_len: has_len
|
||||||
has_len: has_len
|
has_cap: has_cap
|
||||||
has_cap: has_cap
|
has_default: has_default
|
||||||
has_default: has_default
|
len_expr: len_expr
|
||||||
len_expr: len_expr
|
cap_expr: cap_expr
|
||||||
cap_expr: cap_expr
|
default_expr: default_expr
|
||||||
default_expr: default_expr
|
exprs: exprs
|
||||||
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
|
|
||||||
}
|
}
|
||||||
vals << field.expr
|
|
||||||
}
|
}
|
||||||
return MapInit{
|
Map {
|
||||||
typ: typ
|
mut keys := []Expr{}
|
||||||
key_type: map_info.key_type
|
mut vals := []Expr{}
|
||||||
value_type: map_info.value_type
|
for field in node.fields {
|
||||||
keys: keys
|
keys << StringLiteral{
|
||||||
vals: vals
|
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 {
|
ast.StructInit {
|
||||||
if node.unresolved {
|
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)
|
return c.struct_init(mut node, false)
|
||||||
}
|
}
|
||||||
|
@ -3310,7 +3310,7 @@ fn (mut g Gen) expr(node_ ast.Expr) {
|
|||||||
}
|
}
|
||||||
ast.StructInit {
|
ast.StructInit {
|
||||||
if node.unresolved {
|
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 {
|
} else {
|
||||||
// `user := User{name: 'Bob'}`
|
// `user := User{name: 'Bob'}`
|
||||||
g.inside_struct_init = true
|
g.inside_struct_init = true
|
||||||
|
@ -1049,7 +1049,7 @@ fn (mut g JsGen) expr(node_ ast.Expr) {
|
|||||||
}
|
}
|
||||||
ast.StructInit {
|
ast.StructInit {
|
||||||
if node.unresolved {
|
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)
|
g.expr(resolved)
|
||||||
} else {
|
} else {
|
||||||
g.gen_struct_init(node)
|
g.gen_struct_init(node)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user