]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.typeparams] cmd/compile: formalize "hidden parameters" idea
authorMatthew Dempsky <mdempsky@google.com>
Fri, 2 Jul 2021 23:29:42 +0000 (16:29 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Sat, 3 Jul 2021 10:21:47 +0000 (10:21 +0000)
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>
src/cmd/compile/internal/escape/call.go
src/cmd/compile/internal/escape/escape.go
src/cmd/compile/internal/escape/expr.go
src/cmd/compile/internal/escape/graph.go
src/cmd/compile/internal/ir/name.go
src/cmd/compile/internal/noder/reader.go
src/cmd/compile/internal/typecheck/iimport.go
src/cmd/compile/internal/walk/closure.go

index 46bfe65aff1b7b2f751aca330341983e3da0d244..5bd748027e9b177e468f3850e8264ff88f956a35 100644 (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
index 04d0c2356c1e3d14085a8dce47f197df29c8900f..61e0121a40aec2ea54b6829932eecd3641e00ba6 100644 (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)
                }
        }
 
index dfcd55734abe703c1c8196b65617531cb33dac5b..c2a679d4749922282afc020b8dafd88648431c0a 100644 (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:
index 6316435dfe45f93c8362b3cb4726bc40164a290e..d3ae1da693e5833de105eaf563d56878f5bb0dfe 100644 (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)
                        }
 
index ff9784df1b15a16ab8dfb5c4eb0903b0f3c406e2..a2eec05013e1092076ec62524c1f3235bc5d1fd1 100644 (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
index 275baead04b5ff85d9cd8601f3d33788c6f5d675..14d982a1af0219e20ad64b9180b92f188ec3206e 100644 (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)
 
index 7b61260e7988b6ca7af008b0116767235c07e36b..a45bbfd1f82aafb95dc7030f891a5249b2fe97b4 100644 (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
index a86ed2ab80531b941f659de26258c08df11b730c..7f6ef473bf219f4c09e9ebf95bcc8641b4c97231 100644 (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())