Skip to content

Commit 3377b46

Browse files
committed
cmd/compile: encapsulate and document two types.Type internal fields
Change-Id: I5f7d2155c2c3a47dabdf16fe46b122ede81de4fc Reviewed-on: https://go-review.googlesource.com/c/147284 Reviewed-by: Matthew Dempsky <[email protected]>
1 parent 2ae8bf7 commit 3377b46

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ func dcommontype(lsym *obj.LSym, t *types.Type) int {
812812

813813
sptrWeak := true
814814
var sptr *obj.LSym
815-
if !t.IsPtr() || t.PtrBase != nil {
815+
if !t.IsPtr() || t.IsPtrElem() {
816816
tptr := types.NewPtr(t)
817817
if t.Sym != nil || methods(tptr) != nil {
818818
sptrWeak = false

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3671,8 +3671,7 @@ func copytype(n *Node, t *types.Type) {
36713671
embedlineno := n.Type.ForwardType().Embedlineno
36723672
l := n.Type.ForwardType().Copyto
36733673

3674-
ptrBase := n.Type.PtrBase
3675-
sliceOf := n.Type.SliceOf
3674+
cache := n.Type.Cache
36763675

36773676
// TODO(mdempsky): Fix Type rekinding.
36783677
*n.Type = *t
@@ -3693,8 +3692,7 @@ func copytype(n *Node, t *types.Type) {
36933692

36943693
t.Nod = asTypesNode(n)
36953694
t.SetDeferwidth(false)
3696-
t.PtrBase = ptrBase
3697-
t.SliceOf = sliceOf
3695+
t.Cache = cache
36983696

36993697
// Propagate go:notinheap pragma from the Name to the Type.
37003698
if n.Name != nil && n.Name.Param != nil && n.Name.Param.Pragma&NotInHeap != 0 {

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,11 @@ type Type struct {
149149
Nod *Node // canonical OTYPE node
150150
Orig *Type // original type (type literal or predefined type)
151151

152-
SliceOf *Type
153-
PtrBase *Type
152+
// Cache of composite types, with this type being the element type.
153+
Cache struct {
154+
ptr *Type // *T, or nil
155+
slice *Type // []T, or nil
156+
}
154157

155158
Sym *Sym // symbol containing name, for named types
156159
Vargen int32 // unique name for OTYPE/ONAME
@@ -488,7 +491,7 @@ func NewArray(elem *Type, bound int64) *Type {
488491

489492
// NewSlice returns the slice Type with element type elem.
490493
func NewSlice(elem *Type) *Type {
491-
if t := elem.SliceOf; t != nil {
494+
if t := elem.Cache.slice; t != nil {
492495
if t.Elem() != elem {
493496
Fatalf("elem mismatch")
494497
}
@@ -497,7 +500,7 @@ func NewSlice(elem *Type) *Type {
497500

498501
t := New(TSLICE)
499502
t.Extra = Slice{Elem: elem}
500-
elem.SliceOf = t
503+
elem.Cache.slice = t
501504
return t
502505
}
503506

@@ -551,7 +554,7 @@ func NewPtr(elem *Type) *Type {
551554
Fatalf("NewPtr: pointer to elem Type is nil")
552555
}
553556

554-
if t := elem.PtrBase; t != nil {
557+
if t := elem.Cache.ptr; t != nil {
555558
if t.Elem() != elem {
556559
Fatalf("NewPtr: elem mismatch")
557560
}
@@ -563,7 +566,7 @@ func NewPtr(elem *Type) *Type {
563566
t.Width = int64(Widthptr)
564567
t.Align = uint8(Widthptr)
565568
if NewPtrCacheEnabled {
566-
elem.PtrBase = t
569+
elem.Cache.ptr = t
567570
}
568571
return t
569572
}
@@ -1258,6 +1261,11 @@ func (t *Type) IsPtr() bool {
12581261
return t.Etype == TPTR
12591262
}
12601263

1264+
// IsPtrElem reports whether t is the element of a pointer (to t).
1265+
func (t *Type) IsPtrElem() bool {
1266+
return t.Cache.ptr != nil
1267+
}
1268+
12611269
// IsUnsafePtr reports whether t is an unsafe pointer.
12621270
func (t *Type) IsUnsafePtr() bool {
12631271
return t.Etype == TUNSAFEPTR

0 commit comments

Comments
 (0)