| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package typeparams |
| 6 |
|
| 7 |
import ( |
| 8 |
"errors" |
| 9 |
"fmt" |
| 10 |
"go/types" |
| 11 |
"os" |
| 12 |
"strings" |
| 13 |
) |
| 14 |
|
| 15 |
const debug = false |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
var ErrEmptyTypeSet = errors.New("empty type set") |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
func NormalTerms(typ types.Type) ([]*Term, error) { |
| 67 |
if tparam, ok := typ.(*TypeParam); ok { |
| 68 |
constraint := tparam.Constraint() |
| 69 |
if constraint == nil { |
| 70 |
return nil, fmt.Errorf("%s has nil constraint", tparam) |
| 71 |
} |
| 72 |
iface, _ := constraint.Underlying().(*types.Interface) |
| 73 |
if iface == nil { |
| 74 |
return nil, fmt.Errorf("constraint is %T, not *types.Interface", constraint.Underlying()) |
| 75 |
} |
| 76 |
typ = iface |
| 77 |
} |
| 78 |
tset, err := computeTermSetInternal(typ, make(map[types.Type]*termSet), 0) |
| 79 |
if err != nil { |
| 80 |
return nil, err |
| 81 |
} |
| 82 |
if tset.terms.isEmpty() { |
| 83 |
return nil, ErrEmptyTypeSet |
| 84 |
} |
| 85 |
if tset.terms.isAll() { |
| 86 |
return nil, nil |
| 87 |
} |
| 88 |
var terms []*Term |
| 89 |
for _, term := range tset.terms { |
| 90 |
terms = append(terms, NewTerm(term.tilde, term.typ)) |
| 91 |
} |
| 92 |
return terms, nil |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
type termSet struct { |
| 101 |
complete bool |
| 102 |
terms termlist |
| 103 |
} |
| 104 |
|
| 105 |
func indentf(depth int, format string, args ...interface{}) { |
| 106 |
fmt.Fprintf(os.Stderr, strings.Repeat(".", depth)+format+"\n", args...) |
| 107 |
} |
| 108 |
|
| 109 |
func computeTermSetInternal(t types.Type, seen map[types.Type]*termSet, depth int) (res *termSet, err error) { |
| 110 |
if t == nil { |
| 111 |
panic("nil type") |
| 112 |
} |
| 113 |
|
| 114 |
if debug { |
| 115 |
indentf(depth, "%s", t.String()) |
| 116 |
defer func() { |
| 117 |
if err != nil { |
| 118 |
indentf(depth, "=> %s", err) |
| 119 |
} else { |
| 120 |
indentf(depth, "=> %s", res.terms.String()) |
| 121 |
} |
| 122 |
}() |
| 123 |
} |
| 124 |
|
| 125 |
const maxTermCount = 100 |
| 126 |
if tset, ok := seen[t]; ok { |
| 127 |
if !tset.complete { |
| 128 |
return nil, fmt.Errorf("cycle detected in the declaration of %s", t) |
| 129 |
} |
| 130 |
return tset, nil |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
tset := new(termSet) |
| 135 |
defer func() { |
| 136 |
tset.complete = true |
| 137 |
}() |
| 138 |
seen[t] = tset |
| 139 |
|
| 140 |
switch u := t.Underlying().(type) { |
| 141 |
case *types.Interface: |
| 142 |
|
| 143 |
|
| 144 |
tset.terms = allTermlist |
| 145 |
for i := 0; i < u.NumEmbeddeds(); i++ { |
| 146 |
embedded := u.EmbeddedType(i) |
| 147 |
if _, ok := embedded.Underlying().(*TypeParam); ok { |
| 148 |
return nil, fmt.Errorf("invalid embedded type %T", embedded) |
| 149 |
} |
| 150 |
tset2, err := computeTermSetInternal(embedded, seen, depth+1) |
| 151 |
if err != nil { |
| 152 |
return nil, err |
| 153 |
} |
| 154 |
tset.terms = tset.terms.intersect(tset2.terms) |
| 155 |
} |
| 156 |
case *Union: |
| 157 |
|
| 158 |
tset.terms = nil |
| 159 |
for i := 0; i < u.Len(); i++ { |
| 160 |
t := u.Term(i) |
| 161 |
var terms termlist |
| 162 |
switch t.Type().Underlying().(type) { |
| 163 |
case *types.Interface: |
| 164 |
tset2, err := computeTermSetInternal(t.Type(), seen, depth+1) |
| 165 |
if err != nil { |
| 166 |
return nil, err |
| 167 |
} |
| 168 |
terms = tset2.terms |
| 169 |
case *TypeParam, *Union: |
| 170 |
|
| 171 |
|
| 172 |
return nil, fmt.Errorf("invalid union term %T", t) |
| 173 |
default: |
| 174 |
if t.Type() == types.Typ[types.Invalid] { |
| 175 |
continue |
| 176 |
} |
| 177 |
terms = termlist{{t.Tilde(), t.Type()}} |
| 178 |
} |
| 179 |
tset.terms = tset.terms.union(terms) |
| 180 |
if len(tset.terms) > maxTermCount { |
| 181 |
return nil, fmt.Errorf("exceeded max term count %d", maxTermCount) |
| 182 |
} |
| 183 |
} |
| 184 |
case *TypeParam: |
| 185 |
panic("unreachable") |
| 186 |
default: |
| 187 |
|
| 188 |
|
| 189 |
if u != types.Typ[types.Invalid] { |
| 190 |
tset.terms = termlist{{false, t}} |
| 191 |
} |
| 192 |
} |
| 193 |
return tset, nil |
| 194 |
} |