diff options
Diffstat (limited to 'src/cmd/compile/internal/ir/name.go')
-rw-r--r-- | src/cmd/compile/internal/ir/name.go | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/src/cmd/compile/internal/ir/name.go b/src/cmd/compile/internal/ir/name.go index ff9784df1b..a2eec05013 100644 --- a/src/cmd/compile/internal/ir/name.go +++ b/src/cmd/compile/internal/ir/name.go @@ -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 |