[PATCH] introduce classify_type(), use it in obvious places
[smatch.git] / evaluate.c
blobb3a79e725fabbb7300a3f2b9708ab21882ffe4db
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 (!Wenum_mismatch)
247 return;
248 if (typea->type == SYM_NODE)
249 typea = typea->ctype.base_type;
250 if (typeb->type == SYM_NODE)
251 typeb = typeb->ctype.base_type;
253 if (typea == typeb)
254 return;
256 if (typea->type == SYM_ENUM && typeb->type == SYM_ENUM)
257 warning(pos, "mixing different enum types");
261 * This gets called for implicit casts in assignments and
262 * integer promotion. We often want to try to move the
263 * cast down, because the ops involved may have been
264 * implicitly cast up, and we can get rid of the casts
265 * early.
267 static struct expression * cast_to(struct expression *old, struct symbol *type)
269 struct expression *expr;
271 warn_for_different_enum_types (old->pos, old->ctype, type);
273 if (is_same_type(old, type))
274 return old;
277 * See if we can simplify the op. Move the cast down.
279 switch (old->type) {
280 case EXPR_PREOP:
281 if (old->ctype->bit_size < type->bit_size)
282 break;
283 if (old->op == '~') {
284 old->ctype = type;
285 old->unop = cast_to(old->unop, type);
286 return old;
288 break;
290 case EXPR_IMPLIED_CAST:
291 warn_for_different_enum_types(old->pos, old->ctype, type);
293 if (old->ctype->bit_size >= type->bit_size) {
294 struct expression *orig = old->cast_expression;
295 if (same_cast_type(orig->ctype, type))
296 return orig;
297 if (old->ctype->bit_offset == type->bit_offset) {
298 old->ctype = type;
299 old->cast_type = type;
300 return old;
303 break;
305 default:
306 /* nothing */;
309 expr = alloc_expression(old->pos, EXPR_IMPLIED_CAST);
310 expr->ctype = type;
311 expr->cast_type = type;
312 expr->cast_expression = old;
313 return expr;
316 static int is_type_type(struct symbol *type)
318 return (type->ctype.modifiers & MOD_TYPE) != 0;
321 int is_ptr_type(struct symbol *type)
323 if (type->type == SYM_NODE)
324 type = type->ctype.base_type;
325 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
328 static inline int is_float_type(struct symbol *type)
330 if (type->type == SYM_NODE)
331 type = type->ctype.base_type;
332 return type->ctype.base_type == &fp_type;
335 static inline int is_byte_type(struct symbol *type)
337 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
340 enum {
341 TYPE_NUM = 1,
342 TYPE_BITFIELD = 2,
343 TYPE_RESTRICT = 4,
344 TYPE_FLOAT = 8,
345 TYPE_PTR = 16,
346 TYPE_COMPOUND = 32,
349 static inline int classify_type(struct symbol *type, struct symbol **base)
351 static int type_class[SYM_BAD + 1] = {
352 [SYM_PTR] = TYPE_PTR,
353 [SYM_FN] = TYPE_PTR,
354 [SYM_ARRAY] = TYPE_PTR | TYPE_COMPOUND,
355 [SYM_STRUCT] = TYPE_COMPOUND,
356 [SYM_UNION] = TYPE_COMPOUND,
357 [SYM_BITFIELD] = TYPE_NUM | TYPE_BITFIELD,
358 [SYM_RESTRICT] = TYPE_NUM | TYPE_RESTRICT,
360 if (type->type == SYM_NODE)
361 type = type->ctype.base_type;
362 if (type->type == SYM_ENUM)
363 type = type->ctype.base_type;
364 *base = type;
365 if (type->type == SYM_BASETYPE) {
366 if (type->ctype.base_type == &int_type)
367 return TYPE_NUM;
368 if (type->ctype.base_type == &fp_type)
369 return TYPE_NUM | TYPE_FLOAT;
371 return type_class[type->type];
374 static inline int is_string_type(struct symbol *type)
376 if (type->type == SYM_NODE)
377 type = type->ctype.base_type;
378 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
381 static struct symbol *bad_expr_type(struct expression *expr)
383 sparse_error(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
384 switch (expr->type) {
385 case EXPR_BINOP:
386 case EXPR_COMPARE:
387 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
388 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
389 break;
390 case EXPR_PREOP:
391 case EXPR_POSTOP:
392 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
393 break;
394 default:
395 break;
398 return expr->ctype = &bad_ctype;
401 static struct symbol *compatible_float_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_float_type(ltype)) {
411 if (is_int_type(rtype))
412 goto Left;
413 if (is_float_type(rtype)) {
414 unsigned long lmod = ltype->ctype.modifiers;
415 unsigned long rmod = rtype->ctype.modifiers;
416 lmod &= MOD_LONG | MOD_LONGLONG;
417 rmod &= MOD_LONG | MOD_LONGLONG;
418 if (lmod == rmod)
419 return ltype;
420 if (lmod & ~rmod)
421 goto Left;
422 else
423 goto Right;
425 return NULL;
427 if (!is_float_type(rtype) || !is_int_type(ltype))
428 return NULL;
429 Right:
430 *lp = cast_to(left, rtype);
431 return rtype;
432 Left:
433 *rp = cast_to(right, ltype);
434 return ltype;
437 static struct symbol *compatible_integer_binop(struct expression **lp, struct expression **rp)
439 struct expression *left = *lp, *right = *rp;
440 struct symbol *ltype = left->ctype, *rtype = right->ctype;
442 if (ltype->type == SYM_NODE)
443 ltype = ltype->ctype.base_type;
444 if (rtype->type == SYM_NODE)
445 rtype = rtype->ctype.base_type;
446 if (is_int_type(ltype) && is_int_type(rtype)) {
447 struct symbol *ctype = bigger_int_type(ltype, rtype);
449 *lp = cast_to(left, ctype);
450 *rp = cast_to(right, ctype);
451 return ctype;
453 return NULL;
456 static int restricted_value(struct expression *v, struct symbol *type)
458 if (v->type != EXPR_VALUE)
459 return 1;
460 if (v->value != 0)
461 return 1;
462 return 0;
465 static int restricted_binop(int op, struct symbol *type)
467 switch (op) {
468 case '&':
469 case '|':
470 case '^':
471 case '?':
472 case '=':
473 case SPECIAL_EQUAL:
474 case SPECIAL_NOTEQUAL:
475 case SPECIAL_AND_ASSIGN:
476 case SPECIAL_OR_ASSIGN:
477 case SPECIAL_XOR_ASSIGN:
478 return 0;
479 default:
480 return 1;
484 static int restricted_unop(int op, struct symbol *type)
486 if (op == '~' && type->bit_size >= bits_in_int)
487 return 0;
488 if (op == '+')
489 return 0;
490 return 1;
493 static struct symbol *compatible_restricted_binop(int op, struct expression **lp, struct expression **rp)
495 struct expression *left = *lp, *right = *rp;
496 struct symbol *ltype = left->ctype, *rtype = right->ctype;
497 struct symbol *type = NULL;
499 if (ltype->type == SYM_NODE)
500 ltype = ltype->ctype.base_type;
501 if (rtype->type == SYM_NODE)
502 rtype = rtype->ctype.base_type;
504 warn_for_different_enum_types(right->pos, ltype, rtype);
506 if (ltype->type == SYM_ENUM)
507 ltype = ltype->ctype.base_type;
508 if (rtype->type == SYM_ENUM)
509 rtype = rtype->ctype.base_type;
511 if (is_restricted_type(ltype)) {
512 if (is_restricted_type(rtype)) {
513 if (ltype == rtype)
514 type = ltype;
515 } else {
516 if (!restricted_value(right, ltype))
517 type = ltype;
519 } else if (is_restricted_type(rtype)) {
520 if (!restricted_value(left, rtype))
521 type = rtype;
523 if (!type)
524 return NULL;
525 if (restricted_binop(op, type))
526 return NULL;
527 return type;
530 static struct symbol *evaluate_arith(struct expression *expr, int float_ok)
532 struct symbol *ctype = compatible_integer_binop(&expr->left, &expr->right);
533 if (!ctype && float_ok)
534 ctype = compatible_float_binop(&expr->left, &expr->right);
535 if (!ctype)
536 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
537 if (ctype) {
538 expr->ctype = ctype;
539 return ctype;
541 return bad_expr_type(expr);
544 static inline int lvalue_expression(struct expression *expr)
546 return expr->type == EXPR_PREOP && expr->op == '*';
549 static int ptr_object_size(struct symbol *ptr_type)
551 if (ptr_type->type == SYM_NODE)
552 ptr_type = ptr_type->ctype.base_type;
553 if (ptr_type->type == SYM_PTR)
554 ptr_type = get_base_type(ptr_type);
555 return ptr_type->bit_size;
558 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *ctype, struct expression **ip)
560 struct expression *i = *ip;
561 struct symbol *ptr_type = ctype;
562 int bit_size;
564 if (ptr_type->type == SYM_NODE)
565 ptr_type = ptr_type->ctype.base_type;
567 if (!is_int_type(i->ctype))
568 return bad_expr_type(expr);
570 examine_symbol_type(ctype);
572 if (!ctype->ctype.base_type) {
573 sparse_error(expr->pos, "missing type information");
574 return NULL;
577 /* Get the size of whatever the pointer points to */
578 bit_size = ptr_object_size(ctype);
580 if (bit_size > bits_in_char) {
581 int multiply = bit_size >> 3;
582 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
584 if (i->type == EXPR_VALUE) {
585 val->value = i->value * multiply;
586 val->ctype = size_t_ctype;
587 *ip = val;
588 } else {
589 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
591 val->ctype = size_t_ctype;
592 val->value = bit_size >> 3;
594 mul->op = '*';
595 mul->ctype = size_t_ctype;
596 mul->left = i;
597 mul->right = val;
599 *ip = mul;
603 expr->ctype = ctype;
604 return ctype;
607 static struct symbol *evaluate_add(struct expression *expr)
609 struct expression *left = expr->left, *right = expr->right;
610 struct symbol *ltype = left->ctype, *rtype = right->ctype;
612 if (is_ptr_type(ltype))
613 return evaluate_ptr_add(expr, degenerate(left), &expr->right);
615 if (is_ptr_type(rtype))
616 return evaluate_ptr_add(expr, degenerate(right), &expr->left);
618 return evaluate_arith(expr, 1);
621 const char * type_difference(struct symbol *target, struct symbol *source,
622 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
624 for (;;) {
625 unsigned long mod1, mod2, diff;
626 unsigned long as1, as2;
627 int type1, type2;
628 struct symbol *base1, *base2;
630 if (target == source)
631 break;
632 if (!target || !source)
633 return "different types";
635 * Peel of per-node information.
636 * FIXME! Check alignment and context too here!
638 mod1 = target->ctype.modifiers;
639 as1 = target->ctype.as;
640 mod2 = source->ctype.modifiers;
641 as2 = source->ctype.as;
642 if (target->type == SYM_NODE) {
643 target = target->ctype.base_type;
644 if (!target)
645 return "bad types";
646 if (target->type == SYM_PTR) {
647 mod1 = 0;
648 as1 = 0;
650 mod1 |= target->ctype.modifiers;
651 as1 |= target->ctype.as;
653 if (source->type == SYM_NODE) {
654 source = source->ctype.base_type;
655 if (!source)
656 return "bad types";
657 if (source->type == SYM_PTR) {
658 mod2 = 0;
659 as2 = 0;
661 mod2 |= source->ctype.modifiers;
662 as2 |= source->ctype.as;
664 if (target->type == SYM_ENUM) {
665 target = target->ctype.base_type;
666 if (!target)
667 return "bad types";
669 if (source->type == SYM_ENUM) {
670 source = source->ctype.base_type;
671 if (!source)
672 return "bad types";
675 if (target == source)
676 break;
677 if (!target || !source)
678 return "different types";
680 type1 = target->type;
681 base1 = target->ctype.base_type;
683 type2 = source->type;
684 base2 = source->ctype.base_type;
687 * Pointers to functions compare as the function itself
689 if (type1 == SYM_PTR && base1) {
690 base1 = examine_symbol_type(base1);
691 switch (base1->type) {
692 case SYM_FN:
693 type1 = SYM_FN;
694 target = base1;
695 base1 = base1->ctype.base_type;
696 default:
697 /* nothing */;
700 if (type2 == SYM_PTR && base2) {
701 base2 = examine_symbol_type(base2);
702 switch (base2->type) {
703 case SYM_FN:
704 type2 = SYM_FN;
705 source = base2;
706 base2 = base2->ctype.base_type;
707 default:
708 /* nothing */;
712 /* Arrays degenerate to pointers for type comparisons */
713 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
714 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
716 if (type1 != type2 || type1 == SYM_RESTRICT)
717 return "different base types";
719 /* Must be same address space to be comparable */
720 if (Waddress_space && as1 != as2)
721 return "different address spaces";
723 /* Ignore differences in storage types or addressability */
724 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
725 diff &= (mod1 & ~target_mod_ignore) | (mod2 & ~source_mod_ignore);
726 if (diff) {
727 if (diff & MOD_SIZE)
728 return "different type sizes";
729 if (diff & ~MOD_SIGNEDNESS)
730 return "different modifiers";
732 /* Differs in signedness only.. */
733 if (Wtypesign) {
735 * Warn if both are explicitly signed ("unsigned" is obvously
736 * always explicit, and since we know one of them has to be
737 * unsigned, we check if the signed one was explicit).
739 if ((mod1 | mod2) & MOD_EXPLICITLY_SIGNED)
740 return "different explicit signedness";
743 * "char" matches both "unsigned char" and "signed char",
744 * so if the explicit test didn't trigger, then we should
745 * not warn about a char.
747 if (!(mod1 & MOD_CHAR))
748 return "different signedness";
752 if (type1 == SYM_FN) {
753 int i;
754 struct symbol *arg1, *arg2;
755 if (base1->variadic != base2->variadic)
756 return "incompatible variadic arguments";
757 PREPARE_PTR_LIST(target->arguments, arg1);
758 PREPARE_PTR_LIST(source->arguments, arg2);
759 i = 1;
760 for (;;) {
761 const char *diff;
762 diff = type_difference(arg1, arg2, 0, 0);
763 if (diff) {
764 static char argdiff[80];
765 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
766 return argdiff;
768 if (!arg1)
769 break;
770 NEXT_PTR_LIST(arg1);
771 NEXT_PTR_LIST(arg2);
772 i++;
774 FINISH_PTR_LIST(arg2);
775 FINISH_PTR_LIST(arg1);
778 target = base1;
779 source = base2;
781 return NULL;
784 static int is_null_ptr(struct expression *expr)
786 if (expr->type != EXPR_VALUE || expr->value)
787 return 0;
788 if (!is_ptr_type(expr->ctype))
789 warning(expr->pos, "Using plain integer as NULL pointer");
790 return 1;
793 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
795 /* NULL expression? Just return the type of the "other side" */
796 if (is_null_ptr(r))
797 return l->ctype;
798 if (is_null_ptr(l))
799 return r->ctype;
800 return NULL;
804 * Ignore differences in "volatile" and "const"ness when
805 * subtracting pointers
807 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
809 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression **rp)
811 const char *typediff;
812 struct symbol *ctype;
813 struct symbol *ltype, *rtype;
814 struct expression *r = *rp;
816 ltype = degenerate(l);
817 rtype = degenerate(r);
820 * If it is an integer subtract: the ptr add case will do the
821 * right thing.
823 if (!is_ptr_type(rtype))
824 return evaluate_ptr_add(expr, degenerate(l), rp);
826 ctype = ltype;
827 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
828 if (typediff) {
829 ctype = common_ptr_type(l, r);
830 if (!ctype) {
831 sparse_error(expr->pos, "subtraction of different types can't work (%s)", typediff);
832 return NULL;
835 examine_symbol_type(ctype);
837 /* Figure out the base type we point to */
838 if (ctype->type == SYM_NODE)
839 ctype = ctype->ctype.base_type;
840 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
841 sparse_error(expr->pos, "subtraction of functions? Share your drugs");
842 return NULL;
844 ctype = get_base_type(ctype);
846 expr->ctype = ssize_t_ctype;
847 if (ctype->bit_size > bits_in_char) {
848 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
849 struct expression *div = expr;
850 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
851 unsigned long value = ctype->bit_size >> 3;
853 val->ctype = size_t_ctype;
854 val->value = value;
856 if (value & (value-1)) {
857 if (Wptr_subtraction_blows)
858 warning(expr->pos, "potentially expensive pointer subtraction");
861 sub->op = '-';
862 sub->ctype = ssize_t_ctype;
863 sub->left = l;
864 sub->right = r;
866 div->op = '/';
867 div->left = sub;
868 div->right = val;
871 return ssize_t_ctype;
874 static struct symbol *evaluate_sub(struct expression *expr)
876 struct expression *left = expr->left;
877 struct symbol *ltype = left->ctype;
879 if (is_ptr_type(ltype))
880 return evaluate_ptr_sub(expr, left, &expr->right);
882 return evaluate_arith(expr, 1);
885 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
887 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
889 struct symbol *ctype;
891 if (!expr)
892 return NULL;
894 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
895 warning(expr->pos, "assignment expression in conditional");
897 ctype = evaluate_expression(expr);
898 if (ctype) {
899 if (is_safe_type(ctype))
900 warning(expr->pos, "testing a 'safe expression'");
903 return ctype;
906 static struct symbol *evaluate_logical(struct expression *expr)
908 if (!evaluate_conditional(expr->left, 0))
909 return NULL;
910 if (!evaluate_conditional(expr->right, 0))
911 return NULL;
913 expr->ctype = &bool_ctype;
914 return &bool_ctype;
917 static struct symbol *evaluate_shift(struct expression *expr)
919 struct expression *left = expr->left, *right = expr->right;
920 struct symbol *ltype = left->ctype, *rtype = right->ctype;
922 if (ltype->type == SYM_NODE)
923 ltype = ltype->ctype.base_type;
924 if (rtype->type == SYM_NODE)
925 rtype = rtype->ctype.base_type;
926 if (is_int_type(ltype) && is_int_type(rtype)) {
927 struct symbol *ctype = integer_promotion(ltype);
928 expr->left = cast_to(expr->left, ctype);
929 expr->ctype = ctype;
930 ctype = integer_promotion(rtype);
931 expr->right = cast_to(expr->right, ctype);
932 return expr->ctype;
934 return bad_expr_type(expr);
937 static struct symbol *evaluate_binop(struct expression *expr)
939 switch (expr->op) {
940 // addition can take ptr+int, fp and int
941 case '+':
942 return evaluate_add(expr);
944 // subtraction can take ptr-ptr, fp and int
945 case '-':
946 return evaluate_sub(expr);
948 // Arithmetic operations can take fp and int
949 case '*': case '/':
950 return evaluate_arith(expr, 1);
952 // shifts do integer promotions, but that's it.
953 case SPECIAL_LEFTSHIFT: case SPECIAL_RIGHTSHIFT:
954 return evaluate_shift(expr);
956 // The rest are integer operations
957 // '%', '&', '^', '|'
958 default:
959 return evaluate_arith(expr, 0);
963 static struct symbol *evaluate_comma(struct expression *expr)
965 expr->ctype = expr->right->ctype;
966 return expr->ctype;
969 static int modify_for_unsigned(int op)
971 if (op == '<')
972 op = SPECIAL_UNSIGNED_LT;
973 else if (op == '>')
974 op = SPECIAL_UNSIGNED_GT;
975 else if (op == SPECIAL_LTE)
976 op = SPECIAL_UNSIGNED_LTE;
977 else if (op == SPECIAL_GTE)
978 op = SPECIAL_UNSIGNED_GTE;
979 return op;
982 static struct symbol *evaluate_compare(struct expression *expr)
984 struct expression *left = expr->left, *right = expr->right;
985 struct symbol *ltype = left->ctype, *rtype = right->ctype;
986 struct symbol *ctype;
988 /* Type types? */
989 if (is_type_type(ltype) && is_type_type(rtype))
990 goto OK;
992 if (is_safe_type(ltype) || is_safe_type(rtype))
993 warning(expr->pos, "testing a 'safe expression'");
995 /* Pointer types? */
996 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
997 // FIXME! Check the types for compatibility
998 expr->op = modify_for_unsigned(expr->op);
999 goto OK;
1002 ctype = compatible_integer_binop(&expr->left, &expr->right);
1003 if (ctype) {
1004 if (ctype->ctype.modifiers & MOD_UNSIGNED)
1005 expr->op = modify_for_unsigned(expr->op);
1006 goto OK;
1009 ctype = compatible_float_binop(&expr->left, &expr->right);
1010 if (ctype)
1011 goto OK;
1013 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
1014 if (ctype) {
1015 if (ctype->ctype.modifiers & MOD_UNSIGNED)
1016 expr->op = modify_for_unsigned(expr->op);
1017 goto OK;
1020 bad_expr_type(expr);
1023 expr->ctype = &bool_ctype;
1024 return &bool_ctype;
1028 * FIXME!! This should do casts, array degeneration etc..
1030 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
1032 struct symbol *ltype = left->ctype, *rtype = right->ctype;
1034 if (ltype->type == SYM_NODE)
1035 ltype = ltype->ctype.base_type;
1037 if (rtype->type == SYM_NODE)
1038 rtype = rtype->ctype.base_type;
1040 if (ltype->type == SYM_PTR) {
1041 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
1042 return ltype;
1045 if (rtype->type == SYM_PTR) {
1046 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
1047 return rtype;
1049 return NULL;
1053 * NOTE! The degenerate case of "x ? : y", where we don't
1054 * have a true case, this will possibly promote "x" to the
1055 * same type as "y", and thus _change_ the conditional
1056 * test in the expression. But since promotion is "safe"
1057 * for testing, that's ok.
1059 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1061 struct expression **true;
1062 struct symbol *ctype, *ltype, *rtype;
1063 const char * typediff;
1065 if (!evaluate_conditional(expr->conditional, 0))
1066 return NULL;
1067 if (!evaluate_expression(expr->cond_false))
1068 return NULL;
1070 ctype = degenerate(expr->conditional);
1071 rtype = degenerate(expr->cond_false);
1073 true = &expr->conditional;
1074 ltype = ctype;
1075 if (expr->cond_true) {
1076 if (!evaluate_expression(expr->cond_true))
1077 return NULL;
1078 ltype = degenerate(expr->cond_true);
1079 true = &expr->cond_true;
1082 ctype = compatible_integer_binop(true, &expr->cond_false);
1083 if (ctype)
1084 goto out;
1085 ctype = compatible_ptr_type(*true, expr->cond_false);
1086 if (ctype)
1087 goto out;
1088 ctype = compatible_float_binop(true, &expr->cond_false);
1089 if (ctype)
1090 goto out;
1091 ctype = compatible_restricted_binop('?', true, &expr->cond_false);
1092 if (ctype)
1093 goto out;
1094 ctype = ltype;
1095 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
1096 if (!typediff)
1097 goto out;
1098 sparse_error(expr->pos, "incompatible types in conditional expression (%s)", typediff);
1099 return NULL;
1101 out:
1102 expr->ctype = ctype;
1103 return ctype;
1106 /* FP assignments can not do modulo or bit operations */
1107 static int compatible_float_op(int op)
1109 return op == '=' ||
1110 op == SPECIAL_ADD_ASSIGN ||
1111 op == SPECIAL_SUB_ASSIGN ||
1112 op == SPECIAL_MUL_ASSIGN ||
1113 op == SPECIAL_DIV_ASSIGN;
1116 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1117 struct expression **rp, struct symbol *source, const char *where, int op)
1119 const char *typediff;
1120 struct symbol *t, *s;
1121 int target_as;
1122 int tclass = classify_type(target, &t);
1123 int sclass = classify_type(source, &s);
1125 if (tclass & sclass & TYPE_NUM) {
1126 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1127 sparse_error(expr->pos, "invalid assignment");
1128 return 0;
1130 if (tclass & TYPE_RESTRICT) {
1131 if (restricted_binop(op, target)) {
1132 sparse_error(expr->pos, "bad restricted assignment");
1133 return 0;
1135 if (!restricted_value(*rp, target))
1136 return 1;
1137 } else if (!(sclass & TYPE_RESTRICT))
1138 goto Cast;
1139 } else if (tclass & TYPE_PTR) {
1140 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1141 evaluate_ptr_add(expr, target, rp);
1142 return 1;
1144 if (op != '=') {
1145 sparse_error(expr->pos, "invalid pointer assignment");
1146 return 0;
1148 } else if (op != '=') {
1149 sparse_error(expr->pos, "invalid assignment");
1150 return 0;
1153 /* It's ok if the target is more volatile or const than the source */
1154 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
1155 if (!typediff)
1156 return 1;
1158 /* Pointer destination? */
1159 if (tclass & TYPE_PTR) {
1160 struct expression *right = *rp;
1161 int source_as;
1163 // NULL pointer is always ok
1164 if (is_null_ptr(right))
1165 goto Cast;
1167 /* "void *" matches anything as long as the address space is ok */
1168 target_as = t->ctype.as | target->ctype.as;
1169 source_as = s->ctype.as | source->ctype.as;
1170 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
1171 s = get_base_type(s);
1172 t = get_base_type(t);
1173 if (s == &void_ctype || t == &void_ctype)
1174 goto Cast;
1178 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1179 info(expr->pos, " expected %s", show_typename(target));
1180 info(expr->pos, " got %s", show_typename(source));
1181 *rp = cast_to(*rp, target);
1182 return 0;
1183 Cast:
1184 *rp = cast_to(*rp, target);
1185 return 1;
1188 static void mark_assigned(struct expression *expr)
1190 struct symbol *sym;
1192 if (!expr)
1193 return;
1194 switch (expr->type) {
1195 case EXPR_SYMBOL:
1196 sym = expr->symbol;
1197 if (!sym)
1198 return;
1199 if (sym->type != SYM_NODE)
1200 return;
1201 sym->ctype.modifiers |= MOD_ASSIGNED;
1202 return;
1204 case EXPR_BINOP:
1205 mark_assigned(expr->left);
1206 mark_assigned(expr->right);
1207 return;
1208 case EXPR_CAST:
1209 mark_assigned(expr->cast_expression);
1210 return;
1211 case EXPR_SLICE:
1212 mark_assigned(expr->base);
1213 return;
1214 default:
1215 /* Hmm? */
1216 return;
1220 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1222 if (type->ctype.modifiers & MOD_CONST)
1223 sparse_error(left->pos, "assignment to const expression");
1225 /* We know left is an lvalue, so it's a "preop-*" */
1226 mark_assigned(left->unop);
1229 static struct symbol *evaluate_assignment(struct expression *expr)
1231 struct expression *left = expr->left, *right = expr->right;
1232 struct expression *where = expr;
1233 struct symbol *ltype, *rtype;
1235 if (!lvalue_expression(left)) {
1236 sparse_error(expr->pos, "not an lvalue");
1237 return NULL;
1240 ltype = left->ctype;
1242 rtype = degenerate(right);
1244 if (!compatible_assignment_types(where, ltype, &where->right, rtype, "assignment", expr->op))
1245 return NULL;
1247 evaluate_assign_to(left, ltype);
1249 expr->ctype = ltype;
1250 return ltype;
1253 static void examine_fn_arguments(struct symbol *fn)
1255 struct symbol *s;
1257 FOR_EACH_PTR(fn->arguments, s) {
1258 struct symbol *arg = evaluate_symbol(s);
1259 /* Array/function arguments silently degenerate into pointers */
1260 if (arg) {
1261 struct symbol *ptr;
1262 switch(arg->type) {
1263 case SYM_ARRAY:
1264 case SYM_FN:
1265 ptr = alloc_symbol(s->pos, SYM_PTR);
1266 if (arg->type == SYM_ARRAY)
1267 ptr->ctype = arg->ctype;
1268 else
1269 ptr->ctype.base_type = arg;
1270 ptr->ctype.as |= s->ctype.as;
1271 ptr->ctype.modifiers |= s->ctype.modifiers;
1273 s->ctype.base_type = ptr;
1274 s->ctype.as = 0;
1275 s->ctype.modifiers = 0;
1276 s->bit_size = 0;
1277 s->examined = 0;
1278 examine_symbol_type(s);
1279 break;
1280 default:
1281 /* nothing */
1282 break;
1285 } END_FOR_EACH_PTR(s);
1288 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1290 /* Take the modifiers of the pointer, and apply them to the member */
1291 mod |= sym->ctype.modifiers;
1292 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1293 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1294 *newsym = *sym;
1295 newsym->ctype.as = as;
1296 newsym->ctype.modifiers = mod;
1297 sym = newsym;
1299 return sym;
1302 #define MOD_PTRINHERIT (MOD_VOLATILE | MOD_CONST | MOD_NODEREF | MOD_STORAGE)
1304 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1306 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1307 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1309 node->ctype.base_type = ptr;
1310 ptr->bit_size = bits_in_pointer;
1311 ptr->ctype.alignment = pointer_alignment;
1313 node->bit_size = bits_in_pointer;
1314 node->ctype.alignment = pointer_alignment;
1316 access_symbol(sym);
1317 if (sym->ctype.modifiers & MOD_REGISTER) {
1318 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1319 sym->ctype.modifiers &= ~MOD_REGISTER;
1321 if (sym->type == SYM_NODE) {
1322 ptr->ctype.as |= sym->ctype.as;
1323 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1324 sym = sym->ctype.base_type;
1326 if (degenerate && sym->type == SYM_ARRAY) {
1327 ptr->ctype.as |= sym->ctype.as;
1328 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1329 sym = sym->ctype.base_type;
1331 ptr->ctype.base_type = sym;
1333 return node;
1336 /* Arrays degenerate into pointers on pointer arithmetic */
1337 static struct symbol *degenerate(struct expression *expr)
1339 struct symbol *ctype, *base;
1341 if (!expr)
1342 return NULL;
1343 ctype = expr->ctype;
1344 if (!ctype)
1345 return NULL;
1346 base = examine_symbol_type(ctype);
1347 if (ctype->type == SYM_NODE)
1348 base = ctype->ctype.base_type;
1350 * Arrays degenerate into pointers to the entries, while
1351 * functions degenerate into pointers to themselves.
1352 * If array was part of non-lvalue compound, we create a copy
1353 * of that compound first and then act as if we were dealing with
1354 * the corresponding field in there.
1356 switch (base->type) {
1357 case SYM_ARRAY:
1358 if (expr->type == EXPR_SLICE) {
1359 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1360 struct expression *e0, *e1, *e2, *e3, *e4;
1362 a->ctype.base_type = expr->base->ctype;
1363 a->bit_size = expr->base->ctype->bit_size;
1364 a->array_size = expr->base->ctype->array_size;
1366 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1367 e0->symbol = a;
1368 e0->ctype = &lazy_ptr_ctype;
1370 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1371 e1->unop = e0;
1372 e1->op = '*';
1373 e1->ctype = expr->base->ctype; /* XXX */
1375 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1376 e2->left = e1;
1377 e2->right = expr->base;
1378 e2->op = '=';
1379 e2->ctype = expr->base->ctype;
1381 if (expr->r_bitpos) {
1382 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1383 e3->op = '+';
1384 e3->left = e0;
1385 e3->right = alloc_const_expression(expr->pos,
1386 expr->r_bitpos >> 3);
1387 e3->ctype = &lazy_ptr_ctype;
1388 } else {
1389 e3 = e0;
1392 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1393 e4->left = e2;
1394 e4->right = e3;
1395 e4->ctype = &lazy_ptr_ctype;
1397 expr->unop = e4;
1398 expr->type = EXPR_PREOP;
1399 expr->op = '*';
1401 case SYM_FN:
1402 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1403 sparse_error(expr->pos, "strange non-value function or array");
1404 return &bad_ctype;
1406 *expr = *expr->unop;
1407 ctype = create_pointer(expr, ctype, 1);
1408 expr->ctype = ctype;
1409 default:
1410 /* nothing */;
1412 return ctype;
1415 static struct symbol *evaluate_addressof(struct expression *expr)
1417 struct expression *op = expr->unop;
1418 struct symbol *ctype;
1420 if (op->op != '*' || op->type != EXPR_PREOP) {
1421 sparse_error(expr->pos, "not addressable");
1422 return NULL;
1424 ctype = op->ctype;
1425 *expr = *op->unop;
1427 if (expr->type == EXPR_SYMBOL) {
1428 struct symbol *sym = expr->symbol;
1429 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1433 * symbol expression evaluation is lazy about the type
1434 * of the sub-expression, so we may have to generate
1435 * the type here if so..
1437 if (expr->ctype == &lazy_ptr_ctype) {
1438 ctype = create_pointer(expr, ctype, 0);
1439 expr->ctype = ctype;
1441 return expr->ctype;
1445 static struct symbol *evaluate_dereference(struct expression *expr)
1447 struct expression *op = expr->unop;
1448 struct symbol *ctype = op->ctype, *node, *target;
1450 /* Simplify: *&(expr) => (expr) */
1451 if (op->type == EXPR_PREOP && op->op == '&') {
1452 *expr = *op->unop;
1453 return expr->ctype;
1456 /* Dereferencing a node drops all the node information. */
1457 if (ctype->type == SYM_NODE)
1458 ctype = ctype->ctype.base_type;
1460 node = alloc_symbol(expr->pos, SYM_NODE);
1461 target = ctype->ctype.base_type;
1463 switch (ctype->type) {
1464 default:
1465 sparse_error(expr->pos, "cannot derefence this type");
1466 return NULL;
1467 case SYM_PTR:
1468 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1469 merge_type(node, ctype);
1470 break;
1472 case SYM_ARRAY:
1473 if (!lvalue_expression(op)) {
1474 sparse_error(op->pos, "non-lvalue array??");
1475 return NULL;
1478 /* Do the implied "addressof" on the array */
1479 *op = *op->unop;
1482 * When an array is dereferenced, we need to pick
1483 * up the attributes of the original node too..
1485 merge_type(node, op->ctype);
1486 merge_type(node, ctype);
1487 break;
1490 node->bit_size = target->bit_size;
1491 node->array_size = target->array_size;
1493 expr->ctype = node;
1494 return node;
1498 * Unary post-ops: x++ and x--
1500 static struct symbol *evaluate_postop(struct expression *expr)
1502 struct expression *op = expr->unop;
1503 struct symbol *ctype = op->ctype;
1505 if (!lvalue_expression(expr->unop)) {
1506 sparse_error(expr->pos, "need lvalue expression for ++/--");
1507 return NULL;
1509 if (is_restricted_type(ctype) && restricted_unop(expr->op, ctype)) {
1510 sparse_error(expr->pos, "bad operation on restricted");
1511 return NULL;
1514 evaluate_assign_to(op, ctype);
1516 expr->ctype = ctype;
1517 expr->op_value = 1;
1518 if (is_ptr_type(ctype))
1519 expr->op_value = ptr_object_size(ctype) >> 3;
1521 return ctype;
1524 static struct symbol *evaluate_sign(struct expression *expr)
1526 struct symbol *ctype = expr->unop->ctype;
1527 if (is_int_type(ctype)) {
1528 struct symbol *rtype = rtype = integer_promotion(ctype);
1529 expr->unop = cast_to(expr->unop, rtype);
1530 ctype = rtype;
1531 } else if (is_float_type(ctype) && expr->op != '~') {
1532 /* no conversions needed */
1533 } else if (is_restricted_type(ctype) && !restricted_unop(expr->op, ctype)) {
1534 /* no conversions needed */
1535 } else {
1536 return bad_expr_type(expr);
1538 if (expr->op == '+')
1539 *expr = *expr->unop;
1540 expr->ctype = ctype;
1541 return ctype;
1544 static struct symbol *evaluate_preop(struct expression *expr)
1546 struct symbol *ctype = expr->unop->ctype;
1548 switch (expr->op) {
1549 case '(':
1550 *expr = *expr->unop;
1551 return ctype;
1553 case '+':
1554 case '-':
1555 case '~':
1556 return evaluate_sign(expr);
1558 case '*':
1559 return evaluate_dereference(expr);
1561 case '&':
1562 return evaluate_addressof(expr);
1564 case SPECIAL_INCREMENT:
1565 case SPECIAL_DECREMENT:
1567 * From a type evaluation standpoint the pre-ops are
1568 * the same as the postops
1570 return evaluate_postop(expr);
1572 case '!':
1573 if (is_safe_type(ctype))
1574 warning(expr->pos, "testing a 'safe expression'");
1575 if (is_float_type(ctype)) {
1576 struct expression *arg = expr->unop;
1577 expr->type = EXPR_BINOP;
1578 expr->op = SPECIAL_EQUAL;
1579 expr->left = arg;
1580 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1581 expr->right->ctype = ctype;
1582 expr->right->fvalue = 0;
1584 ctype = &bool_ctype;
1585 break;
1587 default:
1588 break;
1590 expr->ctype = ctype;
1591 return &bool_ctype;
1594 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1596 struct ptr_list *head = (struct ptr_list *)_list;
1597 struct ptr_list *list = head;
1599 if (!head)
1600 return NULL;
1601 do {
1602 int i;
1603 for (i = 0; i < list->nr; i++) {
1604 struct symbol *sym = (struct symbol *) list->list[i];
1605 if (sym->ident) {
1606 if (sym->ident != ident)
1607 continue;
1608 *offset = sym->offset;
1609 return sym;
1610 } else {
1611 struct symbol *ctype = sym->ctype.base_type;
1612 struct symbol *sub;
1613 if (!ctype)
1614 continue;
1615 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1616 continue;
1617 sub = find_identifier(ident, ctype->symbol_list, offset);
1618 if (!sub)
1619 continue;
1620 *offset += sym->offset;
1621 return sub;
1624 } while ((list = list->next) != head);
1625 return NULL;
1628 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1630 struct expression *add;
1633 * Create a new add-expression
1635 * NOTE! Even if we just add zero, we need a new node
1636 * for the member pointer, since it has a different
1637 * type than the original pointer. We could make that
1638 * be just a cast, but the fact is, a node is a node,
1639 * so we might as well just do the "add zero" here.
1641 add = alloc_expression(expr->pos, EXPR_BINOP);
1642 add->op = '+';
1643 add->left = expr;
1644 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1645 add->right->ctype = &int_ctype;
1646 add->right->value = offset;
1649 * The ctype of the pointer will be lazily evaluated if
1650 * we ever take the address of this member dereference..
1652 add->ctype = &lazy_ptr_ctype;
1653 return add;
1656 /* structure/union dereference */
1657 static struct symbol *evaluate_member_dereference(struct expression *expr)
1659 int offset;
1660 struct symbol *ctype, *member;
1661 struct expression *deref = expr->deref, *add;
1662 struct ident *ident = expr->member;
1663 unsigned int mod;
1664 int address_space;
1666 if (!evaluate_expression(deref))
1667 return NULL;
1668 if (!ident) {
1669 sparse_error(expr->pos, "bad member name");
1670 return NULL;
1673 ctype = deref->ctype;
1674 address_space = ctype->ctype.as;
1675 mod = ctype->ctype.modifiers;
1676 if (ctype->type == SYM_NODE) {
1677 ctype = ctype->ctype.base_type;
1678 address_space |= ctype->ctype.as;
1679 mod |= ctype->ctype.modifiers;
1681 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1682 sparse_error(expr->pos, "expected structure or union");
1683 return NULL;
1685 examine_symbol_type(ctype);
1686 offset = 0;
1687 member = find_identifier(ident, ctype->symbol_list, &offset);
1688 if (!member) {
1689 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1690 const char *name = "<unnamed>";
1691 int namelen = 9;
1692 if (ctype->ident) {
1693 name = ctype->ident->name;
1694 namelen = ctype->ident->len;
1696 sparse_error(expr->pos, "no member '%s' in %s %.*s",
1697 show_ident(ident), type, namelen, name);
1698 return NULL;
1702 * The member needs to take on the address space and modifiers of
1703 * the "parent" type.
1705 member = convert_to_as_mod(member, address_space, mod);
1706 ctype = get_base_type(member);
1708 if (!lvalue_expression(deref)) {
1709 if (deref->type != EXPR_SLICE) {
1710 expr->base = deref;
1711 expr->r_bitpos = 0;
1712 } else {
1713 expr->base = deref->base;
1714 expr->r_bitpos = deref->r_bitpos;
1716 expr->r_bitpos += offset << 3;
1717 expr->type = EXPR_SLICE;
1718 expr->r_nrbits = member->bit_size;
1719 expr->r_bitpos += member->bit_offset;
1720 expr->ctype = member;
1721 return member;
1724 deref = deref->unop;
1725 expr->deref = deref;
1727 add = evaluate_offset(deref, offset);
1728 expr->type = EXPR_PREOP;
1729 expr->op = '*';
1730 expr->unop = add;
1732 expr->ctype = member;
1733 return member;
1736 static int is_promoted(struct expression *expr)
1738 while (1) {
1739 switch (expr->type) {
1740 case EXPR_BINOP:
1741 case EXPR_SELECT:
1742 case EXPR_CONDITIONAL:
1743 return 1;
1744 case EXPR_COMMA:
1745 expr = expr->right;
1746 continue;
1747 case EXPR_PREOP:
1748 switch (expr->op) {
1749 case '(':
1750 expr = expr->unop;
1751 continue;
1752 case '+':
1753 case '-':
1754 case '~':
1755 return 1;
1756 default:
1757 return 0;
1759 default:
1760 return 0;
1766 static struct symbol *evaluate_cast(struct expression *);
1768 static struct symbol *evaluate_type_information(struct expression *expr)
1770 struct symbol *sym = expr->cast_type;
1771 if (!sym) {
1772 sym = evaluate_expression(expr->cast_expression);
1773 if (!sym)
1774 return NULL;
1776 * Expressions of restricted types will possibly get
1777 * promoted - check that here
1779 if (is_restricted_type(sym)) {
1780 if (sym->bit_size < bits_in_int && is_promoted(expr))
1781 sym = &int_ctype;
1784 examine_symbol_type(sym);
1785 if (is_bitfield_type(sym)) {
1786 sparse_error(expr->pos, "trying to examine bitfield type");
1787 return NULL;
1789 return sym;
1792 static struct symbol *evaluate_sizeof(struct expression *expr)
1794 struct symbol *type;
1795 int size;
1797 type = evaluate_type_information(expr);
1798 if (!type)
1799 return NULL;
1801 size = type->bit_size;
1802 if ((size < 0) || (size & 7))
1803 sparse_error(expr->pos, "cannot size expression");
1804 expr->type = EXPR_VALUE;
1805 expr->value = size >> 3;
1806 expr->ctype = size_t_ctype;
1807 return size_t_ctype;
1810 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
1812 struct symbol *type;
1813 int size;
1815 type = evaluate_type_information(expr);
1816 if (!type)
1817 return NULL;
1819 if (type->type == SYM_NODE)
1820 type = type->ctype.base_type;
1821 if (!type)
1822 return NULL;
1823 switch (type->type) {
1824 case SYM_ARRAY:
1825 break;
1826 case SYM_PTR:
1827 type = get_base_type(type);
1828 if (type)
1829 break;
1830 default:
1831 sparse_error(expr->pos, "expected pointer expression");
1832 return NULL;
1834 size = type->bit_size;
1835 if (size & 7)
1836 size = 0;
1837 expr->type = EXPR_VALUE;
1838 expr->value = size >> 3;
1839 expr->ctype = size_t_ctype;
1840 return size_t_ctype;
1843 static struct symbol *evaluate_alignof(struct expression *expr)
1845 struct symbol *type;
1847 type = evaluate_type_information(expr);
1848 if (!type)
1849 return NULL;
1851 expr->type = EXPR_VALUE;
1852 expr->value = type->ctype.alignment;
1853 expr->ctype = size_t_ctype;
1854 return size_t_ctype;
1857 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1859 struct expression *expr;
1860 struct symbol_list *argument_types = fn->arguments;
1861 struct symbol *argtype;
1862 int i = 1;
1864 PREPARE_PTR_LIST(argument_types, argtype);
1865 FOR_EACH_PTR (head, expr) {
1866 struct expression **p = THIS_ADDRESS(expr);
1867 struct symbol *ctype, *target;
1868 ctype = evaluate_expression(expr);
1870 if (!ctype)
1871 return 0;
1873 ctype = degenerate(expr);
1875 target = argtype;
1876 if (!target && ctype->bit_size < bits_in_int)
1877 target = &int_ctype;
1878 if (target) {
1879 static char where[30];
1880 examine_symbol_type(target);
1881 sprintf(where, "argument %d", i);
1882 compatible_assignment_types(expr, target, p, ctype, where, '=');
1885 i++;
1886 NEXT_PTR_LIST(argtype);
1887 } END_FOR_EACH_PTR(expr);
1888 FINISH_PTR_LIST(argtype);
1889 return 1;
1892 static void evaluate_initializer(struct symbol *ctype, struct expression **ep);
1894 static int evaluate_one_array_initializer(struct symbol *ctype, struct expression **ep, int current)
1896 struct expression *entry = *ep;
1897 struct expression **parent, *reuse = NULL;
1898 unsigned long offset;
1899 struct symbol *sym;
1900 unsigned long from, to;
1901 int accept_string = is_byte_type(ctype);
1903 from = current;
1904 to = from+1;
1905 parent = ep;
1906 if (entry->type == EXPR_INDEX) {
1907 from = entry->idx_from;
1908 to = entry->idx_to+1;
1909 parent = &entry->idx_expression;
1910 reuse = entry;
1911 entry = entry->idx_expression;
1914 offset = from * (ctype->bit_size>>3);
1915 if (offset) {
1916 if (!reuse) reuse = alloc_expression(entry->pos, EXPR_POS);
1917 reuse->type = EXPR_POS;
1918 reuse->ctype = ctype;
1919 reuse->init_offset = offset;
1920 reuse->init_nr = to - from;
1921 reuse->init_expr = entry;
1922 parent = &reuse->init_expr;
1923 entry = reuse;
1925 *ep = entry;
1927 if (accept_string && entry->type == EXPR_STRING) {
1928 sym = evaluate_expression(entry);
1929 to = from + get_expression_value(sym->array_size);
1930 } else {
1931 evaluate_initializer(ctype, parent);
1933 return to;
1936 static void evaluate_array_initializer(struct symbol *ctype, struct expression *expr)
1938 struct expression *entry;
1939 int current = 0;
1941 FOR_EACH_PTR(expr->expr_list, entry) {
1942 current = evaluate_one_array_initializer(ctype, THIS_ADDRESS(entry), current);
1943 } END_FOR_EACH_PTR(entry);
1946 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1947 static void evaluate_scalar_initializer(struct symbol *ctype, struct expression *expr)
1949 if (expression_list_size(expr->expr_list) != 1) {
1950 sparse_error(expr->pos, "unexpected compound initializer");
1951 return;
1953 evaluate_array_initializer(ctype, expr);
1954 return;
1957 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
1959 struct symbol *sym;
1961 FOR_EACH_PTR(ctype->symbol_list, sym) {
1962 if (sym->ident == ident)
1963 return sym;
1964 } END_FOR_EACH_PTR(sym);
1965 return NULL;
1968 static int evaluate_one_struct_initializer(struct symbol *ctype, struct expression **ep, struct symbol *sym)
1970 struct expression *entry = *ep;
1971 struct expression **parent;
1972 struct expression *reuse = NULL;
1973 unsigned long offset;
1975 if (!sym) {
1976 sparse_error(entry->pos, "unknown named initializer");
1977 return -1;
1980 if (entry->type == EXPR_IDENTIFIER) {
1981 reuse = entry;
1982 entry = entry->ident_expression;
1985 parent = ep;
1986 offset = sym->offset;
1987 if (offset) {
1988 if (!reuse)
1989 reuse = alloc_expression(entry->pos, EXPR_POS);
1990 reuse->type = EXPR_POS;
1991 reuse->ctype = sym;
1992 reuse->init_offset = offset;
1993 reuse->init_nr = 1;
1994 reuse->init_expr = entry;
1995 parent = &reuse->init_expr;
1996 entry = reuse;
1998 *ep = entry;
1999 evaluate_initializer(sym, parent);
2000 return 0;
2003 static void evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple)
2005 struct expression *entry;
2006 struct symbol *sym;
2008 PREPARE_PTR_LIST(ctype->symbol_list, sym);
2009 FOR_EACH_PTR(expr->expr_list, entry) {
2010 if (entry->type == EXPR_IDENTIFIER) {
2011 struct ident *ident = entry->expr_ident;
2012 /* We special-case the "already right place" case */
2013 if (!sym || sym->ident != ident) {
2014 RESET_PTR_LIST(sym);
2015 for (;;) {
2016 if (!sym)
2017 break;
2018 if (sym->ident == ident)
2019 break;
2020 NEXT_PTR_LIST(sym);
2024 if (evaluate_one_struct_initializer(ctype, THIS_ADDRESS(entry), sym))
2025 return;
2026 NEXT_PTR_LIST(sym);
2027 } END_FOR_EACH_PTR(entry);
2028 FINISH_PTR_LIST(sym);
2032 * Initializers are kind of like assignments. Except
2033 * they can be a hell of a lot more complex.
2035 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2037 struct expression *expr = *ep;
2040 * Simple non-structure/array initializers are the simple
2041 * case, and look (and parse) largely like assignments.
2043 switch (expr->type) {
2044 default: {
2045 int is_string = expr->type == EXPR_STRING;
2046 struct symbol *rtype = evaluate_expression(expr);
2047 if (rtype) {
2049 * Special case:
2050 * char array[] = "string"
2051 * should _not_ degenerate.
2053 if (!is_string || !is_string_type(ctype))
2054 rtype = degenerate(expr);
2055 compatible_assignment_types(expr, ctype, ep, rtype, "initializer", '=');
2057 return;
2060 case EXPR_INITIALIZER:
2061 expr->ctype = ctype;
2062 if (ctype->type == SYM_NODE)
2063 ctype = ctype->ctype.base_type;
2065 switch (ctype->type) {
2066 case SYM_ARRAY:
2067 case SYM_PTR:
2068 evaluate_array_initializer(get_base_type(ctype), expr);
2069 return;
2070 case SYM_UNION:
2071 evaluate_struct_or_union_initializer(ctype, expr, 0);
2072 return;
2073 case SYM_STRUCT:
2074 evaluate_struct_or_union_initializer(ctype, expr, 1);
2075 return;
2076 default:
2077 evaluate_scalar_initializer(ctype, expr);
2078 return;
2081 case EXPR_IDENTIFIER:
2082 if (ctype->type == SYM_NODE)
2083 ctype = ctype->ctype.base_type;
2084 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2085 sparse_error(expr->pos, "expected structure or union for '%s' dereference", show_ident(expr->expr_ident));
2086 show_symbol(ctype);
2087 return;
2089 evaluate_one_struct_initializer(ctype, ep,
2090 find_struct_ident(ctype, expr->expr_ident));
2091 return;
2093 case EXPR_INDEX:
2094 if (ctype->type == SYM_NODE)
2095 ctype = ctype->ctype.base_type;
2096 if (ctype->type != SYM_ARRAY) {
2097 sparse_error(expr->pos, "expected array");
2098 return;
2100 evaluate_one_array_initializer(ctype->ctype.base_type, ep, 0);
2101 return;
2103 case EXPR_POS:
2105 * An EXPR_POS expression has already been evaluated, and we don't
2106 * need to do anything more
2108 return;
2112 static int get_as(struct symbol *sym)
2114 int as;
2115 unsigned long mod;
2117 if (!sym)
2118 return 0;
2119 as = sym->ctype.as;
2120 mod = sym->ctype.modifiers;
2121 if (sym->type == SYM_NODE) {
2122 sym = sym->ctype.base_type;
2123 as |= sym->ctype.as;
2124 mod |= sym->ctype.modifiers;
2128 * At least for now, allow casting to a "unsigned long".
2129 * That's how we do things like pointer arithmetic and
2130 * store pointers to registers.
2132 if (sym == &ulong_ctype)
2133 return -1;
2135 if (sym && sym->type == SYM_PTR) {
2136 sym = get_base_type(sym);
2137 as |= sym->ctype.as;
2138 mod |= sym->ctype.modifiers;
2140 if (mod & MOD_FORCE)
2141 return -1;
2142 return as;
2145 static void cast_to_as(struct expression *e, int as)
2147 struct expression *v = e->cast_expression;
2148 struct symbol *type = v->ctype;
2150 if (!Wcast_to_address_space)
2151 return;
2153 if (v->type != EXPR_VALUE || v->value)
2154 goto out;
2156 /* cast from constant 0 to pointer is OK */
2157 if (is_int_type(type))
2158 return;
2160 if (type->type == SYM_NODE)
2161 type = type->ctype.base_type;
2163 if (type->type == SYM_PTR && type->ctype.base_type == &void_ctype)
2164 return;
2166 out:
2167 warning(e->pos, "cast adds address space to expression (<asn:%d>)", as);
2170 static struct symbol *evaluate_cast(struct expression *expr)
2172 struct expression *target = expr->cast_expression;
2173 struct symbol *ctype = examine_symbol_type(expr->cast_type);
2174 struct symbol *t1, *t2;
2175 int class1, class2;
2176 int as1, as2;
2178 if (!target)
2179 return NULL;
2181 expr->ctype = ctype;
2182 expr->cast_type = ctype;
2185 * Special case: a cast can be followed by an
2186 * initializer, in which case we need to pass
2187 * the type value down to that initializer rather
2188 * than trying to evaluate it as an expression
2190 * A more complex case is when the initializer is
2191 * dereferenced as part of a post-fix expression.
2192 * We need to produce an expression that can be dereferenced.
2194 if (target->type == EXPR_INITIALIZER) {
2195 struct symbol *sym = expr->cast_type;
2196 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2198 sym->initializer = expr->cast_expression;
2199 evaluate_symbol(sym);
2201 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2202 addr->symbol = sym;
2204 expr->type = EXPR_PREOP;
2205 expr->op = '*';
2206 expr->unop = addr;
2207 expr->ctype = sym;
2209 return sym;
2212 evaluate_expression(target);
2213 degenerate(target);
2215 class1 = classify_type(ctype, &t1);
2217 * You can always throw a value away by casting to
2218 * "void" - that's an implicit "force". Note that
2219 * the same is _not_ true of "void *".
2221 if (t1 == &void_ctype)
2222 goto out;
2224 if (class1 & TYPE_COMPOUND)
2225 warning(expr->pos, "cast to non-scalar");
2227 t2 = target->ctype;
2228 if (!t2) {
2229 sparse_error(expr->pos, "cast from unknown type");
2230 goto out;
2232 class2 = classify_type(t2, &t2);
2234 if (class2 & TYPE_COMPOUND)
2235 warning(expr->pos, "cast from non-scalar");
2237 if (!(ctype->ctype.modifiers & MOD_FORCE) && t1 != t2) {
2238 if (class1 & TYPE_RESTRICT)
2239 warning(expr->pos, "cast to restricted type");
2240 if (class2 & TYPE_RESTRICT)
2241 warning(expr->pos, "cast from restricted type");
2244 as1 = get_as(ctype);
2245 as2 = get_as(target->ctype);
2246 if (!as1 && as2 > 0)
2247 warning(expr->pos, "cast removes address space of expression");
2248 if (as1 > 0 && as2 > 0 && as1 != as2)
2249 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2250 if (as1 > 0 && !as2)
2251 cast_to_as(expr, as1);
2254 * Casts of constant values are special: they
2255 * can be NULL, and thus need to be simplified
2256 * early.
2258 if (target->type == EXPR_VALUE)
2259 cast_value(expr, ctype, target, target->ctype);
2261 out:
2262 return ctype;
2266 * Evaluate a call expression with a symbol. This
2267 * should expand inline functions, and evaluate
2268 * builtins.
2270 static int evaluate_symbol_call(struct expression *expr)
2272 struct expression *fn = expr->fn;
2273 struct symbol *ctype = fn->ctype;
2275 if (fn->type != EXPR_PREOP)
2276 return 0;
2278 if (ctype->op && ctype->op->evaluate)
2279 return ctype->op->evaluate(expr);
2281 if (ctype->ctype.modifiers & MOD_INLINE) {
2282 int ret;
2283 struct symbol *curr = current_fn;
2284 current_fn = ctype->ctype.base_type;
2285 examine_fn_arguments(current_fn);
2287 ret = inline_function(expr, ctype);
2289 /* restore the old function */
2290 current_fn = curr;
2291 return ret;
2294 return 0;
2297 static struct symbol *evaluate_call(struct expression *expr)
2299 int args, fnargs;
2300 struct symbol *ctype, *sym;
2301 struct expression *fn = expr->fn;
2302 struct expression_list *arglist = expr->args;
2304 if (!evaluate_expression(fn))
2305 return NULL;
2306 sym = ctype = fn->ctype;
2307 if (ctype->type == SYM_NODE)
2308 ctype = ctype->ctype.base_type;
2309 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
2310 ctype = get_base_type(ctype);
2312 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
2313 sym->op && sym->op->args) {
2314 if (!sym->op->args(expr))
2315 return NULL;
2316 } else {
2317 if (!evaluate_arguments(sym, ctype, arglist))
2318 return NULL;
2319 if (ctype->type != SYM_FN) {
2320 sparse_error(expr->pos, "not a function %s",
2321 show_ident(sym->ident));
2322 return NULL;
2324 args = expression_list_size(expr->args);
2325 fnargs = symbol_list_size(ctype->arguments);
2326 if (args < fnargs)
2327 sparse_error(expr->pos,
2328 "not enough arguments for function %s",
2329 show_ident(sym->ident));
2330 if (args > fnargs && !ctype->variadic)
2331 sparse_error(expr->pos,
2332 "too many arguments for function %s",
2333 show_ident(sym->ident));
2335 if (sym->type == SYM_NODE) {
2336 if (evaluate_symbol_call(expr))
2337 return expr->ctype;
2339 expr->ctype = ctype->ctype.base_type;
2340 return expr->ctype;
2343 struct symbol *evaluate_expression(struct expression *expr)
2345 if (!expr)
2346 return NULL;
2347 if (expr->ctype)
2348 return expr->ctype;
2350 switch (expr->type) {
2351 case EXPR_VALUE:
2352 case EXPR_FVALUE:
2353 sparse_error(expr->pos, "value expression without a type");
2354 return NULL;
2355 case EXPR_STRING:
2356 return evaluate_string(expr);
2357 case EXPR_SYMBOL:
2358 return evaluate_symbol_expression(expr);
2359 case EXPR_BINOP:
2360 if (!evaluate_expression(expr->left))
2361 return NULL;
2362 if (!evaluate_expression(expr->right))
2363 return NULL;
2364 return evaluate_binop(expr);
2365 case EXPR_LOGICAL:
2366 return evaluate_logical(expr);
2367 case EXPR_COMMA:
2368 evaluate_expression(expr->left);
2369 if (!evaluate_expression(expr->right))
2370 return NULL;
2371 return evaluate_comma(expr);
2372 case EXPR_COMPARE:
2373 if (!evaluate_expression(expr->left))
2374 return NULL;
2375 if (!evaluate_expression(expr->right))
2376 return NULL;
2377 return evaluate_compare(expr);
2378 case EXPR_ASSIGNMENT:
2379 if (!evaluate_expression(expr->left))
2380 return NULL;
2381 if (!evaluate_expression(expr->right))
2382 return NULL;
2383 return evaluate_assignment(expr);
2384 case EXPR_PREOP:
2385 if (!evaluate_expression(expr->unop))
2386 return NULL;
2387 return evaluate_preop(expr);
2388 case EXPR_POSTOP:
2389 if (!evaluate_expression(expr->unop))
2390 return NULL;
2391 return evaluate_postop(expr);
2392 case EXPR_CAST:
2393 case EXPR_IMPLIED_CAST:
2394 return evaluate_cast(expr);
2395 case EXPR_SIZEOF:
2396 return evaluate_sizeof(expr);
2397 case EXPR_PTRSIZEOF:
2398 return evaluate_ptrsizeof(expr);
2399 case EXPR_ALIGNOF:
2400 return evaluate_alignof(expr);
2401 case EXPR_DEREF:
2402 return evaluate_member_dereference(expr);
2403 case EXPR_CALL:
2404 return evaluate_call(expr);
2405 case EXPR_SELECT:
2406 case EXPR_CONDITIONAL:
2407 return evaluate_conditional_expression(expr);
2408 case EXPR_STATEMENT:
2409 expr->ctype = evaluate_statement(expr->statement);
2410 return expr->ctype;
2412 case EXPR_LABEL:
2413 expr->ctype = &ptr_ctype;
2414 return &ptr_ctype;
2416 case EXPR_TYPE:
2417 /* Evaluate the type of the symbol .. */
2418 evaluate_symbol(expr->symbol);
2419 /* .. but the type of the _expression_ is a "type" */
2420 expr->ctype = &type_ctype;
2421 return &type_ctype;
2423 /* These can not exist as stand-alone expressions */
2424 case EXPR_INITIALIZER:
2425 case EXPR_IDENTIFIER:
2426 case EXPR_INDEX:
2427 case EXPR_POS:
2428 sparse_error(expr->pos, "internal front-end error: initializer in expression");
2429 return NULL;
2430 case EXPR_SLICE:
2431 sparse_error(expr->pos, "internal front-end error: SLICE re-evaluated");
2432 return NULL;
2434 return NULL;
2437 static void check_duplicates(struct symbol *sym)
2439 int declared = 0;
2440 struct symbol *next = sym;
2442 while ((next = next->same_symbol) != NULL) {
2443 const char *typediff;
2444 evaluate_symbol(next);
2445 declared++;
2446 typediff = type_difference(sym, next, 0, 0);
2447 if (typediff) {
2448 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2449 show_ident(sym->ident),
2450 stream_name(next->pos.stream), next->pos.line, typediff);
2451 return;
2454 if (!declared) {
2455 unsigned long mod = sym->ctype.modifiers;
2456 if (mod & (MOD_STATIC | MOD_REGISTER))
2457 return;
2458 if (!(mod & MOD_TOPLEVEL))
2459 return;
2460 if (!Wdecl)
2461 return;
2462 if (sym->ident == &main_ident)
2463 return;
2464 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
2468 static struct symbol *evaluate_symbol(struct symbol *sym)
2470 struct symbol *base_type;
2472 if (!sym)
2473 return sym;
2474 if (sym->evaluated)
2475 return sym;
2476 sym->evaluated = 1;
2478 sym = examine_symbol_type(sym);
2479 base_type = get_base_type(sym);
2480 if (!base_type)
2481 return NULL;
2483 /* Evaluate the initializers */
2484 if (sym->initializer)
2485 evaluate_initializer(sym, &sym->initializer);
2487 /* And finally, evaluate the body of the symbol too */
2488 if (base_type->type == SYM_FN) {
2489 struct symbol *curr = current_fn;
2491 current_fn = base_type;
2493 examine_fn_arguments(base_type);
2494 if (!base_type->stmt && base_type->inline_stmt)
2495 uninline(sym);
2496 if (base_type->stmt)
2497 evaluate_statement(base_type->stmt);
2499 current_fn = curr;
2502 return base_type;
2505 void evaluate_symbol_list(struct symbol_list *list)
2507 struct symbol *sym;
2509 FOR_EACH_PTR(list, sym) {
2510 evaluate_symbol(sym);
2511 check_duplicates(sym);
2512 } END_FOR_EACH_PTR(sym);
2515 static struct symbol *evaluate_return_expression(struct statement *stmt)
2517 struct expression *expr = stmt->expression;
2518 struct symbol *ctype, *fntype;
2520 evaluate_expression(expr);
2521 ctype = degenerate(expr);
2522 fntype = current_fn->ctype.base_type;
2523 if (!fntype || fntype == &void_ctype) {
2524 if (expr && ctype != &void_ctype)
2525 sparse_error(expr->pos, "return expression in %s function", fntype?"void":"typeless");
2526 return NULL;
2529 if (!expr) {
2530 sparse_error(stmt->pos, "return with no return value");
2531 return NULL;
2533 if (!ctype)
2534 return NULL;
2535 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression", '=');
2536 return NULL;
2539 static void evaluate_if_statement(struct statement *stmt)
2541 if (!stmt->if_conditional)
2542 return;
2544 evaluate_conditional(stmt->if_conditional, 0);
2545 evaluate_statement(stmt->if_true);
2546 evaluate_statement(stmt->if_false);
2549 static void evaluate_iterator(struct statement *stmt)
2551 evaluate_conditional(stmt->iterator_pre_condition, 1);
2552 evaluate_conditional(stmt->iterator_post_condition,1);
2553 evaluate_statement(stmt->iterator_pre_statement);
2554 evaluate_statement(stmt->iterator_statement);
2555 evaluate_statement(stmt->iterator_post_statement);
2558 static void verify_output_constraint(struct expression *expr, const char *constraint)
2560 switch (*constraint) {
2561 case '=': /* Assignment */
2562 case '+': /* Update */
2563 break;
2564 default:
2565 sparse_error(expr->pos, "output constraint is not an assignment constraint (\"%s\")", constraint);
2569 static void verify_input_constraint(struct expression *expr, const char *constraint)
2571 switch (*constraint) {
2572 case '=': /* Assignment */
2573 case '+': /* Update */
2574 sparse_error(expr->pos, "input constraint with assignment (\"%s\")", constraint);
2578 static void evaluate_asm_statement(struct statement *stmt)
2580 struct expression *expr;
2581 int state;
2583 expr = stmt->asm_string;
2584 if (!expr || expr->type != EXPR_STRING) {
2585 sparse_error(stmt->pos, "need constant string for inline asm");
2586 return;
2589 state = 0;
2590 FOR_EACH_PTR(stmt->asm_outputs, expr) {
2591 struct ident *ident;
2593 switch (state) {
2594 case 0: /* Identifier */
2595 state = 1;
2596 ident = (struct ident *)expr;
2597 continue;
2599 case 1: /* Constraint */
2600 state = 2;
2601 if (!expr || expr->type != EXPR_STRING) {
2602 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
2603 *THIS_ADDRESS(expr) = NULL;
2604 continue;
2606 verify_output_constraint(expr, expr->string->data);
2607 continue;
2609 case 2: /* Expression */
2610 state = 0;
2611 if (!evaluate_expression(expr))
2612 return;
2613 if (!lvalue_expression(expr))
2614 warning(expr->pos, "asm output is not an lvalue");
2615 evaluate_assign_to(expr, expr->ctype);
2616 continue;
2618 } END_FOR_EACH_PTR(expr);
2620 state = 0;
2621 FOR_EACH_PTR(stmt->asm_inputs, expr) {
2622 struct ident *ident;
2624 switch (state) {
2625 case 0: /* Identifier */
2626 state = 1;
2627 ident = (struct ident *)expr;
2628 continue;
2630 case 1: /* Constraint */
2631 state = 2;
2632 if (!expr || expr->type != EXPR_STRING) {
2633 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
2634 *THIS_ADDRESS(expr) = NULL;
2635 continue;
2637 verify_input_constraint(expr, expr->string->data);
2638 continue;
2640 case 2: /* Expression */
2641 state = 0;
2642 if (!evaluate_expression(expr))
2643 return;
2644 continue;
2646 } END_FOR_EACH_PTR(expr);
2648 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
2649 if (!expr) {
2650 sparse_error(stmt->pos, "bad asm output");
2651 return;
2653 if (expr->type == EXPR_STRING)
2654 continue;
2655 sparse_error(expr->pos, "asm clobber is not a string");
2656 } END_FOR_EACH_PTR(expr);
2659 static void evaluate_case_statement(struct statement *stmt)
2661 evaluate_expression(stmt->case_expression);
2662 evaluate_expression(stmt->case_to);
2663 evaluate_statement(stmt->case_statement);
2666 static void check_case_type(struct expression *switch_expr,
2667 struct expression *case_expr,
2668 struct expression **enumcase)
2670 struct symbol *switch_type, *case_type;
2671 if (!case_expr)
2672 return;
2673 switch_type = switch_expr->ctype;
2674 case_type = evaluate_expression(case_expr);
2676 if (case_type && switch_type) {
2677 if (enumcase) {
2678 if (*enumcase)
2679 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
2680 else if (is_enum_type(case_type))
2681 *enumcase = case_expr;
2684 /* Both integer types? */
2685 if (compatible_restricted_binop(SPECIAL_EQUAL, &switch_expr, &case_expr))
2686 return;
2687 if (is_int_type(switch_type) && is_int_type(case_type))
2688 return;
2691 sparse_error(case_expr->pos, "incompatible types for 'case' statement");
2694 static void evaluate_switch_statement(struct statement *stmt)
2696 struct symbol *sym;
2697 struct expression *enumcase = NULL;
2698 struct expression **enumcase_holder = &enumcase;
2699 struct expression *sel = stmt->switch_expression;
2701 evaluate_expression(sel);
2702 evaluate_statement(stmt->switch_statement);
2703 if (!sel)
2704 return;
2705 if (sel->ctype && is_enum_type(sel->ctype))
2706 enumcase_holder = NULL; /* Only check cases against switch */
2708 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
2709 struct statement *case_stmt = sym->stmt;
2710 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
2711 check_case_type(sel, case_stmt->case_to, enumcase_holder);
2712 } END_FOR_EACH_PTR(sym);
2715 struct symbol *evaluate_statement(struct statement *stmt)
2717 if (!stmt)
2718 return NULL;
2720 switch (stmt->type) {
2721 case STMT_DECLARATION: {
2722 struct symbol *s;
2723 FOR_EACH_PTR(stmt->declaration, s) {
2724 evaluate_symbol(s);
2725 } END_FOR_EACH_PTR(s);
2726 return NULL;
2729 case STMT_RETURN:
2730 return evaluate_return_expression(stmt);
2732 case STMT_EXPRESSION:
2733 if (!evaluate_expression(stmt->expression))
2734 return NULL;
2735 return degenerate(stmt->expression);
2737 case STMT_COMPOUND: {
2738 struct statement *s;
2739 struct symbol *type = NULL;
2741 /* Evaluate the return symbol in the compound statement */
2742 evaluate_symbol(stmt->ret);
2745 * Then, evaluate each statement, making the type of the
2746 * compound statement be the type of the last statement
2748 type = NULL;
2749 FOR_EACH_PTR(stmt->stmts, s) {
2750 type = evaluate_statement(s);
2751 } END_FOR_EACH_PTR(s);
2752 if (!type)
2753 type = &void_ctype;
2754 return type;
2756 case STMT_IF:
2757 evaluate_if_statement(stmt);
2758 return NULL;
2759 case STMT_ITERATOR:
2760 evaluate_iterator(stmt);
2761 return NULL;
2762 case STMT_SWITCH:
2763 evaluate_switch_statement(stmt);
2764 return NULL;
2765 case STMT_CASE:
2766 evaluate_case_statement(stmt);
2767 return NULL;
2768 case STMT_LABEL:
2769 return evaluate_statement(stmt->label_statement);
2770 case STMT_GOTO:
2771 evaluate_expression(stmt->goto_expression);
2772 return NULL;
2773 case STMT_NONE:
2774 break;
2775 case STMT_ASM:
2776 evaluate_asm_statement(stmt);
2777 return NULL;
2778 case STMT_CONTEXT:
2779 evaluate_expression(stmt->expression);
2780 return NULL;
2781 case STMT_RANGE:
2782 evaluate_expression(stmt->range_expression);
2783 evaluate_expression(stmt->range_low);
2784 evaluate_expression(stmt->range_high);
2785 return NULL;
2787 return NULL;