1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker/cgen: simplify assigning fn to var

This commit is contained in:
joe-conigliaro 2020-05-11 00:13:30 +10:00
parent 0af939c30d
commit a73660a4c3
No known key found for this signature in database
GPG Key ID: C12F7136C08206F1
2 changed files with 29 additions and 48 deletions

View File

@ -1764,11 +1764,6 @@ pub fn (mut c Checker) ident(ident mut ast.Ident) table.Type {
info := ident.info as ast.IdentFn
return info.typ
} else if ident.kind == .unresolved {
// prepend mod to look for fn call or const
mut name := ident.name
if !name.contains('.') && ident.mod !in ['builtin', 'main'] {
name = '${ident.mod}.$ident.name'
}
// first use
start_scope := c.file.scope.innermost(ident.pos.pos)
if obj := start_scope.find(ident.name) {
@ -1778,35 +1773,27 @@ pub fn (mut c Checker) ident(ident mut ast.Ident) table.Type {
if typ == 0 {
typ = c.expr(it.expr)
}
sym := c.table.get_type_symbol(typ)
if sym.info is table.FnType {
// anon/local fn assigned to new variable uses this
info := sym.info as table.FnType
fn_type := table.new_type(c.table.find_or_register_fn_type(info.func,
true, true))
ident.kind = .function
ident.info = ast.IdentFn{
typ: fn_type
}
return fn_type
} else {
is_optional := typ.flag_is(.optional)
ident.kind = .variable
ident.info = ast.IdentVar{
typ: typ
is_optional: is_optional
}
it.typ = typ
// unwrap optional (`println(x)`)
if is_optional {
return typ.set_flag(.unset)
}
return typ
is_optional := typ.flag_is(.optional)
ident.kind = .variable
ident.info = ast.IdentVar{
typ: typ
is_optional: is_optional
}
it.typ = typ
// unwrap optional (`println(x)`)
if is_optional {
return typ.set_flag(.unset)
}
return typ
}
else {}
}
}
// prepend mod to look for fn call or const
mut name := ident.name
if !name.contains('.') && ident.mod !in ['builtin', 'main'] {
name = '${ident.mod}.$ident.name'
}
if obj := c.file.global_scope.find(name) {
match obj {
ast.GlobalDecl {

View File

@ -923,23 +923,6 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
}
continue
}
ast.Ident {
if it.info is ast.IdentFn {
thing := it.info as ast.IdentFn
sym := g.table.get_type_symbol(thing.typ)
info := sym.info as table.FnType
func := info.func
ret_styp := g.typ(func.return_type)
g.write('$ret_styp (*$ident.name) (')
def_pos := g.definitions.len
g.fn_args(func.args, func.is_variadic)
g.definitions.go_back(g.definitions.len - def_pos)
g.write(') = ')
g.expr(*it)
g.writeln(';')
continue
}
}
else {}
}
gen_or := is_call && return_type.flag_is(.optional)
@ -965,10 +948,21 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
}
is_decl := assign_stmt.op == .decl_assign
// g.write('/*assign_stmt*/')
if is_decl {
if is_decl && right_sym.kind != .function {
g.write('$styp ')
}
g.ident(ident)
if right_sym.kind == .function {
func := right_sym.info as table.FnType
ret_styp := g.typ(func.func.return_type)
g.write('$ret_styp (*$ident.name) (')
def_pos := g.definitions.len
g.fn_args(func.func.args, func.func.is_variadic)
g.definitions.go_back(g.definitions.len - def_pos)
g.write(')')
}
else {
g.ident(ident)
}
if g.autofree && right_sym.kind in [.array, .string] {
if g.gen_clone_assignment(val, right_sym, true) {
g.writeln(';')