Merge branch 'llvmcore'
[smatch.git] / evaluate.c
blob19be637e00a55631861d624addfffdb948cc6785
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 /* the result is int [6.5.13(3), 6.5.14(3)] */
864 expr->ctype = &int_ctype;
865 if (expr->flags) {
866 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
867 expr->flags = 0;
869 return &int_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.as == rtype->ctype.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 /* the result is int [6.5.8(6), 6.5.9(3)]*/
1069 expr->ctype = &int_ctype;
1070 return &int_ctype;
1074 * NOTE! The degenerate case of "x ? : y", where we don't
1075 * have a true case, this will possibly promote "x" to the
1076 * same type as "y", and thus _change_ the conditional
1077 * test in the expression. But since promotion is "safe"
1078 * for testing, that's OK.
1080 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1082 struct expression **true;
1083 struct symbol *ctype, *ltype, *rtype, *lbase, *rbase;
1084 int lclass, rclass;
1085 const char * typediff;
1086 int qual;
1088 if (!evaluate_conditional(expr->conditional, 0))
1089 return NULL;
1090 if (!evaluate_expression(expr->cond_false))
1091 return NULL;
1093 ctype = degenerate(expr->conditional);
1094 rtype = degenerate(expr->cond_false);
1096 true = &expr->conditional;
1097 ltype = ctype;
1098 if (expr->cond_true) {
1099 if (!evaluate_expression(expr->cond_true))
1100 return NULL;
1101 ltype = degenerate(expr->cond_true);
1102 true = &expr->cond_true;
1105 if (expr->flags) {
1106 int flags = expr->conditional->flags & Int_const_expr;
1107 flags &= (*true)->flags & expr->cond_false->flags;
1108 if (!flags)
1109 expr->flags = 0;
1112 lclass = classify_type(ltype, &ltype);
1113 rclass = classify_type(rtype, &rtype);
1114 if (lclass & rclass & TYPE_NUM) {
1115 ctype = usual_conversions('?', *true, expr->cond_false,
1116 lclass, rclass, ltype, rtype);
1117 *true = cast_to(*true, ctype);
1118 expr->cond_false = cast_to(expr->cond_false, ctype);
1119 goto out;
1122 if ((lclass | rclass) & TYPE_PTR) {
1123 int is_null1 = is_null_pointer_constant(*true);
1124 int is_null2 = is_null_pointer_constant(expr->cond_false);
1126 if (is_null1 && is_null2) {
1127 *true = cast_to(*true, &ptr_ctype);
1128 expr->cond_false = cast_to(expr->cond_false, &ptr_ctype);
1129 ctype = &ptr_ctype;
1130 goto out;
1132 if (is_null1 && (rclass & TYPE_PTR)) {
1133 if (is_null1 == 2)
1134 bad_null(*true);
1135 *true = cast_to(*true, rtype);
1136 ctype = rtype;
1137 goto out;
1139 if (is_null2 && (lclass & TYPE_PTR)) {
1140 if (is_null2 == 2)
1141 bad_null(expr->cond_false);
1142 expr->cond_false = cast_to(expr->cond_false, ltype);
1143 ctype = ltype;
1144 goto out;
1146 if (!(lclass & rclass & TYPE_PTR)) {
1147 typediff = "different types";
1148 goto Err;
1150 /* OK, it's pointer on pointer */
1151 if (ltype->ctype.as != rtype->ctype.as) {
1152 typediff = "different address spaces";
1153 goto Err;
1156 /* need to be lazier here */
1157 lbase = examine_pointer_target(ltype);
1158 rbase = examine_pointer_target(rtype);
1159 qual = target_qualifiers(ltype) | target_qualifiers(rtype);
1161 if (lbase == &void_ctype) {
1162 /* XXX: pointers to function should warn here */
1163 ctype = ltype;
1164 goto Qual;
1167 if (rbase == &void_ctype) {
1168 /* XXX: pointers to function should warn here */
1169 ctype = rtype;
1170 goto Qual;
1172 /* XXX: that should be pointer to composite */
1173 ctype = ltype;
1174 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1175 qual, qual);
1176 if (!typediff)
1177 goto Qual;
1178 goto Err;
1181 /* void on void, struct on same struct, union on same union */
1182 if (ltype == rtype) {
1183 ctype = ltype;
1184 goto out;
1186 typediff = "different base types";
1188 Err:
1189 expression_error(expr, "incompatible types in conditional expression (%s)", typediff);
1190 return NULL;
1192 out:
1193 expr->ctype = ctype;
1194 return ctype;
1196 Qual:
1197 if (qual & ~ctype->ctype.modifiers) {
1198 struct symbol *sym = alloc_symbol(ctype->pos, SYM_PTR);
1199 *sym = *ctype;
1200 sym->ctype.modifiers |= qual;
1201 ctype = sym;
1203 *true = cast_to(*true, ctype);
1204 expr->cond_false = cast_to(expr->cond_false, ctype);
1205 goto out;
1208 /* FP assignments can not do modulo or bit operations */
1209 static int compatible_float_op(int op)
1211 return op == SPECIAL_ADD_ASSIGN ||
1212 op == SPECIAL_SUB_ASSIGN ||
1213 op == SPECIAL_MUL_ASSIGN ||
1214 op == SPECIAL_DIV_ASSIGN;
1217 static int evaluate_assign_op(struct expression *expr)
1219 struct symbol *target = expr->left->ctype;
1220 struct symbol *source = expr->right->ctype;
1221 struct symbol *t, *s;
1222 int tclass = classify_type(target, &t);
1223 int sclass = classify_type(source, &s);
1224 int op = expr->op;
1226 if (tclass & sclass & TYPE_NUM) {
1227 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1228 expression_error(expr, "invalid assignment");
1229 return 0;
1231 if (tclass & TYPE_RESTRICT) {
1232 if (!restricted_binop(op, t)) {
1233 warning(expr->pos, "bad assignment (%s) to %s",
1234 show_special(op), show_typename(t));
1235 expr->right = cast_to(expr->right, target);
1236 return 0;
1238 /* allowed assignments unfoul */
1239 if (sclass & TYPE_FOULED && unfoul(s) == t)
1240 goto Cast;
1241 if (!restricted_value(expr->right, t))
1242 return 1;
1243 } else if (!(sclass & TYPE_RESTRICT))
1244 goto Cast;
1245 /* source and target would better be identical restricted */
1246 if (t == s)
1247 return 1;
1248 warning(expr->pos, "invalid assignment: %s", show_special(op));
1249 info(expr->pos, " left side has type %s", show_typename(t));
1250 info(expr->pos, " right side has type %s", show_typename(s));
1251 expr->right = cast_to(expr->right, target);
1252 return 0;
1254 if (tclass == TYPE_PTR && is_int(sclass)) {
1255 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1256 unrestrict(expr->right, sclass, &s);
1257 evaluate_ptr_add(expr, s);
1258 return 1;
1260 expression_error(expr, "invalid pointer assignment");
1261 return 0;
1264 expression_error(expr, "invalid assignment");
1265 return 0;
1267 Cast:
1268 expr->right = cast_to(expr->right, target);
1269 return 1;
1272 static int whitelist_pointers(struct symbol *t1, struct symbol *t2)
1274 if (t1 == t2)
1275 return 0; /* yes, 0 - we don't want a cast_to here */
1276 if (t1 == &void_ctype)
1277 return 1;
1278 if (t2 == &void_ctype)
1279 return 1;
1280 if (classify_type(t1, &t1) != TYPE_NUM)
1281 return 0;
1282 if (classify_type(t2, &t2) != TYPE_NUM)
1283 return 0;
1284 if (t1 == t2)
1285 return 1;
1286 if (t1->ctype.modifiers & t2->ctype.modifiers & MOD_CHAR)
1287 return 1;
1288 if ((t1->ctype.modifiers ^ t2->ctype.modifiers) & MOD_SIZE)
1289 return 0;
1290 return !Wtypesign;
1293 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1294 struct expression **rp, const char *where)
1296 const char *typediff;
1297 struct symbol *source = degenerate(*rp);
1298 struct symbol *t, *s;
1299 int tclass = classify_type(target, &t);
1300 int sclass = classify_type(source, &s);
1302 if (tclass & sclass & TYPE_NUM) {
1303 if (tclass & TYPE_RESTRICT) {
1304 /* allowed assignments unfoul */
1305 if (sclass & TYPE_FOULED && unfoul(s) == t)
1306 goto Cast;
1307 if (!restricted_value(*rp, target))
1308 return 1;
1309 if (s == t)
1310 return 1;
1311 } else if (!(sclass & TYPE_RESTRICT))
1312 goto Cast;
1313 typediff = "different base types";
1314 goto Err;
1317 if (tclass == TYPE_PTR) {
1318 unsigned long mod1, mod2;
1319 struct symbol *b1, *b2;
1320 // NULL pointer is always OK
1321 int is_null = is_null_pointer_constant(*rp);
1322 if (is_null) {
1323 if (is_null == 2)
1324 bad_null(*rp);
1325 goto Cast;
1327 if (!(sclass & TYPE_PTR)) {
1328 typediff = "different base types";
1329 goto Err;
1331 b1 = examine_pointer_target(t);
1332 b2 = examine_pointer_target(s);
1333 mod1 = target_qualifiers(t);
1334 mod2 = target_qualifiers(s);
1335 if (whitelist_pointers(b1, b2)) {
1337 * assignments to/from void * are OK, provided that
1338 * we do not remove qualifiers from pointed to [C]
1339 * or mix address spaces [sparse].
1341 if (t->ctype.as != s->ctype.as) {
1342 typediff = "different address spaces";
1343 goto Err;
1345 if (mod2 & ~mod1) {
1346 typediff = "different modifiers";
1347 goto Err;
1349 goto Cast;
1351 /* It's OK if the target is more volatile or const than the source */
1352 typediff = type_difference(&t->ctype, &s->ctype, 0, mod1);
1353 if (typediff)
1354 goto Err;
1355 return 1;
1358 if ((tclass & TYPE_COMPOUND) && s == t)
1359 return 1;
1361 if (tclass & TYPE_NUM) {
1362 /* XXX: need to turn into comparison with NULL */
1363 if (t == &bool_ctype && (sclass & TYPE_PTR))
1364 goto Cast;
1365 typediff = "different base types";
1366 goto Err;
1368 typediff = "invalid types";
1370 Err:
1371 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1372 info(expr->pos, " expected %s", show_typename(target));
1373 info(expr->pos, " got %s", show_typename(source));
1374 *rp = cast_to(*rp, target);
1375 return 0;
1376 Cast:
1377 *rp = cast_to(*rp, target);
1378 return 1;
1381 static void mark_assigned(struct expression *expr)
1383 struct symbol *sym;
1385 if (!expr)
1386 return;
1387 switch (expr->type) {
1388 case EXPR_SYMBOL:
1389 sym = expr->symbol;
1390 if (!sym)
1391 return;
1392 if (sym->type != SYM_NODE)
1393 return;
1394 sym->ctype.modifiers |= MOD_ASSIGNED;
1395 return;
1397 case EXPR_BINOP:
1398 mark_assigned(expr->left);
1399 mark_assigned(expr->right);
1400 return;
1401 case EXPR_CAST:
1402 case EXPR_FORCE_CAST:
1403 mark_assigned(expr->cast_expression);
1404 return;
1405 case EXPR_SLICE:
1406 mark_assigned(expr->base);
1407 return;
1408 default:
1409 /* Hmm? */
1410 return;
1414 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1416 if (type->ctype.modifiers & MOD_CONST)
1417 expression_error(left, "assignment to const expression");
1419 /* We know left is an lvalue, so it's a "preop-*" */
1420 mark_assigned(left->unop);
1423 static struct symbol *evaluate_assignment(struct expression *expr)
1425 struct expression *left = expr->left;
1426 struct expression *where = expr;
1427 struct symbol *ltype;
1429 if (!lvalue_expression(left)) {
1430 expression_error(expr, "not an lvalue");
1431 return NULL;
1434 ltype = left->ctype;
1436 if (expr->op != '=') {
1437 if (!evaluate_assign_op(expr))
1438 return NULL;
1439 } else {
1440 if (!compatible_assignment_types(where, ltype, &expr->right, "assignment"))
1441 return NULL;
1444 evaluate_assign_to(left, ltype);
1446 expr->ctype = ltype;
1447 return ltype;
1450 static void examine_fn_arguments(struct symbol *fn)
1452 struct symbol *s;
1454 FOR_EACH_PTR(fn->arguments, s) {
1455 struct symbol *arg = evaluate_symbol(s);
1456 /* Array/function arguments silently degenerate into pointers */
1457 if (arg) {
1458 struct symbol *ptr;
1459 switch(arg->type) {
1460 case SYM_ARRAY:
1461 case SYM_FN:
1462 ptr = alloc_symbol(s->pos, SYM_PTR);
1463 if (arg->type == SYM_ARRAY)
1464 ptr->ctype = arg->ctype;
1465 else
1466 ptr->ctype.base_type = arg;
1467 ptr->ctype.as |= s->ctype.as;
1468 ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1470 s->ctype.base_type = ptr;
1471 s->ctype.as = 0;
1472 s->ctype.modifiers &= ~MOD_PTRINHERIT;
1473 s->bit_size = 0;
1474 s->examined = 0;
1475 examine_symbol_type(s);
1476 break;
1477 default:
1478 /* nothing */
1479 break;
1482 } END_FOR_EACH_PTR(s);
1485 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1487 /* Take the modifiers of the pointer, and apply them to the member */
1488 mod |= sym->ctype.modifiers;
1489 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1490 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1491 *newsym = *sym;
1492 newsym->ctype.as = as;
1493 newsym->ctype.modifiers = mod;
1494 sym = newsym;
1496 return sym;
1499 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1501 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1502 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1504 node->ctype.base_type = ptr;
1505 ptr->bit_size = bits_in_pointer;
1506 ptr->ctype.alignment = pointer_alignment;
1508 node->bit_size = bits_in_pointer;
1509 node->ctype.alignment = pointer_alignment;
1511 access_symbol(sym);
1512 if (sym->ctype.modifiers & MOD_REGISTER) {
1513 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1514 sym->ctype.modifiers &= ~MOD_REGISTER;
1516 if (sym->type == SYM_NODE) {
1517 ptr->ctype.as |= sym->ctype.as;
1518 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1519 sym = sym->ctype.base_type;
1521 if (degenerate && sym->type == SYM_ARRAY) {
1522 ptr->ctype.as |= sym->ctype.as;
1523 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1524 sym = sym->ctype.base_type;
1526 ptr->ctype.base_type = sym;
1528 return node;
1531 /* Arrays degenerate into pointers on pointer arithmetic */
1532 static struct symbol *degenerate(struct expression *expr)
1534 struct symbol *ctype, *base;
1536 if (!expr)
1537 return NULL;
1538 ctype = expr->ctype;
1539 if (!ctype)
1540 return NULL;
1541 base = examine_symbol_type(ctype);
1542 if (ctype->type == SYM_NODE)
1543 base = ctype->ctype.base_type;
1545 * Arrays degenerate into pointers to the entries, while
1546 * functions degenerate into pointers to themselves.
1547 * If array was part of non-lvalue compound, we create a copy
1548 * of that compound first and then act as if we were dealing with
1549 * the corresponding field in there.
1551 switch (base->type) {
1552 case SYM_ARRAY:
1553 if (expr->type == EXPR_SLICE) {
1554 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1555 struct expression *e0, *e1, *e2, *e3, *e4;
1557 a->ctype.base_type = expr->base->ctype;
1558 a->bit_size = expr->base->ctype->bit_size;
1559 a->array_size = expr->base->ctype->array_size;
1561 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1562 e0->symbol = a;
1563 e0->ctype = &lazy_ptr_ctype;
1565 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1566 e1->unop = e0;
1567 e1->op = '*';
1568 e1->ctype = expr->base->ctype; /* XXX */
1570 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1571 e2->left = e1;
1572 e2->right = expr->base;
1573 e2->op = '=';
1574 e2->ctype = expr->base->ctype;
1576 if (expr->r_bitpos) {
1577 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1578 e3->op = '+';
1579 e3->left = e0;
1580 e3->right = alloc_const_expression(expr->pos,
1581 bits_to_bytes(expr->r_bitpos));
1582 e3->ctype = &lazy_ptr_ctype;
1583 } else {
1584 e3 = e0;
1587 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1588 e4->left = e2;
1589 e4->right = e3;
1590 e4->ctype = &lazy_ptr_ctype;
1592 expr->unop = e4;
1593 expr->type = EXPR_PREOP;
1594 expr->op = '*';
1596 case SYM_FN:
1597 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1598 expression_error(expr, "strange non-value function or array");
1599 return &bad_ctype;
1601 *expr = *expr->unop;
1602 ctype = create_pointer(expr, ctype, 1);
1603 expr->ctype = ctype;
1604 default:
1605 /* nothing */;
1607 return ctype;
1610 static struct symbol *evaluate_addressof(struct expression *expr)
1612 struct expression *op = expr->unop;
1613 struct symbol *ctype;
1615 if (op->op != '*' || op->type != EXPR_PREOP) {
1616 expression_error(expr, "not addressable");
1617 return NULL;
1619 ctype = op->ctype;
1620 *expr = *op->unop;
1621 expr->flags = 0;
1623 if (expr->type == EXPR_SYMBOL) {
1624 struct symbol *sym = expr->symbol;
1625 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1629 * symbol expression evaluation is lazy about the type
1630 * of the sub-expression, so we may have to generate
1631 * the type here if so..
1633 if (expr->ctype == &lazy_ptr_ctype) {
1634 ctype = create_pointer(expr, ctype, 0);
1635 expr->ctype = ctype;
1637 return expr->ctype;
1641 static struct symbol *evaluate_dereference(struct expression *expr)
1643 struct expression *op = expr->unop;
1644 struct symbol *ctype = op->ctype, *node, *target;
1646 /* Simplify: *&(expr) => (expr) */
1647 if (op->type == EXPR_PREOP && op->op == '&') {
1648 *expr = *op->unop;
1649 expr->flags = 0;
1650 return expr->ctype;
1653 /* Dereferencing a node drops all the node information. */
1654 if (ctype->type == SYM_NODE)
1655 ctype = ctype->ctype.base_type;
1657 node = alloc_symbol(expr->pos, SYM_NODE);
1658 target = ctype->ctype.base_type;
1660 switch (ctype->type) {
1661 default:
1662 expression_error(expr, "cannot dereference this type");
1663 return NULL;
1664 case SYM_PTR:
1665 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1666 merge_type(node, ctype);
1667 break;
1669 case SYM_ARRAY:
1670 if (!lvalue_expression(op)) {
1671 expression_error(op, "non-lvalue array??");
1672 return NULL;
1675 /* Do the implied "addressof" on the array */
1676 *op = *op->unop;
1679 * When an array is dereferenced, we need to pick
1680 * up the attributes of the original node too..
1682 merge_type(node, op->ctype);
1683 merge_type(node, ctype);
1684 break;
1687 node->bit_size = target->bit_size;
1688 node->array_size = target->array_size;
1690 expr->ctype = node;
1691 return node;
1695 * Unary post-ops: x++ and x--
1697 static struct symbol *evaluate_postop(struct expression *expr)
1699 struct expression *op = expr->unop;
1700 struct symbol *ctype = op->ctype;
1701 int class = classify_type(ctype, &ctype);
1702 int multiply = 0;
1704 if (!class || class & TYPE_COMPOUND) {
1705 expression_error(expr, "need scalar for ++/--");
1706 return NULL;
1708 if (!lvalue_expression(expr->unop)) {
1709 expression_error(expr, "need lvalue expression for ++/--");
1710 return NULL;
1713 if ((class & TYPE_RESTRICT) && restricted_unop(expr->op, &ctype))
1714 unrestrict(expr, class, &ctype);
1716 if (class & TYPE_NUM) {
1717 multiply = 1;
1718 } else if (class == TYPE_PTR) {
1719 struct symbol *target = examine_pointer_target(ctype);
1720 if (!is_function(target))
1721 multiply = bits_to_bytes(target->bit_size);
1724 if (multiply) {
1725 evaluate_assign_to(op, op->ctype);
1726 expr->op_value = multiply;
1727 expr->ctype = ctype;
1728 return ctype;
1731 expression_error(expr, "bad argument type for ++/--");
1732 return NULL;
1735 static struct symbol *evaluate_sign(struct expression *expr)
1737 struct symbol *ctype = expr->unop->ctype;
1738 int class = classify_type(ctype, &ctype);
1739 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1740 expr->flags = 0;
1741 /* should be an arithmetic type */
1742 if (!(class & TYPE_NUM))
1743 return bad_expr_type(expr);
1744 if (class & TYPE_RESTRICT)
1745 goto Restr;
1746 Normal:
1747 if (!(class & TYPE_FLOAT)) {
1748 ctype = integer_promotion(ctype);
1749 expr->unop = cast_to(expr->unop, ctype);
1750 } else if (expr->op != '~') {
1751 /* no conversions needed */
1752 } else {
1753 return bad_expr_type(expr);
1755 if (expr->op == '+')
1756 *expr = *expr->unop;
1757 expr->ctype = ctype;
1758 return ctype;
1759 Restr:
1760 if (restricted_unop(expr->op, &ctype))
1761 unrestrict(expr, class, &ctype);
1762 goto Normal;
1765 static struct symbol *evaluate_preop(struct expression *expr)
1767 struct symbol *ctype = expr->unop->ctype;
1769 switch (expr->op) {
1770 case '(':
1771 *expr = *expr->unop;
1772 return ctype;
1774 case '+':
1775 case '-':
1776 case '~':
1777 return evaluate_sign(expr);
1779 case '*':
1780 return evaluate_dereference(expr);
1782 case '&':
1783 return evaluate_addressof(expr);
1785 case SPECIAL_INCREMENT:
1786 case SPECIAL_DECREMENT:
1788 * From a type evaluation standpoint the preops are
1789 * the same as the postops
1791 return evaluate_postop(expr);
1793 case '!':
1794 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1795 expr->flags = 0;
1796 if (is_safe_type(ctype))
1797 warning(expr->pos, "testing a 'safe expression'");
1798 if (is_float_type(ctype)) {
1799 struct expression *arg = expr->unop;
1800 expr->type = EXPR_COMPARE;
1801 expr->op = SPECIAL_EQUAL;
1802 expr->left = arg;
1803 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1804 expr->right->ctype = ctype;
1805 expr->right->fvalue = 0;
1806 } else if (is_fouled_type(ctype)) {
1807 warning(expr->pos, "%s degrades to integer",
1808 show_typename(ctype->ctype.base_type));
1810 /* the result is int [6.5.3.3(5)]*/
1811 ctype = &int_ctype;
1812 break;
1814 default:
1815 break;
1817 expr->ctype = ctype;
1818 return ctype;
1821 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1823 struct ptr_list *head = (struct ptr_list *)_list;
1824 struct ptr_list *list = head;
1826 if (!head)
1827 return NULL;
1828 do {
1829 int i;
1830 for (i = 0; i < list->nr; i++) {
1831 struct symbol *sym = (struct symbol *) list->list[i];
1832 if (sym->ident) {
1833 if (sym->ident != ident)
1834 continue;
1835 *offset = sym->offset;
1836 return sym;
1837 } else {
1838 struct symbol *ctype = sym->ctype.base_type;
1839 struct symbol *sub;
1840 if (!ctype)
1841 continue;
1842 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1843 continue;
1844 sub = find_identifier(ident, ctype->symbol_list, offset);
1845 if (!sub)
1846 continue;
1847 *offset += sym->offset;
1848 return sub;
1851 } while ((list = list->next) != head);
1852 return NULL;
1855 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1857 struct expression *add;
1860 * Create a new add-expression
1862 * NOTE! Even if we just add zero, we need a new node
1863 * for the member pointer, since it has a different
1864 * type than the original pointer. We could make that
1865 * be just a cast, but the fact is, a node is a node,
1866 * so we might as well just do the "add zero" here.
1868 add = alloc_expression(expr->pos, EXPR_BINOP);
1869 add->op = '+';
1870 add->left = expr;
1871 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1872 add->right->ctype = &int_ctype;
1873 add->right->value = offset;
1876 * The ctype of the pointer will be lazily evaluated if
1877 * we ever take the address of this member dereference..
1879 add->ctype = &lazy_ptr_ctype;
1880 return add;
1883 /* structure/union dereference */
1884 static struct symbol *evaluate_member_dereference(struct expression *expr)
1886 int offset;
1887 struct symbol *ctype, *member;
1888 struct expression *deref = expr->deref, *add;
1889 struct ident *ident = expr->member;
1890 unsigned int mod;
1891 int address_space;
1893 if (!evaluate_expression(deref))
1894 return NULL;
1895 if (!ident) {
1896 expression_error(expr, "bad member name");
1897 return NULL;
1900 ctype = deref->ctype;
1901 examine_symbol_type(ctype);
1902 address_space = ctype->ctype.as;
1903 mod = ctype->ctype.modifiers;
1904 if (ctype->type == SYM_NODE) {
1905 ctype = ctype->ctype.base_type;
1906 address_space |= ctype->ctype.as;
1907 mod |= ctype->ctype.modifiers;
1909 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1910 expression_error(expr, "expected structure or union");
1911 return NULL;
1913 offset = 0;
1914 member = find_identifier(ident, ctype->symbol_list, &offset);
1915 if (!member) {
1916 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1917 const char *name = "<unnamed>";
1918 int namelen = 9;
1919 if (ctype->ident) {
1920 name = ctype->ident->name;
1921 namelen = ctype->ident->len;
1923 if (ctype->symbol_list)
1924 expression_error(expr, "no member '%s' in %s %.*s",
1925 show_ident(ident), type, namelen, name);
1926 else
1927 expression_error(expr, "using member '%s' in "
1928 "incomplete %s %.*s", show_ident(ident),
1929 type, namelen, name);
1930 return NULL;
1934 * The member needs to take on the address space and modifiers of
1935 * the "parent" type.
1937 member = convert_to_as_mod(member, address_space, mod);
1938 ctype = get_base_type(member);
1940 if (!lvalue_expression(deref)) {
1941 if (deref->type != EXPR_SLICE) {
1942 expr->base = deref;
1943 expr->r_bitpos = 0;
1944 } else {
1945 expr->base = deref->base;
1946 expr->r_bitpos = deref->r_bitpos;
1948 expr->r_bitpos += bytes_to_bits(offset);
1949 expr->type = EXPR_SLICE;
1950 expr->r_nrbits = member->bit_size;
1951 expr->r_bitpos += member->bit_offset;
1952 expr->ctype = member;
1953 return member;
1956 deref = deref->unop;
1957 expr->deref = deref;
1959 add = evaluate_offset(deref, offset);
1960 expr->type = EXPR_PREOP;
1961 expr->op = '*';
1962 expr->unop = add;
1964 expr->ctype = member;
1965 return member;
1968 static int is_promoted(struct expression *expr)
1970 while (1) {
1971 switch (expr->type) {
1972 case EXPR_BINOP:
1973 case EXPR_SELECT:
1974 case EXPR_CONDITIONAL:
1975 return 1;
1976 case EXPR_COMMA:
1977 expr = expr->right;
1978 continue;
1979 case EXPR_PREOP:
1980 switch (expr->op) {
1981 case '(':
1982 expr = expr->unop;
1983 continue;
1984 case '+':
1985 case '-':
1986 case '~':
1987 return 1;
1988 default:
1989 return 0;
1991 default:
1992 return 0;
1998 static struct symbol *evaluate_cast(struct expression *);
2000 static struct symbol *evaluate_type_information(struct expression *expr)
2002 struct symbol *sym = expr->cast_type;
2003 if (!sym) {
2004 sym = evaluate_expression(expr->cast_expression);
2005 if (!sym)
2006 return NULL;
2008 * Expressions of restricted types will possibly get
2009 * promoted - check that here
2011 if (is_restricted_type(sym)) {
2012 if (sym->bit_size < bits_in_int && is_promoted(expr))
2013 sym = &int_ctype;
2014 } else if (is_fouled_type(sym)) {
2015 sym = &int_ctype;
2018 examine_symbol_type(sym);
2019 if (is_bitfield_type(sym)) {
2020 expression_error(expr, "trying to examine bitfield type");
2021 return NULL;
2023 return sym;
2026 static struct symbol *evaluate_sizeof(struct expression *expr)
2028 struct symbol *type;
2029 int size;
2031 type = evaluate_type_information(expr);
2032 if (!type)
2033 return NULL;
2035 size = type->bit_size;
2037 if (size < 0 && is_void_type(type)) {
2038 warning(expr->pos, "expression using sizeof(void)");
2039 size = bits_in_char;
2042 if (size == 1 && is_bool_type(type)) {
2043 warning(expr->pos, "expression using sizeof bool");
2044 size = bits_in_char;
2047 if (is_function(type->ctype.base_type)) {
2048 warning(expr->pos, "expression using sizeof on a function");
2049 size = bits_in_char;
2052 if ((size < 0) || (size & (bits_in_char - 1)))
2053 expression_error(expr, "cannot size expression");
2055 expr->type = EXPR_VALUE;
2056 expr->value = bits_to_bytes(size);
2057 expr->taint = 0;
2058 expr->ctype = size_t_ctype;
2059 return size_t_ctype;
2062 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
2064 struct symbol *type;
2065 int size;
2067 type = evaluate_type_information(expr);
2068 if (!type)
2069 return NULL;
2071 if (type->type == SYM_NODE)
2072 type = type->ctype.base_type;
2073 if (!type)
2074 return NULL;
2075 switch (type->type) {
2076 case SYM_ARRAY:
2077 break;
2078 case SYM_PTR:
2079 type = get_base_type(type);
2080 if (type)
2081 break;
2082 default:
2083 expression_error(expr, "expected pointer expression");
2084 return NULL;
2086 size = type->bit_size;
2087 if (size & (bits_in_char-1))
2088 size = 0;
2089 expr->type = EXPR_VALUE;
2090 expr->value = bits_to_bytes(size);
2091 expr->taint = 0;
2092 expr->ctype = size_t_ctype;
2093 return size_t_ctype;
2096 static struct symbol *evaluate_alignof(struct expression *expr)
2098 struct symbol *type;
2100 type = evaluate_type_information(expr);
2101 if (!type)
2102 return NULL;
2104 expr->type = EXPR_VALUE;
2105 expr->value = type->ctype.alignment;
2106 expr->taint = 0;
2107 expr->ctype = size_t_ctype;
2108 return size_t_ctype;
2111 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
2113 struct expression *expr;
2114 struct symbol_list *argument_types = fn->arguments;
2115 struct symbol *argtype;
2116 int i = 1;
2118 PREPARE_PTR_LIST(argument_types, argtype);
2119 FOR_EACH_PTR (head, expr) {
2120 struct expression **p = THIS_ADDRESS(expr);
2121 struct symbol *ctype, *target;
2122 ctype = evaluate_expression(expr);
2124 if (!ctype)
2125 return 0;
2127 target = argtype;
2128 if (!target) {
2129 struct symbol *type;
2130 int class = classify_type(ctype, &type);
2131 if (is_int(class)) {
2132 *p = cast_to(expr, integer_promotion(type));
2133 } else if (class & TYPE_FLOAT) {
2134 unsigned long mod = type->ctype.modifiers;
2135 if (!(mod & (MOD_LONG_ALL)))
2136 *p = cast_to(expr, &double_ctype);
2137 } else if (class & TYPE_PTR) {
2138 if (expr->ctype == &null_ctype)
2139 *p = cast_to(expr, &ptr_ctype);
2140 else
2141 degenerate(expr);
2143 } else if (!target->forced_arg){
2144 static char where[30];
2145 examine_symbol_type(target);
2146 sprintf(where, "argument %d", i);
2147 compatible_assignment_types(expr, target, p, where);
2150 i++;
2151 NEXT_PTR_LIST(argtype);
2152 } END_FOR_EACH_PTR(expr);
2153 FINISH_PTR_LIST(argtype);
2154 return 1;
2157 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
2159 struct symbol *sym;
2161 FOR_EACH_PTR(ctype->symbol_list, sym) {
2162 if (sym->ident == ident)
2163 return sym;
2164 } END_FOR_EACH_PTR(sym);
2165 return NULL;
2168 static void convert_index(struct expression *e)
2170 struct expression *child = e->idx_expression;
2171 unsigned from = e->idx_from;
2172 unsigned to = e->idx_to + 1;
2173 e->type = EXPR_POS;
2174 e->init_offset = from * bits_to_bytes(e->ctype->bit_size);
2175 e->init_nr = to - from;
2176 e->init_expr = child;
2179 static void convert_ident(struct expression *e)
2181 struct expression *child = e->ident_expression;
2182 struct symbol *sym = e->field;
2183 e->type = EXPR_POS;
2184 e->init_offset = sym->offset;
2185 e->init_nr = 1;
2186 e->init_expr = child;
2189 static void convert_designators(struct expression *e)
2191 while (e) {
2192 if (e->type == EXPR_INDEX)
2193 convert_index(e);
2194 else if (e->type == EXPR_IDENTIFIER)
2195 convert_ident(e);
2196 else
2197 break;
2198 e = e->init_expr;
2202 static void excess(struct expression *e, const char *s)
2204 warning(e->pos, "excessive elements in %s initializer", s);
2208 * implicit designator for the first element
2210 static struct expression *first_subobject(struct symbol *ctype, int class,
2211 struct expression **v)
2213 struct expression *e = *v, *new;
2215 if (ctype->type == SYM_NODE)
2216 ctype = ctype->ctype.base_type;
2218 if (class & TYPE_PTR) { /* array */
2219 if (!ctype->bit_size)
2220 return NULL;
2221 new = alloc_expression(e->pos, EXPR_INDEX);
2222 new->idx_expression = e;
2223 new->ctype = ctype->ctype.base_type;
2224 } else {
2225 struct symbol *field, *p;
2226 PREPARE_PTR_LIST(ctype->symbol_list, p);
2227 while (p && !p->ident && is_bitfield_type(p))
2228 NEXT_PTR_LIST(p);
2229 field = p;
2230 FINISH_PTR_LIST(p);
2231 if (!field)
2232 return NULL;
2233 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2234 new->ident_expression = e;
2235 new->field = new->ctype = field;
2237 *v = new;
2238 return new;
2242 * sanity-check explicit designators; return the innermost one or NULL
2243 * in case of error. Assign types.
2245 static struct expression *check_designators(struct expression *e,
2246 struct symbol *ctype)
2248 struct expression *last = NULL;
2249 const char *err;
2250 while (1) {
2251 if (ctype->type == SYM_NODE)
2252 ctype = ctype->ctype.base_type;
2253 if (e->type == EXPR_INDEX) {
2254 struct symbol *type;
2255 if (ctype->type != SYM_ARRAY) {
2256 err = "array index in non-array";
2257 break;
2259 type = ctype->ctype.base_type;
2260 if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2261 unsigned offset = e->idx_to * type->bit_size;
2262 if (offset >= ctype->bit_size) {
2263 err = "index out of bounds in";
2264 break;
2267 e->ctype = ctype = type;
2268 ctype = type;
2269 last = e;
2270 if (!e->idx_expression) {
2271 err = "invalid";
2272 break;
2274 e = e->idx_expression;
2275 } else if (e->type == EXPR_IDENTIFIER) {
2276 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2277 err = "field name not in struct or union";
2278 break;
2280 ctype = find_struct_ident(ctype, e->expr_ident);
2281 if (!ctype) {
2282 err = "unknown field name in";
2283 break;
2285 e->field = e->ctype = ctype;
2286 last = e;
2287 if (!e->ident_expression) {
2288 err = "invalid";
2289 break;
2291 e = e->ident_expression;
2292 } else if (e->type == EXPR_POS) {
2293 err = "internal front-end error: EXPR_POS in";
2294 break;
2295 } else
2296 return last;
2298 expression_error(e, "%s initializer", err);
2299 return NULL;
2303 * choose the next subobject to initialize.
2305 * Get designators for next element, switch old ones to EXPR_POS.
2306 * Return the resulting expression or NULL if we'd run out of subobjects.
2307 * The innermost designator is returned in *v. Designators in old
2308 * are assumed to be already sanity-checked.
2310 static struct expression *next_designators(struct expression *old,
2311 struct symbol *ctype,
2312 struct expression *e, struct expression **v)
2314 struct expression *new = NULL;
2316 if (!old)
2317 return NULL;
2318 if (old->type == EXPR_INDEX) {
2319 struct expression *copy;
2320 unsigned n;
2322 copy = next_designators(old->idx_expression,
2323 old->ctype, e, v);
2324 if (!copy) {
2325 n = old->idx_to + 1;
2326 if (n * old->ctype->bit_size == ctype->bit_size) {
2327 convert_index(old);
2328 return NULL;
2330 copy = e;
2331 *v = new = alloc_expression(e->pos, EXPR_INDEX);
2332 } else {
2333 n = old->idx_to;
2334 new = alloc_expression(e->pos, EXPR_INDEX);
2337 new->idx_from = new->idx_to = n;
2338 new->idx_expression = copy;
2339 new->ctype = old->ctype;
2340 convert_index(old);
2341 } else if (old->type == EXPR_IDENTIFIER) {
2342 struct expression *copy;
2343 struct symbol *field;
2345 copy = next_designators(old->ident_expression,
2346 old->ctype, e, v);
2347 if (!copy) {
2348 field = old->field->next_subobject;
2349 if (!field) {
2350 convert_ident(old);
2351 return NULL;
2353 copy = e;
2354 *v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2355 } else {
2356 field = old->field;
2357 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2360 new->field = field;
2361 new->expr_ident = field->ident;
2362 new->ident_expression = copy;
2363 new->ctype = field;
2364 convert_ident(old);
2366 return new;
2369 static int handle_simple_initializer(struct expression **ep, int nested,
2370 int class, struct symbol *ctype);
2373 * deal with traversing subobjects [6.7.8(17,18,20)]
2375 static void handle_list_initializer(struct expression *expr,
2376 int class, struct symbol *ctype)
2378 struct expression *e, *last = NULL, *top = NULL, *next;
2379 int jumped = 0;
2381 FOR_EACH_PTR(expr->expr_list, e) {
2382 struct expression **v;
2383 struct symbol *type;
2384 int lclass;
2386 if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2387 struct symbol *struct_sym;
2388 if (!top) {
2389 top = e;
2390 last = first_subobject(ctype, class, &top);
2391 } else {
2392 last = next_designators(last, ctype, e, &top);
2394 if (!last) {
2395 excess(e, class & TYPE_PTR ? "array" :
2396 "struct or union");
2397 DELETE_CURRENT_PTR(e);
2398 continue;
2400 struct_sym = ctype->type == SYM_NODE ? ctype->ctype.base_type : ctype;
2401 if (Wdesignated_init && struct_sym->designated_init)
2402 warning(e->pos, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2403 ctype->ident ? "in initializer for " : "",
2404 ctype->ident ? ctype->ident->len : 0,
2405 ctype->ident ? ctype->ident->name : "",
2406 ctype->ident ? ": " : "",
2407 get_type_name(struct_sym->type),
2408 show_ident(struct_sym->ident));
2409 if (jumped) {
2410 warning(e->pos, "advancing past deep designator");
2411 jumped = 0;
2413 REPLACE_CURRENT_PTR(e, last);
2414 } else {
2415 next = check_designators(e, ctype);
2416 if (!next) {
2417 DELETE_CURRENT_PTR(e);
2418 continue;
2420 top = next;
2421 /* deeper than one designator? */
2422 jumped = top != e;
2423 convert_designators(last);
2424 last = e;
2427 found:
2428 lclass = classify_type(top->ctype, &type);
2429 if (top->type == EXPR_INDEX)
2430 v = &top->idx_expression;
2431 else
2432 v = &top->ident_expression;
2434 if (handle_simple_initializer(v, 1, lclass, top->ctype))
2435 continue;
2437 if (!(lclass & TYPE_COMPOUND)) {
2438 warning(e->pos, "bogus scalar initializer");
2439 DELETE_CURRENT_PTR(e);
2440 continue;
2443 next = first_subobject(type, lclass, v);
2444 if (next) {
2445 warning(e->pos, "missing braces around initializer");
2446 top = next;
2447 goto found;
2450 DELETE_CURRENT_PTR(e);
2451 excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2453 } END_FOR_EACH_PTR(e);
2455 convert_designators(last);
2456 expr->ctype = ctype;
2459 static int is_string_literal(struct expression **v)
2461 struct expression *e = *v;
2462 while (e && e->type == EXPR_PREOP && e->op == '(')
2463 e = e->unop;
2464 if (!e || e->type != EXPR_STRING)
2465 return 0;
2466 if (e != *v && Wparen_string)
2467 warning(e->pos,
2468 "array initialized from parenthesized string constant");
2469 *v = e;
2470 return 1;
2474 * We want a normal expression, possibly in one layer of braces. Warn
2475 * if the latter happens inside a list (it's legal, but likely to be
2476 * an effect of screwup). In case of anything not legal, we are definitely
2477 * having an effect of screwup, so just fail and let the caller warn.
2479 static struct expression *handle_scalar(struct expression *e, int nested)
2481 struct expression *v = NULL, *p;
2482 int count = 0;
2484 /* normal case */
2485 if (e->type != EXPR_INITIALIZER)
2486 return e;
2488 FOR_EACH_PTR(e->expr_list, p) {
2489 if (!v)
2490 v = p;
2491 count++;
2492 } END_FOR_EACH_PTR(p);
2493 if (count != 1)
2494 return NULL;
2495 switch(v->type) {
2496 case EXPR_INITIALIZER:
2497 case EXPR_INDEX:
2498 case EXPR_IDENTIFIER:
2499 return NULL;
2500 default:
2501 break;
2503 if (nested)
2504 warning(e->pos, "braces around scalar initializer");
2505 return v;
2509 * deal with the cases that don't care about subobjects:
2510 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2511 * character array <- string literal, possibly in braces [6.7.8(14)]
2512 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2513 * compound type <- initializer list in braces [6.7.8(16)]
2514 * The last one punts to handle_list_initializer() which, in turn will call
2515 * us for individual elements of the list.
2517 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2518 * the lack of support of wide char stuff in general.
2520 * One note: we need to take care not to evaluate a string literal until
2521 * we know that we *will* handle it right here. Otherwise we would screw
2522 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2523 * { "string", ...} - we need to preserve that string literal recognizable
2524 * until we dig into the inner struct.
2526 static int handle_simple_initializer(struct expression **ep, int nested,
2527 int class, struct symbol *ctype)
2529 int is_string = is_string_type(ctype);
2530 struct expression *e = *ep, *p;
2531 struct symbol *type;
2533 if (!e)
2534 return 0;
2536 /* scalar */
2537 if (!(class & TYPE_COMPOUND)) {
2538 e = handle_scalar(e, nested);
2539 if (!e)
2540 return 0;
2541 *ep = e;
2542 if (!evaluate_expression(e))
2543 return 1;
2544 compatible_assignment_types(e, ctype, ep, "initializer");
2545 return 1;
2549 * sublist; either a string, or we dig in; the latter will deal with
2550 * pathologies, so we don't need anything fancy here.
2552 if (e->type == EXPR_INITIALIZER) {
2553 if (is_string) {
2554 struct expression *v = NULL;
2555 int count = 0;
2557 FOR_EACH_PTR(e->expr_list, p) {
2558 if (!v)
2559 v = p;
2560 count++;
2561 } END_FOR_EACH_PTR(p);
2562 if (count == 1 && is_string_literal(&v)) {
2563 *ep = e = v;
2564 goto String;
2567 handle_list_initializer(e, class, ctype);
2568 return 1;
2571 /* string */
2572 if (is_string_literal(&e)) {
2573 /* either we are doing array of char, or we'll have to dig in */
2574 if (is_string) {
2575 *ep = e;
2576 goto String;
2578 return 0;
2580 /* struct or union can be initialized by compatible */
2581 if (class != TYPE_COMPOUND)
2582 return 0;
2583 type = evaluate_expression(e);
2584 if (!type)
2585 return 0;
2586 if (ctype->type == SYM_NODE)
2587 ctype = ctype->ctype.base_type;
2588 if (type->type == SYM_NODE)
2589 type = type->ctype.base_type;
2590 if (ctype == type)
2591 return 1;
2592 return 0;
2594 String:
2595 p = alloc_expression(e->pos, EXPR_STRING);
2596 *p = *e;
2597 type = evaluate_expression(p);
2598 if (ctype->bit_size != -1) {
2599 if (ctype->bit_size + bits_in_char < type->bit_size)
2600 warning(e->pos,
2601 "too long initializer-string for array of char");
2602 else if (Winit_cstring && ctype->bit_size + bits_in_char == type->bit_size) {
2603 warning(e->pos,
2604 "too long initializer-string for array of char(no space for nul char)");
2607 *ep = p;
2608 return 1;
2611 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2613 struct symbol *type;
2614 int class = classify_type(ctype, &type);
2615 if (!handle_simple_initializer(ep, 0, class, ctype))
2616 expression_error(*ep, "invalid initializer");
2619 static struct symbol *evaluate_cast(struct expression *expr)
2621 struct expression *target = expr->cast_expression;
2622 struct symbol *ctype;
2623 struct symbol *t1, *t2;
2624 int class1, class2;
2625 int as1 = 0, as2 = 0;
2627 if (!target)
2628 return NULL;
2631 * Special case: a cast can be followed by an
2632 * initializer, in which case we need to pass
2633 * the type value down to that initializer rather
2634 * than trying to evaluate it as an expression
2636 * A more complex case is when the initializer is
2637 * dereferenced as part of a post-fix expression.
2638 * We need to produce an expression that can be dereferenced.
2640 if (target->type == EXPR_INITIALIZER) {
2641 struct symbol *sym = expr->cast_type;
2642 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2644 sym->initializer = target;
2645 evaluate_symbol(sym);
2647 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2648 addr->symbol = sym;
2650 expr->type = EXPR_PREOP;
2651 expr->op = '*';
2652 expr->unop = addr;
2653 expr->ctype = sym;
2655 return sym;
2658 ctype = examine_symbol_type(expr->cast_type);
2659 expr->ctype = ctype;
2660 expr->cast_type = ctype;
2662 evaluate_expression(target);
2663 degenerate(target);
2665 class1 = classify_type(ctype, &t1);
2667 /* cast to non-integer type -> not an integer constant expression */
2668 if (!is_int(class1))
2669 expr->flags = 0;
2670 /* if argument turns out to be not an integer constant expression *and*
2671 it was not a floating literal to start with -> too bad */
2672 else if (expr->flags == Int_const_expr &&
2673 !(target->flags & Int_const_expr))
2674 expr->flags = 0;
2676 * You can always throw a value away by casting to
2677 * "void" - that's an implicit "force". Note that
2678 * the same is _not_ true of "void *".
2680 if (t1 == &void_ctype)
2681 goto out;
2683 if (class1 & (TYPE_COMPOUND | TYPE_FN))
2684 warning(expr->pos, "cast to non-scalar");
2686 t2 = target->ctype;
2687 if (!t2) {
2688 expression_error(expr, "cast from unknown type");
2689 goto out;
2691 class2 = classify_type(t2, &t2);
2693 if (class2 & TYPE_COMPOUND)
2694 warning(expr->pos, "cast from non-scalar");
2696 if (expr->type == EXPR_FORCE_CAST)
2697 goto out;
2699 /* allowed cast unfouls */
2700 if (class2 & TYPE_FOULED)
2701 t2 = unfoul(t2);
2703 if (t1 != t2) {
2704 if (class1 & TYPE_RESTRICT)
2705 warning(expr->pos, "cast to %s",
2706 show_typename(t1));
2707 if (class2 & TYPE_RESTRICT)
2708 warning(expr->pos, "cast from %s",
2709 show_typename(t2));
2712 if (t1 == &ulong_ctype)
2713 as1 = -1;
2714 else if (class1 == TYPE_PTR) {
2715 examine_pointer_target(t1);
2716 as1 = t1->ctype.as;
2719 if (t2 == &ulong_ctype)
2720 as2 = -1;
2721 else if (class2 == TYPE_PTR) {
2722 examine_pointer_target(t2);
2723 as2 = t2->ctype.as;
2726 if (!as1 && as2 > 0)
2727 warning(expr->pos, "cast removes address space of expression");
2728 if (as1 > 0 && as2 > 0 && as1 != as2)
2729 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2730 if (as1 > 0 && !as2 &&
2731 !is_null_pointer_constant(target) && Wcast_to_as)
2732 warning(expr->pos,
2733 "cast adds address space to expression (<asn:%d>)", as1);
2735 if (!(t1->ctype.modifiers & MOD_PTRINHERIT) && class1 == TYPE_PTR &&
2736 !as1 && (target->flags & Int_const_expr)) {
2737 if (t1->ctype.base_type == &void_ctype) {
2738 if (is_zero_constant(target)) {
2739 /* NULL */
2740 expr->type = EXPR_VALUE;
2741 expr->ctype = &null_ctype;
2742 expr->value = 0;
2743 return ctype;
2747 out:
2748 return ctype;
2752 * Evaluate a call expression with a symbol. This
2753 * should expand inline functions, and evaluate
2754 * builtins.
2756 static int evaluate_symbol_call(struct expression *expr)
2758 struct expression *fn = expr->fn;
2759 struct symbol *ctype = fn->ctype;
2761 if (fn->type != EXPR_PREOP)
2762 return 0;
2764 if (ctype->op && ctype->op->evaluate)
2765 return ctype->op->evaluate(expr);
2767 if (ctype->ctype.modifiers & MOD_INLINE) {
2768 int ret;
2769 struct symbol *curr = current_fn;
2771 if (ctype->definition)
2772 ctype = ctype->definition;
2774 current_fn = ctype->ctype.base_type;
2776 ret = inline_function(expr, ctype);
2778 /* restore the old function */
2779 current_fn = curr;
2780 return ret;
2783 return 0;
2786 static struct symbol *evaluate_call(struct expression *expr)
2788 int args, fnargs;
2789 struct symbol *ctype, *sym;
2790 struct expression *fn = expr->fn;
2791 struct expression_list *arglist = expr->args;
2793 if (!evaluate_expression(fn))
2794 return NULL;
2795 sym = ctype = fn->ctype;
2796 if (ctype->type == SYM_NODE)
2797 ctype = ctype->ctype.base_type;
2798 if (ctype->type == SYM_PTR)
2799 ctype = get_base_type(ctype);
2801 if (ctype->type != SYM_FN) {
2802 struct expression *arg;
2803 expression_error(expr, "not a function %s",
2804 show_ident(sym->ident));
2805 /* do typechecking in arguments */
2806 FOR_EACH_PTR (arglist, arg) {
2807 evaluate_expression(arg);
2808 } END_FOR_EACH_PTR(arg);
2809 return NULL;
2812 examine_fn_arguments(ctype);
2813 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
2814 sym->op && sym->op->args) {
2815 if (!sym->op->args(expr))
2816 return NULL;
2817 } else {
2818 if (!evaluate_arguments(sym, ctype, arglist))
2819 return NULL;
2820 args = expression_list_size(expr->args);
2821 fnargs = symbol_list_size(ctype->arguments);
2822 if (args < fnargs)
2823 expression_error(expr,
2824 "not enough arguments for function %s",
2825 show_ident(sym->ident));
2826 if (args > fnargs && !ctype->variadic)
2827 expression_error(expr,
2828 "too many arguments for function %s",
2829 show_ident(sym->ident));
2831 if (sym->type == SYM_NODE) {
2832 if (evaluate_symbol_call(expr))
2833 return expr->ctype;
2835 expr->ctype = ctype->ctype.base_type;
2836 return expr->ctype;
2839 static struct symbol *evaluate_offsetof(struct expression *expr)
2841 struct expression *e = expr->down;
2842 struct symbol *ctype = expr->in;
2843 int class;
2845 if (expr->op == '.') {
2846 struct symbol *field;
2847 int offset = 0;
2848 if (!ctype) {
2849 expression_error(expr, "expected structure or union");
2850 return NULL;
2852 examine_symbol_type(ctype);
2853 class = classify_type(ctype, &ctype);
2854 if (class != TYPE_COMPOUND) {
2855 expression_error(expr, "expected structure or union");
2856 return NULL;
2859 field = find_identifier(expr->ident, ctype->symbol_list, &offset);
2860 if (!field) {
2861 expression_error(expr, "unknown member");
2862 return NULL;
2864 ctype = field;
2865 expr->type = EXPR_VALUE;
2866 expr->flags = Int_const_expr;
2867 expr->value = offset;
2868 expr->taint = 0;
2869 expr->ctype = size_t_ctype;
2870 } else {
2871 if (!ctype) {
2872 expression_error(expr, "expected structure or union");
2873 return NULL;
2875 examine_symbol_type(ctype);
2876 class = classify_type(ctype, &ctype);
2877 if (class != (TYPE_COMPOUND | TYPE_PTR)) {
2878 expression_error(expr, "expected array");
2879 return NULL;
2881 ctype = ctype->ctype.base_type;
2882 if (!expr->index) {
2883 expr->type = EXPR_VALUE;
2884 expr->flags = Int_const_expr;
2885 expr->value = 0;
2886 expr->taint = 0;
2887 expr->ctype = size_t_ctype;
2888 } else {
2889 struct expression *idx = expr->index, *m;
2890 struct symbol *i_type = evaluate_expression(idx);
2891 int i_class = classify_type(i_type, &i_type);
2892 if (!is_int(i_class)) {
2893 expression_error(expr, "non-integer index");
2894 return NULL;
2896 unrestrict(idx, i_class, &i_type);
2897 idx = cast_to(idx, size_t_ctype);
2898 m = alloc_const_expression(expr->pos,
2899 bits_to_bytes(ctype->bit_size));
2900 m->ctype = size_t_ctype;
2901 m->flags = Int_const_expr;
2902 expr->type = EXPR_BINOP;
2903 expr->left = idx;
2904 expr->right = m;
2905 expr->op = '*';
2906 expr->ctype = size_t_ctype;
2907 expr->flags = m->flags & idx->flags & Int_const_expr;
2910 if (e) {
2911 struct expression *copy = __alloc_expression(0);
2912 *copy = *expr;
2913 if (e->type == EXPR_OFFSETOF)
2914 e->in = ctype;
2915 if (!evaluate_expression(e))
2916 return NULL;
2917 expr->type = EXPR_BINOP;
2918 expr->flags = e->flags & copy->flags & Int_const_expr;
2919 expr->op = '+';
2920 expr->ctype = size_t_ctype;
2921 expr->left = copy;
2922 expr->right = e;
2924 return size_t_ctype;
2927 struct symbol *evaluate_expression(struct expression *expr)
2929 if (!expr)
2930 return NULL;
2931 if (expr->ctype)
2932 return expr->ctype;
2934 switch (expr->type) {
2935 case EXPR_VALUE:
2936 case EXPR_FVALUE:
2937 expression_error(expr, "value expression without a type");
2938 return NULL;
2939 case EXPR_STRING:
2940 return evaluate_string(expr);
2941 case EXPR_SYMBOL:
2942 return evaluate_symbol_expression(expr);
2943 case EXPR_BINOP:
2944 if (!evaluate_expression(expr->left))
2945 return NULL;
2946 if (!evaluate_expression(expr->right))
2947 return NULL;
2948 return evaluate_binop(expr);
2949 case EXPR_LOGICAL:
2950 return evaluate_logical(expr);
2951 case EXPR_COMMA:
2952 evaluate_expression(expr->left);
2953 if (!evaluate_expression(expr->right))
2954 return NULL;
2955 return evaluate_comma(expr);
2956 case EXPR_COMPARE:
2957 if (!evaluate_expression(expr->left))
2958 return NULL;
2959 if (!evaluate_expression(expr->right))
2960 return NULL;
2961 return evaluate_compare(expr);
2962 case EXPR_ASSIGNMENT:
2963 if (!evaluate_expression(expr->left))
2964 return NULL;
2965 if (!evaluate_expression(expr->right))
2966 return NULL;
2967 return evaluate_assignment(expr);
2968 case EXPR_PREOP:
2969 if (!evaluate_expression(expr->unop))
2970 return NULL;
2971 return evaluate_preop(expr);
2972 case EXPR_POSTOP:
2973 if (!evaluate_expression(expr->unop))
2974 return NULL;
2975 return evaluate_postop(expr);
2976 case EXPR_CAST:
2977 case EXPR_FORCE_CAST:
2978 case EXPR_IMPLIED_CAST:
2979 return evaluate_cast(expr);
2980 case EXPR_SIZEOF:
2981 return evaluate_sizeof(expr);
2982 case EXPR_PTRSIZEOF:
2983 return evaluate_ptrsizeof(expr);
2984 case EXPR_ALIGNOF:
2985 return evaluate_alignof(expr);
2986 case EXPR_DEREF:
2987 return evaluate_member_dereference(expr);
2988 case EXPR_CALL:
2989 return evaluate_call(expr);
2990 case EXPR_SELECT:
2991 case EXPR_CONDITIONAL:
2992 return evaluate_conditional_expression(expr);
2993 case EXPR_STATEMENT:
2994 expr->ctype = evaluate_statement(expr->statement);
2995 return expr->ctype;
2997 case EXPR_LABEL:
2998 expr->ctype = &ptr_ctype;
2999 return &ptr_ctype;
3001 case EXPR_TYPE:
3002 /* Evaluate the type of the symbol .. */
3003 evaluate_symbol(expr->symbol);
3004 /* .. but the type of the _expression_ is a "type" */
3005 expr->ctype = &type_ctype;
3006 return &type_ctype;
3008 case EXPR_OFFSETOF:
3009 return evaluate_offsetof(expr);
3011 /* These can not exist as stand-alone expressions */
3012 case EXPR_INITIALIZER:
3013 case EXPR_IDENTIFIER:
3014 case EXPR_INDEX:
3015 case EXPR_POS:
3016 expression_error(expr, "internal front-end error: initializer in expression");
3017 return NULL;
3018 case EXPR_SLICE:
3019 expression_error(expr, "internal front-end error: SLICE re-evaluated");
3020 return NULL;
3022 return NULL;
3025 static void check_duplicates(struct symbol *sym)
3027 int declared = 0;
3028 struct symbol *next = sym;
3030 while ((next = next->same_symbol) != NULL) {
3031 const char *typediff;
3032 evaluate_symbol(next);
3033 declared++;
3034 typediff = type_difference(&sym->ctype, &next->ctype, 0, 0);
3035 if (typediff) {
3036 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
3037 show_ident(sym->ident),
3038 stream_name(next->pos.stream), next->pos.line, typediff);
3039 return;
3042 if (!declared) {
3043 unsigned long mod = sym->ctype.modifiers;
3044 if (mod & (MOD_STATIC | MOD_REGISTER))
3045 return;
3046 if (!(mod & MOD_TOPLEVEL))
3047 return;
3048 if (!Wdecl)
3049 return;
3050 if (sym->ident == &main_ident)
3051 return;
3052 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
3056 static struct symbol *evaluate_symbol(struct symbol *sym)
3058 struct symbol *base_type;
3060 if (!sym)
3061 return sym;
3062 if (sym->evaluated)
3063 return sym;
3064 sym->evaluated = 1;
3066 sym = examine_symbol_type(sym);
3067 base_type = get_base_type(sym);
3068 if (!base_type)
3069 return NULL;
3071 /* Evaluate the initializers */
3072 if (sym->initializer)
3073 evaluate_initializer(sym, &sym->initializer);
3075 /* And finally, evaluate the body of the symbol too */
3076 if (base_type->type == SYM_FN) {
3077 struct symbol *curr = current_fn;
3079 if (sym->definition && sym->definition != sym)
3080 return evaluate_symbol(sym->definition);
3082 current_fn = base_type;
3084 examine_fn_arguments(base_type);
3085 if (!base_type->stmt && base_type->inline_stmt)
3086 uninline(sym);
3087 if (base_type->stmt)
3088 evaluate_statement(base_type->stmt);
3090 current_fn = curr;
3093 return base_type;
3096 void evaluate_symbol_list(struct symbol_list *list)
3098 struct symbol *sym;
3100 FOR_EACH_PTR(list, sym) {
3101 evaluate_symbol(sym);
3102 check_duplicates(sym);
3103 } END_FOR_EACH_PTR(sym);
3106 static struct symbol *evaluate_return_expression(struct statement *stmt)
3108 struct expression *expr = stmt->expression;
3109 struct symbol *fntype;
3111 evaluate_expression(expr);
3112 fntype = current_fn->ctype.base_type;
3113 if (!fntype || fntype == &void_ctype) {
3114 if (expr && expr->ctype != &void_ctype)
3115 expression_error(expr, "return expression in %s function", fntype?"void":"typeless");
3116 if (expr && Wreturn_void)
3117 warning(stmt->pos, "returning void-valued expression");
3118 return NULL;
3121 if (!expr) {
3122 sparse_error(stmt->pos, "return with no return value");
3123 return NULL;
3125 if (!expr->ctype)
3126 return NULL;
3127 compatible_assignment_types(expr, fntype, &stmt->expression, "return expression");
3128 return NULL;
3131 static void evaluate_if_statement(struct statement *stmt)
3133 if (!stmt->if_conditional)
3134 return;
3136 evaluate_conditional(stmt->if_conditional, 0);
3137 evaluate_statement(stmt->if_true);
3138 evaluate_statement(stmt->if_false);
3141 static void evaluate_iterator(struct statement *stmt)
3143 evaluate_symbol_list(stmt->iterator_syms);
3144 evaluate_conditional(stmt->iterator_pre_condition, 1);
3145 evaluate_conditional(stmt->iterator_post_condition,1);
3146 evaluate_statement(stmt->iterator_pre_statement);
3147 evaluate_statement(stmt->iterator_statement);
3148 evaluate_statement(stmt->iterator_post_statement);
3151 static void verify_output_constraint(struct expression *expr, const char *constraint)
3153 switch (*constraint) {
3154 case '=': /* Assignment */
3155 case '+': /* Update */
3156 break;
3157 default:
3158 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
3162 static void verify_input_constraint(struct expression *expr, const char *constraint)
3164 switch (*constraint) {
3165 case '=': /* Assignment */
3166 case '+': /* Update */
3167 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
3171 static void evaluate_asm_statement(struct statement *stmt)
3173 struct expression *expr;
3174 struct symbol *sym;
3175 int state;
3177 expr = stmt->asm_string;
3178 if (!expr || expr->type != EXPR_STRING) {
3179 sparse_error(stmt->pos, "need constant string for inline asm");
3180 return;
3183 state = 0;
3184 FOR_EACH_PTR(stmt->asm_outputs, expr) {
3185 switch (state) {
3186 case 0: /* Identifier */
3187 state = 1;
3188 continue;
3190 case 1: /* Constraint */
3191 state = 2;
3192 if (!expr || expr->type != EXPR_STRING) {
3193 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
3194 *THIS_ADDRESS(expr) = NULL;
3195 continue;
3197 verify_output_constraint(expr, expr->string->data);
3198 continue;
3200 case 2: /* Expression */
3201 state = 0;
3202 if (!evaluate_expression(expr))
3203 return;
3204 if (!lvalue_expression(expr))
3205 warning(expr->pos, "asm output is not an lvalue");
3206 evaluate_assign_to(expr, expr->ctype);
3207 continue;
3209 } END_FOR_EACH_PTR(expr);
3211 state = 0;
3212 FOR_EACH_PTR(stmt->asm_inputs, expr) {
3213 switch (state) {
3214 case 0: /* Identifier */
3215 state = 1;
3216 continue;
3218 case 1: /* Constraint */
3219 state = 2;
3220 if (!expr || expr->type != EXPR_STRING) {
3221 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
3222 *THIS_ADDRESS(expr) = NULL;
3223 continue;
3225 verify_input_constraint(expr, expr->string->data);
3226 continue;
3228 case 2: /* Expression */
3229 state = 0;
3230 if (!evaluate_expression(expr))
3231 return;
3232 continue;
3234 } END_FOR_EACH_PTR(expr);
3236 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
3237 if (!expr) {
3238 sparse_error(stmt->pos, "bad asm clobbers");
3239 return;
3241 if (expr->type == EXPR_STRING)
3242 continue;
3243 expression_error(expr, "asm clobber is not a string");
3244 } END_FOR_EACH_PTR(expr);
3246 FOR_EACH_PTR(stmt->asm_labels, sym) {
3247 if (!sym || sym->type != SYM_LABEL) {
3248 sparse_error(stmt->pos, "bad asm label");
3249 return;
3251 } END_FOR_EACH_PTR(sym);
3254 static void evaluate_case_statement(struct statement *stmt)
3256 evaluate_expression(stmt->case_expression);
3257 evaluate_expression(stmt->case_to);
3258 evaluate_statement(stmt->case_statement);
3261 static void check_case_type(struct expression *switch_expr,
3262 struct expression *case_expr,
3263 struct expression **enumcase)
3265 struct symbol *switch_type, *case_type;
3266 int sclass, cclass;
3268 if (!case_expr)
3269 return;
3271 switch_type = switch_expr->ctype;
3272 case_type = evaluate_expression(case_expr);
3274 if (!switch_type || !case_type)
3275 goto Bad;
3276 if (enumcase) {
3277 if (*enumcase)
3278 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
3279 else if (is_enum_type(case_type))
3280 *enumcase = case_expr;
3283 sclass = classify_type(switch_type, &switch_type);
3284 cclass = classify_type(case_type, &case_type);
3286 /* both should be arithmetic */
3287 if (!(sclass & cclass & TYPE_NUM))
3288 goto Bad;
3290 /* neither should be floating */
3291 if ((sclass | cclass) & TYPE_FLOAT)
3292 goto Bad;
3294 /* if neither is restricted, we are OK */
3295 if (!((sclass | cclass) & TYPE_RESTRICT))
3296 return;
3298 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
3299 cclass, sclass, case_type, switch_type)) {
3300 unrestrict(case_expr, cclass, &case_type);
3301 unrestrict(switch_expr, sclass, &switch_type);
3303 return;
3305 Bad:
3306 expression_error(case_expr, "incompatible types for 'case' statement");
3309 static void evaluate_switch_statement(struct statement *stmt)
3311 struct symbol *sym;
3312 struct expression *enumcase = NULL;
3313 struct expression **enumcase_holder = &enumcase;
3314 struct expression *sel = stmt->switch_expression;
3316 evaluate_expression(sel);
3317 evaluate_statement(stmt->switch_statement);
3318 if (!sel)
3319 return;
3320 if (sel->ctype && is_enum_type(sel->ctype))
3321 enumcase_holder = NULL; /* Only check cases against switch */
3323 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
3324 struct statement *case_stmt = sym->stmt;
3325 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
3326 check_case_type(sel, case_stmt->case_to, enumcase_holder);
3327 } END_FOR_EACH_PTR(sym);
3330 static void evaluate_goto_statement(struct statement *stmt)
3332 struct symbol *label = stmt->goto_label;
3334 if (label && !label->stmt && !lookup_keyword(label->ident, NS_KEYWORD))
3335 sparse_error(stmt->pos, "label '%s' was not declared", show_ident(label->ident));
3337 evaluate_expression(stmt->goto_expression);
3340 struct symbol *evaluate_statement(struct statement *stmt)
3342 if (!stmt)
3343 return NULL;
3345 switch (stmt->type) {
3346 case STMT_DECLARATION: {
3347 struct symbol *s;
3348 FOR_EACH_PTR(stmt->declaration, s) {
3349 evaluate_symbol(s);
3350 } END_FOR_EACH_PTR(s);
3351 return NULL;
3354 case STMT_RETURN:
3355 return evaluate_return_expression(stmt);
3357 case STMT_EXPRESSION:
3358 if (!evaluate_expression(stmt->expression))
3359 return NULL;
3360 if (stmt->expression->ctype == &null_ctype)
3361 stmt->expression = cast_to(stmt->expression, &ptr_ctype);
3362 return degenerate(stmt->expression);
3364 case STMT_COMPOUND: {
3365 struct statement *s;
3366 struct symbol *type = NULL;
3368 /* Evaluate the return symbol in the compound statement */
3369 evaluate_symbol(stmt->ret);
3372 * Then, evaluate each statement, making the type of the
3373 * compound statement be the type of the last statement
3375 type = evaluate_statement(stmt->args);
3376 FOR_EACH_PTR(stmt->stmts, s) {
3377 type = evaluate_statement(s);
3378 } END_FOR_EACH_PTR(s);
3379 if (!type)
3380 type = &void_ctype;
3381 return type;
3383 case STMT_IF:
3384 evaluate_if_statement(stmt);
3385 return NULL;
3386 case STMT_ITERATOR:
3387 evaluate_iterator(stmt);
3388 return NULL;
3389 case STMT_SWITCH:
3390 evaluate_switch_statement(stmt);
3391 return NULL;
3392 case STMT_CASE:
3393 evaluate_case_statement(stmt);
3394 return NULL;
3395 case STMT_LABEL:
3396 return evaluate_statement(stmt->label_statement);
3397 case STMT_GOTO:
3398 evaluate_goto_statement(stmt);
3399 return NULL;
3400 case STMT_NONE:
3401 break;
3402 case STMT_ASM:
3403 evaluate_asm_statement(stmt);
3404 return NULL;
3405 case STMT_CONTEXT:
3406 evaluate_expression(stmt->expression);
3407 return NULL;
3408 case STMT_RANGE:
3409 evaluate_expression(stmt->range_expression);
3410 evaluate_expression(stmt->range_low);
3411 evaluate_expression(stmt->range_high);
3412 return NULL;
3414 return NULL;