-
Notifications
You must be signed in to change notification settings - Fork 6
/
types.go
684 lines (586 loc) · 15.9 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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
package types
import (
"fmt"
"sort"
"strings"
"github.com/gowebapi/webidlparser/ast"
)
type TypeRef interface {
link(conv *Convert, inuse inuseLogic) TypeRef
// Basic type infomation
Basic() BasicInfo
// Param type information
Param(nullable, option, vardic bool) (info *TypeInfo, inner TypeRef)
// DefaultParam return how this parameter should be processed by default
DefaultParam() (info *TypeInfo, inner TypeRef)
// the type is doing some allocation that needs manual release.
NeedRelease() bool
}
const BuiltInPackage = "<built-in>"
func convertType(in ast.Type, exrType *extractTypes) TypeRef {
ref := createRef(in, exrType)
var ret TypeRef
switch in := in.(type) {
case *ast.TypeName:
switch in.Name {
case "boolean":
ret = newPrimitiveType(in.Name, "bool", "Bool", false, false)
case "byte":
ret = newPrimitiveType(in.Name, "int", "Int", false, false)
case "octet":
ret = newPrimitiveType(in.Name, "int", "Int", false, false)
case "short":
ret = newPrimitiveType(in.Name, "int", "Int", false, true)
case "unsigned short":
ret = newPrimitiveType(in.Name, "int", "Int", false, true)
case "long":
ret = newPrimitiveType(in.Name, "int", "Int", false, false)
case "unsigned long":
ret = newPrimitiveType(in.Name, "uint", "Int", true, false)
case "long long":
ret = newPrimitiveType(in.Name, "int", "Int", false, false)
case "unsigned long long":
ret = newPrimitiveType(in.Name, "int", "Int", false, false)
case "float":
ret = newPrimitiveType(in.Name, "float32", "Float", true, true)
case "unrestricted float":
ret = newPrimitiveType(in.Name, "float32", "Float", true, true)
case "double":
ret = newPrimitiveType(in.Name, "float64", "Float", false, true)
case "unrestricted double":
ret = newPrimitiveType(in.Name, "float64", "Float", false, true)
case "void":
ret = newVoidType(in)
case "DOMString":
ret = newPrimitiveType(in.Name, "string", "String", false, false)
case "USVString":
ret = newPrimitiveType(in.Name, "string", "String", false, false)
default:
ret = newTypeNameRef(in, ref)
}
case *ast.AnyType:
ret = newAnyType()
case *ast.SequenceType:
elem := convertType(in.Elem, exrType)
if primitive, ok := elem.(*PrimitiveType); ok {
if primitive.supportTypedArray {
ret = newTypedArrayType(primitive)
}
}
if ret == nil {
ret = newSequenceType(elem)
}
case *ast.RecordType:
panic(fmt.Sprintf("support not implemented: input source line %d", in.Line))
case *ast.ParametrizedType:
var elems []TypeRef
for _, e := range in.Elems {
elems = append(elems, convertType(e, exrType))
}
// if in.Name = "Promise" && len(elems) == 1 {
// ret = newPromiseType(in, elems , ref )
// } else {
ret = newParametrizedType(in, in.Name, elems, ref)
// }
case *ast.UnionType:
ret = newUnionType(in, exrType)
case *ast.NullableType:
inner := convertType(in.Type, exrType)
ret = newNullableType(inner)
}
if ret == nil {
msg := fmt.Sprintf("unknown type %T: %#v", in, in)
panic(msg)
}
return ret
}
type basicType struct {
needRelease bool
}
func (t *basicType) NeedRelease() bool {
return t.needRelease
}
type AnyType struct {
basicType
}
var _ TypeRef = &AnyType{}
func newAnyType() *AnyType {
return &AnyType{
basicType: basicType{
needRelease: false,
},
}
}
func (t *AnyType) Basic() BasicInfo {
ret := BasicInfo{
Idl: "any",
Package: BuiltInPackage,
Def: "Any",
Internal: "<any>",
Template: "any",
}
return TransformBasic(t, ret)
}
func (t *AnyType) DefaultParam() (info *TypeInfo, inner TypeRef) {
return t.Param(false, false, false)
}
func (t *AnyType) link(conv *Convert, inuse inuseLogic) TypeRef {
return t
}
func (t *AnyType) Param(nullable, option, variadic bool) (info *TypeInfo, inner TypeRef) {
ret := &TypeInfo{
BasicInfo: t.Basic(),
Input: "interface{}",
VarIn: "interface{}",
VarInInner: "interface{}",
Output: "js.Value",
VarOut: "js.Value",
VarOutInner: "js.Value",
Pointer: false,
NeedRelease: false,
Nullable: false,
Option: option,
Variadic: variadic,
}
if variadic {
ret.Input = "..." + ret.Input
ret.VarIn = "[]" + ret.VarIn
ret.Output = "[]" + ret.Output
ret.VarOut = "[]" + ret.VarOut
}
return ret, t
}
type interfaceType struct {
basicType
If *Interface
}
// InterfaceType must implement TypeRef
var _ TypeRef = &interfaceType{}
func newInterfaceType(link *Interface) *interfaceType {
return &interfaceType{
basicType: basicType{
needRelease: false,
},
If: link,
}
}
func (t *interfaceType) Basic() BasicInfo {
panic("not supported for this type")
// return t.If.Basic()
}
func (t *interfaceType) DefaultParam() (info *TypeInfo, inner TypeRef) {
return t.Param(false, false, false)
}
func (t *interfaceType) link(conv *Convert, inuse inuseLogic) TypeRef {
return t.If
}
func (t *interfaceType) Param(nullable, option, variadic bool) (info *TypeInfo, inner TypeRef) {
panic("not supported for this type")
// return t.If.Param(nullable, option, variadic)
}
type nullableType struct {
Type TypeRef
}
var _ TypeRef = &nullableType{}
func newNullableType(inner TypeRef) *nullableType {
return &nullableType{Type: inner}
}
func (t *nullableType) Basic() BasicInfo {
return t.Type.Basic()
}
func (t *nullableType) DefaultParam() (info *TypeInfo, inner TypeRef) {
return t.Param(true, false, false)
}
func (t *nullableType) link(conv *Convert, inuse inuseLogic) TypeRef {
t.Type = t.Type.link(conv, inuse)
return t
}
func (t *nullableType) Param(nullable, option, variadic bool) (info *TypeInfo, inner TypeRef) {
return t.Type.Param(true, option, variadic)
}
func (t *nullableType) NeedRelease() bool {
return t.Type.NeedRelease()
}
// ParametrizedType is e.g. "Promise<any>"
type ParametrizedType struct {
*Ref
name string
ParamName string
Elems []TypeRef
Type TypeRef
}
var _ TypeRef = &ParametrizedType{}
func newParametrizedType(in *ast.ParametrizedType, name string, elems []TypeRef, ref *Ref) *ParametrizedType {
// what types are parameterized? only Promise or is there more?
if name != "Promise" && name != "FrozenArray" {
panic("parameterized type name: " + name)
}
return &ParametrizedType{
Ref: ref,
name: in.Name,
ParamName: name,
Elems: elems,
}
}
func (t *ParametrizedType) Basic() BasicInfo {
basic := t.Type.Basic()
basic.Template = "parametrized"
return basic
}
func (t *ParametrizedType) DefaultParam() (info *TypeInfo, inner TypeRef) {
return t.Param(false, false, false)
}
func (t *ParametrizedType) link(conv *Convert, inuse inuseLogic) TypeRef {
// names := []string{}
for i := range t.Elems {
inner := make(inuseLogic)
t.Elems[i] = t.Elems[i].link(conv, inner)
// names = append(names, t.Elems[i].Basic().Idl)
}
if len(t.Elems) != 1 {
conv.failing(t, "parameterized type support only 1 parameter, not %d", len(t.Elems))
}
candidate := getIdlName(t.ParamName)
if real, f := conv.Types[candidate]; f {
t.Type = real.link(conv, inuse)
} else {
conv.failing(t, "reference to unknown type '%s' (%s)", candidate, t.name)
return t
}
return t
}
func (t *ParametrizedType) Param(nullable, option, variadic bool) (info *TypeInfo, inner TypeRef) {
return newTypeInfo(t.Basic(), nullable, option, variadic, true, false, false), t
}
func (t *ParametrizedType) NeedRelease() bool {
for _, t := range t.Elems {
if t.NeedRelease() {
return true
}
}
return false
}
type PrimitiveType struct {
basicType
Idl string
Lang string
JsMethod string
Cast bool
// if this represent a primitive type that can be supported
// by TypedArray, e.g. int8, int, float32 etc
supportTypedArray bool
}
var _ TypeRef = &PrimitiveType{}
func newPrimitiveType(idl, lang, method string, cast, sta bool) *PrimitiveType {
return &PrimitiveType{
basicType: basicType{
needRelease: false,
},
Idl: idl,
Lang: lang,
JsMethod: method,
Cast: cast,
supportTypedArray: sta,
}
}
func (t *PrimitiveType) Basic() BasicInfo {
basic := BasicInfo{
Idl: t.Idl,
Package: BuiltInPackage,
Def: t.Lang,
Internal: "<primitive-internal-name>",
Template: "primitive",
}
return TransformBasic(t, basic)
}
func (t *PrimitiveType) DefaultParam() (info *TypeInfo, inner TypeRef) {
return t.Param(false, false, false)
}
func (t *PrimitiveType) link(conv *Convert, inuse inuseLogic) TypeRef {
return t
}
func (t *PrimitiveType) Param(nullable, option, variadic bool) (info *TypeInfo, inner TypeRef) {
return newTypeInfo(t.Basic(), nullable, option, variadic, false, false, false), t
}
// RawJSType used when no conversion should take place and
// the raw underlying js.Value should be returned or inserted
// instead
type RawJSType struct {
IdlType string
}
var _ TypeRef = &RawJSType{}
func NewRawJSType(idlType string) *RawJSType {
return &RawJSType{IdlType: idlType}
}
func (t *RawJSType) Basic() BasicInfo {
return BasicInfo{
Idl: t.IdlType,
Def: "js.Value",
Internal: "js.Value",
Package: BuiltInPackage,
Template: "rawjs",
}
}
func (t *RawJSType) DefaultParam() (info *TypeInfo, inner TypeRef) {
return t.Param(false, false, false)
}
func (t *RawJSType) link(conv *Convert, inuse inuseLogic) TypeRef {
return t
}
func (t *RawJSType) Param(nullable, option, variadic bool) (info *TypeInfo, inner TypeRef) {
return newTypeInfo(t.Basic(), nullable, option, variadic, false, false, false), t
}
func (t *RawJSType) NeedRelease() bool {
return false
}
type SequenceType struct {
Elem TypeRef
basic BasicInfo
}
var _ TypeRef = &SequenceType{}
func newSequenceType(elem TypeRef) *SequenceType {
ret := &SequenceType{
Elem: elem,
basic: BasicInfo{
Idl: "idl-sequence",
Package: BuiltInPackage,
Def: "def-sequence",
Internal: "internal-sequence",
Template: "sequence",
},
}
return ret
}
func (t *SequenceType) Basic() BasicInfo {
value := t.basic
eb := t.Elem.Basic()
_, prim := t.Elem.(*PrimitiveType)
_, enum := t.Elem.(*Enum)
_, cb := t.Elem.(*Callback)
if prim || enum || cb {
value.Def = "[]" + eb.Def
} else {
value.Def = "[]*" + eb.Def
}
value.Idl = "sequence<" + eb.Idl + ">"
// basic is already transformed and doesn't need to be done again
return value
}
func (t *SequenceType) DefaultParam() (info *TypeInfo, inner TypeRef) {
return t.Param(false, false, false)
}
func (t *SequenceType) link(conv *Convert, inuse inuseLogic) TypeRef {
inner := make(inuseLogic)
t.Elem = t.Elem.link(conv, inner)
if conv.HaveError {
// if we have failing, we can't evaluate Basic() as e.g.
// typeNameRef is returning faulty type when trigger an
// error
return t
}
return t
}
func (t *SequenceType) Param(nullable, option, variadic bool) (info *TypeInfo, inner TypeRef) {
info, _ = t.Elem.Param(false, false, false)
info.Idl = "sequence<" + info.Idl + ">"
info.Def = "[]" + info.Def
info.Package = t.basic.Package
info.Internal = t.basic.Internal
info.Template = t.basic.Template
info.Input = "[]" + info.Input
info.Output = "[]" + info.Output
info.VarIn = "[]" + info.VarIn
info.VarOut = "[]" + info.VarOut
info.NeedRelease = false
info.Pointer = false
info.Nullable = nullable
info.Option = option
info.Variadic = variadic
if variadic {
info.Input = "..." + info.Input
info.VarIn = "[]" + info.VarIn
info.Output = "[]" + info.Output
info.VarOut = "[]" + info.VarOut
}
if _, multi := t.Elem.(*SequenceType); multi {
info.VarInInner = "[]" + info.VarInInner
info.VarOutInner = "[]" + info.VarOutInner
}
return info, t
}
func (t *SequenceType) NeedRelease() bool {
return t.Elem.NeedRelease()
}
type TypedArrayType struct {
Elem *PrimitiveType
basic BasicInfo
}
var _ TypeRef = &TypedArrayType{}
func newTypedArrayType(primitive *PrimitiveType) *TypedArrayType {
return &TypedArrayType{
Elem: primitive,
basic: BasicInfo{
Idl: "typed-array",
Package: BuiltInPackage,
Def: "[]" + primitive.Lang,
Internal: "typed-array",
Template: "typedarray",
},
}
}
func (t *TypedArrayType) Basic() BasicInfo {
return TransformBasic(t, t.basic)
}
func (t *TypedArrayType) DefaultParam() (info *TypeInfo, inner TypeRef) {
return t.Param(false, false, false)
}
func (t *TypedArrayType) link(conv *Convert, inuse inuseLogic) TypeRef {
// assumes that PrimitiveType.link() doesn't do anything
return t
}
func (t *TypedArrayType) Param(nullable, option, variadic bool) (info *TypeInfo, inner TypeRef) {
return newTypeInfo(t.Basic(), nullable, option, variadic, false, false, false), t
}
func (t *TypedArrayType) NeedRelease() bool {
return false
}
type typeNameRef struct {
*Ref
name string
Underlying TypeRef
}
var _ TypeRef = &typeNameRef{}
func newTypeNameRef(in *ast.TypeName, ref *Ref) *typeNameRef {
return &typeNameRef{
name: in.Name,
Ref: ref,
}
}
func (t *typeNameRef) Basic() BasicInfo {
panic("not supported by this type")
// return t.Underlying.Basic()
}
func (t *typeNameRef) DefaultParam() (info *TypeInfo, inner TypeRef) {
return t.Param(false, false, false)
}
func (t *typeNameRef) link(conv *Convert, inuse inuseLogic) TypeRef {
candidate := getIdlName(t.name)
if real, f := conv.Types[candidate]; f {
t.Underlying = real.link(conv, inuse)
return t.Underlying
} else {
conv.failing(t, "reference to unknown type '%s' (%s)", candidate, t.name)
return t
}
}
func (t *typeNameRef) Param(nullable, option, variadic bool) (info *TypeInfo, inner TypeRef) {
panic("not supported by this type")
// return t.Underlying.Param(nullable, option, variadic)
}
func (t *typeNameRef) NeedRelease() bool {
return t.Underlying.NeedRelease()
}
type UnionType struct {
ref *Ref
name string
Types []TypeRef
basic BasicInfo
use bool
}
var _ TypeRef = &UnionType{}
func newUnionType(in *ast.UnionType, exrTypes *extractTypes) *UnionType {
ret := &UnionType{ref: createRef(in, exrTypes)}
for _, t := range in.Types {
ret.Types = append(ret.Types, convertType(t, exrTypes))
}
return ret
}
func (t *UnionType) Basic() BasicInfo {
return TransformBasic(t, t.basic)
}
func (t *UnionType) DefaultParam() (info *TypeInfo, inner TypeRef) {
return t.Param(false, false, false)
}
func (t *UnionType) lessThan(b *UnionType) bool {
if t.ref.Filename != b.ref.Filename {
return t.ref.Filename < b.ref.Filename
}
return t.ref.Line < b.ref.Line
}
func (t *UnionType) link(conv *Convert, inuse inuseLogic) TypeRef {
if t.use {
return t
}
t.use = true
conv.Unions = append(conv.Unions, t)
names := []string{}
for idx := range t.Types {
inner := make(inuseLogic)
t.Types[idx] = t.Types[idx].link(conv, inner)
n := toCamelCase(t.Basic().Idl, true)
names = append(names, n)
}
sort.Strings(names)
t.name = strings.Join(names, "")
t.basic = BasicInfo{
Idl: t.name + "Union",
Package: BuiltInPackage,
Def: t.name + "Union",
Internal: "union" + t.name,
Template: "union",
}
return t
}
func (t *UnionType) Param(nullable, option, variadic bool) (info *TypeInfo, inner TypeRef) {
return newTypeInfo(t.Basic(), nullable, option, variadic, true, false, false), t
}
func (t *UnionType) NeedRelease() bool {
for _, t := range t.Types {
if t.NeedRelease() {
return true
}
}
return false
}
type voidType struct {
basicType
}
var _ TypeRef = &voidType{}
func newVoidType(in *ast.TypeName) *voidType {
return &voidType{
basicType: basicType{
needRelease: false,
},
}
}
func (t *voidType) Basic() BasicInfo {
basic := BasicInfo{
Idl: "void",
Package: BuiltInPackage,
Def: "",
Internal: "void",
Template: "void",
}
return TransformBasic(t, basic)
}
func (t *voidType) DefaultParam() (info *TypeInfo, inner TypeRef) {
return t.Param(false, false, false)
}
func (t *voidType) link(conv *Convert, inuse inuseLogic) TypeRef {
return t
}
func (t *voidType) Param(nullable, option, variadic bool) (info *TypeInfo, inner TypeRef) {
return &TypeInfo{
BasicInfo: t.Basic(),
Input: "",
Output: "",
VarIn: "",
VarInInner: "",
VarOut: "",
VarOutInner: "",
NeedRelease: false,
Nullable: false,
Option: false,
Variadic: false,
}, t
}