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