[PATCH] Attribute "sentinel"
[smatch.git] / evaluate.c
blob1a12b67641e82e5376693a75fa322521755b87ff
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 sparse_error(expr->pos, "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 sparse_error(expr->pos, "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->ctype.modifiers = MOD_STATIC;
78 sym->ctype.base_type = array;
79 sym->initializer = initstr;
81 initstr->ctype = sym;
82 initstr->string = expr->string;
84 array->array_size = sym->array_size;
85 array->bit_size = bits_in_char * length;
86 array->ctype.alignment = 1;
87 array->ctype.modifiers = MOD_STATIC;
88 array->ctype.base_type = &char_ctype;
90 addr->symbol = sym;
91 addr->ctype = &lazy_ptr_ctype;
93 expr->type = EXPR_PREOP;
94 expr->op = '*';
95 expr->unop = addr;
96 expr->ctype = sym;
97 return sym;
100 static inline struct symbol *integer_promotion(struct symbol *type)
102 struct symbol *orig_type = type;
103 unsigned long mod = type->ctype.modifiers;
104 int width;
106 if (type->type == SYM_NODE)
107 type = type->ctype.base_type;
108 if (type->type == SYM_ENUM)
109 type = type->ctype.base_type;
110 width = type->bit_size;
113 * Bitfields always promote to the base type,
114 * even if the bitfield might be bigger than
115 * an "int".
117 if (type->type == SYM_BITFIELD) {
118 type = type->ctype.base_type;
119 orig_type = type;
121 mod = type->ctype.modifiers;
122 if (width < bits_in_int)
123 return &int_ctype;
125 /* If char/short has as many bits as int, it still gets "promoted" */
126 if (mod & (MOD_CHAR | MOD_SHORT)) {
127 if (mod & MOD_UNSIGNED)
128 return &uint_ctype;
129 return &int_ctype;
131 return orig_type;
135 * integer part of usual arithmetic conversions:
136 * integer promotions are applied
137 * if left and right are identical, we are done
138 * if signedness is the same, convert one with lower rank
139 * unless unsigned argument has rank lower than signed one, convert the
140 * signed one.
141 * if signed argument is bigger than unsigned one, convert the unsigned.
142 * otherwise, convert signed.
144 * Leaving aside the integer promotions, that is equivalent to
145 * if identical, don't convert
146 * if left is bigger than right, convert right
147 * if right is bigger than left, convert right
148 * otherwise, if signedness is the same, convert one with lower rank
149 * otherwise convert the signed one.
151 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
153 unsigned long lmod, rmod;
155 left = integer_promotion(left);
156 right = integer_promotion(right);
158 if (left == right)
159 goto left;
161 if (left->bit_size > right->bit_size)
162 goto left;
164 if (right->bit_size > left->bit_size)
165 goto right;
167 lmod = left->ctype.modifiers;
168 rmod = right->ctype.modifiers;
169 if ((lmod ^ rmod) & MOD_UNSIGNED) {
170 if (lmod & MOD_UNSIGNED)
171 goto left;
172 } else if ((lmod & ~rmod) & (MOD_LONG | MOD_LONGLONG))
173 goto left;
174 right:
175 left = right;
176 left:
177 return left;
180 static int same_cast_type(struct symbol *orig, struct symbol *new)
182 return orig->bit_size == new->bit_size && orig->bit_offset == orig->bit_offset;
185 static struct symbol *base_type(struct symbol *node, unsigned long *modp, unsigned long *asp)
187 unsigned long mod, as;
189 mod = 0; as = 0;
190 while (node) {
191 mod |= node->ctype.modifiers;
192 as |= node->ctype.as;
193 if (node->type == SYM_NODE) {
194 node = node->ctype.base_type;
195 continue;
197 break;
199 *modp = mod & ~MOD_IGNORE;
200 *asp = as;
201 return node;
204 static int is_same_type(struct expression *expr, struct symbol *new)
206 struct symbol *old = expr->ctype;
207 unsigned long oldmod, newmod, oldas, newas;
209 old = base_type(old, &oldmod, &oldas);
210 new = base_type(new, &newmod, &newas);
212 /* Same base type, same address space? */
213 if (old == new && oldas == newas) {
214 unsigned long difmod;
216 /* Check the modifier bits. */
217 difmod = (oldmod ^ newmod) & ~MOD_NOCAST;
219 /* Exact same type? */
220 if (!difmod)
221 return 1;
224 * Not the same type, but differs only in "const".
225 * Don't warn about MOD_NOCAST.
227 if (difmod == MOD_CONST)
228 return 0;
230 if ((oldmod | newmod) & MOD_NOCAST) {
231 const char *tofrom = "to/from";
232 if (!(newmod & MOD_NOCAST))
233 tofrom = "from";
234 if (!(oldmod & MOD_NOCAST))
235 tofrom = "to";
236 warning(expr->pos, "implicit cast %s nocast type", tofrom);
238 return 0;
242 * This gets called for implicit casts in assignments and
243 * integer promotion. We often want to try to move the
244 * cast down, because the ops involved may have been
245 * implicitly cast up, and we can get rid of the casts
246 * early.
248 static struct expression * cast_to(struct expression *old, struct symbol *type)
250 struct expression *expr;
252 if (is_same_type(old, type))
253 return old;
256 * See if we can simplify the op. Move the cast down.
258 switch (old->type) {
259 case EXPR_PREOP:
260 if (old->ctype->bit_size < type->bit_size)
261 break;
262 if (old->op == '~') {
263 old->ctype = type;
264 old->unop = cast_to(old->unop, type);
265 return old;
267 break;
269 case EXPR_IMPLIED_CAST:
270 if (old->ctype->bit_size >= type->bit_size) {
271 struct expression *orig = old->cast_expression;
272 if (same_cast_type(orig->ctype, type))
273 return orig;
274 if (old->ctype->bit_offset == type->bit_offset) {
275 old->ctype = type;
276 old->cast_type = type;
277 return old;
280 break;
282 default:
283 /* nothing */;
286 expr = alloc_expression(old->pos, EXPR_IMPLIED_CAST);
287 expr->ctype = type;
288 expr->cast_type = type;
289 expr->cast_expression = old;
290 return expr;
293 static int is_type_type(struct symbol *type)
295 return (type->ctype.modifiers & MOD_TYPE) != 0;
298 int is_ptr_type(struct symbol *type)
300 if (type->type == SYM_NODE)
301 type = type->ctype.base_type;
302 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
305 static inline int is_float_type(struct symbol *type)
307 if (type->type == SYM_NODE)
308 type = type->ctype.base_type;
309 return type->ctype.base_type == &fp_type;
312 static inline int is_byte_type(struct symbol *type)
314 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
317 static inline int is_string_type(struct symbol *type)
319 if (type->type == SYM_NODE)
320 type = type->ctype.base_type;
321 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
324 static struct symbol *bad_expr_type(struct expression *expr)
326 sparse_error(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
327 switch (expr->type) {
328 case EXPR_BINOP:
329 case EXPR_COMPARE:
330 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
331 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
332 break;
333 case EXPR_PREOP:
334 case EXPR_POSTOP:
335 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
336 break;
337 default:
338 break;
341 return NULL;
344 static struct symbol *compatible_float_binop(struct expression **lp, struct expression **rp)
346 struct expression *left = *lp, *right = *rp;
347 struct symbol *ltype = left->ctype, *rtype = right->ctype;
349 if (ltype->type == SYM_NODE)
350 ltype = ltype->ctype.base_type;
351 if (rtype->type == SYM_NODE)
352 rtype = rtype->ctype.base_type;
353 if (is_float_type(ltype)) {
354 if (is_int_type(rtype))
355 goto Left;
356 if (is_float_type(rtype)) {
357 unsigned long lmod = ltype->ctype.modifiers;
358 unsigned long rmod = rtype->ctype.modifiers;
359 lmod &= MOD_LONG | MOD_LONGLONG;
360 rmod &= MOD_LONG | MOD_LONGLONG;
361 if (lmod == rmod)
362 return ltype;
363 if (lmod & ~rmod)
364 goto Left;
365 else
366 goto Right;
368 return NULL;
370 if (!is_float_type(rtype) || !is_int_type(ltype))
371 return NULL;
372 Right:
373 *lp = cast_to(left, rtype);
374 return rtype;
375 Left:
376 *rp = cast_to(right, ltype);
377 return ltype;
380 static struct symbol *compatible_integer_binop(struct expression **lp, struct expression **rp)
382 struct expression *left = *lp, *right = *rp;
383 struct symbol *ltype = left->ctype, *rtype = right->ctype;
385 if (ltype->type == SYM_NODE)
386 ltype = ltype->ctype.base_type;
387 if (rtype->type == SYM_NODE)
388 rtype = rtype->ctype.base_type;
389 if (is_int_type(ltype) && is_int_type(rtype)) {
390 struct symbol *ctype = bigger_int_type(ltype, rtype);
392 *lp = cast_to(left, ctype);
393 *rp = cast_to(right, ctype);
394 return ctype;
396 return NULL;
399 static int restricted_value(struct expression *v, struct symbol *type)
401 if (v->type != EXPR_VALUE)
402 return 1;
403 if (v->value != 0)
404 return 1;
405 return 0;
408 static int restricted_binop(int op, struct symbol *type)
410 switch (op) {
411 case '&':
412 case '|':
413 case '^':
414 case '?':
415 case '=':
416 case SPECIAL_EQUAL:
417 case SPECIAL_NOTEQUAL:
418 case SPECIAL_AND_ASSIGN:
419 case SPECIAL_OR_ASSIGN:
420 case SPECIAL_XOR_ASSIGN:
421 return 0;
422 default:
423 return 1;
427 static int restricted_unop(int op, struct symbol *type)
429 if (op == '~' && type->bit_size >= bits_in_int)
430 return 0;
431 if (op == '+')
432 return 0;
433 return 1;
436 static struct symbol *compatible_restricted_binop(int op, struct expression **lp, struct expression **rp)
438 struct expression *left = *lp, *right = *rp;
439 struct symbol *ltype = left->ctype, *rtype = right->ctype;
440 struct symbol *type = NULL;
442 if (ltype->type == SYM_NODE)
443 ltype = ltype->ctype.base_type;
444 if (ltype->type == SYM_ENUM)
445 ltype = ltype->ctype.base_type;
446 if (rtype->type == SYM_NODE)
447 rtype = rtype->ctype.base_type;
448 if (rtype->type == SYM_ENUM)
449 rtype = rtype->ctype.base_type;
450 if (is_restricted_type(ltype)) {
451 if (is_restricted_type(rtype)) {
452 if (ltype == rtype)
453 type = ltype;
454 } else {
455 if (!restricted_value(right, ltype))
456 type = ltype;
458 } else if (is_restricted_type(rtype)) {
459 if (!restricted_value(left, rtype))
460 type = rtype;
462 if (!type)
463 return NULL;
464 if (restricted_binop(op, type))
465 return NULL;
466 return type;
469 static struct symbol *evaluate_arith(struct expression *expr, int float_ok)
471 struct symbol *ctype = compatible_integer_binop(&expr->left, &expr->right);
472 if (!ctype && float_ok)
473 ctype = compatible_float_binop(&expr->left, &expr->right);
474 if (!ctype)
475 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
476 if (ctype) {
477 expr->ctype = ctype;
478 return ctype;
480 return bad_expr_type(expr);
483 static inline int lvalue_expression(struct expression *expr)
485 return expr->type == EXPR_PREOP && expr->op == '*';
488 static int ptr_object_size(struct symbol *ptr_type)
490 if (ptr_type->type == SYM_NODE)
491 ptr_type = ptr_type->ctype.base_type;
492 if (ptr_type->type == SYM_PTR)
493 ptr_type = get_base_type(ptr_type);
494 return ptr_type->bit_size;
497 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *ctype, struct expression **ip)
499 struct expression *i = *ip;
500 struct symbol *ptr_type = ctype;
501 int bit_size;
503 if (ptr_type->type == SYM_NODE)
504 ptr_type = ptr_type->ctype.base_type;
506 if (!is_int_type(i->ctype))
507 return bad_expr_type(expr);
509 examine_symbol_type(ctype);
511 if (!ctype->ctype.base_type) {
512 sparse_error(expr->pos, "missing type information");
513 return NULL;
516 /* Get the size of whatever the pointer points to */
517 bit_size = ptr_object_size(ctype);
519 if (bit_size > bits_in_char) {
520 int multiply = bit_size >> 3;
521 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
523 if (i->type == EXPR_VALUE) {
524 val->value = i->value * multiply;
525 val->ctype = size_t_ctype;
526 *ip = val;
527 } else {
528 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
530 val->ctype = size_t_ctype;
531 val->value = bit_size >> 3;
533 mul->op = '*';
534 mul->ctype = size_t_ctype;
535 mul->left = i;
536 mul->right = val;
538 *ip = mul;
542 expr->ctype = ctype;
543 return ctype;
546 static struct symbol *evaluate_add(struct expression *expr)
548 struct expression *left = expr->left, *right = expr->right;
549 struct symbol *ltype = left->ctype, *rtype = right->ctype;
551 if (is_ptr_type(ltype))
552 return evaluate_ptr_add(expr, degenerate(left), &expr->right);
554 if (is_ptr_type(rtype))
555 return evaluate_ptr_add(expr, degenerate(right), &expr->left);
557 return evaluate_arith(expr, 1);
560 const char * type_difference(struct symbol *target, struct symbol *source,
561 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
563 for (;;) {
564 unsigned long mod1, mod2, diff;
565 unsigned long as1, as2;
566 int type1, type2;
567 struct symbol *base1, *base2;
569 if (target == source)
570 break;
571 if (!target || !source)
572 return "different types";
574 * Peel of per-node information.
575 * FIXME! Check alignment and context too here!
577 mod1 = target->ctype.modifiers;
578 as1 = target->ctype.as;
579 mod2 = source->ctype.modifiers;
580 as2 = source->ctype.as;
581 if (target->type == SYM_NODE) {
582 target = target->ctype.base_type;
583 if (!target)
584 return "bad types";
585 if (target->type == SYM_PTR) {
586 mod1 = 0;
587 as1 = 0;
589 mod1 |= target->ctype.modifiers;
590 as1 |= target->ctype.as;
592 if (source->type == SYM_NODE) {
593 source = source->ctype.base_type;
594 if (!source)
595 return "bad types";
596 if (source->type == SYM_PTR) {
597 mod2 = 0;
598 as2 = 0;
600 mod2 |= source->ctype.modifiers;
601 as2 |= source->ctype.as;
603 if (target->type == SYM_ENUM) {
604 target = target->ctype.base_type;
605 if (!target)
606 return "bad types";
608 if (source->type == SYM_ENUM) {
609 source = source->ctype.base_type;
610 if (!source)
611 return "bad types";
614 if (target == source)
615 break;
616 if (!target || !source)
617 return "different types";
619 type1 = target->type;
620 base1 = target->ctype.base_type;
622 type2 = source->type;
623 base2 = source->ctype.base_type;
626 * Pointers to functions compare as the function itself
628 if (type1 == SYM_PTR && base1) {
629 base1 = examine_symbol_type(base1);
630 switch (base1->type) {
631 case SYM_FN:
632 type1 = SYM_FN;
633 target = base1;
634 base1 = base1->ctype.base_type;
635 default:
636 /* nothing */;
639 if (type2 == SYM_PTR && base2) {
640 base2 = examine_symbol_type(base2);
641 switch (base2->type) {
642 case SYM_FN:
643 type2 = SYM_FN;
644 source = base2;
645 base2 = base2->ctype.base_type;
646 default:
647 /* nothing */;
651 /* Arrays degenerate to pointers for type comparisons */
652 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
653 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
655 if (type1 != type2 || type1 == SYM_RESTRICT)
656 return "different base types";
658 /* Must be same address space to be comparable */
659 if (as1 != as2)
660 return "different address spaces";
662 /* Ignore differences in storage types or addressability */
663 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
664 diff &= (mod1 & ~target_mod_ignore) | (mod2 & ~source_mod_ignore);
665 if (diff) {
666 if (diff & MOD_SIZE)
667 return "different type sizes";
668 if (diff & ~MOD_SIGNEDNESS)
669 return "different modifiers";
671 /* Differs in signedness only.. */
672 if (Wtypesign) {
674 * Warn if both are explicitly signed ("unsigned" is obvously
675 * always explicit, and since we know one of them has to be
676 * unsigned, we check if the signed one was explicit).
678 if ((mod1 | mod2) & MOD_EXPLICITLY_SIGNED)
679 return "different explicit signedness";
682 * "char" matches both "unsigned char" and "signed char",
683 * so if the explicit test didn't trigger, then we should
684 * not warn about a char.
686 if (!(mod1 & MOD_CHAR))
687 return "different signedness";
691 if (type1 == SYM_FN) {
692 int i;
693 struct symbol *arg1, *arg2;
694 if (base1->variadic != base2->variadic)
695 return "incompatible variadic arguments";
696 PREPARE_PTR_LIST(target->arguments, arg1);
697 PREPARE_PTR_LIST(source->arguments, arg2);
698 i = 1;
699 for (;;) {
700 const char *diff;
701 diff = type_difference(arg1, arg2, 0, 0);
702 if (diff) {
703 static char argdiff[80];
704 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
705 return argdiff;
707 if (!arg1)
708 break;
709 NEXT_PTR_LIST(arg1);
710 NEXT_PTR_LIST(arg2);
711 i++;
713 FINISH_PTR_LIST(arg2);
714 FINISH_PTR_LIST(arg1);
717 target = base1;
718 source = base2;
720 return NULL;
723 static int is_null_ptr(struct expression *expr)
725 if (expr->type != EXPR_VALUE || expr->value)
726 return 0;
727 if (!is_ptr_type(expr->ctype))
728 warning(expr->pos, "Using plain integer as NULL pointer");
729 return 1;
732 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
734 /* NULL expression? Just return the type of the "other side" */
735 if (is_null_ptr(r))
736 return l->ctype;
737 if (is_null_ptr(l))
738 return r->ctype;
739 return NULL;
743 * Ignore differences in "volatile" and "const"ness when
744 * subtracting pointers
746 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
748 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression **rp)
750 const char *typediff;
751 struct symbol *ctype;
752 struct symbol *ltype, *rtype;
753 struct expression *r = *rp;
755 ltype = degenerate(l);
756 rtype = degenerate(r);
759 * If it is an integer subtract: the ptr add case will do the
760 * right thing.
762 if (!is_ptr_type(rtype))
763 return evaluate_ptr_add(expr, degenerate(l), rp);
765 ctype = ltype;
766 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
767 if (typediff) {
768 ctype = common_ptr_type(l, r);
769 if (!ctype) {
770 sparse_error(expr->pos, "subtraction of different types can't work (%s)", typediff);
771 return NULL;
774 examine_symbol_type(ctype);
776 /* Figure out the base type we point to */
777 if (ctype->type == SYM_NODE)
778 ctype = ctype->ctype.base_type;
779 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
780 sparse_error(expr->pos, "subtraction of functions? Share your drugs");
781 return NULL;
783 ctype = get_base_type(ctype);
785 expr->ctype = ssize_t_ctype;
786 if (ctype->bit_size > bits_in_char) {
787 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
788 struct expression *div = expr;
789 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
790 unsigned long value = ctype->bit_size >> 3;
792 val->ctype = size_t_ctype;
793 val->value = value;
795 if (value & (value-1)) {
796 if (Wptr_subtraction_blows)
797 warning(expr->pos, "potentially expensive pointer subtraction");
800 sub->op = '-';
801 sub->ctype = ssize_t_ctype;
802 sub->left = l;
803 sub->right = r;
805 div->op = '/';
806 div->left = sub;
807 div->right = val;
810 return ssize_t_ctype;
813 static struct symbol *evaluate_sub(struct expression *expr)
815 struct expression *left = expr->left;
816 struct symbol *ltype = left->ctype;
818 if (is_ptr_type(ltype))
819 return evaluate_ptr_sub(expr, left, &expr->right);
821 return evaluate_arith(expr, 1);
824 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
826 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
828 struct symbol *ctype;
830 if (!expr)
831 return NULL;
833 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
834 warning(expr->pos, "assignment expression in conditional");
836 ctype = evaluate_expression(expr);
837 if (ctype) {
838 if (is_safe_type(ctype))
839 warning(expr->pos, "testing a 'safe expression'");
842 return ctype;
845 static struct symbol *evaluate_logical(struct expression *expr)
847 if (!evaluate_conditional(expr->left, 0))
848 return NULL;
849 if (!evaluate_conditional(expr->right, 0))
850 return NULL;
852 expr->ctype = &bool_ctype;
853 return &bool_ctype;
856 static struct symbol *evaluate_shift(struct expression *expr)
858 struct expression *left = expr->left, *right = expr->right;
859 struct symbol *ltype = left->ctype, *rtype = right->ctype;
861 if (ltype->type == SYM_NODE)
862 ltype = ltype->ctype.base_type;
863 if (rtype->type == SYM_NODE)
864 rtype = rtype->ctype.base_type;
865 if (is_int_type(ltype) && is_int_type(rtype)) {
866 struct symbol *ctype = integer_promotion(ltype);
867 expr->left = cast_to(expr->left, ctype);
868 expr->ctype = ctype;
869 ctype = integer_promotion(rtype);
870 expr->right = cast_to(expr->right, ctype);
871 return expr->ctype;
873 return bad_expr_type(expr);
876 static struct symbol *evaluate_binop(struct expression *expr)
878 switch (expr->op) {
879 // addition can take ptr+int, fp and int
880 case '+':
881 return evaluate_add(expr);
883 // subtraction can take ptr-ptr, fp and int
884 case '-':
885 return evaluate_sub(expr);
887 // Arithmetic operations can take fp and int
888 case '*': case '/':
889 return evaluate_arith(expr, 1);
891 // shifts do integer promotions, but that's it.
892 case SPECIAL_LEFTSHIFT: case SPECIAL_RIGHTSHIFT:
893 return evaluate_shift(expr);
895 // The rest are integer operations
896 // '%', '&', '^', '|'
897 default:
898 return evaluate_arith(expr, 0);
902 static struct symbol *evaluate_comma(struct expression *expr)
904 expr->ctype = expr->right->ctype;
905 return expr->ctype;
908 static int modify_for_unsigned(int op)
910 if (op == '<')
911 op = SPECIAL_UNSIGNED_LT;
912 else if (op == '>')
913 op = SPECIAL_UNSIGNED_GT;
914 else if (op == SPECIAL_LTE)
915 op = SPECIAL_UNSIGNED_LTE;
916 else if (op == SPECIAL_GTE)
917 op = SPECIAL_UNSIGNED_GTE;
918 return op;
921 static struct symbol *evaluate_compare(struct expression *expr)
923 struct expression *left = expr->left, *right = expr->right;
924 struct symbol *ltype = left->ctype, *rtype = right->ctype;
925 struct symbol *ctype;
927 /* Type types? */
928 if (is_type_type(ltype) && is_type_type(rtype))
929 goto OK;
931 if (is_safe_type(ltype) || is_safe_type(rtype))
932 warning(expr->pos, "testing a 'safe expression'");
934 /* Pointer types? */
935 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
936 // FIXME! Check the types for compatibility
937 expr->op = modify_for_unsigned(expr->op);
938 goto OK;
941 ctype = compatible_integer_binop(&expr->left, &expr->right);
942 if (ctype) {
943 if (ctype->ctype.modifiers & MOD_UNSIGNED)
944 expr->op = modify_for_unsigned(expr->op);
945 goto OK;
948 ctype = compatible_float_binop(&expr->left, &expr->right);
949 if (ctype)
950 goto OK;
952 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
953 if (ctype) {
954 if (ctype->ctype.modifiers & MOD_UNSIGNED)
955 expr->op = modify_for_unsigned(expr->op);
956 goto OK;
959 bad_expr_type(expr);
962 expr->ctype = &bool_ctype;
963 return &bool_ctype;
967 * FIXME!! This should do casts, array degeneration etc..
969 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
971 struct symbol *ltype = left->ctype, *rtype = right->ctype;
973 if (ltype->type == SYM_NODE)
974 ltype = ltype->ctype.base_type;
976 if (rtype->type == SYM_NODE)
977 rtype = rtype->ctype.base_type;
979 if (ltype->type == SYM_PTR) {
980 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
981 return ltype;
984 if (rtype->type == SYM_PTR) {
985 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
986 return rtype;
988 return NULL;
992 * NOTE! The degenerate case of "x ? : y", where we don't
993 * have a true case, this will possibly promote "x" to the
994 * same type as "y", and thus _change_ the conditional
995 * test in the expression. But since promotion is "safe"
996 * for testing, that's ok.
998 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1000 struct expression **true;
1001 struct symbol *ctype, *ltype, *rtype;
1002 const char * typediff;
1004 if (!evaluate_conditional(expr->conditional, 0))
1005 return NULL;
1006 if (!evaluate_expression(expr->cond_false))
1007 return NULL;
1009 ctype = degenerate(expr->conditional);
1010 rtype = degenerate(expr->cond_false);
1012 true = &expr->conditional;
1013 ltype = ctype;
1014 if (expr->cond_true) {
1015 if (!evaluate_expression(expr->cond_true))
1016 return NULL;
1017 ltype = degenerate(expr->cond_true);
1018 true = &expr->cond_true;
1021 ctype = compatible_integer_binop(true, &expr->cond_false);
1022 if (ctype)
1023 goto out;
1024 ctype = compatible_ptr_type(*true, expr->cond_false);
1025 if (ctype)
1026 goto out;
1027 ctype = compatible_float_binop(true, &expr->cond_false);
1028 if (ctype)
1029 goto out;
1030 ctype = compatible_restricted_binop('?', true, &expr->cond_false);
1031 if (ctype)
1032 goto out;
1033 ctype = ltype;
1034 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
1035 if (!typediff)
1036 goto out;
1037 sparse_error(expr->pos, "incompatible types in conditional expression (%s)", typediff);
1038 return NULL;
1040 out:
1041 expr->ctype = ctype;
1042 return ctype;
1045 /* FP assignments can not do modulo or bit operations */
1046 static int compatible_float_op(int op)
1048 return op == '=' ||
1049 op == SPECIAL_ADD_ASSIGN ||
1050 op == SPECIAL_SUB_ASSIGN ||
1051 op == SPECIAL_MUL_ASSIGN ||
1052 op == SPECIAL_DIV_ASSIGN;
1055 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1056 struct expression **rp, struct symbol *source, const char *where, int op)
1058 const char *typediff;
1059 struct symbol *t;
1060 int target_as;
1062 if (is_int_type(target)) {
1063 if (is_int_type(source))
1064 goto Cast;
1065 if (is_float_type(source))
1066 goto Cast;
1067 } else if (is_float_type(target)) {
1068 if (!compatible_float_op(op)) {
1069 sparse_error(expr->pos, "invalid assignment");
1070 return 0;
1072 if (is_int_type(source))
1073 goto Cast;
1074 if (is_float_type(source))
1075 goto Cast;
1076 } else if (is_restricted_type(target)) {
1077 if (restricted_binop(op, target)) {
1078 sparse_error(expr->pos, "bad restricted assignment");
1079 return 0;
1081 if (!restricted_value(*rp, target))
1082 return 1;
1083 } else if (is_ptr_type(target)) {
1084 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1085 evaluate_ptr_add(expr, target, rp);
1086 return 1;
1088 if (op != '=') {
1089 sparse_error(expr->pos, "invalid pointer assignment");
1090 return 0;
1092 } else if (op != '=') {
1093 sparse_error(expr->pos, "invalid assignment");
1094 return 0;
1097 /* It's ok if the target is more volatile or const than the source */
1098 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
1099 if (!typediff)
1100 return 1;
1102 /* Pointer destination? */
1103 t = target;
1104 target_as = t->ctype.as;
1105 if (t->type == SYM_NODE) {
1106 t = t->ctype.base_type;
1107 target_as |= t->ctype.as;
1109 if (t->type == SYM_PTR || t->type == SYM_FN || t->type == SYM_ARRAY) {
1110 struct expression *right = *rp;
1111 struct symbol *s = source;
1112 int source_as;
1114 // NULL pointer is always ok
1115 if (is_null_ptr(right))
1116 goto Cast;
1118 /* "void *" matches anything as long as the address space is ok */
1119 source_as = s->ctype.as;
1120 if (s->type == SYM_NODE) {
1121 s = s->ctype.base_type;
1122 source_as |= s->ctype.as;
1124 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
1125 s = get_base_type(s);
1126 t = get_base_type(t);
1127 if (s == &void_ctype || t == &void_ctype)
1128 goto Cast;
1132 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1133 info(expr->pos, " expected %s", show_typename(target));
1134 info(expr->pos, " got %s", show_typename(source));
1135 *rp = cast_to(*rp, target);
1136 return 0;
1137 Cast:
1138 *rp = cast_to(*rp, target);
1139 return 1;
1142 static void mark_assigned(struct expression *expr)
1144 struct symbol *sym;
1146 if (!expr)
1147 return;
1148 switch (expr->type) {
1149 case EXPR_SYMBOL:
1150 sym = expr->symbol;
1151 if (!sym)
1152 return;
1153 if (sym->type != SYM_NODE)
1154 return;
1155 sym->ctype.modifiers |= MOD_ASSIGNED;
1156 return;
1158 case EXPR_BINOP:
1159 mark_assigned(expr->left);
1160 mark_assigned(expr->right);
1161 return;
1162 case EXPR_CAST:
1163 mark_assigned(expr->cast_expression);
1164 return;
1165 case EXPR_SLICE:
1166 mark_assigned(expr->base);
1167 return;
1168 default:
1169 /* Hmm? */
1170 return;
1174 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1176 if (type->ctype.modifiers & MOD_CONST)
1177 sparse_error(left->pos, "assignment to const expression");
1179 /* We know left is an lvalue, so it's a "preop-*" */
1180 mark_assigned(left->unop);
1183 static struct symbol *evaluate_assignment(struct expression *expr)
1185 struct expression *left = expr->left, *right = expr->right;
1186 struct expression *where = expr;
1187 struct symbol *ltype, *rtype;
1189 if (!lvalue_expression(left)) {
1190 sparse_error(expr->pos, "not an lvalue");
1191 return NULL;
1194 ltype = left->ctype;
1196 rtype = degenerate(right);
1198 if (!compatible_assignment_types(where, ltype, &where->right, rtype, "assignment", expr->op))
1199 return NULL;
1201 evaluate_assign_to(left, ltype);
1203 expr->ctype = ltype;
1204 return ltype;
1207 static void examine_fn_arguments(struct symbol *fn)
1209 struct symbol *s;
1211 FOR_EACH_PTR(fn->arguments, s) {
1212 struct symbol *arg = evaluate_symbol(s);
1213 /* Array/function arguments silently degenerate into pointers */
1214 if (arg) {
1215 struct symbol *ptr;
1216 switch(arg->type) {
1217 case SYM_ARRAY:
1218 case SYM_FN:
1219 ptr = alloc_symbol(s->pos, SYM_PTR);
1220 if (arg->type == SYM_ARRAY)
1221 ptr->ctype = arg->ctype;
1222 else
1223 ptr->ctype.base_type = arg;
1224 ptr->ctype.as |= s->ctype.as;
1225 ptr->ctype.modifiers |= s->ctype.modifiers;
1227 s->ctype.base_type = ptr;
1228 s->ctype.as = 0;
1229 s->ctype.modifiers = 0;
1230 s->bit_size = 0;
1231 s->examined = 0;
1232 examine_symbol_type(s);
1233 break;
1234 default:
1235 /* nothing */
1236 break;
1239 } END_FOR_EACH_PTR(s);
1242 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1244 /* Take the modifiers of the pointer, and apply them to the member */
1245 mod |= sym->ctype.modifiers;
1246 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1247 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1248 *newsym = *sym;
1249 newsym->ctype.as = as;
1250 newsym->ctype.modifiers = mod;
1251 sym = newsym;
1253 return sym;
1256 #define MOD_PTRINHERIT (MOD_VOLATILE | MOD_CONST | MOD_NODEREF | MOD_STORAGE)
1258 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1260 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1261 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1263 node->ctype.base_type = ptr;
1264 ptr->bit_size = bits_in_pointer;
1265 ptr->ctype.alignment = pointer_alignment;
1267 node->bit_size = bits_in_pointer;
1268 node->ctype.alignment = pointer_alignment;
1270 access_symbol(sym);
1271 if (sym->ctype.modifiers & MOD_REGISTER) {
1272 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1273 sym->ctype.modifiers &= ~MOD_REGISTER;
1275 if (sym->type == SYM_NODE) {
1276 ptr->ctype.as |= sym->ctype.as;
1277 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1278 sym = sym->ctype.base_type;
1280 if (degenerate && sym->type == SYM_ARRAY) {
1281 ptr->ctype.as |= sym->ctype.as;
1282 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1283 sym = sym->ctype.base_type;
1285 ptr->ctype.base_type = sym;
1287 return node;
1290 /* Arrays degenerate into pointers on pointer arithmetic */
1291 static struct symbol *degenerate(struct expression *expr)
1293 struct symbol *ctype, *base;
1295 if (!expr)
1296 return NULL;
1297 ctype = expr->ctype;
1298 if (!ctype)
1299 return NULL;
1300 base = examine_symbol_type(ctype);
1301 if (ctype->type == SYM_NODE)
1302 base = ctype->ctype.base_type;
1304 * Arrays degenerate into pointers to the entries, while
1305 * functions degenerate into pointers to themselves.
1306 * If array was part of non-lvalue compound, we create a copy
1307 * of that compound first and then act as if we were dealing with
1308 * the corresponding field in there.
1310 switch (base->type) {
1311 case SYM_ARRAY:
1312 if (expr->type == EXPR_SLICE) {
1313 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1314 struct expression *e0, *e1, *e2, *e3, *e4;
1316 a->ctype.base_type = expr->base->ctype;
1317 a->bit_size = expr->base->ctype->bit_size;
1318 a->array_size = expr->base->ctype->array_size;
1320 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1321 e0->symbol = a;
1322 e0->ctype = &lazy_ptr_ctype;
1324 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1325 e1->unop = e0;
1326 e1->op = '*';
1327 e1->ctype = expr->base->ctype; /* XXX */
1329 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1330 e2->left = e1;
1331 e2->right = expr->base;
1332 e2->op = '=';
1333 e2->ctype = expr->base->ctype;
1335 if (expr->r_bitpos) {
1336 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1337 e3->op = '+';
1338 e3->left = e0;
1339 e3->right = alloc_const_expression(expr->pos,
1340 expr->r_bitpos >> 3);
1341 e3->ctype = &lazy_ptr_ctype;
1342 } else {
1343 e3 = e0;
1346 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1347 e4->left = e2;
1348 e4->right = e3;
1349 e4->ctype = &lazy_ptr_ctype;
1351 expr->unop = e4;
1352 expr->type = EXPR_PREOP;
1353 expr->op = '*';
1355 case SYM_FN:
1356 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1357 sparse_error(expr->pos, "strange non-value function or array");
1358 return &bad_ctype;
1360 *expr = *expr->unop;
1361 ctype = create_pointer(expr, ctype, 1);
1362 expr->ctype = ctype;
1363 default:
1364 /* nothing */;
1366 return ctype;
1369 static struct symbol *evaluate_addressof(struct expression *expr)
1371 struct expression *op = expr->unop;
1372 struct symbol *ctype;
1374 if (op->op != '*' || op->type != EXPR_PREOP) {
1375 sparse_error(expr->pos, "not addressable");
1376 return NULL;
1378 ctype = op->ctype;
1379 *expr = *op->unop;
1381 if (expr->type == EXPR_SYMBOL) {
1382 struct symbol *sym = expr->symbol;
1383 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1387 * symbol expression evaluation is lazy about the type
1388 * of the sub-expression, so we may have to generate
1389 * the type here if so..
1391 if (expr->ctype == &lazy_ptr_ctype) {
1392 ctype = create_pointer(expr, ctype, 0);
1393 expr->ctype = ctype;
1395 return expr->ctype;
1399 static struct symbol *evaluate_dereference(struct expression *expr)
1401 struct expression *op = expr->unop;
1402 struct symbol *ctype = op->ctype, *node, *target;
1404 /* Simplify: *&(expr) => (expr) */
1405 if (op->type == EXPR_PREOP && op->op == '&') {
1406 *expr = *op->unop;
1407 return expr->ctype;
1410 /* Dereferencing a node drops all the node information. */
1411 if (ctype->type == SYM_NODE)
1412 ctype = ctype->ctype.base_type;
1414 node = alloc_symbol(expr->pos, SYM_NODE);
1415 target = ctype->ctype.base_type;
1417 switch (ctype->type) {
1418 default:
1419 sparse_error(expr->pos, "cannot derefence this type");
1420 return NULL;
1421 case SYM_PTR:
1422 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1423 merge_type(node, ctype);
1424 break;
1426 case SYM_ARRAY:
1427 if (!lvalue_expression(op)) {
1428 sparse_error(op->pos, "non-lvalue array??");
1429 return NULL;
1432 /* Do the implied "addressof" on the array */
1433 *op = *op->unop;
1436 * When an array is dereferenced, we need to pick
1437 * up the attributes of the original node too..
1439 merge_type(node, op->ctype);
1440 merge_type(node, ctype);
1441 break;
1444 node->bit_size = target->bit_size;
1445 node->array_size = target->array_size;
1447 expr->ctype = node;
1448 return node;
1452 * Unary post-ops: x++ and x--
1454 static struct symbol *evaluate_postop(struct expression *expr)
1456 struct expression *op = expr->unop;
1457 struct symbol *ctype = op->ctype;
1459 if (!lvalue_expression(expr->unop)) {
1460 sparse_error(expr->pos, "need lvalue expression for ++/--");
1461 return NULL;
1463 if (is_restricted_type(ctype) && restricted_unop(expr->op, ctype)) {
1464 sparse_error(expr->pos, "bad operation on restricted");
1465 return NULL;
1468 evaluate_assign_to(op, ctype);
1470 expr->ctype = ctype;
1471 expr->op_value = 1;
1472 if (is_ptr_type(ctype))
1473 expr->op_value = ptr_object_size(ctype) >> 3;
1475 return ctype;
1478 static struct symbol *evaluate_sign(struct expression *expr)
1480 struct symbol *ctype = expr->unop->ctype;
1481 if (is_int_type(ctype)) {
1482 struct symbol *rtype = rtype = integer_promotion(ctype);
1483 expr->unop = cast_to(expr->unop, rtype);
1484 ctype = rtype;
1485 } else if (is_float_type(ctype) && expr->op != '~') {
1486 /* no conversions needed */
1487 } else if (is_restricted_type(ctype) && !restricted_unop(expr->op, ctype)) {
1488 /* no conversions needed */
1489 } else {
1490 return bad_expr_type(expr);
1492 if (expr->op == '+')
1493 *expr = *expr->unop;
1494 expr->ctype = ctype;
1495 return ctype;
1498 static struct symbol *evaluate_preop(struct expression *expr)
1500 struct symbol *ctype = expr->unop->ctype;
1502 switch (expr->op) {
1503 case '(':
1504 *expr = *expr->unop;
1505 return ctype;
1507 case '+':
1508 case '-':
1509 case '~':
1510 return evaluate_sign(expr);
1512 case '*':
1513 return evaluate_dereference(expr);
1515 case '&':
1516 return evaluate_addressof(expr);
1518 case SPECIAL_INCREMENT:
1519 case SPECIAL_DECREMENT:
1521 * From a type evaluation standpoint the pre-ops are
1522 * the same as the postops
1524 return evaluate_postop(expr);
1526 case '!':
1527 if (is_safe_type(ctype))
1528 warning(expr->pos, "testing a 'safe expression'");
1529 if (is_float_type(ctype)) {
1530 struct expression *arg = expr->unop;
1531 expr->type = EXPR_BINOP;
1532 expr->op = SPECIAL_EQUAL;
1533 expr->left = arg;
1534 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1535 expr->right->ctype = ctype;
1536 expr->right->fvalue = 0;
1538 ctype = &bool_ctype;
1539 break;
1541 default:
1542 break;
1544 expr->ctype = ctype;
1545 return &bool_ctype;
1548 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1550 struct ptr_list *head = (struct ptr_list *)_list;
1551 struct ptr_list *list = head;
1553 if (!head)
1554 return NULL;
1555 do {
1556 int i;
1557 for (i = 0; i < list->nr; i++) {
1558 struct symbol *sym = (struct symbol *) list->list[i];
1559 if (sym->ident) {
1560 if (sym->ident != ident)
1561 continue;
1562 *offset = sym->offset;
1563 return sym;
1564 } else {
1565 struct symbol *ctype = sym->ctype.base_type;
1566 struct symbol *sub;
1567 if (!ctype)
1568 continue;
1569 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1570 continue;
1571 sub = find_identifier(ident, ctype->symbol_list, offset);
1572 if (!sub)
1573 continue;
1574 *offset += sym->offset;
1575 return sub;
1578 } while ((list = list->next) != head);
1579 return NULL;
1582 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1584 struct expression *add;
1587 * Create a new add-expression
1589 * NOTE! Even if we just add zero, we need a new node
1590 * for the member pointer, since it has a different
1591 * type than the original pointer. We could make that
1592 * be just a cast, but the fact is, a node is a node,
1593 * so we might as well just do the "add zero" here.
1595 add = alloc_expression(expr->pos, EXPR_BINOP);
1596 add->op = '+';
1597 add->left = expr;
1598 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1599 add->right->ctype = &int_ctype;
1600 add->right->value = offset;
1603 * The ctype of the pointer will be lazily evaluated if
1604 * we ever take the address of this member dereference..
1606 add->ctype = &lazy_ptr_ctype;
1607 return add;
1610 /* structure/union dereference */
1611 static struct symbol *evaluate_member_dereference(struct expression *expr)
1613 int offset;
1614 struct symbol *ctype, *member;
1615 struct expression *deref = expr->deref, *add;
1616 struct ident *ident = expr->member;
1617 unsigned int mod;
1618 int address_space;
1620 if (!evaluate_expression(deref))
1621 return NULL;
1622 if (!ident) {
1623 sparse_error(expr->pos, "bad member name");
1624 return NULL;
1627 ctype = deref->ctype;
1628 address_space = ctype->ctype.as;
1629 mod = ctype->ctype.modifiers;
1630 if (ctype->type == SYM_NODE) {
1631 ctype = ctype->ctype.base_type;
1632 address_space |= ctype->ctype.as;
1633 mod |= ctype->ctype.modifiers;
1635 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1636 sparse_error(expr->pos, "expected structure or union");
1637 return NULL;
1639 examine_symbol_type(ctype);
1640 offset = 0;
1641 member = find_identifier(ident, ctype->symbol_list, &offset);
1642 if (!member) {
1643 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1644 const char *name = "<unnamed>";
1645 int namelen = 9;
1646 if (ctype->ident) {
1647 name = ctype->ident->name;
1648 namelen = ctype->ident->len;
1650 sparse_error(expr->pos, "no member '%s' in %s %.*s",
1651 show_ident(ident), type, namelen, name);
1652 return NULL;
1656 * The member needs to take on the address space and modifiers of
1657 * the "parent" type.
1659 member = convert_to_as_mod(member, address_space, mod);
1660 ctype = get_base_type(member);
1662 if (!lvalue_expression(deref)) {
1663 if (deref->type != EXPR_SLICE) {
1664 expr->base = deref;
1665 expr->r_bitpos = 0;
1666 } else {
1667 expr->base = deref->base;
1668 expr->r_bitpos = deref->r_bitpos;
1670 expr->r_bitpos += offset << 3;
1671 expr->type = EXPR_SLICE;
1672 expr->r_nrbits = member->bit_size;
1673 expr->r_bitpos += member->bit_offset;
1674 expr->ctype = member;
1675 return member;
1678 deref = deref->unop;
1679 expr->deref = deref;
1681 add = evaluate_offset(deref, offset);
1682 expr->type = EXPR_PREOP;
1683 expr->op = '*';
1684 expr->unop = add;
1686 expr->ctype = member;
1687 return member;
1690 static int is_promoted(struct expression *expr)
1692 while (1) {
1693 switch (expr->type) {
1694 case EXPR_BINOP:
1695 case EXPR_SELECT:
1696 case EXPR_CONDITIONAL:
1697 return 1;
1698 case EXPR_COMMA:
1699 expr = expr->right;
1700 continue;
1701 case EXPR_PREOP:
1702 switch (expr->op) {
1703 case '(':
1704 expr = expr->unop;
1705 continue;
1706 case '+':
1707 case '-':
1708 case '~':
1709 return 1;
1710 default:
1711 return 0;
1713 default:
1714 return 0;
1720 static struct symbol *evaluate_cast(struct expression *);
1722 static struct symbol *evaluate_type_information(struct expression *expr)
1724 struct symbol *sym = expr->cast_type;
1725 if (!sym) {
1726 sym = evaluate_expression(expr->cast_expression);
1727 if (!sym)
1728 return NULL;
1730 * Expressions of restricted types will possibly get
1731 * promoted - check that here
1733 if (is_restricted_type(sym)) {
1734 if (sym->bit_size < bits_in_int && is_promoted(expr))
1735 sym = &int_ctype;
1738 examine_symbol_type(sym);
1739 if (is_bitfield_type(sym)) {
1740 sparse_error(expr->pos, "trying to examine bitfield type");
1741 return NULL;
1743 return sym;
1746 static struct symbol *evaluate_sizeof(struct expression *expr)
1748 struct symbol *type;
1749 int size;
1751 type = evaluate_type_information(expr);
1752 if (!type)
1753 return NULL;
1755 size = type->bit_size;
1756 if ((size < 0) || (size & 7))
1757 sparse_error(expr->pos, "cannot size expression");
1758 expr->type = EXPR_VALUE;
1759 expr->value = size >> 3;
1760 expr->ctype = size_t_ctype;
1761 return size_t_ctype;
1764 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
1766 struct symbol *type;
1767 int size;
1769 type = evaluate_type_information(expr);
1770 if (!type)
1771 return NULL;
1773 if (type->type == SYM_NODE)
1774 type = type->ctype.base_type;
1775 if (!type)
1776 return NULL;
1777 switch (type->type) {
1778 case SYM_ARRAY:
1779 break;
1780 case SYM_PTR:
1781 type = get_base_type(type);
1782 if (type)
1783 break;
1784 default:
1785 sparse_error(expr->pos, "expected pointer expression");
1786 return NULL;
1788 size = type->bit_size;
1789 if (size & 7)
1790 size = 0;
1791 expr->type = EXPR_VALUE;
1792 expr->value = size >> 3;
1793 expr->ctype = size_t_ctype;
1794 return size_t_ctype;
1797 static struct symbol *evaluate_alignof(struct expression *expr)
1799 struct symbol *type;
1801 type = evaluate_type_information(expr);
1802 if (!type)
1803 return NULL;
1805 expr->type = EXPR_VALUE;
1806 expr->value = type->ctype.alignment;
1807 expr->ctype = size_t_ctype;
1808 return size_t_ctype;
1811 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1813 struct expression *expr;
1814 struct symbol_list *argument_types = fn->arguments;
1815 struct symbol *argtype;
1816 int i = 1;
1818 PREPARE_PTR_LIST(argument_types, argtype);
1819 FOR_EACH_PTR (head, expr) {
1820 struct expression **p = THIS_ADDRESS(expr);
1821 struct symbol *ctype, *target;
1822 ctype = evaluate_expression(expr);
1824 if (!ctype)
1825 return 0;
1827 ctype = degenerate(expr);
1829 target = argtype;
1830 if (!target && ctype->bit_size < bits_in_int)
1831 target = &int_ctype;
1832 if (target) {
1833 static char where[30];
1834 examine_symbol_type(target);
1835 sprintf(where, "argument %d", i);
1836 compatible_assignment_types(expr, target, p, ctype, where, '=');
1839 i++;
1840 NEXT_PTR_LIST(argtype);
1841 } END_FOR_EACH_PTR(expr);
1842 FINISH_PTR_LIST(argtype);
1843 return 1;
1846 static void evaluate_initializer(struct symbol *ctype, struct expression **ep);
1848 static int evaluate_one_array_initializer(struct symbol *ctype, struct expression **ep, int current)
1850 struct expression *entry = *ep;
1851 struct expression **parent, *reuse = NULL;
1852 unsigned long offset;
1853 struct symbol *sym;
1854 unsigned long from, to;
1855 int accept_string = is_byte_type(ctype);
1857 from = current;
1858 to = from+1;
1859 parent = ep;
1860 if (entry->type == EXPR_INDEX) {
1861 from = entry->idx_from;
1862 to = entry->idx_to+1;
1863 parent = &entry->idx_expression;
1864 reuse = entry;
1865 entry = entry->idx_expression;
1868 offset = from * (ctype->bit_size>>3);
1869 if (offset) {
1870 if (!reuse) reuse = alloc_expression(entry->pos, EXPR_POS);
1871 reuse->type = EXPR_POS;
1872 reuse->ctype = ctype;
1873 reuse->init_offset = offset;
1874 reuse->init_nr = to - from;
1875 reuse->init_expr = entry;
1876 parent = &reuse->init_expr;
1877 entry = reuse;
1879 *ep = entry;
1881 if (accept_string && entry->type == EXPR_STRING) {
1882 sym = evaluate_expression(entry);
1883 to = from + get_expression_value(sym->array_size);
1884 } else {
1885 evaluate_initializer(ctype, parent);
1887 return to;
1890 static void evaluate_array_initializer(struct symbol *ctype, struct expression *expr)
1892 struct expression *entry;
1893 int current = 0;
1895 FOR_EACH_PTR(expr->expr_list, entry) {
1896 current = evaluate_one_array_initializer(ctype, THIS_ADDRESS(entry), current);
1897 } END_FOR_EACH_PTR(entry);
1900 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1901 static void evaluate_scalar_initializer(struct symbol *ctype, struct expression *expr)
1903 if (expression_list_size(expr->expr_list) != 1) {
1904 sparse_error(expr->pos, "unexpected compound initializer");
1905 return;
1907 evaluate_array_initializer(ctype, expr);
1908 return;
1911 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
1913 struct symbol *sym;
1915 FOR_EACH_PTR(ctype->symbol_list, sym) {
1916 if (sym->ident == ident)
1917 return sym;
1918 } END_FOR_EACH_PTR(sym);
1919 return NULL;
1922 static int evaluate_one_struct_initializer(struct symbol *ctype, struct expression **ep, struct symbol *sym)
1924 struct expression *entry = *ep;
1925 struct expression **parent;
1926 struct expression *reuse = NULL;
1927 unsigned long offset;
1929 if (!sym) {
1930 sparse_error(entry->pos, "unknown named initializer");
1931 return -1;
1934 if (entry->type == EXPR_IDENTIFIER) {
1935 reuse = entry;
1936 entry = entry->ident_expression;
1939 parent = ep;
1940 offset = sym->offset;
1941 if (offset) {
1942 if (!reuse)
1943 reuse = alloc_expression(entry->pos, EXPR_POS);
1944 reuse->type = EXPR_POS;
1945 reuse->ctype = sym;
1946 reuse->init_offset = offset;
1947 reuse->init_nr = 1;
1948 reuse->init_expr = entry;
1949 parent = &reuse->init_expr;
1950 entry = reuse;
1952 *ep = entry;
1953 evaluate_initializer(sym, parent);
1954 return 0;
1957 static void evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple)
1959 struct expression *entry;
1960 struct symbol *sym;
1962 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1963 FOR_EACH_PTR(expr->expr_list, entry) {
1964 if (entry->type == EXPR_IDENTIFIER) {
1965 struct ident *ident = entry->expr_ident;
1966 /* We special-case the "already right place" case */
1967 if (!sym || sym->ident != ident) {
1968 RESET_PTR_LIST(sym);
1969 for (;;) {
1970 if (!sym)
1971 break;
1972 if (sym->ident == ident)
1973 break;
1974 NEXT_PTR_LIST(sym);
1978 if (evaluate_one_struct_initializer(ctype, THIS_ADDRESS(entry), sym))
1979 return;
1980 NEXT_PTR_LIST(sym);
1981 } END_FOR_EACH_PTR(entry);
1982 FINISH_PTR_LIST(sym);
1986 * Initializers are kind of like assignments. Except
1987 * they can be a hell of a lot more complex.
1989 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
1991 struct expression *expr = *ep;
1994 * Simple non-structure/array initializers are the simple
1995 * case, and look (and parse) largely like assignments.
1997 switch (expr->type) {
1998 default: {
1999 int is_string = expr->type == EXPR_STRING;
2000 struct symbol *rtype = evaluate_expression(expr);
2001 if (rtype) {
2003 * Special case:
2004 * char array[] = "string"
2005 * should _not_ degenerate.
2007 if (!is_string || !is_string_type(ctype))
2008 rtype = degenerate(expr);
2009 compatible_assignment_types(expr, ctype, ep, rtype, "initializer", '=');
2011 return;
2014 case EXPR_INITIALIZER:
2015 expr->ctype = ctype;
2016 if (ctype->type == SYM_NODE)
2017 ctype = ctype->ctype.base_type;
2019 switch (ctype->type) {
2020 case SYM_ARRAY:
2021 case SYM_PTR:
2022 evaluate_array_initializer(get_base_type(ctype), expr);
2023 return;
2024 case SYM_UNION:
2025 evaluate_struct_or_union_initializer(ctype, expr, 0);
2026 return;
2027 case SYM_STRUCT:
2028 evaluate_struct_or_union_initializer(ctype, expr, 1);
2029 return;
2030 default:
2031 evaluate_scalar_initializer(ctype, expr);
2032 return;
2035 case EXPR_IDENTIFIER:
2036 if (ctype->type == SYM_NODE)
2037 ctype = ctype->ctype.base_type;
2038 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2039 sparse_error(expr->pos, "expected structure or union for '%s' dereference", show_ident(expr->expr_ident));
2040 show_symbol(ctype);
2041 return;
2043 evaluate_one_struct_initializer(ctype, ep,
2044 find_struct_ident(ctype, expr->expr_ident));
2045 return;
2047 case EXPR_INDEX:
2048 if (ctype->type == SYM_NODE)
2049 ctype = ctype->ctype.base_type;
2050 if (ctype->type != SYM_ARRAY) {
2051 sparse_error(expr->pos, "expected array");
2052 return;
2054 evaluate_one_array_initializer(ctype->ctype.base_type, ep, 0);
2055 return;
2057 case EXPR_POS:
2059 * An EXPR_POS expression has already been evaluated, and we don't
2060 * need to do anything more
2062 return;
2066 static int get_as(struct symbol *sym)
2068 int as;
2069 unsigned long mod;
2071 if (!sym)
2072 return 0;
2073 as = sym->ctype.as;
2074 mod = sym->ctype.modifiers;
2075 if (sym->type == SYM_NODE) {
2076 sym = sym->ctype.base_type;
2077 as |= sym->ctype.as;
2078 mod |= sym->ctype.modifiers;
2082 * At least for now, allow casting to a "unsigned long".
2083 * That's how we do things like pointer arithmetic and
2084 * store pointers to registers.
2086 if (sym == &ulong_ctype)
2087 return -1;
2089 if (sym && sym->type == SYM_PTR) {
2090 sym = get_base_type(sym);
2091 as |= sym->ctype.as;
2092 mod |= sym->ctype.modifiers;
2094 if (mod & MOD_FORCE)
2095 return -1;
2096 return as;
2099 static void cast_to_as(struct expression *e, int as)
2101 struct expression *v = e->cast_expression;
2103 if (!Wcast_to_address_space)
2104 return;
2106 /* cast from constant 0 to pointer is OK */
2107 if (v->type == EXPR_VALUE && is_int_type(v->ctype) && !v->value)
2108 return;
2110 warning(e->pos, "cast adds address space to expression (<asn:%d>)", as);
2113 static struct symbol *evaluate_cast(struct expression *expr)
2115 struct expression *target = expr->cast_expression;
2116 struct symbol *ctype = examine_symbol_type(expr->cast_type);
2117 struct symbol *t1, *t2;
2118 enum type type1, type2;
2119 int as1, as2;
2121 if (!target)
2122 return NULL;
2124 expr->ctype = ctype;
2125 expr->cast_type = ctype;
2128 * Special case: a cast can be followed by an
2129 * initializer, in which case we need to pass
2130 * the type value down to that initializer rather
2131 * than trying to evaluate it as an expression
2133 * A more complex case is when the initializer is
2134 * dereferenced as part of a post-fix expression.
2135 * We need to produce an expression that can be dereferenced.
2137 if (target->type == EXPR_INITIALIZER) {
2138 struct symbol *sym = expr->cast_type;
2139 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2141 sym->initializer = expr->cast_expression;
2142 evaluate_symbol(sym);
2144 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2145 addr->symbol = sym;
2147 expr->type = EXPR_PREOP;
2148 expr->op = '*';
2149 expr->unop = addr;
2150 expr->ctype = sym;
2152 return sym;
2155 evaluate_expression(target);
2156 degenerate(target);
2158 t1 = ctype;
2159 if (t1->type == SYM_NODE)
2160 t1 = t1->ctype.base_type;
2161 if (t1->type == SYM_ENUM)
2162 t1 = t1->ctype.base_type;
2165 * You can always throw a value away by casting to
2166 * "void" - that's an implicit "force". Note that
2167 * the same is _not_ true of "void *".
2169 if (t1 == &void_ctype)
2170 goto out;
2172 type1 = t1->type;
2173 if (type1 == SYM_ARRAY || type1 == SYM_UNION || type1 == SYM_STRUCT)
2174 warning(expr->pos, "cast to non-scalar");
2176 t2 = target->ctype;
2177 if (!t2) {
2178 sparse_error(expr->pos, "cast from unknown type");
2179 goto out;
2181 if (t2->type == SYM_NODE)
2182 t2 = t2->ctype.base_type;
2183 if (t2->type == SYM_ENUM)
2184 t2 = t2->ctype.base_type;
2186 type2 = t2->type;
2187 if (type2 == SYM_ARRAY || type2 == SYM_UNION || type2 == SYM_STRUCT)
2188 warning(expr->pos, "cast from non-scalar");
2190 if (!(ctype->ctype.modifiers & MOD_FORCE) && t1 != t2) {
2191 if (t1->type == SYM_RESTRICT)
2192 warning(expr->pos, "cast to restricted type");
2193 if (t2->type == SYM_RESTRICT)
2194 warning(expr->pos, "cast from restricted type");
2197 as1 = get_as(ctype);
2198 as2 = get_as(target->ctype);
2199 if (!as1 && as2 > 0)
2200 warning(expr->pos, "cast removes address space of expression");
2201 if (as1 > 0 && as2 > 0 && as1 != as2)
2202 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2203 if (as1 > 0 && !as2)
2204 cast_to_as(expr, as1);
2207 * Casts of constant values are special: they
2208 * can be NULL, and thus need to be simplified
2209 * early.
2211 if (target->type == EXPR_VALUE)
2212 cast_value(expr, ctype, target, target->ctype);
2214 out:
2215 return ctype;
2219 * Evaluate a call expression with a symbol. This
2220 * should expand inline functions, and evaluate
2221 * builtins.
2223 static int evaluate_symbol_call(struct expression *expr)
2225 struct expression *fn = expr->fn;
2226 struct symbol *ctype = fn->ctype;
2228 if (fn->type != EXPR_PREOP)
2229 return 0;
2231 if (ctype->op && ctype->op->evaluate)
2232 return ctype->op->evaluate(expr);
2234 if (ctype->ctype.modifiers & MOD_INLINE) {
2235 int ret;
2236 struct symbol *curr = current_fn;
2237 current_fn = ctype->ctype.base_type;
2238 examine_fn_arguments(current_fn);
2240 ret = inline_function(expr, ctype);
2242 /* restore the old function */
2243 current_fn = curr;
2244 return ret;
2247 return 0;
2250 static struct symbol *evaluate_call(struct expression *expr)
2252 int args, fnargs;
2253 struct symbol *ctype, *sym;
2254 struct expression *fn = expr->fn;
2255 struct expression_list *arglist = expr->args;
2257 if (!evaluate_expression(fn))
2258 return NULL;
2259 sym = ctype = fn->ctype;
2260 if (ctype->type == SYM_NODE)
2261 ctype = ctype->ctype.base_type;
2262 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
2263 ctype = get_base_type(ctype);
2264 if (!evaluate_arguments(sym, ctype, arglist))
2265 return NULL;
2266 if (ctype->type != SYM_FN) {
2267 sparse_error(expr->pos, "not a function %s", show_ident(sym->ident));
2268 return NULL;
2270 args = expression_list_size(expr->args);
2271 fnargs = symbol_list_size(ctype->arguments);
2272 if (args < fnargs)
2273 sparse_error(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
2274 if (args > fnargs && !ctype->variadic)
2275 sparse_error(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
2276 if (sym->type == SYM_NODE) {
2277 if (evaluate_symbol_call(expr))
2278 return expr->ctype;
2280 expr->ctype = ctype->ctype.base_type;
2281 return expr->ctype;
2284 struct symbol *evaluate_expression(struct expression *expr)
2286 if (!expr)
2287 return NULL;
2288 if (expr->ctype)
2289 return expr->ctype;
2291 switch (expr->type) {
2292 case EXPR_VALUE:
2293 case EXPR_FVALUE:
2294 sparse_error(expr->pos, "value expression without a type");
2295 return NULL;
2296 case EXPR_STRING:
2297 return evaluate_string(expr);
2298 case EXPR_SYMBOL:
2299 return evaluate_symbol_expression(expr);
2300 case EXPR_BINOP:
2301 if (!evaluate_expression(expr->left))
2302 return NULL;
2303 if (!evaluate_expression(expr->right))
2304 return NULL;
2305 return evaluate_binop(expr);
2306 case EXPR_LOGICAL:
2307 return evaluate_logical(expr);
2308 case EXPR_COMMA:
2309 evaluate_expression(expr->left);
2310 if (!evaluate_expression(expr->right))
2311 return NULL;
2312 return evaluate_comma(expr);
2313 case EXPR_COMPARE:
2314 if (!evaluate_expression(expr->left))
2315 return NULL;
2316 if (!evaluate_expression(expr->right))
2317 return NULL;
2318 return evaluate_compare(expr);
2319 case EXPR_ASSIGNMENT:
2320 if (!evaluate_expression(expr->left))
2321 return NULL;
2322 if (!evaluate_expression(expr->right))
2323 return NULL;
2324 return evaluate_assignment(expr);
2325 case EXPR_PREOP:
2326 if (!evaluate_expression(expr->unop))
2327 return NULL;
2328 return evaluate_preop(expr);
2329 case EXPR_POSTOP:
2330 if (!evaluate_expression(expr->unop))
2331 return NULL;
2332 return evaluate_postop(expr);
2333 case EXPR_CAST:
2334 case EXPR_IMPLIED_CAST:
2335 return evaluate_cast(expr);
2336 case EXPR_SIZEOF:
2337 return evaluate_sizeof(expr);
2338 case EXPR_PTRSIZEOF:
2339 return evaluate_ptrsizeof(expr);
2340 case EXPR_ALIGNOF:
2341 return evaluate_alignof(expr);
2342 case EXPR_DEREF:
2343 return evaluate_member_dereference(expr);
2344 case EXPR_CALL:
2345 return evaluate_call(expr);
2346 case EXPR_SELECT:
2347 case EXPR_CONDITIONAL:
2348 return evaluate_conditional_expression(expr);
2349 case EXPR_STATEMENT:
2350 expr->ctype = evaluate_statement(expr->statement);
2351 return expr->ctype;
2353 case EXPR_LABEL:
2354 expr->ctype = &ptr_ctype;
2355 return &ptr_ctype;
2357 case EXPR_TYPE:
2358 /* Evaluate the type of the symbol .. */
2359 evaluate_symbol(expr->symbol);
2360 /* .. but the type of the _expression_ is a "type" */
2361 expr->ctype = &type_ctype;
2362 return &type_ctype;
2364 /* These can not exist as stand-alone expressions */
2365 case EXPR_INITIALIZER:
2366 case EXPR_IDENTIFIER:
2367 case EXPR_INDEX:
2368 case EXPR_POS:
2369 sparse_error(expr->pos, "internal front-end error: initializer in expression");
2370 return NULL;
2371 case EXPR_SLICE:
2372 sparse_error(expr->pos, "internal front-end error: SLICE re-evaluated");
2373 return NULL;
2375 return NULL;
2378 static void check_duplicates(struct symbol *sym)
2380 int declared = 0;
2381 struct symbol *next = sym;
2383 while ((next = next->same_symbol) != NULL) {
2384 const char *typediff;
2385 evaluate_symbol(next);
2386 declared++;
2387 typediff = type_difference(sym, next, 0, 0);
2388 if (typediff) {
2389 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2390 show_ident(sym->ident),
2391 stream_name(next->pos.stream), next->pos.line, typediff);
2392 return;
2395 if (!declared) {
2396 unsigned long mod = sym->ctype.modifiers;
2397 if (mod & (MOD_STATIC | MOD_REGISTER))
2398 return;
2399 if (!(mod & MOD_TOPLEVEL))
2400 return;
2401 if (!Wdecl)
2402 return;
2403 if (sym->ident == &main_ident)
2404 return;
2405 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
2409 static struct symbol *evaluate_symbol(struct symbol *sym)
2411 struct symbol *base_type;
2413 if (!sym)
2414 return sym;
2415 if (sym->evaluated)
2416 return sym;
2417 sym->evaluated = 1;
2419 sym = examine_symbol_type(sym);
2420 base_type = get_base_type(sym);
2421 if (!base_type)
2422 return NULL;
2424 /* Evaluate the initializers */
2425 if (sym->initializer)
2426 evaluate_initializer(sym, &sym->initializer);
2428 /* And finally, evaluate the body of the symbol too */
2429 if (base_type->type == SYM_FN) {
2430 struct symbol *curr = current_fn;
2432 current_fn = base_type;
2434 examine_fn_arguments(base_type);
2435 if (!base_type->stmt && base_type->inline_stmt)
2436 uninline(sym);
2437 if (base_type->stmt)
2438 evaluate_statement(base_type->stmt);
2440 current_fn = curr;
2443 return base_type;
2446 void evaluate_symbol_list(struct symbol_list *list)
2448 struct symbol *sym;
2450 FOR_EACH_PTR(list, sym) {
2451 check_duplicates(sym);
2452 evaluate_symbol(sym);
2453 } END_FOR_EACH_PTR(sym);
2456 static struct symbol *evaluate_return_expression(struct statement *stmt)
2458 struct expression *expr = stmt->expression;
2459 struct symbol *ctype, *fntype;
2461 evaluate_expression(expr);
2462 ctype = degenerate(expr);
2463 fntype = current_fn->ctype.base_type;
2464 if (!fntype || fntype == &void_ctype) {
2465 if (expr && ctype != &void_ctype)
2466 sparse_error(expr->pos, "return expression in %s function", fntype?"void":"typeless");
2467 return NULL;
2470 if (!expr) {
2471 sparse_error(stmt->pos, "return with no return value");
2472 return NULL;
2474 if (!ctype)
2475 return NULL;
2476 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression", '=');
2477 return NULL;
2480 static void evaluate_if_statement(struct statement *stmt)
2482 if (!stmt->if_conditional)
2483 return;
2485 evaluate_conditional(stmt->if_conditional, 0);
2486 evaluate_statement(stmt->if_true);
2487 evaluate_statement(stmt->if_false);
2490 static void evaluate_iterator(struct statement *stmt)
2492 evaluate_conditional(stmt->iterator_pre_condition, 1);
2493 evaluate_conditional(stmt->iterator_post_condition,1);
2494 evaluate_statement(stmt->iterator_pre_statement);
2495 evaluate_statement(stmt->iterator_statement);
2496 evaluate_statement(stmt->iterator_post_statement);
2499 static void verify_output_constraint(struct expression *expr, const char *constraint)
2501 switch (*constraint) {
2502 case '=': /* Assignment */
2503 case '+': /* Update */
2504 break;
2505 default:
2506 sparse_error(expr->pos, "output constraint is not an assignment constraint (\"%s\")", constraint);
2510 static void verify_input_constraint(struct expression *expr, const char *constraint)
2512 switch (*constraint) {
2513 case '=': /* Assignment */
2514 case '+': /* Update */
2515 sparse_error(expr->pos, "input constraint with assignment (\"%s\")", constraint);
2519 static void evaluate_asm_statement(struct statement *stmt)
2521 struct expression *expr;
2522 int state;
2524 expr = stmt->asm_string;
2525 if (!expr || expr->type != EXPR_STRING) {
2526 sparse_error(stmt->pos, "need constant string for inline asm");
2527 return;
2530 state = 0;
2531 FOR_EACH_PTR(stmt->asm_outputs, expr) {
2532 struct ident *ident;
2534 switch (state) {
2535 case 0: /* Identifier */
2536 state = 1;
2537 ident = (struct ident *)expr;
2538 continue;
2540 case 1: /* Constraint */
2541 state = 2;
2542 if (!expr || expr->type != EXPR_STRING) {
2543 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
2544 *THIS_ADDRESS(expr) = NULL;
2545 continue;
2547 verify_output_constraint(expr, expr->string->data);
2548 continue;
2550 case 2: /* Expression */
2551 state = 0;
2552 if (!evaluate_expression(expr))
2553 return;
2554 if (!lvalue_expression(expr))
2555 warning(expr->pos, "asm output is not an lvalue");
2556 evaluate_assign_to(expr, expr->ctype);
2557 continue;
2559 } END_FOR_EACH_PTR(expr);
2561 state = 0;
2562 FOR_EACH_PTR(stmt->asm_inputs, expr) {
2563 struct ident *ident;
2565 switch (state) {
2566 case 0: /* Identifier */
2567 state = 1;
2568 ident = (struct ident *)expr;
2569 continue;
2571 case 1: /* Constraint */
2572 state = 2;
2573 if (!expr || expr->type != EXPR_STRING) {
2574 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
2575 *THIS_ADDRESS(expr) = NULL;
2576 continue;
2578 verify_input_constraint(expr, expr->string->data);
2579 continue;
2581 case 2: /* Expression */
2582 state = 0;
2583 if (!evaluate_expression(expr))
2584 return;
2585 continue;
2587 } END_FOR_EACH_PTR(expr);
2589 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
2590 if (!expr) {
2591 sparse_error(stmt->pos, "bad asm output");
2592 return;
2594 if (expr->type == EXPR_STRING)
2595 continue;
2596 sparse_error(expr->pos, "asm clobber is not a string");
2597 } END_FOR_EACH_PTR(expr);
2600 static void evaluate_case_statement(struct statement *stmt)
2602 evaluate_expression(stmt->case_expression);
2603 evaluate_expression(stmt->case_to);
2604 evaluate_statement(stmt->case_statement);
2607 static void check_case_type(struct expression *switch_expr, struct expression *case_expr)
2609 struct symbol *switch_type, *case_type;
2610 if (!case_expr)
2611 return;
2612 switch_type = switch_expr->ctype;
2613 case_type = evaluate_expression(case_expr);
2615 if (case_type && switch_type) {
2616 /* Both integer types? */
2617 if (is_int_type(switch_type) && is_int_type(case_type))
2618 return;
2619 if (compatible_restricted_binop(SPECIAL_EQUAL, &switch_expr, &case_expr))
2620 return;
2623 sparse_error(case_expr->pos, "incompatible types for 'case' statement");
2626 static void evaluate_switch_statement(struct statement *stmt)
2628 struct symbol *sym;
2630 evaluate_expression(stmt->switch_expression);
2631 evaluate_statement(stmt->switch_statement);
2632 if (!stmt->switch_expression)
2633 return;
2634 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
2635 struct statement *case_stmt = sym->stmt;
2636 check_case_type(stmt->switch_expression, case_stmt->case_expression);
2637 check_case_type(stmt->switch_expression, case_stmt->case_to);
2638 } END_FOR_EACH_PTR(sym);
2641 struct symbol *evaluate_statement(struct statement *stmt)
2643 if (!stmt)
2644 return NULL;
2646 switch (stmt->type) {
2647 case STMT_DECLARATION: {
2648 struct symbol *s;
2649 FOR_EACH_PTR(stmt->declaration, s) {
2650 evaluate_symbol(s);
2651 } END_FOR_EACH_PTR(s);
2652 return NULL;
2655 case STMT_RETURN:
2656 return evaluate_return_expression(stmt);
2658 case STMT_EXPRESSION:
2659 if (!evaluate_expression(stmt->expression))
2660 return NULL;
2661 return degenerate(stmt->expression);
2663 case STMT_COMPOUND: {
2664 struct statement *s;
2665 struct symbol *type = NULL;
2667 /* Evaluate the return symbol in the compound statement */
2668 evaluate_symbol(stmt->ret);
2671 * Then, evaluate each statement, making the type of the
2672 * compound statement be the type of the last statement
2674 type = NULL;
2675 FOR_EACH_PTR(stmt->stmts, s) {
2676 type = evaluate_statement(s);
2677 } END_FOR_EACH_PTR(s);
2678 if (!type)
2679 type = &void_ctype;
2680 return type;
2682 case STMT_IF:
2683 evaluate_if_statement(stmt);
2684 return NULL;
2685 case STMT_ITERATOR:
2686 evaluate_iterator(stmt);
2687 return NULL;
2688 case STMT_SWITCH:
2689 evaluate_switch_statement(stmt);
2690 return NULL;
2691 case STMT_CASE:
2692 evaluate_case_statement(stmt);
2693 return NULL;
2694 case STMT_LABEL:
2695 return evaluate_statement(stmt->label_statement);
2696 case STMT_GOTO:
2697 evaluate_expression(stmt->goto_expression);
2698 return NULL;
2699 case STMT_NONE:
2700 break;
2701 case STMT_ASM:
2702 evaluate_asm_statement(stmt);
2703 return NULL;
2704 case STMT_CONTEXT:
2705 evaluate_expression(stmt->expression);
2706 return NULL;
2707 case STMT_RANGE:
2708 evaluate_expression(stmt->range_expression);
2709 evaluate_expression(stmt->range_low);
2710 evaluate_expression(stmt->range_high);
2711 return NULL;
2713 return NULL;