[PATCH] integer_promotions() can't get SYM_NODE or SYM_ENUM
[smatch.git] / evaluate.c
blob88435da4659652fee20988d3ef52af8989e7d997
1 /*
2 * sparse/evaluate.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
9 * Evaluate constant expressions.
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <stddef.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <limits.h>
21 #include "lib.h"
22 #include "allocate.h"
23 #include "parse.h"
24 #include "token.h"
25 #include "symbol.h"
26 #include "target.h"
27 #include "expression.h"
29 struct symbol *current_fn;
31 static struct symbol *degenerate(struct expression *expr);
32 static struct symbol *evaluate_symbol(struct symbol *sym);
34 static struct symbol *evaluate_symbol_expression(struct expression *expr)
36 struct expression *addr;
37 struct symbol *sym = expr->symbol;
38 struct symbol *base_type;
40 if (!sym) {
41 expression_error(expr, "undefined identifier '%s'", show_ident(expr->symbol_name));
42 return NULL;
45 examine_symbol_type(sym);
47 base_type = get_base_type(sym);
48 if (!base_type) {
49 expression_error(expr, "identifier '%s' has no type", show_ident(expr->symbol_name));
50 return NULL;
53 addr = alloc_expression(expr->pos, EXPR_SYMBOL);
54 addr->symbol = sym;
55 addr->symbol_name = expr->symbol_name;
56 addr->ctype = &lazy_ptr_ctype; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
57 expr->type = EXPR_PREOP;
58 expr->op = '*';
59 expr->unop = addr;
61 /* The type of a symbol is the symbol itself! */
62 expr->ctype = sym;
63 return sym;
66 static struct symbol *evaluate_string(struct expression *expr)
68 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
69 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
70 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
71 struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
72 unsigned int length = expr->string->length;
74 sym->array_size = alloc_const_expression(expr->pos, length);
75 sym->bit_size = bits_in_char * length;
76 sym->ctype.alignment = 1;
77 sym->string = 1;
78 sym->ctype.modifiers = MOD_STATIC;
79 sym->ctype.base_type = array;
80 sym->initializer = initstr;
82 initstr->ctype = sym;
83 initstr->string = expr->string;
85 array->array_size = sym->array_size;
86 array->bit_size = bits_in_char * length;
87 array->ctype.alignment = 1;
88 array->ctype.modifiers = MOD_STATIC;
89 array->ctype.base_type = &char_ctype;
91 addr->symbol = sym;
92 addr->ctype = &lazy_ptr_ctype;
94 expr->type = EXPR_PREOP;
95 expr->op = '*';
96 expr->unop = addr;
97 expr->ctype = sym;
98 return sym;
101 /* type has come from classify_type and is an integer type */
102 static inline struct symbol *integer_promotion(struct symbol *type)
104 struct symbol *orig_type = type;
105 unsigned long mod = type->ctype.modifiers;
106 int width = type->bit_size;
109 * Bitfields always promote to the base type,
110 * even if the bitfield might be bigger than
111 * an "int".
113 if (type->type == SYM_BITFIELD) {
114 type = type->ctype.base_type;
115 orig_type = type;
117 mod = type->ctype.modifiers;
118 if (width < bits_in_int)
119 return &int_ctype;
121 /* If char/short has as many bits as int, it still gets "promoted" */
122 if (mod & (MOD_CHAR | MOD_SHORT)) {
123 if (mod & MOD_UNSIGNED)
124 return &uint_ctype;
125 return &int_ctype;
127 return orig_type;
131 * integer part of usual arithmetic conversions:
132 * integer promotions are applied
133 * if left and right are identical, we are done
134 * if signedness is the same, convert one with lower rank
135 * unless unsigned argument has rank lower than signed one, convert the
136 * signed one.
137 * if signed argument is bigger than unsigned one, convert the unsigned.
138 * otherwise, convert signed.
140 * Leaving aside the integer promotions, that is equivalent to
141 * if identical, don't convert
142 * if left is bigger than right, convert right
143 * if right is bigger than left, convert right
144 * otherwise, if signedness is the same, convert one with lower rank
145 * otherwise convert the signed one.
147 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
149 unsigned long lmod, rmod;
151 left = integer_promotion(left);
152 right = integer_promotion(right);
154 if (left == right)
155 goto left;
157 if (left->bit_size > right->bit_size)
158 goto left;
160 if (right->bit_size > left->bit_size)
161 goto right;
163 lmod = left->ctype.modifiers;
164 rmod = right->ctype.modifiers;
165 if ((lmod ^ rmod) & MOD_UNSIGNED) {
166 if (lmod & MOD_UNSIGNED)
167 goto left;
168 } else if ((lmod & ~rmod) & (MOD_LONG | MOD_LONGLONG))
169 goto left;
170 right:
171 left = right;
172 left:
173 return left;
176 static int same_cast_type(struct symbol *orig, struct symbol *new)
178 return orig->bit_size == new->bit_size && orig->bit_offset == new->bit_offset;
181 static struct symbol *base_type(struct symbol *node, unsigned long *modp, unsigned long *asp)
183 unsigned long mod, as;
185 mod = 0; as = 0;
186 while (node) {
187 mod |= node->ctype.modifiers;
188 as |= node->ctype.as;
189 if (node->type == SYM_NODE) {
190 node = node->ctype.base_type;
191 continue;
193 break;
195 *modp = mod & ~MOD_IGNORE;
196 *asp = as;
197 return node;
200 static int is_same_type(struct expression *expr, struct symbol *new)
202 struct symbol *old = expr->ctype;
203 unsigned long oldmod, newmod, oldas, newas;
205 old = base_type(old, &oldmod, &oldas);
206 new = base_type(new, &newmod, &newas);
208 /* Same base type, same address space? */
209 if (old == new && oldas == newas) {
210 unsigned long difmod;
212 /* Check the modifier bits. */
213 difmod = (oldmod ^ newmod) & ~MOD_NOCAST;
215 /* Exact same type? */
216 if (!difmod)
217 return 1;
220 * Not the same type, but differs only in "const".
221 * Don't warn about MOD_NOCAST.
223 if (difmod == MOD_CONST)
224 return 0;
226 if ((oldmod | newmod) & MOD_NOCAST) {
227 const char *tofrom = "to/from";
228 if (!(newmod & MOD_NOCAST))
229 tofrom = "from";
230 if (!(oldmod & MOD_NOCAST))
231 tofrom = "to";
232 warning(expr->pos, "implicit cast %s nocast type", tofrom);
234 return 0;
237 static void
238 warn_for_different_enum_types (struct position pos,
239 struct symbol *typea,
240 struct symbol *typeb)
242 if (!Wenum_mismatch)
243 return;
244 if (typea->type == SYM_NODE)
245 typea = typea->ctype.base_type;
246 if (typeb->type == SYM_NODE)
247 typeb = typeb->ctype.base_type;
249 if (typea == typeb)
250 return;
252 if (typea->type == SYM_ENUM && typeb->type == SYM_ENUM) {
253 warning(pos, "mixing different enum types");
254 info(pos, " %s versus", show_typename(typea));
255 info(pos, " %s", show_typename(typeb));
260 * This gets called for implicit casts in assignments and
261 * integer promotion. We often want to try to move the
262 * cast down, because the ops involved may have been
263 * implicitly cast up, and we can get rid of the casts
264 * early.
266 static struct expression * cast_to(struct expression *old, struct symbol *type)
268 struct expression *expr;
270 warn_for_different_enum_types (old->pos, old->ctype, type);
272 if (old->ctype != &null_ctype && is_same_type(old, type))
273 return old;
276 * See if we can simplify the op. Move the cast down.
278 switch (old->type) {
279 case EXPR_PREOP:
280 if (old->ctype->bit_size < type->bit_size)
281 break;
282 if (old->op == '~') {
283 old->ctype = type;
284 old->unop = cast_to(old->unop, type);
285 return old;
287 break;
289 case EXPR_IMPLIED_CAST:
290 warn_for_different_enum_types(old->pos, old->ctype, type);
292 if (old->ctype->bit_size >= type->bit_size) {
293 struct expression *orig = old->cast_expression;
294 if (same_cast_type(orig->ctype, type))
295 return orig;
296 if (old->ctype->bit_offset == type->bit_offset) {
297 old->ctype = type;
298 old->cast_type = type;
299 return old;
302 break;
304 default:
305 /* nothing */;
308 expr = alloc_expression(old->pos, EXPR_IMPLIED_CAST);
309 expr->flags = old->flags;
310 expr->ctype = type;
311 expr->cast_type = type;
312 expr->cast_expression = old;
313 return expr;
316 static int is_type_type(struct symbol *type)
318 return (type->ctype.modifiers & MOD_TYPE) != 0;
321 int is_ptr_type(struct symbol *type)
323 if (type->type == SYM_NODE)
324 type = type->ctype.base_type;
325 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
328 static inline int is_float_type(struct symbol *type)
330 if (type->type == SYM_NODE)
331 type = type->ctype.base_type;
332 return type->ctype.base_type == &fp_type;
335 static inline int is_byte_type(struct symbol *type)
337 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
340 enum {
341 TYPE_NUM = 1,
342 TYPE_BITFIELD = 2,
343 TYPE_RESTRICT = 4,
344 TYPE_FLOAT = 8,
345 TYPE_PTR = 16,
346 TYPE_COMPOUND = 32,
347 TYPE_FOULED = 64,
350 static inline int classify_type(struct symbol *type, struct symbol **base)
352 static int type_class[SYM_BAD + 1] = {
353 [SYM_PTR] = TYPE_PTR,
354 [SYM_FN] = TYPE_PTR,
355 [SYM_ARRAY] = TYPE_PTR | TYPE_COMPOUND,
356 [SYM_STRUCT] = TYPE_COMPOUND,
357 [SYM_UNION] = TYPE_COMPOUND,
358 [SYM_BITFIELD] = TYPE_NUM | TYPE_BITFIELD,
359 [SYM_RESTRICT] = TYPE_NUM | TYPE_RESTRICT,
360 [SYM_FOULED] = TYPE_NUM | TYPE_RESTRICT | TYPE_FOULED,
362 if (type->type == SYM_NODE)
363 type = type->ctype.base_type;
364 if (type->type == SYM_ENUM)
365 type = type->ctype.base_type;
366 *base = type;
367 if (type->type == SYM_BASETYPE) {
368 if (type->ctype.base_type == &int_type)
369 return TYPE_NUM;
370 if (type->ctype.base_type == &fp_type)
371 return TYPE_NUM | TYPE_FLOAT;
373 return type_class[type->type];
376 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
378 static inline int is_string_type(struct symbol *type)
380 if (type->type == SYM_NODE)
381 type = type->ctype.base_type;
382 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
385 static struct symbol *bad_expr_type(struct expression *expr)
387 sparse_error(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
388 switch (expr->type) {
389 case EXPR_BINOP:
390 case EXPR_COMPARE:
391 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
392 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
393 break;
394 case EXPR_PREOP:
395 case EXPR_POSTOP:
396 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
397 break;
398 default:
399 break;
402 expr->flags = 0;
403 return expr->ctype = &bad_ctype;
406 static int restricted_value(struct expression *v, struct symbol *type)
408 if (v->type != EXPR_VALUE)
409 return 1;
410 if (v->value != 0)
411 return 1;
412 return 0;
415 static int restricted_binop(int op, struct symbol *type)
417 switch (op) {
418 case '&':
419 case '=':
420 case SPECIAL_AND_ASSIGN:
421 case SPECIAL_OR_ASSIGN:
422 case SPECIAL_XOR_ASSIGN:
423 return 1; /* unfoul */
424 case '|':
425 case '^':
426 case '?':
427 return 2; /* keep fouled */
428 case SPECIAL_EQUAL:
429 case SPECIAL_NOTEQUAL:
430 return 3; /* warn if fouled */
431 default:
432 return 0; /* warn */
436 static int restricted_unop(int op, struct symbol **type)
438 if (op == '~') {
439 if ((*type)->bit_size < bits_in_int)
440 *type = befoul(*type);
441 return 0;
442 } if (op == '+')
443 return 0;
444 return 1;
447 static struct symbol *restricted_binop_type(int op,
448 struct expression *left,
449 struct expression *right,
450 int lclass, int rclass,
451 struct symbol *ltype,
452 struct symbol *rtype)
454 struct symbol *ctype = NULL;
455 if (lclass & TYPE_RESTRICT) {
456 if (rclass & TYPE_RESTRICT) {
457 if (ltype == rtype) {
458 ctype = ltype;
459 } else if (lclass & TYPE_FOULED) {
460 if (ltype->ctype.base_type == rtype)
461 ctype = ltype;
462 } else if (rclass & TYPE_FOULED) {
463 if (rtype->ctype.base_type == ltype)
464 ctype = rtype;
466 } else {
467 if (!restricted_value(right, ltype))
468 ctype = ltype;
470 } else if (!restricted_value(left, rtype))
471 ctype = rtype;
473 if (ctype) {
474 switch (restricted_binop(op, ctype)) {
475 case 1:
476 if ((lclass ^ rclass) & TYPE_FOULED)
477 ctype = ctype->ctype.base_type;
478 break;
479 case 3:
480 if (!(lclass & rclass & TYPE_FOULED))
481 break;
482 case 0:
483 ctype = NULL;
484 default:
485 break;
489 return ctype;
492 static inline void unrestrict(struct expression *expr,
493 int class, struct symbol **ctype)
495 if (class & TYPE_RESTRICT) {
496 warning(expr->pos, "restricted degrades to integer");
497 if (class & TYPE_FOULED) /* unfoul it first */
498 *ctype = (*ctype)->ctype.base_type;
499 *ctype = (*ctype)->ctype.base_type; /* get to arithmetic type */
503 static struct symbol *usual_conversions(int op,
504 struct expression *left,
505 struct expression *right,
506 int lclass, int rclass,
507 struct symbol *ltype,
508 struct symbol *rtype)
510 struct symbol *ctype;
512 warn_for_different_enum_types(right->pos, left->ctype, right->ctype);
514 if ((lclass | rclass) & TYPE_RESTRICT)
515 goto Restr;
517 Normal:
518 if (!(lclass & TYPE_FLOAT)) {
519 if (!(rclass & TYPE_FLOAT))
520 return bigger_int_type(ltype, rtype);
521 else
522 return rtype;
523 } else if (rclass & TYPE_FLOAT) {
524 unsigned long lmod = ltype->ctype.modifiers;
525 unsigned long rmod = rtype->ctype.modifiers;
526 if (rmod & ~lmod & (MOD_LONG | MOD_LONGLONG))
527 return rtype;
528 else
529 return ltype;
530 } else
531 return ltype;
533 Restr:
534 ctype = restricted_binop_type(op, left, right,
535 lclass, rclass, ltype, rtype);
536 if (ctype)
537 return ctype;
539 unrestrict(left, lclass, &ltype);
540 unrestrict(right, rclass, &rtype);
542 goto Normal;
545 static inline int lvalue_expression(struct expression *expr)
547 return expr->type == EXPR_PREOP && expr->op == '*';
550 static int ptr_object_size(struct symbol *ptr_type)
552 if (ptr_type->type == SYM_NODE)
553 ptr_type = ptr_type->ctype.base_type;
554 if (ptr_type->type == SYM_PTR)
555 ptr_type = get_base_type(ptr_type);
556 return ptr_type->bit_size;
559 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *ctype, struct symbol *itype)
561 struct expression *index = expr->right;
562 int multiply;
563 int bit_size;
565 if (ctype == &null_ctype)
566 ctype = &ptr_ctype;
568 examine_symbol_type(ctype);
570 if (!ctype->ctype.base_type) {
571 expression_error(expr, "missing type information");
572 return NULL;
575 /* Get the size of whatever the pointer points to */
576 bit_size = ptr_object_size(ctype);
577 multiply = bit_size >> 3;
579 expr->ctype = ctype;
581 if (multiply == 1 && itype->bit_size >= bits_in_pointer)
582 return ctype;
584 if (index->type == EXPR_VALUE) {
585 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
586 unsigned long long v = index->value, mask;
587 mask = 1ULL << (itype->bit_size - 1);
588 if (v & mask)
589 v |= -mask;
590 else
591 v &= mask - 1;
592 v *= multiply;
593 mask = 1ULL << (bits_in_pointer - 1);
594 v &= mask | (mask - 1);
595 val->value = v;
596 val->ctype = ssize_t_ctype;
597 expr->right = val;
598 return ctype;
601 if (itype->bit_size < bits_in_pointer)
602 index = cast_to(index, ssize_t_ctype);
604 if (multiply > 1) {
605 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
606 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
608 val->ctype = ssize_t_ctype;
609 val->value = multiply;
611 mul->op = '*';
612 mul->ctype = ssize_t_ctype;
613 mul->left = index;
614 mul->right = val;
615 index = mul;
618 expr->right = index;
619 return ctype;
622 const char * type_difference(struct symbol *target, struct symbol *source,
623 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
625 for (;;) {
626 unsigned long mod1, mod2, diff;
627 unsigned long as1, as2;
628 int type1, type2;
629 struct symbol *base1, *base2;
631 if (target == source)
632 break;
633 if (!target || !source)
634 return "different types";
636 * Peel of per-node information.
637 * FIXME! Check alignment and context too here!
639 mod1 = target->ctype.modifiers;
640 as1 = target->ctype.as;
641 mod2 = source->ctype.modifiers;
642 as2 = source->ctype.as;
643 if (target->type == SYM_NODE) {
644 target = target->ctype.base_type;
645 if (!target)
646 return "bad types";
647 if (target->type == SYM_PTR) {
648 mod1 = 0;
649 as1 = 0;
651 mod1 |= target->ctype.modifiers;
652 as1 |= target->ctype.as;
654 if (source->type == SYM_NODE) {
655 source = source->ctype.base_type;
656 if (!source)
657 return "bad types";
658 if (source->type == SYM_PTR) {
659 mod2 = 0;
660 as2 = 0;
662 mod2 |= source->ctype.modifiers;
663 as2 |= source->ctype.as;
665 if (target->type == SYM_ENUM) {
666 target = target->ctype.base_type;
667 if (!target)
668 return "bad types";
670 if (source->type == SYM_ENUM) {
671 source = source->ctype.base_type;
672 if (!source)
673 return "bad types";
676 if (target == source)
677 break;
678 if (!target || !source)
679 return "different types";
681 type1 = target->type;
682 base1 = target->ctype.base_type;
684 type2 = source->type;
685 base2 = source->ctype.base_type;
688 * Pointers to functions compare as the function itself
690 if (type1 == SYM_PTR && base1) {
691 base1 = examine_symbol_type(base1);
692 switch (base1->type) {
693 case SYM_FN:
694 type1 = SYM_FN;
695 target = base1;
696 base1 = base1->ctype.base_type;
697 default:
698 /* nothing */;
701 if (type2 == SYM_PTR && base2) {
702 base2 = examine_symbol_type(base2);
703 switch (base2->type) {
704 case SYM_FN:
705 type2 = SYM_FN;
706 source = base2;
707 base2 = base2->ctype.base_type;
708 default:
709 /* nothing */;
713 /* Arrays degenerate to pointers for type comparisons */
714 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
715 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
717 if (type1 != type2 || type1 == SYM_RESTRICT)
718 return "different base types";
720 /* Must be same address space to be comparable */
721 if (Waddress_space && as1 != as2)
722 return "different address spaces";
724 /* Ignore differences in storage types or addressability */
725 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
726 diff &= (mod1 & ~target_mod_ignore) | (mod2 & ~source_mod_ignore);
727 if (diff) {
728 if (diff & MOD_SIZE)
729 return "different type sizes";
730 if (diff & ~MOD_SIGNEDNESS)
731 return "different modifiers";
733 /* Differs in signedness only.. */
734 if (Wtypesign) {
736 * Warn if both are explicitly signed ("unsigned" is obviously
737 * always explicit, and since we know one of them has to be
738 * unsigned, we check if the signed one was explicit).
740 if ((mod1 | mod2) & MOD_EXPLICITLY_SIGNED)
741 return "different explicit signedness";
744 * "char" matches both "unsigned char" and "signed char",
745 * so if the explicit test didn't trigger, then we should
746 * not warn about a char.
748 if (!(mod1 & MOD_CHAR))
749 return "different signedness";
753 if (type1 == SYM_FN) {
754 int i;
755 struct symbol *arg1, *arg2;
756 if (base1->variadic != base2->variadic)
757 return "incompatible variadic arguments";
758 PREPARE_PTR_LIST(target->arguments, arg1);
759 PREPARE_PTR_LIST(source->arguments, arg2);
760 i = 1;
761 for (;;) {
762 const char *diffstr;
763 diffstr = type_difference(arg1, arg2, 0, 0);
764 if (diffstr) {
765 static char argdiff[80];
766 sprintf(argdiff, "incompatible argument %d (%s)", i, diffstr);
767 return argdiff;
769 if (!arg1)
770 break;
771 NEXT_PTR_LIST(arg1);
772 NEXT_PTR_LIST(arg2);
773 i++;
775 FINISH_PTR_LIST(arg2);
776 FINISH_PTR_LIST(arg1);
779 target = base1;
780 source = base2;
782 return NULL;
785 static void bad_null(struct expression *expr)
787 if (Wnon_pointer_null)
788 warning(expr->pos, "Using plain integer as NULL pointer");
792 * Ignore differences in "volatile" and "const"ness when
793 * subtracting pointers
795 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
797 static struct symbol *evaluate_ptr_sub(struct expression *expr)
799 const char *typediff;
800 struct symbol *ctype;
801 struct symbol *ltype, *rtype;
802 struct expression *l = expr->left;
803 struct expression *r = expr->right;
805 ltype = degenerate(l);
806 rtype = degenerate(r);
808 ctype = ltype;
809 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
810 if (typediff)
811 expression_error(expr, "subtraction of different types can't work (%s)", typediff);
812 examine_symbol_type(ctype);
814 /* Figure out the base type we point to */
815 if (ctype->type == SYM_NODE)
816 ctype = ctype->ctype.base_type;
817 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
818 expression_error(expr, "subtraction of functions? Share your drugs");
819 return NULL;
821 ctype = get_base_type(ctype);
823 expr->ctype = ssize_t_ctype;
824 if (ctype->bit_size > bits_in_char) {
825 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
826 struct expression *div = expr;
827 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
828 unsigned long value = ctype->bit_size >> 3;
830 val->ctype = size_t_ctype;
831 val->value = value;
833 if (value & (value-1)) {
834 if (Wptr_subtraction_blows)
835 warning(expr->pos, "potentially expensive pointer subtraction");
838 sub->op = '-';
839 sub->ctype = ssize_t_ctype;
840 sub->left = l;
841 sub->right = r;
843 div->op = '/';
844 div->left = sub;
845 div->right = val;
848 return ssize_t_ctype;
851 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
853 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
855 struct symbol *ctype;
857 if (!expr)
858 return NULL;
860 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
861 warning(expr->pos, "assignment expression in conditional");
863 ctype = evaluate_expression(expr);
864 if (ctype) {
865 if (is_safe_type(ctype))
866 warning(expr->pos, "testing a 'safe expression'");
869 return ctype;
872 static struct symbol *evaluate_logical(struct expression *expr)
874 if (!evaluate_conditional(expr->left, 0))
875 return NULL;
876 if (!evaluate_conditional(expr->right, 0))
877 return NULL;
879 expr->ctype = &bool_ctype;
880 if (expr->flags) {
881 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
882 expr->flags = 0;
884 return &bool_ctype;
887 static struct symbol *evaluate_binop(struct expression *expr)
889 struct symbol *ltype, *rtype, *ctype;
890 int lclass = classify_type(expr->left->ctype, &ltype);
891 int rclass = classify_type(expr->right->ctype, &rtype);
892 int op = expr->op;
894 if (expr->flags) {
895 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
896 expr->flags = 0;
899 /* number op number */
900 if (lclass & rclass & TYPE_NUM) {
901 if ((lclass | rclass) & TYPE_FLOAT) {
902 switch (op) {
903 case '+': case '-': case '*': case '/':
904 break;
905 default:
906 return bad_expr_type(expr);
910 if (op == SPECIAL_LEFTSHIFT || op == SPECIAL_RIGHTSHIFT) {
911 // shifts do integer promotions, but that's it.
912 unrestrict(expr->left, lclass, &ltype);
913 unrestrict(expr->right, rclass, &rtype);
914 ctype = ltype = integer_promotion(ltype);
915 rtype = integer_promotion(rtype);
916 } else {
917 // The rest do usual conversions
918 ltype = usual_conversions(op, expr->left, expr->right,
919 lclass, rclass, ltype, rtype);
920 ctype = rtype = ltype;
923 expr->left = cast_to(expr->left, ltype);
924 expr->right = cast_to(expr->right, rtype);
925 expr->ctype = ctype;
926 return ctype;
929 /* pointer (+|-) integer */
930 if (lclass & TYPE_PTR && is_int(rclass) && (op == '+' || op == '-')) {
931 unrestrict(expr->right, rclass, &rtype);
932 return evaluate_ptr_add(expr, degenerate(expr->left), rtype);
935 /* integer + pointer */
936 if (rclass & TYPE_PTR && is_int(lclass) && op == '+') {
937 struct expression *index = expr->left;
938 unrestrict(index, lclass, &ltype);
939 expr->left = expr->right;
940 expr->right = index;
941 return evaluate_ptr_add(expr, degenerate(expr->left), ltype);
944 /* pointer - pointer */
945 if (lclass & rclass & TYPE_PTR && expr->op == '-')
946 return evaluate_ptr_sub(expr);
948 return bad_expr_type(expr);
951 static struct symbol *evaluate_comma(struct expression *expr)
953 expr->ctype = expr->right->ctype;
954 expr->flags &= expr->left->flags & expr->right->flags;
955 return expr->ctype;
958 static int modify_for_unsigned(int op)
960 if (op == '<')
961 op = SPECIAL_UNSIGNED_LT;
962 else if (op == '>')
963 op = SPECIAL_UNSIGNED_GT;
964 else if (op == SPECIAL_LTE)
965 op = SPECIAL_UNSIGNED_LTE;
966 else if (op == SPECIAL_GTE)
967 op = SPECIAL_UNSIGNED_GTE;
968 return op;
971 static inline int is_null_pointer_constant(struct expression *e)
973 if (e->ctype == &null_ctype)
974 return 1;
975 if (!(e->flags & Int_const_expr))
976 return 0;
977 return is_zero_constant(e) ? 2 : 0;
980 static struct symbol *evaluate_compare(struct expression *expr)
982 struct expression *left = expr->left, *right = expr->right;
983 struct symbol *ltype, *rtype;
984 int lclass = classify_type(degenerate(left), &ltype);
985 int rclass = classify_type(degenerate(right), &rtype);
986 struct symbol *ctype;
987 const char *typediff;
989 if (expr->flags) {
990 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
991 expr->flags = 0;
994 /* Type types? */
995 if (is_type_type(ltype) && is_type_type(rtype))
996 goto OK;
998 if (is_safe_type(left->ctype) || is_safe_type(right->ctype))
999 warning(expr->pos, "testing a 'safe expression'");
1001 /* number on number */
1002 if (lclass & rclass & TYPE_NUM) {
1003 ctype = usual_conversions(expr->op, expr->left, expr->right,
1004 lclass, rclass, ltype, rtype);
1005 expr->left = cast_to(expr->left, ctype);
1006 expr->right = cast_to(expr->right, ctype);
1007 if (ctype->ctype.modifiers & MOD_UNSIGNED)
1008 expr->op = modify_for_unsigned(expr->op);
1009 goto OK;
1012 /* at least one must be a pointer */
1013 if (!((lclass | rclass) & TYPE_PTR))
1014 return bad_expr_type(expr);
1016 /* equality comparisons can be with null pointer constants */
1017 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1018 int is_null1 = is_null_pointer_constant(left);
1019 int is_null2 = is_null_pointer_constant(right);
1020 if (is_null1 == 2)
1021 bad_null(left);
1022 if (is_null2 == 2)
1023 bad_null(right);
1024 if (is_null1 && is_null2) {
1025 int positive = expr->op == SPECIAL_EQUAL;
1026 expr->type = EXPR_VALUE;
1027 expr->value = positive;
1028 goto OK;
1030 if (is_null1) {
1031 left = cast_to(left, rtype);
1032 goto OK;
1034 if (is_null2) {
1035 right = cast_to(right, ltype);
1036 goto OK;
1038 /* they also have special treatment for pointers to void */
1039 if (lclass & rclass & TYPE_PTR) {
1040 if (get_base_type(ltype) == &void_ctype) {
1041 right = cast_to(right, ltype);
1042 goto OK;
1044 if (get_base_type(rtype) == &void_ctype) {
1045 left = cast_to(left, rtype);
1046 goto OK;
1050 /* both should be pointers */
1051 if (!(lclass & rclass & TYPE_PTR))
1052 return bad_expr_type(expr);
1054 expr->op = modify_for_unsigned(expr->op);
1055 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
1056 if (!typediff)
1057 goto OK;
1059 expression_error(expr, "incompatible types in comparison expression (%s)", typediff);
1060 return NULL;
1063 expr->ctype = &bool_ctype;
1064 return &bool_ctype;
1068 * NOTE! The degenerate case of "x ? : y", where we don't
1069 * have a true case, this will possibly promote "x" to the
1070 * same type as "y", and thus _change_ the conditional
1071 * test in the expression. But since promotion is "safe"
1072 * for testing, that's OK.
1074 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1076 struct expression **true;
1077 struct symbol *ctype, *ltype, *rtype, *lbase, *rbase;
1078 int lclass, rclass;
1079 const char * typediff;
1080 int qual;
1082 if (!evaluate_conditional(expr->conditional, 0))
1083 return NULL;
1084 if (!evaluate_expression(expr->cond_false))
1085 return NULL;
1087 ctype = degenerate(expr->conditional);
1088 rtype = degenerate(expr->cond_false);
1090 true = &expr->conditional;
1091 ltype = ctype;
1092 if (expr->cond_true) {
1093 if (!evaluate_expression(expr->cond_true))
1094 return NULL;
1095 ltype = degenerate(expr->cond_true);
1096 true = &expr->cond_true;
1099 if (expr->flags) {
1100 int flags = expr->conditional->flags & Int_const_expr;
1101 flags &= (*true)->flags & expr->cond_false->flags;
1102 if (!flags)
1103 expr->flags = 0;
1106 lclass = classify_type(ltype, &ltype);
1107 rclass = classify_type(rtype, &rtype);
1108 if (lclass & rclass & TYPE_NUM) {
1109 ctype = usual_conversions('?', *true, expr->cond_false,
1110 lclass, rclass, ltype, rtype);
1111 *true = cast_to(*true, ctype);
1112 expr->cond_false = cast_to(expr->cond_false, ctype);
1113 goto out;
1116 if ((lclass | rclass) & TYPE_PTR) {
1117 int is_null1 = is_null_pointer_constant(*true);
1118 int is_null2 = is_null_pointer_constant(expr->cond_false);
1120 if (is_null1 && is_null2) {
1121 *true = cast_to(*true, &ptr_ctype);
1122 expr->cond_false = cast_to(expr->cond_false, &ptr_ctype);
1123 ctype = &ptr_ctype;
1124 goto out;
1126 if (is_null1 && (rclass & TYPE_PTR)) {
1127 if (is_null1 == 2)
1128 bad_null(*true);
1129 *true = cast_to(*true, rtype);
1130 ctype = rtype;
1131 goto out;
1133 if (is_null2 && (lclass & TYPE_PTR)) {
1134 if (is_null2 == 2)
1135 bad_null(expr->cond_false);
1136 expr->cond_false = cast_to(expr->cond_false, ltype);
1137 ctype = ltype;
1138 goto out;
1140 if (!(lclass & rclass & TYPE_PTR)) {
1141 typediff = "different types";
1142 goto Err;
1144 /* OK, it's pointer on pointer */
1145 if (ltype->ctype.as != rtype->ctype.as) {
1146 typediff = "different address spaces";
1147 goto Err;
1150 /* need to be lazier here */
1151 lbase = get_base_type(ltype);
1152 rbase = get_base_type(rtype);
1153 qual = ltype->ctype.modifiers | rtype->ctype.modifiers;
1154 qual &= MOD_CONST | MOD_VOLATILE;
1156 if (lbase == &void_ctype) {
1157 /* XXX: pointers to function should warn here */
1158 ctype = ltype;
1159 goto Qual;
1162 if (rbase == &void_ctype) {
1163 /* XXX: pointers to function should warn here */
1164 ctype = rtype;
1165 goto Qual;
1167 /* XXX: that should be pointer to composite */
1168 ctype = ltype;
1169 typediff = type_difference(lbase, rbase, MOD_IGN, MOD_IGN);
1170 if (!typediff)
1171 goto Qual;
1172 goto Err;
1175 /* void on void, struct on same struct, union on same union */
1176 if (ltype == rtype) {
1177 ctype = ltype;
1178 goto out;
1180 typediff = "different base types";
1182 Err:
1183 expression_error(expr, "incompatible types in conditional expression (%s)", typediff);
1184 return NULL;
1186 out:
1187 expr->ctype = ctype;
1188 return ctype;
1190 Qual:
1191 if (qual & ~ctype->ctype.modifiers) {
1192 struct symbol *sym = alloc_symbol(ctype->pos, SYM_PTR);
1193 *sym = *ctype;
1194 sym->ctype.modifiers |= qual;
1195 ctype = sym;
1197 *true = cast_to(*true, ctype);
1198 expr->cond_false = cast_to(expr->cond_false, ctype);
1199 goto out;
1202 /* FP assignments can not do modulo or bit operations */
1203 static int compatible_float_op(int op)
1205 return op == SPECIAL_ADD_ASSIGN ||
1206 op == SPECIAL_SUB_ASSIGN ||
1207 op == SPECIAL_MUL_ASSIGN ||
1208 op == SPECIAL_DIV_ASSIGN;
1211 static int evaluate_assign_op(struct expression *expr)
1213 struct symbol *target = expr->left->ctype;
1214 struct symbol *source = expr->right->ctype;
1215 struct symbol *t, *s;
1216 int tclass = classify_type(target, &t);
1217 int sclass = classify_type(source, &s);
1218 int op = expr->op;
1220 if (tclass & sclass & TYPE_NUM) {
1221 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1222 expression_error(expr, "invalid assignment");
1223 return 0;
1225 if (tclass & TYPE_RESTRICT) {
1226 if (!restricted_binop(op, t)) {
1227 expression_error(expr, "bad restricted assignment");
1228 return 0;
1230 /* allowed assignments unfoul */
1231 if (sclass & TYPE_FOULED && s->ctype.base_type == t)
1232 goto Cast;
1233 if (!restricted_value(expr->right, t))
1234 return 1;
1235 } else if (!(sclass & TYPE_RESTRICT))
1236 goto Cast;
1237 /* source and target would better be identical restricted */
1238 if (t == s)
1239 return 1;
1240 warning(expr->pos, "invalid restricted assignment");
1241 expr->right = cast_to(expr->right, target);
1242 return 0;
1244 if (tclass & TYPE_PTR && is_int(sclass)) {
1245 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1246 unrestrict(expr->right, sclass, &s);
1247 evaluate_ptr_add(expr, target, s);
1248 return 1;
1250 expression_error(expr, "invalid pointer assignment");
1251 return 0;
1254 expression_error(expr, "invalid assignment");
1255 return 0;
1257 Cast:
1258 expr->right = cast_to(expr->right, target);
1259 return 1;
1262 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1263 struct expression **rp, const char *where)
1265 const char *typediff;
1266 struct symbol *source = degenerate(*rp);
1267 struct symbol *t, *s;
1268 int tclass = classify_type(target, &t);
1269 int sclass = classify_type(source, &s);
1271 if (tclass & sclass & TYPE_NUM) {
1272 if (tclass & TYPE_RESTRICT) {
1273 /* allowed assignments unfoul */
1274 if (sclass & TYPE_FOULED && s->ctype.base_type == t)
1275 goto Cast;
1276 if (!restricted_value(*rp, target))
1277 return 1;
1278 } else if (!(sclass & TYPE_RESTRICT))
1279 goto Cast;
1282 if (tclass & TYPE_PTR) {
1283 // NULL pointer is always OK
1284 int is_null = is_null_pointer_constant(*rp);
1285 if (is_null) {
1286 if (is_null == 2)
1287 bad_null(*rp);
1288 goto Cast;
1290 if (sclass & TYPE_PTR && t->ctype.as == s->ctype.as) {
1291 /* we should be more lazy here */
1292 int mod1 = t->ctype.modifiers;
1293 int mod2 = s->ctype.modifiers;
1294 s = get_base_type(s);
1295 t = get_base_type(t);
1298 * assignments to/from void * are OK, provided that
1299 * we do not remove qualifiers from pointed to [C]
1300 * or mix address spaces [sparse].
1302 if (!(mod2 & ~mod1 & (MOD_VOLATILE | MOD_CONST)))
1303 if (s == &void_ctype || t == &void_ctype)
1304 goto Cast;
1308 /* It's OK if the target is more volatile or const than the source */
1309 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
1310 if (!typediff)
1311 return 1;
1313 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1314 info(expr->pos, " expected %s", show_typename(target));
1315 info(expr->pos, " got %s", show_typename(source));
1316 *rp = cast_to(*rp, target);
1317 return 0;
1318 Cast:
1319 *rp = cast_to(*rp, target);
1320 return 1;
1323 static void mark_assigned(struct expression *expr)
1325 struct symbol *sym;
1327 if (!expr)
1328 return;
1329 switch (expr->type) {
1330 case EXPR_SYMBOL:
1331 sym = expr->symbol;
1332 if (!sym)
1333 return;
1334 if (sym->type != SYM_NODE)
1335 return;
1336 sym->ctype.modifiers |= MOD_ASSIGNED;
1337 return;
1339 case EXPR_BINOP:
1340 mark_assigned(expr->left);
1341 mark_assigned(expr->right);
1342 return;
1343 case EXPR_CAST:
1344 case EXPR_FORCE_CAST:
1345 mark_assigned(expr->cast_expression);
1346 return;
1347 case EXPR_SLICE:
1348 mark_assigned(expr->base);
1349 return;
1350 default:
1351 /* Hmm? */
1352 return;
1356 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1358 if (type->ctype.modifiers & MOD_CONST)
1359 expression_error(left, "assignment to const expression");
1361 /* We know left is an lvalue, so it's a "preop-*" */
1362 mark_assigned(left->unop);
1365 static struct symbol *evaluate_assignment(struct expression *expr)
1367 struct expression *left = expr->left;
1368 struct expression *where = expr;
1369 struct symbol *ltype;
1371 if (!lvalue_expression(left)) {
1372 expression_error(expr, "not an lvalue");
1373 return NULL;
1376 ltype = left->ctype;
1378 if (expr->op != '=') {
1379 if (!evaluate_assign_op(expr))
1380 return NULL;
1381 } else {
1382 if (!compatible_assignment_types(where, ltype, &expr->right, "assignment"))
1383 return NULL;
1386 evaluate_assign_to(left, ltype);
1388 expr->ctype = ltype;
1389 return ltype;
1392 static void examine_fn_arguments(struct symbol *fn)
1394 struct symbol *s;
1396 FOR_EACH_PTR(fn->arguments, s) {
1397 struct symbol *arg = evaluate_symbol(s);
1398 /* Array/function arguments silently degenerate into pointers */
1399 if (arg) {
1400 struct symbol *ptr;
1401 switch(arg->type) {
1402 case SYM_ARRAY:
1403 case SYM_FN:
1404 ptr = alloc_symbol(s->pos, SYM_PTR);
1405 if (arg->type == SYM_ARRAY)
1406 ptr->ctype = arg->ctype;
1407 else
1408 ptr->ctype.base_type = arg;
1409 ptr->ctype.as |= s->ctype.as;
1410 ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1412 s->ctype.base_type = ptr;
1413 s->ctype.as = 0;
1414 s->ctype.modifiers &= ~MOD_PTRINHERIT;
1415 s->bit_size = 0;
1416 s->examined = 0;
1417 examine_symbol_type(s);
1418 break;
1419 default:
1420 /* nothing */
1421 break;
1424 } END_FOR_EACH_PTR(s);
1427 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1429 /* Take the modifiers of the pointer, and apply them to the member */
1430 mod |= sym->ctype.modifiers;
1431 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1432 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1433 *newsym = *sym;
1434 newsym->ctype.as = as;
1435 newsym->ctype.modifiers = mod;
1436 sym = newsym;
1438 return sym;
1441 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1443 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1444 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1446 node->ctype.base_type = ptr;
1447 ptr->bit_size = bits_in_pointer;
1448 ptr->ctype.alignment = pointer_alignment;
1450 node->bit_size = bits_in_pointer;
1451 node->ctype.alignment = pointer_alignment;
1453 access_symbol(sym);
1454 if (sym->ctype.modifiers & MOD_REGISTER) {
1455 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1456 sym->ctype.modifiers &= ~MOD_REGISTER;
1458 if (sym->type == SYM_NODE) {
1459 ptr->ctype.as |= sym->ctype.as;
1460 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1461 sym = sym->ctype.base_type;
1463 if (degenerate && sym->type == SYM_ARRAY) {
1464 ptr->ctype.as |= sym->ctype.as;
1465 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1466 sym = sym->ctype.base_type;
1468 ptr->ctype.base_type = sym;
1470 return node;
1473 /* Arrays degenerate into pointers on pointer arithmetic */
1474 static struct symbol *degenerate(struct expression *expr)
1476 struct symbol *ctype, *base;
1478 if (!expr)
1479 return NULL;
1480 ctype = expr->ctype;
1481 if (!ctype)
1482 return NULL;
1483 base = examine_symbol_type(ctype);
1484 if (ctype->type == SYM_NODE)
1485 base = ctype->ctype.base_type;
1487 * Arrays degenerate into pointers to the entries, while
1488 * functions degenerate into pointers to themselves.
1489 * If array was part of non-lvalue compound, we create a copy
1490 * of that compound first and then act as if we were dealing with
1491 * the corresponding field in there.
1493 switch (base->type) {
1494 case SYM_ARRAY:
1495 if (expr->type == EXPR_SLICE) {
1496 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1497 struct expression *e0, *e1, *e2, *e3, *e4;
1499 a->ctype.base_type = expr->base->ctype;
1500 a->bit_size = expr->base->ctype->bit_size;
1501 a->array_size = expr->base->ctype->array_size;
1503 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1504 e0->symbol = a;
1505 e0->ctype = &lazy_ptr_ctype;
1507 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1508 e1->unop = e0;
1509 e1->op = '*';
1510 e1->ctype = expr->base->ctype; /* XXX */
1512 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1513 e2->left = e1;
1514 e2->right = expr->base;
1515 e2->op = '=';
1516 e2->ctype = expr->base->ctype;
1518 if (expr->r_bitpos) {
1519 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1520 e3->op = '+';
1521 e3->left = e0;
1522 e3->right = alloc_const_expression(expr->pos,
1523 expr->r_bitpos >> 3);
1524 e3->ctype = &lazy_ptr_ctype;
1525 } else {
1526 e3 = e0;
1529 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1530 e4->left = e2;
1531 e4->right = e3;
1532 e4->ctype = &lazy_ptr_ctype;
1534 expr->unop = e4;
1535 expr->type = EXPR_PREOP;
1536 expr->op = '*';
1538 case SYM_FN:
1539 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1540 expression_error(expr, "strange non-value function or array");
1541 return &bad_ctype;
1543 *expr = *expr->unop;
1544 ctype = create_pointer(expr, ctype, 1);
1545 expr->ctype = ctype;
1546 default:
1547 /* nothing */;
1549 return ctype;
1552 static struct symbol *evaluate_addressof(struct expression *expr)
1554 struct expression *op = expr->unop;
1555 struct symbol *ctype;
1557 if (op->op != '*' || op->type != EXPR_PREOP) {
1558 expression_error(expr, "not addressable");
1559 return NULL;
1561 ctype = op->ctype;
1562 *expr = *op->unop;
1563 expr->flags = 0;
1565 if (expr->type == EXPR_SYMBOL) {
1566 struct symbol *sym = expr->symbol;
1567 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1571 * symbol expression evaluation is lazy about the type
1572 * of the sub-expression, so we may have to generate
1573 * the type here if so..
1575 if (expr->ctype == &lazy_ptr_ctype) {
1576 ctype = create_pointer(expr, ctype, 0);
1577 expr->ctype = ctype;
1579 return expr->ctype;
1583 static struct symbol *evaluate_dereference(struct expression *expr)
1585 struct expression *op = expr->unop;
1586 struct symbol *ctype = op->ctype, *node, *target;
1588 /* Simplify: *&(expr) => (expr) */
1589 if (op->type == EXPR_PREOP && op->op == '&') {
1590 *expr = *op->unop;
1591 expr->flags = 0;
1592 return expr->ctype;
1595 /* Dereferencing a node drops all the node information. */
1596 if (ctype->type == SYM_NODE)
1597 ctype = ctype->ctype.base_type;
1599 node = alloc_symbol(expr->pos, SYM_NODE);
1600 target = ctype->ctype.base_type;
1602 switch (ctype->type) {
1603 default:
1604 expression_error(expr, "cannot dereference this type");
1605 return NULL;
1606 case SYM_PTR:
1607 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1608 merge_type(node, ctype);
1609 break;
1611 case SYM_ARRAY:
1612 if (!lvalue_expression(op)) {
1613 expression_error(op, "non-lvalue array??");
1614 return NULL;
1617 /* Do the implied "addressof" on the array */
1618 *op = *op->unop;
1621 * When an array is dereferenced, we need to pick
1622 * up the attributes of the original node too..
1624 merge_type(node, op->ctype);
1625 merge_type(node, ctype);
1626 break;
1629 node->bit_size = target->bit_size;
1630 node->array_size = target->array_size;
1632 expr->ctype = node;
1633 return node;
1637 * Unary post-ops: x++ and x--
1639 static struct symbol *evaluate_postop(struct expression *expr)
1641 struct expression *op = expr->unop;
1642 struct symbol *ctype = op->ctype;
1644 if (!lvalue_expression(expr->unop)) {
1645 expression_error(expr, "need lvalue expression for ++/--");
1646 return NULL;
1648 if (is_restricted_type(ctype) && restricted_unop(expr->op, &ctype)) {
1649 expression_error(expr, "bad operation on restricted");
1650 return NULL;
1651 } else if (is_fouled_type(ctype) && restricted_unop(expr->op, &ctype)) {
1652 expression_error(expr, "bad operation on restricted");
1653 return NULL;
1656 evaluate_assign_to(op, ctype);
1658 expr->ctype = ctype;
1659 expr->op_value = 1;
1660 if (is_ptr_type(ctype))
1661 expr->op_value = ptr_object_size(ctype) >> 3;
1663 return ctype;
1666 static struct symbol *evaluate_sign(struct expression *expr)
1668 struct symbol *ctype = expr->unop->ctype;
1669 int class = classify_type(ctype, &ctype);
1670 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1671 expr->flags = 0;
1672 /* should be an arithmetic type */
1673 if (!(class & TYPE_NUM))
1674 return bad_expr_type(expr);
1675 if (!(class & (TYPE_FLOAT|TYPE_RESTRICT))) {
1676 struct symbol *rtype = integer_promotion(ctype);
1677 expr->unop = cast_to(expr->unop, rtype);
1678 ctype = rtype;
1679 } else if ((class & TYPE_FLOAT) && expr->op != '~') {
1680 /* no conversions needed */
1681 } else if ((class & TYPE_RESTRICT) && !restricted_unop(expr->op, &ctype)) {
1682 /* no conversions needed */
1683 } else {
1684 return bad_expr_type(expr);
1686 if (expr->op == '+')
1687 *expr = *expr->unop;
1688 expr->ctype = ctype;
1689 return ctype;
1692 static struct symbol *evaluate_preop(struct expression *expr)
1694 struct symbol *ctype = expr->unop->ctype;
1696 switch (expr->op) {
1697 case '(':
1698 *expr = *expr->unop;
1699 return ctype;
1701 case '+':
1702 case '-':
1703 case '~':
1704 return evaluate_sign(expr);
1706 case '*':
1707 return evaluate_dereference(expr);
1709 case '&':
1710 return evaluate_addressof(expr);
1712 case SPECIAL_INCREMENT:
1713 case SPECIAL_DECREMENT:
1715 * From a type evaluation standpoint the preops are
1716 * the same as the postops
1718 return evaluate_postop(expr);
1720 case '!':
1721 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1722 expr->flags = 0;
1723 if (is_safe_type(ctype))
1724 warning(expr->pos, "testing a 'safe expression'");
1725 if (is_float_type(ctype)) {
1726 struct expression *arg = expr->unop;
1727 expr->type = EXPR_BINOP;
1728 expr->op = SPECIAL_EQUAL;
1729 expr->left = arg;
1730 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1731 expr->right->ctype = ctype;
1732 expr->right->fvalue = 0;
1733 } else if (is_fouled_type(ctype)) {
1734 warning(expr->pos, "restricted degrades to integer");
1736 ctype = &bool_ctype;
1737 break;
1739 default:
1740 break;
1742 expr->ctype = ctype;
1743 return &bool_ctype;
1746 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1748 struct ptr_list *head = (struct ptr_list *)_list;
1749 struct ptr_list *list = head;
1751 if (!head)
1752 return NULL;
1753 do {
1754 int i;
1755 for (i = 0; i < list->nr; i++) {
1756 struct symbol *sym = (struct symbol *) list->list[i];
1757 if (sym->ident) {
1758 if (sym->ident != ident)
1759 continue;
1760 *offset = sym->offset;
1761 return sym;
1762 } else {
1763 struct symbol *ctype = sym->ctype.base_type;
1764 struct symbol *sub;
1765 if (!ctype)
1766 continue;
1767 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1768 continue;
1769 sub = find_identifier(ident, ctype->symbol_list, offset);
1770 if (!sub)
1771 continue;
1772 *offset += sym->offset;
1773 return sub;
1776 } while ((list = list->next) != head);
1777 return NULL;
1780 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1782 struct expression *add;
1785 * Create a new add-expression
1787 * NOTE! Even if we just add zero, we need a new node
1788 * for the member pointer, since it has a different
1789 * type than the original pointer. We could make that
1790 * be just a cast, but the fact is, a node is a node,
1791 * so we might as well just do the "add zero" here.
1793 add = alloc_expression(expr->pos, EXPR_BINOP);
1794 add->op = '+';
1795 add->left = expr;
1796 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1797 add->right->ctype = &int_ctype;
1798 add->right->value = offset;
1801 * The ctype of the pointer will be lazily evaluated if
1802 * we ever take the address of this member dereference..
1804 add->ctype = &lazy_ptr_ctype;
1805 return add;
1808 /* structure/union dereference */
1809 static struct symbol *evaluate_member_dereference(struct expression *expr)
1811 int offset;
1812 struct symbol *ctype, *member;
1813 struct expression *deref = expr->deref, *add;
1814 struct ident *ident = expr->member;
1815 unsigned int mod;
1816 int address_space;
1818 if (!evaluate_expression(deref))
1819 return NULL;
1820 if (!ident) {
1821 expression_error(expr, "bad member name");
1822 return NULL;
1825 ctype = deref->ctype;
1826 address_space = ctype->ctype.as;
1827 mod = ctype->ctype.modifiers;
1828 if (ctype->type == SYM_NODE) {
1829 ctype = ctype->ctype.base_type;
1830 address_space |= ctype->ctype.as;
1831 mod |= ctype->ctype.modifiers;
1833 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1834 expression_error(expr, "expected structure or union");
1835 return NULL;
1837 examine_symbol_type(ctype);
1838 offset = 0;
1839 member = find_identifier(ident, ctype->symbol_list, &offset);
1840 if (!member) {
1841 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1842 const char *name = "<unnamed>";
1843 int namelen = 9;
1844 if (ctype->ident) {
1845 name = ctype->ident->name;
1846 namelen = ctype->ident->len;
1848 if (ctype->symbol_list)
1849 expression_error(expr, "no member '%s' in %s %.*s",
1850 show_ident(ident), type, namelen, name);
1851 else
1852 expression_error(expr, "using member '%s' in "
1853 "incomplete %s %.*s", show_ident(ident),
1854 type, namelen, name);
1855 return NULL;
1859 * The member needs to take on the address space and modifiers of
1860 * the "parent" type.
1862 member = convert_to_as_mod(member, address_space, mod);
1863 ctype = get_base_type(member);
1865 if (!lvalue_expression(deref)) {
1866 if (deref->type != EXPR_SLICE) {
1867 expr->base = deref;
1868 expr->r_bitpos = 0;
1869 } else {
1870 expr->base = deref->base;
1871 expr->r_bitpos = deref->r_bitpos;
1873 expr->r_bitpos += offset << 3;
1874 expr->type = EXPR_SLICE;
1875 expr->r_nrbits = member->bit_size;
1876 expr->r_bitpos += member->bit_offset;
1877 expr->ctype = member;
1878 return member;
1881 deref = deref->unop;
1882 expr->deref = deref;
1884 add = evaluate_offset(deref, offset);
1885 expr->type = EXPR_PREOP;
1886 expr->op = '*';
1887 expr->unop = add;
1889 expr->ctype = member;
1890 return member;
1893 static int is_promoted(struct expression *expr)
1895 while (1) {
1896 switch (expr->type) {
1897 case EXPR_BINOP:
1898 case EXPR_SELECT:
1899 case EXPR_CONDITIONAL:
1900 return 1;
1901 case EXPR_COMMA:
1902 expr = expr->right;
1903 continue;
1904 case EXPR_PREOP:
1905 switch (expr->op) {
1906 case '(':
1907 expr = expr->unop;
1908 continue;
1909 case '+':
1910 case '-':
1911 case '~':
1912 return 1;
1913 default:
1914 return 0;
1916 default:
1917 return 0;
1923 static struct symbol *evaluate_cast(struct expression *);
1925 static struct symbol *evaluate_type_information(struct expression *expr)
1927 struct symbol *sym = expr->cast_type;
1928 if (!sym) {
1929 sym = evaluate_expression(expr->cast_expression);
1930 if (!sym)
1931 return NULL;
1933 * Expressions of restricted types will possibly get
1934 * promoted - check that here
1936 if (is_restricted_type(sym)) {
1937 if (sym->bit_size < bits_in_int && is_promoted(expr))
1938 sym = &int_ctype;
1939 } else if (is_fouled_type(sym)) {
1940 sym = &int_ctype;
1943 examine_symbol_type(sym);
1944 if (is_bitfield_type(sym)) {
1945 expression_error(expr, "trying to examine bitfield type");
1946 return NULL;
1948 return sym;
1951 static struct symbol *evaluate_sizeof(struct expression *expr)
1953 struct symbol *type;
1954 int size;
1956 type = evaluate_type_information(expr);
1957 if (!type)
1958 return NULL;
1960 size = type->bit_size;
1961 if ((size < 0) || (size & 7))
1962 expression_error(expr, "cannot size expression");
1963 expr->type = EXPR_VALUE;
1964 expr->value = size >> 3;
1965 expr->taint = 0;
1966 expr->ctype = size_t_ctype;
1967 return size_t_ctype;
1970 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
1972 struct symbol *type;
1973 int size;
1975 type = evaluate_type_information(expr);
1976 if (!type)
1977 return NULL;
1979 if (type->type == SYM_NODE)
1980 type = type->ctype.base_type;
1981 if (!type)
1982 return NULL;
1983 switch (type->type) {
1984 case SYM_ARRAY:
1985 break;
1986 case SYM_PTR:
1987 type = get_base_type(type);
1988 if (type)
1989 break;
1990 default:
1991 expression_error(expr, "expected pointer expression");
1992 return NULL;
1994 size = type->bit_size;
1995 if (size & 7)
1996 size = 0;
1997 expr->type = EXPR_VALUE;
1998 expr->value = size >> 3;
1999 expr->taint = 0;
2000 expr->ctype = size_t_ctype;
2001 return size_t_ctype;
2004 static struct symbol *evaluate_alignof(struct expression *expr)
2006 struct symbol *type;
2008 type = evaluate_type_information(expr);
2009 if (!type)
2010 return NULL;
2012 expr->type = EXPR_VALUE;
2013 expr->value = type->ctype.alignment;
2014 expr->taint = 0;
2015 expr->ctype = size_t_ctype;
2016 return size_t_ctype;
2019 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
2021 struct expression *expr;
2022 struct symbol_list *argument_types = fn->arguments;
2023 struct symbol *argtype;
2024 int i = 1;
2026 PREPARE_PTR_LIST(argument_types, argtype);
2027 FOR_EACH_PTR (head, expr) {
2028 struct expression **p = THIS_ADDRESS(expr);
2029 struct symbol *ctype, *target;
2030 ctype = evaluate_expression(expr);
2032 if (!ctype)
2033 return 0;
2035 target = argtype;
2036 if (!target) {
2037 struct symbol *type;
2038 int class = classify_type(ctype, &type);
2039 if (is_int(class)) {
2040 *p = cast_to(expr, integer_promotion(type));
2041 } else if (class & TYPE_FLOAT) {
2042 unsigned long mod = type->ctype.modifiers;
2043 if (!(mod & (MOD_LONG|MOD_LONGLONG)))
2044 *p = cast_to(expr, &double_ctype);
2045 } else if (class & TYPE_PTR) {
2046 if (expr->ctype == &null_ctype)
2047 *p = cast_to(expr, &ptr_ctype);
2048 else
2049 degenerate(expr);
2051 } else {
2052 static char where[30];
2053 examine_symbol_type(target);
2054 sprintf(where, "argument %d", i);
2055 compatible_assignment_types(expr, target, p, where);
2058 i++;
2059 NEXT_PTR_LIST(argtype);
2060 } END_FOR_EACH_PTR(expr);
2061 FINISH_PTR_LIST(argtype);
2062 return 1;
2065 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
2067 struct symbol *sym;
2069 FOR_EACH_PTR(ctype->symbol_list, sym) {
2070 if (sym->ident == ident)
2071 return sym;
2072 } END_FOR_EACH_PTR(sym);
2073 return NULL;
2076 static void convert_index(struct expression *e)
2078 struct expression *child = e->idx_expression;
2079 unsigned from = e->idx_from;
2080 unsigned to = e->idx_to + 1;
2081 e->type = EXPR_POS;
2082 e->init_offset = from * (e->ctype->bit_size>>3);
2083 e->init_nr = to - from;
2084 e->init_expr = child;
2087 static void convert_ident(struct expression *e)
2089 struct expression *child = e->ident_expression;
2090 struct symbol *sym = e->field;
2091 e->type = EXPR_POS;
2092 e->init_offset = sym->offset;
2093 e->init_nr = 1;
2094 e->init_expr = child;
2097 static void convert_designators(struct expression *e)
2099 while (e) {
2100 if (e->type == EXPR_INDEX)
2101 convert_index(e);
2102 else if (e->type == EXPR_IDENTIFIER)
2103 convert_ident(e);
2104 else
2105 break;
2106 e = e->init_expr;
2110 static void excess(struct expression *e, const char *s)
2112 warning(e->pos, "excessive elements in %s initializer", s);
2116 * implicit designator for the first element
2118 static struct expression *first_subobject(struct symbol *ctype, int class,
2119 struct expression **v)
2121 struct expression *e = *v, *new;
2123 if (ctype->type == SYM_NODE)
2124 ctype = ctype->ctype.base_type;
2126 if (class & TYPE_PTR) { /* array */
2127 if (!ctype->bit_size)
2128 return NULL;
2129 new = alloc_expression(e->pos, EXPR_INDEX);
2130 new->idx_expression = e;
2131 new->ctype = ctype->ctype.base_type;
2132 } else {
2133 struct symbol *field, *p;
2134 PREPARE_PTR_LIST(ctype->symbol_list, p);
2135 while (p && !p->ident && is_bitfield_type(p))
2136 NEXT_PTR_LIST(p);
2137 field = p;
2138 FINISH_PTR_LIST(p);
2139 if (!field)
2140 return NULL;
2141 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2142 new->ident_expression = e;
2143 new->field = new->ctype = field;
2145 *v = new;
2146 return new;
2150 * sanity-check explicit designators; return the innermost one or NULL
2151 * in case of error. Assign types.
2153 static struct expression *check_designators(struct expression *e,
2154 struct symbol *ctype)
2156 struct expression *last = NULL;
2157 const char *err;
2158 while (1) {
2159 if (ctype->type == SYM_NODE)
2160 ctype = ctype->ctype.base_type;
2161 if (e->type == EXPR_INDEX) {
2162 struct symbol *type;
2163 if (ctype->type != SYM_ARRAY) {
2164 err = "array index in non-array";
2165 break;
2167 type = ctype->ctype.base_type;
2168 if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2169 unsigned offset = e->idx_to * type->bit_size;
2170 if (offset >= ctype->bit_size) {
2171 err = "index out of bounds in";
2172 break;
2175 e->ctype = ctype = type;
2176 ctype = type;
2177 last = e;
2178 if (!e->idx_expression) {
2179 err = "invalid";
2180 break;
2182 e = e->idx_expression;
2183 } else if (e->type == EXPR_IDENTIFIER) {
2184 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2185 err = "field name not in struct or union";
2186 break;
2188 ctype = find_struct_ident(ctype, e->expr_ident);
2189 if (!ctype) {
2190 err = "unknown field name in";
2191 break;
2193 e->field = e->ctype = ctype;
2194 last = e;
2195 if (!e->ident_expression) {
2196 err = "invalid";
2197 break;
2199 e = e->ident_expression;
2200 } else if (e->type == EXPR_POS) {
2201 err = "internal front-end error: EXPR_POS in";
2202 break;
2203 } else
2204 return last;
2206 expression_error(e, "%s initializer", err);
2207 return NULL;
2211 * choose the next subobject to initialize.
2213 * Get designators for next element, switch old ones to EXPR_POS.
2214 * Return the resulting expression or NULL if we'd run out of subobjects.
2215 * The innermost designator is returned in *v. Designators in old
2216 * are assumed to be already sanity-checked.
2218 static struct expression *next_designators(struct expression *old,
2219 struct symbol *ctype,
2220 struct expression *e, struct expression **v)
2222 struct expression *new = NULL;
2224 if (!old)
2225 return NULL;
2226 if (old->type == EXPR_INDEX) {
2227 struct expression *copy;
2228 unsigned n;
2230 copy = next_designators(old->idx_expression,
2231 old->ctype, e, v);
2232 if (!copy) {
2233 n = old->idx_to + 1;
2234 if (n * old->ctype->bit_size == ctype->bit_size) {
2235 convert_index(old);
2236 return NULL;
2238 copy = e;
2239 *v = new = alloc_expression(e->pos, EXPR_INDEX);
2240 } else {
2241 n = old->idx_to;
2242 new = alloc_expression(e->pos, EXPR_INDEX);
2245 new->idx_from = new->idx_to = n;
2246 new->idx_expression = copy;
2247 new->ctype = old->ctype;
2248 convert_index(old);
2249 } else if (old->type == EXPR_IDENTIFIER) {
2250 struct expression *copy;
2251 struct symbol *field;
2253 copy = next_designators(old->ident_expression,
2254 old->ctype, e, v);
2255 if (!copy) {
2256 field = old->field->next_subobject;
2257 if (!field) {
2258 convert_ident(old);
2259 return NULL;
2261 copy = e;
2262 *v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2263 } else {
2264 field = old->field;
2265 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2268 new->field = field;
2269 new->expr_ident = field->ident;
2270 new->ident_expression = copy;
2271 new->ctype = field;
2272 convert_ident(old);
2274 return new;
2277 static int handle_simple_initializer(struct expression **ep, int nested,
2278 int class, struct symbol *ctype);
2281 * deal with traversing subobjects [6.7.8(17,18,20)]
2283 static void handle_list_initializer(struct expression *expr,
2284 int class, struct symbol *ctype)
2286 struct expression *e, *last = NULL, *top = NULL, *next;
2287 int jumped = 0;
2289 FOR_EACH_PTR(expr->expr_list, e) {
2290 struct expression **v;
2291 struct symbol *type;
2292 int lclass;
2294 if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2295 if (!top) {
2296 top = e;
2297 last = first_subobject(ctype, class, &top);
2298 } else {
2299 last = next_designators(last, ctype, e, &top);
2301 if (!last) {
2302 excess(e, class & TYPE_PTR ? "array" :
2303 "struct or union");
2304 DELETE_CURRENT_PTR(e);
2305 continue;
2307 if (jumped) {
2308 warning(e->pos, "advancing past deep designator");
2309 jumped = 0;
2311 REPLACE_CURRENT_PTR(e, last);
2312 } else {
2313 next = check_designators(e, ctype);
2314 if (!next) {
2315 DELETE_CURRENT_PTR(e);
2316 continue;
2318 top = next;
2319 /* deeper than one designator? */
2320 jumped = top != e;
2321 convert_designators(last);
2322 last = e;
2325 found:
2326 lclass = classify_type(top->ctype, &type);
2327 if (top->type == EXPR_INDEX)
2328 v = &top->idx_expression;
2329 else
2330 v = &top->ident_expression;
2332 if (handle_simple_initializer(v, 1, lclass, top->ctype))
2333 continue;
2335 if (!(lclass & TYPE_COMPOUND)) {
2336 warning(e->pos, "bogus scalar initializer");
2337 DELETE_CURRENT_PTR(e);
2338 continue;
2341 next = first_subobject(type, lclass, v);
2342 if (next) {
2343 warning(e->pos, "missing braces around initializer");
2344 top = next;
2345 goto found;
2348 DELETE_CURRENT_PTR(e);
2349 excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2351 } END_FOR_EACH_PTR(e);
2353 convert_designators(last);
2354 expr->ctype = ctype;
2357 static int is_string_literal(struct expression **v)
2359 struct expression *e = *v;
2360 while (e && e->type == EXPR_PREOP && e->op == '(')
2361 e = e->unop;
2362 if (!e || e->type != EXPR_STRING)
2363 return 0;
2364 if (e != *v && Wparen_string)
2365 warning(e->pos,
2366 "array initialized from parenthesized string constant");
2367 *v = e;
2368 return 1;
2372 * We want a normal expression, possibly in one layer of braces. Warn
2373 * if the latter happens inside a list (it's legal, but likely to be
2374 * an effect of screwup). In case of anything not legal, we are definitely
2375 * having an effect of screwup, so just fail and let the caller warn.
2377 static struct expression *handle_scalar(struct expression *e, int nested)
2379 struct expression *v = NULL, *p;
2380 int count = 0;
2382 /* normal case */
2383 if (e->type != EXPR_INITIALIZER)
2384 return e;
2386 FOR_EACH_PTR(e->expr_list, p) {
2387 if (!v)
2388 v = p;
2389 count++;
2390 } END_FOR_EACH_PTR(p);
2391 if (count != 1)
2392 return NULL;
2393 switch(v->type) {
2394 case EXPR_INITIALIZER:
2395 case EXPR_INDEX:
2396 case EXPR_IDENTIFIER:
2397 return NULL;
2398 default:
2399 break;
2401 if (nested)
2402 warning(e->pos, "braces around scalar initializer");
2403 return v;
2407 * deal with the cases that don't care about subobjects:
2408 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2409 * character array <- string literal, possibly in braces [6.7.8(14)]
2410 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2411 * compound type <- initializer list in braces [6.7.8(16)]
2412 * The last one punts to handle_list_initializer() which, in turn will call
2413 * us for individual elements of the list.
2415 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2416 * the lack of support of wide char stuff in general.
2418 * One note: we need to take care not to evaluate a string literal until
2419 * we know that we *will* handle it right here. Otherwise we would screw
2420 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2421 * { "string", ...} - we need to preserve that string literal recognizable
2422 * until we dig into the inner struct.
2424 static int handle_simple_initializer(struct expression **ep, int nested,
2425 int class, struct symbol *ctype)
2427 int is_string = is_string_type(ctype);
2428 struct expression *e = *ep, *p;
2429 struct symbol *type;
2431 if (!e)
2432 return 0;
2434 /* scalar */
2435 if (!(class & TYPE_COMPOUND)) {
2436 e = handle_scalar(e, nested);
2437 if (!e)
2438 return 0;
2439 *ep = e;
2440 if (!evaluate_expression(e))
2441 return 1;
2442 compatible_assignment_types(e, ctype, ep, "initializer");
2443 return 1;
2447 * sublist; either a string, or we dig in; the latter will deal with
2448 * pathologies, so we don't need anything fancy here.
2450 if (e->type == EXPR_INITIALIZER) {
2451 if (is_string) {
2452 struct expression *v = NULL;
2453 int count = 0;
2455 FOR_EACH_PTR(e->expr_list, p) {
2456 if (!v)
2457 v = p;
2458 count++;
2459 } END_FOR_EACH_PTR(p);
2460 if (count == 1 && is_string_literal(&v)) {
2461 *ep = e = v;
2462 goto String;
2465 handle_list_initializer(e, class, ctype);
2466 return 1;
2469 /* string */
2470 if (is_string_literal(&e)) {
2471 /* either we are doing array of char, or we'll have to dig in */
2472 if (is_string) {
2473 *ep = e;
2474 goto String;
2476 return 0;
2478 /* struct or union can be initialized by compatible */
2479 if (class != TYPE_COMPOUND)
2480 return 0;
2481 type = evaluate_expression(e);
2482 if (!type)
2483 return 0;
2484 if (ctype->type == SYM_NODE)
2485 ctype = ctype->ctype.base_type;
2486 if (type->type == SYM_NODE)
2487 type = type->ctype.base_type;
2488 if (ctype == type)
2489 return 1;
2490 return 0;
2492 String:
2493 p = alloc_expression(e->pos, EXPR_STRING);
2494 *p = *e;
2495 type = evaluate_expression(p);
2496 if (ctype->bit_size != -1 &&
2497 ctype->bit_size + bits_in_char < type->bit_size) {
2498 warning(e->pos,
2499 "too long initializer-string for array of char");
2501 *ep = p;
2502 return 1;
2505 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2507 struct symbol *type;
2508 int class = classify_type(ctype, &type);
2509 if (!handle_simple_initializer(ep, 0, class, ctype))
2510 expression_error(*ep, "invalid initializer");
2513 static struct symbol *evaluate_cast(struct expression *expr)
2515 struct expression *target = expr->cast_expression;
2516 struct symbol *ctype;
2517 struct symbol *t1, *t2;
2518 int class1, class2;
2519 int as1 = 0, as2 = 0;
2521 if (!target)
2522 return NULL;
2525 * Special case: a cast can be followed by an
2526 * initializer, in which case we need to pass
2527 * the type value down to that initializer rather
2528 * than trying to evaluate it as an expression
2530 * A more complex case is when the initializer is
2531 * dereferenced as part of a post-fix expression.
2532 * We need to produce an expression that can be dereferenced.
2534 if (target->type == EXPR_INITIALIZER) {
2535 struct symbol *sym = expr->cast_type;
2536 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2538 sym->initializer = target;
2539 evaluate_symbol(sym);
2541 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2542 addr->symbol = sym;
2544 expr->type = EXPR_PREOP;
2545 expr->op = '*';
2546 expr->unop = addr;
2547 expr->ctype = sym;
2549 return sym;
2552 ctype = examine_symbol_type(expr->cast_type);
2553 expr->ctype = ctype;
2554 expr->cast_type = ctype;
2556 evaluate_expression(target);
2557 degenerate(target);
2559 class1 = classify_type(ctype, &t1);
2561 /* cast to non-integer type -> not an integer constant expression */
2562 if (!is_int(class1))
2563 expr->flags = 0;
2564 /* if argument turns out to be not an integer constant expression *and*
2565 it was not a floating literal to start with -> too bad */
2566 else if (expr->flags == Int_const_expr &&
2567 !(target->flags & Int_const_expr))
2568 expr->flags = 0;
2570 * You can always throw a value away by casting to
2571 * "void" - that's an implicit "force". Note that
2572 * the same is _not_ true of "void *".
2574 if (t1 == &void_ctype)
2575 goto out;
2577 if (class1 & TYPE_COMPOUND)
2578 warning(expr->pos, "cast to non-scalar");
2580 if (class1 == TYPE_PTR)
2581 get_base_type(t1);
2583 t2 = target->ctype;
2584 if (!t2) {
2585 expression_error(expr, "cast from unknown type");
2586 goto out;
2588 class2 = classify_type(t2, &t2);
2590 if (class2 & TYPE_COMPOUND)
2591 warning(expr->pos, "cast from non-scalar");
2593 if (expr->type == EXPR_FORCE_CAST)
2594 goto out;
2596 /* allowed cast unfouls */
2597 if (class2 & TYPE_FOULED)
2598 t2 = t2->ctype.base_type;
2600 if (t1 != t2) {
2601 if (class1 & TYPE_RESTRICT)
2602 warning(expr->pos, "cast to restricted type");
2603 if (class2 & TYPE_RESTRICT)
2604 warning(expr->pos, "cast from restricted type");
2607 if (t1 == &ulong_ctype)
2608 as1 = -1;
2609 else if (class1 == TYPE_PTR)
2610 as1 = t1->ctype.as;
2612 if (t2 == &ulong_ctype)
2613 as2 = -1;
2614 else if (class2 == TYPE_PTR)
2615 as2 = t2->ctype.as;
2617 if (!as1 && as2 > 0)
2618 warning(expr->pos, "cast removes address space of expression");
2619 if (as1 > 0 && as2 > 0 && as1 != as2)
2620 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2621 if (as1 > 0 && !as2 &&
2622 !is_null_pointer_constant(target) && Wcast_to_address_space)
2623 warning(expr->pos,
2624 "cast adds address space to expression (<asn:%d>)", as1);
2626 if (!(t1->ctype.modifiers & MOD_PTRINHERIT) && class1 == TYPE_PTR &&
2627 !as1 && (target->flags & Int_const_expr)) {
2628 if (t1->ctype.base_type == &void_ctype) {
2629 if (is_zero_constant(target)) {
2630 /* NULL */
2631 expr->type = EXPR_VALUE;
2632 expr->ctype = &null_ctype;
2633 expr->value = 0;
2634 return ctype;
2638 out:
2639 return ctype;
2643 * Evaluate a call expression with a symbol. This
2644 * should expand inline functions, and evaluate
2645 * builtins.
2647 static int evaluate_symbol_call(struct expression *expr)
2649 struct expression *fn = expr->fn;
2650 struct symbol *ctype = fn->ctype;
2652 if (fn->type != EXPR_PREOP)
2653 return 0;
2655 if (ctype->op && ctype->op->evaluate)
2656 return ctype->op->evaluate(expr);
2658 if (ctype->ctype.modifiers & MOD_INLINE) {
2659 int ret;
2660 struct symbol *curr = current_fn;
2661 current_fn = ctype->ctype.base_type;
2663 ret = inline_function(expr, ctype);
2665 /* restore the old function */
2666 current_fn = curr;
2667 return ret;
2670 return 0;
2673 static struct symbol *evaluate_call(struct expression *expr)
2675 int args, fnargs;
2676 struct symbol *ctype, *sym;
2677 struct expression *fn = expr->fn;
2678 struct expression_list *arglist = expr->args;
2680 if (!evaluate_expression(fn))
2681 return NULL;
2682 sym = ctype = fn->ctype;
2683 if (ctype->type == SYM_NODE)
2684 ctype = ctype->ctype.base_type;
2685 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
2686 ctype = get_base_type(ctype);
2688 examine_fn_arguments(ctype);
2689 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
2690 sym->op && sym->op->args) {
2691 if (!sym->op->args(expr))
2692 return NULL;
2693 } else {
2694 if (!evaluate_arguments(sym, ctype, arglist))
2695 return NULL;
2696 if (ctype->type != SYM_FN) {
2697 expression_error(expr, "not a function %s",
2698 show_ident(sym->ident));
2699 return NULL;
2701 args = expression_list_size(expr->args);
2702 fnargs = symbol_list_size(ctype->arguments);
2703 if (args < fnargs)
2704 expression_error(expr,
2705 "not enough arguments for function %s",
2706 show_ident(sym->ident));
2707 if (args > fnargs && !ctype->variadic)
2708 expression_error(expr,
2709 "too many arguments for function %s",
2710 show_ident(sym->ident));
2712 if (sym->type == SYM_NODE) {
2713 if (evaluate_symbol_call(expr))
2714 return expr->ctype;
2716 expr->ctype = ctype->ctype.base_type;
2717 return expr->ctype;
2720 static struct symbol *evaluate_offsetof(struct expression *expr)
2722 struct expression *e = expr->down;
2723 struct symbol *ctype = expr->in;
2724 int class;
2726 if (expr->op == '.') {
2727 struct symbol *field;
2728 int offset = 0;
2729 if (!ctype) {
2730 expression_error(expr, "expected structure or union");
2731 return NULL;
2733 examine_symbol_type(ctype);
2734 class = classify_type(ctype, &ctype);
2735 if (class != TYPE_COMPOUND) {
2736 expression_error(expr, "expected structure or union");
2737 return NULL;
2740 field = find_identifier(expr->ident, ctype->symbol_list, &offset);
2741 if (!field) {
2742 expression_error(expr, "unknown member");
2743 return NULL;
2745 ctype = field;
2746 expr->type = EXPR_VALUE;
2747 expr->flags = Int_const_expr;
2748 expr->value = offset;
2749 expr->taint = 0;
2750 expr->ctype = size_t_ctype;
2751 } else {
2752 if (!ctype) {
2753 expression_error(expr, "expected structure or union");
2754 return NULL;
2756 examine_symbol_type(ctype);
2757 class = classify_type(ctype, &ctype);
2758 if (class != (TYPE_COMPOUND | TYPE_PTR)) {
2759 expression_error(expr, "expected array");
2760 return NULL;
2762 ctype = ctype->ctype.base_type;
2763 if (!expr->index) {
2764 expr->type = EXPR_VALUE;
2765 expr->flags = Int_const_expr;
2766 expr->value = 0;
2767 expr->taint = 0;
2768 expr->ctype = size_t_ctype;
2769 } else {
2770 struct expression *idx = expr->index, *m;
2771 struct symbol *i_type = evaluate_expression(idx);
2772 int i_class = classify_type(i_type, &i_type);
2773 if (!is_int(i_class)) {
2774 expression_error(expr, "non-integer index");
2775 return NULL;
2777 unrestrict(idx, i_class, &i_type);
2778 idx = cast_to(idx, size_t_ctype);
2779 m = alloc_const_expression(expr->pos,
2780 ctype->bit_size >> 3);
2781 m->ctype = size_t_ctype;
2782 m->flags = Int_const_expr;
2783 expr->type = EXPR_BINOP;
2784 expr->left = idx;
2785 expr->right = m;
2786 expr->op = '*';
2787 expr->ctype = size_t_ctype;
2788 expr->flags = m->flags & idx->flags & Int_const_expr;
2791 if (e) {
2792 struct expression *copy = __alloc_expression(0);
2793 *copy = *expr;
2794 if (e->type == EXPR_OFFSETOF)
2795 e->in = ctype;
2796 if (!evaluate_expression(e))
2797 return NULL;
2798 expr->type = EXPR_BINOP;
2799 expr->flags = e->flags & copy->flags & Int_const_expr;
2800 expr->op = '+';
2801 expr->ctype = size_t_ctype;
2802 expr->left = copy;
2803 expr->right = e;
2805 return size_t_ctype;
2808 struct symbol *evaluate_expression(struct expression *expr)
2810 if (!expr)
2811 return NULL;
2812 if (expr->ctype)
2813 return expr->ctype;
2815 switch (expr->type) {
2816 case EXPR_VALUE:
2817 case EXPR_FVALUE:
2818 expression_error(expr, "value expression without a type");
2819 return NULL;
2820 case EXPR_STRING:
2821 return evaluate_string(expr);
2822 case EXPR_SYMBOL:
2823 return evaluate_symbol_expression(expr);
2824 case EXPR_BINOP:
2825 if (!evaluate_expression(expr->left))
2826 return NULL;
2827 if (!evaluate_expression(expr->right))
2828 return NULL;
2829 return evaluate_binop(expr);
2830 case EXPR_LOGICAL:
2831 return evaluate_logical(expr);
2832 case EXPR_COMMA:
2833 evaluate_expression(expr->left);
2834 if (!evaluate_expression(expr->right))
2835 return NULL;
2836 return evaluate_comma(expr);
2837 case EXPR_COMPARE:
2838 if (!evaluate_expression(expr->left))
2839 return NULL;
2840 if (!evaluate_expression(expr->right))
2841 return NULL;
2842 return evaluate_compare(expr);
2843 case EXPR_ASSIGNMENT:
2844 if (!evaluate_expression(expr->left))
2845 return NULL;
2846 if (!evaluate_expression(expr->right))
2847 return NULL;
2848 return evaluate_assignment(expr);
2849 case EXPR_PREOP:
2850 if (!evaluate_expression(expr->unop))
2851 return NULL;
2852 return evaluate_preop(expr);
2853 case EXPR_POSTOP:
2854 if (!evaluate_expression(expr->unop))
2855 return NULL;
2856 return evaluate_postop(expr);
2857 case EXPR_CAST:
2858 case EXPR_FORCE_CAST:
2859 case EXPR_IMPLIED_CAST:
2860 return evaluate_cast(expr);
2861 case EXPR_SIZEOF:
2862 return evaluate_sizeof(expr);
2863 case EXPR_PTRSIZEOF:
2864 return evaluate_ptrsizeof(expr);
2865 case EXPR_ALIGNOF:
2866 return evaluate_alignof(expr);
2867 case EXPR_DEREF:
2868 return evaluate_member_dereference(expr);
2869 case EXPR_CALL:
2870 return evaluate_call(expr);
2871 case EXPR_SELECT:
2872 case EXPR_CONDITIONAL:
2873 return evaluate_conditional_expression(expr);
2874 case EXPR_STATEMENT:
2875 expr->ctype = evaluate_statement(expr->statement);
2876 return expr->ctype;
2878 case EXPR_LABEL:
2879 expr->ctype = &ptr_ctype;
2880 return &ptr_ctype;
2882 case EXPR_TYPE:
2883 /* Evaluate the type of the symbol .. */
2884 evaluate_symbol(expr->symbol);
2885 /* .. but the type of the _expression_ is a "type" */
2886 expr->ctype = &type_ctype;
2887 return &type_ctype;
2889 case EXPR_OFFSETOF:
2890 return evaluate_offsetof(expr);
2892 /* These can not exist as stand-alone expressions */
2893 case EXPR_INITIALIZER:
2894 case EXPR_IDENTIFIER:
2895 case EXPR_INDEX:
2896 case EXPR_POS:
2897 expression_error(expr, "internal front-end error: initializer in expression");
2898 return NULL;
2899 case EXPR_SLICE:
2900 expression_error(expr, "internal front-end error: SLICE re-evaluated");
2901 return NULL;
2903 return NULL;
2906 static void check_duplicates(struct symbol *sym)
2908 int declared = 0;
2909 struct symbol *next = sym;
2911 while ((next = next->same_symbol) != NULL) {
2912 const char *typediff;
2913 evaluate_symbol(next);
2914 declared++;
2915 typediff = type_difference(sym, next, 0, 0);
2916 if (typediff) {
2917 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2918 show_ident(sym->ident),
2919 stream_name(next->pos.stream), next->pos.line, typediff);
2920 return;
2923 if (!declared) {
2924 unsigned long mod = sym->ctype.modifiers;
2925 if (mod & (MOD_STATIC | MOD_REGISTER))
2926 return;
2927 if (!(mod & MOD_TOPLEVEL))
2928 return;
2929 if (!Wdecl)
2930 return;
2931 if (sym->ident == &main_ident)
2932 return;
2933 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
2937 static struct symbol *evaluate_symbol(struct symbol *sym)
2939 struct symbol *base_type;
2941 if (!sym)
2942 return sym;
2943 if (sym->evaluated)
2944 return sym;
2945 sym->evaluated = 1;
2947 sym = examine_symbol_type(sym);
2948 base_type = get_base_type(sym);
2949 if (!base_type)
2950 return NULL;
2952 /* Evaluate the initializers */
2953 if (sym->initializer)
2954 evaluate_initializer(sym, &sym->initializer);
2956 /* And finally, evaluate the body of the symbol too */
2957 if (base_type->type == SYM_FN) {
2958 struct symbol *curr = current_fn;
2960 current_fn = base_type;
2962 examine_fn_arguments(base_type);
2963 if (!base_type->stmt && base_type->inline_stmt)
2964 uninline(sym);
2965 if (base_type->stmt)
2966 evaluate_statement(base_type->stmt);
2968 current_fn = curr;
2971 return base_type;
2974 void evaluate_symbol_list(struct symbol_list *list)
2976 struct symbol *sym;
2978 FOR_EACH_PTR(list, sym) {
2979 evaluate_symbol(sym);
2980 check_duplicates(sym);
2981 } END_FOR_EACH_PTR(sym);
2984 static struct symbol *evaluate_return_expression(struct statement *stmt)
2986 struct expression *expr = stmt->expression;
2987 struct symbol *fntype;
2989 evaluate_expression(expr);
2990 fntype = current_fn->ctype.base_type;
2991 if (!fntype || fntype == &void_ctype) {
2992 if (expr && expr->ctype != &void_ctype)
2993 expression_error(expr, "return expression in %s function", fntype?"void":"typeless");
2994 if (expr && Wreturn_void)
2995 warning(stmt->pos, "returning void-valued expression");
2996 return NULL;
2999 if (!expr) {
3000 sparse_error(stmt->pos, "return with no return value");
3001 return NULL;
3003 if (!expr->ctype)
3004 return NULL;
3005 compatible_assignment_types(expr, fntype, &stmt->expression, "return expression");
3006 return NULL;
3009 static void evaluate_if_statement(struct statement *stmt)
3011 if (!stmt->if_conditional)
3012 return;
3014 evaluate_conditional(stmt->if_conditional, 0);
3015 evaluate_statement(stmt->if_true);
3016 evaluate_statement(stmt->if_false);
3019 static void evaluate_iterator(struct statement *stmt)
3021 evaluate_conditional(stmt->iterator_pre_condition, 1);
3022 evaluate_conditional(stmt->iterator_post_condition,1);
3023 evaluate_statement(stmt->iterator_pre_statement);
3024 evaluate_statement(stmt->iterator_statement);
3025 evaluate_statement(stmt->iterator_post_statement);
3028 static void verify_output_constraint(struct expression *expr, const char *constraint)
3030 switch (*constraint) {
3031 case '=': /* Assignment */
3032 case '+': /* Update */
3033 break;
3034 default:
3035 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
3039 static void verify_input_constraint(struct expression *expr, const char *constraint)
3041 switch (*constraint) {
3042 case '=': /* Assignment */
3043 case '+': /* Update */
3044 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
3048 static void evaluate_asm_statement(struct statement *stmt)
3050 struct expression *expr;
3051 int state;
3053 expr = stmt->asm_string;
3054 if (!expr || expr->type != EXPR_STRING) {
3055 sparse_error(stmt->pos, "need constant string for inline asm");
3056 return;
3059 state = 0;
3060 FOR_EACH_PTR(stmt->asm_outputs, expr) {
3061 struct ident *ident;
3063 switch (state) {
3064 case 0: /* Identifier */
3065 state = 1;
3066 ident = (struct ident *)expr;
3067 continue;
3069 case 1: /* Constraint */
3070 state = 2;
3071 if (!expr || expr->type != EXPR_STRING) {
3072 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
3073 *THIS_ADDRESS(expr) = NULL;
3074 continue;
3076 verify_output_constraint(expr, expr->string->data);
3077 continue;
3079 case 2: /* Expression */
3080 state = 0;
3081 if (!evaluate_expression(expr))
3082 return;
3083 if (!lvalue_expression(expr))
3084 warning(expr->pos, "asm output is not an lvalue");
3085 evaluate_assign_to(expr, expr->ctype);
3086 continue;
3088 } END_FOR_EACH_PTR(expr);
3090 state = 0;
3091 FOR_EACH_PTR(stmt->asm_inputs, expr) {
3092 struct ident *ident;
3094 switch (state) {
3095 case 0: /* Identifier */
3096 state = 1;
3097 ident = (struct ident *)expr;
3098 continue;
3100 case 1: /* Constraint */
3101 state = 2;
3102 if (!expr || expr->type != EXPR_STRING) {
3103 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
3104 *THIS_ADDRESS(expr) = NULL;
3105 continue;
3107 verify_input_constraint(expr, expr->string->data);
3108 continue;
3110 case 2: /* Expression */
3111 state = 0;
3112 if (!evaluate_expression(expr))
3113 return;
3114 continue;
3116 } END_FOR_EACH_PTR(expr);
3118 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
3119 if (!expr) {
3120 sparse_error(stmt->pos, "bad asm output");
3121 return;
3123 if (expr->type == EXPR_STRING)
3124 continue;
3125 expression_error(expr, "asm clobber is not a string");
3126 } END_FOR_EACH_PTR(expr);
3129 static void evaluate_case_statement(struct statement *stmt)
3131 evaluate_expression(stmt->case_expression);
3132 evaluate_expression(stmt->case_to);
3133 evaluate_statement(stmt->case_statement);
3136 static void check_case_type(struct expression *switch_expr,
3137 struct expression *case_expr,
3138 struct expression **enumcase)
3140 struct symbol *switch_type, *case_type;
3141 int sclass, cclass;
3143 if (!case_expr)
3144 return;
3146 switch_type = switch_expr->ctype;
3147 case_type = evaluate_expression(case_expr);
3149 if (!switch_type || !case_type)
3150 goto Bad;
3151 if (enumcase) {
3152 if (*enumcase)
3153 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
3154 else if (is_enum_type(case_type))
3155 *enumcase = case_expr;
3158 sclass = classify_type(switch_type, &switch_type);
3159 cclass = classify_type(case_type, &case_type);
3161 /* both should be arithmetic */
3162 if (!(sclass & cclass & TYPE_NUM))
3163 goto Bad;
3165 /* neither should be floating */
3166 if ((sclass | cclass) & TYPE_FLOAT)
3167 goto Bad;
3169 /* if neither is restricted, we are OK */
3170 if (!((sclass | cclass) & TYPE_RESTRICT))
3171 return;
3173 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
3174 cclass, sclass, case_type, switch_type))
3175 warning(case_expr->pos, "restricted degrades to integer");
3177 return;
3179 Bad:
3180 expression_error(case_expr, "incompatible types for 'case' statement");
3183 static void evaluate_switch_statement(struct statement *stmt)
3185 struct symbol *sym;
3186 struct expression *enumcase = NULL;
3187 struct expression **enumcase_holder = &enumcase;
3188 struct expression *sel = stmt->switch_expression;
3190 evaluate_expression(sel);
3191 evaluate_statement(stmt->switch_statement);
3192 if (!sel)
3193 return;
3194 if (sel->ctype && is_enum_type(sel->ctype))
3195 enumcase_holder = NULL; /* Only check cases against switch */
3197 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
3198 struct statement *case_stmt = sym->stmt;
3199 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
3200 check_case_type(sel, case_stmt->case_to, enumcase_holder);
3201 } END_FOR_EACH_PTR(sym);
3204 struct symbol *evaluate_statement(struct statement *stmt)
3206 if (!stmt)
3207 return NULL;
3209 switch (stmt->type) {
3210 case STMT_DECLARATION: {
3211 struct symbol *s;
3212 FOR_EACH_PTR(stmt->declaration, s) {
3213 evaluate_symbol(s);
3214 } END_FOR_EACH_PTR(s);
3215 return NULL;
3218 case STMT_RETURN:
3219 return evaluate_return_expression(stmt);
3221 case STMT_EXPRESSION:
3222 if (!evaluate_expression(stmt->expression))
3223 return NULL;
3224 if (stmt->expression->ctype == &null_ctype)
3225 stmt->expression = cast_to(stmt->expression, &ptr_ctype);
3226 return degenerate(stmt->expression);
3228 case STMT_COMPOUND: {
3229 struct statement *s;
3230 struct symbol *type = NULL;
3232 /* Evaluate the return symbol in the compound statement */
3233 evaluate_symbol(stmt->ret);
3236 * Then, evaluate each statement, making the type of the
3237 * compound statement be the type of the last statement
3239 type = evaluate_statement(stmt->args);
3240 FOR_EACH_PTR(stmt->stmts, s) {
3241 type = evaluate_statement(s);
3242 } END_FOR_EACH_PTR(s);
3243 if (!type)
3244 type = &void_ctype;
3245 return type;
3247 case STMT_IF:
3248 evaluate_if_statement(stmt);
3249 return NULL;
3250 case STMT_ITERATOR:
3251 evaluate_iterator(stmt);
3252 return NULL;
3253 case STMT_SWITCH:
3254 evaluate_switch_statement(stmt);
3255 return NULL;
3256 case STMT_CASE:
3257 evaluate_case_statement(stmt);
3258 return NULL;
3259 case STMT_LABEL:
3260 return evaluate_statement(stmt->label_statement);
3261 case STMT_GOTO:
3262 evaluate_expression(stmt->goto_expression);
3263 return NULL;
3264 case STMT_NONE:
3265 break;
3266 case STMT_ASM:
3267 evaluate_asm_statement(stmt);
3268 return NULL;
3269 case STMT_CONTEXT:
3270 evaluate_expression(stmt->expression);
3271 return NULL;
3272 case STMT_RANGE:
3273 evaluate_expression(stmt->range_expression);
3274 evaluate_expression(stmt->range_low);
3275 evaluate_expression(stmt->range_high);
3276 return NULL;
3278 return NULL;