/
typecheck.go
3989 lines (3482 loc) · 81.6 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/internal/obj"
"cmd/internal/src"
"fmt"
"math"
"strings"
)
const (
Etop = 1 << iota // evaluated at statement level
Erv // evaluated in value context
Etype // evaluated in type context
Ecall // call-only expressions are ok
Efnstruct // multivalue function returns are ok
Easgn // assigning to expression
Ecomplit // type in composite literal
)
// type check 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) *Node {
if n != nil && n.Op == ONONAME && n.Sym != nil {
r := n.Sym.Def
if r != nil {
if r.Op != OIOTA {
n = r
} else if len(typecheckdefstack) > 0 {
x := typecheckdefstack[len(typecheckdefstack)-1]
if x.Op == OLITERAL {
n = nodintconst(x.Iota())
}
}
}
}
return n
}
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",
TPTR32: "pointer",
TPTR64: "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 *Type) string {
if t.IsSlice() {
return "slice"
}
et := t.Etype
if int(et) < len(_typekind) {
s := _typekind[et]
if s != "" {
return s
}
}
return fmt.Sprintf("etype=%d", et)
}
// sprint_depchain prints a dependency chain of nodes into trace.
// It is used by typecheck in the case of OLITERAL nodes
// to print constant definition loops.
func sprint_depchain(trace *string, stack []*Node, cur *Node, first *Node) {
for i := len(stack) - 1; i >= 0; i-- {
if n := stack[i]; n.Op == cur.Op {
if n != first {
sprint_depchain(trace, stack[:i], n, first)
}
*trace += fmt.Sprintf("\n\t%v: %v uses %v", n.Line(), n, cur)
return
}
}
}
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) *Node {
// cannot type check until all the source has been parsed
if !typecheckok {
Fatalf("early typecheck")
}
if n == nil {
return nil
}
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&(Erv|Etype) == Etype {
yyerror("%v is not a type", n)
}
case OTYPE:
if top&Etype == Etype {
var trace string
sprint_depchain(&trace, typecheck_tcstack, n, n)
yyerrorl(n.Pos, "invalid recursive type alias %v%s", n, trace)
}
case OLITERAL:
if top&(Erv|Etype) == Etype {
yyerror("%v is not a type", n)
break
}
var trace string
sprint_depchain(&trace, typecheck_tcstack, n, n)
yyerrorl(n.Pos, "constant definition loop%s", trace)
}
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.Typecheck = 2
typecheck_tcstack = append(typecheck_tcstack, n)
n = typecheck1(n, top)
n.Typecheck = 1
last := len(typecheck_tcstack) - 1
typecheck_tcstack[last] = nil
typecheck_tcstack = typecheck_tcstack[:last]
lineno = lno
return n
}
// does n contain a call or receive operation?
func callrecv(n *Node) bool {
if n == nil {
return false
}
switch n.Op {
case OCALL,
OCALLMETH,
OCALLINTER,
OCALLFUNC,
ORECV,
OCAP,
OLEN,
OCOPY,
ONEW,
OAPPEND,
ODELETE:
return true
}
return callrecv(n.Left) || callrecv(n.Right) || callrecvlist(n.Ninit) || callrecvlist(n.Nbody) || callrecvlist(n.List) || callrecvlist(n.Rlist)
}
func callrecvlist(l Nodes) bool {
for _, n := range l.Slice() {
if callrecv(n) {
return true
}
}
return false
}
// indexlit implements typechecking of untyped values as
// array/slice indexes. It is equivalent to defaultlit
// except for constants of numerical kind, which are acceptable
// whenever they can be represented by a value of type int.
// 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.IsUntyped() {
return n
}
switch consttype(n) {
case CTINT, CTRUNE, CTFLT, CTCPLX:
n = defaultlit(n, Types[TINT])
}
n = defaultlit(n, nil)
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) *Node {
switch n.Op {
case OXDOT, ODOT, ODOTPTR, ODOTMETH, ODOTINTER:
// n.Sym is a field/method name, not a variable.
default:
if n.Sym != nil {
if n.Op == ONAME && n.Etype != 0 && top&Ecall == 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
OpSwitch:
switch n.Op {
// until typecheck is complete, do nothing.
default:
Dump("typecheck", n)
Fatalf("typecheck %v", n.Op)
// names
case OLITERAL:
ok |= Erv
if n.Type == nil && n.Val().Ctype() == CTSTR {
n.Type = idealstring
}
break OpSwitch
case ONONAME:
ok |= Erv
break OpSwitch
case ONAME:
if n.Name.Decldepth == 0 {
n.Name.Decldepth = decldepth
}
if n.Etype != 0 {
ok |= Ecall
break OpSwitch
}
if top&Easgn == 0 {
// not a write to the variable
if isblank(n) {
yyerror("cannot use _ as value")
n.Type = nil
return n
}
n.Used = true
}
ok |= Erv
break OpSwitch
case OPACK:
yyerror("use of package %v without selector", n.Sym)
n.Type = nil
return n
case ODDD:
break
// types (OIND is with exprs)
case OTYPE:
ok |= Etype
if n.Type == nil {
return n
}
case OTARRAY:
ok |= Etype
r := typecheck(n.Right, Etype)
if r.Type == nil {
n.Type = nil
return n
}
var t *Type
if n.Left == nil {
t = typSlice(r.Type)
} else if n.Left.Op == ODDD {
if top&Ecomplit == 0 {
if !n.Diag {
n.Diag = true
yyerror("use of [...] array outside of array literal")
}
n.Type = nil
return n
}
t = typDDDArray(r.Type)
} else {
n.Left = indexlit(typecheck(n.Left, Erv))
l := n.Left
if consttype(l) != CTINT {
if l.Type != nil && l.Type.IsInteger() && l.Op != OLITERAL {
yyerror("non-constant array bound %v", l)
} else {
yyerror("invalid array bound %v", l)
}
n.Type = nil
return n
}
v := l.Val()
if doesoverflow(v, 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 = typArray(r.Type, bound)
}
n.Op = OTYPE
n.Type = t
n.Left = nil
n.Right = nil
if !t.isDDDArray() {
checkwidth(t)
}
case OTMAP:
ok |= Etype
n.Left = typecheck(n.Left, Etype)
n.Right = typecheck(n.Right, Etype)
l := n.Left
r := n.Right
if l.Type == nil || r.Type == nil {
n.Type = nil
return n
}
if l.Type.NotInHeap {
yyerror("go:notinheap map key not allowed")
}
if r.Type.NotInHeap {
yyerror("go:notinheap map value not allowed")
}
n.Op = OTYPE
n.Type = typMap(l.Type, r.Type)
// map key validation
alg, bad := algtype1(l.Type)
if alg == ANOEQ {
if bad.Etype == TFORW {
// queue check for map until all the types are done settling.
mapqueue = append(mapqueue, mapqueueval{l, n.Pos})
} else if bad.Etype != TANY {
// no need to queue, key is already bad
yyerror("invalid map key type %v", l.Type)
}
}
n.Left = nil
n.Right = nil
case OTCHAN:
ok |= Etype
n.Left = typecheck(n.Left, Etype)
l := n.Left
if l.Type == nil {
n.Type = nil
return n
}
if l.Type.NotInHeap {
yyerror("chan of go:notinheap type not allowed")
}
t := typChan(l.Type, ChanDir(n.Etype)) // TODO(marvin): Fix Node.EType type union.
n.Op = OTYPE
n.Type = t
n.Left = nil
n.Etype = 0
case OTSTRUCT:
ok |= Etype
n.Op = OTYPE
n.Type = tostruct(n.List.Slice())
if n.Type == nil || n.Type.Broke {
n.Type = nil
return n
}
n.List.Set(nil)
case OTINTER:
ok |= Etype
n.Op = OTYPE
n.Type = tointerface(n.List.Slice())
if n.Type == nil {
return n
}
case OTFUNC:
ok |= Etype
n.Op = OTYPE
n.Type = functype(n.Left, n.List.Slice(), n.Rlist.Slice())
if n.Type == nil {
return n
}
n.Left = nil
n.List.Set(nil)
n.Rlist.Set(nil)
// type or expr
case OIND:
n.Left = typecheck(n.Left, Erv|Etype|top&Ecomplit)
l := n.Left
t := l.Type
if t == nil {
n.Type = nil
return n
}
if l.Op == OTYPE {
ok |= Etype
n.Op = OTYPE
n.Type = ptrto(l.Type)
n.Left = nil
break OpSwitch
}
if !t.IsPtr() {
if top&(Erv|Etop) != 0 {
yyerror("invalid indirect of %L", n.Left)
n.Type = nil
return n
}
break OpSwitch
}
ok |= Erv
n.Type = t.Elem()
break OpSwitch
// arithmetic exprs
case OASOP,
OADD,
OAND,
OANDAND,
OANDNOT,
ODIV,
OEQ,
OGE,
OGT,
OHMUL,
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 |= Etop
n.Left = typecheck(n.Left, Erv)
n.Right = typecheck(n.Right, Erv)
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 = Op(n.Etype)
} else {
ok |= Erv
n.Left = typecheck(n.Left, Erv)
n.Right = typecheck(n.Right, Erv)
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[TUINT])
n.Right = r
t := r.Type
if !t.IsInteger() || t.IsSigned() {
yyerror("invalid operation: %v (shift count type %v, must be unsigned integer)", 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
break OpSwitch
}
// 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
}
var aop Op = OXXX
if iscmp[n.Op] && t.Etype != TIDEAL && !eqtype(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.
if r.Type.Etype != TBLANK {
aop = assignop(l.Type, r.Type, nil)
if aop != 0 {
if r.Type.IsInterface() && !l.Type.IsInterface() && !l.Type.IsComparable() {
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.Typecheck = 1
n.Left = l
}
t = r.Type
goto converted
}
}
if l.Type.Etype != TBLANK {
aop = assignop(r.Type, l.Type, nil)
if aop != 0 {
if l.Type.IsInterface() && !r.Type.IsInterface() && !r.Type.IsComparable() {
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.Typecheck = 1
n.Right = r
}
t = l.Type
}
}
converted:
et = t.Etype
}
if t.Etype != TIDEAL && !eqtype(l.Type, r.Type) {
l, r = defaultlit2(l, r, true)
if r.Type.IsInterface() == l.Type.IsInterface() || aop == 0 {
yyerror("invalid operation: %v (mismatched types %v and %v)", n, l.Type, r.Type)
n.Type = nil
return n
}
}
if !okfor[op][et] {
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() && !l.Type.IsComparable() {
yyerror("invalid operation: %v (%v cannot be compared)", n, l.Type)
n.Type = nil
return n
}
if l.Type.IsSlice() && !isnil(l) && !isnil(r) {
yyerror("invalid operation: %v (slice can only be compared to nil)", n)
n.Type = nil
return n
}
if l.Type.IsMap() && !isnil(l) && !isnil(r) {
yyerror("invalid operation: %v (map can only be compared to nil)", n)
n.Type = nil
return n
}
if l.Type.Etype == TFUNC && !isnil(l) && !isnil(r) {
yyerror("invalid operation: %v (func can only be compared to nil)", n)
n.Type = nil
return n
}
if l.Type.IsStruct() {
if f := l.Type.IncomparableField(); f != nil {
yyerror("invalid operation: %v (struct containing %v cannot be compared)", n, f.Type)
n.Type = nil
return n
}
}
t = l.Type
if iscmp[n.Op] {
evconst(n)
t = idealbool
if n.Op != OLITERAL {
l, r = defaultlit2(l, r, true)
n.Left = l
n.Right = r
}
}
if et == TSTRING {
if iscmp[n.Op] {
// TODO(marvin): Fix Node.EType type union.
n.Etype = EType(n.Op)
n.Op = OCMPSTR
} else if 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 et == TINTER {
if l.Op == OLITERAL && l.Val().Ctype() == CTNIL {
// swap for back end
n.Left = r
n.Right = l
} else if r.Op == OLITERAL && r.Val().Ctype() == CTNIL {
} else // leave alone for back end
if r.Type.IsInterface() == l.Type.IsInterface() {
// TODO(marvin): Fix Node.EType type union.
n.Etype = EType(n.Op)
n.Op = OCMPIFACE
}
}
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
break OpSwitch
case OCOM, OMINUS, ONOT, OPLUS:
ok |= Erv
n.Left = typecheck(n.Left, Erv)
l := n.Left
t := l.Type
if t == nil {
n.Type = nil
return n
}
if !okfor[n.Op][t.Etype] {
yyerror("invalid operation: %v %v", n.Op, t)
n.Type = nil
return n
}
n.Type = t
break OpSwitch
// exprs
case OADDR:
ok |= Erv
n.Left = typecheck(n.Left, Erv)
if n.Left.Type == nil {
n.Type = nil
return n
}
checklvalue(n.Left, "take the address of")
r := outervalue(n.Left)
var l *Node
for l = n.Left; l != r; l = l.Left {
l.Addrtaken = true
if l.isClosureVar() {
l.Name.Defn.Addrtaken = true
}
}
if l.Orig != l && l.Op == ONAME {
Fatalf("found non-orig name node %v", l)
}
l.Addrtaken = true
if l.isClosureVar() {
l.Name.Defn.Addrtaken = true
}
n.Left = defaultlit(n.Left, nil)
l = n.Left
t := l.Type
if t == nil {
n.Type = nil
return n
}
n.Type = ptrto(t)
break OpSwitch
case OCOMPLIT:
ok |= Erv
n = typecheckcomplit(n)
if n.Type == nil {
return n
}
break OpSwitch
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, Erv|Etype)
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 {
if !looktypedot(n, t, 0) {
if looktypedot(n, t, 1) {
yyerror("%v undefined (cannot refer to unexported method %v)", n, n.Sym)
} else {
yyerror("%v undefined (type %v has no method %v)", n, t, n.Sym)
}
n.Type = nil
return n
}
if n.Type.Etype != TFUNC || !n.IsMethod() {
yyerror("type %v has no method %S", n.Left.Type, n.Sym)
n.Type = nil
return n
}
n.Op = ONAME
if n.Name == nil {
n.Name = new(Name)
}
n.Right = newname(n.Sym)
n.Type = methodfunc(n.Type, n.Left.Type)
n.Xoffset = 0
n.Class = PFUNC
ok = Erv
break OpSwitch
}
if t.IsPtr() && !t.Elem().IsInterface() {
t = t.Elem()
if t == nil {
n.Type = nil
return n
}
n.Op = ODOTPTR
checkwidth(t)
}
if isblanksym(n.Sym) {
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 { // 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&Ecall != 0 {
ok |= Ecall
} else {
typecheckpartialcall(n, s)
ok |= Erv
}
default:
ok |= Erv
}
break OpSwitch
case ODOTTYPE:
ok |= Erv
n.Left = typecheck(n.Left, Erv)
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, Etype)
n.Type = n.Right.Type
n.Right = nil
if n.Type == nil {
return n
}
}
if n.Type != nil && !n.Type.IsInterface() {
var missing, have *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)
} else {
yyerror("impossible type assertion:\n\t%v does not implement %v (missing %v method)", n.Type, t, missing.Sym)
}
n.Type = nil
return n
}
}
break OpSwitch
case OINDEX:
ok |= Erv
n.Left = typecheck(n.Left, Erv)
n.Left = defaultlit(n.Left, nil)
n.Left = implicitstar(n.Left)
l := n.Left
n.Right = typecheck(n.Right, Erv)
r := n.Right