Debian Codegraph @

go/types/typeutil/typeparams.go

raw
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 !types.Identical(ch1.Elem(), ch2.Elem()) {
53 return nil
54 }
55 if ch1.Dir() == types.SendRecv {
56 // typ is currently a bidirectional channel. The term's type is either also bidirectional, or
57 // unidirectional. Use the term's type.
58 typ = ut
59 } else if ch2.Dir() == types.SendRecv {
60 // typ is currently a unidirectional channel and the term's type is bidirectional, which means it has no
61 // effect.
62 continue
63 } else if ch1.Dir() != ch2.Dir() {
64 // typ is not bidirectional and typ and term disagree about the direction
65 return nil
66 }
67 }
68 return typ
69 }
70
71 // CoreType is a wrapper for NewTypeSet(typ).CoreType()
72 func CoreType(typ types.Type) types.Type {
73 return NewTypeSet(typ).CoreType()
74 }
75
76 // All calls fn for each term in the type set and reports whether all invocations returned true.
77 // If the type set is empty or unconstrained, All immediately returns false.
78 func (ts TypeSet) All(fn func(*types.Term) bool) bool {
79 if len(ts.Terms) == 0 {
80 return false
81 }
82 for _, term := range ts.Terms {
83 if !fn(term) {
84 return false
85 }
86 }
87 return true
88 }
89
90 // Any calls fn for each term in the type set and reports whether any invocation returned true.
91 // It stops after the first call that returned true.
92 func (ts TypeSet) Any(fn func(*types.Term) bool) bool {
93 return slices.ContainsFunc(ts.Terms, fn)
94 }
95
96 // All is a wrapper for NewTypeSet(typ).All(fn).
97 func All(typ types.Type, fn func(*types.Term) bool) bool {
98 return NewTypeSet(typ).All(fn)
99 }
100
101 // Any is a wrapper for NewTypeSet(typ).Any(fn).
102 func Any(typ types.Type, fn func(*types.Term) bool) bool {
103 return NewTypeSet(typ).Any(fn)
104 }
105
106 func IsType[T types.Type](term *types.Term) bool {
107 _, ok := term.Type().Underlying().(T)
108 return ok
109 }
110
111 //go:fix inline
112 func IsSlice(term *types.Term) bool {
113 return IsType[*types.Slice](term)
114 }