[PATCH] fix the sanity check in evaluate_ptr_sub()
[smatch.git] / evaluate.c
blob8df2042ad3cf9c3bf4b08f7c9c48572601cb299f
1 /*
2 * sparse/evaluate.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
9 * Evaluate constant expressions.
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <stddef.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <limits.h>
21 #include "lib.h"
22 #include "allocate.h"
23 #include "parse.h"
24 #include "token.h"
25 #include "symbol.h"
26 #include "target.h"
27 #include "expression.h"
29 struct symbol *current_fn;
31 static struct symbol *degenerate(struct expression *expr);
32 static struct symbol *evaluate_symbol(struct symbol *sym);
34 static struct symbol *evaluate_symbol_expression(struct expression *expr)
36 struct expression *addr;
37 struct symbol *sym = expr->symbol;
38 struct symbol *base_type;
40 if (!sym) {
41 expression_error(expr, "undefined identifier '%s'", show_ident(expr->symbol_name));
42 return NULL;
45 examine_symbol_type(sym);
47 base_type = get_base_type(sym);
48 if (!base_type) {
49 expression_error(expr, "identifier '%s' has no type", show_ident(expr->symbol_name));
50 return NULL;
53 addr = alloc_expression(expr->pos, EXPR_SYMBOL);
54 addr->symbol = sym;
55 addr->symbol_name = expr->symbol_name;
56 addr->ctype = &lazy_ptr_ctype; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
57 expr->type = EXPR_PREOP;
58 expr->op = '*';
59 expr->unop = addr;
61 /* The type of a symbol is the symbol itself! */
62 expr->ctype = sym;
63 return sym;
66 static struct symbol *evaluate_string(struct expression *expr)
68 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
69 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
70 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
71 struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
72 unsigned int length = expr->string->length;
74 sym->array_size = alloc_const_expression(expr->pos, length);
75 sym->bit_size = bits_in_char * length;
76 sym->ctype.alignment = 1;
77 sym->string = 1;
78 sym->ctype.modifiers = MOD_STATIC;
79 sym->ctype.base_type = array;
80 sym->initializer = initstr;
82 initstr->ctype = sym;
83 initstr->string = expr->string;
85 array->array_size = sym->array_size;
86 array->bit_size = bits_in_char * length;
87 array->ctype.alignment = 1;
88 array->ctype.modifiers = MOD_STATIC;
89 array->ctype.base_type = &char_ctype;
91 addr->symbol = sym;
92 addr->ctype = &lazy_ptr_ctype;
94 expr->type = EXPR_PREOP;
95 expr->op = '*';
96 expr->unop = addr;
97 expr->ctype = sym;
98 return sym;
101 /* type has come from classify_type and is an integer type */
102 static inline struct symbol *integer_promotion(struct symbol *type)
104 struct symbol *orig_type = type;
105 unsigned long mod = type->ctype.modifiers;
106 int width = type->bit_size;
109 * Bitfields always promote to the base type,
110 * even if the bitfield might be bigger than
111 * an "int".
113 if (type->type == SYM_BITFIELD) {
114 type = type->ctype.base_type;
115 orig_type = type;
117 mod = type->ctype.modifiers;
118 if (width < bits_in_int)
119 return &int_ctype;
121 /* If char/short has as many bits as int, it still gets "promoted" */
122 if (mod & (MOD_CHAR | MOD_SHORT)) {
123 if (mod & MOD_UNSIGNED)
124 return &uint_ctype;
125 return &int_ctype;
127 return orig_type;
131 * integer part of usual arithmetic conversions:
132 * integer promotions are applied
133 * if left and right are identical, we are done
134 * if signedness is the same, convert one with lower rank
135 * unless unsigned argument has rank lower than signed one, convert the
136 * signed one.
137 * if signed argument is bigger than unsigned one, convert the unsigned.
138 * otherwise, convert signed.
140 * Leaving aside the integer promotions, that is equivalent to
141 * if identical, don't convert
142 * if left is bigger than right, convert right
143 * if right is bigger than left, convert right
144 * otherwise, if signedness is the same, convert one with lower rank
145 * otherwise convert the signed one.
147 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
149 unsigned long lmod, rmod;
151 left = integer_promotion(left);
152 right = integer_promotion(right);
154 if (left == right)
155 goto left;
157 if (left->bit_size > right->bit_size)
158 goto left;
160 if (right->bit_size > left->bit_size)
161 goto right;
163 lmod = left->ctype.modifiers;
164 rmod = right->ctype.modifiers;
165 if ((lmod ^ rmod) & MOD_UNSIGNED) {
166 if (lmod & MOD_UNSIGNED)
167 goto left;
168 } else if ((lmod & ~rmod) & (MOD_LONG | MOD_LONGLONG))
169 goto left;
170 right:
171 left = right;
172 left:
173 return left;
176 static int same_cast_type(struct symbol *orig, struct symbol *new)
178 return orig->bit_size == new->bit_size && orig->bit_offset == new->bit_offset;
181 static struct symbol *base_type(struct symbol *node, unsigned long *modp, unsigned long *asp)
183 unsigned long mod, as;
185 mod = 0; as = 0;
186 while (node) {
187 mod |= node->ctype.modifiers;
188 as |= node->ctype.as;
189 if (node->type == SYM_NODE) {
190 node = node->ctype.base_type;
191 continue;
193 break;
195 *modp = mod & ~MOD_IGNORE;
196 *asp = as;
197 return node;
200 static int is_same_type(struct expression *expr, struct symbol *new)
202 struct symbol *old = expr->ctype;
203 unsigned long oldmod, newmod, oldas, newas;
205 old = base_type(old, &oldmod, &oldas);
206 new = base_type(new, &newmod, &newas);
208 /* Same base type, same address space? */
209 if (old == new && oldas == newas) {
210 unsigned long difmod;
212 /* Check the modifier bits. */
213 difmod = (oldmod ^ newmod) & ~MOD_NOCAST;
215 /* Exact same type? */
216 if (!difmod)
217 return 1;
220 * Not the same type, but differs only in "const".
221 * Don't warn about MOD_NOCAST.
223 if (difmod == MOD_CONST)
224 return 0;
226 if ((oldmod | newmod) & MOD_NOCAST) {
227 const char *tofrom = "to/from";
228 if (!(newmod & MOD_NOCAST))
229 tofrom = "from";
230 if (!(oldmod & MOD_NOCAST))
231 tofrom = "to";
232 warning(expr->pos, "implicit cast %s nocast type", tofrom);
234 return 0;
237 static void
238 warn_for_different_enum_types (struct position pos,
239 struct symbol *typea,
240 struct symbol *typeb)
242 if (!Wenum_mismatch)
243 return;
244 if (typea->type == SYM_NODE)
245 typea = typea->ctype.base_type;
246 if (typeb->type == SYM_NODE)
247 typeb = typeb->ctype.base_type;
249 if (typea == typeb)
250 return;
252 if (typea->type == SYM_ENUM && typeb->type == SYM_ENUM) {
253 warning(pos, "mixing different enum types");
254 info(pos, " %s versus", show_typename(typea));
255 info(pos, " %s", show_typename(typeb));
260 * This gets called for implicit casts in assignments and
261 * integer promotion. We often want to try to move the
262 * cast down, because the ops involved may have been
263 * implicitly cast up, and we can get rid of the casts
264 * early.
266 static struct expression * cast_to(struct expression *old, struct symbol *type)
268 struct expression *expr;
270 warn_for_different_enum_types (old->pos, old->ctype, type);
272 if (old->ctype != &null_ctype && is_same_type(old, type))
273 return old;
276 * See if we can simplify the op. Move the cast down.
278 switch (old->type) {
279 case EXPR_PREOP:
280 if (old->ctype->bit_size < type->bit_size)
281 break;
282 if (old->op == '~') {
283 old->ctype = type;
284 old->unop = cast_to(old->unop, type);
285 return old;
287 break;
289 case EXPR_IMPLIED_CAST:
290 warn_for_different_enum_types(old->pos, old->ctype, type);
292 if (old->ctype->bit_size >= type->bit_size) {
293 struct expression *orig = old->cast_expression;
294 if (same_cast_type(orig->ctype, type))
295 return orig;
296 if (old->ctype->bit_offset == type->bit_offset) {
297 old->ctype = type;
298 old->cast_type = type;
299 return old;
302 break;
304 default:
305 /* nothing */;
308 expr = alloc_expression(old->pos, EXPR_IMPLIED_CAST);
309 expr->flags = old->flags;
310 expr->ctype = type;
311 expr->cast_type = type;
312 expr->cast_expression = old;
313 return expr;
316 static int is_type_type(struct symbol *type)
318 return (type->ctype.modifiers & MOD_TYPE) != 0;
321 int is_ptr_type(struct symbol *type)
323 if (type->type == SYM_NODE)
324 type = type->ctype.base_type;
325 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
328 static inline int is_float_type(struct symbol *type)
330 if (type->type == SYM_NODE)
331 type = type->ctype.base_type;
332 return type->ctype.base_type == &fp_type;
335 static inline int is_byte_type(struct symbol *type)
337 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
340 enum {
341 TYPE_NUM = 1,
342 TYPE_BITFIELD = 2,
343 TYPE_RESTRICT = 4,
344 TYPE_FLOAT = 8,
345 TYPE_PTR = 16,
346 TYPE_COMPOUND = 32,
347 TYPE_FOULED = 64,
350 static inline int classify_type(struct symbol *type, struct symbol **base)
352 static int type_class[SYM_BAD + 1] = {
353 [SYM_PTR] = TYPE_PTR,
354 [SYM_FN] = TYPE_PTR,
355 [SYM_ARRAY] = TYPE_PTR | TYPE_COMPOUND,
356 [SYM_STRUCT] = TYPE_COMPOUND,
357 [SYM_UNION] = TYPE_COMPOUND,
358 [SYM_BITFIELD] = TYPE_NUM | TYPE_BITFIELD,
359 [SYM_RESTRICT] = TYPE_NUM | TYPE_RESTRICT,
360 [SYM_FOULED] = TYPE_NUM | TYPE_RESTRICT | TYPE_FOULED,
362 if (type->type == SYM_NODE)
363 type = type->ctype.base_type;
364 if (type->type == SYM_ENUM)
365 type = type->ctype.base_type;
366 *base = type;
367 if (type->type == SYM_BASETYPE) {
368 if (type->ctype.base_type == &int_type)
369 return TYPE_NUM;
370 if (type->ctype.base_type == &fp_type)
371 return TYPE_NUM | TYPE_FLOAT;
373 return type_class[type->type];
376 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
378 static inline int is_string_type(struct symbol *type)
380 if (type->type == SYM_NODE)
381 type = type->ctype.base_type;
382 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
385 static struct symbol *bad_expr_type(struct expression *expr)
387 sparse_error(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
388 switch (expr->type) {
389 case EXPR_BINOP:
390 case EXPR_COMPARE:
391 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
392 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
393 break;
394 case EXPR_PREOP:
395 case EXPR_POSTOP:
396 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
397 break;
398 default:
399 break;
402 expr->flags = 0;
403 return expr->ctype = &bad_ctype;
406 static int restricted_value(struct expression *v, struct symbol *type)
408 if (v->type != EXPR_VALUE)
409 return 1;
410 if (v->value != 0)
411 return 1;
412 return 0;
415 static int restricted_binop(int op, struct symbol *type)
417 switch (op) {
418 case '&':
419 case '=':
420 case SPECIAL_AND_ASSIGN:
421 case SPECIAL_OR_ASSIGN:
422 case SPECIAL_XOR_ASSIGN:
423 return 1; /* unfoul */
424 case '|':
425 case '^':
426 case '?':
427 return 2; /* keep fouled */
428 case SPECIAL_EQUAL:
429 case SPECIAL_NOTEQUAL:
430 return 3; /* warn if fouled */
431 default:
432 return 0; /* warn */
436 static int restricted_unop(int op, struct symbol **type)
438 if (op == '~') {
439 if ((*type)->bit_size < bits_in_int)
440 *type = befoul(*type);
441 return 0;
442 } if (op == '+')
443 return 0;
444 return 1;
447 static struct symbol *restricted_binop_type(int op,
448 struct expression *left,
449 struct expression *right,
450 int lclass, int rclass,
451 struct symbol *ltype,
452 struct symbol *rtype)
454 struct symbol *ctype = NULL;
455 if (lclass & TYPE_RESTRICT) {
456 if (rclass & TYPE_RESTRICT) {
457 if (ltype == rtype) {
458 ctype = ltype;
459 } else if (lclass & TYPE_FOULED) {
460 if (ltype->ctype.base_type == rtype)
461 ctype = ltype;
462 } else if (rclass & TYPE_FOULED) {
463 if (rtype->ctype.base_type == ltype)
464 ctype = rtype;
466 } else {
467 if (!restricted_value(right, ltype))
468 ctype = ltype;
470 } else if (!restricted_value(left, rtype))
471 ctype = rtype;
473 if (ctype) {
474 switch (restricted_binop(op, ctype)) {
475 case 1:
476 if ((lclass ^ rclass) & TYPE_FOULED)
477 ctype = ctype->ctype.base_type;
478 break;
479 case 3:
480 if (!(lclass & rclass & TYPE_FOULED))
481 break;
482 case 0:
483 ctype = NULL;
484 default:
485 break;
489 return ctype;
492 static inline void unrestrict(struct expression *expr,
493 int class, struct symbol **ctype)
495 if (class & TYPE_RESTRICT) {
496 warning(expr->pos, "restricted degrades to integer");
497 if (class & TYPE_FOULED) /* unfoul it first */
498 *ctype = (*ctype)->ctype.base_type;
499 *ctype = (*ctype)->ctype.base_type; /* get to arithmetic type */
503 static struct symbol *usual_conversions(int op,
504 struct expression *left,
505 struct expression *right,
506 int lclass, int rclass,
507 struct symbol *ltype,
508 struct symbol *rtype)
510 struct symbol *ctype;
512 warn_for_different_enum_types(right->pos, left->ctype, right->ctype);
514 if ((lclass | rclass) & TYPE_RESTRICT)
515 goto Restr;
517 Normal:
518 if (!(lclass & TYPE_FLOAT)) {
519 if (!(rclass & TYPE_FLOAT))
520 return bigger_int_type(ltype, rtype);
521 else
522 return rtype;
523 } else if (rclass & TYPE_FLOAT) {
524 unsigned long lmod = ltype->ctype.modifiers;
525 unsigned long rmod = rtype->ctype.modifiers;
526 if (rmod & ~lmod & (MOD_LONG | MOD_LONGLONG))
527 return rtype;
528 else
529 return ltype;
530 } else
531 return ltype;
533 Restr:
534 ctype = restricted_binop_type(op, left, right,
535 lclass, rclass, ltype, rtype);
536 if (ctype)
537 return ctype;
539 unrestrict(left, lclass, &ltype);
540 unrestrict(right, rclass, &rtype);
542 goto Normal;
545 static inline int lvalue_expression(struct expression *expr)
547 return expr->type == EXPR_PREOP && expr->op == '*';
550 static int ptr_object_size(struct symbol *ptr_type)
552 if (ptr_type->type == SYM_NODE)
553 ptr_type = ptr_type->ctype.base_type;
554 if (ptr_type->type == SYM_PTR)
555 ptr_type = get_base_type(ptr_type);
556 return ptr_type->bit_size;
559 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *ctype, struct symbol *itype)
561 struct expression *index = expr->right;
562 int multiply;
563 int bit_size;
565 if (ctype == &null_ctype)
566 ctype = &ptr_ctype;
568 examine_symbol_type(ctype);
570 if (!ctype->ctype.base_type) {
571 expression_error(expr, "missing type information");
572 return NULL;
575 /* Get the size of whatever the pointer points to */
576 bit_size = ptr_object_size(ctype);
577 multiply = bit_size >> 3;
579 expr->ctype = ctype;
581 if (multiply == 1 && itype->bit_size >= bits_in_pointer)
582 return ctype;
584 if (index->type == EXPR_VALUE) {
585 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
586 unsigned long long v = index->value, mask;
587 mask = 1ULL << (itype->bit_size - 1);
588 if (v & mask)
589 v |= -mask;
590 else
591 v &= mask - 1;
592 v *= multiply;
593 mask = 1ULL << (bits_in_pointer - 1);
594 v &= mask | (mask - 1);
595 val->value = v;
596 val->ctype = ssize_t_ctype;
597 expr->right = val;
598 return ctype;
601 if (itype->bit_size < bits_in_pointer)
602 index = cast_to(index, ssize_t_ctype);
604 if (multiply > 1) {
605 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
606 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
608 val->ctype = ssize_t_ctype;
609 val->value = multiply;
611 mul->op = '*';
612 mul->ctype = ssize_t_ctype;
613 mul->left = index;
614 mul->right = val;
615 index = mul;
618 expr->right = index;
619 return ctype;
622 static void examine_fn_arguments(struct symbol *fn);
624 const char * type_difference(struct symbol *target, struct symbol *source,
625 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
627 for (;;) {
628 unsigned long mod1, mod2, diff;
629 unsigned long as1, as2;
630 int type1, type2;
631 struct symbol *base1, *base2;
633 if (target == source)
634 break;
635 if (!target || !source)
636 return "different types";
638 * Peel of per-node information.
639 * FIXME! Check alignment and context too here!
641 mod1 = target->ctype.modifiers;
642 as1 = target->ctype.as;
643 mod2 = source->ctype.modifiers;
644 as2 = source->ctype.as;
645 if (target->type == SYM_NODE) {
646 target = target->ctype.base_type;
647 if (!target)
648 return "bad types";
649 if (target->type == SYM_PTR) {
650 mod1 = 0;
651 as1 = 0;
653 mod1 |= target->ctype.modifiers;
654 as1 |= target->ctype.as;
656 if (source->type == SYM_NODE) {
657 source = source->ctype.base_type;
658 if (!source)
659 return "bad types";
660 if (source->type == SYM_PTR) {
661 mod2 = 0;
662 as2 = 0;
664 mod2 |= source->ctype.modifiers;
665 as2 |= source->ctype.as;
667 if (target->type == SYM_ENUM) {
668 target = target->ctype.base_type;
669 if (!target)
670 return "bad types";
672 if (source->type == SYM_ENUM) {
673 source = source->ctype.base_type;
674 if (!source)
675 return "bad types";
678 if (target == source)
679 break;
680 if (!target || !source)
681 return "different types";
683 type1 = target->type;
684 base1 = target->ctype.base_type;
686 type2 = source->type;
687 base2 = source->ctype.base_type;
690 * Pointers to functions compare as the function itself
692 if (type1 == SYM_PTR && base1)
693 base1 = examine_symbol_type(base1);
695 if (type2 == SYM_PTR && base2)
696 base2 = examine_symbol_type(base2);
698 if (type1 != type2 || type1 == SYM_RESTRICT ||
699 type1 == SYM_UNION || type1 == SYM_STRUCT)
700 return "different base types";
702 /* Must be same address space to be comparable */
703 if (Waddress_space && as1 != as2)
704 return "different address spaces";
706 /* Ignore differences in storage types or addressability */
707 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
708 diff &= (mod1 & ~target_mod_ignore) | (mod2 & ~source_mod_ignore);
709 if (diff) {
710 if (diff & MOD_SIZE)
711 return "different type sizes";
712 if (diff & ~MOD_SIGNEDNESS)
713 return "different modifiers";
715 /* Differs in signedness only.. */
716 if (Wtypesign) {
718 * Warn if both are explicitly signed ("unsigned" is obviously
719 * always explicit, and since we know one of them has to be
720 * unsigned, we check if the signed one was explicit).
722 if ((mod1 | mod2) & MOD_EXPLICITLY_SIGNED)
723 return "different explicit signedness";
726 * "char" matches both "unsigned char" and "signed char",
727 * so if the explicit test didn't trigger, then we should
728 * not warn about a char.
730 if (!(mod1 & MOD_CHAR))
731 return "different signedness";
735 if (type1 == SYM_FN) {
736 int i;
737 struct symbol *arg1, *arg2;
738 if (base1->variadic != base2->variadic)
739 return "incompatible variadic arguments";
740 examine_fn_arguments(target);
741 examine_fn_arguments(source);
742 PREPARE_PTR_LIST(target->arguments, arg1);
743 PREPARE_PTR_LIST(source->arguments, arg2);
744 i = 1;
745 for (;;) {
746 const char *diffstr;
747 diffstr = type_difference(arg1, arg2, 0, 0);
748 if (diffstr) {
749 static char argdiff[80];
750 sprintf(argdiff, "incompatible argument %d (%s)", i, diffstr);
751 return argdiff;
753 if (!arg1)
754 break;
755 NEXT_PTR_LIST(arg1);
756 NEXT_PTR_LIST(arg2);
757 i++;
759 FINISH_PTR_LIST(arg2);
760 FINISH_PTR_LIST(arg1);
763 target = base1;
764 source = base2;
766 return NULL;
769 static void bad_null(struct expression *expr)
771 if (Wnon_pointer_null)
772 warning(expr->pos, "Using plain integer as NULL pointer");
776 * Ignore differences in "volatile" and "const"ness when
777 * subtracting pointers
779 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
781 static struct symbol *evaluate_ptr_sub(struct expression *expr)
783 const char *typediff;
784 struct symbol *ctype;
785 struct symbol *ltype, *rtype;
786 struct expression *l = expr->left;
787 struct expression *r = expr->right;
788 struct symbol *lbase, *rbase;
790 classify_type(degenerate(l), &ltype);
791 classify_type(degenerate(r), &rtype);
793 lbase = get_base_type(ltype);
794 rbase = get_base_type(rtype);
795 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
796 if (typediff)
797 expression_error(expr, "subtraction of different types can't work (%s)", typediff);
799 ctype = lbase;
800 /* Figure out the base type we point to */
801 if (ctype->type == SYM_NODE)
802 ctype = ctype->ctype.base_type;
803 if (ctype->type == SYM_FN) {
804 expression_error(expr, "subtraction of functions? Share your drugs");
805 return NULL;
808 expr->ctype = ssize_t_ctype;
809 if (ctype->bit_size > bits_in_char) {
810 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
811 struct expression *div = expr;
812 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
813 unsigned long value = ctype->bit_size >> 3;
815 val->ctype = size_t_ctype;
816 val->value = value;
818 if (value & (value-1)) {
819 if (Wptr_subtraction_blows)
820 warning(expr->pos, "potentially expensive pointer subtraction");
823 sub->op = '-';
824 sub->ctype = ssize_t_ctype;
825 sub->left = l;
826 sub->right = r;
828 div->op = '/';
829 div->left = sub;
830 div->right = val;
833 return ssize_t_ctype;
836 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
838 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
840 struct symbol *ctype;
842 if (!expr)
843 return NULL;
845 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
846 warning(expr->pos, "assignment expression in conditional");
848 ctype = evaluate_expression(expr);
849 if (ctype) {
850 if (is_safe_type(ctype))
851 warning(expr->pos, "testing a 'safe expression'");
854 return ctype;
857 static struct symbol *evaluate_logical(struct expression *expr)
859 if (!evaluate_conditional(expr->left, 0))
860 return NULL;
861 if (!evaluate_conditional(expr->right, 0))
862 return NULL;
864 expr->ctype = &bool_ctype;
865 if (expr->flags) {
866 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
867 expr->flags = 0;
869 return &bool_ctype;
872 static struct symbol *evaluate_binop(struct expression *expr)
874 struct symbol *ltype, *rtype, *ctype;
875 int lclass = classify_type(expr->left->ctype, &ltype);
876 int rclass = classify_type(expr->right->ctype, &rtype);
877 int op = expr->op;
879 if (expr->flags) {
880 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
881 expr->flags = 0;
884 /* number op number */
885 if (lclass & rclass & TYPE_NUM) {
886 if ((lclass | rclass) & TYPE_FLOAT) {
887 switch (op) {
888 case '+': case '-': case '*': case '/':
889 break;
890 default:
891 return bad_expr_type(expr);
895 if (op == SPECIAL_LEFTSHIFT || op == SPECIAL_RIGHTSHIFT) {
896 // shifts do integer promotions, but that's it.
897 unrestrict(expr->left, lclass, &ltype);
898 unrestrict(expr->right, rclass, &rtype);
899 ctype = ltype = integer_promotion(ltype);
900 rtype = integer_promotion(rtype);
901 } else {
902 // The rest do usual conversions
903 ltype = usual_conversions(op, expr->left, expr->right,
904 lclass, rclass, ltype, rtype);
905 ctype = rtype = ltype;
908 expr->left = cast_to(expr->left, ltype);
909 expr->right = cast_to(expr->right, rtype);
910 expr->ctype = ctype;
911 return ctype;
914 /* pointer (+|-) integer */
915 if (lclass & TYPE_PTR && is_int(rclass) && (op == '+' || op == '-')) {
916 unrestrict(expr->right, rclass, &rtype);
917 return evaluate_ptr_add(expr, degenerate(expr->left), rtype);
920 /* integer + pointer */
921 if (rclass & TYPE_PTR && is_int(lclass) && op == '+') {
922 struct expression *index = expr->left;
923 unrestrict(index, lclass, &ltype);
924 expr->left = expr->right;
925 expr->right = index;
926 return evaluate_ptr_add(expr, degenerate(expr->left), ltype);
929 /* pointer - pointer */
930 if (lclass & rclass & TYPE_PTR && expr->op == '-')
931 return evaluate_ptr_sub(expr);
933 return bad_expr_type(expr);
936 static struct symbol *evaluate_comma(struct expression *expr)
938 expr->ctype = expr->right->ctype;
939 expr->flags &= expr->left->flags & expr->right->flags;
940 return expr->ctype;
943 static int modify_for_unsigned(int op)
945 if (op == '<')
946 op = SPECIAL_UNSIGNED_LT;
947 else if (op == '>')
948 op = SPECIAL_UNSIGNED_GT;
949 else if (op == SPECIAL_LTE)
950 op = SPECIAL_UNSIGNED_LTE;
951 else if (op == SPECIAL_GTE)
952 op = SPECIAL_UNSIGNED_GTE;
953 return op;
956 static inline int is_null_pointer_constant(struct expression *e)
958 if (e->ctype == &null_ctype)
959 return 1;
960 if (!(e->flags & Int_const_expr))
961 return 0;
962 return is_zero_constant(e) ? 2 : 0;
965 static struct symbol *evaluate_compare(struct expression *expr)
967 struct expression *left = expr->left, *right = expr->right;
968 struct symbol *ltype, *rtype;
969 int lclass = classify_type(degenerate(left), &ltype);
970 int rclass = classify_type(degenerate(right), &rtype);
971 struct symbol *ctype;
972 const char *typediff;
974 if (expr->flags) {
975 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
976 expr->flags = 0;
979 /* Type types? */
980 if (is_type_type(ltype) && is_type_type(rtype))
981 goto OK;
983 if (is_safe_type(left->ctype) || is_safe_type(right->ctype))
984 warning(expr->pos, "testing a 'safe expression'");
986 /* number on number */
987 if (lclass & rclass & TYPE_NUM) {
988 ctype = usual_conversions(expr->op, expr->left, expr->right,
989 lclass, rclass, ltype, rtype);
990 expr->left = cast_to(expr->left, ctype);
991 expr->right = cast_to(expr->right, ctype);
992 if (ctype->ctype.modifiers & MOD_UNSIGNED)
993 expr->op = modify_for_unsigned(expr->op);
994 goto OK;
997 /* at least one must be a pointer */
998 if (!((lclass | rclass) & TYPE_PTR))
999 return bad_expr_type(expr);
1001 /* equality comparisons can be with null pointer constants */
1002 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1003 int is_null1 = is_null_pointer_constant(left);
1004 int is_null2 = is_null_pointer_constant(right);
1005 if (is_null1 == 2)
1006 bad_null(left);
1007 if (is_null2 == 2)
1008 bad_null(right);
1009 if (is_null1 && is_null2) {
1010 int positive = expr->op == SPECIAL_EQUAL;
1011 expr->type = EXPR_VALUE;
1012 expr->value = positive;
1013 goto OK;
1015 if (is_null1) {
1016 left = cast_to(left, rtype);
1017 goto OK;
1019 if (is_null2) {
1020 right = cast_to(right, ltype);
1021 goto OK;
1023 /* they also have special treatment for pointers to void */
1024 if (lclass & rclass & TYPE_PTR) {
1025 if (get_base_type(ltype) == &void_ctype) {
1026 right = cast_to(right, ltype);
1027 goto OK;
1029 if (get_base_type(rtype) == &void_ctype) {
1030 left = cast_to(left, rtype);
1031 goto OK;
1035 /* both should be pointers */
1036 if (!(lclass & rclass & TYPE_PTR))
1037 return bad_expr_type(expr);
1039 expr->op = modify_for_unsigned(expr->op);
1040 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
1041 if (!typediff)
1042 goto OK;
1044 expression_error(expr, "incompatible types in comparison expression (%s)", typediff);
1045 return NULL;
1048 expr->ctype = &bool_ctype;
1049 return &bool_ctype;
1053 * NOTE! The degenerate case of "x ? : y", where we don't
1054 * have a true case, this will possibly promote "x" to the
1055 * same type as "y", and thus _change_ the conditional
1056 * test in the expression. But since promotion is "safe"
1057 * for testing, that's OK.
1059 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1061 struct expression **true;
1062 struct symbol *ctype, *ltype, *rtype, *lbase, *rbase;
1063 int lclass, rclass;
1064 const char * typediff;
1065 int qual;
1067 if (!evaluate_conditional(expr->conditional, 0))
1068 return NULL;
1069 if (!evaluate_expression(expr->cond_false))
1070 return NULL;
1072 ctype = degenerate(expr->conditional);
1073 rtype = degenerate(expr->cond_false);
1075 true = &expr->conditional;
1076 ltype = ctype;
1077 if (expr->cond_true) {
1078 if (!evaluate_expression(expr->cond_true))
1079 return NULL;
1080 ltype = degenerate(expr->cond_true);
1081 true = &expr->cond_true;
1084 if (expr->flags) {
1085 int flags = expr->conditional->flags & Int_const_expr;
1086 flags &= (*true)->flags & expr->cond_false->flags;
1087 if (!flags)
1088 expr->flags = 0;
1091 lclass = classify_type(ltype, &ltype);
1092 rclass = classify_type(rtype, &rtype);
1093 if (lclass & rclass & TYPE_NUM) {
1094 ctype = usual_conversions('?', *true, expr->cond_false,
1095 lclass, rclass, ltype, rtype);
1096 *true = cast_to(*true, ctype);
1097 expr->cond_false = cast_to(expr->cond_false, ctype);
1098 goto out;
1101 if ((lclass | rclass) & TYPE_PTR) {
1102 int is_null1 = is_null_pointer_constant(*true);
1103 int is_null2 = is_null_pointer_constant(expr->cond_false);
1105 if (is_null1 && is_null2) {
1106 *true = cast_to(*true, &ptr_ctype);
1107 expr->cond_false = cast_to(expr->cond_false, &ptr_ctype);
1108 ctype = &ptr_ctype;
1109 goto out;
1111 if (is_null1 && (rclass & TYPE_PTR)) {
1112 if (is_null1 == 2)
1113 bad_null(*true);
1114 *true = cast_to(*true, rtype);
1115 ctype = rtype;
1116 goto out;
1118 if (is_null2 && (lclass & TYPE_PTR)) {
1119 if (is_null2 == 2)
1120 bad_null(expr->cond_false);
1121 expr->cond_false = cast_to(expr->cond_false, ltype);
1122 ctype = ltype;
1123 goto out;
1125 if (!(lclass & rclass & TYPE_PTR)) {
1126 typediff = "different types";
1127 goto Err;
1129 /* OK, it's pointer on pointer */
1130 if (ltype->ctype.as != rtype->ctype.as) {
1131 typediff = "different address spaces";
1132 goto Err;
1135 /* need to be lazier here */
1136 lbase = get_base_type(ltype);
1137 rbase = get_base_type(rtype);
1138 qual = ltype->ctype.modifiers | rtype->ctype.modifiers;
1139 qual &= MOD_CONST | MOD_VOLATILE;
1141 if (lbase == &void_ctype) {
1142 /* XXX: pointers to function should warn here */
1143 ctype = ltype;
1144 goto Qual;
1147 if (rbase == &void_ctype) {
1148 /* XXX: pointers to function should warn here */
1149 ctype = rtype;
1150 goto Qual;
1152 /* XXX: that should be pointer to composite */
1153 ctype = ltype;
1154 typediff = type_difference(lbase, rbase, MOD_IGN, MOD_IGN);
1155 if (!typediff)
1156 goto Qual;
1157 goto Err;
1160 /* void on void, struct on same struct, union on same union */
1161 if (ltype == rtype) {
1162 ctype = ltype;
1163 goto out;
1165 typediff = "different base types";
1167 Err:
1168 expression_error(expr, "incompatible types in conditional expression (%s)", typediff);
1169 return NULL;
1171 out:
1172 expr->ctype = ctype;
1173 return ctype;
1175 Qual:
1176 if (qual & ~ctype->ctype.modifiers) {
1177 struct symbol *sym = alloc_symbol(ctype->pos, SYM_PTR);
1178 *sym = *ctype;
1179 sym->ctype.modifiers |= qual;
1180 ctype = sym;
1182 *true = cast_to(*true, ctype);
1183 expr->cond_false = cast_to(expr->cond_false, ctype);
1184 goto out;
1187 /* FP assignments can not do modulo or bit operations */
1188 static int compatible_float_op(int op)
1190 return op == SPECIAL_ADD_ASSIGN ||
1191 op == SPECIAL_SUB_ASSIGN ||
1192 op == SPECIAL_MUL_ASSIGN ||
1193 op == SPECIAL_DIV_ASSIGN;
1196 static int evaluate_assign_op(struct expression *expr)
1198 struct symbol *target = expr->left->ctype;
1199 struct symbol *source = expr->right->ctype;
1200 struct symbol *t, *s;
1201 int tclass = classify_type(target, &t);
1202 int sclass = classify_type(source, &s);
1203 int op = expr->op;
1205 if (tclass & sclass & TYPE_NUM) {
1206 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1207 expression_error(expr, "invalid assignment");
1208 return 0;
1210 if (tclass & TYPE_RESTRICT) {
1211 if (!restricted_binop(op, t)) {
1212 expression_error(expr, "bad restricted assignment");
1213 return 0;
1215 /* allowed assignments unfoul */
1216 if (sclass & TYPE_FOULED && s->ctype.base_type == t)
1217 goto Cast;
1218 if (!restricted_value(expr->right, t))
1219 return 1;
1220 } else if (!(sclass & TYPE_RESTRICT))
1221 goto Cast;
1222 /* source and target would better be identical restricted */
1223 if (t == s)
1224 return 1;
1225 warning(expr->pos, "invalid restricted assignment");
1226 expr->right = cast_to(expr->right, target);
1227 return 0;
1229 if (tclass & TYPE_PTR && is_int(sclass)) {
1230 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1231 unrestrict(expr->right, sclass, &s);
1232 evaluate_ptr_add(expr, target, s);
1233 return 1;
1235 expression_error(expr, "invalid pointer assignment");
1236 return 0;
1239 expression_error(expr, "invalid assignment");
1240 return 0;
1242 Cast:
1243 expr->right = cast_to(expr->right, target);
1244 return 1;
1247 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1248 struct expression **rp, const char *where)
1250 const char *typediff;
1251 struct symbol *source = degenerate(*rp);
1252 struct symbol *t, *s;
1253 int tclass = classify_type(target, &t);
1254 int sclass = classify_type(source, &s);
1256 if (tclass & sclass & TYPE_NUM) {
1257 if (tclass & TYPE_RESTRICT) {
1258 /* allowed assignments unfoul */
1259 if (sclass & TYPE_FOULED && s->ctype.base_type == t)
1260 goto Cast;
1261 if (!restricted_value(*rp, target))
1262 return 1;
1263 if (s == t)
1264 return 1;
1265 } else if (!(sclass & TYPE_RESTRICT))
1266 goto Cast;
1267 typediff = "different base types";
1268 goto Err;
1271 if (tclass == TYPE_PTR) {
1272 unsigned long mod1, mod2;
1273 struct symbol *b1, *b2;
1274 // NULL pointer is always OK
1275 int is_null = is_null_pointer_constant(*rp);
1276 if (is_null) {
1277 if (is_null == 2)
1278 bad_null(*rp);
1279 goto Cast;
1281 if (!(sclass & TYPE_PTR)) {
1282 typediff = "different base types";
1283 goto Err;
1285 /* we should be more lazy here */
1286 mod1 = t->ctype.modifiers;
1287 mod2 = s->ctype.modifiers;
1288 b1 = get_base_type(t);
1289 b2 = get_base_type(s);
1290 if (b1 == &void_ctype || b2 == &void_ctype) {
1292 * assignments to/from void * are OK, provided that
1293 * we do not remove qualifiers from pointed to [C]
1294 * or mix address spaces [sparse].
1296 if (t->ctype.as != s->ctype.as) {
1297 typediff = "different address spaces";
1298 goto Err;
1300 if (mod2 & ~mod1 & MOD_IGN) {
1301 typediff = "different modifiers";
1302 goto Err;
1304 goto Cast;
1306 /* It's OK if the target is more volatile or const than the source */
1307 typediff = type_difference(target, source,
1308 MOD_VOLATILE | MOD_CONST, 0);
1309 if (typediff)
1310 goto Err;
1311 return 1;
1314 if ((tclass & TYPE_COMPOUND) && s == t)
1315 return 1;
1317 if (tclass & TYPE_NUM) {
1318 /* XXX: need to turn into comparison with NULL */
1319 if (t == &bool_ctype && (sclass & TYPE_PTR))
1320 goto Cast;
1321 typediff = "different base types";
1322 goto Err;
1324 typediff = "invalid types";
1326 Err:
1327 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1328 info(expr->pos, " expected %s", show_typename(target));
1329 info(expr->pos, " got %s", show_typename(source));
1330 *rp = cast_to(*rp, target);
1331 return 0;
1332 Cast:
1333 *rp = cast_to(*rp, target);
1334 return 1;
1337 static void mark_assigned(struct expression *expr)
1339 struct symbol *sym;
1341 if (!expr)
1342 return;
1343 switch (expr->type) {
1344 case EXPR_SYMBOL:
1345 sym = expr->symbol;
1346 if (!sym)
1347 return;
1348 if (sym->type != SYM_NODE)
1349 return;
1350 sym->ctype.modifiers |= MOD_ASSIGNED;
1351 return;
1353 case EXPR_BINOP:
1354 mark_assigned(expr->left);
1355 mark_assigned(expr->right);
1356 return;
1357 case EXPR_CAST:
1358 case EXPR_FORCE_CAST:
1359 mark_assigned(expr->cast_expression);
1360 return;
1361 case EXPR_SLICE:
1362 mark_assigned(expr->base);
1363 return;
1364 default:
1365 /* Hmm? */
1366 return;
1370 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1372 if (type->ctype.modifiers & MOD_CONST)
1373 expression_error(left, "assignment to const expression");
1375 /* We know left is an lvalue, so it's a "preop-*" */
1376 mark_assigned(left->unop);
1379 static struct symbol *evaluate_assignment(struct expression *expr)
1381 struct expression *left = expr->left;
1382 struct expression *where = expr;
1383 struct symbol *ltype;
1385 if (!lvalue_expression(left)) {
1386 expression_error(expr, "not an lvalue");
1387 return NULL;
1390 ltype = left->ctype;
1392 if (expr->op != '=') {
1393 if (!evaluate_assign_op(expr))
1394 return NULL;
1395 } else {
1396 if (!compatible_assignment_types(where, ltype, &expr->right, "assignment"))
1397 return NULL;
1400 evaluate_assign_to(left, ltype);
1402 expr->ctype = ltype;
1403 return ltype;
1406 static void examine_fn_arguments(struct symbol *fn)
1408 struct symbol *s;
1410 FOR_EACH_PTR(fn->arguments, s) {
1411 struct symbol *arg = evaluate_symbol(s);
1412 /* Array/function arguments silently degenerate into pointers */
1413 if (arg) {
1414 struct symbol *ptr;
1415 switch(arg->type) {
1416 case SYM_ARRAY:
1417 case SYM_FN:
1418 ptr = alloc_symbol(s->pos, SYM_PTR);
1419 if (arg->type == SYM_ARRAY)
1420 ptr->ctype = arg->ctype;
1421 else
1422 ptr->ctype.base_type = arg;
1423 ptr->ctype.as |= s->ctype.as;
1424 ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1426 s->ctype.base_type = ptr;
1427 s->ctype.as = 0;
1428 s->ctype.modifiers &= ~MOD_PTRINHERIT;
1429 s->bit_size = 0;
1430 s->examined = 0;
1431 examine_symbol_type(s);
1432 break;
1433 default:
1434 /* nothing */
1435 break;
1438 } END_FOR_EACH_PTR(s);
1441 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1443 /* Take the modifiers of the pointer, and apply them to the member */
1444 mod |= sym->ctype.modifiers;
1445 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1446 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1447 *newsym = *sym;
1448 newsym->ctype.as = as;
1449 newsym->ctype.modifiers = mod;
1450 sym = newsym;
1452 return sym;
1455 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1457 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1458 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1460 node->ctype.base_type = ptr;
1461 ptr->bit_size = bits_in_pointer;
1462 ptr->ctype.alignment = pointer_alignment;
1464 node->bit_size = bits_in_pointer;
1465 node->ctype.alignment = pointer_alignment;
1467 access_symbol(sym);
1468 if (sym->ctype.modifiers & MOD_REGISTER) {
1469 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1470 sym->ctype.modifiers &= ~MOD_REGISTER;
1472 if (sym->type == SYM_NODE) {
1473 ptr->ctype.as |= sym->ctype.as;
1474 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1475 sym = sym->ctype.base_type;
1477 if (degenerate && sym->type == SYM_ARRAY) {
1478 ptr->ctype.as |= sym->ctype.as;
1479 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1480 sym = sym->ctype.base_type;
1482 ptr->ctype.base_type = sym;
1484 return node;
1487 /* Arrays degenerate into pointers on pointer arithmetic */
1488 static struct symbol *degenerate(struct expression *expr)
1490 struct symbol *ctype, *base;
1492 if (!expr)
1493 return NULL;
1494 ctype = expr->ctype;
1495 if (!ctype)
1496 return NULL;
1497 base = examine_symbol_type(ctype);
1498 if (ctype->type == SYM_NODE)
1499 base = ctype->ctype.base_type;
1501 * Arrays degenerate into pointers to the entries, while
1502 * functions degenerate into pointers to themselves.
1503 * If array was part of non-lvalue compound, we create a copy
1504 * of that compound first and then act as if we were dealing with
1505 * the corresponding field in there.
1507 switch (base->type) {
1508 case SYM_ARRAY:
1509 if (expr->type == EXPR_SLICE) {
1510 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1511 struct expression *e0, *e1, *e2, *e3, *e4;
1513 a->ctype.base_type = expr->base->ctype;
1514 a->bit_size = expr->base->ctype->bit_size;
1515 a->array_size = expr->base->ctype->array_size;
1517 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1518 e0->symbol = a;
1519 e0->ctype = &lazy_ptr_ctype;
1521 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1522 e1->unop = e0;
1523 e1->op = '*';
1524 e1->ctype = expr->base->ctype; /* XXX */
1526 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1527 e2->left = e1;
1528 e2->right = expr->base;
1529 e2->op = '=';
1530 e2->ctype = expr->base->ctype;
1532 if (expr->r_bitpos) {
1533 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1534 e3->op = '+';
1535 e3->left = e0;
1536 e3->right = alloc_const_expression(expr->pos,
1537 expr->r_bitpos >> 3);
1538 e3->ctype = &lazy_ptr_ctype;
1539 } else {
1540 e3 = e0;
1543 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1544 e4->left = e2;
1545 e4->right = e3;
1546 e4->ctype = &lazy_ptr_ctype;
1548 expr->unop = e4;
1549 expr->type = EXPR_PREOP;
1550 expr->op = '*';
1552 case SYM_FN:
1553 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1554 expression_error(expr, "strange non-value function or array");
1555 return &bad_ctype;
1557 *expr = *expr->unop;
1558 ctype = create_pointer(expr, ctype, 1);
1559 expr->ctype = ctype;
1560 default:
1561 /* nothing */;
1563 return ctype;
1566 static struct symbol *evaluate_addressof(struct expression *expr)
1568 struct expression *op = expr->unop;
1569 struct symbol *ctype;
1571 if (op->op != '*' || op->type != EXPR_PREOP) {
1572 expression_error(expr, "not addressable");
1573 return NULL;
1575 ctype = op->ctype;
1576 *expr = *op->unop;
1577 expr->flags = 0;
1579 if (expr->type == EXPR_SYMBOL) {
1580 struct symbol *sym = expr->symbol;
1581 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1585 * symbol expression evaluation is lazy about the type
1586 * of the sub-expression, so we may have to generate
1587 * the type here if so..
1589 if (expr->ctype == &lazy_ptr_ctype) {
1590 ctype = create_pointer(expr, ctype, 0);
1591 expr->ctype = ctype;
1593 return expr->ctype;
1597 static struct symbol *evaluate_dereference(struct expression *expr)
1599 struct expression *op = expr->unop;
1600 struct symbol *ctype = op->ctype, *node, *target;
1602 /* Simplify: *&(expr) => (expr) */
1603 if (op->type == EXPR_PREOP && op->op == '&') {
1604 *expr = *op->unop;
1605 expr->flags = 0;
1606 return expr->ctype;
1609 /* Dereferencing a node drops all the node information. */
1610 if (ctype->type == SYM_NODE)
1611 ctype = ctype->ctype.base_type;
1613 node = alloc_symbol(expr->pos, SYM_NODE);
1614 target = ctype->ctype.base_type;
1616 switch (ctype->type) {
1617 default:
1618 expression_error(expr, "cannot dereference this type");
1619 return NULL;
1620 case SYM_PTR:
1621 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1622 merge_type(node, ctype);
1623 break;
1625 case SYM_ARRAY:
1626 if (!lvalue_expression(op)) {
1627 expression_error(op, "non-lvalue array??");
1628 return NULL;
1631 /* Do the implied "addressof" on the array */
1632 *op = *op->unop;
1635 * When an array is dereferenced, we need to pick
1636 * up the attributes of the original node too..
1638 merge_type(node, op->ctype);
1639 merge_type(node, ctype);
1640 break;
1643 node->bit_size = target->bit_size;
1644 node->array_size = target->array_size;
1646 expr->ctype = node;
1647 return node;
1651 * Unary post-ops: x++ and x--
1653 static struct symbol *evaluate_postop(struct expression *expr)
1655 struct expression *op = expr->unop;
1656 struct symbol *ctype = op->ctype;
1658 if (!lvalue_expression(expr->unop)) {
1659 expression_error(expr, "need lvalue expression for ++/--");
1660 return NULL;
1662 if (is_restricted_type(ctype) && restricted_unop(expr->op, &ctype)) {
1663 expression_error(expr, "bad operation on restricted");
1664 return NULL;
1665 } else if (is_fouled_type(ctype) && restricted_unop(expr->op, &ctype)) {
1666 expression_error(expr, "bad operation on restricted");
1667 return NULL;
1670 evaluate_assign_to(op, ctype);
1672 expr->ctype = ctype;
1673 expr->op_value = 1;
1674 if (is_ptr_type(ctype))
1675 expr->op_value = ptr_object_size(ctype) >> 3;
1677 return ctype;
1680 static struct symbol *evaluate_sign(struct expression *expr)
1682 struct symbol *ctype = expr->unop->ctype;
1683 int class = classify_type(ctype, &ctype);
1684 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1685 expr->flags = 0;
1686 /* should be an arithmetic type */
1687 if (!(class & TYPE_NUM))
1688 return bad_expr_type(expr);
1689 if (!(class & (TYPE_FLOAT|TYPE_RESTRICT))) {
1690 struct symbol *rtype = integer_promotion(ctype);
1691 expr->unop = cast_to(expr->unop, rtype);
1692 ctype = rtype;
1693 } else if ((class & TYPE_FLOAT) && expr->op != '~') {
1694 /* no conversions needed */
1695 } else if ((class & TYPE_RESTRICT) && !restricted_unop(expr->op, &ctype)) {
1696 /* no conversions needed */
1697 } else {
1698 return bad_expr_type(expr);
1700 if (expr->op == '+')
1701 *expr = *expr->unop;
1702 expr->ctype = ctype;
1703 return ctype;
1706 static struct symbol *evaluate_preop(struct expression *expr)
1708 struct symbol *ctype = expr->unop->ctype;
1710 switch (expr->op) {
1711 case '(':
1712 *expr = *expr->unop;
1713 return ctype;
1715 case '+':
1716 case '-':
1717 case '~':
1718 return evaluate_sign(expr);
1720 case '*':
1721 return evaluate_dereference(expr);
1723 case '&':
1724 return evaluate_addressof(expr);
1726 case SPECIAL_INCREMENT:
1727 case SPECIAL_DECREMENT:
1729 * From a type evaluation standpoint the preops are
1730 * the same as the postops
1732 return evaluate_postop(expr);
1734 case '!':
1735 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1736 expr->flags = 0;
1737 if (is_safe_type(ctype))
1738 warning(expr->pos, "testing a 'safe expression'");
1739 if (is_float_type(ctype)) {
1740 struct expression *arg = expr->unop;
1741 expr->type = EXPR_BINOP;
1742 expr->op = SPECIAL_EQUAL;
1743 expr->left = arg;
1744 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1745 expr->right->ctype = ctype;
1746 expr->right->fvalue = 0;
1747 } else if (is_fouled_type(ctype)) {
1748 warning(expr->pos, "restricted degrades to integer");
1750 ctype = &bool_ctype;
1751 break;
1753 default:
1754 break;
1756 expr->ctype = ctype;
1757 return &bool_ctype;
1760 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1762 struct ptr_list *head = (struct ptr_list *)_list;
1763 struct ptr_list *list = head;
1765 if (!head)
1766 return NULL;
1767 do {
1768 int i;
1769 for (i = 0; i < list->nr; i++) {
1770 struct symbol *sym = (struct symbol *) list->list[i];
1771 if (sym->ident) {
1772 if (sym->ident != ident)
1773 continue;
1774 *offset = sym->offset;
1775 return sym;
1776 } else {
1777 struct symbol *ctype = sym->ctype.base_type;
1778 struct symbol *sub;
1779 if (!ctype)
1780 continue;
1781 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1782 continue;
1783 sub = find_identifier(ident, ctype->symbol_list, offset);
1784 if (!sub)
1785 continue;
1786 *offset += sym->offset;
1787 return sub;
1790 } while ((list = list->next) != head);
1791 return NULL;
1794 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1796 struct expression *add;
1799 * Create a new add-expression
1801 * NOTE! Even if we just add zero, we need a new node
1802 * for the member pointer, since it has a different
1803 * type than the original pointer. We could make that
1804 * be just a cast, but the fact is, a node is a node,
1805 * so we might as well just do the "add zero" here.
1807 add = alloc_expression(expr->pos, EXPR_BINOP);
1808 add->op = '+';
1809 add->left = expr;
1810 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1811 add->right->ctype = &int_ctype;
1812 add->right->value = offset;
1815 * The ctype of the pointer will be lazily evaluated if
1816 * we ever take the address of this member dereference..
1818 add->ctype = &lazy_ptr_ctype;
1819 return add;
1822 /* structure/union dereference */
1823 static struct symbol *evaluate_member_dereference(struct expression *expr)
1825 int offset;
1826 struct symbol *ctype, *member;
1827 struct expression *deref = expr->deref, *add;
1828 struct ident *ident = expr->member;
1829 unsigned int mod;
1830 int address_space;
1832 if (!evaluate_expression(deref))
1833 return NULL;
1834 if (!ident) {
1835 expression_error(expr, "bad member name");
1836 return NULL;
1839 ctype = deref->ctype;
1840 address_space = ctype->ctype.as;
1841 mod = ctype->ctype.modifiers;
1842 if (ctype->type == SYM_NODE) {
1843 ctype = ctype->ctype.base_type;
1844 address_space |= ctype->ctype.as;
1845 mod |= ctype->ctype.modifiers;
1847 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1848 expression_error(expr, "expected structure or union");
1849 return NULL;
1851 examine_symbol_type(ctype);
1852 offset = 0;
1853 member = find_identifier(ident, ctype->symbol_list, &offset);
1854 if (!member) {
1855 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1856 const char *name = "<unnamed>";
1857 int namelen = 9;
1858 if (ctype->ident) {
1859 name = ctype->ident->name;
1860 namelen = ctype->ident->len;
1862 if (ctype->symbol_list)
1863 expression_error(expr, "no member '%s' in %s %.*s",
1864 show_ident(ident), type, namelen, name);
1865 else
1866 expression_error(expr, "using member '%s' in "
1867 "incomplete %s %.*s", show_ident(ident),
1868 type, namelen, name);
1869 return NULL;
1873 * The member needs to take on the address space and modifiers of
1874 * the "parent" type.
1876 member = convert_to_as_mod(member, address_space, mod);
1877 ctype = get_base_type(member);
1879 if (!lvalue_expression(deref)) {
1880 if (deref->type != EXPR_SLICE) {
1881 expr->base = deref;
1882 expr->r_bitpos = 0;
1883 } else {
1884 expr->base = deref->base;
1885 expr->r_bitpos = deref->r_bitpos;
1887 expr->r_bitpos += offset << 3;
1888 expr->type = EXPR_SLICE;
1889 expr->r_nrbits = member->bit_size;
1890 expr->r_bitpos += member->bit_offset;
1891 expr->ctype = member;
1892 return member;
1895 deref = deref->unop;
1896 expr->deref = deref;
1898 add = evaluate_offset(deref, offset);
1899 expr->type = EXPR_PREOP;
1900 expr->op = '*';
1901 expr->unop = add;
1903 expr->ctype = member;
1904 return member;
1907 static int is_promoted(struct expression *expr)
1909 while (1) {
1910 switch (expr->type) {
1911 case EXPR_BINOP:
1912 case EXPR_SELECT:
1913 case EXPR_CONDITIONAL:
1914 return 1;
1915 case EXPR_COMMA:
1916 expr = expr->right;
1917 continue;
1918 case EXPR_PREOP:
1919 switch (expr->op) {
1920 case '(':
1921 expr = expr->unop;
1922 continue;
1923 case '+':
1924 case '-':
1925 case '~':
1926 return 1;
1927 default:
1928 return 0;
1930 default:
1931 return 0;
1937 static struct symbol *evaluate_cast(struct expression *);
1939 static struct symbol *evaluate_type_information(struct expression *expr)
1941 struct symbol *sym = expr->cast_type;
1942 if (!sym) {
1943 sym = evaluate_expression(expr->cast_expression);
1944 if (!sym)
1945 return NULL;
1947 * Expressions of restricted types will possibly get
1948 * promoted - check that here
1950 if (is_restricted_type(sym)) {
1951 if (sym->bit_size < bits_in_int && is_promoted(expr))
1952 sym = &int_ctype;
1953 } else if (is_fouled_type(sym)) {
1954 sym = &int_ctype;
1957 examine_symbol_type(sym);
1958 if (is_bitfield_type(sym)) {
1959 expression_error(expr, "trying to examine bitfield type");
1960 return NULL;
1962 return sym;
1965 static struct symbol *evaluate_sizeof(struct expression *expr)
1967 struct symbol *type;
1968 int size;
1970 type = evaluate_type_information(expr);
1971 if (!type)
1972 return NULL;
1974 size = type->bit_size;
1975 if ((size < 0) || (size & 7))
1976 expression_error(expr, "cannot size expression");
1977 expr->type = EXPR_VALUE;
1978 expr->value = size >> 3;
1979 expr->taint = 0;
1980 expr->ctype = size_t_ctype;
1981 return size_t_ctype;
1984 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
1986 struct symbol *type;
1987 int size;
1989 type = evaluate_type_information(expr);
1990 if (!type)
1991 return NULL;
1993 if (type->type == SYM_NODE)
1994 type = type->ctype.base_type;
1995 if (!type)
1996 return NULL;
1997 switch (type->type) {
1998 case SYM_ARRAY:
1999 break;
2000 case SYM_PTR:
2001 type = get_base_type(type);
2002 if (type)
2003 break;
2004 default:
2005 expression_error(expr, "expected pointer expression");
2006 return NULL;
2008 size = type->bit_size;
2009 if (size & 7)
2010 size = 0;
2011 expr->type = EXPR_VALUE;
2012 expr->value = size >> 3;
2013 expr->taint = 0;
2014 expr->ctype = size_t_ctype;
2015 return size_t_ctype;
2018 static struct symbol *evaluate_alignof(struct expression *expr)
2020 struct symbol *type;
2022 type = evaluate_type_information(expr);
2023 if (!type)
2024 return NULL;
2026 expr->type = EXPR_VALUE;
2027 expr->value = type->ctype.alignment;
2028 expr->taint = 0;
2029 expr->ctype = size_t_ctype;
2030 return size_t_ctype;
2033 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
2035 struct expression *expr;
2036 struct symbol_list *argument_types = fn->arguments;
2037 struct symbol *argtype;
2038 int i = 1;
2040 PREPARE_PTR_LIST(argument_types, argtype);
2041 FOR_EACH_PTR (head, expr) {
2042 struct expression **p = THIS_ADDRESS(expr);
2043 struct symbol *ctype, *target;
2044 ctype = evaluate_expression(expr);
2046 if (!ctype)
2047 return 0;
2049 target = argtype;
2050 if (!target) {
2051 struct symbol *type;
2052 int class = classify_type(ctype, &type);
2053 if (is_int(class)) {
2054 *p = cast_to(expr, integer_promotion(type));
2055 } else if (class & TYPE_FLOAT) {
2056 unsigned long mod = type->ctype.modifiers;
2057 if (!(mod & (MOD_LONG|MOD_LONGLONG)))
2058 *p = cast_to(expr, &double_ctype);
2059 } else if (class & TYPE_PTR) {
2060 if (expr->ctype == &null_ctype)
2061 *p = cast_to(expr, &ptr_ctype);
2062 else
2063 degenerate(expr);
2065 } else {
2066 static char where[30];
2067 examine_symbol_type(target);
2068 sprintf(where, "argument %d", i);
2069 compatible_assignment_types(expr, target, p, where);
2072 i++;
2073 NEXT_PTR_LIST(argtype);
2074 } END_FOR_EACH_PTR(expr);
2075 FINISH_PTR_LIST(argtype);
2076 return 1;
2079 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
2081 struct symbol *sym;
2083 FOR_EACH_PTR(ctype->symbol_list, sym) {
2084 if (sym->ident == ident)
2085 return sym;
2086 } END_FOR_EACH_PTR(sym);
2087 return NULL;
2090 static void convert_index(struct expression *e)
2092 struct expression *child = e->idx_expression;
2093 unsigned from = e->idx_from;
2094 unsigned to = e->idx_to + 1;
2095 e->type = EXPR_POS;
2096 e->init_offset = from * (e->ctype->bit_size>>3);
2097 e->init_nr = to - from;
2098 e->init_expr = child;
2101 static void convert_ident(struct expression *e)
2103 struct expression *child = e->ident_expression;
2104 struct symbol *sym = e->field;
2105 e->type = EXPR_POS;
2106 e->init_offset = sym->offset;
2107 e->init_nr = 1;
2108 e->init_expr = child;
2111 static void convert_designators(struct expression *e)
2113 while (e) {
2114 if (e->type == EXPR_INDEX)
2115 convert_index(e);
2116 else if (e->type == EXPR_IDENTIFIER)
2117 convert_ident(e);
2118 else
2119 break;
2120 e = e->init_expr;
2124 static void excess(struct expression *e, const char *s)
2126 warning(e->pos, "excessive elements in %s initializer", s);
2130 * implicit designator for the first element
2132 static struct expression *first_subobject(struct symbol *ctype, int class,
2133 struct expression **v)
2135 struct expression *e = *v, *new;
2137 if (ctype->type == SYM_NODE)
2138 ctype = ctype->ctype.base_type;
2140 if (class & TYPE_PTR) { /* array */
2141 if (!ctype->bit_size)
2142 return NULL;
2143 new = alloc_expression(e->pos, EXPR_INDEX);
2144 new->idx_expression = e;
2145 new->ctype = ctype->ctype.base_type;
2146 } else {
2147 struct symbol *field, *p;
2148 PREPARE_PTR_LIST(ctype->symbol_list, p);
2149 while (p && !p->ident && is_bitfield_type(p))
2150 NEXT_PTR_LIST(p);
2151 field = p;
2152 FINISH_PTR_LIST(p);
2153 if (!field)
2154 return NULL;
2155 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2156 new->ident_expression = e;
2157 new->field = new->ctype = field;
2159 *v = new;
2160 return new;
2164 * sanity-check explicit designators; return the innermost one or NULL
2165 * in case of error. Assign types.
2167 static struct expression *check_designators(struct expression *e,
2168 struct symbol *ctype)
2170 struct expression *last = NULL;
2171 const char *err;
2172 while (1) {
2173 if (ctype->type == SYM_NODE)
2174 ctype = ctype->ctype.base_type;
2175 if (e->type == EXPR_INDEX) {
2176 struct symbol *type;
2177 if (ctype->type != SYM_ARRAY) {
2178 err = "array index in non-array";
2179 break;
2181 type = ctype->ctype.base_type;
2182 if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2183 unsigned offset = e->idx_to * type->bit_size;
2184 if (offset >= ctype->bit_size) {
2185 err = "index out of bounds in";
2186 break;
2189 e->ctype = ctype = type;
2190 ctype = type;
2191 last = e;
2192 if (!e->idx_expression) {
2193 err = "invalid";
2194 break;
2196 e = e->idx_expression;
2197 } else if (e->type == EXPR_IDENTIFIER) {
2198 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2199 err = "field name not in struct or union";
2200 break;
2202 ctype = find_struct_ident(ctype, e->expr_ident);
2203 if (!ctype) {
2204 err = "unknown field name in";
2205 break;
2207 e->field = e->ctype = ctype;
2208 last = e;
2209 if (!e->ident_expression) {
2210 err = "invalid";
2211 break;
2213 e = e->ident_expression;
2214 } else if (e->type == EXPR_POS) {
2215 err = "internal front-end error: EXPR_POS in";
2216 break;
2217 } else
2218 return last;
2220 expression_error(e, "%s initializer", err);
2221 return NULL;
2225 * choose the next subobject to initialize.
2227 * Get designators for next element, switch old ones to EXPR_POS.
2228 * Return the resulting expression or NULL if we'd run out of subobjects.
2229 * The innermost designator is returned in *v. Designators in old
2230 * are assumed to be already sanity-checked.
2232 static struct expression *next_designators(struct expression *old,
2233 struct symbol *ctype,
2234 struct expression *e, struct expression **v)
2236 struct expression *new = NULL;
2238 if (!old)
2239 return NULL;
2240 if (old->type == EXPR_INDEX) {
2241 struct expression *copy;
2242 unsigned n;
2244 copy = next_designators(old->idx_expression,
2245 old->ctype, e, v);
2246 if (!copy) {
2247 n = old->idx_to + 1;
2248 if (n * old->ctype->bit_size == ctype->bit_size) {
2249 convert_index(old);
2250 return NULL;
2252 copy = e;
2253 *v = new = alloc_expression(e->pos, EXPR_INDEX);
2254 } else {
2255 n = old->idx_to;
2256 new = alloc_expression(e->pos, EXPR_INDEX);
2259 new->idx_from = new->idx_to = n;
2260 new->idx_expression = copy;
2261 new->ctype = old->ctype;
2262 convert_index(old);
2263 } else if (old->type == EXPR_IDENTIFIER) {
2264 struct expression *copy;
2265 struct symbol *field;
2267 copy = next_designators(old->ident_expression,
2268 old->ctype, e, v);
2269 if (!copy) {
2270 field = old->field->next_subobject;
2271 if (!field) {
2272 convert_ident(old);
2273 return NULL;
2275 copy = e;
2276 *v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2277 } else {
2278 field = old->field;
2279 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2282 new->field = field;
2283 new->expr_ident = field->ident;
2284 new->ident_expression = copy;
2285 new->ctype = field;
2286 convert_ident(old);
2288 return new;
2291 static int handle_simple_initializer(struct expression **ep, int nested,
2292 int class, struct symbol *ctype);
2295 * deal with traversing subobjects [6.7.8(17,18,20)]
2297 static void handle_list_initializer(struct expression *expr,
2298 int class, struct symbol *ctype)
2300 struct expression *e, *last = NULL, *top = NULL, *next;
2301 int jumped = 0;
2303 FOR_EACH_PTR(expr->expr_list, e) {
2304 struct expression **v;
2305 struct symbol *type;
2306 int lclass;
2308 if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2309 if (!top) {
2310 top = e;
2311 last = first_subobject(ctype, class, &top);
2312 } else {
2313 last = next_designators(last, ctype, e, &top);
2315 if (!last) {
2316 excess(e, class & TYPE_PTR ? "array" :
2317 "struct or union");
2318 DELETE_CURRENT_PTR(e);
2319 continue;
2321 if (jumped) {
2322 warning(e->pos, "advancing past deep designator");
2323 jumped = 0;
2325 REPLACE_CURRENT_PTR(e, last);
2326 } else {
2327 next = check_designators(e, ctype);
2328 if (!next) {
2329 DELETE_CURRENT_PTR(e);
2330 continue;
2332 top = next;
2333 /* deeper than one designator? */
2334 jumped = top != e;
2335 convert_designators(last);
2336 last = e;
2339 found:
2340 lclass = classify_type(top->ctype, &type);
2341 if (top->type == EXPR_INDEX)
2342 v = &top->idx_expression;
2343 else
2344 v = &top->ident_expression;
2346 if (handle_simple_initializer(v, 1, lclass, top->ctype))
2347 continue;
2349 if (!(lclass & TYPE_COMPOUND)) {
2350 warning(e->pos, "bogus scalar initializer");
2351 DELETE_CURRENT_PTR(e);
2352 continue;
2355 next = first_subobject(type, lclass, v);
2356 if (next) {
2357 warning(e->pos, "missing braces around initializer");
2358 top = next;
2359 goto found;
2362 DELETE_CURRENT_PTR(e);
2363 excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2365 } END_FOR_EACH_PTR(e);
2367 convert_designators(last);
2368 expr->ctype = ctype;
2371 static int is_string_literal(struct expression **v)
2373 struct expression *e = *v;
2374 while (e && e->type == EXPR_PREOP && e->op == '(')
2375 e = e->unop;
2376 if (!e || e->type != EXPR_STRING)
2377 return 0;
2378 if (e != *v && Wparen_string)
2379 warning(e->pos,
2380 "array initialized from parenthesized string constant");
2381 *v = e;
2382 return 1;
2386 * We want a normal expression, possibly in one layer of braces. Warn
2387 * if the latter happens inside a list (it's legal, but likely to be
2388 * an effect of screwup). In case of anything not legal, we are definitely
2389 * having an effect of screwup, so just fail and let the caller warn.
2391 static struct expression *handle_scalar(struct expression *e, int nested)
2393 struct expression *v = NULL, *p;
2394 int count = 0;
2396 /* normal case */
2397 if (e->type != EXPR_INITIALIZER)
2398 return e;
2400 FOR_EACH_PTR(e->expr_list, p) {
2401 if (!v)
2402 v = p;
2403 count++;
2404 } END_FOR_EACH_PTR(p);
2405 if (count != 1)
2406 return NULL;
2407 switch(v->type) {
2408 case EXPR_INITIALIZER:
2409 case EXPR_INDEX:
2410 case EXPR_IDENTIFIER:
2411 return NULL;
2412 default:
2413 break;
2415 if (nested)
2416 warning(e->pos, "braces around scalar initializer");
2417 return v;
2421 * deal with the cases that don't care about subobjects:
2422 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2423 * character array <- string literal, possibly in braces [6.7.8(14)]
2424 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2425 * compound type <- initializer list in braces [6.7.8(16)]
2426 * The last one punts to handle_list_initializer() which, in turn will call
2427 * us for individual elements of the list.
2429 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2430 * the lack of support of wide char stuff in general.
2432 * One note: we need to take care not to evaluate a string literal until
2433 * we know that we *will* handle it right here. Otherwise we would screw
2434 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2435 * { "string", ...} - we need to preserve that string literal recognizable
2436 * until we dig into the inner struct.
2438 static int handle_simple_initializer(struct expression **ep, int nested,
2439 int class, struct symbol *ctype)
2441 int is_string = is_string_type(ctype);
2442 struct expression *e = *ep, *p;
2443 struct symbol *type;
2445 if (!e)
2446 return 0;
2448 /* scalar */
2449 if (!(class & TYPE_COMPOUND)) {
2450 e = handle_scalar(e, nested);
2451 if (!e)
2452 return 0;
2453 *ep = e;
2454 if (!evaluate_expression(e))
2455 return 1;
2456 compatible_assignment_types(e, ctype, ep, "initializer");
2457 return 1;
2461 * sublist; either a string, or we dig in; the latter will deal with
2462 * pathologies, so we don't need anything fancy here.
2464 if (e->type == EXPR_INITIALIZER) {
2465 if (is_string) {
2466 struct expression *v = NULL;
2467 int count = 0;
2469 FOR_EACH_PTR(e->expr_list, p) {
2470 if (!v)
2471 v = p;
2472 count++;
2473 } END_FOR_EACH_PTR(p);
2474 if (count == 1 && is_string_literal(&v)) {
2475 *ep = e = v;
2476 goto String;
2479 handle_list_initializer(e, class, ctype);
2480 return 1;
2483 /* string */
2484 if (is_string_literal(&e)) {
2485 /* either we are doing array of char, or we'll have to dig in */
2486 if (is_string) {
2487 *ep = e;
2488 goto String;
2490 return 0;
2492 /* struct or union can be initialized by compatible */
2493 if (class != TYPE_COMPOUND)
2494 return 0;
2495 type = evaluate_expression(e);
2496 if (!type)
2497 return 0;
2498 if (ctype->type == SYM_NODE)
2499 ctype = ctype->ctype.base_type;
2500 if (type->type == SYM_NODE)
2501 type = type->ctype.base_type;
2502 if (ctype == type)
2503 return 1;
2504 return 0;
2506 String:
2507 p = alloc_expression(e->pos, EXPR_STRING);
2508 *p = *e;
2509 type = evaluate_expression(p);
2510 if (ctype->bit_size != -1 &&
2511 ctype->bit_size + bits_in_char < type->bit_size) {
2512 warning(e->pos,
2513 "too long initializer-string for array of char");
2515 *ep = p;
2516 return 1;
2519 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2521 struct symbol *type;
2522 int class = classify_type(ctype, &type);
2523 if (!handle_simple_initializer(ep, 0, class, ctype))
2524 expression_error(*ep, "invalid initializer");
2527 static struct symbol *evaluate_cast(struct expression *expr)
2529 struct expression *target = expr->cast_expression;
2530 struct symbol *ctype;
2531 struct symbol *t1, *t2;
2532 int class1, class2;
2533 int as1 = 0, as2 = 0;
2535 if (!target)
2536 return NULL;
2539 * Special case: a cast can be followed by an
2540 * initializer, in which case we need to pass
2541 * the type value down to that initializer rather
2542 * than trying to evaluate it as an expression
2544 * A more complex case is when the initializer is
2545 * dereferenced as part of a post-fix expression.
2546 * We need to produce an expression that can be dereferenced.
2548 if (target->type == EXPR_INITIALIZER) {
2549 struct symbol *sym = expr->cast_type;
2550 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2552 sym->initializer = target;
2553 evaluate_symbol(sym);
2555 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2556 addr->symbol = sym;
2558 expr->type = EXPR_PREOP;
2559 expr->op = '*';
2560 expr->unop = addr;
2561 expr->ctype = sym;
2563 return sym;
2566 ctype = examine_symbol_type(expr->cast_type);
2567 expr->ctype = ctype;
2568 expr->cast_type = ctype;
2570 evaluate_expression(target);
2571 degenerate(target);
2573 class1 = classify_type(ctype, &t1);
2575 /* cast to non-integer type -> not an integer constant expression */
2576 if (!is_int(class1))
2577 expr->flags = 0;
2578 /* if argument turns out to be not an integer constant expression *and*
2579 it was not a floating literal to start with -> too bad */
2580 else if (expr->flags == Int_const_expr &&
2581 !(target->flags & Int_const_expr))
2582 expr->flags = 0;
2584 * You can always throw a value away by casting to
2585 * "void" - that's an implicit "force". Note that
2586 * the same is _not_ true of "void *".
2588 if (t1 == &void_ctype)
2589 goto out;
2591 if (class1 & TYPE_COMPOUND)
2592 warning(expr->pos, "cast to non-scalar");
2594 if (class1 == TYPE_PTR)
2595 get_base_type(t1);
2597 t2 = target->ctype;
2598 if (!t2) {
2599 expression_error(expr, "cast from unknown type");
2600 goto out;
2602 class2 = classify_type(t2, &t2);
2604 if (class2 & TYPE_COMPOUND)
2605 warning(expr->pos, "cast from non-scalar");
2607 if (expr->type == EXPR_FORCE_CAST)
2608 goto out;
2610 /* allowed cast unfouls */
2611 if (class2 & TYPE_FOULED)
2612 t2 = t2->ctype.base_type;
2614 if (t1 != t2) {
2615 if (class1 & TYPE_RESTRICT)
2616 warning(expr->pos, "cast to restricted type");
2617 if (class2 & TYPE_RESTRICT)
2618 warning(expr->pos, "cast from restricted type");
2621 if (t1 == &ulong_ctype)
2622 as1 = -1;
2623 else if (class1 == TYPE_PTR)
2624 as1 = t1->ctype.as;
2626 if (t2 == &ulong_ctype)
2627 as2 = -1;
2628 else if (class2 == TYPE_PTR)
2629 as2 = t2->ctype.as;
2631 if (!as1 && as2 > 0)
2632 warning(expr->pos, "cast removes address space of expression");
2633 if (as1 > 0 && as2 > 0 && as1 != as2)
2634 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2635 if (as1 > 0 && !as2 &&
2636 !is_null_pointer_constant(target) && Wcast_to_address_space)
2637 warning(expr->pos,
2638 "cast adds address space to expression (<asn:%d>)", as1);
2640 if (!(t1->ctype.modifiers & MOD_PTRINHERIT) && class1 == TYPE_PTR &&
2641 !as1 && (target->flags & Int_const_expr)) {
2642 if (t1->ctype.base_type == &void_ctype) {
2643 if (is_zero_constant(target)) {
2644 /* NULL */
2645 expr->type = EXPR_VALUE;
2646 expr->ctype = &null_ctype;
2647 expr->value = 0;
2648 return ctype;
2652 out:
2653 return ctype;
2657 * Evaluate a call expression with a symbol. This
2658 * should expand inline functions, and evaluate
2659 * builtins.
2661 static int evaluate_symbol_call(struct expression *expr)
2663 struct expression *fn = expr->fn;
2664 struct symbol *ctype = fn->ctype;
2666 if (fn->type != EXPR_PREOP)
2667 return 0;
2669 if (ctype->op && ctype->op->evaluate)
2670 return ctype->op->evaluate(expr);
2672 if (ctype->ctype.modifiers & MOD_INLINE) {
2673 int ret;
2674 struct symbol *curr = current_fn;
2675 current_fn = ctype->ctype.base_type;
2677 ret = inline_function(expr, ctype);
2679 /* restore the old function */
2680 current_fn = curr;
2681 return ret;
2684 return 0;
2687 static struct symbol *evaluate_call(struct expression *expr)
2689 int args, fnargs;
2690 struct symbol *ctype, *sym;
2691 struct expression *fn = expr->fn;
2692 struct expression_list *arglist = expr->args;
2694 if (!evaluate_expression(fn))
2695 return NULL;
2696 sym = ctype = fn->ctype;
2697 if (ctype->type == SYM_NODE)
2698 ctype = ctype->ctype.base_type;
2699 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
2700 ctype = get_base_type(ctype);
2702 examine_fn_arguments(ctype);
2703 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
2704 sym->op && sym->op->args) {
2705 if (!sym->op->args(expr))
2706 return NULL;
2707 } else {
2708 if (!evaluate_arguments(sym, ctype, arglist))
2709 return NULL;
2710 if (ctype->type != SYM_FN) {
2711 expression_error(expr, "not a function %s",
2712 show_ident(sym->ident));
2713 return NULL;
2715 args = expression_list_size(expr->args);
2716 fnargs = symbol_list_size(ctype->arguments);
2717 if (args < fnargs)
2718 expression_error(expr,
2719 "not enough arguments for function %s",
2720 show_ident(sym->ident));
2721 if (args > fnargs && !ctype->variadic)
2722 expression_error(expr,
2723 "too many arguments for function %s",
2724 show_ident(sym->ident));
2726 if (sym->type == SYM_NODE) {
2727 if (evaluate_symbol_call(expr))
2728 return expr->ctype;
2730 expr->ctype = ctype->ctype.base_type;
2731 return expr->ctype;
2734 static struct symbol *evaluate_offsetof(struct expression *expr)
2736 struct expression *e = expr->down;
2737 struct symbol *ctype = expr->in;
2738 int class;
2740 if (expr->op == '.') {
2741 struct symbol *field;
2742 int offset = 0;
2743 if (!ctype) {
2744 expression_error(expr, "expected structure or union");
2745 return NULL;
2747 examine_symbol_type(ctype);
2748 class = classify_type(ctype, &ctype);
2749 if (class != TYPE_COMPOUND) {
2750 expression_error(expr, "expected structure or union");
2751 return NULL;
2754 field = find_identifier(expr->ident, ctype->symbol_list, &offset);
2755 if (!field) {
2756 expression_error(expr, "unknown member");
2757 return NULL;
2759 ctype = field;
2760 expr->type = EXPR_VALUE;
2761 expr->flags = Int_const_expr;
2762 expr->value = offset;
2763 expr->taint = 0;
2764 expr->ctype = size_t_ctype;
2765 } else {
2766 if (!ctype) {
2767 expression_error(expr, "expected structure or union");
2768 return NULL;
2770 examine_symbol_type(ctype);
2771 class = classify_type(ctype, &ctype);
2772 if (class != (TYPE_COMPOUND | TYPE_PTR)) {
2773 expression_error(expr, "expected array");
2774 return NULL;
2776 ctype = ctype->ctype.base_type;
2777 if (!expr->index) {
2778 expr->type = EXPR_VALUE;
2779 expr->flags = Int_const_expr;
2780 expr->value = 0;
2781 expr->taint = 0;
2782 expr->ctype = size_t_ctype;
2783 } else {
2784 struct expression *idx = expr->index, *m;
2785 struct symbol *i_type = evaluate_expression(idx);
2786 int i_class = classify_type(i_type, &i_type);
2787 if (!is_int(i_class)) {
2788 expression_error(expr, "non-integer index");
2789 return NULL;
2791 unrestrict(idx, i_class, &i_type);
2792 idx = cast_to(idx, size_t_ctype);
2793 m = alloc_const_expression(expr->pos,
2794 ctype->bit_size >> 3);
2795 m->ctype = size_t_ctype;
2796 m->flags = Int_const_expr;
2797 expr->type = EXPR_BINOP;
2798 expr->left = idx;
2799 expr->right = m;
2800 expr->op = '*';
2801 expr->ctype = size_t_ctype;
2802 expr->flags = m->flags & idx->flags & Int_const_expr;
2805 if (e) {
2806 struct expression *copy = __alloc_expression(0);
2807 *copy = *expr;
2808 if (e->type == EXPR_OFFSETOF)
2809 e->in = ctype;
2810 if (!evaluate_expression(e))
2811 return NULL;
2812 expr->type = EXPR_BINOP;
2813 expr->flags = e->flags & copy->flags & Int_const_expr;
2814 expr->op = '+';
2815 expr->ctype = size_t_ctype;
2816 expr->left = copy;
2817 expr->right = e;
2819 return size_t_ctype;
2822 struct symbol *evaluate_expression(struct expression *expr)
2824 if (!expr)
2825 return NULL;
2826 if (expr->ctype)
2827 return expr->ctype;
2829 switch (expr->type) {
2830 case EXPR_VALUE:
2831 case EXPR_FVALUE:
2832 expression_error(expr, "value expression without a type");
2833 return NULL;
2834 case EXPR_STRING:
2835 return evaluate_string(expr);
2836 case EXPR_SYMBOL:
2837 return evaluate_symbol_expression(expr);
2838 case EXPR_BINOP:
2839 if (!evaluate_expression(expr->left))
2840 return NULL;
2841 if (!evaluate_expression(expr->right))
2842 return NULL;
2843 return evaluate_binop(expr);
2844 case EXPR_LOGICAL:
2845 return evaluate_logical(expr);
2846 case EXPR_COMMA:
2847 evaluate_expression(expr->left);
2848 if (!evaluate_expression(expr->right))
2849 return NULL;
2850 return evaluate_comma(expr);
2851 case EXPR_COMPARE:
2852 if (!evaluate_expression(expr->left))
2853 return NULL;
2854 if (!evaluate_expression(expr->right))
2855 return NULL;
2856 return evaluate_compare(expr);
2857 case EXPR_ASSIGNMENT:
2858 if (!evaluate_expression(expr->left))
2859 return NULL;
2860 if (!evaluate_expression(expr->right))
2861 return NULL;
2862 return evaluate_assignment(expr);
2863 case EXPR_PREOP:
2864 if (!evaluate_expression(expr->unop))
2865 return NULL;
2866 return evaluate_preop(expr);
2867 case EXPR_POSTOP:
2868 if (!evaluate_expression(expr->unop))
2869 return NULL;
2870 return evaluate_postop(expr);
2871 case EXPR_CAST:
2872 case EXPR_FORCE_CAST:
2873 case EXPR_IMPLIED_CAST:
2874 return evaluate_cast(expr);
2875 case EXPR_SIZEOF:
2876 return evaluate_sizeof(expr);
2877 case EXPR_PTRSIZEOF:
2878 return evaluate_ptrsizeof(expr);
2879 case EXPR_ALIGNOF:
2880 return evaluate_alignof(expr);
2881 case EXPR_DEREF:
2882 return evaluate_member_dereference(expr);
2883 case EXPR_CALL:
2884 return evaluate_call(expr);
2885 case EXPR_SELECT:
2886 case EXPR_CONDITIONAL:
2887 return evaluate_conditional_expression(expr);
2888 case EXPR_STATEMENT:
2889 expr->ctype = evaluate_statement(expr->statement);
2890 return expr->ctype;
2892 case EXPR_LABEL:
2893 expr->ctype = &ptr_ctype;
2894 return &ptr_ctype;
2896 case EXPR_TYPE:
2897 /* Evaluate the type of the symbol .. */
2898 evaluate_symbol(expr->symbol);
2899 /* .. but the type of the _expression_ is a "type" */
2900 expr->ctype = &type_ctype;
2901 return &type_ctype;
2903 case EXPR_OFFSETOF:
2904 return evaluate_offsetof(expr);
2906 /* These can not exist as stand-alone expressions */
2907 case EXPR_INITIALIZER:
2908 case EXPR_IDENTIFIER:
2909 case EXPR_INDEX:
2910 case EXPR_POS:
2911 expression_error(expr, "internal front-end error: initializer in expression");
2912 return NULL;
2913 case EXPR_SLICE:
2914 expression_error(expr, "internal front-end error: SLICE re-evaluated");
2915 return NULL;
2917 return NULL;
2920 static void check_duplicates(struct symbol *sym)
2922 int declared = 0;
2923 struct symbol *next = sym;
2925 while ((next = next->same_symbol) != NULL) {
2926 const char *typediff;
2927 evaluate_symbol(next);
2928 declared++;
2929 typediff = type_difference(sym, next, 0, 0);
2930 if (typediff) {
2931 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2932 show_ident(sym->ident),
2933 stream_name(next->pos.stream), next->pos.line, typediff);
2934 return;
2937 if (!declared) {
2938 unsigned long mod = sym->ctype.modifiers;
2939 if (mod & (MOD_STATIC | MOD_REGISTER))
2940 return;
2941 if (!(mod & MOD_TOPLEVEL))
2942 return;
2943 if (!Wdecl)
2944 return;
2945 if (sym->ident == &main_ident)
2946 return;
2947 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
2951 static struct symbol *evaluate_symbol(struct symbol *sym)
2953 struct symbol *base_type;
2955 if (!sym)
2956 return sym;
2957 if (sym->evaluated)
2958 return sym;
2959 sym->evaluated = 1;
2961 sym = examine_symbol_type(sym);
2962 base_type = get_base_type(sym);
2963 if (!base_type)
2964 return NULL;
2966 /* Evaluate the initializers */
2967 if (sym->initializer)
2968 evaluate_initializer(sym, &sym->initializer);
2970 /* And finally, evaluate the body of the symbol too */
2971 if (base_type->type == SYM_FN) {
2972 struct symbol *curr = current_fn;
2974 current_fn = base_type;
2976 examine_fn_arguments(base_type);
2977 if (!base_type->stmt && base_type->inline_stmt)
2978 uninline(sym);
2979 if (base_type->stmt)
2980 evaluate_statement(base_type->stmt);
2982 current_fn = curr;
2985 return base_type;
2988 void evaluate_symbol_list(struct symbol_list *list)
2990 struct symbol *sym;
2992 FOR_EACH_PTR(list, sym) {
2993 evaluate_symbol(sym);
2994 check_duplicates(sym);
2995 } END_FOR_EACH_PTR(sym);
2998 static struct symbol *evaluate_return_expression(struct statement *stmt)
3000 struct expression *expr = stmt->expression;
3001 struct symbol *fntype;
3003 evaluate_expression(expr);
3004 fntype = current_fn->ctype.base_type;
3005 if (!fntype || fntype == &void_ctype) {
3006 if (expr && expr->ctype != &void_ctype)
3007 expression_error(expr, "return expression in %s function", fntype?"void":"typeless");
3008 if (expr && Wreturn_void)
3009 warning(stmt->pos, "returning void-valued expression");
3010 return NULL;
3013 if (!expr) {
3014 sparse_error(stmt->pos, "return with no return value");
3015 return NULL;
3017 if (!expr->ctype)
3018 return NULL;
3019 compatible_assignment_types(expr, fntype, &stmt->expression, "return expression");
3020 return NULL;
3023 static void evaluate_if_statement(struct statement *stmt)
3025 if (!stmt->if_conditional)
3026 return;
3028 evaluate_conditional(stmt->if_conditional, 0);
3029 evaluate_statement(stmt->if_true);
3030 evaluate_statement(stmt->if_false);
3033 static void evaluate_iterator(struct statement *stmt)
3035 evaluate_conditional(stmt->iterator_pre_condition, 1);
3036 evaluate_conditional(stmt->iterator_post_condition,1);
3037 evaluate_statement(stmt->iterator_pre_statement);
3038 evaluate_statement(stmt->iterator_statement);
3039 evaluate_statement(stmt->iterator_post_statement);
3042 static void verify_output_constraint(struct expression *expr, const char *constraint)
3044 switch (*constraint) {
3045 case '=': /* Assignment */
3046 case '+': /* Update */
3047 break;
3048 default:
3049 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
3053 static void verify_input_constraint(struct expression *expr, const char *constraint)
3055 switch (*constraint) {
3056 case '=': /* Assignment */
3057 case '+': /* Update */
3058 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
3062 static void evaluate_asm_statement(struct statement *stmt)
3064 struct expression *expr;
3065 int state;
3067 expr = stmt->asm_string;
3068 if (!expr || expr->type != EXPR_STRING) {
3069 sparse_error(stmt->pos, "need constant string for inline asm");
3070 return;
3073 state = 0;
3074 FOR_EACH_PTR(stmt->asm_outputs, expr) {
3075 struct ident *ident;
3077 switch (state) {
3078 case 0: /* Identifier */
3079 state = 1;
3080 ident = (struct ident *)expr;
3081 continue;
3083 case 1: /* Constraint */
3084 state = 2;
3085 if (!expr || expr->type != EXPR_STRING) {
3086 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
3087 *THIS_ADDRESS(expr) = NULL;
3088 continue;
3090 verify_output_constraint(expr, expr->string->data);
3091 continue;
3093 case 2: /* Expression */
3094 state = 0;
3095 if (!evaluate_expression(expr))
3096 return;
3097 if (!lvalue_expression(expr))
3098 warning(expr->pos, "asm output is not an lvalue");
3099 evaluate_assign_to(expr, expr->ctype);
3100 continue;
3102 } END_FOR_EACH_PTR(expr);
3104 state = 0;
3105 FOR_EACH_PTR(stmt->asm_inputs, expr) {
3106 struct ident *ident;
3108 switch (state) {
3109 case 0: /* Identifier */
3110 state = 1;
3111 ident = (struct ident *)expr;
3112 continue;
3114 case 1: /* Constraint */
3115 state = 2;
3116 if (!expr || expr->type != EXPR_STRING) {
3117 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
3118 *THIS_ADDRESS(expr) = NULL;
3119 continue;
3121 verify_input_constraint(expr, expr->string->data);
3122 continue;
3124 case 2: /* Expression */
3125 state = 0;
3126 if (!evaluate_expression(expr))
3127 return;
3128 continue;
3130 } END_FOR_EACH_PTR(expr);
3132 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
3133 if (!expr) {
3134 sparse_error(stmt->pos, "bad asm output");
3135 return;
3137 if (expr->type == EXPR_STRING)
3138 continue;
3139 expression_error(expr, "asm clobber is not a string");
3140 } END_FOR_EACH_PTR(expr);
3143 static void evaluate_case_statement(struct statement *stmt)
3145 evaluate_expression(stmt->case_expression);
3146 evaluate_expression(stmt->case_to);
3147 evaluate_statement(stmt->case_statement);
3150 static void check_case_type(struct expression *switch_expr,
3151 struct expression *case_expr,
3152 struct expression **enumcase)
3154 struct symbol *switch_type, *case_type;
3155 int sclass, cclass;
3157 if (!case_expr)
3158 return;
3160 switch_type = switch_expr->ctype;
3161 case_type = evaluate_expression(case_expr);
3163 if (!switch_type || !case_type)
3164 goto Bad;
3165 if (enumcase) {
3166 if (*enumcase)
3167 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
3168 else if (is_enum_type(case_type))
3169 *enumcase = case_expr;
3172 sclass = classify_type(switch_type, &switch_type);
3173 cclass = classify_type(case_type, &case_type);
3175 /* both should be arithmetic */
3176 if (!(sclass & cclass & TYPE_NUM))
3177 goto Bad;
3179 /* neither should be floating */
3180 if ((sclass | cclass) & TYPE_FLOAT)
3181 goto Bad;
3183 /* if neither is restricted, we are OK */
3184 if (!((sclass | cclass) & TYPE_RESTRICT))
3185 return;
3187 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
3188 cclass, sclass, case_type, switch_type))
3189 warning(case_expr->pos, "restricted degrades to integer");
3191 return;
3193 Bad:
3194 expression_error(case_expr, "incompatible types for 'case' statement");
3197 static void evaluate_switch_statement(struct statement *stmt)
3199 struct symbol *sym;
3200 struct expression *enumcase = NULL;
3201 struct expression **enumcase_holder = &enumcase;
3202 struct expression *sel = stmt->switch_expression;
3204 evaluate_expression(sel);
3205 evaluate_statement(stmt->switch_statement);
3206 if (!sel)
3207 return;
3208 if (sel->ctype && is_enum_type(sel->ctype))
3209 enumcase_holder = NULL; /* Only check cases against switch */
3211 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
3212 struct statement *case_stmt = sym->stmt;
3213 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
3214 check_case_type(sel, case_stmt->case_to, enumcase_holder);
3215 } END_FOR_EACH_PTR(sym);
3218 struct symbol *evaluate_statement(struct statement *stmt)
3220 if (!stmt)
3221 return NULL;
3223 switch (stmt->type) {
3224 case STMT_DECLARATION: {
3225 struct symbol *s;
3226 FOR_EACH_PTR(stmt->declaration, s) {
3227 evaluate_symbol(s);
3228 } END_FOR_EACH_PTR(s);
3229 return NULL;
3232 case STMT_RETURN:
3233 return evaluate_return_expression(stmt);
3235 case STMT_EXPRESSION:
3236 if (!evaluate_expression(stmt->expression))
3237 return NULL;
3238 if (stmt->expression->ctype == &null_ctype)
3239 stmt->expression = cast_to(stmt->expression, &ptr_ctype);
3240 return degenerate(stmt->expression);
3242 case STMT_COMPOUND: {
3243 struct statement *s;
3244 struct symbol *type = NULL;
3246 /* Evaluate the return symbol in the compound statement */
3247 evaluate_symbol(stmt->ret);
3250 * Then, evaluate each statement, making the type of the
3251 * compound statement be the type of the last statement
3253 type = evaluate_statement(stmt->args);
3254 FOR_EACH_PTR(stmt->stmts, s) {
3255 type = evaluate_statement(s);
3256 } END_FOR_EACH_PTR(s);
3257 if (!type)
3258 type = &void_ctype;
3259 return type;
3261 case STMT_IF:
3262 evaluate_if_statement(stmt);
3263 return NULL;
3264 case STMT_ITERATOR:
3265 evaluate_iterator(stmt);
3266 return NULL;
3267 case STMT_SWITCH:
3268 evaluate_switch_statement(stmt);
3269 return NULL;
3270 case STMT_CASE:
3271 evaluate_case_statement(stmt);
3272 return NULL;
3273 case STMT_LABEL:
3274 return evaluate_statement(stmt->label_statement);
3275 case STMT_GOTO:
3276 evaluate_expression(stmt->goto_expression);
3277 return NULL;
3278 case STMT_NONE:
3279 break;
3280 case STMT_ASM:
3281 evaluate_asm_statement(stmt);
3282 return NULL;
3283 case STMT_CONTEXT:
3284 evaluate_expression(stmt->expression);
3285 return NULL;
3286 case STMT_RANGE:
3287 evaluate_expression(stmt->range_expression);
3288 evaluate_expression(stmt->range_low);
3289 evaluate_expression(stmt->range_high);
3290 return NULL;
3292 return NULL;