-
Notifications
You must be signed in to change notification settings - Fork 340
/
type.go
1627 lines (1520 loc) · 39.8 KB
/
type.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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package interp
import (
"fmt"
"go/constant"
"path/filepath"
"reflect"
"strconv"
)
// tcat defines interpreter type categories.
type tcat uint
// Types for go language.
const (
nilT tcat = iota
aliasT
arrayT
binT
binPkgT
boolT
builtinT
chanT
chanSendT
chanRecvT
complex64T
complex128T
errorT
float32T
float64T
funcT
interfaceT
intT
int8T
int16T
int32T
int64T
mapT
ptrT
srcPkgT
stringT
structT
uintT
uint8T
uint16T
uint32T
uint64T
uintptrT
valueT
variadicT
maxT
)
var cats = [...]string{
nilT: "nilT",
aliasT: "aliasT",
arrayT: "arrayT",
binT: "binT",
binPkgT: "binPkgT",
boolT: "boolT",
builtinT: "builtinT",
chanT: "chanT",
complex64T: "complex64T",
complex128T: "complex128T",
errorT: "errorT",
float32T: "float32",
float64T: "float64T",
funcT: "funcT",
interfaceT: "interfaceT",
intT: "intT",
int8T: "int8T",
int16T: "int16T",
int32T: "int32T",
int64T: "int64T",
mapT: "mapT",
ptrT: "ptrT",
srcPkgT: "srcPkgT",
stringT: "stringT",
structT: "structT",
uintT: "uintT",
uint8T: "uint8T",
uint16T: "uint16T",
uint32T: "uint32T",
uint64T: "uint64T",
uintptrT: "uintptrT",
valueT: "valueT",
variadicT: "variadicT",
}
func (c tcat) String() string {
if c < tcat(len(cats)) {
return cats[c]
}
return "Cat(" + strconv.Itoa(int(c)) + ")"
}
// structField type defines a field in a struct.
type structField struct {
name string
tag string
embed bool
typ *itype
}
// itype defines the internal representation of types in the interpreter.
type itype struct {
cat tcat // Type category
field []structField // Array of struct fields if structT or interfaceT
key *itype // Type of key element if MapT or nil
val *itype // Type of value element if chanT,chanSendT, chanRecvT, mapT, ptrT, aliasT, arrayT or variadicT
arg []*itype // Argument types if funcT or nil
ret []*itype // Return types if funcT or nil
method []*node // Associated methods or nil
name string // name of type within its package for a defined type
path string // for a defined type, the package import path
size int // Size of array if ArrayT
rtype reflect.Type // Reflection type if ValueT, or nil
incomplete bool // true if type must be parsed again (out of order declarations)
recursive bool // true if the type has an element which refer to itself
untyped bool // true for a literal value (string or number)
sizedef bool // true if array size is computed from type definition
isBinMethod bool // true if the type refers to a bin method function
node *node // root AST node of type definition
scope *scope // type declaration scope (in case of re-parse incomplete type)
}
// nodeType returns a type definition for the corresponding AST subtree.
func nodeType(interp *Interpreter, sc *scope, n *node) (*itype, error) {
if n.typ != nil && !n.typ.incomplete {
if n.kind == sliceExpr {
n.typ.sizedef = false
}
return n.typ, nil
}
var t = &itype{node: n, scope: sc}
if n.anc.kind == typeSpec {
name := n.anc.child[0].ident
if sym := sc.sym[name]; sym != nil {
// recover previously declared methods
t.method = sym.typ.method
t.path = sym.typ.path
t.name = name
}
}
var err error
switch n.kind {
case addressExpr, starExpr:
t.cat = ptrT
if t.val, err = nodeType(interp, sc, n.child[0]); err != nil {
return nil, err
}
t.incomplete = t.val.incomplete
case arrayType:
t.cat = arrayT
if len(n.child) > 1 {
v := n.child[0].rval
switch {
case v.IsValid():
// constant size
if isConstantValue(v.Type()) {
c := v.Interface().(constant.Value)
t.size = constToInt(c)
} else {
t.size = int(v.Int())
}
case n.child[0].kind == ellipsisExpr:
// [...]T expression
t.size = arrayTypeLen(n.anc)
default:
if sym, _, ok := sc.lookup(n.child[0].ident); ok {
// Resolve symbol to get size value
if sym.typ != nil && sym.typ.cat == intT {
if v, ok := sym.rval.Interface().(int); ok {
t.size = v
} else if c, ok := sym.rval.Interface().(constant.Value); ok {
t.size = constToInt(c)
} else {
t.incomplete = true
}
} else {
t.incomplete = true
}
} else {
// Evaluate constant array size expression
if _, err = interp.cfg(n.child[0], sc.pkgID); err != nil {
return nil, err
}
t.incomplete = true
}
}
if t.val, err = nodeType(interp, sc, n.child[1]); err != nil {
return nil, err
}
t.sizedef = true
t.incomplete = t.incomplete || t.val.incomplete
} else {
if t.val, err = nodeType(interp, sc, n.child[0]); err != nil {
return nil, err
}
t.incomplete = t.val.incomplete
}
case basicLit:
switch v := n.rval.Interface().(type) {
case bool:
t.cat = boolT
t.name = "bool"
case byte:
t.cat = uint8T
t.name = "uint8"
t.untyped = true
case complex64:
t.cat = complex64T
t.name = "complex64"
case complex128:
t.cat = complex128T
t.name = "complex128"
t.untyped = true
case float32:
t.cat = float32T
t.name = "float32"
t.untyped = true
case float64:
t.cat = float64T
t.name = "float64"
t.untyped = true
case int:
t.cat = intT
t.name = "int"
t.untyped = true
case uint:
t.cat = uintT
t.name = "uint"
t.untyped = true
case rune:
t.cat = int32T
t.name = "int32"
t.untyped = true
case string:
t.cat = stringT
t.name = "string"
t.untyped = true
case constant.Value:
switch v.Kind() {
case constant.Int:
t.cat = intT
t.name = "int"
t.untyped = true
case constant.Float:
t.cat = float64T
t.name = "float64"
t.untyped = true
case constant.Complex:
t.cat = complex128T
t.name = "complex128"
t.untyped = true
default:
err = n.cfgErrorf("missing support for type %v", n.rval)
}
default:
err = n.cfgErrorf("missing support for type %T: %v", v, n.rval)
}
case unaryExpr:
t, err = nodeType(interp, sc, n.child[0])
case binaryExpr:
// Get type of first operand.
if t, err = nodeType(interp, sc, n.child[0]); err != nil {
return nil, err
}
// For operators other than shift, get the type from the 2nd operand if the first is untyped.
if t.untyped && !isShiftNode(n) {
var t1 *itype
t1, err = nodeType(interp, sc, n.child[1])
if !(t1.untyped && isInt(t1.TypeOf()) && isFloat(t.TypeOf())) {
t = t1
}
}
// If the node is to be assigned or returned, the node type is the destination type.
dt := t
switch a := n.anc; {
case a.kind == defineStmt && len(a.child) > a.nleft+a.nright:
if dt, err = nodeType(interp, sc, a.child[a.nleft]); err != nil {
return nil, err
}
case a.kind == returnStmt:
dt = sc.def.typ.ret[childPos(n)]
}
if isInterface(dt) {
dt.val = t
}
t = dt
case callExpr:
if interp.isBuiltinCall(n) {
// Builtin types are special and may depend from their input arguments.
t.cat = builtinT
switch n.child[0].ident {
case "complex":
var nt0, nt1 *itype
if nt0, err = nodeType(interp, sc, n.child[1]); err != nil {
return nil, err
}
if nt1, err = nodeType(interp, sc, n.child[2]); err != nil {
return nil, err
}
if nt0.incomplete || nt1.incomplete {
t.incomplete = true
} else {
switch t0, t1 := nt0.TypeOf(), nt1.TypeOf(); {
case isFloat32(t0) && isFloat32(t1):
t = sc.getType("complex64")
case isFloat64(t0) && isFloat64(t1):
t = sc.getType("complex128")
case nt0.untyped && isNumber(t0) && nt1.untyped && isNumber(t1):
t = &itype{cat: valueT, rtype: complexType, scope: sc}
case nt0.untyped && isFloat32(t1) || nt1.untyped && isFloat32(t0):
t = sc.getType("complex64")
case nt0.untyped && isFloat64(t1) || nt1.untyped && isFloat64(t0):
t = sc.getType("complex128")
default:
err = n.cfgErrorf("invalid types %s and %s", t0.Kind(), t1.Kind())
}
if nt0.untyped && nt1.untyped {
t.untyped = true
}
}
case "real", "imag":
if t, err = nodeType(interp, sc, n.child[1]); err != nil {
return nil, err
}
if !t.incomplete {
switch k := t.TypeOf().Kind(); {
case k == reflect.Complex64:
t = sc.getType("float32")
case k == reflect.Complex128:
t = sc.getType("float64")
case t.untyped && isNumber(t.TypeOf()):
t = &itype{cat: valueT, rtype: floatType, untyped: true, scope: sc}
default:
err = n.cfgErrorf("invalid complex type %s", k)
}
}
case "cap", "copy", "len":
t = sc.getType("int")
case "append", "make":
t, err = nodeType(interp, sc, n.child[1])
case "new":
t, err = nodeType(interp, sc, n.child[1])
t = &itype{cat: ptrT, val: t, incomplete: t.incomplete, scope: sc}
case "recover":
t = sc.getType("interface{}")
}
if err != nil {
return nil, err
}
} else {
if t, err = nodeType(interp, sc, n.child[0]); err != nil {
return nil, err
}
switch t.cat {
case valueT:
if rt := t.rtype; rt.Kind() == reflect.Func && rt.NumOut() == 1 {
t = &itype{cat: valueT, rtype: rt.Out(0), scope: sc}
}
default:
if len(t.ret) == 1 {
t = t.ret[0]
}
}
}
case compositeLitExpr:
t, err = nodeType(interp, sc, n.child[0])
case chanType:
t.cat = chanT
if t.val, err = nodeType(interp, sc, n.child[0]); err != nil {
return nil, err
}
t.incomplete = t.val.incomplete
case chanTypeRecv:
t.cat = chanRecvT
if t.val, err = nodeType(interp, sc, n.child[0]); err != nil {
return nil, err
}
t.incomplete = t.val.incomplete
case chanTypeSend:
t.cat = chanSendT
if t.val, err = nodeType(interp, sc, n.child[0]); err != nil {
return nil, err
}
t.incomplete = t.val.incomplete
case ellipsisExpr:
t.cat = variadicT
if t.val, err = nodeType(interp, sc, n.child[0]); err != nil {
return nil, err
}
t.incomplete = t.val.incomplete
case funcLit:
t, err = nodeType(interp, sc, n.child[2])
case funcType:
t.cat = funcT
// Handle input parameters
for _, arg := range n.child[0].child {
cl := len(arg.child) - 1
typ, err := nodeType(interp, sc, arg.child[cl])
if err != nil {
return nil, err
}
t.arg = append(t.arg, typ)
for i := 1; i < cl; i++ {
// Several arguments may be factorized on the same field type
t.arg = append(t.arg, typ)
}
t.incomplete = t.incomplete || typ.incomplete
}
if len(n.child) == 2 {
// Handle returned values
for _, ret := range n.child[1].child {
cl := len(ret.child) - 1
typ, err := nodeType(interp, sc, ret.child[cl])
if err != nil {
return nil, err
}
t.ret = append(t.ret, typ)
for i := 1; i < cl; i++ {
// Several arguments may be factorized on the same field type
t.ret = append(t.ret, typ)
}
t.incomplete = t.incomplete || typ.incomplete
}
}
case identExpr:
sym, _, found := sc.lookup(n.ident)
if !found {
// retry with the filename, in case ident is a package name.
// TODO(mpl): try to move that into lookup instead?
baseName := filepath.Base(interp.fset.Position(n.pos).Filename)
ident := filepath.Join(n.ident, baseName)
sym, _, found = sc.lookup(ident)
if !found {
t.incomplete = true
sc.sym[n.ident] = &symbol{kind: typeSym, typ: t}
break
}
}
t = sym.typ
if t.incomplete && t.node != n {
m := t.method
if t, err = nodeType(interp, sc, t.node); err != nil {
return nil, err
}
t.method = m
sym.typ = t
}
if t.node == nil {
t.node = n
}
case indexExpr:
var lt *itype
if lt, err = nodeType(interp, sc, n.child[0]); err != nil {
return nil, err
}
if lt.incomplete {
t.incomplete = true
break
}
switch lt.cat {
case arrayT, mapT:
t = lt.val
}
case interfaceType:
t.cat = interfaceT
var incomplete bool
if sname := typeName(n); sname != "" {
if sym, _, found := sc.lookup(sname); found && sym.kind == typeSym {
sym.typ = t
}
}
for _, field := range n.child[0].child {
if len(field.child) == 1 {
typ, err := nodeType(interp, sc, field.child[0])
if err != nil {
return nil, err
}
t.field = append(t.field, structField{name: fieldName(field.child[0]), embed: true, typ: typ})
incomplete = incomplete || typ.incomplete
} else {
typ, err := nodeType(interp, sc, field.child[1])
if err != nil {
return nil, err
}
t.field = append(t.field, structField{name: field.child[0].ident, typ: typ})
incomplete = incomplete || typ.incomplete
}
}
t.incomplete = incomplete
case landExpr, lorExpr:
t.cat = boolT
case mapType:
t.cat = mapT
if t.key, err = nodeType(interp, sc, n.child[0]); err != nil {
return nil, err
}
if t.val, err = nodeType(interp, sc, n.child[1]); err != nil {
return nil, err
}
t.incomplete = t.key.incomplete || t.val.incomplete
case parenExpr:
t, err = nodeType(interp, sc, n.child[0])
case selectorExpr:
// Resolve the left part of selector, then lookup the right part on it
var lt *itype
// If we are in a list of func parameters, and we are a selector on a binPkgT, but
// one of the other parameters has the same name as the pkg name, in the list of
// symbols we would find the other parameter instead of the pkg because it comes
// first when looking up in the stack of scopes. So in that case we force the
// lookup directly in the root scope to shortcircuit that issue.
var localScope *scope
localScope = sc
if n.anc != nil && len(n.anc.child) > 1 && n.anc.child[1] == n &&
// This check is weaker than what we actually want to know, i.e. whether
// n.anc.child[0] is a variable, but it seems at this point in the run we have no
// way of knowing that yet (typ is nil, so there's no typ.cat yet).
n.anc.child[0].kind == identExpr {
for {
if localScope.level == 0 {
break
}
localScope = localScope.anc
}
}
if lt, err = nodeType(interp, localScope, n.child[0]); err != nil {
return nil, err
}
if lt.incomplete {
t.incomplete = true
break
}
name := n.child[1].ident
switch lt.cat {
case binPkgT:
pkg := interp.binPkg[lt.path]
if v, ok := pkg[name]; ok {
t.cat = valueT
t.rtype = v.Type()
if isBinType(v) { // a bin type is encoded as a pointer on nil value
t.rtype = t.rtype.Elem()
}
} else {
err = n.cfgErrorf("undefined selector %s.%s", lt.path, name)
panic(err)
}
case srcPkgT:
pkg := interp.srcPkg[lt.path]
if s, ok := pkg[name]; ok {
t = s.typ
} else {
err = n.cfgErrorf("undefined selector %s.%s", lt.path, name)
}
default:
if m, _ := lt.lookupMethod(name); m != nil {
t, err = nodeType(interp, sc, m.child[2])
} else if bm, _, _, ok := lt.lookupBinMethod(name); ok {
t = &itype{cat: valueT, rtype: bm.Type, isBinMethod: true, scope: sc}
} else if ti := lt.lookupField(name); len(ti) > 0 {
t = lt.fieldSeq(ti)
} else if bs, _, ok := lt.lookupBinField(name); ok {
t = &itype{cat: valueT, rtype: bs.Type, scope: sc}
} else {
err = lt.node.cfgErrorf("undefined selector %s", name)
}
}
case sliceExpr:
t, err = nodeType(interp, sc, n.child[0])
if t.cat == ptrT {
t = t.val
}
if err == nil && t.size != 0 {
t1 := *t
t1.size = 0
t1.rtype = nil
t = &t1
}
case structType:
t.cat = structT
var incomplete bool
if sname := typeName(n); sname != "" {
if sym, _, found := sc.lookup(sname); found && sym.kind == typeSym {
sym.typ = t
}
}
for _, c := range n.child[0].child {
switch {
case len(c.child) == 1:
typ, err := nodeType(interp, sc, c.child[0])
if err != nil {
return nil, err
}
t.field = append(t.field, structField{name: fieldName(c.child[0]), embed: true, typ: typ})
incomplete = incomplete || typ.incomplete
case len(c.child) == 2 && c.child[1].kind == basicLit:
tag := c.child[1].rval.String()
typ, err := nodeType(interp, sc, c.child[0])
if err != nil {
return nil, err
}
t.field = append(t.field, structField{name: fieldName(c.child[0]), embed: true, typ: typ, tag: tag})
incomplete = incomplete || typ.incomplete
default:
var tag string
l := len(c.child)
if c.lastChild().kind == basicLit {
tag = c.lastChild().rval.String()
l--
}
typ, err := nodeType(interp, sc, c.child[l-1])
if err != nil {
return nil, err
}
incomplete = incomplete || typ.incomplete
for _, d := range c.child[:l-1] {
t.field = append(t.field, structField{name: d.ident, typ: typ, tag: tag})
}
}
}
t.incomplete = incomplete
default:
err = n.cfgErrorf("type definition not implemented: %s", n.kind)
}
if err == nil && t.cat == nilT && !t.incomplete {
err = n.cfgErrorf("use of untyped nil %s", t.name)
}
return t, err
}
func (interp *Interpreter) isBuiltinCall(n *node) bool {
if n.kind != callExpr {
return false
}
s := interp.universe.sym[n.child[0].ident]
return s != nil && s.kind == bltnSym
}
// struct name returns the name of a struct type.
func typeName(n *node) string {
if n.anc.kind == typeSpec {
return n.anc.child[0].ident
}
return ""
}
// fieldName returns an implicit struct field name according to node kind.
func fieldName(n *node) string {
switch n.kind {
case selectorExpr:
return fieldName(n.child[1])
case starExpr:
return fieldName(n.child[0])
case identExpr:
return n.ident
default:
return ""
}
}
var zeroValues [maxT]reflect.Value
var okFor [aMax][maxT]bool
func init() {
zeroValues[boolT] = reflect.ValueOf(false)
zeroValues[complex64T] = reflect.ValueOf(complex64(0))
zeroValues[complex128T] = reflect.ValueOf(complex128(0))
zeroValues[errorT] = reflect.ValueOf(new(error)).Elem()
zeroValues[float32T] = reflect.ValueOf(float32(0))
zeroValues[float64T] = reflect.ValueOf(float64(0))
zeroValues[intT] = reflect.ValueOf(int(0))
zeroValues[int8T] = reflect.ValueOf(int8(0))
zeroValues[int16T] = reflect.ValueOf(int16(0))
zeroValues[int32T] = reflect.ValueOf(int32(0))
zeroValues[int64T] = reflect.ValueOf(int64(0))
zeroValues[stringT] = reflect.ValueOf("")
zeroValues[uintT] = reflect.ValueOf(uint(0))
zeroValues[uint8T] = reflect.ValueOf(uint8(0))
zeroValues[uint16T] = reflect.ValueOf(uint16(0))
zeroValues[uint32T] = reflect.ValueOf(uint32(0))
zeroValues[uint64T] = reflect.ValueOf(uint64(0))
zeroValues[uintptrT] = reflect.ValueOf(uintptr(0))
// Calculate the action -> type allowances
var (
okForEq [maxT]bool
okForCmp [maxT]bool
okForAdd [maxT]bool
okForAnd [maxT]bool
okForBool [maxT]bool
okForArith [maxT]bool
)
for cat := tcat(0); cat < maxT; cat++ {
if (cat >= intT && cat <= int64T) || (cat >= uintT && cat <= uintptrT) {
okForEq[cat] = true
okForCmp[cat] = true
okForAdd[cat] = true
okForAnd[cat] = true
okForArith[cat] = true
}
if cat == float32T || cat == float64T {
okForEq[cat] = true
okForCmp[cat] = true
okForAdd[cat] = true
okForArith[cat] = true
}
if cat == complex64T || cat == complex128T {
okForEq[cat] = true
okForAdd[cat] = true
okForArith[cat] = true
}
}
okForAdd[stringT] = true
okForBool[boolT] = true
okForEq[nilT] = true
okForEq[ptrT] = true
okForEq[interfaceT] = true
okForEq[errorT] = true
okForEq[chanT] = true
okForEq[stringT] = true
okForEq[boolT] = true
okForEq[mapT] = true // nil only
okForEq[funcT] = true // nil only
okForEq[arrayT] = true // array: only if element type is comparable slice: nil only
okForEq[structT] = true // only if all struct fields are comparable
okForCmp[stringT] = true
okFor[aAdd] = okForAdd
okFor[aAnd] = okForAnd
okFor[aLand] = okForBool
okFor[aAndNot] = okForAnd
okFor[aQuo] = okForArith
okFor[aEqual] = okForEq
okFor[aGreaterEqual] = okForCmp
okFor[aGreater] = okForCmp
okFor[aLowerEqual] = okForCmp
okFor[aLower] = okForCmp
okFor[aRem] = okForAnd
okFor[aMul] = okForArith
okFor[aNotEqual] = okForEq
okFor[aOr] = okForAnd
okFor[aLor] = okForBool
okFor[aSub] = okForArith
okFor[aXor] = okForAnd
okFor[aShl] = okForAnd
okFor[aShr] = okForAnd
okFor[aNeg] = okForArith
okFor[aNot] = okForBool
okFor[aPos] = okForArith
}
// Finalize returns a type pointer and error. It reparses a type from the
// partial AST if necessary (after missing dependecy data is available).
// If error is nil, the type is guarranteed to be completely defined and
// usable for CFG.
func (t *itype) finalize() (*itype, error) {
var err error
if t.incomplete {
sym, _, found := t.scope.lookup(t.name)
if found && !sym.typ.incomplete {
sym.typ.method = append(sym.typ.method, t.method...)
t.method = sym.typ.method
t.incomplete = false
return sym.typ, nil
}
m := t.method
if t, err = nodeType(t.node.interp, t.scope, t.node); err != nil {
return nil, err
}
if t.incomplete {
return nil, t.node.cfgErrorf("incomplete type %s", t.name)
}
t.method = m
t.node.typ = t
if sym != nil {
sym.typ = t
}
}
return t, err
}
// ReferTo returns true if the type contains a reference to a
// full type name. It allows to asses a type recursive status.
func (t *itype) referTo(name string, seen map[*itype]bool) bool {
if t.path+"/"+t.name == name {
return true
}
if seen[t] {
return false
}
seen[t] = true
switch t.cat {
case aliasT, arrayT, chanT, chanRecvT, chanSendT, ptrT:
return t.val.referTo(name, seen)
case funcT:
for _, a := range t.arg {
if a.referTo(name, seen) {
return true
}
}
for _, a := range t.ret {
if a.referTo(name, seen) {
return true
}
}
case mapT:
return t.key.referTo(name, seen) || t.val.referTo(name, seen)
case structT, interfaceT:
for _, f := range t.field {
if f.typ.referTo(name, seen) {
return true
}
}
}
return false
}
func (t *itype) numOut() int {
switch t.cat {
case funcT:
return len(t.ret)
case valueT:
if t.rtype.Kind() == reflect.Func {
return t.rtype.NumOut()
}
}
return 1
}
func (t *itype) concrete() *itype {
if isInterface(t) && t.val != nil {
return t.val.concrete()
}
return t
}
// IsRecursive returns true if type is recursive.
// Only a named struct or interface can be recursive.
func (t *itype) isRecursive() bool {
if t.name == "" {
return false
}
switch t.cat {
case structT, interfaceT:
for _, f := range t.field {
if f.typ.referTo(t.path+"/"+t.name, map[*itype]bool{}) {
return true
}
}
}
return false
}
// isComplete returns true if type definition is complete.
func (t *itype) isComplete() bool { return isComplete(t, map[string]bool{}) }
func isComplete(t *itype, visited map[string]bool) bool {
if t.incomplete {
return false
}
name := t.path + "/" + t.name
if visited[name] {
return !t.incomplete
}
if t.name != "" {
visited[name] = true
}
switch t.cat {
case aliasT, arrayT, chanT, chanRecvT, chanSendT, ptrT:
return isComplete(t.val, visited)
case funcT:
complete := true
for _, a := range t.arg {
complete = complete && isComplete(a, visited)
}
for _, a := range t.ret {
complete = complete && isComplete(a, visited)
}
return complete
case interfaceT, structT:
complete := true
for _, f := range t.field {
complete = complete && isComplete(f.typ, visited)
}
return complete
case mapT:
return isComplete(t.key, visited) && isComplete(t.val, visited)
case nilT:
return false
}
return true
}
// comparable returns true if the type is comparable.
func (t *itype) comparable() bool {
typ := t.TypeOf()
return t.cat == nilT || typ != nil && typ.Comparable()
}
// Equals returns true if the given type is identical to the receiver one.
func (t *itype) equals(o *itype) bool {
switch ti, oi := isInterface(t), isInterface(o); {
case ti && oi:
return t.methods().equals(o.methods())
case ti && !oi:
return o.methods().contains(t.methods())
case oi && !ti:
return t.methods().contains(o.methods())
default:
return t.id() == o.id()
}
}
// MethodSet defines the set of methods signatures as strings, indexed per method name.
type methodSet map[string]string
// Contains returns true if the method set m contains the method set n.
func (m methodSet) contains(n methodSet) bool {
for k, v := range n {
if m[k] != v {
return false
}
}
return true
}
// Equal returns true if the method set m is equal to the method set n.
func (m methodSet) equals(n methodSet) bool {
return m.contains(n) && n.contains(m)
}
// Methods returns a map of method type strings, indexed by method names.
func (t *itype) methods() methodSet {
res := make(methodSet)
switch t.cat {
case interfaceT:
// Get methods from recursive analysis of interface fields.
for _, f := range t.field {
if f.typ.cat == funcT {
res[f.name] = f.typ.TypeOf().String()
} else {
for k, v := range f.typ.methods() {
res[k] = v
}
}
}
case valueT, errorT:
// Get method from corresponding reflect.Type.
for i := t.rtype.NumMethod() - 1; i >= 0; i-- {
m := t.rtype.Method(i)
res[m.Name] = m.Type.String()
}
case ptrT:
for k, v := range t.val.methods() {
res[k] = v
}
case structT:
for _, f := range t.field {
for k, v := range f.typ.methods() {
res[k] = v
}
}
}
// Get all methods defined on this type.