aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/syntax
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2020-12-10 21:16:02 -0800
committerRobert Griesemer <gri@golang.org>2020-12-14 21:41:25 +0000
commit3a912f279fb6e3b6942a3a6c2449288a33351b69 (patch)
treee8098ab2709d66c9d35ecea60b403490301c0b47 /src/cmd/compile/internal/syntax
parent8ec9e890008e681dcebdae50379b785eb6f160bb (diff)
downloadgo-3a912f279fb6e3b6942a3a6c2449288a33351b69.tar.gz
go-3a912f279fb6e3b6942a3a6c2449288a33351b69.zip
[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>
Diffstat (limited to 'src/cmd/compile/internal/syntax')
-rw-r--r--src/cmd/compile/internal/syntax/nodes.go7
-rw-r--r--src/cmd/compile/internal/syntax/parser.go23
2 files changed, 13 insertions, 17 deletions
diff --git a/src/cmd/compile/internal/syntax/nodes.go b/src/cmd/compile/internal/syntax/nodes.go
index 306e695a33..fe8f62c6e6 100644
--- a/src/cmd/compile/internal/syntax/nodes.go
+++ b/src/cmd/compile/internal/syntax/nodes.go
@@ -122,6 +122,13 @@ type Group struct {
// ----------------------------------------------------------------------------
// Expressions
+func NewName(pos Pos, value string) *Name {
+ n := new(Name)
+ n.pos = pos
+ n.Value = value
+ return n
+}
+
type (
Expr interface {
Node
diff --git a/src/cmd/compile/internal/syntax/parser.go b/src/cmd/compile/internal/syntax/parser.go
index dbec462ab1..4af7e462ed 100644
--- a/src/cmd/compile/internal/syntax/parser.go
+++ b/src/cmd/compile/internal/syntax/parser.go
@@ -533,7 +533,7 @@ func (p *parser) importDecl(group *Group) Decl {
case _Name:
d.LocalPkgName = p.name()
case _Dot:
- d.LocalPkgName = p.newName(".")
+ d.LocalPkgName = NewName(p.pos(), ".")
p.next()
}
d.Path = p.oliteral()
@@ -1409,9 +1409,7 @@ func (p *parser) interfaceType() *InterfaceType {
case _Type:
if p.mode&AllowGenerics != 0 {
// TODO(gri) factor this better
- type_ := new(Name)
- type_.pos = p.pos()
- type_.Value = "type" // cannot have a method named "type"
+ type_ := NewName(p.pos(), "type") // cannot have a method named "type"
p.next()
if p.tok != _Semi && p.tok != _Rbrace {
f := new(Field)
@@ -1833,9 +1831,7 @@ func (p *parser) paramList(name *Name, close token) (list []*Field) {
typ = par.Type
if par.Name == nil {
pos = typ.Pos()
- n := p.newName("_")
- n.pos = pos // correct position
- par.Name = n
+ par.Name = NewName(pos, "_")
}
} else if typ != nil {
par.Type = typ
@@ -2468,23 +2464,16 @@ func (p *parser) argList() (list []Expr, hasDots bool) {
// ----------------------------------------------------------------------------
// 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 {
// no tracing to avoid overly verbose output
if p.tok == _Name {
- n := p.newName(p.lit)
+ n := NewName(p.pos(), p.lit)
p.next()
return n
}
- n := p.newName("_")
+ n := NewName(p.pos(), "_")
p.syntaxError("expecting name")
p.advance()
return n
@@ -2522,7 +2511,7 @@ func (p *parser) qualifiedName(name *Name) Expr {
case p.tok == _Name:
x = p.name()
default:
- x = p.newName("_")
+ x = NewName(p.pos(), "_")
p.syntaxError("expecting name")
p.advance(_Dot, _Semi, _Rbrace)
}