[PATCH] mechanically split compatible_assignment_types()
[smatch.git] / evaluate.c
blobfebfad28040f7f07aff5e30de33aaeec5b22ba09
1 /*
2 * sparse/evaluate.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
9 * Evaluate constant expressions.
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <stddef.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <limits.h>
21 #include "lib.h"
22 #include "allocate.h"
23 #include "parse.h"
24 #include "token.h"
25 #include "symbol.h"
26 #include "target.h"
27 #include "expression.h"
29 struct symbol *current_fn;
31 static struct symbol *degenerate(struct expression *expr);
32 static struct symbol *evaluate_symbol(struct symbol *sym);
34 static struct symbol *evaluate_symbol_expression(struct expression *expr)
36 struct expression *addr;
37 struct symbol *sym = expr->symbol;
38 struct symbol *base_type;
40 if (!sym) {
41 expression_error(expr, "undefined identifier '%s'", show_ident(expr->symbol_name));
42 return NULL;
45 examine_symbol_type(sym);
47 base_type = get_base_type(sym);
48 if (!base_type) {
49 expression_error(expr, "identifier '%s' has no type", show_ident(expr->symbol_name));
50 return NULL;
53 addr = alloc_expression(expr->pos, EXPR_SYMBOL);
54 addr->symbol = sym;
55 addr->symbol_name = expr->symbol_name;
56 addr->ctype = &lazy_ptr_ctype; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
57 expr->type = EXPR_PREOP;
58 expr->op = '*';
59 expr->unop = addr;
61 /* The type of a symbol is the symbol itself! */
62 expr->ctype = sym;
63 return sym;
66 static struct symbol *evaluate_string(struct expression *expr)
68 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
69 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
70 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
71 struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
72 unsigned int length = expr->string->length;
74 sym->array_size = alloc_const_expression(expr->pos, length);
75 sym->bit_size = bits_in_char * length;
76 sym->ctype.alignment = 1;
77 sym->string = 1;
78 sym->ctype.modifiers = MOD_STATIC;
79 sym->ctype.base_type = array;
80 sym->initializer = initstr;
82 initstr->ctype = sym;
83 initstr->string = expr->string;
85 array->array_size = sym->array_size;
86 array->bit_size = bits_in_char * length;
87 array->ctype.alignment = 1;
88 array->ctype.modifiers = MOD_STATIC;
89 array->ctype.base_type = &char_ctype;
91 addr->symbol = sym;
92 addr->ctype = &lazy_ptr_ctype;
94 expr->type = EXPR_PREOP;
95 expr->op = '*';
96 expr->unop = addr;
97 expr->ctype = sym;
98 return sym;
101 static inline struct symbol *integer_promotion(struct symbol *type)
103 struct symbol *orig_type = type;
104 unsigned long mod = type->ctype.modifiers;
105 int width;
107 if (type->type == SYM_NODE)
108 type = type->ctype.base_type;
109 if (type->type == SYM_ENUM)
110 type = type->ctype.base_type;
111 width = type->bit_size;
114 * Bitfields always promote to the base type,
115 * even if the bitfield might be bigger than
116 * an "int".
118 if (type->type == SYM_BITFIELD) {
119 type = type->ctype.base_type;
120 orig_type = type;
122 mod = type->ctype.modifiers;
123 if (width < bits_in_int)
124 return &int_ctype;
126 /* If char/short has as many bits as int, it still gets "promoted" */
127 if (mod & (MOD_CHAR | MOD_SHORT)) {
128 if (mod & MOD_UNSIGNED)
129 return &uint_ctype;
130 return &int_ctype;
132 return orig_type;
136 * integer part of usual arithmetic conversions:
137 * integer promotions are applied
138 * if left and right are identical, we are done
139 * if signedness is the same, convert one with lower rank
140 * unless unsigned argument has rank lower than signed one, convert the
141 * signed one.
142 * if signed argument is bigger than unsigned one, convert the unsigned.
143 * otherwise, convert signed.
145 * Leaving aside the integer promotions, that is equivalent to
146 * if identical, don't convert
147 * if left is bigger than right, convert right
148 * if right is bigger than left, convert right
149 * otherwise, if signedness is the same, convert one with lower rank
150 * otherwise convert the signed one.
152 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
154 unsigned long lmod, rmod;
156 left = integer_promotion(left);
157 right = integer_promotion(right);
159 if (left == right)
160 goto left;
162 if (left->bit_size > right->bit_size)
163 goto left;
165 if (right->bit_size > left->bit_size)
166 goto right;
168 lmod = left->ctype.modifiers;
169 rmod = right->ctype.modifiers;
170 if ((lmod ^ rmod) & MOD_UNSIGNED) {
171 if (lmod & MOD_UNSIGNED)
172 goto left;
173 } else if ((lmod & ~rmod) & (MOD_LONG | MOD_LONGLONG))
174 goto left;
175 right:
176 left = right;
177 left:
178 return left;
181 static int same_cast_type(struct symbol *orig, struct symbol *new)
183 return orig->bit_size == new->bit_size && orig->bit_offset == new->bit_offset;
186 static struct symbol *base_type(struct symbol *node, unsigned long *modp, unsigned long *asp)
188 unsigned long mod, as;
190 mod = 0; as = 0;
191 while (node) {
192 mod |= node->ctype.modifiers;
193 as |= node->ctype.as;
194 if (node->type == SYM_NODE) {
195 node = node->ctype.base_type;
196 continue;
198 break;
200 *modp = mod & ~MOD_IGNORE;
201 *asp = as;
202 return node;
205 static int is_same_type(struct expression *expr, struct symbol *new)
207 struct symbol *old = expr->ctype;
208 unsigned long oldmod, newmod, oldas, newas;
210 old = base_type(old, &oldmod, &oldas);
211 new = base_type(new, &newmod, &newas);
213 /* Same base type, same address space? */
214 if (old == new && oldas == newas) {
215 unsigned long difmod;
217 /* Check the modifier bits. */
218 difmod = (oldmod ^ newmod) & ~MOD_NOCAST;
220 /* Exact same type? */
221 if (!difmod)
222 return 1;
225 * Not the same type, but differs only in "const".
226 * Don't warn about MOD_NOCAST.
228 if (difmod == MOD_CONST)
229 return 0;
231 if ((oldmod | newmod) & MOD_NOCAST) {
232 const char *tofrom = "to/from";
233 if (!(newmod & MOD_NOCAST))
234 tofrom = "from";
235 if (!(oldmod & MOD_NOCAST))
236 tofrom = "to";
237 warning(expr->pos, "implicit cast %s nocast type", tofrom);
239 return 0;
242 static void
243 warn_for_different_enum_types (struct position pos,
244 struct symbol *typea,
245 struct symbol *typeb)
247 if (!Wenum_mismatch)
248 return;
249 if (typea->type == SYM_NODE)
250 typea = typea->ctype.base_type;
251 if (typeb->type == SYM_NODE)
252 typeb = typeb->ctype.base_type;
254 if (typea == typeb)
255 return;
257 if (typea->type == SYM_ENUM && typeb->type == SYM_ENUM) {
258 warning(pos, "mixing different enum types");
259 info(pos, " %s versus", show_typename(typea));
260 info(pos, " %s", show_typename(typeb));
265 * This gets called for implicit casts in assignments and
266 * integer promotion. We often want to try to move the
267 * cast down, because the ops involved may have been
268 * implicitly cast up, and we can get rid of the casts
269 * early.
271 static struct expression * cast_to(struct expression *old, struct symbol *type)
273 struct expression *expr;
275 warn_for_different_enum_types (old->pos, old->ctype, type);
277 if (is_same_type(old, type))
278 return old;
281 * See if we can simplify the op. Move the cast down.
283 switch (old->type) {
284 case EXPR_PREOP:
285 if (old->ctype->bit_size < type->bit_size)
286 break;
287 if (old->op == '~') {
288 old->ctype = type;
289 old->unop = cast_to(old->unop, type);
290 return old;
292 break;
294 case EXPR_IMPLIED_CAST:
295 warn_for_different_enum_types(old->pos, old->ctype, type);
297 if (old->ctype->bit_size >= type->bit_size) {
298 struct expression *orig = old->cast_expression;
299 if (same_cast_type(orig->ctype, type))
300 return orig;
301 if (old->ctype->bit_offset == type->bit_offset) {
302 old->ctype = type;
303 old->cast_type = type;
304 return old;
307 break;
309 default:
310 /* nothing */;
313 expr = alloc_expression(old->pos, EXPR_IMPLIED_CAST);
314 expr->ctype = type;
315 expr->cast_type = type;
316 expr->cast_expression = old;
317 return expr;
320 static int is_type_type(struct symbol *type)
322 return (type->ctype.modifiers & MOD_TYPE) != 0;
325 int is_ptr_type(struct symbol *type)
327 if (type->type == SYM_NODE)
328 type = type->ctype.base_type;
329 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
332 static inline int is_float_type(struct symbol *type)
334 if (type->type == SYM_NODE)
335 type = type->ctype.base_type;
336 return type->ctype.base_type == &fp_type;
339 static inline int is_byte_type(struct symbol *type)
341 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
344 enum {
345 TYPE_NUM = 1,
346 TYPE_BITFIELD = 2,
347 TYPE_RESTRICT = 4,
348 TYPE_FLOAT = 8,
349 TYPE_PTR = 16,
350 TYPE_COMPOUND = 32,
351 TYPE_FOULED = 64,
354 static inline int classify_type(struct symbol *type, struct symbol **base)
356 static int type_class[SYM_BAD + 1] = {
357 [SYM_PTR] = TYPE_PTR,
358 [SYM_FN] = TYPE_PTR,
359 [SYM_ARRAY] = TYPE_PTR | TYPE_COMPOUND,
360 [SYM_STRUCT] = TYPE_COMPOUND,
361 [SYM_UNION] = TYPE_COMPOUND,
362 [SYM_BITFIELD] = TYPE_NUM | TYPE_BITFIELD,
363 [SYM_RESTRICT] = TYPE_NUM | TYPE_RESTRICT,
364 [SYM_FOULED] = TYPE_NUM | TYPE_RESTRICT | TYPE_FOULED,
366 if (type->type == SYM_NODE)
367 type = type->ctype.base_type;
368 if (type->type == SYM_ENUM)
369 type = type->ctype.base_type;
370 *base = type;
371 if (type->type == SYM_BASETYPE) {
372 if (type->ctype.base_type == &int_type)
373 return TYPE_NUM;
374 if (type->ctype.base_type == &fp_type)
375 return TYPE_NUM | TYPE_FLOAT;
377 return type_class[type->type];
380 static inline int is_string_type(struct symbol *type)
382 if (type->type == SYM_NODE)
383 type = type->ctype.base_type;
384 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
387 static struct symbol *bad_expr_type(struct expression *expr)
389 sparse_error(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
390 switch (expr->type) {
391 case EXPR_BINOP:
392 case EXPR_COMPARE:
393 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
394 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
395 break;
396 case EXPR_PREOP:
397 case EXPR_POSTOP:
398 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
399 break;
400 default:
401 break;
404 return expr->ctype = &bad_ctype;
407 static int restricted_value(struct expression *v, struct symbol *type)
409 if (v->type != EXPR_VALUE)
410 return 1;
411 if (v->value != 0)
412 return 1;
413 return 0;
416 static int restricted_binop(int op, struct symbol *type)
418 switch (op) {
419 case '&':
420 case '=':
421 case SPECIAL_AND_ASSIGN:
422 case SPECIAL_OR_ASSIGN:
423 case SPECIAL_XOR_ASSIGN:
424 return 1; /* unfoul */
425 case '|':
426 case '^':
427 case '?':
428 return 2; /* keep fouled */
429 case SPECIAL_EQUAL:
430 case SPECIAL_NOTEQUAL:
431 return 3; /* warn if fouled */
432 default:
433 return 0; /* warn */
437 static int restricted_unop(int op, struct symbol **type)
439 if (op == '~') {
440 if ((*type)->bit_size < bits_in_int)
441 *type = befoul(*type);
442 return 0;
443 } if (op == '+')
444 return 0;
445 return 1;
448 static struct symbol *restricted_binop_type(int op,
449 struct expression *left,
450 struct expression *right,
451 int lclass, int rclass,
452 struct symbol *ltype,
453 struct symbol *rtype)
455 struct symbol *ctype = NULL;
456 if (lclass & TYPE_RESTRICT) {
457 if (rclass & TYPE_RESTRICT) {
458 if (ltype == rtype) {
459 ctype = ltype;
460 } else if (lclass & TYPE_FOULED) {
461 if (ltype->ctype.base_type == rtype)
462 ctype = ltype;
463 } else if (rclass & TYPE_FOULED) {
464 if (rtype->ctype.base_type == ltype)
465 ctype = rtype;
467 } else {
468 if (!restricted_value(right, ltype))
469 ctype = ltype;
471 } else if (!restricted_value(left, rtype))
472 ctype = rtype;
474 if (ctype) {
475 switch (restricted_binop(op, ctype)) {
476 case 1:
477 if ((lclass ^ rclass) & TYPE_FOULED)
478 ctype = ctype->ctype.base_type;
479 break;
480 case 3:
481 if (!(lclass & rclass & TYPE_FOULED))
482 break;
483 case 0:
484 ctype = NULL;
485 default:
486 break;
490 return ctype;
493 static struct symbol *usual_conversions(int op,
494 struct expression *left,
495 struct expression *right,
496 int lclass, int rclass,
497 struct symbol *ltype,
498 struct symbol *rtype)
500 struct symbol *ctype;
502 warn_for_different_enum_types(right->pos, left->ctype, right->ctype);
504 if ((lclass | rclass) & TYPE_RESTRICT)
505 goto Restr;
507 Normal:
508 if (!(lclass & TYPE_FLOAT)) {
509 if (!(rclass & TYPE_FLOAT))
510 ctype = bigger_int_type(ltype, rtype);
511 else
512 ctype = rtype;
513 } else if (rclass & TYPE_FLOAT) {
514 unsigned long lmod = ltype->ctype.modifiers;
515 unsigned long rmod = rtype->ctype.modifiers;
516 if (rmod & ~lmod & (MOD_LONG | MOD_LONGLONG))
517 ctype = rtype;
518 else
519 ctype = ltype;
520 } else
521 ctype = ltype;
523 Convert:
524 return ctype;
526 Restr:
527 ctype = restricted_binop_type(op, left, right,
528 lclass, rclass, ltype, rtype);
529 if (ctype)
530 goto Convert;
532 if (lclass & TYPE_RESTRICT) {
533 warning(left->pos, "restricted degrades to integer");
534 if (lclass & TYPE_FOULED)
535 ltype = ltype->ctype.base_type;
536 ltype = ltype->ctype.base_type;
538 if (rclass & TYPE_RESTRICT) {
539 warning(right->pos, "restricted degrades to integer");
540 if (rclass & TYPE_FOULED)
541 rtype = rtype->ctype.base_type;
542 rtype = rtype->ctype.base_type;
544 goto Normal;
547 static struct symbol *evaluate_arith(struct expression *expr, int float_ok)
549 struct symbol *ltype, *rtype;
550 int lclass = classify_type(expr->left->ctype, &ltype);
551 int rclass = classify_type(expr->right->ctype, &rtype);
552 struct symbol *ctype;
554 if (!(lclass & rclass & TYPE_NUM))
555 goto Bad;
557 if (!float_ok && (lclass | rclass) & TYPE_FLOAT)
558 goto Bad;
560 ctype = usual_conversions(expr->op, expr->left, expr->right,
561 lclass, rclass, ltype, rtype);
562 expr->left = cast_to(expr->left, ctype);
563 expr->right = cast_to(expr->right, ctype);
564 expr->ctype = ctype;
565 return ctype;
567 Bad:
568 return bad_expr_type(expr);
571 static inline int lvalue_expression(struct expression *expr)
573 return expr->type == EXPR_PREOP && expr->op == '*';
576 static int ptr_object_size(struct symbol *ptr_type)
578 if (ptr_type->type == SYM_NODE)
579 ptr_type = ptr_type->ctype.base_type;
580 if (ptr_type->type == SYM_PTR)
581 ptr_type = get_base_type(ptr_type);
582 return ptr_type->bit_size;
585 static inline int want_int(struct expression **expr, struct symbol **ctype)
587 int class = classify_type((*expr)->ctype, ctype);
589 if (!(class & TYPE_NUM))
590 return 0;
591 if (!(class & TYPE_RESTRICT))
592 return 1;
593 warning((*expr)->pos, "restricted degrades to integer");
594 if (class & TYPE_FOULED) /* unfoul it first */
595 (*ctype) = (*ctype)->ctype.base_type;
596 (*ctype) = (*ctype)->ctype.base_type; /* get to arithmetic type */
597 *expr = cast_to(*expr, *ctype);
598 return 1;
601 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *ctype, struct expression **ip)
603 struct expression *i = *ip;
604 struct symbol *ptr_type = ctype, *itype;
605 int bit_size;
607 if (ptr_type->type == SYM_NODE)
608 ptr_type = ptr_type->ctype.base_type;
610 if (!want_int(&i, &itype))
611 return bad_expr_type(expr);
613 examine_symbol_type(ctype);
615 if (!ctype->ctype.base_type) {
616 expression_error(expr, "missing type information");
617 return NULL;
620 /* Get the size of whatever the pointer points to */
621 bit_size = ptr_object_size(ctype);
623 if (bit_size > bits_in_char) {
624 int multiply = bit_size >> 3;
625 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
627 if (i->type == EXPR_VALUE) {
628 val->value = i->value * multiply;
629 val->ctype = size_t_ctype;
630 *ip = val;
631 } else {
632 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
634 val->ctype = size_t_ctype;
635 val->value = bit_size >> 3;
637 mul->op = '*';
638 mul->ctype = size_t_ctype;
639 mul->left = i;
640 mul->right = val;
642 *ip = mul;
646 expr->ctype = ctype;
647 return ctype;
650 static struct symbol *evaluate_add(struct expression *expr)
652 struct expression *left = expr->left, *right = expr->right;
653 struct symbol *ltype = left->ctype, *rtype = right->ctype;
655 if (is_ptr_type(ltype))
656 return evaluate_ptr_add(expr, degenerate(left), &expr->right);
658 if (is_ptr_type(rtype))
659 return evaluate_ptr_add(expr, degenerate(right), &expr->left);
661 return evaluate_arith(expr, 1);
664 const char * type_difference(struct symbol *target, struct symbol *source,
665 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
667 for (;;) {
668 unsigned long mod1, mod2, diff;
669 unsigned long as1, as2;
670 int type1, type2;
671 struct symbol *base1, *base2;
673 if (target == source)
674 break;
675 if (!target || !source)
676 return "different types";
678 * Peel of per-node information.
679 * FIXME! Check alignment and context too here!
681 mod1 = target->ctype.modifiers;
682 as1 = target->ctype.as;
683 mod2 = source->ctype.modifiers;
684 as2 = source->ctype.as;
685 if (target->type == SYM_NODE) {
686 target = target->ctype.base_type;
687 if (!target)
688 return "bad types";
689 if (target->type == SYM_PTR) {
690 mod1 = 0;
691 as1 = 0;
693 mod1 |= target->ctype.modifiers;
694 as1 |= target->ctype.as;
696 if (source->type == SYM_NODE) {
697 source = source->ctype.base_type;
698 if (!source)
699 return "bad types";
700 if (source->type == SYM_PTR) {
701 mod2 = 0;
702 as2 = 0;
704 mod2 |= source->ctype.modifiers;
705 as2 |= source->ctype.as;
707 if (target->type == SYM_ENUM) {
708 target = target->ctype.base_type;
709 if (!target)
710 return "bad types";
712 if (source->type == SYM_ENUM) {
713 source = source->ctype.base_type;
714 if (!source)
715 return "bad types";
718 if (target == source)
719 break;
720 if (!target || !source)
721 return "different types";
723 type1 = target->type;
724 base1 = target->ctype.base_type;
726 type2 = source->type;
727 base2 = source->ctype.base_type;
730 * Pointers to functions compare as the function itself
732 if (type1 == SYM_PTR && base1) {
733 base1 = examine_symbol_type(base1);
734 switch (base1->type) {
735 case SYM_FN:
736 type1 = SYM_FN;
737 target = base1;
738 base1 = base1->ctype.base_type;
739 default:
740 /* nothing */;
743 if (type2 == SYM_PTR && base2) {
744 base2 = examine_symbol_type(base2);
745 switch (base2->type) {
746 case SYM_FN:
747 type2 = SYM_FN;
748 source = base2;
749 base2 = base2->ctype.base_type;
750 default:
751 /* nothing */;
755 /* Arrays degenerate to pointers for type comparisons */
756 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
757 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
759 if (type1 != type2 || type1 == SYM_RESTRICT)
760 return "different base types";
762 /* Must be same address space to be comparable */
763 if (Waddress_space && as1 != as2)
764 return "different address spaces";
766 /* Ignore differences in storage types or addressability */
767 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
768 diff &= (mod1 & ~target_mod_ignore) | (mod2 & ~source_mod_ignore);
769 if (diff) {
770 if (diff & MOD_SIZE)
771 return "different type sizes";
772 if (diff & ~MOD_SIGNEDNESS)
773 return "different modifiers";
775 /* Differs in signedness only.. */
776 if (Wtypesign) {
778 * Warn if both are explicitly signed ("unsigned" is obviously
779 * always explicit, and since we know one of them has to be
780 * unsigned, we check if the signed one was explicit).
782 if ((mod1 | mod2) & MOD_EXPLICITLY_SIGNED)
783 return "different explicit signedness";
786 * "char" matches both "unsigned char" and "signed char",
787 * so if the explicit test didn't trigger, then we should
788 * not warn about a char.
790 if (!(mod1 & MOD_CHAR))
791 return "different signedness";
795 if (type1 == SYM_FN) {
796 int i;
797 struct symbol *arg1, *arg2;
798 if (base1->variadic != base2->variadic)
799 return "incompatible variadic arguments";
800 PREPARE_PTR_LIST(target->arguments, arg1);
801 PREPARE_PTR_LIST(source->arguments, arg2);
802 i = 1;
803 for (;;) {
804 const char *diffstr;
805 diffstr = type_difference(arg1, arg2, 0, 0);
806 if (diffstr) {
807 static char argdiff[80];
808 sprintf(argdiff, "incompatible argument %d (%s)", i, diffstr);
809 return argdiff;
811 if (!arg1)
812 break;
813 NEXT_PTR_LIST(arg1);
814 NEXT_PTR_LIST(arg2);
815 i++;
817 FINISH_PTR_LIST(arg2);
818 FINISH_PTR_LIST(arg1);
821 target = base1;
822 source = base2;
824 return NULL;
827 static int is_null_ptr(struct expression *expr)
829 if (expr->type != EXPR_VALUE || expr->value)
830 return 0;
831 if (Wnon_pointer_null && !is_ptr_type(expr->ctype))
832 warning(expr->pos, "Using plain integer as NULL pointer");
833 return 1;
836 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
838 /* NULL expression? Just return the type of the "other side" */
839 if (is_null_ptr(r))
840 return l->ctype;
841 if (is_null_ptr(l))
842 return r->ctype;
843 return NULL;
847 * Ignore differences in "volatile" and "const"ness when
848 * subtracting pointers
850 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
852 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression **rp)
854 const char *typediff;
855 struct symbol *ctype;
856 struct symbol *ltype, *rtype;
857 struct expression *r = *rp;
859 ltype = degenerate(l);
860 rtype = degenerate(r);
863 * If it is an integer subtract: the ptr add case will do the
864 * right thing.
866 if (!is_ptr_type(rtype))
867 return evaluate_ptr_add(expr, degenerate(l), rp);
869 ctype = ltype;
870 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
871 if (typediff) {
872 ctype = common_ptr_type(l, r);
873 if (!ctype) {
874 expression_error(expr, "subtraction of different types can't work (%s)", typediff);
875 return NULL;
878 examine_symbol_type(ctype);
880 /* Figure out the base type we point to */
881 if (ctype->type == SYM_NODE)
882 ctype = ctype->ctype.base_type;
883 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
884 expression_error(expr, "subtraction of functions? Share your drugs");
885 return NULL;
887 ctype = get_base_type(ctype);
889 expr->ctype = ssize_t_ctype;
890 if (ctype->bit_size > bits_in_char) {
891 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
892 struct expression *div = expr;
893 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
894 unsigned long value = ctype->bit_size >> 3;
896 val->ctype = size_t_ctype;
897 val->value = value;
899 if (value & (value-1)) {
900 if (Wptr_subtraction_blows)
901 warning(expr->pos, "potentially expensive pointer subtraction");
904 sub->op = '-';
905 sub->ctype = ssize_t_ctype;
906 sub->left = l;
907 sub->right = r;
909 div->op = '/';
910 div->left = sub;
911 div->right = val;
914 return ssize_t_ctype;
917 static struct symbol *evaluate_sub(struct expression *expr)
919 struct expression *left = expr->left;
920 struct symbol *ltype = left->ctype;
922 if (is_ptr_type(ltype))
923 return evaluate_ptr_sub(expr, left, &expr->right);
925 return evaluate_arith(expr, 1);
928 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
930 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
932 struct symbol *ctype;
934 if (!expr)
935 return NULL;
937 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
938 warning(expr->pos, "assignment expression in conditional");
940 ctype = evaluate_expression(expr);
941 if (ctype) {
942 if (is_safe_type(ctype))
943 warning(expr->pos, "testing a 'safe expression'");
946 return ctype;
949 static struct symbol *evaluate_logical(struct expression *expr)
951 if (!evaluate_conditional(expr->left, 0))
952 return NULL;
953 if (!evaluate_conditional(expr->right, 0))
954 return NULL;
956 expr->ctype = &bool_ctype;
957 return &bool_ctype;
960 static struct symbol *evaluate_shift(struct expression *expr)
962 struct symbol *ltype, *rtype;
964 if (want_int(&expr->left, &ltype) && want_int(&expr->right, &rtype)) {
965 struct symbol *ctype = integer_promotion(ltype);
966 expr->left = cast_to(expr->left, ctype);
967 expr->ctype = ctype;
968 ctype = integer_promotion(rtype);
969 expr->right = cast_to(expr->right, ctype);
970 return expr->ctype;
972 return bad_expr_type(expr);
975 static struct symbol *evaluate_binop(struct expression *expr)
977 switch (expr->op) {
978 // addition can take ptr+int, fp and int
979 case '+':
980 return evaluate_add(expr);
982 // subtraction can take ptr-ptr, fp and int
983 case '-':
984 return evaluate_sub(expr);
986 // Arithmetic operations can take fp and int
987 case '*': case '/':
988 return evaluate_arith(expr, 1);
990 // shifts do integer promotions, but that's it.
991 case SPECIAL_LEFTSHIFT: case SPECIAL_RIGHTSHIFT:
992 return evaluate_shift(expr);
994 // The rest are integer operations
995 // '%', '&', '^', '|'
996 default:
997 return evaluate_arith(expr, 0);
1001 static struct symbol *evaluate_comma(struct expression *expr)
1003 expr->ctype = expr->right->ctype;
1004 return expr->ctype;
1007 static int modify_for_unsigned(int op)
1009 if (op == '<')
1010 op = SPECIAL_UNSIGNED_LT;
1011 else if (op == '>')
1012 op = SPECIAL_UNSIGNED_GT;
1013 else if (op == SPECIAL_LTE)
1014 op = SPECIAL_UNSIGNED_LTE;
1015 else if (op == SPECIAL_GTE)
1016 op = SPECIAL_UNSIGNED_GTE;
1017 return op;
1020 static struct symbol *evaluate_compare(struct expression *expr)
1022 struct expression *left = expr->left, *right = expr->right;
1023 struct symbol *ltype = left->ctype, *rtype = right->ctype;
1024 struct symbol *ctype;
1026 /* Type types? */
1027 if (is_type_type(ltype) && is_type_type(rtype))
1028 goto OK;
1030 if (is_safe_type(ltype) || is_safe_type(rtype))
1031 warning(expr->pos, "testing a 'safe expression'");
1033 /* Pointer types? */
1034 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
1035 // FIXME! Check the types for compatibility
1036 expr->op = modify_for_unsigned(expr->op);
1037 goto OK;
1040 ctype = evaluate_arith(expr, 1);
1041 if (ctype) {
1042 if (ctype->ctype.modifiers & MOD_UNSIGNED)
1043 expr->op = modify_for_unsigned(expr->op);
1046 expr->ctype = &bool_ctype;
1047 return &bool_ctype;
1051 * FIXME!! This should do casts, array degeneration etc..
1053 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
1055 struct symbol *ltype = left->ctype, *rtype = right->ctype;
1057 if (ltype->type == SYM_NODE)
1058 ltype = ltype->ctype.base_type;
1060 if (rtype->type == SYM_NODE)
1061 rtype = rtype->ctype.base_type;
1063 if (ltype->type == SYM_PTR) {
1064 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
1065 return ltype;
1068 if (rtype->type == SYM_PTR) {
1069 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
1070 return rtype;
1072 return NULL;
1076 * NOTE! The degenerate case of "x ? : y", where we don't
1077 * have a true case, this will possibly promote "x" to the
1078 * same type as "y", and thus _change_ the conditional
1079 * test in the expression. But since promotion is "safe"
1080 * for testing, that's OK.
1082 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1084 struct expression **true;
1085 struct symbol *ctype, *ltype, *rtype;
1086 int lclass, rclass;
1087 const char * typediff;
1089 if (!evaluate_conditional(expr->conditional, 0))
1090 return NULL;
1091 if (!evaluate_expression(expr->cond_false))
1092 return NULL;
1094 ctype = degenerate(expr->conditional);
1095 rtype = degenerate(expr->cond_false);
1097 true = &expr->conditional;
1098 ltype = ctype;
1099 if (expr->cond_true) {
1100 if (!evaluate_expression(expr->cond_true))
1101 return NULL;
1102 ltype = degenerate(expr->cond_true);
1103 true = &expr->cond_true;
1106 lclass = classify_type(ltype, &ltype);
1107 rclass = classify_type(rtype, &rtype);
1108 if (lclass & rclass & TYPE_NUM) {
1109 ctype = usual_conversions('?', *true, expr->cond_false,
1110 lclass, rclass, ltype, rtype);
1111 *true = cast_to(*true, ctype);
1112 expr->cond_false = cast_to(expr->cond_false, ctype);
1113 goto out;
1115 ctype = compatible_ptr_type(*true, expr->cond_false);
1116 if (ctype)
1117 goto out;
1118 ctype = ltype;
1119 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
1120 if (!typediff)
1121 goto out;
1122 expression_error(expr, "incompatible types in conditional expression (%s)", typediff);
1123 return NULL;
1125 out:
1126 expr->ctype = ctype;
1127 return ctype;
1130 /* FP assignments can not do modulo or bit operations */
1131 static int compatible_float_op(int op)
1133 return op == SPECIAL_ADD_ASSIGN ||
1134 op == SPECIAL_SUB_ASSIGN ||
1135 op == SPECIAL_MUL_ASSIGN ||
1136 op == SPECIAL_DIV_ASSIGN;
1139 static int evaluate_assign_op(struct expression *expr, struct symbol *target,
1140 struct expression **rp, struct symbol *source, int op)
1142 struct symbol *t, *s;
1143 int tclass = classify_type(target, &t);
1144 int sclass = classify_type(source, &s);
1146 if (tclass & sclass & TYPE_NUM) {
1147 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1148 expression_error(expr, "invalid assignment");
1149 return 0;
1151 if (tclass & TYPE_RESTRICT) {
1152 if (!restricted_binop(op, target)) {
1153 expression_error(expr, "bad restricted assignment");
1154 return 0;
1156 /* allowed assignments unfoul */
1157 if (sclass & TYPE_FOULED && s->ctype.base_type == t)
1158 goto Cast;
1159 if (!restricted_value(*rp, target))
1160 return 1;
1161 } else if (!(sclass & TYPE_RESTRICT))
1162 goto Cast;
1163 /* source and target would better be identical restricted */
1164 if (t == s)
1165 return 1;
1166 warning(expr->pos, "invalid restricted assignment");
1167 *rp = cast_to(*rp, target);
1168 return 0;
1169 } else if (tclass & TYPE_PTR) {
1170 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1171 evaluate_ptr_add(expr, target, rp);
1172 return 1;
1174 expression_error(expr, "invalid pointer assignment");
1175 return 0;
1176 } else {
1177 expression_error(expr, "invalid assignment");
1178 return 0;
1181 Cast:
1182 *rp = cast_to(*rp, target);
1183 return 1;
1186 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1187 struct expression **rp, struct symbol *source, const char *where)
1189 const char *typediff;
1190 struct symbol *t, *s;
1191 int target_as;
1192 int tclass = classify_type(target, &t);
1193 int sclass = classify_type(source, &s);
1195 if (tclass & sclass & TYPE_NUM) {
1196 if (tclass & TYPE_RESTRICT) {
1197 /* allowed assignments unfoul */
1198 if (sclass & TYPE_FOULED && s->ctype.base_type == t)
1199 goto Cast;
1200 if (!restricted_value(*rp, target))
1201 return 1;
1202 } else if (!(sclass & TYPE_RESTRICT))
1203 goto Cast;
1206 /* It's OK if the target is more volatile or const than the source */
1207 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
1208 if (!typediff)
1209 return 1;
1211 /* Pointer destination? */
1212 if (tclass & TYPE_PTR) {
1213 struct expression *right = *rp;
1214 int source_as;
1216 // NULL pointer is always OK
1217 if (is_null_ptr(right))
1218 goto Cast;
1220 /* "void *" matches anything as long as the address space is OK */
1221 target_as = t->ctype.as | target->ctype.as;
1222 source_as = s->ctype.as | source->ctype.as;
1223 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
1224 s = get_base_type(s);
1225 t = get_base_type(t);
1226 if (s == &void_ctype || t == &void_ctype)
1227 goto Cast;
1231 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1232 info(expr->pos, " expected %s", show_typename(target));
1233 info(expr->pos, " got %s", show_typename(source));
1234 *rp = cast_to(*rp, target);
1235 return 0;
1236 Cast:
1237 *rp = cast_to(*rp, target);
1238 return 1;
1241 static void mark_assigned(struct expression *expr)
1243 struct symbol *sym;
1245 if (!expr)
1246 return;
1247 switch (expr->type) {
1248 case EXPR_SYMBOL:
1249 sym = expr->symbol;
1250 if (!sym)
1251 return;
1252 if (sym->type != SYM_NODE)
1253 return;
1254 sym->ctype.modifiers |= MOD_ASSIGNED;
1255 return;
1257 case EXPR_BINOP:
1258 mark_assigned(expr->left);
1259 mark_assigned(expr->right);
1260 return;
1261 case EXPR_CAST:
1262 mark_assigned(expr->cast_expression);
1263 return;
1264 case EXPR_SLICE:
1265 mark_assigned(expr->base);
1266 return;
1267 default:
1268 /* Hmm? */
1269 return;
1273 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1275 if (type->ctype.modifiers & MOD_CONST)
1276 expression_error(left, "assignment to const expression");
1278 /* We know left is an lvalue, so it's a "preop-*" */
1279 mark_assigned(left->unop);
1282 static struct symbol *evaluate_assignment(struct expression *expr)
1284 struct expression *left = expr->left, *right = expr->right;
1285 struct expression *where = expr;
1286 struct symbol *ltype, *rtype;
1288 if (!lvalue_expression(left)) {
1289 expression_error(expr, "not an lvalue");
1290 return NULL;
1293 ltype = left->ctype;
1295 rtype = degenerate(right);
1297 if (expr->op != '=') {
1298 if (!evaluate_assign_op(where, ltype, &where->right, rtype, expr->op))
1299 return NULL;
1300 } else {
1301 if (!compatible_assignment_types(where, ltype, &where->right, rtype, "assignment"))
1302 return NULL;
1305 evaluate_assign_to(left, ltype);
1307 expr->ctype = ltype;
1308 return ltype;
1311 static void examine_fn_arguments(struct symbol *fn)
1313 struct symbol *s;
1315 FOR_EACH_PTR(fn->arguments, s) {
1316 struct symbol *arg = evaluate_symbol(s);
1317 /* Array/function arguments silently degenerate into pointers */
1318 if (arg) {
1319 struct symbol *ptr;
1320 switch(arg->type) {
1321 case SYM_ARRAY:
1322 case SYM_FN:
1323 ptr = alloc_symbol(s->pos, SYM_PTR);
1324 if (arg->type == SYM_ARRAY)
1325 ptr->ctype = arg->ctype;
1326 else
1327 ptr->ctype.base_type = arg;
1328 ptr->ctype.as |= s->ctype.as;
1329 ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1331 s->ctype.base_type = ptr;
1332 s->ctype.as = 0;
1333 s->ctype.modifiers &= ~MOD_PTRINHERIT;
1334 s->bit_size = 0;
1335 s->examined = 0;
1336 examine_symbol_type(s);
1337 break;
1338 default:
1339 /* nothing */
1340 break;
1343 } END_FOR_EACH_PTR(s);
1346 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1348 /* Take the modifiers of the pointer, and apply them to the member */
1349 mod |= sym->ctype.modifiers;
1350 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1351 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1352 *newsym = *sym;
1353 newsym->ctype.as = as;
1354 newsym->ctype.modifiers = mod;
1355 sym = newsym;
1357 return sym;
1360 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1362 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1363 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1365 node->ctype.base_type = ptr;
1366 ptr->bit_size = bits_in_pointer;
1367 ptr->ctype.alignment = pointer_alignment;
1369 node->bit_size = bits_in_pointer;
1370 node->ctype.alignment = pointer_alignment;
1372 access_symbol(sym);
1373 if (sym->ctype.modifiers & MOD_REGISTER) {
1374 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1375 sym->ctype.modifiers &= ~MOD_REGISTER;
1377 if (sym->type == SYM_NODE) {
1378 ptr->ctype.as |= sym->ctype.as;
1379 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1380 sym = sym->ctype.base_type;
1382 if (degenerate && sym->type == SYM_ARRAY) {
1383 ptr->ctype.as |= sym->ctype.as;
1384 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1385 sym = sym->ctype.base_type;
1387 ptr->ctype.base_type = sym;
1389 return node;
1392 /* Arrays degenerate into pointers on pointer arithmetic */
1393 static struct symbol *degenerate(struct expression *expr)
1395 struct symbol *ctype, *base;
1397 if (!expr)
1398 return NULL;
1399 ctype = expr->ctype;
1400 if (!ctype)
1401 return NULL;
1402 base = examine_symbol_type(ctype);
1403 if (ctype->type == SYM_NODE)
1404 base = ctype->ctype.base_type;
1406 * Arrays degenerate into pointers to the entries, while
1407 * functions degenerate into pointers to themselves.
1408 * If array was part of non-lvalue compound, we create a copy
1409 * of that compound first and then act as if we were dealing with
1410 * the corresponding field in there.
1412 switch (base->type) {
1413 case SYM_ARRAY:
1414 if (expr->type == EXPR_SLICE) {
1415 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1416 struct expression *e0, *e1, *e2, *e3, *e4;
1418 a->ctype.base_type = expr->base->ctype;
1419 a->bit_size = expr->base->ctype->bit_size;
1420 a->array_size = expr->base->ctype->array_size;
1422 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1423 e0->symbol = a;
1424 e0->ctype = &lazy_ptr_ctype;
1426 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1427 e1->unop = e0;
1428 e1->op = '*';
1429 e1->ctype = expr->base->ctype; /* XXX */
1431 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1432 e2->left = e1;
1433 e2->right = expr->base;
1434 e2->op = '=';
1435 e2->ctype = expr->base->ctype;
1437 if (expr->r_bitpos) {
1438 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1439 e3->op = '+';
1440 e3->left = e0;
1441 e3->right = alloc_const_expression(expr->pos,
1442 expr->r_bitpos >> 3);
1443 e3->ctype = &lazy_ptr_ctype;
1444 } else {
1445 e3 = e0;
1448 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1449 e4->left = e2;
1450 e4->right = e3;
1451 e4->ctype = &lazy_ptr_ctype;
1453 expr->unop = e4;
1454 expr->type = EXPR_PREOP;
1455 expr->op = '*';
1457 case SYM_FN:
1458 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1459 expression_error(expr, "strange non-value function or array");
1460 return &bad_ctype;
1462 *expr = *expr->unop;
1463 ctype = create_pointer(expr, ctype, 1);
1464 expr->ctype = ctype;
1465 default:
1466 /* nothing */;
1468 return ctype;
1471 static struct symbol *evaluate_addressof(struct expression *expr)
1473 struct expression *op = expr->unop;
1474 struct symbol *ctype;
1476 if (op->op != '*' || op->type != EXPR_PREOP) {
1477 expression_error(expr, "not addressable");
1478 return NULL;
1480 ctype = op->ctype;
1481 *expr = *op->unop;
1483 if (expr->type == EXPR_SYMBOL) {
1484 struct symbol *sym = expr->symbol;
1485 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1489 * symbol expression evaluation is lazy about the type
1490 * of the sub-expression, so we may have to generate
1491 * the type here if so..
1493 if (expr->ctype == &lazy_ptr_ctype) {
1494 ctype = create_pointer(expr, ctype, 0);
1495 expr->ctype = ctype;
1497 return expr->ctype;
1501 static struct symbol *evaluate_dereference(struct expression *expr)
1503 struct expression *op = expr->unop;
1504 struct symbol *ctype = op->ctype, *node, *target;
1506 /* Simplify: *&(expr) => (expr) */
1507 if (op->type == EXPR_PREOP && op->op == '&') {
1508 *expr = *op->unop;
1509 return expr->ctype;
1512 /* Dereferencing a node drops all the node information. */
1513 if (ctype->type == SYM_NODE)
1514 ctype = ctype->ctype.base_type;
1516 node = alloc_symbol(expr->pos, SYM_NODE);
1517 target = ctype->ctype.base_type;
1519 switch (ctype->type) {
1520 default:
1521 expression_error(expr, "cannot dereference this type");
1522 return NULL;
1523 case SYM_PTR:
1524 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1525 merge_type(node, ctype);
1526 break;
1528 case SYM_ARRAY:
1529 if (!lvalue_expression(op)) {
1530 expression_error(op, "non-lvalue array??");
1531 return NULL;
1534 /* Do the implied "addressof" on the array */
1535 *op = *op->unop;
1538 * When an array is dereferenced, we need to pick
1539 * up the attributes of the original node too..
1541 merge_type(node, op->ctype);
1542 merge_type(node, ctype);
1543 break;
1546 node->bit_size = target->bit_size;
1547 node->array_size = target->array_size;
1549 expr->ctype = node;
1550 return node;
1554 * Unary post-ops: x++ and x--
1556 static struct symbol *evaluate_postop(struct expression *expr)
1558 struct expression *op = expr->unop;
1559 struct symbol *ctype = op->ctype;
1561 if (!lvalue_expression(expr->unop)) {
1562 expression_error(expr, "need lvalue expression for ++/--");
1563 return NULL;
1565 if (is_restricted_type(ctype) && restricted_unop(expr->op, &ctype)) {
1566 expression_error(expr, "bad operation on restricted");
1567 return NULL;
1568 } else if (is_fouled_type(ctype) && restricted_unop(expr->op, &ctype)) {
1569 expression_error(expr, "bad operation on restricted");
1570 return NULL;
1573 evaluate_assign_to(op, ctype);
1575 expr->ctype = ctype;
1576 expr->op_value = 1;
1577 if (is_ptr_type(ctype))
1578 expr->op_value = ptr_object_size(ctype) >> 3;
1580 return ctype;
1583 static struct symbol *evaluate_sign(struct expression *expr)
1585 struct symbol *ctype = expr->unop->ctype;
1586 if (is_int_type(ctype)) {
1587 struct symbol *rtype = rtype = integer_promotion(ctype);
1588 expr->unop = cast_to(expr->unop, rtype);
1589 ctype = rtype;
1590 } else if (is_float_type(ctype) && expr->op != '~') {
1591 /* no conversions needed */
1592 } else if (is_restricted_type(ctype) && !restricted_unop(expr->op, &ctype)) {
1593 /* no conversions needed */
1594 } else if (is_fouled_type(ctype) && !restricted_unop(expr->op, &ctype)) {
1595 /* no conversions needed */
1596 } else {
1597 return bad_expr_type(expr);
1599 if (expr->op == '+')
1600 *expr = *expr->unop;
1601 expr->ctype = ctype;
1602 return ctype;
1605 static struct symbol *evaluate_preop(struct expression *expr)
1607 struct symbol *ctype = expr->unop->ctype;
1609 switch (expr->op) {
1610 case '(':
1611 *expr = *expr->unop;
1612 return ctype;
1614 case '+':
1615 case '-':
1616 case '~':
1617 return evaluate_sign(expr);
1619 case '*':
1620 return evaluate_dereference(expr);
1622 case '&':
1623 return evaluate_addressof(expr);
1625 case SPECIAL_INCREMENT:
1626 case SPECIAL_DECREMENT:
1628 * From a type evaluation standpoint the preops are
1629 * the same as the postops
1631 return evaluate_postop(expr);
1633 case '!':
1634 if (is_safe_type(ctype))
1635 warning(expr->pos, "testing a 'safe expression'");
1636 if (is_float_type(ctype)) {
1637 struct expression *arg = expr->unop;
1638 expr->type = EXPR_BINOP;
1639 expr->op = SPECIAL_EQUAL;
1640 expr->left = arg;
1641 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1642 expr->right->ctype = ctype;
1643 expr->right->fvalue = 0;
1644 } else if (is_fouled_type(ctype)) {
1645 warning(expr->pos, "restricted degrades to integer");
1647 ctype = &bool_ctype;
1648 break;
1650 default:
1651 break;
1653 expr->ctype = ctype;
1654 return &bool_ctype;
1657 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1659 struct ptr_list *head = (struct ptr_list *)_list;
1660 struct ptr_list *list = head;
1662 if (!head)
1663 return NULL;
1664 do {
1665 int i;
1666 for (i = 0; i < list->nr; i++) {
1667 struct symbol *sym = (struct symbol *) list->list[i];
1668 if (sym->ident) {
1669 if (sym->ident != ident)
1670 continue;
1671 *offset = sym->offset;
1672 return sym;
1673 } else {
1674 struct symbol *ctype = sym->ctype.base_type;
1675 struct symbol *sub;
1676 if (!ctype)
1677 continue;
1678 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1679 continue;
1680 sub = find_identifier(ident, ctype->symbol_list, offset);
1681 if (!sub)
1682 continue;
1683 *offset += sym->offset;
1684 return sub;
1687 } while ((list = list->next) != head);
1688 return NULL;
1691 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1693 struct expression *add;
1696 * Create a new add-expression
1698 * NOTE! Even if we just add zero, we need a new node
1699 * for the member pointer, since it has a different
1700 * type than the original pointer. We could make that
1701 * be just a cast, but the fact is, a node is a node,
1702 * so we might as well just do the "add zero" here.
1704 add = alloc_expression(expr->pos, EXPR_BINOP);
1705 add->op = '+';
1706 add->left = expr;
1707 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1708 add->right->ctype = &int_ctype;
1709 add->right->value = offset;
1712 * The ctype of the pointer will be lazily evaluated if
1713 * we ever take the address of this member dereference..
1715 add->ctype = &lazy_ptr_ctype;
1716 return add;
1719 /* structure/union dereference */
1720 static struct symbol *evaluate_member_dereference(struct expression *expr)
1722 int offset;
1723 struct symbol *ctype, *member;
1724 struct expression *deref = expr->deref, *add;
1725 struct ident *ident = expr->member;
1726 unsigned int mod;
1727 int address_space;
1729 if (!evaluate_expression(deref))
1730 return NULL;
1731 if (!ident) {
1732 expression_error(expr, "bad member name");
1733 return NULL;
1736 ctype = deref->ctype;
1737 address_space = ctype->ctype.as;
1738 mod = ctype->ctype.modifiers;
1739 if (ctype->type == SYM_NODE) {
1740 ctype = ctype->ctype.base_type;
1741 address_space |= ctype->ctype.as;
1742 mod |= ctype->ctype.modifiers;
1744 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1745 expression_error(expr, "expected structure or union");
1746 return NULL;
1748 examine_symbol_type(ctype);
1749 offset = 0;
1750 member = find_identifier(ident, ctype->symbol_list, &offset);
1751 if (!member) {
1752 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1753 const char *name = "<unnamed>";
1754 int namelen = 9;
1755 if (ctype->ident) {
1756 name = ctype->ident->name;
1757 namelen = ctype->ident->len;
1759 if (ctype->symbol_list)
1760 expression_error(expr, "no member '%s' in %s %.*s",
1761 show_ident(ident), type, namelen, name);
1762 else
1763 expression_error(expr, "using member '%s' in "
1764 "incomplete %s %.*s", show_ident(ident),
1765 type, namelen, name);
1766 return NULL;
1770 * The member needs to take on the address space and modifiers of
1771 * the "parent" type.
1773 member = convert_to_as_mod(member, address_space, mod);
1774 ctype = get_base_type(member);
1776 if (!lvalue_expression(deref)) {
1777 if (deref->type != EXPR_SLICE) {
1778 expr->base = deref;
1779 expr->r_bitpos = 0;
1780 } else {
1781 expr->base = deref->base;
1782 expr->r_bitpos = deref->r_bitpos;
1784 expr->r_bitpos += offset << 3;
1785 expr->type = EXPR_SLICE;
1786 expr->r_nrbits = member->bit_size;
1787 expr->r_bitpos += member->bit_offset;
1788 expr->ctype = member;
1789 return member;
1792 deref = deref->unop;
1793 expr->deref = deref;
1795 add = evaluate_offset(deref, offset);
1796 expr->type = EXPR_PREOP;
1797 expr->op = '*';
1798 expr->unop = add;
1800 expr->ctype = member;
1801 return member;
1804 static int is_promoted(struct expression *expr)
1806 while (1) {
1807 switch (expr->type) {
1808 case EXPR_BINOP:
1809 case EXPR_SELECT:
1810 case EXPR_CONDITIONAL:
1811 return 1;
1812 case EXPR_COMMA:
1813 expr = expr->right;
1814 continue;
1815 case EXPR_PREOP:
1816 switch (expr->op) {
1817 case '(':
1818 expr = expr->unop;
1819 continue;
1820 case '+':
1821 case '-':
1822 case '~':
1823 return 1;
1824 default:
1825 return 0;
1827 default:
1828 return 0;
1834 static struct symbol *evaluate_cast(struct expression *);
1836 static struct symbol *evaluate_type_information(struct expression *expr)
1838 struct symbol *sym = expr->cast_type;
1839 if (!sym) {
1840 sym = evaluate_expression(expr->cast_expression);
1841 if (!sym)
1842 return NULL;
1844 * Expressions of restricted types will possibly get
1845 * promoted - check that here
1847 if (is_restricted_type(sym)) {
1848 if (sym->bit_size < bits_in_int && is_promoted(expr))
1849 sym = &int_ctype;
1850 } else if (is_fouled_type(sym)) {
1851 sym = &int_ctype;
1854 examine_symbol_type(sym);
1855 if (is_bitfield_type(sym)) {
1856 expression_error(expr, "trying to examine bitfield type");
1857 return NULL;
1859 return sym;
1862 static struct symbol *evaluate_sizeof(struct expression *expr)
1864 struct symbol *type;
1865 int size;
1867 type = evaluate_type_information(expr);
1868 if (!type)
1869 return NULL;
1871 size = type->bit_size;
1872 if ((size < 0) || (size & 7))
1873 expression_error(expr, "cannot size expression");
1874 expr->type = EXPR_VALUE;
1875 expr->value = size >> 3;
1876 expr->ctype = size_t_ctype;
1877 return size_t_ctype;
1880 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
1882 struct symbol *type;
1883 int size;
1885 type = evaluate_type_information(expr);
1886 if (!type)
1887 return NULL;
1889 if (type->type == SYM_NODE)
1890 type = type->ctype.base_type;
1891 if (!type)
1892 return NULL;
1893 switch (type->type) {
1894 case SYM_ARRAY:
1895 break;
1896 case SYM_PTR:
1897 type = get_base_type(type);
1898 if (type)
1899 break;
1900 default:
1901 expression_error(expr, "expected pointer expression");
1902 return NULL;
1904 size = type->bit_size;
1905 if (size & 7)
1906 size = 0;
1907 expr->type = EXPR_VALUE;
1908 expr->value = size >> 3;
1909 expr->ctype = size_t_ctype;
1910 return size_t_ctype;
1913 static struct symbol *evaluate_alignof(struct expression *expr)
1915 struct symbol *type;
1917 type = evaluate_type_information(expr);
1918 if (!type)
1919 return NULL;
1921 expr->type = EXPR_VALUE;
1922 expr->value = type->ctype.alignment;
1923 expr->ctype = size_t_ctype;
1924 return size_t_ctype;
1927 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1929 struct expression *expr;
1930 struct symbol_list *argument_types = fn->arguments;
1931 struct symbol *argtype;
1932 int i = 1;
1934 PREPARE_PTR_LIST(argument_types, argtype);
1935 FOR_EACH_PTR (head, expr) {
1936 struct expression **p = THIS_ADDRESS(expr);
1937 struct symbol *ctype, *target;
1938 ctype = evaluate_expression(expr);
1940 if (!ctype)
1941 return 0;
1943 ctype = degenerate(expr);
1945 target = argtype;
1946 if (!target && ctype->bit_size < bits_in_int)
1947 target = &int_ctype;
1948 if (target) {
1949 static char where[30];
1950 examine_symbol_type(target);
1951 sprintf(where, "argument %d", i);
1952 compatible_assignment_types(expr, target, p, ctype, where);
1955 i++;
1956 NEXT_PTR_LIST(argtype);
1957 } END_FOR_EACH_PTR(expr);
1958 FINISH_PTR_LIST(argtype);
1959 return 1;
1962 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
1964 struct symbol *sym;
1966 FOR_EACH_PTR(ctype->symbol_list, sym) {
1967 if (sym->ident == ident)
1968 return sym;
1969 } END_FOR_EACH_PTR(sym);
1970 return NULL;
1973 static void convert_index(struct expression *e)
1975 struct expression *child = e->idx_expression;
1976 unsigned from = e->idx_from;
1977 unsigned to = e->idx_to + 1;
1978 e->type = EXPR_POS;
1979 e->init_offset = from * (e->ctype->bit_size>>3);
1980 e->init_nr = to - from;
1981 e->init_expr = child;
1984 static void convert_ident(struct expression *e)
1986 struct expression *child = e->ident_expression;
1987 struct symbol *sym = e->field;
1988 e->type = EXPR_POS;
1989 e->init_offset = sym->offset;
1990 e->init_nr = 1;
1991 e->init_expr = child;
1994 static void convert_designators(struct expression *e)
1996 while (e) {
1997 if (e->type == EXPR_INDEX)
1998 convert_index(e);
1999 else if (e->type == EXPR_IDENTIFIER)
2000 convert_ident(e);
2001 else
2002 break;
2003 e = e->init_expr;
2007 static void excess(struct expression *e, const char *s)
2009 warning(e->pos, "excessive elements in %s initializer", s);
2013 * implicit designator for the first element
2015 static struct expression *first_subobject(struct symbol *ctype, int class,
2016 struct expression **v)
2018 struct expression *e = *v, *new;
2020 if (ctype->type == SYM_NODE)
2021 ctype = ctype->ctype.base_type;
2023 if (class & TYPE_PTR) { /* array */
2024 if (!ctype->bit_size)
2025 return NULL;
2026 new = alloc_expression(e->pos, EXPR_INDEX);
2027 new->idx_expression = e;
2028 new->ctype = ctype->ctype.base_type;
2029 } else {
2030 struct symbol *field, *p;
2031 PREPARE_PTR_LIST(ctype->symbol_list, p);
2032 while (p && !p->ident && is_bitfield_type(p))
2033 NEXT_PTR_LIST(p);
2034 field = p;
2035 FINISH_PTR_LIST(p);
2036 if (!field)
2037 return NULL;
2038 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2039 new->ident_expression = e;
2040 new->field = new->ctype = field;
2042 *v = new;
2043 return new;
2047 * sanity-check explicit designators; return the innermost one or NULL
2048 * in case of error. Assign types.
2050 static struct expression *check_designators(struct expression *e,
2051 struct symbol *ctype)
2053 struct expression *last = NULL;
2054 const char *err;
2055 while (1) {
2056 if (ctype->type == SYM_NODE)
2057 ctype = ctype->ctype.base_type;
2058 if (e->type == EXPR_INDEX) {
2059 struct symbol *type;
2060 if (ctype->type != SYM_ARRAY) {
2061 err = "array index in non-array";
2062 break;
2064 type = ctype->ctype.base_type;
2065 if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2066 unsigned offset = e->idx_to * type->bit_size;
2067 if (offset >= ctype->bit_size) {
2068 err = "index out of bounds in";
2069 break;
2072 e->ctype = ctype = type;
2073 ctype = type;
2074 last = e;
2075 if (!e->idx_expression) {
2076 err = "invalid";
2077 break;
2079 e = e->idx_expression;
2080 } else if (e->type == EXPR_IDENTIFIER) {
2081 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2082 err = "field name not in struct or union";
2083 break;
2085 ctype = find_struct_ident(ctype, e->expr_ident);
2086 if (!ctype) {
2087 err = "unknown field name in";
2088 break;
2090 e->field = e->ctype = ctype;
2091 last = e;
2092 if (!e->ident_expression) {
2093 err = "invalid";
2094 break;
2096 e = e->ident_expression;
2097 } else if (e->type == EXPR_POS) {
2098 err = "internal front-end error: EXPR_POS in";
2099 break;
2100 } else
2101 return last;
2103 expression_error(e, "%s initializer", err);
2104 return NULL;
2108 * choose the next subobject to initialize.
2110 * Get designators for next element, switch old ones to EXPR_POS.
2111 * Return the resulting expression or NULL if we'd run out of subobjects.
2112 * The innermost designator is returned in *v. Designators in old
2113 * are assumed to be already sanity-checked.
2115 static struct expression *next_designators(struct expression *old,
2116 struct symbol *ctype,
2117 struct expression *e, struct expression **v)
2119 struct expression *new = NULL;
2121 if (!old)
2122 return NULL;
2123 if (old->type == EXPR_INDEX) {
2124 struct expression *copy;
2125 unsigned n;
2127 copy = next_designators(old->idx_expression,
2128 old->ctype, e, v);
2129 if (!copy) {
2130 n = old->idx_to + 1;
2131 if (n * old->ctype->bit_size == ctype->bit_size) {
2132 convert_index(old);
2133 return NULL;
2135 copy = e;
2136 *v = new = alloc_expression(e->pos, EXPR_INDEX);
2137 } else {
2138 n = old->idx_to;
2139 new = alloc_expression(e->pos, EXPR_INDEX);
2142 new->idx_from = new->idx_to = n;
2143 new->idx_expression = copy;
2144 new->ctype = old->ctype;
2145 convert_index(old);
2146 } else if (old->type == EXPR_IDENTIFIER) {
2147 struct expression *copy;
2148 struct symbol *field;
2150 copy = next_designators(old->ident_expression,
2151 old->ctype, e, v);
2152 if (!copy) {
2153 field = old->field->next_subobject;
2154 if (!field) {
2155 convert_ident(old);
2156 return NULL;
2158 copy = e;
2159 *v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2160 } else {
2161 field = old->field;
2162 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2165 new->field = field;
2166 new->expr_ident = field->ident;
2167 new->ident_expression = copy;
2168 new->ctype = field;
2169 convert_ident(old);
2171 return new;
2174 static int handle_simple_initializer(struct expression **ep, int nested,
2175 int class, struct symbol *ctype);
2178 * deal with traversing subobjects [6.7.8(17,18,20)]
2180 static void handle_list_initializer(struct expression *expr,
2181 int class, struct symbol *ctype)
2183 struct expression *e, *last = NULL, *top = NULL, *next;
2184 int jumped = 0;
2186 FOR_EACH_PTR(expr->expr_list, e) {
2187 struct expression **v;
2188 struct symbol *type;
2189 int lclass;
2191 if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2192 if (!top) {
2193 top = e;
2194 last = first_subobject(ctype, class, &top);
2195 } else {
2196 last = next_designators(last, ctype, e, &top);
2198 if (!last) {
2199 excess(e, class & TYPE_PTR ? "array" :
2200 "struct or union");
2201 DELETE_CURRENT_PTR(e);
2202 continue;
2204 if (jumped) {
2205 warning(e->pos, "advancing past deep designator");
2206 jumped = 0;
2208 REPLACE_CURRENT_PTR(e, last);
2209 } else {
2210 next = check_designators(e, ctype);
2211 if (!next) {
2212 DELETE_CURRENT_PTR(e);
2213 continue;
2215 top = next;
2216 /* deeper than one designator? */
2217 jumped = top != e;
2218 convert_designators(last);
2219 last = e;
2222 found:
2223 lclass = classify_type(top->ctype, &type);
2224 if (top->type == EXPR_INDEX)
2225 v = &top->idx_expression;
2226 else
2227 v = &top->ident_expression;
2229 if (handle_simple_initializer(v, 1, lclass, top->ctype))
2230 continue;
2232 if (!(lclass & TYPE_COMPOUND)) {
2233 warning(e->pos, "bogus scalar initializer");
2234 DELETE_CURRENT_PTR(e);
2235 continue;
2238 next = first_subobject(type, lclass, v);
2239 if (next) {
2240 warning(e->pos, "missing braces around initializer");
2241 top = next;
2242 goto found;
2245 DELETE_CURRENT_PTR(e);
2246 excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2248 } END_FOR_EACH_PTR(e);
2250 convert_designators(last);
2251 expr->ctype = ctype;
2254 static int is_string_literal(struct expression **v)
2256 struct expression *e = *v;
2257 while (e && e->type == EXPR_PREOP && e->op == '(')
2258 e = e->unop;
2259 if (!e || e->type != EXPR_STRING)
2260 return 0;
2261 if (e != *v && Wparen_string)
2262 warning(e->pos,
2263 "array initialized from parenthesized string constant");
2264 *v = e;
2265 return 1;
2269 * We want a normal expression, possibly in one layer of braces. Warn
2270 * if the latter happens inside a list (it's legal, but likely to be
2271 * an effect of screwup). In case of anything not legal, we are definitely
2272 * having an effect of screwup, so just fail and let the caller warn.
2274 static struct expression *handle_scalar(struct expression *e, int nested)
2276 struct expression *v = NULL, *p;
2277 int count = 0;
2279 /* normal case */
2280 if (e->type != EXPR_INITIALIZER)
2281 return e;
2283 FOR_EACH_PTR(e->expr_list, p) {
2284 if (!v)
2285 v = p;
2286 count++;
2287 } END_FOR_EACH_PTR(p);
2288 if (count != 1)
2289 return NULL;
2290 switch(v->type) {
2291 case EXPR_INITIALIZER:
2292 case EXPR_INDEX:
2293 case EXPR_IDENTIFIER:
2294 return NULL;
2295 default:
2296 break;
2298 if (nested)
2299 warning(e->pos, "braces around scalar initializer");
2300 return v;
2304 * deal with the cases that don't care about subobjects:
2305 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2306 * character array <- string literal, possibly in braces [6.7.8(14)]
2307 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2308 * compound type <- initializer list in braces [6.7.8(16)]
2309 * The last one punts to handle_list_initializer() which, in turn will call
2310 * us for individual elements of the list.
2312 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2313 * the lack of support of wide char stuff in general.
2315 * One note: we need to take care not to evaluate a string literal until
2316 * we know that we *will* handle it right here. Otherwise we would screw
2317 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2318 * { "string", ...} - we need to preserve that string literal recognizable
2319 * until we dig into the inner struct.
2321 static int handle_simple_initializer(struct expression **ep, int nested,
2322 int class, struct symbol *ctype)
2324 int is_string = is_string_type(ctype);
2325 struct expression *e = *ep, *p;
2326 struct symbol *type;
2328 if (!e)
2329 return 0;
2331 /* scalar */
2332 if (!(class & TYPE_COMPOUND)) {
2333 e = handle_scalar(e, nested);
2334 if (!e)
2335 return 0;
2336 *ep = e;
2337 type = evaluate_expression(e);
2338 if (!e->ctype)
2339 return 1;
2340 compatible_assignment_types(e, ctype, ep, degenerate(e),
2341 "initializer");
2342 return 1;
2346 * sublist; either a string, or we dig in; the latter will deal with
2347 * pathologies, so we don't need anything fancy here.
2349 if (e->type == EXPR_INITIALIZER) {
2350 if (is_string) {
2351 struct expression *v = NULL;
2352 int count = 0;
2354 FOR_EACH_PTR(e->expr_list, p) {
2355 if (!v)
2356 v = p;
2357 count++;
2358 } END_FOR_EACH_PTR(p);
2359 if (count == 1 && is_string_literal(&v)) {
2360 *ep = e = v;
2361 goto String;
2364 handle_list_initializer(e, class, ctype);
2365 return 1;
2368 /* string */
2369 if (is_string_literal(&e)) {
2370 /* either we are doing array of char, or we'll have to dig in */
2371 if (is_string) {
2372 *ep = e;
2373 goto String;
2375 return 0;
2377 /* struct or union can be initialized by compatible */
2378 if (class != TYPE_COMPOUND)
2379 return 0;
2380 type = evaluate_expression(e);
2381 if (!type)
2382 return 0;
2383 if (ctype->type == SYM_NODE)
2384 ctype = ctype->ctype.base_type;
2385 if (type->type == SYM_NODE)
2386 type = type->ctype.base_type;
2387 if (ctype == type)
2388 return 1;
2389 return 0;
2391 String:
2392 p = alloc_expression(e->pos, EXPR_STRING);
2393 *p = *e;
2394 type = evaluate_expression(p);
2395 if (ctype->bit_size != -1 &&
2396 ctype->bit_size + bits_in_char < type->bit_size) {
2397 warning(e->pos,
2398 "too long initializer-string for array of char");
2400 *ep = p;
2401 return 1;
2404 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2406 struct symbol *type;
2407 int class = classify_type(ctype, &type);
2408 if (!handle_simple_initializer(ep, 0, class, ctype))
2409 expression_error(*ep, "invalid initializer");
2412 static int get_as(struct symbol *sym)
2414 int as;
2415 unsigned long mod;
2417 if (!sym)
2418 return 0;
2419 as = sym->ctype.as;
2420 mod = sym->ctype.modifiers;
2421 if (sym->type == SYM_NODE) {
2422 sym = sym->ctype.base_type;
2423 as |= sym->ctype.as;
2424 mod |= sym->ctype.modifiers;
2428 * At least for now, allow casting to a "unsigned long".
2429 * That's how we do things like pointer arithmetic and
2430 * store pointers to registers.
2432 if (sym == &ulong_ctype)
2433 return -1;
2435 if (sym && sym->type == SYM_PTR) {
2436 sym = get_base_type(sym);
2437 as |= sym->ctype.as;
2438 mod |= sym->ctype.modifiers;
2440 if (mod & MOD_FORCE)
2441 return -1;
2442 return as;
2445 static void cast_to_as(struct expression *e, int as)
2447 struct expression *v = e->cast_expression;
2448 struct symbol *type = v->ctype;
2450 if (!Wcast_to_address_space)
2451 return;
2453 if (v->type != EXPR_VALUE || v->value)
2454 goto out;
2456 /* cast from constant 0 to pointer is OK */
2457 if (is_int_type(type))
2458 return;
2460 if (type->type == SYM_NODE)
2461 type = type->ctype.base_type;
2463 if (type->type == SYM_PTR && type->ctype.base_type == &void_ctype)
2464 return;
2466 out:
2467 warning(e->pos, "cast adds address space to expression (<asn:%d>)", as);
2470 static struct symbol *evaluate_cast(struct expression *expr)
2472 struct expression *target = expr->cast_expression;
2473 struct symbol *ctype = examine_symbol_type(expr->cast_type);
2474 struct symbol *t1, *t2;
2475 int class1, class2;
2476 int as1, as2;
2478 if (!target)
2479 return NULL;
2481 expr->ctype = ctype;
2482 expr->cast_type = ctype;
2485 * Special case: a cast can be followed by an
2486 * initializer, in which case we need to pass
2487 * the type value down to that initializer rather
2488 * than trying to evaluate it as an expression
2490 * A more complex case is when the initializer is
2491 * dereferenced as part of a post-fix expression.
2492 * We need to produce an expression that can be dereferenced.
2494 if (target->type == EXPR_INITIALIZER) {
2495 struct symbol *sym = expr->cast_type;
2496 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2498 sym->initializer = expr->cast_expression;
2499 evaluate_symbol(sym);
2501 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2502 addr->symbol = sym;
2504 expr->type = EXPR_PREOP;
2505 expr->op = '*';
2506 expr->unop = addr;
2507 expr->ctype = sym;
2509 return sym;
2512 evaluate_expression(target);
2513 degenerate(target);
2515 class1 = classify_type(ctype, &t1);
2517 * You can always throw a value away by casting to
2518 * "void" - that's an implicit "force". Note that
2519 * the same is _not_ true of "void *".
2521 if (t1 == &void_ctype)
2522 goto out;
2524 if (class1 & TYPE_COMPOUND)
2525 warning(expr->pos, "cast to non-scalar");
2527 t2 = target->ctype;
2528 if (!t2) {
2529 expression_error(expr, "cast from unknown type");
2530 goto out;
2532 class2 = classify_type(t2, &t2);
2534 if (class2 & TYPE_COMPOUND)
2535 warning(expr->pos, "cast from non-scalar");
2537 /* allowed cast unfouls */
2538 if (class2 & TYPE_FOULED)
2539 t2 = t2->ctype.base_type;
2541 if (!(ctype->ctype.modifiers & MOD_FORCE) && t1 != t2) {
2542 if (class1 & TYPE_RESTRICT)
2543 warning(expr->pos, "cast to restricted type");
2544 if (class2 & TYPE_RESTRICT)
2545 warning(expr->pos, "cast from restricted type");
2548 as1 = get_as(ctype);
2549 as2 = get_as(target->ctype);
2550 if (!as1 && as2 > 0)
2551 warning(expr->pos, "cast removes address space of expression");
2552 if (as1 > 0 && as2 > 0 && as1 != as2)
2553 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2554 if (as1 > 0 && !as2)
2555 cast_to_as(expr, as1);
2558 * Casts of constant values are special: they
2559 * can be NULL, and thus need to be simplified
2560 * early.
2562 if (target->type == EXPR_VALUE)
2563 cast_value(expr, ctype, target, target->ctype);
2565 out:
2566 return ctype;
2570 * Evaluate a call expression with a symbol. This
2571 * should expand inline functions, and evaluate
2572 * builtins.
2574 static int evaluate_symbol_call(struct expression *expr)
2576 struct expression *fn = expr->fn;
2577 struct symbol *ctype = fn->ctype;
2579 if (fn->type != EXPR_PREOP)
2580 return 0;
2582 if (ctype->op && ctype->op->evaluate)
2583 return ctype->op->evaluate(expr);
2585 if (ctype->ctype.modifiers & MOD_INLINE) {
2586 int ret;
2587 struct symbol *curr = current_fn;
2588 current_fn = ctype->ctype.base_type;
2590 ret = inline_function(expr, ctype);
2592 /* restore the old function */
2593 current_fn = curr;
2594 return ret;
2597 return 0;
2600 static struct symbol *evaluate_call(struct expression *expr)
2602 int args, fnargs;
2603 struct symbol *ctype, *sym;
2604 struct expression *fn = expr->fn;
2605 struct expression_list *arglist = expr->args;
2607 if (!evaluate_expression(fn))
2608 return NULL;
2609 sym = ctype = fn->ctype;
2610 if (ctype->type == SYM_NODE)
2611 ctype = ctype->ctype.base_type;
2612 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
2613 ctype = get_base_type(ctype);
2615 examine_fn_arguments(ctype);
2616 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
2617 sym->op && sym->op->args) {
2618 if (!sym->op->args(expr))
2619 return NULL;
2620 } else {
2621 if (!evaluate_arguments(sym, ctype, arglist))
2622 return NULL;
2623 if (ctype->type != SYM_FN) {
2624 expression_error(expr, "not a function %s",
2625 show_ident(sym->ident));
2626 return NULL;
2628 args = expression_list_size(expr->args);
2629 fnargs = symbol_list_size(ctype->arguments);
2630 if (args < fnargs)
2631 expression_error(expr,
2632 "not enough arguments for function %s",
2633 show_ident(sym->ident));
2634 if (args > fnargs && !ctype->variadic)
2635 expression_error(expr,
2636 "too many arguments for function %s",
2637 show_ident(sym->ident));
2639 if (sym->type == SYM_NODE) {
2640 if (evaluate_symbol_call(expr))
2641 return expr->ctype;
2643 expr->ctype = ctype->ctype.base_type;
2644 return expr->ctype;
2647 struct symbol *evaluate_expression(struct expression *expr)
2649 if (!expr)
2650 return NULL;
2651 if (expr->ctype)
2652 return expr->ctype;
2654 switch (expr->type) {
2655 case EXPR_VALUE:
2656 case EXPR_FVALUE:
2657 expression_error(expr, "value expression without a type");
2658 return NULL;
2659 case EXPR_STRING:
2660 return evaluate_string(expr);
2661 case EXPR_SYMBOL:
2662 return evaluate_symbol_expression(expr);
2663 case EXPR_BINOP:
2664 if (!evaluate_expression(expr->left))
2665 return NULL;
2666 if (!evaluate_expression(expr->right))
2667 return NULL;
2668 return evaluate_binop(expr);
2669 case EXPR_LOGICAL:
2670 return evaluate_logical(expr);
2671 case EXPR_COMMA:
2672 evaluate_expression(expr->left);
2673 if (!evaluate_expression(expr->right))
2674 return NULL;
2675 return evaluate_comma(expr);
2676 case EXPR_COMPARE:
2677 if (!evaluate_expression(expr->left))
2678 return NULL;
2679 if (!evaluate_expression(expr->right))
2680 return NULL;
2681 return evaluate_compare(expr);
2682 case EXPR_ASSIGNMENT:
2683 if (!evaluate_expression(expr->left))
2684 return NULL;
2685 if (!evaluate_expression(expr->right))
2686 return NULL;
2687 return evaluate_assignment(expr);
2688 case EXPR_PREOP:
2689 if (!evaluate_expression(expr->unop))
2690 return NULL;
2691 return evaluate_preop(expr);
2692 case EXPR_POSTOP:
2693 if (!evaluate_expression(expr->unop))
2694 return NULL;
2695 return evaluate_postop(expr);
2696 case EXPR_CAST:
2697 case EXPR_IMPLIED_CAST:
2698 return evaluate_cast(expr);
2699 case EXPR_SIZEOF:
2700 return evaluate_sizeof(expr);
2701 case EXPR_PTRSIZEOF:
2702 return evaluate_ptrsizeof(expr);
2703 case EXPR_ALIGNOF:
2704 return evaluate_alignof(expr);
2705 case EXPR_DEREF:
2706 return evaluate_member_dereference(expr);
2707 case EXPR_CALL:
2708 return evaluate_call(expr);
2709 case EXPR_SELECT:
2710 case EXPR_CONDITIONAL:
2711 return evaluate_conditional_expression(expr);
2712 case EXPR_STATEMENT:
2713 expr->ctype = evaluate_statement(expr->statement);
2714 return expr->ctype;
2716 case EXPR_LABEL:
2717 expr->ctype = &ptr_ctype;
2718 return &ptr_ctype;
2720 case EXPR_TYPE:
2721 /* Evaluate the type of the symbol .. */
2722 evaluate_symbol(expr->symbol);
2723 /* .. but the type of the _expression_ is a "type" */
2724 expr->ctype = &type_ctype;
2725 return &type_ctype;
2727 /* These can not exist as stand-alone expressions */
2728 case EXPR_INITIALIZER:
2729 case EXPR_IDENTIFIER:
2730 case EXPR_INDEX:
2731 case EXPR_POS:
2732 expression_error(expr, "internal front-end error: initializer in expression");
2733 return NULL;
2734 case EXPR_SLICE:
2735 expression_error(expr, "internal front-end error: SLICE re-evaluated");
2736 return NULL;
2738 return NULL;
2741 static void check_duplicates(struct symbol *sym)
2743 int declared = 0;
2744 struct symbol *next = sym;
2746 while ((next = next->same_symbol) != NULL) {
2747 const char *typediff;
2748 evaluate_symbol(next);
2749 declared++;
2750 typediff = type_difference(sym, next, 0, 0);
2751 if (typediff) {
2752 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2753 show_ident(sym->ident),
2754 stream_name(next->pos.stream), next->pos.line, typediff);
2755 return;
2758 if (!declared) {
2759 unsigned long mod = sym->ctype.modifiers;
2760 if (mod & (MOD_STATIC | MOD_REGISTER))
2761 return;
2762 if (!(mod & MOD_TOPLEVEL))
2763 return;
2764 if (!Wdecl)
2765 return;
2766 if (sym->ident == &main_ident)
2767 return;
2768 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
2772 static struct symbol *evaluate_symbol(struct symbol *sym)
2774 struct symbol *base_type;
2776 if (!sym)
2777 return sym;
2778 if (sym->evaluated)
2779 return sym;
2780 sym->evaluated = 1;
2782 sym = examine_symbol_type(sym);
2783 base_type = get_base_type(sym);
2784 if (!base_type)
2785 return NULL;
2787 /* Evaluate the initializers */
2788 if (sym->initializer)
2789 evaluate_initializer(sym, &sym->initializer);
2791 /* And finally, evaluate the body of the symbol too */
2792 if (base_type->type == SYM_FN) {
2793 struct symbol *curr = current_fn;
2795 current_fn = base_type;
2797 examine_fn_arguments(base_type);
2798 if (!base_type->stmt && base_type->inline_stmt)
2799 uninline(sym);
2800 if (base_type->stmt)
2801 evaluate_statement(base_type->stmt);
2803 current_fn = curr;
2806 return base_type;
2809 void evaluate_symbol_list(struct symbol_list *list)
2811 struct symbol *sym;
2813 FOR_EACH_PTR(list, sym) {
2814 evaluate_symbol(sym);
2815 check_duplicates(sym);
2816 } END_FOR_EACH_PTR(sym);
2819 static struct symbol *evaluate_return_expression(struct statement *stmt)
2821 struct expression *expr = stmt->expression;
2822 struct symbol *ctype, *fntype;
2824 evaluate_expression(expr);
2825 ctype = degenerate(expr);
2826 fntype = current_fn->ctype.base_type;
2827 if (!fntype || fntype == &void_ctype) {
2828 if (expr && ctype != &void_ctype)
2829 expression_error(expr, "return expression in %s function", fntype?"void":"typeless");
2830 return NULL;
2833 if (!expr) {
2834 sparse_error(stmt->pos, "return with no return value");
2835 return NULL;
2837 if (!ctype)
2838 return NULL;
2839 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression");
2840 return NULL;
2843 static void evaluate_if_statement(struct statement *stmt)
2845 if (!stmt->if_conditional)
2846 return;
2848 evaluate_conditional(stmt->if_conditional, 0);
2849 evaluate_statement(stmt->if_true);
2850 evaluate_statement(stmt->if_false);
2853 static void evaluate_iterator(struct statement *stmt)
2855 evaluate_conditional(stmt->iterator_pre_condition, 1);
2856 evaluate_conditional(stmt->iterator_post_condition,1);
2857 evaluate_statement(stmt->iterator_pre_statement);
2858 evaluate_statement(stmt->iterator_statement);
2859 evaluate_statement(stmt->iterator_post_statement);
2862 static void verify_output_constraint(struct expression *expr, const char *constraint)
2864 switch (*constraint) {
2865 case '=': /* Assignment */
2866 case '+': /* Update */
2867 break;
2868 default:
2869 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
2873 static void verify_input_constraint(struct expression *expr, const char *constraint)
2875 switch (*constraint) {
2876 case '=': /* Assignment */
2877 case '+': /* Update */
2878 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
2882 static void evaluate_asm_statement(struct statement *stmt)
2884 struct expression *expr;
2885 int state;
2887 expr = stmt->asm_string;
2888 if (!expr || expr->type != EXPR_STRING) {
2889 sparse_error(stmt->pos, "need constant string for inline asm");
2890 return;
2893 state = 0;
2894 FOR_EACH_PTR(stmt->asm_outputs, expr) {
2895 struct ident *ident;
2897 switch (state) {
2898 case 0: /* Identifier */
2899 state = 1;
2900 ident = (struct ident *)expr;
2901 continue;
2903 case 1: /* Constraint */
2904 state = 2;
2905 if (!expr || expr->type != EXPR_STRING) {
2906 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
2907 *THIS_ADDRESS(expr) = NULL;
2908 continue;
2910 verify_output_constraint(expr, expr->string->data);
2911 continue;
2913 case 2: /* Expression */
2914 state = 0;
2915 if (!evaluate_expression(expr))
2916 return;
2917 if (!lvalue_expression(expr))
2918 warning(expr->pos, "asm output is not an lvalue");
2919 evaluate_assign_to(expr, expr->ctype);
2920 continue;
2922 } END_FOR_EACH_PTR(expr);
2924 state = 0;
2925 FOR_EACH_PTR(stmt->asm_inputs, expr) {
2926 struct ident *ident;
2928 switch (state) {
2929 case 0: /* Identifier */
2930 state = 1;
2931 ident = (struct ident *)expr;
2932 continue;
2934 case 1: /* Constraint */
2935 state = 2;
2936 if (!expr || expr->type != EXPR_STRING) {
2937 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
2938 *THIS_ADDRESS(expr) = NULL;
2939 continue;
2941 verify_input_constraint(expr, expr->string->data);
2942 continue;
2944 case 2: /* Expression */
2945 state = 0;
2946 if (!evaluate_expression(expr))
2947 return;
2948 continue;
2950 } END_FOR_EACH_PTR(expr);
2952 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
2953 if (!expr) {
2954 sparse_error(stmt->pos, "bad asm output");
2955 return;
2957 if (expr->type == EXPR_STRING)
2958 continue;
2959 expression_error(expr, "asm clobber is not a string");
2960 } END_FOR_EACH_PTR(expr);
2963 static void evaluate_case_statement(struct statement *stmt)
2965 evaluate_expression(stmt->case_expression);
2966 evaluate_expression(stmt->case_to);
2967 evaluate_statement(stmt->case_statement);
2970 static void check_case_type(struct expression *switch_expr,
2971 struct expression *case_expr,
2972 struct expression **enumcase)
2974 struct symbol *switch_type, *case_type;
2975 int sclass, cclass;
2977 if (!case_expr)
2978 return;
2980 switch_type = switch_expr->ctype;
2981 case_type = evaluate_expression(case_expr);
2983 if (!switch_type || !case_type)
2984 goto Bad;
2985 if (enumcase) {
2986 if (*enumcase)
2987 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
2988 else if (is_enum_type(case_type))
2989 *enumcase = case_expr;
2992 sclass = classify_type(switch_type, &switch_type);
2993 cclass = classify_type(case_type, &case_type);
2995 /* both should be arithmetic */
2996 if (!(sclass & cclass & TYPE_NUM))
2997 goto Bad;
2999 /* neither should be floating */
3000 if ((sclass | cclass) & TYPE_FLOAT)
3001 goto Bad;
3003 /* if neither is restricted, we are OK */
3004 if (!((sclass | cclass) & TYPE_RESTRICT))
3005 return;
3007 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
3008 cclass, sclass, case_type, switch_type))
3009 warning(case_expr->pos, "restricted degrades to integer");
3011 return;
3013 Bad:
3014 expression_error(case_expr, "incompatible types for 'case' statement");
3017 static void evaluate_switch_statement(struct statement *stmt)
3019 struct symbol *sym;
3020 struct expression *enumcase = NULL;
3021 struct expression **enumcase_holder = &enumcase;
3022 struct expression *sel = stmt->switch_expression;
3024 evaluate_expression(sel);
3025 evaluate_statement(stmt->switch_statement);
3026 if (!sel)
3027 return;
3028 if (sel->ctype && is_enum_type(sel->ctype))
3029 enumcase_holder = NULL; /* Only check cases against switch */
3031 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
3032 struct statement *case_stmt = sym->stmt;
3033 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
3034 check_case_type(sel, case_stmt->case_to, enumcase_holder);
3035 } END_FOR_EACH_PTR(sym);
3038 struct symbol *evaluate_statement(struct statement *stmt)
3040 if (!stmt)
3041 return NULL;
3043 switch (stmt->type) {
3044 case STMT_DECLARATION: {
3045 struct symbol *s;
3046 FOR_EACH_PTR(stmt->declaration, s) {
3047 evaluate_symbol(s);
3048 } END_FOR_EACH_PTR(s);
3049 return NULL;
3052 case STMT_RETURN:
3053 return evaluate_return_expression(stmt);
3055 case STMT_EXPRESSION:
3056 if (!evaluate_expression(stmt->expression))
3057 return NULL;
3058 return degenerate(stmt->expression);
3060 case STMT_COMPOUND: {
3061 struct statement *s;
3062 struct symbol *type = NULL;
3064 /* Evaluate the return symbol in the compound statement */
3065 evaluate_symbol(stmt->ret);
3068 * Then, evaluate each statement, making the type of the
3069 * compound statement be the type of the last statement
3071 type = evaluate_statement(stmt->args);
3072 FOR_EACH_PTR(stmt->stmts, s) {
3073 type = evaluate_statement(s);
3074 } END_FOR_EACH_PTR(s);
3075 if (!type)
3076 type = &void_ctype;
3077 return type;
3079 case STMT_IF:
3080 evaluate_if_statement(stmt);
3081 return NULL;
3082 case STMT_ITERATOR:
3083 evaluate_iterator(stmt);
3084 return NULL;
3085 case STMT_SWITCH:
3086 evaluate_switch_statement(stmt);
3087 return NULL;
3088 case STMT_CASE:
3089 evaluate_case_statement(stmt);
3090 return NULL;
3091 case STMT_LABEL:
3092 return evaluate_statement(stmt->label_statement);
3093 case STMT_GOTO:
3094 evaluate_expression(stmt->goto_expression);
3095 return NULL;
3096 case STMT_NONE:
3097 break;
3098 case STMT_ASM:
3099 evaluate_asm_statement(stmt);
3100 return NULL;
3101 case STMT_CONTEXT:
3102 evaluate_expression(stmt->expression);
3103 return NULL;
3104 case STMT_RANGE:
3105 evaluate_expression(stmt->range_expression);
3106 evaluate_expression(stmt->range_low);
3107 evaluate_expression(stmt->range_high);
3108 return NULL;
3110 return NULL;