Commit 2b2348ab authored by Daniel Martí's avatar Daniel Martí

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í <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarMatthew Dempsky <mdempsky@google.com>
parent d6976006
...@@ -136,7 +136,7 @@ func closurename(outerfunc *Node) *types.Sym { ...@@ -136,7 +136,7 @@ func closurename(outerfunc *Node) *types.Sym {
// There may be multiple functions named "_". In those // There may be multiple functions named "_". In those
// cases, we can't use their individual Closgens as it // cases, we can't use their individual Closgens as it
// would lead to name clashes. // would lead to name clashes.
if !isblank(outerfunc.Func.Nname) { if !outerfunc.Func.Nname.isBlank() {
gen = &outerfunc.Func.Closgen gen = &outerfunc.Func.Closgen
} }
} }
......
...@@ -1506,12 +1506,14 @@ func nonnegintconst(n *Node) int64 { ...@@ -1506,12 +1506,14 @@ func nonnegintconst(n *Node) int64 {
return vi.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 // Expressions derived from nil, like string([]byte(nil)), while they
// may be known at compile time, are not Go language constants. // may be known at compile time, are not Go language constants.
// Only called for expressions known to evaluated to compile-time // Only called for expressions known to evaluated to compile-time
// constants. // constants.
func isgoconst(n *Node) bool { func (n *Node) isGoConst() bool {
if n.Orig != nil { if n.Orig != nil {
n = n.Orig n = n.Orig
} }
...@@ -1545,18 +1547,18 @@ func isgoconst(n *Node) bool { ...@@ -1545,18 +1547,18 @@ func isgoconst(n *Node) bool {
OCOMPLEX, OCOMPLEX,
OREAL, OREAL,
OIMAG: OIMAG:
if isgoconst(n.Left) && (n.Right == nil || isgoconst(n.Right)) { if n.Left.isGoConst() && (n.Right == nil || n.Right.isGoConst()) {
return true return true
} }
case OCONV: case OCONV:
if okforconst[n.Type.Etype] && isgoconst(n.Left) { if okforconst[n.Type.Etype] && n.Left.isGoConst() {
return true return true
} }
case OLEN, OCAP: case OLEN, OCAP:
l := n.Left l := n.Left
if isgoconst(l) { if l.isGoConst() {
return true return true
} }
......
...@@ -64,7 +64,7 @@ func declare(n *Node, ctxt Class) { ...@@ -64,7 +64,7 @@ func declare(n *Node, ctxt Class) {
return return
} }
if isblank(n) { if n.isBlank() {
return return
} }
...@@ -178,7 +178,7 @@ func variter(vl []*Node, t *Node, el []*Node) []*Node { ...@@ -178,7 +178,7 @@ func variter(vl []*Node, t *Node, el []*Node) []*Node {
declare(v, dclcontext) declare(v, dclcontext)
v.Name.Param.Ntype = t v.Name.Param.Ntype = t
if e != nil || Curfn != nil || isblank(v) { if e != nil || Curfn != nil || v.isBlank() {
if Curfn != nil { if Curfn != nil {
init = append(init, nod(ODCL, v, nil)) init = append(init, nod(ODCL, v, nil))
} }
...@@ -326,7 +326,7 @@ func colasdefn(left []*Node, defn *Node) { ...@@ -326,7 +326,7 @@ func colasdefn(left []*Node, defn *Node) {
var nnew, nerr int var nnew, nerr int
for i, n := range left { for i, n := range left {
if isblank(n) { if n.isBlank() {
continue continue
} }
if !colasname(n) { if !colasname(n) {
...@@ -367,7 +367,7 @@ func ifacedcl(n *Node) { ...@@ -367,7 +367,7 @@ func ifacedcl(n *Node) {
Fatalf("ifacedcl") Fatalf("ifacedcl")
} }
if isblank(n.Left) { if n.Left.isBlank() {
yyerror("methods must have a unique non-blank name") yyerror("methods must have a unique non-blank name")
} }
} }
...@@ -457,7 +457,7 @@ func funcargs(nt *Node) { ...@@ -457,7 +457,7 @@ func funcargs(nt *Node) {
// TODO: n->left->missing = 1; // TODO: n->left->missing = 1;
n.Left.Op = ONAME 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'. // 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 // The name must be different from ~r above because if you have
// func f() (_ int) // func f() (_ int)
......
...@@ -1066,7 +1066,7 @@ func (e *EscState) escassignSinkWhyWhere(dst, src *Node, reason string, call *No ...@@ -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 // evaluated in curfn. For expr==nil, dst must still be examined for
// evaluations inside it (e.g *f(x) = y) // evaluations inside it (e.g *f(x) = y)
func (e *EscState) escassign(dst, src *Node, step *EscStep) { 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 return
} }
......
...@@ -174,7 +174,7 @@ func (f *Func) initLSym() { ...@@ -174,7 +174,7 @@ func (f *Func) initLSym() {
Fatalf("Func.initLSym called twice") Fatalf("Func.initLSym called twice")
} }
if nam := f.Nname; !isblank(nam) { if nam := f.Nname; !nam.isBlank() {
f.lsym = nam.Sym.Linksym() f.lsym = nam.Sym.Linksym()
if f.Pragma&Systemstack != 0 { if f.Pragma&Systemstack != 0 {
f.lsym.Set(obj.AttrCFunc, true) f.lsym.Set(obj.AttrCFunc, true)
......
...@@ -26,7 +26,7 @@ func anyinit(n []*Node) bool { ...@@ -26,7 +26,7 @@ func anyinit(n []*Node) bool {
switch ln.Op { switch ln.Op {
case ODCLFUNC, ODCLCONST, ODCLTYPE, OEMPTY: case ODCLFUNC, ODCLCONST, ODCLTYPE, OEMPTY:
case OAS: case OAS:
if !isblank(ln.Left) || !candiscard(ln.Right) { if !ln.Left.isBlank() || !candiscard(ln.Right) {
return true return true
} }
default: default:
......
...@@ -753,7 +753,7 @@ func mkinlcall(n *Node, fn *Node) *Node { ...@@ -753,7 +753,7 @@ func mkinlcall(n *Node, fn *Node) *Node {
} }
func tinlvar(t *types.Field, inlvars map[*Node]*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)] inlvar := inlvars[asNode(t.Nname)]
if inlvar == nil { if inlvar == nil {
Fatalf("missing inlvar for %v\n", asNode(t.Nname)) Fatalf("missing inlvar for %v\n", asNode(t.Nname))
...@@ -885,7 +885,7 @@ func mkinlcall1(n, fn *Node) *Node { ...@@ -885,7 +885,7 @@ func mkinlcall1(n, fn *Node) *Node {
for i, t := range fn.Type.Results().Fields().Slice() { for i, t := range fn.Type.Results().Fields().Slice() {
var m *Node var m *Node
var mpos src.XPos 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 mpos = asNode(t.Nname).Pos
m = inlvar(asNode(t.Nname)) m = inlvar(asNode(t.Nname))
m = typecheck(m, Erv) m = typecheck(m, Erv)
......
...@@ -654,7 +654,7 @@ func (p *noder) expr(expr syntax.Expr) *Node { ...@@ -654,7 +654,7 @@ func (p *noder) expr(expr syntax.Expr) *Node {
n := p.nod(expr, OTYPESW, nil, p.expr(expr.X)) n := p.nod(expr, OTYPESW, nil, p.expr(expr.X))
if expr.Lhs != nil { if expr.Lhs != nil {
n.Left = p.declName(expr.Lhs) n.Left = p.declName(expr.Lhs)
if isblank(n.Left) { if n.Left.isBlank() {
yyerror("invalid variable name %v in type switch", n.Left) yyerror("invalid variable name %v in type switch", n.Left)
} }
} }
......
...@@ -461,7 +461,7 @@ func (o *Order) mapAssign(n *Node) { ...@@ -461,7 +461,7 @@ func (o *Order) mapAssign(n *Node) {
m.Right = o.copyExpr(m.Right, m.Right.Type, false) m.Right = o.copyExpr(m.Right, m.Right.Type, false)
} }
fallthrough fallthrough
case instrumenting && n.Op == OAS2FUNC && !isblank(m): case instrumenting && n.Op == OAS2FUNC && !m.isBlank():
t := o.newTemp(m.Type, false) t := o.newTemp(m.Type, false)
n.List.SetIndex(i, t) n.List.SetIndex(i, t)
a := nod(OAS, m, t) a := nod(OAS, m, t)
...@@ -700,7 +700,7 @@ func (o *Order) stmt(n *Node) { ...@@ -700,7 +700,7 @@ func (o *Order) stmt(n *Node) {
Fatalf("orderstmt range %v", n.Type) Fatalf("orderstmt range %v", n.Type)
case TARRAY, TSLICE: 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). // for i := range x will only use x once, to compute len(x).
// No need to copy it. // No need to copy it.
break break
...@@ -812,7 +812,7 @@ func (o *Order) stmt(n *Node) { ...@@ -812,7 +812,7 @@ func (o *Order) stmt(n *Node) {
// temporary per distinct type, sharing the temp among all receives // temporary per distinct type, sharing the temp among all receives
// with that temp. Similarly one ok bool could be shared among all // 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. // 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 r.Left = nil
} }
if r.Left != nil { if r.Left != nil {
...@@ -833,7 +833,7 @@ func (o *Order) stmt(n *Node) { ...@@ -833,7 +833,7 @@ func (o *Order) stmt(n *Node) {
n2.Ninit.Append(tmp2) 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) r.List.Set(nil)
} }
if r.List.Len() != 0 { if r.List.Len() != 0 {
...@@ -1178,7 +1178,7 @@ func (o *Order) expr(n, lhs *Node) *Node { ...@@ -1178,7 +1178,7 @@ func (o *Order) expr(n, lhs *Node) *Node {
// okas creates and returns an assignment of val to ok, // okas creates and returns an assignment of val to ok,
// including an explicit conversion if necessary. // including an explicit conversion if necessary.
func okas(ok, val *Node) *Node { func okas(ok, val *Node) *Node {
if !isblank(ok) { if !ok.isBlank() {
val = conv(val, ok.Type) val = conv(val, ok.Type)
} }
return nod(OAS, ok, val) return nod(OAS, ok, val)
...@@ -1196,7 +1196,7 @@ func (o *Order) as2(n *Node) { ...@@ -1196,7 +1196,7 @@ func (o *Order) as2(n *Node) {
tmplist := []*Node{} tmplist := []*Node{}
left := []*Node{} left := []*Node{}
for _, l := range n.List.Slice() { for _, l := range n.List.Slice() {
if !isblank(l) { if !l.isBlank() {
tmp := o.newTemp(l.Type, types.Haspointers(l.Type)) tmp := o.newTemp(l.Type, types.Haspointers(l.Type))
tmplist = append(tmplist, tmp) tmplist = append(tmplist, tmp)
left = append(left, l) left = append(left, l)
...@@ -1213,7 +1213,7 @@ func (o *Order) as2(n *Node) { ...@@ -1213,7 +1213,7 @@ func (o *Order) as2(n *Node) {
ti := 0 ti := 0
for ni, l := range n.List.Slice() { for ni, l := range n.List.Slice() {
if !isblank(l) { if !l.isBlank() {
n.List.SetIndex(ni, tmplist[ti]) n.List.SetIndex(ni, tmplist[ti])
ti++ ti++
} }
...@@ -1224,12 +1224,12 @@ func (o *Order) as2(n *Node) { ...@@ -1224,12 +1224,12 @@ func (o *Order) as2(n *Node) {
// Just like as2, this also adds temporaries to ensure left-to-right assignment. // Just like as2, this also adds temporaries to ensure left-to-right assignment.
func (o *Order) okAs2(n *Node) { func (o *Order) okAs2(n *Node) {
var tmp1, tmp2 *Node var tmp1, tmp2 *Node
if !isblank(n.List.First()) { if !n.List.First().isBlank() {
typ := n.Rlist.First().Type typ := n.Rlist.First().Type
tmp1 = o.newTemp(typ, types.Haspointers(typ)) tmp1 = o.newTemp(typ, types.Haspointers(typ))
} }
if !isblank(n.List.Second()) { if !n.List.Second().isBlank() {
tmp2 = o.newTemp(types.Types[TBOOL], false) tmp2 = o.newTemp(types.Types[TBOOL], false)
} }
......
...@@ -106,7 +106,7 @@ func typecheckrangeExpr(n *Node) { ...@@ -106,7 +106,7 @@ func typecheckrangeExpr(n *Node) {
// "if the second iteration variable is the blank identifier, the range // "if the second iteration variable is the blank identifier, the range
// clause is equivalent to the same clause with only the first variable // clause is equivalent to the same clause with only the first variable
// present." // present."
if isblank(v2) { if v2.isBlank() {
if v1 != nil { if v1 != nil {
n.List.Set1(v1) n.List.Set1(v1)
} }
...@@ -177,11 +177,11 @@ func walkrange(n *Node) *Node { ...@@ -177,11 +177,11 @@ func walkrange(n *Node) *Node {
v2 = n.List.Second() v2 = n.List.Second()
} }
if isblank(v2) { if v2.isBlank() {
v2 = nil v2 = nil
} }
if isblank(v1) && v2 == nil { if v1.isBlank() && v2 == nil {
v1 = nil v1 = nil
} }
...@@ -478,7 +478,7 @@ func memclrrange(n, v1, v2, a *Node) bool { ...@@ -478,7 +478,7 @@ func memclrrange(n, v1, v2, a *Node) bool {
return false return false
} }
elemsize := n.Type.Elem().Width elemsize := n.Type.Elem().Width
if elemsize <= 0 || !iszero(stmt.Right) { if elemsize <= 0 || !isZero(stmt.Right) {
return false return false
} }
......
...@@ -56,7 +56,7 @@ func init1(n *Node, out *[]*Node) { ...@@ -56,7 +56,7 @@ func init1(n *Node, out *[]*Node) {
switch n.Class() { switch n.Class() {
case PEXTERN, PFUNC: case PEXTERN, PFUNC:
default: 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 // blank names initialization is part of init() but not
// when they are inside a function. // when they are inside a function.
break break
...@@ -115,7 +115,7 @@ func init1(n *Node, out *[]*Node) { ...@@ -115,7 +115,7 @@ func init1(n *Node, out *[]*Node) {
Dump("defn", defn) Dump("defn", defn)
Fatalf("init1: bad defn") Fatalf("init1: bad defn")
} }
if isblank(defn.Left) && candiscard(defn.Right) { if defn.Left.isBlank() && candiscard(defn.Right) {
defn.Op = OEMPTY defn.Op = OEMPTY
defn.Left = nil defn.Left = nil
defn.Right = nil defn.Right = nil
...@@ -126,7 +126,7 @@ func init1(n *Node, out *[]*Node) { ...@@ -126,7 +126,7 @@ func init1(n *Node, out *[]*Node) {
if Debug['j'] != 0 { if Debug['j'] != 0 {
fmt.Printf("%v\n", n.Sym) fmt.Printf("%v\n", n.Sym)
} }
if isblank(n) || !staticinit(n, out) { if n.isBlank() || !staticinit(n, out) {
if Debug['%'] != 0 { if Debug['%'] != 0 {
Dump("nonstatic", defn) Dump("nonstatic", defn)
} }
...@@ -303,7 +303,7 @@ func staticcopy(l *Node, r *Node, out *[]*Node) bool { ...@@ -303,7 +303,7 @@ func staticcopy(l *Node, r *Node, out *[]*Node) bool {
return true return true
case OLITERAL: case OLITERAL:
if iszero(r) { if isZero(r) {
return true return true
} }
gdata(l, r, int(l.Type.Width)) gdata(l, r, int(l.Type.Width))
...@@ -380,7 +380,7 @@ func staticassign(l *Node, r *Node, out *[]*Node) bool { ...@@ -380,7 +380,7 @@ func staticassign(l *Node, r *Node, out *[]*Node) bool {
return staticcopy(l, r, out) return staticcopy(l, r, out)
case OLITERAL: case OLITERAL:
if iszero(r) { if isZero(r) {
return true return true
} }
gdata(l, r, int(l.Type.Width)) gdata(l, r, int(l.Type.Width))
...@@ -578,7 +578,7 @@ func staticname(t *types.Type) *Node { ...@@ -578,7 +578,7 @@ func staticname(t *types.Type) *Node {
return n return n
} }
func isliteral(n *Node) bool { func isLiteral(n *Node) bool {
// Treat nils as zeros rather than literals. // Treat nils as zeros rather than literals.
return n.Op == OLITERAL && n.Val().Ctype() != CTNIL return n.Op == OLITERAL && n.Val().Ctype() != CTNIL
} }
...@@ -607,7 +607,7 @@ const ( ...@@ -607,7 +607,7 @@ const (
func getdyn(n *Node, top bool) initGenType { func getdyn(n *Node, top bool) initGenType {
switch n.Op { switch n.Op {
default: default:
if isliteral(n) { if isLiteral(n) {
return initConst return initConst
} }
return initDynamic return initDynamic
...@@ -742,7 +742,7 @@ func fixedlit(ctxt initContext, kind initKind, n *Node, var_ *Node, init *Nodes) ...@@ -742,7 +742,7 @@ func fixedlit(ctxt initContext, kind initKind, n *Node, var_ *Node, init *Nodes)
continue continue
} }
islit := isliteral(value) islit := isLiteral(value)
if (kind == initKindStatic && !islit) || (kind == initKindDynamic && islit) { if (kind == initKindStatic && !islit) || (kind == initKindDynamic && islit) {
continue continue
} }
...@@ -898,7 +898,7 @@ func slicelit(ctxt initContext, n *Node, var_ *Node, init *Nodes) { ...@@ -898,7 +898,7 @@ func slicelit(ctxt initContext, n *Node, var_ *Node, init *Nodes) {
continue continue
} }
if isliteral(value) { if isLiteral(value) {
continue continue
} }
...@@ -1264,7 +1264,7 @@ func initplan(n *Node) { ...@@ -1264,7 +1264,7 @@ func initplan(n *Node) {
func addvalue(p *InitPlan, xoffset int64, n *Node) { func addvalue(p *InitPlan, xoffset int64, n *Node) {
// special case: zero can be dropped entirely // special case: zero can be dropped entirely
if iszero(n) { if isZero(n) {
return return
} }
...@@ -1284,13 +1284,13 @@ func addvalue(p *InitPlan, xoffset int64, n *Node) { ...@@ -1284,13 +1284,13 @@ func addvalue(p *InitPlan, xoffset int64, n *Node) {
p.E = append(p.E, InitEntry{Xoffset: xoffset, Expr: n}) p.E = append(p.E, InitEntry{Xoffset: xoffset, Expr: n})
} }
func iszero(n *Node) bool { func isZero(n *Node) bool {
switch n.Op { switch n.Op {
case OLITERAL: case OLITERAL:
switch u := n.Val().U.(type) { switch u := n.Val().U.(type) {
default: default:
Dump("unexpected literal", n) Dump("unexpected literal", n)
Fatalf("iszero") Fatalf("isZero")
case *NilVal: case *NilVal:
return true return true
case string: case string:
...@@ -1310,7 +1310,7 @@ func iszero(n *Node) bool { ...@@ -1310,7 +1310,7 @@ func iszero(n *Node) bool {
if n1.Op == OKEY { if n1.Op == OKEY {
n1 = n1.Right n1 = n1.Right
} }
if !iszero(n1) { if !isZero(n1) {
return false return false
} }
} }
...@@ -1318,7 +1318,7 @@ func iszero(n *Node) bool { ...@@ -1318,7 +1318,7 @@ func iszero(n *Node) bool {
case OSTRUCTLIT: case OSTRUCTLIT:
for _, n1 := range n.List.Slice() { for _, n1 := range n.List.Slice() {
if !iszero(n1.Left) { if !isZero(n1.Left) {
return false return false
} }
} }
......
...@@ -800,7 +800,7 @@ func (s *state) stmt(n *Node) { ...@@ -800,7 +800,7 @@ func (s *state) stmt(n *Node) {
// All literals with nonzero fields have already been // All literals with nonzero fields have already been
// rewritten during walk. Any that remain are just T{} // rewritten during walk. Any that remain are just T{}
// or equivalents. Use the zero value. // or equivalents. Use the zero value.
if !iszero(rhs) { if !isZero(rhs) {
Fatalf("literal with nonzero value in SSA: %v", rhs) Fatalf("literal with nonzero value in SSA: %v", rhs)
} }
rhs = nil rhs = nil
...@@ -828,7 +828,7 @@ func (s *state) stmt(n *Node) { ...@@ -828,7 +828,7 @@ func (s *state) stmt(n *Node) {
} }
} }
if isblank(n.Left) { if n.Left.isBlank() {
// _ = rhs // _ = rhs
// Just evaluate rhs for side-effects. // Just evaluate rhs for side-effects.
if rhs != nil { if rhs != nil {
...@@ -2115,7 +2115,7 @@ func (s *state) expr(n *Node) *ssa.Value { ...@@ -2115,7 +2115,7 @@ func (s *state) expr(n *Node) *ssa.Value {
// All literals with nonzero fields have already been // All literals with nonzero fields have already been
// rewritten during walk. Any that remain are just T{} // rewritten during walk. Any that remain are just T{}
// or equivalents. Use the zero value. // or equivalents. Use the zero value.
if !iszero(n.Left) { if !isZero(n.Left) {
Fatalf("literal with nonzero value in SSA: %v", n.Left) Fatalf("literal with nonzero value in SSA: %v", n.Left)
} }
return s.zeroVal(n.Type) return s.zeroVal(n.Type)
...@@ -2267,7 +2267,7 @@ func (s *state) expr(n *Node) *ssa.Value { ...@@ -2267,7 +2267,7 @@ func (s *state) expr(n *Node) *ssa.Value {
// All literals with nonzero fields have already been // All literals with nonzero fields have already been
// rewritten during walk. Any that remain are just T{} // rewritten during walk. Any that remain are just T{}
// or equivalents. Use the zero value. // or equivalents. Use the zero value.
if !iszero(n) { if !isZero(n) {
Fatalf("literal with nonzero value in SSA: %v", n) Fatalf("literal with nonzero value in SSA: %v", n)
} }
return s.zeroVal(n.Type) return s.zeroVal(n.Type)
...@@ -2495,7 +2495,7 @@ const ( ...@@ -2495,7 +2495,7 @@ const (
// If deref is true and right == nil, just do left = 0. // If deref is true and right == nil, just do left = 0.
// skip indicates assignments (at the top level) that can be avoided. // skip indicates assignments (at the top level) that can be avoided.
func (s *state) assign(left *Node, right *ssa.Value, deref bool, skip skipMask) { func (s *state) assign(left *Node, right *ssa.Value, deref bool, skip skipMask) {
if left.Op == ONAME && isblank(left) { if left.Op == ONAME && left.isBlank() {
return return
} }
t := left.Type t := left.Type
......
...@@ -438,8 +438,8 @@ func treecopy(n *Node, pos src.XPos) *Node { ...@@ -438,8 +438,8 @@ func treecopy(n *Node, pos src.XPos) *Node {
} }
} }
// isnil reports whether n represents the universal untyped zero value "nil". // isNil reports whether n represents the universal untyped zero value "nil".
func isnil(n *Node) bool { func (n *Node) isNil() bool {
// Check n.Orig because constant propagation may produce typed nil constants, // Check n.Orig because constant propagation may produce typed nil constants,
// which don't exist in the Go spec. // which don't exist in the Go spec.
return Isconst(n.Orig, CTNIL) return Isconst(n.Orig, CTNIL)
...@@ -462,7 +462,7 @@ func isptrto(t *types.Type, et types.EType) bool { ...@@ -462,7 +462,7 @@ func isptrto(t *types.Type, et types.EType) bool {
return true return true
} }
func isblank(n *Node) bool { func (n *Node) isBlank() bool {
if n == nil { if n == nil {
return false return false
} }
......
...@@ -70,7 +70,7 @@ func typecheckswitch(n *Node) { ...@@ -70,7 +70,7 @@ func typecheckswitch(n *Node) {
if t != nil && !t.IsInterface() { if t != nil && !t.IsInterface() {
yyerrorl(n.Pos, "cannot type switch on non-interface value %L", n.Left.Right) yyerrorl(n.Pos, "cannot type switch on non-interface value %L", n.Left.Right)
} }
if v := n.Left.Left; v != nil && !isblank(v) && n.List.Len() == 0 { if v := n.Left.Left; v != nil && !v.isBlank() && n.List.Len() == 0 {
// We don't actually declare the type switch's guarded // We don't actually declare the type switch's guarded
// declaration itself. So if there are no cases, we // declaration itself. So if there are no cases, we
// won't notice that it went unused. // won't notice that it went unused.
...@@ -143,7 +143,7 @@ func typecheckswitch(n *Node) { ...@@ -143,7 +143,7 @@ func typecheckswitch(n *Node) {
} else { } else {
yyerrorl(ncase.Pos, "invalid case %v in switch (mismatched types %v and bool)", n1, n1.Type) yyerrorl(ncase.Pos, "invalid case %v in switch (mismatched types %v and bool)", n1, n1.Type)
} }
case nilonly != "" && !isnil(n1): case nilonly != "" && !n1.isNil():
yyerrorl(ncase.Pos, "invalid case %v in switch (can only compare %s %v to nil)", n1, nilonly, n.Left) yyerrorl(ncase.Pos, "invalid case %v in switch (can only compare %s %v to nil)", n1, nilonly, n.Left)
case t.IsInterface() && !n1.Type.IsInterface() && !IsComparable(n1.Type): case t.IsInterface() && !n1.Type.IsInterface() && !IsComparable(n1.Type):
yyerrorl(ncase.Pos, "invalid case %L in switch (incomparable type)", n1) yyerrorl(ncase.Pos, "invalid case %L in switch (incomparable type)", n1)
......
...@@ -307,7 +307,7 @@ func typecheck1(n *Node, top int) *Node { ...@@ -307,7 +307,7 @@ func typecheck1(n *Node, top int) *Node {
if top&Easgn == 0 { if top&Easgn == 0 {
// not a write to the variable // not a write to the variable
if isblank(n) { if n.isBlank() {
yyerror("cannot use _ as value") yyerror("cannot use _ as value")
n.Type = nil n.Type = nil
return n return n
...@@ -673,19 +673,19 @@ func typecheck1(n *Node, top int) *Node { ...@@ -673,19 +673,19 @@ func typecheck1(n *Node, top int) *Node {
return n return n
} }
if l.Type.IsSlice() && !isnil(l) && !isnil(r) { if l.Type.IsSlice() && !l.isNil() && !r.isNil() {
yyerror("invalid operation: %v (slice can only be compared to nil)", n) yyerror("invalid operation: %v (slice can only be compared to nil)", n)
n.Type = nil n.Type = nil
return n return n
} }
if l.Type.IsMap() && !isnil(l) && !isnil(r) { if l.Type.IsMap() && !l.isNil() && !r.isNil() {
yyerror("invalid operation: %v (map can only be compared to nil)", n) yyerror("invalid operation: %v (map can only be compared to nil)", n)
n.Type = nil n.Type = nil
return n return n
} }
if l.Type.Etype == TFUNC && !isnil(l) && !isnil(r) { if l.Type.Etype == TFUNC && !l.isNil() && !r.isNil() {
yyerror("invalid operation: %v (func can only be compared to nil)", n) yyerror("invalid operation: %v (func can only be compared to nil)", n)
n.Type = nil n.Type = nil
return n return n
...@@ -3304,7 +3304,7 @@ func typecheckas(n *Node) { ...@@ -3304,7 +3304,7 @@ func typecheckas(n *Node) {
if n.Left.Typecheck() == 0 { if n.Left.Typecheck() == 0 {
n.Left = typecheck(n.Left, Erv|Easgn) n.Left = typecheck(n.Left, Erv|Easgn)
} }
if !isblank(n.Left) { if !n.Left.isBlank() {
checkwidth(n.Left.Type) // ensure width is calculated for backend checkwidth(n.Left.Type) // ensure width is calculated for backend
} }
} }
...@@ -3664,7 +3664,7 @@ func typecheckdef(n *Node) { ...@@ -3664,7 +3664,7 @@ func typecheckdef(n *Node) {
goto ret goto ret
} }
if e.Type != nil && e.Op != OLITERAL || !isgoconst(e) { if e.Type != nil && e.Op != OLITERAL || !e.isGoConst() {
if !e.Diag() { if !e.Diag() {
yyerror("const initializer %v is not a constant", e) yyerror("const initializer %v is not a constant", e)
e.SetDiag(true) e.SetDiag(true)
......
...@@ -703,7 +703,7 @@ opswitch: ...@@ -703,7 +703,7 @@ opswitch:
break break
} }
if !instrumenting && iszero(n.Right) { if !instrumenting && isZero(n.Right) {
break break
} }
...@@ -783,7 +783,7 @@ opswitch: ...@@ -783,7 +783,7 @@ opswitch:
walkexprlistsafe(n.List.Slice(), init) walkexprlistsafe(n.List.Slice(), init)
r.Left = walkexpr(r.Left, init) r.Left = walkexpr(r.Left, init)
var n1 *Node var n1 *Node
if isblank(n.List.First()) { if n.List.First().isBlank() {
n1 = nodnil() n1 = nodnil()
} else { } else {
n1 = nod(OADDR, n.List.First(), nil) n1 = nod(OADDR, n.List.First(), nil)
...@@ -834,14 +834,14 @@ opswitch: ...@@ -834,14 +834,14 @@ opswitch:
// mapaccess2* returns a typed bool, but due to spec changes, // mapaccess2* returns a typed bool, but due to spec changes,
// the boolean result of i.(T) is now untyped so we make it the // the boolean result of i.(T) is now untyped so we make it the
// same type as the variable on the lhs. // same type as the variable on the lhs.
if ok := n.List.Second(); !isblank(ok) && ok.Type.IsBoolean() { if ok := n.List.Second(); !ok.isBlank() && ok.Type.IsBoolean() {
r.Type.Field(1).Type = ok.Type r.Type.Field(1).Type = ok.Type
} }
n.Rlist.Set1(r) n.Rlist.Set1(r)
n.Op = OAS2FUNC n.Op = OAS2FUNC
// don't generate a = *var if a is _ // don't generate a = *var if a is _
if !isblank(a) { if !a.isBlank() {
var_ := temp(types.NewPtr(t.Val())) var_ := temp(types.NewPtr(t.Val()))
var_.SetTypecheck(1) var_.SetTypecheck(1)
var_.SetNonNil(true) // mapaccess always returns a non-nil pointer var_.SetNonNil(true) // mapaccess always returns a non-nil pointer
...@@ -1216,7 +1216,7 @@ opswitch: ...@@ -1216,7 +1216,7 @@ opswitch:
n.Left = walkexpr(n.Left, init) n.Left = walkexpr(n.Left, init)
low, high, max := n.SliceBounds() low, high, max := n.SliceBounds()
low = walkexpr(low, init) low = walkexpr(low, init)
if low != nil && iszero(low) { if low != nil && isZero(low) {
// Reduce x[0:j] to x[:j] and x[0:j:k] to x[:j:k]. // Reduce x[0:j] to x[:j] and x[0:j:k] to x[:j:k].
low = nil low = nil
} }
...@@ -1848,7 +1848,7 @@ func ascompatet(nl Nodes, nr *types.Type) []*Node { ...@@ -1848,7 +1848,7 @@ func ascompatet(nl Nodes, nr *types.Type) []*Node {
var nn, mm Nodes var nn, mm Nodes
for i, l := range nl.Slice() { for i, l := range nl.Slice() {
if isblank(l) { if l.isBlank() {
continue continue
} }
r := nr.Field(i) r := nr.Field(i)
...@@ -1967,7 +1967,7 @@ func nodarg(t interface{}, fp int) *Node { ...@@ -1967,7 +1967,7 @@ func nodarg(t interface{}, fp int) *Node {
// Rewrite argument named _ to __, // Rewrite argument named _ to __,
// or else the assignment to _ will be // or else the assignment to _ will be
// discarded during code generation. // discarded during code generation.
if isblank(n) { if n.isBlank() {
n.Sym = lookup("__") n.Sym = lookup("__")
} }
...@@ -2268,7 +2268,7 @@ func convas(n *Node, init *Nodes) *Node { ...@@ -2268,7 +2268,7 @@ func convas(n *Node, init *Nodes) *Node {
return n return n
} }
if isblank(n.Left) { if n.Left.isBlank() {
n.Right = defaultlit(n.Right, nil) n.Right = defaultlit(n.Right, nil)
return n return n
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment