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
|
// 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.
#include "go.h"
enum
{
Snorm = 0,
Strue,
Sfalse,
Stype,
Tdefault, // default case
Texprconst, // normal constant case
Texprvar, // normal variable case
Ttypenil, // case nil
Ttypeconst, // type hashes
Ttypevar, // interface type
Ncase = 4, // count needed to split
};
typedef struct Case Case;
struct Case
{
Node* node; // points at case statement
uint32 hash; // hash of a type switch
uint8 type; // type of case
uint8 diag; // suppress multiple diagnostics
uint16 ordinal; // position in switch
Case* link; // linked list to link
};
#define C ((Case*)nil)
void
dumpcase(Case *c0)
{
Case *c;
for(c=c0; c!=C; c=c->link) {
switch(c->type) {
case Tdefault:
print("case-default\n");
print(" ord=%d\n", c->ordinal);
break;
case Texprconst:
print("case-exprconst\n");
print(" ord=%d\n", c->ordinal);
break;
case Texprvar:
print("case-exprvar\n");
print(" ord=%d\n", c->ordinal);
print(" op=%O\n", c->node->left->op);
break;
case Ttypenil:
print("case-typenil\n");
print(" ord=%d\n", c->ordinal);
break;
case Ttypeconst:
print("case-typeconst\n");
print(" ord=%d\n", c->ordinal);
print(" hash=%ux\n", c->hash);
break;
case Ttypevar:
print("case-typevar\n");
print(" ord=%d\n", c->ordinal);
break;
default:
print("case-???\n");
print(" ord=%d\n", c->ordinal);
print(" op=%O\n", c->node->left->op);
print(" hash=%ux\n", c->hash);
break;
}
}
print("\n");
}
static int
ordlcmp(Case *c1, Case *c2)
{
// 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;
}
static int
exprcmp(Case *c1, Case *c2)
{
int ct, n;
Node *n1, *n2;
// sort non-constants last
if(c1->type != Texprconst)
return +1;
if(c2->type != Texprconst)
return -1;
n1 = c1->node->left;
n2 = c2->node->left;
ct = n1->val.ctype;
if(ct != n2->val.ctype) {
// invalid program, but return a sort
// order so that we can give a better
// error later.
return ct - n2->val.ctype;
}
// sort by constant value
n = 0;
switch(ct) {
case CTFLT:
n = mpcmpfltflt(n1->val.u.fval, n2->val.u.fval);
break;
case CTINT:
n = mpcmpfixfix(n1->val.u.xval, n2->val.u.xval);
break;
case CTSTR:
n = cmpslit(n1, n2);
break;
}
return n;
}
static int
typecmp(Case *c1, Case *c2)
{
// 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;
}
static Case*
csort(Case *l, int(*f)(Case*, Case*))
{
Case *l1, *l2, *le;
if(l == C || l->link == C)
return l;
l1 = l;
l2 = l;
for(;;) {
l2 = l2->link;
if(l2 == C)
break;
l2 = l2->link;
if(l2 == C)
break;
l1 = l1->link;
}
l2 = l1->link;
l1->link = C;
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 == C) {
while(l2) {
le->link = l2;
le = l2;
l2 = l2->link;
}
le->link = C;
break;
}
if(l2 == C) {
while(l1) {
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 = C;
return l;
}
static Node*
newlabel(void)
{
static int label;
label++;
snprint(namebuf, sizeof(namebuf), "%.6d", label);
return newname(lookup(namebuf));
}
/*
* build separate list of statements and cases
* make labels between cases and statements
* deal with fallthrough, break, unreachable statements
*/
static void
casebody(Node *sw, Node *typeswvar)
{
Node *os, *oc, *n, *c, *last;
Node *def;
NodeList *cas, *stat, *l, *lc;
Node *go, *br;
int32 lno, needvar;
lno = setlineno(sw);
if(sw->list == nil)
return;
cas = nil; // cases
stat = nil; // statements
def = N; // defaults
os = N; // last statement
oc = N; // last case
br = nod(OBREAK, N, N);
for(l=sw->list; l; l=l->next) {
n = l->n;
lno = setlineno(n);
if(n->op != OXCASE)
fatal("casebody %O", n->op);
n->op = OCASE;
needvar = count(n->list) != 1 || n->list->n->op == OLITERAL;
go = nod(OGOTO, newlabel(), N);
if(n->list == nil) {
if(def != N)
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; lc=lc->next) {
c = lc->n;
cas = list(cas, nod(OCASE, c, go));
}
}
stat = list(stat, nod(OLABEL, go->left, N));
if(typeswvar && needvar && n->nname != N) {
NodeList *l;
l = list1(nod(ODCL, n->nname, N));
l = list(l, nod(OAS, n->nname, typeswvar));
typechecklist(l, Etop);
stat = concat(stat, l);
}
stat = concat(stat, n->nbody);
// botch - shouldnt fall thru declaration
last = stat->end->n;
if(last->op == OXFALL) {
if(typeswvar) {
setlineno(last);
yyerror("cannot fallthrough in type switch");
}
last->op = OFALL;
} else
stat = list(stat, br);
}
stat = list(stat, br);
if(def)
cas = list(cas, def);
sw->list = cas;
sw->nbody = stat;
lineno = lno;
}
static Case*
mkcaselist(Node *sw, int arg)
{
Node *n;
Case *c, *c1, *c2;
NodeList *l;
int ord;
c = C;
ord = 0;
for(l=sw->list; l; l=l->next) {
n = l->n;
c1 = mal(sizeof(*c1));
c1->link = c;
c = c1;
ord++;
c->ordinal = ord;
c->node = n;
if(n->left == N) {
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:
case Strue:
case Sfalse:
c->type = Texprvar;
switch(consttype(n->left)) {
case CTFLT:
case CTINT:
case CTSTR:
c->type = Texprconst;
}
continue;
}
}
if(c == C)
return C;
// sort by value and diagnose duplicate cases
switch(arg) {
case Stype:
c = csort(c, typecmp);
for(c1=c; c1!=C; c1=c1->link) {
for(c2=c1->link; c2!=C && 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(c2->node->lineno, "duplicate case in switch\n\tprevious case at %L", c1->node->lineno);
}
}
break;
case Snorm:
case Strue:
case Sfalse:
c = csort(c, exprcmp);
for(c1=c; c1->link!=C; c1=c1->link) {
if(exprcmp(c1, c1->link) != 0)
continue;
setlineno(c1->link->node);
yyerror("duplicate case in switch\n\tprevious case at %L", c1->node->lineno);
}
break;
}
// put list back in processing order
c = csort(c, ordlcmp);
return c;
}
static Node* exprname;
static Node*
exprbsw(Case *c0, int ncase, int arg)
{
NodeList *cas;
Node *a, *n;
Case *c;
int i, half, lno;
cas = nil;
if(ncase < Ncase) {
for(i=0; i<ncase; i++) {
n = c0->node;
lno = setlineno(n);
switch(arg) {
case Strue:
a = nod(OIF, N, N);
a->ntest = n->left; // if val
a->nbody = list1(n->right); // then goto l
break;
case Sfalse:
a = nod(OIF, N, N);
a->ntest = nod(ONOT, n->left, N); // if !val
typecheck(&a->ntest, Erv);
a->nbody = list1(n->right); // then goto l
break;
default:
a = nod(OIF, N, N);
a->ntest = nod(OEQ, exprname, n->left); // if name == val
typecheck(&a->ntest, Erv);
a->nbody = list1(n->right); // then goto l
break;
}
cas = list(cas, a);
c0 = c0->link;
lineno = 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, N, N);
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.
* rebulid case statements into if .. goto
*/
static void
exprswitch(Node *sw)
{
Node *def;
NodeList *cas;
Node *a;
Case *c0, *c, *c1;
Type *t;
int arg, ncase;
casebody(sw, N);
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 == T)
return;
/*
* convert the switch into OIF statements
*/
exprname = N;
cas = nil;
if(arg != Strue && arg != Sfalse) {
exprname = nod(OXXX, N, N);
tempname(exprname, sw->ntest->type);
cas = list1(nod(OAS, exprname, sw->ntest));
typechecklist(cas, Etop);
}
c0 = mkcaselist(sw, arg);
if(c0 != C && c0->type == Tdefault) {
def = c0->node->right;
c0 = c0->link;
} else {
def = nod(OBREAK, N, N);
}
loop:
if(c0 == C) {
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(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!=C; c=c->link) {
if(c->link->type != Texprconst)
break;
ncase++;
}
// break the chain at the count
c1 = c->link;
c->link = C;
// sort and compile constants
c0 = csort(c0, exprcmp);
a = exprbsw(c0, ncase, arg);
cas = list(cas, a);
c0 = c1;
goto loop;
}
static Node* hashname;
static Node* facename;
static Node* boolname;
static Node*
typeone(Node *t)
{
NodeList *init;
Node *a, *b, *var;
var = t->nname;
init = nil;
if(var == N) {
typecheck(&nblank, Erv | Easgn);
var = nblank;
} else
init = list1(nod(ODCL, var, N));
a = nod(OAS2, N, N);
a->list = list(list1(var), boolname); // var,bool =
b = nod(ODOTTYPE, facename, N);
b->type = t->left->type; // interface.(type)
a->rlist = list1(b);
typecheck(&a, Etop);
init = list(init, a);
b = nod(OIF, N, N);
b->ntest = boolname;
b->nbody = list1(t->right); // if bool { goto l }
a = liststmt(list(init, b));
return a;
}
static Node*
typebsw(Case *c0, int ncase)
{
NodeList *cas;
Node *a, *n;
Case *c;
int i, half;
cas = nil;
if(ncase < Ncase) {
for(i=0; i<ncase; i++) {
n = c0->node;
if(c0->type != Ttypeconst)
fatal("typebsw");
a = nod(OIF, N, N);
a->ntest = nod(OEQ, hashname, nodintconst(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, N, N);
a->ntest = nod(OLE, hashname, nodintconst(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
*/
static void
typeswitch(Node *sw)
{
Node *def;
NodeList *cas, *hash;
Node *a, *n;
Case *c, *c0, *c1;
int ncase;
Type *t;
Val v;
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 = nod(OXXX, N, N);
tempname(facename, sw->ntest->right->type);
a = nod(OAS, facename, sw->ntest->right);
typecheck(&a, Etop);
cas = list(cas, a);
casebody(sw, facename);
boolname = nod(OXXX, N, N);
tempname(boolname, types[TBOOL]);
typecheck(&boolname, Erv);
hashname = nod(OXXX, N, N);
tempname(hashname, 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, N);
a->list = list1(facename);
a = nod(OAS, hashname, a);
typecheck(&a, Etop);
cas = list(cas, a);
c0 = mkcaselist(sw, Stype);
if(c0 != C && c0->type == Tdefault) {
def = c0->node->right;
c0 = c0->link;
} else {
def = nod(OBREAK, N, N);
}
/*
* insert if statement into each case block
*/
for(c=c0; c!=C; c=c->link) {
n = c->node;
switch(c->type) {
case Ttypenil:
v.ctype = CTNIL;
a = nod(OIF, N, N);
a->ntest = nod(OEQ, facename, nodlit(v));
typecheck(&a->ntest, Erv);
a->nbody = list1(n->right); // if i==nil { goto l }
n->right = a;
break;
case Ttypevar:
case Ttypeconst:
n->right = typeone(n);
break;
}
}
/*
* generate list of if statements, binary search for constant sequences
*/
while(c0 != C) {
if(c0->type != Ttypeconst) {
n = c0->node;
cas = list(cas, n->right);
c0=c0->link;
continue;
}
// identify run of constants
c1 = c = c0;
while(c->link!=C && 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(0) {
for(c=c1; c!=C; 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!=C; c=c->link) {
ncase++;
hash = list1(c->node->right);
while(c->link != C && 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);
}
}
void
walkswitch(Node *sw)
{
/*
* reorder the body into (OLIST, cases, statements)
* cases have OGOTO into statements.
* both have inserted OBREAK statements
*/
walkstmtlist(sw->ninit);
if(sw->ntest == N) {
sw->ntest = nodbool(1);
typecheck(&sw->ntest, Erv);
}
if(sw->ntest->op == OTYPESW) {
typeswitch(sw);
//dump("sw", sw);
return;
}
exprswitch(sw);
}
/*
* type check switch statement
*/
void
typecheckswitch(Node *n)
{
int top, lno;
Type *t;
NodeList *l, *ll;
Node *ncase, *nvar;
Node *def;
lno = lineno;
typechecklist(n->ninit, Etop);
if(n->ntest != N && n->ntest->op == OTYPESW) {
// type switch
top = Etype;
typecheck(&n->ntest->right, Erv);
t = n->ntest->right->type;
if(t != T && t->etype != TINTER)
yyerror("cannot type switch on non-interface value %+N", n->ntest->right);
} else {
// value switch
top = Erv;
if(n->ntest) {
typecheck(&n->ntest, Erv);
defaultlit(&n->ntest, T);
t = n->ntest->type;
} else
t = types[TBOOL];
}
n->type = t;
def = N;
for(l=n->list; l; l=l->next) {
ncase = l->n;
setlineno(n);
if(ncase->list == nil) {
// default
if(def != N)
yyerror("multiple defaults in switch (first at %L)", def->lineno);
else
def = ncase;
} else {
for(ll=ncase->list; ll; ll=ll->next) {
setlineno(ll->n);
typecheck(&ll->n, Erv | Etype);
if(ll->n->type == T || t == T)
continue;
switch(top) {
case Erv: // expression switch
defaultlit(&ll->n, t);
if(ll->n->op == OTYPE)
yyerror("type %T is not an expression", ll->n->type);
else if(ll->n->type != T && !eqtype(ll->n->type, t))
yyerror("case %+N in %T switch", ll->n, t);
break;
case Etype: // type switch
if(ll->n->op == OLITERAL && istype(ll->n->type, TNIL))
;
else if(ll->n->op != OTYPE && ll->n->type != T)
yyerror("%#N is not a type", ll->n);
break;
}
}
}
if(top == Etype && n->type != T) {
ll = ncase->list;
nvar = ncase->nname;
if(nvar != N) {
if(ll && ll->next == nil && ll->n->type != T && !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);
}
}
}
typechecklist(ncase->nbody, Etop);
}
lineno = lno;
}
|