fix 'weak' attribute loss
[smatch.git] / evaluate.c
blobca49ed089602c80b4de31db1b4ace3143ff6dc09
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");
258 info(pos, " %s versus", show_typename(typea));
259 info(pos, " %s", show_typename(typeb));
264 * This gets called for implicit casts in assignments and
265 * integer promotion. We often want to try to move the
266 * cast down, because the ops involved may have been
267 * implicitly cast up, and we can get rid of the casts
268 * early.
270 static struct expression * cast_to(struct expression *old, struct symbol *type)
272 struct expression *expr;
274 warn_for_different_enum_types (old->pos, old->ctype, type);
276 if (is_same_type(old, type))
277 return old;
280 * See if we can simplify the op. Move the cast down.
282 switch (old->type) {
283 case EXPR_PREOP:
284 if (old->ctype->bit_size < type->bit_size)
285 break;
286 if (old->op == '~') {
287 old->ctype = type;
288 old->unop = cast_to(old->unop, type);
289 return old;
291 break;
293 case EXPR_IMPLIED_CAST:
294 warn_for_different_enum_types(old->pos, old->ctype, type);
296 if (old->ctype->bit_size >= type->bit_size) {
297 struct expression *orig = old->cast_expression;
298 if (same_cast_type(orig->ctype, type))
299 return orig;
300 if (old->ctype->bit_offset == type->bit_offset) {
301 old->ctype = type;
302 old->cast_type = type;
303 return old;
306 break;
308 default:
309 /* nothing */;
312 expr = alloc_expression(old->pos, EXPR_IMPLIED_CAST);
313 expr->ctype = type;
314 expr->cast_type = type;
315 expr->cast_expression = old;
316 return expr;
319 static int is_type_type(struct symbol *type)
321 return (type->ctype.modifiers & MOD_TYPE) != 0;
324 int is_ptr_type(struct symbol *type)
326 if (type->type == SYM_NODE)
327 type = type->ctype.base_type;
328 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
331 static inline int is_float_type(struct symbol *type)
333 if (type->type == SYM_NODE)
334 type = type->ctype.base_type;
335 return type->ctype.base_type == &fp_type;
338 static inline int is_byte_type(struct symbol *type)
340 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
343 enum {
344 TYPE_NUM = 1,
345 TYPE_BITFIELD = 2,
346 TYPE_RESTRICT = 4,
347 TYPE_FLOAT = 8,
348 TYPE_PTR = 16,
349 TYPE_COMPOUND = 32,
350 TYPE_FOULED = 64,
353 static inline int classify_type(struct symbol *type, struct symbol **base)
355 static int type_class[SYM_BAD + 1] = {
356 [SYM_PTR] = TYPE_PTR,
357 [SYM_FN] = TYPE_PTR,
358 [SYM_ARRAY] = TYPE_PTR | TYPE_COMPOUND,
359 [SYM_STRUCT] = TYPE_COMPOUND,
360 [SYM_UNION] = TYPE_COMPOUND,
361 [SYM_BITFIELD] = TYPE_NUM | TYPE_BITFIELD,
362 [SYM_RESTRICT] = TYPE_NUM | TYPE_RESTRICT,
363 [SYM_FOULED] = TYPE_NUM | TYPE_RESTRICT | TYPE_FOULED,
365 if (type->type == SYM_NODE)
366 type = type->ctype.base_type;
367 if (type->type == SYM_ENUM)
368 type = type->ctype.base_type;
369 *base = type;
370 if (type->type == SYM_BASETYPE) {
371 if (type->ctype.base_type == &int_type)
372 return TYPE_NUM;
373 if (type->ctype.base_type == &fp_type)
374 return TYPE_NUM | TYPE_FLOAT;
376 return type_class[type->type];
379 static inline int is_string_type(struct symbol *type)
381 if (type->type == SYM_NODE)
382 type = type->ctype.base_type;
383 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
386 static struct symbol *bad_expr_type(struct expression *expr)
388 sparse_error(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
389 switch (expr->type) {
390 case EXPR_BINOP:
391 case EXPR_COMPARE:
392 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
393 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
394 break;
395 case EXPR_PREOP:
396 case EXPR_POSTOP:
397 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
398 break;
399 default:
400 break;
403 return expr->ctype = &bad_ctype;
406 static int restricted_value(struct expression *v, struct symbol *type)
408 if (v->type != EXPR_VALUE)
409 return 1;
410 if (v->value != 0)
411 return 1;
412 return 0;
415 static int restricted_binop(int op, struct symbol *type)
417 switch (op) {
418 case '&':
419 case '=':
420 case SPECIAL_AND_ASSIGN:
421 case SPECIAL_OR_ASSIGN:
422 case SPECIAL_XOR_ASSIGN:
423 return 1; /* unfoul */
424 case '|':
425 case '^':
426 case '?':
427 return 2; /* keep fouled */
428 case SPECIAL_EQUAL:
429 case SPECIAL_NOTEQUAL:
430 return 3; /* warn if fouled */
431 default:
432 return 0; /* warn */
436 static int restricted_unop(int op, struct symbol **type)
438 if (op == '~') {
439 if ((*type)->bit_size < bits_in_int)
440 *type = befoul(*type);
441 return 0;
442 } if (op == '+')
443 return 0;
444 return 1;
447 static struct symbol *restricted_binop_type(int op,
448 struct expression *left,
449 struct expression *right,
450 int lclass, int rclass,
451 struct symbol *ltype,
452 struct symbol *rtype)
454 struct symbol *ctype = NULL;
455 if (lclass & TYPE_RESTRICT) {
456 if (rclass & TYPE_RESTRICT) {
457 if (ltype == rtype) {
458 ctype = ltype;
459 } else if (lclass & TYPE_FOULED) {
460 if (ltype->ctype.base_type == rtype)
461 ctype = ltype;
462 } else if (rclass & TYPE_FOULED) {
463 if (rtype->ctype.base_type == ltype)
464 ctype = rtype;
466 } else {
467 if (!restricted_value(right, ltype))
468 ctype = ltype;
470 } else if (!restricted_value(left, rtype))
471 ctype = rtype;
473 if (ctype) {
474 switch (restricted_binop(op, ctype)) {
475 case 1:
476 if ((lclass ^ rclass) & TYPE_FOULED)
477 ctype = ctype->ctype.base_type;
478 break;
479 case 3:
480 if (!(lclass & rclass & TYPE_FOULED))
481 break;
482 case 0:
483 ctype = NULL;
484 default:
485 break;
489 return ctype;
492 static struct symbol *usual_conversions(int op,
493 struct expression **left,
494 struct expression **right,
495 int lclass, int rclass,
496 struct symbol *ltype,
497 struct symbol *rtype)
499 struct symbol *ctype;
501 warn_for_different_enum_types((*right)->pos, (*left)->ctype, (*right)->ctype);
503 if ((lclass | rclass) & TYPE_RESTRICT)
504 goto Restr;
506 Normal:
507 if (!(lclass & TYPE_FLOAT)) {
508 if (!(rclass & TYPE_FLOAT))
509 ctype = bigger_int_type(ltype, rtype);
510 else
511 ctype = rtype;
512 } else if (rclass & TYPE_FLOAT) {
513 unsigned long lmod = ltype->ctype.modifiers;
514 unsigned long rmod = rtype->ctype.modifiers;
515 if (rmod & ~lmod & (MOD_LONG | MOD_LONGLONG))
516 ctype = rtype;
517 else
518 ctype = ltype;
519 } else
520 ctype = ltype;
522 Convert:
523 *left = cast_to(*left, ctype);
524 *right = cast_to(*right, ctype);
525 return ctype;
527 Restr:
528 ctype = restricted_binop_type(op, *left, *right,
529 lclass, rclass, ltype, rtype);
530 if (ctype)
531 goto Convert;
533 if (lclass & TYPE_RESTRICT) {
534 warning((*left)->pos, "restricted degrades to integer");
535 ltype = ltype->ctype.base_type;
536 if (is_restricted_type(ltype)) /* was fouled */
537 ltype = ltype->ctype.base_type;
539 if (rclass & TYPE_RESTRICT) {
540 warning((*right)->pos, "restricted degrades to integer");
541 rtype = rtype->ctype.base_type;
542 if (is_restricted_type(rtype)) /* was fouled */
543 rtype = rtype->ctype.base_type;
545 goto Normal;
548 static struct symbol *evaluate_arith(struct expression *expr, int float_ok)
550 struct symbol *ltype, *rtype;
551 int lclass = classify_type(expr->left->ctype, &ltype);
552 int rclass = classify_type(expr->right->ctype, &rtype);
553 struct symbol *ctype;
555 if (!(lclass & rclass & TYPE_NUM))
556 goto Bad;
558 if (!float_ok && (lclass | rclass) & TYPE_FLOAT)
559 goto Bad;
561 ctype = usual_conversions(expr->op, &expr->left, &expr->right,
562 lclass, rclass, ltype, rtype);
563 expr->ctype = ctype;
564 return ctype;
566 Bad:
567 return bad_expr_type(expr);
570 static inline int lvalue_expression(struct expression *expr)
572 return expr->type == EXPR_PREOP && expr->op == '*';
575 static int ptr_object_size(struct symbol *ptr_type)
577 if (ptr_type->type == SYM_NODE)
578 ptr_type = ptr_type->ctype.base_type;
579 if (ptr_type->type == SYM_PTR)
580 ptr_type = get_base_type(ptr_type);
581 return ptr_type->bit_size;
584 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *ctype, struct expression **ip)
586 struct expression *i = *ip;
587 struct symbol *ptr_type = ctype;
588 int bit_size;
590 if (ptr_type->type == SYM_NODE)
591 ptr_type = ptr_type->ctype.base_type;
593 if (!is_int_type(i->ctype))
594 return bad_expr_type(expr);
596 examine_symbol_type(ctype);
598 if (!ctype->ctype.base_type) {
599 sparse_error(expr->pos, "missing type information");
600 return NULL;
603 /* Get the size of whatever the pointer points to */
604 bit_size = ptr_object_size(ctype);
606 if (bit_size > bits_in_char) {
607 int multiply = bit_size >> 3;
608 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
610 if (i->type == EXPR_VALUE) {
611 val->value = i->value * multiply;
612 val->ctype = size_t_ctype;
613 *ip = val;
614 } else {
615 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
617 val->ctype = size_t_ctype;
618 val->value = bit_size >> 3;
620 mul->op = '*';
621 mul->ctype = size_t_ctype;
622 mul->left = i;
623 mul->right = val;
625 *ip = mul;
629 expr->ctype = ctype;
630 return ctype;
633 static struct symbol *evaluate_add(struct expression *expr)
635 struct expression *left = expr->left, *right = expr->right;
636 struct symbol *ltype = left->ctype, *rtype = right->ctype;
638 if (is_ptr_type(ltype))
639 return evaluate_ptr_add(expr, degenerate(left), &expr->right);
641 if (is_ptr_type(rtype))
642 return evaluate_ptr_add(expr, degenerate(right), &expr->left);
644 return evaluate_arith(expr, 1);
647 const char * type_difference(struct symbol *target, struct symbol *source,
648 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
650 for (;;) {
651 unsigned long mod1, mod2, diff;
652 unsigned long as1, as2;
653 int type1, type2;
654 struct symbol *base1, *base2;
656 if (target == source)
657 break;
658 if (!target || !source)
659 return "different types";
661 * Peel of per-node information.
662 * FIXME! Check alignment and context too here!
664 mod1 = target->ctype.modifiers;
665 as1 = target->ctype.as;
666 mod2 = source->ctype.modifiers;
667 as2 = source->ctype.as;
668 if (target->type == SYM_NODE) {
669 target = target->ctype.base_type;
670 if (!target)
671 return "bad types";
672 if (target->type == SYM_PTR) {
673 mod1 = 0;
674 as1 = 0;
676 mod1 |= target->ctype.modifiers;
677 as1 |= target->ctype.as;
679 if (source->type == SYM_NODE) {
680 source = source->ctype.base_type;
681 if (!source)
682 return "bad types";
683 if (source->type == SYM_PTR) {
684 mod2 = 0;
685 as2 = 0;
687 mod2 |= source->ctype.modifiers;
688 as2 |= source->ctype.as;
690 if (target->type == SYM_ENUM) {
691 target = target->ctype.base_type;
692 if (!target)
693 return "bad types";
695 if (source->type == SYM_ENUM) {
696 source = source->ctype.base_type;
697 if (!source)
698 return "bad types";
701 if (target == source)
702 break;
703 if (!target || !source)
704 return "different types";
706 type1 = target->type;
707 base1 = target->ctype.base_type;
709 type2 = source->type;
710 base2 = source->ctype.base_type;
713 * Pointers to functions compare as the function itself
715 if (type1 == SYM_PTR && base1) {
716 base1 = examine_symbol_type(base1);
717 switch (base1->type) {
718 case SYM_FN:
719 type1 = SYM_FN;
720 target = base1;
721 base1 = base1->ctype.base_type;
722 default:
723 /* nothing */;
726 if (type2 == SYM_PTR && base2) {
727 base2 = examine_symbol_type(base2);
728 switch (base2->type) {
729 case SYM_FN:
730 type2 = SYM_FN;
731 source = base2;
732 base2 = base2->ctype.base_type;
733 default:
734 /* nothing */;
738 /* Arrays degenerate to pointers for type comparisons */
739 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
740 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
742 if (type1 != type2 || type1 == SYM_RESTRICT)
743 return "different base types";
745 /* Must be same address space to be comparable */
746 if (Waddress_space && as1 != as2)
747 return "different address spaces";
749 /* Ignore differences in storage types or addressability */
750 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
751 diff &= (mod1 & ~target_mod_ignore) | (mod2 & ~source_mod_ignore);
752 if (diff) {
753 if (diff & MOD_SIZE)
754 return "different type sizes";
755 if (diff & ~MOD_SIGNEDNESS)
756 return "different modifiers";
758 /* Differs in signedness only.. */
759 if (Wtypesign) {
761 * Warn if both are explicitly signed ("unsigned" is obvously
762 * always explicit, and since we know one of them has to be
763 * unsigned, we check if the signed one was explicit).
765 if ((mod1 | mod2) & MOD_EXPLICITLY_SIGNED)
766 return "different explicit signedness";
769 * "char" matches both "unsigned char" and "signed char",
770 * so if the explicit test didn't trigger, then we should
771 * not warn about a char.
773 if (!(mod1 & MOD_CHAR))
774 return "different signedness";
778 if (type1 == SYM_FN) {
779 int i;
780 struct symbol *arg1, *arg2;
781 if (base1->variadic != base2->variadic)
782 return "incompatible variadic arguments";
783 PREPARE_PTR_LIST(target->arguments, arg1);
784 PREPARE_PTR_LIST(source->arguments, arg2);
785 i = 1;
786 for (;;) {
787 const char *diff;
788 diff = type_difference(arg1, arg2, 0, 0);
789 if (diff) {
790 static char argdiff[80];
791 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
792 return argdiff;
794 if (!arg1)
795 break;
796 NEXT_PTR_LIST(arg1);
797 NEXT_PTR_LIST(arg2);
798 i++;
800 FINISH_PTR_LIST(arg2);
801 FINISH_PTR_LIST(arg1);
804 target = base1;
805 source = base2;
807 return NULL;
810 static int is_null_ptr(struct expression *expr)
812 if (expr->type != EXPR_VALUE || expr->value)
813 return 0;
814 if (!is_ptr_type(expr->ctype))
815 warning(expr->pos, "Using plain integer as NULL pointer");
816 return 1;
819 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
821 /* NULL expression? Just return the type of the "other side" */
822 if (is_null_ptr(r))
823 return l->ctype;
824 if (is_null_ptr(l))
825 return r->ctype;
826 return NULL;
830 * Ignore differences in "volatile" and "const"ness when
831 * subtracting pointers
833 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
835 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression **rp)
837 const char *typediff;
838 struct symbol *ctype;
839 struct symbol *ltype, *rtype;
840 struct expression *r = *rp;
842 ltype = degenerate(l);
843 rtype = degenerate(r);
846 * If it is an integer subtract: the ptr add case will do the
847 * right thing.
849 if (!is_ptr_type(rtype))
850 return evaluate_ptr_add(expr, degenerate(l), rp);
852 ctype = ltype;
853 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
854 if (typediff) {
855 ctype = common_ptr_type(l, r);
856 if (!ctype) {
857 sparse_error(expr->pos, "subtraction of different types can't work (%s)", typediff);
858 return NULL;
861 examine_symbol_type(ctype);
863 /* Figure out the base type we point to */
864 if (ctype->type == SYM_NODE)
865 ctype = ctype->ctype.base_type;
866 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
867 sparse_error(expr->pos, "subtraction of functions? Share your drugs");
868 return NULL;
870 ctype = get_base_type(ctype);
872 expr->ctype = ssize_t_ctype;
873 if (ctype->bit_size > bits_in_char) {
874 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
875 struct expression *div = expr;
876 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
877 unsigned long value = ctype->bit_size >> 3;
879 val->ctype = size_t_ctype;
880 val->value = value;
882 if (value & (value-1)) {
883 if (Wptr_subtraction_blows)
884 warning(expr->pos, "potentially expensive pointer subtraction");
887 sub->op = '-';
888 sub->ctype = ssize_t_ctype;
889 sub->left = l;
890 sub->right = r;
892 div->op = '/';
893 div->left = sub;
894 div->right = val;
897 return ssize_t_ctype;
900 static struct symbol *evaluate_sub(struct expression *expr)
902 struct expression *left = expr->left;
903 struct symbol *ltype = left->ctype;
905 if (is_ptr_type(ltype))
906 return evaluate_ptr_sub(expr, left, &expr->right);
908 return evaluate_arith(expr, 1);
911 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
913 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
915 struct symbol *ctype;
917 if (!expr)
918 return NULL;
920 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
921 warning(expr->pos, "assignment expression in conditional");
923 ctype = evaluate_expression(expr);
924 if (ctype) {
925 if (is_safe_type(ctype))
926 warning(expr->pos, "testing a 'safe expression'");
929 return ctype;
932 static struct symbol *evaluate_logical(struct expression *expr)
934 if (!evaluate_conditional(expr->left, 0))
935 return NULL;
936 if (!evaluate_conditional(expr->right, 0))
937 return NULL;
939 expr->ctype = &bool_ctype;
940 return &bool_ctype;
943 static struct symbol *evaluate_shift(struct expression *expr)
945 struct expression *left = expr->left, *right = expr->right;
946 struct symbol *ltype = left->ctype, *rtype = right->ctype;
948 if (ltype->type == SYM_NODE)
949 ltype = ltype->ctype.base_type;
950 if (rtype->type == SYM_NODE)
951 rtype = rtype->ctype.base_type;
952 if (is_int_type(ltype) && is_int_type(rtype)) {
953 struct symbol *ctype = integer_promotion(ltype);
954 expr->left = cast_to(expr->left, ctype);
955 expr->ctype = ctype;
956 ctype = integer_promotion(rtype);
957 expr->right = cast_to(expr->right, ctype);
958 return expr->ctype;
960 return bad_expr_type(expr);
963 static struct symbol *evaluate_binop(struct expression *expr)
965 switch (expr->op) {
966 // addition can take ptr+int, fp and int
967 case '+':
968 return evaluate_add(expr);
970 // subtraction can take ptr-ptr, fp and int
971 case '-':
972 return evaluate_sub(expr);
974 // Arithmetic operations can take fp and int
975 case '*': case '/':
976 return evaluate_arith(expr, 1);
978 // shifts do integer promotions, but that's it.
979 case SPECIAL_LEFTSHIFT: case SPECIAL_RIGHTSHIFT:
980 return evaluate_shift(expr);
982 // The rest are integer operations
983 // '%', '&', '^', '|'
984 default:
985 return evaluate_arith(expr, 0);
989 static struct symbol *evaluate_comma(struct expression *expr)
991 expr->ctype = expr->right->ctype;
992 return expr->ctype;
995 static int modify_for_unsigned(int op)
997 if (op == '<')
998 op = SPECIAL_UNSIGNED_LT;
999 else if (op == '>')
1000 op = SPECIAL_UNSIGNED_GT;
1001 else if (op == SPECIAL_LTE)
1002 op = SPECIAL_UNSIGNED_LTE;
1003 else if (op == SPECIAL_GTE)
1004 op = SPECIAL_UNSIGNED_GTE;
1005 return op;
1008 static struct symbol *evaluate_compare(struct expression *expr)
1010 struct expression *left = expr->left, *right = expr->right;
1011 struct symbol *ltype = left->ctype, *rtype = right->ctype;
1012 struct symbol *ctype;
1014 /* Type types? */
1015 if (is_type_type(ltype) && is_type_type(rtype))
1016 goto OK;
1018 if (is_safe_type(ltype) || is_safe_type(rtype))
1019 warning(expr->pos, "testing a 'safe expression'");
1021 /* Pointer types? */
1022 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
1023 // FIXME! Check the types for compatibility
1024 expr->op = modify_for_unsigned(expr->op);
1025 goto OK;
1028 ctype = evaluate_arith(expr, 1);
1029 if (ctype) {
1030 if (ctype->ctype.modifiers & MOD_UNSIGNED)
1031 expr->op = modify_for_unsigned(expr->op);
1034 expr->ctype = &bool_ctype;
1035 return &bool_ctype;
1039 * FIXME!! This should do casts, array degeneration etc..
1041 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
1043 struct symbol *ltype = left->ctype, *rtype = right->ctype;
1045 if (ltype->type == SYM_NODE)
1046 ltype = ltype->ctype.base_type;
1048 if (rtype->type == SYM_NODE)
1049 rtype = rtype->ctype.base_type;
1051 if (ltype->type == SYM_PTR) {
1052 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
1053 return ltype;
1056 if (rtype->type == SYM_PTR) {
1057 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
1058 return rtype;
1060 return NULL;
1064 * NOTE! The degenerate case of "x ? : y", where we don't
1065 * have a true case, this will possibly promote "x" to the
1066 * same type as "y", and thus _change_ the conditional
1067 * test in the expression. But since promotion is "safe"
1068 * for testing, that's ok.
1070 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1072 struct expression **true;
1073 struct symbol *ctype, *ltype, *rtype;
1074 int lclass, rclass;
1075 const char * typediff;
1077 if (!evaluate_conditional(expr->conditional, 0))
1078 return NULL;
1079 if (!evaluate_expression(expr->cond_false))
1080 return NULL;
1082 ctype = degenerate(expr->conditional);
1083 rtype = degenerate(expr->cond_false);
1085 true = &expr->conditional;
1086 ltype = ctype;
1087 if (expr->cond_true) {
1088 if (!evaluate_expression(expr->cond_true))
1089 return NULL;
1090 ltype = degenerate(expr->cond_true);
1091 true = &expr->cond_true;
1094 lclass = classify_type(ltype, &ltype);
1095 rclass = classify_type(rtype, &rtype);
1096 if (lclass & rclass & TYPE_NUM) {
1097 ctype = usual_conversions('?', true, &expr->cond_false,
1098 lclass, rclass, ltype, rtype);
1099 goto out;
1101 ctype = compatible_ptr_type(*true, expr->cond_false);
1102 if (ctype)
1103 goto out;
1104 ctype = ltype;
1105 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
1106 if (!typediff)
1107 goto out;
1108 sparse_error(expr->pos, "incompatible types in conditional expression (%s)", typediff);
1109 return NULL;
1111 out:
1112 expr->ctype = ctype;
1113 return ctype;
1116 /* FP assignments can not do modulo or bit operations */
1117 static int compatible_float_op(int op)
1119 return op == '=' ||
1120 op == SPECIAL_ADD_ASSIGN ||
1121 op == SPECIAL_SUB_ASSIGN ||
1122 op == SPECIAL_MUL_ASSIGN ||
1123 op == SPECIAL_DIV_ASSIGN;
1126 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1127 struct expression **rp, struct symbol *source, const char *where, int op)
1129 const char *typediff;
1130 struct symbol *t, *s;
1131 int target_as;
1132 int tclass = classify_type(target, &t);
1133 int sclass = classify_type(source, &s);
1135 if (tclass & sclass & TYPE_NUM) {
1136 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1137 sparse_error(expr->pos, "invalid assignment");
1138 return 0;
1140 if (tclass & TYPE_RESTRICT) {
1141 if (!restricted_binop(op, target)) {
1142 sparse_error(expr->pos, "bad restricted assignment");
1143 return 0;
1145 /* allowed assignments unfoul */
1146 if (sclass & TYPE_FOULED && s->ctype.base_type == t)
1147 goto Cast;
1148 if (!restricted_value(*rp, target))
1149 return 1;
1150 } else if (!(sclass & TYPE_RESTRICT))
1151 goto Cast;
1152 } else if (tclass & TYPE_PTR) {
1153 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1154 evaluate_ptr_add(expr, target, rp);
1155 return 1;
1157 if (op != '=') {
1158 sparse_error(expr->pos, "invalid pointer assignment");
1159 return 0;
1161 } else if (op != '=') {
1162 sparse_error(expr->pos, "invalid assignment");
1163 return 0;
1166 /* It's ok if the target is more volatile or const than the source */
1167 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
1168 if (!typediff)
1169 return 1;
1171 /* Pointer destination? */
1172 if (tclass & TYPE_PTR) {
1173 struct expression *right = *rp;
1174 int source_as;
1176 // NULL pointer is always ok
1177 if (is_null_ptr(right))
1178 goto Cast;
1180 /* "void *" matches anything as long as the address space is ok */
1181 target_as = t->ctype.as | target->ctype.as;
1182 source_as = s->ctype.as | source->ctype.as;
1183 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
1184 s = get_base_type(s);
1185 t = get_base_type(t);
1186 if (s == &void_ctype || t == &void_ctype)
1187 goto Cast;
1191 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1192 info(expr->pos, " expected %s", show_typename(target));
1193 info(expr->pos, " got %s", show_typename(source));
1194 *rp = cast_to(*rp, target);
1195 return 0;
1196 Cast:
1197 *rp = cast_to(*rp, target);
1198 return 1;
1201 static void mark_assigned(struct expression *expr)
1203 struct symbol *sym;
1205 if (!expr)
1206 return;
1207 switch (expr->type) {
1208 case EXPR_SYMBOL:
1209 sym = expr->symbol;
1210 if (!sym)
1211 return;
1212 if (sym->type != SYM_NODE)
1213 return;
1214 sym->ctype.modifiers |= MOD_ASSIGNED;
1215 return;
1217 case EXPR_BINOP:
1218 mark_assigned(expr->left);
1219 mark_assigned(expr->right);
1220 return;
1221 case EXPR_CAST:
1222 mark_assigned(expr->cast_expression);
1223 return;
1224 case EXPR_SLICE:
1225 mark_assigned(expr->base);
1226 return;
1227 default:
1228 /* Hmm? */
1229 return;
1233 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1235 if (type->ctype.modifiers & MOD_CONST)
1236 sparse_error(left->pos, "assignment to const expression");
1238 /* We know left is an lvalue, so it's a "preop-*" */
1239 mark_assigned(left->unop);
1242 static struct symbol *evaluate_assignment(struct expression *expr)
1244 struct expression *left = expr->left, *right = expr->right;
1245 struct expression *where = expr;
1246 struct symbol *ltype, *rtype;
1248 if (!lvalue_expression(left)) {
1249 sparse_error(expr->pos, "not an lvalue");
1250 return NULL;
1253 ltype = left->ctype;
1255 rtype = degenerate(right);
1257 if (!compatible_assignment_types(where, ltype, &where->right, rtype, "assignment", expr->op))
1258 return NULL;
1260 evaluate_assign_to(left, ltype);
1262 expr->ctype = ltype;
1263 return ltype;
1266 static void examine_fn_arguments(struct symbol *fn)
1268 struct symbol *s;
1270 FOR_EACH_PTR(fn->arguments, s) {
1271 struct symbol *arg = evaluate_symbol(s);
1272 /* Array/function arguments silently degenerate into pointers */
1273 if (arg) {
1274 struct symbol *ptr;
1275 switch(arg->type) {
1276 case SYM_ARRAY:
1277 case SYM_FN:
1278 ptr = alloc_symbol(s->pos, SYM_PTR);
1279 if (arg->type == SYM_ARRAY)
1280 ptr->ctype = arg->ctype;
1281 else
1282 ptr->ctype.base_type = arg;
1283 ptr->ctype.as |= s->ctype.as;
1284 ptr->ctype.modifiers |= s->ctype.modifiers;
1286 s->ctype.base_type = ptr;
1287 s->ctype.as = 0;
1288 s->ctype.modifiers = 0;
1289 s->bit_size = 0;
1290 s->examined = 0;
1291 examine_symbol_type(s);
1292 break;
1293 default:
1294 /* nothing */
1295 break;
1298 } END_FOR_EACH_PTR(s);
1301 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1303 /* Take the modifiers of the pointer, and apply them to the member */
1304 mod |= sym->ctype.modifiers;
1305 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1306 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1307 *newsym = *sym;
1308 newsym->ctype.as = as;
1309 newsym->ctype.modifiers = mod;
1310 sym = newsym;
1312 return sym;
1315 #define MOD_PTRINHERIT (MOD_VOLATILE | MOD_CONST | MOD_NODEREF | MOD_STORAGE)
1317 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1319 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1320 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1322 node->ctype.base_type = ptr;
1323 ptr->bit_size = bits_in_pointer;
1324 ptr->ctype.alignment = pointer_alignment;
1326 node->bit_size = bits_in_pointer;
1327 node->ctype.alignment = pointer_alignment;
1329 access_symbol(sym);
1330 if (sym->ctype.modifiers & MOD_REGISTER) {
1331 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1332 sym->ctype.modifiers &= ~MOD_REGISTER;
1334 if (sym->type == SYM_NODE) {
1335 ptr->ctype.as |= sym->ctype.as;
1336 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1337 sym = sym->ctype.base_type;
1339 if (degenerate && sym->type == SYM_ARRAY) {
1340 ptr->ctype.as |= sym->ctype.as;
1341 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1342 sym = sym->ctype.base_type;
1344 ptr->ctype.base_type = sym;
1346 return node;
1349 /* Arrays degenerate into pointers on pointer arithmetic */
1350 static struct symbol *degenerate(struct expression *expr)
1352 struct symbol *ctype, *base;
1354 if (!expr)
1355 return NULL;
1356 ctype = expr->ctype;
1357 if (!ctype)
1358 return NULL;
1359 base = examine_symbol_type(ctype);
1360 if (ctype->type == SYM_NODE)
1361 base = ctype->ctype.base_type;
1363 * Arrays degenerate into pointers to the entries, while
1364 * functions degenerate into pointers to themselves.
1365 * If array was part of non-lvalue compound, we create a copy
1366 * of that compound first and then act as if we were dealing with
1367 * the corresponding field in there.
1369 switch (base->type) {
1370 case SYM_ARRAY:
1371 if (expr->type == EXPR_SLICE) {
1372 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1373 struct expression *e0, *e1, *e2, *e3, *e4;
1375 a->ctype.base_type = expr->base->ctype;
1376 a->bit_size = expr->base->ctype->bit_size;
1377 a->array_size = expr->base->ctype->array_size;
1379 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1380 e0->symbol = a;
1381 e0->ctype = &lazy_ptr_ctype;
1383 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1384 e1->unop = e0;
1385 e1->op = '*';
1386 e1->ctype = expr->base->ctype; /* XXX */
1388 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1389 e2->left = e1;
1390 e2->right = expr->base;
1391 e2->op = '=';
1392 e2->ctype = expr->base->ctype;
1394 if (expr->r_bitpos) {
1395 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1396 e3->op = '+';
1397 e3->left = e0;
1398 e3->right = alloc_const_expression(expr->pos,
1399 expr->r_bitpos >> 3);
1400 e3->ctype = &lazy_ptr_ctype;
1401 } else {
1402 e3 = e0;
1405 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1406 e4->left = e2;
1407 e4->right = e3;
1408 e4->ctype = &lazy_ptr_ctype;
1410 expr->unop = e4;
1411 expr->type = EXPR_PREOP;
1412 expr->op = '*';
1414 case SYM_FN:
1415 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1416 sparse_error(expr->pos, "strange non-value function or array");
1417 return &bad_ctype;
1419 *expr = *expr->unop;
1420 ctype = create_pointer(expr, ctype, 1);
1421 expr->ctype = ctype;
1422 default:
1423 /* nothing */;
1425 return ctype;
1428 static struct symbol *evaluate_addressof(struct expression *expr)
1430 struct expression *op = expr->unop;
1431 struct symbol *ctype;
1433 if (op->op != '*' || op->type != EXPR_PREOP) {
1434 sparse_error(expr->pos, "not addressable");
1435 return NULL;
1437 ctype = op->ctype;
1438 *expr = *op->unop;
1440 if (expr->type == EXPR_SYMBOL) {
1441 struct symbol *sym = expr->symbol;
1442 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1446 * symbol expression evaluation is lazy about the type
1447 * of the sub-expression, so we may have to generate
1448 * the type here if so..
1450 if (expr->ctype == &lazy_ptr_ctype) {
1451 ctype = create_pointer(expr, ctype, 0);
1452 expr->ctype = ctype;
1454 return expr->ctype;
1458 static struct symbol *evaluate_dereference(struct expression *expr)
1460 struct expression *op = expr->unop;
1461 struct symbol *ctype = op->ctype, *node, *target;
1463 /* Simplify: *&(expr) => (expr) */
1464 if (op->type == EXPR_PREOP && op->op == '&') {
1465 *expr = *op->unop;
1466 return expr->ctype;
1469 /* Dereferencing a node drops all the node information. */
1470 if (ctype->type == SYM_NODE)
1471 ctype = ctype->ctype.base_type;
1473 node = alloc_symbol(expr->pos, SYM_NODE);
1474 target = ctype->ctype.base_type;
1476 switch (ctype->type) {
1477 default:
1478 sparse_error(expr->pos, "cannot dereference this type");
1479 return NULL;
1480 case SYM_PTR:
1481 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1482 merge_type(node, ctype);
1483 break;
1485 case SYM_ARRAY:
1486 if (!lvalue_expression(op)) {
1487 sparse_error(op->pos, "non-lvalue array??");
1488 return NULL;
1491 /* Do the implied "addressof" on the array */
1492 *op = *op->unop;
1495 * When an array is dereferenced, we need to pick
1496 * up the attributes of the original node too..
1498 merge_type(node, op->ctype);
1499 merge_type(node, ctype);
1500 break;
1503 node->bit_size = target->bit_size;
1504 node->array_size = target->array_size;
1506 expr->ctype = node;
1507 return node;
1511 * Unary post-ops: x++ and x--
1513 static struct symbol *evaluate_postop(struct expression *expr)
1515 struct expression *op = expr->unop;
1516 struct symbol *ctype = op->ctype;
1518 if (!lvalue_expression(expr->unop)) {
1519 sparse_error(expr->pos, "need lvalue expression for ++/--");
1520 return NULL;
1522 if (is_restricted_type(ctype) && restricted_unop(expr->op, &ctype)) {
1523 sparse_error(expr->pos, "bad operation on restricted");
1524 return NULL;
1525 } else if (is_fouled_type(ctype) && restricted_unop(expr->op, &ctype)) {
1526 sparse_error(expr->pos, "bad operation on restricted");
1527 return NULL;
1530 evaluate_assign_to(op, ctype);
1532 expr->ctype = ctype;
1533 expr->op_value = 1;
1534 if (is_ptr_type(ctype))
1535 expr->op_value = ptr_object_size(ctype) >> 3;
1537 return ctype;
1540 static struct symbol *evaluate_sign(struct expression *expr)
1542 struct symbol *ctype = expr->unop->ctype;
1543 if (is_int_type(ctype)) {
1544 struct symbol *rtype = rtype = integer_promotion(ctype);
1545 expr->unop = cast_to(expr->unop, rtype);
1546 ctype = rtype;
1547 } else if (is_float_type(ctype) && expr->op != '~') {
1548 /* no conversions needed */
1549 } else if (is_restricted_type(ctype) && !restricted_unop(expr->op, &ctype)) {
1550 /* no conversions needed */
1551 } else if (is_fouled_type(ctype) && !restricted_unop(expr->op, &ctype)) {
1552 /* no conversions needed */
1553 } else {
1554 return bad_expr_type(expr);
1556 if (expr->op == '+')
1557 *expr = *expr->unop;
1558 expr->ctype = ctype;
1559 return ctype;
1562 static struct symbol *evaluate_preop(struct expression *expr)
1564 struct symbol *ctype = expr->unop->ctype;
1566 switch (expr->op) {
1567 case '(':
1568 *expr = *expr->unop;
1569 return ctype;
1571 case '+':
1572 case '-':
1573 case '~':
1574 return evaluate_sign(expr);
1576 case '*':
1577 return evaluate_dereference(expr);
1579 case '&':
1580 return evaluate_addressof(expr);
1582 case SPECIAL_INCREMENT:
1583 case SPECIAL_DECREMENT:
1585 * From a type evaluation standpoint the pre-ops are
1586 * the same as the postops
1588 return evaluate_postop(expr);
1590 case '!':
1591 if (is_safe_type(ctype))
1592 warning(expr->pos, "testing a 'safe expression'");
1593 if (is_float_type(ctype)) {
1594 struct expression *arg = expr->unop;
1595 expr->type = EXPR_BINOP;
1596 expr->op = SPECIAL_EQUAL;
1597 expr->left = arg;
1598 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1599 expr->right->ctype = ctype;
1600 expr->right->fvalue = 0;
1601 } else if (is_fouled_type(ctype)) {
1602 warning(expr->pos, "restricted degrades to integer");
1604 ctype = &bool_ctype;
1605 break;
1607 default:
1608 break;
1610 expr->ctype = ctype;
1611 return &bool_ctype;
1614 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1616 struct ptr_list *head = (struct ptr_list *)_list;
1617 struct ptr_list *list = head;
1619 if (!head)
1620 return NULL;
1621 do {
1622 int i;
1623 for (i = 0; i < list->nr; i++) {
1624 struct symbol *sym = (struct symbol *) list->list[i];
1625 if (sym->ident) {
1626 if (sym->ident != ident)
1627 continue;
1628 *offset = sym->offset;
1629 return sym;
1630 } else {
1631 struct symbol *ctype = sym->ctype.base_type;
1632 struct symbol *sub;
1633 if (!ctype)
1634 continue;
1635 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1636 continue;
1637 sub = find_identifier(ident, ctype->symbol_list, offset);
1638 if (!sub)
1639 continue;
1640 *offset += sym->offset;
1641 return sub;
1644 } while ((list = list->next) != head);
1645 return NULL;
1648 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1650 struct expression *add;
1653 * Create a new add-expression
1655 * NOTE! Even if we just add zero, we need a new node
1656 * for the member pointer, since it has a different
1657 * type than the original pointer. We could make that
1658 * be just a cast, but the fact is, a node is a node,
1659 * so we might as well just do the "add zero" here.
1661 add = alloc_expression(expr->pos, EXPR_BINOP);
1662 add->op = '+';
1663 add->left = expr;
1664 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1665 add->right->ctype = &int_ctype;
1666 add->right->value = offset;
1669 * The ctype of the pointer will be lazily evaluated if
1670 * we ever take the address of this member dereference..
1672 add->ctype = &lazy_ptr_ctype;
1673 return add;
1676 /* structure/union dereference */
1677 static struct symbol *evaluate_member_dereference(struct expression *expr)
1679 int offset;
1680 struct symbol *ctype, *member;
1681 struct expression *deref = expr->deref, *add;
1682 struct ident *ident = expr->member;
1683 unsigned int mod;
1684 int address_space;
1686 if (!evaluate_expression(deref))
1687 return NULL;
1688 if (!ident) {
1689 sparse_error(expr->pos, "bad member name");
1690 return NULL;
1693 ctype = deref->ctype;
1694 address_space = ctype->ctype.as;
1695 mod = ctype->ctype.modifiers;
1696 if (ctype->type == SYM_NODE) {
1697 ctype = ctype->ctype.base_type;
1698 address_space |= ctype->ctype.as;
1699 mod |= ctype->ctype.modifiers;
1701 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1702 sparse_error(expr->pos, "expected structure or union");
1703 return NULL;
1705 examine_symbol_type(ctype);
1706 offset = 0;
1707 member = find_identifier(ident, ctype->symbol_list, &offset);
1708 if (!member) {
1709 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1710 const char *name = "<unnamed>";
1711 int namelen = 9;
1712 if (ctype->ident) {
1713 name = ctype->ident->name;
1714 namelen = ctype->ident->len;
1716 sparse_error(expr->pos, "no member '%s' in %s %.*s",
1717 show_ident(ident), type, namelen, name);
1718 return NULL;
1722 * The member needs to take on the address space and modifiers of
1723 * the "parent" type.
1725 member = convert_to_as_mod(member, address_space, mod);
1726 ctype = get_base_type(member);
1728 if (!lvalue_expression(deref)) {
1729 if (deref->type != EXPR_SLICE) {
1730 expr->base = deref;
1731 expr->r_bitpos = 0;
1732 } else {
1733 expr->base = deref->base;
1734 expr->r_bitpos = deref->r_bitpos;
1736 expr->r_bitpos += offset << 3;
1737 expr->type = EXPR_SLICE;
1738 expr->r_nrbits = member->bit_size;
1739 expr->r_bitpos += member->bit_offset;
1740 expr->ctype = member;
1741 return member;
1744 deref = deref->unop;
1745 expr->deref = deref;
1747 add = evaluate_offset(deref, offset);
1748 expr->type = EXPR_PREOP;
1749 expr->op = '*';
1750 expr->unop = add;
1752 expr->ctype = member;
1753 return member;
1756 static int is_promoted(struct expression *expr)
1758 while (1) {
1759 switch (expr->type) {
1760 case EXPR_BINOP:
1761 case EXPR_SELECT:
1762 case EXPR_CONDITIONAL:
1763 return 1;
1764 case EXPR_COMMA:
1765 expr = expr->right;
1766 continue;
1767 case EXPR_PREOP:
1768 switch (expr->op) {
1769 case '(':
1770 expr = expr->unop;
1771 continue;
1772 case '+':
1773 case '-':
1774 case '~':
1775 return 1;
1776 default:
1777 return 0;
1779 default:
1780 return 0;
1786 static struct symbol *evaluate_cast(struct expression *);
1788 static struct symbol *evaluate_type_information(struct expression *expr)
1790 struct symbol *sym = expr->cast_type;
1791 if (!sym) {
1792 sym = evaluate_expression(expr->cast_expression);
1793 if (!sym)
1794 return NULL;
1796 * Expressions of restricted types will possibly get
1797 * promoted - check that here
1799 if (is_restricted_type(sym)) {
1800 if (sym->bit_size < bits_in_int && is_promoted(expr))
1801 sym = &int_ctype;
1802 } else if (is_fouled_type(sym)) {
1803 sym = &int_ctype;
1806 examine_symbol_type(sym);
1807 if (is_bitfield_type(sym)) {
1808 sparse_error(expr->pos, "trying to examine bitfield type");
1809 return NULL;
1811 return sym;
1814 static struct symbol *evaluate_sizeof(struct expression *expr)
1816 struct symbol *type;
1817 int size;
1819 type = evaluate_type_information(expr);
1820 if (!type)
1821 return NULL;
1823 size = type->bit_size;
1824 if ((size < 0) || (size & 7))
1825 sparse_error(expr->pos, "cannot size expression");
1826 expr->type = EXPR_VALUE;
1827 expr->value = size >> 3;
1828 expr->ctype = size_t_ctype;
1829 return size_t_ctype;
1832 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
1834 struct symbol *type;
1835 int size;
1837 type = evaluate_type_information(expr);
1838 if (!type)
1839 return NULL;
1841 if (type->type == SYM_NODE)
1842 type = type->ctype.base_type;
1843 if (!type)
1844 return NULL;
1845 switch (type->type) {
1846 case SYM_ARRAY:
1847 break;
1848 case SYM_PTR:
1849 type = get_base_type(type);
1850 if (type)
1851 break;
1852 default:
1853 sparse_error(expr->pos, "expected pointer expression");
1854 return NULL;
1856 size = type->bit_size;
1857 if (size & 7)
1858 size = 0;
1859 expr->type = EXPR_VALUE;
1860 expr->value = size >> 3;
1861 expr->ctype = size_t_ctype;
1862 return size_t_ctype;
1865 static struct symbol *evaluate_alignof(struct expression *expr)
1867 struct symbol *type;
1869 type = evaluate_type_information(expr);
1870 if (!type)
1871 return NULL;
1873 expr->type = EXPR_VALUE;
1874 expr->value = type->ctype.alignment;
1875 expr->ctype = size_t_ctype;
1876 return size_t_ctype;
1879 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1881 struct expression *expr;
1882 struct symbol_list *argument_types = fn->arguments;
1883 struct symbol *argtype;
1884 int i = 1;
1886 PREPARE_PTR_LIST(argument_types, argtype);
1887 FOR_EACH_PTR (head, expr) {
1888 struct expression **p = THIS_ADDRESS(expr);
1889 struct symbol *ctype, *target;
1890 ctype = evaluate_expression(expr);
1892 if (!ctype)
1893 return 0;
1895 ctype = degenerate(expr);
1897 target = argtype;
1898 if (!target && ctype->bit_size < bits_in_int)
1899 target = &int_ctype;
1900 if (target) {
1901 static char where[30];
1902 examine_symbol_type(target);
1903 sprintf(where, "argument %d", i);
1904 compatible_assignment_types(expr, target, p, ctype, where, '=');
1907 i++;
1908 NEXT_PTR_LIST(argtype);
1909 } END_FOR_EACH_PTR(expr);
1910 FINISH_PTR_LIST(argtype);
1911 return 1;
1914 static void evaluate_initializer(struct symbol *ctype, struct expression **ep);
1916 static int evaluate_one_array_initializer(struct symbol *ctype, struct expression **ep, int current)
1918 struct expression *entry = *ep;
1919 struct expression **parent, *reuse = NULL;
1920 unsigned long offset;
1921 struct symbol *sym;
1922 unsigned long from, to;
1923 int accept_string = is_byte_type(ctype);
1925 from = current;
1926 to = from+1;
1927 parent = ep;
1928 if (entry->type == EXPR_INDEX) {
1929 from = entry->idx_from;
1930 to = entry->idx_to+1;
1931 parent = &entry->idx_expression;
1932 reuse = entry;
1933 entry = entry->idx_expression;
1936 offset = from * (ctype->bit_size>>3);
1937 if (offset) {
1938 if (!reuse) reuse = alloc_expression(entry->pos, EXPR_POS);
1939 reuse->type = EXPR_POS;
1940 reuse->ctype = ctype;
1941 reuse->init_offset = offset;
1942 reuse->init_nr = to - from;
1943 reuse->init_expr = entry;
1944 parent = &reuse->init_expr;
1945 entry = reuse;
1947 *ep = entry;
1949 if (accept_string && entry->type == EXPR_STRING) {
1950 sym = evaluate_expression(entry);
1951 to = from + get_expression_value(sym->array_size);
1952 } else {
1953 evaluate_initializer(ctype, parent);
1955 return to;
1958 static void evaluate_array_initializer(struct symbol *ctype, struct expression *expr)
1960 struct expression *entry;
1961 int current = 0;
1963 FOR_EACH_PTR(expr->expr_list, entry) {
1964 current = evaluate_one_array_initializer(ctype, THIS_ADDRESS(entry), current);
1965 } END_FOR_EACH_PTR(entry);
1968 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1969 static void evaluate_scalar_initializer(struct symbol *ctype, struct expression *expr)
1971 if (expression_list_size(expr->expr_list) != 1) {
1972 sparse_error(expr->pos, "unexpected compound initializer");
1973 return;
1975 evaluate_array_initializer(ctype, expr);
1976 return;
1979 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
1981 struct symbol *sym;
1983 FOR_EACH_PTR(ctype->symbol_list, sym) {
1984 if (sym->ident == ident)
1985 return sym;
1986 } END_FOR_EACH_PTR(sym);
1987 return NULL;
1990 static int evaluate_one_struct_initializer(struct symbol *ctype, struct expression **ep, struct symbol *sym)
1992 struct expression *entry = *ep;
1993 struct expression **parent;
1994 struct expression *reuse = NULL;
1995 unsigned long offset;
1997 if (!sym) {
1998 sparse_error(entry->pos, "unknown named initializer");
1999 return -1;
2002 if (entry->type == EXPR_IDENTIFIER) {
2003 reuse = entry;
2004 entry = entry->ident_expression;
2007 parent = ep;
2008 offset = sym->offset;
2009 if (offset) {
2010 if (!reuse)
2011 reuse = alloc_expression(entry->pos, EXPR_POS);
2012 reuse->type = EXPR_POS;
2013 reuse->ctype = sym;
2014 reuse->init_offset = offset;
2015 reuse->init_nr = 1;
2016 reuse->init_expr = entry;
2017 parent = &reuse->init_expr;
2018 entry = reuse;
2020 *ep = entry;
2021 evaluate_initializer(sym, parent);
2022 return 0;
2025 static void evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple)
2027 struct expression *entry;
2028 struct symbol *sym;
2030 PREPARE_PTR_LIST(ctype->symbol_list, sym);
2031 FOR_EACH_PTR(expr->expr_list, entry) {
2032 if (entry->type == EXPR_IDENTIFIER) {
2033 struct ident *ident = entry->expr_ident;
2034 /* We special-case the "already right place" case */
2035 if (!sym || sym->ident != ident) {
2036 RESET_PTR_LIST(sym);
2037 for (;;) {
2038 if (!sym)
2039 break;
2040 if (sym->ident == ident)
2041 break;
2042 NEXT_PTR_LIST(sym);
2046 if (evaluate_one_struct_initializer(ctype, THIS_ADDRESS(entry), sym))
2047 return;
2048 NEXT_PTR_LIST(sym);
2049 } END_FOR_EACH_PTR(entry);
2050 FINISH_PTR_LIST(sym);
2054 * Initializers are kind of like assignments. Except
2055 * they can be a hell of a lot more complex.
2057 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2059 struct expression *expr = *ep;
2062 * Simple non-structure/array initializers are the simple
2063 * case, and look (and parse) largely like assignments.
2065 switch (expr->type) {
2066 default: {
2067 int is_string = expr->type == EXPR_STRING;
2068 struct symbol *rtype = evaluate_expression(expr);
2069 if (rtype) {
2071 * Special case:
2072 * char array[] = "string"
2073 * should _not_ degenerate.
2075 if (!is_string || !is_string_type(ctype))
2076 rtype = degenerate(expr);
2077 compatible_assignment_types(expr, ctype, ep, rtype, "initializer", '=');
2079 return;
2082 case EXPR_INITIALIZER:
2083 expr->ctype = ctype;
2084 if (ctype->type == SYM_NODE)
2085 ctype = ctype->ctype.base_type;
2087 switch (ctype->type) {
2088 case SYM_ARRAY:
2089 case SYM_PTR:
2090 evaluate_array_initializer(get_base_type(ctype), expr);
2091 return;
2092 case SYM_UNION:
2093 evaluate_struct_or_union_initializer(ctype, expr, 0);
2094 return;
2095 case SYM_STRUCT:
2096 evaluate_struct_or_union_initializer(ctype, expr, 1);
2097 return;
2098 default:
2099 evaluate_scalar_initializer(ctype, expr);
2100 return;
2103 case EXPR_IDENTIFIER:
2104 if (ctype->type == SYM_NODE)
2105 ctype = ctype->ctype.base_type;
2106 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2107 sparse_error(expr->pos, "expected structure or union for '%s' dereference", show_ident(expr->expr_ident));
2108 show_symbol(ctype);
2109 return;
2111 evaluate_one_struct_initializer(ctype, ep,
2112 find_struct_ident(ctype, expr->expr_ident));
2113 return;
2115 case EXPR_INDEX:
2116 if (ctype->type == SYM_NODE)
2117 ctype = ctype->ctype.base_type;
2118 if (ctype->type != SYM_ARRAY) {
2119 sparse_error(expr->pos, "expected array");
2120 return;
2122 evaluate_one_array_initializer(ctype->ctype.base_type, ep, 0);
2123 return;
2125 case EXPR_POS:
2127 * An EXPR_POS expression has already been evaluated, and we don't
2128 * need to do anything more
2130 return;
2134 static int get_as(struct symbol *sym)
2136 int as;
2137 unsigned long mod;
2139 if (!sym)
2140 return 0;
2141 as = sym->ctype.as;
2142 mod = sym->ctype.modifiers;
2143 if (sym->type == SYM_NODE) {
2144 sym = sym->ctype.base_type;
2145 as |= sym->ctype.as;
2146 mod |= sym->ctype.modifiers;
2150 * At least for now, allow casting to a "unsigned long".
2151 * That's how we do things like pointer arithmetic and
2152 * store pointers to registers.
2154 if (sym == &ulong_ctype)
2155 return -1;
2157 if (sym && sym->type == SYM_PTR) {
2158 sym = get_base_type(sym);
2159 as |= sym->ctype.as;
2160 mod |= sym->ctype.modifiers;
2162 if (mod & MOD_FORCE)
2163 return -1;
2164 return as;
2167 static void cast_to_as(struct expression *e, int as)
2169 struct expression *v = e->cast_expression;
2170 struct symbol *type = v->ctype;
2172 if (!Wcast_to_address_space)
2173 return;
2175 if (v->type != EXPR_VALUE || v->value)
2176 goto out;
2178 /* cast from constant 0 to pointer is OK */
2179 if (is_int_type(type))
2180 return;
2182 if (type->type == SYM_NODE)
2183 type = type->ctype.base_type;
2185 if (type->type == SYM_PTR && type->ctype.base_type == &void_ctype)
2186 return;
2188 out:
2189 warning(e->pos, "cast adds address space to expression (<asn:%d>)", as);
2192 static struct symbol *evaluate_cast(struct expression *expr)
2194 struct expression *target = expr->cast_expression;
2195 struct symbol *ctype = examine_symbol_type(expr->cast_type);
2196 struct symbol *t1, *t2;
2197 int class1, class2;
2198 int as1, as2;
2200 if (!target)
2201 return NULL;
2203 expr->ctype = ctype;
2204 expr->cast_type = ctype;
2207 * Special case: a cast can be followed by an
2208 * initializer, in which case we need to pass
2209 * the type value down to that initializer rather
2210 * than trying to evaluate it as an expression
2212 * A more complex case is when the initializer is
2213 * dereferenced as part of a post-fix expression.
2214 * We need to produce an expression that can be dereferenced.
2216 if (target->type == EXPR_INITIALIZER) {
2217 struct symbol *sym = expr->cast_type;
2218 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2220 sym->initializer = expr->cast_expression;
2221 evaluate_symbol(sym);
2223 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2224 addr->symbol = sym;
2226 expr->type = EXPR_PREOP;
2227 expr->op = '*';
2228 expr->unop = addr;
2229 expr->ctype = sym;
2231 return sym;
2234 evaluate_expression(target);
2235 degenerate(target);
2237 class1 = classify_type(ctype, &t1);
2239 * You can always throw a value away by casting to
2240 * "void" - that's an implicit "force". Note that
2241 * the same is _not_ true of "void *".
2243 if (t1 == &void_ctype)
2244 goto out;
2246 if (class1 & TYPE_COMPOUND)
2247 warning(expr->pos, "cast to non-scalar");
2249 t2 = target->ctype;
2250 if (!t2) {
2251 sparse_error(expr->pos, "cast from unknown type");
2252 goto out;
2254 class2 = classify_type(t2, &t2);
2256 if (class2 & TYPE_COMPOUND)
2257 warning(expr->pos, "cast from non-scalar");
2259 /* allowed cast unfouls */
2260 if (class2 & TYPE_FOULED)
2261 t2 = t2->ctype.base_type;
2263 if (!(ctype->ctype.modifiers & MOD_FORCE) && t1 != t2) {
2264 if (class1 & TYPE_RESTRICT)
2265 warning(expr->pos, "cast to restricted type");
2266 if (class2 & TYPE_RESTRICT)
2267 warning(expr->pos, "cast from restricted type");
2270 as1 = get_as(ctype);
2271 as2 = get_as(target->ctype);
2272 if (!as1 && as2 > 0)
2273 warning(expr->pos, "cast removes address space of expression");
2274 if (as1 > 0 && as2 > 0 && as1 != as2)
2275 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2276 if (as1 > 0 && !as2)
2277 cast_to_as(expr, as1);
2280 * Casts of constant values are special: they
2281 * can be NULL, and thus need to be simplified
2282 * early.
2284 if (target->type == EXPR_VALUE)
2285 cast_value(expr, ctype, target, target->ctype);
2287 out:
2288 return ctype;
2292 * Evaluate a call expression with a symbol. This
2293 * should expand inline functions, and evaluate
2294 * builtins.
2296 static int evaluate_symbol_call(struct expression *expr)
2298 struct expression *fn = expr->fn;
2299 struct symbol *ctype = fn->ctype;
2301 if (fn->type != EXPR_PREOP)
2302 return 0;
2304 if (ctype->op && ctype->op->evaluate)
2305 return ctype->op->evaluate(expr);
2307 if (ctype->ctype.modifiers & MOD_INLINE) {
2308 int ret;
2309 struct symbol *curr = current_fn;
2310 current_fn = ctype->ctype.base_type;
2311 examine_fn_arguments(current_fn);
2313 ret = inline_function(expr, ctype);
2315 /* restore the old function */
2316 current_fn = curr;
2317 return ret;
2320 return 0;
2323 static struct symbol *evaluate_call(struct expression *expr)
2325 int args, fnargs;
2326 struct symbol *ctype, *sym;
2327 struct expression *fn = expr->fn;
2328 struct expression_list *arglist = expr->args;
2330 if (!evaluate_expression(fn))
2331 return NULL;
2332 sym = ctype = fn->ctype;
2333 if (ctype->type == SYM_NODE)
2334 ctype = ctype->ctype.base_type;
2335 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
2336 ctype = get_base_type(ctype);
2338 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
2339 sym->op && sym->op->args) {
2340 if (!sym->op->args(expr))
2341 return NULL;
2342 } else {
2343 if (!evaluate_arguments(sym, ctype, arglist))
2344 return NULL;
2345 if (ctype->type != SYM_FN) {
2346 sparse_error(expr->pos, "not a function %s",
2347 show_ident(sym->ident));
2348 return NULL;
2350 args = expression_list_size(expr->args);
2351 fnargs = symbol_list_size(ctype->arguments);
2352 if (args < fnargs)
2353 sparse_error(expr->pos,
2354 "not enough arguments for function %s",
2355 show_ident(sym->ident));
2356 if (args > fnargs && !ctype->variadic)
2357 sparse_error(expr->pos,
2358 "too many arguments for function %s",
2359 show_ident(sym->ident));
2361 if (sym->type == SYM_NODE) {
2362 if (evaluate_symbol_call(expr))
2363 return expr->ctype;
2365 expr->ctype = ctype->ctype.base_type;
2366 return expr->ctype;
2369 struct symbol *evaluate_expression(struct expression *expr)
2371 if (!expr)
2372 return NULL;
2373 if (expr->ctype)
2374 return expr->ctype;
2376 switch (expr->type) {
2377 case EXPR_VALUE:
2378 case EXPR_FVALUE:
2379 sparse_error(expr->pos, "value expression without a type");
2380 return NULL;
2381 case EXPR_STRING:
2382 return evaluate_string(expr);
2383 case EXPR_SYMBOL:
2384 return evaluate_symbol_expression(expr);
2385 case EXPR_BINOP:
2386 if (!evaluate_expression(expr->left))
2387 return NULL;
2388 if (!evaluate_expression(expr->right))
2389 return NULL;
2390 return evaluate_binop(expr);
2391 case EXPR_LOGICAL:
2392 return evaluate_logical(expr);
2393 case EXPR_COMMA:
2394 evaluate_expression(expr->left);
2395 if (!evaluate_expression(expr->right))
2396 return NULL;
2397 return evaluate_comma(expr);
2398 case EXPR_COMPARE:
2399 if (!evaluate_expression(expr->left))
2400 return NULL;
2401 if (!evaluate_expression(expr->right))
2402 return NULL;
2403 return evaluate_compare(expr);
2404 case EXPR_ASSIGNMENT:
2405 if (!evaluate_expression(expr->left))
2406 return NULL;
2407 if (!evaluate_expression(expr->right))
2408 return NULL;
2409 return evaluate_assignment(expr);
2410 case EXPR_PREOP:
2411 if (!evaluate_expression(expr->unop))
2412 return NULL;
2413 return evaluate_preop(expr);
2414 case EXPR_POSTOP:
2415 if (!evaluate_expression(expr->unop))
2416 return NULL;
2417 return evaluate_postop(expr);
2418 case EXPR_CAST:
2419 case EXPR_IMPLIED_CAST:
2420 return evaluate_cast(expr);
2421 case EXPR_SIZEOF:
2422 return evaluate_sizeof(expr);
2423 case EXPR_PTRSIZEOF:
2424 return evaluate_ptrsizeof(expr);
2425 case EXPR_ALIGNOF:
2426 return evaluate_alignof(expr);
2427 case EXPR_DEREF:
2428 return evaluate_member_dereference(expr);
2429 case EXPR_CALL:
2430 return evaluate_call(expr);
2431 case EXPR_SELECT:
2432 case EXPR_CONDITIONAL:
2433 return evaluate_conditional_expression(expr);
2434 case EXPR_STATEMENT:
2435 expr->ctype = evaluate_statement(expr->statement);
2436 return expr->ctype;
2438 case EXPR_LABEL:
2439 expr->ctype = &ptr_ctype;
2440 return &ptr_ctype;
2442 case EXPR_TYPE:
2443 /* Evaluate the type of the symbol .. */
2444 evaluate_symbol(expr->symbol);
2445 /* .. but the type of the _expression_ is a "type" */
2446 expr->ctype = &type_ctype;
2447 return &type_ctype;
2449 /* These can not exist as stand-alone expressions */
2450 case EXPR_INITIALIZER:
2451 case EXPR_IDENTIFIER:
2452 case EXPR_INDEX:
2453 case EXPR_POS:
2454 sparse_error(expr->pos, "internal front-end error: initializer in expression");
2455 return NULL;
2456 case EXPR_SLICE:
2457 sparse_error(expr->pos, "internal front-end error: SLICE re-evaluated");
2458 return NULL;
2460 return NULL;
2463 static void check_duplicates(struct symbol *sym)
2465 int declared = 0;
2466 struct symbol *next = sym;
2468 while ((next = next->same_symbol) != NULL) {
2469 const char *typediff;
2470 evaluate_symbol(next);
2471 declared++;
2472 typediff = type_difference(sym, next, 0, 0);
2473 if (typediff) {
2474 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2475 show_ident(sym->ident),
2476 stream_name(next->pos.stream), next->pos.line, typediff);
2477 return;
2480 if (!declared) {
2481 unsigned long mod = sym->ctype.modifiers;
2482 if (mod & (MOD_STATIC | MOD_REGISTER))
2483 return;
2484 if (!(mod & MOD_TOPLEVEL))
2485 return;
2486 if (!Wdecl)
2487 return;
2488 if (sym->ident == &main_ident)
2489 return;
2490 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
2494 static struct symbol *evaluate_symbol(struct symbol *sym)
2496 struct symbol *base_type;
2498 if (!sym)
2499 return sym;
2500 if (sym->evaluated)
2501 return sym;
2502 sym->evaluated = 1;
2504 sym = examine_symbol_type(sym);
2505 base_type = get_base_type(sym);
2506 if (!base_type)
2507 return NULL;
2509 /* Evaluate the initializers */
2510 if (sym->initializer)
2511 evaluate_initializer(sym, &sym->initializer);
2513 /* And finally, evaluate the body of the symbol too */
2514 if (base_type->type == SYM_FN) {
2515 struct symbol *curr = current_fn;
2517 current_fn = base_type;
2519 examine_fn_arguments(base_type);
2520 if (!base_type->stmt && base_type->inline_stmt)
2521 uninline(sym);
2522 if (base_type->stmt)
2523 evaluate_statement(base_type->stmt);
2525 current_fn = curr;
2528 return base_type;
2531 void evaluate_symbol_list(struct symbol_list *list)
2533 struct symbol *sym;
2535 FOR_EACH_PTR(list, sym) {
2536 evaluate_symbol(sym);
2537 check_duplicates(sym);
2538 } END_FOR_EACH_PTR(sym);
2541 static struct symbol *evaluate_return_expression(struct statement *stmt)
2543 struct expression *expr = stmt->expression;
2544 struct symbol *ctype, *fntype;
2546 evaluate_expression(expr);
2547 ctype = degenerate(expr);
2548 fntype = current_fn->ctype.base_type;
2549 if (!fntype || fntype == &void_ctype) {
2550 if (expr && ctype != &void_ctype)
2551 sparse_error(expr->pos, "return expression in %s function", fntype?"void":"typeless");
2552 return NULL;
2555 if (!expr) {
2556 sparse_error(stmt->pos, "return with no return value");
2557 return NULL;
2559 if (!ctype)
2560 return NULL;
2561 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression", '=');
2562 return NULL;
2565 static void evaluate_if_statement(struct statement *stmt)
2567 if (!stmt->if_conditional)
2568 return;
2570 evaluate_conditional(stmt->if_conditional, 0);
2571 evaluate_statement(stmt->if_true);
2572 evaluate_statement(stmt->if_false);
2575 static void evaluate_iterator(struct statement *stmt)
2577 evaluate_conditional(stmt->iterator_pre_condition, 1);
2578 evaluate_conditional(stmt->iterator_post_condition,1);
2579 evaluate_statement(stmt->iterator_pre_statement);
2580 evaluate_statement(stmt->iterator_statement);
2581 evaluate_statement(stmt->iterator_post_statement);
2584 static void verify_output_constraint(struct expression *expr, const char *constraint)
2586 switch (*constraint) {
2587 case '=': /* Assignment */
2588 case '+': /* Update */
2589 break;
2590 default:
2591 sparse_error(expr->pos, "output constraint is not an assignment constraint (\"%s\")", constraint);
2595 static void verify_input_constraint(struct expression *expr, const char *constraint)
2597 switch (*constraint) {
2598 case '=': /* Assignment */
2599 case '+': /* Update */
2600 sparse_error(expr->pos, "input constraint with assignment (\"%s\")", constraint);
2604 static void evaluate_asm_statement(struct statement *stmt)
2606 struct expression *expr;
2607 int state;
2609 expr = stmt->asm_string;
2610 if (!expr || expr->type != EXPR_STRING) {
2611 sparse_error(stmt->pos, "need constant string for inline asm");
2612 return;
2615 state = 0;
2616 FOR_EACH_PTR(stmt->asm_outputs, expr) {
2617 struct ident *ident;
2619 switch (state) {
2620 case 0: /* Identifier */
2621 state = 1;
2622 ident = (struct ident *)expr;
2623 continue;
2625 case 1: /* Constraint */
2626 state = 2;
2627 if (!expr || expr->type != EXPR_STRING) {
2628 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
2629 *THIS_ADDRESS(expr) = NULL;
2630 continue;
2632 verify_output_constraint(expr, expr->string->data);
2633 continue;
2635 case 2: /* Expression */
2636 state = 0;
2637 if (!evaluate_expression(expr))
2638 return;
2639 if (!lvalue_expression(expr))
2640 warning(expr->pos, "asm output is not an lvalue");
2641 evaluate_assign_to(expr, expr->ctype);
2642 continue;
2644 } END_FOR_EACH_PTR(expr);
2646 state = 0;
2647 FOR_EACH_PTR(stmt->asm_inputs, expr) {
2648 struct ident *ident;
2650 switch (state) {
2651 case 0: /* Identifier */
2652 state = 1;
2653 ident = (struct ident *)expr;
2654 continue;
2656 case 1: /* Constraint */
2657 state = 2;
2658 if (!expr || expr->type != EXPR_STRING) {
2659 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
2660 *THIS_ADDRESS(expr) = NULL;
2661 continue;
2663 verify_input_constraint(expr, expr->string->data);
2664 continue;
2666 case 2: /* Expression */
2667 state = 0;
2668 if (!evaluate_expression(expr))
2669 return;
2670 continue;
2672 } END_FOR_EACH_PTR(expr);
2674 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
2675 if (!expr) {
2676 sparse_error(stmt->pos, "bad asm output");
2677 return;
2679 if (expr->type == EXPR_STRING)
2680 continue;
2681 sparse_error(expr->pos, "asm clobber is not a string");
2682 } END_FOR_EACH_PTR(expr);
2685 static void evaluate_case_statement(struct statement *stmt)
2687 evaluate_expression(stmt->case_expression);
2688 evaluate_expression(stmt->case_to);
2689 evaluate_statement(stmt->case_statement);
2692 static void check_case_type(struct expression *switch_expr,
2693 struct expression *case_expr,
2694 struct expression **enumcase)
2696 struct symbol *switch_type, *case_type;
2697 int sclass, cclass;
2699 if (!case_expr)
2700 return;
2702 switch_type = switch_expr->ctype;
2703 case_type = evaluate_expression(case_expr);
2705 if (!switch_type || !case_type)
2706 goto Bad;
2707 if (enumcase) {
2708 if (*enumcase)
2709 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
2710 else if (is_enum_type(case_type))
2711 *enumcase = case_expr;
2714 sclass = classify_type(switch_type, &switch_type);
2715 cclass = classify_type(case_type, &case_type);
2717 /* both should be arithmetic */
2718 if (!(sclass & cclass & TYPE_NUM))
2719 goto Bad;
2721 /* neither should be floating */
2722 if ((sclass | cclass) & TYPE_FLOAT)
2723 goto Bad;
2725 /* if neither is restricted, we are OK */
2726 if (!((sclass | cclass) & TYPE_RESTRICT))
2727 return;
2729 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
2730 cclass, sclass, case_type, switch_type))
2731 warning(case_expr->pos, "restricted degrades to integer");
2733 return;
2735 Bad:
2736 sparse_error(case_expr->pos, "incompatible types for 'case' statement");
2739 static void evaluate_switch_statement(struct statement *stmt)
2741 struct symbol *sym;
2742 struct expression *enumcase = NULL;
2743 struct expression **enumcase_holder = &enumcase;
2744 struct expression *sel = stmt->switch_expression;
2746 evaluate_expression(sel);
2747 evaluate_statement(stmt->switch_statement);
2748 if (!sel)
2749 return;
2750 if (sel->ctype && is_enum_type(sel->ctype))
2751 enumcase_holder = NULL; /* Only check cases against switch */
2753 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
2754 struct statement *case_stmt = sym->stmt;
2755 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
2756 check_case_type(sel, case_stmt->case_to, enumcase_holder);
2757 } END_FOR_EACH_PTR(sym);
2760 struct symbol *evaluate_statement(struct statement *stmt)
2762 if (!stmt)
2763 return NULL;
2765 switch (stmt->type) {
2766 case STMT_DECLARATION: {
2767 struct symbol *s;
2768 FOR_EACH_PTR(stmt->declaration, s) {
2769 evaluate_symbol(s);
2770 } END_FOR_EACH_PTR(s);
2771 return NULL;
2774 case STMT_RETURN:
2775 return evaluate_return_expression(stmt);
2777 case STMT_EXPRESSION:
2778 if (!evaluate_expression(stmt->expression))
2779 return NULL;
2780 return degenerate(stmt->expression);
2782 case STMT_COMPOUND: {
2783 struct statement *s;
2784 struct symbol *type = NULL;
2786 /* Evaluate the return symbol in the compound statement */
2787 evaluate_symbol(stmt->ret);
2790 * Then, evaluate each statement, making the type of the
2791 * compound statement be the type of the last statement
2793 type = NULL;
2794 FOR_EACH_PTR(stmt->stmts, s) {
2795 type = evaluate_statement(s);
2796 } END_FOR_EACH_PTR(s);
2797 if (!type)
2798 type = &void_ctype;
2799 return type;
2801 case STMT_IF:
2802 evaluate_if_statement(stmt);
2803 return NULL;
2804 case STMT_ITERATOR:
2805 evaluate_iterator(stmt);
2806 return NULL;
2807 case STMT_SWITCH:
2808 evaluate_switch_statement(stmt);
2809 return NULL;
2810 case STMT_CASE:
2811 evaluate_case_statement(stmt);
2812 return NULL;
2813 case STMT_LABEL:
2814 return evaluate_statement(stmt->label_statement);
2815 case STMT_GOTO:
2816 evaluate_expression(stmt->goto_expression);
2817 return NULL;
2818 case STMT_NONE:
2819 break;
2820 case STMT_ASM:
2821 evaluate_asm_statement(stmt);
2822 return NULL;
2823 case STMT_CONTEXT:
2824 evaluate_expression(stmt->expression);
2825 return NULL;
2826 case STMT_RANGE:
2827 evaluate_expression(stmt->range_expression);
2828 evaluate_expression(stmt->range_low);
2829 evaluate_expression(stmt->range_high);
2830 return NULL;
2832 return NULL;