mirror of
https://github.com/golang/go
synced 2024-11-26 07:38:00 -07:00
[dev.typeparams] cmd/compile/internal/syntax: export NewName and use it
Most syntax.Nodes are allocated in one place and there didn't seem a need to provide factory methods - so as a matter of API design, all nodes are "naked", without any constructors. However, Name nodes are frequently used/replaced and also are created as helper nodes in clients (types2). Make an exception and export NewName. Change-Id: I4b5c499d65bba74592dea68b0936c118b3edaca7 Reviewed-on: https://go-review.googlesource.com/c/go/+/277572 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
parent
8ec9e89000
commit
3a912f279f
@ -122,6 +122,13 @@ type Group struct {
|
|||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// Expressions
|
// Expressions
|
||||||
|
|
||||||
|
func NewName(pos Pos, value string) *Name {
|
||||||
|
n := new(Name)
|
||||||
|
n.pos = pos
|
||||||
|
n.Value = value
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
type (
|
type (
|
||||||
Expr interface {
|
Expr interface {
|
||||||
Node
|
Node
|
||||||
|
@ -533,7 +533,7 @@ func (p *parser) importDecl(group *Group) Decl {
|
|||||||
case _Name:
|
case _Name:
|
||||||
d.LocalPkgName = p.name()
|
d.LocalPkgName = p.name()
|
||||||
case _Dot:
|
case _Dot:
|
||||||
d.LocalPkgName = p.newName(".")
|
d.LocalPkgName = NewName(p.pos(), ".")
|
||||||
p.next()
|
p.next()
|
||||||
}
|
}
|
||||||
d.Path = p.oliteral()
|
d.Path = p.oliteral()
|
||||||
@ -1409,9 +1409,7 @@ func (p *parser) interfaceType() *InterfaceType {
|
|||||||
case _Type:
|
case _Type:
|
||||||
if p.mode&AllowGenerics != 0 {
|
if p.mode&AllowGenerics != 0 {
|
||||||
// TODO(gri) factor this better
|
// TODO(gri) factor this better
|
||||||
type_ := new(Name)
|
type_ := NewName(p.pos(), "type") // cannot have a method named "type"
|
||||||
type_.pos = p.pos()
|
|
||||||
type_.Value = "type" // cannot have a method named "type"
|
|
||||||
p.next()
|
p.next()
|
||||||
if p.tok != _Semi && p.tok != _Rbrace {
|
if p.tok != _Semi && p.tok != _Rbrace {
|
||||||
f := new(Field)
|
f := new(Field)
|
||||||
@ -1833,9 +1831,7 @@ func (p *parser) paramList(name *Name, close token) (list []*Field) {
|
|||||||
typ = par.Type
|
typ = par.Type
|
||||||
if par.Name == nil {
|
if par.Name == nil {
|
||||||
pos = typ.Pos()
|
pos = typ.Pos()
|
||||||
n := p.newName("_")
|
par.Name = NewName(pos, "_")
|
||||||
n.pos = pos // correct position
|
|
||||||
par.Name = n
|
|
||||||
}
|
}
|
||||||
} else if typ != nil {
|
} else if typ != nil {
|
||||||
par.Type = typ
|
par.Type = typ
|
||||||
@ -2468,23 +2464,16 @@ func (p *parser) argList() (list []Expr, hasDots bool) {
|
|||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// Common productions
|
// Common productions
|
||||||
|
|
||||||
func (p *parser) newName(value string) *Name {
|
|
||||||
n := new(Name)
|
|
||||||
n.pos = p.pos()
|
|
||||||
n.Value = value
|
|
||||||
return n
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *parser) name() *Name {
|
func (p *parser) name() *Name {
|
||||||
// no tracing to avoid overly verbose output
|
// no tracing to avoid overly verbose output
|
||||||
|
|
||||||
if p.tok == _Name {
|
if p.tok == _Name {
|
||||||
n := p.newName(p.lit)
|
n := NewName(p.pos(), p.lit)
|
||||||
p.next()
|
p.next()
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
n := p.newName("_")
|
n := NewName(p.pos(), "_")
|
||||||
p.syntaxError("expecting name")
|
p.syntaxError("expecting name")
|
||||||
p.advance()
|
p.advance()
|
||||||
return n
|
return n
|
||||||
@ -2522,7 +2511,7 @@ func (p *parser) qualifiedName(name *Name) Expr {
|
|||||||
case p.tok == _Name:
|
case p.tok == _Name:
|
||||||
x = p.name()
|
x = p.name()
|
||||||
default:
|
default:
|
||||||
x = p.newName("_")
|
x = NewName(p.pos(), "_")
|
||||||
p.syntaxError("expecting name")
|
p.syntaxError("expecting name")
|
||||||
p.advance(_Dot, _Semi, _Rbrace)
|
p.advance(_Dot, _Semi, _Rbrace)
|
||||||
}
|
}
|
||||||
|
@ -523,7 +523,7 @@ L: // unpack receiver type
|
|||||||
check.errorf(arg, "receiver type parameter %s must be an identifier", arg)
|
check.errorf(arg, "receiver type parameter %s must be an identifier", arg)
|
||||||
}
|
}
|
||||||
if par == nil {
|
if par == nil {
|
||||||
par = newName(arg.Pos(), "_")
|
par = syntax.NewName(arg.Pos(), "_")
|
||||||
}
|
}
|
||||||
tparams = append(tparams, par)
|
tparams = append(tparams, par)
|
||||||
}
|
}
|
||||||
|
@ -596,14 +596,6 @@ func (check *Checker) stmt(ctxt stmtContext, s syntax.Stmt) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newName(pos syntax.Pos, value string) *syntax.Name {
|
|
||||||
n := new(syntax.Name)
|
|
||||||
// TODO(gri) why does this not work?
|
|
||||||
//n.pos = pos
|
|
||||||
n.Value = value
|
|
||||||
return n
|
|
||||||
}
|
|
||||||
|
|
||||||
func (check *Checker) switchStmt(inner stmtContext, s *syntax.SwitchStmt) {
|
func (check *Checker) switchStmt(inner stmtContext, s *syntax.SwitchStmt) {
|
||||||
// init statement already handled
|
// init statement already handled
|
||||||
|
|
||||||
@ -624,7 +616,7 @@ func (check *Checker) switchStmt(inner stmtContext, s *syntax.SwitchStmt) {
|
|||||||
if len(s.Body) > 0 {
|
if len(s.Body) > 0 {
|
||||||
pos = s.Body[0].Pos()
|
pos = s.Body[0].Pos()
|
||||||
}
|
}
|
||||||
x.expr = newName(pos, "true")
|
x.expr = syntax.NewName(pos, "true")
|
||||||
}
|
}
|
||||||
|
|
||||||
check.multipleSwitchDefaults(s.Body)
|
check.multipleSwitchDefaults(s.Body)
|
||||||
|
Loading…
Reference in New Issue
Block a user