1
0
mirror of https://github.com/golang/go synced 2024-11-23 09:30:03 -07:00

cmd/compile/internal/gc: more direct noder.nod implementation (cleanup)

Also, renamed

- noder.lineno -> noder.setlineno (because that's what it does)
- noder.setlineno -> noder.pos    (and return the src.XPos)

Change-Id: I5d3442cf2af97028afcab028290152ce8d062927
Reviewed-on: https://go-review.googlesource.com/c/146317
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Robert Griesemer 2018-10-31 11:38:37 -07:00
parent 4d1e95bb63
commit f6f27bfb97
3 changed files with 35 additions and 43 deletions

View File

@ -16,7 +16,7 @@ func (p *noder) funcLit(expr *syntax.FuncLit) *Node {
xfunc := p.nod(expr, ODCLFUNC, nil, nil) xfunc := p.nod(expr, ODCLFUNC, nil, nil)
xfunc.Func.SetIsHiddenClosure(Curfn != nil) xfunc.Func.SetIsHiddenClosure(Curfn != nil)
xfunc.Func.Nname = p.setlineno(expr, newfuncname(nblank.Sym)) // filled in by typecheckclosure xfunc.Func.Nname = newfuncnamel(p.pos(expr), nblank.Sym) // filled in by typecheckclosure
xfunc.Func.Nname.Name.Param.Ntype = xtype xfunc.Func.Nname.Name.Param.Ntype = xtype
xfunc.Func.Nname.Name.Defn = xfunc xfunc.Func.Nname.Name.Defn = xfunc

View File

@ -208,12 +208,6 @@ func newnoname(s *types.Sym) *Node {
return n return n
} }
// newfuncname generates a new name node for a function or method.
// TODO(rsc): Use an ODCLFUNC node instead. See comment in CL 7360.
func newfuncname(s *types.Sym) *Node {
return newfuncnamel(lineno, s)
}
// newfuncnamel generates a new name node for a function or method. // newfuncnamel generates a new name node for a function or method.
// TODO(rsc): Use an ODCLFUNC node instead. See comment in CL 7360. // TODO(rsc): Use an ODCLFUNC node instead. See comment in CL 7360.
func newfuncnamel(pos src.XPos, s *types.Sym) *Node { func newfuncnamel(pos src.XPos, s *types.Sym) *Node {
@ -1013,7 +1007,7 @@ func dclfunc(sym *types.Sym, tfn *Node) *Node {
} }
fn := nod(ODCLFUNC, nil, nil) fn := nod(ODCLFUNC, nil, nil)
fn.Func.Nname = newfuncname(sym) fn.Func.Nname = newfuncnamel(lineno, sym)
fn.Func.Nname.Name.Defn = fn fn.Func.Nname.Name.Defn = fn
fn.Func.Nname.Name.Param.Ntype = tfn fn.Func.Nname.Name.Param.Ntype = tfn
declare(fn.Func.Nname, PFUNC) declare(fn.Func.Nname, PFUNC)

View File

@ -237,7 +237,7 @@ func (p *noder) node() {
types.Block = 1 types.Block = 1
imported_unsafe = false imported_unsafe = false
p.lineno(p.file.PkgName) p.setlineno(p.file.PkgName)
mkpackage(p.file.PkgName.Value) mkpackage(p.file.PkgName.Value)
xtop = append(xtop, p.decls(p.file.DeclList)...) xtop = append(xtop, p.decls(p.file.DeclList)...)
@ -259,7 +259,7 @@ func (p *noder) decls(decls []syntax.Decl) (l []*Node) {
var cs constState var cs constState
for _, decl := range decls { for _, decl := range decls {
p.lineno(decl) p.setlineno(decl)
switch decl := decl.(type) { switch decl := decl.(type) {
case *syntax.ImportDecl: case *syntax.ImportDecl:
p.importDecl(decl) p.importDecl(decl)
@ -335,7 +335,7 @@ func (p *noder) varDecl(decl *syntax.VarDecl) []*Node {
exprs = p.exprList(decl.Values) exprs = p.exprList(decl.Values)
} }
p.lineno(decl) p.setlineno(decl)
return variter(names, typ, exprs) return variter(names, typ, exprs)
} }
@ -433,7 +433,9 @@ func (p *noder) declNames(names []*syntax.Name) []*Node {
} }
func (p *noder) declName(name *syntax.Name) *Node { func (p *noder) declName(name *syntax.Name) *Node {
return p.setlineno(name, dclname(p.name(name))) n := dclname(p.name(name))
n.Pos = p.pos(name)
return n
} }
func (p *noder) funcDecl(fun *syntax.FuncDecl) *Node { func (p *noder) funcDecl(fun *syntax.FuncDecl) *Node {
@ -459,7 +461,7 @@ func (p *noder) funcDecl(fun *syntax.FuncDecl) *Node {
name = nblank.Sym // filled in by typecheckfunc name = nblank.Sym // filled in by typecheckfunc
} }
f.Func.Nname = p.setlineno(fun.Name, newfuncname(name)) f.Func.Nname = newfuncnamel(p.pos(fun.Name), name)
f.Func.Nname.Name.Defn = f f.Func.Nname.Name.Defn = f
f.Func.Nname.Name.Param.Ntype = t f.Func.Nname.Name.Param.Ntype = t
@ -502,7 +504,7 @@ func (p *noder) signature(recv *syntax.Field, typ *syntax.FuncType) *Node {
func (p *noder) params(params []*syntax.Field, dddOk bool) []*Node { func (p *noder) params(params []*syntax.Field, dddOk bool) []*Node {
var nodes []*Node var nodes []*Node
for i, param := range params { for i, param := range params {
p.lineno(param) p.setlineno(param)
nodes = append(nodes, p.param(param, dddOk, i+1 == len(params))) nodes = append(nodes, p.param(param, dddOk, i+1 == len(params)))
} }
return nodes return nodes
@ -552,15 +554,14 @@ func (p *noder) exprs(exprs []syntax.Expr) []*Node {
} }
func (p *noder) expr(expr syntax.Expr) *Node { func (p *noder) expr(expr syntax.Expr) *Node {
p.lineno(expr) p.setlineno(expr)
switch expr := expr.(type) { switch expr := expr.(type) {
case nil, *syntax.BadExpr: case nil, *syntax.BadExpr:
return nil return nil
case *syntax.Name: case *syntax.Name:
return p.mkname(expr) return p.mkname(expr)
case *syntax.BasicLit: case *syntax.BasicLit:
return p.setlineno(expr, nodlit(p.basicLit(expr))) return nodlit(p.basicLit(expr))
case *syntax.CompositeLit: case *syntax.CompositeLit:
n := p.nod(expr, OCOMPLIT, nil, nil) n := p.nod(expr, OCOMPLIT, nil, nil)
if expr.Type != nil { if expr.Type != nil {
@ -587,7 +588,9 @@ func (p *noder) expr(expr syntax.Expr) *Node {
obj.Name.SetUsed(true) obj.Name.SetUsed(true)
return oldname(restrictlookup(expr.Sel.Value, obj.Name.Pkg)) return oldname(restrictlookup(expr.Sel.Value, obj.Name.Pkg))
} }
return p.setlineno(expr, nodSym(OXDOT, obj, p.name(expr.Sel))) n := nodSym(OXDOT, obj, p.name(expr.Sel))
n.Pos = p.pos(expr) // lineno may have been changed by p.expr(expr.X)
return n
case *syntax.IndexExpr: case *syntax.IndexExpr:
return p.nod(expr, OINDEX, p.expr(expr.X), p.expr(expr.Index)) return p.nod(expr, OINDEX, p.expr(expr.X), p.expr(expr.Index))
case *syntax.SliceExpr: case *syntax.SliceExpr:
@ -771,7 +774,7 @@ func (p *noder) chanDir(dir syntax.ChanDir) types.ChanDir {
func (p *noder) structType(expr *syntax.StructType) *Node { func (p *noder) structType(expr *syntax.StructType) *Node {
var l []*Node var l []*Node
for i, field := range expr.FieldList { for i, field := range expr.FieldList {
p.lineno(field) p.setlineno(field)
var n *Node var n *Node
if field.Name == nil { if field.Name == nil {
n = p.embedded(field.Type) n = p.embedded(field.Type)
@ -784,7 +787,7 @@ func (p *noder) structType(expr *syntax.StructType) *Node {
l = append(l, n) l = append(l, n)
} }
p.lineno(expr) p.setlineno(expr)
n := p.nod(expr, OTSTRUCT, nil, nil) n := p.nod(expr, OTSTRUCT, nil, nil)
n.List.Set(l) n.List.Set(l)
return n return n
@ -793,7 +796,7 @@ func (p *noder) structType(expr *syntax.StructType) *Node {
func (p *noder) interfaceType(expr *syntax.InterfaceType) *Node { func (p *noder) interfaceType(expr *syntax.InterfaceType) *Node {
var l []*Node var l []*Node
for _, method := range expr.MethodList { for _, method := range expr.MethodList {
p.lineno(method) p.setlineno(method)
var n *Node var n *Node
if method.Name == nil { if method.Name == nil {
n = p.nodSym(method, ODCLFIELD, oldname(p.packname(method.Type)), nil) n = p.nodSym(method, ODCLFIELD, oldname(p.packname(method.Type)), nil)
@ -882,7 +885,7 @@ func (p *noder) stmt(stmt syntax.Stmt) *Node {
} }
func (p *noder) stmtFall(stmt syntax.Stmt, fallOK bool) *Node { func (p *noder) stmtFall(stmt syntax.Stmt, fallOK bool) *Node {
p.lineno(stmt) p.setlineno(stmt)
switch stmt := stmt.(type) { switch stmt := stmt.(type) {
case *syntax.EmptyStmt: case *syntax.EmptyStmt:
return nil return nil
@ -1010,7 +1013,7 @@ func (p *noder) assignList(expr syntax.Expr, defn *Node, colas bool) []*Node {
newOrErr := false newOrErr := false
for i, expr := range exprs { for i, expr := range exprs {
p.lineno(expr) p.setlineno(expr)
res[i] = nblank res[i] = nblank
name, ok := expr.(*syntax.Name) name, ok := expr.(*syntax.Name)
@ -1132,7 +1135,7 @@ func (p *noder) switchStmt(stmt *syntax.SwitchStmt) *Node {
func (p *noder) caseClauses(clauses []*syntax.CaseClause, tswitch *Node, rbrace syntax.Pos) []*Node { func (p *noder) caseClauses(clauses []*syntax.CaseClause, tswitch *Node, rbrace syntax.Pos) []*Node {
var nodes []*Node var nodes []*Node
for i, clause := range clauses { for i, clause := range clauses {
p.lineno(clause) p.setlineno(clause)
if i > 0 { if i > 0 {
p.closeScope(clause.Pos()) p.closeScope(clause.Pos())
} }
@ -1188,7 +1191,7 @@ func (p *noder) selectStmt(stmt *syntax.SelectStmt) *Node {
func (p *noder) commClauses(clauses []*syntax.CommClause, rbrace syntax.Pos) []*Node { func (p *noder) commClauses(clauses []*syntax.CommClause, rbrace syntax.Pos) []*Node {
var nodes []*Node var nodes []*Node
for i, clause := range clauses { for i, clause := range clauses {
p.lineno(clause) p.setlineno(clause)
if i > 0 { if i > 0 {
p.closeScope(clause.Pos()) p.closeScope(clause.Pos())
} }
@ -1361,33 +1364,28 @@ func (p *noder) wrapname(n syntax.Node, x *Node) *Node {
} }
func (p *noder) nod(orig syntax.Node, op Op, left, right *Node) *Node { func (p *noder) nod(orig syntax.Node, op Op, left, right *Node) *Node {
return p.setlineno(orig, nod(op, left, right)) return nodl(p.pos(orig), op, left, right)
} }
func (p *noder) nodSym(orig syntax.Node, op Op, left *Node, sym *types.Sym) *Node { func (p *noder) nodSym(orig syntax.Node, op Op, left *Node, sym *types.Sym) *Node {
return p.setlineno(orig, nodSym(op, left, sym)) n := nodSym(op, left, sym)
n.Pos = p.pos(orig)
return n
} }
func (p *noder) setlineno(src_ syntax.Node, dst *Node) *Node { func (p *noder) pos(n syntax.Node) src.XPos {
pos := src_.Pos() // TODO(gri): orig.Pos() should always be known - fix package syntax
if !pos.IsKnown() { xpos := lineno
// TODO(mdempsky): Shouldn't happen. Fix package syntax. if pos := n.Pos(); pos.IsKnown() {
return dst xpos = p.makeXPos(pos)
} }
dst.Pos = p.makeXPos(pos) return xpos
return dst
} }
func (p *noder) lineno(n syntax.Node) { func (p *noder) setlineno(n syntax.Node) {
if n == nil { if n != nil {
return lineno = p.pos(n)
} }
pos := n.Pos()
if !pos.IsKnown() {
// TODO(mdempsky): Shouldn't happen. Fix package syntax.
return
}
lineno = p.makeXPos(pos)
} }
// error is called concurrently if files are parsed concurrently. // error is called concurrently if files are parsed concurrently.