Source file
src/go/types/type.go
1
2
3
4
5 package types
6
7 import (
8 "fmt"
9 "go/token"
10 "sync/atomic"
11 )
12
13
14
15 type Type interface {
16
17
18
19 Underlying() Type
20
21
22 String() string
23 }
24
25
26 type BasicKind int
27
28 const (
29 Invalid BasicKind = iota
30
31
32 Bool
33 Int
34 Int8
35 Int16
36 Int32
37 Int64
38 Uint
39 Uint8
40 Uint16
41 Uint32
42 Uint64
43 Uintptr
44 Float32
45 Float64
46 Complex64
47 Complex128
48 String
49 UnsafePointer
50
51
52 UntypedBool
53 UntypedInt
54 UntypedRune
55 UntypedFloat
56 UntypedComplex
57 UntypedString
58 UntypedNil
59
60
61 Byte = Uint8
62 Rune = Int32
63 )
64
65
66 type BasicInfo int
67
68
69 const (
70 IsBoolean BasicInfo = 1 << iota
71 IsInteger
72 IsUnsigned
73 IsFloat
74 IsComplex
75 IsString
76 IsUntyped
77
78 IsOrdered = IsInteger | IsFloat | IsString
79 IsNumeric = IsInteger | IsFloat | IsComplex
80 IsConstType = IsBoolean | IsNumeric | IsString
81 )
82
83
84 type Basic struct {
85 kind BasicKind
86 info BasicInfo
87 name string
88 }
89
90
91 func (b *Basic) Kind() BasicKind { return b.kind }
92
93
94 func (b *Basic) Info() BasicInfo { return b.info }
95
96
97 func (b *Basic) Name() string { return b.name }
98
99
100 type Array struct {
101 len int64
102 elem Type
103 }
104
105
106
107 func NewArray(elem Type, len int64) *Array { return &Array{len: len, elem: elem} }
108
109
110
111 func (a *Array) Len() int64 { return a.len }
112
113
114 func (a *Array) Elem() Type { return a.elem }
115
116
117 type Slice struct {
118 elem Type
119 }
120
121
122 func NewSlice(elem Type) *Slice { return &Slice{elem: elem} }
123
124
125 func (s *Slice) Elem() Type { return s.elem }
126
127
128 type Struct struct {
129 fields []*Var
130 tags []string
131 }
132
133
134
135
136
137 func NewStruct(fields []*Var, tags []string) *Struct {
138 var fset objset
139 for _, f := range fields {
140 if f.name != "_" && fset.insert(f) != nil {
141 panic("multiple fields with the same name")
142 }
143 }
144 if len(tags) > len(fields) {
145 panic("more tags than fields")
146 }
147 return &Struct{fields: fields, tags: tags}
148 }
149
150
151 func (s *Struct) NumFields() int { return len(s.fields) }
152
153
154 func (s *Struct) Field(i int) *Var { return s.fields[i] }
155
156
157 func (s *Struct) Tag(i int) string {
158 if i < len(s.tags) {
159 return s.tags[i]
160 }
161 return ""
162 }
163
164
165 type Pointer struct {
166 base Type
167 }
168
169
170 func NewPointer(elem Type) *Pointer { return &Pointer{base: elem} }
171
172
173 func (p *Pointer) Elem() Type { return p.base }
174
175
176
177
178 type Tuple struct {
179 vars []*Var
180 }
181
182
183 func NewTuple(x ...*Var) *Tuple {
184 if len(x) > 0 {
185 return &Tuple{vars: x}
186 }
187
188
189 return nil
190 }
191
192
193 func (t *Tuple) Len() int {
194 if t != nil {
195 return len(t.vars)
196 }
197 return 0
198 }
199
200
201 func (t *Tuple) At(i int) *Var { return t.vars[i] }
202
203
204
205 type Signature struct {
206
207
208
209
210 rparams []*TypeName
211 tparams []*TypeName
212 scope *Scope
213 recv *Var
214 params *Tuple
215 results *Tuple
216 variadic bool
217 }
218
219
220
221
222
223 func NewSignature(recv *Var, params, results *Tuple, variadic bool) *Signature {
224 if variadic {
225 n := params.Len()
226 if n == 0 {
227 panic("types.NewSignature: variadic function must have at least one parameter")
228 }
229 if _, ok := params.At(n - 1).typ.(*Slice); !ok {
230 panic("types.NewSignature: variadic parameter must be of unnamed slice type")
231 }
232 }
233 return &Signature{recv: recv, params: params, results: results, variadic: variadic}
234 }
235
236
237
238
239
240
241
242 func (s *Signature) Recv() *Var { return s.recv }
243
244
245 func (s *Signature) _TParams() []*TypeName { return s.tparams }
246
247
248 func (s *Signature) _SetTParams(tparams []*TypeName) { s.tparams = tparams }
249
250
251 func (s *Signature) Params() *Tuple { return s.params }
252
253
254 func (s *Signature) Results() *Tuple { return s.results }
255
256
257 func (s *Signature) Variadic() bool { return s.variadic }
258
259
260
261
262
263 type _Sum struct {
264 types []Type
265 }
266
267
268
269
270
271 func _NewSum(types []Type) Type {
272 if len(types) == 0 {
273 return nil
274 }
275
276
277
278
279
280
281 for _, t := range types {
282 if _, ok := t.(*_Sum); ok {
283 panic("sum type contains sum type - unimplemented")
284 }
285 }
286
287 if len(types) == 1 {
288 return types[0]
289 }
290 return &_Sum{types: types}
291 }
292
293
294 func (s *_Sum) is(pred func(Type) bool) bool {
295 if s == nil {
296 return false
297 }
298 for _, t := range s.types {
299 if !pred(t) {
300 return false
301 }
302 }
303 return true
304 }
305
306
307 type Interface struct {
308 methods []*Func
309 types Type
310 embeddeds []Type
311
312 allMethods []*Func
313 allTypes Type
314
315 obj Object
316 }
317
318
319
320 func unpackType(typ Type) []Type {
321 if typ == nil {
322 return nil
323 }
324 if sum := asSum(typ); sum != nil {
325 return sum.types
326 }
327 return []Type{typ}
328 }
329
330
331 func (t *Interface) is(pred func(Type) bool) bool {
332 if t.allTypes == nil {
333 return false
334 }
335 for _, t := range unpackType(t.allTypes) {
336 if !pred(t) {
337 return false
338 }
339 }
340 return true
341 }
342
343
344 var emptyInterface = Interface{allMethods: markComplete}
345
346
347
348 var markComplete = make([]*Func, 0)
349
350
351
352
353
354
355
356
357
358 func NewInterface(methods []*Func, embeddeds []*Named) *Interface {
359 tnames := make([]Type, len(embeddeds))
360 for i, t := range embeddeds {
361 tnames[i] = t
362 }
363 return NewInterfaceType(methods, tnames)
364 }
365
366
367
368
369
370
371
372 func NewInterfaceType(methods []*Func, embeddeds []Type) *Interface {
373 if len(methods) == 0 && len(embeddeds) == 0 {
374 return &emptyInterface
375 }
376
377
378 typ := new(Interface)
379 for _, m := range methods {
380 if sig := m.typ.(*Signature); sig.recv == nil {
381 sig.recv = NewVar(m.pos, m.pkg, "", typ)
382 }
383 }
384
385
386
387
388
389 for _, t := range embeddeds {
390 if _, ok := t.(*Named); !ok && !IsInterface(t) {
391 panic("embedded type is not an interface")
392 }
393 }
394
395
396 sortMethods(methods)
397 sortTypes(embeddeds)
398
399 typ.methods = methods
400 typ.embeddeds = embeddeds
401 return typ
402 }
403
404
405 func (t *Interface) NumExplicitMethods() int { return len(t.methods) }
406
407
408
409 func (t *Interface) ExplicitMethod(i int) *Func { return t.methods[i] }
410
411
412 func (t *Interface) NumEmbeddeds() int { return len(t.embeddeds) }
413
414
415
416
417
418 func (t *Interface) Embedded(i int) *Named { tname, _ := t.embeddeds[i].(*Named); return tname }
419
420
421 func (t *Interface) EmbeddedType(i int) Type { return t.embeddeds[i] }
422
423
424
425 func (t *Interface) NumMethods() int { t.assertCompleteness(); return len(t.allMethods) }
426
427 func (t *Interface) assertCompleteness() {
428 if t.allMethods == nil {
429 panic("interface is incomplete")
430 }
431 }
432
433
434
435
436 func (t *Interface) Method(i int) *Func { t.assertCompleteness(); return t.allMethods[i] }
437
438
439 func (t *Interface) Empty() bool {
440 if t.allMethods != nil {
441
442
443 return len(t.allMethods) == 0 && t.allTypes == nil
444 }
445 return !t.iterate(func(t *Interface) bool {
446 return len(t.methods) > 0 || t.types != nil
447 }, nil)
448 }
449
450
451 func (t *Interface) _HasTypeList() bool {
452 if t.allMethods != nil {
453
454 return t.allTypes != nil
455 }
456
457 return t.iterate(func(t *Interface) bool {
458 return t.types != nil
459 }, nil)
460 }
461
462
463 func (t *Interface) _IsComparable() bool {
464 if t.allMethods != nil {
465
466 _, m := lookupMethod(t.allMethods, nil, "==")
467 return m != nil
468 }
469
470 return t.iterate(func(t *Interface) bool {
471 _, m := lookupMethod(t.methods, nil, "==")
472 return m != nil
473 }, nil)
474 }
475
476
477 func (t *Interface) _IsConstraint() bool {
478 if t.allMethods != nil {
479
480 if t.allTypes != nil {
481 return true
482 }
483 _, m := lookupMethod(t.allMethods, nil, "==")
484 return m != nil
485 }
486
487 return t.iterate(func(t *Interface) bool {
488 if t.types != nil {
489 return true
490 }
491 _, m := lookupMethod(t.methods, nil, "==")
492 return m != nil
493 }, nil)
494 }
495
496
497
498 func (t *Interface) iterate(f func(*Interface) bool, seen map[*Interface]bool) bool {
499 if f(t) {
500 return true
501 }
502 for _, e := range t.embeddeds {
503
504 if e := asInterface(e); e != nil {
505
506
507 if seen[e] {
508 continue
509 }
510 if seen == nil {
511 seen = make(map[*Interface]bool)
512 }
513 seen[e] = true
514 if e.iterate(f, seen) {
515 return true
516 }
517 }
518 }
519 return false
520 }
521
522
523
524
525
526 func (t *Interface) isSatisfiedBy(typ Type) bool {
527 t.Complete()
528 if t.allTypes == nil {
529 return true
530 }
531 types := unpackType(t.allTypes)
532 return includes(types, typ) || includes(types, under(typ))
533 }
534
535
536
537
538
539
540 func (t *Interface) Complete() *Interface {
541
542 if t.allMethods != nil {
543 return t
544 }
545
546 t.allMethods = markComplete
547
548 var todo []*Func
549 var methods []*Func
550 var seen objset
551 addMethod := func(m *Func, explicit bool) {
552 switch other := seen.insert(m); {
553 case other == nil:
554 methods = append(methods, m)
555 case explicit:
556 panic("duplicate method " + m.name)
557 default:
558
559 todo = append(todo, m, other.(*Func))
560 }
561 }
562
563 for _, m := range t.methods {
564 addMethod(m, true)
565 }
566
567 allTypes := t.types
568
569 for _, typ := range t.embeddeds {
570 utyp := under(typ)
571 etyp := asInterface(utyp)
572 if etyp == nil {
573 if utyp != Typ[Invalid] {
574 panic(fmt.Sprintf("%s is not an interface", typ))
575 }
576 continue
577 }
578 etyp.Complete()
579 for _, m := range etyp.allMethods {
580 addMethod(m, false)
581 }
582 allTypes = intersect(allTypes, etyp.allTypes)
583 }
584
585 for i := 0; i < len(todo); i += 2 {
586 m := todo[i]
587 other := todo[i+1]
588 if !Identical(m.typ, other.typ) {
589 panic("duplicate method " + m.name)
590 }
591 }
592
593 if methods != nil {
594 sortMethods(methods)
595 t.allMethods = methods
596 }
597 t.allTypes = allTypes
598
599 return t
600 }
601
602
603 type Map struct {
604 key, elem Type
605 }
606
607
608 func NewMap(key, elem Type) *Map {
609 return &Map{key: key, elem: elem}
610 }
611
612
613 func (m *Map) Key() Type { return m.key }
614
615
616 func (m *Map) Elem() Type { return m.elem }
617
618
619 type Chan struct {
620 dir ChanDir
621 elem Type
622 }
623
624
625 type ChanDir int
626
627
628 const (
629 SendRecv ChanDir = iota
630 SendOnly
631 RecvOnly
632 )
633
634
635 func NewChan(dir ChanDir, elem Type) *Chan {
636 return &Chan{dir: dir, elem: elem}
637 }
638
639
640 func (c *Chan) Dir() ChanDir { return c.dir }
641
642
643 func (c *Chan) Elem() Type { return c.elem }
644
645
646 type Named struct {
647 check *Checker
648 info typeInfo
649 obj *TypeName
650 orig Type
651 underlying Type
652 tparams []*TypeName
653 targs []Type
654 methods []*Func
655 }
656
657
658
659
660 func NewNamed(obj *TypeName, underlying Type, methods []*Func) *Named {
661 if _, ok := underlying.(*Named); ok {
662 panic("types.NewNamed: underlying type must not be *Named")
663 }
664 return (*Checker)(nil).newNamed(obj, underlying, methods)
665 }
666
667 func (check *Checker) newNamed(obj *TypeName, underlying Type, methods []*Func) *Named {
668 typ := &Named{check: check, obj: obj, orig: underlying, underlying: underlying, methods: methods}
669 if obj.typ == nil {
670 obj.typ = typ
671 }
672
673
674
675
676
677
678
679
680 if check != nil {
681 check.later(func() {
682 switch typ.under().(type) {
683 case *Named, *instance:
684 panic("internal error: unexpanded underlying type")
685 }
686 typ.check = nil
687 })
688 }
689 return typ
690 }
691
692
693 func (t *Named) Obj() *TypeName { return t.obj }
694
695
696
697
698
699
700 func (t *Named) _TParams() []*TypeName { return t.tparams }
701
702
703 func (t *Named) _TArgs() []Type { return t.targs }
704
705
706 func (t *Named) _SetTArgs(args []Type) { t.targs = args }
707
708
709 func (t *Named) NumMethods() int { return len(t.methods) }
710
711
712 func (t *Named) Method(i int) *Func { return t.methods[i] }
713
714
715 func (t *Named) SetUnderlying(underlying Type) {
716 if underlying == nil {
717 panic("types.Named.SetUnderlying: underlying type must not be nil")
718 }
719 if _, ok := underlying.(*Named); ok {
720 panic("types.Named.SetUnderlying: underlying type must not be *Named")
721 }
722 t.underlying = underlying
723 }
724
725
726 func (t *Named) AddMethod(m *Func) {
727 if i, _ := lookupMethod(t.methods, m.pkg, m.name); i < 0 {
728 t.methods = append(t.methods, m)
729 }
730 }
731
732
733
734
735 var lastId uint32
736
737
738
739 func nextId() uint64 { return uint64(atomic.AddUint32(&lastId, 1)) }
740
741
742 type _TypeParam struct {
743 check *Checker
744 id uint64
745 obj *TypeName
746 index int
747 bound Type
748 }
749
750
751 func (check *Checker) newTypeParam(obj *TypeName, index int, bound Type) *_TypeParam {
752 assert(bound != nil)
753 typ := &_TypeParam{check: check, id: nextId(), obj: obj, index: index, bound: bound}
754 if obj.typ == nil {
755 obj.typ = typ
756 }
757 return typ
758 }
759
760 func (t *_TypeParam) Bound() *Interface {
761 iface := asInterface(t.bound)
762 if iface == nil {
763 return &emptyInterface
764 }
765
766 pos := token.NoPos
767 if n, _ := t.bound.(*Named); n != nil {
768 pos = n.obj.pos
769 }
770
771 t.check.completeInterface(pos, iface)
772 return iface
773 }
774
775
776
777
778
779
780
781
782 func optype(typ Type) Type {
783 if t := asTypeParam(typ); t != nil {
784
785
786
787
788
789
790 if u := t.Bound().allTypes; u != nil && u != typ {
791
792 return under(u)
793 }
794 return theTop
795 }
796 return under(typ)
797 }
798
799
800
801
802
803 type instance struct {
804 check *Checker
805 pos token.Pos
806 base *Named
807 targs []Type
808 poslist []token.Pos
809 value Type
810 }
811
812
813
814
815 func (t *instance) expand() Type {
816 v := t.value
817 if v == nil {
818 v = t.check.instantiate(t.pos, t.base, t.targs, t.poslist)
819 if v == nil {
820 v = Typ[Invalid]
821 }
822 t.value = v
823 }
824
825 if debug && v != Typ[Invalid] {
826 _ = v.(*Named)
827 }
828 return v
829 }
830
831
832
833
834 func expand(typ Type) Type {
835 if t, _ := typ.(*instance); t != nil {
836 return t.expand()
837 }
838 return typ
839 }
840
841
842
843 var expandf func(Type) Type
844
845 func init() { expandf = expand }
846
847
848
849
850
851 type bottom struct{}
852
853
854 var theBottom = &bottom{}
855
856
857
858
859
860
861 type top struct{}
862
863
864 var theTop = &top{}
865
866
867 func (t *Basic) Underlying() Type { return t }
868 func (t *Array) Underlying() Type { return t }
869 func (t *Slice) Underlying() Type { return t }
870 func (t *Struct) Underlying() Type { return t }
871 func (t *Pointer) Underlying() Type { return t }
872 func (t *Tuple) Underlying() Type { return t }
873 func (t *Signature) Underlying() Type { return t }
874 func (t *_Sum) Underlying() Type { return t }
875 func (t *Interface) Underlying() Type { return t }
876 func (t *Map) Underlying() Type { return t }
877 func (t *Chan) Underlying() Type { return t }
878 func (t *Named) Underlying() Type { return t.underlying }
879 func (t *_TypeParam) Underlying() Type { return t }
880 func (t *instance) Underlying() Type { return t }
881 func (t *bottom) Underlying() Type { return t }
882 func (t *top) Underlying() Type { return t }
883
884
885 func (t *Basic) String() string { return TypeString(t, nil) }
886 func (t *Array) String() string { return TypeString(t, nil) }
887 func (t *Slice) String() string { return TypeString(t, nil) }
888 func (t *Struct) String() string { return TypeString(t, nil) }
889 func (t *Pointer) String() string { return TypeString(t, nil) }
890 func (t *Tuple) String() string { return TypeString(t, nil) }
891 func (t *Signature) String() string { return TypeString(t, nil) }
892 func (t *_Sum) String() string { return TypeString(t, nil) }
893 func (t *Interface) String() string { return TypeString(t, nil) }
894 func (t *Map) String() string { return TypeString(t, nil) }
895 func (t *Chan) String() string { return TypeString(t, nil) }
896 func (t *Named) String() string { return TypeString(t, nil) }
897 func (t *_TypeParam) String() string { return TypeString(t, nil) }
898 func (t *instance) String() string { return TypeString(t, nil) }
899 func (t *bottom) String() string { return TypeString(t, nil) }
900 func (t *top) String() string { return TypeString(t, nil) }
901
902
903
904
905
906 func under(t Type) Type {
907
908 if n := asNamed(t); n != nil {
909 return n.under()
910 }
911 return t
912 }
913
914
915
916
917
918
919
920
921
922 func asBasic(t Type) *Basic {
923 op, _ := optype(t).(*Basic)
924 return op
925 }
926
927 func asArray(t Type) *Array {
928 op, _ := optype(t).(*Array)
929 return op
930 }
931
932 func asSlice(t Type) *Slice {
933 op, _ := optype(t).(*Slice)
934 return op
935 }
936
937 func asStruct(t Type) *Struct {
938 op, _ := optype(t).(*Struct)
939 return op
940 }
941
942 func asPointer(t Type) *Pointer {
943 op, _ := optype(t).(*Pointer)
944 return op
945 }
946
947 func asTuple(t Type) *Tuple {
948 op, _ := optype(t).(*Tuple)
949 return op
950 }
951
952 func asSignature(t Type) *Signature {
953 op, _ := optype(t).(*Signature)
954 return op
955 }
956
957 func asSum(t Type) *_Sum {
958 op, _ := optype(t).(*_Sum)
959 return op
960 }
961
962 func asInterface(t Type) *Interface {
963 op, _ := optype(t).(*Interface)
964 return op
965 }
966
967 func asMap(t Type) *Map {
968 op, _ := optype(t).(*Map)
969 return op
970 }
971
972 func asChan(t Type) *Chan {
973 op, _ := optype(t).(*Chan)
974 return op
975 }
976
977
978
979
980
981 func asNamed(t Type) *Named {
982 e, _ := expand(t).(*Named)
983 return e
984 }
985
986 func asTypeParam(t Type) *_TypeParam {
987 u, _ := under(t).(*_TypeParam)
988 return u
989 }
990
View as plain text