-
Notifications
You must be signed in to change notification settings - Fork 17.7k
/
typecheck.go
4019 lines (3518 loc) · 88.8 KB
/
typecheck.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 gc
import (
"cmd/compile/internal/types"
"fmt"
"strings"
)
// To enable tracing support (-t flag), set enableTrace to true.
const enableTrace = false
var trace bool
var traceIndent []byte
var skipDowidthForTracing bool
func tracePrint(title string, n *Node) func(np **Node) {
indent := traceIndent
// guard against nil
var pos, op string
var tc uint8
if n != nil {
pos = linestr(n.Pos)
op = n.Op.String()
tc = n.Typecheck()
}
skipDowidthForTracing = true
defer func() { skipDowidthForTracing = false }()
fmt.Printf("%s: %s%s %p %s %v tc=%d\n", pos, indent, title, n, op, n, tc)
traceIndent = append(traceIndent, ". "...)
return func(np **Node) {
traceIndent = traceIndent[:len(traceIndent)-2]
// if we have a result, use that
if np != nil {
n = *np
}
// guard against nil
// use outer pos, op so we don't get empty pos/op if n == nil (nicer output)
var tc uint8
var typ *types.Type
if n != nil {
pos = linestr(n.Pos)
op = n.Op.String()
tc = n.Typecheck()
typ = n.Type
}
skipDowidthForTracing = true
defer func() { skipDowidthForTracing = false }()
fmt.Printf("%s: %s=> %p %s %v tc=%d type=%#L\n", pos, indent, n, op, n, tc, typ)
}
}
const (
ctxStmt = 1 << iota // evaluated at statement level
ctxExpr // evaluated in value context
ctxType // evaluated in type context
ctxCallee // call-only expressions are ok
ctxMultiOK // multivalue function returns are ok
ctxAssign // assigning to expression
)
// type checks the whole tree of an expression.
// calculates expression types.
// evaluates compile time constants.
// marks variables that escape the local frame.
// rewrites n.Op to be more specific in some cases.
var typecheckdefstack []*Node
// resolve ONONAME to definition, if any.
func resolve(n *Node) (res *Node) {
if n == nil || n.Op != ONONAME {
return n
}
// only trace if there's work to do
if enableTrace && trace {
defer tracePrint("resolve", n)(&res)
}
if n.Sym.Pkg != localpkg {
if inimport {
Fatalf("recursive inimport")
}
inimport = true
expandDecl(n)
inimport = false
return n
}
r := asNode(n.Sym.Def)
if r == nil {
return n
}
if r.Op == OIOTA {
if x := getIotaValue(); x >= 0 {
return nodintconst(x)
}
return n
}
return r
}
func typecheckslice(l []*Node, top int) {
for i := range l {
l[i] = typecheck(l[i], top)
}
}
var _typekind = []string{
TINT: "int",
TUINT: "uint",
TINT8: "int8",
TUINT8: "uint8",
TINT16: "int16",
TUINT16: "uint16",
TINT32: "int32",
TUINT32: "uint32",
TINT64: "int64",
TUINT64: "uint64",
TUINTPTR: "uintptr",
TCOMPLEX64: "complex64",
TCOMPLEX128: "complex128",
TFLOAT32: "float32",
TFLOAT64: "float64",
TBOOL: "bool",
TSTRING: "string",
TPTR: "pointer",
TUNSAFEPTR: "unsafe.Pointer",
TSTRUCT: "struct",
TINTER: "interface",
TCHAN: "chan",
TMAP: "map",
TARRAY: "array",
TSLICE: "slice",
TFUNC: "func",
TNIL: "nil",
TIDEAL: "untyped number",
}
func typekind(t *types.Type) string {
if t.IsUntyped() {
return fmt.Sprintf("%v", t)
}
et := t.Etype
if int(et) < len(_typekind) {
s := _typekind[et]
if s != "" {
return s
}
}
return fmt.Sprintf("etype=%d", et)
}
func cycleFor(start *Node) []*Node {
// Find the start node in typecheck_tcstack.
// We know that it must exist because each time we mark
// a node with n.SetTypecheck(2) we push it on the stack,
// and each time we mark a node with n.SetTypecheck(2) we
// pop it from the stack. We hit a cycle when we encounter
// a node marked 2 in which case is must be on the stack.
i := len(typecheck_tcstack) - 1
for i > 0 && typecheck_tcstack[i] != start {
i--
}
// collect all nodes with same Op
var cycle []*Node
for _, n := range typecheck_tcstack[i:] {
if n.Op == start.Op {
cycle = append(cycle, n)
}
}
return cycle
}
func cycleTrace(cycle []*Node) string {
var s string
for i, n := range cycle {
s += fmt.Sprintf("\n\t%v: %v uses %v", n.Line(), n, cycle[(i+1)%len(cycle)])
}
return s
}
var typecheck_tcstack []*Node
// typecheck type checks node n.
// The result of typecheck MUST be assigned back to n, e.g.
// n.Left = typecheck(n.Left, top)
func typecheck(n *Node, top int) (res *Node) {
// cannot type check until all the source has been parsed
if !typecheckok {
Fatalf("early typecheck")
}
if n == nil {
return nil
}
// only trace if there's work to do
if enableTrace && trace {
defer tracePrint("typecheck", n)(&res)
}
lno := setlineno(n)
// Skip over parens.
for n.Op == OPAREN {
n = n.Left
}
// Resolve definition of name and value of iota lazily.
n = resolve(n)
// Skip typecheck if already done.
// But re-typecheck ONAME/OTYPE/OLITERAL/OPACK node in case context has changed.
if n.Typecheck() == 1 {
switch n.Op {
case ONAME, OTYPE, OLITERAL, OPACK:
break
default:
lineno = lno
return n
}
}
if n.Typecheck() == 2 {
// Typechecking loop. Trying printing a meaningful message,
// otherwise a stack trace of typechecking.
switch n.Op {
// We can already diagnose variables used as types.
case ONAME:
if top&(ctxExpr|ctxType) == ctxType {
yyerror("%v is not a type", n)
}
case OTYPE:
// Only report a type cycle if we are expecting a type.
// Otherwise let other code report an error.
if top&ctxType == ctxType {
// A cycle containing only alias types is an error
// since it would expand indefinitely when aliases
// are substituted.
cycle := cycleFor(n)
for _, n1 := range cycle {
if n1.Name != nil && !n1.Name.Param.Alias() {
// Cycle is ok. But if n is an alias type and doesn't
// have a type yet, we have a recursive type declaration
// with aliases that we can't handle properly yet.
// Report an error rather than crashing later.
if n.Name != nil && n.Name.Param.Alias() && n.Type == nil {
lineno = n.Pos
Fatalf("cannot handle alias type declaration (issue #25838): %v", n)
}
lineno = lno
return n
}
}
yyerrorl(n.Pos, "invalid recursive type alias %v%s", n, cycleTrace(cycle))
}
case OLITERAL:
if top&(ctxExpr|ctxType) == ctxType {
yyerror("%v is not a type", n)
break
}
yyerrorl(n.Pos, "constant definition loop%s", cycleTrace(cycleFor(n)))
}
if nsavederrors+nerrors == 0 {
var trace string
for i := len(typecheck_tcstack) - 1; i >= 0; i-- {
x := typecheck_tcstack[i]
trace += fmt.Sprintf("\n\t%v %v", x.Line(), x)
}
yyerror("typechecking loop involving %v%s", n, trace)
}
lineno = lno
return n
}
n.SetTypecheck(2)
typecheck_tcstack = append(typecheck_tcstack, n)
n = typecheck1(n, top)
n.SetTypecheck(1)
last := len(typecheck_tcstack) - 1
typecheck_tcstack[last] = nil
typecheck_tcstack = typecheck_tcstack[:last]
lineno = lno
return n
}
// indexlit implements typechecking of untyped values as
// array/slice indexes. It is almost equivalent to defaultlit
// but also accepts untyped numeric values representable as
// value of type int (see also checkmake for comparison).
// The result of indexlit MUST be assigned back to n, e.g.
// n.Left = indexlit(n.Left)
func indexlit(n *Node) *Node {
if n != nil && n.Type != nil && n.Type.Etype == TIDEAL {
return defaultlit(n, types.Types[TINT])
}
return n
}
// The result of typecheck1 MUST be assigned back to n, e.g.
// n.Left = typecheck1(n.Left, top)
func typecheck1(n *Node, top int) (res *Node) {
if enableTrace && trace {
defer tracePrint("typecheck1", n)(&res)
}
switch n.Op {
case OLITERAL, ONAME, ONONAME, OTYPE:
if n.Sym == nil {
break
}
if n.Op == ONAME && n.SubOp() != 0 && top&ctxCallee == 0 {
yyerror("use of builtin %v not in function call", n.Sym)
n.Type = nil
return n
}
typecheckdef(n)
if n.Op == ONONAME {
n.Type = nil
return n
}
}
ok := 0
switch n.Op {
// until typecheck is complete, do nothing.
default:
Dump("typecheck", n)
Fatalf("typecheck %v", n.Op)
// names
case OLITERAL:
ok |= ctxExpr
if n.Type == nil && n.Val().Ctype() == CTSTR {
n.Type = types.UntypedString
}
case ONONAME:
ok |= ctxExpr
case ONAME:
if n.Name.Decldepth == 0 {
n.Name.Decldepth = decldepth
}
if n.SubOp() != 0 {
ok |= ctxCallee
break
}
if top&ctxAssign == 0 {
// not a write to the variable
if n.isBlank() {
yyerror("cannot use _ as value")
n.Type = nil
return n
}
n.Name.SetUsed(true)
}
ok |= ctxExpr
case OPACK:
yyerror("use of package %v without selector", n.Sym)
n.Type = nil
return n
case ODDD:
break
// types (ODEREF is with exprs)
case OTYPE:
ok |= ctxType
if n.Type == nil {
return n
}
case OTARRAY:
ok |= ctxType
r := typecheck(n.Right, ctxType)
if r.Type == nil {
n.Type = nil
return n
}
var t *types.Type
if n.Left == nil {
t = types.NewSlice(r.Type)
} else if n.Left.Op == ODDD {
if !n.Diag() {
n.SetDiag(true)
yyerror("use of [...] array outside of array literal")
}
n.Type = nil
return n
} else {
n.Left = indexlit(typecheck(n.Left, ctxExpr))
l := n.Left
if consttype(l) != CTINT {
switch {
case l.Type == nil:
// Error already reported elsewhere.
case l.Type.IsInteger() && l.Op != OLITERAL:
yyerror("non-constant array bound %v", l)
default:
yyerror("invalid array bound %v", l)
}
n.Type = nil
return n
}
v := l.Val()
if doesoverflow(v, types.Types[TINT]) {
yyerror("array bound is too large")
n.Type = nil
return n
}
bound := v.U.(*Mpint).Int64()
if bound < 0 {
yyerror("array bound must be non-negative")
n.Type = nil
return n
}
t = types.NewArray(r.Type, bound)
}
setTypeNode(n, t)
n.Left = nil
n.Right = nil
checkwidth(t)
case OTMAP:
ok |= ctxType
n.Left = typecheck(n.Left, ctxType)
n.Right = typecheck(n.Right, ctxType)
l := n.Left
r := n.Right
if l.Type == nil || r.Type == nil {
n.Type = nil
return n
}
if l.Type.NotInHeap() {
yyerror("incomplete (or unallocatable) map key not allowed")
}
if r.Type.NotInHeap() {
yyerror("incomplete (or unallocatable) map value not allowed")
}
setTypeNode(n, types.NewMap(l.Type, r.Type))
mapqueue = append(mapqueue, n) // check map keys when all types are settled
n.Left = nil
n.Right = nil
case OTCHAN:
ok |= ctxType
n.Left = typecheck(n.Left, ctxType)
l := n.Left
if l.Type == nil {
n.Type = nil
return n
}
if l.Type.NotInHeap() {
yyerror("chan of incomplete (or unallocatable) type not allowed")
}
setTypeNode(n, types.NewChan(l.Type, n.TChanDir()))
n.Left = nil
n.ResetAux()
case OTSTRUCT:
ok |= ctxType
setTypeNode(n, tostruct(n.List.Slice()))
n.List.Set(nil)
case OTINTER:
ok |= ctxType
setTypeNode(n, tointerface(n.List.Slice()))
case OTFUNC:
ok |= ctxType
setTypeNode(n, functype(n.Left, n.List.Slice(), n.Rlist.Slice()))
n.Left = nil
n.List.Set(nil)
n.Rlist.Set(nil)
// type or expr
case ODEREF:
n.Left = typecheck(n.Left, ctxExpr|ctxType)
l := n.Left
t := l.Type
if t == nil {
n.Type = nil
return n
}
if l.Op == OTYPE {
ok |= ctxType
setTypeNode(n, types.NewPtr(l.Type))
n.Left = nil
// Ensure l.Type gets dowidth'd for the backend. Issue 20174.
checkwidth(l.Type)
break
}
if !t.IsPtr() {
if top&(ctxExpr|ctxStmt) != 0 {
yyerror("invalid indirect of %L", n.Left)
n.Type = nil
return n
}
break
}
ok |= ctxExpr
n.Type = t.Elem()
// arithmetic exprs
case OASOP,
OADD,
OAND,
OANDAND,
OANDNOT,
ODIV,
OEQ,
OGE,
OGT,
OLE,
OLT,
OLSH,
ORSH,
OMOD,
OMUL,
ONE,
OOR,
OOROR,
OSUB,
OXOR:
var l *Node
var op Op
var r *Node
if n.Op == OASOP {
ok |= ctxStmt
n.Left = typecheck(n.Left, ctxExpr)
n.Right = typecheck(n.Right, ctxExpr)
l = n.Left
r = n.Right
checkassign(n, n.Left)
if l.Type == nil || r.Type == nil {
n.Type = nil
return n
}
if n.Implicit() && !okforarith[l.Type.Etype] {
yyerror("invalid operation: %v (non-numeric type %v)", n, l.Type)
n.Type = nil
return n
}
// TODO(marvin): Fix Node.EType type union.
op = n.SubOp()
} else {
ok |= ctxExpr
n.Left = typecheck(n.Left, ctxExpr)
n.Right = typecheck(n.Right, ctxExpr)
l = n.Left
r = n.Right
if l.Type == nil || r.Type == nil {
n.Type = nil
return n
}
op = n.Op
}
if op == OLSH || op == ORSH {
r = defaultlit(r, types.Types[TUINT])
n.Right = r
t := r.Type
if !t.IsInteger() {
yyerror("invalid operation: %v (shift count type %v, must be integer)", n, r.Type)
n.Type = nil
return n
}
if t.IsSigned() && !langSupported(1, 13, curpkg()) {
yyerrorv("go1.13", "invalid operation: %v (signed shift count type %v)", n, r.Type)
n.Type = nil
return n
}
t = l.Type
if t != nil && t.Etype != TIDEAL && !t.IsInteger() {
yyerror("invalid operation: %v (shift of type %v)", n, t)
n.Type = nil
return n
}
// no defaultlit for left
// the outer context gives the type
n.Type = l.Type
if (l.Type == types.UntypedFloat || l.Type == types.UntypedComplex) && r.Op == OLITERAL {
n.Type = types.UntypedInt
}
break
}
// For "x == x && len(s)", it's better to report that "len(s)" (type int)
// can't be used with "&&" than to report that "x == x" (type untyped bool)
// can't be converted to int (see issue #41500).
if n.Op == OANDAND || n.Op == OOROR {
if !n.Left.Type.IsBoolean() {
yyerror("invalid operation: %v (operator %v not defined on %s)", n, n.Op, typekind(n.Left.Type))
n.Type = nil
return n
}
if !n.Right.Type.IsBoolean() {
yyerror("invalid operation: %v (operator %v not defined on %s)", n, n.Op, typekind(n.Right.Type))
n.Type = nil
return n
}
}
// ideal mixed with non-ideal
l, r = defaultlit2(l, r, false)
n.Left = l
n.Right = r
if l.Type == nil || r.Type == nil {
n.Type = nil
return n
}
t := l.Type
if t.Etype == TIDEAL {
t = r.Type
}
et := t.Etype
if et == TIDEAL {
et = TINT
}
aop := OXXX
if iscmp[n.Op] && t.Etype != TIDEAL && !types.Identical(l.Type, r.Type) {
// comparison is okay as long as one side is
// assignable to the other. convert so they have
// the same type.
//
// the only conversion that isn't a no-op is concrete == interface.
// in that case, check comparability of the concrete type.
// The conversion allocates, so only do it if the concrete type is huge.
converted := false
if r.Type.Etype != TBLANK {
aop, _ = assignop(l.Type, r.Type)
if aop != OXXX {
if r.Type.IsInterface() && !l.Type.IsInterface() && !IsComparable(l.Type) {
yyerror("invalid operation: %v (operator %v not defined on %s)", n, op, typekind(l.Type))
n.Type = nil
return n
}
dowidth(l.Type)
if r.Type.IsInterface() == l.Type.IsInterface() || l.Type.Width >= 1<<16 {
l = nod(aop, l, nil)
l.Type = r.Type
l.SetTypecheck(1)
n.Left = l
}
t = r.Type
converted = true
}
}
if !converted && l.Type.Etype != TBLANK {
aop, _ = assignop(r.Type, l.Type)
if aop != OXXX {
if l.Type.IsInterface() && !r.Type.IsInterface() && !IsComparable(r.Type) {
yyerror("invalid operation: %v (operator %v not defined on %s)", n, op, typekind(r.Type))
n.Type = nil
return n
}
dowidth(r.Type)
if r.Type.IsInterface() == l.Type.IsInterface() || r.Type.Width >= 1<<16 {
r = nod(aop, r, nil)
r.Type = l.Type
r.SetTypecheck(1)
n.Right = r
}
t = l.Type
}
}
et = t.Etype
}
if t.Etype != TIDEAL && !types.Identical(l.Type, r.Type) {
l, r = defaultlit2(l, r, true)
if l.Type == nil || r.Type == nil {
n.Type = nil
return n
}
if l.Type.IsInterface() == r.Type.IsInterface() || aop == 0 {
yyerror("invalid operation: %v (mismatched types %v and %v)", n, l.Type, r.Type)
n.Type = nil
return n
}
}
if t.Etype == TIDEAL {
t = mixUntyped(l.Type, r.Type)
}
if dt := defaultType(t); !okfor[op][dt.Etype] {
yyerror("invalid operation: %v (operator %v not defined on %s)", n, op, typekind(t))
n.Type = nil
return n
}
// okfor allows any array == array, map == map, func == func.
// restrict to slice/map/func == nil and nil == slice/map/func.
if l.Type.IsArray() && !IsComparable(l.Type) {
yyerror("invalid operation: %v (%v cannot be compared)", n, l.Type)
n.Type = nil
return n
}
if l.Type.IsSlice() && !l.isNil() && !r.isNil() {
yyerror("invalid operation: %v (slice can only be compared to nil)", n)
n.Type = nil
return n
}
if l.Type.IsMap() && !l.isNil() && !r.isNil() {
yyerror("invalid operation: %v (map can only be compared to nil)", n)
n.Type = nil
return n
}
if l.Type.Etype == TFUNC && !l.isNil() && !r.isNil() {
yyerror("invalid operation: %v (func can only be compared to nil)", n)
n.Type = nil
return n
}
if l.Type.IsStruct() {
if f := IncomparableField(l.Type); f != nil {
yyerror("invalid operation: %v (struct containing %v cannot be compared)", n, f.Type)
n.Type = nil
return n
}
}
if iscmp[n.Op] {
evconst(n)
t = types.UntypedBool
if n.Op != OLITERAL {
l, r = defaultlit2(l, r, true)
n.Left = l
n.Right = r
}
}
if et == TSTRING && n.Op == OADD {
// create OADDSTR node with list of strings in x + y + z + (w + v) + ...
n.Op = OADDSTR
if l.Op == OADDSTR {
n.List.Set(l.List.Slice())
} else {
n.List.Set1(l)
}
if r.Op == OADDSTR {
n.List.AppendNodes(&r.List)
} else {
n.List.Append(r)
}
n.Left = nil
n.Right = nil
}
if (op == ODIV || op == OMOD) && Isconst(r, CTINT) {
if r.Val().U.(*Mpint).CmpInt64(0) == 0 {
yyerror("division by zero")
n.Type = nil
return n
}
}
n.Type = t
case OBITNOT, ONEG, ONOT, OPLUS:
ok |= ctxExpr
n.Left = typecheck(n.Left, ctxExpr)
l := n.Left
t := l.Type
if t == nil {
n.Type = nil
return n
}
if !okfor[n.Op][defaultType(t).Etype] {
yyerror("invalid operation: %v (operator %v not defined on %s)", n, n.Op, typekind(t))
n.Type = nil
return n
}
n.Type = t
// exprs
case OADDR:
ok |= ctxExpr
n.Left = typecheck(n.Left, ctxExpr)
if n.Left.Type == nil {
n.Type = nil
return n
}
switch n.Left.Op {
case OARRAYLIT, OMAPLIT, OSLICELIT, OSTRUCTLIT:
n.Op = OPTRLIT
default:
checklvalue(n.Left, "take the address of")
r := outervalue(n.Left)
if r.Op == ONAME {
if r.Orig != r {
Fatalf("found non-orig name node %v", r) // TODO(mdempsky): What does this mean?
}
r.Name.SetAddrtaken(true)
if r.Name.IsClosureVar() && !capturevarscomplete {
// Mark the original variable as Addrtaken so that capturevars
// knows not to pass it by value.
// But if the capturevars phase is complete, don't touch it,
// in case l.Name's containing function has not yet been compiled.
r.Name.Defn.Name.SetAddrtaken(true)
}
}
n.Left = defaultlit(n.Left, nil)
if n.Left.Type == nil {
n.Type = nil
return n
}
}
n.Type = types.NewPtr(n.Left.Type)
case OCOMPLIT:
ok |= ctxExpr
n = typecheckcomplit(n)
if n.Type == nil {
return n
}
case OXDOT, ODOT:
if n.Op == OXDOT {
n = adddot(n)
n.Op = ODOT
if n.Left == nil {
n.Type = nil
return n
}
}
n.Left = typecheck(n.Left, ctxExpr|ctxType)
n.Left = defaultlit(n.Left, nil)
t := n.Left.Type
if t == nil {
adderrorname(n)
n.Type = nil
return n
}
s := n.Sym
if n.Left.Op == OTYPE {
n = typecheckMethodExpr(n)
if n.Type == nil {
return n
}
ok = ctxExpr
break
}
if t.IsPtr() && !t.Elem().IsInterface() {
t = t.Elem()
if t == nil {
n.Type = nil
return n
}
n.Op = ODOTPTR
checkwidth(t)
}
if n.Sym.IsBlank() {
yyerror("cannot refer to blank field or method")
n.Type = nil
return n
}
if lookdot(n, t, 0) == nil {
// Legitimate field or method lookup failed, try to explain the error
switch {
case t.IsEmptyInterface():
yyerror("%v undefined (type %v is interface with no methods)", n, n.Left.Type)
case t.IsPtr() && t.Elem().IsInterface():
// Pointer to interface is almost always a mistake.
yyerror("%v undefined (type %v is pointer to interface, not interface)", n, n.Left.Type)
case lookdot(n, t, 1) != nil:
// Field or method matches by name, but it is not exported.
yyerror("%v undefined (cannot refer to unexported field or method %v)", n, n.Sym)
default:
if mt := lookdot(n, t, 2); mt != nil && visible(mt.Sym) { // Case-insensitive lookup.
yyerror("%v undefined (type %v has no field or method %v, but does have %v)", n, n.Left.Type, n.Sym, mt.Sym)
} else {
yyerror("%v undefined (type %v has no field or method %v)", n, n.Left.Type, n.Sym)
}
}
n.Type = nil
return n
}
switch n.Op {
case ODOTINTER, ODOTMETH:
if top&ctxCallee != 0 {
ok |= ctxCallee
} else {
typecheckpartialcall(n, s)
ok |= ctxExpr
}
default:
ok |= ctxExpr
}
case ODOTTYPE:
ok |= ctxExpr
n.Left = typecheck(n.Left, ctxExpr)
n.Left = defaultlit(n.Left, nil)
l := n.Left
t := l.Type
if t == nil {
n.Type = nil
return n
}
if !t.IsInterface() {
yyerror("invalid type assertion: %v (non-interface type %v on left)", n, t)
n.Type = nil
return n
}
if n.Right != nil {
n.Right = typecheck(n.Right, ctxType)
n.Type = n.Right.Type
n.Right = nil
if n.Type == nil {
return n
}
}
if n.Type != nil && !n.Type.IsInterface() {
var missing, have *types.Field
var ptr int
if !implements(n.Type, t, &missing, &have, &ptr) {
if have != nil && have.Sym == missing.Sym {
yyerror("impossible type assertion:\n\t%v does not implement %v (wrong type for %v method)\n"+
"\t\thave %v%0S\n\t\twant %v%0S", n.Type, t, missing.Sym, have.Sym, have.Type, missing.Sym, missing.Type)
} else if ptr != 0 {
yyerror("impossible type assertion:\n\t%v does not implement %v (%v method has pointer receiver)", n.Type, t, missing.Sym)
} else if have != nil {
yyerror("impossible type assertion:\n\t%v does not implement %v (missing %v method)\n"+
"\t\thave %v%0S\n\t\twant %v%0S", n.Type, t, missing.Sym, have.Sym, have.Type, missing.Sym, missing.Type)