aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/internal/gc/swt.go
blob: 7c25041b461496fffcda03dfad0c457e281d168b (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
// 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"
	"fmt"
)

const (
	Snorm = 0 + iota
	Strue
	Sfalse
	Stype
	Tdefault
	Texprconst
	Texprvar
	Ttypenil
	Ttypeconst
	Ttypevar
	Ncase = 4
)

type Case struct {
	node    *Node
	hash    uint32
	type_   uint8
	diag    uint8
	ordinal uint16
	link    *Case
}

var C *Case

func dumpcase(c0 *Case) {
	var c *Case

	for c = c0; c != nil; c = c.link {
		switch c.type_ {
		case Tdefault:
			fmt.Printf("case-default\n")
			fmt.Printf("\tord=%d\n", c.ordinal)

		case Texprconst:
			fmt.Printf("case-exprconst\n")
			fmt.Printf("\tord=%d\n", c.ordinal)

		case Texprvar:
			fmt.Printf("case-exprvar\n")
			fmt.Printf("\tord=%d\n", c.ordinal)
			fmt.Printf("\top=%v\n", Oconv(int(c.node.Left.Op), 0))

		case Ttypenil:
			fmt.Printf("case-typenil\n")
			fmt.Printf("\tord=%d\n", c.ordinal)

		case Ttypeconst:
			fmt.Printf("case-typeconst\n")
			fmt.Printf("\tord=%d\n", c.ordinal)
			fmt.Printf("\thash=%x\n", c.hash)

		case Ttypevar:
			fmt.Printf("case-typevar\n")
			fmt.Printf("\tord=%d\n", c.ordinal)

		default:
			fmt.Printf("case-???\n")
			fmt.Printf("\tord=%d\n", c.ordinal)
			fmt.Printf("\top=%v\n", Oconv(int(c.node.Left.Op), 0))
			fmt.Printf("\thash=%x\n", c.hash)
		}
	}

	fmt.Printf("\n")
}

func ordlcmp(c1 *Case, c2 *Case) int {
	// sort default first
	if c1.type_ == Tdefault {
		return -1
	}
	if c2.type_ == Tdefault {
		return +1
	}

	// sort nil second
	if c1.type_ == Ttypenil {
		return -1
	}
	if c2.type_ == Ttypenil {
		return +1
	}

	// sort by ordinal
	if c1.ordinal > c2.ordinal {
		return +1
	}
	if c1.ordinal < c2.ordinal {
		return -1
	}
	return 0
}

func exprcmp(c1 *Case, c2 *Case) int {
	var ct int
	var n int
	var n1 *Node
	var n2 *Node

	// sort non-constants last
	if c1.type_ != Texprconst {
		return +1
	}
	if c2.type_ != Texprconst {
		return -1
	}

	n1 = c1.node.Left
	n2 = c2.node.Left

	// sort by type (for switches on interface)
	ct = int(n1.Val.Ctype)

	if ct != int(n2.Val.Ctype) {
		return ct - int(n2.Val.Ctype)
	}
	if !Eqtype(n1.Type, n2.Type) {
		if n1.Type.Vargen > n2.Type.Vargen {
			return +1
		} else {
			return -1
		}
	}

	// sort by constant value
	n = 0

	switch ct {
	case CTFLT:
		n = mpcmpfltflt(n1.Val.U.Fval, n2.Val.U.Fval)

	case CTINT,
		CTRUNE:
		n = Mpcmpfixfix(n1.Val.U.Xval, n2.Val.U.Xval)

	case CTSTR:
		n = cmpslit(n1, n2)
	}

	return n
}

func typecmp(c1 *Case, c2 *Case) int {
	// sort non-constants last
	if c1.type_ != Ttypeconst {
		return +1
	}
	if c2.type_ != Ttypeconst {
		return -1
	}

	// sort by hash code
	if c1.hash > c2.hash {
		return +1
	}
	if c1.hash < c2.hash {
		return -1
	}

	// sort by ordinal so duplicate error
	// happens on later case.
	if c1.ordinal > c2.ordinal {
		return +1
	}
	if c1.ordinal < c2.ordinal {
		return -1
	}
	return 0
}

func csort(l *Case, f func(*Case, *Case) int) *Case {
	var l1 *Case
	var l2 *Case
	var le *Case

	if l == nil || l.link == nil {
		return l
	}

	l1 = l
	l2 = l
	for {
		l2 = l2.link
		if l2 == nil {
			break
		}
		l2 = l2.link
		if l2 == nil {
			break
		}
		l1 = l1.link
	}

	l2 = l1.link
	l1.link = nil
	l1 = csort(l, f)
	l2 = csort(l2, f)

	/* set up lead element */
	if f(l1, l2) < 0 {
		l = l1
		l1 = l1.link
	} else {
		l = l2
		l2 = l2.link
	}

	le = l

	for {
		if l1 == nil {
			for l2 != nil {
				le.link = l2
				le = l2
				l2 = l2.link
			}

			le.link = nil
			break
		}

		if l2 == nil {
			for l1 != nil {
				le.link = l1
				le = l1
				l1 = l1.link
			}

			break
		}

		if f(l1, l2) < 0 {
			le.link = l1
			le = l1
			l1 = l1.link
		} else {
			le.link = l2
			le = l2
			l2 = l2.link
		}
	}

	le.link = nil
	return l
}

var newlabel_swt_label int

func newlabel_swt() *Node {
	newlabel_swt_label++
	namebuf = fmt.Sprintf("%.6d", newlabel_swt_label)
	return newname(Lookup(namebuf))
}

/*
 * build separate list of statements and cases
 * make labels between cases and statements
 * deal with fallthrough, break, unreachable statements
 */
func casebody(sw *Node, typeswvar *Node) {
	var n *Node
	var c *Node
	var last *Node
	var def *Node
	var cas *NodeList
	var stat *NodeList
	var l *NodeList
	var lc *NodeList
	var go_ *Node
	var br *Node
	var lno int32
	var needvar bool

	if sw.List == nil {
		return
	}

	lno = setlineno(sw)

	cas = nil  // cases
	stat = nil // statements
	def = nil  // defaults
	br = Nod(OBREAK, nil, nil)

	for l = sw.List; l != nil; l = l.Next {
		n = l.N
		setlineno(n)
		if n.Op != OXCASE {
			Fatal("casebody %v", Oconv(int(n.Op), 0))
		}
		n.Op = OCASE
		needvar = count(n.List) != 1 || n.List.N.Op == OLITERAL

		go_ = Nod(OGOTO, newlabel_swt(), nil)
		if n.List == nil {
			if def != nil {
				Yyerror("more than one default case")
			}

			// reuse original default case
			n.Right = go_

			def = n
		}

		if n.List != nil && n.List.Next == nil {
			// one case - reuse OCASE node.
			c = n.List.N

			n.Left = c
			n.Right = go_
			n.List = nil
			cas = list(cas, n)
		} else {
			// expand multi-valued cases
			for lc = n.List; lc != nil; lc = lc.Next {
				c = lc.N
				cas = list(cas, Nod(OCASE, c, go_))
			}
		}

		stat = list(stat, Nod(OLABEL, go_.Left, nil))
		if typeswvar != nil && needvar && n.Nname != nil {
			var l *NodeList

			l = list1(Nod(ODCL, n.Nname, nil))
			l = list(l, Nod(OAS, n.Nname, typeswvar))
			typechecklist(l, Etop)
			stat = concat(stat, l)
		}

		stat = concat(stat, n.Nbody)

		// botch - shouldn't fall thru declaration
		last = stat.End.N

		if last.Xoffset == n.Xoffset && last.Op == OXFALL {
			if typeswvar != nil {
				setlineno(last)
				Yyerror("cannot fallthrough in type switch")
			}

			if l.Next == nil {
				setlineno(last)
				Yyerror("cannot fallthrough final case in switch")
			}

			last.Op = OFALL
		} else {
			stat = list(stat, br)
		}
	}

	stat = list(stat, br)
	if def != nil {
		cas = list(cas, def)
	}

	sw.List = cas
	sw.Nbody = stat
	lineno = lno
}

func mkcaselist(sw *Node, arg int) *Case {
	var n *Node
	var c *Case
	var c1 *Case
	var c2 *Case
	var l *NodeList
	var ord int

	c = nil
	ord = 0

	for l = sw.List; l != nil; l = l.Next {
		n = l.N
		c1 = new(Case)
		c1.link = c
		c = c1

		ord++
		if int(uint16(ord)) != ord {
			Fatal("too many cases in switch")
		}
		c.ordinal = uint16(ord)
		c.node = n

		if n.Left == nil {
			c.type_ = Tdefault
			continue
		}

		switch arg {
		case Stype:
			c.hash = 0
			if n.Left.Op == OLITERAL {
				c.type_ = Ttypenil
				continue
			}

			if Istype(n.Left.Type, TINTER) {
				c.type_ = Ttypevar
				continue
			}

			c.hash = typehash(n.Left.Type)
			c.type_ = Ttypeconst
			continue

		case Snorm,
			Strue,
			Sfalse:
			c.type_ = Texprvar
			c.hash = typehash(n.Left.Type)
			switch consttype(n.Left) {
			case CTFLT,
				CTINT,
				CTRUNE,
				CTSTR:
				c.type_ = Texprconst
			}

			continue
		}
	}

	if c == nil {
		return nil
	}

	// sort by value and diagnose duplicate cases
	switch arg {
	case Stype:
		c = csort(c, typecmp)
		for c1 = c; c1 != nil; c1 = c1.link {
			for c2 = c1.link; c2 != nil && c2.hash == c1.hash; c2 = c2.link {
				if c1.type_ == Ttypenil || c1.type_ == Tdefault {
					break
				}
				if c2.type_ == Ttypenil || c2.type_ == Tdefault {
					break
				}
				if !Eqtype(c1.node.Left.Type, c2.node.Left.Type) {
					continue
				}
				yyerrorl(int(c2.node.Lineno), "duplicate case %v in type switch\n\tprevious case at %v", Tconv(c2.node.Left.Type, 0), c1.node.Line())
			}
		}

	case Snorm,
		Strue,
		Sfalse:
		c = csort(c, exprcmp)
		for c1 = c; c1.link != nil; c1 = c1.link {
			if exprcmp(c1, c1.link) != 0 {
				continue
			}
			setlineno(c1.link.node)
			Yyerror("duplicate case %v in switch\n\tprevious case at %v", Nconv(c1.node.Left, 0), c1.node.Line())
		}
	}

	// put list back in processing order
	c = csort(c, ordlcmp)

	return c
}

var exprname *Node

func exprbsw(c0 *Case, ncase int, arg int) *Node {
	var cas *NodeList
	var a *Node
	var n *Node
	var c *Case
	var i int
	var half int
	var lno int

	cas = nil
	if ncase < Ncase {
		for i = 0; i < ncase; i++ {
			n = c0.node
			lno = int(setlineno(n))

			if (arg != Strue && arg != Sfalse) || assignop(n.Left.Type, exprname.Type, nil) == OCONVIFACE || assignop(exprname.Type, n.Left.Type, nil) == OCONVIFACE {
				a = Nod(OIF, nil, nil)
				a.Ntest = Nod(OEQ, exprname, n.Left) // if name == val
				typecheck(&a.Ntest, Erv)
				a.Nbody = list1(n.Right) // then goto l
			} else if arg == Strue {
				a = Nod(OIF, nil, nil)
				a.Ntest = n.Left         // if val
				a.Nbody = list1(n.Right) // then goto l // arg == Sfalse
			} else {
				a = Nod(OIF, nil, nil)
				a.Ntest = Nod(ONOT, n.Left, nil) // if !val
				typecheck(&a.Ntest, Erv)
				a.Nbody = list1(n.Right) // then goto l
			}

			cas = list(cas, a)
			c0 = c0.link
			lineno = int32(lno)
		}

		return liststmt(cas)
	}

	// find the middle and recur
	c = c0

	half = ncase >> 1
	for i = 1; i < half; i++ {
		c = c.link
	}
	a = Nod(OIF, nil, nil)
	a.Ntest = Nod(OLE, exprname, c.node.Left)
	typecheck(&a.Ntest, Erv)
	a.Nbody = list1(exprbsw(c0, half, arg))
	a.Nelse = list1(exprbsw(c.link, ncase-half, arg))
	return a
}

/*
 * normal (expression) switch.
 * rebuild case statements into if .. goto
 */
func exprswitch(sw *Node) {
	var def *Node
	var cas *NodeList
	var a *Node
	var c0 *Case
	var c *Case
	var c1 *Case
	var t *Type
	var arg int
	var ncase int

	casebody(sw, nil)

	arg = Snorm
	if Isconst(sw.Ntest, CTBOOL) {
		arg = Strue
		if sw.Ntest.Val.U.Bval == 0 {
			arg = Sfalse
		}
	}

	walkexpr(&sw.Ntest, &sw.Ninit)
	t = sw.Type
	if t == nil {
		return
	}

	/*
	 * convert the switch into OIF statements
	 */
	exprname = nil

	cas = nil
	if arg == Strue || arg == Sfalse {
		exprname = Nodbool(arg == Strue)
	} else if consttype(sw.Ntest) >= 0 {
		// leave constants to enable dead code elimination (issue 9608)
		exprname = sw.Ntest
	} else {
		exprname = temp(sw.Ntest.Type)
		cas = list1(Nod(OAS, exprname, sw.Ntest))
		typechecklist(cas, Etop)
	}

	c0 = mkcaselist(sw, arg)
	if c0 != nil && c0.type_ == Tdefault {
		def = c0.node.Right
		c0 = c0.link
	} else {
		def = Nod(OBREAK, nil, nil)
	}

loop:
	if c0 == nil {
		cas = list(cas, def)
		sw.Nbody = concat(cas, sw.Nbody)
		sw.List = nil
		walkstmtlist(sw.Nbody)
		return
	}

	// deal with the variables one-at-a-time
	if okforcmp[t.Etype] == 0 || c0.type_ != Texprconst {
		a = exprbsw(c0, 1, arg)
		cas = list(cas, a)
		c0 = c0.link
		goto loop
	}

	// do binary search on run of constants
	ncase = 1

	for c = c0; c.link != nil; c = c.link {
		if c.link.type_ != Texprconst {
			break
		}
		ncase++
	}

	// break the chain at the count
	c1 = c.link

	c.link = nil

	// sort and compile constants
	c0 = csort(c0, exprcmp)

	a = exprbsw(c0, ncase, arg)
	cas = list(cas, a)

	c0 = c1
	goto loop
}

var hashname *Node

var facename *Node

var boolname *Node

func typeone(t *Node) *Node {
	var init *NodeList
	var a *Node
	var b *Node
	var var_ *Node

	var_ = t.Nname
	init = nil
	if var_ == nil {
		typecheck(&nblank, Erv|Easgn)
		var_ = nblank
	} else {
		init = list1(Nod(ODCL, var_, nil))
	}

	a = Nod(OAS2, nil, nil)
	a.List = list(list1(var_), boolname) // var,bool =
	b = Nod(ODOTTYPE, facename, nil)
	b.Type = t.Left.Type // interface.(type)
	a.Rlist = list1(b)
	typecheck(&a, Etop)
	init = list(init, a)

	b = Nod(OIF, nil, nil)
	b.Ntest = boolname
	b.Nbody = list1(t.Right) // if bool { goto l }
	a = liststmt(list(init, b))
	return a
}

func typebsw(c0 *Case, ncase int) *Node {
	var cas *NodeList
	var a *Node
	var n *Node
	var c *Case
	var i int
	var half int

	cas = nil

	if ncase < Ncase {
		for i = 0; i < ncase; i++ {
			n = c0.node
			if c0.type_ != Ttypeconst {
				Fatal("typebsw")
			}
			a = Nod(OIF, nil, nil)
			a.Ntest = Nod(OEQ, hashname, Nodintconst(int64(c0.hash)))
			typecheck(&a.Ntest, Erv)
			a.Nbody = list1(n.Right)
			cas = list(cas, a)
			c0 = c0.link
		}

		return liststmt(cas)
	}

	// find the middle and recur
	c = c0

	half = ncase >> 1
	for i = 1; i < half; i++ {
		c = c.link
	}
	a = Nod(OIF, nil, nil)
	a.Ntest = Nod(OLE, hashname, Nodintconst(int64(c.hash)))
	typecheck(&a.Ntest, Erv)
	a.Nbody = list1(typebsw(c0, half))
	a.Nelse = list1(typebsw(c.link, ncase-half))
	return a
}

/*
 * convert switch of the form
 *	switch v := i.(type) { case t1: ..; case t2: ..; }
 * into if statements
 */
func typeswitch(sw *Node) {
	var def *Node
	var cas *NodeList
	var hash *NodeList
	var a *Node
	var n *Node
	var c *Case
	var c0 *Case
	var c1 *Case
	var ncase int
	var t *Type
	var v Val

	if sw.Ntest == nil {
		return
	}
	if sw.Ntest.Right == nil {
		setlineno(sw)
		Yyerror("type switch must have an assignment")
		return
	}

	walkexpr(&sw.Ntest.Right, &sw.Ninit)
	if !Istype(sw.Ntest.Right.Type, TINTER) {
		Yyerror("type switch must be on an interface")
		return
	}

	cas = nil

	/*
	 * predeclare temporary variables
	 * and the boolean var
	 */
	facename = temp(sw.Ntest.Right.Type)

	a = Nod(OAS, facename, sw.Ntest.Right)
	typecheck(&a, Etop)
	cas = list(cas, a)

	casebody(sw, facename)

	boolname = temp(Types[TBOOL])
	typecheck(&boolname, Erv)

	hashname = temp(Types[TUINT32])
	typecheck(&hashname, Erv)

	t = sw.Ntest.Right.Type
	if isnilinter(t) {
		a = syslook("efacethash", 1)
	} else {
		a = syslook("ifacethash", 1)
	}
	argtype(a, t)
	a = Nod(OCALL, a, nil)
	a.List = list1(facename)
	a = Nod(OAS, hashname, a)
	typecheck(&a, Etop)
	cas = list(cas, a)

	c0 = mkcaselist(sw, Stype)
	if c0 != nil && c0.type_ == Tdefault {
		def = c0.node.Right
		c0 = c0.link
	} else {
		def = Nod(OBREAK, nil, nil)
	}

	/*
	 * insert if statement into each case block
	 */
	for c = c0; c != nil; c = c.link {
		n = c.node
		switch c.type_ {
		case Ttypenil:
			v.Ctype = CTNIL
			a = Nod(OIF, nil, nil)
			a.Ntest = Nod(OEQ, facename, nodlit(v))
			typecheck(&a.Ntest, Erv)
			a.Nbody = list1(n.Right) // if i==nil { goto l }
			n.Right = a

		case Ttypevar,
			Ttypeconst:
			n.Right = typeone(n)
		}
	}

	/*
	 * generate list of if statements, binary search for constant sequences
	 */
	for c0 != nil {
		if c0.type_ != Ttypeconst {
			n = c0.node
			cas = list(cas, n.Right)
			c0 = c0.link
			continue
		}

		// identify run of constants
		c = c0
		c1 = c

		for c.link != nil && c.link.type_ == Ttypeconst {
			c = c.link
		}
		c0 = c.link
		c.link = nil

		// sort by hash
		c1 = csort(c1, typecmp)

		// for debugging: linear search
		if false {
			for c = c1; c != nil; c = c.link {
				n = c.node
				cas = list(cas, n.Right)
			}

			continue
		}

		// combine adjacent cases with the same hash
		ncase = 0

		for c = c1; c != nil; c = c.link {
			ncase++
			hash = list1(c.node.Right)
			for c.link != nil && c.link.hash == c.hash {
				hash = list(hash, c.link.node.Right)
				c.link = c.link.link
			}

			c.node.Right = liststmt(hash)
		}

		// binary search among cases to narrow by hash
		cas = list(cas, typebsw(c1, ncase))
	}

	if nerrors == 0 {
		cas = list(cas, def)
		sw.Nbody = concat(cas, sw.Nbody)
		sw.List = nil
		walkstmtlist(sw.Nbody)
	}
}

func walkswitch(sw *Node) {
	/*
	 * reorder the body into (OLIST, cases, statements)
	 * cases have OGOTO into statements.
	 * both have inserted OBREAK statements
	 */
	if sw.Ntest == nil {
		sw.Ntest = Nodbool(true)
		typecheck(&sw.Ntest, Erv)
	}

	if sw.Ntest.Op == OTYPESW {
		typeswitch(sw)

		//dump("sw", sw);
		return
	}

	exprswitch(sw)

	// Discard old AST elements after a walk. They can confuse racewealk.
	sw.Ntest = nil

	sw.List = nil
}

/*
 * type check switch statement
 */
func typecheckswitch(n *Node) {
	var top int
	var lno int
	var ptr int
	var nilonly string
	var t *Type
	var badtype *Type
	var missing *Type
	var have *Type
	var l *NodeList
	var ll *NodeList
	var ncase *Node
	var nvar *Node
	var def *Node

	lno = int(lineno)
	typechecklist(n.Ninit, Etop)
	nilonly = ""

	if n.Ntest != nil && n.Ntest.Op == OTYPESW {
		// type switch
		top = Etype

		typecheck(&n.Ntest.Right, Erv)
		t = n.Ntest.Right.Type
		if t != nil && t.Etype != TINTER {
			Yyerror("cannot type switch on non-interface value %v", Nconv(n.Ntest.Right, obj.FmtLong))
		}
	} else {
		// value switch
		top = Erv

		if n.Ntest != nil {
			typecheck(&n.Ntest, Erv)
			defaultlit(&n.Ntest, nil)
			t = n.Ntest.Type
		} else {
			t = Types[TBOOL]
		}
		if t != nil {
			if okforeq[t.Etype] == 0 {
				Yyerror("cannot switch on %v", Nconv(n.Ntest, obj.FmtLong))
			} else if t.Etype == TARRAY && !Isfixedarray(t) {
				nilonly = "slice"
			} else if t.Etype == TARRAY && Isfixedarray(t) && algtype1(t, nil) == ANOEQ {
				Yyerror("cannot switch on %v", Nconv(n.Ntest, obj.FmtLong))
			} else if t.Etype == TSTRUCT && algtype1(t, &badtype) == ANOEQ {
				Yyerror("cannot switch on %v (struct containing %v cannot be compared)", Nconv(n.Ntest, obj.FmtLong), Tconv(badtype, 0))
			} else if t.Etype == TFUNC {
				nilonly = "func"
			} else if t.Etype == TMAP {
				nilonly = "map"
			}
		}
	}

	n.Type = t

	def = nil
	for l = n.List; l != nil; l = l.Next {
		ncase = l.N
		setlineno(n)
		if ncase.List == nil {
			// default
			if def != nil {
				Yyerror("multiple defaults in switch (first at %v)", def.Line())
			} else {
				def = ncase
			}
		} else {
			for ll = ncase.List; ll != nil; ll = ll.Next {
				setlineno(ll.N)
				typecheck(&ll.N, Erv|Etype)
				if ll.N.Type == nil || t == nil {
					continue
				}
				setlineno(ncase)
				switch top {
				case Erv: // expression switch
					defaultlit(&ll.N, t)

					if ll.N.Op == OTYPE {
						Yyerror("type %v is not an expression", Tconv(ll.N.Type, 0))
					} else if ll.N.Type != nil && assignop(ll.N.Type, t, nil) == 0 && assignop(t, ll.N.Type, nil) == 0 {
						if n.Ntest != nil {
							Yyerror("invalid case %v in switch on %v (mismatched types %v and %v)", Nconv(ll.N, 0), Nconv(n.Ntest, 0), Tconv(ll.N.Type, 0), Tconv(t, 0))
						} else {
							Yyerror("invalid case %v in switch (mismatched types %v and bool)", Nconv(ll.N, 0), Tconv(ll.N.Type, 0))
						}
					} else if nilonly != "" && !Isconst(ll.N, CTNIL) {
						Yyerror("invalid case %v in switch (can only compare %s %v to nil)", Nconv(ll.N, 0), nilonly, Nconv(n.Ntest, 0))
					}

				case Etype: // type switch
					if ll.N.Op == OLITERAL && Istype(ll.N.Type, TNIL) {
					} else if ll.N.Op != OTYPE && ll.N.Type != nil { // should this be ||?
						Yyerror("%v is not a type", Nconv(ll.N, obj.FmtLong))

						// reset to original type
						ll.N = n.Ntest.Right
					} else if ll.N.Type.Etype != TINTER && t.Etype == TINTER && !implements(ll.N.Type, t, &missing, &have, &ptr) {
						if have != nil && missing.Broke == 0 && have.Broke == 0 {
							Yyerror("impossible type switch case: %v cannot have dynamic type %v"+" (wrong type for %v method)\n\thave %v%v\n\twant %v%v", Nconv(n.Ntest.Right, obj.FmtLong), Tconv(ll.N.Type, 0), Sconv(missing.Sym, 0), Sconv(have.Sym, 0), Tconv(have.Type, obj.FmtShort), Sconv(missing.Sym, 0), Tconv(missing.Type, obj.FmtShort))
						} else if missing.Broke == 0 {
							Yyerror("impossible type switch case: %v cannot have dynamic type %v"+" (missing %v method)", Nconv(n.Ntest.Right, obj.FmtLong), Tconv(ll.N.Type, 0), Sconv(missing.Sym, 0))
						}
					}
				}
			}
		}

		if top == Etype && n.Type != nil {
			ll = ncase.List
			nvar = ncase.Nname
			if nvar != nil {
				if ll != nil && ll.Next == nil && ll.N.Type != nil && !Istype(ll.N.Type, TNIL) {
					// single entry type switch
					nvar.Ntype = typenod(ll.N.Type)
				} else {
					// multiple entry type switch or default
					nvar.Ntype = typenod(n.Type)
				}

				typecheck(&nvar, Erv|Easgn)
				ncase.Nname = nvar
			}
		}

		typechecklist(ncase.Nbody, Etop)
	}

	lineno = int32(lno)
}