mirror of
https://github.com/golang/go
synced 2024-09-05 07:54:46 +00:00
[dev.typeparams] cmd/compile: formalize "hidden parameters" idea
This CL formalizes the closure-var trick used for method-value wrappers to be reusable for defining other functions that take hidden parameters via the closure-context register. In particular, it: 1. Adds a new ir.NewHiddenParam function for creating hidden parameters. 2. Changes ir.NewClosureVar to copy Type/Typecheck from the closure variable, so that callers can needing to manually copy these. 3. Updates existing code accordingly (i.e., method-value wrappers to start using ir.NewHiddenParam, and closure builders to stop copying types). Longer term, I anticipate using this to pass dictionaries to stenciled functions within unified IR. Change-Id: I9da3ffdb2a26d15c6e89a21b4e080686d6dc872c Reviewed-on: https://go-review.googlesource.com/c/go/+/332612 Trust: Matthew Dempsky <mdempsky@google.com> Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
This commit is contained in:
parent
611056ec34
commit
5dac279fbd
|
@ -343,9 +343,7 @@ func (e *escape) wrapExpr(pos src.XPos, exprp *ir.Node, init *ir.Nodes, call ir.
|
||||||
|
|
||||||
e.oldLoc(tmp).captured = true
|
e.oldLoc(tmp).captured = true
|
||||||
|
|
||||||
cv := ir.NewClosureVar(pos, wrapper, tmp)
|
tmp = ir.NewClosureVar(pos, wrapper, tmp)
|
||||||
cv.SetType(tmp.Type())
|
|
||||||
tmp = typecheck.Expr(cv).(*ir.Name)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
*exprp = tmp
|
*exprp = tmp
|
||||||
|
|
|
@ -183,8 +183,14 @@ func (b *batch) initFunc(fn *ir.Func) {
|
||||||
|
|
||||||
// Allocate locations for local variables.
|
// Allocate locations for local variables.
|
||||||
for _, n := range fn.Dcl {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -46,9 +46,6 @@ func (e *escape) exprSkipInit(k hole, n ir.Node) {
|
||||||
if n.Class == ir.PFUNC || n.Class == ir.PEXTERN {
|
if n.Class == ir.PFUNC || n.Class == ir.PEXTERN {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if n.IsClosureVar() && n.Defn == nil {
|
|
||||||
return // ".this" from method value wrapper
|
|
||||||
}
|
|
||||||
e.flow(k, e.oldLoc(n))
|
e.flow(k, e.oldLoc(n))
|
||||||
|
|
||||||
case ir.OPLUS, ir.ONEG, ir.OBITNOT, ir.ONOT:
|
case ir.OPLUS, ir.ONEG, ir.OBITNOT, ir.ONOT:
|
||||||
|
|
|
@ -222,7 +222,9 @@ func (e *escape) newLoc(n ir.Node, transient bool) *location {
|
||||||
}
|
}
|
||||||
|
|
||||||
if n != nil && n.Op() == ir.ONAME {
|
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{
|
loc := &location{
|
||||||
n: n,
|
n: n,
|
||||||
|
@ -234,7 +236,9 @@ func (e *escape) newLoc(n ir.Node, transient bool) *location {
|
||||||
if n != nil {
|
if n != nil {
|
||||||
if n.Op() == ir.ONAME {
|
if n.Op() == ir.ONAME {
|
||||||
n := n.(*ir.Name)
|
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)
|
base.Fatalf("curfn mismatch: %v != %v for %v", n.Curfn, e.curfn, n)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -358,7 +358,7 @@ func (n *Name) Byval() bool {
|
||||||
return n.Canonical().flags&nameByval != 0
|
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.
|
// outer variable n.
|
||||||
func NewClosureVar(pos src.XPos, fn *Func, n *Name) *Name {
|
func NewClosureVar(pos src.XPos, fn *Func, n *Name) *Name {
|
||||||
c := NewNameAt(pos, n.Sym())
|
c := NewNameAt(pos, n.Sym())
|
||||||
|
@ -368,11 +368,33 @@ func NewClosureVar(pos src.XPos, fn *Func, n *Name) *Name {
|
||||||
c.Defn = n.Canonical()
|
c.Defn = n.Canonical()
|
||||||
c.Outer = n
|
c.Outer = n
|
||||||
|
|
||||||
|
c.SetType(n.Type())
|
||||||
|
c.SetTypecheck(n.Typecheck())
|
||||||
|
|
||||||
fn.ClosureVars = append(fn.ClosureVars, c)
|
fn.ClosureVars = append(fn.ClosureVars, c)
|
||||||
|
|
||||||
return 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
|
// 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
|
// 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
|
// within a function that encloses fn, then CaptureName returns the closure
|
||||||
|
|
|
@ -1623,11 +1623,7 @@ func (r *reader) funcLit() ir.Node {
|
||||||
|
|
||||||
fn.ClosureVars = make([]*ir.Name, 0, r.len())
|
fn.ClosureVars = make([]*ir.Name, 0, r.len())
|
||||||
for len(fn.ClosureVars) < cap(fn.ClosureVars) {
|
for len(fn.ClosureVars) < cap(fn.ClosureVars) {
|
||||||
pos := r.pos()
|
ir.NewClosureVar(r.pos(), fn, r.useLocal())
|
||||||
outer := r.useLocal()
|
|
||||||
|
|
||||||
cv := ir.NewClosureVar(pos, fn, outer)
|
|
||||||
r.setType(cv, outer.Type())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
r.addBody(fn)
|
r.addBody(fn)
|
||||||
|
@ -2204,17 +2200,10 @@ func (r *reader) methodValueWrapper(tbase *types.Type, method *types.Field, targ
|
||||||
pos := base.AutogeneratedPos
|
pos := base.AutogeneratedPos
|
||||||
|
|
||||||
fn := r.newWrapperFunc(pos, sym, nil, method)
|
fn := r.newWrapperFunc(pos, sym, nil, method)
|
||||||
fn.SetNeedctxt(true)
|
|
||||||
sym.Def = fn
|
sym.Def = fn
|
||||||
|
|
||||||
// Declare and initialize variable holding receiver.
|
// Declare and initialize variable holding receiver.
|
||||||
recv := ir.NewNameAt(pos, typecheck.Lookup(".this"))
|
recv := ir.NewHiddenParam(pos, fn, typecheck.Lookup(".this"), recvType)
|
||||||
recv.Class = ir.PAUTOHEAP
|
|
||||||
recv.SetType(recvType)
|
|
||||||
recv.Curfn = fn
|
|
||||||
recv.SetIsClosureVar(true)
|
|
||||||
recv.SetByval(true)
|
|
||||||
fn.ClosureVars = append(fn.ClosureVars, recv)
|
|
||||||
|
|
||||||
addTailCall(pos, fn, recv, method)
|
addTailCall(pos, fn, recv, method)
|
||||||
|
|
||||||
|
|
|
@ -1289,13 +1289,8 @@ func (r *importReader) node() ir.Node {
|
||||||
cvars := make([]*ir.Name, r.int64())
|
cvars := make([]*ir.Name, r.int64())
|
||||||
for i := range cvars {
|
for i := range cvars {
|
||||||
cvars[i] = ir.CaptureName(r.pos(), fn, r.localName().Canonical())
|
cvars[i] = ir.CaptureName(r.pos(), fn, r.localName().Canonical())
|
||||||
if go117ExportTypes {
|
if go117ExportTypes && cvars[i].Defn == nil {
|
||||||
if cvars[i].Type() != nil || cvars[i].Defn == nil {
|
base.Fatalf("bad import of closure variable")
|
||||||
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())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn.ClosureVars = cvars
|
fn.ClosureVars = cvars
|
||||||
|
|
|
@ -238,17 +238,10 @@ func methodValueWrapper(dot *ir.SelectorExpr) *ir.Func {
|
||||||
|
|
||||||
fn := typecheck.DeclFunc(sym, tfn)
|
fn := typecheck.DeclFunc(sym, tfn)
|
||||||
fn.SetDupok(true)
|
fn.SetDupok(true)
|
||||||
fn.SetNeedctxt(true)
|
|
||||||
fn.SetWrapper(true)
|
fn.SetWrapper(true)
|
||||||
|
|
||||||
// Declare and initialize variable holding receiver.
|
// Declare and initialize variable holding receiver.
|
||||||
ptr := ir.NewNameAt(base.Pos, typecheck.Lookup(".this"))
|
ptr := ir.NewHiddenParam(base.Pos, fn, typecheck.Lookup(".this"), rcvrtype)
|
||||||
ptr.Class = ir.PAUTOHEAP
|
|
||||||
ptr.SetType(rcvrtype)
|
|
||||||
ptr.Curfn = fn
|
|
||||||
ptr.SetIsClosureVar(true)
|
|
||||||
ptr.SetByval(true)
|
|
||||||
fn.ClosureVars = append(fn.ClosureVars, ptr)
|
|
||||||
|
|
||||||
call := ir.NewCallExpr(base.Pos, ir.OCALL, ir.NewSelectorExpr(base.Pos, ir.OXDOT, ptr, meth), nil)
|
call := ir.NewCallExpr(base.Pos, ir.OCALL, ir.NewSelectorExpr(base.Pos, ir.OXDOT, ptr, meth), nil)
|
||||||
call.Args = ir.ParamNames(tfn.Type())
|
call.Args = ir.ParamNames(tfn.Type())
|
||||||
|
|
Loading…
Reference in a new issue