add sparse_keep_tokens api to lib.h
[smatch.git] / evaluate.c
blob9f543feb1a3981153ef58ab9bebcab32c66f18d1
1 /*
2 * sparse/evaluate.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
9 * Evaluate constant expressions.
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <stddef.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <limits.h>
21 #include "lib.h"
22 #include "allocate.h"
23 #include "parse.h"
24 #include "token.h"
25 #include "symbol.h"
26 #include "target.h"
27 #include "expression.h"
29 struct symbol *current_fn;
31 static struct symbol *degenerate(struct expression *expr);
32 static struct symbol *evaluate_symbol(struct symbol *sym);
34 static struct symbol *evaluate_symbol_expression(struct expression *expr)
36 struct expression *addr;
37 struct symbol *sym = expr->symbol;
38 struct symbol *base_type;
40 if (!sym) {
41 expression_error(expr, "undefined identifier '%s'", show_ident(expr->symbol_name));
42 return NULL;
45 examine_symbol_type(sym);
47 base_type = get_base_type(sym);
48 if (!base_type) {
49 expression_error(expr, "identifier '%s' has no type", show_ident(expr->symbol_name));
50 return NULL;
53 addr = alloc_expression(expr->pos, EXPR_SYMBOL);
54 addr->symbol = sym;
55 addr->symbol_name = expr->symbol_name;
56 addr->ctype = &lazy_ptr_ctype; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
57 expr->type = EXPR_PREOP;
58 expr->op = '*';
59 expr->unop = addr;
61 /* The type of a symbol is the symbol itself! */
62 expr->ctype = sym;
63 return sym;
66 static struct symbol *evaluate_string(struct expression *expr)
68 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
69 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
70 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
71 struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
72 unsigned int length = expr->string->length;
74 sym->array_size = alloc_const_expression(expr->pos, length);
75 sym->bit_size = bits_in_char * length;
76 sym->ctype.alignment = 1;
77 sym->string = 1;
78 sym->ctype.modifiers = MOD_STATIC;
79 sym->ctype.base_type = array;
80 sym->initializer = initstr;
82 initstr->ctype = sym;
83 initstr->string = expr->string;
85 array->array_size = sym->array_size;
86 array->bit_size = bits_in_char * length;
87 array->ctype.alignment = 1;
88 array->ctype.modifiers = MOD_STATIC;
89 array->ctype.base_type = &char_ctype;
91 addr->symbol = sym;
92 addr->ctype = &lazy_ptr_ctype;
94 expr->type = EXPR_PREOP;
95 expr->op = '*';
96 expr->unop = addr;
97 expr->ctype = sym;
98 return sym;
101 static inline struct symbol *integer_promotion(struct symbol *type)
103 struct symbol *orig_type = type;
104 unsigned long mod = type->ctype.modifiers;
105 int width;
107 if (type->type == SYM_NODE)
108 type = type->ctype.base_type;
109 if (type->type == SYM_ENUM)
110 type = type->ctype.base_type;
111 width = type->bit_size;
114 * Bitfields always promote to the base type,
115 * even if the bitfield might be bigger than
116 * an "int".
118 if (type->type == SYM_BITFIELD) {
119 type = type->ctype.base_type;
120 orig_type = type;
122 mod = type->ctype.modifiers;
123 if (width < bits_in_int)
124 return &int_ctype;
126 /* If char/short has as many bits as int, it still gets "promoted" */
127 if (mod & (MOD_CHAR | MOD_SHORT)) {
128 if (mod & MOD_UNSIGNED)
129 return &uint_ctype;
130 return &int_ctype;
132 return orig_type;
136 * integer part of usual arithmetic conversions:
137 * integer promotions are applied
138 * if left and right are identical, we are done
139 * if signedness is the same, convert one with lower rank
140 * unless unsigned argument has rank lower than signed one, convert the
141 * signed one.
142 * if signed argument is bigger than unsigned one, convert the unsigned.
143 * otherwise, convert signed.
145 * Leaving aside the integer promotions, that is equivalent to
146 * if identical, don't convert
147 * if left is bigger than right, convert right
148 * if right is bigger than left, convert right
149 * otherwise, if signedness is the same, convert one with lower rank
150 * otherwise convert the signed one.
152 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
154 unsigned long lmod, rmod;
156 left = integer_promotion(left);
157 right = integer_promotion(right);
159 if (left == right)
160 goto left;
162 if (left->bit_size > right->bit_size)
163 goto left;
165 if (right->bit_size > left->bit_size)
166 goto right;
168 lmod = left->ctype.modifiers;
169 rmod = right->ctype.modifiers;
170 if ((lmod ^ rmod) & MOD_UNSIGNED) {
171 if (lmod & MOD_UNSIGNED)
172 goto left;
173 } else if ((lmod & ~rmod) & (MOD_LONG | MOD_LONGLONG))
174 goto left;
175 right:
176 left = right;
177 left:
178 return left;
181 static int same_cast_type(struct symbol *orig, struct symbol *new)
183 return orig->bit_size == new->bit_size && orig->bit_offset == new->bit_offset;
186 static struct symbol *base_type(struct symbol *node, unsigned long *modp, unsigned long *asp)
188 unsigned long mod, as;
190 mod = 0; as = 0;
191 while (node) {
192 mod |= node->ctype.modifiers;
193 as |= node->ctype.as;
194 if (node->type == SYM_NODE) {
195 node = node->ctype.base_type;
196 continue;
198 break;
200 *modp = mod & ~MOD_IGNORE;
201 *asp = as;
202 return node;
205 static int is_same_type(struct expression *expr, struct symbol *new)
207 struct symbol *old = expr->ctype;
208 unsigned long oldmod, newmod, oldas, newas;
210 old = base_type(old, &oldmod, &oldas);
211 new = base_type(new, &newmod, &newas);
213 /* Same base type, same address space? */
214 if (old == new && oldas == newas) {
215 unsigned long difmod;
217 /* Check the modifier bits. */
218 difmod = (oldmod ^ newmod) & ~MOD_NOCAST;
220 /* Exact same type? */
221 if (!difmod)
222 return 1;
225 * Not the same type, but differs only in "const".
226 * Don't warn about MOD_NOCAST.
228 if (difmod == MOD_CONST)
229 return 0;
231 if ((oldmod | newmod) & MOD_NOCAST) {
232 const char *tofrom = "to/from";
233 if (!(newmod & MOD_NOCAST))
234 tofrom = "from";
235 if (!(oldmod & MOD_NOCAST))
236 tofrom = "to";
237 warning(expr->pos, "implicit cast %s nocast type", tofrom);
239 return 0;
242 static void
243 warn_for_different_enum_types (struct position pos,
244 struct symbol *typea,
245 struct symbol *typeb)
247 if (!Wenum_mismatch)
248 return;
249 if (typea->type == SYM_NODE)
250 typea = typea->ctype.base_type;
251 if (typeb->type == SYM_NODE)
252 typeb = typeb->ctype.base_type;
254 if (typea == typeb)
255 return;
257 if (typea->type == SYM_ENUM && typeb->type == SYM_ENUM) {
258 warning(pos, "mixing different enum types");
259 info(pos, " %s versus", show_typename(typea));
260 info(pos, " %s", show_typename(typeb));
265 * This gets called for implicit casts in assignments and
266 * integer promotion. We often want to try to move the
267 * cast down, because the ops involved may have been
268 * implicitly cast up, and we can get rid of the casts
269 * early.
271 static struct expression * cast_to(struct expression *old, struct symbol *type)
273 struct expression *expr;
275 warn_for_different_enum_types (old->pos, old->ctype, type);
277 if (old->ctype != &null_ctype && is_same_type(old, type))
278 return old;
281 * See if we can simplify the op. Move the cast down.
283 switch (old->type) {
284 case EXPR_PREOP:
285 if (old->ctype->bit_size < type->bit_size)
286 break;
287 if (old->op == '~') {
288 old->ctype = type;
289 old->unop = cast_to(old->unop, type);
290 return old;
292 break;
294 case EXPR_IMPLIED_CAST:
295 warn_for_different_enum_types(old->pos, old->ctype, type);
297 if (old->ctype->bit_size >= type->bit_size) {
298 struct expression *orig = old->cast_expression;
299 if (same_cast_type(orig->ctype, type))
300 return orig;
301 if (old->ctype->bit_offset == type->bit_offset) {
302 old->ctype = type;
303 old->cast_type = type;
304 return old;
307 break;
309 default:
310 /* nothing */;
313 expr = alloc_expression(old->pos, EXPR_IMPLIED_CAST);
314 expr->flags = old->flags;
315 expr->ctype = type;
316 expr->cast_type = type;
317 expr->cast_expression = old;
318 return expr;
321 static int is_type_type(struct symbol *type)
323 return (type->ctype.modifiers & MOD_TYPE) != 0;
326 int is_ptr_type(struct symbol *type)
328 if (type->type == SYM_NODE)
329 type = type->ctype.base_type;
330 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
333 static inline int is_float_type(struct symbol *type)
335 if (type->type == SYM_NODE)
336 type = type->ctype.base_type;
337 return type->ctype.base_type == &fp_type;
340 static inline int is_byte_type(struct symbol *type)
342 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
345 enum {
346 TYPE_NUM = 1,
347 TYPE_BITFIELD = 2,
348 TYPE_RESTRICT = 4,
349 TYPE_FLOAT = 8,
350 TYPE_PTR = 16,
351 TYPE_COMPOUND = 32,
352 TYPE_FOULED = 64,
355 static inline int classify_type(struct symbol *type, struct symbol **base)
357 static int type_class[SYM_BAD + 1] = {
358 [SYM_PTR] = TYPE_PTR,
359 [SYM_FN] = TYPE_PTR,
360 [SYM_ARRAY] = TYPE_PTR | TYPE_COMPOUND,
361 [SYM_STRUCT] = TYPE_COMPOUND,
362 [SYM_UNION] = TYPE_COMPOUND,
363 [SYM_BITFIELD] = TYPE_NUM | TYPE_BITFIELD,
364 [SYM_RESTRICT] = TYPE_NUM | TYPE_RESTRICT,
365 [SYM_FOULED] = TYPE_NUM | TYPE_RESTRICT | TYPE_FOULED,
367 if (type->type == SYM_NODE)
368 type = type->ctype.base_type;
369 if (type->type == SYM_ENUM)
370 type = type->ctype.base_type;
371 *base = type;
372 if (type->type == SYM_BASETYPE) {
373 if (type->ctype.base_type == &int_type)
374 return TYPE_NUM;
375 if (type->ctype.base_type == &fp_type)
376 return TYPE_NUM | TYPE_FLOAT;
378 return type_class[type->type];
381 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
383 static inline int is_string_type(struct symbol *type)
385 if (type->type == SYM_NODE)
386 type = type->ctype.base_type;
387 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
390 static struct symbol *bad_expr_type(struct expression *expr)
392 sparse_error(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
393 switch (expr->type) {
394 case EXPR_BINOP:
395 case EXPR_COMPARE:
396 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
397 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
398 break;
399 case EXPR_PREOP:
400 case EXPR_POSTOP:
401 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
402 break;
403 default:
404 break;
407 expr->flags = 0;
408 return expr->ctype = &bad_ctype;
411 static int restricted_value(struct expression *v, struct symbol *type)
413 if (v->type != EXPR_VALUE)
414 return 1;
415 if (v->value != 0)
416 return 1;
417 return 0;
420 static int restricted_binop(int op, struct symbol *type)
422 switch (op) {
423 case '&':
424 case '=':
425 case SPECIAL_AND_ASSIGN:
426 case SPECIAL_OR_ASSIGN:
427 case SPECIAL_XOR_ASSIGN:
428 return 1; /* unfoul */
429 case '|':
430 case '^':
431 case '?':
432 return 2; /* keep fouled */
433 case SPECIAL_EQUAL:
434 case SPECIAL_NOTEQUAL:
435 return 3; /* warn if fouled */
436 default:
437 return 0; /* warn */
441 static int restricted_unop(int op, struct symbol **type)
443 if (op == '~') {
444 if ((*type)->bit_size < bits_in_int)
445 *type = befoul(*type);
446 return 0;
447 } if (op == '+')
448 return 0;
449 return 1;
452 static struct symbol *restricted_binop_type(int op,
453 struct expression *left,
454 struct expression *right,
455 int lclass, int rclass,
456 struct symbol *ltype,
457 struct symbol *rtype)
459 struct symbol *ctype = NULL;
460 if (lclass & TYPE_RESTRICT) {
461 if (rclass & TYPE_RESTRICT) {
462 if (ltype == rtype) {
463 ctype = ltype;
464 } else if (lclass & TYPE_FOULED) {
465 if (ltype->ctype.base_type == rtype)
466 ctype = ltype;
467 } else if (rclass & TYPE_FOULED) {
468 if (rtype->ctype.base_type == ltype)
469 ctype = rtype;
471 } else {
472 if (!restricted_value(right, ltype))
473 ctype = ltype;
475 } else if (!restricted_value(left, rtype))
476 ctype = rtype;
478 if (ctype) {
479 switch (restricted_binop(op, ctype)) {
480 case 1:
481 if ((lclass ^ rclass) & TYPE_FOULED)
482 ctype = ctype->ctype.base_type;
483 break;
484 case 3:
485 if (!(lclass & rclass & TYPE_FOULED))
486 break;
487 case 0:
488 ctype = NULL;
489 default:
490 break;
494 return ctype;
497 static inline void unrestrict(struct expression *expr,
498 int class, struct symbol **ctype)
500 if (class & TYPE_RESTRICT) {
501 warning(expr->pos, "restricted degrades to integer");
502 if (class & TYPE_FOULED) /* unfoul it first */
503 *ctype = (*ctype)->ctype.base_type;
504 *ctype = (*ctype)->ctype.base_type; /* get to arithmetic type */
508 static struct symbol *usual_conversions(int op,
509 struct expression *left,
510 struct expression *right,
511 int lclass, int rclass,
512 struct symbol *ltype,
513 struct symbol *rtype)
515 struct symbol *ctype;
517 warn_for_different_enum_types(right->pos, left->ctype, right->ctype);
519 if ((lclass | rclass) & TYPE_RESTRICT)
520 goto Restr;
522 Normal:
523 if (!(lclass & TYPE_FLOAT)) {
524 if (!(rclass & TYPE_FLOAT))
525 return bigger_int_type(ltype, rtype);
526 else
527 return rtype;
528 } else if (rclass & TYPE_FLOAT) {
529 unsigned long lmod = ltype->ctype.modifiers;
530 unsigned long rmod = rtype->ctype.modifiers;
531 if (rmod & ~lmod & (MOD_LONG | MOD_LONGLONG))
532 return rtype;
533 else
534 return ltype;
535 } else
536 return ltype;
538 Restr:
539 ctype = restricted_binop_type(op, left, right,
540 lclass, rclass, ltype, rtype);
541 if (ctype)
542 return ctype;
544 unrestrict(left, lclass, &ltype);
545 unrestrict(right, rclass, &rtype);
547 goto Normal;
550 static inline int lvalue_expression(struct expression *expr)
552 return expr->type == EXPR_PREOP && expr->op == '*';
555 static int ptr_object_size(struct symbol *ptr_type)
557 if (ptr_type->type == SYM_NODE)
558 ptr_type = ptr_type->ctype.base_type;
559 if (ptr_type->type == SYM_PTR)
560 ptr_type = get_base_type(ptr_type);
561 return ptr_type->bit_size;
564 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *ctype, struct symbol *itype)
566 struct expression *index = expr->right;
567 int multiply;
568 int bit_size;
570 if (ctype == &null_ctype)
571 ctype = &ptr_ctype;
573 examine_symbol_type(ctype);
575 if (!ctype->ctype.base_type) {
576 expression_error(expr, "missing type information");
577 return NULL;
580 /* Get the size of whatever the pointer points to */
581 bit_size = ptr_object_size(ctype);
582 multiply = bit_size >> 3;
584 expr->ctype = ctype;
586 if (multiply == 1 && itype->bit_size >= bits_in_pointer)
587 return ctype;
589 if (index->type == EXPR_VALUE) {
590 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
591 unsigned long long v = index->value, mask;
592 mask = 1ULL << (itype->bit_size - 1);
593 if (v & mask)
594 v |= -mask;
595 else
596 v &= mask - 1;
597 v *= multiply;
598 mask = 1ULL << (bits_in_pointer - 1);
599 v &= mask | (mask - 1);
600 val->value = v;
601 val->ctype = ssize_t_ctype;
602 expr->right = val;
603 return ctype;
606 if (itype->bit_size < bits_in_pointer)
607 index = cast_to(index, ssize_t_ctype);
609 if (multiply > 1) {
610 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
611 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
613 val->ctype = ssize_t_ctype;
614 val->value = multiply;
616 mul->op = '*';
617 mul->ctype = ssize_t_ctype;
618 mul->left = index;
619 mul->right = val;
620 index = mul;
623 expr->right = index;
624 return ctype;
627 const char * type_difference(struct symbol *target, struct symbol *source,
628 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
630 for (;;) {
631 unsigned long mod1, mod2, diff;
632 unsigned long as1, as2;
633 int type1, type2;
634 struct symbol *base1, *base2;
636 if (target == source)
637 break;
638 if (!target || !source)
639 return "different types";
641 * Peel of per-node information.
642 * FIXME! Check alignment and context too here!
644 mod1 = target->ctype.modifiers;
645 as1 = target->ctype.as;
646 mod2 = source->ctype.modifiers;
647 as2 = source->ctype.as;
648 if (target->type == SYM_NODE) {
649 target = target->ctype.base_type;
650 if (!target)
651 return "bad types";
652 if (target->type == SYM_PTR) {
653 mod1 = 0;
654 as1 = 0;
656 mod1 |= target->ctype.modifiers;
657 as1 |= target->ctype.as;
659 if (source->type == SYM_NODE) {
660 source = source->ctype.base_type;
661 if (!source)
662 return "bad types";
663 if (source->type == SYM_PTR) {
664 mod2 = 0;
665 as2 = 0;
667 mod2 |= source->ctype.modifiers;
668 as2 |= source->ctype.as;
670 if (target->type == SYM_ENUM) {
671 target = target->ctype.base_type;
672 if (!target)
673 return "bad types";
675 if (source->type == SYM_ENUM) {
676 source = source->ctype.base_type;
677 if (!source)
678 return "bad types";
681 if (target == source)
682 break;
683 if (!target || !source)
684 return "different types";
686 type1 = target->type;
687 base1 = target->ctype.base_type;
689 type2 = source->type;
690 base2 = source->ctype.base_type;
693 * Pointers to functions compare as the function itself
695 if (type1 == SYM_PTR && base1) {
696 base1 = examine_symbol_type(base1);
697 switch (base1->type) {
698 case SYM_FN:
699 type1 = SYM_FN;
700 target = base1;
701 base1 = base1->ctype.base_type;
702 default:
703 /* nothing */;
706 if (type2 == SYM_PTR && base2) {
707 base2 = examine_symbol_type(base2);
708 switch (base2->type) {
709 case SYM_FN:
710 type2 = SYM_FN;
711 source = base2;
712 base2 = base2->ctype.base_type;
713 default:
714 /* nothing */;
718 /* Arrays degenerate to pointers for type comparisons */
719 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
720 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
722 if (type1 != type2 || type1 == SYM_RESTRICT)
723 return "different base types";
725 /* Must be same address space to be comparable */
726 if (Waddress_space && as1 != as2)
727 return "different address spaces";
729 /* Ignore differences in storage types or addressability */
730 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
731 diff &= (mod1 & ~target_mod_ignore) | (mod2 & ~source_mod_ignore);
732 if (diff) {
733 if (diff & MOD_SIZE)
734 return "different type sizes";
735 if (diff & ~MOD_SIGNEDNESS)
736 return "different modifiers";
738 /* Differs in signedness only.. */
739 if (Wtypesign) {
741 * Warn if both are explicitly signed ("unsigned" is obviously
742 * always explicit, and since we know one of them has to be
743 * unsigned, we check if the signed one was explicit).
745 if ((mod1 | mod2) & MOD_EXPLICITLY_SIGNED)
746 return "different explicit signedness";
749 * "char" matches both "unsigned char" and "signed char",
750 * so if the explicit test didn't trigger, then we should
751 * not warn about a char.
753 if (!(mod1 & MOD_CHAR))
754 return "different signedness";
758 if (type1 == SYM_FN) {
759 int i;
760 struct symbol *arg1, *arg2;
761 if (base1->variadic != base2->variadic)
762 return "incompatible variadic arguments";
763 PREPARE_PTR_LIST(target->arguments, arg1);
764 PREPARE_PTR_LIST(source->arguments, arg2);
765 i = 1;
766 for (;;) {
767 const char *diffstr;
768 diffstr = type_difference(arg1, arg2, 0, 0);
769 if (diffstr) {
770 static char argdiff[80];
771 sprintf(argdiff, "incompatible argument %d (%s)", i, diffstr);
772 return argdiff;
774 if (!arg1)
775 break;
776 NEXT_PTR_LIST(arg1);
777 NEXT_PTR_LIST(arg2);
778 i++;
780 FINISH_PTR_LIST(arg2);
781 FINISH_PTR_LIST(arg1);
784 target = base1;
785 source = base2;
787 return NULL;
790 static void bad_null(struct expression *expr)
792 if (Wnon_pointer_null)
793 warning(expr->pos, "Using plain integer as NULL pointer");
797 * Ignore differences in "volatile" and "const"ness when
798 * subtracting pointers
800 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
802 static struct symbol *evaluate_ptr_sub(struct expression *expr)
804 const char *typediff;
805 struct symbol *ctype;
806 struct symbol *ltype, *rtype;
807 struct expression *l = expr->left;
808 struct expression *r = expr->right;
810 ltype = degenerate(l);
811 rtype = degenerate(r);
813 ctype = ltype;
814 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
815 if (typediff)
816 expression_error(expr, "subtraction of different types can't work (%s)", typediff);
817 examine_symbol_type(ctype);
819 /* Figure out the base type we point to */
820 if (ctype->type == SYM_NODE)
821 ctype = ctype->ctype.base_type;
822 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
823 expression_error(expr, "subtraction of functions? Share your drugs");
824 return NULL;
826 ctype = get_base_type(ctype);
828 expr->ctype = ssize_t_ctype;
829 if (ctype->bit_size > bits_in_char) {
830 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
831 struct expression *div = expr;
832 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
833 unsigned long value = ctype->bit_size >> 3;
835 val->ctype = size_t_ctype;
836 val->value = value;
838 if (value & (value-1)) {
839 if (Wptr_subtraction_blows)
840 warning(expr->pos, "potentially expensive pointer subtraction");
843 sub->op = '-';
844 sub->ctype = ssize_t_ctype;
845 sub->left = l;
846 sub->right = r;
848 div->op = '/';
849 div->left = sub;
850 div->right = val;
853 return ssize_t_ctype;
856 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
858 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
860 struct symbol *ctype;
862 if (!expr)
863 return NULL;
865 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
866 warning(expr->pos, "assignment expression in conditional");
868 ctype = evaluate_expression(expr);
869 if (ctype) {
870 if (is_safe_type(ctype))
871 warning(expr->pos, "testing a 'safe expression'");
874 return ctype;
877 static struct symbol *evaluate_logical(struct expression *expr)
879 if (!evaluate_conditional(expr->left, 0))
880 return NULL;
881 if (!evaluate_conditional(expr->right, 0))
882 return NULL;
884 expr->ctype = &bool_ctype;
885 if (expr->flags) {
886 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
887 expr->flags = 0;
889 return &bool_ctype;
892 static struct symbol *evaluate_binop(struct expression *expr)
894 struct symbol *ltype, *rtype, *ctype;
895 int lclass = classify_type(expr->left->ctype, &ltype);
896 int rclass = classify_type(expr->right->ctype, &rtype);
897 int op = expr->op;
899 if (expr->flags) {
900 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
901 expr->flags = 0;
904 /* number op number */
905 if (lclass & rclass & TYPE_NUM) {
906 if ((lclass | rclass) & TYPE_FLOAT) {
907 switch (op) {
908 case '+': case '-': case '*': case '/':
909 break;
910 default:
911 return bad_expr_type(expr);
915 if (op == SPECIAL_LEFTSHIFT || op == SPECIAL_RIGHTSHIFT) {
916 // shifts do integer promotions, but that's it.
917 unrestrict(expr->left, lclass, &ltype);
918 unrestrict(expr->right, rclass, &rtype);
919 ctype = ltype = integer_promotion(ltype);
920 rtype = integer_promotion(rtype);
921 } else {
922 // The rest do usual conversions
923 ltype = usual_conversions(op, expr->left, expr->right,
924 lclass, rclass, ltype, rtype);
925 ctype = rtype = ltype;
928 expr->left = cast_to(expr->left, ltype);
929 expr->right = cast_to(expr->right, rtype);
930 expr->ctype = ctype;
931 return ctype;
934 /* pointer (+|-) integer */
935 if (lclass & TYPE_PTR && is_int(rclass) && (op == '+' || op == '-')) {
936 unrestrict(expr->right, rclass, &rtype);
937 return evaluate_ptr_add(expr, degenerate(expr->left), rtype);
940 /* integer + pointer */
941 if (rclass & TYPE_PTR && is_int(lclass) && op == '+') {
942 struct expression *index = expr->left;
943 unrestrict(index, lclass, &ltype);
944 expr->left = expr->right;
945 expr->right = index;
946 return evaluate_ptr_add(expr, degenerate(expr->left), ltype);
949 /* pointer - pointer */
950 if (lclass & rclass & TYPE_PTR && expr->op == '-')
951 return evaluate_ptr_sub(expr);
953 return bad_expr_type(expr);
956 static struct symbol *evaluate_comma(struct expression *expr)
958 expr->ctype = expr->right->ctype;
959 expr->flags &= expr->left->flags & expr->right->flags;
960 return expr->ctype;
963 static int modify_for_unsigned(int op)
965 if (op == '<')
966 op = SPECIAL_UNSIGNED_LT;
967 else if (op == '>')
968 op = SPECIAL_UNSIGNED_GT;
969 else if (op == SPECIAL_LTE)
970 op = SPECIAL_UNSIGNED_LTE;
971 else if (op == SPECIAL_GTE)
972 op = SPECIAL_UNSIGNED_GTE;
973 return op;
976 static inline int is_null_pointer_constant(struct expression *e)
978 if (e->ctype == &null_ctype)
979 return 1;
980 if (!(e->flags & Int_const_expr))
981 return 0;
982 return is_zero_constant(e) ? 2 : 0;
985 static struct symbol *evaluate_compare(struct expression *expr)
987 struct expression *left = expr->left, *right = expr->right;
988 struct symbol *ltype, *rtype;
989 int lclass = classify_type(degenerate(left), &ltype);
990 int rclass = classify_type(degenerate(right), &rtype);
991 struct symbol *ctype;
992 const char *typediff;
994 if (expr->flags) {
995 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
996 expr->flags = 0;
999 /* Type types? */
1000 if (is_type_type(ltype) && is_type_type(rtype))
1001 goto OK;
1003 if (is_safe_type(left->ctype) || is_safe_type(right->ctype))
1004 warning(expr->pos, "testing a 'safe expression'");
1006 /* number on number */
1007 if (lclass & rclass & TYPE_NUM) {
1008 ctype = usual_conversions(expr->op, expr->left, expr->right,
1009 lclass, rclass, ltype, rtype);
1010 expr->left = cast_to(expr->left, ctype);
1011 expr->right = cast_to(expr->right, ctype);
1012 if (ctype->ctype.modifiers & MOD_UNSIGNED)
1013 expr->op = modify_for_unsigned(expr->op);
1014 goto OK;
1017 /* at least one must be a pointer */
1018 if (!((lclass | rclass) & TYPE_PTR))
1019 return bad_expr_type(expr);
1021 /* equality comparisons can be with null pointer constants */
1022 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1023 int is_null1 = is_null_pointer_constant(left);
1024 int is_null2 = is_null_pointer_constant(right);
1025 if (is_null1 == 2)
1026 bad_null(left);
1027 if (is_null2 == 2)
1028 bad_null(right);
1029 if (is_null1 && is_null2) {
1030 int positive = expr->op == SPECIAL_EQUAL;
1031 expr->type = EXPR_VALUE;
1032 expr->value = positive;
1033 goto OK;
1035 if (is_null1) {
1036 left = cast_to(left, rtype);
1037 goto OK;
1039 if (is_null2) {
1040 right = cast_to(right, ltype);
1041 goto OK;
1043 /* they also have special treatment for pointers to void */
1044 if (lclass & rclass & TYPE_PTR) {
1045 if (get_base_type(ltype) == &void_ctype) {
1046 right = cast_to(right, ltype);
1047 goto OK;
1049 if (get_base_type(rtype) == &void_ctype) {
1050 left = cast_to(left, rtype);
1051 goto OK;
1055 /* both should be pointers */
1056 if (!(lclass & rclass & TYPE_PTR))
1057 return bad_expr_type(expr);
1059 expr->op = modify_for_unsigned(expr->op);
1060 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
1061 if (!typediff)
1062 goto OK;
1064 expression_error(expr, "incompatible types in comparison expression (%s)", typediff);
1065 return NULL;
1068 expr->ctype = &bool_ctype;
1069 return &bool_ctype;
1073 * NOTE! The degenerate case of "x ? : y", where we don't
1074 * have a true case, this will possibly promote "x" to the
1075 * same type as "y", and thus _change_ the conditional
1076 * test in the expression. But since promotion is "safe"
1077 * for testing, that's OK.
1079 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1081 struct expression **true;
1082 struct symbol *ctype, *ltype, *rtype, *lbase, *rbase;
1083 int lclass, rclass;
1084 const char * typediff;
1085 int qual;
1087 if (!evaluate_conditional(expr->conditional, 0))
1088 return NULL;
1089 if (!evaluate_expression(expr->cond_false))
1090 return NULL;
1092 ctype = degenerate(expr->conditional);
1093 rtype = degenerate(expr->cond_false);
1095 true = &expr->conditional;
1096 ltype = ctype;
1097 if (expr->cond_true) {
1098 if (!evaluate_expression(expr->cond_true))
1099 return NULL;
1100 ltype = degenerate(expr->cond_true);
1101 true = &expr->cond_true;
1104 if (expr->flags) {
1105 int flags = expr->conditional->flags & Int_const_expr;
1106 flags &= (*true)->flags & expr->cond_false->flags;
1107 if (!flags)
1108 expr->flags = 0;
1111 lclass = classify_type(ltype, &ltype);
1112 rclass = classify_type(rtype, &rtype);
1113 if (lclass & rclass & TYPE_NUM) {
1114 ctype = usual_conversions('?', *true, expr->cond_false,
1115 lclass, rclass, ltype, rtype);
1116 *true = cast_to(*true, ctype);
1117 expr->cond_false = cast_to(expr->cond_false, ctype);
1118 goto out;
1121 if ((lclass | rclass) & TYPE_PTR) {
1122 int is_null1 = is_null_pointer_constant(*true);
1123 int is_null2 = is_null_pointer_constant(expr->cond_false);
1125 if (is_null1 && is_null2) {
1126 *true = cast_to(*true, &ptr_ctype);
1127 expr->cond_false = cast_to(expr->cond_false, &ptr_ctype);
1128 ctype = &ptr_ctype;
1129 goto out;
1131 if (is_null1 && (rclass & TYPE_PTR)) {
1132 if (is_null1 == 2)
1133 bad_null(*true);
1134 *true = cast_to(*true, rtype);
1135 ctype = rtype;
1136 goto out;
1138 if (is_null2 && (lclass & TYPE_PTR)) {
1139 if (is_null2 == 2)
1140 bad_null(expr->cond_false);
1141 expr->cond_false = cast_to(expr->cond_false, ltype);
1142 ctype = ltype;
1143 goto out;
1145 if (!(lclass & rclass & TYPE_PTR)) {
1146 typediff = "different types";
1147 goto Err;
1149 /* OK, it's pointer on pointer */
1150 if (ltype->ctype.as != rtype->ctype.as) {
1151 typediff = "different address spaces";
1152 goto Err;
1155 /* need to be lazier here */
1156 lbase = get_base_type(ltype);
1157 rbase = get_base_type(rtype);
1158 qual = ltype->ctype.modifiers | rtype->ctype.modifiers;
1159 qual &= MOD_CONST | MOD_VOLATILE;
1161 if (lbase == &void_ctype) {
1162 /* XXX: pointers to function should warn here */
1163 ctype = ltype;
1164 goto Qual;
1167 if (rbase == &void_ctype) {
1168 /* XXX: pointers to function should warn here */
1169 ctype = rtype;
1170 goto Qual;
1172 /* XXX: that should be pointer to composite */
1173 ctype = ltype;
1174 typediff = type_difference(lbase, rbase, MOD_IGN, MOD_IGN);
1175 if (!typediff)
1176 goto Qual;
1177 goto Err;
1180 /* void on void, struct on same struct, union on same union */
1181 if (ltype == rtype) {
1182 ctype = ltype;
1183 goto out;
1185 typediff = "different base types";
1187 Err:
1188 expression_error(expr, "incompatible types in conditional expression (%s)", typediff);
1189 return NULL;
1191 out:
1192 expr->ctype = ctype;
1193 return ctype;
1195 Qual:
1196 if (qual & ~ctype->ctype.modifiers) {
1197 struct symbol *sym = alloc_symbol(ctype->pos, SYM_PTR);
1198 *sym = *ctype;
1199 sym->ctype.modifiers |= qual;
1200 ctype = sym;
1202 *true = cast_to(*true, ctype);
1203 expr->cond_false = cast_to(expr->cond_false, ctype);
1204 goto out;
1207 /* FP assignments can not do modulo or bit operations */
1208 static int compatible_float_op(int op)
1210 return op == SPECIAL_ADD_ASSIGN ||
1211 op == SPECIAL_SUB_ASSIGN ||
1212 op == SPECIAL_MUL_ASSIGN ||
1213 op == SPECIAL_DIV_ASSIGN;
1216 static int evaluate_assign_op(struct expression *expr)
1218 struct symbol *target = expr->left->ctype;
1219 struct symbol *source = expr->right->ctype;
1220 struct symbol *t, *s;
1221 int tclass = classify_type(target, &t);
1222 int sclass = classify_type(source, &s);
1223 int op = expr->op;
1225 if (tclass & sclass & TYPE_NUM) {
1226 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1227 expression_error(expr, "invalid assignment");
1228 return 0;
1230 if (tclass & TYPE_RESTRICT) {
1231 if (!restricted_binop(op, t)) {
1232 expression_error(expr, "bad restricted assignment");
1233 return 0;
1235 /* allowed assignments unfoul */
1236 if (sclass & TYPE_FOULED && s->ctype.base_type == t)
1237 goto Cast;
1238 if (!restricted_value(expr->right, t))
1239 return 1;
1240 } else if (!(sclass & TYPE_RESTRICT))
1241 goto Cast;
1242 /* source and target would better be identical restricted */
1243 if (t == s)
1244 return 1;
1245 warning(expr->pos, "invalid restricted assignment");
1246 expr->right = cast_to(expr->right, target);
1247 return 0;
1249 if (tclass & TYPE_PTR && is_int(sclass)) {
1250 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1251 unrestrict(expr->right, sclass, &s);
1252 evaluate_ptr_add(expr, target, s);
1253 return 1;
1255 expression_error(expr, "invalid pointer assignment");
1256 return 0;
1259 expression_error(expr, "invalid assignment");
1260 return 0;
1262 Cast:
1263 expr->right = cast_to(expr->right, target);
1264 return 1;
1267 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1268 struct expression **rp, const char *where)
1270 const char *typediff;
1271 struct symbol *source = degenerate(*rp);
1272 struct symbol *t, *s;
1273 int tclass = classify_type(target, &t);
1274 int sclass = classify_type(source, &s);
1276 if (tclass & sclass & TYPE_NUM) {
1277 if (tclass & TYPE_RESTRICT) {
1278 /* allowed assignments unfoul */
1279 if (sclass & TYPE_FOULED && s->ctype.base_type == t)
1280 goto Cast;
1281 if (!restricted_value(*rp, target))
1282 return 1;
1283 } else if (!(sclass & TYPE_RESTRICT))
1284 goto Cast;
1287 if (tclass & TYPE_PTR) {
1288 // NULL pointer is always OK
1289 int is_null = is_null_pointer_constant(*rp);
1290 if (is_null) {
1291 if (is_null == 2)
1292 bad_null(*rp);
1293 goto Cast;
1295 if (sclass & TYPE_PTR && t->ctype.as == s->ctype.as) {
1296 /* we should be more lazy here */
1297 int mod1 = t->ctype.modifiers;
1298 int mod2 = s->ctype.modifiers;
1299 s = get_base_type(s);
1300 t = get_base_type(t);
1303 * assignments to/from void * are OK, provided that
1304 * we do not remove qualifiers from pointed to [C]
1305 * or mix address spaces [sparse].
1307 if (!(mod2 & ~mod1 & (MOD_VOLATILE | MOD_CONST)))
1308 if (s == &void_ctype || t == &void_ctype)
1309 goto Cast;
1313 /* It's OK if the target is more volatile or const than the source */
1314 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
1315 if (!typediff)
1316 return 1;
1318 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1319 info(expr->pos, " expected %s", show_typename(target));
1320 info(expr->pos, " got %s", show_typename(source));
1321 *rp = cast_to(*rp, target);
1322 return 0;
1323 Cast:
1324 *rp = cast_to(*rp, target);
1325 return 1;
1328 static void mark_assigned(struct expression *expr)
1330 struct symbol *sym;
1332 if (!expr)
1333 return;
1334 switch (expr->type) {
1335 case EXPR_SYMBOL:
1336 sym = expr->symbol;
1337 if (!sym)
1338 return;
1339 if (sym->type != SYM_NODE)
1340 return;
1341 sym->ctype.modifiers |= MOD_ASSIGNED;
1342 return;
1344 case EXPR_BINOP:
1345 mark_assigned(expr->left);
1346 mark_assigned(expr->right);
1347 return;
1348 case EXPR_CAST:
1349 case EXPR_FORCE_CAST:
1350 mark_assigned(expr->cast_expression);
1351 return;
1352 case EXPR_SLICE:
1353 mark_assigned(expr->base);
1354 return;
1355 default:
1356 /* Hmm? */
1357 return;
1361 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1363 if (type->ctype.modifiers & MOD_CONST)
1364 expression_error(left, "assignment to const expression");
1366 /* We know left is an lvalue, so it's a "preop-*" */
1367 mark_assigned(left->unop);
1370 static struct symbol *evaluate_assignment(struct expression *expr)
1372 struct expression *left = expr->left;
1373 struct expression *where = expr;
1374 struct symbol *ltype;
1376 if (!lvalue_expression(left)) {
1377 expression_error(expr, "not an lvalue");
1378 return NULL;
1381 ltype = left->ctype;
1383 if (expr->op != '=') {
1384 if (!evaluate_assign_op(expr))
1385 return NULL;
1386 } else {
1387 if (!compatible_assignment_types(where, ltype, &expr->right, "assignment"))
1388 return NULL;
1391 evaluate_assign_to(left, ltype);
1393 expr->ctype = ltype;
1394 return ltype;
1397 static void examine_fn_arguments(struct symbol *fn)
1399 struct symbol *s;
1401 FOR_EACH_PTR(fn->arguments, s) {
1402 struct symbol *arg = evaluate_symbol(s);
1403 /* Array/function arguments silently degenerate into pointers */
1404 if (arg) {
1405 struct symbol *ptr;
1406 switch(arg->type) {
1407 case SYM_ARRAY:
1408 case SYM_FN:
1409 ptr = alloc_symbol(s->pos, SYM_PTR);
1410 if (arg->type == SYM_ARRAY)
1411 ptr->ctype = arg->ctype;
1412 else
1413 ptr->ctype.base_type = arg;
1414 ptr->ctype.as |= s->ctype.as;
1415 ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1417 s->ctype.base_type = ptr;
1418 s->ctype.as = 0;
1419 s->ctype.modifiers &= ~MOD_PTRINHERIT;
1420 s->bit_size = 0;
1421 s->examined = 0;
1422 examine_symbol_type(s);
1423 break;
1424 default:
1425 /* nothing */
1426 break;
1429 } END_FOR_EACH_PTR(s);
1432 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1434 /* Take the modifiers of the pointer, and apply them to the member */
1435 mod |= sym->ctype.modifiers;
1436 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1437 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1438 *newsym = *sym;
1439 newsym->ctype.as = as;
1440 newsym->ctype.modifiers = mod;
1441 sym = newsym;
1443 return sym;
1446 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1448 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1449 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1451 node->ctype.base_type = ptr;
1452 ptr->bit_size = bits_in_pointer;
1453 ptr->ctype.alignment = pointer_alignment;
1455 node->bit_size = bits_in_pointer;
1456 node->ctype.alignment = pointer_alignment;
1458 access_symbol(sym);
1459 if (sym->ctype.modifiers & MOD_REGISTER) {
1460 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1461 sym->ctype.modifiers &= ~MOD_REGISTER;
1463 if (sym->type == SYM_NODE) {
1464 ptr->ctype.as |= sym->ctype.as;
1465 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1466 sym = sym->ctype.base_type;
1468 if (degenerate && sym->type == SYM_ARRAY) {
1469 ptr->ctype.as |= sym->ctype.as;
1470 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1471 sym = sym->ctype.base_type;
1473 ptr->ctype.base_type = sym;
1475 return node;
1478 /* Arrays degenerate into pointers on pointer arithmetic */
1479 static struct symbol *degenerate(struct expression *expr)
1481 struct symbol *ctype, *base;
1483 if (!expr)
1484 return NULL;
1485 ctype = expr->ctype;
1486 if (!ctype)
1487 return NULL;
1488 base = examine_symbol_type(ctype);
1489 if (ctype->type == SYM_NODE)
1490 base = ctype->ctype.base_type;
1492 * Arrays degenerate into pointers to the entries, while
1493 * functions degenerate into pointers to themselves.
1494 * If array was part of non-lvalue compound, we create a copy
1495 * of that compound first and then act as if we were dealing with
1496 * the corresponding field in there.
1498 switch (base->type) {
1499 case SYM_ARRAY:
1500 if (expr->type == EXPR_SLICE) {
1501 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1502 struct expression *e0, *e1, *e2, *e3, *e4;
1504 a->ctype.base_type = expr->base->ctype;
1505 a->bit_size = expr->base->ctype->bit_size;
1506 a->array_size = expr->base->ctype->array_size;
1508 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1509 e0->symbol = a;
1510 e0->ctype = &lazy_ptr_ctype;
1512 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1513 e1->unop = e0;
1514 e1->op = '*';
1515 e1->ctype = expr->base->ctype; /* XXX */
1517 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1518 e2->left = e1;
1519 e2->right = expr->base;
1520 e2->op = '=';
1521 e2->ctype = expr->base->ctype;
1523 if (expr->r_bitpos) {
1524 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1525 e3->op = '+';
1526 e3->left = e0;
1527 e3->right = alloc_const_expression(expr->pos,
1528 expr->r_bitpos >> 3);
1529 e3->ctype = &lazy_ptr_ctype;
1530 } else {
1531 e3 = e0;
1534 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1535 e4->left = e2;
1536 e4->right = e3;
1537 e4->ctype = &lazy_ptr_ctype;
1539 expr->unop = e4;
1540 expr->type = EXPR_PREOP;
1541 expr->op = '*';
1543 case SYM_FN:
1544 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1545 expression_error(expr, "strange non-value function or array");
1546 return &bad_ctype;
1548 *expr = *expr->unop;
1549 ctype = create_pointer(expr, ctype, 1);
1550 expr->ctype = ctype;
1551 default:
1552 /* nothing */;
1554 return ctype;
1557 static struct symbol *evaluate_addressof(struct expression *expr)
1559 struct expression *op = expr->unop;
1560 struct symbol *ctype;
1562 if (op->op != '*' || op->type != EXPR_PREOP) {
1563 expression_error(expr, "not addressable");
1564 return NULL;
1566 ctype = op->ctype;
1567 *expr = *op->unop;
1568 expr->flags = 0;
1570 if (expr->type == EXPR_SYMBOL) {
1571 struct symbol *sym = expr->symbol;
1572 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1576 * symbol expression evaluation is lazy about the type
1577 * of the sub-expression, so we may have to generate
1578 * the type here if so..
1580 if (expr->ctype == &lazy_ptr_ctype) {
1581 ctype = create_pointer(expr, ctype, 0);
1582 expr->ctype = ctype;
1584 return expr->ctype;
1588 static struct symbol *evaluate_dereference(struct expression *expr)
1590 struct expression *op = expr->unop;
1591 struct symbol *ctype = op->ctype, *node, *target;
1593 /* Simplify: *&(expr) => (expr) */
1594 if (op->type == EXPR_PREOP && op->op == '&') {
1595 *expr = *op->unop;
1596 expr->flags = 0;
1597 return expr->ctype;
1600 /* Dereferencing a node drops all the node information. */
1601 if (ctype->type == SYM_NODE)
1602 ctype = ctype->ctype.base_type;
1604 node = alloc_symbol(expr->pos, SYM_NODE);
1605 target = ctype->ctype.base_type;
1607 switch (ctype->type) {
1608 default:
1609 expression_error(expr, "cannot dereference this type");
1610 return NULL;
1611 case SYM_PTR:
1612 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1613 merge_type(node, ctype);
1614 break;
1616 case SYM_ARRAY:
1617 if (!lvalue_expression(op)) {
1618 expression_error(op, "non-lvalue array??");
1619 return NULL;
1622 /* Do the implied "addressof" on the array */
1623 *op = *op->unop;
1626 * When an array is dereferenced, we need to pick
1627 * up the attributes of the original node too..
1629 merge_type(node, op->ctype);
1630 merge_type(node, ctype);
1631 break;
1634 node->bit_size = target->bit_size;
1635 node->array_size = target->array_size;
1637 expr->ctype = node;
1638 return node;
1642 * Unary post-ops: x++ and x--
1644 static struct symbol *evaluate_postop(struct expression *expr)
1646 struct expression *op = expr->unop;
1647 struct symbol *ctype = op->ctype;
1649 if (!lvalue_expression(expr->unop)) {
1650 expression_error(expr, "need lvalue expression for ++/--");
1651 return NULL;
1653 if (is_restricted_type(ctype) && restricted_unop(expr->op, &ctype)) {
1654 expression_error(expr, "bad operation on restricted");
1655 return NULL;
1656 } else if (is_fouled_type(ctype) && restricted_unop(expr->op, &ctype)) {
1657 expression_error(expr, "bad operation on restricted");
1658 return NULL;
1661 evaluate_assign_to(op, ctype);
1663 expr->ctype = ctype;
1664 expr->op_value = 1;
1665 if (is_ptr_type(ctype))
1666 expr->op_value = ptr_object_size(ctype) >> 3;
1668 return ctype;
1671 static struct symbol *evaluate_sign(struct expression *expr)
1673 struct symbol *ctype = expr->unop->ctype;
1674 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1675 expr->flags = 0;
1676 if (is_int_type(ctype)) {
1677 struct symbol *rtype = rtype = integer_promotion(ctype);
1678 expr->unop = cast_to(expr->unop, rtype);
1679 ctype = rtype;
1680 } else if (is_float_type(ctype) && expr->op != '~') {
1681 /* no conversions needed */
1682 } else if (is_restricted_type(ctype) && !restricted_unop(expr->op, &ctype)) {
1683 /* no conversions needed */
1684 } else if (is_fouled_type(ctype) && !restricted_unop(expr->op, &ctype)) {
1685 /* no conversions needed */
1686 } else {
1687 return bad_expr_type(expr);
1689 if (expr->op == '+')
1690 *expr = *expr->unop;
1691 expr->ctype = ctype;
1692 return ctype;
1695 static struct symbol *evaluate_preop(struct expression *expr)
1697 struct symbol *ctype = expr->unop->ctype;
1699 switch (expr->op) {
1700 case '(':
1701 *expr = *expr->unop;
1702 return ctype;
1704 case '+':
1705 case '-':
1706 case '~':
1707 return evaluate_sign(expr);
1709 case '*':
1710 return evaluate_dereference(expr);
1712 case '&':
1713 return evaluate_addressof(expr);
1715 case SPECIAL_INCREMENT:
1716 case SPECIAL_DECREMENT:
1718 * From a type evaluation standpoint the preops are
1719 * the same as the postops
1721 return evaluate_postop(expr);
1723 case '!':
1724 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1725 expr->flags = 0;
1726 if (is_safe_type(ctype))
1727 warning(expr->pos, "testing a 'safe expression'");
1728 if (is_float_type(ctype)) {
1729 struct expression *arg = expr->unop;
1730 expr->type = EXPR_BINOP;
1731 expr->op = SPECIAL_EQUAL;
1732 expr->left = arg;
1733 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1734 expr->right->ctype = ctype;
1735 expr->right->fvalue = 0;
1736 } else if (is_fouled_type(ctype)) {
1737 warning(expr->pos, "restricted degrades to integer");
1739 ctype = &bool_ctype;
1740 break;
1742 default:
1743 break;
1745 expr->ctype = ctype;
1746 return &bool_ctype;
1749 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1751 struct ptr_list *head = (struct ptr_list *)_list;
1752 struct ptr_list *list = head;
1754 if (!head)
1755 return NULL;
1756 do {
1757 int i;
1758 for (i = 0; i < list->nr; i++) {
1759 struct symbol *sym = (struct symbol *) list->list[i];
1760 if (sym->ident) {
1761 if (sym->ident != ident)
1762 continue;
1763 *offset = sym->offset;
1764 return sym;
1765 } else {
1766 struct symbol *ctype = sym->ctype.base_type;
1767 struct symbol *sub;
1768 if (!ctype)
1769 continue;
1770 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1771 continue;
1772 sub = find_identifier(ident, ctype->symbol_list, offset);
1773 if (!sub)
1774 continue;
1775 *offset += sym->offset;
1776 return sub;
1779 } while ((list = list->next) != head);
1780 return NULL;
1783 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1785 struct expression *add;
1788 * Create a new add-expression
1790 * NOTE! Even if we just add zero, we need a new node
1791 * for the member pointer, since it has a different
1792 * type than the original pointer. We could make that
1793 * be just a cast, but the fact is, a node is a node,
1794 * so we might as well just do the "add zero" here.
1796 add = alloc_expression(expr->pos, EXPR_BINOP);
1797 add->op = '+';
1798 add->left = expr;
1799 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1800 add->right->ctype = &int_ctype;
1801 add->right->value = offset;
1804 * The ctype of the pointer will be lazily evaluated if
1805 * we ever take the address of this member dereference..
1807 add->ctype = &lazy_ptr_ctype;
1808 return add;
1811 /* structure/union dereference */
1812 static struct symbol *evaluate_member_dereference(struct expression *expr)
1814 int offset;
1815 struct symbol *ctype, *member;
1816 struct expression *deref = expr->deref, *add;
1817 struct ident *ident = expr->member;
1818 unsigned int mod;
1819 int address_space;
1821 if (!evaluate_expression(deref))
1822 return NULL;
1823 if (!ident) {
1824 expression_error(expr, "bad member name");
1825 return NULL;
1828 ctype = deref->ctype;
1829 address_space = ctype->ctype.as;
1830 mod = ctype->ctype.modifiers;
1831 if (ctype->type == SYM_NODE) {
1832 ctype = ctype->ctype.base_type;
1833 address_space |= ctype->ctype.as;
1834 mod |= ctype->ctype.modifiers;
1836 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1837 expression_error(expr, "expected structure or union");
1838 return NULL;
1840 examine_symbol_type(ctype);
1841 offset = 0;
1842 member = find_identifier(ident, ctype->symbol_list, &offset);
1843 if (!member) {
1844 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1845 const char *name = "<unnamed>";
1846 int namelen = 9;
1847 if (ctype->ident) {
1848 name = ctype->ident->name;
1849 namelen = ctype->ident->len;
1851 if (ctype->symbol_list)
1852 expression_error(expr, "no member '%s' in %s %.*s",
1853 show_ident(ident), type, namelen, name);
1854 else
1855 expression_error(expr, "using member '%s' in "
1856 "incomplete %s %.*s", show_ident(ident),
1857 type, namelen, name);
1858 return NULL;
1862 * The member needs to take on the address space and modifiers of
1863 * the "parent" type.
1865 member = convert_to_as_mod(member, address_space, mod);
1866 ctype = get_base_type(member);
1868 if (!lvalue_expression(deref)) {
1869 if (deref->type != EXPR_SLICE) {
1870 expr->base = deref;
1871 expr->r_bitpos = 0;
1872 } else {
1873 expr->base = deref->base;
1874 expr->r_bitpos = deref->r_bitpos;
1876 expr->r_bitpos += offset << 3;
1877 expr->type = EXPR_SLICE;
1878 expr->r_nrbits = member->bit_size;
1879 expr->r_bitpos += member->bit_offset;
1880 expr->ctype = member;
1881 return member;
1884 deref = deref->unop;
1885 expr->deref = deref;
1887 add = evaluate_offset(deref, offset);
1888 expr->type = EXPR_PREOP;
1889 expr->op = '*';
1890 expr->unop = add;
1892 expr->ctype = member;
1893 return member;
1896 static int is_promoted(struct expression *expr)
1898 while (1) {
1899 switch (expr->type) {
1900 case EXPR_BINOP:
1901 case EXPR_SELECT:
1902 case EXPR_CONDITIONAL:
1903 return 1;
1904 case EXPR_COMMA:
1905 expr = expr->right;
1906 continue;
1907 case EXPR_PREOP:
1908 switch (expr->op) {
1909 case '(':
1910 expr = expr->unop;
1911 continue;
1912 case '+':
1913 case '-':
1914 case '~':
1915 return 1;
1916 default:
1917 return 0;
1919 default:
1920 return 0;
1926 static struct symbol *evaluate_cast(struct expression *);
1928 static struct symbol *evaluate_type_information(struct expression *expr)
1930 struct symbol *sym = expr->cast_type;
1931 if (!sym) {
1932 sym = evaluate_expression(expr->cast_expression);
1933 if (!sym)
1934 return NULL;
1936 * Expressions of restricted types will possibly get
1937 * promoted - check that here
1939 if (is_restricted_type(sym)) {
1940 if (sym->bit_size < bits_in_int && is_promoted(expr))
1941 sym = &int_ctype;
1942 } else if (is_fouled_type(sym)) {
1943 sym = &int_ctype;
1946 examine_symbol_type(sym);
1947 if (is_bitfield_type(sym)) {
1948 expression_error(expr, "trying to examine bitfield type");
1949 return NULL;
1951 return sym;
1954 static struct symbol *evaluate_sizeof(struct expression *expr)
1956 struct symbol *type;
1957 int size;
1959 type = evaluate_type_information(expr);
1960 if (!type)
1961 return NULL;
1963 size = type->bit_size;
1964 if ((size < 0) || (size & 7))
1965 expression_error(expr, "cannot size expression");
1966 expr->type = EXPR_VALUE;
1967 expr->value = size >> 3;
1968 expr->taint = 0;
1969 expr->ctype = size_t_ctype;
1970 return size_t_ctype;
1973 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
1975 struct symbol *type;
1976 int size;
1978 type = evaluate_type_information(expr);
1979 if (!type)
1980 return NULL;
1982 if (type->type == SYM_NODE)
1983 type = type->ctype.base_type;
1984 if (!type)
1985 return NULL;
1986 switch (type->type) {
1987 case SYM_ARRAY:
1988 break;
1989 case SYM_PTR:
1990 type = get_base_type(type);
1991 if (type)
1992 break;
1993 default:
1994 expression_error(expr, "expected pointer expression");
1995 return NULL;
1997 size = type->bit_size;
1998 if (size & 7)
1999 size = 0;
2000 expr->type = EXPR_VALUE;
2001 expr->value = size >> 3;
2002 expr->taint = 0;
2003 expr->ctype = size_t_ctype;
2004 return size_t_ctype;
2007 static struct symbol *evaluate_alignof(struct expression *expr)
2009 struct symbol *type;
2011 type = evaluate_type_information(expr);
2012 if (!type)
2013 return NULL;
2015 expr->type = EXPR_VALUE;
2016 expr->value = type->ctype.alignment;
2017 expr->taint = 0;
2018 expr->ctype = size_t_ctype;
2019 return size_t_ctype;
2022 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
2024 struct expression *expr;
2025 struct symbol_list *argument_types = fn->arguments;
2026 struct symbol *argtype;
2027 int i = 1;
2029 PREPARE_PTR_LIST(argument_types, argtype);
2030 FOR_EACH_PTR (head, expr) {
2031 struct expression **p = THIS_ADDRESS(expr);
2032 struct symbol *ctype, *target;
2033 ctype = evaluate_expression(expr);
2035 if (!ctype)
2036 return 0;
2038 target = argtype;
2039 if (!target) {
2040 struct symbol *type;
2041 int class = classify_type(ctype, &type);
2042 if (is_int(class)) {
2043 *p = cast_to(expr, integer_promotion(type));
2044 } else if (class & TYPE_FLOAT) {
2045 unsigned long mod = type->ctype.modifiers;
2046 if (!(mod & (MOD_LONG|MOD_LONGLONG)))
2047 *p = cast_to(expr, &double_ctype);
2048 } else if (class & TYPE_PTR) {
2049 if (expr->ctype == &null_ctype)
2050 *p = cast_to(expr, &ptr_ctype);
2051 else
2052 degenerate(expr);
2054 } else {
2055 static char where[30];
2056 examine_symbol_type(target);
2057 sprintf(where, "argument %d", i);
2058 compatible_assignment_types(expr, target, p, where);
2061 i++;
2062 NEXT_PTR_LIST(argtype);
2063 } END_FOR_EACH_PTR(expr);
2064 FINISH_PTR_LIST(argtype);
2065 return 1;
2068 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
2070 struct symbol *sym;
2072 FOR_EACH_PTR(ctype->symbol_list, sym) {
2073 if (sym->ident == ident)
2074 return sym;
2075 } END_FOR_EACH_PTR(sym);
2076 return NULL;
2079 static void convert_index(struct expression *e)
2081 struct expression *child = e->idx_expression;
2082 unsigned from = e->idx_from;
2083 unsigned to = e->idx_to + 1;
2084 e->type = EXPR_POS;
2085 e->init_offset = from * (e->ctype->bit_size>>3);
2086 e->init_nr = to - from;
2087 e->init_expr = child;
2090 static void convert_ident(struct expression *e)
2092 struct expression *child = e->ident_expression;
2093 struct symbol *sym = e->field;
2094 e->type = EXPR_POS;
2095 e->init_offset = sym->offset;
2096 e->init_nr = 1;
2097 e->init_expr = child;
2100 static void convert_designators(struct expression *e)
2102 while (e) {
2103 if (e->type == EXPR_INDEX)
2104 convert_index(e);
2105 else if (e->type == EXPR_IDENTIFIER)
2106 convert_ident(e);
2107 else
2108 break;
2109 e = e->init_expr;
2113 static void excess(struct expression *e, const char *s)
2115 warning(e->pos, "excessive elements in %s initializer", s);
2119 * implicit designator for the first element
2121 static struct expression *first_subobject(struct symbol *ctype, int class,
2122 struct expression **v)
2124 struct expression *e = *v, *new;
2126 if (ctype->type == SYM_NODE)
2127 ctype = ctype->ctype.base_type;
2129 if (class & TYPE_PTR) { /* array */
2130 if (!ctype->bit_size)
2131 return NULL;
2132 new = alloc_expression(e->pos, EXPR_INDEX);
2133 new->idx_expression = e;
2134 new->ctype = ctype->ctype.base_type;
2135 } else {
2136 struct symbol *field, *p;
2137 PREPARE_PTR_LIST(ctype->symbol_list, p);
2138 while (p && !p->ident && is_bitfield_type(p))
2139 NEXT_PTR_LIST(p);
2140 field = p;
2141 FINISH_PTR_LIST(p);
2142 if (!field)
2143 return NULL;
2144 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2145 new->ident_expression = e;
2146 new->field = new->ctype = field;
2148 *v = new;
2149 return new;
2153 * sanity-check explicit designators; return the innermost one or NULL
2154 * in case of error. Assign types.
2156 static struct expression *check_designators(struct expression *e,
2157 struct symbol *ctype)
2159 struct expression *last = NULL;
2160 const char *err;
2161 while (1) {
2162 if (ctype->type == SYM_NODE)
2163 ctype = ctype->ctype.base_type;
2164 if (e->type == EXPR_INDEX) {
2165 struct symbol *type;
2166 if (ctype->type != SYM_ARRAY) {
2167 err = "array index in non-array";
2168 break;
2170 type = ctype->ctype.base_type;
2171 if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2172 unsigned offset = e->idx_to * type->bit_size;
2173 if (offset >= ctype->bit_size) {
2174 err = "index out of bounds in";
2175 break;
2178 e->ctype = ctype = type;
2179 ctype = type;
2180 last = e;
2181 if (!e->idx_expression) {
2182 err = "invalid";
2183 break;
2185 e = e->idx_expression;
2186 } else if (e->type == EXPR_IDENTIFIER) {
2187 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2188 err = "field name not in struct or union";
2189 break;
2191 ctype = find_struct_ident(ctype, e->expr_ident);
2192 if (!ctype) {
2193 err = "unknown field name in";
2194 break;
2196 e->field = e->ctype = ctype;
2197 last = e;
2198 if (!e->ident_expression) {
2199 err = "invalid";
2200 break;
2202 e = e->ident_expression;
2203 } else if (e->type == EXPR_POS) {
2204 err = "internal front-end error: EXPR_POS in";
2205 break;
2206 } else
2207 return last;
2209 expression_error(e, "%s initializer", err);
2210 return NULL;
2214 * choose the next subobject to initialize.
2216 * Get designators for next element, switch old ones to EXPR_POS.
2217 * Return the resulting expression or NULL if we'd run out of subobjects.
2218 * The innermost designator is returned in *v. Designators in old
2219 * are assumed to be already sanity-checked.
2221 static struct expression *next_designators(struct expression *old,
2222 struct symbol *ctype,
2223 struct expression *e, struct expression **v)
2225 struct expression *new = NULL;
2227 if (!old)
2228 return NULL;
2229 if (old->type == EXPR_INDEX) {
2230 struct expression *copy;
2231 unsigned n;
2233 copy = next_designators(old->idx_expression,
2234 old->ctype, e, v);
2235 if (!copy) {
2236 n = old->idx_to + 1;
2237 if (n * old->ctype->bit_size == ctype->bit_size) {
2238 convert_index(old);
2239 return NULL;
2241 copy = e;
2242 *v = new = alloc_expression(e->pos, EXPR_INDEX);
2243 } else {
2244 n = old->idx_to;
2245 new = alloc_expression(e->pos, EXPR_INDEX);
2248 new->idx_from = new->idx_to = n;
2249 new->idx_expression = copy;
2250 new->ctype = old->ctype;
2251 convert_index(old);
2252 } else if (old->type == EXPR_IDENTIFIER) {
2253 struct expression *copy;
2254 struct symbol *field;
2256 copy = next_designators(old->ident_expression,
2257 old->ctype, e, v);
2258 if (!copy) {
2259 field = old->field->next_subobject;
2260 if (!field) {
2261 convert_ident(old);
2262 return NULL;
2264 copy = e;
2265 *v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2266 } else {
2267 field = old->field;
2268 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2271 new->field = field;
2272 new->expr_ident = field->ident;
2273 new->ident_expression = copy;
2274 new->ctype = field;
2275 convert_ident(old);
2277 return new;
2280 static int handle_simple_initializer(struct expression **ep, int nested,
2281 int class, struct symbol *ctype);
2284 * deal with traversing subobjects [6.7.8(17,18,20)]
2286 static void handle_list_initializer(struct expression *expr,
2287 int class, struct symbol *ctype)
2289 struct expression *e, *last = NULL, *top = NULL, *next;
2290 int jumped = 0;
2292 FOR_EACH_PTR(expr->expr_list, e) {
2293 struct expression **v;
2294 struct symbol *type;
2295 int lclass;
2297 if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2298 if (!top) {
2299 top = e;
2300 last = first_subobject(ctype, class, &top);
2301 } else {
2302 last = next_designators(last, ctype, e, &top);
2304 if (!last) {
2305 excess(e, class & TYPE_PTR ? "array" :
2306 "struct or union");
2307 DELETE_CURRENT_PTR(e);
2308 continue;
2310 if (jumped) {
2311 warning(e->pos, "advancing past deep designator");
2312 jumped = 0;
2314 REPLACE_CURRENT_PTR(e, last);
2315 } else {
2316 next = check_designators(e, ctype);
2317 if (!next) {
2318 DELETE_CURRENT_PTR(e);
2319 continue;
2321 top = next;
2322 /* deeper than one designator? */
2323 jumped = top != e;
2324 convert_designators(last);
2325 last = e;
2328 found:
2329 lclass = classify_type(top->ctype, &type);
2330 if (top->type == EXPR_INDEX)
2331 v = &top->idx_expression;
2332 else
2333 v = &top->ident_expression;
2335 if (handle_simple_initializer(v, 1, lclass, top->ctype))
2336 continue;
2338 if (!(lclass & TYPE_COMPOUND)) {
2339 warning(e->pos, "bogus scalar initializer");
2340 DELETE_CURRENT_PTR(e);
2341 continue;
2344 next = first_subobject(type, lclass, v);
2345 if (next) {
2346 warning(e->pos, "missing braces around initializer");
2347 top = next;
2348 goto found;
2351 DELETE_CURRENT_PTR(e);
2352 excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2354 } END_FOR_EACH_PTR(e);
2356 convert_designators(last);
2357 expr->ctype = ctype;
2360 static int is_string_literal(struct expression **v)
2362 struct expression *e = *v;
2363 while (e && e->type == EXPR_PREOP && e->op == '(')
2364 e = e->unop;
2365 if (!e || e->type != EXPR_STRING)
2366 return 0;
2367 if (e != *v && Wparen_string)
2368 warning(e->pos,
2369 "array initialized from parenthesized string constant");
2370 *v = e;
2371 return 1;
2375 * We want a normal expression, possibly in one layer of braces. Warn
2376 * if the latter happens inside a list (it's legal, but likely to be
2377 * an effect of screwup). In case of anything not legal, we are definitely
2378 * having an effect of screwup, so just fail and let the caller warn.
2380 static struct expression *handle_scalar(struct expression *e, int nested)
2382 struct expression *v = NULL, *p;
2383 int count = 0;
2385 /* normal case */
2386 if (e->type != EXPR_INITIALIZER)
2387 return e;
2389 FOR_EACH_PTR(e->expr_list, p) {
2390 if (!v)
2391 v = p;
2392 count++;
2393 } END_FOR_EACH_PTR(p);
2394 if (count != 1)
2395 return NULL;
2396 switch(v->type) {
2397 case EXPR_INITIALIZER:
2398 case EXPR_INDEX:
2399 case EXPR_IDENTIFIER:
2400 return NULL;
2401 default:
2402 break;
2404 if (nested)
2405 warning(e->pos, "braces around scalar initializer");
2406 return v;
2410 * deal with the cases that don't care about subobjects:
2411 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2412 * character array <- string literal, possibly in braces [6.7.8(14)]
2413 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2414 * compound type <- initializer list in braces [6.7.8(16)]
2415 * The last one punts to handle_list_initializer() which, in turn will call
2416 * us for individual elements of the list.
2418 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2419 * the lack of support of wide char stuff in general.
2421 * One note: we need to take care not to evaluate a string literal until
2422 * we know that we *will* handle it right here. Otherwise we would screw
2423 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2424 * { "string", ...} - we need to preserve that string literal recognizable
2425 * until we dig into the inner struct.
2427 static int handle_simple_initializer(struct expression **ep, int nested,
2428 int class, struct symbol *ctype)
2430 int is_string = is_string_type(ctype);
2431 struct expression *e = *ep, *p;
2432 struct symbol *type;
2434 if (!e)
2435 return 0;
2437 /* scalar */
2438 if (!(class & TYPE_COMPOUND)) {
2439 e = handle_scalar(e, nested);
2440 if (!e)
2441 return 0;
2442 *ep = e;
2443 if (!evaluate_expression(e))
2444 return 1;
2445 compatible_assignment_types(e, ctype, ep, "initializer");
2446 return 1;
2450 * sublist; either a string, or we dig in; the latter will deal with
2451 * pathologies, so we don't need anything fancy here.
2453 if (e->type == EXPR_INITIALIZER) {
2454 if (is_string) {
2455 struct expression *v = NULL;
2456 int count = 0;
2458 FOR_EACH_PTR(e->expr_list, p) {
2459 if (!v)
2460 v = p;
2461 count++;
2462 } END_FOR_EACH_PTR(p);
2463 if (count == 1 && is_string_literal(&v)) {
2464 *ep = e = v;
2465 goto String;
2468 handle_list_initializer(e, class, ctype);
2469 return 1;
2472 /* string */
2473 if (is_string_literal(&e)) {
2474 /* either we are doing array of char, or we'll have to dig in */
2475 if (is_string) {
2476 *ep = e;
2477 goto String;
2479 return 0;
2481 /* struct or union can be initialized by compatible */
2482 if (class != TYPE_COMPOUND)
2483 return 0;
2484 type = evaluate_expression(e);
2485 if (!type)
2486 return 0;
2487 if (ctype->type == SYM_NODE)
2488 ctype = ctype->ctype.base_type;
2489 if (type->type == SYM_NODE)
2490 type = type->ctype.base_type;
2491 if (ctype == type)
2492 return 1;
2493 return 0;
2495 String:
2496 p = alloc_expression(e->pos, EXPR_STRING);
2497 *p = *e;
2498 type = evaluate_expression(p);
2499 if (ctype->bit_size != -1 &&
2500 ctype->bit_size + bits_in_char < type->bit_size) {
2501 warning(e->pos,
2502 "too long initializer-string for array of char");
2504 *ep = p;
2505 return 1;
2508 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2510 struct symbol *type;
2511 int class = classify_type(ctype, &type);
2512 if (!handle_simple_initializer(ep, 0, class, ctype))
2513 expression_error(*ep, "invalid initializer");
2516 static struct symbol *evaluate_cast(struct expression *expr)
2518 struct expression *target = expr->cast_expression;
2519 struct symbol *ctype;
2520 struct symbol *t1, *t2;
2521 int class1, class2;
2522 int as1 = 0, as2 = 0;
2524 if (!target)
2525 return NULL;
2528 * Special case: a cast can be followed by an
2529 * initializer, in which case we need to pass
2530 * the type value down to that initializer rather
2531 * than trying to evaluate it as an expression
2533 * A more complex case is when the initializer is
2534 * dereferenced as part of a post-fix expression.
2535 * We need to produce an expression that can be dereferenced.
2537 if (target->type == EXPR_INITIALIZER) {
2538 struct symbol *sym = expr->cast_type;
2539 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2541 sym->initializer = target;
2542 evaluate_symbol(sym);
2544 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2545 addr->symbol = sym;
2547 expr->type = EXPR_PREOP;
2548 expr->op = '*';
2549 expr->unop = addr;
2550 expr->ctype = sym;
2552 return sym;
2555 ctype = examine_symbol_type(expr->cast_type);
2556 expr->ctype = ctype;
2557 expr->cast_type = ctype;
2559 evaluate_expression(target);
2560 degenerate(target);
2562 class1 = classify_type(ctype, &t1);
2564 /* cast to non-integer type -> not an integer constant expression */
2565 if (!is_int(class1))
2566 expr->flags = 0;
2567 /* if argument turns out to be not an integer constant expression *and*
2568 it was not a floating literal to start with -> too bad */
2569 else if (expr->flags == Int_const_expr &&
2570 !(target->flags & Int_const_expr))
2571 expr->flags = 0;
2573 * You can always throw a value away by casting to
2574 * "void" - that's an implicit "force". Note that
2575 * the same is _not_ true of "void *".
2577 if (t1 == &void_ctype)
2578 goto out;
2580 if (class1 & TYPE_COMPOUND)
2581 warning(expr->pos, "cast to non-scalar");
2583 if (class1 == TYPE_PTR)
2584 get_base_type(t1);
2586 t2 = target->ctype;
2587 if (!t2) {
2588 expression_error(expr, "cast from unknown type");
2589 goto out;
2591 class2 = classify_type(t2, &t2);
2593 if (class2 & TYPE_COMPOUND)
2594 warning(expr->pos, "cast from non-scalar");
2596 if (expr->type == EXPR_FORCE_CAST)
2597 goto out;
2599 /* allowed cast unfouls */
2600 if (class2 & TYPE_FOULED)
2601 t2 = t2->ctype.base_type;
2603 if (t1 != t2) {
2604 if (class1 & TYPE_RESTRICT)
2605 warning(expr->pos, "cast to restricted type");
2606 if (class2 & TYPE_RESTRICT)
2607 warning(expr->pos, "cast from restricted type");
2610 if (t1 == &ulong_ctype)
2611 as1 = -1;
2612 else if (class1 == TYPE_PTR)
2613 as1 = t1->ctype.as;
2615 if (t2 == &ulong_ctype)
2616 as2 = -1;
2617 else if (class2 == TYPE_PTR)
2618 as2 = t2->ctype.as;
2620 if (!as1 && as2 > 0)
2621 warning(expr->pos, "cast removes address space of expression");
2622 if (as1 > 0 && as2 > 0 && as1 != as2)
2623 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2624 if (as1 > 0 && !as2 &&
2625 !is_null_pointer_constant(target) && Wcast_to_address_space)
2626 warning(expr->pos,
2627 "cast adds address space to expression (<asn:%d>)", as1);
2629 if (!(t1->ctype.modifiers & MOD_PTRINHERIT) && class1 == TYPE_PTR &&
2630 !as1 && (target->flags & Int_const_expr)) {
2631 if (t1->ctype.base_type == &void_ctype) {
2632 if (is_zero_constant(target)) {
2633 /* NULL */
2634 expr->type = EXPR_VALUE;
2635 expr->ctype = &null_ctype;
2636 expr->value = 0;
2637 return ctype;
2641 out:
2642 return ctype;
2646 * Evaluate a call expression with a symbol. This
2647 * should expand inline functions, and evaluate
2648 * builtins.
2650 static int evaluate_symbol_call(struct expression *expr)
2652 struct expression *fn = expr->fn;
2653 struct symbol *ctype = fn->ctype;
2655 if (fn->type != EXPR_PREOP)
2656 return 0;
2658 if (ctype->op && ctype->op->evaluate)
2659 return ctype->op->evaluate(expr);
2661 if (ctype->ctype.modifiers & MOD_INLINE) {
2662 int ret;
2663 struct symbol *curr = current_fn;
2664 current_fn = ctype->ctype.base_type;
2666 ret = inline_function(expr, ctype);
2668 /* restore the old function */
2669 current_fn = curr;
2670 return ret;
2673 return 0;
2676 static struct symbol *evaluate_call(struct expression *expr)
2678 int args, fnargs;
2679 struct symbol *ctype, *sym;
2680 struct expression *fn = expr->fn;
2681 struct expression_list *arglist = expr->args;
2683 if (!evaluate_expression(fn))
2684 return NULL;
2685 sym = ctype = fn->ctype;
2686 if (ctype->type == SYM_NODE)
2687 ctype = ctype->ctype.base_type;
2688 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
2689 ctype = get_base_type(ctype);
2691 examine_fn_arguments(ctype);
2692 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
2693 sym->op && sym->op->args) {
2694 if (!sym->op->args(expr))
2695 return NULL;
2696 } else {
2697 if (!evaluate_arguments(sym, ctype, arglist))
2698 return NULL;
2699 if (ctype->type != SYM_FN) {
2700 expression_error(expr, "not a function %s",
2701 show_ident(sym->ident));
2702 return NULL;
2704 args = expression_list_size(expr->args);
2705 fnargs = symbol_list_size(ctype->arguments);
2706 if (args < fnargs)
2707 expression_error(expr,
2708 "not enough arguments for function %s",
2709 show_ident(sym->ident));
2710 if (args > fnargs && !ctype->variadic)
2711 expression_error(expr,
2712 "too many arguments for function %s",
2713 show_ident(sym->ident));
2715 if (sym->type == SYM_NODE) {
2716 if (evaluate_symbol_call(expr))
2717 return expr->ctype;
2719 expr->ctype = ctype->ctype.base_type;
2720 return expr->ctype;
2723 static struct symbol *evaluate_offsetof(struct expression *expr)
2725 struct expression *e = expr->down;
2726 struct symbol *ctype = expr->in;
2727 int class;
2729 if (expr->op == '.') {
2730 struct symbol *field;
2731 int offset = 0;
2732 if (!ctype) {
2733 expression_error(expr, "expected structure or union");
2734 return NULL;
2736 examine_symbol_type(ctype);
2737 class = classify_type(ctype, &ctype);
2738 if (class != TYPE_COMPOUND) {
2739 expression_error(expr, "expected structure or union");
2740 return NULL;
2743 field = find_identifier(expr->ident, ctype->symbol_list, &offset);
2744 if (!field) {
2745 expression_error(expr, "unknown member");
2746 return NULL;
2748 ctype = field;
2749 expr->type = EXPR_VALUE;
2750 expr->flags = Int_const_expr;
2751 expr->value = offset;
2752 expr->taint = 0;
2753 expr->ctype = size_t_ctype;
2754 } else {
2755 if (!ctype) {
2756 expression_error(expr, "expected structure or union");
2757 return NULL;
2759 examine_symbol_type(ctype);
2760 class = classify_type(ctype, &ctype);
2761 if (class != (TYPE_COMPOUND | TYPE_PTR)) {
2762 expression_error(expr, "expected array");
2763 return NULL;
2765 ctype = ctype->ctype.base_type;
2766 if (!expr->index) {
2767 expr->type = EXPR_VALUE;
2768 expr->flags = Int_const_expr;
2769 expr->value = 0;
2770 expr->taint = 0;
2771 expr->ctype = size_t_ctype;
2772 } else {
2773 struct expression *idx = expr->index, *m;
2774 struct symbol *i_type = evaluate_expression(idx);
2775 int i_class = classify_type(i_type, &i_type);
2776 if (!is_int(i_class)) {
2777 expression_error(expr, "non-integer index");
2778 return NULL;
2780 unrestrict(idx, i_class, &i_type);
2781 idx = cast_to(idx, size_t_ctype);
2782 m = alloc_const_expression(expr->pos,
2783 ctype->bit_size >> 3);
2784 m->ctype = size_t_ctype;
2785 m->flags = Int_const_expr;
2786 expr->type = EXPR_BINOP;
2787 expr->left = idx;
2788 expr->right = m;
2789 expr->op = '*';
2790 expr->ctype = size_t_ctype;
2791 expr->flags = m->flags & idx->flags & Int_const_expr;
2794 if (e) {
2795 struct expression *copy = __alloc_expression(0);
2796 *copy = *expr;
2797 if (e->type == EXPR_OFFSETOF)
2798 e->in = ctype;
2799 if (!evaluate_expression(e))
2800 return NULL;
2801 expr->type = EXPR_BINOP;
2802 expr->flags = e->flags & copy->flags & Int_const_expr;
2803 expr->op = '+';
2804 expr->ctype = size_t_ctype;
2805 expr->left = copy;
2806 expr->right = e;
2808 return size_t_ctype;
2811 struct symbol *evaluate_expression(struct expression *expr)
2813 if (!expr)
2814 return NULL;
2815 if (expr->ctype)
2816 return expr->ctype;
2818 switch (expr->type) {
2819 case EXPR_VALUE:
2820 case EXPR_FVALUE:
2821 expression_error(expr, "value expression without a type");
2822 return NULL;
2823 case EXPR_STRING:
2824 return evaluate_string(expr);
2825 case EXPR_SYMBOL:
2826 return evaluate_symbol_expression(expr);
2827 case EXPR_BINOP:
2828 if (!evaluate_expression(expr->left))
2829 return NULL;
2830 if (!evaluate_expression(expr->right))
2831 return NULL;
2832 return evaluate_binop(expr);
2833 case EXPR_LOGICAL:
2834 return evaluate_logical(expr);
2835 case EXPR_COMMA:
2836 evaluate_expression(expr->left);
2837 if (!evaluate_expression(expr->right))
2838 return NULL;
2839 return evaluate_comma(expr);
2840 case EXPR_COMPARE:
2841 if (!evaluate_expression(expr->left))
2842 return NULL;
2843 if (!evaluate_expression(expr->right))
2844 return NULL;
2845 return evaluate_compare(expr);
2846 case EXPR_ASSIGNMENT:
2847 if (!evaluate_expression(expr->left))
2848 return NULL;
2849 if (!evaluate_expression(expr->right))
2850 return NULL;
2851 return evaluate_assignment(expr);
2852 case EXPR_PREOP:
2853 if (!evaluate_expression(expr->unop))
2854 return NULL;
2855 return evaluate_preop(expr);
2856 case EXPR_POSTOP:
2857 if (!evaluate_expression(expr->unop))
2858 return NULL;
2859 return evaluate_postop(expr);
2860 case EXPR_CAST:
2861 case EXPR_FORCE_CAST:
2862 case EXPR_IMPLIED_CAST:
2863 return evaluate_cast(expr);
2864 case EXPR_SIZEOF:
2865 return evaluate_sizeof(expr);
2866 case EXPR_PTRSIZEOF:
2867 return evaluate_ptrsizeof(expr);
2868 case EXPR_ALIGNOF:
2869 return evaluate_alignof(expr);
2870 case EXPR_DEREF:
2871 return evaluate_member_dereference(expr);
2872 case EXPR_CALL:
2873 return evaluate_call(expr);
2874 case EXPR_SELECT:
2875 case EXPR_CONDITIONAL:
2876 return evaluate_conditional_expression(expr);
2877 case EXPR_STATEMENT:
2878 expr->ctype = evaluate_statement(expr->statement);
2879 return expr->ctype;
2881 case EXPR_LABEL:
2882 expr->ctype = &ptr_ctype;
2883 return &ptr_ctype;
2885 case EXPR_TYPE:
2886 /* Evaluate the type of the symbol .. */
2887 evaluate_symbol(expr->symbol);
2888 /* .. but the type of the _expression_ is a "type" */
2889 expr->ctype = &type_ctype;
2890 return &type_ctype;
2892 case EXPR_OFFSETOF:
2893 return evaluate_offsetof(expr);
2895 /* These can not exist as stand-alone expressions */
2896 case EXPR_INITIALIZER:
2897 case EXPR_IDENTIFIER:
2898 case EXPR_INDEX:
2899 case EXPR_POS:
2900 expression_error(expr, "internal front-end error: initializer in expression");
2901 return NULL;
2902 case EXPR_SLICE:
2903 expression_error(expr, "internal front-end error: SLICE re-evaluated");
2904 return NULL;
2906 return NULL;
2909 static void check_duplicates(struct symbol *sym)
2911 int declared = 0;
2912 struct symbol *next = sym;
2914 while ((next = next->same_symbol) != NULL) {
2915 const char *typediff;
2916 evaluate_symbol(next);
2917 declared++;
2918 typediff = type_difference(sym, next, 0, 0);
2919 if (typediff) {
2920 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2921 show_ident(sym->ident),
2922 stream_name(next->pos.stream), next->pos.line, typediff);
2923 return;
2926 if (!declared) {
2927 unsigned long mod = sym->ctype.modifiers;
2928 if (mod & (MOD_STATIC | MOD_REGISTER))
2929 return;
2930 if (!(mod & MOD_TOPLEVEL))
2931 return;
2932 if (!Wdecl)
2933 return;
2934 if (sym->ident == &main_ident)
2935 return;
2936 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
2940 static struct symbol *evaluate_symbol(struct symbol *sym)
2942 struct symbol *base_type;
2944 if (!sym)
2945 return sym;
2946 if (sym->evaluated)
2947 return sym;
2948 sym->evaluated = 1;
2950 sym = examine_symbol_type(sym);
2951 base_type = get_base_type(sym);
2952 if (!base_type)
2953 return NULL;
2955 /* Evaluate the initializers */
2956 if (sym->initializer)
2957 evaluate_initializer(sym, &sym->initializer);
2959 /* And finally, evaluate the body of the symbol too */
2960 if (base_type->type == SYM_FN) {
2961 struct symbol *curr = current_fn;
2963 current_fn = base_type;
2965 examine_fn_arguments(base_type);
2966 if (!base_type->stmt && base_type->inline_stmt)
2967 uninline(sym);
2968 if (base_type->stmt)
2969 evaluate_statement(base_type->stmt);
2971 current_fn = curr;
2974 return base_type;
2977 void evaluate_symbol_list(struct symbol_list *list)
2979 struct symbol *sym;
2981 FOR_EACH_PTR(list, sym) {
2982 evaluate_symbol(sym);
2983 check_duplicates(sym);
2984 } END_FOR_EACH_PTR(sym);
2987 static struct symbol *evaluate_return_expression(struct statement *stmt)
2989 struct expression *expr = stmt->expression;
2990 struct symbol *fntype;
2992 evaluate_expression(expr);
2993 fntype = current_fn->ctype.base_type;
2994 if (!fntype || fntype == &void_ctype) {
2995 if (expr && expr->ctype != &void_ctype)
2996 expression_error(expr, "return expression in %s function", fntype?"void":"typeless");
2997 if (expr && Wreturn_void)
2998 warning(stmt->pos, "returning void-valued expression");
2999 return NULL;
3002 if (!expr) {
3003 sparse_error(stmt->pos, "return with no return value");
3004 return NULL;
3006 if (!expr->ctype)
3007 return NULL;
3008 compatible_assignment_types(expr, fntype, &stmt->expression, "return expression");
3009 return NULL;
3012 static void evaluate_if_statement(struct statement *stmt)
3014 if (!stmt->if_conditional)
3015 return;
3017 evaluate_conditional(stmt->if_conditional, 0);
3018 evaluate_statement(stmt->if_true);
3019 evaluate_statement(stmt->if_false);
3022 static void evaluate_iterator(struct statement *stmt)
3024 evaluate_conditional(stmt->iterator_pre_condition, 1);
3025 evaluate_conditional(stmt->iterator_post_condition,1);
3026 evaluate_statement(stmt->iterator_pre_statement);
3027 evaluate_statement(stmt->iterator_statement);
3028 evaluate_statement(stmt->iterator_post_statement);
3031 static void verify_output_constraint(struct expression *expr, const char *constraint)
3033 switch (*constraint) {
3034 case '=': /* Assignment */
3035 case '+': /* Update */
3036 break;
3037 default:
3038 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
3042 static void verify_input_constraint(struct expression *expr, const char *constraint)
3044 switch (*constraint) {
3045 case '=': /* Assignment */
3046 case '+': /* Update */
3047 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
3051 static void evaluate_asm_statement(struct statement *stmt)
3053 struct expression *expr;
3054 int state;
3056 expr = stmt->asm_string;
3057 if (!expr || expr->type != EXPR_STRING) {
3058 sparse_error(stmt->pos, "need constant string for inline asm");
3059 return;
3062 state = 0;
3063 FOR_EACH_PTR(stmt->asm_outputs, expr) {
3064 struct ident *ident;
3066 switch (state) {
3067 case 0: /* Identifier */
3068 state = 1;
3069 ident = (struct ident *)expr;
3070 continue;
3072 case 1: /* Constraint */
3073 state = 2;
3074 if (!expr || expr->type != EXPR_STRING) {
3075 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
3076 *THIS_ADDRESS(expr) = NULL;
3077 continue;
3079 verify_output_constraint(expr, expr->string->data);
3080 continue;
3082 case 2: /* Expression */
3083 state = 0;
3084 if (!evaluate_expression(expr))
3085 return;
3086 if (!lvalue_expression(expr))
3087 warning(expr->pos, "asm output is not an lvalue");
3088 evaluate_assign_to(expr, expr->ctype);
3089 continue;
3091 } END_FOR_EACH_PTR(expr);
3093 state = 0;
3094 FOR_EACH_PTR(stmt->asm_inputs, expr) {
3095 struct ident *ident;
3097 switch (state) {
3098 case 0: /* Identifier */
3099 state = 1;
3100 ident = (struct ident *)expr;
3101 continue;
3103 case 1: /* Constraint */
3104 state = 2;
3105 if (!expr || expr->type != EXPR_STRING) {
3106 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
3107 *THIS_ADDRESS(expr) = NULL;
3108 continue;
3110 verify_input_constraint(expr, expr->string->data);
3111 continue;
3113 case 2: /* Expression */
3114 state = 0;
3115 if (!evaluate_expression(expr))
3116 return;
3117 continue;
3119 } END_FOR_EACH_PTR(expr);
3121 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
3122 if (!expr) {
3123 sparse_error(stmt->pos, "bad asm output");
3124 return;
3126 if (expr->type == EXPR_STRING)
3127 continue;
3128 expression_error(expr, "asm clobber is not a string");
3129 } END_FOR_EACH_PTR(expr);
3132 static void evaluate_case_statement(struct statement *stmt)
3134 evaluate_expression(stmt->case_expression);
3135 evaluate_expression(stmt->case_to);
3136 evaluate_statement(stmt->case_statement);
3139 static void check_case_type(struct expression *switch_expr,
3140 struct expression *case_expr,
3141 struct expression **enumcase)
3143 struct symbol *switch_type, *case_type;
3144 int sclass, cclass;
3146 if (!case_expr)
3147 return;
3149 switch_type = switch_expr->ctype;
3150 case_type = evaluate_expression(case_expr);
3152 if (!switch_type || !case_type)
3153 goto Bad;
3154 if (enumcase) {
3155 if (*enumcase)
3156 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
3157 else if (is_enum_type(case_type))
3158 *enumcase = case_expr;
3161 sclass = classify_type(switch_type, &switch_type);
3162 cclass = classify_type(case_type, &case_type);
3164 /* both should be arithmetic */
3165 if (!(sclass & cclass & TYPE_NUM))
3166 goto Bad;
3168 /* neither should be floating */
3169 if ((sclass | cclass) & TYPE_FLOAT)
3170 goto Bad;
3172 /* if neither is restricted, we are OK */
3173 if (!((sclass | cclass) & TYPE_RESTRICT))
3174 return;
3176 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
3177 cclass, sclass, case_type, switch_type))
3178 warning(case_expr->pos, "restricted degrades to integer");
3180 return;
3182 Bad:
3183 expression_error(case_expr, "incompatible types for 'case' statement");
3186 static void evaluate_switch_statement(struct statement *stmt)
3188 struct symbol *sym;
3189 struct expression *enumcase = NULL;
3190 struct expression **enumcase_holder = &enumcase;
3191 struct expression *sel = stmt->switch_expression;
3193 evaluate_expression(sel);
3194 evaluate_statement(stmt->switch_statement);
3195 if (!sel)
3196 return;
3197 if (sel->ctype && is_enum_type(sel->ctype))
3198 enumcase_holder = NULL; /* Only check cases against switch */
3200 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
3201 struct statement *case_stmt = sym->stmt;
3202 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
3203 check_case_type(sel, case_stmt->case_to, enumcase_holder);
3204 } END_FOR_EACH_PTR(sym);
3207 struct symbol *evaluate_statement(struct statement *stmt)
3209 if (!stmt)
3210 return NULL;
3212 switch (stmt->type) {
3213 case STMT_DECLARATION: {
3214 struct symbol *s;
3215 FOR_EACH_PTR(stmt->declaration, s) {
3216 evaluate_symbol(s);
3217 } END_FOR_EACH_PTR(s);
3218 return NULL;
3221 case STMT_RETURN:
3222 return evaluate_return_expression(stmt);
3224 case STMT_EXPRESSION:
3225 if (!evaluate_expression(stmt->expression))
3226 return NULL;
3227 if (stmt->expression->ctype == &null_ctype)
3228 stmt->expression = cast_to(stmt->expression, &ptr_ctype);
3229 return degenerate(stmt->expression);
3231 case STMT_COMPOUND: {
3232 struct statement *s;
3233 struct symbol *type = NULL;
3235 /* Evaluate the return symbol in the compound statement */
3236 evaluate_symbol(stmt->ret);
3239 * Then, evaluate each statement, making the type of the
3240 * compound statement be the type of the last statement
3242 type = evaluate_statement(stmt->args);
3243 FOR_EACH_PTR(stmt->stmts, s) {
3244 type = evaluate_statement(s);
3245 } END_FOR_EACH_PTR(s);
3246 if (!type)
3247 type = &void_ctype;
3248 return type;
3250 case STMT_IF:
3251 evaluate_if_statement(stmt);
3252 return NULL;
3253 case STMT_ITERATOR:
3254 evaluate_iterator(stmt);
3255 return NULL;
3256 case STMT_SWITCH:
3257 evaluate_switch_statement(stmt);
3258 return NULL;
3259 case STMT_CASE:
3260 evaluate_case_statement(stmt);
3261 return NULL;
3262 case STMT_LABEL:
3263 return evaluate_statement(stmt->label_statement);
3264 case STMT_GOTO:
3265 evaluate_expression(stmt->goto_expression);
3266 return NULL;
3267 case STMT_NONE:
3268 break;
3269 case STMT_ASM:
3270 evaluate_asm_statement(stmt);
3271 return NULL;
3272 case STMT_CONTEXT:
3273 evaluate_expression(stmt->expression);
3274 return NULL;
3275 case STMT_RANGE:
3276 evaluate_expression(stmt->range_expression);
3277 evaluate_expression(stmt->range_low);
3278 evaluate_expression(stmt->range_high);
3279 return NULL;
3281 return NULL;