[dev.regabi] cmd/compile: add ir.Closure, ir.ClosureRead

Closures are another reference to Funcs,
and it cleans up the code quite a bit to be clear about types.

OCLOSUREVAR is renamed to OCLOSUREREAD to make
clearer that it is unrelated to the list Func.ClosureVars.

Passes buildall w/ toolstash -cmp.

Change-Id: Id0d28df2d4d6e9954e34df7a39ea226995eee937
Reviewed-on: https://go-review.googlesource.com/c/go/+/274098
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Russ Cox 2020-11-26 00:47:44 -05:00
parent e84b27bec5
commit 4eaef981b5
11 changed files with 82 additions and 51 deletions

View file

@ -6,6 +6,8 @@ package ir
import (
"cmd/compile/internal/types"
"cmd/internal/src"
"fmt"
)
// A miniStmt is a miniNode with extra fields common to expressions.
@ -45,3 +47,40 @@ func (n *miniExpr) SetBounded(b bool) { n.flags.set(miniExprBounded, b) }
func (n *miniExpr) Init() Nodes { return n.init }
func (n *miniExpr) PtrInit() *Nodes { return &n.init }
func (n *miniExpr) SetInit(x Nodes) { n.init = x }
// A ClosureExpr is a function literal expression.
type ClosureExpr struct {
miniExpr
fn *Func
}
func NewClosureExpr(pos src.XPos, fn *Func) *ClosureExpr {
n := &ClosureExpr{fn: fn}
n.op = OCLOSURE
n.pos = pos
return n
}
func (n *ClosureExpr) String() string { return fmt.Sprint(n) }
func (n *ClosureExpr) Format(s fmt.State, verb rune) { FmtNode(n, s, verb) }
func (n *ClosureExpr) RawCopy() Node { c := *n; return &c }
func (n *ClosureExpr) Func() *Func { return n.fn }
// A ClosureRead denotes reading a variable stored within a closure struct.
type ClosureRead struct {
miniExpr
offset int64
}
func NewClosureRead(typ *types.Type, offset int64) *ClosureRead {
n := &ClosureRead{offset: offset}
n.typ = typ
n.op = OCLOSUREREAD
return n
}
func (n *ClosureRead) String() string { return fmt.Sprint(n) }
func (n *ClosureRead) Format(s fmt.State, verb rune) { FmtNode(n, s, verb) }
func (n *ClosureRead) RawCopy() Node { c := *n; return &c }
func (n *ClosureRead) Type() *types.Type { return n.typ }
func (n *ClosureRead) Offset() int64 { return n.offset }