[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:
Matthew Dempsky 2021-07-02 16:29:42 -07:00
parent 611056ec34
commit 5dac279fbd
8 changed files with 43 additions and 39 deletions

View file

@ -343,9 +343,7 @@ func (e *escape) wrapExpr(pos src.XPos, exprp *ir.Node, init *ir.Nodes, call ir.
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

View file

@ -183,8 +183,14 @@ func (b *batch) initFunc(fn *ir.Func) {
// 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)
}
}

View file

@ -46,9 +46,6 @@ func (e *escape) exprSkipInit(k hole, n ir.Node) {
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:

View file

@ -222,7 +222,9 @@ func (e *escape) newLoc(n ir.Node, transient bool) *location {
}
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,
@ -234,7 +236,9 @@ func (e *escape) newLoc(n ir.Node, transient bool) *location {
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)
}

View file

@ -358,7 +358,7 @@ func (n *Name) Byval() bool {
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())
@ -368,11 +368,33 @@ func NewClosureVar(pos src.XPos, fn *Func, n *Name) *Name {
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

View file

@ -1623,11 +1623,7 @@ func (r *reader) funcLit() ir.Node {
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)
@ -2204,17 +2200,10 @@ func (r *reader) methodValueWrapper(tbase *types.Type, method *types.Field, targ
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)

View file

@ -1289,13 +1289,8 @@ func (r *importReader) node() ir.Node {
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

View file

@ -238,17 +238,10 @@ func methodValueWrapper(dot *ir.SelectorExpr) *ir.Func {
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())