This repository has been archived by the owner on Apr 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
type.go
1292 lines (1071 loc) · 28 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
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package eval
import (
"go/ast"
"go/token"
"log"
"math/big"
"reflect"
"sort"
"unsafe" // For Sizeof
)
// XXX(Spec) The type compatibility section is very confusing because
// it makes it seem like there are three distinct types of
// compatibility: plain compatibility, assignment compatibility, and
// comparison compatibility. As I understand it, there's really only
// assignment compatibility and comparison and conversion have some
// restrictions and have special meaning in some cases where the types
// are not otherwise assignment compatible. The comparison
// compatibility section is almost all about the semantics of
// comparison, not the type checking of it, so it would make much more
// sense in the comparison operators section. The compatibility and
// assignment compatibility sections should be rolled into one.
type Type interface {
// compat returns whether this type is compatible with another
// type. If conv is false, this is normal compatibility,
// where two named types are compatible only if they are the
// same named type. If conv if true, this is conversion
// compatibility, where two named types are conversion
// compatible if their definitions are conversion compatible.
//
// TODO(austin) Deal with recursive types
compat(o Type, conv bool) bool
// lit returns this type's literal. If this is a named type,
// this is the unnamed underlying type. Otherwise, this is an
// identity operation.
lit() Type
// isBoolean returns true if this is a boolean type.
isBoolean() bool
// isInteger returns true if this is an integer type.
isInteger() bool
// isFloat returns true if this is a floating type.
isFloat() bool
// isIdeal returns true if this is an ideal int or float.
isIdeal() bool
// Zero returns a new zero value of this type.
Zero() Value
// String returns the string representation of this type.
String() string
// The position where this type was defined, if any.
//Pos() token.Pos
}
type BoundedType interface {
Type
// minVal returns the smallest value of this type.
minVal() *big.Rat
// maxVal returns the largest value of this type.
maxVal() *big.Rat
}
var universePos = token.NoPos
/*
* Type array maps. These are used to memoize composite types.
*/
type typeArrayMapEntry struct {
key []Type
v interface{}
next *typeArrayMapEntry
}
type typeArrayMap map[uintptr]*typeArrayMapEntry
func hashTypeArray(key []Type) uintptr {
hash := uintptr(0)
for _, t := range key {
hash = hash * 33
if t == nil {
continue
}
addr := reflect.ValueOf(t).Pointer()
hash ^= addr
}
return hash
}
func newTypeArrayMap() typeArrayMap { return make(map[uintptr]*typeArrayMapEntry) }
func (m typeArrayMap) Get(key []Type) interface{} {
ent, ok := m[hashTypeArray(key)]
if !ok {
return nil
}
nextEnt:
for ; ent != nil; ent = ent.next {
if len(key) != len(ent.key) {
continue
}
for i := 0; i < len(key); i++ {
if key[i] != ent.key[i] {
continue nextEnt
}
}
// Found it
return ent.v
}
return nil
}
func (m typeArrayMap) Put(key []Type, v interface{}) interface{} {
hash := hashTypeArray(key)
ent := m[hash]
new := &typeArrayMapEntry{key, v, ent}
m[hash] = new
return v
}
/*
* Common type
*/
type commonType struct{}
func (commonType) isBoolean() bool { return false }
func (commonType) isInteger() bool { return false }
func (commonType) isFloat() bool { return false }
func (commonType) isIdeal() bool { return false }
/*
* Package
*/
type packageField struct {
Name string
Type Type
}
type packageType struct {
commonType
Elems []packageField
}
var packageTypes = make(map[string]*packageType)
func newPackageType(path string, fields []packageField) *packageType {
t, ok := packageTypes[path]
if !ok {
t = &packageType{commonType{}, fields}
packageTypes[path] = t
}
return t
}
func (p *packageType) compat(o Type, conv bool) bool { return false }
func (p *packageType) lit() Type { return p }
func (p *packageType) String() string {
// Use angle brackets as a convention for printing the
// underlying, unnamed type. This should only show up in
// debug output.
return "<package>"
}
func (p *packageType) Zero() Value {
pkg := packageV{}
pkg.name = ""
pkg.idents = make([]Value, 0)
return &pkg
}
/*
* Bool
*/
type boolType struct {
commonType
}
var BoolType = universe.DefineType("bool", universePos, &boolType{})
func (t *boolType) compat(o Type, conv bool) bool {
_, ok := o.lit().(*boolType)
return ok
}
func (t *boolType) lit() Type { return t }
func (t *boolType) isBoolean() bool { return true }
func (boolType) String() string {
// Use angle brackets as a convention for printing the
// underlying, unnamed type. This should only show up in
// debug output.
return "<bool>"
}
func (t *boolType) Zero() Value {
res := boolV(false)
return &res
}
/*
* Uint
*/
type uintType struct {
commonType
// 0 for architecture-dependent types
Bits uint
// true for uintptr, false for all others
Ptr bool
name string
}
var (
Uint8Type = universe.DefineType("uint8", universePos, &uintType{commonType{}, 8, false, "uint8"})
Uint16Type = universe.DefineType("uint16", universePos, &uintType{commonType{}, 16, false, "uint16"})
Uint32Type = universe.DefineType("uint32", universePos, &uintType{commonType{}, 32, false, "uint32"})
Uint64Type = universe.DefineType("uint64", universePos, &uintType{commonType{}, 64, false, "uint64"})
UintType = universe.DefineType("uint", universePos, &uintType{commonType{}, 0, false, "uint"})
UintptrType = universe.DefineType("uintptr", universePos, &uintType{commonType{}, 0, true, "uintptr"})
)
func (t *uintType) compat(o Type, conv bool) bool {
t2, ok := o.lit().(*uintType)
return ok && t == t2
}
func (t *uintType) lit() Type { return t }
func (t *uintType) isInteger() bool { return true }
func (t *uintType) String() string { return "<" + t.name + ">" }
func (t *uintType) Zero() Value {
switch t.Bits {
case 0:
if t.Ptr {
res := uintptrV(0)
return &res
} else {
res := uintV(0)
return &res
}
case 8:
res := uint8V(0)
return &res
case 16:
res := uint16V(0)
return &res
case 32:
res := uint32V(0)
return &res
case 64:
res := uint64V(0)
return &res
}
panic("unexpected uint bit count")
}
func (t *uintType) minVal() *big.Rat { return big.NewRat(0, 1) }
func (t *uintType) maxVal() *big.Rat {
bits := t.Bits
if bits == 0 {
if t.Ptr {
bits = uint(8 * unsafe.Sizeof(uintptr(0)))
} else {
bits = uint(8 * unsafe.Sizeof(uint(0)))
}
}
numer := big.NewInt(1)
numer.Lsh(numer, bits)
numer.Sub(numer, idealOne)
return new(big.Rat).SetInt(numer)
}
/*
* Int
*/
type intType struct {
commonType
// XXX(Spec) Numeric types: "There is also a set of
// architecture-independent basic numeric types whose size
// depends on the architecture." Should that be
// architecture-dependent?
// 0 for architecture-dependent types
Bits uint
name string
}
var (
Int8Type = universe.DefineType("int8", universePos, &intType{commonType{}, 8, "int8"})
Int16Type = universe.DefineType("int16", universePos, &intType{commonType{}, 16, "int16"})
Int32Type = universe.DefineType("int32", universePos, &intType{commonType{}, 32, "int32"})
Int64Type = universe.DefineType("int64", universePos, &intType{commonType{}, 64, "int64"})
IntType = universe.DefineType("int", universePos, &intType{commonType{}, 0, "int"})
)
func (t *intType) compat(o Type, conv bool) bool {
t2, ok := o.lit().(*intType)
return ok && t == t2
}
func (t *intType) lit() Type { return t }
func (t *intType) isInteger() bool { return true }
func (t *intType) String() string { return "<" + t.name + ">" }
func (t *intType) Zero() Value {
switch t.Bits {
case 8:
res := int8V(0)
return &res
case 16:
res := int16V(0)
return &res
case 32:
res := int32V(0)
return &res
case 64:
res := int64V(0)
return &res
case 0:
res := intV(0)
return &res
}
panic("unexpected int bit count")
}
func (t *intType) minVal() *big.Rat {
bits := t.Bits
if bits == 0 {
bits = uint(8 * unsafe.Sizeof(int(0)))
}
numer := big.NewInt(-1)
numer.Lsh(numer, bits-1)
return new(big.Rat).SetInt(numer)
}
func (t *intType) maxVal() *big.Rat {
bits := t.Bits
if bits == 0 {
bits = uint(8 * unsafe.Sizeof(int(0)))
}
numer := big.NewInt(1)
numer.Lsh(numer, bits-1)
numer.Sub(numer, idealOne)
return new(big.Rat).SetInt(numer)
}
/*
* Ideal int
*/
type idealIntType struct {
commonType
}
var IdealIntType Type = &idealIntType{}
func (t *idealIntType) compat(o Type, conv bool) bool {
_, ok := o.lit().(*idealIntType)
return ok
}
func (t *idealIntType) lit() Type { return t }
func (t *idealIntType) isInteger() bool { return true }
func (t *idealIntType) isIdeal() bool { return true }
func (t *idealIntType) String() string { return "ideal integer" }
func (t *idealIntType) Zero() Value { return &idealIntV{idealZero} }
/*
* Float
*/
type floatType struct {
commonType
// 0 for architecture-dependent type
Bits uint
name string
}
var (
Float32Type = universe.DefineType("float32", universePos, &floatType{commonType{}, 32, "float32"})
Float64Type = universe.DefineType("float64", universePos, &floatType{commonType{}, 64, "float64"})
)
func (t *floatType) compat(o Type, conv bool) bool {
t2, ok := o.lit().(*floatType)
return ok && t == t2
}
func (t *floatType) lit() Type { return t }
func (t *floatType) isFloat() bool { return true }
func (t *floatType) String() string { return "<" + t.name + ">" }
func (t *floatType) Zero() Value {
switch t.Bits {
case 32:
res := float32V(0)
return &res
case 64:
res := float64V(0)
return &res
}
panic("unexpected float bit count")
}
var maxFloat32Val *big.Rat
var maxFloat64Val *big.Rat
var minFloat32Val *big.Rat
var minFloat64Val *big.Rat
func (t *floatType) minVal() *big.Rat {
bits := t.Bits
switch bits {
case 32:
return minFloat32Val
case 64:
return minFloat64Val
}
log.Panicf("unexpected floating point bit count: %d", bits)
panic("unreachable")
}
func (t *floatType) maxVal() *big.Rat {
bits := t.Bits
switch bits {
case 32:
return maxFloat32Val
case 64:
return maxFloat64Val
}
log.Panicf("unexpected floating point bit count: %d", bits)
panic("unreachable")
}
/*
* Ideal float
*/
type idealFloatType struct {
commonType
}
var IdealFloatType Type = &idealFloatType{}
func (t *idealFloatType) compat(o Type, conv bool) bool {
_, ok := o.lit().(*idealFloatType)
return ok
}
func (t *idealFloatType) lit() Type { return t }
func (t *idealFloatType) isFloat() bool { return true }
func (t *idealFloatType) isIdeal() bool { return true }
func (t *idealFloatType) String() string { return "ideal float" }
func (t *idealFloatType) Zero() Value { return &idealFloatV{big.NewRat(0, 1)} }
/*
* String
*/
type stringType struct {
commonType
}
var StringType = universe.DefineType("string", universePos, &stringType{})
func (t *stringType) compat(o Type, conv bool) bool {
_, ok := o.lit().(*stringType)
return ok
}
func (t *stringType) lit() Type { return t }
func (t *stringType) String() string { return "<string>" }
func (t *stringType) Zero() Value {
res := stringV("")
return &res
}
/*
* Array
*/
type ArrayType struct {
commonType
Len int64
Elem Type
}
var arrayTypes = make(map[int64]map[Type]*ArrayType)
// Two array types are identical if they have identical element types
// and the same array length.
func NewArrayType(len int64, elem Type) *ArrayType {
ts, ok := arrayTypes[len]
if !ok {
ts = make(map[Type]*ArrayType)
arrayTypes[len] = ts
}
t, ok := ts[elem]
if !ok {
t = &ArrayType{commonType{}, len, elem}
ts[elem] = t
}
return t
}
func (t *ArrayType) compat(o Type, conv bool) bool {
t2, ok := o.lit().(*ArrayType)
if !ok {
return false
}
return t.Len == t2.Len && t.Elem.compat(t2.Elem, conv)
}
func (t *ArrayType) lit() Type { return t }
func (t *ArrayType) String() string { return "[]" + t.Elem.String() }
func (t *ArrayType) Zero() Value {
res := arrayV(make([]Value, t.Len))
// TODO(austin) It's unfortunate that each element is
// separately heap allocated. We could add ZeroArray to
// everything, though that doesn't help with multidimensional
// arrays. Or we could do something unsafe. We'll have this
// same problem with structs.
for i := int64(0); i < t.Len; i++ {
res[i] = t.Elem.Zero()
}
return &res
}
/*
* Struct
*/
type StructField struct {
Name string
Type Type
Anonymous bool
}
type StructType struct {
commonType
Elems []StructField
}
var structTypes = newTypeArrayMap()
// Two struct types are identical if they have the same sequence of
// fields, and if corresponding fields have the same names and
// identical types. Two anonymous fields are considered to have the
// same name.
func NewStructType(fields []StructField) *StructType {
// Start by looking up just the types
fts := make([]Type, len(fields))
for i, f := range fields {
fts[i] = f.Type
}
tMapI := structTypes.Get(fts)
if tMapI == nil {
tMapI = structTypes.Put(fts, make(map[string]*StructType))
}
tMap := tMapI.(map[string]*StructType)
// Construct key for field names
key := ""
for _, f := range fields {
// XXX(Spec) It's not clear if struct { T } and struct
// { T T } are either identical or compatible. The
// "Struct Types" section says that the name of that
// field is "T", which suggests that they are
// identical, but it really means that it's the name
// for the purpose of selector expressions and nothing
// else. We decided that they should be neither
// identical or compatible.
if f.Anonymous {
key += "!"
}
key += f.Name + " "
}
// XXX(Spec) Do the tags also have to be identical for the
// types to be identical? I certainly hope so, because
// otherwise, this is the only case where two distinct type
// objects can represent identical types.
t, ok := tMap[key]
if !ok {
// Create new struct type
t = &StructType{commonType{}, fields}
tMap[key] = t
}
return t
}
func (t *StructType) compat(o Type, conv bool) bool {
t2, ok := o.lit().(*StructType)
if !ok {
return false
}
if len(t.Elems) != len(t2.Elems) {
return false
}
for i, e := range t.Elems {
e2 := t2.Elems[i]
// XXX(Spec) An anonymous and a non-anonymous field
// are neither identical nor compatible.
if e.Anonymous != e2.Anonymous ||
(!e.Anonymous && e.Name != e2.Name) ||
!e.Type.compat(e2.Type, conv) {
return false
}
}
return true
}
func (t *StructType) lit() Type { return t }
func (t *StructType) String() string {
s := "struct {"
for i, f := range t.Elems {
if i > 0 {
s += "; "
}
if !f.Anonymous {
s += f.Name + " "
}
s += f.Type.String()
}
return s + "}"
}
func (t *StructType) Zero() Value {
res := structV(make([]Value, len(t.Elems)))
for i, f := range t.Elems {
res[i] = f.Type.Zero()
}
return &res
}
/*
* Pointer
*/
type PtrType struct {
commonType
Elem Type
}
var ptrTypes = make(map[Type]*PtrType)
// Two pointer types are identical if they have identical base types.
func NewPtrType(elem Type) *PtrType {
t, ok := ptrTypes[elem]
if !ok {
t = &PtrType{commonType{}, elem}
ptrTypes[elem] = t
}
return t
}
func (t *PtrType) compat(o Type, conv bool) bool {
t2, ok := o.lit().(*PtrType)
if !ok {
return false
}
return t.Elem.compat(t2.Elem, conv)
}
func (t *PtrType) lit() Type { return t }
func (t *PtrType) String() string { return "*" + t.Elem.String() }
func (t *PtrType) Zero() Value { return &ptrV{nil} }
/*
* Function
*/
type FuncType struct {
commonType
// TODO(austin) Separate receiver Type for methods?
In []Type
Variadic bool
Out []Type
builtin string
}
var funcTypes = newTypeArrayMap()
var variadicFuncTypes = newTypeArrayMap()
// Create singleton function types for magic built-in functions
var (
appendType = &FuncType{builtin: "append"}
capType = &FuncType{builtin: "cap"}
closeType = &FuncType{builtin: "close"}
closedType = &FuncType{builtin: "closed"}
lenType = &FuncType{builtin: "len"}
makeType = &FuncType{builtin: "make"}
newType = &FuncType{builtin: "new"}
panicType = &FuncType{builtin: "panic"}
printType = &FuncType{builtin: "print"}
printlnType = &FuncType{builtin: "println"}
copyType = &FuncType{builtin: "copy"}
)
// Two function types are identical if they have the same number of
// parameters and result values and if corresponding parameter and
// result types are identical. All "..." parameters have identical
// type. Parameter and result names are not required to match.
func NewFuncType(in []Type, variadic bool, out []Type) *FuncType {
inMap := funcTypes
if variadic {
inMap = variadicFuncTypes
}
outMapI := inMap.Get(in)
if outMapI == nil {
outMapI = inMap.Put(in, newTypeArrayMap())
}
outMap := outMapI.(typeArrayMap)
tI := outMap.Get(out)
if tI != nil {
return tI.(*FuncType)
}
t := &FuncType{commonType{}, in, variadic, out, ""}
outMap.Put(out, t)
return t
}
func (t *FuncType) compat(o Type, conv bool) bool {
t2, ok := o.lit().(*FuncType)
if !ok {
return false
}
if len(t.In) != len(t2.In) || t.Variadic != t2.Variadic || len(t.Out) != len(t2.Out) {
return false
}
for i := range t.In {
if !t.In[i].compat(t2.In[i], conv) {
return false
}
}
for i := range t.Out {
if !t.Out[i].compat(t2.Out[i], conv) {
return false
}
}
return true
}
func (t *FuncType) lit() Type { return t }
func typeListString(ts []Type, ns []*ast.Ident) string {
s := ""
for i, t := range ts {
if i > 0 {
s += ", "
}
if ns != nil && ns[i] != nil {
s += ns[i].Name + " "
}
if t == nil {
// Some places use nil types to represent errors
s += "<none>"
} else {
s += t.String()
}
}
return s
}
func (t *FuncType) String() string {
if t.builtin != "" {
return "built-in function " + t.builtin
}
args := typeListString(t.In, nil)
if t.Variadic {
if len(args) > 0 {
args += ", "
}
args += "..."
}
s := "func(" + args + ")"
if len(t.Out) > 0 {
s += " (" + typeListString(t.Out, nil) + ")"
}
return s
}
func (t *FuncType) Zero() Value { return &funcV{nil} }
type FuncDecl struct {
Type *FuncType
Name *ast.Ident // nil for function literals
// InNames will be one longer than Type.In if this function is
// variadic.
InNames []*ast.Ident
OutNames []*ast.Ident
}
func (t *FuncDecl) String() string {
s := "func"
if t.Name != nil {
s += " " + t.Name.Name
}
s += funcTypeString(t.Type, t.InNames, t.OutNames)
return s
}
func funcTypeString(ft *FuncType, ins []*ast.Ident, outs []*ast.Ident) string {
s := "("
s += typeListString(ft.In, ins)
if ft.Variadic {
if len(ft.In) > 0 {
s += ", "
}
s += "..."
}
s += ")"
if len(ft.Out) > 0 {
s += " (" + typeListString(ft.Out, outs) + ")"
}
return s
}
/*
* Interface
*/
// TODO(austin) Interface values, types, and type compilation are
// implemented, but none of the type checking or semantics of
// interfaces are.
type InterfaceType struct {
commonType
// TODO(austin) This should be a map from names to
// *FuncType's. We only need the sorted list for generating
// the type map key. It's detrimental for everything else.
methods []IMethod
}
type IMethod struct {
Name string
Type *FuncType
}
var interfaceTypes = newTypeArrayMap()
func NewInterfaceType(methods []IMethod, embeds []*InterfaceType) *InterfaceType {
// Count methods of embedded interfaces
nMethods := len(methods)
for _, e := range embeds {
nMethods += len(e.methods)
}
// Combine methods
allMethods := make([]IMethod, nMethods)
copy(allMethods, methods)
n := len(methods)
for _, e := range embeds {
for _, m := range e.methods {
allMethods[n] = m
n++
}
}
// Sort methods
sort.Sort(iMethodSorter(allMethods))
mts := make([]Type, len(allMethods))
for i, m := range methods {
mts[i] = m.Type
}
tMapI := interfaceTypes.Get(mts)
if tMapI == nil {
tMapI = interfaceTypes.Put(mts, make(map[string]*InterfaceType))
}
tMap := tMapI.(map[string]*InterfaceType)
key := ""
for _, m := range allMethods {
key += m.Name + " "
}
t, ok := tMap[key]
if !ok {
t = &InterfaceType{commonType{}, allMethods}
tMap[key] = t
}
return t
}
type iMethodSorter []IMethod
func (s iMethodSorter) Less(a, b int) bool { return s[a].Name < s[b].Name }
func (s iMethodSorter) Swap(a, b int) { s[a], s[b] = s[b], s[a] }
func (s iMethodSorter) Len() int { return len(s) }
func (t *InterfaceType) compat(o Type, conv bool) bool {
t2, ok := o.lit().(*InterfaceType)
if !ok {
return false
}
if len(t.methods) != len(t2.methods) {
return false
}
for i, e := range t.methods {
e2 := t2.methods[i]
if e.Name != e2.Name || !e.Type.compat(e2.Type, conv) {
return false
}
}
return true
}
func (t *InterfaceType) lit() Type { return t }
func (t *InterfaceType) String() string {
// TODO(austin) Instead of showing embedded interfaces, this
// shows their methods.
s := "interface {"
for i, m := range t.methods {
if i > 0 {
s += "; "
}
s += m.Name + funcTypeString(m.Type, nil, nil)
}
return s + "}"
}
// implementedBy tests if o implements t, returning nil, true if it does.
// Otherwise, it returns a method of t that o is missing and false.
func (t *InterfaceType) implementedBy(o Type) (*IMethod, bool) {
if len(t.methods) == 0 {
return nil, true
}
// The methods of a named interface types are those of the
// underlying type.
if it, ok := o.lit().(*InterfaceType); ok {
o = it
}
// XXX(Spec) Interface types: "A type implements any interface
// comprising any subset of its methods" It's unclear if
// methods must have identical or compatible types. 6g
// requires identical types.
switch o := o.(type) {
case *NamedType:
for _, tm := range t.methods {
sm, ok := o.methods[tm.Name]
if !ok || sm.decl.Type != tm.Type {
return &tm, false