/
types.go
304 lines (241 loc) · 6.55 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package util
import (
"go/types"
"sync"
"golang.org/x/tools/go/types/typeutil"
)
type ImmType interface {
isImmType()
}
type (
// ImmTypeBasic identifies Go types that are inherently immutable, e.g.
// ints, strings
ImmTypeBasic struct{}
// ImmTypeStruct is used to indicate a type that is immutable by virtue of
// being a pointer to a struct type that was itself generated from an _Imm_
// struct template.
ImmTypeStruct struct {
Struct *types.Struct
}
// ImmTypeMap is used to indicate a type that is immutable by virtue of
// being a pointer to a struct type that was itself generated from an _Imm_
// map template.
ImmTypeMap struct {
Key types.Type
Elem types.Type
}
// ImmTypeMap is used to indicate a type that is immutable by virtue of
// being a pointer to a struct type that was itself generated from an _Imm_
// slice template.
ImmTypeSlice struct {
Elem types.Type
}
// ImmTypeImplsIntf is used to indicate a type that is not an ImmTypeStruct,
// ImmTypeMap or ImmTypeSlice, but still satisfies the immutable "interface".
// See the docs for myitcv.io/immutable.Immutable.
ImmTypeImplsIntf struct{}
// ImmTypeSimple is used to indiciate an interface type that extends the
// myitcv.io/immutable.Immutable interface.
ImmTypeSimple struct{}
)
func (i ImmTypeBasic) isImmType() {}
func (i ImmTypeStruct) isImmType() {}
func (i ImmTypeMap) isImmType() {}
func (i ImmTypeSlice) isImmType() {}
func (i ImmTypeImplsIntf) isImmType() {}
func (i ImmTypeSimple) isImmType() {}
// TODO make a non-global; define a good API for creating a new checker
// after we have a couple of use cases that break with this approach.
var ic = &immCache{
msCache: new(typeutil.MethodSetCache),
res: make(map[types.Type]ImmType),
}
type immCache struct {
mu sync.Mutex
// not entirely clear we even need this because we cache
// the results of determining whether a pointer type is immutable
// or not.
msCache *typeutil.MethodSetCache
// res is a cache of the non-pointer type to the result
// because pointer type values are not comparable
res map[types.Type]ImmType
}
func (i *immCache) lookup(tt types.Type) (v ImmType) {
var cacheKey = tt
// fast path for Go types that are inherently immutable
switch tt := tt.Underlying().(type) {
case *types.Basic:
return ImmTypeBasic{}
case *types.Pointer:
cacheKey = tt.Elem()
case *types.Interface:
// ideally we would use types.Implements here... but we don't have a
// reference to myitcv.io/immutable.Immutable. So we do it by hand for now.
default:
// see comment below
return nil
}
// From this point onwards we have to implement the immutable "interface". And to my best
// understanding at this point in time, that is only possible if the type is a pointer.
// Hence anything else cannot be an immutable type.
// We don't actually care whether we are pointing to a named type or not... because we use
// underlying below.
i.mu.Lock()
defer i.mu.Unlock()
v, ok := i.res[cacheKey]
if ok {
return v
}
defer func() {
i.res[cacheKey] = v
}()
ms := i.msCache.MethodSet(tt)
foundMutable := false
foundAsMutable := false
foundAsImmutable := false
foundWithMutable := false
foundWithImmutable := false
foundIsDeeply := false
pt, ptOk := tt.(*types.Pointer)
isPtrToSelf := func(t types.Type) bool {
if !ptOk {
return false
}
ppt, ok := t.(*types.Pointer)
if !ok {
return false
}
return ppt.Elem() == pt.Elem()
}
for i := 0; i < ms.Len(); i++ {
f := ms.At(i).Obj().(*types.Func)
t := f.Type().(*types.Signature)
switch mn := f.Name(); mn {
case "Mutable":
if t.Params().Len() != 0 {
break
}
if t.Results().Len() != 1 {
break
}
tres := t.Results().At(0)
if b, ok := tres.Type().(*types.Basic); ok {
foundMutable = b.Kind() == types.Bool
}
case "AsMutable":
if t.Params().Len() != 0 {
break
}
if t.Results().Len() != 1 {
break
}
foundAsMutable = isPtrToSelf(t.Results().At(0).Type())
case "AsImmutable":
if t.Params().Len() != 1 {
break
}
if !isPtrToSelf(t.Params().At(0).Type()) {
break
}
if t.Results().Len() != 1 {
break
}
foundAsImmutable = isPtrToSelf(t.Results().At(0).Type())
case "WithMutable", "WithImmutable":
if t.Params().Len() != 1 {
break
}
st, ok := t.Params().At(0).Type().(*types.Signature)
if !ok {
break
}
if st.Params().Len() != 1 {
break
}
if !isPtrToSelf(st.Params().At(0).Type()) {
break
}
if st.Results().Len() != 0 {
break
}
if t.Results().Len() != 1 {
break
}
valid := isPtrToSelf(t.Results().At(0).Type())
switch mn {
case "WithMutable":
foundWithMutable = valid
case "WithImmutable":
foundWithImmutable = valid
}
case "IsDeeplyNonMutable":
if t.Params().Len() != 1 {
break
}
mt, ok := t.Params().At(0).Type().(*types.Map)
if !ok {
break
}
if it, ok := mt.Key().(*types.Interface); !ok || !it.Empty() {
break
}
if t.Results().Len() != 1 {
break
}
foundIsDeeply = t.Results().At(0).Type() == types.Typ[types.Bool]
}
}
isImm := foundMutable && foundAsMutable && foundAsImmutable &&
foundWithMutable && foundWithImmutable && foundIsDeeply
isImmSimple := foundMutable && foundIsDeeply
if !isImm {
if isImmSimple {
v = ImmTypeSimple{}
}
return
}
v = ImmTypeImplsIntf{}
// now we work out whether it's a struct, slice of map... else
// it's unknown to this package
st, ok := pt.Elem().Underlying().(*types.Struct)
if !ok {
return
}
hasTmpl := false
// TODO this could probably be a bit more robust
// but we use this fairly coarse mechanism to determine
// whether the struct we have in hand is the result of
// immutableGen generation by looking for well-known fields
for i := 0; i < st.NumFields(); i++ {
f := st.Field(i)
switch f.Name() {
case "__tmpl":
hasTmpl = true
case "theMap":
m := f.Type().(*types.Map)
v = ImmTypeMap{
Key: m.Key(),
Elem: m.Elem(),
}
case "theSlice":
s := f.Type().(*types.Slice)
v = ImmTypeSlice{
Elem: s.Elem(),
}
}
}
if v == (ImmTypeImplsIntf{}) && hasTmpl {
v = ImmTypeStruct{
Struct: st,
}
}
return
}
// IsImmType determines whether the supplied type is an immutable type. In case
// a type is immutable, a value of type ImmTypeStruct, ImmTypeSlice or
// ImmTypeMap is returned. In case the type is immutable but neither of the
// aforementioned instances, ImmTypeUnknown is returned. If a type is not
// immutable then nil is returned
func IsImmType(t types.Type) ImmType {
return ic.lookup(t)
}