constants: define a bunch of constant svals
[smatch.git] / evaluate.c
blobf327dcff2e2dd3a6a04cb6f43d1a26e52c9a7686
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 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_nrbits = member->bit_size;
2174 expr->r_bitpos += member->bit_offset;
2175 expr->ctype = member;
2176 return member;
2179 deref = deref->unop;
2180 expr->deref = deref;
2182 add = evaluate_offset(deref, offset);
2183 expr->type = EXPR_PREOP;
2184 expr->op = '*';
2185 expr->unop = add;
2187 expr->ctype = member;
2188 return member;
2191 static int is_promoted(struct expression *expr)
2193 while (1) {
2194 switch (expr->type) {
2195 case EXPR_BINOP:
2196 case EXPR_SELECT:
2197 case EXPR_CONDITIONAL:
2198 return 1;
2199 case EXPR_COMMA:
2200 expr = expr->right;
2201 continue;
2202 case EXPR_PREOP:
2203 switch (expr->op) {
2204 case '(':
2205 expr = expr->unop;
2206 continue;
2207 case '+':
2208 case '-':
2209 case '~':
2210 return 1;
2211 default:
2212 return 0;
2214 default:
2215 return 0;
2221 static struct symbol *evaluate_type_information(struct expression *expr)
2223 struct symbol *sym = expr->cast_type;
2224 if (!sym) {
2225 sym = evaluate_expression(expr->cast_expression);
2226 if (!sym)
2227 return NULL;
2229 * Expressions of restricted types will possibly get
2230 * promoted - check that here
2232 if (is_restricted_type(sym)) {
2233 if (sym->bit_size < bits_in_int && is_promoted(expr))
2234 sym = &int_ctype;
2235 } else if (is_fouled_type(sym)) {
2236 sym = &int_ctype;
2239 examine_symbol_type(sym);
2240 if (is_bitfield_type(sym)) {
2241 expression_error(expr, "trying to examine bitfield type");
2242 return NULL;
2244 return sym;
2247 static struct symbol *evaluate_sizeof(struct expression *expr)
2249 struct symbol *type;
2250 int size;
2252 type = evaluate_type_information(expr);
2253 if (!type)
2254 return NULL;
2256 size = type->bit_size;
2258 if (size < 0 && is_void_type(type)) {
2259 if (Wpointer_arith)
2260 warning(expr->pos, "expression using sizeof(void)");
2261 size = bits_in_char;
2264 if (is_bool_type(type)) {
2265 if (Wsizeof_bool)
2266 warning(expr->pos, "expression using sizeof _Bool");
2267 size = bits_to_bytes(bits_in_bool) * bits_in_char;
2270 if (is_function(type->ctype.base_type)) {
2271 if (Wpointer_arith)
2272 warning(expr->pos, "expression using sizeof on a function");
2273 size = bits_in_char;
2276 if (has_flexible_array(type) && Wflexible_array_sizeof)
2277 warning(expr->pos, "using sizeof on a flexible structure");
2279 if (is_array_type(type) && size < 0) { // VLA, 1-dimension only
2280 struct expression *base, *size;
2281 struct symbol *base_type;
2283 if (type->type == SYM_NODE)
2284 type = type->ctype.base_type; // strip the SYM_NODE
2285 base_type = get_base_type(type);
2286 if (!base_type)
2287 goto error;
2288 if (base_type->bit_size <= 0) {
2289 base = alloc_expression(expr->pos, EXPR_SIZEOF);
2290 base->cast_type = base_type;
2291 if (!evaluate_sizeof(base))
2292 goto error;
2293 } else {
2294 base = alloc_expression(expr->pos, EXPR_VALUE);
2295 base->value = bits_to_bytes(base_type->bit_size);
2296 base->ctype = size_t_ctype;
2298 size = alloc_expression(expr->pos, EXPR_CAST);
2299 size->cast_type = size_t_ctype;
2300 size->cast_expression = type->array_size;
2301 if (!evaluate_expression(size))
2302 goto error;
2303 expr->left = size;
2304 expr->right = base;
2305 expr->type = EXPR_BINOP;
2306 expr->op = '*';
2307 return expr->ctype = size_t_ctype;
2310 error:
2311 if ((size < 0) || (size & (bits_in_char - 1)))
2312 expression_error(expr, "cannot size expression");
2314 expr->type = EXPR_VALUE;
2315 expr->value = bits_to_bytes(size);
2316 expr->taint = 0;
2317 expr->ctype = size_t_ctype;
2318 return size_t_ctype;
2321 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
2323 struct symbol *type;
2324 int size;
2326 type = evaluate_type_information(expr);
2327 if (!type)
2328 return NULL;
2330 if (type->type == SYM_NODE)
2331 type = type->ctype.base_type;
2332 if (!type)
2333 return NULL;
2334 switch (type->type) {
2335 case SYM_ARRAY:
2336 break;
2337 case SYM_PTR:
2338 type = get_base_type(type);
2339 if (type)
2340 break;
2341 default:
2342 expression_error(expr, "expected pointer expression");
2343 return NULL;
2345 size = type->bit_size;
2346 if (size & (bits_in_char-1))
2347 size = 0;
2348 expr->type = EXPR_VALUE;
2349 expr->value = bits_to_bytes(size);
2350 expr->taint = 0;
2351 expr->ctype = size_t_ctype;
2352 return size_t_ctype;
2355 static struct symbol *evaluate_alignof(struct expression *expr)
2357 struct symbol *type;
2359 type = evaluate_type_information(expr);
2360 if (!type)
2361 return NULL;
2363 expr->type = EXPR_VALUE;
2364 expr->value = type->ctype.alignment;
2365 expr->taint = 0;
2366 expr->ctype = size_t_ctype;
2367 return size_t_ctype;
2370 int evaluate_arguments(struct symbol_list *argtypes, struct expression_list *head)
2372 struct expression *expr;
2373 struct symbol *argtype;
2374 int i = 1;
2376 PREPARE_PTR_LIST(argtypes, argtype);
2377 FOR_EACH_PTR (head, expr) {
2378 struct expression **p = THIS_ADDRESS(expr);
2379 struct symbol *ctype, *target;
2380 ctype = evaluate_expression(expr);
2382 if (!ctype)
2383 return 0;
2385 target = argtype;
2386 if (!target) {
2387 struct symbol *type;
2388 int class = classify_type(ctype, &type);
2389 if (is_int(class)) {
2390 *p = cast_to(expr, integer_promotion(type));
2391 } else if (class & TYPE_FLOAT) {
2392 if (type->rank < 0)
2393 *p = cast_to(expr, &double_ctype);
2394 } else if (class & TYPE_PTR) {
2395 if (expr->ctype == &null_ctype)
2396 *p = cast_to(expr, &ptr_ctype);
2397 else
2398 degenerate(expr);
2400 } else if (!target->forced_arg){
2401 static char where[30];
2402 examine_symbol_type(target);
2403 sprintf(where, "argument %d", i);
2404 compatible_argument_type(expr, target, p, where);
2407 i++;
2408 NEXT_PTR_LIST(argtype);
2409 } END_FOR_EACH_PTR(expr);
2410 FINISH_PTR_LIST(argtype);
2411 return 1;
2414 static void convert_index(struct expression *e)
2416 struct expression *child = e->idx_expression;
2417 unsigned from = e->idx_from;
2418 unsigned to = e->idx_to + 1;
2419 e->type = EXPR_POS;
2420 e->init_offset = from * bits_to_bytes(e->ctype->bit_size);
2421 e->init_nr = to - from;
2422 e->init_expr = child;
2425 static void convert_ident(struct expression *e)
2427 struct expression *child = e->ident_expression;
2428 int offset = e->offset;
2430 e->type = EXPR_POS;
2431 e->init_offset = offset;
2432 e->init_nr = 1;
2433 e->init_expr = child;
2436 static void convert_designators(struct expression *e)
2438 while (e) {
2439 if (e->type == EXPR_INDEX)
2440 convert_index(e);
2441 else if (e->type == EXPR_IDENTIFIER)
2442 convert_ident(e);
2443 else
2444 break;
2445 e = e->init_expr;
2449 static void excess(struct expression *e, const char *s)
2451 warning(e->pos, "excessive elements in %s initializer", s);
2455 * implicit designator for the first element
2457 static struct expression *first_subobject(struct symbol *ctype, int class,
2458 struct expression **v)
2460 struct expression *e = *v, *new;
2462 if (ctype->type == SYM_NODE)
2463 ctype = ctype->ctype.base_type;
2465 if (class & TYPE_PTR) { /* array */
2466 if (!ctype->bit_size)
2467 return NULL;
2468 new = alloc_expression(e->pos, EXPR_INDEX);
2469 new->idx_expression = e;
2470 new->ctype = ctype->ctype.base_type;
2471 } else {
2472 struct symbol *field, *p;
2473 PREPARE_PTR_LIST(ctype->symbol_list, p);
2474 while (p && !p->ident && is_bitfield_type(p))
2475 NEXT_PTR_LIST(p);
2476 field = p;
2477 FINISH_PTR_LIST(p);
2478 if (!field)
2479 return NULL;
2480 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2481 new->ident_expression = e;
2482 new->field = new->ctype = field;
2483 new->offset = field->offset;
2485 *v = new;
2486 return new;
2490 * sanity-check explicit designators; return the innermost one or NULL
2491 * in case of error. Assign types.
2493 static struct expression *check_designators(struct expression *e,
2494 struct symbol *ctype)
2496 struct expression *last = NULL;
2497 const char *err;
2498 while (1) {
2499 if (ctype->type == SYM_NODE)
2500 ctype = ctype->ctype.base_type;
2501 if (e->type == EXPR_INDEX) {
2502 struct symbol *type;
2503 if (ctype->type != SYM_ARRAY) {
2504 err = "array index in non-array";
2505 break;
2507 type = ctype->ctype.base_type;
2508 if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2509 unsigned offset = array_element_offset(type->bit_size, e->idx_to);
2510 if (offset >= ctype->bit_size) {
2511 err = "index out of bounds in";
2512 break;
2515 e->ctype = ctype = type;
2516 ctype = type;
2517 last = e;
2518 if (!e->idx_expression) {
2519 err = "invalid";
2520 break;
2522 e = e->idx_expression;
2523 } else if (e->type == EXPR_IDENTIFIER) {
2524 int offset = 0;
2525 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2526 err = "field name not in struct or union";
2527 break;
2529 ctype = find_identifier(e->expr_ident, ctype->symbol_list, &offset);
2530 if (!ctype) {
2531 err = "unknown field name in";
2532 break;
2534 e->offset = offset;
2535 e->field = e->ctype = ctype;
2536 last = e;
2537 if (!e->ident_expression) {
2538 err = "invalid";
2539 break;
2541 e = e->ident_expression;
2542 } else if (e->type == EXPR_POS) {
2543 err = "internal front-end error: EXPR_POS in";
2544 break;
2545 } else
2546 return last;
2548 expression_error(e, "%s initializer", err);
2549 return NULL;
2553 * choose the next subobject to initialize.
2555 * Get designators for next element, switch old ones to EXPR_POS.
2556 * Return the resulting expression or NULL if we'd run out of subobjects.
2557 * The innermost designator is returned in *v. Designators in old
2558 * are assumed to be already sanity-checked.
2560 static struct expression *next_designators(struct expression *old,
2561 struct symbol *ctype,
2562 struct expression *e, struct expression **v)
2564 struct expression *new = NULL;
2566 if (!old)
2567 return NULL;
2568 if (old->type == EXPR_INDEX) {
2569 struct expression *copy;
2570 unsigned n;
2572 copy = next_designators(old->idx_expression,
2573 old->ctype, e, v);
2574 if (!copy) {
2575 n = old->idx_to + 1;
2576 if (array_element_offset(old->ctype->bit_size, n) == ctype->bit_size) {
2577 convert_index(old);
2578 return NULL;
2580 copy = e;
2581 *v = new = alloc_expression(e->pos, EXPR_INDEX);
2582 } else {
2583 n = old->idx_to;
2584 new = alloc_expression(e->pos, EXPR_INDEX);
2587 new->idx_from = new->idx_to = n;
2588 new->idx_expression = copy;
2589 new->ctype = old->ctype;
2590 convert_index(old);
2591 } else if (old->type == EXPR_IDENTIFIER) {
2592 struct expression *copy;
2593 struct symbol *field;
2594 int offset = 0;
2596 copy = next_designators(old->ident_expression,
2597 old->ctype, e, v);
2598 if (!copy) {
2599 field = old->field->next_subobject;
2600 if (!field) {
2601 convert_ident(old);
2602 return NULL;
2604 copy = e;
2605 *v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2607 * We can't necessarily trust "field->offset",
2608 * because the field might be in an anonymous
2609 * union, and the field offset is then the offset
2610 * within that union.
2612 * The "old->offset - old->field->offset"
2613 * would be the offset of such an anonymous
2614 * union.
2616 offset = old->offset - old->field->offset;
2617 } else {
2618 field = old->field;
2619 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2622 new->field = field;
2623 new->expr_ident = field->ident;
2624 new->ident_expression = copy;
2625 new->ctype = field;
2626 new->offset = field->offset + offset;
2627 convert_ident(old);
2629 return new;
2632 static int handle_initializer(struct expression **ep, int nested,
2633 int class, struct symbol *ctype, unsigned long mods);
2636 * deal with traversing subobjects [6.7.8(17,18,20)]
2638 static void handle_list_initializer(struct expression *expr,
2639 int class, struct symbol *ctype, unsigned long mods)
2641 struct expression *e, *last = NULL, *top = NULL, *next;
2642 int jumped = 0; // has the last designator multiple levels?
2644 if (expr->zero_init)
2645 free_ptr_list(&expr->expr_list);
2647 FOR_EACH_PTR(expr->expr_list, e) {
2648 struct expression **v;
2649 struct symbol *type;
2650 int lclass;
2652 if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2653 struct symbol *struct_sym;
2654 if (!top) {
2655 top = e;
2656 last = first_subobject(ctype, class, &top);
2657 } else {
2658 last = next_designators(last, ctype, e, &top);
2660 if (!last) {
2661 excess(e, class & TYPE_PTR ? "array" :
2662 "struct or union");
2663 DELETE_CURRENT_PTR(e);
2664 continue;
2666 struct_sym = ctype->type == SYM_NODE ? ctype->ctype.base_type : ctype;
2667 if (Wdesignated_init && struct_sym->designated_init)
2668 warning(e->pos, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2669 ctype->ident ? "in initializer for " : "",
2670 ctype->ident ? ctype->ident->len : 0,
2671 ctype->ident ? ctype->ident->name : "",
2672 ctype->ident ? ": " : "",
2673 get_type_name(struct_sym->type),
2674 show_ident(struct_sym->ident));
2675 if (jumped && Wpast_deep_designator) {
2676 warning(e->pos, "advancing past deep designator");
2677 jumped = 0;
2679 REPLACE_CURRENT_PTR(e, last);
2680 } else {
2681 next = check_designators(e, ctype);
2682 if (!next) {
2683 DELETE_CURRENT_PTR(e);
2684 continue;
2686 top = next;
2687 /* deeper than one designator? */
2688 jumped = top != e;
2689 convert_designators(last);
2690 last = e;
2693 found:
2694 lclass = classify_type(top->ctype, &type);
2695 if (top->type == EXPR_INDEX)
2696 v = &top->idx_expression;
2697 else
2698 v = &top->ident_expression;
2700 mods |= ctype->ctype.modifiers & MOD_STORAGE;
2701 if (handle_initializer(v, 1, lclass, top->ctype, mods))
2702 continue;
2704 if (!(lclass & TYPE_COMPOUND)) {
2705 warning(e->pos, "bogus scalar initializer");
2706 DELETE_CURRENT_PTR(e);
2707 continue;
2710 next = first_subobject(type, lclass, v);
2711 if (next) {
2712 warning(e->pos, "missing braces around initializer");
2713 top = next;
2714 goto found;
2717 DELETE_CURRENT_PTR(e);
2718 excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2720 } END_FOR_EACH_PTR(e);
2722 convert_designators(last);
2723 expr->ctype = ctype;
2726 static int is_string_literal(struct expression **v)
2728 struct expression *e = *v;
2729 while (e && e->type == EXPR_PREOP && e->op == '(')
2730 e = e->unop;
2731 if (!e || e->type != EXPR_STRING)
2732 return 0;
2733 if (e != *v && Wparen_string)
2734 warning(e->pos,
2735 "array initialized from parenthesized string constant");
2736 *v = e;
2737 return 1;
2741 * We want a normal expression, possibly in one layer of braces. Warn
2742 * if the latter happens inside a list (it's legal, but likely to be
2743 * an effect of screwup). In case of anything not legal, we are definitely
2744 * having an effect of screwup, so just fail and let the caller warn.
2746 static struct expression *handle_scalar(struct expression *e, int nested)
2748 struct expression *v = NULL, *p;
2749 int count = 0;
2751 /* normal case */
2752 if (e->type != EXPR_INITIALIZER)
2753 return e;
2755 FOR_EACH_PTR(e->expr_list, p) {
2756 if (!v)
2757 v = p;
2758 count++;
2759 } END_FOR_EACH_PTR(p);
2760 if (count != 1)
2761 return NULL;
2762 switch(v->type) {
2763 case EXPR_INITIALIZER:
2764 case EXPR_INDEX:
2765 case EXPR_IDENTIFIER:
2766 return NULL;
2767 default:
2768 break;
2770 if (nested)
2771 warning(e->pos, "braces around scalar initializer");
2772 return v;
2776 * deal with the cases that don't care about subobjects:
2777 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2778 * character array <- string literal, possibly in braces [6.7.8(14)]
2779 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2780 * compound type <- initializer list in braces [6.7.8(16)]
2781 * The last one punts to handle_list_initializer() which, in turn will call
2782 * us for individual elements of the list.
2784 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2785 * the lack of support of wide char stuff in general.
2787 * One note: we need to take care not to evaluate a string literal until
2788 * we know that we *will* handle it right here. Otherwise we would screw
2789 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2790 * { "string", ...} - we need to preserve that string literal recognizable
2791 * until we dig into the inner struct.
2793 static int handle_initializer(struct expression **ep, int nested,
2794 int class, struct symbol *ctype, unsigned long mods)
2796 struct expression *e = *ep, *p;
2797 struct symbol *type;
2799 if (!e)
2800 return 0;
2802 /* scalar */
2803 if (!(class & TYPE_COMPOUND)) {
2804 e = handle_scalar(e, nested);
2805 if (!e)
2806 return 0;
2807 *ep = e;
2808 if (!evaluate_expression(e))
2809 return 1;
2810 compatible_assignment_types(e, ctype, ep, "initializer");
2812 * Initializers for static storage duration objects
2813 * shall be constant expressions or a string literal [6.7.8(4)].
2815 mods |= ctype->ctype.modifiers;
2816 mods &= (MOD_TOPLEVEL | MOD_STATIC);
2817 if (mods && !(e->flags & (CEF_ACE | CEF_ADDR)))
2818 if (Wconstexpr_not_const)
2819 warning(e->pos, "non-constant initializer for static object");
2821 return 1;
2825 * sublist; either a string, or we dig in; the latter will deal with
2826 * pathologies, so we don't need anything fancy here.
2828 if (e->type == EXPR_INITIALIZER) {
2829 if (is_string_type(ctype)) {
2830 struct expression *v = NULL;
2831 int count = 0;
2833 FOR_EACH_PTR(e->expr_list, p) {
2834 if (!v)
2835 v = p;
2836 count++;
2837 } END_FOR_EACH_PTR(p);
2838 if (count == 1 && is_string_literal(&v)) {
2839 *ep = e = v;
2840 goto String;
2843 handle_list_initializer(e, class, ctype, mods);
2844 return 1;
2847 /* string */
2848 if (is_string_literal(&e)) {
2849 /* either we are doing array of char, or we'll have to dig in */
2850 if (is_string_type(ctype)) {
2851 *ep = e;
2852 goto String;
2854 return 0;
2856 /* struct or union can be initialized by compatible */
2857 if (class != TYPE_COMPOUND)
2858 return 0;
2859 type = evaluate_expression(e);
2860 if (!type)
2861 return 0;
2862 if (ctype->type == SYM_NODE)
2863 ctype = ctype->ctype.base_type;
2864 if (type->type == SYM_NODE)
2865 type = type->ctype.base_type;
2866 if (ctype == type)
2867 return 1;
2868 return 0;
2870 String:
2871 p = alloc_expression(e->pos, EXPR_STRING);
2872 *p = *e;
2873 type = evaluate_expression(p);
2874 if (ctype->bit_size != -1) {
2875 struct symbol *char_type = e->wide ? wchar_ctype : &char_ctype;
2876 unsigned int size_with_null = ctype->bit_size + char_type->bit_size;
2877 if (size_with_null < type->bit_size)
2878 warning(e->pos,
2879 "too long initializer-string for array of char");
2880 else if (Winit_cstring && size_with_null == type->bit_size) {
2881 warning(e->pos,
2882 "too long initializer-string for array of char(no space for nul char)");
2885 *ep = p;
2886 return 1;
2889 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2891 struct symbol *type;
2892 int class = classify_type(ctype, &type);
2893 if (!handle_initializer(ep, 0, class, ctype, 0))
2894 expression_error(*ep, "invalid initializer");
2897 static struct symbol *cast_to_bool(struct expression *expr)
2899 struct expression *old = expr->cast_expression;
2900 struct expression *zero;
2901 struct symbol *otype;
2902 int oclass = classify_type(degenerate(old), &otype);
2903 struct symbol *ctype;
2905 if (oclass & TYPE_COMPOUND)
2906 return NULL;
2908 zero = alloc_const_expression(expr->pos, 0);
2909 if (oclass & TYPE_PTR)
2910 zero->ctype = otype;
2911 expr->op = SPECIAL_NOTEQUAL;
2912 ctype = usual_conversions(expr->op, old, zero,
2913 oclass, TYPE_NUM, otype, zero->ctype);
2914 expr->type = EXPR_COMPARE;
2915 expr->left = cast_to(old, ctype);
2916 expr->right = cast_to(zero, ctype);
2918 return expr->ctype;
2921 static int cast_flags(struct expression *expr, struct expression *old)
2923 struct symbol *t;
2924 int class;
2925 int flags = CEF_NONE;
2927 class = classify_type(expr->ctype, &t);
2928 if (class & TYPE_NUM) {
2929 flags = old->flags & ~CEF_CONST_MASK;
2931 * Casts to numeric types never result in address
2932 * constants [6.6(9)].
2934 flags &= ~CEF_ADDR;
2937 * As an extension, treat address constants cast to
2938 * integer type as an arithmetic constant.
2940 if (old->flags & CEF_ADDR)
2941 flags = CEF_ACE;
2944 * Cast to float type -> not an integer constant
2945 * expression [6.6(6)].
2947 if (class & TYPE_FLOAT)
2948 flags &= ~CEF_CLR_ICE;
2950 * Casts of float literals to integer type results in
2951 * a constant integer expression [6.6(6)].
2953 else if (old->flags & CEF_FLOAT)
2954 flags = CEF_SET_ICE;
2955 } else if (class & TYPE_PTR) {
2957 * Casts of integer literals to pointer type yield
2958 * address constants [6.6(9)].
2960 * As an extension, treat address constants cast to a
2961 * different pointer type as address constants again.
2963 * As another extension, treat integer constant
2964 * expressions (in contrast to literals) cast to
2965 * pointer type as address constants.
2967 if (old->flags & (CEF_ICE | CEF_ADDR))
2968 flags = CEF_ADDR;
2971 return flags;
2975 // check if a type matches one of the members of a union type
2976 // @utype: the union type
2977 // @type: to type to check
2978 // @return: to identifier of the matching type in the union.
2979 static struct symbol *find_member_type(struct symbol *utype, struct symbol *type)
2981 struct symbol *t, *member;
2983 if (utype->type != SYM_UNION)
2984 return NULL;
2986 FOR_EACH_PTR(utype->symbol_list, member) {
2987 classify_type(member, &t);
2988 if (type == t)
2989 return member;
2990 } END_FOR_EACH_PTR(member);
2991 return NULL;
2994 static struct symbol *evaluate_compound_literal(struct expression *expr, struct expression *source)
2996 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2997 struct symbol *sym = expr->cast_type;
2999 sym->initializer = source;
3000 evaluate_symbol(sym);
3002 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
3003 addr->symbol = sym;
3004 if (sym->ctype.modifiers & MOD_TOPLEVEL)
3005 addr->flags |= CEF_ADDR;
3007 expr->type = EXPR_PREOP;
3008 expr->op = '*';
3009 expr->deref = addr;
3010 expr->ctype = sym;
3011 return sym;
3014 static struct symbol *evaluate_cast(struct expression *expr)
3016 struct expression *source = expr->cast_expression;
3017 struct symbol *ctype;
3018 struct symbol *ttype, *stype;
3019 struct symbol *member;
3020 int tclass, sclass;
3021 struct ident *tas = NULL, *sas = NULL;
3023 if (!source)
3024 return NULL;
3027 * Special case: a cast can be followed by an
3028 * initializer, in which case we need to pass
3029 * the type value down to that initializer rather
3030 * than trying to evaluate it as an expression
3031 * (cfr. compound literals: C99 & C11 6.5.2.5).
3033 * A more complex case is when the initializer is
3034 * dereferenced as part of a post-fix expression.
3035 * We need to produce an expression that can be dereferenced.
3037 if (source->type == EXPR_INITIALIZER)
3038 return evaluate_compound_literal(expr, source);
3040 ctype = examine_symbol_type(expr->cast_type);
3041 ctype = unqualify_type(ctype);
3042 expr->ctype = ctype;
3043 expr->cast_type = ctype;
3045 evaluate_expression(source);
3046 degenerate(source);
3048 tclass = classify_type(ctype, &ttype);
3050 expr->flags = cast_flags(expr, source);
3053 * You can always throw a value away by casting to
3054 * "void" - that's an implicit "force". Note that
3055 * the same is _not_ true of "void *".
3057 if (ttype == &void_ctype)
3058 goto out;
3060 stype = source->ctype;
3061 if (!stype) {
3062 expression_error(expr, "cast from unknown type");
3063 goto out;
3065 sclass = classify_type(stype, &stype);
3067 if (expr->type == EXPR_FORCE_CAST)
3068 goto out;
3070 if (tclass & (TYPE_COMPOUND | TYPE_FN)) {
3072 * Special case: cast to union type (GCC extension)
3073 * The effect is similar to a compound literal except
3074 * that the result is a rvalue.
3076 if ((member = find_member_type(ttype, stype))) {
3077 struct expression *item, *init;
3079 if (Wunion_cast)
3080 warning(expr->pos, "cast to union type");
3082 item = alloc_expression(source->pos, EXPR_IDENTIFIER);
3083 item->expr_ident = member->ident;
3084 item->ident_expression = source;
3086 init = alloc_expression(source->pos, EXPR_INITIALIZER);
3087 add_expression(&init->expr_list, item);
3089 // FIXME: this should be a rvalue
3090 evaluate_compound_literal(expr, init);
3091 return ctype;
3094 warning(expr->pos, "cast to non-scalar");
3097 if (sclass & TYPE_COMPOUND)
3098 warning(expr->pos, "cast from non-scalar");
3100 /* allowed cast unfouls */
3101 if (sclass & TYPE_FOULED)
3102 stype = unfoul(stype);
3104 if (ttype != stype) {
3105 if ((tclass & TYPE_RESTRICT) && restricted_value(source, ttype))
3106 warning(expr->pos, "cast to %s",
3107 show_typename(ttype));
3108 if (sclass & TYPE_RESTRICT) {
3109 if (ttype == &bool_ctype) {
3110 if (sclass & TYPE_FOULED)
3111 warning(expr->pos, "%s degrades to integer",
3112 show_typename(stype));
3113 } else {
3114 warning(expr->pos, "cast from %s",
3115 show_typename(stype));
3120 if ((ttype == &ulong_ctype || ttype == uintptr_ctype) && !Wcast_from_as)
3121 tas = &bad_address_space;
3122 else if (tclass == TYPE_PTR) {
3123 examine_pointer_target(ttype);
3124 tas = ttype->ctype.as;
3127 if ((stype == &ulong_ctype || stype == uintptr_ctype))
3128 sas = &bad_address_space;
3129 else if (sclass == TYPE_PTR) {
3130 examine_pointer_target(stype);
3131 sas = stype->ctype.as;
3134 if (!tas && valid_as(sas))
3135 warning(expr->pos, "cast removes address space '%s' of expression", show_as(sas));
3136 if (valid_as(tas) && valid_as(sas) && tas != sas)
3137 warning(expr->pos, "cast between address spaces (%s -> %s)", show_as(sas), show_as(tas));
3138 if (valid_as(tas) && !sas &&
3139 !is_null_pointer_constant(source) && Wcast_to_as)
3140 warning(expr->pos,
3141 "cast adds address space '%s' to expression", show_as(tas));
3143 if (!(ttype->ctype.modifiers & MOD_PTRINHERIT) && tclass == TYPE_PTR &&
3144 !tas && (source->flags & CEF_ICE)) {
3145 if (ttype->ctype.base_type == &void_ctype) {
3146 if (is_zero_constant(source)) {
3147 /* NULL */
3148 expr->type = EXPR_VALUE;
3149 expr->ctype = &null_ctype;
3150 expr->value = 0;
3151 return expr->ctype;
3156 if (ttype == &bool_ctype)
3157 cast_to_bool(expr);
3159 // checks pointers to restricted
3160 while (Wbitwise_pointer && tclass == TYPE_PTR && sclass == TYPE_PTR) {
3161 tclass = classify_type(ttype->ctype.base_type, &ttype);
3162 sclass = classify_type(stype->ctype.base_type, &stype);
3163 if (ttype == stype)
3164 break;
3165 if (!ttype || !stype)
3166 break;
3167 if (ttype == &void_ctype || stype == &void_ctype)
3168 break;
3169 if (tclass & TYPE_RESTRICT) {
3170 warning(expr->pos, "cast to %s", show_typename(ctype));
3171 break;
3173 if (sclass & TYPE_RESTRICT) {
3174 warning(expr->pos, "cast from %s", show_typename(source->ctype));
3175 break;
3178 out:
3179 return ctype;
3183 * Evaluate a call expression with a symbol. This
3184 * should expand inline functions, and evaluate
3185 * builtins.
3187 static int evaluate_symbol_call(struct expression *expr)
3189 struct expression *fn = expr->fn;
3190 struct symbol *ctype = fn->ctype;
3192 if (fn->type != EXPR_PREOP)
3193 return 0;
3195 if (ctype->op && ctype->op->evaluate)
3196 return ctype->op->evaluate(expr);
3198 return 0;
3201 static struct symbol *evaluate_call(struct expression *expr)
3203 int args, fnargs;
3204 struct symbol *ctype, *sym;
3205 struct expression *fn = expr->fn;
3206 struct expression_list *arglist = expr->args;
3208 if (!evaluate_expression(fn))
3209 return NULL;
3210 sym = ctype = fn->ctype;
3211 if (ctype->type == SYM_NODE)
3212 ctype = ctype->ctype.base_type;
3213 if (ctype->type == SYM_PTR)
3214 ctype = get_base_type(ctype);
3216 if (ctype->type != SYM_FN) {
3217 struct expression *arg;
3219 if (fn->ctype == &bad_ctype)
3220 return NULL;
3222 expression_error(expr, "not a function %s",
3223 show_ident(sym->ident));
3224 /* do typechecking in arguments */
3225 FOR_EACH_PTR (arglist, arg) {
3226 evaluate_expression(arg);
3227 } END_FOR_EACH_PTR(arg);
3228 return NULL;
3231 examine_fn_arguments(ctype);
3232 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
3233 sym->op && sym->op->args) {
3234 if (!sym->op->args(expr))
3235 return NULL;
3236 } else {
3237 if (!evaluate_arguments(ctype->arguments, arglist))
3238 return NULL;
3239 args = expression_list_size(expr->args);
3240 fnargs = symbol_list_size(ctype->arguments);
3241 if (args < fnargs) {
3242 expression_error(expr,
3243 "not enough arguments for function %s",
3244 show_ident(sym->ident));
3245 return NULL;
3247 if (args > fnargs && !ctype->variadic)
3248 expression_error(expr,
3249 "too many arguments for function %s",
3250 show_ident(sym->ident));
3252 expr->ctype = ctype->ctype.base_type;
3253 if (sym->type == SYM_NODE) {
3254 if (evaluate_symbol_call(expr))
3255 return expr->ctype;
3257 return expr->ctype;
3260 static struct symbol *evaluate_offsetof(struct expression *expr)
3262 struct expression *e = expr->down;
3263 struct symbol *ctype = expr->in;
3264 int class;
3266 if (expr->op == '.') {
3267 struct symbol *field;
3268 int offset = 0;
3269 if (!ctype) {
3270 expression_error(expr, "expected structure or union");
3271 return NULL;
3273 examine_symbol_type(ctype);
3274 class = classify_type(ctype, &ctype);
3275 if (class != TYPE_COMPOUND) {
3276 expression_error(expr, "expected structure or union");
3277 return NULL;
3280 field = find_identifier(expr->ident, ctype->symbol_list, &offset);
3281 if (!field) {
3282 expression_error(expr, "unknown member");
3283 return NULL;
3285 ctype = field;
3286 expr->type = EXPR_VALUE;
3287 expr->flags = CEF_SET_ICE;
3288 expr->value = offset;
3289 expr->taint = 0;
3290 expr->ctype = size_t_ctype;
3291 } else {
3292 if (!ctype) {
3293 expression_error(expr, "expected structure or union");
3294 return NULL;
3296 examine_symbol_type(ctype);
3297 class = classify_type(ctype, &ctype);
3298 if (class != (TYPE_COMPOUND | TYPE_PTR)) {
3299 expression_error(expr, "expected array");
3300 return NULL;
3302 ctype = ctype->ctype.base_type;
3303 if (!expr->index) {
3304 expr->type = EXPR_VALUE;
3305 expr->flags = CEF_SET_ICE;
3306 expr->value = 0;
3307 expr->taint = 0;
3308 expr->ctype = size_t_ctype;
3309 } else {
3310 struct expression *idx = expr->index, *m;
3311 struct symbol *i_type = evaluate_expression(idx);
3312 unsigned old_idx_flags;
3313 int i_class = classify_type(i_type, &i_type);
3315 if (!is_int(i_class)) {
3316 expression_error(expr, "non-integer index");
3317 return NULL;
3319 unrestrict(idx, i_class, &i_type);
3320 old_idx_flags = idx->flags;
3321 idx = cast_to(idx, size_t_ctype);
3322 idx->flags = old_idx_flags;
3323 m = alloc_const_expression(expr->pos,
3324 bits_to_bytes(ctype->bit_size));
3325 m->ctype = size_t_ctype;
3326 m->flags = CEF_SET_INT;
3327 expr->type = EXPR_BINOP;
3328 expr->left = idx;
3329 expr->right = m;
3330 expr->op = '*';
3331 expr->ctype = size_t_ctype;
3332 expr->flags = m->flags & idx->flags & ~CEF_CONST_MASK;
3335 if (e) {
3336 struct expression *copy = __alloc_expression(0);
3337 *copy = *expr;
3338 if (e->type == EXPR_OFFSETOF)
3339 e->in = ctype;
3340 if (!evaluate_expression(e))
3341 return NULL;
3342 expr->type = EXPR_BINOP;
3343 expr->flags = e->flags & copy->flags & ~CEF_CONST_MASK;
3344 expr->op = '+';
3345 expr->ctype = size_t_ctype;
3346 expr->left = copy;
3347 expr->right = e;
3349 return size_t_ctype;
3352 static void check_label_declaration(struct position pos, struct symbol *label)
3354 switch (label->namespace) {
3355 case NS_LABEL:
3356 if (label->stmt)
3357 break;
3358 sparse_error(pos, "label '%s' was not declared", show_ident(label->ident));
3359 /* fallthrough */
3360 case NS_NONE:
3361 current_fn->bogus_linear = 1;
3362 default:
3363 break;
3367 static int type_selection(struct symbol *ctrl, struct symbol *type)
3369 struct ctype c = { .base_type = ctrl };
3370 struct ctype t = { .base_type = type };
3372 return !type_difference(&c, &t, 0, 0);
3375 static struct symbol *evaluate_generic_selection(struct expression *expr)
3377 struct type_expression *map;
3378 struct expression *res;
3379 struct symbol source;
3380 struct symbol *ctrl;
3382 if (!evaluate_expression(expr->control))
3383 return NULL;
3384 if (!(ctrl = degenerate(expr->control)))
3385 return NULL;
3387 source = *ctrl;
3388 source.ctype.modifiers &= ~(MOD_QUALIFIER|MOD_ATOMIC);
3389 for (map = expr->map; map; map = map->next) {
3390 struct symbol *stype = map->type;
3391 struct symbol *base;
3393 if (!evaluate_symbol(stype))
3394 continue;
3396 base = stype->ctype.base_type;
3397 if (base->type == SYM_ARRAY && base->array_size) {
3398 get_expression_value_silent(base->array_size);
3399 if (base->array_size->type == EXPR_VALUE)
3400 continue;
3401 sparse_error(stype->pos, "variable length array type in generic selection");
3402 continue;
3404 if (is_func_type(stype)) {
3405 sparse_error(stype->pos, "function type in generic selection");
3406 continue;
3408 if (stype->bit_size <= 0 || is_void_type(stype)) {
3409 sparse_error(stype->pos, "incomplete type in generic selection");
3410 continue;
3412 if (!type_selection(&source, stype))
3413 continue;
3415 res = map->expr;
3416 goto found;
3418 res = expr->def;
3419 if (!res) {
3420 sparse_error(expr->pos, "no generic selection for '%s'", show_typename(ctrl));
3421 return NULL;
3424 found:
3425 *expr = *res;
3426 return evaluate_expression(expr);
3429 struct symbol *evaluate_expression(struct expression *expr)
3431 if (!expr)
3432 return NULL;
3433 if (expr->ctype)
3434 return expr->ctype;
3436 switch (expr->type) {
3437 case EXPR_VALUE:
3438 case EXPR_FVALUE:
3439 expression_error(expr, "value expression without a type");
3440 return NULL;
3441 case EXPR_STRING:
3442 return evaluate_string(expr);
3443 case EXPR_SYMBOL:
3444 return evaluate_symbol_expression(expr);
3445 case EXPR_BINOP:
3446 evaluate_expression(expr->left);
3447 evaluate_expression(expr->right);
3448 if (!valid_subexpr_type(expr))
3449 return NULL;
3450 return evaluate_binop(expr);
3451 case EXPR_LOGICAL:
3452 return evaluate_logical(expr);
3453 case EXPR_COMMA:
3454 evaluate_expression(expr->left);
3455 if (!evaluate_expression(expr->right))
3456 return NULL;
3457 return evaluate_comma(expr);
3458 case EXPR_COMPARE:
3459 evaluate_expression(expr->left);
3460 evaluate_expression(expr->right);
3461 if (!valid_subexpr_type(expr))
3462 return NULL;
3463 return evaluate_compare(expr);
3464 case EXPR_ASSIGNMENT:
3465 evaluate_expression(expr->left);
3466 evaluate_expression(expr->right);
3467 if (!valid_subexpr_type(expr))
3468 return NULL;
3469 return evaluate_assignment(expr);
3470 case EXPR_PREOP:
3471 if (!evaluate_expression(expr->unop))
3472 return NULL;
3473 return evaluate_preop(expr);
3474 case EXPR_POSTOP:
3475 if (!evaluate_expression(expr->unop))
3476 return NULL;
3477 return evaluate_postop(expr);
3478 case EXPR_CAST:
3479 case EXPR_FORCE_CAST:
3480 case EXPR_IMPLIED_CAST:
3481 return evaluate_cast(expr);
3482 case EXPR_SIZEOF:
3483 return evaluate_sizeof(expr);
3484 case EXPR_PTRSIZEOF:
3485 return evaluate_ptrsizeof(expr);
3486 case EXPR_ALIGNOF:
3487 return evaluate_alignof(expr);
3488 case EXPR_DEREF:
3489 return evaluate_member_dereference(expr);
3490 case EXPR_CALL:
3491 return evaluate_call(expr);
3492 case EXPR_SELECT:
3493 case EXPR_CONDITIONAL:
3494 return evaluate_conditional_expression(expr);
3495 case EXPR_STATEMENT:
3496 expr->ctype = evaluate_statement(expr->statement);
3497 return expr->ctype;
3499 case EXPR_LABEL:
3500 expr->ctype = &ptr_ctype;
3501 check_label_declaration(expr->pos, expr->label_symbol);
3502 return &ptr_ctype;
3504 case EXPR_TYPE:
3505 /* Evaluate the type of the symbol .. */
3506 evaluate_symbol(expr->symbol);
3507 /* .. but the type of the _expression_ is a "type" */
3508 expr->ctype = &type_ctype;
3509 return &type_ctype;
3511 case EXPR_OFFSETOF:
3512 return evaluate_offsetof(expr);
3514 case EXPR_GENERIC:
3515 return evaluate_generic_selection(expr);
3517 /* These can not exist as stand-alone expressions */
3518 case EXPR_INITIALIZER:
3519 case EXPR_IDENTIFIER:
3520 case EXPR_INDEX:
3521 case EXPR_POS:
3522 expression_error(expr, "internal front-end error: initializer in expression");
3523 return NULL;
3524 case EXPR_SLICE:
3525 expression_error(expr, "internal front-end error: SLICE re-evaluated");
3526 return NULL;
3528 return NULL;
3531 void check_duplicates(struct symbol *sym)
3533 int declared = 0;
3534 struct symbol *next = sym;
3535 int initialized = sym->initializer != NULL;
3537 while ((next = next->same_symbol) != NULL) {
3538 const char *typediff;
3539 evaluate_symbol(next);
3540 if (initialized && next->initializer) {
3541 sparse_error(sym->pos, "symbol '%s' has multiple initializers (originally initialized at %s:%d)",
3542 show_ident(sym->ident),
3543 stream_name(next->pos.stream), next->pos.line);
3544 /* Only warn once */
3545 initialized = 0;
3547 declared++;
3548 typediff = type_difference(&sym->ctype, &next->ctype, 0, 0);
3549 if (typediff) {
3550 sparse_error(sym->pos, "symbol '%s' redeclared with different type (%s):",
3551 show_ident(sym->ident), typediff);
3552 info(sym->pos, " %s", show_typename(sym));
3553 info(next->pos, "note: previously declared as:");
3554 info(next->pos, " %s", show_typename(next));
3555 return;
3558 if (!declared) {
3559 unsigned long mod = sym->ctype.modifiers;
3560 if (mod & (MOD_STATIC | MOD_REGISTER | MOD_EXT_VISIBLE))
3561 return;
3562 if (!(mod & MOD_TOPLEVEL))
3563 return;
3564 if (!Wdecl)
3565 return;
3566 if (sym->ident == &main_ident)
3567 return;
3568 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
3572 static struct symbol *evaluate_symbol(struct symbol *sym)
3574 struct symbol *base_type;
3576 if (!sym)
3577 return sym;
3578 if (sym->evaluated)
3579 return sym;
3580 sym->evaluated = 1;
3582 sym = examine_symbol_type(sym);
3583 base_type = get_base_type(sym);
3584 if (!base_type)
3585 return NULL;
3587 /* Evaluate the initializers */
3588 if (sym->initializer)
3589 evaluate_initializer(sym, &sym->initializer);
3591 /* And finally, evaluate the body of the symbol too */
3592 if (base_type->type == SYM_FN) {
3593 struct symbol *curr = current_fn;
3595 if (sym->definition && sym->definition != sym)
3596 return evaluate_symbol(sym->definition);
3598 current_fn = sym;
3600 examine_fn_arguments(base_type);
3601 if (!base_type->stmt && base_type->inline_stmt)
3602 uninline(sym);
3603 if (base_type->stmt)
3604 evaluate_statement(base_type->stmt);
3606 current_fn = curr;
3609 return base_type;
3612 void evaluate_symbol_list(struct symbol_list *list)
3614 struct symbol *sym;
3616 FOR_EACH_PTR(list, sym) {
3617 has_error &= ~ERROR_CURR_PHASE;
3618 evaluate_symbol(sym);
3619 check_duplicates(sym);
3620 } END_FOR_EACH_PTR(sym);
3623 static struct symbol *evaluate_return_expression(struct statement *stmt)
3625 struct expression *expr = stmt->expression;
3626 struct symbol *fntype, *rettype;
3628 evaluate_expression(expr);
3629 fntype = current_fn->ctype.base_type;
3630 rettype = fntype->ctype.base_type;
3631 if (!rettype || rettype == &void_ctype) {
3632 if (expr && expr->ctype && !is_void_type(expr->ctype))
3633 expression_error(expr, "return expression in %s function", rettype?"void":"typeless");
3634 if (expr && Wreturn_void)
3635 warning(stmt->pos, "returning void-valued expression");
3636 return NULL;
3639 if (!expr) {
3640 sparse_error(stmt->pos, "return with no return value");
3641 return NULL;
3643 if (!expr->ctype)
3644 return NULL;
3645 compatible_assignment_types(expr, rettype, &stmt->expression, "return expression");
3646 return NULL;
3649 static void evaluate_if_statement(struct statement *stmt)
3651 if (!stmt->if_conditional)
3652 return;
3654 evaluate_conditional(stmt->if_conditional, 0);
3655 evaluate_statement(stmt->if_true);
3656 evaluate_statement(stmt->if_false);
3659 static void evaluate_iterator(struct statement *stmt)
3661 evaluate_symbol_list(stmt->iterator_syms);
3662 evaluate_conditional(stmt->iterator_pre_condition, 1);
3663 evaluate_conditional(stmt->iterator_post_condition,1);
3664 evaluate_statement(stmt->iterator_pre_statement);
3665 evaluate_statement(stmt->iterator_statement);
3666 evaluate_statement(stmt->iterator_post_statement);
3670 static void parse_asm_constraint(struct asm_operand *op)
3672 struct expression *constraint = op->constraint;
3673 const char *str = constraint->string->data;
3674 int c;
3676 switch (str[0]) {
3677 case '\0':
3678 sparse_error(constraint->pos, "invalid ASM constraint (\"\")");
3679 break;
3680 case '+':
3681 op->is_modify = true;
3682 /* fall-through */
3683 case '=':
3684 op->is_assign = true;
3685 str++;
3686 break;
3689 while ((c = *str++)) {
3690 switch (c) {
3691 case '=':
3692 case '+':
3693 sparse_error(constraint->pos, "invalid ASM constraint '%c'", c);
3694 break;
3696 case '&':
3697 op->is_earlyclobber = true;
3698 break;
3699 case '%':
3700 op->is_commutative = true;
3701 break;
3702 case 'r':
3703 op->is_register = true;
3704 break;
3706 case 'm':
3707 case 'o':
3708 case 'V':
3709 case 'Q':
3710 op->is_memory = true;
3711 break;
3713 case '<':
3714 case '>':
3715 // FIXME: ignored for now
3716 break;
3718 case ',':
3719 // FIXME: multiple alternative constraints
3720 break;
3722 case '0' ... '9':
3723 // FIXME: numeric matching constraint?
3724 break;
3725 case '[':
3726 // FIXME: symbolic matching constraint
3727 return;
3729 default:
3730 if (arch_target->asm_constraint)
3731 str = arch_target->asm_constraint(op, c, str);
3733 // FIXME: multi-letter constraints
3734 break;
3738 // FIXME: how to deal with multi-constraint?
3739 if (op->is_register)
3740 op->is_memory = 0;
3743 static void verify_output_constraint(struct asm_operand *op)
3745 struct expression *expr = op->constraint;
3746 const char *constraint = expr->string->data;
3748 if (!op->is_assign)
3749 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
3752 static void verify_input_constraint(struct asm_operand *op)
3754 struct expression *expr = op->constraint;
3755 const char *constraint = expr->string->data;
3757 if (op->is_assign)
3758 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
3761 static void evaluate_asm_memop(struct asm_operand *op)
3763 if (op->is_memory) {
3764 struct expression *expr = op->expr;
3765 struct expression *addr;
3767 // implicit addressof
3768 addr = alloc_expression(expr->pos, EXPR_PREOP);
3769 addr->op = '&';
3770 addr->unop = expr;
3772 evaluate_addressof(addr);
3773 op->expr = addr;
3774 } else {
3775 evaluate_expression(op->expr);
3776 degenerate(op->expr);
3780 static void evaluate_asm_statement(struct statement *stmt)
3782 struct expression *expr;
3783 struct asm_operand *op;
3784 struct symbol *sym;
3786 if (!stmt->asm_string)
3787 return;
3789 FOR_EACH_PTR(stmt->asm_outputs, op) {
3790 /* Identifier */
3792 /* Constraint */
3793 if (op->constraint) {
3794 parse_asm_constraint(op);
3795 verify_output_constraint(op);
3798 /* Expression */
3799 expr = op->expr;
3800 if (!evaluate_expression(expr))
3801 return;
3802 if (!lvalue_expression(expr))
3803 warning(expr->pos, "asm output is not an lvalue");
3804 evaluate_assign_to(expr, expr->ctype);
3805 evaluate_asm_memop(op);
3806 } END_FOR_EACH_PTR(op);
3808 FOR_EACH_PTR(stmt->asm_inputs, op) {
3809 /* Identifier */
3811 /* Constraint */
3812 if (op->constraint) {
3813 parse_asm_constraint(op);
3814 verify_input_constraint(op);
3817 /* Expression */
3818 if (!evaluate_expression(op->expr))
3819 return;
3820 evaluate_asm_memop(op);
3821 } END_FOR_EACH_PTR(op);
3823 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
3824 if (!expr) {
3825 sparse_error(stmt->pos, "bad asm clobbers");
3826 return;
3828 if (expr->type == EXPR_STRING)
3829 continue;
3830 expression_error(expr, "asm clobber is not a string");
3831 } END_FOR_EACH_PTR(expr);
3833 FOR_EACH_PTR(stmt->asm_labels, sym) {
3834 if (!sym || sym->type != SYM_LABEL) {
3835 sparse_error(stmt->pos, "bad asm label");
3836 return;
3838 } END_FOR_EACH_PTR(sym);
3841 static void evaluate_case_statement(struct statement *stmt)
3843 evaluate_expression(stmt->case_expression);
3844 evaluate_expression(stmt->case_to);
3845 evaluate_statement(stmt->case_statement);
3848 static void check_case_type(struct expression *switch_expr,
3849 struct expression *case_expr,
3850 struct expression **enumcase)
3852 struct symbol *switch_type, *case_type;
3853 int sclass, cclass;
3855 if (!case_expr)
3856 return;
3858 switch_type = switch_expr->ctype;
3859 case_type = evaluate_expression(case_expr);
3861 if (!switch_type || !case_type)
3862 goto Bad;
3863 if (enumcase) {
3864 if (*enumcase)
3865 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
3866 else if (is_enum_type(case_type))
3867 *enumcase = case_expr;
3870 sclass = classify_type(switch_type, &switch_type);
3871 cclass = classify_type(case_type, &case_type);
3873 /* both should be arithmetic */
3874 if (!(sclass & cclass & TYPE_NUM))
3875 goto Bad;
3877 /* neither should be floating */
3878 if ((sclass | cclass) & TYPE_FLOAT)
3879 goto Bad;
3881 /* if neither is restricted, we are OK */
3882 if (!((sclass | cclass) & TYPE_RESTRICT))
3883 return;
3885 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
3886 cclass, sclass, case_type, switch_type)) {
3887 unrestrict(case_expr, cclass, &case_type);
3888 unrestrict(switch_expr, sclass, &switch_type);
3890 return;
3892 Bad:
3893 expression_error(case_expr, "incompatible types for 'case' statement");
3896 static void evaluate_switch_statement(struct statement *stmt)
3898 struct symbol *sym;
3899 struct expression *enumcase = NULL;
3900 struct expression **enumcase_holder = &enumcase;
3901 struct expression *sel = stmt->switch_expression;
3903 evaluate_expression(sel);
3904 evaluate_statement(stmt->switch_statement);
3905 if (!sel)
3906 return;
3907 if (sel->ctype && is_enum_type(sel->ctype))
3908 enumcase_holder = NULL; /* Only check cases against switch */
3910 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
3911 struct statement *case_stmt = sym->stmt;
3912 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
3913 check_case_type(sel, case_stmt->case_to, enumcase_holder);
3914 } END_FOR_EACH_PTR(sym);
3917 static void evaluate_goto_statement(struct statement *stmt)
3919 struct symbol *label = stmt->goto_label;
3921 if (!label) {
3922 // no label associated, may be a computed goto
3923 evaluate_expression(stmt->goto_expression);
3924 return;
3927 check_label_declaration(stmt->pos, label);
3930 struct symbol *evaluate_statement(struct statement *stmt)
3932 if (!stmt)
3933 return NULL;
3935 switch (stmt->type) {
3936 case STMT_DECLARATION: {
3937 struct symbol *s;
3938 FOR_EACH_PTR(stmt->declaration, s) {
3939 evaluate_symbol(s);
3940 } END_FOR_EACH_PTR(s);
3941 return NULL;
3944 case STMT_RETURN:
3945 return evaluate_return_expression(stmt);
3947 case STMT_EXPRESSION:
3948 if (!evaluate_expression(stmt->expression))
3949 return NULL;
3950 if (stmt->expression->ctype == &null_ctype)
3951 stmt->expression = cast_to(stmt->expression, &ptr_ctype);
3952 return unqualify_type(degenerate(stmt->expression));
3954 case STMT_COMPOUND: {
3955 struct statement *s;
3956 struct symbol *type = NULL;
3958 /* Evaluate the return symbol in the compound statement */
3959 evaluate_symbol(stmt->ret);
3962 * Then, evaluate each statement, making the type of the
3963 * compound statement be the type of the last statement
3965 type = evaluate_statement(stmt->args);
3966 FOR_EACH_PTR(stmt->stmts, s) {
3967 type = evaluate_statement(s);
3968 } END_FOR_EACH_PTR(s);
3969 if (!type)
3970 type = &void_ctype;
3971 return type;
3973 case STMT_IF:
3974 evaluate_if_statement(stmt);
3975 return NULL;
3976 case STMT_ITERATOR:
3977 evaluate_iterator(stmt);
3978 return NULL;
3979 case STMT_SWITCH:
3980 evaluate_switch_statement(stmt);
3981 return NULL;
3982 case STMT_CASE:
3983 evaluate_case_statement(stmt);
3984 return NULL;
3985 case STMT_LABEL:
3986 return evaluate_statement(stmt->label_statement);
3987 case STMT_GOTO:
3988 evaluate_goto_statement(stmt);
3989 return NULL;
3990 case STMT_NONE:
3991 break;
3992 case STMT_ASM:
3993 evaluate_asm_statement(stmt);
3994 return NULL;
3995 case STMT_CONTEXT:
3996 evaluate_expression(stmt->expression);
3997 return NULL;
3998 case STMT_RANGE:
3999 evaluate_expression(stmt->range_expression);
4000 evaluate_expression(stmt->range_low);
4001 evaluate_expression(stmt->range_high);
4002 return NULL;
4004 return NULL;