1
0
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:
yuyi 2023-03-15 23:20:04 +08:00 committed by GitHub
parent 2656ce9522
commit d290f432d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 66 additions and 64 deletions

View File

@ -1,9 +1,9 @@
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
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
@ -35,7 +35,7 @@ pub fn resolve_init(node StructInit, typ Type, t &Table) Expr {
// mod: mod
pos: node.pos
typ: typ
elem_type: array_info.elem_type
elem_type: sym.info.elem_type
has_len: has_len
has_cap: has_cap
has_default: has_default
@ -44,8 +44,8 @@ pub fn resolve_init(node StructInit, typ Type, t &Table) Expr {
default_expr: default_expr
exprs: exprs
}
} else if type_sym.kind == .map {
map_info := type_sym.info as Map
}
Map {
mut keys := []Expr{}
mut vals := []Expr{}
for field in node.fields {
@ -56,15 +56,17 @@ pub fn resolve_init(node StructInit, typ Type, t &Table) Expr {
}
return MapInit{
typ: typ
key_type: map_info.key_type
value_type: map_info.value_type
key_type: sym.info.key_type
value_type: sym.info.value_type
keys: keys
vals: vals
}
}
// struct / other (sumtype?)
else {
return StructInit{
...node
unresolved: false
}
}
}
}

View File

@ -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)
}

View File

@ -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

View File

@ -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)