// Kludge for "missing func info" linker panic.
// See also closureInitLSym in inline/inl.go.
if clo, ok := (*as.rhs).(*ir.ClosureExpr); ok && clo.Op() == ir.OCLOSURE {
- if !ir.IsTrivialClosure(clo) {
+ if clo.Func.IsClosure() {
ir.InitLSym(clo.Func, true)
}
}
// typecheck.Target.Decls (ir.UseClosure adds all closures to
// Decls).
//
- // However, non-trivial closures in Decls are ignored, and are
+ // However, closures in Decls are ignored, and are
// instead enqueued when walk of the calling function
// discovers them.
//
}
clo := n.Fun.(*ir.ClosureExpr)
- if ir.IsTrivialClosure(clo) {
- // enqueueFunc will handle trivial closures anyways.
+ if !clo.Func.IsClosure() {
+ // enqueueFunc will handle non closures anyways.
return
}
}
}
+// IsClosure reports whether f is a function literal that captures at least one value.
func (f *Func) IsClosure() bool {
if f.OClosure == nil {
return false
}
- // Trivial closure will be converted to global.
- return !IsTrivialClosure(f.OClosure)
+ return len(f.ClosureVars) > 0
}
// FuncName returns the name (without the package) of the function f.
}
}
-// IsTrivialClosure reports whether closure clo has an
-// empty list of captured vars.
-func IsTrivialClosure(clo *ClosureExpr) bool {
- return len(clo.Func.ClosureVars) == 0
-}
-
// globClosgen is like Func.Closgen, but for the global scope.
var globClosgen int32
// The algorithm (known as Tarjan's algorithm) for doing that is taken from
// Sedgewick, Algorithms, Second Edition, p. 482, with two adaptations.
//
-// First, a non-trivial closure function (fn.OClosure != nil) cannot be
+// First, a closure function (fn.IsClosure()) cannot be
// the root of a connected component. Refusing to use it as a root forces
// it into the component of the function in which it appears. This is
// more convenient for escape analysis.
case ir.OCLOSURE:
r := r.(*ir.ClosureExpr)
- if ir.IsTrivialClosure(r) {
+ if !r.Func.IsClosure() {
if base.Debug.Closure > 0 {
base.WarnfAt(r.Pos(), "closure converted to global")
}
count[x.(*ir.Name)] = 0
}
- hasNonTrivialClosure := false
+ hasClosure := false
ir.Visit(as2body.Rhs[0], func(n ir.Node) {
if name, ok := n.(*ir.Name); ok {
if c, ok := count[name]; ok {
}
}
if clo, ok := n.(*ir.ClosureExpr); ok {
- hasNonTrivialClosure = hasNonTrivialClosure || !ir.IsTrivialClosure(clo)
+ hasClosure = hasClosure || clo.Func.IsClosure()
}
})
- // If there's a non-trivial closure, it has captured the param,
+ // If there's a closure, it has captured the param,
// so we can't substitute arg for param.
- if hasNonTrivialClosure {
+ if hasClosure {
return false
}
clo := n.Fun.(*ir.ClosureExpr)
clofn := clo.Func
- if ir.IsTrivialClosure(clo) {
+ if !clofn.IsClosure() {
return // leave for walkClosure to handle
}
// Add to Closures for enqueueFunc. It's no longer a proper
// closure, but we may have already skipped over it in the
- // functions list as a non-trivial closure, so this just
- // ensures it's compiled.
+ // functions list, so this just ensures it's compiled.
ir.CurFunc.Closures = append(ir.CurFunc.Closures, clofn)
}
func walkClosure(clo *ir.ClosureExpr, init *ir.Nodes) ir.Node {
clofn := clo.Func
- // If no closure vars, don't bother wrapping.
- if ir.IsTrivialClosure(clo) {
+ // If not a closure, don't bother wrapping.
+ if !clofn.IsClosure() {
if base.Debug.Closure > 0 {
base.WarnfAt(clo.Pos(), "closure converted to global")
}