Fix enumeration constants' scope beginning
[smatch.git] / evaluate.c
blob5c3812eb9984b5d053135ed3aab4dd2469f95930
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 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 | MOD_LONGLONG))
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 inline int is_function(struct symbol *type)
541 return type && type->type == SYM_FN;
544 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *itype)
546 struct expression *index = expr->right;
547 struct symbol *ctype, *base;
548 int multiply;
550 classify_type(degenerate(expr->left), &ctype);
551 base = examine_pointer_target(ctype);
553 if (!base) {
554 expression_error(expr, "missing type information");
555 return NULL;
557 if (is_function(base)) {
558 expression_error(expr, "arithmetics on pointers to functions");
559 return NULL;
562 /* Get the size of whatever the pointer points to */
563 multiply = is_void_type(base) ? 1 : bits_to_bytes(base->bit_size);
565 if (ctype == &null_ctype)
566 ctype = &ptr_ctype;
567 expr->ctype = ctype;
569 if (multiply == 1 && itype->bit_size >= bits_in_pointer)
570 return ctype;
572 if (index->type == EXPR_VALUE) {
573 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
574 unsigned long long v = index->value, mask;
575 mask = 1ULL << (itype->bit_size - 1);
576 if (v & mask)
577 v |= -mask;
578 else
579 v &= mask - 1;
580 v *= multiply;
581 mask = 1ULL << (bits_in_pointer - 1);
582 v &= mask | (mask - 1);
583 val->value = v;
584 val->ctype = ssize_t_ctype;
585 expr->right = val;
586 return ctype;
589 if (itype->bit_size < bits_in_pointer)
590 index = cast_to(index, ssize_t_ctype);
592 if (multiply > 1) {
593 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
594 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
596 val->ctype = ssize_t_ctype;
597 val->value = multiply;
599 mul->op = '*';
600 mul->ctype = ssize_t_ctype;
601 mul->left = index;
602 mul->right = val;
603 index = mul;
606 expr->right = index;
607 return ctype;
610 static void examine_fn_arguments(struct symbol *fn);
612 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
614 const char *type_difference(struct ctype *c1, struct ctype *c2,
615 unsigned long mod1, unsigned long mod2)
617 unsigned long as1 = c1->as, as2 = c2->as;
618 struct symbol *t1 = c1->base_type;
619 struct symbol *t2 = c2->base_type;
620 int move1 = 1, move2 = 1;
621 mod1 |= c1->modifiers;
622 mod2 |= c2->modifiers;
623 for (;;) {
624 unsigned long diff;
625 int type;
626 struct symbol *base1 = t1->ctype.base_type;
627 struct symbol *base2 = t2->ctype.base_type;
630 * FIXME! Collect alignment and context too here!
632 if (move1) {
633 if (t1 && t1->type != SYM_PTR) {
634 mod1 |= t1->ctype.modifiers;
635 as1 |= t1->ctype.as;
637 move1 = 0;
640 if (move2) {
641 if (t2 && t2->type != SYM_PTR) {
642 mod2 |= t2->ctype.modifiers;
643 as2 |= t2->ctype.as;
645 move2 = 0;
648 if (t1 == t2)
649 break;
650 if (!t1 || !t2)
651 return "different types";
653 if (t1->type == SYM_NODE || t1->type == SYM_ENUM) {
654 t1 = base1;
655 move1 = 1;
656 if (!t1)
657 return "bad types";
658 continue;
661 if (t2->type == SYM_NODE || t2->type == SYM_ENUM) {
662 t2 = base2;
663 move2 = 1;
664 if (!t2)
665 return "bad types";
666 continue;
669 move1 = move2 = 1;
670 type = t1->type;
671 if (type != t2->type)
672 return "different base types";
674 switch (type) {
675 default:
676 sparse_error(t1->pos,
677 "internal error: bad type in derived(%d)",
678 type);
679 return "bad types";
680 case SYM_RESTRICT:
681 return "different base types";
682 case SYM_UNION:
683 case SYM_STRUCT:
684 /* allow definition of incomplete structs and unions */
685 if (t1->ident == t2->ident)
686 return NULL;
687 return "different base types";
688 case SYM_ARRAY:
689 /* XXX: we ought to compare sizes */
690 break;
691 case SYM_PTR:
692 if (Waddress_space && as1 != as2)
693 return "different address spaces";
694 /* MOD_SPECIFIER is due to idiocy in parse.c */
695 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SPECIFIER)
696 return "different modifiers";
697 /* we could be lazier here */
698 base1 = examine_pointer_target(t1);
699 base2 = examine_pointer_target(t2);
700 mod1 = t1->ctype.modifiers;
701 as1 = t1->ctype.as;
702 mod2 = t2->ctype.modifiers;
703 as2 = t2->ctype.as;
704 break;
705 case SYM_FN: {
706 struct symbol *arg1, *arg2;
707 int i;
709 if (Waddress_space && as1 != as2)
710 return "different address spaces";
711 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
712 return "different modifiers";
713 mod1 = t1->ctype.modifiers;
714 as1 = t1->ctype.as;
715 mod2 = t2->ctype.modifiers;
716 as2 = t2->ctype.as;
718 if (base1->variadic != base2->variadic)
719 return "incompatible variadic arguments";
720 examine_fn_arguments(t1);
721 examine_fn_arguments(t2);
722 PREPARE_PTR_LIST(t1->arguments, arg1);
723 PREPARE_PTR_LIST(t2->arguments, arg2);
724 i = 1;
725 for (;;) {
726 const char *diffstr;
727 if (!arg1 && !arg2)
728 break;
729 if (!arg1 || !arg2)
730 return "different argument counts";
731 diffstr = type_difference(&arg1->ctype,
732 &arg2->ctype,
733 MOD_IGN, MOD_IGN);
734 if (diffstr) {
735 static char argdiff[80];
736 sprintf(argdiff, "incompatible argument %d (%s)", i, diffstr);
737 return argdiff;
739 NEXT_PTR_LIST(arg1);
740 NEXT_PTR_LIST(arg2);
741 i++;
743 FINISH_PTR_LIST(arg2);
744 FINISH_PTR_LIST(arg1);
745 break;
747 case SYM_BASETYPE:
748 if (Waddress_space && as1 != as2)
749 return "different address spaces";
750 if (base1 != base2)
751 return "different base types";
752 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
753 if (!diff)
754 return NULL;
755 if (diff & MOD_SIZE)
756 return "different type sizes";
757 else if (diff & ~MOD_SIGNEDNESS)
758 return "different modifiers";
759 else
760 return "different signedness";
762 t1 = base1;
763 t2 = base2;
765 if (Waddress_space && as1 != as2)
766 return "different address spaces";
767 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
768 return "different modifiers";
769 return NULL;
772 static void bad_null(struct expression *expr)
774 if (Wnon_pointer_null)
775 warning(expr->pos, "Using plain integer as NULL pointer");
778 static unsigned long target_qualifiers(struct symbol *type)
780 unsigned long mod = type->ctype.modifiers & MOD_IGN;
781 if (type->ctype.base_type && type->ctype.base_type->type == SYM_ARRAY)
782 mod = 0;
783 return mod;
786 static struct symbol *evaluate_ptr_sub(struct expression *expr)
788 const char *typediff;
789 struct symbol *ltype, *rtype;
790 struct expression *l = expr->left;
791 struct expression *r = expr->right;
792 struct symbol *lbase, *rbase;
794 classify_type(degenerate(l), &ltype);
795 classify_type(degenerate(r), &rtype);
797 lbase = examine_pointer_target(ltype);
798 rbase = examine_pointer_target(rtype);
799 typediff = type_difference(&ltype->ctype, &rtype->ctype,
800 target_qualifiers(rtype),
801 target_qualifiers(ltype));
802 if (typediff)
803 expression_error(expr, "subtraction of different types can't work (%s)", typediff);
805 if (is_function(lbase)) {
806 expression_error(expr, "subtraction of functions? Share your drugs");
807 return NULL;
810 expr->ctype = ssize_t_ctype;
811 if (lbase->bit_size > bits_in_char) {
812 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
813 struct expression *div = expr;
814 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
815 unsigned long value = bits_to_bytes(lbase->bit_size);
817 val->ctype = size_t_ctype;
818 val->value = value;
820 if (value & (value-1)) {
821 if (Wptr_subtraction_blows)
822 warning(expr->pos, "potentially expensive pointer subtraction");
825 sub->op = '-';
826 sub->ctype = ssize_t_ctype;
827 sub->left = l;
828 sub->right = r;
830 div->op = '/';
831 div->left = sub;
832 div->right = val;
835 return ssize_t_ctype;
838 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
840 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
842 struct symbol *ctype;
844 if (!expr)
845 return NULL;
847 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
848 warning(expr->pos, "assignment expression in conditional");
850 ctype = evaluate_expression(expr);
851 if (ctype) {
852 if (is_safe_type(ctype))
853 warning(expr->pos, "testing a 'safe expression'");
856 return ctype;
859 static struct symbol *evaluate_logical(struct expression *expr)
861 if (!evaluate_conditional(expr->left, 0))
862 return NULL;
863 if (!evaluate_conditional(expr->right, 0))
864 return NULL;
866 expr->ctype = &bool_ctype;
867 if (expr->flags) {
868 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
869 expr->flags = 0;
871 return &bool_ctype;
874 static struct symbol *evaluate_binop(struct expression *expr)
876 struct symbol *ltype, *rtype, *ctype;
877 int lclass = classify_type(expr->left->ctype, &ltype);
878 int rclass = classify_type(expr->right->ctype, &rtype);
879 int op = expr->op;
881 if (expr->flags) {
882 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
883 expr->flags = 0;
886 /* number op number */
887 if (lclass & rclass & TYPE_NUM) {
888 if ((lclass | rclass) & TYPE_FLOAT) {
889 switch (op) {
890 case '+': case '-': case '*': case '/':
891 break;
892 default:
893 return bad_expr_type(expr);
897 if (op == SPECIAL_LEFTSHIFT || op == SPECIAL_RIGHTSHIFT) {
898 // shifts do integer promotions, but that's it.
899 unrestrict(expr->left, lclass, &ltype);
900 unrestrict(expr->right, rclass, &rtype);
901 ctype = ltype = integer_promotion(ltype);
902 rtype = integer_promotion(rtype);
903 } else {
904 // The rest do usual conversions
905 const unsigned left_not = expr->left->type == EXPR_PREOP
906 && expr->left->op == '!';
907 const unsigned right_not = expr->right->type == EXPR_PREOP
908 && expr->right->op == '!';
909 if ((op == '&' || op == '|') && (left_not || right_not))
910 warning(expr->pos, "dubious: %sx %c %sy",
911 left_not ? "!" : "",
913 right_not ? "!" : "");
915 ltype = usual_conversions(op, expr->left, expr->right,
916 lclass, rclass, ltype, rtype);
917 ctype = rtype = ltype;
920 expr->left = cast_to(expr->left, ltype);
921 expr->right = cast_to(expr->right, rtype);
922 expr->ctype = ctype;
923 return ctype;
926 /* pointer (+|-) integer */
927 if (lclass & TYPE_PTR && is_int(rclass) && (op == '+' || op == '-')) {
928 unrestrict(expr->right, rclass, &rtype);
929 return evaluate_ptr_add(expr, rtype);
932 /* integer + pointer */
933 if (rclass & TYPE_PTR && is_int(lclass) && op == '+') {
934 struct expression *index = expr->left;
935 unrestrict(index, lclass, &ltype);
936 expr->left = expr->right;
937 expr->right = index;
938 return evaluate_ptr_add(expr, ltype);
941 /* pointer - pointer */
942 if (lclass & rclass & TYPE_PTR && expr->op == '-')
943 return evaluate_ptr_sub(expr);
945 return bad_expr_type(expr);
948 static struct symbol *evaluate_comma(struct expression *expr)
950 expr->ctype = degenerate(expr->right);
951 if (expr->ctype == &null_ctype)
952 expr->ctype = &ptr_ctype;
953 expr->flags &= expr->left->flags & expr->right->flags;
954 return expr->ctype;
957 static int modify_for_unsigned(int op)
959 if (op == '<')
960 op = SPECIAL_UNSIGNED_LT;
961 else if (op == '>')
962 op = SPECIAL_UNSIGNED_GT;
963 else if (op == SPECIAL_LTE)
964 op = SPECIAL_UNSIGNED_LTE;
965 else if (op == SPECIAL_GTE)
966 op = SPECIAL_UNSIGNED_GTE;
967 return op;
970 static inline int is_null_pointer_constant(struct expression *e)
972 if (e->ctype == &null_ctype)
973 return 1;
974 if (!(e->flags & Int_const_expr))
975 return 0;
976 return is_zero_constant(e) ? 2 : 0;
979 static struct symbol *evaluate_compare(struct expression *expr)
981 struct expression *left = expr->left, *right = expr->right;
982 struct symbol *ltype, *rtype, *lbase, *rbase;
983 int lclass = classify_type(degenerate(left), &ltype);
984 int rclass = classify_type(degenerate(right), &rtype);
985 struct symbol *ctype;
986 const char *typediff;
988 if (expr->flags) {
989 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
990 expr->flags = 0;
993 /* Type types? */
994 if (is_type_type(ltype) && is_type_type(rtype))
995 goto OK;
997 if (is_safe_type(left->ctype) || is_safe_type(right->ctype))
998 warning(expr->pos, "testing a 'safe expression'");
1000 /* number on number */
1001 if (lclass & rclass & TYPE_NUM) {
1002 ctype = usual_conversions(expr->op, expr->left, expr->right,
1003 lclass, rclass, ltype, rtype);
1004 expr->left = cast_to(expr->left, ctype);
1005 expr->right = cast_to(expr->right, ctype);
1006 if (ctype->ctype.modifiers & MOD_UNSIGNED)
1007 expr->op = modify_for_unsigned(expr->op);
1008 goto OK;
1011 /* at least one must be a pointer */
1012 if (!((lclass | rclass) & TYPE_PTR))
1013 return bad_expr_type(expr);
1015 /* equality comparisons can be with null pointer constants */
1016 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1017 int is_null1 = is_null_pointer_constant(left);
1018 int is_null2 = is_null_pointer_constant(right);
1019 if (is_null1 == 2)
1020 bad_null(left);
1021 if (is_null2 == 2)
1022 bad_null(right);
1023 if (is_null1 && is_null2) {
1024 int positive = expr->op == SPECIAL_EQUAL;
1025 expr->type = EXPR_VALUE;
1026 expr->value = positive;
1027 goto OK;
1029 if (is_null1 && (rclass & TYPE_PTR)) {
1030 left = cast_to(left, rtype);
1031 goto OK;
1033 if (is_null2 && (lclass & TYPE_PTR)) {
1034 right = cast_to(right, ltype);
1035 goto OK;
1038 /* both should be pointers */
1039 if (!(lclass & rclass & TYPE_PTR))
1040 return bad_expr_type(expr);
1041 expr->op = modify_for_unsigned(expr->op);
1043 lbase = examine_pointer_target(ltype);
1044 rbase = examine_pointer_target(rtype);
1046 /* they also have special treatment for pointers to void */
1047 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1048 if (ltype->ctype.as == rtype->ctype.as) {
1049 if (lbase == &void_ctype) {
1050 right = cast_to(right, ltype);
1051 goto OK;
1053 if (rbase == &void_ctype) {
1054 left = cast_to(left, rtype);
1055 goto OK;
1060 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1061 target_qualifiers(rtype),
1062 target_qualifiers(ltype));
1063 if (!typediff)
1064 goto OK;
1066 expression_error(expr, "incompatible types in comparison expression (%s)", typediff);
1067 return NULL;
1070 expr->ctype = &bool_ctype;
1071 return &bool_ctype;
1075 * NOTE! The degenerate case of "x ? : y", where we don't
1076 * have a true case, this will possibly promote "x" to the
1077 * same type as "y", and thus _change_ the conditional
1078 * test in the expression. But since promotion is "safe"
1079 * for testing, that's OK.
1081 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1083 struct expression **true;
1084 struct symbol *ctype, *ltype, *rtype, *lbase, *rbase;
1085 int lclass, rclass;
1086 const char * typediff;
1087 int qual;
1089 if (!evaluate_conditional(expr->conditional, 0))
1090 return NULL;
1091 if (!evaluate_expression(expr->cond_false))
1092 return NULL;
1094 ctype = degenerate(expr->conditional);
1095 rtype = degenerate(expr->cond_false);
1097 true = &expr->conditional;
1098 ltype = ctype;
1099 if (expr->cond_true) {
1100 if (!evaluate_expression(expr->cond_true))
1101 return NULL;
1102 ltype = degenerate(expr->cond_true);
1103 true = &expr->cond_true;
1106 if (expr->flags) {
1107 int flags = expr->conditional->flags & Int_const_expr;
1108 flags &= (*true)->flags & expr->cond_false->flags;
1109 if (!flags)
1110 expr->flags = 0;
1113 lclass = classify_type(ltype, &ltype);
1114 rclass = classify_type(rtype, &rtype);
1115 if (lclass & rclass & TYPE_NUM) {
1116 ctype = usual_conversions('?', *true, expr->cond_false,
1117 lclass, rclass, ltype, rtype);
1118 *true = cast_to(*true, ctype);
1119 expr->cond_false = cast_to(expr->cond_false, ctype);
1120 goto out;
1123 if ((lclass | rclass) & TYPE_PTR) {
1124 int is_null1 = is_null_pointer_constant(*true);
1125 int is_null2 = is_null_pointer_constant(expr->cond_false);
1127 if (is_null1 && is_null2) {
1128 *true = cast_to(*true, &ptr_ctype);
1129 expr->cond_false = cast_to(expr->cond_false, &ptr_ctype);
1130 ctype = &ptr_ctype;
1131 goto out;
1133 if (is_null1 && (rclass & TYPE_PTR)) {
1134 if (is_null1 == 2)
1135 bad_null(*true);
1136 *true = cast_to(*true, rtype);
1137 ctype = rtype;
1138 goto out;
1140 if (is_null2 && (lclass & TYPE_PTR)) {
1141 if (is_null2 == 2)
1142 bad_null(expr->cond_false);
1143 expr->cond_false = cast_to(expr->cond_false, ltype);
1144 ctype = ltype;
1145 goto out;
1147 if (!(lclass & rclass & TYPE_PTR)) {
1148 typediff = "different types";
1149 goto Err;
1151 /* OK, it's pointer on pointer */
1152 if (ltype->ctype.as != rtype->ctype.as) {
1153 typediff = "different address spaces";
1154 goto Err;
1157 /* need to be lazier here */
1158 lbase = examine_pointer_target(ltype);
1159 rbase = examine_pointer_target(rtype);
1160 qual = target_qualifiers(ltype) | target_qualifiers(rtype);
1162 if (lbase == &void_ctype) {
1163 /* XXX: pointers to function should warn here */
1164 ctype = ltype;
1165 goto Qual;
1168 if (rbase == &void_ctype) {
1169 /* XXX: pointers to function should warn here */
1170 ctype = rtype;
1171 goto Qual;
1173 /* XXX: that should be pointer to composite */
1174 ctype = ltype;
1175 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1176 qual, qual);
1177 if (!typediff)
1178 goto Qual;
1179 goto Err;
1182 /* void on void, struct on same struct, union on same union */
1183 if (ltype == rtype) {
1184 ctype = ltype;
1185 goto out;
1187 typediff = "different base types";
1189 Err:
1190 expression_error(expr, "incompatible types in conditional expression (%s)", typediff);
1191 return NULL;
1193 out:
1194 expr->ctype = ctype;
1195 return ctype;
1197 Qual:
1198 if (qual & ~ctype->ctype.modifiers) {
1199 struct symbol *sym = alloc_symbol(ctype->pos, SYM_PTR);
1200 *sym = *ctype;
1201 sym->ctype.modifiers |= qual;
1202 ctype = sym;
1204 *true = cast_to(*true, ctype);
1205 expr->cond_false = cast_to(expr->cond_false, ctype);
1206 goto out;
1209 /* FP assignments can not do modulo or bit operations */
1210 static int compatible_float_op(int op)
1212 return op == SPECIAL_ADD_ASSIGN ||
1213 op == SPECIAL_SUB_ASSIGN ||
1214 op == SPECIAL_MUL_ASSIGN ||
1215 op == SPECIAL_DIV_ASSIGN;
1218 static int evaluate_assign_op(struct expression *expr)
1220 struct symbol *target = expr->left->ctype;
1221 struct symbol *source = expr->right->ctype;
1222 struct symbol *t, *s;
1223 int tclass = classify_type(target, &t);
1224 int sclass = classify_type(source, &s);
1225 int op = expr->op;
1227 if (tclass & sclass & TYPE_NUM) {
1228 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1229 expression_error(expr, "invalid assignment");
1230 return 0;
1232 if (tclass & TYPE_RESTRICT) {
1233 if (!restricted_binop(op, t)) {
1234 warning(expr->pos, "bad assignment (%s) to %s",
1235 show_special(op), show_typename(t));
1236 expr->right = cast_to(expr->right, target);
1237 return 0;
1239 /* allowed assignments unfoul */
1240 if (sclass & TYPE_FOULED && unfoul(s) == t)
1241 goto Cast;
1242 if (!restricted_value(expr->right, t))
1243 return 1;
1244 } else if (!(sclass & TYPE_RESTRICT))
1245 goto Cast;
1246 /* source and target would better be identical restricted */
1247 if (t == s)
1248 return 1;
1249 warning(expr->pos, "invalid assignment: %s", show_special(op));
1250 info(expr->pos, " left side has type %s", show_typename(t));
1251 info(expr->pos, " right side has type %s", show_typename(s));
1252 expr->right = cast_to(expr->right, target);
1253 return 0;
1255 if (tclass == TYPE_PTR && is_int(sclass)) {
1256 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1257 unrestrict(expr->right, sclass, &s);
1258 evaluate_ptr_add(expr, s);
1259 return 1;
1261 expression_error(expr, "invalid pointer assignment");
1262 return 0;
1265 expression_error(expr, "invalid assignment");
1266 return 0;
1268 Cast:
1269 expr->right = cast_to(expr->right, target);
1270 return 1;
1273 static int whitelist_pointers(struct symbol *t1, struct symbol *t2)
1275 if (t1 == t2)
1276 return 0; /* yes, 0 - we don't want a cast_to here */
1277 if (t1 == &void_ctype)
1278 return 1;
1279 if (t2 == &void_ctype)
1280 return 1;
1281 if (classify_type(t1, &t1) != TYPE_NUM)
1282 return 0;
1283 if (classify_type(t2, &t2) != TYPE_NUM)
1284 return 0;
1285 if (t1 == t2)
1286 return 1;
1287 if (t1->ctype.modifiers & t2->ctype.modifiers & MOD_CHAR)
1288 return 1;
1289 if ((t1->ctype.modifiers ^ t2->ctype.modifiers) & MOD_SIZE)
1290 return 0;
1291 return !Wtypesign;
1294 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1295 struct expression **rp, const char *where)
1297 const char *typediff;
1298 struct symbol *source = degenerate(*rp);
1299 struct symbol *t, *s;
1300 int tclass = classify_type(target, &t);
1301 int sclass = classify_type(source, &s);
1303 if (tclass & sclass & TYPE_NUM) {
1304 if (tclass & TYPE_RESTRICT) {
1305 /* allowed assignments unfoul */
1306 if (sclass & TYPE_FOULED && unfoul(s) == t)
1307 goto Cast;
1308 if (!restricted_value(*rp, target))
1309 return 1;
1310 if (s == t)
1311 return 1;
1312 } else if (!(sclass & TYPE_RESTRICT))
1313 goto Cast;
1314 typediff = "different base types";
1315 goto Err;
1318 if (tclass == TYPE_PTR) {
1319 unsigned long mod1, mod2;
1320 struct symbol *b1, *b2;
1321 // NULL pointer is always OK
1322 int is_null = is_null_pointer_constant(*rp);
1323 if (is_null) {
1324 if (is_null == 2)
1325 bad_null(*rp);
1326 goto Cast;
1328 if (!(sclass & TYPE_PTR)) {
1329 typediff = "different base types";
1330 goto Err;
1332 b1 = examine_pointer_target(t);
1333 b2 = examine_pointer_target(s);
1334 mod1 = target_qualifiers(t);
1335 mod2 = target_qualifiers(s);
1336 if (whitelist_pointers(b1, b2)) {
1338 * assignments to/from void * are OK, provided that
1339 * we do not remove qualifiers from pointed to [C]
1340 * or mix address spaces [sparse].
1342 if (t->ctype.as != s->ctype.as) {
1343 typediff = "different address spaces";
1344 goto Err;
1346 if (mod2 & ~mod1) {
1347 typediff = "different modifiers";
1348 goto Err;
1350 goto Cast;
1352 /* It's OK if the target is more volatile or const than the source */
1353 typediff = type_difference(&t->ctype, &s->ctype, 0, mod1);
1354 if (typediff)
1355 goto Err;
1356 return 1;
1359 if ((tclass & TYPE_COMPOUND) && s == t)
1360 return 1;
1362 if (tclass & TYPE_NUM) {
1363 /* XXX: need to turn into comparison with NULL */
1364 if (t == &bool_ctype && (sclass & TYPE_PTR))
1365 goto Cast;
1366 typediff = "different base types";
1367 goto Err;
1369 typediff = "invalid types";
1371 Err:
1372 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1373 info(expr->pos, " expected %s", show_typename(target));
1374 info(expr->pos, " got %s", show_typename(source));
1375 *rp = cast_to(*rp, target);
1376 return 0;
1377 Cast:
1378 *rp = cast_to(*rp, target);
1379 return 1;
1382 static void mark_assigned(struct expression *expr)
1384 struct symbol *sym;
1386 if (!expr)
1387 return;
1388 switch (expr->type) {
1389 case EXPR_SYMBOL:
1390 sym = expr->symbol;
1391 if (!sym)
1392 return;
1393 if (sym->type != SYM_NODE)
1394 return;
1395 sym->ctype.modifiers |= MOD_ASSIGNED;
1396 return;
1398 case EXPR_BINOP:
1399 mark_assigned(expr->left);
1400 mark_assigned(expr->right);
1401 return;
1402 case EXPR_CAST:
1403 case EXPR_FORCE_CAST:
1404 mark_assigned(expr->cast_expression);
1405 return;
1406 case EXPR_SLICE:
1407 mark_assigned(expr->base);
1408 return;
1409 default:
1410 /* Hmm? */
1411 return;
1415 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1417 if (type->ctype.modifiers & MOD_CONST)
1418 expression_error(left, "assignment to const expression");
1420 /* We know left is an lvalue, so it's a "preop-*" */
1421 mark_assigned(left->unop);
1424 static struct symbol *evaluate_assignment(struct expression *expr)
1426 struct expression *left = expr->left;
1427 struct expression *where = expr;
1428 struct symbol *ltype;
1430 if (!lvalue_expression(left)) {
1431 expression_error(expr, "not an lvalue");
1432 return NULL;
1435 ltype = left->ctype;
1437 if (expr->op != '=') {
1438 if (!evaluate_assign_op(expr))
1439 return NULL;
1440 } else {
1441 if (!compatible_assignment_types(where, ltype, &expr->right, "assignment"))
1442 return NULL;
1445 evaluate_assign_to(left, ltype);
1447 expr->ctype = ltype;
1448 return ltype;
1451 static void examine_fn_arguments(struct symbol *fn)
1453 struct symbol *s;
1455 FOR_EACH_PTR(fn->arguments, s) {
1456 struct symbol *arg = evaluate_symbol(s);
1457 /* Array/function arguments silently degenerate into pointers */
1458 if (arg) {
1459 struct symbol *ptr;
1460 switch(arg->type) {
1461 case SYM_ARRAY:
1462 case SYM_FN:
1463 ptr = alloc_symbol(s->pos, SYM_PTR);
1464 if (arg->type == SYM_ARRAY)
1465 ptr->ctype = arg->ctype;
1466 else
1467 ptr->ctype.base_type = arg;
1468 ptr->ctype.as |= s->ctype.as;
1469 ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1471 s->ctype.base_type = ptr;
1472 s->ctype.as = 0;
1473 s->ctype.modifiers &= ~MOD_PTRINHERIT;
1474 s->bit_size = 0;
1475 s->examined = 0;
1476 examine_symbol_type(s);
1477 break;
1478 default:
1479 /* nothing */
1480 break;
1483 } END_FOR_EACH_PTR(s);
1486 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1488 /* Take the modifiers of the pointer, and apply them to the member */
1489 mod |= sym->ctype.modifiers;
1490 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1491 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1492 *newsym = *sym;
1493 newsym->ctype.as = as;
1494 newsym->ctype.modifiers = mod;
1495 sym = newsym;
1497 return sym;
1500 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1502 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1503 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1505 node->ctype.base_type = ptr;
1506 ptr->bit_size = bits_in_pointer;
1507 ptr->ctype.alignment = pointer_alignment;
1509 node->bit_size = bits_in_pointer;
1510 node->ctype.alignment = pointer_alignment;
1512 access_symbol(sym);
1513 if (sym->ctype.modifiers & MOD_REGISTER) {
1514 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1515 sym->ctype.modifiers &= ~MOD_REGISTER;
1517 if (sym->type == SYM_NODE) {
1518 ptr->ctype.as |= sym->ctype.as;
1519 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1520 sym = sym->ctype.base_type;
1522 if (degenerate && sym->type == SYM_ARRAY) {
1523 ptr->ctype.as |= sym->ctype.as;
1524 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1525 sym = sym->ctype.base_type;
1527 ptr->ctype.base_type = sym;
1529 return node;
1532 /* Arrays degenerate into pointers on pointer arithmetic */
1533 static struct symbol *degenerate(struct expression *expr)
1535 struct symbol *ctype, *base;
1537 if (!expr)
1538 return NULL;
1539 ctype = expr->ctype;
1540 if (!ctype)
1541 return NULL;
1542 base = examine_symbol_type(ctype);
1543 if (ctype->type == SYM_NODE)
1544 base = ctype->ctype.base_type;
1546 * Arrays degenerate into pointers to the entries, while
1547 * functions degenerate into pointers to themselves.
1548 * If array was part of non-lvalue compound, we create a copy
1549 * of that compound first and then act as if we were dealing with
1550 * the corresponding field in there.
1552 switch (base->type) {
1553 case SYM_ARRAY:
1554 if (expr->type == EXPR_SLICE) {
1555 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1556 struct expression *e0, *e1, *e2, *e3, *e4;
1558 a->ctype.base_type = expr->base->ctype;
1559 a->bit_size = expr->base->ctype->bit_size;
1560 a->array_size = expr->base->ctype->array_size;
1562 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1563 e0->symbol = a;
1564 e0->ctype = &lazy_ptr_ctype;
1566 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1567 e1->unop = e0;
1568 e1->op = '*';
1569 e1->ctype = expr->base->ctype; /* XXX */
1571 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1572 e2->left = e1;
1573 e2->right = expr->base;
1574 e2->op = '=';
1575 e2->ctype = expr->base->ctype;
1577 if (expr->r_bitpos) {
1578 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1579 e3->op = '+';
1580 e3->left = e0;
1581 e3->right = alloc_const_expression(expr->pos,
1582 bits_to_bytes(expr->r_bitpos));
1583 e3->ctype = &lazy_ptr_ctype;
1584 } else {
1585 e3 = e0;
1588 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1589 e4->left = e2;
1590 e4->right = e3;
1591 e4->ctype = &lazy_ptr_ctype;
1593 expr->unop = e4;
1594 expr->type = EXPR_PREOP;
1595 expr->op = '*';
1597 case SYM_FN:
1598 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1599 expression_error(expr, "strange non-value function or array");
1600 return &bad_ctype;
1602 *expr = *expr->unop;
1603 ctype = create_pointer(expr, ctype, 1);
1604 expr->ctype = ctype;
1605 default:
1606 /* nothing */;
1608 return ctype;
1611 static struct symbol *evaluate_addressof(struct expression *expr)
1613 struct expression *op = expr->unop;
1614 struct symbol *ctype;
1616 if (op->op != '*' || op->type != EXPR_PREOP) {
1617 expression_error(expr, "not addressable");
1618 return NULL;
1620 ctype = op->ctype;
1621 *expr = *op->unop;
1622 expr->flags = 0;
1624 if (expr->type == EXPR_SYMBOL) {
1625 struct symbol *sym = expr->symbol;
1626 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1630 * symbol expression evaluation is lazy about the type
1631 * of the sub-expression, so we may have to generate
1632 * the type here if so..
1634 if (expr->ctype == &lazy_ptr_ctype) {
1635 ctype = create_pointer(expr, ctype, 0);
1636 expr->ctype = ctype;
1638 return expr->ctype;
1642 static struct symbol *evaluate_dereference(struct expression *expr)
1644 struct expression *op = expr->unop;
1645 struct symbol *ctype = op->ctype, *node, *target;
1647 /* Simplify: *&(expr) => (expr) */
1648 if (op->type == EXPR_PREOP && op->op == '&') {
1649 *expr = *op->unop;
1650 expr->flags = 0;
1651 return expr->ctype;
1654 /* Dereferencing a node drops all the node information. */
1655 if (ctype->type == SYM_NODE)
1656 ctype = ctype->ctype.base_type;
1658 node = alloc_symbol(expr->pos, SYM_NODE);
1659 target = ctype->ctype.base_type;
1661 switch (ctype->type) {
1662 default:
1663 expression_error(expr, "cannot dereference this type");
1664 return NULL;
1665 case SYM_PTR:
1666 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1667 merge_type(node, ctype);
1668 break;
1670 case SYM_ARRAY:
1671 if (!lvalue_expression(op)) {
1672 expression_error(op, "non-lvalue array??");
1673 return NULL;
1676 /* Do the implied "addressof" on the array */
1677 *op = *op->unop;
1680 * When an array is dereferenced, we need to pick
1681 * up the attributes of the original node too..
1683 merge_type(node, op->ctype);
1684 merge_type(node, ctype);
1685 break;
1688 node->bit_size = target->bit_size;
1689 node->array_size = target->array_size;
1691 expr->ctype = node;
1692 return node;
1696 * Unary post-ops: x++ and x--
1698 static struct symbol *evaluate_postop(struct expression *expr)
1700 struct expression *op = expr->unop;
1701 struct symbol *ctype = op->ctype;
1702 int class = classify_type(op->ctype, &ctype);
1703 int multiply = 0;
1705 if (!lvalue_expression(expr->unop)) {
1706 expression_error(expr, "need lvalue expression for ++/--");
1707 return NULL;
1710 if ((class & TYPE_RESTRICT) && restricted_unop(expr->op, &ctype))
1711 return bad_expr_type(expr);
1713 if (class & TYPE_NUM) {
1714 multiply = 1;
1715 } else if (class == TYPE_PTR) {
1716 struct symbol *target = examine_pointer_target(ctype);
1717 if (!is_function(target))
1718 multiply = bits_to_bytes(target->bit_size);
1721 if (multiply) {
1722 evaluate_assign_to(op, op->ctype);
1723 expr->op_value = multiply;
1724 expr->ctype = ctype;
1725 return ctype;
1728 expression_error(expr, "bad argument type for ++/--");
1729 return NULL;
1732 static struct symbol *evaluate_sign(struct expression *expr)
1734 struct symbol *ctype = expr->unop->ctype;
1735 int class = classify_type(ctype, &ctype);
1736 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1737 expr->flags = 0;
1738 /* should be an arithmetic type */
1739 if (!(class & TYPE_NUM))
1740 return bad_expr_type(expr);
1741 if (!(class & (TYPE_FLOAT|TYPE_RESTRICT))) {
1742 struct symbol *rtype = integer_promotion(ctype);
1743 expr->unop = cast_to(expr->unop, rtype);
1744 ctype = rtype;
1745 } else if ((class & TYPE_FLOAT) && expr->op != '~') {
1746 /* no conversions needed */
1747 } else if ((class & TYPE_RESTRICT) && !restricted_unop(expr->op, &ctype)) {
1748 /* no conversions needed */
1749 } else {
1750 return bad_expr_type(expr);
1752 if (expr->op == '+')
1753 *expr = *expr->unop;
1754 expr->ctype = ctype;
1755 return ctype;
1758 static struct symbol *evaluate_preop(struct expression *expr)
1760 struct symbol *ctype = expr->unop->ctype;
1762 switch (expr->op) {
1763 case '(':
1764 *expr = *expr->unop;
1765 return ctype;
1767 case '+':
1768 case '-':
1769 case '~':
1770 return evaluate_sign(expr);
1772 case '*':
1773 return evaluate_dereference(expr);
1775 case '&':
1776 return evaluate_addressof(expr);
1778 case SPECIAL_INCREMENT:
1779 case SPECIAL_DECREMENT:
1781 * From a type evaluation standpoint the preops are
1782 * the same as the postops
1784 return evaluate_postop(expr);
1786 case '!':
1787 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1788 expr->flags = 0;
1789 if (is_safe_type(ctype))
1790 warning(expr->pos, "testing a 'safe expression'");
1791 if (is_float_type(ctype)) {
1792 struct expression *arg = expr->unop;
1793 expr->type = EXPR_BINOP;
1794 expr->op = SPECIAL_EQUAL;
1795 expr->left = arg;
1796 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1797 expr->right->ctype = ctype;
1798 expr->right->fvalue = 0;
1799 } else if (is_fouled_type(ctype)) {
1800 warning(expr->pos, "%s degrades to integer",
1801 show_typename(ctype->ctype.base_type));
1803 ctype = &bool_ctype;
1804 break;
1806 default:
1807 break;
1809 expr->ctype = ctype;
1810 return &bool_ctype;
1813 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1815 struct ptr_list *head = (struct ptr_list *)_list;
1816 struct ptr_list *list = head;
1818 if (!head)
1819 return NULL;
1820 do {
1821 int i;
1822 for (i = 0; i < list->nr; i++) {
1823 struct symbol *sym = (struct symbol *) list->list[i];
1824 if (sym->ident) {
1825 if (sym->ident != ident)
1826 continue;
1827 *offset = sym->offset;
1828 return sym;
1829 } else {
1830 struct symbol *ctype = sym->ctype.base_type;
1831 struct symbol *sub;
1832 if (!ctype)
1833 continue;
1834 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1835 continue;
1836 sub = find_identifier(ident, ctype->symbol_list, offset);
1837 if (!sub)
1838 continue;
1839 *offset += sym->offset;
1840 return sub;
1843 } while ((list = list->next) != head);
1844 return NULL;
1847 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1849 struct expression *add;
1852 * Create a new add-expression
1854 * NOTE! Even if we just add zero, we need a new node
1855 * for the member pointer, since it has a different
1856 * type than the original pointer. We could make that
1857 * be just a cast, but the fact is, a node is a node,
1858 * so we might as well just do the "add zero" here.
1860 add = alloc_expression(expr->pos, EXPR_BINOP);
1861 add->op = '+';
1862 add->left = expr;
1863 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1864 add->right->ctype = &int_ctype;
1865 add->right->value = offset;
1868 * The ctype of the pointer will be lazily evaluated if
1869 * we ever take the address of this member dereference..
1871 add->ctype = &lazy_ptr_ctype;
1872 return add;
1875 /* structure/union dereference */
1876 static struct symbol *evaluate_member_dereference(struct expression *expr)
1878 int offset;
1879 struct symbol *ctype, *member;
1880 struct expression *deref = expr->deref, *add;
1881 struct ident *ident = expr->member;
1882 unsigned int mod;
1883 int address_space;
1885 if (!evaluate_expression(deref))
1886 return NULL;
1887 if (!ident) {
1888 expression_error(expr, "bad member name");
1889 return NULL;
1892 ctype = deref->ctype;
1893 examine_symbol_type(ctype);
1894 address_space = ctype->ctype.as;
1895 mod = ctype->ctype.modifiers;
1896 if (ctype->type == SYM_NODE) {
1897 ctype = ctype->ctype.base_type;
1898 address_space |= ctype->ctype.as;
1899 mod |= ctype->ctype.modifiers;
1901 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1902 expression_error(expr, "expected structure or union");
1903 return NULL;
1905 offset = 0;
1906 member = find_identifier(ident, ctype->symbol_list, &offset);
1907 if (!member) {
1908 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1909 const char *name = "<unnamed>";
1910 int namelen = 9;
1911 if (ctype->ident) {
1912 name = ctype->ident->name;
1913 namelen = ctype->ident->len;
1915 if (ctype->symbol_list)
1916 expression_error(expr, "no member '%s' in %s %.*s",
1917 show_ident(ident), type, namelen, name);
1918 else
1919 expression_error(expr, "using member '%s' in "
1920 "incomplete %s %.*s", show_ident(ident),
1921 type, namelen, name);
1922 return NULL;
1926 * The member needs to take on the address space and modifiers of
1927 * the "parent" type.
1929 member = convert_to_as_mod(member, address_space, mod);
1930 ctype = get_base_type(member);
1932 if (!lvalue_expression(deref)) {
1933 if (deref->type != EXPR_SLICE) {
1934 expr->base = deref;
1935 expr->r_bitpos = 0;
1936 } else {
1937 expr->base = deref->base;
1938 expr->r_bitpos = deref->r_bitpos;
1940 expr->r_bitpos += bytes_to_bits(offset);
1941 expr->type = EXPR_SLICE;
1942 expr->r_nrbits = member->bit_size;
1943 expr->r_bitpos += member->bit_offset;
1944 expr->ctype = member;
1945 return member;
1948 deref = deref->unop;
1949 expr->deref = deref;
1951 add = evaluate_offset(deref, offset);
1952 expr->type = EXPR_PREOP;
1953 expr->op = '*';
1954 expr->unop = add;
1956 expr->ctype = member;
1957 return member;
1960 static int is_promoted(struct expression *expr)
1962 while (1) {
1963 switch (expr->type) {
1964 case EXPR_BINOP:
1965 case EXPR_SELECT:
1966 case EXPR_CONDITIONAL:
1967 return 1;
1968 case EXPR_COMMA:
1969 expr = expr->right;
1970 continue;
1971 case EXPR_PREOP:
1972 switch (expr->op) {
1973 case '(':
1974 expr = expr->unop;
1975 continue;
1976 case '+':
1977 case '-':
1978 case '~':
1979 return 1;
1980 default:
1981 return 0;
1983 default:
1984 return 0;
1990 static struct symbol *evaluate_cast(struct expression *);
1992 static struct symbol *evaluate_type_information(struct expression *expr)
1994 struct symbol *sym = expr->cast_type;
1995 if (!sym) {
1996 sym = evaluate_expression(expr->cast_expression);
1997 if (!sym)
1998 return NULL;
2000 * Expressions of restricted types will possibly get
2001 * promoted - check that here
2003 if (is_restricted_type(sym)) {
2004 if (sym->bit_size < bits_in_int && is_promoted(expr))
2005 sym = &int_ctype;
2006 } else if (is_fouled_type(sym)) {
2007 sym = &int_ctype;
2010 examine_symbol_type(sym);
2011 if (is_bitfield_type(sym)) {
2012 expression_error(expr, "trying to examine bitfield type");
2013 return NULL;
2015 return sym;
2018 static struct symbol *evaluate_sizeof(struct expression *expr)
2020 struct symbol *type;
2021 int size;
2023 type = evaluate_type_information(expr);
2024 if (!type)
2025 return NULL;
2027 size = type->bit_size;
2029 if (size < 0 && is_void_type(type)) {
2030 warning(expr->pos, "expression using sizeof(void)");
2031 size = bits_in_char;
2034 if (is_function(type->ctype.base_type)) {
2035 warning(expr->pos, "expression using sizeof on a function");
2036 size = bits_in_char;
2039 if ((size < 0) || (size & (bits_in_char - 1)))
2040 expression_error(expr, "cannot size expression");
2042 expr->type = EXPR_VALUE;
2043 expr->value = bits_to_bytes(size);
2044 expr->taint = 0;
2045 expr->ctype = size_t_ctype;
2046 return size_t_ctype;
2049 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
2051 struct symbol *type;
2052 int size;
2054 type = evaluate_type_information(expr);
2055 if (!type)
2056 return NULL;
2058 if (type->type == SYM_NODE)
2059 type = type->ctype.base_type;
2060 if (!type)
2061 return NULL;
2062 switch (type->type) {
2063 case SYM_ARRAY:
2064 break;
2065 case SYM_PTR:
2066 type = get_base_type(type);
2067 if (type)
2068 break;
2069 default:
2070 expression_error(expr, "expected pointer expression");
2071 return NULL;
2073 size = type->bit_size;
2074 if (size & (bits_in_char-1))
2075 size = 0;
2076 expr->type = EXPR_VALUE;
2077 expr->value = bits_to_bytes(size);
2078 expr->taint = 0;
2079 expr->ctype = size_t_ctype;
2080 return size_t_ctype;
2083 static struct symbol *evaluate_alignof(struct expression *expr)
2085 struct symbol *type;
2087 type = evaluate_type_information(expr);
2088 if (!type)
2089 return NULL;
2091 expr->type = EXPR_VALUE;
2092 expr->value = type->ctype.alignment;
2093 expr->taint = 0;
2094 expr->ctype = size_t_ctype;
2095 return size_t_ctype;
2098 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
2100 struct expression *expr;
2101 struct symbol_list *argument_types = fn->arguments;
2102 struct symbol *argtype;
2103 int i = 1;
2105 PREPARE_PTR_LIST(argument_types, argtype);
2106 FOR_EACH_PTR (head, expr) {
2107 struct expression **p = THIS_ADDRESS(expr);
2108 struct symbol *ctype, *target;
2109 ctype = evaluate_expression(expr);
2111 if (!ctype)
2112 return 0;
2114 target = argtype;
2115 if (!target) {
2116 struct symbol *type;
2117 int class = classify_type(ctype, &type);
2118 if (is_int(class)) {
2119 *p = cast_to(expr, integer_promotion(type));
2120 } else if (class & TYPE_FLOAT) {
2121 unsigned long mod = type->ctype.modifiers;
2122 if (!(mod & (MOD_LONG|MOD_LONGLONG)))
2123 *p = cast_to(expr, &double_ctype);
2124 } else if (class & TYPE_PTR) {
2125 if (expr->ctype == &null_ctype)
2126 *p = cast_to(expr, &ptr_ctype);
2127 else
2128 degenerate(expr);
2130 } else {
2131 static char where[30];
2132 examine_symbol_type(target);
2133 sprintf(where, "argument %d", i);
2134 compatible_assignment_types(expr, target, p, where);
2137 i++;
2138 NEXT_PTR_LIST(argtype);
2139 } END_FOR_EACH_PTR(expr);
2140 FINISH_PTR_LIST(argtype);
2141 return 1;
2144 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
2146 struct symbol *sym;
2148 FOR_EACH_PTR(ctype->symbol_list, sym) {
2149 if (sym->ident == ident)
2150 return sym;
2151 } END_FOR_EACH_PTR(sym);
2152 return NULL;
2155 static void convert_index(struct expression *e)
2157 struct expression *child = e->idx_expression;
2158 unsigned from = e->idx_from;
2159 unsigned to = e->idx_to + 1;
2160 e->type = EXPR_POS;
2161 e->init_offset = from * bits_to_bytes(e->ctype->bit_size);
2162 e->init_nr = to - from;
2163 e->init_expr = child;
2166 static void convert_ident(struct expression *e)
2168 struct expression *child = e->ident_expression;
2169 struct symbol *sym = e->field;
2170 e->type = EXPR_POS;
2171 e->init_offset = sym->offset;
2172 e->init_nr = 1;
2173 e->init_expr = child;
2176 static void convert_designators(struct expression *e)
2178 while (e) {
2179 if (e->type == EXPR_INDEX)
2180 convert_index(e);
2181 else if (e->type == EXPR_IDENTIFIER)
2182 convert_ident(e);
2183 else
2184 break;
2185 e = e->init_expr;
2189 static void excess(struct expression *e, const char *s)
2191 warning(e->pos, "excessive elements in %s initializer", s);
2195 * implicit designator for the first element
2197 static struct expression *first_subobject(struct symbol *ctype, int class,
2198 struct expression **v)
2200 struct expression *e = *v, *new;
2202 if (ctype->type == SYM_NODE)
2203 ctype = ctype->ctype.base_type;
2205 if (class & TYPE_PTR) { /* array */
2206 if (!ctype->bit_size)
2207 return NULL;
2208 new = alloc_expression(e->pos, EXPR_INDEX);
2209 new->idx_expression = e;
2210 new->ctype = ctype->ctype.base_type;
2211 } else {
2212 struct symbol *field, *p;
2213 PREPARE_PTR_LIST(ctype->symbol_list, p);
2214 while (p && !p->ident && is_bitfield_type(p))
2215 NEXT_PTR_LIST(p);
2216 field = p;
2217 FINISH_PTR_LIST(p);
2218 if (!field)
2219 return NULL;
2220 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2221 new->ident_expression = e;
2222 new->field = new->ctype = field;
2224 *v = new;
2225 return new;
2229 * sanity-check explicit designators; return the innermost one or NULL
2230 * in case of error. Assign types.
2232 static struct expression *check_designators(struct expression *e,
2233 struct symbol *ctype)
2235 struct expression *last = NULL;
2236 const char *err;
2237 while (1) {
2238 if (ctype->type == SYM_NODE)
2239 ctype = ctype->ctype.base_type;
2240 if (e->type == EXPR_INDEX) {
2241 struct symbol *type;
2242 if (ctype->type != SYM_ARRAY) {
2243 err = "array index in non-array";
2244 break;
2246 type = ctype->ctype.base_type;
2247 if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2248 unsigned offset = e->idx_to * type->bit_size;
2249 if (offset >= ctype->bit_size) {
2250 err = "index out of bounds in";
2251 break;
2254 e->ctype = ctype = type;
2255 ctype = type;
2256 last = e;
2257 if (!e->idx_expression) {
2258 err = "invalid";
2259 break;
2261 e = e->idx_expression;
2262 } else if (e->type == EXPR_IDENTIFIER) {
2263 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2264 err = "field name not in struct or union";
2265 break;
2267 ctype = find_struct_ident(ctype, e->expr_ident);
2268 if (!ctype) {
2269 err = "unknown field name in";
2270 break;
2272 e->field = e->ctype = ctype;
2273 last = e;
2274 if (!e->ident_expression) {
2275 err = "invalid";
2276 break;
2278 e = e->ident_expression;
2279 } else if (e->type == EXPR_POS) {
2280 err = "internal front-end error: EXPR_POS in";
2281 break;
2282 } else
2283 return last;
2285 expression_error(e, "%s initializer", err);
2286 return NULL;
2290 * choose the next subobject to initialize.
2292 * Get designators for next element, switch old ones to EXPR_POS.
2293 * Return the resulting expression or NULL if we'd run out of subobjects.
2294 * The innermost designator is returned in *v. Designators in old
2295 * are assumed to be already sanity-checked.
2297 static struct expression *next_designators(struct expression *old,
2298 struct symbol *ctype,
2299 struct expression *e, struct expression **v)
2301 struct expression *new = NULL;
2303 if (!old)
2304 return NULL;
2305 if (old->type == EXPR_INDEX) {
2306 struct expression *copy;
2307 unsigned n;
2309 copy = next_designators(old->idx_expression,
2310 old->ctype, e, v);
2311 if (!copy) {
2312 n = old->idx_to + 1;
2313 if (n * old->ctype->bit_size == ctype->bit_size) {
2314 convert_index(old);
2315 return NULL;
2317 copy = e;
2318 *v = new = alloc_expression(e->pos, EXPR_INDEX);
2319 } else {
2320 n = old->idx_to;
2321 new = alloc_expression(e->pos, EXPR_INDEX);
2324 new->idx_from = new->idx_to = n;
2325 new->idx_expression = copy;
2326 new->ctype = old->ctype;
2327 convert_index(old);
2328 } else if (old->type == EXPR_IDENTIFIER) {
2329 struct expression *copy;
2330 struct symbol *field;
2332 copy = next_designators(old->ident_expression,
2333 old->ctype, e, v);
2334 if (!copy) {
2335 field = old->field->next_subobject;
2336 if (!field) {
2337 convert_ident(old);
2338 return NULL;
2340 copy = e;
2341 *v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2342 } else {
2343 field = old->field;
2344 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2347 new->field = field;
2348 new->expr_ident = field->ident;
2349 new->ident_expression = copy;
2350 new->ctype = field;
2351 convert_ident(old);
2353 return new;
2356 static int handle_simple_initializer(struct expression **ep, int nested,
2357 int class, struct symbol *ctype);
2360 * deal with traversing subobjects [6.7.8(17,18,20)]
2362 static void handle_list_initializer(struct expression *expr,
2363 int class, struct symbol *ctype)
2365 struct expression *e, *last = NULL, *top = NULL, *next;
2366 int jumped = 0;
2368 FOR_EACH_PTR(expr->expr_list, e) {
2369 struct expression **v;
2370 struct symbol *type;
2371 int lclass;
2373 if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2374 if (!top) {
2375 top = e;
2376 last = first_subobject(ctype, class, &top);
2377 } else {
2378 last = next_designators(last, ctype, e, &top);
2380 if (!last) {
2381 excess(e, class & TYPE_PTR ? "array" :
2382 "struct or union");
2383 DELETE_CURRENT_PTR(e);
2384 continue;
2386 if (jumped) {
2387 warning(e->pos, "advancing past deep designator");
2388 jumped = 0;
2390 REPLACE_CURRENT_PTR(e, last);
2391 } else {
2392 next = check_designators(e, ctype);
2393 if (!next) {
2394 DELETE_CURRENT_PTR(e);
2395 continue;
2397 top = next;
2398 /* deeper than one designator? */
2399 jumped = top != e;
2400 convert_designators(last);
2401 last = e;
2404 found:
2405 lclass = classify_type(top->ctype, &type);
2406 if (top->type == EXPR_INDEX)
2407 v = &top->idx_expression;
2408 else
2409 v = &top->ident_expression;
2411 if (handle_simple_initializer(v, 1, lclass, top->ctype))
2412 continue;
2414 if (!(lclass & TYPE_COMPOUND)) {
2415 warning(e->pos, "bogus scalar initializer");
2416 DELETE_CURRENT_PTR(e);
2417 continue;
2420 next = first_subobject(type, lclass, v);
2421 if (next) {
2422 warning(e->pos, "missing braces around initializer");
2423 top = next;
2424 goto found;
2427 DELETE_CURRENT_PTR(e);
2428 excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2430 } END_FOR_EACH_PTR(e);
2432 convert_designators(last);
2433 expr->ctype = ctype;
2436 static int is_string_literal(struct expression **v)
2438 struct expression *e = *v;
2439 while (e && e->type == EXPR_PREOP && e->op == '(')
2440 e = e->unop;
2441 if (!e || e->type != EXPR_STRING)
2442 return 0;
2443 if (e != *v && Wparen_string)
2444 warning(e->pos,
2445 "array initialized from parenthesized string constant");
2446 *v = e;
2447 return 1;
2451 * We want a normal expression, possibly in one layer of braces. Warn
2452 * if the latter happens inside a list (it's legal, but likely to be
2453 * an effect of screwup). In case of anything not legal, we are definitely
2454 * having an effect of screwup, so just fail and let the caller warn.
2456 static struct expression *handle_scalar(struct expression *e, int nested)
2458 struct expression *v = NULL, *p;
2459 int count = 0;
2461 /* normal case */
2462 if (e->type != EXPR_INITIALIZER)
2463 return e;
2465 FOR_EACH_PTR(e->expr_list, p) {
2466 if (!v)
2467 v = p;
2468 count++;
2469 } END_FOR_EACH_PTR(p);
2470 if (count != 1)
2471 return NULL;
2472 switch(v->type) {
2473 case EXPR_INITIALIZER:
2474 case EXPR_INDEX:
2475 case EXPR_IDENTIFIER:
2476 return NULL;
2477 default:
2478 break;
2480 if (nested)
2481 warning(e->pos, "braces around scalar initializer");
2482 return v;
2486 * deal with the cases that don't care about subobjects:
2487 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2488 * character array <- string literal, possibly in braces [6.7.8(14)]
2489 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2490 * compound type <- initializer list in braces [6.7.8(16)]
2491 * The last one punts to handle_list_initializer() which, in turn will call
2492 * us for individual elements of the list.
2494 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2495 * the lack of support of wide char stuff in general.
2497 * One note: we need to take care not to evaluate a string literal until
2498 * we know that we *will* handle it right here. Otherwise we would screw
2499 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2500 * { "string", ...} - we need to preserve that string literal recognizable
2501 * until we dig into the inner struct.
2503 static int handle_simple_initializer(struct expression **ep, int nested,
2504 int class, struct symbol *ctype)
2506 int is_string = is_string_type(ctype);
2507 struct expression *e = *ep, *p;
2508 struct symbol *type;
2510 if (!e)
2511 return 0;
2513 /* scalar */
2514 if (!(class & TYPE_COMPOUND)) {
2515 e = handle_scalar(e, nested);
2516 if (!e)
2517 return 0;
2518 *ep = e;
2519 if (!evaluate_expression(e))
2520 return 1;
2521 compatible_assignment_types(e, ctype, ep, "initializer");
2522 return 1;
2526 * sublist; either a string, or we dig in; the latter will deal with
2527 * pathologies, so we don't need anything fancy here.
2529 if (e->type == EXPR_INITIALIZER) {
2530 if (is_string) {
2531 struct expression *v = NULL;
2532 int count = 0;
2534 FOR_EACH_PTR(e->expr_list, p) {
2535 if (!v)
2536 v = p;
2537 count++;
2538 } END_FOR_EACH_PTR(p);
2539 if (count == 1 && is_string_literal(&v)) {
2540 *ep = e = v;
2541 goto String;
2544 handle_list_initializer(e, class, ctype);
2545 return 1;
2548 /* string */
2549 if (is_string_literal(&e)) {
2550 /* either we are doing array of char, or we'll have to dig in */
2551 if (is_string) {
2552 *ep = e;
2553 goto String;
2555 return 0;
2557 /* struct or union can be initialized by compatible */
2558 if (class != TYPE_COMPOUND)
2559 return 0;
2560 type = evaluate_expression(e);
2561 if (!type)
2562 return 0;
2563 if (ctype->type == SYM_NODE)
2564 ctype = ctype->ctype.base_type;
2565 if (type->type == SYM_NODE)
2566 type = type->ctype.base_type;
2567 if (ctype == type)
2568 return 1;
2569 return 0;
2571 String:
2572 p = alloc_expression(e->pos, EXPR_STRING);
2573 *p = *e;
2574 type = evaluate_expression(p);
2575 if (ctype->bit_size != -1 &&
2576 ctype->bit_size + bits_in_char < type->bit_size) {
2577 warning(e->pos,
2578 "too long initializer-string for array of char");
2580 *ep = p;
2581 return 1;
2584 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2586 struct symbol *type;
2587 int class = classify_type(ctype, &type);
2588 if (!handle_simple_initializer(ep, 0, class, ctype))
2589 expression_error(*ep, "invalid initializer");
2592 static struct symbol *evaluate_cast(struct expression *expr)
2594 struct expression *target = expr->cast_expression;
2595 struct symbol *ctype;
2596 struct symbol *t1, *t2;
2597 int class1, class2;
2598 int as1 = 0, as2 = 0;
2600 if (!target)
2601 return NULL;
2604 * Special case: a cast can be followed by an
2605 * initializer, in which case we need to pass
2606 * the type value down to that initializer rather
2607 * than trying to evaluate it as an expression
2609 * A more complex case is when the initializer is
2610 * dereferenced as part of a post-fix expression.
2611 * We need to produce an expression that can be dereferenced.
2613 if (target->type == EXPR_INITIALIZER) {
2614 struct symbol *sym = expr->cast_type;
2615 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2617 sym->initializer = target;
2618 evaluate_symbol(sym);
2620 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2621 addr->symbol = sym;
2623 expr->type = EXPR_PREOP;
2624 expr->op = '*';
2625 expr->unop = addr;
2626 expr->ctype = sym;
2628 return sym;
2631 ctype = examine_symbol_type(expr->cast_type);
2632 expr->ctype = ctype;
2633 expr->cast_type = ctype;
2635 evaluate_expression(target);
2636 degenerate(target);
2638 class1 = classify_type(ctype, &t1);
2640 /* cast to non-integer type -> not an integer constant expression */
2641 if (!is_int(class1))
2642 expr->flags = 0;
2643 /* if argument turns out to be not an integer constant expression *and*
2644 it was not a floating literal to start with -> too bad */
2645 else if (expr->flags == Int_const_expr &&
2646 !(target->flags & Int_const_expr))
2647 expr->flags = 0;
2649 * You can always throw a value away by casting to
2650 * "void" - that's an implicit "force". Note that
2651 * the same is _not_ true of "void *".
2653 if (t1 == &void_ctype)
2654 goto out;
2656 if (class1 & (TYPE_COMPOUND | TYPE_FN))
2657 warning(expr->pos, "cast to non-scalar");
2659 t2 = target->ctype;
2660 if (!t2) {
2661 expression_error(expr, "cast from unknown type");
2662 goto out;
2664 class2 = classify_type(t2, &t2);
2666 if (class2 & TYPE_COMPOUND)
2667 warning(expr->pos, "cast from non-scalar");
2669 if (expr->type == EXPR_FORCE_CAST)
2670 goto out;
2672 /* allowed cast unfouls */
2673 if (class2 & TYPE_FOULED)
2674 t2 = unfoul(t2);
2676 if (t1 != t2) {
2677 if (class1 & TYPE_RESTRICT)
2678 warning(expr->pos, "cast to %s",
2679 show_typename(t1));
2680 if (class2 & TYPE_RESTRICT)
2681 warning(expr->pos, "cast from %s",
2682 show_typename(t2));
2685 if (t1 == &ulong_ctype)
2686 as1 = -1;
2687 else if (class1 == TYPE_PTR) {
2688 examine_pointer_target(t1);
2689 as1 = t1->ctype.as;
2692 if (t2 == &ulong_ctype)
2693 as2 = -1;
2694 else if (class2 == TYPE_PTR) {
2695 examine_pointer_target(t2);
2696 as2 = t2->ctype.as;
2699 if (!as1 && as2 > 0)
2700 warning(expr->pos, "cast removes address space of expression");
2701 if (as1 > 0 && as2 > 0 && as1 != as2)
2702 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2703 if (as1 > 0 && !as2 &&
2704 !is_null_pointer_constant(target) && Wcast_to_as)
2705 warning(expr->pos,
2706 "cast adds address space to expression (<asn:%d>)", as1);
2708 if (!(t1->ctype.modifiers & MOD_PTRINHERIT) && class1 == TYPE_PTR &&
2709 !as1 && (target->flags & Int_const_expr)) {
2710 if (t1->ctype.base_type == &void_ctype) {
2711 if (is_zero_constant(target)) {
2712 /* NULL */
2713 expr->type = EXPR_VALUE;
2714 expr->ctype = &null_ctype;
2715 expr->value = 0;
2716 return ctype;
2720 out:
2721 return ctype;
2725 * Evaluate a call expression with a symbol. This
2726 * should expand inline functions, and evaluate
2727 * builtins.
2729 static int evaluate_symbol_call(struct expression *expr)
2731 struct expression *fn = expr->fn;
2732 struct symbol *ctype = fn->ctype;
2734 if (fn->type != EXPR_PREOP)
2735 return 0;
2737 if (ctype->op && ctype->op->evaluate)
2738 return ctype->op->evaluate(expr);
2740 if (ctype->ctype.modifiers & MOD_INLINE) {
2741 int ret;
2742 struct symbol *curr = current_fn;
2744 if (ctype->definition)
2745 ctype = ctype->definition;
2747 current_fn = ctype->ctype.base_type;
2749 ret = inline_function(expr, ctype);
2751 /* restore the old function */
2752 current_fn = curr;
2753 return ret;
2756 return 0;
2759 static struct symbol *evaluate_call(struct expression *expr)
2761 int args, fnargs;
2762 struct symbol *ctype, *sym;
2763 struct expression *fn = expr->fn;
2764 struct expression_list *arglist = expr->args;
2766 if (!evaluate_expression(fn))
2767 return NULL;
2768 sym = ctype = fn->ctype;
2769 if (ctype->type == SYM_NODE)
2770 ctype = ctype->ctype.base_type;
2771 if (ctype->type == SYM_PTR)
2772 ctype = get_base_type(ctype);
2774 if (ctype->type != SYM_FN) {
2775 struct expression *arg;
2776 expression_error(expr, "not a function %s",
2777 show_ident(sym->ident));
2778 /* do typechecking in arguments */
2779 FOR_EACH_PTR (arglist, arg) {
2780 evaluate_expression(arg);
2781 } END_FOR_EACH_PTR(arg);
2782 return NULL;
2785 examine_fn_arguments(ctype);
2786 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
2787 sym->op && sym->op->args) {
2788 if (!sym->op->args(expr))
2789 return NULL;
2790 } else {
2791 if (!evaluate_arguments(sym, ctype, arglist))
2792 return NULL;
2793 args = expression_list_size(expr->args);
2794 fnargs = symbol_list_size(ctype->arguments);
2795 if (args < fnargs)
2796 expression_error(expr,
2797 "not enough arguments for function %s",
2798 show_ident(sym->ident));
2799 if (args > fnargs && !ctype->variadic)
2800 expression_error(expr,
2801 "too many arguments for function %s",
2802 show_ident(sym->ident));
2804 if (sym->type == SYM_NODE) {
2805 if (evaluate_symbol_call(expr))
2806 return expr->ctype;
2808 expr->ctype = ctype->ctype.base_type;
2809 return expr->ctype;
2812 static struct symbol *evaluate_offsetof(struct expression *expr)
2814 struct expression *e = expr->down;
2815 struct symbol *ctype = expr->in;
2816 int class;
2818 if (expr->op == '.') {
2819 struct symbol *field;
2820 int offset = 0;
2821 if (!ctype) {
2822 expression_error(expr, "expected structure or union");
2823 return NULL;
2825 examine_symbol_type(ctype);
2826 class = classify_type(ctype, &ctype);
2827 if (class != TYPE_COMPOUND) {
2828 expression_error(expr, "expected structure or union");
2829 return NULL;
2832 field = find_identifier(expr->ident, ctype->symbol_list, &offset);
2833 if (!field) {
2834 expression_error(expr, "unknown member");
2835 return NULL;
2837 ctype = field;
2838 expr->type = EXPR_VALUE;
2839 expr->flags = Int_const_expr;
2840 expr->value = offset;
2841 expr->taint = 0;
2842 expr->ctype = size_t_ctype;
2843 } else {
2844 if (!ctype) {
2845 expression_error(expr, "expected structure or union");
2846 return NULL;
2848 examine_symbol_type(ctype);
2849 class = classify_type(ctype, &ctype);
2850 if (class != (TYPE_COMPOUND | TYPE_PTR)) {
2851 expression_error(expr, "expected array");
2852 return NULL;
2854 ctype = ctype->ctype.base_type;
2855 if (!expr->index) {
2856 expr->type = EXPR_VALUE;
2857 expr->flags = Int_const_expr;
2858 expr->value = 0;
2859 expr->taint = 0;
2860 expr->ctype = size_t_ctype;
2861 } else {
2862 struct expression *idx = expr->index, *m;
2863 struct symbol *i_type = evaluate_expression(idx);
2864 int i_class = classify_type(i_type, &i_type);
2865 if (!is_int(i_class)) {
2866 expression_error(expr, "non-integer index");
2867 return NULL;
2869 unrestrict(idx, i_class, &i_type);
2870 idx = cast_to(idx, size_t_ctype);
2871 m = alloc_const_expression(expr->pos,
2872 bits_to_bytes(ctype->bit_size));
2873 m->ctype = size_t_ctype;
2874 m->flags = Int_const_expr;
2875 expr->type = EXPR_BINOP;
2876 expr->left = idx;
2877 expr->right = m;
2878 expr->op = '*';
2879 expr->ctype = size_t_ctype;
2880 expr->flags = m->flags & idx->flags & Int_const_expr;
2883 if (e) {
2884 struct expression *copy = __alloc_expression(0);
2885 *copy = *expr;
2886 if (e->type == EXPR_OFFSETOF)
2887 e->in = ctype;
2888 if (!evaluate_expression(e))
2889 return NULL;
2890 expr->type = EXPR_BINOP;
2891 expr->flags = e->flags & copy->flags & Int_const_expr;
2892 expr->op = '+';
2893 expr->ctype = size_t_ctype;
2894 expr->left = copy;
2895 expr->right = e;
2897 return size_t_ctype;
2900 struct symbol *evaluate_expression(struct expression *expr)
2902 if (!expr)
2903 return NULL;
2904 if (expr->ctype)
2905 return expr->ctype;
2907 switch (expr->type) {
2908 case EXPR_VALUE:
2909 case EXPR_FVALUE:
2910 expression_error(expr, "value expression without a type");
2911 return NULL;
2912 case EXPR_STRING:
2913 return evaluate_string(expr);
2914 case EXPR_SYMBOL:
2915 return evaluate_symbol_expression(expr);
2916 case EXPR_BINOP:
2917 if (!evaluate_expression(expr->left))
2918 return NULL;
2919 if (!evaluate_expression(expr->right))
2920 return NULL;
2921 return evaluate_binop(expr);
2922 case EXPR_LOGICAL:
2923 return evaluate_logical(expr);
2924 case EXPR_COMMA:
2925 evaluate_expression(expr->left);
2926 if (!evaluate_expression(expr->right))
2927 return NULL;
2928 return evaluate_comma(expr);
2929 case EXPR_COMPARE:
2930 if (!evaluate_expression(expr->left))
2931 return NULL;
2932 if (!evaluate_expression(expr->right))
2933 return NULL;
2934 return evaluate_compare(expr);
2935 case EXPR_ASSIGNMENT:
2936 if (!evaluate_expression(expr->left))
2937 return NULL;
2938 if (!evaluate_expression(expr->right))
2939 return NULL;
2940 return evaluate_assignment(expr);
2941 case EXPR_PREOP:
2942 if (!evaluate_expression(expr->unop))
2943 return NULL;
2944 return evaluate_preop(expr);
2945 case EXPR_POSTOP:
2946 if (!evaluate_expression(expr->unop))
2947 return NULL;
2948 return evaluate_postop(expr);
2949 case EXPR_CAST:
2950 case EXPR_FORCE_CAST:
2951 case EXPR_IMPLIED_CAST:
2952 return evaluate_cast(expr);
2953 case EXPR_SIZEOF:
2954 return evaluate_sizeof(expr);
2955 case EXPR_PTRSIZEOF:
2956 return evaluate_ptrsizeof(expr);
2957 case EXPR_ALIGNOF:
2958 return evaluate_alignof(expr);
2959 case EXPR_DEREF:
2960 return evaluate_member_dereference(expr);
2961 case EXPR_CALL:
2962 return evaluate_call(expr);
2963 case EXPR_SELECT:
2964 case EXPR_CONDITIONAL:
2965 return evaluate_conditional_expression(expr);
2966 case EXPR_STATEMENT:
2967 expr->ctype = evaluate_statement(expr->statement);
2968 return expr->ctype;
2970 case EXPR_LABEL:
2971 expr->ctype = &ptr_ctype;
2972 return &ptr_ctype;
2974 case EXPR_TYPE:
2975 /* Evaluate the type of the symbol .. */
2976 evaluate_symbol(expr->symbol);
2977 /* .. but the type of the _expression_ is a "type" */
2978 expr->ctype = &type_ctype;
2979 return &type_ctype;
2981 case EXPR_OFFSETOF:
2982 return evaluate_offsetof(expr);
2984 /* These can not exist as stand-alone expressions */
2985 case EXPR_INITIALIZER:
2986 case EXPR_IDENTIFIER:
2987 case EXPR_INDEX:
2988 case EXPR_POS:
2989 expression_error(expr, "internal front-end error: initializer in expression");
2990 return NULL;
2991 case EXPR_SLICE:
2992 expression_error(expr, "internal front-end error: SLICE re-evaluated");
2993 return NULL;
2995 return NULL;
2998 static void check_duplicates(struct symbol *sym)
3000 int declared = 0;
3001 struct symbol *next = sym;
3003 while ((next = next->same_symbol) != NULL) {
3004 const char *typediff;
3005 evaluate_symbol(next);
3006 declared++;
3007 typediff = type_difference(&sym->ctype, &next->ctype, 0, 0);
3008 if (typediff) {
3009 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
3010 show_ident(sym->ident),
3011 stream_name(next->pos.stream), next->pos.line, typediff);
3012 return;
3015 if (!declared) {
3016 unsigned long mod = sym->ctype.modifiers;
3017 if (mod & (MOD_STATIC | MOD_REGISTER))
3018 return;
3019 if (!(mod & MOD_TOPLEVEL))
3020 return;
3021 if (!Wdecl)
3022 return;
3023 if (sym->ident == &main_ident)
3024 return;
3025 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
3029 static struct symbol *evaluate_symbol(struct symbol *sym)
3031 struct symbol *base_type;
3033 if (!sym)
3034 return sym;
3035 if (sym->evaluated)
3036 return sym;
3037 sym->evaluated = 1;
3039 sym = examine_symbol_type(sym);
3040 base_type = get_base_type(sym);
3041 if (!base_type)
3042 return NULL;
3044 /* Evaluate the initializers */
3045 if (sym->initializer)
3046 evaluate_initializer(sym, &sym->initializer);
3048 /* And finally, evaluate the body of the symbol too */
3049 if (base_type->type == SYM_FN) {
3050 struct symbol *curr = current_fn;
3052 if (sym->definition && sym->definition != sym)
3053 return evaluate_symbol(sym->definition);
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;