forked from farisawan-2000/cfront-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcl2.c
1402 lines (1311 loc) · 50.8 KB
/
dcl2.c
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
/*ident "@(#)cls4:src/dcl2.c 1.12" */
/*******************************************************************************
C++ source for the C++ Language System, Release 3.0. This product
is a new release of the original cfront developed in the computer
science research center of AT&T Bell Laboratories.
Copyright (c) 1993 UNIX System Laboratories, Inc.
Copyright (c) 1991, 1992 AT&T and UNIX System Laboratories, Inc.
Copyright (c) 1984, 1989, 1990 AT&T. All Rights Reserved.
THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE of AT&T and UNIX System
Laboratories, Inc. The copyright notice above does not evidence
any actual or intended publication of such source code.
*******************************************************************************/
#include "cfront.h"
#include "size.h"
#include "overload.h"
extern Pexpr make_dot(Pexpr, Ptable, char *s = "i");
extern Pblock top_block;
struct for_info {
char *s;
Pstmt rb;
for_info *next;
};
static for_info *for_info_head = 0;
static int for_check(Pname n, Pstmt rb) {
for_info *p;
if (!n || !rb)
error('i', "for_check(n=%d,rb=%d)", n, rb);
if (n->tp->base == FCT || n->tp->base == OVERLOAD)
return 0;
p = for_info_head;
while (p && (strcmp(p->s, n->string) || p->rb != rb))
p = p->next;
if (p) {
return 1;
} else {
p = new for_info;
p->s = new char[strlen(n->string) + 1];
strcpy(p->s, n->string);
p->rb = rb;
p->next = for_info_head;
for_info_head = p;
return 0;
}
}
void for_check_delete() {
for_info *p;
for_info *r;
p = for_info_head;
while (p) {
r = p;
delete p->s;
p = p->next;
delete r;
}
for_info_head = 0;
}
static Pname is_decl(Pblock cb, Pname v) {
if (!cb->d)
return 0;
for (Pname n = cb->d; n; n = n->n_list) {
if (strcmp(v->string, n->string) == 0)
return n;
}
return 0;
}
static Pname redecl = 0;
Pstmt find_return(Pstmt s, Pname v) {
while (s) {
switch (s->base) {
case RETURN: {
Pname te = s->e->base == NAME ? Pname(s->e) : 0;
if (!te || strcmp(te->string, v->string)) {
return s;
}
if (te == v)
return 0;
if (redecl)
return s;
else
return 0;
}
case BLOCK: {
if (!redecl)
redecl = is_decl(Pblock(s), v);
Pstmt tt = find_return(Pblock(s)->s, v);
if (tt)
return tt;
redecl = 0;
break;
}
case DO:
case WHILE:
case FOR:
case SWITCH:
case CASE:
case DEFAULT: {
Pstmt tt = find_return(s->s, v);
if (tt)
return tt;
break;
}
case IF: {
Pstmt tt = find_return(s->s, v);
if (tt)
return tt;
tt = find_return(s->else_stmt, v);
if (tt)
return tt;
break;
}
}
s = s->s_list;
}
return 0;
}
Pname hier_dominates(Pname on1, Pname on2)
/*
compare for duplicates and dominance:
on1 and on2 are two conversion operator functions
return the the one that dominates the other (according
to the class hierarchy) otherwise 0 (0 thus indicates
ambiguous conversion)
*/
{
Pfct f1 = on1->get_fct();
Pfct f2 = on2->get_fct();
Ptype r1 = f1->returns;
Ptype r2 = f2->returns;
if (r1 == r2 || r1->check(r2, 0) == 0) {
Pclass m1 = f1->memof;
Pclass m2 = f2->memof;
if (m1->has_base(m2))
return on1;
if (m2->has_base(m1))
return on2;
return 0;
}
Pptr p1 = r1->is_ptr_or_ref();
Pptr p2 = r2->is_ptr_or_ref();
if (p1 && p2) {
Pname cn1 = p1->typ->is_cl_obj();
Pname cn2 = p2->typ->is_cl_obj();
if (cn1 && cn2) {
Pclass c1 = Pclass(cn1->tp);
Pclass c2 = Pclass(cn2->tp);
if (c1 && c2) {
if (c1->has_base(c2))
return on1;
else if (c2->has_base(c1))
return on2;
}
}
}
return 0;
}
Bits best_conv(const Block(Pname) & CONV, int &nfound, bit cO) {
Bits zeroBits(0, nfound);
Bits result = ~zeroBits;
Bits tempBits = ~zeroBits;
int sigbit = tempBits.signif() - 1;
tempBits.reset(sigbit);
while (tempBits.count()) {
int tempPtr = tempBits.signif() - 1;
Pname tname = CONV[tempPtr];
for (int k = nfound - 1; k > tempPtr; k--) {
if (!result[k])
continue;
Pname r = really_dominate(tname, CONV[k], cO);
if (r == tname)
result.reset(k);
if (r == CONV[k]) {
result.reset(tempPtr);
break;
}
}
tempBits.reset(tempPtr);
}
nfound = result.count();
return result;
}
Pstmt curr_loop;
Pstmt curr_switch;
Pblock curr_block;
static Pstmt curr_case; // case or default
static Pblock block_since_case; // set to curr_block if it follows curr_case
void stmt::reached() {
register Pstmt ss = s_list;
if (ss == 0)
return;
switch (ss->base) {
case LABEL:
case CASE:
case DEFAULT:
break;
default:
if (ss->where.line != 0)
error('w', &ss->where, "S after%k not reached", base);
else
error('w', "S after%k not reached", base);
for (; ss; ss = ss->s_list) { // delete unreacheable code
switch (ss->base) {
case LABEL:
case CASE:
case DEFAULT: // reachable
s_list = ss;
return;
case DCL: // the dcl may be used later
// keep to avoid cascading errors
case IF:
case DO:
case WHILE:
case SWITCH:
case FOR:
case BLOCK: // may hide a label
s_list = ss;
return;
}
}
s_list = 0;
}
}
bit oper_okay(Ptype t, TOK b) {
if (t->is_ref())
t = Pptr(t)->typ;
switch (t->base) {
case PTR:
if (b == UMINUS)
break;
case FLOAT:
case DOUBLE:
case LDOUBLE:
if (b == COMPL || b == DEREF || b == SWITCH)
break;
case CHAR:
case SHORT:
case INT:
case LONG:
case EOBJ:
return 1;
}
return 0;
}
Pexpr check_cond(Pexpr e, TOK b, Ptable tbl) {
Pname cn;
if (e == dummy)
error("empty condition for %k", b);
int const_obj = 0;
const_obj = e->tp->tconst() ? 1 : e->is_const_obj();
if (cn = e->tp->is_cl_obj()) {
int no_const = 0; // for better error reporting
Pclass cl = Pclass(cn->tp);
Pname found = 0;
Pname other = 0;
int nfound = 0;
Block(Pname) conv;
for (Pname on = cl->conv; on; on = on->n_list) {
Pfct f = on->get_fct();
Ptype t = f->returns->skiptypedefs();
if (oper_okay(t, b) == 1) {
if (const_obj && !f->f_const) {
no_const++;
continue;
}
conv.reserve(nfound + 1);
conv[nfound++] = on;
}
}
if (nfound == 1)
found = conv[nfound - 1];
if (nfound > 1) {
Bits b = best_conv(conv, nfound, const_obj);
int i = b.signif() - 1;
found = conv[i];
if (b.count() == 2) {
b.reset(i);
other = conv[b.signif() - 1];
}
}
switch (nfound) {
case 0:
if (no_const)
error("%nO in%kE (no usable const conversion)", cn, b);
else
error("%nO in%kE", cn, b);
break;
case 1: {
Pname xx = new name(found->string);
Pexpr c;
if (e->lval(0)) {
Pref r = new ref(DOT, e, xx);
Pexpr rr = r->typ(tbl);
c = new expr(G_CALL, rr, 0);
} else { // (temp=init,temp.coerce())
// Pname tmp = make_tmp('U',e->tp,tbl);
// Pexpr ass = init_tmp(tmp,e,tbl);
Pref r = new ref(DOT, e, xx);
Pexpr rr = r->typ(tbl);
c = new expr(G_CALL, rr, 0);
// c = new expr(CM,ass,c);
}
Pexpr E = c->typ(tbl);
if (E->tp->memptr()) {
E = make_dot(E, tbl);
E = new expr(NE, E, zero);
E->tp = int_type;
}
e = E;
break;
}
case 2:
error("two conversions for%nO in%kE: %n and %n", cn, b, found, other);
break;
default:
error("multiple conversions for%nO in%kE", cn, b);
break;
}
return e;
}
if (e->tp->base == FCT) {
e = new expr(G_ADDROF, 0, e);
e = e->typ(tbl);
}
if (e->tp->memptr()) {
if (b == SWITCH) {
error("P toM as switchE");
return e;
}
if (e->base == ILIST || ((e->base == CAST || e->base == G_CAST) && e->e1->base == ILIST)) {
if (e->base != ILIST)
e = e->e1;
if (e->e1->e2 != zero)
e = one;
else
e = zero;
} else {
e = make_dot(e, tbl);
e = new expr(NE, e, zero);
e->tp = int_type;
}
} else if (e->tp->num_ptr(b) == FCT)
error("%k(F)", b);
return e;
}
void stmt::dcl(int forflag)
/*
typecheck statement "this" in scope "curr_block->tbl"
*/
{
Pstmt ss;
Pname n;
Pname nn;
Pstmt ostmt = Cstmt;
DB(if (Ddebug >= 1) error('d', &where, "'%k'->stmt::dcl()", base););
for (ss = this; ss; ss = ss->s_list) {
Pstmt old_loop, old_switch;
Cstmt = ss;
Ptable tbl = curr_block->memtbl;
// error('d',"stmt::dcl %k",ss->base);
switch (ss->base) {
case BREAK:
inline_restr |= 16;
if (curr_loop == 0 && curr_switch == 0)
error("break not in loop or switch");
ss->reached();
break;
case CONTINUE:
inline_restr |= 32;
if (curr_loop == 0)
error("continue not in loop");
ss->reached();
break;
case DEFAULT:
if (curr_switch == 0) {
error("default not in switch");
break;
}
if (curr_switch->has_default)
error("two defaults in switch");
curr_case = ss;
block_since_case = 0;
curr_switch->has_default = ss;
ss->s->s_list = ss->s_list;
ss->s_list = 0;
ss->s->dcl();
break;
case SM: {
if (ss->e == 0)
break;
TOK b = ss->e->base;
switch (b) {
case DUMMY: // null or error
case DTOR: // dummy type destructor
ss->e = 0;
break;
// check for unused results
// don't check operators that are likely
// to be overloaded to represent "actions":
// ! ~ < <= > >= << >>
case EQ:
case NE:
case GT:
case GE:
case LT:
case LE:
case PLUS:
case MINUS:
case REF:
case DOT:
case MUL:
case DIV:
case ADDROF:
case AND:
case OR:
case ER:
case DEREF:
case ANDAND:
case OROR:
case NAME:
case VALUE:
if (ss->e->tp)
break; // avoid looking at generated code
ss->e = ss->e->typ(tbl);
if (ss->e->base == CALL)
break;
if (ss->e->tp->base != VOID) {
if (ss->e->base == DEREF)
error('w', "result of %sE not used", ss->e->e2 ? "[]" : "*");
else {
error('w', "result of%kE not used", b);
if (ss->e->base == NAME && ss->e->tp->base == FCT) {
int i = Pname(ss->e)->n_addr_taken;
Pname(ss->e)->n_addr_taken = 1;
Pname(ss->e)->dcl_print(0);
Pname(ss->e)->n_addr_taken = i;
}
}
if (ss->e->not_simple() == 0)
ss->e = dummy;
}
if (ss->e->base == ILIST) {
Pname tt = make_tmp('A', mptr_type, tbl);
ss->e = mptr_assign(tt, ss->e);
}
break;
default:
ss->e = ss->e->typ(tbl);
}
break;
}
case RETURN: {
Pname fn = cc->nof;
Pfct f = Pfct(fn->tp);
Ptype rt = f->returns->skiptypedefs();
Pexpr v = ss->e;
// error('d',"rt %t",rt);
if (v != dummy) {
if (rt->base == RPTR) {
ref_initializer++;
v = v->typ(tbl);
ref_initializer--;
} else
v = v->typ(tbl);
if (fn->n_oper == CTOR || fn->n_oper == DTOR || rt->base == VOID) {
error("unexpected return value");
// refuse to return the value:
ss->e = dummy;
} else {
switch (rt->base) {
case RPTR:
switch (v->base) {
case NAME:
if (Pname(v)->n_scope == FCT || Pname(v)->n_scope == ARG)
error('w', "R to localV returned");
break;
case ICON:
case CCON:
case FCON:
case STRING:
if (Pptr(rt)->typ->tconst() == 0)
error('w', "R to literal returned");
}
in_return = 1;
v = ref_init(Pptr(rt), v, tbl);
in_return = 0;
if (v->base == G_CM && v->e2->base == G_ADDROF
&& v->e2->e2->base == NAME && is_probably_temp(v->e2->e2->string))
error('w', "R to temporary returned (return value is not lvalue or "
"of wrongT)");
case ANY:
break;
case COBJ:
if (v->base == DEREF) {
Pexpr v1 = v->e1;
if (v1->base == CAST || v1->base == G_CAST) {
Pexpr v2 = v1->e1;
if (v2->base == G_CM) {
// *(T)(e1,e2) => (e1,*(T)e2)
Pexpr v3 = v2->e2;
v2->e2 = v;
v2->tp = v->tp;
v = v2;
v1->e1 = v3;
}
}
}
if (f->f_result) {
if (v->base == G_CM && rt->check(v->tp, 0 /*ASSIGN*/) == 0)
v = replace_temp(v, f->f_result);
else {
if (!f->nrv)
v = class_init(f->f_result->contents(), rt, v, tbl);
Pname rcn = rt->is_cl_obj();
if (Pclass(rcn->tp)->has_itor() == 0) {
// can happen for virtuals and for user defined conversions
v->tp = rt;
v = new expr(ASSIGN, f->f_result->contents(), v);
v->tp = rt;
}
}
} else
v = class_init(0, rt, v, tbl);
break;
case PTR: {
v = ptr_init(Pptr(rt), v, tbl);
if (v->base == ADDROF && v->e2->base == NAME
&& Pname(v->e2)->n_stclass == AUTO)
error('w', "P to local variable%n returned", Pname(v->e2));
if (Pchecked == 0)
goto def;
goto ret_save;
}
case INT:
case CHAR:
case LONG:
case SHORT:
if (Pbase(rt)->b_unsigned && v->base == UMINUS && v->e2->base == ICON)
error('w', "negative returned fromF returning unsigned");
default:
def : {
Pexpr x = try_to_coerce(rt, v, "return value", tbl);
int ct = no_const;
if (x)
v = x;
else if (rt->check(v->tp, ASSIGN)) {
error('e', "bad return valueT for%n:%t (%tX)", fn, v->tp, rt);
if (ct)
error('c', " (no usable const conversion)\n");
else
error('c', "\n");
}
break;
}
}
ret_save:
ss->ret_tp = rt;
ss->e = v;
}
} else {
if (rt->base != VOID)
error("return valueX");
}
ss->reached();
break;
}
case DO: // in DO the stmt is before the test
inline_restr |= 8;
old_loop = curr_loop;
curr_loop = ss;
{
Pstmt st = ss->s;
while (st && st->base == FOR)
st = st->for_init;
if (st && st->base == DCL)
if (st == ss->s)
error("D as onlyS in do-loop");
}
ss->s->dcl();
ss->e = ss->e->typ(tbl);
ss->e = check_cond(ss->e, DO, tbl);
curr_loop = old_loop;
break;
case WHILE:
inline_restr |= 8;
old_loop = curr_loop;
curr_loop = ss;
ss->e = ss->e->typ(tbl);
ss->e = check_cond(ss->e, WHILE, tbl);
{
Pstmt st = ss->s;
while (st && st->base == FOR)
st = st->for_init;
if (st && st->base == DCL)
if (st == ss->s)
error("D as onlyS in while-loop");
}
ss->s->dcl();
curr_loop = old_loop;
break;
case SWITCH: {
int ne = 0;
Pstmt old_case = curr_case;
Pblock old_bsc = block_since_case;
curr_case = 0;
block_since_case = 0;
inline_restr |= 4;
old_switch = curr_switch;
curr_switch = ss;
ss->e = ss->e->typ(tbl);
ss->e = check_cond(ss->e, SWITCH, tbl);
{
Pstmt st = ss->s;
while (st && st->base == FOR)
st = st->for_init;
if (st && st->base == DCL)
if (st == ss->s)
error("D as onlyS in switchS");
}
{
Ptype tt = ss->e->tp->skiptypedefs();
switch (tt->base) {
case EOBJ:
ne = Penum(Pbase(tt)->b_name->tp)->no_of_enumerators;
case ZTYPE:
case ANY:
case CHAR:
case SHORT:
case INT:
case LONG:
case FIELD:
break;
default:
error("%t switchE", ss->e->tp);
}
}
ss->s->dcl();
if (ne) { // see if the number of cases is "close to"
// but not equal to the number of enumerators
int i = 0;
Pstmt cs;
for (cs = ss->case_list; cs; cs = cs->case_list)
i++;
if (i && i != ne) {
if (ne < i) {
ee:
if (ss->has_default == 0)
error('w', "switch (%t)W %d cases (%d enumerators)", ss->e->tp, i, ne);
} else {
switch (ne - i) {
case 1:
if (3 < ne)
goto ee;
case 2:
if (7 < ne)
goto ee;
case 3:
if (23 < ne)
goto ee;
case 4:
if (60 < ne)
goto ee;
case 5:
if (99 < ne)
goto ee;
}
}
}
}
curr_switch = old_switch;
curr_case = old_case;
block_since_case = old_bsc;
break;
}
case CASE:
if (curr_switch == 0) {
error("case not in switch");
break;
}
curr_case = ss;
block_since_case = 0;
ss->e = ss->e->typ(tbl);
ss->e->tp->num_ptr(CASE);
{
Ptype tt = ss->e->tp->skiptypedefs();
switch (tt->base) {
case ZTYPE:
case ANY:
case CHAR:
case SHORT:
case INT:
case LONG:
case EOBJ: {
Neval = 0;
long i = ss->e->eval();
if (Neval == 0) {
Pstmt cs;
if (largest_int < i)
error("long case value");
for (cs = curr_switch->case_list; cs; cs = cs->case_list) {
if (cs->case_value == i)
error("case %d used twice in switch", i);
}
ss->case_value = int(i);
ss->case_list = curr_switch->case_list;
curr_switch->case_list = ss;
} else
error("bad case label: %s", Neval);
break;
}
default:
error("%t caseE", ss->e->tp);
}
}
if (ss->s->s_list)
error('i', "case%k", ss->s->s_list->base);
ss->s->s_list = ss->s_list;
ss->s_list = 0;
ss->s->dcl();
break;
case GOTO:
inline_restr |= 2;
ss->reached();
case LABEL:
// Insert label in function mem table;
// labels have function scope.
n = ss->d;
nn = cc->ftbl->insert(n, LABEL);
// Set a ptr to the mem table corresponding to the scope
// in which the label actually occurred. This allows the
// processing of goto's in the presence of ctors and dtors
if (ss->base == LABEL) {
nn->n_realscope = curr_block->memtbl;
inline_restr |= 1;
}
if (Nold) {
if (ss->base == LABEL) {
if (nn->n_initializer)
error("twoDs of label%n", n);
nn->n_initializer = (Pexpr) 1;
}
if (n != nn)
ss->d = nn;
} else {
if (ss->base == LABEL)
nn->n_initializer = (Pexpr) 1;
nn->where = ss->where;
}
if (ss->base == GOTO)
nn->use();
else {
if (ss->s->s_list)
error('i', "label%k", ss->s->s_list->base);
ss->s->s_list = ss->s_list;
ss->s_list = 0;
nn->assign();
}
if (ss->s)
ss->s->dcl();
break;
case IF: {
Pexpr ee = ss->e->typ(tbl);
if (ee->base == ASSIGN) {
Neval = 0;
(void) ee->e2->eval();
if (Neval == 0)
error('w', "constant assignment in condition");
}
ss->e = ee = check_cond(ee, IF, tbl);
if (ss->s && (ss->s->base == DCL || ss->s->base == FDCL))
error(&ss->s->where, "D as onlyS after `if'");
// pointer to member returns with a tp set to 0
if (ee->tp)
switch (ee->tp->base) {
case INT:
case EOBJ:
case ZTYPE: {
long i;
Neval = 0;
i = ee->eval();
if (Neval == 0) {
Pstmt sl = ss->s_list;
if (i) {
DEL(ss->else_stmt);
ss->s->dcl();
*ss = *ss->s;
} else {
DEL(ss->s);
if (ss->else_stmt) {
ss->else_stmt->dcl();
*ss = *ss->else_stmt;
} else {
ss->base = SM;
ss->e = dummy;
ss->s = 0;
}
}
ss->s_list = sl;
continue;
}
}
}
if (ss->s->base != FDCL)
ss->s->dcl();
if (ss->else_stmt) {
if (ss->else_stmt->base == DCL || ss->else_stmt->base == FDCL)
error(&ss->else_stmt->where, "D as onlyS after `else'");
if (ss->else_stmt->base != FDCL)
ss->else_stmt->dcl();
}
break;
}
case FOR:
inline_restr |= 8;
old_loop = curr_loop;
curr_loop = ss;
if (ss->for_init) {
Pstmt fi = ss->for_init;
switch (fi->base) {
case SM:
if (fi->e == dummy) {
ss->for_init = 0;
break;
}
fi->dcl();
break;
default:
// for (stmt; e1; e2) stmt1 stmt2
// => {stmt; for(; e1; e2) stmt1 stmt2}
// if stmt != declaration
// if stmt == declaration, temporarily
// rewrite to avoid symbol table
// problems in some contexts.
// Then put decl back to avoid
// extraneous {}.
// Note: to maintain pointers, ss
// must not change
{
Pstmt tmp = new stmt(SM, curloc, 0);
*tmp = *ss; // tmp = original for
tmp->for_init = 0;
fi->s_list = tmp;
*ss = *fi;
curr_loop = old_loop;
ss->dcl(1);
tmp = ss->s_list;
if (ss->base == DCL && tmp->base == FOR // sanity check
&& tmp->for_init == 0 // sanity check
) {
// put DCL back in for init
*fi = *ss;
*ss = *tmp;
ss->for_init = fi;
fi->s_list = 0;
} else {
// non-decl stmt in for init
// put stmts in block in case
// they follow a condition...
// allocate tmp to be sure
// fields are initialized
*fi = *ss;
tmp = new block(ss->where, 0, fi);
tmp->own_tbl = 0;
tmp->memtbl = curr_block->memtbl;
tmp->permanent = ss->permanent;
*ss = *tmp;
tmp->permanent = 0;
delete tmp;
}
// don't repeat stmt::dcl() for
// remaining stmts
goto done;
}
}
}
if (ss->e == dummy)
ss->e = 0;
else {
ss->e = ss->e->typ(tbl);
// handle temporary of class object with dtor
if (Ntmp) {
Ntmp_dtor = new expr(ELIST, ss->e, Ntmp_dtor);
// error('d',"dcl2: stmt::simpl (e %d) temp %n",ss->e,Ntmp);
}
ss->e = check_cond(ss->e, FOR, tbl);
}
{
Pstmt st = ss->s;
while (st && st->base == FOR)
st = st->for_init;
if (st && (st->base == DCL || st->base == FDCL))
if (st == ss->s)
error(&ss->s->where, "D as onlyS in for-loop");
}
if (ss->s->base != FDCL)
ss->s->dcl();
ss->e2 = (ss->e2 == dummy) ? 0 : ss->e2->typ(tbl);
curr_loop = old_loop;
break;
case DCL: /* declaration after statement */
{
// collect all the contiguous DCL nodes from the
// head of the s_list. find the next statement
int non_trivial = 0;
int count = 0;
Pname tail = ss->d;
for (Pname nn = tail; nn; nn = nn->n_list) {
// find tail;
// detect non-trivial declarations
count++;
if (nn->n_list)
tail = nn->n_list;
Pname n = tbl->look(nn->string, 0);
if (n && n->n_table == tbl)
non_trivial = 2;
if (non_trivial >= 2)
continue;
if (nn->n_sto == STATIC && nn->tp->base != FCT || nn->tp->is_ref()
|| nn->tp->tconst() && fct_const == 0) {
non_trivial = 2;
continue;
}
if (nn->base == NAME) {
Pexpr in = nn->n_initializer;
if (in)
switch (in->base) {
case ILIST:
case STRING:
non_trivial = 2;
continue;
}
if (in)
non_trivial = 3;
else
non_trivial = 1;
}
Pname cln = nn->tp->is_cl_obj();
if (cln == 0)
cln = cl_obj_vec;
if (cln == 0)
continue;
if (Pclass(cln->tp)->has_ctor()) {
non_trivial = 3;