Allow forced attribute in function argument
[smatch.git] / evaluate.c
blob0dfa519bd62baa4d27e70f1dad35d5c8e772feda
1 /*
2 * sparse/evaluate.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
9 * Evaluate constant expressions.
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <stddef.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <limits.h>
21 #include "lib.h"
22 #include "allocate.h"
23 #include "parse.h"
24 #include "token.h"
25 #include "symbol.h"
26 #include "target.h"
27 #include "expression.h"
29 struct symbol *current_fn;
31 static struct symbol *degenerate(struct expression *expr);
32 static struct symbol *evaluate_symbol(struct symbol *sym);
34 static struct symbol *evaluate_symbol_expression(struct expression *expr)
36 struct expression *addr;
37 struct symbol *sym = expr->symbol;
38 struct symbol *base_type;
40 if (!sym) {
41 expression_error(expr, "undefined identifier '%s'", show_ident(expr->symbol_name));
42 return NULL;
45 examine_symbol_type(sym);
47 base_type = get_base_type(sym);
48 if (!base_type) {
49 expression_error(expr, "identifier '%s' has no type", show_ident(expr->symbol_name));
50 return NULL;
53 addr = alloc_expression(expr->pos, EXPR_SYMBOL);
54 addr->symbol = sym;
55 addr->symbol_name = expr->symbol_name;
56 addr->ctype = &lazy_ptr_ctype; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
57 expr->type = EXPR_PREOP;
58 expr->op = '*';
59 expr->unop = addr;
61 /* The type of a symbol is the symbol itself! */
62 expr->ctype = sym;
63 return sym;
66 static struct symbol *evaluate_string(struct expression *expr)
68 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
69 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
70 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
71 struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
72 unsigned int length = expr->string->length;
74 sym->array_size = alloc_const_expression(expr->pos, length);
75 sym->bit_size = bytes_to_bits(length);
76 sym->ctype.alignment = 1;
77 sym->string = 1;
78 sym->ctype.modifiers = MOD_STATIC;
79 sym->ctype.base_type = array;
80 sym->initializer = initstr;
82 initstr->ctype = sym;
83 initstr->string = expr->string;
85 array->array_size = sym->array_size;
86 array->bit_size = bytes_to_bits(length);
87 array->ctype.alignment = 1;
88 array->ctype.modifiers = MOD_STATIC;
89 array->ctype.base_type = &char_ctype;
91 addr->symbol = sym;
92 addr->ctype = &lazy_ptr_ctype;
94 expr->type = EXPR_PREOP;
95 expr->op = '*';
96 expr->unop = addr;
97 expr->ctype = sym;
98 return sym;
101 /* type has come from classify_type and is an integer type */
102 static inline struct symbol *integer_promotion(struct symbol *type)
104 struct symbol *orig_type = type;
105 unsigned long mod = type->ctype.modifiers;
106 int width = type->bit_size;
109 * Bitfields always promote to the base type,
110 * even if the bitfield might be bigger than
111 * an "int".
113 if (type->type == SYM_BITFIELD) {
114 type = type->ctype.base_type;
115 orig_type = type;
117 mod = type->ctype.modifiers;
118 if (width < bits_in_int)
119 return &int_ctype;
121 /* If char/short has as many bits as int, it still gets "promoted" */
122 if (mod & (MOD_CHAR | MOD_SHORT)) {
123 if (mod & MOD_UNSIGNED)
124 return &uint_ctype;
125 return &int_ctype;
127 return orig_type;
131 * integer part of usual arithmetic conversions:
132 * integer promotions are applied
133 * if left and right are identical, we are done
134 * if signedness is the same, convert one with lower rank
135 * unless unsigned argument has rank lower than signed one, convert the
136 * signed one.
137 * if signed argument is bigger than unsigned one, convert the unsigned.
138 * otherwise, convert signed.
140 * Leaving aside the integer promotions, that is equivalent to
141 * if identical, don't convert
142 * if left is bigger than right, convert right
143 * if right is bigger than left, convert right
144 * otherwise, if signedness is the same, convert one with lower rank
145 * otherwise convert the signed one.
147 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
149 unsigned long lmod, rmod;
151 left = integer_promotion(left);
152 right = integer_promotion(right);
154 if (left == right)
155 goto left;
157 if (left->bit_size > right->bit_size)
158 goto left;
160 if (right->bit_size > left->bit_size)
161 goto right;
163 lmod = left->ctype.modifiers;
164 rmod = right->ctype.modifiers;
165 if ((lmod ^ rmod) & MOD_UNSIGNED) {
166 if (lmod & MOD_UNSIGNED)
167 goto left;
168 } else if ((lmod & ~rmod) & (MOD_LONG_ALL))
169 goto left;
170 right:
171 left = right;
172 left:
173 return left;
176 static int same_cast_type(struct symbol *orig, struct symbol *new)
178 return orig->bit_size == new->bit_size && orig->bit_offset == new->bit_offset;
181 static struct symbol *base_type(struct symbol *node, unsigned long *modp, unsigned long *asp)
183 unsigned long mod, as;
185 mod = 0; as = 0;
186 while (node) {
187 mod |= node->ctype.modifiers;
188 as |= node->ctype.as;
189 if (node->type == SYM_NODE) {
190 node = node->ctype.base_type;
191 continue;
193 break;
195 *modp = mod & ~MOD_IGNORE;
196 *asp = as;
197 return node;
200 static int is_same_type(struct expression *expr, struct symbol *new)
202 struct symbol *old = expr->ctype;
203 unsigned long oldmod, newmod, oldas, newas;
205 old = base_type(old, &oldmod, &oldas);
206 new = base_type(new, &newmod, &newas);
208 /* Same base type, same address space? */
209 if (old == new && oldas == newas) {
210 unsigned long difmod;
212 /* Check the modifier bits. */
213 difmod = (oldmod ^ newmod) & ~MOD_NOCAST;
215 /* Exact same type? */
216 if (!difmod)
217 return 1;
220 * Not the same type, but differs only in "const".
221 * Don't warn about MOD_NOCAST.
223 if (difmod == MOD_CONST)
224 return 0;
226 if ((oldmod | newmod) & MOD_NOCAST) {
227 const char *tofrom = "to/from";
228 if (!(newmod & MOD_NOCAST))
229 tofrom = "from";
230 if (!(oldmod & MOD_NOCAST))
231 tofrom = "to";
232 warning(expr->pos, "implicit cast %s nocast type", tofrom);
234 return 0;
237 static void
238 warn_for_different_enum_types (struct position pos,
239 struct symbol *typea,
240 struct symbol *typeb)
242 if (!Wenum_mismatch)
243 return;
244 if (typea->type == SYM_NODE)
245 typea = typea->ctype.base_type;
246 if (typeb->type == SYM_NODE)
247 typeb = typeb->ctype.base_type;
249 if (typea == typeb)
250 return;
252 if (typea->type == SYM_ENUM && typeb->type == SYM_ENUM) {
253 warning(pos, "mixing different enum types");
254 info(pos, " %s versus", show_typename(typea));
255 info(pos, " %s", show_typename(typeb));
260 * This gets called for implicit casts in assignments and
261 * integer promotion. We often want to try to move the
262 * cast down, because the ops involved may have been
263 * implicitly cast up, and we can get rid of the casts
264 * early.
266 static struct expression * cast_to(struct expression *old, struct symbol *type)
268 struct expression *expr;
270 warn_for_different_enum_types (old->pos, old->ctype, type);
272 if (old->ctype != &null_ctype && is_same_type(old, type))
273 return old;
276 * See if we can simplify the op. Move the cast down.
278 switch (old->type) {
279 case EXPR_PREOP:
280 if (old->ctype->bit_size < type->bit_size)
281 break;
282 if (old->op == '~') {
283 old->ctype = type;
284 old->unop = cast_to(old->unop, type);
285 return old;
287 break;
289 case EXPR_IMPLIED_CAST:
290 warn_for_different_enum_types(old->pos, old->ctype, type);
292 if (old->ctype->bit_size >= type->bit_size) {
293 struct expression *orig = old->cast_expression;
294 if (same_cast_type(orig->ctype, type))
295 return orig;
296 if (old->ctype->bit_offset == type->bit_offset) {
297 old->ctype = type;
298 old->cast_type = type;
299 return old;
302 break;
304 default:
305 /* nothing */;
308 expr = alloc_expression(old->pos, EXPR_IMPLIED_CAST);
309 expr->flags = old->flags;
310 expr->ctype = type;
311 expr->cast_type = type;
312 expr->cast_expression = old;
313 return expr;
316 enum {
317 TYPE_NUM = 1,
318 TYPE_BITFIELD = 2,
319 TYPE_RESTRICT = 4,
320 TYPE_FLOAT = 8,
321 TYPE_PTR = 16,
322 TYPE_COMPOUND = 32,
323 TYPE_FOULED = 64,
324 TYPE_FN = 128,
327 static inline int classify_type(struct symbol *type, struct symbol **base)
329 static int type_class[SYM_BAD + 1] = {
330 [SYM_PTR] = TYPE_PTR,
331 [SYM_FN] = TYPE_PTR | TYPE_FN,
332 [SYM_ARRAY] = TYPE_PTR | TYPE_COMPOUND,
333 [SYM_STRUCT] = TYPE_COMPOUND,
334 [SYM_UNION] = TYPE_COMPOUND,
335 [SYM_BITFIELD] = TYPE_NUM | TYPE_BITFIELD,
336 [SYM_RESTRICT] = TYPE_NUM | TYPE_RESTRICT,
337 [SYM_FOULED] = TYPE_NUM | TYPE_RESTRICT | TYPE_FOULED,
339 if (type->type == SYM_NODE)
340 type = type->ctype.base_type;
341 if (type->type == SYM_TYPEOF) {
342 type = evaluate_expression(type->initializer);
343 if (!type)
344 type = &bad_ctype;
345 else if (type->type == SYM_NODE)
346 type = type->ctype.base_type;
348 if (type->type == SYM_ENUM)
349 type = type->ctype.base_type;
350 *base = type;
351 if (type->type == SYM_BASETYPE) {
352 if (type->ctype.base_type == &int_type)
353 return TYPE_NUM;
354 if (type->ctype.base_type == &fp_type)
355 return TYPE_NUM | TYPE_FLOAT;
357 return type_class[type->type];
360 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
362 static inline int is_string_type(struct symbol *type)
364 if (type->type == SYM_NODE)
365 type = type->ctype.base_type;
366 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
369 static struct symbol *bad_expr_type(struct expression *expr)
371 sparse_error(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
372 switch (expr->type) {
373 case EXPR_BINOP:
374 case EXPR_COMPARE:
375 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
376 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
377 break;
378 case EXPR_PREOP:
379 case EXPR_POSTOP:
380 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
381 break;
382 default:
383 break;
386 expr->flags = 0;
387 return expr->ctype = &bad_ctype;
390 static int restricted_value(struct expression *v, struct symbol *type)
392 if (v->type != EXPR_VALUE)
393 return 1;
394 if (v->value != 0)
395 return 1;
396 return 0;
399 static int restricted_binop(int op, struct symbol *type)
401 switch (op) {
402 case '&':
403 case '=':
404 case SPECIAL_AND_ASSIGN:
405 case SPECIAL_OR_ASSIGN:
406 case SPECIAL_XOR_ASSIGN:
407 return 1; /* unfoul */
408 case '|':
409 case '^':
410 case '?':
411 return 2; /* keep fouled */
412 case SPECIAL_EQUAL:
413 case SPECIAL_NOTEQUAL:
414 return 3; /* warn if fouled */
415 default:
416 return 0; /* warn */
420 static int restricted_unop(int op, struct symbol **type)
422 if (op == '~') {
423 if ((*type)->bit_size < bits_in_int)
424 *type = befoul(*type);
425 return 0;
426 } if (op == '+')
427 return 0;
428 return 1;
431 /* type should be SYM_FOULED */
432 static inline struct symbol *unfoul(struct symbol *type)
434 return type->ctype.base_type;
437 static struct symbol *restricted_binop_type(int op,
438 struct expression *left,
439 struct expression *right,
440 int lclass, int rclass,
441 struct symbol *ltype,
442 struct symbol *rtype)
444 struct symbol *ctype = NULL;
445 if (lclass & TYPE_RESTRICT) {
446 if (rclass & TYPE_RESTRICT) {
447 if (ltype == rtype) {
448 ctype = ltype;
449 } else if (lclass & TYPE_FOULED) {
450 if (unfoul(ltype) == rtype)
451 ctype = ltype;
452 } else if (rclass & TYPE_FOULED) {
453 if (unfoul(rtype) == ltype)
454 ctype = rtype;
456 } else {
457 if (!restricted_value(right, ltype))
458 ctype = ltype;
460 } else if (!restricted_value(left, rtype))
461 ctype = rtype;
463 if (ctype) {
464 switch (restricted_binop(op, ctype)) {
465 case 1:
466 if ((lclass ^ rclass) & TYPE_FOULED)
467 ctype = unfoul(ctype);
468 break;
469 case 3:
470 if (!(lclass & rclass & TYPE_FOULED))
471 break;
472 case 0:
473 ctype = NULL;
474 default:
475 break;
479 return ctype;
482 static inline void unrestrict(struct expression *expr,
483 int class, struct symbol **ctype)
485 if (class & TYPE_RESTRICT) {
486 if (class & TYPE_FOULED)
487 *ctype = unfoul(*ctype);
488 warning(expr->pos, "%s degrades to integer",
489 show_typename(*ctype));
490 *ctype = (*ctype)->ctype.base_type; /* get to arithmetic type */
494 static struct symbol *usual_conversions(int op,
495 struct expression *left,
496 struct expression *right,
497 int lclass, int rclass,
498 struct symbol *ltype,
499 struct symbol *rtype)
501 struct symbol *ctype;
503 warn_for_different_enum_types(right->pos, left->ctype, right->ctype);
505 if ((lclass | rclass) & TYPE_RESTRICT)
506 goto Restr;
508 Normal:
509 if (!(lclass & TYPE_FLOAT)) {
510 if (!(rclass & TYPE_FLOAT))
511 return bigger_int_type(ltype, rtype);
512 else
513 return rtype;
514 } else if (rclass & TYPE_FLOAT) {
515 unsigned long lmod = ltype->ctype.modifiers;
516 unsigned long rmod = rtype->ctype.modifiers;
517 if (rmod & ~lmod & (MOD_LONG_ALL))
518 return rtype;
519 else
520 return ltype;
521 } else
522 return ltype;
524 Restr:
525 ctype = restricted_binop_type(op, left, right,
526 lclass, rclass, ltype, rtype);
527 if (ctype)
528 return ctype;
530 unrestrict(left, lclass, &ltype);
531 unrestrict(right, rclass, &rtype);
533 goto Normal;
536 static inline int lvalue_expression(struct expression *expr)
538 return expr->type == EXPR_PREOP && expr->op == '*';
541 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *itype)
543 struct expression *index = expr->right;
544 struct symbol *ctype, *base;
545 int multiply;
547 classify_type(degenerate(expr->left), &ctype);
548 base = examine_pointer_target(ctype);
550 if (!base) {
551 expression_error(expr, "missing type information");
552 return NULL;
554 if (is_function(base)) {
555 expression_error(expr, "arithmetics on pointers to functions");
556 return NULL;
559 /* Get the size of whatever the pointer points to */
560 multiply = is_void_type(base) ? 1 : bits_to_bytes(base->bit_size);
562 if (ctype == &null_ctype)
563 ctype = &ptr_ctype;
564 expr->ctype = ctype;
566 if (multiply == 1 && itype->bit_size >= bits_in_pointer)
567 return ctype;
569 if (index->type == EXPR_VALUE) {
570 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
571 unsigned long long v = index->value, mask;
572 mask = 1ULL << (itype->bit_size - 1);
573 if (v & mask)
574 v |= -mask;
575 else
576 v &= mask - 1;
577 v *= multiply;
578 mask = 1ULL << (bits_in_pointer - 1);
579 v &= mask | (mask - 1);
580 val->value = v;
581 val->ctype = ssize_t_ctype;
582 expr->right = val;
583 return ctype;
586 if (itype->bit_size < bits_in_pointer)
587 index = cast_to(index, ssize_t_ctype);
589 if (multiply > 1) {
590 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
591 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
593 val->ctype = ssize_t_ctype;
594 val->value = multiply;
596 mul->op = '*';
597 mul->ctype = ssize_t_ctype;
598 mul->left = index;
599 mul->right = val;
600 index = mul;
603 expr->right = index;
604 return ctype;
607 static void examine_fn_arguments(struct symbol *fn);
609 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
611 const char *type_difference(struct ctype *c1, struct ctype *c2,
612 unsigned long mod1, unsigned long mod2)
614 unsigned long as1 = c1->as, as2 = c2->as;
615 struct symbol *t1 = c1->base_type;
616 struct symbol *t2 = c2->base_type;
617 int move1 = 1, move2 = 1;
618 mod1 |= c1->modifiers;
619 mod2 |= c2->modifiers;
620 for (;;) {
621 unsigned long diff;
622 int type;
623 struct symbol *base1 = t1->ctype.base_type;
624 struct symbol *base2 = t2->ctype.base_type;
627 * FIXME! Collect alignment and context too here!
629 if (move1) {
630 if (t1 && t1->type != SYM_PTR) {
631 mod1 |= t1->ctype.modifiers;
632 as1 |= t1->ctype.as;
634 move1 = 0;
637 if (move2) {
638 if (t2 && t2->type != SYM_PTR) {
639 mod2 |= t2->ctype.modifiers;
640 as2 |= t2->ctype.as;
642 move2 = 0;
645 if (t1 == t2)
646 break;
647 if (!t1 || !t2)
648 return "different types";
650 if (t1->type == SYM_NODE || t1->type == SYM_ENUM) {
651 t1 = base1;
652 move1 = 1;
653 if (!t1)
654 return "bad types";
655 continue;
658 if (t2->type == SYM_NODE || t2->type == SYM_ENUM) {
659 t2 = base2;
660 move2 = 1;
661 if (!t2)
662 return "bad types";
663 continue;
666 move1 = move2 = 1;
667 type = t1->type;
668 if (type != t2->type)
669 return "different base types";
671 switch (type) {
672 default:
673 sparse_error(t1->pos,
674 "internal error: bad type in derived(%d)",
675 type);
676 return "bad types";
677 case SYM_RESTRICT:
678 return "different base types";
679 case SYM_UNION:
680 case SYM_STRUCT:
681 /* allow definition of incomplete structs and unions */
682 if (t1->ident == t2->ident)
683 return NULL;
684 return "different base types";
685 case SYM_ARRAY:
686 /* XXX: we ought to compare sizes */
687 break;
688 case SYM_PTR:
689 if (as1 != as2)
690 return "different address spaces";
691 /* MOD_SPECIFIER is due to idiocy in parse.c */
692 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SPECIFIER)
693 return "different modifiers";
694 /* we could be lazier here */
695 base1 = examine_pointer_target(t1);
696 base2 = examine_pointer_target(t2);
697 mod1 = t1->ctype.modifiers;
698 as1 = t1->ctype.as;
699 mod2 = t2->ctype.modifiers;
700 as2 = t2->ctype.as;
701 break;
702 case SYM_FN: {
703 struct symbol *arg1, *arg2;
704 int i;
706 if (as1 != as2)
707 return "different address spaces";
708 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
709 return "different modifiers";
710 mod1 = t1->ctype.modifiers;
711 as1 = t1->ctype.as;
712 mod2 = t2->ctype.modifiers;
713 as2 = t2->ctype.as;
715 if (base1->variadic != base2->variadic)
716 return "incompatible variadic arguments";
717 examine_fn_arguments(t1);
718 examine_fn_arguments(t2);
719 PREPARE_PTR_LIST(t1->arguments, arg1);
720 PREPARE_PTR_LIST(t2->arguments, arg2);
721 i = 1;
722 for (;;) {
723 const char *diffstr;
724 if (!arg1 && !arg2)
725 break;
726 if (!arg1 || !arg2)
727 return "different argument counts";
728 diffstr = type_difference(&arg1->ctype,
729 &arg2->ctype,
730 MOD_IGN, MOD_IGN);
731 if (diffstr) {
732 static char argdiff[80];
733 sprintf(argdiff, "incompatible argument %d (%s)", i, diffstr);
734 return argdiff;
736 NEXT_PTR_LIST(arg1);
737 NEXT_PTR_LIST(arg2);
738 i++;
740 FINISH_PTR_LIST(arg2);
741 FINISH_PTR_LIST(arg1);
742 break;
744 case SYM_BASETYPE:
745 if (as1 != as2)
746 return "different address spaces";
747 if (base1 != base2)
748 return "different base types";
749 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
750 if (!diff)
751 return NULL;
752 if (diff & MOD_SIZE)
753 return "different type sizes";
754 else if (diff & ~MOD_SIGNEDNESS)
755 return "different modifiers";
756 else
757 return "different signedness";
759 t1 = base1;
760 t2 = base2;
762 if (as1 != as2)
763 return "different address spaces";
764 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
765 return "different modifiers";
766 return NULL;
769 static void bad_null(struct expression *expr)
771 if (Wnon_pointer_null)
772 warning(expr->pos, "Using plain integer as NULL pointer");
775 static unsigned long target_qualifiers(struct symbol *type)
777 unsigned long mod = type->ctype.modifiers & MOD_IGN;
778 if (type->ctype.base_type && type->ctype.base_type->type == SYM_ARRAY)
779 mod = 0;
780 return mod;
783 static struct symbol *evaluate_ptr_sub(struct expression *expr)
785 const char *typediff;
786 struct symbol *ltype, *rtype;
787 struct expression *l = expr->left;
788 struct expression *r = expr->right;
789 struct symbol *lbase;
791 classify_type(degenerate(l), &ltype);
792 classify_type(degenerate(r), &rtype);
794 lbase = examine_pointer_target(ltype);
795 examine_pointer_target(rtype);
796 typediff = type_difference(&ltype->ctype, &rtype->ctype,
797 target_qualifiers(rtype),
798 target_qualifiers(ltype));
799 if (typediff)
800 expression_error(expr, "subtraction of different types can't work (%s)", typediff);
802 if (is_function(lbase)) {
803 expression_error(expr, "subtraction of functions? Share your drugs");
804 return NULL;
807 expr->ctype = ssize_t_ctype;
808 if (lbase->bit_size > bits_in_char) {
809 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
810 struct expression *div = expr;
811 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
812 unsigned long value = bits_to_bytes(lbase->bit_size);
814 val->ctype = size_t_ctype;
815 val->value = value;
817 if (value & (value-1)) {
818 if (Wptr_subtraction_blows)
819 warning(expr->pos, "potentially expensive pointer subtraction");
822 sub->op = '-';
823 sub->ctype = ssize_t_ctype;
824 sub->left = l;
825 sub->right = r;
827 div->op = '/';
828 div->left = sub;
829 div->right = val;
832 return ssize_t_ctype;
835 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
837 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
839 struct symbol *ctype;
841 if (!expr)
842 return NULL;
844 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
845 warning(expr->pos, "assignment expression in conditional");
847 ctype = evaluate_expression(expr);
848 if (ctype) {
849 if (is_safe_type(ctype))
850 warning(expr->pos, "testing a 'safe expression'");
853 return ctype;
856 static struct symbol *evaluate_logical(struct expression *expr)
858 if (!evaluate_conditional(expr->left, 0))
859 return NULL;
860 if (!evaluate_conditional(expr->right, 0))
861 return NULL;
863 expr->ctype = &bool_ctype;
864 if (expr->flags) {
865 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
866 expr->flags = 0;
868 return &bool_ctype;
871 static struct symbol *evaluate_binop(struct expression *expr)
873 struct symbol *ltype, *rtype, *ctype;
874 int lclass = classify_type(expr->left->ctype, &ltype);
875 int rclass = classify_type(expr->right->ctype, &rtype);
876 int op = expr->op;
878 if (expr->flags) {
879 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
880 expr->flags = 0;
883 /* number op number */
884 if (lclass & rclass & TYPE_NUM) {
885 if ((lclass | rclass) & TYPE_FLOAT) {
886 switch (op) {
887 case '+': case '-': case '*': case '/':
888 break;
889 default:
890 return bad_expr_type(expr);
894 if (op == SPECIAL_LEFTSHIFT || op == SPECIAL_RIGHTSHIFT) {
895 // shifts do integer promotions, but that's it.
896 unrestrict(expr->left, lclass, &ltype);
897 unrestrict(expr->right, rclass, &rtype);
898 ctype = ltype = integer_promotion(ltype);
899 rtype = integer_promotion(rtype);
900 } else {
901 // The rest do usual conversions
902 const unsigned left_not = expr->left->type == EXPR_PREOP
903 && expr->left->op == '!';
904 const unsigned right_not = expr->right->type == EXPR_PREOP
905 && expr->right->op == '!';
906 if ((op == '&' || op == '|') && (left_not || right_not))
907 warning(expr->pos, "dubious: %sx %c %sy",
908 left_not ? "!" : "",
910 right_not ? "!" : "");
912 ltype = usual_conversions(op, expr->left, expr->right,
913 lclass, rclass, ltype, rtype);
914 ctype = rtype = ltype;
917 expr->left = cast_to(expr->left, ltype);
918 expr->right = cast_to(expr->right, rtype);
919 expr->ctype = ctype;
920 return ctype;
923 /* pointer (+|-) integer */
924 if (lclass & TYPE_PTR && is_int(rclass) && (op == '+' || op == '-')) {
925 unrestrict(expr->right, rclass, &rtype);
926 return evaluate_ptr_add(expr, rtype);
929 /* integer + pointer */
930 if (rclass & TYPE_PTR && is_int(lclass) && op == '+') {
931 struct expression *index = expr->left;
932 unrestrict(index, lclass, &ltype);
933 expr->left = expr->right;
934 expr->right = index;
935 return evaluate_ptr_add(expr, ltype);
938 /* pointer - pointer */
939 if (lclass & rclass & TYPE_PTR && expr->op == '-')
940 return evaluate_ptr_sub(expr);
942 return bad_expr_type(expr);
945 static struct symbol *evaluate_comma(struct expression *expr)
947 expr->ctype = degenerate(expr->right);
948 if (expr->ctype == &null_ctype)
949 expr->ctype = &ptr_ctype;
950 expr->flags &= expr->left->flags & expr->right->flags;
951 return expr->ctype;
954 static int modify_for_unsigned(int op)
956 if (op == '<')
957 op = SPECIAL_UNSIGNED_LT;
958 else if (op == '>')
959 op = SPECIAL_UNSIGNED_GT;
960 else if (op == SPECIAL_LTE)
961 op = SPECIAL_UNSIGNED_LTE;
962 else if (op == SPECIAL_GTE)
963 op = SPECIAL_UNSIGNED_GTE;
964 return op;
967 static inline int is_null_pointer_constant(struct expression *e)
969 if (e->ctype == &null_ctype)
970 return 1;
971 if (!(e->flags & Int_const_expr))
972 return 0;
973 return is_zero_constant(e) ? 2 : 0;
976 static struct symbol *evaluate_compare(struct expression *expr)
978 struct expression *left = expr->left, *right = expr->right;
979 struct symbol *ltype, *rtype, *lbase, *rbase;
980 int lclass = classify_type(degenerate(left), &ltype);
981 int rclass = classify_type(degenerate(right), &rtype);
982 struct symbol *ctype;
983 const char *typediff;
985 if (expr->flags) {
986 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
987 expr->flags = 0;
990 /* Type types? */
991 if (is_type_type(ltype) && is_type_type(rtype))
992 goto OK;
994 if (is_safe_type(left->ctype) || is_safe_type(right->ctype))
995 warning(expr->pos, "testing a 'safe expression'");
997 /* number on number */
998 if (lclass & rclass & TYPE_NUM) {
999 ctype = usual_conversions(expr->op, expr->left, expr->right,
1000 lclass, rclass, ltype, rtype);
1001 expr->left = cast_to(expr->left, ctype);
1002 expr->right = cast_to(expr->right, ctype);
1003 if (ctype->ctype.modifiers & MOD_UNSIGNED)
1004 expr->op = modify_for_unsigned(expr->op);
1005 goto OK;
1008 /* at least one must be a pointer */
1009 if (!((lclass | rclass) & TYPE_PTR))
1010 return bad_expr_type(expr);
1012 /* equality comparisons can be with null pointer constants */
1013 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1014 int is_null1 = is_null_pointer_constant(left);
1015 int is_null2 = is_null_pointer_constant(right);
1016 if (is_null1 == 2)
1017 bad_null(left);
1018 if (is_null2 == 2)
1019 bad_null(right);
1020 if (is_null1 && is_null2) {
1021 int positive = expr->op == SPECIAL_EQUAL;
1022 expr->type = EXPR_VALUE;
1023 expr->value = positive;
1024 goto OK;
1026 if (is_null1 && (rclass & TYPE_PTR)) {
1027 left = cast_to(left, rtype);
1028 goto OK;
1030 if (is_null2 && (lclass & TYPE_PTR)) {
1031 right = cast_to(right, ltype);
1032 goto OK;
1035 /* both should be pointers */
1036 if (!(lclass & rclass & TYPE_PTR))
1037 return bad_expr_type(expr);
1038 expr->op = modify_for_unsigned(expr->op);
1040 lbase = examine_pointer_target(ltype);
1041 rbase = examine_pointer_target(rtype);
1043 /* they also have special treatment for pointers to void */
1044 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1045 if (ltype->ctype.as == rtype->ctype.as) {
1046 if (lbase == &void_ctype) {
1047 right = cast_to(right, ltype);
1048 goto OK;
1050 if (rbase == &void_ctype) {
1051 left = cast_to(left, rtype);
1052 goto OK;
1057 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1058 target_qualifiers(rtype),
1059 target_qualifiers(ltype));
1060 if (!typediff)
1061 goto OK;
1063 expression_error(expr, "incompatible types in comparison expression (%s)", typediff);
1064 return NULL;
1067 expr->ctype = &bool_ctype;
1068 return &bool_ctype;
1072 * NOTE! The degenerate case of "x ? : y", where we don't
1073 * have a true case, this will possibly promote "x" to the
1074 * same type as "y", and thus _change_ the conditional
1075 * test in the expression. But since promotion is "safe"
1076 * for testing, that's OK.
1078 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1080 struct expression **true;
1081 struct symbol *ctype, *ltype, *rtype, *lbase, *rbase;
1082 int lclass, rclass;
1083 const char * typediff;
1084 int qual;
1086 if (!evaluate_conditional(expr->conditional, 0))
1087 return NULL;
1088 if (!evaluate_expression(expr->cond_false))
1089 return NULL;
1091 ctype = degenerate(expr->conditional);
1092 rtype = degenerate(expr->cond_false);
1094 true = &expr->conditional;
1095 ltype = ctype;
1096 if (expr->cond_true) {
1097 if (!evaluate_expression(expr->cond_true))
1098 return NULL;
1099 ltype = degenerate(expr->cond_true);
1100 true = &expr->cond_true;
1103 if (expr->flags) {
1104 int flags = expr->conditional->flags & Int_const_expr;
1105 flags &= (*true)->flags & expr->cond_false->flags;
1106 if (!flags)
1107 expr->flags = 0;
1110 lclass = classify_type(ltype, &ltype);
1111 rclass = classify_type(rtype, &rtype);
1112 if (lclass & rclass & TYPE_NUM) {
1113 ctype = usual_conversions('?', *true, expr->cond_false,
1114 lclass, rclass, ltype, rtype);
1115 *true = cast_to(*true, ctype);
1116 expr->cond_false = cast_to(expr->cond_false, ctype);
1117 goto out;
1120 if ((lclass | rclass) & TYPE_PTR) {
1121 int is_null1 = is_null_pointer_constant(*true);
1122 int is_null2 = is_null_pointer_constant(expr->cond_false);
1124 if (is_null1 && is_null2) {
1125 *true = cast_to(*true, &ptr_ctype);
1126 expr->cond_false = cast_to(expr->cond_false, &ptr_ctype);
1127 ctype = &ptr_ctype;
1128 goto out;
1130 if (is_null1 && (rclass & TYPE_PTR)) {
1131 if (is_null1 == 2)
1132 bad_null(*true);
1133 *true = cast_to(*true, rtype);
1134 ctype = rtype;
1135 goto out;
1137 if (is_null2 && (lclass & TYPE_PTR)) {
1138 if (is_null2 == 2)
1139 bad_null(expr->cond_false);
1140 expr->cond_false = cast_to(expr->cond_false, ltype);
1141 ctype = ltype;
1142 goto out;
1144 if (!(lclass & rclass & TYPE_PTR)) {
1145 typediff = "different types";
1146 goto Err;
1148 /* OK, it's pointer on pointer */
1149 if (ltype->ctype.as != rtype->ctype.as) {
1150 typediff = "different address spaces";
1151 goto Err;
1154 /* need to be lazier here */
1155 lbase = examine_pointer_target(ltype);
1156 rbase = examine_pointer_target(rtype);
1157 qual = target_qualifiers(ltype) | target_qualifiers(rtype);
1159 if (lbase == &void_ctype) {
1160 /* XXX: pointers to function should warn here */
1161 ctype = ltype;
1162 goto Qual;
1165 if (rbase == &void_ctype) {
1166 /* XXX: pointers to function should warn here */
1167 ctype = rtype;
1168 goto Qual;
1170 /* XXX: that should be pointer to composite */
1171 ctype = ltype;
1172 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1173 qual, qual);
1174 if (!typediff)
1175 goto Qual;
1176 goto Err;
1179 /* void on void, struct on same struct, union on same union */
1180 if (ltype == rtype) {
1181 ctype = ltype;
1182 goto out;
1184 typediff = "different base types";
1186 Err:
1187 expression_error(expr, "incompatible types in conditional expression (%s)", typediff);
1188 return NULL;
1190 out:
1191 expr->ctype = ctype;
1192 return ctype;
1194 Qual:
1195 if (qual & ~ctype->ctype.modifiers) {
1196 struct symbol *sym = alloc_symbol(ctype->pos, SYM_PTR);
1197 *sym = *ctype;
1198 sym->ctype.modifiers |= qual;
1199 ctype = sym;
1201 *true = cast_to(*true, ctype);
1202 expr->cond_false = cast_to(expr->cond_false, ctype);
1203 goto out;
1206 /* FP assignments can not do modulo or bit operations */
1207 static int compatible_float_op(int op)
1209 return op == SPECIAL_ADD_ASSIGN ||
1210 op == SPECIAL_SUB_ASSIGN ||
1211 op == SPECIAL_MUL_ASSIGN ||
1212 op == SPECIAL_DIV_ASSIGN;
1215 static int evaluate_assign_op(struct expression *expr)
1217 struct symbol *target = expr->left->ctype;
1218 struct symbol *source = expr->right->ctype;
1219 struct symbol *t, *s;
1220 int tclass = classify_type(target, &t);
1221 int sclass = classify_type(source, &s);
1222 int op = expr->op;
1224 if (tclass & sclass & TYPE_NUM) {
1225 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1226 expression_error(expr, "invalid assignment");
1227 return 0;
1229 if (tclass & TYPE_RESTRICT) {
1230 if (!restricted_binop(op, t)) {
1231 warning(expr->pos, "bad assignment (%s) to %s",
1232 show_special(op), show_typename(t));
1233 expr->right = cast_to(expr->right, target);
1234 return 0;
1236 /* allowed assignments unfoul */
1237 if (sclass & TYPE_FOULED && unfoul(s) == t)
1238 goto Cast;
1239 if (!restricted_value(expr->right, t))
1240 return 1;
1241 } else if (!(sclass & TYPE_RESTRICT))
1242 goto Cast;
1243 /* source and target would better be identical restricted */
1244 if (t == s)
1245 return 1;
1246 warning(expr->pos, "invalid assignment: %s", show_special(op));
1247 info(expr->pos, " left side has type %s", show_typename(t));
1248 info(expr->pos, " right side has type %s", show_typename(s));
1249 expr->right = cast_to(expr->right, target);
1250 return 0;
1252 if (tclass == TYPE_PTR && is_int(sclass)) {
1253 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1254 unrestrict(expr->right, sclass, &s);
1255 evaluate_ptr_add(expr, s);
1256 return 1;
1258 expression_error(expr, "invalid pointer assignment");
1259 return 0;
1262 expression_error(expr, "invalid assignment");
1263 return 0;
1265 Cast:
1266 expr->right = cast_to(expr->right, target);
1267 return 1;
1270 static int whitelist_pointers(struct symbol *t1, struct symbol *t2)
1272 if (t1 == t2)
1273 return 0; /* yes, 0 - we don't want a cast_to here */
1274 if (t1 == &void_ctype)
1275 return 1;
1276 if (t2 == &void_ctype)
1277 return 1;
1278 if (classify_type(t1, &t1) != TYPE_NUM)
1279 return 0;
1280 if (classify_type(t2, &t2) != TYPE_NUM)
1281 return 0;
1282 if (t1 == t2)
1283 return 1;
1284 if (t1->ctype.modifiers & t2->ctype.modifiers & MOD_CHAR)
1285 return 1;
1286 if ((t1->ctype.modifiers ^ t2->ctype.modifiers) & MOD_SIZE)
1287 return 0;
1288 return !Wtypesign;
1291 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1292 struct expression **rp, const char *where)
1294 const char *typediff;
1295 struct symbol *source = degenerate(*rp);
1296 struct symbol *t, *s;
1297 int tclass = classify_type(target, &t);
1298 int sclass = classify_type(source, &s);
1300 if (tclass & sclass & TYPE_NUM) {
1301 if (tclass & TYPE_RESTRICT) {
1302 /* allowed assignments unfoul */
1303 if (sclass & TYPE_FOULED && unfoul(s) == t)
1304 goto Cast;
1305 if (!restricted_value(*rp, target))
1306 return 1;
1307 if (s == t)
1308 return 1;
1309 } else if (!(sclass & TYPE_RESTRICT))
1310 goto Cast;
1311 typediff = "different base types";
1312 goto Err;
1315 if (tclass == TYPE_PTR) {
1316 unsigned long mod1, mod2;
1317 struct symbol *b1, *b2;
1318 // NULL pointer is always OK
1319 int is_null = is_null_pointer_constant(*rp);
1320 if (is_null) {
1321 if (is_null == 2)
1322 bad_null(*rp);
1323 goto Cast;
1325 if (!(sclass & TYPE_PTR)) {
1326 typediff = "different base types";
1327 goto Err;
1329 b1 = examine_pointer_target(t);
1330 b2 = examine_pointer_target(s);
1331 mod1 = target_qualifiers(t);
1332 mod2 = target_qualifiers(s);
1333 if (whitelist_pointers(b1, b2)) {
1335 * assignments to/from void * are OK, provided that
1336 * we do not remove qualifiers from pointed to [C]
1337 * or mix address spaces [sparse].
1339 if (t->ctype.as != s->ctype.as) {
1340 typediff = "different address spaces";
1341 goto Err;
1343 if (mod2 & ~mod1) {
1344 typediff = "different modifiers";
1345 goto Err;
1347 goto Cast;
1349 /* It's OK if the target is more volatile or const than the source */
1350 typediff = type_difference(&t->ctype, &s->ctype, 0, mod1);
1351 if (typediff)
1352 goto Err;
1353 return 1;
1356 if ((tclass & TYPE_COMPOUND) && s == t)
1357 return 1;
1359 if (tclass & TYPE_NUM) {
1360 /* XXX: need to turn into comparison with NULL */
1361 if (t == &bool_ctype && (sclass & TYPE_PTR))
1362 goto Cast;
1363 typediff = "different base types";
1364 goto Err;
1366 typediff = "invalid types";
1368 Err:
1369 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1370 info(expr->pos, " expected %s", show_typename(target));
1371 info(expr->pos, " got %s", show_typename(source));
1372 *rp = cast_to(*rp, target);
1373 return 0;
1374 Cast:
1375 *rp = cast_to(*rp, target);
1376 return 1;
1379 static void mark_assigned(struct expression *expr)
1381 struct symbol *sym;
1383 if (!expr)
1384 return;
1385 switch (expr->type) {
1386 case EXPR_SYMBOL:
1387 sym = expr->symbol;
1388 if (!sym)
1389 return;
1390 if (sym->type != SYM_NODE)
1391 return;
1392 sym->ctype.modifiers |= MOD_ASSIGNED;
1393 return;
1395 case EXPR_BINOP:
1396 mark_assigned(expr->left);
1397 mark_assigned(expr->right);
1398 return;
1399 case EXPR_CAST:
1400 case EXPR_FORCE_CAST:
1401 mark_assigned(expr->cast_expression);
1402 return;
1403 case EXPR_SLICE:
1404 mark_assigned(expr->base);
1405 return;
1406 default:
1407 /* Hmm? */
1408 return;
1412 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1414 if (type->ctype.modifiers & MOD_CONST)
1415 expression_error(left, "assignment to const expression");
1417 /* We know left is an lvalue, so it's a "preop-*" */
1418 mark_assigned(left->unop);
1421 static struct symbol *evaluate_assignment(struct expression *expr)
1423 struct expression *left = expr->left;
1424 struct expression *where = expr;
1425 struct symbol *ltype;
1427 if (!lvalue_expression(left)) {
1428 expression_error(expr, "not an lvalue");
1429 return NULL;
1432 ltype = left->ctype;
1434 if (expr->op != '=') {
1435 if (!evaluate_assign_op(expr))
1436 return NULL;
1437 } else {
1438 if (!compatible_assignment_types(where, ltype, &expr->right, "assignment"))
1439 return NULL;
1442 evaluate_assign_to(left, ltype);
1444 expr->ctype = ltype;
1445 return ltype;
1448 static void examine_fn_arguments(struct symbol *fn)
1450 struct symbol *s;
1452 FOR_EACH_PTR(fn->arguments, s) {
1453 struct symbol *arg = evaluate_symbol(s);
1454 /* Array/function arguments silently degenerate into pointers */
1455 if (arg) {
1456 struct symbol *ptr;
1457 switch(arg->type) {
1458 case SYM_ARRAY:
1459 case SYM_FN:
1460 ptr = alloc_symbol(s->pos, SYM_PTR);
1461 if (arg->type == SYM_ARRAY)
1462 ptr->ctype = arg->ctype;
1463 else
1464 ptr->ctype.base_type = arg;
1465 ptr->ctype.as |= s->ctype.as;
1466 ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1468 s->ctype.base_type = ptr;
1469 s->ctype.as = 0;
1470 s->ctype.modifiers &= ~MOD_PTRINHERIT;
1471 s->bit_size = 0;
1472 s->examined = 0;
1473 examine_symbol_type(s);
1474 break;
1475 default:
1476 /* nothing */
1477 break;
1480 } END_FOR_EACH_PTR(s);
1483 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1485 /* Take the modifiers of the pointer, and apply them to the member */
1486 mod |= sym->ctype.modifiers;
1487 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1488 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1489 *newsym = *sym;
1490 newsym->ctype.as = as;
1491 newsym->ctype.modifiers = mod;
1492 sym = newsym;
1494 return sym;
1497 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1499 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1500 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1502 node->ctype.base_type = ptr;
1503 ptr->bit_size = bits_in_pointer;
1504 ptr->ctype.alignment = pointer_alignment;
1506 node->bit_size = bits_in_pointer;
1507 node->ctype.alignment = pointer_alignment;
1509 access_symbol(sym);
1510 if (sym->ctype.modifiers & MOD_REGISTER) {
1511 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1512 sym->ctype.modifiers &= ~MOD_REGISTER;
1514 if (sym->type == SYM_NODE) {
1515 ptr->ctype.as |= sym->ctype.as;
1516 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1517 sym = sym->ctype.base_type;
1519 if (degenerate && sym->type == SYM_ARRAY) {
1520 ptr->ctype.as |= sym->ctype.as;
1521 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1522 sym = sym->ctype.base_type;
1524 ptr->ctype.base_type = sym;
1526 return node;
1529 /* Arrays degenerate into pointers on pointer arithmetic */
1530 static struct symbol *degenerate(struct expression *expr)
1532 struct symbol *ctype, *base;
1534 if (!expr)
1535 return NULL;
1536 ctype = expr->ctype;
1537 if (!ctype)
1538 return NULL;
1539 base = examine_symbol_type(ctype);
1540 if (ctype->type == SYM_NODE)
1541 base = ctype->ctype.base_type;
1543 * Arrays degenerate into pointers to the entries, while
1544 * functions degenerate into pointers to themselves.
1545 * If array was part of non-lvalue compound, we create a copy
1546 * of that compound first and then act as if we were dealing with
1547 * the corresponding field in there.
1549 switch (base->type) {
1550 case SYM_ARRAY:
1551 if (expr->type == EXPR_SLICE) {
1552 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1553 struct expression *e0, *e1, *e2, *e3, *e4;
1555 a->ctype.base_type = expr->base->ctype;
1556 a->bit_size = expr->base->ctype->bit_size;
1557 a->array_size = expr->base->ctype->array_size;
1559 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1560 e0->symbol = a;
1561 e0->ctype = &lazy_ptr_ctype;
1563 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1564 e1->unop = e0;
1565 e1->op = '*';
1566 e1->ctype = expr->base->ctype; /* XXX */
1568 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1569 e2->left = e1;
1570 e2->right = expr->base;
1571 e2->op = '=';
1572 e2->ctype = expr->base->ctype;
1574 if (expr->r_bitpos) {
1575 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1576 e3->op = '+';
1577 e3->left = e0;
1578 e3->right = alloc_const_expression(expr->pos,
1579 bits_to_bytes(expr->r_bitpos));
1580 e3->ctype = &lazy_ptr_ctype;
1581 } else {
1582 e3 = e0;
1585 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1586 e4->left = e2;
1587 e4->right = e3;
1588 e4->ctype = &lazy_ptr_ctype;
1590 expr->unop = e4;
1591 expr->type = EXPR_PREOP;
1592 expr->op = '*';
1594 case SYM_FN:
1595 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1596 expression_error(expr, "strange non-value function or array");
1597 return &bad_ctype;
1599 *expr = *expr->unop;
1600 ctype = create_pointer(expr, ctype, 1);
1601 expr->ctype = ctype;
1602 default:
1603 /* nothing */;
1605 return ctype;
1608 static struct symbol *evaluate_addressof(struct expression *expr)
1610 struct expression *op = expr->unop;
1611 struct symbol *ctype;
1613 if (op->op != '*' || op->type != EXPR_PREOP) {
1614 expression_error(expr, "not addressable");
1615 return NULL;
1617 ctype = op->ctype;
1618 *expr = *op->unop;
1619 expr->flags = 0;
1621 if (expr->type == EXPR_SYMBOL) {
1622 struct symbol *sym = expr->symbol;
1623 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1627 * symbol expression evaluation is lazy about the type
1628 * of the sub-expression, so we may have to generate
1629 * the type here if so..
1631 if (expr->ctype == &lazy_ptr_ctype) {
1632 ctype = create_pointer(expr, ctype, 0);
1633 expr->ctype = ctype;
1635 return expr->ctype;
1639 static struct symbol *evaluate_dereference(struct expression *expr)
1641 struct expression *op = expr->unop;
1642 struct symbol *ctype = op->ctype, *node, *target;
1644 /* Simplify: *&(expr) => (expr) */
1645 if (op->type == EXPR_PREOP && op->op == '&') {
1646 *expr = *op->unop;
1647 expr->flags = 0;
1648 return expr->ctype;
1651 /* Dereferencing a node drops all the node information. */
1652 if (ctype->type == SYM_NODE)
1653 ctype = ctype->ctype.base_type;
1655 node = alloc_symbol(expr->pos, SYM_NODE);
1656 target = ctype->ctype.base_type;
1658 switch (ctype->type) {
1659 default:
1660 expression_error(expr, "cannot dereference this type");
1661 return NULL;
1662 case SYM_PTR:
1663 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1664 merge_type(node, ctype);
1665 break;
1667 case SYM_ARRAY:
1668 if (!lvalue_expression(op)) {
1669 expression_error(op, "non-lvalue array??");
1670 return NULL;
1673 /* Do the implied "addressof" on the array */
1674 *op = *op->unop;
1677 * When an array is dereferenced, we need to pick
1678 * up the attributes of the original node too..
1680 merge_type(node, op->ctype);
1681 merge_type(node, ctype);
1682 break;
1685 node->bit_size = target->bit_size;
1686 node->array_size = target->array_size;
1688 expr->ctype = node;
1689 return node;
1693 * Unary post-ops: x++ and x--
1695 static struct symbol *evaluate_postop(struct expression *expr)
1697 struct expression *op = expr->unop;
1698 struct symbol *ctype = op->ctype;
1699 int class = classify_type(ctype, &ctype);
1700 int multiply = 0;
1702 if (!class || class & TYPE_COMPOUND) {
1703 expression_error(expr, "need scalar for ++/--");
1704 return NULL;
1706 if (!lvalue_expression(expr->unop)) {
1707 expression_error(expr, "need lvalue expression for ++/--");
1708 return NULL;
1711 if ((class & TYPE_RESTRICT) && restricted_unop(expr->op, &ctype))
1712 unrestrict(expr, class, &ctype);
1714 if (class & TYPE_NUM) {
1715 multiply = 1;
1716 } else if (class == TYPE_PTR) {
1717 struct symbol *target = examine_pointer_target(ctype);
1718 if (!is_function(target))
1719 multiply = bits_to_bytes(target->bit_size);
1722 if (multiply) {
1723 evaluate_assign_to(op, op->ctype);
1724 expr->op_value = multiply;
1725 expr->ctype = ctype;
1726 return ctype;
1729 expression_error(expr, "bad argument type for ++/--");
1730 return NULL;
1733 static struct symbol *evaluate_sign(struct expression *expr)
1735 struct symbol *ctype = expr->unop->ctype;
1736 int class = classify_type(ctype, &ctype);
1737 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1738 expr->flags = 0;
1739 /* should be an arithmetic type */
1740 if (!(class & TYPE_NUM))
1741 return bad_expr_type(expr);
1742 if (class & TYPE_RESTRICT)
1743 goto Restr;
1744 Normal:
1745 if (!(class & TYPE_FLOAT)) {
1746 ctype = integer_promotion(ctype);
1747 expr->unop = cast_to(expr->unop, ctype);
1748 } else if (expr->op != '~') {
1749 /* no conversions needed */
1750 } else {
1751 return bad_expr_type(expr);
1753 if (expr->op == '+')
1754 *expr = *expr->unop;
1755 expr->ctype = ctype;
1756 return ctype;
1757 Restr:
1758 if (restricted_unop(expr->op, &ctype))
1759 unrestrict(expr, class, &ctype);
1760 goto Normal;
1763 static struct symbol *evaluate_preop(struct expression *expr)
1765 struct symbol *ctype = expr->unop->ctype;
1767 switch (expr->op) {
1768 case '(':
1769 *expr = *expr->unop;
1770 return ctype;
1772 case '+':
1773 case '-':
1774 case '~':
1775 return evaluate_sign(expr);
1777 case '*':
1778 return evaluate_dereference(expr);
1780 case '&':
1781 return evaluate_addressof(expr);
1783 case SPECIAL_INCREMENT:
1784 case SPECIAL_DECREMENT:
1786 * From a type evaluation standpoint the preops are
1787 * the same as the postops
1789 return evaluate_postop(expr);
1791 case '!':
1792 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1793 expr->flags = 0;
1794 if (is_safe_type(ctype))
1795 warning(expr->pos, "testing a 'safe expression'");
1796 if (is_float_type(ctype)) {
1797 struct expression *arg = expr->unop;
1798 expr->type = EXPR_BINOP;
1799 expr->op = SPECIAL_EQUAL;
1800 expr->left = arg;
1801 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1802 expr->right->ctype = ctype;
1803 expr->right->fvalue = 0;
1804 } else if (is_fouled_type(ctype)) {
1805 warning(expr->pos, "%s degrades to integer",
1806 show_typename(ctype->ctype.base_type));
1808 ctype = &bool_ctype;
1809 break;
1811 default:
1812 break;
1814 expr->ctype = ctype;
1815 return &bool_ctype;
1818 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1820 struct ptr_list *head = (struct ptr_list *)_list;
1821 struct ptr_list *list = head;
1823 if (!head)
1824 return NULL;
1825 do {
1826 int i;
1827 for (i = 0; i < list->nr; i++) {
1828 struct symbol *sym = (struct symbol *) list->list[i];
1829 if (sym->ident) {
1830 if (sym->ident != ident)
1831 continue;
1832 *offset = sym->offset;
1833 return sym;
1834 } else {
1835 struct symbol *ctype = sym->ctype.base_type;
1836 struct symbol *sub;
1837 if (!ctype)
1838 continue;
1839 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1840 continue;
1841 sub = find_identifier(ident, ctype->symbol_list, offset);
1842 if (!sub)
1843 continue;
1844 *offset += sym->offset;
1845 return sub;
1848 } while ((list = list->next) != head);
1849 return NULL;
1852 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1854 struct expression *add;
1857 * Create a new add-expression
1859 * NOTE! Even if we just add zero, we need a new node
1860 * for the member pointer, since it has a different
1861 * type than the original pointer. We could make that
1862 * be just a cast, but the fact is, a node is a node,
1863 * so we might as well just do the "add zero" here.
1865 add = alloc_expression(expr->pos, EXPR_BINOP);
1866 add->op = '+';
1867 add->left = expr;
1868 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1869 add->right->ctype = &int_ctype;
1870 add->right->value = offset;
1873 * The ctype of the pointer will be lazily evaluated if
1874 * we ever take the address of this member dereference..
1876 add->ctype = &lazy_ptr_ctype;
1877 return add;
1880 /* structure/union dereference */
1881 static struct symbol *evaluate_member_dereference(struct expression *expr)
1883 int offset;
1884 struct symbol *ctype, *member;
1885 struct expression *deref = expr->deref, *add;
1886 struct ident *ident = expr->member;
1887 unsigned int mod;
1888 int address_space;
1890 if (!evaluate_expression(deref))
1891 return NULL;
1892 if (!ident) {
1893 expression_error(expr, "bad member name");
1894 return NULL;
1897 ctype = deref->ctype;
1898 examine_symbol_type(ctype);
1899 address_space = ctype->ctype.as;
1900 mod = ctype->ctype.modifiers;
1901 if (ctype->type == SYM_NODE) {
1902 ctype = ctype->ctype.base_type;
1903 address_space |= ctype->ctype.as;
1904 mod |= ctype->ctype.modifiers;
1906 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1907 expression_error(expr, "expected structure or union");
1908 return NULL;
1910 offset = 0;
1911 member = find_identifier(ident, ctype->symbol_list, &offset);
1912 if (!member) {
1913 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1914 const char *name = "<unnamed>";
1915 int namelen = 9;
1916 if (ctype->ident) {
1917 name = ctype->ident->name;
1918 namelen = ctype->ident->len;
1920 if (ctype->symbol_list)
1921 expression_error(expr, "no member '%s' in %s %.*s",
1922 show_ident(ident), type, namelen, name);
1923 else
1924 expression_error(expr, "using member '%s' in "
1925 "incomplete %s %.*s", show_ident(ident),
1926 type, namelen, name);
1927 return NULL;
1931 * The member needs to take on the address space and modifiers of
1932 * the "parent" type.
1934 member = convert_to_as_mod(member, address_space, mod);
1935 ctype = get_base_type(member);
1937 if (!lvalue_expression(deref)) {
1938 if (deref->type != EXPR_SLICE) {
1939 expr->base = deref;
1940 expr->r_bitpos = 0;
1941 } else {
1942 expr->base = deref->base;
1943 expr->r_bitpos = deref->r_bitpos;
1945 expr->r_bitpos += bytes_to_bits(offset);
1946 expr->type = EXPR_SLICE;
1947 expr->r_nrbits = member->bit_size;
1948 expr->r_bitpos += member->bit_offset;
1949 expr->ctype = member;
1950 return member;
1953 deref = deref->unop;
1954 expr->deref = deref;
1956 add = evaluate_offset(deref, offset);
1957 expr->type = EXPR_PREOP;
1958 expr->op = '*';
1959 expr->unop = add;
1961 expr->ctype = member;
1962 return member;
1965 static int is_promoted(struct expression *expr)
1967 while (1) {
1968 switch (expr->type) {
1969 case EXPR_BINOP:
1970 case EXPR_SELECT:
1971 case EXPR_CONDITIONAL:
1972 return 1;
1973 case EXPR_COMMA:
1974 expr = expr->right;
1975 continue;
1976 case EXPR_PREOP:
1977 switch (expr->op) {
1978 case '(':
1979 expr = expr->unop;
1980 continue;
1981 case '+':
1982 case '-':
1983 case '~':
1984 return 1;
1985 default:
1986 return 0;
1988 default:
1989 return 0;
1995 static struct symbol *evaluate_cast(struct expression *);
1997 static struct symbol *evaluate_type_information(struct expression *expr)
1999 struct symbol *sym = expr->cast_type;
2000 if (!sym) {
2001 sym = evaluate_expression(expr->cast_expression);
2002 if (!sym)
2003 return NULL;
2005 * Expressions of restricted types will possibly get
2006 * promoted - check that here
2008 if (is_restricted_type(sym)) {
2009 if (sym->bit_size < bits_in_int && is_promoted(expr))
2010 sym = &int_ctype;
2011 } else if (is_fouled_type(sym)) {
2012 sym = &int_ctype;
2015 examine_symbol_type(sym);
2016 if (is_bitfield_type(sym)) {
2017 expression_error(expr, "trying to examine bitfield type");
2018 return NULL;
2020 return sym;
2023 static struct symbol *evaluate_sizeof(struct expression *expr)
2025 struct symbol *type;
2026 int size;
2028 type = evaluate_type_information(expr);
2029 if (!type)
2030 return NULL;
2032 size = type->bit_size;
2034 if (size < 0 && is_void_type(type)) {
2035 warning(expr->pos, "expression using sizeof(void)");
2036 size = bits_in_char;
2039 if (size == 1 && is_bool_type(type)) {
2040 warning(expr->pos, "expression using sizeof bool");
2041 size = bits_in_char;
2044 if (is_function(type->ctype.base_type)) {
2045 warning(expr->pos, "expression using sizeof on a function");
2046 size = bits_in_char;
2049 if ((size < 0) || (size & (bits_in_char - 1)))
2050 expression_error(expr, "cannot size expression");
2052 expr->type = EXPR_VALUE;
2053 expr->value = bits_to_bytes(size);
2054 expr->taint = 0;
2055 expr->ctype = size_t_ctype;
2056 return size_t_ctype;
2059 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
2061 struct symbol *type;
2062 int size;
2064 type = evaluate_type_information(expr);
2065 if (!type)
2066 return NULL;
2068 if (type->type == SYM_NODE)
2069 type = type->ctype.base_type;
2070 if (!type)
2071 return NULL;
2072 switch (type->type) {
2073 case SYM_ARRAY:
2074 break;
2075 case SYM_PTR:
2076 type = get_base_type(type);
2077 if (type)
2078 break;
2079 default:
2080 expression_error(expr, "expected pointer expression");
2081 return NULL;
2083 size = type->bit_size;
2084 if (size & (bits_in_char-1))
2085 size = 0;
2086 expr->type = EXPR_VALUE;
2087 expr->value = bits_to_bytes(size);
2088 expr->taint = 0;
2089 expr->ctype = size_t_ctype;
2090 return size_t_ctype;
2093 static struct symbol *evaluate_alignof(struct expression *expr)
2095 struct symbol *type;
2097 type = evaluate_type_information(expr);
2098 if (!type)
2099 return NULL;
2101 expr->type = EXPR_VALUE;
2102 expr->value = type->ctype.alignment;
2103 expr->taint = 0;
2104 expr->ctype = size_t_ctype;
2105 return size_t_ctype;
2108 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
2110 struct expression *expr;
2111 struct symbol_list *argument_types = fn->arguments;
2112 struct symbol *argtype;
2113 int i = 1;
2115 PREPARE_PTR_LIST(argument_types, argtype);
2116 FOR_EACH_PTR (head, expr) {
2117 struct expression **p = THIS_ADDRESS(expr);
2118 struct symbol *ctype, *target;
2119 ctype = evaluate_expression(expr);
2121 if (!ctype)
2122 return 0;
2124 target = argtype;
2125 if (!target) {
2126 struct symbol *type;
2127 int class = classify_type(ctype, &type);
2128 if (is_int(class)) {
2129 *p = cast_to(expr, integer_promotion(type));
2130 } else if (class & TYPE_FLOAT) {
2131 unsigned long mod = type->ctype.modifiers;
2132 if (!(mod & (MOD_LONG_ALL)))
2133 *p = cast_to(expr, &double_ctype);
2134 } else if (class & TYPE_PTR) {
2135 if (expr->ctype == &null_ctype)
2136 *p = cast_to(expr, &ptr_ctype);
2137 else
2138 degenerate(expr);
2140 } else if (!target->forced_arg){
2141 static char where[30];
2142 examine_symbol_type(target);
2143 sprintf(where, "argument %d", i);
2144 compatible_assignment_types(expr, target, p, where);
2147 i++;
2148 NEXT_PTR_LIST(argtype);
2149 } END_FOR_EACH_PTR(expr);
2150 FINISH_PTR_LIST(argtype);
2151 return 1;
2154 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
2156 struct symbol *sym;
2158 FOR_EACH_PTR(ctype->symbol_list, sym) {
2159 if (sym->ident == ident)
2160 return sym;
2161 } END_FOR_EACH_PTR(sym);
2162 return NULL;
2165 static void convert_index(struct expression *e)
2167 struct expression *child = e->idx_expression;
2168 unsigned from = e->idx_from;
2169 unsigned to = e->idx_to + 1;
2170 e->type = EXPR_POS;
2171 e->init_offset = from * bits_to_bytes(e->ctype->bit_size);
2172 e->init_nr = to - from;
2173 e->init_expr = child;
2176 static void convert_ident(struct expression *e)
2178 struct expression *child = e->ident_expression;
2179 struct symbol *sym = e->field;
2180 e->type = EXPR_POS;
2181 e->init_offset = sym->offset;
2182 e->init_nr = 1;
2183 e->init_expr = child;
2186 static void convert_designators(struct expression *e)
2188 while (e) {
2189 if (e->type == EXPR_INDEX)
2190 convert_index(e);
2191 else if (e->type == EXPR_IDENTIFIER)
2192 convert_ident(e);
2193 else
2194 break;
2195 e = e->init_expr;
2199 static void excess(struct expression *e, const char *s)
2201 warning(e->pos, "excessive elements in %s initializer", s);
2205 * implicit designator for the first element
2207 static struct expression *first_subobject(struct symbol *ctype, int class,
2208 struct expression **v)
2210 struct expression *e = *v, *new;
2212 if (ctype->type == SYM_NODE)
2213 ctype = ctype->ctype.base_type;
2215 if (class & TYPE_PTR) { /* array */
2216 if (!ctype->bit_size)
2217 return NULL;
2218 new = alloc_expression(e->pos, EXPR_INDEX);
2219 new->idx_expression = e;
2220 new->ctype = ctype->ctype.base_type;
2221 } else {
2222 struct symbol *field, *p;
2223 PREPARE_PTR_LIST(ctype->symbol_list, p);
2224 while (p && !p->ident && is_bitfield_type(p))
2225 NEXT_PTR_LIST(p);
2226 field = p;
2227 FINISH_PTR_LIST(p);
2228 if (!field)
2229 return NULL;
2230 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2231 new->ident_expression = e;
2232 new->field = new->ctype = field;
2234 *v = new;
2235 return new;
2239 * sanity-check explicit designators; return the innermost one or NULL
2240 * in case of error. Assign types.
2242 static struct expression *check_designators(struct expression *e,
2243 struct symbol *ctype)
2245 struct expression *last = NULL;
2246 const char *err;
2247 while (1) {
2248 if (ctype->type == SYM_NODE)
2249 ctype = ctype->ctype.base_type;
2250 if (e->type == EXPR_INDEX) {
2251 struct symbol *type;
2252 if (ctype->type != SYM_ARRAY) {
2253 err = "array index in non-array";
2254 break;
2256 type = ctype->ctype.base_type;
2257 if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2258 unsigned offset = e->idx_to * type->bit_size;
2259 if (offset >= ctype->bit_size) {
2260 err = "index out of bounds in";
2261 break;
2264 e->ctype = ctype = type;
2265 ctype = type;
2266 last = e;
2267 if (!e->idx_expression) {
2268 err = "invalid";
2269 break;
2271 e = e->idx_expression;
2272 } else if (e->type == EXPR_IDENTIFIER) {
2273 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2274 err = "field name not in struct or union";
2275 break;
2277 ctype = find_struct_ident(ctype, e->expr_ident);
2278 if (!ctype) {
2279 err = "unknown field name in";
2280 break;
2282 e->field = e->ctype = ctype;
2283 last = e;
2284 if (!e->ident_expression) {
2285 err = "invalid";
2286 break;
2288 e = e->ident_expression;
2289 } else if (e->type == EXPR_POS) {
2290 err = "internal front-end error: EXPR_POS in";
2291 break;
2292 } else
2293 return last;
2295 expression_error(e, "%s initializer", err);
2296 return NULL;
2300 * choose the next subobject to initialize.
2302 * Get designators for next element, switch old ones to EXPR_POS.
2303 * Return the resulting expression or NULL if we'd run out of subobjects.
2304 * The innermost designator is returned in *v. Designators in old
2305 * are assumed to be already sanity-checked.
2307 static struct expression *next_designators(struct expression *old,
2308 struct symbol *ctype,
2309 struct expression *e, struct expression **v)
2311 struct expression *new = NULL;
2313 if (!old)
2314 return NULL;
2315 if (old->type == EXPR_INDEX) {
2316 struct expression *copy;
2317 unsigned n;
2319 copy = next_designators(old->idx_expression,
2320 old->ctype, e, v);
2321 if (!copy) {
2322 n = old->idx_to + 1;
2323 if (n * old->ctype->bit_size == ctype->bit_size) {
2324 convert_index(old);
2325 return NULL;
2327 copy = e;
2328 *v = new = alloc_expression(e->pos, EXPR_INDEX);
2329 } else {
2330 n = old->idx_to;
2331 new = alloc_expression(e->pos, EXPR_INDEX);
2334 new->idx_from = new->idx_to = n;
2335 new->idx_expression = copy;
2336 new->ctype = old->ctype;
2337 convert_index(old);
2338 } else if (old->type == EXPR_IDENTIFIER) {
2339 struct expression *copy;
2340 struct symbol *field;
2342 copy = next_designators(old->ident_expression,
2343 old->ctype, e, v);
2344 if (!copy) {
2345 field = old->field->next_subobject;
2346 if (!field) {
2347 convert_ident(old);
2348 return NULL;
2350 copy = e;
2351 *v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2352 } else {
2353 field = old->field;
2354 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2357 new->field = field;
2358 new->expr_ident = field->ident;
2359 new->ident_expression = copy;
2360 new->ctype = field;
2361 convert_ident(old);
2363 return new;
2366 static int handle_simple_initializer(struct expression **ep, int nested,
2367 int class, struct symbol *ctype);
2370 * deal with traversing subobjects [6.7.8(17,18,20)]
2372 static void handle_list_initializer(struct expression *expr,
2373 int class, struct symbol *ctype)
2375 struct expression *e, *last = NULL, *top = NULL, *next;
2376 int jumped = 0;
2378 FOR_EACH_PTR(expr->expr_list, e) {
2379 struct expression **v;
2380 struct symbol *type;
2381 int lclass;
2383 if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2384 struct symbol *struct_sym;
2385 if (!top) {
2386 top = e;
2387 last = first_subobject(ctype, class, &top);
2388 } else {
2389 last = next_designators(last, ctype, e, &top);
2391 if (!last) {
2392 excess(e, class & TYPE_PTR ? "array" :
2393 "struct or union");
2394 DELETE_CURRENT_PTR(e);
2395 continue;
2397 struct_sym = ctype->type == SYM_NODE ? ctype->ctype.base_type : ctype;
2398 if (Wdesignated_init && struct_sym->designated_init)
2399 warning(e->pos, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2400 ctype->ident ? "in initializer for " : "",
2401 ctype->ident ? ctype->ident->len : 0,
2402 ctype->ident ? ctype->ident->name : "",
2403 ctype->ident ? ": " : "",
2404 get_type_name(struct_sym->type),
2405 show_ident(struct_sym->ident));
2406 if (jumped) {
2407 warning(e->pos, "advancing past deep designator");
2408 jumped = 0;
2410 REPLACE_CURRENT_PTR(e, last);
2411 } else {
2412 next = check_designators(e, ctype);
2413 if (!next) {
2414 DELETE_CURRENT_PTR(e);
2415 continue;
2417 top = next;
2418 /* deeper than one designator? */
2419 jumped = top != e;
2420 convert_designators(last);
2421 last = e;
2424 found:
2425 lclass = classify_type(top->ctype, &type);
2426 if (top->type == EXPR_INDEX)
2427 v = &top->idx_expression;
2428 else
2429 v = &top->ident_expression;
2431 if (handle_simple_initializer(v, 1, lclass, top->ctype))
2432 continue;
2434 if (!(lclass & TYPE_COMPOUND)) {
2435 warning(e->pos, "bogus scalar initializer");
2436 DELETE_CURRENT_PTR(e);
2437 continue;
2440 next = first_subobject(type, lclass, v);
2441 if (next) {
2442 warning(e->pos, "missing braces around initializer");
2443 top = next;
2444 goto found;
2447 DELETE_CURRENT_PTR(e);
2448 excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2450 } END_FOR_EACH_PTR(e);
2452 convert_designators(last);
2453 expr->ctype = ctype;
2456 static int is_string_literal(struct expression **v)
2458 struct expression *e = *v;
2459 while (e && e->type == EXPR_PREOP && e->op == '(')
2460 e = e->unop;
2461 if (!e || e->type != EXPR_STRING)
2462 return 0;
2463 if (e != *v && Wparen_string)
2464 warning(e->pos,
2465 "array initialized from parenthesized string constant");
2466 *v = e;
2467 return 1;
2471 * We want a normal expression, possibly in one layer of braces. Warn
2472 * if the latter happens inside a list (it's legal, but likely to be
2473 * an effect of screwup). In case of anything not legal, we are definitely
2474 * having an effect of screwup, so just fail and let the caller warn.
2476 static struct expression *handle_scalar(struct expression *e, int nested)
2478 struct expression *v = NULL, *p;
2479 int count = 0;
2481 /* normal case */
2482 if (e->type != EXPR_INITIALIZER)
2483 return e;
2485 FOR_EACH_PTR(e->expr_list, p) {
2486 if (!v)
2487 v = p;
2488 count++;
2489 } END_FOR_EACH_PTR(p);
2490 if (count != 1)
2491 return NULL;
2492 switch(v->type) {
2493 case EXPR_INITIALIZER:
2494 case EXPR_INDEX:
2495 case EXPR_IDENTIFIER:
2496 return NULL;
2497 default:
2498 break;
2500 if (nested)
2501 warning(e->pos, "braces around scalar initializer");
2502 return v;
2506 * deal with the cases that don't care about subobjects:
2507 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2508 * character array <- string literal, possibly in braces [6.7.8(14)]
2509 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2510 * compound type <- initializer list in braces [6.7.8(16)]
2511 * The last one punts to handle_list_initializer() which, in turn will call
2512 * us for individual elements of the list.
2514 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2515 * the lack of support of wide char stuff in general.
2517 * One note: we need to take care not to evaluate a string literal until
2518 * we know that we *will* handle it right here. Otherwise we would screw
2519 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2520 * { "string", ...} - we need to preserve that string literal recognizable
2521 * until we dig into the inner struct.
2523 static int handle_simple_initializer(struct expression **ep, int nested,
2524 int class, struct symbol *ctype)
2526 int is_string = is_string_type(ctype);
2527 struct expression *e = *ep, *p;
2528 struct symbol *type;
2530 if (!e)
2531 return 0;
2533 /* scalar */
2534 if (!(class & TYPE_COMPOUND)) {
2535 e = handle_scalar(e, nested);
2536 if (!e)
2537 return 0;
2538 *ep = e;
2539 if (!evaluate_expression(e))
2540 return 1;
2541 compatible_assignment_types(e, ctype, ep, "initializer");
2542 return 1;
2546 * sublist; either a string, or we dig in; the latter will deal with
2547 * pathologies, so we don't need anything fancy here.
2549 if (e->type == EXPR_INITIALIZER) {
2550 if (is_string) {
2551 struct expression *v = NULL;
2552 int count = 0;
2554 FOR_EACH_PTR(e->expr_list, p) {
2555 if (!v)
2556 v = p;
2557 count++;
2558 } END_FOR_EACH_PTR(p);
2559 if (count == 1 && is_string_literal(&v)) {
2560 *ep = e = v;
2561 goto String;
2564 handle_list_initializer(e, class, ctype);
2565 return 1;
2568 /* string */
2569 if (is_string_literal(&e)) {
2570 /* either we are doing array of char, or we'll have to dig in */
2571 if (is_string) {
2572 *ep = e;
2573 goto String;
2575 return 0;
2577 /* struct or union can be initialized by compatible */
2578 if (class != TYPE_COMPOUND)
2579 return 0;
2580 type = evaluate_expression(e);
2581 if (!type)
2582 return 0;
2583 if (ctype->type == SYM_NODE)
2584 ctype = ctype->ctype.base_type;
2585 if (type->type == SYM_NODE)
2586 type = type->ctype.base_type;
2587 if (ctype == type)
2588 return 1;
2589 return 0;
2591 String:
2592 p = alloc_expression(e->pos, EXPR_STRING);
2593 *p = *e;
2594 type = evaluate_expression(p);
2595 if (ctype->bit_size != -1) {
2596 if (ctype->bit_size + bits_in_char < type->bit_size)
2597 warning(e->pos,
2598 "too long initializer-string for array of char");
2599 else if (Winit_cstring && ctype->bit_size + bits_in_char == type->bit_size) {
2600 warning(e->pos,
2601 "too long initializer-string for array of char(no space for nul char)");
2604 *ep = p;
2605 return 1;
2608 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2610 struct symbol *type;
2611 int class = classify_type(ctype, &type);
2612 if (!handle_simple_initializer(ep, 0, class, ctype))
2613 expression_error(*ep, "invalid initializer");
2616 static struct symbol *evaluate_cast(struct expression *expr)
2618 struct expression *target = expr->cast_expression;
2619 struct symbol *ctype;
2620 struct symbol *t1, *t2;
2621 int class1, class2;
2622 int as1 = 0, as2 = 0;
2624 if (!target)
2625 return NULL;
2628 * Special case: a cast can be followed by an
2629 * initializer, in which case we need to pass
2630 * the type value down to that initializer rather
2631 * than trying to evaluate it as an expression
2633 * A more complex case is when the initializer is
2634 * dereferenced as part of a post-fix expression.
2635 * We need to produce an expression that can be dereferenced.
2637 if (target->type == EXPR_INITIALIZER) {
2638 struct symbol *sym = expr->cast_type;
2639 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2641 sym->initializer = target;
2642 evaluate_symbol(sym);
2644 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2645 addr->symbol = sym;
2647 expr->type = EXPR_PREOP;
2648 expr->op = '*';
2649 expr->unop = addr;
2650 expr->ctype = sym;
2652 return sym;
2655 ctype = examine_symbol_type(expr->cast_type);
2656 expr->ctype = ctype;
2657 expr->cast_type = ctype;
2659 evaluate_expression(target);
2660 degenerate(target);
2662 class1 = classify_type(ctype, &t1);
2664 /* cast to non-integer type -> not an integer constant expression */
2665 if (!is_int(class1))
2666 expr->flags = 0;
2667 /* if argument turns out to be not an integer constant expression *and*
2668 it was not a floating literal to start with -> too bad */
2669 else if (expr->flags == Int_const_expr &&
2670 !(target->flags & Int_const_expr))
2671 expr->flags = 0;
2673 * You can always throw a value away by casting to
2674 * "void" - that's an implicit "force". Note that
2675 * the same is _not_ true of "void *".
2677 if (t1 == &void_ctype)
2678 goto out;
2680 if (class1 & (TYPE_COMPOUND | TYPE_FN))
2681 warning(expr->pos, "cast to non-scalar");
2683 t2 = target->ctype;
2684 if (!t2) {
2685 expression_error(expr, "cast from unknown type");
2686 goto out;
2688 class2 = classify_type(t2, &t2);
2690 if (class2 & TYPE_COMPOUND)
2691 warning(expr->pos, "cast from non-scalar");
2693 if (expr->type == EXPR_FORCE_CAST)
2694 goto out;
2696 /* allowed cast unfouls */
2697 if (class2 & TYPE_FOULED)
2698 t2 = unfoul(t2);
2700 if (t1 != t2) {
2701 if (class1 & TYPE_RESTRICT)
2702 warning(expr->pos, "cast to %s",
2703 show_typename(t1));
2704 if (class2 & TYPE_RESTRICT)
2705 warning(expr->pos, "cast from %s",
2706 show_typename(t2));
2709 if (t1 == &ulong_ctype)
2710 as1 = -1;
2711 else if (class1 == TYPE_PTR) {
2712 examine_pointer_target(t1);
2713 as1 = t1->ctype.as;
2716 if (t2 == &ulong_ctype)
2717 as2 = -1;
2718 else if (class2 == TYPE_PTR) {
2719 examine_pointer_target(t2);
2720 as2 = t2->ctype.as;
2723 if (!as1 && as2 > 0)
2724 warning(expr->pos, "cast removes address space of expression");
2725 if (as1 > 0 && as2 > 0 && as1 != as2)
2726 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2727 if (as1 > 0 && !as2 &&
2728 !is_null_pointer_constant(target) && Wcast_to_as)
2729 warning(expr->pos,
2730 "cast adds address space to expression (<asn:%d>)", as1);
2732 if (!(t1->ctype.modifiers & MOD_PTRINHERIT) && class1 == TYPE_PTR &&
2733 !as1 && (target->flags & Int_const_expr)) {
2734 if (t1->ctype.base_type == &void_ctype) {
2735 if (is_zero_constant(target)) {
2736 /* NULL */
2737 expr->type = EXPR_VALUE;
2738 expr->ctype = &null_ctype;
2739 expr->value = 0;
2740 return ctype;
2744 out:
2745 return ctype;
2749 * Evaluate a call expression with a symbol. This
2750 * should expand inline functions, and evaluate
2751 * builtins.
2753 static int evaluate_symbol_call(struct expression *expr)
2755 struct expression *fn = expr->fn;
2756 struct symbol *ctype = fn->ctype;
2758 if (fn->type != EXPR_PREOP)
2759 return 0;
2761 if (ctype->op && ctype->op->evaluate)
2762 return ctype->op->evaluate(expr);
2764 if (ctype->ctype.modifiers & MOD_INLINE) {
2765 int ret;
2766 struct symbol *curr = current_fn;
2768 if (ctype->definition)
2769 ctype = ctype->definition;
2771 current_fn = ctype->ctype.base_type;
2773 ret = inline_function(expr, ctype);
2775 /* restore the old function */
2776 current_fn = curr;
2777 return ret;
2780 return 0;
2783 static struct symbol *evaluate_call(struct expression *expr)
2785 int args, fnargs;
2786 struct symbol *ctype, *sym;
2787 struct expression *fn = expr->fn;
2788 struct expression_list *arglist = expr->args;
2790 if (!evaluate_expression(fn))
2791 return NULL;
2792 sym = ctype = fn->ctype;
2793 if (ctype->type == SYM_NODE)
2794 ctype = ctype->ctype.base_type;
2795 if (ctype->type == SYM_PTR)
2796 ctype = get_base_type(ctype);
2798 if (ctype->type != SYM_FN) {
2799 struct expression *arg;
2800 expression_error(expr, "not a function %s",
2801 show_ident(sym->ident));
2802 /* do typechecking in arguments */
2803 FOR_EACH_PTR (arglist, arg) {
2804 evaluate_expression(arg);
2805 } END_FOR_EACH_PTR(arg);
2806 return NULL;
2809 examine_fn_arguments(ctype);
2810 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
2811 sym->op && sym->op->args) {
2812 if (!sym->op->args(expr))
2813 return NULL;
2814 } else {
2815 if (!evaluate_arguments(sym, ctype, arglist))
2816 return NULL;
2817 args = expression_list_size(expr->args);
2818 fnargs = symbol_list_size(ctype->arguments);
2819 if (args < fnargs)
2820 expression_error(expr,
2821 "not enough arguments for function %s",
2822 show_ident(sym->ident));
2823 if (args > fnargs && !ctype->variadic)
2824 expression_error(expr,
2825 "too many arguments for function %s",
2826 show_ident(sym->ident));
2828 if (sym->type == SYM_NODE) {
2829 if (evaluate_symbol_call(expr))
2830 return expr->ctype;
2832 expr->ctype = ctype->ctype.base_type;
2833 return expr->ctype;
2836 static struct symbol *evaluate_offsetof(struct expression *expr)
2838 struct expression *e = expr->down;
2839 struct symbol *ctype = expr->in;
2840 int class;
2842 if (expr->op == '.') {
2843 struct symbol *field;
2844 int offset = 0;
2845 if (!ctype) {
2846 expression_error(expr, "expected structure or union");
2847 return NULL;
2849 examine_symbol_type(ctype);
2850 class = classify_type(ctype, &ctype);
2851 if (class != TYPE_COMPOUND) {
2852 expression_error(expr, "expected structure or union");
2853 return NULL;
2856 field = find_identifier(expr->ident, ctype->symbol_list, &offset);
2857 if (!field) {
2858 expression_error(expr, "unknown member");
2859 return NULL;
2861 ctype = field;
2862 expr->type = EXPR_VALUE;
2863 expr->flags = Int_const_expr;
2864 expr->value = offset;
2865 expr->taint = 0;
2866 expr->ctype = size_t_ctype;
2867 } else {
2868 if (!ctype) {
2869 expression_error(expr, "expected structure or union");
2870 return NULL;
2872 examine_symbol_type(ctype);
2873 class = classify_type(ctype, &ctype);
2874 if (class != (TYPE_COMPOUND | TYPE_PTR)) {
2875 expression_error(expr, "expected array");
2876 return NULL;
2878 ctype = ctype->ctype.base_type;
2879 if (!expr->index) {
2880 expr->type = EXPR_VALUE;
2881 expr->flags = Int_const_expr;
2882 expr->value = 0;
2883 expr->taint = 0;
2884 expr->ctype = size_t_ctype;
2885 } else {
2886 struct expression *idx = expr->index, *m;
2887 struct symbol *i_type = evaluate_expression(idx);
2888 int i_class = classify_type(i_type, &i_type);
2889 if (!is_int(i_class)) {
2890 expression_error(expr, "non-integer index");
2891 return NULL;
2893 unrestrict(idx, i_class, &i_type);
2894 idx = cast_to(idx, size_t_ctype);
2895 m = alloc_const_expression(expr->pos,
2896 bits_to_bytes(ctype->bit_size));
2897 m->ctype = size_t_ctype;
2898 m->flags = Int_const_expr;
2899 expr->type = EXPR_BINOP;
2900 expr->left = idx;
2901 expr->right = m;
2902 expr->op = '*';
2903 expr->ctype = size_t_ctype;
2904 expr->flags = m->flags & idx->flags & Int_const_expr;
2907 if (e) {
2908 struct expression *copy = __alloc_expression(0);
2909 *copy = *expr;
2910 if (e->type == EXPR_OFFSETOF)
2911 e->in = ctype;
2912 if (!evaluate_expression(e))
2913 return NULL;
2914 expr->type = EXPR_BINOP;
2915 expr->flags = e->flags & copy->flags & Int_const_expr;
2916 expr->op = '+';
2917 expr->ctype = size_t_ctype;
2918 expr->left = copy;
2919 expr->right = e;
2921 return size_t_ctype;
2924 struct symbol *evaluate_expression(struct expression *expr)
2926 if (!expr)
2927 return NULL;
2928 if (expr->ctype)
2929 return expr->ctype;
2931 switch (expr->type) {
2932 case EXPR_VALUE:
2933 case EXPR_FVALUE:
2934 expression_error(expr, "value expression without a type");
2935 return NULL;
2936 case EXPR_STRING:
2937 return evaluate_string(expr);
2938 case EXPR_SYMBOL:
2939 return evaluate_symbol_expression(expr);
2940 case EXPR_BINOP:
2941 if (!evaluate_expression(expr->left))
2942 return NULL;
2943 if (!evaluate_expression(expr->right))
2944 return NULL;
2945 return evaluate_binop(expr);
2946 case EXPR_LOGICAL:
2947 return evaluate_logical(expr);
2948 case EXPR_COMMA:
2949 evaluate_expression(expr->left);
2950 if (!evaluate_expression(expr->right))
2951 return NULL;
2952 return evaluate_comma(expr);
2953 case EXPR_COMPARE:
2954 if (!evaluate_expression(expr->left))
2955 return NULL;
2956 if (!evaluate_expression(expr->right))
2957 return NULL;
2958 return evaluate_compare(expr);
2959 case EXPR_ASSIGNMENT:
2960 if (!evaluate_expression(expr->left))
2961 return NULL;
2962 if (!evaluate_expression(expr->right))
2963 return NULL;
2964 return evaluate_assignment(expr);
2965 case EXPR_PREOP:
2966 if (!evaluate_expression(expr->unop))
2967 return NULL;
2968 return evaluate_preop(expr);
2969 case EXPR_POSTOP:
2970 if (!evaluate_expression(expr->unop))
2971 return NULL;
2972 return evaluate_postop(expr);
2973 case EXPR_CAST:
2974 case EXPR_FORCE_CAST:
2975 case EXPR_IMPLIED_CAST:
2976 return evaluate_cast(expr);
2977 case EXPR_SIZEOF:
2978 return evaluate_sizeof(expr);
2979 case EXPR_PTRSIZEOF:
2980 return evaluate_ptrsizeof(expr);
2981 case EXPR_ALIGNOF:
2982 return evaluate_alignof(expr);
2983 case EXPR_DEREF:
2984 return evaluate_member_dereference(expr);
2985 case EXPR_CALL:
2986 return evaluate_call(expr);
2987 case EXPR_SELECT:
2988 case EXPR_CONDITIONAL:
2989 return evaluate_conditional_expression(expr);
2990 case EXPR_STATEMENT:
2991 expr->ctype = evaluate_statement(expr->statement);
2992 return expr->ctype;
2994 case EXPR_LABEL:
2995 expr->ctype = &ptr_ctype;
2996 return &ptr_ctype;
2998 case EXPR_TYPE:
2999 /* Evaluate the type of the symbol .. */
3000 evaluate_symbol(expr->symbol);
3001 /* .. but the type of the _expression_ is a "type" */
3002 expr->ctype = &type_ctype;
3003 return &type_ctype;
3005 case EXPR_OFFSETOF:
3006 return evaluate_offsetof(expr);
3008 /* These can not exist as stand-alone expressions */
3009 case EXPR_INITIALIZER:
3010 case EXPR_IDENTIFIER:
3011 case EXPR_INDEX:
3012 case EXPR_POS:
3013 expression_error(expr, "internal front-end error: initializer in expression");
3014 return NULL;
3015 case EXPR_SLICE:
3016 expression_error(expr, "internal front-end error: SLICE re-evaluated");
3017 return NULL;
3019 return NULL;
3022 static void check_duplicates(struct symbol *sym)
3024 int declared = 0;
3025 struct symbol *next = sym;
3027 while ((next = next->same_symbol) != NULL) {
3028 const char *typediff;
3029 evaluate_symbol(next);
3030 declared++;
3031 typediff = type_difference(&sym->ctype, &next->ctype, 0, 0);
3032 if (typediff) {
3033 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
3034 show_ident(sym->ident),
3035 stream_name(next->pos.stream), next->pos.line, typediff);
3036 return;
3039 if (!declared) {
3040 unsigned long mod = sym->ctype.modifiers;
3041 if (mod & (MOD_STATIC | MOD_REGISTER))
3042 return;
3043 if (!(mod & MOD_TOPLEVEL))
3044 return;
3045 if (!Wdecl)
3046 return;
3047 if (sym->ident == &main_ident)
3048 return;
3049 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
3053 static struct symbol *evaluate_symbol(struct symbol *sym)
3055 struct symbol *base_type;
3057 if (!sym)
3058 return sym;
3059 if (sym->evaluated)
3060 return sym;
3061 sym->evaluated = 1;
3063 sym = examine_symbol_type(sym);
3064 base_type = get_base_type(sym);
3065 if (!base_type)
3066 return NULL;
3068 /* Evaluate the initializers */
3069 if (sym->initializer)
3070 evaluate_initializer(sym, &sym->initializer);
3072 /* And finally, evaluate the body of the symbol too */
3073 if (base_type->type == SYM_FN) {
3074 struct symbol *curr = current_fn;
3076 if (sym->definition && sym->definition != sym)
3077 return evaluate_symbol(sym->definition);
3079 current_fn = base_type;
3081 examine_fn_arguments(base_type);
3082 if (!base_type->stmt && base_type->inline_stmt)
3083 uninline(sym);
3084 if (base_type->stmt)
3085 evaluate_statement(base_type->stmt);
3087 current_fn = curr;
3090 return base_type;
3093 void evaluate_symbol_list(struct symbol_list *list)
3095 struct symbol *sym;
3097 FOR_EACH_PTR(list, sym) {
3098 evaluate_symbol(sym);
3099 check_duplicates(sym);
3100 } END_FOR_EACH_PTR(sym);
3103 static struct symbol *evaluate_return_expression(struct statement *stmt)
3105 struct expression *expr = stmt->expression;
3106 struct symbol *fntype;
3108 evaluate_expression(expr);
3109 fntype = current_fn->ctype.base_type;
3110 if (!fntype || fntype == &void_ctype) {
3111 if (expr && expr->ctype != &void_ctype)
3112 expression_error(expr, "return expression in %s function", fntype?"void":"typeless");
3113 if (expr && Wreturn_void)
3114 warning(stmt->pos, "returning void-valued expression");
3115 return NULL;
3118 if (!expr) {
3119 sparse_error(stmt->pos, "return with no return value");
3120 return NULL;
3122 if (!expr->ctype)
3123 return NULL;
3124 compatible_assignment_types(expr, fntype, &stmt->expression, "return expression");
3125 return NULL;
3128 static void evaluate_if_statement(struct statement *stmt)
3130 if (!stmt->if_conditional)
3131 return;
3133 evaluate_conditional(stmt->if_conditional, 0);
3134 evaluate_statement(stmt->if_true);
3135 evaluate_statement(stmt->if_false);
3138 static void evaluate_iterator(struct statement *stmt)
3140 evaluate_symbol_list(stmt->iterator_syms);
3141 evaluate_conditional(stmt->iterator_pre_condition, 1);
3142 evaluate_conditional(stmt->iterator_post_condition,1);
3143 evaluate_statement(stmt->iterator_pre_statement);
3144 evaluate_statement(stmt->iterator_statement);
3145 evaluate_statement(stmt->iterator_post_statement);
3148 static void verify_output_constraint(struct expression *expr, const char *constraint)
3150 switch (*constraint) {
3151 case '=': /* Assignment */
3152 case '+': /* Update */
3153 break;
3154 default:
3155 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
3159 static void verify_input_constraint(struct expression *expr, const char *constraint)
3161 switch (*constraint) {
3162 case '=': /* Assignment */
3163 case '+': /* Update */
3164 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
3168 static void evaluate_asm_statement(struct statement *stmt)
3170 struct expression *expr;
3171 struct symbol *sym;
3172 int state;
3174 expr = stmt->asm_string;
3175 if (!expr || expr->type != EXPR_STRING) {
3176 sparse_error(stmt->pos, "need constant string for inline asm");
3177 return;
3180 state = 0;
3181 FOR_EACH_PTR(stmt->asm_outputs, expr) {
3182 switch (state) {
3183 case 0: /* Identifier */
3184 state = 1;
3185 continue;
3187 case 1: /* Constraint */
3188 state = 2;
3189 if (!expr || expr->type != EXPR_STRING) {
3190 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
3191 *THIS_ADDRESS(expr) = NULL;
3192 continue;
3194 verify_output_constraint(expr, expr->string->data);
3195 continue;
3197 case 2: /* Expression */
3198 state = 0;
3199 if (!evaluate_expression(expr))
3200 return;
3201 if (!lvalue_expression(expr))
3202 warning(expr->pos, "asm output is not an lvalue");
3203 evaluate_assign_to(expr, expr->ctype);
3204 continue;
3206 } END_FOR_EACH_PTR(expr);
3208 state = 0;
3209 FOR_EACH_PTR(stmt->asm_inputs, expr) {
3210 switch (state) {
3211 case 0: /* Identifier */
3212 state = 1;
3213 continue;
3215 case 1: /* Constraint */
3216 state = 2;
3217 if (!expr || expr->type != EXPR_STRING) {
3218 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
3219 *THIS_ADDRESS(expr) = NULL;
3220 continue;
3222 verify_input_constraint(expr, expr->string->data);
3223 continue;
3225 case 2: /* Expression */
3226 state = 0;
3227 if (!evaluate_expression(expr))
3228 return;
3229 continue;
3231 } END_FOR_EACH_PTR(expr);
3233 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
3234 if (!expr) {
3235 sparse_error(stmt->pos, "bad asm clobbers");
3236 return;
3238 if (expr->type == EXPR_STRING)
3239 continue;
3240 expression_error(expr, "asm clobber is not a string");
3241 } END_FOR_EACH_PTR(expr);
3243 FOR_EACH_PTR(stmt->asm_labels, sym) {
3244 if (!sym || sym->type != SYM_LABEL) {
3245 sparse_error(stmt->pos, "bad asm label");
3246 return;
3248 } END_FOR_EACH_PTR(sym);
3251 static void evaluate_case_statement(struct statement *stmt)
3253 evaluate_expression(stmt->case_expression);
3254 evaluate_expression(stmt->case_to);
3255 evaluate_statement(stmt->case_statement);
3258 static void check_case_type(struct expression *switch_expr,
3259 struct expression *case_expr,
3260 struct expression **enumcase)
3262 struct symbol *switch_type, *case_type;
3263 int sclass, cclass;
3265 if (!case_expr)
3266 return;
3268 switch_type = switch_expr->ctype;
3269 case_type = evaluate_expression(case_expr);
3271 if (!switch_type || !case_type)
3272 goto Bad;
3273 if (enumcase) {
3274 if (*enumcase)
3275 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
3276 else if (is_enum_type(case_type))
3277 *enumcase = case_expr;
3280 sclass = classify_type(switch_type, &switch_type);
3281 cclass = classify_type(case_type, &case_type);
3283 /* both should be arithmetic */
3284 if (!(sclass & cclass & TYPE_NUM))
3285 goto Bad;
3287 /* neither should be floating */
3288 if ((sclass | cclass) & TYPE_FLOAT)
3289 goto Bad;
3291 /* if neither is restricted, we are OK */
3292 if (!((sclass | cclass) & TYPE_RESTRICT))
3293 return;
3295 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
3296 cclass, sclass, case_type, switch_type)) {
3297 unrestrict(case_expr, cclass, &case_type);
3298 unrestrict(switch_expr, sclass, &switch_type);
3300 return;
3302 Bad:
3303 expression_error(case_expr, "incompatible types for 'case' statement");
3306 static void evaluate_switch_statement(struct statement *stmt)
3308 struct symbol *sym;
3309 struct expression *enumcase = NULL;
3310 struct expression **enumcase_holder = &enumcase;
3311 struct expression *sel = stmt->switch_expression;
3313 evaluate_expression(sel);
3314 evaluate_statement(stmt->switch_statement);
3315 if (!sel)
3316 return;
3317 if (sel->ctype && is_enum_type(sel->ctype))
3318 enumcase_holder = NULL; /* Only check cases against switch */
3320 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
3321 struct statement *case_stmt = sym->stmt;
3322 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
3323 check_case_type(sel, case_stmt->case_to, enumcase_holder);
3324 } END_FOR_EACH_PTR(sym);
3327 static void evaluate_goto_statement(struct statement *stmt)
3329 struct symbol *label = stmt->goto_label;
3331 if (label && !label->stmt && !lookup_keyword(label->ident, NS_KEYWORD))
3332 sparse_error(stmt->pos, "label '%s' was not declared", show_ident(label->ident));
3334 evaluate_expression(stmt->goto_expression);
3337 struct symbol *evaluate_statement(struct statement *stmt)
3339 if (!stmt)
3340 return NULL;
3342 switch (stmt->type) {
3343 case STMT_DECLARATION: {
3344 struct symbol *s;
3345 FOR_EACH_PTR(stmt->declaration, s) {
3346 evaluate_symbol(s);
3347 } END_FOR_EACH_PTR(s);
3348 return NULL;
3351 case STMT_RETURN:
3352 return evaluate_return_expression(stmt);
3354 case STMT_EXPRESSION:
3355 if (!evaluate_expression(stmt->expression))
3356 return NULL;
3357 if (stmt->expression->ctype == &null_ctype)
3358 stmt->expression = cast_to(stmt->expression, &ptr_ctype);
3359 return degenerate(stmt->expression);
3361 case STMT_COMPOUND: {
3362 struct statement *s;
3363 struct symbol *type = NULL;
3365 /* Evaluate the return symbol in the compound statement */
3366 evaluate_symbol(stmt->ret);
3369 * Then, evaluate each statement, making the type of the
3370 * compound statement be the type of the last statement
3372 type = evaluate_statement(stmt->args);
3373 FOR_EACH_PTR(stmt->stmts, s) {
3374 type = evaluate_statement(s);
3375 } END_FOR_EACH_PTR(s);
3376 if (!type)
3377 type = &void_ctype;
3378 return type;
3380 case STMT_IF:
3381 evaluate_if_statement(stmt);
3382 return NULL;
3383 case STMT_ITERATOR:
3384 evaluate_iterator(stmt);
3385 return NULL;
3386 case STMT_SWITCH:
3387 evaluate_switch_statement(stmt);
3388 return NULL;
3389 case STMT_CASE:
3390 evaluate_case_statement(stmt);
3391 return NULL;
3392 case STMT_LABEL:
3393 return evaluate_statement(stmt->label_statement);
3394 case STMT_GOTO:
3395 evaluate_goto_statement(stmt);
3396 return NULL;
3397 case STMT_NONE:
3398 break;
3399 case STMT_ASM:
3400 evaluate_asm_statement(stmt);
3401 return NULL;
3402 case STMT_CONTEXT:
3403 evaluate_expression(stmt->expression);
3404 return NULL;
3405 case STMT_RANGE:
3406 evaluate_expression(stmt->range_expression);
3407 evaluate_expression(stmt->range_low);
3408 evaluate_expression(stmt->range_high);
3409 return NULL;
3411 return NULL;