1
0
mirror of https://github.com/golang/go synced 2024-11-26 14:46:47 -07:00

[dev.regabi] cmd/compile: add ir.PkgName

OPACK was using a whole Node and Name and Param
to hold about three fields. Give it its own implementation.

Passes buildall w/ toolstash -cmp.

Change-Id: I85a28b43d37183b2062d337b0b1b2eea52884e8c
Reviewed-on: https://go-review.googlesource.com/c/go/+/274093
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Russ Cox 2020-11-28 01:11:49 -05:00
parent 420809ab08
commit f6106d195d
6 changed files with 44 additions and 29 deletions

View File

@ -955,8 +955,9 @@ func clearImports() {
// leave s->block set to cause redeclaration // leave s->block set to cause redeclaration
// errors if a conflicting top-level name is // errors if a conflicting top-level name is
// introduced by a different file. // introduced by a different file.
if !n.Name().Used() && base.SyntaxErrors() == 0 { p := n.(*ir.PkgName)
unused = append(unused, importedPkg{n.Pos(), n.Name().Pkg.Path, s.Name}) if !p.Used && base.SyntaxErrors() == 0 {
unused = append(unused, importedPkg{p.Pos(), p.Pkg.Path, s.Name})
} }
s.Def = nil s.Def = nil
continue continue
@ -964,9 +965,9 @@ func clearImports() {
if IsAlias(s) { if IsAlias(s) {
// throw away top-level name left over // throw away top-level name left over
// from previous import . "x" // from previous import . "x"
if n.Name() != nil && n.Name().Pack != nil && !n.Name().Pack.Name().Used() && base.SyntaxErrors() == 0 { if name := n.Name(); name != nil && name.PkgName != nil && !name.PkgName.Used && base.SyntaxErrors() == 0 {
unused = append(unused, importedPkg{n.Name().Pack.Pos(), n.Name().Pack.Name().Pkg.Path, ""}) unused = append(unused, importedPkg{name.PkgName.Pos(), name.PkgName.Pkg.Path, ""})
n.Name().Pack.Name().SetUsed(true) name.PkgName.Used = true
} }
s.Def = nil s.Def = nil
continue continue

View File

@ -356,9 +356,7 @@ func (p *noder) importDecl(imp *syntax.ImportDecl) {
my = lookup(ipkg.Name) my = lookup(ipkg.Name)
} }
pack := p.nod(imp, ir.OPACK, nil, nil) pack := ir.NewPkgName(p.pos(imp), my, ipkg)
pack.SetSym(my)
pack.Name().Pkg = ipkg
switch my.Name { switch my.Name {
case ".": case ".":
@ -685,8 +683,9 @@ func (p *noder) expr(expr syntax.Expr) ir.Node {
// parser.new_dotname // parser.new_dotname
obj := p.expr(expr.X) obj := p.expr(expr.X)
if obj.Op() == ir.OPACK { if obj.Op() == ir.OPACK {
obj.Name().SetUsed(true) pack := obj.(*ir.PkgName)
return importName(obj.Name().Pkg.Lookup(expr.Sel.Value)) pack.Used = true
return importName(pack.Pkg.Lookup(expr.Sel.Value))
} }
n := nodSym(ir.OXDOT, obj, p.name(expr.Sel)) n := nodSym(ir.OXDOT, obj, p.name(expr.Sel))
n.SetPos(p.pos(expr)) // lineno may have been changed by p.expr(expr.X) n.SetPos(p.pos(expr)) // lineno may have been changed by p.expr(expr.X)
@ -910,8 +909,8 @@ func (p *noder) packname(expr syntax.Expr) *types.Sym {
switch expr := expr.(type) { switch expr := expr.(type) {
case *syntax.Name: case *syntax.Name:
name := p.name(expr) name := p.name(expr)
if n := oldname(name); n.Name() != nil && n.Name().Pack != nil { if n := oldname(name); n.Name() != nil && n.Name().PkgName != nil {
n.Name().Pack.Name().SetUsed(true) n.Name().PkgName.Used = true
} }
return name return name
case *syntax.SelectorExpr: case *syntax.SelectorExpr:
@ -926,8 +925,9 @@ func (p *noder) packname(expr syntax.Expr) *types.Sym {
base.Errorf("%v is not a package", name) base.Errorf("%v is not a package", name)
pkg = ir.LocalPkg pkg = ir.LocalPkg
} else { } else {
def.Name().SetUsed(true) def := def.(*ir.PkgName)
pkg = def.Name().Pkg def.Used = true
pkg = def.Pkg
} }
return pkg.Lookup(expr.Sel.Value) return pkg.Lookup(expr.Sel.Value)
} }
@ -1675,8 +1675,8 @@ func safeArg(name string) bool {
func mkname(sym *types.Sym) ir.Node { func mkname(sym *types.Sym) ir.Node {
n := oldname(sym) n := oldname(sym)
if n.Name() != nil && n.Name().Pack != nil { if n.Name() != nil && n.Name().PkgName != nil {
n.Name().Pack.Name().SetUsed(true) n.Name().PkgName.Used = true
} }
return n return n
} }

View File

@ -102,7 +102,7 @@ func autolabel(prefix string) *types.Sym {
// find all the exported symbols in package opkg // find all the exported symbols in package opkg
// and make them available in the current package // and make them available in the current package
func importdot(opkg *types.Pkg, pack ir.Node) { func importdot(opkg *types.Pkg, pack *ir.PkgName) {
n := 0 n := 0
for _, s := range opkg.Syms { for _, s := range opkg.Syms {
if s.Def == nil { if s.Def == nil {
@ -124,7 +124,7 @@ func importdot(opkg *types.Pkg, pack ir.Node) {
ir.Dump("s1def", ir.AsNode(s1.Def)) ir.Dump("s1def", ir.AsNode(s1.Def))
base.Fatalf("missing Name") base.Fatalf("missing Name")
} }
ir.AsNode(s1.Def).Name().Pack = pack ir.AsNode(s1.Def).Name().PkgName = pack
s1.Origpkg = opkg s1.Origpkg = opkg
n++ n++
} }

View File

@ -9,13 +9,13 @@ import (
"cmd/compile/internal/types" "cmd/compile/internal/types"
"cmd/internal/objabi" "cmd/internal/objabi"
"cmd/internal/src" "cmd/internal/src"
"fmt"
"go/constant" "go/constant"
) )
// Name holds Node fields used only by named nodes (ONAME, OTYPE, OPACK, OLABEL, some OLITERAL). // Name holds Node fields used only by named nodes (ONAME, OTYPE, some OLITERAL).
type Name struct { type Name struct {
Pack Node // real package for import . names PkgName *PkgName // real package for import . names
Pkg *types.Pkg // pkg for OPACK nodes
// For a local variable (not param) or extern, the initializing assignment (OAS or OAS2). // For a local variable (not param) or extern, the initializing assignment (OAS or OAS2).
// For a closure var, the ONAME node of the outer captured variable // For a closure var, the ONAME node of the outer captured variable
Defn Node Defn Node
@ -374,3 +374,23 @@ const (
// Careful: Class is stored in three bits in Node.flags. // Careful: Class is stored in three bits in Node.flags.
_ = uint((1 << 3) - iota) // static assert for iota <= (1 << 3) _ = uint((1 << 3) - iota) // static assert for iota <= (1 << 3)
) )
// A Pack is an identifier referring to an imported package.
type PkgName struct {
miniNode
sym *types.Sym
Pkg *types.Pkg
Used bool
}
func (p *PkgName) String() string { return fmt.Sprint(p) }
func (p *PkgName) Format(s fmt.State, verb rune) { FmtNode(p, s, verb) }
func (p *PkgName) RawCopy() Node { c := *p; return &c }
func (p *PkgName) Sym() *types.Sym { return p.sym }
func NewPkgName(pos src.XPos, sym *types.Sym, pkg *types.Pkg) *PkgName {
p := &PkgName{sym: sym, Pkg: pkg}
p.op = OPACK
p.pos = pos
return p
}

View File

@ -1339,12 +1339,7 @@ func NodAt(pos src.XPos, op Op, nleft, nright Node) Node {
n.SetFunc(&x.f) n.SetFunc(&x.f)
n.Func().Decl = n n.Func().Decl = n
case OPACK: case OPACK:
var x struct { return NewPkgName(pos, nil, nil)
n node
m Name
}
n = &x.n
n.SetName(&x.m)
case OEMPTY: case OEMPTY:
return NewEmptyStmt(pos) return NewEmptyStmt(pos)
case OBREAK, OCONTINUE, OFALL, OGOTO: case OBREAK, OCONTINUE, OFALL, OGOTO:
@ -1462,7 +1457,6 @@ var okForNod = [OEND]bool{
OOFFSETOF: true, OOFFSETOF: true,
OOR: true, OOR: true,
OOROR: true, OOROR: true,
OPACK: true,
OPANIC: true, OPANIC: true,
OPAREN: true, OPAREN: true,
OPLUS: true, OPLUS: true,

View File

@ -21,7 +21,7 @@ func TestSizeof(t *testing.T) {
_64bit uintptr // size on 64bit platforms _64bit uintptr // size on 64bit platforms
}{ }{
{Func{}, 152, 280}, {Func{}, 152, 280},
{Name{}, 44, 80}, {Name{}, 36, 64},
{Param{}, 44, 88}, {Param{}, 44, 88},
{node{}, 88, 152}, {node{}, 88, 152},
} }