Document -gcc-base-dir in sparse.1
[smatch.git] / evaluate.c
blobf976645409fa313a13a011910001064ddaa81f87
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 | MOD_LONGLONG))
169 goto left;
170 right:
171 left = right;
172 left:
173 return left;
176 static int same_cast_type(struct symbol *orig, struct symbol *new)
178 return orig->bit_size == new->bit_size && orig->bit_offset == new->bit_offset;
181 static struct symbol *base_type(struct symbol *node, unsigned long *modp, unsigned long *asp)
183 unsigned long mod, as;
185 mod = 0; as = 0;
186 while (node) {
187 mod |= node->ctype.modifiers;
188 as |= node->ctype.as;
189 if (node->type == SYM_NODE) {
190 node = node->ctype.base_type;
191 continue;
193 break;
195 *modp = mod & ~MOD_IGNORE;
196 *asp = as;
197 return node;
200 static int is_same_type(struct expression *expr, struct symbol *new)
202 struct symbol *old = expr->ctype;
203 unsigned long oldmod, newmod, oldas, newas;
205 old = base_type(old, &oldmod, &oldas);
206 new = base_type(new, &newmod, &newas);
208 /* Same base type, same address space? */
209 if (old == new && oldas == newas) {
210 unsigned long difmod;
212 /* Check the modifier bits. */
213 difmod = (oldmod ^ newmod) & ~MOD_NOCAST;
215 /* Exact same type? */
216 if (!difmod)
217 return 1;
220 * Not the same type, but differs only in "const".
221 * Don't warn about MOD_NOCAST.
223 if (difmod == MOD_CONST)
224 return 0;
226 if ((oldmod | newmod) & MOD_NOCAST) {
227 const char *tofrom = "to/from";
228 if (!(newmod & MOD_NOCAST))
229 tofrom = "from";
230 if (!(oldmod & MOD_NOCAST))
231 tofrom = "to";
232 warning(expr->pos, "implicit cast %s nocast type", tofrom);
234 return 0;
237 static void
238 warn_for_different_enum_types (struct position pos,
239 struct symbol *typea,
240 struct symbol *typeb)
242 if (!Wenum_mismatch)
243 return;
244 if (typea->type == SYM_NODE)
245 typea = typea->ctype.base_type;
246 if (typeb->type == SYM_NODE)
247 typeb = typeb->ctype.base_type;
249 if (typea == typeb)
250 return;
252 if (typea->type == SYM_ENUM && typeb->type == SYM_ENUM) {
253 warning(pos, "mixing different enum types");
254 info(pos, " %s versus", show_typename(typea));
255 info(pos, " %s", show_typename(typeb));
260 * This gets called for implicit casts in assignments and
261 * integer promotion. We often want to try to move the
262 * cast down, because the ops involved may have been
263 * implicitly cast up, and we can get rid of the casts
264 * early.
266 static struct expression * cast_to(struct expression *old, struct symbol *type)
268 struct expression *expr;
270 warn_for_different_enum_types (old->pos, old->ctype, type);
272 if (old->ctype != &null_ctype && is_same_type(old, type))
273 return old;
276 * See if we can simplify the op. Move the cast down.
278 switch (old->type) {
279 case EXPR_PREOP:
280 if (old->ctype->bit_size < type->bit_size)
281 break;
282 if (old->op == '~') {
283 old->ctype = type;
284 old->unop = cast_to(old->unop, type);
285 return old;
287 break;
289 case EXPR_IMPLIED_CAST:
290 warn_for_different_enum_types(old->pos, old->ctype, type);
292 if (old->ctype->bit_size >= type->bit_size) {
293 struct expression *orig = old->cast_expression;
294 if (same_cast_type(orig->ctype, type))
295 return orig;
296 if (old->ctype->bit_offset == type->bit_offset) {
297 old->ctype = type;
298 old->cast_type = type;
299 return old;
302 break;
304 default:
305 /* nothing */;
308 expr = alloc_expression(old->pos, EXPR_IMPLIED_CAST);
309 expr->flags = old->flags;
310 expr->ctype = type;
311 expr->cast_type = type;
312 expr->cast_expression = old;
313 return expr;
316 static int is_type_type(struct symbol *type)
318 return (type->ctype.modifiers & MOD_TYPE) != 0;
321 int is_ptr_type(struct symbol *type)
323 if (type->type == SYM_NODE)
324 type = type->ctype.base_type;
325 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
328 static inline int is_float_type(struct symbol *type)
330 if (type->type == SYM_NODE)
331 type = type->ctype.base_type;
332 return type->ctype.base_type == &fp_type;
335 static inline int is_byte_type(struct symbol *type)
337 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
340 enum {
341 TYPE_NUM = 1,
342 TYPE_BITFIELD = 2,
343 TYPE_RESTRICT = 4,
344 TYPE_FLOAT = 8,
345 TYPE_PTR = 16,
346 TYPE_COMPOUND = 32,
347 TYPE_FOULED = 64,
348 TYPE_FN = 128,
351 static inline int classify_type(struct symbol *type, struct symbol **base)
353 static int type_class[SYM_BAD + 1] = {
354 [SYM_PTR] = TYPE_PTR,
355 [SYM_FN] = TYPE_PTR | TYPE_FN,
356 [SYM_ARRAY] = TYPE_PTR | TYPE_COMPOUND,
357 [SYM_STRUCT] = TYPE_COMPOUND,
358 [SYM_UNION] = TYPE_COMPOUND,
359 [SYM_BITFIELD] = TYPE_NUM | TYPE_BITFIELD,
360 [SYM_RESTRICT] = TYPE_NUM | TYPE_RESTRICT,
361 [SYM_FOULED] = TYPE_NUM | TYPE_RESTRICT | TYPE_FOULED,
363 if (type->type == SYM_NODE)
364 type = type->ctype.base_type;
365 if (type->type == SYM_ENUM)
366 type = type->ctype.base_type;
367 *base = type;
368 if (type->type == SYM_BASETYPE) {
369 if (type->ctype.base_type == &int_type)
370 return TYPE_NUM;
371 if (type->ctype.base_type == &fp_type)
372 return TYPE_NUM | TYPE_FLOAT;
374 return type_class[type->type];
377 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
379 static inline int is_string_type(struct symbol *type)
381 if (type->type == SYM_NODE)
382 type = type->ctype.base_type;
383 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
386 static struct symbol *bad_expr_type(struct expression *expr)
388 sparse_error(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
389 switch (expr->type) {
390 case EXPR_BINOP:
391 case EXPR_COMPARE:
392 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
393 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
394 break;
395 case EXPR_PREOP:
396 case EXPR_POSTOP:
397 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
398 break;
399 default:
400 break;
403 expr->flags = 0;
404 return expr->ctype = &bad_ctype;
407 static int restricted_value(struct expression *v, struct symbol *type)
409 if (v->type != EXPR_VALUE)
410 return 1;
411 if (v->value != 0)
412 return 1;
413 return 0;
416 static int restricted_binop(int op, struct symbol *type)
418 switch (op) {
419 case '&':
420 case '=':
421 case SPECIAL_AND_ASSIGN:
422 case SPECIAL_OR_ASSIGN:
423 case SPECIAL_XOR_ASSIGN:
424 return 1; /* unfoul */
425 case '|':
426 case '^':
427 case '?':
428 return 2; /* keep fouled */
429 case SPECIAL_EQUAL:
430 case SPECIAL_NOTEQUAL:
431 return 3; /* warn if fouled */
432 default:
433 return 0; /* warn */
437 static int restricted_unop(int op, struct symbol **type)
439 if (op == '~') {
440 if ((*type)->bit_size < bits_in_int)
441 *type = befoul(*type);
442 return 0;
443 } if (op == '+')
444 return 0;
445 return 1;
448 /* type should be SYM_FOULED */
449 static inline struct symbol *unfoul(struct symbol *type)
451 return type->ctype.base_type;
454 static struct symbol *restricted_binop_type(int op,
455 struct expression *left,
456 struct expression *right,
457 int lclass, int rclass,
458 struct symbol *ltype,
459 struct symbol *rtype)
461 struct symbol *ctype = NULL;
462 if (lclass & TYPE_RESTRICT) {
463 if (rclass & TYPE_RESTRICT) {
464 if (ltype == rtype) {
465 ctype = ltype;
466 } else if (lclass & TYPE_FOULED) {
467 if (unfoul(ltype) == rtype)
468 ctype = ltype;
469 } else if (rclass & TYPE_FOULED) {
470 if (unfoul(rtype) == ltype)
471 ctype = rtype;
473 } else {
474 if (!restricted_value(right, ltype))
475 ctype = ltype;
477 } else if (!restricted_value(left, rtype))
478 ctype = rtype;
480 if (ctype) {
481 switch (restricted_binop(op, ctype)) {
482 case 1:
483 if ((lclass ^ rclass) & TYPE_FOULED)
484 ctype = unfoul(ctype);
485 break;
486 case 3:
487 if (!(lclass & rclass & TYPE_FOULED))
488 break;
489 case 0:
490 ctype = NULL;
491 default:
492 break;
496 return ctype;
499 static inline void unrestrict(struct expression *expr,
500 int class, struct symbol **ctype)
502 if (class & TYPE_RESTRICT) {
503 if (class & TYPE_FOULED)
504 *ctype = unfoul(*ctype);
505 warning(expr->pos, "%s degrades to integer",
506 show_typename(*ctype));
507 *ctype = (*ctype)->ctype.base_type; /* get to arithmetic type */
511 static struct symbol *usual_conversions(int op,
512 struct expression *left,
513 struct expression *right,
514 int lclass, int rclass,
515 struct symbol *ltype,
516 struct symbol *rtype)
518 struct symbol *ctype;
520 warn_for_different_enum_types(right->pos, left->ctype, right->ctype);
522 if ((lclass | rclass) & TYPE_RESTRICT)
523 goto Restr;
525 Normal:
526 if (!(lclass & TYPE_FLOAT)) {
527 if (!(rclass & TYPE_FLOAT))
528 return bigger_int_type(ltype, rtype);
529 else
530 return rtype;
531 } else if (rclass & TYPE_FLOAT) {
532 unsigned long lmod = ltype->ctype.modifiers;
533 unsigned long rmod = rtype->ctype.modifiers;
534 if (rmod & ~lmod & (MOD_LONG | MOD_LONGLONG))
535 return rtype;
536 else
537 return ltype;
538 } else
539 return ltype;
541 Restr:
542 ctype = restricted_binop_type(op, left, right,
543 lclass, rclass, ltype, rtype);
544 if (ctype)
545 return ctype;
547 unrestrict(left, lclass, &ltype);
548 unrestrict(right, rclass, &rtype);
550 goto Normal;
553 static inline int lvalue_expression(struct expression *expr)
555 return expr->type == EXPR_PREOP && expr->op == '*';
558 static inline int is_function(struct symbol *type)
560 return type && type->type == SYM_FN;
563 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *itype)
565 struct expression *index = expr->right;
566 struct symbol *ctype, *base;
567 int multiply;
569 classify_type(degenerate(expr->left), &ctype);
570 base = examine_pointer_target(ctype);
572 if (!base) {
573 expression_error(expr, "missing type information");
574 return NULL;
576 if (is_function(base)) {
577 expression_error(expr, "arithmetics on pointers to functions");
578 return NULL;
581 /* Get the size of whatever the pointer points to */
582 multiply = bits_to_bytes(base->bit_size);
584 if (ctype == &null_ctype)
585 ctype = &ptr_ctype;
586 expr->ctype = ctype;
588 if (multiply == 1 && itype->bit_size >= bits_in_pointer)
589 return ctype;
591 if (index->type == EXPR_VALUE) {
592 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
593 unsigned long long v = index->value, mask;
594 mask = 1ULL << (itype->bit_size - 1);
595 if (v & mask)
596 v |= -mask;
597 else
598 v &= mask - 1;
599 v *= multiply;
600 mask = 1ULL << (bits_in_pointer - 1);
601 v &= mask | (mask - 1);
602 val->value = v;
603 val->ctype = ssize_t_ctype;
604 expr->right = val;
605 return ctype;
608 if (itype->bit_size < bits_in_pointer)
609 index = cast_to(index, ssize_t_ctype);
611 if (multiply > 1) {
612 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
613 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
615 val->ctype = ssize_t_ctype;
616 val->value = multiply;
618 mul->op = '*';
619 mul->ctype = ssize_t_ctype;
620 mul->left = index;
621 mul->right = val;
622 index = mul;
625 expr->right = index;
626 return ctype;
629 static void examine_fn_arguments(struct symbol *fn);
631 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
633 const char *type_difference(struct ctype *c1, struct ctype *c2,
634 unsigned long mod1, unsigned long mod2)
636 unsigned long as1 = c1->as, as2 = c2->as;
637 struct symbol *t1 = c1->base_type;
638 struct symbol *t2 = c2->base_type;
639 int move1 = 1, move2 = 1;
640 mod1 |= c1->modifiers;
641 mod2 |= c2->modifiers;
642 for (;;) {
643 unsigned long diff;
644 int type;
645 struct symbol *base1 = t1->ctype.base_type;
646 struct symbol *base2 = t2->ctype.base_type;
649 * FIXME! Collect alignment and context too here!
651 if (move1) {
652 if (t1 && t1->type != SYM_PTR) {
653 mod1 |= t1->ctype.modifiers;
654 as1 |= t1->ctype.as;
656 move1 = 0;
659 if (move2) {
660 if (t2 && t2->type != SYM_PTR) {
661 mod2 |= t2->ctype.modifiers;
662 as2 |= t2->ctype.as;
664 move2 = 0;
667 if (t1 == t2)
668 break;
669 if (!t1 || !t2)
670 return "different types";
672 if (t1->type == SYM_NODE || t1->type == SYM_ENUM) {
673 t1 = base1;
674 move1 = 1;
675 if (!t1)
676 return "bad types";
677 continue;
680 if (t2->type == SYM_NODE || t2->type == SYM_ENUM) {
681 t2 = base2;
682 move2 = 1;
683 if (!t2)
684 return "bad types";
685 continue;
688 move1 = move2 = 1;
689 type = t1->type;
690 if (type != t2->type)
691 return "different base types";
693 switch (type) {
694 default:
695 sparse_error(t1->pos,
696 "internal error: bad type in derived(%d)",
697 type);
698 return "bad types";
699 case SYM_RESTRICT:
700 return "different base types";
701 case SYM_UNION:
702 case SYM_STRUCT:
703 /* allow definition of incomplete structs and unions */
704 if (t1->ident == t2->ident)
705 return NULL;
706 return "different base types";
707 case SYM_ARRAY:
708 /* XXX: we ought to compare sizes */
709 break;
710 case SYM_PTR:
711 if (Waddress_space && as1 != as2)
712 return "different address spaces";
713 /* MOD_SPECIFIER is due to idiocy in parse.c */
714 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SPECIFIER)
715 return "different modifiers";
716 /* we could be lazier here */
717 base1 = examine_pointer_target(t1);
718 base2 = examine_pointer_target(t2);
719 mod1 = t1->ctype.modifiers;
720 as1 = t1->ctype.as;
721 mod2 = t2->ctype.modifiers;
722 as2 = t2->ctype.as;
723 break;
724 case SYM_FN: {
725 struct symbol *arg1, *arg2;
726 int i;
728 if (Waddress_space && as1 != as2)
729 return "different address spaces";
730 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
731 return "different modifiers";
732 mod1 = t1->ctype.modifiers;
733 as1 = t1->ctype.as;
734 mod2 = t2->ctype.modifiers;
735 as2 = t2->ctype.as;
737 if (base1->variadic != base2->variadic)
738 return "incompatible variadic arguments";
739 examine_fn_arguments(t1);
740 examine_fn_arguments(t2);
741 PREPARE_PTR_LIST(t1->arguments, arg1);
742 PREPARE_PTR_LIST(t2->arguments, arg2);
743 i = 1;
744 for (;;) {
745 const char *diffstr;
746 if (!arg1 && !arg2)
747 break;
748 if (!arg1 || !arg2)
749 return "different argument counts";
750 diffstr = type_difference(&arg1->ctype,
751 &arg2->ctype,
752 MOD_IGN, MOD_IGN);
753 if (diffstr) {
754 static char argdiff[80];
755 sprintf(argdiff, "incompatible argument %d (%s)", i, diffstr);
756 return argdiff;
758 NEXT_PTR_LIST(arg1);
759 NEXT_PTR_LIST(arg2);
760 i++;
762 FINISH_PTR_LIST(arg2);
763 FINISH_PTR_LIST(arg1);
764 break;
766 case SYM_BASETYPE:
767 if (Waddress_space && as1 != as2)
768 return "different address spaces";
769 if (base1 != base2)
770 return "different base types";
771 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
772 if (!diff)
773 return NULL;
774 if (diff & MOD_SIZE)
775 return "different type sizes";
776 else if (diff & ~MOD_SIGNEDNESS)
777 return "different modifiers";
778 else
779 return "different signedness";
781 t1 = base1;
782 t2 = base2;
784 if (Waddress_space && as1 != as2)
785 return "different address spaces";
786 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
787 return "different modifiers";
788 return NULL;
791 static void bad_null(struct expression *expr)
793 if (Wnon_pointer_null)
794 warning(expr->pos, "Using plain integer as NULL pointer");
797 static unsigned long target_qualifiers(struct symbol *type)
799 unsigned long mod = type->ctype.modifiers & MOD_IGN;
800 if (type->ctype.base_type && type->ctype.base_type->type == SYM_ARRAY)
801 mod = 0;
802 return mod;
805 static struct symbol *evaluate_ptr_sub(struct expression *expr)
807 const char *typediff;
808 struct symbol *ltype, *rtype;
809 struct expression *l = expr->left;
810 struct expression *r = expr->right;
811 struct symbol *lbase, *rbase;
813 classify_type(degenerate(l), &ltype);
814 classify_type(degenerate(r), &rtype);
816 lbase = examine_pointer_target(ltype);
817 rbase = examine_pointer_target(rtype);
818 typediff = type_difference(&ltype->ctype, &rtype->ctype,
819 target_qualifiers(rtype),
820 target_qualifiers(ltype));
821 if (typediff)
822 expression_error(expr, "subtraction of different types can't work (%s)", typediff);
824 if (is_function(lbase)) {
825 expression_error(expr, "subtraction of functions? Share your drugs");
826 return NULL;
829 expr->ctype = ssize_t_ctype;
830 if (lbase->bit_size > bits_in_char) {
831 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
832 struct expression *div = expr;
833 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
834 unsigned long value = bits_to_bytes(lbase->bit_size);
836 val->ctype = size_t_ctype;
837 val->value = value;
839 if (value & (value-1)) {
840 if (Wptr_subtraction_blows)
841 warning(expr->pos, "potentially expensive pointer subtraction");
844 sub->op = '-';
845 sub->ctype = ssize_t_ctype;
846 sub->left = l;
847 sub->right = r;
849 div->op = '/';
850 div->left = sub;
851 div->right = val;
854 return ssize_t_ctype;
857 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
859 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
861 struct symbol *ctype;
863 if (!expr)
864 return NULL;
866 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
867 warning(expr->pos, "assignment expression in conditional");
869 ctype = evaluate_expression(expr);
870 if (ctype) {
871 if (is_safe_type(ctype))
872 warning(expr->pos, "testing a 'safe expression'");
875 return ctype;
878 static struct symbol *evaluate_logical(struct expression *expr)
880 if (!evaluate_conditional(expr->left, 0))
881 return NULL;
882 if (!evaluate_conditional(expr->right, 0))
883 return NULL;
885 expr->ctype = &bool_ctype;
886 if (expr->flags) {
887 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
888 expr->flags = 0;
890 return &bool_ctype;
893 static struct symbol *evaluate_binop(struct expression *expr)
895 struct symbol *ltype, *rtype, *ctype;
896 int lclass = classify_type(expr->left->ctype, &ltype);
897 int rclass = classify_type(expr->right->ctype, &rtype);
898 int op = expr->op;
900 if (expr->flags) {
901 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
902 expr->flags = 0;
905 /* number op number */
906 if (lclass & rclass & TYPE_NUM) {
907 if ((lclass | rclass) & TYPE_FLOAT) {
908 switch (op) {
909 case '+': case '-': case '*': case '/':
910 break;
911 default:
912 return bad_expr_type(expr);
916 if (op == SPECIAL_LEFTSHIFT || op == SPECIAL_RIGHTSHIFT) {
917 // shifts do integer promotions, but that's it.
918 unrestrict(expr->left, lclass, &ltype);
919 unrestrict(expr->right, rclass, &rtype);
920 ctype = ltype = integer_promotion(ltype);
921 rtype = integer_promotion(rtype);
922 } else {
923 // The rest do usual conversions
924 const unsigned left_not = expr->left->type == EXPR_PREOP
925 && expr->left->op == '!';
926 const unsigned right_not = expr->right->type == EXPR_PREOP
927 && expr->right->op == '!';
928 if ((op == '&' || op == '|') && (left_not || right_not))
929 warning(expr->pos, "dubious: %sx %c %sy",
930 left_not ? "!" : "",
932 right_not ? "!" : "");
934 ltype = usual_conversions(op, expr->left, expr->right,
935 lclass, rclass, ltype, rtype);
936 ctype = rtype = ltype;
939 expr->left = cast_to(expr->left, ltype);
940 expr->right = cast_to(expr->right, rtype);
941 expr->ctype = ctype;
942 return ctype;
945 /* pointer (+|-) integer */
946 if (lclass & TYPE_PTR && is_int(rclass) && (op == '+' || op == '-')) {
947 unrestrict(expr->right, rclass, &rtype);
948 return evaluate_ptr_add(expr, rtype);
951 /* integer + pointer */
952 if (rclass & TYPE_PTR && is_int(lclass) && op == '+') {
953 struct expression *index = expr->left;
954 unrestrict(index, lclass, &ltype);
955 expr->left = expr->right;
956 expr->right = index;
957 return evaluate_ptr_add(expr, ltype);
960 /* pointer - pointer */
961 if (lclass & rclass & TYPE_PTR && expr->op == '-')
962 return evaluate_ptr_sub(expr);
964 return bad_expr_type(expr);
967 static struct symbol *evaluate_comma(struct expression *expr)
969 expr->ctype = degenerate(expr->right);
970 if (expr->ctype == &null_ctype)
971 expr->ctype = &ptr_ctype;
972 expr->flags &= expr->left->flags & expr->right->flags;
973 return expr->ctype;
976 static int modify_for_unsigned(int op)
978 if (op == '<')
979 op = SPECIAL_UNSIGNED_LT;
980 else if (op == '>')
981 op = SPECIAL_UNSIGNED_GT;
982 else if (op == SPECIAL_LTE)
983 op = SPECIAL_UNSIGNED_LTE;
984 else if (op == SPECIAL_GTE)
985 op = SPECIAL_UNSIGNED_GTE;
986 return op;
989 static inline int is_null_pointer_constant(struct expression *e)
991 if (e->ctype == &null_ctype)
992 return 1;
993 if (!(e->flags & Int_const_expr))
994 return 0;
995 return is_zero_constant(e) ? 2 : 0;
998 static struct symbol *evaluate_compare(struct expression *expr)
1000 struct expression *left = expr->left, *right = expr->right;
1001 struct symbol *ltype, *rtype, *lbase, *rbase;
1002 int lclass = classify_type(degenerate(left), &ltype);
1003 int rclass = classify_type(degenerate(right), &rtype);
1004 struct symbol *ctype;
1005 const char *typediff;
1007 if (expr->flags) {
1008 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
1009 expr->flags = 0;
1012 /* Type types? */
1013 if (is_type_type(ltype) && is_type_type(rtype))
1014 goto OK;
1016 if (is_safe_type(left->ctype) || is_safe_type(right->ctype))
1017 warning(expr->pos, "testing a 'safe expression'");
1019 /* number on number */
1020 if (lclass & rclass & TYPE_NUM) {
1021 ctype = usual_conversions(expr->op, expr->left, expr->right,
1022 lclass, rclass, ltype, rtype);
1023 expr->left = cast_to(expr->left, ctype);
1024 expr->right = cast_to(expr->right, ctype);
1025 if (ctype->ctype.modifiers & MOD_UNSIGNED)
1026 expr->op = modify_for_unsigned(expr->op);
1027 goto OK;
1030 /* at least one must be a pointer */
1031 if (!((lclass | rclass) & TYPE_PTR))
1032 return bad_expr_type(expr);
1034 /* equality comparisons can be with null pointer constants */
1035 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1036 int is_null1 = is_null_pointer_constant(left);
1037 int is_null2 = is_null_pointer_constant(right);
1038 if (is_null1 == 2)
1039 bad_null(left);
1040 if (is_null2 == 2)
1041 bad_null(right);
1042 if (is_null1 && is_null2) {
1043 int positive = expr->op == SPECIAL_EQUAL;
1044 expr->type = EXPR_VALUE;
1045 expr->value = positive;
1046 goto OK;
1048 if (is_null1 && (rclass & TYPE_PTR)) {
1049 left = cast_to(left, rtype);
1050 goto OK;
1052 if (is_null2 && (lclass & TYPE_PTR)) {
1053 right = cast_to(right, ltype);
1054 goto OK;
1057 /* both should be pointers */
1058 if (!(lclass & rclass & TYPE_PTR))
1059 return bad_expr_type(expr);
1060 expr->op = modify_for_unsigned(expr->op);
1062 lbase = examine_pointer_target(ltype);
1063 rbase = examine_pointer_target(rtype);
1065 /* they also have special treatment for pointers to void */
1066 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1067 if (ltype->ctype.as == rtype->ctype.as) {
1068 if (lbase == &void_ctype) {
1069 right = cast_to(right, ltype);
1070 goto OK;
1072 if (rbase == &void_ctype) {
1073 left = cast_to(left, rtype);
1074 goto OK;
1079 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1080 target_qualifiers(rtype),
1081 target_qualifiers(ltype));
1082 if (!typediff)
1083 goto OK;
1085 expression_error(expr, "incompatible types in comparison expression (%s)", typediff);
1086 return NULL;
1089 expr->ctype = &bool_ctype;
1090 return &bool_ctype;
1094 * NOTE! The degenerate case of "x ? : y", where we don't
1095 * have a true case, this will possibly promote "x" to the
1096 * same type as "y", and thus _change_ the conditional
1097 * test in the expression. But since promotion is "safe"
1098 * for testing, that's OK.
1100 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1102 struct expression **true;
1103 struct symbol *ctype, *ltype, *rtype, *lbase, *rbase;
1104 int lclass, rclass;
1105 const char * typediff;
1106 int qual;
1108 if (!evaluate_conditional(expr->conditional, 0))
1109 return NULL;
1110 if (!evaluate_expression(expr->cond_false))
1111 return NULL;
1113 ctype = degenerate(expr->conditional);
1114 rtype = degenerate(expr->cond_false);
1116 true = &expr->conditional;
1117 ltype = ctype;
1118 if (expr->cond_true) {
1119 if (!evaluate_expression(expr->cond_true))
1120 return NULL;
1121 ltype = degenerate(expr->cond_true);
1122 true = &expr->cond_true;
1125 if (expr->flags) {
1126 int flags = expr->conditional->flags & Int_const_expr;
1127 flags &= (*true)->flags & expr->cond_false->flags;
1128 if (!flags)
1129 expr->flags = 0;
1132 lclass = classify_type(ltype, &ltype);
1133 rclass = classify_type(rtype, &rtype);
1134 if (lclass & rclass & TYPE_NUM) {
1135 ctype = usual_conversions('?', *true, expr->cond_false,
1136 lclass, rclass, ltype, rtype);
1137 *true = cast_to(*true, ctype);
1138 expr->cond_false = cast_to(expr->cond_false, ctype);
1139 goto out;
1142 if ((lclass | rclass) & TYPE_PTR) {
1143 int is_null1 = is_null_pointer_constant(*true);
1144 int is_null2 = is_null_pointer_constant(expr->cond_false);
1146 if (is_null1 && is_null2) {
1147 *true = cast_to(*true, &ptr_ctype);
1148 expr->cond_false = cast_to(expr->cond_false, &ptr_ctype);
1149 ctype = &ptr_ctype;
1150 goto out;
1152 if (is_null1 && (rclass & TYPE_PTR)) {
1153 if (is_null1 == 2)
1154 bad_null(*true);
1155 *true = cast_to(*true, rtype);
1156 ctype = rtype;
1157 goto out;
1159 if (is_null2 && (lclass & TYPE_PTR)) {
1160 if (is_null2 == 2)
1161 bad_null(expr->cond_false);
1162 expr->cond_false = cast_to(expr->cond_false, ltype);
1163 ctype = ltype;
1164 goto out;
1166 if (!(lclass & rclass & TYPE_PTR)) {
1167 typediff = "different types";
1168 goto Err;
1170 /* OK, it's pointer on pointer */
1171 if (ltype->ctype.as != rtype->ctype.as) {
1172 typediff = "different address spaces";
1173 goto Err;
1176 /* need to be lazier here */
1177 lbase = examine_pointer_target(ltype);
1178 rbase = examine_pointer_target(rtype);
1179 qual = target_qualifiers(ltype) | target_qualifiers(rtype);
1181 if (lbase == &void_ctype) {
1182 /* XXX: pointers to function should warn here */
1183 ctype = ltype;
1184 goto Qual;
1187 if (rbase == &void_ctype) {
1188 /* XXX: pointers to function should warn here */
1189 ctype = rtype;
1190 goto Qual;
1192 /* XXX: that should be pointer to composite */
1193 ctype = ltype;
1194 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1195 qual, qual);
1196 if (!typediff)
1197 goto Qual;
1198 goto Err;
1201 /* void on void, struct on same struct, union on same union */
1202 if (ltype == rtype) {
1203 ctype = ltype;
1204 goto out;
1206 typediff = "different base types";
1208 Err:
1209 expression_error(expr, "incompatible types in conditional expression (%s)", typediff);
1210 return NULL;
1212 out:
1213 expr->ctype = ctype;
1214 return ctype;
1216 Qual:
1217 if (qual & ~ctype->ctype.modifiers) {
1218 struct symbol *sym = alloc_symbol(ctype->pos, SYM_PTR);
1219 *sym = *ctype;
1220 sym->ctype.modifiers |= qual;
1221 ctype = sym;
1223 *true = cast_to(*true, ctype);
1224 expr->cond_false = cast_to(expr->cond_false, ctype);
1225 goto out;
1228 /* FP assignments can not do modulo or bit operations */
1229 static int compatible_float_op(int op)
1231 return op == SPECIAL_ADD_ASSIGN ||
1232 op == SPECIAL_SUB_ASSIGN ||
1233 op == SPECIAL_MUL_ASSIGN ||
1234 op == SPECIAL_DIV_ASSIGN;
1237 static int evaluate_assign_op(struct expression *expr)
1239 struct symbol *target = expr->left->ctype;
1240 struct symbol *source = expr->right->ctype;
1241 struct symbol *t, *s;
1242 int tclass = classify_type(target, &t);
1243 int sclass = classify_type(source, &s);
1244 int op = expr->op;
1246 if (tclass & sclass & TYPE_NUM) {
1247 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1248 expression_error(expr, "invalid assignment");
1249 return 0;
1251 if (tclass & TYPE_RESTRICT) {
1252 if (!restricted_binop(op, t)) {
1253 warning(expr->pos, "bad assignment (%s) to %s",
1254 show_special(op), show_typename(t));
1255 expr->right = cast_to(expr->right, target);
1256 return 0;
1258 /* allowed assignments unfoul */
1259 if (sclass & TYPE_FOULED && unfoul(s) == t)
1260 goto Cast;
1261 if (!restricted_value(expr->right, t))
1262 return 1;
1263 } else if (!(sclass & TYPE_RESTRICT))
1264 goto Cast;
1265 /* source and target would better be identical restricted */
1266 if (t == s)
1267 return 1;
1268 warning(expr->pos, "invalid assignment: %s", show_special(op));
1269 info(expr->pos, " left side has type %s", show_typename(t));
1270 info(expr->pos, " right side has type %s", show_typename(s));
1271 expr->right = cast_to(expr->right, target);
1272 return 0;
1274 if (tclass == TYPE_PTR && is_int(sclass)) {
1275 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1276 unrestrict(expr->right, sclass, &s);
1277 evaluate_ptr_add(expr, s);
1278 return 1;
1280 expression_error(expr, "invalid pointer assignment");
1281 return 0;
1284 expression_error(expr, "invalid assignment");
1285 return 0;
1287 Cast:
1288 expr->right = cast_to(expr->right, target);
1289 return 1;
1292 static int whitelist_pointers(struct symbol *t1, struct symbol *t2)
1294 if (t1 == t2)
1295 return 0; /* yes, 0 - we don't want a cast_to here */
1296 if (t1 == &void_ctype)
1297 return 1;
1298 if (t2 == &void_ctype)
1299 return 1;
1300 if (classify_type(t1, &t1) != TYPE_NUM)
1301 return 0;
1302 if (classify_type(t2, &t2) != TYPE_NUM)
1303 return 0;
1304 if (t1 == t2)
1305 return 1;
1306 if (t1->ctype.modifiers & t2->ctype.modifiers & MOD_CHAR)
1307 return 1;
1308 if ((t1->ctype.modifiers ^ t2->ctype.modifiers) & MOD_SIZE)
1309 return 0;
1310 return !Wtypesign;
1313 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1314 struct expression **rp, const char *where)
1316 const char *typediff;
1317 struct symbol *source = degenerate(*rp);
1318 struct symbol *t, *s;
1319 int tclass = classify_type(target, &t);
1320 int sclass = classify_type(source, &s);
1322 if (tclass & sclass & TYPE_NUM) {
1323 if (tclass & TYPE_RESTRICT) {
1324 /* allowed assignments unfoul */
1325 if (sclass & TYPE_FOULED && unfoul(s) == t)
1326 goto Cast;
1327 if (!restricted_value(*rp, target))
1328 return 1;
1329 if (s == t)
1330 return 1;
1331 } else if (!(sclass & TYPE_RESTRICT))
1332 goto Cast;
1333 typediff = "different base types";
1334 goto Err;
1337 if (tclass == TYPE_PTR) {
1338 unsigned long mod1, mod2;
1339 struct symbol *b1, *b2;
1340 // NULL pointer is always OK
1341 int is_null = is_null_pointer_constant(*rp);
1342 if (is_null) {
1343 if (is_null == 2)
1344 bad_null(*rp);
1345 goto Cast;
1347 if (!(sclass & TYPE_PTR)) {
1348 typediff = "different base types";
1349 goto Err;
1351 b1 = examine_pointer_target(t);
1352 b2 = examine_pointer_target(s);
1353 mod1 = target_qualifiers(t);
1354 mod2 = target_qualifiers(s);
1355 if (whitelist_pointers(b1, b2)) {
1357 * assignments to/from void * are OK, provided that
1358 * we do not remove qualifiers from pointed to [C]
1359 * or mix address spaces [sparse].
1361 if (t->ctype.as != s->ctype.as) {
1362 typediff = "different address spaces";
1363 goto Err;
1365 if (mod2 & ~mod1) {
1366 typediff = "different modifiers";
1367 goto Err;
1369 goto Cast;
1371 /* It's OK if the target is more volatile or const than the source */
1372 typediff = type_difference(&t->ctype, &s->ctype, 0, mod1);
1373 if (typediff)
1374 goto Err;
1375 return 1;
1378 if ((tclass & TYPE_COMPOUND) && s == t)
1379 return 1;
1381 if (tclass & TYPE_NUM) {
1382 /* XXX: need to turn into comparison with NULL */
1383 if (t == &bool_ctype && (sclass & TYPE_PTR))
1384 goto Cast;
1385 typediff = "different base types";
1386 goto Err;
1388 typediff = "invalid types";
1390 Err:
1391 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1392 info(expr->pos, " expected %s", show_typename(target));
1393 info(expr->pos, " got %s", show_typename(source));
1394 *rp = cast_to(*rp, target);
1395 return 0;
1396 Cast:
1397 *rp = cast_to(*rp, target);
1398 return 1;
1401 static void mark_assigned(struct expression *expr)
1403 struct symbol *sym;
1405 if (!expr)
1406 return;
1407 switch (expr->type) {
1408 case EXPR_SYMBOL:
1409 sym = expr->symbol;
1410 if (!sym)
1411 return;
1412 if (sym->type != SYM_NODE)
1413 return;
1414 sym->ctype.modifiers |= MOD_ASSIGNED;
1415 return;
1417 case EXPR_BINOP:
1418 mark_assigned(expr->left);
1419 mark_assigned(expr->right);
1420 return;
1421 case EXPR_CAST:
1422 case EXPR_FORCE_CAST:
1423 mark_assigned(expr->cast_expression);
1424 return;
1425 case EXPR_SLICE:
1426 mark_assigned(expr->base);
1427 return;
1428 default:
1429 /* Hmm? */
1430 return;
1434 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1436 if (type->ctype.modifiers & MOD_CONST)
1437 expression_error(left, "assignment to const expression");
1439 /* We know left is an lvalue, so it's a "preop-*" */
1440 mark_assigned(left->unop);
1443 static struct symbol *evaluate_assignment(struct expression *expr)
1445 struct expression *left = expr->left;
1446 struct expression *where = expr;
1447 struct symbol *ltype;
1449 if (!lvalue_expression(left)) {
1450 expression_error(expr, "not an lvalue");
1451 return NULL;
1454 ltype = left->ctype;
1456 if (expr->op != '=') {
1457 if (!evaluate_assign_op(expr))
1458 return NULL;
1459 } else {
1460 if (!compatible_assignment_types(where, ltype, &expr->right, "assignment"))
1461 return NULL;
1464 evaluate_assign_to(left, ltype);
1466 expr->ctype = ltype;
1467 return ltype;
1470 static void examine_fn_arguments(struct symbol *fn)
1472 struct symbol *s;
1474 FOR_EACH_PTR(fn->arguments, s) {
1475 struct symbol *arg = evaluate_symbol(s);
1476 /* Array/function arguments silently degenerate into pointers */
1477 if (arg) {
1478 struct symbol *ptr;
1479 switch(arg->type) {
1480 case SYM_ARRAY:
1481 case SYM_FN:
1482 ptr = alloc_symbol(s->pos, SYM_PTR);
1483 if (arg->type == SYM_ARRAY)
1484 ptr->ctype = arg->ctype;
1485 else
1486 ptr->ctype.base_type = arg;
1487 ptr->ctype.as |= s->ctype.as;
1488 ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1490 s->ctype.base_type = ptr;
1491 s->ctype.as = 0;
1492 s->ctype.modifiers &= ~MOD_PTRINHERIT;
1493 s->bit_size = 0;
1494 s->examined = 0;
1495 examine_symbol_type(s);
1496 break;
1497 default:
1498 /* nothing */
1499 break;
1502 } END_FOR_EACH_PTR(s);
1505 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1507 /* Take the modifiers of the pointer, and apply them to the member */
1508 mod |= sym->ctype.modifiers;
1509 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1510 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1511 *newsym = *sym;
1512 newsym->ctype.as = as;
1513 newsym->ctype.modifiers = mod;
1514 sym = newsym;
1516 return sym;
1519 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1521 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1522 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1524 node->ctype.base_type = ptr;
1525 ptr->bit_size = bits_in_pointer;
1526 ptr->ctype.alignment = pointer_alignment;
1528 node->bit_size = bits_in_pointer;
1529 node->ctype.alignment = pointer_alignment;
1531 access_symbol(sym);
1532 if (sym->ctype.modifiers & MOD_REGISTER) {
1533 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1534 sym->ctype.modifiers &= ~MOD_REGISTER;
1536 if (sym->type == SYM_NODE) {
1537 ptr->ctype.as |= sym->ctype.as;
1538 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1539 sym = sym->ctype.base_type;
1541 if (degenerate && sym->type == SYM_ARRAY) {
1542 ptr->ctype.as |= sym->ctype.as;
1543 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1544 sym = sym->ctype.base_type;
1546 ptr->ctype.base_type = sym;
1548 return node;
1551 /* Arrays degenerate into pointers on pointer arithmetic */
1552 static struct symbol *degenerate(struct expression *expr)
1554 struct symbol *ctype, *base;
1556 if (!expr)
1557 return NULL;
1558 ctype = expr->ctype;
1559 if (!ctype)
1560 return NULL;
1561 base = examine_symbol_type(ctype);
1562 if (ctype->type == SYM_NODE)
1563 base = ctype->ctype.base_type;
1565 * Arrays degenerate into pointers to the entries, while
1566 * functions degenerate into pointers to themselves.
1567 * If array was part of non-lvalue compound, we create a copy
1568 * of that compound first and then act as if we were dealing with
1569 * the corresponding field in there.
1571 switch (base->type) {
1572 case SYM_ARRAY:
1573 if (expr->type == EXPR_SLICE) {
1574 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1575 struct expression *e0, *e1, *e2, *e3, *e4;
1577 a->ctype.base_type = expr->base->ctype;
1578 a->bit_size = expr->base->ctype->bit_size;
1579 a->array_size = expr->base->ctype->array_size;
1581 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1582 e0->symbol = a;
1583 e0->ctype = &lazy_ptr_ctype;
1585 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1586 e1->unop = e0;
1587 e1->op = '*';
1588 e1->ctype = expr->base->ctype; /* XXX */
1590 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1591 e2->left = e1;
1592 e2->right = expr->base;
1593 e2->op = '=';
1594 e2->ctype = expr->base->ctype;
1596 if (expr->r_bitpos) {
1597 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1598 e3->op = '+';
1599 e3->left = e0;
1600 e3->right = alloc_const_expression(expr->pos,
1601 bits_to_bytes(expr->r_bitpos));
1602 e3->ctype = &lazy_ptr_ctype;
1603 } else {
1604 e3 = e0;
1607 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1608 e4->left = e2;
1609 e4->right = e3;
1610 e4->ctype = &lazy_ptr_ctype;
1612 expr->unop = e4;
1613 expr->type = EXPR_PREOP;
1614 expr->op = '*';
1616 case SYM_FN:
1617 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1618 expression_error(expr, "strange non-value function or array");
1619 return &bad_ctype;
1621 *expr = *expr->unop;
1622 ctype = create_pointer(expr, ctype, 1);
1623 expr->ctype = ctype;
1624 default:
1625 /* nothing */;
1627 return ctype;
1630 static struct symbol *evaluate_addressof(struct expression *expr)
1632 struct expression *op = expr->unop;
1633 struct symbol *ctype;
1635 if (op->op != '*' || op->type != EXPR_PREOP) {
1636 expression_error(expr, "not addressable");
1637 return NULL;
1639 ctype = op->ctype;
1640 *expr = *op->unop;
1641 expr->flags = 0;
1643 if (expr->type == EXPR_SYMBOL) {
1644 struct symbol *sym = expr->symbol;
1645 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1649 * symbol expression evaluation is lazy about the type
1650 * of the sub-expression, so we may have to generate
1651 * the type here if so..
1653 if (expr->ctype == &lazy_ptr_ctype) {
1654 ctype = create_pointer(expr, ctype, 0);
1655 expr->ctype = ctype;
1657 return expr->ctype;
1661 static struct symbol *evaluate_dereference(struct expression *expr)
1663 struct expression *op = expr->unop;
1664 struct symbol *ctype = op->ctype, *node, *target;
1666 /* Simplify: *&(expr) => (expr) */
1667 if (op->type == EXPR_PREOP && op->op == '&') {
1668 *expr = *op->unop;
1669 expr->flags = 0;
1670 return expr->ctype;
1673 /* Dereferencing a node drops all the node information. */
1674 if (ctype->type == SYM_NODE)
1675 ctype = ctype->ctype.base_type;
1677 node = alloc_symbol(expr->pos, SYM_NODE);
1678 target = ctype->ctype.base_type;
1680 switch (ctype->type) {
1681 default:
1682 expression_error(expr, "cannot dereference this type");
1683 return NULL;
1684 case SYM_PTR:
1685 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1686 merge_type(node, ctype);
1687 break;
1689 case SYM_ARRAY:
1690 if (!lvalue_expression(op)) {
1691 expression_error(op, "non-lvalue array??");
1692 return NULL;
1695 /* Do the implied "addressof" on the array */
1696 *op = *op->unop;
1699 * When an array is dereferenced, we need to pick
1700 * up the attributes of the original node too..
1702 merge_type(node, op->ctype);
1703 merge_type(node, ctype);
1704 break;
1707 node->bit_size = target->bit_size;
1708 node->array_size = target->array_size;
1710 expr->ctype = node;
1711 return node;
1715 * Unary post-ops: x++ and x--
1717 static struct symbol *evaluate_postop(struct expression *expr)
1719 struct expression *op = expr->unop;
1720 struct symbol *ctype = op->ctype;
1721 int class = classify_type(op->ctype, &ctype);
1722 int multiply = 0;
1724 if (!lvalue_expression(expr->unop)) {
1725 expression_error(expr, "need lvalue expression for ++/--");
1726 return NULL;
1729 if ((class & TYPE_RESTRICT) && restricted_unop(expr->op, &ctype))
1730 return bad_expr_type(expr);
1732 if (class & TYPE_NUM) {
1733 multiply = 1;
1734 } else if (class == TYPE_PTR) {
1735 struct symbol *target = examine_pointer_target(ctype);
1736 if (!is_function(target))
1737 multiply = bits_to_bytes(target->bit_size);
1740 if (multiply) {
1741 evaluate_assign_to(op, op->ctype);
1742 expr->op_value = multiply;
1743 expr->ctype = ctype;
1744 return ctype;
1747 expression_error(expr, "bad argument type for ++/--");
1748 return NULL;
1751 static struct symbol *evaluate_sign(struct expression *expr)
1753 struct symbol *ctype = expr->unop->ctype;
1754 int class = classify_type(ctype, &ctype);
1755 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1756 expr->flags = 0;
1757 /* should be an arithmetic type */
1758 if (!(class & TYPE_NUM))
1759 return bad_expr_type(expr);
1760 if (!(class & (TYPE_FLOAT|TYPE_RESTRICT))) {
1761 struct symbol *rtype = integer_promotion(ctype);
1762 expr->unop = cast_to(expr->unop, rtype);
1763 ctype = rtype;
1764 } else if ((class & TYPE_FLOAT) && expr->op != '~') {
1765 /* no conversions needed */
1766 } else if ((class & TYPE_RESTRICT) && !restricted_unop(expr->op, &ctype)) {
1767 /* no conversions needed */
1768 } else {
1769 return bad_expr_type(expr);
1771 if (expr->op == '+')
1772 *expr = *expr->unop;
1773 expr->ctype = ctype;
1774 return ctype;
1777 static struct symbol *evaluate_preop(struct expression *expr)
1779 struct symbol *ctype = expr->unop->ctype;
1781 switch (expr->op) {
1782 case '(':
1783 *expr = *expr->unop;
1784 return ctype;
1786 case '+':
1787 case '-':
1788 case '~':
1789 return evaluate_sign(expr);
1791 case '*':
1792 return evaluate_dereference(expr);
1794 case '&':
1795 return evaluate_addressof(expr);
1797 case SPECIAL_INCREMENT:
1798 case SPECIAL_DECREMENT:
1800 * From a type evaluation standpoint the preops are
1801 * the same as the postops
1803 return evaluate_postop(expr);
1805 case '!':
1806 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1807 expr->flags = 0;
1808 if (is_safe_type(ctype))
1809 warning(expr->pos, "testing a 'safe expression'");
1810 if (is_float_type(ctype)) {
1811 struct expression *arg = expr->unop;
1812 expr->type = EXPR_BINOP;
1813 expr->op = SPECIAL_EQUAL;
1814 expr->left = arg;
1815 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1816 expr->right->ctype = ctype;
1817 expr->right->fvalue = 0;
1818 } else if (is_fouled_type(ctype)) {
1819 warning(expr->pos, "%s degrades to integer",
1820 show_typename(ctype->ctype.base_type));
1822 ctype = &bool_ctype;
1823 break;
1825 default:
1826 break;
1828 expr->ctype = ctype;
1829 return &bool_ctype;
1832 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1834 struct ptr_list *head = (struct ptr_list *)_list;
1835 struct ptr_list *list = head;
1837 if (!head)
1838 return NULL;
1839 do {
1840 int i;
1841 for (i = 0; i < list->nr; i++) {
1842 struct symbol *sym = (struct symbol *) list->list[i];
1843 if (sym->ident) {
1844 if (sym->ident != ident)
1845 continue;
1846 *offset = sym->offset;
1847 return sym;
1848 } else {
1849 struct symbol *ctype = sym->ctype.base_type;
1850 struct symbol *sub;
1851 if (!ctype)
1852 continue;
1853 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1854 continue;
1855 sub = find_identifier(ident, ctype->symbol_list, offset);
1856 if (!sub)
1857 continue;
1858 *offset += sym->offset;
1859 return sub;
1862 } while ((list = list->next) != head);
1863 return NULL;
1866 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1868 struct expression *add;
1871 * Create a new add-expression
1873 * NOTE! Even if we just add zero, we need a new node
1874 * for the member pointer, since it has a different
1875 * type than the original pointer. We could make that
1876 * be just a cast, but the fact is, a node is a node,
1877 * so we might as well just do the "add zero" here.
1879 add = alloc_expression(expr->pos, EXPR_BINOP);
1880 add->op = '+';
1881 add->left = expr;
1882 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1883 add->right->ctype = &int_ctype;
1884 add->right->value = offset;
1887 * The ctype of the pointer will be lazily evaluated if
1888 * we ever take the address of this member dereference..
1890 add->ctype = &lazy_ptr_ctype;
1891 return add;
1894 /* structure/union dereference */
1895 static struct symbol *evaluate_member_dereference(struct expression *expr)
1897 int offset;
1898 struct symbol *ctype, *member;
1899 struct expression *deref = expr->deref, *add;
1900 struct ident *ident = expr->member;
1901 unsigned int mod;
1902 int address_space;
1904 if (!evaluate_expression(deref))
1905 return NULL;
1906 if (!ident) {
1907 expression_error(expr, "bad member name");
1908 return NULL;
1911 ctype = deref->ctype;
1912 examine_symbol_type(ctype);
1913 address_space = ctype->ctype.as;
1914 mod = ctype->ctype.modifiers;
1915 if (ctype->type == SYM_NODE) {
1916 ctype = ctype->ctype.base_type;
1917 address_space |= ctype->ctype.as;
1918 mod |= ctype->ctype.modifiers;
1920 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1921 expression_error(expr, "expected structure or union");
1922 return NULL;
1924 offset = 0;
1925 member = find_identifier(ident, ctype->symbol_list, &offset);
1926 if (!member) {
1927 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1928 const char *name = "<unnamed>";
1929 int namelen = 9;
1930 if (ctype->ident) {
1931 name = ctype->ident->name;
1932 namelen = ctype->ident->len;
1934 if (ctype->symbol_list)
1935 expression_error(expr, "no member '%s' in %s %.*s",
1936 show_ident(ident), type, namelen, name);
1937 else
1938 expression_error(expr, "using member '%s' in "
1939 "incomplete %s %.*s", show_ident(ident),
1940 type, namelen, name);
1941 return NULL;
1945 * The member needs to take on the address space and modifiers of
1946 * the "parent" type.
1948 member = convert_to_as_mod(member, address_space, mod);
1949 ctype = get_base_type(member);
1951 if (!lvalue_expression(deref)) {
1952 if (deref->type != EXPR_SLICE) {
1953 expr->base = deref;
1954 expr->r_bitpos = 0;
1955 } else {
1956 expr->base = deref->base;
1957 expr->r_bitpos = deref->r_bitpos;
1959 expr->r_bitpos += bytes_to_bits(offset);
1960 expr->type = EXPR_SLICE;
1961 expr->r_nrbits = member->bit_size;
1962 expr->r_bitpos += member->bit_offset;
1963 expr->ctype = member;
1964 return member;
1967 deref = deref->unop;
1968 expr->deref = deref;
1970 add = evaluate_offset(deref, offset);
1971 expr->type = EXPR_PREOP;
1972 expr->op = '*';
1973 expr->unop = add;
1975 expr->ctype = member;
1976 return member;
1979 static int is_promoted(struct expression *expr)
1981 while (1) {
1982 switch (expr->type) {
1983 case EXPR_BINOP:
1984 case EXPR_SELECT:
1985 case EXPR_CONDITIONAL:
1986 return 1;
1987 case EXPR_COMMA:
1988 expr = expr->right;
1989 continue;
1990 case EXPR_PREOP:
1991 switch (expr->op) {
1992 case '(':
1993 expr = expr->unop;
1994 continue;
1995 case '+':
1996 case '-':
1997 case '~':
1998 return 1;
1999 default:
2000 return 0;
2002 default:
2003 return 0;
2009 static struct symbol *evaluate_cast(struct expression *);
2011 static struct symbol *evaluate_type_information(struct expression *expr)
2013 struct symbol *sym = expr->cast_type;
2014 if (!sym) {
2015 sym = evaluate_expression(expr->cast_expression);
2016 if (!sym)
2017 return NULL;
2019 * Expressions of restricted types will possibly get
2020 * promoted - check that here
2022 if (is_restricted_type(sym)) {
2023 if (sym->bit_size < bits_in_int && is_promoted(expr))
2024 sym = &int_ctype;
2025 } else if (is_fouled_type(sym)) {
2026 sym = &int_ctype;
2029 examine_symbol_type(sym);
2030 if (is_bitfield_type(sym)) {
2031 expression_error(expr, "trying to examine bitfield type");
2032 return NULL;
2034 return sym;
2037 static struct symbol *evaluate_sizeof(struct expression *expr)
2039 struct symbol *type;
2040 int size;
2042 type = evaluate_type_information(expr);
2043 if (!type)
2044 return NULL;
2046 size = type->bit_size;
2047 if ((size < 0) || (size & (bits_in_char - 1)))
2048 expression_error(expr, "cannot size expression");
2049 expr->type = EXPR_VALUE;
2050 expr->value = bits_to_bytes(size);
2051 expr->taint = 0;
2052 expr->ctype = size_t_ctype;
2053 return size_t_ctype;
2056 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
2058 struct symbol *type;
2059 int size;
2061 type = evaluate_type_information(expr);
2062 if (!type)
2063 return NULL;
2065 if (type->type == SYM_NODE)
2066 type = type->ctype.base_type;
2067 if (!type)
2068 return NULL;
2069 switch (type->type) {
2070 case SYM_ARRAY:
2071 break;
2072 case SYM_PTR:
2073 type = get_base_type(type);
2074 if (type)
2075 break;
2076 default:
2077 expression_error(expr, "expected pointer expression");
2078 return NULL;
2080 size = type->bit_size;
2081 if (size & (bits_in_char-1))
2082 size = 0;
2083 expr->type = EXPR_VALUE;
2084 expr->value = bits_to_bytes(size);
2085 expr->taint = 0;
2086 expr->ctype = size_t_ctype;
2087 return size_t_ctype;
2090 static struct symbol *evaluate_alignof(struct expression *expr)
2092 struct symbol *type;
2094 type = evaluate_type_information(expr);
2095 if (!type)
2096 return NULL;
2098 expr->type = EXPR_VALUE;
2099 expr->value = type->ctype.alignment;
2100 expr->taint = 0;
2101 expr->ctype = size_t_ctype;
2102 return size_t_ctype;
2105 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
2107 struct expression *expr;
2108 struct symbol_list *argument_types = fn->arguments;
2109 struct symbol *argtype;
2110 int i = 1;
2112 PREPARE_PTR_LIST(argument_types, argtype);
2113 FOR_EACH_PTR (head, expr) {
2114 struct expression **p = THIS_ADDRESS(expr);
2115 struct symbol *ctype, *target;
2116 ctype = evaluate_expression(expr);
2118 if (!ctype)
2119 return 0;
2121 target = argtype;
2122 if (!target) {
2123 struct symbol *type;
2124 int class = classify_type(ctype, &type);
2125 if (is_int(class)) {
2126 *p = cast_to(expr, integer_promotion(type));
2127 } else if (class & TYPE_FLOAT) {
2128 unsigned long mod = type->ctype.modifiers;
2129 if (!(mod & (MOD_LONG|MOD_LONGLONG)))
2130 *p = cast_to(expr, &double_ctype);
2131 } else if (class & TYPE_PTR) {
2132 if (expr->ctype == &null_ctype)
2133 *p = cast_to(expr, &ptr_ctype);
2134 else
2135 degenerate(expr);
2137 } else {
2138 static char where[30];
2139 examine_symbol_type(target);
2140 sprintf(where, "argument %d", i);
2141 compatible_assignment_types(expr, target, p, where);
2144 i++;
2145 NEXT_PTR_LIST(argtype);
2146 } END_FOR_EACH_PTR(expr);
2147 FINISH_PTR_LIST(argtype);
2148 return 1;
2151 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
2153 struct symbol *sym;
2155 FOR_EACH_PTR(ctype->symbol_list, sym) {
2156 if (sym->ident == ident)
2157 return sym;
2158 } END_FOR_EACH_PTR(sym);
2159 return NULL;
2162 static void convert_index(struct expression *e)
2164 struct expression *child = e->idx_expression;
2165 unsigned from = e->idx_from;
2166 unsigned to = e->idx_to + 1;
2167 e->type = EXPR_POS;
2168 e->init_offset = from * bits_to_bytes(e->ctype->bit_size);
2169 e->init_nr = to - from;
2170 e->init_expr = child;
2173 static void convert_ident(struct expression *e)
2175 struct expression *child = e->ident_expression;
2176 struct symbol *sym = e->field;
2177 e->type = EXPR_POS;
2178 e->init_offset = sym->offset;
2179 e->init_nr = 1;
2180 e->init_expr = child;
2183 static void convert_designators(struct expression *e)
2185 while (e) {
2186 if (e->type == EXPR_INDEX)
2187 convert_index(e);
2188 else if (e->type == EXPR_IDENTIFIER)
2189 convert_ident(e);
2190 else
2191 break;
2192 e = e->init_expr;
2196 static void excess(struct expression *e, const char *s)
2198 warning(e->pos, "excessive elements in %s initializer", s);
2202 * implicit designator for the first element
2204 static struct expression *first_subobject(struct symbol *ctype, int class,
2205 struct expression **v)
2207 struct expression *e = *v, *new;
2209 if (ctype->type == SYM_NODE)
2210 ctype = ctype->ctype.base_type;
2212 if (class & TYPE_PTR) { /* array */
2213 if (!ctype->bit_size)
2214 return NULL;
2215 new = alloc_expression(e->pos, EXPR_INDEX);
2216 new->idx_expression = e;
2217 new->ctype = ctype->ctype.base_type;
2218 } else {
2219 struct symbol *field, *p;
2220 PREPARE_PTR_LIST(ctype->symbol_list, p);
2221 while (p && !p->ident && is_bitfield_type(p))
2222 NEXT_PTR_LIST(p);
2223 field = p;
2224 FINISH_PTR_LIST(p);
2225 if (!field)
2226 return NULL;
2227 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2228 new->ident_expression = e;
2229 new->field = new->ctype = field;
2231 *v = new;
2232 return new;
2236 * sanity-check explicit designators; return the innermost one or NULL
2237 * in case of error. Assign types.
2239 static struct expression *check_designators(struct expression *e,
2240 struct symbol *ctype)
2242 struct expression *last = NULL;
2243 const char *err;
2244 while (1) {
2245 if (ctype->type == SYM_NODE)
2246 ctype = ctype->ctype.base_type;
2247 if (e->type == EXPR_INDEX) {
2248 struct symbol *type;
2249 if (ctype->type != SYM_ARRAY) {
2250 err = "array index in non-array";
2251 break;
2253 type = ctype->ctype.base_type;
2254 if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2255 unsigned offset = e->idx_to * type->bit_size;
2256 if (offset >= ctype->bit_size) {
2257 err = "index out of bounds in";
2258 break;
2261 e->ctype = ctype = type;
2262 ctype = type;
2263 last = e;
2264 if (!e->idx_expression) {
2265 err = "invalid";
2266 break;
2268 e = e->idx_expression;
2269 } else if (e->type == EXPR_IDENTIFIER) {
2270 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2271 err = "field name not in struct or union";
2272 break;
2274 ctype = find_struct_ident(ctype, e->expr_ident);
2275 if (!ctype) {
2276 err = "unknown field name in";
2277 break;
2279 e->field = e->ctype = ctype;
2280 last = e;
2281 if (!e->ident_expression) {
2282 err = "invalid";
2283 break;
2285 e = e->ident_expression;
2286 } else if (e->type == EXPR_POS) {
2287 err = "internal front-end error: EXPR_POS in";
2288 break;
2289 } else
2290 return last;
2292 expression_error(e, "%s initializer", err);
2293 return NULL;
2297 * choose the next subobject to initialize.
2299 * Get designators for next element, switch old ones to EXPR_POS.
2300 * Return the resulting expression or NULL if we'd run out of subobjects.
2301 * The innermost designator is returned in *v. Designators in old
2302 * are assumed to be already sanity-checked.
2304 static struct expression *next_designators(struct expression *old,
2305 struct symbol *ctype,
2306 struct expression *e, struct expression **v)
2308 struct expression *new = NULL;
2310 if (!old)
2311 return NULL;
2312 if (old->type == EXPR_INDEX) {
2313 struct expression *copy;
2314 unsigned n;
2316 copy = next_designators(old->idx_expression,
2317 old->ctype, e, v);
2318 if (!copy) {
2319 n = old->idx_to + 1;
2320 if (n * old->ctype->bit_size == ctype->bit_size) {
2321 convert_index(old);
2322 return NULL;
2324 copy = e;
2325 *v = new = alloc_expression(e->pos, EXPR_INDEX);
2326 } else {
2327 n = old->idx_to;
2328 new = alloc_expression(e->pos, EXPR_INDEX);
2331 new->idx_from = new->idx_to = n;
2332 new->idx_expression = copy;
2333 new->ctype = old->ctype;
2334 convert_index(old);
2335 } else if (old->type == EXPR_IDENTIFIER) {
2336 struct expression *copy;
2337 struct symbol *field;
2339 copy = next_designators(old->ident_expression,
2340 old->ctype, e, v);
2341 if (!copy) {
2342 field = old->field->next_subobject;
2343 if (!field) {
2344 convert_ident(old);
2345 return NULL;
2347 copy = e;
2348 *v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2349 } else {
2350 field = old->field;
2351 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2354 new->field = field;
2355 new->expr_ident = field->ident;
2356 new->ident_expression = copy;
2357 new->ctype = field;
2358 convert_ident(old);
2360 return new;
2363 static int handle_simple_initializer(struct expression **ep, int nested,
2364 int class, struct symbol *ctype);
2367 * deal with traversing subobjects [6.7.8(17,18,20)]
2369 static void handle_list_initializer(struct expression *expr,
2370 int class, struct symbol *ctype)
2372 struct expression *e, *last = NULL, *top = NULL, *next;
2373 int jumped = 0;
2375 FOR_EACH_PTR(expr->expr_list, e) {
2376 struct expression **v;
2377 struct symbol *type;
2378 int lclass;
2380 if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2381 if (!top) {
2382 top = e;
2383 last = first_subobject(ctype, class, &top);
2384 } else {
2385 last = next_designators(last, ctype, e, &top);
2387 if (!last) {
2388 excess(e, class & TYPE_PTR ? "array" :
2389 "struct or union");
2390 DELETE_CURRENT_PTR(e);
2391 continue;
2393 if (jumped) {
2394 warning(e->pos, "advancing past deep designator");
2395 jumped = 0;
2397 REPLACE_CURRENT_PTR(e, last);
2398 } else {
2399 next = check_designators(e, ctype);
2400 if (!next) {
2401 DELETE_CURRENT_PTR(e);
2402 continue;
2404 top = next;
2405 /* deeper than one designator? */
2406 jumped = top != e;
2407 convert_designators(last);
2408 last = e;
2411 found:
2412 lclass = classify_type(top->ctype, &type);
2413 if (top->type == EXPR_INDEX)
2414 v = &top->idx_expression;
2415 else
2416 v = &top->ident_expression;
2418 if (handle_simple_initializer(v, 1, lclass, top->ctype))
2419 continue;
2421 if (!(lclass & TYPE_COMPOUND)) {
2422 warning(e->pos, "bogus scalar initializer");
2423 DELETE_CURRENT_PTR(e);
2424 continue;
2427 next = first_subobject(type, lclass, v);
2428 if (next) {
2429 warning(e->pos, "missing braces around initializer");
2430 top = next;
2431 goto found;
2434 DELETE_CURRENT_PTR(e);
2435 excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2437 } END_FOR_EACH_PTR(e);
2439 convert_designators(last);
2440 expr->ctype = ctype;
2443 static int is_string_literal(struct expression **v)
2445 struct expression *e = *v;
2446 while (e && e->type == EXPR_PREOP && e->op == '(')
2447 e = e->unop;
2448 if (!e || e->type != EXPR_STRING)
2449 return 0;
2450 if (e != *v && Wparen_string)
2451 warning(e->pos,
2452 "array initialized from parenthesized string constant");
2453 *v = e;
2454 return 1;
2458 * We want a normal expression, possibly in one layer of braces. Warn
2459 * if the latter happens inside a list (it's legal, but likely to be
2460 * an effect of screwup). In case of anything not legal, we are definitely
2461 * having an effect of screwup, so just fail and let the caller warn.
2463 static struct expression *handle_scalar(struct expression *e, int nested)
2465 struct expression *v = NULL, *p;
2466 int count = 0;
2468 /* normal case */
2469 if (e->type != EXPR_INITIALIZER)
2470 return e;
2472 FOR_EACH_PTR(e->expr_list, p) {
2473 if (!v)
2474 v = p;
2475 count++;
2476 } END_FOR_EACH_PTR(p);
2477 if (count != 1)
2478 return NULL;
2479 switch(v->type) {
2480 case EXPR_INITIALIZER:
2481 case EXPR_INDEX:
2482 case EXPR_IDENTIFIER:
2483 return NULL;
2484 default:
2485 break;
2487 if (nested)
2488 warning(e->pos, "braces around scalar initializer");
2489 return v;
2493 * deal with the cases that don't care about subobjects:
2494 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2495 * character array <- string literal, possibly in braces [6.7.8(14)]
2496 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2497 * compound type <- initializer list in braces [6.7.8(16)]
2498 * The last one punts to handle_list_initializer() which, in turn will call
2499 * us for individual elements of the list.
2501 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2502 * the lack of support of wide char stuff in general.
2504 * One note: we need to take care not to evaluate a string literal until
2505 * we know that we *will* handle it right here. Otherwise we would screw
2506 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2507 * { "string", ...} - we need to preserve that string literal recognizable
2508 * until we dig into the inner struct.
2510 static int handle_simple_initializer(struct expression **ep, int nested,
2511 int class, struct symbol *ctype)
2513 int is_string = is_string_type(ctype);
2514 struct expression *e = *ep, *p;
2515 struct symbol *type;
2517 if (!e)
2518 return 0;
2520 /* scalar */
2521 if (!(class & TYPE_COMPOUND)) {
2522 e = handle_scalar(e, nested);
2523 if (!e)
2524 return 0;
2525 *ep = e;
2526 if (!evaluate_expression(e))
2527 return 1;
2528 compatible_assignment_types(e, ctype, ep, "initializer");
2529 return 1;
2533 * sublist; either a string, or we dig in; the latter will deal with
2534 * pathologies, so we don't need anything fancy here.
2536 if (e->type == EXPR_INITIALIZER) {
2537 if (is_string) {
2538 struct expression *v = NULL;
2539 int count = 0;
2541 FOR_EACH_PTR(e->expr_list, p) {
2542 if (!v)
2543 v = p;
2544 count++;
2545 } END_FOR_EACH_PTR(p);
2546 if (count == 1 && is_string_literal(&v)) {
2547 *ep = e = v;
2548 goto String;
2551 handle_list_initializer(e, class, ctype);
2552 return 1;
2555 /* string */
2556 if (is_string_literal(&e)) {
2557 /* either we are doing array of char, or we'll have to dig in */
2558 if (is_string) {
2559 *ep = e;
2560 goto String;
2562 return 0;
2564 /* struct or union can be initialized by compatible */
2565 if (class != TYPE_COMPOUND)
2566 return 0;
2567 type = evaluate_expression(e);
2568 if (!type)
2569 return 0;
2570 if (ctype->type == SYM_NODE)
2571 ctype = ctype->ctype.base_type;
2572 if (type->type == SYM_NODE)
2573 type = type->ctype.base_type;
2574 if (ctype == type)
2575 return 1;
2576 return 0;
2578 String:
2579 p = alloc_expression(e->pos, EXPR_STRING);
2580 *p = *e;
2581 type = evaluate_expression(p);
2582 if (ctype->bit_size != -1 &&
2583 ctype->bit_size + bits_in_char < type->bit_size) {
2584 warning(e->pos,
2585 "too long initializer-string for array of char");
2587 *ep = p;
2588 return 1;
2591 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2593 struct symbol *type;
2594 int class = classify_type(ctype, &type);
2595 if (!handle_simple_initializer(ep, 0, class, ctype))
2596 expression_error(*ep, "invalid initializer");
2599 static struct symbol *evaluate_cast(struct expression *expr)
2601 struct expression *target = expr->cast_expression;
2602 struct symbol *ctype;
2603 struct symbol *t1, *t2;
2604 int class1, class2;
2605 int as1 = 0, as2 = 0;
2607 if (!target)
2608 return NULL;
2611 * Special case: a cast can be followed by an
2612 * initializer, in which case we need to pass
2613 * the type value down to that initializer rather
2614 * than trying to evaluate it as an expression
2616 * A more complex case is when the initializer is
2617 * dereferenced as part of a post-fix expression.
2618 * We need to produce an expression that can be dereferenced.
2620 if (target->type == EXPR_INITIALIZER) {
2621 struct symbol *sym = expr->cast_type;
2622 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2624 sym->initializer = target;
2625 evaluate_symbol(sym);
2627 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2628 addr->symbol = sym;
2630 expr->type = EXPR_PREOP;
2631 expr->op = '*';
2632 expr->unop = addr;
2633 expr->ctype = sym;
2635 return sym;
2638 ctype = examine_symbol_type(expr->cast_type);
2639 expr->ctype = ctype;
2640 expr->cast_type = ctype;
2642 evaluate_expression(target);
2643 degenerate(target);
2645 class1 = classify_type(ctype, &t1);
2647 /* cast to non-integer type -> not an integer constant expression */
2648 if (!is_int(class1))
2649 expr->flags = 0;
2650 /* if argument turns out to be not an integer constant expression *and*
2651 it was not a floating literal to start with -> too bad */
2652 else if (expr->flags == Int_const_expr &&
2653 !(target->flags & Int_const_expr))
2654 expr->flags = 0;
2656 * You can always throw a value away by casting to
2657 * "void" - that's an implicit "force". Note that
2658 * the same is _not_ true of "void *".
2660 if (t1 == &void_ctype)
2661 goto out;
2663 if (class1 & (TYPE_COMPOUND | TYPE_FN))
2664 warning(expr->pos, "cast to non-scalar");
2666 t2 = target->ctype;
2667 if (!t2) {
2668 expression_error(expr, "cast from unknown type");
2669 goto out;
2671 class2 = classify_type(t2, &t2);
2673 if (class2 & TYPE_COMPOUND)
2674 warning(expr->pos, "cast from non-scalar");
2676 if (expr->type == EXPR_FORCE_CAST)
2677 goto out;
2679 /* allowed cast unfouls */
2680 if (class2 & TYPE_FOULED)
2681 t2 = unfoul(t2);
2683 if (t1 != t2) {
2684 if (class1 & TYPE_RESTRICT)
2685 warning(expr->pos, "cast to %s",
2686 show_typename(t1));
2687 if (class2 & TYPE_RESTRICT)
2688 warning(expr->pos, "cast from %s",
2689 show_typename(t2));
2692 if (t1 == &ulong_ctype)
2693 as1 = -1;
2694 else if (class1 == TYPE_PTR) {
2695 examine_pointer_target(t1);
2696 as1 = t1->ctype.as;
2699 if (t2 == &ulong_ctype)
2700 as2 = -1;
2701 else if (class2 == TYPE_PTR) {
2702 examine_pointer_target(t2);
2703 as2 = t2->ctype.as;
2706 if (!as1 && as2 > 0)
2707 warning(expr->pos, "cast removes address space of expression");
2708 if (as1 > 0 && as2 > 0 && as1 != as2)
2709 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2710 if (as1 > 0 && !as2 &&
2711 !is_null_pointer_constant(target) && Wcast_to_as)
2712 warning(expr->pos,
2713 "cast adds address space to expression (<asn:%d>)", as1);
2715 if (!(t1->ctype.modifiers & MOD_PTRINHERIT) && class1 == TYPE_PTR &&
2716 !as1 && (target->flags & Int_const_expr)) {
2717 if (t1->ctype.base_type == &void_ctype) {
2718 if (is_zero_constant(target)) {
2719 /* NULL */
2720 expr->type = EXPR_VALUE;
2721 expr->ctype = &null_ctype;
2722 expr->value = 0;
2723 return ctype;
2727 out:
2728 return ctype;
2732 * Evaluate a call expression with a symbol. This
2733 * should expand inline functions, and evaluate
2734 * builtins.
2736 static int evaluate_symbol_call(struct expression *expr)
2738 struct expression *fn = expr->fn;
2739 struct symbol *ctype = fn->ctype;
2741 if (fn->type != EXPR_PREOP)
2742 return 0;
2744 if (ctype->op && ctype->op->evaluate)
2745 return ctype->op->evaluate(expr);
2747 if (ctype->ctype.modifiers & MOD_INLINE) {
2748 int ret;
2749 struct symbol *curr = current_fn;
2750 current_fn = ctype->ctype.base_type;
2752 ret = inline_function(expr, ctype);
2754 /* restore the old function */
2755 current_fn = curr;
2756 return ret;
2759 return 0;
2762 static struct symbol *evaluate_call(struct expression *expr)
2764 int args, fnargs;
2765 struct symbol *ctype, *sym;
2766 struct expression *fn = expr->fn;
2767 struct expression_list *arglist = expr->args;
2769 if (!evaluate_expression(fn))
2770 return NULL;
2771 sym = ctype = fn->ctype;
2772 if (ctype->type == SYM_NODE)
2773 ctype = ctype->ctype.base_type;
2774 if (ctype->type == SYM_PTR)
2775 ctype = get_base_type(ctype);
2777 if (ctype->type != SYM_FN) {
2778 struct expression *arg;
2779 expression_error(expr, "not a function %s",
2780 show_ident(sym->ident));
2781 /* do typechecking in arguments */
2782 FOR_EACH_PTR (arglist, arg) {
2783 evaluate_expression(arg);
2784 } END_FOR_EACH_PTR(arg);
2785 return NULL;
2788 examine_fn_arguments(ctype);
2789 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
2790 sym->op && sym->op->args) {
2791 if (!sym->op->args(expr))
2792 return NULL;
2793 } else {
2794 if (!evaluate_arguments(sym, ctype, arglist))
2795 return NULL;
2796 args = expression_list_size(expr->args);
2797 fnargs = symbol_list_size(ctype->arguments);
2798 if (args < fnargs)
2799 expression_error(expr,
2800 "not enough arguments for function %s",
2801 show_ident(sym->ident));
2802 if (args > fnargs && !ctype->variadic)
2803 expression_error(expr,
2804 "too many arguments for function %s",
2805 show_ident(sym->ident));
2807 if (sym->type == SYM_NODE) {
2808 if (evaluate_symbol_call(expr))
2809 return expr->ctype;
2811 expr->ctype = ctype->ctype.base_type;
2812 return expr->ctype;
2815 static struct symbol *evaluate_offsetof(struct expression *expr)
2817 struct expression *e = expr->down;
2818 struct symbol *ctype = expr->in;
2819 int class;
2821 if (expr->op == '.') {
2822 struct symbol *field;
2823 int offset = 0;
2824 if (!ctype) {
2825 expression_error(expr, "expected structure or union");
2826 return NULL;
2828 examine_symbol_type(ctype);
2829 class = classify_type(ctype, &ctype);
2830 if (class != TYPE_COMPOUND) {
2831 expression_error(expr, "expected structure or union");
2832 return NULL;
2835 field = find_identifier(expr->ident, ctype->symbol_list, &offset);
2836 if (!field) {
2837 expression_error(expr, "unknown member");
2838 return NULL;
2840 ctype = field;
2841 expr->type = EXPR_VALUE;
2842 expr->flags = Int_const_expr;
2843 expr->value = offset;
2844 expr->taint = 0;
2845 expr->ctype = size_t_ctype;
2846 } else {
2847 if (!ctype) {
2848 expression_error(expr, "expected structure or union");
2849 return NULL;
2851 examine_symbol_type(ctype);
2852 class = classify_type(ctype, &ctype);
2853 if (class != (TYPE_COMPOUND | TYPE_PTR)) {
2854 expression_error(expr, "expected array");
2855 return NULL;
2857 ctype = ctype->ctype.base_type;
2858 if (!expr->index) {
2859 expr->type = EXPR_VALUE;
2860 expr->flags = Int_const_expr;
2861 expr->value = 0;
2862 expr->taint = 0;
2863 expr->ctype = size_t_ctype;
2864 } else {
2865 struct expression *idx = expr->index, *m;
2866 struct symbol *i_type = evaluate_expression(idx);
2867 int i_class = classify_type(i_type, &i_type);
2868 if (!is_int(i_class)) {
2869 expression_error(expr, "non-integer index");
2870 return NULL;
2872 unrestrict(idx, i_class, &i_type);
2873 idx = cast_to(idx, size_t_ctype);
2874 m = alloc_const_expression(expr->pos,
2875 bits_to_bytes(ctype->bit_size));
2876 m->ctype = size_t_ctype;
2877 m->flags = Int_const_expr;
2878 expr->type = EXPR_BINOP;
2879 expr->left = idx;
2880 expr->right = m;
2881 expr->op = '*';
2882 expr->ctype = size_t_ctype;
2883 expr->flags = m->flags & idx->flags & Int_const_expr;
2886 if (e) {
2887 struct expression *copy = __alloc_expression(0);
2888 *copy = *expr;
2889 if (e->type == EXPR_OFFSETOF)
2890 e->in = ctype;
2891 if (!evaluate_expression(e))
2892 return NULL;
2893 expr->type = EXPR_BINOP;
2894 expr->flags = e->flags & copy->flags & Int_const_expr;
2895 expr->op = '+';
2896 expr->ctype = size_t_ctype;
2897 expr->left = copy;
2898 expr->right = e;
2900 return size_t_ctype;
2903 struct symbol *evaluate_expression(struct expression *expr)
2905 if (!expr)
2906 return NULL;
2907 if (expr->ctype)
2908 return expr->ctype;
2910 switch (expr->type) {
2911 case EXPR_VALUE:
2912 case EXPR_FVALUE:
2913 expression_error(expr, "value expression without a type");
2914 return NULL;
2915 case EXPR_STRING:
2916 return evaluate_string(expr);
2917 case EXPR_SYMBOL:
2918 return evaluate_symbol_expression(expr);
2919 case EXPR_BINOP:
2920 if (!evaluate_expression(expr->left))
2921 return NULL;
2922 if (!evaluate_expression(expr->right))
2923 return NULL;
2924 return evaluate_binop(expr);
2925 case EXPR_LOGICAL:
2926 return evaluate_logical(expr);
2927 case EXPR_COMMA:
2928 evaluate_expression(expr->left);
2929 if (!evaluate_expression(expr->right))
2930 return NULL;
2931 return evaluate_comma(expr);
2932 case EXPR_COMPARE:
2933 if (!evaluate_expression(expr->left))
2934 return NULL;
2935 if (!evaluate_expression(expr->right))
2936 return NULL;
2937 return evaluate_compare(expr);
2938 case EXPR_ASSIGNMENT:
2939 if (!evaluate_expression(expr->left))
2940 return NULL;
2941 if (!evaluate_expression(expr->right))
2942 return NULL;
2943 return evaluate_assignment(expr);
2944 case EXPR_PREOP:
2945 if (!evaluate_expression(expr->unop))
2946 return NULL;
2947 return evaluate_preop(expr);
2948 case EXPR_POSTOP:
2949 if (!evaluate_expression(expr->unop))
2950 return NULL;
2951 return evaluate_postop(expr);
2952 case EXPR_CAST:
2953 case EXPR_FORCE_CAST:
2954 case EXPR_IMPLIED_CAST:
2955 return evaluate_cast(expr);
2956 case EXPR_SIZEOF:
2957 return evaluate_sizeof(expr);
2958 case EXPR_PTRSIZEOF:
2959 return evaluate_ptrsizeof(expr);
2960 case EXPR_ALIGNOF:
2961 return evaluate_alignof(expr);
2962 case EXPR_DEREF:
2963 return evaluate_member_dereference(expr);
2964 case EXPR_CALL:
2965 return evaluate_call(expr);
2966 case EXPR_SELECT:
2967 case EXPR_CONDITIONAL:
2968 return evaluate_conditional_expression(expr);
2969 case EXPR_STATEMENT:
2970 expr->ctype = evaluate_statement(expr->statement);
2971 return expr->ctype;
2973 case EXPR_LABEL:
2974 expr->ctype = &ptr_ctype;
2975 return &ptr_ctype;
2977 case EXPR_TYPE:
2978 /* Evaluate the type of the symbol .. */
2979 evaluate_symbol(expr->symbol);
2980 /* .. but the type of the _expression_ is a "type" */
2981 expr->ctype = &type_ctype;
2982 return &type_ctype;
2984 case EXPR_OFFSETOF:
2985 return evaluate_offsetof(expr);
2987 /* These can not exist as stand-alone expressions */
2988 case EXPR_INITIALIZER:
2989 case EXPR_IDENTIFIER:
2990 case EXPR_INDEX:
2991 case EXPR_POS:
2992 expression_error(expr, "internal front-end error: initializer in expression");
2993 return NULL;
2994 case EXPR_SLICE:
2995 expression_error(expr, "internal front-end error: SLICE re-evaluated");
2996 return NULL;
2998 return NULL;
3001 static void check_duplicates(struct symbol *sym)
3003 int declared = 0;
3004 struct symbol *next = sym;
3006 while ((next = next->same_symbol) != NULL) {
3007 const char *typediff;
3008 evaluate_symbol(next);
3009 declared++;
3010 typediff = type_difference(&sym->ctype, &next->ctype, 0, 0);
3011 if (typediff) {
3012 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
3013 show_ident(sym->ident),
3014 stream_name(next->pos.stream), next->pos.line, typediff);
3015 return;
3018 if (!declared) {
3019 unsigned long mod = sym->ctype.modifiers;
3020 if (mod & (MOD_STATIC | MOD_REGISTER))
3021 return;
3022 if (!(mod & MOD_TOPLEVEL))
3023 return;
3024 if (!Wdecl)
3025 return;
3026 if (sym->ident == &main_ident)
3027 return;
3028 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
3032 static struct symbol *evaluate_symbol(struct symbol *sym)
3034 struct symbol *base_type;
3036 if (!sym)
3037 return sym;
3038 if (sym->evaluated)
3039 return sym;
3040 sym->evaluated = 1;
3042 sym = examine_symbol_type(sym);
3043 base_type = get_base_type(sym);
3044 if (!base_type)
3045 return NULL;
3047 /* Evaluate the initializers */
3048 if (sym->initializer)
3049 evaluate_initializer(sym, &sym->initializer);
3051 /* And finally, evaluate the body of the symbol too */
3052 if (base_type->type == SYM_FN) {
3053 struct symbol *curr = current_fn;
3055 current_fn = base_type;
3057 examine_fn_arguments(base_type);
3058 if (!base_type->stmt && base_type->inline_stmt)
3059 uninline(sym);
3060 if (base_type->stmt)
3061 evaluate_statement(base_type->stmt);
3063 current_fn = curr;
3066 return base_type;
3069 void evaluate_symbol_list(struct symbol_list *list)
3071 struct symbol *sym;
3073 FOR_EACH_PTR(list, sym) {
3074 evaluate_symbol(sym);
3075 check_duplicates(sym);
3076 } END_FOR_EACH_PTR(sym);
3079 static struct symbol *evaluate_return_expression(struct statement *stmt)
3081 struct expression *expr = stmt->expression;
3082 struct symbol *fntype;
3084 evaluate_expression(expr);
3085 fntype = current_fn->ctype.base_type;
3086 if (!fntype || fntype == &void_ctype) {
3087 if (expr && expr->ctype != &void_ctype)
3088 expression_error(expr, "return expression in %s function", fntype?"void":"typeless");
3089 if (expr && Wreturn_void)
3090 warning(stmt->pos, "returning void-valued expression");
3091 return NULL;
3094 if (!expr) {
3095 sparse_error(stmt->pos, "return with no return value");
3096 return NULL;
3098 if (!expr->ctype)
3099 return NULL;
3100 compatible_assignment_types(expr, fntype, &stmt->expression, "return expression");
3101 return NULL;
3104 static void evaluate_if_statement(struct statement *stmt)
3106 if (!stmt->if_conditional)
3107 return;
3109 evaluate_conditional(stmt->if_conditional, 0);
3110 evaluate_statement(stmt->if_true);
3111 evaluate_statement(stmt->if_false);
3114 static void evaluate_iterator(struct statement *stmt)
3116 evaluate_symbol_list(stmt->iterator_syms);
3117 evaluate_conditional(stmt->iterator_pre_condition, 1);
3118 evaluate_conditional(stmt->iterator_post_condition,1);
3119 evaluate_statement(stmt->iterator_pre_statement);
3120 evaluate_statement(stmt->iterator_statement);
3121 evaluate_statement(stmt->iterator_post_statement);
3124 static void verify_output_constraint(struct expression *expr, const char *constraint)
3126 switch (*constraint) {
3127 case '=': /* Assignment */
3128 case '+': /* Update */
3129 break;
3130 default:
3131 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
3135 static void verify_input_constraint(struct expression *expr, const char *constraint)
3137 switch (*constraint) {
3138 case '=': /* Assignment */
3139 case '+': /* Update */
3140 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
3144 static void evaluate_asm_statement(struct statement *stmt)
3146 struct expression *expr;
3147 int state;
3149 expr = stmt->asm_string;
3150 if (!expr || expr->type != EXPR_STRING) {
3151 sparse_error(stmt->pos, "need constant string for inline asm");
3152 return;
3155 state = 0;
3156 FOR_EACH_PTR(stmt->asm_outputs, expr) {
3157 struct ident *ident;
3159 switch (state) {
3160 case 0: /* Identifier */
3161 state = 1;
3162 ident = (struct ident *)expr;
3163 continue;
3165 case 1: /* Constraint */
3166 state = 2;
3167 if (!expr || expr->type != EXPR_STRING) {
3168 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
3169 *THIS_ADDRESS(expr) = NULL;
3170 continue;
3172 verify_output_constraint(expr, expr->string->data);
3173 continue;
3175 case 2: /* Expression */
3176 state = 0;
3177 if (!evaluate_expression(expr))
3178 return;
3179 if (!lvalue_expression(expr))
3180 warning(expr->pos, "asm output is not an lvalue");
3181 evaluate_assign_to(expr, expr->ctype);
3182 continue;
3184 } END_FOR_EACH_PTR(expr);
3186 state = 0;
3187 FOR_EACH_PTR(stmt->asm_inputs, expr) {
3188 struct ident *ident;
3190 switch (state) {
3191 case 0: /* Identifier */
3192 state = 1;
3193 ident = (struct ident *)expr;
3194 continue;
3196 case 1: /* Constraint */
3197 state = 2;
3198 if (!expr || expr->type != EXPR_STRING) {
3199 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
3200 *THIS_ADDRESS(expr) = NULL;
3201 continue;
3203 verify_input_constraint(expr, expr->string->data);
3204 continue;
3206 case 2: /* Expression */
3207 state = 0;
3208 if (!evaluate_expression(expr))
3209 return;
3210 continue;
3212 } END_FOR_EACH_PTR(expr);
3214 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
3215 if (!expr) {
3216 sparse_error(stmt->pos, "bad asm output");
3217 return;
3219 if (expr->type == EXPR_STRING)
3220 continue;
3221 expression_error(expr, "asm clobber is not a string");
3222 } END_FOR_EACH_PTR(expr);
3225 static void evaluate_case_statement(struct statement *stmt)
3227 evaluate_expression(stmt->case_expression);
3228 evaluate_expression(stmt->case_to);
3229 evaluate_statement(stmt->case_statement);
3232 static void check_case_type(struct expression *switch_expr,
3233 struct expression *case_expr,
3234 struct expression **enumcase)
3236 struct symbol *switch_type, *case_type;
3237 int sclass, cclass;
3239 if (!case_expr)
3240 return;
3242 switch_type = switch_expr->ctype;
3243 case_type = evaluate_expression(case_expr);
3245 if (!switch_type || !case_type)
3246 goto Bad;
3247 if (enumcase) {
3248 if (*enumcase)
3249 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
3250 else if (is_enum_type(case_type))
3251 *enumcase = case_expr;
3254 sclass = classify_type(switch_type, &switch_type);
3255 cclass = classify_type(case_type, &case_type);
3257 /* both should be arithmetic */
3258 if (!(sclass & cclass & TYPE_NUM))
3259 goto Bad;
3261 /* neither should be floating */
3262 if ((sclass | cclass) & TYPE_FLOAT)
3263 goto Bad;
3265 /* if neither is restricted, we are OK */
3266 if (!((sclass | cclass) & TYPE_RESTRICT))
3267 return;
3269 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
3270 cclass, sclass, case_type, switch_type)) {
3271 unrestrict(case_expr, cclass, &case_type);
3272 unrestrict(switch_expr, sclass, &switch_type);
3274 return;
3276 Bad:
3277 expression_error(case_expr, "incompatible types for 'case' statement");
3280 static void evaluate_switch_statement(struct statement *stmt)
3282 struct symbol *sym;
3283 struct expression *enumcase = NULL;
3284 struct expression **enumcase_holder = &enumcase;
3285 struct expression *sel = stmt->switch_expression;
3287 evaluate_expression(sel);
3288 evaluate_statement(stmt->switch_statement);
3289 if (!sel)
3290 return;
3291 if (sel->ctype && is_enum_type(sel->ctype))
3292 enumcase_holder = NULL; /* Only check cases against switch */
3294 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
3295 struct statement *case_stmt = sym->stmt;
3296 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
3297 check_case_type(sel, case_stmt->case_to, enumcase_holder);
3298 } END_FOR_EACH_PTR(sym);
3301 struct symbol *evaluate_statement(struct statement *stmt)
3303 if (!stmt)
3304 return NULL;
3306 switch (stmt->type) {
3307 case STMT_DECLARATION: {
3308 struct symbol *s;
3309 FOR_EACH_PTR(stmt->declaration, s) {
3310 evaluate_symbol(s);
3311 } END_FOR_EACH_PTR(s);
3312 return NULL;
3315 case STMT_RETURN:
3316 return evaluate_return_expression(stmt);
3318 case STMT_EXPRESSION:
3319 if (!evaluate_expression(stmt->expression))
3320 return NULL;
3321 if (stmt->expression->ctype == &null_ctype)
3322 stmt->expression = cast_to(stmt->expression, &ptr_ctype);
3323 return degenerate(stmt->expression);
3325 case STMT_COMPOUND: {
3326 struct statement *s;
3327 struct symbol *type = NULL;
3329 /* Evaluate the return symbol in the compound statement */
3330 evaluate_symbol(stmt->ret);
3333 * Then, evaluate each statement, making the type of the
3334 * compound statement be the type of the last statement
3336 type = evaluate_statement(stmt->args);
3337 FOR_EACH_PTR(stmt->stmts, s) {
3338 type = evaluate_statement(s);
3339 } END_FOR_EACH_PTR(s);
3340 if (!type)
3341 type = &void_ctype;
3342 return type;
3344 case STMT_IF:
3345 evaluate_if_statement(stmt);
3346 return NULL;
3347 case STMT_ITERATOR:
3348 evaluate_iterator(stmt);
3349 return NULL;
3350 case STMT_SWITCH:
3351 evaluate_switch_statement(stmt);
3352 return NULL;
3353 case STMT_CASE:
3354 evaluate_case_statement(stmt);
3355 return NULL;
3356 case STMT_LABEL:
3357 return evaluate_statement(stmt->label_statement);
3358 case STMT_GOTO:
3359 evaluate_expression(stmt->goto_expression);
3360 return NULL;
3361 case STMT_NONE:
3362 break;
3363 case STMT_ASM:
3364 evaluate_asm_statement(stmt);
3365 return NULL;
3366 case STMT_CONTEXT:
3367 evaluate_expression(stmt->expression);
3368 return NULL;
3369 case STMT_RANGE:
3370 evaluate_expression(stmt->range_expression);
3371 evaluate_expression(stmt->range_low);
3372 evaluate_expression(stmt->range_high);
3373 return NULL;
3375 return NULL;