| 1 |
package typeutil |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"go/types" |
| 6 |
"slices" |
| 7 |
|
| 8 |
"golang.org/x/exp/typeparams" |
| 9 |
) |
| 10 |
|
| 11 |
type TypeSet struct { |
| 12 |
Terms []*types.Term |
| 13 |
empty bool |
| 14 |
} |
| 15 |
|
| 16 |
func NewTypeSet(typ types.Type) TypeSet { |
| 17 |
terms, err := typeparams.NormalTerms(typ) |
| 18 |
if err != nil { |
| 19 |
if errors.Is(err, typeparams.ErrEmptyTypeSet) { |
| 20 |
return TypeSet{nil, true} |
| 21 |
} else { |
| 22 |
// We couldn't determine the type set. Assume it's all types. |
| 23 |
return TypeSet{nil, false} |
| 24 |
} |
| 25 |
} |
| 26 |
return TypeSet{terms, false} |
| 27 |
} |
| 28 |
|
| 29 |
// CoreType returns the type set's core type, or nil if it has none. |
| 30 |
// The function only looks at type terms and may thus return core types for some empty type sets, such as |
| 31 |
// 'interface { map[int]string; foo() }' |
| 32 |
func (ts TypeSet) CoreType() types.Type { |
| 33 |
if len(ts.Terms) == 0 { |
| 34 |
// Either the type set is empty, or it isn't constrained. Either way it doesn't have a core type. |
| 35 |
return nil |
| 36 |
} |
| 37 |
typ := ts.Terms[0].Type().Underlying() |
| 38 |
for _, term := range ts.Terms[1:] { |
| 39 |
ut := term.Type().Underlying() |
| 40 |
if types.Identical(typ, ut) { |
| 41 |
continue |
| 42 |
} |
| 43 |
|
| 44 |
ch1, ok := typ.(*types.Chan) |
| 45 |
if !ok { |
| 46 |
return nil |
| 47 |
} |
| 48 |
ch2, ok := ut.(*types.Chan) |
| 49 |
if !ok { |
| 50 |
return nil |
| 51 |
} |
| 52 |
if ch1.Dir() == types.SendRecv { |
| 53 |
// typ is currently a bidirectional channel. The term's type is either also bidirectional, or |
| 54 |
// unidirectional. Use the term's type. |
| 55 |
typ = ut |
| 56 |
} else if ch2.Dir() == types.SendRecv { |
| 57 |
// typ is currently a unidirectional channel and the term's type is bidirectional, which means it has no |
| 58 |
// effect. |
| 59 |
continue |
| 60 |
} else if ch1.Dir() != ch2.Dir() { |
| 61 |
// typ is not bidirectional and typ and term disagree about the direction |
| 62 |
return nil |
| 63 |
} |
| 64 |
} |
| 65 |
return typ |
| 66 |
} |
| 67 |
|
| 68 |
// CoreType is a wrapper for NewTypeSet(typ).CoreType() |
| 69 |
func CoreType(typ types.Type) types.Type { |
| 70 |
return NewTypeSet(typ).CoreType() |
| 71 |
} |
| 72 |
|
| 73 |
// All calls fn for each term in the type set and reports whether all invocations returned true. |
| 74 |
// If the type set is empty or unconstrained, All immediately returns false. |
| 75 |
func (ts TypeSet) All(fn func(*types.Term) bool) bool { |
| 76 |
if len(ts.Terms) == 0 { |
| 77 |
return false |
| 78 |
} |
| 79 |
for _, term := range ts.Terms { |
| 80 |
if !fn(term) { |
| 81 |
return false |
| 82 |
} |
| 83 |
} |
| 84 |
return true |
| 85 |
} |
| 86 |
|
| 87 |
// Any calls fn for each term in the type set and reports whether any invocation returned true. |
| 88 |
// It stops after the first call that returned true. |
| 89 |
func (ts TypeSet) Any(fn func(*types.Term) bool) bool { |
| 90 |
return slices.ContainsFunc(ts.Terms, fn) |
| 91 |
} |
| 92 |
|
| 93 |
// All is a wrapper for NewTypeSet(typ).All(fn). |
| 94 |
func All(typ types.Type, fn func(*types.Term) bool) bool { |
| 95 |
return NewTypeSet(typ).All(fn) |
| 96 |
} |
| 97 |
|
| 98 |
// Any is a wrapper for NewTypeSet(typ).Any(fn). |
| 99 |
func Any(typ types.Type, fn func(*types.Term) bool) bool { |
| 100 |
return NewTypeSet(typ).Any(fn) |
| 101 |
} |
| 102 |
|
| 103 |
func IsSlice(term *types.Term) bool { |
| 104 |
_, ok := term.Type().Underlying().(*types.Slice) |
| 105 |
return ok |
| 106 |
} |