forked from farisawan-2000/cfront-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpr2.c
2793 lines (2566 loc) · 85.3 KB
/
expr2.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/expr2.c 1.29" */
/*******************************************************************************
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.
expr2.c:
type check expressions
************************************************************************/
#include "cfront.h"
#include "size.h"
#include "overload.h"
#include "template.h"
static int const_obj1, const_obj2;
static void opov_error(Ptype, Ptype, TOK);
static Pname compare_builtin(Ptype, Ptype, Pname, Pname, Pname, int);
extern int oper_okay(Ptype, TOK);
extern Pname best_conv(const Block(Pname) &, Pname &, int &, bit);
Pname really_dominate(Pname on1, Pname on2, bit tc) {
Pname best = hier_dominates(on1, on2);
if (best)
return best;
Pfct f1 = on1->get_fct();
Pfct f2 = on2->get_fct();
Ptype t1 = f1->returns->skiptypedefs();
Ptype t2 = f2->returns->skiptypedefs();
if (t1->is_ref())
t1 = Pptr(t1)->typ;
if (t2->is_ref())
t2 = Pptr(t2)->typ;
if (!t1 || !t2 || (t1->check(t2, OVERLOAD) && !const_problem)
|| (t1->is_ref() && t2->is_ref() && Pptr(t1)->typ->check(Pptr(t2)->typ, OVERLOAD)
&& !const_problem)
|| (t1->is_ptr() && t2->is_ptr() && Pptr(t1)->typ->check(Pptr(t2)->typ, OVERLOAD)
&& !const_problem)) {
return 0;
}
// const check
int c1 = f1->f_const;
int c2 = f2->f_const;
if (c1 == c2)
;
else if (c1 && !c2)
return tc ? on1 : on2;
else if (c2 && !c1)
return tc ? on2 : on1;
return 0;
}
void name::assign() {
if (n_assigned_to++ == 0) {
switch (n_scope) {
case FCT:
if (n_used && n_addr_taken == 0) {
Ptype t = tp->skiptypedefs();
switch (t->base) {
case VEC:
break;
default:
if (curr_loop)
error('w', &where, "%n may have been used before set", this);
else
error('w', &where, "%n used before set", this);
}
}
}
}
}
void name::take_addr() {
// error('d', "%n->take_addr tp: %t", this, tp?tp:0 );
// error('d', "%n->take_addr tp: %d %d", this, tp?tp:0, tp?tp->base:0 );
// if ( (warning_opt) && (! n_addr_taken) && (tp) && (tp->base==FCT) && Pfct (tp)->f_inline)
if ((warning_opt) && (!n_addr_taken) && (tp) && (tp->base == FCT) && fct_type()->f_inline)
error('w', "can't take address of inlineF%n,%n not inlined", this, this);
n_addr_taken++;
if (n_sto == EXTERN && tp) {
Ptype t = tp->skiptypedefs();
switch (t->base) {
case COBJ:
t = Pbase(t)->b_name->tp; // no break
case CLASS: {
Pclass cl = Pclass(t);
if (cl->c_body == 1)
cl->dcl_print(0);
}
}
}
}
int ignore_const; // for use by ref_init
static int is_dataMemPtr(Pexpr);
int expr::lval(TOK oper) {
register Pexpr ee = this;
register Pname n;
int deref = 0;
char *es;
// error('d',"%k expr::lval %k",base,oper);
switch (oper) {
case ADDROF:
case G_ADDROF:
es = "address of";
break;
case DEREF:
es = "dereference of";
break;
case INCR:
es = "increment of";
goto def;
case DECR:
es = "decrement of";
goto def;
default:
es = "assignment to";
def:
if (ignore_const == 0 && tp->tconst()) {
if (oper) {
const char *ms = vec_const ? "array" : fct_const ? "function" : "const type";
if (base == NAME) {
if (vec_const && Pname(this)->n_scope == ARG)
break;
error("%s%s%n", es, ms, this);
} else
error("%s%s", es, ms);
}
return 0;
}
}
for (;;) {
// error('d',"loop %k",ee->base);
switch (ee->base) {
// case G_CALL:
// case CALL:
default:
defa:
if (deref == 0) {
if (oper)
error("%s%k (not an lvalue)", es, ee->base);
return 0;
}
return 1;
case ZERO:
case CCON:
case ICON:
case FCON:
if (oper)
error("%s numeric constant", es);
return 0;
case STRING:
if (oper)
error('w', "%s string constant", es);
return 1;
case CAST:
case G_CAST:
switch (oper) {
case 0:
case ADDROF:
case G_ADDROF:
case DEREF:
goto defa;
default:
if (ee->tp->base == PTR && is_dataMemPtr(ee)) { // check for const class object
Pexpr te;
te = ee->e1->e1->e1;
if (te->base == G_ADDROF)
te = te->e2;
if (te->base == NAME) {
Ptype pt = te->tp;
if (pt->base == PTR)
pt = Pptr(pt)->typ;
if (pt->tconst())
error("%sCMP of const%n", es, te);
return 0;
}
}
goto defa;
}
case DEREF: {
Pexpr ee1 = ee->e1;
// error( 'd', "ee1: %k", ee1->base );
switch (ee1->base) { // *& vanishes
case ADDROF: // *&
return 1;
case G_CM:
case CM: // look for *(a,&b)
if (ee1->e2->base == G_ADDROF || ee1->e2->base == ADDROF)
return 1;
goto defaa;
case QUEST: // look for *(q?&a:&b)
if ((ee1->e1->base == G_ADDROF || ee1->e1->base == ADDROF)
&& (ee1->e2->base == G_ADDROF || ee1->e2->base == ADDROF))
return 1;
// no break
default:
defaa:
ee = ee1;
deref = 1;
}
break;
}
case QUEST: {
int x1 = ee->e1->lval(deref ? 0 : oper);
int x2 = ee->e2->lval(deref ? 0 : oper);
if (ee->e1->tp->check(ee->e2->tp, 0))
return 0;
if (deref)
return 1;
return x1 && x2;
}
case INCR:
case DECR:
if (e1)
goto defa; // postfix does not preserve lval
case ASSIGN:
case ASPLUS:
case ASMINUS:
case ASMUL:
case ASDIV:
case ASMOD:
case ASAND:
case ASOR:
case ASER:
case ASLS:
case ASRS:
return 1;
case CM:
case G_CM:
if (ee->e2->lval(deref ? 0 : oper) == 0)
return deref;
return 1;
case MEMPTR:
ee = ee->e2;
break;
case MDOT:
ee = ee->mem;
break;
case DOT: {
// error('d',"dot %k oper %k",ee->e1->base,oper);
Pexpr e = 0;
int e_const = 0; // to catch x.y.val = 1, where x is const
switch (ee->e1->base) { // update use counts, etc.
case NAME:
switch (oper) {
case ADDROF:
case G_ADDROF:
Pname(ee->e1)->take_addr();
case 0:
break;
case ASSIGN:
Pname(ee->e1)->n_used--;
default:
Pname(ee->e1)->assign(); // asop
}
break;
case DOT:
e = ee->e1;
do
e = e->e1;
while (e->base == DOT);
if (e->base == NAME) {
e_const = e->tp->tconst();
switch (oper) {
case ADDROF:
case G_ADDROF:
Pname(e)->take_addr();
case 0:
break;
case ASSIGN:
Pname(e)->n_used--;
default:
Pname(e)->assign(); // asop
}
}
}
n = Pname(ee->mem);
while (n->base == MDOT)
n = Pname(Pref(n)->mem);
if (deref == 0 && (ee->e1->tp->tconst() || e_const)) {
switch (oper) {
case 0:
case ADDROF:
case G_ADDROF:
case DEREF:
break;
default:
error("%sM%n of%t", es, n, e_const ? e->tp : ee->e1->tp);
}
return 0;
}
}
goto xx;
case REF:
n = Pname(ee->mem);
while (n->base == MDOT)
n = Pname(Pref(n)->mem);
if (deref == 0 && ee->e1) { // BR
Ptype p = ee->e1->tp->skiptypedefs();
switch (p->base) {
case PTR:
case VEC:
break;
case ANY:
return 0;
default:
error('i', "expr::lval%t ->%n", p, n);
}
if (ignore_const == 0 && Pptr(p)->typ->tconst()) {
switch (oper) {
case 0:
case ADDROF:
case G_ADDROF:
case DEREF:
break;
default:
error("%sM%n of%t", es, n, Pptr(p)->typ);
}
return 0;
}
}
goto xx;
case NAME:
n = Pname(ee);
xx:
// error('d',"name xx: %n oper %d lex_level: %d",n,oper,n->lex_level);
if (deref)
return 1;
if (oper == 0)
return n->n_stclass != ENUM;
if (n->tp->base == FIELD && Pbase(n->tp)->b_bits == 0) {
error("%s 0-length field%n", es, n);
return 0;
}
switch (oper) {
case ADDROF:
case G_ADDROF: {
if (n->n_sto == REGISTER) {
if (warning_opt)
error('w', "& register%n", n);
// return 0;
n->n_sto = 0;
n->n_stclass = AUTO;
}
if (n->tp == 0) {
error("& label%n", n);
return 0;
}
if (n->n_stclass == ENUM) {
error("& enumerator%n", n);
return 0;
}
if (n->tp->base == FIELD) {
error("& field%n", n);
return 0;
}
n->n_used--;
if (n->n_qualifier) { // oops, not the real one
Pname tn = Pclass(n->n_table->t_name->tp)->memtbl->look(n->string, 0);
n = tn ? tn : n;
}
n->take_addr();
// suppress hoisting of local consts
int statmem = n->n_scope == 0 || n->n_scope == PUBLIC || n->n_scope == FCT;
if (n->n_evaluated && n->n_scope != ARG) { // &const
if (statmem == 0 && n->n_dcl_printed == 0) {
n->n_initializer = new ival(n->n_val);
n->dcl_print(0);
}
} else if (n->tp->base == FCT && n->n_dcl_printed == 0) {
Pfct f = Pfct(n->tp);
if (f->fct_base == INSTANTIATED)
current_fct_instantiation = f;
if (!f->fct_base || f->fct_base == INSTANTIATED)
n->dcl_print(0);
if (f->fct_base == INSTANTIATED)
current_fct_instantiation = 0;
}
break;
}
case ASSIGN:
// error('d',"ass %n %d",n,n->n_used);
n->n_used--;
n->assign();
break;
case INCR:
case DECR:
if (n->tp->skiptypedefs()->base == EOBJ) {
error("%s enum", es);
return 0;
}
// no break
default: /* incr ops, and asops */
if (cc->tot && n == cc->c_this) {
error("%n%k", n, oper);
return 0;
}
n->assign();
}
return 1;
}
}
}
int char_to_int(char *s)
/* assume s points to a string:
'c'
or '\c'
or '\0'
or '\ddd'
or multi-character versions of the above
(hex constants have been converted to octal by the parser)
*/
{
register int i = 0;
register char c, d, e;
switch (*s) {
default:
error('i', "char constant store corrupted");
case '`':
error("bcd constant");
return 0;
case '\'':
break;
}
for (;;) /* also handle multi-character constants */
switch (c = *++s) {
case '\'':
return i;
case '\\': /* special character */
switch (c = *++s) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7': /* octal representation */
c -= '0';
switch (d = *++s) { /* try for 2 */
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
d -= '0';
switch (e = *++s) { /* try for 3 */
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
c = c * 64 + d * 8 + e - '0';
break;
default:
c = c * 8 + d;
s--;
}
break;
default:
s--;
}
break;
case 'a':
c = '\a';
break;
case 'b':
c = '\b';
break;
case 'f':
c = '\f';
break;
case 'n':
c = '\n';
break;
case 'r':
c = '\r';
break;
case 't':
c = '\t';
break;
case 'v':
c = '\v';
break;
case '\\':
c = '\\';
break;
case '\'':
c = '\'';
break;
}
/* no break */
default:
if (i)
i <<= BI_IN_BYTE;
i += c;
}
}
const int A10 = 'A' - 10;
const int a10 = 'a' - 10;
long str_to_long(register const char *p) {
register int c, j;
register int dotflag = 0;
register int dotcount = 0;
register long exp = 0;
register unsigned long i = 0;
const char *pp = p;
// error( 'd', "str_to_long: %s", p );
if ((c = *p++) == '0') {
switch (c = *p++) {
case 0:
return 0;
case 'l':
case 'L': /* long zero */
return 0;
case 'x':
case 'X': /* hexadecimal */
while (c = *p++) {
switch (c) {
case 'l':
case 'L':
case 'U':
case 'u':
return i;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
i = i * 16 + c - A10;
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
i = i * 16 + c - a10;
break;
default:
i = i * 16 + c - '0';
}
}
return i;
default: /* octal */
do
switch (c) {
case 'l':
case 'L':
case 'U':
case 'u':
return i;
default:
i = i * 8 + c - '0';
}
while (c = *p++);
return i;
}
}
/* decimal */
i = c - '0';
while (c = *p++)
switch (c) {
case 'l':
case 'L':
case 'U':
case 'u':
return i;
case '.':
dotflag = 1;
break;
case 'e':
case 'E': /* scientific notation */
{
int negative = 1;
if (*p == '+' || *p == '-')
negative = (*p++ == '-' ? -1 : 1);
exp = str_to_long(p) * negative;
exp = exp - dotcount;
if (exp >= 1) {
unsigned long ii = i;
for (j = 0; exp > j; j++, ii = i) {
i = i * 10;
if (i < ii) {
error('w', "integral conversion of %s is out of range", pp);
return i;
}
}
} else
for (j = 0; exp < j; j--)
i = i * (0.1);
return i;
}
default: {
unsigned long ii = i;
i = i * 10 + c - '0';
if (dotflag)
dotcount++;
if (i < ii)
goto bad;
}
}
return i;
bad:
error("integer constant %s larger than the largest long", pp);
return i;
}
static void ftp_normalize(char *&str)
/*
strip off leading '0' in a scientific floating point string
so that str_to_long() can process it correctly.
*/
{
char *p = str;
char *pp = str;
char c;
register int adjust = 0;
register int dotflag = 0;
register int dotcnt = 0;
while (c = *p) {
switch (c) {
case '+':
case '-':
pp++;
break;
case '0':
if (dotflag)
dotcnt++;
break;
case '.':
dotflag = 1;
break;
default: { // non-zero digits
if (p == pp)
return;
*pp++ = *p++;
if (dotflag == 0) {
while (*p)
*pp++ = *p++;
*pp = 0;
return;
} else {
*pp++ = '.';
while (*p != 'E' && *p != 'e')
if (*p == '.')
p++;
else
*pp++ = *p++;
}
adjust = 1;
}
case 'E':
case 'e': {
if (adjust == 0) { // 0 significant value
*pp++ = '0';
*pp = 0;
return;
}
// have to adjust exponent
*pp++ = *p++;
int sign = 1;
if (*p == '+' || *p == '-')
sign = (*p++ == '-' ? -1 : 1);
long i = sign * str_to_long(p) - dotcnt - 1;
char tmp[40]; // enough for 128-bit doubles
sprintf(tmp, "%-d", i);
if ((strlen(p) + (p - pp)) < strlen(tmp)) {
char *newstr = new char[strlen(tmp) + (pp - str) + 1];
strcpy(newstr, str);
int offset = (pp - str);
str = newstr;
pp = str + offset;
}
sprintf(pp, "%-d", i);
return;
}
}
p++;
}
}
bit type::is_unsigned() {
Ptype t = skiptypedefs();
if (t->base == PTR)
return 0;
return Pbase(t)->b_unsigned;
}
char *Neval;
bit binary_val;
unsigned long expr::ueval(long x1, long x2) {
unsigned long i1 = (unsigned long) x1;
unsigned long i2 = (unsigned long) x2;
// error('d',"ueval %k %ld %ld",base,x1,x2);
switch (base) {
case UMINUS:
return -i2;
case UPLUS:
return i2;
case NOT:
return !i2;
case COMPL:
return ~i2;
case CAST:
case G_CAST:
return i1;
case PLUS:
return i1 + i2;
case MINUS:
return i1 - i2;
case MUL:
return i1 * i2;
case LS:
return i1 << i2;
case RS:
return i1 >> i2;
case NE:
return i1 != i2;
case EQ:
return i1 == i2;
case LT:
return i1 < i2;
case LE:
return i1 <= i2;
case GT:
return i1 > i2;
case GE:
return i1 >= i2;
case AND:
return i1 & i2;
case ANDAND:
return i1 && i2;
case OR:
return i1 | i2;
case OROR:
return i1 || i2;
case ER:
return i1 ^ i2;
case MOD:
if (i2 == 0) {
if (Neval == 0) {
Neval = "mod zero";
error("mod zero");
}
return 1;
}
return i1 % i2;
case QUEST:
return (cond->eval()) ? i1 : i2;
case DIV:
if (i2 == 0) {
if (Neval == 0) {
Neval = "divide by zero";
error("divide by zero");
}
return 1;
}
return i1 / i2;
case CM:
case G_CM:
return i2;
}
Neval = "unsigned expression";
return 0;
}
long expr::eval() {
if (Neval)
return 1;
// error('d',"eval %k",base);
Ptype tt;
switch (base) {
case ZERO:
return 0;
case IVAL:
return i1;
case ICON:
return str_to_long(string);
case CCON:
return char_to_int(string);
case FCON:
Neval = "float in constant expression";
return 1;
case STRING:
Neval = "string in constant expression";
return 1;
case EOBJ:
return Pname(this)->n_val;
case SIZEOF: {
extern int no_sizeof;
if (no_sizeof)
Neval = "sizeof";
char *cese = "cannot evaluate sizeof expression";
if (!tp2) {
Neval = cese;
return 1;
} else {
tt = tp2;
while (tt && tt->vec_type()) { // ANY,VEC,PTR,RPTR
tt = tt->skiptypedefs();
if (tt->base == ANY) {
Neval = cese;
return 1;
}
tt = (tt->base == VEC) ? Pvec(tt)->typ : Pptr(tt)->typ;
}
}
return tp2->tsizeof();
}
case NAME: {
Pname n = Pname(this);
// error('d',"eval %n eval %d %d",n,n->n_evaluated,n->n_val);
// error('d',"eval tp->tconst() %d, n->n_initializer: %k", n->tp->tconst(),
// n->n_initializer?n->n_initializer->base:0 );
if (n->n_evaluated && n->n_scope != ARG)
return n->n_val;
if (binary_val && strcmp(string, "_result") == 0)
return 8888;
Neval = "cannot evaluate constant";
return 1;
}
case ICALL:
if (e1) {
il->i_next = curr_icall;
curr_icall = il;
long i = e1->eval();
curr_icall = il->i_next;
return i;
}
Neval = "void inlineF";
return 1;
case ANAME: {
Pname n = (Pname) this;
Pin il;
for (il = curr_icall; il; il = il->i_next)
if (il->i_table == n->n_table)
goto aok;
goto bok;
aok:
if (il->i_args[n->argno].local) {
bok:
Neval = "inlineF call too complicated for constant expression";
return 1;
}
Pexpr aa = il->i_args[n->argno].arg;
return aa->eval();
}
case CAST:
case G_CAST: {
if (tp2->base != FLOAT && tp2->base != DOUBLE
&& (e1->base == FCON
|| (e1->base == UMINUS || e1->base == UPLUS) && e1->e2->base == FCON)) {
char *&str = (e1->base == FCON ? e1->string : e1->e2->string);
char *p = str;
while (*p && *p != '.' && *p != 'E' && *p != 'e')
p++;
if (*p) {
if ((strchr(p, 'E') == 0) && (strchr(p, 'e') == 0)) {
if (p == str)
*p++ = '0';
*p = 0;
} else {
ftp_normalize(str);
}
}
if (e1->base == FCON)
e1->base = ICON;
else
e1->e2->base = ICON;
}
long i = e1->eval();
tt = tp2->skiptypedefs();
switch (tt->base) {
default:
Neval = "cast to non-integral type in constant expression";
break;
case ENUM:
case EOBJ:
case LONG:
case INT:
case CHAR:
case SHORT: {
long j =
(((~(unsigned long) 0) << (BI_IN_BYTE * (tp2->tsizeof() - 1))) << BI_IN_BYTE);
i &= ~j;
if (tt->is_unsigned() == 0)
if (long((i << (BI_IN_BYTE * (sizeof(long) - tp2->tsizeof())))) < 0)
i |= j;
break;
}
}
return i;
}
case UMINUS:
case UPLUS:
case NOT:
case COMPL:
case PLUS:
case MINUS:
case MUL:
case LS:
case RS:
case NE:
case LT:
case LE:
case GT:
case GE:
case AND:
case OR:
case ER:
case DIV:
case MOD:
case QUEST:
case EQ:
case ANDAND:
break;
case OROR:
if (binary_val) { // a||b, don't evaluate b if a!=0
long i1 = (e1) ? e1->eval() : 0;
if (Neval == 0 && i1 && e1->tp->is_unsigned() == 0)
return i1;
}
break;
case CM:
case G_CM:
Neval = "comma operator in constant expression";