Commit eaf02e1f authored by Robert Griesemer's avatar Robert Griesemer

cmd/compile/internal/types: remove unused lineno arguments for PushDcl/MarkDcl

More steps towards simpler symbol handling:

- Pushdcl's incoming pos argument, saved in a newly pushed *Sym, was always
  immediately overwritten by the Lastlineno value of the saved *Sym.

- Markdcl's incoming pos argument, saved in the stack mark *Sym, was not
  restored when the stack mark was popped.

- Popdcl always maintained the most recent Lastlineno for a *Sym given
  by package and name, making it unnecessary to save Lastlineno in the
  first place. Removed Lastlineno from the set of fields that need saving,
  and simplified Popdcl.

Change-Id: Ie93da1fbd780dcafc2703044e781c0c6298df569
Reviewed-on: https://go-review.googlesource.com/41390
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: 's avatarMatthew Dempsky <mdempsky@google.com>
parent 227fff2e
...@@ -191,7 +191,7 @@ func genhash(sym *types.Sym, t *types.Type) { ...@@ -191,7 +191,7 @@ func genhash(sym *types.Sym, t *types.Type) {
lineno = autogeneratedPos // less confusing than end of input lineno = autogeneratedPos // less confusing than end of input
dclcontext = PEXTERN dclcontext = PEXTERN
types.Markdcl(lineno) types.Markdcl()
// func sym(p *T, h uintptr) uintptr // func sym(p *T, h uintptr) uintptr
tfn := nod(OTFUNC, nil, nil) tfn := nod(OTFUNC, nil, nil)
...@@ -362,7 +362,7 @@ func geneq(sym *types.Sym, t *types.Type) { ...@@ -362,7 +362,7 @@ func geneq(sym *types.Sym, t *types.Type) {
lineno = autogeneratedPos // less confusing than end of input lineno = autogeneratedPos // less confusing than end of input
dclcontext = PEXTERN dclcontext = PEXTERN
types.Markdcl(lineno) types.Markdcl()
// func sym(p, q *T) bool // func sym(p, q *T) bool
tfn := nod(OTFUNC, nil, nil) tfn := nod(OTFUNC, nil, nil)
......
...@@ -1091,7 +1091,7 @@ func (p *importer) node() *Node { ...@@ -1091,7 +1091,7 @@ func (p *importer) node() *Node {
return nodl(p.pos(), op, p.expr(), nil) return nodl(p.pos(), op, p.expr(), nil)
case OIF: case OIF:
types.Markdcl(lineno) types.Markdcl()
n := nodl(p.pos(), OIF, nil, nil) n := nodl(p.pos(), OIF, nil, nil)
n.Ninit.Set(p.stmtList()) n.Ninit.Set(p.stmtList())
n.Left = p.expr() n.Left = p.expr()
...@@ -1101,7 +1101,7 @@ func (p *importer) node() *Node { ...@@ -1101,7 +1101,7 @@ func (p *importer) node() *Node {
return n return n
case OFOR: case OFOR:
types.Markdcl(lineno) types.Markdcl()
n := nodl(p.pos(), OFOR, nil, nil) n := nodl(p.pos(), OFOR, nil, nil)
n.Ninit.Set(p.stmtList()) n.Ninit.Set(p.stmtList())
n.Left, n.Right = p.exprsOrNil() n.Left, n.Right = p.exprsOrNil()
...@@ -1110,7 +1110,7 @@ func (p *importer) node() *Node { ...@@ -1110,7 +1110,7 @@ func (p *importer) node() *Node {
return n return n
case ORANGE: case ORANGE:
types.Markdcl(lineno) types.Markdcl()
n := nodl(p.pos(), ORANGE, nil, nil) n := nodl(p.pos(), ORANGE, nil, nil)
n.List.Set(p.stmtList()) n.List.Set(p.stmtList())
n.Right = p.expr() n.Right = p.expr()
...@@ -1119,7 +1119,7 @@ func (p *importer) node() *Node { ...@@ -1119,7 +1119,7 @@ func (p *importer) node() *Node {
return n return n
case OSELECT, OSWITCH: case OSELECT, OSWITCH:
types.Markdcl(lineno) types.Markdcl()
n := nodl(p.pos(), op, nil, nil) n := nodl(p.pos(), op, nil, nil)
n.Ninit.Set(p.stmtList()) n.Ninit.Set(p.stmtList())
n.Left, _ = p.exprsOrNil() n.Left, _ = p.exprsOrNil()
...@@ -1131,7 +1131,7 @@ func (p *importer) node() *Node { ...@@ -1131,7 +1131,7 @@ func (p *importer) node() *Node {
// unreachable - mapped to OXCASE case below by exporter // unreachable - mapped to OXCASE case below by exporter
case OXCASE: case OXCASE:
types.Markdcl(lineno) types.Markdcl()
n := nodl(p.pos(), OXCASE, nil, nil) n := nodl(p.pos(), OXCASE, nil, nil)
n.Xoffset = int64(types.Block) n.Xoffset = int64(types.Block)
n.List.Set(p.exprList()) n.List.Set(p.exprList())
......
...@@ -104,7 +104,7 @@ func declare(n *Node, ctxt Class) { ...@@ -104,7 +104,7 @@ func declare(n *Node, ctxt Class) {
vargen++ vargen++
gen = vargen gen = vargen
} }
types.Pushdcl(s, lineno) types.Pushdcl(s)
n.Name.Curfn = Curfn n.Name.Curfn = Curfn
} }
...@@ -510,7 +510,7 @@ var funcdepth int32 // len(funcstack) during parsing, but then forced to be th ...@@ -510,7 +510,7 @@ var funcdepth int32 // len(funcstack) during parsing, but then forced to be th
// start the function. // start the function.
// called before funcargs; undone at end of funcbody. // called before funcargs; undone at end of funcbody.
func funcstart(n *Node) { func funcstart(n *Node) {
types.Markdcl(lineno) types.Markdcl()
funcstack = append(funcstack, Curfn) funcstack = append(funcstack, Curfn)
funcdepth++ funcdepth++
Curfn = n Curfn = n
......
...@@ -774,14 +774,14 @@ func (p *noder) stmt(stmt syntax.Stmt) *Node { ...@@ -774,14 +774,14 @@ func (p *noder) stmt(stmt syntax.Stmt) *Node {
} }
func (p *noder) blockStmt(stmt *syntax.BlockStmt) []*Node { func (p *noder) blockStmt(stmt *syntax.BlockStmt) []*Node {
types.Markdcl(lineno) types.Markdcl()
nodes := p.stmts(stmt.List) nodes := p.stmts(stmt.List)
types.Popdcl() types.Popdcl()
return nodes return nodes
} }
func (p *noder) ifStmt(stmt *syntax.IfStmt) *Node { func (p *noder) ifStmt(stmt *syntax.IfStmt) *Node {
types.Markdcl(lineno) types.Markdcl()
n := p.nod(stmt, OIF, nil, nil) n := p.nod(stmt, OIF, nil, nil)
if stmt.Init != nil { if stmt.Init != nil {
n.Ninit.Set1(p.stmt(stmt.Init)) n.Ninit.Set1(p.stmt(stmt.Init))
...@@ -803,7 +803,7 @@ func (p *noder) ifStmt(stmt *syntax.IfStmt) *Node { ...@@ -803,7 +803,7 @@ func (p *noder) ifStmt(stmt *syntax.IfStmt) *Node {
} }
func (p *noder) forStmt(stmt *syntax.ForStmt) *Node { func (p *noder) forStmt(stmt *syntax.ForStmt) *Node {
types.Markdcl(lineno) types.Markdcl()
var n *Node var n *Node
if r, ok := stmt.Init.(*syntax.RangeClause); ok { if r, ok := stmt.Init.(*syntax.RangeClause); ok {
if stmt.Cond != nil || stmt.Post != nil { if stmt.Cond != nil || stmt.Post != nil {
...@@ -837,7 +837,7 @@ func (p *noder) forStmt(stmt *syntax.ForStmt) *Node { ...@@ -837,7 +837,7 @@ func (p *noder) forStmt(stmt *syntax.ForStmt) *Node {
} }
func (p *noder) switchStmt(stmt *syntax.SwitchStmt) *Node { func (p *noder) switchStmt(stmt *syntax.SwitchStmt) *Node {
types.Markdcl(lineno) types.Markdcl()
n := p.nod(stmt, OSWITCH, nil, nil) n := p.nod(stmt, OSWITCH, nil, nil)
if stmt.Init != nil { if stmt.Init != nil {
n.Ninit.Set1(p.stmt(stmt.Init)) n.Ninit.Set1(p.stmt(stmt.Init))
...@@ -861,7 +861,7 @@ func (p *noder) caseClauses(clauses []*syntax.CaseClause, tswitch *Node) []*Node ...@@ -861,7 +861,7 @@ func (p *noder) caseClauses(clauses []*syntax.CaseClause, tswitch *Node) []*Node
var nodes []*Node var nodes []*Node
for _, clause := range clauses { for _, clause := range clauses {
p.lineno(clause) p.lineno(clause)
types.Markdcl(lineno) types.Markdcl()
n := p.nod(clause, OXCASE, nil, nil) n := p.nod(clause, OXCASE, nil, nil)
if clause.Cases != nil { if clause.Cases != nil {
n.List.Set(p.exprList(clause.Cases)) n.List.Set(p.exprList(clause.Cases))
...@@ -891,7 +891,7 @@ func (p *noder) commClauses(clauses []*syntax.CommClause) []*Node { ...@@ -891,7 +891,7 @@ func (p *noder) commClauses(clauses []*syntax.CommClause) []*Node {
var nodes []*Node var nodes []*Node
for _, clause := range clauses { for _, clause := range clauses {
p.lineno(clause) p.lineno(clause)
types.Markdcl(lineno) types.Markdcl()
n := p.nod(clause, OXCASE, nil, nil) n := p.nod(clause, OXCASE, nil, nil)
if clause.Comm != nil { if clause.Comm != nil {
n.List.Set1(p.stmt(clause.Comm)) n.List.Set1(p.stmt(clause.Comm))
......
...@@ -1684,7 +1684,7 @@ func genwrapper(rcvr *types.Type, method *types.Field, newnam *types.Sym, iface ...@@ -1684,7 +1684,7 @@ func genwrapper(rcvr *types.Type, method *types.Field, newnam *types.Sym, iface
lineno = autogeneratedPos lineno = autogeneratedPos
dclcontext = PEXTERN dclcontext = PEXTERN
types.Markdcl(lineno) types.Markdcl()
this := namedfield(".this", rcvr) this := namedfield(".this", rcvr)
this.Left.Name.Param.Ntype = this.Right this.Left.Name.Param.Ntype = this.Right
......
...@@ -4,15 +4,13 @@ ...@@ -4,15 +4,13 @@
package types package types
import "cmd/internal/src"
// Declaration stack & operations // Declaration stack & operations
var blockgen int32 = 1 // max block number var blockgen int32 = 1 // max block number
var Block int32 // current block number var Block int32 // current block number
// dclstack maintains a stack of shadowed symbol declarations so that // dclstack maintains a stack of shadowed symbol declarations so that
// popdcl can restore their declarations when a block scope ends. // Popdcl can restore their declarations when a block scope ends.
// //
// The Syms on this stack are not "real" Syms as they don't actually // The Syms on this stack are not "real" Syms as they don't actually
// represent object names. Sym is just a convenient type for saving shadowed // represent object names. Sym is just a convenient type for saving shadowed
...@@ -24,52 +22,39 @@ func dcopy(a, b *Sym) { ...@@ -24,52 +22,39 @@ func dcopy(a, b *Sym) {
a.Name = b.Name a.Name = b.Name
a.Def = b.Def a.Def = b.Def
a.Block = b.Block a.Block = b.Block
a.Lastlineno = b.Lastlineno
} }
func push(pos src.XPos) *Sym { func push() *Sym {
d := new(Sym) d := new(Sym)
d.Lastlineno = pos
dclstack = append(dclstack, d) dclstack = append(dclstack, d)
return d return d
} }
// Pushdcl pushes the current declaration for symbol s (if any) so that // Pushdcl pushes the current declaration for symbol s (if any) so that
// it can be shadowed by a new declaration within a nested block scope. // it can be shadowed by a new declaration within a nested block scope.
func Pushdcl(s *Sym, pos src.XPos) { func Pushdcl(s *Sym) {
d := push(pos) dcopy(push(), s)
dcopy(d, s)
} }
// Popdcl pops the innermost block scope and restores all symbol declarations // Popdcl pops the innermost block scope and restores all symbol declarations
// to their previous state. // to their previous state.
func Popdcl() { func Popdcl() {
i := len(dclstack) for i := len(dclstack); i > 0; i-- {
for ; i > 0; i-- {
d := dclstack[i-1] d := dclstack[i-1]
if d.Name == "" { if d.Name == "" {
break // pop stack mark
} Block = d.Block
s := d.Pkg.Lookup(d.Name) dclstack = dclstack[:i-1]
lno := s.Lastlineno return
dcopy(s, d)
d.Lastlineno = lno
} }
dcopy(d.Pkg.Lookup(d.Name), d)
if i == 0 {
Fatalf("popdcl: no mark")
} }
Fatalf("popdcl: no stack mark")
Block = dclstack[i-1].Block
dclstack = dclstack[:i-1] // pop mark
} }
// Markdcl records the start of a new block scope for declarations. // Markdcl records the start of a new block scope for declarations.
func Markdcl(lineno src.XPos) { func Markdcl() {
d := push(lineno) push().Block = Block // stack mark (Name == "")
d.Name = "" // used as stack mark
d.Block = Block
blockgen++ blockgen++
Block = blockgen Block = blockgen
} }
......
...@@ -20,12 +20,12 @@ import ( ...@@ -20,12 +20,12 @@ import (
type Sym struct { type Sym struct {
Importdef *Pkg // where imported definition was found Importdef *Pkg // where imported definition was found
Linkname string // link name Linkname string // link name
Lastlineno src.XPos // last declaration for diagnostic
// saved and restored by dcopy // saved and restored by dcopy
Pkg *Pkg Pkg *Pkg
Name string // object name Name string // object name
Def *Node // definition: ONAME OTYPE OPACK or OLITERAL Def *Node // definition: ONAME OTYPE OPACK or OLITERAL
Lastlineno src.XPos // last declaration for diagnostic
Block int32 // blocknumber to catch redeclaration Block int32 // blocknumber to catch redeclaration
flags bitset8 flags bitset8
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment