Merge branch 'fix-defined-twice-error-on-empty-struct' into staging
[smatch.git] / evaluate.c
blob0ebbfc7c7dabb58c4f5d0f3db95da3c6676bac33
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,
347 TYPE_FOULED = 64,
350 static inline int classify_type(struct symbol *type, struct symbol **base)
352 static int type_class[SYM_BAD + 1] = {
353 [SYM_PTR] = TYPE_PTR,
354 [SYM_FN] = TYPE_PTR,
355 [SYM_ARRAY] = TYPE_PTR | TYPE_COMPOUND,
356 [SYM_STRUCT] = TYPE_COMPOUND,
357 [SYM_UNION] = TYPE_COMPOUND,
358 [SYM_BITFIELD] = TYPE_NUM | TYPE_BITFIELD,
359 [SYM_RESTRICT] = TYPE_NUM | TYPE_RESTRICT,
360 [SYM_FOULED] = TYPE_NUM | TYPE_RESTRICT | TYPE_FOULED,
362 if (type->type == SYM_NODE)
363 type = type->ctype.base_type;
364 if (type->type == SYM_ENUM)
365 type = type->ctype.base_type;
366 *base = type;
367 if (type->type == SYM_BASETYPE) {
368 if (type->ctype.base_type == &int_type)
369 return TYPE_NUM;
370 if (type->ctype.base_type == &fp_type)
371 return TYPE_NUM | TYPE_FLOAT;
373 return type_class[type->type];
376 static inline int is_string_type(struct symbol *type)
378 if (type->type == SYM_NODE)
379 type = type->ctype.base_type;
380 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
383 static struct symbol *bad_expr_type(struct expression *expr)
385 sparse_error(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
386 switch (expr->type) {
387 case EXPR_BINOP:
388 case EXPR_COMPARE:
389 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
390 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
391 break;
392 case EXPR_PREOP:
393 case EXPR_POSTOP:
394 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
395 break;
396 default:
397 break;
400 return expr->ctype = &bad_ctype;
403 static int restricted_value(struct expression *v, struct symbol *type)
405 if (v->type != EXPR_VALUE)
406 return 1;
407 if (v->value != 0)
408 return 1;
409 return 0;
412 static int restricted_binop(int op, struct symbol *type)
414 switch (op) {
415 case '&':
416 case '=':
417 case SPECIAL_AND_ASSIGN:
418 case SPECIAL_OR_ASSIGN:
419 case SPECIAL_XOR_ASSIGN:
420 return 1; /* unfoul */
421 case '|':
422 case '^':
423 case '?':
424 return 2; /* keep fouled */
425 case SPECIAL_EQUAL:
426 case SPECIAL_NOTEQUAL:
427 return 3; /* warn if fouled */
428 default:
429 return 0; /* warn */
433 static int restricted_unop(int op, struct symbol **type)
435 if (op == '~') {
436 if ((*type)->bit_size < bits_in_int)
437 *type = befoul(*type);
438 return 0;
439 } if (op == '+')
440 return 0;
441 return 1;
444 static struct symbol *restricted_binop_type(int op,
445 struct expression *left,
446 struct expression *right,
447 int lclass, int rclass,
448 struct symbol *ltype,
449 struct symbol *rtype)
451 struct symbol *ctype = NULL;
452 if (lclass & TYPE_RESTRICT) {
453 if (rclass & TYPE_RESTRICT) {
454 if (ltype == rtype) {
455 ctype = ltype;
456 } else if (lclass & TYPE_FOULED) {
457 if (ltype->ctype.base_type == rtype)
458 ctype = ltype;
459 } else if (rclass & TYPE_FOULED) {
460 if (rtype->ctype.base_type == ltype)
461 ctype = rtype;
463 } else {
464 if (!restricted_value(right, ltype))
465 ctype = ltype;
467 } else if (!restricted_value(left, rtype))
468 ctype = rtype;
470 if (ctype) {
471 switch (restricted_binop(op, ctype)) {
472 case 1:
473 if ((lclass ^ rclass) & TYPE_FOULED)
474 ctype = ctype->ctype.base_type;
475 break;
476 case 3:
477 if (!(lclass & rclass & TYPE_FOULED))
478 break;
479 case 0:
480 ctype = NULL;
481 default:
482 break;
486 return ctype;
489 static struct symbol *usual_conversions(int op,
490 struct expression **left,
491 struct expression **right,
492 int lclass, int rclass,
493 struct symbol *ltype,
494 struct symbol *rtype)
496 struct symbol *ctype;
498 warn_for_different_enum_types((*right)->pos, (*left)->ctype, (*right)->ctype);
500 if ((lclass | rclass) & TYPE_RESTRICT)
501 goto Restr;
503 Normal:
504 if (!(lclass & TYPE_FLOAT)) {
505 if (!(rclass & TYPE_FLOAT))
506 ctype = bigger_int_type(ltype, rtype);
507 else
508 ctype = rtype;
509 } else if (rclass & TYPE_FLOAT) {
510 unsigned long lmod = ltype->ctype.modifiers;
511 unsigned long rmod = rtype->ctype.modifiers;
512 if (rmod & ~lmod & (MOD_LONG | MOD_LONGLONG))
513 ctype = rtype;
514 else
515 ctype = ltype;
516 } else
517 ctype = ltype;
519 Convert:
520 *left = cast_to(*left, ctype);
521 *right = cast_to(*right, ctype);
522 return ctype;
524 Restr:
525 ctype = restricted_binop_type(op, *left, *right,
526 lclass, rclass, ltype, rtype);
527 if (ctype)
528 goto Convert;
530 if (lclass & TYPE_RESTRICT) {
531 warning((*left)->pos, "restricted degrades to integer");
532 ltype = ltype->ctype.base_type;
533 if (is_restricted_type(ltype)) /* was fouled */
534 ltype = ltype->ctype.base_type;
536 if (rclass & TYPE_RESTRICT) {
537 warning((*right)->pos, "restricted degrades to integer");
538 rtype = rtype->ctype.base_type;
539 if (is_restricted_type(rtype)) /* was fouled */
540 rtype = rtype->ctype.base_type;
542 goto Normal;
545 static struct symbol *evaluate_arith(struct expression *expr, int float_ok)
547 struct symbol *ltype, *rtype;
548 int lclass = classify_type(expr->left->ctype, &ltype);
549 int rclass = classify_type(expr->right->ctype, &rtype);
550 struct symbol *ctype;
552 if (!(lclass & rclass & TYPE_NUM))
553 goto Bad;
555 if (!float_ok && (lclass | rclass) & TYPE_FLOAT)
556 goto Bad;
558 ctype = usual_conversions(expr->op, &expr->left, &expr->right,
559 lclass, rclass, ltype, rtype);
560 expr->ctype = ctype;
561 return ctype;
563 Bad:
564 return bad_expr_type(expr);
567 static inline int lvalue_expression(struct expression *expr)
569 return expr->type == EXPR_PREOP && expr->op == '*';
572 static int ptr_object_size(struct symbol *ptr_type)
574 if (ptr_type->type == SYM_NODE)
575 ptr_type = ptr_type->ctype.base_type;
576 if (ptr_type->type == SYM_PTR)
577 ptr_type = get_base_type(ptr_type);
578 return ptr_type->bit_size;
581 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *ctype, struct expression **ip)
583 struct expression *i = *ip;
584 struct symbol *ptr_type = ctype;
585 int bit_size;
587 if (ptr_type->type == SYM_NODE)
588 ptr_type = ptr_type->ctype.base_type;
590 if (!is_int_type(i->ctype))
591 return bad_expr_type(expr);
593 examine_symbol_type(ctype);
595 if (!ctype->ctype.base_type) {
596 sparse_error(expr->pos, "missing type information");
597 return NULL;
600 /* Get the size of whatever the pointer points to */
601 bit_size = ptr_object_size(ctype);
603 if (bit_size > bits_in_char) {
604 int multiply = bit_size >> 3;
605 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
607 if (i->type == EXPR_VALUE) {
608 val->value = i->value * multiply;
609 val->ctype = size_t_ctype;
610 *ip = val;
611 } else {
612 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
614 val->ctype = size_t_ctype;
615 val->value = bit_size >> 3;
617 mul->op = '*';
618 mul->ctype = size_t_ctype;
619 mul->left = i;
620 mul->right = val;
622 *ip = mul;
626 expr->ctype = ctype;
627 return ctype;
630 static struct symbol *evaluate_add(struct expression *expr)
632 struct expression *left = expr->left, *right = expr->right;
633 struct symbol *ltype = left->ctype, *rtype = right->ctype;
635 if (is_ptr_type(ltype))
636 return evaluate_ptr_add(expr, degenerate(left), &expr->right);
638 if (is_ptr_type(rtype))
639 return evaluate_ptr_add(expr, degenerate(right), &expr->left);
641 return evaluate_arith(expr, 1);
644 const char * type_difference(struct symbol *target, struct symbol *source,
645 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
647 for (;;) {
648 unsigned long mod1, mod2, diff;
649 unsigned long as1, as2;
650 int type1, type2;
651 struct symbol *base1, *base2;
653 if (target == source)
654 break;
655 if (!target || !source)
656 return "different types";
658 * Peel of per-node information.
659 * FIXME! Check alignment and context too here!
661 mod1 = target->ctype.modifiers;
662 as1 = target->ctype.as;
663 mod2 = source->ctype.modifiers;
664 as2 = source->ctype.as;
665 if (target->type == SYM_NODE) {
666 target = target->ctype.base_type;
667 if (!target)
668 return "bad types";
669 if (target->type == SYM_PTR) {
670 mod1 = 0;
671 as1 = 0;
673 mod1 |= target->ctype.modifiers;
674 as1 |= target->ctype.as;
676 if (source->type == SYM_NODE) {
677 source = source->ctype.base_type;
678 if (!source)
679 return "bad types";
680 if (source->type == SYM_PTR) {
681 mod2 = 0;
682 as2 = 0;
684 mod2 |= source->ctype.modifiers;
685 as2 |= source->ctype.as;
687 if (target->type == SYM_ENUM) {
688 target = target->ctype.base_type;
689 if (!target)
690 return "bad types";
692 if (source->type == SYM_ENUM) {
693 source = source->ctype.base_type;
694 if (!source)
695 return "bad types";
698 if (target == source)
699 break;
700 if (!target || !source)
701 return "different types";
703 type1 = target->type;
704 base1 = target->ctype.base_type;
706 type2 = source->type;
707 base2 = source->ctype.base_type;
710 * Pointers to functions compare as the function itself
712 if (type1 == SYM_PTR && base1) {
713 base1 = examine_symbol_type(base1);
714 switch (base1->type) {
715 case SYM_FN:
716 type1 = SYM_FN;
717 target = base1;
718 base1 = base1->ctype.base_type;
719 default:
720 /* nothing */;
723 if (type2 == SYM_PTR && base2) {
724 base2 = examine_symbol_type(base2);
725 switch (base2->type) {
726 case SYM_FN:
727 type2 = SYM_FN;
728 source = base2;
729 base2 = base2->ctype.base_type;
730 default:
731 /* nothing */;
735 /* Arrays degenerate to pointers for type comparisons */
736 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
737 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
739 if (type1 != type2 || type1 == SYM_RESTRICT)
740 return "different base types";
742 /* Must be same address space to be comparable */
743 if (Waddress_space && as1 != as2)
744 return "different address spaces";
746 /* Ignore differences in storage types or addressability */
747 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
748 diff &= (mod1 & ~target_mod_ignore) | (mod2 & ~source_mod_ignore);
749 if (diff) {
750 if (diff & MOD_SIZE)
751 return "different type sizes";
752 if (diff & ~MOD_SIGNEDNESS)
753 return "different modifiers";
755 /* Differs in signedness only.. */
756 if (Wtypesign) {
758 * Warn if both are explicitly signed ("unsigned" is obvously
759 * always explicit, and since we know one of them has to be
760 * unsigned, we check if the signed one was explicit).
762 if ((mod1 | mod2) & MOD_EXPLICITLY_SIGNED)
763 return "different explicit signedness";
766 * "char" matches both "unsigned char" and "signed char",
767 * so if the explicit test didn't trigger, then we should
768 * not warn about a char.
770 if (!(mod1 & MOD_CHAR))
771 return "different signedness";
775 if (type1 == SYM_FN) {
776 int i;
777 struct symbol *arg1, *arg2;
778 if (base1->variadic != base2->variadic)
779 return "incompatible variadic arguments";
780 PREPARE_PTR_LIST(target->arguments, arg1);
781 PREPARE_PTR_LIST(source->arguments, arg2);
782 i = 1;
783 for (;;) {
784 const char *diff;
785 diff = type_difference(arg1, arg2, 0, 0);
786 if (diff) {
787 static char argdiff[80];
788 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
789 return argdiff;
791 if (!arg1)
792 break;
793 NEXT_PTR_LIST(arg1);
794 NEXT_PTR_LIST(arg2);
795 i++;
797 FINISH_PTR_LIST(arg2);
798 FINISH_PTR_LIST(arg1);
801 target = base1;
802 source = base2;
804 return NULL;
807 static int is_null_ptr(struct expression *expr)
809 if (expr->type != EXPR_VALUE || expr->value)
810 return 0;
811 if (!is_ptr_type(expr->ctype))
812 warning(expr->pos, "Using plain integer as NULL pointer");
813 return 1;
816 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
818 /* NULL expression? Just return the type of the "other side" */
819 if (is_null_ptr(r))
820 return l->ctype;
821 if (is_null_ptr(l))
822 return r->ctype;
823 return NULL;
827 * Ignore differences in "volatile" and "const"ness when
828 * subtracting pointers
830 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
832 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression **rp)
834 const char *typediff;
835 struct symbol *ctype;
836 struct symbol *ltype, *rtype;
837 struct expression *r = *rp;
839 ltype = degenerate(l);
840 rtype = degenerate(r);
843 * If it is an integer subtract: the ptr add case will do the
844 * right thing.
846 if (!is_ptr_type(rtype))
847 return evaluate_ptr_add(expr, degenerate(l), rp);
849 ctype = ltype;
850 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
851 if (typediff) {
852 ctype = common_ptr_type(l, r);
853 if (!ctype) {
854 sparse_error(expr->pos, "subtraction of different types can't work (%s)", typediff);
855 return NULL;
858 examine_symbol_type(ctype);
860 /* Figure out the base type we point to */
861 if (ctype->type == SYM_NODE)
862 ctype = ctype->ctype.base_type;
863 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
864 sparse_error(expr->pos, "subtraction of functions? Share your drugs");
865 return NULL;
867 ctype = get_base_type(ctype);
869 expr->ctype = ssize_t_ctype;
870 if (ctype->bit_size > bits_in_char) {
871 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
872 struct expression *div = expr;
873 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
874 unsigned long value = ctype->bit_size >> 3;
876 val->ctype = size_t_ctype;
877 val->value = value;
879 if (value & (value-1)) {
880 if (Wptr_subtraction_blows)
881 warning(expr->pos, "potentially expensive pointer subtraction");
884 sub->op = '-';
885 sub->ctype = ssize_t_ctype;
886 sub->left = l;
887 sub->right = r;
889 div->op = '/';
890 div->left = sub;
891 div->right = val;
894 return ssize_t_ctype;
897 static struct symbol *evaluate_sub(struct expression *expr)
899 struct expression *left = expr->left;
900 struct symbol *ltype = left->ctype;
902 if (is_ptr_type(ltype))
903 return evaluate_ptr_sub(expr, left, &expr->right);
905 return evaluate_arith(expr, 1);
908 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
910 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
912 struct symbol *ctype;
914 if (!expr)
915 return NULL;
917 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
918 warning(expr->pos, "assignment expression in conditional");
920 ctype = evaluate_expression(expr);
921 if (ctype) {
922 if (is_safe_type(ctype))
923 warning(expr->pos, "testing a 'safe expression'");
926 return ctype;
929 static struct symbol *evaluate_logical(struct expression *expr)
931 if (!evaluate_conditional(expr->left, 0))
932 return NULL;
933 if (!evaluate_conditional(expr->right, 0))
934 return NULL;
936 expr->ctype = &bool_ctype;
937 return &bool_ctype;
940 static struct symbol *evaluate_shift(struct expression *expr)
942 struct expression *left = expr->left, *right = expr->right;
943 struct symbol *ltype = left->ctype, *rtype = right->ctype;
945 if (ltype->type == SYM_NODE)
946 ltype = ltype->ctype.base_type;
947 if (rtype->type == SYM_NODE)
948 rtype = rtype->ctype.base_type;
949 if (is_int_type(ltype) && is_int_type(rtype)) {
950 struct symbol *ctype = integer_promotion(ltype);
951 expr->left = cast_to(expr->left, ctype);
952 expr->ctype = ctype;
953 ctype = integer_promotion(rtype);
954 expr->right = cast_to(expr->right, ctype);
955 return expr->ctype;
957 return bad_expr_type(expr);
960 static struct symbol *evaluate_binop(struct expression *expr)
962 switch (expr->op) {
963 // addition can take ptr+int, fp and int
964 case '+':
965 return evaluate_add(expr);
967 // subtraction can take ptr-ptr, fp and int
968 case '-':
969 return evaluate_sub(expr);
971 // Arithmetic operations can take fp and int
972 case '*': case '/':
973 return evaluate_arith(expr, 1);
975 // shifts do integer promotions, but that's it.
976 case SPECIAL_LEFTSHIFT: case SPECIAL_RIGHTSHIFT:
977 return evaluate_shift(expr);
979 // The rest are integer operations
980 // '%', '&', '^', '|'
981 default:
982 return evaluate_arith(expr, 0);
986 static struct symbol *evaluate_comma(struct expression *expr)
988 expr->ctype = expr->right->ctype;
989 return expr->ctype;
992 static int modify_for_unsigned(int op)
994 if (op == '<')
995 op = SPECIAL_UNSIGNED_LT;
996 else if (op == '>')
997 op = SPECIAL_UNSIGNED_GT;
998 else if (op == SPECIAL_LTE)
999 op = SPECIAL_UNSIGNED_LTE;
1000 else if (op == SPECIAL_GTE)
1001 op = SPECIAL_UNSIGNED_GTE;
1002 return op;
1005 static struct symbol *evaluate_compare(struct expression *expr)
1007 struct expression *left = expr->left, *right = expr->right;
1008 struct symbol *ltype = left->ctype, *rtype = right->ctype;
1009 struct symbol *ctype;
1011 /* Type types? */
1012 if (is_type_type(ltype) && is_type_type(rtype))
1013 goto OK;
1015 if (is_safe_type(ltype) || is_safe_type(rtype))
1016 warning(expr->pos, "testing a 'safe expression'");
1018 /* Pointer types? */
1019 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
1020 // FIXME! Check the types for compatibility
1021 expr->op = modify_for_unsigned(expr->op);
1022 goto OK;
1025 ctype = evaluate_arith(expr, 1);
1026 if (ctype) {
1027 if (ctype->ctype.modifiers & MOD_UNSIGNED)
1028 expr->op = modify_for_unsigned(expr->op);
1031 expr->ctype = &bool_ctype;
1032 return &bool_ctype;
1036 * FIXME!! This should do casts, array degeneration etc..
1038 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
1040 struct symbol *ltype = left->ctype, *rtype = right->ctype;
1042 if (ltype->type == SYM_NODE)
1043 ltype = ltype->ctype.base_type;
1045 if (rtype->type == SYM_NODE)
1046 rtype = rtype->ctype.base_type;
1048 if (ltype->type == SYM_PTR) {
1049 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
1050 return ltype;
1053 if (rtype->type == SYM_PTR) {
1054 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
1055 return rtype;
1057 return NULL;
1061 * NOTE! The degenerate case of "x ? : y", where we don't
1062 * have a true case, this will possibly promote "x" to the
1063 * same type as "y", and thus _change_ the conditional
1064 * test in the expression. But since promotion is "safe"
1065 * for testing, that's ok.
1067 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1069 struct expression **true;
1070 struct symbol *ctype, *ltype, *rtype;
1071 int lclass, rclass;
1072 const char * typediff;
1074 if (!evaluate_conditional(expr->conditional, 0))
1075 return NULL;
1076 if (!evaluate_expression(expr->cond_false))
1077 return NULL;
1079 ctype = degenerate(expr->conditional);
1080 rtype = degenerate(expr->cond_false);
1082 true = &expr->conditional;
1083 ltype = ctype;
1084 if (expr->cond_true) {
1085 if (!evaluate_expression(expr->cond_true))
1086 return NULL;
1087 ltype = degenerate(expr->cond_true);
1088 true = &expr->cond_true;
1091 lclass = classify_type(ltype, &ltype);
1092 rclass = classify_type(rtype, &rtype);
1093 if (lclass & rclass & TYPE_NUM) {
1094 ctype = usual_conversions('?', true, &expr->cond_false,
1095 lclass, rclass, ltype, rtype);
1096 goto out;
1098 ctype = compatible_ptr_type(*true, expr->cond_false);
1099 if (ctype)
1100 goto out;
1101 ctype = ltype;
1102 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
1103 if (!typediff)
1104 goto out;
1105 sparse_error(expr->pos, "incompatible types in conditional expression (%s)", typediff);
1106 return NULL;
1108 out:
1109 expr->ctype = ctype;
1110 return ctype;
1113 /* FP assignments can not do modulo or bit operations */
1114 static int compatible_float_op(int op)
1116 return op == '=' ||
1117 op == SPECIAL_ADD_ASSIGN ||
1118 op == SPECIAL_SUB_ASSIGN ||
1119 op == SPECIAL_MUL_ASSIGN ||
1120 op == SPECIAL_DIV_ASSIGN;
1123 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1124 struct expression **rp, struct symbol *source, const char *where, int op)
1126 const char *typediff;
1127 struct symbol *t, *s;
1128 int target_as;
1129 int tclass = classify_type(target, &t);
1130 int sclass = classify_type(source, &s);
1132 if (tclass & sclass & TYPE_NUM) {
1133 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1134 sparse_error(expr->pos, "invalid assignment");
1135 return 0;
1137 if (tclass & TYPE_RESTRICT) {
1138 if (!restricted_binop(op, target)) {
1139 sparse_error(expr->pos, "bad restricted assignment");
1140 return 0;
1142 /* allowed assignments unfoul */
1143 if (sclass & TYPE_FOULED && s->ctype.base_type == t)
1144 goto Cast;
1145 if (!restricted_value(*rp, target))
1146 return 1;
1147 } else if (!(sclass & TYPE_RESTRICT))
1148 goto Cast;
1149 } else if (tclass & TYPE_PTR) {
1150 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1151 evaluate_ptr_add(expr, target, rp);
1152 return 1;
1154 if (op != '=') {
1155 sparse_error(expr->pos, "invalid pointer assignment");
1156 return 0;
1158 } else if (op != '=') {
1159 sparse_error(expr->pos, "invalid assignment");
1160 return 0;
1163 /* It's ok if the target is more volatile or const than the source */
1164 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
1165 if (!typediff)
1166 return 1;
1168 /* Pointer destination? */
1169 if (tclass & TYPE_PTR) {
1170 struct expression *right = *rp;
1171 int source_as;
1173 // NULL pointer is always ok
1174 if (is_null_ptr(right))
1175 goto Cast;
1177 /* "void *" matches anything as long as the address space is ok */
1178 target_as = t->ctype.as | target->ctype.as;
1179 source_as = s->ctype.as | source->ctype.as;
1180 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
1181 s = get_base_type(s);
1182 t = get_base_type(t);
1183 if (s == &void_ctype || t == &void_ctype)
1184 goto Cast;
1188 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1189 info(expr->pos, " expected %s", show_typename(target));
1190 info(expr->pos, " got %s", show_typename(source));
1191 *rp = cast_to(*rp, target);
1192 return 0;
1193 Cast:
1194 *rp = cast_to(*rp, target);
1195 return 1;
1198 static void mark_assigned(struct expression *expr)
1200 struct symbol *sym;
1202 if (!expr)
1203 return;
1204 switch (expr->type) {
1205 case EXPR_SYMBOL:
1206 sym = expr->symbol;
1207 if (!sym)
1208 return;
1209 if (sym->type != SYM_NODE)
1210 return;
1211 sym->ctype.modifiers |= MOD_ASSIGNED;
1212 return;
1214 case EXPR_BINOP:
1215 mark_assigned(expr->left);
1216 mark_assigned(expr->right);
1217 return;
1218 case EXPR_CAST:
1219 mark_assigned(expr->cast_expression);
1220 return;
1221 case EXPR_SLICE:
1222 mark_assigned(expr->base);
1223 return;
1224 default:
1225 /* Hmm? */
1226 return;
1230 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1232 if (type->ctype.modifiers & MOD_CONST)
1233 sparse_error(left->pos, "assignment to const expression");
1235 /* We know left is an lvalue, so it's a "preop-*" */
1236 mark_assigned(left->unop);
1239 static struct symbol *evaluate_assignment(struct expression *expr)
1241 struct expression *left = expr->left, *right = expr->right;
1242 struct expression *where = expr;
1243 struct symbol *ltype, *rtype;
1245 if (!lvalue_expression(left)) {
1246 sparse_error(expr->pos, "not an lvalue");
1247 return NULL;
1250 ltype = left->ctype;
1252 rtype = degenerate(right);
1254 if (!compatible_assignment_types(where, ltype, &where->right, rtype, "assignment", expr->op))
1255 return NULL;
1257 evaluate_assign_to(left, ltype);
1259 expr->ctype = ltype;
1260 return ltype;
1263 static void examine_fn_arguments(struct symbol *fn)
1265 struct symbol *s;
1267 FOR_EACH_PTR(fn->arguments, s) {
1268 struct symbol *arg = evaluate_symbol(s);
1269 /* Array/function arguments silently degenerate into pointers */
1270 if (arg) {
1271 struct symbol *ptr;
1272 switch(arg->type) {
1273 case SYM_ARRAY:
1274 case SYM_FN:
1275 ptr = alloc_symbol(s->pos, SYM_PTR);
1276 if (arg->type == SYM_ARRAY)
1277 ptr->ctype = arg->ctype;
1278 else
1279 ptr->ctype.base_type = arg;
1280 ptr->ctype.as |= s->ctype.as;
1281 ptr->ctype.modifiers |= s->ctype.modifiers;
1283 s->ctype.base_type = ptr;
1284 s->ctype.as = 0;
1285 s->ctype.modifiers = 0;
1286 s->bit_size = 0;
1287 s->examined = 0;
1288 examine_symbol_type(s);
1289 break;
1290 default:
1291 /* nothing */
1292 break;
1295 } END_FOR_EACH_PTR(s);
1298 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1300 /* Take the modifiers of the pointer, and apply them to the member */
1301 mod |= sym->ctype.modifiers;
1302 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1303 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1304 *newsym = *sym;
1305 newsym->ctype.as = as;
1306 newsym->ctype.modifiers = mod;
1307 sym = newsym;
1309 return sym;
1312 #define MOD_PTRINHERIT (MOD_VOLATILE | MOD_CONST | MOD_NODEREF | MOD_STORAGE)
1314 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1316 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1317 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1319 node->ctype.base_type = ptr;
1320 ptr->bit_size = bits_in_pointer;
1321 ptr->ctype.alignment = pointer_alignment;
1323 node->bit_size = bits_in_pointer;
1324 node->ctype.alignment = pointer_alignment;
1326 access_symbol(sym);
1327 if (sym->ctype.modifiers & MOD_REGISTER) {
1328 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1329 sym->ctype.modifiers &= ~MOD_REGISTER;
1331 if (sym->type == SYM_NODE) {
1332 ptr->ctype.as |= sym->ctype.as;
1333 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1334 sym = sym->ctype.base_type;
1336 if (degenerate && sym->type == SYM_ARRAY) {
1337 ptr->ctype.as |= sym->ctype.as;
1338 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1339 sym = sym->ctype.base_type;
1341 ptr->ctype.base_type = sym;
1343 return node;
1346 /* Arrays degenerate into pointers on pointer arithmetic */
1347 static struct symbol *degenerate(struct expression *expr)
1349 struct symbol *ctype, *base;
1351 if (!expr)
1352 return NULL;
1353 ctype = expr->ctype;
1354 if (!ctype)
1355 return NULL;
1356 base = examine_symbol_type(ctype);
1357 if (ctype->type == SYM_NODE)
1358 base = ctype->ctype.base_type;
1360 * Arrays degenerate into pointers to the entries, while
1361 * functions degenerate into pointers to themselves.
1362 * If array was part of non-lvalue compound, we create a copy
1363 * of that compound first and then act as if we were dealing with
1364 * the corresponding field in there.
1366 switch (base->type) {
1367 case SYM_ARRAY:
1368 if (expr->type == EXPR_SLICE) {
1369 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1370 struct expression *e0, *e1, *e2, *e3, *e4;
1372 a->ctype.base_type = expr->base->ctype;
1373 a->bit_size = expr->base->ctype->bit_size;
1374 a->array_size = expr->base->ctype->array_size;
1376 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1377 e0->symbol = a;
1378 e0->ctype = &lazy_ptr_ctype;
1380 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1381 e1->unop = e0;
1382 e1->op = '*';
1383 e1->ctype = expr->base->ctype; /* XXX */
1385 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1386 e2->left = e1;
1387 e2->right = expr->base;
1388 e2->op = '=';
1389 e2->ctype = expr->base->ctype;
1391 if (expr->r_bitpos) {
1392 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1393 e3->op = '+';
1394 e3->left = e0;
1395 e3->right = alloc_const_expression(expr->pos,
1396 expr->r_bitpos >> 3);
1397 e3->ctype = &lazy_ptr_ctype;
1398 } else {
1399 e3 = e0;
1402 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1403 e4->left = e2;
1404 e4->right = e3;
1405 e4->ctype = &lazy_ptr_ctype;
1407 expr->unop = e4;
1408 expr->type = EXPR_PREOP;
1409 expr->op = '*';
1411 case SYM_FN:
1412 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1413 sparse_error(expr->pos, "strange non-value function or array");
1414 return &bad_ctype;
1416 *expr = *expr->unop;
1417 ctype = create_pointer(expr, ctype, 1);
1418 expr->ctype = ctype;
1419 default:
1420 /* nothing */;
1422 return ctype;
1425 static struct symbol *evaluate_addressof(struct expression *expr)
1427 struct expression *op = expr->unop;
1428 struct symbol *ctype;
1430 if (op->op != '*' || op->type != EXPR_PREOP) {
1431 sparse_error(expr->pos, "not addressable");
1432 return NULL;
1434 ctype = op->ctype;
1435 *expr = *op->unop;
1437 if (expr->type == EXPR_SYMBOL) {
1438 struct symbol *sym = expr->symbol;
1439 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1443 * symbol expression evaluation is lazy about the type
1444 * of the sub-expression, so we may have to generate
1445 * the type here if so..
1447 if (expr->ctype == &lazy_ptr_ctype) {
1448 ctype = create_pointer(expr, ctype, 0);
1449 expr->ctype = ctype;
1451 return expr->ctype;
1455 static struct symbol *evaluate_dereference(struct expression *expr)
1457 struct expression *op = expr->unop;
1458 struct symbol *ctype = op->ctype, *node, *target;
1460 /* Simplify: *&(expr) => (expr) */
1461 if (op->type == EXPR_PREOP && op->op == '&') {
1462 *expr = *op->unop;
1463 return expr->ctype;
1466 /* Dereferencing a node drops all the node information. */
1467 if (ctype->type == SYM_NODE)
1468 ctype = ctype->ctype.base_type;
1470 node = alloc_symbol(expr->pos, SYM_NODE);
1471 target = ctype->ctype.base_type;
1473 switch (ctype->type) {
1474 default:
1475 sparse_error(expr->pos, "cannot derefence this type");
1476 return NULL;
1477 case SYM_PTR:
1478 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1479 merge_type(node, ctype);
1480 break;
1482 case SYM_ARRAY:
1483 if (!lvalue_expression(op)) {
1484 sparse_error(op->pos, "non-lvalue array??");
1485 return NULL;
1488 /* Do the implied "addressof" on the array */
1489 *op = *op->unop;
1492 * When an array is dereferenced, we need to pick
1493 * up the attributes of the original node too..
1495 merge_type(node, op->ctype);
1496 merge_type(node, ctype);
1497 break;
1500 node->bit_size = target->bit_size;
1501 node->array_size = target->array_size;
1503 expr->ctype = node;
1504 return node;
1508 * Unary post-ops: x++ and x--
1510 static struct symbol *evaluate_postop(struct expression *expr)
1512 struct expression *op = expr->unop;
1513 struct symbol *ctype = op->ctype;
1515 if (!lvalue_expression(expr->unop)) {
1516 sparse_error(expr->pos, "need lvalue expression for ++/--");
1517 return NULL;
1519 if (is_restricted_type(ctype) && restricted_unop(expr->op, &ctype)) {
1520 sparse_error(expr->pos, "bad operation on restricted");
1521 return NULL;
1522 } else if (is_fouled_type(ctype) && restricted_unop(expr->op, &ctype)) {
1523 sparse_error(expr->pos, "bad operation on restricted");
1524 return NULL;
1527 evaluate_assign_to(op, ctype);
1529 expr->ctype = ctype;
1530 expr->op_value = 1;
1531 if (is_ptr_type(ctype))
1532 expr->op_value = ptr_object_size(ctype) >> 3;
1534 return ctype;
1537 static struct symbol *evaluate_sign(struct expression *expr)
1539 struct symbol *ctype = expr->unop->ctype;
1540 if (is_int_type(ctype)) {
1541 struct symbol *rtype = rtype = integer_promotion(ctype);
1542 expr->unop = cast_to(expr->unop, rtype);
1543 ctype = rtype;
1544 } else if (is_float_type(ctype) && expr->op != '~') {
1545 /* no conversions needed */
1546 } else if (is_restricted_type(ctype) && !restricted_unop(expr->op, &ctype)) {
1547 /* no conversions needed */
1548 } else if (is_fouled_type(ctype) && !restricted_unop(expr->op, &ctype)) {
1549 /* no conversions needed */
1550 } else {
1551 return bad_expr_type(expr);
1553 if (expr->op == '+')
1554 *expr = *expr->unop;
1555 expr->ctype = ctype;
1556 return ctype;
1559 static struct symbol *evaluate_preop(struct expression *expr)
1561 struct symbol *ctype = expr->unop->ctype;
1563 switch (expr->op) {
1564 case '(':
1565 *expr = *expr->unop;
1566 return ctype;
1568 case '+':
1569 case '-':
1570 case '~':
1571 return evaluate_sign(expr);
1573 case '*':
1574 return evaluate_dereference(expr);
1576 case '&':
1577 return evaluate_addressof(expr);
1579 case SPECIAL_INCREMENT:
1580 case SPECIAL_DECREMENT:
1582 * From a type evaluation standpoint the pre-ops are
1583 * the same as the postops
1585 return evaluate_postop(expr);
1587 case '!':
1588 if (is_safe_type(ctype))
1589 warning(expr->pos, "testing a 'safe expression'");
1590 if (is_float_type(ctype)) {
1591 struct expression *arg = expr->unop;
1592 expr->type = EXPR_BINOP;
1593 expr->op = SPECIAL_EQUAL;
1594 expr->left = arg;
1595 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1596 expr->right->ctype = ctype;
1597 expr->right->fvalue = 0;
1598 } else if (is_fouled_type(ctype)) {
1599 warning(expr->pos, "restricted degrades to integer");
1601 ctype = &bool_ctype;
1602 break;
1604 default:
1605 break;
1607 expr->ctype = ctype;
1608 return &bool_ctype;
1611 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1613 struct ptr_list *head = (struct ptr_list *)_list;
1614 struct ptr_list *list = head;
1616 if (!head)
1617 return NULL;
1618 do {
1619 int i;
1620 for (i = 0; i < list->nr; i++) {
1621 struct symbol *sym = (struct symbol *) list->list[i];
1622 if (sym->ident) {
1623 if (sym->ident != ident)
1624 continue;
1625 *offset = sym->offset;
1626 return sym;
1627 } else {
1628 struct symbol *ctype = sym->ctype.base_type;
1629 struct symbol *sub;
1630 if (!ctype)
1631 continue;
1632 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1633 continue;
1634 sub = find_identifier(ident, ctype->symbol_list, offset);
1635 if (!sub)
1636 continue;
1637 *offset += sym->offset;
1638 return sub;
1641 } while ((list = list->next) != head);
1642 return NULL;
1645 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1647 struct expression *add;
1650 * Create a new add-expression
1652 * NOTE! Even if we just add zero, we need a new node
1653 * for the member pointer, since it has a different
1654 * type than the original pointer. We could make that
1655 * be just a cast, but the fact is, a node is a node,
1656 * so we might as well just do the "add zero" here.
1658 add = alloc_expression(expr->pos, EXPR_BINOP);
1659 add->op = '+';
1660 add->left = expr;
1661 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1662 add->right->ctype = &int_ctype;
1663 add->right->value = offset;
1666 * The ctype of the pointer will be lazily evaluated if
1667 * we ever take the address of this member dereference..
1669 add->ctype = &lazy_ptr_ctype;
1670 return add;
1673 /* structure/union dereference */
1674 static struct symbol *evaluate_member_dereference(struct expression *expr)
1676 int offset;
1677 struct symbol *ctype, *member;
1678 struct expression *deref = expr->deref, *add;
1679 struct ident *ident = expr->member;
1680 unsigned int mod;
1681 int address_space;
1683 if (!evaluate_expression(deref))
1684 return NULL;
1685 if (!ident) {
1686 sparse_error(expr->pos, "bad member name");
1687 return NULL;
1690 ctype = deref->ctype;
1691 address_space = ctype->ctype.as;
1692 mod = ctype->ctype.modifiers;
1693 if (ctype->type == SYM_NODE) {
1694 ctype = ctype->ctype.base_type;
1695 address_space |= ctype->ctype.as;
1696 mod |= ctype->ctype.modifiers;
1698 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1699 sparse_error(expr->pos, "expected structure or union");
1700 return NULL;
1702 examine_symbol_type(ctype);
1703 offset = 0;
1704 member = find_identifier(ident, ctype->symbol_list, &offset);
1705 if (!member) {
1706 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1707 const char *name = "<unnamed>";
1708 int namelen = 9;
1709 if (ctype->ident) {
1710 name = ctype->ident->name;
1711 namelen = ctype->ident->len;
1713 sparse_error(expr->pos, "no member '%s' in %s %.*s",
1714 show_ident(ident), type, namelen, name);
1715 return NULL;
1719 * The member needs to take on the address space and modifiers of
1720 * the "parent" type.
1722 member = convert_to_as_mod(member, address_space, mod);
1723 ctype = get_base_type(member);
1725 if (!lvalue_expression(deref)) {
1726 if (deref->type != EXPR_SLICE) {
1727 expr->base = deref;
1728 expr->r_bitpos = 0;
1729 } else {
1730 expr->base = deref->base;
1731 expr->r_bitpos = deref->r_bitpos;
1733 expr->r_bitpos += offset << 3;
1734 expr->type = EXPR_SLICE;
1735 expr->r_nrbits = member->bit_size;
1736 expr->r_bitpos += member->bit_offset;
1737 expr->ctype = member;
1738 return member;
1741 deref = deref->unop;
1742 expr->deref = deref;
1744 add = evaluate_offset(deref, offset);
1745 expr->type = EXPR_PREOP;
1746 expr->op = '*';
1747 expr->unop = add;
1749 expr->ctype = member;
1750 return member;
1753 static int is_promoted(struct expression *expr)
1755 while (1) {
1756 switch (expr->type) {
1757 case EXPR_BINOP:
1758 case EXPR_SELECT:
1759 case EXPR_CONDITIONAL:
1760 return 1;
1761 case EXPR_COMMA:
1762 expr = expr->right;
1763 continue;
1764 case EXPR_PREOP:
1765 switch (expr->op) {
1766 case '(':
1767 expr = expr->unop;
1768 continue;
1769 case '+':
1770 case '-':
1771 case '~':
1772 return 1;
1773 default:
1774 return 0;
1776 default:
1777 return 0;
1783 static struct symbol *evaluate_cast(struct expression *);
1785 static struct symbol *evaluate_type_information(struct expression *expr)
1787 struct symbol *sym = expr->cast_type;
1788 if (!sym) {
1789 sym = evaluate_expression(expr->cast_expression);
1790 if (!sym)
1791 return NULL;
1793 * Expressions of restricted types will possibly get
1794 * promoted - check that here
1796 if (is_restricted_type(sym)) {
1797 if (sym->bit_size < bits_in_int && is_promoted(expr))
1798 sym = &int_ctype;
1799 } else if (is_fouled_type(sym)) {
1800 sym = &int_ctype;
1803 examine_symbol_type(sym);
1804 if (is_bitfield_type(sym)) {
1805 sparse_error(expr->pos, "trying to examine bitfield type");
1806 return NULL;
1808 return sym;
1811 static struct symbol *evaluate_sizeof(struct expression *expr)
1813 struct symbol *type;
1814 int size;
1816 type = evaluate_type_information(expr);
1817 if (!type)
1818 return NULL;
1820 size = type->bit_size;
1821 if ((size < 0) || (size & 7))
1822 sparse_error(expr->pos, "cannot size expression");
1823 expr->type = EXPR_VALUE;
1824 expr->value = size >> 3;
1825 expr->ctype = size_t_ctype;
1826 return size_t_ctype;
1829 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
1831 struct symbol *type;
1832 int size;
1834 type = evaluate_type_information(expr);
1835 if (!type)
1836 return NULL;
1838 if (type->type == SYM_NODE)
1839 type = type->ctype.base_type;
1840 if (!type)
1841 return NULL;
1842 switch (type->type) {
1843 case SYM_ARRAY:
1844 break;
1845 case SYM_PTR:
1846 type = get_base_type(type);
1847 if (type)
1848 break;
1849 default:
1850 sparse_error(expr->pos, "expected pointer expression");
1851 return NULL;
1853 size = type->bit_size;
1854 if (size & 7)
1855 size = 0;
1856 expr->type = EXPR_VALUE;
1857 expr->value = size >> 3;
1858 expr->ctype = size_t_ctype;
1859 return size_t_ctype;
1862 static struct symbol *evaluate_alignof(struct expression *expr)
1864 struct symbol *type;
1866 type = evaluate_type_information(expr);
1867 if (!type)
1868 return NULL;
1870 expr->type = EXPR_VALUE;
1871 expr->value = type->ctype.alignment;
1872 expr->ctype = size_t_ctype;
1873 return size_t_ctype;
1876 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1878 struct expression *expr;
1879 struct symbol_list *argument_types = fn->arguments;
1880 struct symbol *argtype;
1881 int i = 1;
1883 PREPARE_PTR_LIST(argument_types, argtype);
1884 FOR_EACH_PTR (head, expr) {
1885 struct expression **p = THIS_ADDRESS(expr);
1886 struct symbol *ctype, *target;
1887 ctype = evaluate_expression(expr);
1889 if (!ctype)
1890 return 0;
1892 ctype = degenerate(expr);
1894 target = argtype;
1895 if (!target && ctype->bit_size < bits_in_int)
1896 target = &int_ctype;
1897 if (target) {
1898 static char where[30];
1899 examine_symbol_type(target);
1900 sprintf(where, "argument %d", i);
1901 compatible_assignment_types(expr, target, p, ctype, where, '=');
1904 i++;
1905 NEXT_PTR_LIST(argtype);
1906 } END_FOR_EACH_PTR(expr);
1907 FINISH_PTR_LIST(argtype);
1908 return 1;
1911 static void evaluate_initializer(struct symbol *ctype, struct expression **ep);
1913 static int evaluate_one_array_initializer(struct symbol *ctype, struct expression **ep, int current)
1915 struct expression *entry = *ep;
1916 struct expression **parent, *reuse = NULL;
1917 unsigned long offset;
1918 struct symbol *sym;
1919 unsigned long from, to;
1920 int accept_string = is_byte_type(ctype);
1922 from = current;
1923 to = from+1;
1924 parent = ep;
1925 if (entry->type == EXPR_INDEX) {
1926 from = entry->idx_from;
1927 to = entry->idx_to+1;
1928 parent = &entry->idx_expression;
1929 reuse = entry;
1930 entry = entry->idx_expression;
1933 offset = from * (ctype->bit_size>>3);
1934 if (offset) {
1935 if (!reuse) reuse = alloc_expression(entry->pos, EXPR_POS);
1936 reuse->type = EXPR_POS;
1937 reuse->ctype = ctype;
1938 reuse->init_offset = offset;
1939 reuse->init_nr = to - from;
1940 reuse->init_expr = entry;
1941 parent = &reuse->init_expr;
1942 entry = reuse;
1944 *ep = entry;
1946 if (accept_string && entry->type == EXPR_STRING) {
1947 sym = evaluate_expression(entry);
1948 to = from + get_expression_value(sym->array_size);
1949 } else {
1950 evaluate_initializer(ctype, parent);
1952 return to;
1955 static void evaluate_array_initializer(struct symbol *ctype, struct expression *expr)
1957 struct expression *entry;
1958 int current = 0;
1960 FOR_EACH_PTR(expr->expr_list, entry) {
1961 current = evaluate_one_array_initializer(ctype, THIS_ADDRESS(entry), current);
1962 } END_FOR_EACH_PTR(entry);
1965 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1966 static void evaluate_scalar_initializer(struct symbol *ctype, struct expression *expr)
1968 if (expression_list_size(expr->expr_list) != 1) {
1969 sparse_error(expr->pos, "unexpected compound initializer");
1970 return;
1972 evaluate_array_initializer(ctype, expr);
1973 return;
1976 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
1978 struct symbol *sym;
1980 FOR_EACH_PTR(ctype->symbol_list, sym) {
1981 if (sym->ident == ident)
1982 return sym;
1983 } END_FOR_EACH_PTR(sym);
1984 return NULL;
1987 static int evaluate_one_struct_initializer(struct symbol *ctype, struct expression **ep, struct symbol *sym)
1989 struct expression *entry = *ep;
1990 struct expression **parent;
1991 struct expression *reuse = NULL;
1992 unsigned long offset;
1994 if (!sym) {
1995 sparse_error(entry->pos, "unknown named initializer");
1996 return -1;
1999 if (entry->type == EXPR_IDENTIFIER) {
2000 reuse = entry;
2001 entry = entry->ident_expression;
2004 parent = ep;
2005 offset = sym->offset;
2006 if (offset) {
2007 if (!reuse)
2008 reuse = alloc_expression(entry->pos, EXPR_POS);
2009 reuse->type = EXPR_POS;
2010 reuse->ctype = sym;
2011 reuse->init_offset = offset;
2012 reuse->init_nr = 1;
2013 reuse->init_expr = entry;
2014 parent = &reuse->init_expr;
2015 entry = reuse;
2017 *ep = entry;
2018 evaluate_initializer(sym, parent);
2019 return 0;
2022 static void evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple)
2024 struct expression *entry;
2025 struct symbol *sym;
2027 PREPARE_PTR_LIST(ctype->symbol_list, sym);
2028 FOR_EACH_PTR(expr->expr_list, entry) {
2029 if (entry->type == EXPR_IDENTIFIER) {
2030 struct ident *ident = entry->expr_ident;
2031 /* We special-case the "already right place" case */
2032 if (!sym || sym->ident != ident) {
2033 RESET_PTR_LIST(sym);
2034 for (;;) {
2035 if (!sym)
2036 break;
2037 if (sym->ident == ident)
2038 break;
2039 NEXT_PTR_LIST(sym);
2043 if (evaluate_one_struct_initializer(ctype, THIS_ADDRESS(entry), sym))
2044 return;
2045 NEXT_PTR_LIST(sym);
2046 } END_FOR_EACH_PTR(entry);
2047 FINISH_PTR_LIST(sym);
2051 * Initializers are kind of like assignments. Except
2052 * they can be a hell of a lot more complex.
2054 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2056 struct expression *expr = *ep;
2059 * Simple non-structure/array initializers are the simple
2060 * case, and look (and parse) largely like assignments.
2062 switch (expr->type) {
2063 default: {
2064 int is_string = expr->type == EXPR_STRING;
2065 struct symbol *rtype = evaluate_expression(expr);
2066 if (rtype) {
2068 * Special case:
2069 * char array[] = "string"
2070 * should _not_ degenerate.
2072 if (!is_string || !is_string_type(ctype))
2073 rtype = degenerate(expr);
2074 compatible_assignment_types(expr, ctype, ep, rtype, "initializer", '=');
2076 return;
2079 case EXPR_INITIALIZER:
2080 expr->ctype = ctype;
2081 if (ctype->type == SYM_NODE)
2082 ctype = ctype->ctype.base_type;
2084 switch (ctype->type) {
2085 case SYM_ARRAY:
2086 case SYM_PTR:
2087 evaluate_array_initializer(get_base_type(ctype), expr);
2088 return;
2089 case SYM_UNION:
2090 evaluate_struct_or_union_initializer(ctype, expr, 0);
2091 return;
2092 case SYM_STRUCT:
2093 evaluate_struct_or_union_initializer(ctype, expr, 1);
2094 return;
2095 default:
2096 evaluate_scalar_initializer(ctype, expr);
2097 return;
2100 case EXPR_IDENTIFIER:
2101 if (ctype->type == SYM_NODE)
2102 ctype = ctype->ctype.base_type;
2103 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2104 sparse_error(expr->pos, "expected structure or union for '%s' dereference", show_ident(expr->expr_ident));
2105 show_symbol(ctype);
2106 return;
2108 evaluate_one_struct_initializer(ctype, ep,
2109 find_struct_ident(ctype, expr->expr_ident));
2110 return;
2112 case EXPR_INDEX:
2113 if (ctype->type == SYM_NODE)
2114 ctype = ctype->ctype.base_type;
2115 if (ctype->type != SYM_ARRAY) {
2116 sparse_error(expr->pos, "expected array");
2117 return;
2119 evaluate_one_array_initializer(ctype->ctype.base_type, ep, 0);
2120 return;
2122 case EXPR_POS:
2124 * An EXPR_POS expression has already been evaluated, and we don't
2125 * need to do anything more
2127 return;
2131 static int get_as(struct symbol *sym)
2133 int as;
2134 unsigned long mod;
2136 if (!sym)
2137 return 0;
2138 as = sym->ctype.as;
2139 mod = sym->ctype.modifiers;
2140 if (sym->type == SYM_NODE) {
2141 sym = sym->ctype.base_type;
2142 as |= sym->ctype.as;
2143 mod |= sym->ctype.modifiers;
2147 * At least for now, allow casting to a "unsigned long".
2148 * That's how we do things like pointer arithmetic and
2149 * store pointers to registers.
2151 if (sym == &ulong_ctype)
2152 return -1;
2154 if (sym && sym->type == SYM_PTR) {
2155 sym = get_base_type(sym);
2156 as |= sym->ctype.as;
2157 mod |= sym->ctype.modifiers;
2159 if (mod & MOD_FORCE)
2160 return -1;
2161 return as;
2164 static void cast_to_as(struct expression *e, int as)
2166 struct expression *v = e->cast_expression;
2167 struct symbol *type = v->ctype;
2169 if (!Wcast_to_address_space)
2170 return;
2172 if (v->type != EXPR_VALUE || v->value)
2173 goto out;
2175 /* cast from constant 0 to pointer is OK */
2176 if (is_int_type(type))
2177 return;
2179 if (type->type == SYM_NODE)
2180 type = type->ctype.base_type;
2182 if (type->type == SYM_PTR && type->ctype.base_type == &void_ctype)
2183 return;
2185 out:
2186 warning(e->pos, "cast adds address space to expression (<asn:%d>)", as);
2189 static struct symbol *evaluate_cast(struct expression *expr)
2191 struct expression *target = expr->cast_expression;
2192 struct symbol *ctype = examine_symbol_type(expr->cast_type);
2193 struct symbol *t1, *t2;
2194 int class1, class2;
2195 int as1, as2;
2197 if (!target)
2198 return NULL;
2200 expr->ctype = ctype;
2201 expr->cast_type = ctype;
2204 * Special case: a cast can be followed by an
2205 * initializer, in which case we need to pass
2206 * the type value down to that initializer rather
2207 * than trying to evaluate it as an expression
2209 * A more complex case is when the initializer is
2210 * dereferenced as part of a post-fix expression.
2211 * We need to produce an expression that can be dereferenced.
2213 if (target->type == EXPR_INITIALIZER) {
2214 struct symbol *sym = expr->cast_type;
2215 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2217 sym->initializer = expr->cast_expression;
2218 evaluate_symbol(sym);
2220 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2221 addr->symbol = sym;
2223 expr->type = EXPR_PREOP;
2224 expr->op = '*';
2225 expr->unop = addr;
2226 expr->ctype = sym;
2228 return sym;
2231 evaluate_expression(target);
2232 degenerate(target);
2234 class1 = classify_type(ctype, &t1);
2236 * You can always throw a value away by casting to
2237 * "void" - that's an implicit "force". Note that
2238 * the same is _not_ true of "void *".
2240 if (t1 == &void_ctype)
2241 goto out;
2243 if (class1 & TYPE_COMPOUND)
2244 warning(expr->pos, "cast to non-scalar");
2246 t2 = target->ctype;
2247 if (!t2) {
2248 sparse_error(expr->pos, "cast from unknown type");
2249 goto out;
2251 class2 = classify_type(t2, &t2);
2253 if (class2 & TYPE_COMPOUND)
2254 warning(expr->pos, "cast from non-scalar");
2256 /* allowed cast unfouls */
2257 if (class2 & TYPE_FOULED)
2258 t2 = t2->ctype.base_type;
2260 if (!(ctype->ctype.modifiers & MOD_FORCE) && t1 != t2) {
2261 if (class1 & TYPE_RESTRICT)
2262 warning(expr->pos, "cast to restricted type");
2263 if (class2 & TYPE_RESTRICT)
2264 warning(expr->pos, "cast from restricted type");
2267 as1 = get_as(ctype);
2268 as2 = get_as(target->ctype);
2269 if (!as1 && as2 > 0)
2270 warning(expr->pos, "cast removes address space of expression");
2271 if (as1 > 0 && as2 > 0 && as1 != as2)
2272 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2273 if (as1 > 0 && !as2)
2274 cast_to_as(expr, as1);
2277 * Casts of constant values are special: they
2278 * can be NULL, and thus need to be simplified
2279 * early.
2281 if (target->type == EXPR_VALUE)
2282 cast_value(expr, ctype, target, target->ctype);
2284 out:
2285 return ctype;
2289 * Evaluate a call expression with a symbol. This
2290 * should expand inline functions, and evaluate
2291 * builtins.
2293 static int evaluate_symbol_call(struct expression *expr)
2295 struct expression *fn = expr->fn;
2296 struct symbol *ctype = fn->ctype;
2298 if (fn->type != EXPR_PREOP)
2299 return 0;
2301 if (ctype->op && ctype->op->evaluate)
2302 return ctype->op->evaluate(expr);
2304 if (ctype->ctype.modifiers & MOD_INLINE) {
2305 int ret;
2306 struct symbol *curr = current_fn;
2307 current_fn = ctype->ctype.base_type;
2308 examine_fn_arguments(current_fn);
2310 ret = inline_function(expr, ctype);
2312 /* restore the old function */
2313 current_fn = curr;
2314 return ret;
2317 return 0;
2320 static struct symbol *evaluate_call(struct expression *expr)
2322 int args, fnargs;
2323 struct symbol *ctype, *sym;
2324 struct expression *fn = expr->fn;
2325 struct expression_list *arglist = expr->args;
2327 if (!evaluate_expression(fn))
2328 return NULL;
2329 sym = ctype = fn->ctype;
2330 if (ctype->type == SYM_NODE)
2331 ctype = ctype->ctype.base_type;
2332 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
2333 ctype = get_base_type(ctype);
2335 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
2336 sym->op && sym->op->args) {
2337 if (!sym->op->args(expr))
2338 return NULL;
2339 } else {
2340 if (!evaluate_arguments(sym, ctype, arglist))
2341 return NULL;
2342 if (ctype->type != SYM_FN) {
2343 sparse_error(expr->pos, "not a function %s",
2344 show_ident(sym->ident));
2345 return NULL;
2347 args = expression_list_size(expr->args);
2348 fnargs = symbol_list_size(ctype->arguments);
2349 if (args < fnargs)
2350 sparse_error(expr->pos,
2351 "not enough arguments for function %s",
2352 show_ident(sym->ident));
2353 if (args > fnargs && !ctype->variadic)
2354 sparse_error(expr->pos,
2355 "too many arguments for function %s",
2356 show_ident(sym->ident));
2358 if (sym->type == SYM_NODE) {
2359 if (evaluate_symbol_call(expr))
2360 return expr->ctype;
2362 expr->ctype = ctype->ctype.base_type;
2363 return expr->ctype;
2366 struct symbol *evaluate_expression(struct expression *expr)
2368 if (!expr)
2369 return NULL;
2370 if (expr->ctype)
2371 return expr->ctype;
2373 switch (expr->type) {
2374 case EXPR_VALUE:
2375 case EXPR_FVALUE:
2376 sparse_error(expr->pos, "value expression without a type");
2377 return NULL;
2378 case EXPR_STRING:
2379 return evaluate_string(expr);
2380 case EXPR_SYMBOL:
2381 return evaluate_symbol_expression(expr);
2382 case EXPR_BINOP:
2383 if (!evaluate_expression(expr->left))
2384 return NULL;
2385 if (!evaluate_expression(expr->right))
2386 return NULL;
2387 return evaluate_binop(expr);
2388 case EXPR_LOGICAL:
2389 return evaluate_logical(expr);
2390 case EXPR_COMMA:
2391 evaluate_expression(expr->left);
2392 if (!evaluate_expression(expr->right))
2393 return NULL;
2394 return evaluate_comma(expr);
2395 case EXPR_COMPARE:
2396 if (!evaluate_expression(expr->left))
2397 return NULL;
2398 if (!evaluate_expression(expr->right))
2399 return NULL;
2400 return evaluate_compare(expr);
2401 case EXPR_ASSIGNMENT:
2402 if (!evaluate_expression(expr->left))
2403 return NULL;
2404 if (!evaluate_expression(expr->right))
2405 return NULL;
2406 return evaluate_assignment(expr);
2407 case EXPR_PREOP:
2408 if (!evaluate_expression(expr->unop))
2409 return NULL;
2410 return evaluate_preop(expr);
2411 case EXPR_POSTOP:
2412 if (!evaluate_expression(expr->unop))
2413 return NULL;
2414 return evaluate_postop(expr);
2415 case EXPR_CAST:
2416 case EXPR_IMPLIED_CAST:
2417 return evaluate_cast(expr);
2418 case EXPR_SIZEOF:
2419 return evaluate_sizeof(expr);
2420 case EXPR_PTRSIZEOF:
2421 return evaluate_ptrsizeof(expr);
2422 case EXPR_ALIGNOF:
2423 return evaluate_alignof(expr);
2424 case EXPR_DEREF:
2425 return evaluate_member_dereference(expr);
2426 case EXPR_CALL:
2427 return evaluate_call(expr);
2428 case EXPR_SELECT:
2429 case EXPR_CONDITIONAL:
2430 return evaluate_conditional_expression(expr);
2431 case EXPR_STATEMENT:
2432 expr->ctype = evaluate_statement(expr->statement);
2433 return expr->ctype;
2435 case EXPR_LABEL:
2436 expr->ctype = &ptr_ctype;
2437 return &ptr_ctype;
2439 case EXPR_TYPE:
2440 /* Evaluate the type of the symbol .. */
2441 evaluate_symbol(expr->symbol);
2442 /* .. but the type of the _expression_ is a "type" */
2443 expr->ctype = &type_ctype;
2444 return &type_ctype;
2446 /* These can not exist as stand-alone expressions */
2447 case EXPR_INITIALIZER:
2448 case EXPR_IDENTIFIER:
2449 case EXPR_INDEX:
2450 case EXPR_POS:
2451 sparse_error(expr->pos, "internal front-end error: initializer in expression");
2452 return NULL;
2453 case EXPR_SLICE:
2454 sparse_error(expr->pos, "internal front-end error: SLICE re-evaluated");
2455 return NULL;
2457 return NULL;
2460 static void check_duplicates(struct symbol *sym)
2462 int declared = 0;
2463 struct symbol *next = sym;
2465 while ((next = next->same_symbol) != NULL) {
2466 const char *typediff;
2467 evaluate_symbol(next);
2468 declared++;
2469 typediff = type_difference(sym, next, 0, 0);
2470 if (typediff) {
2471 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2472 show_ident(sym->ident),
2473 stream_name(next->pos.stream), next->pos.line, typediff);
2474 return;
2477 if (!declared) {
2478 unsigned long mod = sym->ctype.modifiers;
2479 if (mod & (MOD_STATIC | MOD_REGISTER))
2480 return;
2481 if (!(mod & MOD_TOPLEVEL))
2482 return;
2483 if (!Wdecl)
2484 return;
2485 if (sym->ident == &main_ident)
2486 return;
2487 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
2491 static struct symbol *evaluate_symbol(struct symbol *sym)
2493 struct symbol *base_type;
2495 if (!sym)
2496 return sym;
2497 if (sym->evaluated)
2498 return sym;
2499 sym->evaluated = 1;
2501 sym = examine_symbol_type(sym);
2502 base_type = get_base_type(sym);
2503 if (!base_type)
2504 return NULL;
2506 /* Evaluate the initializers */
2507 if (sym->initializer)
2508 evaluate_initializer(sym, &sym->initializer);
2510 /* And finally, evaluate the body of the symbol too */
2511 if (base_type->type == SYM_FN) {
2512 struct symbol *curr = current_fn;
2514 current_fn = base_type;
2516 examine_fn_arguments(base_type);
2517 if (!base_type->stmt && base_type->inline_stmt)
2518 uninline(sym);
2519 if (base_type->stmt)
2520 evaluate_statement(base_type->stmt);
2522 current_fn = curr;
2525 return base_type;
2528 void evaluate_symbol_list(struct symbol_list *list)
2530 struct symbol *sym;
2532 FOR_EACH_PTR(list, sym) {
2533 evaluate_symbol(sym);
2534 check_duplicates(sym);
2535 } END_FOR_EACH_PTR(sym);
2538 static struct symbol *evaluate_return_expression(struct statement *stmt)
2540 struct expression *expr = stmt->expression;
2541 struct symbol *ctype, *fntype;
2543 evaluate_expression(expr);
2544 ctype = degenerate(expr);
2545 fntype = current_fn->ctype.base_type;
2546 if (!fntype || fntype == &void_ctype) {
2547 if (expr && ctype != &void_ctype)
2548 sparse_error(expr->pos, "return expression in %s function", fntype?"void":"typeless");
2549 return NULL;
2552 if (!expr) {
2553 sparse_error(stmt->pos, "return with no return value");
2554 return NULL;
2556 if (!ctype)
2557 return NULL;
2558 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression", '=');
2559 return NULL;
2562 static void evaluate_if_statement(struct statement *stmt)
2564 if (!stmt->if_conditional)
2565 return;
2567 evaluate_conditional(stmt->if_conditional, 0);
2568 evaluate_statement(stmt->if_true);
2569 evaluate_statement(stmt->if_false);
2572 static void evaluate_iterator(struct statement *stmt)
2574 evaluate_conditional(stmt->iterator_pre_condition, 1);
2575 evaluate_conditional(stmt->iterator_post_condition,1);
2576 evaluate_statement(stmt->iterator_pre_statement);
2577 evaluate_statement(stmt->iterator_statement);
2578 evaluate_statement(stmt->iterator_post_statement);
2581 static void verify_output_constraint(struct expression *expr, const char *constraint)
2583 switch (*constraint) {
2584 case '=': /* Assignment */
2585 case '+': /* Update */
2586 break;
2587 default:
2588 sparse_error(expr->pos, "output constraint is not an assignment constraint (\"%s\")", constraint);
2592 static void verify_input_constraint(struct expression *expr, const char *constraint)
2594 switch (*constraint) {
2595 case '=': /* Assignment */
2596 case '+': /* Update */
2597 sparse_error(expr->pos, "input constraint with assignment (\"%s\")", constraint);
2601 static void evaluate_asm_statement(struct statement *stmt)
2603 struct expression *expr;
2604 int state;
2606 expr = stmt->asm_string;
2607 if (!expr || expr->type != EXPR_STRING) {
2608 sparse_error(stmt->pos, "need constant string for inline asm");
2609 return;
2612 state = 0;
2613 FOR_EACH_PTR(stmt->asm_outputs, expr) {
2614 struct ident *ident;
2616 switch (state) {
2617 case 0: /* Identifier */
2618 state = 1;
2619 ident = (struct ident *)expr;
2620 continue;
2622 case 1: /* Constraint */
2623 state = 2;
2624 if (!expr || expr->type != EXPR_STRING) {
2625 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
2626 *THIS_ADDRESS(expr) = NULL;
2627 continue;
2629 verify_output_constraint(expr, expr->string->data);
2630 continue;
2632 case 2: /* Expression */
2633 state = 0;
2634 if (!evaluate_expression(expr))
2635 return;
2636 if (!lvalue_expression(expr))
2637 warning(expr->pos, "asm output is not an lvalue");
2638 evaluate_assign_to(expr, expr->ctype);
2639 continue;
2641 } END_FOR_EACH_PTR(expr);
2643 state = 0;
2644 FOR_EACH_PTR(stmt->asm_inputs, expr) {
2645 struct ident *ident;
2647 switch (state) {
2648 case 0: /* Identifier */
2649 state = 1;
2650 ident = (struct ident *)expr;
2651 continue;
2653 case 1: /* Constraint */
2654 state = 2;
2655 if (!expr || expr->type != EXPR_STRING) {
2656 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
2657 *THIS_ADDRESS(expr) = NULL;
2658 continue;
2660 verify_input_constraint(expr, expr->string->data);
2661 continue;
2663 case 2: /* Expression */
2664 state = 0;
2665 if (!evaluate_expression(expr))
2666 return;
2667 continue;
2669 } END_FOR_EACH_PTR(expr);
2671 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
2672 if (!expr) {
2673 sparse_error(stmt->pos, "bad asm output");
2674 return;
2676 if (expr->type == EXPR_STRING)
2677 continue;
2678 sparse_error(expr->pos, "asm clobber is not a string");
2679 } END_FOR_EACH_PTR(expr);
2682 static void evaluate_case_statement(struct statement *stmt)
2684 evaluate_expression(stmt->case_expression);
2685 evaluate_expression(stmt->case_to);
2686 evaluate_statement(stmt->case_statement);
2689 static void check_case_type(struct expression *switch_expr,
2690 struct expression *case_expr,
2691 struct expression **enumcase)
2693 struct symbol *switch_type, *case_type;
2694 int sclass, cclass;
2696 if (!case_expr)
2697 return;
2699 switch_type = switch_expr->ctype;
2700 case_type = evaluate_expression(case_expr);
2702 if (!switch_type || !case_type)
2703 goto Bad;
2704 if (enumcase) {
2705 if (*enumcase)
2706 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
2707 else if (is_enum_type(case_type))
2708 *enumcase = case_expr;
2711 sclass = classify_type(switch_type, &switch_type);
2712 cclass = classify_type(case_type, &case_type);
2714 /* both should be arithmetic */
2715 if (!(sclass & cclass & TYPE_NUM))
2716 goto Bad;
2718 /* neither should be floating */
2719 if ((sclass | cclass) & TYPE_FLOAT)
2720 goto Bad;
2722 /* if neither is restricted, we are OK */
2723 if (!((sclass | cclass) & TYPE_RESTRICT))
2724 return;
2726 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
2727 cclass, sclass, case_type, switch_type))
2728 warning(case_expr->pos, "restricted degrades to integer");
2730 return;
2732 Bad:
2733 sparse_error(case_expr->pos, "incompatible types for 'case' statement");
2736 static void evaluate_switch_statement(struct statement *stmt)
2738 struct symbol *sym;
2739 struct expression *enumcase = NULL;
2740 struct expression **enumcase_holder = &enumcase;
2741 struct expression *sel = stmt->switch_expression;
2743 evaluate_expression(sel);
2744 evaluate_statement(stmt->switch_statement);
2745 if (!sel)
2746 return;
2747 if (sel->ctype && is_enum_type(sel->ctype))
2748 enumcase_holder = NULL; /* Only check cases against switch */
2750 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
2751 struct statement *case_stmt = sym->stmt;
2752 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
2753 check_case_type(sel, case_stmt->case_to, enumcase_holder);
2754 } END_FOR_EACH_PTR(sym);
2757 struct symbol *evaluate_statement(struct statement *stmt)
2759 if (!stmt)
2760 return NULL;
2762 switch (stmt->type) {
2763 case STMT_DECLARATION: {
2764 struct symbol *s;
2765 FOR_EACH_PTR(stmt->declaration, s) {
2766 evaluate_symbol(s);
2767 } END_FOR_EACH_PTR(s);
2768 return NULL;
2771 case STMT_RETURN:
2772 return evaluate_return_expression(stmt);
2774 case STMT_EXPRESSION:
2775 if (!evaluate_expression(stmt->expression))
2776 return NULL;
2777 return degenerate(stmt->expression);
2779 case STMT_COMPOUND: {
2780 struct statement *s;
2781 struct symbol *type = NULL;
2783 /* Evaluate the return symbol in the compound statement */
2784 evaluate_symbol(stmt->ret);
2787 * Then, evaluate each statement, making the type of the
2788 * compound statement be the type of the last statement
2790 type = NULL;
2791 FOR_EACH_PTR(stmt->stmts, s) {
2792 type = evaluate_statement(s);
2793 } END_FOR_EACH_PTR(s);
2794 if (!type)
2795 type = &void_ctype;
2796 return type;
2798 case STMT_IF:
2799 evaluate_if_statement(stmt);
2800 return NULL;
2801 case STMT_ITERATOR:
2802 evaluate_iterator(stmt);
2803 return NULL;
2804 case STMT_SWITCH:
2805 evaluate_switch_statement(stmt);
2806 return NULL;
2807 case STMT_CASE:
2808 evaluate_case_statement(stmt);
2809 return NULL;
2810 case STMT_LABEL:
2811 return evaluate_statement(stmt->label_statement);
2812 case STMT_GOTO:
2813 evaluate_expression(stmt->goto_expression);
2814 return NULL;
2815 case STMT_NONE:
2816 break;
2817 case STMT_ASM:
2818 evaluate_asm_statement(stmt);
2819 return NULL;
2820 case STMT_CONTEXT:
2821 evaluate_expression(stmt->expression);
2822 return NULL;
2823 case STMT_RANGE:
2824 evaluate_expression(stmt->range_expression);
2825 evaluate_expression(stmt->range_low);
2826 evaluate_expression(stmt->range_high);
2827 return NULL;
2829 return NULL;