mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
Now that the generic graph structure methods - Left, Right, and so on -
have been removed from the Node interface, each implementation's uses
can be replaced with direct field access, using more specific names,
and the methods themselves can be deleted.
Passes buildall w/ toolstash -cmp.
[git-generate]
cd src/cmd/compile/internal/ir
rf '
mv Func.iota Func.Iota_
mv Name.fn Name.Func_
'
cd ../gc
rf '
ex . ../ir {
import "cmd/compile/internal/ir"
import "cmd/compile/internal/types"
var ns ir.Nodes
var b bool
var i64 int64
var n ir.Node
var op ir.Op
var sym *types.Sym
var class ir.Class
var decl *ir.Decl
decl.Left() -> decl.X
decl.SetLeft(n) -> decl.X = n
var asl *ir.AssignListStmt
asl.List() -> asl.Lhs
asl.PtrList() -> &asl.Lhs
asl.SetList(ns) -> asl.Lhs = ns
asl.Rlist() -> asl.Rhs
asl.PtrRlist() -> &asl.Rhs
asl.SetRlist(ns) -> asl.Rhs = ns
asl.Colas() -> asl.Def
asl.SetColas(b) -> asl.Def = b
var as *ir.AssignStmt
as.Left() -> as.X
as.SetLeft(n) -> as.X = n
as.Right() -> as.Y
as.SetRight(n) -> as.Y = n
as.Colas() -> as.Def
as.SetColas(b) -> as.Def = b
var ao *ir.AssignOpStmt
ao.Left() -> ao.X
ao.SetLeft(n) -> ao.X = n
ao.Right() -> ao.Y
ao.SetRight(n) -> ao.Y = n
ao.SubOp() -> ao.AsOp
ao.SetSubOp(op) -> ao.AsOp = op
ao.Implicit() -> ao.IncDec
ao.SetImplicit(b) -> ao.IncDec = b
var bl *ir.BlockStmt
bl.List() -> bl.List_
bl.PtrList() -> &bl.List_
bl.SetList(ns) -> bl.List_ = ns
var br *ir.BranchStmt
br.Sym() -> br.Label
br.SetSym(sym) -> br.Label = sym
var cas *ir.CaseStmt
cas.List() -> cas.List_
cas.PtrList() -> &cas.List_
cas.SetList(ns) -> cas.List_ = ns
cas.Body() -> cas.Body_
cas.PtrBody() -> &cas.Body_
cas.SetBody(ns) -> cas.Body_ = ns
cas.Rlist() -> cas.Vars
cas.PtrRlist() -> &cas.Vars
cas.SetRlist(ns) -> cas.Vars = ns
cas.Left() -> cas.Comm
cas.SetLeft(n) -> cas.Comm = n
var fr *ir.ForStmt
fr.Sym() -> fr.Label
fr.SetSym(sym) -> fr.Label = sym
fr.Left() -> fr.Cond
fr.SetLeft(n) -> fr.Cond = n
fr.Right() -> fr.Post
fr.SetRight(n) -> fr.Post = n
fr.Body() -> fr.Body_
fr.PtrBody() -> &fr.Body_
fr.SetBody(ns) -> fr.Body_ = ns
fr.List() -> fr.Late
fr.PtrList() -> &fr.Late
fr.SetList(ns) -> fr.Late = ns
fr.HasBreak() -> fr.HasBreak_
fr.SetHasBreak(b) -> fr.HasBreak_ = b
var gs *ir.GoDeferStmt
gs.Left() -> gs.Call
gs.SetLeft(n) -> gs.Call = n
var ifs *ir.IfStmt
ifs.Left() -> ifs.Cond
ifs.SetLeft(n) -> ifs.Cond = n
ifs.Body() -> ifs.Body_
ifs.PtrBody() -> &ifs.Body_
ifs.SetBody(ns) -> ifs.Body_ = ns
ifs.Rlist() -> ifs.Else
ifs.PtrRlist() -> &ifs.Else
ifs.SetRlist(ns) -> ifs.Else = ns
ifs.Likely() -> ifs.Likely_
ifs.SetLikely(b) -> ifs.Likely_ = b
var im *ir.InlineMarkStmt
im.Offset() -> im.Index
im.SetOffset(i64) -> im.Index = i64
var lab *ir.LabelStmt
lab.Sym() -> lab.Label
lab.SetSym(sym) -> lab.Label = sym
var rng *ir.RangeStmt
rng.Sym() -> rng.Label
rng.SetSym(sym) -> rng.Label = sym
rng.Right() -> rng.X
rng.SetRight(n) -> rng.X = n
rng.Body() -> rng.Body_
rng.PtrBody() -> &rng.Body_
rng.SetBody(ns) -> rng.Body_ = ns
rng.List() -> rng.Vars
rng.PtrList() -> &rng.Vars
rng.SetList(ns) -> rng.Vars = ns
rng.HasBreak() -> rng.HasBreak_
rng.SetHasBreak(b) -> rng.HasBreak_ = b
rng.Colas() -> rng.Def
rng.SetColas(b) -> rng.Def = b
var ret *ir.ReturnStmt
ret.List() -> ret.Results
ret.PtrList() -> &ret.Results
ret.SetList(ns) -> ret.Results = ns
var sel *ir.SelectStmt
sel.List() -> sel.Cases
sel.PtrList() -> &sel.Cases
sel.SetList(ns) -> sel.Cases = ns
sel.Sym() -> sel.Label
sel.SetSym(sym) -> sel.Label = sym
sel.HasBreak() -> sel.HasBreak_
sel.SetHasBreak(b) -> sel.HasBreak_ = b
sel.Body() -> sel.Compiled
sel.PtrBody() -> &sel.Compiled
sel.SetBody(ns) -> sel.Compiled = ns
var send *ir.SendStmt
send.Left() -> send.Chan
send.SetLeft(n) -> send.Chan = n
send.Right() -> send.Value
send.SetRight(n) -> send.Value = n
var sw *ir.SwitchStmt
sw.Left() -> sw.Tag
sw.SetLeft(n) -> sw.Tag = n
sw.List() -> sw.Cases
sw.PtrList() -> &sw.Cases
sw.SetList(ns) -> sw.Cases = ns
sw.Body() -> sw.Compiled
sw.PtrBody() -> &sw.Compiled
sw.SetBody(ns) -> sw.Compiled = ns
sw.Sym() -> sw.Label
sw.SetSym(sym) -> sw.Label = sym
sw.HasBreak() -> sw.HasBreak_
sw.SetHasBreak(b) -> sw.HasBreak_ = b
var tg *ir.TypeSwitchGuard
tg.Left() -> tg.Tag
tg.SetLeft(nil) -> tg.Tag = nil
tg.SetLeft(n) -> tg.Tag = n.(*ir.Ident)
tg.Right() -> tg.X
tg.SetRight(n) -> tg.X = n
var adds *ir.AddStringExpr
adds.List() -> adds.List_
adds.PtrList() -> &adds.List_
adds.SetList(ns) -> adds.List_ = ns
var addr *ir.AddrExpr
addr.Left() -> addr.X
addr.SetLeft(n) -> addr.X = n
addr.Right() -> addr.Alloc
addr.SetRight(n) -> addr.Alloc = n
var bin *ir.BinaryExpr
bin.Left() -> bin.X
bin.SetLeft(n) -> bin.X = n
bin.Right() -> bin.Y
bin.SetRight(n) -> bin.Y = n
var log *ir.LogicalExpr
log.Left() -> log.X
log.SetLeft(n) -> log.X = n
log.Right() -> log.Y
log.SetRight(n) -> log.Y = n
var call *ir.CallExpr
call.Left() -> call.X
call.SetLeft(n) -> call.X = n
call.List() -> call.Args
call.PtrList() -> &call.Args
call.SetList(ns) -> call.Args = ns
call.Rlist() -> call.Rargs
call.PtrRlist() -> &call.Rargs
call.SetRlist(ns) -> call.Rargs = ns
call.IsDDD() -> call.DDD
call.SetIsDDD(b) -> call.DDD = b
call.NoInline() -> call.NoInline_
call.SetNoInline(b) -> call.NoInline_ = b
call.Body() -> call.Body_
call.PtrBody() -> &call.Body_
call.SetBody(ns) -> call.Body_ = ns
var cp *ir.CallPartExpr
cp.Func() -> cp.Func_
cp.Left() -> cp.X
cp.SetLeft(n) -> cp.X = n
cp.Sym() -> cp.Method.Sym
var clo *ir.ClosureExpr
clo.Func() -> clo.Func_
var cr *ir.ClosureReadExpr
cr.Offset() -> cr.Offset_
var cl *ir.CompLitExpr
cl.Right() -> cl.Ntype
cl.SetRight(nil) -> cl.Ntype = nil
cl.SetRight(n) -> cl.Ntype = ir.Node(n).(ir.Ntype)
cl.List() -> cl.List_
cl.PtrList() -> &cl.List_
cl.SetList(ns) -> cl.List_ = ns
var conv *ir.ConvExpr
conv.Left() -> conv.X
conv.SetLeft(n) -> conv.X = n
var ix *ir.IndexExpr
ix.Left() -> ix.X
ix.SetLeft(n) -> ix.X = n
ix.Right() -> ix.Index
ix.SetRight(n) -> ix.Index = n
ix.IndexMapLValue() -> ix.Assigned
ix.SetIndexMapLValue(b) -> ix.Assigned = b
var kv *ir.KeyExpr
kv.Left() -> kv.Key
kv.SetLeft(n) -> kv.Key = n
kv.Right() -> kv.Value
kv.SetRight(n) -> kv.Value = n
var sk *ir.StructKeyExpr
sk.Sym() -> sk.Field
sk.SetSym(sym) -> sk.Field = sym
sk.Left() -> sk.Value
sk.SetLeft(n) -> sk.Value = n
sk.Offset() -> sk.Offset_
sk.SetOffset(i64) -> sk.Offset_ = i64
var ic *ir.InlinedCallExpr
ic.Body() -> ic.Body_
ic.PtrBody() -> &ic.Body_
ic.SetBody(ns) -> ic.Body_ = ns
ic.Rlist() -> ic.ReturnVars
ic.PtrRlist() -> &ic.ReturnVars
ic.SetRlist(ns) -> ic.ReturnVars = ns
var mak *ir.MakeExpr
mak.Left() -> mak.Len
mak.SetLeft(n) -> mak.Len = n
mak.Right() -> mak.Cap
mak.SetRight(n) -> mak.Cap = n
var par *ir.ParenExpr
par.Left() -> par.X
par.SetLeft(n) -> par.X = n
var res *ir.ResultExpr
res.Offset() -> res.Offset_
res.SetOffset(i64) -> res.Offset_ = i64
var dot *ir.SelectorExpr
dot.Left() -> dot.X
dot.SetLeft(n) -> dot.X = n
dot.Sym() -> dot.Sel
dot.SetSym(sym) -> dot.Sel = sym
dot.Offset() -> dot.Offset_
dot.SetOffset(i64) -> dot.Offset_ = i64
var sl *ir.SliceExpr
sl.Left() -> sl.X
sl.SetLeft(n) -> sl.X = n
sl.List() -> sl.List_
sl.PtrList() -> &sl.List_
sl.SetList(ns) -> sl.List_ = ns
var sh *ir.SliceHeaderExpr
sh.Left() -> sh.Ptr
sh.SetLeft(n) -> sh.Ptr = n
sh.List() -> sh.LenCap_
sh.PtrList() -> &sh.LenCap_
sh.SetList(ns) -> sh.LenCap_ = ns
var st *ir.StarExpr
st.Left() -> st.X
st.SetLeft(n) -> st.X = n
var ta *ir.TypeAssertExpr
ta.Left() -> ta.X
ta.SetLeft(n) -> ta.X = n
ta.Right() -> ta.Ntype
ta.SetRight(n) -> ta.Ntype = n
ta.List() -> ta.Itab
ta.PtrList() -> &ta.Itab
ta.SetList(ns) -> ta.Itab = ns
var u *ir.UnaryExpr
u.Left() -> u.X
u.SetLeft(n) -> u.X = n
var fn *ir.Func
fn.Body() -> fn.Body_
fn.PtrBody() -> &fn.Body_
fn.SetBody(ns) -> fn.Body_ = ns
fn.Iota() -> fn.Iota_
fn.SetIota(i64) -> fn.Iota_ = i64
fn.Func() -> fn
var nam *ir.Name
nam.SubOp() -> nam.BuiltinOp
nam.SetSubOp(op) -> nam.BuiltinOp = op
nam.Class() -> nam.Class_
nam.SetClass(class) -> nam.Class_ = class
nam.Func() -> nam.Func_
nam.Offset() -> nam.Offset_
nam.SetOffset(i64) -> nam.Offset_ = i64
}
ex . ../ir {
import "cmd/compile/internal/ir"
var n ir.Nodes
(&n).Append -> n.Append
(&n).AppendNodes -> n.AppendNodes
(&n).MoveNodes -> n.MoveNodes
(&n).Prepend -> n.Prepend
(&n).Set -> n.Set
(&n).Set1 -> n.Set1
(&n).Set2 -> n.Set2
(&n).Set3 -> n.Set3
var ntype ir.Ntype
ir.Node(ntype).(ir.Ntype) -> ntype
}
'
cd ../ir
rf '
rm \
Decl.Left Decl.SetLeft \
AssignListStmt.List AssignListStmt.PtrList AssignListStmt.SetList \
AssignListStmt.Rlist AssignListStmt.PtrRlist AssignListStmt.SetRlist \
AssignListStmt.Colas AssignListStmt.SetColas \
AssignStmt.Left AssignStmt.SetLeft \
AssignStmt.Right AssignStmt.SetRight \
AssignStmt.Colas AssignStmt.SetColas \
AssignOpStmt.Left AssignOpStmt.SetLeft \
AssignOpStmt.Right AssignOpStmt.SetRight \
AssignOpStmt.SubOp AssignOpStmt.SetSubOp \
AssignOpStmt.Implicit AssignOpStmt.SetImplicit \
BlockStmt.List BlockStmt.PtrList BlockStmt.SetList \
BranchStmt.SetSym \
CaseStmt.List CaseStmt.PtrList CaseStmt.SetList \
CaseStmt.Body CaseStmt.PtrBody CaseStmt.SetBody \
CaseStmt.Rlist CaseStmt.PtrRlist CaseStmt.SetRlist \
CaseStmt.Left CaseStmt.SetLeft \
ForStmt.Left ForStmt.SetLeft \
ForStmt.Right ForStmt.SetRight \
ForStmt.Body ForStmt.PtrBody ForStmt.SetBody \
ForStmt.List ForStmt.PtrList ForStmt.SetList \
ForStmt.HasBreak ForStmt.SetHasBreak \
ForStmt.Sym ForStmt.SetSym \
GoDeferStmt.Left GoDeferStmt.SetLeft \
IfStmt.Left IfStmt.SetLeft \
IfStmt.Body IfStmt.PtrBody IfStmt.SetBody \
IfStmt.Rlist IfStmt.PtrRlist IfStmt.SetRlist \
IfStmt.Likely IfStmt.SetLikely \
LabelStmt.SetSym \
RangeStmt.Right RangeStmt.SetRight \
RangeStmt.Body RangeStmt.PtrBody RangeStmt.SetBody \
RangeStmt.List RangeStmt.PtrList RangeStmt.SetList \
RangeStmt.HasBreak RangeStmt.SetHasBreak \
RangeStmt.Colas RangeStmt.SetColas \
RangeStmt.Sym RangeStmt.SetSym \
ReturnStmt.List ReturnStmt.PtrList ReturnStmt.SetList \
SelectStmt.List SelectStmt.PtrList SelectStmt.SetList \
SelectStmt.HasBreak SelectStmt.SetHasBreak \
SelectStmt.Body SelectStmt.PtrBody SelectStmt.SetBody \
SelectStmt.Sym SelectStmt.SetSym \
SendStmt.Left SendStmt.SetLeft \
SendStmt.Right SendStmt.SetRight \
SwitchStmt.Left SwitchStmt.SetLeft \
SwitchStmt.List SwitchStmt.PtrList SwitchStmt.SetList \
SwitchStmt.Body SwitchStmt.PtrBody SwitchStmt.SetBody \
SwitchStmt.HasBreak SwitchStmt.SetHasBreak \
SwitchStmt.Sym SwitchStmt.SetSym \
TypeSwitchGuard.Left TypeSwitchGuard.SetLeft \
TypeSwitchGuard.Right TypeSwitchGuard.SetRight \
AddStringExpr.List AddStringExpr.PtrList AddStringExpr.SetList \
AddrExpr.Left AddrExpr.SetLeft \
AddrExpr.Right AddrExpr.SetRight \
BinaryExpr.Left BinaryExpr.SetLeft \
BinaryExpr.Right BinaryExpr.SetRight \
LogicalExpr.Left LogicalExpr.SetLeft \
LogicalExpr.Right LogicalExpr.SetRight \
CallExpr.Left CallExpr.SetLeft \
CallExpr.List CallExpr.PtrList CallExpr.SetList \
CallExpr.Rlist CallExpr.PtrRlist CallExpr.SetRlist \
CallExpr.NoInline CallExpr.SetNoInline \
CallExpr.Body CallExpr.PtrBody CallExpr.SetBody \
CallExpr.IsDDD CallExpr.SetIsDDD \
CallPartExpr.Left CallPartExpr.SetLeft \
ClosureReadExpr.Offset \
ClosureReadExpr.Type \ # provided by miniExpr already
CompLitExpr.Right CompLitExpr.SetRight \
CompLitExpr.List CompLitExpr.PtrList CompLitExpr.SetList \
ConvExpr.Left ConvExpr.SetLeft \
IndexExpr.Left IndexExpr.SetLeft \
IndexExpr.Right IndexExpr.SetRight \
IndexExpr.IndexMapLValue IndexExpr.SetIndexMapLValue \
KeyExpr.Left KeyExpr.SetLeft \
KeyExpr.Right KeyExpr.SetRight \
StructKeyExpr.Left StructKeyExpr.SetLeft \
StructKeyExpr.Offset StructKeyExpr.SetOffset \
StructKeyExpr.SetSym \
InlinedCallExpr.Body InlinedCallExpr.PtrBody InlinedCallExpr.SetBody \
InlinedCallExpr.Rlist InlinedCallExpr.PtrRlist InlinedCallExpr.SetRlist \
MakeExpr.Left MakeExpr.SetLeft \
MakeExpr.Right MakeExpr.SetRight \
MethodExpr.Left MethodExpr.SetLeft \
MethodExpr.Right MethodExpr.SetRight \
MethodExpr.Offset MethodExpr.SetOffset \
MethodExpr.Class MethodExpr.SetClass \
ParenExpr.Left ParenExpr.SetLeft \
ResultExpr.Offset ResultExpr.SetOffset \
ReturnStmt.IsDDD \
SelectorExpr.Left SelectorExpr.SetLeft \
SelectorExpr.Offset SelectorExpr.SetOffset \
SelectorExpr.SetSym \
SliceExpr.Left SliceExpr.SetLeft \
SliceExpr.List SliceExpr.PtrList SliceExpr.SetList \
SliceHeaderExpr.Left SliceHeaderExpr.SetLeft \
SliceHeaderExpr.List SliceHeaderExpr.PtrList SliceHeaderExpr.SetList \
StarExpr.Left StarExpr.SetLeft \
TypeAssertExpr.Left TypeAssertExpr.SetLeft \
TypeAssertExpr.Right TypeAssertExpr.SetRight \
TypeAssertExpr.List TypeAssertExpr.PtrList TypeAssertExpr.SetList \
UnaryExpr.Left UnaryExpr.SetLeft \
Func.Body Func.PtrBody Func.SetBody \
Func.Iota Func.SetIota \
CallPartExpr.Func ClosureExpr.Func Func.Func Name.Func \
mv BlockStmt.List_ BlockStmt.List
mv CaseStmt.List_ CaseStmt.List
mv CaseStmt.Body_ CaseStmt.Body
mv ForStmt.Body_ ForStmt.Body
mv ForStmt.HasBreak_ ForStmt.HasBreak
mv Func.Iota_ Func.Iota
mv IfStmt.Body_ IfStmt.Body
mv IfStmt.Likely_ IfStmt.Likely
mv RangeStmt.Body_ RangeStmt.Body
mv RangeStmt.HasBreak_ RangeStmt.HasBreak
mv SelectStmt.HasBreak_ SelectStmt.HasBreak
mv SwitchStmt.HasBreak_ SwitchStmt.HasBreak
mv AddStringExpr.List_ AddStringExpr.List
mv CallExpr.NoInline_ CallExpr.NoInline
mv CallExpr.Body_ CallExpr.Body # TODO what is this?
mv CallExpr.DDD CallExpr.IsDDD
mv ClosureReadExpr.Offset_ ClosureReadExpr.Offset
mv CompLitExpr.List_ CompLitExpr.List
mv StructKeyExpr.Offset_ StructKeyExpr.Offset
mv InlinedCallExpr.Body_ InlinedCallExpr.Body
mv ResultExpr.Offset_ ResultExpr.Offset
mv SelectorExpr.Offset_ SelectorExpr.Offset
mv SliceExpr.List_ SliceExpr.List
mv SliceHeaderExpr.LenCap_ SliceHeaderExpr.LenCap
mv Func.Body_ Func.Body
mv CallPartExpr.Func_ CallPartExpr.Func
mv ClosureExpr.Func_ ClosureExpr.Func
mv Name.Func_ Name.Func
'
Change-Id: Ia2ee59649674f83eb123e63fda7a7781cf91cc56
Reviewed-on: https://go-review.googlesource.com/c/go/+/277935
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>
570 lines
16 KiB
Go
570 lines
16 KiB
Go
// Copyright 2009 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package gc
|
|
|
|
import (
|
|
"cmd/compile/internal/base"
|
|
"cmd/compile/internal/ir"
|
|
"cmd/compile/internal/syntax"
|
|
"cmd/compile/internal/types"
|
|
"cmd/internal/src"
|
|
"fmt"
|
|
)
|
|
|
|
func (p *noder) funcLit(expr *syntax.FuncLit) ir.Node {
|
|
xtype := p.typeExpr(expr.Type)
|
|
ntype := p.typeExpr(expr.Type)
|
|
|
|
fn := ir.NewFunc(p.pos(expr))
|
|
fn.SetIsHiddenClosure(Curfn != nil)
|
|
fn.Nname = newFuncNameAt(p.pos(expr), ir.BlankNode.Sym(), fn) // filled in by typecheckclosure
|
|
fn.Nname.Ntype = xtype
|
|
fn.Nname.Defn = fn
|
|
|
|
clo := ir.NewClosureExpr(p.pos(expr), fn)
|
|
fn.ClosureType = ntype
|
|
fn.OClosure = clo
|
|
|
|
p.funcBody(fn, expr.Body)
|
|
|
|
// closure-specific variables are hanging off the
|
|
// ordinary ones in the symbol table; see oldname.
|
|
// unhook them.
|
|
// make the list of pointers for the closure call.
|
|
for _, v := range fn.ClosureVars {
|
|
// Unlink from v1; see comment in syntax.go type Param for these fields.
|
|
v1 := v.Defn
|
|
v1.Name().Innermost = v.Outer
|
|
|
|
// If the closure usage of v is not dense,
|
|
// we need to make it dense; now that we're out
|
|
// of the function in which v appeared,
|
|
// look up v.Sym in the enclosing function
|
|
// and keep it around for use in the compiled code.
|
|
//
|
|
// That is, suppose we just finished parsing the innermost
|
|
// closure f4 in this code:
|
|
//
|
|
// func f() {
|
|
// v := 1
|
|
// func() { // f2
|
|
// use(v)
|
|
// func() { // f3
|
|
// func() { // f4
|
|
// use(v)
|
|
// }()
|
|
// }()
|
|
// }()
|
|
// }
|
|
//
|
|
// At this point v.Outer is f2's v; there is no f3's v.
|
|
// To construct the closure f4 from within f3,
|
|
// we need to use f3's v and in this case we need to create f3's v.
|
|
// We are now in the context of f3, so calling oldname(v.Sym)
|
|
// obtains f3's v, creating it if necessary (as it is in the example).
|
|
//
|
|
// capturevars will decide whether to use v directly or &v.
|
|
v.Outer = oldname(v.Sym()).(*ir.Name)
|
|
}
|
|
|
|
return clo
|
|
}
|
|
|
|
// typecheckclosure typechecks an OCLOSURE node. It also creates the named
|
|
// function associated with the closure.
|
|
// TODO: This creation of the named function should probably really be done in a
|
|
// separate pass from type-checking.
|
|
func typecheckclosure(clo *ir.ClosureExpr, top int) {
|
|
fn := clo.Func
|
|
// Set current associated iota value, so iota can be used inside
|
|
// function in ConstSpec, see issue #22344
|
|
if x := getIotaValue(); x >= 0 {
|
|
fn.Iota = x
|
|
}
|
|
|
|
fn.ClosureType = typecheck(fn.ClosureType, ctxType)
|
|
clo.SetType(fn.ClosureType.Type())
|
|
fn.SetClosureCalled(top&ctxCallee != 0)
|
|
|
|
// Do not typecheck fn twice, otherwise, we will end up pushing
|
|
// fn to Target.Decls multiple times, causing initLSym called twice.
|
|
// See #30709
|
|
if fn.Typecheck() == 1 {
|
|
return
|
|
}
|
|
|
|
for _, ln := range fn.ClosureVars {
|
|
n := ln.Defn
|
|
if !n.Name().Captured() {
|
|
n.Name().SetCaptured(true)
|
|
if n.Name().Decldepth == 0 {
|
|
base.Fatalf("typecheckclosure: var %v does not have decldepth assigned", n)
|
|
}
|
|
|
|
// Ignore assignments to the variable in straightline code
|
|
// preceding the first capturing by a closure.
|
|
if n.Name().Decldepth == decldepth {
|
|
n.Name().SetAssigned(false)
|
|
}
|
|
}
|
|
}
|
|
|
|
fn.Nname.SetSym(closurename(Curfn))
|
|
setNodeNameFunc(fn.Nname)
|
|
typecheckFunc(fn)
|
|
|
|
// Type check the body now, but only if we're inside a function.
|
|
// At top level (in a variable initialization: curfn==nil) we're not
|
|
// ready to type check code yet; we'll check it later, because the
|
|
// underlying closure function we create is added to Target.Decls.
|
|
if Curfn != nil && clo.Type() != nil {
|
|
oldfn := Curfn
|
|
Curfn = fn
|
|
olddd := decldepth
|
|
decldepth = 1
|
|
typecheckslice(fn.Body.Slice(), ctxStmt)
|
|
decldepth = olddd
|
|
Curfn = oldfn
|
|
}
|
|
|
|
Target.Decls = append(Target.Decls, fn)
|
|
}
|
|
|
|
// globClosgen is like Func.Closgen, but for the global scope.
|
|
var globClosgen int32
|
|
|
|
// closurename generates a new unique name for a closure within
|
|
// outerfunc.
|
|
func closurename(outerfunc *ir.Func) *types.Sym {
|
|
outer := "glob."
|
|
prefix := "func"
|
|
gen := &globClosgen
|
|
|
|
if outerfunc != nil {
|
|
if outerfunc.OClosure != nil {
|
|
prefix = ""
|
|
}
|
|
|
|
outer = ir.FuncName(outerfunc)
|
|
|
|
// There may be multiple functions named "_". In those
|
|
// cases, we can't use their individual Closgens as it
|
|
// would lead to name clashes.
|
|
if !ir.IsBlank(outerfunc.Nname) {
|
|
gen = &outerfunc.Closgen
|
|
}
|
|
}
|
|
|
|
*gen++
|
|
return lookup(fmt.Sprintf("%s.%s%d", outer, prefix, *gen))
|
|
}
|
|
|
|
// capturevarscomplete is set to true when the capturevars phase is done.
|
|
var capturevarscomplete bool
|
|
|
|
// capturevars is called in a separate phase after all typechecking is done.
|
|
// It decides whether each variable captured by a closure should be captured
|
|
// by value or by reference.
|
|
// We use value capturing for values <= 128 bytes that are never reassigned
|
|
// after capturing (effectively constant).
|
|
func capturevars(fn *ir.Func) {
|
|
lno := base.Pos
|
|
base.Pos = fn.Pos()
|
|
cvars := fn.ClosureVars
|
|
out := cvars[:0]
|
|
for _, v := range cvars {
|
|
if v.Type() == nil {
|
|
// If v.Type is nil, it means v looked like it
|
|
// was going to be used in the closure, but
|
|
// isn't. This happens in struct literals like
|
|
// s{f: x} where we can't distinguish whether
|
|
// f is a field identifier or expression until
|
|
// resolving s.
|
|
continue
|
|
}
|
|
out = append(out, v)
|
|
|
|
// type check the & of closed variables outside the closure,
|
|
// so that the outer frame also grabs them and knows they escape.
|
|
dowidth(v.Type())
|
|
|
|
var outer ir.Node
|
|
outer = v.Outer
|
|
outermost := v.Defn.(*ir.Name)
|
|
|
|
// out parameters will be assigned to implicitly upon return.
|
|
if outermost.Class_ != ir.PPARAMOUT && !outermost.Name().Addrtaken() && !outermost.Name().Assigned() && v.Type().Width <= 128 {
|
|
v.SetByval(true)
|
|
} else {
|
|
outermost.Name().SetAddrtaken(true)
|
|
outer = nodAddr(outer)
|
|
}
|
|
|
|
if base.Flag.LowerM > 1 {
|
|
var name *types.Sym
|
|
if v.Curfn != nil && v.Curfn.Nname != nil {
|
|
name = v.Curfn.Sym()
|
|
}
|
|
how := "ref"
|
|
if v.Byval() {
|
|
how = "value"
|
|
}
|
|
base.WarnfAt(v.Pos(), "%v capturing by %s: %v (addr=%v assign=%v width=%d)", name, how, v.Sym(), outermost.Name().Addrtaken(), outermost.Name().Assigned(), int32(v.Type().Width))
|
|
}
|
|
|
|
outer = typecheck(outer, ctxExpr)
|
|
fn.ClosureEnter.Append(outer)
|
|
}
|
|
|
|
fn.ClosureVars = out
|
|
base.Pos = lno
|
|
}
|
|
|
|
// transformclosure is called in a separate phase after escape analysis.
|
|
// It transform closure bodies to properly reference captured variables.
|
|
func transformclosure(fn *ir.Func) {
|
|
lno := base.Pos
|
|
base.Pos = fn.Pos()
|
|
|
|
if fn.ClosureCalled() {
|
|
// If the closure is directly called, we transform it to a plain function call
|
|
// with variables passed as args. This avoids allocation of a closure object.
|
|
// Here we do only a part of the transformation. Walk of OCALLFUNC(OCLOSURE)
|
|
// will complete the transformation later.
|
|
// For illustration, the following closure:
|
|
// func(a int) {
|
|
// println(byval)
|
|
// byref++
|
|
// }(42)
|
|
// becomes:
|
|
// func(byval int, &byref *int, a int) {
|
|
// println(byval)
|
|
// (*&byref)++
|
|
// }(byval, &byref, 42)
|
|
|
|
// f is ONAME of the actual function.
|
|
f := fn.Nname
|
|
|
|
// We are going to insert captured variables before input args.
|
|
var params []*types.Field
|
|
var decls []*ir.Name
|
|
for _, v := range fn.ClosureVars {
|
|
if !v.Byval() {
|
|
// If v of type T is captured by reference,
|
|
// we introduce function param &v *T
|
|
// and v remains PAUTOHEAP with &v heapaddr
|
|
// (accesses will implicitly deref &v).
|
|
addr := NewName(lookup("&" + v.Sym().Name))
|
|
addr.SetType(types.NewPtr(v.Type()))
|
|
v.Heapaddr = addr
|
|
v = addr
|
|
}
|
|
|
|
v.Class_ = ir.PPARAM
|
|
decls = append(decls, v)
|
|
|
|
fld := types.NewField(src.NoXPos, v.Sym(), v.Type())
|
|
fld.Nname = v
|
|
params = append(params, fld)
|
|
}
|
|
|
|
if len(params) > 0 {
|
|
// Prepend params and decls.
|
|
f.Type().Params().SetFields(append(params, f.Type().Params().FieldSlice()...))
|
|
fn.Dcl = append(decls, fn.Dcl...)
|
|
}
|
|
|
|
dowidth(f.Type())
|
|
fn.SetType(f.Type()) // update type of ODCLFUNC
|
|
} else {
|
|
// The closure is not called, so it is going to stay as closure.
|
|
var body []ir.Node
|
|
offset := int64(Widthptr)
|
|
for _, v := range fn.ClosureVars {
|
|
// cv refers to the field inside of closure OSTRUCTLIT.
|
|
typ := v.Type()
|
|
if !v.Byval() {
|
|
typ = types.NewPtr(typ)
|
|
}
|
|
offset = Rnd(offset, int64(typ.Align))
|
|
cr := ir.NewClosureRead(typ, offset)
|
|
offset += typ.Width
|
|
|
|
if v.Byval() && v.Type().Width <= int64(2*Widthptr) {
|
|
// If it is a small variable captured by value, downgrade it to PAUTO.
|
|
v.Class_ = ir.PAUTO
|
|
fn.Dcl = append(fn.Dcl, v)
|
|
body = append(body, ir.NewAssignStmt(base.Pos, v, cr))
|
|
} else {
|
|
// Declare variable holding addresses taken from closure
|
|
// and initialize in entry prologue.
|
|
addr := NewName(lookup("&" + v.Sym().Name))
|
|
addr.SetType(types.NewPtr(v.Type()))
|
|
addr.Class_ = ir.PAUTO
|
|
addr.SetUsed(true)
|
|
addr.Curfn = fn
|
|
fn.Dcl = append(fn.Dcl, addr)
|
|
v.Heapaddr = addr
|
|
var src ir.Node = cr
|
|
if v.Byval() {
|
|
src = nodAddr(cr)
|
|
}
|
|
body = append(body, ir.NewAssignStmt(base.Pos, addr, src))
|
|
}
|
|
}
|
|
|
|
if len(body) > 0 {
|
|
typecheckslice(body, ctxStmt)
|
|
fn.Enter.Set(body)
|
|
fn.SetNeedctxt(true)
|
|
}
|
|
}
|
|
|
|
base.Pos = lno
|
|
}
|
|
|
|
// hasemptycvars reports whether closure clo has an
|
|
// empty list of captured vars.
|
|
func hasemptycvars(clo *ir.ClosureExpr) bool {
|
|
return len(clo.Func.ClosureVars) == 0
|
|
}
|
|
|
|
// closuredebugruntimecheck applies boilerplate checks for debug flags
|
|
// and compiling runtime
|
|
func closuredebugruntimecheck(clo *ir.ClosureExpr) {
|
|
if base.Debug.Closure > 0 {
|
|
if clo.Esc() == EscHeap {
|
|
base.WarnfAt(clo.Pos(), "heap closure, captured vars = %v", clo.Func.ClosureVars)
|
|
} else {
|
|
base.WarnfAt(clo.Pos(), "stack closure, captured vars = %v", clo.Func.ClosureVars)
|
|
}
|
|
}
|
|
if base.Flag.CompilingRuntime && clo.Esc() == EscHeap {
|
|
base.ErrorfAt(clo.Pos(), "heap-allocated closure, not allowed in runtime")
|
|
}
|
|
}
|
|
|
|
// closureType returns the struct type used to hold all the information
|
|
// needed in the closure for clo (clo must be a OCLOSURE node).
|
|
// The address of a variable of the returned type can be cast to a func.
|
|
func closureType(clo *ir.ClosureExpr) *types.Type {
|
|
// Create closure in the form of a composite literal.
|
|
// supposing the closure captures an int i and a string s
|
|
// and has one float64 argument and no results,
|
|
// the generated code looks like:
|
|
//
|
|
// clos = &struct{.F uintptr; i *int; s *string}{func.1, &i, &s}
|
|
//
|
|
// The use of the struct provides type information to the garbage
|
|
// collector so that it can walk the closure. We could use (in this case)
|
|
// [3]unsafe.Pointer instead, but that would leave the gc in the dark.
|
|
// The information appears in the binary in the form of type descriptors;
|
|
// the struct is unnamed so that closures in multiple packages with the
|
|
// same struct type can share the descriptor.
|
|
fields := []*ir.Field{
|
|
namedfield(".F", types.Types[types.TUINTPTR]),
|
|
}
|
|
for _, v := range clo.Func.ClosureVars {
|
|
typ := v.Type()
|
|
if !v.Byval() {
|
|
typ = types.NewPtr(typ)
|
|
}
|
|
fields = append(fields, symfield(v.Sym(), typ))
|
|
}
|
|
typ := tostruct(fields)
|
|
typ.SetNoalg(true)
|
|
return typ
|
|
}
|
|
|
|
func walkclosure(clo *ir.ClosureExpr, init *ir.Nodes) ir.Node {
|
|
fn := clo.Func
|
|
|
|
// If no closure vars, don't bother wrapping.
|
|
if hasemptycvars(clo) {
|
|
if base.Debug.Closure > 0 {
|
|
base.WarnfAt(clo.Pos(), "closure converted to global")
|
|
}
|
|
return fn.Nname
|
|
}
|
|
closuredebugruntimecheck(clo)
|
|
|
|
typ := closureType(clo)
|
|
|
|
clos := ir.NewCompLitExpr(base.Pos, ir.OCOMPLIT, ir.TypeNode(typ).(ir.Ntype), nil)
|
|
clos.SetEsc(clo.Esc())
|
|
clos.List.Set(append([]ir.Node{ir.NewUnaryExpr(base.Pos, ir.OCFUNC, fn.Nname)}, fn.ClosureEnter.Slice()...))
|
|
|
|
addr := nodAddr(clos)
|
|
addr.SetEsc(clo.Esc())
|
|
|
|
// Force type conversion from *struct to the func type.
|
|
cfn := convnop(addr, clo.Type())
|
|
|
|
// non-escaping temp to use, if any.
|
|
if x := clo.Prealloc; x != nil {
|
|
if !types.Identical(typ, x.Type()) {
|
|
panic("closure type does not match order's assigned type")
|
|
}
|
|
addr.Alloc = x
|
|
clo.Prealloc = nil
|
|
}
|
|
|
|
return walkexpr(cfn, init)
|
|
}
|
|
|
|
func typecheckpartialcall(n ir.Node, sym *types.Sym) *ir.CallPartExpr {
|
|
switch n.Op() {
|
|
case ir.ODOTINTER, ir.ODOTMETH:
|
|
break
|
|
|
|
default:
|
|
base.Fatalf("invalid typecheckpartialcall")
|
|
}
|
|
dot := n.(*ir.SelectorExpr)
|
|
|
|
// Create top-level function.
|
|
fn := makepartialcall(dot, dot.Type(), sym)
|
|
fn.SetWrapper(true)
|
|
|
|
return ir.NewCallPartExpr(dot.Pos(), dot.X, dot.Selection, fn)
|
|
}
|
|
|
|
// makepartialcall returns a DCLFUNC node representing the wrapper function (*-fm) needed
|
|
// for partial calls.
|
|
func makepartialcall(dot *ir.SelectorExpr, t0 *types.Type, meth *types.Sym) *ir.Func {
|
|
rcvrtype := dot.X.Type()
|
|
sym := methodSymSuffix(rcvrtype, meth, "-fm")
|
|
|
|
if sym.Uniq() {
|
|
return sym.Def.(*ir.Func)
|
|
}
|
|
sym.SetUniq(true)
|
|
|
|
savecurfn := Curfn
|
|
saveLineNo := base.Pos
|
|
Curfn = nil
|
|
|
|
// Set line number equal to the line number where the method is declared.
|
|
var m *types.Field
|
|
if lookdot0(meth, rcvrtype, &m, false) == 1 && m.Pos.IsKnown() {
|
|
base.Pos = m.Pos
|
|
}
|
|
// Note: !m.Pos.IsKnown() happens for method expressions where
|
|
// the method is implicitly declared. The Error method of the
|
|
// built-in error type is one such method. We leave the line
|
|
// number at the use of the method expression in this
|
|
// case. See issue 29389.
|
|
|
|
tfn := ir.NewFuncType(base.Pos, nil,
|
|
structargs(t0.Params(), true),
|
|
structargs(t0.Results(), false))
|
|
|
|
fn := dclfunc(sym, tfn)
|
|
fn.SetDupok(true)
|
|
fn.SetNeedctxt(true)
|
|
|
|
// Declare and initialize variable holding receiver.
|
|
cr := ir.NewClosureRead(rcvrtype, Rnd(int64(Widthptr), int64(rcvrtype.Align)))
|
|
ptr := NewName(lookup(".this"))
|
|
declare(ptr, ir.PAUTO)
|
|
ptr.SetUsed(true)
|
|
var body []ir.Node
|
|
if rcvrtype.IsPtr() || rcvrtype.IsInterface() {
|
|
ptr.SetType(rcvrtype)
|
|
body = append(body, ir.NewAssignStmt(base.Pos, ptr, cr))
|
|
} else {
|
|
ptr.SetType(types.NewPtr(rcvrtype))
|
|
body = append(body, ir.NewAssignStmt(base.Pos, ptr, nodAddr(cr)))
|
|
}
|
|
|
|
call := ir.NewCallExpr(base.Pos, ir.OCALL, ir.NewSelectorExpr(base.Pos, ir.OXDOT, ptr, meth), nil)
|
|
call.Args.Set(paramNnames(tfn.Type()))
|
|
call.IsDDD = tfn.Type().IsVariadic()
|
|
if t0.NumResults() != 0 {
|
|
ret := ir.NewReturnStmt(base.Pos, nil)
|
|
ret.Results.Set1(call)
|
|
body = append(body, ret)
|
|
} else {
|
|
body = append(body, call)
|
|
}
|
|
|
|
fn.Body.Set(body)
|
|
funcbody()
|
|
|
|
typecheckFunc(fn)
|
|
// Need to typecheck the body of the just-generated wrapper.
|
|
// typecheckslice() requires that Curfn is set when processing an ORETURN.
|
|
Curfn = fn
|
|
typecheckslice(fn.Body.Slice(), ctxStmt)
|
|
sym.Def = fn
|
|
Target.Decls = append(Target.Decls, fn)
|
|
Curfn = savecurfn
|
|
base.Pos = saveLineNo
|
|
|
|
return fn
|
|
}
|
|
|
|
// partialCallType returns the struct type used to hold all the information
|
|
// needed in the closure for n (n must be a OCALLPART node).
|
|
// The address of a variable of the returned type can be cast to a func.
|
|
func partialCallType(n *ir.CallPartExpr) *types.Type {
|
|
t := tostruct([]*ir.Field{
|
|
namedfield("F", types.Types[types.TUINTPTR]),
|
|
namedfield("R", n.X.Type()),
|
|
})
|
|
t.SetNoalg(true)
|
|
return t
|
|
}
|
|
|
|
func walkpartialcall(n *ir.CallPartExpr, init *ir.Nodes) ir.Node {
|
|
// Create closure in the form of a composite literal.
|
|
// For x.M with receiver (x) type T, the generated code looks like:
|
|
//
|
|
// clos = &struct{F uintptr; R T}{T.M·f, x}
|
|
//
|
|
// Like walkclosure above.
|
|
|
|
if n.X.Type().IsInterface() {
|
|
// Trigger panic for method on nil interface now.
|
|
// Otherwise it happens in the wrapper and is confusing.
|
|
n.X = cheapexpr(n.X, init)
|
|
n.X = walkexpr(n.X, nil)
|
|
|
|
tab := typecheck(ir.NewUnaryExpr(base.Pos, ir.OITAB, n.X), ctxExpr)
|
|
|
|
c := ir.NewUnaryExpr(base.Pos, ir.OCHECKNIL, tab)
|
|
c.SetTypecheck(1)
|
|
init.Append(c)
|
|
}
|
|
|
|
typ := partialCallType(n)
|
|
|
|
clos := ir.NewCompLitExpr(base.Pos, ir.OCOMPLIT, ir.TypeNode(typ).(ir.Ntype), nil)
|
|
clos.SetEsc(n.Esc())
|
|
clos.List.Set2(ir.NewUnaryExpr(base.Pos, ir.OCFUNC, n.Func.Nname), n.X)
|
|
|
|
addr := nodAddr(clos)
|
|
addr.SetEsc(n.Esc())
|
|
|
|
// Force type conversion from *struct to the func type.
|
|
cfn := convnop(addr, n.Type())
|
|
|
|
// non-escaping temp to use, if any.
|
|
if x := n.Prealloc; x != nil {
|
|
if !types.Identical(typ, x.Type()) {
|
|
panic("partial call type does not match order's assigned type")
|
|
}
|
|
addr.Alloc = x
|
|
n.Prealloc = nil
|
|
}
|
|
|
|
return walkexpr(cfn, init)
|
|
}
|
|
|
|
// callpartMethod returns the *types.Field representing the method
|
|
// referenced by method value n.
|
|
func callpartMethod(n ir.Node) *types.Field {
|
|
return n.(*ir.CallPartExpr).Method
|
|
}
|