mirror of
https://github.com/golang/go
synced 2024-09-05 07:54:46 +00:00
[dev.typeparams] go/types: simplify Interface accessors
This is a straightforward port of CL 321850 to go/types. Change-Id: I719c19b8839390fdfa961255c6f1e79561cda6e6 Reviewed-on: https://go-review.googlesource.com/c/go/+/324757 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
parent
2f26adc232
commit
7497e57a39
|
@ -421,79 +421,41 @@ func (t *Interface) EmbeddedType(i int) Type { return t.embeddeds[i] }
|
||||||
|
|
||||||
// NumMethods returns the total number of methods of interface t.
|
// NumMethods returns the total number of methods of interface t.
|
||||||
// The interface must have been completed.
|
// The interface must have been completed.
|
||||||
func (t *Interface) NumMethods() int { t.assertCompleteness(); return len(t.allMethods) }
|
func (t *Interface) NumMethods() int { t.Complete(); return len(t.allMethods) }
|
||||||
|
|
||||||
func (t *Interface) assertCompleteness() {
|
|
||||||
if t.allMethods == nil {
|
|
||||||
panic("interface is incomplete")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Method returns the i'th method of interface t for 0 <= i < t.NumMethods().
|
// Method returns the i'th method of interface t for 0 <= i < t.NumMethods().
|
||||||
// The methods are ordered by their unique Id.
|
// The methods are ordered by their unique Id.
|
||||||
// The interface must have been completed.
|
// The interface must have been completed.
|
||||||
func (t *Interface) Method(i int) *Func { t.assertCompleteness(); return t.allMethods[i] }
|
func (t *Interface) Method(i int) *Func { t.Complete(); return t.allMethods[i] }
|
||||||
|
|
||||||
// Empty reports whether t is the empty interface.
|
// Empty reports whether t is the empty interface.
|
||||||
func (t *Interface) Empty() bool {
|
func (t *Interface) Empty() bool {
|
||||||
if t.allMethods != nil {
|
t.Complete()
|
||||||
// interface is complete - quick test
|
// A non-nil allTypes may still have length 0 but represents the bottom type.
|
||||||
// A non-nil allTypes may still be empty and represents the bottom type.
|
return len(t.allMethods) == 0 && t.allTypes == nil
|
||||||
return len(t.allMethods) == 0 && t.allTypes == nil
|
|
||||||
}
|
|
||||||
return !t.iterate(func(t *Interface) bool {
|
|
||||||
return len(t.methods) > 0 || t.types != nil
|
|
||||||
}, nil)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// _HasTypeList reports whether interface t has a type list, possibly from an embedded type.
|
// _HasTypeList reports whether interface t has a type list, possibly from an embedded type.
|
||||||
func (t *Interface) _HasTypeList() bool {
|
func (t *Interface) _HasTypeList() bool {
|
||||||
if t.allMethods != nil {
|
t.Complete()
|
||||||
// interface is complete - quick test
|
return t.allTypes != nil
|
||||||
return t.allTypes != nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return t.iterate(func(t *Interface) bool {
|
|
||||||
return t.types != nil
|
|
||||||
}, nil)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// _IsComparable reports whether interface t is or embeds the predeclared interface "comparable".
|
// _IsComparable reports whether interface t is or embeds the predeclared interface "comparable".
|
||||||
func (t *Interface) _IsComparable() bool {
|
func (t *Interface) _IsComparable() bool {
|
||||||
if t.allMethods != nil {
|
t.Complete()
|
||||||
// interface is complete - quick test
|
_, m := lookupMethod(t.allMethods, nil, "==")
|
||||||
_, m := lookupMethod(t.allMethods, nil, "==")
|
return m != nil
|
||||||
return m != nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return t.iterate(func(t *Interface) bool {
|
|
||||||
_, m := lookupMethod(t.methods, nil, "==")
|
|
||||||
return m != nil
|
|
||||||
}, nil)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// _IsConstraint reports t.HasTypeList() || t.IsComparable().
|
// _IsConstraint reports t.HasTypeList() || t.IsComparable().
|
||||||
func (t *Interface) _IsConstraint() bool {
|
func (t *Interface) _IsConstraint() bool {
|
||||||
if t.allMethods != nil {
|
return t._HasTypeList() || t._IsComparable()
|
||||||
// interface is complete - quick test
|
|
||||||
if t.allTypes != nil {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
_, m := lookupMethod(t.allMethods, nil, "==")
|
|
||||||
return m != nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return t.iterate(func(t *Interface) bool {
|
|
||||||
if t.types != nil {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
_, m := lookupMethod(t.methods, nil, "==")
|
|
||||||
return m != nil
|
|
||||||
}, nil)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// iterate calls f with t and then with any embedded interface of t, recursively, until f returns true.
|
// iterate calls f with t and then with any embedded interface of t, recursively, until f returns true.
|
||||||
// iterate reports whether any call to f returned true.
|
// iterate reports whether any call to f returned true.
|
||||||
|
// TODO(rfindley) This is now only used by infer.go - see if we can eliminate it.
|
||||||
func (t *Interface) iterate(f func(*Interface) bool, seen map[*Interface]bool) bool {
|
func (t *Interface) iterate(f func(*Interface) bool, seen map[*Interface]bool) bool {
|
||||||
if f(t) {
|
if f(t) {
|
||||||
return true
|
return true
|
||||||
|
|
Loading…
Reference in a new issue