Commit 5dd129bc authored by Josh Bleecher Snyder's avatar Josh Bleecher Snyder

cmd/compile: add Type.SetNumElem

This removes all access to Type.Bound
from outside type.go.

Update sinit to make a new type rather than
copy and mutate.

Update bimport to create a new slice type
instead of mutating TDDDFIELD.
These are rare, so the extra allocs are nominal.

I’m not happy about having a setter,
but it appears the most practical route
forward at the moment, and it only has a few uses.

Passes toolstash -cmp.

Change-Id: I174f07c8f336afc656904bde4bdbde4f3ef0db96
Reviewed-on: https://go-review.googlesource.com/21423
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
parent e504055e
...@@ -277,15 +277,15 @@ func (p *importer) typ() *Type { ...@@ -277,15 +277,15 @@ func (p *importer) typ() *Type {
case arrayTag, sliceTag: case arrayTag, sliceTag:
t = p.newtyp(TARRAY) t = p.newtyp(TARRAY)
t.Bound = sliceBound
if i == arrayTag { if i == arrayTag {
t.Bound = p.int64() t.SetNumElem(p.int64())
} else {
t.SetNumElem(sliceBound)
} }
t.Type = p.typ() t.Type = p.typ()
case dddTag: case dddTag:
t = p.newtyp(TDDDFIELD) t = p.newtyp(TDDDFIELD)
t.Bound = sliceBound
t.Type = p.typ() t.Type = p.typ()
case structTag: case structTag:
...@@ -448,9 +448,8 @@ func (p *importer) param(named bool) *Node { ...@@ -448,9 +448,8 @@ func (p *importer) param(named bool) *Node {
isddd := false isddd := false
if typ.Etype == TDDDFIELD { if typ.Etype == TDDDFIELD {
// TDDDFIELD indicates ... type // TDDDFIELD indicates wrapped ... slice type
// TODO(mdempsky): Fix Type rekinding. typ = typSlice(typ.Wrapped())
typ.Etype = TARRAY
isddd = true isddd = true
} }
......
...@@ -688,12 +688,8 @@ func arraylit(ctxt int, pass int, n *Node, var_ *Node, init *Nodes) { ...@@ -688,12 +688,8 @@ func arraylit(ctxt int, pass int, n *Node, var_ *Node, init *Nodes) {
} }
func slicelit(ctxt int, n *Node, var_ *Node, init *Nodes) { func slicelit(ctxt int, n *Node, var_ *Node, init *Nodes) {
// make an array type // make an array type corresponding the number of elements we have
t := n.Type.Copy() t := typArray(n.Type.Elem(), n.Right.Int())
t.Bound = n.Right.Int()
t.Width = 0
t.Sym = nil
t.Haspointers = 0
dowidth(t) dowidth(t)
if ctxt != 0 { if ctxt != 0 {
......
...@@ -936,6 +936,15 @@ func (t *Type) NumElem() int64 { ...@@ -936,6 +936,15 @@ func (t *Type) NumElem() int64 {
return t.Bound return t.Bound
} }
// SetNumElem sets the number of elements in an array type.
// It should not be used if at all possible.
// Create a new array/slice/dddArray with typX instead.
// TODO(josharian): figure out how to get rid of this.
func (t *Type) SetNumElem(n int64) {
t.wantEtype(TARRAY)
t.Bound = n
}
func (t *Type) IsMemory() bool { return false } func (t *Type) IsMemory() bool { return false }
func (t *Type) IsFlags() bool { return false } func (t *Type) IsFlags() bool { return false }
func (t *Type) IsVoid() bool { return false } func (t *Type) IsVoid() bool { return false }
...@@ -2977,7 +2977,7 @@ func typecheckcomplit(n *Node) *Node { ...@@ -2977,7 +2977,7 @@ func typecheckcomplit(n *Node) *Node {
setlineno(l) setlineno(l)
Yyerror("array index %d out of bounds [0:%d]", length-1, t.NumElem()) Yyerror("array index %d out of bounds [0:%d]", length-1, t.NumElem())
// suppress any further errors out of bounds errors for the same type by pretending it is a slice // suppress any further errors out of bounds errors for the same type by pretending it is a slice
t.Bound = sliceBound t.SetNumElem(sliceBound)
} }
} }
...@@ -2989,7 +2989,7 @@ func typecheckcomplit(n *Node) *Node { ...@@ -2989,7 +2989,7 @@ func typecheckcomplit(n *Node) *Node {
} }
if t.isDDDArray() { if t.isDDDArray() {
t.Bound = length t.SetNumElem(length)
} }
if t.IsSlice() { if t.IsSlice() {
n.Right = Nodintconst(length) n.Right = Nodintconst(length)
......
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