1
2
3
4
5
6
7 package types2
8
9 import (
10 "cmd/compile/internal/syntax"
11 "fmt"
12 "go/constant"
13 "go/token"
14 . "internal/types/errors"
15 )
16
17
58
59 type opPredicates map[syntax.Operator]func(Type) bool
60
61 var unaryOpPredicates opPredicates
62
63 func init() {
64
65 unaryOpPredicates = opPredicates{
66 syntax.Add: allNumeric,
67 syntax.Sub: allNumeric,
68 syntax.Xor: allInteger,
69 syntax.Not: allBoolean,
70 }
71 }
72
73 func (check *Checker) op(m opPredicates, x *operand, op syntax.Operator) bool {
74 if pred := m[op]; pred != nil {
75 if !pred(x.typ) {
76 check.errorf(x, UndefinedOp, invalidOp+"operator %s not defined on %s", op, x)
77 return false
78 }
79 } else {
80 check.errorf(x, InvalidSyntaxTree, "unknown operator %s", op)
81 return false
82 }
83 return true
84 }
85
86
87
88 func opPos(x syntax.Expr) syntax.Pos {
89 switch op := x.(type) {
90 case nil:
91 return nopos
92 case *syntax.Operation:
93 return op.Pos()
94 default:
95 return syntax.StartPos(x)
96 }
97 }
98
99
100
101 func opName(x syntax.Expr) string {
102 if e, _ := x.(*syntax.Operation); e != nil {
103 op := int(e.Op)
104 if e.Y == nil {
105 if op < len(op2str1) {
106 return op2str1[op]
107 }
108 } else {
109 if op < len(op2str2) {
110 return op2str2[op]
111 }
112 }
113 }
114 return ""
115 }
116
117 var op2str1 = [...]string{
118 syntax.Xor: "bitwise complement",
119 }
120
121
122 var op2str2 = [...]string{
123 syntax.Add: "addition",
124 syntax.Sub: "subtraction",
125 syntax.Xor: "bitwise XOR",
126 syntax.Mul: "multiplication",
127 syntax.Shl: "shift",
128 }
129
130 func (check *Checker) unary(x *operand, e *syntax.Operation) {
131 check.expr(nil, x, e.X)
132 if x.mode == invalid {
133 return
134 }
135
136 op := e.Op
137 switch op {
138 case syntax.And:
139
140
141 if _, ok := syntax.Unparen(e.X).(*syntax.CompositeLit); !ok && x.mode != variable {
142 check.errorf(x, UnaddressableOperand, invalidOp+"cannot take address of %s", x)
143 x.mode = invalid
144 return
145 }
146 x.mode = value
147 x.typ = &Pointer{base: x.typ}
148 return
149
150 case syntax.Recv:
151 if elem := check.chanElem(x, x, true); elem != nil {
152 x.mode = commaok
153 x.typ = elem
154 check.hasCallOrRecv = true
155 return
156 }
157 x.mode = invalid
158 return
159
160 case syntax.Tilde:
161
162 if !allInteger(x.typ) {
163 check.error(e, UndefinedOp, "cannot use ~ outside of interface or type constraint")
164 x.mode = invalid
165 return
166 }
167 check.error(e, UndefinedOp, "cannot use ~ outside of interface or type constraint (use ^ for bitwise complement)")
168 op = syntax.Xor
169 }
170
171 if !check.op(unaryOpPredicates, x, op) {
172 x.mode = invalid
173 return
174 }
175
176 if x.mode == constant_ {
177 if x.val.Kind() == constant.Unknown {
178
179 return
180 }
181 var prec uint
182 if isUnsigned(x.typ) {
183 prec = uint(check.conf.sizeof(x.typ) * 8)
184 }
185 x.val = constant.UnaryOp(op2tok[op], x.val, prec)
186 x.expr = e
187 check.overflow(x, opPos(x.expr))
188 return
189 }
190
191 x.mode = value
192
193 }
194
195
196
197
198 func (check *Checker) chanElem(pos poser, x *operand, recv bool) Type {
199 u, err := commonUnder(x.typ, func(t, u Type) *typeError {
200 if u == nil {
201 return typeErrorf("no specific channel type")
202 }
203 ch, _ := u.(*Chan)
204 if ch == nil {
205 return typeErrorf("non-channel %s", t)
206 }
207 if recv && ch.dir == SendOnly {
208 return typeErrorf("send-only channel %s", t)
209 }
210 if !recv && ch.dir == RecvOnly {
211 return typeErrorf("receive-only channel %s", t)
212 }
213 return nil
214 })
215
216 if u != nil {
217 return u.(*Chan).elem
218 }
219
220 cause := err.format(check)
221 if recv {
222 if isTypeParam(x.typ) {
223 check.errorf(pos, InvalidReceive, invalidOp+"cannot receive from %s: %s", x, cause)
224 } else {
225
226 check.errorf(pos, InvalidReceive, invalidOp+"cannot receive from %s %s", cause, x)
227 }
228 } else {
229 if isTypeParam(x.typ) {
230 check.errorf(pos, InvalidSend, invalidOp+"cannot send to %s: %s", x, cause)
231 } else {
232
233 check.errorf(pos, InvalidSend, invalidOp+"cannot send to %s %s", cause, x)
234 }
235 }
236 return nil
237 }
238
239 func isShift(op syntax.Operator) bool {
240 return op == syntax.Shl || op == syntax.Shr
241 }
242
243 func isComparison(op syntax.Operator) bool {
244
245 switch op {
246 case syntax.Eql, syntax.Neq, syntax.Lss, syntax.Leq, syntax.Gtr, syntax.Geq:
247 return true
248 }
249 return false
250 }
251
252
253
254
255
256
257
258
259
260
261 func (check *Checker) updateExprType(x syntax.Expr, typ Type, final bool) {
262 old, found := check.untyped[x]
263 if !found {
264 return
265 }
266
267
268 switch x := x.(type) {
269 case *syntax.BadExpr,
270 *syntax.FuncLit,
271 *syntax.CompositeLit,
272 *syntax.IndexExpr,
273 *syntax.SliceExpr,
274 *syntax.AssertExpr,
275 *syntax.ListExpr,
276
277 *syntax.KeyValueExpr,
278 *syntax.ArrayType,
279 *syntax.StructType,
280 *syntax.FuncType,
281 *syntax.InterfaceType,
282 *syntax.MapType,
283 *syntax.ChanType:
284
285
286
287 if debug {
288 check.dump("%v: found old type(%s): %s (new: %s)", atPos(x), x, old.typ, typ)
289 panic("unreachable")
290 }
291 return
292
293 case *syntax.CallExpr:
294
295
296
297
298 case *syntax.Name, *syntax.BasicLit, *syntax.SelectorExpr:
299
300
301
302
303 case *syntax.ParenExpr:
304 check.updateExprType(x.X, typ, final)
305
306
307
308
309
310
311
312
313
314
315
316
317 case *syntax.Operation:
318 if x.Y == nil {
319
320 if x.Op == syntax.Mul {
321
322
323 if debug {
324 panic("unimplemented")
325 }
326 return
327 }
328
329
330
331
332
333 if old.val != nil {
334 break
335 }
336 check.updateExprType(x.X, typ, final)
337 break
338 }
339
340
341 if old.val != nil {
342 break
343 }
344 if isComparison(x.Op) {
345
346
347 } else if isShift(x.Op) {
348
349
350 check.updateExprType(x.X, typ, final)
351 } else {
352
353 check.updateExprType(x.X, typ, final)
354 check.updateExprType(x.Y, typ, final)
355 }
356
357 default:
358 panic("unreachable")
359 }
360
361
362
363 if !final && isUntyped(typ) {
364 old.typ = under(typ).(*Basic)
365 check.untyped[x] = old
366 return
367 }
368
369
370
371 delete(check.untyped, x)
372
373 if old.isLhs {
374
375
376
377 if !allInteger(typ) {
378 check.errorf(x, InvalidShiftOperand, invalidOp+"shifted operand %s (type %s) must be integer", x, typ)
379 return
380 }
381
382
383
384 }
385 if old.val != nil {
386
387 c := operand{old.mode, x, old.typ, old.val, 0}
388 check.convertUntyped(&c, typ)
389 if c.mode == invalid {
390 return
391 }
392 }
393
394
395 check.recordTypeAndValue(x, old.mode, typ, old.val)
396 }
397
398
399 func (check *Checker) updateExprVal(x syntax.Expr, val constant.Value) {
400 if info, ok := check.untyped[x]; ok {
401 info.val = val
402 check.untyped[x] = info
403 }
404 }
405
406
407
408
409
410
411
412 func (check *Checker) implicitTypeAndValue(x *operand, target Type) (Type, constant.Value, Code) {
413 if x.mode == invalid || isTyped(x.typ) || !isValid(target) {
414 return x.typ, nil, 0
415 }
416
417
418 if isUntyped(target) {
419
420 if m := maxType(x.typ, target); m != nil {
421 return m, nil, 0
422 }
423 return nil, nil, InvalidUntypedConversion
424 }
425
426 if x.isNil() {
427 assert(isUntyped(x.typ))
428 if hasNil(target) {
429 return target, nil, 0
430 }
431 return nil, nil, InvalidUntypedConversion
432 }
433
434 switch u := under(target).(type) {
435 case *Basic:
436 if x.mode == constant_ {
437 v, code := check.representation(x, u)
438 if code != 0 {
439 return nil, nil, code
440 }
441 return target, v, code
442 }
443
444
445
446
447 switch x.typ.(*Basic).kind {
448 case UntypedBool:
449 if !isBoolean(target) {
450 return nil, nil, InvalidUntypedConversion
451 }
452 case UntypedInt, UntypedRune, UntypedFloat, UntypedComplex:
453 if !isNumeric(target) {
454 return nil, nil, InvalidUntypedConversion
455 }
456 case UntypedString:
457
458
459
460 if !isString(target) {
461 return nil, nil, InvalidUntypedConversion
462 }
463 default:
464 return nil, nil, InvalidUntypedConversion
465 }
466 case *Interface:
467 if isTypeParam(target) {
468 if !underIs(target, func(u Type) bool {
469 if u == nil {
470 return false
471 }
472 t, _, _ := check.implicitTypeAndValue(x, u)
473 return t != nil
474 }) {
475 return nil, nil, InvalidUntypedConversion
476 }
477 break
478 }
479
480
481
482 if !u.Empty() {
483 return nil, nil, InvalidUntypedConversion
484 }
485 return Default(x.typ), nil, 0
486 default:
487 return nil, nil, InvalidUntypedConversion
488 }
489 return target, nil, 0
490 }
491
492
493 func (check *Checker) comparison(x, y *operand, op syntax.Operator, switchCase bool) {
494
495 if !isValid(x.typ) || !isValid(y.typ) {
496 x.mode = invalid
497 return
498 }
499
500 if switchCase {
501 op = syntax.Eql
502 }
503
504 errOp := x
505 cause := ""
506
507
508
509 code := MismatchedTypes
510 ok, _ := x.assignableTo(check, y.typ, nil)
511 if !ok {
512 ok, _ = y.assignableTo(check, x.typ, nil)
513 }
514 if !ok {
515
516
517
518 errOp = y
519 cause = check.sprintf("mismatched types %s and %s", x.typ, y.typ)
520 goto Error
521 }
522
523
524 code = UndefinedOp
525 switch op {
526 case syntax.Eql, syntax.Neq:
527
528 switch {
529 case x.isNil() || y.isNil():
530
531 typ := x.typ
532 if x.isNil() {
533 typ = y.typ
534 }
535 if !hasNil(typ) {
536
537
538
539
540 errOp = y
541 goto Error
542 }
543
544 case !Comparable(x.typ):
545 errOp = x
546 cause = check.incomparableCause(x.typ)
547 goto Error
548
549 case !Comparable(y.typ):
550 errOp = y
551 cause = check.incomparableCause(y.typ)
552 goto Error
553 }
554
555 case syntax.Lss, syntax.Leq, syntax.Gtr, syntax.Geq:
556
557 switch {
558 case !allOrdered(x.typ):
559 errOp = x
560 goto Error
561 case !allOrdered(y.typ):
562 errOp = y
563 goto Error
564 }
565
566 default:
567 panic("unreachable")
568 }
569
570
571 if x.mode == constant_ && y.mode == constant_ {
572 x.val = constant.MakeBool(constant.Compare(x.val, op2tok[op], y.val))
573
574
575 } else {
576 x.mode = value
577
578
579
580
581 check.updateExprType(x.expr, Default(x.typ), true)
582 check.updateExprType(y.expr, Default(y.typ), true)
583 }
584
585
586
587 x.typ = Typ[UntypedBool]
588 return
589
590 Error:
591
592 if cause == "" {
593 if isTypeParam(x.typ) || isTypeParam(y.typ) {
594
595 if !isTypeParam(x.typ) {
596 errOp = y
597 }
598 cause = check.sprintf("type parameter %s cannot use operator %s", errOp.typ, op)
599 } else {
600
601 what := compositeKind(errOp.typ)
602 if what == "" {
603 what = check.sprintf("%s", errOp.typ)
604 }
605 cause = check.sprintf("operator %s not defined on %s", op, what)
606 }
607 }
608 if switchCase {
609 check.errorf(x, code, "invalid case %s in switch on %s (%s)", x.expr, y.expr, cause)
610 } else {
611 check.errorf(errOp, code, invalidOp+"%s %s %s (%s)", x.expr, op, y.expr, cause)
612 }
613 x.mode = invalid
614 }
615
616
617
618 func (check *Checker) incomparableCause(typ Type) string {
619 switch under(typ).(type) {
620 case *Slice, *Signature, *Map:
621 return compositeKind(typ) + " can only be compared to nil"
622 }
623
624 return comparableType(typ, true, nil).format(check)
625 }
626
627
628 func (check *Checker) shift(x, y *operand, e syntax.Expr, op syntax.Operator) {
629
630
631 var xval constant.Value
632 if x.mode == constant_ {
633 xval = constant.ToInt(x.val)
634 }
635
636 if allInteger(x.typ) || isUntyped(x.typ) && xval != nil && xval.Kind() == constant.Int {
637
638
639 } else {
640
641 check.errorf(x, InvalidShiftOperand, invalidOp+"shifted operand %s must be integer", x)
642 x.mode = invalid
643 return
644 }
645
646
647
648
649
650
651 var yval constant.Value
652 if y.mode == constant_ {
653
654 yval = constant.ToInt(y.val)
655 if yval.Kind() == constant.Int && constant.Sign(yval) < 0 {
656 check.errorf(y, InvalidShiftCount, invalidOp+"negative shift count %s", y)
657 x.mode = invalid
658 return
659 }
660
661 if isUntyped(y.typ) {
662
663
664 check.representable(y, Typ[Uint])
665 if y.mode == invalid {
666 x.mode = invalid
667 return
668 }
669 }
670 } else {
671
672 switch {
673 case allInteger(y.typ):
674 if !allUnsigned(y.typ) && !check.verifyVersionf(y, go1_13, invalidOp+"signed shift count %s", y) {
675 x.mode = invalid
676 return
677 }
678 case isUntyped(y.typ):
679
680
681 check.convertUntyped(y, Typ[Uint])
682 if y.mode == invalid {
683 x.mode = invalid
684 return
685 }
686 default:
687 check.errorf(y, InvalidShiftCount, invalidOp+"shift count %s must be integer", y)
688 x.mode = invalid
689 return
690 }
691 }
692
693 if x.mode == constant_ {
694 if y.mode == constant_ {
695
696 if x.val.Kind() == constant.Unknown || y.val.Kind() == constant.Unknown {
697 x.val = constant.MakeUnknown()
698
699 if !isInteger(x.typ) {
700 x.typ = Typ[UntypedInt]
701 }
702 return
703 }
704
705 const shiftBound = 1023 - 1 + 52
706 s, ok := constant.Uint64Val(yval)
707 if !ok || s > shiftBound {
708 check.errorf(y, InvalidShiftCount, invalidOp+"invalid shift count %s", y)
709 x.mode = invalid
710 return
711 }
712
713
714
715
716 if !isInteger(x.typ) {
717 x.typ = Typ[UntypedInt]
718 }
719
720 x.val = constant.Shift(xval, op2tok[op], uint(s))
721 x.expr = e
722 check.overflow(x, opPos(x.expr))
723 return
724 }
725
726
727 if isUntyped(x.typ) {
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747 if info, found := check.untyped[x.expr]; found {
748 info.isLhs = true
749 check.untyped[x.expr] = info
750 }
751
752 x.mode = value
753 return
754 }
755 }
756
757
758 if !allInteger(x.typ) {
759 check.errorf(x, InvalidShiftOperand, invalidOp+"shifted operand %s must be integer", x)
760 x.mode = invalid
761 return
762 }
763
764 x.mode = value
765 }
766
767 var binaryOpPredicates opPredicates
768
769 func init() {
770
771 binaryOpPredicates = opPredicates{
772 syntax.Add: allNumericOrString,
773 syntax.Sub: allNumeric,
774 syntax.Mul: allNumeric,
775 syntax.Div: allNumeric,
776 syntax.Rem: allInteger,
777
778 syntax.And: allInteger,
779 syntax.Or: allInteger,
780 syntax.Xor: allInteger,
781 syntax.AndNot: allInteger,
782
783 syntax.AndAnd: allBoolean,
784 syntax.OrOr: allBoolean,
785 }
786 }
787
788
789
790 func (check *Checker) binary(x *operand, e syntax.Expr, lhs, rhs syntax.Expr, op syntax.Operator) {
791 var y operand
792
793 check.expr(nil, x, lhs)
794 check.expr(nil, &y, rhs)
795
796 if x.mode == invalid {
797 return
798 }
799 if y.mode == invalid {
800 x.mode = invalid
801 x.expr = y.expr
802 return
803 }
804
805 if isShift(op) {
806 check.shift(x, &y, e, op)
807 return
808 }
809
810 check.matchTypes(x, &y)
811 if x.mode == invalid {
812 return
813 }
814
815 if isComparison(op) {
816 check.comparison(x, &y, op, false)
817 return
818 }
819
820 if !Identical(x.typ, y.typ) {
821
822
823 if isValid(x.typ) && isValid(y.typ) {
824 if e != nil {
825 check.errorf(x, MismatchedTypes, invalidOp+"%s (mismatched types %s and %s)", e, x.typ, y.typ)
826 } else {
827 check.errorf(x, MismatchedTypes, invalidOp+"%s %s= %s (mismatched types %s and %s)", lhs, op, rhs, x.typ, y.typ)
828 }
829 }
830 x.mode = invalid
831 return
832 }
833
834 if !check.op(binaryOpPredicates, x, op) {
835 x.mode = invalid
836 return
837 }
838
839 if op == syntax.Div || op == syntax.Rem {
840
841 if (x.mode == constant_ || allInteger(x.typ)) && y.mode == constant_ && constant.Sign(y.val) == 0 {
842 check.error(&y, DivByZero, invalidOp+"division by zero")
843 x.mode = invalid
844 return
845 }
846
847
848 if x.mode == constant_ && y.mode == constant_ && isComplex(x.typ) {
849 re, im := constant.Real(y.val), constant.Imag(y.val)
850 re2, im2 := constant.BinaryOp(re, token.MUL, re), constant.BinaryOp(im, token.MUL, im)
851 if constant.Sign(re2) == 0 && constant.Sign(im2) == 0 {
852 check.error(&y, DivByZero, invalidOp+"division by zero")
853 x.mode = invalid
854 return
855 }
856 }
857 }
858
859 if x.mode == constant_ && y.mode == constant_ {
860
861 if x.val.Kind() == constant.Unknown || y.val.Kind() == constant.Unknown {
862 x.val = constant.MakeUnknown()
863
864 return
865 }
866
867 tok := op2tok[op]
868 if op == syntax.Div && isInteger(x.typ) {
869 tok = token.QUO_ASSIGN
870 }
871 x.val = constant.BinaryOp(x.val, tok, y.val)
872 x.expr = e
873 check.overflow(x, opPos(x.expr))
874 return
875 }
876
877 x.mode = value
878
879 }
880
881
882
883 func (check *Checker) matchTypes(x, y *operand) {
884
885
886
887
888
889
890
891
892
893 mayConvert := func(x, y *operand) bool {
894
895 if isTyped(x.typ) && isTyped(y.typ) {
896 return false
897 }
898
899 if allNumeric(x.typ) != allNumeric(y.typ) {
900 return false
901 }
902
903
904
905
906 if isNonTypeParamInterface(x.typ) || isNonTypeParamInterface(y.typ) {
907 return true
908 }
909
910 if allBoolean(x.typ) != allBoolean(y.typ) {
911 return false
912 }
913
914 if allString(x.typ) != allString(y.typ) {
915 return false
916 }
917
918 if x.isNil() {
919 return hasNil(y.typ)
920 }
921 if y.isNil() {
922 return hasNil(x.typ)
923 }
924
925
926 if isPointer(x.typ) || isPointer(y.typ) {
927 return false
928 }
929 return true
930 }
931
932 if mayConvert(x, y) {
933 check.convertUntyped(x, y.typ)
934 if x.mode == invalid {
935 return
936 }
937 check.convertUntyped(y, x.typ)
938 if y.mode == invalid {
939 x.mode = invalid
940 return
941 }
942 }
943 }
944
945
946
947 type exprKind int
948
949 const (
950 conversion exprKind = iota
951 expression
952 statement
953 )
954
955
956
957 type target struct {
958 sig *Signature
959 desc string
960 }
961
962
963
964 func newTarget(typ Type, desc string) *target {
965 if typ != nil {
966 if sig, _ := under(typ).(*Signature); sig != nil {
967 return &target{sig, desc}
968 }
969 }
970 return nil
971 }
972
973
974
975
976
977
978
979
980 func (check *Checker) rawExpr(T *target, x *operand, e syntax.Expr, hint Type, allowGeneric bool) exprKind {
981 if check.conf.Trace {
982 check.trace(e.Pos(), "-- expr %s", e)
983 check.indent++
984 defer func() {
985 check.indent--
986 check.trace(e.Pos(), "=> %s", x)
987 }()
988 }
989
990 kind := check.exprInternal(T, x, e, hint)
991
992 if !allowGeneric {
993 check.nonGeneric(T, x)
994 }
995
996 check.record(x)
997
998 return kind
999 }
1000
1001
1002
1003
1004 func (check *Checker) nonGeneric(T *target, x *operand) {
1005 if x.mode == invalid || x.mode == novalue {
1006 return
1007 }
1008 var what string
1009 switch t := x.typ.(type) {
1010 case *Alias, *Named:
1011 if isGeneric(t) {
1012 what = "type"
1013 }
1014 case *Signature:
1015 if t.tparams != nil {
1016 if enableReverseTypeInference && T != nil {
1017 check.funcInst(T, x.Pos(), x, nil, true)
1018 return
1019 }
1020 what = "function"
1021 }
1022 }
1023 if what != "" {
1024 check.errorf(x.expr, WrongTypeArgCount, "cannot use generic %s %s without instantiation", what, x.expr)
1025 x.mode = invalid
1026 x.typ = Typ[Invalid]
1027 }
1028 }
1029
1030
1031
1032
1033 func (check *Checker) exprInternal(T *target, x *operand, e syntax.Expr, hint Type) exprKind {
1034
1035
1036 x.mode = invalid
1037 x.typ = Typ[Invalid]
1038
1039 switch e := e.(type) {
1040 case nil:
1041 panic("unreachable")
1042
1043 case *syntax.BadExpr:
1044 goto Error
1045
1046 case *syntax.Name:
1047 check.ident(x, e, nil, false)
1048
1049 case *syntax.DotsType:
1050
1051 check.error(e, InvalidSyntaxTree, "invalid use of ...")
1052 goto Error
1053
1054 case *syntax.BasicLit:
1055 if e.Bad {
1056 goto Error
1057 }
1058 check.basicLit(x, e)
1059 if x.mode == invalid {
1060 goto Error
1061 }
1062
1063 case *syntax.FuncLit:
1064 check.funcLit(x, e)
1065 if x.mode == invalid {
1066 goto Error
1067 }
1068
1069 case *syntax.CompositeLit:
1070 check.compositeLit(x, e, hint)
1071 if x.mode == invalid {
1072 goto Error
1073 }
1074
1075 case *syntax.ParenExpr:
1076
1077 kind := check.rawExpr(nil, x, e.X, nil, false)
1078 x.expr = e
1079 return kind
1080
1081 case *syntax.SelectorExpr:
1082 check.selector(x, e, nil, false)
1083
1084 case *syntax.IndexExpr:
1085 if check.indexExpr(x, e) {
1086 if !enableReverseTypeInference {
1087 T = nil
1088 }
1089 check.funcInst(T, e.Pos(), x, e, true)
1090 }
1091 if x.mode == invalid {
1092 goto Error
1093 }
1094
1095 case *syntax.SliceExpr:
1096 check.sliceExpr(x, e)
1097 if x.mode == invalid {
1098 goto Error
1099 }
1100
1101 case *syntax.AssertExpr:
1102 check.expr(nil, x, e.X)
1103 if x.mode == invalid {
1104 goto Error
1105 }
1106
1107 if e.Type == nil {
1108 check.error(e, InvalidSyntaxTree, "invalid use of AssertExpr")
1109 goto Error
1110 }
1111 if isTypeParam(x.typ) {
1112 check.errorf(x, InvalidAssert, invalidOp+"cannot use type assertion on type parameter value %s", x)
1113 goto Error
1114 }
1115 if _, ok := under(x.typ).(*Interface); !ok {
1116 check.errorf(x, InvalidAssert, invalidOp+"%s is not an interface", x)
1117 goto Error
1118 }
1119 T := check.varType(e.Type)
1120 if !isValid(T) {
1121 goto Error
1122 }
1123 check.typeAssertion(e, x, T, false)
1124 x.mode = commaok
1125 x.typ = T
1126
1127 case *syntax.TypeSwitchGuard:
1128
1129 check.error(e, InvalidSyntaxTree, "use of .(type) outside type switch")
1130 check.use(e.X)
1131 goto Error
1132
1133 case *syntax.CallExpr:
1134 return check.callExpr(x, e)
1135
1136 case *syntax.ListExpr:
1137
1138 check.error(e, InvalidSyntaxTree, "unexpected list of expressions")
1139 goto Error
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161 case *syntax.Operation:
1162 if e.Y == nil {
1163
1164 if e.Op == syntax.Mul {
1165
1166 check.exprOrType(x, e.X, false)
1167 switch x.mode {
1168 case invalid:
1169 goto Error
1170 case typexpr:
1171 check.validVarType(e.X, x.typ)
1172 x.typ = &Pointer{base: x.typ}
1173 default:
1174 var base Type
1175 if !underIs(x.typ, func(u Type) bool {
1176 p, _ := u.(*Pointer)
1177 if p == nil {
1178 check.errorf(x, InvalidIndirection, invalidOp+"cannot indirect %s", x)
1179 return false
1180 }
1181 if base != nil && !Identical(p.base, base) {
1182 check.errorf(x, InvalidIndirection, invalidOp+"pointers of %s must have identical base types", x)
1183 return false
1184 }
1185 base = p.base
1186 return true
1187 }) {
1188 goto Error
1189 }
1190 x.mode = variable
1191 x.typ = base
1192 }
1193 break
1194 }
1195
1196 check.unary(x, e)
1197 if x.mode == invalid {
1198 goto Error
1199 }
1200 if e.Op == syntax.Recv {
1201 x.expr = e
1202 return statement
1203 }
1204 break
1205 }
1206
1207
1208 check.binary(x, e, e.X, e.Y, e.Op)
1209 if x.mode == invalid {
1210 goto Error
1211 }
1212
1213 case *syntax.KeyValueExpr:
1214
1215 check.error(e, InvalidSyntaxTree, "no key:value expected")
1216 goto Error
1217
1218 case *syntax.ArrayType, *syntax.SliceType, *syntax.StructType, *syntax.FuncType,
1219 *syntax.InterfaceType, *syntax.MapType, *syntax.ChanType:
1220 x.mode = typexpr
1221 x.typ = check.typ(e)
1222
1223
1224
1225
1226
1227
1228 default:
1229 panic(fmt.Sprintf("%s: unknown expression type %T", atPos(e), e))
1230 }
1231
1232
1233 x.expr = e
1234 return expression
1235
1236 Error:
1237 x.mode = invalid
1238 x.expr = e
1239 return statement
1240 }
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250 func keyVal(x constant.Value) interface{} {
1251 switch x.Kind() {
1252 case constant.Complex:
1253 f := constant.ToFloat(x)
1254 if f.Kind() != constant.Float {
1255 r, _ := constant.Float64Val(constant.Real(x))
1256 i, _ := constant.Float64Val(constant.Imag(x))
1257 return complex(r, i)
1258 }
1259 x = f
1260 fallthrough
1261 case constant.Float:
1262 i := constant.ToInt(x)
1263 if i.Kind() != constant.Int {
1264 v, _ := constant.Float64Val(x)
1265 return v
1266 }
1267 x = i
1268 fallthrough
1269 case constant.Int:
1270 if v, ok := constant.Int64Val(x); ok {
1271 return v
1272 }
1273 if v, ok := constant.Uint64Val(x); ok {
1274 return v
1275 }
1276 case constant.String:
1277 return constant.StringVal(x)
1278 case constant.Bool:
1279 return constant.BoolVal(x)
1280 }
1281 return x
1282 }
1283
1284
1285 func (check *Checker) typeAssertion(e syntax.Expr, x *operand, T Type, typeSwitch bool) {
1286 var cause string
1287 if check.assertableTo(x.typ, T, &cause) {
1288 return
1289 }
1290
1291 if typeSwitch {
1292 check.errorf(e, ImpossibleAssert, "impossible type switch case: %s\n\t%s cannot have dynamic type %s %s", e, x, T, cause)
1293 return
1294 }
1295
1296 check.errorf(e, ImpossibleAssert, "impossible type assertion: %s\n\t%s does not implement %s %s", e, T, x.typ, cause)
1297 }
1298
1299
1300
1301
1302
1303
1304 func (check *Checker) expr(T *target, x *operand, e syntax.Expr) {
1305 check.rawExpr(T, x, e, nil, false)
1306 check.exclude(x, 1<<novalue|1<<builtin|1<<typexpr)
1307 check.singleValue(x)
1308 }
1309
1310
1311 func (check *Checker) genericExpr(x *operand, e syntax.Expr) {
1312 check.rawExpr(nil, x, e, nil, true)
1313 check.exclude(x, 1<<novalue|1<<builtin|1<<typexpr)
1314 check.singleValue(x)
1315 }
1316
1317
1318
1319
1320
1321
1322 func (check *Checker) multiExpr(e syntax.Expr, allowCommaOk bool) (list []*operand, commaOk bool) {
1323 var x operand
1324 check.rawExpr(nil, &x, e, nil, false)
1325 check.exclude(&x, 1<<novalue|1<<builtin|1<<typexpr)
1326
1327 if t, ok := x.typ.(*Tuple); ok && x.mode != invalid {
1328
1329 list = make([]*operand, t.Len())
1330 for i, v := range t.vars {
1331 list[i] = &operand{mode: value, expr: e, typ: v.typ}
1332 }
1333 return
1334 }
1335
1336
1337 list = []*operand{&x}
1338 if allowCommaOk && (x.mode == mapindex || x.mode == commaok || x.mode == commaerr) {
1339 x2 := &operand{mode: value, expr: e, typ: Typ[UntypedBool]}
1340 if x.mode == commaerr {
1341 x2.typ = universeError
1342 }
1343 list = append(list, x2)
1344 commaOk = true
1345 }
1346
1347 return
1348 }
1349
1350
1351
1352
1353 func (check *Checker) exprWithHint(x *operand, e syntax.Expr, hint Type) {
1354 assert(hint != nil)
1355 check.rawExpr(nil, x, e, hint, false)
1356 check.exclude(x, 1<<novalue|1<<builtin|1<<typexpr)
1357 check.singleValue(x)
1358 }
1359
1360
1361
1362
1363
1364 func (check *Checker) exprOrType(x *operand, e syntax.Expr, allowGeneric bool) {
1365 check.rawExpr(nil, x, e, nil, allowGeneric)
1366 check.exclude(x, 1<<novalue)
1367 check.singleValue(x)
1368 }
1369
1370
1371
1372 func (check *Checker) exclude(x *operand, modeset uint) {
1373 if modeset&(1<<x.mode) != 0 {
1374 var msg string
1375 var code Code
1376 switch x.mode {
1377 case novalue:
1378 if modeset&(1<<typexpr) != 0 {
1379 msg = "%s used as value"
1380 } else {
1381 msg = "%s used as value or type"
1382 }
1383 code = TooManyValues
1384 case builtin:
1385 msg = "%s must be called"
1386 code = UncalledBuiltin
1387 case typexpr:
1388 msg = "%s is not an expression"
1389 code = NotAnExpr
1390 default:
1391 panic("unreachable")
1392 }
1393 check.errorf(x, code, msg, x)
1394 x.mode = invalid
1395 }
1396 }
1397
1398
1399 func (check *Checker) singleValue(x *operand) {
1400 if x.mode == value {
1401
1402 if t, ok := x.typ.(*Tuple); ok {
1403 assert(t.Len() != 1)
1404 check.errorf(x, TooManyValues, "multiple-value %s in single-value context", x)
1405 x.mode = invalid
1406 }
1407 }
1408 }
1409
1410
1411 var op2tok = [...]token.Token{
1412 syntax.Def: token.ILLEGAL,
1413 syntax.Not: token.NOT,
1414 syntax.Recv: token.ILLEGAL,
1415
1416 syntax.OrOr: token.LOR,
1417 syntax.AndAnd: token.LAND,
1418
1419 syntax.Eql: token.EQL,
1420 syntax.Neq: token.NEQ,
1421 syntax.Lss: token.LSS,
1422 syntax.Leq: token.LEQ,
1423 syntax.Gtr: token.GTR,
1424 syntax.Geq: token.GEQ,
1425
1426 syntax.Add: token.ADD,
1427 syntax.Sub: token.SUB,
1428 syntax.Or: token.OR,
1429 syntax.Xor: token.XOR,
1430
1431 syntax.Mul: token.MUL,
1432 syntax.Div: token.QUO,
1433 syntax.Rem: token.REM,
1434 syntax.And: token.AND,
1435 syntax.AndNot: token.AND_NOT,
1436 syntax.Shl: token.SHL,
1437 syntax.Shr: token.SHR,
1438 }
1439
View as plain text