-
Notifications
You must be signed in to change notification settings - Fork 11
/
expression.c
874 lines (809 loc) · 20 KB
/
expression.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
#include <stddef.h>
#include "compiler.h"
static void unexarg(void)
{
error("unexpected argument");
}
static void missedarg(unsigned narg, unsigned ti)
{
/* Make sure no arguments is acceptable */
if (!(narg == 0 || ti == ELLIPSIS || ti == VOID))
error("missing argument");
}
/*
* At the moment this is used for functions, but it will be used for
* casting, hence all the sanity checks.
*/
struct node *typeconv(register struct node *n, register unsigned type, unsigned warn)
{
register unsigned nt = type_canonical(n->type);
/* Weirdness with functions. Properly you should write
funcptr = &func,
but compilers allow funcptr = func even though this is
by strict interpretation nonsense */
if (PTR(type) == 1 && IS_FUNCTION(n->type)) {
/* A function type can only be a name, you can't do maths
on them or dereference them */
n->type++;
}
type = target_type_remap(type);
/* Handle the various cases where we are working with complex types
and they already match */
if (n->type == type) {
return n;
}
if (!PTR(nt)) {
/* Casting an arithmetic type to pointer */
if (PTR(type) && IS_INTARITH(nt)) {
if (warn && !is_constant_zero(n))
typemismatch();
return make_cast(n, type);
}
/* You can cast pointers to things but not actual block
classes */
if (!IS_SIMPLE(nt) || !IS_ARITH(nt) ||
!IS_SIMPLE(type) || !IS_ARITH(type)) {
error("invalid type conversion");
return n;
}
} else {
if (type_pointerconv(n, type, warn))
return make_cast(n, type);
}
if (nt == type || (IS_ARITH(nt) && IS_ARITH(type)))
return make_cast(n, type);
if ((IS_ARITH(nt) && PTR(type)) || (IS_ARITH(type) && PTR(nt))) {
if (!warn)
return make_cast(n, type);
}
typemismatch();
n->type = nt;
return n;
}
/*
* Perform the implicit legacy type conversions C specifies for
* unprototyped arguments
*/
struct node *typeconv_implicit(register struct node *n)
{
register unsigned t = n->type;
if (t == CCHAR || t == UCHAR)
return typeconv(n, CINT, 0);
if (t == FLOAT)
return typeconv(n, DOUBLE, 0);
n->type = type_canonical(t);
return n;
}
/*
* Build an argument tree for right to left stacking
*
* Both here and in the space allocation we need to
* do type / size fixes for argument spacing. For example on an 8080
* we always push 2 bytes so char as arg takes 2 and we need to do
* the right thing.
*/
struct node *call_args(unsigned *narg, register unsigned *argt, unsigned *argsize, unsigned *va)
{
register struct node *n = expression_tree(0);
unsigned t;
/* See what argument type handling is needed */
if (*argt == VOID)
unexarg();
/* Implicit */
else if (*argt == ELLIPSIS) {
n = typeconv_implicit(n);
*va = 1;
} else {
/* Explicit prototyped argument */
if (*narg) {
n = typeconv(n, type_canonical(*argt++), 1);
(*narg)--;
/* Once we hit ellipsis we can accept any number
of arguments including none */
if (*argt == ELLIPSIS) {
*va = 1;
*narg = 0;
}
} else
unexarg();
}
*argsize += target_argsize(n->type);
t = n->type;
if (match(T_COMMA)) {
/* Switch around for calling order */
n = typed_tree(T_ARGCOMMA, t, call_args(narg, argt, argsize, va), n);
return n;
}
require(T_RPAREN);
return n;
}
/*
* Generate a function call tree - no type checking arg counts etc
* yet. Take any arguments for a function we've not seen a prototype for.
*/
static unsigned dummy_argp = ELLIPSIS;
struct node *function_call(register struct node *n)
{
unsigned type;
unsigned *argt, *argp;
unsigned argsize = 0;
unsigned narg;
unsigned va = 0;
/* Must be a function or pointer to function */
if (!IS_FUNCTION(n->type)) {
error("not a function");
return n;
}
type = func_return(n->type);
argt = func_args(n->type);
if (!argt)
fatal("narg");
narg = *argt;
if (narg == 0)
argp = &dummy_argp;
else
argp = argt + 1;
/* A function without arguments */
if (match(T_RPAREN)) {
/* Make sure no arguments is acceptable */
n = sf_tree(T_FUNCCALL, NULL, n);
missedarg(narg, argp[0]);
} else {
n = sf_tree(T_FUNCCALL, call_args(&narg, argp, &argsize, &va), n);
missedarg(narg, argp[0]);
}
/* Always emit this - some targets have other uses for knowing
the boundary of a function call return */
n->type = type;
n = tree(T_CLEANUP, n, make_constant(argsize, UINT));
n->val2 = va;
return n;
}
static struct node *badsizeof(void)
{
error("bad sizeof");
return make_constant(1, UINT);
}
/*
* sizeof() is a strange C thing that is sort of
* a function call but magic.
*/
struct node *get_sizeof(void)
{
unsigned name;
unsigned type;
struct node *n, *r;
unsigned want_paren = 0;
if (match(T_LPAREN))
want_paren = 1;
/* We will eventually need to count typedefs as type_word */
if (is_type_word() || is_typedef()) {
type = type_name_parse(S_NONE, get_type(), &name);
if (type == UNKNOWN || name)
return badsizeof();
require(T_RPAREN);
return make_constant(type_sizeof(type), UINT);
}
/* Sizeof an expression. This is one case that does not degrade to a pointer
if the result is an array. We track whether we are in sizeof so that
we can optimize some of the symbol table tracking for constanrt strings
to keep memory usage a bit more controlled. See primary.c */
in_sizeof++;
n = hier0(0);
r = make_constant(type_sizeof(n->type), UINT);
free_tree(n);
if (want_paren)
require(T_RPAREN);
in_sizeof--;
return r;
}
/*
* Postfixed array and structure dereferences (basically the same but
* one is named and re-typed), and function calls.
*
* left to right
*/
static struct node *hier11(void)
{
register struct node *l, *r;
register unsigned lt;
int direct;
unsigned ptr;
unsigned scale;
unsigned *tag;
l = primary();
lt = l->type;
if (token == T_LSQUARE || token == T_LPAREN || token == T_DOT
|| token == T_POINTSTO) {
for (;;) {
lt = l->type;
ptr = PTR(type_canonical(lt));
if (match(T_LSQUARE)) {
if (ptr == 0) {
error("can't subscript");
junk();
return l;
}
/* TODO: assumes ptrdiff is an integer sized type */
r = typeconv(expression_tree(1), UINT, 0);
require(T_RSQUARE);
scale = type_ptrscale(lt);
l = tree(T_PLUS, make_rval(l),
tree(T_STAR, r,
make_constant(scale, UINT)));
l->flags |= LVAL;
/* Force the type back correct */
lt = type_deref(lt);
l->type = lt;
} else if (match(T_LPAREN)) {
l = function_call(make_rval(l));
} else if ((direct = match(T_DOT))
|| match(T_POINTSTO)) {
if (direct == 0) {
/* The pointer we have holds the address of the
struct which is thus an lval */
l = make_rval(l);
l->flags |= LVAL;
lt = type_deref(lt);
}
if (PTR(lt)
|| !IS_STRUCT(lt)) {
error("can't take member");
junk();
l->type = CINT;
return l;
}
tag = struct_find_member(lt, symname());
if (tag == NULL) {
error("unknown member");
/* So we don't internal error later */
l->type = CINT;
return l;
}
l = target_struct_ref(l, tag[1], tag[2]);
l->flags |= LVAL;
} else
return l;
}
}
return l;
}
/*
* Unary operators
*
* type_scale() typechecks the increment/decrement operators
*
* These all associate right to left
*
* FIXME: sizeof() belongs here not primary
*/
static struct node *hier10(void)
{
register struct node *l, *r;
unsigned op;
unsigned name;
unsigned t;
unsigned is_tcast = 0;
unsigned s;
/* C syntax fun. The grammar has two cases here for (, the first
is a primary (a bracketed expression) the second is a typecast
which has a *different* priority */
op = token;
if (op == T_LPAREN) {
next_token();
if (is_modifier() || is_type_word() || is_typedef())
is_tcast = 1;
push_token(T_LPAREN);
}
if (token != T_PLUSPLUS
&& token != T_SIZEOF
&& token != T_MINUSMINUS
&& token != T_MINUS
&& token != T_TILDE
&& is_tcast == 0
&& token != T_BANG && token != T_STAR && token != T_AND) {
/* Check for trailing forms */
l = hier11();
if (token == T_PLUSPLUS || token == T_MINUSMINUS) {
if (!(l->flags & LVAL)) {
needlval();
return l;
}
op = token;
/* It's an lval so we want the pointer form */
s = type_scale(l->type);
next_token();
/* Put the constant on the right for convenience */
/* We can know the constant will fit a UINT for 16bit boxes
but 32bit ptr 16bit int this is borked FIXME */
if (PTR(l->type))
r = sf_tree(op, l, make_constant(s, UINT));
else
r = sf_tree(op, l, make_constant(s, l->type));
return r;
}
return l;
}
next_token();
switch (op) {
case T_PLUSPLUS:
case T_MINUSMINUS:
r = hier10();
if (!(r->flags & LVAL)) {
needlval();
return r;
}
if (op == T_PLUSPLUS)
op = T_PLUSEQ;
else
op = T_MINUSEQ;
/* FIXME: turning it into a PLUSEQ/MINUSEQ implies the right side
type needs to be ptr size not UINT ?? */
if (PTR(r->type))
return sf_tree(op, r, make_constant(type_scale(r->type), UINT));
/* We should probably keep an optimized ++/-- FIXME */
return sf_tree(op, r, make_constant(1, r->type));
case T_TILDE:
/* Floating point bit ops are not allowed */
r = make_rval(hier10());
if (!IS_INTARITH(r->type))
badtype();
return tree(op, NULL, r);
case T_MINUS:
/* Floating point allowed */
r = make_rval(hier10());
if (!IS_ARITH(r->type) && !PTR(r->type))
badtype();
return tree(T_NEGATE, NULL, r);
case T_BANG:
/* Floating point allowed */
r = make_rval(hier10());
if (!IS_ARITH(r->type) && !PTR(r->type))
badtype();
return bool_tree(tree(op, NULL, r), 0);
case T_STAR:
r = make_rval(hier10());
if (!PTR(r->type))
badtype();
r->flags |= LVAL;
r->type = type_deref(r->type);
return r;
case T_AND:
r = hier10();
if (r->op == T_REG)
error("can't take address of register");
/* If it's an lvalue then just stop being an lvalue */
if (r->flags & LVAL) {
r->flags &= ~LVAL;
/* We are now a pointer to */
r->type = type_ptr(r->type);
return r;
}
r = typed_tree(T_ADDROF, type_addrof(r->type), NULL, r);
return r;
case T_LPAREN:
/* Should be a type without a name */
t = type_name_parse(S_NONE, get_type(), &name);
require(T_RPAREN);
if (t == UNKNOWN || name)
badtype();
return typeconv(make_rval(hier10()), t, 0);
case T_SIZEOF:
return get_sizeof();
}
fatal("h10");
}
/*
* Multiplication, division and remainder
* The '%' operator does not apply to floating point.
*
* As usual left associative
*/
static struct node *hier9(void)
{
register struct node *l;
struct node *r;
unsigned op;
l = hier10();
while (token == T_STAR || token == T_PERCENT || token == T_SLASH) {
op = token;
next_token();
l = make_rval(l);
r = make_rval(hier10());
if (op == T_PERCENT)
l = intarith_tree(op, l, r);
else
l = arith_tree(op, l, r);
}
return l;
}
/*
* Addition and subtraction. Messy because of the pointer scaling
* rules and even more so because of arrays.
*
* As usual left associative
*/
static struct node *hier8(void)
{
register struct node *l, *r;
unsigned op;
int scale = 1;
unsigned rt;
l = hier9();
while (token == T_PLUS || token == T_MINUS) {
op = token;
next_token();
l = make_rval(l);
r = make_rval(hier9());
/* Deal with the non pointer case firt */
if (IS_ARITH(l->type) && IS_ARITH(r->type))
l = arith_tree(op, l, r);
else {
scale = type_ptrscale_binop(op, l, r, &rt);
/* The type checking was done in type_ptrscale_binop */
if (scale < 0)
l = typed_tree(T_SLASH, rt, typed_tree(op, rt, l, r), make_constant(-scale, UINT));
/* TODO: these two assume ptrdiff is an int sized type */
else if (PTR(l->type)) {
r = typeconv(r, UINT, 0);
if (scale)
l = typed_tree(op, rt, l, tree(T_STAR, r, make_constant(scale, UINT)));
else
l = typed_tree(op, rt, l, r);
} else {
l = typeconv(l, UINT, 0);
if (scale)
l = typed_tree(op, rt, tree(T_STAR, l, make_constant(scale, UINT)), r);
else
l = typed_tree(op, rt, l, r);
}
}
}
return l;
}
/*
* Shifts
*/
static struct node *hier7(void)
{
struct node *l;
unsigned op;
l = hier8();
while(token == T_GTGT || token == T_LTLT) {
op = token;
next_token();
/* The tree code knows about the shift rule being different for types */
l = intarith_tree(op, make_rval(l), make_rval(hier8()));
}
return l;
}
/*
* Relational comparison operators
*
* Left to right
*/
static struct node *hier6(void)
{
struct node *l;
unsigned op;
l = hier7();
while(token == T_LT || token == T_GT
|| token == T_LTEQ || token == T_GTEQ) {
op = token;
next_token();
l = ordercomp_tree(op, make_rval(l), make_rval(hier7()));
}
return l;
}
/*
* Equality and not equal operators
*
* Left to right
*/
static struct node *hier5(void)
{
struct node *l;
unsigned op;
l = hier6();
while (token == T_EQEQ || token == T_BANGEQ) {
op = token;
next_token();
l = ordercomp_tree(op, make_rval(l), make_rval(hier6()));
}
return l;
}
/*
* Bitwise and
*/
static struct node *hier4(void)
{
struct node *l;
l = hier5();
while(match(T_AND))
l = intarith_tree(T_AND, make_rval(l), make_rval(hier5()));
return l;
}
/*
* Bitwise xor
*/
static struct node *hier3(void)
{
struct node *l;
l = hier4();
while(match(T_HAT))
l = intarith_tree(T_HAT, make_rval(l), make_rval(hier4()));
return l;
}
/*
* Bitwise or
*
* Bitwise operators also associate left to right
*/
static struct node *hier2(void)
{
struct node *l;
l = hier3();
while(match(T_OR))
l = intarith_tree(T_OR, make_rval(l), make_rval(hier3()));
return l;
}
/*
* logical and
*
* Evaulates left to right, may shortcut evaulation
*/
static struct node *hier1c(void)
{
struct node *l;
l = hier2();
while(match(T_ANDAND))
l = logic_tree(T_ANDAND, make_rval(l), make_rval(hier2()));
return l;
}
/*
* logical or
*
* Evaulates left to right, may shortcut evaulation
*/
static struct node *hier1b(void)
{
struct node *l;
l = hier1c();
while(match(T_OROR))
l = logic_tree(T_OROR, make_rval(l), make_rval(hier1c()));
return l;
}
/*
* The ?: operator. We turn this into trees, the backend turns it into
* bramches/
*
* Type rules are bool for ? and both sides matching for :
*
* : is very unrestricted, you can do things like
* (a?b:c).x or (a?b:c)(foo);
*
* ?: associates right to left.
*/
static struct node *hier1a(void)
{
struct node *l;
struct node *a1, *a2;
unsigned lt;
unsigned a1t, a2t;
l = hier1b();
if (!match(T_QUESTION))
return l;
l = make_rval(l);
lt = l->type;
/* Must be convertible to a boolean != 0 test */
/* TODO: is float ? valid */
if (!PTR(lt) && !IS_ARITH(lt))
badtype();
/* Now do the left of the colon */
a1 = make_rval(hier1a());
if (!match(T_COLON)) {
error("missing colon");
return l;
}
/* We can have a ? a ? b : c : d ? e : f .. */
a2 = make_rval(hier1a());
a1t = type_canonical(a1->type);
a2t = type_canonical(a2->type);
/* Check the two sides of colon are compatible */
if (a1t == a2t || type_pointermatch(a1, a2) || (IS_ARITH(a1t) && IS_ARITH(a2t))) {
/* Takes the type of the : arguments not the ? */
a2 = typed_tree(T_QUESTION, a1t, bool_tree(l, NEEDCC), tree(T_COLON, a1, typeconv(a2, a1t, 1)));
}
else
badtype();
return a2;
}
/*
* Assignment between an lval on the left and an rval on the right
*
* Handle pointer scaling on += and -= by emitting the maths into the
* tree.
*
* Assignment associates right to left
*/
static struct node *hier1(void)
{
register struct node *l, *r;
unsigned fc;
unsigned scale = 1;
l = hier1a();
if (match(T_EQ)) {
if ((l->flags & LVAL) == 0)
needlval();
r = make_rval(hier1());
/* You can't assign to an array/offset, you assign to
the underlying type */
l->type = type_canonical(l->type);
if (!IS_SIMPLE(l->type) && !PTR(l->type)) {
badtype();
return l;
}
return assign_tree(l, r); /* Assignment */
} else {
fc = token;
if (match(T_MINUSEQ) ||
match(T_PLUSEQ) ||
match(T_STAREQ) ||
match(T_SLASHEQ) ||
match(T_PERCENTEQ) ||
match(T_SHREQ) ||
match(T_SHLEQ) ||
match(T_ANDEQ) || match(T_HATEQ) || match(T_OREQ)) {
if ((l->flags & LVAL) == 0) {
needlval();
return l;
}
/* TODO: review - fix things like float ^= and fold
these rules and the non eq versions together somehow */
r = make_rval(hier1());
switch (fc) {
case T_MINUSEQ:
case T_PLUSEQ:
scale = type_scale(l->type);
case T_STAREQ:
case T_SLASHEQ:
if (!IS_ARITH(r->type))
badtype();
break;
default:
if (!IS_INTARITH(r->type))
badtype();
}
/* Get the type converted to the bit width of the maths */
r = make_cast(r, l->type);
if (scale)
return sf_tree(fc, l,
tree(T_STAR, r,
make_constant(scale, UINT)));
return sf_tree(fc, l, r);
} else
return l;
}
/* gcc */
return NULL;
}
/* Comma: left to right which means the final type is the right hand type of the final
expression */
struct node *hier0(unsigned comma)
{
register struct node *l = hier1();
register struct node *r;
while (comma && match(T_COMMA)) {
l->flags |= NORETURN;
r = hier0(comma);
/* The return of a comma operator is never an lval */
l = typed_tree(T_COMMA, r->type, make_rval(l), make_rval(r));
}
return l;
}
/*
* Top level of the expression tree. Make the tree an rval in case
* we need the result. Allow for both the expr,expr,expr format and
* the cases where C doesnt allow it (expr, expr in function calls
* or initializers is not the same
*/
struct node *expression_tree(unsigned comma)
{
return make_rval(hier0(comma));
}
/* Generate an expression and write it the output */
static struct node *expression(unsigned comma, unsigned mkbool, unsigned flags)
{
register struct node *n;
if (token == T_SEMICOLON)
return NULL;
n = expression_tree(comma);
if (mkbool && !(flags & NORETURN)) {
/* Float and double are valid */
if (!IS_ARITH(n->type) && !PTR(n->type))
typemismatch();
/* NORETURN CCONLY etc also apply both to the bool node and the original */
n->flags |= flags;
n = bool_tree(n, NEEDCC | flags);
}
n->flags |= flags;
return n;
}
/* We need a another version of this for initializers that allows global or
static names (and string labels) too */
unsigned const_int_expression(void)
{
unsigned v = 1;
struct node *n = expression_tree(0);
if (n->op == T_CONSTANT)
v = n->value;
else
notconst();
free_tree(n);
return v;
}
/* This is used for bracketed expressions following keywords such as if. These are
normally boolean/condition code except switch */
unsigned bracketed_expression(unsigned mkbool)
{
struct node *n;
unsigned t;
require(T_LPAREN);
n = expression(1, mkbool, mkbool ? CCONLY : 0);
require(T_RPAREN);
/* No expression was present - this can be valid with for() */
if (n == NULL)
return VOID;
t = n->type;
write_tree(n);
return t;
}
/* Bracketed expressions where we also want to do code block optimizing.
As well as the expression we return an indication of whether the
result is constant true, false or unknown.
In order to get the header/expression ordering right this function
does not write the tree immediately. See tree.c:write_logic as well */
struct node *logic_expression(unsigned *truth)
{
register struct node *n, *r;
require(T_LPAREN);
n = expression(1, 1, CCONLY);
if (n == NULL) {
error("expression expected");
/* Return something so we can continue parsing */
return bool_tree(make_constant(0, CINT), 0);
}
r = n->right;
require(T_RPAREN);
/* bool of a constant */
if (n->op == T_BOOL && r->op == T_CONSTANT)
*truth = r->value;
else /* Unknown */
*truth = -1;
return n;
}
void expression_or_null(unsigned mkbool, unsigned flags)
{
register struct node *n;
if (token == T_SEMICOLON || token == T_RPAREN) {
/* A null tree - force the type to void so we can spot it in the backend */
n = typed_tree(T_NULL, VOID, NULL, NULL);
write_tree(n);
} else {
n = expression(1, mkbool, flags);
write_tree(n);
}
}
/* This is used for return */
void expression_typed(unsigned type)
{
register struct node *n;
if (type == VOID && token == T_SEMICOLON) {
write_tree(tree(T_NULL, NULL, NULL));
return;
}
n = typeconv(expression_tree(0), type, 0);
/* Don't lose return statements */
n->flags |= SIDEEFFECT;
write_tree(n);
}