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 {
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
......
......@@ -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 {
......
......@@ -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
......
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