diff options
author | Matthew Dempsky <mdempsky@google.com> | 2020-12-01 01:42:47 -0800 |
---|---|---|
committer | Matthew Dempsky <mdempsky@google.com> | 2020-12-01 17:16:25 +0000 |
commit | f37aa5e4e26a7212b6300e2021b8e6ea7000979b (patch) | |
tree | 20bff229f4f365898b0f2882f4cc13915df59564 /src/cmd/compile/internal/types/type.go | |
parent | 63a6f08b39b8ccb0dbbd373572a04f1a089f3573 (diff) | |
download | go-f37aa5e4e26a7212b6300e2021b8e6ea7000979b.tar.gz go-f37aa5e4e26a7212b6300e2021b8e6ea7000979b.zip |
[dev.regabi] cmd/compile: add NewNamed
The start of abstracting away Type fields. This adds a new constructor
for named types, styled after go/types.NewNamed. Along with helper
methods for SetNod and Pos, this allows hiding Nod.
Change-Id: Ica107034b6346c7b523bf6ae2a34009e350a9aa8
Reviewed-on: https://go-review.googlesource.com/c/go/+/274434
Trust: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src/cmd/compile/internal/types/type.go')
-rw-r--r-- | src/cmd/compile/internal/types/type.go | 51 |
1 files changed, 45 insertions, 6 deletions
diff --git a/src/cmd/compile/internal/types/type.go b/src/cmd/compile/internal/types/type.go index 2a65b713be..d6d56426a5 100644 --- a/src/cmd/compile/internal/types/type.go +++ b/src/cmd/compile/internal/types/type.go @@ -14,7 +14,11 @@ import ( // IRNode represents an ir.Node, but without needing to import cmd/compile/internal/ir, // which would cause an import cycle. The uses in other packages must type assert // values of type IRNode to ir.Node or a more specific type. -type IRNode interface{ Type() *Type } +type IRNode interface { + Pos() src.XPos + Sym() *Sym + Type() *Type +} //go:generate stringer -type EType -trimprefix T @@ -142,7 +146,7 @@ type Type struct { methods Fields allMethods Fields - Nod IRNode // canonical OTYPE node + nod IRNode // canonical OTYPE node Orig *Type // original type (type literal or predefined type) // Cache of composite types, with this type being the element type. @@ -180,6 +184,24 @@ func (t *Type) SetNoalg(b bool) { t.flags.set(typeNoalg, b) } func (t *Type) SetDeferwidth(b bool) { t.flags.set(typeDeferwidth, b) } func (t *Type) SetRecur(b bool) { t.flags.set(typeRecur, b) } +// SetNod associates t with syntax node n. +func (t *Type) SetNod(n IRNode) { + // t.nod can be non-nil already + // in the case of shared *Types, like []byte or interface{}. + if t.nod == nil { + t.nod = n + } +} + +// Pos returns a position associated with t, if any. +// This should only be used for diagnostics. +func (t *Type) Pos() src.XPos { + if t.nod != nil { + return t.nod.Pos() + } + return src.NoXPos +} + // Pkg returns the package that t appeared in. // // Pkg is only defined for function, struct, and interface types @@ -1519,7 +1541,24 @@ var ( TypeInt128 = newSSA("int128") ) -func SetUnderlying(t, underlying *Type) { +// NewNamed returns a new named type for the given type name. +func NewNamed(obj IRNode) *Type { + t := New(TFORW) + t.Sym = obj.Sym() + t.nod = obj + return t +} + +// Obj returns the type name for the named type t. +func (t *Type) Obj() IRNode { + if t.Sym != nil { + return t.nod + } + return nil +} + +// SetUnderlying sets the underlying type. +func (t *Type) SetUnderlying(underlying *Type) { if underlying.Etype == TFORW { // This type isn't computed yet; when it is, update n. underlying.ForwardType().Copyto = append(underlying.ForwardType().Copyto, t) @@ -1546,13 +1585,13 @@ func SetUnderlying(t, underlying *Type) { // to the existing type, but the method set of an interface // type [...] remains unchanged." if t.IsInterface() { - *t.Methods() = *underlying.Methods() - *t.AllMethods() = *underlying.AllMethods() + t.methods = underlying.methods + t.allMethods = underlying.allMethods } // Update types waiting on this type. for _, w := range ft.Copyto { - SetUnderlying(w, t) + w.SetUnderlying(t) } // Double-check use of type as embedded type. |