/
type_solver.go
340 lines (304 loc) · 7.87 KB
/
type_solver.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package collector
import (
"bytes"
"fmt"
"go/types"
"reflect"
"regexp"
"strings"
"github.com/hsfzxjy/dgo/dgo-gen/internal/ir"
"github.com/hsfzxjy/dgo/dgo-gen/internal/uri"
)
type TypeSolveConfig struct {
IsPinnable bool
}
func (ctx *Context) SolveType(obj types.Object) ir.Term {
return ctx.SolveTypeEx(obj, TypeSolveConfig{}).Result
}
func (ctx *Context) SolveTypeEx(obj types.Object, config TypeSolveConfig) *typeSolver {
solver := &typeSolver{Context: ctx, config: config}
solver.Do(obj, nil)
term := solver.Result
ir.FillAllSize(term)
return solver
}
type termLayerKind int
const (
tlkInvalid termLayerKind = iota
tlkElem
tlkField
tlkDeref
tlkTypedef
tlkOther
)
type termLayer struct {
kind termLayerKind
typ types.Type
fieldName string
term ir.Term
}
type typeSolver struct {
*Context
config TypeSolveConfig
layers []termLayer
visited map[uri.Uri]struct{}
Result ir.Term
}
func (r *typeSolver) Do(obj types.Object, ityp types.Type) {
if r.visited == nil {
r.visited = make(map[uri.Uri]struct{})
}
if ityp == nil {
ityp = obj.Type()
}
SWITCH:
switch typ := ityp.(type) {
case *types.Basic:
switch typ.Kind() {
case types.Bool,
types.Int, types.Int8, types.Int16, types.Int32, types.Int64,
types.Uint, types.Uint8, types.Uint16, types.Uint32, types.Uint64, types.Uintptr,
types.Float32, types.Float64,
types.String:
r.push(tlkOther, typ, ir.NewBasic(typ))
r.pop()
return
default:
r.throwAt(obj, "not exportable type: %s", typ.String())
}
case *types.Slice:
r.push(tlkElem, typ, ir.NewSlice())
defer r.pop()
ityp = typ.Elem()
goto SWITCH
case *types.Map:
irMap := ir.NewMap()
r.push(tlkOther, typ, irMap)
defer r.pop()
r.Do(obj, typ.Key())
r.Do(obj, typ.Elem())
irKey := irMap.Key
SWITCH_KEY:
switch key := irKey.(type) {
case *ir.Basic:
case *ir.Coerce:
irKey = key.Elem
goto SWITCH_KEY
default:
r.throwAt(obj, "map type with key %s is not exportable", typ.Key().String())
}
case *types.Array:
r.push(tlkElem, typ, ir.NewArray(int(typ.Len())))
defer r.pop()
ityp = typ.Elem()
goto SWITCH
case *types.Pointer:
r.push(tlkDeref, typ, ir.NewPtrTo())
defer r.pop()
ityp = typ.Elem()
goto SWITCH
case *types.Named:
if isOptional(typ) {
ityp = ityp.Underlying().(*types.Struct).Field(0).Type()
r.push(tlkField, ityp, ir.NewOptional()).fieldName = "Value"
defer r.pop()
goto SWITCH
}
if isPinToken(typ) {
ityp = typ.TypeArgs().At(0)
r.push(tlkField, ityp, ir.NewPinToken()).fieldName = "Underlying"
defer r.pop()
goto SWITCH
}
if !r.HasTypeName(typ.Obj()) {
r.throwAt(obj, "type not marked as exported: %s", typ)
}
r.push(tlkTypedef, ityp, ir.NewCoerce(typ.Obj(), r.UriFor(typ.Obj())))
defer r.pop()
ityp = ityp.Underlying()
goto SWITCH
case *types.Chan:
if !r.isChanAllowedHere() {
r.throwAt(obj, "chan type is not exportable")
}
if typ.Dir() == types.SendOnly {
r.throwAt(obj, "send-only chan type is not exportable")
}
r.push(tlkElem, ityp, ir.NewChan())
defer r.pop()
ityp = typ.Elem()
goto SWITCH
case *types.Struct:
if !r.isTypeNamed() {
r.throwAt(obj, "anonymous type is not exportable")
}
irStruct := ir.NewStruct()
r.push(tlkOther, typ, irStruct)
defer r.pop()
var i = 0
if r.config.IsPinnable && r.isRootLevelStruct() {
if typ.NumFields() == 0 {
goto BAD_PINNABLE
}
i = 1
field := typ.Field(0)
if !isPinMeta(field.Type()) || !field.Embedded() {
goto BAD_PINNABLE
}
}
goto NEXT_FIELD
BAD_PINNABLE:
r.throwAt(obj, "pinnable struct should have an embedded pin.Meta as the first field")
NEXT_FIELD:
for ; i < typ.NumFields(); i++ {
field := typ.Field(i)
if i == 0 && isPinMeta(field.Type()) {
continue NEXT_FIELD
}
var directives *ir.FieldDirectives
{
tag := reflect.StructTag(typ.Tag(i))
spec, _ := tag.Lookup("dgo")
directives = ir.ParseFieldDirectives(spec)
}
switch {
case !directives.SendToDart:
continue NEXT_FIELD
}
irField := ir.NewField(field.Name(), directives)
if irStruct.IsFieldNameConflicted(irField.DartName()) {
r.throwAt(field, "conflicted field name: %s", irField.DartName())
}
r.push(tlkField, field.Type(), irField).
fieldName = field.Name()
r.Do(field, nil)
r.pop()
nFields := len(irStruct.Fields)
if c, ok := irField.Term.(*ir.Chan); ok {
irStruct.Fields = irStruct.Fields[:nFields-1]
irStruct.Chans = append(irStruct.Chans, irField)
c.Chid = uint8(irStruct.Nchans)
irStruct.Nchans++
}
}
return
default:
r.throwAt(obj, "not exportable type: %s", typ)
}
}
func (r *typeSolver) printPath() string {
if len(r.layers) == 0 {
return ""
}
b := strings.Builder{}
b.WriteString(fmt.Sprintf("\n\ttype = %s", r.layers[0].typ))
for _, a := range r.layers {
switch a.kind {
case tlkElem:
b.WriteString(fmt.Sprintf(",\n\t\twhose element is\n\ttype = %s", a.typ.(interface{ Elem() types.Type }).Elem()))
case tlkField:
b.WriteString(fmt.Sprintf(",\n\t\twhose field `.%s` is of\n\ttype = %s", a.fieldName, a.typ))
case tlkDeref:
b.WriteString(fmt.Sprintf(",\n\t\twhich de-references into\n\ttype = %s", a.typ.(*types.Pointer).Elem()))
case tlkTypedef:
b.WriteString(fmt.Sprintf(",\n\t\twhich is a new type from\n\ttype = %s", a.typ.Underlying().String()))
}
}
return b.String()
}
func (r *typeSolver) throwAt(obj types.Object, args ...any) {
msg := fmt.Sprintf(args[0].(string), args[1:]...)
msg = fmt.Sprintf("%s%s", msg, r.printPath())
r.NameSolver.ThrowAt(obj, msg)
}
func (r *typeSolver) push(kind termLayerKind, typ types.Type, term ir.Term) *termLayer {
length := len(r.layers)
if length > 0 {
if length == 1 {
lasttermLayer := &r.layers[0]
if lastTerm, ok := lasttermLayer.term.(*ir.Coerce); ok {
term.(ir.HasIdent).SetIdent(lastTerm.Ident)
lasttermLayer.term = term
goto SKIP_ADD_CHILD
}
}
r.layers[length-1].term.AddChild(term)
SKIP_ADD_CHILD:
}
r.layers = append(r.layers, termLayer{kind: kind, typ: typ, term: term})
lasttermLayer := &r.layers[length]
if typ, ok := typ.(*types.Named); ok && kind == tlkTypedef {
uri := r.UriFor(typ.Obj())
if _, ok := r.visited[uri]; ok {
r.throwAt(typ.Obj(), "circular dependency: %s", uri)
return nil
}
r.visited[uri] = struct{}{}
}
return lasttermLayer
}
func (r *typeSolver) pop() {
length := len(r.layers)
lasttermLayer := r.layers[length-1]
r.layers = r.layers[:length-1]
if typ, ok := lasttermLayer.typ.(*types.Named); ok && lasttermLayer.kind == tlkTypedef {
uri := r.UriFor(typ.Obj())
delete(r.visited, uri)
}
if length == 1 {
r.Result = lasttermLayer.term
}
}
func (r *typeSolver) matchPattern(pat *regexp.Regexp) bool {
buf := bytes.Buffer{}
var prev ir.Term
for _, l := range r.layers {
t := l.term
if t == prev {
continue
}
buf.WriteByte(t.GetHeader().Abbr)
prev = t
}
return pat.Match(buf.Bytes())
}
func (r *typeSolver) isChanAllowedHere() bool {
if r.matchPattern(regexp.MustCompile("^(.*[^c]|)Sf$")) {
return r.config.IsPinnable
}
return r.matchPattern(regexp.MustCompile("^.*cSf$"))
}
func (r *typeSolver) isRootLevelStruct() bool {
return r.matchPattern(regexp.MustCompile("^[^S]*S$"))
}
func (r *typeSolver) isTypeNamed() bool {
var i int
var term ir.Term
for i = len(r.layers) - 1; i >= 0; i-- {
term = r.layers[i].term
if _, ok := term.(*ir.PtrTo); !ok {
if _, ok = term.(*ir.Coerce); ok {
return true
}
}
}
return false
}
func isDgoType(typ types.Type, subPath, name string) bool {
if named, ok := typ.(*types.Named); ok {
obj := named.Obj()
return obj.Pkg() != nil &&
obj.Pkg().Path() == "github.com/hsfzxjy/dgo/go"+subPath &&
obj.Name() == name
}
return false
}
func isPinMeta(typ types.Type) bool {
return isDgoType(typ, "/pin", "Meta")
}
func isOptional(typ types.Type) bool {
return isDgoType(typ, "", "Optional")
}
func isPinToken(typ types.Type) bool {
return isDgoType(typ, "/pin", "Token")
}