saner warnings for restricted types
[smatch.git] / evaluate.c
blob777f60344e8bb1d57b2b4326d2a757c1d80e1c4d
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,
348 TYPE_FN = 128,
351 static inline int classify_type(struct symbol *type, struct symbol **base)
353 static int type_class[SYM_BAD + 1] = {
354 [SYM_PTR] = TYPE_PTR,
355 [SYM_FN] = TYPE_PTR | TYPE_FN,
356 [SYM_ARRAY] = TYPE_PTR | TYPE_COMPOUND,
357 [SYM_STRUCT] = TYPE_COMPOUND,
358 [SYM_UNION] = TYPE_COMPOUND,
359 [SYM_BITFIELD] = TYPE_NUM | TYPE_BITFIELD,
360 [SYM_RESTRICT] = TYPE_NUM | TYPE_RESTRICT,
361 [SYM_FOULED] = TYPE_NUM | TYPE_RESTRICT | TYPE_FOULED,
363 if (type->type == SYM_NODE)
364 type = type->ctype.base_type;
365 if (type->type == SYM_ENUM)
366 type = type->ctype.base_type;
367 *base = type;
368 if (type->type == SYM_BASETYPE) {
369 if (type->ctype.base_type == &int_type)
370 return TYPE_NUM;
371 if (type->ctype.base_type == &fp_type)
372 return TYPE_NUM | TYPE_FLOAT;
374 return type_class[type->type];
377 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
379 static inline int is_string_type(struct symbol *type)
381 if (type->type == SYM_NODE)
382 type = type->ctype.base_type;
383 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
386 static struct symbol *bad_expr_type(struct expression *expr)
388 sparse_error(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
389 switch (expr->type) {
390 case EXPR_BINOP:
391 case EXPR_COMPARE:
392 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
393 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
394 break;
395 case EXPR_PREOP:
396 case EXPR_POSTOP:
397 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
398 break;
399 default:
400 break;
403 expr->flags = 0;
404 return expr->ctype = &bad_ctype;
407 static int restricted_value(struct expression *v, struct symbol *type)
409 if (v->type != EXPR_VALUE)
410 return 1;
411 if (v->value != 0)
412 return 1;
413 return 0;
416 static int restricted_binop(int op, struct symbol *type)
418 switch (op) {
419 case '&':
420 case '=':
421 case SPECIAL_AND_ASSIGN:
422 case SPECIAL_OR_ASSIGN:
423 case SPECIAL_XOR_ASSIGN:
424 return 1; /* unfoul */
425 case '|':
426 case '^':
427 case '?':
428 return 2; /* keep fouled */
429 case SPECIAL_EQUAL:
430 case SPECIAL_NOTEQUAL:
431 return 3; /* warn if fouled */
432 default:
433 return 0; /* warn */
437 static int restricted_unop(int op, struct symbol **type)
439 if (op == '~') {
440 if ((*type)->bit_size < bits_in_int)
441 *type = befoul(*type);
442 return 0;
443 } if (op == '+')
444 return 0;
445 return 1;
448 /* type should be SYM_FOULED */
449 static inline struct symbol *unfoul(struct symbol *type)
451 return type->ctype.base_type;
454 static struct symbol *restricted_binop_type(int op,
455 struct expression *left,
456 struct expression *right,
457 int lclass, int rclass,
458 struct symbol *ltype,
459 struct symbol *rtype)
461 struct symbol *ctype = NULL;
462 if (lclass & TYPE_RESTRICT) {
463 if (rclass & TYPE_RESTRICT) {
464 if (ltype == rtype) {
465 ctype = ltype;
466 } else if (lclass & TYPE_FOULED) {
467 if (unfoul(ltype) == rtype)
468 ctype = ltype;
469 } else if (rclass & TYPE_FOULED) {
470 if (unfoul(rtype) == ltype)
471 ctype = rtype;
473 } else {
474 if (!restricted_value(right, ltype))
475 ctype = ltype;
477 } else if (!restricted_value(left, rtype))
478 ctype = rtype;
480 if (ctype) {
481 switch (restricted_binop(op, ctype)) {
482 case 1:
483 if ((lclass ^ rclass) & TYPE_FOULED)
484 ctype = unfoul(ctype);
485 break;
486 case 3:
487 if (!(lclass & rclass & TYPE_FOULED))
488 break;
489 case 0:
490 ctype = NULL;
491 default:
492 break;
496 return ctype;
499 static inline void unrestrict(struct expression *expr,
500 int class, struct symbol **ctype)
502 if (class & TYPE_RESTRICT) {
503 if (class & TYPE_FOULED)
504 *ctype = unfoul(*ctype);
505 warning(expr->pos, "%sdegrades to integer",
506 show_typename(*ctype));
507 *ctype = (*ctype)->ctype.base_type; /* get to arithmetic type */
511 static struct symbol *usual_conversions(int op,
512 struct expression *left,
513 struct expression *right,
514 int lclass, int rclass,
515 struct symbol *ltype,
516 struct symbol *rtype)
518 struct symbol *ctype;
520 warn_for_different_enum_types(right->pos, left->ctype, right->ctype);
522 if ((lclass | rclass) & TYPE_RESTRICT)
523 goto Restr;
525 Normal:
526 if (!(lclass & TYPE_FLOAT)) {
527 if (!(rclass & TYPE_FLOAT))
528 return bigger_int_type(ltype, rtype);
529 else
530 return rtype;
531 } else if (rclass & TYPE_FLOAT) {
532 unsigned long lmod = ltype->ctype.modifiers;
533 unsigned long rmod = rtype->ctype.modifiers;
534 if (rmod & ~lmod & (MOD_LONG | MOD_LONGLONG))
535 return rtype;
536 else
537 return ltype;
538 } else
539 return ltype;
541 Restr:
542 ctype = restricted_binop_type(op, left, right,
543 lclass, rclass, ltype, rtype);
544 if (ctype)
545 return ctype;
547 unrestrict(left, lclass, &ltype);
548 unrestrict(right, rclass, &rtype);
550 goto Normal;
553 static inline int lvalue_expression(struct expression *expr)
555 return expr->type == EXPR_PREOP && expr->op == '*';
558 static inline int is_function(struct symbol *type)
560 return type && type->type == SYM_FN;
563 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *itype)
565 struct expression *index = expr->right;
566 struct symbol *ctype, *base;
567 int multiply;
569 classify_type(degenerate(expr->left), &ctype);
570 base = examine_pointer_target(ctype);
572 if (!base) {
573 expression_error(expr, "missing type information");
574 return NULL;
576 if (is_function(base)) {
577 expression_error(expr, "arithmetics on pointers to functions");
578 return NULL;
581 /* Get the size of whatever the pointer points to */
582 multiply = base->bit_size >> 3;
584 if (ctype == &null_ctype)
585 ctype = &ptr_ctype;
586 expr->ctype = ctype;
588 if (multiply == 1 && itype->bit_size >= bits_in_pointer)
589 return ctype;
591 if (index->type == EXPR_VALUE) {
592 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
593 unsigned long long v = index->value, mask;
594 mask = 1ULL << (itype->bit_size - 1);
595 if (v & mask)
596 v |= -mask;
597 else
598 v &= mask - 1;
599 v *= multiply;
600 mask = 1ULL << (bits_in_pointer - 1);
601 v &= mask | (mask - 1);
602 val->value = v;
603 val->ctype = ssize_t_ctype;
604 expr->right = val;
605 return ctype;
608 if (itype->bit_size < bits_in_pointer)
609 index = cast_to(index, ssize_t_ctype);
611 if (multiply > 1) {
612 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
613 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
615 val->ctype = ssize_t_ctype;
616 val->value = multiply;
618 mul->op = '*';
619 mul->ctype = ssize_t_ctype;
620 mul->left = index;
621 mul->right = val;
622 index = mul;
625 expr->right = index;
626 return ctype;
629 static void examine_fn_arguments(struct symbol *fn);
631 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
633 const char *type_difference(struct ctype *c1, struct ctype *c2,
634 unsigned long mod1, unsigned long mod2)
636 unsigned long as1 = c1->as, as2 = c2->as;
637 struct symbol *t1 = c1->base_type;
638 struct symbol *t2 = c2->base_type;
639 int move1 = 1, move2 = 1;
640 mod1 |= c1->modifiers;
641 mod2 |= c2->modifiers;
642 for (;;) {
643 unsigned long diff;
644 int type;
645 struct symbol *base1 = t1->ctype.base_type;
646 struct symbol *base2 = t2->ctype.base_type;
649 * FIXME! Collect alignment and context too here!
651 if (move1) {
652 if (t1 && t1->type != SYM_PTR) {
653 mod1 |= t1->ctype.modifiers;
654 as1 |= t1->ctype.as;
656 move1 = 0;
659 if (move2) {
660 if (t2 && t2->type != SYM_PTR) {
661 mod2 |= t2->ctype.modifiers;
662 as2 |= t2->ctype.as;
664 move2 = 0;
667 if (t1 == t2)
668 break;
669 if (!t1 || !t2)
670 return "different types";
672 if (t1->type == SYM_NODE || t1->type == SYM_ENUM) {
673 t1 = base1;
674 move1 = 1;
675 if (!t1)
676 return "bad types";
677 continue;
680 if (t2->type == SYM_NODE || t2->type == SYM_ENUM) {
681 t2 = base2;
682 move2 = 1;
683 if (!t2)
684 return "bad types";
685 continue;
688 move1 = move2 = 1;
689 type = t1->type;
690 if (type != t2->type)
691 return "different base types";
693 switch (type) {
694 default:
695 sparse_error(t1->pos,
696 "internal error: bad type in derived(%d)",
697 type);
698 return "bad types";
699 case SYM_RESTRICT:
700 case SYM_UNION:
701 case SYM_STRUCT:
702 return "different base types";
703 case SYM_ARRAY:
704 /* XXX: we ought to compare sizes */
705 break;
706 case SYM_PTR:
707 if (Waddress_space && as1 != as2)
708 return "different address spaces";
709 /* MOD_SPECIFIER is due to idiocy in parse.c */
710 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SPECIFIER)
711 return "different modifiers";
712 /* we could be lazier here */
713 base1 = examine_pointer_target(t1);
714 base2 = examine_pointer_target(t2);
715 mod1 = t1->ctype.modifiers;
716 as1 = t1->ctype.as;
717 mod2 = t2->ctype.modifiers;
718 as2 = t2->ctype.as;
719 break;
720 case SYM_FN: {
721 struct symbol *arg1, *arg2;
722 int i;
724 if (Waddress_space && as1 != as2)
725 return "different address spaces";
726 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
727 return "different modifiers";
728 mod1 = t1->ctype.modifiers;
729 as1 = t1->ctype.as;
730 mod2 = t2->ctype.modifiers;
731 as2 = t2->ctype.as;
733 if (base1->variadic != base2->variadic)
734 return "incompatible variadic arguments";
735 examine_fn_arguments(t1);
736 examine_fn_arguments(t2);
737 PREPARE_PTR_LIST(t1->arguments, arg1);
738 PREPARE_PTR_LIST(t2->arguments, arg2);
739 i = 1;
740 for (;;) {
741 const char *diffstr;
742 if (!arg1 && !arg2)
743 break;
744 if (!arg1 || !arg2)
745 return "different argument counts";
746 diffstr = type_difference(&arg1->ctype,
747 &arg2->ctype,
748 MOD_IGN, MOD_IGN);
749 if (diffstr) {
750 static char argdiff[80];
751 sprintf(argdiff, "incompatible argument %d (%s)", i, diffstr);
752 return argdiff;
754 NEXT_PTR_LIST(arg1);
755 NEXT_PTR_LIST(arg2);
756 i++;
758 FINISH_PTR_LIST(arg2);
759 FINISH_PTR_LIST(arg1);
760 break;
762 case SYM_BASETYPE:
763 if (Waddress_space && as1 != as2)
764 return "different address spaces";
765 if (base1 != base2)
766 return "different base types";
767 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
768 if (!diff)
769 return NULL;
770 if (diff & MOD_SIZE)
771 return "different type sizes";
772 else if (diff & ~MOD_SIGNEDNESS)
773 return "different modifiers";
774 else
775 return "different signedness";
777 t1 = base1;
778 t2 = base2;
780 if (Waddress_space && as1 != as2)
781 return "different address spaces";
782 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
783 return "different modifiers";
784 return NULL;
787 static void bad_null(struct expression *expr)
789 if (Wnon_pointer_null)
790 warning(expr->pos, "Using plain integer as NULL pointer");
793 static unsigned long target_qualifiers(struct symbol *type)
795 unsigned long mod = type->ctype.modifiers & MOD_IGN;
796 if (type->ctype.base_type && type->ctype.base_type->type == SYM_ARRAY)
797 mod = 0;
798 return mod;
801 static struct symbol *evaluate_ptr_sub(struct expression *expr)
803 const char *typediff;
804 struct symbol *ltype, *rtype;
805 struct expression *l = expr->left;
806 struct expression *r = expr->right;
807 struct symbol *lbase, *rbase;
809 classify_type(degenerate(l), &ltype);
810 classify_type(degenerate(r), &rtype);
812 lbase = examine_pointer_target(ltype);
813 rbase = examine_pointer_target(rtype);
814 typediff = type_difference(&ltype->ctype, &rtype->ctype,
815 target_qualifiers(rtype),
816 target_qualifiers(ltype));
817 if (typediff)
818 expression_error(expr, "subtraction of different types can't work (%s)", typediff);
820 if (is_function(lbase)) {
821 expression_error(expr, "subtraction of functions? Share your drugs");
822 return NULL;
825 expr->ctype = ssize_t_ctype;
826 if (lbase->bit_size > bits_in_char) {
827 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
828 struct expression *div = expr;
829 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
830 unsigned long value = lbase->bit_size >> 3;
832 val->ctype = size_t_ctype;
833 val->value = value;
835 if (value & (value-1)) {
836 if (Wptr_subtraction_blows)
837 warning(expr->pos, "potentially expensive pointer subtraction");
840 sub->op = '-';
841 sub->ctype = ssize_t_ctype;
842 sub->left = l;
843 sub->right = r;
845 div->op = '/';
846 div->left = sub;
847 div->right = val;
850 return ssize_t_ctype;
853 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
855 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
857 struct symbol *ctype;
859 if (!expr)
860 return NULL;
862 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
863 warning(expr->pos, "assignment expression in conditional");
865 ctype = evaluate_expression(expr);
866 if (ctype) {
867 if (is_safe_type(ctype))
868 warning(expr->pos, "testing a 'safe expression'");
871 return ctype;
874 static struct symbol *evaluate_logical(struct expression *expr)
876 if (!evaluate_conditional(expr->left, 0))
877 return NULL;
878 if (!evaluate_conditional(expr->right, 0))
879 return NULL;
881 expr->ctype = &bool_ctype;
882 if (expr->flags) {
883 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
884 expr->flags = 0;
886 return &bool_ctype;
889 static struct symbol *evaluate_binop(struct expression *expr)
891 struct symbol *ltype, *rtype, *ctype;
892 int lclass = classify_type(expr->left->ctype, &ltype);
893 int rclass = classify_type(expr->right->ctype, &rtype);
894 int op = expr->op;
896 if (expr->flags) {
897 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
898 expr->flags = 0;
901 /* number op number */
902 if (lclass & rclass & TYPE_NUM) {
903 if ((lclass | rclass) & TYPE_FLOAT) {
904 switch (op) {
905 case '+': case '-': case '*': case '/':
906 break;
907 default:
908 return bad_expr_type(expr);
912 if (op == SPECIAL_LEFTSHIFT || op == SPECIAL_RIGHTSHIFT) {
913 // shifts do integer promotions, but that's it.
914 unrestrict(expr->left, lclass, &ltype);
915 unrestrict(expr->right, rclass, &rtype);
916 ctype = ltype = integer_promotion(ltype);
917 rtype = integer_promotion(rtype);
918 } else {
919 // The rest do usual conversions
920 ltype = usual_conversions(op, expr->left, expr->right,
921 lclass, rclass, ltype, rtype);
922 ctype = rtype = ltype;
925 expr->left = cast_to(expr->left, ltype);
926 expr->right = cast_to(expr->right, rtype);
927 expr->ctype = ctype;
928 return ctype;
931 /* pointer (+|-) integer */
932 if (lclass & TYPE_PTR && is_int(rclass) && (op == '+' || op == '-')) {
933 unrestrict(expr->right, rclass, &rtype);
934 return evaluate_ptr_add(expr, rtype);
937 /* integer + pointer */
938 if (rclass & TYPE_PTR && is_int(lclass) && op == '+') {
939 struct expression *index = expr->left;
940 unrestrict(index, lclass, &ltype);
941 expr->left = expr->right;
942 expr->right = index;
943 return evaluate_ptr_add(expr, ltype);
946 /* pointer - pointer */
947 if (lclass & rclass & TYPE_PTR && expr->op == '-')
948 return evaluate_ptr_sub(expr);
950 return bad_expr_type(expr);
953 static struct symbol *evaluate_comma(struct expression *expr)
955 expr->ctype = degenerate(expr->right);
956 if (expr->ctype == &null_ctype)
957 expr->ctype = &ptr_ctype;
958 expr->flags &= expr->left->flags & expr->right->flags;
959 return expr->ctype;
962 static int modify_for_unsigned(int op)
964 if (op == '<')
965 op = SPECIAL_UNSIGNED_LT;
966 else if (op == '>')
967 op = SPECIAL_UNSIGNED_GT;
968 else if (op == SPECIAL_LTE)
969 op = SPECIAL_UNSIGNED_LTE;
970 else if (op == SPECIAL_GTE)
971 op = SPECIAL_UNSIGNED_GTE;
972 return op;
975 static inline int is_null_pointer_constant(struct expression *e)
977 if (e->ctype == &null_ctype)
978 return 1;
979 if (!(e->flags & Int_const_expr))
980 return 0;
981 return is_zero_constant(e) ? 2 : 0;
984 static struct symbol *evaluate_compare(struct expression *expr)
986 struct expression *left = expr->left, *right = expr->right;
987 struct symbol *ltype, *rtype, *lbase, *rbase;
988 int lclass = classify_type(degenerate(left), &ltype);
989 int rclass = classify_type(degenerate(right), &rtype);
990 struct symbol *ctype;
991 const char *typediff;
993 if (expr->flags) {
994 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
995 expr->flags = 0;
998 /* Type types? */
999 if (is_type_type(ltype) && is_type_type(rtype))
1000 goto OK;
1002 if (is_safe_type(left->ctype) || is_safe_type(right->ctype))
1003 warning(expr->pos, "testing a 'safe expression'");
1005 /* number on number */
1006 if (lclass & rclass & TYPE_NUM) {
1007 ctype = usual_conversions(expr->op, expr->left, expr->right,
1008 lclass, rclass, ltype, rtype);
1009 expr->left = cast_to(expr->left, ctype);
1010 expr->right = cast_to(expr->right, ctype);
1011 if (ctype->ctype.modifiers & MOD_UNSIGNED)
1012 expr->op = modify_for_unsigned(expr->op);
1013 goto OK;
1016 /* at least one must be a pointer */
1017 if (!((lclass | rclass) & TYPE_PTR))
1018 return bad_expr_type(expr);
1020 /* equality comparisons can be with null pointer constants */
1021 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1022 int is_null1 = is_null_pointer_constant(left);
1023 int is_null2 = is_null_pointer_constant(right);
1024 if (is_null1 == 2)
1025 bad_null(left);
1026 if (is_null2 == 2)
1027 bad_null(right);
1028 if (is_null1 && is_null2) {
1029 int positive = expr->op == SPECIAL_EQUAL;
1030 expr->type = EXPR_VALUE;
1031 expr->value = positive;
1032 goto OK;
1034 if (is_null1 && (rclass & TYPE_PTR)) {
1035 left = cast_to(left, rtype);
1036 goto OK;
1038 if (is_null2 && (lclass & TYPE_PTR)) {
1039 right = cast_to(right, ltype);
1040 goto OK;
1043 /* both should be pointers */
1044 if (!(lclass & rclass & TYPE_PTR))
1045 return bad_expr_type(expr);
1046 expr->op = modify_for_unsigned(expr->op);
1048 lbase = examine_pointer_target(ltype);
1049 rbase = examine_pointer_target(rtype);
1051 /* they also have special treatment for pointers to void */
1052 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1053 if (ltype->ctype.as == rtype->ctype.as) {
1054 if (lbase == &void_ctype) {
1055 right = cast_to(right, ltype);
1056 goto OK;
1058 if (rbase == &void_ctype) {
1059 left = cast_to(left, rtype);
1060 goto OK;
1065 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1066 target_qualifiers(rtype),
1067 target_qualifiers(ltype));
1068 if (!typediff)
1069 goto OK;
1071 expression_error(expr, "incompatible types in comparison expression (%s)", typediff);
1072 return NULL;
1075 expr->ctype = &bool_ctype;
1076 return &bool_ctype;
1080 * NOTE! The degenerate case of "x ? : y", where we don't
1081 * have a true case, this will possibly promote "x" to the
1082 * same type as "y", and thus _change_ the conditional
1083 * test in the expression. But since promotion is "safe"
1084 * for testing, that's OK.
1086 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1088 struct expression **true;
1089 struct symbol *ctype, *ltype, *rtype, *lbase, *rbase;
1090 int lclass, rclass;
1091 const char * typediff;
1092 int qual;
1094 if (!evaluate_conditional(expr->conditional, 0))
1095 return NULL;
1096 if (!evaluate_expression(expr->cond_false))
1097 return NULL;
1099 ctype = degenerate(expr->conditional);
1100 rtype = degenerate(expr->cond_false);
1102 true = &expr->conditional;
1103 ltype = ctype;
1104 if (expr->cond_true) {
1105 if (!evaluate_expression(expr->cond_true))
1106 return NULL;
1107 ltype = degenerate(expr->cond_true);
1108 true = &expr->cond_true;
1111 if (expr->flags) {
1112 int flags = expr->conditional->flags & Int_const_expr;
1113 flags &= (*true)->flags & expr->cond_false->flags;
1114 if (!flags)
1115 expr->flags = 0;
1118 lclass = classify_type(ltype, &ltype);
1119 rclass = classify_type(rtype, &rtype);
1120 if (lclass & rclass & TYPE_NUM) {
1121 ctype = usual_conversions('?', *true, expr->cond_false,
1122 lclass, rclass, ltype, rtype);
1123 *true = cast_to(*true, ctype);
1124 expr->cond_false = cast_to(expr->cond_false, ctype);
1125 goto out;
1128 if ((lclass | rclass) & TYPE_PTR) {
1129 int is_null1 = is_null_pointer_constant(*true);
1130 int is_null2 = is_null_pointer_constant(expr->cond_false);
1132 if (is_null1 && is_null2) {
1133 *true = cast_to(*true, &ptr_ctype);
1134 expr->cond_false = cast_to(expr->cond_false, &ptr_ctype);
1135 ctype = &ptr_ctype;
1136 goto out;
1138 if (is_null1 && (rclass & TYPE_PTR)) {
1139 if (is_null1 == 2)
1140 bad_null(*true);
1141 *true = cast_to(*true, rtype);
1142 ctype = rtype;
1143 goto out;
1145 if (is_null2 && (lclass & TYPE_PTR)) {
1146 if (is_null2 == 2)
1147 bad_null(expr->cond_false);
1148 expr->cond_false = cast_to(expr->cond_false, ltype);
1149 ctype = ltype;
1150 goto out;
1152 if (!(lclass & rclass & TYPE_PTR)) {
1153 typediff = "different types";
1154 goto Err;
1156 /* OK, it's pointer on pointer */
1157 if (ltype->ctype.as != rtype->ctype.as) {
1158 typediff = "different address spaces";
1159 goto Err;
1162 /* need to be lazier here */
1163 lbase = examine_pointer_target(ltype);
1164 rbase = examine_pointer_target(rtype);
1165 qual = target_qualifiers(ltype) | target_qualifiers(rtype);
1167 if (lbase == &void_ctype) {
1168 /* XXX: pointers to function should warn here */
1169 ctype = ltype;
1170 goto Qual;
1173 if (rbase == &void_ctype) {
1174 /* XXX: pointers to function should warn here */
1175 ctype = rtype;
1176 goto Qual;
1178 /* XXX: that should be pointer to composite */
1179 ctype = ltype;
1180 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1181 qual, qual);
1182 if (!typediff)
1183 goto Qual;
1184 goto Err;
1187 /* void on void, struct on same struct, union on same union */
1188 if (ltype == rtype) {
1189 ctype = ltype;
1190 goto out;
1192 typediff = "different base types";
1194 Err:
1195 expression_error(expr, "incompatible types in conditional expression (%s)", typediff);
1196 return NULL;
1198 out:
1199 expr->ctype = ctype;
1200 return ctype;
1202 Qual:
1203 if (qual & ~ctype->ctype.modifiers) {
1204 struct symbol *sym = alloc_symbol(ctype->pos, SYM_PTR);
1205 *sym = *ctype;
1206 sym->ctype.modifiers |= qual;
1207 ctype = sym;
1209 *true = cast_to(*true, ctype);
1210 expr->cond_false = cast_to(expr->cond_false, ctype);
1211 goto out;
1214 /* FP assignments can not do modulo or bit operations */
1215 static int compatible_float_op(int op)
1217 return op == SPECIAL_ADD_ASSIGN ||
1218 op == SPECIAL_SUB_ASSIGN ||
1219 op == SPECIAL_MUL_ASSIGN ||
1220 op == SPECIAL_DIV_ASSIGN;
1223 static int evaluate_assign_op(struct expression *expr)
1225 struct symbol *target = expr->left->ctype;
1226 struct symbol *source = expr->right->ctype;
1227 struct symbol *t, *s;
1228 int tclass = classify_type(target, &t);
1229 int sclass = classify_type(source, &s);
1230 int op = expr->op;
1232 if (tclass & sclass & TYPE_NUM) {
1233 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1234 expression_error(expr, "invalid assignment");
1235 return 0;
1237 if (tclass & TYPE_RESTRICT) {
1238 if (!restricted_binop(op, t)) {
1239 warning(expr->pos, "bad assignment (%s) to %s",
1240 show_special(op), show_typename(t));
1241 expr->right = cast_to(expr->right, target);
1242 return 0;
1244 /* allowed assignments unfoul */
1245 if (sclass & TYPE_FOULED && unfoul(s) == t)
1246 goto Cast;
1247 if (!restricted_value(expr->right, t))
1248 return 1;
1249 } else if (!(sclass & TYPE_RESTRICT))
1250 goto Cast;
1251 /* source and target would better be identical restricted */
1252 if (t == s)
1253 return 1;
1254 warning(expr->pos, "invalid assignment: %s", show_special(op));
1255 info(expr->pos, " left side has type %s", show_typename(t));
1256 info(expr->pos, " right side has type %s", show_typename(s));
1257 expr->right = cast_to(expr->right, target);
1258 return 0;
1260 if (tclass == TYPE_PTR && is_int(sclass)) {
1261 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1262 unrestrict(expr->right, sclass, &s);
1263 evaluate_ptr_add(expr, s);
1264 return 1;
1266 expression_error(expr, "invalid pointer assignment");
1267 return 0;
1270 expression_error(expr, "invalid assignment");
1271 return 0;
1273 Cast:
1274 expr->right = cast_to(expr->right, target);
1275 return 1;
1278 static int whitelist_pointers(struct symbol *t1, struct symbol *t2)
1280 if (t1 == t2)
1281 return 0; /* yes, 0 - we don't want a cast_to here */
1282 if (t1 == &void_ctype)
1283 return 1;
1284 if (t2 == &void_ctype)
1285 return 1;
1286 if (classify_type(t1, &t1) != TYPE_NUM)
1287 return 0;
1288 if (classify_type(t2, &t2) != TYPE_NUM)
1289 return 0;
1290 if (t1 == t2)
1291 return 1;
1292 if (t1->ctype.modifiers & t2->ctype.modifiers & MOD_CHAR)
1293 return 1;
1294 if ((t1->ctype.modifiers ^ t2->ctype.modifiers) & MOD_SIZE)
1295 return 0;
1296 return !Wtypesign;
1299 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1300 struct expression **rp, const char *where)
1302 const char *typediff;
1303 struct symbol *source = degenerate(*rp);
1304 struct symbol *t, *s;
1305 int tclass = classify_type(target, &t);
1306 int sclass = classify_type(source, &s);
1308 if (tclass & sclass & TYPE_NUM) {
1309 if (tclass & TYPE_RESTRICT) {
1310 /* allowed assignments unfoul */
1311 if (sclass & TYPE_FOULED && unfoul(s) == t)
1312 goto Cast;
1313 if (!restricted_value(*rp, target))
1314 return 1;
1315 if (s == t)
1316 return 1;
1317 } else if (!(sclass & TYPE_RESTRICT))
1318 goto Cast;
1319 typediff = "different base types";
1320 goto Err;
1323 if (tclass == TYPE_PTR) {
1324 unsigned long mod1, mod2;
1325 struct symbol *b1, *b2;
1326 // NULL pointer is always OK
1327 int is_null = is_null_pointer_constant(*rp);
1328 if (is_null) {
1329 if (is_null == 2)
1330 bad_null(*rp);
1331 goto Cast;
1333 if (!(sclass & TYPE_PTR)) {
1334 typediff = "different base types";
1335 goto Err;
1337 b1 = examine_pointer_target(t);
1338 b2 = examine_pointer_target(s);
1339 mod1 = target_qualifiers(t);
1340 mod2 = target_qualifiers(s);
1341 if (whitelist_pointers(b1, b2)) {
1343 * assignments to/from void * are OK, provided that
1344 * we do not remove qualifiers from pointed to [C]
1345 * or mix address spaces [sparse].
1347 if (t->ctype.as != s->ctype.as) {
1348 typediff = "different address spaces";
1349 goto Err;
1351 if (mod2 & ~mod1) {
1352 typediff = "different modifiers";
1353 goto Err;
1355 goto Cast;
1357 /* It's OK if the target is more volatile or const than the source */
1358 typediff = type_difference(&t->ctype, &s->ctype, 0, mod1);
1359 if (typediff)
1360 goto Err;
1361 return 1;
1364 if ((tclass & TYPE_COMPOUND) && s == t)
1365 return 1;
1367 if (tclass & TYPE_NUM) {
1368 /* XXX: need to turn into comparison with NULL */
1369 if (t == &bool_ctype && (sclass & TYPE_PTR))
1370 goto Cast;
1371 typediff = "different base types";
1372 goto Err;
1374 typediff = "invalid types";
1376 Err:
1377 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1378 info(expr->pos, " expected %s", show_typename(target));
1379 info(expr->pos, " got %s", show_typename(source));
1380 *rp = cast_to(*rp, target);
1381 return 0;
1382 Cast:
1383 *rp = cast_to(*rp, target);
1384 return 1;
1387 static void mark_assigned(struct expression *expr)
1389 struct symbol *sym;
1391 if (!expr)
1392 return;
1393 switch (expr->type) {
1394 case EXPR_SYMBOL:
1395 sym = expr->symbol;
1396 if (!sym)
1397 return;
1398 if (sym->type != SYM_NODE)
1399 return;
1400 sym->ctype.modifiers |= MOD_ASSIGNED;
1401 return;
1403 case EXPR_BINOP:
1404 mark_assigned(expr->left);
1405 mark_assigned(expr->right);
1406 return;
1407 case EXPR_CAST:
1408 case EXPR_FORCE_CAST:
1409 mark_assigned(expr->cast_expression);
1410 return;
1411 case EXPR_SLICE:
1412 mark_assigned(expr->base);
1413 return;
1414 default:
1415 /* Hmm? */
1416 return;
1420 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1422 if (type->ctype.modifiers & MOD_CONST)
1423 expression_error(left, "assignment to const expression");
1425 /* We know left is an lvalue, so it's a "preop-*" */
1426 mark_assigned(left->unop);
1429 static struct symbol *evaluate_assignment(struct expression *expr)
1431 struct expression *left = expr->left;
1432 struct expression *where = expr;
1433 struct symbol *ltype;
1435 if (!lvalue_expression(left)) {
1436 expression_error(expr, "not an lvalue");
1437 return NULL;
1440 ltype = left->ctype;
1442 if (expr->op != '=') {
1443 if (!evaluate_assign_op(expr))
1444 return NULL;
1445 } else {
1446 if (!compatible_assignment_types(where, ltype, &expr->right, "assignment"))
1447 return NULL;
1450 evaluate_assign_to(left, ltype);
1452 expr->ctype = ltype;
1453 return ltype;
1456 static void examine_fn_arguments(struct symbol *fn)
1458 struct symbol *s;
1460 FOR_EACH_PTR(fn->arguments, s) {
1461 struct symbol *arg = evaluate_symbol(s);
1462 /* Array/function arguments silently degenerate into pointers */
1463 if (arg) {
1464 struct symbol *ptr;
1465 switch(arg->type) {
1466 case SYM_ARRAY:
1467 case SYM_FN:
1468 ptr = alloc_symbol(s->pos, SYM_PTR);
1469 if (arg->type == SYM_ARRAY)
1470 ptr->ctype = arg->ctype;
1471 else
1472 ptr->ctype.base_type = arg;
1473 ptr->ctype.as |= s->ctype.as;
1474 ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1476 s->ctype.base_type = ptr;
1477 s->ctype.as = 0;
1478 s->ctype.modifiers &= ~MOD_PTRINHERIT;
1479 s->bit_size = 0;
1480 s->examined = 0;
1481 examine_symbol_type(s);
1482 break;
1483 default:
1484 /* nothing */
1485 break;
1488 } END_FOR_EACH_PTR(s);
1491 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1493 /* Take the modifiers of the pointer, and apply them to the member */
1494 mod |= sym->ctype.modifiers;
1495 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1496 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1497 *newsym = *sym;
1498 newsym->ctype.as = as;
1499 newsym->ctype.modifiers = mod;
1500 sym = newsym;
1502 return sym;
1505 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1507 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1508 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1510 node->ctype.base_type = ptr;
1511 ptr->bit_size = bits_in_pointer;
1512 ptr->ctype.alignment = pointer_alignment;
1514 node->bit_size = bits_in_pointer;
1515 node->ctype.alignment = pointer_alignment;
1517 access_symbol(sym);
1518 if (sym->ctype.modifiers & MOD_REGISTER) {
1519 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1520 sym->ctype.modifiers &= ~MOD_REGISTER;
1522 if (sym->type == SYM_NODE) {
1523 ptr->ctype.as |= sym->ctype.as;
1524 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1525 sym = sym->ctype.base_type;
1527 if (degenerate && sym->type == SYM_ARRAY) {
1528 ptr->ctype.as |= sym->ctype.as;
1529 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1530 sym = sym->ctype.base_type;
1532 ptr->ctype.base_type = sym;
1534 return node;
1537 /* Arrays degenerate into pointers on pointer arithmetic */
1538 static struct symbol *degenerate(struct expression *expr)
1540 struct symbol *ctype, *base;
1542 if (!expr)
1543 return NULL;
1544 ctype = expr->ctype;
1545 if (!ctype)
1546 return NULL;
1547 base = examine_symbol_type(ctype);
1548 if (ctype->type == SYM_NODE)
1549 base = ctype->ctype.base_type;
1551 * Arrays degenerate into pointers to the entries, while
1552 * functions degenerate into pointers to themselves.
1553 * If array was part of non-lvalue compound, we create a copy
1554 * of that compound first and then act as if we were dealing with
1555 * the corresponding field in there.
1557 switch (base->type) {
1558 case SYM_ARRAY:
1559 if (expr->type == EXPR_SLICE) {
1560 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1561 struct expression *e0, *e1, *e2, *e3, *e4;
1563 a->ctype.base_type = expr->base->ctype;
1564 a->bit_size = expr->base->ctype->bit_size;
1565 a->array_size = expr->base->ctype->array_size;
1567 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1568 e0->symbol = a;
1569 e0->ctype = &lazy_ptr_ctype;
1571 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1572 e1->unop = e0;
1573 e1->op = '*';
1574 e1->ctype = expr->base->ctype; /* XXX */
1576 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1577 e2->left = e1;
1578 e2->right = expr->base;
1579 e2->op = '=';
1580 e2->ctype = expr->base->ctype;
1582 if (expr->r_bitpos) {
1583 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1584 e3->op = '+';
1585 e3->left = e0;
1586 e3->right = alloc_const_expression(expr->pos,
1587 expr->r_bitpos >> 3);
1588 e3->ctype = &lazy_ptr_ctype;
1589 } else {
1590 e3 = e0;
1593 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1594 e4->left = e2;
1595 e4->right = e3;
1596 e4->ctype = &lazy_ptr_ctype;
1598 expr->unop = e4;
1599 expr->type = EXPR_PREOP;
1600 expr->op = '*';
1602 case SYM_FN:
1603 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1604 expression_error(expr, "strange non-value function or array");
1605 return &bad_ctype;
1607 *expr = *expr->unop;
1608 ctype = create_pointer(expr, ctype, 1);
1609 expr->ctype = ctype;
1610 default:
1611 /* nothing */;
1613 return ctype;
1616 static struct symbol *evaluate_addressof(struct expression *expr)
1618 struct expression *op = expr->unop;
1619 struct symbol *ctype;
1621 if (op->op != '*' || op->type != EXPR_PREOP) {
1622 expression_error(expr, "not addressable");
1623 return NULL;
1625 ctype = op->ctype;
1626 *expr = *op->unop;
1627 expr->flags = 0;
1629 if (expr->type == EXPR_SYMBOL) {
1630 struct symbol *sym = expr->symbol;
1631 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1635 * symbol expression evaluation is lazy about the type
1636 * of the sub-expression, so we may have to generate
1637 * the type here if so..
1639 if (expr->ctype == &lazy_ptr_ctype) {
1640 ctype = create_pointer(expr, ctype, 0);
1641 expr->ctype = ctype;
1643 return expr->ctype;
1647 static struct symbol *evaluate_dereference(struct expression *expr)
1649 struct expression *op = expr->unop;
1650 struct symbol *ctype = op->ctype, *node, *target;
1652 /* Simplify: *&(expr) => (expr) */
1653 if (op->type == EXPR_PREOP && op->op == '&') {
1654 *expr = *op->unop;
1655 expr->flags = 0;
1656 return expr->ctype;
1659 /* Dereferencing a node drops all the node information. */
1660 if (ctype->type == SYM_NODE)
1661 ctype = ctype->ctype.base_type;
1663 node = alloc_symbol(expr->pos, SYM_NODE);
1664 target = ctype->ctype.base_type;
1666 switch (ctype->type) {
1667 default:
1668 expression_error(expr, "cannot dereference this type");
1669 return NULL;
1670 case SYM_PTR:
1671 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1672 merge_type(node, ctype);
1673 break;
1675 case SYM_ARRAY:
1676 if (!lvalue_expression(op)) {
1677 expression_error(op, "non-lvalue array??");
1678 return NULL;
1681 /* Do the implied "addressof" on the array */
1682 *op = *op->unop;
1685 * When an array is dereferenced, we need to pick
1686 * up the attributes of the original node too..
1688 merge_type(node, op->ctype);
1689 merge_type(node, ctype);
1690 break;
1693 node->bit_size = target->bit_size;
1694 node->array_size = target->array_size;
1696 expr->ctype = node;
1697 return node;
1701 * Unary post-ops: x++ and x--
1703 static struct symbol *evaluate_postop(struct expression *expr)
1705 struct expression *op = expr->unop;
1706 struct symbol *ctype = op->ctype;
1707 int class = classify_type(op->ctype, &ctype);
1708 int multiply = 0;
1710 if (!lvalue_expression(expr->unop)) {
1711 expression_error(expr, "need lvalue expression for ++/--");
1712 return NULL;
1715 if ((class & TYPE_RESTRICT) && restricted_unop(expr->op, &ctype))
1716 return bad_expr_type(expr);
1718 if (class & TYPE_NUM) {
1719 multiply = 1;
1720 } else if (class == TYPE_PTR) {
1721 struct symbol *target = examine_pointer_target(ctype);
1722 if (!is_function(target))
1723 multiply = target->bit_size >> 3;
1726 if (multiply) {
1727 evaluate_assign_to(op, op->ctype);
1728 expr->op_value = multiply;
1729 expr->ctype = ctype;
1730 return ctype;
1733 expression_error(expr, "bad argument type for ++/--");
1734 return NULL;
1737 static struct symbol *evaluate_sign(struct expression *expr)
1739 struct symbol *ctype = expr->unop->ctype;
1740 int class = classify_type(ctype, &ctype);
1741 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1742 expr->flags = 0;
1743 /* should be an arithmetic type */
1744 if (!(class & TYPE_NUM))
1745 return bad_expr_type(expr);
1746 if (!(class & (TYPE_FLOAT|TYPE_RESTRICT))) {
1747 struct symbol *rtype = integer_promotion(ctype);
1748 expr->unop = cast_to(expr->unop, rtype);
1749 ctype = rtype;
1750 } else if ((class & TYPE_FLOAT) && expr->op != '~') {
1751 /* no conversions needed */
1752 } else if ((class & TYPE_RESTRICT) && !restricted_unop(expr->op, &ctype)) {
1753 /* no conversions needed */
1754 } else {
1755 return bad_expr_type(expr);
1757 if (expr->op == '+')
1758 *expr = *expr->unop;
1759 expr->ctype = ctype;
1760 return ctype;
1763 static struct symbol *evaluate_preop(struct expression *expr)
1765 struct symbol *ctype = expr->unop->ctype;
1767 switch (expr->op) {
1768 case '(':
1769 *expr = *expr->unop;
1770 return ctype;
1772 case '+':
1773 case '-':
1774 case '~':
1775 return evaluate_sign(expr);
1777 case '*':
1778 return evaluate_dereference(expr);
1780 case '&':
1781 return evaluate_addressof(expr);
1783 case SPECIAL_INCREMENT:
1784 case SPECIAL_DECREMENT:
1786 * From a type evaluation standpoint the preops are
1787 * the same as the postops
1789 return evaluate_postop(expr);
1791 case '!':
1792 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1793 expr->flags = 0;
1794 if (is_safe_type(ctype))
1795 warning(expr->pos, "testing a 'safe expression'");
1796 if (is_float_type(ctype)) {
1797 struct expression *arg = expr->unop;
1798 expr->type = EXPR_BINOP;
1799 expr->op = SPECIAL_EQUAL;
1800 expr->left = arg;
1801 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1802 expr->right->ctype = ctype;
1803 expr->right->fvalue = 0;
1804 } else if (is_fouled_type(ctype)) {
1805 warning(expr->pos, "%sdegrades to integer",
1806 show_typename(ctype->ctype.base_type));
1808 ctype = &bool_ctype;
1809 break;
1811 default:
1812 break;
1814 expr->ctype = ctype;
1815 return &bool_ctype;
1818 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1820 struct ptr_list *head = (struct ptr_list *)_list;
1821 struct ptr_list *list = head;
1823 if (!head)
1824 return NULL;
1825 do {
1826 int i;
1827 for (i = 0; i < list->nr; i++) {
1828 struct symbol *sym = (struct symbol *) list->list[i];
1829 if (sym->ident) {
1830 if (sym->ident != ident)
1831 continue;
1832 *offset = sym->offset;
1833 return sym;
1834 } else {
1835 struct symbol *ctype = sym->ctype.base_type;
1836 struct symbol *sub;
1837 if (!ctype)
1838 continue;
1839 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1840 continue;
1841 sub = find_identifier(ident, ctype->symbol_list, offset);
1842 if (!sub)
1843 continue;
1844 *offset += sym->offset;
1845 return sub;
1848 } while ((list = list->next) != head);
1849 return NULL;
1852 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1854 struct expression *add;
1857 * Create a new add-expression
1859 * NOTE! Even if we just add zero, we need a new node
1860 * for the member pointer, since it has a different
1861 * type than the original pointer. We could make that
1862 * be just a cast, but the fact is, a node is a node,
1863 * so we might as well just do the "add zero" here.
1865 add = alloc_expression(expr->pos, EXPR_BINOP);
1866 add->op = '+';
1867 add->left = expr;
1868 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1869 add->right->ctype = &int_ctype;
1870 add->right->value = offset;
1873 * The ctype of the pointer will be lazily evaluated if
1874 * we ever take the address of this member dereference..
1876 add->ctype = &lazy_ptr_ctype;
1877 return add;
1880 /* structure/union dereference */
1881 static struct symbol *evaluate_member_dereference(struct expression *expr)
1883 int offset;
1884 struct symbol *ctype, *member;
1885 struct expression *deref = expr->deref, *add;
1886 struct ident *ident = expr->member;
1887 unsigned int mod;
1888 int address_space;
1890 if (!evaluate_expression(deref))
1891 return NULL;
1892 if (!ident) {
1893 expression_error(expr, "bad member name");
1894 return NULL;
1897 ctype = deref->ctype;
1898 examine_symbol_type(ctype);
1899 address_space = ctype->ctype.as;
1900 mod = ctype->ctype.modifiers;
1901 if (ctype->type == SYM_NODE) {
1902 ctype = ctype->ctype.base_type;
1903 address_space |= ctype->ctype.as;
1904 mod |= ctype->ctype.modifiers;
1906 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1907 expression_error(expr, "expected structure or union");
1908 return NULL;
1910 offset = 0;
1911 member = find_identifier(ident, ctype->symbol_list, &offset);
1912 if (!member) {
1913 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1914 const char *name = "<unnamed>";
1915 int namelen = 9;
1916 if (ctype->ident) {
1917 name = ctype->ident->name;
1918 namelen = ctype->ident->len;
1920 if (ctype->symbol_list)
1921 expression_error(expr, "no member '%s' in %s %.*s",
1922 show_ident(ident), type, namelen, name);
1923 else
1924 expression_error(expr, "using member '%s' in "
1925 "incomplete %s %.*s", show_ident(ident),
1926 type, namelen, name);
1927 return NULL;
1931 * The member needs to take on the address space and modifiers of
1932 * the "parent" type.
1934 member = convert_to_as_mod(member, address_space, mod);
1935 ctype = get_base_type(member);
1937 if (!lvalue_expression(deref)) {
1938 if (deref->type != EXPR_SLICE) {
1939 expr->base = deref;
1940 expr->r_bitpos = 0;
1941 } else {
1942 expr->base = deref->base;
1943 expr->r_bitpos = deref->r_bitpos;
1945 expr->r_bitpos += offset << 3;
1946 expr->type = EXPR_SLICE;
1947 expr->r_nrbits = member->bit_size;
1948 expr->r_bitpos += member->bit_offset;
1949 expr->ctype = member;
1950 return member;
1953 deref = deref->unop;
1954 expr->deref = deref;
1956 add = evaluate_offset(deref, offset);
1957 expr->type = EXPR_PREOP;
1958 expr->op = '*';
1959 expr->unop = add;
1961 expr->ctype = member;
1962 return member;
1965 static int is_promoted(struct expression *expr)
1967 while (1) {
1968 switch (expr->type) {
1969 case EXPR_BINOP:
1970 case EXPR_SELECT:
1971 case EXPR_CONDITIONAL:
1972 return 1;
1973 case EXPR_COMMA:
1974 expr = expr->right;
1975 continue;
1976 case EXPR_PREOP:
1977 switch (expr->op) {
1978 case '(':
1979 expr = expr->unop;
1980 continue;
1981 case '+':
1982 case '-':
1983 case '~':
1984 return 1;
1985 default:
1986 return 0;
1988 default:
1989 return 0;
1995 static struct symbol *evaluate_cast(struct expression *);
1997 static struct symbol *evaluate_type_information(struct expression *expr)
1999 struct symbol *sym = expr->cast_type;
2000 if (!sym) {
2001 sym = evaluate_expression(expr->cast_expression);
2002 if (!sym)
2003 return NULL;
2005 * Expressions of restricted types will possibly get
2006 * promoted - check that here
2008 if (is_restricted_type(sym)) {
2009 if (sym->bit_size < bits_in_int && is_promoted(expr))
2010 sym = &int_ctype;
2011 } else if (is_fouled_type(sym)) {
2012 sym = &int_ctype;
2015 examine_symbol_type(sym);
2016 if (is_bitfield_type(sym)) {
2017 expression_error(expr, "trying to examine bitfield type");
2018 return NULL;
2020 return sym;
2023 static struct symbol *evaluate_sizeof(struct expression *expr)
2025 struct symbol *type;
2026 int size;
2028 type = evaluate_type_information(expr);
2029 if (!type)
2030 return NULL;
2032 size = type->bit_size;
2033 if ((size < 0) || (size & 7))
2034 expression_error(expr, "cannot size expression");
2035 expr->type = EXPR_VALUE;
2036 expr->value = size >> 3;
2037 expr->taint = 0;
2038 expr->ctype = size_t_ctype;
2039 return size_t_ctype;
2042 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
2044 struct symbol *type;
2045 int size;
2047 type = evaluate_type_information(expr);
2048 if (!type)
2049 return NULL;
2051 if (type->type == SYM_NODE)
2052 type = type->ctype.base_type;
2053 if (!type)
2054 return NULL;
2055 switch (type->type) {
2056 case SYM_ARRAY:
2057 break;
2058 case SYM_PTR:
2059 type = get_base_type(type);
2060 if (type)
2061 break;
2062 default:
2063 expression_error(expr, "expected pointer expression");
2064 return NULL;
2066 size = type->bit_size;
2067 if (size & 7)
2068 size = 0;
2069 expr->type = EXPR_VALUE;
2070 expr->value = size >> 3;
2071 expr->taint = 0;
2072 expr->ctype = size_t_ctype;
2073 return size_t_ctype;
2076 static struct symbol *evaluate_alignof(struct expression *expr)
2078 struct symbol *type;
2080 type = evaluate_type_information(expr);
2081 if (!type)
2082 return NULL;
2084 expr->type = EXPR_VALUE;
2085 expr->value = type->ctype.alignment;
2086 expr->taint = 0;
2087 expr->ctype = size_t_ctype;
2088 return size_t_ctype;
2091 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
2093 struct expression *expr;
2094 struct symbol_list *argument_types = fn->arguments;
2095 struct symbol *argtype;
2096 int i = 1;
2098 PREPARE_PTR_LIST(argument_types, argtype);
2099 FOR_EACH_PTR (head, expr) {
2100 struct expression **p = THIS_ADDRESS(expr);
2101 struct symbol *ctype, *target;
2102 ctype = evaluate_expression(expr);
2104 if (!ctype)
2105 return 0;
2107 target = argtype;
2108 if (!target) {
2109 struct symbol *type;
2110 int class = classify_type(ctype, &type);
2111 if (is_int(class)) {
2112 *p = cast_to(expr, integer_promotion(type));
2113 } else if (class & TYPE_FLOAT) {
2114 unsigned long mod = type->ctype.modifiers;
2115 if (!(mod & (MOD_LONG|MOD_LONGLONG)))
2116 *p = cast_to(expr, &double_ctype);
2117 } else if (class & TYPE_PTR) {
2118 if (expr->ctype == &null_ctype)
2119 *p = cast_to(expr, &ptr_ctype);
2120 else
2121 degenerate(expr);
2123 } else {
2124 static char where[30];
2125 examine_symbol_type(target);
2126 sprintf(where, "argument %d", i);
2127 compatible_assignment_types(expr, target, p, where);
2130 i++;
2131 NEXT_PTR_LIST(argtype);
2132 } END_FOR_EACH_PTR(expr);
2133 FINISH_PTR_LIST(argtype);
2134 return 1;
2137 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
2139 struct symbol *sym;
2141 FOR_EACH_PTR(ctype->symbol_list, sym) {
2142 if (sym->ident == ident)
2143 return sym;
2144 } END_FOR_EACH_PTR(sym);
2145 return NULL;
2148 static void convert_index(struct expression *e)
2150 struct expression *child = e->idx_expression;
2151 unsigned from = e->idx_from;
2152 unsigned to = e->idx_to + 1;
2153 e->type = EXPR_POS;
2154 e->init_offset = from * (e->ctype->bit_size>>3);
2155 e->init_nr = to - from;
2156 e->init_expr = child;
2159 static void convert_ident(struct expression *e)
2161 struct expression *child = e->ident_expression;
2162 struct symbol *sym = e->field;
2163 e->type = EXPR_POS;
2164 e->init_offset = sym->offset;
2165 e->init_nr = 1;
2166 e->init_expr = child;
2169 static void convert_designators(struct expression *e)
2171 while (e) {
2172 if (e->type == EXPR_INDEX)
2173 convert_index(e);
2174 else if (e->type == EXPR_IDENTIFIER)
2175 convert_ident(e);
2176 else
2177 break;
2178 e = e->init_expr;
2182 static void excess(struct expression *e, const char *s)
2184 warning(e->pos, "excessive elements in %s initializer", s);
2188 * implicit designator for the first element
2190 static struct expression *first_subobject(struct symbol *ctype, int class,
2191 struct expression **v)
2193 struct expression *e = *v, *new;
2195 if (ctype->type == SYM_NODE)
2196 ctype = ctype->ctype.base_type;
2198 if (class & TYPE_PTR) { /* array */
2199 if (!ctype->bit_size)
2200 return NULL;
2201 new = alloc_expression(e->pos, EXPR_INDEX);
2202 new->idx_expression = e;
2203 new->ctype = ctype->ctype.base_type;
2204 } else {
2205 struct symbol *field, *p;
2206 PREPARE_PTR_LIST(ctype->symbol_list, p);
2207 while (p && !p->ident && is_bitfield_type(p))
2208 NEXT_PTR_LIST(p);
2209 field = p;
2210 FINISH_PTR_LIST(p);
2211 if (!field)
2212 return NULL;
2213 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2214 new->ident_expression = e;
2215 new->field = new->ctype = field;
2217 *v = new;
2218 return new;
2222 * sanity-check explicit designators; return the innermost one or NULL
2223 * in case of error. Assign types.
2225 static struct expression *check_designators(struct expression *e,
2226 struct symbol *ctype)
2228 struct expression *last = NULL;
2229 const char *err;
2230 while (1) {
2231 if (ctype->type == SYM_NODE)
2232 ctype = ctype->ctype.base_type;
2233 if (e->type == EXPR_INDEX) {
2234 struct symbol *type;
2235 if (ctype->type != SYM_ARRAY) {
2236 err = "array index in non-array";
2237 break;
2239 type = ctype->ctype.base_type;
2240 if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2241 unsigned offset = e->idx_to * type->bit_size;
2242 if (offset >= ctype->bit_size) {
2243 err = "index out of bounds in";
2244 break;
2247 e->ctype = ctype = type;
2248 ctype = type;
2249 last = e;
2250 if (!e->idx_expression) {
2251 err = "invalid";
2252 break;
2254 e = e->idx_expression;
2255 } else if (e->type == EXPR_IDENTIFIER) {
2256 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2257 err = "field name not in struct or union";
2258 break;
2260 ctype = find_struct_ident(ctype, e->expr_ident);
2261 if (!ctype) {
2262 err = "unknown field name in";
2263 break;
2265 e->field = e->ctype = ctype;
2266 last = e;
2267 if (!e->ident_expression) {
2268 err = "invalid";
2269 break;
2271 e = e->ident_expression;
2272 } else if (e->type == EXPR_POS) {
2273 err = "internal front-end error: EXPR_POS in";
2274 break;
2275 } else
2276 return last;
2278 expression_error(e, "%s initializer", err);
2279 return NULL;
2283 * choose the next subobject to initialize.
2285 * Get designators for next element, switch old ones to EXPR_POS.
2286 * Return the resulting expression or NULL if we'd run out of subobjects.
2287 * The innermost designator is returned in *v. Designators in old
2288 * are assumed to be already sanity-checked.
2290 static struct expression *next_designators(struct expression *old,
2291 struct symbol *ctype,
2292 struct expression *e, struct expression **v)
2294 struct expression *new = NULL;
2296 if (!old)
2297 return NULL;
2298 if (old->type == EXPR_INDEX) {
2299 struct expression *copy;
2300 unsigned n;
2302 copy = next_designators(old->idx_expression,
2303 old->ctype, e, v);
2304 if (!copy) {
2305 n = old->idx_to + 1;
2306 if (n * old->ctype->bit_size == ctype->bit_size) {
2307 convert_index(old);
2308 return NULL;
2310 copy = e;
2311 *v = new = alloc_expression(e->pos, EXPR_INDEX);
2312 } else {
2313 n = old->idx_to;
2314 new = alloc_expression(e->pos, EXPR_INDEX);
2317 new->idx_from = new->idx_to = n;
2318 new->idx_expression = copy;
2319 new->ctype = old->ctype;
2320 convert_index(old);
2321 } else if (old->type == EXPR_IDENTIFIER) {
2322 struct expression *copy;
2323 struct symbol *field;
2325 copy = next_designators(old->ident_expression,
2326 old->ctype, e, v);
2327 if (!copy) {
2328 field = old->field->next_subobject;
2329 if (!field) {
2330 convert_ident(old);
2331 return NULL;
2333 copy = e;
2334 *v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2335 } else {
2336 field = old->field;
2337 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2340 new->field = field;
2341 new->expr_ident = field->ident;
2342 new->ident_expression = copy;
2343 new->ctype = field;
2344 convert_ident(old);
2346 return new;
2349 static int handle_simple_initializer(struct expression **ep, int nested,
2350 int class, struct symbol *ctype);
2353 * deal with traversing subobjects [6.7.8(17,18,20)]
2355 static void handle_list_initializer(struct expression *expr,
2356 int class, struct symbol *ctype)
2358 struct expression *e, *last = NULL, *top = NULL, *next;
2359 int jumped = 0;
2361 FOR_EACH_PTR(expr->expr_list, e) {
2362 struct expression **v;
2363 struct symbol *type;
2364 int lclass;
2366 if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2367 if (!top) {
2368 top = e;
2369 last = first_subobject(ctype, class, &top);
2370 } else {
2371 last = next_designators(last, ctype, e, &top);
2373 if (!last) {
2374 excess(e, class & TYPE_PTR ? "array" :
2375 "struct or union");
2376 DELETE_CURRENT_PTR(e);
2377 continue;
2379 if (jumped) {
2380 warning(e->pos, "advancing past deep designator");
2381 jumped = 0;
2383 REPLACE_CURRENT_PTR(e, last);
2384 } else {
2385 next = check_designators(e, ctype);
2386 if (!next) {
2387 DELETE_CURRENT_PTR(e);
2388 continue;
2390 top = next;
2391 /* deeper than one designator? */
2392 jumped = top != e;
2393 convert_designators(last);
2394 last = e;
2397 found:
2398 lclass = classify_type(top->ctype, &type);
2399 if (top->type == EXPR_INDEX)
2400 v = &top->idx_expression;
2401 else
2402 v = &top->ident_expression;
2404 if (handle_simple_initializer(v, 1, lclass, top->ctype))
2405 continue;
2407 if (!(lclass & TYPE_COMPOUND)) {
2408 warning(e->pos, "bogus scalar initializer");
2409 DELETE_CURRENT_PTR(e);
2410 continue;
2413 next = first_subobject(type, lclass, v);
2414 if (next) {
2415 warning(e->pos, "missing braces around initializer");
2416 top = next;
2417 goto found;
2420 DELETE_CURRENT_PTR(e);
2421 excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2423 } END_FOR_EACH_PTR(e);
2425 convert_designators(last);
2426 expr->ctype = ctype;
2429 static int is_string_literal(struct expression **v)
2431 struct expression *e = *v;
2432 while (e && e->type == EXPR_PREOP && e->op == '(')
2433 e = e->unop;
2434 if (!e || e->type != EXPR_STRING)
2435 return 0;
2436 if (e != *v && Wparen_string)
2437 warning(e->pos,
2438 "array initialized from parenthesized string constant");
2439 *v = e;
2440 return 1;
2444 * We want a normal expression, possibly in one layer of braces. Warn
2445 * if the latter happens inside a list (it's legal, but likely to be
2446 * an effect of screwup). In case of anything not legal, we are definitely
2447 * having an effect of screwup, so just fail and let the caller warn.
2449 static struct expression *handle_scalar(struct expression *e, int nested)
2451 struct expression *v = NULL, *p;
2452 int count = 0;
2454 /* normal case */
2455 if (e->type != EXPR_INITIALIZER)
2456 return e;
2458 FOR_EACH_PTR(e->expr_list, p) {
2459 if (!v)
2460 v = p;
2461 count++;
2462 } END_FOR_EACH_PTR(p);
2463 if (count != 1)
2464 return NULL;
2465 switch(v->type) {
2466 case EXPR_INITIALIZER:
2467 case EXPR_INDEX:
2468 case EXPR_IDENTIFIER:
2469 return NULL;
2470 default:
2471 break;
2473 if (nested)
2474 warning(e->pos, "braces around scalar initializer");
2475 return v;
2479 * deal with the cases that don't care about subobjects:
2480 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2481 * character array <- string literal, possibly in braces [6.7.8(14)]
2482 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2483 * compound type <- initializer list in braces [6.7.8(16)]
2484 * The last one punts to handle_list_initializer() which, in turn will call
2485 * us for individual elements of the list.
2487 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2488 * the lack of support of wide char stuff in general.
2490 * One note: we need to take care not to evaluate a string literal until
2491 * we know that we *will* handle it right here. Otherwise we would screw
2492 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2493 * { "string", ...} - we need to preserve that string literal recognizable
2494 * until we dig into the inner struct.
2496 static int handle_simple_initializer(struct expression **ep, int nested,
2497 int class, struct symbol *ctype)
2499 int is_string = is_string_type(ctype);
2500 struct expression *e = *ep, *p;
2501 struct symbol *type;
2503 if (!e)
2504 return 0;
2506 /* scalar */
2507 if (!(class & TYPE_COMPOUND)) {
2508 e = handle_scalar(e, nested);
2509 if (!e)
2510 return 0;
2511 *ep = e;
2512 if (!evaluate_expression(e))
2513 return 1;
2514 compatible_assignment_types(e, ctype, ep, "initializer");
2515 return 1;
2519 * sublist; either a string, or we dig in; the latter will deal with
2520 * pathologies, so we don't need anything fancy here.
2522 if (e->type == EXPR_INITIALIZER) {
2523 if (is_string) {
2524 struct expression *v = NULL;
2525 int count = 0;
2527 FOR_EACH_PTR(e->expr_list, p) {
2528 if (!v)
2529 v = p;
2530 count++;
2531 } END_FOR_EACH_PTR(p);
2532 if (count == 1 && is_string_literal(&v)) {
2533 *ep = e = v;
2534 goto String;
2537 handle_list_initializer(e, class, ctype);
2538 return 1;
2541 /* string */
2542 if (is_string_literal(&e)) {
2543 /* either we are doing array of char, or we'll have to dig in */
2544 if (is_string) {
2545 *ep = e;
2546 goto String;
2548 return 0;
2550 /* struct or union can be initialized by compatible */
2551 if (class != TYPE_COMPOUND)
2552 return 0;
2553 type = evaluate_expression(e);
2554 if (!type)
2555 return 0;
2556 if (ctype->type == SYM_NODE)
2557 ctype = ctype->ctype.base_type;
2558 if (type->type == SYM_NODE)
2559 type = type->ctype.base_type;
2560 if (ctype == type)
2561 return 1;
2562 return 0;
2564 String:
2565 p = alloc_expression(e->pos, EXPR_STRING);
2566 *p = *e;
2567 type = evaluate_expression(p);
2568 if (ctype->bit_size != -1 &&
2569 ctype->bit_size + bits_in_char < type->bit_size) {
2570 warning(e->pos,
2571 "too long initializer-string for array of char");
2573 *ep = p;
2574 return 1;
2577 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2579 struct symbol *type;
2580 int class = classify_type(ctype, &type);
2581 if (!handle_simple_initializer(ep, 0, class, ctype))
2582 expression_error(*ep, "invalid initializer");
2585 static struct symbol *evaluate_cast(struct expression *expr)
2587 struct expression *target = expr->cast_expression;
2588 struct symbol *ctype;
2589 struct symbol *t1, *t2;
2590 int class1, class2;
2591 int as1 = 0, as2 = 0;
2593 if (!target)
2594 return NULL;
2597 * Special case: a cast can be followed by an
2598 * initializer, in which case we need to pass
2599 * the type value down to that initializer rather
2600 * than trying to evaluate it as an expression
2602 * A more complex case is when the initializer is
2603 * dereferenced as part of a post-fix expression.
2604 * We need to produce an expression that can be dereferenced.
2606 if (target->type == EXPR_INITIALIZER) {
2607 struct symbol *sym = expr->cast_type;
2608 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2610 sym->initializer = target;
2611 evaluate_symbol(sym);
2613 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2614 addr->symbol = sym;
2616 expr->type = EXPR_PREOP;
2617 expr->op = '*';
2618 expr->unop = addr;
2619 expr->ctype = sym;
2621 return sym;
2624 ctype = examine_symbol_type(expr->cast_type);
2625 expr->ctype = ctype;
2626 expr->cast_type = ctype;
2628 evaluate_expression(target);
2629 degenerate(target);
2631 class1 = classify_type(ctype, &t1);
2633 /* cast to non-integer type -> not an integer constant expression */
2634 if (!is_int(class1))
2635 expr->flags = 0;
2636 /* if argument turns out to be not an integer constant expression *and*
2637 it was not a floating literal to start with -> too bad */
2638 else if (expr->flags == Int_const_expr &&
2639 !(target->flags & Int_const_expr))
2640 expr->flags = 0;
2642 * You can always throw a value away by casting to
2643 * "void" - that's an implicit "force". Note that
2644 * the same is _not_ true of "void *".
2646 if (t1 == &void_ctype)
2647 goto out;
2649 if (class1 & (TYPE_COMPOUND | TYPE_FN))
2650 warning(expr->pos, "cast to non-scalar");
2652 t2 = target->ctype;
2653 if (!t2) {
2654 expression_error(expr, "cast from unknown type");
2655 goto out;
2657 class2 = classify_type(t2, &t2);
2659 if (class2 & TYPE_COMPOUND)
2660 warning(expr->pos, "cast from non-scalar");
2662 if (expr->type == EXPR_FORCE_CAST)
2663 goto out;
2665 /* allowed cast unfouls */
2666 if (class2 & TYPE_FOULED)
2667 t2 = unfoul(t2);
2669 if (t1 != t2) {
2670 if (class1 & TYPE_RESTRICT)
2671 warning(expr->pos, "cast to %s",
2672 show_typename(t1));
2673 if (class2 & TYPE_RESTRICT)
2674 warning(expr->pos, "cast from %s",
2675 show_typename(t2));
2678 if (t1 == &ulong_ctype)
2679 as1 = -1;
2680 else if (class1 == TYPE_PTR) {
2681 examine_pointer_target(t1);
2682 as1 = t1->ctype.as;
2685 if (t2 == &ulong_ctype)
2686 as2 = -1;
2687 else if (class2 == TYPE_PTR) {
2688 examine_pointer_target(t2);
2689 as2 = t2->ctype.as;
2692 if (!as1 && as2 > 0)
2693 warning(expr->pos, "cast removes address space of expression");
2694 if (as1 > 0 && as2 > 0 && as1 != as2)
2695 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2696 if (as1 > 0 && !as2 &&
2697 !is_null_pointer_constant(target) && Wcast_to_as)
2698 warning(expr->pos,
2699 "cast adds address space to expression (<asn:%d>)", as1);
2701 if (!(t1->ctype.modifiers & MOD_PTRINHERIT) && class1 == TYPE_PTR &&
2702 !as1 && (target->flags & Int_const_expr)) {
2703 if (t1->ctype.base_type == &void_ctype) {
2704 if (is_zero_constant(target)) {
2705 /* NULL */
2706 expr->type = EXPR_VALUE;
2707 expr->ctype = &null_ctype;
2708 expr->value = 0;
2709 return ctype;
2713 out:
2714 return ctype;
2718 * Evaluate a call expression with a symbol. This
2719 * should expand inline functions, and evaluate
2720 * builtins.
2722 static int evaluate_symbol_call(struct expression *expr)
2724 struct expression *fn = expr->fn;
2725 struct symbol *ctype = fn->ctype;
2727 if (fn->type != EXPR_PREOP)
2728 return 0;
2730 if (ctype->op && ctype->op->evaluate)
2731 return ctype->op->evaluate(expr);
2733 if (ctype->ctype.modifiers & MOD_INLINE) {
2734 int ret;
2735 struct symbol *curr = current_fn;
2736 current_fn = ctype->ctype.base_type;
2738 ret = inline_function(expr, ctype);
2740 /* restore the old function */
2741 current_fn = curr;
2742 return ret;
2745 return 0;
2748 static struct symbol *evaluate_call(struct expression *expr)
2750 int args, fnargs;
2751 struct symbol *ctype, *sym;
2752 struct expression *fn = expr->fn;
2753 struct expression_list *arglist = expr->args;
2755 if (!evaluate_expression(fn))
2756 return NULL;
2757 sym = ctype = fn->ctype;
2758 if (ctype->type == SYM_NODE)
2759 ctype = ctype->ctype.base_type;
2760 if (ctype->type == SYM_PTR)
2761 ctype = get_base_type(ctype);
2763 if (ctype->type != SYM_FN) {
2764 struct expression *arg;
2765 expression_error(expr, "not a function %s",
2766 show_ident(sym->ident));
2767 /* do typechecking in arguments */
2768 FOR_EACH_PTR (arglist, arg) {
2769 evaluate_expression(arg);
2770 } END_FOR_EACH_PTR(arg);
2771 return NULL;
2774 examine_fn_arguments(ctype);
2775 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
2776 sym->op && sym->op->args) {
2777 if (!sym->op->args(expr))
2778 return NULL;
2779 } else {
2780 if (!evaluate_arguments(sym, ctype, arglist))
2781 return NULL;
2782 args = expression_list_size(expr->args);
2783 fnargs = symbol_list_size(ctype->arguments);
2784 if (args < fnargs)
2785 expression_error(expr,
2786 "not enough arguments for function %s",
2787 show_ident(sym->ident));
2788 if (args > fnargs && !ctype->variadic)
2789 expression_error(expr,
2790 "too many arguments for function %s",
2791 show_ident(sym->ident));
2793 if (sym->type == SYM_NODE) {
2794 if (evaluate_symbol_call(expr))
2795 return expr->ctype;
2797 expr->ctype = ctype->ctype.base_type;
2798 return expr->ctype;
2801 static struct symbol *evaluate_offsetof(struct expression *expr)
2803 struct expression *e = expr->down;
2804 struct symbol *ctype = expr->in;
2805 int class;
2807 if (expr->op == '.') {
2808 struct symbol *field;
2809 int offset = 0;
2810 if (!ctype) {
2811 expression_error(expr, "expected structure or union");
2812 return NULL;
2814 examine_symbol_type(ctype);
2815 class = classify_type(ctype, &ctype);
2816 if (class != TYPE_COMPOUND) {
2817 expression_error(expr, "expected structure or union");
2818 return NULL;
2821 field = find_identifier(expr->ident, ctype->symbol_list, &offset);
2822 if (!field) {
2823 expression_error(expr, "unknown member");
2824 return NULL;
2826 ctype = field;
2827 expr->type = EXPR_VALUE;
2828 expr->flags = Int_const_expr;
2829 expr->value = offset;
2830 expr->taint = 0;
2831 expr->ctype = size_t_ctype;
2832 } else {
2833 if (!ctype) {
2834 expression_error(expr, "expected structure or union");
2835 return NULL;
2837 examine_symbol_type(ctype);
2838 class = classify_type(ctype, &ctype);
2839 if (class != (TYPE_COMPOUND | TYPE_PTR)) {
2840 expression_error(expr, "expected array");
2841 return NULL;
2843 ctype = ctype->ctype.base_type;
2844 if (!expr->index) {
2845 expr->type = EXPR_VALUE;
2846 expr->flags = Int_const_expr;
2847 expr->value = 0;
2848 expr->taint = 0;
2849 expr->ctype = size_t_ctype;
2850 } else {
2851 struct expression *idx = expr->index, *m;
2852 struct symbol *i_type = evaluate_expression(idx);
2853 int i_class = classify_type(i_type, &i_type);
2854 if (!is_int(i_class)) {
2855 expression_error(expr, "non-integer index");
2856 return NULL;
2858 unrestrict(idx, i_class, &i_type);
2859 idx = cast_to(idx, size_t_ctype);
2860 m = alloc_const_expression(expr->pos,
2861 ctype->bit_size >> 3);
2862 m->ctype = size_t_ctype;
2863 m->flags = Int_const_expr;
2864 expr->type = EXPR_BINOP;
2865 expr->left = idx;
2866 expr->right = m;
2867 expr->op = '*';
2868 expr->ctype = size_t_ctype;
2869 expr->flags = m->flags & idx->flags & Int_const_expr;
2872 if (e) {
2873 struct expression *copy = __alloc_expression(0);
2874 *copy = *expr;
2875 if (e->type == EXPR_OFFSETOF)
2876 e->in = ctype;
2877 if (!evaluate_expression(e))
2878 return NULL;
2879 expr->type = EXPR_BINOP;
2880 expr->flags = e->flags & copy->flags & Int_const_expr;
2881 expr->op = '+';
2882 expr->ctype = size_t_ctype;
2883 expr->left = copy;
2884 expr->right = e;
2886 return size_t_ctype;
2889 struct symbol *evaluate_expression(struct expression *expr)
2891 if (!expr)
2892 return NULL;
2893 if (expr->ctype)
2894 return expr->ctype;
2896 switch (expr->type) {
2897 case EXPR_VALUE:
2898 case EXPR_FVALUE:
2899 expression_error(expr, "value expression without a type");
2900 return NULL;
2901 case EXPR_STRING:
2902 return evaluate_string(expr);
2903 case EXPR_SYMBOL:
2904 return evaluate_symbol_expression(expr);
2905 case EXPR_BINOP:
2906 if (!evaluate_expression(expr->left))
2907 return NULL;
2908 if (!evaluate_expression(expr->right))
2909 return NULL;
2910 return evaluate_binop(expr);
2911 case EXPR_LOGICAL:
2912 return evaluate_logical(expr);
2913 case EXPR_COMMA:
2914 evaluate_expression(expr->left);
2915 if (!evaluate_expression(expr->right))
2916 return NULL;
2917 return evaluate_comma(expr);
2918 case EXPR_COMPARE:
2919 if (!evaluate_expression(expr->left))
2920 return NULL;
2921 if (!evaluate_expression(expr->right))
2922 return NULL;
2923 return evaluate_compare(expr);
2924 case EXPR_ASSIGNMENT:
2925 if (!evaluate_expression(expr->left))
2926 return NULL;
2927 if (!evaluate_expression(expr->right))
2928 return NULL;
2929 return evaluate_assignment(expr);
2930 case EXPR_PREOP:
2931 if (!evaluate_expression(expr->unop))
2932 return NULL;
2933 return evaluate_preop(expr);
2934 case EXPR_POSTOP:
2935 if (!evaluate_expression(expr->unop))
2936 return NULL;
2937 return evaluate_postop(expr);
2938 case EXPR_CAST:
2939 case EXPR_FORCE_CAST:
2940 case EXPR_IMPLIED_CAST:
2941 return evaluate_cast(expr);
2942 case EXPR_SIZEOF:
2943 return evaluate_sizeof(expr);
2944 case EXPR_PTRSIZEOF:
2945 return evaluate_ptrsizeof(expr);
2946 case EXPR_ALIGNOF:
2947 return evaluate_alignof(expr);
2948 case EXPR_DEREF:
2949 return evaluate_member_dereference(expr);
2950 case EXPR_CALL:
2951 return evaluate_call(expr);
2952 case EXPR_SELECT:
2953 case EXPR_CONDITIONAL:
2954 return evaluate_conditional_expression(expr);
2955 case EXPR_STATEMENT:
2956 expr->ctype = evaluate_statement(expr->statement);
2957 return expr->ctype;
2959 case EXPR_LABEL:
2960 expr->ctype = &ptr_ctype;
2961 return &ptr_ctype;
2963 case EXPR_TYPE:
2964 /* Evaluate the type of the symbol .. */
2965 evaluate_symbol(expr->symbol);
2966 /* .. but the type of the _expression_ is a "type" */
2967 expr->ctype = &type_ctype;
2968 return &type_ctype;
2970 case EXPR_OFFSETOF:
2971 return evaluate_offsetof(expr);
2973 /* These can not exist as stand-alone expressions */
2974 case EXPR_INITIALIZER:
2975 case EXPR_IDENTIFIER:
2976 case EXPR_INDEX:
2977 case EXPR_POS:
2978 expression_error(expr, "internal front-end error: initializer in expression");
2979 return NULL;
2980 case EXPR_SLICE:
2981 expression_error(expr, "internal front-end error: SLICE re-evaluated");
2982 return NULL;
2984 return NULL;
2987 static void check_duplicates(struct symbol *sym)
2989 int declared = 0;
2990 struct symbol *next = sym;
2992 while ((next = next->same_symbol) != NULL) {
2993 const char *typediff;
2994 evaluate_symbol(next);
2995 declared++;
2996 typediff = type_difference(&sym->ctype, &next->ctype, 0, 0);
2997 if (typediff) {
2998 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2999 show_ident(sym->ident),
3000 stream_name(next->pos.stream), next->pos.line, typediff);
3001 return;
3004 if (!declared) {
3005 unsigned long mod = sym->ctype.modifiers;
3006 if (mod & (MOD_STATIC | MOD_REGISTER))
3007 return;
3008 if (!(mod & MOD_TOPLEVEL))
3009 return;
3010 if (!Wdecl)
3011 return;
3012 if (sym->ident == &main_ident)
3013 return;
3014 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
3018 static struct symbol *evaluate_symbol(struct symbol *sym)
3020 struct symbol *base_type;
3022 if (!sym)
3023 return sym;
3024 if (sym->evaluated)
3025 return sym;
3026 sym->evaluated = 1;
3028 sym = examine_symbol_type(sym);
3029 base_type = get_base_type(sym);
3030 if (!base_type)
3031 return NULL;
3033 /* Evaluate the initializers */
3034 if (sym->initializer)
3035 evaluate_initializer(sym, &sym->initializer);
3037 /* And finally, evaluate the body of the symbol too */
3038 if (base_type->type == SYM_FN) {
3039 struct symbol *curr = current_fn;
3041 current_fn = base_type;
3043 examine_fn_arguments(base_type);
3044 if (!base_type->stmt && base_type->inline_stmt)
3045 uninline(sym);
3046 if (base_type->stmt)
3047 evaluate_statement(base_type->stmt);
3049 current_fn = curr;
3052 return base_type;
3055 void evaluate_symbol_list(struct symbol_list *list)
3057 struct symbol *sym;
3059 FOR_EACH_PTR(list, sym) {
3060 evaluate_symbol(sym);
3061 check_duplicates(sym);
3062 } END_FOR_EACH_PTR(sym);
3065 static struct symbol *evaluate_return_expression(struct statement *stmt)
3067 struct expression *expr = stmt->expression;
3068 struct symbol *fntype;
3070 evaluate_expression(expr);
3071 fntype = current_fn->ctype.base_type;
3072 if (!fntype || fntype == &void_ctype) {
3073 if (expr && expr->ctype != &void_ctype)
3074 expression_error(expr, "return expression in %s function", fntype?"void":"typeless");
3075 if (expr && Wreturn_void)
3076 warning(stmt->pos, "returning void-valued expression");
3077 return NULL;
3080 if (!expr) {
3081 sparse_error(stmt->pos, "return with no return value");
3082 return NULL;
3084 if (!expr->ctype)
3085 return NULL;
3086 compatible_assignment_types(expr, fntype, &stmt->expression, "return expression");
3087 return NULL;
3090 static void evaluate_if_statement(struct statement *stmt)
3092 if (!stmt->if_conditional)
3093 return;
3095 evaluate_conditional(stmt->if_conditional, 0);
3096 evaluate_statement(stmt->if_true);
3097 evaluate_statement(stmt->if_false);
3100 static void evaluate_iterator(struct statement *stmt)
3102 evaluate_conditional(stmt->iterator_pre_condition, 1);
3103 evaluate_conditional(stmt->iterator_post_condition,1);
3104 evaluate_statement(stmt->iterator_pre_statement);
3105 evaluate_statement(stmt->iterator_statement);
3106 evaluate_statement(stmt->iterator_post_statement);
3109 static void verify_output_constraint(struct expression *expr, const char *constraint)
3111 switch (*constraint) {
3112 case '=': /* Assignment */
3113 case '+': /* Update */
3114 break;
3115 default:
3116 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
3120 static void verify_input_constraint(struct expression *expr, const char *constraint)
3122 switch (*constraint) {
3123 case '=': /* Assignment */
3124 case '+': /* Update */
3125 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
3129 static void evaluate_asm_statement(struct statement *stmt)
3131 struct expression *expr;
3132 int state;
3134 expr = stmt->asm_string;
3135 if (!expr || expr->type != EXPR_STRING) {
3136 sparse_error(stmt->pos, "need constant string for inline asm");
3137 return;
3140 state = 0;
3141 FOR_EACH_PTR(stmt->asm_outputs, expr) {
3142 struct ident *ident;
3144 switch (state) {
3145 case 0: /* Identifier */
3146 state = 1;
3147 ident = (struct ident *)expr;
3148 continue;
3150 case 1: /* Constraint */
3151 state = 2;
3152 if (!expr || expr->type != EXPR_STRING) {
3153 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
3154 *THIS_ADDRESS(expr) = NULL;
3155 continue;
3157 verify_output_constraint(expr, expr->string->data);
3158 continue;
3160 case 2: /* Expression */
3161 state = 0;
3162 if (!evaluate_expression(expr))
3163 return;
3164 if (!lvalue_expression(expr))
3165 warning(expr->pos, "asm output is not an lvalue");
3166 evaluate_assign_to(expr, expr->ctype);
3167 continue;
3169 } END_FOR_EACH_PTR(expr);
3171 state = 0;
3172 FOR_EACH_PTR(stmt->asm_inputs, expr) {
3173 struct ident *ident;
3175 switch (state) {
3176 case 0: /* Identifier */
3177 state = 1;
3178 ident = (struct ident *)expr;
3179 continue;
3181 case 1: /* Constraint */
3182 state = 2;
3183 if (!expr || expr->type != EXPR_STRING) {
3184 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
3185 *THIS_ADDRESS(expr) = NULL;
3186 continue;
3188 verify_input_constraint(expr, expr->string->data);
3189 continue;
3191 case 2: /* Expression */
3192 state = 0;
3193 if (!evaluate_expression(expr))
3194 return;
3195 continue;
3197 } END_FOR_EACH_PTR(expr);
3199 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
3200 if (!expr) {
3201 sparse_error(stmt->pos, "bad asm output");
3202 return;
3204 if (expr->type == EXPR_STRING)
3205 continue;
3206 expression_error(expr, "asm clobber is not a string");
3207 } END_FOR_EACH_PTR(expr);
3210 static void evaluate_case_statement(struct statement *stmt)
3212 evaluate_expression(stmt->case_expression);
3213 evaluate_expression(stmt->case_to);
3214 evaluate_statement(stmt->case_statement);
3217 static void check_case_type(struct expression *switch_expr,
3218 struct expression *case_expr,
3219 struct expression **enumcase)
3221 struct symbol *switch_type, *case_type;
3222 int sclass, cclass;
3224 if (!case_expr)
3225 return;
3227 switch_type = switch_expr->ctype;
3228 case_type = evaluate_expression(case_expr);
3230 if (!switch_type || !case_type)
3231 goto Bad;
3232 if (enumcase) {
3233 if (*enumcase)
3234 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
3235 else if (is_enum_type(case_type))
3236 *enumcase = case_expr;
3239 sclass = classify_type(switch_type, &switch_type);
3240 cclass = classify_type(case_type, &case_type);
3242 /* both should be arithmetic */
3243 if (!(sclass & cclass & TYPE_NUM))
3244 goto Bad;
3246 /* neither should be floating */
3247 if ((sclass | cclass) & TYPE_FLOAT)
3248 goto Bad;
3250 /* if neither is restricted, we are OK */
3251 if (!((sclass | cclass) & TYPE_RESTRICT))
3252 return;
3254 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
3255 cclass, sclass, case_type, switch_type)) {
3256 unrestrict(case_expr, cclass, &case_type);
3257 unrestrict(switch_expr, sclass, &switch_type);
3259 return;
3261 Bad:
3262 expression_error(case_expr, "incompatible types for 'case' statement");
3265 static void evaluate_switch_statement(struct statement *stmt)
3267 struct symbol *sym;
3268 struct expression *enumcase = NULL;
3269 struct expression **enumcase_holder = &enumcase;
3270 struct expression *sel = stmt->switch_expression;
3272 evaluate_expression(sel);
3273 evaluate_statement(stmt->switch_statement);
3274 if (!sel)
3275 return;
3276 if (sel->ctype && is_enum_type(sel->ctype))
3277 enumcase_holder = NULL; /* Only check cases against switch */
3279 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
3280 struct statement *case_stmt = sym->stmt;
3281 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
3282 check_case_type(sel, case_stmt->case_to, enumcase_holder);
3283 } END_FOR_EACH_PTR(sym);
3286 struct symbol *evaluate_statement(struct statement *stmt)
3288 if (!stmt)
3289 return NULL;
3291 switch (stmt->type) {
3292 case STMT_DECLARATION: {
3293 struct symbol *s;
3294 FOR_EACH_PTR(stmt->declaration, s) {
3295 evaluate_symbol(s);
3296 } END_FOR_EACH_PTR(s);
3297 return NULL;
3300 case STMT_RETURN:
3301 return evaluate_return_expression(stmt);
3303 case STMT_EXPRESSION:
3304 if (!evaluate_expression(stmt->expression))
3305 return NULL;
3306 if (stmt->expression->ctype == &null_ctype)
3307 stmt->expression = cast_to(stmt->expression, &ptr_ctype);
3308 return degenerate(stmt->expression);
3310 case STMT_COMPOUND: {
3311 struct statement *s;
3312 struct symbol *type = NULL;
3314 /* Evaluate the return symbol in the compound statement */
3315 evaluate_symbol(stmt->ret);
3318 * Then, evaluate each statement, making the type of the
3319 * compound statement be the type of the last statement
3321 type = evaluate_statement(stmt->args);
3322 FOR_EACH_PTR(stmt->stmts, s) {
3323 type = evaluate_statement(s);
3324 } END_FOR_EACH_PTR(s);
3325 if (!type)
3326 type = &void_ctype;
3327 return type;
3329 case STMT_IF:
3330 evaluate_if_statement(stmt);
3331 return NULL;
3332 case STMT_ITERATOR:
3333 evaluate_iterator(stmt);
3334 return NULL;
3335 case STMT_SWITCH:
3336 evaluate_switch_statement(stmt);
3337 return NULL;
3338 case STMT_CASE:
3339 evaluate_case_statement(stmt);
3340 return NULL;
3341 case STMT_LABEL:
3342 return evaluate_statement(stmt->label_statement);
3343 case STMT_GOTO:
3344 evaluate_expression(stmt->goto_expression);
3345 return NULL;
3346 case STMT_NONE:
3347 break;
3348 case STMT_ASM:
3349 evaluate_asm_statement(stmt);
3350 return NULL;
3351 case STMT_CONTEXT:
3352 evaluate_expression(stmt->expression);
3353 return NULL;
3354 case STMT_RANGE:
3355 evaluate_expression(stmt->range_expression);
3356 evaluate_expression(stmt->range_low);
3357 evaluate_expression(stmt->range_high);
3358 return NULL;
3360 return NULL;