[PATCH] ...,array should degenerate
[smatch.git] / evaluate.c
blob5cb1b8958d4135b3a5c47eb88f88e7fef6b4406b
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 /* type should be SYM_FOULED */
448 static inline struct symbol *unfoul(struct symbol *type)
450 return type->ctype.base_type;
453 static struct symbol *restricted_binop_type(int op,
454 struct expression *left,
455 struct expression *right,
456 int lclass, int rclass,
457 struct symbol *ltype,
458 struct symbol *rtype)
460 struct symbol *ctype = NULL;
461 if (lclass & TYPE_RESTRICT) {
462 if (rclass & TYPE_RESTRICT) {
463 if (ltype == rtype) {
464 ctype = ltype;
465 } else if (lclass & TYPE_FOULED) {
466 if (unfoul(ltype) == rtype)
467 ctype = ltype;
468 } else if (rclass & TYPE_FOULED) {
469 if (unfoul(rtype) == ltype)
470 ctype = rtype;
472 } else {
473 if (!restricted_value(right, ltype))
474 ctype = ltype;
476 } else if (!restricted_value(left, rtype))
477 ctype = rtype;
479 if (ctype) {
480 switch (restricted_binop(op, ctype)) {
481 case 1:
482 if ((lclass ^ rclass) & TYPE_FOULED)
483 ctype = unfoul(ctype);
484 break;
485 case 3:
486 if (!(lclass & rclass & TYPE_FOULED))
487 break;
488 case 0:
489 ctype = NULL;
490 default:
491 break;
495 return ctype;
498 static inline void unrestrict(struct expression *expr,
499 int class, struct symbol **ctype)
501 if (class & TYPE_RESTRICT) {
502 warning(expr->pos, "restricted degrades to integer");
503 if (class & TYPE_FOULED)
504 *ctype = unfoul(*ctype);
505 *ctype = (*ctype)->ctype.base_type; /* get to arithmetic type */
509 static struct symbol *usual_conversions(int op,
510 struct expression *left,
511 struct expression *right,
512 int lclass, int rclass,
513 struct symbol *ltype,
514 struct symbol *rtype)
516 struct symbol *ctype;
518 warn_for_different_enum_types(right->pos, left->ctype, right->ctype);
520 if ((lclass | rclass) & TYPE_RESTRICT)
521 goto Restr;
523 Normal:
524 if (!(lclass & TYPE_FLOAT)) {
525 if (!(rclass & TYPE_FLOAT))
526 return bigger_int_type(ltype, rtype);
527 else
528 return rtype;
529 } else if (rclass & TYPE_FLOAT) {
530 unsigned long lmod = ltype->ctype.modifiers;
531 unsigned long rmod = rtype->ctype.modifiers;
532 if (rmod & ~lmod & (MOD_LONG | MOD_LONGLONG))
533 return rtype;
534 else
535 return ltype;
536 } else
537 return ltype;
539 Restr:
540 ctype = restricted_binop_type(op, left, right,
541 lclass, rclass, ltype, rtype);
542 if (ctype)
543 return ctype;
545 unrestrict(left, lclass, &ltype);
546 unrestrict(right, rclass, &rtype);
548 goto Normal;
551 static inline int lvalue_expression(struct expression *expr)
553 return expr->type == EXPR_PREOP && expr->op == '*';
556 static int ptr_object_size(struct symbol *ptr_type)
558 if (ptr_type->type == SYM_NODE)
559 ptr_type = ptr_type->ctype.base_type;
560 if (ptr_type->type == SYM_PTR)
561 ptr_type = get_base_type(ptr_type);
562 return ptr_type->bit_size;
565 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *ctype, struct symbol *itype)
567 struct expression *index = expr->right;
568 int multiply;
569 int bit_size;
571 if (ctype == &null_ctype)
572 ctype = &ptr_ctype;
574 examine_symbol_type(ctype);
576 if (!ctype->ctype.base_type) {
577 expression_error(expr, "missing type information");
578 return NULL;
581 /* Get the size of whatever the pointer points to */
582 bit_size = ptr_object_size(ctype);
583 multiply = bit_size >> 3;
585 expr->ctype = ctype;
587 if (multiply == 1 && itype->bit_size >= bits_in_pointer)
588 return ctype;
590 if (index->type == EXPR_VALUE) {
591 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
592 unsigned long long v = index->value, mask;
593 mask = 1ULL << (itype->bit_size - 1);
594 if (v & mask)
595 v |= -mask;
596 else
597 v &= mask - 1;
598 v *= multiply;
599 mask = 1ULL << (bits_in_pointer - 1);
600 v &= mask | (mask - 1);
601 val->value = v;
602 val->ctype = ssize_t_ctype;
603 expr->right = val;
604 return ctype;
607 if (itype->bit_size < bits_in_pointer)
608 index = cast_to(index, ssize_t_ctype);
610 if (multiply > 1) {
611 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
612 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
614 val->ctype = ssize_t_ctype;
615 val->value = multiply;
617 mul->op = '*';
618 mul->ctype = ssize_t_ctype;
619 mul->left = index;
620 mul->right = val;
621 index = mul;
624 expr->right = index;
625 return ctype;
628 static void examine_fn_arguments(struct symbol *fn);
630 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
632 const char *type_difference(struct ctype *c1, struct ctype *c2,
633 unsigned long mod1, unsigned long mod2)
635 unsigned long as1 = c1->as, as2 = c2->as;
636 struct symbol *t1 = c1->base_type;
637 struct symbol *t2 = c2->base_type;
638 int move1 = 1, move2 = 1;
639 mod1 |= c1->modifiers;
640 mod2 |= c2->modifiers;
641 for (;;) {
642 unsigned long diff;
643 int type;
644 struct symbol *base1 = t1->ctype.base_type;
645 struct symbol *base2 = t2->ctype.base_type;
648 * FIXME! Collect alignment and context too here!
650 if (move1) {
651 if (t1 && t1->type != SYM_PTR) {
652 mod1 |= t1->ctype.modifiers;
653 as1 |= t1->ctype.as;
655 move1 = 0;
658 if (move2) {
659 if (t2 && t2->type != SYM_PTR) {
660 mod2 |= t2->ctype.modifiers;
661 as2 |= t2->ctype.as;
663 move2 = 0;
666 if (t1 == t2)
667 break;
668 if (!t1 || !t2)
669 return "different types";
671 if (t1->type == SYM_NODE || t1->type == SYM_ENUM) {
672 t1 = base1;
673 move1 = 1;
674 if (!t1)
675 return "bad types";
676 continue;
679 if (t2->type == SYM_NODE || t2->type == SYM_ENUM) {
680 t2 = base2;
681 move2 = 1;
682 if (!t2)
683 return "bad types";
684 continue;
687 move1 = move2 = 1;
688 type = t1->type;
689 if (type != t2->type)
690 return "different base types";
692 switch (type) {
693 default:
694 sparse_error(t1->pos,
695 "internal error: bad type in derived(%d)",
696 type);
697 return "bad types";
698 case SYM_RESTRICT:
699 case SYM_UNION:
700 case SYM_STRUCT:
701 return "different base types";
702 case SYM_ARRAY:
703 /* XXX: we ought to compare sizes */
704 break;
705 case SYM_PTR:
706 if (Waddress_space && as1 != as2)
707 return "different address spaces";
708 /* MOD_SPECIFIER is due to idiocy in parse.c */
709 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SPECIFIER)
710 return "different modifiers";
711 /* we could be lazier here */
712 base1 = examine_pointer_target(t1);
713 base2 = examine_pointer_target(t2);
714 mod1 = t1->ctype.modifiers;
715 as1 = t1->ctype.as;
716 mod2 = t2->ctype.modifiers;
717 as2 = t2->ctype.as;
718 break;
719 case SYM_FN: {
720 struct symbol *arg1, *arg2;
721 int i;
723 if (Waddress_space && as1 != as2)
724 return "different address spaces";
725 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
726 return "different modifiers";
727 mod1 = t1->ctype.modifiers;
728 as1 = t1->ctype.as;
729 mod2 = t2->ctype.modifiers;
730 as2 = t2->ctype.as;
732 if (base1->variadic != base2->variadic)
733 return "incompatible variadic arguments";
734 examine_fn_arguments(t1);
735 examine_fn_arguments(t2);
736 PREPARE_PTR_LIST(t1->arguments, arg1);
737 PREPARE_PTR_LIST(t2->arguments, arg2);
738 i = 1;
739 for (;;) {
740 const char *diffstr;
741 if (!arg1 && !arg2)
742 break;
743 if (!arg1 || !arg2)
744 return "different argument counts";
745 diffstr = type_difference(&arg1->ctype,
746 &arg2->ctype,
747 MOD_IGN, MOD_IGN);
748 if (diffstr) {
749 static char argdiff[80];
750 sprintf(argdiff, "incompatible argument %d (%s)", i, diffstr);
751 return argdiff;
753 NEXT_PTR_LIST(arg1);
754 NEXT_PTR_LIST(arg2);
755 i++;
757 FINISH_PTR_LIST(arg2);
758 FINISH_PTR_LIST(arg1);
759 break;
761 case SYM_BASETYPE:
762 if (Waddress_space && as1 != as2)
763 return "different address spaces";
764 if (base1 != base2)
765 return "different base types";
766 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
767 if (!diff)
768 return NULL;
769 if (diff & MOD_SIZE)
770 return "different type sizes";
771 if (diff & ~MOD_SIGNEDNESS)
772 return "different modifiers";
774 /* Differs in signedness only.. */
775 if (Wtypesign) {
777 * Warn if both are explicitly signed ("unsigned" is obviously
778 * always explicit, and since we know one of them has to be
779 * unsigned, we check if the signed one was explicit).
781 if ((mod1 | mod2) & MOD_EXPLICITLY_SIGNED)
782 return "different explicit signedness";
785 * "char" matches both "unsigned char" and "signed char",
786 * so if the explicit test didn't trigger, then we should
787 * not warn about a char.
789 if (!(mod1 & MOD_CHAR))
790 return "different signedness";
792 return NULL;
794 t1 = base1;
795 t2 = base2;
797 if (Waddress_space && as1 != as2)
798 return "different address spaces";
799 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
800 return "different modifiers";
801 return NULL;
804 static void bad_null(struct expression *expr)
806 if (Wnon_pointer_null)
807 warning(expr->pos, "Using plain integer as NULL pointer");
810 static unsigned long target_qualifiers(struct symbol *type)
812 unsigned long mod = type->ctype.modifiers & MOD_IGN;
813 if (type->ctype.base_type && type->ctype.base_type->type == SYM_ARRAY)
814 mod = 0;
815 return mod;
818 static struct symbol *evaluate_ptr_sub(struct expression *expr)
820 const char *typediff;
821 struct symbol *ltype, *rtype;
822 struct expression *l = expr->left;
823 struct expression *r = expr->right;
824 struct symbol *lbase, *rbase;
826 classify_type(degenerate(l), &ltype);
827 classify_type(degenerate(r), &rtype);
829 lbase = examine_pointer_target(ltype);
830 rbase = examine_pointer_target(rtype);
831 typediff = type_difference(&ltype->ctype, &rtype->ctype,
832 target_qualifiers(rtype),
833 target_qualifiers(ltype));
834 if (typediff)
835 expression_error(expr, "subtraction of different types can't work (%s)", typediff);
837 if (lbase->type == SYM_FN) {
838 expression_error(expr, "subtraction of functions? Share your drugs");
839 return NULL;
842 expr->ctype = ssize_t_ctype;
843 if (lbase->bit_size > bits_in_char) {
844 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
845 struct expression *div = expr;
846 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
847 unsigned long value = lbase->bit_size >> 3;
849 val->ctype = size_t_ctype;
850 val->value = value;
852 if (value & (value-1)) {
853 if (Wptr_subtraction_blows)
854 warning(expr->pos, "potentially expensive pointer subtraction");
857 sub->op = '-';
858 sub->ctype = ssize_t_ctype;
859 sub->left = l;
860 sub->right = r;
862 div->op = '/';
863 div->left = sub;
864 div->right = val;
867 return ssize_t_ctype;
870 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
872 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
874 struct symbol *ctype;
876 if (!expr)
877 return NULL;
879 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
880 warning(expr->pos, "assignment expression in conditional");
882 ctype = evaluate_expression(expr);
883 if (ctype) {
884 if (is_safe_type(ctype))
885 warning(expr->pos, "testing a 'safe expression'");
888 return ctype;
891 static struct symbol *evaluate_logical(struct expression *expr)
893 if (!evaluate_conditional(expr->left, 0))
894 return NULL;
895 if (!evaluate_conditional(expr->right, 0))
896 return NULL;
898 expr->ctype = &bool_ctype;
899 if (expr->flags) {
900 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
901 expr->flags = 0;
903 return &bool_ctype;
906 static struct symbol *evaluate_binop(struct expression *expr)
908 struct symbol *ltype, *rtype, *ctype;
909 int lclass = classify_type(expr->left->ctype, &ltype);
910 int rclass = classify_type(expr->right->ctype, &rtype);
911 int op = expr->op;
913 if (expr->flags) {
914 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
915 expr->flags = 0;
918 /* number op number */
919 if (lclass & rclass & TYPE_NUM) {
920 if ((lclass | rclass) & TYPE_FLOAT) {
921 switch (op) {
922 case '+': case '-': case '*': case '/':
923 break;
924 default:
925 return bad_expr_type(expr);
929 if (op == SPECIAL_LEFTSHIFT || op == SPECIAL_RIGHTSHIFT) {
930 // shifts do integer promotions, but that's it.
931 unrestrict(expr->left, lclass, &ltype);
932 unrestrict(expr->right, rclass, &rtype);
933 ctype = ltype = integer_promotion(ltype);
934 rtype = integer_promotion(rtype);
935 } else {
936 // The rest do usual conversions
937 ltype = usual_conversions(op, expr->left, expr->right,
938 lclass, rclass, ltype, rtype);
939 ctype = rtype = ltype;
942 expr->left = cast_to(expr->left, ltype);
943 expr->right = cast_to(expr->right, rtype);
944 expr->ctype = ctype;
945 return ctype;
948 /* pointer (+|-) integer */
949 if (lclass & TYPE_PTR && is_int(rclass) && (op == '+' || op == '-')) {
950 unrestrict(expr->right, rclass, &rtype);
951 return evaluate_ptr_add(expr, degenerate(expr->left), rtype);
954 /* integer + pointer */
955 if (rclass & TYPE_PTR && is_int(lclass) && op == '+') {
956 struct expression *index = expr->left;
957 unrestrict(index, lclass, &ltype);
958 expr->left = expr->right;
959 expr->right = index;
960 return evaluate_ptr_add(expr, degenerate(expr->left), ltype);
963 /* pointer - pointer */
964 if (lclass & rclass & TYPE_PTR && expr->op == '-')
965 return evaluate_ptr_sub(expr);
967 return bad_expr_type(expr);
970 static struct symbol *evaluate_comma(struct expression *expr)
972 expr->ctype = degenerate(expr->right);
973 if (expr->ctype == &null_ctype)
974 expr->ctype = &ptr_ctype;
975 expr->flags &= expr->left->flags & expr->right->flags;
976 return expr->ctype;
979 static int modify_for_unsigned(int op)
981 if (op == '<')
982 op = SPECIAL_UNSIGNED_LT;
983 else if (op == '>')
984 op = SPECIAL_UNSIGNED_GT;
985 else if (op == SPECIAL_LTE)
986 op = SPECIAL_UNSIGNED_LTE;
987 else if (op == SPECIAL_GTE)
988 op = SPECIAL_UNSIGNED_GTE;
989 return op;
992 static inline int is_null_pointer_constant(struct expression *e)
994 if (e->ctype == &null_ctype)
995 return 1;
996 if (!(e->flags & Int_const_expr))
997 return 0;
998 return is_zero_constant(e) ? 2 : 0;
1001 static struct symbol *evaluate_compare(struct expression *expr)
1003 struct expression *left = expr->left, *right = expr->right;
1004 struct symbol *ltype, *rtype, *lbase, *rbase;
1005 int lclass = classify_type(degenerate(left), &ltype);
1006 int rclass = classify_type(degenerate(right), &rtype);
1007 struct symbol *ctype;
1008 const char *typediff;
1010 if (expr->flags) {
1011 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
1012 expr->flags = 0;
1015 /* Type types? */
1016 if (is_type_type(ltype) && is_type_type(rtype))
1017 goto OK;
1019 if (is_safe_type(left->ctype) || is_safe_type(right->ctype))
1020 warning(expr->pos, "testing a 'safe expression'");
1022 /* number on number */
1023 if (lclass & rclass & TYPE_NUM) {
1024 ctype = usual_conversions(expr->op, expr->left, expr->right,
1025 lclass, rclass, ltype, rtype);
1026 expr->left = cast_to(expr->left, ctype);
1027 expr->right = cast_to(expr->right, ctype);
1028 if (ctype->ctype.modifiers & MOD_UNSIGNED)
1029 expr->op = modify_for_unsigned(expr->op);
1030 goto OK;
1033 /* at least one must be a pointer */
1034 if (!((lclass | rclass) & TYPE_PTR))
1035 return bad_expr_type(expr);
1037 /* equality comparisons can be with null pointer constants */
1038 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1039 int is_null1 = is_null_pointer_constant(left);
1040 int is_null2 = is_null_pointer_constant(right);
1041 if (is_null1 == 2)
1042 bad_null(left);
1043 if (is_null2 == 2)
1044 bad_null(right);
1045 if (is_null1 && is_null2) {
1046 int positive = expr->op == SPECIAL_EQUAL;
1047 expr->type = EXPR_VALUE;
1048 expr->value = positive;
1049 goto OK;
1051 if (is_null1) {
1052 left = cast_to(left, rtype);
1053 goto OK;
1055 if (is_null2) {
1056 right = cast_to(right, ltype);
1057 goto OK;
1060 /* both should be pointers */
1061 if (!(lclass & rclass & TYPE_PTR))
1062 return bad_expr_type(expr);
1063 expr->op = modify_for_unsigned(expr->op);
1065 lbase = examine_pointer_target(ltype);
1066 rbase = examine_pointer_target(rtype);
1068 /* they also have special treatment for pointers to void */
1069 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1070 if (ltype->ctype.as == rtype->ctype.as) {
1071 if (lbase == &void_ctype) {
1072 right = cast_to(right, ltype);
1073 goto OK;
1075 if (rbase == &void_ctype) {
1076 left = cast_to(left, rtype);
1077 goto OK;
1082 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1083 target_qualifiers(rtype),
1084 target_qualifiers(ltype));
1085 if (!typediff)
1086 goto OK;
1088 expression_error(expr, "incompatible types in comparison expression (%s)", typediff);
1089 return NULL;
1092 expr->ctype = &bool_ctype;
1093 return &bool_ctype;
1097 * NOTE! The degenerate case of "x ? : y", where we don't
1098 * have a true case, this will possibly promote "x" to the
1099 * same type as "y", and thus _change_ the conditional
1100 * test in the expression. But since promotion is "safe"
1101 * for testing, that's OK.
1103 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1105 struct expression **true;
1106 struct symbol *ctype, *ltype, *rtype, *lbase, *rbase;
1107 int lclass, rclass;
1108 const char * typediff;
1109 int qual;
1111 if (!evaluate_conditional(expr->conditional, 0))
1112 return NULL;
1113 if (!evaluate_expression(expr->cond_false))
1114 return NULL;
1116 ctype = degenerate(expr->conditional);
1117 rtype = degenerate(expr->cond_false);
1119 true = &expr->conditional;
1120 ltype = ctype;
1121 if (expr->cond_true) {
1122 if (!evaluate_expression(expr->cond_true))
1123 return NULL;
1124 ltype = degenerate(expr->cond_true);
1125 true = &expr->cond_true;
1128 if (expr->flags) {
1129 int flags = expr->conditional->flags & Int_const_expr;
1130 flags &= (*true)->flags & expr->cond_false->flags;
1131 if (!flags)
1132 expr->flags = 0;
1135 lclass = classify_type(ltype, &ltype);
1136 rclass = classify_type(rtype, &rtype);
1137 if (lclass & rclass & TYPE_NUM) {
1138 ctype = usual_conversions('?', *true, expr->cond_false,
1139 lclass, rclass, ltype, rtype);
1140 *true = cast_to(*true, ctype);
1141 expr->cond_false = cast_to(expr->cond_false, ctype);
1142 goto out;
1145 if ((lclass | rclass) & TYPE_PTR) {
1146 int is_null1 = is_null_pointer_constant(*true);
1147 int is_null2 = is_null_pointer_constant(expr->cond_false);
1149 if (is_null1 && is_null2) {
1150 *true = cast_to(*true, &ptr_ctype);
1151 expr->cond_false = cast_to(expr->cond_false, &ptr_ctype);
1152 ctype = &ptr_ctype;
1153 goto out;
1155 if (is_null1 && (rclass & TYPE_PTR)) {
1156 if (is_null1 == 2)
1157 bad_null(*true);
1158 *true = cast_to(*true, rtype);
1159 ctype = rtype;
1160 goto out;
1162 if (is_null2 && (lclass & TYPE_PTR)) {
1163 if (is_null2 == 2)
1164 bad_null(expr->cond_false);
1165 expr->cond_false = cast_to(expr->cond_false, ltype);
1166 ctype = ltype;
1167 goto out;
1169 if (!(lclass & rclass & TYPE_PTR)) {
1170 typediff = "different types";
1171 goto Err;
1173 /* OK, it's pointer on pointer */
1174 if (ltype->ctype.as != rtype->ctype.as) {
1175 typediff = "different address spaces";
1176 goto Err;
1179 /* need to be lazier here */
1180 lbase = examine_pointer_target(ltype);
1181 rbase = examine_pointer_target(rtype);
1182 qual = target_qualifiers(ltype) | target_qualifiers(rtype);
1184 if (lbase == &void_ctype) {
1185 /* XXX: pointers to function should warn here */
1186 ctype = ltype;
1187 goto Qual;
1190 if (rbase == &void_ctype) {
1191 /* XXX: pointers to function should warn here */
1192 ctype = rtype;
1193 goto Qual;
1195 /* XXX: that should be pointer to composite */
1196 ctype = ltype;
1197 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1198 qual, qual);
1199 if (!typediff)
1200 goto Qual;
1201 goto Err;
1204 /* void on void, struct on same struct, union on same union */
1205 if (ltype == rtype) {
1206 ctype = ltype;
1207 goto out;
1209 typediff = "different base types";
1211 Err:
1212 expression_error(expr, "incompatible types in conditional expression (%s)", typediff);
1213 return NULL;
1215 out:
1216 expr->ctype = ctype;
1217 return ctype;
1219 Qual:
1220 if (qual & ~ctype->ctype.modifiers) {
1221 struct symbol *sym = alloc_symbol(ctype->pos, SYM_PTR);
1222 *sym = *ctype;
1223 sym->ctype.modifiers |= qual;
1224 ctype = sym;
1226 *true = cast_to(*true, ctype);
1227 expr->cond_false = cast_to(expr->cond_false, ctype);
1228 goto out;
1231 /* FP assignments can not do modulo or bit operations */
1232 static int compatible_float_op(int op)
1234 return op == SPECIAL_ADD_ASSIGN ||
1235 op == SPECIAL_SUB_ASSIGN ||
1236 op == SPECIAL_MUL_ASSIGN ||
1237 op == SPECIAL_DIV_ASSIGN;
1240 static int evaluate_assign_op(struct expression *expr)
1242 struct symbol *target = expr->left->ctype;
1243 struct symbol *source = expr->right->ctype;
1244 struct symbol *t, *s;
1245 int tclass = classify_type(target, &t);
1246 int sclass = classify_type(source, &s);
1247 int op = expr->op;
1249 if (tclass & sclass & TYPE_NUM) {
1250 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1251 expression_error(expr, "invalid assignment");
1252 return 0;
1254 if (tclass & TYPE_RESTRICT) {
1255 if (!restricted_binop(op, t)) {
1256 expression_error(expr, "bad restricted assignment");
1257 return 0;
1259 /* allowed assignments unfoul */
1260 if (sclass & TYPE_FOULED && unfoul(s) == t)
1261 goto Cast;
1262 if (!restricted_value(expr->right, t))
1263 return 1;
1264 } else if (!(sclass & TYPE_RESTRICT))
1265 goto Cast;
1266 /* source and target would better be identical restricted */
1267 if (t == s)
1268 return 1;
1269 warning(expr->pos, "invalid restricted assignment");
1270 expr->right = cast_to(expr->right, target);
1271 return 0;
1273 if (tclass & TYPE_PTR && is_int(sclass)) {
1274 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1275 unrestrict(expr->right, sclass, &s);
1276 evaluate_ptr_add(expr, target, s);
1277 return 1;
1279 expression_error(expr, "invalid pointer assignment");
1280 return 0;
1283 expression_error(expr, "invalid assignment");
1284 return 0;
1286 Cast:
1287 expr->right = cast_to(expr->right, target);
1288 return 1;
1291 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1292 struct expression **rp, const char *where)
1294 const char *typediff;
1295 struct symbol *source = degenerate(*rp);
1296 struct symbol *t, *s;
1297 int tclass = classify_type(target, &t);
1298 int sclass = classify_type(source, &s);
1300 if (tclass & sclass & TYPE_NUM) {
1301 if (tclass & TYPE_RESTRICT) {
1302 /* allowed assignments unfoul */
1303 if (sclass & TYPE_FOULED && unfoul(s) == t)
1304 goto Cast;
1305 if (!restricted_value(*rp, target))
1306 return 1;
1307 if (s == t)
1308 return 1;
1309 } else if (!(sclass & TYPE_RESTRICT))
1310 goto Cast;
1311 typediff = "different base types";
1312 goto Err;
1315 if (tclass == TYPE_PTR) {
1316 unsigned long mod1, mod2;
1317 struct symbol *b1, *b2;
1318 // NULL pointer is always OK
1319 int is_null = is_null_pointer_constant(*rp);
1320 if (is_null) {
1321 if (is_null == 2)
1322 bad_null(*rp);
1323 goto Cast;
1325 if (!(sclass & TYPE_PTR)) {
1326 typediff = "different base types";
1327 goto Err;
1329 b1 = examine_pointer_target(t);
1330 b2 = examine_pointer_target(s);
1331 mod1 = target_qualifiers(t);
1332 mod2 = target_qualifiers(s);
1333 if (b1 == &void_ctype || b2 == &void_ctype) {
1335 * assignments to/from void * are OK, provided that
1336 * we do not remove qualifiers from pointed to [C]
1337 * or mix address spaces [sparse].
1339 if (t->ctype.as != s->ctype.as) {
1340 typediff = "different address spaces";
1341 goto Err;
1343 if (mod2 & ~mod1) {
1344 typediff = "different modifiers";
1345 goto Err;
1347 goto Cast;
1349 /* It's OK if the target is more volatile or const than the source */
1350 typediff = type_difference(&t->ctype, &s->ctype, 0, mod1);
1351 if (typediff)
1352 goto Err;
1353 return 1;
1356 if ((tclass & TYPE_COMPOUND) && s == t)
1357 return 1;
1359 if (tclass & TYPE_NUM) {
1360 /* XXX: need to turn into comparison with NULL */
1361 if (t == &bool_ctype && (sclass & TYPE_PTR))
1362 goto Cast;
1363 typediff = "different base types";
1364 goto Err;
1366 typediff = "invalid types";
1368 Err:
1369 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1370 info(expr->pos, " expected %s", show_typename(target));
1371 info(expr->pos, " got %s", show_typename(source));
1372 *rp = cast_to(*rp, target);
1373 return 0;
1374 Cast:
1375 *rp = cast_to(*rp, target);
1376 return 1;
1379 static void mark_assigned(struct expression *expr)
1381 struct symbol *sym;
1383 if (!expr)
1384 return;
1385 switch (expr->type) {
1386 case EXPR_SYMBOL:
1387 sym = expr->symbol;
1388 if (!sym)
1389 return;
1390 if (sym->type != SYM_NODE)
1391 return;
1392 sym->ctype.modifiers |= MOD_ASSIGNED;
1393 return;
1395 case EXPR_BINOP:
1396 mark_assigned(expr->left);
1397 mark_assigned(expr->right);
1398 return;
1399 case EXPR_CAST:
1400 case EXPR_FORCE_CAST:
1401 mark_assigned(expr->cast_expression);
1402 return;
1403 case EXPR_SLICE:
1404 mark_assigned(expr->base);
1405 return;
1406 default:
1407 /* Hmm? */
1408 return;
1412 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1414 if (type->ctype.modifiers & MOD_CONST)
1415 expression_error(left, "assignment to const expression");
1417 /* We know left is an lvalue, so it's a "preop-*" */
1418 mark_assigned(left->unop);
1421 static struct symbol *evaluate_assignment(struct expression *expr)
1423 struct expression *left = expr->left;
1424 struct expression *where = expr;
1425 struct symbol *ltype;
1427 if (!lvalue_expression(left)) {
1428 expression_error(expr, "not an lvalue");
1429 return NULL;
1432 ltype = left->ctype;
1434 if (expr->op != '=') {
1435 if (!evaluate_assign_op(expr))
1436 return NULL;
1437 } else {
1438 if (!compatible_assignment_types(where, ltype, &expr->right, "assignment"))
1439 return NULL;
1442 evaluate_assign_to(left, ltype);
1444 expr->ctype = ltype;
1445 return ltype;
1448 static void examine_fn_arguments(struct symbol *fn)
1450 struct symbol *s;
1452 FOR_EACH_PTR(fn->arguments, s) {
1453 struct symbol *arg = evaluate_symbol(s);
1454 /* Array/function arguments silently degenerate into pointers */
1455 if (arg) {
1456 struct symbol *ptr;
1457 switch(arg->type) {
1458 case SYM_ARRAY:
1459 case SYM_FN:
1460 ptr = alloc_symbol(s->pos, SYM_PTR);
1461 if (arg->type == SYM_ARRAY)
1462 ptr->ctype = arg->ctype;
1463 else
1464 ptr->ctype.base_type = arg;
1465 ptr->ctype.as |= s->ctype.as;
1466 ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1468 s->ctype.base_type = ptr;
1469 s->ctype.as = 0;
1470 s->ctype.modifiers &= ~MOD_PTRINHERIT;
1471 s->bit_size = 0;
1472 s->examined = 0;
1473 examine_symbol_type(s);
1474 break;
1475 default:
1476 /* nothing */
1477 break;
1480 } END_FOR_EACH_PTR(s);
1483 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1485 /* Take the modifiers of the pointer, and apply them to the member */
1486 mod |= sym->ctype.modifiers;
1487 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1488 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1489 *newsym = *sym;
1490 newsym->ctype.as = as;
1491 newsym->ctype.modifiers = mod;
1492 sym = newsym;
1494 return sym;
1497 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1499 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1500 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1502 node->ctype.base_type = ptr;
1503 ptr->bit_size = bits_in_pointer;
1504 ptr->ctype.alignment = pointer_alignment;
1506 node->bit_size = bits_in_pointer;
1507 node->ctype.alignment = pointer_alignment;
1509 access_symbol(sym);
1510 if (sym->ctype.modifiers & MOD_REGISTER) {
1511 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1512 sym->ctype.modifiers &= ~MOD_REGISTER;
1514 if (sym->type == SYM_NODE) {
1515 ptr->ctype.as |= sym->ctype.as;
1516 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1517 sym = sym->ctype.base_type;
1519 if (degenerate && sym->type == SYM_ARRAY) {
1520 ptr->ctype.as |= sym->ctype.as;
1521 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1522 sym = sym->ctype.base_type;
1524 ptr->ctype.base_type = sym;
1526 return node;
1529 /* Arrays degenerate into pointers on pointer arithmetic */
1530 static struct symbol *degenerate(struct expression *expr)
1532 struct symbol *ctype, *base;
1534 if (!expr)
1535 return NULL;
1536 ctype = expr->ctype;
1537 if (!ctype)
1538 return NULL;
1539 base = examine_symbol_type(ctype);
1540 if (ctype->type == SYM_NODE)
1541 base = ctype->ctype.base_type;
1543 * Arrays degenerate into pointers to the entries, while
1544 * functions degenerate into pointers to themselves.
1545 * If array was part of non-lvalue compound, we create a copy
1546 * of that compound first and then act as if we were dealing with
1547 * the corresponding field in there.
1549 switch (base->type) {
1550 case SYM_ARRAY:
1551 if (expr->type == EXPR_SLICE) {
1552 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1553 struct expression *e0, *e1, *e2, *e3, *e4;
1555 a->ctype.base_type = expr->base->ctype;
1556 a->bit_size = expr->base->ctype->bit_size;
1557 a->array_size = expr->base->ctype->array_size;
1559 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1560 e0->symbol = a;
1561 e0->ctype = &lazy_ptr_ctype;
1563 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1564 e1->unop = e0;
1565 e1->op = '*';
1566 e1->ctype = expr->base->ctype; /* XXX */
1568 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1569 e2->left = e1;
1570 e2->right = expr->base;
1571 e2->op = '=';
1572 e2->ctype = expr->base->ctype;
1574 if (expr->r_bitpos) {
1575 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1576 e3->op = '+';
1577 e3->left = e0;
1578 e3->right = alloc_const_expression(expr->pos,
1579 expr->r_bitpos >> 3);
1580 e3->ctype = &lazy_ptr_ctype;
1581 } else {
1582 e3 = e0;
1585 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1586 e4->left = e2;
1587 e4->right = e3;
1588 e4->ctype = &lazy_ptr_ctype;
1590 expr->unop = e4;
1591 expr->type = EXPR_PREOP;
1592 expr->op = '*';
1594 case SYM_FN:
1595 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1596 expression_error(expr, "strange non-value function or array");
1597 return &bad_ctype;
1599 *expr = *expr->unop;
1600 ctype = create_pointer(expr, ctype, 1);
1601 expr->ctype = ctype;
1602 default:
1603 /* nothing */;
1605 return ctype;
1608 static struct symbol *evaluate_addressof(struct expression *expr)
1610 struct expression *op = expr->unop;
1611 struct symbol *ctype;
1613 if (op->op != '*' || op->type != EXPR_PREOP) {
1614 expression_error(expr, "not addressable");
1615 return NULL;
1617 ctype = op->ctype;
1618 *expr = *op->unop;
1619 expr->flags = 0;
1621 if (expr->type == EXPR_SYMBOL) {
1622 struct symbol *sym = expr->symbol;
1623 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1627 * symbol expression evaluation is lazy about the type
1628 * of the sub-expression, so we may have to generate
1629 * the type here if so..
1631 if (expr->ctype == &lazy_ptr_ctype) {
1632 ctype = create_pointer(expr, ctype, 0);
1633 expr->ctype = ctype;
1635 return expr->ctype;
1639 static struct symbol *evaluate_dereference(struct expression *expr)
1641 struct expression *op = expr->unop;
1642 struct symbol *ctype = op->ctype, *node, *target;
1644 /* Simplify: *&(expr) => (expr) */
1645 if (op->type == EXPR_PREOP && op->op == '&') {
1646 *expr = *op->unop;
1647 expr->flags = 0;
1648 return expr->ctype;
1651 /* Dereferencing a node drops all the node information. */
1652 if (ctype->type == SYM_NODE)
1653 ctype = ctype->ctype.base_type;
1655 node = alloc_symbol(expr->pos, SYM_NODE);
1656 target = ctype->ctype.base_type;
1658 switch (ctype->type) {
1659 default:
1660 expression_error(expr, "cannot dereference this type");
1661 return NULL;
1662 case SYM_PTR:
1663 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1664 merge_type(node, ctype);
1665 break;
1667 case SYM_ARRAY:
1668 if (!lvalue_expression(op)) {
1669 expression_error(op, "non-lvalue array??");
1670 return NULL;
1673 /* Do the implied "addressof" on the array */
1674 *op = *op->unop;
1677 * When an array is dereferenced, we need to pick
1678 * up the attributes of the original node too..
1680 merge_type(node, op->ctype);
1681 merge_type(node, ctype);
1682 break;
1685 node->bit_size = target->bit_size;
1686 node->array_size = target->array_size;
1688 expr->ctype = node;
1689 return node;
1693 * Unary post-ops: x++ and x--
1695 static struct symbol *evaluate_postop(struct expression *expr)
1697 struct expression *op = expr->unop;
1698 struct symbol *ctype = op->ctype;
1700 if (!lvalue_expression(expr->unop)) {
1701 expression_error(expr, "need lvalue expression for ++/--");
1702 return NULL;
1704 if (is_restricted_type(ctype) && restricted_unop(expr->op, &ctype)) {
1705 expression_error(expr, "bad operation on restricted");
1706 return NULL;
1707 } else if (is_fouled_type(ctype) && restricted_unop(expr->op, &ctype)) {
1708 expression_error(expr, "bad operation on restricted");
1709 return NULL;
1712 evaluate_assign_to(op, ctype);
1714 expr->ctype = ctype;
1715 expr->op_value = 1;
1716 if (is_ptr_type(ctype))
1717 expr->op_value = ptr_object_size(ctype) >> 3;
1719 return ctype;
1722 static struct symbol *evaluate_sign(struct expression *expr)
1724 struct symbol *ctype = expr->unop->ctype;
1725 int class = classify_type(ctype, &ctype);
1726 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1727 expr->flags = 0;
1728 /* should be an arithmetic type */
1729 if (!(class & TYPE_NUM))
1730 return bad_expr_type(expr);
1731 if (!(class & (TYPE_FLOAT|TYPE_RESTRICT))) {
1732 struct symbol *rtype = integer_promotion(ctype);
1733 expr->unop = cast_to(expr->unop, rtype);
1734 ctype = rtype;
1735 } else if ((class & TYPE_FLOAT) && expr->op != '~') {
1736 /* no conversions needed */
1737 } else if ((class & TYPE_RESTRICT) && !restricted_unop(expr->op, &ctype)) {
1738 /* no conversions needed */
1739 } else {
1740 return bad_expr_type(expr);
1742 if (expr->op == '+')
1743 *expr = *expr->unop;
1744 expr->ctype = ctype;
1745 return ctype;
1748 static struct symbol *evaluate_preop(struct expression *expr)
1750 struct symbol *ctype = expr->unop->ctype;
1752 switch (expr->op) {
1753 case '(':
1754 *expr = *expr->unop;
1755 return ctype;
1757 case '+':
1758 case '-':
1759 case '~':
1760 return evaluate_sign(expr);
1762 case '*':
1763 return evaluate_dereference(expr);
1765 case '&':
1766 return evaluate_addressof(expr);
1768 case SPECIAL_INCREMENT:
1769 case SPECIAL_DECREMENT:
1771 * From a type evaluation standpoint the preops are
1772 * the same as the postops
1774 return evaluate_postop(expr);
1776 case '!':
1777 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1778 expr->flags = 0;
1779 if (is_safe_type(ctype))
1780 warning(expr->pos, "testing a 'safe expression'");
1781 if (is_float_type(ctype)) {
1782 struct expression *arg = expr->unop;
1783 expr->type = EXPR_BINOP;
1784 expr->op = SPECIAL_EQUAL;
1785 expr->left = arg;
1786 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1787 expr->right->ctype = ctype;
1788 expr->right->fvalue = 0;
1789 } else if (is_fouled_type(ctype)) {
1790 warning(expr->pos, "restricted degrades to integer");
1792 ctype = &bool_ctype;
1793 break;
1795 default:
1796 break;
1798 expr->ctype = ctype;
1799 return &bool_ctype;
1802 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1804 struct ptr_list *head = (struct ptr_list *)_list;
1805 struct ptr_list *list = head;
1807 if (!head)
1808 return NULL;
1809 do {
1810 int i;
1811 for (i = 0; i < list->nr; i++) {
1812 struct symbol *sym = (struct symbol *) list->list[i];
1813 if (sym->ident) {
1814 if (sym->ident != ident)
1815 continue;
1816 *offset = sym->offset;
1817 return sym;
1818 } else {
1819 struct symbol *ctype = sym->ctype.base_type;
1820 struct symbol *sub;
1821 if (!ctype)
1822 continue;
1823 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1824 continue;
1825 sub = find_identifier(ident, ctype->symbol_list, offset);
1826 if (!sub)
1827 continue;
1828 *offset += sym->offset;
1829 return sub;
1832 } while ((list = list->next) != head);
1833 return NULL;
1836 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1838 struct expression *add;
1841 * Create a new add-expression
1843 * NOTE! Even if we just add zero, we need a new node
1844 * for the member pointer, since it has a different
1845 * type than the original pointer. We could make that
1846 * be just a cast, but the fact is, a node is a node,
1847 * so we might as well just do the "add zero" here.
1849 add = alloc_expression(expr->pos, EXPR_BINOP);
1850 add->op = '+';
1851 add->left = expr;
1852 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1853 add->right->ctype = &int_ctype;
1854 add->right->value = offset;
1857 * The ctype of the pointer will be lazily evaluated if
1858 * we ever take the address of this member dereference..
1860 add->ctype = &lazy_ptr_ctype;
1861 return add;
1864 /* structure/union dereference */
1865 static struct symbol *evaluate_member_dereference(struct expression *expr)
1867 int offset;
1868 struct symbol *ctype, *member;
1869 struct expression *deref = expr->deref, *add;
1870 struct ident *ident = expr->member;
1871 unsigned int mod;
1872 int address_space;
1874 if (!evaluate_expression(deref))
1875 return NULL;
1876 if (!ident) {
1877 expression_error(expr, "bad member name");
1878 return NULL;
1881 ctype = deref->ctype;
1882 examine_symbol_type(ctype);
1883 address_space = ctype->ctype.as;
1884 mod = ctype->ctype.modifiers;
1885 if (ctype->type == SYM_NODE) {
1886 ctype = ctype->ctype.base_type;
1887 address_space |= ctype->ctype.as;
1888 mod |= ctype->ctype.modifiers;
1890 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1891 expression_error(expr, "expected structure or union");
1892 return NULL;
1894 offset = 0;
1895 member = find_identifier(ident, ctype->symbol_list, &offset);
1896 if (!member) {
1897 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1898 const char *name = "<unnamed>";
1899 int namelen = 9;
1900 if (ctype->ident) {
1901 name = ctype->ident->name;
1902 namelen = ctype->ident->len;
1904 if (ctype->symbol_list)
1905 expression_error(expr, "no member '%s' in %s %.*s",
1906 show_ident(ident), type, namelen, name);
1907 else
1908 expression_error(expr, "using member '%s' in "
1909 "incomplete %s %.*s", show_ident(ident),
1910 type, namelen, name);
1911 return NULL;
1915 * The member needs to take on the address space and modifiers of
1916 * the "parent" type.
1918 member = convert_to_as_mod(member, address_space, mod);
1919 ctype = get_base_type(member);
1921 if (!lvalue_expression(deref)) {
1922 if (deref->type != EXPR_SLICE) {
1923 expr->base = deref;
1924 expr->r_bitpos = 0;
1925 } else {
1926 expr->base = deref->base;
1927 expr->r_bitpos = deref->r_bitpos;
1929 expr->r_bitpos += offset << 3;
1930 expr->type = EXPR_SLICE;
1931 expr->r_nrbits = member->bit_size;
1932 expr->r_bitpos += member->bit_offset;
1933 expr->ctype = member;
1934 return member;
1937 deref = deref->unop;
1938 expr->deref = deref;
1940 add = evaluate_offset(deref, offset);
1941 expr->type = EXPR_PREOP;
1942 expr->op = '*';
1943 expr->unop = add;
1945 expr->ctype = member;
1946 return member;
1949 static int is_promoted(struct expression *expr)
1951 while (1) {
1952 switch (expr->type) {
1953 case EXPR_BINOP:
1954 case EXPR_SELECT:
1955 case EXPR_CONDITIONAL:
1956 return 1;
1957 case EXPR_COMMA:
1958 expr = expr->right;
1959 continue;
1960 case EXPR_PREOP:
1961 switch (expr->op) {
1962 case '(':
1963 expr = expr->unop;
1964 continue;
1965 case '+':
1966 case '-':
1967 case '~':
1968 return 1;
1969 default:
1970 return 0;
1972 default:
1973 return 0;
1979 static struct symbol *evaluate_cast(struct expression *);
1981 static struct symbol *evaluate_type_information(struct expression *expr)
1983 struct symbol *sym = expr->cast_type;
1984 if (!sym) {
1985 sym = evaluate_expression(expr->cast_expression);
1986 if (!sym)
1987 return NULL;
1989 * Expressions of restricted types will possibly get
1990 * promoted - check that here
1992 if (is_restricted_type(sym)) {
1993 if (sym->bit_size < bits_in_int && is_promoted(expr))
1994 sym = &int_ctype;
1995 } else if (is_fouled_type(sym)) {
1996 sym = &int_ctype;
1999 examine_symbol_type(sym);
2000 if (is_bitfield_type(sym)) {
2001 expression_error(expr, "trying to examine bitfield type");
2002 return NULL;
2004 return sym;
2007 static struct symbol *evaluate_sizeof(struct expression *expr)
2009 struct symbol *type;
2010 int size;
2012 type = evaluate_type_information(expr);
2013 if (!type)
2014 return NULL;
2016 size = type->bit_size;
2017 if ((size < 0) || (size & 7))
2018 expression_error(expr, "cannot size expression");
2019 expr->type = EXPR_VALUE;
2020 expr->value = size >> 3;
2021 expr->taint = 0;
2022 expr->ctype = size_t_ctype;
2023 return size_t_ctype;
2026 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
2028 struct symbol *type;
2029 int size;
2031 type = evaluate_type_information(expr);
2032 if (!type)
2033 return NULL;
2035 if (type->type == SYM_NODE)
2036 type = type->ctype.base_type;
2037 if (!type)
2038 return NULL;
2039 switch (type->type) {
2040 case SYM_ARRAY:
2041 break;
2042 case SYM_PTR:
2043 type = get_base_type(type);
2044 if (type)
2045 break;
2046 default:
2047 expression_error(expr, "expected pointer expression");
2048 return NULL;
2050 size = type->bit_size;
2051 if (size & 7)
2052 size = 0;
2053 expr->type = EXPR_VALUE;
2054 expr->value = size >> 3;
2055 expr->taint = 0;
2056 expr->ctype = size_t_ctype;
2057 return size_t_ctype;
2060 static struct symbol *evaluate_alignof(struct expression *expr)
2062 struct symbol *type;
2064 type = evaluate_type_information(expr);
2065 if (!type)
2066 return NULL;
2068 expr->type = EXPR_VALUE;
2069 expr->value = type->ctype.alignment;
2070 expr->taint = 0;
2071 expr->ctype = size_t_ctype;
2072 return size_t_ctype;
2075 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
2077 struct expression *expr;
2078 struct symbol_list *argument_types = fn->arguments;
2079 struct symbol *argtype;
2080 int i = 1;
2082 PREPARE_PTR_LIST(argument_types, argtype);
2083 FOR_EACH_PTR (head, expr) {
2084 struct expression **p = THIS_ADDRESS(expr);
2085 struct symbol *ctype, *target;
2086 ctype = evaluate_expression(expr);
2088 if (!ctype)
2089 return 0;
2091 target = argtype;
2092 if (!target) {
2093 struct symbol *type;
2094 int class = classify_type(ctype, &type);
2095 if (is_int(class)) {
2096 *p = cast_to(expr, integer_promotion(type));
2097 } else if (class & TYPE_FLOAT) {
2098 unsigned long mod = type->ctype.modifiers;
2099 if (!(mod & (MOD_LONG|MOD_LONGLONG)))
2100 *p = cast_to(expr, &double_ctype);
2101 } else if (class & TYPE_PTR) {
2102 if (expr->ctype == &null_ctype)
2103 *p = cast_to(expr, &ptr_ctype);
2104 else
2105 degenerate(expr);
2107 } else {
2108 static char where[30];
2109 examine_symbol_type(target);
2110 sprintf(where, "argument %d", i);
2111 compatible_assignment_types(expr, target, p, where);
2114 i++;
2115 NEXT_PTR_LIST(argtype);
2116 } END_FOR_EACH_PTR(expr);
2117 FINISH_PTR_LIST(argtype);
2118 return 1;
2121 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
2123 struct symbol *sym;
2125 FOR_EACH_PTR(ctype->symbol_list, sym) {
2126 if (sym->ident == ident)
2127 return sym;
2128 } END_FOR_EACH_PTR(sym);
2129 return NULL;
2132 static void convert_index(struct expression *e)
2134 struct expression *child = e->idx_expression;
2135 unsigned from = e->idx_from;
2136 unsigned to = e->idx_to + 1;
2137 e->type = EXPR_POS;
2138 e->init_offset = from * (e->ctype->bit_size>>3);
2139 e->init_nr = to - from;
2140 e->init_expr = child;
2143 static void convert_ident(struct expression *e)
2145 struct expression *child = e->ident_expression;
2146 struct symbol *sym = e->field;
2147 e->type = EXPR_POS;
2148 e->init_offset = sym->offset;
2149 e->init_nr = 1;
2150 e->init_expr = child;
2153 static void convert_designators(struct expression *e)
2155 while (e) {
2156 if (e->type == EXPR_INDEX)
2157 convert_index(e);
2158 else if (e->type == EXPR_IDENTIFIER)
2159 convert_ident(e);
2160 else
2161 break;
2162 e = e->init_expr;
2166 static void excess(struct expression *e, const char *s)
2168 warning(e->pos, "excessive elements in %s initializer", s);
2172 * implicit designator for the first element
2174 static struct expression *first_subobject(struct symbol *ctype, int class,
2175 struct expression **v)
2177 struct expression *e = *v, *new;
2179 if (ctype->type == SYM_NODE)
2180 ctype = ctype->ctype.base_type;
2182 if (class & TYPE_PTR) { /* array */
2183 if (!ctype->bit_size)
2184 return NULL;
2185 new = alloc_expression(e->pos, EXPR_INDEX);
2186 new->idx_expression = e;
2187 new->ctype = ctype->ctype.base_type;
2188 } else {
2189 struct symbol *field, *p;
2190 PREPARE_PTR_LIST(ctype->symbol_list, p);
2191 while (p && !p->ident && is_bitfield_type(p))
2192 NEXT_PTR_LIST(p);
2193 field = p;
2194 FINISH_PTR_LIST(p);
2195 if (!field)
2196 return NULL;
2197 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2198 new->ident_expression = e;
2199 new->field = new->ctype = field;
2201 *v = new;
2202 return new;
2206 * sanity-check explicit designators; return the innermost one or NULL
2207 * in case of error. Assign types.
2209 static struct expression *check_designators(struct expression *e,
2210 struct symbol *ctype)
2212 struct expression *last = NULL;
2213 const char *err;
2214 while (1) {
2215 if (ctype->type == SYM_NODE)
2216 ctype = ctype->ctype.base_type;
2217 if (e->type == EXPR_INDEX) {
2218 struct symbol *type;
2219 if (ctype->type != SYM_ARRAY) {
2220 err = "array index in non-array";
2221 break;
2223 type = ctype->ctype.base_type;
2224 if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2225 unsigned offset = e->idx_to * type->bit_size;
2226 if (offset >= ctype->bit_size) {
2227 err = "index out of bounds in";
2228 break;
2231 e->ctype = ctype = type;
2232 ctype = type;
2233 last = e;
2234 if (!e->idx_expression) {
2235 err = "invalid";
2236 break;
2238 e = e->idx_expression;
2239 } else if (e->type == EXPR_IDENTIFIER) {
2240 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2241 err = "field name not in struct or union";
2242 break;
2244 ctype = find_struct_ident(ctype, e->expr_ident);
2245 if (!ctype) {
2246 err = "unknown field name in";
2247 break;
2249 e->field = e->ctype = ctype;
2250 last = e;
2251 if (!e->ident_expression) {
2252 err = "invalid";
2253 break;
2255 e = e->ident_expression;
2256 } else if (e->type == EXPR_POS) {
2257 err = "internal front-end error: EXPR_POS in";
2258 break;
2259 } else
2260 return last;
2262 expression_error(e, "%s initializer", err);
2263 return NULL;
2267 * choose the next subobject to initialize.
2269 * Get designators for next element, switch old ones to EXPR_POS.
2270 * Return the resulting expression or NULL if we'd run out of subobjects.
2271 * The innermost designator is returned in *v. Designators in old
2272 * are assumed to be already sanity-checked.
2274 static struct expression *next_designators(struct expression *old,
2275 struct symbol *ctype,
2276 struct expression *e, struct expression **v)
2278 struct expression *new = NULL;
2280 if (!old)
2281 return NULL;
2282 if (old->type == EXPR_INDEX) {
2283 struct expression *copy;
2284 unsigned n;
2286 copy = next_designators(old->idx_expression,
2287 old->ctype, e, v);
2288 if (!copy) {
2289 n = old->idx_to + 1;
2290 if (n * old->ctype->bit_size == ctype->bit_size) {
2291 convert_index(old);
2292 return NULL;
2294 copy = e;
2295 *v = new = alloc_expression(e->pos, EXPR_INDEX);
2296 } else {
2297 n = old->idx_to;
2298 new = alloc_expression(e->pos, EXPR_INDEX);
2301 new->idx_from = new->idx_to = n;
2302 new->idx_expression = copy;
2303 new->ctype = old->ctype;
2304 convert_index(old);
2305 } else if (old->type == EXPR_IDENTIFIER) {
2306 struct expression *copy;
2307 struct symbol *field;
2309 copy = next_designators(old->ident_expression,
2310 old->ctype, e, v);
2311 if (!copy) {
2312 field = old->field->next_subobject;
2313 if (!field) {
2314 convert_ident(old);
2315 return NULL;
2317 copy = e;
2318 *v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2319 } else {
2320 field = old->field;
2321 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2324 new->field = field;
2325 new->expr_ident = field->ident;
2326 new->ident_expression = copy;
2327 new->ctype = field;
2328 convert_ident(old);
2330 return new;
2333 static int handle_simple_initializer(struct expression **ep, int nested,
2334 int class, struct symbol *ctype);
2337 * deal with traversing subobjects [6.7.8(17,18,20)]
2339 static void handle_list_initializer(struct expression *expr,
2340 int class, struct symbol *ctype)
2342 struct expression *e, *last = NULL, *top = NULL, *next;
2343 int jumped = 0;
2345 FOR_EACH_PTR(expr->expr_list, e) {
2346 struct expression **v;
2347 struct symbol *type;
2348 int lclass;
2350 if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2351 if (!top) {
2352 top = e;
2353 last = first_subobject(ctype, class, &top);
2354 } else {
2355 last = next_designators(last, ctype, e, &top);
2357 if (!last) {
2358 excess(e, class & TYPE_PTR ? "array" :
2359 "struct or union");
2360 DELETE_CURRENT_PTR(e);
2361 continue;
2363 if (jumped) {
2364 warning(e->pos, "advancing past deep designator");
2365 jumped = 0;
2367 REPLACE_CURRENT_PTR(e, last);
2368 } else {
2369 next = check_designators(e, ctype);
2370 if (!next) {
2371 DELETE_CURRENT_PTR(e);
2372 continue;
2374 top = next;
2375 /* deeper than one designator? */
2376 jumped = top != e;
2377 convert_designators(last);
2378 last = e;
2381 found:
2382 lclass = classify_type(top->ctype, &type);
2383 if (top->type == EXPR_INDEX)
2384 v = &top->idx_expression;
2385 else
2386 v = &top->ident_expression;
2388 if (handle_simple_initializer(v, 1, lclass, top->ctype))
2389 continue;
2391 if (!(lclass & TYPE_COMPOUND)) {
2392 warning(e->pos, "bogus scalar initializer");
2393 DELETE_CURRENT_PTR(e);
2394 continue;
2397 next = first_subobject(type, lclass, v);
2398 if (next) {
2399 warning(e->pos, "missing braces around initializer");
2400 top = next;
2401 goto found;
2404 DELETE_CURRENT_PTR(e);
2405 excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2407 } END_FOR_EACH_PTR(e);
2409 convert_designators(last);
2410 expr->ctype = ctype;
2413 static int is_string_literal(struct expression **v)
2415 struct expression *e = *v;
2416 while (e && e->type == EXPR_PREOP && e->op == '(')
2417 e = e->unop;
2418 if (!e || e->type != EXPR_STRING)
2419 return 0;
2420 if (e != *v && Wparen_string)
2421 warning(e->pos,
2422 "array initialized from parenthesized string constant");
2423 *v = e;
2424 return 1;
2428 * We want a normal expression, possibly in one layer of braces. Warn
2429 * if the latter happens inside a list (it's legal, but likely to be
2430 * an effect of screwup). In case of anything not legal, we are definitely
2431 * having an effect of screwup, so just fail and let the caller warn.
2433 static struct expression *handle_scalar(struct expression *e, int nested)
2435 struct expression *v = NULL, *p;
2436 int count = 0;
2438 /* normal case */
2439 if (e->type != EXPR_INITIALIZER)
2440 return e;
2442 FOR_EACH_PTR(e->expr_list, p) {
2443 if (!v)
2444 v = p;
2445 count++;
2446 } END_FOR_EACH_PTR(p);
2447 if (count != 1)
2448 return NULL;
2449 switch(v->type) {
2450 case EXPR_INITIALIZER:
2451 case EXPR_INDEX:
2452 case EXPR_IDENTIFIER:
2453 return NULL;
2454 default:
2455 break;
2457 if (nested)
2458 warning(e->pos, "braces around scalar initializer");
2459 return v;
2463 * deal with the cases that don't care about subobjects:
2464 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2465 * character array <- string literal, possibly in braces [6.7.8(14)]
2466 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2467 * compound type <- initializer list in braces [6.7.8(16)]
2468 * The last one punts to handle_list_initializer() which, in turn will call
2469 * us for individual elements of the list.
2471 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2472 * the lack of support of wide char stuff in general.
2474 * One note: we need to take care not to evaluate a string literal until
2475 * we know that we *will* handle it right here. Otherwise we would screw
2476 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2477 * { "string", ...} - we need to preserve that string literal recognizable
2478 * until we dig into the inner struct.
2480 static int handle_simple_initializer(struct expression **ep, int nested,
2481 int class, struct symbol *ctype)
2483 int is_string = is_string_type(ctype);
2484 struct expression *e = *ep, *p;
2485 struct symbol *type;
2487 if (!e)
2488 return 0;
2490 /* scalar */
2491 if (!(class & TYPE_COMPOUND)) {
2492 e = handle_scalar(e, nested);
2493 if (!e)
2494 return 0;
2495 *ep = e;
2496 if (!evaluate_expression(e))
2497 return 1;
2498 compatible_assignment_types(e, ctype, ep, "initializer");
2499 return 1;
2503 * sublist; either a string, or we dig in; the latter will deal with
2504 * pathologies, so we don't need anything fancy here.
2506 if (e->type == EXPR_INITIALIZER) {
2507 if (is_string) {
2508 struct expression *v = NULL;
2509 int count = 0;
2511 FOR_EACH_PTR(e->expr_list, p) {
2512 if (!v)
2513 v = p;
2514 count++;
2515 } END_FOR_EACH_PTR(p);
2516 if (count == 1 && is_string_literal(&v)) {
2517 *ep = e = v;
2518 goto String;
2521 handle_list_initializer(e, class, ctype);
2522 return 1;
2525 /* string */
2526 if (is_string_literal(&e)) {
2527 /* either we are doing array of char, or we'll have to dig in */
2528 if (is_string) {
2529 *ep = e;
2530 goto String;
2532 return 0;
2534 /* struct or union can be initialized by compatible */
2535 if (class != TYPE_COMPOUND)
2536 return 0;
2537 type = evaluate_expression(e);
2538 if (!type)
2539 return 0;
2540 if (ctype->type == SYM_NODE)
2541 ctype = ctype->ctype.base_type;
2542 if (type->type == SYM_NODE)
2543 type = type->ctype.base_type;
2544 if (ctype == type)
2545 return 1;
2546 return 0;
2548 String:
2549 p = alloc_expression(e->pos, EXPR_STRING);
2550 *p = *e;
2551 type = evaluate_expression(p);
2552 if (ctype->bit_size != -1 &&
2553 ctype->bit_size + bits_in_char < type->bit_size) {
2554 warning(e->pos,
2555 "too long initializer-string for array of char");
2557 *ep = p;
2558 return 1;
2561 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2563 struct symbol *type;
2564 int class = classify_type(ctype, &type);
2565 if (!handle_simple_initializer(ep, 0, class, ctype))
2566 expression_error(*ep, "invalid initializer");
2569 static struct symbol *evaluate_cast(struct expression *expr)
2571 struct expression *target = expr->cast_expression;
2572 struct symbol *ctype;
2573 struct symbol *t1, *t2;
2574 int class1, class2;
2575 int as1 = 0, as2 = 0;
2577 if (!target)
2578 return NULL;
2581 * Special case: a cast can be followed by an
2582 * initializer, in which case we need to pass
2583 * the type value down to that initializer rather
2584 * than trying to evaluate it as an expression
2586 * A more complex case is when the initializer is
2587 * dereferenced as part of a post-fix expression.
2588 * We need to produce an expression that can be dereferenced.
2590 if (target->type == EXPR_INITIALIZER) {
2591 struct symbol *sym = expr->cast_type;
2592 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2594 sym->initializer = target;
2595 evaluate_symbol(sym);
2597 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2598 addr->symbol = sym;
2600 expr->type = EXPR_PREOP;
2601 expr->op = '*';
2602 expr->unop = addr;
2603 expr->ctype = sym;
2605 return sym;
2608 ctype = examine_symbol_type(expr->cast_type);
2609 expr->ctype = ctype;
2610 expr->cast_type = ctype;
2612 evaluate_expression(target);
2613 degenerate(target);
2615 class1 = classify_type(ctype, &t1);
2617 /* cast to non-integer type -> not an integer constant expression */
2618 if (!is_int(class1))
2619 expr->flags = 0;
2620 /* if argument turns out to be not an integer constant expression *and*
2621 it was not a floating literal to start with -> too bad */
2622 else if (expr->flags == Int_const_expr &&
2623 !(target->flags & Int_const_expr))
2624 expr->flags = 0;
2626 * You can always throw a value away by casting to
2627 * "void" - that's an implicit "force". Note that
2628 * the same is _not_ true of "void *".
2630 if (t1 == &void_ctype)
2631 goto out;
2633 if (class1 & TYPE_COMPOUND)
2634 warning(expr->pos, "cast to non-scalar");
2636 t2 = target->ctype;
2637 if (!t2) {
2638 expression_error(expr, "cast from unknown type");
2639 goto out;
2641 class2 = classify_type(t2, &t2);
2643 if (class2 & TYPE_COMPOUND)
2644 warning(expr->pos, "cast from non-scalar");
2646 if (expr->type == EXPR_FORCE_CAST)
2647 goto out;
2649 /* allowed cast unfouls */
2650 if (class2 & TYPE_FOULED)
2651 t2 = unfoul(t2);
2653 if (t1 != t2) {
2654 if (class1 & TYPE_RESTRICT)
2655 warning(expr->pos, "cast to restricted type");
2656 if (class2 & TYPE_RESTRICT)
2657 warning(expr->pos, "cast from restricted type");
2660 if (t1 == &ulong_ctype)
2661 as1 = -1;
2662 else if (class1 == TYPE_PTR) {
2663 examine_pointer_target(t1);
2664 as1 = t1->ctype.as;
2667 if (t2 == &ulong_ctype)
2668 as2 = -1;
2669 else if (class2 == TYPE_PTR) {
2670 examine_pointer_target(t2);
2671 as2 = t2->ctype.as;
2674 if (!as1 && as2 > 0)
2675 warning(expr->pos, "cast removes address space of expression");
2676 if (as1 > 0 && as2 > 0 && as1 != as2)
2677 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2678 if (as1 > 0 && !as2 &&
2679 !is_null_pointer_constant(target) && Wcast_to_address_space)
2680 warning(expr->pos,
2681 "cast adds address space to expression (<asn:%d>)", as1);
2683 if (!(t1->ctype.modifiers & MOD_PTRINHERIT) && class1 == TYPE_PTR &&
2684 !as1 && (target->flags & Int_const_expr)) {
2685 if (t1->ctype.base_type == &void_ctype) {
2686 if (is_zero_constant(target)) {
2687 /* NULL */
2688 expr->type = EXPR_VALUE;
2689 expr->ctype = &null_ctype;
2690 expr->value = 0;
2691 return ctype;
2695 out:
2696 return ctype;
2700 * Evaluate a call expression with a symbol. This
2701 * should expand inline functions, and evaluate
2702 * builtins.
2704 static int evaluate_symbol_call(struct expression *expr)
2706 struct expression *fn = expr->fn;
2707 struct symbol *ctype = fn->ctype;
2709 if (fn->type != EXPR_PREOP)
2710 return 0;
2712 if (ctype->op && ctype->op->evaluate)
2713 return ctype->op->evaluate(expr);
2715 if (ctype->ctype.modifiers & MOD_INLINE) {
2716 int ret;
2717 struct symbol *curr = current_fn;
2718 current_fn = ctype->ctype.base_type;
2720 ret = inline_function(expr, ctype);
2722 /* restore the old function */
2723 current_fn = curr;
2724 return ret;
2727 return 0;
2730 static struct symbol *evaluate_call(struct expression *expr)
2732 int args, fnargs;
2733 struct symbol *ctype, *sym;
2734 struct expression *fn = expr->fn;
2735 struct expression_list *arglist = expr->args;
2737 if (!evaluate_expression(fn))
2738 return NULL;
2739 sym = ctype = fn->ctype;
2740 if (ctype->type == SYM_NODE)
2741 ctype = ctype->ctype.base_type;
2742 if (ctype->type == SYM_PTR)
2743 ctype = get_base_type(ctype);
2745 if (ctype->type != SYM_FN) {
2746 struct expression *arg;
2747 expression_error(expr, "not a function %s",
2748 show_ident(sym->ident));
2749 /* do typechecking in arguments */
2750 FOR_EACH_PTR (arglist, arg) {
2751 evaluate_expression(arg);
2752 } END_FOR_EACH_PTR(arg);
2753 return NULL;
2756 examine_fn_arguments(ctype);
2757 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
2758 sym->op && sym->op->args) {
2759 if (!sym->op->args(expr))
2760 return NULL;
2761 } else {
2762 if (!evaluate_arguments(sym, ctype, arglist))
2763 return NULL;
2764 args = expression_list_size(expr->args);
2765 fnargs = symbol_list_size(ctype->arguments);
2766 if (args < fnargs)
2767 expression_error(expr,
2768 "not enough arguments for function %s",
2769 show_ident(sym->ident));
2770 if (args > fnargs && !ctype->variadic)
2771 expression_error(expr,
2772 "too many arguments for function %s",
2773 show_ident(sym->ident));
2775 if (sym->type == SYM_NODE) {
2776 if (evaluate_symbol_call(expr))
2777 return expr->ctype;
2779 expr->ctype = ctype->ctype.base_type;
2780 return expr->ctype;
2783 static struct symbol *evaluate_offsetof(struct expression *expr)
2785 struct expression *e = expr->down;
2786 struct symbol *ctype = expr->in;
2787 int class;
2789 if (expr->op == '.') {
2790 struct symbol *field;
2791 int offset = 0;
2792 if (!ctype) {
2793 expression_error(expr, "expected structure or union");
2794 return NULL;
2796 examine_symbol_type(ctype);
2797 class = classify_type(ctype, &ctype);
2798 if (class != TYPE_COMPOUND) {
2799 expression_error(expr, "expected structure or union");
2800 return NULL;
2803 field = find_identifier(expr->ident, ctype->symbol_list, &offset);
2804 if (!field) {
2805 expression_error(expr, "unknown member");
2806 return NULL;
2808 ctype = field;
2809 expr->type = EXPR_VALUE;
2810 expr->flags = Int_const_expr;
2811 expr->value = offset;
2812 expr->taint = 0;
2813 expr->ctype = size_t_ctype;
2814 } else {
2815 if (!ctype) {
2816 expression_error(expr, "expected structure or union");
2817 return NULL;
2819 examine_symbol_type(ctype);
2820 class = classify_type(ctype, &ctype);
2821 if (class != (TYPE_COMPOUND | TYPE_PTR)) {
2822 expression_error(expr, "expected array");
2823 return NULL;
2825 ctype = ctype->ctype.base_type;
2826 if (!expr->index) {
2827 expr->type = EXPR_VALUE;
2828 expr->flags = Int_const_expr;
2829 expr->value = 0;
2830 expr->taint = 0;
2831 expr->ctype = size_t_ctype;
2832 } else {
2833 struct expression *idx = expr->index, *m;
2834 struct symbol *i_type = evaluate_expression(idx);
2835 int i_class = classify_type(i_type, &i_type);
2836 if (!is_int(i_class)) {
2837 expression_error(expr, "non-integer index");
2838 return NULL;
2840 unrestrict(idx, i_class, &i_type);
2841 idx = cast_to(idx, size_t_ctype);
2842 m = alloc_const_expression(expr->pos,
2843 ctype->bit_size >> 3);
2844 m->ctype = size_t_ctype;
2845 m->flags = Int_const_expr;
2846 expr->type = EXPR_BINOP;
2847 expr->left = idx;
2848 expr->right = m;
2849 expr->op = '*';
2850 expr->ctype = size_t_ctype;
2851 expr->flags = m->flags & idx->flags & Int_const_expr;
2854 if (e) {
2855 struct expression *copy = __alloc_expression(0);
2856 *copy = *expr;
2857 if (e->type == EXPR_OFFSETOF)
2858 e->in = ctype;
2859 if (!evaluate_expression(e))
2860 return NULL;
2861 expr->type = EXPR_BINOP;
2862 expr->flags = e->flags & copy->flags & Int_const_expr;
2863 expr->op = '+';
2864 expr->ctype = size_t_ctype;
2865 expr->left = copy;
2866 expr->right = e;
2868 return size_t_ctype;
2871 struct symbol *evaluate_expression(struct expression *expr)
2873 if (!expr)
2874 return NULL;
2875 if (expr->ctype)
2876 return expr->ctype;
2878 switch (expr->type) {
2879 case EXPR_VALUE:
2880 case EXPR_FVALUE:
2881 expression_error(expr, "value expression without a type");
2882 return NULL;
2883 case EXPR_STRING:
2884 return evaluate_string(expr);
2885 case EXPR_SYMBOL:
2886 return evaluate_symbol_expression(expr);
2887 case EXPR_BINOP:
2888 if (!evaluate_expression(expr->left))
2889 return NULL;
2890 if (!evaluate_expression(expr->right))
2891 return NULL;
2892 return evaluate_binop(expr);
2893 case EXPR_LOGICAL:
2894 return evaluate_logical(expr);
2895 case EXPR_COMMA:
2896 evaluate_expression(expr->left);
2897 if (!evaluate_expression(expr->right))
2898 return NULL;
2899 return evaluate_comma(expr);
2900 case EXPR_COMPARE:
2901 if (!evaluate_expression(expr->left))
2902 return NULL;
2903 if (!evaluate_expression(expr->right))
2904 return NULL;
2905 return evaluate_compare(expr);
2906 case EXPR_ASSIGNMENT:
2907 if (!evaluate_expression(expr->left))
2908 return NULL;
2909 if (!evaluate_expression(expr->right))
2910 return NULL;
2911 return evaluate_assignment(expr);
2912 case EXPR_PREOP:
2913 if (!evaluate_expression(expr->unop))
2914 return NULL;
2915 return evaluate_preop(expr);
2916 case EXPR_POSTOP:
2917 if (!evaluate_expression(expr->unop))
2918 return NULL;
2919 return evaluate_postop(expr);
2920 case EXPR_CAST:
2921 case EXPR_FORCE_CAST:
2922 case EXPR_IMPLIED_CAST:
2923 return evaluate_cast(expr);
2924 case EXPR_SIZEOF:
2925 return evaluate_sizeof(expr);
2926 case EXPR_PTRSIZEOF:
2927 return evaluate_ptrsizeof(expr);
2928 case EXPR_ALIGNOF:
2929 return evaluate_alignof(expr);
2930 case EXPR_DEREF:
2931 return evaluate_member_dereference(expr);
2932 case EXPR_CALL:
2933 return evaluate_call(expr);
2934 case EXPR_SELECT:
2935 case EXPR_CONDITIONAL:
2936 return evaluate_conditional_expression(expr);
2937 case EXPR_STATEMENT:
2938 expr->ctype = evaluate_statement(expr->statement);
2939 return expr->ctype;
2941 case EXPR_LABEL:
2942 expr->ctype = &ptr_ctype;
2943 return &ptr_ctype;
2945 case EXPR_TYPE:
2946 /* Evaluate the type of the symbol .. */
2947 evaluate_symbol(expr->symbol);
2948 /* .. but the type of the _expression_ is a "type" */
2949 expr->ctype = &type_ctype;
2950 return &type_ctype;
2952 case EXPR_OFFSETOF:
2953 return evaluate_offsetof(expr);
2955 /* These can not exist as stand-alone expressions */
2956 case EXPR_INITIALIZER:
2957 case EXPR_IDENTIFIER:
2958 case EXPR_INDEX:
2959 case EXPR_POS:
2960 expression_error(expr, "internal front-end error: initializer in expression");
2961 return NULL;
2962 case EXPR_SLICE:
2963 expression_error(expr, "internal front-end error: SLICE re-evaluated");
2964 return NULL;
2966 return NULL;
2969 static void check_duplicates(struct symbol *sym)
2971 int declared = 0;
2972 struct symbol *next = sym;
2974 while ((next = next->same_symbol) != NULL) {
2975 const char *typediff;
2976 evaluate_symbol(next);
2977 declared++;
2978 typediff = type_difference(&sym->ctype, &next->ctype, 0, 0);
2979 if (typediff) {
2980 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2981 show_ident(sym->ident),
2982 stream_name(next->pos.stream), next->pos.line, typediff);
2983 return;
2986 if (!declared) {
2987 unsigned long mod = sym->ctype.modifiers;
2988 if (mod & (MOD_STATIC | MOD_REGISTER))
2989 return;
2990 if (!(mod & MOD_TOPLEVEL))
2991 return;
2992 if (!Wdecl)
2993 return;
2994 if (sym->ident == &main_ident)
2995 return;
2996 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
3000 static struct symbol *evaluate_symbol(struct symbol *sym)
3002 struct symbol *base_type;
3004 if (!sym)
3005 return sym;
3006 if (sym->evaluated)
3007 return sym;
3008 sym->evaluated = 1;
3010 sym = examine_symbol_type(sym);
3011 base_type = get_base_type(sym);
3012 if (!base_type)
3013 return NULL;
3015 /* Evaluate the initializers */
3016 if (sym->initializer)
3017 evaluate_initializer(sym, &sym->initializer);
3019 /* And finally, evaluate the body of the symbol too */
3020 if (base_type->type == SYM_FN) {
3021 struct symbol *curr = current_fn;
3023 current_fn = base_type;
3025 examine_fn_arguments(base_type);
3026 if (!base_type->stmt && base_type->inline_stmt)
3027 uninline(sym);
3028 if (base_type->stmt)
3029 evaluate_statement(base_type->stmt);
3031 current_fn = curr;
3034 return base_type;
3037 void evaluate_symbol_list(struct symbol_list *list)
3039 struct symbol *sym;
3041 FOR_EACH_PTR(list, sym) {
3042 evaluate_symbol(sym);
3043 check_duplicates(sym);
3044 } END_FOR_EACH_PTR(sym);
3047 static struct symbol *evaluate_return_expression(struct statement *stmt)
3049 struct expression *expr = stmt->expression;
3050 struct symbol *fntype;
3052 evaluate_expression(expr);
3053 fntype = current_fn->ctype.base_type;
3054 if (!fntype || fntype == &void_ctype) {
3055 if (expr && expr->ctype != &void_ctype)
3056 expression_error(expr, "return expression in %s function", fntype?"void":"typeless");
3057 if (expr && Wreturn_void)
3058 warning(stmt->pos, "returning void-valued expression");
3059 return NULL;
3062 if (!expr) {
3063 sparse_error(stmt->pos, "return with no return value");
3064 return NULL;
3066 if (!expr->ctype)
3067 return NULL;
3068 compatible_assignment_types(expr, fntype, &stmt->expression, "return expression");
3069 return NULL;
3072 static void evaluate_if_statement(struct statement *stmt)
3074 if (!stmt->if_conditional)
3075 return;
3077 evaluate_conditional(stmt->if_conditional, 0);
3078 evaluate_statement(stmt->if_true);
3079 evaluate_statement(stmt->if_false);
3082 static void evaluate_iterator(struct statement *stmt)
3084 evaluate_conditional(stmt->iterator_pre_condition, 1);
3085 evaluate_conditional(stmt->iterator_post_condition,1);
3086 evaluate_statement(stmt->iterator_pre_statement);
3087 evaluate_statement(stmt->iterator_statement);
3088 evaluate_statement(stmt->iterator_post_statement);
3091 static void verify_output_constraint(struct expression *expr, const char *constraint)
3093 switch (*constraint) {
3094 case '=': /* Assignment */
3095 case '+': /* Update */
3096 break;
3097 default:
3098 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
3102 static void verify_input_constraint(struct expression *expr, const char *constraint)
3104 switch (*constraint) {
3105 case '=': /* Assignment */
3106 case '+': /* Update */
3107 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
3111 static void evaluate_asm_statement(struct statement *stmt)
3113 struct expression *expr;
3114 int state;
3116 expr = stmt->asm_string;
3117 if (!expr || expr->type != EXPR_STRING) {
3118 sparse_error(stmt->pos, "need constant string for inline asm");
3119 return;
3122 state = 0;
3123 FOR_EACH_PTR(stmt->asm_outputs, expr) {
3124 struct ident *ident;
3126 switch (state) {
3127 case 0: /* Identifier */
3128 state = 1;
3129 ident = (struct ident *)expr;
3130 continue;
3132 case 1: /* Constraint */
3133 state = 2;
3134 if (!expr || expr->type != EXPR_STRING) {
3135 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
3136 *THIS_ADDRESS(expr) = NULL;
3137 continue;
3139 verify_output_constraint(expr, expr->string->data);
3140 continue;
3142 case 2: /* Expression */
3143 state = 0;
3144 if (!evaluate_expression(expr))
3145 return;
3146 if (!lvalue_expression(expr))
3147 warning(expr->pos, "asm output is not an lvalue");
3148 evaluate_assign_to(expr, expr->ctype);
3149 continue;
3151 } END_FOR_EACH_PTR(expr);
3153 state = 0;
3154 FOR_EACH_PTR(stmt->asm_inputs, expr) {
3155 struct ident *ident;
3157 switch (state) {
3158 case 0: /* Identifier */
3159 state = 1;
3160 ident = (struct ident *)expr;
3161 continue;
3163 case 1: /* Constraint */
3164 state = 2;
3165 if (!expr || expr->type != EXPR_STRING) {
3166 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
3167 *THIS_ADDRESS(expr) = NULL;
3168 continue;
3170 verify_input_constraint(expr, expr->string->data);
3171 continue;
3173 case 2: /* Expression */
3174 state = 0;
3175 if (!evaluate_expression(expr))
3176 return;
3177 continue;
3179 } END_FOR_EACH_PTR(expr);
3181 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
3182 if (!expr) {
3183 sparse_error(stmt->pos, "bad asm output");
3184 return;
3186 if (expr->type == EXPR_STRING)
3187 continue;
3188 expression_error(expr, "asm clobber is not a string");
3189 } END_FOR_EACH_PTR(expr);
3192 static void evaluate_case_statement(struct statement *stmt)
3194 evaluate_expression(stmt->case_expression);
3195 evaluate_expression(stmt->case_to);
3196 evaluate_statement(stmt->case_statement);
3199 static void check_case_type(struct expression *switch_expr,
3200 struct expression *case_expr,
3201 struct expression **enumcase)
3203 struct symbol *switch_type, *case_type;
3204 int sclass, cclass;
3206 if (!case_expr)
3207 return;
3209 switch_type = switch_expr->ctype;
3210 case_type = evaluate_expression(case_expr);
3212 if (!switch_type || !case_type)
3213 goto Bad;
3214 if (enumcase) {
3215 if (*enumcase)
3216 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
3217 else if (is_enum_type(case_type))
3218 *enumcase = case_expr;
3221 sclass = classify_type(switch_type, &switch_type);
3222 cclass = classify_type(case_type, &case_type);
3224 /* both should be arithmetic */
3225 if (!(sclass & cclass & TYPE_NUM))
3226 goto Bad;
3228 /* neither should be floating */
3229 if ((sclass | cclass) & TYPE_FLOAT)
3230 goto Bad;
3232 /* if neither is restricted, we are OK */
3233 if (!((sclass | cclass) & TYPE_RESTRICT))
3234 return;
3236 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
3237 cclass, sclass, case_type, switch_type))
3238 warning(case_expr->pos, "restricted degrades to integer");
3240 return;
3242 Bad:
3243 expression_error(case_expr, "incompatible types for 'case' statement");
3246 static void evaluate_switch_statement(struct statement *stmt)
3248 struct symbol *sym;
3249 struct expression *enumcase = NULL;
3250 struct expression **enumcase_holder = &enumcase;
3251 struct expression *sel = stmt->switch_expression;
3253 evaluate_expression(sel);
3254 evaluate_statement(stmt->switch_statement);
3255 if (!sel)
3256 return;
3257 if (sel->ctype && is_enum_type(sel->ctype))
3258 enumcase_holder = NULL; /* Only check cases against switch */
3260 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
3261 struct statement *case_stmt = sym->stmt;
3262 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
3263 check_case_type(sel, case_stmt->case_to, enumcase_holder);
3264 } END_FOR_EACH_PTR(sym);
3267 struct symbol *evaluate_statement(struct statement *stmt)
3269 if (!stmt)
3270 return NULL;
3272 switch (stmt->type) {
3273 case STMT_DECLARATION: {
3274 struct symbol *s;
3275 FOR_EACH_PTR(stmt->declaration, s) {
3276 evaluate_symbol(s);
3277 } END_FOR_EACH_PTR(s);
3278 return NULL;
3281 case STMT_RETURN:
3282 return evaluate_return_expression(stmt);
3284 case STMT_EXPRESSION:
3285 if (!evaluate_expression(stmt->expression))
3286 return NULL;
3287 if (stmt->expression->ctype == &null_ctype)
3288 stmt->expression = cast_to(stmt->expression, &ptr_ctype);
3289 return degenerate(stmt->expression);
3291 case STMT_COMPOUND: {
3292 struct statement *s;
3293 struct symbol *type = NULL;
3295 /* Evaluate the return symbol in the compound statement */
3296 evaluate_symbol(stmt->ret);
3299 * Then, evaluate each statement, making the type of the
3300 * compound statement be the type of the last statement
3302 type = evaluate_statement(stmt->args);
3303 FOR_EACH_PTR(stmt->stmts, s) {
3304 type = evaluate_statement(s);
3305 } END_FOR_EACH_PTR(s);
3306 if (!type)
3307 type = &void_ctype;
3308 return type;
3310 case STMT_IF:
3311 evaluate_if_statement(stmt);
3312 return NULL;
3313 case STMT_ITERATOR:
3314 evaluate_iterator(stmt);
3315 return NULL;
3316 case STMT_SWITCH:
3317 evaluate_switch_statement(stmt);
3318 return NULL;
3319 case STMT_CASE:
3320 evaluate_case_statement(stmt);
3321 return NULL;
3322 case STMT_LABEL:
3323 return evaluate_statement(stmt->label_statement);
3324 case STMT_GOTO:
3325 evaluate_expression(stmt->goto_expression);
3326 return NULL;
3327 case STMT_NONE:
3328 break;
3329 case STMT_ASM:
3330 evaluate_asm_statement(stmt);
3331 return NULL;
3332 case STMT_CONTEXT:
3333 evaluate_expression(stmt->expression);
3334 return NULL;
3335 case STMT_RANGE:
3336 evaluate_expression(stmt->range_expression);
3337 evaluate_expression(stmt->range_low);
3338 evaluate_expression(stmt->range_high);
3339 return NULL;
3341 return NULL;