[PATCH] saner -Wtypesign
[smatch.git] / evaluate.c
blob6db1354889bdbae4d2b9a05b9e766523e49bde74
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 warning(expr->pos, "restricted degrades to integer");
504 if (class & TYPE_FOULED)
505 *ctype = unfoul(*ctype);
506 *ctype = (*ctype)->ctype.base_type; /* get to arithmetic type */
510 static struct symbol *usual_conversions(int op,
511 struct expression *left,
512 struct expression *right,
513 int lclass, int rclass,
514 struct symbol *ltype,
515 struct symbol *rtype)
517 struct symbol *ctype;
519 warn_for_different_enum_types(right->pos, left->ctype, right->ctype);
521 if ((lclass | rclass) & TYPE_RESTRICT)
522 goto Restr;
524 Normal:
525 if (!(lclass & TYPE_FLOAT)) {
526 if (!(rclass & TYPE_FLOAT))
527 return bigger_int_type(ltype, rtype);
528 else
529 return rtype;
530 } else if (rclass & TYPE_FLOAT) {
531 unsigned long lmod = ltype->ctype.modifiers;
532 unsigned long rmod = rtype->ctype.modifiers;
533 if (rmod & ~lmod & (MOD_LONG | MOD_LONGLONG))
534 return rtype;
535 else
536 return ltype;
537 } else
538 return ltype;
540 Restr:
541 ctype = restricted_binop_type(op, left, right,
542 lclass, rclass, ltype, rtype);
543 if (ctype)
544 return ctype;
546 unrestrict(left, lclass, &ltype);
547 unrestrict(right, rclass, &rtype);
549 goto Normal;
552 static inline int lvalue_expression(struct expression *expr)
554 return expr->type == EXPR_PREOP && expr->op == '*';
557 static inline int is_function(struct symbol *type)
559 return type && type->type == SYM_FN;
562 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *itype)
564 struct expression *index = expr->right;
565 struct symbol *ctype, *base;
566 int multiply;
568 classify_type(degenerate(expr->left), &ctype);
569 base = examine_pointer_target(ctype);
571 if (!base) {
572 expression_error(expr, "missing type information");
573 return NULL;
575 if (is_function(base)) {
576 expression_error(expr, "arithmetics on pointers to functions");
577 return NULL;
580 /* Get the size of whatever the pointer points to */
581 multiply = base->bit_size >> 3;
583 if (ctype == &null_ctype)
584 ctype = &ptr_ctype;
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 else if (diff & ~MOD_SIGNEDNESS)
772 return "different modifiers";
773 else
774 return "different signedness";
776 t1 = base1;
777 t2 = base2;
779 if (Waddress_space && as1 != as2)
780 return "different address spaces";
781 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
782 return "different modifiers";
783 return NULL;
786 static void bad_null(struct expression *expr)
788 if (Wnon_pointer_null)
789 warning(expr->pos, "Using plain integer as NULL pointer");
792 static unsigned long target_qualifiers(struct symbol *type)
794 unsigned long mod = type->ctype.modifiers & MOD_IGN;
795 if (type->ctype.base_type && type->ctype.base_type->type == SYM_ARRAY)
796 mod = 0;
797 return mod;
800 static struct symbol *evaluate_ptr_sub(struct expression *expr)
802 const char *typediff;
803 struct symbol *ltype, *rtype;
804 struct expression *l = expr->left;
805 struct expression *r = expr->right;
806 struct symbol *lbase, *rbase;
808 classify_type(degenerate(l), &ltype);
809 classify_type(degenerate(r), &rtype);
811 lbase = examine_pointer_target(ltype);
812 rbase = examine_pointer_target(rtype);
813 typediff = type_difference(&ltype->ctype, &rtype->ctype,
814 target_qualifiers(rtype),
815 target_qualifiers(ltype));
816 if (typediff)
817 expression_error(expr, "subtraction of different types can't work (%s)", typediff);
819 if (is_function(lbase)) {
820 expression_error(expr, "subtraction of functions? Share your drugs");
821 return NULL;
824 expr->ctype = ssize_t_ctype;
825 if (lbase->bit_size > bits_in_char) {
826 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
827 struct expression *div = expr;
828 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
829 unsigned long value = lbase->bit_size >> 3;
831 val->ctype = size_t_ctype;
832 val->value = value;
834 if (value & (value-1)) {
835 if (Wptr_subtraction_blows)
836 warning(expr->pos, "potentially expensive pointer subtraction");
839 sub->op = '-';
840 sub->ctype = ssize_t_ctype;
841 sub->left = l;
842 sub->right = r;
844 div->op = '/';
845 div->left = sub;
846 div->right = val;
849 return ssize_t_ctype;
852 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
854 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
856 struct symbol *ctype;
858 if (!expr)
859 return NULL;
861 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
862 warning(expr->pos, "assignment expression in conditional");
864 ctype = evaluate_expression(expr);
865 if (ctype) {
866 if (is_safe_type(ctype))
867 warning(expr->pos, "testing a 'safe expression'");
870 return ctype;
873 static struct symbol *evaluate_logical(struct expression *expr)
875 if (!evaluate_conditional(expr->left, 0))
876 return NULL;
877 if (!evaluate_conditional(expr->right, 0))
878 return NULL;
880 expr->ctype = &bool_ctype;
881 if (expr->flags) {
882 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
883 expr->flags = 0;
885 return &bool_ctype;
888 static struct symbol *evaluate_binop(struct expression *expr)
890 struct symbol *ltype, *rtype, *ctype;
891 int lclass = classify_type(expr->left->ctype, &ltype);
892 int rclass = classify_type(expr->right->ctype, &rtype);
893 int op = expr->op;
895 if (expr->flags) {
896 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
897 expr->flags = 0;
900 /* number op number */
901 if (lclass & rclass & TYPE_NUM) {
902 if ((lclass | rclass) & TYPE_FLOAT) {
903 switch (op) {
904 case '+': case '-': case '*': case '/':
905 break;
906 default:
907 return bad_expr_type(expr);
911 if (op == SPECIAL_LEFTSHIFT || op == SPECIAL_RIGHTSHIFT) {
912 // shifts do integer promotions, but that's it.
913 unrestrict(expr->left, lclass, &ltype);
914 unrestrict(expr->right, rclass, &rtype);
915 ctype = ltype = integer_promotion(ltype);
916 rtype = integer_promotion(rtype);
917 } else {
918 // The rest do usual conversions
919 ltype = usual_conversions(op, expr->left, expr->right,
920 lclass, rclass, ltype, rtype);
921 ctype = rtype = ltype;
924 expr->left = cast_to(expr->left, ltype);
925 expr->right = cast_to(expr->right, rtype);
926 expr->ctype = ctype;
927 return ctype;
930 /* pointer (+|-) integer */
931 if (lclass & TYPE_PTR && is_int(rclass) && (op == '+' || op == '-')) {
932 unrestrict(expr->right, rclass, &rtype);
933 return evaluate_ptr_add(expr, rtype);
936 /* integer + pointer */
937 if (rclass & TYPE_PTR && is_int(lclass) && op == '+') {
938 struct expression *index = expr->left;
939 unrestrict(index, lclass, &ltype);
940 expr->left = expr->right;
941 expr->right = index;
942 return evaluate_ptr_add(expr, ltype);
945 /* pointer - pointer */
946 if (lclass & rclass & TYPE_PTR && expr->op == '-')
947 return evaluate_ptr_sub(expr);
949 return bad_expr_type(expr);
952 static struct symbol *evaluate_comma(struct expression *expr)
954 expr->ctype = degenerate(expr->right);
955 if (expr->ctype == &null_ctype)
956 expr->ctype = &ptr_ctype;
957 expr->flags &= expr->left->flags & expr->right->flags;
958 return expr->ctype;
961 static int modify_for_unsigned(int op)
963 if (op == '<')
964 op = SPECIAL_UNSIGNED_LT;
965 else if (op == '>')
966 op = SPECIAL_UNSIGNED_GT;
967 else if (op == SPECIAL_LTE)
968 op = SPECIAL_UNSIGNED_LTE;
969 else if (op == SPECIAL_GTE)
970 op = SPECIAL_UNSIGNED_GTE;
971 return op;
974 static inline int is_null_pointer_constant(struct expression *e)
976 if (e->ctype == &null_ctype)
977 return 1;
978 if (!(e->flags & Int_const_expr))
979 return 0;
980 return is_zero_constant(e) ? 2 : 0;
983 static struct symbol *evaluate_compare(struct expression *expr)
985 struct expression *left = expr->left, *right = expr->right;
986 struct symbol *ltype, *rtype, *lbase, *rbase;
987 int lclass = classify_type(degenerate(left), &ltype);
988 int rclass = classify_type(degenerate(right), &rtype);
989 struct symbol *ctype;
990 const char *typediff;
992 if (expr->flags) {
993 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
994 expr->flags = 0;
997 /* Type types? */
998 if (is_type_type(ltype) && is_type_type(rtype))
999 goto OK;
1001 if (is_safe_type(left->ctype) || is_safe_type(right->ctype))
1002 warning(expr->pos, "testing a 'safe expression'");
1004 /* number on number */
1005 if (lclass & rclass & TYPE_NUM) {
1006 ctype = usual_conversions(expr->op, expr->left, expr->right,
1007 lclass, rclass, ltype, rtype);
1008 expr->left = cast_to(expr->left, ctype);
1009 expr->right = cast_to(expr->right, ctype);
1010 if (ctype->ctype.modifiers & MOD_UNSIGNED)
1011 expr->op = modify_for_unsigned(expr->op);
1012 goto OK;
1015 /* at least one must be a pointer */
1016 if (!((lclass | rclass) & TYPE_PTR))
1017 return bad_expr_type(expr);
1019 /* equality comparisons can be with null pointer constants */
1020 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1021 int is_null1 = is_null_pointer_constant(left);
1022 int is_null2 = is_null_pointer_constant(right);
1023 if (is_null1 == 2)
1024 bad_null(left);
1025 if (is_null2 == 2)
1026 bad_null(right);
1027 if (is_null1 && is_null2) {
1028 int positive = expr->op == SPECIAL_EQUAL;
1029 expr->type = EXPR_VALUE;
1030 expr->value = positive;
1031 goto OK;
1033 if (is_null1 && (rclass & TYPE_PTR)) {
1034 left = cast_to(left, rtype);
1035 goto OK;
1037 if (is_null2 && (lclass & TYPE_PTR)) {
1038 right = cast_to(right, ltype);
1039 goto OK;
1042 /* both should be pointers */
1043 if (!(lclass & rclass & TYPE_PTR))
1044 return bad_expr_type(expr);
1045 expr->op = modify_for_unsigned(expr->op);
1047 lbase = examine_pointer_target(ltype);
1048 rbase = examine_pointer_target(rtype);
1050 /* they also have special treatment for pointers to void */
1051 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1052 if (ltype->ctype.as == rtype->ctype.as) {
1053 if (lbase == &void_ctype) {
1054 right = cast_to(right, ltype);
1055 goto OK;
1057 if (rbase == &void_ctype) {
1058 left = cast_to(left, rtype);
1059 goto OK;
1064 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1065 target_qualifiers(rtype),
1066 target_qualifiers(ltype));
1067 if (!typediff)
1068 goto OK;
1070 expression_error(expr, "incompatible types in comparison expression (%s)", typediff);
1071 return NULL;
1074 expr->ctype = &bool_ctype;
1075 return &bool_ctype;
1079 * NOTE! The degenerate case of "x ? : y", where we don't
1080 * have a true case, this will possibly promote "x" to the
1081 * same type as "y", and thus _change_ the conditional
1082 * test in the expression. But since promotion is "safe"
1083 * for testing, that's OK.
1085 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1087 struct expression **true;
1088 struct symbol *ctype, *ltype, *rtype, *lbase, *rbase;
1089 int lclass, rclass;
1090 const char * typediff;
1091 int qual;
1093 if (!evaluate_conditional(expr->conditional, 0))
1094 return NULL;
1095 if (!evaluate_expression(expr->cond_false))
1096 return NULL;
1098 ctype = degenerate(expr->conditional);
1099 rtype = degenerate(expr->cond_false);
1101 true = &expr->conditional;
1102 ltype = ctype;
1103 if (expr->cond_true) {
1104 if (!evaluate_expression(expr->cond_true))
1105 return NULL;
1106 ltype = degenerate(expr->cond_true);
1107 true = &expr->cond_true;
1110 if (expr->flags) {
1111 int flags = expr->conditional->flags & Int_const_expr;
1112 flags &= (*true)->flags & expr->cond_false->flags;
1113 if (!flags)
1114 expr->flags = 0;
1117 lclass = classify_type(ltype, &ltype);
1118 rclass = classify_type(rtype, &rtype);
1119 if (lclass & rclass & TYPE_NUM) {
1120 ctype = usual_conversions('?', *true, expr->cond_false,
1121 lclass, rclass, ltype, rtype);
1122 *true = cast_to(*true, ctype);
1123 expr->cond_false = cast_to(expr->cond_false, ctype);
1124 goto out;
1127 if ((lclass | rclass) & TYPE_PTR) {
1128 int is_null1 = is_null_pointer_constant(*true);
1129 int is_null2 = is_null_pointer_constant(expr->cond_false);
1131 if (is_null1 && is_null2) {
1132 *true = cast_to(*true, &ptr_ctype);
1133 expr->cond_false = cast_to(expr->cond_false, &ptr_ctype);
1134 ctype = &ptr_ctype;
1135 goto out;
1137 if (is_null1 && (rclass & TYPE_PTR)) {
1138 if (is_null1 == 2)
1139 bad_null(*true);
1140 *true = cast_to(*true, rtype);
1141 ctype = rtype;
1142 goto out;
1144 if (is_null2 && (lclass & TYPE_PTR)) {
1145 if (is_null2 == 2)
1146 bad_null(expr->cond_false);
1147 expr->cond_false = cast_to(expr->cond_false, ltype);
1148 ctype = ltype;
1149 goto out;
1151 if (!(lclass & rclass & TYPE_PTR)) {
1152 typediff = "different types";
1153 goto Err;
1155 /* OK, it's pointer on pointer */
1156 if (ltype->ctype.as != rtype->ctype.as) {
1157 typediff = "different address spaces";
1158 goto Err;
1161 /* need to be lazier here */
1162 lbase = examine_pointer_target(ltype);
1163 rbase = examine_pointer_target(rtype);
1164 qual = target_qualifiers(ltype) | target_qualifiers(rtype);
1166 if (lbase == &void_ctype) {
1167 /* XXX: pointers to function should warn here */
1168 ctype = ltype;
1169 goto Qual;
1172 if (rbase == &void_ctype) {
1173 /* XXX: pointers to function should warn here */
1174 ctype = rtype;
1175 goto Qual;
1177 /* XXX: that should be pointer to composite */
1178 ctype = ltype;
1179 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1180 qual, qual);
1181 if (!typediff)
1182 goto Qual;
1183 goto Err;
1186 /* void on void, struct on same struct, union on same union */
1187 if (ltype == rtype) {
1188 ctype = ltype;
1189 goto out;
1191 typediff = "different base types";
1193 Err:
1194 expression_error(expr, "incompatible types in conditional expression (%s)", typediff);
1195 return NULL;
1197 out:
1198 expr->ctype = ctype;
1199 return ctype;
1201 Qual:
1202 if (qual & ~ctype->ctype.modifiers) {
1203 struct symbol *sym = alloc_symbol(ctype->pos, SYM_PTR);
1204 *sym = *ctype;
1205 sym->ctype.modifiers |= qual;
1206 ctype = sym;
1208 *true = cast_to(*true, ctype);
1209 expr->cond_false = cast_to(expr->cond_false, ctype);
1210 goto out;
1213 /* FP assignments can not do modulo or bit operations */
1214 static int compatible_float_op(int op)
1216 return op == SPECIAL_ADD_ASSIGN ||
1217 op == SPECIAL_SUB_ASSIGN ||
1218 op == SPECIAL_MUL_ASSIGN ||
1219 op == SPECIAL_DIV_ASSIGN;
1222 static int evaluate_assign_op(struct expression *expr)
1224 struct symbol *target = expr->left->ctype;
1225 struct symbol *source = expr->right->ctype;
1226 struct symbol *t, *s;
1227 int tclass = classify_type(target, &t);
1228 int sclass = classify_type(source, &s);
1229 int op = expr->op;
1231 if (tclass & sclass & TYPE_NUM) {
1232 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1233 expression_error(expr, "invalid assignment");
1234 return 0;
1236 if (tclass & TYPE_RESTRICT) {
1237 if (!restricted_binop(op, t)) {
1238 expression_error(expr, "bad restricted assignment");
1239 return 0;
1241 /* allowed assignments unfoul */
1242 if (sclass & TYPE_FOULED && unfoul(s) == t)
1243 goto Cast;
1244 if (!restricted_value(expr->right, t))
1245 return 1;
1246 } else if (!(sclass & TYPE_RESTRICT))
1247 goto Cast;
1248 /* source and target would better be identical restricted */
1249 if (t == s)
1250 return 1;
1251 warning(expr->pos, "invalid restricted assignment");
1252 expr->right = cast_to(expr->right, target);
1253 return 0;
1255 if (tclass == TYPE_PTR && is_int(sclass)) {
1256 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1257 unrestrict(expr->right, sclass, &s);
1258 evaluate_ptr_add(expr, s);
1259 return 1;
1261 expression_error(expr, "invalid pointer assignment");
1262 return 0;
1265 expression_error(expr, "invalid assignment");
1266 return 0;
1268 Cast:
1269 expr->right = cast_to(expr->right, target);
1270 return 1;
1273 static int whitelist_pointers(struct symbol *t1, struct symbol *t2)
1275 if (t1 == t2)
1276 return 0; /* yes, 0 - we don't want a cast_to here */
1277 if (t1 == &void_ctype)
1278 return 1;
1279 if (t2 == &void_ctype)
1280 return 1;
1281 if (classify_type(t1, &t1) != TYPE_NUM)
1282 return 0;
1283 if (classify_type(t2, &t2) != TYPE_NUM)
1284 return 0;
1285 if (t1 == t2)
1286 return 1;
1287 if (t1->ctype.modifiers & t2->ctype.modifiers & MOD_CHAR)
1288 return 1;
1289 if ((t1->ctype.modifiers ^ t2->ctype.modifiers) & MOD_SIZE)
1290 return 0;
1291 return !Wtypesign;
1294 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1295 struct expression **rp, const char *where)
1297 const char *typediff;
1298 struct symbol *source = degenerate(*rp);
1299 struct symbol *t, *s;
1300 int tclass = classify_type(target, &t);
1301 int sclass = classify_type(source, &s);
1303 if (tclass & sclass & TYPE_NUM) {
1304 if (tclass & TYPE_RESTRICT) {
1305 /* allowed assignments unfoul */
1306 if (sclass & TYPE_FOULED && unfoul(s) == t)
1307 goto Cast;
1308 if (!restricted_value(*rp, target))
1309 return 1;
1310 if (s == t)
1311 return 1;
1312 } else if (!(sclass & TYPE_RESTRICT))
1313 goto Cast;
1314 typediff = "different base types";
1315 goto Err;
1318 if (tclass == TYPE_PTR) {
1319 unsigned long mod1, mod2;
1320 struct symbol *b1, *b2;
1321 // NULL pointer is always OK
1322 int is_null = is_null_pointer_constant(*rp);
1323 if (is_null) {
1324 if (is_null == 2)
1325 bad_null(*rp);
1326 goto Cast;
1328 if (!(sclass & TYPE_PTR)) {
1329 typediff = "different base types";
1330 goto Err;
1332 b1 = examine_pointer_target(t);
1333 b2 = examine_pointer_target(s);
1334 mod1 = target_qualifiers(t);
1335 mod2 = target_qualifiers(s);
1336 if (whitelist_pointers(b1, b2)) {
1338 * assignments to/from void * are OK, provided that
1339 * we do not remove qualifiers from pointed to [C]
1340 * or mix address spaces [sparse].
1342 if (t->ctype.as != s->ctype.as) {
1343 typediff = "different address spaces";
1344 goto Err;
1346 if (mod2 & ~mod1) {
1347 typediff = "different modifiers";
1348 goto Err;
1350 goto Cast;
1352 /* It's OK if the target is more volatile or const than the source */
1353 typediff = type_difference(&t->ctype, &s->ctype, 0, mod1);
1354 if (typediff)
1355 goto Err;
1356 return 1;
1359 if ((tclass & TYPE_COMPOUND) && s == t)
1360 return 1;
1362 if (tclass & TYPE_NUM) {
1363 /* XXX: need to turn into comparison with NULL */
1364 if (t == &bool_ctype && (sclass & TYPE_PTR))
1365 goto Cast;
1366 typediff = "different base types";
1367 goto Err;
1369 typediff = "invalid types";
1371 Err:
1372 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1373 info(expr->pos, " expected %s", show_typename(target));
1374 info(expr->pos, " got %s", show_typename(source));
1375 *rp = cast_to(*rp, target);
1376 return 0;
1377 Cast:
1378 *rp = cast_to(*rp, target);
1379 return 1;
1382 static void mark_assigned(struct expression *expr)
1384 struct symbol *sym;
1386 if (!expr)
1387 return;
1388 switch (expr->type) {
1389 case EXPR_SYMBOL:
1390 sym = expr->symbol;
1391 if (!sym)
1392 return;
1393 if (sym->type != SYM_NODE)
1394 return;
1395 sym->ctype.modifiers |= MOD_ASSIGNED;
1396 return;
1398 case EXPR_BINOP:
1399 mark_assigned(expr->left);
1400 mark_assigned(expr->right);
1401 return;
1402 case EXPR_CAST:
1403 case EXPR_FORCE_CAST:
1404 mark_assigned(expr->cast_expression);
1405 return;
1406 case EXPR_SLICE:
1407 mark_assigned(expr->base);
1408 return;
1409 default:
1410 /* Hmm? */
1411 return;
1415 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1417 if (type->ctype.modifiers & MOD_CONST)
1418 expression_error(left, "assignment to const expression");
1420 /* We know left is an lvalue, so it's a "preop-*" */
1421 mark_assigned(left->unop);
1424 static struct symbol *evaluate_assignment(struct expression *expr)
1426 struct expression *left = expr->left;
1427 struct expression *where = expr;
1428 struct symbol *ltype;
1430 if (!lvalue_expression(left)) {
1431 expression_error(expr, "not an lvalue");
1432 return NULL;
1435 ltype = left->ctype;
1437 if (expr->op != '=') {
1438 if (!evaluate_assign_op(expr))
1439 return NULL;
1440 } else {
1441 if (!compatible_assignment_types(where, ltype, &expr->right, "assignment"))
1442 return NULL;
1445 evaluate_assign_to(left, ltype);
1447 expr->ctype = ltype;
1448 return ltype;
1451 static void examine_fn_arguments(struct symbol *fn)
1453 struct symbol *s;
1455 FOR_EACH_PTR(fn->arguments, s) {
1456 struct symbol *arg = evaluate_symbol(s);
1457 /* Array/function arguments silently degenerate into pointers */
1458 if (arg) {
1459 struct symbol *ptr;
1460 switch(arg->type) {
1461 case SYM_ARRAY:
1462 case SYM_FN:
1463 ptr = alloc_symbol(s->pos, SYM_PTR);
1464 if (arg->type == SYM_ARRAY)
1465 ptr->ctype = arg->ctype;
1466 else
1467 ptr->ctype.base_type = arg;
1468 ptr->ctype.as |= s->ctype.as;
1469 ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1471 s->ctype.base_type = ptr;
1472 s->ctype.as = 0;
1473 s->ctype.modifiers &= ~MOD_PTRINHERIT;
1474 s->bit_size = 0;
1475 s->examined = 0;
1476 examine_symbol_type(s);
1477 break;
1478 default:
1479 /* nothing */
1480 break;
1483 } END_FOR_EACH_PTR(s);
1486 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1488 /* Take the modifiers of the pointer, and apply them to the member */
1489 mod |= sym->ctype.modifiers;
1490 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1491 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1492 *newsym = *sym;
1493 newsym->ctype.as = as;
1494 newsym->ctype.modifiers = mod;
1495 sym = newsym;
1497 return sym;
1500 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1502 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1503 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1505 node->ctype.base_type = ptr;
1506 ptr->bit_size = bits_in_pointer;
1507 ptr->ctype.alignment = pointer_alignment;
1509 node->bit_size = bits_in_pointer;
1510 node->ctype.alignment = pointer_alignment;
1512 access_symbol(sym);
1513 if (sym->ctype.modifiers & MOD_REGISTER) {
1514 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1515 sym->ctype.modifiers &= ~MOD_REGISTER;
1517 if (sym->type == SYM_NODE) {
1518 ptr->ctype.as |= sym->ctype.as;
1519 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1520 sym = sym->ctype.base_type;
1522 if (degenerate && sym->type == SYM_ARRAY) {
1523 ptr->ctype.as |= sym->ctype.as;
1524 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1525 sym = sym->ctype.base_type;
1527 ptr->ctype.base_type = sym;
1529 return node;
1532 /* Arrays degenerate into pointers on pointer arithmetic */
1533 static struct symbol *degenerate(struct expression *expr)
1535 struct symbol *ctype, *base;
1537 if (!expr)
1538 return NULL;
1539 ctype = expr->ctype;
1540 if (!ctype)
1541 return NULL;
1542 base = examine_symbol_type(ctype);
1543 if (ctype->type == SYM_NODE)
1544 base = ctype->ctype.base_type;
1546 * Arrays degenerate into pointers to the entries, while
1547 * functions degenerate into pointers to themselves.
1548 * If array was part of non-lvalue compound, we create a copy
1549 * of that compound first and then act as if we were dealing with
1550 * the corresponding field in there.
1552 switch (base->type) {
1553 case SYM_ARRAY:
1554 if (expr->type == EXPR_SLICE) {
1555 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1556 struct expression *e0, *e1, *e2, *e3, *e4;
1558 a->ctype.base_type = expr->base->ctype;
1559 a->bit_size = expr->base->ctype->bit_size;
1560 a->array_size = expr->base->ctype->array_size;
1562 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1563 e0->symbol = a;
1564 e0->ctype = &lazy_ptr_ctype;
1566 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1567 e1->unop = e0;
1568 e1->op = '*';
1569 e1->ctype = expr->base->ctype; /* XXX */
1571 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1572 e2->left = e1;
1573 e2->right = expr->base;
1574 e2->op = '=';
1575 e2->ctype = expr->base->ctype;
1577 if (expr->r_bitpos) {
1578 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1579 e3->op = '+';
1580 e3->left = e0;
1581 e3->right = alloc_const_expression(expr->pos,
1582 expr->r_bitpos >> 3);
1583 e3->ctype = &lazy_ptr_ctype;
1584 } else {
1585 e3 = e0;
1588 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1589 e4->left = e2;
1590 e4->right = e3;
1591 e4->ctype = &lazy_ptr_ctype;
1593 expr->unop = e4;
1594 expr->type = EXPR_PREOP;
1595 expr->op = '*';
1597 case SYM_FN:
1598 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1599 expression_error(expr, "strange non-value function or array");
1600 return &bad_ctype;
1602 *expr = *expr->unop;
1603 ctype = create_pointer(expr, ctype, 1);
1604 expr->ctype = ctype;
1605 default:
1606 /* nothing */;
1608 return ctype;
1611 static struct symbol *evaluate_addressof(struct expression *expr)
1613 struct expression *op = expr->unop;
1614 struct symbol *ctype;
1616 if (op->op != '*' || op->type != EXPR_PREOP) {
1617 expression_error(expr, "not addressable");
1618 return NULL;
1620 ctype = op->ctype;
1621 *expr = *op->unop;
1622 expr->flags = 0;
1624 if (expr->type == EXPR_SYMBOL) {
1625 struct symbol *sym = expr->symbol;
1626 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1630 * symbol expression evaluation is lazy about the type
1631 * of the sub-expression, so we may have to generate
1632 * the type here if so..
1634 if (expr->ctype == &lazy_ptr_ctype) {
1635 ctype = create_pointer(expr, ctype, 0);
1636 expr->ctype = ctype;
1638 return expr->ctype;
1642 static struct symbol *evaluate_dereference(struct expression *expr)
1644 struct expression *op = expr->unop;
1645 struct symbol *ctype = op->ctype, *node, *target;
1647 /* Simplify: *&(expr) => (expr) */
1648 if (op->type == EXPR_PREOP && op->op == '&') {
1649 *expr = *op->unop;
1650 expr->flags = 0;
1651 return expr->ctype;
1654 /* Dereferencing a node drops all the node information. */
1655 if (ctype->type == SYM_NODE)
1656 ctype = ctype->ctype.base_type;
1658 node = alloc_symbol(expr->pos, SYM_NODE);
1659 target = ctype->ctype.base_type;
1661 switch (ctype->type) {
1662 default:
1663 expression_error(expr, "cannot dereference this type");
1664 return NULL;
1665 case SYM_PTR:
1666 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1667 merge_type(node, ctype);
1668 break;
1670 case SYM_ARRAY:
1671 if (!lvalue_expression(op)) {
1672 expression_error(op, "non-lvalue array??");
1673 return NULL;
1676 /* Do the implied "addressof" on the array */
1677 *op = *op->unop;
1680 * When an array is dereferenced, we need to pick
1681 * up the attributes of the original node too..
1683 merge_type(node, op->ctype);
1684 merge_type(node, ctype);
1685 break;
1688 node->bit_size = target->bit_size;
1689 node->array_size = target->array_size;
1691 expr->ctype = node;
1692 return node;
1696 * Unary post-ops: x++ and x--
1698 static struct symbol *evaluate_postop(struct expression *expr)
1700 struct expression *op = expr->unop;
1701 struct symbol *ctype = op->ctype;
1702 int class = classify_type(op->ctype, &ctype);
1703 int multiply = 0;
1705 if (!lvalue_expression(expr->unop)) {
1706 expression_error(expr, "need lvalue expression for ++/--");
1707 return NULL;
1710 if ((class & TYPE_RESTRICT) && restricted_unop(expr->op, &ctype)) {
1711 expression_error(expr, "bad operation on restricted");
1712 return NULL;
1715 if (class & TYPE_NUM) {
1716 multiply = 1;
1717 } else if (class == TYPE_PTR) {
1718 struct symbol *target = examine_pointer_target(ctype);
1719 if (!is_function(target))
1720 multiply = target->bit_size >> 3;
1723 if (multiply) {
1724 evaluate_assign_to(op, op->ctype);
1725 expr->op_value = multiply;
1726 expr->ctype = ctype;
1727 return ctype;
1730 expression_error(expr, "bad argument type for ++/--");
1731 return NULL;
1734 static struct symbol *evaluate_sign(struct expression *expr)
1736 struct symbol *ctype = expr->unop->ctype;
1737 int class = classify_type(ctype, &ctype);
1738 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1739 expr->flags = 0;
1740 /* should be an arithmetic type */
1741 if (!(class & TYPE_NUM))
1742 return bad_expr_type(expr);
1743 if (!(class & (TYPE_FLOAT|TYPE_RESTRICT))) {
1744 struct symbol *rtype = integer_promotion(ctype);
1745 expr->unop = cast_to(expr->unop, rtype);
1746 ctype = rtype;
1747 } else if ((class & TYPE_FLOAT) && expr->op != '~') {
1748 /* no conversions needed */
1749 } else if ((class & TYPE_RESTRICT) && !restricted_unop(expr->op, &ctype)) {
1750 /* no conversions needed */
1751 } else {
1752 return bad_expr_type(expr);
1754 if (expr->op == '+')
1755 *expr = *expr->unop;
1756 expr->ctype = ctype;
1757 return ctype;
1760 static struct symbol *evaluate_preop(struct expression *expr)
1762 struct symbol *ctype = expr->unop->ctype;
1764 switch (expr->op) {
1765 case '(':
1766 *expr = *expr->unop;
1767 return ctype;
1769 case '+':
1770 case '-':
1771 case '~':
1772 return evaluate_sign(expr);
1774 case '*':
1775 return evaluate_dereference(expr);
1777 case '&':
1778 return evaluate_addressof(expr);
1780 case SPECIAL_INCREMENT:
1781 case SPECIAL_DECREMENT:
1783 * From a type evaluation standpoint the preops are
1784 * the same as the postops
1786 return evaluate_postop(expr);
1788 case '!':
1789 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1790 expr->flags = 0;
1791 if (is_safe_type(ctype))
1792 warning(expr->pos, "testing a 'safe expression'");
1793 if (is_float_type(ctype)) {
1794 struct expression *arg = expr->unop;
1795 expr->type = EXPR_BINOP;
1796 expr->op = SPECIAL_EQUAL;
1797 expr->left = arg;
1798 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1799 expr->right->ctype = ctype;
1800 expr->right->fvalue = 0;
1801 } else if (is_fouled_type(ctype)) {
1802 warning(expr->pos, "restricted degrades to integer");
1804 ctype = &bool_ctype;
1805 break;
1807 default:
1808 break;
1810 expr->ctype = ctype;
1811 return &bool_ctype;
1814 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1816 struct ptr_list *head = (struct ptr_list *)_list;
1817 struct ptr_list *list = head;
1819 if (!head)
1820 return NULL;
1821 do {
1822 int i;
1823 for (i = 0; i < list->nr; i++) {
1824 struct symbol *sym = (struct symbol *) list->list[i];
1825 if (sym->ident) {
1826 if (sym->ident != ident)
1827 continue;
1828 *offset = sym->offset;
1829 return sym;
1830 } else {
1831 struct symbol *ctype = sym->ctype.base_type;
1832 struct symbol *sub;
1833 if (!ctype)
1834 continue;
1835 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1836 continue;
1837 sub = find_identifier(ident, ctype->symbol_list, offset);
1838 if (!sub)
1839 continue;
1840 *offset += sym->offset;
1841 return sub;
1844 } while ((list = list->next) != head);
1845 return NULL;
1848 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1850 struct expression *add;
1853 * Create a new add-expression
1855 * NOTE! Even if we just add zero, we need a new node
1856 * for the member pointer, since it has a different
1857 * type than the original pointer. We could make that
1858 * be just a cast, but the fact is, a node is a node,
1859 * so we might as well just do the "add zero" here.
1861 add = alloc_expression(expr->pos, EXPR_BINOP);
1862 add->op = '+';
1863 add->left = expr;
1864 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1865 add->right->ctype = &int_ctype;
1866 add->right->value = offset;
1869 * The ctype of the pointer will be lazily evaluated if
1870 * we ever take the address of this member dereference..
1872 add->ctype = &lazy_ptr_ctype;
1873 return add;
1876 /* structure/union dereference */
1877 static struct symbol *evaluate_member_dereference(struct expression *expr)
1879 int offset;
1880 struct symbol *ctype, *member;
1881 struct expression *deref = expr->deref, *add;
1882 struct ident *ident = expr->member;
1883 unsigned int mod;
1884 int address_space;
1886 if (!evaluate_expression(deref))
1887 return NULL;
1888 if (!ident) {
1889 expression_error(expr, "bad member name");
1890 return NULL;
1893 ctype = deref->ctype;
1894 examine_symbol_type(ctype);
1895 address_space = ctype->ctype.as;
1896 mod = ctype->ctype.modifiers;
1897 if (ctype->type == SYM_NODE) {
1898 ctype = ctype->ctype.base_type;
1899 address_space |= ctype->ctype.as;
1900 mod |= ctype->ctype.modifiers;
1902 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1903 expression_error(expr, "expected structure or union");
1904 return NULL;
1906 offset = 0;
1907 member = find_identifier(ident, ctype->symbol_list, &offset);
1908 if (!member) {
1909 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1910 const char *name = "<unnamed>";
1911 int namelen = 9;
1912 if (ctype->ident) {
1913 name = ctype->ident->name;
1914 namelen = ctype->ident->len;
1916 if (ctype->symbol_list)
1917 expression_error(expr, "no member '%s' in %s %.*s",
1918 show_ident(ident), type, namelen, name);
1919 else
1920 expression_error(expr, "using member '%s' in "
1921 "incomplete %s %.*s", show_ident(ident),
1922 type, namelen, name);
1923 return NULL;
1927 * The member needs to take on the address space and modifiers of
1928 * the "parent" type.
1930 member = convert_to_as_mod(member, address_space, mod);
1931 ctype = get_base_type(member);
1933 if (!lvalue_expression(deref)) {
1934 if (deref->type != EXPR_SLICE) {
1935 expr->base = deref;
1936 expr->r_bitpos = 0;
1937 } else {
1938 expr->base = deref->base;
1939 expr->r_bitpos = deref->r_bitpos;
1941 expr->r_bitpos += offset << 3;
1942 expr->type = EXPR_SLICE;
1943 expr->r_nrbits = member->bit_size;
1944 expr->r_bitpos += member->bit_offset;
1945 expr->ctype = member;
1946 return member;
1949 deref = deref->unop;
1950 expr->deref = deref;
1952 add = evaluate_offset(deref, offset);
1953 expr->type = EXPR_PREOP;
1954 expr->op = '*';
1955 expr->unop = add;
1957 expr->ctype = member;
1958 return member;
1961 static int is_promoted(struct expression *expr)
1963 while (1) {
1964 switch (expr->type) {
1965 case EXPR_BINOP:
1966 case EXPR_SELECT:
1967 case EXPR_CONDITIONAL:
1968 return 1;
1969 case EXPR_COMMA:
1970 expr = expr->right;
1971 continue;
1972 case EXPR_PREOP:
1973 switch (expr->op) {
1974 case '(':
1975 expr = expr->unop;
1976 continue;
1977 case '+':
1978 case '-':
1979 case '~':
1980 return 1;
1981 default:
1982 return 0;
1984 default:
1985 return 0;
1991 static struct symbol *evaluate_cast(struct expression *);
1993 static struct symbol *evaluate_type_information(struct expression *expr)
1995 struct symbol *sym = expr->cast_type;
1996 if (!sym) {
1997 sym = evaluate_expression(expr->cast_expression);
1998 if (!sym)
1999 return NULL;
2001 * Expressions of restricted types will possibly get
2002 * promoted - check that here
2004 if (is_restricted_type(sym)) {
2005 if (sym->bit_size < bits_in_int && is_promoted(expr))
2006 sym = &int_ctype;
2007 } else if (is_fouled_type(sym)) {
2008 sym = &int_ctype;
2011 examine_symbol_type(sym);
2012 if (is_bitfield_type(sym)) {
2013 expression_error(expr, "trying to examine bitfield type");
2014 return NULL;
2016 return sym;
2019 static struct symbol *evaluate_sizeof(struct expression *expr)
2021 struct symbol *type;
2022 int size;
2024 type = evaluate_type_information(expr);
2025 if (!type)
2026 return NULL;
2028 size = type->bit_size;
2029 if ((size < 0) || (size & 7))
2030 expression_error(expr, "cannot size expression");
2031 expr->type = EXPR_VALUE;
2032 expr->value = size >> 3;
2033 expr->taint = 0;
2034 expr->ctype = size_t_ctype;
2035 return size_t_ctype;
2038 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
2040 struct symbol *type;
2041 int size;
2043 type = evaluate_type_information(expr);
2044 if (!type)
2045 return NULL;
2047 if (type->type == SYM_NODE)
2048 type = type->ctype.base_type;
2049 if (!type)
2050 return NULL;
2051 switch (type->type) {
2052 case SYM_ARRAY:
2053 break;
2054 case SYM_PTR:
2055 type = get_base_type(type);
2056 if (type)
2057 break;
2058 default:
2059 expression_error(expr, "expected pointer expression");
2060 return NULL;
2062 size = type->bit_size;
2063 if (size & 7)
2064 size = 0;
2065 expr->type = EXPR_VALUE;
2066 expr->value = size >> 3;
2067 expr->taint = 0;
2068 expr->ctype = size_t_ctype;
2069 return size_t_ctype;
2072 static struct symbol *evaluate_alignof(struct expression *expr)
2074 struct symbol *type;
2076 type = evaluate_type_information(expr);
2077 if (!type)
2078 return NULL;
2080 expr->type = EXPR_VALUE;
2081 expr->value = type->ctype.alignment;
2082 expr->taint = 0;
2083 expr->ctype = size_t_ctype;
2084 return size_t_ctype;
2087 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
2089 struct expression *expr;
2090 struct symbol_list *argument_types = fn->arguments;
2091 struct symbol *argtype;
2092 int i = 1;
2094 PREPARE_PTR_LIST(argument_types, argtype);
2095 FOR_EACH_PTR (head, expr) {
2096 struct expression **p = THIS_ADDRESS(expr);
2097 struct symbol *ctype, *target;
2098 ctype = evaluate_expression(expr);
2100 if (!ctype)
2101 return 0;
2103 target = argtype;
2104 if (!target) {
2105 struct symbol *type;
2106 int class = classify_type(ctype, &type);
2107 if (is_int(class)) {
2108 *p = cast_to(expr, integer_promotion(type));
2109 } else if (class & TYPE_FLOAT) {
2110 unsigned long mod = type->ctype.modifiers;
2111 if (!(mod & (MOD_LONG|MOD_LONGLONG)))
2112 *p = cast_to(expr, &double_ctype);
2113 } else if (class & TYPE_PTR) {
2114 if (expr->ctype == &null_ctype)
2115 *p = cast_to(expr, &ptr_ctype);
2116 else
2117 degenerate(expr);
2119 } else {
2120 static char where[30];
2121 examine_symbol_type(target);
2122 sprintf(where, "argument %d", i);
2123 compatible_assignment_types(expr, target, p, where);
2126 i++;
2127 NEXT_PTR_LIST(argtype);
2128 } END_FOR_EACH_PTR(expr);
2129 FINISH_PTR_LIST(argtype);
2130 return 1;
2133 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
2135 struct symbol *sym;
2137 FOR_EACH_PTR(ctype->symbol_list, sym) {
2138 if (sym->ident == ident)
2139 return sym;
2140 } END_FOR_EACH_PTR(sym);
2141 return NULL;
2144 static void convert_index(struct expression *e)
2146 struct expression *child = e->idx_expression;
2147 unsigned from = e->idx_from;
2148 unsigned to = e->idx_to + 1;
2149 e->type = EXPR_POS;
2150 e->init_offset = from * (e->ctype->bit_size>>3);
2151 e->init_nr = to - from;
2152 e->init_expr = child;
2155 static void convert_ident(struct expression *e)
2157 struct expression *child = e->ident_expression;
2158 struct symbol *sym = e->field;
2159 e->type = EXPR_POS;
2160 e->init_offset = sym->offset;
2161 e->init_nr = 1;
2162 e->init_expr = child;
2165 static void convert_designators(struct expression *e)
2167 while (e) {
2168 if (e->type == EXPR_INDEX)
2169 convert_index(e);
2170 else if (e->type == EXPR_IDENTIFIER)
2171 convert_ident(e);
2172 else
2173 break;
2174 e = e->init_expr;
2178 static void excess(struct expression *e, const char *s)
2180 warning(e->pos, "excessive elements in %s initializer", s);
2184 * implicit designator for the first element
2186 static struct expression *first_subobject(struct symbol *ctype, int class,
2187 struct expression **v)
2189 struct expression *e = *v, *new;
2191 if (ctype->type == SYM_NODE)
2192 ctype = ctype->ctype.base_type;
2194 if (class & TYPE_PTR) { /* array */
2195 if (!ctype->bit_size)
2196 return NULL;
2197 new = alloc_expression(e->pos, EXPR_INDEX);
2198 new->idx_expression = e;
2199 new->ctype = ctype->ctype.base_type;
2200 } else {
2201 struct symbol *field, *p;
2202 PREPARE_PTR_LIST(ctype->symbol_list, p);
2203 while (p && !p->ident && is_bitfield_type(p))
2204 NEXT_PTR_LIST(p);
2205 field = p;
2206 FINISH_PTR_LIST(p);
2207 if (!field)
2208 return NULL;
2209 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2210 new->ident_expression = e;
2211 new->field = new->ctype = field;
2213 *v = new;
2214 return new;
2218 * sanity-check explicit designators; return the innermost one or NULL
2219 * in case of error. Assign types.
2221 static struct expression *check_designators(struct expression *e,
2222 struct symbol *ctype)
2224 struct expression *last = NULL;
2225 const char *err;
2226 while (1) {
2227 if (ctype->type == SYM_NODE)
2228 ctype = ctype->ctype.base_type;
2229 if (e->type == EXPR_INDEX) {
2230 struct symbol *type;
2231 if (ctype->type != SYM_ARRAY) {
2232 err = "array index in non-array";
2233 break;
2235 type = ctype->ctype.base_type;
2236 if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2237 unsigned offset = e->idx_to * type->bit_size;
2238 if (offset >= ctype->bit_size) {
2239 err = "index out of bounds in";
2240 break;
2243 e->ctype = ctype = type;
2244 ctype = type;
2245 last = e;
2246 if (!e->idx_expression) {
2247 err = "invalid";
2248 break;
2250 e = e->idx_expression;
2251 } else if (e->type == EXPR_IDENTIFIER) {
2252 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2253 err = "field name not in struct or union";
2254 break;
2256 ctype = find_struct_ident(ctype, e->expr_ident);
2257 if (!ctype) {
2258 err = "unknown field name in";
2259 break;
2261 e->field = e->ctype = ctype;
2262 last = e;
2263 if (!e->ident_expression) {
2264 err = "invalid";
2265 break;
2267 e = e->ident_expression;
2268 } else if (e->type == EXPR_POS) {
2269 err = "internal front-end error: EXPR_POS in";
2270 break;
2271 } else
2272 return last;
2274 expression_error(e, "%s initializer", err);
2275 return NULL;
2279 * choose the next subobject to initialize.
2281 * Get designators for next element, switch old ones to EXPR_POS.
2282 * Return the resulting expression or NULL if we'd run out of subobjects.
2283 * The innermost designator is returned in *v. Designators in old
2284 * are assumed to be already sanity-checked.
2286 static struct expression *next_designators(struct expression *old,
2287 struct symbol *ctype,
2288 struct expression *e, struct expression **v)
2290 struct expression *new = NULL;
2292 if (!old)
2293 return NULL;
2294 if (old->type == EXPR_INDEX) {
2295 struct expression *copy;
2296 unsigned n;
2298 copy = next_designators(old->idx_expression,
2299 old->ctype, e, v);
2300 if (!copy) {
2301 n = old->idx_to + 1;
2302 if (n * old->ctype->bit_size == ctype->bit_size) {
2303 convert_index(old);
2304 return NULL;
2306 copy = e;
2307 *v = new = alloc_expression(e->pos, EXPR_INDEX);
2308 } else {
2309 n = old->idx_to;
2310 new = alloc_expression(e->pos, EXPR_INDEX);
2313 new->idx_from = new->idx_to = n;
2314 new->idx_expression = copy;
2315 new->ctype = old->ctype;
2316 convert_index(old);
2317 } else if (old->type == EXPR_IDENTIFIER) {
2318 struct expression *copy;
2319 struct symbol *field;
2321 copy = next_designators(old->ident_expression,
2322 old->ctype, e, v);
2323 if (!copy) {
2324 field = old->field->next_subobject;
2325 if (!field) {
2326 convert_ident(old);
2327 return NULL;
2329 copy = e;
2330 *v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2331 } else {
2332 field = old->field;
2333 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2336 new->field = field;
2337 new->expr_ident = field->ident;
2338 new->ident_expression = copy;
2339 new->ctype = field;
2340 convert_ident(old);
2342 return new;
2345 static int handle_simple_initializer(struct expression **ep, int nested,
2346 int class, struct symbol *ctype);
2349 * deal with traversing subobjects [6.7.8(17,18,20)]
2351 static void handle_list_initializer(struct expression *expr,
2352 int class, struct symbol *ctype)
2354 struct expression *e, *last = NULL, *top = NULL, *next;
2355 int jumped = 0;
2357 FOR_EACH_PTR(expr->expr_list, e) {
2358 struct expression **v;
2359 struct symbol *type;
2360 int lclass;
2362 if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2363 if (!top) {
2364 top = e;
2365 last = first_subobject(ctype, class, &top);
2366 } else {
2367 last = next_designators(last, ctype, e, &top);
2369 if (!last) {
2370 excess(e, class & TYPE_PTR ? "array" :
2371 "struct or union");
2372 DELETE_CURRENT_PTR(e);
2373 continue;
2375 if (jumped) {
2376 warning(e->pos, "advancing past deep designator");
2377 jumped = 0;
2379 REPLACE_CURRENT_PTR(e, last);
2380 } else {
2381 next = check_designators(e, ctype);
2382 if (!next) {
2383 DELETE_CURRENT_PTR(e);
2384 continue;
2386 top = next;
2387 /* deeper than one designator? */
2388 jumped = top != e;
2389 convert_designators(last);
2390 last = e;
2393 found:
2394 lclass = classify_type(top->ctype, &type);
2395 if (top->type == EXPR_INDEX)
2396 v = &top->idx_expression;
2397 else
2398 v = &top->ident_expression;
2400 if (handle_simple_initializer(v, 1, lclass, top->ctype))
2401 continue;
2403 if (!(lclass & TYPE_COMPOUND)) {
2404 warning(e->pos, "bogus scalar initializer");
2405 DELETE_CURRENT_PTR(e);
2406 continue;
2409 next = first_subobject(type, lclass, v);
2410 if (next) {
2411 warning(e->pos, "missing braces around initializer");
2412 top = next;
2413 goto found;
2416 DELETE_CURRENT_PTR(e);
2417 excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2419 } END_FOR_EACH_PTR(e);
2421 convert_designators(last);
2422 expr->ctype = ctype;
2425 static int is_string_literal(struct expression **v)
2427 struct expression *e = *v;
2428 while (e && e->type == EXPR_PREOP && e->op == '(')
2429 e = e->unop;
2430 if (!e || e->type != EXPR_STRING)
2431 return 0;
2432 if (e != *v && Wparen_string)
2433 warning(e->pos,
2434 "array initialized from parenthesized string constant");
2435 *v = e;
2436 return 1;
2440 * We want a normal expression, possibly in one layer of braces. Warn
2441 * if the latter happens inside a list (it's legal, but likely to be
2442 * an effect of screwup). In case of anything not legal, we are definitely
2443 * having an effect of screwup, so just fail and let the caller warn.
2445 static struct expression *handle_scalar(struct expression *e, int nested)
2447 struct expression *v = NULL, *p;
2448 int count = 0;
2450 /* normal case */
2451 if (e->type != EXPR_INITIALIZER)
2452 return e;
2454 FOR_EACH_PTR(e->expr_list, p) {
2455 if (!v)
2456 v = p;
2457 count++;
2458 } END_FOR_EACH_PTR(p);
2459 if (count != 1)
2460 return NULL;
2461 switch(v->type) {
2462 case EXPR_INITIALIZER:
2463 case EXPR_INDEX:
2464 case EXPR_IDENTIFIER:
2465 return NULL;
2466 default:
2467 break;
2469 if (nested)
2470 warning(e->pos, "braces around scalar initializer");
2471 return v;
2475 * deal with the cases that don't care about subobjects:
2476 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2477 * character array <- string literal, possibly in braces [6.7.8(14)]
2478 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2479 * compound type <- initializer list in braces [6.7.8(16)]
2480 * The last one punts to handle_list_initializer() which, in turn will call
2481 * us for individual elements of the list.
2483 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2484 * the lack of support of wide char stuff in general.
2486 * One note: we need to take care not to evaluate a string literal until
2487 * we know that we *will* handle it right here. Otherwise we would screw
2488 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2489 * { "string", ...} - we need to preserve that string literal recognizable
2490 * until we dig into the inner struct.
2492 static int handle_simple_initializer(struct expression **ep, int nested,
2493 int class, struct symbol *ctype)
2495 int is_string = is_string_type(ctype);
2496 struct expression *e = *ep, *p;
2497 struct symbol *type;
2499 if (!e)
2500 return 0;
2502 /* scalar */
2503 if (!(class & TYPE_COMPOUND)) {
2504 e = handle_scalar(e, nested);
2505 if (!e)
2506 return 0;
2507 *ep = e;
2508 if (!evaluate_expression(e))
2509 return 1;
2510 compatible_assignment_types(e, ctype, ep, "initializer");
2511 return 1;
2515 * sublist; either a string, or we dig in; the latter will deal with
2516 * pathologies, so we don't need anything fancy here.
2518 if (e->type == EXPR_INITIALIZER) {
2519 if (is_string) {
2520 struct expression *v = NULL;
2521 int count = 0;
2523 FOR_EACH_PTR(e->expr_list, p) {
2524 if (!v)
2525 v = p;
2526 count++;
2527 } END_FOR_EACH_PTR(p);
2528 if (count == 1 && is_string_literal(&v)) {
2529 *ep = e = v;
2530 goto String;
2533 handle_list_initializer(e, class, ctype);
2534 return 1;
2537 /* string */
2538 if (is_string_literal(&e)) {
2539 /* either we are doing array of char, or we'll have to dig in */
2540 if (is_string) {
2541 *ep = e;
2542 goto String;
2544 return 0;
2546 /* struct or union can be initialized by compatible */
2547 if (class != TYPE_COMPOUND)
2548 return 0;
2549 type = evaluate_expression(e);
2550 if (!type)
2551 return 0;
2552 if (ctype->type == SYM_NODE)
2553 ctype = ctype->ctype.base_type;
2554 if (type->type == SYM_NODE)
2555 type = type->ctype.base_type;
2556 if (ctype == type)
2557 return 1;
2558 return 0;
2560 String:
2561 p = alloc_expression(e->pos, EXPR_STRING);
2562 *p = *e;
2563 type = evaluate_expression(p);
2564 if (ctype->bit_size != -1 &&
2565 ctype->bit_size + bits_in_char < type->bit_size) {
2566 warning(e->pos,
2567 "too long initializer-string for array of char");
2569 *ep = p;
2570 return 1;
2573 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2575 struct symbol *type;
2576 int class = classify_type(ctype, &type);
2577 if (!handle_simple_initializer(ep, 0, class, ctype))
2578 expression_error(*ep, "invalid initializer");
2581 static struct symbol *evaluate_cast(struct expression *expr)
2583 struct expression *target = expr->cast_expression;
2584 struct symbol *ctype;
2585 struct symbol *t1, *t2;
2586 int class1, class2;
2587 int as1 = 0, as2 = 0;
2589 if (!target)
2590 return NULL;
2593 * Special case: a cast can be followed by an
2594 * initializer, in which case we need to pass
2595 * the type value down to that initializer rather
2596 * than trying to evaluate it as an expression
2598 * A more complex case is when the initializer is
2599 * dereferenced as part of a post-fix expression.
2600 * We need to produce an expression that can be dereferenced.
2602 if (target->type == EXPR_INITIALIZER) {
2603 struct symbol *sym = expr->cast_type;
2604 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2606 sym->initializer = target;
2607 evaluate_symbol(sym);
2609 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2610 addr->symbol = sym;
2612 expr->type = EXPR_PREOP;
2613 expr->op = '*';
2614 expr->unop = addr;
2615 expr->ctype = sym;
2617 return sym;
2620 ctype = examine_symbol_type(expr->cast_type);
2621 expr->ctype = ctype;
2622 expr->cast_type = ctype;
2624 evaluate_expression(target);
2625 degenerate(target);
2627 class1 = classify_type(ctype, &t1);
2629 /* cast to non-integer type -> not an integer constant expression */
2630 if (!is_int(class1))
2631 expr->flags = 0;
2632 /* if argument turns out to be not an integer constant expression *and*
2633 it was not a floating literal to start with -> too bad */
2634 else if (expr->flags == Int_const_expr &&
2635 !(target->flags & Int_const_expr))
2636 expr->flags = 0;
2638 * You can always throw a value away by casting to
2639 * "void" - that's an implicit "force". Note that
2640 * the same is _not_ true of "void *".
2642 if (t1 == &void_ctype)
2643 goto out;
2645 if (class1 & (TYPE_COMPOUND | TYPE_FN))
2646 warning(expr->pos, "cast to non-scalar");
2648 t2 = target->ctype;
2649 if (!t2) {
2650 expression_error(expr, "cast from unknown type");
2651 goto out;
2653 class2 = classify_type(t2, &t2);
2655 if (class2 & TYPE_COMPOUND)
2656 warning(expr->pos, "cast from non-scalar");
2658 if (expr->type == EXPR_FORCE_CAST)
2659 goto out;
2661 /* allowed cast unfouls */
2662 if (class2 & TYPE_FOULED)
2663 t2 = unfoul(t2);
2665 if (t1 != t2) {
2666 if (class1 & TYPE_RESTRICT)
2667 warning(expr->pos, "cast to restricted type");
2668 if (class2 & TYPE_RESTRICT)
2669 warning(expr->pos, "cast from restricted type");
2672 if (t1 == &ulong_ctype)
2673 as1 = -1;
2674 else if (class1 == TYPE_PTR) {
2675 examine_pointer_target(t1);
2676 as1 = t1->ctype.as;
2679 if (t2 == &ulong_ctype)
2680 as2 = -1;
2681 else if (class2 == TYPE_PTR) {
2682 examine_pointer_target(t2);
2683 as2 = t2->ctype.as;
2686 if (!as1 && as2 > 0)
2687 warning(expr->pos, "cast removes address space of expression");
2688 if (as1 > 0 && as2 > 0 && as1 != as2)
2689 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2690 if (as1 > 0 && !as2 &&
2691 !is_null_pointer_constant(target) && Wcast_to_address_space)
2692 warning(expr->pos,
2693 "cast adds address space to expression (<asn:%d>)", as1);
2695 if (!(t1->ctype.modifiers & MOD_PTRINHERIT) && class1 == TYPE_PTR &&
2696 !as1 && (target->flags & Int_const_expr)) {
2697 if (t1->ctype.base_type == &void_ctype) {
2698 if (is_zero_constant(target)) {
2699 /* NULL */
2700 expr->type = EXPR_VALUE;
2701 expr->ctype = &null_ctype;
2702 expr->value = 0;
2703 return ctype;
2707 out:
2708 return ctype;
2712 * Evaluate a call expression with a symbol. This
2713 * should expand inline functions, and evaluate
2714 * builtins.
2716 static int evaluate_symbol_call(struct expression *expr)
2718 struct expression *fn = expr->fn;
2719 struct symbol *ctype = fn->ctype;
2721 if (fn->type != EXPR_PREOP)
2722 return 0;
2724 if (ctype->op && ctype->op->evaluate)
2725 return ctype->op->evaluate(expr);
2727 if (ctype->ctype.modifiers & MOD_INLINE) {
2728 int ret;
2729 struct symbol *curr = current_fn;
2730 current_fn = ctype->ctype.base_type;
2732 ret = inline_function(expr, ctype);
2734 /* restore the old function */
2735 current_fn = curr;
2736 return ret;
2739 return 0;
2742 static struct symbol *evaluate_call(struct expression *expr)
2744 int args, fnargs;
2745 struct symbol *ctype, *sym;
2746 struct expression *fn = expr->fn;
2747 struct expression_list *arglist = expr->args;
2749 if (!evaluate_expression(fn))
2750 return NULL;
2751 sym = ctype = fn->ctype;
2752 if (ctype->type == SYM_NODE)
2753 ctype = ctype->ctype.base_type;
2754 if (ctype->type == SYM_PTR)
2755 ctype = get_base_type(ctype);
2757 if (ctype->type != SYM_FN) {
2758 struct expression *arg;
2759 expression_error(expr, "not a function %s",
2760 show_ident(sym->ident));
2761 /* do typechecking in arguments */
2762 FOR_EACH_PTR (arglist, arg) {
2763 evaluate_expression(arg);
2764 } END_FOR_EACH_PTR(arg);
2765 return NULL;
2768 examine_fn_arguments(ctype);
2769 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
2770 sym->op && sym->op->args) {
2771 if (!sym->op->args(expr))
2772 return NULL;
2773 } else {
2774 if (!evaluate_arguments(sym, ctype, arglist))
2775 return NULL;
2776 args = expression_list_size(expr->args);
2777 fnargs = symbol_list_size(ctype->arguments);
2778 if (args < fnargs)
2779 expression_error(expr,
2780 "not enough arguments for function %s",
2781 show_ident(sym->ident));
2782 if (args > fnargs && !ctype->variadic)
2783 expression_error(expr,
2784 "too many arguments for function %s",
2785 show_ident(sym->ident));
2787 if (sym->type == SYM_NODE) {
2788 if (evaluate_symbol_call(expr))
2789 return expr->ctype;
2791 expr->ctype = ctype->ctype.base_type;
2792 return expr->ctype;
2795 static struct symbol *evaluate_offsetof(struct expression *expr)
2797 struct expression *e = expr->down;
2798 struct symbol *ctype = expr->in;
2799 int class;
2801 if (expr->op == '.') {
2802 struct symbol *field;
2803 int offset = 0;
2804 if (!ctype) {
2805 expression_error(expr, "expected structure or union");
2806 return NULL;
2808 examine_symbol_type(ctype);
2809 class = classify_type(ctype, &ctype);
2810 if (class != TYPE_COMPOUND) {
2811 expression_error(expr, "expected structure or union");
2812 return NULL;
2815 field = find_identifier(expr->ident, ctype->symbol_list, &offset);
2816 if (!field) {
2817 expression_error(expr, "unknown member");
2818 return NULL;
2820 ctype = field;
2821 expr->type = EXPR_VALUE;
2822 expr->flags = Int_const_expr;
2823 expr->value = offset;
2824 expr->taint = 0;
2825 expr->ctype = size_t_ctype;
2826 } else {
2827 if (!ctype) {
2828 expression_error(expr, "expected structure or union");
2829 return NULL;
2831 examine_symbol_type(ctype);
2832 class = classify_type(ctype, &ctype);
2833 if (class != (TYPE_COMPOUND | TYPE_PTR)) {
2834 expression_error(expr, "expected array");
2835 return NULL;
2837 ctype = ctype->ctype.base_type;
2838 if (!expr->index) {
2839 expr->type = EXPR_VALUE;
2840 expr->flags = Int_const_expr;
2841 expr->value = 0;
2842 expr->taint = 0;
2843 expr->ctype = size_t_ctype;
2844 } else {
2845 struct expression *idx = expr->index, *m;
2846 struct symbol *i_type = evaluate_expression(idx);
2847 int i_class = classify_type(i_type, &i_type);
2848 if (!is_int(i_class)) {
2849 expression_error(expr, "non-integer index");
2850 return NULL;
2852 unrestrict(idx, i_class, &i_type);
2853 idx = cast_to(idx, size_t_ctype);
2854 m = alloc_const_expression(expr->pos,
2855 ctype->bit_size >> 3);
2856 m->ctype = size_t_ctype;
2857 m->flags = Int_const_expr;
2858 expr->type = EXPR_BINOP;
2859 expr->left = idx;
2860 expr->right = m;
2861 expr->op = '*';
2862 expr->ctype = size_t_ctype;
2863 expr->flags = m->flags & idx->flags & Int_const_expr;
2866 if (e) {
2867 struct expression *copy = __alloc_expression(0);
2868 *copy = *expr;
2869 if (e->type == EXPR_OFFSETOF)
2870 e->in = ctype;
2871 if (!evaluate_expression(e))
2872 return NULL;
2873 expr->type = EXPR_BINOP;
2874 expr->flags = e->flags & copy->flags & Int_const_expr;
2875 expr->op = '+';
2876 expr->ctype = size_t_ctype;
2877 expr->left = copy;
2878 expr->right = e;
2880 return size_t_ctype;
2883 struct symbol *evaluate_expression(struct expression *expr)
2885 if (!expr)
2886 return NULL;
2887 if (expr->ctype)
2888 return expr->ctype;
2890 switch (expr->type) {
2891 case EXPR_VALUE:
2892 case EXPR_FVALUE:
2893 expression_error(expr, "value expression without a type");
2894 return NULL;
2895 case EXPR_STRING:
2896 return evaluate_string(expr);
2897 case EXPR_SYMBOL:
2898 return evaluate_symbol_expression(expr);
2899 case EXPR_BINOP:
2900 if (!evaluate_expression(expr->left))
2901 return NULL;
2902 if (!evaluate_expression(expr->right))
2903 return NULL;
2904 return evaluate_binop(expr);
2905 case EXPR_LOGICAL:
2906 return evaluate_logical(expr);
2907 case EXPR_COMMA:
2908 evaluate_expression(expr->left);
2909 if (!evaluate_expression(expr->right))
2910 return NULL;
2911 return evaluate_comma(expr);
2912 case EXPR_COMPARE:
2913 if (!evaluate_expression(expr->left))
2914 return NULL;
2915 if (!evaluate_expression(expr->right))
2916 return NULL;
2917 return evaluate_compare(expr);
2918 case EXPR_ASSIGNMENT:
2919 if (!evaluate_expression(expr->left))
2920 return NULL;
2921 if (!evaluate_expression(expr->right))
2922 return NULL;
2923 return evaluate_assignment(expr);
2924 case EXPR_PREOP:
2925 if (!evaluate_expression(expr->unop))
2926 return NULL;
2927 return evaluate_preop(expr);
2928 case EXPR_POSTOP:
2929 if (!evaluate_expression(expr->unop))
2930 return NULL;
2931 return evaluate_postop(expr);
2932 case EXPR_CAST:
2933 case EXPR_FORCE_CAST:
2934 case EXPR_IMPLIED_CAST:
2935 return evaluate_cast(expr);
2936 case EXPR_SIZEOF:
2937 return evaluate_sizeof(expr);
2938 case EXPR_PTRSIZEOF:
2939 return evaluate_ptrsizeof(expr);
2940 case EXPR_ALIGNOF:
2941 return evaluate_alignof(expr);
2942 case EXPR_DEREF:
2943 return evaluate_member_dereference(expr);
2944 case EXPR_CALL:
2945 return evaluate_call(expr);
2946 case EXPR_SELECT:
2947 case EXPR_CONDITIONAL:
2948 return evaluate_conditional_expression(expr);
2949 case EXPR_STATEMENT:
2950 expr->ctype = evaluate_statement(expr->statement);
2951 return expr->ctype;
2953 case EXPR_LABEL:
2954 expr->ctype = &ptr_ctype;
2955 return &ptr_ctype;
2957 case EXPR_TYPE:
2958 /* Evaluate the type of the symbol .. */
2959 evaluate_symbol(expr->symbol);
2960 /* .. but the type of the _expression_ is a "type" */
2961 expr->ctype = &type_ctype;
2962 return &type_ctype;
2964 case EXPR_OFFSETOF:
2965 return evaluate_offsetof(expr);
2967 /* These can not exist as stand-alone expressions */
2968 case EXPR_INITIALIZER:
2969 case EXPR_IDENTIFIER:
2970 case EXPR_INDEX:
2971 case EXPR_POS:
2972 expression_error(expr, "internal front-end error: initializer in expression");
2973 return NULL;
2974 case EXPR_SLICE:
2975 expression_error(expr, "internal front-end error: SLICE re-evaluated");
2976 return NULL;
2978 return NULL;
2981 static void check_duplicates(struct symbol *sym)
2983 int declared = 0;
2984 struct symbol *next = sym;
2986 while ((next = next->same_symbol) != NULL) {
2987 const char *typediff;
2988 evaluate_symbol(next);
2989 declared++;
2990 typediff = type_difference(&sym->ctype, &next->ctype, 0, 0);
2991 if (typediff) {
2992 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2993 show_ident(sym->ident),
2994 stream_name(next->pos.stream), next->pos.line, typediff);
2995 return;
2998 if (!declared) {
2999 unsigned long mod = sym->ctype.modifiers;
3000 if (mod & (MOD_STATIC | MOD_REGISTER))
3001 return;
3002 if (!(mod & MOD_TOPLEVEL))
3003 return;
3004 if (!Wdecl)
3005 return;
3006 if (sym->ident == &main_ident)
3007 return;
3008 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
3012 static struct symbol *evaluate_symbol(struct symbol *sym)
3014 struct symbol *base_type;
3016 if (!sym)
3017 return sym;
3018 if (sym->evaluated)
3019 return sym;
3020 sym->evaluated = 1;
3022 sym = examine_symbol_type(sym);
3023 base_type = get_base_type(sym);
3024 if (!base_type)
3025 return NULL;
3027 /* Evaluate the initializers */
3028 if (sym->initializer)
3029 evaluate_initializer(sym, &sym->initializer);
3031 /* And finally, evaluate the body of the symbol too */
3032 if (base_type->type == SYM_FN) {
3033 struct symbol *curr = current_fn;
3035 current_fn = base_type;
3037 examine_fn_arguments(base_type);
3038 if (!base_type->stmt && base_type->inline_stmt)
3039 uninline(sym);
3040 if (base_type->stmt)
3041 evaluate_statement(base_type->stmt);
3043 current_fn = curr;
3046 return base_type;
3049 void evaluate_symbol_list(struct symbol_list *list)
3051 struct symbol *sym;
3053 FOR_EACH_PTR(list, sym) {
3054 evaluate_symbol(sym);
3055 check_duplicates(sym);
3056 } END_FOR_EACH_PTR(sym);
3059 static struct symbol *evaluate_return_expression(struct statement *stmt)
3061 struct expression *expr = stmt->expression;
3062 struct symbol *fntype;
3064 evaluate_expression(expr);
3065 fntype = current_fn->ctype.base_type;
3066 if (!fntype || fntype == &void_ctype) {
3067 if (expr && expr->ctype != &void_ctype)
3068 expression_error(expr, "return expression in %s function", fntype?"void":"typeless");
3069 if (expr && Wreturn_void)
3070 warning(stmt->pos, "returning void-valued expression");
3071 return NULL;
3074 if (!expr) {
3075 sparse_error(stmt->pos, "return with no return value");
3076 return NULL;
3078 if (!expr->ctype)
3079 return NULL;
3080 compatible_assignment_types(expr, fntype, &stmt->expression, "return expression");
3081 return NULL;
3084 static void evaluate_if_statement(struct statement *stmt)
3086 if (!stmt->if_conditional)
3087 return;
3089 evaluate_conditional(stmt->if_conditional, 0);
3090 evaluate_statement(stmt->if_true);
3091 evaluate_statement(stmt->if_false);
3094 static void evaluate_iterator(struct statement *stmt)
3096 evaluate_conditional(stmt->iterator_pre_condition, 1);
3097 evaluate_conditional(stmt->iterator_post_condition,1);
3098 evaluate_statement(stmt->iterator_pre_statement);
3099 evaluate_statement(stmt->iterator_statement);
3100 evaluate_statement(stmt->iterator_post_statement);
3103 static void verify_output_constraint(struct expression *expr, const char *constraint)
3105 switch (*constraint) {
3106 case '=': /* Assignment */
3107 case '+': /* Update */
3108 break;
3109 default:
3110 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
3114 static void verify_input_constraint(struct expression *expr, const char *constraint)
3116 switch (*constraint) {
3117 case '=': /* Assignment */
3118 case '+': /* Update */
3119 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
3123 static void evaluate_asm_statement(struct statement *stmt)
3125 struct expression *expr;
3126 int state;
3128 expr = stmt->asm_string;
3129 if (!expr || expr->type != EXPR_STRING) {
3130 sparse_error(stmt->pos, "need constant string for inline asm");
3131 return;
3134 state = 0;
3135 FOR_EACH_PTR(stmt->asm_outputs, expr) {
3136 struct ident *ident;
3138 switch (state) {
3139 case 0: /* Identifier */
3140 state = 1;
3141 ident = (struct ident *)expr;
3142 continue;
3144 case 1: /* Constraint */
3145 state = 2;
3146 if (!expr || expr->type != EXPR_STRING) {
3147 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
3148 *THIS_ADDRESS(expr) = NULL;
3149 continue;
3151 verify_output_constraint(expr, expr->string->data);
3152 continue;
3154 case 2: /* Expression */
3155 state = 0;
3156 if (!evaluate_expression(expr))
3157 return;
3158 if (!lvalue_expression(expr))
3159 warning(expr->pos, "asm output is not an lvalue");
3160 evaluate_assign_to(expr, expr->ctype);
3161 continue;
3163 } END_FOR_EACH_PTR(expr);
3165 state = 0;
3166 FOR_EACH_PTR(stmt->asm_inputs, expr) {
3167 struct ident *ident;
3169 switch (state) {
3170 case 0: /* Identifier */
3171 state = 1;
3172 ident = (struct ident *)expr;
3173 continue;
3175 case 1: /* Constraint */
3176 state = 2;
3177 if (!expr || expr->type != EXPR_STRING) {
3178 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
3179 *THIS_ADDRESS(expr) = NULL;
3180 continue;
3182 verify_input_constraint(expr, expr->string->data);
3183 continue;
3185 case 2: /* Expression */
3186 state = 0;
3187 if (!evaluate_expression(expr))
3188 return;
3189 continue;
3191 } END_FOR_EACH_PTR(expr);
3193 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
3194 if (!expr) {
3195 sparse_error(stmt->pos, "bad asm output");
3196 return;
3198 if (expr->type == EXPR_STRING)
3199 continue;
3200 expression_error(expr, "asm clobber is not a string");
3201 } END_FOR_EACH_PTR(expr);
3204 static void evaluate_case_statement(struct statement *stmt)
3206 evaluate_expression(stmt->case_expression);
3207 evaluate_expression(stmt->case_to);
3208 evaluate_statement(stmt->case_statement);
3211 static void check_case_type(struct expression *switch_expr,
3212 struct expression *case_expr,
3213 struct expression **enumcase)
3215 struct symbol *switch_type, *case_type;
3216 int sclass, cclass;
3218 if (!case_expr)
3219 return;
3221 switch_type = switch_expr->ctype;
3222 case_type = evaluate_expression(case_expr);
3224 if (!switch_type || !case_type)
3225 goto Bad;
3226 if (enumcase) {
3227 if (*enumcase)
3228 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
3229 else if (is_enum_type(case_type))
3230 *enumcase = case_expr;
3233 sclass = classify_type(switch_type, &switch_type);
3234 cclass = classify_type(case_type, &case_type);
3236 /* both should be arithmetic */
3237 if (!(sclass & cclass & TYPE_NUM))
3238 goto Bad;
3240 /* neither should be floating */
3241 if ((sclass | cclass) & TYPE_FLOAT)
3242 goto Bad;
3244 /* if neither is restricted, we are OK */
3245 if (!((sclass | cclass) & TYPE_RESTRICT))
3246 return;
3248 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
3249 cclass, sclass, case_type, switch_type))
3250 warning(case_expr->pos, "restricted degrades to integer");
3252 return;
3254 Bad:
3255 expression_error(case_expr, "incompatible types for 'case' statement");
3258 static void evaluate_switch_statement(struct statement *stmt)
3260 struct symbol *sym;
3261 struct expression *enumcase = NULL;
3262 struct expression **enumcase_holder = &enumcase;
3263 struct expression *sel = stmt->switch_expression;
3265 evaluate_expression(sel);
3266 evaluate_statement(stmt->switch_statement);
3267 if (!sel)
3268 return;
3269 if (sel->ctype && is_enum_type(sel->ctype))
3270 enumcase_holder = NULL; /* Only check cases against switch */
3272 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
3273 struct statement *case_stmt = sym->stmt;
3274 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
3275 check_case_type(sel, case_stmt->case_to, enumcase_holder);
3276 } END_FOR_EACH_PTR(sym);
3279 struct symbol *evaluate_statement(struct statement *stmt)
3281 if (!stmt)
3282 return NULL;
3284 switch (stmt->type) {
3285 case STMT_DECLARATION: {
3286 struct symbol *s;
3287 FOR_EACH_PTR(stmt->declaration, s) {
3288 evaluate_symbol(s);
3289 } END_FOR_EACH_PTR(s);
3290 return NULL;
3293 case STMT_RETURN:
3294 return evaluate_return_expression(stmt);
3296 case STMT_EXPRESSION:
3297 if (!evaluate_expression(stmt->expression))
3298 return NULL;
3299 if (stmt->expression->ctype == &null_ctype)
3300 stmt->expression = cast_to(stmt->expression, &ptr_ctype);
3301 return degenerate(stmt->expression);
3303 case STMT_COMPOUND: {
3304 struct statement *s;
3305 struct symbol *type = NULL;
3307 /* Evaluate the return symbol in the compound statement */
3308 evaluate_symbol(stmt->ret);
3311 * Then, evaluate each statement, making the type of the
3312 * compound statement be the type of the last statement
3314 type = evaluate_statement(stmt->args);
3315 FOR_EACH_PTR(stmt->stmts, s) {
3316 type = evaluate_statement(s);
3317 } END_FOR_EACH_PTR(s);
3318 if (!type)
3319 type = &void_ctype;
3320 return type;
3322 case STMT_IF:
3323 evaluate_if_statement(stmt);
3324 return NULL;
3325 case STMT_ITERATOR:
3326 evaluate_iterator(stmt);
3327 return NULL;
3328 case STMT_SWITCH:
3329 evaluate_switch_statement(stmt);
3330 return NULL;
3331 case STMT_CASE:
3332 evaluate_case_statement(stmt);
3333 return NULL;
3334 case STMT_LABEL:
3335 return evaluate_statement(stmt->label_statement);
3336 case STMT_GOTO:
3337 evaluate_expression(stmt->goto_expression);
3338 return NULL;
3339 case STMT_NONE:
3340 break;
3341 case STMT_ASM:
3342 evaluate_asm_statement(stmt);
3343 return NULL;
3344 case STMT_CONTEXT:
3345 evaluate_expression(stmt->expression);
3346 return NULL;
3347 case STMT_RANGE:
3348 evaluate_expression(stmt->range_expression);
3349 evaluate_expression(stmt->range_low);
3350 evaluate_expression(stmt->range_high);
3351 return NULL;
3353 return NULL;