e.oldLoc(tmp).captured = true
- cv := ir.NewClosureVar(pos, wrapper, tmp)
- cv.SetType(tmp.Type())
- tmp = typecheck.Expr(cv).(*ir.Name)
+ tmp = ir.NewClosureVar(pos, wrapper, tmp)
}
*exprp = tmp
// Allocate locations for local variables.
for _, n := range fn.Dcl {
- if n.Op() == ir.ONAME {
- e.newLoc(n, false)
+ e.newLoc(n, false)
+ }
+
+ // Also for hidden parameters (e.g., the ".this" parameter to a
+ // method value wrapper).
+ if fn.OClosure == nil {
+ for _, n := range fn.ClosureVars {
+ e.newLoc(n.Canonical(), false)
}
}
if n.Class == ir.PFUNC || n.Class == ir.PEXTERN {
return
}
- if n.IsClosureVar() && n.Defn == nil {
- return // ".this" from method value wrapper
- }
e.flow(k, e.oldLoc(n))
case ir.OPLUS, ir.ONEG, ir.OBITNOT, ir.ONOT:
}
if n != nil && n.Op() == ir.ONAME {
- n = n.(*ir.Name).Canonical()
+ if canon := n.(*ir.Name).Canonical(); n != canon {
+ base.Fatalf("newLoc on non-canonical %v (canonical is %v)", n, canon)
+ }
}
loc := &location{
n: n,
if n != nil {
if n.Op() == ir.ONAME {
n := n.(*ir.Name)
- if n.Curfn != e.curfn {
+ if n.Class == ir.PPARAM && n.Curfn == nil {
+ // ok; hidden parameter
+ } else if n.Curfn != e.curfn {
base.Fatalf("curfn mismatch: %v != %v for %v", n.Curfn, e.curfn, n)
}
return n.Canonical().flags&nameByval != 0
}
-// NewClosureVar creates a new closure variable for fn to refer to
+// NewClosureVar returns a new closure variable for fn to refer to
// outer variable n.
func NewClosureVar(pos src.XPos, fn *Func, n *Name) *Name {
c := NewNameAt(pos, n.Sym())
c.Defn = n.Canonical()
c.Outer = n
+ c.SetType(n.Type())
+ c.SetTypecheck(n.Typecheck())
+
fn.ClosureVars = append(fn.ClosureVars, c)
return c
}
+// NewHiddenParam returns a new hidden parameter for fn with the given
+// name and type.
+func NewHiddenParam(pos src.XPos, fn *Func, sym *types.Sym, typ *types.Type) *Name {
+ if fn.OClosure != nil {
+ base.FatalfAt(fn.Pos(), "cannot add hidden parameters to closures")
+ }
+
+ fn.SetNeedctxt(true)
+
+ // Create a fake parameter, disassociated from any real function, to
+ // pretend to capture.
+ fake := NewNameAt(pos, sym)
+ fake.Class = PPARAM
+ fake.SetType(typ)
+ fake.SetByval(true)
+
+ return NewClosureVar(pos, fn, fake)
+}
+
// CaptureName returns a Name suitable for referring to n from within function
// fn or from the package block if fn is nil. If n is a free variable declared
// within a function that encloses fn, then CaptureName returns the closure
fn.ClosureVars = make([]*ir.Name, 0, r.len())
for len(fn.ClosureVars) < cap(fn.ClosureVars) {
- pos := r.pos()
- outer := r.useLocal()
-
- cv := ir.NewClosureVar(pos, fn, outer)
- r.setType(cv, outer.Type())
+ ir.NewClosureVar(r.pos(), fn, r.useLocal())
}
r.addBody(fn)
pos := base.AutogeneratedPos
fn := r.newWrapperFunc(pos, sym, nil, method)
- fn.SetNeedctxt(true)
sym.Def = fn
// Declare and initialize variable holding receiver.
- recv := ir.NewNameAt(pos, typecheck.Lookup(".this"))
- recv.Class = ir.PAUTOHEAP
- recv.SetType(recvType)
- recv.Curfn = fn
- recv.SetIsClosureVar(true)
- recv.SetByval(true)
- fn.ClosureVars = append(fn.ClosureVars, recv)
+ recv := ir.NewHiddenParam(pos, fn, typecheck.Lookup(".this"), recvType)
addTailCall(pos, fn, recv, method)
cvars := make([]*ir.Name, r.int64())
for i := range cvars {
cvars[i] = ir.CaptureName(r.pos(), fn, r.localName().Canonical())
- if go117ExportTypes {
- if cvars[i].Type() != nil || cvars[i].Defn == nil {
- base.Fatalf("bad import of closure variable")
- }
- // Closure variable should have Defn set, which is its captured
- // variable, and it gets the same type as the captured variable.
- cvars[i].SetType(cvars[i].Defn.Type())
+ if go117ExportTypes && cvars[i].Defn == nil {
+ base.Fatalf("bad import of closure variable")
}
}
fn.ClosureVars = cvars
fn := typecheck.DeclFunc(sym, tfn)
fn.SetDupok(true)
- fn.SetNeedctxt(true)
fn.SetWrapper(true)
// Declare and initialize variable holding receiver.
- ptr := ir.NewNameAt(base.Pos, typecheck.Lookup(".this"))
- ptr.Class = ir.PAUTOHEAP
- ptr.SetType(rcvrtype)
- ptr.Curfn = fn
- ptr.SetIsClosureVar(true)
- ptr.SetByval(true)
- fn.ClosureVars = append(fn.ClosureVars, ptr)
+ ptr := ir.NewHiddenParam(base.Pos, fn, typecheck.Lookup(".this"), rcvrtype)
call := ir.NewCallExpr(base.Pos, ir.OCALL, ir.NewSelectorExpr(base.Pos, ir.OXDOT, ptr, meth), nil)
call.Args = ir.ParamNames(tfn.Type())