Skip to content

Commit

Permalink
cmd/compile/internal/gc: add some Node methods
Browse files Browse the repository at this point in the history
Focus on "isfoo" funcs that take a *Node, and conver them to isFoo
methods instead. This makes for more idiomatic Go code, and also more
readable func names.

Found candidates with grep, and applied most changes with sed. The funcs
chosen were isgoconst, isnil, and isblank. All had the same signature,
func(*Node) bool.

While at it, camelCase the isliteral and iszero function names. Don't
move these to methods, as they are only used in the backend part of gc,
which might one day be split into a separate package.

Passes toolstash -cmp on std cmd.

Change-Id: I4df081b12d36c46c253167c8841c5a841f1c5a16
Reviewed-on: https://go-review.googlesource.com/105555
Run-TryBot: Daniel Martí <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Matthew Dempsky <[email protected]>
  • Loading branch information
mvdan committed Apr 16, 2018
1 parent d697600 commit 2b2348a
Show file tree
Hide file tree
Showing 16 changed files with 70 additions and 68 deletions.
2 changes: 1 addition & 1 deletion src/cmd/compile/internal/gc/closure.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func closurename(outerfunc *Node) *types.Sym {
// There may be multiple functions named "_". In those
// cases, we can't use their individual Closgens as it
// would lead to name clashes.
if !isblank(outerfunc.Func.Nname) {
if !outerfunc.Func.Nname.isBlank() {
gen = &outerfunc.Func.Closgen
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/cmd/compile/internal/gc/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -1506,12 +1506,14 @@ func nonnegintconst(n *Node) int64 {
return vi.Int64()
}

// Is n a Go language constant (as opposed to a compile-time constant)?
// isGoConst reports whether n is a Go language constant (as opposed to a
// compile-time constant).
//
// Expressions derived from nil, like string([]byte(nil)), while they
// may be known at compile time, are not Go language constants.
// Only called for expressions known to evaluated to compile-time
// constants.
func isgoconst(n *Node) bool {
func (n *Node) isGoConst() bool {
if n.Orig != nil {
n = n.Orig
}
Expand Down Expand Up @@ -1545,18 +1547,18 @@ func isgoconst(n *Node) bool {
OCOMPLEX,
OREAL,
OIMAG:
if isgoconst(n.Left) && (n.Right == nil || isgoconst(n.Right)) {
if n.Left.isGoConst() && (n.Right == nil || n.Right.isGoConst()) {
return true
}

case OCONV:
if okforconst[n.Type.Etype] && isgoconst(n.Left) {
if okforconst[n.Type.Etype] && n.Left.isGoConst() {
return true
}

case OLEN, OCAP:
l := n.Left
if isgoconst(l) {
if l.isGoConst() {
return true
}

Expand Down
10 changes: 5 additions & 5 deletions src/cmd/compile/internal/gc/dcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func declare(n *Node, ctxt Class) {
return
}

if isblank(n) {
if n.isBlank() {
return
}

Expand Down Expand Up @@ -178,7 +178,7 @@ func variter(vl []*Node, t *Node, el []*Node) []*Node {
declare(v, dclcontext)
v.Name.Param.Ntype = t

if e != nil || Curfn != nil || isblank(v) {
if e != nil || Curfn != nil || v.isBlank() {
if Curfn != nil {
init = append(init, nod(ODCL, v, nil))
}
Expand Down Expand Up @@ -326,7 +326,7 @@ func colasdefn(left []*Node, defn *Node) {

var nnew, nerr int
for i, n := range left {
if isblank(n) {
if n.isBlank() {
continue
}
if !colasname(n) {
Expand Down Expand Up @@ -367,7 +367,7 @@ func ifacedcl(n *Node) {
Fatalf("ifacedcl")
}

if isblank(n.Left) {
if n.Left.isBlank() {
yyerror("methods must have a unique non-blank name")
}
}
Expand Down Expand Up @@ -457,7 +457,7 @@ func funcargs(nt *Node) {
// TODO: n->left->missing = 1;
n.Left.Op = ONAME

if isblank(n.Left) {
if n.Left.isBlank() {
// Give it a name so we can assign to it during return. ~b stands for 'blank'.
// The name must be different from ~r above because if you have
// func f() (_ int)
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/compile/internal/gc/esc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ func (e *EscState) escassignSinkWhyWhere(dst, src *Node, reason string, call *No
// evaluated in curfn. For expr==nil, dst must still be examined for
// evaluations inside it (e.g *f(x) = y)
func (e *EscState) escassign(dst, src *Node, step *EscStep) {
if isblank(dst) || dst == nil || src == nil || src.Op == ONONAME || src.Op == OXXX {
if dst.isBlank() || dst == nil || src == nil || src.Op == ONONAME || src.Op == OXXX {
return
}

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/compile/internal/gc/gsubr.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (f *Func) initLSym() {
Fatalf("Func.initLSym called twice")
}

if nam := f.Nname; !isblank(nam) {
if nam := f.Nname; !nam.isBlank() {
f.lsym = nam.Sym.Linksym()
if f.Pragma&Systemstack != 0 {
f.lsym.Set(obj.AttrCFunc, true)
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/compile/internal/gc/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func anyinit(n []*Node) bool {
switch ln.Op {
case ODCLFUNC, ODCLCONST, ODCLTYPE, OEMPTY:
case OAS:
if !isblank(ln.Left) || !candiscard(ln.Right) {
if !ln.Left.isBlank() || !candiscard(ln.Right) {
return true
}
default:
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/compile/internal/gc/inl.go
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ func mkinlcall(n *Node, fn *Node) *Node {
}

func tinlvar(t *types.Field, inlvars map[*Node]*Node) *Node {
if asNode(t.Nname) != nil && !isblank(asNode(t.Nname)) {
if asNode(t.Nname) != nil && !asNode(t.Nname).isBlank() {
inlvar := inlvars[asNode(t.Nname)]
if inlvar == nil {
Fatalf("missing inlvar for %v\n", asNode(t.Nname))
Expand Down Expand Up @@ -885,7 +885,7 @@ func mkinlcall1(n, fn *Node) *Node {
for i, t := range fn.Type.Results().Fields().Slice() {
var m *Node
var mpos src.XPos
if t != nil && asNode(t.Nname) != nil && !isblank(asNode(t.Nname)) {
if t != nil && asNode(t.Nname) != nil && !asNode(t.Nname).isBlank() {
mpos = asNode(t.Nname).Pos
m = inlvar(asNode(t.Nname))
m = typecheck(m, Erv)
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/compile/internal/gc/noder.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ func (p *noder) expr(expr syntax.Expr) *Node {
n := p.nod(expr, OTYPESW, nil, p.expr(expr.X))
if expr.Lhs != nil {
n.Left = p.declName(expr.Lhs)
if isblank(n.Left) {
if n.Left.isBlank() {
yyerror("invalid variable name %v in type switch", n.Left)
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/cmd/compile/internal/gc/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ func (o *Order) mapAssign(n *Node) {
m.Right = o.copyExpr(m.Right, m.Right.Type, false)
}
fallthrough
case instrumenting && n.Op == OAS2FUNC && !isblank(m):
case instrumenting && n.Op == OAS2FUNC && !m.isBlank():
t := o.newTemp(m.Type, false)
n.List.SetIndex(i, t)
a := nod(OAS, m, t)
Expand Down Expand Up @@ -700,7 +700,7 @@ func (o *Order) stmt(n *Node) {
Fatalf("orderstmt range %v", n.Type)

case TARRAY, TSLICE:
if n.List.Len() < 2 || isblank(n.List.Second()) {
if n.List.Len() < 2 || n.List.Second().isBlank() {
// for i := range x will only use x once, to compute len(x).
// No need to copy it.
break
Expand Down Expand Up @@ -812,7 +812,7 @@ func (o *Order) stmt(n *Node) {
// temporary per distinct type, sharing the temp among all receives
// with that temp. Similarly one ok bool could be shared among all
// the x,ok receives. Not worth doing until there's a clear need.
if r.Left != nil && isblank(r.Left) {
if r.Left != nil && r.Left.isBlank() {
r.Left = nil
}
if r.Left != nil {
Expand All @@ -833,7 +833,7 @@ func (o *Order) stmt(n *Node) {
n2.Ninit.Append(tmp2)
}

if r.List.Len() != 0 && isblank(r.List.First()) {
if r.List.Len() != 0 && r.List.First().isBlank() {
r.List.Set(nil)
}
if r.List.Len() != 0 {
Expand Down Expand Up @@ -1178,7 +1178,7 @@ func (o *Order) expr(n, lhs *Node) *Node {
// okas creates and returns an assignment of val to ok,
// including an explicit conversion if necessary.
func okas(ok, val *Node) *Node {
if !isblank(ok) {
if !ok.isBlank() {
val = conv(val, ok.Type)
}
return nod(OAS, ok, val)
Expand All @@ -1196,7 +1196,7 @@ func (o *Order) as2(n *Node) {
tmplist := []*Node{}
left := []*Node{}
for _, l := range n.List.Slice() {
if !isblank(l) {
if !l.isBlank() {
tmp := o.newTemp(l.Type, types.Haspointers(l.Type))
tmplist = append(tmplist, tmp)
left = append(left, l)
Expand All @@ -1213,7 +1213,7 @@ func (o *Order) as2(n *Node) {

ti := 0
for ni, l := range n.List.Slice() {
if !isblank(l) {
if !l.isBlank() {
n.List.SetIndex(ni, tmplist[ti])
ti++
}
Expand All @@ -1224,12 +1224,12 @@ func (o *Order) as2(n *Node) {
// Just like as2, this also adds temporaries to ensure left-to-right assignment.
func (o *Order) okAs2(n *Node) {
var tmp1, tmp2 *Node
if !isblank(n.List.First()) {
if !n.List.First().isBlank() {
typ := n.Rlist.First().Type
tmp1 = o.newTemp(typ, types.Haspointers(typ))
}

if !isblank(n.List.Second()) {
if !n.List.Second().isBlank() {
tmp2 = o.newTemp(types.Types[TBOOL], false)
}

Expand Down
8 changes: 4 additions & 4 deletions src/cmd/compile/internal/gc/range.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func typecheckrangeExpr(n *Node) {
// "if the second iteration variable is the blank identifier, the range
// clause is equivalent to the same clause with only the first variable
// present."
if isblank(v2) {
if v2.isBlank() {
if v1 != nil {
n.List.Set1(v1)
}
Expand Down Expand Up @@ -177,11 +177,11 @@ func walkrange(n *Node) *Node {
v2 = n.List.Second()
}

if isblank(v2) {
if v2.isBlank() {
v2 = nil
}

if isblank(v1) && v2 == nil {
if v1.isBlank() && v2 == nil {
v1 = nil
}

Expand Down Expand Up @@ -478,7 +478,7 @@ func memclrrange(n, v1, v2, a *Node) bool {
return false
}
elemsize := n.Type.Elem().Width
if elemsize <= 0 || !iszero(stmt.Right) {
if elemsize <= 0 || !isZero(stmt.Right) {
return false
}

Expand Down
28 changes: 14 additions & 14 deletions src/cmd/compile/internal/gc/sinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func init1(n *Node, out *[]*Node) {
switch n.Class() {
case PEXTERN, PFUNC:
default:
if isblank(n) && n.Name.Curfn == nil && n.Name.Defn != nil && n.Name.Defn.Initorder() == InitNotStarted {
if n.isBlank() && n.Name.Curfn == nil && n.Name.Defn != nil && n.Name.Defn.Initorder() == InitNotStarted {
// blank names initialization is part of init() but not
// when they are inside a function.
break
Expand Down Expand Up @@ -115,7 +115,7 @@ func init1(n *Node, out *[]*Node) {
Dump("defn", defn)
Fatalf("init1: bad defn")
}
if isblank(defn.Left) && candiscard(defn.Right) {
if defn.Left.isBlank() && candiscard(defn.Right) {
defn.Op = OEMPTY
defn.Left = nil
defn.Right = nil
Expand All @@ -126,7 +126,7 @@ func init1(n *Node, out *[]*Node) {
if Debug['j'] != 0 {
fmt.Printf("%v\n", n.Sym)
}
if isblank(n) || !staticinit(n, out) {
if n.isBlank() || !staticinit(n, out) {
if Debug['%'] != 0 {
Dump("nonstatic", defn)
}
Expand Down Expand Up @@ -303,7 +303,7 @@ func staticcopy(l *Node, r *Node, out *[]*Node) bool {
return true

case OLITERAL:
if iszero(r) {
if isZero(r) {
return true
}
gdata(l, r, int(l.Type.Width))
Expand Down Expand Up @@ -380,7 +380,7 @@ func staticassign(l *Node, r *Node, out *[]*Node) bool {
return staticcopy(l, r, out)

case OLITERAL:
if iszero(r) {
if isZero(r) {
return true
}
gdata(l, r, int(l.Type.Width))
Expand Down Expand Up @@ -578,7 +578,7 @@ func staticname(t *types.Type) *Node {
return n
}

func isliteral(n *Node) bool {
func isLiteral(n *Node) bool {
// Treat nils as zeros rather than literals.
return n.Op == OLITERAL && n.Val().Ctype() != CTNIL
}
Expand Down Expand Up @@ -607,7 +607,7 @@ const (
func getdyn(n *Node, top bool) initGenType {
switch n.Op {
default:
if isliteral(n) {
if isLiteral(n) {
return initConst
}
return initDynamic
Expand Down Expand Up @@ -742,7 +742,7 @@ func fixedlit(ctxt initContext, kind initKind, n *Node, var_ *Node, init *Nodes)
continue
}

islit := isliteral(value)
islit := isLiteral(value)
if (kind == initKindStatic && !islit) || (kind == initKindDynamic && islit) {
continue
}
Expand Down Expand Up @@ -898,7 +898,7 @@ func slicelit(ctxt initContext, n *Node, var_ *Node, init *Nodes) {
continue
}

if isliteral(value) {
if isLiteral(value) {
continue
}

Expand Down Expand Up @@ -1264,7 +1264,7 @@ func initplan(n *Node) {

func addvalue(p *InitPlan, xoffset int64, n *Node) {
// special case: zero can be dropped entirely
if iszero(n) {
if isZero(n) {
return
}

Expand All @@ -1284,13 +1284,13 @@ func addvalue(p *InitPlan, xoffset int64, n *Node) {
p.E = append(p.E, InitEntry{Xoffset: xoffset, Expr: n})
}

func iszero(n *Node) bool {
func isZero(n *Node) bool {
switch n.Op {
case OLITERAL:
switch u := n.Val().U.(type) {
default:
Dump("unexpected literal", n)
Fatalf("iszero")
Fatalf("isZero")
case *NilVal:
return true
case string:
Expand All @@ -1310,15 +1310,15 @@ func iszero(n *Node) bool {
if n1.Op == OKEY {
n1 = n1.Right
}
if !iszero(n1) {
if !isZero(n1) {
return false
}
}
return true

case OSTRUCTLIT:
for _, n1 := range n.List.Slice() {
if !iszero(n1.Left) {
if !isZero(n1.Left) {
return false
}
}
Expand Down
Loading

0 comments on commit 2b2348a

Please sign in to comment.