From 331f9625087ab0169909012ac9a45b7321b81a37 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Tue, 29 Mar 2016 09:14:19 -0700 Subject: [PATCH] cmd/compile: use IsSlice and IsArray instead of checking Bound Changes generated by eg and manually checked. Isfixedarray, Isslice, and many other Type-related functions in subr.go should either be deleted or moved to type.go. Later, though; the game now is cleanup via encapsulation. Passes toolstash -cmp. Change-Id: I83dd8816f6263b74367d23c2719a08c362e330f9 Reviewed-on: https://go-review.googlesource.com/21303 Reviewed-by: Brad Fitzpatrick --- src/cmd/compile/internal/gc/align.go | 2 +- src/cmd/compile/internal/gc/bexport.go | 2 +- src/cmd/compile/internal/gc/fmt.go | 2 +- src/cmd/compile/internal/gc/reflect.go | 6 +++--- src/cmd/compile/internal/gc/sinit.go | 14 +++++++------- src/cmd/compile/internal/gc/subr.go | 4 ++-- src/cmd/compile/internal/gc/type.go | 1 + src/cmd/compile/internal/gc/typecheck.go | 8 ++++---- 8 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/cmd/compile/internal/gc/align.go b/src/cmd/compile/internal/gc/align.go index e852c0b1226..1ca7dd4d20b 100644 --- a/src/cmd/compile/internal/gc/align.go +++ b/src/cmd/compile/internal/gc/align.go @@ -240,7 +240,7 @@ func dowidth(t *Type) { if t.Type == nil { break } - if t.Bound >= 0 { + if t.IsArray() { dowidth(t.Type) if t.Type.Width != 0 { cap := (uint64(Thearch.MAXWIDTH) - 1) / uint64(t.Type.Width) diff --git a/src/cmd/compile/internal/gc/bexport.go b/src/cmd/compile/internal/gc/bexport.go index dd1af1c0f82..f47d7bb06ec 100644 --- a/src/cmd/compile/internal/gc/bexport.go +++ b/src/cmd/compile/internal/gc/bexport.go @@ -506,7 +506,7 @@ func (p *exporter) typ(t *Type) { if t.isDDDArray() { Fatalf("array bounds should be known at export time: %v", t) } - if t.Bound >= 0 { + if t.IsArray() { p.tag(arrayTag) p.int64(t.Bound) } else { diff --git a/src/cmd/compile/internal/gc/fmt.go b/src/cmd/compile/internal/gc/fmt.go index 11122a47418..79d64a10be7 100644 --- a/src/cmd/compile/internal/gc/fmt.go +++ b/src/cmd/compile/internal/gc/fmt.go @@ -586,7 +586,7 @@ func typefmt(t *Type, flag FmtFlag) string { return "*" + t.Type.String() case TARRAY: - if t.Bound >= 0 { + if t.IsArray() { return fmt.Sprintf("[%d]%v", t.Bound, t.Type) } if t.isDDDArray() { diff --git a/src/cmd/compile/internal/gc/reflect.go b/src/cmd/compile/internal/gc/reflect.go index c05ee3cdd11..92f8285fa83 100644 --- a/src/cmd/compile/internal/gc/reflect.go +++ b/src/cmd/compile/internal/gc/reflect.go @@ -674,7 +674,7 @@ func haspointers(t *Type) bool { ret = false case TARRAY: - if t.Bound < 0 { // slice + if t.IsSlice() { ret = true break } @@ -835,7 +835,7 @@ func dcommontype(s *Sym, ot int, t *Type) int { ot = duint8(s, ot, t.Align) // fieldAlign i = kinds[t.Etype] - if t.Etype == TARRAY && t.Bound < 0 { + if t.IsSlice() { i = obj.KindSlice } if !haspointers(t) { @@ -1114,7 +1114,7 @@ ok: ot = dextratype(s, ot, t, 0) case TARRAY: - if t.Bound >= 0 { + if t.IsArray() { // ../../../../runtime/type.go:/arrayType s1 := dtypesym(t.Type) t2 := typSlice(t.Type) diff --git a/src/cmd/compile/internal/gc/sinit.go b/src/cmd/compile/internal/gc/sinit.go index 9782673892e..06776121532 100644 --- a/src/cmd/compile/internal/gc/sinit.go +++ b/src/cmd/compile/internal/gc/sinit.go @@ -540,7 +540,7 @@ func getdyn(n *Node, top int) initGenType { return initDynamic case OARRAYLIT: - if top == 0 && n.Type.Bound < 0 { + if top == 0 && n.Type.IsSlice() { return initDynamic } @@ -568,7 +568,7 @@ func structlit(ctxt int, pass int, n *Node, var_ *Node, init *Nodes) { switch value.Op { case OARRAYLIT: - if value.Type.Bound < 0 { + if value.Type.IsSlice() { if pass == 1 && ctxt != 0 { a := NodSym(ODOT, var_, index.Sym) slicelit(ctxt, value, a, init) @@ -630,7 +630,7 @@ func arraylit(ctxt int, pass int, n *Node, var_ *Node, init *Nodes) { switch value.Op { case OARRAYLIT: - if value.Type.Bound < 0 { + if value.Type.IsSlice() { if pass == 1 && ctxt != 0 { a := Nod(OINDEX, var_, index) slicelit(ctxt, value, a, init) @@ -804,7 +804,7 @@ func slicelit(ctxt int, n *Node, var_ *Node, init *Nodes) { switch value.Op { case OARRAYLIT: - if value.Type.Bound < 0 { + if value.Type.IsSlice() { break } arraylit(ctxt, 2, value, a, init) @@ -1076,7 +1076,7 @@ func anylit(ctxt int, n *Node, var_ *Node, init *Nodes) { if t.Etype != TARRAY { Fatalf("anylit: not array") } - if t.Bound < 0 { + if t.IsSlice() { slicelit(ctxt, n, var_, init) break } @@ -1195,7 +1195,7 @@ func stataddr(nam *Node, n *Node) bool { return true case OINDEX: - if n.Left.Type.Bound < 0 { + if n.Left.Type.IsSlice() { break } if !stataddr(nam, n.Left) { @@ -1384,7 +1384,7 @@ func genAsInitNoCheck(n *Node, reportOnly bool) bool { } // nr is the array being converted to a slice - if nr.Type == nil || nr.Type.Etype != TARRAY || nr.Type.Bound < 0 { + if nr.Type == nil || nr.Type.Etype != TARRAY || nr.Type.IsSlice() { return false } diff --git a/src/cmd/compile/internal/gc/subr.go b/src/cmd/compile/internal/gc/subr.go index d4f2f83a7ff..96fe2196866 100644 --- a/src/cmd/compile/internal/gc/subr.go +++ b/src/cmd/compile/internal/gc/subr.go @@ -583,11 +583,11 @@ func Istype(t *Type, et EType) bool { } func Isfixedarray(t *Type) bool { - return t != nil && t.Etype == TARRAY && t.Bound >= 0 + return t != nil && t.IsArray() } func Isslice(t *Type) bool { - return t != nil && t.Etype == TARRAY && t.Bound < 0 + return t != nil && t.IsSlice() } func isblank(n *Node) bool { diff --git a/src/cmd/compile/internal/gc/type.go b/src/cmd/compile/internal/gc/type.go index 26bcd8f1cb8..6f40b00bc09 100644 --- a/src/cmd/compile/internal/gc/type.go +++ b/src/cmd/compile/internal/gc/type.go @@ -789,6 +789,7 @@ func (t *Type) IsChan() bool { } func (t *Type) IsSlice() bool { + // TODO(josharian): Change this to t.Bound == -1. return t.Etype == TARRAY && t.Bound < 0 } diff --git a/src/cmd/compile/internal/gc/typecheck.go b/src/cmd/compile/internal/gc/typecheck.go index f6f13c485c0..ffd885671e8 100644 --- a/src/cmd/compile/internal/gc/typecheck.go +++ b/src/cmd/compile/internal/gc/typecheck.go @@ -375,7 +375,7 @@ OpSwitch: Yyerror("array bound is too large") n.Type = nil return n - } else if t.Bound < 0 { + } else if t.IsSlice() { Yyerror("array bound must be non-negative") n.Type = nil return n @@ -1412,7 +1412,7 @@ OpSwitch: } case TARRAY: - if t.Bound < 0 { // slice + if t.IsSlice() { break } if callrecv(l) { // has call or receive @@ -2974,7 +2974,7 @@ func typecheckcomplit(n *Node) *Node { i++ if int64(i) > length { length = int64(i) - if t.Bound >= 0 && length > t.Bound { + if t.IsArray() && length > t.Bound { setlineno(l) Yyerror("array index %d out of bounds [0:%d]", length-1, t.Bound) t.Bound = -1 // no more errors @@ -2991,7 +2991,7 @@ func typecheckcomplit(n *Node) *Node { if t.isDDDArray() { t.Bound = length } - if t.Bound < 0 { + if t.IsSlice() { n.Right = Nodintconst(length) } n.Op = OARRAYLIT