Skip to content

Commit 2b2348a

Browse files
committed
cmd/compile/internal/gc: add some Node methods
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]>
1 parent d697600 commit 2b2348a

File tree

16 files changed

+70
-68
lines changed

16 files changed

+70
-68
lines changed

src/cmd/compile/internal/gc/closure.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func closurename(outerfunc *Node) *types.Sym {
136136
// There may be multiple functions named "_". In those
137137
// cases, we can't use their individual Closgens as it
138138
// would lead to name clashes.
139-
if !isblank(outerfunc.Func.Nname) {
139+
if !outerfunc.Func.Nname.isBlank() {
140140
gen = &outerfunc.Func.Closgen
141141
}
142142
}

src/cmd/compile/internal/gc/const.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,12 +1506,14 @@ func nonnegintconst(n *Node) int64 {
15061506
return vi.Int64()
15071507
}
15081508

1509-
// Is n a Go language constant (as opposed to a compile-time constant)?
1509+
// isGoConst reports whether n is a Go language constant (as opposed to a
1510+
// compile-time constant).
1511+
//
15101512
// Expressions derived from nil, like string([]byte(nil)), while they
15111513
// may be known at compile time, are not Go language constants.
15121514
// Only called for expressions known to evaluated to compile-time
15131515
// constants.
1514-
func isgoconst(n *Node) bool {
1516+
func (n *Node) isGoConst() bool {
15151517
if n.Orig != nil {
15161518
n = n.Orig
15171519
}
@@ -1545,18 +1547,18 @@ func isgoconst(n *Node) bool {
15451547
OCOMPLEX,
15461548
OREAL,
15471549
OIMAG:
1548-
if isgoconst(n.Left) && (n.Right == nil || isgoconst(n.Right)) {
1550+
if n.Left.isGoConst() && (n.Right == nil || n.Right.isGoConst()) {
15491551
return true
15501552
}
15511553

15521554
case OCONV:
1553-
if okforconst[n.Type.Etype] && isgoconst(n.Left) {
1555+
if okforconst[n.Type.Etype] && n.Left.isGoConst() {
15541556
return true
15551557
}
15561558

15571559
case OLEN, OCAP:
15581560
l := n.Left
1559-
if isgoconst(l) {
1561+
if l.isGoConst() {
15601562
return true
15611563
}
15621564

src/cmd/compile/internal/gc/dcl.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func declare(n *Node, ctxt Class) {
6464
return
6565
}
6666

67-
if isblank(n) {
67+
if n.isBlank() {
6868
return
6969
}
7070

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

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

327327
var nnew, nerr int
328328
for i, n := range left {
329-
if isblank(n) {
329+
if n.isBlank() {
330330
continue
331331
}
332332
if !colasname(n) {
@@ -367,7 +367,7 @@ func ifacedcl(n *Node) {
367367
Fatalf("ifacedcl")
368368
}
369369

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

460-
if isblank(n.Left) {
460+
if n.Left.isBlank() {
461461
// Give it a name so we can assign to it during return. ~b stands for 'blank'.
462462
// The name must be different from ~r above because if you have
463463
// func f() (_ int)

src/cmd/compile/internal/gc/esc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1066,7 +1066,7 @@ func (e *EscState) escassignSinkWhyWhere(dst, src *Node, reason string, call *No
10661066
// evaluated in curfn. For expr==nil, dst must still be examined for
10671067
// evaluations inside it (e.g *f(x) = y)
10681068
func (e *EscState) escassign(dst, src *Node, step *EscStep) {
1069-
if isblank(dst) || dst == nil || src == nil || src.Op == ONONAME || src.Op == OXXX {
1069+
if dst.isBlank() || dst == nil || src == nil || src.Op == ONONAME || src.Op == OXXX {
10701070
return
10711071
}
10721072

src/cmd/compile/internal/gc/gsubr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func (f *Func) initLSym() {
174174
Fatalf("Func.initLSym called twice")
175175
}
176176

177-
if nam := f.Nname; !isblank(nam) {
177+
if nam := f.Nname; !nam.isBlank() {
178178
f.lsym = nam.Sym.Linksym()
179179
if f.Pragma&Systemstack != 0 {
180180
f.lsym.Set(obj.AttrCFunc, true)

src/cmd/compile/internal/gc/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func anyinit(n []*Node) bool {
2626
switch ln.Op {
2727
case ODCLFUNC, ODCLCONST, ODCLTYPE, OEMPTY:
2828
case OAS:
29-
if !isblank(ln.Left) || !candiscard(ln.Right) {
29+
if !ln.Left.isBlank() || !candiscard(ln.Right) {
3030
return true
3131
}
3232
default:

src/cmd/compile/internal/gc/inl.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ func mkinlcall(n *Node, fn *Node) *Node {
753753
}
754754

755755
func tinlvar(t *types.Field, inlvars map[*Node]*Node) *Node {
756-
if asNode(t.Nname) != nil && !isblank(asNode(t.Nname)) {
756+
if asNode(t.Nname) != nil && !asNode(t.Nname).isBlank() {
757757
inlvar := inlvars[asNode(t.Nname)]
758758
if inlvar == nil {
759759
Fatalf("missing inlvar for %v\n", asNode(t.Nname))
@@ -885,7 +885,7 @@ func mkinlcall1(n, fn *Node) *Node {
885885
for i, t := range fn.Type.Results().Fields().Slice() {
886886
var m *Node
887887
var mpos src.XPos
888-
if t != nil && asNode(t.Nname) != nil && !isblank(asNode(t.Nname)) {
888+
if t != nil && asNode(t.Nname) != nil && !asNode(t.Nname).isBlank() {
889889
mpos = asNode(t.Nname).Pos
890890
m = inlvar(asNode(t.Nname))
891891
m = typecheck(m, Erv)

src/cmd/compile/internal/gc/noder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ func (p *noder) expr(expr syntax.Expr) *Node {
654654
n := p.nod(expr, OTYPESW, nil, p.expr(expr.X))
655655
if expr.Lhs != nil {
656656
n.Left = p.declName(expr.Lhs)
657-
if isblank(n.Left) {
657+
if n.Left.isBlank() {
658658
yyerror("invalid variable name %v in type switch", n.Left)
659659
}
660660
}

src/cmd/compile/internal/gc/order.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ func (o *Order) mapAssign(n *Node) {
461461
m.Right = o.copyExpr(m.Right, m.Right.Type, false)
462462
}
463463
fallthrough
464-
case instrumenting && n.Op == OAS2FUNC && !isblank(m):
464+
case instrumenting && n.Op == OAS2FUNC && !m.isBlank():
465465
t := o.newTemp(m.Type, false)
466466
n.List.SetIndex(i, t)
467467
a := nod(OAS, m, t)
@@ -700,7 +700,7 @@ func (o *Order) stmt(n *Node) {
700700
Fatalf("orderstmt range %v", n.Type)
701701

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

836-
if r.List.Len() != 0 && isblank(r.List.First()) {
836+
if r.List.Len() != 0 && r.List.First().isBlank() {
837837
r.List.Set(nil)
838838
}
839839
if r.List.Len() != 0 {
@@ -1178,7 +1178,7 @@ func (o *Order) expr(n, lhs *Node) *Node {
11781178
// okas creates and returns an assignment of val to ok,
11791179
// including an explicit conversion if necessary.
11801180
func okas(ok, val *Node) *Node {
1181-
if !isblank(ok) {
1181+
if !ok.isBlank() {
11821182
val = conv(val, ok.Type)
11831183
}
11841184
return nod(OAS, ok, val)
@@ -1196,7 +1196,7 @@ func (o *Order) as2(n *Node) {
11961196
tmplist := []*Node{}
11971197
left := []*Node{}
11981198
for _, l := range n.List.Slice() {
1199-
if !isblank(l) {
1199+
if !l.isBlank() {
12001200
tmp := o.newTemp(l.Type, types.Haspointers(l.Type))
12011201
tmplist = append(tmplist, tmp)
12021202
left = append(left, l)
@@ -1213,7 +1213,7 @@ func (o *Order) as2(n *Node) {
12131213

12141214
ti := 0
12151215
for ni, l := range n.List.Slice() {
1216-
if !isblank(l) {
1216+
if !l.isBlank() {
12171217
n.List.SetIndex(ni, tmplist[ti])
12181218
ti++
12191219
}
@@ -1224,12 +1224,12 @@ func (o *Order) as2(n *Node) {
12241224
// Just like as2, this also adds temporaries to ensure left-to-right assignment.
12251225
func (o *Order) okAs2(n *Node) {
12261226
var tmp1, tmp2 *Node
1227-
if !isblank(n.List.First()) {
1227+
if !n.List.First().isBlank() {
12281228
typ := n.Rlist.First().Type
12291229
tmp1 = o.newTemp(typ, types.Haspointers(typ))
12301230
}
12311231

1232-
if !isblank(n.List.Second()) {
1232+
if !n.List.Second().isBlank() {
12331233
tmp2 = o.newTemp(types.Types[TBOOL], false)
12341234
}
12351235

src/cmd/compile/internal/gc/range.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func typecheckrangeExpr(n *Node) {
106106
// "if the second iteration variable is the blank identifier, the range
107107
// clause is equivalent to the same clause with only the first variable
108108
// present."
109-
if isblank(v2) {
109+
if v2.isBlank() {
110110
if v1 != nil {
111111
n.List.Set1(v1)
112112
}
@@ -177,11 +177,11 @@ func walkrange(n *Node) *Node {
177177
v2 = n.List.Second()
178178
}
179179

180-
if isblank(v2) {
180+
if v2.isBlank() {
181181
v2 = nil
182182
}
183183

184-
if isblank(v1) && v2 == nil {
184+
if v1.isBlank() && v2 == nil {
185185
v1 = nil
186186
}
187187

@@ -478,7 +478,7 @@ func memclrrange(n, v1, v2, a *Node) bool {
478478
return false
479479
}
480480
elemsize := n.Type.Elem().Width
481-
if elemsize <= 0 || !iszero(stmt.Right) {
481+
if elemsize <= 0 || !isZero(stmt.Right) {
482482
return false
483483
}
484484

src/cmd/compile/internal/gc/sinit.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func init1(n *Node, out *[]*Node) {
5656
switch n.Class() {
5757
case PEXTERN, PFUNC:
5858
default:
59-
if isblank(n) && n.Name.Curfn == nil && n.Name.Defn != nil && n.Name.Defn.Initorder() == InitNotStarted {
59+
if n.isBlank() && n.Name.Curfn == nil && n.Name.Defn != nil && n.Name.Defn.Initorder() == InitNotStarted {
6060
// blank names initialization is part of init() but not
6161
// when they are inside a function.
6262
break
@@ -115,7 +115,7 @@ func init1(n *Node, out *[]*Node) {
115115
Dump("defn", defn)
116116
Fatalf("init1: bad defn")
117117
}
118-
if isblank(defn.Left) && candiscard(defn.Right) {
118+
if defn.Left.isBlank() && candiscard(defn.Right) {
119119
defn.Op = OEMPTY
120120
defn.Left = nil
121121
defn.Right = nil
@@ -126,7 +126,7 @@ func init1(n *Node, out *[]*Node) {
126126
if Debug['j'] != 0 {
127127
fmt.Printf("%v\n", n.Sym)
128128
}
129-
if isblank(n) || !staticinit(n, out) {
129+
if n.isBlank() || !staticinit(n, out) {
130130
if Debug['%'] != 0 {
131131
Dump("nonstatic", defn)
132132
}
@@ -303,7 +303,7 @@ func staticcopy(l *Node, r *Node, out *[]*Node) bool {
303303
return true
304304

305305
case OLITERAL:
306-
if iszero(r) {
306+
if isZero(r) {
307307
return true
308308
}
309309
gdata(l, r, int(l.Type.Width))
@@ -380,7 +380,7 @@ func staticassign(l *Node, r *Node, out *[]*Node) bool {
380380
return staticcopy(l, r, out)
381381

382382
case OLITERAL:
383-
if iszero(r) {
383+
if isZero(r) {
384384
return true
385385
}
386386
gdata(l, r, int(l.Type.Width))
@@ -578,7 +578,7 @@ func staticname(t *types.Type) *Node {
578578
return n
579579
}
580580

581-
func isliteral(n *Node) bool {
581+
func isLiteral(n *Node) bool {
582582
// Treat nils as zeros rather than literals.
583583
return n.Op == OLITERAL && n.Val().Ctype() != CTNIL
584584
}
@@ -607,7 +607,7 @@ const (
607607
func getdyn(n *Node, top bool) initGenType {
608608
switch n.Op {
609609
default:
610-
if isliteral(n) {
610+
if isLiteral(n) {
611611
return initConst
612612
}
613613
return initDynamic
@@ -742,7 +742,7 @@ func fixedlit(ctxt initContext, kind initKind, n *Node, var_ *Node, init *Nodes)
742742
continue
743743
}
744744

745-
islit := isliteral(value)
745+
islit := isLiteral(value)
746746
if (kind == initKindStatic && !islit) || (kind == initKindDynamic && islit) {
747747
continue
748748
}
@@ -898,7 +898,7 @@ func slicelit(ctxt initContext, n *Node, var_ *Node, init *Nodes) {
898898
continue
899899
}
900900

901-
if isliteral(value) {
901+
if isLiteral(value) {
902902
continue
903903
}
904904

@@ -1264,7 +1264,7 @@ func initplan(n *Node) {
12641264

12651265
func addvalue(p *InitPlan, xoffset int64, n *Node) {
12661266
// special case: zero can be dropped entirely
1267-
if iszero(n) {
1267+
if isZero(n) {
12681268
return
12691269
}
12701270

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

1287-
func iszero(n *Node) bool {
1287+
func isZero(n *Node) bool {
12881288
switch n.Op {
12891289
case OLITERAL:
12901290
switch u := n.Val().U.(type) {
12911291
default:
12921292
Dump("unexpected literal", n)
1293-
Fatalf("iszero")
1293+
Fatalf("isZero")
12941294
case *NilVal:
12951295
return true
12961296
case string:
@@ -1310,15 +1310,15 @@ func iszero(n *Node) bool {
13101310
if n1.Op == OKEY {
13111311
n1 = n1.Right
13121312
}
1313-
if !iszero(n1) {
1313+
if !isZero(n1) {
13141314
return false
13151315
}
13161316
}
13171317
return true
13181318

13191319
case OSTRUCTLIT:
13201320
for _, n1 := range n.List.Slice() {
1321-
if !iszero(n1.Left) {
1321+
if !isZero(n1.Left) {
13221322
return false
13231323
}
13241324
}

0 commit comments

Comments
 (0)