Commit 3377b467 authored by Robert Griesemer's avatar Robert Griesemer

cmd/compile: encapsulate and document two types.Type internal fields

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