Fix most -Wshadow warnings in Sparse.
[smatch.git] / evaluate.c
blob59b3d32edc1ce8ecc33f6bc34e8a1473f17202c3
1 /*
2 * sparse/evaluate.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
9 * Evaluate constant expressions.
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <stddef.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <limits.h>
21 #include "lib.h"
22 #include "allocate.h"
23 #include "parse.h"
24 #include "token.h"
25 #include "symbol.h"
26 #include "target.h"
27 #include "expression.h"
29 struct symbol *current_fn;
31 static struct symbol *degenerate(struct expression *expr);
32 static struct symbol *evaluate_symbol(struct symbol *sym);
34 static struct symbol *evaluate_symbol_expression(struct expression *expr)
36 struct expression *addr;
37 struct symbol *sym = expr->symbol;
38 struct symbol *base_type;
40 if (!sym) {
41 expression_error(expr, "undefined identifier '%s'", show_ident(expr->symbol_name));
42 return NULL;
45 examine_symbol_type(sym);
47 base_type = get_base_type(sym);
48 if (!base_type) {
49 expression_error(expr, "identifier '%s' has no type", show_ident(expr->symbol_name));
50 return NULL;
53 addr = alloc_expression(expr->pos, EXPR_SYMBOL);
54 addr->symbol = sym;
55 addr->symbol_name = expr->symbol_name;
56 addr->ctype = &lazy_ptr_ctype; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
57 expr->type = EXPR_PREOP;
58 expr->op = '*';
59 expr->unop = addr;
61 /* The type of a symbol is the symbol itself! */
62 expr->ctype = sym;
63 return sym;
66 static struct symbol *evaluate_string(struct expression *expr)
68 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
69 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
70 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
71 struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
72 unsigned int length = expr->string->length;
74 sym->array_size = alloc_const_expression(expr->pos, length);
75 sym->bit_size = bits_in_char * length;
76 sym->ctype.alignment = 1;
77 sym->string = 1;
78 sym->ctype.modifiers = MOD_STATIC;
79 sym->ctype.base_type = array;
80 sym->initializer = initstr;
82 initstr->ctype = sym;
83 initstr->string = expr->string;
85 array->array_size = sym->array_size;
86 array->bit_size = bits_in_char * length;
87 array->ctype.alignment = 1;
88 array->ctype.modifiers = MOD_STATIC;
89 array->ctype.base_type = &char_ctype;
91 addr->symbol = sym;
92 addr->ctype = &lazy_ptr_ctype;
94 expr->type = EXPR_PREOP;
95 expr->op = '*';
96 expr->unop = addr;
97 expr->ctype = sym;
98 return sym;
101 static inline struct symbol *integer_promotion(struct symbol *type)
103 struct symbol *orig_type = type;
104 unsigned long mod = type->ctype.modifiers;
105 int width;
107 if (type->type == SYM_NODE)
108 type = type->ctype.base_type;
109 if (type->type == SYM_ENUM)
110 type = type->ctype.base_type;
111 width = type->bit_size;
114 * Bitfields always promote to the base type,
115 * even if the bitfield might be bigger than
116 * an "int".
118 if (type->type == SYM_BITFIELD) {
119 type = type->ctype.base_type;
120 orig_type = type;
122 mod = type->ctype.modifiers;
123 if (width < bits_in_int)
124 return &int_ctype;
126 /* If char/short has as many bits as int, it still gets "promoted" */
127 if (mod & (MOD_CHAR | MOD_SHORT)) {
128 if (mod & MOD_UNSIGNED)
129 return &uint_ctype;
130 return &int_ctype;
132 return orig_type;
136 * integer part of usual arithmetic conversions:
137 * integer promotions are applied
138 * if left and right are identical, we are done
139 * if signedness is the same, convert one with lower rank
140 * unless unsigned argument has rank lower than signed one, convert the
141 * signed one.
142 * if signed argument is bigger than unsigned one, convert the unsigned.
143 * otherwise, convert signed.
145 * Leaving aside the integer promotions, that is equivalent to
146 * if identical, don't convert
147 * if left is bigger than right, convert right
148 * if right is bigger than left, convert right
149 * otherwise, if signedness is the same, convert one with lower rank
150 * otherwise convert the signed one.
152 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
154 unsigned long lmod, rmod;
156 left = integer_promotion(left);
157 right = integer_promotion(right);
159 if (left == right)
160 goto left;
162 if (left->bit_size > right->bit_size)
163 goto left;
165 if (right->bit_size > left->bit_size)
166 goto right;
168 lmod = left->ctype.modifiers;
169 rmod = right->ctype.modifiers;
170 if ((lmod ^ rmod) & MOD_UNSIGNED) {
171 if (lmod & MOD_UNSIGNED)
172 goto left;
173 } else if ((lmod & ~rmod) & (MOD_LONG | MOD_LONGLONG))
174 goto left;
175 right:
176 left = right;
177 left:
178 return left;
181 static int same_cast_type(struct symbol *orig, struct symbol *new)
183 return orig->bit_size == new->bit_size && orig->bit_offset == new->bit_offset;
186 static struct symbol *base_type(struct symbol *node, unsigned long *modp, unsigned long *asp)
188 unsigned long mod, as;
190 mod = 0; as = 0;
191 while (node) {
192 mod |= node->ctype.modifiers;
193 as |= node->ctype.as;
194 if (node->type == SYM_NODE) {
195 node = node->ctype.base_type;
196 continue;
198 break;
200 *modp = mod & ~MOD_IGNORE;
201 *asp = as;
202 return node;
205 static int is_same_type(struct expression *expr, struct symbol *new)
207 struct symbol *old = expr->ctype;
208 unsigned long oldmod, newmod, oldas, newas;
210 old = base_type(old, &oldmod, &oldas);
211 new = base_type(new, &newmod, &newas);
213 /* Same base type, same address space? */
214 if (old == new && oldas == newas) {
215 unsigned long difmod;
217 /* Check the modifier bits. */
218 difmod = (oldmod ^ newmod) & ~MOD_NOCAST;
220 /* Exact same type? */
221 if (!difmod)
222 return 1;
225 * Not the same type, but differs only in "const".
226 * Don't warn about MOD_NOCAST.
228 if (difmod == MOD_CONST)
229 return 0;
231 if ((oldmod | newmod) & MOD_NOCAST) {
232 const char *tofrom = "to/from";
233 if (!(newmod & MOD_NOCAST))
234 tofrom = "from";
235 if (!(oldmod & MOD_NOCAST))
236 tofrom = "to";
237 warning(expr->pos, "implicit cast %s nocast type", tofrom);
239 return 0;
242 static void
243 warn_for_different_enum_types (struct position pos,
244 struct symbol *typea,
245 struct symbol *typeb)
247 if (!Wenum_mismatch)
248 return;
249 if (typea->type == SYM_NODE)
250 typea = typea->ctype.base_type;
251 if (typeb->type == SYM_NODE)
252 typeb = typeb->ctype.base_type;
254 if (typea == typeb)
255 return;
257 if (typea->type == SYM_ENUM && typeb->type == SYM_ENUM) {
258 warning(pos, "mixing different enum types");
259 info(pos, " %s versus", show_typename(typea));
260 info(pos, " %s", show_typename(typeb));
265 * This gets called for implicit casts in assignments and
266 * integer promotion. We often want to try to move the
267 * cast down, because the ops involved may have been
268 * implicitly cast up, and we can get rid of the casts
269 * early.
271 static struct expression * cast_to(struct expression *old, struct symbol *type)
273 struct expression *expr;
275 warn_for_different_enum_types (old->pos, old->ctype, type);
277 if (is_same_type(old, type))
278 return old;
281 * See if we can simplify the op. Move the cast down.
283 switch (old->type) {
284 case EXPR_PREOP:
285 if (old->ctype->bit_size < type->bit_size)
286 break;
287 if (old->op == '~') {
288 old->ctype = type;
289 old->unop = cast_to(old->unop, type);
290 return old;
292 break;
294 case EXPR_IMPLIED_CAST:
295 warn_for_different_enum_types(old->pos, old->ctype, type);
297 if (old->ctype->bit_size >= type->bit_size) {
298 struct expression *orig = old->cast_expression;
299 if (same_cast_type(orig->ctype, type))
300 return orig;
301 if (old->ctype->bit_offset == type->bit_offset) {
302 old->ctype = type;
303 old->cast_type = type;
304 return old;
307 break;
309 default:
310 /* nothing */;
313 expr = alloc_expression(old->pos, EXPR_IMPLIED_CAST);
314 expr->ctype = type;
315 expr->cast_type = type;
316 expr->cast_expression = old;
317 return expr;
320 static int is_type_type(struct symbol *type)
322 return (type->ctype.modifiers & MOD_TYPE) != 0;
325 int is_ptr_type(struct symbol *type)
327 if (type->type == SYM_NODE)
328 type = type->ctype.base_type;
329 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
332 static inline int is_float_type(struct symbol *type)
334 if (type->type == SYM_NODE)
335 type = type->ctype.base_type;
336 return type->ctype.base_type == &fp_type;
339 static inline int is_byte_type(struct symbol *type)
341 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
344 enum {
345 TYPE_NUM = 1,
346 TYPE_BITFIELD = 2,
347 TYPE_RESTRICT = 4,
348 TYPE_FLOAT = 8,
349 TYPE_PTR = 16,
350 TYPE_COMPOUND = 32,
351 TYPE_FOULED = 64,
354 static inline int classify_type(struct symbol *type, struct symbol **base)
356 static int type_class[SYM_BAD + 1] = {
357 [SYM_PTR] = TYPE_PTR,
358 [SYM_FN] = TYPE_PTR,
359 [SYM_ARRAY] = TYPE_PTR | TYPE_COMPOUND,
360 [SYM_STRUCT] = TYPE_COMPOUND,
361 [SYM_UNION] = TYPE_COMPOUND,
362 [SYM_BITFIELD] = TYPE_NUM | TYPE_BITFIELD,
363 [SYM_RESTRICT] = TYPE_NUM | TYPE_RESTRICT,
364 [SYM_FOULED] = TYPE_NUM | TYPE_RESTRICT | TYPE_FOULED,
366 if (type->type == SYM_NODE)
367 type = type->ctype.base_type;
368 if (type->type == SYM_ENUM)
369 type = type->ctype.base_type;
370 *base = type;
371 if (type->type == SYM_BASETYPE) {
372 if (type->ctype.base_type == &int_type)
373 return TYPE_NUM;
374 if (type->ctype.base_type == &fp_type)
375 return TYPE_NUM | TYPE_FLOAT;
377 return type_class[type->type];
380 static inline int is_string_type(struct symbol *type)
382 if (type->type == SYM_NODE)
383 type = type->ctype.base_type;
384 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
387 static struct symbol *bad_expr_type(struct expression *expr)
389 sparse_error(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
390 switch (expr->type) {
391 case EXPR_BINOP:
392 case EXPR_COMPARE:
393 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
394 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
395 break;
396 case EXPR_PREOP:
397 case EXPR_POSTOP:
398 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
399 break;
400 default:
401 break;
404 return expr->ctype = &bad_ctype;
407 static int restricted_value(struct expression *v, struct symbol *type)
409 if (v->type != EXPR_VALUE)
410 return 1;
411 if (v->value != 0)
412 return 1;
413 return 0;
416 static int restricted_binop(int op, struct symbol *type)
418 switch (op) {
419 case '&':
420 case '=':
421 case SPECIAL_AND_ASSIGN:
422 case SPECIAL_OR_ASSIGN:
423 case SPECIAL_XOR_ASSIGN:
424 return 1; /* unfoul */
425 case '|':
426 case '^':
427 case '?':
428 return 2; /* keep fouled */
429 case SPECIAL_EQUAL:
430 case SPECIAL_NOTEQUAL:
431 return 3; /* warn if fouled */
432 default:
433 return 0; /* warn */
437 static int restricted_unop(int op, struct symbol **type)
439 if (op == '~') {
440 if ((*type)->bit_size < bits_in_int)
441 *type = befoul(*type);
442 return 0;
443 } if (op == '+')
444 return 0;
445 return 1;
448 static struct symbol *restricted_binop_type(int op,
449 struct expression *left,
450 struct expression *right,
451 int lclass, int rclass,
452 struct symbol *ltype,
453 struct symbol *rtype)
455 struct symbol *ctype = NULL;
456 if (lclass & TYPE_RESTRICT) {
457 if (rclass & TYPE_RESTRICT) {
458 if (ltype == rtype) {
459 ctype = ltype;
460 } else if (lclass & TYPE_FOULED) {
461 if (ltype->ctype.base_type == rtype)
462 ctype = ltype;
463 } else if (rclass & TYPE_FOULED) {
464 if (rtype->ctype.base_type == ltype)
465 ctype = rtype;
467 } else {
468 if (!restricted_value(right, ltype))
469 ctype = ltype;
471 } else if (!restricted_value(left, rtype))
472 ctype = rtype;
474 if (ctype) {
475 switch (restricted_binop(op, ctype)) {
476 case 1:
477 if ((lclass ^ rclass) & TYPE_FOULED)
478 ctype = ctype->ctype.base_type;
479 break;
480 case 3:
481 if (!(lclass & rclass & TYPE_FOULED))
482 break;
483 case 0:
484 ctype = NULL;
485 default:
486 break;
490 return ctype;
493 static struct symbol *usual_conversions(int op,
494 struct expression **left,
495 struct expression **right,
496 int lclass, int rclass,
497 struct symbol *ltype,
498 struct symbol *rtype)
500 struct symbol *ctype;
502 warn_for_different_enum_types((*right)->pos, (*left)->ctype, (*right)->ctype);
504 if ((lclass | rclass) & TYPE_RESTRICT)
505 goto Restr;
507 Normal:
508 if (!(lclass & TYPE_FLOAT)) {
509 if (!(rclass & TYPE_FLOAT))
510 ctype = bigger_int_type(ltype, rtype);
511 else
512 ctype = rtype;
513 } else if (rclass & TYPE_FLOAT) {
514 unsigned long lmod = ltype->ctype.modifiers;
515 unsigned long rmod = rtype->ctype.modifiers;
516 if (rmod & ~lmod & (MOD_LONG | MOD_LONGLONG))
517 ctype = rtype;
518 else
519 ctype = ltype;
520 } else
521 ctype = ltype;
523 Convert:
524 *left = cast_to(*left, ctype);
525 *right = cast_to(*right, ctype);
526 return ctype;
528 Restr:
529 ctype = restricted_binop_type(op, *left, *right,
530 lclass, rclass, ltype, rtype);
531 if (ctype)
532 goto Convert;
534 if (lclass & TYPE_RESTRICT) {
535 warning((*left)->pos, "restricted degrades to integer");
536 ltype = ltype->ctype.base_type;
537 if (is_restricted_type(ltype)) /* was fouled */
538 ltype = ltype->ctype.base_type;
540 if (rclass & TYPE_RESTRICT) {
541 warning((*right)->pos, "restricted degrades to integer");
542 rtype = rtype->ctype.base_type;
543 if (is_restricted_type(rtype)) /* was fouled */
544 rtype = rtype->ctype.base_type;
546 goto Normal;
549 static struct symbol *evaluate_arith(struct expression *expr, int float_ok)
551 struct symbol *ltype, *rtype;
552 int lclass = classify_type(expr->left->ctype, &ltype);
553 int rclass = classify_type(expr->right->ctype, &rtype);
554 struct symbol *ctype;
556 if (!(lclass & rclass & TYPE_NUM))
557 goto Bad;
559 if (!float_ok && (lclass | rclass) & TYPE_FLOAT)
560 goto Bad;
562 ctype = usual_conversions(expr->op, &expr->left, &expr->right,
563 lclass, rclass, ltype, rtype);
564 expr->ctype = ctype;
565 return ctype;
567 Bad:
568 return bad_expr_type(expr);
571 static inline int lvalue_expression(struct expression *expr)
573 return expr->type == EXPR_PREOP && expr->op == '*';
576 static int ptr_object_size(struct symbol *ptr_type)
578 if (ptr_type->type == SYM_NODE)
579 ptr_type = ptr_type->ctype.base_type;
580 if (ptr_type->type == SYM_PTR)
581 ptr_type = get_base_type(ptr_type);
582 return ptr_type->bit_size;
585 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *ctype, struct expression **ip)
587 struct expression *i = *ip;
588 struct symbol *ptr_type = ctype;
589 int bit_size;
591 if (ptr_type->type == SYM_NODE)
592 ptr_type = ptr_type->ctype.base_type;
594 if (!is_int_type(i->ctype))
595 return bad_expr_type(expr);
597 examine_symbol_type(ctype);
599 if (!ctype->ctype.base_type) {
600 expression_error(expr, "missing type information");
601 return NULL;
604 /* Get the size of whatever the pointer points to */
605 bit_size = ptr_object_size(ctype);
607 if (bit_size > bits_in_char) {
608 int multiply = bit_size >> 3;
609 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
611 if (i->type == EXPR_VALUE) {
612 val->value = i->value * multiply;
613 val->ctype = size_t_ctype;
614 *ip = val;
615 } else {
616 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
618 val->ctype = size_t_ctype;
619 val->value = bit_size >> 3;
621 mul->op = '*';
622 mul->ctype = size_t_ctype;
623 mul->left = i;
624 mul->right = val;
626 *ip = mul;
630 expr->ctype = ctype;
631 return ctype;
634 static struct symbol *evaluate_add(struct expression *expr)
636 struct expression *left = expr->left, *right = expr->right;
637 struct symbol *ltype = left->ctype, *rtype = right->ctype;
639 if (is_ptr_type(ltype))
640 return evaluate_ptr_add(expr, degenerate(left), &expr->right);
642 if (is_ptr_type(rtype))
643 return evaluate_ptr_add(expr, degenerate(right), &expr->left);
645 return evaluate_arith(expr, 1);
648 const char * type_difference(struct symbol *target, struct symbol *source,
649 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
651 for (;;) {
652 unsigned long mod1, mod2, diff;
653 unsigned long as1, as2;
654 int type1, type2;
655 struct symbol *base1, *base2;
657 if (target == source)
658 break;
659 if (!target || !source)
660 return "different types";
662 * Peel of per-node information.
663 * FIXME! Check alignment and context too here!
665 mod1 = target->ctype.modifiers;
666 as1 = target->ctype.as;
667 mod2 = source->ctype.modifiers;
668 as2 = source->ctype.as;
669 if (target->type == SYM_NODE) {
670 target = target->ctype.base_type;
671 if (!target)
672 return "bad types";
673 if (target->type == SYM_PTR) {
674 mod1 = 0;
675 as1 = 0;
677 mod1 |= target->ctype.modifiers;
678 as1 |= target->ctype.as;
680 if (source->type == SYM_NODE) {
681 source = source->ctype.base_type;
682 if (!source)
683 return "bad types";
684 if (source->type == SYM_PTR) {
685 mod2 = 0;
686 as2 = 0;
688 mod2 |= source->ctype.modifiers;
689 as2 |= source->ctype.as;
691 if (target->type == SYM_ENUM) {
692 target = target->ctype.base_type;
693 if (!target)
694 return "bad types";
696 if (source->type == SYM_ENUM) {
697 source = source->ctype.base_type;
698 if (!source)
699 return "bad types";
702 if (target == source)
703 break;
704 if (!target || !source)
705 return "different types";
707 type1 = target->type;
708 base1 = target->ctype.base_type;
710 type2 = source->type;
711 base2 = source->ctype.base_type;
714 * Pointers to functions compare as the function itself
716 if (type1 == SYM_PTR && base1) {
717 base1 = examine_symbol_type(base1);
718 switch (base1->type) {
719 case SYM_FN:
720 type1 = SYM_FN;
721 target = base1;
722 base1 = base1->ctype.base_type;
723 default:
724 /* nothing */;
727 if (type2 == SYM_PTR && base2) {
728 base2 = examine_symbol_type(base2);
729 switch (base2->type) {
730 case SYM_FN:
731 type2 = SYM_FN;
732 source = base2;
733 base2 = base2->ctype.base_type;
734 default:
735 /* nothing */;
739 /* Arrays degenerate to pointers for type comparisons */
740 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
741 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
743 if (type1 != type2 || type1 == SYM_RESTRICT)
744 return "different base types";
746 /* Must be same address space to be comparable */
747 if (Waddress_space && as1 != as2)
748 return "different address spaces";
750 /* Ignore differences in storage types or addressability */
751 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
752 diff &= (mod1 & ~target_mod_ignore) | (mod2 & ~source_mod_ignore);
753 if (diff) {
754 if (diff & MOD_SIZE)
755 return "different type sizes";
756 if (diff & ~MOD_SIGNEDNESS)
757 return "different modifiers";
759 /* Differs in signedness only.. */
760 if (Wtypesign) {
762 * Warn if both are explicitly signed ("unsigned" is obviously
763 * always explicit, and since we know one of them has to be
764 * unsigned, we check if the signed one was explicit).
766 if ((mod1 | mod2) & MOD_EXPLICITLY_SIGNED)
767 return "different explicit signedness";
770 * "char" matches both "unsigned char" and "signed char",
771 * so if the explicit test didn't trigger, then we should
772 * not warn about a char.
774 if (!(mod1 & MOD_CHAR))
775 return "different signedness";
779 if (type1 == SYM_FN) {
780 int i;
781 struct symbol *arg1, *arg2;
782 if (base1->variadic != base2->variadic)
783 return "incompatible variadic arguments";
784 PREPARE_PTR_LIST(target->arguments, arg1);
785 PREPARE_PTR_LIST(source->arguments, arg2);
786 i = 1;
787 for (;;) {
788 const char *diffstr;
789 diffstr = type_difference(arg1, arg2, 0, 0);
790 if (diffstr) {
791 static char argdiff[80];
792 sprintf(argdiff, "incompatible argument %d (%s)", i, diffstr);
793 return argdiff;
795 if (!arg1)
796 break;
797 NEXT_PTR_LIST(arg1);
798 NEXT_PTR_LIST(arg2);
799 i++;
801 FINISH_PTR_LIST(arg2);
802 FINISH_PTR_LIST(arg1);
805 target = base1;
806 source = base2;
808 return NULL;
811 static int is_null_ptr(struct expression *expr)
813 if (expr->type != EXPR_VALUE || expr->value)
814 return 0;
815 if (!is_ptr_type(expr->ctype))
816 warning(expr->pos, "Using plain integer as NULL pointer");
817 return 1;
820 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
822 /* NULL expression? Just return the type of the "other side" */
823 if (is_null_ptr(r))
824 return l->ctype;
825 if (is_null_ptr(l))
826 return r->ctype;
827 return NULL;
831 * Ignore differences in "volatile" and "const"ness when
832 * subtracting pointers
834 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
836 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression **rp)
838 const char *typediff;
839 struct symbol *ctype;
840 struct symbol *ltype, *rtype;
841 struct expression *r = *rp;
843 ltype = degenerate(l);
844 rtype = degenerate(r);
847 * If it is an integer subtract: the ptr add case will do the
848 * right thing.
850 if (!is_ptr_type(rtype))
851 return evaluate_ptr_add(expr, degenerate(l), rp);
853 ctype = ltype;
854 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
855 if (typediff) {
856 ctype = common_ptr_type(l, r);
857 if (!ctype) {
858 expression_error(expr, "subtraction of different types can't work (%s)", typediff);
859 return NULL;
862 examine_symbol_type(ctype);
864 /* Figure out the base type we point to */
865 if (ctype->type == SYM_NODE)
866 ctype = ctype->ctype.base_type;
867 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
868 expression_error(expr, "subtraction of functions? Share your drugs");
869 return NULL;
871 ctype = get_base_type(ctype);
873 expr->ctype = ssize_t_ctype;
874 if (ctype->bit_size > bits_in_char) {
875 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
876 struct expression *div = expr;
877 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
878 unsigned long value = ctype->bit_size >> 3;
880 val->ctype = size_t_ctype;
881 val->value = value;
883 if (value & (value-1)) {
884 if (Wptr_subtraction_blows)
885 warning(expr->pos, "potentially expensive pointer subtraction");
888 sub->op = '-';
889 sub->ctype = ssize_t_ctype;
890 sub->left = l;
891 sub->right = r;
893 div->op = '/';
894 div->left = sub;
895 div->right = val;
898 return ssize_t_ctype;
901 static struct symbol *evaluate_sub(struct expression *expr)
903 struct expression *left = expr->left;
904 struct symbol *ltype = left->ctype;
906 if (is_ptr_type(ltype))
907 return evaluate_ptr_sub(expr, left, &expr->right);
909 return evaluate_arith(expr, 1);
912 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
914 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
916 struct symbol *ctype;
918 if (!expr)
919 return NULL;
921 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
922 warning(expr->pos, "assignment expression in conditional");
924 ctype = evaluate_expression(expr);
925 if (ctype) {
926 if (is_safe_type(ctype))
927 warning(expr->pos, "testing a 'safe expression'");
930 return ctype;
933 static struct symbol *evaluate_logical(struct expression *expr)
935 if (!evaluate_conditional(expr->left, 0))
936 return NULL;
937 if (!evaluate_conditional(expr->right, 0))
938 return NULL;
940 expr->ctype = &bool_ctype;
941 return &bool_ctype;
944 static struct symbol *evaluate_shift(struct expression *expr)
946 struct expression *left = expr->left, *right = expr->right;
947 struct symbol *ltype = left->ctype, *rtype = right->ctype;
949 if (ltype->type == SYM_NODE)
950 ltype = ltype->ctype.base_type;
951 if (rtype->type == SYM_NODE)
952 rtype = rtype->ctype.base_type;
953 if (is_int_type(ltype) && is_int_type(rtype)) {
954 struct symbol *ctype = integer_promotion(ltype);
955 expr->left = cast_to(expr->left, ctype);
956 expr->ctype = ctype;
957 ctype = integer_promotion(rtype);
958 expr->right = cast_to(expr->right, ctype);
959 return expr->ctype;
961 return bad_expr_type(expr);
964 static struct symbol *evaluate_binop(struct expression *expr)
966 switch (expr->op) {
967 // addition can take ptr+int, fp and int
968 case '+':
969 return evaluate_add(expr);
971 // subtraction can take ptr-ptr, fp and int
972 case '-':
973 return evaluate_sub(expr);
975 // Arithmetic operations can take fp and int
976 case '*': case '/':
977 return evaluate_arith(expr, 1);
979 // shifts do integer promotions, but that's it.
980 case SPECIAL_LEFTSHIFT: case SPECIAL_RIGHTSHIFT:
981 return evaluate_shift(expr);
983 // The rest are integer operations
984 // '%', '&', '^', '|'
985 default:
986 return evaluate_arith(expr, 0);
990 static struct symbol *evaluate_comma(struct expression *expr)
992 expr->ctype = expr->right->ctype;
993 return expr->ctype;
996 static int modify_for_unsigned(int op)
998 if (op == '<')
999 op = SPECIAL_UNSIGNED_LT;
1000 else if (op == '>')
1001 op = SPECIAL_UNSIGNED_GT;
1002 else if (op == SPECIAL_LTE)
1003 op = SPECIAL_UNSIGNED_LTE;
1004 else if (op == SPECIAL_GTE)
1005 op = SPECIAL_UNSIGNED_GTE;
1006 return op;
1009 static struct symbol *evaluate_compare(struct expression *expr)
1011 struct expression *left = expr->left, *right = expr->right;
1012 struct symbol *ltype = left->ctype, *rtype = right->ctype;
1013 struct symbol *ctype;
1015 /* Type types? */
1016 if (is_type_type(ltype) && is_type_type(rtype))
1017 goto OK;
1019 if (is_safe_type(ltype) || is_safe_type(rtype))
1020 warning(expr->pos, "testing a 'safe expression'");
1022 /* Pointer types? */
1023 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
1024 // FIXME! Check the types for compatibility
1025 expr->op = modify_for_unsigned(expr->op);
1026 goto OK;
1029 ctype = evaluate_arith(expr, 1);
1030 if (ctype) {
1031 if (ctype->ctype.modifiers & MOD_UNSIGNED)
1032 expr->op = modify_for_unsigned(expr->op);
1035 expr->ctype = &bool_ctype;
1036 return &bool_ctype;
1040 * FIXME!! This should do casts, array degeneration etc..
1042 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
1044 struct symbol *ltype = left->ctype, *rtype = right->ctype;
1046 if (ltype->type == SYM_NODE)
1047 ltype = ltype->ctype.base_type;
1049 if (rtype->type == SYM_NODE)
1050 rtype = rtype->ctype.base_type;
1052 if (ltype->type == SYM_PTR) {
1053 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
1054 return ltype;
1057 if (rtype->type == SYM_PTR) {
1058 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
1059 return rtype;
1061 return NULL;
1065 * NOTE! The degenerate case of "x ? : y", where we don't
1066 * have a true case, this will possibly promote "x" to the
1067 * same type as "y", and thus _change_ the conditional
1068 * test in the expression. But since promotion is "safe"
1069 * for testing, that's OK.
1071 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1073 struct expression **true;
1074 struct symbol *ctype, *ltype, *rtype;
1075 int lclass, rclass;
1076 const char * typediff;
1078 if (!evaluate_conditional(expr->conditional, 0))
1079 return NULL;
1080 if (!evaluate_expression(expr->cond_false))
1081 return NULL;
1083 ctype = degenerate(expr->conditional);
1084 rtype = degenerate(expr->cond_false);
1086 true = &expr->conditional;
1087 ltype = ctype;
1088 if (expr->cond_true) {
1089 if (!evaluate_expression(expr->cond_true))
1090 return NULL;
1091 ltype = degenerate(expr->cond_true);
1092 true = &expr->cond_true;
1095 lclass = classify_type(ltype, &ltype);
1096 rclass = classify_type(rtype, &rtype);
1097 if (lclass & rclass & TYPE_NUM) {
1098 ctype = usual_conversions('?', true, &expr->cond_false,
1099 lclass, rclass, ltype, rtype);
1100 goto out;
1102 ctype = compatible_ptr_type(*true, expr->cond_false);
1103 if (ctype)
1104 goto out;
1105 ctype = ltype;
1106 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
1107 if (!typediff)
1108 goto out;
1109 expression_error(expr, "incompatible types in conditional expression (%s)", typediff);
1110 return NULL;
1112 out:
1113 expr->ctype = ctype;
1114 return ctype;
1117 /* FP assignments can not do modulo or bit operations */
1118 static int compatible_float_op(int op)
1120 return op == '=' ||
1121 op == SPECIAL_ADD_ASSIGN ||
1122 op == SPECIAL_SUB_ASSIGN ||
1123 op == SPECIAL_MUL_ASSIGN ||
1124 op == SPECIAL_DIV_ASSIGN;
1127 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1128 struct expression **rp, struct symbol *source, const char *where, int op)
1130 const char *typediff;
1131 struct symbol *t, *s;
1132 int target_as;
1133 int tclass = classify_type(target, &t);
1134 int sclass = classify_type(source, &s);
1136 if (tclass & sclass & TYPE_NUM) {
1137 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1138 expression_error(expr, "invalid assignment");
1139 return 0;
1141 if (tclass & TYPE_RESTRICT) {
1142 if (!restricted_binop(op, target)) {
1143 expression_error(expr, "bad restricted assignment");
1144 return 0;
1146 /* allowed assignments unfoul */
1147 if (sclass & TYPE_FOULED && s->ctype.base_type == t)
1148 goto Cast;
1149 if (!restricted_value(*rp, target))
1150 return 1;
1151 } else if (!(sclass & TYPE_RESTRICT))
1152 goto Cast;
1153 } else if (tclass & TYPE_PTR) {
1154 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1155 evaluate_ptr_add(expr, target, rp);
1156 return 1;
1158 if (op != '=') {
1159 expression_error(expr, "invalid pointer assignment");
1160 return 0;
1162 } else if (op != '=') {
1163 expression_error(expr, "invalid assignment");
1164 return 0;
1167 /* It's OK if the target is more volatile or const than the source */
1168 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
1169 if (!typediff)
1170 return 1;
1172 /* Pointer destination? */
1173 if (tclass & TYPE_PTR) {
1174 struct expression *right = *rp;
1175 int source_as;
1177 // NULL pointer is always OK
1178 if (is_null_ptr(right))
1179 goto Cast;
1181 /* "void *" matches anything as long as the address space is OK */
1182 target_as = t->ctype.as | target->ctype.as;
1183 source_as = s->ctype.as | source->ctype.as;
1184 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
1185 s = get_base_type(s);
1186 t = get_base_type(t);
1187 if (s == &void_ctype || t == &void_ctype)
1188 goto Cast;
1192 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1193 info(expr->pos, " expected %s", show_typename(target));
1194 info(expr->pos, " got %s", show_typename(source));
1195 *rp = cast_to(*rp, target);
1196 return 0;
1197 Cast:
1198 *rp = cast_to(*rp, target);
1199 return 1;
1202 static void mark_assigned(struct expression *expr)
1204 struct symbol *sym;
1206 if (!expr)
1207 return;
1208 switch (expr->type) {
1209 case EXPR_SYMBOL:
1210 sym = expr->symbol;
1211 if (!sym)
1212 return;
1213 if (sym->type != SYM_NODE)
1214 return;
1215 sym->ctype.modifiers |= MOD_ASSIGNED;
1216 return;
1218 case EXPR_BINOP:
1219 mark_assigned(expr->left);
1220 mark_assigned(expr->right);
1221 return;
1222 case EXPR_CAST:
1223 mark_assigned(expr->cast_expression);
1224 return;
1225 case EXPR_SLICE:
1226 mark_assigned(expr->base);
1227 return;
1228 default:
1229 /* Hmm? */
1230 return;
1234 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1236 if (type->ctype.modifiers & MOD_CONST)
1237 expression_error(left, "assignment to const expression");
1239 /* We know left is an lvalue, so it's a "preop-*" */
1240 mark_assigned(left->unop);
1243 static struct symbol *evaluate_assignment(struct expression *expr)
1245 struct expression *left = expr->left, *right = expr->right;
1246 struct expression *where = expr;
1247 struct symbol *ltype, *rtype;
1249 if (!lvalue_expression(left)) {
1250 expression_error(expr, "not an lvalue");
1251 return NULL;
1254 ltype = left->ctype;
1256 rtype = degenerate(right);
1258 if (!compatible_assignment_types(where, ltype, &where->right, rtype, "assignment", expr->op))
1259 return NULL;
1261 evaluate_assign_to(left, ltype);
1263 expr->ctype = ltype;
1264 return ltype;
1267 static void examine_fn_arguments(struct symbol *fn)
1269 struct symbol *s;
1271 FOR_EACH_PTR(fn->arguments, s) {
1272 struct symbol *arg = evaluate_symbol(s);
1273 /* Array/function arguments silently degenerate into pointers */
1274 if (arg) {
1275 struct symbol *ptr;
1276 switch(arg->type) {
1277 case SYM_ARRAY:
1278 case SYM_FN:
1279 ptr = alloc_symbol(s->pos, SYM_PTR);
1280 if (arg->type == SYM_ARRAY)
1281 ptr->ctype = arg->ctype;
1282 else
1283 ptr->ctype.base_type = arg;
1284 ptr->ctype.as |= s->ctype.as;
1285 ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1287 s->ctype.base_type = ptr;
1288 s->ctype.as = 0;
1289 s->ctype.modifiers &= ~MOD_PTRINHERIT;
1290 s->bit_size = 0;
1291 s->examined = 0;
1292 examine_symbol_type(s);
1293 break;
1294 default:
1295 /* nothing */
1296 break;
1299 } END_FOR_EACH_PTR(s);
1302 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1304 /* Take the modifiers of the pointer, and apply them to the member */
1305 mod |= sym->ctype.modifiers;
1306 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1307 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1308 *newsym = *sym;
1309 newsym->ctype.as = as;
1310 newsym->ctype.modifiers = mod;
1311 sym = newsym;
1313 return sym;
1316 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1318 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1319 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1321 node->ctype.base_type = ptr;
1322 ptr->bit_size = bits_in_pointer;
1323 ptr->ctype.alignment = pointer_alignment;
1325 node->bit_size = bits_in_pointer;
1326 node->ctype.alignment = pointer_alignment;
1328 access_symbol(sym);
1329 if (sym->ctype.modifiers & MOD_REGISTER) {
1330 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1331 sym->ctype.modifiers &= ~MOD_REGISTER;
1333 if (sym->type == SYM_NODE) {
1334 ptr->ctype.as |= sym->ctype.as;
1335 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1336 sym = sym->ctype.base_type;
1338 if (degenerate && sym->type == SYM_ARRAY) {
1339 ptr->ctype.as |= sym->ctype.as;
1340 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1341 sym = sym->ctype.base_type;
1343 ptr->ctype.base_type = sym;
1345 return node;
1348 /* Arrays degenerate into pointers on pointer arithmetic */
1349 static struct symbol *degenerate(struct expression *expr)
1351 struct symbol *ctype, *base;
1353 if (!expr)
1354 return NULL;
1355 ctype = expr->ctype;
1356 if (!ctype)
1357 return NULL;
1358 base = examine_symbol_type(ctype);
1359 if (ctype->type == SYM_NODE)
1360 base = ctype->ctype.base_type;
1362 * Arrays degenerate into pointers to the entries, while
1363 * functions degenerate into pointers to themselves.
1364 * If array was part of non-lvalue compound, we create a copy
1365 * of that compound first and then act as if we were dealing with
1366 * the corresponding field in there.
1368 switch (base->type) {
1369 case SYM_ARRAY:
1370 if (expr->type == EXPR_SLICE) {
1371 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1372 struct expression *e0, *e1, *e2, *e3, *e4;
1374 a->ctype.base_type = expr->base->ctype;
1375 a->bit_size = expr->base->ctype->bit_size;
1376 a->array_size = expr->base->ctype->array_size;
1378 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1379 e0->symbol = a;
1380 e0->ctype = &lazy_ptr_ctype;
1382 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1383 e1->unop = e0;
1384 e1->op = '*';
1385 e1->ctype = expr->base->ctype; /* XXX */
1387 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1388 e2->left = e1;
1389 e2->right = expr->base;
1390 e2->op = '=';
1391 e2->ctype = expr->base->ctype;
1393 if (expr->r_bitpos) {
1394 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1395 e3->op = '+';
1396 e3->left = e0;
1397 e3->right = alloc_const_expression(expr->pos,
1398 expr->r_bitpos >> 3);
1399 e3->ctype = &lazy_ptr_ctype;
1400 } else {
1401 e3 = e0;
1404 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1405 e4->left = e2;
1406 e4->right = e3;
1407 e4->ctype = &lazy_ptr_ctype;
1409 expr->unop = e4;
1410 expr->type = EXPR_PREOP;
1411 expr->op = '*';
1413 case SYM_FN:
1414 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1415 expression_error(expr, "strange non-value function or array");
1416 return &bad_ctype;
1418 *expr = *expr->unop;
1419 ctype = create_pointer(expr, ctype, 1);
1420 expr->ctype = ctype;
1421 default:
1422 /* nothing */;
1424 return ctype;
1427 static struct symbol *evaluate_addressof(struct expression *expr)
1429 struct expression *op = expr->unop;
1430 struct symbol *ctype;
1432 if (op->op != '*' || op->type != EXPR_PREOP) {
1433 expression_error(expr, "not addressable");
1434 return NULL;
1436 ctype = op->ctype;
1437 *expr = *op->unop;
1439 if (expr->type == EXPR_SYMBOL) {
1440 struct symbol *sym = expr->symbol;
1441 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1445 * symbol expression evaluation is lazy about the type
1446 * of the sub-expression, so we may have to generate
1447 * the type here if so..
1449 if (expr->ctype == &lazy_ptr_ctype) {
1450 ctype = create_pointer(expr, ctype, 0);
1451 expr->ctype = ctype;
1453 return expr->ctype;
1457 static struct symbol *evaluate_dereference(struct expression *expr)
1459 struct expression *op = expr->unop;
1460 struct symbol *ctype = op->ctype, *node, *target;
1462 /* Simplify: *&(expr) => (expr) */
1463 if (op->type == EXPR_PREOP && op->op == '&') {
1464 *expr = *op->unop;
1465 return expr->ctype;
1468 /* Dereferencing a node drops all the node information. */
1469 if (ctype->type == SYM_NODE)
1470 ctype = ctype->ctype.base_type;
1472 node = alloc_symbol(expr->pos, SYM_NODE);
1473 target = ctype->ctype.base_type;
1475 switch (ctype->type) {
1476 default:
1477 expression_error(expr, "cannot dereference this type");
1478 return NULL;
1479 case SYM_PTR:
1480 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1481 merge_type(node, ctype);
1482 break;
1484 case SYM_ARRAY:
1485 if (!lvalue_expression(op)) {
1486 expression_error(op, "non-lvalue array??");
1487 return NULL;
1490 /* Do the implied "addressof" on the array */
1491 *op = *op->unop;
1494 * When an array is dereferenced, we need to pick
1495 * up the attributes of the original node too..
1497 merge_type(node, op->ctype);
1498 merge_type(node, ctype);
1499 break;
1502 node->bit_size = target->bit_size;
1503 node->array_size = target->array_size;
1505 expr->ctype = node;
1506 return node;
1510 * Unary post-ops: x++ and x--
1512 static struct symbol *evaluate_postop(struct expression *expr)
1514 struct expression *op = expr->unop;
1515 struct symbol *ctype = op->ctype;
1517 if (!lvalue_expression(expr->unop)) {
1518 expression_error(expr, "need lvalue expression for ++/--");
1519 return NULL;
1521 if (is_restricted_type(ctype) && restricted_unop(expr->op, &ctype)) {
1522 expression_error(expr, "bad operation on restricted");
1523 return NULL;
1524 } else if (is_fouled_type(ctype) && restricted_unop(expr->op, &ctype)) {
1525 expression_error(expr, "bad operation on restricted");
1526 return NULL;
1529 evaluate_assign_to(op, ctype);
1531 expr->ctype = ctype;
1532 expr->op_value = 1;
1533 if (is_ptr_type(ctype))
1534 expr->op_value = ptr_object_size(ctype) >> 3;
1536 return ctype;
1539 static struct symbol *evaluate_sign(struct expression *expr)
1541 struct symbol *ctype = expr->unop->ctype;
1542 if (is_int_type(ctype)) {
1543 struct symbol *rtype = rtype = integer_promotion(ctype);
1544 expr->unop = cast_to(expr->unop, rtype);
1545 ctype = rtype;
1546 } else if (is_float_type(ctype) && expr->op != '~') {
1547 /* no conversions needed */
1548 } else if (is_restricted_type(ctype) && !restricted_unop(expr->op, &ctype)) {
1549 /* no conversions needed */
1550 } else if (is_fouled_type(ctype) && !restricted_unop(expr->op, &ctype)) {
1551 /* no conversions needed */
1552 } else {
1553 return bad_expr_type(expr);
1555 if (expr->op == '+')
1556 *expr = *expr->unop;
1557 expr->ctype = ctype;
1558 return ctype;
1561 static struct symbol *evaluate_preop(struct expression *expr)
1563 struct symbol *ctype = expr->unop->ctype;
1565 switch (expr->op) {
1566 case '(':
1567 *expr = *expr->unop;
1568 return ctype;
1570 case '+':
1571 case '-':
1572 case '~':
1573 return evaluate_sign(expr);
1575 case '*':
1576 return evaluate_dereference(expr);
1578 case '&':
1579 return evaluate_addressof(expr);
1581 case SPECIAL_INCREMENT:
1582 case SPECIAL_DECREMENT:
1584 * From a type evaluation standpoint the preops are
1585 * the same as the postops
1587 return evaluate_postop(expr);
1589 case '!':
1590 if (is_safe_type(ctype))
1591 warning(expr->pos, "testing a 'safe expression'");
1592 if (is_float_type(ctype)) {
1593 struct expression *arg = expr->unop;
1594 expr->type = EXPR_BINOP;
1595 expr->op = SPECIAL_EQUAL;
1596 expr->left = arg;
1597 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1598 expr->right->ctype = ctype;
1599 expr->right->fvalue = 0;
1600 } else if (is_fouled_type(ctype)) {
1601 warning(expr->pos, "restricted degrades to integer");
1603 ctype = &bool_ctype;
1604 break;
1606 default:
1607 break;
1609 expr->ctype = ctype;
1610 return &bool_ctype;
1613 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1615 struct ptr_list *head = (struct ptr_list *)_list;
1616 struct ptr_list *list = head;
1618 if (!head)
1619 return NULL;
1620 do {
1621 int i;
1622 for (i = 0; i < list->nr; i++) {
1623 struct symbol *sym = (struct symbol *) list->list[i];
1624 if (sym->ident) {
1625 if (sym->ident != ident)
1626 continue;
1627 *offset = sym->offset;
1628 return sym;
1629 } else {
1630 struct symbol *ctype = sym->ctype.base_type;
1631 struct symbol *sub;
1632 if (!ctype)
1633 continue;
1634 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1635 continue;
1636 sub = find_identifier(ident, ctype->symbol_list, offset);
1637 if (!sub)
1638 continue;
1639 *offset += sym->offset;
1640 return sub;
1643 } while ((list = list->next) != head);
1644 return NULL;
1647 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1649 struct expression *add;
1652 * Create a new add-expression
1654 * NOTE! Even if we just add zero, we need a new node
1655 * for the member pointer, since it has a different
1656 * type than the original pointer. We could make that
1657 * be just a cast, but the fact is, a node is a node,
1658 * so we might as well just do the "add zero" here.
1660 add = alloc_expression(expr->pos, EXPR_BINOP);
1661 add->op = '+';
1662 add->left = expr;
1663 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1664 add->right->ctype = &int_ctype;
1665 add->right->value = offset;
1668 * The ctype of the pointer will be lazily evaluated if
1669 * we ever take the address of this member dereference..
1671 add->ctype = &lazy_ptr_ctype;
1672 return add;
1675 /* structure/union dereference */
1676 static struct symbol *evaluate_member_dereference(struct expression *expr)
1678 int offset;
1679 struct symbol *ctype, *member;
1680 struct expression *deref = expr->deref, *add;
1681 struct ident *ident = expr->member;
1682 unsigned int mod;
1683 int address_space;
1685 if (!evaluate_expression(deref))
1686 return NULL;
1687 if (!ident) {
1688 expression_error(expr, "bad member name");
1689 return NULL;
1692 ctype = deref->ctype;
1693 address_space = ctype->ctype.as;
1694 mod = ctype->ctype.modifiers;
1695 if (ctype->type == SYM_NODE) {
1696 ctype = ctype->ctype.base_type;
1697 address_space |= ctype->ctype.as;
1698 mod |= ctype->ctype.modifiers;
1700 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1701 expression_error(expr, "expected structure or union");
1702 return NULL;
1704 examine_symbol_type(ctype);
1705 offset = 0;
1706 member = find_identifier(ident, ctype->symbol_list, &offset);
1707 if (!member) {
1708 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1709 const char *name = "<unnamed>";
1710 int namelen = 9;
1711 if (ctype->ident) {
1712 name = ctype->ident->name;
1713 namelen = ctype->ident->len;
1715 expression_error(expr, "no member '%s' in %s %.*s",
1716 show_ident(ident), type, namelen, name);
1717 return NULL;
1721 * The member needs to take on the address space and modifiers of
1722 * the "parent" type.
1724 member = convert_to_as_mod(member, address_space, mod);
1725 ctype = get_base_type(member);
1727 if (!lvalue_expression(deref)) {
1728 if (deref->type != EXPR_SLICE) {
1729 expr->base = deref;
1730 expr->r_bitpos = 0;
1731 } else {
1732 expr->base = deref->base;
1733 expr->r_bitpos = deref->r_bitpos;
1735 expr->r_bitpos += offset << 3;
1736 expr->type = EXPR_SLICE;
1737 expr->r_nrbits = member->bit_size;
1738 expr->r_bitpos += member->bit_offset;
1739 expr->ctype = member;
1740 return member;
1743 deref = deref->unop;
1744 expr->deref = deref;
1746 add = evaluate_offset(deref, offset);
1747 expr->type = EXPR_PREOP;
1748 expr->op = '*';
1749 expr->unop = add;
1751 expr->ctype = member;
1752 return member;
1755 static int is_promoted(struct expression *expr)
1757 while (1) {
1758 switch (expr->type) {
1759 case EXPR_BINOP:
1760 case EXPR_SELECT:
1761 case EXPR_CONDITIONAL:
1762 return 1;
1763 case EXPR_COMMA:
1764 expr = expr->right;
1765 continue;
1766 case EXPR_PREOP:
1767 switch (expr->op) {
1768 case '(':
1769 expr = expr->unop;
1770 continue;
1771 case '+':
1772 case '-':
1773 case '~':
1774 return 1;
1775 default:
1776 return 0;
1778 default:
1779 return 0;
1785 static struct symbol *evaluate_cast(struct expression *);
1787 static struct symbol *evaluate_type_information(struct expression *expr)
1789 struct symbol *sym = expr->cast_type;
1790 if (!sym) {
1791 sym = evaluate_expression(expr->cast_expression);
1792 if (!sym)
1793 return NULL;
1795 * Expressions of restricted types will possibly get
1796 * promoted - check that here
1798 if (is_restricted_type(sym)) {
1799 if (sym->bit_size < bits_in_int && is_promoted(expr))
1800 sym = &int_ctype;
1801 } else if (is_fouled_type(sym)) {
1802 sym = &int_ctype;
1805 examine_symbol_type(sym);
1806 if (is_bitfield_type(sym)) {
1807 expression_error(expr, "trying to examine bitfield type");
1808 return NULL;
1810 return sym;
1813 static struct symbol *evaluate_sizeof(struct expression *expr)
1815 struct symbol *type;
1816 int size;
1818 type = evaluate_type_information(expr);
1819 if (!type)
1820 return NULL;
1822 size = type->bit_size;
1823 if ((size < 0) || (size & 7))
1824 expression_error(expr, "cannot size expression");
1825 expr->type = EXPR_VALUE;
1826 expr->value = size >> 3;
1827 expr->ctype = size_t_ctype;
1828 return size_t_ctype;
1831 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
1833 struct symbol *type;
1834 int size;
1836 type = evaluate_type_information(expr);
1837 if (!type)
1838 return NULL;
1840 if (type->type == SYM_NODE)
1841 type = type->ctype.base_type;
1842 if (!type)
1843 return NULL;
1844 switch (type->type) {
1845 case SYM_ARRAY:
1846 break;
1847 case SYM_PTR:
1848 type = get_base_type(type);
1849 if (type)
1850 break;
1851 default:
1852 expression_error(expr, "expected pointer expression");
1853 return NULL;
1855 size = type->bit_size;
1856 if (size & 7)
1857 size = 0;
1858 expr->type = EXPR_VALUE;
1859 expr->value = size >> 3;
1860 expr->ctype = size_t_ctype;
1861 return size_t_ctype;
1864 static struct symbol *evaluate_alignof(struct expression *expr)
1866 struct symbol *type;
1868 type = evaluate_type_information(expr);
1869 if (!type)
1870 return NULL;
1872 expr->type = EXPR_VALUE;
1873 expr->value = type->ctype.alignment;
1874 expr->ctype = size_t_ctype;
1875 return size_t_ctype;
1878 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1880 struct expression *expr;
1881 struct symbol_list *argument_types = fn->arguments;
1882 struct symbol *argtype;
1883 int i = 1;
1885 PREPARE_PTR_LIST(argument_types, argtype);
1886 FOR_EACH_PTR (head, expr) {
1887 struct expression **p = THIS_ADDRESS(expr);
1888 struct symbol *ctype, *target;
1889 ctype = evaluate_expression(expr);
1891 if (!ctype)
1892 return 0;
1894 ctype = degenerate(expr);
1896 target = argtype;
1897 if (!target && ctype->bit_size < bits_in_int)
1898 target = &int_ctype;
1899 if (target) {
1900 static char where[30];
1901 examine_symbol_type(target);
1902 sprintf(where, "argument %d", i);
1903 compatible_assignment_types(expr, target, p, ctype, where, '=');
1906 i++;
1907 NEXT_PTR_LIST(argtype);
1908 } END_FOR_EACH_PTR(expr);
1909 FINISH_PTR_LIST(argtype);
1910 return 1;
1913 static void evaluate_initializer(struct symbol *ctype, struct expression **ep);
1915 static int evaluate_one_array_initializer(struct symbol *ctype, struct expression **ep, int current)
1917 struct expression *entry = *ep;
1918 struct expression **parent, *reuse = NULL;
1919 unsigned long offset;
1920 struct symbol *sym;
1921 unsigned long from, to;
1922 int accept_string = is_byte_type(ctype);
1924 from = current;
1925 to = from+1;
1926 parent = ep;
1927 if (entry->type == EXPR_INDEX) {
1928 from = entry->idx_from;
1929 to = entry->idx_to+1;
1930 parent = &entry->idx_expression;
1931 reuse = entry;
1932 entry = entry->idx_expression;
1935 offset = from * (ctype->bit_size>>3);
1936 if (offset) {
1937 if (!reuse) reuse = alloc_expression(entry->pos, EXPR_POS);
1938 reuse->type = EXPR_POS;
1939 reuse->ctype = ctype;
1940 reuse->init_offset = offset;
1941 reuse->init_nr = to - from;
1942 reuse->init_expr = entry;
1943 parent = &reuse->init_expr;
1944 entry = reuse;
1946 *ep = entry;
1948 if (accept_string && entry->type == EXPR_STRING) {
1949 sym = evaluate_expression(entry);
1950 to = from + get_expression_value(sym->array_size);
1951 } else {
1952 evaluate_initializer(ctype, parent);
1954 return to;
1957 static void evaluate_array_initializer(struct symbol *ctype, struct expression *expr)
1959 struct expression *entry;
1960 int current = 0;
1962 FOR_EACH_PTR(expr->expr_list, entry) {
1963 current = evaluate_one_array_initializer(ctype, THIS_ADDRESS(entry), current);
1964 } END_FOR_EACH_PTR(entry);
1967 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1968 static void evaluate_scalar_initializer(struct symbol *ctype, struct expression *expr)
1970 if (expression_list_size(expr->expr_list) != 1) {
1971 expression_error(expr, "unexpected compound initializer");
1972 return;
1974 evaluate_array_initializer(ctype, expr);
1975 return;
1978 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
1980 struct symbol *sym;
1982 FOR_EACH_PTR(ctype->symbol_list, sym) {
1983 if (sym->ident == ident)
1984 return sym;
1985 } END_FOR_EACH_PTR(sym);
1986 return NULL;
1989 static int evaluate_one_struct_initializer(struct symbol *ctype, struct expression **ep, struct symbol *sym)
1991 struct expression *entry = *ep;
1992 struct expression **parent;
1993 struct expression *reuse = NULL;
1994 unsigned long offset;
1996 if (!sym) {
1997 expression_error(entry, "unknown named initializer");
1998 return -1;
2001 if (entry->type == EXPR_IDENTIFIER) {
2002 reuse = entry;
2003 entry = entry->ident_expression;
2006 parent = ep;
2007 offset = sym->offset;
2008 if (offset) {
2009 if (!reuse)
2010 reuse = alloc_expression(entry->pos, EXPR_POS);
2011 reuse->type = EXPR_POS;
2012 reuse->ctype = sym;
2013 reuse->init_offset = offset;
2014 reuse->init_nr = 1;
2015 reuse->init_expr = entry;
2016 parent = &reuse->init_expr;
2017 entry = reuse;
2019 *ep = entry;
2020 evaluate_initializer(sym, parent);
2021 return 0;
2024 static void evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple)
2026 struct expression *entry;
2027 struct symbol *sym;
2029 PREPARE_PTR_LIST(ctype->symbol_list, sym);
2030 FOR_EACH_PTR(expr->expr_list, entry) {
2031 if (entry->type == EXPR_IDENTIFIER) {
2032 struct ident *ident = entry->expr_ident;
2033 /* We special-case the "already right place" case */
2034 if (!sym || sym->ident != ident) {
2035 RESET_PTR_LIST(sym);
2036 for (;;) {
2037 if (!sym)
2038 break;
2039 if (sym->ident == ident)
2040 break;
2041 NEXT_PTR_LIST(sym);
2045 if (evaluate_one_struct_initializer(ctype, THIS_ADDRESS(entry), sym))
2046 return;
2047 NEXT_PTR_LIST(sym);
2048 } END_FOR_EACH_PTR(entry);
2049 FINISH_PTR_LIST(sym);
2053 * Initializers are kind of like assignments. Except
2054 * they can be a hell of a lot more complex.
2056 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2058 struct expression *expr = *ep;
2061 * Simple non-structure/array initializers are the simple
2062 * case, and look (and parse) largely like assignments.
2064 switch (expr->type) {
2065 default: {
2066 int is_string = expr->type == EXPR_STRING;
2067 struct symbol *rtype = evaluate_expression(expr);
2068 if (rtype) {
2070 * Special case:
2071 * char array[] = "string"
2072 * should _not_ degenerate.
2074 if (!is_string || !is_string_type(ctype))
2075 rtype = degenerate(expr);
2076 compatible_assignment_types(expr, ctype, ep, rtype, "initializer", '=');
2078 return;
2081 case EXPR_INITIALIZER:
2082 expr->ctype = ctype;
2083 if (ctype->type == SYM_NODE)
2084 ctype = ctype->ctype.base_type;
2086 switch (ctype->type) {
2087 case SYM_ARRAY:
2088 case SYM_PTR:
2089 evaluate_array_initializer(get_base_type(ctype), expr);
2090 return;
2091 case SYM_UNION:
2092 evaluate_struct_or_union_initializer(ctype, expr, 0);
2093 return;
2094 case SYM_STRUCT:
2095 evaluate_struct_or_union_initializer(ctype, expr, 1);
2096 return;
2097 default:
2098 evaluate_scalar_initializer(ctype, expr);
2099 return;
2102 case EXPR_IDENTIFIER:
2103 if (ctype->type == SYM_NODE)
2104 ctype = ctype->ctype.base_type;
2105 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2106 expression_error(expr, "expected structure or union for '%s' dereference", show_ident(expr->expr_ident));
2107 show_symbol(ctype);
2108 return;
2110 evaluate_one_struct_initializer(ctype, ep,
2111 find_struct_ident(ctype, expr->expr_ident));
2112 return;
2114 case EXPR_INDEX:
2115 if (ctype->type == SYM_NODE)
2116 ctype = ctype->ctype.base_type;
2117 if (ctype->type != SYM_ARRAY) {
2118 expression_error(expr, "expected array");
2119 return;
2121 evaluate_one_array_initializer(ctype->ctype.base_type, ep, 0);
2122 return;
2124 case EXPR_POS:
2126 * An EXPR_POS expression has already been evaluated, and we don't
2127 * need to do anything more
2129 return;
2133 static int get_as(struct symbol *sym)
2135 int as;
2136 unsigned long mod;
2138 if (!sym)
2139 return 0;
2140 as = sym->ctype.as;
2141 mod = sym->ctype.modifiers;
2142 if (sym->type == SYM_NODE) {
2143 sym = sym->ctype.base_type;
2144 as |= sym->ctype.as;
2145 mod |= sym->ctype.modifiers;
2149 * At least for now, allow casting to a "unsigned long".
2150 * That's how we do things like pointer arithmetic and
2151 * store pointers to registers.
2153 if (sym == &ulong_ctype)
2154 return -1;
2156 if (sym && sym->type == SYM_PTR) {
2157 sym = get_base_type(sym);
2158 as |= sym->ctype.as;
2159 mod |= sym->ctype.modifiers;
2161 if (mod & MOD_FORCE)
2162 return -1;
2163 return as;
2166 static void cast_to_as(struct expression *e, int as)
2168 struct expression *v = e->cast_expression;
2169 struct symbol *type = v->ctype;
2171 if (!Wcast_to_address_space)
2172 return;
2174 if (v->type != EXPR_VALUE || v->value)
2175 goto out;
2177 /* cast from constant 0 to pointer is OK */
2178 if (is_int_type(type))
2179 return;
2181 if (type->type == SYM_NODE)
2182 type = type->ctype.base_type;
2184 if (type->type == SYM_PTR && type->ctype.base_type == &void_ctype)
2185 return;
2187 out:
2188 warning(e->pos, "cast adds address space to expression (<asn:%d>)", as);
2191 static struct symbol *evaluate_cast(struct expression *expr)
2193 struct expression *target = expr->cast_expression;
2194 struct symbol *ctype = examine_symbol_type(expr->cast_type);
2195 struct symbol *t1, *t2;
2196 int class1, class2;
2197 int as1, as2;
2199 if (!target)
2200 return NULL;
2202 expr->ctype = ctype;
2203 expr->cast_type = ctype;
2206 * Special case: a cast can be followed by an
2207 * initializer, in which case we need to pass
2208 * the type value down to that initializer rather
2209 * than trying to evaluate it as an expression
2211 * A more complex case is when the initializer is
2212 * dereferenced as part of a post-fix expression.
2213 * We need to produce an expression that can be dereferenced.
2215 if (target->type == EXPR_INITIALIZER) {
2216 struct symbol *sym = expr->cast_type;
2217 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2219 sym->initializer = expr->cast_expression;
2220 evaluate_symbol(sym);
2222 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2223 addr->symbol = sym;
2225 expr->type = EXPR_PREOP;
2226 expr->op = '*';
2227 expr->unop = addr;
2228 expr->ctype = sym;
2230 return sym;
2233 evaluate_expression(target);
2234 degenerate(target);
2236 class1 = classify_type(ctype, &t1);
2238 * You can always throw a value away by casting to
2239 * "void" - that's an implicit "force". Note that
2240 * the same is _not_ true of "void *".
2242 if (t1 == &void_ctype)
2243 goto out;
2245 if (class1 & TYPE_COMPOUND)
2246 warning(expr->pos, "cast to non-scalar");
2248 t2 = target->ctype;
2249 if (!t2) {
2250 expression_error(expr, "cast from unknown type");
2251 goto out;
2253 class2 = classify_type(t2, &t2);
2255 if (class2 & TYPE_COMPOUND)
2256 warning(expr->pos, "cast from non-scalar");
2258 /* allowed cast unfouls */
2259 if (class2 & TYPE_FOULED)
2260 t2 = t2->ctype.base_type;
2262 if (!(ctype->ctype.modifiers & MOD_FORCE) && t1 != t2) {
2263 if (class1 & TYPE_RESTRICT)
2264 warning(expr->pos, "cast to restricted type");
2265 if (class2 & TYPE_RESTRICT)
2266 warning(expr->pos, "cast from restricted type");
2269 as1 = get_as(ctype);
2270 as2 = get_as(target->ctype);
2271 if (!as1 && as2 > 0)
2272 warning(expr->pos, "cast removes address space of expression");
2273 if (as1 > 0 && as2 > 0 && as1 != as2)
2274 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2275 if (as1 > 0 && !as2)
2276 cast_to_as(expr, as1);
2279 * Casts of constant values are special: they
2280 * can be NULL, and thus need to be simplified
2281 * early.
2283 if (target->type == EXPR_VALUE)
2284 cast_value(expr, ctype, target, target->ctype);
2286 out:
2287 return ctype;
2291 * Evaluate a call expression with a symbol. This
2292 * should expand inline functions, and evaluate
2293 * builtins.
2295 static int evaluate_symbol_call(struct expression *expr)
2297 struct expression *fn = expr->fn;
2298 struct symbol *ctype = fn->ctype;
2300 if (fn->type != EXPR_PREOP)
2301 return 0;
2303 if (ctype->op && ctype->op->evaluate)
2304 return ctype->op->evaluate(expr);
2306 if (ctype->ctype.modifiers & MOD_INLINE) {
2307 int ret;
2308 struct symbol *curr = current_fn;
2309 current_fn = ctype->ctype.base_type;
2311 ret = inline_function(expr, ctype);
2313 /* restore the old function */
2314 current_fn = curr;
2315 return ret;
2318 return 0;
2321 static struct symbol *evaluate_call(struct expression *expr)
2323 int args, fnargs;
2324 struct symbol *ctype, *sym;
2325 struct expression *fn = expr->fn;
2326 struct expression_list *arglist = expr->args;
2328 if (!evaluate_expression(fn))
2329 return NULL;
2330 sym = ctype = fn->ctype;
2331 if (ctype->type == SYM_NODE)
2332 ctype = ctype->ctype.base_type;
2333 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
2334 ctype = get_base_type(ctype);
2336 examine_fn_arguments(ctype);
2337 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
2338 sym->op && sym->op->args) {
2339 if (!sym->op->args(expr))
2340 return NULL;
2341 } else {
2342 if (!evaluate_arguments(sym, ctype, arglist))
2343 return NULL;
2344 if (ctype->type != SYM_FN) {
2345 expression_error(expr, "not a function %s",
2346 show_ident(sym->ident));
2347 return NULL;
2349 args = expression_list_size(expr->args);
2350 fnargs = symbol_list_size(ctype->arguments);
2351 if (args < fnargs)
2352 expression_error(expr,
2353 "not enough arguments for function %s",
2354 show_ident(sym->ident));
2355 if (args > fnargs && !ctype->variadic)
2356 expression_error(expr,
2357 "too many arguments for function %s",
2358 show_ident(sym->ident));
2360 if (sym->type == SYM_NODE) {
2361 if (evaluate_symbol_call(expr))
2362 return expr->ctype;
2364 expr->ctype = ctype->ctype.base_type;
2365 return expr->ctype;
2368 struct symbol *evaluate_expression(struct expression *expr)
2370 if (!expr)
2371 return NULL;
2372 if (expr->ctype)
2373 return expr->ctype;
2375 switch (expr->type) {
2376 case EXPR_VALUE:
2377 case EXPR_FVALUE:
2378 expression_error(expr, "value expression without a type");
2379 return NULL;
2380 case EXPR_STRING:
2381 return evaluate_string(expr);
2382 case EXPR_SYMBOL:
2383 return evaluate_symbol_expression(expr);
2384 case EXPR_BINOP:
2385 if (!evaluate_expression(expr->left))
2386 return NULL;
2387 if (!evaluate_expression(expr->right))
2388 return NULL;
2389 return evaluate_binop(expr);
2390 case EXPR_LOGICAL:
2391 return evaluate_logical(expr);
2392 case EXPR_COMMA:
2393 evaluate_expression(expr->left);
2394 if (!evaluate_expression(expr->right))
2395 return NULL;
2396 return evaluate_comma(expr);
2397 case EXPR_COMPARE:
2398 if (!evaluate_expression(expr->left))
2399 return NULL;
2400 if (!evaluate_expression(expr->right))
2401 return NULL;
2402 return evaluate_compare(expr);
2403 case EXPR_ASSIGNMENT:
2404 if (!evaluate_expression(expr->left))
2405 return NULL;
2406 if (!evaluate_expression(expr->right))
2407 return NULL;
2408 return evaluate_assignment(expr);
2409 case EXPR_PREOP:
2410 if (!evaluate_expression(expr->unop))
2411 return NULL;
2412 return evaluate_preop(expr);
2413 case EXPR_POSTOP:
2414 if (!evaluate_expression(expr->unop))
2415 return NULL;
2416 return evaluate_postop(expr);
2417 case EXPR_CAST:
2418 case EXPR_IMPLIED_CAST:
2419 return evaluate_cast(expr);
2420 case EXPR_SIZEOF:
2421 return evaluate_sizeof(expr);
2422 case EXPR_PTRSIZEOF:
2423 return evaluate_ptrsizeof(expr);
2424 case EXPR_ALIGNOF:
2425 return evaluate_alignof(expr);
2426 case EXPR_DEREF:
2427 return evaluate_member_dereference(expr);
2428 case EXPR_CALL:
2429 return evaluate_call(expr);
2430 case EXPR_SELECT:
2431 case EXPR_CONDITIONAL:
2432 return evaluate_conditional_expression(expr);
2433 case EXPR_STATEMENT:
2434 expr->ctype = evaluate_statement(expr->statement);
2435 return expr->ctype;
2437 case EXPR_LABEL:
2438 expr->ctype = &ptr_ctype;
2439 return &ptr_ctype;
2441 case EXPR_TYPE:
2442 /* Evaluate the type of the symbol .. */
2443 evaluate_symbol(expr->symbol);
2444 /* .. but the type of the _expression_ is a "type" */
2445 expr->ctype = &type_ctype;
2446 return &type_ctype;
2448 /* These can not exist as stand-alone expressions */
2449 case EXPR_INITIALIZER:
2450 case EXPR_IDENTIFIER:
2451 case EXPR_INDEX:
2452 case EXPR_POS:
2453 expression_error(expr, "internal front-end error: initializer in expression");
2454 return NULL;
2455 case EXPR_SLICE:
2456 expression_error(expr, "internal front-end error: SLICE re-evaluated");
2457 return NULL;
2459 return NULL;
2462 static void check_duplicates(struct symbol *sym)
2464 int declared = 0;
2465 struct symbol *next = sym;
2467 while ((next = next->same_symbol) != NULL) {
2468 const char *typediff;
2469 evaluate_symbol(next);
2470 declared++;
2471 typediff = type_difference(sym, next, 0, 0);
2472 if (typediff) {
2473 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2474 show_ident(sym->ident),
2475 stream_name(next->pos.stream), next->pos.line, typediff);
2476 return;
2479 if (!declared) {
2480 unsigned long mod = sym->ctype.modifiers;
2481 if (mod & (MOD_STATIC | MOD_REGISTER))
2482 return;
2483 if (!(mod & MOD_TOPLEVEL))
2484 return;
2485 if (!Wdecl)
2486 return;
2487 if (sym->ident == &main_ident)
2488 return;
2489 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
2493 static struct symbol *evaluate_symbol(struct symbol *sym)
2495 struct symbol *base_type;
2497 if (!sym)
2498 return sym;
2499 if (sym->evaluated)
2500 return sym;
2501 sym->evaluated = 1;
2503 sym = examine_symbol_type(sym);
2504 base_type = get_base_type(sym);
2505 if (!base_type)
2506 return NULL;
2508 /* Evaluate the initializers */
2509 if (sym->initializer)
2510 evaluate_initializer(sym, &sym->initializer);
2512 /* And finally, evaluate the body of the symbol too */
2513 if (base_type->type == SYM_FN) {
2514 struct symbol *curr = current_fn;
2516 current_fn = base_type;
2518 examine_fn_arguments(base_type);
2519 if (!base_type->stmt && base_type->inline_stmt)
2520 uninline(sym);
2521 if (base_type->stmt)
2522 evaluate_statement(base_type->stmt);
2524 current_fn = curr;
2527 return base_type;
2530 void evaluate_symbol_list(struct symbol_list *list)
2532 struct symbol *sym;
2534 FOR_EACH_PTR(list, sym) {
2535 evaluate_symbol(sym);
2536 check_duplicates(sym);
2537 } END_FOR_EACH_PTR(sym);
2540 static struct symbol *evaluate_return_expression(struct statement *stmt)
2542 struct expression *expr = stmt->expression;
2543 struct symbol *ctype, *fntype;
2545 evaluate_expression(expr);
2546 ctype = degenerate(expr);
2547 fntype = current_fn->ctype.base_type;
2548 if (!fntype || fntype == &void_ctype) {
2549 if (expr && ctype != &void_ctype)
2550 expression_error(expr, "return expression in %s function", fntype?"void":"typeless");
2551 return NULL;
2554 if (!expr) {
2555 sparse_error(stmt->pos, "return with no return value");
2556 return NULL;
2558 if (!ctype)
2559 return NULL;
2560 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression", '=');
2561 return NULL;
2564 static void evaluate_if_statement(struct statement *stmt)
2566 if (!stmt->if_conditional)
2567 return;
2569 evaluate_conditional(stmt->if_conditional, 0);
2570 evaluate_statement(stmt->if_true);
2571 evaluate_statement(stmt->if_false);
2574 static void evaluate_iterator(struct statement *stmt)
2576 evaluate_conditional(stmt->iterator_pre_condition, 1);
2577 evaluate_conditional(stmt->iterator_post_condition,1);
2578 evaluate_statement(stmt->iterator_pre_statement);
2579 evaluate_statement(stmt->iterator_statement);
2580 evaluate_statement(stmt->iterator_post_statement);
2583 static void verify_output_constraint(struct expression *expr, const char *constraint)
2585 switch (*constraint) {
2586 case '=': /* Assignment */
2587 case '+': /* Update */
2588 break;
2589 default:
2590 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
2594 static void verify_input_constraint(struct expression *expr, const char *constraint)
2596 switch (*constraint) {
2597 case '=': /* Assignment */
2598 case '+': /* Update */
2599 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
2603 static void evaluate_asm_statement(struct statement *stmt)
2605 struct expression *expr;
2606 int state;
2608 expr = stmt->asm_string;
2609 if (!expr || expr->type != EXPR_STRING) {
2610 sparse_error(stmt->pos, "need constant string for inline asm");
2611 return;
2614 state = 0;
2615 FOR_EACH_PTR(stmt->asm_outputs, expr) {
2616 struct ident *ident;
2618 switch (state) {
2619 case 0: /* Identifier */
2620 state = 1;
2621 ident = (struct ident *)expr;
2622 continue;
2624 case 1: /* Constraint */
2625 state = 2;
2626 if (!expr || expr->type != EXPR_STRING) {
2627 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
2628 *THIS_ADDRESS(expr) = NULL;
2629 continue;
2631 verify_output_constraint(expr, expr->string->data);
2632 continue;
2634 case 2: /* Expression */
2635 state = 0;
2636 if (!evaluate_expression(expr))
2637 return;
2638 if (!lvalue_expression(expr))
2639 warning(expr->pos, "asm output is not an lvalue");
2640 evaluate_assign_to(expr, expr->ctype);
2641 continue;
2643 } END_FOR_EACH_PTR(expr);
2645 state = 0;
2646 FOR_EACH_PTR(stmt->asm_inputs, expr) {
2647 struct ident *ident;
2649 switch (state) {
2650 case 0: /* Identifier */
2651 state = 1;
2652 ident = (struct ident *)expr;
2653 continue;
2655 case 1: /* Constraint */
2656 state = 2;
2657 if (!expr || expr->type != EXPR_STRING) {
2658 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
2659 *THIS_ADDRESS(expr) = NULL;
2660 continue;
2662 verify_input_constraint(expr, expr->string->data);
2663 continue;
2665 case 2: /* Expression */
2666 state = 0;
2667 if (!evaluate_expression(expr))
2668 return;
2669 continue;
2671 } END_FOR_EACH_PTR(expr);
2673 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
2674 if (!expr) {
2675 sparse_error(stmt->pos, "bad asm output");
2676 return;
2678 if (expr->type == EXPR_STRING)
2679 continue;
2680 expression_error(expr, "asm clobber is not a string");
2681 } END_FOR_EACH_PTR(expr);
2684 static void evaluate_case_statement(struct statement *stmt)
2686 evaluate_expression(stmt->case_expression);
2687 evaluate_expression(stmt->case_to);
2688 evaluate_statement(stmt->case_statement);
2691 static void check_case_type(struct expression *switch_expr,
2692 struct expression *case_expr,
2693 struct expression **enumcase)
2695 struct symbol *switch_type, *case_type;
2696 int sclass, cclass;
2698 if (!case_expr)
2699 return;
2701 switch_type = switch_expr->ctype;
2702 case_type = evaluate_expression(case_expr);
2704 if (!switch_type || !case_type)
2705 goto Bad;
2706 if (enumcase) {
2707 if (*enumcase)
2708 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
2709 else if (is_enum_type(case_type))
2710 *enumcase = case_expr;
2713 sclass = classify_type(switch_type, &switch_type);
2714 cclass = classify_type(case_type, &case_type);
2716 /* both should be arithmetic */
2717 if (!(sclass & cclass & TYPE_NUM))
2718 goto Bad;
2720 /* neither should be floating */
2721 if ((sclass | cclass) & TYPE_FLOAT)
2722 goto Bad;
2724 /* if neither is restricted, we are OK */
2725 if (!((sclass | cclass) & TYPE_RESTRICT))
2726 return;
2728 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
2729 cclass, sclass, case_type, switch_type))
2730 warning(case_expr->pos, "restricted degrades to integer");
2732 return;
2734 Bad:
2735 expression_error(case_expr, "incompatible types for 'case' statement");
2738 static void evaluate_switch_statement(struct statement *stmt)
2740 struct symbol *sym;
2741 struct expression *enumcase = NULL;
2742 struct expression **enumcase_holder = &enumcase;
2743 struct expression *sel = stmt->switch_expression;
2745 evaluate_expression(sel);
2746 evaluate_statement(stmt->switch_statement);
2747 if (!sel)
2748 return;
2749 if (sel->ctype && is_enum_type(sel->ctype))
2750 enumcase_holder = NULL; /* Only check cases against switch */
2752 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
2753 struct statement *case_stmt = sym->stmt;
2754 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
2755 check_case_type(sel, case_stmt->case_to, enumcase_holder);
2756 } END_FOR_EACH_PTR(sym);
2759 struct symbol *evaluate_statement(struct statement *stmt)
2761 if (!stmt)
2762 return NULL;
2764 switch (stmt->type) {
2765 case STMT_DECLARATION: {
2766 struct symbol *s;
2767 FOR_EACH_PTR(stmt->declaration, s) {
2768 evaluate_symbol(s);
2769 } END_FOR_EACH_PTR(s);
2770 return NULL;
2773 case STMT_RETURN:
2774 return evaluate_return_expression(stmt);
2776 case STMT_EXPRESSION:
2777 if (!evaluate_expression(stmt->expression))
2778 return NULL;
2779 return degenerate(stmt->expression);
2781 case STMT_COMPOUND: {
2782 struct statement *s;
2783 struct symbol *type = NULL;
2785 /* Evaluate the return symbol in the compound statement */
2786 evaluate_symbol(stmt->ret);
2789 * Then, evaluate each statement, making the type of the
2790 * compound statement be the type of the last statement
2792 type = evaluate_statement(stmt->args);
2793 FOR_EACH_PTR(stmt->stmts, s) {
2794 type = evaluate_statement(s);
2795 } END_FOR_EACH_PTR(s);
2796 if (!type)
2797 type = &void_ctype;
2798 return type;
2800 case STMT_IF:
2801 evaluate_if_statement(stmt);
2802 return NULL;
2803 case STMT_ITERATOR:
2804 evaluate_iterator(stmt);
2805 return NULL;
2806 case STMT_SWITCH:
2807 evaluate_switch_statement(stmt);
2808 return NULL;
2809 case STMT_CASE:
2810 evaluate_case_statement(stmt);
2811 return NULL;
2812 case STMT_LABEL:
2813 return evaluate_statement(stmt->label_statement);
2814 case STMT_GOTO:
2815 evaluate_expression(stmt->goto_expression);
2816 return NULL;
2817 case STMT_NONE:
2818 break;
2819 case STMT_ASM:
2820 evaluate_asm_statement(stmt);
2821 return NULL;
2822 case STMT_CONTEXT:
2823 evaluate_expression(stmt->expression);
2824 return NULL;
2825 case STMT_RANGE:
2826 evaluate_expression(stmt->range_expression);
2827 evaluate_expression(stmt->range_low);
2828 evaluate_expression(stmt->range_high);
2829 return NULL;
2831 return NULL;