allocate.h: Stop needlessly returning a void value in __DO_ALLOCATOR
[smatch.git] / evaluate.c
blobdb334425fd2590664f6caccb9c97a203117379e4
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 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
626 const char *type_difference(struct ctype *c1, struct ctype *c2,
627 unsigned long mod1, unsigned long mod2)
629 unsigned long as1 = c1->as, as2 = c2->as;
630 struct symbol *t1 = c1->base_type;
631 struct symbol *t2 = c2->base_type;
632 int move1 = 1, move2 = 1;
633 mod1 |= c1->modifiers;
634 mod2 |= c2->modifiers;
635 for (;;) {
636 unsigned long diff;
637 int type;
638 struct symbol *base1 = t1->ctype.base_type;
639 struct symbol *base2 = t2->ctype.base_type;
642 * FIXME! Collect alignment and context too here!
644 if (move1) {
645 if (t1 && t1->type != SYM_PTR) {
646 mod1 |= t1->ctype.modifiers;
647 as1 |= t1->ctype.as;
649 move1 = 0;
652 if (move2) {
653 if (t2 && t2->type != SYM_PTR) {
654 mod2 |= t2->ctype.modifiers;
655 as2 |= t2->ctype.as;
657 move2 = 0;
660 if (t1 == t2)
661 break;
662 if (!t1 || !t2)
663 return "different types";
665 if (t1->type == SYM_NODE || t1->type == SYM_ENUM) {
666 t1 = base1;
667 move1 = 1;
668 if (!t1)
669 return "bad types";
670 continue;
673 if (t2->type == SYM_NODE || t2->type == SYM_ENUM) {
674 t2 = base2;
675 move2 = 1;
676 if (!t2)
677 return "bad types";
678 continue;
681 move1 = move2 = 1;
682 type = t1->type;
683 if (type != t2->type)
684 return "different base types";
686 switch (type) {
687 default:
688 sparse_error(t1->pos,
689 "internal error: bad type in derived(%d)",
690 type);
691 return "bad types";
692 case SYM_RESTRICT:
693 case SYM_UNION:
694 case SYM_STRUCT:
695 return "different base types";
696 case SYM_ARRAY:
697 /* XXX: we ought to compare sizes */
698 break;
699 case SYM_PTR:
700 if (Waddress_space && as1 != as2)
701 return "different address spaces";
702 /* MOD_SPECIFIER is due to idiocy in parse.c */
703 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SPECIFIER)
704 return "different modifiers";
705 /* we could be lazier here */
706 base1 = examine_pointer_target(t1);
707 base2 = examine_pointer_target(t2);
708 mod1 = t1->ctype.modifiers;
709 as1 = t1->ctype.as;
710 mod2 = t2->ctype.modifiers;
711 as2 = t2->ctype.as;
712 break;
713 case SYM_FN: {
714 struct symbol *arg1, *arg2;
715 int i;
717 if (Waddress_space && as1 != as2)
718 return "different address spaces";
719 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
720 return "different modifiers";
721 mod1 = t1->ctype.modifiers;
722 as1 = t1->ctype.as;
723 mod2 = t2->ctype.modifiers;
724 as2 = t2->ctype.as;
726 if (base1->variadic != base2->variadic)
727 return "incompatible variadic arguments";
728 examine_fn_arguments(t1);
729 examine_fn_arguments(t2);
730 PREPARE_PTR_LIST(t1->arguments, arg1);
731 PREPARE_PTR_LIST(t2->arguments, arg2);
732 i = 1;
733 for (;;) {
734 const char *diffstr;
735 if (!arg1 && !arg2)
736 break;
737 if (!arg1 || !arg2)
738 return "different argument counts";
739 diffstr = type_difference(&arg1->ctype,
740 &arg2->ctype,
741 MOD_IGN, MOD_IGN);
742 if (diffstr) {
743 static char argdiff[80];
744 sprintf(argdiff, "incompatible argument %d (%s)", i, diffstr);
745 return argdiff;
747 NEXT_PTR_LIST(arg1);
748 NEXT_PTR_LIST(arg2);
749 i++;
751 FINISH_PTR_LIST(arg2);
752 FINISH_PTR_LIST(arg1);
753 break;
755 case SYM_BASETYPE:
756 if (Waddress_space && as1 != as2)
757 return "different address spaces";
758 if (base1 != base2)
759 return "different base types";
760 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
761 if (!diff)
762 return NULL;
763 if (diff & MOD_SIZE)
764 return "different type sizes";
765 if (diff & ~MOD_SIGNEDNESS)
766 return "different modifiers";
768 /* Differs in signedness only.. */
769 if (Wtypesign) {
771 * Warn if both are explicitly signed ("unsigned" is obviously
772 * always explicit, and since we know one of them has to be
773 * unsigned, we check if the signed one was explicit).
775 if ((mod1 | mod2) & MOD_EXPLICITLY_SIGNED)
776 return "different explicit signedness";
779 * "char" matches both "unsigned char" and "signed char",
780 * so if the explicit test didn't trigger, then we should
781 * not warn about a char.
783 if (!(mod1 & MOD_CHAR))
784 return "different signedness";
786 return NULL;
788 t1 = base1;
789 t2 = base2;
791 if (Waddress_space && as1 != as2)
792 return "different address spaces";
793 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
794 return "different modifiers";
795 return NULL;
798 static void bad_null(struct expression *expr)
800 if (Wnon_pointer_null)
801 warning(expr->pos, "Using plain integer as NULL pointer");
804 static unsigned long target_qualifiers(struct symbol *type)
806 unsigned long mod = type->ctype.modifiers & MOD_IGN;
807 if (type->ctype.base_type && type->ctype.base_type->type == SYM_ARRAY)
808 mod = 0;
809 return mod;
812 static struct symbol *evaluate_ptr_sub(struct expression *expr)
814 const char *typediff;
815 struct symbol *ltype, *rtype;
816 struct expression *l = expr->left;
817 struct expression *r = expr->right;
818 struct symbol *lbase, *rbase;
820 classify_type(degenerate(l), &ltype);
821 classify_type(degenerate(r), &rtype);
823 lbase = examine_pointer_target(ltype);
824 rbase = examine_pointer_target(rtype);
825 typediff = type_difference(&ltype->ctype, &rtype->ctype,
826 target_qualifiers(rtype),
827 target_qualifiers(ltype));
828 if (typediff)
829 expression_error(expr, "subtraction of different types can't work (%s)", typediff);
831 if (lbase->type == SYM_FN) {
832 expression_error(expr, "subtraction of functions? Share your drugs");
833 return NULL;
836 expr->ctype = ssize_t_ctype;
837 if (lbase->bit_size > bits_in_char) {
838 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
839 struct expression *div = expr;
840 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
841 unsigned long value = lbase->bit_size >> 3;
843 val->ctype = size_t_ctype;
844 val->value = value;
846 if (value & (value-1)) {
847 if (Wptr_subtraction_blows)
848 warning(expr->pos, "potentially expensive pointer subtraction");
851 sub->op = '-';
852 sub->ctype = ssize_t_ctype;
853 sub->left = l;
854 sub->right = r;
856 div->op = '/';
857 div->left = sub;
858 div->right = val;
861 return ssize_t_ctype;
864 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
866 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
868 struct symbol *ctype;
870 if (!expr)
871 return NULL;
873 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
874 warning(expr->pos, "assignment expression in conditional");
876 ctype = evaluate_expression(expr);
877 if (ctype) {
878 if (is_safe_type(ctype))
879 warning(expr->pos, "testing a 'safe expression'");
882 return ctype;
885 static struct symbol *evaluate_logical(struct expression *expr)
887 if (!evaluate_conditional(expr->left, 0))
888 return NULL;
889 if (!evaluate_conditional(expr->right, 0))
890 return NULL;
892 expr->ctype = &bool_ctype;
893 if (expr->flags) {
894 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
895 expr->flags = 0;
897 return &bool_ctype;
900 static struct symbol *evaluate_binop(struct expression *expr)
902 struct symbol *ltype, *rtype, *ctype;
903 int lclass = classify_type(expr->left->ctype, &ltype);
904 int rclass = classify_type(expr->right->ctype, &rtype);
905 int op = expr->op;
907 if (expr->flags) {
908 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
909 expr->flags = 0;
912 /* number op number */
913 if (lclass & rclass & TYPE_NUM) {
914 if ((lclass | rclass) & TYPE_FLOAT) {
915 switch (op) {
916 case '+': case '-': case '*': case '/':
917 break;
918 default:
919 return bad_expr_type(expr);
923 if (op == SPECIAL_LEFTSHIFT || op == SPECIAL_RIGHTSHIFT) {
924 // shifts do integer promotions, but that's it.
925 unrestrict(expr->left, lclass, &ltype);
926 unrestrict(expr->right, rclass, &rtype);
927 ctype = ltype = integer_promotion(ltype);
928 rtype = integer_promotion(rtype);
929 } else {
930 // The rest do usual conversions
931 ltype = usual_conversions(op, expr->left, expr->right,
932 lclass, rclass, ltype, rtype);
933 ctype = rtype = ltype;
936 expr->left = cast_to(expr->left, ltype);
937 expr->right = cast_to(expr->right, rtype);
938 expr->ctype = ctype;
939 return ctype;
942 /* pointer (+|-) integer */
943 if (lclass & TYPE_PTR && is_int(rclass) && (op == '+' || op == '-')) {
944 unrestrict(expr->right, rclass, &rtype);
945 return evaluate_ptr_add(expr, degenerate(expr->left), rtype);
948 /* integer + pointer */
949 if (rclass & TYPE_PTR && is_int(lclass) && op == '+') {
950 struct expression *index = expr->left;
951 unrestrict(index, lclass, &ltype);
952 expr->left = expr->right;
953 expr->right = index;
954 return evaluate_ptr_add(expr, degenerate(expr->left), ltype);
957 /* pointer - pointer */
958 if (lclass & rclass & TYPE_PTR && expr->op == '-')
959 return evaluate_ptr_sub(expr);
961 return bad_expr_type(expr);
964 static struct symbol *evaluate_comma(struct expression *expr)
966 expr->ctype = expr->right->ctype;
967 expr->flags &= expr->left->flags & expr->right->flags;
968 return expr->ctype;
971 static int modify_for_unsigned(int op)
973 if (op == '<')
974 op = SPECIAL_UNSIGNED_LT;
975 else if (op == '>')
976 op = SPECIAL_UNSIGNED_GT;
977 else if (op == SPECIAL_LTE)
978 op = SPECIAL_UNSIGNED_LTE;
979 else if (op == SPECIAL_GTE)
980 op = SPECIAL_UNSIGNED_GTE;
981 return op;
984 static inline int is_null_pointer_constant(struct expression *e)
986 if (e->ctype == &null_ctype)
987 return 1;
988 if (!(e->flags & Int_const_expr))
989 return 0;
990 return is_zero_constant(e) ? 2 : 0;
993 static struct symbol *evaluate_compare(struct expression *expr)
995 struct expression *left = expr->left, *right = expr->right;
996 struct symbol *ltype, *rtype, *lbase, *rbase;
997 int lclass = classify_type(degenerate(left), &ltype);
998 int rclass = classify_type(degenerate(right), &rtype);
999 struct symbol *ctype;
1000 const char *typediff;
1002 if (expr->flags) {
1003 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
1004 expr->flags = 0;
1007 /* Type types? */
1008 if (is_type_type(ltype) && is_type_type(rtype))
1009 goto OK;
1011 if (is_safe_type(left->ctype) || is_safe_type(right->ctype))
1012 warning(expr->pos, "testing a 'safe expression'");
1014 /* number on number */
1015 if (lclass & rclass & TYPE_NUM) {
1016 ctype = usual_conversions(expr->op, expr->left, expr->right,
1017 lclass, rclass, ltype, rtype);
1018 expr->left = cast_to(expr->left, ctype);
1019 expr->right = cast_to(expr->right, ctype);
1020 if (ctype->ctype.modifiers & MOD_UNSIGNED)
1021 expr->op = modify_for_unsigned(expr->op);
1022 goto OK;
1025 /* at least one must be a pointer */
1026 if (!((lclass | rclass) & TYPE_PTR))
1027 return bad_expr_type(expr);
1029 /* equality comparisons can be with null pointer constants */
1030 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1031 int is_null1 = is_null_pointer_constant(left);
1032 int is_null2 = is_null_pointer_constant(right);
1033 if (is_null1 == 2)
1034 bad_null(left);
1035 if (is_null2 == 2)
1036 bad_null(right);
1037 if (is_null1 && is_null2) {
1038 int positive = expr->op == SPECIAL_EQUAL;
1039 expr->type = EXPR_VALUE;
1040 expr->value = positive;
1041 goto OK;
1043 if (is_null1) {
1044 left = cast_to(left, rtype);
1045 goto OK;
1047 if (is_null2) {
1048 right = cast_to(right, ltype);
1049 goto OK;
1052 /* both should be pointers */
1053 if (!(lclass & rclass & TYPE_PTR))
1054 return bad_expr_type(expr);
1055 expr->op = modify_for_unsigned(expr->op);
1057 lbase = examine_pointer_target(ltype);
1058 rbase = examine_pointer_target(rtype);
1060 /* they also have special treatment for pointers to void */
1061 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1062 if (ltype->ctype.as == rtype->ctype.as) {
1063 if (lbase == &void_ctype) {
1064 right = cast_to(right, ltype);
1065 goto OK;
1067 if (rbase == &void_ctype) {
1068 left = cast_to(left, rtype);
1069 goto OK;
1074 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1075 target_qualifiers(rtype),
1076 target_qualifiers(ltype));
1077 if (!typediff)
1078 goto OK;
1080 expression_error(expr, "incompatible types in comparison expression (%s)", typediff);
1081 return NULL;
1084 expr->ctype = &bool_ctype;
1085 return &bool_ctype;
1089 * NOTE! The degenerate case of "x ? : y", where we don't
1090 * have a true case, this will possibly promote "x" to the
1091 * same type as "y", and thus _change_ the conditional
1092 * test in the expression. But since promotion is "safe"
1093 * for testing, that's OK.
1095 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1097 struct expression **true;
1098 struct symbol *ctype, *ltype, *rtype, *lbase, *rbase;
1099 int lclass, rclass;
1100 const char * typediff;
1101 int qual;
1103 if (!evaluate_conditional(expr->conditional, 0))
1104 return NULL;
1105 if (!evaluate_expression(expr->cond_false))
1106 return NULL;
1108 ctype = degenerate(expr->conditional);
1109 rtype = degenerate(expr->cond_false);
1111 true = &expr->conditional;
1112 ltype = ctype;
1113 if (expr->cond_true) {
1114 if (!evaluate_expression(expr->cond_true))
1115 return NULL;
1116 ltype = degenerate(expr->cond_true);
1117 true = &expr->cond_true;
1120 if (expr->flags) {
1121 int flags = expr->conditional->flags & Int_const_expr;
1122 flags &= (*true)->flags & expr->cond_false->flags;
1123 if (!flags)
1124 expr->flags = 0;
1127 lclass = classify_type(ltype, &ltype);
1128 rclass = classify_type(rtype, &rtype);
1129 if (lclass & rclass & TYPE_NUM) {
1130 ctype = usual_conversions('?', *true, expr->cond_false,
1131 lclass, rclass, ltype, rtype);
1132 *true = cast_to(*true, ctype);
1133 expr->cond_false = cast_to(expr->cond_false, ctype);
1134 goto out;
1137 if ((lclass | rclass) & TYPE_PTR) {
1138 int is_null1 = is_null_pointer_constant(*true);
1139 int is_null2 = is_null_pointer_constant(expr->cond_false);
1141 if (is_null1 && is_null2) {
1142 *true = cast_to(*true, &ptr_ctype);
1143 expr->cond_false = cast_to(expr->cond_false, &ptr_ctype);
1144 ctype = &ptr_ctype;
1145 goto out;
1147 if (is_null1 && (rclass & TYPE_PTR)) {
1148 if (is_null1 == 2)
1149 bad_null(*true);
1150 *true = cast_to(*true, rtype);
1151 ctype = rtype;
1152 goto out;
1154 if (is_null2 && (lclass & TYPE_PTR)) {
1155 if (is_null2 == 2)
1156 bad_null(expr->cond_false);
1157 expr->cond_false = cast_to(expr->cond_false, ltype);
1158 ctype = ltype;
1159 goto out;
1161 if (!(lclass & rclass & TYPE_PTR)) {
1162 typediff = "different types";
1163 goto Err;
1165 /* OK, it's pointer on pointer */
1166 if (ltype->ctype.as != rtype->ctype.as) {
1167 typediff = "different address spaces";
1168 goto Err;
1171 /* need to be lazier here */
1172 lbase = examine_pointer_target(ltype);
1173 rbase = examine_pointer_target(rtype);
1174 qual = target_qualifiers(ltype) | target_qualifiers(rtype);
1176 if (lbase == &void_ctype) {
1177 /* XXX: pointers to function should warn here */
1178 ctype = ltype;
1179 goto Qual;
1182 if (rbase == &void_ctype) {
1183 /* XXX: pointers to function should warn here */
1184 ctype = rtype;
1185 goto Qual;
1187 /* XXX: that should be pointer to composite */
1188 ctype = ltype;
1189 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1190 qual, qual);
1191 if (!typediff)
1192 goto Qual;
1193 goto Err;
1196 /* void on void, struct on same struct, union on same union */
1197 if (ltype == rtype) {
1198 ctype = ltype;
1199 goto out;
1201 typediff = "different base types";
1203 Err:
1204 expression_error(expr, "incompatible types in conditional expression (%s)", typediff);
1205 return NULL;
1207 out:
1208 expr->ctype = ctype;
1209 return ctype;
1211 Qual:
1212 if (qual & ~ctype->ctype.modifiers) {
1213 struct symbol *sym = alloc_symbol(ctype->pos, SYM_PTR);
1214 *sym = *ctype;
1215 sym->ctype.modifiers |= qual;
1216 ctype = sym;
1218 *true = cast_to(*true, ctype);
1219 expr->cond_false = cast_to(expr->cond_false, ctype);
1220 goto out;
1223 /* FP assignments can not do modulo or bit operations */
1224 static int compatible_float_op(int op)
1226 return op == SPECIAL_ADD_ASSIGN ||
1227 op == SPECIAL_SUB_ASSIGN ||
1228 op == SPECIAL_MUL_ASSIGN ||
1229 op == SPECIAL_DIV_ASSIGN;
1232 static int evaluate_assign_op(struct expression *expr)
1234 struct symbol *target = expr->left->ctype;
1235 struct symbol *source = expr->right->ctype;
1236 struct symbol *t, *s;
1237 int tclass = classify_type(target, &t);
1238 int sclass = classify_type(source, &s);
1239 int op = expr->op;
1241 if (tclass & sclass & TYPE_NUM) {
1242 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1243 expression_error(expr, "invalid assignment");
1244 return 0;
1246 if (tclass & TYPE_RESTRICT) {
1247 if (!restricted_binop(op, t)) {
1248 expression_error(expr, "bad restricted assignment");
1249 return 0;
1251 /* allowed assignments unfoul */
1252 if (sclass & TYPE_FOULED && s->ctype.base_type == t)
1253 goto Cast;
1254 if (!restricted_value(expr->right, t))
1255 return 1;
1256 } else if (!(sclass & TYPE_RESTRICT))
1257 goto Cast;
1258 /* source and target would better be identical restricted */
1259 if (t == s)
1260 return 1;
1261 warning(expr->pos, "invalid restricted assignment");
1262 expr->right = cast_to(expr->right, target);
1263 return 0;
1265 if (tclass & TYPE_PTR && is_int(sclass)) {
1266 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1267 unrestrict(expr->right, sclass, &s);
1268 evaluate_ptr_add(expr, target, s);
1269 return 1;
1271 expression_error(expr, "invalid pointer assignment");
1272 return 0;
1275 expression_error(expr, "invalid assignment");
1276 return 0;
1278 Cast:
1279 expr->right = cast_to(expr->right, target);
1280 return 1;
1283 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1284 struct expression **rp, const char *where)
1286 const char *typediff;
1287 struct symbol *source = degenerate(*rp);
1288 struct symbol *t, *s;
1289 int tclass = classify_type(target, &t);
1290 int sclass = classify_type(source, &s);
1292 if (tclass & sclass & TYPE_NUM) {
1293 if (tclass & TYPE_RESTRICT) {
1294 /* allowed assignments unfoul */
1295 if (sclass & TYPE_FOULED && s->ctype.base_type == t)
1296 goto Cast;
1297 if (!restricted_value(*rp, target))
1298 return 1;
1299 if (s == t)
1300 return 1;
1301 } else if (!(sclass & TYPE_RESTRICT))
1302 goto Cast;
1303 typediff = "different base types";
1304 goto Err;
1307 if (tclass == TYPE_PTR) {
1308 unsigned long mod1, mod2;
1309 struct symbol *b1, *b2;
1310 // NULL pointer is always OK
1311 int is_null = is_null_pointer_constant(*rp);
1312 if (is_null) {
1313 if (is_null == 2)
1314 bad_null(*rp);
1315 goto Cast;
1317 if (!(sclass & TYPE_PTR)) {
1318 typediff = "different base types";
1319 goto Err;
1321 b1 = examine_pointer_target(t);
1322 b2 = examine_pointer_target(s);
1323 mod1 = target_qualifiers(t);
1324 mod2 = target_qualifiers(s);
1325 if (b1 == &void_ctype || b2 == &void_ctype) {
1327 * assignments to/from void * are OK, provided that
1328 * we do not remove qualifiers from pointed to [C]
1329 * or mix address spaces [sparse].
1331 if (t->ctype.as != s->ctype.as) {
1332 typediff = "different address spaces";
1333 goto Err;
1335 if (mod2 & ~mod1) {
1336 typediff = "different modifiers";
1337 goto Err;
1339 goto Cast;
1341 /* It's OK if the target is more volatile or const than the source */
1342 typediff = type_difference(&t->ctype, &s->ctype, 0, mod1);
1343 if (typediff)
1344 goto Err;
1345 return 1;
1348 if ((tclass & TYPE_COMPOUND) && s == t)
1349 return 1;
1351 if (tclass & TYPE_NUM) {
1352 /* XXX: need to turn into comparison with NULL */
1353 if (t == &bool_ctype && (sclass & TYPE_PTR))
1354 goto Cast;
1355 typediff = "different base types";
1356 goto Err;
1358 typediff = "invalid types";
1360 Err:
1361 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1362 info(expr->pos, " expected %s", show_typename(target));
1363 info(expr->pos, " got %s", show_typename(source));
1364 *rp = cast_to(*rp, target);
1365 return 0;
1366 Cast:
1367 *rp = cast_to(*rp, target);
1368 return 1;
1371 static void mark_assigned(struct expression *expr)
1373 struct symbol *sym;
1375 if (!expr)
1376 return;
1377 switch (expr->type) {
1378 case EXPR_SYMBOL:
1379 sym = expr->symbol;
1380 if (!sym)
1381 return;
1382 if (sym->type != SYM_NODE)
1383 return;
1384 sym->ctype.modifiers |= MOD_ASSIGNED;
1385 return;
1387 case EXPR_BINOP:
1388 mark_assigned(expr->left);
1389 mark_assigned(expr->right);
1390 return;
1391 case EXPR_CAST:
1392 case EXPR_FORCE_CAST:
1393 mark_assigned(expr->cast_expression);
1394 return;
1395 case EXPR_SLICE:
1396 mark_assigned(expr->base);
1397 return;
1398 default:
1399 /* Hmm? */
1400 return;
1404 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1406 if (type->ctype.modifiers & MOD_CONST)
1407 expression_error(left, "assignment to const expression");
1409 /* We know left is an lvalue, so it's a "preop-*" */
1410 mark_assigned(left->unop);
1413 static struct symbol *evaluate_assignment(struct expression *expr)
1415 struct expression *left = expr->left;
1416 struct expression *where = expr;
1417 struct symbol *ltype;
1419 if (!lvalue_expression(left)) {
1420 expression_error(expr, "not an lvalue");
1421 return NULL;
1424 ltype = left->ctype;
1426 if (expr->op != '=') {
1427 if (!evaluate_assign_op(expr))
1428 return NULL;
1429 } else {
1430 if (!compatible_assignment_types(where, ltype, &expr->right, "assignment"))
1431 return NULL;
1434 evaluate_assign_to(left, ltype);
1436 expr->ctype = ltype;
1437 return ltype;
1440 static void examine_fn_arguments(struct symbol *fn)
1442 struct symbol *s;
1444 FOR_EACH_PTR(fn->arguments, s) {
1445 struct symbol *arg = evaluate_symbol(s);
1446 /* Array/function arguments silently degenerate into pointers */
1447 if (arg) {
1448 struct symbol *ptr;
1449 switch(arg->type) {
1450 case SYM_ARRAY:
1451 case SYM_FN:
1452 ptr = alloc_symbol(s->pos, SYM_PTR);
1453 if (arg->type == SYM_ARRAY)
1454 ptr->ctype = arg->ctype;
1455 else
1456 ptr->ctype.base_type = arg;
1457 ptr->ctype.as |= s->ctype.as;
1458 ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1460 s->ctype.base_type = ptr;
1461 s->ctype.as = 0;
1462 s->ctype.modifiers &= ~MOD_PTRINHERIT;
1463 s->bit_size = 0;
1464 s->examined = 0;
1465 examine_symbol_type(s);
1466 break;
1467 default:
1468 /* nothing */
1469 break;
1472 } END_FOR_EACH_PTR(s);
1475 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1477 /* Take the modifiers of the pointer, and apply them to the member */
1478 mod |= sym->ctype.modifiers;
1479 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1480 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1481 *newsym = *sym;
1482 newsym->ctype.as = as;
1483 newsym->ctype.modifiers = mod;
1484 sym = newsym;
1486 return sym;
1489 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1491 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1492 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1494 node->ctype.base_type = ptr;
1495 ptr->bit_size = bits_in_pointer;
1496 ptr->ctype.alignment = pointer_alignment;
1498 node->bit_size = bits_in_pointer;
1499 node->ctype.alignment = pointer_alignment;
1501 access_symbol(sym);
1502 if (sym->ctype.modifiers & MOD_REGISTER) {
1503 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1504 sym->ctype.modifiers &= ~MOD_REGISTER;
1506 if (sym->type == SYM_NODE) {
1507 ptr->ctype.as |= sym->ctype.as;
1508 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1509 sym = sym->ctype.base_type;
1511 if (degenerate && sym->type == SYM_ARRAY) {
1512 ptr->ctype.as |= sym->ctype.as;
1513 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1514 sym = sym->ctype.base_type;
1516 ptr->ctype.base_type = sym;
1518 return node;
1521 /* Arrays degenerate into pointers on pointer arithmetic */
1522 static struct symbol *degenerate(struct expression *expr)
1524 struct symbol *ctype, *base;
1526 if (!expr)
1527 return NULL;
1528 ctype = expr->ctype;
1529 if (!ctype)
1530 return NULL;
1531 base = examine_symbol_type(ctype);
1532 if (ctype->type == SYM_NODE)
1533 base = ctype->ctype.base_type;
1535 * Arrays degenerate into pointers to the entries, while
1536 * functions degenerate into pointers to themselves.
1537 * If array was part of non-lvalue compound, we create a copy
1538 * of that compound first and then act as if we were dealing with
1539 * the corresponding field in there.
1541 switch (base->type) {
1542 case SYM_ARRAY:
1543 if (expr->type == EXPR_SLICE) {
1544 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1545 struct expression *e0, *e1, *e2, *e3, *e4;
1547 a->ctype.base_type = expr->base->ctype;
1548 a->bit_size = expr->base->ctype->bit_size;
1549 a->array_size = expr->base->ctype->array_size;
1551 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1552 e0->symbol = a;
1553 e0->ctype = &lazy_ptr_ctype;
1555 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1556 e1->unop = e0;
1557 e1->op = '*';
1558 e1->ctype = expr->base->ctype; /* XXX */
1560 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1561 e2->left = e1;
1562 e2->right = expr->base;
1563 e2->op = '=';
1564 e2->ctype = expr->base->ctype;
1566 if (expr->r_bitpos) {
1567 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1568 e3->op = '+';
1569 e3->left = e0;
1570 e3->right = alloc_const_expression(expr->pos,
1571 expr->r_bitpos >> 3);
1572 e3->ctype = &lazy_ptr_ctype;
1573 } else {
1574 e3 = e0;
1577 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1578 e4->left = e2;
1579 e4->right = e3;
1580 e4->ctype = &lazy_ptr_ctype;
1582 expr->unop = e4;
1583 expr->type = EXPR_PREOP;
1584 expr->op = '*';
1586 case SYM_FN:
1587 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1588 expression_error(expr, "strange non-value function or array");
1589 return &bad_ctype;
1591 *expr = *expr->unop;
1592 ctype = create_pointer(expr, ctype, 1);
1593 expr->ctype = ctype;
1594 default:
1595 /* nothing */;
1597 return ctype;
1600 static struct symbol *evaluate_addressof(struct expression *expr)
1602 struct expression *op = expr->unop;
1603 struct symbol *ctype;
1605 if (op->op != '*' || op->type != EXPR_PREOP) {
1606 expression_error(expr, "not addressable");
1607 return NULL;
1609 ctype = op->ctype;
1610 *expr = *op->unop;
1611 expr->flags = 0;
1613 if (expr->type == EXPR_SYMBOL) {
1614 struct symbol *sym = expr->symbol;
1615 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1619 * symbol expression evaluation is lazy about the type
1620 * of the sub-expression, so we may have to generate
1621 * the type here if so..
1623 if (expr->ctype == &lazy_ptr_ctype) {
1624 ctype = create_pointer(expr, ctype, 0);
1625 expr->ctype = ctype;
1627 return expr->ctype;
1631 static struct symbol *evaluate_dereference(struct expression *expr)
1633 struct expression *op = expr->unop;
1634 struct symbol *ctype = op->ctype, *node, *target;
1636 /* Simplify: *&(expr) => (expr) */
1637 if (op->type == EXPR_PREOP && op->op == '&') {
1638 *expr = *op->unop;
1639 expr->flags = 0;
1640 return expr->ctype;
1643 /* Dereferencing a node drops all the node information. */
1644 if (ctype->type == SYM_NODE)
1645 ctype = ctype->ctype.base_type;
1647 node = alloc_symbol(expr->pos, SYM_NODE);
1648 target = ctype->ctype.base_type;
1650 switch (ctype->type) {
1651 default:
1652 expression_error(expr, "cannot dereference this type");
1653 return NULL;
1654 case SYM_PTR:
1655 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1656 merge_type(node, ctype);
1657 break;
1659 case SYM_ARRAY:
1660 if (!lvalue_expression(op)) {
1661 expression_error(op, "non-lvalue array??");
1662 return NULL;
1665 /* Do the implied "addressof" on the array */
1666 *op = *op->unop;
1669 * When an array is dereferenced, we need to pick
1670 * up the attributes of the original node too..
1672 merge_type(node, op->ctype);
1673 merge_type(node, ctype);
1674 break;
1677 node->bit_size = target->bit_size;
1678 node->array_size = target->array_size;
1680 expr->ctype = node;
1681 return node;
1685 * Unary post-ops: x++ and x--
1687 static struct symbol *evaluate_postop(struct expression *expr)
1689 struct expression *op = expr->unop;
1690 struct symbol *ctype = op->ctype;
1692 if (!lvalue_expression(expr->unop)) {
1693 expression_error(expr, "need lvalue expression for ++/--");
1694 return NULL;
1696 if (is_restricted_type(ctype) && restricted_unop(expr->op, &ctype)) {
1697 expression_error(expr, "bad operation on restricted");
1698 return NULL;
1699 } else if (is_fouled_type(ctype) && restricted_unop(expr->op, &ctype)) {
1700 expression_error(expr, "bad operation on restricted");
1701 return NULL;
1704 evaluate_assign_to(op, ctype);
1706 expr->ctype = ctype;
1707 expr->op_value = 1;
1708 if (is_ptr_type(ctype))
1709 expr->op_value = ptr_object_size(ctype) >> 3;
1711 return ctype;
1714 static struct symbol *evaluate_sign(struct expression *expr)
1716 struct symbol *ctype = expr->unop->ctype;
1717 int class = classify_type(ctype, &ctype);
1718 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1719 expr->flags = 0;
1720 /* should be an arithmetic type */
1721 if (!(class & TYPE_NUM))
1722 return bad_expr_type(expr);
1723 if (!(class & (TYPE_FLOAT|TYPE_RESTRICT))) {
1724 struct symbol *rtype = integer_promotion(ctype);
1725 expr->unop = cast_to(expr->unop, rtype);
1726 ctype = rtype;
1727 } else if ((class & TYPE_FLOAT) && expr->op != '~') {
1728 /* no conversions needed */
1729 } else if ((class & TYPE_RESTRICT) && !restricted_unop(expr->op, &ctype)) {
1730 /* no conversions needed */
1731 } else {
1732 return bad_expr_type(expr);
1734 if (expr->op == '+')
1735 *expr = *expr->unop;
1736 expr->ctype = ctype;
1737 return ctype;
1740 static struct symbol *evaluate_preop(struct expression *expr)
1742 struct symbol *ctype = expr->unop->ctype;
1744 switch (expr->op) {
1745 case '(':
1746 *expr = *expr->unop;
1747 return ctype;
1749 case '+':
1750 case '-':
1751 case '~':
1752 return evaluate_sign(expr);
1754 case '*':
1755 return evaluate_dereference(expr);
1757 case '&':
1758 return evaluate_addressof(expr);
1760 case SPECIAL_INCREMENT:
1761 case SPECIAL_DECREMENT:
1763 * From a type evaluation standpoint the preops are
1764 * the same as the postops
1766 return evaluate_postop(expr);
1768 case '!':
1769 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1770 expr->flags = 0;
1771 if (is_safe_type(ctype))
1772 warning(expr->pos, "testing a 'safe expression'");
1773 if (is_float_type(ctype)) {
1774 struct expression *arg = expr->unop;
1775 expr->type = EXPR_BINOP;
1776 expr->op = SPECIAL_EQUAL;
1777 expr->left = arg;
1778 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1779 expr->right->ctype = ctype;
1780 expr->right->fvalue = 0;
1781 } else if (is_fouled_type(ctype)) {
1782 warning(expr->pos, "restricted degrades to integer");
1784 ctype = &bool_ctype;
1785 break;
1787 default:
1788 break;
1790 expr->ctype = ctype;
1791 return &bool_ctype;
1794 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1796 struct ptr_list *head = (struct ptr_list *)_list;
1797 struct ptr_list *list = head;
1799 if (!head)
1800 return NULL;
1801 do {
1802 int i;
1803 for (i = 0; i < list->nr; i++) {
1804 struct symbol *sym = (struct symbol *) list->list[i];
1805 if (sym->ident) {
1806 if (sym->ident != ident)
1807 continue;
1808 *offset = sym->offset;
1809 return sym;
1810 } else {
1811 struct symbol *ctype = sym->ctype.base_type;
1812 struct symbol *sub;
1813 if (!ctype)
1814 continue;
1815 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1816 continue;
1817 sub = find_identifier(ident, ctype->symbol_list, offset);
1818 if (!sub)
1819 continue;
1820 *offset += sym->offset;
1821 return sub;
1824 } while ((list = list->next) != head);
1825 return NULL;
1828 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1830 struct expression *add;
1833 * Create a new add-expression
1835 * NOTE! Even if we just add zero, we need a new node
1836 * for the member pointer, since it has a different
1837 * type than the original pointer. We could make that
1838 * be just a cast, but the fact is, a node is a node,
1839 * so we might as well just do the "add zero" here.
1841 add = alloc_expression(expr->pos, EXPR_BINOP);
1842 add->op = '+';
1843 add->left = expr;
1844 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1845 add->right->ctype = &int_ctype;
1846 add->right->value = offset;
1849 * The ctype of the pointer will be lazily evaluated if
1850 * we ever take the address of this member dereference..
1852 add->ctype = &lazy_ptr_ctype;
1853 return add;
1856 /* structure/union dereference */
1857 static struct symbol *evaluate_member_dereference(struct expression *expr)
1859 int offset;
1860 struct symbol *ctype, *member;
1861 struct expression *deref = expr->deref, *add;
1862 struct ident *ident = expr->member;
1863 unsigned int mod;
1864 int address_space;
1866 if (!evaluate_expression(deref))
1867 return NULL;
1868 if (!ident) {
1869 expression_error(expr, "bad member name");
1870 return NULL;
1873 ctype = deref->ctype;
1874 address_space = ctype->ctype.as;
1875 mod = ctype->ctype.modifiers;
1876 if (ctype->type == SYM_NODE) {
1877 ctype = ctype->ctype.base_type;
1878 address_space |= ctype->ctype.as;
1879 mod |= ctype->ctype.modifiers;
1881 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1882 expression_error(expr, "expected structure or union");
1883 return NULL;
1885 examine_symbol_type(ctype);
1886 offset = 0;
1887 member = find_identifier(ident, ctype->symbol_list, &offset);
1888 if (!member) {
1889 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1890 const char *name = "<unnamed>";
1891 int namelen = 9;
1892 if (ctype->ident) {
1893 name = ctype->ident->name;
1894 namelen = ctype->ident->len;
1896 if (ctype->symbol_list)
1897 expression_error(expr, "no member '%s' in %s %.*s",
1898 show_ident(ident), type, namelen, name);
1899 else
1900 expression_error(expr, "using member '%s' in "
1901 "incomplete %s %.*s", show_ident(ident),
1902 type, namelen, name);
1903 return NULL;
1907 * The member needs to take on the address space and modifiers of
1908 * the "parent" type.
1910 member = convert_to_as_mod(member, address_space, mod);
1911 ctype = get_base_type(member);
1913 if (!lvalue_expression(deref)) {
1914 if (deref->type != EXPR_SLICE) {
1915 expr->base = deref;
1916 expr->r_bitpos = 0;
1917 } else {
1918 expr->base = deref->base;
1919 expr->r_bitpos = deref->r_bitpos;
1921 expr->r_bitpos += offset << 3;
1922 expr->type = EXPR_SLICE;
1923 expr->r_nrbits = member->bit_size;
1924 expr->r_bitpos += member->bit_offset;
1925 expr->ctype = member;
1926 return member;
1929 deref = deref->unop;
1930 expr->deref = deref;
1932 add = evaluate_offset(deref, offset);
1933 expr->type = EXPR_PREOP;
1934 expr->op = '*';
1935 expr->unop = add;
1937 expr->ctype = member;
1938 return member;
1941 static int is_promoted(struct expression *expr)
1943 while (1) {
1944 switch (expr->type) {
1945 case EXPR_BINOP:
1946 case EXPR_SELECT:
1947 case EXPR_CONDITIONAL:
1948 return 1;
1949 case EXPR_COMMA:
1950 expr = expr->right;
1951 continue;
1952 case EXPR_PREOP:
1953 switch (expr->op) {
1954 case '(':
1955 expr = expr->unop;
1956 continue;
1957 case '+':
1958 case '-':
1959 case '~':
1960 return 1;
1961 default:
1962 return 0;
1964 default:
1965 return 0;
1971 static struct symbol *evaluate_cast(struct expression *);
1973 static struct symbol *evaluate_type_information(struct expression *expr)
1975 struct symbol *sym = expr->cast_type;
1976 if (!sym) {
1977 sym = evaluate_expression(expr->cast_expression);
1978 if (!sym)
1979 return NULL;
1981 * Expressions of restricted types will possibly get
1982 * promoted - check that here
1984 if (is_restricted_type(sym)) {
1985 if (sym->bit_size < bits_in_int && is_promoted(expr))
1986 sym = &int_ctype;
1987 } else if (is_fouled_type(sym)) {
1988 sym = &int_ctype;
1991 examine_symbol_type(sym);
1992 if (is_bitfield_type(sym)) {
1993 expression_error(expr, "trying to examine bitfield type");
1994 return NULL;
1996 return sym;
1999 static struct symbol *evaluate_sizeof(struct expression *expr)
2001 struct symbol *type;
2002 int size;
2004 type = evaluate_type_information(expr);
2005 if (!type)
2006 return NULL;
2008 size = type->bit_size;
2009 if ((size < 0) || (size & 7))
2010 expression_error(expr, "cannot size expression");
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_ptrsizeof(struct expression *expr)
2020 struct symbol *type;
2021 int size;
2023 type = evaluate_type_information(expr);
2024 if (!type)
2025 return NULL;
2027 if (type->type == SYM_NODE)
2028 type = type->ctype.base_type;
2029 if (!type)
2030 return NULL;
2031 switch (type->type) {
2032 case SYM_ARRAY:
2033 break;
2034 case SYM_PTR:
2035 type = get_base_type(type);
2036 if (type)
2037 break;
2038 default:
2039 expression_error(expr, "expected pointer expression");
2040 return NULL;
2042 size = type->bit_size;
2043 if (size & 7)
2044 size = 0;
2045 expr->type = EXPR_VALUE;
2046 expr->value = size >> 3;
2047 expr->taint = 0;
2048 expr->ctype = size_t_ctype;
2049 return size_t_ctype;
2052 static struct symbol *evaluate_alignof(struct expression *expr)
2054 struct symbol *type;
2056 type = evaluate_type_information(expr);
2057 if (!type)
2058 return NULL;
2060 expr->type = EXPR_VALUE;
2061 expr->value = type->ctype.alignment;
2062 expr->taint = 0;
2063 expr->ctype = size_t_ctype;
2064 return size_t_ctype;
2067 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
2069 struct expression *expr;
2070 struct symbol_list *argument_types = fn->arguments;
2071 struct symbol *argtype;
2072 int i = 1;
2074 PREPARE_PTR_LIST(argument_types, argtype);
2075 FOR_EACH_PTR (head, expr) {
2076 struct expression **p = THIS_ADDRESS(expr);
2077 struct symbol *ctype, *target;
2078 ctype = evaluate_expression(expr);
2080 if (!ctype)
2081 return 0;
2083 target = argtype;
2084 if (!target) {
2085 struct symbol *type;
2086 int class = classify_type(ctype, &type);
2087 if (is_int(class)) {
2088 *p = cast_to(expr, integer_promotion(type));
2089 } else if (class & TYPE_FLOAT) {
2090 unsigned long mod = type->ctype.modifiers;
2091 if (!(mod & (MOD_LONG|MOD_LONGLONG)))
2092 *p = cast_to(expr, &double_ctype);
2093 } else if (class & TYPE_PTR) {
2094 if (expr->ctype == &null_ctype)
2095 *p = cast_to(expr, &ptr_ctype);
2096 else
2097 degenerate(expr);
2099 } else {
2100 static char where[30];
2101 examine_symbol_type(target);
2102 sprintf(where, "argument %d", i);
2103 compatible_assignment_types(expr, target, p, where);
2106 i++;
2107 NEXT_PTR_LIST(argtype);
2108 } END_FOR_EACH_PTR(expr);
2109 FINISH_PTR_LIST(argtype);
2110 return 1;
2113 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
2115 struct symbol *sym;
2117 FOR_EACH_PTR(ctype->symbol_list, sym) {
2118 if (sym->ident == ident)
2119 return sym;
2120 } END_FOR_EACH_PTR(sym);
2121 return NULL;
2124 static void convert_index(struct expression *e)
2126 struct expression *child = e->idx_expression;
2127 unsigned from = e->idx_from;
2128 unsigned to = e->idx_to + 1;
2129 e->type = EXPR_POS;
2130 e->init_offset = from * (e->ctype->bit_size>>3);
2131 e->init_nr = to - from;
2132 e->init_expr = child;
2135 static void convert_ident(struct expression *e)
2137 struct expression *child = e->ident_expression;
2138 struct symbol *sym = e->field;
2139 e->type = EXPR_POS;
2140 e->init_offset = sym->offset;
2141 e->init_nr = 1;
2142 e->init_expr = child;
2145 static void convert_designators(struct expression *e)
2147 while (e) {
2148 if (e->type == EXPR_INDEX)
2149 convert_index(e);
2150 else if (e->type == EXPR_IDENTIFIER)
2151 convert_ident(e);
2152 else
2153 break;
2154 e = e->init_expr;
2158 static void excess(struct expression *e, const char *s)
2160 warning(e->pos, "excessive elements in %s initializer", s);
2164 * implicit designator for the first element
2166 static struct expression *first_subobject(struct symbol *ctype, int class,
2167 struct expression **v)
2169 struct expression *e = *v, *new;
2171 if (ctype->type == SYM_NODE)
2172 ctype = ctype->ctype.base_type;
2174 if (class & TYPE_PTR) { /* array */
2175 if (!ctype->bit_size)
2176 return NULL;
2177 new = alloc_expression(e->pos, EXPR_INDEX);
2178 new->idx_expression = e;
2179 new->ctype = ctype->ctype.base_type;
2180 } else {
2181 struct symbol *field, *p;
2182 PREPARE_PTR_LIST(ctype->symbol_list, p);
2183 while (p && !p->ident && is_bitfield_type(p))
2184 NEXT_PTR_LIST(p);
2185 field = p;
2186 FINISH_PTR_LIST(p);
2187 if (!field)
2188 return NULL;
2189 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2190 new->ident_expression = e;
2191 new->field = new->ctype = field;
2193 *v = new;
2194 return new;
2198 * sanity-check explicit designators; return the innermost one or NULL
2199 * in case of error. Assign types.
2201 static struct expression *check_designators(struct expression *e,
2202 struct symbol *ctype)
2204 struct expression *last = NULL;
2205 const char *err;
2206 while (1) {
2207 if (ctype->type == SYM_NODE)
2208 ctype = ctype->ctype.base_type;
2209 if (e->type == EXPR_INDEX) {
2210 struct symbol *type;
2211 if (ctype->type != SYM_ARRAY) {
2212 err = "array index in non-array";
2213 break;
2215 type = ctype->ctype.base_type;
2216 if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2217 unsigned offset = e->idx_to * type->bit_size;
2218 if (offset >= ctype->bit_size) {
2219 err = "index out of bounds in";
2220 break;
2223 e->ctype = ctype = type;
2224 ctype = type;
2225 last = e;
2226 if (!e->idx_expression) {
2227 err = "invalid";
2228 break;
2230 e = e->idx_expression;
2231 } else if (e->type == EXPR_IDENTIFIER) {
2232 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2233 err = "field name not in struct or union";
2234 break;
2236 ctype = find_struct_ident(ctype, e->expr_ident);
2237 if (!ctype) {
2238 err = "unknown field name in";
2239 break;
2241 e->field = e->ctype = ctype;
2242 last = e;
2243 if (!e->ident_expression) {
2244 err = "invalid";
2245 break;
2247 e = e->ident_expression;
2248 } else if (e->type == EXPR_POS) {
2249 err = "internal front-end error: EXPR_POS in";
2250 break;
2251 } else
2252 return last;
2254 expression_error(e, "%s initializer", err);
2255 return NULL;
2259 * choose the next subobject to initialize.
2261 * Get designators for next element, switch old ones to EXPR_POS.
2262 * Return the resulting expression or NULL if we'd run out of subobjects.
2263 * The innermost designator is returned in *v. Designators in old
2264 * are assumed to be already sanity-checked.
2266 static struct expression *next_designators(struct expression *old,
2267 struct symbol *ctype,
2268 struct expression *e, struct expression **v)
2270 struct expression *new = NULL;
2272 if (!old)
2273 return NULL;
2274 if (old->type == EXPR_INDEX) {
2275 struct expression *copy;
2276 unsigned n;
2278 copy = next_designators(old->idx_expression,
2279 old->ctype, e, v);
2280 if (!copy) {
2281 n = old->idx_to + 1;
2282 if (n * old->ctype->bit_size == ctype->bit_size) {
2283 convert_index(old);
2284 return NULL;
2286 copy = e;
2287 *v = new = alloc_expression(e->pos, EXPR_INDEX);
2288 } else {
2289 n = old->idx_to;
2290 new = alloc_expression(e->pos, EXPR_INDEX);
2293 new->idx_from = new->idx_to = n;
2294 new->idx_expression = copy;
2295 new->ctype = old->ctype;
2296 convert_index(old);
2297 } else if (old->type == EXPR_IDENTIFIER) {
2298 struct expression *copy;
2299 struct symbol *field;
2301 copy = next_designators(old->ident_expression,
2302 old->ctype, e, v);
2303 if (!copy) {
2304 field = old->field->next_subobject;
2305 if (!field) {
2306 convert_ident(old);
2307 return NULL;
2309 copy = e;
2310 *v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2311 } else {
2312 field = old->field;
2313 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2316 new->field = field;
2317 new->expr_ident = field->ident;
2318 new->ident_expression = copy;
2319 new->ctype = field;
2320 convert_ident(old);
2322 return new;
2325 static int handle_simple_initializer(struct expression **ep, int nested,
2326 int class, struct symbol *ctype);
2329 * deal with traversing subobjects [6.7.8(17,18,20)]
2331 static void handle_list_initializer(struct expression *expr,
2332 int class, struct symbol *ctype)
2334 struct expression *e, *last = NULL, *top = NULL, *next;
2335 int jumped = 0;
2337 FOR_EACH_PTR(expr->expr_list, e) {
2338 struct expression **v;
2339 struct symbol *type;
2340 int lclass;
2342 if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2343 if (!top) {
2344 top = e;
2345 last = first_subobject(ctype, class, &top);
2346 } else {
2347 last = next_designators(last, ctype, e, &top);
2349 if (!last) {
2350 excess(e, class & TYPE_PTR ? "array" :
2351 "struct or union");
2352 DELETE_CURRENT_PTR(e);
2353 continue;
2355 if (jumped) {
2356 warning(e->pos, "advancing past deep designator");
2357 jumped = 0;
2359 REPLACE_CURRENT_PTR(e, last);
2360 } else {
2361 next = check_designators(e, ctype);
2362 if (!next) {
2363 DELETE_CURRENT_PTR(e);
2364 continue;
2366 top = next;
2367 /* deeper than one designator? */
2368 jumped = top != e;
2369 convert_designators(last);
2370 last = e;
2373 found:
2374 lclass = classify_type(top->ctype, &type);
2375 if (top->type == EXPR_INDEX)
2376 v = &top->idx_expression;
2377 else
2378 v = &top->ident_expression;
2380 if (handle_simple_initializer(v, 1, lclass, top->ctype))
2381 continue;
2383 if (!(lclass & TYPE_COMPOUND)) {
2384 warning(e->pos, "bogus scalar initializer");
2385 DELETE_CURRENT_PTR(e);
2386 continue;
2389 next = first_subobject(type, lclass, v);
2390 if (next) {
2391 warning(e->pos, "missing braces around initializer");
2392 top = next;
2393 goto found;
2396 DELETE_CURRENT_PTR(e);
2397 excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2399 } END_FOR_EACH_PTR(e);
2401 convert_designators(last);
2402 expr->ctype = ctype;
2405 static int is_string_literal(struct expression **v)
2407 struct expression *e = *v;
2408 while (e && e->type == EXPR_PREOP && e->op == '(')
2409 e = e->unop;
2410 if (!e || e->type != EXPR_STRING)
2411 return 0;
2412 if (e != *v && Wparen_string)
2413 warning(e->pos,
2414 "array initialized from parenthesized string constant");
2415 *v = e;
2416 return 1;
2420 * We want a normal expression, possibly in one layer of braces. Warn
2421 * if the latter happens inside a list (it's legal, but likely to be
2422 * an effect of screwup). In case of anything not legal, we are definitely
2423 * having an effect of screwup, so just fail and let the caller warn.
2425 static struct expression *handle_scalar(struct expression *e, int nested)
2427 struct expression *v = NULL, *p;
2428 int count = 0;
2430 /* normal case */
2431 if (e->type != EXPR_INITIALIZER)
2432 return e;
2434 FOR_EACH_PTR(e->expr_list, p) {
2435 if (!v)
2436 v = p;
2437 count++;
2438 } END_FOR_EACH_PTR(p);
2439 if (count != 1)
2440 return NULL;
2441 switch(v->type) {
2442 case EXPR_INITIALIZER:
2443 case EXPR_INDEX:
2444 case EXPR_IDENTIFIER:
2445 return NULL;
2446 default:
2447 break;
2449 if (nested)
2450 warning(e->pos, "braces around scalar initializer");
2451 return v;
2455 * deal with the cases that don't care about subobjects:
2456 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2457 * character array <- string literal, possibly in braces [6.7.8(14)]
2458 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2459 * compound type <- initializer list in braces [6.7.8(16)]
2460 * The last one punts to handle_list_initializer() which, in turn will call
2461 * us for individual elements of the list.
2463 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2464 * the lack of support of wide char stuff in general.
2466 * One note: we need to take care not to evaluate a string literal until
2467 * we know that we *will* handle it right here. Otherwise we would screw
2468 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2469 * { "string", ...} - we need to preserve that string literal recognizable
2470 * until we dig into the inner struct.
2472 static int handle_simple_initializer(struct expression **ep, int nested,
2473 int class, struct symbol *ctype)
2475 int is_string = is_string_type(ctype);
2476 struct expression *e = *ep, *p;
2477 struct symbol *type;
2479 if (!e)
2480 return 0;
2482 /* scalar */
2483 if (!(class & TYPE_COMPOUND)) {
2484 e = handle_scalar(e, nested);
2485 if (!e)
2486 return 0;
2487 *ep = e;
2488 if (!evaluate_expression(e))
2489 return 1;
2490 compatible_assignment_types(e, ctype, ep, "initializer");
2491 return 1;
2495 * sublist; either a string, or we dig in; the latter will deal with
2496 * pathologies, so we don't need anything fancy here.
2498 if (e->type == EXPR_INITIALIZER) {
2499 if (is_string) {
2500 struct expression *v = NULL;
2501 int count = 0;
2503 FOR_EACH_PTR(e->expr_list, p) {
2504 if (!v)
2505 v = p;
2506 count++;
2507 } END_FOR_EACH_PTR(p);
2508 if (count == 1 && is_string_literal(&v)) {
2509 *ep = e = v;
2510 goto String;
2513 handle_list_initializer(e, class, ctype);
2514 return 1;
2517 /* string */
2518 if (is_string_literal(&e)) {
2519 /* either we are doing array of char, or we'll have to dig in */
2520 if (is_string) {
2521 *ep = e;
2522 goto String;
2524 return 0;
2526 /* struct or union can be initialized by compatible */
2527 if (class != TYPE_COMPOUND)
2528 return 0;
2529 type = evaluate_expression(e);
2530 if (!type)
2531 return 0;
2532 if (ctype->type == SYM_NODE)
2533 ctype = ctype->ctype.base_type;
2534 if (type->type == SYM_NODE)
2535 type = type->ctype.base_type;
2536 if (ctype == type)
2537 return 1;
2538 return 0;
2540 String:
2541 p = alloc_expression(e->pos, EXPR_STRING);
2542 *p = *e;
2543 type = evaluate_expression(p);
2544 if (ctype->bit_size != -1 &&
2545 ctype->bit_size + bits_in_char < type->bit_size) {
2546 warning(e->pos,
2547 "too long initializer-string for array of char");
2549 *ep = p;
2550 return 1;
2553 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2555 struct symbol *type;
2556 int class = classify_type(ctype, &type);
2557 if (!handle_simple_initializer(ep, 0, class, ctype))
2558 expression_error(*ep, "invalid initializer");
2561 static struct symbol *evaluate_cast(struct expression *expr)
2563 struct expression *target = expr->cast_expression;
2564 struct symbol *ctype;
2565 struct symbol *t1, *t2;
2566 int class1, class2;
2567 int as1 = 0, as2 = 0;
2569 if (!target)
2570 return NULL;
2573 * Special case: a cast can be followed by an
2574 * initializer, in which case we need to pass
2575 * the type value down to that initializer rather
2576 * than trying to evaluate it as an expression
2578 * A more complex case is when the initializer is
2579 * dereferenced as part of a post-fix expression.
2580 * We need to produce an expression that can be dereferenced.
2582 if (target->type == EXPR_INITIALIZER) {
2583 struct symbol *sym = expr->cast_type;
2584 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2586 sym->initializer = target;
2587 evaluate_symbol(sym);
2589 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2590 addr->symbol = sym;
2592 expr->type = EXPR_PREOP;
2593 expr->op = '*';
2594 expr->unop = addr;
2595 expr->ctype = sym;
2597 return sym;
2600 ctype = examine_symbol_type(expr->cast_type);
2601 expr->ctype = ctype;
2602 expr->cast_type = ctype;
2604 evaluate_expression(target);
2605 degenerate(target);
2607 class1 = classify_type(ctype, &t1);
2609 /* cast to non-integer type -> not an integer constant expression */
2610 if (!is_int(class1))
2611 expr->flags = 0;
2612 /* if argument turns out to be not an integer constant expression *and*
2613 it was not a floating literal to start with -> too bad */
2614 else if (expr->flags == Int_const_expr &&
2615 !(target->flags & Int_const_expr))
2616 expr->flags = 0;
2618 * You can always throw a value away by casting to
2619 * "void" - that's an implicit "force". Note that
2620 * the same is _not_ true of "void *".
2622 if (t1 == &void_ctype)
2623 goto out;
2625 if (class1 & TYPE_COMPOUND)
2626 warning(expr->pos, "cast to non-scalar");
2628 t2 = target->ctype;
2629 if (!t2) {
2630 expression_error(expr, "cast from unknown type");
2631 goto out;
2633 class2 = classify_type(t2, &t2);
2635 if (class2 & TYPE_COMPOUND)
2636 warning(expr->pos, "cast from non-scalar");
2638 if (expr->type == EXPR_FORCE_CAST)
2639 goto out;
2641 /* allowed cast unfouls */
2642 if (class2 & TYPE_FOULED)
2643 t2 = t2->ctype.base_type;
2645 if (t1 != t2) {
2646 if (class1 & TYPE_RESTRICT)
2647 warning(expr->pos, "cast to restricted type");
2648 if (class2 & TYPE_RESTRICT)
2649 warning(expr->pos, "cast from restricted type");
2652 if (t1 == &ulong_ctype)
2653 as1 = -1;
2654 else if (class1 == TYPE_PTR) {
2655 examine_pointer_target(t1);
2656 as1 = t1->ctype.as;
2659 if (t2 == &ulong_ctype)
2660 as2 = -1;
2661 else if (class2 == TYPE_PTR) {
2662 examine_pointer_target(t2);
2663 as2 = t2->ctype.as;
2666 if (!as1 && as2 > 0)
2667 warning(expr->pos, "cast removes address space of expression");
2668 if (as1 > 0 && as2 > 0 && as1 != as2)
2669 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2670 if (as1 > 0 && !as2 &&
2671 !is_null_pointer_constant(target) && Wcast_to_address_space)
2672 warning(expr->pos,
2673 "cast adds address space to expression (<asn:%d>)", as1);
2675 if (!(t1->ctype.modifiers & MOD_PTRINHERIT) && class1 == TYPE_PTR &&
2676 !as1 && (target->flags & Int_const_expr)) {
2677 if (t1->ctype.base_type == &void_ctype) {
2678 if (is_zero_constant(target)) {
2679 /* NULL */
2680 expr->type = EXPR_VALUE;
2681 expr->ctype = &null_ctype;
2682 expr->value = 0;
2683 return ctype;
2687 out:
2688 return ctype;
2692 * Evaluate a call expression with a symbol. This
2693 * should expand inline functions, and evaluate
2694 * builtins.
2696 static int evaluate_symbol_call(struct expression *expr)
2698 struct expression *fn = expr->fn;
2699 struct symbol *ctype = fn->ctype;
2701 if (fn->type != EXPR_PREOP)
2702 return 0;
2704 if (ctype->op && ctype->op->evaluate)
2705 return ctype->op->evaluate(expr);
2707 if (ctype->ctype.modifiers & MOD_INLINE) {
2708 int ret;
2709 struct symbol *curr = current_fn;
2710 current_fn = ctype->ctype.base_type;
2712 ret = inline_function(expr, ctype);
2714 /* restore the old function */
2715 current_fn = curr;
2716 return ret;
2719 return 0;
2722 static struct symbol *evaluate_call(struct expression *expr)
2724 int args, fnargs;
2725 struct symbol *ctype, *sym;
2726 struct expression *fn = expr->fn;
2727 struct expression_list *arglist = expr->args;
2729 if (!evaluate_expression(fn))
2730 return NULL;
2731 sym = ctype = fn->ctype;
2732 if (ctype->type == SYM_NODE)
2733 ctype = ctype->ctype.base_type;
2734 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
2735 ctype = get_base_type(ctype);
2737 examine_fn_arguments(ctype);
2738 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
2739 sym->op && sym->op->args) {
2740 if (!sym->op->args(expr))
2741 return NULL;
2742 } else {
2743 if (!evaluate_arguments(sym, ctype, arglist))
2744 return NULL;
2745 if (ctype->type != SYM_FN) {
2746 expression_error(expr, "not a function %s",
2747 show_ident(sym->ident));
2748 return NULL;
2750 args = expression_list_size(expr->args);
2751 fnargs = symbol_list_size(ctype->arguments);
2752 if (args < fnargs)
2753 expression_error(expr,
2754 "not enough arguments for function %s",
2755 show_ident(sym->ident));
2756 if (args > fnargs && !ctype->variadic)
2757 expression_error(expr,
2758 "too many arguments for function %s",
2759 show_ident(sym->ident));
2761 if (sym->type == SYM_NODE) {
2762 if (evaluate_symbol_call(expr))
2763 return expr->ctype;
2765 expr->ctype = ctype->ctype.base_type;
2766 return expr->ctype;
2769 static struct symbol *evaluate_offsetof(struct expression *expr)
2771 struct expression *e = expr->down;
2772 struct symbol *ctype = expr->in;
2773 int class;
2775 if (expr->op == '.') {
2776 struct symbol *field;
2777 int offset = 0;
2778 if (!ctype) {
2779 expression_error(expr, "expected structure or union");
2780 return NULL;
2782 examine_symbol_type(ctype);
2783 class = classify_type(ctype, &ctype);
2784 if (class != TYPE_COMPOUND) {
2785 expression_error(expr, "expected structure or union");
2786 return NULL;
2789 field = find_identifier(expr->ident, ctype->symbol_list, &offset);
2790 if (!field) {
2791 expression_error(expr, "unknown member");
2792 return NULL;
2794 ctype = field;
2795 expr->type = EXPR_VALUE;
2796 expr->flags = Int_const_expr;
2797 expr->value = offset;
2798 expr->taint = 0;
2799 expr->ctype = size_t_ctype;
2800 } else {
2801 if (!ctype) {
2802 expression_error(expr, "expected structure or union");
2803 return NULL;
2805 examine_symbol_type(ctype);
2806 class = classify_type(ctype, &ctype);
2807 if (class != (TYPE_COMPOUND | TYPE_PTR)) {
2808 expression_error(expr, "expected array");
2809 return NULL;
2811 ctype = ctype->ctype.base_type;
2812 if (!expr->index) {
2813 expr->type = EXPR_VALUE;
2814 expr->flags = Int_const_expr;
2815 expr->value = 0;
2816 expr->taint = 0;
2817 expr->ctype = size_t_ctype;
2818 } else {
2819 struct expression *idx = expr->index, *m;
2820 struct symbol *i_type = evaluate_expression(idx);
2821 int i_class = classify_type(i_type, &i_type);
2822 if (!is_int(i_class)) {
2823 expression_error(expr, "non-integer index");
2824 return NULL;
2826 unrestrict(idx, i_class, &i_type);
2827 idx = cast_to(idx, size_t_ctype);
2828 m = alloc_const_expression(expr->pos,
2829 ctype->bit_size >> 3);
2830 m->ctype = size_t_ctype;
2831 m->flags = Int_const_expr;
2832 expr->type = EXPR_BINOP;
2833 expr->left = idx;
2834 expr->right = m;
2835 expr->op = '*';
2836 expr->ctype = size_t_ctype;
2837 expr->flags = m->flags & idx->flags & Int_const_expr;
2840 if (e) {
2841 struct expression *copy = __alloc_expression(0);
2842 *copy = *expr;
2843 if (e->type == EXPR_OFFSETOF)
2844 e->in = ctype;
2845 if (!evaluate_expression(e))
2846 return NULL;
2847 expr->type = EXPR_BINOP;
2848 expr->flags = e->flags & copy->flags & Int_const_expr;
2849 expr->op = '+';
2850 expr->ctype = size_t_ctype;
2851 expr->left = copy;
2852 expr->right = e;
2854 return size_t_ctype;
2857 struct symbol *evaluate_expression(struct expression *expr)
2859 if (!expr)
2860 return NULL;
2861 if (expr->ctype)
2862 return expr->ctype;
2864 switch (expr->type) {
2865 case EXPR_VALUE:
2866 case EXPR_FVALUE:
2867 expression_error(expr, "value expression without a type");
2868 return NULL;
2869 case EXPR_STRING:
2870 return evaluate_string(expr);
2871 case EXPR_SYMBOL:
2872 return evaluate_symbol_expression(expr);
2873 case EXPR_BINOP:
2874 if (!evaluate_expression(expr->left))
2875 return NULL;
2876 if (!evaluate_expression(expr->right))
2877 return NULL;
2878 return evaluate_binop(expr);
2879 case EXPR_LOGICAL:
2880 return evaluate_logical(expr);
2881 case EXPR_COMMA:
2882 evaluate_expression(expr->left);
2883 if (!evaluate_expression(expr->right))
2884 return NULL;
2885 return evaluate_comma(expr);
2886 case EXPR_COMPARE:
2887 if (!evaluate_expression(expr->left))
2888 return NULL;
2889 if (!evaluate_expression(expr->right))
2890 return NULL;
2891 return evaluate_compare(expr);
2892 case EXPR_ASSIGNMENT:
2893 if (!evaluate_expression(expr->left))
2894 return NULL;
2895 if (!evaluate_expression(expr->right))
2896 return NULL;
2897 return evaluate_assignment(expr);
2898 case EXPR_PREOP:
2899 if (!evaluate_expression(expr->unop))
2900 return NULL;
2901 return evaluate_preop(expr);
2902 case EXPR_POSTOP:
2903 if (!evaluate_expression(expr->unop))
2904 return NULL;
2905 return evaluate_postop(expr);
2906 case EXPR_CAST:
2907 case EXPR_FORCE_CAST:
2908 case EXPR_IMPLIED_CAST:
2909 return evaluate_cast(expr);
2910 case EXPR_SIZEOF:
2911 return evaluate_sizeof(expr);
2912 case EXPR_PTRSIZEOF:
2913 return evaluate_ptrsizeof(expr);
2914 case EXPR_ALIGNOF:
2915 return evaluate_alignof(expr);
2916 case EXPR_DEREF:
2917 return evaluate_member_dereference(expr);
2918 case EXPR_CALL:
2919 return evaluate_call(expr);
2920 case EXPR_SELECT:
2921 case EXPR_CONDITIONAL:
2922 return evaluate_conditional_expression(expr);
2923 case EXPR_STATEMENT:
2924 expr->ctype = evaluate_statement(expr->statement);
2925 return expr->ctype;
2927 case EXPR_LABEL:
2928 expr->ctype = &ptr_ctype;
2929 return &ptr_ctype;
2931 case EXPR_TYPE:
2932 /* Evaluate the type of the symbol .. */
2933 evaluate_symbol(expr->symbol);
2934 /* .. but the type of the _expression_ is a "type" */
2935 expr->ctype = &type_ctype;
2936 return &type_ctype;
2938 case EXPR_OFFSETOF:
2939 return evaluate_offsetof(expr);
2941 /* These can not exist as stand-alone expressions */
2942 case EXPR_INITIALIZER:
2943 case EXPR_IDENTIFIER:
2944 case EXPR_INDEX:
2945 case EXPR_POS:
2946 expression_error(expr, "internal front-end error: initializer in expression");
2947 return NULL;
2948 case EXPR_SLICE:
2949 expression_error(expr, "internal front-end error: SLICE re-evaluated");
2950 return NULL;
2952 return NULL;
2955 static void check_duplicates(struct symbol *sym)
2957 int declared = 0;
2958 struct symbol *next = sym;
2960 while ((next = next->same_symbol) != NULL) {
2961 const char *typediff;
2962 evaluate_symbol(next);
2963 declared++;
2964 typediff = type_difference(&sym->ctype, &next->ctype, 0, 0);
2965 if (typediff) {
2966 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2967 show_ident(sym->ident),
2968 stream_name(next->pos.stream), next->pos.line, typediff);
2969 return;
2972 if (!declared) {
2973 unsigned long mod = sym->ctype.modifiers;
2974 if (mod & (MOD_STATIC | MOD_REGISTER))
2975 return;
2976 if (!(mod & MOD_TOPLEVEL))
2977 return;
2978 if (!Wdecl)
2979 return;
2980 if (sym->ident == &main_ident)
2981 return;
2982 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
2986 static struct symbol *evaluate_symbol(struct symbol *sym)
2988 struct symbol *base_type;
2990 if (!sym)
2991 return sym;
2992 if (sym->evaluated)
2993 return sym;
2994 sym->evaluated = 1;
2996 sym = examine_symbol_type(sym);
2997 base_type = get_base_type(sym);
2998 if (!base_type)
2999 return NULL;
3001 /* Evaluate the initializers */
3002 if (sym->initializer)
3003 evaluate_initializer(sym, &sym->initializer);
3005 /* And finally, evaluate the body of the symbol too */
3006 if (base_type->type == SYM_FN) {
3007 struct symbol *curr = current_fn;
3009 current_fn = base_type;
3011 examine_fn_arguments(base_type);
3012 if (!base_type->stmt && base_type->inline_stmt)
3013 uninline(sym);
3014 if (base_type->stmt)
3015 evaluate_statement(base_type->stmt);
3017 current_fn = curr;
3020 return base_type;
3023 void evaluate_symbol_list(struct symbol_list *list)
3025 struct symbol *sym;
3027 FOR_EACH_PTR(list, sym) {
3028 evaluate_symbol(sym);
3029 check_duplicates(sym);
3030 } END_FOR_EACH_PTR(sym);
3033 static struct symbol *evaluate_return_expression(struct statement *stmt)
3035 struct expression *expr = stmt->expression;
3036 struct symbol *fntype;
3038 evaluate_expression(expr);
3039 fntype = current_fn->ctype.base_type;
3040 if (!fntype || fntype == &void_ctype) {
3041 if (expr && expr->ctype != &void_ctype)
3042 expression_error(expr, "return expression in %s function", fntype?"void":"typeless");
3043 if (expr && Wreturn_void)
3044 warning(stmt->pos, "returning void-valued expression");
3045 return NULL;
3048 if (!expr) {
3049 sparse_error(stmt->pos, "return with no return value");
3050 return NULL;
3052 if (!expr->ctype)
3053 return NULL;
3054 compatible_assignment_types(expr, fntype, &stmt->expression, "return expression");
3055 return NULL;
3058 static void evaluate_if_statement(struct statement *stmt)
3060 if (!stmt->if_conditional)
3061 return;
3063 evaluate_conditional(stmt->if_conditional, 0);
3064 evaluate_statement(stmt->if_true);
3065 evaluate_statement(stmt->if_false);
3068 static void evaluate_iterator(struct statement *stmt)
3070 evaluate_conditional(stmt->iterator_pre_condition, 1);
3071 evaluate_conditional(stmt->iterator_post_condition,1);
3072 evaluate_statement(stmt->iterator_pre_statement);
3073 evaluate_statement(stmt->iterator_statement);
3074 evaluate_statement(stmt->iterator_post_statement);
3077 static void verify_output_constraint(struct expression *expr, const char *constraint)
3079 switch (*constraint) {
3080 case '=': /* Assignment */
3081 case '+': /* Update */
3082 break;
3083 default:
3084 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
3088 static void verify_input_constraint(struct expression *expr, const char *constraint)
3090 switch (*constraint) {
3091 case '=': /* Assignment */
3092 case '+': /* Update */
3093 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
3097 static void evaluate_asm_statement(struct statement *stmt)
3099 struct expression *expr;
3100 int state;
3102 expr = stmt->asm_string;
3103 if (!expr || expr->type != EXPR_STRING) {
3104 sparse_error(stmt->pos, "need constant string for inline asm");
3105 return;
3108 state = 0;
3109 FOR_EACH_PTR(stmt->asm_outputs, expr) {
3110 struct ident *ident;
3112 switch (state) {
3113 case 0: /* Identifier */
3114 state = 1;
3115 ident = (struct ident *)expr;
3116 continue;
3118 case 1: /* Constraint */
3119 state = 2;
3120 if (!expr || expr->type != EXPR_STRING) {
3121 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
3122 *THIS_ADDRESS(expr) = NULL;
3123 continue;
3125 verify_output_constraint(expr, expr->string->data);
3126 continue;
3128 case 2: /* Expression */
3129 state = 0;
3130 if (!evaluate_expression(expr))
3131 return;
3132 if (!lvalue_expression(expr))
3133 warning(expr->pos, "asm output is not an lvalue");
3134 evaluate_assign_to(expr, expr->ctype);
3135 continue;
3137 } END_FOR_EACH_PTR(expr);
3139 state = 0;
3140 FOR_EACH_PTR(stmt->asm_inputs, expr) {
3141 struct ident *ident;
3143 switch (state) {
3144 case 0: /* Identifier */
3145 state = 1;
3146 ident = (struct ident *)expr;
3147 continue;
3149 case 1: /* Constraint */
3150 state = 2;
3151 if (!expr || expr->type != EXPR_STRING) {
3152 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
3153 *THIS_ADDRESS(expr) = NULL;
3154 continue;
3156 verify_input_constraint(expr, expr->string->data);
3157 continue;
3159 case 2: /* Expression */
3160 state = 0;
3161 if (!evaluate_expression(expr))
3162 return;
3163 continue;
3165 } END_FOR_EACH_PTR(expr);
3167 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
3168 if (!expr) {
3169 sparse_error(stmt->pos, "bad asm output");
3170 return;
3172 if (expr->type == EXPR_STRING)
3173 continue;
3174 expression_error(expr, "asm clobber is not a string");
3175 } END_FOR_EACH_PTR(expr);
3178 static void evaluate_case_statement(struct statement *stmt)
3180 evaluate_expression(stmt->case_expression);
3181 evaluate_expression(stmt->case_to);
3182 evaluate_statement(stmt->case_statement);
3185 static void check_case_type(struct expression *switch_expr,
3186 struct expression *case_expr,
3187 struct expression **enumcase)
3189 struct symbol *switch_type, *case_type;
3190 int sclass, cclass;
3192 if (!case_expr)
3193 return;
3195 switch_type = switch_expr->ctype;
3196 case_type = evaluate_expression(case_expr);
3198 if (!switch_type || !case_type)
3199 goto Bad;
3200 if (enumcase) {
3201 if (*enumcase)
3202 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
3203 else if (is_enum_type(case_type))
3204 *enumcase = case_expr;
3207 sclass = classify_type(switch_type, &switch_type);
3208 cclass = classify_type(case_type, &case_type);
3210 /* both should be arithmetic */
3211 if (!(sclass & cclass & TYPE_NUM))
3212 goto Bad;
3214 /* neither should be floating */
3215 if ((sclass | cclass) & TYPE_FLOAT)
3216 goto Bad;
3218 /* if neither is restricted, we are OK */
3219 if (!((sclass | cclass) & TYPE_RESTRICT))
3220 return;
3222 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
3223 cclass, sclass, case_type, switch_type))
3224 warning(case_expr->pos, "restricted degrades to integer");
3226 return;
3228 Bad:
3229 expression_error(case_expr, "incompatible types for 'case' statement");
3232 static void evaluate_switch_statement(struct statement *stmt)
3234 struct symbol *sym;
3235 struct expression *enumcase = NULL;
3236 struct expression **enumcase_holder = &enumcase;
3237 struct expression *sel = stmt->switch_expression;
3239 evaluate_expression(sel);
3240 evaluate_statement(stmt->switch_statement);
3241 if (!sel)
3242 return;
3243 if (sel->ctype && is_enum_type(sel->ctype))
3244 enumcase_holder = NULL; /* Only check cases against switch */
3246 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
3247 struct statement *case_stmt = sym->stmt;
3248 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
3249 check_case_type(sel, case_stmt->case_to, enumcase_holder);
3250 } END_FOR_EACH_PTR(sym);
3253 struct symbol *evaluate_statement(struct statement *stmt)
3255 if (!stmt)
3256 return NULL;
3258 switch (stmt->type) {
3259 case STMT_DECLARATION: {
3260 struct symbol *s;
3261 FOR_EACH_PTR(stmt->declaration, s) {
3262 evaluate_symbol(s);
3263 } END_FOR_EACH_PTR(s);
3264 return NULL;
3267 case STMT_RETURN:
3268 return evaluate_return_expression(stmt);
3270 case STMT_EXPRESSION:
3271 if (!evaluate_expression(stmt->expression))
3272 return NULL;
3273 if (stmt->expression->ctype == &null_ctype)
3274 stmt->expression = cast_to(stmt->expression, &ptr_ctype);
3275 return degenerate(stmt->expression);
3277 case STMT_COMPOUND: {
3278 struct statement *s;
3279 struct symbol *type = NULL;
3281 /* Evaluate the return symbol in the compound statement */
3282 evaluate_symbol(stmt->ret);
3285 * Then, evaluate each statement, making the type of the
3286 * compound statement be the type of the last statement
3288 type = evaluate_statement(stmt->args);
3289 FOR_EACH_PTR(stmt->stmts, s) {
3290 type = evaluate_statement(s);
3291 } END_FOR_EACH_PTR(s);
3292 if (!type)
3293 type = &void_ctype;
3294 return type;
3296 case STMT_IF:
3297 evaluate_if_statement(stmt);
3298 return NULL;
3299 case STMT_ITERATOR:
3300 evaluate_iterator(stmt);
3301 return NULL;
3302 case STMT_SWITCH:
3303 evaluate_switch_statement(stmt);
3304 return NULL;
3305 case STMT_CASE:
3306 evaluate_case_statement(stmt);
3307 return NULL;
3308 case STMT_LABEL:
3309 return evaluate_statement(stmt->label_statement);
3310 case STMT_GOTO:
3311 evaluate_expression(stmt->goto_expression);
3312 return NULL;
3313 case STMT_NONE:
3314 break;
3315 case STMT_ASM:
3316 evaluate_asm_statement(stmt);
3317 return NULL;
3318 case STMT_CONTEXT:
3319 evaluate_expression(stmt->expression);
3320 return NULL;
3321 case STMT_RANGE:
3322 evaluate_expression(stmt->range_expression);
3323 evaluate_expression(stmt->range_low);
3324 evaluate_expression(stmt->range_high);
3325 return NULL;
3327 return NULL;