mirror of
https://github.com/golang/go
synced 2024-11-17 07:45:09 -07:00
cmd/compile: remove funcdepth variables
There were only two large classes of use for these variables: 1) Testing "funcdepth != 0" or "funcdepth > 0", which is equivalent to checking "Curfn != nil". 2) In oldname, detecting whether a closure variable has been created for the current function, which can be handled by instead testing "n.Name.Curfn != Curfn". Lastly, merge funcstart into funchdr, since it's only called once, and it better matches up with funcbody now. Passes toolstash-check. Change-Id: I8fe159a9d37ef7debc4cd310354cea22a8b23394 Reviewed-on: https://go-review.googlesource.com/99076 Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
aa00ca12fe
commit
d7eb4901f1
@ -178,8 +178,8 @@ func Import(imp *types.Pkg, in *bufio.Reader) {
|
||||
}
|
||||
i0 = i
|
||||
|
||||
if funcdepth != 0 {
|
||||
p.formatErrorf("unexpected Funcdepth %d", funcdepth)
|
||||
if Curfn != nil {
|
||||
p.formatErrorf("unexpected Curfn %v", Curfn)
|
||||
}
|
||||
|
||||
// Note: In the original code, funchdr and funcbody are called for
|
||||
|
@ -16,7 +16,6 @@ func (p *noder) funcLit(expr *syntax.FuncLit) *Node {
|
||||
n := p.nod(expr, OCLOSURE, nil, nil)
|
||||
n.Func.SetIsHiddenClosure(Curfn != nil)
|
||||
n.Func.Ntype = ntype
|
||||
n.Func.Depth = funcdepth
|
||||
n.Func.Outerfunc = Curfn
|
||||
|
||||
old := p.funchdr(n)
|
||||
@ -220,8 +219,6 @@ func makeclosure(func_ *Node) *Node {
|
||||
xfunc.Func.Nname.Name.Param.Ntype = xtype
|
||||
xfunc.Func.Nname.Name.Defn = xfunc
|
||||
declare(xfunc.Func.Nname, PFUNC)
|
||||
xfunc.Func.Nname.Name.Funcdepth = func_.Func.Depth
|
||||
xfunc.Func.Depth = func_.Func.Depth
|
||||
xfunc.Func.Endlineno = func_.Func.Endlineno
|
||||
if Ctxt.Flag_dynlink {
|
||||
makefuncsym(xfunc.Func.Nname.Sym)
|
||||
|
@ -128,7 +128,6 @@ func declare(n *Node, ctxt Class) {
|
||||
s.Lastlineno = lineno
|
||||
s.Def = asTypesNode(n)
|
||||
n.Name.Vargen = int32(gen)
|
||||
n.Name.Funcdepth = funcdepth
|
||||
n.SetClass(ctxt)
|
||||
|
||||
autoexport(n, ctxt)
|
||||
@ -160,7 +159,7 @@ func variter(vl []*Node, t *Node, el []*Node) []*Node {
|
||||
declare(v, dclcontext)
|
||||
v.Name.Param.Ntype = t
|
||||
v.Name.Defn = as2
|
||||
if funcdepth > 0 {
|
||||
if Curfn != nil {
|
||||
init = append(init, nod(ODCL, v, nil))
|
||||
}
|
||||
}
|
||||
@ -183,8 +182,8 @@ func variter(vl []*Node, t *Node, el []*Node) []*Node {
|
||||
declare(v, dclcontext)
|
||||
v.Name.Param.Ntype = t
|
||||
|
||||
if e != nil || funcdepth > 0 || isblank(v) {
|
||||
if funcdepth > 0 {
|
||||
if e != nil || Curfn != nil || isblank(v) {
|
||||
if Curfn != nil {
|
||||
init = append(init, nod(ODCL, v, nil))
|
||||
}
|
||||
e = nod(OAS, v, e)
|
||||
@ -276,7 +275,7 @@ func oldname(s *types.Sym) *Node {
|
||||
return newnoname(s)
|
||||
}
|
||||
|
||||
if Curfn != nil && n.Op == ONAME && n.Name.Funcdepth > 0 && n.Name.Funcdepth != funcdepth {
|
||||
if Curfn != nil && n.Op == ONAME && n.Name.Curfn != nil && n.Name.Curfn != Curfn {
|
||||
// Inner func is referring to var in outer func.
|
||||
//
|
||||
// TODO(rsc): If there is an outer variable x and we
|
||||
@ -284,7 +283,7 @@ func oldname(s *types.Sym) *Node {
|
||||
// the := it looks like a reference to the outer x so we'll
|
||||
// make x a closure variable unnecessarily.
|
||||
c := n.Name.Param.Innermost
|
||||
if c == nil || c.Name.Funcdepth != funcdepth {
|
||||
if c == nil || c.Name.Curfn != Curfn {
|
||||
// Do not have a closure var for the active closure yet; make one.
|
||||
c = newname(s)
|
||||
c.SetClass(PAUTOHEAP)
|
||||
@ -292,7 +291,6 @@ func oldname(s *types.Sym) *Node {
|
||||
c.SetIsddd(n.Isddd())
|
||||
c.Name.Defn = n
|
||||
c.SetAddable(false)
|
||||
c.Name.Funcdepth = funcdepth
|
||||
|
||||
// Link into list of active closure variables.
|
||||
// Popped from list in func closurebody.
|
||||
@ -384,12 +382,14 @@ func ifacedcl(n *Node) {
|
||||
// returns in auto-declaration context.
|
||||
func funchdr(n *Node) {
|
||||
// change the declaration context from extern to auto
|
||||
if funcdepth == 0 && dclcontext != PEXTERN {
|
||||
if Curfn == nil && dclcontext != PEXTERN {
|
||||
Fatalf("funchdr: dclcontext = %d", dclcontext)
|
||||
}
|
||||
|
||||
dclcontext = PAUTO
|
||||
funcstart(n)
|
||||
types.Markdcl()
|
||||
funcstack = append(funcstack, Curfn)
|
||||
Curfn = n
|
||||
|
||||
if n.Func.Nname != nil {
|
||||
funcargs(n.Func.Nname.Name.Param.Ntype)
|
||||
@ -523,16 +523,6 @@ func funcargs2(t *types.Type) {
|
||||
}
|
||||
|
||||
var funcstack []*Node // stack of previous values of Curfn
|
||||
var funcdepth int32 // len(funcstack) during parsing, but then forced to be the same later during compilation
|
||||
|
||||
// start the function.
|
||||
// called before funcargs; undone at end of funcbody.
|
||||
func funcstart(n *Node) {
|
||||
types.Markdcl()
|
||||
funcstack = append(funcstack, Curfn)
|
||||
funcdepth++
|
||||
Curfn = n
|
||||
}
|
||||
|
||||
// finish the body.
|
||||
// called in auto-declaration context.
|
||||
@ -544,8 +534,7 @@ func funcbody() {
|
||||
}
|
||||
types.Popdcl()
|
||||
funcstack, Curfn = funcstack[:len(funcstack)-1], funcstack[len(funcstack)-1]
|
||||
funcdepth--
|
||||
if funcdepth == 0 {
|
||||
if Curfn == nil {
|
||||
dclcontext = PEXTERN
|
||||
}
|
||||
}
|
||||
|
@ -436,13 +436,6 @@ func (n *Node) jconv(s fmt.State, flag FmtFlag) {
|
||||
fmt.Fprintf(s, " colas(%v)", n.Colas())
|
||||
}
|
||||
|
||||
if n.Name != nil && n.Name.Funcdepth != 0 {
|
||||
fmt.Fprintf(s, " f(%d)", n.Name.Funcdepth)
|
||||
}
|
||||
if n.Func != nil && n.Func.Depth != 0 {
|
||||
fmt.Fprintf(s, " ff(%d)", n.Func.Depth)
|
||||
}
|
||||
|
||||
switch n.Esc {
|
||||
case EscUnknown:
|
||||
break
|
||||
|
@ -204,13 +204,11 @@ func funccompile(fn *Node) {
|
||||
}
|
||||
|
||||
dclcontext = PAUTO
|
||||
funcdepth = fn.Func.Depth + 1
|
||||
Curfn = fn
|
||||
|
||||
compile(fn)
|
||||
|
||||
Curfn = nil
|
||||
funcdepth = 0
|
||||
dclcontext = PEXTERN
|
||||
}
|
||||
|
||||
|
@ -22,8 +22,8 @@ func TestSizeof(t *testing.T) {
|
||||
_32bit uintptr // size on 32bit platforms
|
||||
_64bit uintptr // size on 64bit platforms
|
||||
}{
|
||||
{Func{}, 132, 232},
|
||||
{Name{}, 36, 56},
|
||||
{Func{}, 128, 232},
|
||||
{Name{}, 32, 56},
|
||||
{Param{}, 28, 56},
|
||||
{Node{}, 76, 128},
|
||||
}
|
||||
|
@ -242,7 +242,6 @@ type Name struct {
|
||||
Param *Param // additional fields for ONAME, OTYPE
|
||||
Decldepth int32 // declaration loop depth, increased for every loop or label
|
||||
Vargen int32 // unique name for ONAME within a function. Function outputs are numbered starting at one.
|
||||
Funcdepth int32
|
||||
|
||||
used bool // for variable declared and not used error
|
||||
flags bitset8
|
||||
@ -433,7 +432,6 @@ type Func struct {
|
||||
|
||||
Inl Nodes // copy of the body for use in inlining
|
||||
InlCost int32
|
||||
Depth int32
|
||||
|
||||
Label int32 // largest auto-generated label in this function
|
||||
|
||||
|
@ -3868,9 +3868,6 @@ func wrapCall(n *Node, init *Nodes) *Node {
|
||||
args = append(args, a.Left)
|
||||
}
|
||||
|
||||
oldfn := Curfn
|
||||
Curfn = nil
|
||||
|
||||
wrapCall_prgen++
|
||||
sym := lookupN("wrap·", wrapCall_prgen)
|
||||
fn := dclfunc(sym, t)
|
||||
@ -3885,7 +3882,6 @@ func wrapCall(n *Node, init *Nodes) *Node {
|
||||
fn = typecheck(fn, Etop)
|
||||
typecheckslice(fn.Nbody.Slice(), Etop)
|
||||
xtop = append(xtop, fn)
|
||||
Curfn = oldfn
|
||||
|
||||
a = nod(OCALL, nil, nil)
|
||||
a.Left = fn.Func.Nname
|
||||
|
Loading…
Reference in New Issue
Block a user