mirror of
https://github.com/golang/go
synced 2024-09-05 07:54:46 +00:00
[dev.typeparams] cmd/compile/internal/types2: move Interface type decl into interface.go (cleanup)
Change-Id: Ie1ba50c82afb7409f9495a19b8629c61c6a8d4dc Reviewed-on: https://go-review.googlesource.com/c/go/+/332092 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
parent
838079beef
commit
9c1e7d9eff
|
@ -6,6 +6,154 @@ package types2
|
||||||
|
|
||||||
import "cmd/compile/internal/syntax"
|
import "cmd/compile/internal/syntax"
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// API
|
||||||
|
|
||||||
|
// An Interface represents an interface type.
|
||||||
|
type Interface struct {
|
||||||
|
obj *TypeName // corresponding declared object; or nil (for better error messages)
|
||||||
|
methods []*Func // ordered list of explicitly declared methods
|
||||||
|
embeddeds []Type // ordered list of explicitly embedded elements
|
||||||
|
embedPos *[]syntax.Pos // positions of embedded elements; or nil (for error messages) - use pointer to save space
|
||||||
|
complete bool // indicates that all fields (except for tset) are set up
|
||||||
|
|
||||||
|
tset *TypeSet // type set described by this interface, computed lazily
|
||||||
|
}
|
||||||
|
|
||||||
|
// typeSet returns the type set for interface t.
|
||||||
|
func (t *Interface) typeSet() *TypeSet { return computeTypeSet(nil, nopos, t) }
|
||||||
|
|
||||||
|
// is reports whether interface t represents types that all satisfy f.
|
||||||
|
func (t *Interface) is(f func(Type, bool) bool) bool {
|
||||||
|
switch t := t.typeSet().types.(type) {
|
||||||
|
case nil, *top:
|
||||||
|
// TODO(gri) should settle on top or nil to represent this case
|
||||||
|
return false // we must have at least one type! (was bug)
|
||||||
|
case *Union:
|
||||||
|
return t.is(func(typ Type, tilde bool) bool { return f(typ, tilde) })
|
||||||
|
default:
|
||||||
|
return f(t, false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// emptyInterface represents the empty interface
|
||||||
|
var emptyInterface = Interface{complete: true, tset: &topTypeSet}
|
||||||
|
|
||||||
|
// NewInterface returns a new interface for the given methods and embedded types.
|
||||||
|
// NewInterface takes ownership of the provided methods and may modify their types
|
||||||
|
// by setting missing receivers.
|
||||||
|
//
|
||||||
|
// Deprecated: Use NewInterfaceType instead which allows arbitrary embedded types.
|
||||||
|
func NewInterface(methods []*Func, embeddeds []*Named) *Interface {
|
||||||
|
tnames := make([]Type, len(embeddeds))
|
||||||
|
for i, t := range embeddeds {
|
||||||
|
tnames[i] = t
|
||||||
|
}
|
||||||
|
return NewInterfaceType(methods, tnames)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewInterfaceType returns a new interface for the given methods and embedded types.
|
||||||
|
// NewInterfaceType takes ownership of the provided methods and may modify their types
|
||||||
|
// by setting missing receivers.
|
||||||
|
func NewInterfaceType(methods []*Func, embeddeds []Type) *Interface {
|
||||||
|
if len(methods) == 0 && len(embeddeds) == 0 {
|
||||||
|
return &emptyInterface
|
||||||
|
}
|
||||||
|
|
||||||
|
// set method receivers if necessary
|
||||||
|
typ := new(Interface)
|
||||||
|
for _, m := range methods {
|
||||||
|
if sig := m.typ.(*Signature); sig.recv == nil {
|
||||||
|
sig.recv = NewVar(m.pos, m.pkg, "", typ)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// sort for API stability
|
||||||
|
sortMethods(methods)
|
||||||
|
|
||||||
|
typ.methods = methods
|
||||||
|
typ.embeddeds = embeddeds
|
||||||
|
typ.complete = true
|
||||||
|
|
||||||
|
return typ
|
||||||
|
}
|
||||||
|
|
||||||
|
// NumExplicitMethods returns the number of explicitly declared methods of interface t.
|
||||||
|
func (t *Interface) NumExplicitMethods() int { return len(t.methods) }
|
||||||
|
|
||||||
|
// ExplicitMethod returns the i'th explicitly declared method of interface t for 0 <= i < t.NumExplicitMethods().
|
||||||
|
// The methods are ordered by their unique Id.
|
||||||
|
func (t *Interface) ExplicitMethod(i int) *Func { return t.methods[i] }
|
||||||
|
|
||||||
|
// NumEmbeddeds returns the number of embedded types in interface t.
|
||||||
|
func (t *Interface) NumEmbeddeds() int { return len(t.embeddeds) }
|
||||||
|
|
||||||
|
// Embedded returns the i'th embedded defined (*Named) type of interface t for 0 <= i < t.NumEmbeddeds().
|
||||||
|
// The result is nil if the i'th embedded type is not a defined type.
|
||||||
|
//
|
||||||
|
// Deprecated: Use EmbeddedType which is not restricted to defined (*Named) types.
|
||||||
|
func (t *Interface) Embedded(i int) *Named { tname, _ := t.embeddeds[i].(*Named); return tname }
|
||||||
|
|
||||||
|
// EmbeddedType returns the i'th embedded type of interface t for 0 <= i < t.NumEmbeddeds().
|
||||||
|
func (t *Interface) EmbeddedType(i int) Type { return t.embeddeds[i] }
|
||||||
|
|
||||||
|
// NumMethods returns the total number of methods of interface t.
|
||||||
|
func (t *Interface) NumMethods() int { return t.typeSet().NumMethods() }
|
||||||
|
|
||||||
|
// Method returns the i'th method of interface t for 0 <= i < t.NumMethods().
|
||||||
|
// The methods are ordered by their unique Id.
|
||||||
|
func (t *Interface) Method(i int) *Func { return t.typeSet().Method(i) }
|
||||||
|
|
||||||
|
// Empty reports whether t is the empty interface.
|
||||||
|
func (t *Interface) Empty() bool { return t.typeSet().IsTop() }
|
||||||
|
|
||||||
|
// IsComparable reports whether interface t is or embeds the predeclared interface "comparable".
|
||||||
|
func (t *Interface) IsComparable() bool { return t.typeSet().IsComparable() }
|
||||||
|
|
||||||
|
// IsConstraint reports whether interface t is not just a method set.
|
||||||
|
func (t *Interface) IsConstraint() bool { return !t.typeSet().IsMethodSet() }
|
||||||
|
|
||||||
|
// isSatisfiedBy reports whether interface t's type list is satisfied by the type typ.
|
||||||
|
// If the type list is empty (absent), typ trivially satisfies the interface.
|
||||||
|
// TODO(gri) This is not a great name. Eventually, we should have a more comprehensive
|
||||||
|
// "implements" predicate.
|
||||||
|
func (t *Interface) isSatisfiedBy(typ Type) bool {
|
||||||
|
switch t := t.typeSet().types.(type) {
|
||||||
|
case nil:
|
||||||
|
return true // no type restrictions
|
||||||
|
case *Union:
|
||||||
|
r, _ := t.intersect(typ, false)
|
||||||
|
return r != nil
|
||||||
|
default:
|
||||||
|
return Identical(t, typ)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Complete computes the interface's type set. It must be called by users of
|
||||||
|
// NewInterfaceType and NewInterface after the interface's embedded types are
|
||||||
|
// fully defined and before using the interface type in any way other than to
|
||||||
|
// form other types. The interface must not contain duplicate methods or a
|
||||||
|
// panic occurs. Complete returns the receiver.
|
||||||
|
//
|
||||||
|
// Deprecated: Type sets are now computed lazily, on demand; this function
|
||||||
|
// is only here for backward-compatibility. It does not have to
|
||||||
|
// be called explicitly anymore.
|
||||||
|
func (t *Interface) Complete() *Interface {
|
||||||
|
// Some tests are still depending on the state change
|
||||||
|
// (string representation of an Interface not containing an
|
||||||
|
// /* incomplete */ marker) caused by the explicit Complete
|
||||||
|
// call, so we compute the type set eagerly here.
|
||||||
|
t.complete = true
|
||||||
|
t.typeSet()
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Interface) Underlying() Type { return t }
|
||||||
|
func (t *Interface) String() string { return TypeString(t, nil) }
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// Implementation
|
||||||
|
|
||||||
func (check *Checker) interfaceType(ityp *Interface, iface *syntax.InterfaceType, def *Named) {
|
func (check *Checker) interfaceType(ityp *Interface, iface *syntax.InterfaceType, def *Named) {
|
||||||
var tlist []syntax.Expr // types collected from all type lists
|
var tlist []syntax.Expr // types collected from all type lists
|
||||||
var tname *syntax.Name // most recent "type" name
|
var tname *syntax.Name // most recent "type" name
|
||||||
|
|
|
@ -163,145 +163,6 @@ func (t *Tuple) Len() int {
|
||||||
// At returns the i'th variable of tuple t.
|
// At returns the i'th variable of tuple t.
|
||||||
func (t *Tuple) At(i int) *Var { return t.vars[i] }
|
func (t *Tuple) At(i int) *Var { return t.vars[i] }
|
||||||
|
|
||||||
// An Interface represents an interface type.
|
|
||||||
type Interface struct {
|
|
||||||
obj *TypeName // corresponding declared object; or nil (for better error messages)
|
|
||||||
methods []*Func // ordered list of explicitly declared methods
|
|
||||||
embeddeds []Type // ordered list of explicitly embedded elements
|
|
||||||
embedPos *[]syntax.Pos // positions of embedded elements; or nil (for error messages) - use pointer to save space
|
|
||||||
complete bool // indicates that all fields (except for tset) are set up
|
|
||||||
|
|
||||||
tset *TypeSet // type set described by this interface, computed lazily
|
|
||||||
}
|
|
||||||
|
|
||||||
// typeSet returns the type set for interface t.
|
|
||||||
func (t *Interface) typeSet() *TypeSet { return computeTypeSet(nil, nopos, t) }
|
|
||||||
|
|
||||||
// is reports whether interface t represents types that all satisfy f.
|
|
||||||
func (t *Interface) is(f func(Type, bool) bool) bool {
|
|
||||||
switch t := t.typeSet().types.(type) {
|
|
||||||
case nil, *top:
|
|
||||||
// TODO(gri) should settle on top or nil to represent this case
|
|
||||||
return false // we must have at least one type! (was bug)
|
|
||||||
case *Union:
|
|
||||||
return t.is(func(typ Type, tilde bool) bool { return f(typ, tilde) })
|
|
||||||
default:
|
|
||||||
return f(t, false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// emptyInterface represents the empty interface
|
|
||||||
var emptyInterface = Interface{complete: true, tset: &topTypeSet}
|
|
||||||
|
|
||||||
// NewInterface returns a new interface for the given methods and embedded types.
|
|
||||||
// NewInterface takes ownership of the provided methods and may modify their types
|
|
||||||
// by setting missing receivers.
|
|
||||||
//
|
|
||||||
// Deprecated: Use NewInterfaceType instead which allows arbitrary embedded types.
|
|
||||||
func NewInterface(methods []*Func, embeddeds []*Named) *Interface {
|
|
||||||
tnames := make([]Type, len(embeddeds))
|
|
||||||
for i, t := range embeddeds {
|
|
||||||
tnames[i] = t
|
|
||||||
}
|
|
||||||
return NewInterfaceType(methods, tnames)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewInterfaceType returns a new interface for the given methods and embedded types.
|
|
||||||
// NewInterfaceType takes ownership of the provided methods and may modify their types
|
|
||||||
// by setting missing receivers.
|
|
||||||
func NewInterfaceType(methods []*Func, embeddeds []Type) *Interface {
|
|
||||||
if len(methods) == 0 && len(embeddeds) == 0 {
|
|
||||||
return &emptyInterface
|
|
||||||
}
|
|
||||||
|
|
||||||
// set method receivers if necessary
|
|
||||||
typ := new(Interface)
|
|
||||||
for _, m := range methods {
|
|
||||||
if sig := m.typ.(*Signature); sig.recv == nil {
|
|
||||||
sig.recv = NewVar(m.pos, m.pkg, "", typ)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// sort for API stability
|
|
||||||
sortMethods(methods)
|
|
||||||
|
|
||||||
typ.methods = methods
|
|
||||||
typ.embeddeds = embeddeds
|
|
||||||
typ.complete = true
|
|
||||||
|
|
||||||
return typ
|
|
||||||
}
|
|
||||||
|
|
||||||
// NumExplicitMethods returns the number of explicitly declared methods of interface t.
|
|
||||||
func (t *Interface) NumExplicitMethods() int { return len(t.methods) }
|
|
||||||
|
|
||||||
// ExplicitMethod returns the i'th explicitly declared method of interface t for 0 <= i < t.NumExplicitMethods().
|
|
||||||
// The methods are ordered by their unique Id.
|
|
||||||
func (t *Interface) ExplicitMethod(i int) *Func { return t.methods[i] }
|
|
||||||
|
|
||||||
// NumEmbeddeds returns the number of embedded types in interface t.
|
|
||||||
func (t *Interface) NumEmbeddeds() int { return len(t.embeddeds) }
|
|
||||||
|
|
||||||
// Embedded returns the i'th embedded defined (*Named) type of interface t for 0 <= i < t.NumEmbeddeds().
|
|
||||||
// The result is nil if the i'th embedded type is not a defined type.
|
|
||||||
//
|
|
||||||
// Deprecated: Use EmbeddedType which is not restricted to defined (*Named) types.
|
|
||||||
func (t *Interface) Embedded(i int) *Named { tname, _ := t.embeddeds[i].(*Named); return tname }
|
|
||||||
|
|
||||||
// EmbeddedType returns the i'th embedded type of interface t for 0 <= i < t.NumEmbeddeds().
|
|
||||||
func (t *Interface) EmbeddedType(i int) Type { return t.embeddeds[i] }
|
|
||||||
|
|
||||||
// NumMethods returns the total number of methods of interface t.
|
|
||||||
func (t *Interface) NumMethods() int { return t.typeSet().NumMethods() }
|
|
||||||
|
|
||||||
// Method returns the i'th method of interface t for 0 <= i < t.NumMethods().
|
|
||||||
// The methods are ordered by their unique Id.
|
|
||||||
func (t *Interface) Method(i int) *Func { return t.typeSet().Method(i) }
|
|
||||||
|
|
||||||
// Empty reports whether t is the empty interface.
|
|
||||||
func (t *Interface) Empty() bool { return t.typeSet().IsTop() }
|
|
||||||
|
|
||||||
// IsComparable reports whether interface t is or embeds the predeclared interface "comparable".
|
|
||||||
func (t *Interface) IsComparable() bool { return t.typeSet().IsComparable() }
|
|
||||||
|
|
||||||
// IsConstraint reports whether interface t is not just a method set.
|
|
||||||
func (t *Interface) IsConstraint() bool { return !t.typeSet().IsMethodSet() }
|
|
||||||
|
|
||||||
// isSatisfiedBy reports whether interface t's type list is satisfied by the type typ.
|
|
||||||
// If the type list is empty (absent), typ trivially satisfies the interface.
|
|
||||||
// TODO(gri) This is not a great name. Eventually, we should have a more comprehensive
|
|
||||||
// "implements" predicate.
|
|
||||||
func (t *Interface) isSatisfiedBy(typ Type) bool {
|
|
||||||
switch t := t.typeSet().types.(type) {
|
|
||||||
case nil:
|
|
||||||
return true // no type restrictions
|
|
||||||
case *Union:
|
|
||||||
r, _ := t.intersect(typ, false)
|
|
||||||
return r != nil
|
|
||||||
default:
|
|
||||||
return Identical(t, typ)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Complete computes the interface's type set. It must be called by users of
|
|
||||||
// NewInterfaceType and NewInterface after the interface's embedded types are
|
|
||||||
// fully defined and before using the interface type in any way other than to
|
|
||||||
// form other types. The interface must not contain duplicate methods or a
|
|
||||||
// panic occurs. Complete returns the receiver.
|
|
||||||
//
|
|
||||||
// Deprecated: Type sets are now computed lazily, on demand; this function
|
|
||||||
// is only here for backward-compatibility. It does not have to
|
|
||||||
// be called explicitly anymore.
|
|
||||||
func (t *Interface) Complete() *Interface {
|
|
||||||
// Some tests are still depending on the state change
|
|
||||||
// (string representation of an Interface not containing an
|
|
||||||
// /* incomplete */ marker) caused by the explicit Complete
|
|
||||||
// call, so we compute the type set eagerly here.
|
|
||||||
t.complete = true
|
|
||||||
t.typeSet()
|
|
||||||
return t
|
|
||||||
}
|
|
||||||
|
|
||||||
// A Map represents a map type.
|
// A Map represents a map type.
|
||||||
type Map struct {
|
type Map struct {
|
||||||
key, elem Type
|
key, elem Type
|
||||||
|
@ -644,7 +505,6 @@ func (t *Array) Underlying() Type { return t }
|
||||||
func (t *Slice) Underlying() Type { return t }
|
func (t *Slice) Underlying() Type { return t }
|
||||||
func (t *Pointer) Underlying() Type { return t }
|
func (t *Pointer) Underlying() Type { return t }
|
||||||
func (t *Tuple) Underlying() Type { return t }
|
func (t *Tuple) Underlying() Type { return t }
|
||||||
func (t *Interface) Underlying() Type { return t }
|
|
||||||
func (t *Map) Underlying() Type { return t }
|
func (t *Map) Underlying() Type { return t }
|
||||||
func (t *Chan) Underlying() Type { return t }
|
func (t *Chan) Underlying() Type { return t }
|
||||||
func (t *Named) Underlying() Type { return t.expand().underlying }
|
func (t *Named) Underlying() Type { return t.expand().underlying }
|
||||||
|
@ -658,7 +518,6 @@ func (t *Array) String() string { return TypeString(t, nil) }
|
||||||
func (t *Slice) String() string { return TypeString(t, nil) }
|
func (t *Slice) String() string { return TypeString(t, nil) }
|
||||||
func (t *Pointer) String() string { return TypeString(t, nil) }
|
func (t *Pointer) String() string { return TypeString(t, nil) }
|
||||||
func (t *Tuple) String() string { return TypeString(t, nil) }
|
func (t *Tuple) String() string { return TypeString(t, nil) }
|
||||||
func (t *Interface) String() string { return TypeString(t, nil) }
|
|
||||||
func (t *Map) String() string { return TypeString(t, nil) }
|
func (t *Map) String() string { return TypeString(t, nil) }
|
||||||
func (t *Chan) String() string { return TypeString(t, nil) }
|
func (t *Chan) String() string { return TypeString(t, nil) }
|
||||||
func (t *Named) String() string { return TypeString(t, nil) }
|
func (t *Named) String() string { return TypeString(t, nil) }
|
||||||
|
|
Loading…
Reference in a new issue