]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: encapsulate and document two types.Type internal fields
authorRobert Griesemer <gri@golang.org>
Sat, 3 Nov 2018 04:41:41 +0000 (21:41 -0700)
committerRobert Griesemer <gri@golang.org>
Mon, 5 Nov 2018 20:30:14 +0000 (20:30 +0000)
Change-Id: I5f7d2155c2c3a47dabdf16fe46b122ede81de4fc
Reviewed-on: https://go-review.googlesource.com/c/147284
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/gc/reflect.go
src/cmd/compile/internal/gc/typecheck.go
src/cmd/compile/internal/types/type.go

index 415d3cd5948b77fcd98209cd3d4e99cfbec65057..50b741358f41e167c6977b6049675f20dbdd5f58 100644 (file)
@@ -812,7 +812,7 @@ func dcommontype(lsym *obj.LSym, t *types.Type) int {
 
        sptrWeak := true
        var sptr *obj.LSym
-       if !t.IsPtr() || t.PtrBase != nil {
+       if !t.IsPtr() || t.IsPtrElem() {
                tptr := types.NewPtr(t)
                if t.Sym != nil || methods(tptr) != nil {
                        sptrWeak = false
index be11a9841f0bf6cb753f7c7234dafa6f2eb98b97..2a595214842d1737229b1f60ac1013cb0f8f4ab8 100644 (file)
@@ -3671,8 +3671,7 @@ func copytype(n *Node, t *types.Type) {
        embedlineno := n.Type.ForwardType().Embedlineno
        l := n.Type.ForwardType().Copyto
 
-       ptrBase := n.Type.PtrBase
-       sliceOf := n.Type.SliceOf
+       cache := n.Type.Cache
 
        // TODO(mdempsky): Fix Type rekinding.
        *n.Type = *t
@@ -3693,8 +3692,7 @@ func copytype(n *Node, t *types.Type) {
 
        t.Nod = asTypesNode(n)
        t.SetDeferwidth(false)
-       t.PtrBase = ptrBase
-       t.SliceOf = sliceOf
+       t.Cache = cache
 
        // Propagate go:notinheap pragma from the Name to the Type.
        if n.Name != nil && n.Name.Param != nil && n.Name.Param.Pragma&NotInHeap != 0 {
index 39f4d2aa7b06bb12989bef9f95785b41a927953d..b20039239b9cfb816f5a7564ea453869568c79b3 100644 (file)
@@ -149,8 +149,11 @@ type Type struct {
        Nod  *Node // canonical OTYPE node
        Orig *Type // original type (type literal or predefined type)
 
-       SliceOf *Type
-       PtrBase *Type
+       // Cache of composite types, with this type being the element type.
+       Cache struct {
+               ptr   *Type // *T, or nil
+               slice *Type // []T, or nil
+       }
 
        Sym    *Sym  // symbol containing name, for named types
        Vargen int32 // unique name for OTYPE/ONAME
@@ -488,7 +491,7 @@ func NewArray(elem *Type, bound int64) *Type {
 
 // NewSlice returns the slice Type with element type elem.
 func NewSlice(elem *Type) *Type {
-       if t := elem.SliceOf; t != nil {
+       if t := elem.Cache.slice; t != nil {
                if t.Elem() != elem {
                        Fatalf("elem mismatch")
                }
@@ -497,7 +500,7 @@ func NewSlice(elem *Type) *Type {
 
        t := New(TSLICE)
        t.Extra = Slice{Elem: elem}
-       elem.SliceOf = t
+       elem.Cache.slice = t
        return t
 }
 
@@ -551,7 +554,7 @@ func NewPtr(elem *Type) *Type {
                Fatalf("NewPtr: pointer to elem Type is nil")
        }
 
-       if t := elem.PtrBase; t != nil {
+       if t := elem.Cache.ptr; t != nil {
                if t.Elem() != elem {
                        Fatalf("NewPtr: elem mismatch")
                }
@@ -563,7 +566,7 @@ func NewPtr(elem *Type) *Type {
        t.Width = int64(Widthptr)
        t.Align = uint8(Widthptr)
        if NewPtrCacheEnabled {
-               elem.PtrBase = t
+               elem.Cache.ptr = t
        }
        return t
 }
@@ -1258,6 +1261,11 @@ func (t *Type) IsPtr() bool {
        return t.Etype == TPTR
 }
 
+// IsPtrElem reports whether t is the element of a pointer (to t).
+func (t *Type) IsPtrElem() bool {
+       return t.Cache.ptr != nil
+}
+
 // IsUnsafePtr reports whether t is an unsafe pointer.
 func (t *Type) IsUnsafePtr() bool {
        return t.Etype == TUNSAFEPTR