slice: remove unneeded nr_nrbits from EXPR_SLICE
[smatch.git] / evaluate.c
blobe13edf5488b445203cf7d86a47abd169a8d7da14
1 /*
2 * sparse/evaluate.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
25 * Evaluate constant expressions.
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <stddef.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <limits.h>
37 #include "evaluate.h"
38 #include "lib.h"
39 #include "allocate.h"
40 #include "parse.h"
41 #include "token.h"
42 #include "symbol.h"
43 #include "target.h"
44 #include "expression.h"
46 struct symbol *current_fn;
48 struct ident bad_address_space = { .len = 6, .name = "bad AS", };
50 static struct symbol *degenerate(struct expression *expr);
51 static struct symbol *evaluate_symbol(struct symbol *sym);
53 static inline int valid_expr_type(struct expression *expr)
55 return expr && valid_type(expr->ctype);
58 static inline int valid_subexpr_type(struct expression *expr)
60 return valid_expr_type(expr->left)
61 && valid_expr_type(expr->right);
64 static struct symbol *unqualify_type(struct symbol *ctype)
66 if (!ctype)
67 return ctype;
68 if (ctype->type == SYM_NODE && (ctype->ctype.modifiers & MOD_QUALIFIER)) {
69 struct symbol *unqual = alloc_symbol(ctype->pos, 0);
71 *unqual = *ctype;
72 unqual->ctype.modifiers &= ~MOD_QUALIFIER;
73 return unqual;
75 return ctype;
78 static struct symbol *evaluate_symbol_expression(struct expression *expr)
80 struct expression *addr;
81 struct symbol *sym = expr->symbol;
82 struct symbol *base_type;
84 if (!sym) {
85 expression_error(expr, "undefined identifier '%s'", show_ident(expr->symbol_name));
86 return NULL;
89 examine_symbol_type(sym);
91 base_type = get_base_type(sym);
92 if (!base_type) {
93 expression_error(expr, "identifier '%s' has no type", show_ident(expr->symbol_name));
94 return NULL;
97 addr = alloc_expression(expr->pos, EXPR_SYMBOL);
98 addr->symbol = sym;
99 addr->symbol_name = expr->symbol_name;
100 addr->ctype = &lazy_ptr_ctype; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
101 addr->flags = expr->flags;
102 expr->type = EXPR_PREOP;
103 expr->op = '*';
104 expr->unop = addr;
105 expr->flags = CEF_NONE;
107 /* The type of a symbol is the symbol itself! */
108 expr->ctype = sym;
109 return sym;
112 static struct symbol *evaluate_string(struct expression *expr)
114 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
115 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
116 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
117 struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
118 unsigned int length = expr->string->length;
119 struct symbol *char_type = expr->wide ? wchar_ctype : &char_ctype;
121 sym->array_size = alloc_const_expression(expr->pos, length);
122 sym->bit_size = length * char_type->bit_size;
123 sym->ctype.alignment = 1;
124 sym->string = 1;
125 sym->ctype.modifiers = MOD_STATIC;
126 sym->ctype.base_type = array;
127 sym->initializer = initstr;
128 sym->examined = 1;
129 sym->evaluated = 1;
131 initstr->ctype = sym;
132 initstr->string = expr->string;
134 array->array_size = sym->array_size;
135 array->bit_size = sym->bit_size;
136 array->ctype.alignment = char_type->ctype.alignment;
137 array->ctype.modifiers = MOD_STATIC;
138 array->ctype.base_type = char_type;
139 array->examined = 1;
140 array->evaluated = 1;
142 addr->symbol = sym;
143 addr->ctype = &lazy_ptr_ctype;
144 addr->flags = CEF_ADDR;
146 expr->type = EXPR_PREOP;
147 expr->op = '*';
148 expr->unop = addr;
149 expr->ctype = sym;
150 return sym;
153 /* type has come from classify_type and is an integer type */
154 static inline struct symbol *integer_promotion(struct symbol *type)
156 unsigned long mod = type->ctype.modifiers;
157 int width = type->bit_size;
160 * Bitfields always promote to the base type,
161 * even if the bitfield might be bigger than
162 * an "int".
164 if (type->type == SYM_BITFIELD) {
165 type = type->ctype.base_type;
167 mod = type->ctype.modifiers;
168 if (width < bits_in_int)
169 return &int_ctype;
171 /* If char/short has as many bits as int, it still gets "promoted" */
172 if (type->rank < 0) {
173 if (mod & MOD_UNSIGNED)
174 return &uint_ctype;
175 return &int_ctype;
177 return type;
181 * After integer promotons:
182 * If both types are the same
183 * -> no conversion needed
184 * If the types have the same signedness (their rank must be different)
185 * -> convert to the type of the highest rank
186 * If rank(unsigned type) >= rank(signed type)
187 * -> convert to the unsigned type
188 * If size(signed type) > size(unsigned type)
189 * -> convert to the signed type
190 * Otherwise
191 * -> convert to the unsigned type corresponding to the signed type.
193 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
195 static struct symbol *unsigned_types[] = {
196 [0] = &uint_ctype,
197 [1] = &ulong_ctype,
198 [2] = &ullong_ctype,
199 [3] = &uint128_ctype,
201 unsigned long lmod, rmod;
202 struct symbol *stype, *utype;
204 left = integer_promotion(left);
205 right = integer_promotion(right);
207 if (left == right)
208 return left;
210 lmod = left->ctype.modifiers;
211 rmod = right->ctype.modifiers;
212 if (((lmod ^ rmod) & MOD_UNSIGNED) == 0)
213 return (left->rank > right->rank) ? left : right;
214 if (lmod & MOD_UNSIGNED) {
215 utype = left;
216 stype = right;
217 } else {
218 stype = left;
219 utype = right;
221 if (utype->rank >= stype->rank)
222 return utype;
223 if (stype->bit_size > utype->bit_size)
224 return stype;
225 utype = unsigned_types[stype->rank];
226 return utype;
229 static int same_cast_type(struct symbol *orig, struct symbol *new)
231 return orig->bit_size == new->bit_size &&
232 orig->bit_offset == new->bit_offset;
235 static struct symbol *base_type(struct symbol *node, unsigned long *modp, struct ident **asp)
237 unsigned long mod = 0;
238 struct ident *as = NULL;
240 while (node) {
241 mod |= node->ctype.modifiers;
242 combine_address_space(node->pos, &as, node->ctype.as);
243 if (node->type == SYM_NODE) {
244 node = node->ctype.base_type;
245 continue;
247 break;
249 *modp = mod & ~MOD_IGNORE;
250 *asp = as;
251 return node;
254 static int is_same_type(struct expression *expr, struct symbol *new)
256 struct symbol *old = expr->ctype;
257 unsigned long oldmod, newmod;
258 struct ident *oldas, *newas;
260 old = base_type(old, &oldmod, &oldas);
261 new = base_type(new, &newmod, &newas);
263 /* Same base type, same address space? */
264 if (old == new && oldas == newas) {
265 unsigned long difmod;
267 /* Check the modifier bits. */
268 difmod = (oldmod ^ newmod) & ~MOD_NOCAST;
270 /* Exact same type? */
271 if (!difmod)
272 return 1;
275 * Not the same type, but differs only in "const".
276 * Don't warn about MOD_NOCAST.
278 if (difmod == MOD_CONST)
279 return 0;
281 if ((oldmod | newmod) & MOD_NOCAST) {
282 const char *tofrom = "to/from";
283 if (!(newmod & MOD_NOCAST))
284 tofrom = "from";
285 if (!(oldmod & MOD_NOCAST))
286 tofrom = "to";
287 warning(expr->pos, "implicit cast %s nocast type", tofrom);
289 return 0;
292 static void
293 warn_for_different_enum_types (struct position pos,
294 struct symbol *typea,
295 struct symbol *typeb)
297 if (!Wenum_mismatch)
298 return;
299 if (typea->type == SYM_NODE)
300 typea = typea->ctype.base_type;
301 if (typeb->type == SYM_NODE)
302 typeb = typeb->ctype.base_type;
304 if (typea == typeb)
305 return;
307 if (typea->type == SYM_ENUM && typeb->type == SYM_ENUM) {
308 warning(pos, "mixing different enum types:");
309 info(pos, " %s", show_typename(typea));
310 info(pos, " %s", show_typename(typeb));
314 static int cast_flags(struct expression *expr, struct expression *target);
315 static struct symbol *cast_to_bool(struct expression *expr);
318 * This gets called for implicit casts in assignments and
319 * integer promotion. We often want to try to move the
320 * cast down, because the ops involved may have been
321 * implicitly cast up, and we can get rid of the casts
322 * early.
324 static struct expression * cast_to(struct expression *old, struct symbol *type)
326 struct expression *expr;
328 warn_for_different_enum_types (old->pos, old->ctype, type);
330 if (old->ctype != &null_ctype && is_same_type(old, type))
331 return old;
334 * See if we can simplify the op. Move the cast down.
336 switch (old->type) {
337 case EXPR_PREOP:
338 if (old->ctype->bit_size < type->bit_size)
339 break;
340 if (old->op == '~') {
341 old->ctype = type;
342 old->unop = cast_to(old->unop, type);
343 return old;
345 break;
347 case EXPR_IMPLIED_CAST:
348 warn_for_different_enum_types(old->pos, old->ctype, type);
350 if (old->ctype->bit_size >= type->bit_size) {
351 struct expression *orig = old->cast_expression;
352 if (same_cast_type(orig->ctype, type))
353 return orig;
354 if (old->ctype->bit_offset == type->bit_offset) {
355 old->ctype = type;
356 old->cast_type = type;
357 return old;
360 break;
362 default:
363 /* nothing */;
366 expr = alloc_expression(old->pos, EXPR_IMPLIED_CAST);
367 expr->ctype = type;
368 expr->cast_type = type;
369 expr->cast_expression = old;
370 expr->flags = cast_flags(expr, old);
372 if (is_bool_type(type))
373 cast_to_bool(expr);
375 return expr;
378 enum {
379 TYPE_NUM = 1,
380 TYPE_BITFIELD = 2,
381 TYPE_RESTRICT = 4,
382 TYPE_FLOAT = 8,
383 TYPE_PTR = 16,
384 TYPE_COMPOUND = 32,
385 TYPE_FOULED = 64,
386 TYPE_FN = 128,
389 static inline int classify_type(struct symbol *type, struct symbol **base)
391 static int type_class[SYM_BAD + 1] = {
392 [SYM_PTR] = TYPE_PTR,
393 [SYM_FN] = TYPE_PTR | TYPE_FN,
394 [SYM_ARRAY] = TYPE_PTR | TYPE_COMPOUND,
395 [SYM_STRUCT] = TYPE_COMPOUND,
396 [SYM_UNION] = TYPE_COMPOUND,
397 [SYM_BITFIELD] = TYPE_NUM | TYPE_BITFIELD,
398 [SYM_RESTRICT] = TYPE_NUM | TYPE_RESTRICT,
399 [SYM_FOULED] = TYPE_NUM | TYPE_RESTRICT | TYPE_FOULED,
401 if (type->type == SYM_NODE)
402 type = type->ctype.base_type;
403 if (type->type == SYM_TYPEOF) {
404 type = examine_symbol_type(type);
405 if (type->type == SYM_NODE)
406 type = type->ctype.base_type;
408 if (type->type == SYM_ENUM)
409 type = type->ctype.base_type;
410 *base = type;
411 if (type->type == SYM_BASETYPE) {
412 if (type->ctype.base_type == &int_type)
413 return TYPE_NUM;
414 if (type->ctype.base_type == &fp_type)
415 return TYPE_NUM | TYPE_FLOAT;
417 return type_class[type->type];
420 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
422 static inline int is_string_type(struct symbol *type)
424 if (type->type == SYM_NODE)
425 type = type->ctype.base_type;
426 if (type->type != SYM_ARRAY)
427 return 0;
428 type = type->ctype.base_type;
429 return is_byte_type(type) || is_wchar_type(type);
432 static struct symbol *bad_expr_type(struct expression *expr)
434 switch (expr->type) {
435 case EXPR_BINOP:
436 case EXPR_COMPARE:
437 if (!valid_subexpr_type(expr))
438 break;
439 sparse_error(expr->pos, "incompatible types for operation (%s):", show_special(expr->op));
440 info(expr->pos, " %s", show_typename(expr->left->ctype));
441 info(expr->pos, " %s", show_typename(expr->right->ctype));
442 break;
443 case EXPR_PREOP:
444 case EXPR_POSTOP:
445 if (!valid_expr_type(expr->unop))
446 break;
447 sparse_error(expr->pos, "incompatible type for operation (%s):", show_special(expr->op));
448 info(expr->pos, " %s", show_typename(expr->unop->ctype));
449 break;
450 default:
451 break;
454 expr->flags = CEF_NONE;
455 return expr->ctype = &bad_ctype;
458 static int restricted_value(struct expression *v, struct symbol *type)
460 if (v->type != EXPR_VALUE)
461 return 1;
462 if (v->value != 0)
463 return 1;
464 return 0;
467 static int restricted_binop(int op, struct symbol *type)
469 switch (op) {
470 case '&':
471 case '=':
472 case SPECIAL_AND_ASSIGN:
473 case SPECIAL_OR_ASSIGN:
474 case SPECIAL_XOR_ASSIGN:
475 return 1; /* unfoul */
476 case '|':
477 case '^':
478 case '?':
479 return 2; /* keep fouled */
480 case SPECIAL_EQUAL:
481 case SPECIAL_NOTEQUAL:
482 return 3; /* warn if fouled */
483 default:
484 return 0; /* warn */
488 static int restricted_unop(int op, struct symbol **type)
490 if (op == '~') {
491 if ((*type)->bit_size < bits_in_int)
492 *type = befoul(*type);
493 return 0;
494 } if (op == '+')
495 return 0;
496 return 1;
499 /* type should be SYM_FOULED */
500 static inline struct symbol *unfoul(struct symbol *type)
502 return type->ctype.base_type;
505 static struct symbol *restricted_binop_type(int op,
506 struct expression *left,
507 struct expression *right,
508 int lclass, int rclass,
509 struct symbol *ltype,
510 struct symbol *rtype)
512 struct symbol *ctype = NULL;
513 if (lclass & TYPE_RESTRICT) {
514 if (rclass & TYPE_RESTRICT) {
515 if (ltype == rtype) {
516 ctype = ltype;
517 } else if (lclass & TYPE_FOULED) {
518 if (unfoul(ltype) == rtype)
519 ctype = ltype;
520 } else if (rclass & TYPE_FOULED) {
521 if (unfoul(rtype) == ltype)
522 ctype = rtype;
524 } else {
525 if (!restricted_value(right, ltype))
526 ctype = ltype;
528 } else if (!restricted_value(left, rtype))
529 ctype = rtype;
531 if (ctype) {
532 switch (restricted_binop(op, ctype)) {
533 case 1:
534 if ((lclass ^ rclass) & TYPE_FOULED)
535 ctype = unfoul(ctype);
536 break;
537 case 3:
538 if (!(lclass & rclass & TYPE_FOULED))
539 break;
540 case 0:
541 ctype = NULL;
542 default:
543 break;
547 return ctype;
550 static inline void unrestrict(struct expression *expr,
551 int class, struct symbol **ctype)
553 if (class & TYPE_RESTRICT) {
554 if (class & TYPE_FOULED)
555 *ctype = unfoul(*ctype);
556 warning(expr->pos, "%s degrades to integer",
557 show_typename(*ctype));
558 *ctype = (*ctype)->ctype.base_type; /* get to arithmetic type */
562 static struct symbol *usual_conversions(int op,
563 struct expression *left,
564 struct expression *right,
565 int lclass, int rclass,
566 struct symbol *ltype,
567 struct symbol *rtype)
569 struct symbol *ctype;
571 warn_for_different_enum_types(right->pos, left->ctype, right->ctype);
573 if ((lclass | rclass) & TYPE_RESTRICT)
574 goto Restr;
576 Normal:
577 if (!(lclass & TYPE_FLOAT)) {
578 if (!(rclass & TYPE_FLOAT))
579 return bigger_int_type(ltype, rtype);
580 else
581 return rtype;
582 } else if (rclass & TYPE_FLOAT) {
583 if (rtype->rank > ltype->rank)
584 return rtype;
585 else
586 return ltype;
587 } else
588 return ltype;
590 Restr:
591 ctype = restricted_binop_type(op, left, right,
592 lclass, rclass, ltype, rtype);
593 if (ctype)
594 return ctype;
596 unrestrict(left, lclass, &ltype);
597 unrestrict(right, rclass, &rtype);
599 goto Normal;
602 static inline int lvalue_expression(struct expression *expr)
604 return expr->type == EXPR_PREOP && expr->op == '*';
607 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *itype)
609 struct expression *index = expr->right;
610 struct symbol *ctype, *base;
611 int multiply;
613 classify_type(degenerate(expr->left), &ctype);
614 base = examine_pointer_target(ctype);
617 * An address constant +/- an integer constant expression
618 * yields an address constant again [6.6(7)].
620 if ((expr->left->flags & CEF_ADDR) && (expr->right->flags & CEF_ICE))
621 expr->flags = CEF_ADDR;
623 if (!base) {
624 expression_error(expr, "missing type information");
625 return NULL;
627 if (is_function(base)) {
628 expression_error(expr, "arithmetics on pointers to functions");
629 return NULL;
632 /* Get the size of whatever the pointer points to */
633 multiply = is_void_type(base) ? 1 : bits_to_bytes(base->bit_size);
635 if (ctype == &null_ctype)
636 ctype = &ptr_ctype;
637 expr->ctype = ctype;
639 if (multiply == 1 && itype->bit_size == bits_in_pointer)
640 return ctype;
642 if (index->type == EXPR_VALUE) {
643 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
644 unsigned long long v = index->value, mask;
645 mask = 1ULL << (itype->bit_size - 1);
646 if (v & mask)
647 v |= -mask;
648 else
649 v &= mask - 1;
650 v *= multiply;
651 mask = 1ULL << (bits_in_pointer - 1);
652 v &= mask | (mask - 1);
653 val->value = v;
654 val->ctype = ssize_t_ctype;
655 expr->right = val;
656 return ctype;
659 if (itype->bit_size != bits_in_pointer)
660 index = cast_to(index, ssize_t_ctype);
662 if (multiply > 1) {
663 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
664 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
666 val->ctype = ssize_t_ctype;
667 val->value = multiply;
669 mul->op = '*';
670 mul->ctype = ssize_t_ctype;
671 mul->left = index;
672 mul->right = val;
673 index = mul;
676 expr->right = index;
677 return ctype;
680 static void examine_fn_arguments(struct symbol *fn);
682 #define MOD_IGN (MOD_QUALIFIER | MOD_FUN_ATTR)
684 const char *type_difference(struct ctype *c1, struct ctype *c2,
685 unsigned long mod1, unsigned long mod2)
687 struct ident *as1 = c1->as, *as2 = c2->as;
688 struct symbol *t1 = c1->base_type;
689 struct symbol *t2 = c2->base_type;
690 int move1 = 1, move2 = 1;
691 mod1 |= c1->modifiers;
692 mod2 |= c2->modifiers;
693 for (;;) {
694 unsigned long diff;
695 int type;
696 struct symbol *base1 = t1->ctype.base_type;
697 struct symbol *base2 = t2->ctype.base_type;
700 * FIXME! Collect alignment and context too here!
702 if (move1) {
703 if (t1 && t1->type != SYM_PTR) {
704 mod1 |= t1->ctype.modifiers;
705 combine_address_space(t1->pos, &as1, t1->ctype.as);
707 move1 = 0;
710 if (move2) {
711 if (t2 && t2->type != SYM_PTR) {
712 mod2 |= t2->ctype.modifiers;
713 combine_address_space(t2->pos, &as2, t2->ctype.as);
715 move2 = 0;
718 if (t1 == t2)
719 break;
720 if (!t1 || !t2)
721 return "different types";
723 if (t1->type == SYM_NODE || t1->type == SYM_ENUM) {
724 t1 = base1;
725 move1 = 1;
726 if (!t1)
727 return "bad types";
728 continue;
731 if (t2->type == SYM_NODE || t2->type == SYM_ENUM) {
732 t2 = base2;
733 move2 = 1;
734 if (!t2)
735 return "bad types";
736 continue;
739 move1 = move2 = 1;
740 type = t1->type;
741 if (type != t2->type)
742 return "different base types";
744 switch (type) {
745 default:
746 sparse_error(t1->pos,
747 "internal error: bad type in derived(%d)",
748 type);
749 return "bad types";
750 case SYM_RESTRICT:
751 return "different base types";
752 case SYM_UNION:
753 case SYM_STRUCT:
754 /* allow definition of incomplete structs and unions */
755 if (t1->ident == t2->ident)
756 return NULL;
757 return "different base types";
758 case SYM_ARRAY:
759 /* XXX: we ought to compare sizes */
760 break;
761 case SYM_PTR:
762 if (as1 != as2)
763 return "different address spaces";
764 /* MOD_SPECIFIER is due to idiocy in parse.c */
765 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SPECIFIER)
766 return "different modifiers";
767 /* we could be lazier here */
768 base1 = examine_pointer_target(t1);
769 base2 = examine_pointer_target(t2);
770 mod1 = t1->ctype.modifiers;
771 as1 = t1->ctype.as;
772 mod2 = t2->ctype.modifiers;
773 as2 = t2->ctype.as;
774 break;
775 case SYM_FN: {
776 struct symbol *arg1, *arg2;
777 int i;
779 if (as1 != as2)
780 return "different address spaces";
781 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
782 return "different modifiers";
783 mod1 = t1->ctype.modifiers;
784 as1 = t1->ctype.as;
785 mod2 = t2->ctype.modifiers;
786 as2 = t2->ctype.as;
788 if (t1->variadic != t2->variadic)
789 return "incompatible variadic arguments";
790 examine_fn_arguments(t1);
791 examine_fn_arguments(t2);
792 PREPARE_PTR_LIST(t1->arguments, arg1);
793 PREPARE_PTR_LIST(t2->arguments, arg2);
794 i = 1;
795 for (;;) {
796 const char *diffstr;
797 if (!arg1 && !arg2)
798 break;
799 if (!arg1 || !arg2)
800 return "different argument counts";
801 diffstr = type_difference(&arg1->ctype,
802 &arg2->ctype,
803 MOD_IGN, MOD_IGN);
804 if (diffstr) {
805 static char argdiff[80];
806 sprintf(argdiff, "incompatible argument %d (%s)", i, diffstr);
807 return argdiff;
809 NEXT_PTR_LIST(arg1);
810 NEXT_PTR_LIST(arg2);
811 i++;
813 FINISH_PTR_LIST(arg2);
814 FINISH_PTR_LIST(arg1);
815 break;
817 case SYM_BASETYPE:
818 if (as1 != as2)
819 return "different address spaces";
820 if (base1 != base2)
821 return "different base types";
822 if (t1->rank != t2->rank)
823 return "different type sizes";
824 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
825 if (!diff)
826 return NULL;
827 else if (diff & ~MOD_SIGNEDNESS)
828 return "different modifiers";
829 else
830 return "different signedness";
832 t1 = base1;
833 t2 = base2;
835 if (as1 != as2)
836 return "different address spaces";
837 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
838 return "different modifiers";
839 return NULL;
842 static void bad_null(struct expression *expr)
844 if (Wnon_pointer_null)
845 warning(expr->pos, "Using plain integer as NULL pointer");
848 static unsigned long target_qualifiers(struct symbol *type)
850 unsigned long mod = type->ctype.modifiers & MOD_IGN;
851 if (type->ctype.base_type && type->ctype.base_type->type == SYM_ARRAY)
852 mod = 0;
853 return mod;
856 static struct symbol *evaluate_ptr_sub(struct expression *expr)
858 const char *typediff;
859 struct symbol *ltype, *rtype;
860 struct expression *l = expr->left;
861 struct expression *r = expr->right;
862 struct symbol *lbase;
864 classify_type(degenerate(l), &ltype);
865 classify_type(degenerate(r), &rtype);
867 lbase = examine_pointer_target(ltype);
868 examine_pointer_target(rtype);
869 typediff = type_difference(&ltype->ctype, &rtype->ctype,
870 target_qualifiers(rtype),
871 target_qualifiers(ltype));
872 if (typediff)
873 expression_error(expr, "subtraction of different types can't work (%s)", typediff);
875 if (is_function(lbase)) {
876 expression_error(expr, "subtraction of functions? Share your drugs");
877 return NULL;
880 expr->ctype = ssize_t_ctype;
881 if (lbase->bit_size > bits_in_char) {
882 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
883 struct expression *div = expr;
884 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
885 unsigned long value = bits_to_bytes(lbase->bit_size);
887 val->ctype = size_t_ctype;
888 val->value = value;
890 if (value & (value-1)) {
891 if (Wptr_subtraction_blows) {
892 warning(expr->pos, "potentially expensive pointer subtraction");
893 info(expr->pos, " '%s' has a non-power-of-2 size: %lu", show_typename(lbase), value);
897 sub->op = '-';
898 sub->ctype = ssize_t_ctype;
899 sub->left = l;
900 sub->right = r;
902 div->op = '/';
903 div->left = sub;
904 div->right = val;
907 return ssize_t_ctype;
910 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
912 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
914 struct symbol *ctype;
916 if (!expr)
917 return NULL;
919 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
920 warning(expr->pos, "assignment expression in conditional");
922 ctype = evaluate_expression(expr);
923 if (!valid_type(ctype))
924 return NULL;
925 if (is_safe_type(ctype))
926 warning(expr->pos, "testing a 'safe expression'");
927 if (is_func_type(ctype)) {
928 if (Waddress)
929 warning(expr->pos, "the address of %s will always evaluate as true", "a function");
930 } else if (is_array_type(ctype)) {
931 if (Waddress)
932 warning(expr->pos, "the address of %s will always evaluate as true", "an array");
933 } else if (!is_scalar_type(ctype)) {
934 sparse_error(expr->pos, "non-scalar type in conditional:");
935 info(expr->pos, " %s", show_typename(ctype));
936 return NULL;
939 ctype = degenerate(expr);
940 return ctype;
943 static struct symbol *evaluate_logical(struct expression *expr)
945 if (!evaluate_conditional(expr->left, 0))
946 return NULL;
947 if (!evaluate_conditional(expr->right, 0))
948 return NULL;
950 /* the result is int [6.5.13(3), 6.5.14(3)] */
951 expr->ctype = &int_ctype;
952 expr->flags = expr->left->flags & expr->right->flags;
953 expr->flags &= ~(CEF_CONST_MASK | CEF_ADDR);
954 return &int_ctype;
957 static struct symbol *evaluate_binop(struct expression *expr)
959 struct symbol *ltype, *rtype, *ctype;
960 int lclass = classify_type(expr->left->ctype, &ltype);
961 int rclass = classify_type(expr->right->ctype, &rtype);
962 int op = expr->op;
964 /* number op number */
965 if (lclass & rclass & TYPE_NUM) {
966 expr->flags = expr->left->flags & expr->right->flags;
967 expr->flags &= ~CEF_CONST_MASK;
969 if ((lclass | rclass) & TYPE_FLOAT) {
970 switch (op) {
971 case '+': case '-': case '*': case '/':
972 break;
973 default:
974 return bad_expr_type(expr);
978 if (op == SPECIAL_LEFTSHIFT || op == SPECIAL_RIGHTSHIFT) {
979 // shifts do integer promotions, but that's it.
980 unrestrict(expr->left, lclass, &ltype);
981 unrestrict(expr->right, rclass, &rtype);
982 ctype = ltype = integer_promotion(ltype);
983 rtype = integer_promotion(rtype);
984 } else {
985 // The rest do usual conversions
986 const unsigned left_not = expr->left->type == EXPR_PREOP
987 && expr->left->op == '!';
988 const unsigned right_not = expr->right->type == EXPR_PREOP
989 && expr->right->op == '!';
990 if ((op == '&' || op == '|') && (left_not || right_not))
991 warning(expr->pos, "dubious: %sx %c %sy",
992 left_not ? "!" : "",
994 right_not ? "!" : "");
996 ltype = usual_conversions(op, expr->left, expr->right,
997 lclass, rclass, ltype, rtype);
998 ctype = rtype = ltype;
1001 expr->left = cast_to(expr->left, ltype);
1002 expr->right = cast_to(expr->right, rtype);
1003 expr->ctype = ctype;
1004 return ctype;
1007 /* pointer (+|-) integer */
1008 if (lclass & TYPE_PTR && is_int(rclass) && (op == '+' || op == '-')) {
1009 unrestrict(expr->right, rclass, &rtype);
1010 return evaluate_ptr_add(expr, rtype);
1013 /* integer + pointer */
1014 if (rclass & TYPE_PTR && is_int(lclass) && op == '+') {
1015 struct expression *index = expr->left;
1016 unrestrict(index, lclass, &ltype);
1017 expr->left = expr->right;
1018 expr->right = index;
1019 return evaluate_ptr_add(expr, ltype);
1022 /* pointer - pointer */
1023 if (lclass & rclass & TYPE_PTR && expr->op == '-')
1024 return evaluate_ptr_sub(expr);
1026 return bad_expr_type(expr);
1029 static struct symbol *evaluate_comma(struct expression *expr)
1031 expr->ctype = unqualify_type(degenerate(expr->right));
1032 if (expr->ctype == &null_ctype)
1033 expr->ctype = &ptr_ctype;
1034 expr->flags &= expr->left->flags & expr->right->flags;
1035 return expr->ctype;
1038 static int modify_for_unsigned(int op)
1040 if (op == '<')
1041 op = SPECIAL_UNSIGNED_LT;
1042 else if (op == '>')
1043 op = SPECIAL_UNSIGNED_GT;
1044 else if (op == SPECIAL_LTE)
1045 op = SPECIAL_UNSIGNED_LTE;
1046 else if (op == SPECIAL_GTE)
1047 op = SPECIAL_UNSIGNED_GTE;
1048 return op;
1051 enum null_constant_type {
1052 NON_NULL,
1053 NULL_PTR,
1054 NULL_ZERO,
1057 static inline int is_null_pointer_constant(struct expression *e)
1059 if (e->ctype == &null_ctype)
1060 return NULL_PTR;
1061 if (!(e->flags & CEF_ICE))
1062 return NON_NULL;
1063 return is_zero_constant(e) ? NULL_ZERO : NON_NULL;
1066 static struct symbol *evaluate_compare(struct expression *expr)
1068 struct expression *left = expr->left, *right = expr->right;
1069 struct symbol *ltype, *rtype, *lbase, *rbase;
1070 int lclass = classify_type(degenerate(left), &ltype);
1071 int rclass = classify_type(degenerate(right), &rtype);
1072 struct symbol *ctype;
1073 const char *typediff;
1075 /* Type types? */
1076 if (is_type_type(ltype) && is_type_type(rtype)) {
1078 * __builtin_types_compatible_p() yields an integer
1079 * constant expression
1081 expr->flags = CEF_SET_ICE;
1082 goto OK;
1085 if (is_safe_type(left->ctype) || is_safe_type(right->ctype))
1086 warning(expr->pos, "testing a 'safe expression'");
1088 expr->flags = left->flags & right->flags & ~CEF_CONST_MASK & ~CEF_ADDR;
1090 /* number on number */
1091 if (lclass & rclass & TYPE_NUM) {
1092 ctype = usual_conversions(expr->op, expr->left, expr->right,
1093 lclass, rclass, ltype, rtype);
1094 expr->left = cast_to(expr->left, ctype);
1095 expr->right = cast_to(expr->right, ctype);
1096 if (ctype->ctype.modifiers & MOD_UNSIGNED)
1097 expr->op = modify_for_unsigned(expr->op);
1098 goto OK;
1101 /* at least one must be a pointer */
1102 if (!((lclass | rclass) & TYPE_PTR))
1103 return bad_expr_type(expr);
1105 /* equality comparisons can be with null pointer constants */
1106 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1107 int is_null1 = is_null_pointer_constant(left);
1108 int is_null2 = is_null_pointer_constant(right);
1109 if (is_null1 == NULL_ZERO)
1110 bad_null(left);
1111 if (is_null2 == NULL_ZERO)
1112 bad_null(right);
1113 if (is_null1 && is_null2) {
1114 int positive = expr->op == SPECIAL_EQUAL;
1115 expr->type = EXPR_VALUE;
1116 expr->value = positive;
1117 goto OK;
1119 if (is_null1 && (rclass & TYPE_PTR)) {
1120 expr->left = cast_to(left, rtype);
1121 goto OK;
1123 if (is_null2 && (lclass & TYPE_PTR)) {
1124 expr->right = cast_to(right, ltype);
1125 goto OK;
1128 /* both should be pointers */
1129 if (!(lclass & rclass & TYPE_PTR))
1130 return bad_expr_type(expr);
1131 expr->op = modify_for_unsigned(expr->op);
1133 lbase = examine_pointer_target(ltype);
1134 rbase = examine_pointer_target(rtype);
1136 /* they also have special treatment for pointers to void */
1137 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1138 if (ltype->ctype.as == rtype->ctype.as) {
1139 if (lbase == &void_ctype) {
1140 expr->right = cast_to(right, ltype);
1141 goto OK;
1143 if (rbase == &void_ctype) {
1144 expr->left = cast_to(left, rtype);
1145 goto OK;
1150 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1151 target_qualifiers(rtype),
1152 target_qualifiers(ltype));
1153 if (!typediff)
1154 goto OK;
1156 expression_error(expr, "incompatible types in comparison expression (%s):", typediff);
1157 info(expr->pos, " %s", show_typename(ltype));
1158 info(expr->pos, " %s", show_typename(rtype));
1159 return NULL;
1162 /* the result is int [6.5.8(6), 6.5.9(3)]*/
1163 expr->ctype = &int_ctype;
1164 return &int_ctype;
1168 * NOTE! The degenerate case of "x ? : y", where we don't
1169 * have a true case, this will possibly promote "x" to the
1170 * same type as "y", and thus _change_ the conditional
1171 * test in the expression. But since promotion is "safe"
1172 * for testing, that's OK.
1174 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1176 struct expression **cond;
1177 struct symbol *ctype, *ltype, *rtype, *lbase, *rbase;
1178 int lclass, rclass;
1179 const char * typediff;
1180 int qual;
1182 if (!evaluate_conditional(expr->conditional, 0))
1183 return NULL;
1184 if (!evaluate_expression(expr->cond_false))
1185 return NULL;
1187 ctype = degenerate(expr->conditional);
1188 rtype = degenerate(expr->cond_false);
1190 cond = &expr->conditional;
1191 ltype = ctype;
1192 if (expr->cond_true) {
1193 if (!evaluate_expression(expr->cond_true))
1194 return NULL;
1195 ltype = degenerate(expr->cond_true);
1196 cond = &expr->cond_true;
1199 expr->flags = (expr->conditional->flags & (*cond)->flags &
1200 expr->cond_false->flags & ~CEF_CONST_MASK);
1202 * In the standard, it is defined that an integer constant expression
1203 * shall only have operands that are themselves constant [6.6(6)].
1204 * While this definition is very clear for expressions that need all
1205 * their operands to be evaluated, for conditional expressions with a
1206 * constant condition things are much less obvious.
1207 * So, as an extension, do the same as GCC seems to do:
1208 * Consider a conditional expression with a constant condition
1209 * as having the same constantness as the argument corresponding
1210 * to the truth value (including in the case of address constants
1211 * which are defined more stricly [6.6(9)]).
1213 if (expr->conditional->flags & (CEF_ACE | CEF_ADDR)) {
1214 int is_true = expr_truth_value(expr->conditional);
1215 struct expression *arg = is_true ? *cond : expr->cond_false;
1216 expr->flags = arg->flags & ~CEF_CONST_MASK;
1219 lclass = classify_type(ltype, &ltype);
1220 rclass = classify_type(rtype, &rtype);
1221 if (lclass & rclass & TYPE_NUM) {
1222 ctype = usual_conversions('?', *cond, expr->cond_false,
1223 lclass, rclass, ltype, rtype);
1224 *cond = cast_to(*cond, ctype);
1225 expr->cond_false = cast_to(expr->cond_false, ctype);
1226 goto out;
1229 if ((lclass | rclass) & TYPE_PTR) {
1230 int is_null1 = is_null_pointer_constant(*cond);
1231 int is_null2 = is_null_pointer_constant(expr->cond_false);
1233 if (is_null1 && is_null2) {
1234 *cond = cast_to(*cond, &ptr_ctype);
1235 expr->cond_false = cast_to(expr->cond_false, &ptr_ctype);
1236 ctype = &ptr_ctype;
1237 goto out;
1239 if (is_null1 && (rclass & TYPE_PTR)) {
1240 if (is_null1 == NULL_ZERO)
1241 bad_null(*cond);
1242 *cond = cast_to(*cond, rtype);
1243 ctype = rtype;
1244 goto out;
1246 if (is_null2 && (lclass & TYPE_PTR)) {
1247 if (is_null2 == NULL_ZERO)
1248 bad_null(expr->cond_false);
1249 expr->cond_false = cast_to(expr->cond_false, ltype);
1250 ctype = ltype;
1251 goto out;
1253 if (!(lclass & rclass & TYPE_PTR)) {
1254 typediff = "different types";
1255 goto Err;
1257 /* OK, it's pointer on pointer */
1258 if (ltype->ctype.as != rtype->ctype.as) {
1259 typediff = "different address spaces";
1260 goto Err;
1263 /* need to be lazier here */
1264 lbase = examine_pointer_target(ltype);
1265 rbase = examine_pointer_target(rtype);
1266 qual = target_qualifiers(ltype) | target_qualifiers(rtype);
1268 if (lbase == &void_ctype) {
1269 /* XXX: pointers to function should warn here */
1270 ctype = ltype;
1271 goto Qual;
1274 if (rbase == &void_ctype) {
1275 /* XXX: pointers to function should warn here */
1276 ctype = rtype;
1277 goto Qual;
1279 /* XXX: that should be pointer to composite */
1280 ctype = ltype;
1281 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1282 qual, qual);
1283 if (!typediff)
1284 goto Qual;
1285 goto Err;
1288 /* void on void, struct on same struct, union on same union */
1289 if (ltype == rtype) {
1290 ctype = ltype;
1291 goto out;
1293 typediff = "different base types";
1295 Err:
1296 expression_error(expr, "incompatible types in conditional expression (%s):", typediff);
1297 info(expr->pos, " %s", show_typename(ltype));
1298 info(expr->pos, " %s", show_typename(rtype));
1300 * if the condition is constant, the type is in fact known
1301 * so use it, as gcc & clang do.
1303 switch (expr_truth_value(expr->conditional)) {
1304 case 1: expr->ctype = ltype;
1305 break;
1306 case 0: expr->ctype = rtype;
1307 break;
1308 default:
1309 break;
1311 return NULL;
1313 out:
1314 expr->ctype = ctype;
1315 return ctype;
1317 Qual:
1318 if (qual & ~ctype->ctype.modifiers) {
1319 struct symbol *sym = alloc_symbol(ctype->pos, SYM_PTR);
1320 *sym = *ctype;
1321 sym->ctype.modifiers |= qual;
1322 ctype = sym;
1324 *cond = cast_to(*cond, ctype);
1325 expr->cond_false = cast_to(expr->cond_false, ctype);
1326 goto out;
1329 /* FP assignments can not do modulo or bit operations */
1330 static int compatible_float_op(int op)
1332 return op == SPECIAL_ADD_ASSIGN ||
1333 op == SPECIAL_SUB_ASSIGN ||
1334 op == SPECIAL_MUL_ASSIGN ||
1335 op == SPECIAL_DIV_ASSIGN;
1338 static int evaluate_assign_op(struct expression *expr)
1340 struct symbol *target = expr->left->ctype;
1341 struct symbol *source = expr->right->ctype;
1342 struct symbol *t, *s;
1343 int tclass = classify_type(target, &t);
1344 int sclass = classify_type(source, &s);
1345 int op = expr->op;
1347 if (tclass & sclass & TYPE_NUM) {
1348 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1349 expression_error(expr, "invalid assignment");
1350 return 0;
1352 if (tclass & TYPE_RESTRICT) {
1353 if (!restricted_binop(op, t)) {
1354 warning(expr->pos, "bad assignment (%s) to %s",
1355 show_special(op), show_typename(t));
1356 expr->right = cast_to(expr->right, target);
1357 return 0;
1359 /* allowed assignments unfoul */
1360 if (sclass & TYPE_FOULED && unfoul(s) == t)
1361 goto Cast;
1362 if (!restricted_value(expr->right, t))
1363 return 1;
1364 } else if (op == SPECIAL_SHR_ASSIGN || op == SPECIAL_SHL_ASSIGN) {
1365 // shifts do integer promotions, but that's it.
1366 unrestrict(expr->left, tclass, &t);
1367 target = integer_promotion(t);
1369 unrestrict(expr->right, sclass, &s);
1370 source = integer_promotion(s);
1371 expr->right = cast_to(expr->right, source);
1373 // both gcc & clang seems to do this, so ...
1374 if (target->bit_size > source->bit_size)
1375 expr->right = cast_to(expr->right, &uint_ctype);
1377 goto Cast;
1378 } else if (!(sclass & TYPE_RESTRICT))
1379 goto usual;
1380 /* source and target would better be identical restricted */
1381 if (t == s)
1382 return 1;
1383 warning(expr->pos, "invalid assignment: %s", show_special(op));
1384 info(expr->pos, " left side has type %s", show_typename(t));
1385 info(expr->pos, " right side has type %s", show_typename(s));
1386 expr->right = cast_to(expr->right, target);
1387 return 0;
1389 if (tclass == TYPE_PTR && is_int(sclass)) {
1390 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1391 unrestrict(expr->right, sclass, &s);
1392 evaluate_ptr_add(expr, s);
1393 return 1;
1395 expression_error(expr, "invalid pointer assignment");
1396 return 0;
1399 expression_error(expr, "invalid assignment");
1400 return 0;
1402 usual:
1403 target = usual_conversions(op, expr->left, expr->right,
1404 tclass, sclass, target, source);
1405 Cast:
1406 expr->right = cast_to(expr->right, target);
1407 return 1;
1410 static int whitelist_pointers(struct symbol *t1, struct symbol *t2)
1412 if (t1 == t2)
1413 return 0; /* yes, 0 - we don't want a cast_to here */
1414 if (t1 == &void_ctype)
1415 return 1;
1416 if (t2 == &void_ctype)
1417 return 1;
1418 if (classify_type(t1, &t1) != TYPE_NUM)
1419 return 0;
1420 if (classify_type(t2, &t2) != TYPE_NUM)
1421 return 0;
1422 if (t1 == t2)
1423 return 1;
1424 if (t1->rank == -2 && t2->rank == -2)
1425 return 1;
1426 if (t1->rank != t2->rank)
1427 return 0;
1428 return !Wtypesign;
1431 static int check_assignment_types(struct symbol *target, struct expression **rp,
1432 const char **typediff)
1434 struct symbol *source = degenerate(*rp);
1435 struct symbol *t, *s;
1436 int tclass = classify_type(target, &t);
1437 int sclass = classify_type(source, &s);
1439 if (tclass & sclass & TYPE_NUM) {
1440 if (tclass & TYPE_RESTRICT) {
1441 /* allowed assignments unfoul */
1442 if (sclass & TYPE_FOULED && unfoul(s) == t)
1443 goto Cast;
1444 if (!restricted_value(*rp, target))
1445 return 1;
1446 if (s == t)
1447 return 1;
1448 } else if (!(sclass & TYPE_RESTRICT))
1449 goto Cast;
1450 if (t == &bool_ctype) {
1451 if (is_fouled_type(s))
1452 warning((*rp)->pos, "%s degrades to integer",
1453 show_typename(s->ctype.base_type));
1454 goto Cast;
1456 *typediff = "different base types";
1457 return 0;
1460 if (tclass == TYPE_PTR) {
1461 unsigned long mod1, mod2;
1462 unsigned long modl, modr;
1463 struct symbol *b1, *b2;
1464 // NULL pointer is always OK
1465 int is_null = is_null_pointer_constant(*rp);
1466 if (is_null) {
1467 if (is_null == NULL_ZERO)
1468 bad_null(*rp);
1469 goto Cast;
1471 if (!(sclass & TYPE_PTR)) {
1472 *typediff = "different base types";
1473 return 0;
1475 b1 = examine_pointer_target(t);
1476 b2 = examine_pointer_target(s);
1477 mod1 = t->ctype.modifiers & MOD_IGN;
1478 mod2 = s->ctype.modifiers & MOD_IGN;
1479 if (whitelist_pointers(b1, b2)) {
1481 * assignments to/from void * are OK, provided that
1482 * we do not remove qualifiers from pointed to [C]
1483 * or mix address spaces [sparse].
1485 if (t->ctype.as != s->ctype.as) {
1486 *typediff = "different address spaces";
1487 return 0;
1490 * If this is a function pointer assignment, it is
1491 * actually fine to assign a pointer to const data to
1492 * it, as a function pointer points to const data
1493 * implicitly, i.e., dereferencing it does not produce
1494 * an lvalue.
1496 if (b1->type == SYM_FN)
1497 mod1 |= MOD_CONST;
1498 if (mod2 & ~mod1 & ~MOD_FUN_ATTR) {
1499 *typediff = "different modifiers";
1500 return 0;
1502 goto Cast;
1504 /* It's OK if the target is more volatile or const than the source */
1505 /* It's OK if the source is more pure/noreturn than the target */
1506 modr = mod1 & ~MOD_REV_QUAL;
1507 modl = mod2 & MOD_REV_QUAL;
1508 *typediff = type_difference(&t->ctype, &s->ctype, modl, modr);
1509 if (*typediff)
1510 return 0;
1511 return 1;
1514 if ((tclass & TYPE_COMPOUND) && s == t)
1515 return 1;
1517 if (tclass & TYPE_NUM) {
1518 /* XXX: need to turn into comparison with NULL */
1519 if (t == &bool_ctype && (sclass & TYPE_PTR))
1520 goto Cast;
1521 *typediff = "different base types";
1522 return 0;
1524 *typediff = "invalid types";
1525 return 0;
1527 Cast:
1528 *rp = cast_to(*rp, target);
1529 return 1;
1532 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1533 struct expression **rp, const char *where)
1535 const char *typediff;
1537 if (!check_assignment_types(target, rp, &typediff)) {
1538 struct symbol *source = *rp ? (*rp)->ctype : NULL;
1539 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1540 info(expr->pos, " expected %s", show_typename(target));
1541 info(expr->pos, " got %s", show_typename(source));
1542 *rp = cast_to(*rp, target);
1543 return 0;
1546 return 1;
1549 static int compatible_transparent_union(struct symbol *target,
1550 struct expression **rp)
1552 struct symbol *t, *member;
1553 classify_type(target, &t);
1554 if (t->type != SYM_UNION || !t->transparent_union)
1555 return 0;
1557 FOR_EACH_PTR(t->symbol_list, member) {
1558 const char *typediff;
1559 if (check_assignment_types(member, rp, &typediff))
1560 return 1;
1561 } END_FOR_EACH_PTR(member);
1563 return 0;
1566 static int compatible_argument_type(struct expression *expr, struct symbol *target,
1567 struct expression **rp, const char *where)
1569 if (compatible_transparent_union(target, rp))
1570 return 1;
1572 return compatible_assignment_types(expr, target, rp, where);
1575 static void mark_addressable(struct expression *expr)
1577 while (expr->type == EXPR_BINOP && expr->op == '+')
1578 expr = expr->left;
1579 if (expr->type == EXPR_SYMBOL) {
1580 struct symbol *sym = expr->symbol;
1581 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1585 static void mark_assigned(struct expression *expr)
1587 struct symbol *sym;
1589 if (!expr)
1590 return;
1591 switch (expr->type) {
1592 case EXPR_SYMBOL:
1593 sym = expr->symbol;
1594 if (!sym)
1595 return;
1596 if (sym->type != SYM_NODE)
1597 return;
1598 sym->ctype.modifiers |= MOD_ASSIGNED;
1599 return;
1601 case EXPR_BINOP:
1602 mark_assigned(expr->left);
1603 mark_assigned(expr->right);
1604 return;
1605 case EXPR_CAST:
1606 case EXPR_FORCE_CAST:
1607 mark_assigned(expr->cast_expression);
1608 return;
1609 case EXPR_SLICE:
1610 mark_assigned(expr->base);
1611 return;
1612 default:
1613 /* Hmm? */
1614 return;
1618 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1620 if (type->ctype.modifiers & MOD_CONST)
1621 expression_error(left, "assignment to const expression");
1623 /* We know left is an lvalue, so it's a "preop-*" */
1624 mark_assigned(left->unop);
1627 static struct symbol *evaluate_assignment(struct expression *expr)
1629 struct expression *left = expr->left;
1630 struct symbol *ltype;
1632 if (!lvalue_expression(left)) {
1633 expression_error(expr, "not an lvalue");
1634 return NULL;
1637 ltype = left->ctype;
1639 if (expr->op != '=') {
1640 if (!evaluate_assign_op(expr))
1641 return NULL;
1642 } else {
1643 if (!compatible_assignment_types(expr, ltype, &expr->right, "assignment"))
1644 return NULL;
1647 evaluate_assign_to(left, ltype);
1649 expr->ctype = ltype;
1650 return ltype;
1653 static void examine_fn_arguments(struct symbol *fn)
1655 struct symbol *s;
1657 FOR_EACH_PTR(fn->arguments, s) {
1658 struct symbol *arg = evaluate_symbol(s);
1659 /* Array/function arguments silently degenerate into pointers */
1660 if (arg) {
1661 struct symbol *ptr;
1662 switch(arg->type) {
1663 case SYM_ARRAY:
1664 case SYM_FN:
1665 ptr = alloc_symbol(s->pos, SYM_PTR);
1666 if (arg->type == SYM_ARRAY)
1667 ptr->ctype = arg->ctype;
1668 else
1669 ptr->ctype.base_type = arg;
1670 combine_address_space(s->pos, &ptr->ctype.as, s->ctype.as);
1671 ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1673 s->ctype.base_type = ptr;
1674 s->ctype.as = NULL;
1675 s->ctype.modifiers &= ~MOD_PTRINHERIT;
1676 s->bit_size = 0;
1677 s->examined = 0;
1678 examine_symbol_type(s);
1679 break;
1680 default:
1681 /* nothing */
1682 break;
1685 } END_FOR_EACH_PTR(s);
1688 static struct symbol *convert_to_as_mod(struct symbol *sym, struct ident *as, int mod)
1690 /* Take the modifiers of the pointer, and apply them to the member */
1691 mod |= sym->ctype.modifiers;
1692 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1693 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1694 *newsym = *sym;
1695 newsym->ctype.as = as;
1696 newsym->ctype.modifiers = mod;
1697 sym = newsym;
1699 return sym;
1702 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1704 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1705 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1707 node->ctype.base_type = ptr;
1708 ptr->bit_size = bits_in_pointer;
1709 ptr->ctype.alignment = pointer_alignment;
1711 node->bit_size = bits_in_pointer;
1712 node->ctype.alignment = pointer_alignment;
1714 access_symbol(sym);
1715 if (sym->ctype.modifiers & MOD_REGISTER) {
1716 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1717 sym->ctype.modifiers &= ~MOD_REGISTER;
1719 if (sym->type == SYM_NODE) {
1720 combine_address_space(sym->pos, &ptr->ctype.as, sym->ctype.as);
1721 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1722 sym = sym->ctype.base_type;
1724 if (degenerate && sym->type == SYM_ARRAY) {
1725 combine_address_space(sym->pos, &ptr->ctype.as, sym->ctype.as);
1726 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1727 sym = sym->ctype.base_type;
1729 ptr->ctype.base_type = sym;
1731 return node;
1734 /* Arrays degenerate into pointers on pointer arithmetic */
1735 static struct symbol *degenerate(struct expression *expr)
1737 struct symbol *ctype, *base;
1739 if (!expr)
1740 return NULL;
1741 ctype = expr->ctype;
1742 if (!ctype)
1743 return NULL;
1744 base = examine_symbol_type(ctype);
1745 if (ctype->type == SYM_NODE)
1746 base = ctype->ctype.base_type;
1748 * Arrays degenerate into pointers to the entries, while
1749 * functions degenerate into pointers to themselves.
1750 * If array was part of non-lvalue compound, we create a copy
1751 * of that compound first and then act as if we were dealing with
1752 * the corresponding field in there.
1754 switch (base->type) {
1755 case SYM_ARRAY:
1756 if (expr->type == EXPR_SLICE) {
1757 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1758 struct expression *e0, *e1, *e2, *e3, *e4;
1760 a->ctype.base_type = expr->base->ctype;
1761 a->bit_size = expr->base->ctype->bit_size;
1762 a->array_size = expr->base->ctype->array_size;
1764 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1765 e0->symbol = a;
1766 e0->ctype = &lazy_ptr_ctype;
1768 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1769 e1->unop = e0;
1770 e1->op = '*';
1771 e1->ctype = expr->base->ctype; /* XXX */
1773 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1774 e2->left = e1;
1775 e2->right = expr->base;
1776 e2->op = '=';
1777 e2->ctype = expr->base->ctype;
1779 if (expr->r_bitpos) {
1780 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1781 e3->op = '+';
1782 e3->left = e0;
1783 e3->right = alloc_const_expression(expr->pos,
1784 bits_to_bytes(expr->r_bitpos));
1785 e3->ctype = &lazy_ptr_ctype;
1786 } else {
1787 e3 = e0;
1790 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1791 e4->left = e2;
1792 e4->right = e3;
1793 e4->ctype = &lazy_ptr_ctype;
1795 expr->unop = e4;
1796 expr->type = EXPR_PREOP;
1797 expr->op = '*';
1799 case SYM_FN:
1800 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1801 expression_error(expr, "strange non-value function or array");
1802 return &bad_ctype;
1804 if (ctype->builtin)
1805 sparse_error(expr->pos, "taking the address of built-in function '%s'", show_ident(ctype->ident));
1806 *expr = *expr->unop;
1807 ctype = create_pointer(expr, ctype, 1);
1808 expr->ctype = ctype;
1809 mark_addressable(expr);
1810 default:
1811 /* nothing */;
1813 return ctype;
1816 static struct symbol *evaluate_addressof(struct expression *expr)
1818 struct expression *op = expr->unop;
1819 struct symbol *ctype;
1821 if (op->op != '*' || op->type != EXPR_PREOP) {
1822 expression_error(expr, "not addressable");
1823 return NULL;
1825 ctype = op->ctype;
1826 if (ctype->builtin)
1827 sparse_error(expr->pos, "taking the address of built-in function '%s'", show_ident(ctype->ident));
1828 *expr = *op->unop;
1830 mark_addressable(expr);
1833 * symbol expression evaluation is lazy about the type
1834 * of the sub-expression, so we may have to generate
1835 * the type here if so..
1837 if (expr->ctype == &lazy_ptr_ctype) {
1838 ctype = create_pointer(expr, ctype, 0);
1839 expr->ctype = ctype;
1841 return expr->ctype;
1845 static struct symbol *evaluate_dereference(struct expression *expr)
1847 struct expression *op = expr->unop;
1848 struct symbol *ctype = op->ctype, *node, *target;
1850 /* Simplify: *&(expr) => (expr) */
1851 if (op->type == EXPR_PREOP && op->op == '&') {
1852 *expr = *op->unop;
1853 expr->flags = CEF_NONE;
1854 return expr->ctype;
1857 examine_symbol_type(ctype);
1859 /* Dereferencing a node drops all the node information. */
1860 if (ctype->type == SYM_NODE)
1861 ctype = ctype->ctype.base_type;
1863 target = ctype->ctype.base_type;
1865 switch (ctype->type) {
1866 default:
1867 expression_error(expr, "cannot dereference this type");
1868 return NULL;
1869 case SYM_FN:
1870 *expr = *op;
1871 return expr->ctype;
1872 case SYM_PTR:
1873 examine_symbol_type(target);
1874 node = alloc_symbol(expr->pos, SYM_NODE);
1875 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1876 merge_type(node, ctype);
1877 break;
1879 case SYM_ARRAY:
1880 if (!lvalue_expression(op)) {
1881 expression_error(op, "non-lvalue array??");
1882 return NULL;
1885 /* Do the implied "addressof" on the array */
1886 *op = *op->unop;
1889 * When an array is dereferenced, we need to pick
1890 * up the attributes of the original node too..
1892 node = alloc_symbol(expr->pos, SYM_NODE);
1893 merge_type(node, op->ctype);
1894 merge_type(node, ctype);
1895 break;
1898 node->bit_size = target->bit_size;
1899 node->array_size = target->array_size;
1901 expr->ctype = node;
1902 return node;
1906 * Unary post-ops: x++ and x--
1908 static struct symbol *evaluate_postop(struct expression *expr)
1910 struct expression *op = expr->unop;
1911 struct symbol *ctype = op->ctype;
1912 int class = classify_type(ctype, &ctype);
1913 int multiply = 0;
1915 if (!class || class & TYPE_COMPOUND) {
1916 expression_error(expr, "need scalar for ++/--");
1917 return NULL;
1919 if (!lvalue_expression(expr->unop)) {
1920 expression_error(expr, "need lvalue expression for ++/--");
1921 return NULL;
1924 unrestrict(expr, class, &ctype);
1926 if (class & TYPE_NUM) {
1927 multiply = 1;
1928 } else if (class == TYPE_PTR) {
1929 struct symbol *target = examine_pointer_target(ctype);
1930 if (!is_function(target))
1931 multiply = bits_to_bytes(target->bit_size);
1934 if (multiply) {
1935 evaluate_assign_to(op, op->ctype);
1936 expr->op_value = multiply;
1937 expr->ctype = ctype;
1938 return ctype;
1941 expression_error(expr, "bad argument type for ++/--");
1942 return NULL;
1945 static struct symbol *evaluate_sign(struct expression *expr)
1947 struct symbol *ctype = expr->unop->ctype;
1948 int class = classify_type(ctype, &ctype);
1949 unsigned char flags = expr->unop->flags & ~CEF_CONST_MASK;
1951 /* should be an arithmetic type */
1952 if (!(class & TYPE_NUM))
1953 return bad_expr_type(expr);
1954 if (class & TYPE_RESTRICT)
1955 goto Restr;
1956 Normal:
1957 if (!(class & TYPE_FLOAT)) {
1958 ctype = integer_promotion(ctype);
1959 expr->unop = cast_to(expr->unop, ctype);
1960 } else if (expr->op != '~') {
1961 /* no conversions needed */
1962 } else {
1963 return bad_expr_type(expr);
1965 if (expr->op == '+')
1966 *expr = *expr->unop;
1967 expr->flags = flags;
1968 expr->ctype = ctype;
1969 return ctype;
1970 Restr:
1971 if (restricted_unop(expr->op, &ctype))
1972 unrestrict(expr, class, &ctype);
1973 goto Normal;
1976 static struct symbol *evaluate_preop(struct expression *expr)
1978 struct symbol *ctype = expr->unop->ctype;
1980 switch (expr->op) {
1981 case '(':
1982 *expr = *expr->unop;
1983 return ctype;
1985 case '+':
1986 case '-':
1987 case '~':
1988 return evaluate_sign(expr);
1990 case '*':
1991 return evaluate_dereference(expr);
1993 case '&':
1994 return evaluate_addressof(expr);
1996 case SPECIAL_INCREMENT:
1997 case SPECIAL_DECREMENT:
1999 * From a type evaluation standpoint the preops are
2000 * the same as the postops
2002 return evaluate_postop(expr);
2004 case '!':
2005 ctype = degenerate(expr->unop);
2006 expr->flags = expr->unop->flags & ~CEF_CONST_MASK;
2008 * A logical negation never yields an address constant
2009 * [6.6(9)].
2011 expr->flags &= ~CEF_ADDR;
2013 if (is_safe_type(ctype))
2014 warning(expr->pos, "testing a 'safe expression'");
2015 if (is_float_type(ctype)) {
2016 struct expression *arg = expr->unop;
2017 expr->type = EXPR_COMPARE;
2018 expr->op = SPECIAL_EQUAL;
2019 expr->left = arg;
2020 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
2021 expr->right->ctype = ctype;
2022 expr->right->fvalue = 0;
2023 } else if (is_fouled_type(ctype)) {
2024 warning(expr->pos, "%s degrades to integer",
2025 show_typename(ctype->ctype.base_type));
2027 /* the result is int [6.5.3.3(5)]*/
2028 ctype = &int_ctype;
2029 break;
2031 default:
2032 break;
2034 expr->ctype = ctype;
2035 return ctype;
2038 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
2040 struct ptr_list *head = (struct ptr_list *)_list;
2041 struct ptr_list *list = head;
2043 if (!head)
2044 return NULL;
2045 do {
2046 int i;
2047 for (i = 0; i < list->nr; i++) {
2048 struct symbol *sym = (struct symbol *) list->list[i];
2049 if (sym->ident) {
2050 if (sym->ident != ident)
2051 continue;
2052 *offset = sym->offset;
2053 return sym;
2054 } else {
2055 struct symbol *ctype = sym->ctype.base_type;
2056 struct symbol *sub;
2057 if (!ctype)
2058 continue;
2059 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
2060 continue;
2061 sub = find_identifier(ident, ctype->symbol_list, offset);
2062 if (!sub)
2063 continue;
2064 *offset += sym->offset;
2065 return sub;
2068 } while ((list = list->next) != head);
2069 return NULL;
2072 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
2074 struct expression *add;
2077 * Create a new add-expression
2079 * NOTE! Even if we just add zero, we need a new node
2080 * for the member pointer, since it has a different
2081 * type than the original pointer. We could make that
2082 * be just a cast, but the fact is, a node is a node,
2083 * so we might as well just do the "add zero" here.
2085 add = alloc_expression(expr->pos, EXPR_BINOP);
2086 add->op = '+';
2087 add->left = expr;
2088 add->right = alloc_expression(expr->pos, EXPR_VALUE);
2089 add->right->ctype = &int_ctype;
2090 add->right->value = offset;
2093 * The ctype of the pointer will be lazily evaluated if
2094 * we ever take the address of this member dereference..
2096 add->ctype = &lazy_ptr_ctype;
2098 * The resulting address of a member access through an address
2099 * constant is an address constant again [6.6(9)].
2101 add->flags = expr->flags;
2103 return add;
2106 /* structure/union dereference */
2107 static struct symbol *evaluate_member_dereference(struct expression *expr)
2109 int offset;
2110 struct symbol *ctype, *member;
2111 struct expression *deref = expr->deref, *add;
2112 struct ident *ident = expr->member;
2113 struct ident *address_space;
2114 unsigned int mod;
2116 if (!evaluate_expression(deref))
2117 return NULL;
2118 if (!ident) {
2119 expression_error(expr, "bad member name");
2120 return NULL;
2123 ctype = deref->ctype;
2124 examine_symbol_type(ctype);
2125 address_space = ctype->ctype.as;
2126 mod = ctype->ctype.modifiers;
2127 if (ctype->type == SYM_NODE) {
2128 ctype = ctype->ctype.base_type;
2129 combine_address_space(deref->pos, &address_space, ctype->ctype.as);
2130 mod |= ctype->ctype.modifiers;
2132 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
2133 expression_error(expr, "expected structure or union");
2134 return NULL;
2136 offset = 0;
2137 member = find_identifier(ident, ctype->symbol_list, &offset);
2138 if (!member) {
2139 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
2140 const char *name = "<unnamed>";
2141 int namelen = 9;
2142 if (ctype->ident) {
2143 name = ctype->ident->name;
2144 namelen = ctype->ident->len;
2146 if (ctype->symbol_list)
2147 expression_error(expr, "no member '%s' in %s %.*s",
2148 show_ident(ident), type, namelen, name);
2149 else
2150 expression_error(expr, "using member '%s' in "
2151 "incomplete %s %.*s", show_ident(ident),
2152 type, namelen, name);
2153 return NULL;
2157 * The member needs to take on the address space and modifiers of
2158 * the "parent" type.
2160 member = convert_to_as_mod(member, address_space, mod);
2161 ctype = get_base_type(member);
2163 if (!lvalue_expression(deref)) {
2164 if (deref->type != EXPR_SLICE) {
2165 expr->base = deref;
2166 expr->r_bitpos = 0;
2167 } else {
2168 expr->base = deref->base;
2169 expr->r_bitpos = deref->r_bitpos;
2171 expr->r_bitpos += bytes_to_bits(offset);
2172 expr->type = EXPR_SLICE;
2173 expr->r_bitpos += member->bit_offset;
2174 expr->ctype = member;
2175 return member;
2178 deref = deref->unop;
2179 expr->deref = deref;
2181 add = evaluate_offset(deref, offset);
2182 expr->type = EXPR_PREOP;
2183 expr->op = '*';
2184 expr->unop = add;
2186 expr->ctype = member;
2187 return member;
2190 static int is_promoted(struct expression *expr)
2192 while (1) {
2193 switch (expr->type) {
2194 case EXPR_BINOP:
2195 case EXPR_SELECT:
2196 case EXPR_CONDITIONAL:
2197 return 1;
2198 case EXPR_COMMA:
2199 expr = expr->right;
2200 continue;
2201 case EXPR_PREOP:
2202 switch (expr->op) {
2203 case '(':
2204 expr = expr->unop;
2205 continue;
2206 case '+':
2207 case '-':
2208 case '~':
2209 return 1;
2210 default:
2211 return 0;
2213 default:
2214 return 0;
2220 static struct symbol *evaluate_type_information(struct expression *expr)
2222 struct symbol *sym = expr->cast_type;
2223 if (!sym) {
2224 sym = evaluate_expression(expr->cast_expression);
2225 if (!sym)
2226 return NULL;
2228 * Expressions of restricted types will possibly get
2229 * promoted - check that here
2231 if (is_restricted_type(sym)) {
2232 if (sym->bit_size < bits_in_int && is_promoted(expr))
2233 sym = &int_ctype;
2234 } else if (is_fouled_type(sym)) {
2235 sym = &int_ctype;
2238 examine_symbol_type(sym);
2239 if (is_bitfield_type(sym)) {
2240 expression_error(expr, "trying to examine bitfield type");
2241 return NULL;
2243 return sym;
2246 static struct symbol *evaluate_sizeof(struct expression *expr)
2248 struct symbol *type;
2249 int size;
2251 type = evaluate_type_information(expr);
2252 if (!type)
2253 return NULL;
2255 size = type->bit_size;
2257 if (size < 0 && is_void_type(type)) {
2258 if (Wpointer_arith)
2259 warning(expr->pos, "expression using sizeof(void)");
2260 size = bits_in_char;
2263 if (is_bool_type(type)) {
2264 if (Wsizeof_bool)
2265 warning(expr->pos, "expression using sizeof _Bool");
2266 size = bits_to_bytes(bits_in_bool) * bits_in_char;
2269 if (is_function(type->ctype.base_type)) {
2270 if (Wpointer_arith)
2271 warning(expr->pos, "expression using sizeof on a function");
2272 size = bits_in_char;
2275 if (has_flexible_array(type) && Wflexible_array_sizeof)
2276 warning(expr->pos, "using sizeof on a flexible structure");
2278 if (is_array_type(type) && size < 0) { // VLA, 1-dimension only
2279 struct expression *base, *size;
2280 struct symbol *base_type;
2282 if (type->type == SYM_NODE)
2283 type = type->ctype.base_type; // strip the SYM_NODE
2284 base_type = get_base_type(type);
2285 if (!base_type)
2286 goto error;
2287 if (base_type->bit_size <= 0) {
2288 base = alloc_expression(expr->pos, EXPR_SIZEOF);
2289 base->cast_type = base_type;
2290 if (!evaluate_sizeof(base))
2291 goto error;
2292 } else {
2293 base = alloc_expression(expr->pos, EXPR_VALUE);
2294 base->value = bits_to_bytes(base_type->bit_size);
2295 base->ctype = size_t_ctype;
2297 size = alloc_expression(expr->pos, EXPR_CAST);
2298 size->cast_type = size_t_ctype;
2299 size->cast_expression = type->array_size;
2300 if (!evaluate_expression(size))
2301 goto error;
2302 expr->left = size;
2303 expr->right = base;
2304 expr->type = EXPR_BINOP;
2305 expr->op = '*';
2306 return expr->ctype = size_t_ctype;
2309 error:
2310 if ((size < 0) || (size & (bits_in_char - 1)))
2311 expression_error(expr, "cannot size expression");
2313 expr->type = EXPR_VALUE;
2314 expr->value = bits_to_bytes(size);
2315 expr->taint = 0;
2316 expr->ctype = size_t_ctype;
2317 return size_t_ctype;
2320 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
2322 struct symbol *type;
2323 int size;
2325 type = evaluate_type_information(expr);
2326 if (!type)
2327 return NULL;
2329 if (type->type == SYM_NODE)
2330 type = type->ctype.base_type;
2331 if (!type)
2332 return NULL;
2333 switch (type->type) {
2334 case SYM_ARRAY:
2335 break;
2336 case SYM_PTR:
2337 type = get_base_type(type);
2338 if (type)
2339 break;
2340 default:
2341 expression_error(expr, "expected pointer expression");
2342 return NULL;
2344 size = type->bit_size;
2345 if (size & (bits_in_char-1))
2346 size = 0;
2347 expr->type = EXPR_VALUE;
2348 expr->value = bits_to_bytes(size);
2349 expr->taint = 0;
2350 expr->ctype = size_t_ctype;
2351 return size_t_ctype;
2354 static struct symbol *evaluate_alignof(struct expression *expr)
2356 struct symbol *type;
2358 type = evaluate_type_information(expr);
2359 if (!type)
2360 return NULL;
2362 expr->type = EXPR_VALUE;
2363 expr->value = type->ctype.alignment;
2364 expr->taint = 0;
2365 expr->ctype = size_t_ctype;
2366 return size_t_ctype;
2369 int evaluate_arguments(struct symbol_list *argtypes, struct expression_list *head)
2371 struct expression *expr;
2372 struct symbol *argtype;
2373 int i = 1;
2375 PREPARE_PTR_LIST(argtypes, argtype);
2376 FOR_EACH_PTR (head, expr) {
2377 struct expression **p = THIS_ADDRESS(expr);
2378 struct symbol *ctype, *target;
2379 ctype = evaluate_expression(expr);
2381 if (!ctype)
2382 return 0;
2384 target = argtype;
2385 if (!target) {
2386 struct symbol *type;
2387 int class = classify_type(ctype, &type);
2388 if (is_int(class)) {
2389 *p = cast_to(expr, integer_promotion(type));
2390 } else if (class & TYPE_FLOAT) {
2391 if (type->rank < 0)
2392 *p = cast_to(expr, &double_ctype);
2393 } else if (class & TYPE_PTR) {
2394 if (expr->ctype == &null_ctype)
2395 *p = cast_to(expr, &ptr_ctype);
2396 else
2397 degenerate(expr);
2399 } else if (!target->forced_arg){
2400 static char where[30];
2401 examine_symbol_type(target);
2402 sprintf(where, "argument %d", i);
2403 compatible_argument_type(expr, target, p, where);
2406 i++;
2407 NEXT_PTR_LIST(argtype);
2408 } END_FOR_EACH_PTR(expr);
2409 FINISH_PTR_LIST(argtype);
2410 return 1;
2413 static void convert_index(struct expression *e)
2415 struct expression *child = e->idx_expression;
2416 unsigned from = e->idx_from;
2417 unsigned to = e->idx_to + 1;
2418 e->type = EXPR_POS;
2419 e->init_offset = from * bits_to_bytes(e->ctype->bit_size);
2420 e->init_nr = to - from;
2421 e->init_expr = child;
2424 static void convert_ident(struct expression *e)
2426 struct expression *child = e->ident_expression;
2427 int offset = e->offset;
2429 e->type = EXPR_POS;
2430 e->init_offset = offset;
2431 e->init_nr = 1;
2432 e->init_expr = child;
2435 static void convert_designators(struct expression *e)
2437 while (e) {
2438 if (e->type == EXPR_INDEX)
2439 convert_index(e);
2440 else if (e->type == EXPR_IDENTIFIER)
2441 convert_ident(e);
2442 else
2443 break;
2444 e = e->init_expr;
2448 static void excess(struct expression *e, const char *s)
2450 warning(e->pos, "excessive elements in %s initializer", s);
2454 * implicit designator for the first element
2456 static struct expression *first_subobject(struct symbol *ctype, int class,
2457 struct expression **v)
2459 struct expression *e = *v, *new;
2461 if (ctype->type == SYM_NODE)
2462 ctype = ctype->ctype.base_type;
2464 if (class & TYPE_PTR) { /* array */
2465 if (!ctype->bit_size)
2466 return NULL;
2467 new = alloc_expression(e->pos, EXPR_INDEX);
2468 new->idx_expression = e;
2469 new->ctype = ctype->ctype.base_type;
2470 } else {
2471 struct symbol *field, *p;
2472 PREPARE_PTR_LIST(ctype->symbol_list, p);
2473 while (p && !p->ident && is_bitfield_type(p))
2474 NEXT_PTR_LIST(p);
2475 field = p;
2476 FINISH_PTR_LIST(p);
2477 if (!field)
2478 return NULL;
2479 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2480 new->ident_expression = e;
2481 new->field = new->ctype = field;
2482 new->offset = field->offset;
2484 *v = new;
2485 return new;
2489 * sanity-check explicit designators; return the innermost one or NULL
2490 * in case of error. Assign types.
2492 static struct expression *check_designators(struct expression *e,
2493 struct symbol *ctype)
2495 struct expression *last = NULL;
2496 const char *err;
2497 while (1) {
2498 if (ctype->type == SYM_NODE)
2499 ctype = ctype->ctype.base_type;
2500 if (e->type == EXPR_INDEX) {
2501 struct symbol *type;
2502 if (ctype->type != SYM_ARRAY) {
2503 err = "array index in non-array";
2504 break;
2506 type = ctype->ctype.base_type;
2507 if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2508 unsigned offset = array_element_offset(type->bit_size, e->idx_to);
2509 if (offset >= ctype->bit_size) {
2510 err = "index out of bounds in";
2511 break;
2514 e->ctype = ctype = type;
2515 ctype = type;
2516 last = e;
2517 if (!e->idx_expression) {
2518 err = "invalid";
2519 break;
2521 e = e->idx_expression;
2522 } else if (e->type == EXPR_IDENTIFIER) {
2523 int offset = 0;
2524 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2525 err = "field name not in struct or union";
2526 break;
2528 ctype = find_identifier(e->expr_ident, ctype->symbol_list, &offset);
2529 if (!ctype) {
2530 err = "unknown field name in";
2531 break;
2533 e->offset = offset;
2534 e->field = e->ctype = ctype;
2535 last = e;
2536 if (!e->ident_expression) {
2537 err = "invalid";
2538 break;
2540 e = e->ident_expression;
2541 } else if (e->type == EXPR_POS) {
2542 err = "internal front-end error: EXPR_POS in";
2543 break;
2544 } else
2545 return last;
2547 expression_error(e, "%s initializer", err);
2548 return NULL;
2552 * choose the next subobject to initialize.
2554 * Get designators for next element, switch old ones to EXPR_POS.
2555 * Return the resulting expression or NULL if we'd run out of subobjects.
2556 * The innermost designator is returned in *v. Designators in old
2557 * are assumed to be already sanity-checked.
2559 static struct expression *next_designators(struct expression *old,
2560 struct symbol *ctype,
2561 struct expression *e, struct expression **v)
2563 struct expression *new = NULL;
2565 if (!old)
2566 return NULL;
2567 if (old->type == EXPR_INDEX) {
2568 struct expression *copy;
2569 unsigned n;
2571 copy = next_designators(old->idx_expression,
2572 old->ctype, e, v);
2573 if (!copy) {
2574 n = old->idx_to + 1;
2575 if (array_element_offset(old->ctype->bit_size, n) == ctype->bit_size) {
2576 convert_index(old);
2577 return NULL;
2579 copy = e;
2580 *v = new = alloc_expression(e->pos, EXPR_INDEX);
2581 } else {
2582 n = old->idx_to;
2583 new = alloc_expression(e->pos, EXPR_INDEX);
2586 new->idx_from = new->idx_to = n;
2587 new->idx_expression = copy;
2588 new->ctype = old->ctype;
2589 convert_index(old);
2590 } else if (old->type == EXPR_IDENTIFIER) {
2591 struct expression *copy;
2592 struct symbol *field;
2593 int offset = 0;
2595 copy = next_designators(old->ident_expression,
2596 old->ctype, e, v);
2597 if (!copy) {
2598 field = old->field->next_subobject;
2599 if (!field) {
2600 convert_ident(old);
2601 return NULL;
2603 copy = e;
2604 *v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2606 * We can't necessarily trust "field->offset",
2607 * because the field might be in an anonymous
2608 * union, and the field offset is then the offset
2609 * within that union.
2611 * The "old->offset - old->field->offset"
2612 * would be the offset of such an anonymous
2613 * union.
2615 offset = old->offset - old->field->offset;
2616 } else {
2617 field = old->field;
2618 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2621 new->field = field;
2622 new->expr_ident = field->ident;
2623 new->ident_expression = copy;
2624 new->ctype = field;
2625 new->offset = field->offset + offset;
2626 convert_ident(old);
2628 return new;
2631 static int handle_initializer(struct expression **ep, int nested,
2632 int class, struct symbol *ctype, unsigned long mods);
2635 * deal with traversing subobjects [6.7.8(17,18,20)]
2637 static void handle_list_initializer(struct expression *expr,
2638 int class, struct symbol *ctype, unsigned long mods)
2640 struct expression *e, *last = NULL, *top = NULL, *next;
2641 int jumped = 0; // has the last designator multiple levels?
2643 if (expr->zero_init)
2644 free_ptr_list(&expr->expr_list);
2646 FOR_EACH_PTR(expr->expr_list, e) {
2647 struct expression **v;
2648 struct symbol *type;
2649 int lclass;
2651 if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2652 struct symbol *struct_sym;
2653 if (!top) {
2654 top = e;
2655 last = first_subobject(ctype, class, &top);
2656 } else {
2657 last = next_designators(last, ctype, e, &top);
2659 if (!last) {
2660 excess(e, class & TYPE_PTR ? "array" :
2661 "struct or union");
2662 DELETE_CURRENT_PTR(e);
2663 continue;
2665 struct_sym = ctype->type == SYM_NODE ? ctype->ctype.base_type : ctype;
2666 if (Wdesignated_init && struct_sym->designated_init)
2667 warning(e->pos, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2668 ctype->ident ? "in initializer for " : "",
2669 ctype->ident ? ctype->ident->len : 0,
2670 ctype->ident ? ctype->ident->name : "",
2671 ctype->ident ? ": " : "",
2672 get_type_name(struct_sym->type),
2673 show_ident(struct_sym->ident));
2674 if (jumped && Wpast_deep_designator) {
2675 warning(e->pos, "advancing past deep designator");
2676 jumped = 0;
2678 REPLACE_CURRENT_PTR(e, last);
2679 } else {
2680 next = check_designators(e, ctype);
2681 if (!next) {
2682 DELETE_CURRENT_PTR(e);
2683 continue;
2685 top = next;
2686 /* deeper than one designator? */
2687 jumped = top != e;
2688 convert_designators(last);
2689 last = e;
2692 found:
2693 lclass = classify_type(top->ctype, &type);
2694 if (top->type == EXPR_INDEX)
2695 v = &top->idx_expression;
2696 else
2697 v = &top->ident_expression;
2699 mods |= ctype->ctype.modifiers & MOD_STORAGE;
2700 if (handle_initializer(v, 1, lclass, top->ctype, mods))
2701 continue;
2703 if (!(lclass & TYPE_COMPOUND)) {
2704 warning(e->pos, "bogus scalar initializer");
2705 DELETE_CURRENT_PTR(e);
2706 continue;
2709 next = first_subobject(type, lclass, v);
2710 if (next) {
2711 warning(e->pos, "missing braces around initializer");
2712 top = next;
2713 goto found;
2716 DELETE_CURRENT_PTR(e);
2717 excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2719 } END_FOR_EACH_PTR(e);
2721 convert_designators(last);
2722 expr->ctype = ctype;
2725 static int is_string_literal(struct expression **v)
2727 struct expression *e = *v;
2728 while (e && e->type == EXPR_PREOP && e->op == '(')
2729 e = e->unop;
2730 if (!e || e->type != EXPR_STRING)
2731 return 0;
2732 if (e != *v && Wparen_string)
2733 warning(e->pos,
2734 "array initialized from parenthesized string constant");
2735 *v = e;
2736 return 1;
2740 * We want a normal expression, possibly in one layer of braces. Warn
2741 * if the latter happens inside a list (it's legal, but likely to be
2742 * an effect of screwup). In case of anything not legal, we are definitely
2743 * having an effect of screwup, so just fail and let the caller warn.
2745 static struct expression *handle_scalar(struct expression *e, int nested)
2747 struct expression *v = NULL, *p;
2748 int count = 0;
2750 /* normal case */
2751 if (e->type != EXPR_INITIALIZER)
2752 return e;
2754 FOR_EACH_PTR(e->expr_list, p) {
2755 if (!v)
2756 v = p;
2757 count++;
2758 } END_FOR_EACH_PTR(p);
2759 if (count != 1)
2760 return NULL;
2761 switch(v->type) {
2762 case EXPR_INITIALIZER:
2763 case EXPR_INDEX:
2764 case EXPR_IDENTIFIER:
2765 return NULL;
2766 default:
2767 break;
2769 if (nested)
2770 warning(e->pos, "braces around scalar initializer");
2771 return v;
2775 * deal with the cases that don't care about subobjects:
2776 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2777 * character array <- string literal, possibly in braces [6.7.8(14)]
2778 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2779 * compound type <- initializer list in braces [6.7.8(16)]
2780 * The last one punts to handle_list_initializer() which, in turn will call
2781 * us for individual elements of the list.
2783 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2784 * the lack of support of wide char stuff in general.
2786 * One note: we need to take care not to evaluate a string literal until
2787 * we know that we *will* handle it right here. Otherwise we would screw
2788 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2789 * { "string", ...} - we need to preserve that string literal recognizable
2790 * until we dig into the inner struct.
2792 static int handle_initializer(struct expression **ep, int nested,
2793 int class, struct symbol *ctype, unsigned long mods)
2795 struct expression *e = *ep, *p;
2796 struct symbol *type;
2798 if (!e)
2799 return 0;
2801 /* scalar */
2802 if (!(class & TYPE_COMPOUND)) {
2803 e = handle_scalar(e, nested);
2804 if (!e)
2805 return 0;
2806 *ep = e;
2807 if (!evaluate_expression(e))
2808 return 1;
2809 compatible_assignment_types(e, ctype, ep, "initializer");
2811 * Initializers for static storage duration objects
2812 * shall be constant expressions or a string literal [6.7.8(4)].
2814 mods |= ctype->ctype.modifiers;
2815 mods &= (MOD_TOPLEVEL | MOD_STATIC);
2816 if (mods && !(e->flags & (CEF_ACE | CEF_ADDR)))
2817 if (Wconstexpr_not_const)
2818 warning(e->pos, "non-constant initializer for static object");
2820 return 1;
2824 * sublist; either a string, or we dig in; the latter will deal with
2825 * pathologies, so we don't need anything fancy here.
2827 if (e->type == EXPR_INITIALIZER) {
2828 if (is_string_type(ctype)) {
2829 struct expression *v = NULL;
2830 int count = 0;
2832 FOR_EACH_PTR(e->expr_list, p) {
2833 if (!v)
2834 v = p;
2835 count++;
2836 } END_FOR_EACH_PTR(p);
2837 if (count == 1 && is_string_literal(&v)) {
2838 *ep = e = v;
2839 goto String;
2842 handle_list_initializer(e, class, ctype, mods);
2843 return 1;
2846 /* string */
2847 if (is_string_literal(&e)) {
2848 /* either we are doing array of char, or we'll have to dig in */
2849 if (is_string_type(ctype)) {
2850 *ep = e;
2851 goto String;
2853 return 0;
2855 /* struct or union can be initialized by compatible */
2856 if (class != TYPE_COMPOUND)
2857 return 0;
2858 type = evaluate_expression(e);
2859 if (!type)
2860 return 0;
2861 if (ctype->type == SYM_NODE)
2862 ctype = ctype->ctype.base_type;
2863 if (type->type == SYM_NODE)
2864 type = type->ctype.base_type;
2865 if (ctype == type)
2866 return 1;
2867 return 0;
2869 String:
2870 p = alloc_expression(e->pos, EXPR_STRING);
2871 *p = *e;
2872 type = evaluate_expression(p);
2873 if (ctype->bit_size != -1) {
2874 struct symbol *char_type = e->wide ? wchar_ctype : &char_ctype;
2875 unsigned int size_with_null = ctype->bit_size + char_type->bit_size;
2876 if (size_with_null < type->bit_size)
2877 warning(e->pos,
2878 "too long initializer-string for array of char");
2879 else if (Winit_cstring && size_with_null == type->bit_size) {
2880 warning(e->pos,
2881 "too long initializer-string for array of char(no space for nul char)");
2884 *ep = p;
2885 return 1;
2888 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2890 struct symbol *type;
2891 int class = classify_type(ctype, &type);
2892 if (!handle_initializer(ep, 0, class, ctype, 0))
2893 expression_error(*ep, "invalid initializer");
2896 static struct symbol *cast_to_bool(struct expression *expr)
2898 struct expression *old = expr->cast_expression;
2899 struct expression *zero;
2900 struct symbol *otype;
2901 int oclass = classify_type(degenerate(old), &otype);
2902 struct symbol *ctype;
2904 if (oclass & TYPE_COMPOUND)
2905 return NULL;
2907 zero = alloc_const_expression(expr->pos, 0);
2908 if (oclass & TYPE_PTR)
2909 zero->ctype = otype;
2910 expr->op = SPECIAL_NOTEQUAL;
2911 ctype = usual_conversions(expr->op, old, zero,
2912 oclass, TYPE_NUM, otype, zero->ctype);
2913 expr->type = EXPR_COMPARE;
2914 expr->left = cast_to(old, ctype);
2915 expr->right = cast_to(zero, ctype);
2917 return expr->ctype;
2920 static int cast_flags(struct expression *expr, struct expression *old)
2922 struct symbol *t;
2923 int class;
2924 int flags = CEF_NONE;
2926 class = classify_type(expr->ctype, &t);
2927 if (class & TYPE_NUM) {
2928 flags = old->flags & ~CEF_CONST_MASK;
2930 * Casts to numeric types never result in address
2931 * constants [6.6(9)].
2933 flags &= ~CEF_ADDR;
2936 * As an extension, treat address constants cast to
2937 * integer type as an arithmetic constant.
2939 if (old->flags & CEF_ADDR)
2940 flags = CEF_ACE;
2943 * Cast to float type -> not an integer constant
2944 * expression [6.6(6)].
2946 if (class & TYPE_FLOAT)
2947 flags &= ~CEF_CLR_ICE;
2949 * Casts of float literals to integer type results in
2950 * a constant integer expression [6.6(6)].
2952 else if (old->flags & CEF_FLOAT)
2953 flags = CEF_SET_ICE;
2954 } else if (class & TYPE_PTR) {
2956 * Casts of integer literals to pointer type yield
2957 * address constants [6.6(9)].
2959 * As an extension, treat address constants cast to a
2960 * different pointer type as address constants again.
2962 * As another extension, treat integer constant
2963 * expressions (in contrast to literals) cast to
2964 * pointer type as address constants.
2966 if (old->flags & (CEF_ICE | CEF_ADDR))
2967 flags = CEF_ADDR;
2970 return flags;
2974 // check if a type matches one of the members of a union type
2975 // @utype: the union type
2976 // @type: to type to check
2977 // @return: to identifier of the matching type in the union.
2978 static struct symbol *find_member_type(struct symbol *utype, struct symbol *type)
2980 struct symbol *t, *member;
2982 if (utype->type != SYM_UNION)
2983 return NULL;
2985 FOR_EACH_PTR(utype->symbol_list, member) {
2986 classify_type(member, &t);
2987 if (type == t)
2988 return member;
2989 } END_FOR_EACH_PTR(member);
2990 return NULL;
2993 static struct symbol *evaluate_compound_literal(struct expression *expr, struct expression *source)
2995 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2996 struct symbol *sym = expr->cast_type;
2998 sym->initializer = source;
2999 evaluate_symbol(sym);
3001 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
3002 addr->symbol = sym;
3003 if (sym->ctype.modifiers & MOD_TOPLEVEL)
3004 addr->flags |= CEF_ADDR;
3006 expr->type = EXPR_PREOP;
3007 expr->op = '*';
3008 expr->deref = addr;
3009 expr->ctype = sym;
3010 return sym;
3013 static struct symbol *evaluate_cast(struct expression *expr)
3015 struct expression *source = expr->cast_expression;
3016 struct symbol *ctype;
3017 struct symbol *ttype, *stype;
3018 struct symbol *member;
3019 int tclass, sclass;
3020 struct ident *tas = NULL, *sas = NULL;
3022 if (!source)
3023 return NULL;
3026 * Special case: a cast can be followed by an
3027 * initializer, in which case we need to pass
3028 * the type value down to that initializer rather
3029 * than trying to evaluate it as an expression
3030 * (cfr. compound literals: C99 & C11 6.5.2.5).
3032 * A more complex case is when the initializer is
3033 * dereferenced as part of a post-fix expression.
3034 * We need to produce an expression that can be dereferenced.
3036 if (source->type == EXPR_INITIALIZER)
3037 return evaluate_compound_literal(expr, source);
3039 ctype = examine_symbol_type(expr->cast_type);
3040 ctype = unqualify_type(ctype);
3041 expr->ctype = ctype;
3042 expr->cast_type = ctype;
3044 evaluate_expression(source);
3045 degenerate(source);
3047 tclass = classify_type(ctype, &ttype);
3049 expr->flags = cast_flags(expr, source);
3052 * You can always throw a value away by casting to
3053 * "void" - that's an implicit "force". Note that
3054 * the same is _not_ true of "void *".
3056 if (ttype == &void_ctype)
3057 goto out;
3059 stype = source->ctype;
3060 if (!stype) {
3061 expression_error(expr, "cast from unknown type");
3062 goto out;
3064 sclass = classify_type(stype, &stype);
3066 if (expr->type == EXPR_FORCE_CAST)
3067 goto out;
3069 if (tclass & (TYPE_COMPOUND | TYPE_FN)) {
3071 * Special case: cast to union type (GCC extension)
3072 * The effect is similar to a compound literal except
3073 * that the result is a rvalue.
3075 if ((member = find_member_type(ttype, stype))) {
3076 struct expression *item, *init;
3078 if (Wunion_cast)
3079 warning(expr->pos, "cast to union type");
3081 item = alloc_expression(source->pos, EXPR_IDENTIFIER);
3082 item->expr_ident = member->ident;
3083 item->ident_expression = source;
3085 init = alloc_expression(source->pos, EXPR_INITIALIZER);
3086 add_expression(&init->expr_list, item);
3088 // FIXME: this should be a rvalue
3089 evaluate_compound_literal(expr, init);
3090 return ctype;
3093 warning(expr->pos, "cast to non-scalar");
3096 if (sclass & TYPE_COMPOUND)
3097 warning(expr->pos, "cast from non-scalar");
3099 /* allowed cast unfouls */
3100 if (sclass & TYPE_FOULED)
3101 stype = unfoul(stype);
3103 if (ttype != stype) {
3104 if ((tclass & TYPE_RESTRICT) && restricted_value(source, ttype))
3105 warning(expr->pos, "cast to %s",
3106 show_typename(ttype));
3107 if (sclass & TYPE_RESTRICT) {
3108 if (ttype == &bool_ctype) {
3109 if (sclass & TYPE_FOULED)
3110 warning(expr->pos, "%s degrades to integer",
3111 show_typename(stype));
3112 } else {
3113 warning(expr->pos, "cast from %s",
3114 show_typename(stype));
3119 if ((ttype == &ulong_ctype || ttype == uintptr_ctype) && !Wcast_from_as)
3120 tas = &bad_address_space;
3121 else if (tclass == TYPE_PTR) {
3122 examine_pointer_target(ttype);
3123 tas = ttype->ctype.as;
3126 if ((stype == &ulong_ctype || stype == uintptr_ctype))
3127 sas = &bad_address_space;
3128 else if (sclass == TYPE_PTR) {
3129 examine_pointer_target(stype);
3130 sas = stype->ctype.as;
3133 if (!tas && valid_as(sas))
3134 warning(expr->pos, "cast removes address space '%s' of expression", show_as(sas));
3135 if (valid_as(tas) && valid_as(sas) && tas != sas)
3136 warning(expr->pos, "cast between address spaces (%s -> %s)", show_as(sas), show_as(tas));
3137 if (valid_as(tas) && !sas &&
3138 !is_null_pointer_constant(source) && Wcast_to_as)
3139 warning(expr->pos,
3140 "cast adds address space '%s' to expression", show_as(tas));
3142 if (!(ttype->ctype.modifiers & MOD_PTRINHERIT) && tclass == TYPE_PTR &&
3143 !tas && (source->flags & CEF_ICE)) {
3144 if (ttype->ctype.base_type == &void_ctype) {
3145 if (is_zero_constant(source)) {
3146 /* NULL */
3147 expr->type = EXPR_VALUE;
3148 expr->ctype = &null_ctype;
3149 expr->value = 0;
3150 return expr->ctype;
3155 if (ttype == &bool_ctype)
3156 cast_to_bool(expr);
3158 // checks pointers to restricted
3159 while (Wbitwise_pointer && tclass == TYPE_PTR && sclass == TYPE_PTR) {
3160 tclass = classify_type(ttype->ctype.base_type, &ttype);
3161 sclass = classify_type(stype->ctype.base_type, &stype);
3162 if (ttype == stype)
3163 break;
3164 if (!ttype || !stype)
3165 break;
3166 if (ttype == &void_ctype || stype == &void_ctype)
3167 break;
3168 if (tclass & TYPE_RESTRICT) {
3169 warning(expr->pos, "cast to %s", show_typename(ctype));
3170 break;
3172 if (sclass & TYPE_RESTRICT) {
3173 warning(expr->pos, "cast from %s", show_typename(source->ctype));
3174 break;
3177 out:
3178 return ctype;
3182 * Evaluate a call expression with a symbol. This
3183 * should expand inline functions, and evaluate
3184 * builtins.
3186 static int evaluate_symbol_call(struct expression *expr)
3188 struct expression *fn = expr->fn;
3189 struct symbol *ctype = fn->ctype;
3191 if (fn->type != EXPR_PREOP)
3192 return 0;
3194 if (ctype->op && ctype->op->evaluate)
3195 return ctype->op->evaluate(expr);
3197 return 0;
3200 static struct symbol *evaluate_call(struct expression *expr)
3202 int args, fnargs;
3203 struct symbol *ctype, *sym;
3204 struct expression *fn = expr->fn;
3205 struct expression_list *arglist = expr->args;
3207 if (!evaluate_expression(fn))
3208 return NULL;
3209 sym = ctype = fn->ctype;
3210 if (ctype->type == SYM_NODE)
3211 ctype = ctype->ctype.base_type;
3212 if (ctype->type == SYM_PTR)
3213 ctype = get_base_type(ctype);
3215 if (ctype->type != SYM_FN) {
3216 struct expression *arg;
3218 if (fn->ctype == &bad_ctype)
3219 return NULL;
3221 expression_error(expr, "not a function %s",
3222 show_ident(sym->ident));
3223 /* do typechecking in arguments */
3224 FOR_EACH_PTR (arglist, arg) {
3225 evaluate_expression(arg);
3226 } END_FOR_EACH_PTR(arg);
3227 return NULL;
3230 examine_fn_arguments(ctype);
3231 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
3232 sym->op && sym->op->args) {
3233 if (!sym->op->args(expr))
3234 return NULL;
3235 } else {
3236 if (!evaluate_arguments(ctype->arguments, arglist))
3237 return NULL;
3238 args = expression_list_size(expr->args);
3239 fnargs = symbol_list_size(ctype->arguments);
3240 if (args < fnargs) {
3241 expression_error(expr,
3242 "not enough arguments for function %s",
3243 show_ident(sym->ident));
3244 return NULL;
3246 if (args > fnargs && !ctype->variadic)
3247 expression_error(expr,
3248 "too many arguments for function %s",
3249 show_ident(sym->ident));
3251 expr->ctype = ctype->ctype.base_type;
3252 if (sym->type == SYM_NODE) {
3253 if (evaluate_symbol_call(expr))
3254 return expr->ctype;
3256 return expr->ctype;
3259 static struct symbol *evaluate_offsetof(struct expression *expr)
3261 struct expression *e = expr->down;
3262 struct symbol *ctype = expr->in;
3263 int class;
3265 if (expr->op == '.') {
3266 struct symbol *field;
3267 int offset = 0;
3268 if (!ctype) {
3269 expression_error(expr, "expected structure or union");
3270 return NULL;
3272 examine_symbol_type(ctype);
3273 class = classify_type(ctype, &ctype);
3274 if (class != TYPE_COMPOUND) {
3275 expression_error(expr, "expected structure or union");
3276 return NULL;
3279 field = find_identifier(expr->ident, ctype->symbol_list, &offset);
3280 if (!field) {
3281 expression_error(expr, "unknown member");
3282 return NULL;
3284 ctype = field;
3285 expr->type = EXPR_VALUE;
3286 expr->flags = CEF_SET_ICE;
3287 expr->value = offset;
3288 expr->taint = 0;
3289 expr->ctype = size_t_ctype;
3290 } else {
3291 if (!ctype) {
3292 expression_error(expr, "expected structure or union");
3293 return NULL;
3295 examine_symbol_type(ctype);
3296 class = classify_type(ctype, &ctype);
3297 if (class != (TYPE_COMPOUND | TYPE_PTR)) {
3298 expression_error(expr, "expected array");
3299 return NULL;
3301 ctype = ctype->ctype.base_type;
3302 if (!expr->index) {
3303 expr->type = EXPR_VALUE;
3304 expr->flags = CEF_SET_ICE;
3305 expr->value = 0;
3306 expr->taint = 0;
3307 expr->ctype = size_t_ctype;
3308 } else {
3309 struct expression *idx = expr->index, *m;
3310 struct symbol *i_type = evaluate_expression(idx);
3311 unsigned old_idx_flags;
3312 int i_class = classify_type(i_type, &i_type);
3314 if (!is_int(i_class)) {
3315 expression_error(expr, "non-integer index");
3316 return NULL;
3318 unrestrict(idx, i_class, &i_type);
3319 old_idx_flags = idx->flags;
3320 idx = cast_to(idx, size_t_ctype);
3321 idx->flags = old_idx_flags;
3322 m = alloc_const_expression(expr->pos,
3323 bits_to_bytes(ctype->bit_size));
3324 m->ctype = size_t_ctype;
3325 m->flags = CEF_SET_INT;
3326 expr->type = EXPR_BINOP;
3327 expr->left = idx;
3328 expr->right = m;
3329 expr->op = '*';
3330 expr->ctype = size_t_ctype;
3331 expr->flags = m->flags & idx->flags & ~CEF_CONST_MASK;
3334 if (e) {
3335 struct expression *copy = __alloc_expression(0);
3336 *copy = *expr;
3337 if (e->type == EXPR_OFFSETOF)
3338 e->in = ctype;
3339 if (!evaluate_expression(e))
3340 return NULL;
3341 expr->type = EXPR_BINOP;
3342 expr->flags = e->flags & copy->flags & ~CEF_CONST_MASK;
3343 expr->op = '+';
3344 expr->ctype = size_t_ctype;
3345 expr->left = copy;
3346 expr->right = e;
3348 return size_t_ctype;
3351 static void check_label_declaration(struct position pos, struct symbol *label)
3353 switch (label->namespace) {
3354 case NS_LABEL:
3355 if (label->stmt)
3356 break;
3357 sparse_error(pos, "label '%s' was not declared", show_ident(label->ident));
3358 /* fallthrough */
3359 case NS_NONE:
3360 current_fn->bogus_linear = 1;
3361 default:
3362 break;
3366 static int type_selection(struct symbol *ctrl, struct symbol *type)
3368 struct ctype c = { .base_type = ctrl };
3369 struct ctype t = { .base_type = type };
3371 return !type_difference(&c, &t, 0, 0);
3374 static struct symbol *evaluate_generic_selection(struct expression *expr)
3376 struct type_expression *map;
3377 struct expression *res;
3378 struct symbol source;
3379 struct symbol *ctrl;
3381 if (!evaluate_expression(expr->control))
3382 return NULL;
3383 if (!(ctrl = degenerate(expr->control)))
3384 return NULL;
3386 source = *ctrl;
3387 source.ctype.modifiers &= ~(MOD_QUALIFIER|MOD_ATOMIC);
3388 for (map = expr->map; map; map = map->next) {
3389 struct symbol *stype = map->type;
3390 struct symbol *base;
3392 if (!evaluate_symbol(stype))
3393 continue;
3395 base = stype->ctype.base_type;
3396 if (base->type == SYM_ARRAY && base->array_size) {
3397 get_expression_value_silent(base->array_size);
3398 if (base->array_size->type == EXPR_VALUE)
3399 continue;
3400 sparse_error(stype->pos, "variable length array type in generic selection");
3401 continue;
3403 if (is_func_type(stype)) {
3404 sparse_error(stype->pos, "function type in generic selection");
3405 continue;
3407 if (stype->bit_size <= 0 || is_void_type(stype)) {
3408 sparse_error(stype->pos, "incomplete type in generic selection");
3409 continue;
3411 if (!type_selection(&source, stype))
3412 continue;
3414 res = map->expr;
3415 goto found;
3417 res = expr->def;
3418 if (!res) {
3419 sparse_error(expr->pos, "no generic selection for '%s'", show_typename(ctrl));
3420 return NULL;
3423 found:
3424 *expr = *res;
3425 return evaluate_expression(expr);
3428 struct symbol *evaluate_expression(struct expression *expr)
3430 if (!expr)
3431 return NULL;
3432 if (expr->ctype)
3433 return expr->ctype;
3435 switch (expr->type) {
3436 case EXPR_VALUE:
3437 case EXPR_FVALUE:
3438 expression_error(expr, "value expression without a type");
3439 return NULL;
3440 case EXPR_STRING:
3441 return evaluate_string(expr);
3442 case EXPR_SYMBOL:
3443 return evaluate_symbol_expression(expr);
3444 case EXPR_BINOP:
3445 evaluate_expression(expr->left);
3446 evaluate_expression(expr->right);
3447 if (!valid_subexpr_type(expr))
3448 return NULL;
3449 return evaluate_binop(expr);
3450 case EXPR_LOGICAL:
3451 return evaluate_logical(expr);
3452 case EXPR_COMMA:
3453 evaluate_expression(expr->left);
3454 if (!evaluate_expression(expr->right))
3455 return NULL;
3456 return evaluate_comma(expr);
3457 case EXPR_COMPARE:
3458 evaluate_expression(expr->left);
3459 evaluate_expression(expr->right);
3460 if (!valid_subexpr_type(expr))
3461 return NULL;
3462 return evaluate_compare(expr);
3463 case EXPR_ASSIGNMENT:
3464 evaluate_expression(expr->left);
3465 evaluate_expression(expr->right);
3466 if (!valid_subexpr_type(expr))
3467 return NULL;
3468 return evaluate_assignment(expr);
3469 case EXPR_PREOP:
3470 if (!evaluate_expression(expr->unop))
3471 return NULL;
3472 return evaluate_preop(expr);
3473 case EXPR_POSTOP:
3474 if (!evaluate_expression(expr->unop))
3475 return NULL;
3476 return evaluate_postop(expr);
3477 case EXPR_CAST:
3478 case EXPR_FORCE_CAST:
3479 case EXPR_IMPLIED_CAST:
3480 return evaluate_cast(expr);
3481 case EXPR_SIZEOF:
3482 return evaluate_sizeof(expr);
3483 case EXPR_PTRSIZEOF:
3484 return evaluate_ptrsizeof(expr);
3485 case EXPR_ALIGNOF:
3486 return evaluate_alignof(expr);
3487 case EXPR_DEREF:
3488 return evaluate_member_dereference(expr);
3489 case EXPR_CALL:
3490 return evaluate_call(expr);
3491 case EXPR_SELECT:
3492 case EXPR_CONDITIONAL:
3493 return evaluate_conditional_expression(expr);
3494 case EXPR_STATEMENT:
3495 expr->ctype = evaluate_statement(expr->statement);
3496 return expr->ctype;
3498 case EXPR_LABEL:
3499 expr->ctype = &ptr_ctype;
3500 check_label_declaration(expr->pos, expr->label_symbol);
3501 return &ptr_ctype;
3503 case EXPR_TYPE:
3504 /* Evaluate the type of the symbol .. */
3505 evaluate_symbol(expr->symbol);
3506 /* .. but the type of the _expression_ is a "type" */
3507 expr->ctype = &type_ctype;
3508 return &type_ctype;
3510 case EXPR_OFFSETOF:
3511 return evaluate_offsetof(expr);
3513 case EXPR_GENERIC:
3514 return evaluate_generic_selection(expr);
3516 /* These can not exist as stand-alone expressions */
3517 case EXPR_INITIALIZER:
3518 case EXPR_IDENTIFIER:
3519 case EXPR_INDEX:
3520 case EXPR_POS:
3521 expression_error(expr, "internal front-end error: initializer in expression");
3522 return NULL;
3523 case EXPR_SLICE:
3524 expression_error(expr, "internal front-end error: SLICE re-evaluated");
3525 return NULL;
3527 return NULL;
3530 void check_duplicates(struct symbol *sym)
3532 int declared = 0;
3533 struct symbol *next = sym;
3534 int initialized = sym->initializer != NULL;
3536 while ((next = next->same_symbol) != NULL) {
3537 const char *typediff;
3538 evaluate_symbol(next);
3539 if (initialized && next->initializer) {
3540 sparse_error(sym->pos, "symbol '%s' has multiple initializers (originally initialized at %s:%d)",
3541 show_ident(sym->ident),
3542 stream_name(next->pos.stream), next->pos.line);
3543 /* Only warn once */
3544 initialized = 0;
3546 declared++;
3547 typediff = type_difference(&sym->ctype, &next->ctype, 0, 0);
3548 if (typediff) {
3549 sparse_error(sym->pos, "symbol '%s' redeclared with different type (%s):",
3550 show_ident(sym->ident), typediff);
3551 info(sym->pos, " %s", show_typename(sym));
3552 info(next->pos, "note: previously declared as:");
3553 info(next->pos, " %s", show_typename(next));
3554 return;
3557 if (!declared) {
3558 unsigned long mod = sym->ctype.modifiers;
3559 if (mod & (MOD_STATIC | MOD_REGISTER | MOD_EXT_VISIBLE))
3560 return;
3561 if (!(mod & MOD_TOPLEVEL))
3562 return;
3563 if (!Wdecl)
3564 return;
3565 if (sym->ident == &main_ident)
3566 return;
3567 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
3571 static struct symbol *evaluate_symbol(struct symbol *sym)
3573 struct symbol *base_type;
3575 if (!sym)
3576 return sym;
3577 if (sym->evaluated)
3578 return sym;
3579 sym->evaluated = 1;
3581 sym = examine_symbol_type(sym);
3582 base_type = get_base_type(sym);
3583 if (!base_type)
3584 return NULL;
3586 /* Evaluate the initializers */
3587 if (sym->initializer)
3588 evaluate_initializer(sym, &sym->initializer);
3590 /* And finally, evaluate the body of the symbol too */
3591 if (base_type->type == SYM_FN) {
3592 struct symbol *curr = current_fn;
3594 if (sym->definition && sym->definition != sym)
3595 return evaluate_symbol(sym->definition);
3597 current_fn = sym;
3599 examine_fn_arguments(base_type);
3600 if (!base_type->stmt && base_type->inline_stmt)
3601 uninline(sym);
3602 if (base_type->stmt)
3603 evaluate_statement(base_type->stmt);
3605 current_fn = curr;
3608 return base_type;
3611 void evaluate_symbol_list(struct symbol_list *list)
3613 struct symbol *sym;
3615 FOR_EACH_PTR(list, sym) {
3616 has_error &= ~ERROR_CURR_PHASE;
3617 evaluate_symbol(sym);
3618 check_duplicates(sym);
3619 } END_FOR_EACH_PTR(sym);
3622 static struct symbol *evaluate_return_expression(struct statement *stmt)
3624 struct expression *expr = stmt->expression;
3625 struct symbol *fntype, *rettype;
3627 evaluate_expression(expr);
3628 fntype = current_fn->ctype.base_type;
3629 rettype = fntype->ctype.base_type;
3630 if (!rettype || rettype == &void_ctype) {
3631 if (expr && expr->ctype && !is_void_type(expr->ctype))
3632 expression_error(expr, "return expression in %s function", rettype?"void":"typeless");
3633 if (expr && Wreturn_void)
3634 warning(stmt->pos, "returning void-valued expression");
3635 return NULL;
3638 if (!expr) {
3639 sparse_error(stmt->pos, "return with no return value");
3640 return NULL;
3642 if (!expr->ctype)
3643 return NULL;
3644 compatible_assignment_types(expr, rettype, &stmt->expression, "return expression");
3645 return NULL;
3648 static void evaluate_if_statement(struct statement *stmt)
3650 if (!stmt->if_conditional)
3651 return;
3653 evaluate_conditional(stmt->if_conditional, 0);
3654 evaluate_statement(stmt->if_true);
3655 evaluate_statement(stmt->if_false);
3658 static void evaluate_iterator(struct statement *stmt)
3660 evaluate_symbol_list(stmt->iterator_syms);
3661 evaluate_conditional(stmt->iterator_pre_condition, 1);
3662 evaluate_conditional(stmt->iterator_post_condition,1);
3663 evaluate_statement(stmt->iterator_pre_statement);
3664 evaluate_statement(stmt->iterator_statement);
3665 evaluate_statement(stmt->iterator_post_statement);
3669 static void parse_asm_constraint(struct asm_operand *op)
3671 struct expression *constraint = op->constraint;
3672 const char *str = constraint->string->data;
3673 int c;
3675 switch (str[0]) {
3676 case '\0':
3677 sparse_error(constraint->pos, "invalid ASM constraint (\"\")");
3678 break;
3679 case '+':
3680 op->is_modify = true;
3681 /* fall-through */
3682 case '=':
3683 op->is_assign = true;
3684 str++;
3685 break;
3688 while ((c = *str++)) {
3689 switch (c) {
3690 case '=':
3691 case '+':
3692 sparse_error(constraint->pos, "invalid ASM constraint '%c'", c);
3693 break;
3695 case '&':
3696 op->is_earlyclobber = true;
3697 break;
3698 case '%':
3699 op->is_commutative = true;
3700 break;
3701 case 'r':
3702 op->is_register = true;
3703 break;
3705 case 'm':
3706 case 'o':
3707 case 'V':
3708 case 'Q':
3709 op->is_memory = true;
3710 break;
3712 case '<':
3713 case '>':
3714 // FIXME: ignored for now
3715 break;
3717 case ',':
3718 // FIXME: multiple alternative constraints
3719 break;
3721 case '0' ... '9':
3722 // FIXME: numeric matching constraint?
3723 break;
3724 case '[':
3725 // FIXME: symbolic matching constraint
3726 return;
3728 default:
3729 if (arch_target->asm_constraint)
3730 str = arch_target->asm_constraint(op, c, str);
3732 // FIXME: multi-letter constraints
3733 break;
3737 // FIXME: how to deal with multi-constraint?
3738 if (op->is_register)
3739 op->is_memory = 0;
3742 static void verify_output_constraint(struct asm_operand *op)
3744 struct expression *expr = op->constraint;
3745 const char *constraint = expr->string->data;
3747 if (!op->is_assign)
3748 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
3751 static void verify_input_constraint(struct asm_operand *op)
3753 struct expression *expr = op->constraint;
3754 const char *constraint = expr->string->data;
3756 if (op->is_assign)
3757 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
3760 static void evaluate_asm_memop(struct asm_operand *op)
3762 if (op->is_memory) {
3763 struct expression *expr = op->expr;
3764 struct expression *addr;
3766 // implicit addressof
3767 addr = alloc_expression(expr->pos, EXPR_PREOP);
3768 addr->op = '&';
3769 addr->unop = expr;
3771 evaluate_addressof(addr);
3772 op->expr = addr;
3773 } else {
3774 evaluate_expression(op->expr);
3775 degenerate(op->expr);
3779 static void evaluate_asm_statement(struct statement *stmt)
3781 struct expression *expr;
3782 struct asm_operand *op;
3783 struct symbol *sym;
3785 if (!stmt->asm_string)
3786 return;
3788 FOR_EACH_PTR(stmt->asm_outputs, op) {
3789 /* Identifier */
3791 /* Constraint */
3792 if (op->constraint) {
3793 parse_asm_constraint(op);
3794 verify_output_constraint(op);
3797 /* Expression */
3798 expr = op->expr;
3799 if (!evaluate_expression(expr))
3800 return;
3801 if (!lvalue_expression(expr))
3802 warning(expr->pos, "asm output is not an lvalue");
3803 evaluate_assign_to(expr, expr->ctype);
3804 evaluate_asm_memop(op);
3805 } END_FOR_EACH_PTR(op);
3807 FOR_EACH_PTR(stmt->asm_inputs, op) {
3808 /* Identifier */
3810 /* Constraint */
3811 if (op->constraint) {
3812 parse_asm_constraint(op);
3813 verify_input_constraint(op);
3816 /* Expression */
3817 if (!evaluate_expression(op->expr))
3818 return;
3819 evaluate_asm_memop(op);
3820 } END_FOR_EACH_PTR(op);
3822 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
3823 if (!expr) {
3824 sparse_error(stmt->pos, "bad asm clobbers");
3825 return;
3827 if (expr->type == EXPR_STRING)
3828 continue;
3829 expression_error(expr, "asm clobber is not a string");
3830 } END_FOR_EACH_PTR(expr);
3832 FOR_EACH_PTR(stmt->asm_labels, sym) {
3833 if (!sym || sym->type != SYM_LABEL) {
3834 sparse_error(stmt->pos, "bad asm label");
3835 return;
3837 } END_FOR_EACH_PTR(sym);
3840 static void evaluate_case_statement(struct statement *stmt)
3842 evaluate_expression(stmt->case_expression);
3843 evaluate_expression(stmt->case_to);
3844 evaluate_statement(stmt->case_statement);
3847 static void check_case_type(struct expression *switch_expr,
3848 struct expression *case_expr,
3849 struct expression **enumcase)
3851 struct symbol *switch_type, *case_type;
3852 int sclass, cclass;
3854 if (!case_expr)
3855 return;
3857 switch_type = switch_expr->ctype;
3858 case_type = evaluate_expression(case_expr);
3860 if (!switch_type || !case_type)
3861 goto Bad;
3862 if (enumcase) {
3863 if (*enumcase)
3864 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
3865 else if (is_enum_type(case_type))
3866 *enumcase = case_expr;
3869 sclass = classify_type(switch_type, &switch_type);
3870 cclass = classify_type(case_type, &case_type);
3872 /* both should be arithmetic */
3873 if (!(sclass & cclass & TYPE_NUM))
3874 goto Bad;
3876 /* neither should be floating */
3877 if ((sclass | cclass) & TYPE_FLOAT)
3878 goto Bad;
3880 /* if neither is restricted, we are OK */
3881 if (!((sclass | cclass) & TYPE_RESTRICT))
3882 return;
3884 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
3885 cclass, sclass, case_type, switch_type)) {
3886 unrestrict(case_expr, cclass, &case_type);
3887 unrestrict(switch_expr, sclass, &switch_type);
3889 return;
3891 Bad:
3892 expression_error(case_expr, "incompatible types for 'case' statement");
3895 static void evaluate_switch_statement(struct statement *stmt)
3897 struct symbol *sym;
3898 struct expression *enumcase = NULL;
3899 struct expression **enumcase_holder = &enumcase;
3900 struct expression *sel = stmt->switch_expression;
3902 evaluate_expression(sel);
3903 evaluate_statement(stmt->switch_statement);
3904 if (!sel)
3905 return;
3906 if (sel->ctype && is_enum_type(sel->ctype))
3907 enumcase_holder = NULL; /* Only check cases against switch */
3909 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
3910 struct statement *case_stmt = sym->stmt;
3911 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
3912 check_case_type(sel, case_stmt->case_to, enumcase_holder);
3913 } END_FOR_EACH_PTR(sym);
3916 static void evaluate_goto_statement(struct statement *stmt)
3918 struct symbol *label = stmt->goto_label;
3920 if (!label) {
3921 // no label associated, may be a computed goto
3922 evaluate_expression(stmt->goto_expression);
3923 return;
3926 check_label_declaration(stmt->pos, label);
3929 struct symbol *evaluate_statement(struct statement *stmt)
3931 if (!stmt)
3932 return NULL;
3934 switch (stmt->type) {
3935 case STMT_DECLARATION: {
3936 struct symbol *s;
3937 FOR_EACH_PTR(stmt->declaration, s) {
3938 evaluate_symbol(s);
3939 } END_FOR_EACH_PTR(s);
3940 return NULL;
3943 case STMT_RETURN:
3944 return evaluate_return_expression(stmt);
3946 case STMT_EXPRESSION:
3947 if (!evaluate_expression(stmt->expression))
3948 return NULL;
3949 if (stmt->expression->ctype == &null_ctype)
3950 stmt->expression = cast_to(stmt->expression, &ptr_ctype);
3951 return unqualify_type(degenerate(stmt->expression));
3953 case STMT_COMPOUND: {
3954 struct statement *s;
3955 struct symbol *type = NULL;
3957 /* Evaluate the return symbol in the compound statement */
3958 evaluate_symbol(stmt->ret);
3961 * Then, evaluate each statement, making the type of the
3962 * compound statement be the type of the last statement
3964 type = evaluate_statement(stmt->args);
3965 FOR_EACH_PTR(stmt->stmts, s) {
3966 type = evaluate_statement(s);
3967 } END_FOR_EACH_PTR(s);
3968 if (!type)
3969 type = &void_ctype;
3970 return type;
3972 case STMT_IF:
3973 evaluate_if_statement(stmt);
3974 return NULL;
3975 case STMT_ITERATOR:
3976 evaluate_iterator(stmt);
3977 return NULL;
3978 case STMT_SWITCH:
3979 evaluate_switch_statement(stmt);
3980 return NULL;
3981 case STMT_CASE:
3982 evaluate_case_statement(stmt);
3983 return NULL;
3984 case STMT_LABEL:
3985 return evaluate_statement(stmt->label_statement);
3986 case STMT_GOTO:
3987 evaluate_goto_statement(stmt);
3988 return NULL;
3989 case STMT_NONE:
3990 break;
3991 case STMT_ASM:
3992 evaluate_asm_statement(stmt);
3993 return NULL;
3994 case STMT_CONTEXT:
3995 evaluate_expression(stmt->expression);
3996 return NULL;
3997 case STMT_RANGE:
3998 evaluate_expression(stmt->range_expression);
3999 evaluate_expression(stmt->range_low);
4000 evaluate_expression(stmt->range_high);
4001 return NULL;
4003 return NULL;