return_to_param: delete debug code
[smatch.git] / evaluate.c
blob8e03b4fa45c7228a4395a5179dd2b4f28b98c31b
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 *evaluate_symbol_expression(struct expression *expr)
66 struct expression *addr;
67 struct symbol *sym = expr->symbol;
68 struct symbol *base_type;
70 if (!sym) {
71 expression_error(expr, "undefined identifier '%s'", show_ident(expr->symbol_name));
72 return NULL;
75 examine_symbol_type(sym);
77 base_type = get_base_type(sym);
78 if (!base_type) {
79 expression_error(expr, "identifier '%s' has no type", show_ident(expr->symbol_name));
80 return NULL;
83 addr = alloc_expression(expr->pos, EXPR_SYMBOL);
84 addr->symbol = sym;
85 addr->symbol_name = expr->symbol_name;
86 addr->ctype = &lazy_ptr_ctype; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
87 addr->flags = expr->flags;
88 expr->type = EXPR_PREOP;
89 expr->op = '*';
90 expr->unop = addr;
91 expr->flags = CEF_NONE;
93 /* The type of a symbol is the symbol itself! */
94 expr->ctype = sym;
95 return sym;
98 static struct symbol *evaluate_string(struct expression *expr)
100 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
101 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
102 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
103 struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
104 unsigned int length = expr->string->length;
105 struct symbol *char_type = expr->wide ? wchar_ctype : &char_ctype;
107 sym->array_size = alloc_const_expression(expr->pos, length);
108 sym->bit_size = length * char_type->bit_size;
109 sym->ctype.alignment = 1;
110 sym->string = 1;
111 sym->ctype.modifiers = MOD_STATIC;
112 sym->ctype.base_type = array;
113 sym->initializer = initstr;
114 sym->examined = 1;
115 sym->evaluated = 1;
117 initstr->ctype = sym;
118 initstr->string = expr->string;
120 array->array_size = sym->array_size;
121 array->bit_size = sym->bit_size;
122 array->ctype.alignment = char_type->ctype.alignment;
123 array->ctype.modifiers = MOD_STATIC;
124 array->ctype.base_type = char_type;
125 array->examined = 1;
126 array->evaluated = 1;
128 addr->symbol = sym;
129 addr->ctype = &lazy_ptr_ctype;
130 addr->flags = CEF_ADDR;
132 expr->type = EXPR_PREOP;
133 expr->op = '*';
134 expr->unop = addr;
135 expr->ctype = sym;
136 return sym;
139 /* type has come from classify_type and is an integer type */
140 static inline struct symbol *integer_promotion(struct symbol *type)
142 unsigned long mod = type->ctype.modifiers;
143 int width = type->bit_size;
146 * Bitfields always promote to the base type,
147 * even if the bitfield might be bigger than
148 * an "int".
150 if (type->type == SYM_BITFIELD) {
151 type = type->ctype.base_type;
153 mod = type->ctype.modifiers;
154 if (width < bits_in_int)
155 return &int_ctype;
157 /* If char/short has as many bits as int, it still gets "promoted" */
158 if (type->rank < 0) {
159 if (mod & MOD_UNSIGNED)
160 return &uint_ctype;
161 return &int_ctype;
163 return type;
167 * integer part of usual arithmetic conversions:
168 * integer promotions are applied
169 * if left and right are identical, we are done
170 * if signedness is the same, convert one with lower rank
171 * unless unsigned argument has rank lower than signed one, convert the
172 * signed one.
173 * if signed argument is bigger than unsigned one, convert the unsigned.
174 * otherwise, convert signed.
176 * Leaving aside the integer promotions, that is equivalent to
177 * if identical, don't convert
178 * if left is bigger than right, convert right
179 * if right is bigger than left, convert right
180 * otherwise, if signedness is the same, convert one with lower rank
181 * otherwise convert the signed one.
183 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
185 unsigned long lmod, rmod;
187 left = integer_promotion(left);
188 right = integer_promotion(right);
190 if (left == right)
191 goto left;
193 if (left->bit_size > right->bit_size)
194 goto left;
196 if (right->bit_size > left->bit_size)
197 goto right;
199 lmod = left->ctype.modifiers;
200 rmod = right->ctype.modifiers;
201 if ((lmod ^ rmod) & MOD_UNSIGNED) {
202 if (lmod & MOD_UNSIGNED)
203 goto left;
204 } else if (left->rank > right->rank)
205 goto left;
206 right:
207 left = right;
208 left:
209 return left;
212 static int same_cast_type(struct symbol *orig, struct symbol *new)
214 return orig->bit_size == new->bit_size &&
215 orig->bit_offset == new->bit_offset;
218 static struct symbol *base_type(struct symbol *node, unsigned long *modp, struct ident **asp)
220 unsigned long mod = 0;
221 struct ident *as = NULL;
223 while (node) {
224 mod |= node->ctype.modifiers;
225 combine_address_space(node->pos, &as, node->ctype.as);
226 if (node->type == SYM_NODE) {
227 node = node->ctype.base_type;
228 continue;
230 break;
232 *modp = mod & ~MOD_IGNORE;
233 *asp = as;
234 return node;
237 static int is_same_type(struct expression *expr, struct symbol *new)
239 struct symbol *old = expr->ctype;
240 unsigned long oldmod, newmod;
241 struct ident *oldas, *newas;
243 old = base_type(old, &oldmod, &oldas);
244 new = base_type(new, &newmod, &newas);
246 /* Same base type, same address space? */
247 if (old == new && oldas == newas) {
248 unsigned long difmod;
250 /* Check the modifier bits. */
251 difmod = (oldmod ^ newmod) & ~MOD_NOCAST;
253 /* Exact same type? */
254 if (!difmod)
255 return 1;
258 * Not the same type, but differs only in "const".
259 * Don't warn about MOD_NOCAST.
261 if (difmod == MOD_CONST)
262 return 0;
264 if ((oldmod | newmod) & MOD_NOCAST) {
265 const char *tofrom = "to/from";
266 if (!(newmod & MOD_NOCAST))
267 tofrom = "from";
268 if (!(oldmod & MOD_NOCAST))
269 tofrom = "to";
270 warning(expr->pos, "implicit cast %s nocast type", tofrom);
272 return 0;
275 static void
276 warn_for_different_enum_types (struct position pos,
277 struct symbol *typea,
278 struct symbol *typeb)
280 if (!Wenum_mismatch)
281 return;
282 if (typea->type == SYM_NODE)
283 typea = typea->ctype.base_type;
284 if (typeb->type == SYM_NODE)
285 typeb = typeb->ctype.base_type;
287 if (typea == typeb)
288 return;
290 if (typea->type == SYM_ENUM && typeb->type == SYM_ENUM) {
291 warning(pos, "mixing different enum types:");
292 info(pos, " %s", show_typename(typea));
293 info(pos, " %s", show_typename(typeb));
297 static int cast_flags(struct expression *expr, struct expression *target);
298 static struct symbol *cast_to_bool(struct expression *expr);
301 * This gets called for implicit casts in assignments and
302 * integer promotion. We often want to try to move the
303 * cast down, because the ops involved may have been
304 * implicitly cast up, and we can get rid of the casts
305 * early.
307 static struct expression * cast_to(struct expression *old, struct symbol *type)
309 struct expression *expr;
311 warn_for_different_enum_types (old->pos, old->ctype, type);
313 if (old->ctype != &null_ctype && is_same_type(old, type))
314 return old;
317 * See if we can simplify the op. Move the cast down.
319 switch (old->type) {
320 case EXPR_PREOP:
321 if (old->ctype->bit_size < type->bit_size)
322 break;
323 if (old->op == '~') {
324 old->ctype = type;
325 old->unop = cast_to(old->unop, type);
326 return old;
328 break;
330 case EXPR_IMPLIED_CAST:
331 warn_for_different_enum_types(old->pos, old->ctype, type);
333 if (old->ctype->bit_size >= type->bit_size) {
334 struct expression *orig = old->cast_expression;
335 if (same_cast_type(orig->ctype, type))
336 return orig;
337 if (old->ctype->bit_offset == type->bit_offset) {
338 old->ctype = type;
339 old->cast_type = type;
340 return old;
343 break;
345 default:
346 /* nothing */;
349 expr = alloc_expression(old->pos, EXPR_IMPLIED_CAST);
350 expr->ctype = type;
351 expr->cast_type = type;
352 expr->cast_expression = old;
353 expr->flags = cast_flags(expr, old);
355 if (is_bool_type(type))
356 cast_to_bool(expr);
358 return expr;
361 enum {
362 TYPE_NUM = 1,
363 TYPE_BITFIELD = 2,
364 TYPE_RESTRICT = 4,
365 TYPE_FLOAT = 8,
366 TYPE_PTR = 16,
367 TYPE_COMPOUND = 32,
368 TYPE_FOULED = 64,
369 TYPE_FN = 128,
372 static inline int classify_type(struct symbol *type, struct symbol **base)
374 static int type_class[SYM_BAD + 1] = {
375 [SYM_PTR] = TYPE_PTR,
376 [SYM_FN] = TYPE_PTR | TYPE_FN,
377 [SYM_ARRAY] = TYPE_PTR | TYPE_COMPOUND,
378 [SYM_STRUCT] = TYPE_COMPOUND,
379 [SYM_UNION] = TYPE_COMPOUND,
380 [SYM_BITFIELD] = TYPE_NUM | TYPE_BITFIELD,
381 [SYM_RESTRICT] = TYPE_NUM | TYPE_RESTRICT,
382 [SYM_FOULED] = TYPE_NUM | TYPE_RESTRICT | TYPE_FOULED,
384 if (type->type == SYM_NODE)
385 type = type->ctype.base_type;
386 if (type->type == SYM_TYPEOF) {
387 type = examine_symbol_type(type);
388 if (type->type == SYM_NODE)
389 type = type->ctype.base_type;
391 if (type->type == SYM_ENUM)
392 type = type->ctype.base_type;
393 *base = type;
394 if (type->type == SYM_BASETYPE) {
395 if (type->ctype.base_type == &int_type)
396 return TYPE_NUM;
397 if (type->ctype.base_type == &fp_type)
398 return TYPE_NUM | TYPE_FLOAT;
400 return type_class[type->type];
403 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
405 static inline int is_string_type(struct symbol *type)
407 if (type->type == SYM_NODE)
408 type = type->ctype.base_type;
409 if (type->type != SYM_ARRAY)
410 return 0;
411 type = type->ctype.base_type;
412 return is_byte_type(type) || is_wchar_type(type);
415 static struct symbol *bad_expr_type(struct expression *expr)
417 switch (expr->type) {
418 case EXPR_BINOP:
419 case EXPR_COMPARE:
420 if (!valid_subexpr_type(expr))
421 break;
422 sparse_error(expr->pos, "incompatible types for operation (%s):", show_special(expr->op));
423 info(expr->pos, " %s", show_typename(expr->left->ctype));
424 info(expr->pos, " %s", show_typename(expr->right->ctype));
425 break;
426 case EXPR_PREOP:
427 case EXPR_POSTOP:
428 if (!valid_expr_type(expr->unop))
429 break;
430 sparse_error(expr->pos, "incompatible type for operation (%s):", show_special(expr->op));
431 info(expr->pos, " %s", show_typename(expr->unop->ctype));
432 break;
433 default:
434 break;
437 expr->flags = CEF_NONE;
438 return expr->ctype = &bad_ctype;
441 static int restricted_value(struct expression *v, struct symbol *type)
443 if (v->type != EXPR_VALUE)
444 return 1;
445 if (v->value != 0)
446 return 1;
447 return 0;
450 static int restricted_binop(int op, struct symbol *type)
452 switch (op) {
453 case '&':
454 case '=':
455 case SPECIAL_AND_ASSIGN:
456 case SPECIAL_OR_ASSIGN:
457 case SPECIAL_XOR_ASSIGN:
458 return 1; /* unfoul */
459 case '|':
460 case '^':
461 case '?':
462 return 2; /* keep fouled */
463 case SPECIAL_EQUAL:
464 case SPECIAL_NOTEQUAL:
465 return 3; /* warn if fouled */
466 default:
467 return 0; /* warn */
471 static int restricted_unop(int op, struct symbol **type)
473 if (op == '~') {
474 if ((*type)->bit_size < bits_in_int)
475 *type = befoul(*type);
476 return 0;
477 } if (op == '+')
478 return 0;
479 return 1;
482 /* type should be SYM_FOULED */
483 static inline struct symbol *unfoul(struct symbol *type)
485 return type->ctype.base_type;
488 static struct symbol *restricted_binop_type(int op,
489 struct expression *left,
490 struct expression *right,
491 int lclass, int rclass,
492 struct symbol *ltype,
493 struct symbol *rtype)
495 struct symbol *ctype = NULL;
496 if (lclass & TYPE_RESTRICT) {
497 if (rclass & TYPE_RESTRICT) {
498 if (ltype == rtype) {
499 ctype = ltype;
500 } else if (lclass & TYPE_FOULED) {
501 if (unfoul(ltype) == rtype)
502 ctype = ltype;
503 } else if (rclass & TYPE_FOULED) {
504 if (unfoul(rtype) == ltype)
505 ctype = rtype;
507 } else {
508 if (!restricted_value(right, ltype))
509 ctype = ltype;
511 } else if (!restricted_value(left, rtype))
512 ctype = rtype;
514 if (ctype) {
515 switch (restricted_binop(op, ctype)) {
516 case 1:
517 if ((lclass ^ rclass) & TYPE_FOULED)
518 ctype = unfoul(ctype);
519 break;
520 case 3:
521 if (!(lclass & rclass & TYPE_FOULED))
522 break;
523 case 0:
524 ctype = NULL;
525 default:
526 break;
530 return ctype;
533 static inline void unrestrict(struct expression *expr,
534 int class, struct symbol **ctype)
536 if (class & TYPE_RESTRICT) {
537 if (class & TYPE_FOULED)
538 *ctype = unfoul(*ctype);
539 warning(expr->pos, "%s degrades to integer",
540 show_typename(*ctype));
541 *ctype = (*ctype)->ctype.base_type; /* get to arithmetic type */
545 static struct symbol *usual_conversions(int op,
546 struct expression *left,
547 struct expression *right,
548 int lclass, int rclass,
549 struct symbol *ltype,
550 struct symbol *rtype)
552 struct symbol *ctype;
554 warn_for_different_enum_types(right->pos, left->ctype, right->ctype);
556 if ((lclass | rclass) & TYPE_RESTRICT)
557 goto Restr;
559 Normal:
560 if (!(lclass & TYPE_FLOAT)) {
561 if (!(rclass & TYPE_FLOAT))
562 return bigger_int_type(ltype, rtype);
563 else
564 return rtype;
565 } else if (rclass & TYPE_FLOAT) {
566 if (rtype->rank > ltype->rank)
567 return rtype;
568 else
569 return ltype;
570 } else
571 return ltype;
573 Restr:
574 ctype = restricted_binop_type(op, left, right,
575 lclass, rclass, ltype, rtype);
576 if (ctype)
577 return ctype;
579 unrestrict(left, lclass, &ltype);
580 unrestrict(right, rclass, &rtype);
582 goto Normal;
585 static inline int lvalue_expression(struct expression *expr)
587 return expr->type == EXPR_PREOP && expr->op == '*';
590 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *itype)
592 struct expression *index = expr->right;
593 struct symbol *ctype, *base;
594 int multiply;
596 classify_type(degenerate(expr->left), &ctype);
597 base = examine_pointer_target(ctype);
600 * An address constant +/- an integer constant expression
601 * yields an address constant again [6.6(7)].
603 if ((expr->left->flags & CEF_ADDR) && (expr->right->flags & CEF_ICE))
604 expr->flags = CEF_ADDR;
606 if (!base) {
607 expression_error(expr, "missing type information");
608 return NULL;
610 if (is_function(base)) {
611 expression_error(expr, "arithmetics on pointers to functions");
612 return NULL;
615 /* Get the size of whatever the pointer points to */
616 multiply = is_void_type(base) ? 1 : bits_to_bytes(base->bit_size);
618 if (ctype == &null_ctype)
619 ctype = &ptr_ctype;
620 expr->ctype = ctype;
622 if (multiply == 1 && itype->bit_size == bits_in_pointer)
623 return ctype;
625 if (index->type == EXPR_VALUE) {
626 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
627 unsigned long long v = index->value, mask;
628 mask = 1ULL << (itype->bit_size - 1);
629 if (v & mask)
630 v |= -mask;
631 else
632 v &= mask - 1;
633 v *= multiply;
634 mask = 1ULL << (bits_in_pointer - 1);
635 v &= mask | (mask - 1);
636 val->value = v;
637 val->ctype = ssize_t_ctype;
638 expr->right = val;
639 return ctype;
642 if (itype->bit_size != bits_in_pointer)
643 index = cast_to(index, ssize_t_ctype);
645 if (multiply > 1) {
646 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
647 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
649 val->ctype = ssize_t_ctype;
650 val->value = multiply;
652 mul->op = '*';
653 mul->ctype = ssize_t_ctype;
654 mul->left = index;
655 mul->right = val;
656 index = mul;
659 expr->right = index;
660 return ctype;
663 static void examine_fn_arguments(struct symbol *fn);
665 #define MOD_IGN (MOD_QUALIFIER | MOD_FUN_ATTR)
667 const char *type_difference(struct ctype *c1, struct ctype *c2,
668 unsigned long mod1, unsigned long mod2)
670 struct ident *as1 = c1->as, *as2 = c2->as;
671 struct symbol *t1 = c1->base_type;
672 struct symbol *t2 = c2->base_type;
673 int move1 = 1, move2 = 1;
674 mod1 |= c1->modifiers;
675 mod2 |= c2->modifiers;
676 for (;;) {
677 unsigned long diff;
678 int type;
679 struct symbol *base1 = t1->ctype.base_type;
680 struct symbol *base2 = t2->ctype.base_type;
683 * FIXME! Collect alignment and context too here!
685 if (move1) {
686 if (t1 && t1->type != SYM_PTR) {
687 mod1 |= t1->ctype.modifiers;
688 combine_address_space(t1->pos, &as1, t1->ctype.as);
690 move1 = 0;
693 if (move2) {
694 if (t2 && t2->type != SYM_PTR) {
695 mod2 |= t2->ctype.modifiers;
696 combine_address_space(t2->pos, &as2, t2->ctype.as);
698 move2 = 0;
701 if (t1 == t2)
702 break;
703 if (!t1 || !t2)
704 return "different types";
706 if (t1->type == SYM_NODE || t1->type == SYM_ENUM) {
707 t1 = base1;
708 move1 = 1;
709 if (!t1)
710 return "bad types";
711 continue;
714 if (t2->type == SYM_NODE || t2->type == SYM_ENUM) {
715 t2 = base2;
716 move2 = 1;
717 if (!t2)
718 return "bad types";
719 continue;
722 move1 = move2 = 1;
723 type = t1->type;
724 if (type != t2->type)
725 return "different base types";
727 switch (type) {
728 default:
729 sparse_error(t1->pos,
730 "internal error: bad type in derived(%d)",
731 type);
732 return "bad types";
733 case SYM_RESTRICT:
734 return "different base types";
735 case SYM_UNION:
736 case SYM_STRUCT:
737 /* allow definition of incomplete structs and unions */
738 if (t1->ident == t2->ident)
739 return NULL;
740 return "different base types";
741 case SYM_ARRAY:
742 /* XXX: we ought to compare sizes */
743 break;
744 case SYM_PTR:
745 if (as1 != as2)
746 return "different address spaces";
747 /* MOD_SPECIFIER is due to idiocy in parse.c */
748 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SPECIFIER)
749 return "different modifiers";
750 /* we could be lazier here */
751 base1 = examine_pointer_target(t1);
752 base2 = examine_pointer_target(t2);
753 mod1 = t1->ctype.modifiers;
754 as1 = t1->ctype.as;
755 mod2 = t2->ctype.modifiers;
756 as2 = t2->ctype.as;
757 break;
758 case SYM_FN: {
759 struct symbol *arg1, *arg2;
760 int i;
762 if (as1 != as2)
763 return "different address spaces";
764 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
765 return "different modifiers";
766 mod1 = t1->ctype.modifiers;
767 as1 = t1->ctype.as;
768 mod2 = t2->ctype.modifiers;
769 as2 = t2->ctype.as;
771 if (t1->variadic != t2->variadic)
772 return "incompatible variadic arguments";
773 examine_fn_arguments(t1);
774 examine_fn_arguments(t2);
775 PREPARE_PTR_LIST(t1->arguments, arg1);
776 PREPARE_PTR_LIST(t2->arguments, arg2);
777 i = 1;
778 for (;;) {
779 const char *diffstr;
780 if (!arg1 && !arg2)
781 break;
782 if (!arg1 || !arg2)
783 return "different argument counts";
784 diffstr = type_difference(&arg1->ctype,
785 &arg2->ctype,
786 MOD_IGN, MOD_IGN);
787 if (diffstr) {
788 static char argdiff[80];
789 sprintf(argdiff, "incompatible argument %d (%s)", i, diffstr);
790 return argdiff;
792 NEXT_PTR_LIST(arg1);
793 NEXT_PTR_LIST(arg2);
794 i++;
796 FINISH_PTR_LIST(arg2);
797 FINISH_PTR_LIST(arg1);
798 break;
800 case SYM_BASETYPE:
801 if (as1 != as2)
802 return "different address spaces";
803 if (base1 != base2)
804 return "different base types";
805 if (t1->rank != t2->rank)
806 return "different type sizes";
807 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
808 if (!diff)
809 return NULL;
810 else if (diff & ~MOD_SIGNEDNESS)
811 return "different modifiers";
812 else
813 return "different signedness";
815 t1 = base1;
816 t2 = base2;
818 if (as1 != as2)
819 return "different address spaces";
820 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
821 return "different modifiers";
822 return NULL;
825 static void bad_null(struct expression *expr)
827 if (Wnon_pointer_null)
828 warning(expr->pos, "Using plain integer as NULL pointer");
831 static unsigned long target_qualifiers(struct symbol *type)
833 unsigned long mod = type->ctype.modifiers & MOD_IGN;
834 if (type->ctype.base_type && type->ctype.base_type->type == SYM_ARRAY)
835 mod = 0;
836 return mod;
839 static struct symbol *evaluate_ptr_sub(struct expression *expr)
841 const char *typediff;
842 struct symbol *ltype, *rtype;
843 struct expression *l = expr->left;
844 struct expression *r = expr->right;
845 struct symbol *lbase;
847 classify_type(degenerate(l), &ltype);
848 classify_type(degenerate(r), &rtype);
850 lbase = examine_pointer_target(ltype);
851 examine_pointer_target(rtype);
852 typediff = type_difference(&ltype->ctype, &rtype->ctype,
853 target_qualifiers(rtype),
854 target_qualifiers(ltype));
855 if (typediff)
856 expression_error(expr, "subtraction of different types can't work (%s)", typediff);
858 if (is_function(lbase)) {
859 expression_error(expr, "subtraction of functions? Share your drugs");
860 return NULL;
863 expr->ctype = ssize_t_ctype;
864 if (lbase->bit_size > bits_in_char) {
865 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
866 struct expression *div = expr;
867 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
868 unsigned long value = bits_to_bytes(lbase->bit_size);
870 val->ctype = size_t_ctype;
871 val->value = value;
873 if (value & (value-1)) {
874 if (Wptr_subtraction_blows) {
875 warning(expr->pos, "potentially expensive pointer subtraction");
876 info(expr->pos, " '%s' has a non-power-of-2 size: %lu", show_typename(lbase), value);
880 sub->op = '-';
881 sub->ctype = ssize_t_ctype;
882 sub->left = l;
883 sub->right = r;
885 div->op = '/';
886 div->left = sub;
887 div->right = val;
890 return ssize_t_ctype;
893 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
895 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
897 struct symbol *ctype;
899 if (!expr)
900 return NULL;
902 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
903 warning(expr->pos, "assignment expression in conditional");
905 ctype = evaluate_expression(expr);
906 if (!valid_type(ctype))
907 return NULL;
908 if (is_safe_type(ctype))
909 warning(expr->pos, "testing a 'safe expression'");
910 if (is_func_type(ctype)) {
911 if (Waddress)
912 warning(expr->pos, "the address of %s will always evaluate as true", "a function");
913 } else if (is_array_type(ctype)) {
914 if (Waddress)
915 warning(expr->pos, "the address of %s will always evaluate as true", "an array");
916 } else if (!is_scalar_type(ctype)) {
917 sparse_error(expr->pos, "non-scalar type in conditional:");
918 info(expr->pos, " %s", show_typename(ctype));
919 return NULL;
922 ctype = degenerate(expr);
923 return ctype;
926 static struct symbol *evaluate_logical(struct expression *expr)
928 if (!evaluate_conditional(expr->left, 0))
929 return NULL;
930 if (!evaluate_conditional(expr->right, 0))
931 return NULL;
933 /* the result is int [6.5.13(3), 6.5.14(3)] */
934 expr->ctype = &int_ctype;
935 expr->flags = expr->left->flags & expr->right->flags;
936 expr->flags &= ~(CEF_CONST_MASK | CEF_ADDR);
937 return &int_ctype;
940 static struct symbol *evaluate_binop(struct expression *expr)
942 struct symbol *ltype, *rtype, *ctype;
943 int lclass = classify_type(expr->left->ctype, &ltype);
944 int rclass = classify_type(expr->right->ctype, &rtype);
945 int op = expr->op;
947 /* number op number */
948 if (lclass & rclass & TYPE_NUM) {
949 expr->flags = expr->left->flags & expr->right->flags;
950 expr->flags &= ~CEF_CONST_MASK;
952 if ((lclass | rclass) & TYPE_FLOAT) {
953 switch (op) {
954 case '+': case '-': case '*': case '/':
955 break;
956 default:
957 return bad_expr_type(expr);
961 if (op == SPECIAL_LEFTSHIFT || op == SPECIAL_RIGHTSHIFT) {
962 // shifts do integer promotions, but that's it.
963 unrestrict(expr->left, lclass, &ltype);
964 unrestrict(expr->right, rclass, &rtype);
965 ctype = ltype = integer_promotion(ltype);
966 rtype = integer_promotion(rtype);
967 } else {
968 // The rest do usual conversions
969 const unsigned left_not = expr->left->type == EXPR_PREOP
970 && expr->left->op == '!';
971 const unsigned right_not = expr->right->type == EXPR_PREOP
972 && expr->right->op == '!';
973 if ((op == '&' || op == '|') && (left_not || right_not))
974 warning(expr->pos, "dubious: %sx %c %sy",
975 left_not ? "!" : "",
977 right_not ? "!" : "");
979 ltype = usual_conversions(op, expr->left, expr->right,
980 lclass, rclass, ltype, rtype);
981 ctype = rtype = ltype;
984 expr->left = cast_to(expr->left, ltype);
985 expr->right = cast_to(expr->right, rtype);
986 expr->ctype = ctype;
987 return ctype;
990 /* pointer (+|-) integer */
991 if (lclass & TYPE_PTR && is_int(rclass) && (op == '+' || op == '-')) {
992 unrestrict(expr->right, rclass, &rtype);
993 return evaluate_ptr_add(expr, rtype);
996 /* integer + pointer */
997 if (rclass & TYPE_PTR && is_int(lclass) && op == '+') {
998 struct expression *index = expr->left;
999 unrestrict(index, lclass, &ltype);
1000 expr->left = expr->right;
1001 expr->right = index;
1002 return evaluate_ptr_add(expr, ltype);
1005 /* pointer - pointer */
1006 if (lclass & rclass & TYPE_PTR && expr->op == '-')
1007 return evaluate_ptr_sub(expr);
1009 return bad_expr_type(expr);
1012 static struct symbol *evaluate_comma(struct expression *expr)
1014 expr->ctype = degenerate(expr->right);
1015 if (expr->ctype == &null_ctype)
1016 expr->ctype = &ptr_ctype;
1017 expr->flags &= expr->left->flags & expr->right->flags;
1018 return expr->ctype;
1021 static int modify_for_unsigned(int op)
1023 if (op == '<')
1024 op = SPECIAL_UNSIGNED_LT;
1025 else if (op == '>')
1026 op = SPECIAL_UNSIGNED_GT;
1027 else if (op == SPECIAL_LTE)
1028 op = SPECIAL_UNSIGNED_LTE;
1029 else if (op == SPECIAL_GTE)
1030 op = SPECIAL_UNSIGNED_GTE;
1031 return op;
1034 enum null_constant_type {
1035 NON_NULL,
1036 NULL_PTR,
1037 NULL_ZERO,
1040 static inline int is_null_pointer_constant(struct expression *e)
1042 if (e->ctype == &null_ctype)
1043 return NULL_PTR;
1044 if (!(e->flags & CEF_ICE))
1045 return NON_NULL;
1046 return is_zero_constant(e) ? NULL_ZERO : NON_NULL;
1049 static struct symbol *evaluate_compare(struct expression *expr)
1051 struct expression *left = expr->left, *right = expr->right;
1052 struct symbol *ltype, *rtype, *lbase, *rbase;
1053 int lclass = classify_type(degenerate(left), &ltype);
1054 int rclass = classify_type(degenerate(right), &rtype);
1055 struct symbol *ctype;
1056 const char *typediff;
1058 /* Type types? */
1059 if (is_type_type(ltype) && is_type_type(rtype)) {
1061 * __builtin_types_compatible_p() yields an integer
1062 * constant expression
1064 expr->flags = CEF_SET_ICE;
1065 goto OK;
1068 if (is_safe_type(left->ctype) || is_safe_type(right->ctype))
1069 warning(expr->pos, "testing a 'safe expression'");
1071 expr->flags = left->flags & right->flags & ~CEF_CONST_MASK & ~CEF_ADDR;
1073 /* number on number */
1074 if (lclass & rclass & TYPE_NUM) {
1075 ctype = usual_conversions(expr->op, expr->left, expr->right,
1076 lclass, rclass, ltype, rtype);
1077 expr->left = cast_to(expr->left, ctype);
1078 expr->right = cast_to(expr->right, ctype);
1079 if (ctype->ctype.modifiers & MOD_UNSIGNED)
1080 expr->op = modify_for_unsigned(expr->op);
1081 goto OK;
1084 /* at least one must be a pointer */
1085 if (!((lclass | rclass) & TYPE_PTR))
1086 return bad_expr_type(expr);
1088 /* equality comparisons can be with null pointer constants */
1089 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1090 int is_null1 = is_null_pointer_constant(left);
1091 int is_null2 = is_null_pointer_constant(right);
1092 if (is_null1 == NULL_ZERO)
1093 bad_null(left);
1094 if (is_null2 == NULL_ZERO)
1095 bad_null(right);
1096 if (is_null1 && is_null2) {
1097 int positive = expr->op == SPECIAL_EQUAL;
1098 expr->type = EXPR_VALUE;
1099 expr->value = positive;
1100 goto OK;
1102 if (is_null1 && (rclass & TYPE_PTR)) {
1103 expr->left = cast_to(left, rtype);
1104 goto OK;
1106 if (is_null2 && (lclass & TYPE_PTR)) {
1107 expr->right = cast_to(right, ltype);
1108 goto OK;
1111 /* both should be pointers */
1112 if (!(lclass & rclass & TYPE_PTR))
1113 return bad_expr_type(expr);
1114 expr->op = modify_for_unsigned(expr->op);
1116 lbase = examine_pointer_target(ltype);
1117 rbase = examine_pointer_target(rtype);
1119 /* they also have special treatment for pointers to void */
1120 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1121 if (ltype->ctype.as == rtype->ctype.as) {
1122 if (lbase == &void_ctype) {
1123 expr->right = cast_to(right, ltype);
1124 goto OK;
1126 if (rbase == &void_ctype) {
1127 expr->left = cast_to(left, rtype);
1128 goto OK;
1133 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1134 target_qualifiers(rtype),
1135 target_qualifiers(ltype));
1136 if (!typediff)
1137 goto OK;
1139 expression_error(expr, "incompatible types in comparison expression (%s):", typediff);
1140 info(expr->pos, " %s", show_typename(ltype));
1141 info(expr->pos, " %s", show_typename(rtype));
1142 return NULL;
1145 /* the result is int [6.5.8(6), 6.5.9(3)]*/
1146 expr->ctype = &int_ctype;
1147 return &int_ctype;
1151 * NOTE! The degenerate case of "x ? : y", where we don't
1152 * have a true case, this will possibly promote "x" to the
1153 * same type as "y", and thus _change_ the conditional
1154 * test in the expression. But since promotion is "safe"
1155 * for testing, that's OK.
1157 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1159 struct expression **cond;
1160 struct symbol *ctype, *ltype, *rtype, *lbase, *rbase;
1161 int lclass, rclass;
1162 const char * typediff;
1163 int qual;
1165 if (!evaluate_conditional(expr->conditional, 0))
1166 return NULL;
1167 if (!evaluate_expression(expr->cond_false))
1168 return NULL;
1170 ctype = degenerate(expr->conditional);
1171 rtype = degenerate(expr->cond_false);
1173 cond = &expr->conditional;
1174 ltype = ctype;
1175 if (expr->cond_true) {
1176 if (!evaluate_expression(expr->cond_true))
1177 return NULL;
1178 ltype = degenerate(expr->cond_true);
1179 cond = &expr->cond_true;
1182 expr->flags = (expr->conditional->flags & (*cond)->flags &
1183 expr->cond_false->flags & ~CEF_CONST_MASK);
1185 * In the standard, it is defined that an integer constant expression
1186 * shall only have operands that are themselves constant [6.6(6)].
1187 * While this definition is very clear for expressions that need all
1188 * their operands to be evaluated, for conditional expressions with a
1189 * constant condition things are much less obvious.
1190 * So, as an extension, do the same as GCC seems to do:
1191 * Consider a conditional expression with a constant condition
1192 * as having the same constantness as the argument corresponding
1193 * to the truth value (including in the case of address constants
1194 * which are defined more stricly [6.6(9)]).
1196 if (expr->conditional->flags & (CEF_ACE | CEF_ADDR)) {
1197 int is_true = expr_truth_value(expr->conditional);
1198 struct expression *arg = is_true ? *cond : expr->cond_false;
1199 expr->flags = arg->flags & ~CEF_CONST_MASK;
1202 lclass = classify_type(ltype, &ltype);
1203 rclass = classify_type(rtype, &rtype);
1204 if (lclass & rclass & TYPE_NUM) {
1205 ctype = usual_conversions('?', *cond, expr->cond_false,
1206 lclass, rclass, ltype, rtype);
1207 *cond = cast_to(*cond, ctype);
1208 expr->cond_false = cast_to(expr->cond_false, ctype);
1209 goto out;
1212 if ((lclass | rclass) & TYPE_PTR) {
1213 int is_null1 = is_null_pointer_constant(*cond);
1214 int is_null2 = is_null_pointer_constant(expr->cond_false);
1216 if (is_null1 && is_null2) {
1217 *cond = cast_to(*cond, &ptr_ctype);
1218 expr->cond_false = cast_to(expr->cond_false, &ptr_ctype);
1219 ctype = &ptr_ctype;
1220 goto out;
1222 if (is_null1 && (rclass & TYPE_PTR)) {
1223 if (is_null1 == NULL_ZERO)
1224 bad_null(*cond);
1225 *cond = cast_to(*cond, rtype);
1226 ctype = rtype;
1227 goto out;
1229 if (is_null2 && (lclass & TYPE_PTR)) {
1230 if (is_null2 == NULL_ZERO)
1231 bad_null(expr->cond_false);
1232 expr->cond_false = cast_to(expr->cond_false, ltype);
1233 ctype = ltype;
1234 goto out;
1236 if (!(lclass & rclass & TYPE_PTR)) {
1237 typediff = "different types";
1238 goto Err;
1240 /* OK, it's pointer on pointer */
1241 if (ltype->ctype.as != rtype->ctype.as) {
1242 typediff = "different address spaces";
1243 goto Err;
1246 /* need to be lazier here */
1247 lbase = examine_pointer_target(ltype);
1248 rbase = examine_pointer_target(rtype);
1249 qual = target_qualifiers(ltype) | target_qualifiers(rtype);
1251 if (lbase == &void_ctype) {
1252 /* XXX: pointers to function should warn here */
1253 ctype = ltype;
1254 goto Qual;
1257 if (rbase == &void_ctype) {
1258 /* XXX: pointers to function should warn here */
1259 ctype = rtype;
1260 goto Qual;
1262 /* XXX: that should be pointer to composite */
1263 ctype = ltype;
1264 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1265 qual, qual);
1266 if (!typediff)
1267 goto Qual;
1268 goto Err;
1271 /* void on void, struct on same struct, union on same union */
1272 if (ltype == rtype) {
1273 ctype = ltype;
1274 goto out;
1276 typediff = "different base types";
1278 Err:
1279 expression_error(expr, "incompatible types in conditional expression (%s):", typediff);
1280 info(expr->pos, " %s", show_typename(ltype));
1281 info(expr->pos, " %s", show_typename(rtype));
1283 * if the condition is constant, the type is in fact known
1284 * so use it, as gcc & clang do.
1286 switch (expr_truth_value(expr->conditional)) {
1287 case 1: expr->ctype = ltype;
1288 break;
1289 case 0: expr->ctype = rtype;
1290 break;
1291 default:
1292 break;
1294 return NULL;
1296 out:
1297 expr->ctype = ctype;
1298 return ctype;
1300 Qual:
1301 if (qual & ~ctype->ctype.modifiers) {
1302 struct symbol *sym = alloc_symbol(ctype->pos, SYM_PTR);
1303 *sym = *ctype;
1304 sym->ctype.modifiers |= qual;
1305 ctype = sym;
1307 *cond = cast_to(*cond, ctype);
1308 expr->cond_false = cast_to(expr->cond_false, ctype);
1309 goto out;
1312 /* FP assignments can not do modulo or bit operations */
1313 static int compatible_float_op(int op)
1315 return op == SPECIAL_ADD_ASSIGN ||
1316 op == SPECIAL_SUB_ASSIGN ||
1317 op == SPECIAL_MUL_ASSIGN ||
1318 op == SPECIAL_DIV_ASSIGN;
1321 static int evaluate_assign_op(struct expression *expr)
1323 struct symbol *target = expr->left->ctype;
1324 struct symbol *source = expr->right->ctype;
1325 struct symbol *t, *s;
1326 int tclass = classify_type(target, &t);
1327 int sclass = classify_type(source, &s);
1328 int op = expr->op;
1330 if (tclass & sclass & TYPE_NUM) {
1331 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1332 expression_error(expr, "invalid assignment");
1333 return 0;
1335 if (tclass & TYPE_RESTRICT) {
1336 if (!restricted_binop(op, t)) {
1337 warning(expr->pos, "bad assignment (%s) to %s",
1338 show_special(op), show_typename(t));
1339 expr->right = cast_to(expr->right, target);
1340 return 0;
1342 /* allowed assignments unfoul */
1343 if (sclass & TYPE_FOULED && unfoul(s) == t)
1344 goto Cast;
1345 if (!restricted_value(expr->right, t))
1346 return 1;
1347 } else if (op == SPECIAL_SHR_ASSIGN || op == SPECIAL_SHL_ASSIGN) {
1348 // shifts do integer promotions, but that's it.
1349 unrestrict(expr->left, tclass, &t);
1350 target = integer_promotion(t);
1352 unrestrict(expr->right, sclass, &s);
1353 source = integer_promotion(s);
1354 expr->right = cast_to(expr->right, source);
1356 // both gcc & clang seems to do this, so ...
1357 if (target->bit_size > source->bit_size)
1358 expr->right = cast_to(expr->right, &uint_ctype);
1360 goto Cast;
1361 } else if (!(sclass & TYPE_RESTRICT))
1362 goto usual;
1363 /* source and target would better be identical restricted */
1364 if (t == s)
1365 return 1;
1366 warning(expr->pos, "invalid assignment: %s", show_special(op));
1367 info(expr->pos, " left side has type %s", show_typename(t));
1368 info(expr->pos, " right side has type %s", show_typename(s));
1369 expr->right = cast_to(expr->right, target);
1370 return 0;
1372 if (tclass == TYPE_PTR && is_int(sclass)) {
1373 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1374 unrestrict(expr->right, sclass, &s);
1375 evaluate_ptr_add(expr, s);
1376 return 1;
1378 expression_error(expr, "invalid pointer assignment");
1379 return 0;
1382 expression_error(expr, "invalid assignment");
1383 return 0;
1385 usual:
1386 target = usual_conversions(op, expr->left, expr->right,
1387 tclass, sclass, target, source);
1388 Cast:
1389 expr->right = cast_to(expr->right, target);
1390 return 1;
1393 static int whitelist_pointers(struct symbol *t1, struct symbol *t2)
1395 if (t1 == t2)
1396 return 0; /* yes, 0 - we don't want a cast_to here */
1397 if (t1 == &void_ctype)
1398 return 1;
1399 if (t2 == &void_ctype)
1400 return 1;
1401 if (classify_type(t1, &t1) != TYPE_NUM)
1402 return 0;
1403 if (classify_type(t2, &t2) != TYPE_NUM)
1404 return 0;
1405 if (t1 == t2)
1406 return 1;
1407 if (t1->rank == -2 && t2->rank == -2)
1408 return 1;
1409 if (t1->rank != t2->rank)
1410 return 0;
1411 return !Wtypesign;
1414 static int check_assignment_types(struct symbol *target, struct expression **rp,
1415 const char **typediff)
1417 struct symbol *source = degenerate(*rp);
1418 struct symbol *t, *s;
1419 int tclass = classify_type(target, &t);
1420 int sclass = classify_type(source, &s);
1422 if (tclass & sclass & TYPE_NUM) {
1423 if (tclass & TYPE_RESTRICT) {
1424 /* allowed assignments unfoul */
1425 if (sclass & TYPE_FOULED && unfoul(s) == t)
1426 goto Cast;
1427 if (!restricted_value(*rp, target))
1428 return 1;
1429 if (s == t)
1430 return 1;
1431 } else if (!(sclass & TYPE_RESTRICT))
1432 goto Cast;
1433 if (t == &bool_ctype) {
1434 if (is_fouled_type(s))
1435 warning((*rp)->pos, "%s degrades to integer",
1436 show_typename(s->ctype.base_type));
1437 goto Cast;
1439 *typediff = "different base types";
1440 return 0;
1443 if (tclass == TYPE_PTR) {
1444 unsigned long mod1, mod2;
1445 unsigned long modl, modr;
1446 struct symbol *b1, *b2;
1447 // NULL pointer is always OK
1448 int is_null = is_null_pointer_constant(*rp);
1449 if (is_null) {
1450 if (is_null == NULL_ZERO)
1451 bad_null(*rp);
1452 goto Cast;
1454 if (!(sclass & TYPE_PTR)) {
1455 *typediff = "different base types";
1456 return 0;
1458 b1 = examine_pointer_target(t);
1459 b2 = examine_pointer_target(s);
1460 mod1 = t->ctype.modifiers & MOD_IGN;
1461 mod2 = s->ctype.modifiers & MOD_IGN;
1462 if (whitelist_pointers(b1, b2)) {
1464 * assignments to/from void * are OK, provided that
1465 * we do not remove qualifiers from pointed to [C]
1466 * or mix address spaces [sparse].
1468 if (t->ctype.as != s->ctype.as) {
1469 *typediff = "different address spaces";
1470 return 0;
1473 * If this is a function pointer assignment, it is
1474 * actually fine to assign a pointer to const data to
1475 * it, as a function pointer points to const data
1476 * implicitly, i.e., dereferencing it does not produce
1477 * an lvalue.
1479 if (b1->type == SYM_FN)
1480 mod1 |= MOD_CONST;
1481 if (mod2 & ~mod1 & ~MOD_FUN_ATTR) {
1482 *typediff = "different modifiers";
1483 return 0;
1485 goto Cast;
1487 /* It's OK if the target is more volatile or const than the source */
1488 /* It's OK if the source is more pure/noreturn than the target */
1489 modr = mod1 & ~MOD_REV_QUAL;
1490 modl = mod2 & MOD_REV_QUAL;
1491 *typediff = type_difference(&t->ctype, &s->ctype, modl, modr);
1492 if (*typediff)
1493 return 0;
1494 return 1;
1497 if ((tclass & TYPE_COMPOUND) && s == t)
1498 return 1;
1500 if (tclass & TYPE_NUM) {
1501 /* XXX: need to turn into comparison with NULL */
1502 if (t == &bool_ctype && (sclass & TYPE_PTR))
1503 goto Cast;
1504 *typediff = "different base types";
1505 return 0;
1507 *typediff = "invalid types";
1508 return 0;
1510 Cast:
1511 *rp = cast_to(*rp, target);
1512 return 1;
1515 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1516 struct expression **rp, const char *where)
1518 const char *typediff;
1520 if (!check_assignment_types(target, rp, &typediff)) {
1521 struct symbol *source = *rp ? (*rp)->ctype : NULL;
1522 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1523 info(expr->pos, " expected %s", show_typename(target));
1524 info(expr->pos, " got %s", show_typename(source));
1525 *rp = cast_to(*rp, target);
1526 return 0;
1529 return 1;
1532 static int compatible_transparent_union(struct symbol *target,
1533 struct expression **rp)
1535 struct symbol *t, *member;
1536 classify_type(target, &t);
1537 if (t->type != SYM_UNION || !t->transparent_union)
1538 return 0;
1540 FOR_EACH_PTR(t->symbol_list, member) {
1541 const char *typediff;
1542 if (check_assignment_types(member, rp, &typediff))
1543 return 1;
1544 } END_FOR_EACH_PTR(member);
1546 return 0;
1549 static int compatible_argument_type(struct expression *expr, struct symbol *target,
1550 struct expression **rp, const char *where)
1552 if (compatible_transparent_union(target, rp))
1553 return 1;
1555 return compatible_assignment_types(expr, target, rp, where);
1558 static void mark_addressable(struct expression *expr)
1560 while (expr->type == EXPR_BINOP && expr->op == '+')
1561 expr = expr->left;
1562 if (expr->type == EXPR_SYMBOL) {
1563 struct symbol *sym = expr->symbol;
1564 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1568 static void mark_assigned(struct expression *expr)
1570 struct symbol *sym;
1572 if (!expr)
1573 return;
1574 switch (expr->type) {
1575 case EXPR_SYMBOL:
1576 sym = expr->symbol;
1577 if (!sym)
1578 return;
1579 if (sym->type != SYM_NODE)
1580 return;
1581 sym->ctype.modifiers |= MOD_ASSIGNED;
1582 return;
1584 case EXPR_BINOP:
1585 mark_assigned(expr->left);
1586 mark_assigned(expr->right);
1587 return;
1588 case EXPR_CAST:
1589 case EXPR_FORCE_CAST:
1590 mark_assigned(expr->cast_expression);
1591 return;
1592 case EXPR_SLICE:
1593 mark_assigned(expr->base);
1594 return;
1595 default:
1596 /* Hmm? */
1597 return;
1601 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1603 if (type->ctype.modifiers & MOD_CONST)
1604 expression_error(left, "assignment to const expression");
1606 /* We know left is an lvalue, so it's a "preop-*" */
1607 mark_assigned(left->unop);
1610 static struct symbol *evaluate_assignment(struct expression *expr)
1612 struct expression *left = expr->left;
1613 struct symbol *ltype;
1615 if (!lvalue_expression(left)) {
1616 expression_error(expr, "not an lvalue");
1617 return NULL;
1620 ltype = left->ctype;
1622 if (expr->op != '=') {
1623 if (!evaluate_assign_op(expr))
1624 return NULL;
1625 } else {
1626 if (!compatible_assignment_types(expr, ltype, &expr->right, "assignment"))
1627 return NULL;
1630 evaluate_assign_to(left, ltype);
1632 expr->ctype = ltype;
1633 return ltype;
1636 static void examine_fn_arguments(struct symbol *fn)
1638 struct symbol *s;
1640 FOR_EACH_PTR(fn->arguments, s) {
1641 struct symbol *arg = evaluate_symbol(s);
1642 /* Array/function arguments silently degenerate into pointers */
1643 if (arg) {
1644 struct symbol *ptr;
1645 switch(arg->type) {
1646 case SYM_ARRAY:
1647 case SYM_FN:
1648 ptr = alloc_symbol(s->pos, SYM_PTR);
1649 if (arg->type == SYM_ARRAY)
1650 ptr->ctype = arg->ctype;
1651 else
1652 ptr->ctype.base_type = arg;
1653 combine_address_space(s->pos, &ptr->ctype.as, s->ctype.as);
1654 ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1656 s->ctype.base_type = ptr;
1657 s->ctype.as = NULL;
1658 s->ctype.modifiers &= ~MOD_PTRINHERIT;
1659 s->bit_size = 0;
1660 s->examined = 0;
1661 examine_symbol_type(s);
1662 break;
1663 default:
1664 /* nothing */
1665 break;
1668 } END_FOR_EACH_PTR(s);
1671 static struct symbol *convert_to_as_mod(struct symbol *sym, struct ident *as, int mod)
1673 /* Take the modifiers of the pointer, and apply them to the member */
1674 mod |= sym->ctype.modifiers;
1675 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1676 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1677 *newsym = *sym;
1678 newsym->ctype.as = as;
1679 newsym->ctype.modifiers = mod;
1680 sym = newsym;
1682 return sym;
1685 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1687 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1688 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1690 node->ctype.base_type = ptr;
1691 ptr->bit_size = bits_in_pointer;
1692 ptr->ctype.alignment = pointer_alignment;
1694 node->bit_size = bits_in_pointer;
1695 node->ctype.alignment = pointer_alignment;
1697 access_symbol(sym);
1698 if (sym->ctype.modifiers & MOD_REGISTER) {
1699 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1700 sym->ctype.modifiers &= ~MOD_REGISTER;
1702 if (sym->type == SYM_NODE) {
1703 combine_address_space(sym->pos, &ptr->ctype.as, sym->ctype.as);
1704 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1705 sym = sym->ctype.base_type;
1707 if (degenerate && sym->type == SYM_ARRAY) {
1708 combine_address_space(sym->pos, &ptr->ctype.as, sym->ctype.as);
1709 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1710 sym = sym->ctype.base_type;
1712 ptr->ctype.base_type = sym;
1714 return node;
1717 /* Arrays degenerate into pointers on pointer arithmetic */
1718 static struct symbol *degenerate(struct expression *expr)
1720 struct symbol *ctype, *base;
1722 if (!expr)
1723 return NULL;
1724 ctype = expr->ctype;
1725 if (!ctype)
1726 return NULL;
1727 base = examine_symbol_type(ctype);
1728 if (ctype->type == SYM_NODE)
1729 base = ctype->ctype.base_type;
1731 * Arrays degenerate into pointers to the entries, while
1732 * functions degenerate into pointers to themselves.
1733 * If array was part of non-lvalue compound, we create a copy
1734 * of that compound first and then act as if we were dealing with
1735 * the corresponding field in there.
1737 switch (base->type) {
1738 case SYM_ARRAY:
1739 if (expr->type == EXPR_SLICE) {
1740 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1741 struct expression *e0, *e1, *e2, *e3, *e4;
1743 a->ctype.base_type = expr->base->ctype;
1744 a->bit_size = expr->base->ctype->bit_size;
1745 a->array_size = expr->base->ctype->array_size;
1747 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1748 e0->symbol = a;
1749 e0->ctype = &lazy_ptr_ctype;
1751 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1752 e1->unop = e0;
1753 e1->op = '*';
1754 e1->ctype = expr->base->ctype; /* XXX */
1756 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1757 e2->left = e1;
1758 e2->right = expr->base;
1759 e2->op = '=';
1760 e2->ctype = expr->base->ctype;
1762 if (expr->r_bitpos) {
1763 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1764 e3->op = '+';
1765 e3->left = e0;
1766 e3->right = alloc_const_expression(expr->pos,
1767 bits_to_bytes(expr->r_bitpos));
1768 e3->ctype = &lazy_ptr_ctype;
1769 } else {
1770 e3 = e0;
1773 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1774 e4->left = e2;
1775 e4->right = e3;
1776 e4->ctype = &lazy_ptr_ctype;
1778 expr->unop = e4;
1779 expr->type = EXPR_PREOP;
1780 expr->op = '*';
1782 case SYM_FN:
1783 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1784 expression_error(expr, "strange non-value function or array");
1785 return &bad_ctype;
1787 *expr = *expr->unop;
1788 ctype = create_pointer(expr, ctype, 1);
1789 expr->ctype = ctype;
1790 mark_addressable(expr);
1791 default:
1792 /* nothing */;
1794 return ctype;
1797 static struct symbol *evaluate_addressof(struct expression *expr)
1799 struct expression *op = expr->unop;
1800 struct symbol *ctype;
1802 if (op->op != '*' || op->type != EXPR_PREOP) {
1803 expression_error(expr, "not addressable");
1804 return NULL;
1806 ctype = op->ctype;
1807 *expr = *op->unop;
1809 mark_addressable(expr);
1812 * symbol expression evaluation is lazy about the type
1813 * of the sub-expression, so we may have to generate
1814 * the type here if so..
1816 if (expr->ctype == &lazy_ptr_ctype) {
1817 ctype = create_pointer(expr, ctype, 0);
1818 expr->ctype = ctype;
1820 return expr->ctype;
1824 static struct symbol *evaluate_dereference(struct expression *expr)
1826 struct expression *op = expr->unop;
1827 struct symbol *ctype = op->ctype, *node, *target;
1829 /* Simplify: *&(expr) => (expr) */
1830 if (op->type == EXPR_PREOP && op->op == '&') {
1831 *expr = *op->unop;
1832 expr->flags = CEF_NONE;
1833 return expr->ctype;
1836 examine_symbol_type(ctype);
1838 /* Dereferencing a node drops all the node information. */
1839 if (ctype->type == SYM_NODE)
1840 ctype = ctype->ctype.base_type;
1842 target = ctype->ctype.base_type;
1844 switch (ctype->type) {
1845 default:
1846 expression_error(expr, "cannot dereference this type");
1847 return NULL;
1848 case SYM_FN:
1849 *expr = *op;
1850 return expr->ctype;
1851 case SYM_PTR:
1852 examine_symbol_type(target);
1853 node = alloc_symbol(expr->pos, SYM_NODE);
1854 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1855 merge_type(node, ctype);
1856 break;
1858 case SYM_ARRAY:
1859 if (!lvalue_expression(op)) {
1860 expression_error(op, "non-lvalue array??");
1861 return NULL;
1864 /* Do the implied "addressof" on the array */
1865 *op = *op->unop;
1868 * When an array is dereferenced, we need to pick
1869 * up the attributes of the original node too..
1871 node = alloc_symbol(expr->pos, SYM_NODE);
1872 merge_type(node, op->ctype);
1873 merge_type(node, ctype);
1874 break;
1877 node->bit_size = target->bit_size;
1878 node->array_size = target->array_size;
1880 expr->ctype = node;
1881 return node;
1885 * Unary post-ops: x++ and x--
1887 static struct symbol *evaluate_postop(struct expression *expr)
1889 struct expression *op = expr->unop;
1890 struct symbol *ctype = op->ctype;
1891 int class = classify_type(ctype, &ctype);
1892 int multiply = 0;
1894 if (!class || class & TYPE_COMPOUND) {
1895 expression_error(expr, "need scalar for ++/--");
1896 return NULL;
1898 if (!lvalue_expression(expr->unop)) {
1899 expression_error(expr, "need lvalue expression for ++/--");
1900 return NULL;
1903 if ((class & TYPE_RESTRICT) && restricted_unop(expr->op, &ctype))
1904 unrestrict(expr, class, &ctype);
1906 if (class & TYPE_NUM) {
1907 multiply = 1;
1908 } else if (class == TYPE_PTR) {
1909 struct symbol *target = examine_pointer_target(ctype);
1910 if (!is_function(target))
1911 multiply = bits_to_bytes(target->bit_size);
1914 if (multiply) {
1915 evaluate_assign_to(op, op->ctype);
1916 expr->op_value = multiply;
1917 expr->ctype = ctype;
1918 return ctype;
1921 expression_error(expr, "bad argument type for ++/--");
1922 return NULL;
1925 static struct symbol *evaluate_sign(struct expression *expr)
1927 struct symbol *ctype = expr->unop->ctype;
1928 int class = classify_type(ctype, &ctype);
1929 unsigned char flags = expr->unop->flags & ~CEF_CONST_MASK;
1931 /* should be an arithmetic type */
1932 if (!(class & TYPE_NUM))
1933 return bad_expr_type(expr);
1934 if (class & TYPE_RESTRICT)
1935 goto Restr;
1936 Normal:
1937 if (!(class & TYPE_FLOAT)) {
1938 ctype = integer_promotion(ctype);
1939 expr->unop = cast_to(expr->unop, ctype);
1940 } else if (expr->op != '~') {
1941 /* no conversions needed */
1942 } else {
1943 return bad_expr_type(expr);
1945 if (expr->op == '+')
1946 *expr = *expr->unop;
1947 expr->flags = flags;
1948 expr->ctype = ctype;
1949 return ctype;
1950 Restr:
1951 if (restricted_unop(expr->op, &ctype))
1952 unrestrict(expr, class, &ctype);
1953 goto Normal;
1956 static struct symbol *evaluate_preop(struct expression *expr)
1958 struct symbol *ctype = expr->unop->ctype;
1960 switch (expr->op) {
1961 case '(':
1962 *expr = *expr->unop;
1963 return ctype;
1965 case '+':
1966 case '-':
1967 case '~':
1968 return evaluate_sign(expr);
1970 case '*':
1971 return evaluate_dereference(expr);
1973 case '&':
1974 return evaluate_addressof(expr);
1976 case SPECIAL_INCREMENT:
1977 case SPECIAL_DECREMENT:
1979 * From a type evaluation standpoint the preops are
1980 * the same as the postops
1982 return evaluate_postop(expr);
1984 case '!':
1985 ctype = degenerate(expr->unop);
1986 expr->flags = expr->unop->flags & ~CEF_CONST_MASK;
1988 * A logical negation never yields an address constant
1989 * [6.6(9)].
1991 expr->flags &= ~CEF_ADDR;
1993 if (is_safe_type(ctype))
1994 warning(expr->pos, "testing a 'safe expression'");
1995 if (is_float_type(ctype)) {
1996 struct expression *arg = expr->unop;
1997 expr->type = EXPR_COMPARE;
1998 expr->op = SPECIAL_EQUAL;
1999 expr->left = arg;
2000 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
2001 expr->right->ctype = ctype;
2002 expr->right->fvalue = 0;
2003 } else if (is_fouled_type(ctype)) {
2004 warning(expr->pos, "%s degrades to integer",
2005 show_typename(ctype->ctype.base_type));
2007 /* the result is int [6.5.3.3(5)]*/
2008 ctype = &int_ctype;
2009 break;
2011 default:
2012 break;
2014 expr->ctype = ctype;
2015 return ctype;
2018 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
2020 struct ptr_list *head = (struct ptr_list *)_list;
2021 struct ptr_list *list = head;
2023 if (!head)
2024 return NULL;
2025 do {
2026 int i;
2027 for (i = 0; i < list->nr; i++) {
2028 struct symbol *sym = (struct symbol *) list->list[i];
2029 if (sym->ident) {
2030 if (sym->ident != ident)
2031 continue;
2032 *offset = sym->offset;
2033 return sym;
2034 } else {
2035 struct symbol *ctype = sym->ctype.base_type;
2036 struct symbol *sub;
2037 if (!ctype)
2038 continue;
2039 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
2040 continue;
2041 sub = find_identifier(ident, ctype->symbol_list, offset);
2042 if (!sub)
2043 continue;
2044 *offset += sym->offset;
2045 return sub;
2048 } while ((list = list->next) != head);
2049 return NULL;
2052 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
2054 struct expression *add;
2057 * Create a new add-expression
2059 * NOTE! Even if we just add zero, we need a new node
2060 * for the member pointer, since it has a different
2061 * type than the original pointer. We could make that
2062 * be just a cast, but the fact is, a node is a node,
2063 * so we might as well just do the "add zero" here.
2065 add = alloc_expression(expr->pos, EXPR_BINOP);
2066 add->op = '+';
2067 add->left = expr;
2068 add->right = alloc_expression(expr->pos, EXPR_VALUE);
2069 add->right->ctype = &int_ctype;
2070 add->right->value = offset;
2073 * The ctype of the pointer will be lazily evaluated if
2074 * we ever take the address of this member dereference..
2076 add->ctype = &lazy_ptr_ctype;
2078 * The resulting address of a member access through an address
2079 * constant is an address constant again [6.6(9)].
2081 add->flags = expr->flags;
2083 return add;
2086 /* structure/union dereference */
2087 static struct symbol *evaluate_member_dereference(struct expression *expr)
2089 int offset;
2090 struct symbol *ctype, *member;
2091 struct expression *deref = expr->deref, *add;
2092 struct ident *ident = expr->member;
2093 struct ident *address_space;
2094 unsigned int mod;
2096 if (!evaluate_expression(deref))
2097 return NULL;
2098 if (!ident) {
2099 expression_error(expr, "bad member name");
2100 return NULL;
2103 ctype = deref->ctype;
2104 examine_symbol_type(ctype);
2105 address_space = ctype->ctype.as;
2106 mod = ctype->ctype.modifiers;
2107 if (ctype->type == SYM_NODE) {
2108 ctype = ctype->ctype.base_type;
2109 combine_address_space(deref->pos, &address_space, ctype->ctype.as);
2110 mod |= ctype->ctype.modifiers;
2112 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
2113 expression_error(expr, "expected structure or union");
2114 return NULL;
2116 offset = 0;
2117 member = find_identifier(ident, ctype->symbol_list, &offset);
2118 if (!member) {
2119 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
2120 const char *name = "<unnamed>";
2121 int namelen = 9;
2122 if (ctype->ident) {
2123 name = ctype->ident->name;
2124 namelen = ctype->ident->len;
2126 if (ctype->symbol_list)
2127 expression_error(expr, "no member '%s' in %s %.*s",
2128 show_ident(ident), type, namelen, name);
2129 else
2130 expression_error(expr, "using member '%s' in "
2131 "incomplete %s %.*s", show_ident(ident),
2132 type, namelen, name);
2133 return NULL;
2137 * The member needs to take on the address space and modifiers of
2138 * the "parent" type.
2140 member = convert_to_as_mod(member, address_space, mod);
2141 ctype = get_base_type(member);
2143 if (!lvalue_expression(deref)) {
2144 if (deref->type != EXPR_SLICE) {
2145 expr->base = deref;
2146 expr->r_bitpos = 0;
2147 } else {
2148 expr->base = deref->base;
2149 expr->r_bitpos = deref->r_bitpos;
2151 expr->r_bitpos += bytes_to_bits(offset);
2152 expr->type = EXPR_SLICE;
2153 expr->r_nrbits = member->bit_size;
2154 expr->r_bitpos += member->bit_offset;
2155 expr->ctype = member;
2156 return member;
2159 deref = deref->unop;
2160 expr->deref = deref;
2162 add = evaluate_offset(deref, offset);
2163 expr->type = EXPR_PREOP;
2164 expr->op = '*';
2165 expr->unop = add;
2167 expr->ctype = member;
2168 return member;
2171 static int is_promoted(struct expression *expr)
2173 while (1) {
2174 switch (expr->type) {
2175 case EXPR_BINOP:
2176 case EXPR_SELECT:
2177 case EXPR_CONDITIONAL:
2178 return 1;
2179 case EXPR_COMMA:
2180 expr = expr->right;
2181 continue;
2182 case EXPR_PREOP:
2183 switch (expr->op) {
2184 case '(':
2185 expr = expr->unop;
2186 continue;
2187 case '+':
2188 case '-':
2189 case '~':
2190 return 1;
2191 default:
2192 return 0;
2194 default:
2195 return 0;
2201 static struct symbol *evaluate_type_information(struct expression *expr)
2203 struct symbol *sym = expr->cast_type;
2204 if (!sym) {
2205 sym = evaluate_expression(expr->cast_expression);
2206 if (!sym)
2207 return NULL;
2209 * Expressions of restricted types will possibly get
2210 * promoted - check that here
2212 if (is_restricted_type(sym)) {
2213 if (sym->bit_size < bits_in_int && is_promoted(expr))
2214 sym = &int_ctype;
2215 } else if (is_fouled_type(sym)) {
2216 sym = &int_ctype;
2219 examine_symbol_type(sym);
2220 if (is_bitfield_type(sym)) {
2221 expression_error(expr, "trying to examine bitfield type");
2222 return NULL;
2224 return sym;
2227 static struct symbol *evaluate_sizeof(struct expression *expr)
2229 struct symbol *type;
2230 int size;
2232 type = evaluate_type_information(expr);
2233 if (!type)
2234 return NULL;
2236 size = type->bit_size;
2238 if (size < 0 && is_void_type(type)) {
2239 if (Wpointer_arith)
2240 warning(expr->pos, "expression using sizeof(void)");
2241 size = bits_in_char;
2244 if (is_bool_type(type)) {
2245 if (Wsizeof_bool)
2246 warning(expr->pos, "expression using sizeof _Bool");
2247 size = bits_to_bytes(bits_in_bool) * bits_in_char;
2250 if (is_function(type->ctype.base_type)) {
2251 if (Wpointer_arith)
2252 warning(expr->pos, "expression using sizeof on a function");
2253 size = bits_in_char;
2256 if (is_array_type(type) && size < 0) { // VLA, 1-dimension only
2257 struct expression *base, *size;
2258 struct symbol *base_type;
2260 if (type->type == SYM_NODE)
2261 type = type->ctype.base_type; // strip the SYM_NODE
2262 base_type = get_base_type(type);
2263 if (!base_type)
2264 goto error;
2265 if (base_type->bit_size <= 0) {
2266 base = alloc_expression(expr->pos, EXPR_SIZEOF);
2267 base->cast_type = base_type;
2268 if (!evaluate_sizeof(base))
2269 goto error;
2270 } else {
2271 base = alloc_expression(expr->pos, EXPR_VALUE);
2272 base->value = bits_to_bytes(base_type->bit_size);
2273 base->ctype = size_t_ctype;
2275 size = alloc_expression(expr->pos, EXPR_CAST);
2276 size->cast_type = size_t_ctype;
2277 size->cast_expression = type->array_size;
2278 if (!evaluate_expression(size))
2279 goto error;
2280 expr->left = size;
2281 expr->right = base;
2282 expr->type = EXPR_BINOP;
2283 expr->op = '*';
2284 return expr->ctype = size_t_ctype;
2287 error:
2288 if ((size < 0) || (size & (bits_in_char - 1)))
2289 expression_error(expr, "cannot size expression");
2291 expr->type = EXPR_VALUE;
2292 expr->value = bits_to_bytes(size);
2293 expr->taint = 0;
2294 expr->ctype = size_t_ctype;
2295 return size_t_ctype;
2298 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
2300 struct symbol *type;
2301 int size;
2303 type = evaluate_type_information(expr);
2304 if (!type)
2305 return NULL;
2307 if (type->type == SYM_NODE)
2308 type = type->ctype.base_type;
2309 if (!type)
2310 return NULL;
2311 switch (type->type) {
2312 case SYM_ARRAY:
2313 break;
2314 case SYM_PTR:
2315 type = get_base_type(type);
2316 if (type)
2317 break;
2318 default:
2319 expression_error(expr, "expected pointer expression");
2320 return NULL;
2322 size = type->bit_size;
2323 if (size & (bits_in_char-1))
2324 size = 0;
2325 expr->type = EXPR_VALUE;
2326 expr->value = bits_to_bytes(size);
2327 expr->taint = 0;
2328 expr->ctype = size_t_ctype;
2329 return size_t_ctype;
2332 static struct symbol *evaluate_alignof(struct expression *expr)
2334 struct symbol *type;
2336 type = evaluate_type_information(expr);
2337 if (!type)
2338 return NULL;
2340 expr->type = EXPR_VALUE;
2341 expr->value = type->ctype.alignment;
2342 expr->taint = 0;
2343 expr->ctype = size_t_ctype;
2344 return size_t_ctype;
2347 int evaluate_arguments(struct symbol_list *argtypes, struct expression_list *head)
2349 struct expression *expr;
2350 struct symbol *argtype;
2351 int i = 1;
2353 PREPARE_PTR_LIST(argtypes, argtype);
2354 FOR_EACH_PTR (head, expr) {
2355 struct expression **p = THIS_ADDRESS(expr);
2356 struct symbol *ctype, *target;
2357 ctype = evaluate_expression(expr);
2359 if (!ctype)
2360 return 0;
2362 target = argtype;
2363 if (!target) {
2364 struct symbol *type;
2365 int class = classify_type(ctype, &type);
2366 if (is_int(class)) {
2367 *p = cast_to(expr, integer_promotion(type));
2368 } else if (class & TYPE_FLOAT) {
2369 if (type->rank < 0)
2370 *p = cast_to(expr, &double_ctype);
2371 } else if (class & TYPE_PTR) {
2372 if (expr->ctype == &null_ctype)
2373 *p = cast_to(expr, &ptr_ctype);
2374 else
2375 degenerate(expr);
2377 } else if (!target->forced_arg){
2378 static char where[30];
2379 examine_symbol_type(target);
2380 sprintf(where, "argument %d", i);
2381 compatible_argument_type(expr, target, p, where);
2384 i++;
2385 NEXT_PTR_LIST(argtype);
2386 } END_FOR_EACH_PTR(expr);
2387 FINISH_PTR_LIST(argtype);
2388 return 1;
2391 static void convert_index(struct expression *e)
2393 struct expression *child = e->idx_expression;
2394 unsigned from = e->idx_from;
2395 unsigned to = e->idx_to + 1;
2396 e->type = EXPR_POS;
2397 e->init_offset = from * bits_to_bytes(e->ctype->bit_size);
2398 e->init_nr = to - from;
2399 e->init_expr = child;
2402 static void convert_ident(struct expression *e)
2404 struct expression *child = e->ident_expression;
2405 int offset = e->offset;
2407 e->type = EXPR_POS;
2408 e->init_offset = offset;
2409 e->init_nr = 1;
2410 e->init_expr = child;
2413 static void convert_designators(struct expression *e)
2415 while (e) {
2416 if (e->type == EXPR_INDEX)
2417 convert_index(e);
2418 else if (e->type == EXPR_IDENTIFIER)
2419 convert_ident(e);
2420 else
2421 break;
2422 e = e->init_expr;
2426 static void excess(struct expression *e, const char *s)
2428 warning(e->pos, "excessive elements in %s initializer", s);
2432 * implicit designator for the first element
2434 static struct expression *first_subobject(struct symbol *ctype, int class,
2435 struct expression **v)
2437 struct expression *e = *v, *new;
2439 if (ctype->type == SYM_NODE)
2440 ctype = ctype->ctype.base_type;
2442 if (class & TYPE_PTR) { /* array */
2443 if (!ctype->bit_size)
2444 return NULL;
2445 new = alloc_expression(e->pos, EXPR_INDEX);
2446 new->idx_expression = e;
2447 new->ctype = ctype->ctype.base_type;
2448 } else {
2449 struct symbol *field, *p;
2450 PREPARE_PTR_LIST(ctype->symbol_list, p);
2451 while (p && !p->ident && is_bitfield_type(p))
2452 NEXT_PTR_LIST(p);
2453 field = p;
2454 FINISH_PTR_LIST(p);
2455 if (!field)
2456 return NULL;
2457 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2458 new->ident_expression = e;
2459 new->field = new->ctype = field;
2460 new->offset = field->offset;
2462 *v = new;
2463 return new;
2467 * sanity-check explicit designators; return the innermost one or NULL
2468 * in case of error. Assign types.
2470 static struct expression *check_designators(struct expression *e,
2471 struct symbol *ctype)
2473 struct expression *last = NULL;
2474 const char *err;
2475 while (1) {
2476 if (ctype->type == SYM_NODE)
2477 ctype = ctype->ctype.base_type;
2478 if (e->type == EXPR_INDEX) {
2479 struct symbol *type;
2480 if (ctype->type != SYM_ARRAY) {
2481 err = "array index in non-array";
2482 break;
2484 type = ctype->ctype.base_type;
2485 if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2486 unsigned offset = array_element_offset(type->bit_size, e->idx_to);
2487 if (offset >= ctype->bit_size) {
2488 err = "index out of bounds in";
2489 break;
2492 e->ctype = ctype = type;
2493 ctype = type;
2494 last = e;
2495 if (!e->idx_expression) {
2496 err = "invalid";
2497 break;
2499 e = e->idx_expression;
2500 } else if (e->type == EXPR_IDENTIFIER) {
2501 int offset = 0;
2502 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2503 err = "field name not in struct or union";
2504 break;
2506 ctype = find_identifier(e->expr_ident, ctype->symbol_list, &offset);
2507 if (!ctype) {
2508 err = "unknown field name in";
2509 break;
2511 e->offset = offset;
2512 e->field = e->ctype = ctype;
2513 last = e;
2514 if (!e->ident_expression) {
2515 err = "invalid";
2516 break;
2518 e = e->ident_expression;
2519 } else if (e->type == EXPR_POS) {
2520 err = "internal front-end error: EXPR_POS in";
2521 break;
2522 } else
2523 return last;
2525 expression_error(e, "%s initializer", err);
2526 return NULL;
2530 * choose the next subobject to initialize.
2532 * Get designators for next element, switch old ones to EXPR_POS.
2533 * Return the resulting expression or NULL if we'd run out of subobjects.
2534 * The innermost designator is returned in *v. Designators in old
2535 * are assumed to be already sanity-checked.
2537 static struct expression *next_designators(struct expression *old,
2538 struct symbol *ctype,
2539 struct expression *e, struct expression **v)
2541 struct expression *new = NULL;
2543 if (!old)
2544 return NULL;
2545 if (old->type == EXPR_INDEX) {
2546 struct expression *copy;
2547 unsigned n;
2549 copy = next_designators(old->idx_expression,
2550 old->ctype, e, v);
2551 if (!copy) {
2552 n = old->idx_to + 1;
2553 if (array_element_offset(old->ctype->bit_size, n) == ctype->bit_size) {
2554 convert_index(old);
2555 return NULL;
2557 copy = e;
2558 *v = new = alloc_expression(e->pos, EXPR_INDEX);
2559 } else {
2560 n = old->idx_to;
2561 new = alloc_expression(e->pos, EXPR_INDEX);
2564 new->idx_from = new->idx_to = n;
2565 new->idx_expression = copy;
2566 new->ctype = old->ctype;
2567 convert_index(old);
2568 } else if (old->type == EXPR_IDENTIFIER) {
2569 struct expression *copy;
2570 struct symbol *field;
2571 int offset = 0;
2573 copy = next_designators(old->ident_expression,
2574 old->ctype, e, v);
2575 if (!copy) {
2576 field = old->field->next_subobject;
2577 if (!field) {
2578 convert_ident(old);
2579 return NULL;
2581 copy = e;
2582 *v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2584 * We can't necessarily trust "field->offset",
2585 * because the field might be in an anonymous
2586 * union, and the field offset is then the offset
2587 * within that union.
2589 * The "old->offset - old->field->offset"
2590 * would be the offset of such an anonymous
2591 * union.
2593 offset = old->offset - old->field->offset;
2594 } else {
2595 field = old->field;
2596 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2599 new->field = field;
2600 new->expr_ident = field->ident;
2601 new->ident_expression = copy;
2602 new->ctype = field;
2603 new->offset = field->offset + offset;
2604 convert_ident(old);
2606 return new;
2609 static int handle_initializer(struct expression **ep, int nested,
2610 int class, struct symbol *ctype, unsigned long mods);
2613 * deal with traversing subobjects [6.7.8(17,18,20)]
2615 static void handle_list_initializer(struct expression *expr,
2616 int class, struct symbol *ctype, unsigned long mods)
2618 struct expression *e, *last = NULL, *top = NULL, *next;
2619 int jumped = 0; // has the last designator multiple levels?
2621 if (expr->zero_init)
2622 free_ptr_list(&expr->expr_list);
2624 FOR_EACH_PTR(expr->expr_list, e) {
2625 struct expression **v;
2626 struct symbol *type;
2627 int lclass;
2629 if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2630 struct symbol *struct_sym;
2631 if (!top) {
2632 top = e;
2633 last = first_subobject(ctype, class, &top);
2634 } else {
2635 last = next_designators(last, ctype, e, &top);
2637 if (!last) {
2638 excess(e, class & TYPE_PTR ? "array" :
2639 "struct or union");
2640 DELETE_CURRENT_PTR(e);
2641 continue;
2643 struct_sym = ctype->type == SYM_NODE ? ctype->ctype.base_type : ctype;
2644 if (Wdesignated_init && struct_sym->designated_init)
2645 warning(e->pos, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2646 ctype->ident ? "in initializer for " : "",
2647 ctype->ident ? ctype->ident->len : 0,
2648 ctype->ident ? ctype->ident->name : "",
2649 ctype->ident ? ": " : "",
2650 get_type_name(struct_sym->type),
2651 show_ident(struct_sym->ident));
2652 if (jumped && Wpast_deep_designator) {
2653 warning(e->pos, "advancing past deep designator");
2654 jumped = 0;
2656 REPLACE_CURRENT_PTR(e, last);
2657 } else {
2658 next = check_designators(e, ctype);
2659 if (!next) {
2660 DELETE_CURRENT_PTR(e);
2661 continue;
2663 top = next;
2664 /* deeper than one designator? */
2665 jumped = top != e;
2666 convert_designators(last);
2667 last = e;
2670 found:
2671 lclass = classify_type(top->ctype, &type);
2672 if (top->type == EXPR_INDEX)
2673 v = &top->idx_expression;
2674 else
2675 v = &top->ident_expression;
2677 mods |= ctype->ctype.modifiers & MOD_STORAGE;
2678 if (handle_initializer(v, 1, lclass, top->ctype, mods))
2679 continue;
2681 if (!(lclass & TYPE_COMPOUND)) {
2682 warning(e->pos, "bogus scalar initializer");
2683 DELETE_CURRENT_PTR(e);
2684 continue;
2687 next = first_subobject(type, lclass, v);
2688 if (next) {
2689 warning(e->pos, "missing braces around initializer");
2690 top = next;
2691 goto found;
2694 DELETE_CURRENT_PTR(e);
2695 excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2697 } END_FOR_EACH_PTR(e);
2699 convert_designators(last);
2700 expr->ctype = ctype;
2703 static int is_string_literal(struct expression **v)
2705 struct expression *e = *v;
2706 while (e && e->type == EXPR_PREOP && e->op == '(')
2707 e = e->unop;
2708 if (!e || e->type != EXPR_STRING)
2709 return 0;
2710 if (e != *v && Wparen_string)
2711 warning(e->pos,
2712 "array initialized from parenthesized string constant");
2713 *v = e;
2714 return 1;
2718 * We want a normal expression, possibly in one layer of braces. Warn
2719 * if the latter happens inside a list (it's legal, but likely to be
2720 * an effect of screwup). In case of anything not legal, we are definitely
2721 * having an effect of screwup, so just fail and let the caller warn.
2723 static struct expression *handle_scalar(struct expression *e, int nested)
2725 struct expression *v = NULL, *p;
2726 int count = 0;
2728 /* normal case */
2729 if (e->type != EXPR_INITIALIZER)
2730 return e;
2732 FOR_EACH_PTR(e->expr_list, p) {
2733 if (!v)
2734 v = p;
2735 count++;
2736 } END_FOR_EACH_PTR(p);
2737 if (count != 1)
2738 return NULL;
2739 switch(v->type) {
2740 case EXPR_INITIALIZER:
2741 case EXPR_INDEX:
2742 case EXPR_IDENTIFIER:
2743 return NULL;
2744 default:
2745 break;
2747 if (nested)
2748 warning(e->pos, "braces around scalar initializer");
2749 return v;
2753 * deal with the cases that don't care about subobjects:
2754 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2755 * character array <- string literal, possibly in braces [6.7.8(14)]
2756 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2757 * compound type <- initializer list in braces [6.7.8(16)]
2758 * The last one punts to handle_list_initializer() which, in turn will call
2759 * us for individual elements of the list.
2761 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2762 * the lack of support of wide char stuff in general.
2764 * One note: we need to take care not to evaluate a string literal until
2765 * we know that we *will* handle it right here. Otherwise we would screw
2766 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2767 * { "string", ...} - we need to preserve that string literal recognizable
2768 * until we dig into the inner struct.
2770 static int handle_initializer(struct expression **ep, int nested,
2771 int class, struct symbol *ctype, unsigned long mods)
2773 struct expression *e = *ep, *p;
2774 struct symbol *type;
2776 if (!e)
2777 return 0;
2779 /* scalar */
2780 if (!(class & TYPE_COMPOUND)) {
2781 e = handle_scalar(e, nested);
2782 if (!e)
2783 return 0;
2784 *ep = e;
2785 if (!evaluate_expression(e))
2786 return 1;
2787 compatible_assignment_types(e, ctype, ep, "initializer");
2789 * Initializers for static storage duration objects
2790 * shall be constant expressions or a string literal [6.7.8(4)].
2792 mods |= ctype->ctype.modifiers;
2793 mods &= (MOD_TOPLEVEL | MOD_STATIC);
2794 if (mods && !(e->flags & (CEF_ACE | CEF_ADDR)))
2795 if (Wconstexpr_not_const)
2796 warning(e->pos, "non-constant initializer for static object");
2798 return 1;
2802 * sublist; either a string, or we dig in; the latter will deal with
2803 * pathologies, so we don't need anything fancy here.
2805 if (e->type == EXPR_INITIALIZER) {
2806 if (is_string_type(ctype)) {
2807 struct expression *v = NULL;
2808 int count = 0;
2810 FOR_EACH_PTR(e->expr_list, p) {
2811 if (!v)
2812 v = p;
2813 count++;
2814 } END_FOR_EACH_PTR(p);
2815 if (count == 1 && is_string_literal(&v)) {
2816 *ep = e = v;
2817 goto String;
2820 handle_list_initializer(e, class, ctype, mods);
2821 return 1;
2824 /* string */
2825 if (is_string_literal(&e)) {
2826 /* either we are doing array of char, or we'll have to dig in */
2827 if (is_string_type(ctype)) {
2828 *ep = e;
2829 goto String;
2831 return 0;
2833 /* struct or union can be initialized by compatible */
2834 if (class != TYPE_COMPOUND)
2835 return 0;
2836 type = evaluate_expression(e);
2837 if (!type)
2838 return 0;
2839 if (ctype->type == SYM_NODE)
2840 ctype = ctype->ctype.base_type;
2841 if (type->type == SYM_NODE)
2842 type = type->ctype.base_type;
2843 if (ctype == type)
2844 return 1;
2845 return 0;
2847 String:
2848 p = alloc_expression(e->pos, EXPR_STRING);
2849 *p = *e;
2850 type = evaluate_expression(p);
2851 if (ctype->bit_size != -1) {
2852 struct symbol *char_type = e->wide ? wchar_ctype : &char_ctype;
2853 unsigned int size_with_null = ctype->bit_size + char_type->bit_size;
2854 if (size_with_null < type->bit_size)
2855 warning(e->pos,
2856 "too long initializer-string for array of char");
2857 else if (Winit_cstring && size_with_null == type->bit_size) {
2858 warning(e->pos,
2859 "too long initializer-string for array of char(no space for nul char)");
2862 *ep = p;
2863 return 1;
2866 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2868 struct symbol *type;
2869 int class = classify_type(ctype, &type);
2870 if (!handle_initializer(ep, 0, class, ctype, 0))
2871 expression_error(*ep, "invalid initializer");
2874 static struct symbol *cast_to_bool(struct expression *expr)
2876 struct expression *old = expr->cast_expression;
2877 struct expression *zero;
2878 struct symbol *otype;
2879 int oclass = classify_type(degenerate(old), &otype);
2880 struct symbol *ctype;
2882 if (oclass & TYPE_COMPOUND)
2883 return NULL;
2885 zero = alloc_const_expression(expr->pos, 0);
2886 expr->op = SPECIAL_NOTEQUAL;
2887 ctype = usual_conversions(expr->op, old, zero,
2888 oclass, TYPE_NUM, otype, zero->ctype);
2889 expr->type = EXPR_COMPARE;
2890 expr->left = cast_to(old, ctype);
2891 expr->right = cast_to(zero, ctype);
2893 return expr->ctype;
2896 static int cast_flags(struct expression *expr, struct expression *old)
2898 struct symbol *t;
2899 int class;
2900 int flags = CEF_NONE;
2902 class = classify_type(expr->ctype, &t);
2903 if (class & TYPE_NUM) {
2904 flags = old->flags & ~CEF_CONST_MASK;
2906 * Casts to numeric types never result in address
2907 * constants [6.6(9)].
2909 flags &= ~CEF_ADDR;
2912 * As an extension, treat address constants cast to
2913 * integer type as an arithmetic constant.
2915 if (old->flags & CEF_ADDR)
2916 flags = CEF_ACE;
2919 * Cast to float type -> not an integer constant
2920 * expression [6.6(6)].
2922 if (class & TYPE_FLOAT)
2923 flags &= ~CEF_CLR_ICE;
2925 * Casts of float literals to integer type results in
2926 * a constant integer expression [6.6(6)].
2928 else if (old->flags & CEF_FLOAT)
2929 flags = CEF_SET_ICE;
2930 } else if (class & TYPE_PTR) {
2932 * Casts of integer literals to pointer type yield
2933 * address constants [6.6(9)].
2935 * As an extension, treat address constants cast to a
2936 * different pointer type as address constants again.
2938 * As another extension, treat integer constant
2939 * expressions (in contrast to literals) cast to
2940 * pointer type as address constants.
2942 if (old->flags & (CEF_ICE | CEF_ADDR))
2943 flags = CEF_ADDR;
2946 return flags;
2950 // check if a type matches one of the members of a union type
2951 // @utype: the union type
2952 // @type: to type to check
2953 // @return: to identifier of the matching type in the union.
2954 static struct symbol *find_member_type(struct symbol *utype, struct symbol *type)
2956 struct symbol *t, *member;
2958 if (utype->type != SYM_UNION)
2959 return NULL;
2961 FOR_EACH_PTR(utype->symbol_list, member) {
2962 classify_type(member, &t);
2963 if (type == t)
2964 return member;
2965 } END_FOR_EACH_PTR(member);
2966 return NULL;
2969 static struct symbol *evaluate_compound_literal(struct expression *expr, struct expression *source)
2971 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2972 struct symbol *sym = expr->cast_type;
2974 sym->initializer = source;
2975 evaluate_symbol(sym);
2977 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2978 addr->symbol = sym;
2979 if (sym->ctype.modifiers & MOD_TOPLEVEL)
2980 addr->flags |= CEF_ADDR;
2982 expr->type = EXPR_PREOP;
2983 expr->op = '*';
2984 expr->deref = addr;
2985 expr->ctype = sym;
2986 return sym;
2989 static struct symbol *evaluate_cast(struct expression *expr)
2991 struct expression *source = expr->cast_expression;
2992 struct symbol *ctype;
2993 struct symbol *ttype, *stype;
2994 struct symbol *member;
2995 int tclass, sclass;
2996 struct ident *tas = NULL, *sas = NULL;
2998 if (!source)
2999 return NULL;
3002 * Special case: a cast can be followed by an
3003 * initializer, in which case we need to pass
3004 * the type value down to that initializer rather
3005 * than trying to evaluate it as an expression
3006 * (cfr. compound literals: C99 & C11 6.5.2.5).
3008 * A more complex case is when the initializer is
3009 * dereferenced as part of a post-fix expression.
3010 * We need to produce an expression that can be dereferenced.
3012 if (source->type == EXPR_INITIALIZER)
3013 return evaluate_compound_literal(expr, source);
3015 ctype = examine_symbol_type(expr->cast_type);
3016 expr->ctype = ctype;
3017 expr->cast_type = ctype;
3019 evaluate_expression(source);
3020 degenerate(source);
3022 tclass = classify_type(ctype, &ttype);
3024 expr->flags = cast_flags(expr, source);
3027 * You can always throw a value away by casting to
3028 * "void" - that's an implicit "force". Note that
3029 * the same is _not_ true of "void *".
3031 if (ttype == &void_ctype)
3032 goto out;
3034 stype = source->ctype;
3035 if (!stype) {
3036 expression_error(expr, "cast from unknown type");
3037 goto out;
3039 sclass = classify_type(stype, &stype);
3041 if (expr->type == EXPR_FORCE_CAST)
3042 goto out;
3044 if (tclass & (TYPE_COMPOUND | TYPE_FN)) {
3046 * Special case: cast to union type (GCC extension)
3047 * The effect is similar to a compound literal except
3048 * that the result is a rvalue.
3050 if ((member = find_member_type(ttype, stype))) {
3051 struct expression *item, *init;
3053 if (Wunion_cast)
3054 warning(expr->pos, "cast to union type");
3056 item = alloc_expression(source->pos, EXPR_IDENTIFIER);
3057 item->expr_ident = member->ident;
3058 item->ident_expression = source;
3060 init = alloc_expression(source->pos, EXPR_INITIALIZER);
3061 add_expression(&init->expr_list, item);
3063 // FIXME: this should be a rvalue
3064 evaluate_compound_literal(expr, init);
3065 return ctype;
3068 warning(expr->pos, "cast to non-scalar");
3071 if (sclass & TYPE_COMPOUND)
3072 warning(expr->pos, "cast from non-scalar");
3074 /* allowed cast unfouls */
3075 if (sclass & TYPE_FOULED)
3076 stype = unfoul(stype);
3078 if (ttype != stype) {
3079 if ((tclass & TYPE_RESTRICT) && restricted_value(source, ttype))
3080 warning(expr->pos, "cast to %s",
3081 show_typename(ttype));
3082 if (sclass & TYPE_RESTRICT) {
3083 if (ttype == &bool_ctype) {
3084 if (sclass & TYPE_FOULED)
3085 warning(expr->pos, "%s degrades to integer",
3086 show_typename(stype));
3087 } else {
3088 warning(expr->pos, "cast from %s",
3089 show_typename(stype));
3094 if ((ttype == &ulong_ctype || ttype == uintptr_ctype) && !Wcast_from_as)
3095 tas = &bad_address_space;
3096 else if (tclass == TYPE_PTR) {
3097 examine_pointer_target(ttype);
3098 tas = ttype->ctype.as;
3101 if ((stype == &ulong_ctype || stype == uintptr_ctype))
3102 sas = &bad_address_space;
3103 else if (sclass == TYPE_PTR) {
3104 examine_pointer_target(stype);
3105 sas = stype->ctype.as;
3108 if (!tas && valid_as(sas))
3109 warning(expr->pos, "cast removes address space '%s' of expression", show_as(sas));
3110 if (valid_as(tas) && valid_as(sas) && tas != sas)
3111 warning(expr->pos, "cast between address spaces (%s -> %s)", show_as(sas), show_as(tas));
3112 if (valid_as(tas) && !sas &&
3113 !is_null_pointer_constant(source) && Wcast_to_as)
3114 warning(expr->pos,
3115 "cast adds address space '%s' to expression", show_as(tas));
3117 if (!(ttype->ctype.modifiers & MOD_PTRINHERIT) && tclass == TYPE_PTR &&
3118 !tas && (source->flags & CEF_ICE)) {
3119 if (ttype->ctype.base_type == &void_ctype) {
3120 if (is_zero_constant(source)) {
3121 /* NULL */
3122 expr->type = EXPR_VALUE;
3123 expr->ctype = &null_ctype;
3124 expr->value = 0;
3125 return expr->ctype;
3130 if (ttype == &bool_ctype)
3131 cast_to_bool(expr);
3133 // checks pointers to restricted
3134 while (Wbitwise_pointer && tclass == TYPE_PTR && sclass == TYPE_PTR) {
3135 tclass = classify_type(ttype->ctype.base_type, &ttype);
3136 sclass = classify_type(stype->ctype.base_type, &stype);
3137 if (ttype == stype)
3138 break;
3139 if (!ttype || !stype)
3140 break;
3141 if (ttype == &void_ctype || stype == &void_ctype)
3142 break;
3143 if (tclass & TYPE_RESTRICT) {
3144 warning(expr->pos, "cast to %s", show_typename(ctype));
3145 break;
3147 if (sclass & TYPE_RESTRICT) {
3148 warning(expr->pos, "cast from %s", show_typename(source->ctype));
3149 break;
3152 out:
3153 return ctype;
3157 * Evaluate a call expression with a symbol. This
3158 * should expand inline functions, and evaluate
3159 * builtins.
3161 static int evaluate_symbol_call(struct expression *expr)
3163 struct expression *fn = expr->fn;
3164 struct symbol *ctype = fn->ctype;
3166 if (fn->type != EXPR_PREOP)
3167 return 0;
3169 if (ctype->op && ctype->op->evaluate)
3170 return ctype->op->evaluate(expr);
3172 return 0;
3175 static struct symbol *evaluate_call(struct expression *expr)
3177 int args, fnargs;
3178 struct symbol *ctype, *sym;
3179 struct expression *fn = expr->fn;
3180 struct expression_list *arglist = expr->args;
3182 if (!evaluate_expression(fn))
3183 return NULL;
3184 sym = ctype = fn->ctype;
3185 if (ctype->type == SYM_NODE)
3186 ctype = ctype->ctype.base_type;
3187 if (ctype->type == SYM_PTR)
3188 ctype = get_base_type(ctype);
3190 if (ctype->type != SYM_FN) {
3191 struct expression *arg;
3193 if (fn->ctype == &bad_ctype)
3194 return NULL;
3196 expression_error(expr, "not a function %s",
3197 show_ident(sym->ident));
3198 /* do typechecking in arguments */
3199 FOR_EACH_PTR (arglist, arg) {
3200 evaluate_expression(arg);
3201 } END_FOR_EACH_PTR(arg);
3202 return NULL;
3205 examine_fn_arguments(ctype);
3206 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
3207 sym->op && sym->op->args) {
3208 if (!sym->op->args(expr))
3209 return NULL;
3210 } else {
3211 if (!evaluate_arguments(ctype->arguments, arglist))
3212 return NULL;
3213 args = expression_list_size(expr->args);
3214 fnargs = symbol_list_size(ctype->arguments);
3215 if (args < fnargs) {
3216 expression_error(expr,
3217 "not enough arguments for function %s",
3218 show_ident(sym->ident));
3219 return NULL;
3221 if (args > fnargs && !ctype->variadic)
3222 expression_error(expr,
3223 "too many arguments for function %s",
3224 show_ident(sym->ident));
3226 expr->ctype = ctype->ctype.base_type;
3227 if (sym->type == SYM_NODE) {
3228 if (evaluate_symbol_call(expr))
3229 return expr->ctype;
3231 return expr->ctype;
3234 static struct symbol *evaluate_offsetof(struct expression *expr)
3236 struct expression *e = expr->down;
3237 struct symbol *ctype = expr->in;
3238 int class;
3240 if (expr->op == '.') {
3241 struct symbol *field;
3242 int offset = 0;
3243 if (!ctype) {
3244 expression_error(expr, "expected structure or union");
3245 return NULL;
3247 examine_symbol_type(ctype);
3248 class = classify_type(ctype, &ctype);
3249 if (class != TYPE_COMPOUND) {
3250 expression_error(expr, "expected structure or union");
3251 return NULL;
3254 field = find_identifier(expr->ident, ctype->symbol_list, &offset);
3255 if (!field) {
3256 expression_error(expr, "unknown member");
3257 return NULL;
3259 ctype = field;
3260 expr->type = EXPR_VALUE;
3261 expr->flags = CEF_SET_ICE;
3262 expr->value = offset;
3263 expr->taint = 0;
3264 expr->ctype = size_t_ctype;
3265 } else {
3266 if (!ctype) {
3267 expression_error(expr, "expected structure or union");
3268 return NULL;
3270 examine_symbol_type(ctype);
3271 class = classify_type(ctype, &ctype);
3272 if (class != (TYPE_COMPOUND | TYPE_PTR)) {
3273 expression_error(expr, "expected array");
3274 return NULL;
3276 ctype = ctype->ctype.base_type;
3277 if (!expr->index) {
3278 expr->type = EXPR_VALUE;
3279 expr->flags = CEF_SET_ICE;
3280 expr->value = 0;
3281 expr->taint = 0;
3282 expr->ctype = size_t_ctype;
3283 } else {
3284 struct expression *idx = expr->index, *m;
3285 struct symbol *i_type = evaluate_expression(idx);
3286 unsigned old_idx_flags;
3287 int i_class = classify_type(i_type, &i_type);
3289 if (!is_int(i_class)) {
3290 expression_error(expr, "non-integer index");
3291 return NULL;
3293 unrestrict(idx, i_class, &i_type);
3294 old_idx_flags = idx->flags;
3295 idx = cast_to(idx, size_t_ctype);
3296 idx->flags = old_idx_flags;
3297 m = alloc_const_expression(expr->pos,
3298 bits_to_bytes(ctype->bit_size));
3299 m->ctype = size_t_ctype;
3300 m->flags = CEF_SET_INT;
3301 expr->type = EXPR_BINOP;
3302 expr->left = idx;
3303 expr->right = m;
3304 expr->op = '*';
3305 expr->ctype = size_t_ctype;
3306 expr->flags = m->flags & idx->flags & ~CEF_CONST_MASK;
3309 if (e) {
3310 struct expression *copy = __alloc_expression(0);
3311 *copy = *expr;
3312 if (e->type == EXPR_OFFSETOF)
3313 e->in = ctype;
3314 if (!evaluate_expression(e))
3315 return NULL;
3316 expr->type = EXPR_BINOP;
3317 expr->flags = e->flags & copy->flags & ~CEF_CONST_MASK;
3318 expr->op = '+';
3319 expr->ctype = size_t_ctype;
3320 expr->left = copy;
3321 expr->right = e;
3323 return size_t_ctype;
3326 static void check_label_declaration(struct position pos, struct symbol *label)
3328 switch (label->namespace) {
3329 case NS_LABEL:
3330 if (label->stmt)
3331 break;
3332 sparse_error(pos, "label '%s' was not declared", show_ident(label->ident));
3333 /* fallthrough */
3334 case NS_NONE:
3335 current_fn->bogus_linear = 1;
3336 default:
3337 break;
3341 static int type_selection(struct symbol *ctrl, struct symbol *type)
3343 struct ctype c = { .base_type = ctrl };
3344 struct ctype t = { .base_type = type };
3346 return !type_difference(&c, &t, 0, 0);
3349 static struct symbol *evaluate_generic_selection(struct expression *expr)
3351 struct type_expression *map;
3352 struct expression *res;
3353 struct symbol source;
3354 struct symbol *ctrl;
3356 if (!evaluate_expression(expr->control))
3357 return NULL;
3358 if (!(ctrl = degenerate(expr->control)))
3359 return NULL;
3361 source = *ctrl;
3362 source.ctype.modifiers &= ~(MOD_QUALIFIER|MOD_ATOMIC);
3363 for (map = expr->map; map; map = map->next) {
3364 struct symbol *stype = map->type;
3365 struct symbol *base;
3367 if (!evaluate_symbol(stype))
3368 continue;
3370 base = stype->ctype.base_type;
3371 if (base->type == SYM_ARRAY && base->array_size) {
3372 get_expression_value_silent(base->array_size);
3373 if (base->array_size->type == EXPR_VALUE)
3374 continue;
3375 sparse_error(stype->pos, "variable length array type in generic selection");
3376 continue;
3378 if (is_func_type(stype)) {
3379 sparse_error(stype->pos, "function type in generic selection");
3380 continue;
3382 if (stype->bit_size <= 0 || is_void_type(stype)) {
3383 sparse_error(stype->pos, "incomplete type in generic selection");
3384 continue;
3386 if (!type_selection(&source, stype))
3387 continue;
3389 res = map->expr;
3390 goto found;
3392 res = expr->def;
3393 if (!res) {
3394 sparse_error(expr->pos, "no generic selection for '%s'", show_typename(ctrl));
3395 return NULL;
3398 found:
3399 *expr = *res;
3400 return evaluate_expression(expr);
3403 struct symbol *evaluate_expression(struct expression *expr)
3405 if (!expr)
3406 return NULL;
3407 if (expr->ctype)
3408 return expr->ctype;
3410 switch (expr->type) {
3411 case EXPR_VALUE:
3412 case EXPR_FVALUE:
3413 expression_error(expr, "value expression without a type");
3414 return NULL;
3415 case EXPR_STRING:
3416 return evaluate_string(expr);
3417 case EXPR_SYMBOL:
3418 return evaluate_symbol_expression(expr);
3419 case EXPR_BINOP:
3420 evaluate_expression(expr->left);
3421 evaluate_expression(expr->right);
3422 if (!valid_subexpr_type(expr))
3423 return NULL;
3424 return evaluate_binop(expr);
3425 case EXPR_LOGICAL:
3426 return evaluate_logical(expr);
3427 case EXPR_COMMA:
3428 evaluate_expression(expr->left);
3429 if (!evaluate_expression(expr->right))
3430 return NULL;
3431 return evaluate_comma(expr);
3432 case EXPR_COMPARE:
3433 evaluate_expression(expr->left);
3434 evaluate_expression(expr->right);
3435 if (!valid_subexpr_type(expr))
3436 return NULL;
3437 return evaluate_compare(expr);
3438 case EXPR_ASSIGNMENT:
3439 evaluate_expression(expr->left);
3440 evaluate_expression(expr->right);
3441 if (!valid_subexpr_type(expr))
3442 return NULL;
3443 return evaluate_assignment(expr);
3444 case EXPR_PREOP:
3445 if (!evaluate_expression(expr->unop))
3446 return NULL;
3447 return evaluate_preop(expr);
3448 case EXPR_POSTOP:
3449 if (!evaluate_expression(expr->unop))
3450 return NULL;
3451 return evaluate_postop(expr);
3452 case EXPR_CAST:
3453 case EXPR_FORCE_CAST:
3454 case EXPR_IMPLIED_CAST:
3455 return evaluate_cast(expr);
3456 case EXPR_SIZEOF:
3457 return evaluate_sizeof(expr);
3458 case EXPR_PTRSIZEOF:
3459 return evaluate_ptrsizeof(expr);
3460 case EXPR_ALIGNOF:
3461 return evaluate_alignof(expr);
3462 case EXPR_DEREF:
3463 return evaluate_member_dereference(expr);
3464 case EXPR_CALL:
3465 return evaluate_call(expr);
3466 case EXPR_SELECT:
3467 case EXPR_CONDITIONAL:
3468 return evaluate_conditional_expression(expr);
3469 case EXPR_STATEMENT:
3470 expr->ctype = evaluate_statement(expr->statement);
3471 return expr->ctype;
3473 case EXPR_LABEL:
3474 expr->ctype = &ptr_ctype;
3475 check_label_declaration(expr->pos, expr->label_symbol);
3476 return &ptr_ctype;
3478 case EXPR_TYPE:
3479 /* Evaluate the type of the symbol .. */
3480 evaluate_symbol(expr->symbol);
3481 /* .. but the type of the _expression_ is a "type" */
3482 expr->ctype = &type_ctype;
3483 return &type_ctype;
3485 case EXPR_OFFSETOF:
3486 return evaluate_offsetof(expr);
3488 case EXPR_GENERIC:
3489 return evaluate_generic_selection(expr);
3491 /* These can not exist as stand-alone expressions */
3492 case EXPR_INITIALIZER:
3493 case EXPR_IDENTIFIER:
3494 case EXPR_INDEX:
3495 case EXPR_POS:
3496 expression_error(expr, "internal front-end error: initializer in expression");
3497 return NULL;
3498 case EXPR_SLICE:
3499 expression_error(expr, "internal front-end error: SLICE re-evaluated");
3500 return NULL;
3502 return NULL;
3505 void check_duplicates(struct symbol *sym)
3507 int declared = 0;
3508 struct symbol *next = sym;
3509 int initialized = sym->initializer != NULL;
3511 while ((next = next->same_symbol) != NULL) {
3512 const char *typediff;
3513 evaluate_symbol(next);
3514 if (initialized && next->initializer) {
3515 sparse_error(sym->pos, "symbol '%s' has multiple initializers (originally initialized at %s:%d)",
3516 show_ident(sym->ident),
3517 stream_name(next->pos.stream), next->pos.line);
3518 /* Only warn once */
3519 initialized = 0;
3521 declared++;
3522 typediff = type_difference(&sym->ctype, &next->ctype, 0, 0);
3523 if (typediff) {
3524 sparse_error(sym->pos, "symbol '%s' redeclared with different type (%s):",
3525 show_ident(sym->ident), typediff);
3526 info(sym->pos, " %s", show_typename(sym));
3527 info(next->pos, "note: previously declared as:");
3528 info(next->pos, " %s", show_typename(next));
3529 return;
3532 if (!declared) {
3533 unsigned long mod = sym->ctype.modifiers;
3534 if (mod & (MOD_STATIC | MOD_REGISTER | MOD_EXT_VISIBLE))
3535 return;
3536 if (!(mod & MOD_TOPLEVEL))
3537 return;
3538 if (!Wdecl)
3539 return;
3540 if (sym->ident == &main_ident)
3541 return;
3542 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
3546 static struct symbol *evaluate_symbol(struct symbol *sym)
3548 struct symbol *base_type;
3550 if (!sym)
3551 return sym;
3552 if (sym->evaluated)
3553 return sym;
3554 sym->evaluated = 1;
3556 sym = examine_symbol_type(sym);
3557 base_type = get_base_type(sym);
3558 if (!base_type)
3559 return NULL;
3561 /* Evaluate the initializers */
3562 if (sym->initializer)
3563 evaluate_initializer(sym, &sym->initializer);
3565 /* And finally, evaluate the body of the symbol too */
3566 if (base_type->type == SYM_FN) {
3567 struct symbol *curr = current_fn;
3569 if (sym->definition && sym->definition != sym)
3570 return evaluate_symbol(sym->definition);
3572 current_fn = sym;
3574 examine_fn_arguments(base_type);
3575 if (!base_type->stmt && base_type->inline_stmt)
3576 uninline(sym);
3577 if (base_type->stmt)
3578 evaluate_statement(base_type->stmt);
3580 current_fn = curr;
3583 return base_type;
3586 void evaluate_symbol_list(struct symbol_list *list)
3588 struct symbol *sym;
3590 FOR_EACH_PTR(list, sym) {
3591 has_error &= ~ERROR_CURR_PHASE;
3592 evaluate_symbol(sym);
3593 check_duplicates(sym);
3594 } END_FOR_EACH_PTR(sym);
3597 static struct symbol *evaluate_return_expression(struct statement *stmt)
3599 struct expression *expr = stmt->expression;
3600 struct symbol *fntype, *rettype;
3602 evaluate_expression(expr);
3603 fntype = current_fn->ctype.base_type;
3604 rettype = fntype->ctype.base_type;
3605 if (!rettype || rettype == &void_ctype) {
3606 if (expr && !is_void_type(expr->ctype))
3607 expression_error(expr, "return expression in %s function", rettype?"void":"typeless");
3608 if (expr && Wreturn_void)
3609 warning(stmt->pos, "returning void-valued expression");
3610 return NULL;
3613 if (!expr) {
3614 sparse_error(stmt->pos, "return with no return value");
3615 return NULL;
3617 if (!expr->ctype)
3618 return NULL;
3619 compatible_assignment_types(expr, rettype, &stmt->expression, "return expression");
3620 return NULL;
3623 static void evaluate_if_statement(struct statement *stmt)
3625 if (!stmt->if_conditional)
3626 return;
3628 evaluate_conditional(stmt->if_conditional, 0);
3629 evaluate_statement(stmt->if_true);
3630 evaluate_statement(stmt->if_false);
3633 static void evaluate_iterator(struct statement *stmt)
3635 evaluate_symbol_list(stmt->iterator_syms);
3636 evaluate_conditional(stmt->iterator_pre_condition, 1);
3637 evaluate_conditional(stmt->iterator_post_condition,1);
3638 evaluate_statement(stmt->iterator_pre_statement);
3639 evaluate_statement(stmt->iterator_statement);
3640 evaluate_statement(stmt->iterator_post_statement);
3644 static void parse_asm_constraint(struct asm_operand *op)
3646 struct expression *constraint = op->constraint;
3647 const char *str = constraint->string->data;
3648 int c;
3650 switch (str[0]) {
3651 case '\0':
3652 sparse_error(constraint->pos, "invalid ASM constraint (\"\")");
3653 break;
3654 case '+':
3655 op->is_modify = true;
3656 /* fall-through */
3657 case '=':
3658 op->is_assign = true;
3659 str++;
3660 break;
3663 while ((c = *str++)) {
3664 switch (c) {
3665 case '=':
3666 case '+':
3667 sparse_error(constraint->pos, "invalid ASM constraint '%c'", c);
3668 break;
3670 case '&':
3671 op->is_earlyclobber = true;
3672 break;
3673 case '%':
3674 op->is_commutative = true;
3675 break;
3676 case 'r':
3677 op->is_register = true;
3678 break;
3680 case 'm':
3681 case 'o':
3682 case 'V':
3683 case 'Q':
3684 op->is_memory = true;
3685 break;
3687 case '<':
3688 case '>':
3689 // FIXME: ignored for now
3690 break;
3692 case ',':
3693 // FIXME: multiple alternative constraints
3694 break;
3696 case '0' ... '9':
3697 // FIXME: numeric matching constraint?
3698 break;
3699 case '[':
3700 // FIXME: symbolic matching constraint
3701 return;
3703 default:
3704 if (arch_target->asm_constraint)
3705 str = arch_target->asm_constraint(op, c, str);
3707 // FIXME: multi-letter constraints
3708 break;
3712 // FIXME: how to deal with multi-constraint?
3713 if (op->is_register)
3714 op->is_memory = 0;
3717 static void verify_output_constraint(struct asm_operand *op)
3719 struct expression *expr = op->constraint;
3720 const char *constraint = expr->string->data;
3722 if (!op->is_assign)
3723 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
3726 static void verify_input_constraint(struct asm_operand *op)
3728 struct expression *expr = op->constraint;
3729 const char *constraint = expr->string->data;
3731 if (op->is_assign)
3732 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
3735 static void evaluate_asm_memop(struct asm_operand *op)
3737 if (op->is_memory) {
3738 struct expression *expr = op->expr;
3739 struct expression *addr;
3741 // implicit addressof
3742 addr = alloc_expression(expr->pos, EXPR_PREOP);
3743 addr->op = '&';
3744 addr->unop = expr;
3746 evaluate_addressof(addr);
3747 op->expr = addr;
3748 } else {
3749 evaluate_expression(op->expr);
3750 degenerate(op->expr);
3754 static void evaluate_asm_statement(struct statement *stmt)
3756 struct expression *expr;
3757 struct asm_operand *op;
3758 struct symbol *sym;
3760 if (!stmt->asm_string)
3761 return;
3763 FOR_EACH_PTR(stmt->asm_outputs, op) {
3764 /* Identifier */
3766 /* Constraint */
3767 if (op->constraint) {
3768 parse_asm_constraint(op);
3769 verify_output_constraint(op);
3772 /* Expression */
3773 expr = op->expr;
3774 if (!evaluate_expression(expr))
3775 return;
3776 if (!lvalue_expression(expr))
3777 warning(expr->pos, "asm output is not an lvalue");
3778 evaluate_assign_to(expr, expr->ctype);
3779 evaluate_asm_memop(op);
3780 } END_FOR_EACH_PTR(op);
3782 FOR_EACH_PTR(stmt->asm_inputs, op) {
3783 /* Identifier */
3785 /* Constraint */
3786 if (op->constraint) {
3787 parse_asm_constraint(op);
3788 verify_input_constraint(op);
3791 /* Expression */
3792 if (!evaluate_expression(op->expr))
3793 return;
3794 evaluate_asm_memop(op);
3795 } END_FOR_EACH_PTR(op);
3797 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
3798 if (!expr) {
3799 sparse_error(stmt->pos, "bad asm clobbers");
3800 return;
3802 if (expr->type == EXPR_STRING)
3803 continue;
3804 expression_error(expr, "asm clobber is not a string");
3805 } END_FOR_EACH_PTR(expr);
3807 FOR_EACH_PTR(stmt->asm_labels, sym) {
3808 if (!sym || sym->type != SYM_LABEL) {
3809 sparse_error(stmt->pos, "bad asm label");
3810 return;
3812 } END_FOR_EACH_PTR(sym);
3815 static void evaluate_case_statement(struct statement *stmt)
3817 evaluate_expression(stmt->case_expression);
3818 evaluate_expression(stmt->case_to);
3819 evaluate_statement(stmt->case_statement);
3822 static void check_case_type(struct expression *switch_expr,
3823 struct expression *case_expr,
3824 struct expression **enumcase)
3826 struct symbol *switch_type, *case_type;
3827 int sclass, cclass;
3829 if (!case_expr)
3830 return;
3832 switch_type = switch_expr->ctype;
3833 case_type = evaluate_expression(case_expr);
3835 if (!switch_type || !case_type)
3836 goto Bad;
3837 if (enumcase) {
3838 if (*enumcase)
3839 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
3840 else if (is_enum_type(case_type))
3841 *enumcase = case_expr;
3844 sclass = classify_type(switch_type, &switch_type);
3845 cclass = classify_type(case_type, &case_type);
3847 /* both should be arithmetic */
3848 if (!(sclass & cclass & TYPE_NUM))
3849 goto Bad;
3851 /* neither should be floating */
3852 if ((sclass | cclass) & TYPE_FLOAT)
3853 goto Bad;
3855 /* if neither is restricted, we are OK */
3856 if (!((sclass | cclass) & TYPE_RESTRICT))
3857 return;
3859 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
3860 cclass, sclass, case_type, switch_type)) {
3861 unrestrict(case_expr, cclass, &case_type);
3862 unrestrict(switch_expr, sclass, &switch_type);
3864 return;
3866 Bad:
3867 expression_error(case_expr, "incompatible types for 'case' statement");
3870 static void evaluate_switch_statement(struct statement *stmt)
3872 struct symbol *sym;
3873 struct expression *enumcase = NULL;
3874 struct expression **enumcase_holder = &enumcase;
3875 struct expression *sel = stmt->switch_expression;
3877 evaluate_expression(sel);
3878 evaluate_statement(stmt->switch_statement);
3879 if (!sel)
3880 return;
3881 if (sel->ctype && is_enum_type(sel->ctype))
3882 enumcase_holder = NULL; /* Only check cases against switch */
3884 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
3885 struct statement *case_stmt = sym->stmt;
3886 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
3887 check_case_type(sel, case_stmt->case_to, enumcase_holder);
3888 } END_FOR_EACH_PTR(sym);
3891 static void evaluate_goto_statement(struct statement *stmt)
3893 struct symbol *label = stmt->goto_label;
3895 if (!label) {
3896 // no label associated, may be a computed goto
3897 evaluate_expression(stmt->goto_expression);
3898 return;
3901 check_label_declaration(stmt->pos, label);
3904 struct symbol *evaluate_statement(struct statement *stmt)
3906 if (!stmt)
3907 return NULL;
3909 switch (stmt->type) {
3910 case STMT_DECLARATION: {
3911 struct symbol *s;
3912 FOR_EACH_PTR(stmt->declaration, s) {
3913 evaluate_symbol(s);
3914 } END_FOR_EACH_PTR(s);
3915 return NULL;
3918 case STMT_RETURN:
3919 return evaluate_return_expression(stmt);
3921 case STMT_EXPRESSION:
3922 if (!evaluate_expression(stmt->expression))
3923 return NULL;
3924 if (stmt->expression->ctype == &null_ctype)
3925 stmt->expression = cast_to(stmt->expression, &ptr_ctype);
3926 return degenerate(stmt->expression);
3928 case STMT_COMPOUND: {
3929 struct statement *s;
3930 struct symbol *type = NULL;
3932 /* Evaluate the return symbol in the compound statement */
3933 evaluate_symbol(stmt->ret);
3936 * Then, evaluate each statement, making the type of the
3937 * compound statement be the type of the last statement
3939 type = evaluate_statement(stmt->args);
3940 FOR_EACH_PTR(stmt->stmts, s) {
3941 type = evaluate_statement(s);
3942 } END_FOR_EACH_PTR(s);
3943 if (!type)
3944 type = &void_ctype;
3945 return type;
3947 case STMT_IF:
3948 evaluate_if_statement(stmt);
3949 return NULL;
3950 case STMT_ITERATOR:
3951 evaluate_iterator(stmt);
3952 return NULL;
3953 case STMT_SWITCH:
3954 evaluate_switch_statement(stmt);
3955 return NULL;
3956 case STMT_CASE:
3957 evaluate_case_statement(stmt);
3958 return NULL;
3959 case STMT_LABEL:
3960 return evaluate_statement(stmt->label_statement);
3961 case STMT_GOTO:
3962 evaluate_goto_statement(stmt);
3963 return NULL;
3964 case STMT_NONE:
3965 break;
3966 case STMT_ASM:
3967 evaluate_asm_statement(stmt);
3968 return NULL;
3969 case STMT_CONTEXT:
3970 evaluate_expression(stmt->expression);
3971 return NULL;
3972 case STMT_RANGE:
3973 evaluate_expression(stmt->range_expression);
3974 evaluate_expression(stmt->range_low);
3975 evaluate_expression(stmt->range_high);
3976 return NULL;
3978 return NULL;