forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
2537 lines (2320 loc) · 57.7 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
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 gno
import (
"fmt"
"reflect"
"sort"
"strconv"
"strings"
)
// NOTE: TypeID() implementations are currently
// experimental, and will probably get replaced with
// some other system. TypeID may become variable length,
// rather than contain a single Hashlet.
//----------------------------------------
// (runtime) Type
type Type interface {
assertType()
Kind() Kind // penetrates *DeclaredType & *NativeType
TypeID() TypeID // deterministic
String() string // for dev/debugging
Elem() Type // for TODO... types
GetPkgPath() string
}
type TypeID string
func (tid TypeID) IsZero() bool {
return tid == ""
}
func (tid TypeID) Bytes() []byte {
return []byte(tid)
}
func (tid TypeID) String() string {
return string(tid)
}
func typeid(f string, args ...interface{}) (tid TypeID) {
fs := fmt.Sprintf(f, args...)
x := TypeID(fs)
if debug {
debug.Println("TYPEID", fs)
}
return x
}
// Complex types are pointers, but due to the design goal
// of the language to enable mass scale persistence, we
// cannot use pointer equality to test for type equality.
// Instead, for checking equalty use the TypeID.
func (PrimitiveType) assertType() {}
func (*PointerType) assertType() {}
func (FieldType) assertType() {}
func (*ArrayType) assertType() {}
func (*SliceType) assertType() {}
func (*StructType) assertType() {}
func (*FuncType) assertType() {}
func (*MapType) assertType() {}
func (*InterfaceType) assertType() {}
func (*TypeType) assertType() {}
func (*DeclaredType) assertType() {}
func (*PackageType) assertType() {}
func (*ChanType) assertType() {}
func (*NativeType) assertType() {}
func (blockType) assertType() {}
func (*tupleType) assertType() {}
func (RefType) assertType() {}
func (MaybeNativeType) assertType() {}
//----------------------------------------
// Primitive types
type PrimitiveType int
const (
InvalidType PrimitiveType = 1 << iota
UntypedBoolType
BoolType
UntypedStringType
StringType
IntType
Int8Type
Int16Type
UntypedRuneType
Int32Type
Int64Type
UintType
Uint8Type
DataByteType
Uint16Type
Uint32Type
Uint64Type
// UintptrType
UntypedBigintType
BigintType
)
// Used for converting constant binary expressions.
// Smaller number means more specific.
// Spec: "If the untyped operands of a binary operation (other than a shift) are
// of different kinds, the result is of the operand's kind that appears later
// in this list: integer, rune, floating-point, complex. For example, an
// untyped integer constant divided by an untyped complex constant yields an
// untyped complex constant."
func (pt PrimitiveType) Specificity() int {
switch pt {
case InvalidType:
panic("invalid type has no specificity")
case BoolType:
return 0
case StringType:
return 0
case IntType:
return 0
case Int8Type:
return 0
case Int16Type:
return 0
case Int32Type:
return 0
case Int64Type:
return 0
case UintType:
return 0
case Uint8Type, DataByteType:
return 0
case Uint16Type:
return 0
case Uint32Type:
return 0
case Uint64Type:
return 0
case BigintType:
return 1
case UntypedBoolType:
return 2
case UntypedRuneType:
return 3
case UntypedStringType:
return 4
case UntypedBigintType:
return 4
default:
panic(fmt.Sprintf("unexpected primitive type %v", pt))
}
}
func (pt PrimitiveType) Kind() Kind {
switch pt {
case InvalidType:
panic("invalid type has no kind")
case BoolType, UntypedBoolType:
return BoolKind
case StringType, UntypedStringType:
return StringKind
case IntType:
return IntKind
case Int8Type:
return Int8Kind
case Int16Type:
return Int16Kind
case Int32Type, UntypedRuneType:
return Int32Kind
case Int64Type:
return Int64Kind
case UintType:
return UintKind
case Uint8Type, DataByteType:
return Uint8Kind
case Uint16Type:
return Uint16Kind
case Uint32Type:
return Uint32Kind
case Uint64Type:
return Uint64Kind
case BigintType, UntypedBigintType:
return BigintKind
default:
panic(fmt.Sprintf("unexpected primitive type %v", pt))
}
}
func (pt PrimitiveType) TypeID() TypeID {
switch pt {
case InvalidType:
panic("invalid type has no typeid")
case UntypedBoolType:
return typeid("<untyped> bool")
case BoolType:
return typeid("bool")
case UntypedStringType:
return typeid("<untyped> string")
case StringType:
return typeid("string")
case IntType:
return typeid("int")
case Int8Type:
return typeid("int8")
case Int16Type:
return typeid("int16")
case UntypedRuneType:
return typeid("<untyped> rune")
case Int32Type:
return typeid("int32")
case Int64Type:
return typeid("int64")
case UintType:
return typeid("uint")
case Uint8Type:
return typeid("uint8")
case DataByteType:
// should not be persisted...
panic("untyped data byte type has no typeid")
case Uint16Type:
return typeid("uint16")
case Uint32Type:
return typeid("uint32")
case Uint64Type:
return typeid("uint64")
case UntypedBigintType:
return typeid("<untyped> bigint")
case BigintType:
return typeid("bigint")
default:
panic(fmt.Sprintf("unexpected primitive type %v", pt))
}
}
func (pt PrimitiveType) String() string {
switch pt {
case InvalidType:
return string("<invalid type>")
case UntypedBoolType:
return string("<untyped> bool")
case BoolType:
return string("bool")
case UntypedStringType:
return string("<untyped> string")
case StringType:
return string("string")
case IntType:
return string("int")
case Int8Type:
return string("int8")
case Int16Type:
return string("int16")
case UntypedRuneType:
return string("<untyped> int32")
case Int32Type:
return string("int32")
case Int64Type:
return string("int64")
case UintType:
return string("uint")
case Uint8Type:
return string("uint8")
case DataByteType:
return string("<databyte> uint8")
case Uint16Type:
return string("uint16")
case Uint32Type:
return string("uint32")
case Uint64Type:
return string("uint64")
case UntypedBigintType:
return string("<untyped> bigint")
case BigintType:
return string("bigint")
default:
panic(fmt.Sprintf("unexpected primitive type %d", pt))
}
}
func (pt PrimitiveType) Elem() Type {
if pt.Kind() == StringKind {
// NOTE: this is different than Go1.
return Uint8Type
} else {
panic("non-string primitive types have no elements")
}
}
func (pt PrimitiveType) GetPkgPath() string {
return ""
}
//----------------------------------------
// Field type (partial)
type Tag string
type FieldType struct {
Name Name
Type Type
Embedded bool
Tag Tag
}
func (ft FieldType) Kind() Kind {
panic("FieldType is a pseudotype of unknown kind")
}
func (ft FieldType) TypeID() TypeID {
s := ""
if ft.Name == "" {
s += ft.Type.TypeID().String()
} else {
s += string(ft.Name) + " " + ft.Type.TypeID().String()
}
return typeid(s)
}
func (ft FieldType) String() string {
tag := ""
if ft.Tag != "" {
tag = " " + strconv.Quote(string(ft.Tag))
}
if ft.Name == "" {
return fmt.Sprintf("(embedded) %s%s", ft.Type.String(), tag)
} else {
return fmt.Sprintf("%s %s%s", ft.Name, ft.Type.String(), tag)
}
}
func (ft FieldType) Elem() Type {
panic("FieldType is a pseudotype with no elements")
}
func (ft FieldType) GetPkgPath() string {
panic("FieldType is a pseudotype with no package path")
}
//----------------------------------------
// FieldTypeList
type FieldTypeList []FieldType
// FieldTypeList implements sort.Interface.
func (l FieldTypeList) Len() int {
return len(l)
}
// FieldTypeList implements sort.Interface.
func (l FieldTypeList) Less(i, j int) bool {
iname, jname := l[i].Name, l[j].Name
if iname == jname {
panic(fmt.Sprintf("duplicate name found in field list: %s", iname))
}
return iname < jname
}
// FieldTypeList implements sort.Interface.
func (l FieldTypeList) Swap(i, j int) {
t := l[i]
l[i] = l[j]
l[j] = t
}
// User should call sort for interface methods.
// XXX how though?
func (l FieldTypeList) TypeID() TypeID {
ll := len(l)
s := ""
for i, ft := range l {
if ft.Name == "" {
s += ft.Type.TypeID().String()
} else {
s += string(ft.Name) + " " + ft.Type.TypeID().String()
}
if i != ll-1 {
s += ";"
}
}
return typeid(s)
}
// For use in fields of packages, structs, and interfaces, where any
// unexported lowercase fields are private and unequal to other package
// types.
func (l FieldTypeList) TypeIDForPackage(pkgPath string) TypeID {
ll := len(l)
s := ""
for i, ft := range l {
fn := ft.Name
if isUpper(string(fn)) {
s += string(fn) + " " + ft.Type.TypeID().String()
} else {
s += pkgPath + "." + string(fn) + " " + ft.Type.TypeID().String()
}
if i != ll-1 {
s += ";"
}
}
return typeid(s)
}
func (l FieldTypeList) HasUnexported() bool {
for _, ft := range l {
if debug {
if ft.Name == "" {
// incorrect usage.
panic("should not happen")
}
}
if !isUpper(string(ft.Name)) {
return true
}
}
return false
}
func (l FieldTypeList) String() string {
ll := len(l)
s := ""
for i, ft := range l {
s += string(ft.Name) + " " + ft.Type.TypeID().String()
if i != ll-1 {
s += ";"
}
}
return s
}
func (l FieldTypeList) StringWithCommas() string {
ll := len(l)
s := ""
for i, ft := range l {
s += string(ft.Name) + " " + ft.Type.String()
if i != ll-1 {
s += ","
}
}
return s
}
// Like TypeID() but without considering field names;
// used for function parameters and results.
func (l FieldTypeList) UnnamedTypeID() TypeID {
ll := len(l)
s := ""
for i, ft := range l {
s += ft.Type.TypeID().String()
if i != ll-1 {
s += ";"
}
}
return typeid(s)
}
func (l FieldTypeList) Types() []Type {
res := make([]Type, len(l))
for i, ft := range l {
res[i] = ft.Type
}
return res
}
//----------------------------------------
// Array type
type ArrayType struct {
Len int
Elt Type
Vrd bool
typeid TypeID
}
func (at *ArrayType) Kind() Kind {
return ArrayKind
}
func (at *ArrayType) TypeID() TypeID {
if at.typeid.IsZero() {
at.typeid = typeid("[%d]%s", at.Len, at.Elt.TypeID().String())
}
return at.typeid
}
func (at *ArrayType) String() string {
return fmt.Sprintf("[%d]%s", at.Len, at.Elt.String())
}
func (at *ArrayType) Elem() Type {
return at.Elt
}
func (at *ArrayType) GetPkgPath() string {
return ""
}
//----------------------------------------
// Slice type
var gByteSliceType = &SliceType{
Elt: Uint8Type,
Vrd: false,
}
type SliceType struct {
Elt Type
Vrd bool // used for *FuncType.HasVarg()
typeid TypeID
}
func (st *SliceType) Kind() Kind {
return SliceKind
}
func (st *SliceType) TypeID() TypeID {
if st.typeid.IsZero() {
if st.Vrd {
st.typeid = typeid("...%s", st.Elt.TypeID().String())
} else {
st.typeid = typeid("[]%s", st.Elt.TypeID().String())
}
}
return st.typeid
}
func (st *SliceType) String() string {
if st.Vrd {
return fmt.Sprintf("...%s", st.Elt.String())
} else {
return fmt.Sprintf("[]%s", st.Elt.String())
}
}
func (st *SliceType) Elem() Type {
return st.Elt
}
func (st *SliceType) GetPkgPath() string {
return ""
}
//----------------------------------------
// Pointer type
type PointerType struct {
Elt Type
typeid TypeID
}
func (pt *PointerType) Kind() Kind {
return PointerKind
}
func (pt *PointerType) TypeID() TypeID {
if pt.typeid.IsZero() {
pt.typeid = typeid("*%s", pt.Elt.TypeID().String())
}
return pt.typeid
}
func (pt *PointerType) String() string {
if pt == nil {
panic("invalid nil pointer type")
} else if pt.Elt == nil {
panic("invalid nil pointer element type")
} else {
return fmt.Sprintf("*%v", pt.Elt)
}
}
func (pt *PointerType) Elem() Type {
return pt.Elt
}
func (pt *PointerType) GetPkgPath() string {
return pt.Elt.GetPkgPath()
}
func (pt *PointerType) FindEmbeddedFieldType(callerPath string, n Name, m map[Type]struct{}) (
trail []ValuePath, hasPtr bool, rcvr Type, field Type, accessError bool,
) {
// Recursion guard.
if m == nil {
m = map[Type]struct{}{pt: (struct{}{})}
} else if _, exists := m[pt]; exists {
return nil, false, nil, nil, false
} else {
m[pt] = struct{}{}
}
// ...
switch cet := pt.Elt.(type) {
case *DeclaredType, *StructType:
// Pointer to declared types and structs
// expose embedded methods and fields.
// See tests/selector_test.go for examples.
trail, hasPtr, rcvr, field, accessError = findEmbeddedFieldType(callerPath, cet, n, m)
if trail != nil { // found
hasPtr = true // pt *is* a pointer.
switch trail[0].Type {
case VPField:
// Case 1: If trail is of form [VPField, VPField, ... VPPtrMethod],
// that is, one or more fields followed by a pointer method,
// convert to [VPSubrefField, VPSubrefField, ... VPDerefPtrMethod].
if func() bool {
for i, path := range trail {
if i < len(trail)-1 {
if path.Type != VPField {
return false
}
} else {
if path.Type != VPPtrMethod {
return false
}
}
}
return true
}() {
for i := range trail {
if i < len(trail)-1 {
trail[i].Type = VPSubrefField
} else {
trail[i].Type = VPDerefPtrMethod
}
}
return
} else {
// Case 2: otherwise, is just a deref field.
trail[0].Type = VPDerefField
switch trail[0].Depth {
case 0:
// *PointerType > *StructType.Field has depth 0.
case 1:
// *DeclaredType > *StructType.Field has depth 1 (& type VPField).
// *PointerType > *DeclaredType > *StructType.Field has depth 2.
trail[0].Depth = 2
/*
// If trail[-1].Type == VPPtrMethod, set VPDerefPtrMethod.
if len(trail) > 1 && trail[1].Type == VPPtrMethod {
trail[1].Type = VPDerefPtrMethod
}
*/
default:
panic("should not happen")
}
return
}
case VPValMethod:
trail[0].Type = VPDerefValMethod
return
case VPPtrMethod:
trail[0].Type = VPDerefPtrMethod
return
case VPDerefValMethod, VPDerefPtrMethod:
panic("should not happen")
default:
panic("should not happen")
}
} else { // not found
return
}
case *NativeType:
npt := &NativeType{
Type: reflect.PtrTo(cet.Type),
}
return npt.FindEmbeddedFieldType(n, m)
default:
// nester pointers or pointer to interfaces
// and other pointer types do not expose their methods.
return
}
}
//----------------------------------------
// Struct type
type StructType struct {
PkgPath string
Fields []FieldType
typeid TypeID
}
func (st *StructType) Kind() Kind {
return StructKind
}
func (st *StructType) TypeID() TypeID {
if st.typeid.IsZero() {
// NOTE Struct types expressed or declared in different packages
// may have the same TypeID if and only if neither have
// unexported fields. st.PkgPath is only included in field
// names that are not uppercase.
st.typeid = typeid(
"struct{%s}",
FieldTypeList(st.Fields).TypeIDForPackage(st.PkgPath),
)
}
return st.typeid
}
func (st *StructType) String() string {
return fmt.Sprintf("struct{%s}",
FieldTypeList(st.Fields).String())
}
func (st *StructType) Elem() Type {
panic("struct types have no (universal) elements")
}
func (st *StructType) GetPkgPath() string {
return st.PkgPath
}
// NOTE only works for exposed non-embedded fields.
func (st *StructType) GetPathForName(n Name) ValuePath {
for i := 0; i < len(st.Fields); i++ {
ft := st.Fields[i]
if ft.Name == n {
if i > 2<<16-1 {
panic("too many fields")
}
return NewValuePathField(0, uint16(i), n)
}
if st, ok := ft.Type.(*StructType); ok {
if ft.Name != "" {
// skip fields not promoted.
i += len(st.Fields)
}
}
}
panic(fmt.Sprintf("struct type %s has no field %s",
st.String(), n))
}
func (st *StructType) GetStaticTypeOfAt(path ValuePath) Type {
if debug {
if path.Depth != 0 {
panic("expected path.Depth of 0")
}
}
return st.Fields[path.Index].Type
}
// Searches embedded fields to find matching method or field,
// which may be embedded. This function is slow. DeclaredType uses
// this. There is probably no need to cache positive results here;
// it may be better to implement it on DeclaredType. The resulting
// ValuePaths may be modified. If not found, all returned values
// are nil; for consistency, check the trail.
func (st *StructType) FindEmbeddedFieldType(callerPath string, n Name, m map[Type]struct{}) (
trail []ValuePath, hasPtr bool, rcvr Type, field Type, accessError bool,
) {
// Recursion guard
if m == nil {
m = map[Type]struct{}{st: (struct{}{})}
} else if _, exists := m[st]; exists {
return nil, false, nil, nil, false
} else {
m[st] = struct{}{}
}
// Search fields.
for i := 0; i < len(st.Fields); i++ {
sf := &st.Fields[i]
// Maybe is a field of the struct.
if sf.Name == n {
// Ensure exposed or package match.
if !isUpper(string(n)) && st.PkgPath != callerPath {
return nil, false, nil, nil, true
}
vp := NewValuePathField(0, uint16(i), n)
return []ValuePath{vp}, false, nil, sf.Type, false
}
// Maybe is embedded within a field.
if sf.Embedded {
st := sf.Type
trail2, hasPtr2, rcvr2, field2, accessError2 := findEmbeddedFieldType(callerPath, st, n, m)
if accessError2 {
// XXX make test case and check against go
return nil, false, nil, nil, true
} else if trail2 != nil {
if trail != nil {
// conflict detected. return none.
return nil, false, nil, nil, false
} else {
// remember.
vp := NewValuePathField(0, uint16(i), sf.Name)
trail, hasPtr, rcvr, field = append([]ValuePath{vp}, trail2...), hasPtr2, rcvr2, field2
}
}
}
}
return // may be found or nil.
}
//----------------------------------------
// Package type
// The package type holds no data.
// The PackageNode holds static declarations,
// and the PackageValue embeds a block.
var gPackageType = &PackageType{}
type PackageType struct {
typeid TypeID
}
func (pt *PackageType) Kind() Kind {
return PackageKind
}
func (pt *PackageType) TypeID() TypeID {
if pt.typeid.IsZero() {
// NOTE Different package types may have the same
// TypeID if and only if neither have unexported fields.
// pt.Path is only included in field names that are not
// uppercase.
pt.typeid = typeid("package{}")
}
return pt.typeid
}
func (pt *PackageType) String() string {
return "package{}"
}
func (pt *PackageType) Elem() Type {
panic("package types have no elements")
}
func (pt *PackageType) GetPkgPath() string {
panic("package types has no package path (unlike package values)")
}
//----------------------------------------
// Interface type
type InterfaceType struct {
PkgPath string
Methods []FieldType
Generic Name // for uverse "generics"
typeid TypeID
}
// General empty interface.
var gEmptyInterfaceType *InterfaceType = &InterfaceType{}
func (it *InterfaceType) IsEmptyInterface() bool {
return len(it.Methods) == 0
}
func (it *InterfaceType) Kind() Kind {
return InterfaceKind
}
func (it *InterfaceType) TypeID() TypeID {
if debug {
if it.Generic != "" {
panic("generic type has no TypeID")
}
}
if it.typeid.IsZero() {
// NOTE Interface types expressed or declared in different
// packages may have the same TypeID if and only if
// neither have unexported fields. pt.Path is only
// included in field names that are not uppercase.
ms := FieldTypeList(it.Methods)
// XXX pre-sort.
sort.Sort(ms)
it.typeid = typeid("interface{" + ms.TypeIDForPackage(it.PkgPath).String() + "}")
}
return it.typeid
}
func (it *InterfaceType) String() string {
if it.Generic != "" {
return fmt.Sprintf("<%s>{%s}",
it.Generic,
FieldTypeList(it.Methods).String())
} else {
return fmt.Sprintf("interface{%s}",
FieldTypeList(it.Methods).String())
}
}
func (it *InterfaceType) Elem() Type {
panic("interface types have no elements")
}
func (it *InterfaceType) GetPkgPath() string {
return it.PkgPath
}
func (it *InterfaceType) FindEmbeddedFieldType(callerPath string, n Name, m map[Type]struct{}) (
trail []ValuePath, hasPtr bool, rcvr Type, ft Type, accessError bool,
) {
// Recursion guard
if m == nil {
m = map[Type]struct{}{it: (struct{}{})}
} else if _, exists := m[it]; exists {
return nil, false, nil, nil, false
} else {
m[it] = struct{}{}
}
// ...
for _, im := range it.Methods {
if im.Name == n {
// Ensure exposed or package match.
if !isUpper(string(n)) && it.PkgPath != callerPath {
return nil, false, nil, nil, true
}
// a matched name cannot be an embedded interface.
if im.Type.Kind() == InterfaceKind {
return nil, false, nil, nil, false
}
// match found.
tr := []ValuePath{NewValuePathInterface(n)}
hasPtr := false
rcvr := Type(nil)
ft := im.Type
return tr, hasPtr, rcvr, ft, false
}
if et, ok := baseOf(im.Type).(*InterfaceType); ok {
// embedded interfaces must be recursively searched.
trail, hasPtr, rcvr, ft, accessError = et.FindEmbeddedFieldType(callerPath, n, m)
if accessError {
// XXX make test case and check against go
return nil, false, nil, nil, true
} else if trail != nil {
if debug {
if len(trail) != 1 || trail[0].Type != VPInterface {
panic("should not happen")
}
}
return trail, hasPtr, rcvr, ft, false
} // else continue search.
} // else continue search.
}
return nil, false, nil, nil, false
}
// For run-time type assertion.
// TODO: optimize somehow.
func (it *InterfaceType) IsImplementedBy(ot Type) (result bool) {
for _, im := range it.Methods {
if im.Type.Kind() == InterfaceKind {
// field is embedded interface...
im2 := baseOf(im.Type).(*InterfaceType)
if !im2.IsImplementedBy(ot) {
return false
} else {
continue
}
}
// find method in field.
tr, hp, rt, ft, _ := findEmbeddedFieldType(it.PkgPath, ot, im.Name, nil)
if tr == nil { // not found.
return false
}
if nft, ok := ft.(*NativeType); ok {
// Treat native function types as autoNative calls.
// ft: possibly gonative function type.
// gnot: the corresponding gno type (GnoType()).
// im.Type: the desired interface gno type.
// ie, if each of ft's arg types can match
// against the desired arg types in im.Types.
if !gno2GoTypeMatches(im.Type, nft.Type) {
return false
}
} else if mt, ok := ft.(*FuncType); ok {
// if method is pointer receiver, check addressability:
if _, ptrRcvr := rt.(*PointerType); ptrRcvr && !hp {
return false // not addressable.
}
// check for func type equality.
dmtid := mt.TypeID()
imtid := im.Type.TypeID()
if dmtid != imtid {
return false
}
}
}
return true
}
func (it *InterfaceType) GetPathForName(n Name) ValuePath {
return NewValuePathInterface(n)
}
//----------------------------------------
// Chan type
type ChanType struct {
Dir ChanDir
Elt Type
typeid TypeID
}