Avoid bogus gcc warnings about unused results
[smatch.git] / evaluate.c
blob42005eb89813aebe80878645caf3fb3ceb1e0b9f
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;
241 static void
242 warn_for_different_enum_types (struct position pos,
243 struct symbol *typea,
244 struct symbol *typeb)
246 if (typea->type == SYM_NODE)
247 typea = typea->ctype.base_type;
248 if (typeb->type == SYM_NODE)
249 typeb = typeb->ctype.base_type;
251 if (typea == typeb)
252 return;
254 if (typea->type == SYM_ENUM && typeb->type == SYM_ENUM)
255 warning(pos, "mixing different enum types");
259 * This gets called for implicit casts in assignments and
260 * integer promotion. We often want to try to move the
261 * cast down, because the ops involved may have been
262 * implicitly cast up, and we can get rid of the casts
263 * early.
265 static struct expression * cast_to(struct expression *old, struct symbol *type)
267 struct expression *expr;
269 warn_for_different_enum_types (old->pos, old->ctype, type);
271 if (is_same_type(old, type))
272 return old;
275 * See if we can simplify the op. Move the cast down.
277 switch (old->type) {
278 case EXPR_PREOP:
279 if (old->ctype->bit_size < type->bit_size)
280 break;
281 if (old->op == '~') {
282 old->ctype = type;
283 old->unop = cast_to(old->unop, type);
284 return old;
286 break;
288 case EXPR_IMPLIED_CAST:
289 warn_for_different_enum_types(old->pos, old->ctype, type);
291 if (old->ctype->bit_size >= type->bit_size) {
292 struct expression *orig = old->cast_expression;
293 if (same_cast_type(orig->ctype, type))
294 return orig;
295 if (old->ctype->bit_offset == type->bit_offset) {
296 old->ctype = type;
297 old->cast_type = type;
298 return old;
301 break;
303 default:
304 /* nothing */;
307 expr = alloc_expression(old->pos, EXPR_IMPLIED_CAST);
308 expr->ctype = type;
309 expr->cast_type = type;
310 expr->cast_expression = old;
311 return expr;
314 static int is_type_type(struct symbol *type)
316 return (type->ctype.modifiers & MOD_TYPE) != 0;
319 int is_ptr_type(struct symbol *type)
321 if (type->type == SYM_NODE)
322 type = type->ctype.base_type;
323 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
326 static inline int is_float_type(struct symbol *type)
328 if (type->type == SYM_NODE)
329 type = type->ctype.base_type;
330 return type->ctype.base_type == &fp_type;
333 static inline int is_byte_type(struct symbol *type)
335 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
338 static inline int is_string_type(struct symbol *type)
340 if (type->type == SYM_NODE)
341 type = type->ctype.base_type;
342 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
345 static struct symbol *bad_expr_type(struct expression *expr)
347 sparse_error(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
348 switch (expr->type) {
349 case EXPR_BINOP:
350 case EXPR_COMPARE:
351 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
352 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
353 break;
354 case EXPR_PREOP:
355 case EXPR_POSTOP:
356 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
357 break;
358 default:
359 break;
362 return expr->ctype = &bad_ctype;
365 static struct symbol *compatible_float_binop(struct expression **lp, struct expression **rp)
367 struct expression *left = *lp, *right = *rp;
368 struct symbol *ltype = left->ctype, *rtype = right->ctype;
370 if (ltype->type == SYM_NODE)
371 ltype = ltype->ctype.base_type;
372 if (rtype->type == SYM_NODE)
373 rtype = rtype->ctype.base_type;
374 if (is_float_type(ltype)) {
375 if (is_int_type(rtype))
376 goto Left;
377 if (is_float_type(rtype)) {
378 unsigned long lmod = ltype->ctype.modifiers;
379 unsigned long rmod = rtype->ctype.modifiers;
380 lmod &= MOD_LONG | MOD_LONGLONG;
381 rmod &= MOD_LONG | MOD_LONGLONG;
382 if (lmod == rmod)
383 return ltype;
384 if (lmod & ~rmod)
385 goto Left;
386 else
387 goto Right;
389 return NULL;
391 if (!is_float_type(rtype) || !is_int_type(ltype))
392 return NULL;
393 Right:
394 *lp = cast_to(left, rtype);
395 return rtype;
396 Left:
397 *rp = cast_to(right, ltype);
398 return ltype;
401 static struct symbol *compatible_integer_binop(struct expression **lp, struct expression **rp)
403 struct expression *left = *lp, *right = *rp;
404 struct symbol *ltype = left->ctype, *rtype = right->ctype;
406 if (ltype->type == SYM_NODE)
407 ltype = ltype->ctype.base_type;
408 if (rtype->type == SYM_NODE)
409 rtype = rtype->ctype.base_type;
410 if (is_int_type(ltype) && is_int_type(rtype)) {
411 struct symbol *ctype = bigger_int_type(ltype, rtype);
413 *lp = cast_to(left, ctype);
414 *rp = cast_to(right, ctype);
415 return ctype;
417 return NULL;
420 static int restricted_value(struct expression *v, struct symbol *type)
422 if (v->type != EXPR_VALUE)
423 return 1;
424 if (v->value != 0)
425 return 1;
426 return 0;
429 static int restricted_binop(int op, struct symbol *type)
431 switch (op) {
432 case '&':
433 case '|':
434 case '^':
435 case '?':
436 case '=':
437 case SPECIAL_EQUAL:
438 case SPECIAL_NOTEQUAL:
439 case SPECIAL_AND_ASSIGN:
440 case SPECIAL_OR_ASSIGN:
441 case SPECIAL_XOR_ASSIGN:
442 return 0;
443 default:
444 return 1;
448 static int restricted_unop(int op, struct symbol *type)
450 if (op == '~' && type->bit_size >= bits_in_int)
451 return 0;
452 if (op == '+')
453 return 0;
454 return 1;
457 static struct symbol *compatible_restricted_binop(int op, struct expression **lp, struct expression **rp)
459 struct expression *left = *lp, *right = *rp;
460 struct symbol *ltype = left->ctype, *rtype = right->ctype;
461 struct symbol *type = NULL;
463 if (ltype->type == SYM_NODE)
464 ltype = ltype->ctype.base_type;
465 if (rtype->type == SYM_NODE)
466 rtype = rtype->ctype.base_type;
468 warn_for_different_enum_types(right->pos, ltype, rtype);
470 if (ltype->type == SYM_ENUM)
471 ltype = ltype->ctype.base_type;
472 if (rtype->type == SYM_ENUM)
473 rtype = rtype->ctype.base_type;
475 if (is_restricted_type(ltype)) {
476 if (is_restricted_type(rtype)) {
477 if (ltype == rtype)
478 type = ltype;
479 } else {
480 if (!restricted_value(right, ltype))
481 type = ltype;
483 } else if (is_restricted_type(rtype)) {
484 if (!restricted_value(left, rtype))
485 type = rtype;
487 if (!type)
488 return NULL;
489 if (restricted_binop(op, type))
490 return NULL;
491 return type;
494 static struct symbol *evaluate_arith(struct expression *expr, int float_ok)
496 struct symbol *ctype = compatible_integer_binop(&expr->left, &expr->right);
497 if (!ctype && float_ok)
498 ctype = compatible_float_binop(&expr->left, &expr->right);
499 if (!ctype)
500 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
501 if (ctype) {
502 expr->ctype = ctype;
503 return ctype;
505 return bad_expr_type(expr);
508 static inline int lvalue_expression(struct expression *expr)
510 return expr->type == EXPR_PREOP && expr->op == '*';
513 static int ptr_object_size(struct symbol *ptr_type)
515 if (ptr_type->type == SYM_NODE)
516 ptr_type = ptr_type->ctype.base_type;
517 if (ptr_type->type == SYM_PTR)
518 ptr_type = get_base_type(ptr_type);
519 return ptr_type->bit_size;
522 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *ctype, struct expression **ip)
524 struct expression *i = *ip;
525 struct symbol *ptr_type = ctype;
526 int bit_size;
528 if (ptr_type->type == SYM_NODE)
529 ptr_type = ptr_type->ctype.base_type;
531 if (!is_int_type(i->ctype))
532 return bad_expr_type(expr);
534 examine_symbol_type(ctype);
536 if (!ctype->ctype.base_type) {
537 sparse_error(expr->pos, "missing type information");
538 return NULL;
541 /* Get the size of whatever the pointer points to */
542 bit_size = ptr_object_size(ctype);
544 if (bit_size > bits_in_char) {
545 int multiply = bit_size >> 3;
546 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
548 if (i->type == EXPR_VALUE) {
549 val->value = i->value * multiply;
550 val->ctype = size_t_ctype;
551 *ip = val;
552 } else {
553 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
555 val->ctype = size_t_ctype;
556 val->value = bit_size >> 3;
558 mul->op = '*';
559 mul->ctype = size_t_ctype;
560 mul->left = i;
561 mul->right = val;
563 *ip = mul;
567 expr->ctype = ctype;
568 return ctype;
571 static struct symbol *evaluate_add(struct expression *expr)
573 struct expression *left = expr->left, *right = expr->right;
574 struct symbol *ltype = left->ctype, *rtype = right->ctype;
576 if (is_ptr_type(ltype))
577 return evaluate_ptr_add(expr, degenerate(left), &expr->right);
579 if (is_ptr_type(rtype))
580 return evaluate_ptr_add(expr, degenerate(right), &expr->left);
582 return evaluate_arith(expr, 1);
585 const char * type_difference(struct symbol *target, struct symbol *source,
586 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
588 for (;;) {
589 unsigned long mod1, mod2, diff;
590 unsigned long as1, as2;
591 int type1, type2;
592 struct symbol *base1, *base2;
594 if (target == source)
595 break;
596 if (!target || !source)
597 return "different types";
599 * Peel of per-node information.
600 * FIXME! Check alignment and context too here!
602 mod1 = target->ctype.modifiers;
603 as1 = target->ctype.as;
604 mod2 = source->ctype.modifiers;
605 as2 = source->ctype.as;
606 if (target->type == SYM_NODE) {
607 target = target->ctype.base_type;
608 if (!target)
609 return "bad types";
610 if (target->type == SYM_PTR) {
611 mod1 = 0;
612 as1 = 0;
614 mod1 |= target->ctype.modifiers;
615 as1 |= target->ctype.as;
617 if (source->type == SYM_NODE) {
618 source = source->ctype.base_type;
619 if (!source)
620 return "bad types";
621 if (source->type == SYM_PTR) {
622 mod2 = 0;
623 as2 = 0;
625 mod2 |= source->ctype.modifiers;
626 as2 |= source->ctype.as;
628 if (target->type == SYM_ENUM) {
629 target = target->ctype.base_type;
630 if (!target)
631 return "bad types";
633 if (source->type == SYM_ENUM) {
634 source = source->ctype.base_type;
635 if (!source)
636 return "bad types";
639 if (target == source)
640 break;
641 if (!target || !source)
642 return "different types";
644 type1 = target->type;
645 base1 = target->ctype.base_type;
647 type2 = source->type;
648 base2 = source->ctype.base_type;
651 * Pointers to functions compare as the function itself
653 if (type1 == SYM_PTR && base1) {
654 base1 = examine_symbol_type(base1);
655 switch (base1->type) {
656 case SYM_FN:
657 type1 = SYM_FN;
658 target = base1;
659 base1 = base1->ctype.base_type;
660 default:
661 /* nothing */;
664 if (type2 == SYM_PTR && base2) {
665 base2 = examine_symbol_type(base2);
666 switch (base2->type) {
667 case SYM_FN:
668 type2 = SYM_FN;
669 source = base2;
670 base2 = base2->ctype.base_type;
671 default:
672 /* nothing */;
676 /* Arrays degenerate to pointers for type comparisons */
677 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
678 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
680 if (type1 != type2 || type1 == SYM_RESTRICT)
681 return "different base types";
683 /* Must be same address space to be comparable */
684 if (as1 != as2)
685 return "different address spaces";
687 /* Ignore differences in storage types or addressability */
688 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
689 diff &= (mod1 & ~target_mod_ignore) | (mod2 & ~source_mod_ignore);
690 if (diff) {
691 if (diff & MOD_SIZE)
692 return "different type sizes";
693 if (diff & ~MOD_SIGNEDNESS)
694 return "different modifiers";
696 /* Differs in signedness only.. */
697 if (Wtypesign) {
699 * Warn if both are explicitly signed ("unsigned" is obvously
700 * always explicit, and since we know one of them has to be
701 * unsigned, we check if the signed one was explicit).
703 if ((mod1 | mod2) & MOD_EXPLICITLY_SIGNED)
704 return "different explicit signedness";
707 * "char" matches both "unsigned char" and "signed char",
708 * so if the explicit test didn't trigger, then we should
709 * not warn about a char.
711 if (!(mod1 & MOD_CHAR))
712 return "different signedness";
716 if (type1 == SYM_FN) {
717 int i;
718 struct symbol *arg1, *arg2;
719 if (base1->variadic != base2->variadic)
720 return "incompatible variadic arguments";
721 PREPARE_PTR_LIST(target->arguments, arg1);
722 PREPARE_PTR_LIST(source->arguments, arg2);
723 i = 1;
724 for (;;) {
725 const char *diff;
726 diff = type_difference(arg1, arg2, 0, 0);
727 if (diff) {
728 static char argdiff[80];
729 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
730 return argdiff;
732 if (!arg1)
733 break;
734 NEXT_PTR_LIST(arg1);
735 NEXT_PTR_LIST(arg2);
736 i++;
738 FINISH_PTR_LIST(arg2);
739 FINISH_PTR_LIST(arg1);
742 target = base1;
743 source = base2;
745 return NULL;
748 static int is_null_ptr(struct expression *expr)
750 if (expr->type != EXPR_VALUE || expr->value)
751 return 0;
752 if (!is_ptr_type(expr->ctype))
753 warning(expr->pos, "Using plain integer as NULL pointer");
754 return 1;
757 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
759 /* NULL expression? Just return the type of the "other side" */
760 if (is_null_ptr(r))
761 return l->ctype;
762 if (is_null_ptr(l))
763 return r->ctype;
764 return NULL;
768 * Ignore differences in "volatile" and "const"ness when
769 * subtracting pointers
771 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
773 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression **rp)
775 const char *typediff;
776 struct symbol *ctype;
777 struct symbol *ltype, *rtype;
778 struct expression *r = *rp;
780 ltype = degenerate(l);
781 rtype = degenerate(r);
784 * If it is an integer subtract: the ptr add case will do the
785 * right thing.
787 if (!is_ptr_type(rtype))
788 return evaluate_ptr_add(expr, degenerate(l), rp);
790 ctype = ltype;
791 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
792 if (typediff) {
793 ctype = common_ptr_type(l, r);
794 if (!ctype) {
795 sparse_error(expr->pos, "subtraction of different types can't work (%s)", typediff);
796 return NULL;
799 examine_symbol_type(ctype);
801 /* Figure out the base type we point to */
802 if (ctype->type == SYM_NODE)
803 ctype = ctype->ctype.base_type;
804 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
805 sparse_error(expr->pos, "subtraction of functions? Share your drugs");
806 return NULL;
808 ctype = get_base_type(ctype);
810 expr->ctype = ssize_t_ctype;
811 if (ctype->bit_size > bits_in_char) {
812 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
813 struct expression *div = expr;
814 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
815 unsigned long value = ctype->bit_size >> 3;
817 val->ctype = size_t_ctype;
818 val->value = value;
820 if (value & (value-1)) {
821 if (Wptr_subtraction_blows)
822 warning(expr->pos, "potentially expensive pointer subtraction");
825 sub->op = '-';
826 sub->ctype = ssize_t_ctype;
827 sub->left = l;
828 sub->right = r;
830 div->op = '/';
831 div->left = sub;
832 div->right = val;
835 return ssize_t_ctype;
838 static struct symbol *evaluate_sub(struct expression *expr)
840 struct expression *left = expr->left;
841 struct symbol *ltype = left->ctype;
843 if (is_ptr_type(ltype))
844 return evaluate_ptr_sub(expr, left, &expr->right);
846 return evaluate_arith(expr, 1);
849 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
851 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
853 struct symbol *ctype;
855 if (!expr)
856 return NULL;
858 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
859 warning(expr->pos, "assignment expression in conditional");
861 ctype = evaluate_expression(expr);
862 if (ctype) {
863 if (is_safe_type(ctype))
864 warning(expr->pos, "testing a 'safe expression'");
867 return ctype;
870 static struct symbol *evaluate_logical(struct expression *expr)
872 if (!evaluate_conditional(expr->left, 0))
873 return NULL;
874 if (!evaluate_conditional(expr->right, 0))
875 return NULL;
877 expr->ctype = &bool_ctype;
878 return &bool_ctype;
881 static struct symbol *evaluate_shift(struct expression *expr)
883 struct expression *left = expr->left, *right = expr->right;
884 struct symbol *ltype = left->ctype, *rtype = right->ctype;
886 if (ltype->type == SYM_NODE)
887 ltype = ltype->ctype.base_type;
888 if (rtype->type == SYM_NODE)
889 rtype = rtype->ctype.base_type;
890 if (is_int_type(ltype) && is_int_type(rtype)) {
891 struct symbol *ctype = integer_promotion(ltype);
892 expr->left = cast_to(expr->left, ctype);
893 expr->ctype = ctype;
894 ctype = integer_promotion(rtype);
895 expr->right = cast_to(expr->right, ctype);
896 return expr->ctype;
898 return bad_expr_type(expr);
901 static struct symbol *evaluate_binop(struct expression *expr)
903 switch (expr->op) {
904 // addition can take ptr+int, fp and int
905 case '+':
906 return evaluate_add(expr);
908 // subtraction can take ptr-ptr, fp and int
909 case '-':
910 return evaluate_sub(expr);
912 // Arithmetic operations can take fp and int
913 case '*': case '/':
914 return evaluate_arith(expr, 1);
916 // shifts do integer promotions, but that's it.
917 case SPECIAL_LEFTSHIFT: case SPECIAL_RIGHTSHIFT:
918 return evaluate_shift(expr);
920 // The rest are integer operations
921 // '%', '&', '^', '|'
922 default:
923 return evaluate_arith(expr, 0);
927 static struct symbol *evaluate_comma(struct expression *expr)
929 expr->ctype = expr->right->ctype;
930 return expr->ctype;
933 static int modify_for_unsigned(int op)
935 if (op == '<')
936 op = SPECIAL_UNSIGNED_LT;
937 else if (op == '>')
938 op = SPECIAL_UNSIGNED_GT;
939 else if (op == SPECIAL_LTE)
940 op = SPECIAL_UNSIGNED_LTE;
941 else if (op == SPECIAL_GTE)
942 op = SPECIAL_UNSIGNED_GTE;
943 return op;
946 static struct symbol *evaluate_compare(struct expression *expr)
948 struct expression *left = expr->left, *right = expr->right;
949 struct symbol *ltype = left->ctype, *rtype = right->ctype;
950 struct symbol *ctype;
952 /* Type types? */
953 if (is_type_type(ltype) && is_type_type(rtype))
954 goto OK;
956 if (is_safe_type(ltype) || is_safe_type(rtype))
957 warning(expr->pos, "testing a 'safe expression'");
959 /* Pointer types? */
960 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
961 // FIXME! Check the types for compatibility
962 expr->op = modify_for_unsigned(expr->op);
963 goto OK;
966 ctype = compatible_integer_binop(&expr->left, &expr->right);
967 if (ctype) {
968 if (ctype->ctype.modifiers & MOD_UNSIGNED)
969 expr->op = modify_for_unsigned(expr->op);
970 goto OK;
973 ctype = compatible_float_binop(&expr->left, &expr->right);
974 if (ctype)
975 goto OK;
977 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
978 if (ctype) {
979 if (ctype->ctype.modifiers & MOD_UNSIGNED)
980 expr->op = modify_for_unsigned(expr->op);
981 goto OK;
984 bad_expr_type(expr);
987 expr->ctype = &bool_ctype;
988 return &bool_ctype;
992 * FIXME!! This should do casts, array degeneration etc..
994 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
996 struct symbol *ltype = left->ctype, *rtype = right->ctype;
998 if (ltype->type == SYM_NODE)
999 ltype = ltype->ctype.base_type;
1001 if (rtype->type == SYM_NODE)
1002 rtype = rtype->ctype.base_type;
1004 if (ltype->type == SYM_PTR) {
1005 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
1006 return ltype;
1009 if (rtype->type == SYM_PTR) {
1010 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
1011 return rtype;
1013 return NULL;
1017 * NOTE! The degenerate case of "x ? : y", where we don't
1018 * have a true case, this will possibly promote "x" to the
1019 * same type as "y", and thus _change_ the conditional
1020 * test in the expression. But since promotion is "safe"
1021 * for testing, that's ok.
1023 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1025 struct expression **true;
1026 struct symbol *ctype, *ltype, *rtype;
1027 const char * typediff;
1029 if (!evaluate_conditional(expr->conditional, 0))
1030 return NULL;
1031 if (!evaluate_expression(expr->cond_false))
1032 return NULL;
1034 ctype = degenerate(expr->conditional);
1035 rtype = degenerate(expr->cond_false);
1037 true = &expr->conditional;
1038 ltype = ctype;
1039 if (expr->cond_true) {
1040 if (!evaluate_expression(expr->cond_true))
1041 return NULL;
1042 ltype = degenerate(expr->cond_true);
1043 true = &expr->cond_true;
1046 ctype = compatible_integer_binop(true, &expr->cond_false);
1047 if (ctype)
1048 goto out;
1049 ctype = compatible_ptr_type(*true, expr->cond_false);
1050 if (ctype)
1051 goto out;
1052 ctype = compatible_float_binop(true, &expr->cond_false);
1053 if (ctype)
1054 goto out;
1055 ctype = compatible_restricted_binop('?', true, &expr->cond_false);
1056 if (ctype)
1057 goto out;
1058 ctype = ltype;
1059 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
1060 if (!typediff)
1061 goto out;
1062 sparse_error(expr->pos, "incompatible types in conditional expression (%s)", typediff);
1063 return NULL;
1065 out:
1066 expr->ctype = ctype;
1067 return ctype;
1070 /* FP assignments can not do modulo or bit operations */
1071 static int compatible_float_op(int op)
1073 return op == '=' ||
1074 op == SPECIAL_ADD_ASSIGN ||
1075 op == SPECIAL_SUB_ASSIGN ||
1076 op == SPECIAL_MUL_ASSIGN ||
1077 op == SPECIAL_DIV_ASSIGN;
1080 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1081 struct expression **rp, struct symbol *source, const char *where, int op)
1083 const char *typediff;
1084 struct symbol *t;
1085 int target_as;
1087 if (is_int_type(target)) {
1088 if (is_int_type(source))
1089 goto Cast;
1090 if (is_float_type(source))
1091 goto Cast;
1092 } else if (is_float_type(target)) {
1093 if (!compatible_float_op(op)) {
1094 sparse_error(expr->pos, "invalid assignment");
1095 return 0;
1097 if (is_int_type(source))
1098 goto Cast;
1099 if (is_float_type(source))
1100 goto Cast;
1101 } else if (is_restricted_type(target)) {
1102 if (restricted_binop(op, target)) {
1103 sparse_error(expr->pos, "bad restricted assignment");
1104 return 0;
1106 if (!restricted_value(*rp, target))
1107 return 1;
1108 } else if (is_ptr_type(target)) {
1109 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1110 evaluate_ptr_add(expr, target, rp);
1111 return 1;
1113 if (op != '=') {
1114 sparse_error(expr->pos, "invalid pointer assignment");
1115 return 0;
1117 } else if (op != '=') {
1118 sparse_error(expr->pos, "invalid assignment");
1119 return 0;
1122 /* It's ok if the target is more volatile or const than the source */
1123 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
1124 if (!typediff)
1125 return 1;
1127 /* Pointer destination? */
1128 t = target;
1129 target_as = t->ctype.as;
1130 if (t->type == SYM_NODE) {
1131 t = t->ctype.base_type;
1132 target_as |= t->ctype.as;
1134 if (t->type == SYM_PTR || t->type == SYM_FN || t->type == SYM_ARRAY) {
1135 struct expression *right = *rp;
1136 struct symbol *s = source;
1137 int source_as;
1139 // NULL pointer is always ok
1140 if (is_null_ptr(right))
1141 goto Cast;
1143 /* "void *" matches anything as long as the address space is ok */
1144 source_as = s->ctype.as;
1145 if (s->type == SYM_NODE) {
1146 s = s->ctype.base_type;
1147 source_as |= s->ctype.as;
1149 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
1150 s = get_base_type(s);
1151 t = get_base_type(t);
1152 if (s == &void_ctype || t == &void_ctype)
1153 goto Cast;
1157 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1158 info(expr->pos, " expected %s", show_typename(target));
1159 info(expr->pos, " got %s", show_typename(source));
1160 *rp = cast_to(*rp, target);
1161 return 0;
1162 Cast:
1163 *rp = cast_to(*rp, target);
1164 return 1;
1167 static void mark_assigned(struct expression *expr)
1169 struct symbol *sym;
1171 if (!expr)
1172 return;
1173 switch (expr->type) {
1174 case EXPR_SYMBOL:
1175 sym = expr->symbol;
1176 if (!sym)
1177 return;
1178 if (sym->type != SYM_NODE)
1179 return;
1180 sym->ctype.modifiers |= MOD_ASSIGNED;
1181 return;
1183 case EXPR_BINOP:
1184 mark_assigned(expr->left);
1185 mark_assigned(expr->right);
1186 return;
1187 case EXPR_CAST:
1188 mark_assigned(expr->cast_expression);
1189 return;
1190 case EXPR_SLICE:
1191 mark_assigned(expr->base);
1192 return;
1193 default:
1194 /* Hmm? */
1195 return;
1199 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1201 if (type->ctype.modifiers & MOD_CONST)
1202 sparse_error(left->pos, "assignment to const expression");
1204 /* We know left is an lvalue, so it's a "preop-*" */
1205 mark_assigned(left->unop);
1208 static struct symbol *evaluate_assignment(struct expression *expr)
1210 struct expression *left = expr->left, *right = expr->right;
1211 struct expression *where = expr;
1212 struct symbol *ltype, *rtype;
1214 if (!lvalue_expression(left)) {
1215 sparse_error(expr->pos, "not an lvalue");
1216 return NULL;
1219 ltype = left->ctype;
1221 rtype = degenerate(right);
1223 if (!compatible_assignment_types(where, ltype, &where->right, rtype, "assignment", expr->op))
1224 return NULL;
1226 evaluate_assign_to(left, ltype);
1228 expr->ctype = ltype;
1229 return ltype;
1232 static void examine_fn_arguments(struct symbol *fn)
1234 struct symbol *s;
1236 FOR_EACH_PTR(fn->arguments, s) {
1237 struct symbol *arg = evaluate_symbol(s);
1238 /* Array/function arguments silently degenerate into pointers */
1239 if (arg) {
1240 struct symbol *ptr;
1241 switch(arg->type) {
1242 case SYM_ARRAY:
1243 case SYM_FN:
1244 ptr = alloc_symbol(s->pos, SYM_PTR);
1245 if (arg->type == SYM_ARRAY)
1246 ptr->ctype = arg->ctype;
1247 else
1248 ptr->ctype.base_type = arg;
1249 ptr->ctype.as |= s->ctype.as;
1250 ptr->ctype.modifiers |= s->ctype.modifiers;
1252 s->ctype.base_type = ptr;
1253 s->ctype.as = 0;
1254 s->ctype.modifiers = 0;
1255 s->bit_size = 0;
1256 s->examined = 0;
1257 examine_symbol_type(s);
1258 break;
1259 default:
1260 /* nothing */
1261 break;
1264 } END_FOR_EACH_PTR(s);
1267 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1269 /* Take the modifiers of the pointer, and apply them to the member */
1270 mod |= sym->ctype.modifiers;
1271 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1272 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1273 *newsym = *sym;
1274 newsym->ctype.as = as;
1275 newsym->ctype.modifiers = mod;
1276 sym = newsym;
1278 return sym;
1281 #define MOD_PTRINHERIT (MOD_VOLATILE | MOD_CONST | MOD_NODEREF | MOD_STORAGE)
1283 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1285 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1286 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1288 node->ctype.base_type = ptr;
1289 ptr->bit_size = bits_in_pointer;
1290 ptr->ctype.alignment = pointer_alignment;
1292 node->bit_size = bits_in_pointer;
1293 node->ctype.alignment = pointer_alignment;
1295 access_symbol(sym);
1296 if (sym->ctype.modifiers & MOD_REGISTER) {
1297 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1298 sym->ctype.modifiers &= ~MOD_REGISTER;
1300 if (sym->type == SYM_NODE) {
1301 ptr->ctype.as |= sym->ctype.as;
1302 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1303 sym = sym->ctype.base_type;
1305 if (degenerate && sym->type == SYM_ARRAY) {
1306 ptr->ctype.as |= sym->ctype.as;
1307 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1308 sym = sym->ctype.base_type;
1310 ptr->ctype.base_type = sym;
1312 return node;
1315 /* Arrays degenerate into pointers on pointer arithmetic */
1316 static struct symbol *degenerate(struct expression *expr)
1318 struct symbol *ctype, *base;
1320 if (!expr)
1321 return NULL;
1322 ctype = expr->ctype;
1323 if (!ctype)
1324 return NULL;
1325 base = examine_symbol_type(ctype);
1326 if (ctype->type == SYM_NODE)
1327 base = ctype->ctype.base_type;
1329 * Arrays degenerate into pointers to the entries, while
1330 * functions degenerate into pointers to themselves.
1331 * If array was part of non-lvalue compound, we create a copy
1332 * of that compound first and then act as if we were dealing with
1333 * the corresponding field in there.
1335 switch (base->type) {
1336 case SYM_ARRAY:
1337 if (expr->type == EXPR_SLICE) {
1338 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1339 struct expression *e0, *e1, *e2, *e3, *e4;
1341 a->ctype.base_type = expr->base->ctype;
1342 a->bit_size = expr->base->ctype->bit_size;
1343 a->array_size = expr->base->ctype->array_size;
1345 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1346 e0->symbol = a;
1347 e0->ctype = &lazy_ptr_ctype;
1349 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1350 e1->unop = e0;
1351 e1->op = '*';
1352 e1->ctype = expr->base->ctype; /* XXX */
1354 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1355 e2->left = e1;
1356 e2->right = expr->base;
1357 e2->op = '=';
1358 e2->ctype = expr->base->ctype;
1360 if (expr->r_bitpos) {
1361 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1362 e3->op = '+';
1363 e3->left = e0;
1364 e3->right = alloc_const_expression(expr->pos,
1365 expr->r_bitpos >> 3);
1366 e3->ctype = &lazy_ptr_ctype;
1367 } else {
1368 e3 = e0;
1371 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1372 e4->left = e2;
1373 e4->right = e3;
1374 e4->ctype = &lazy_ptr_ctype;
1376 expr->unop = e4;
1377 expr->type = EXPR_PREOP;
1378 expr->op = '*';
1380 case SYM_FN:
1381 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1382 sparse_error(expr->pos, "strange non-value function or array");
1383 return &bad_ctype;
1385 *expr = *expr->unop;
1386 ctype = create_pointer(expr, ctype, 1);
1387 expr->ctype = ctype;
1388 default:
1389 /* nothing */;
1391 return ctype;
1394 static struct symbol *evaluate_addressof(struct expression *expr)
1396 struct expression *op = expr->unop;
1397 struct symbol *ctype;
1399 if (op->op != '*' || op->type != EXPR_PREOP) {
1400 sparse_error(expr->pos, "not addressable");
1401 return NULL;
1403 ctype = op->ctype;
1404 *expr = *op->unop;
1406 if (expr->type == EXPR_SYMBOL) {
1407 struct symbol *sym = expr->symbol;
1408 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1412 * symbol expression evaluation is lazy about the type
1413 * of the sub-expression, so we may have to generate
1414 * the type here if so..
1416 if (expr->ctype == &lazy_ptr_ctype) {
1417 ctype = create_pointer(expr, ctype, 0);
1418 expr->ctype = ctype;
1420 return expr->ctype;
1424 static struct symbol *evaluate_dereference(struct expression *expr)
1426 struct expression *op = expr->unop;
1427 struct symbol *ctype = op->ctype, *node, *target;
1429 /* Simplify: *&(expr) => (expr) */
1430 if (op->type == EXPR_PREOP && op->op == '&') {
1431 *expr = *op->unop;
1432 return expr->ctype;
1435 /* Dereferencing a node drops all the node information. */
1436 if (ctype->type == SYM_NODE)
1437 ctype = ctype->ctype.base_type;
1439 node = alloc_symbol(expr->pos, SYM_NODE);
1440 target = ctype->ctype.base_type;
1442 switch (ctype->type) {
1443 default:
1444 sparse_error(expr->pos, "cannot derefence this type");
1445 return NULL;
1446 case SYM_PTR:
1447 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1448 merge_type(node, ctype);
1449 break;
1451 case SYM_ARRAY:
1452 if (!lvalue_expression(op)) {
1453 sparse_error(op->pos, "non-lvalue array??");
1454 return NULL;
1457 /* Do the implied "addressof" on the array */
1458 *op = *op->unop;
1461 * When an array is dereferenced, we need to pick
1462 * up the attributes of the original node too..
1464 merge_type(node, op->ctype);
1465 merge_type(node, ctype);
1466 break;
1469 node->bit_size = target->bit_size;
1470 node->array_size = target->array_size;
1472 expr->ctype = node;
1473 return node;
1477 * Unary post-ops: x++ and x--
1479 static struct symbol *evaluate_postop(struct expression *expr)
1481 struct expression *op = expr->unop;
1482 struct symbol *ctype = op->ctype;
1484 if (!lvalue_expression(expr->unop)) {
1485 sparse_error(expr->pos, "need lvalue expression for ++/--");
1486 return NULL;
1488 if (is_restricted_type(ctype) && restricted_unop(expr->op, ctype)) {
1489 sparse_error(expr->pos, "bad operation on restricted");
1490 return NULL;
1493 evaluate_assign_to(op, ctype);
1495 expr->ctype = ctype;
1496 expr->op_value = 1;
1497 if (is_ptr_type(ctype))
1498 expr->op_value = ptr_object_size(ctype) >> 3;
1500 return ctype;
1503 static struct symbol *evaluate_sign(struct expression *expr)
1505 struct symbol *ctype = expr->unop->ctype;
1506 if (is_int_type(ctype)) {
1507 struct symbol *rtype = rtype = integer_promotion(ctype);
1508 expr->unop = cast_to(expr->unop, rtype);
1509 ctype = rtype;
1510 } else if (is_float_type(ctype) && expr->op != '~') {
1511 /* no conversions needed */
1512 } else if (is_restricted_type(ctype) && !restricted_unop(expr->op, ctype)) {
1513 /* no conversions needed */
1514 } else {
1515 return bad_expr_type(expr);
1517 if (expr->op == '+')
1518 *expr = *expr->unop;
1519 expr->ctype = ctype;
1520 return ctype;
1523 static struct symbol *evaluate_preop(struct expression *expr)
1525 struct symbol *ctype = expr->unop->ctype;
1527 switch (expr->op) {
1528 case '(':
1529 *expr = *expr->unop;
1530 return ctype;
1532 case '+':
1533 case '-':
1534 case '~':
1535 return evaluate_sign(expr);
1537 case '*':
1538 return evaluate_dereference(expr);
1540 case '&':
1541 return evaluate_addressof(expr);
1543 case SPECIAL_INCREMENT:
1544 case SPECIAL_DECREMENT:
1546 * From a type evaluation standpoint the pre-ops are
1547 * the same as the postops
1549 return evaluate_postop(expr);
1551 case '!':
1552 if (is_safe_type(ctype))
1553 warning(expr->pos, "testing a 'safe expression'");
1554 if (is_float_type(ctype)) {
1555 struct expression *arg = expr->unop;
1556 expr->type = EXPR_BINOP;
1557 expr->op = SPECIAL_EQUAL;
1558 expr->left = arg;
1559 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1560 expr->right->ctype = ctype;
1561 expr->right->fvalue = 0;
1563 ctype = &bool_ctype;
1564 break;
1566 default:
1567 break;
1569 expr->ctype = ctype;
1570 return &bool_ctype;
1573 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1575 struct ptr_list *head = (struct ptr_list *)_list;
1576 struct ptr_list *list = head;
1578 if (!head)
1579 return NULL;
1580 do {
1581 int i;
1582 for (i = 0; i < list->nr; i++) {
1583 struct symbol *sym = (struct symbol *) list->list[i];
1584 if (sym->ident) {
1585 if (sym->ident != ident)
1586 continue;
1587 *offset = sym->offset;
1588 return sym;
1589 } else {
1590 struct symbol *ctype = sym->ctype.base_type;
1591 struct symbol *sub;
1592 if (!ctype)
1593 continue;
1594 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1595 continue;
1596 sub = find_identifier(ident, ctype->symbol_list, offset);
1597 if (!sub)
1598 continue;
1599 *offset += sym->offset;
1600 return sub;
1603 } while ((list = list->next) != head);
1604 return NULL;
1607 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1609 struct expression *add;
1612 * Create a new add-expression
1614 * NOTE! Even if we just add zero, we need a new node
1615 * for the member pointer, since it has a different
1616 * type than the original pointer. We could make that
1617 * be just a cast, but the fact is, a node is a node,
1618 * so we might as well just do the "add zero" here.
1620 add = alloc_expression(expr->pos, EXPR_BINOP);
1621 add->op = '+';
1622 add->left = expr;
1623 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1624 add->right->ctype = &int_ctype;
1625 add->right->value = offset;
1628 * The ctype of the pointer will be lazily evaluated if
1629 * we ever take the address of this member dereference..
1631 add->ctype = &lazy_ptr_ctype;
1632 return add;
1635 /* structure/union dereference */
1636 static struct symbol *evaluate_member_dereference(struct expression *expr)
1638 int offset;
1639 struct symbol *ctype, *member;
1640 struct expression *deref = expr->deref, *add;
1641 struct ident *ident = expr->member;
1642 unsigned int mod;
1643 int address_space;
1645 if (!evaluate_expression(deref))
1646 return NULL;
1647 if (!ident) {
1648 sparse_error(expr->pos, "bad member name");
1649 return NULL;
1652 ctype = deref->ctype;
1653 address_space = ctype->ctype.as;
1654 mod = ctype->ctype.modifiers;
1655 if (ctype->type == SYM_NODE) {
1656 ctype = ctype->ctype.base_type;
1657 address_space |= ctype->ctype.as;
1658 mod |= ctype->ctype.modifiers;
1660 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1661 sparse_error(expr->pos, "expected structure or union");
1662 return NULL;
1664 examine_symbol_type(ctype);
1665 offset = 0;
1666 member = find_identifier(ident, ctype->symbol_list, &offset);
1667 if (!member) {
1668 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1669 const char *name = "<unnamed>";
1670 int namelen = 9;
1671 if (ctype->ident) {
1672 name = ctype->ident->name;
1673 namelen = ctype->ident->len;
1675 sparse_error(expr->pos, "no member '%s' in %s %.*s",
1676 show_ident(ident), type, namelen, name);
1677 return NULL;
1681 * The member needs to take on the address space and modifiers of
1682 * the "parent" type.
1684 member = convert_to_as_mod(member, address_space, mod);
1685 ctype = get_base_type(member);
1687 if (!lvalue_expression(deref)) {
1688 if (deref->type != EXPR_SLICE) {
1689 expr->base = deref;
1690 expr->r_bitpos = 0;
1691 } else {
1692 expr->base = deref->base;
1693 expr->r_bitpos = deref->r_bitpos;
1695 expr->r_bitpos += offset << 3;
1696 expr->type = EXPR_SLICE;
1697 expr->r_nrbits = member->bit_size;
1698 expr->r_bitpos += member->bit_offset;
1699 expr->ctype = member;
1700 return member;
1703 deref = deref->unop;
1704 expr->deref = deref;
1706 add = evaluate_offset(deref, offset);
1707 expr->type = EXPR_PREOP;
1708 expr->op = '*';
1709 expr->unop = add;
1711 expr->ctype = member;
1712 return member;
1715 static int is_promoted(struct expression *expr)
1717 while (1) {
1718 switch (expr->type) {
1719 case EXPR_BINOP:
1720 case EXPR_SELECT:
1721 case EXPR_CONDITIONAL:
1722 return 1;
1723 case EXPR_COMMA:
1724 expr = expr->right;
1725 continue;
1726 case EXPR_PREOP:
1727 switch (expr->op) {
1728 case '(':
1729 expr = expr->unop;
1730 continue;
1731 case '+':
1732 case '-':
1733 case '~':
1734 return 1;
1735 default:
1736 return 0;
1738 default:
1739 return 0;
1745 static struct symbol *evaluate_cast(struct expression *);
1747 static struct symbol *evaluate_type_information(struct expression *expr)
1749 struct symbol *sym = expr->cast_type;
1750 if (!sym) {
1751 sym = evaluate_expression(expr->cast_expression);
1752 if (!sym)
1753 return NULL;
1755 * Expressions of restricted types will possibly get
1756 * promoted - check that here
1758 if (is_restricted_type(sym)) {
1759 if (sym->bit_size < bits_in_int && is_promoted(expr))
1760 sym = &int_ctype;
1763 examine_symbol_type(sym);
1764 if (is_bitfield_type(sym)) {
1765 sparse_error(expr->pos, "trying to examine bitfield type");
1766 return NULL;
1768 return sym;
1771 static struct symbol *evaluate_sizeof(struct expression *expr)
1773 struct symbol *type;
1774 int size;
1776 type = evaluate_type_information(expr);
1777 if (!type)
1778 return NULL;
1780 size = type->bit_size;
1781 if ((size < 0) || (size & 7))
1782 sparse_error(expr->pos, "cannot size expression");
1783 expr->type = EXPR_VALUE;
1784 expr->value = size >> 3;
1785 expr->ctype = size_t_ctype;
1786 return size_t_ctype;
1789 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
1791 struct symbol *type;
1792 int size;
1794 type = evaluate_type_information(expr);
1795 if (!type)
1796 return NULL;
1798 if (type->type == SYM_NODE)
1799 type = type->ctype.base_type;
1800 if (!type)
1801 return NULL;
1802 switch (type->type) {
1803 case SYM_ARRAY:
1804 break;
1805 case SYM_PTR:
1806 type = get_base_type(type);
1807 if (type)
1808 break;
1809 default:
1810 sparse_error(expr->pos, "expected pointer expression");
1811 return NULL;
1813 size = type->bit_size;
1814 if (size & 7)
1815 size = 0;
1816 expr->type = EXPR_VALUE;
1817 expr->value = size >> 3;
1818 expr->ctype = size_t_ctype;
1819 return size_t_ctype;
1822 static struct symbol *evaluate_alignof(struct expression *expr)
1824 struct symbol *type;
1826 type = evaluate_type_information(expr);
1827 if (!type)
1828 return NULL;
1830 expr->type = EXPR_VALUE;
1831 expr->value = type->ctype.alignment;
1832 expr->ctype = size_t_ctype;
1833 return size_t_ctype;
1836 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1838 struct expression *expr;
1839 struct symbol_list *argument_types = fn->arguments;
1840 struct symbol *argtype;
1841 int i = 1;
1843 PREPARE_PTR_LIST(argument_types, argtype);
1844 FOR_EACH_PTR (head, expr) {
1845 struct expression **p = THIS_ADDRESS(expr);
1846 struct symbol *ctype, *target;
1847 ctype = evaluate_expression(expr);
1849 if (!ctype)
1850 return 0;
1852 ctype = degenerate(expr);
1854 target = argtype;
1855 if (!target && ctype->bit_size < bits_in_int)
1856 target = &int_ctype;
1857 if (target) {
1858 static char where[30];
1859 examine_symbol_type(target);
1860 sprintf(where, "argument %d", i);
1861 compatible_assignment_types(expr, target, p, ctype, where, '=');
1864 i++;
1865 NEXT_PTR_LIST(argtype);
1866 } END_FOR_EACH_PTR(expr);
1867 FINISH_PTR_LIST(argtype);
1868 return 1;
1871 static void evaluate_initializer(struct symbol *ctype, struct expression **ep);
1873 static int evaluate_one_array_initializer(struct symbol *ctype, struct expression **ep, int current)
1875 struct expression *entry = *ep;
1876 struct expression **parent, *reuse = NULL;
1877 unsigned long offset;
1878 struct symbol *sym;
1879 unsigned long from, to;
1880 int accept_string = is_byte_type(ctype);
1882 from = current;
1883 to = from+1;
1884 parent = ep;
1885 if (entry->type == EXPR_INDEX) {
1886 from = entry->idx_from;
1887 to = entry->idx_to+1;
1888 parent = &entry->idx_expression;
1889 reuse = entry;
1890 entry = entry->idx_expression;
1893 offset = from * (ctype->bit_size>>3);
1894 if (offset) {
1895 if (!reuse) reuse = alloc_expression(entry->pos, EXPR_POS);
1896 reuse->type = EXPR_POS;
1897 reuse->ctype = ctype;
1898 reuse->init_offset = offset;
1899 reuse->init_nr = to - from;
1900 reuse->init_expr = entry;
1901 parent = &reuse->init_expr;
1902 entry = reuse;
1904 *ep = entry;
1906 if (accept_string && entry->type == EXPR_STRING) {
1907 sym = evaluate_expression(entry);
1908 to = from + get_expression_value(sym->array_size);
1909 } else {
1910 evaluate_initializer(ctype, parent);
1912 return to;
1915 static void evaluate_array_initializer(struct symbol *ctype, struct expression *expr)
1917 struct expression *entry;
1918 int current = 0;
1920 FOR_EACH_PTR(expr->expr_list, entry) {
1921 current = evaluate_one_array_initializer(ctype, THIS_ADDRESS(entry), current);
1922 } END_FOR_EACH_PTR(entry);
1925 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1926 static void evaluate_scalar_initializer(struct symbol *ctype, struct expression *expr)
1928 if (expression_list_size(expr->expr_list) != 1) {
1929 sparse_error(expr->pos, "unexpected compound initializer");
1930 return;
1932 evaluate_array_initializer(ctype, expr);
1933 return;
1936 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
1938 struct symbol *sym;
1940 FOR_EACH_PTR(ctype->symbol_list, sym) {
1941 if (sym->ident == ident)
1942 return sym;
1943 } END_FOR_EACH_PTR(sym);
1944 return NULL;
1947 static int evaluate_one_struct_initializer(struct symbol *ctype, struct expression **ep, struct symbol *sym)
1949 struct expression *entry = *ep;
1950 struct expression **parent;
1951 struct expression *reuse = NULL;
1952 unsigned long offset;
1954 if (!sym) {
1955 sparse_error(entry->pos, "unknown named initializer");
1956 return -1;
1959 if (entry->type == EXPR_IDENTIFIER) {
1960 reuse = entry;
1961 entry = entry->ident_expression;
1964 parent = ep;
1965 offset = sym->offset;
1966 if (offset) {
1967 if (!reuse)
1968 reuse = alloc_expression(entry->pos, EXPR_POS);
1969 reuse->type = EXPR_POS;
1970 reuse->ctype = sym;
1971 reuse->init_offset = offset;
1972 reuse->init_nr = 1;
1973 reuse->init_expr = entry;
1974 parent = &reuse->init_expr;
1975 entry = reuse;
1977 *ep = entry;
1978 evaluate_initializer(sym, parent);
1979 return 0;
1982 static void evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple)
1984 struct expression *entry;
1985 struct symbol *sym;
1987 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1988 FOR_EACH_PTR(expr->expr_list, entry) {
1989 if (entry->type == EXPR_IDENTIFIER) {
1990 struct ident *ident = entry->expr_ident;
1991 /* We special-case the "already right place" case */
1992 if (!sym || sym->ident != ident) {
1993 RESET_PTR_LIST(sym);
1994 for (;;) {
1995 if (!sym)
1996 break;
1997 if (sym->ident == ident)
1998 break;
1999 NEXT_PTR_LIST(sym);
2003 if (evaluate_one_struct_initializer(ctype, THIS_ADDRESS(entry), sym))
2004 return;
2005 NEXT_PTR_LIST(sym);
2006 } END_FOR_EACH_PTR(entry);
2007 FINISH_PTR_LIST(sym);
2011 * Initializers are kind of like assignments. Except
2012 * they can be a hell of a lot more complex.
2014 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2016 struct expression *expr = *ep;
2019 * Simple non-structure/array initializers are the simple
2020 * case, and look (and parse) largely like assignments.
2022 switch (expr->type) {
2023 default: {
2024 int is_string = expr->type == EXPR_STRING;
2025 struct symbol *rtype = evaluate_expression(expr);
2026 if (rtype) {
2028 * Special case:
2029 * char array[] = "string"
2030 * should _not_ degenerate.
2032 if (!is_string || !is_string_type(ctype))
2033 rtype = degenerate(expr);
2034 compatible_assignment_types(expr, ctype, ep, rtype, "initializer", '=');
2036 return;
2039 case EXPR_INITIALIZER:
2040 expr->ctype = ctype;
2041 if (ctype->type == SYM_NODE)
2042 ctype = ctype->ctype.base_type;
2044 switch (ctype->type) {
2045 case SYM_ARRAY:
2046 case SYM_PTR:
2047 evaluate_array_initializer(get_base_type(ctype), expr);
2048 return;
2049 case SYM_UNION:
2050 evaluate_struct_or_union_initializer(ctype, expr, 0);
2051 return;
2052 case SYM_STRUCT:
2053 evaluate_struct_or_union_initializer(ctype, expr, 1);
2054 return;
2055 default:
2056 evaluate_scalar_initializer(ctype, expr);
2057 return;
2060 case EXPR_IDENTIFIER:
2061 if (ctype->type == SYM_NODE)
2062 ctype = ctype->ctype.base_type;
2063 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2064 sparse_error(expr->pos, "expected structure or union for '%s' dereference", show_ident(expr->expr_ident));
2065 show_symbol(ctype);
2066 return;
2068 evaluate_one_struct_initializer(ctype, ep,
2069 find_struct_ident(ctype, expr->expr_ident));
2070 return;
2072 case EXPR_INDEX:
2073 if (ctype->type == SYM_NODE)
2074 ctype = ctype->ctype.base_type;
2075 if (ctype->type != SYM_ARRAY) {
2076 sparse_error(expr->pos, "expected array");
2077 return;
2079 evaluate_one_array_initializer(ctype->ctype.base_type, ep, 0);
2080 return;
2082 case EXPR_POS:
2084 * An EXPR_POS expression has already been evaluated, and we don't
2085 * need to do anything more
2087 return;
2091 static int get_as(struct symbol *sym)
2093 int as;
2094 unsigned long mod;
2096 if (!sym)
2097 return 0;
2098 as = sym->ctype.as;
2099 mod = sym->ctype.modifiers;
2100 if (sym->type == SYM_NODE) {
2101 sym = sym->ctype.base_type;
2102 as |= sym->ctype.as;
2103 mod |= sym->ctype.modifiers;
2107 * At least for now, allow casting to a "unsigned long".
2108 * That's how we do things like pointer arithmetic and
2109 * store pointers to registers.
2111 if (sym == &ulong_ctype)
2112 return -1;
2114 if (sym && sym->type == SYM_PTR) {
2115 sym = get_base_type(sym);
2116 as |= sym->ctype.as;
2117 mod |= sym->ctype.modifiers;
2119 if (mod & MOD_FORCE)
2120 return -1;
2121 return as;
2124 static void cast_to_as(struct expression *e, int as)
2126 struct expression *v = e->cast_expression;
2128 if (!Wcast_to_address_space)
2129 return;
2131 /* cast from constant 0 to pointer is OK */
2132 if (v->type == EXPR_VALUE && is_int_type(v->ctype) && !v->value)
2133 return;
2135 warning(e->pos, "cast adds address space to expression (<asn:%d>)", as);
2138 static struct symbol *evaluate_cast(struct expression *expr)
2140 struct expression *target = expr->cast_expression;
2141 struct symbol *ctype = examine_symbol_type(expr->cast_type);
2142 struct symbol *t1, *t2;
2143 enum type type1, type2;
2144 int as1, as2;
2146 if (!target)
2147 return NULL;
2149 expr->ctype = ctype;
2150 expr->cast_type = ctype;
2153 * Special case: a cast can be followed by an
2154 * initializer, in which case we need to pass
2155 * the type value down to that initializer rather
2156 * than trying to evaluate it as an expression
2158 * A more complex case is when the initializer is
2159 * dereferenced as part of a post-fix expression.
2160 * We need to produce an expression that can be dereferenced.
2162 if (target->type == EXPR_INITIALIZER) {
2163 struct symbol *sym = expr->cast_type;
2164 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2166 sym->initializer = expr->cast_expression;
2167 evaluate_symbol(sym);
2169 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2170 addr->symbol = sym;
2172 expr->type = EXPR_PREOP;
2173 expr->op = '*';
2174 expr->unop = addr;
2175 expr->ctype = sym;
2177 return sym;
2180 evaluate_expression(target);
2181 degenerate(target);
2183 t1 = ctype;
2184 if (t1->type == SYM_NODE)
2185 t1 = t1->ctype.base_type;
2186 if (t1->type == SYM_ENUM)
2187 t1 = t1->ctype.base_type;
2190 * You can always throw a value away by casting to
2191 * "void" - that's an implicit "force". Note that
2192 * the same is _not_ true of "void *".
2194 if (t1 == &void_ctype)
2195 goto out;
2197 type1 = t1->type;
2198 if (type1 == SYM_ARRAY || type1 == SYM_UNION || type1 == SYM_STRUCT)
2199 warning(expr->pos, "cast to non-scalar");
2201 t2 = target->ctype;
2202 if (!t2) {
2203 sparse_error(expr->pos, "cast from unknown type");
2204 goto out;
2206 if (t2->type == SYM_NODE)
2207 t2 = t2->ctype.base_type;
2208 if (t2->type == SYM_ENUM)
2209 t2 = t2->ctype.base_type;
2211 type2 = t2->type;
2212 if (type2 == SYM_ARRAY || type2 == SYM_UNION || type2 == SYM_STRUCT)
2213 warning(expr->pos, "cast from non-scalar");
2215 if (!(ctype->ctype.modifiers & MOD_FORCE) && t1 != t2) {
2216 if (t1->type == SYM_RESTRICT)
2217 warning(expr->pos, "cast to restricted type");
2218 if (t2->type == SYM_RESTRICT)
2219 warning(expr->pos, "cast from restricted type");
2222 as1 = get_as(ctype);
2223 as2 = get_as(target->ctype);
2224 if (!as1 && as2 > 0)
2225 warning(expr->pos, "cast removes address space of expression");
2226 if (as1 > 0 && as2 > 0 && as1 != as2)
2227 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2228 if (as1 > 0 && !as2)
2229 cast_to_as(expr, as1);
2232 * Casts of constant values are special: they
2233 * can be NULL, and thus need to be simplified
2234 * early.
2236 if (target->type == EXPR_VALUE)
2237 cast_value(expr, ctype, target, target->ctype);
2239 out:
2240 return ctype;
2244 * Evaluate a call expression with a symbol. This
2245 * should expand inline functions, and evaluate
2246 * builtins.
2248 static int evaluate_symbol_call(struct expression *expr)
2250 struct expression *fn = expr->fn;
2251 struct symbol *ctype = fn->ctype;
2253 if (fn->type != EXPR_PREOP)
2254 return 0;
2256 if (ctype->op && ctype->op->evaluate)
2257 return ctype->op->evaluate(expr);
2259 if (ctype->ctype.modifiers & MOD_INLINE) {
2260 int ret;
2261 struct symbol *curr = current_fn;
2262 current_fn = ctype->ctype.base_type;
2263 examine_fn_arguments(current_fn);
2265 ret = inline_function(expr, ctype);
2267 /* restore the old function */
2268 current_fn = curr;
2269 return ret;
2272 return 0;
2275 static struct symbol *evaluate_call(struct expression *expr)
2277 int args, fnargs;
2278 struct symbol *ctype, *sym;
2279 struct expression *fn = expr->fn;
2280 struct expression_list *arglist = expr->args;
2282 if (!evaluate_expression(fn))
2283 return NULL;
2284 sym = ctype = fn->ctype;
2285 if (ctype->type == SYM_NODE)
2286 ctype = ctype->ctype.base_type;
2287 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
2288 ctype = get_base_type(ctype);
2289 if (!evaluate_arguments(sym, ctype, arglist))
2290 return NULL;
2291 if (ctype->type != SYM_FN) {
2292 sparse_error(expr->pos, "not a function %s", show_ident(sym->ident));
2293 return NULL;
2295 args = expression_list_size(expr->args);
2296 fnargs = symbol_list_size(ctype->arguments);
2297 if (args < fnargs)
2298 sparse_error(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
2299 if (args > fnargs && !ctype->variadic)
2300 sparse_error(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
2301 if (sym->type == SYM_NODE) {
2302 if (evaluate_symbol_call(expr))
2303 return expr->ctype;
2305 expr->ctype = ctype->ctype.base_type;
2306 return expr->ctype;
2309 struct symbol *evaluate_expression(struct expression *expr)
2311 if (!expr)
2312 return NULL;
2313 if (expr->ctype)
2314 return expr->ctype;
2316 switch (expr->type) {
2317 case EXPR_VALUE:
2318 case EXPR_FVALUE:
2319 sparse_error(expr->pos, "value expression without a type");
2320 return NULL;
2321 case EXPR_STRING:
2322 return evaluate_string(expr);
2323 case EXPR_SYMBOL:
2324 return evaluate_symbol_expression(expr);
2325 case EXPR_BINOP:
2326 if (!evaluate_expression(expr->left))
2327 return NULL;
2328 if (!evaluate_expression(expr->right))
2329 return NULL;
2330 return evaluate_binop(expr);
2331 case EXPR_LOGICAL:
2332 return evaluate_logical(expr);
2333 case EXPR_COMMA:
2334 evaluate_expression(expr->left);
2335 if (!evaluate_expression(expr->right))
2336 return NULL;
2337 return evaluate_comma(expr);
2338 case EXPR_COMPARE:
2339 if (!evaluate_expression(expr->left))
2340 return NULL;
2341 if (!evaluate_expression(expr->right))
2342 return NULL;
2343 return evaluate_compare(expr);
2344 case EXPR_ASSIGNMENT:
2345 if (!evaluate_expression(expr->left))
2346 return NULL;
2347 if (!evaluate_expression(expr->right))
2348 return NULL;
2349 return evaluate_assignment(expr);
2350 case EXPR_PREOP:
2351 if (!evaluate_expression(expr->unop))
2352 return NULL;
2353 return evaluate_preop(expr);
2354 case EXPR_POSTOP:
2355 if (!evaluate_expression(expr->unop))
2356 return NULL;
2357 return evaluate_postop(expr);
2358 case EXPR_CAST:
2359 case EXPR_IMPLIED_CAST:
2360 return evaluate_cast(expr);
2361 case EXPR_SIZEOF:
2362 return evaluate_sizeof(expr);
2363 case EXPR_PTRSIZEOF:
2364 return evaluate_ptrsizeof(expr);
2365 case EXPR_ALIGNOF:
2366 return evaluate_alignof(expr);
2367 case EXPR_DEREF:
2368 return evaluate_member_dereference(expr);
2369 case EXPR_CALL:
2370 return evaluate_call(expr);
2371 case EXPR_SELECT:
2372 case EXPR_CONDITIONAL:
2373 return evaluate_conditional_expression(expr);
2374 case EXPR_STATEMENT:
2375 expr->ctype = evaluate_statement(expr->statement);
2376 return expr->ctype;
2378 case EXPR_LABEL:
2379 expr->ctype = &ptr_ctype;
2380 return &ptr_ctype;
2382 case EXPR_TYPE:
2383 /* Evaluate the type of the symbol .. */
2384 evaluate_symbol(expr->symbol);
2385 /* .. but the type of the _expression_ is a "type" */
2386 expr->ctype = &type_ctype;
2387 return &type_ctype;
2389 /* These can not exist as stand-alone expressions */
2390 case EXPR_INITIALIZER:
2391 case EXPR_IDENTIFIER:
2392 case EXPR_INDEX:
2393 case EXPR_POS:
2394 sparse_error(expr->pos, "internal front-end error: initializer in expression");
2395 return NULL;
2396 case EXPR_SLICE:
2397 sparse_error(expr->pos, "internal front-end error: SLICE re-evaluated");
2398 return NULL;
2400 return NULL;
2403 static void check_duplicates(struct symbol *sym)
2405 int declared = 0;
2406 struct symbol *next = sym;
2408 while ((next = next->same_symbol) != NULL) {
2409 const char *typediff;
2410 evaluate_symbol(next);
2411 declared++;
2412 typediff = type_difference(sym, next, 0, 0);
2413 if (typediff) {
2414 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2415 show_ident(sym->ident),
2416 stream_name(next->pos.stream), next->pos.line, typediff);
2417 return;
2420 if (!declared) {
2421 unsigned long mod = sym->ctype.modifiers;
2422 if (mod & (MOD_STATIC | MOD_REGISTER))
2423 return;
2424 if (!(mod & MOD_TOPLEVEL))
2425 return;
2426 if (!Wdecl)
2427 return;
2428 if (sym->ident == &main_ident)
2429 return;
2430 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
2434 static struct symbol *evaluate_symbol(struct symbol *sym)
2436 struct symbol *base_type;
2438 if (!sym)
2439 return sym;
2440 if (sym->evaluated)
2441 return sym;
2442 sym->evaluated = 1;
2444 sym = examine_symbol_type(sym);
2445 base_type = get_base_type(sym);
2446 if (!base_type)
2447 return NULL;
2449 /* Evaluate the initializers */
2450 if (sym->initializer)
2451 evaluate_initializer(sym, &sym->initializer);
2453 /* And finally, evaluate the body of the symbol too */
2454 if (base_type->type == SYM_FN) {
2455 struct symbol *curr = current_fn;
2457 current_fn = base_type;
2459 examine_fn_arguments(base_type);
2460 if (!base_type->stmt && base_type->inline_stmt)
2461 uninline(sym);
2462 if (base_type->stmt)
2463 evaluate_statement(base_type->stmt);
2465 current_fn = curr;
2468 return base_type;
2471 void evaluate_symbol_list(struct symbol_list *list)
2473 struct symbol *sym;
2475 FOR_EACH_PTR(list, sym) {
2476 check_duplicates(sym);
2477 evaluate_symbol(sym);
2478 } END_FOR_EACH_PTR(sym);
2481 static struct symbol *evaluate_return_expression(struct statement *stmt)
2483 struct expression *expr = stmt->expression;
2484 struct symbol *ctype, *fntype;
2486 evaluate_expression(expr);
2487 ctype = degenerate(expr);
2488 fntype = current_fn->ctype.base_type;
2489 if (!fntype || fntype == &void_ctype) {
2490 if (expr && ctype != &void_ctype)
2491 sparse_error(expr->pos, "return expression in %s function", fntype?"void":"typeless");
2492 return NULL;
2495 if (!expr) {
2496 sparse_error(stmt->pos, "return with no return value");
2497 return NULL;
2499 if (!ctype)
2500 return NULL;
2501 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression", '=');
2502 return NULL;
2505 static void evaluate_if_statement(struct statement *stmt)
2507 if (!stmt->if_conditional)
2508 return;
2510 evaluate_conditional(stmt->if_conditional, 0);
2511 evaluate_statement(stmt->if_true);
2512 evaluate_statement(stmt->if_false);
2515 static void evaluate_iterator(struct statement *stmt)
2517 evaluate_conditional(stmt->iterator_pre_condition, 1);
2518 evaluate_conditional(stmt->iterator_post_condition,1);
2519 evaluate_statement(stmt->iterator_pre_statement);
2520 evaluate_statement(stmt->iterator_statement);
2521 evaluate_statement(stmt->iterator_post_statement);
2524 static void verify_output_constraint(struct expression *expr, const char *constraint)
2526 switch (*constraint) {
2527 case '=': /* Assignment */
2528 case '+': /* Update */
2529 break;
2530 default:
2531 sparse_error(expr->pos, "output constraint is not an assignment constraint (\"%s\")", constraint);
2535 static void verify_input_constraint(struct expression *expr, const char *constraint)
2537 switch (*constraint) {
2538 case '=': /* Assignment */
2539 case '+': /* Update */
2540 sparse_error(expr->pos, "input constraint with assignment (\"%s\")", constraint);
2544 static void evaluate_asm_statement(struct statement *stmt)
2546 struct expression *expr;
2547 int state;
2549 expr = stmt->asm_string;
2550 if (!expr || expr->type != EXPR_STRING) {
2551 sparse_error(stmt->pos, "need constant string for inline asm");
2552 return;
2555 state = 0;
2556 FOR_EACH_PTR(stmt->asm_outputs, expr) {
2557 struct ident *ident;
2559 switch (state) {
2560 case 0: /* Identifier */
2561 state = 1;
2562 ident = (struct ident *)expr;
2563 continue;
2565 case 1: /* Constraint */
2566 state = 2;
2567 if (!expr || expr->type != EXPR_STRING) {
2568 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
2569 *THIS_ADDRESS(expr) = NULL;
2570 continue;
2572 verify_output_constraint(expr, expr->string->data);
2573 continue;
2575 case 2: /* Expression */
2576 state = 0;
2577 if (!evaluate_expression(expr))
2578 return;
2579 if (!lvalue_expression(expr))
2580 warning(expr->pos, "asm output is not an lvalue");
2581 evaluate_assign_to(expr, expr->ctype);
2582 continue;
2584 } END_FOR_EACH_PTR(expr);
2586 state = 0;
2587 FOR_EACH_PTR(stmt->asm_inputs, expr) {
2588 struct ident *ident;
2590 switch (state) {
2591 case 0: /* Identifier */
2592 state = 1;
2593 ident = (struct ident *)expr;
2594 continue;
2596 case 1: /* Constraint */
2597 state = 2;
2598 if (!expr || expr->type != EXPR_STRING) {
2599 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
2600 *THIS_ADDRESS(expr) = NULL;
2601 continue;
2603 verify_input_constraint(expr, expr->string->data);
2604 continue;
2606 case 2: /* Expression */
2607 state = 0;
2608 if (!evaluate_expression(expr))
2609 return;
2610 continue;
2612 } END_FOR_EACH_PTR(expr);
2614 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
2615 if (!expr) {
2616 sparse_error(stmt->pos, "bad asm output");
2617 return;
2619 if (expr->type == EXPR_STRING)
2620 continue;
2621 sparse_error(expr->pos, "asm clobber is not a string");
2622 } END_FOR_EACH_PTR(expr);
2625 static void evaluate_case_statement(struct statement *stmt)
2627 evaluate_expression(stmt->case_expression);
2628 evaluate_expression(stmt->case_to);
2629 evaluate_statement(stmt->case_statement);
2632 static void check_case_type(struct expression *switch_expr,
2633 struct expression *case_expr,
2634 struct expression **enumcase)
2636 struct symbol *switch_type, *case_type;
2637 if (!case_expr)
2638 return;
2639 switch_type = switch_expr->ctype;
2640 case_type = evaluate_expression(case_expr);
2642 if (case_type && switch_type) {
2643 if (enumcase) {
2644 if (*enumcase)
2645 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
2646 else if (is_enum_type(case_type))
2647 *enumcase = case_expr;
2650 /* Both integer types? */
2651 if (compatible_restricted_binop(SPECIAL_EQUAL, &switch_expr, &case_expr))
2652 return;
2653 if (is_int_type(switch_type) && is_int_type(case_type))
2654 return;
2657 sparse_error(case_expr->pos, "incompatible types for 'case' statement");
2660 static void evaluate_switch_statement(struct statement *stmt)
2662 struct symbol *sym;
2663 struct expression *enumcase = NULL;
2664 struct expression **enumcase_holder;
2666 evaluate_expression(stmt->switch_expression);
2667 evaluate_statement(stmt->switch_statement);
2668 if (!stmt->switch_expression)
2669 return;
2670 enumcase_holder = is_enum_type(stmt->switch_expression->ctype)
2671 ? NULL /* Only check cases against switch */
2672 : &enumcase;
2674 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
2675 struct statement *case_stmt = sym->stmt;
2676 check_case_type(stmt->switch_expression, case_stmt->case_expression, enumcase_holder);
2677 check_case_type(stmt->switch_expression, case_stmt->case_to, enumcase_holder);
2678 } END_FOR_EACH_PTR(sym);
2681 struct symbol *evaluate_statement(struct statement *stmt)
2683 if (!stmt)
2684 return NULL;
2686 switch (stmt->type) {
2687 case STMT_DECLARATION: {
2688 struct symbol *s;
2689 FOR_EACH_PTR(stmt->declaration, s) {
2690 evaluate_symbol(s);
2691 } END_FOR_EACH_PTR(s);
2692 return NULL;
2695 case STMT_RETURN:
2696 return evaluate_return_expression(stmt);
2698 case STMT_EXPRESSION:
2699 if (!evaluate_expression(stmt->expression))
2700 return NULL;
2701 return degenerate(stmt->expression);
2703 case STMT_COMPOUND: {
2704 struct statement *s;
2705 struct symbol *type = NULL;
2707 /* Evaluate the return symbol in the compound statement */
2708 evaluate_symbol(stmt->ret);
2711 * Then, evaluate each statement, making the type of the
2712 * compound statement be the type of the last statement
2714 type = NULL;
2715 FOR_EACH_PTR(stmt->stmts, s) {
2716 type = evaluate_statement(s);
2717 } END_FOR_EACH_PTR(s);
2718 if (!type)
2719 type = &void_ctype;
2720 return type;
2722 case STMT_IF:
2723 evaluate_if_statement(stmt);
2724 return NULL;
2725 case STMT_ITERATOR:
2726 evaluate_iterator(stmt);
2727 return NULL;
2728 case STMT_SWITCH:
2729 evaluate_switch_statement(stmt);
2730 return NULL;
2731 case STMT_CASE:
2732 evaluate_case_statement(stmt);
2733 return NULL;
2734 case STMT_LABEL:
2735 return evaluate_statement(stmt->label_statement);
2736 case STMT_GOTO:
2737 evaluate_expression(stmt->goto_expression);
2738 return NULL;
2739 case STMT_NONE:
2740 break;
2741 case STMT_ASM:
2742 evaluate_asm_statement(stmt);
2743 return NULL;
2744 case STMT_CONTEXT:
2745 evaluate_expression(stmt->expression);
2746 return NULL;
2747 case STMT_RANGE:
2748 evaluate_expression(stmt->range_expression);
2749 evaluate_expression(stmt->range_low);
2750 evaluate_expression(stmt->range_high);
2751 return NULL;
2753 return NULL;