[PATCH] remove long-dead variable in evaluate_ptr_add()
[smatch.git] / evaluate.c
blob5eba8174b6a13e3cb72771f086875a1a7aedef3a
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 return ctype;
526 Restr:
527 ctype = restricted_binop_type(op, left, right,
528 lclass, rclass, ltype, rtype);
529 if (ctype)
530 goto Convert;
532 if (lclass & TYPE_RESTRICT) {
533 warning(left->pos, "restricted degrades to integer");
534 if (lclass & TYPE_FOULED)
535 ltype = ltype->ctype.base_type;
536 ltype = ltype->ctype.base_type;
538 if (rclass & TYPE_RESTRICT) {
539 warning(right->pos, "restricted degrades to integer");
540 if (rclass & TYPE_FOULED)
541 rtype = rtype->ctype.base_type;
542 rtype = rtype->ctype.base_type;
544 goto Normal;
547 static struct symbol *evaluate_arith(struct expression *expr, int float_ok)
549 struct symbol *ltype, *rtype;
550 int lclass = classify_type(expr->left->ctype, &ltype);
551 int rclass = classify_type(expr->right->ctype, &rtype);
552 struct symbol *ctype;
554 if (!(lclass & rclass & TYPE_NUM))
555 goto Bad;
557 if (!float_ok && (lclass | rclass) & TYPE_FLOAT)
558 goto Bad;
560 ctype = usual_conversions(expr->op, expr->left, expr->right,
561 lclass, rclass, ltype, rtype);
562 expr->left = cast_to(expr->left, ctype);
563 expr->right = cast_to(expr->right, ctype);
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 inline int want_int(struct expression **expr, struct symbol **ctype)
587 int class = classify_type((*expr)->ctype, ctype);
589 if (!(class & TYPE_NUM))
590 return 0;
591 if (!(class & TYPE_RESTRICT))
592 return 1;
593 warning((*expr)->pos, "restricted degrades to integer");
594 if (class & TYPE_FOULED) /* unfoul it first */
595 (*ctype) = (*ctype)->ctype.base_type;
596 (*ctype) = (*ctype)->ctype.base_type; /* get to arithmetic type */
597 *expr = cast_to(*expr, *ctype);
598 return 1;
601 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *ctype, struct expression **ip)
603 struct expression *i = *ip;
604 struct symbol *itype;
605 int bit_size;
607 if (!want_int(&i, &itype))
608 return bad_expr_type(expr);
610 examine_symbol_type(ctype);
612 if (!ctype->ctype.base_type) {
613 expression_error(expr, "missing type information");
614 return NULL;
617 /* Get the size of whatever the pointer points to */
618 bit_size = ptr_object_size(ctype);
620 if (bit_size > bits_in_char) {
621 int multiply = bit_size >> 3;
622 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
624 if (i->type == EXPR_VALUE) {
625 val->value = i->value * multiply;
626 val->ctype = size_t_ctype;
627 *ip = val;
628 } else {
629 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
631 val->ctype = size_t_ctype;
632 val->value = bit_size >> 3;
634 mul->op = '*';
635 mul->ctype = size_t_ctype;
636 mul->left = i;
637 mul->right = val;
639 *ip = mul;
643 expr->ctype = ctype;
644 return ctype;
647 static struct symbol *evaluate_add(struct expression *expr)
649 struct expression *left = expr->left, *right = expr->right;
650 struct symbol *ltype = left->ctype, *rtype = right->ctype;
652 if (is_ptr_type(ltype))
653 return evaluate_ptr_add(expr, degenerate(left), &expr->right);
655 if (is_ptr_type(rtype))
656 return evaluate_ptr_add(expr, degenerate(right), &expr->left);
658 return evaluate_arith(expr, 1);
661 const char * type_difference(struct symbol *target, struct symbol *source,
662 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
664 for (;;) {
665 unsigned long mod1, mod2, diff;
666 unsigned long as1, as2;
667 int type1, type2;
668 struct symbol *base1, *base2;
670 if (target == source)
671 break;
672 if (!target || !source)
673 return "different types";
675 * Peel of per-node information.
676 * FIXME! Check alignment and context too here!
678 mod1 = target->ctype.modifiers;
679 as1 = target->ctype.as;
680 mod2 = source->ctype.modifiers;
681 as2 = source->ctype.as;
682 if (target->type == SYM_NODE) {
683 target = target->ctype.base_type;
684 if (!target)
685 return "bad types";
686 if (target->type == SYM_PTR) {
687 mod1 = 0;
688 as1 = 0;
690 mod1 |= target->ctype.modifiers;
691 as1 |= target->ctype.as;
693 if (source->type == SYM_NODE) {
694 source = source->ctype.base_type;
695 if (!source)
696 return "bad types";
697 if (source->type == SYM_PTR) {
698 mod2 = 0;
699 as2 = 0;
701 mod2 |= source->ctype.modifiers;
702 as2 |= source->ctype.as;
704 if (target->type == SYM_ENUM) {
705 target = target->ctype.base_type;
706 if (!target)
707 return "bad types";
709 if (source->type == SYM_ENUM) {
710 source = source->ctype.base_type;
711 if (!source)
712 return "bad types";
715 if (target == source)
716 break;
717 if (!target || !source)
718 return "different types";
720 type1 = target->type;
721 base1 = target->ctype.base_type;
723 type2 = source->type;
724 base2 = source->ctype.base_type;
727 * Pointers to functions compare as the function itself
729 if (type1 == SYM_PTR && base1) {
730 base1 = examine_symbol_type(base1);
731 switch (base1->type) {
732 case SYM_FN:
733 type1 = SYM_FN;
734 target = base1;
735 base1 = base1->ctype.base_type;
736 default:
737 /* nothing */;
740 if (type2 == SYM_PTR && base2) {
741 base2 = examine_symbol_type(base2);
742 switch (base2->type) {
743 case SYM_FN:
744 type2 = SYM_FN;
745 source = base2;
746 base2 = base2->ctype.base_type;
747 default:
748 /* nothing */;
752 /* Arrays degenerate to pointers for type comparisons */
753 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
754 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
756 if (type1 != type2 || type1 == SYM_RESTRICT)
757 return "different base types";
759 /* Must be same address space to be comparable */
760 if (Waddress_space && as1 != as2)
761 return "different address spaces";
763 /* Ignore differences in storage types or addressability */
764 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
765 diff &= (mod1 & ~target_mod_ignore) | (mod2 & ~source_mod_ignore);
766 if (diff) {
767 if (diff & MOD_SIZE)
768 return "different type sizes";
769 if (diff & ~MOD_SIGNEDNESS)
770 return "different modifiers";
772 /* Differs in signedness only.. */
773 if (Wtypesign) {
775 * Warn if both are explicitly signed ("unsigned" is obviously
776 * always explicit, and since we know one of them has to be
777 * unsigned, we check if the signed one was explicit).
779 if ((mod1 | mod2) & MOD_EXPLICITLY_SIGNED)
780 return "different explicit signedness";
783 * "char" matches both "unsigned char" and "signed char",
784 * so if the explicit test didn't trigger, then we should
785 * not warn about a char.
787 if (!(mod1 & MOD_CHAR))
788 return "different signedness";
792 if (type1 == SYM_FN) {
793 int i;
794 struct symbol *arg1, *arg2;
795 if (base1->variadic != base2->variadic)
796 return "incompatible variadic arguments";
797 PREPARE_PTR_LIST(target->arguments, arg1);
798 PREPARE_PTR_LIST(source->arguments, arg2);
799 i = 1;
800 for (;;) {
801 const char *diffstr;
802 diffstr = type_difference(arg1, arg2, 0, 0);
803 if (diffstr) {
804 static char argdiff[80];
805 sprintf(argdiff, "incompatible argument %d (%s)", i, diffstr);
806 return argdiff;
808 if (!arg1)
809 break;
810 NEXT_PTR_LIST(arg1);
811 NEXT_PTR_LIST(arg2);
812 i++;
814 FINISH_PTR_LIST(arg2);
815 FINISH_PTR_LIST(arg1);
818 target = base1;
819 source = base2;
821 return NULL;
824 static int is_null_ptr(struct expression *expr)
826 if (expr->type != EXPR_VALUE || expr->value)
827 return 0;
828 if (Wnon_pointer_null && !is_ptr_type(expr->ctype))
829 warning(expr->pos, "Using plain integer as NULL pointer");
830 return 1;
834 * Ignore differences in "volatile" and "const"ness when
835 * subtracting pointers
837 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
839 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression **rp)
841 const char *typediff;
842 struct symbol *ctype;
843 struct symbol *ltype, *rtype;
844 struct expression *r = *rp;
846 ltype = degenerate(l);
847 rtype = degenerate(r);
850 * If it is an integer subtract: the ptr add case will do the
851 * right thing.
853 if (!is_ptr_type(rtype))
854 return evaluate_ptr_add(expr, degenerate(l), rp);
856 ctype = ltype;
857 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
858 if (typediff)
859 expression_error(expr, "subtraction of different types can't work (%s)", typediff);
860 examine_symbol_type(ctype);
862 /* Figure out the base type we point to */
863 if (ctype->type == SYM_NODE)
864 ctype = ctype->ctype.base_type;
865 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
866 expression_error(expr, "subtraction of functions? Share your drugs");
867 return NULL;
869 ctype = get_base_type(ctype);
871 expr->ctype = ssize_t_ctype;
872 if (ctype->bit_size > bits_in_char) {
873 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
874 struct expression *div = expr;
875 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
876 unsigned long value = ctype->bit_size >> 3;
878 val->ctype = size_t_ctype;
879 val->value = value;
881 if (value & (value-1)) {
882 if (Wptr_subtraction_blows)
883 warning(expr->pos, "potentially expensive pointer subtraction");
886 sub->op = '-';
887 sub->ctype = ssize_t_ctype;
888 sub->left = l;
889 sub->right = r;
891 div->op = '/';
892 div->left = sub;
893 div->right = val;
896 return ssize_t_ctype;
899 static struct symbol *evaluate_sub(struct expression *expr)
901 struct expression *left = expr->left;
902 struct symbol *ltype = left->ctype;
904 if (is_ptr_type(ltype))
905 return evaluate_ptr_sub(expr, left, &expr->right);
907 return evaluate_arith(expr, 1);
910 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
912 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
914 struct symbol *ctype;
916 if (!expr)
917 return NULL;
919 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
920 warning(expr->pos, "assignment expression in conditional");
922 ctype = evaluate_expression(expr);
923 if (ctype) {
924 if (is_safe_type(ctype))
925 warning(expr->pos, "testing a 'safe expression'");
928 return ctype;
931 static struct symbol *evaluate_logical(struct expression *expr)
933 if (!evaluate_conditional(expr->left, 0))
934 return NULL;
935 if (!evaluate_conditional(expr->right, 0))
936 return NULL;
938 expr->ctype = &bool_ctype;
939 return &bool_ctype;
942 static struct symbol *evaluate_shift(struct expression *expr)
944 struct symbol *ltype, *rtype;
946 if (want_int(&expr->left, &ltype) && want_int(&expr->right, &rtype)) {
947 struct symbol *ctype = integer_promotion(ltype);
948 expr->left = cast_to(expr->left, ctype);
949 expr->ctype = ctype;
950 ctype = integer_promotion(rtype);
951 expr->right = cast_to(expr->right, ctype);
952 return expr->ctype;
954 return bad_expr_type(expr);
957 static struct symbol *evaluate_binop(struct expression *expr)
959 switch (expr->op) {
960 // addition can take ptr+int, fp and int
961 case '+':
962 return evaluate_add(expr);
964 // subtraction can take ptr-ptr, fp and int
965 case '-':
966 return evaluate_sub(expr);
968 // Arithmetic operations can take fp and int
969 case '*': case '/':
970 return evaluate_arith(expr, 1);
972 // shifts do integer promotions, but that's it.
973 case SPECIAL_LEFTSHIFT: case SPECIAL_RIGHTSHIFT:
974 return evaluate_shift(expr);
976 // The rest are integer operations
977 // '%', '&', '^', '|'
978 default:
979 return evaluate_arith(expr, 0);
983 static struct symbol *evaluate_comma(struct expression *expr)
985 expr->ctype = expr->right->ctype;
986 return expr->ctype;
989 static int modify_for_unsigned(int op)
991 if (op == '<')
992 op = SPECIAL_UNSIGNED_LT;
993 else if (op == '>')
994 op = SPECIAL_UNSIGNED_GT;
995 else if (op == SPECIAL_LTE)
996 op = SPECIAL_UNSIGNED_LTE;
997 else if (op == SPECIAL_GTE)
998 op = SPECIAL_UNSIGNED_GTE;
999 return op;
1002 static struct symbol *evaluate_compare(struct expression *expr)
1004 struct expression *left = expr->left, *right = expr->right;
1005 struct symbol *ltype = left->ctype, *rtype = right->ctype;
1006 struct symbol *ctype;
1008 /* Type types? */
1009 if (is_type_type(ltype) && is_type_type(rtype))
1010 goto OK;
1012 if (is_safe_type(ltype) || is_safe_type(rtype))
1013 warning(expr->pos, "testing a 'safe expression'");
1015 /* Pointer types? */
1016 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
1017 // FIXME! Check the types for compatibility
1018 expr->op = modify_for_unsigned(expr->op);
1019 goto OK;
1022 ctype = evaluate_arith(expr, 1);
1023 if (ctype) {
1024 if (ctype->ctype.modifiers & MOD_UNSIGNED)
1025 expr->op = modify_for_unsigned(expr->op);
1028 expr->ctype = &bool_ctype;
1029 return &bool_ctype;
1033 * FIXME!! This should do casts, array degeneration etc..
1035 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
1037 struct symbol *ltype = left->ctype, *rtype = right->ctype;
1039 if (ltype->type == SYM_NODE)
1040 ltype = ltype->ctype.base_type;
1042 if (rtype->type == SYM_NODE)
1043 rtype = rtype->ctype.base_type;
1045 if (ltype->type == SYM_PTR) {
1046 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
1047 return ltype;
1050 if (rtype->type == SYM_PTR) {
1051 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
1052 return rtype;
1054 return NULL;
1058 * NOTE! The degenerate case of "x ? : y", where we don't
1059 * have a true case, this will possibly promote "x" to the
1060 * same type as "y", and thus _change_ the conditional
1061 * test in the expression. But since promotion is "safe"
1062 * for testing, that's OK.
1064 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1066 struct expression **true;
1067 struct symbol *ctype, *ltype, *rtype;
1068 int lclass, rclass;
1069 const char * typediff;
1071 if (!evaluate_conditional(expr->conditional, 0))
1072 return NULL;
1073 if (!evaluate_expression(expr->cond_false))
1074 return NULL;
1076 ctype = degenerate(expr->conditional);
1077 rtype = degenerate(expr->cond_false);
1079 true = &expr->conditional;
1080 ltype = ctype;
1081 if (expr->cond_true) {
1082 if (!evaluate_expression(expr->cond_true))
1083 return NULL;
1084 ltype = degenerate(expr->cond_true);
1085 true = &expr->cond_true;
1088 lclass = classify_type(ltype, &ltype);
1089 rclass = classify_type(rtype, &rtype);
1090 if (lclass & rclass & TYPE_NUM) {
1091 ctype = usual_conversions('?', *true, expr->cond_false,
1092 lclass, rclass, ltype, rtype);
1093 *true = cast_to(*true, ctype);
1094 expr->cond_false = cast_to(expr->cond_false, ctype);
1095 goto out;
1097 ctype = compatible_ptr_type(*true, expr->cond_false);
1098 if (ctype)
1099 goto out;
1100 ctype = ltype;
1101 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
1102 if (!typediff)
1103 goto out;
1104 expression_error(expr, "incompatible types in conditional expression (%s)", typediff);
1105 return NULL;
1107 out:
1108 expr->ctype = ctype;
1109 return ctype;
1112 /* FP assignments can not do modulo or bit operations */
1113 static int compatible_float_op(int op)
1115 return op == SPECIAL_ADD_ASSIGN ||
1116 op == SPECIAL_SUB_ASSIGN ||
1117 op == SPECIAL_MUL_ASSIGN ||
1118 op == SPECIAL_DIV_ASSIGN;
1121 static int evaluate_assign_op(struct expression *expr, struct symbol *target,
1122 struct expression **rp, struct symbol *source, int op)
1124 struct symbol *t, *s;
1125 int tclass = classify_type(target, &t);
1126 int sclass = classify_type(source, &s);
1128 if (tclass & sclass & TYPE_NUM) {
1129 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1130 expression_error(expr, "invalid assignment");
1131 return 0;
1133 if (tclass & TYPE_RESTRICT) {
1134 if (!restricted_binop(op, target)) {
1135 expression_error(expr, "bad restricted assignment");
1136 return 0;
1138 /* allowed assignments unfoul */
1139 if (sclass & TYPE_FOULED && s->ctype.base_type == t)
1140 goto Cast;
1141 if (!restricted_value(*rp, target))
1142 return 1;
1143 } else if (!(sclass & TYPE_RESTRICT))
1144 goto Cast;
1145 /* source and target would better be identical restricted */
1146 if (t == s)
1147 return 1;
1148 warning(expr->pos, "invalid restricted assignment");
1149 *rp = cast_to(*rp, target);
1150 return 0;
1151 } else if (tclass & TYPE_PTR) {
1152 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1153 evaluate_ptr_add(expr, target, rp);
1154 return 1;
1156 expression_error(expr, "invalid pointer assignment");
1157 return 0;
1158 } else {
1159 expression_error(expr, "invalid assignment");
1160 return 0;
1163 Cast:
1164 *rp = cast_to(*rp, target);
1165 return 1;
1168 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1169 struct expression **rp, struct symbol *source, const char *where)
1171 const char *typediff;
1172 struct symbol *t, *s;
1173 int target_as;
1174 int tclass = classify_type(target, &t);
1175 int sclass = classify_type(source, &s);
1177 if (tclass & sclass & TYPE_NUM) {
1178 if (tclass & TYPE_RESTRICT) {
1179 /* allowed assignments unfoul */
1180 if (sclass & TYPE_FOULED && s->ctype.base_type == t)
1181 goto Cast;
1182 if (!restricted_value(*rp, target))
1183 return 1;
1184 } else if (!(sclass & TYPE_RESTRICT))
1185 goto Cast;
1188 /* It's OK if the target is more volatile or const than the source */
1189 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
1190 if (!typediff)
1191 return 1;
1193 /* Pointer destination? */
1194 if (tclass & TYPE_PTR) {
1195 struct expression *right = *rp;
1196 int source_as;
1198 // NULL pointer is always OK
1199 if (is_null_ptr(right))
1200 goto Cast;
1202 /* "void *" matches anything as long as the address space is OK */
1203 target_as = t->ctype.as | target->ctype.as;
1204 source_as = s->ctype.as | source->ctype.as;
1205 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
1206 s = get_base_type(s);
1207 t = get_base_type(t);
1208 if (s == &void_ctype || t == &void_ctype)
1209 goto Cast;
1213 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1214 info(expr->pos, " expected %s", show_typename(target));
1215 info(expr->pos, " got %s", show_typename(source));
1216 *rp = cast_to(*rp, target);
1217 return 0;
1218 Cast:
1219 *rp = cast_to(*rp, target);
1220 return 1;
1223 static void mark_assigned(struct expression *expr)
1225 struct symbol *sym;
1227 if (!expr)
1228 return;
1229 switch (expr->type) {
1230 case EXPR_SYMBOL:
1231 sym = expr->symbol;
1232 if (!sym)
1233 return;
1234 if (sym->type != SYM_NODE)
1235 return;
1236 sym->ctype.modifiers |= MOD_ASSIGNED;
1237 return;
1239 case EXPR_BINOP:
1240 mark_assigned(expr->left);
1241 mark_assigned(expr->right);
1242 return;
1243 case EXPR_CAST:
1244 mark_assigned(expr->cast_expression);
1245 return;
1246 case EXPR_SLICE:
1247 mark_assigned(expr->base);
1248 return;
1249 default:
1250 /* Hmm? */
1251 return;
1255 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1257 if (type->ctype.modifiers & MOD_CONST)
1258 expression_error(left, "assignment to const expression");
1260 /* We know left is an lvalue, so it's a "preop-*" */
1261 mark_assigned(left->unop);
1264 static struct symbol *evaluate_assignment(struct expression *expr)
1266 struct expression *left = expr->left, *right = expr->right;
1267 struct expression *where = expr;
1268 struct symbol *ltype, *rtype;
1270 if (!lvalue_expression(left)) {
1271 expression_error(expr, "not an lvalue");
1272 return NULL;
1275 ltype = left->ctype;
1277 rtype = degenerate(right);
1279 if (expr->op != '=') {
1280 if (!evaluate_assign_op(where, ltype, &where->right, rtype, expr->op))
1281 return NULL;
1282 } else {
1283 if (!compatible_assignment_types(where, ltype, &where->right, rtype, "assignment"))
1284 return NULL;
1287 evaluate_assign_to(left, ltype);
1289 expr->ctype = ltype;
1290 return ltype;
1293 static void examine_fn_arguments(struct symbol *fn)
1295 struct symbol *s;
1297 FOR_EACH_PTR(fn->arguments, s) {
1298 struct symbol *arg = evaluate_symbol(s);
1299 /* Array/function arguments silently degenerate into pointers */
1300 if (arg) {
1301 struct symbol *ptr;
1302 switch(arg->type) {
1303 case SYM_ARRAY:
1304 case SYM_FN:
1305 ptr = alloc_symbol(s->pos, SYM_PTR);
1306 if (arg->type == SYM_ARRAY)
1307 ptr->ctype = arg->ctype;
1308 else
1309 ptr->ctype.base_type = arg;
1310 ptr->ctype.as |= s->ctype.as;
1311 ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1313 s->ctype.base_type = ptr;
1314 s->ctype.as = 0;
1315 s->ctype.modifiers &= ~MOD_PTRINHERIT;
1316 s->bit_size = 0;
1317 s->examined = 0;
1318 examine_symbol_type(s);
1319 break;
1320 default:
1321 /* nothing */
1322 break;
1325 } END_FOR_EACH_PTR(s);
1328 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1330 /* Take the modifiers of the pointer, and apply them to the member */
1331 mod |= sym->ctype.modifiers;
1332 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1333 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1334 *newsym = *sym;
1335 newsym->ctype.as = as;
1336 newsym->ctype.modifiers = mod;
1337 sym = newsym;
1339 return sym;
1342 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1344 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1345 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1347 node->ctype.base_type = ptr;
1348 ptr->bit_size = bits_in_pointer;
1349 ptr->ctype.alignment = pointer_alignment;
1351 node->bit_size = bits_in_pointer;
1352 node->ctype.alignment = pointer_alignment;
1354 access_symbol(sym);
1355 if (sym->ctype.modifiers & MOD_REGISTER) {
1356 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1357 sym->ctype.modifiers &= ~MOD_REGISTER;
1359 if (sym->type == SYM_NODE) {
1360 ptr->ctype.as |= sym->ctype.as;
1361 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1362 sym = sym->ctype.base_type;
1364 if (degenerate && sym->type == SYM_ARRAY) {
1365 ptr->ctype.as |= sym->ctype.as;
1366 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1367 sym = sym->ctype.base_type;
1369 ptr->ctype.base_type = sym;
1371 return node;
1374 /* Arrays degenerate into pointers on pointer arithmetic */
1375 static struct symbol *degenerate(struct expression *expr)
1377 struct symbol *ctype, *base;
1379 if (!expr)
1380 return NULL;
1381 ctype = expr->ctype;
1382 if (!ctype)
1383 return NULL;
1384 base = examine_symbol_type(ctype);
1385 if (ctype->type == SYM_NODE)
1386 base = ctype->ctype.base_type;
1388 * Arrays degenerate into pointers to the entries, while
1389 * functions degenerate into pointers to themselves.
1390 * If array was part of non-lvalue compound, we create a copy
1391 * of that compound first and then act as if we were dealing with
1392 * the corresponding field in there.
1394 switch (base->type) {
1395 case SYM_ARRAY:
1396 if (expr->type == EXPR_SLICE) {
1397 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1398 struct expression *e0, *e1, *e2, *e3, *e4;
1400 a->ctype.base_type = expr->base->ctype;
1401 a->bit_size = expr->base->ctype->bit_size;
1402 a->array_size = expr->base->ctype->array_size;
1404 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1405 e0->symbol = a;
1406 e0->ctype = &lazy_ptr_ctype;
1408 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1409 e1->unop = e0;
1410 e1->op = '*';
1411 e1->ctype = expr->base->ctype; /* XXX */
1413 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1414 e2->left = e1;
1415 e2->right = expr->base;
1416 e2->op = '=';
1417 e2->ctype = expr->base->ctype;
1419 if (expr->r_bitpos) {
1420 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1421 e3->op = '+';
1422 e3->left = e0;
1423 e3->right = alloc_const_expression(expr->pos,
1424 expr->r_bitpos >> 3);
1425 e3->ctype = &lazy_ptr_ctype;
1426 } else {
1427 e3 = e0;
1430 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1431 e4->left = e2;
1432 e4->right = e3;
1433 e4->ctype = &lazy_ptr_ctype;
1435 expr->unop = e4;
1436 expr->type = EXPR_PREOP;
1437 expr->op = '*';
1439 case SYM_FN:
1440 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1441 expression_error(expr, "strange non-value function or array");
1442 return &bad_ctype;
1444 *expr = *expr->unop;
1445 ctype = create_pointer(expr, ctype, 1);
1446 expr->ctype = ctype;
1447 default:
1448 /* nothing */;
1450 return ctype;
1453 static struct symbol *evaluate_addressof(struct expression *expr)
1455 struct expression *op = expr->unop;
1456 struct symbol *ctype;
1458 if (op->op != '*' || op->type != EXPR_PREOP) {
1459 expression_error(expr, "not addressable");
1460 return NULL;
1462 ctype = op->ctype;
1463 *expr = *op->unop;
1465 if (expr->type == EXPR_SYMBOL) {
1466 struct symbol *sym = expr->symbol;
1467 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1471 * symbol expression evaluation is lazy about the type
1472 * of the sub-expression, so we may have to generate
1473 * the type here if so..
1475 if (expr->ctype == &lazy_ptr_ctype) {
1476 ctype = create_pointer(expr, ctype, 0);
1477 expr->ctype = ctype;
1479 return expr->ctype;
1483 static struct symbol *evaluate_dereference(struct expression *expr)
1485 struct expression *op = expr->unop;
1486 struct symbol *ctype = op->ctype, *node, *target;
1488 /* Simplify: *&(expr) => (expr) */
1489 if (op->type == EXPR_PREOP && op->op == '&') {
1490 *expr = *op->unop;
1491 return expr->ctype;
1494 /* Dereferencing a node drops all the node information. */
1495 if (ctype->type == SYM_NODE)
1496 ctype = ctype->ctype.base_type;
1498 node = alloc_symbol(expr->pos, SYM_NODE);
1499 target = ctype->ctype.base_type;
1501 switch (ctype->type) {
1502 default:
1503 expression_error(expr, "cannot dereference this type");
1504 return NULL;
1505 case SYM_PTR:
1506 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1507 merge_type(node, ctype);
1508 break;
1510 case SYM_ARRAY:
1511 if (!lvalue_expression(op)) {
1512 expression_error(op, "non-lvalue array??");
1513 return NULL;
1516 /* Do the implied "addressof" on the array */
1517 *op = *op->unop;
1520 * When an array is dereferenced, we need to pick
1521 * up the attributes of the original node too..
1523 merge_type(node, op->ctype);
1524 merge_type(node, ctype);
1525 break;
1528 node->bit_size = target->bit_size;
1529 node->array_size = target->array_size;
1531 expr->ctype = node;
1532 return node;
1536 * Unary post-ops: x++ and x--
1538 static struct symbol *evaluate_postop(struct expression *expr)
1540 struct expression *op = expr->unop;
1541 struct symbol *ctype = op->ctype;
1543 if (!lvalue_expression(expr->unop)) {
1544 expression_error(expr, "need lvalue expression for ++/--");
1545 return NULL;
1547 if (is_restricted_type(ctype) && restricted_unop(expr->op, &ctype)) {
1548 expression_error(expr, "bad operation on restricted");
1549 return NULL;
1550 } else if (is_fouled_type(ctype) && restricted_unop(expr->op, &ctype)) {
1551 expression_error(expr, "bad operation on restricted");
1552 return NULL;
1555 evaluate_assign_to(op, ctype);
1557 expr->ctype = ctype;
1558 expr->op_value = 1;
1559 if (is_ptr_type(ctype))
1560 expr->op_value = ptr_object_size(ctype) >> 3;
1562 return ctype;
1565 static struct symbol *evaluate_sign(struct expression *expr)
1567 struct symbol *ctype = expr->unop->ctype;
1568 if (is_int_type(ctype)) {
1569 struct symbol *rtype = rtype = integer_promotion(ctype);
1570 expr->unop = cast_to(expr->unop, rtype);
1571 ctype = rtype;
1572 } else if (is_float_type(ctype) && expr->op != '~') {
1573 /* no conversions needed */
1574 } else if (is_restricted_type(ctype) && !restricted_unop(expr->op, &ctype)) {
1575 /* no conversions needed */
1576 } else if (is_fouled_type(ctype) && !restricted_unop(expr->op, &ctype)) {
1577 /* no conversions needed */
1578 } else {
1579 return bad_expr_type(expr);
1581 if (expr->op == '+')
1582 *expr = *expr->unop;
1583 expr->ctype = ctype;
1584 return ctype;
1587 static struct symbol *evaluate_preop(struct expression *expr)
1589 struct symbol *ctype = expr->unop->ctype;
1591 switch (expr->op) {
1592 case '(':
1593 *expr = *expr->unop;
1594 return ctype;
1596 case '+':
1597 case '-':
1598 case '~':
1599 return evaluate_sign(expr);
1601 case '*':
1602 return evaluate_dereference(expr);
1604 case '&':
1605 return evaluate_addressof(expr);
1607 case SPECIAL_INCREMENT:
1608 case SPECIAL_DECREMENT:
1610 * From a type evaluation standpoint the preops are
1611 * the same as the postops
1613 return evaluate_postop(expr);
1615 case '!':
1616 if (is_safe_type(ctype))
1617 warning(expr->pos, "testing a 'safe expression'");
1618 if (is_float_type(ctype)) {
1619 struct expression *arg = expr->unop;
1620 expr->type = EXPR_BINOP;
1621 expr->op = SPECIAL_EQUAL;
1622 expr->left = arg;
1623 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1624 expr->right->ctype = ctype;
1625 expr->right->fvalue = 0;
1626 } else if (is_fouled_type(ctype)) {
1627 warning(expr->pos, "restricted degrades to integer");
1629 ctype = &bool_ctype;
1630 break;
1632 default:
1633 break;
1635 expr->ctype = ctype;
1636 return &bool_ctype;
1639 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1641 struct ptr_list *head = (struct ptr_list *)_list;
1642 struct ptr_list *list = head;
1644 if (!head)
1645 return NULL;
1646 do {
1647 int i;
1648 for (i = 0; i < list->nr; i++) {
1649 struct symbol *sym = (struct symbol *) list->list[i];
1650 if (sym->ident) {
1651 if (sym->ident != ident)
1652 continue;
1653 *offset = sym->offset;
1654 return sym;
1655 } else {
1656 struct symbol *ctype = sym->ctype.base_type;
1657 struct symbol *sub;
1658 if (!ctype)
1659 continue;
1660 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1661 continue;
1662 sub = find_identifier(ident, ctype->symbol_list, offset);
1663 if (!sub)
1664 continue;
1665 *offset += sym->offset;
1666 return sub;
1669 } while ((list = list->next) != head);
1670 return NULL;
1673 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1675 struct expression *add;
1678 * Create a new add-expression
1680 * NOTE! Even if we just add zero, we need a new node
1681 * for the member pointer, since it has a different
1682 * type than the original pointer. We could make that
1683 * be just a cast, but the fact is, a node is a node,
1684 * so we might as well just do the "add zero" here.
1686 add = alloc_expression(expr->pos, EXPR_BINOP);
1687 add->op = '+';
1688 add->left = expr;
1689 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1690 add->right->ctype = &int_ctype;
1691 add->right->value = offset;
1694 * The ctype of the pointer will be lazily evaluated if
1695 * we ever take the address of this member dereference..
1697 add->ctype = &lazy_ptr_ctype;
1698 return add;
1701 /* structure/union dereference */
1702 static struct symbol *evaluate_member_dereference(struct expression *expr)
1704 int offset;
1705 struct symbol *ctype, *member;
1706 struct expression *deref = expr->deref, *add;
1707 struct ident *ident = expr->member;
1708 unsigned int mod;
1709 int address_space;
1711 if (!evaluate_expression(deref))
1712 return NULL;
1713 if (!ident) {
1714 expression_error(expr, "bad member name");
1715 return NULL;
1718 ctype = deref->ctype;
1719 address_space = ctype->ctype.as;
1720 mod = ctype->ctype.modifiers;
1721 if (ctype->type == SYM_NODE) {
1722 ctype = ctype->ctype.base_type;
1723 address_space |= ctype->ctype.as;
1724 mod |= ctype->ctype.modifiers;
1726 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1727 expression_error(expr, "expected structure or union");
1728 return NULL;
1730 examine_symbol_type(ctype);
1731 offset = 0;
1732 member = find_identifier(ident, ctype->symbol_list, &offset);
1733 if (!member) {
1734 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1735 const char *name = "<unnamed>";
1736 int namelen = 9;
1737 if (ctype->ident) {
1738 name = ctype->ident->name;
1739 namelen = ctype->ident->len;
1741 if (ctype->symbol_list)
1742 expression_error(expr, "no member '%s' in %s %.*s",
1743 show_ident(ident), type, namelen, name);
1744 else
1745 expression_error(expr, "using member '%s' in "
1746 "incomplete %s %.*s", show_ident(ident),
1747 type, namelen, name);
1748 return NULL;
1752 * The member needs to take on the address space and modifiers of
1753 * the "parent" type.
1755 member = convert_to_as_mod(member, address_space, mod);
1756 ctype = get_base_type(member);
1758 if (!lvalue_expression(deref)) {
1759 if (deref->type != EXPR_SLICE) {
1760 expr->base = deref;
1761 expr->r_bitpos = 0;
1762 } else {
1763 expr->base = deref->base;
1764 expr->r_bitpos = deref->r_bitpos;
1766 expr->r_bitpos += offset << 3;
1767 expr->type = EXPR_SLICE;
1768 expr->r_nrbits = member->bit_size;
1769 expr->r_bitpos += member->bit_offset;
1770 expr->ctype = member;
1771 return member;
1774 deref = deref->unop;
1775 expr->deref = deref;
1777 add = evaluate_offset(deref, offset);
1778 expr->type = EXPR_PREOP;
1779 expr->op = '*';
1780 expr->unop = add;
1782 expr->ctype = member;
1783 return member;
1786 static int is_promoted(struct expression *expr)
1788 while (1) {
1789 switch (expr->type) {
1790 case EXPR_BINOP:
1791 case EXPR_SELECT:
1792 case EXPR_CONDITIONAL:
1793 return 1;
1794 case EXPR_COMMA:
1795 expr = expr->right;
1796 continue;
1797 case EXPR_PREOP:
1798 switch (expr->op) {
1799 case '(':
1800 expr = expr->unop;
1801 continue;
1802 case '+':
1803 case '-':
1804 case '~':
1805 return 1;
1806 default:
1807 return 0;
1809 default:
1810 return 0;
1816 static struct symbol *evaluate_cast(struct expression *);
1818 static struct symbol *evaluate_type_information(struct expression *expr)
1820 struct symbol *sym = expr->cast_type;
1821 if (!sym) {
1822 sym = evaluate_expression(expr->cast_expression);
1823 if (!sym)
1824 return NULL;
1826 * Expressions of restricted types will possibly get
1827 * promoted - check that here
1829 if (is_restricted_type(sym)) {
1830 if (sym->bit_size < bits_in_int && is_promoted(expr))
1831 sym = &int_ctype;
1832 } else if (is_fouled_type(sym)) {
1833 sym = &int_ctype;
1836 examine_symbol_type(sym);
1837 if (is_bitfield_type(sym)) {
1838 expression_error(expr, "trying to examine bitfield type");
1839 return NULL;
1841 return sym;
1844 static struct symbol *evaluate_sizeof(struct expression *expr)
1846 struct symbol *type;
1847 int size;
1849 type = evaluate_type_information(expr);
1850 if (!type)
1851 return NULL;
1853 size = type->bit_size;
1854 if ((size < 0) || (size & 7))
1855 expression_error(expr, "cannot size expression");
1856 expr->type = EXPR_VALUE;
1857 expr->value = size >> 3;
1858 expr->ctype = size_t_ctype;
1859 return size_t_ctype;
1862 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
1864 struct symbol *type;
1865 int size;
1867 type = evaluate_type_information(expr);
1868 if (!type)
1869 return NULL;
1871 if (type->type == SYM_NODE)
1872 type = type->ctype.base_type;
1873 if (!type)
1874 return NULL;
1875 switch (type->type) {
1876 case SYM_ARRAY:
1877 break;
1878 case SYM_PTR:
1879 type = get_base_type(type);
1880 if (type)
1881 break;
1882 default:
1883 expression_error(expr, "expected pointer expression");
1884 return NULL;
1886 size = type->bit_size;
1887 if (size & 7)
1888 size = 0;
1889 expr->type = EXPR_VALUE;
1890 expr->value = size >> 3;
1891 expr->ctype = size_t_ctype;
1892 return size_t_ctype;
1895 static struct symbol *evaluate_alignof(struct expression *expr)
1897 struct symbol *type;
1899 type = evaluate_type_information(expr);
1900 if (!type)
1901 return NULL;
1903 expr->type = EXPR_VALUE;
1904 expr->value = type->ctype.alignment;
1905 expr->ctype = size_t_ctype;
1906 return size_t_ctype;
1909 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1911 struct expression *expr;
1912 struct symbol_list *argument_types = fn->arguments;
1913 struct symbol *argtype;
1914 int i = 1;
1916 PREPARE_PTR_LIST(argument_types, argtype);
1917 FOR_EACH_PTR (head, expr) {
1918 struct expression **p = THIS_ADDRESS(expr);
1919 struct symbol *ctype, *target;
1920 ctype = evaluate_expression(expr);
1922 if (!ctype)
1923 return 0;
1925 ctype = degenerate(expr);
1927 target = argtype;
1928 if (!target && ctype->bit_size < bits_in_int)
1929 target = &int_ctype;
1930 if (target) {
1931 static char where[30];
1932 examine_symbol_type(target);
1933 sprintf(where, "argument %d", i);
1934 compatible_assignment_types(expr, target, p, ctype, where);
1937 i++;
1938 NEXT_PTR_LIST(argtype);
1939 } END_FOR_EACH_PTR(expr);
1940 FINISH_PTR_LIST(argtype);
1941 return 1;
1944 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
1946 struct symbol *sym;
1948 FOR_EACH_PTR(ctype->symbol_list, sym) {
1949 if (sym->ident == ident)
1950 return sym;
1951 } END_FOR_EACH_PTR(sym);
1952 return NULL;
1955 static void convert_index(struct expression *e)
1957 struct expression *child = e->idx_expression;
1958 unsigned from = e->idx_from;
1959 unsigned to = e->idx_to + 1;
1960 e->type = EXPR_POS;
1961 e->init_offset = from * (e->ctype->bit_size>>3);
1962 e->init_nr = to - from;
1963 e->init_expr = child;
1966 static void convert_ident(struct expression *e)
1968 struct expression *child = e->ident_expression;
1969 struct symbol *sym = e->field;
1970 e->type = EXPR_POS;
1971 e->init_offset = sym->offset;
1972 e->init_nr = 1;
1973 e->init_expr = child;
1976 static void convert_designators(struct expression *e)
1978 while (e) {
1979 if (e->type == EXPR_INDEX)
1980 convert_index(e);
1981 else if (e->type == EXPR_IDENTIFIER)
1982 convert_ident(e);
1983 else
1984 break;
1985 e = e->init_expr;
1989 static void excess(struct expression *e, const char *s)
1991 warning(e->pos, "excessive elements in %s initializer", s);
1995 * implicit designator for the first element
1997 static struct expression *first_subobject(struct symbol *ctype, int class,
1998 struct expression **v)
2000 struct expression *e = *v, *new;
2002 if (ctype->type == SYM_NODE)
2003 ctype = ctype->ctype.base_type;
2005 if (class & TYPE_PTR) { /* array */
2006 if (!ctype->bit_size)
2007 return NULL;
2008 new = alloc_expression(e->pos, EXPR_INDEX);
2009 new->idx_expression = e;
2010 new->ctype = ctype->ctype.base_type;
2011 } else {
2012 struct symbol *field, *p;
2013 PREPARE_PTR_LIST(ctype->symbol_list, p);
2014 while (p && !p->ident && is_bitfield_type(p))
2015 NEXT_PTR_LIST(p);
2016 field = p;
2017 FINISH_PTR_LIST(p);
2018 if (!field)
2019 return NULL;
2020 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2021 new->ident_expression = e;
2022 new->field = new->ctype = field;
2024 *v = new;
2025 return new;
2029 * sanity-check explicit designators; return the innermost one or NULL
2030 * in case of error. Assign types.
2032 static struct expression *check_designators(struct expression *e,
2033 struct symbol *ctype)
2035 struct expression *last = NULL;
2036 const char *err;
2037 while (1) {
2038 if (ctype->type == SYM_NODE)
2039 ctype = ctype->ctype.base_type;
2040 if (e->type == EXPR_INDEX) {
2041 struct symbol *type;
2042 if (ctype->type != SYM_ARRAY) {
2043 err = "array index in non-array";
2044 break;
2046 type = ctype->ctype.base_type;
2047 if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2048 unsigned offset = e->idx_to * type->bit_size;
2049 if (offset >= ctype->bit_size) {
2050 err = "index out of bounds in";
2051 break;
2054 e->ctype = ctype = type;
2055 ctype = type;
2056 last = e;
2057 if (!e->idx_expression) {
2058 err = "invalid";
2059 break;
2061 e = e->idx_expression;
2062 } else if (e->type == EXPR_IDENTIFIER) {
2063 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2064 err = "field name not in struct or union";
2065 break;
2067 ctype = find_struct_ident(ctype, e->expr_ident);
2068 if (!ctype) {
2069 err = "unknown field name in";
2070 break;
2072 e->field = e->ctype = ctype;
2073 last = e;
2074 if (!e->ident_expression) {
2075 err = "invalid";
2076 break;
2078 e = e->ident_expression;
2079 } else if (e->type == EXPR_POS) {
2080 err = "internal front-end error: EXPR_POS in";
2081 break;
2082 } else
2083 return last;
2085 expression_error(e, "%s initializer", err);
2086 return NULL;
2090 * choose the next subobject to initialize.
2092 * Get designators for next element, switch old ones to EXPR_POS.
2093 * Return the resulting expression or NULL if we'd run out of subobjects.
2094 * The innermost designator is returned in *v. Designators in old
2095 * are assumed to be already sanity-checked.
2097 static struct expression *next_designators(struct expression *old,
2098 struct symbol *ctype,
2099 struct expression *e, struct expression **v)
2101 struct expression *new = NULL;
2103 if (!old)
2104 return NULL;
2105 if (old->type == EXPR_INDEX) {
2106 struct expression *copy;
2107 unsigned n;
2109 copy = next_designators(old->idx_expression,
2110 old->ctype, e, v);
2111 if (!copy) {
2112 n = old->idx_to + 1;
2113 if (n * old->ctype->bit_size == ctype->bit_size) {
2114 convert_index(old);
2115 return NULL;
2117 copy = e;
2118 *v = new = alloc_expression(e->pos, EXPR_INDEX);
2119 } else {
2120 n = old->idx_to;
2121 new = alloc_expression(e->pos, EXPR_INDEX);
2124 new->idx_from = new->idx_to = n;
2125 new->idx_expression = copy;
2126 new->ctype = old->ctype;
2127 convert_index(old);
2128 } else if (old->type == EXPR_IDENTIFIER) {
2129 struct expression *copy;
2130 struct symbol *field;
2132 copy = next_designators(old->ident_expression,
2133 old->ctype, e, v);
2134 if (!copy) {
2135 field = old->field->next_subobject;
2136 if (!field) {
2137 convert_ident(old);
2138 return NULL;
2140 copy = e;
2141 *v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2142 } else {
2143 field = old->field;
2144 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2147 new->field = field;
2148 new->expr_ident = field->ident;
2149 new->ident_expression = copy;
2150 new->ctype = field;
2151 convert_ident(old);
2153 return new;
2156 static int handle_simple_initializer(struct expression **ep, int nested,
2157 int class, struct symbol *ctype);
2160 * deal with traversing subobjects [6.7.8(17,18,20)]
2162 static void handle_list_initializer(struct expression *expr,
2163 int class, struct symbol *ctype)
2165 struct expression *e, *last = NULL, *top = NULL, *next;
2166 int jumped = 0;
2168 FOR_EACH_PTR(expr->expr_list, e) {
2169 struct expression **v;
2170 struct symbol *type;
2171 int lclass;
2173 if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2174 if (!top) {
2175 top = e;
2176 last = first_subobject(ctype, class, &top);
2177 } else {
2178 last = next_designators(last, ctype, e, &top);
2180 if (!last) {
2181 excess(e, class & TYPE_PTR ? "array" :
2182 "struct or union");
2183 DELETE_CURRENT_PTR(e);
2184 continue;
2186 if (jumped) {
2187 warning(e->pos, "advancing past deep designator");
2188 jumped = 0;
2190 REPLACE_CURRENT_PTR(e, last);
2191 } else {
2192 next = check_designators(e, ctype);
2193 if (!next) {
2194 DELETE_CURRENT_PTR(e);
2195 continue;
2197 top = next;
2198 /* deeper than one designator? */
2199 jumped = top != e;
2200 convert_designators(last);
2201 last = e;
2204 found:
2205 lclass = classify_type(top->ctype, &type);
2206 if (top->type == EXPR_INDEX)
2207 v = &top->idx_expression;
2208 else
2209 v = &top->ident_expression;
2211 if (handle_simple_initializer(v, 1, lclass, top->ctype))
2212 continue;
2214 if (!(lclass & TYPE_COMPOUND)) {
2215 warning(e->pos, "bogus scalar initializer");
2216 DELETE_CURRENT_PTR(e);
2217 continue;
2220 next = first_subobject(type, lclass, v);
2221 if (next) {
2222 warning(e->pos, "missing braces around initializer");
2223 top = next;
2224 goto found;
2227 DELETE_CURRENT_PTR(e);
2228 excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2230 } END_FOR_EACH_PTR(e);
2232 convert_designators(last);
2233 expr->ctype = ctype;
2236 static int is_string_literal(struct expression **v)
2238 struct expression *e = *v;
2239 while (e && e->type == EXPR_PREOP && e->op == '(')
2240 e = e->unop;
2241 if (!e || e->type != EXPR_STRING)
2242 return 0;
2243 if (e != *v && Wparen_string)
2244 warning(e->pos,
2245 "array initialized from parenthesized string constant");
2246 *v = e;
2247 return 1;
2251 * We want a normal expression, possibly in one layer of braces. Warn
2252 * if the latter happens inside a list (it's legal, but likely to be
2253 * an effect of screwup). In case of anything not legal, we are definitely
2254 * having an effect of screwup, so just fail and let the caller warn.
2256 static struct expression *handle_scalar(struct expression *e, int nested)
2258 struct expression *v = NULL, *p;
2259 int count = 0;
2261 /* normal case */
2262 if (e->type != EXPR_INITIALIZER)
2263 return e;
2265 FOR_EACH_PTR(e->expr_list, p) {
2266 if (!v)
2267 v = p;
2268 count++;
2269 } END_FOR_EACH_PTR(p);
2270 if (count != 1)
2271 return NULL;
2272 switch(v->type) {
2273 case EXPR_INITIALIZER:
2274 case EXPR_INDEX:
2275 case EXPR_IDENTIFIER:
2276 return NULL;
2277 default:
2278 break;
2280 if (nested)
2281 warning(e->pos, "braces around scalar initializer");
2282 return v;
2286 * deal with the cases that don't care about subobjects:
2287 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2288 * character array <- string literal, possibly in braces [6.7.8(14)]
2289 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2290 * compound type <- initializer list in braces [6.7.8(16)]
2291 * The last one punts to handle_list_initializer() which, in turn will call
2292 * us for individual elements of the list.
2294 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2295 * the lack of support of wide char stuff in general.
2297 * One note: we need to take care not to evaluate a string literal until
2298 * we know that we *will* handle it right here. Otherwise we would screw
2299 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2300 * { "string", ...} - we need to preserve that string literal recognizable
2301 * until we dig into the inner struct.
2303 static int handle_simple_initializer(struct expression **ep, int nested,
2304 int class, struct symbol *ctype)
2306 int is_string = is_string_type(ctype);
2307 struct expression *e = *ep, *p;
2308 struct symbol *type;
2310 if (!e)
2311 return 0;
2313 /* scalar */
2314 if (!(class & TYPE_COMPOUND)) {
2315 e = handle_scalar(e, nested);
2316 if (!e)
2317 return 0;
2318 *ep = e;
2319 type = evaluate_expression(e);
2320 if (!e->ctype)
2321 return 1;
2322 compatible_assignment_types(e, ctype, ep, degenerate(e),
2323 "initializer");
2324 return 1;
2328 * sublist; either a string, or we dig in; the latter will deal with
2329 * pathologies, so we don't need anything fancy here.
2331 if (e->type == EXPR_INITIALIZER) {
2332 if (is_string) {
2333 struct expression *v = NULL;
2334 int count = 0;
2336 FOR_EACH_PTR(e->expr_list, p) {
2337 if (!v)
2338 v = p;
2339 count++;
2340 } END_FOR_EACH_PTR(p);
2341 if (count == 1 && is_string_literal(&v)) {
2342 *ep = e = v;
2343 goto String;
2346 handle_list_initializer(e, class, ctype);
2347 return 1;
2350 /* string */
2351 if (is_string_literal(&e)) {
2352 /* either we are doing array of char, or we'll have to dig in */
2353 if (is_string) {
2354 *ep = e;
2355 goto String;
2357 return 0;
2359 /* struct or union can be initialized by compatible */
2360 if (class != TYPE_COMPOUND)
2361 return 0;
2362 type = evaluate_expression(e);
2363 if (!type)
2364 return 0;
2365 if (ctype->type == SYM_NODE)
2366 ctype = ctype->ctype.base_type;
2367 if (type->type == SYM_NODE)
2368 type = type->ctype.base_type;
2369 if (ctype == type)
2370 return 1;
2371 return 0;
2373 String:
2374 p = alloc_expression(e->pos, EXPR_STRING);
2375 *p = *e;
2376 type = evaluate_expression(p);
2377 if (ctype->bit_size != -1 &&
2378 ctype->bit_size + bits_in_char < type->bit_size) {
2379 warning(e->pos,
2380 "too long initializer-string for array of char");
2382 *ep = p;
2383 return 1;
2386 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2388 struct symbol *type;
2389 int class = classify_type(ctype, &type);
2390 if (!handle_simple_initializer(ep, 0, class, ctype))
2391 expression_error(*ep, "invalid initializer");
2394 static int get_as(struct symbol *sym)
2396 int as;
2397 unsigned long mod;
2399 if (!sym)
2400 return 0;
2401 as = sym->ctype.as;
2402 mod = sym->ctype.modifiers;
2403 if (sym->type == SYM_NODE) {
2404 sym = sym->ctype.base_type;
2405 as |= sym->ctype.as;
2406 mod |= sym->ctype.modifiers;
2410 * At least for now, allow casting to a "unsigned long".
2411 * That's how we do things like pointer arithmetic and
2412 * store pointers to registers.
2414 if (sym == &ulong_ctype)
2415 return -1;
2417 if (sym && sym->type == SYM_PTR) {
2418 sym = get_base_type(sym);
2419 as |= sym->ctype.as;
2420 mod |= sym->ctype.modifiers;
2422 if (mod & MOD_FORCE)
2423 return -1;
2424 return as;
2427 static void cast_to_as(struct expression *e, int as)
2429 struct expression *v = e->cast_expression;
2430 struct symbol *type = v->ctype;
2432 if (!Wcast_to_address_space)
2433 return;
2435 if (v->type != EXPR_VALUE || v->value)
2436 goto out;
2438 /* cast from constant 0 to pointer is OK */
2439 if (is_int_type(type))
2440 return;
2442 if (type->type == SYM_NODE)
2443 type = type->ctype.base_type;
2445 if (type->type == SYM_PTR && type->ctype.base_type == &void_ctype)
2446 return;
2448 out:
2449 warning(e->pos, "cast adds address space to expression (<asn:%d>)", as);
2452 static struct symbol *evaluate_cast(struct expression *expr)
2454 struct expression *target = expr->cast_expression;
2455 struct symbol *ctype = examine_symbol_type(expr->cast_type);
2456 struct symbol *t1, *t2;
2457 int class1, class2;
2458 int as1, as2;
2460 if (!target)
2461 return NULL;
2463 expr->ctype = ctype;
2464 expr->cast_type = ctype;
2467 * Special case: a cast can be followed by an
2468 * initializer, in which case we need to pass
2469 * the type value down to that initializer rather
2470 * than trying to evaluate it as an expression
2472 * A more complex case is when the initializer is
2473 * dereferenced as part of a post-fix expression.
2474 * We need to produce an expression that can be dereferenced.
2476 if (target->type == EXPR_INITIALIZER) {
2477 struct symbol *sym = expr->cast_type;
2478 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2480 sym->initializer = expr->cast_expression;
2481 evaluate_symbol(sym);
2483 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2484 addr->symbol = sym;
2486 expr->type = EXPR_PREOP;
2487 expr->op = '*';
2488 expr->unop = addr;
2489 expr->ctype = sym;
2491 return sym;
2494 evaluate_expression(target);
2495 degenerate(target);
2497 class1 = classify_type(ctype, &t1);
2499 * You can always throw a value away by casting to
2500 * "void" - that's an implicit "force". Note that
2501 * the same is _not_ true of "void *".
2503 if (t1 == &void_ctype)
2504 goto out;
2506 if (class1 & TYPE_COMPOUND)
2507 warning(expr->pos, "cast to non-scalar");
2509 t2 = target->ctype;
2510 if (!t2) {
2511 expression_error(expr, "cast from unknown type");
2512 goto out;
2514 class2 = classify_type(t2, &t2);
2516 if (class2 & TYPE_COMPOUND)
2517 warning(expr->pos, "cast from non-scalar");
2519 /* allowed cast unfouls */
2520 if (class2 & TYPE_FOULED)
2521 t2 = t2->ctype.base_type;
2523 if (!(ctype->ctype.modifiers & MOD_FORCE) && t1 != t2) {
2524 if (class1 & TYPE_RESTRICT)
2525 warning(expr->pos, "cast to restricted type");
2526 if (class2 & TYPE_RESTRICT)
2527 warning(expr->pos, "cast from restricted type");
2530 as1 = get_as(ctype);
2531 as2 = get_as(target->ctype);
2532 if (!as1 && as2 > 0)
2533 warning(expr->pos, "cast removes address space of expression");
2534 if (as1 > 0 && as2 > 0 && as1 != as2)
2535 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2536 if (as1 > 0 && !as2)
2537 cast_to_as(expr, as1);
2540 * Casts of constant values are special: they
2541 * can be NULL, and thus need to be simplified
2542 * early.
2544 if (target->type == EXPR_VALUE)
2545 cast_value(expr, ctype, target, target->ctype);
2547 out:
2548 return ctype;
2552 * Evaluate a call expression with a symbol. This
2553 * should expand inline functions, and evaluate
2554 * builtins.
2556 static int evaluate_symbol_call(struct expression *expr)
2558 struct expression *fn = expr->fn;
2559 struct symbol *ctype = fn->ctype;
2561 if (fn->type != EXPR_PREOP)
2562 return 0;
2564 if (ctype->op && ctype->op->evaluate)
2565 return ctype->op->evaluate(expr);
2567 if (ctype->ctype.modifiers & MOD_INLINE) {
2568 int ret;
2569 struct symbol *curr = current_fn;
2570 current_fn = ctype->ctype.base_type;
2572 ret = inline_function(expr, ctype);
2574 /* restore the old function */
2575 current_fn = curr;
2576 return ret;
2579 return 0;
2582 static struct symbol *evaluate_call(struct expression *expr)
2584 int args, fnargs;
2585 struct symbol *ctype, *sym;
2586 struct expression *fn = expr->fn;
2587 struct expression_list *arglist = expr->args;
2589 if (!evaluate_expression(fn))
2590 return NULL;
2591 sym = ctype = fn->ctype;
2592 if (ctype->type == SYM_NODE)
2593 ctype = ctype->ctype.base_type;
2594 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
2595 ctype = get_base_type(ctype);
2597 examine_fn_arguments(ctype);
2598 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
2599 sym->op && sym->op->args) {
2600 if (!sym->op->args(expr))
2601 return NULL;
2602 } else {
2603 if (!evaluate_arguments(sym, ctype, arglist))
2604 return NULL;
2605 if (ctype->type != SYM_FN) {
2606 expression_error(expr, "not a function %s",
2607 show_ident(sym->ident));
2608 return NULL;
2610 args = expression_list_size(expr->args);
2611 fnargs = symbol_list_size(ctype->arguments);
2612 if (args < fnargs)
2613 expression_error(expr,
2614 "not enough arguments for function %s",
2615 show_ident(sym->ident));
2616 if (args > fnargs && !ctype->variadic)
2617 expression_error(expr,
2618 "too many arguments for function %s",
2619 show_ident(sym->ident));
2621 if (sym->type == SYM_NODE) {
2622 if (evaluate_symbol_call(expr))
2623 return expr->ctype;
2625 expr->ctype = ctype->ctype.base_type;
2626 return expr->ctype;
2629 struct symbol *evaluate_expression(struct expression *expr)
2631 if (!expr)
2632 return NULL;
2633 if (expr->ctype)
2634 return expr->ctype;
2636 switch (expr->type) {
2637 case EXPR_VALUE:
2638 case EXPR_FVALUE:
2639 expression_error(expr, "value expression without a type");
2640 return NULL;
2641 case EXPR_STRING:
2642 return evaluate_string(expr);
2643 case EXPR_SYMBOL:
2644 return evaluate_symbol_expression(expr);
2645 case EXPR_BINOP:
2646 if (!evaluate_expression(expr->left))
2647 return NULL;
2648 if (!evaluate_expression(expr->right))
2649 return NULL;
2650 return evaluate_binop(expr);
2651 case EXPR_LOGICAL:
2652 return evaluate_logical(expr);
2653 case EXPR_COMMA:
2654 evaluate_expression(expr->left);
2655 if (!evaluate_expression(expr->right))
2656 return NULL;
2657 return evaluate_comma(expr);
2658 case EXPR_COMPARE:
2659 if (!evaluate_expression(expr->left))
2660 return NULL;
2661 if (!evaluate_expression(expr->right))
2662 return NULL;
2663 return evaluate_compare(expr);
2664 case EXPR_ASSIGNMENT:
2665 if (!evaluate_expression(expr->left))
2666 return NULL;
2667 if (!evaluate_expression(expr->right))
2668 return NULL;
2669 return evaluate_assignment(expr);
2670 case EXPR_PREOP:
2671 if (!evaluate_expression(expr->unop))
2672 return NULL;
2673 return evaluate_preop(expr);
2674 case EXPR_POSTOP:
2675 if (!evaluate_expression(expr->unop))
2676 return NULL;
2677 return evaluate_postop(expr);
2678 case EXPR_CAST:
2679 case EXPR_IMPLIED_CAST:
2680 return evaluate_cast(expr);
2681 case EXPR_SIZEOF:
2682 return evaluate_sizeof(expr);
2683 case EXPR_PTRSIZEOF:
2684 return evaluate_ptrsizeof(expr);
2685 case EXPR_ALIGNOF:
2686 return evaluate_alignof(expr);
2687 case EXPR_DEREF:
2688 return evaluate_member_dereference(expr);
2689 case EXPR_CALL:
2690 return evaluate_call(expr);
2691 case EXPR_SELECT:
2692 case EXPR_CONDITIONAL:
2693 return evaluate_conditional_expression(expr);
2694 case EXPR_STATEMENT:
2695 expr->ctype = evaluate_statement(expr->statement);
2696 return expr->ctype;
2698 case EXPR_LABEL:
2699 expr->ctype = &ptr_ctype;
2700 return &ptr_ctype;
2702 case EXPR_TYPE:
2703 /* Evaluate the type of the symbol .. */
2704 evaluate_symbol(expr->symbol);
2705 /* .. but the type of the _expression_ is a "type" */
2706 expr->ctype = &type_ctype;
2707 return &type_ctype;
2709 /* These can not exist as stand-alone expressions */
2710 case EXPR_INITIALIZER:
2711 case EXPR_IDENTIFIER:
2712 case EXPR_INDEX:
2713 case EXPR_POS:
2714 expression_error(expr, "internal front-end error: initializer in expression");
2715 return NULL;
2716 case EXPR_SLICE:
2717 expression_error(expr, "internal front-end error: SLICE re-evaluated");
2718 return NULL;
2720 return NULL;
2723 static void check_duplicates(struct symbol *sym)
2725 int declared = 0;
2726 struct symbol *next = sym;
2728 while ((next = next->same_symbol) != NULL) {
2729 const char *typediff;
2730 evaluate_symbol(next);
2731 declared++;
2732 typediff = type_difference(sym, next, 0, 0);
2733 if (typediff) {
2734 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2735 show_ident(sym->ident),
2736 stream_name(next->pos.stream), next->pos.line, typediff);
2737 return;
2740 if (!declared) {
2741 unsigned long mod = sym->ctype.modifiers;
2742 if (mod & (MOD_STATIC | MOD_REGISTER))
2743 return;
2744 if (!(mod & MOD_TOPLEVEL))
2745 return;
2746 if (!Wdecl)
2747 return;
2748 if (sym->ident == &main_ident)
2749 return;
2750 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
2754 static struct symbol *evaluate_symbol(struct symbol *sym)
2756 struct symbol *base_type;
2758 if (!sym)
2759 return sym;
2760 if (sym->evaluated)
2761 return sym;
2762 sym->evaluated = 1;
2764 sym = examine_symbol_type(sym);
2765 base_type = get_base_type(sym);
2766 if (!base_type)
2767 return NULL;
2769 /* Evaluate the initializers */
2770 if (sym->initializer)
2771 evaluate_initializer(sym, &sym->initializer);
2773 /* And finally, evaluate the body of the symbol too */
2774 if (base_type->type == SYM_FN) {
2775 struct symbol *curr = current_fn;
2777 current_fn = base_type;
2779 examine_fn_arguments(base_type);
2780 if (!base_type->stmt && base_type->inline_stmt)
2781 uninline(sym);
2782 if (base_type->stmt)
2783 evaluate_statement(base_type->stmt);
2785 current_fn = curr;
2788 return base_type;
2791 void evaluate_symbol_list(struct symbol_list *list)
2793 struct symbol *sym;
2795 FOR_EACH_PTR(list, sym) {
2796 evaluate_symbol(sym);
2797 check_duplicates(sym);
2798 } END_FOR_EACH_PTR(sym);
2801 static struct symbol *evaluate_return_expression(struct statement *stmt)
2803 struct expression *expr = stmt->expression;
2804 struct symbol *ctype, *fntype;
2806 evaluate_expression(expr);
2807 ctype = degenerate(expr);
2808 fntype = current_fn->ctype.base_type;
2809 if (!fntype || fntype == &void_ctype) {
2810 if (expr && ctype != &void_ctype)
2811 expression_error(expr, "return expression in %s function", fntype?"void":"typeless");
2812 return NULL;
2815 if (!expr) {
2816 sparse_error(stmt->pos, "return with no return value");
2817 return NULL;
2819 if (!ctype)
2820 return NULL;
2821 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression");
2822 return NULL;
2825 static void evaluate_if_statement(struct statement *stmt)
2827 if (!stmt->if_conditional)
2828 return;
2830 evaluate_conditional(stmt->if_conditional, 0);
2831 evaluate_statement(stmt->if_true);
2832 evaluate_statement(stmt->if_false);
2835 static void evaluate_iterator(struct statement *stmt)
2837 evaluate_conditional(stmt->iterator_pre_condition, 1);
2838 evaluate_conditional(stmt->iterator_post_condition,1);
2839 evaluate_statement(stmt->iterator_pre_statement);
2840 evaluate_statement(stmt->iterator_statement);
2841 evaluate_statement(stmt->iterator_post_statement);
2844 static void verify_output_constraint(struct expression *expr, const char *constraint)
2846 switch (*constraint) {
2847 case '=': /* Assignment */
2848 case '+': /* Update */
2849 break;
2850 default:
2851 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
2855 static void verify_input_constraint(struct expression *expr, const char *constraint)
2857 switch (*constraint) {
2858 case '=': /* Assignment */
2859 case '+': /* Update */
2860 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
2864 static void evaluate_asm_statement(struct statement *stmt)
2866 struct expression *expr;
2867 int state;
2869 expr = stmt->asm_string;
2870 if (!expr || expr->type != EXPR_STRING) {
2871 sparse_error(stmt->pos, "need constant string for inline asm");
2872 return;
2875 state = 0;
2876 FOR_EACH_PTR(stmt->asm_outputs, expr) {
2877 struct ident *ident;
2879 switch (state) {
2880 case 0: /* Identifier */
2881 state = 1;
2882 ident = (struct ident *)expr;
2883 continue;
2885 case 1: /* Constraint */
2886 state = 2;
2887 if (!expr || expr->type != EXPR_STRING) {
2888 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
2889 *THIS_ADDRESS(expr) = NULL;
2890 continue;
2892 verify_output_constraint(expr, expr->string->data);
2893 continue;
2895 case 2: /* Expression */
2896 state = 0;
2897 if (!evaluate_expression(expr))
2898 return;
2899 if (!lvalue_expression(expr))
2900 warning(expr->pos, "asm output is not an lvalue");
2901 evaluate_assign_to(expr, expr->ctype);
2902 continue;
2904 } END_FOR_EACH_PTR(expr);
2906 state = 0;
2907 FOR_EACH_PTR(stmt->asm_inputs, expr) {
2908 struct ident *ident;
2910 switch (state) {
2911 case 0: /* Identifier */
2912 state = 1;
2913 ident = (struct ident *)expr;
2914 continue;
2916 case 1: /* Constraint */
2917 state = 2;
2918 if (!expr || expr->type != EXPR_STRING) {
2919 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
2920 *THIS_ADDRESS(expr) = NULL;
2921 continue;
2923 verify_input_constraint(expr, expr->string->data);
2924 continue;
2926 case 2: /* Expression */
2927 state = 0;
2928 if (!evaluate_expression(expr))
2929 return;
2930 continue;
2932 } END_FOR_EACH_PTR(expr);
2934 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
2935 if (!expr) {
2936 sparse_error(stmt->pos, "bad asm output");
2937 return;
2939 if (expr->type == EXPR_STRING)
2940 continue;
2941 expression_error(expr, "asm clobber is not a string");
2942 } END_FOR_EACH_PTR(expr);
2945 static void evaluate_case_statement(struct statement *stmt)
2947 evaluate_expression(stmt->case_expression);
2948 evaluate_expression(stmt->case_to);
2949 evaluate_statement(stmt->case_statement);
2952 static void check_case_type(struct expression *switch_expr,
2953 struct expression *case_expr,
2954 struct expression **enumcase)
2956 struct symbol *switch_type, *case_type;
2957 int sclass, cclass;
2959 if (!case_expr)
2960 return;
2962 switch_type = switch_expr->ctype;
2963 case_type = evaluate_expression(case_expr);
2965 if (!switch_type || !case_type)
2966 goto Bad;
2967 if (enumcase) {
2968 if (*enumcase)
2969 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
2970 else if (is_enum_type(case_type))
2971 *enumcase = case_expr;
2974 sclass = classify_type(switch_type, &switch_type);
2975 cclass = classify_type(case_type, &case_type);
2977 /* both should be arithmetic */
2978 if (!(sclass & cclass & TYPE_NUM))
2979 goto Bad;
2981 /* neither should be floating */
2982 if ((sclass | cclass) & TYPE_FLOAT)
2983 goto Bad;
2985 /* if neither is restricted, we are OK */
2986 if (!((sclass | cclass) & TYPE_RESTRICT))
2987 return;
2989 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
2990 cclass, sclass, case_type, switch_type))
2991 warning(case_expr->pos, "restricted degrades to integer");
2993 return;
2995 Bad:
2996 expression_error(case_expr, "incompatible types for 'case' statement");
2999 static void evaluate_switch_statement(struct statement *stmt)
3001 struct symbol *sym;
3002 struct expression *enumcase = NULL;
3003 struct expression **enumcase_holder = &enumcase;
3004 struct expression *sel = stmt->switch_expression;
3006 evaluate_expression(sel);
3007 evaluate_statement(stmt->switch_statement);
3008 if (!sel)
3009 return;
3010 if (sel->ctype && is_enum_type(sel->ctype))
3011 enumcase_holder = NULL; /* Only check cases against switch */
3013 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
3014 struct statement *case_stmt = sym->stmt;
3015 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
3016 check_case_type(sel, case_stmt->case_to, enumcase_holder);
3017 } END_FOR_EACH_PTR(sym);
3020 struct symbol *evaluate_statement(struct statement *stmt)
3022 if (!stmt)
3023 return NULL;
3025 switch (stmt->type) {
3026 case STMT_DECLARATION: {
3027 struct symbol *s;
3028 FOR_EACH_PTR(stmt->declaration, s) {
3029 evaluate_symbol(s);
3030 } END_FOR_EACH_PTR(s);
3031 return NULL;
3034 case STMT_RETURN:
3035 return evaluate_return_expression(stmt);
3037 case STMT_EXPRESSION:
3038 if (!evaluate_expression(stmt->expression))
3039 return NULL;
3040 return degenerate(stmt->expression);
3042 case STMT_COMPOUND: {
3043 struct statement *s;
3044 struct symbol *type = NULL;
3046 /* Evaluate the return symbol in the compound statement */
3047 evaluate_symbol(stmt->ret);
3050 * Then, evaluate each statement, making the type of the
3051 * compound statement be the type of the last statement
3053 type = evaluate_statement(stmt->args);
3054 FOR_EACH_PTR(stmt->stmts, s) {
3055 type = evaluate_statement(s);
3056 } END_FOR_EACH_PTR(s);
3057 if (!type)
3058 type = &void_ctype;
3059 return type;
3061 case STMT_IF:
3062 evaluate_if_statement(stmt);
3063 return NULL;
3064 case STMT_ITERATOR:
3065 evaluate_iterator(stmt);
3066 return NULL;
3067 case STMT_SWITCH:
3068 evaluate_switch_statement(stmt);
3069 return NULL;
3070 case STMT_CASE:
3071 evaluate_case_statement(stmt);
3072 return NULL;
3073 case STMT_LABEL:
3074 return evaluate_statement(stmt->label_statement);
3075 case STMT_GOTO:
3076 evaluate_expression(stmt->goto_expression);
3077 return NULL;
3078 case STMT_NONE:
3079 break;
3080 case STMT_ASM:
3081 evaluate_asm_statement(stmt);
3082 return NULL;
3083 case STMT_CONTEXT:
3084 evaluate_expression(stmt->expression);
3085 return NULL;
3086 case STMT_RANGE:
3087 evaluate_expression(stmt->range_expression);
3088 evaluate_expression(stmt->range_low);
3089 evaluate_expression(stmt->range_high);
3090 return NULL;
3092 return NULL;