[PATCH] fix default argument promotion
[smatch.git] / evaluate.c
blob1d34cdf3c8eaf2a1eb50cedf39bdbb35cac3d616
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 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
382 static inline int is_string_type(struct symbol *type)
384 if (type->type == SYM_NODE)
385 type = type->ctype.base_type;
386 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
389 static struct symbol *bad_expr_type(struct expression *expr)
391 sparse_error(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
392 switch (expr->type) {
393 case EXPR_BINOP:
394 case EXPR_COMPARE:
395 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
396 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
397 break;
398 case EXPR_PREOP:
399 case EXPR_POSTOP:
400 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
401 break;
402 default:
403 break;
406 return expr->ctype = &bad_ctype;
409 static int restricted_value(struct expression *v, struct symbol *type)
411 if (v->type != EXPR_VALUE)
412 return 1;
413 if (v->value != 0)
414 return 1;
415 return 0;
418 static int restricted_binop(int op, struct symbol *type)
420 switch (op) {
421 case '&':
422 case '=':
423 case SPECIAL_AND_ASSIGN:
424 case SPECIAL_OR_ASSIGN:
425 case SPECIAL_XOR_ASSIGN:
426 return 1; /* unfoul */
427 case '|':
428 case '^':
429 case '?':
430 return 2; /* keep fouled */
431 case SPECIAL_EQUAL:
432 case SPECIAL_NOTEQUAL:
433 return 3; /* warn if fouled */
434 default:
435 return 0; /* warn */
439 static int restricted_unop(int op, struct symbol **type)
441 if (op == '~') {
442 if ((*type)->bit_size < bits_in_int)
443 *type = befoul(*type);
444 return 0;
445 } if (op == '+')
446 return 0;
447 return 1;
450 static struct symbol *restricted_binop_type(int op,
451 struct expression *left,
452 struct expression *right,
453 int lclass, int rclass,
454 struct symbol *ltype,
455 struct symbol *rtype)
457 struct symbol *ctype = NULL;
458 if (lclass & TYPE_RESTRICT) {
459 if (rclass & TYPE_RESTRICT) {
460 if (ltype == rtype) {
461 ctype = ltype;
462 } else if (lclass & TYPE_FOULED) {
463 if (ltype->ctype.base_type == rtype)
464 ctype = ltype;
465 } else if (rclass & TYPE_FOULED) {
466 if (rtype->ctype.base_type == ltype)
467 ctype = rtype;
469 } else {
470 if (!restricted_value(right, ltype))
471 ctype = ltype;
473 } else if (!restricted_value(left, rtype))
474 ctype = rtype;
476 if (ctype) {
477 switch (restricted_binop(op, ctype)) {
478 case 1:
479 if ((lclass ^ rclass) & TYPE_FOULED)
480 ctype = ctype->ctype.base_type;
481 break;
482 case 3:
483 if (!(lclass & rclass & TYPE_FOULED))
484 break;
485 case 0:
486 ctype = NULL;
487 default:
488 break;
492 return ctype;
495 static inline void unrestrict(struct expression *expr,
496 int class, struct symbol **ctype)
498 if (class & TYPE_RESTRICT) {
499 warning(expr->pos, "restricted degrades to integer");
500 if (class & TYPE_FOULED) /* unfoul it first */
501 *ctype = (*ctype)->ctype.base_type;
502 *ctype = (*ctype)->ctype.base_type; /* get to arithmetic type */
506 static struct symbol *usual_conversions(int op,
507 struct expression *left,
508 struct expression *right,
509 int lclass, int rclass,
510 struct symbol *ltype,
511 struct symbol *rtype)
513 struct symbol *ctype;
515 warn_for_different_enum_types(right->pos, left->ctype, right->ctype);
517 if ((lclass | rclass) & TYPE_RESTRICT)
518 goto Restr;
520 Normal:
521 if (!(lclass & TYPE_FLOAT)) {
522 if (!(rclass & TYPE_FLOAT))
523 return bigger_int_type(ltype, rtype);
524 else
525 return rtype;
526 } else if (rclass & TYPE_FLOAT) {
527 unsigned long lmod = ltype->ctype.modifiers;
528 unsigned long rmod = rtype->ctype.modifiers;
529 if (rmod & ~lmod & (MOD_LONG | MOD_LONGLONG))
530 return rtype;
531 else
532 return ltype;
533 } else
534 return ltype;
536 Restr:
537 ctype = restricted_binop_type(op, left, right,
538 lclass, rclass, ltype, rtype);
539 if (ctype)
540 return ctype;
542 unrestrict(left, lclass, &ltype);
543 unrestrict(right, rclass, &rtype);
545 goto Normal;
548 static inline int lvalue_expression(struct expression *expr)
550 return expr->type == EXPR_PREOP && expr->op == '*';
553 static int ptr_object_size(struct symbol *ptr_type)
555 if (ptr_type->type == SYM_NODE)
556 ptr_type = ptr_type->ctype.base_type;
557 if (ptr_type->type == SYM_PTR)
558 ptr_type = get_base_type(ptr_type);
559 return ptr_type->bit_size;
562 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *ctype, struct symbol *itype)
564 struct expression *index = expr->right;
565 int multiply;
566 int bit_size;
568 examine_symbol_type(ctype);
570 if (!ctype->ctype.base_type) {
571 expression_error(expr, "missing type information");
572 return NULL;
575 /* Get the size of whatever the pointer points to */
576 bit_size = ptr_object_size(ctype);
577 multiply = bit_size >> 3;
579 expr->ctype = ctype;
581 if (multiply == 1 && itype->bit_size >= bits_in_pointer)
582 return ctype;
584 if (index->type == EXPR_VALUE) {
585 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
586 unsigned long long v = index->value, mask;
587 mask = 1ULL << (itype->bit_size - 1);
588 if (v & mask)
589 v |= -mask;
590 else
591 v &= mask - 1;
592 v *= multiply;
593 mask = 1ULL << (bits_in_pointer - 1);
594 v &= mask | (mask - 1);
595 val->value = v;
596 val->ctype = ssize_t_ctype;
597 expr->right = val;
598 return ctype;
601 if (itype->bit_size < bits_in_pointer)
602 index = cast_to(index, ssize_t_ctype);
604 if (multiply > 1) {
605 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
606 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
608 val->ctype = ssize_t_ctype;
609 val->value = multiply;
611 mul->op = '*';
612 mul->ctype = ssize_t_ctype;
613 mul->left = index;
614 mul->right = val;
615 index = mul;
618 expr->right = index;
619 return ctype;
622 const char * type_difference(struct symbol *target, struct symbol *source,
623 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
625 for (;;) {
626 unsigned long mod1, mod2, diff;
627 unsigned long as1, as2;
628 int type1, type2;
629 struct symbol *base1, *base2;
631 if (target == source)
632 break;
633 if (!target || !source)
634 return "different types";
636 * Peel of per-node information.
637 * FIXME! Check alignment and context too here!
639 mod1 = target->ctype.modifiers;
640 as1 = target->ctype.as;
641 mod2 = source->ctype.modifiers;
642 as2 = source->ctype.as;
643 if (target->type == SYM_NODE) {
644 target = target->ctype.base_type;
645 if (!target)
646 return "bad types";
647 if (target->type == SYM_PTR) {
648 mod1 = 0;
649 as1 = 0;
651 mod1 |= target->ctype.modifiers;
652 as1 |= target->ctype.as;
654 if (source->type == SYM_NODE) {
655 source = source->ctype.base_type;
656 if (!source)
657 return "bad types";
658 if (source->type == SYM_PTR) {
659 mod2 = 0;
660 as2 = 0;
662 mod2 |= source->ctype.modifiers;
663 as2 |= source->ctype.as;
665 if (target->type == SYM_ENUM) {
666 target = target->ctype.base_type;
667 if (!target)
668 return "bad types";
670 if (source->type == SYM_ENUM) {
671 source = source->ctype.base_type;
672 if (!source)
673 return "bad types";
676 if (target == source)
677 break;
678 if (!target || !source)
679 return "different types";
681 type1 = target->type;
682 base1 = target->ctype.base_type;
684 type2 = source->type;
685 base2 = source->ctype.base_type;
688 * Pointers to functions compare as the function itself
690 if (type1 == SYM_PTR && base1) {
691 base1 = examine_symbol_type(base1);
692 switch (base1->type) {
693 case SYM_FN:
694 type1 = SYM_FN;
695 target = base1;
696 base1 = base1->ctype.base_type;
697 default:
698 /* nothing */;
701 if (type2 == SYM_PTR && base2) {
702 base2 = examine_symbol_type(base2);
703 switch (base2->type) {
704 case SYM_FN:
705 type2 = SYM_FN;
706 source = base2;
707 base2 = base2->ctype.base_type;
708 default:
709 /* nothing */;
713 /* Arrays degenerate to pointers for type comparisons */
714 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
715 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
717 if (type1 != type2 || type1 == SYM_RESTRICT)
718 return "different base types";
720 /* Must be same address space to be comparable */
721 if (Waddress_space && as1 != as2)
722 return "different address spaces";
724 /* Ignore differences in storage types or addressability */
725 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
726 diff &= (mod1 & ~target_mod_ignore) | (mod2 & ~source_mod_ignore);
727 if (diff) {
728 if (diff & MOD_SIZE)
729 return "different type sizes";
730 if (diff & ~MOD_SIGNEDNESS)
731 return "different modifiers";
733 /* Differs in signedness only.. */
734 if (Wtypesign) {
736 * Warn if both are explicitly signed ("unsigned" is obviously
737 * always explicit, and since we know one of them has to be
738 * unsigned, we check if the signed one was explicit).
740 if ((mod1 | mod2) & MOD_EXPLICITLY_SIGNED)
741 return "different explicit signedness";
744 * "char" matches both "unsigned char" and "signed char",
745 * so if the explicit test didn't trigger, then we should
746 * not warn about a char.
748 if (!(mod1 & MOD_CHAR))
749 return "different signedness";
753 if (type1 == SYM_FN) {
754 int i;
755 struct symbol *arg1, *arg2;
756 if (base1->variadic != base2->variadic)
757 return "incompatible variadic arguments";
758 PREPARE_PTR_LIST(target->arguments, arg1);
759 PREPARE_PTR_LIST(source->arguments, arg2);
760 i = 1;
761 for (;;) {
762 const char *diffstr;
763 diffstr = type_difference(arg1, arg2, 0, 0);
764 if (diffstr) {
765 static char argdiff[80];
766 sprintf(argdiff, "incompatible argument %d (%s)", i, diffstr);
767 return argdiff;
769 if (!arg1)
770 break;
771 NEXT_PTR_LIST(arg1);
772 NEXT_PTR_LIST(arg2);
773 i++;
775 FINISH_PTR_LIST(arg2);
776 FINISH_PTR_LIST(arg1);
779 target = base1;
780 source = base2;
782 return NULL;
785 static int is_null_ptr(struct expression *expr)
787 if (expr->type != EXPR_VALUE || expr->value)
788 return 0;
789 if (Wnon_pointer_null && !is_ptr_type(expr->ctype))
790 warning(expr->pos, "Using plain integer as NULL pointer");
791 return 1;
795 * Ignore differences in "volatile" and "const"ness when
796 * subtracting pointers
798 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
800 static struct symbol *evaluate_ptr_sub(struct expression *expr)
802 const char *typediff;
803 struct symbol *ctype;
804 struct symbol *ltype, *rtype;
805 struct expression *l = expr->left;
806 struct expression *r = expr->right;
808 ltype = degenerate(l);
809 rtype = degenerate(r);
811 ctype = ltype;
812 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
813 if (typediff)
814 expression_error(expr, "subtraction of different types can't work (%s)", typediff);
815 examine_symbol_type(ctype);
817 /* Figure out the base type we point to */
818 if (ctype->type == SYM_NODE)
819 ctype = ctype->ctype.base_type;
820 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
821 expression_error(expr, "subtraction of functions? Share your drugs");
822 return NULL;
824 ctype = get_base_type(ctype);
826 expr->ctype = ssize_t_ctype;
827 if (ctype->bit_size > bits_in_char) {
828 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
829 struct expression *div = expr;
830 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
831 unsigned long value = ctype->bit_size >> 3;
833 val->ctype = size_t_ctype;
834 val->value = value;
836 if (value & (value-1)) {
837 if (Wptr_subtraction_blows)
838 warning(expr->pos, "potentially expensive pointer subtraction");
841 sub->op = '-';
842 sub->ctype = ssize_t_ctype;
843 sub->left = l;
844 sub->right = r;
846 div->op = '/';
847 div->left = sub;
848 div->right = val;
851 return ssize_t_ctype;
854 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
856 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
858 struct symbol *ctype;
860 if (!expr)
861 return NULL;
863 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
864 warning(expr->pos, "assignment expression in conditional");
866 ctype = evaluate_expression(expr);
867 if (ctype) {
868 if (is_safe_type(ctype))
869 warning(expr->pos, "testing a 'safe expression'");
872 return ctype;
875 static struct symbol *evaluate_logical(struct expression *expr)
877 if (!evaluate_conditional(expr->left, 0))
878 return NULL;
879 if (!evaluate_conditional(expr->right, 0))
880 return NULL;
882 expr->ctype = &bool_ctype;
883 return &bool_ctype;
886 static struct symbol *evaluate_binop(struct expression *expr)
888 struct symbol *ltype, *rtype, *ctype;
889 int lclass = classify_type(expr->left->ctype, &ltype);
890 int rclass = classify_type(expr->right->ctype, &rtype);
891 int op = expr->op;
893 /* number op number */
894 if (lclass & rclass & TYPE_NUM) {
895 if ((lclass | rclass) & TYPE_FLOAT) {
896 switch (op) {
897 case '+': case '-': case '*': case '/':
898 break;
899 default:
900 return bad_expr_type(expr);
904 if (op == SPECIAL_LEFTSHIFT || op == SPECIAL_RIGHTSHIFT) {
905 // shifts do integer promotions, but that's it.
906 unrestrict(expr->left, lclass, &ltype);
907 unrestrict(expr->right, rclass, &rtype);
908 ctype = ltype = integer_promotion(ltype);
909 rtype = integer_promotion(rtype);
910 } else {
911 // The rest do usual conversions
912 ltype = usual_conversions(op, expr->left, expr->right,
913 lclass, rclass, ltype, rtype);
914 ctype = rtype = ltype;
917 expr->left = cast_to(expr->left, ltype);
918 expr->right = cast_to(expr->right, rtype);
919 expr->ctype = ctype;
920 return ctype;
923 /* pointer (+|-) integer */
924 if (lclass & TYPE_PTR && is_int(rclass) && (op == '+' || op == '-')) {
925 unrestrict(expr->right, rclass, &rtype);
926 return evaluate_ptr_add(expr, degenerate(expr->left), rtype);
929 /* integer + pointer */
930 if (rclass & TYPE_PTR && is_int(lclass) && op == '+') {
931 struct expression *index = expr->left;
932 unrestrict(index, lclass, &ltype);
933 expr->left = expr->right;
934 expr->right = index;
935 return evaluate_ptr_add(expr, degenerate(expr->left), ltype);
938 /* pointer - pointer */
939 if (lclass & rclass & TYPE_PTR && expr->op == '-')
940 return evaluate_ptr_sub(expr);
942 return bad_expr_type(expr);
945 static struct symbol *evaluate_comma(struct expression *expr)
947 expr->ctype = expr->right->ctype;
948 return expr->ctype;
951 static int modify_for_unsigned(int op)
953 if (op == '<')
954 op = SPECIAL_UNSIGNED_LT;
955 else if (op == '>')
956 op = SPECIAL_UNSIGNED_GT;
957 else if (op == SPECIAL_LTE)
958 op = SPECIAL_UNSIGNED_LTE;
959 else if (op == SPECIAL_GTE)
960 op = SPECIAL_UNSIGNED_GTE;
961 return op;
964 static struct symbol *evaluate_compare(struct expression *expr)
966 struct expression *left = expr->left, *right = expr->right;
967 struct symbol *ltype = left->ctype, *rtype = right->ctype;
968 struct symbol *ctype;
969 int lclass, rclass;
971 /* Type types? */
972 if (is_type_type(ltype) && is_type_type(rtype))
973 goto OK;
975 if (is_safe_type(ltype) || is_safe_type(rtype))
976 warning(expr->pos, "testing a 'safe expression'");
978 lclass = classify_type(ltype, &ltype);
979 rclass = classify_type(rtype, &rtype);
981 /* Pointer types? */
982 if ((lclass | rclass) & TYPE_PTR) {
983 // FIXME! Check the types for compatibility
984 expr->op = modify_for_unsigned(expr->op);
985 goto OK;
988 /* Both should be numbers */
989 if (!(lclass & rclass & TYPE_NUM))
990 return bad_expr_type(expr);
992 ctype = usual_conversions(expr->op, expr->left, expr->right,
993 lclass, rclass, ltype, rtype);
994 expr->left = cast_to(expr->left, ctype);
995 expr->right = cast_to(expr->right, ctype);
996 if (ctype->ctype.modifiers & MOD_UNSIGNED)
997 expr->op = modify_for_unsigned(expr->op);
1000 expr->ctype = &bool_ctype;
1001 return &bool_ctype;
1005 * FIXME!! This should do casts, array degeneration etc..
1007 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
1009 struct symbol *ltype = left->ctype, *rtype = right->ctype;
1011 if (ltype->type == SYM_NODE)
1012 ltype = ltype->ctype.base_type;
1014 if (rtype->type == SYM_NODE)
1015 rtype = rtype->ctype.base_type;
1017 if (ltype->type == SYM_PTR) {
1018 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
1019 return ltype;
1022 if (rtype->type == SYM_PTR) {
1023 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
1024 return rtype;
1026 return NULL;
1030 * NOTE! The degenerate case of "x ? : y", where we don't
1031 * have a true case, this will possibly promote "x" to the
1032 * same type as "y", and thus _change_ the conditional
1033 * test in the expression. But since promotion is "safe"
1034 * for testing, that's OK.
1036 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1038 struct expression **true;
1039 struct symbol *ctype, *ltype, *rtype;
1040 int lclass, rclass;
1041 const char * typediff;
1043 if (!evaluate_conditional(expr->conditional, 0))
1044 return NULL;
1045 if (!evaluate_expression(expr->cond_false))
1046 return NULL;
1048 ctype = degenerate(expr->conditional);
1049 rtype = degenerate(expr->cond_false);
1051 true = &expr->conditional;
1052 ltype = ctype;
1053 if (expr->cond_true) {
1054 if (!evaluate_expression(expr->cond_true))
1055 return NULL;
1056 ltype = degenerate(expr->cond_true);
1057 true = &expr->cond_true;
1060 lclass = classify_type(ltype, &ltype);
1061 rclass = classify_type(rtype, &rtype);
1062 if (lclass & rclass & TYPE_NUM) {
1063 ctype = usual_conversions('?', *true, expr->cond_false,
1064 lclass, rclass, ltype, rtype);
1065 *true = cast_to(*true, ctype);
1066 expr->cond_false = cast_to(expr->cond_false, ctype);
1067 goto out;
1069 ctype = compatible_ptr_type(*true, expr->cond_false);
1070 if (ctype)
1071 goto out;
1072 ctype = ltype;
1073 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
1074 if (!typediff)
1075 goto out;
1076 expression_error(expr, "incompatible types in conditional expression (%s)", typediff);
1077 return NULL;
1079 out:
1080 expr->ctype = ctype;
1081 return ctype;
1084 /* FP assignments can not do modulo or bit operations */
1085 static int compatible_float_op(int op)
1087 return op == SPECIAL_ADD_ASSIGN ||
1088 op == SPECIAL_SUB_ASSIGN ||
1089 op == SPECIAL_MUL_ASSIGN ||
1090 op == SPECIAL_DIV_ASSIGN;
1093 static int evaluate_assign_op(struct expression *expr)
1095 struct symbol *target = expr->left->ctype;
1096 struct symbol *source = expr->right->ctype;
1097 struct symbol *t, *s;
1098 int tclass = classify_type(target, &t);
1099 int sclass = classify_type(source, &s);
1100 int op = expr->op;
1102 if (tclass & sclass & TYPE_NUM) {
1103 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1104 expression_error(expr, "invalid assignment");
1105 return 0;
1107 if (tclass & TYPE_RESTRICT) {
1108 if (!restricted_binop(op, t)) {
1109 expression_error(expr, "bad restricted assignment");
1110 return 0;
1112 /* allowed assignments unfoul */
1113 if (sclass & TYPE_FOULED && s->ctype.base_type == t)
1114 goto Cast;
1115 if (!restricted_value(expr->right, t))
1116 return 1;
1117 } else if (!(sclass & TYPE_RESTRICT))
1118 goto Cast;
1119 /* source and target would better be identical restricted */
1120 if (t == s)
1121 return 1;
1122 warning(expr->pos, "invalid restricted assignment");
1123 expr->right = cast_to(expr->right, target);
1124 return 0;
1126 if (tclass & TYPE_PTR && is_int(sclass)) {
1127 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1128 unrestrict(expr->right, sclass, &s);
1129 evaluate_ptr_add(expr, target, s);
1130 return 1;
1132 expression_error(expr, "invalid pointer assignment");
1133 return 0;
1136 expression_error(expr, "invalid assignment");
1137 return 0;
1139 Cast:
1140 expr->right = cast_to(expr->right, target);
1141 return 1;
1144 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1145 struct expression **rp, struct symbol *source, const char *where)
1147 const char *typediff;
1148 struct symbol *t, *s;
1149 int target_as;
1150 int tclass = classify_type(target, &t);
1151 int sclass = classify_type(source, &s);
1153 if (tclass & sclass & TYPE_NUM) {
1154 if (tclass & TYPE_RESTRICT) {
1155 /* allowed assignments unfoul */
1156 if (sclass & TYPE_FOULED && s->ctype.base_type == t)
1157 goto Cast;
1158 if (!restricted_value(*rp, target))
1159 return 1;
1160 } else if (!(sclass & TYPE_RESTRICT))
1161 goto Cast;
1164 /* It's OK if the target is more volatile or const than the source */
1165 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
1166 if (!typediff)
1167 return 1;
1169 /* Pointer destination? */
1170 if (tclass & TYPE_PTR) {
1171 struct expression *right = *rp;
1172 int source_as;
1174 // NULL pointer is always OK
1175 if (is_null_ptr(right))
1176 goto Cast;
1178 /* "void *" matches anything as long as the address space is OK */
1179 target_as = t->ctype.as | target->ctype.as;
1180 source_as = s->ctype.as | source->ctype.as;
1181 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
1182 s = get_base_type(s);
1183 t = get_base_type(t);
1184 if (s == &void_ctype || t == &void_ctype)
1185 goto Cast;
1189 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1190 info(expr->pos, " expected %s", show_typename(target));
1191 info(expr->pos, " got %s", show_typename(source));
1192 *rp = cast_to(*rp, target);
1193 return 0;
1194 Cast:
1195 *rp = cast_to(*rp, target);
1196 return 1;
1199 static void mark_assigned(struct expression *expr)
1201 struct symbol *sym;
1203 if (!expr)
1204 return;
1205 switch (expr->type) {
1206 case EXPR_SYMBOL:
1207 sym = expr->symbol;
1208 if (!sym)
1209 return;
1210 if (sym->type != SYM_NODE)
1211 return;
1212 sym->ctype.modifiers |= MOD_ASSIGNED;
1213 return;
1215 case EXPR_BINOP:
1216 mark_assigned(expr->left);
1217 mark_assigned(expr->right);
1218 return;
1219 case EXPR_CAST:
1220 mark_assigned(expr->cast_expression);
1221 return;
1222 case EXPR_SLICE:
1223 mark_assigned(expr->base);
1224 return;
1225 default:
1226 /* Hmm? */
1227 return;
1231 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1233 if (type->ctype.modifiers & MOD_CONST)
1234 expression_error(left, "assignment to const expression");
1236 /* We know left is an lvalue, so it's a "preop-*" */
1237 mark_assigned(left->unop);
1240 static struct symbol *evaluate_assignment(struct expression *expr)
1242 struct expression *left = expr->left, *right = expr->right;
1243 struct expression *where = expr;
1244 struct symbol *ltype, *rtype;
1246 if (!lvalue_expression(left)) {
1247 expression_error(expr, "not an lvalue");
1248 return NULL;
1251 ltype = left->ctype;
1253 if (expr->op != '=') {
1254 if (!evaluate_assign_op(expr))
1255 return NULL;
1256 } else {
1257 rtype = degenerate(right);
1258 if (!compatible_assignment_types(where, ltype, &where->right, rtype, "assignment"))
1259 return NULL;
1262 evaluate_assign_to(left, ltype);
1264 expr->ctype = ltype;
1265 return ltype;
1268 static void examine_fn_arguments(struct symbol *fn)
1270 struct symbol *s;
1272 FOR_EACH_PTR(fn->arguments, s) {
1273 struct symbol *arg = evaluate_symbol(s);
1274 /* Array/function arguments silently degenerate into pointers */
1275 if (arg) {
1276 struct symbol *ptr;
1277 switch(arg->type) {
1278 case SYM_ARRAY:
1279 case SYM_FN:
1280 ptr = alloc_symbol(s->pos, SYM_PTR);
1281 if (arg->type == SYM_ARRAY)
1282 ptr->ctype = arg->ctype;
1283 else
1284 ptr->ctype.base_type = arg;
1285 ptr->ctype.as |= s->ctype.as;
1286 ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1288 s->ctype.base_type = ptr;
1289 s->ctype.as = 0;
1290 s->ctype.modifiers &= ~MOD_PTRINHERIT;
1291 s->bit_size = 0;
1292 s->examined = 0;
1293 examine_symbol_type(s);
1294 break;
1295 default:
1296 /* nothing */
1297 break;
1300 } END_FOR_EACH_PTR(s);
1303 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1305 /* Take the modifiers of the pointer, and apply them to the member */
1306 mod |= sym->ctype.modifiers;
1307 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1308 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1309 *newsym = *sym;
1310 newsym->ctype.as = as;
1311 newsym->ctype.modifiers = mod;
1312 sym = newsym;
1314 return sym;
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 expression_error(expr, "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 expression_error(expr, "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 expression_error(expr, "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 expression_error(op, "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 expression_error(expr, "need lvalue expression for ++/--");
1520 return NULL;
1522 if (is_restricted_type(ctype) && restricted_unop(expr->op, &ctype)) {
1523 expression_error(expr, "bad operation on restricted");
1524 return NULL;
1525 } else if (is_fouled_type(ctype) && restricted_unop(expr->op, &ctype)) {
1526 expression_error(expr, "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 preops 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 expression_error(expr, "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 expression_error(expr, "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 if (ctype->symbol_list)
1717 expression_error(expr, "no member '%s' in %s %.*s",
1718 show_ident(ident), type, namelen, name);
1719 else
1720 expression_error(expr, "using member '%s' in "
1721 "incomplete %s %.*s", show_ident(ident),
1722 type, namelen, name);
1723 return NULL;
1727 * The member needs to take on the address space and modifiers of
1728 * the "parent" type.
1730 member = convert_to_as_mod(member, address_space, mod);
1731 ctype = get_base_type(member);
1733 if (!lvalue_expression(deref)) {
1734 if (deref->type != EXPR_SLICE) {
1735 expr->base = deref;
1736 expr->r_bitpos = 0;
1737 } else {
1738 expr->base = deref->base;
1739 expr->r_bitpos = deref->r_bitpos;
1741 expr->r_bitpos += offset << 3;
1742 expr->type = EXPR_SLICE;
1743 expr->r_nrbits = member->bit_size;
1744 expr->r_bitpos += member->bit_offset;
1745 expr->ctype = member;
1746 return member;
1749 deref = deref->unop;
1750 expr->deref = deref;
1752 add = evaluate_offset(deref, offset);
1753 expr->type = EXPR_PREOP;
1754 expr->op = '*';
1755 expr->unop = add;
1757 expr->ctype = member;
1758 return member;
1761 static int is_promoted(struct expression *expr)
1763 while (1) {
1764 switch (expr->type) {
1765 case EXPR_BINOP:
1766 case EXPR_SELECT:
1767 case EXPR_CONDITIONAL:
1768 return 1;
1769 case EXPR_COMMA:
1770 expr = expr->right;
1771 continue;
1772 case EXPR_PREOP:
1773 switch (expr->op) {
1774 case '(':
1775 expr = expr->unop;
1776 continue;
1777 case '+':
1778 case '-':
1779 case '~':
1780 return 1;
1781 default:
1782 return 0;
1784 default:
1785 return 0;
1791 static struct symbol *evaluate_cast(struct expression *);
1793 static struct symbol *evaluate_type_information(struct expression *expr)
1795 struct symbol *sym = expr->cast_type;
1796 if (!sym) {
1797 sym = evaluate_expression(expr->cast_expression);
1798 if (!sym)
1799 return NULL;
1801 * Expressions of restricted types will possibly get
1802 * promoted - check that here
1804 if (is_restricted_type(sym)) {
1805 if (sym->bit_size < bits_in_int && is_promoted(expr))
1806 sym = &int_ctype;
1807 } else if (is_fouled_type(sym)) {
1808 sym = &int_ctype;
1811 examine_symbol_type(sym);
1812 if (is_bitfield_type(sym)) {
1813 expression_error(expr, "trying to examine bitfield type");
1814 return NULL;
1816 return sym;
1819 static struct symbol *evaluate_sizeof(struct expression *expr)
1821 struct symbol *type;
1822 int size;
1824 type = evaluate_type_information(expr);
1825 if (!type)
1826 return NULL;
1828 size = type->bit_size;
1829 if ((size < 0) || (size & 7))
1830 expression_error(expr, "cannot size expression");
1831 expr->type = EXPR_VALUE;
1832 expr->value = size >> 3;
1833 expr->ctype = size_t_ctype;
1834 return size_t_ctype;
1837 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
1839 struct symbol *type;
1840 int size;
1842 type = evaluate_type_information(expr);
1843 if (!type)
1844 return NULL;
1846 if (type->type == SYM_NODE)
1847 type = type->ctype.base_type;
1848 if (!type)
1849 return NULL;
1850 switch (type->type) {
1851 case SYM_ARRAY:
1852 break;
1853 case SYM_PTR:
1854 type = get_base_type(type);
1855 if (type)
1856 break;
1857 default:
1858 expression_error(expr, "expected pointer expression");
1859 return NULL;
1861 size = type->bit_size;
1862 if (size & 7)
1863 size = 0;
1864 expr->type = EXPR_VALUE;
1865 expr->value = size >> 3;
1866 expr->ctype = size_t_ctype;
1867 return size_t_ctype;
1870 static struct symbol *evaluate_alignof(struct expression *expr)
1872 struct symbol *type;
1874 type = evaluate_type_information(expr);
1875 if (!type)
1876 return NULL;
1878 expr->type = EXPR_VALUE;
1879 expr->value = type->ctype.alignment;
1880 expr->ctype = size_t_ctype;
1881 return size_t_ctype;
1884 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1886 struct expression *expr;
1887 struct symbol_list *argument_types = fn->arguments;
1888 struct symbol *argtype;
1889 int i = 1;
1891 PREPARE_PTR_LIST(argument_types, argtype);
1892 FOR_EACH_PTR (head, expr) {
1893 struct expression **p = THIS_ADDRESS(expr);
1894 struct symbol *ctype, *target;
1895 ctype = evaluate_expression(expr);
1897 if (!ctype)
1898 return 0;
1900 target = argtype;
1901 if (!target) {
1902 struct symbol *type;
1903 int class = classify_type(ctype, &type);
1904 if (is_int(class)) {
1905 *p = cast_to(expr, integer_promotion(type));
1906 } else if (class & TYPE_FLOAT) {
1907 unsigned long mod = type->ctype.modifiers;
1908 if (!(mod & (MOD_LONG|MOD_LONGLONG)))
1909 *p = cast_to(expr, &double_ctype);
1910 } else if (class & TYPE_PTR) {
1911 degenerate(expr);
1913 } else {
1914 static char where[30];
1915 examine_symbol_type(target);
1916 sprintf(where, "argument %d", i);
1917 ctype = degenerate(expr);
1918 compatible_assignment_types(expr, target, p, ctype, where);
1921 i++;
1922 NEXT_PTR_LIST(argtype);
1923 } END_FOR_EACH_PTR(expr);
1924 FINISH_PTR_LIST(argtype);
1925 return 1;
1928 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
1930 struct symbol *sym;
1932 FOR_EACH_PTR(ctype->symbol_list, sym) {
1933 if (sym->ident == ident)
1934 return sym;
1935 } END_FOR_EACH_PTR(sym);
1936 return NULL;
1939 static void convert_index(struct expression *e)
1941 struct expression *child = e->idx_expression;
1942 unsigned from = e->idx_from;
1943 unsigned to = e->idx_to + 1;
1944 e->type = EXPR_POS;
1945 e->init_offset = from * (e->ctype->bit_size>>3);
1946 e->init_nr = to - from;
1947 e->init_expr = child;
1950 static void convert_ident(struct expression *e)
1952 struct expression *child = e->ident_expression;
1953 struct symbol *sym = e->field;
1954 e->type = EXPR_POS;
1955 e->init_offset = sym->offset;
1956 e->init_nr = 1;
1957 e->init_expr = child;
1960 static void convert_designators(struct expression *e)
1962 while (e) {
1963 if (e->type == EXPR_INDEX)
1964 convert_index(e);
1965 else if (e->type == EXPR_IDENTIFIER)
1966 convert_ident(e);
1967 else
1968 break;
1969 e = e->init_expr;
1973 static void excess(struct expression *e, const char *s)
1975 warning(e->pos, "excessive elements in %s initializer", s);
1979 * implicit designator for the first element
1981 static struct expression *first_subobject(struct symbol *ctype, int class,
1982 struct expression **v)
1984 struct expression *e = *v, *new;
1986 if (ctype->type == SYM_NODE)
1987 ctype = ctype->ctype.base_type;
1989 if (class & TYPE_PTR) { /* array */
1990 if (!ctype->bit_size)
1991 return NULL;
1992 new = alloc_expression(e->pos, EXPR_INDEX);
1993 new->idx_expression = e;
1994 new->ctype = ctype->ctype.base_type;
1995 } else {
1996 struct symbol *field, *p;
1997 PREPARE_PTR_LIST(ctype->symbol_list, p);
1998 while (p && !p->ident && is_bitfield_type(p))
1999 NEXT_PTR_LIST(p);
2000 field = p;
2001 FINISH_PTR_LIST(p);
2002 if (!field)
2003 return NULL;
2004 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2005 new->ident_expression = e;
2006 new->field = new->ctype = field;
2008 *v = new;
2009 return new;
2013 * sanity-check explicit designators; return the innermost one or NULL
2014 * in case of error. Assign types.
2016 static struct expression *check_designators(struct expression *e,
2017 struct symbol *ctype)
2019 struct expression *last = NULL;
2020 const char *err;
2021 while (1) {
2022 if (ctype->type == SYM_NODE)
2023 ctype = ctype->ctype.base_type;
2024 if (e->type == EXPR_INDEX) {
2025 struct symbol *type;
2026 if (ctype->type != SYM_ARRAY) {
2027 err = "array index in non-array";
2028 break;
2030 type = ctype->ctype.base_type;
2031 if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2032 unsigned offset = e->idx_to * type->bit_size;
2033 if (offset >= ctype->bit_size) {
2034 err = "index out of bounds in";
2035 break;
2038 e->ctype = ctype = type;
2039 ctype = type;
2040 last = e;
2041 if (!e->idx_expression) {
2042 err = "invalid";
2043 break;
2045 e = e->idx_expression;
2046 } else if (e->type == EXPR_IDENTIFIER) {
2047 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2048 err = "field name not in struct or union";
2049 break;
2051 ctype = find_struct_ident(ctype, e->expr_ident);
2052 if (!ctype) {
2053 err = "unknown field name in";
2054 break;
2056 e->field = e->ctype = ctype;
2057 last = e;
2058 if (!e->ident_expression) {
2059 err = "invalid";
2060 break;
2062 e = e->ident_expression;
2063 } else if (e->type == EXPR_POS) {
2064 err = "internal front-end error: EXPR_POS in";
2065 break;
2066 } else
2067 return last;
2069 expression_error(e, "%s initializer", err);
2070 return NULL;
2074 * choose the next subobject to initialize.
2076 * Get designators for next element, switch old ones to EXPR_POS.
2077 * Return the resulting expression or NULL if we'd run out of subobjects.
2078 * The innermost designator is returned in *v. Designators in old
2079 * are assumed to be already sanity-checked.
2081 static struct expression *next_designators(struct expression *old,
2082 struct symbol *ctype,
2083 struct expression *e, struct expression **v)
2085 struct expression *new = NULL;
2087 if (!old)
2088 return NULL;
2089 if (old->type == EXPR_INDEX) {
2090 struct expression *copy;
2091 unsigned n;
2093 copy = next_designators(old->idx_expression,
2094 old->ctype, e, v);
2095 if (!copy) {
2096 n = old->idx_to + 1;
2097 if (n * old->ctype->bit_size == ctype->bit_size) {
2098 convert_index(old);
2099 return NULL;
2101 copy = e;
2102 *v = new = alloc_expression(e->pos, EXPR_INDEX);
2103 } else {
2104 n = old->idx_to;
2105 new = alloc_expression(e->pos, EXPR_INDEX);
2108 new->idx_from = new->idx_to = n;
2109 new->idx_expression = copy;
2110 new->ctype = old->ctype;
2111 convert_index(old);
2112 } else if (old->type == EXPR_IDENTIFIER) {
2113 struct expression *copy;
2114 struct symbol *field;
2116 copy = next_designators(old->ident_expression,
2117 old->ctype, e, v);
2118 if (!copy) {
2119 field = old->field->next_subobject;
2120 if (!field) {
2121 convert_ident(old);
2122 return NULL;
2124 copy = e;
2125 *v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2126 } else {
2127 field = old->field;
2128 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2131 new->field = field;
2132 new->expr_ident = field->ident;
2133 new->ident_expression = copy;
2134 new->ctype = field;
2135 convert_ident(old);
2137 return new;
2140 static int handle_simple_initializer(struct expression **ep, int nested,
2141 int class, struct symbol *ctype);
2144 * deal with traversing subobjects [6.7.8(17,18,20)]
2146 static void handle_list_initializer(struct expression *expr,
2147 int class, struct symbol *ctype)
2149 struct expression *e, *last = NULL, *top = NULL, *next;
2150 int jumped = 0;
2152 FOR_EACH_PTR(expr->expr_list, e) {
2153 struct expression **v;
2154 struct symbol *type;
2155 int lclass;
2157 if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2158 if (!top) {
2159 top = e;
2160 last = first_subobject(ctype, class, &top);
2161 } else {
2162 last = next_designators(last, ctype, e, &top);
2164 if (!last) {
2165 excess(e, class & TYPE_PTR ? "array" :
2166 "struct or union");
2167 DELETE_CURRENT_PTR(e);
2168 continue;
2170 if (jumped) {
2171 warning(e->pos, "advancing past deep designator");
2172 jumped = 0;
2174 REPLACE_CURRENT_PTR(e, last);
2175 } else {
2176 next = check_designators(e, ctype);
2177 if (!next) {
2178 DELETE_CURRENT_PTR(e);
2179 continue;
2181 top = next;
2182 /* deeper than one designator? */
2183 jumped = top != e;
2184 convert_designators(last);
2185 last = e;
2188 found:
2189 lclass = classify_type(top->ctype, &type);
2190 if (top->type == EXPR_INDEX)
2191 v = &top->idx_expression;
2192 else
2193 v = &top->ident_expression;
2195 if (handle_simple_initializer(v, 1, lclass, top->ctype))
2196 continue;
2198 if (!(lclass & TYPE_COMPOUND)) {
2199 warning(e->pos, "bogus scalar initializer");
2200 DELETE_CURRENT_PTR(e);
2201 continue;
2204 next = first_subobject(type, lclass, v);
2205 if (next) {
2206 warning(e->pos, "missing braces around initializer");
2207 top = next;
2208 goto found;
2211 DELETE_CURRENT_PTR(e);
2212 excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2214 } END_FOR_EACH_PTR(e);
2216 convert_designators(last);
2217 expr->ctype = ctype;
2220 static int is_string_literal(struct expression **v)
2222 struct expression *e = *v;
2223 while (e && e->type == EXPR_PREOP && e->op == '(')
2224 e = e->unop;
2225 if (!e || e->type != EXPR_STRING)
2226 return 0;
2227 if (e != *v && Wparen_string)
2228 warning(e->pos,
2229 "array initialized from parenthesized string constant");
2230 *v = e;
2231 return 1;
2235 * We want a normal expression, possibly in one layer of braces. Warn
2236 * if the latter happens inside a list (it's legal, but likely to be
2237 * an effect of screwup). In case of anything not legal, we are definitely
2238 * having an effect of screwup, so just fail and let the caller warn.
2240 static struct expression *handle_scalar(struct expression *e, int nested)
2242 struct expression *v = NULL, *p;
2243 int count = 0;
2245 /* normal case */
2246 if (e->type != EXPR_INITIALIZER)
2247 return e;
2249 FOR_EACH_PTR(e->expr_list, p) {
2250 if (!v)
2251 v = p;
2252 count++;
2253 } END_FOR_EACH_PTR(p);
2254 if (count != 1)
2255 return NULL;
2256 switch(v->type) {
2257 case EXPR_INITIALIZER:
2258 case EXPR_INDEX:
2259 case EXPR_IDENTIFIER:
2260 return NULL;
2261 default:
2262 break;
2264 if (nested)
2265 warning(e->pos, "braces around scalar initializer");
2266 return v;
2270 * deal with the cases that don't care about subobjects:
2271 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2272 * character array <- string literal, possibly in braces [6.7.8(14)]
2273 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2274 * compound type <- initializer list in braces [6.7.8(16)]
2275 * The last one punts to handle_list_initializer() which, in turn will call
2276 * us for individual elements of the list.
2278 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2279 * the lack of support of wide char stuff in general.
2281 * One note: we need to take care not to evaluate a string literal until
2282 * we know that we *will* handle it right here. Otherwise we would screw
2283 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2284 * { "string", ...} - we need to preserve that string literal recognizable
2285 * until we dig into the inner struct.
2287 static int handle_simple_initializer(struct expression **ep, int nested,
2288 int class, struct symbol *ctype)
2290 int is_string = is_string_type(ctype);
2291 struct expression *e = *ep, *p;
2292 struct symbol *type;
2294 if (!e)
2295 return 0;
2297 /* scalar */
2298 if (!(class & TYPE_COMPOUND)) {
2299 e = handle_scalar(e, nested);
2300 if (!e)
2301 return 0;
2302 *ep = e;
2303 type = evaluate_expression(e);
2304 if (!e->ctype)
2305 return 1;
2306 compatible_assignment_types(e, ctype, ep, degenerate(e),
2307 "initializer");
2308 return 1;
2312 * sublist; either a string, or we dig in; the latter will deal with
2313 * pathologies, so we don't need anything fancy here.
2315 if (e->type == EXPR_INITIALIZER) {
2316 if (is_string) {
2317 struct expression *v = NULL;
2318 int count = 0;
2320 FOR_EACH_PTR(e->expr_list, p) {
2321 if (!v)
2322 v = p;
2323 count++;
2324 } END_FOR_EACH_PTR(p);
2325 if (count == 1 && is_string_literal(&v)) {
2326 *ep = e = v;
2327 goto String;
2330 handle_list_initializer(e, class, ctype);
2331 return 1;
2334 /* string */
2335 if (is_string_literal(&e)) {
2336 /* either we are doing array of char, or we'll have to dig in */
2337 if (is_string) {
2338 *ep = e;
2339 goto String;
2341 return 0;
2343 /* struct or union can be initialized by compatible */
2344 if (class != TYPE_COMPOUND)
2345 return 0;
2346 type = evaluate_expression(e);
2347 if (!type)
2348 return 0;
2349 if (ctype->type == SYM_NODE)
2350 ctype = ctype->ctype.base_type;
2351 if (type->type == SYM_NODE)
2352 type = type->ctype.base_type;
2353 if (ctype == type)
2354 return 1;
2355 return 0;
2357 String:
2358 p = alloc_expression(e->pos, EXPR_STRING);
2359 *p = *e;
2360 type = evaluate_expression(p);
2361 if (ctype->bit_size != -1 &&
2362 ctype->bit_size + bits_in_char < type->bit_size) {
2363 warning(e->pos,
2364 "too long initializer-string for array of char");
2366 *ep = p;
2367 return 1;
2370 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2372 struct symbol *type;
2373 int class = classify_type(ctype, &type);
2374 if (!handle_simple_initializer(ep, 0, class, ctype))
2375 expression_error(*ep, "invalid initializer");
2378 static int get_as(struct symbol *sym)
2380 int as;
2381 unsigned long mod;
2383 if (!sym)
2384 return 0;
2385 as = sym->ctype.as;
2386 mod = sym->ctype.modifiers;
2387 if (sym->type == SYM_NODE) {
2388 sym = sym->ctype.base_type;
2389 as |= sym->ctype.as;
2390 mod |= sym->ctype.modifiers;
2394 * At least for now, allow casting to a "unsigned long".
2395 * That's how we do things like pointer arithmetic and
2396 * store pointers to registers.
2398 if (sym == &ulong_ctype)
2399 return -1;
2401 if (sym && sym->type == SYM_PTR) {
2402 sym = get_base_type(sym);
2403 as |= sym->ctype.as;
2404 mod |= sym->ctype.modifiers;
2406 if (mod & MOD_FORCE)
2407 return -1;
2408 return as;
2411 static void cast_to_as(struct expression *e, int as)
2413 struct expression *v = e->cast_expression;
2414 struct symbol *type = v->ctype;
2416 if (!Wcast_to_address_space)
2417 return;
2419 if (v->type != EXPR_VALUE || v->value)
2420 goto out;
2422 /* cast from constant 0 to pointer is OK */
2423 if (is_int_type(type))
2424 return;
2426 if (type->type == SYM_NODE)
2427 type = type->ctype.base_type;
2429 if (type->type == SYM_PTR && type->ctype.base_type == &void_ctype)
2430 return;
2432 out:
2433 warning(e->pos, "cast adds address space to expression (<asn:%d>)", as);
2436 static struct symbol *evaluate_cast(struct expression *expr)
2438 struct expression *target = expr->cast_expression;
2439 struct symbol *ctype = examine_symbol_type(expr->cast_type);
2440 struct symbol *t1, *t2;
2441 int class1, class2;
2442 int as1, as2;
2444 if (!target)
2445 return NULL;
2447 expr->ctype = ctype;
2448 expr->cast_type = ctype;
2451 * Special case: a cast can be followed by an
2452 * initializer, in which case we need to pass
2453 * the type value down to that initializer rather
2454 * than trying to evaluate it as an expression
2456 * A more complex case is when the initializer is
2457 * dereferenced as part of a post-fix expression.
2458 * We need to produce an expression that can be dereferenced.
2460 if (target->type == EXPR_INITIALIZER) {
2461 struct symbol *sym = expr->cast_type;
2462 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2464 sym->initializer = expr->cast_expression;
2465 evaluate_symbol(sym);
2467 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2468 addr->symbol = sym;
2470 expr->type = EXPR_PREOP;
2471 expr->op = '*';
2472 expr->unop = addr;
2473 expr->ctype = sym;
2475 return sym;
2478 evaluate_expression(target);
2479 degenerate(target);
2481 class1 = classify_type(ctype, &t1);
2483 * You can always throw a value away by casting to
2484 * "void" - that's an implicit "force". Note that
2485 * the same is _not_ true of "void *".
2487 if (t1 == &void_ctype)
2488 goto out;
2490 if (class1 & TYPE_COMPOUND)
2491 warning(expr->pos, "cast to non-scalar");
2493 t2 = target->ctype;
2494 if (!t2) {
2495 expression_error(expr, "cast from unknown type");
2496 goto out;
2498 class2 = classify_type(t2, &t2);
2500 if (class2 & TYPE_COMPOUND)
2501 warning(expr->pos, "cast from non-scalar");
2503 /* allowed cast unfouls */
2504 if (class2 & TYPE_FOULED)
2505 t2 = t2->ctype.base_type;
2507 if (!(ctype->ctype.modifiers & MOD_FORCE) && t1 != t2) {
2508 if (class1 & TYPE_RESTRICT)
2509 warning(expr->pos, "cast to restricted type");
2510 if (class2 & TYPE_RESTRICT)
2511 warning(expr->pos, "cast from restricted type");
2514 as1 = get_as(ctype);
2515 as2 = get_as(target->ctype);
2516 if (!as1 && as2 > 0)
2517 warning(expr->pos, "cast removes address space of expression");
2518 if (as1 > 0 && as2 > 0 && as1 != as2)
2519 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2520 if (as1 > 0 && !as2)
2521 cast_to_as(expr, as1);
2524 * Casts of constant values are special: they
2525 * can be NULL, and thus need to be simplified
2526 * early.
2528 if (target->type == EXPR_VALUE)
2529 cast_value(expr, ctype, target, target->ctype);
2531 out:
2532 return ctype;
2536 * Evaluate a call expression with a symbol. This
2537 * should expand inline functions, and evaluate
2538 * builtins.
2540 static int evaluate_symbol_call(struct expression *expr)
2542 struct expression *fn = expr->fn;
2543 struct symbol *ctype = fn->ctype;
2545 if (fn->type != EXPR_PREOP)
2546 return 0;
2548 if (ctype->op && ctype->op->evaluate)
2549 return ctype->op->evaluate(expr);
2551 if (ctype->ctype.modifiers & MOD_INLINE) {
2552 int ret;
2553 struct symbol *curr = current_fn;
2554 current_fn = ctype->ctype.base_type;
2556 ret = inline_function(expr, ctype);
2558 /* restore the old function */
2559 current_fn = curr;
2560 return ret;
2563 return 0;
2566 static struct symbol *evaluate_call(struct expression *expr)
2568 int args, fnargs;
2569 struct symbol *ctype, *sym;
2570 struct expression *fn = expr->fn;
2571 struct expression_list *arglist = expr->args;
2573 if (!evaluate_expression(fn))
2574 return NULL;
2575 sym = ctype = fn->ctype;
2576 if (ctype->type == SYM_NODE)
2577 ctype = ctype->ctype.base_type;
2578 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
2579 ctype = get_base_type(ctype);
2581 examine_fn_arguments(ctype);
2582 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
2583 sym->op && sym->op->args) {
2584 if (!sym->op->args(expr))
2585 return NULL;
2586 } else {
2587 if (!evaluate_arguments(sym, ctype, arglist))
2588 return NULL;
2589 if (ctype->type != SYM_FN) {
2590 expression_error(expr, "not a function %s",
2591 show_ident(sym->ident));
2592 return NULL;
2594 args = expression_list_size(expr->args);
2595 fnargs = symbol_list_size(ctype->arguments);
2596 if (args < fnargs)
2597 expression_error(expr,
2598 "not enough arguments for function %s",
2599 show_ident(sym->ident));
2600 if (args > fnargs && !ctype->variadic)
2601 expression_error(expr,
2602 "too many arguments for function %s",
2603 show_ident(sym->ident));
2605 if (sym->type == SYM_NODE) {
2606 if (evaluate_symbol_call(expr))
2607 return expr->ctype;
2609 expr->ctype = ctype->ctype.base_type;
2610 return expr->ctype;
2613 struct symbol *evaluate_expression(struct expression *expr)
2615 if (!expr)
2616 return NULL;
2617 if (expr->ctype)
2618 return expr->ctype;
2620 switch (expr->type) {
2621 case EXPR_VALUE:
2622 case EXPR_FVALUE:
2623 expression_error(expr, "value expression without a type");
2624 return NULL;
2625 case EXPR_STRING:
2626 return evaluate_string(expr);
2627 case EXPR_SYMBOL:
2628 return evaluate_symbol_expression(expr);
2629 case EXPR_BINOP:
2630 if (!evaluate_expression(expr->left))
2631 return NULL;
2632 if (!evaluate_expression(expr->right))
2633 return NULL;
2634 return evaluate_binop(expr);
2635 case EXPR_LOGICAL:
2636 return evaluate_logical(expr);
2637 case EXPR_COMMA:
2638 evaluate_expression(expr->left);
2639 if (!evaluate_expression(expr->right))
2640 return NULL;
2641 return evaluate_comma(expr);
2642 case EXPR_COMPARE:
2643 if (!evaluate_expression(expr->left))
2644 return NULL;
2645 if (!evaluate_expression(expr->right))
2646 return NULL;
2647 return evaluate_compare(expr);
2648 case EXPR_ASSIGNMENT:
2649 if (!evaluate_expression(expr->left))
2650 return NULL;
2651 if (!evaluate_expression(expr->right))
2652 return NULL;
2653 return evaluate_assignment(expr);
2654 case EXPR_PREOP:
2655 if (!evaluate_expression(expr->unop))
2656 return NULL;
2657 return evaluate_preop(expr);
2658 case EXPR_POSTOP:
2659 if (!evaluate_expression(expr->unop))
2660 return NULL;
2661 return evaluate_postop(expr);
2662 case EXPR_CAST:
2663 case EXPR_IMPLIED_CAST:
2664 return evaluate_cast(expr);
2665 case EXPR_SIZEOF:
2666 return evaluate_sizeof(expr);
2667 case EXPR_PTRSIZEOF:
2668 return evaluate_ptrsizeof(expr);
2669 case EXPR_ALIGNOF:
2670 return evaluate_alignof(expr);
2671 case EXPR_DEREF:
2672 return evaluate_member_dereference(expr);
2673 case EXPR_CALL:
2674 return evaluate_call(expr);
2675 case EXPR_SELECT:
2676 case EXPR_CONDITIONAL:
2677 return evaluate_conditional_expression(expr);
2678 case EXPR_STATEMENT:
2679 expr->ctype = evaluate_statement(expr->statement);
2680 return expr->ctype;
2682 case EXPR_LABEL:
2683 expr->ctype = &ptr_ctype;
2684 return &ptr_ctype;
2686 case EXPR_TYPE:
2687 /* Evaluate the type of the symbol .. */
2688 evaluate_symbol(expr->symbol);
2689 /* .. but the type of the _expression_ is a "type" */
2690 expr->ctype = &type_ctype;
2691 return &type_ctype;
2693 /* These can not exist as stand-alone expressions */
2694 case EXPR_INITIALIZER:
2695 case EXPR_IDENTIFIER:
2696 case EXPR_INDEX:
2697 case EXPR_POS:
2698 expression_error(expr, "internal front-end error: initializer in expression");
2699 return NULL;
2700 case EXPR_SLICE:
2701 expression_error(expr, "internal front-end error: SLICE re-evaluated");
2702 return NULL;
2704 return NULL;
2707 static void check_duplicates(struct symbol *sym)
2709 int declared = 0;
2710 struct symbol *next = sym;
2712 while ((next = next->same_symbol) != NULL) {
2713 const char *typediff;
2714 evaluate_symbol(next);
2715 declared++;
2716 typediff = type_difference(sym, next, 0, 0);
2717 if (typediff) {
2718 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2719 show_ident(sym->ident),
2720 stream_name(next->pos.stream), next->pos.line, typediff);
2721 return;
2724 if (!declared) {
2725 unsigned long mod = sym->ctype.modifiers;
2726 if (mod & (MOD_STATIC | MOD_REGISTER))
2727 return;
2728 if (!(mod & MOD_TOPLEVEL))
2729 return;
2730 if (!Wdecl)
2731 return;
2732 if (sym->ident == &main_ident)
2733 return;
2734 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
2738 static struct symbol *evaluate_symbol(struct symbol *sym)
2740 struct symbol *base_type;
2742 if (!sym)
2743 return sym;
2744 if (sym->evaluated)
2745 return sym;
2746 sym->evaluated = 1;
2748 sym = examine_symbol_type(sym);
2749 base_type = get_base_type(sym);
2750 if (!base_type)
2751 return NULL;
2753 /* Evaluate the initializers */
2754 if (sym->initializer)
2755 evaluate_initializer(sym, &sym->initializer);
2757 /* And finally, evaluate the body of the symbol too */
2758 if (base_type->type == SYM_FN) {
2759 struct symbol *curr = current_fn;
2761 current_fn = base_type;
2763 examine_fn_arguments(base_type);
2764 if (!base_type->stmt && base_type->inline_stmt)
2765 uninline(sym);
2766 if (base_type->stmt)
2767 evaluate_statement(base_type->stmt);
2769 current_fn = curr;
2772 return base_type;
2775 void evaluate_symbol_list(struct symbol_list *list)
2777 struct symbol *sym;
2779 FOR_EACH_PTR(list, sym) {
2780 evaluate_symbol(sym);
2781 check_duplicates(sym);
2782 } END_FOR_EACH_PTR(sym);
2785 static struct symbol *evaluate_return_expression(struct statement *stmt)
2787 struct expression *expr = stmt->expression;
2788 struct symbol *ctype, *fntype;
2790 evaluate_expression(expr);
2791 ctype = degenerate(expr);
2792 fntype = current_fn->ctype.base_type;
2793 if (!fntype || fntype == &void_ctype) {
2794 if (expr && ctype != &void_ctype)
2795 expression_error(expr, "return expression in %s function", fntype?"void":"typeless");
2796 return NULL;
2799 if (!expr) {
2800 sparse_error(stmt->pos, "return with no return value");
2801 return NULL;
2803 if (!ctype)
2804 return NULL;
2805 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression");
2806 return NULL;
2809 static void evaluate_if_statement(struct statement *stmt)
2811 if (!stmt->if_conditional)
2812 return;
2814 evaluate_conditional(stmt->if_conditional, 0);
2815 evaluate_statement(stmt->if_true);
2816 evaluate_statement(stmt->if_false);
2819 static void evaluate_iterator(struct statement *stmt)
2821 evaluate_conditional(stmt->iterator_pre_condition, 1);
2822 evaluate_conditional(stmt->iterator_post_condition,1);
2823 evaluate_statement(stmt->iterator_pre_statement);
2824 evaluate_statement(stmt->iterator_statement);
2825 evaluate_statement(stmt->iterator_post_statement);
2828 static void verify_output_constraint(struct expression *expr, const char *constraint)
2830 switch (*constraint) {
2831 case '=': /* Assignment */
2832 case '+': /* Update */
2833 break;
2834 default:
2835 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
2839 static void verify_input_constraint(struct expression *expr, const char *constraint)
2841 switch (*constraint) {
2842 case '=': /* Assignment */
2843 case '+': /* Update */
2844 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
2848 static void evaluate_asm_statement(struct statement *stmt)
2850 struct expression *expr;
2851 int state;
2853 expr = stmt->asm_string;
2854 if (!expr || expr->type != EXPR_STRING) {
2855 sparse_error(stmt->pos, "need constant string for inline asm");
2856 return;
2859 state = 0;
2860 FOR_EACH_PTR(stmt->asm_outputs, expr) {
2861 struct ident *ident;
2863 switch (state) {
2864 case 0: /* Identifier */
2865 state = 1;
2866 ident = (struct ident *)expr;
2867 continue;
2869 case 1: /* Constraint */
2870 state = 2;
2871 if (!expr || expr->type != EXPR_STRING) {
2872 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
2873 *THIS_ADDRESS(expr) = NULL;
2874 continue;
2876 verify_output_constraint(expr, expr->string->data);
2877 continue;
2879 case 2: /* Expression */
2880 state = 0;
2881 if (!evaluate_expression(expr))
2882 return;
2883 if (!lvalue_expression(expr))
2884 warning(expr->pos, "asm output is not an lvalue");
2885 evaluate_assign_to(expr, expr->ctype);
2886 continue;
2888 } END_FOR_EACH_PTR(expr);
2890 state = 0;
2891 FOR_EACH_PTR(stmt->asm_inputs, expr) {
2892 struct ident *ident;
2894 switch (state) {
2895 case 0: /* Identifier */
2896 state = 1;
2897 ident = (struct ident *)expr;
2898 continue;
2900 case 1: /* Constraint */
2901 state = 2;
2902 if (!expr || expr->type != EXPR_STRING) {
2903 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
2904 *THIS_ADDRESS(expr) = NULL;
2905 continue;
2907 verify_input_constraint(expr, expr->string->data);
2908 continue;
2910 case 2: /* Expression */
2911 state = 0;
2912 if (!evaluate_expression(expr))
2913 return;
2914 continue;
2916 } END_FOR_EACH_PTR(expr);
2918 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
2919 if (!expr) {
2920 sparse_error(stmt->pos, "bad asm output");
2921 return;
2923 if (expr->type == EXPR_STRING)
2924 continue;
2925 expression_error(expr, "asm clobber is not a string");
2926 } END_FOR_EACH_PTR(expr);
2929 static void evaluate_case_statement(struct statement *stmt)
2931 evaluate_expression(stmt->case_expression);
2932 evaluate_expression(stmt->case_to);
2933 evaluate_statement(stmt->case_statement);
2936 static void check_case_type(struct expression *switch_expr,
2937 struct expression *case_expr,
2938 struct expression **enumcase)
2940 struct symbol *switch_type, *case_type;
2941 int sclass, cclass;
2943 if (!case_expr)
2944 return;
2946 switch_type = switch_expr->ctype;
2947 case_type = evaluate_expression(case_expr);
2949 if (!switch_type || !case_type)
2950 goto Bad;
2951 if (enumcase) {
2952 if (*enumcase)
2953 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
2954 else if (is_enum_type(case_type))
2955 *enumcase = case_expr;
2958 sclass = classify_type(switch_type, &switch_type);
2959 cclass = classify_type(case_type, &case_type);
2961 /* both should be arithmetic */
2962 if (!(sclass & cclass & TYPE_NUM))
2963 goto Bad;
2965 /* neither should be floating */
2966 if ((sclass | cclass) & TYPE_FLOAT)
2967 goto Bad;
2969 /* if neither is restricted, we are OK */
2970 if (!((sclass | cclass) & TYPE_RESTRICT))
2971 return;
2973 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
2974 cclass, sclass, case_type, switch_type))
2975 warning(case_expr->pos, "restricted degrades to integer");
2977 return;
2979 Bad:
2980 expression_error(case_expr, "incompatible types for 'case' statement");
2983 static void evaluate_switch_statement(struct statement *stmt)
2985 struct symbol *sym;
2986 struct expression *enumcase = NULL;
2987 struct expression **enumcase_holder = &enumcase;
2988 struct expression *sel = stmt->switch_expression;
2990 evaluate_expression(sel);
2991 evaluate_statement(stmt->switch_statement);
2992 if (!sel)
2993 return;
2994 if (sel->ctype && is_enum_type(sel->ctype))
2995 enumcase_holder = NULL; /* Only check cases against switch */
2997 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
2998 struct statement *case_stmt = sym->stmt;
2999 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
3000 check_case_type(sel, case_stmt->case_to, enumcase_holder);
3001 } END_FOR_EACH_PTR(sym);
3004 struct symbol *evaluate_statement(struct statement *stmt)
3006 if (!stmt)
3007 return NULL;
3009 switch (stmt->type) {
3010 case STMT_DECLARATION: {
3011 struct symbol *s;
3012 FOR_EACH_PTR(stmt->declaration, s) {
3013 evaluate_symbol(s);
3014 } END_FOR_EACH_PTR(s);
3015 return NULL;
3018 case STMT_RETURN:
3019 return evaluate_return_expression(stmt);
3021 case STMT_EXPRESSION:
3022 if (!evaluate_expression(stmt->expression))
3023 return NULL;
3024 return degenerate(stmt->expression);
3026 case STMT_COMPOUND: {
3027 struct statement *s;
3028 struct symbol *type = NULL;
3030 /* Evaluate the return symbol in the compound statement */
3031 evaluate_symbol(stmt->ret);
3034 * Then, evaluate each statement, making the type of the
3035 * compound statement be the type of the last statement
3037 type = evaluate_statement(stmt->args);
3038 FOR_EACH_PTR(stmt->stmts, s) {
3039 type = evaluate_statement(s);
3040 } END_FOR_EACH_PTR(s);
3041 if (!type)
3042 type = &void_ctype;
3043 return type;
3045 case STMT_IF:
3046 evaluate_if_statement(stmt);
3047 return NULL;
3048 case STMT_ITERATOR:
3049 evaluate_iterator(stmt);
3050 return NULL;
3051 case STMT_SWITCH:
3052 evaluate_switch_statement(stmt);
3053 return NULL;
3054 case STMT_CASE:
3055 evaluate_case_statement(stmt);
3056 return NULL;
3057 case STMT_LABEL:
3058 return evaluate_statement(stmt->label_statement);
3059 case STMT_GOTO:
3060 evaluate_expression(stmt->goto_expression);
3061 return NULL;
3062 case STMT_NONE:
3063 break;
3064 case STMT_ASM:
3065 evaluate_asm_statement(stmt);
3066 return NULL;
3067 case STMT_CONTEXT:
3068 evaluate_expression(stmt->expression);
3069 return NULL;
3070 case STMT_RANGE:
3071 evaluate_expression(stmt->range_expression);
3072 evaluate_expression(stmt->range_low);
3073 evaluate_expression(stmt->range_high);
3074 return NULL;
3076 return NULL;