user_data: export is_user_macro()
[smatch.git] / evaluate.c
blobf3f657269b97ad70d229f24fbfa1d9489a619c3a
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.attribute->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->attribute->as, as2 = c2->attribute->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;
621 for (;;) {
622 unsigned long diff;
623 int type;
624 struct symbol *base1 = t1->ctype.base_type;
625 struct symbol *base2 = t2->ctype.base_type;
628 * FIXME! Collect alignment and context too here!
630 if (move1) {
631 if (t1 && t1->type != SYM_PTR) {
632 mod1 |= t1->ctype.modifiers;
633 as1 |= t1->ctype.attribute->as;
635 move1 = 0;
638 if (move2) {
639 if (t2 && t2->type != SYM_PTR) {
640 mod2 |= t2->ctype.modifiers;
641 as2 |= t2->ctype.attribute->as;
643 move2 = 0;
646 if (t1 == t2)
647 break;
648 if (!t1 || !t2)
649 return "different types";
651 if (t1->type == SYM_NODE || t1->type == SYM_ENUM) {
652 t1 = base1;
653 move1 = 1;
654 if (!t1)
655 return "bad types";
656 continue;
659 if (t2->type == SYM_NODE || t2->type == SYM_ENUM) {
660 t2 = base2;
661 move2 = 1;
662 if (!t2)
663 return "bad types";
664 continue;
667 move1 = move2 = 1;
668 type = t1->type;
669 if (type != t2->type)
670 return "different base types";
672 switch (type) {
673 default:
674 sparse_error(t1->pos,
675 "internal error: bad type in derived(%d)",
676 type);
677 return "bad types";
678 case SYM_RESTRICT:
679 return "different base types";
680 case SYM_UNION:
681 case SYM_STRUCT:
682 /* allow definition of incomplete structs and unions */
683 if (t1->ident == t2->ident)
684 return NULL;
685 return "different base types";
686 case SYM_ARRAY:
687 /* XXX: we ought to compare sizes */
688 break;
689 case SYM_PTR:
690 if (as1 != as2)
691 return "different address spaces";
692 /* MOD_SPECIFIER is due to idiocy in parse.c */
693 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SPECIFIER)
694 return "different modifiers";
695 /* we could be lazier here */
696 base1 = examine_pointer_target(t1);
697 base2 = examine_pointer_target(t2);
698 mod1 = t1->ctype.modifiers;
699 as1 = t1->ctype.attribute->as;
700 mod2 = t2->ctype.modifiers;
701 as2 = t2->ctype.attribute->as;
702 break;
703 case SYM_FN: {
704 struct symbol *arg1, *arg2;
705 int i;
707 if (as1 != as2)
708 return "different address spaces";
709 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
710 return "different modifiers";
711 mod1 = t1->ctype.modifiers;
712 as1 = t1->ctype.attribute->as;
713 mod2 = t2->ctype.modifiers;
714 as2 = t2->ctype.attribute->as;
716 if (base1->variadic != base2->variadic)
717 return "incompatible variadic arguments";
718 examine_fn_arguments(t1);
719 examine_fn_arguments(t2);
720 PREPARE_PTR_LIST(t1->arguments, arg1);
721 PREPARE_PTR_LIST(t2->arguments, arg2);
722 i = 1;
723 for (;;) {
724 const char *diffstr;
725 if (!arg1 && !arg2)
726 break;
727 if (!arg1 || !arg2)
728 return "different argument counts";
729 diffstr = type_difference(&arg1->ctype,
730 &arg2->ctype,
731 MOD_IGN, MOD_IGN);
732 if (diffstr) {
733 static char argdiff[80];
734 sprintf(argdiff, "incompatible argument %d (%s)", i, diffstr);
735 return argdiff;
737 NEXT_PTR_LIST(arg1);
738 NEXT_PTR_LIST(arg2);
739 i++;
741 FINISH_PTR_LIST(arg2);
742 FINISH_PTR_LIST(arg1);
743 break;
745 case SYM_BASETYPE:
746 if (as1 != as2)
747 return "different address spaces";
748 if (base1 != base2)
749 return "different base types";
750 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
751 if (!diff)
752 return NULL;
753 if (diff & MOD_SIZE)
754 return "different type sizes";
755 else if (diff & ~MOD_SIGNEDNESS)
756 return "different modifiers";
757 else
758 return "different signedness";
760 t1 = base1;
761 t2 = base2;
763 if (as1 != as2)
764 return "different address spaces";
765 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
766 return "different modifiers";
767 return NULL;
770 static void bad_null(struct expression *expr)
772 if (Wnon_pointer_null)
773 warning(expr->pos, "Using plain integer as NULL pointer");
776 static unsigned long target_qualifiers(struct symbol *type)
778 unsigned long mod = type->ctype.modifiers & MOD_IGN;
779 if (type->ctype.base_type && type->ctype.base_type->type == SYM_ARRAY)
780 mod = 0;
781 return mod;
784 static struct symbol *evaluate_ptr_sub(struct expression *expr)
786 const char *typediff;
787 struct symbol *ltype, *rtype;
788 struct expression *l = expr->left;
789 struct expression *r = expr->right;
790 struct symbol *lbase;
792 classify_type(degenerate(l), &ltype);
793 classify_type(degenerate(r), &rtype);
795 lbase = examine_pointer_target(ltype);
796 examine_pointer_target(rtype);
797 typediff = type_difference(&ltype->ctype, &rtype->ctype,
798 target_qualifiers(rtype),
799 target_qualifiers(ltype));
800 if (typediff)
801 expression_error(expr, "subtraction of different types can't work (%s)", typediff);
803 if (is_function(lbase)) {
804 expression_error(expr, "subtraction of functions? Share your drugs");
805 return NULL;
808 expr->ctype = ssize_t_ctype;
809 if (lbase->bit_size > bits_in_char) {
810 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
811 struct expression *div = expr;
812 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
813 unsigned long value = bits_to_bytes(lbase->bit_size);
815 val->ctype = size_t_ctype;
816 val->value = value;
818 if (value & (value-1)) {
819 if (Wptr_subtraction_blows)
820 warning(expr->pos, "potentially expensive pointer subtraction");
823 sub->op = '-';
824 sub->ctype = ssize_t_ctype;
825 sub->left = l;
826 sub->right = r;
828 div->op = '/';
829 div->left = sub;
830 div->right = val;
833 return ssize_t_ctype;
836 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
838 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
840 struct symbol *ctype;
842 if (!expr)
843 return NULL;
845 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
846 warning(expr->pos, "assignment expression in conditional");
848 ctype = evaluate_expression(expr);
849 if (ctype) {
850 if (is_safe_type(ctype))
851 warning(expr->pos, "testing a 'safe expression'");
854 return ctype;
857 static struct symbol *evaluate_logical(struct expression *expr)
859 if (!evaluate_conditional(expr->left, 0))
860 return NULL;
861 if (!evaluate_conditional(expr->right, 0))
862 return NULL;
864 expr->ctype = &bool_ctype;
865 if (expr->flags) {
866 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
867 expr->flags = 0;
869 return &bool_ctype;
872 static struct symbol *evaluate_binop(struct expression *expr)
874 struct symbol *ltype, *rtype, *ctype;
875 int lclass = classify_type(expr->left->ctype, &ltype);
876 int rclass = classify_type(expr->right->ctype, &rtype);
877 int op = expr->op;
879 if (expr->flags) {
880 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
881 expr->flags = 0;
884 /* number op number */
885 if (lclass & rclass & TYPE_NUM) {
886 if ((lclass | rclass) & TYPE_FLOAT) {
887 switch (op) {
888 case '+': case '-': case '*': case '/':
889 break;
890 default:
891 return bad_expr_type(expr);
895 if (op == SPECIAL_LEFTSHIFT || op == SPECIAL_RIGHTSHIFT) {
896 // shifts do integer promotions, but that's it.
897 unrestrict(expr->left, lclass, &ltype);
898 unrestrict(expr->right, rclass, &rtype);
899 ctype = ltype = integer_promotion(ltype);
900 rtype = integer_promotion(rtype);
901 } else {
902 // The rest do usual conversions
903 const unsigned left_not = expr->left->type == EXPR_PREOP
904 && expr->left->op == '!';
905 const unsigned right_not = expr->right->type == EXPR_PREOP
906 && expr->right->op == '!';
907 if ((op == '&' || op == '|') && (left_not || right_not))
908 warning(expr->pos, "dubious: %sx %c %sy",
909 left_not ? "!" : "",
911 right_not ? "!" : "");
913 ltype = usual_conversions(op, expr->left, expr->right,
914 lclass, rclass, ltype, rtype);
915 ctype = rtype = ltype;
918 expr->left = cast_to(expr->left, ltype);
919 expr->right = cast_to(expr->right, rtype);
920 expr->ctype = ctype;
921 return ctype;
924 /* pointer (+|-) integer */
925 if (lclass & TYPE_PTR && is_int(rclass) && (op == '+' || op == '-')) {
926 unrestrict(expr->right, rclass, &rtype);
927 return evaluate_ptr_add(expr, rtype);
930 /* integer + pointer */
931 if (rclass & TYPE_PTR && is_int(lclass) && op == '+') {
932 struct expression *index = expr->left;
933 unrestrict(index, lclass, &ltype);
934 expr->left = expr->right;
935 expr->right = index;
936 return evaluate_ptr_add(expr, ltype);
939 /* pointer - pointer */
940 if (lclass & rclass & TYPE_PTR && expr->op == '-')
941 return evaluate_ptr_sub(expr);
943 return bad_expr_type(expr);
946 static struct symbol *evaluate_comma(struct expression *expr)
948 expr->ctype = degenerate(expr->right);
949 if (expr->ctype == &null_ctype)
950 expr->ctype = &ptr_ctype;
951 expr->flags &= expr->left->flags & expr->right->flags;
952 return expr->ctype;
955 static int modify_for_unsigned(int op)
957 if (op == '<')
958 op = SPECIAL_UNSIGNED_LT;
959 else if (op == '>')
960 op = SPECIAL_UNSIGNED_GT;
961 else if (op == SPECIAL_LTE)
962 op = SPECIAL_UNSIGNED_LTE;
963 else if (op == SPECIAL_GTE)
964 op = SPECIAL_UNSIGNED_GTE;
965 return op;
968 static inline int is_null_pointer_constant(struct expression *e)
970 if (e->ctype == &null_ctype)
971 return 1;
972 if (!(e->flags & Int_const_expr))
973 return 0;
974 return is_zero_constant(e) ? 2 : 0;
977 static struct symbol *evaluate_compare(struct expression *expr)
979 struct expression *left = expr->left, *right = expr->right;
980 struct symbol *ltype, *rtype, *lbase, *rbase;
981 int lclass = classify_type(degenerate(left), &ltype);
982 int rclass = classify_type(degenerate(right), &rtype);
983 struct symbol *ctype;
984 const char *typediff;
986 if (expr->flags) {
987 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
988 expr->flags = 0;
991 /* Type types? */
992 if (is_type_type(ltype) && is_type_type(rtype))
993 goto OK;
995 if (is_safe_type(left->ctype) || is_safe_type(right->ctype))
996 warning(expr->pos, "testing a 'safe expression'");
998 /* number on number */
999 if (lclass & rclass & TYPE_NUM) {
1000 ctype = usual_conversions(expr->op, expr->left, expr->right,
1001 lclass, rclass, ltype, rtype);
1002 expr->left = cast_to(expr->left, ctype);
1003 expr->right = cast_to(expr->right, ctype);
1004 if (ctype->ctype.modifiers & MOD_UNSIGNED)
1005 expr->op = modify_for_unsigned(expr->op);
1006 goto OK;
1009 /* at least one must be a pointer */
1010 if (!((lclass | rclass) & TYPE_PTR))
1011 return bad_expr_type(expr);
1013 /* equality comparisons can be with null pointer constants */
1014 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1015 int is_null1 = is_null_pointer_constant(left);
1016 int is_null2 = is_null_pointer_constant(right);
1017 if (is_null1 == 2)
1018 bad_null(left);
1019 if (is_null2 == 2)
1020 bad_null(right);
1021 if (is_null1 && is_null2) {
1022 int positive = expr->op == SPECIAL_EQUAL;
1023 expr->type = EXPR_VALUE;
1024 expr->value = positive;
1025 goto OK;
1027 if (is_null1 && (rclass & TYPE_PTR)) {
1028 left = cast_to(left, rtype);
1029 goto OK;
1031 if (is_null2 && (lclass & TYPE_PTR)) {
1032 right = cast_to(right, ltype);
1033 goto OK;
1036 /* both should be pointers */
1037 if (!(lclass & rclass & TYPE_PTR))
1038 return bad_expr_type(expr);
1039 expr->op = modify_for_unsigned(expr->op);
1041 lbase = examine_pointer_target(ltype);
1042 rbase = examine_pointer_target(rtype);
1044 /* they also have special treatment for pointers to void */
1045 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1046 if (ltype->ctype.attribute->as == rtype->ctype.attribute->as) {
1047 if (lbase == &void_ctype) {
1048 right = cast_to(right, ltype);
1049 goto OK;
1051 if (rbase == &void_ctype) {
1052 left = cast_to(left, rtype);
1053 goto OK;
1058 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1059 target_qualifiers(rtype),
1060 target_qualifiers(ltype));
1061 if (!typediff)
1062 goto OK;
1064 expression_error(expr, "incompatible types in comparison expression (%s)", typediff);
1065 return NULL;
1068 expr->ctype = &bool_ctype;
1069 return &bool_ctype;
1073 * NOTE! The degenerate case of "x ? : y", where we don't
1074 * have a true case, this will possibly promote "x" to the
1075 * same type as "y", and thus _change_ the conditional
1076 * test in the expression. But since promotion is "safe"
1077 * for testing, that's OK.
1079 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1081 struct expression **true;
1082 struct symbol *ctype, *ltype, *rtype, *lbase, *rbase;
1083 int lclass, rclass;
1084 const char * typediff;
1085 int qual;
1087 if (!evaluate_conditional(expr->conditional, 0))
1088 return NULL;
1089 if (!evaluate_expression(expr->cond_false))
1090 return NULL;
1092 ctype = degenerate(expr->conditional);
1093 rtype = degenerate(expr->cond_false);
1095 true = &expr->conditional;
1096 ltype = ctype;
1097 if (expr->cond_true) {
1098 if (!evaluate_expression(expr->cond_true))
1099 return NULL;
1100 ltype = degenerate(expr->cond_true);
1101 true = &expr->cond_true;
1104 if (expr->flags) {
1105 int flags = expr->conditional->flags & Int_const_expr;
1106 flags &= (*true)->flags & expr->cond_false->flags;
1107 if (!flags)
1108 expr->flags = 0;
1111 lclass = classify_type(ltype, &ltype);
1112 rclass = classify_type(rtype, &rtype);
1113 if (lclass & rclass & TYPE_NUM) {
1114 ctype = usual_conversions('?', *true, expr->cond_false,
1115 lclass, rclass, ltype, rtype);
1116 *true = cast_to(*true, ctype);
1117 expr->cond_false = cast_to(expr->cond_false, ctype);
1118 goto out;
1121 if ((lclass | rclass) & TYPE_PTR) {
1122 int is_null1 = is_null_pointer_constant(*true);
1123 int is_null2 = is_null_pointer_constant(expr->cond_false);
1125 if (is_null1 && is_null2) {
1126 *true = cast_to(*true, &ptr_ctype);
1127 expr->cond_false = cast_to(expr->cond_false, &ptr_ctype);
1128 ctype = &ptr_ctype;
1129 goto out;
1131 if (is_null1 && (rclass & TYPE_PTR)) {
1132 if (is_null1 == 2)
1133 bad_null(*true);
1134 *true = cast_to(*true, rtype);
1135 ctype = rtype;
1136 goto out;
1138 if (is_null2 && (lclass & TYPE_PTR)) {
1139 if (is_null2 == 2)
1140 bad_null(expr->cond_false);
1141 expr->cond_false = cast_to(expr->cond_false, ltype);
1142 ctype = ltype;
1143 goto out;
1145 if (!(lclass & rclass & TYPE_PTR)) {
1146 typediff = "different types";
1147 goto Err;
1149 /* OK, it's pointer on pointer */
1150 if (ltype->ctype.attribute->as != rtype->ctype.attribute->as) {
1151 typediff = "different address spaces";
1152 goto Err;
1155 /* need to be lazier here */
1156 lbase = examine_pointer_target(ltype);
1157 rbase = examine_pointer_target(rtype);
1158 qual = target_qualifiers(ltype) | target_qualifiers(rtype);
1160 if (lbase == &void_ctype) {
1161 /* XXX: pointers to function should warn here */
1162 ctype = ltype;
1163 goto Qual;
1166 if (rbase == &void_ctype) {
1167 /* XXX: pointers to function should warn here */
1168 ctype = rtype;
1169 goto Qual;
1171 /* XXX: that should be pointer to composite */
1172 ctype = ltype;
1173 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1174 qual, qual);
1175 if (!typediff)
1176 goto Qual;
1177 goto Err;
1180 /* void on void, struct on same struct, union on same union */
1181 if (ltype == rtype) {
1182 ctype = ltype;
1183 goto out;
1185 typediff = "different base types";
1187 Err:
1188 expression_error(expr, "incompatible types in conditional expression (%s)", typediff);
1189 return NULL;
1191 out:
1192 expr->ctype = ctype;
1193 return ctype;
1195 Qual:
1196 if (qual & ~ctype->ctype.modifiers) {
1197 struct symbol *sym = alloc_symbol(ctype->pos, SYM_PTR);
1198 *sym = *ctype;
1199 sym->ctype.modifiers |= qual;
1200 ctype = sym;
1202 *true = cast_to(*true, ctype);
1203 expr->cond_false = cast_to(expr->cond_false, ctype);
1204 goto out;
1207 /* FP assignments can not do modulo or bit operations */
1208 static int compatible_float_op(int op)
1210 return op == SPECIAL_ADD_ASSIGN ||
1211 op == SPECIAL_SUB_ASSIGN ||
1212 op == SPECIAL_MUL_ASSIGN ||
1213 op == SPECIAL_DIV_ASSIGN;
1216 static int evaluate_assign_op(struct expression *expr)
1218 struct symbol *target = expr->left->ctype;
1219 struct symbol *source = expr->right->ctype;
1220 struct symbol *t, *s;
1221 int tclass = classify_type(target, &t);
1222 int sclass = classify_type(source, &s);
1223 int op = expr->op;
1225 if (tclass & sclass & TYPE_NUM) {
1226 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1227 expression_error(expr, "invalid assignment");
1228 return 0;
1230 if (tclass & TYPE_RESTRICT) {
1231 if (!restricted_binop(op, t)) {
1232 warning(expr->pos, "bad assignment (%s) to %s",
1233 show_special(op), show_typename(t));
1234 expr->right = cast_to(expr->right, target);
1235 return 0;
1237 /* allowed assignments unfoul */
1238 if (sclass & TYPE_FOULED && unfoul(s) == t)
1239 goto Cast;
1240 if (!restricted_value(expr->right, t))
1241 return 1;
1242 } else if (!(sclass & TYPE_RESTRICT))
1243 goto Cast;
1244 /* source and target would better be identical restricted */
1245 if (t == s)
1246 return 1;
1247 warning(expr->pos, "invalid assignment: %s", show_special(op));
1248 info(expr->pos, " left side has type %s", show_typename(t));
1249 info(expr->pos, " right side has type %s", show_typename(s));
1250 expr->right = cast_to(expr->right, target);
1251 return 0;
1253 if (tclass == TYPE_PTR && is_int(sclass)) {
1254 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1255 unrestrict(expr->right, sclass, &s);
1256 evaluate_ptr_add(expr, s);
1257 return 1;
1259 expression_error(expr, "invalid pointer assignment");
1260 return 0;
1263 expression_error(expr, "invalid assignment");
1264 return 0;
1266 Cast:
1267 expr->right = cast_to(expr->right, target);
1268 return 1;
1271 static int whitelist_pointers(struct symbol *t1, struct symbol *t2)
1273 if (t1 == t2)
1274 return 0; /* yes, 0 - we don't want a cast_to here */
1275 if (t1 == &void_ctype)
1276 return 1;
1277 if (t2 == &void_ctype)
1278 return 1;
1279 if (classify_type(t1, &t1) != TYPE_NUM)
1280 return 0;
1281 if (classify_type(t2, &t2) != TYPE_NUM)
1282 return 0;
1283 if (t1 == t2)
1284 return 1;
1285 if (t1->ctype.modifiers & t2->ctype.modifiers & MOD_CHAR)
1286 return 1;
1287 if ((t1->ctype.modifiers ^ t2->ctype.modifiers) & MOD_SIZE)
1288 return 0;
1289 return !Wtypesign;
1292 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1293 struct expression **rp, const char *where)
1295 const char *typediff;
1296 struct symbol *source = degenerate(*rp);
1297 struct symbol *t, *s;
1298 int tclass = classify_type(target, &t);
1299 int sclass = classify_type(source, &s);
1301 if (tclass & sclass & TYPE_NUM) {
1302 if (tclass & TYPE_RESTRICT) {
1303 /* allowed assignments unfoul */
1304 if (sclass & TYPE_FOULED && unfoul(s) == t)
1305 goto Cast;
1306 if (!restricted_value(*rp, target))
1307 return 1;
1308 if (s == t)
1309 return 1;
1310 } else if (!(sclass & TYPE_RESTRICT))
1311 goto Cast;
1312 typediff = "different base types";
1313 goto Err;
1316 if (tclass == TYPE_PTR) {
1317 unsigned long mod1, mod2;
1318 struct symbol *b1, *b2;
1319 // NULL pointer is always OK
1320 int is_null = is_null_pointer_constant(*rp);
1321 if (is_null) {
1322 if (is_null == 2)
1323 bad_null(*rp);
1324 goto Cast;
1326 if (!(sclass & TYPE_PTR)) {
1327 typediff = "different base types";
1328 goto Err;
1330 b1 = examine_pointer_target(t);
1331 b2 = examine_pointer_target(s);
1332 mod1 = target_qualifiers(t);
1333 mod2 = target_qualifiers(s);
1334 if (whitelist_pointers(b1, b2)) {
1336 * assignments to/from void * are OK, provided that
1337 * we do not remove qualifiers from pointed to [C]
1338 * or mix address spaces [sparse].
1340 if (t->ctype.attribute->as != s->ctype.attribute->as) {
1341 typediff = "different address spaces";
1342 goto Err;
1344 if (mod2 & ~mod1) {
1345 typediff = "different modifiers";
1346 goto Err;
1348 goto Cast;
1350 /* It's OK if the target is more volatile or const than the source */
1351 typediff = type_difference(&t->ctype, &s->ctype, 0, mod1);
1352 if (typediff)
1353 goto Err;
1354 return 1;
1357 if ((tclass & TYPE_COMPOUND) && s == t)
1358 return 1;
1360 if (tclass & TYPE_NUM) {
1361 /* XXX: need to turn into comparison with NULL */
1362 if (t == &bool_ctype && (sclass & TYPE_PTR))
1363 goto Cast;
1364 typediff = "different base types";
1365 goto Err;
1367 typediff = "invalid types";
1369 Err:
1370 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1371 info(expr->pos, " expected %s", show_typename(target));
1372 info(expr->pos, " got %s", show_typename(source));
1373 *rp = cast_to(*rp, target);
1374 return 0;
1375 Cast:
1376 *rp = cast_to(*rp, target);
1377 return 1;
1380 static void mark_assigned(struct expression *expr)
1382 struct symbol *sym;
1384 if (!expr)
1385 return;
1386 switch (expr->type) {
1387 case EXPR_SYMBOL:
1388 sym = expr->symbol;
1389 if (!sym)
1390 return;
1391 if (sym->type != SYM_NODE)
1392 return;
1393 sym->ctype.modifiers |= MOD_ASSIGNED;
1394 return;
1396 case EXPR_BINOP:
1397 mark_assigned(expr->left);
1398 mark_assigned(expr->right);
1399 return;
1400 case EXPR_CAST:
1401 case EXPR_FORCE_CAST:
1402 mark_assigned(expr->cast_expression);
1403 return;
1404 case EXPR_SLICE:
1405 mark_assigned(expr->base);
1406 return;
1407 default:
1408 /* Hmm? */
1409 return;
1413 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1415 if (type->ctype.modifiers & MOD_CONST)
1416 expression_error(left, "assignment to const expression");
1418 /* We know left is an lvalue, so it's a "preop-*" */
1419 mark_assigned(left->unop);
1422 static struct symbol *evaluate_assignment(struct expression *expr)
1424 struct expression *left = expr->left;
1425 struct expression *where = expr;
1426 struct symbol *ltype;
1428 if (!lvalue_expression(left)) {
1429 expression_error(expr, "not an lvalue");
1430 return NULL;
1433 ltype = left->ctype;
1435 if (expr->op != '=') {
1436 if (!evaluate_assign_op(expr))
1437 return NULL;
1438 } else {
1439 if (!compatible_assignment_types(where, ltype, &expr->right, "assignment"))
1440 return NULL;
1443 evaluate_assign_to(left, ltype);
1445 expr->ctype = ltype;
1446 return ltype;
1449 static void examine_fn_arguments(struct symbol *fn)
1451 struct symbol *s;
1453 FOR_EACH_PTR(fn->arguments, s) {
1454 struct symbol *arg = evaluate_symbol(s);
1455 /* Array/function arguments silently degenerate into pointers */
1456 if (arg) {
1457 struct symbol *ptr;
1458 switch(arg->type) {
1459 case SYM_ARRAY:
1460 case SYM_FN:
1461 ptr = alloc_symbol(s->pos, SYM_PTR);
1462 if (arg->type == SYM_ARRAY)
1463 ptr->ctype = arg->ctype;
1464 else
1465 ptr->ctype.base_type = arg;
1466 merge_attr(&ptr->ctype, &s->ctype);
1467 ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1469 s->ctype.base_type = ptr;
1470 s->ctype.attribute = &null_attr;
1471 s->ctype.modifiers &= ~MOD_PTRINHERIT;
1472 s->bit_size = 0;
1473 s->examined = 0;
1474 examine_symbol_type(s);
1475 break;
1476 default:
1477 /* nothing */
1478 break;
1481 } END_FOR_EACH_PTR(s);
1484 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1486 /* Take the modifiers of the pointer, and apply them to the member */
1487 mod |= sym->ctype.modifiers;
1488 if (sym->ctype.attribute->as != as || sym->ctype.modifiers != mod) {
1489 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1490 *newsym = *sym;
1491 attr_set_as(&newsym->ctype, as);
1492 newsym->ctype.modifiers = mod;
1493 sym = newsym;
1495 return sym;
1498 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1500 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1501 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1503 node->ctype.base_type = ptr;
1504 ptr->bit_size = bits_in_pointer;
1505 ptr->ctype.alignment = pointer_alignment;
1507 node->bit_size = bits_in_pointer;
1508 node->ctype.alignment = pointer_alignment;
1510 access_symbol(sym);
1511 if (sym->ctype.modifiers & MOD_REGISTER) {
1512 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1513 sym->ctype.modifiers &= ~MOD_REGISTER;
1515 if (sym->type == SYM_NODE) {
1516 merge_attr(&ptr->ctype, &sym->ctype);
1517 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1518 sym = sym->ctype.base_type;
1520 if (degenerate && sym->type == SYM_ARRAY) {
1521 merge_attr(&ptr->ctype, &sym->ctype);
1522 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1523 sym = sym->ctype.base_type;
1525 ptr->ctype.base_type = sym;
1527 return node;
1530 /* Arrays degenerate into pointers on pointer arithmetic */
1531 static struct symbol *degenerate(struct expression *expr)
1533 struct symbol *ctype, *base;
1535 if (!expr)
1536 return NULL;
1537 ctype = expr->ctype;
1538 if (!ctype)
1539 return NULL;
1540 base = examine_symbol_type(ctype);
1541 if (ctype->type == SYM_NODE)
1542 base = ctype->ctype.base_type;
1544 * Arrays degenerate into pointers to the entries, while
1545 * functions degenerate into pointers to themselves.
1546 * If array was part of non-lvalue compound, we create a copy
1547 * of that compound first and then act as if we were dealing with
1548 * the corresponding field in there.
1550 switch (base->type) {
1551 case SYM_ARRAY:
1552 if (expr->type == EXPR_SLICE) {
1553 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1554 struct expression *e0, *e1, *e2, *e3, *e4;
1556 a->ctype.base_type = expr->base->ctype;
1557 a->bit_size = expr->base->ctype->bit_size;
1558 a->array_size = expr->base->ctype->array_size;
1560 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1561 e0->symbol = a;
1562 e0->ctype = &lazy_ptr_ctype;
1564 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1565 e1->unop = e0;
1566 e1->op = '*';
1567 e1->ctype = expr->base->ctype; /* XXX */
1569 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1570 e2->left = e1;
1571 e2->right = expr->base;
1572 e2->op = '=';
1573 e2->ctype = expr->base->ctype;
1575 if (expr->r_bitpos) {
1576 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1577 e3->op = '+';
1578 e3->left = e0;
1579 e3->right = alloc_const_expression(expr->pos,
1580 bits_to_bytes(expr->r_bitpos));
1581 e3->ctype = &lazy_ptr_ctype;
1582 } else {
1583 e3 = e0;
1586 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1587 e4->left = e2;
1588 e4->right = e3;
1589 e4->ctype = &lazy_ptr_ctype;
1591 expr->unop = e4;
1592 expr->type = EXPR_PREOP;
1593 expr->op = '*';
1595 case SYM_FN:
1596 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1597 expression_error(expr, "strange non-value function or array");
1598 return &bad_ctype;
1600 *expr = *expr->unop;
1601 ctype = create_pointer(expr, ctype, 1);
1602 expr->ctype = ctype;
1603 default:
1604 /* nothing */;
1606 return ctype;
1609 static struct symbol *evaluate_addressof(struct expression *expr)
1611 struct expression *op = expr->unop;
1612 struct symbol *ctype;
1614 if (op->op != '*' || op->type != EXPR_PREOP) {
1615 expression_error(expr, "not addressable");
1616 return NULL;
1618 ctype = op->ctype;
1619 *expr = *op->unop;
1620 expr->flags = 0;
1622 if (expr->type == EXPR_SYMBOL) {
1623 struct symbol *sym = expr->symbol;
1624 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1628 * symbol expression evaluation is lazy about the type
1629 * of the sub-expression, so we may have to generate
1630 * the type here if so..
1632 if (expr->ctype == &lazy_ptr_ctype) {
1633 ctype = create_pointer(expr, ctype, 0);
1634 expr->ctype = ctype;
1636 return expr->ctype;
1640 static struct symbol *evaluate_dereference(struct expression *expr)
1642 struct expression *op = expr->unop;
1643 struct symbol *ctype = op->ctype, *node, *target;
1645 /* Simplify: *&(expr) => (expr) */
1646 if (op->type == EXPR_PREOP && op->op == '&') {
1647 *expr = *op->unop;
1648 expr->flags = 0;
1649 return expr->ctype;
1652 /* Dereferencing a node drops all the node information. */
1653 if (ctype->type == SYM_NODE)
1654 ctype = ctype->ctype.base_type;
1656 node = alloc_symbol(expr->pos, SYM_NODE);
1657 target = ctype->ctype.base_type;
1659 switch (ctype->type) {
1660 default:
1661 expression_error(expr, "cannot dereference this type");
1662 return NULL;
1663 case SYM_PTR:
1664 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1665 merge_type(node, ctype);
1666 break;
1668 case SYM_ARRAY:
1669 if (!lvalue_expression(op)) {
1670 expression_error(op, "non-lvalue array??");
1671 return NULL;
1674 /* Do the implied "addressof" on the array */
1675 *op = *op->unop;
1678 * When an array is dereferenced, we need to pick
1679 * up the attributes of the original node too..
1681 merge_type(node, op->ctype);
1682 merge_type(node, ctype);
1683 break;
1686 node->bit_size = target->bit_size;
1687 node->array_size = target->array_size;
1689 expr->ctype = node;
1690 return node;
1694 * Unary post-ops: x++ and x--
1696 static struct symbol *evaluate_postop(struct expression *expr)
1698 struct expression *op = expr->unop;
1699 struct symbol *ctype = op->ctype;
1700 int class = classify_type(ctype, &ctype);
1701 int multiply = 0;
1703 if (!class || class & TYPE_COMPOUND) {
1704 expression_error(expr, "need scalar for ++/--");
1705 return NULL;
1707 if (!lvalue_expression(expr->unop)) {
1708 expression_error(expr, "need lvalue expression for ++/--");
1709 return NULL;
1712 if ((class & TYPE_RESTRICT) && restricted_unop(expr->op, &ctype))
1713 unrestrict(expr, class, &ctype);
1715 if (class & TYPE_NUM) {
1716 multiply = 1;
1717 } else if (class == TYPE_PTR) {
1718 struct symbol *target = examine_pointer_target(ctype);
1719 if (!is_function(target))
1720 multiply = bits_to_bytes(target->bit_size);
1723 if (multiply) {
1724 evaluate_assign_to(op, op->ctype);
1725 expr->op_value = multiply;
1726 expr->ctype = ctype;
1727 return ctype;
1730 expression_error(expr, "bad argument type for ++/--");
1731 return NULL;
1734 static struct symbol *evaluate_sign(struct expression *expr)
1736 struct symbol *ctype = expr->unop->ctype;
1737 int class = classify_type(ctype, &ctype);
1738 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1739 expr->flags = 0;
1740 /* should be an arithmetic type */
1741 if (!(class & TYPE_NUM))
1742 return bad_expr_type(expr);
1743 if (class & TYPE_RESTRICT)
1744 goto Restr;
1745 Normal:
1746 if (!(class & TYPE_FLOAT)) {
1747 ctype = integer_promotion(ctype);
1748 expr->unop = cast_to(expr->unop, ctype);
1749 } else if (expr->op != '~') {
1750 /* no conversions needed */
1751 } else {
1752 return bad_expr_type(expr);
1754 if (expr->op == '+')
1755 *expr = *expr->unop;
1756 expr->ctype = ctype;
1757 return ctype;
1758 Restr:
1759 if (restricted_unop(expr->op, &ctype))
1760 unrestrict(expr, class, &ctype);
1761 goto Normal;
1764 static struct symbol *evaluate_preop(struct expression *expr)
1766 struct symbol *ctype = expr->unop->ctype;
1768 switch (expr->op) {
1769 case '(':
1770 *expr = *expr->unop;
1771 return ctype;
1773 case '+':
1774 case '-':
1775 case '~':
1776 return evaluate_sign(expr);
1778 case '*':
1779 return evaluate_dereference(expr);
1781 case '&':
1782 return evaluate_addressof(expr);
1784 case SPECIAL_INCREMENT:
1785 case SPECIAL_DECREMENT:
1787 * From a type evaluation standpoint the preops are
1788 * the same as the postops
1790 return evaluate_postop(expr);
1792 case '!':
1793 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1794 expr->flags = 0;
1795 if (is_safe_type(ctype))
1796 warning(expr->pos, "testing a 'safe expression'");
1797 if (is_float_type(ctype)) {
1798 struct expression *arg = expr->unop;
1799 expr->type = EXPR_BINOP;
1800 expr->op = SPECIAL_EQUAL;
1801 expr->left = arg;
1802 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1803 expr->right->ctype = ctype;
1804 expr->right->fvalue = 0;
1805 } else if (is_fouled_type(ctype)) {
1806 warning(expr->pos, "%s degrades to integer",
1807 show_typename(ctype->ctype.base_type));
1809 ctype = &bool_ctype;
1810 break;
1812 default:
1813 break;
1815 expr->ctype = ctype;
1816 return &bool_ctype;
1819 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1821 struct ptr_list *head = (struct ptr_list *)_list;
1822 struct ptr_list *list = head;
1824 if (!head)
1825 return NULL;
1826 do {
1827 int i;
1828 for (i = 0; i < list->nr; i++) {
1829 struct symbol *sym = (struct symbol *) list->list[i];
1830 if (sym->ident) {
1831 if (sym->ident != ident)
1832 continue;
1833 *offset = sym->offset;
1834 return sym;
1835 } else {
1836 struct symbol *ctype = sym->ctype.base_type;
1837 struct symbol *sub;
1838 if (!ctype)
1839 continue;
1840 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1841 continue;
1842 sub = find_identifier(ident, ctype->symbol_list, offset);
1843 if (!sub)
1844 continue;
1845 *offset += sym->offset;
1846 return sub;
1849 } while ((list = list->next) != head);
1850 return NULL;
1853 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1855 struct expression *add;
1858 * Create a new add-expression
1860 * NOTE! Even if we just add zero, we need a new node
1861 * for the member pointer, since it has a different
1862 * type than the original pointer. We could make that
1863 * be just a cast, but the fact is, a node is a node,
1864 * so we might as well just do the "add zero" here.
1866 add = alloc_expression(expr->pos, EXPR_BINOP);
1867 add->op = '+';
1868 add->left = expr;
1869 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1870 add->right->ctype = &int_ctype;
1871 add->right->value = offset;
1874 * The ctype of the pointer will be lazily evaluated if
1875 * we ever take the address of this member dereference..
1877 add->ctype = &lazy_ptr_ctype;
1878 return add;
1881 /* structure/union dereference */
1882 static struct symbol *evaluate_member_dereference(struct expression *expr)
1884 int offset;
1885 struct symbol *ctype, *member;
1886 struct expression *deref = expr->deref, *add;
1887 struct ident *ident = expr->member;
1888 unsigned int mod;
1889 int address_space;
1891 if (!evaluate_expression(deref))
1892 return NULL;
1893 if (!ident) {
1894 expression_error(expr, "bad member name");
1895 return NULL;
1898 ctype = deref->ctype;
1899 examine_symbol_type(ctype);
1900 address_space = ctype->ctype.attribute->as;
1901 mod = ctype->ctype.modifiers;
1902 if (ctype->type == SYM_NODE) {
1903 ctype = ctype->ctype.base_type;
1904 address_space |= ctype->ctype.attribute->as;
1905 mod |= ctype->ctype.modifiers;
1907 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1908 expression_error(expr, "expected structure or union");
1909 return NULL;
1911 offset = 0;
1912 member = find_identifier(ident, ctype->symbol_list, &offset);
1913 if (!member) {
1914 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1915 const char *name = "<unnamed>";
1916 int namelen = 9;
1917 if (ctype->ident) {
1918 name = ctype->ident->name;
1919 namelen = ctype->ident->len;
1921 if (ctype->symbol_list)
1922 expression_error(expr, "no member '%s' in %s %.*s",
1923 show_ident(ident), type, namelen, name);
1924 else
1925 expression_error(expr, "using member '%s' in "
1926 "incomplete %s %.*s", show_ident(ident),
1927 type, namelen, name);
1928 return NULL;
1932 * The member needs to take on the address space and modifiers of
1933 * the "parent" type.
1935 member = convert_to_as_mod(member, address_space, mod);
1936 ctype = get_base_type(member);
1938 if (!lvalue_expression(deref)) {
1939 if (deref->type != EXPR_SLICE) {
1940 expr->base = deref;
1941 expr->r_bitpos = 0;
1942 } else {
1943 expr->base = deref->base;
1944 expr->r_bitpos = deref->r_bitpos;
1946 expr->r_bitpos += bytes_to_bits(offset);
1947 expr->type = EXPR_SLICE;
1948 expr->r_nrbits = member->bit_size;
1949 expr->r_bitpos += member->bit_offset;
1950 expr->ctype = member;
1951 return member;
1954 deref = deref->unop;
1955 expr->deref = deref;
1957 add = evaluate_offset(deref, offset);
1958 expr->type = EXPR_PREOP;
1959 expr->op = '*';
1960 expr->unop = add;
1962 expr->ctype = member;
1963 return member;
1966 static int is_promoted(struct expression *expr)
1968 while (1) {
1969 switch (expr->type) {
1970 case EXPR_BINOP:
1971 case EXPR_SELECT:
1972 case EXPR_CONDITIONAL:
1973 return 1;
1974 case EXPR_COMMA:
1975 expr = expr->right;
1976 continue;
1977 case EXPR_PREOP:
1978 switch (expr->op) {
1979 case '(':
1980 expr = expr->unop;
1981 continue;
1982 case '+':
1983 case '-':
1984 case '~':
1985 return 1;
1986 default:
1987 return 0;
1989 default:
1990 return 0;
1996 static struct symbol *evaluate_cast(struct expression *);
1998 static struct symbol *evaluate_type_information(struct expression *expr)
2000 struct symbol *sym = expr->cast_type;
2001 if (!sym) {
2002 sym = evaluate_expression(expr->cast_expression);
2003 if (!sym)
2004 return NULL;
2006 * Expressions of restricted types will possibly get
2007 * promoted - check that here
2009 if (is_restricted_type(sym)) {
2010 if (sym->bit_size < bits_in_int && is_promoted(expr))
2011 sym = &int_ctype;
2012 } else if (is_fouled_type(sym)) {
2013 sym = &int_ctype;
2016 examine_symbol_type(sym);
2017 if (is_bitfield_type(sym)) {
2018 expression_error(expr, "trying to examine bitfield type");
2019 return NULL;
2021 return sym;
2024 static struct symbol *evaluate_sizeof(struct expression *expr)
2026 struct symbol *type;
2027 int size;
2029 type = evaluate_type_information(expr);
2030 if (!type)
2031 return NULL;
2033 size = type->bit_size;
2035 if (size < 0 && is_void_type(type)) {
2036 warning(expr->pos, "expression using sizeof(void)");
2037 size = bits_in_char;
2040 if (size == 1 && is_bool_type(type)) {
2041 warning(expr->pos, "expression using sizeof bool");
2042 size = bits_in_char;
2045 if (is_function(type->ctype.base_type)) {
2046 warning(expr->pos, "expression using sizeof on a function");
2047 size = bits_in_char;
2050 if ((size < 0) || (size & (bits_in_char - 1)))
2051 expression_error(expr, "cannot size expression");
2053 expr->type = EXPR_VALUE;
2054 expr->value = bits_to_bytes(size);
2055 expr->taint = 0;
2056 expr->ctype = size_t_ctype;
2057 return size_t_ctype;
2060 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
2062 struct symbol *type;
2063 int size;
2065 type = evaluate_type_information(expr);
2066 if (!type)
2067 return NULL;
2069 if (type->type == SYM_NODE)
2070 type = type->ctype.base_type;
2071 if (!type)
2072 return NULL;
2073 switch (type->type) {
2074 case SYM_ARRAY:
2075 break;
2076 case SYM_PTR:
2077 type = get_base_type(type);
2078 if (type)
2079 break;
2080 default:
2081 expression_error(expr, "expected pointer expression");
2082 return NULL;
2084 size = type->bit_size;
2085 if (size & (bits_in_char-1))
2086 size = 0;
2087 expr->type = EXPR_VALUE;
2088 expr->value = bits_to_bytes(size);
2089 expr->taint = 0;
2090 expr->ctype = size_t_ctype;
2091 return size_t_ctype;
2094 static struct symbol *evaluate_alignof(struct expression *expr)
2096 struct symbol *type;
2098 type = evaluate_type_information(expr);
2099 if (!type)
2100 return NULL;
2102 expr->type = EXPR_VALUE;
2103 expr->value = type->ctype.alignment;
2104 expr->taint = 0;
2105 expr->ctype = size_t_ctype;
2106 return size_t_ctype;
2109 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
2111 struct expression *expr;
2112 struct symbol_list *argument_types = fn->arguments;
2113 struct symbol *argtype;
2114 int i = 1;
2116 PREPARE_PTR_LIST(argument_types, argtype);
2117 FOR_EACH_PTR (head, expr) {
2118 struct expression **p = THIS_ADDRESS(expr);
2119 struct symbol *ctype, *target;
2120 ctype = evaluate_expression(expr);
2122 if (!ctype)
2123 return 0;
2125 target = argtype;
2126 if (!target) {
2127 struct symbol *type;
2128 int class = classify_type(ctype, &type);
2129 if (is_int(class)) {
2130 *p = cast_to(expr, integer_promotion(type));
2131 } else if (class & TYPE_FLOAT) {
2132 unsigned long mod = type->ctype.modifiers;
2133 if (!(mod & (MOD_LONG_ALL)))
2134 *p = cast_to(expr, &double_ctype);
2135 } else if (class & TYPE_PTR) {
2136 if (expr->ctype == &null_ctype)
2137 *p = cast_to(expr, &ptr_ctype);
2138 else
2139 degenerate(expr);
2141 } else {
2142 static char where[30];
2143 examine_symbol_type(target);
2144 sprintf(where, "argument %d", i);
2145 compatible_assignment_types(expr, target, p, where);
2148 i++;
2149 NEXT_PTR_LIST(argtype);
2150 } END_FOR_EACH_PTR(expr);
2151 FINISH_PTR_LIST(argtype);
2152 return 1;
2155 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
2157 struct symbol *sym;
2159 FOR_EACH_PTR(ctype->symbol_list, sym) {
2160 if (sym->ident == ident)
2161 return sym;
2162 } END_FOR_EACH_PTR(sym);
2163 return NULL;
2166 static void convert_index(struct expression *e)
2168 struct expression *child = e->idx_expression;
2169 unsigned from = e->idx_from;
2170 unsigned to = e->idx_to + 1;
2171 e->type = EXPR_POS;
2172 e->init_offset = from * bits_to_bytes(e->ctype->bit_size);
2173 e->init_nr = to - from;
2174 e->init_expr = child;
2177 static void convert_ident(struct expression *e)
2179 struct expression *child = e->ident_expression;
2180 struct symbol *sym = e->field;
2181 e->type = EXPR_POS;
2182 e->init_offset = sym->offset;
2183 e->init_nr = 1;
2184 e->init_expr = child;
2187 static void convert_designators(struct expression *e)
2189 while (e) {
2190 if (e->type == EXPR_INDEX)
2191 convert_index(e);
2192 else if (e->type == EXPR_IDENTIFIER)
2193 convert_ident(e);
2194 else
2195 break;
2196 e = e->init_expr;
2200 static void excess(struct expression *e, const char *s)
2202 warning(e->pos, "excessive elements in %s initializer", s);
2206 * implicit designator for the first element
2208 static struct expression *first_subobject(struct symbol *ctype, int class,
2209 struct expression **v)
2211 struct expression *e = *v, *new;
2213 if (ctype->type == SYM_NODE)
2214 ctype = ctype->ctype.base_type;
2216 if (class & TYPE_PTR) { /* array */
2217 if (!ctype->bit_size)
2218 return NULL;
2219 new = alloc_expression(e->pos, EXPR_INDEX);
2220 new->idx_expression = e;
2221 new->ctype = ctype->ctype.base_type;
2222 } else {
2223 struct symbol *field, *p;
2224 PREPARE_PTR_LIST(ctype->symbol_list, p);
2225 while (p && !p->ident && is_bitfield_type(p))
2226 NEXT_PTR_LIST(p);
2227 field = p;
2228 FINISH_PTR_LIST(p);
2229 if (!field)
2230 return NULL;
2231 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2232 new->ident_expression = e;
2233 new->field = new->ctype = field;
2235 *v = new;
2236 return new;
2240 * sanity-check explicit designators; return the innermost one or NULL
2241 * in case of error. Assign types.
2243 static struct expression *check_designators(struct expression *e,
2244 struct symbol *ctype)
2246 struct expression *last = NULL;
2247 const char *err;
2248 while (1) {
2249 if (ctype->type == SYM_NODE)
2250 ctype = ctype->ctype.base_type;
2251 if (e->type == EXPR_INDEX) {
2252 struct symbol *type;
2253 if (ctype->type != SYM_ARRAY) {
2254 err = "array index in non-array";
2255 break;
2257 type = ctype->ctype.base_type;
2258 if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2259 unsigned offset = e->idx_to * type->bit_size;
2260 if (offset >= ctype->bit_size) {
2261 err = "index out of bounds in";
2262 break;
2265 e->ctype = ctype = type;
2266 ctype = type;
2267 last = e;
2268 if (!e->idx_expression) {
2269 err = "invalid";
2270 break;
2272 e = e->idx_expression;
2273 } else if (e->type == EXPR_IDENTIFIER) {
2274 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2275 err = "field name not in struct or union";
2276 break;
2278 ctype = find_struct_ident(ctype, e->expr_ident);
2279 if (!ctype) {
2280 err = "unknown field name in";
2281 break;
2283 e->field = e->ctype = ctype;
2284 last = e;
2285 if (!e->ident_expression) {
2286 err = "invalid";
2287 break;
2289 e = e->ident_expression;
2290 } else if (e->type == EXPR_POS) {
2291 err = "internal front-end error: EXPR_POS in";
2292 break;
2293 } else
2294 return last;
2296 expression_error(e, "%s initializer", err);
2297 return NULL;
2301 * choose the next subobject to initialize.
2303 * Get designators for next element, switch old ones to EXPR_POS.
2304 * Return the resulting expression or NULL if we'd run out of subobjects.
2305 * The innermost designator is returned in *v. Designators in old
2306 * are assumed to be already sanity-checked.
2308 static struct expression *next_designators(struct expression *old,
2309 struct symbol *ctype,
2310 struct expression *e, struct expression **v)
2312 struct expression *new = NULL;
2314 if (!old)
2315 return NULL;
2316 if (old->type == EXPR_INDEX) {
2317 struct expression *copy;
2318 unsigned n;
2320 copy = next_designators(old->idx_expression,
2321 old->ctype, e, v);
2322 if (!copy) {
2323 n = old->idx_to + 1;
2324 if (n * old->ctype->bit_size == ctype->bit_size) {
2325 convert_index(old);
2326 return NULL;
2328 copy = e;
2329 *v = new = alloc_expression(e->pos, EXPR_INDEX);
2330 } else {
2331 n = old->idx_to;
2332 new = alloc_expression(e->pos, EXPR_INDEX);
2335 new->idx_from = new->idx_to = n;
2336 new->idx_expression = copy;
2337 new->ctype = old->ctype;
2338 convert_index(old);
2339 } else if (old->type == EXPR_IDENTIFIER) {
2340 struct expression *copy;
2341 struct symbol *field;
2343 copy = next_designators(old->ident_expression,
2344 old->ctype, e, v);
2345 if (!copy) {
2346 field = old->field->next_subobject;
2347 if (!field) {
2348 convert_ident(old);
2349 return NULL;
2351 copy = e;
2352 *v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2353 } else {
2354 field = old->field;
2355 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2358 new->field = field;
2359 new->expr_ident = field->ident;
2360 new->ident_expression = copy;
2361 new->ctype = field;
2362 convert_ident(old);
2364 return new;
2367 static int handle_simple_initializer(struct expression **ep, int nested,
2368 int class, struct symbol *ctype);
2371 * deal with traversing subobjects [6.7.8(17,18,20)]
2373 static void handle_list_initializer(struct expression *expr,
2374 int class, struct symbol *ctype)
2376 struct expression *e, *last = NULL, *top = NULL, *next;
2377 int jumped = 0;
2379 FOR_EACH_PTR(expr->expr_list, e) {
2380 struct expression **v;
2381 struct symbol *type;
2382 int lclass;
2384 if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2385 struct symbol *struct_sym;
2386 if (!top) {
2387 top = e;
2388 last = first_subobject(ctype, class, &top);
2389 } else {
2390 last = next_designators(last, ctype, e, &top);
2392 if (!last) {
2393 excess(e, class & TYPE_PTR ? "array" :
2394 "struct or union");
2395 DELETE_CURRENT_PTR(e);
2396 continue;
2398 struct_sym = ctype->type == SYM_NODE ? ctype->ctype.base_type : ctype;
2399 if (Wdesignated_init && struct_sym->designated_init)
2400 warning(e->pos, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2401 ctype->ident ? "in initializer for " : "",
2402 ctype->ident ? ctype->ident->len : 0,
2403 ctype->ident ? ctype->ident->name : "",
2404 ctype->ident ? ": " : "",
2405 get_type_name(struct_sym->type),
2406 show_ident(struct_sym->ident));
2407 if (jumped) {
2408 warning(e->pos, "advancing past deep designator");
2409 jumped = 0;
2411 REPLACE_CURRENT_PTR(e, last);
2412 } else {
2413 next = check_designators(e, ctype);
2414 if (!next) {
2415 DELETE_CURRENT_PTR(e);
2416 continue;
2418 top = next;
2419 /* deeper than one designator? */
2420 jumped = top != e;
2421 convert_designators(last);
2422 last = e;
2425 found:
2426 lclass = classify_type(top->ctype, &type);
2427 if (top->type == EXPR_INDEX)
2428 v = &top->idx_expression;
2429 else
2430 v = &top->ident_expression;
2432 if (handle_simple_initializer(v, 1, lclass, top->ctype))
2433 continue;
2435 if (!(lclass & TYPE_COMPOUND)) {
2436 warning(e->pos, "bogus scalar initializer");
2437 DELETE_CURRENT_PTR(e);
2438 continue;
2441 next = first_subobject(type, lclass, v);
2442 if (next) {
2443 warning(e->pos, "missing braces around initializer");
2444 top = next;
2445 goto found;
2448 DELETE_CURRENT_PTR(e);
2449 excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2451 } END_FOR_EACH_PTR(e);
2453 convert_designators(last);
2454 expr->ctype = ctype;
2457 static int is_string_literal(struct expression **v)
2459 struct expression *e = *v;
2460 while (e && e->type == EXPR_PREOP && e->op == '(')
2461 e = e->unop;
2462 if (!e || e->type != EXPR_STRING)
2463 return 0;
2464 if (e != *v && Wparen_string)
2465 warning(e->pos,
2466 "array initialized from parenthesized string constant");
2467 *v = e;
2468 return 1;
2472 * We want a normal expression, possibly in one layer of braces. Warn
2473 * if the latter happens inside a list (it's legal, but likely to be
2474 * an effect of screwup). In case of anything not legal, we are definitely
2475 * having an effect of screwup, so just fail and let the caller warn.
2477 static struct expression *handle_scalar(struct expression *e, int nested)
2479 struct expression *v = NULL, *p;
2480 int count = 0;
2482 /* normal case */
2483 if (e->type != EXPR_INITIALIZER)
2484 return e;
2486 FOR_EACH_PTR(e->expr_list, p) {
2487 if (!v)
2488 v = p;
2489 count++;
2490 } END_FOR_EACH_PTR(p);
2491 if (count != 1)
2492 return NULL;
2493 switch(v->type) {
2494 case EXPR_INITIALIZER:
2495 case EXPR_INDEX:
2496 case EXPR_IDENTIFIER:
2497 return NULL;
2498 default:
2499 break;
2501 if (nested)
2502 warning(e->pos, "braces around scalar initializer");
2503 return v;
2507 * deal with the cases that don't care about subobjects:
2508 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2509 * character array <- string literal, possibly in braces [6.7.8(14)]
2510 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2511 * compound type <- initializer list in braces [6.7.8(16)]
2512 * The last one punts to handle_list_initializer() which, in turn will call
2513 * us for individual elements of the list.
2515 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2516 * the lack of support of wide char stuff in general.
2518 * One note: we need to take care not to evaluate a string literal until
2519 * we know that we *will* handle it right here. Otherwise we would screw
2520 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2521 * { "string", ...} - we need to preserve that string literal recognizable
2522 * until we dig into the inner struct.
2524 static int handle_simple_initializer(struct expression **ep, int nested,
2525 int class, struct symbol *ctype)
2527 int is_string = is_string_type(ctype);
2528 struct expression *e = *ep, *p;
2529 struct symbol *type;
2531 if (!e)
2532 return 0;
2534 /* scalar */
2535 if (!(class & TYPE_COMPOUND)) {
2536 e = handle_scalar(e, nested);
2537 if (!e)
2538 return 0;
2539 *ep = e;
2540 if (!evaluate_expression(e))
2541 return 1;
2542 compatible_assignment_types(e, ctype, ep, "initializer");
2543 return 1;
2547 * sublist; either a string, or we dig in; the latter will deal with
2548 * pathologies, so we don't need anything fancy here.
2550 if (e->type == EXPR_INITIALIZER) {
2551 if (is_string) {
2552 struct expression *v = NULL;
2553 int count = 0;
2555 FOR_EACH_PTR(e->expr_list, p) {
2556 if (!v)
2557 v = p;
2558 count++;
2559 } END_FOR_EACH_PTR(p);
2560 if (count == 1 && is_string_literal(&v)) {
2561 *ep = e = v;
2562 goto String;
2565 handle_list_initializer(e, class, ctype);
2566 return 1;
2569 /* string */
2570 if (is_string_literal(&e)) {
2571 /* either we are doing array of char, or we'll have to dig in */
2572 if (is_string) {
2573 *ep = e;
2574 goto String;
2576 return 0;
2578 /* struct or union can be initialized by compatible */
2579 if (class != TYPE_COMPOUND)
2580 return 0;
2581 type = evaluate_expression(e);
2582 if (!type)
2583 return 0;
2584 if (ctype->type == SYM_NODE)
2585 ctype = ctype->ctype.base_type;
2586 if (type->type == SYM_NODE)
2587 type = type->ctype.base_type;
2588 if (ctype == type)
2589 return 1;
2590 return 0;
2592 String:
2593 p = alloc_expression(e->pos, EXPR_STRING);
2594 *p = *e;
2595 type = evaluate_expression(p);
2596 if (ctype->bit_size != -1 &&
2597 ctype->bit_size + bits_in_char < type->bit_size) {
2598 warning(e->pos,
2599 "too long initializer-string for array of char");
2601 *ep = p;
2602 return 1;
2605 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2607 struct symbol *type;
2608 int class = classify_type(ctype, &type);
2609 if (!handle_simple_initializer(ep, 0, class, ctype))
2610 expression_error(*ep, "invalid initializer");
2613 static struct symbol *evaluate_cast(struct expression *expr)
2615 struct expression *target = expr->cast_expression;
2616 struct symbol *ctype;
2617 struct symbol *t1, *t2;
2618 int class1, class2;
2619 int as1 = 0, as2 = 0;
2621 if (!target)
2622 return NULL;
2625 * Special case: a cast can be followed by an
2626 * initializer, in which case we need to pass
2627 * the type value down to that initializer rather
2628 * than trying to evaluate it as an expression
2630 * A more complex case is when the initializer is
2631 * dereferenced as part of a post-fix expression.
2632 * We need to produce an expression that can be dereferenced.
2634 if (target->type == EXPR_INITIALIZER) {
2635 struct symbol *sym = expr->cast_type;
2636 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2638 sym->initializer = target;
2639 evaluate_symbol(sym);
2641 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2642 addr->symbol = sym;
2644 expr->type = EXPR_PREOP;
2645 expr->op = '*';
2646 expr->unop = addr;
2647 expr->ctype = sym;
2649 return sym;
2652 ctype = examine_symbol_type(expr->cast_type);
2653 expr->ctype = ctype;
2654 expr->cast_type = ctype;
2656 evaluate_expression(target);
2657 degenerate(target);
2659 class1 = classify_type(ctype, &t1);
2661 /* cast to non-integer type -> not an integer constant expression */
2662 if (!is_int(class1))
2663 expr->flags = 0;
2664 /* if argument turns out to be not an integer constant expression *and*
2665 it was not a floating literal to start with -> too bad */
2666 else if (expr->flags == Int_const_expr &&
2667 !(target->flags & Int_const_expr))
2668 expr->flags = 0;
2670 * You can always throw a value away by casting to
2671 * "void" - that's an implicit "force". Note that
2672 * the same is _not_ true of "void *".
2674 if (t1 == &void_ctype)
2675 goto out;
2677 if (class1 & (TYPE_COMPOUND | TYPE_FN))
2678 warning(expr->pos, "cast to non-scalar");
2680 t2 = target->ctype;
2681 if (!t2) {
2682 expression_error(expr, "cast from unknown type");
2683 goto out;
2685 class2 = classify_type(t2, &t2);
2687 if (class2 & TYPE_COMPOUND)
2688 warning(expr->pos, "cast from non-scalar");
2690 if (expr->type == EXPR_FORCE_CAST)
2691 goto out;
2693 /* allowed cast unfouls */
2694 if (class2 & TYPE_FOULED)
2695 t2 = unfoul(t2);
2697 if (t1 != t2) {
2698 if (class1 & TYPE_RESTRICT)
2699 warning(expr->pos, "cast to %s",
2700 show_typename(t1));
2701 if (class2 & TYPE_RESTRICT)
2702 warning(expr->pos, "cast from %s",
2703 show_typename(t2));
2706 if (t1 == &ulong_ctype)
2707 as1 = -1;
2708 else if (class1 == TYPE_PTR) {
2709 examine_pointer_target(t1);
2710 as1 = t1->ctype.attribute->as;
2713 if (t2 == &ulong_ctype)
2714 as2 = -1;
2715 else if (class2 == TYPE_PTR) {
2716 examine_pointer_target(t2);
2717 as2 = t2->ctype.attribute->as;
2720 if (!as1 && as2 > 0)
2721 warning(expr->pos, "cast removes address space of expression");
2722 if (as1 > 0 && as2 > 0 && as1 != as2)
2723 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2724 if (as1 > 0 && !as2 &&
2725 !is_null_pointer_constant(target) && Wcast_to_as)
2726 warning(expr->pos,
2727 "cast adds address space to expression (<asn:%d>)", as1);
2729 if (!(t1->ctype.modifiers & MOD_PTRINHERIT) && class1 == TYPE_PTR &&
2730 !as1 && (target->flags & Int_const_expr)) {
2731 if (t1->ctype.base_type == &void_ctype) {
2732 if (is_zero_constant(target)) {
2733 /* NULL */
2734 expr->type = EXPR_VALUE;
2735 expr->ctype = &null_ctype;
2736 expr->value = 0;
2737 return ctype;
2741 out:
2742 return ctype;
2746 * Evaluate a call expression with a symbol. This
2747 * should expand inline functions, and evaluate
2748 * builtins.
2750 static int evaluate_symbol_call(struct expression *expr)
2752 struct expression *fn = expr->fn;
2753 struct symbol *ctype = fn->ctype;
2755 if (fn->type != EXPR_PREOP)
2756 return 0;
2758 if (ctype->op && ctype->op->evaluate)
2759 return ctype->op->evaluate(expr);
2761 if (ctype->ctype.modifiers & MOD_INLINE) {
2762 int ret;
2763 struct symbol *curr = current_fn;
2765 if (ctype->definition)
2766 ctype = ctype->definition;
2768 current_fn = ctype->ctype.base_type;
2770 ret = inline_function(expr, ctype);
2772 /* restore the old function */
2773 current_fn = curr;
2774 return ret;
2777 return 0;
2780 static struct symbol *evaluate_call(struct expression *expr)
2782 int args, fnargs;
2783 struct symbol *ctype, *sym;
2784 struct expression *fn = expr->fn;
2785 struct expression_list *arglist = expr->args;
2787 if (!evaluate_expression(fn))
2788 return NULL;
2789 sym = ctype = fn->ctype;
2790 if (ctype->type == SYM_NODE)
2791 ctype = ctype->ctype.base_type;
2792 if (ctype->type == SYM_PTR)
2793 ctype = get_base_type(ctype);
2795 if (ctype->type != SYM_FN) {
2796 struct expression *arg;
2797 expression_error(expr, "not a function %s",
2798 show_ident(sym->ident));
2799 /* do typechecking in arguments */
2800 FOR_EACH_PTR (arglist, arg) {
2801 evaluate_expression(arg);
2802 } END_FOR_EACH_PTR(arg);
2803 return NULL;
2806 examine_fn_arguments(ctype);
2807 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
2808 sym->op && sym->op->args) {
2809 if (!sym->op->args(expr))
2810 return NULL;
2811 } else {
2812 if (!evaluate_arguments(sym, ctype, arglist))
2813 return NULL;
2814 args = expression_list_size(expr->args);
2815 fnargs = symbol_list_size(ctype->arguments);
2816 if (args < fnargs)
2817 expression_error(expr,
2818 "not enough arguments for function %s",
2819 show_ident(sym->ident));
2820 if (args > fnargs && !ctype->variadic)
2821 expression_error(expr,
2822 "too many arguments for function %s",
2823 show_ident(sym->ident));
2825 if (sym->type == SYM_NODE) {
2826 if (evaluate_symbol_call(expr))
2827 return expr->ctype;
2829 expr->ctype = ctype->ctype.base_type;
2830 return expr->ctype;
2833 static struct symbol *evaluate_offsetof(struct expression *expr)
2835 struct expression *e = expr->down;
2836 struct symbol *ctype = expr->in;
2837 int class;
2839 if (expr->op == '.') {
2840 struct symbol *field;
2841 int offset = 0;
2842 if (!ctype) {
2843 expression_error(expr, "expected structure or union");
2844 return NULL;
2846 examine_symbol_type(ctype);
2847 class = classify_type(ctype, &ctype);
2848 if (class != TYPE_COMPOUND) {
2849 expression_error(expr, "expected structure or union");
2850 return NULL;
2853 field = find_identifier(expr->ident, ctype->symbol_list, &offset);
2854 if (!field) {
2855 expression_error(expr, "unknown member");
2856 return NULL;
2858 ctype = field;
2859 expr->type = EXPR_VALUE;
2860 expr->flags = Int_const_expr;
2861 expr->value = offset;
2862 expr->taint = 0;
2863 expr->ctype = size_t_ctype;
2864 } else {
2865 if (!ctype) {
2866 expression_error(expr, "expected structure or union");
2867 return NULL;
2869 examine_symbol_type(ctype);
2870 class = classify_type(ctype, &ctype);
2871 if (class != (TYPE_COMPOUND | TYPE_PTR)) {
2872 expression_error(expr, "expected array");
2873 return NULL;
2875 ctype = ctype->ctype.base_type;
2876 if (!expr->index) {
2877 expr->type = EXPR_VALUE;
2878 expr->flags = Int_const_expr;
2879 expr->value = 0;
2880 expr->taint = 0;
2881 expr->ctype = size_t_ctype;
2882 } else {
2883 struct expression *idx = expr->index, *m;
2884 struct symbol *i_type = evaluate_expression(idx);
2885 int i_class = classify_type(i_type, &i_type);
2886 if (!is_int(i_class)) {
2887 expression_error(expr, "non-integer index");
2888 return NULL;
2890 unrestrict(idx, i_class, &i_type);
2891 idx = cast_to(idx, size_t_ctype);
2892 m = alloc_const_expression(expr->pos,
2893 bits_to_bytes(ctype->bit_size));
2894 m->ctype = size_t_ctype;
2895 m->flags = Int_const_expr;
2896 expr->type = EXPR_BINOP;
2897 expr->left = idx;
2898 expr->right = m;
2899 expr->op = '*';
2900 expr->ctype = size_t_ctype;
2901 expr->flags = m->flags & idx->flags & Int_const_expr;
2904 if (e) {
2905 struct expression *copy = __alloc_expression(0);
2906 *copy = *expr;
2907 if (e->type == EXPR_OFFSETOF)
2908 e->in = ctype;
2909 if (!evaluate_expression(e))
2910 return NULL;
2911 expr->type = EXPR_BINOP;
2912 expr->flags = e->flags & copy->flags & Int_const_expr;
2913 expr->op = '+';
2914 expr->ctype = size_t_ctype;
2915 expr->left = copy;
2916 expr->right = e;
2918 return size_t_ctype;
2921 struct symbol *evaluate_expression(struct expression *expr)
2923 if (!expr)
2924 return NULL;
2925 if (expr->ctype)
2926 return expr->ctype;
2928 switch (expr->type) {
2929 case EXPR_VALUE:
2930 case EXPR_FVALUE:
2931 expression_error(expr, "value expression without a type");
2932 return NULL;
2933 case EXPR_STRING:
2934 return evaluate_string(expr);
2935 case EXPR_SYMBOL:
2936 return evaluate_symbol_expression(expr);
2937 case EXPR_BINOP:
2938 if (!evaluate_expression(expr->left))
2939 return NULL;
2940 if (!evaluate_expression(expr->right))
2941 return NULL;
2942 return evaluate_binop(expr);
2943 case EXPR_LOGICAL:
2944 return evaluate_logical(expr);
2945 case EXPR_COMMA:
2946 evaluate_expression(expr->left);
2947 if (!evaluate_expression(expr->right))
2948 return NULL;
2949 return evaluate_comma(expr);
2950 case EXPR_COMPARE:
2951 if (!evaluate_expression(expr->left))
2952 return NULL;
2953 if (!evaluate_expression(expr->right))
2954 return NULL;
2955 return evaluate_compare(expr);
2956 case EXPR_ASSIGNMENT:
2957 if (!evaluate_expression(expr->left))
2958 return NULL;
2959 if (!evaluate_expression(expr->right))
2960 return NULL;
2961 return evaluate_assignment(expr);
2962 case EXPR_PREOP:
2963 if (!evaluate_expression(expr->unop))
2964 return NULL;
2965 return evaluate_preop(expr);
2966 case EXPR_POSTOP:
2967 if (!evaluate_expression(expr->unop))
2968 return NULL;
2969 return evaluate_postop(expr);
2970 case EXPR_CAST:
2971 case EXPR_FORCE_CAST:
2972 case EXPR_IMPLIED_CAST:
2973 return evaluate_cast(expr);
2974 case EXPR_SIZEOF:
2975 return evaluate_sizeof(expr);
2976 case EXPR_PTRSIZEOF:
2977 return evaluate_ptrsizeof(expr);
2978 case EXPR_ALIGNOF:
2979 return evaluate_alignof(expr);
2980 case EXPR_DEREF:
2981 return evaluate_member_dereference(expr);
2982 case EXPR_CALL:
2983 return evaluate_call(expr);
2984 case EXPR_SELECT:
2985 case EXPR_CONDITIONAL:
2986 return evaluate_conditional_expression(expr);
2987 case EXPR_STATEMENT:
2988 expr->ctype = evaluate_statement(expr->statement);
2989 return expr->ctype;
2991 case EXPR_LABEL:
2992 expr->ctype = &ptr_ctype;
2993 return &ptr_ctype;
2995 case EXPR_TYPE:
2996 /* Evaluate the type of the symbol .. */
2997 evaluate_symbol(expr->symbol);
2998 /* .. but the type of the _expression_ is a "type" */
2999 expr->ctype = &type_ctype;
3000 return &type_ctype;
3002 case EXPR_OFFSETOF:
3003 return evaluate_offsetof(expr);
3005 /* These can not exist as stand-alone expressions */
3006 case EXPR_INITIALIZER:
3007 case EXPR_IDENTIFIER:
3008 case EXPR_INDEX:
3009 case EXPR_POS:
3010 expression_error(expr, "internal front-end error: initializer in expression");
3011 return NULL;
3012 case EXPR_SLICE:
3013 expression_error(expr, "internal front-end error: SLICE re-evaluated");
3014 return NULL;
3016 return NULL;
3019 static void check_duplicates(struct symbol *sym)
3021 int declared = 0;
3022 struct symbol *next = sym;
3024 while ((next = next->same_symbol) != NULL) {
3025 const char *typediff;
3026 evaluate_symbol(next);
3027 declared++;
3028 typediff = type_difference(&sym->ctype, &next->ctype, 0, 0);
3029 if (typediff) {
3030 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
3031 show_ident(sym->ident),
3032 stream_name(next->pos.stream), next->pos.line, typediff);
3033 return;
3036 if (!declared) {
3037 unsigned long mod = sym->ctype.modifiers;
3038 if (mod & (MOD_STATIC | MOD_REGISTER))
3039 return;
3040 if (!(mod & MOD_TOPLEVEL))
3041 return;
3042 if (!Wdecl)
3043 return;
3044 if (sym->ident == &main_ident)
3045 return;
3046 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
3050 static struct symbol *evaluate_symbol(struct symbol *sym)
3052 struct symbol *base_type;
3054 if (!sym)
3055 return sym;
3056 if (sym->evaluated)
3057 return sym;
3058 sym->evaluated = 1;
3060 sym = examine_symbol_type(sym);
3061 base_type = get_base_type(sym);
3062 if (!base_type)
3063 return NULL;
3065 /* Evaluate the initializers */
3066 if (sym->initializer)
3067 evaluate_initializer(sym, &sym->initializer);
3069 /* And finally, evaluate the body of the symbol too */
3070 if (base_type->type == SYM_FN) {
3071 struct symbol *curr = current_fn;
3073 if (sym->definition && sym->definition != sym)
3074 return evaluate_symbol(sym->definition);
3076 current_fn = base_type;
3078 examine_fn_arguments(base_type);
3079 if (!base_type->stmt && base_type->inline_stmt)
3080 uninline(sym);
3081 if (base_type->stmt)
3082 evaluate_statement(base_type->stmt);
3084 current_fn = curr;
3087 return base_type;
3090 void evaluate_symbol_list(struct symbol_list *list)
3092 struct symbol *sym;
3094 FOR_EACH_PTR(list, sym) {
3095 evaluate_symbol(sym);
3096 check_duplicates(sym);
3097 } END_FOR_EACH_PTR(sym);
3100 static struct symbol *evaluate_return_expression(struct statement *stmt)
3102 struct expression *expr = stmt->expression;
3103 struct symbol *fntype;
3105 evaluate_expression(expr);
3106 fntype = current_fn->ctype.base_type;
3107 if (!fntype || fntype == &void_ctype) {
3108 if (expr && expr->ctype != &void_ctype)
3109 expression_error(expr, "return expression in %s function", fntype?"void":"typeless");
3110 if (expr && Wreturn_void)
3111 warning(stmt->pos, "returning void-valued expression");
3112 return NULL;
3115 if (!expr) {
3116 sparse_error(stmt->pos, "return with no return value");
3117 return NULL;
3119 if (!expr->ctype)
3120 return NULL;
3121 compatible_assignment_types(expr, fntype, &stmt->expression, "return expression");
3122 return NULL;
3125 static void evaluate_if_statement(struct statement *stmt)
3127 if (!stmt->if_conditional)
3128 return;
3130 evaluate_conditional(stmt->if_conditional, 0);
3131 evaluate_statement(stmt->if_true);
3132 evaluate_statement(stmt->if_false);
3135 static void evaluate_iterator(struct statement *stmt)
3137 evaluate_symbol_list(stmt->iterator_syms);
3138 evaluate_conditional(stmt->iterator_pre_condition, 1);
3139 evaluate_conditional(stmt->iterator_post_condition,1);
3140 evaluate_statement(stmt->iterator_pre_statement);
3141 evaluate_statement(stmt->iterator_statement);
3142 evaluate_statement(stmt->iterator_post_statement);
3145 static void verify_output_constraint(struct expression *expr, const char *constraint)
3147 switch (*constraint) {
3148 case '=': /* Assignment */
3149 case '+': /* Update */
3150 break;
3151 default:
3152 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
3156 static void verify_input_constraint(struct expression *expr, const char *constraint)
3158 switch (*constraint) {
3159 case '=': /* Assignment */
3160 case '+': /* Update */
3161 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
3165 static void evaluate_asm_statement(struct statement *stmt)
3167 struct expression *expr;
3168 struct symbol *sym;
3169 int state;
3171 expr = stmt->asm_string;
3172 if (!expr || expr->type != EXPR_STRING) {
3173 sparse_error(stmt->pos, "need constant string for inline asm");
3174 return;
3177 state = 0;
3178 FOR_EACH_PTR(stmt->asm_outputs, expr) {
3179 switch (state) {
3180 case 0: /* Identifier */
3181 state = 1;
3182 continue;
3184 case 1: /* Constraint */
3185 state = 2;
3186 if (!expr || expr->type != EXPR_STRING) {
3187 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
3188 *THIS_ADDRESS(expr) = NULL;
3189 continue;
3191 verify_output_constraint(expr, expr->string->data);
3192 continue;
3194 case 2: /* Expression */
3195 state = 0;
3196 if (!evaluate_expression(expr))
3197 return;
3198 if (!lvalue_expression(expr))
3199 warning(expr->pos, "asm output is not an lvalue");
3200 evaluate_assign_to(expr, expr->ctype);
3201 continue;
3203 } END_FOR_EACH_PTR(expr);
3205 state = 0;
3206 FOR_EACH_PTR(stmt->asm_inputs, expr) {
3207 switch (state) {
3208 case 0: /* Identifier */
3209 state = 1;
3210 continue;
3212 case 1: /* Constraint */
3213 state = 2;
3214 if (!expr || expr->type != EXPR_STRING) {
3215 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
3216 *THIS_ADDRESS(expr) = NULL;
3217 continue;
3219 verify_input_constraint(expr, expr->string->data);
3220 continue;
3222 case 2: /* Expression */
3223 state = 0;
3224 if (!evaluate_expression(expr))
3225 return;
3226 continue;
3228 } END_FOR_EACH_PTR(expr);
3230 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
3231 if (!expr) {
3232 sparse_error(stmt->pos, "bad asm clobbers");
3233 return;
3235 if (expr->type == EXPR_STRING)
3236 continue;
3237 expression_error(expr, "asm clobber is not a string");
3238 } END_FOR_EACH_PTR(expr);
3240 FOR_EACH_PTR(stmt->asm_labels, sym) {
3241 if (!sym || sym->type != SYM_LABEL) {
3242 sparse_error(stmt->pos, "bad asm label");
3243 return;
3245 } END_FOR_EACH_PTR(sym);
3248 static void evaluate_case_statement(struct statement *stmt)
3250 evaluate_expression(stmt->case_expression);
3251 evaluate_expression(stmt->case_to);
3252 evaluate_statement(stmt->case_statement);
3255 static void check_case_type(struct expression *switch_expr,
3256 struct expression *case_expr,
3257 struct expression **enumcase)
3259 struct symbol *switch_type, *case_type;
3260 int sclass, cclass;
3262 if (!case_expr)
3263 return;
3265 switch_type = switch_expr->ctype;
3266 case_type = evaluate_expression(case_expr);
3268 if (!switch_type || !case_type)
3269 goto Bad;
3270 if (enumcase) {
3271 if (*enumcase)
3272 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
3273 else if (is_enum_type(case_type))
3274 *enumcase = case_expr;
3277 sclass = classify_type(switch_type, &switch_type);
3278 cclass = classify_type(case_type, &case_type);
3280 /* both should be arithmetic */
3281 if (!(sclass & cclass & TYPE_NUM))
3282 goto Bad;
3284 /* neither should be floating */
3285 if ((sclass | cclass) & TYPE_FLOAT)
3286 goto Bad;
3288 /* if neither is restricted, we are OK */
3289 if (!((sclass | cclass) & TYPE_RESTRICT))
3290 return;
3292 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
3293 cclass, sclass, case_type, switch_type)) {
3294 unrestrict(case_expr, cclass, &case_type);
3295 unrestrict(switch_expr, sclass, &switch_type);
3297 return;
3299 Bad:
3300 expression_error(case_expr, "incompatible types for 'case' statement");
3303 static void evaluate_switch_statement(struct statement *stmt)
3305 struct symbol *sym;
3306 struct expression *enumcase = NULL;
3307 struct expression **enumcase_holder = &enumcase;
3308 struct expression *sel = stmt->switch_expression;
3310 evaluate_expression(sel);
3311 evaluate_statement(stmt->switch_statement);
3312 if (!sel)
3313 return;
3314 if (sel->ctype && is_enum_type(sel->ctype))
3315 enumcase_holder = NULL; /* Only check cases against switch */
3317 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
3318 struct statement *case_stmt = sym->stmt;
3319 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
3320 check_case_type(sel, case_stmt->case_to, enumcase_holder);
3321 } END_FOR_EACH_PTR(sym);
3324 static void evaluate_goto_statement(struct statement *stmt)
3326 struct symbol *label = stmt->goto_label;
3328 if (label && !label->stmt)
3329 sparse_error(stmt->pos, "label '%s' was not declared", show_ident(label->ident));
3331 evaluate_expression(stmt->goto_expression);
3334 struct symbol *evaluate_statement(struct statement *stmt)
3336 if (!stmt)
3337 return NULL;
3339 switch (stmt->type) {
3340 case STMT_DECLARATION: {
3341 struct symbol *s;
3342 FOR_EACH_PTR(stmt->declaration, s) {
3343 evaluate_symbol(s);
3344 } END_FOR_EACH_PTR(s);
3345 return NULL;
3348 case STMT_RETURN:
3349 return evaluate_return_expression(stmt);
3351 case STMT_EXPRESSION:
3352 if (!evaluate_expression(stmt->expression))
3353 return NULL;
3354 if (stmt->expression->ctype == &null_ctype)
3355 stmt->expression = cast_to(stmt->expression, &ptr_ctype);
3356 return degenerate(stmt->expression);
3358 case STMT_COMPOUND: {
3359 struct statement *s;
3360 struct symbol *type = NULL;
3362 /* Evaluate the return symbol in the compound statement */
3363 evaluate_symbol(stmt->ret);
3366 * Then, evaluate each statement, making the type of the
3367 * compound statement be the type of the last statement
3369 type = evaluate_statement(stmt->args);
3370 FOR_EACH_PTR(stmt->stmts, s) {
3371 type = evaluate_statement(s);
3372 } END_FOR_EACH_PTR(s);
3373 if (!type)
3374 type = &void_ctype;
3375 return type;
3377 case STMT_IF:
3378 evaluate_if_statement(stmt);
3379 return NULL;
3380 case STMT_ITERATOR:
3381 evaluate_iterator(stmt);
3382 return NULL;
3383 case STMT_SWITCH:
3384 evaluate_switch_statement(stmt);
3385 return NULL;
3386 case STMT_CASE:
3387 evaluate_case_statement(stmt);
3388 return NULL;
3389 case STMT_LABEL:
3390 return evaluate_statement(stmt->label_statement);
3391 case STMT_GOTO:
3392 evaluate_goto_statement(stmt);
3393 return NULL;
3394 case STMT_NONE:
3395 break;
3396 case STMT_ASM:
3397 evaluate_asm_statement(stmt);
3398 return NULL;
3399 case STMT_CONTEXT:
3400 evaluate_expression(stmt->expression);
3401 return NULL;
3402 case STMT_RANGE:
3403 evaluate_expression(stmt->range_expression);
3404 evaluate_expression(stmt->range_low);
3405 evaluate_expression(stmt->range_high);
3406 return NULL;
3408 return NULL;