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:
parent
0af939c30d
commit
a73660a4c3
|
@ -1764,11 +1764,6 @@ pub fn (mut c Checker) ident(ident mut ast.Ident) table.Type {
|
||||||
info := ident.info as ast.IdentFn
|
info := ident.info as ast.IdentFn
|
||||||
return info.typ
|
return info.typ
|
||||||
} else if ident.kind == .unresolved {
|
} 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
|
// first use
|
||||||
start_scope := c.file.scope.innermost(ident.pos.pos)
|
start_scope := c.file.scope.innermost(ident.pos.pos)
|
||||||
if obj := start_scope.find(ident.name) {
|
if obj := start_scope.find(ident.name) {
|
||||||
|
@ -1778,18 +1773,6 @@ pub fn (mut c Checker) ident(ident mut ast.Ident) table.Type {
|
||||||
if typ == 0 {
|
if typ == 0 {
|
||||||
typ = c.expr(it.expr)
|
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)
|
is_optional := typ.flag_is(.optional)
|
||||||
ident.kind = .variable
|
ident.kind = .variable
|
||||||
ident.info = ast.IdentVar{
|
ident.info = ast.IdentVar{
|
||||||
|
@ -1803,10 +1786,14 @@ pub fn (mut c Checker) ident(ident mut ast.Ident) table.Type {
|
||||||
}
|
}
|
||||||
return typ
|
return typ
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else {}
|
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) {
|
if obj := c.file.global_scope.find(name) {
|
||||||
match obj {
|
match obj {
|
||||||
ast.GlobalDecl {
|
ast.GlobalDecl {
|
||||||
|
|
|
@ -923,23 +923,6 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
|
||||||
}
|
}
|
||||||
continue
|
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 {}
|
else {}
|
||||||
}
|
}
|
||||||
gen_or := is_call && return_type.flag_is(.optional)
|
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
|
is_decl := assign_stmt.op == .decl_assign
|
||||||
// g.write('/*assign_stmt*/')
|
// g.write('/*assign_stmt*/')
|
||||||
if is_decl {
|
if is_decl && right_sym.kind != .function {
|
||||||
g.write('$styp ')
|
g.write('$styp ')
|
||||||
}
|
}
|
||||||
|
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)
|
g.ident(ident)
|
||||||
|
}
|
||||||
if g.autofree && right_sym.kind in [.array, .string] {
|
if g.autofree && right_sym.kind in [.array, .string] {
|
||||||
if g.gen_clone_assignment(val, right_sym, true) {
|
if g.gen_clone_assignment(val, right_sym, true) {
|
||||||
g.writeln(';')
|
g.writeln(';')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user