Skip to content

Commit d7eb490

Browse files
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent aa00ca1 commit d7eb490

File tree

8 files changed

+14
-43
lines changed

8 files changed

+14
-43
lines changed

Diff for: src/cmd/compile/internal/gc/bimport.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ func Import(imp *types.Pkg, in *bufio.Reader) {
178178
}
179179
i0 = i
180180

181-
if funcdepth != 0 {
182-
p.formatErrorf("unexpected Funcdepth %d", funcdepth)
181+
if Curfn != nil {
182+
p.formatErrorf("unexpected Curfn %v", Curfn)
183183
}
184184

185185
// Note: In the original code, funchdr and funcbody are called for

Diff for: src/cmd/compile/internal/gc/closure.go

-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ func (p *noder) funcLit(expr *syntax.FuncLit) *Node {
1616
n := p.nod(expr, OCLOSURE, nil, nil)
1717
n.Func.SetIsHiddenClosure(Curfn != nil)
1818
n.Func.Ntype = ntype
19-
n.Func.Depth = funcdepth
2019
n.Func.Outerfunc = Curfn
2120

2221
old := p.funchdr(n)
@@ -220,8 +219,6 @@ func makeclosure(func_ *Node) *Node {
220219
xfunc.Func.Nname.Name.Param.Ntype = xtype
221220
xfunc.Func.Nname.Name.Defn = xfunc
222221
declare(xfunc.Func.Nname, PFUNC)
223-
xfunc.Func.Nname.Name.Funcdepth = func_.Func.Depth
224-
xfunc.Func.Depth = func_.Func.Depth
225222
xfunc.Func.Endlineno = func_.Func.Endlineno
226223
if Ctxt.Flag_dynlink {
227224
makefuncsym(xfunc.Func.Nname.Sym)

Diff for: src/cmd/compile/internal/gc/dcl.go

+10-21
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ func declare(n *Node, ctxt Class) {
128128
s.Lastlineno = lineno
129129
s.Def = asTypesNode(n)
130130
n.Name.Vargen = int32(gen)
131-
n.Name.Funcdepth = funcdepth
132131
n.SetClass(ctxt)
133132

134133
autoexport(n, ctxt)
@@ -160,7 +159,7 @@ func variter(vl []*Node, t *Node, el []*Node) []*Node {
160159
declare(v, dclcontext)
161160
v.Name.Param.Ntype = t
162161
v.Name.Defn = as2
163-
if funcdepth > 0 {
162+
if Curfn != nil {
164163
init = append(init, nod(ODCL, v, nil))
165164
}
166165
}
@@ -183,8 +182,8 @@ func variter(vl []*Node, t *Node, el []*Node) []*Node {
183182
declare(v, dclcontext)
184183
v.Name.Param.Ntype = t
185184

186-
if e != nil || funcdepth > 0 || isblank(v) {
187-
if funcdepth > 0 {
185+
if e != nil || Curfn != nil || isblank(v) {
186+
if Curfn != nil {
188187
init = append(init, nod(ODCL, v, nil))
189188
}
190189
e = nod(OAS, v, e)
@@ -276,23 +275,22 @@ func oldname(s *types.Sym) *Node {
276275
return newnoname(s)
277276
}
278277

279-
if Curfn != nil && n.Op == ONAME && n.Name.Funcdepth > 0 && n.Name.Funcdepth != funcdepth {
278+
if Curfn != nil && n.Op == ONAME && n.Name.Curfn != nil && n.Name.Curfn != Curfn {
280279
// Inner func is referring to var in outer func.
281280
//
282281
// TODO(rsc): If there is an outer variable x and we
283282
// are parsing x := 5 inside the closure, until we get to
284283
// the := it looks like a reference to the outer x so we'll
285284
// make x a closure variable unnecessarily.
286285
c := n.Name.Param.Innermost
287-
if c == nil || c.Name.Funcdepth != funcdepth {
286+
if c == nil || c.Name.Curfn != Curfn {
288287
// Do not have a closure var for the active closure yet; make one.
289288
c = newname(s)
290289
c.SetClass(PAUTOHEAP)
291290
c.SetIsClosureVar(true)
292291
c.SetIsddd(n.Isddd())
293292
c.Name.Defn = n
294293
c.SetAddable(false)
295-
c.Name.Funcdepth = funcdepth
296294

297295
// Link into list of active closure variables.
298296
// Popped from list in func closurebody.
@@ -384,12 +382,14 @@ func ifacedcl(n *Node) {
384382
// returns in auto-declaration context.
385383
func funchdr(n *Node) {
386384
// change the declaration context from extern to auto
387-
if funcdepth == 0 && dclcontext != PEXTERN {
385+
if Curfn == nil && dclcontext != PEXTERN {
388386
Fatalf("funchdr: dclcontext = %d", dclcontext)
389387
}
390388

391389
dclcontext = PAUTO
392-
funcstart(n)
390+
types.Markdcl()
391+
funcstack = append(funcstack, Curfn)
392+
Curfn = n
393393

394394
if n.Func.Nname != nil {
395395
funcargs(n.Func.Nname.Name.Param.Ntype)
@@ -523,16 +523,6 @@ func funcargs2(t *types.Type) {
523523
}
524524

525525
var funcstack []*Node // stack of previous values of Curfn
526-
var funcdepth int32 // len(funcstack) during parsing, but then forced to be the same later during compilation
527-
528-
// start the function.
529-
// called before funcargs; undone at end of funcbody.
530-
func funcstart(n *Node) {
531-
types.Markdcl()
532-
funcstack = append(funcstack, Curfn)
533-
funcdepth++
534-
Curfn = n
535-
}
536526

537527
// finish the body.
538528
// called in auto-declaration context.
@@ -544,8 +534,7 @@ func funcbody() {
544534
}
545535
types.Popdcl()
546536
funcstack, Curfn = funcstack[:len(funcstack)-1], funcstack[len(funcstack)-1]
547-
funcdepth--
548-
if funcdepth == 0 {
537+
if Curfn == nil {
549538
dclcontext = PEXTERN
550539
}
551540
}

Diff for: src/cmd/compile/internal/gc/fmt.go

-7
Original file line numberDiff line numberDiff line change
@@ -436,13 +436,6 @@ func (n *Node) jconv(s fmt.State, flag FmtFlag) {
436436
fmt.Fprintf(s, " colas(%v)", n.Colas())
437437
}
438438

439-
if n.Name != nil && n.Name.Funcdepth != 0 {
440-
fmt.Fprintf(s, " f(%d)", n.Name.Funcdepth)
441-
}
442-
if n.Func != nil && n.Func.Depth != 0 {
443-
fmt.Fprintf(s, " ff(%d)", n.Func.Depth)
444-
}
445-
446439
switch n.Esc {
447440
case EscUnknown:
448441
break

Diff for: src/cmd/compile/internal/gc/pgen.go

-2
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,11 @@ func funccompile(fn *Node) {
204204
}
205205

206206
dclcontext = PAUTO
207-
funcdepth = fn.Func.Depth + 1
208207
Curfn = fn
209208

210209
compile(fn)
211210

212211
Curfn = nil
213-
funcdepth = 0
214212
dclcontext = PEXTERN
215213
}
216214

Diff for: src/cmd/compile/internal/gc/sizeof_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ func TestSizeof(t *testing.T) {
2222
_32bit uintptr // size on 32bit platforms
2323
_64bit uintptr // size on 64bit platforms
2424
}{
25-
{Func{}, 132, 232},
26-
{Name{}, 36, 56},
25+
{Func{}, 128, 232},
26+
{Name{}, 32, 56},
2727
{Param{}, 28, 56},
2828
{Node{}, 76, 128},
2929
}

Diff for: src/cmd/compile/internal/gc/syntax.go

-2
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ type Name struct {
242242
Param *Param // additional fields for ONAME, OTYPE
243243
Decldepth int32 // declaration loop depth, increased for every loop or label
244244
Vargen int32 // unique name for ONAME within a function. Function outputs are numbered starting at one.
245-
Funcdepth int32
246245

247246
used bool // for variable declared and not used error
248247
flags bitset8
@@ -433,7 +432,6 @@ type Func struct {
433432

434433
Inl Nodes // copy of the body for use in inlining
435434
InlCost int32
436-
Depth int32
437435

438436
Label int32 // largest auto-generated label in this function
439437

Diff for: src/cmd/compile/internal/gc/walk.go

-4
Original file line numberDiff line numberDiff line change
@@ -3868,9 +3868,6 @@ func wrapCall(n *Node, init *Nodes) *Node {
38683868
args = append(args, a.Left)
38693869
}
38703870

3871-
oldfn := Curfn
3872-
Curfn = nil
3873-
38743871
wrapCall_prgen++
38753872
sym := lookupN("wrap·", wrapCall_prgen)
38763873
fn := dclfunc(sym, t)
@@ -3885,7 +3882,6 @@ func wrapCall(n *Node, init *Nodes) *Node {
38853882
fn = typecheck(fn, Etop)
38863883
typecheckslice(fn.Nbody.Slice(), Etop)
38873884
xtop = append(xtop, fn)
3888-
Curfn = oldfn
38893885

38903886
a = nod(OCALL, nil, nil)
38913887
a.Left = fn.Func.Nname

0 commit comments

Comments
 (0)