Merge commit 'viro/integer-constant'
[smatch.git] / evaluate.c
blobbcac1d2f66cb2521a79f8db24cbcb757959f8d89
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->flags = old->flags;
315 expr->ctype = type;
316 expr->cast_type = type;
317 expr->cast_expression = old;
318 return expr;
321 static int is_type_type(struct symbol *type)
323 return (type->ctype.modifiers & MOD_TYPE) != 0;
326 int is_ptr_type(struct symbol *type)
328 if (type->type == SYM_NODE)
329 type = type->ctype.base_type;
330 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
333 static inline int is_float_type(struct symbol *type)
335 if (type->type == SYM_NODE)
336 type = type->ctype.base_type;
337 return type->ctype.base_type == &fp_type;
340 static inline int is_byte_type(struct symbol *type)
342 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
345 enum {
346 TYPE_NUM = 1,
347 TYPE_BITFIELD = 2,
348 TYPE_RESTRICT = 4,
349 TYPE_FLOAT = 8,
350 TYPE_PTR = 16,
351 TYPE_COMPOUND = 32,
352 TYPE_FOULED = 64,
355 static inline int classify_type(struct symbol *type, struct symbol **base)
357 static int type_class[SYM_BAD + 1] = {
358 [SYM_PTR] = TYPE_PTR,
359 [SYM_FN] = TYPE_PTR,
360 [SYM_ARRAY] = TYPE_PTR | TYPE_COMPOUND,
361 [SYM_STRUCT] = TYPE_COMPOUND,
362 [SYM_UNION] = TYPE_COMPOUND,
363 [SYM_BITFIELD] = TYPE_NUM | TYPE_BITFIELD,
364 [SYM_RESTRICT] = TYPE_NUM | TYPE_RESTRICT,
365 [SYM_FOULED] = TYPE_NUM | TYPE_RESTRICT | TYPE_FOULED,
367 if (type->type == SYM_NODE)
368 type = type->ctype.base_type;
369 if (type->type == SYM_ENUM)
370 type = type->ctype.base_type;
371 *base = type;
372 if (type->type == SYM_BASETYPE) {
373 if (type->ctype.base_type == &int_type)
374 return TYPE_NUM;
375 if (type->ctype.base_type == &fp_type)
376 return TYPE_NUM | TYPE_FLOAT;
378 return type_class[type->type];
381 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
383 static inline int is_string_type(struct symbol *type)
385 if (type->type == SYM_NODE)
386 type = type->ctype.base_type;
387 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
390 static struct symbol *bad_expr_type(struct expression *expr)
392 sparse_error(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
393 switch (expr->type) {
394 case EXPR_BINOP:
395 case EXPR_COMPARE:
396 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
397 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
398 break;
399 case EXPR_PREOP:
400 case EXPR_POSTOP:
401 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
402 break;
403 default:
404 break;
407 expr->flags = 0;
408 return expr->ctype = &bad_ctype;
411 static int restricted_value(struct expression *v, struct symbol *type)
413 if (v->type != EXPR_VALUE)
414 return 1;
415 if (v->value != 0)
416 return 1;
417 return 0;
420 static int restricted_binop(int op, struct symbol *type)
422 switch (op) {
423 case '&':
424 case '=':
425 case SPECIAL_AND_ASSIGN:
426 case SPECIAL_OR_ASSIGN:
427 case SPECIAL_XOR_ASSIGN:
428 return 1; /* unfoul */
429 case '|':
430 case '^':
431 case '?':
432 return 2; /* keep fouled */
433 case SPECIAL_EQUAL:
434 case SPECIAL_NOTEQUAL:
435 return 3; /* warn if fouled */
436 default:
437 return 0; /* warn */
441 static int restricted_unop(int op, struct symbol **type)
443 if (op == '~') {
444 if ((*type)->bit_size < bits_in_int)
445 *type = befoul(*type);
446 return 0;
447 } if (op == '+')
448 return 0;
449 return 1;
452 static struct symbol *restricted_binop_type(int op,
453 struct expression *left,
454 struct expression *right,
455 int lclass, int rclass,
456 struct symbol *ltype,
457 struct symbol *rtype)
459 struct symbol *ctype = NULL;
460 if (lclass & TYPE_RESTRICT) {
461 if (rclass & TYPE_RESTRICT) {
462 if (ltype == rtype) {
463 ctype = ltype;
464 } else if (lclass & TYPE_FOULED) {
465 if (ltype->ctype.base_type == rtype)
466 ctype = ltype;
467 } else if (rclass & TYPE_FOULED) {
468 if (rtype->ctype.base_type == ltype)
469 ctype = rtype;
471 } else {
472 if (!restricted_value(right, ltype))
473 ctype = ltype;
475 } else if (!restricted_value(left, rtype))
476 ctype = rtype;
478 if (ctype) {
479 switch (restricted_binop(op, ctype)) {
480 case 1:
481 if ((lclass ^ rclass) & TYPE_FOULED)
482 ctype = ctype->ctype.base_type;
483 break;
484 case 3:
485 if (!(lclass & rclass & TYPE_FOULED))
486 break;
487 case 0:
488 ctype = NULL;
489 default:
490 break;
494 return ctype;
497 static inline void unrestrict(struct expression *expr,
498 int class, struct symbol **ctype)
500 if (class & TYPE_RESTRICT) {
501 warning(expr->pos, "restricted degrades to integer");
502 if (class & TYPE_FOULED) /* unfoul it first */
503 *ctype = (*ctype)->ctype.base_type;
504 *ctype = (*ctype)->ctype.base_type; /* get to arithmetic type */
508 static struct symbol *usual_conversions(int op,
509 struct expression *left,
510 struct expression *right,
511 int lclass, int rclass,
512 struct symbol *ltype,
513 struct symbol *rtype)
515 struct symbol *ctype;
517 warn_for_different_enum_types(right->pos, left->ctype, right->ctype);
519 if ((lclass | rclass) & TYPE_RESTRICT)
520 goto Restr;
522 Normal:
523 if (!(lclass & TYPE_FLOAT)) {
524 if (!(rclass & TYPE_FLOAT))
525 return bigger_int_type(ltype, rtype);
526 else
527 return rtype;
528 } else if (rclass & TYPE_FLOAT) {
529 unsigned long lmod = ltype->ctype.modifiers;
530 unsigned long rmod = rtype->ctype.modifiers;
531 if (rmod & ~lmod & (MOD_LONG | MOD_LONGLONG))
532 return rtype;
533 else
534 return ltype;
535 } else
536 return ltype;
538 Restr:
539 ctype = restricted_binop_type(op, left, right,
540 lclass, rclass, ltype, rtype);
541 if (ctype)
542 return ctype;
544 unrestrict(left, lclass, &ltype);
545 unrestrict(right, rclass, &rtype);
547 goto Normal;
550 static inline int lvalue_expression(struct expression *expr)
552 return expr->type == EXPR_PREOP && expr->op == '*';
555 static int ptr_object_size(struct symbol *ptr_type)
557 if (ptr_type->type == SYM_NODE)
558 ptr_type = ptr_type->ctype.base_type;
559 if (ptr_type->type == SYM_PTR)
560 ptr_type = get_base_type(ptr_type);
561 return ptr_type->bit_size;
564 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *ctype, struct symbol *itype)
566 struct expression *index = expr->right;
567 int multiply;
568 int bit_size;
570 examine_symbol_type(ctype);
572 if (!ctype->ctype.base_type) {
573 expression_error(expr, "missing type information");
574 return NULL;
577 /* Get the size of whatever the pointer points to */
578 bit_size = ptr_object_size(ctype);
579 multiply = bit_size >> 3;
581 expr->ctype = ctype;
583 if (multiply == 1 && itype->bit_size >= bits_in_pointer)
584 return ctype;
586 if (index->type == EXPR_VALUE) {
587 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
588 unsigned long long v = index->value, mask;
589 mask = 1ULL << (itype->bit_size - 1);
590 if (v & mask)
591 v |= -mask;
592 else
593 v &= mask - 1;
594 v *= multiply;
595 mask = 1ULL << (bits_in_pointer - 1);
596 v &= mask | (mask - 1);
597 val->value = v;
598 val->ctype = ssize_t_ctype;
599 expr->right = val;
600 return ctype;
603 if (itype->bit_size < bits_in_pointer)
604 index = cast_to(index, ssize_t_ctype);
606 if (multiply > 1) {
607 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
608 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
610 val->ctype = ssize_t_ctype;
611 val->value = multiply;
613 mul->op = '*';
614 mul->ctype = ssize_t_ctype;
615 mul->left = index;
616 mul->right = val;
617 index = mul;
620 expr->right = index;
621 return ctype;
624 const char * type_difference(struct symbol *target, struct symbol *source,
625 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
627 for (;;) {
628 unsigned long mod1, mod2, diff;
629 unsigned long as1, as2;
630 int type1, type2;
631 struct symbol *base1, *base2;
633 if (target == source)
634 break;
635 if (!target || !source)
636 return "different types";
638 * Peel of per-node information.
639 * FIXME! Check alignment and context too here!
641 mod1 = target->ctype.modifiers;
642 as1 = target->ctype.as;
643 mod2 = source->ctype.modifiers;
644 as2 = source->ctype.as;
645 if (target->type == SYM_NODE) {
646 target = target->ctype.base_type;
647 if (!target)
648 return "bad types";
649 if (target->type == SYM_PTR) {
650 mod1 = 0;
651 as1 = 0;
653 mod1 |= target->ctype.modifiers;
654 as1 |= target->ctype.as;
656 if (source->type == SYM_NODE) {
657 source = source->ctype.base_type;
658 if (!source)
659 return "bad types";
660 if (source->type == SYM_PTR) {
661 mod2 = 0;
662 as2 = 0;
664 mod2 |= source->ctype.modifiers;
665 as2 |= source->ctype.as;
667 if (target->type == SYM_ENUM) {
668 target = target->ctype.base_type;
669 if (!target)
670 return "bad types";
672 if (source->type == SYM_ENUM) {
673 source = source->ctype.base_type;
674 if (!source)
675 return "bad types";
678 if (target == source)
679 break;
680 if (!target || !source)
681 return "different types";
683 type1 = target->type;
684 base1 = target->ctype.base_type;
686 type2 = source->type;
687 base2 = source->ctype.base_type;
690 * Pointers to functions compare as the function itself
692 if (type1 == SYM_PTR && base1) {
693 base1 = examine_symbol_type(base1);
694 switch (base1->type) {
695 case SYM_FN:
696 type1 = SYM_FN;
697 target = base1;
698 base1 = base1->ctype.base_type;
699 default:
700 /* nothing */;
703 if (type2 == SYM_PTR && base2) {
704 base2 = examine_symbol_type(base2);
705 switch (base2->type) {
706 case SYM_FN:
707 type2 = SYM_FN;
708 source = base2;
709 base2 = base2->ctype.base_type;
710 default:
711 /* nothing */;
715 /* Arrays degenerate to pointers for type comparisons */
716 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
717 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
719 if (type1 != type2 || type1 == SYM_RESTRICT)
720 return "different base types";
722 /* Must be same address space to be comparable */
723 if (Waddress_space && as1 != as2)
724 return "different address spaces";
726 /* Ignore differences in storage types or addressability */
727 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
728 diff &= (mod1 & ~target_mod_ignore) | (mod2 & ~source_mod_ignore);
729 if (diff) {
730 if (diff & MOD_SIZE)
731 return "different type sizes";
732 if (diff & ~MOD_SIGNEDNESS)
733 return "different modifiers";
735 /* Differs in signedness only.. */
736 if (Wtypesign) {
738 * Warn if both are explicitly signed ("unsigned" is obviously
739 * always explicit, and since we know one of them has to be
740 * unsigned, we check if the signed one was explicit).
742 if ((mod1 | mod2) & MOD_EXPLICITLY_SIGNED)
743 return "different explicit signedness";
746 * "char" matches both "unsigned char" and "signed char",
747 * so if the explicit test didn't trigger, then we should
748 * not warn about a char.
750 if (!(mod1 & MOD_CHAR))
751 return "different signedness";
755 if (type1 == SYM_FN) {
756 int i;
757 struct symbol *arg1, *arg2;
758 if (base1->variadic != base2->variadic)
759 return "incompatible variadic arguments";
760 PREPARE_PTR_LIST(target->arguments, arg1);
761 PREPARE_PTR_LIST(source->arguments, arg2);
762 i = 1;
763 for (;;) {
764 const char *diffstr;
765 diffstr = type_difference(arg1, arg2, 0, 0);
766 if (diffstr) {
767 static char argdiff[80];
768 sprintf(argdiff, "incompatible argument %d (%s)", i, diffstr);
769 return argdiff;
771 if (!arg1)
772 break;
773 NEXT_PTR_LIST(arg1);
774 NEXT_PTR_LIST(arg2);
775 i++;
777 FINISH_PTR_LIST(arg2);
778 FINISH_PTR_LIST(arg1);
781 target = base1;
782 source = base2;
784 return NULL;
787 static int is_null_ptr(struct expression *expr)
789 if (expr->type != EXPR_VALUE || expr->value)
790 return 0;
791 if (Wnon_pointer_null && !is_ptr_type(expr->ctype))
792 warning(expr->pos, "Using plain integer as NULL pointer");
793 return 1;
797 * Ignore differences in "volatile" and "const"ness when
798 * subtracting pointers
800 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
802 static struct symbol *evaluate_ptr_sub(struct expression *expr)
804 const char *typediff;
805 struct symbol *ctype;
806 struct symbol *ltype, *rtype;
807 struct expression *l = expr->left;
808 struct expression *r = expr->right;
810 ltype = degenerate(l);
811 rtype = degenerate(r);
813 ctype = ltype;
814 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
815 if (typediff)
816 expression_error(expr, "subtraction of different types can't work (%s)", typediff);
817 examine_symbol_type(ctype);
819 /* Figure out the base type we point to */
820 if (ctype->type == SYM_NODE)
821 ctype = ctype->ctype.base_type;
822 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
823 expression_error(expr, "subtraction of functions? Share your drugs");
824 return NULL;
826 ctype = get_base_type(ctype);
828 expr->ctype = ssize_t_ctype;
829 if (ctype->bit_size > bits_in_char) {
830 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
831 struct expression *div = expr;
832 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
833 unsigned long value = ctype->bit_size >> 3;
835 val->ctype = size_t_ctype;
836 val->value = value;
838 if (value & (value-1)) {
839 if (Wptr_subtraction_blows)
840 warning(expr->pos, "potentially expensive pointer subtraction");
843 sub->op = '-';
844 sub->ctype = ssize_t_ctype;
845 sub->left = l;
846 sub->right = r;
848 div->op = '/';
849 div->left = sub;
850 div->right = val;
853 return ssize_t_ctype;
856 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
858 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
860 struct symbol *ctype;
862 if (!expr)
863 return NULL;
865 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
866 warning(expr->pos, "assignment expression in conditional");
868 ctype = evaluate_expression(expr);
869 if (ctype) {
870 if (is_safe_type(ctype))
871 warning(expr->pos, "testing a 'safe expression'");
874 return ctype;
877 static struct symbol *evaluate_logical(struct expression *expr)
879 if (!evaluate_conditional(expr->left, 0))
880 return NULL;
881 if (!evaluate_conditional(expr->right, 0))
882 return NULL;
884 expr->ctype = &bool_ctype;
885 if (expr->flags) {
886 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
887 expr->flags = 0;
889 return &bool_ctype;
892 static struct symbol *evaluate_binop(struct expression *expr)
894 struct symbol *ltype, *rtype, *ctype;
895 int lclass = classify_type(expr->left->ctype, &ltype);
896 int rclass = classify_type(expr->right->ctype, &rtype);
897 int op = expr->op;
899 if (expr->flags) {
900 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
901 expr->flags = 0;
904 /* number op number */
905 if (lclass & rclass & TYPE_NUM) {
906 if ((lclass | rclass) & TYPE_FLOAT) {
907 switch (op) {
908 case '+': case '-': case '*': case '/':
909 break;
910 default:
911 return bad_expr_type(expr);
915 if (op == SPECIAL_LEFTSHIFT || op == SPECIAL_RIGHTSHIFT) {
916 // shifts do integer promotions, but that's it.
917 unrestrict(expr->left, lclass, &ltype);
918 unrestrict(expr->right, rclass, &rtype);
919 ctype = ltype = integer_promotion(ltype);
920 rtype = integer_promotion(rtype);
921 } else {
922 // The rest do usual conversions
923 ltype = usual_conversions(op, expr->left, expr->right,
924 lclass, rclass, ltype, rtype);
925 ctype = rtype = ltype;
928 expr->left = cast_to(expr->left, ltype);
929 expr->right = cast_to(expr->right, rtype);
930 expr->ctype = ctype;
931 return ctype;
934 /* pointer (+|-) integer */
935 if (lclass & TYPE_PTR && is_int(rclass) && (op == '+' || op == '-')) {
936 unrestrict(expr->right, rclass, &rtype);
937 return evaluate_ptr_add(expr, degenerate(expr->left), rtype);
940 /* integer + pointer */
941 if (rclass & TYPE_PTR && is_int(lclass) && op == '+') {
942 struct expression *index = expr->left;
943 unrestrict(index, lclass, &ltype);
944 expr->left = expr->right;
945 expr->right = index;
946 return evaluate_ptr_add(expr, degenerate(expr->left), ltype);
949 /* pointer - pointer */
950 if (lclass & rclass & TYPE_PTR && expr->op == '-')
951 return evaluate_ptr_sub(expr);
953 return bad_expr_type(expr);
956 static struct symbol *evaluate_comma(struct expression *expr)
958 expr->ctype = expr->right->ctype;
959 return expr->ctype;
962 static int modify_for_unsigned(int op)
964 if (op == '<')
965 op = SPECIAL_UNSIGNED_LT;
966 else if (op == '>')
967 op = SPECIAL_UNSIGNED_GT;
968 else if (op == SPECIAL_LTE)
969 op = SPECIAL_UNSIGNED_LTE;
970 else if (op == SPECIAL_GTE)
971 op = SPECIAL_UNSIGNED_GTE;
972 return op;
975 static struct symbol *evaluate_compare(struct expression *expr)
977 struct expression *left = expr->left, *right = expr->right;
978 struct symbol *ltype = left->ctype, *rtype = right->ctype;
979 struct symbol *ctype;
980 int lclass, rclass;
982 if (expr->flags) {
983 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
984 expr->flags = 0;
987 /* Type types? */
988 if (is_type_type(ltype) && is_type_type(rtype))
989 goto OK;
991 if (is_safe_type(ltype) || is_safe_type(rtype))
992 warning(expr->pos, "testing a 'safe expression'");
994 lclass = classify_type(ltype, &ltype);
995 rclass = classify_type(rtype, &rtype);
997 /* Pointer types? */
998 if ((lclass | rclass) & TYPE_PTR) {
999 // FIXME! Check the types for compatibility
1000 expr->op = modify_for_unsigned(expr->op);
1001 goto OK;
1004 /* Both should be numbers */
1005 if (!(lclass & rclass & TYPE_NUM))
1006 return bad_expr_type(expr);
1008 ctype = usual_conversions(expr->op, expr->left, expr->right,
1009 lclass, rclass, ltype, rtype);
1010 expr->left = cast_to(expr->left, ctype);
1011 expr->right = cast_to(expr->right, ctype);
1012 if (ctype->ctype.modifiers & MOD_UNSIGNED)
1013 expr->op = modify_for_unsigned(expr->op);
1016 expr->ctype = &bool_ctype;
1017 return &bool_ctype;
1021 * FIXME!! This should do casts, array degeneration etc..
1023 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
1025 struct symbol *ltype = left->ctype, *rtype = right->ctype;
1027 if (ltype->type == SYM_NODE)
1028 ltype = ltype->ctype.base_type;
1030 if (rtype->type == SYM_NODE)
1031 rtype = rtype->ctype.base_type;
1033 if (ltype->type == SYM_PTR) {
1034 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
1035 return ltype;
1038 if (rtype->type == SYM_PTR) {
1039 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
1040 return rtype;
1042 return NULL;
1046 * NOTE! The degenerate case of "x ? : y", where we don't
1047 * have a true case, this will possibly promote "x" to the
1048 * same type as "y", and thus _change_ the conditional
1049 * test in the expression. But since promotion is "safe"
1050 * for testing, that's OK.
1052 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1054 struct expression **true;
1055 struct symbol *ctype, *ltype, *rtype;
1056 int lclass, rclass;
1057 const char * typediff;
1059 if (!evaluate_conditional(expr->conditional, 0))
1060 return NULL;
1061 if (!evaluate_expression(expr->cond_false))
1062 return NULL;
1064 ctype = degenerate(expr->conditional);
1065 rtype = degenerate(expr->cond_false);
1067 true = &expr->conditional;
1068 ltype = ctype;
1069 if (expr->cond_true) {
1070 if (!evaluate_expression(expr->cond_true))
1071 return NULL;
1072 ltype = degenerate(expr->cond_true);
1073 true = &expr->cond_true;
1076 if (expr->flags) {
1077 int flags = expr->conditional->flags & Int_const_expr;
1078 flags &= (*true)->flags & expr->cond_false->flags;
1079 if (!flags)
1080 expr->flags = 0;
1083 lclass = classify_type(ltype, &ltype);
1084 rclass = classify_type(rtype, &rtype);
1085 if (lclass & rclass & TYPE_NUM) {
1086 ctype = usual_conversions('?', *true, expr->cond_false,
1087 lclass, rclass, ltype, rtype);
1088 *true = cast_to(*true, ctype);
1089 expr->cond_false = cast_to(expr->cond_false, ctype);
1090 goto out;
1092 ctype = compatible_ptr_type(*true, expr->cond_false);
1093 if (ctype)
1094 goto out;
1095 ctype = ltype;
1096 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
1097 if (!typediff)
1098 goto out;
1099 expression_error(expr, "incompatible types in conditional expression (%s)", typediff);
1100 return NULL;
1102 out:
1103 expr->ctype = ctype;
1104 return ctype;
1107 /* FP assignments can not do modulo or bit operations */
1108 static int compatible_float_op(int op)
1110 return op == SPECIAL_ADD_ASSIGN ||
1111 op == SPECIAL_SUB_ASSIGN ||
1112 op == SPECIAL_MUL_ASSIGN ||
1113 op == SPECIAL_DIV_ASSIGN;
1116 static int evaluate_assign_op(struct expression *expr)
1118 struct symbol *target = expr->left->ctype;
1119 struct symbol *source = expr->right->ctype;
1120 struct symbol *t, *s;
1121 int tclass = classify_type(target, &t);
1122 int sclass = classify_type(source, &s);
1123 int op = expr->op;
1125 if (tclass & sclass & TYPE_NUM) {
1126 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1127 expression_error(expr, "invalid assignment");
1128 return 0;
1130 if (tclass & TYPE_RESTRICT) {
1131 if (!restricted_binop(op, t)) {
1132 expression_error(expr, "bad restricted assignment");
1133 return 0;
1135 /* allowed assignments unfoul */
1136 if (sclass & TYPE_FOULED && s->ctype.base_type == t)
1137 goto Cast;
1138 if (!restricted_value(expr->right, t))
1139 return 1;
1140 } else if (!(sclass & TYPE_RESTRICT))
1141 goto Cast;
1142 /* source and target would better be identical restricted */
1143 if (t == s)
1144 return 1;
1145 warning(expr->pos, "invalid restricted assignment");
1146 expr->right = cast_to(expr->right, target);
1147 return 0;
1149 if (tclass & TYPE_PTR && is_int(sclass)) {
1150 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1151 unrestrict(expr->right, sclass, &s);
1152 evaluate_ptr_add(expr, target, s);
1153 return 1;
1155 expression_error(expr, "invalid pointer assignment");
1156 return 0;
1159 expression_error(expr, "invalid assignment");
1160 return 0;
1162 Cast:
1163 expr->right = cast_to(expr->right, target);
1164 return 1;
1167 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1168 struct expression **rp, const char *where)
1170 const char *typediff;
1171 struct symbol *source = degenerate(*rp);
1172 struct symbol *t, *s;
1173 int tclass = classify_type(target, &t);
1174 int sclass = classify_type(source, &s);
1176 if (tclass & sclass & TYPE_NUM) {
1177 if (tclass & TYPE_RESTRICT) {
1178 /* allowed assignments unfoul */
1179 if (sclass & TYPE_FOULED && s->ctype.base_type == t)
1180 goto Cast;
1181 if (!restricted_value(*rp, target))
1182 return 1;
1183 } else if (!(sclass & TYPE_RESTRICT))
1184 goto Cast;
1187 /* It's OK if the target is more volatile or const than the source */
1188 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
1189 if (!typediff)
1190 return 1;
1192 /* Pointer destination? */
1193 if (tclass & TYPE_PTR) {
1194 struct expression *right = *rp;
1195 int source_as;
1196 int target_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;
1267 struct expression *where = expr;
1268 struct symbol *ltype;
1270 if (!lvalue_expression(left)) {
1271 expression_error(expr, "not an lvalue");
1272 return NULL;
1275 ltype = left->ctype;
1277 if (expr->op != '=') {
1278 if (!evaluate_assign_op(expr))
1279 return NULL;
1280 } else {
1281 if (!compatible_assignment_types(where, ltype, &expr->right, "assignment"))
1282 return NULL;
1285 evaluate_assign_to(left, ltype);
1287 expr->ctype = ltype;
1288 return ltype;
1291 static void examine_fn_arguments(struct symbol *fn)
1293 struct symbol *s;
1295 FOR_EACH_PTR(fn->arguments, s) {
1296 struct symbol *arg = evaluate_symbol(s);
1297 /* Array/function arguments silently degenerate into pointers */
1298 if (arg) {
1299 struct symbol *ptr;
1300 switch(arg->type) {
1301 case SYM_ARRAY:
1302 case SYM_FN:
1303 ptr = alloc_symbol(s->pos, SYM_PTR);
1304 if (arg->type == SYM_ARRAY)
1305 ptr->ctype = arg->ctype;
1306 else
1307 ptr->ctype.base_type = arg;
1308 ptr->ctype.as |= s->ctype.as;
1309 ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1311 s->ctype.base_type = ptr;
1312 s->ctype.as = 0;
1313 s->ctype.modifiers &= ~MOD_PTRINHERIT;
1314 s->bit_size = 0;
1315 s->examined = 0;
1316 examine_symbol_type(s);
1317 break;
1318 default:
1319 /* nothing */
1320 break;
1323 } END_FOR_EACH_PTR(s);
1326 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1328 /* Take the modifiers of the pointer, and apply them to the member */
1329 mod |= sym->ctype.modifiers;
1330 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1331 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1332 *newsym = *sym;
1333 newsym->ctype.as = as;
1334 newsym->ctype.modifiers = mod;
1335 sym = newsym;
1337 return sym;
1340 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1342 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1343 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1345 node->ctype.base_type = ptr;
1346 ptr->bit_size = bits_in_pointer;
1347 ptr->ctype.alignment = pointer_alignment;
1349 node->bit_size = bits_in_pointer;
1350 node->ctype.alignment = pointer_alignment;
1352 access_symbol(sym);
1353 if (sym->ctype.modifiers & MOD_REGISTER) {
1354 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1355 sym->ctype.modifiers &= ~MOD_REGISTER;
1357 if (sym->type == SYM_NODE) {
1358 ptr->ctype.as |= sym->ctype.as;
1359 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1360 sym = sym->ctype.base_type;
1362 if (degenerate && sym->type == SYM_ARRAY) {
1363 ptr->ctype.as |= sym->ctype.as;
1364 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1365 sym = sym->ctype.base_type;
1367 ptr->ctype.base_type = sym;
1369 return node;
1372 /* Arrays degenerate into pointers on pointer arithmetic */
1373 static struct symbol *degenerate(struct expression *expr)
1375 struct symbol *ctype, *base;
1377 if (!expr)
1378 return NULL;
1379 ctype = expr->ctype;
1380 if (!ctype)
1381 return NULL;
1382 base = examine_symbol_type(ctype);
1383 if (ctype->type == SYM_NODE)
1384 base = ctype->ctype.base_type;
1386 * Arrays degenerate into pointers to the entries, while
1387 * functions degenerate into pointers to themselves.
1388 * If array was part of non-lvalue compound, we create a copy
1389 * of that compound first and then act as if we were dealing with
1390 * the corresponding field in there.
1392 switch (base->type) {
1393 case SYM_ARRAY:
1394 if (expr->type == EXPR_SLICE) {
1395 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1396 struct expression *e0, *e1, *e2, *e3, *e4;
1398 a->ctype.base_type = expr->base->ctype;
1399 a->bit_size = expr->base->ctype->bit_size;
1400 a->array_size = expr->base->ctype->array_size;
1402 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1403 e0->symbol = a;
1404 e0->ctype = &lazy_ptr_ctype;
1406 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1407 e1->unop = e0;
1408 e1->op = '*';
1409 e1->ctype = expr->base->ctype; /* XXX */
1411 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1412 e2->left = e1;
1413 e2->right = expr->base;
1414 e2->op = '=';
1415 e2->ctype = expr->base->ctype;
1417 if (expr->r_bitpos) {
1418 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1419 e3->op = '+';
1420 e3->left = e0;
1421 e3->right = alloc_const_expression(expr->pos,
1422 expr->r_bitpos >> 3);
1423 e3->ctype = &lazy_ptr_ctype;
1424 } else {
1425 e3 = e0;
1428 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1429 e4->left = e2;
1430 e4->right = e3;
1431 e4->ctype = &lazy_ptr_ctype;
1433 expr->unop = e4;
1434 expr->type = EXPR_PREOP;
1435 expr->op = '*';
1437 case SYM_FN:
1438 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1439 expression_error(expr, "strange non-value function or array");
1440 return &bad_ctype;
1442 *expr = *expr->unop;
1443 ctype = create_pointer(expr, ctype, 1);
1444 expr->ctype = ctype;
1445 default:
1446 /* nothing */;
1448 return ctype;
1451 static struct symbol *evaluate_addressof(struct expression *expr)
1453 struct expression *op = expr->unop;
1454 struct symbol *ctype;
1456 if (op->op != '*' || op->type != EXPR_PREOP) {
1457 expression_error(expr, "not addressable");
1458 return NULL;
1460 ctype = op->ctype;
1461 *expr = *op->unop;
1462 expr->flags = 0;
1464 if (expr->type == EXPR_SYMBOL) {
1465 struct symbol *sym = expr->symbol;
1466 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1470 * symbol expression evaluation is lazy about the type
1471 * of the sub-expression, so we may have to generate
1472 * the type here if so..
1474 if (expr->ctype == &lazy_ptr_ctype) {
1475 ctype = create_pointer(expr, ctype, 0);
1476 expr->ctype = ctype;
1478 return expr->ctype;
1482 static struct symbol *evaluate_dereference(struct expression *expr)
1484 struct expression *op = expr->unop;
1485 struct symbol *ctype = op->ctype, *node, *target;
1487 /* Simplify: *&(expr) => (expr) */
1488 if (op->type == EXPR_PREOP && op->op == '&') {
1489 *expr = *op->unop;
1490 expr->flags = 0;
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 (expr->flags && !(expr->unop->flags & Int_const_expr))
1569 expr->flags = 0;
1570 if (is_int_type(ctype)) {
1571 struct symbol *rtype = rtype = integer_promotion(ctype);
1572 expr->unop = cast_to(expr->unop, rtype);
1573 ctype = rtype;
1574 } else if (is_float_type(ctype) && expr->op != '~') {
1575 /* no conversions needed */
1576 } else if (is_restricted_type(ctype) && !restricted_unop(expr->op, &ctype)) {
1577 /* no conversions needed */
1578 } else if (is_fouled_type(ctype) && !restricted_unop(expr->op, &ctype)) {
1579 /* no conversions needed */
1580 } else {
1581 return bad_expr_type(expr);
1583 if (expr->op == '+')
1584 *expr = *expr->unop;
1585 expr->ctype = ctype;
1586 return ctype;
1589 static struct symbol *evaluate_preop(struct expression *expr)
1591 struct symbol *ctype = expr->unop->ctype;
1593 switch (expr->op) {
1594 case '(':
1595 *expr = *expr->unop;
1596 return ctype;
1598 case '+':
1599 case '-':
1600 case '~':
1601 return evaluate_sign(expr);
1603 case '*':
1604 return evaluate_dereference(expr);
1606 case '&':
1607 return evaluate_addressof(expr);
1609 case SPECIAL_INCREMENT:
1610 case SPECIAL_DECREMENT:
1612 * From a type evaluation standpoint the preops are
1613 * the same as the postops
1615 return evaluate_postop(expr);
1617 case '!':
1618 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1619 expr->flags = 0;
1620 if (is_safe_type(ctype))
1621 warning(expr->pos, "testing a 'safe expression'");
1622 if (is_float_type(ctype)) {
1623 struct expression *arg = expr->unop;
1624 expr->type = EXPR_BINOP;
1625 expr->op = SPECIAL_EQUAL;
1626 expr->left = arg;
1627 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1628 expr->right->ctype = ctype;
1629 expr->right->fvalue = 0;
1630 } else if (is_fouled_type(ctype)) {
1631 warning(expr->pos, "restricted degrades to integer");
1633 ctype = &bool_ctype;
1634 break;
1636 default:
1637 break;
1639 expr->ctype = ctype;
1640 return &bool_ctype;
1643 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1645 struct ptr_list *head = (struct ptr_list *)_list;
1646 struct ptr_list *list = head;
1648 if (!head)
1649 return NULL;
1650 do {
1651 int i;
1652 for (i = 0; i < list->nr; i++) {
1653 struct symbol *sym = (struct symbol *) list->list[i];
1654 if (sym->ident) {
1655 if (sym->ident != ident)
1656 continue;
1657 *offset = sym->offset;
1658 return sym;
1659 } else {
1660 struct symbol *ctype = sym->ctype.base_type;
1661 struct symbol *sub;
1662 if (!ctype)
1663 continue;
1664 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1665 continue;
1666 sub = find_identifier(ident, ctype->symbol_list, offset);
1667 if (!sub)
1668 continue;
1669 *offset += sym->offset;
1670 return sub;
1673 } while ((list = list->next) != head);
1674 return NULL;
1677 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1679 struct expression *add;
1682 * Create a new add-expression
1684 * NOTE! Even if we just add zero, we need a new node
1685 * for the member pointer, since it has a different
1686 * type than the original pointer. We could make that
1687 * be just a cast, but the fact is, a node is a node,
1688 * so we might as well just do the "add zero" here.
1690 add = alloc_expression(expr->pos, EXPR_BINOP);
1691 add->op = '+';
1692 add->left = expr;
1693 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1694 add->right->ctype = &int_ctype;
1695 add->right->value = offset;
1698 * The ctype of the pointer will be lazily evaluated if
1699 * we ever take the address of this member dereference..
1701 add->ctype = &lazy_ptr_ctype;
1702 return add;
1705 /* structure/union dereference */
1706 static struct symbol *evaluate_member_dereference(struct expression *expr)
1708 int offset;
1709 struct symbol *ctype, *member;
1710 struct expression *deref = expr->deref, *add;
1711 struct ident *ident = expr->member;
1712 unsigned int mod;
1713 int address_space;
1715 if (!evaluate_expression(deref))
1716 return NULL;
1717 if (!ident) {
1718 expression_error(expr, "bad member name");
1719 return NULL;
1722 ctype = deref->ctype;
1723 address_space = ctype->ctype.as;
1724 mod = ctype->ctype.modifiers;
1725 if (ctype->type == SYM_NODE) {
1726 ctype = ctype->ctype.base_type;
1727 address_space |= ctype->ctype.as;
1728 mod |= ctype->ctype.modifiers;
1730 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1731 expression_error(expr, "expected structure or union");
1732 return NULL;
1734 examine_symbol_type(ctype);
1735 offset = 0;
1736 member = find_identifier(ident, ctype->symbol_list, &offset);
1737 if (!member) {
1738 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1739 const char *name = "<unnamed>";
1740 int namelen = 9;
1741 if (ctype->ident) {
1742 name = ctype->ident->name;
1743 namelen = ctype->ident->len;
1745 if (ctype->symbol_list)
1746 expression_error(expr, "no member '%s' in %s %.*s",
1747 show_ident(ident), type, namelen, name);
1748 else
1749 expression_error(expr, "using member '%s' in "
1750 "incomplete %s %.*s", show_ident(ident),
1751 type, namelen, name);
1752 return NULL;
1756 * The member needs to take on the address space and modifiers of
1757 * the "parent" type.
1759 member = convert_to_as_mod(member, address_space, mod);
1760 ctype = get_base_type(member);
1762 if (!lvalue_expression(deref)) {
1763 if (deref->type != EXPR_SLICE) {
1764 expr->base = deref;
1765 expr->r_bitpos = 0;
1766 } else {
1767 expr->base = deref->base;
1768 expr->r_bitpos = deref->r_bitpos;
1770 expr->r_bitpos += offset << 3;
1771 expr->type = EXPR_SLICE;
1772 expr->r_nrbits = member->bit_size;
1773 expr->r_bitpos += member->bit_offset;
1774 expr->ctype = member;
1775 return member;
1778 deref = deref->unop;
1779 expr->deref = deref;
1781 add = evaluate_offset(deref, offset);
1782 expr->type = EXPR_PREOP;
1783 expr->op = '*';
1784 expr->unop = add;
1786 expr->ctype = member;
1787 return member;
1790 static int is_promoted(struct expression *expr)
1792 while (1) {
1793 switch (expr->type) {
1794 case EXPR_BINOP:
1795 case EXPR_SELECT:
1796 case EXPR_CONDITIONAL:
1797 return 1;
1798 case EXPR_COMMA:
1799 expr = expr->right;
1800 continue;
1801 case EXPR_PREOP:
1802 switch (expr->op) {
1803 case '(':
1804 expr = expr->unop;
1805 continue;
1806 case '+':
1807 case '-':
1808 case '~':
1809 return 1;
1810 default:
1811 return 0;
1813 default:
1814 return 0;
1820 static struct symbol *evaluate_cast(struct expression *);
1822 static struct symbol *evaluate_type_information(struct expression *expr)
1824 struct symbol *sym = expr->cast_type;
1825 if (!sym) {
1826 sym = evaluate_expression(expr->cast_expression);
1827 if (!sym)
1828 return NULL;
1830 * Expressions of restricted types will possibly get
1831 * promoted - check that here
1833 if (is_restricted_type(sym)) {
1834 if (sym->bit_size < bits_in_int && is_promoted(expr))
1835 sym = &int_ctype;
1836 } else if (is_fouled_type(sym)) {
1837 sym = &int_ctype;
1840 examine_symbol_type(sym);
1841 if (is_bitfield_type(sym)) {
1842 expression_error(expr, "trying to examine bitfield type");
1843 return NULL;
1845 return sym;
1848 static struct symbol *evaluate_sizeof(struct expression *expr)
1850 struct symbol *type;
1851 int size;
1853 type = evaluate_type_information(expr);
1854 if (!type)
1855 return NULL;
1857 size = type->bit_size;
1858 if ((size < 0) || (size & 7))
1859 expression_error(expr, "cannot size expression");
1860 expr->type = EXPR_VALUE;
1861 expr->value = size >> 3;
1862 expr->ctype = size_t_ctype;
1863 return size_t_ctype;
1866 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
1868 struct symbol *type;
1869 int size;
1871 type = evaluate_type_information(expr);
1872 if (!type)
1873 return NULL;
1875 if (type->type == SYM_NODE)
1876 type = type->ctype.base_type;
1877 if (!type)
1878 return NULL;
1879 switch (type->type) {
1880 case SYM_ARRAY:
1881 break;
1882 case SYM_PTR:
1883 type = get_base_type(type);
1884 if (type)
1885 break;
1886 default:
1887 expression_error(expr, "expected pointer expression");
1888 return NULL;
1890 size = type->bit_size;
1891 if (size & 7)
1892 size = 0;
1893 expr->type = EXPR_VALUE;
1894 expr->value = size >> 3;
1895 expr->ctype = size_t_ctype;
1896 return size_t_ctype;
1899 static struct symbol *evaluate_alignof(struct expression *expr)
1901 struct symbol *type;
1903 type = evaluate_type_information(expr);
1904 if (!type)
1905 return NULL;
1907 expr->type = EXPR_VALUE;
1908 expr->value = type->ctype.alignment;
1909 expr->ctype = size_t_ctype;
1910 return size_t_ctype;
1913 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1915 struct expression *expr;
1916 struct symbol_list *argument_types = fn->arguments;
1917 struct symbol *argtype;
1918 int i = 1;
1920 PREPARE_PTR_LIST(argument_types, argtype);
1921 FOR_EACH_PTR (head, expr) {
1922 struct expression **p = THIS_ADDRESS(expr);
1923 struct symbol *ctype, *target;
1924 ctype = evaluate_expression(expr);
1926 if (!ctype)
1927 return 0;
1929 target = argtype;
1930 if (!target) {
1931 struct symbol *type;
1932 int class = classify_type(ctype, &type);
1933 if (is_int(class)) {
1934 *p = cast_to(expr, integer_promotion(type));
1935 } else if (class & TYPE_FLOAT) {
1936 unsigned long mod = type->ctype.modifiers;
1937 if (!(mod & (MOD_LONG|MOD_LONGLONG)))
1938 *p = cast_to(expr, &double_ctype);
1939 } else if (class & TYPE_PTR) {
1940 degenerate(expr);
1942 } else {
1943 static char where[30];
1944 examine_symbol_type(target);
1945 sprintf(where, "argument %d", i);
1946 compatible_assignment_types(expr, target, p, where);
1949 i++;
1950 NEXT_PTR_LIST(argtype);
1951 } END_FOR_EACH_PTR(expr);
1952 FINISH_PTR_LIST(argtype);
1953 return 1;
1956 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
1958 struct symbol *sym;
1960 FOR_EACH_PTR(ctype->symbol_list, sym) {
1961 if (sym->ident == ident)
1962 return sym;
1963 } END_FOR_EACH_PTR(sym);
1964 return NULL;
1967 static void convert_index(struct expression *e)
1969 struct expression *child = e->idx_expression;
1970 unsigned from = e->idx_from;
1971 unsigned to = e->idx_to + 1;
1972 e->type = EXPR_POS;
1973 e->init_offset = from * (e->ctype->bit_size>>3);
1974 e->init_nr = to - from;
1975 e->init_expr = child;
1978 static void convert_ident(struct expression *e)
1980 struct expression *child = e->ident_expression;
1981 struct symbol *sym = e->field;
1982 e->type = EXPR_POS;
1983 e->init_offset = sym->offset;
1984 e->init_nr = 1;
1985 e->init_expr = child;
1988 static void convert_designators(struct expression *e)
1990 while (e) {
1991 if (e->type == EXPR_INDEX)
1992 convert_index(e);
1993 else if (e->type == EXPR_IDENTIFIER)
1994 convert_ident(e);
1995 else
1996 break;
1997 e = e->init_expr;
2001 static void excess(struct expression *e, const char *s)
2003 warning(e->pos, "excessive elements in %s initializer", s);
2007 * implicit designator for the first element
2009 static struct expression *first_subobject(struct symbol *ctype, int class,
2010 struct expression **v)
2012 struct expression *e = *v, *new;
2014 if (ctype->type == SYM_NODE)
2015 ctype = ctype->ctype.base_type;
2017 if (class & TYPE_PTR) { /* array */
2018 if (!ctype->bit_size)
2019 return NULL;
2020 new = alloc_expression(e->pos, EXPR_INDEX);
2021 new->idx_expression = e;
2022 new->ctype = ctype->ctype.base_type;
2023 } else {
2024 struct symbol *field, *p;
2025 PREPARE_PTR_LIST(ctype->symbol_list, p);
2026 while (p && !p->ident && is_bitfield_type(p))
2027 NEXT_PTR_LIST(p);
2028 field = p;
2029 FINISH_PTR_LIST(p);
2030 if (!field)
2031 return NULL;
2032 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2033 new->ident_expression = e;
2034 new->field = new->ctype = field;
2036 *v = new;
2037 return new;
2041 * sanity-check explicit designators; return the innermost one or NULL
2042 * in case of error. Assign types.
2044 static struct expression *check_designators(struct expression *e,
2045 struct symbol *ctype)
2047 struct expression *last = NULL;
2048 const char *err;
2049 while (1) {
2050 if (ctype->type == SYM_NODE)
2051 ctype = ctype->ctype.base_type;
2052 if (e->type == EXPR_INDEX) {
2053 struct symbol *type;
2054 if (ctype->type != SYM_ARRAY) {
2055 err = "array index in non-array";
2056 break;
2058 type = ctype->ctype.base_type;
2059 if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2060 unsigned offset = e->idx_to * type->bit_size;
2061 if (offset >= ctype->bit_size) {
2062 err = "index out of bounds in";
2063 break;
2066 e->ctype = ctype = type;
2067 ctype = type;
2068 last = e;
2069 if (!e->idx_expression) {
2070 err = "invalid";
2071 break;
2073 e = e->idx_expression;
2074 } else if (e->type == EXPR_IDENTIFIER) {
2075 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2076 err = "field name not in struct or union";
2077 break;
2079 ctype = find_struct_ident(ctype, e->expr_ident);
2080 if (!ctype) {
2081 err = "unknown field name in";
2082 break;
2084 e->field = e->ctype = ctype;
2085 last = e;
2086 if (!e->ident_expression) {
2087 err = "invalid";
2088 break;
2090 e = e->ident_expression;
2091 } else if (e->type == EXPR_POS) {
2092 err = "internal front-end error: EXPR_POS in";
2093 break;
2094 } else
2095 return last;
2097 expression_error(e, "%s initializer", err);
2098 return NULL;
2102 * choose the next subobject to initialize.
2104 * Get designators for next element, switch old ones to EXPR_POS.
2105 * Return the resulting expression or NULL if we'd run out of subobjects.
2106 * The innermost designator is returned in *v. Designators in old
2107 * are assumed to be already sanity-checked.
2109 static struct expression *next_designators(struct expression *old,
2110 struct symbol *ctype,
2111 struct expression *e, struct expression **v)
2113 struct expression *new = NULL;
2115 if (!old)
2116 return NULL;
2117 if (old->type == EXPR_INDEX) {
2118 struct expression *copy;
2119 unsigned n;
2121 copy = next_designators(old->idx_expression,
2122 old->ctype, e, v);
2123 if (!copy) {
2124 n = old->idx_to + 1;
2125 if (n * old->ctype->bit_size == ctype->bit_size) {
2126 convert_index(old);
2127 return NULL;
2129 copy = e;
2130 *v = new = alloc_expression(e->pos, EXPR_INDEX);
2131 } else {
2132 n = old->idx_to;
2133 new = alloc_expression(e->pos, EXPR_INDEX);
2136 new->idx_from = new->idx_to = n;
2137 new->idx_expression = copy;
2138 new->ctype = old->ctype;
2139 convert_index(old);
2140 } else if (old->type == EXPR_IDENTIFIER) {
2141 struct expression *copy;
2142 struct symbol *field;
2144 copy = next_designators(old->ident_expression,
2145 old->ctype, e, v);
2146 if (!copy) {
2147 field = old->field->next_subobject;
2148 if (!field) {
2149 convert_ident(old);
2150 return NULL;
2152 copy = e;
2153 *v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2154 } else {
2155 field = old->field;
2156 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2159 new->field = field;
2160 new->expr_ident = field->ident;
2161 new->ident_expression = copy;
2162 new->ctype = field;
2163 convert_ident(old);
2165 return new;
2168 static int handle_simple_initializer(struct expression **ep, int nested,
2169 int class, struct symbol *ctype);
2172 * deal with traversing subobjects [6.7.8(17,18,20)]
2174 static void handle_list_initializer(struct expression *expr,
2175 int class, struct symbol *ctype)
2177 struct expression *e, *last = NULL, *top = NULL, *next;
2178 int jumped = 0;
2180 FOR_EACH_PTR(expr->expr_list, e) {
2181 struct expression **v;
2182 struct symbol *type;
2183 int lclass;
2185 if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2186 if (!top) {
2187 top = e;
2188 last = first_subobject(ctype, class, &top);
2189 } else {
2190 last = next_designators(last, ctype, e, &top);
2192 if (!last) {
2193 excess(e, class & TYPE_PTR ? "array" :
2194 "struct or union");
2195 DELETE_CURRENT_PTR(e);
2196 continue;
2198 if (jumped) {
2199 warning(e->pos, "advancing past deep designator");
2200 jumped = 0;
2202 REPLACE_CURRENT_PTR(e, last);
2203 } else {
2204 next = check_designators(e, ctype);
2205 if (!next) {
2206 DELETE_CURRENT_PTR(e);
2207 continue;
2209 top = next;
2210 /* deeper than one designator? */
2211 jumped = top != e;
2212 convert_designators(last);
2213 last = e;
2216 found:
2217 lclass = classify_type(top->ctype, &type);
2218 if (top->type == EXPR_INDEX)
2219 v = &top->idx_expression;
2220 else
2221 v = &top->ident_expression;
2223 if (handle_simple_initializer(v, 1, lclass, top->ctype))
2224 continue;
2226 if (!(lclass & TYPE_COMPOUND)) {
2227 warning(e->pos, "bogus scalar initializer");
2228 DELETE_CURRENT_PTR(e);
2229 continue;
2232 next = first_subobject(type, lclass, v);
2233 if (next) {
2234 warning(e->pos, "missing braces around initializer");
2235 top = next;
2236 goto found;
2239 DELETE_CURRENT_PTR(e);
2240 excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2242 } END_FOR_EACH_PTR(e);
2244 convert_designators(last);
2245 expr->ctype = ctype;
2248 static int is_string_literal(struct expression **v)
2250 struct expression *e = *v;
2251 while (e && e->type == EXPR_PREOP && e->op == '(')
2252 e = e->unop;
2253 if (!e || e->type != EXPR_STRING)
2254 return 0;
2255 if (e != *v && Wparen_string)
2256 warning(e->pos,
2257 "array initialized from parenthesized string constant");
2258 *v = e;
2259 return 1;
2263 * We want a normal expression, possibly in one layer of braces. Warn
2264 * if the latter happens inside a list (it's legal, but likely to be
2265 * an effect of screwup). In case of anything not legal, we are definitely
2266 * having an effect of screwup, so just fail and let the caller warn.
2268 static struct expression *handle_scalar(struct expression *e, int nested)
2270 struct expression *v = NULL, *p;
2271 int count = 0;
2273 /* normal case */
2274 if (e->type != EXPR_INITIALIZER)
2275 return e;
2277 FOR_EACH_PTR(e->expr_list, p) {
2278 if (!v)
2279 v = p;
2280 count++;
2281 } END_FOR_EACH_PTR(p);
2282 if (count != 1)
2283 return NULL;
2284 switch(v->type) {
2285 case EXPR_INITIALIZER:
2286 case EXPR_INDEX:
2287 case EXPR_IDENTIFIER:
2288 return NULL;
2289 default:
2290 break;
2292 if (nested)
2293 warning(e->pos, "braces around scalar initializer");
2294 return v;
2298 * deal with the cases that don't care about subobjects:
2299 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2300 * character array <- string literal, possibly in braces [6.7.8(14)]
2301 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2302 * compound type <- initializer list in braces [6.7.8(16)]
2303 * The last one punts to handle_list_initializer() which, in turn will call
2304 * us for individual elements of the list.
2306 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2307 * the lack of support of wide char stuff in general.
2309 * One note: we need to take care not to evaluate a string literal until
2310 * we know that we *will* handle it right here. Otherwise we would screw
2311 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2312 * { "string", ...} - we need to preserve that string literal recognizable
2313 * until we dig into the inner struct.
2315 static int handle_simple_initializer(struct expression **ep, int nested,
2316 int class, struct symbol *ctype)
2318 int is_string = is_string_type(ctype);
2319 struct expression *e = *ep, *p;
2320 struct symbol *type;
2322 if (!e)
2323 return 0;
2325 /* scalar */
2326 if (!(class & TYPE_COMPOUND)) {
2327 e = handle_scalar(e, nested);
2328 if (!e)
2329 return 0;
2330 *ep = e;
2331 if (!evaluate_expression(e))
2332 return 1;
2333 compatible_assignment_types(e, ctype, ep, "initializer");
2334 return 1;
2338 * sublist; either a string, or we dig in; the latter will deal with
2339 * pathologies, so we don't need anything fancy here.
2341 if (e->type == EXPR_INITIALIZER) {
2342 if (is_string) {
2343 struct expression *v = NULL;
2344 int count = 0;
2346 FOR_EACH_PTR(e->expr_list, p) {
2347 if (!v)
2348 v = p;
2349 count++;
2350 } END_FOR_EACH_PTR(p);
2351 if (count == 1 && is_string_literal(&v)) {
2352 *ep = e = v;
2353 goto String;
2356 handle_list_initializer(e, class, ctype);
2357 return 1;
2360 /* string */
2361 if (is_string_literal(&e)) {
2362 /* either we are doing array of char, or we'll have to dig in */
2363 if (is_string) {
2364 *ep = e;
2365 goto String;
2367 return 0;
2369 /* struct or union can be initialized by compatible */
2370 if (class != TYPE_COMPOUND)
2371 return 0;
2372 type = evaluate_expression(e);
2373 if (!type)
2374 return 0;
2375 if (ctype->type == SYM_NODE)
2376 ctype = ctype->ctype.base_type;
2377 if (type->type == SYM_NODE)
2378 type = type->ctype.base_type;
2379 if (ctype == type)
2380 return 1;
2381 return 0;
2383 String:
2384 p = alloc_expression(e->pos, EXPR_STRING);
2385 *p = *e;
2386 type = evaluate_expression(p);
2387 if (ctype->bit_size != -1 &&
2388 ctype->bit_size + bits_in_char < type->bit_size) {
2389 warning(e->pos,
2390 "too long initializer-string for array of char");
2392 *ep = p;
2393 return 1;
2396 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2398 struct symbol *type;
2399 int class = classify_type(ctype, &type);
2400 if (!handle_simple_initializer(ep, 0, class, ctype))
2401 expression_error(*ep, "invalid initializer");
2404 static int get_as(struct symbol *sym)
2406 int as;
2407 unsigned long mod;
2409 if (!sym)
2410 return 0;
2411 as = sym->ctype.as;
2412 mod = sym->ctype.modifiers;
2413 if (sym->type == SYM_NODE) {
2414 sym = sym->ctype.base_type;
2415 as |= sym->ctype.as;
2416 mod |= sym->ctype.modifiers;
2420 * At least for now, allow casting to a "unsigned long".
2421 * That's how we do things like pointer arithmetic and
2422 * store pointers to registers.
2424 if (sym == &ulong_ctype)
2425 return -1;
2427 if (sym && sym->type == SYM_PTR) {
2428 sym = get_base_type(sym);
2429 as |= sym->ctype.as;
2430 mod |= sym->ctype.modifiers;
2432 if (mod & MOD_FORCE)
2433 return -1;
2434 return as;
2437 static void cast_to_as(struct expression *e, int as)
2439 struct expression *v = e->cast_expression;
2440 struct symbol *type = v->ctype;
2442 if (!Wcast_to_address_space)
2443 return;
2445 if (v->type != EXPR_VALUE || v->value)
2446 goto out;
2448 /* cast from constant 0 to pointer is OK */
2449 if (is_int_type(type))
2450 return;
2452 if (type->type == SYM_NODE)
2453 type = type->ctype.base_type;
2455 if (type->type == SYM_PTR && type->ctype.base_type == &void_ctype)
2456 return;
2458 out:
2459 warning(e->pos, "cast adds address space to expression (<asn:%d>)", as);
2462 static struct symbol *evaluate_cast(struct expression *expr)
2464 struct expression *target = expr->cast_expression;
2465 struct symbol *ctype;
2466 struct symbol *t1, *t2;
2467 int class1, class2;
2468 int as1, as2;
2470 if (!target)
2471 return NULL;
2474 * Special case: a cast can be followed by an
2475 * initializer, in which case we need to pass
2476 * the type value down to that initializer rather
2477 * than trying to evaluate it as an expression
2479 * A more complex case is when the initializer is
2480 * dereferenced as part of a post-fix expression.
2481 * We need to produce an expression that can be dereferenced.
2483 if (target->type == EXPR_INITIALIZER) {
2484 struct symbol *sym = expr->cast_type;
2485 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2487 sym->initializer = target;
2488 evaluate_symbol(sym);
2490 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2491 addr->symbol = sym;
2493 expr->type = EXPR_PREOP;
2494 expr->op = '*';
2495 expr->unop = addr;
2496 expr->ctype = sym;
2498 return sym;
2501 ctype = examine_symbol_type(expr->cast_type);
2502 expr->ctype = ctype;
2503 expr->cast_type = ctype;
2505 evaluate_expression(target);
2506 degenerate(target);
2508 class1 = classify_type(ctype, &t1);
2510 /* cast to non-integer type -> not an integer constant expression */
2511 if (!is_int(class1))
2512 expr->flags = 0;
2513 /* if argument turns out to be not an integer constant expression *and*
2514 it was not a floating literal to start with -> too bad */
2515 else if (expr->flags == Int_const_expr &&
2516 !(target->flags & Int_const_expr))
2517 expr->flags = 0;
2519 * You can always throw a value away by casting to
2520 * "void" - that's an implicit "force". Note that
2521 * the same is _not_ true of "void *".
2523 if (t1 == &void_ctype)
2524 goto out;
2526 if (class1 & TYPE_COMPOUND)
2527 warning(expr->pos, "cast to non-scalar");
2529 t2 = target->ctype;
2530 if (!t2) {
2531 expression_error(expr, "cast from unknown type");
2532 goto out;
2534 class2 = classify_type(t2, &t2);
2536 if (class2 & TYPE_COMPOUND)
2537 warning(expr->pos, "cast from non-scalar");
2539 /* allowed cast unfouls */
2540 if (class2 & TYPE_FOULED)
2541 t2 = t2->ctype.base_type;
2543 if (!(ctype->ctype.modifiers & MOD_FORCE) && t1 != t2) {
2544 if (class1 & TYPE_RESTRICT)
2545 warning(expr->pos, "cast to restricted type");
2546 if (class2 & TYPE_RESTRICT)
2547 warning(expr->pos, "cast from restricted type");
2550 as1 = get_as(ctype);
2551 as2 = get_as(target->ctype);
2552 if (!as1 && as2 > 0)
2553 warning(expr->pos, "cast removes address space of expression");
2554 if (as1 > 0 && as2 > 0 && as1 != as2)
2555 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2556 if (as1 > 0 && !as2)
2557 cast_to_as(expr, as1);
2560 * Casts of constant values are special: they
2561 * can be NULL, and thus need to be simplified
2562 * early.
2564 if (target->type == EXPR_VALUE)
2565 cast_value(expr, ctype, target, target->ctype);
2567 out:
2568 return ctype;
2572 * Evaluate a call expression with a symbol. This
2573 * should expand inline functions, and evaluate
2574 * builtins.
2576 static int evaluate_symbol_call(struct expression *expr)
2578 struct expression *fn = expr->fn;
2579 struct symbol *ctype = fn->ctype;
2581 if (fn->type != EXPR_PREOP)
2582 return 0;
2584 if (ctype->op && ctype->op->evaluate)
2585 return ctype->op->evaluate(expr);
2587 if (ctype->ctype.modifiers & MOD_INLINE) {
2588 int ret;
2589 struct symbol *curr = current_fn;
2590 current_fn = ctype->ctype.base_type;
2592 ret = inline_function(expr, ctype);
2594 /* restore the old function */
2595 current_fn = curr;
2596 return ret;
2599 return 0;
2602 static struct symbol *evaluate_call(struct expression *expr)
2604 int args, fnargs;
2605 struct symbol *ctype, *sym;
2606 struct expression *fn = expr->fn;
2607 struct expression_list *arglist = expr->args;
2609 if (!evaluate_expression(fn))
2610 return NULL;
2611 sym = ctype = fn->ctype;
2612 if (ctype->type == SYM_NODE)
2613 ctype = ctype->ctype.base_type;
2614 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
2615 ctype = get_base_type(ctype);
2617 examine_fn_arguments(ctype);
2618 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
2619 sym->op && sym->op->args) {
2620 if (!sym->op->args(expr))
2621 return NULL;
2622 } else {
2623 if (!evaluate_arguments(sym, ctype, arglist))
2624 return NULL;
2625 if (ctype->type != SYM_FN) {
2626 expression_error(expr, "not a function %s",
2627 show_ident(sym->ident));
2628 return NULL;
2630 args = expression_list_size(expr->args);
2631 fnargs = symbol_list_size(ctype->arguments);
2632 if (args < fnargs)
2633 expression_error(expr,
2634 "not enough arguments for function %s",
2635 show_ident(sym->ident));
2636 if (args > fnargs && !ctype->variadic)
2637 expression_error(expr,
2638 "too many arguments for function %s",
2639 show_ident(sym->ident));
2641 if (sym->type == SYM_NODE) {
2642 if (evaluate_symbol_call(expr))
2643 return expr->ctype;
2645 expr->ctype = ctype->ctype.base_type;
2646 return expr->ctype;
2649 static struct symbol *evaluate_offsetof(struct expression *expr)
2651 struct expression *e = expr->down;
2652 struct symbol *ctype = expr->in;
2653 int class;
2655 if (expr->op == '.') {
2656 struct symbol *field;
2657 int offset = 0;
2658 if (!ctype) {
2659 expression_error(expr, "expected structure or union");
2660 return NULL;
2662 examine_symbol_type(ctype);
2663 class = classify_type(ctype, &ctype);
2664 if (class != TYPE_COMPOUND) {
2665 expression_error(expr, "expected structure or union");
2666 return NULL;
2669 field = find_identifier(expr->ident, ctype->symbol_list, &offset);
2670 if (!field) {
2671 expression_error(expr, "unknown member");
2672 return NULL;
2674 ctype = field;
2675 expr->type = EXPR_VALUE;
2676 expr->flags = Int_const_expr;
2677 expr->value = offset;
2678 expr->ctype = size_t_ctype;
2679 } else {
2680 if (!ctype) {
2681 expression_error(expr, "expected structure or union");
2682 return NULL;
2684 examine_symbol_type(ctype);
2685 class = classify_type(ctype, &ctype);
2686 if (class != (TYPE_COMPOUND | TYPE_PTR)) {
2687 expression_error(expr, "expected array");
2688 return NULL;
2690 ctype = ctype->ctype.base_type;
2691 if (!expr->index) {
2692 expr->type = EXPR_VALUE;
2693 expr->flags = Int_const_expr;
2694 expr->value = 0;
2695 expr->ctype = size_t_ctype;
2696 } else {
2697 struct expression *idx = expr->index, *m;
2698 struct symbol *i_type = evaluate_expression(idx);
2699 int i_class = classify_type(i_type, &i_type);
2700 if (!is_int(i_class)) {
2701 expression_error(expr, "non-integer index");
2702 return NULL;
2704 unrestrict(idx, i_class, &i_type);
2705 idx = cast_to(idx, size_t_ctype);
2706 m = alloc_const_expression(expr->pos,
2707 ctype->bit_size >> 3);
2708 m->ctype = size_t_ctype;
2709 m->flags = Int_const_expr;
2710 expr->type = EXPR_BINOP;
2711 expr->left = idx;
2712 expr->right = m;
2713 expr->op = '*';
2714 expr->ctype = size_t_ctype;
2715 expr->flags = m->flags & idx->flags & Int_const_expr;
2718 if (e) {
2719 struct expression *copy = __alloc_expression(0);
2720 *copy = *expr;
2721 if (e->type == EXPR_OFFSETOF)
2722 e->in = ctype;
2723 if (!evaluate_expression(e))
2724 return NULL;
2725 expr->type = EXPR_BINOP;
2726 expr->flags = e->flags & copy->flags & Int_const_expr;
2727 expr->op = '+';
2728 expr->ctype = size_t_ctype;
2729 expr->left = copy;
2730 expr->right = e;
2732 return size_t_ctype;
2735 struct symbol *evaluate_expression(struct expression *expr)
2737 if (!expr)
2738 return NULL;
2739 if (expr->ctype)
2740 return expr->ctype;
2742 switch (expr->type) {
2743 case EXPR_VALUE:
2744 case EXPR_FVALUE:
2745 expression_error(expr, "value expression without a type");
2746 return NULL;
2747 case EXPR_STRING:
2748 return evaluate_string(expr);
2749 case EXPR_SYMBOL:
2750 return evaluate_symbol_expression(expr);
2751 case EXPR_BINOP:
2752 if (!evaluate_expression(expr->left))
2753 return NULL;
2754 if (!evaluate_expression(expr->right))
2755 return NULL;
2756 return evaluate_binop(expr);
2757 case EXPR_LOGICAL:
2758 return evaluate_logical(expr);
2759 case EXPR_COMMA:
2760 evaluate_expression(expr->left);
2761 if (!evaluate_expression(expr->right))
2762 return NULL;
2763 return evaluate_comma(expr);
2764 case EXPR_COMPARE:
2765 if (!evaluate_expression(expr->left))
2766 return NULL;
2767 if (!evaluate_expression(expr->right))
2768 return NULL;
2769 return evaluate_compare(expr);
2770 case EXPR_ASSIGNMENT:
2771 if (!evaluate_expression(expr->left))
2772 return NULL;
2773 if (!evaluate_expression(expr->right))
2774 return NULL;
2775 return evaluate_assignment(expr);
2776 case EXPR_PREOP:
2777 if (!evaluate_expression(expr->unop))
2778 return NULL;
2779 return evaluate_preop(expr);
2780 case EXPR_POSTOP:
2781 if (!evaluate_expression(expr->unop))
2782 return NULL;
2783 return evaluate_postop(expr);
2784 case EXPR_CAST:
2785 case EXPR_IMPLIED_CAST:
2786 return evaluate_cast(expr);
2787 case EXPR_SIZEOF:
2788 return evaluate_sizeof(expr);
2789 case EXPR_PTRSIZEOF:
2790 return evaluate_ptrsizeof(expr);
2791 case EXPR_ALIGNOF:
2792 return evaluate_alignof(expr);
2793 case EXPR_DEREF:
2794 return evaluate_member_dereference(expr);
2795 case EXPR_CALL:
2796 return evaluate_call(expr);
2797 case EXPR_SELECT:
2798 case EXPR_CONDITIONAL:
2799 return evaluate_conditional_expression(expr);
2800 case EXPR_STATEMENT:
2801 expr->ctype = evaluate_statement(expr->statement);
2802 return expr->ctype;
2804 case EXPR_LABEL:
2805 expr->ctype = &ptr_ctype;
2806 return &ptr_ctype;
2808 case EXPR_TYPE:
2809 /* Evaluate the type of the symbol .. */
2810 evaluate_symbol(expr->symbol);
2811 /* .. but the type of the _expression_ is a "type" */
2812 expr->ctype = &type_ctype;
2813 return &type_ctype;
2815 case EXPR_OFFSETOF:
2816 return evaluate_offsetof(expr);
2818 /* These can not exist as stand-alone expressions */
2819 case EXPR_INITIALIZER:
2820 case EXPR_IDENTIFIER:
2821 case EXPR_INDEX:
2822 case EXPR_POS:
2823 expression_error(expr, "internal front-end error: initializer in expression");
2824 return NULL;
2825 case EXPR_SLICE:
2826 expression_error(expr, "internal front-end error: SLICE re-evaluated");
2827 return NULL;
2829 return NULL;
2832 static void check_duplicates(struct symbol *sym)
2834 int declared = 0;
2835 struct symbol *next = sym;
2837 while ((next = next->same_symbol) != NULL) {
2838 const char *typediff;
2839 evaluate_symbol(next);
2840 declared++;
2841 typediff = type_difference(sym, next, 0, 0);
2842 if (typediff) {
2843 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2844 show_ident(sym->ident),
2845 stream_name(next->pos.stream), next->pos.line, typediff);
2846 return;
2849 if (!declared) {
2850 unsigned long mod = sym->ctype.modifiers;
2851 if (mod & (MOD_STATIC | MOD_REGISTER))
2852 return;
2853 if (!(mod & MOD_TOPLEVEL))
2854 return;
2855 if (!Wdecl)
2856 return;
2857 if (sym->ident == &main_ident)
2858 return;
2859 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
2863 static struct symbol *evaluate_symbol(struct symbol *sym)
2865 struct symbol *base_type;
2867 if (!sym)
2868 return sym;
2869 if (sym->evaluated)
2870 return sym;
2871 sym->evaluated = 1;
2873 sym = examine_symbol_type(sym);
2874 base_type = get_base_type(sym);
2875 if (!base_type)
2876 return NULL;
2878 /* Evaluate the initializers */
2879 if (sym->initializer)
2880 evaluate_initializer(sym, &sym->initializer);
2882 /* And finally, evaluate the body of the symbol too */
2883 if (base_type->type == SYM_FN) {
2884 struct symbol *curr = current_fn;
2886 current_fn = base_type;
2888 examine_fn_arguments(base_type);
2889 if (!base_type->stmt && base_type->inline_stmt)
2890 uninline(sym);
2891 if (base_type->stmt)
2892 evaluate_statement(base_type->stmt);
2894 current_fn = curr;
2897 return base_type;
2900 void evaluate_symbol_list(struct symbol_list *list)
2902 struct symbol *sym;
2904 FOR_EACH_PTR(list, sym) {
2905 evaluate_symbol(sym);
2906 check_duplicates(sym);
2907 } END_FOR_EACH_PTR(sym);
2910 static struct symbol *evaluate_return_expression(struct statement *stmt)
2912 struct expression *expr = stmt->expression;
2913 struct symbol *fntype;
2915 evaluate_expression(expr);
2916 fntype = current_fn->ctype.base_type;
2917 if (!fntype || fntype == &void_ctype) {
2918 if (expr && expr->ctype != &void_ctype)
2919 expression_error(expr, "return expression in %s function", fntype?"void":"typeless");
2920 if (expr && Wreturn_void)
2921 warning(stmt->pos, "returning void-valued expression");
2922 return NULL;
2925 if (!expr) {
2926 sparse_error(stmt->pos, "return with no return value");
2927 return NULL;
2929 if (!expr->ctype)
2930 return NULL;
2931 compatible_assignment_types(expr, fntype, &stmt->expression, "return expression");
2932 return NULL;
2935 static void evaluate_if_statement(struct statement *stmt)
2937 if (!stmt->if_conditional)
2938 return;
2940 evaluate_conditional(stmt->if_conditional, 0);
2941 evaluate_statement(stmt->if_true);
2942 evaluate_statement(stmt->if_false);
2945 static void evaluate_iterator(struct statement *stmt)
2947 evaluate_conditional(stmt->iterator_pre_condition, 1);
2948 evaluate_conditional(stmt->iterator_post_condition,1);
2949 evaluate_statement(stmt->iterator_pre_statement);
2950 evaluate_statement(stmt->iterator_statement);
2951 evaluate_statement(stmt->iterator_post_statement);
2954 static void verify_output_constraint(struct expression *expr, const char *constraint)
2956 switch (*constraint) {
2957 case '=': /* Assignment */
2958 case '+': /* Update */
2959 break;
2960 default:
2961 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
2965 static void verify_input_constraint(struct expression *expr, const char *constraint)
2967 switch (*constraint) {
2968 case '=': /* Assignment */
2969 case '+': /* Update */
2970 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
2974 static void evaluate_asm_statement(struct statement *stmt)
2976 struct expression *expr;
2977 int state;
2979 expr = stmt->asm_string;
2980 if (!expr || expr->type != EXPR_STRING) {
2981 sparse_error(stmt->pos, "need constant string for inline asm");
2982 return;
2985 state = 0;
2986 FOR_EACH_PTR(stmt->asm_outputs, expr) {
2987 struct ident *ident;
2989 switch (state) {
2990 case 0: /* Identifier */
2991 state = 1;
2992 ident = (struct ident *)expr;
2993 continue;
2995 case 1: /* Constraint */
2996 state = 2;
2997 if (!expr || expr->type != EXPR_STRING) {
2998 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
2999 *THIS_ADDRESS(expr) = NULL;
3000 continue;
3002 verify_output_constraint(expr, expr->string->data);
3003 continue;
3005 case 2: /* Expression */
3006 state = 0;
3007 if (!evaluate_expression(expr))
3008 return;
3009 if (!lvalue_expression(expr))
3010 warning(expr->pos, "asm output is not an lvalue");
3011 evaluate_assign_to(expr, expr->ctype);
3012 continue;
3014 } END_FOR_EACH_PTR(expr);
3016 state = 0;
3017 FOR_EACH_PTR(stmt->asm_inputs, expr) {
3018 struct ident *ident;
3020 switch (state) {
3021 case 0: /* Identifier */
3022 state = 1;
3023 ident = (struct ident *)expr;
3024 continue;
3026 case 1: /* Constraint */
3027 state = 2;
3028 if (!expr || expr->type != EXPR_STRING) {
3029 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
3030 *THIS_ADDRESS(expr) = NULL;
3031 continue;
3033 verify_input_constraint(expr, expr->string->data);
3034 continue;
3036 case 2: /* Expression */
3037 state = 0;
3038 if (!evaluate_expression(expr))
3039 return;
3040 continue;
3042 } END_FOR_EACH_PTR(expr);
3044 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
3045 if (!expr) {
3046 sparse_error(stmt->pos, "bad asm output");
3047 return;
3049 if (expr->type == EXPR_STRING)
3050 continue;
3051 expression_error(expr, "asm clobber is not a string");
3052 } END_FOR_EACH_PTR(expr);
3055 static void evaluate_case_statement(struct statement *stmt)
3057 evaluate_expression(stmt->case_expression);
3058 evaluate_expression(stmt->case_to);
3059 evaluate_statement(stmt->case_statement);
3062 static void check_case_type(struct expression *switch_expr,
3063 struct expression *case_expr,
3064 struct expression **enumcase)
3066 struct symbol *switch_type, *case_type;
3067 int sclass, cclass;
3069 if (!case_expr)
3070 return;
3072 switch_type = switch_expr->ctype;
3073 case_type = evaluate_expression(case_expr);
3075 if (!switch_type || !case_type)
3076 goto Bad;
3077 if (enumcase) {
3078 if (*enumcase)
3079 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
3080 else if (is_enum_type(case_type))
3081 *enumcase = case_expr;
3084 sclass = classify_type(switch_type, &switch_type);
3085 cclass = classify_type(case_type, &case_type);
3087 /* both should be arithmetic */
3088 if (!(sclass & cclass & TYPE_NUM))
3089 goto Bad;
3091 /* neither should be floating */
3092 if ((sclass | cclass) & TYPE_FLOAT)
3093 goto Bad;
3095 /* if neither is restricted, we are OK */
3096 if (!((sclass | cclass) & TYPE_RESTRICT))
3097 return;
3099 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
3100 cclass, sclass, case_type, switch_type))
3101 warning(case_expr->pos, "restricted degrades to integer");
3103 return;
3105 Bad:
3106 expression_error(case_expr, "incompatible types for 'case' statement");
3109 static void evaluate_switch_statement(struct statement *stmt)
3111 struct symbol *sym;
3112 struct expression *enumcase = NULL;
3113 struct expression **enumcase_holder = &enumcase;
3114 struct expression *sel = stmt->switch_expression;
3116 evaluate_expression(sel);
3117 evaluate_statement(stmt->switch_statement);
3118 if (!sel)
3119 return;
3120 if (sel->ctype && is_enum_type(sel->ctype))
3121 enumcase_holder = NULL; /* Only check cases against switch */
3123 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
3124 struct statement *case_stmt = sym->stmt;
3125 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
3126 check_case_type(sel, case_stmt->case_to, enumcase_holder);
3127 } END_FOR_EACH_PTR(sym);
3130 struct symbol *evaluate_statement(struct statement *stmt)
3132 if (!stmt)
3133 return NULL;
3135 switch (stmt->type) {
3136 case STMT_DECLARATION: {
3137 struct symbol *s;
3138 FOR_EACH_PTR(stmt->declaration, s) {
3139 evaluate_symbol(s);
3140 } END_FOR_EACH_PTR(s);
3141 return NULL;
3144 case STMT_RETURN:
3145 return evaluate_return_expression(stmt);
3147 case STMT_EXPRESSION:
3148 if (!evaluate_expression(stmt->expression))
3149 return NULL;
3150 return degenerate(stmt->expression);
3152 case STMT_COMPOUND: {
3153 struct statement *s;
3154 struct symbol *type = NULL;
3156 /* Evaluate the return symbol in the compound statement */
3157 evaluate_symbol(stmt->ret);
3160 * Then, evaluate each statement, making the type of the
3161 * compound statement be the type of the last statement
3163 type = evaluate_statement(stmt->args);
3164 FOR_EACH_PTR(stmt->stmts, s) {
3165 type = evaluate_statement(s);
3166 } END_FOR_EACH_PTR(s);
3167 if (!type)
3168 type = &void_ctype;
3169 return type;
3171 case STMT_IF:
3172 evaluate_if_statement(stmt);
3173 return NULL;
3174 case STMT_ITERATOR:
3175 evaluate_iterator(stmt);
3176 return NULL;
3177 case STMT_SWITCH:
3178 evaluate_switch_statement(stmt);
3179 return NULL;
3180 case STMT_CASE:
3181 evaluate_case_statement(stmt);
3182 return NULL;
3183 case STMT_LABEL:
3184 return evaluate_statement(stmt->label_statement);
3185 case STMT_GOTO:
3186 evaluate_expression(stmt->goto_expression);
3187 return NULL;
3188 case STMT_NONE:
3189 break;
3190 case STMT_ASM:
3191 evaluate_asm_statement(stmt);
3192 return NULL;
3193 case STMT_CONTEXT:
3194 evaluate_expression(stmt->expression);
3195 return NULL;
3196 case STMT_RANGE:
3197 evaluate_expression(stmt->range_expression);
3198 evaluate_expression(stmt->range_low);
3199 evaluate_expression(stmt->range_high);
3200 return NULL;
3202 return NULL;