handle assignments like: foo = x < 1;
[smatch.git] / evaluate.c
blob28bfd7c684da43dbc7e3a7c0a78d8615d949110b
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 = bytes_to_bits(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 = bytes_to_bits(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_ALL))
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 enum {
317 TYPE_NUM = 1,
318 TYPE_BITFIELD = 2,
319 TYPE_RESTRICT = 4,
320 TYPE_FLOAT = 8,
321 TYPE_PTR = 16,
322 TYPE_COMPOUND = 32,
323 TYPE_FOULED = 64,
324 TYPE_FN = 128,
327 static inline int classify_type(struct symbol *type, struct symbol **base)
329 static int type_class[SYM_BAD + 1] = {
330 [SYM_PTR] = TYPE_PTR,
331 [SYM_FN] = TYPE_PTR | TYPE_FN,
332 [SYM_ARRAY] = TYPE_PTR | TYPE_COMPOUND,
333 [SYM_STRUCT] = TYPE_COMPOUND,
334 [SYM_UNION] = TYPE_COMPOUND,
335 [SYM_BITFIELD] = TYPE_NUM | TYPE_BITFIELD,
336 [SYM_RESTRICT] = TYPE_NUM | TYPE_RESTRICT,
337 [SYM_FOULED] = TYPE_NUM | TYPE_RESTRICT | TYPE_FOULED,
339 if (type->type == SYM_NODE)
340 type = type->ctype.base_type;
341 if (type->type == SYM_TYPEOF) {
342 type = evaluate_expression(type->initializer);
343 if (type->type == SYM_NODE)
344 type = type->ctype.base_type;
346 if (type->type == SYM_ENUM)
347 type = type->ctype.base_type;
348 *base = type;
349 if (type->type == SYM_BASETYPE) {
350 if (type->ctype.base_type == &int_type)
351 return TYPE_NUM;
352 if (type->ctype.base_type == &fp_type)
353 return TYPE_NUM | TYPE_FLOAT;
355 return type_class[type->type];
358 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
360 static inline int is_string_type(struct symbol *type)
362 if (type->type == SYM_NODE)
363 type = type->ctype.base_type;
364 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
367 static struct symbol *bad_expr_type(struct expression *expr)
369 sparse_error(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
370 switch (expr->type) {
371 case EXPR_BINOP:
372 case EXPR_COMPARE:
373 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
374 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
375 break;
376 case EXPR_PREOP:
377 case EXPR_POSTOP:
378 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
379 break;
380 default:
381 break;
384 expr->flags = 0;
385 return expr->ctype = &bad_ctype;
388 static int restricted_value(struct expression *v, struct symbol *type)
390 if (v->type != EXPR_VALUE)
391 return 1;
392 if (v->value != 0)
393 return 1;
394 return 0;
397 static int restricted_binop(int op, struct symbol *type)
399 switch (op) {
400 case '&':
401 case '=':
402 case SPECIAL_AND_ASSIGN:
403 case SPECIAL_OR_ASSIGN:
404 case SPECIAL_XOR_ASSIGN:
405 return 1; /* unfoul */
406 case '|':
407 case '^':
408 case '?':
409 return 2; /* keep fouled */
410 case SPECIAL_EQUAL:
411 case SPECIAL_NOTEQUAL:
412 return 3; /* warn if fouled */
413 default:
414 return 0; /* warn */
418 static int restricted_unop(int op, struct symbol **type)
420 if (op == '~') {
421 if ((*type)->bit_size < bits_in_int)
422 *type = befoul(*type);
423 return 0;
424 } if (op == '+')
425 return 0;
426 return 1;
429 /* type should be SYM_FOULED */
430 static inline struct symbol *unfoul(struct symbol *type)
432 return type->ctype.base_type;
435 static struct symbol *restricted_binop_type(int op,
436 struct expression *left,
437 struct expression *right,
438 int lclass, int rclass,
439 struct symbol *ltype,
440 struct symbol *rtype)
442 struct symbol *ctype = NULL;
443 if (lclass & TYPE_RESTRICT) {
444 if (rclass & TYPE_RESTRICT) {
445 if (ltype == rtype) {
446 ctype = ltype;
447 } else if (lclass & TYPE_FOULED) {
448 if (unfoul(ltype) == rtype)
449 ctype = ltype;
450 } else if (rclass & TYPE_FOULED) {
451 if (unfoul(rtype) == ltype)
452 ctype = rtype;
454 } else {
455 if (!restricted_value(right, ltype))
456 ctype = ltype;
458 } else if (!restricted_value(left, rtype))
459 ctype = rtype;
461 if (ctype) {
462 switch (restricted_binop(op, ctype)) {
463 case 1:
464 if ((lclass ^ rclass) & TYPE_FOULED)
465 ctype = unfoul(ctype);
466 break;
467 case 3:
468 if (!(lclass & rclass & TYPE_FOULED))
469 break;
470 case 0:
471 ctype = NULL;
472 default:
473 break;
477 return ctype;
480 static inline void unrestrict(struct expression *expr,
481 int class, struct symbol **ctype)
483 if (class & TYPE_RESTRICT) {
484 if (class & TYPE_FOULED)
485 *ctype = unfoul(*ctype);
486 warning(expr->pos, "%s degrades to integer",
487 show_typename(*ctype));
488 *ctype = (*ctype)->ctype.base_type; /* get to arithmetic type */
492 static struct symbol *usual_conversions(int op,
493 struct expression *left,
494 struct expression *right,
495 int lclass, int rclass,
496 struct symbol *ltype,
497 struct symbol *rtype)
499 struct symbol *ctype;
501 warn_for_different_enum_types(right->pos, left->ctype, right->ctype);
503 if ((lclass | rclass) & TYPE_RESTRICT)
504 goto Restr;
506 Normal:
507 if (!(lclass & TYPE_FLOAT)) {
508 if (!(rclass & TYPE_FLOAT))
509 return bigger_int_type(ltype, rtype);
510 else
511 return rtype;
512 } else if (rclass & TYPE_FLOAT) {
513 unsigned long lmod = ltype->ctype.modifiers;
514 unsigned long rmod = rtype->ctype.modifiers;
515 if (rmod & ~lmod & (MOD_LONG_ALL))
516 return rtype;
517 else
518 return ltype;
519 } else
520 return ltype;
522 Restr:
523 ctype = restricted_binop_type(op, left, right,
524 lclass, rclass, ltype, rtype);
525 if (ctype)
526 return ctype;
528 unrestrict(left, lclass, &ltype);
529 unrestrict(right, rclass, &rtype);
531 goto Normal;
534 static inline int lvalue_expression(struct expression *expr)
536 return expr->type == EXPR_PREOP && expr->op == '*';
539 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *itype)
541 struct expression *index = expr->right;
542 struct symbol *ctype, *base;
543 int multiply;
545 classify_type(degenerate(expr->left), &ctype);
546 base = examine_pointer_target(ctype);
548 if (!base) {
549 expression_error(expr, "missing type information");
550 return NULL;
552 if (is_function(base)) {
553 expression_error(expr, "arithmetics on pointers to functions");
554 return NULL;
557 /* Get the size of whatever the pointer points to */
558 multiply = is_void_type(base) ? 1 : bits_to_bytes(base->bit_size);
560 if (ctype == &null_ctype)
561 ctype = &ptr_ctype;
562 expr->ctype = ctype;
564 if (multiply == 1 && itype->bit_size >= bits_in_pointer)
565 return ctype;
567 if (index->type == EXPR_VALUE) {
568 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
569 unsigned long long v = index->value, mask;
570 mask = 1ULL << (itype->bit_size - 1);
571 if (v & mask)
572 v |= -mask;
573 else
574 v &= mask - 1;
575 v *= multiply;
576 mask = 1ULL << (bits_in_pointer - 1);
577 v &= mask | (mask - 1);
578 val->value = v;
579 val->ctype = ssize_t_ctype;
580 expr->right = val;
581 return ctype;
584 if (itype->bit_size < bits_in_pointer)
585 index = cast_to(index, ssize_t_ctype);
587 if (multiply > 1) {
588 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
589 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
591 val->ctype = ssize_t_ctype;
592 val->value = multiply;
594 mul->op = '*';
595 mul->ctype = ssize_t_ctype;
596 mul->left = index;
597 mul->right = val;
598 index = mul;
601 expr->right = index;
602 return ctype;
605 static void examine_fn_arguments(struct symbol *fn);
607 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
609 const char *type_difference(struct ctype *c1, struct ctype *c2,
610 unsigned long mod1, unsigned long mod2)
612 unsigned long as1 = c1->as, as2 = c2->as;
613 struct symbol *t1 = c1->base_type;
614 struct symbol *t2 = c2->base_type;
615 int move1 = 1, move2 = 1;
616 mod1 |= c1->modifiers;
617 mod2 |= c2->modifiers;
618 for (;;) {
619 unsigned long diff;
620 int type;
621 struct symbol *base1 = t1->ctype.base_type;
622 struct symbol *base2 = t2->ctype.base_type;
625 * FIXME! Collect alignment and context too here!
627 if (move1) {
628 if (t1 && t1->type != SYM_PTR) {
629 mod1 |= t1->ctype.modifiers;
630 as1 |= t1->ctype.as;
632 move1 = 0;
635 if (move2) {
636 if (t2 && t2->type != SYM_PTR) {
637 mod2 |= t2->ctype.modifiers;
638 as2 |= t2->ctype.as;
640 move2 = 0;
643 if (t1 == t2)
644 break;
645 if (!t1 || !t2)
646 return "different types";
648 if (t1->type == SYM_NODE || t1->type == SYM_ENUM) {
649 t1 = base1;
650 move1 = 1;
651 if (!t1)
652 return "bad types";
653 continue;
656 if (t2->type == SYM_NODE || t2->type == SYM_ENUM) {
657 t2 = base2;
658 move2 = 1;
659 if (!t2)
660 return "bad types";
661 continue;
664 move1 = move2 = 1;
665 type = t1->type;
666 if (type != t2->type)
667 return "different base types";
669 switch (type) {
670 default:
671 sparse_error(t1->pos,
672 "internal error: bad type in derived(%d)",
673 type);
674 return "bad types";
675 case SYM_RESTRICT:
676 return "different base types";
677 case SYM_UNION:
678 case SYM_STRUCT:
679 /* allow definition of incomplete structs and unions */
680 if (t1->ident == t2->ident)
681 return NULL;
682 return "different base types";
683 case SYM_ARRAY:
684 /* XXX: we ought to compare sizes */
685 break;
686 case SYM_PTR:
687 if (as1 != as2)
688 return "different address spaces";
689 /* MOD_SPECIFIER is due to idiocy in parse.c */
690 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SPECIFIER)
691 return "different modifiers";
692 /* we could be lazier here */
693 base1 = examine_pointer_target(t1);
694 base2 = examine_pointer_target(t2);
695 mod1 = t1->ctype.modifiers;
696 as1 = t1->ctype.as;
697 mod2 = t2->ctype.modifiers;
698 as2 = t2->ctype.as;
699 break;
700 case SYM_FN: {
701 struct symbol *arg1, *arg2;
702 int i;
704 if (as1 != as2)
705 return "different address spaces";
706 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
707 return "different modifiers";
708 mod1 = t1->ctype.modifiers;
709 as1 = t1->ctype.as;
710 mod2 = t2->ctype.modifiers;
711 as2 = t2->ctype.as;
713 if (base1->variadic != base2->variadic)
714 return "incompatible variadic arguments";
715 examine_fn_arguments(t1);
716 examine_fn_arguments(t2);
717 PREPARE_PTR_LIST(t1->arguments, arg1);
718 PREPARE_PTR_LIST(t2->arguments, arg2);
719 i = 1;
720 for (;;) {
721 const char *diffstr;
722 if (!arg1 && !arg2)
723 break;
724 if (!arg1 || !arg2)
725 return "different argument counts";
726 diffstr = type_difference(&arg1->ctype,
727 &arg2->ctype,
728 MOD_IGN, MOD_IGN);
729 if (diffstr) {
730 static char argdiff[80];
731 sprintf(argdiff, "incompatible argument %d (%s)", i, diffstr);
732 return argdiff;
734 NEXT_PTR_LIST(arg1);
735 NEXT_PTR_LIST(arg2);
736 i++;
738 FINISH_PTR_LIST(arg2);
739 FINISH_PTR_LIST(arg1);
740 break;
742 case SYM_BASETYPE:
743 if (as1 != as2)
744 return "different address spaces";
745 if (base1 != base2)
746 return "different base types";
747 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
748 if (!diff)
749 return NULL;
750 if (diff & MOD_SIZE)
751 return "different type sizes";
752 else if (diff & ~MOD_SIGNEDNESS)
753 return "different modifiers";
754 else
755 return "different signedness";
757 t1 = base1;
758 t2 = base2;
760 if (as1 != as2)
761 return "different address spaces";
762 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
763 return "different modifiers";
764 return NULL;
767 static void bad_null(struct expression *expr)
769 if (Wnon_pointer_null)
770 warning(expr->pos, "Using plain integer as NULL pointer");
773 static unsigned long target_qualifiers(struct symbol *type)
775 unsigned long mod = type->ctype.modifiers & MOD_IGN;
776 if (type->ctype.base_type && type->ctype.base_type->type == SYM_ARRAY)
777 mod = 0;
778 return mod;
781 static struct symbol *evaluate_ptr_sub(struct expression *expr)
783 const char *typediff;
784 struct symbol *ltype, *rtype;
785 struct expression *l = expr->left;
786 struct expression *r = expr->right;
787 struct symbol *lbase, *rbase;
789 classify_type(degenerate(l), &ltype);
790 classify_type(degenerate(r), &rtype);
792 lbase = examine_pointer_target(ltype);
793 rbase = examine_pointer_target(rtype);
794 typediff = type_difference(&ltype->ctype, &rtype->ctype,
795 target_qualifiers(rtype),
796 target_qualifiers(ltype));
797 if (typediff)
798 expression_error(expr, "subtraction of different types can't work (%s)", typediff);
800 if (is_function(lbase)) {
801 expression_error(expr, "subtraction of functions? Share your drugs");
802 return NULL;
805 expr->ctype = ssize_t_ctype;
806 if (lbase->bit_size > bits_in_char) {
807 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
808 struct expression *div = expr;
809 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
810 unsigned long value = bits_to_bytes(lbase->bit_size);
812 val->ctype = size_t_ctype;
813 val->value = value;
815 if (value & (value-1)) {
816 if (Wptr_subtraction_blows)
817 warning(expr->pos, "potentially expensive pointer subtraction");
820 sub->op = '-';
821 sub->ctype = ssize_t_ctype;
822 sub->left = l;
823 sub->right = r;
825 div->op = '/';
826 div->left = sub;
827 div->right = val;
830 return ssize_t_ctype;
833 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
835 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
837 struct symbol *ctype;
839 if (!expr)
840 return NULL;
842 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
843 warning(expr->pos, "assignment expression in conditional");
845 ctype = evaluate_expression(expr);
846 if (ctype) {
847 if (is_safe_type(ctype))
848 warning(expr->pos, "testing a 'safe expression'");
851 return ctype;
854 static struct symbol *evaluate_logical(struct expression *expr)
856 if (!evaluate_conditional(expr->left, 0))
857 return NULL;
858 if (!evaluate_conditional(expr->right, 0))
859 return NULL;
861 expr->ctype = &bool_ctype;
862 if (expr->flags) {
863 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
864 expr->flags = 0;
866 return &bool_ctype;
869 static struct symbol *evaluate_binop(struct expression *expr)
871 struct symbol *ltype, *rtype, *ctype;
872 int lclass = classify_type(expr->left->ctype, &ltype);
873 int rclass = classify_type(expr->right->ctype, &rtype);
874 int op = expr->op;
876 if (expr->flags) {
877 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
878 expr->flags = 0;
881 /* number op number */
882 if (lclass & rclass & TYPE_NUM) {
883 if ((lclass | rclass) & TYPE_FLOAT) {
884 switch (op) {
885 case '+': case '-': case '*': case '/':
886 break;
887 default:
888 return bad_expr_type(expr);
892 if (op == SPECIAL_LEFTSHIFT || op == SPECIAL_RIGHTSHIFT) {
893 // shifts do integer promotions, but that's it.
894 unrestrict(expr->left, lclass, &ltype);
895 unrestrict(expr->right, rclass, &rtype);
896 ctype = ltype = integer_promotion(ltype);
897 rtype = integer_promotion(rtype);
898 } else {
899 // The rest do usual conversions
900 const unsigned left_not = expr->left->type == EXPR_PREOP
901 && expr->left->op == '!';
902 const unsigned right_not = expr->right->type == EXPR_PREOP
903 && expr->right->op == '!';
904 if ((op == '&' || op == '|') && (left_not || right_not))
905 warning(expr->pos, "dubious: %sx %c %sy",
906 left_not ? "!" : "",
908 right_not ? "!" : "");
910 ltype = usual_conversions(op, expr->left, expr->right,
911 lclass, rclass, ltype, rtype);
912 ctype = rtype = ltype;
915 expr->left = cast_to(expr->left, ltype);
916 expr->right = cast_to(expr->right, rtype);
917 expr->ctype = ctype;
918 return ctype;
921 /* pointer (+|-) integer */
922 if (lclass & TYPE_PTR && is_int(rclass) && (op == '+' || op == '-')) {
923 unrestrict(expr->right, rclass, &rtype);
924 return evaluate_ptr_add(expr, rtype);
927 /* integer + pointer */
928 if (rclass & TYPE_PTR && is_int(lclass) && op == '+') {
929 struct expression *index = expr->left;
930 unrestrict(index, lclass, &ltype);
931 expr->left = expr->right;
932 expr->right = index;
933 return evaluate_ptr_add(expr, ltype);
936 /* pointer - pointer */
937 if (lclass & rclass & TYPE_PTR && expr->op == '-')
938 return evaluate_ptr_sub(expr);
940 return bad_expr_type(expr);
943 static struct symbol *evaluate_comma(struct expression *expr)
945 expr->ctype = degenerate(expr->right);
946 if (expr->ctype == &null_ctype)
947 expr->ctype = &ptr_ctype;
948 expr->flags &= expr->left->flags & expr->right->flags;
949 return expr->ctype;
952 static int modify_for_unsigned(int op)
954 if (op == '<')
955 op = SPECIAL_UNSIGNED_LT;
956 else if (op == '>')
957 op = SPECIAL_UNSIGNED_GT;
958 else if (op == SPECIAL_LTE)
959 op = SPECIAL_UNSIGNED_LTE;
960 else if (op == SPECIAL_GTE)
961 op = SPECIAL_UNSIGNED_GTE;
962 return op;
965 static inline int is_null_pointer_constant(struct expression *e)
967 if (e->ctype == &null_ctype)
968 return 1;
969 if (!(e->flags & Int_const_expr))
970 return 0;
971 return is_zero_constant(e) ? 2 : 0;
974 static struct symbol *evaluate_compare(struct expression *expr)
976 struct expression *left = expr->left, *right = expr->right;
977 struct symbol *ltype, *rtype, *lbase, *rbase;
978 int lclass = classify_type(degenerate(left), &ltype);
979 int rclass = classify_type(degenerate(right), &rtype);
980 struct symbol *ctype;
981 const char *typediff;
983 if (expr->flags) {
984 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
985 expr->flags = 0;
988 /* Type types? */
989 if (is_type_type(ltype) && is_type_type(rtype))
990 goto OK;
992 if (is_safe_type(left->ctype) || is_safe_type(right->ctype))
993 warning(expr->pos, "testing a 'safe expression'");
995 /* number on number */
996 if (lclass & rclass & TYPE_NUM) {
997 ctype = usual_conversions(expr->op, expr->left, expr->right,
998 lclass, rclass, ltype, rtype);
999 expr->left = cast_to(expr->left, ctype);
1000 expr->right = cast_to(expr->right, ctype);
1001 if (ctype->ctype.modifiers & MOD_UNSIGNED)
1002 expr->op = modify_for_unsigned(expr->op);
1003 goto OK;
1006 /* at least one must be a pointer */
1007 if (!((lclass | rclass) & TYPE_PTR))
1008 return bad_expr_type(expr);
1010 /* equality comparisons can be with null pointer constants */
1011 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1012 int is_null1 = is_null_pointer_constant(left);
1013 int is_null2 = is_null_pointer_constant(right);
1014 if (is_null1 == 2)
1015 bad_null(left);
1016 if (is_null2 == 2)
1017 bad_null(right);
1018 if (is_null1 && is_null2) {
1019 int positive = expr->op == SPECIAL_EQUAL;
1020 expr->type = EXPR_VALUE;
1021 expr->value = positive;
1022 goto OK;
1024 if (is_null1 && (rclass & TYPE_PTR)) {
1025 left = cast_to(left, rtype);
1026 goto OK;
1028 if (is_null2 && (lclass & TYPE_PTR)) {
1029 right = cast_to(right, ltype);
1030 goto OK;
1033 /* both should be pointers */
1034 if (!(lclass & rclass & TYPE_PTR))
1035 return bad_expr_type(expr);
1036 expr->op = modify_for_unsigned(expr->op);
1038 lbase = examine_pointer_target(ltype);
1039 rbase = examine_pointer_target(rtype);
1041 /* they also have special treatment for pointers to void */
1042 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1043 if (ltype->ctype.as == rtype->ctype.as) {
1044 if (lbase == &void_ctype) {
1045 right = cast_to(right, ltype);
1046 goto OK;
1048 if (rbase == &void_ctype) {
1049 left = cast_to(left, rtype);
1050 goto OK;
1055 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1056 target_qualifiers(rtype),
1057 target_qualifiers(ltype));
1058 if (!typediff)
1059 goto OK;
1061 expression_error(expr, "incompatible types in comparison expression (%s)", typediff);
1062 return NULL;
1065 expr->ctype = &bool_ctype;
1066 return &bool_ctype;
1070 * NOTE! The degenerate case of "x ? : y", where we don't
1071 * have a true case, this will possibly promote "x" to the
1072 * same type as "y", and thus _change_ the conditional
1073 * test in the expression. But since promotion is "safe"
1074 * for testing, that's OK.
1076 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1078 struct expression **true;
1079 struct symbol *ctype, *ltype, *rtype, *lbase, *rbase;
1080 int lclass, rclass;
1081 const char * typediff;
1082 int qual;
1084 if (!evaluate_conditional(expr->conditional, 0))
1085 return NULL;
1086 if (!evaluate_expression(expr->cond_false))
1087 return NULL;
1089 ctype = degenerate(expr->conditional);
1090 rtype = degenerate(expr->cond_false);
1092 true = &expr->conditional;
1093 ltype = ctype;
1094 if (expr->cond_true) {
1095 if (!evaluate_expression(expr->cond_true))
1096 return NULL;
1097 ltype = degenerate(expr->cond_true);
1098 true = &expr->cond_true;
1101 if (expr->flags) {
1102 int flags = expr->conditional->flags & Int_const_expr;
1103 flags &= (*true)->flags & expr->cond_false->flags;
1104 if (!flags)
1105 expr->flags = 0;
1108 lclass = classify_type(ltype, &ltype);
1109 rclass = classify_type(rtype, &rtype);
1110 if (lclass & rclass & TYPE_NUM) {
1111 ctype = usual_conversions('?', *true, expr->cond_false,
1112 lclass, rclass, ltype, rtype);
1113 *true = cast_to(*true, ctype);
1114 expr->cond_false = cast_to(expr->cond_false, ctype);
1115 goto out;
1118 if ((lclass | rclass) & TYPE_PTR) {
1119 int is_null1 = is_null_pointer_constant(*true);
1120 int is_null2 = is_null_pointer_constant(expr->cond_false);
1122 if (is_null1 && is_null2) {
1123 *true = cast_to(*true, &ptr_ctype);
1124 expr->cond_false = cast_to(expr->cond_false, &ptr_ctype);
1125 ctype = &ptr_ctype;
1126 goto out;
1128 if (is_null1 && (rclass & TYPE_PTR)) {
1129 if (is_null1 == 2)
1130 bad_null(*true);
1131 *true = cast_to(*true, rtype);
1132 ctype = rtype;
1133 goto out;
1135 if (is_null2 && (lclass & TYPE_PTR)) {
1136 if (is_null2 == 2)
1137 bad_null(expr->cond_false);
1138 expr->cond_false = cast_to(expr->cond_false, ltype);
1139 ctype = ltype;
1140 goto out;
1142 if (!(lclass & rclass & TYPE_PTR)) {
1143 typediff = "different types";
1144 goto Err;
1146 /* OK, it's pointer on pointer */
1147 if (ltype->ctype.as != rtype->ctype.as) {
1148 typediff = "different address spaces";
1149 goto Err;
1152 /* need to be lazier here */
1153 lbase = examine_pointer_target(ltype);
1154 rbase = examine_pointer_target(rtype);
1155 qual = target_qualifiers(ltype) | target_qualifiers(rtype);
1157 if (lbase == &void_ctype) {
1158 /* XXX: pointers to function should warn here */
1159 ctype = ltype;
1160 goto Qual;
1163 if (rbase == &void_ctype) {
1164 /* XXX: pointers to function should warn here */
1165 ctype = rtype;
1166 goto Qual;
1168 /* XXX: that should be pointer to composite */
1169 ctype = ltype;
1170 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1171 qual, qual);
1172 if (!typediff)
1173 goto Qual;
1174 goto Err;
1177 /* void on void, struct on same struct, union on same union */
1178 if (ltype == rtype) {
1179 ctype = ltype;
1180 goto out;
1182 typediff = "different base types";
1184 Err:
1185 expression_error(expr, "incompatible types in conditional expression (%s)", typediff);
1186 return NULL;
1188 out:
1189 expr->ctype = ctype;
1190 return ctype;
1192 Qual:
1193 if (qual & ~ctype->ctype.modifiers) {
1194 struct symbol *sym = alloc_symbol(ctype->pos, SYM_PTR);
1195 *sym = *ctype;
1196 sym->ctype.modifiers |= qual;
1197 ctype = sym;
1199 *true = cast_to(*true, ctype);
1200 expr->cond_false = cast_to(expr->cond_false, ctype);
1201 goto out;
1204 /* FP assignments can not do modulo or bit operations */
1205 static int compatible_float_op(int op)
1207 return op == SPECIAL_ADD_ASSIGN ||
1208 op == SPECIAL_SUB_ASSIGN ||
1209 op == SPECIAL_MUL_ASSIGN ||
1210 op == SPECIAL_DIV_ASSIGN;
1213 static int evaluate_assign_op(struct expression *expr)
1215 struct symbol *target = expr->left->ctype;
1216 struct symbol *source = expr->right->ctype;
1217 struct symbol *t, *s;
1218 int tclass = classify_type(target, &t);
1219 int sclass = classify_type(source, &s);
1220 int op = expr->op;
1222 if (tclass & sclass & TYPE_NUM) {
1223 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1224 expression_error(expr, "invalid assignment");
1225 return 0;
1227 if (tclass & TYPE_RESTRICT) {
1228 if (!restricted_binop(op, t)) {
1229 warning(expr->pos, "bad assignment (%s) to %s",
1230 show_special(op), show_typename(t));
1231 expr->right = cast_to(expr->right, target);
1232 return 0;
1234 /* allowed assignments unfoul */
1235 if (sclass & TYPE_FOULED && unfoul(s) == t)
1236 goto Cast;
1237 if (!restricted_value(expr->right, t))
1238 return 1;
1239 } else if (!(sclass & TYPE_RESTRICT))
1240 goto Cast;
1241 /* source and target would better be identical restricted */
1242 if (t == s)
1243 return 1;
1244 warning(expr->pos, "invalid assignment: %s", show_special(op));
1245 info(expr->pos, " left side has type %s", show_typename(t));
1246 info(expr->pos, " right side has type %s", show_typename(s));
1247 expr->right = cast_to(expr->right, target);
1248 return 0;
1250 if (tclass == TYPE_PTR && is_int(sclass)) {
1251 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1252 unrestrict(expr->right, sclass, &s);
1253 evaluate_ptr_add(expr, s);
1254 return 1;
1256 expression_error(expr, "invalid pointer assignment");
1257 return 0;
1260 expression_error(expr, "invalid assignment");
1261 return 0;
1263 Cast:
1264 expr->right = cast_to(expr->right, target);
1265 return 1;
1268 static int whitelist_pointers(struct symbol *t1, struct symbol *t2)
1270 if (t1 == t2)
1271 return 0; /* yes, 0 - we don't want a cast_to here */
1272 if (t1 == &void_ctype)
1273 return 1;
1274 if (t2 == &void_ctype)
1275 return 1;
1276 if (classify_type(t1, &t1) != TYPE_NUM)
1277 return 0;
1278 if (classify_type(t2, &t2) != TYPE_NUM)
1279 return 0;
1280 if (t1 == t2)
1281 return 1;
1282 if (t1->ctype.modifiers & t2->ctype.modifiers & MOD_CHAR)
1283 return 1;
1284 if ((t1->ctype.modifiers ^ t2->ctype.modifiers) & MOD_SIZE)
1285 return 0;
1286 return !Wtypesign;
1289 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1290 struct expression **rp, const char *where)
1292 const char *typediff;
1293 struct symbol *source = degenerate(*rp);
1294 struct symbol *t, *s;
1295 int tclass = classify_type(target, &t);
1296 int sclass = classify_type(source, &s);
1298 if (tclass & sclass & TYPE_NUM) {
1299 if (tclass & TYPE_RESTRICT) {
1300 /* allowed assignments unfoul */
1301 if (sclass & TYPE_FOULED && unfoul(s) == t)
1302 goto Cast;
1303 if (!restricted_value(*rp, target))
1304 return 1;
1305 if (s == t)
1306 return 1;
1307 } else if (!(sclass & TYPE_RESTRICT))
1308 goto Cast;
1309 typediff = "different base types";
1310 goto Err;
1313 if (tclass == TYPE_PTR) {
1314 unsigned long mod1, mod2;
1315 struct symbol *b1, *b2;
1316 // NULL pointer is always OK
1317 int is_null = is_null_pointer_constant(*rp);
1318 if (is_null) {
1319 if (is_null == 2)
1320 bad_null(*rp);
1321 goto Cast;
1323 if (!(sclass & TYPE_PTR)) {
1324 typediff = "different base types";
1325 goto Err;
1327 b1 = examine_pointer_target(t);
1328 b2 = examine_pointer_target(s);
1329 mod1 = target_qualifiers(t);
1330 mod2 = target_qualifiers(s);
1331 if (whitelist_pointers(b1, b2)) {
1333 * assignments to/from void * are OK, provided that
1334 * we do not remove qualifiers from pointed to [C]
1335 * or mix address spaces [sparse].
1337 if (t->ctype.as != s->ctype.as) {
1338 typediff = "different address spaces";
1339 goto Err;
1341 if (mod2 & ~mod1) {
1342 typediff = "different modifiers";
1343 goto Err;
1345 goto Cast;
1347 /* It's OK if the target is more volatile or const than the source */
1348 typediff = type_difference(&t->ctype, &s->ctype, 0, mod1);
1349 if (typediff)
1350 goto Err;
1351 return 1;
1354 if ((tclass & TYPE_COMPOUND) && s == t)
1355 return 1;
1357 if (tclass & TYPE_NUM) {
1358 /* XXX: need to turn into comparison with NULL */
1359 if (t == &bool_ctype && (sclass & TYPE_PTR))
1360 goto Cast;
1361 typediff = "different base types";
1362 goto Err;
1364 typediff = "invalid types";
1366 Err:
1367 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1368 info(expr->pos, " expected %s", show_typename(target));
1369 info(expr->pos, " got %s", show_typename(source));
1370 *rp = cast_to(*rp, target);
1371 return 0;
1372 Cast:
1373 *rp = cast_to(*rp, target);
1374 return 1;
1377 static void mark_assigned(struct expression *expr)
1379 struct symbol *sym;
1381 if (!expr)
1382 return;
1383 switch (expr->type) {
1384 case EXPR_SYMBOL:
1385 sym = expr->symbol;
1386 if (!sym)
1387 return;
1388 if (sym->type != SYM_NODE)
1389 return;
1390 sym->ctype.modifiers |= MOD_ASSIGNED;
1391 return;
1393 case EXPR_BINOP:
1394 mark_assigned(expr->left);
1395 mark_assigned(expr->right);
1396 return;
1397 case EXPR_CAST:
1398 case EXPR_FORCE_CAST:
1399 mark_assigned(expr->cast_expression);
1400 return;
1401 case EXPR_SLICE:
1402 mark_assigned(expr->base);
1403 return;
1404 default:
1405 /* Hmm? */
1406 return;
1410 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1412 if (type->ctype.modifiers & MOD_CONST)
1413 expression_error(left, "assignment to const expression");
1415 /* We know left is an lvalue, so it's a "preop-*" */
1416 mark_assigned(left->unop);
1419 static struct symbol *evaluate_assignment(struct expression *expr)
1421 struct expression *left = expr->left;
1422 struct expression *where = expr;
1423 struct symbol *ltype;
1425 if (!lvalue_expression(left)) {
1426 expression_error(expr, "not an lvalue");
1427 return NULL;
1430 ltype = left->ctype;
1432 if (expr->op != '=') {
1433 if (!evaluate_assign_op(expr))
1434 return NULL;
1435 } else {
1436 if (!compatible_assignment_types(where, ltype, &expr->right, "assignment"))
1437 return NULL;
1440 evaluate_assign_to(left, ltype);
1442 expr->ctype = ltype;
1443 return ltype;
1446 static void examine_fn_arguments(struct symbol *fn)
1448 struct symbol *s;
1450 FOR_EACH_PTR(fn->arguments, s) {
1451 struct symbol *arg = evaluate_symbol(s);
1452 /* Array/function arguments silently degenerate into pointers */
1453 if (arg) {
1454 struct symbol *ptr;
1455 switch(arg->type) {
1456 case SYM_ARRAY:
1457 case SYM_FN:
1458 ptr = alloc_symbol(s->pos, SYM_PTR);
1459 if (arg->type == SYM_ARRAY)
1460 ptr->ctype = arg->ctype;
1461 else
1462 ptr->ctype.base_type = arg;
1463 ptr->ctype.as |= s->ctype.as;
1464 ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1466 s->ctype.base_type = ptr;
1467 s->ctype.as = 0;
1468 s->ctype.modifiers &= ~MOD_PTRINHERIT;
1469 s->bit_size = 0;
1470 s->examined = 0;
1471 examine_symbol_type(s);
1472 break;
1473 default:
1474 /* nothing */
1475 break;
1478 } END_FOR_EACH_PTR(s);
1481 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1483 /* Take the modifiers of the pointer, and apply them to the member */
1484 mod |= sym->ctype.modifiers;
1485 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1486 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1487 *newsym = *sym;
1488 newsym->ctype.as = as;
1489 newsym->ctype.modifiers = mod;
1490 sym = newsym;
1492 return sym;
1495 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1497 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1498 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1500 node->ctype.base_type = ptr;
1501 ptr->bit_size = bits_in_pointer;
1502 ptr->ctype.alignment = pointer_alignment;
1504 node->bit_size = bits_in_pointer;
1505 node->ctype.alignment = pointer_alignment;
1507 access_symbol(sym);
1508 if (sym->ctype.modifiers & MOD_REGISTER) {
1509 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1510 sym->ctype.modifiers &= ~MOD_REGISTER;
1512 if (sym->type == SYM_NODE) {
1513 ptr->ctype.as |= sym->ctype.as;
1514 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1515 sym = sym->ctype.base_type;
1517 if (degenerate && sym->type == SYM_ARRAY) {
1518 ptr->ctype.as |= sym->ctype.as;
1519 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1520 sym = sym->ctype.base_type;
1522 ptr->ctype.base_type = sym;
1524 return node;
1527 /* Arrays degenerate into pointers on pointer arithmetic */
1528 static struct symbol *degenerate(struct expression *expr)
1530 struct symbol *ctype, *base;
1532 if (!expr)
1533 return NULL;
1534 ctype = expr->ctype;
1535 if (!ctype)
1536 return NULL;
1537 base = examine_symbol_type(ctype);
1538 if (ctype->type == SYM_NODE)
1539 base = ctype->ctype.base_type;
1541 * Arrays degenerate into pointers to the entries, while
1542 * functions degenerate into pointers to themselves.
1543 * If array was part of non-lvalue compound, we create a copy
1544 * of that compound first and then act as if we were dealing with
1545 * the corresponding field in there.
1547 switch (base->type) {
1548 case SYM_ARRAY:
1549 if (expr->type == EXPR_SLICE) {
1550 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1551 struct expression *e0, *e1, *e2, *e3, *e4;
1553 a->ctype.base_type = expr->base->ctype;
1554 a->bit_size = expr->base->ctype->bit_size;
1555 a->array_size = expr->base->ctype->array_size;
1557 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1558 e0->symbol = a;
1559 e0->ctype = &lazy_ptr_ctype;
1561 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1562 e1->unop = e0;
1563 e1->op = '*';
1564 e1->ctype = expr->base->ctype; /* XXX */
1566 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1567 e2->left = e1;
1568 e2->right = expr->base;
1569 e2->op = '=';
1570 e2->ctype = expr->base->ctype;
1572 if (expr->r_bitpos) {
1573 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1574 e3->op = '+';
1575 e3->left = e0;
1576 e3->right = alloc_const_expression(expr->pos,
1577 bits_to_bytes(expr->r_bitpos));
1578 e3->ctype = &lazy_ptr_ctype;
1579 } else {
1580 e3 = e0;
1583 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1584 e4->left = e2;
1585 e4->right = e3;
1586 e4->ctype = &lazy_ptr_ctype;
1588 expr->unop = e4;
1589 expr->type = EXPR_PREOP;
1590 expr->op = '*';
1592 case SYM_FN:
1593 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1594 expression_error(expr, "strange non-value function or array");
1595 return &bad_ctype;
1597 *expr = *expr->unop;
1598 ctype = create_pointer(expr, ctype, 1);
1599 expr->ctype = ctype;
1600 default:
1601 /* nothing */;
1603 return ctype;
1606 static struct symbol *evaluate_addressof(struct expression *expr)
1608 struct expression *op = expr->unop;
1609 struct symbol *ctype;
1611 if (op->op != '*' || op->type != EXPR_PREOP) {
1612 expression_error(expr, "not addressable");
1613 return NULL;
1615 ctype = op->ctype;
1616 *expr = *op->unop;
1617 expr->flags = 0;
1619 if (expr->type == EXPR_SYMBOL) {
1620 struct symbol *sym = expr->symbol;
1621 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1625 * symbol expression evaluation is lazy about the type
1626 * of the sub-expression, so we may have to generate
1627 * the type here if so..
1629 if (expr->ctype == &lazy_ptr_ctype) {
1630 ctype = create_pointer(expr, ctype, 0);
1631 expr->ctype = ctype;
1633 return expr->ctype;
1637 static struct symbol *evaluate_dereference(struct expression *expr)
1639 struct expression *op = expr->unop;
1640 struct symbol *ctype = op->ctype, *node, *target;
1642 /* Simplify: *&(expr) => (expr) */
1643 if (op->type == EXPR_PREOP && op->op == '&') {
1644 *expr = *op->unop;
1645 expr->flags = 0;
1646 return expr->ctype;
1649 /* Dereferencing a node drops all the node information. */
1650 if (ctype->type == SYM_NODE)
1651 ctype = ctype->ctype.base_type;
1653 node = alloc_symbol(expr->pos, SYM_NODE);
1654 target = ctype->ctype.base_type;
1656 switch (ctype->type) {
1657 default:
1658 expression_error(expr, "cannot dereference this type");
1659 return NULL;
1660 case SYM_PTR:
1661 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1662 merge_type(node, ctype);
1663 break;
1665 case SYM_ARRAY:
1666 if (!lvalue_expression(op)) {
1667 expression_error(op, "non-lvalue array??");
1668 return NULL;
1671 /* Do the implied "addressof" on the array */
1672 *op = *op->unop;
1675 * When an array is dereferenced, we need to pick
1676 * up the attributes of the original node too..
1678 merge_type(node, op->ctype);
1679 merge_type(node, ctype);
1680 break;
1683 node->bit_size = target->bit_size;
1684 node->array_size = target->array_size;
1686 expr->ctype = node;
1687 return node;
1691 * Unary post-ops: x++ and x--
1693 static struct symbol *evaluate_postop(struct expression *expr)
1695 struct expression *op = expr->unop;
1696 struct symbol *ctype = op->ctype;
1697 int class = classify_type(op->ctype, &ctype);
1698 int multiply = 0;
1700 if (!lvalue_expression(expr->unop)) {
1701 expression_error(expr, "need lvalue expression for ++/--");
1702 return NULL;
1705 if ((class & TYPE_RESTRICT) && restricted_unop(expr->op, &ctype))
1706 return bad_expr_type(expr);
1708 if (class & TYPE_NUM) {
1709 multiply = 1;
1710 } else if (class == TYPE_PTR) {
1711 struct symbol *target = examine_pointer_target(ctype);
1712 if (!is_function(target))
1713 multiply = bits_to_bytes(target->bit_size);
1716 if (multiply) {
1717 evaluate_assign_to(op, op->ctype);
1718 expr->op_value = multiply;
1719 expr->ctype = ctype;
1720 return ctype;
1723 expression_error(expr, "bad argument type for ++/--");
1724 return NULL;
1727 static struct symbol *evaluate_sign(struct expression *expr)
1729 struct symbol *ctype = expr->unop->ctype;
1730 int class = classify_type(ctype, &ctype);
1731 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1732 expr->flags = 0;
1733 /* should be an arithmetic type */
1734 if (!(class & TYPE_NUM))
1735 return bad_expr_type(expr);
1736 if (!(class & (TYPE_FLOAT|TYPE_RESTRICT))) {
1737 struct symbol *rtype = integer_promotion(ctype);
1738 expr->unop = cast_to(expr->unop, rtype);
1739 ctype = rtype;
1740 } else if ((class & TYPE_FLOAT) && expr->op != '~') {
1741 /* no conversions needed */
1742 } else if ((class & TYPE_RESTRICT) && !restricted_unop(expr->op, &ctype)) {
1743 /* no conversions needed */
1744 } else {
1745 return bad_expr_type(expr);
1747 if (expr->op == '+')
1748 *expr = *expr->unop;
1749 expr->ctype = ctype;
1750 return ctype;
1753 static struct symbol *evaluate_preop(struct expression *expr)
1755 struct symbol *ctype = expr->unop->ctype;
1757 switch (expr->op) {
1758 case '(':
1759 *expr = *expr->unop;
1760 return ctype;
1762 case '+':
1763 case '-':
1764 case '~':
1765 return evaluate_sign(expr);
1767 case '*':
1768 return evaluate_dereference(expr);
1770 case '&':
1771 return evaluate_addressof(expr);
1773 case SPECIAL_INCREMENT:
1774 case SPECIAL_DECREMENT:
1776 * From a type evaluation standpoint the preops are
1777 * the same as the postops
1779 return evaluate_postop(expr);
1781 case '!':
1782 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1783 expr->flags = 0;
1784 if (is_safe_type(ctype))
1785 warning(expr->pos, "testing a 'safe expression'");
1786 if (is_float_type(ctype)) {
1787 struct expression *arg = expr->unop;
1788 expr->type = EXPR_BINOP;
1789 expr->op = SPECIAL_EQUAL;
1790 expr->left = arg;
1791 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1792 expr->right->ctype = ctype;
1793 expr->right->fvalue = 0;
1794 } else if (is_fouled_type(ctype)) {
1795 warning(expr->pos, "%s degrades to integer",
1796 show_typename(ctype->ctype.base_type));
1798 ctype = &bool_ctype;
1799 break;
1801 default:
1802 break;
1804 expr->ctype = ctype;
1805 return &bool_ctype;
1808 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1810 struct ptr_list *head = (struct ptr_list *)_list;
1811 struct ptr_list *list = head;
1813 if (!head)
1814 return NULL;
1815 do {
1816 int i;
1817 for (i = 0; i < list->nr; i++) {
1818 struct symbol *sym = (struct symbol *) list->list[i];
1819 if (sym->ident) {
1820 if (sym->ident != ident)
1821 continue;
1822 *offset = sym->offset;
1823 return sym;
1824 } else {
1825 struct symbol *ctype = sym->ctype.base_type;
1826 struct symbol *sub;
1827 if (!ctype)
1828 continue;
1829 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1830 continue;
1831 sub = find_identifier(ident, ctype->symbol_list, offset);
1832 if (!sub)
1833 continue;
1834 *offset += sym->offset;
1835 return sub;
1838 } while ((list = list->next) != head);
1839 return NULL;
1842 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1844 struct expression *add;
1847 * Create a new add-expression
1849 * NOTE! Even if we just add zero, we need a new node
1850 * for the member pointer, since it has a different
1851 * type than the original pointer. We could make that
1852 * be just a cast, but the fact is, a node is a node,
1853 * so we might as well just do the "add zero" here.
1855 add = alloc_expression(expr->pos, EXPR_BINOP);
1856 add->op = '+';
1857 add->left = expr;
1858 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1859 add->right->ctype = &int_ctype;
1860 add->right->value = offset;
1863 * The ctype of the pointer will be lazily evaluated if
1864 * we ever take the address of this member dereference..
1866 add->ctype = &lazy_ptr_ctype;
1867 return add;
1870 /* structure/union dereference */
1871 static struct symbol *evaluate_member_dereference(struct expression *expr)
1873 int offset;
1874 struct symbol *ctype, *member;
1875 struct expression *deref = expr->deref, *add;
1876 struct ident *ident = expr->member;
1877 unsigned int mod;
1878 int address_space;
1880 if (!evaluate_expression(deref))
1881 return NULL;
1882 if (!ident) {
1883 expression_error(expr, "bad member name");
1884 return NULL;
1887 ctype = deref->ctype;
1888 examine_symbol_type(ctype);
1889 address_space = ctype->ctype.as;
1890 mod = ctype->ctype.modifiers;
1891 if (ctype->type == SYM_NODE) {
1892 ctype = ctype->ctype.base_type;
1893 address_space |= ctype->ctype.as;
1894 mod |= ctype->ctype.modifiers;
1896 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1897 expression_error(expr, "expected structure or union");
1898 return NULL;
1900 offset = 0;
1901 member = find_identifier(ident, ctype->symbol_list, &offset);
1902 if (!member) {
1903 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1904 const char *name = "<unnamed>";
1905 int namelen = 9;
1906 if (ctype->ident) {
1907 name = ctype->ident->name;
1908 namelen = ctype->ident->len;
1910 if (ctype->symbol_list)
1911 expression_error(expr, "no member '%s' in %s %.*s",
1912 show_ident(ident), type, namelen, name);
1913 else
1914 expression_error(expr, "using member '%s' in "
1915 "incomplete %s %.*s", show_ident(ident),
1916 type, namelen, name);
1917 return NULL;
1921 * The member needs to take on the address space and modifiers of
1922 * the "parent" type.
1924 member = convert_to_as_mod(member, address_space, mod);
1925 ctype = get_base_type(member);
1927 if (!lvalue_expression(deref)) {
1928 if (deref->type != EXPR_SLICE) {
1929 expr->base = deref;
1930 expr->r_bitpos = 0;
1931 } else {
1932 expr->base = deref->base;
1933 expr->r_bitpos = deref->r_bitpos;
1935 expr->r_bitpos += bytes_to_bits(offset);
1936 expr->type = EXPR_SLICE;
1937 expr->r_nrbits = member->bit_size;
1938 expr->r_bitpos += member->bit_offset;
1939 expr->ctype = member;
1940 return member;
1943 deref = deref->unop;
1944 expr->deref = deref;
1946 add = evaluate_offset(deref, offset);
1947 expr->type = EXPR_PREOP;
1948 expr->op = '*';
1949 expr->unop = add;
1951 expr->ctype = member;
1952 return member;
1955 static int is_promoted(struct expression *expr)
1957 while (1) {
1958 switch (expr->type) {
1959 case EXPR_BINOP:
1960 case EXPR_SELECT:
1961 case EXPR_CONDITIONAL:
1962 return 1;
1963 case EXPR_COMMA:
1964 expr = expr->right;
1965 continue;
1966 case EXPR_PREOP:
1967 switch (expr->op) {
1968 case '(':
1969 expr = expr->unop;
1970 continue;
1971 case '+':
1972 case '-':
1973 case '~':
1974 return 1;
1975 default:
1976 return 0;
1978 default:
1979 return 0;
1985 static struct symbol *evaluate_cast(struct expression *);
1987 static struct symbol *evaluate_type_information(struct expression *expr)
1989 struct symbol *sym = expr->cast_type;
1990 if (!sym) {
1991 sym = evaluate_expression(expr->cast_expression);
1992 if (!sym)
1993 return NULL;
1995 * Expressions of restricted types will possibly get
1996 * promoted - check that here
1998 if (is_restricted_type(sym)) {
1999 if (sym->bit_size < bits_in_int && is_promoted(expr))
2000 sym = &int_ctype;
2001 } else if (is_fouled_type(sym)) {
2002 sym = &int_ctype;
2005 examine_symbol_type(sym);
2006 if (is_bitfield_type(sym)) {
2007 expression_error(expr, "trying to examine bitfield type");
2008 return NULL;
2010 return sym;
2013 static struct symbol *evaluate_sizeof(struct expression *expr)
2015 struct symbol *type;
2016 int size;
2018 type = evaluate_type_information(expr);
2019 if (!type)
2020 return NULL;
2022 size = type->bit_size;
2024 if (size < 0 && is_void_type(type)) {
2025 warning(expr->pos, "expression using sizeof(void)");
2026 size = bits_in_char;
2029 if (is_function(type->ctype.base_type)) {
2030 warning(expr->pos, "expression using sizeof on a function");
2031 size = bits_in_char;
2034 if ((size < 0) || (size & (bits_in_char - 1)))
2035 expression_error(expr, "cannot size expression");
2037 expr->type = EXPR_VALUE;
2038 expr->value = bits_to_bytes(size);
2039 expr->taint = 0;
2040 expr->ctype = size_t_ctype;
2041 return size_t_ctype;
2044 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
2046 struct symbol *type;
2047 int size;
2049 type = evaluate_type_information(expr);
2050 if (!type)
2051 return NULL;
2053 if (type->type == SYM_NODE)
2054 type = type->ctype.base_type;
2055 if (!type)
2056 return NULL;
2057 switch (type->type) {
2058 case SYM_ARRAY:
2059 break;
2060 case SYM_PTR:
2061 type = get_base_type(type);
2062 if (type)
2063 break;
2064 default:
2065 expression_error(expr, "expected pointer expression");
2066 return NULL;
2068 size = type->bit_size;
2069 if (size & (bits_in_char-1))
2070 size = 0;
2071 expr->type = EXPR_VALUE;
2072 expr->value = bits_to_bytes(size);
2073 expr->taint = 0;
2074 expr->ctype = size_t_ctype;
2075 return size_t_ctype;
2078 static struct symbol *evaluate_alignof(struct expression *expr)
2080 struct symbol *type;
2082 type = evaluate_type_information(expr);
2083 if (!type)
2084 return NULL;
2086 expr->type = EXPR_VALUE;
2087 expr->value = type->ctype.alignment;
2088 expr->taint = 0;
2089 expr->ctype = size_t_ctype;
2090 return size_t_ctype;
2093 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
2095 struct expression *expr;
2096 struct symbol_list *argument_types = fn->arguments;
2097 struct symbol *argtype;
2098 int i = 1;
2100 PREPARE_PTR_LIST(argument_types, argtype);
2101 FOR_EACH_PTR (head, expr) {
2102 struct expression **p = THIS_ADDRESS(expr);
2103 struct symbol *ctype, *target;
2104 ctype = evaluate_expression(expr);
2106 if (!ctype)
2107 return 0;
2109 target = argtype;
2110 if (!target) {
2111 struct symbol *type;
2112 int class = classify_type(ctype, &type);
2113 if (is_int(class)) {
2114 *p = cast_to(expr, integer_promotion(type));
2115 } else if (class & TYPE_FLOAT) {
2116 unsigned long mod = type->ctype.modifiers;
2117 if (!(mod & (MOD_LONG_ALL)))
2118 *p = cast_to(expr, &double_ctype);
2119 } else if (class & TYPE_PTR) {
2120 if (expr->ctype == &null_ctype)
2121 *p = cast_to(expr, &ptr_ctype);
2122 else
2123 degenerate(expr);
2125 } else {
2126 static char where[30];
2127 examine_symbol_type(target);
2128 sprintf(where, "argument %d", i);
2129 compatible_assignment_types(expr, target, p, where);
2132 i++;
2133 NEXT_PTR_LIST(argtype);
2134 } END_FOR_EACH_PTR(expr);
2135 FINISH_PTR_LIST(argtype);
2136 return 1;
2139 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
2141 struct symbol *sym;
2143 FOR_EACH_PTR(ctype->symbol_list, sym) {
2144 if (sym->ident == ident)
2145 return sym;
2146 } END_FOR_EACH_PTR(sym);
2147 return NULL;
2150 static void convert_index(struct expression *e)
2152 struct expression *child = e->idx_expression;
2153 unsigned from = e->idx_from;
2154 unsigned to = e->idx_to + 1;
2155 e->type = EXPR_POS;
2156 e->init_offset = from * bits_to_bytes(e->ctype->bit_size);
2157 e->init_nr = to - from;
2158 e->init_expr = child;
2161 static void convert_ident(struct expression *e)
2163 struct expression *child = e->ident_expression;
2164 struct symbol *sym = e->field;
2165 e->type = EXPR_POS;
2166 e->init_offset = sym->offset;
2167 e->init_nr = 1;
2168 e->init_expr = child;
2171 static void convert_designators(struct expression *e)
2173 while (e) {
2174 if (e->type == EXPR_INDEX)
2175 convert_index(e);
2176 else if (e->type == EXPR_IDENTIFIER)
2177 convert_ident(e);
2178 else
2179 break;
2180 e = e->init_expr;
2184 static void excess(struct expression *e, const char *s)
2186 warning(e->pos, "excessive elements in %s initializer", s);
2190 * implicit designator for the first element
2192 static struct expression *first_subobject(struct symbol *ctype, int class,
2193 struct expression **v)
2195 struct expression *e = *v, *new;
2197 if (ctype->type == SYM_NODE)
2198 ctype = ctype->ctype.base_type;
2200 if (class & TYPE_PTR) { /* array */
2201 if (!ctype->bit_size)
2202 return NULL;
2203 new = alloc_expression(e->pos, EXPR_INDEX);
2204 new->idx_expression = e;
2205 new->ctype = ctype->ctype.base_type;
2206 } else {
2207 struct symbol *field, *p;
2208 PREPARE_PTR_LIST(ctype->symbol_list, p);
2209 while (p && !p->ident && is_bitfield_type(p))
2210 NEXT_PTR_LIST(p);
2211 field = p;
2212 FINISH_PTR_LIST(p);
2213 if (!field)
2214 return NULL;
2215 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2216 new->ident_expression = e;
2217 new->field = new->ctype = field;
2219 *v = new;
2220 return new;
2224 * sanity-check explicit designators; return the innermost one or NULL
2225 * in case of error. Assign types.
2227 static struct expression *check_designators(struct expression *e,
2228 struct symbol *ctype)
2230 struct expression *last = NULL;
2231 const char *err;
2232 while (1) {
2233 if (ctype->type == SYM_NODE)
2234 ctype = ctype->ctype.base_type;
2235 if (e->type == EXPR_INDEX) {
2236 struct symbol *type;
2237 if (ctype->type != SYM_ARRAY) {
2238 err = "array index in non-array";
2239 break;
2241 type = ctype->ctype.base_type;
2242 if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2243 unsigned offset = e->idx_to * type->bit_size;
2244 if (offset >= ctype->bit_size) {
2245 err = "index out of bounds in";
2246 break;
2249 e->ctype = ctype = type;
2250 ctype = type;
2251 last = e;
2252 if (!e->idx_expression) {
2253 err = "invalid";
2254 break;
2256 e = e->idx_expression;
2257 } else if (e->type == EXPR_IDENTIFIER) {
2258 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2259 err = "field name not in struct or union";
2260 break;
2262 ctype = find_struct_ident(ctype, e->expr_ident);
2263 if (!ctype) {
2264 err = "unknown field name in";
2265 break;
2267 e->field = e->ctype = ctype;
2268 last = e;
2269 if (!e->ident_expression) {
2270 err = "invalid";
2271 break;
2273 e = e->ident_expression;
2274 } else if (e->type == EXPR_POS) {
2275 err = "internal front-end error: EXPR_POS in";
2276 break;
2277 } else
2278 return last;
2280 expression_error(e, "%s initializer", err);
2281 return NULL;
2285 * choose the next subobject to initialize.
2287 * Get designators for next element, switch old ones to EXPR_POS.
2288 * Return the resulting expression or NULL if we'd run out of subobjects.
2289 * The innermost designator is returned in *v. Designators in old
2290 * are assumed to be already sanity-checked.
2292 static struct expression *next_designators(struct expression *old,
2293 struct symbol *ctype,
2294 struct expression *e, struct expression **v)
2296 struct expression *new = NULL;
2298 if (!old)
2299 return NULL;
2300 if (old->type == EXPR_INDEX) {
2301 struct expression *copy;
2302 unsigned n;
2304 copy = next_designators(old->idx_expression,
2305 old->ctype, e, v);
2306 if (!copy) {
2307 n = old->idx_to + 1;
2308 if (n * old->ctype->bit_size == ctype->bit_size) {
2309 convert_index(old);
2310 return NULL;
2312 copy = e;
2313 *v = new = alloc_expression(e->pos, EXPR_INDEX);
2314 } else {
2315 n = old->idx_to;
2316 new = alloc_expression(e->pos, EXPR_INDEX);
2319 new->idx_from = new->idx_to = n;
2320 new->idx_expression = copy;
2321 new->ctype = old->ctype;
2322 convert_index(old);
2323 } else if (old->type == EXPR_IDENTIFIER) {
2324 struct expression *copy;
2325 struct symbol *field;
2327 copy = next_designators(old->ident_expression,
2328 old->ctype, e, v);
2329 if (!copy) {
2330 field = old->field->next_subobject;
2331 if (!field) {
2332 convert_ident(old);
2333 return NULL;
2335 copy = e;
2336 *v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2337 } else {
2338 field = old->field;
2339 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2342 new->field = field;
2343 new->expr_ident = field->ident;
2344 new->ident_expression = copy;
2345 new->ctype = field;
2346 convert_ident(old);
2348 return new;
2351 static int handle_simple_initializer(struct expression **ep, int nested,
2352 int class, struct symbol *ctype);
2355 * deal with traversing subobjects [6.7.8(17,18,20)]
2357 static void handle_list_initializer(struct expression *expr,
2358 int class, struct symbol *ctype)
2360 struct expression *e, *last = NULL, *top = NULL, *next;
2361 int jumped = 0;
2363 FOR_EACH_PTR(expr->expr_list, e) {
2364 struct expression **v;
2365 struct symbol *type;
2366 int lclass;
2368 if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2369 struct symbol *struct_sym;
2370 if (!top) {
2371 top = e;
2372 last = first_subobject(ctype, class, &top);
2373 } else {
2374 last = next_designators(last, ctype, e, &top);
2376 if (!last) {
2377 excess(e, class & TYPE_PTR ? "array" :
2378 "struct or union");
2379 DELETE_CURRENT_PTR(e);
2380 continue;
2382 struct_sym = ctype->type == SYM_NODE ? ctype->ctype.base_type : ctype;
2383 if (Wdesignated_init && struct_sym->designated_init)
2384 warning(e->pos, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2385 ctype->ident ? "in initializer for " : "",
2386 ctype->ident ? ctype->ident->len : 0,
2387 ctype->ident ? ctype->ident->name : "",
2388 ctype->ident ? ": " : "",
2389 get_type_name(struct_sym->type),
2390 show_ident(struct_sym->ident));
2391 if (jumped) {
2392 warning(e->pos, "advancing past deep designator");
2393 jumped = 0;
2395 REPLACE_CURRENT_PTR(e, last);
2396 } else {
2397 next = check_designators(e, ctype);
2398 if (!next) {
2399 DELETE_CURRENT_PTR(e);
2400 continue;
2402 top = next;
2403 /* deeper than one designator? */
2404 jumped = top != e;
2405 convert_designators(last);
2406 last = e;
2409 found:
2410 lclass = classify_type(top->ctype, &type);
2411 if (top->type == EXPR_INDEX)
2412 v = &top->idx_expression;
2413 else
2414 v = &top->ident_expression;
2416 if (handle_simple_initializer(v, 1, lclass, top->ctype))
2417 continue;
2419 if (!(lclass & TYPE_COMPOUND)) {
2420 warning(e->pos, "bogus scalar initializer");
2421 DELETE_CURRENT_PTR(e);
2422 continue;
2425 next = first_subobject(type, lclass, v);
2426 if (next) {
2427 warning(e->pos, "missing braces around initializer");
2428 top = next;
2429 goto found;
2432 DELETE_CURRENT_PTR(e);
2433 excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2435 } END_FOR_EACH_PTR(e);
2437 convert_designators(last);
2438 expr->ctype = ctype;
2441 static int is_string_literal(struct expression **v)
2443 struct expression *e = *v;
2444 while (e && e->type == EXPR_PREOP && e->op == '(')
2445 e = e->unop;
2446 if (!e || e->type != EXPR_STRING)
2447 return 0;
2448 if (e != *v && Wparen_string)
2449 warning(e->pos,
2450 "array initialized from parenthesized string constant");
2451 *v = e;
2452 return 1;
2456 * We want a normal expression, possibly in one layer of braces. Warn
2457 * if the latter happens inside a list (it's legal, but likely to be
2458 * an effect of screwup). In case of anything not legal, we are definitely
2459 * having an effect of screwup, so just fail and let the caller warn.
2461 static struct expression *handle_scalar(struct expression *e, int nested)
2463 struct expression *v = NULL, *p;
2464 int count = 0;
2466 /* normal case */
2467 if (e->type != EXPR_INITIALIZER)
2468 return e;
2470 FOR_EACH_PTR(e->expr_list, p) {
2471 if (!v)
2472 v = p;
2473 count++;
2474 } END_FOR_EACH_PTR(p);
2475 if (count != 1)
2476 return NULL;
2477 switch(v->type) {
2478 case EXPR_INITIALIZER:
2479 case EXPR_INDEX:
2480 case EXPR_IDENTIFIER:
2481 return NULL;
2482 default:
2483 break;
2485 if (nested)
2486 warning(e->pos, "braces around scalar initializer");
2487 return v;
2491 * deal with the cases that don't care about subobjects:
2492 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2493 * character array <- string literal, possibly in braces [6.7.8(14)]
2494 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2495 * compound type <- initializer list in braces [6.7.8(16)]
2496 * The last one punts to handle_list_initializer() which, in turn will call
2497 * us for individual elements of the list.
2499 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2500 * the lack of support of wide char stuff in general.
2502 * One note: we need to take care not to evaluate a string literal until
2503 * we know that we *will* handle it right here. Otherwise we would screw
2504 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2505 * { "string", ...} - we need to preserve that string literal recognizable
2506 * until we dig into the inner struct.
2508 static int handle_simple_initializer(struct expression **ep, int nested,
2509 int class, struct symbol *ctype)
2511 int is_string = is_string_type(ctype);
2512 struct expression *e = *ep, *p;
2513 struct symbol *type;
2515 if (!e)
2516 return 0;
2518 /* scalar */
2519 if (!(class & TYPE_COMPOUND)) {
2520 e = handle_scalar(e, nested);
2521 if (!e)
2522 return 0;
2523 *ep = e;
2524 if (!evaluate_expression(e))
2525 return 1;
2526 compatible_assignment_types(e, ctype, ep, "initializer");
2527 return 1;
2531 * sublist; either a string, or we dig in; the latter will deal with
2532 * pathologies, so we don't need anything fancy here.
2534 if (e->type == EXPR_INITIALIZER) {
2535 if (is_string) {
2536 struct expression *v = NULL;
2537 int count = 0;
2539 FOR_EACH_PTR(e->expr_list, p) {
2540 if (!v)
2541 v = p;
2542 count++;
2543 } END_FOR_EACH_PTR(p);
2544 if (count == 1 && is_string_literal(&v)) {
2545 *ep = e = v;
2546 goto String;
2549 handle_list_initializer(e, class, ctype);
2550 return 1;
2553 /* string */
2554 if (is_string_literal(&e)) {
2555 /* either we are doing array of char, or we'll have to dig in */
2556 if (is_string) {
2557 *ep = e;
2558 goto String;
2560 return 0;
2562 /* struct or union can be initialized by compatible */
2563 if (class != TYPE_COMPOUND)
2564 return 0;
2565 type = evaluate_expression(e);
2566 if (!type)
2567 return 0;
2568 if (ctype->type == SYM_NODE)
2569 ctype = ctype->ctype.base_type;
2570 if (type->type == SYM_NODE)
2571 type = type->ctype.base_type;
2572 if (ctype == type)
2573 return 1;
2574 return 0;
2576 String:
2577 p = alloc_expression(e->pos, EXPR_STRING);
2578 *p = *e;
2579 type = evaluate_expression(p);
2580 if (ctype->bit_size != -1 &&
2581 ctype->bit_size + bits_in_char < type->bit_size) {
2582 warning(e->pos,
2583 "too long initializer-string for array of char");
2585 *ep = p;
2586 return 1;
2589 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2591 struct symbol *type;
2592 int class = classify_type(ctype, &type);
2593 if (!handle_simple_initializer(ep, 0, class, ctype))
2594 expression_error(*ep, "invalid initializer");
2597 static struct symbol *evaluate_cast(struct expression *expr)
2599 struct expression *target = expr->cast_expression;
2600 struct symbol *ctype;
2601 struct symbol *t1, *t2;
2602 int class1, class2;
2603 int as1 = 0, as2 = 0;
2605 if (!target)
2606 return NULL;
2609 * Special case: a cast can be followed by an
2610 * initializer, in which case we need to pass
2611 * the type value down to that initializer rather
2612 * than trying to evaluate it as an expression
2614 * A more complex case is when the initializer is
2615 * dereferenced as part of a post-fix expression.
2616 * We need to produce an expression that can be dereferenced.
2618 if (target->type == EXPR_INITIALIZER) {
2619 struct symbol *sym = expr->cast_type;
2620 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2622 sym->initializer = target;
2623 evaluate_symbol(sym);
2625 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2626 addr->symbol = sym;
2628 expr->type = EXPR_PREOP;
2629 expr->op = '*';
2630 expr->unop = addr;
2631 expr->ctype = sym;
2633 return sym;
2636 ctype = examine_symbol_type(expr->cast_type);
2637 expr->ctype = ctype;
2638 expr->cast_type = ctype;
2640 evaluate_expression(target);
2641 degenerate(target);
2643 class1 = classify_type(ctype, &t1);
2645 /* cast to non-integer type -> not an integer constant expression */
2646 if (!is_int(class1))
2647 expr->flags = 0;
2648 /* if argument turns out to be not an integer constant expression *and*
2649 it was not a floating literal to start with -> too bad */
2650 else if (expr->flags == Int_const_expr &&
2651 !(target->flags & Int_const_expr))
2652 expr->flags = 0;
2654 * You can always throw a value away by casting to
2655 * "void" - that's an implicit "force". Note that
2656 * the same is _not_ true of "void *".
2658 if (t1 == &void_ctype)
2659 goto out;
2661 if (class1 & (TYPE_COMPOUND | TYPE_FN))
2662 warning(expr->pos, "cast to non-scalar");
2664 t2 = target->ctype;
2665 if (!t2) {
2666 expression_error(expr, "cast from unknown type");
2667 goto out;
2669 class2 = classify_type(t2, &t2);
2671 if (class2 & TYPE_COMPOUND)
2672 warning(expr->pos, "cast from non-scalar");
2674 if (expr->type == EXPR_FORCE_CAST)
2675 goto out;
2677 /* allowed cast unfouls */
2678 if (class2 & TYPE_FOULED)
2679 t2 = unfoul(t2);
2681 if (t1 != t2) {
2682 if (class1 & TYPE_RESTRICT)
2683 warning(expr->pos, "cast to %s",
2684 show_typename(t1));
2685 if (class2 & TYPE_RESTRICT)
2686 warning(expr->pos, "cast from %s",
2687 show_typename(t2));
2690 if (t1 == &ulong_ctype)
2691 as1 = -1;
2692 else if (class1 == TYPE_PTR) {
2693 examine_pointer_target(t1);
2694 as1 = t1->ctype.as;
2697 if (t2 == &ulong_ctype)
2698 as2 = -1;
2699 else if (class2 == TYPE_PTR) {
2700 examine_pointer_target(t2);
2701 as2 = t2->ctype.as;
2704 if (!as1 && as2 > 0)
2705 warning(expr->pos, "cast removes address space of expression");
2706 if (as1 > 0 && as2 > 0 && as1 != as2)
2707 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2708 if (as1 > 0 && !as2 &&
2709 !is_null_pointer_constant(target) && Wcast_to_as)
2710 warning(expr->pos,
2711 "cast adds address space to expression (<asn:%d>)", as1);
2713 if (!(t1->ctype.modifiers & MOD_PTRINHERIT) && class1 == TYPE_PTR &&
2714 !as1 && (target->flags & Int_const_expr)) {
2715 if (t1->ctype.base_type == &void_ctype) {
2716 if (is_zero_constant(target)) {
2717 /* NULL */
2718 expr->type = EXPR_VALUE;
2719 expr->ctype = &null_ctype;
2720 expr->value = 0;
2721 return ctype;
2725 out:
2726 return ctype;
2730 * Evaluate a call expression with a symbol. This
2731 * should expand inline functions, and evaluate
2732 * builtins.
2734 static int evaluate_symbol_call(struct expression *expr)
2736 struct expression *fn = expr->fn;
2737 struct symbol *ctype = fn->ctype;
2739 if (fn->type != EXPR_PREOP)
2740 return 0;
2742 if (ctype->op && ctype->op->evaluate)
2743 return ctype->op->evaluate(expr);
2745 if (ctype->ctype.modifiers & MOD_INLINE) {
2746 int ret;
2747 struct symbol *curr = current_fn;
2749 if (ctype->definition)
2750 ctype = ctype->definition;
2752 current_fn = ctype->ctype.base_type;
2754 ret = inline_function(expr, ctype);
2756 /* restore the old function */
2757 current_fn = curr;
2758 return ret;
2761 return 0;
2764 static struct symbol *evaluate_call(struct expression *expr)
2766 int args, fnargs;
2767 struct symbol *ctype, *sym;
2768 struct expression *fn = expr->fn;
2769 struct expression_list *arglist = expr->args;
2771 if (!evaluate_expression(fn))
2772 return NULL;
2773 sym = ctype = fn->ctype;
2774 if (ctype->type == SYM_NODE)
2775 ctype = ctype->ctype.base_type;
2776 if (ctype->type == SYM_PTR)
2777 ctype = get_base_type(ctype);
2779 if (ctype->type != SYM_FN) {
2780 struct expression *arg;
2781 expression_error(expr, "not a function %s",
2782 show_ident(sym->ident));
2783 /* do typechecking in arguments */
2784 FOR_EACH_PTR (arglist, arg) {
2785 evaluate_expression(arg);
2786 } END_FOR_EACH_PTR(arg);
2787 return NULL;
2790 examine_fn_arguments(ctype);
2791 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
2792 sym->op && sym->op->args) {
2793 if (!sym->op->args(expr))
2794 return NULL;
2795 } else {
2796 if (!evaluate_arguments(sym, ctype, arglist))
2797 return NULL;
2798 args = expression_list_size(expr->args);
2799 fnargs = symbol_list_size(ctype->arguments);
2800 if (args < fnargs)
2801 expression_error(expr,
2802 "not enough arguments for function %s",
2803 show_ident(sym->ident));
2804 if (args > fnargs && !ctype->variadic)
2805 expression_error(expr,
2806 "too many arguments for function %s",
2807 show_ident(sym->ident));
2809 if (sym->type == SYM_NODE) {
2810 if (evaluate_symbol_call(expr))
2811 return expr->ctype;
2813 expr->ctype = ctype->ctype.base_type;
2814 return expr->ctype;
2817 static struct symbol *evaluate_offsetof(struct expression *expr)
2819 struct expression *e = expr->down;
2820 struct symbol *ctype = expr->in;
2821 int class;
2823 if (expr->op == '.') {
2824 struct symbol *field;
2825 int offset = 0;
2826 if (!ctype) {
2827 expression_error(expr, "expected structure or union");
2828 return NULL;
2830 examine_symbol_type(ctype);
2831 class = classify_type(ctype, &ctype);
2832 if (class != TYPE_COMPOUND) {
2833 expression_error(expr, "expected structure or union");
2834 return NULL;
2837 field = find_identifier(expr->ident, ctype->symbol_list, &offset);
2838 if (!field) {
2839 expression_error(expr, "unknown member");
2840 return NULL;
2842 ctype = field;
2843 expr->type = EXPR_VALUE;
2844 expr->flags = Int_const_expr;
2845 expr->value = offset;
2846 expr->taint = 0;
2847 expr->ctype = size_t_ctype;
2848 } else {
2849 if (!ctype) {
2850 expression_error(expr, "expected structure or union");
2851 return NULL;
2853 examine_symbol_type(ctype);
2854 class = classify_type(ctype, &ctype);
2855 if (class != (TYPE_COMPOUND | TYPE_PTR)) {
2856 expression_error(expr, "expected array");
2857 return NULL;
2859 ctype = ctype->ctype.base_type;
2860 if (!expr->index) {
2861 expr->type = EXPR_VALUE;
2862 expr->flags = Int_const_expr;
2863 expr->value = 0;
2864 expr->taint = 0;
2865 expr->ctype = size_t_ctype;
2866 } else {
2867 struct expression *idx = expr->index, *m;
2868 struct symbol *i_type = evaluate_expression(idx);
2869 int i_class = classify_type(i_type, &i_type);
2870 if (!is_int(i_class)) {
2871 expression_error(expr, "non-integer index");
2872 return NULL;
2874 unrestrict(idx, i_class, &i_type);
2875 idx = cast_to(idx, size_t_ctype);
2876 m = alloc_const_expression(expr->pos,
2877 bits_to_bytes(ctype->bit_size));
2878 m->ctype = size_t_ctype;
2879 m->flags = Int_const_expr;
2880 expr->type = EXPR_BINOP;
2881 expr->left = idx;
2882 expr->right = m;
2883 expr->op = '*';
2884 expr->ctype = size_t_ctype;
2885 expr->flags = m->flags & idx->flags & Int_const_expr;
2888 if (e) {
2889 struct expression *copy = __alloc_expression(0);
2890 *copy = *expr;
2891 if (e->type == EXPR_OFFSETOF)
2892 e->in = ctype;
2893 if (!evaluate_expression(e))
2894 return NULL;
2895 expr->type = EXPR_BINOP;
2896 expr->flags = e->flags & copy->flags & Int_const_expr;
2897 expr->op = '+';
2898 expr->ctype = size_t_ctype;
2899 expr->left = copy;
2900 expr->right = e;
2902 return size_t_ctype;
2905 struct symbol *evaluate_expression(struct expression *expr)
2907 if (!expr)
2908 return NULL;
2909 if (expr->ctype)
2910 return expr->ctype;
2912 switch (expr->type) {
2913 case EXPR_VALUE:
2914 case EXPR_FVALUE:
2915 expression_error(expr, "value expression without a type");
2916 return NULL;
2917 case EXPR_STRING:
2918 return evaluate_string(expr);
2919 case EXPR_SYMBOL:
2920 return evaluate_symbol_expression(expr);
2921 case EXPR_BINOP:
2922 if (!evaluate_expression(expr->left))
2923 return NULL;
2924 if (!evaluate_expression(expr->right))
2925 return NULL;
2926 return evaluate_binop(expr);
2927 case EXPR_LOGICAL:
2928 return evaluate_logical(expr);
2929 case EXPR_COMMA:
2930 evaluate_expression(expr->left);
2931 if (!evaluate_expression(expr->right))
2932 return NULL;
2933 return evaluate_comma(expr);
2934 case EXPR_COMPARE:
2935 if (!evaluate_expression(expr->left))
2936 return NULL;
2937 if (!evaluate_expression(expr->right))
2938 return NULL;
2939 return evaluate_compare(expr);
2940 case EXPR_ASSIGNMENT:
2941 if (!evaluate_expression(expr->left))
2942 return NULL;
2943 if (!evaluate_expression(expr->right))
2944 return NULL;
2945 return evaluate_assignment(expr);
2946 case EXPR_PREOP:
2947 if (!evaluate_expression(expr->unop))
2948 return NULL;
2949 return evaluate_preop(expr);
2950 case EXPR_POSTOP:
2951 if (!evaluate_expression(expr->unop))
2952 return NULL;
2953 return evaluate_postop(expr);
2954 case EXPR_CAST:
2955 case EXPR_FORCE_CAST:
2956 case EXPR_IMPLIED_CAST:
2957 return evaluate_cast(expr);
2958 case EXPR_SIZEOF:
2959 return evaluate_sizeof(expr);
2960 case EXPR_PTRSIZEOF:
2961 return evaluate_ptrsizeof(expr);
2962 case EXPR_ALIGNOF:
2963 return evaluate_alignof(expr);
2964 case EXPR_DEREF:
2965 return evaluate_member_dereference(expr);
2966 case EXPR_CALL:
2967 return evaluate_call(expr);
2968 case EXPR_SELECT:
2969 case EXPR_CONDITIONAL:
2970 return evaluate_conditional_expression(expr);
2971 case EXPR_STATEMENT:
2972 expr->ctype = evaluate_statement(expr->statement);
2973 return expr->ctype;
2975 case EXPR_LABEL:
2976 expr->ctype = &ptr_ctype;
2977 return &ptr_ctype;
2979 case EXPR_TYPE:
2980 /* Evaluate the type of the symbol .. */
2981 evaluate_symbol(expr->symbol);
2982 /* .. but the type of the _expression_ is a "type" */
2983 expr->ctype = &type_ctype;
2984 return &type_ctype;
2986 case EXPR_OFFSETOF:
2987 return evaluate_offsetof(expr);
2989 /* These can not exist as stand-alone expressions */
2990 case EXPR_INITIALIZER:
2991 case EXPR_IDENTIFIER:
2992 case EXPR_INDEX:
2993 case EXPR_POS:
2994 expression_error(expr, "internal front-end error: initializer in expression");
2995 return NULL;
2996 case EXPR_SLICE:
2997 expression_error(expr, "internal front-end error: SLICE re-evaluated");
2998 return NULL;
3000 return NULL;
3003 static void check_duplicates(struct symbol *sym)
3005 int declared = 0;
3006 struct symbol *next = sym;
3008 while ((next = next->same_symbol) != NULL) {
3009 const char *typediff;
3010 evaluate_symbol(next);
3011 declared++;
3012 typediff = type_difference(&sym->ctype, &next->ctype, 0, 0);
3013 if (typediff) {
3014 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
3015 show_ident(sym->ident),
3016 stream_name(next->pos.stream), next->pos.line, typediff);
3017 return;
3020 if (!declared) {
3021 unsigned long mod = sym->ctype.modifiers;
3022 if (mod & (MOD_STATIC | MOD_REGISTER))
3023 return;
3024 if (!(mod & MOD_TOPLEVEL))
3025 return;
3026 if (!Wdecl)
3027 return;
3028 if (sym->ident == &main_ident)
3029 return;
3030 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
3034 static struct symbol *evaluate_symbol(struct symbol *sym)
3036 struct symbol *base_type;
3038 if (!sym)
3039 return sym;
3040 if (sym->evaluated)
3041 return sym;
3042 sym->evaluated = 1;
3044 sym = examine_symbol_type(sym);
3045 base_type = get_base_type(sym);
3046 if (!base_type)
3047 return NULL;
3049 /* Evaluate the initializers */
3050 if (sym->initializer)
3051 evaluate_initializer(sym, &sym->initializer);
3053 /* And finally, evaluate the body of the symbol too */
3054 if (base_type->type == SYM_FN) {
3055 struct symbol *curr = current_fn;
3057 if (sym->definition && sym->definition != sym)
3058 return evaluate_symbol(sym->definition);
3060 current_fn = base_type;
3062 examine_fn_arguments(base_type);
3063 if (!base_type->stmt && base_type->inline_stmt)
3064 uninline(sym);
3065 if (base_type->stmt)
3066 evaluate_statement(base_type->stmt);
3068 current_fn = curr;
3071 return base_type;
3074 void evaluate_symbol_list(struct symbol_list *list)
3076 struct symbol *sym;
3078 FOR_EACH_PTR(list, sym) {
3079 evaluate_symbol(sym);
3080 check_duplicates(sym);
3081 } END_FOR_EACH_PTR(sym);
3084 static struct symbol *evaluate_return_expression(struct statement *stmt)
3086 struct expression *expr = stmt->expression;
3087 struct symbol *fntype;
3089 evaluate_expression(expr);
3090 fntype = current_fn->ctype.base_type;
3091 if (!fntype || fntype == &void_ctype) {
3092 if (expr && expr->ctype != &void_ctype)
3093 expression_error(expr, "return expression in %s function", fntype?"void":"typeless");
3094 if (expr && Wreturn_void)
3095 warning(stmt->pos, "returning void-valued expression");
3096 return NULL;
3099 if (!expr) {
3100 sparse_error(stmt->pos, "return with no return value");
3101 return NULL;
3103 if (!expr->ctype)
3104 return NULL;
3105 compatible_assignment_types(expr, fntype, &stmt->expression, "return expression");
3106 return NULL;
3109 static void evaluate_if_statement(struct statement *stmt)
3111 if (!stmt->if_conditional)
3112 return;
3114 evaluate_conditional(stmt->if_conditional, 0);
3115 evaluate_statement(stmt->if_true);
3116 evaluate_statement(stmt->if_false);
3119 static void evaluate_iterator(struct statement *stmt)
3121 evaluate_symbol_list(stmt->iterator_syms);
3122 evaluate_conditional(stmt->iterator_pre_condition, 1);
3123 evaluate_conditional(stmt->iterator_post_condition,1);
3124 evaluate_statement(stmt->iterator_pre_statement);
3125 evaluate_statement(stmt->iterator_statement);
3126 evaluate_statement(stmt->iterator_post_statement);
3129 static void verify_output_constraint(struct expression *expr, const char *constraint)
3131 switch (*constraint) {
3132 case '=': /* Assignment */
3133 case '+': /* Update */
3134 break;
3135 default:
3136 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
3140 static void verify_input_constraint(struct expression *expr, const char *constraint)
3142 switch (*constraint) {
3143 case '=': /* Assignment */
3144 case '+': /* Update */
3145 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
3149 static void evaluate_asm_statement(struct statement *stmt)
3151 struct expression *expr;
3152 int state;
3154 expr = stmt->asm_string;
3155 if (!expr || expr->type != EXPR_STRING) {
3156 sparse_error(stmt->pos, "need constant string for inline asm");
3157 return;
3160 state = 0;
3161 FOR_EACH_PTR(stmt->asm_outputs, expr) {
3162 struct ident *ident;
3164 switch (state) {
3165 case 0: /* Identifier */
3166 state = 1;
3167 ident = (struct ident *)expr;
3168 continue;
3170 case 1: /* Constraint */
3171 state = 2;
3172 if (!expr || expr->type != EXPR_STRING) {
3173 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
3174 *THIS_ADDRESS(expr) = NULL;
3175 continue;
3177 verify_output_constraint(expr, expr->string->data);
3178 continue;
3180 case 2: /* Expression */
3181 state = 0;
3182 if (!evaluate_expression(expr))
3183 return;
3184 if (!lvalue_expression(expr))
3185 warning(expr->pos, "asm output is not an lvalue");
3186 evaluate_assign_to(expr, expr->ctype);
3187 continue;
3189 } END_FOR_EACH_PTR(expr);
3191 state = 0;
3192 FOR_EACH_PTR(stmt->asm_inputs, expr) {
3193 struct ident *ident;
3195 switch (state) {
3196 case 0: /* Identifier */
3197 state = 1;
3198 ident = (struct ident *)expr;
3199 continue;
3201 case 1: /* Constraint */
3202 state = 2;
3203 if (!expr || expr->type != EXPR_STRING) {
3204 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
3205 *THIS_ADDRESS(expr) = NULL;
3206 continue;
3208 verify_input_constraint(expr, expr->string->data);
3209 continue;
3211 case 2: /* Expression */
3212 state = 0;
3213 if (!evaluate_expression(expr))
3214 return;
3215 continue;
3217 } END_FOR_EACH_PTR(expr);
3219 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
3220 if (!expr) {
3221 sparse_error(stmt->pos, "bad asm output");
3222 return;
3224 if (expr->type == EXPR_STRING)
3225 continue;
3226 expression_error(expr, "asm clobber is not a string");
3227 } END_FOR_EACH_PTR(expr);
3230 static void evaluate_case_statement(struct statement *stmt)
3232 evaluate_expression(stmt->case_expression);
3233 evaluate_expression(stmt->case_to);
3234 evaluate_statement(stmt->case_statement);
3237 static void check_case_type(struct expression *switch_expr,
3238 struct expression *case_expr,
3239 struct expression **enumcase)
3241 struct symbol *switch_type, *case_type;
3242 int sclass, cclass;
3244 if (!case_expr)
3245 return;
3247 switch_type = switch_expr->ctype;
3248 case_type = evaluate_expression(case_expr);
3250 if (!switch_type || !case_type)
3251 goto Bad;
3252 if (enumcase) {
3253 if (*enumcase)
3254 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
3255 else if (is_enum_type(case_type))
3256 *enumcase = case_expr;
3259 sclass = classify_type(switch_type, &switch_type);
3260 cclass = classify_type(case_type, &case_type);
3262 /* both should be arithmetic */
3263 if (!(sclass & cclass & TYPE_NUM))
3264 goto Bad;
3266 /* neither should be floating */
3267 if ((sclass | cclass) & TYPE_FLOAT)
3268 goto Bad;
3270 /* if neither is restricted, we are OK */
3271 if (!((sclass | cclass) & TYPE_RESTRICT))
3272 return;
3274 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
3275 cclass, sclass, case_type, switch_type)) {
3276 unrestrict(case_expr, cclass, &case_type);
3277 unrestrict(switch_expr, sclass, &switch_type);
3279 return;
3281 Bad:
3282 expression_error(case_expr, "incompatible types for 'case' statement");
3285 static void evaluate_switch_statement(struct statement *stmt)
3287 struct symbol *sym;
3288 struct expression *enumcase = NULL;
3289 struct expression **enumcase_holder = &enumcase;
3290 struct expression *sel = stmt->switch_expression;
3292 evaluate_expression(sel);
3293 evaluate_statement(stmt->switch_statement);
3294 if (!sel)
3295 return;
3296 if (sel->ctype && is_enum_type(sel->ctype))
3297 enumcase_holder = NULL; /* Only check cases against switch */
3299 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
3300 struct statement *case_stmt = sym->stmt;
3301 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
3302 check_case_type(sel, case_stmt->case_to, enumcase_holder);
3303 } END_FOR_EACH_PTR(sym);
3306 struct symbol *evaluate_statement(struct statement *stmt)
3308 if (!stmt)
3309 return NULL;
3311 switch (stmt->type) {
3312 case STMT_DECLARATION: {
3313 struct symbol *s;
3314 FOR_EACH_PTR(stmt->declaration, s) {
3315 evaluate_symbol(s);
3316 } END_FOR_EACH_PTR(s);
3317 return NULL;
3320 case STMT_RETURN:
3321 return evaluate_return_expression(stmt);
3323 case STMT_EXPRESSION:
3324 if (!evaluate_expression(stmt->expression))
3325 return NULL;
3326 if (stmt->expression->ctype == &null_ctype)
3327 stmt->expression = cast_to(stmt->expression, &ptr_ctype);
3328 return degenerate(stmt->expression);
3330 case STMT_COMPOUND: {
3331 struct statement *s;
3332 struct symbol *type = NULL;
3334 /* Evaluate the return symbol in the compound statement */
3335 evaluate_symbol(stmt->ret);
3338 * Then, evaluate each statement, making the type of the
3339 * compound statement be the type of the last statement
3341 type = evaluate_statement(stmt->args);
3342 FOR_EACH_PTR(stmt->stmts, s) {
3343 type = evaluate_statement(s);
3344 } END_FOR_EACH_PTR(s);
3345 if (!type)
3346 type = &void_ctype;
3347 return type;
3349 case STMT_IF:
3350 evaluate_if_statement(stmt);
3351 return NULL;
3352 case STMT_ITERATOR:
3353 evaluate_iterator(stmt);
3354 return NULL;
3355 case STMT_SWITCH:
3356 evaluate_switch_statement(stmt);
3357 return NULL;
3358 case STMT_CASE:
3359 evaluate_case_statement(stmt);
3360 return NULL;
3361 case STMT_LABEL:
3362 return evaluate_statement(stmt->label_statement);
3363 case STMT_GOTO:
3364 evaluate_expression(stmt->goto_expression);
3365 return NULL;
3366 case STMT_NONE:
3367 break;
3368 case STMT_ASM:
3369 evaluate_asm_statement(stmt);
3370 return NULL;
3371 case STMT_CONTEXT:
3372 evaluate_expression(stmt->expression);
3373 return NULL;
3374 case STMT_RANGE:
3375 evaluate_expression(stmt->range_expression);
3376 evaluate_expression(stmt->range_low);
3377 evaluate_expression(stmt->range_high);
3378 return NULL;
3380 return NULL;