Skip to content

Commit 2083b5d

Browse files
committed
cmd/compile/internal/types: replace Type.Val with Type.Elem
This reduces the API surface of Type slightly (for #25056), but also makes it more consistent with the reflect and go/types APIs. Passes toolstash-check. Change-Id: Ief9a8eb461ae6e88895f347e2a1b7b8a62423222 Reviewed-on: https://go-review.googlesource.com/109138 Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
1 parent e10ee79 commit 2083b5d

File tree

12 files changed

+54
-63
lines changed

12 files changed

+54
-63
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ func dowidth(t *types.Type) {
286286

287287
case TMAP: // implemented as pointer
288288
w = int64(Widthptr)
289-
checkwidth(t.Val())
289+
checkwidth(t.Elem())
290290
checkwidth(t.Key())
291291

292292
case TFORW: // should have been filled in

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -476,12 +476,9 @@ func (p *exporter) markType(t *types.Type) {
476476
// perfect. Worst case, we might miss opportunities to inline
477477
// some function calls in downstream packages.
478478
switch t.Etype {
479-
case TPTR32, TPTR64, TARRAY, TSLICE, TCHAN:
479+
case TPTR32, TPTR64, TARRAY, TSLICE, TCHAN, TMAP:
480480
p.markType(t.Elem())
481481

482-
case TMAP:
483-
p.markType(t.Val())
484-
485482
case TSTRUCT:
486483
for _, f := range t.FieldSlice() {
487484
if types.IsExported(f.Sym.Name) || f.Embedded != 0 {
@@ -798,7 +795,7 @@ func (p *exporter) typ(t *types.Type) {
798795
case TMAP:
799796
p.tag(mapTag)
800797
p.typ(t.Key())
801-
p.typ(t.Val())
798+
p.typ(t.Elem())
802799

803800
case TCHAN:
804801
p.tag(chanTag)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ func (p *importer) typ() *types.Type {
587587
t = p.newtyp(TMAP)
588588
mt := t.MapType()
589589
mt.Key = p.typ()
590-
mt.Val = p.typ()
590+
mt.Elem = p.typ()
591591

592592
case chanTag:
593593
t = p.newtyp(TCHAN)

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ func typefmt(t *types.Type, flag FmtFlag, mode fmtMode, depth int) string {
739739
return "chan " + tmodeString(t.Elem(), mode, depth)
740740

741741
case TMAP:
742-
return "map[" + tmodeString(t.Key(), mode, depth) + "]" + tmodeString(t.Val(), mode, depth)
742+
return "map[" + tmodeString(t.Key(), mode, depth) + "]" + tmodeString(t.Elem(), mode, depth)
743743

744744
case TINTER:
745745
if t.IsEmptyInterface() {
@@ -803,19 +803,18 @@ func typefmt(t *types.Type, flag FmtFlag, mode fmtMode, depth int) string {
803803
mt := m.MapType()
804804
// Format the bucket struct for map[x]y as map.bucket[x]y.
805805
// This avoids a recursive print that generates very long names.
806-
if mt.Bucket == t {
807-
return "map.bucket[" + tmodeString(m.Key(), mode, depth) + "]" + tmodeString(m.Val(), mode, depth)
808-
}
809-
810-
if mt.Hmap == t {
811-
return "map.hdr[" + tmodeString(m.Key(), mode, depth) + "]" + tmodeString(m.Val(), mode, depth)
812-
}
813-
814-
if mt.Hiter == t {
815-
return "map.iter[" + tmodeString(m.Key(), mode, depth) + "]" + tmodeString(m.Val(), mode, depth)
806+
var subtype string
807+
switch t {
808+
case mt.Bucket:
809+
subtype = "bucket"
810+
case mt.Hmap:
811+
subtype = "hdr"
812+
case mt.Hiter:
813+
subtype = "iter"
814+
default:
815+
Fatalf("unknown internal map type")
816816
}
817-
818-
Fatalf("unknown internal map type")
817+
return fmt.Sprintf("map.%s[%s]%s", subtype, tmodeString(m.Key(), mode, depth), tmodeString(m.Elem(), mode, depth))
819818
}
820819

821820
buf := make([]byte, 0, 64)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ func (w *exportWriter) doTyp(t *types.Type) {
638638
case TMAP:
639639
w.startType(mapType)
640640
w.typ(t.Key())
641-
w.typ(t.Val())
641+
w.typ(t.Elem())
642642

643643
case TFUNC:
644644
w.startType(signatureType)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func typecheckrangeExpr(n *Node) {
7171

7272
case TMAP:
7373
t1 = t.Key()
74-
t2 = t.Val()
74+
t2 = t.Elem()
7575

7676
case TCHAN:
7777
if !t.ChanDir().CanRecv() {
@@ -297,7 +297,7 @@ func walkrange(n *Node) *Node {
297297

298298
fn := syslook("mapiterinit")
299299

300-
fn = substArgTypes(fn, t.Key(), t.Val(), th)
300+
fn = substArgTypes(fn, t.Key(), t.Elem(), th)
301301
init = append(init, mkcall1(fn, nil, nil, typename(t), ha, nod(OADDR, hit, nil)))
302302
n.Left = nod(ONE, nodSym(ODOT, hit, keysym), nodnil())
303303

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func bmap(t *types.Type) *types.Type {
8585

8686
bucket := types.New(TSTRUCT)
8787
keytype := t.Key()
88-
valtype := t.Val()
88+
valtype := t.Elem()
8989
dowidth(keytype)
9090
dowidth(valtype)
9191
if keytype.Width > MAXKEYSIZE {
@@ -172,7 +172,7 @@ func bmap(t *types.Type) *types.Type {
172172
if t.Key().Width > MAXKEYSIZE && !keytype.IsPtr() {
173173
Fatalf("key indirect incorrect for %v", t)
174174
}
175-
if t.Val().Width > MAXVALSIZE && !valtype.IsPtr() {
175+
if t.Elem().Width > MAXVALSIZE && !valtype.IsPtr() {
176176
Fatalf("value indirect incorrect for %v", t)
177177
}
178178
if keytype.Width%int64(keytype.Align) != 0 {
@@ -286,8 +286,8 @@ func hiter(t *types.Type) *types.Type {
286286
// }
287287
// must match ../../../../runtime/map.go:hiter.
288288
fields := []*types.Field{
289-
makefield("key", types.NewPtr(t.Key())), // Used in range.go for TMAP.
290-
makefield("val", types.NewPtr(t.Val())), // Used in range.go for TMAP.
289+
makefield("key", types.NewPtr(t.Key())), // Used in range.go for TMAP.
290+
makefield("val", types.NewPtr(t.Elem())), // Used in range.go for TMAP.
291291
makefield("t", types.Types[TUNSAFEPTR]),
292292
makefield("h", types.NewPtr(hmap)),
293293
makefield("buckets", types.NewPtr(bmap)),
@@ -1245,7 +1245,7 @@ func dtypesym(t *types.Type) *obj.LSym {
12451245
// ../../../../runtime/type.go:/mapType
12461246
case TMAP:
12471247
s1 := dtypesym(t.Key())
1248-
s2 := dtypesym(t.Val())
1248+
s2 := dtypesym(t.Elem())
12491249
s3 := dtypesym(bmap(t))
12501250
s4 := dtypesym(hmap(t))
12511251
ot = dcommontype(lsym, t)
@@ -1261,11 +1261,11 @@ func dtypesym(t *types.Type) *obj.LSym {
12611261
ot = duint8(lsym, ot, 0) // not indirect
12621262
}
12631263

1264-
if t.Val().Width > MAXVALSIZE {
1264+
if t.Elem().Width > MAXVALSIZE {
12651265
ot = duint8(lsym, ot, uint8(Widthptr))
12661266
ot = duint8(lsym, ot, 1) // indirect
12671267
} else {
1268-
ot = duint8(lsym, ot, uint8(t.Val().Width))
1268+
ot = duint8(lsym, ot, uint8(t.Elem().Width))
12691269
ot = duint8(lsym, ot, 0) // not indirect
12701270
}
12711271

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ func maplit(n *Node, m *Node, init *Nodes) {
947947

948948
// build types [count]Tindex and [count]Tvalue
949949
tk := types.NewArray(n.Type.Key(), int64(len(stat)))
950-
tv := types.NewArray(n.Type.Val(), int64(len(stat)))
950+
tv := types.NewArray(n.Type.Elem(), int64(len(stat)))
951951

952952
// TODO(josharian): suppress alg generation for these types?
953953
dowidth(tk)
@@ -1012,7 +1012,7 @@ func addMapEntries(m *Node, dyn []*Node, init *Nodes) {
10121012
// Use temporaries so that mapassign1 can have addressable key, val.
10131013
// TODO(josharian): avoid map key temporaries for mapfast_* assignments with literal keys.
10141014
key := temp(m.Type.Key())
1015-
val := temp(m.Type.Val())
1015+
val := temp(m.Type.Elem())
10161016

10171017
for _, r := range dyn {
10181018
index, value := r.Left, r.Right

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,6 @@ func eqtype1(t1, t2 *types.Type, cmpTags bool, assumedEqual map[typePair]struct{
612612
if !eqtype1(t1.Key(), t2.Key(), cmpTags, assumedEqual) {
613613
return false
614614
}
615-
return eqtype1(t1.Val(), t2.Val(), cmpTags, assumedEqual)
616615
}
617616

618617
return eqtype1(t1.Elem(), t2.Elem(), cmpTags, assumedEqual)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,7 @@ func typecheck1(n *Node, top int) *Node {
10191019
if n.Right.Type != nil {
10201020
n.Right = assignconv(n.Right, t.Key(), "map index")
10211021
}
1022-
n.Type = t.Val()
1022+
n.Type = t.Elem()
10231023
n.Op = OINDEXMAP
10241024
n.ResetAux()
10251025
}
@@ -3012,10 +3012,10 @@ func typecheckcomplit(n *Node) *Node {
30123012
}
30133013

30143014
r = l.Right
3015-
pushtype(r, t.Val())
3015+
pushtype(r, t.Elem())
30163016
r = typecheck(r, Erv)
3017-
r = defaultlit(r, t.Val())
3018-
l.Right = assignconv(r, t.Val(), "map value")
3017+
r = defaultlit(r, t.Elem())
3018+
l.Right = assignconv(r, t.Elem(), "map value")
30193019
}
30203020

30213021
n.Op = OMAPLIT

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ opswitch:
822822
// a = *var
823823
a := n.List.First()
824824

825-
if w := t.Val().Width; w <= 1024 { // 1024 must match ../../../../runtime/map.go:maxZero
825+
if w := t.Elem().Width; w <= 1024 { // 1024 must match ../../../../runtime/map.go:maxZero
826826
fn := mapfn(mapaccess2[fast], t)
827827
r = mkcall1(fn, fn.Type.Results(), init, typename(t), r.Left, key)
828828
} else {
@@ -842,7 +842,7 @@ opswitch:
842842

843843
// don't generate a = *var if a is _
844844
if !a.isBlank() {
845-
var_ := temp(types.NewPtr(t.Val()))
845+
var_ := temp(types.NewPtr(t.Elem()))
846846
var_.SetTypecheck(1)
847847
var_.SetNonNil(true) // mapaccess always returns a non-nil pointer
848848
n.List.SetFirst(var_)
@@ -1196,17 +1196,17 @@ opswitch:
11961196
key = nod(OADDR, key, nil)
11971197
}
11981198

1199-
if w := t.Val().Width; w <= 1024 { // 1024 must match ../../../../runtime/map.go:maxZero
1200-
n = mkcall1(mapfn(mapaccess1[fast], t), types.NewPtr(t.Val()), init, typename(t), map_, key)
1199+
if w := t.Elem().Width; w <= 1024 { // 1024 must match ../../../../runtime/map.go:maxZero
1200+
n = mkcall1(mapfn(mapaccess1[fast], t), types.NewPtr(t.Elem()), init, typename(t), map_, key)
12011201
} else {
12021202
z := zeroaddr(w)
1203-
n = mkcall1(mapfn("mapaccess1_fat", t), types.NewPtr(t.Val()), init, typename(t), map_, key, z)
1203+
n = mkcall1(mapfn("mapaccess1_fat", t), types.NewPtr(t.Elem()), init, typename(t), map_, key, z)
12041204
}
12051205
}
1206-
n.Type = types.NewPtr(t.Val())
1206+
n.Type = types.NewPtr(t.Elem())
12071207
n.SetNonNil(true) // mapaccess1* and mapassign always return non-nil pointers.
12081208
n = nod(OIND, n, nil)
1209-
n.Type = t.Val()
1209+
n.Type = t.Elem()
12101210
n.SetTypecheck(1)
12111211

12121212
case ORECV:
@@ -1498,7 +1498,7 @@ opswitch:
14981498
// Call runtime.makehmap to allocate an
14991499
// hmap on the heap and initialize hmap's hash0 field.
15001500
fn := syslook("makemap_small")
1501-
fn = substArgTypes(fn, t.Key(), t.Val())
1501+
fn = substArgTypes(fn, t.Key(), t.Elem())
15021502
n = mkcall1(fn, n.Type, init)
15031503
}
15041504
} else {
@@ -1525,7 +1525,7 @@ opswitch:
15251525
}
15261526

15271527
fn := syslook(fnname)
1528-
fn = substArgTypes(fn, hmapType, t.Key(), t.Val())
1528+
fn = substArgTypes(fn, hmapType, t.Key(), t.Elem())
15291529
n = mkcall1(fn, n.Type, init, typename(n.Type), conv(hint, argtype), h)
15301530
}
15311531

@@ -2792,7 +2792,7 @@ func mapfn(name string, t *types.Type) *Node {
27922792
Fatalf("mapfn %v", t)
27932793
}
27942794
fn := syslook(name)
2795-
fn = substArgTypes(fn, t.Key(), t.Val(), t.Key(), t.Val())
2795+
fn = substArgTypes(fn, t.Key(), t.Elem(), t.Key(), t.Elem())
27962796
return fn
27972797
}
27982798

@@ -2801,7 +2801,7 @@ func mapfndel(name string, t *types.Type) *Node {
28012801
Fatalf("mapfn %v", t)
28022802
}
28032803
fn := syslook(name)
2804-
fn = substArgTypes(fn, t.Key(), t.Val(), t.Key())
2804+
fn = substArgTypes(fn, t.Key(), t.Elem(), t.Key())
28052805
return fn
28062806
}
28072807

@@ -2828,7 +2828,7 @@ var mapdelete = mkmapnames("mapdelete", "")
28282828

28292829
func mapfast(t *types.Type) int {
28302830
// Check ../../runtime/map.go:maxValueSize before changing.
2831-
if t.Val().Width > 128 {
2831+
if t.Elem().Width > 128 {
28322832
return mapslow
28332833
}
28342834
switch algtype(t.Key()) {

src/cmd/compile/internal/types/type.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ func (t *Type) SetPkg(pkg *Pkg) {
219219

220220
// Map contains Type fields specific to maps.
221221
type Map struct {
222-
Key *Type // Key type
223-
Val *Type // Val (elem) type
222+
Key *Type // Key type
223+
Elem *Type // Val (elem) type
224224

225225
Bucket *Type // internal struct type representing a hash bucket
226226
Hmap *Type // internal struct type representing the Hmap (map header object)
@@ -539,7 +539,7 @@ func NewMap(k, v *Type) *Type {
539539
t := New(TMAP)
540540
mt := t.MapType()
541541
mt.Key = k
542-
mt.Val = v
542+
mt.Elem = v
543543
return t
544544
}
545545

@@ -650,11 +650,11 @@ func SubstAny(t *Type, types *[]*Type) *Type {
650650

651651
case TMAP:
652652
key := SubstAny(t.Key(), types)
653-
val := SubstAny(t.Val(), types)
654-
if key != t.Key() || val != t.Val() {
653+
elem := SubstAny(t.Elem(), types)
654+
if key != t.Key() || elem != t.Elem() {
655655
t = t.copy()
656656
t.Extra.(*Map).Key = key
657-
t.Extra.(*Map).Val = val
657+
t.Extra.(*Map).Elem = elem
658658
}
659659

660660
case TFUNC:
@@ -787,14 +787,8 @@ func (t *Type) Key() *Type {
787787
return t.Extra.(*Map).Key
788788
}
789789

790-
// Val returns the value type of map type t.
791-
func (t *Type) Val() *Type {
792-
t.wantEtype(TMAP)
793-
return t.Extra.(*Map).Val
794-
}
795-
796790
// Elem returns the type of elements of t.
797-
// Usable with pointers, channels, arrays, and slices.
791+
// Usable with pointers, channels, arrays, slices, and maps.
798792
func (t *Type) Elem() *Type {
799793
switch t.Etype {
800794
case TPTR32, TPTR64:
@@ -805,6 +799,8 @@ func (t *Type) Elem() *Type {
805799
return t.Extra.(Slice).Elem
806800
case TCHAN:
807801
return t.Extra.(*Chan).Elem
802+
case TMAP:
803+
return t.Extra.(*Map).Elem
808804
}
809805
Fatalf("Type.Elem %s", t.Etype)
810806
return nil
@@ -1104,7 +1100,7 @@ func (t *Type) cmp(x *Type) Cmp {
11041100
if c := t.Key().cmp(x.Key()); c != CMPeq {
11051101
return c
11061102
}
1107-
return t.Val().cmp(x.Val())
1103+
return t.Elem().cmp(x.Elem())
11081104

11091105
case TPTR32, TPTR64, TSLICE:
11101106
// No special cases for these, they are handled

0 commit comments

Comments
 (0)