1
0
mirror of https://github.com/golang/go synced 2024-10-01 12:38:31 -06:00

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 <bradfitz@golang.org>
This commit is contained in:
Josh Bleecher Snyder 2016-03-29 09:14:19 -07:00
parent d7f7ea6ea8
commit 331f962508
8 changed files with 20 additions and 19 deletions

View File

@ -240,7 +240,7 @@ func dowidth(t *Type) {
if t.Type == nil { if t.Type == nil {
break break
} }
if t.Bound >= 0 { if t.IsArray() {
dowidth(t.Type) dowidth(t.Type)
if t.Type.Width != 0 { if t.Type.Width != 0 {
cap := (uint64(Thearch.MAXWIDTH) - 1) / uint64(t.Type.Width) cap := (uint64(Thearch.MAXWIDTH) - 1) / uint64(t.Type.Width)

View File

@ -506,7 +506,7 @@ func (p *exporter) typ(t *Type) {
if t.isDDDArray() { if t.isDDDArray() {
Fatalf("array bounds should be known at export time: %v", t) Fatalf("array bounds should be known at export time: %v", t)
} }
if t.Bound >= 0 { if t.IsArray() {
p.tag(arrayTag) p.tag(arrayTag)
p.int64(t.Bound) p.int64(t.Bound)
} else { } else {

View File

@ -586,7 +586,7 @@ func typefmt(t *Type, flag FmtFlag) string {
return "*" + t.Type.String() return "*" + t.Type.String()
case TARRAY: case TARRAY:
if t.Bound >= 0 { if t.IsArray() {
return fmt.Sprintf("[%d]%v", t.Bound, t.Type) return fmt.Sprintf("[%d]%v", t.Bound, t.Type)
} }
if t.isDDDArray() { if t.isDDDArray() {

View File

@ -674,7 +674,7 @@ func haspointers(t *Type) bool {
ret = false ret = false
case TARRAY: case TARRAY:
if t.Bound < 0 { // slice if t.IsSlice() {
ret = true ret = true
break break
} }
@ -835,7 +835,7 @@ func dcommontype(s *Sym, ot int, t *Type) int {
ot = duint8(s, ot, t.Align) // fieldAlign ot = duint8(s, ot, t.Align) // fieldAlign
i = kinds[t.Etype] i = kinds[t.Etype]
if t.Etype == TARRAY && t.Bound < 0 { if t.IsSlice() {
i = obj.KindSlice i = obj.KindSlice
} }
if !haspointers(t) { if !haspointers(t) {
@ -1114,7 +1114,7 @@ ok:
ot = dextratype(s, ot, t, 0) ot = dextratype(s, ot, t, 0)
case TARRAY: case TARRAY:
if t.Bound >= 0 { if t.IsArray() {
// ../../../../runtime/type.go:/arrayType // ../../../../runtime/type.go:/arrayType
s1 := dtypesym(t.Type) s1 := dtypesym(t.Type)
t2 := typSlice(t.Type) t2 := typSlice(t.Type)

View File

@ -540,7 +540,7 @@ func getdyn(n *Node, top int) initGenType {
return initDynamic return initDynamic
case OARRAYLIT: case OARRAYLIT:
if top == 0 && n.Type.Bound < 0 { if top == 0 && n.Type.IsSlice() {
return initDynamic return initDynamic
} }
@ -568,7 +568,7 @@ func structlit(ctxt int, pass int, n *Node, var_ *Node, init *Nodes) {
switch value.Op { switch value.Op {
case OARRAYLIT: case OARRAYLIT:
if value.Type.Bound < 0 { if value.Type.IsSlice() {
if pass == 1 && ctxt != 0 { if pass == 1 && ctxt != 0 {
a := NodSym(ODOT, var_, index.Sym) a := NodSym(ODOT, var_, index.Sym)
slicelit(ctxt, value, a, init) slicelit(ctxt, value, a, init)
@ -630,7 +630,7 @@ func arraylit(ctxt int, pass int, n *Node, var_ *Node, init *Nodes) {
switch value.Op { switch value.Op {
case OARRAYLIT: case OARRAYLIT:
if value.Type.Bound < 0 { if value.Type.IsSlice() {
if pass == 1 && ctxt != 0 { if pass == 1 && ctxt != 0 {
a := Nod(OINDEX, var_, index) a := Nod(OINDEX, var_, index)
slicelit(ctxt, value, a, init) slicelit(ctxt, value, a, init)
@ -804,7 +804,7 @@ func slicelit(ctxt int, n *Node, var_ *Node, init *Nodes) {
switch value.Op { switch value.Op {
case OARRAYLIT: case OARRAYLIT:
if value.Type.Bound < 0 { if value.Type.IsSlice() {
break break
} }
arraylit(ctxt, 2, value, a, init) arraylit(ctxt, 2, value, a, init)
@ -1076,7 +1076,7 @@ func anylit(ctxt int, n *Node, var_ *Node, init *Nodes) {
if t.Etype != TARRAY { if t.Etype != TARRAY {
Fatalf("anylit: not array") Fatalf("anylit: not array")
} }
if t.Bound < 0 { if t.IsSlice() {
slicelit(ctxt, n, var_, init) slicelit(ctxt, n, var_, init)
break break
} }
@ -1195,7 +1195,7 @@ func stataddr(nam *Node, n *Node) bool {
return true return true
case OINDEX: case OINDEX:
if n.Left.Type.Bound < 0 { if n.Left.Type.IsSlice() {
break break
} }
if !stataddr(nam, n.Left) { 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 // 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 return false
} }

View File

@ -583,11 +583,11 @@ func Istype(t *Type, et EType) bool {
} }
func Isfixedarray(t *Type) 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 { func Isslice(t *Type) bool {
return t != nil && t.Etype == TARRAY && t.Bound < 0 return t != nil && t.IsSlice()
} }
func isblank(n *Node) bool { func isblank(n *Node) bool {

View File

@ -789,6 +789,7 @@ func (t *Type) IsChan() bool {
} }
func (t *Type) IsSlice() bool { func (t *Type) IsSlice() bool {
// TODO(josharian): Change this to t.Bound == -1.
return t.Etype == TARRAY && t.Bound < 0 return t.Etype == TARRAY && t.Bound < 0
} }

View File

@ -375,7 +375,7 @@ OpSwitch:
Yyerror("array bound is too large") Yyerror("array bound is too large")
n.Type = nil n.Type = nil
return n return n
} else if t.Bound < 0 { } else if t.IsSlice() {
Yyerror("array bound must be non-negative") Yyerror("array bound must be non-negative")
n.Type = nil n.Type = nil
return n return n
@ -1412,7 +1412,7 @@ OpSwitch:
} }
case TARRAY: case TARRAY:
if t.Bound < 0 { // slice if t.IsSlice() {
break break
} }
if callrecv(l) { // has call or receive if callrecv(l) { // has call or receive
@ -2974,7 +2974,7 @@ func typecheckcomplit(n *Node) *Node {
i++ i++
if int64(i) > length { if int64(i) > length {
length = int64(i) length = int64(i)
if t.Bound >= 0 && length > t.Bound { if t.IsArray() && length > t.Bound {
setlineno(l) setlineno(l)
Yyerror("array index %d out of bounds [0:%d]", length-1, t.Bound) Yyerror("array index %d out of bounds [0:%d]", length-1, t.Bound)
t.Bound = -1 // no more errors t.Bound = -1 // no more errors
@ -2991,7 +2991,7 @@ func typecheckcomplit(n *Node) *Node {
if t.isDDDArray() { if t.isDDDArray() {
t.Bound = length t.Bound = length
} }
if t.Bound < 0 { if t.IsSlice() {
n.Right = Nodintconst(length) n.Right = Nodintconst(length)
} }
n.Op = OARRAYLIT n.Op = OARRAYLIT