Skip to content

Commit 29517da

Browse files
committed
cmd/compile: extract common noding code from func{Decl,Lit}
Passes toolstash-check. Change-Id: I8290221d6169e077dfa4ea737d685c7fcecf6841 Reviewed-on: https://go-review.googlesource.com/100835 Run-TryBot: Matthew Dempsky <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 463fe95 commit 29517da

File tree

2 files changed

+17
-30
lines changed

2 files changed

+17
-30
lines changed

src/cmd/compile/internal/gc/closure.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,7 @@ func (p *noder) funcLit(expr *syntax.FuncLit) *Node {
2626
xfunc.Func.Closure = clo
2727
clo.Func.Closure = xfunc
2828

29-
oldScope := p.funchdr(xfunc)
30-
31-
body := p.stmts(expr.Body.List)
32-
if body == nil {
33-
body = []*Node{nod(OEMPTY, nil, nil)}
34-
}
35-
xfunc.Nbody.Set(body)
36-
37-
lineno = p.makeXPos(expr.Body.Rbrace)
38-
xfunc.Func.Endlineno = lineno
39-
40-
p.funcbody(oldScope)
29+
p.funcBody(xfunc, expr.Body)
4130

4231
// closure-specific variables are hanging off the
4332
// ordinary ones in the symbol table; see oldname.

src/cmd/compile/internal/gc/noder.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,24 @@ type noder struct {
136136
lastCloseScopePos syntax.Pos
137137
}
138138

139-
func (p *noder) funchdr(n *Node) ScopeID {
140-
old := p.scope
139+
func (p *noder) funcBody(fn *Node, block *syntax.BlockStmt) {
140+
oldScope := p.scope
141141
p.scope = 0
142-
funchdr(n)
143-
return old
144-
}
142+
funchdr(fn)
143+
144+
if block != nil {
145+
body := p.stmts(block.List)
146+
if body == nil {
147+
body = []*Node{nod(OEMPTY, nil, nil)}
148+
}
149+
fn.Nbody.Set(body)
150+
151+
lineno = p.makeXPos(block.Rbrace)
152+
fn.Func.Endlineno = lineno
153+
}
145154

146-
func (p *noder) funcbody(old ScopeID) {
147155
funcbody()
148-
p.scope = old
156+
p.scope = oldScope
149157
}
150158

151159
func (p *noder) openScope(pos syntax.Pos) {
@@ -459,28 +467,18 @@ func (p *noder) funcDecl(fun *syntax.FuncDecl) *Node {
459467
declare(f.Func.Nname, PFUNC)
460468
}
461469

462-
oldScope := p.funchdr(f)
470+
p.funcBody(f, fun.Body)
463471

464472
if fun.Body != nil {
465473
if f.Noescape() {
466474
yyerrorl(f.Pos, "can only use //go:noescape with external func implementations")
467475
}
468-
469-
body := p.stmts(fun.Body.List)
470-
if body == nil {
471-
body = []*Node{p.nod(fun, OEMPTY, nil, nil)}
472-
}
473-
f.Nbody.Set(body)
474-
475-
lineno = p.makeXPos(fun.Body.Rbrace)
476-
f.Func.Endlineno = lineno
477476
} else {
478477
if pure_go || strings.HasPrefix(f.funcname(), "init.") {
479478
yyerrorl(f.Pos, "missing function body")
480479
}
481480
}
482481

483-
p.funcbody(oldScope)
484482
return f
485483
}
486484

0 commit comments

Comments
 (0)