mirror of
https://github.com/golang/go
synced 2024-11-26 09:28:07 -07:00
[dev.typeparams] cmd/compile: simplify NewClosureFunc
I initially made NewClosureFunc take an "outerfn *Func" parameter because I was planning on having it handle closure naming, until remembering that naming needs to wait until typecheck for noder. We don't actually need the *Func yet, just to know whether it's non-nil. So change the parameter to a bool, which simplifies callers a little. Change-Id: Ie83ee4a1ed0571ac6d3879ffd8474c6c3c1a9ff9 Reviewed-on: https://go-review.googlesource.com/c/go/+/327450 Trust: Matthew Dempsky <mdempsky@google.com> Run-TryBot: Matthew Dempsky <mdempsky@google.com> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com> TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
parent
db7c868307
commit
f1b1c2f67f
@ -1153,13 +1153,8 @@ func (subst *inlsubst) closure(n *ir.ClosureExpr) ir.Node {
|
|||||||
|
|
||||||
//fmt.Printf("Inlining func %v with closure into %v\n", subst.fn, ir.FuncName(ir.CurFunc))
|
//fmt.Printf("Inlining func %v with closure into %v\n", subst.fn, ir.FuncName(ir.CurFunc))
|
||||||
|
|
||||||
outerfunc := subst.newclofn
|
|
||||||
if outerfunc == nil {
|
|
||||||
outerfunc = ir.CurFunc
|
|
||||||
}
|
|
||||||
|
|
||||||
oldfn := n.Func
|
oldfn := n.Func
|
||||||
newfn := ir.NewClosureFunc(oldfn.Pos(), outerfunc)
|
newfn := ir.NewClosureFunc(oldfn.Pos(), true)
|
||||||
|
|
||||||
// Ntype can be nil for -G=3 mode.
|
// Ntype can be nil for -G=3 mode.
|
||||||
if oldfn.Nname.Ntype != nil {
|
if oldfn.Nname.Ntype != nil {
|
||||||
|
@ -343,11 +343,13 @@ func closureName(outerfn *Func) *types.Sym {
|
|||||||
return pkg.Lookup(fmt.Sprintf("%s.%s%d", outer, prefix, *gen))
|
return pkg.Lookup(fmt.Sprintf("%s.%s%d", outer, prefix, *gen))
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClosureFunc creates a new Func to represent a function literal
|
// NewClosureFunc creates a new Func to represent a function literal.
|
||||||
// within outerfn.
|
// If hidden is true, then the closure is marked hidden (i.e., as a
|
||||||
func NewClosureFunc(pos src.XPos, outerfn *Func) *Func {
|
// function literal contained within another function, rather than a
|
||||||
|
// package-scope variable initialization expression).
|
||||||
|
func NewClosureFunc(pos src.XPos, hidden bool) *Func {
|
||||||
fn := NewFunc(pos)
|
fn := NewFunc(pos)
|
||||||
fn.SetIsHiddenClosure(outerfn != nil)
|
fn.SetIsHiddenClosure(hidden)
|
||||||
|
|
||||||
fn.Nname = NewNameAt(pos, BlankNode.Sym())
|
fn.Nname = NewNameAt(pos, BlankNode.Sym())
|
||||||
fn.Nname.Func = fn
|
fn.Nname.Func = fn
|
||||||
@ -361,7 +363,12 @@ func NewClosureFunc(pos src.XPos, outerfn *Func) *Func {
|
|||||||
// NameClosure generates a unique for the given function literal,
|
// NameClosure generates a unique for the given function literal,
|
||||||
// which must have appeared within outerfn.
|
// which must have appeared within outerfn.
|
||||||
func NameClosure(clo *ClosureExpr, outerfn *Func) {
|
func NameClosure(clo *ClosureExpr, outerfn *Func) {
|
||||||
name := clo.Func.Nname
|
fn := clo.Func
|
||||||
|
if fn.IsHiddenClosure() != (outerfn != nil) {
|
||||||
|
base.FatalfAt(clo.Pos(), "closure naming inconsistency: hidden %v, but outer %v", fn.IsHiddenClosure(), outerfn)
|
||||||
|
}
|
||||||
|
|
||||||
|
name := fn.Nname
|
||||||
if !IsBlank(name) {
|
if !IsBlank(name) {
|
||||||
base.FatalfAt(clo.Pos(), "closure already named: %v", name)
|
base.FatalfAt(clo.Pos(), "closure already named: %v", name)
|
||||||
}
|
}
|
||||||
|
@ -373,7 +373,7 @@ func (g *irgen) compLit(typ types2.Type, lit *syntax.CompositeLit) ir.Node {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g *irgen) funcLit(typ2 types2.Type, expr *syntax.FuncLit) ir.Node {
|
func (g *irgen) funcLit(typ2 types2.Type, expr *syntax.FuncLit) ir.Node {
|
||||||
fn := ir.NewClosureFunc(g.pos(expr), ir.CurFunc)
|
fn := ir.NewClosureFunc(g.pos(expr), ir.CurFunc != nil)
|
||||||
ir.NameClosure(fn.OClosure, ir.CurFunc)
|
ir.NameClosure(fn.OClosure, ir.CurFunc)
|
||||||
|
|
||||||
typ := g.typ(typ2)
|
typ := g.typ(typ2)
|
||||||
|
@ -1803,7 +1803,7 @@ func fakeRecv() *ir.Field {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *noder) funcLit(expr *syntax.FuncLit) ir.Node {
|
func (p *noder) funcLit(expr *syntax.FuncLit) ir.Node {
|
||||||
fn := ir.NewClosureFunc(p.pos(expr), ir.CurFunc)
|
fn := ir.NewClosureFunc(p.pos(expr), ir.CurFunc != nil)
|
||||||
fn.Nname.Ntype = p.typeExpr(expr.Type)
|
fn.Nname.Ntype = p.typeExpr(expr.Type)
|
||||||
|
|
||||||
p.funcBody(fn, expr.Body)
|
p.funcBody(fn, expr.Body)
|
||||||
|
@ -280,7 +280,7 @@ func (g *irgen) buildClosure(outer *ir.Func, x ir.Node) ir.Node {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
// Make a new internal function.
|
// Make a new internal function.
|
||||||
fn := ir.NewClosureFunc(pos, outer)
|
fn := ir.NewClosureFunc(pos, outer != nil)
|
||||||
ir.NameClosure(fn.OClosure, outer)
|
ir.NameClosure(fn.OClosure, outer)
|
||||||
|
|
||||||
// This is the dictionary we want to use.
|
// This is the dictionary we want to use.
|
||||||
@ -857,7 +857,7 @@ func (subst *subster) node(n ir.Node) ir.Node {
|
|||||||
// Need to duplicate x.Func.Nname, x.Func.Dcl, x.Func.ClosureVars, and
|
// Need to duplicate x.Func.Nname, x.Func.Dcl, x.Func.ClosureVars, and
|
||||||
// x.Func.Body.
|
// x.Func.Body.
|
||||||
oldfn := x.Func
|
oldfn := x.Func
|
||||||
newfn := ir.NewClosureFunc(oldfn.Pos(), subst.newf)
|
newfn := ir.NewClosureFunc(oldfn.Pos(), subst.newf != nil)
|
||||||
ir.NameClosure(newfn.OClosure, subst.newf)
|
ir.NameClosure(newfn.OClosure, subst.newf)
|
||||||
|
|
||||||
newfn.SetClosureCalled(oldfn.ClosureCalled())
|
newfn.SetClosureCalled(oldfn.ClosureCalled())
|
||||||
|
@ -1283,7 +1283,7 @@ func (r *importReader) node() ir.Node {
|
|||||||
|
|
||||||
// All the remaining code below is similar to (*noder).funcLit(), but
|
// All the remaining code below is similar to (*noder).funcLit(), but
|
||||||
// with Dcls and ClosureVars lists already set up
|
// with Dcls and ClosureVars lists already set up
|
||||||
fn := ir.NewClosureFunc(pos, r.curfn)
|
fn := ir.NewClosureFunc(pos, true)
|
||||||
fn.Nname.SetType(typ)
|
fn.Nname.SetType(typ)
|
||||||
|
|
||||||
cvars := make([]*ir.Name, r.int64())
|
cvars := make([]*ir.Name, r.int64())
|
||||||
|
@ -1704,9 +1704,7 @@ func (o *orderState) wrapGoDefer(n *ir.GoDeferStmt) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create a new no-argument function that we'll hand off to defer.
|
// Create a new no-argument function that we'll hand off to defer.
|
||||||
outerfn := ir.CurFunc
|
fn := ir.NewClosureFunc(base.Pos, true)
|
||||||
|
|
||||||
fn := ir.NewClosureFunc(base.Pos, outerfn)
|
|
||||||
fn.Nname.SetType(types.NewSignature(types.LocalPkg, nil, nil, nil, nil))
|
fn.Nname.SetType(types.NewSignature(types.LocalPkg, nil, nil, nil, nil))
|
||||||
fn.SetWrapper(true)
|
fn.SetWrapper(true)
|
||||||
|
|
||||||
@ -1752,7 +1750,7 @@ func (o *orderState) wrapGoDefer(n *ir.GoDeferStmt) {
|
|||||||
|
|
||||||
// Finalize body, register function on the main decls list.
|
// Finalize body, register function on the main decls list.
|
||||||
fn.Body = []ir.Node{newcall}
|
fn.Body = []ir.Node{newcall}
|
||||||
ir.FinishCaptureNames(n.Pos(), outerfn, fn)
|
ir.FinishCaptureNames(n.Pos(), ir.CurFunc, fn)
|
||||||
|
|
||||||
// Create closure expr
|
// Create closure expr
|
||||||
clo := typecheck.Expr(fn.OClosure).(*ir.ClosureExpr)
|
clo := typecheck.Expr(fn.OClosure).(*ir.ClosureExpr)
|
||||||
|
Loading…
Reference in New Issue
Block a user