sparse: Relicense under the MIT license
[smatch.git] / evaluate.c
blob5b643d86d03e9bea75ee842c6556234dfafe90a9
1 /*
2 * sparse/evaluate.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
25 * Evaluate constant expressions.
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <stddef.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <limits.h>
37 #include "lib.h"
38 #include "allocate.h"
39 #include "parse.h"
40 #include "token.h"
41 #include "symbol.h"
42 #include "target.h"
43 #include "expression.h"
45 struct symbol *current_fn;
47 static struct symbol *degenerate(struct expression *expr);
48 static struct symbol *evaluate_symbol(struct symbol *sym);
50 static struct symbol *evaluate_symbol_expression(struct expression *expr)
52 struct expression *addr;
53 struct symbol *sym = expr->symbol;
54 struct symbol *base_type;
56 if (!sym) {
57 expression_error(expr, "undefined identifier '%s'", show_ident(expr->symbol_name));
58 return NULL;
61 examine_symbol_type(sym);
63 base_type = get_base_type(sym);
64 if (!base_type) {
65 expression_error(expr, "identifier '%s' has no type", show_ident(expr->symbol_name));
66 return NULL;
69 addr = alloc_expression(expr->pos, EXPR_SYMBOL);
70 addr->symbol = sym;
71 addr->symbol_name = expr->symbol_name;
72 addr->ctype = &lazy_ptr_ctype; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
73 expr->type = EXPR_PREOP;
74 expr->op = '*';
75 expr->unop = addr;
77 /* The type of a symbol is the symbol itself! */
78 expr->ctype = sym;
79 return sym;
82 static struct symbol *evaluate_string(struct expression *expr)
84 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
85 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
86 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
87 struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
88 unsigned int length = expr->string->length;
90 sym->array_size = alloc_const_expression(expr->pos, length);
91 sym->bit_size = bytes_to_bits(length);
92 sym->ctype.alignment = 1;
93 sym->string = 1;
94 sym->ctype.modifiers = MOD_STATIC;
95 sym->ctype.base_type = array;
96 sym->initializer = initstr;
98 initstr->ctype = sym;
99 initstr->string = expr->string;
101 array->array_size = sym->array_size;
102 array->bit_size = bytes_to_bits(length);
103 array->ctype.alignment = 1;
104 array->ctype.modifiers = MOD_STATIC;
105 array->ctype.base_type = &char_ctype;
107 addr->symbol = sym;
108 addr->ctype = &lazy_ptr_ctype;
110 expr->type = EXPR_PREOP;
111 expr->op = '*';
112 expr->unop = addr;
113 expr->ctype = sym;
114 return sym;
117 /* type has come from classify_type and is an integer type */
118 static inline struct symbol *integer_promotion(struct symbol *type)
120 struct symbol *orig_type = type;
121 unsigned long mod = type->ctype.modifiers;
122 int width = type->bit_size;
125 * Bitfields always promote to the base type,
126 * even if the bitfield might be bigger than
127 * an "int".
129 if (type->type == SYM_BITFIELD) {
130 type = type->ctype.base_type;
131 orig_type = type;
133 mod = type->ctype.modifiers;
134 if (width < bits_in_int)
135 return &int_ctype;
137 /* If char/short has as many bits as int, it still gets "promoted" */
138 if (mod & (MOD_CHAR | MOD_SHORT)) {
139 if (mod & MOD_UNSIGNED)
140 return &uint_ctype;
141 return &int_ctype;
143 return orig_type;
147 * integer part of usual arithmetic conversions:
148 * integer promotions are applied
149 * if left and right are identical, we are done
150 * if signedness is the same, convert one with lower rank
151 * unless unsigned argument has rank lower than signed one, convert the
152 * signed one.
153 * if signed argument is bigger than unsigned one, convert the unsigned.
154 * otherwise, convert signed.
156 * Leaving aside the integer promotions, that is equivalent to
157 * if identical, don't convert
158 * if left is bigger than right, convert right
159 * if right is bigger than left, convert right
160 * otherwise, if signedness is the same, convert one with lower rank
161 * otherwise convert the signed one.
163 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
165 unsigned long lmod, rmod;
167 left = integer_promotion(left);
168 right = integer_promotion(right);
170 if (left == right)
171 goto left;
173 if (left->bit_size > right->bit_size)
174 goto left;
176 if (right->bit_size > left->bit_size)
177 goto right;
179 lmod = left->ctype.modifiers;
180 rmod = right->ctype.modifiers;
181 if ((lmod ^ rmod) & MOD_UNSIGNED) {
182 if (lmod & MOD_UNSIGNED)
183 goto left;
184 } else if ((lmod & ~rmod) & (MOD_LONG_ALL))
185 goto left;
186 right:
187 left = right;
188 left:
189 return left;
192 static int same_cast_type(struct symbol *orig, struct symbol *new)
194 return orig->bit_size == new->bit_size && orig->bit_offset == orig->bit_offset;
197 static struct symbol *base_type(struct symbol *node, unsigned long *modp, unsigned long *asp)
199 unsigned long mod, as;
201 mod = 0; as = 0;
202 while (node) {
203 mod |= node->ctype.modifiers;
204 as |= node->ctype.as;
205 if (node->type == SYM_NODE) {
206 node = node->ctype.base_type;
207 continue;
209 break;
211 *modp = mod & ~MOD_IGNORE;
212 *asp = as;
213 return node;
216 static int is_same_type(struct expression *expr, struct symbol *new)
218 struct symbol *old = expr->ctype;
219 unsigned long oldmod, newmod, oldas, newas;
221 old = base_type(old, &oldmod, &oldas);
222 new = base_type(new, &newmod, &newas);
224 /* Same base type, same address space? */
225 if (old == new && oldas == newas) {
226 unsigned long difmod;
228 /* Check the modifier bits. */
229 difmod = (oldmod ^ newmod) & ~MOD_NOCAST;
231 /* Exact same type? */
232 if (!difmod)
233 return 1;
236 * Not the same type, but differs only in "const".
237 * Don't warn about MOD_NOCAST.
239 if (difmod == MOD_CONST)
240 return 0;
242 if ((oldmod | newmod) & MOD_NOCAST) {
243 const char *tofrom = "to/from";
244 if (!(newmod & MOD_NOCAST))
245 tofrom = "from";
246 if (!(oldmod & MOD_NOCAST))
247 tofrom = "to";
248 warning(expr->pos, "implicit cast %s nocast type", tofrom);
250 return 0;
253 static void
254 warn_for_different_enum_types (struct position pos,
255 struct symbol *typea,
256 struct symbol *typeb)
258 if (!Wenum_mismatch)
259 return;
260 if (typea->type == SYM_NODE)
261 typea = typea->ctype.base_type;
262 if (typeb->type == SYM_NODE)
263 typeb = typeb->ctype.base_type;
265 if (typea == typeb)
266 return;
268 if (typea->type == SYM_ENUM && typeb->type == SYM_ENUM) {
269 warning(pos, "mixing different enum types");
270 info(pos, " %s versus", show_typename(typea));
271 info(pos, " %s", show_typename(typeb));
276 * This gets called for implicit casts in assignments and
277 * integer promotion. We often want to try to move the
278 * cast down, because the ops involved may have been
279 * implicitly cast up, and we can get rid of the casts
280 * early.
282 static struct expression * cast_to(struct expression *old, struct symbol *type)
284 struct expression *expr;
286 warn_for_different_enum_types (old->pos, old->ctype, type);
288 if (old->ctype != &null_ctype && is_same_type(old, type))
289 return old;
292 * See if we can simplify the op. Move the cast down.
294 switch (old->type) {
295 case EXPR_PREOP:
296 if (old->ctype->bit_size < type->bit_size)
297 break;
298 if (old->op == '~') {
299 old->ctype = type;
300 old->unop = cast_to(old->unop, type);
301 return old;
303 break;
305 case EXPR_IMPLIED_CAST:
306 warn_for_different_enum_types(old->pos, old->ctype, type);
308 if (old->ctype->bit_size >= type->bit_size) {
309 struct expression *orig = old->cast_expression;
310 if (same_cast_type(orig->ctype, type))
311 return orig;
312 if (old->ctype->bit_offset == type->bit_offset) {
313 old->ctype = type;
314 old->cast_type = type;
315 return old;
318 break;
320 default:
321 /* nothing */;
324 expr = alloc_expression(old->pos, EXPR_IMPLIED_CAST);
325 expr->flags = old->flags;
326 expr->ctype = type;
327 expr->cast_type = type;
328 expr->cast_expression = old;
329 return expr;
332 enum {
333 TYPE_NUM = 1,
334 TYPE_BITFIELD = 2,
335 TYPE_RESTRICT = 4,
336 TYPE_FLOAT = 8,
337 TYPE_PTR = 16,
338 TYPE_COMPOUND = 32,
339 TYPE_FOULED = 64,
340 TYPE_FN = 128,
343 static inline int classify_type(struct symbol *type, struct symbol **base)
345 static int type_class[SYM_BAD + 1] = {
346 [SYM_PTR] = TYPE_PTR,
347 [SYM_FN] = TYPE_PTR | TYPE_FN,
348 [SYM_ARRAY] = TYPE_PTR | TYPE_COMPOUND,
349 [SYM_STRUCT] = TYPE_COMPOUND,
350 [SYM_UNION] = TYPE_COMPOUND,
351 [SYM_BITFIELD] = TYPE_NUM | TYPE_BITFIELD,
352 [SYM_RESTRICT] = TYPE_NUM | TYPE_RESTRICT,
353 [SYM_FOULED] = TYPE_NUM | TYPE_RESTRICT | TYPE_FOULED,
355 if (type->type == SYM_NODE)
356 type = type->ctype.base_type;
357 if (type->type == SYM_TYPEOF) {
358 type = evaluate_expression(type->initializer);
359 if (!type)
360 type = &bad_ctype;
361 else if (type->type == SYM_NODE)
362 type = type->ctype.base_type;
364 if (type->type == SYM_ENUM)
365 type = type->ctype.base_type;
366 *base = type;
367 if (type->type == SYM_BASETYPE) {
368 if (type->ctype.base_type == &int_type)
369 return TYPE_NUM;
370 if (type->ctype.base_type == &fp_type)
371 return TYPE_NUM | TYPE_FLOAT;
373 return type_class[type->type];
376 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
378 static inline int is_string_type(struct symbol *type)
380 if (type->type == SYM_NODE)
381 type = type->ctype.base_type;
382 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
385 static struct symbol *bad_expr_type(struct expression *expr)
387 sparse_error(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
388 switch (expr->type) {
389 case EXPR_BINOP:
390 case EXPR_COMPARE:
391 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
392 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
393 break;
394 case EXPR_PREOP:
395 case EXPR_POSTOP:
396 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
397 break;
398 default:
399 break;
402 expr->flags = 0;
403 return expr->ctype = &bad_ctype;
406 static int restricted_value(struct expression *v, struct symbol *type)
408 if (v->type != EXPR_VALUE)
409 return 1;
410 if (v->value != 0)
411 return 1;
412 return 0;
415 static int restricted_binop(int op, struct symbol *type)
417 switch (op) {
418 case '&':
419 case '=':
420 case SPECIAL_AND_ASSIGN:
421 case SPECIAL_OR_ASSIGN:
422 case SPECIAL_XOR_ASSIGN:
423 return 1; /* unfoul */
424 case '|':
425 case '^':
426 case '?':
427 return 2; /* keep fouled */
428 case SPECIAL_EQUAL:
429 case SPECIAL_NOTEQUAL:
430 return 3; /* warn if fouled */
431 default:
432 return 0; /* warn */
436 static int restricted_unop(int op, struct symbol **type)
438 if (op == '~') {
439 if ((*type)->bit_size < bits_in_int)
440 *type = befoul(*type);
441 return 0;
442 } if (op == '+')
443 return 0;
444 return 1;
447 /* type should be SYM_FOULED */
448 static inline struct symbol *unfoul(struct symbol *type)
450 return type->ctype.base_type;
453 static struct symbol *restricted_binop_type(int op,
454 struct expression *left,
455 struct expression *right,
456 int lclass, int rclass,
457 struct symbol *ltype,
458 struct symbol *rtype)
460 struct symbol *ctype = NULL;
461 if (lclass & TYPE_RESTRICT) {
462 if (rclass & TYPE_RESTRICT) {
463 if (ltype == rtype) {
464 ctype = ltype;
465 } else if (lclass & TYPE_FOULED) {
466 if (unfoul(ltype) == rtype)
467 ctype = ltype;
468 } else if (rclass & TYPE_FOULED) {
469 if (unfoul(rtype) == ltype)
470 ctype = rtype;
472 } else {
473 if (!restricted_value(right, ltype))
474 ctype = ltype;
476 } else if (!restricted_value(left, rtype))
477 ctype = rtype;
479 if (ctype) {
480 switch (restricted_binop(op, ctype)) {
481 case 1:
482 if ((lclass ^ rclass) & TYPE_FOULED)
483 ctype = unfoul(ctype);
484 break;
485 case 3:
486 if (!(lclass & rclass & TYPE_FOULED))
487 break;
488 case 0:
489 ctype = NULL;
490 default:
491 break;
495 return ctype;
498 static inline void unrestrict(struct expression *expr,
499 int class, struct symbol **ctype)
501 if (class & TYPE_RESTRICT) {
502 if (class & TYPE_FOULED)
503 *ctype = unfoul(*ctype);
504 warning(expr->pos, "%s degrades to integer",
505 show_typename(*ctype));
506 *ctype = (*ctype)->ctype.base_type; /* get to arithmetic type */
510 static struct symbol *usual_conversions(int op,
511 struct expression *left,
512 struct expression *right,
513 int lclass, int rclass,
514 struct symbol *ltype,
515 struct symbol *rtype)
517 struct symbol *ctype;
519 warn_for_different_enum_types(right->pos, left->ctype, right->ctype);
521 if ((lclass | rclass) & TYPE_RESTRICT)
522 goto Restr;
524 Normal:
525 if (!(lclass & TYPE_FLOAT)) {
526 if (!(rclass & TYPE_FLOAT))
527 return bigger_int_type(ltype, rtype);
528 else
529 return rtype;
530 } else if (rclass & TYPE_FLOAT) {
531 unsigned long lmod = ltype->ctype.modifiers;
532 unsigned long rmod = rtype->ctype.modifiers;
533 if (rmod & ~lmod & (MOD_LONG_ALL))
534 return rtype;
535 else
536 return ltype;
537 } else
538 return ltype;
540 Restr:
541 ctype = restricted_binop_type(op, left, right,
542 lclass, rclass, ltype, rtype);
543 if (ctype)
544 return ctype;
546 unrestrict(left, lclass, &ltype);
547 unrestrict(right, rclass, &rtype);
549 goto Normal;
552 static inline int lvalue_expression(struct expression *expr)
554 return expr->type == EXPR_PREOP && expr->op == '*';
557 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *itype)
559 struct expression *index = expr->right;
560 struct symbol *ctype, *base;
561 int multiply;
563 classify_type(degenerate(expr->left), &ctype);
564 base = examine_pointer_target(ctype);
566 if (!base) {
567 expression_error(expr, "missing type information");
568 return NULL;
570 if (is_function(base)) {
571 expression_error(expr, "arithmetics on pointers to functions");
572 return NULL;
575 /* Get the size of whatever the pointer points to */
576 multiply = is_void_type(base) ? 1 : bits_to_bytes(base->bit_size);
578 if (ctype == &null_ctype)
579 ctype = &ptr_ctype;
580 expr->ctype = ctype;
582 if (multiply == 1 && itype->bit_size >= bits_in_pointer)
583 return ctype;
585 if (index->type == EXPR_VALUE) {
586 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
587 unsigned long long v = index->value, mask;
588 mask = 1ULL << (itype->bit_size - 1);
589 if (v & mask)
590 v |= -mask;
591 else
592 v &= mask - 1;
593 v *= multiply;
594 mask = 1ULL << (bits_in_pointer - 1);
595 v &= mask | (mask - 1);
596 val->value = v;
597 val->ctype = ssize_t_ctype;
598 expr->right = val;
599 return ctype;
602 if (itype->bit_size < bits_in_pointer)
603 index = cast_to(index, ssize_t_ctype);
605 if (multiply > 1) {
606 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
607 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
609 val->ctype = ssize_t_ctype;
610 val->value = multiply;
612 mul->op = '*';
613 mul->ctype = ssize_t_ctype;
614 mul->left = index;
615 mul->right = val;
616 index = mul;
619 expr->right = index;
620 return ctype;
623 static void examine_fn_arguments(struct symbol *fn);
625 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
627 const char *type_difference(struct ctype *c1, struct ctype *c2,
628 unsigned long mod1, unsigned long mod2)
630 unsigned long as1 = c1->as, as2 = c2->as;
631 struct symbol *t1 = c1->base_type;
632 struct symbol *t2 = c2->base_type;
633 int move1 = 1, move2 = 1;
634 mod1 |= c1->modifiers;
635 mod2 |= c2->modifiers;
636 for (;;) {
637 unsigned long diff;
638 int type;
639 struct symbol *base1 = t1->ctype.base_type;
640 struct symbol *base2 = t2->ctype.base_type;
643 * FIXME! Collect alignment and context too here!
645 if (move1) {
646 if (t1 && t1->type != SYM_PTR) {
647 mod1 |= t1->ctype.modifiers;
648 as1 |= t1->ctype.as;
650 move1 = 0;
653 if (move2) {
654 if (t2 && t2->type != SYM_PTR) {
655 mod2 |= t2->ctype.modifiers;
656 as2 |= t2->ctype.as;
658 move2 = 0;
661 if (t1 == t2)
662 break;
663 if (!t1 || !t2)
664 return "different types";
666 if (t1->type == SYM_NODE || t1->type == SYM_ENUM) {
667 t1 = base1;
668 move1 = 1;
669 if (!t1)
670 return "bad types";
671 continue;
674 if (t2->type == SYM_NODE || t2->type == SYM_ENUM) {
675 t2 = base2;
676 move2 = 1;
677 if (!t2)
678 return "bad types";
679 continue;
682 move1 = move2 = 1;
683 type = t1->type;
684 if (type != t2->type)
685 return "different base types";
687 switch (type) {
688 default:
689 sparse_error(t1->pos,
690 "internal error: bad type in derived(%d)",
691 type);
692 return "bad types";
693 case SYM_RESTRICT:
694 return "different base types";
695 case SYM_UNION:
696 case SYM_STRUCT:
697 /* allow definition of incomplete structs and unions */
698 if (t1->ident == t2->ident)
699 return NULL;
700 return "different base types";
701 case SYM_ARRAY:
702 /* XXX: we ought to compare sizes */
703 break;
704 case SYM_PTR:
705 if (as1 != as2)
706 return "different address spaces";
707 /* MOD_SPECIFIER is due to idiocy in parse.c */
708 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SPECIFIER)
709 return "different modifiers";
710 /* we could be lazier here */
711 base1 = examine_pointer_target(t1);
712 base2 = examine_pointer_target(t2);
713 mod1 = t1->ctype.modifiers;
714 as1 = t1->ctype.as;
715 mod2 = t2->ctype.modifiers;
716 as2 = t2->ctype.as;
717 break;
718 case SYM_FN: {
719 struct symbol *arg1, *arg2;
720 int i;
722 if (as1 != as2)
723 return "different address spaces";
724 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
725 return "different modifiers";
726 mod1 = t1->ctype.modifiers;
727 as1 = t1->ctype.as;
728 mod2 = t2->ctype.modifiers;
729 as2 = t2->ctype.as;
731 if (base1->variadic != base2->variadic)
732 return "incompatible variadic arguments";
733 examine_fn_arguments(t1);
734 examine_fn_arguments(t2);
735 PREPARE_PTR_LIST(t1->arguments, arg1);
736 PREPARE_PTR_LIST(t2->arguments, arg2);
737 i = 1;
738 for (;;) {
739 const char *diffstr;
740 if (!arg1 && !arg2)
741 break;
742 if (!arg1 || !arg2)
743 return "different argument counts";
744 diffstr = type_difference(&arg1->ctype,
745 &arg2->ctype,
746 MOD_IGN, MOD_IGN);
747 if (diffstr) {
748 static char argdiff[80];
749 sprintf(argdiff, "incompatible argument %d (%s)", i, diffstr);
750 return argdiff;
752 NEXT_PTR_LIST(arg1);
753 NEXT_PTR_LIST(arg2);
754 i++;
756 FINISH_PTR_LIST(arg2);
757 FINISH_PTR_LIST(arg1);
758 break;
760 case SYM_BASETYPE:
761 if (as1 != as2)
762 return "different address spaces";
763 if (base1 != base2)
764 return "different base types";
765 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
766 if (!diff)
767 return NULL;
768 if (diff & MOD_SIZE)
769 return "different type sizes";
770 else if (diff & ~MOD_SIGNEDNESS)
771 return "different modifiers";
772 else
773 return "different signedness";
775 t1 = base1;
776 t2 = base2;
778 if (as1 != as2)
779 return "different address spaces";
780 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
781 return "different modifiers";
782 return NULL;
785 static void bad_null(struct expression *expr)
787 if (Wnon_pointer_null)
788 warning(expr->pos, "Using plain integer as NULL pointer");
791 static unsigned long target_qualifiers(struct symbol *type)
793 unsigned long mod = type->ctype.modifiers & MOD_IGN;
794 if (type->ctype.base_type && type->ctype.base_type->type == SYM_ARRAY)
795 mod = 0;
796 return mod;
799 static struct symbol *evaluate_ptr_sub(struct expression *expr)
801 const char *typediff;
802 struct symbol *ltype, *rtype;
803 struct expression *l = expr->left;
804 struct expression *r = expr->right;
805 struct symbol *lbase;
807 classify_type(degenerate(l), &ltype);
808 classify_type(degenerate(r), &rtype);
810 lbase = examine_pointer_target(ltype);
811 examine_pointer_target(rtype);
812 typediff = type_difference(&ltype->ctype, &rtype->ctype,
813 target_qualifiers(rtype),
814 target_qualifiers(ltype));
815 if (typediff)
816 expression_error(expr, "subtraction of different types can't work (%s)", typediff);
818 if (is_function(lbase)) {
819 expression_error(expr, "subtraction of functions? Share your drugs");
820 return NULL;
823 expr->ctype = ssize_t_ctype;
824 if (lbase->bit_size > bits_in_char) {
825 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
826 struct expression *div = expr;
827 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
828 unsigned long value = bits_to_bytes(lbase->bit_size);
830 val->ctype = size_t_ctype;
831 val->value = value;
833 if (value & (value-1)) {
834 if (Wptr_subtraction_blows)
835 warning(expr->pos, "potentially expensive pointer subtraction");
838 sub->op = '-';
839 sub->ctype = ssize_t_ctype;
840 sub->left = l;
841 sub->right = r;
843 div->op = '/';
844 div->left = sub;
845 div->right = val;
848 return ssize_t_ctype;
851 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
853 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
855 struct symbol *ctype;
857 if (!expr)
858 return NULL;
860 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
861 warning(expr->pos, "assignment expression in conditional");
863 ctype = evaluate_expression(expr);
864 if (ctype) {
865 if (is_safe_type(ctype))
866 warning(expr->pos, "testing a 'safe expression'");
869 return ctype;
872 static struct symbol *evaluate_logical(struct expression *expr)
874 if (!evaluate_conditional(expr->left, 0))
875 return NULL;
876 if (!evaluate_conditional(expr->right, 0))
877 return NULL;
879 /* the result is int [6.5.13(3), 6.5.14(3)] */
880 expr->ctype = &int_ctype;
881 if (expr->flags) {
882 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
883 expr->flags = 0;
885 return &int_ctype;
888 static struct symbol *evaluate_binop(struct expression *expr)
890 struct symbol *ltype, *rtype, *ctype;
891 int lclass = classify_type(expr->left->ctype, &ltype);
892 int rclass = classify_type(expr->right->ctype, &rtype);
893 int op = expr->op;
895 if (expr->flags) {
896 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
897 expr->flags = 0;
900 /* number op number */
901 if (lclass & rclass & TYPE_NUM) {
902 if ((lclass | rclass) & TYPE_FLOAT) {
903 switch (op) {
904 case '+': case '-': case '*': case '/':
905 break;
906 default:
907 return bad_expr_type(expr);
911 if (op == SPECIAL_LEFTSHIFT || op == SPECIAL_RIGHTSHIFT) {
912 // shifts do integer promotions, but that's it.
913 unrestrict(expr->left, lclass, &ltype);
914 unrestrict(expr->right, rclass, &rtype);
915 ctype = ltype = integer_promotion(ltype);
916 rtype = integer_promotion(rtype);
917 } else {
918 // The rest do usual conversions
919 const unsigned left_not = expr->left->type == EXPR_PREOP
920 && expr->left->op == '!';
921 const unsigned right_not = expr->right->type == EXPR_PREOP
922 && expr->right->op == '!';
923 if ((op == '&' || op == '|') && (left_not || right_not))
924 warning(expr->pos, "dubious: %sx %c %sy",
925 left_not ? "!" : "",
927 right_not ? "!" : "");
929 ltype = usual_conversions(op, expr->left, expr->right,
930 lclass, rclass, ltype, rtype);
931 ctype = rtype = ltype;
934 expr->left = cast_to(expr->left, ltype);
935 expr->right = cast_to(expr->right, rtype);
936 expr->ctype = ctype;
937 return ctype;
940 /* pointer (+|-) integer */
941 if (lclass & TYPE_PTR && is_int(rclass) && (op == '+' || op == '-')) {
942 unrestrict(expr->right, rclass, &rtype);
943 return evaluate_ptr_add(expr, rtype);
946 /* integer + pointer */
947 if (rclass & TYPE_PTR && is_int(lclass) && op == '+') {
948 struct expression *index = expr->left;
949 unrestrict(index, lclass, &ltype);
950 expr->left = expr->right;
951 expr->right = index;
952 return evaluate_ptr_add(expr, ltype);
955 /* pointer - pointer */
956 if (lclass & rclass & TYPE_PTR && expr->op == '-')
957 return evaluate_ptr_sub(expr);
959 return bad_expr_type(expr);
962 static struct symbol *evaluate_comma(struct expression *expr)
964 expr->ctype = degenerate(expr->right);
965 if (expr->ctype == &null_ctype)
966 expr->ctype = &ptr_ctype;
967 expr->flags &= expr->left->flags & expr->right->flags;
968 return expr->ctype;
971 static int modify_for_unsigned(int op)
973 if (op == '<')
974 op = SPECIAL_UNSIGNED_LT;
975 else if (op == '>')
976 op = SPECIAL_UNSIGNED_GT;
977 else if (op == SPECIAL_LTE)
978 op = SPECIAL_UNSIGNED_LTE;
979 else if (op == SPECIAL_GTE)
980 op = SPECIAL_UNSIGNED_GTE;
981 return op;
984 static inline int is_null_pointer_constant(struct expression *e)
986 if (e->ctype == &null_ctype)
987 return 1;
988 if (!(e->flags & Int_const_expr))
989 return 0;
990 return is_zero_constant(e) ? 2 : 0;
993 static struct symbol *evaluate_compare(struct expression *expr)
995 struct expression *left = expr->left, *right = expr->right;
996 struct symbol *ltype, *rtype, *lbase, *rbase;
997 int lclass = classify_type(degenerate(left), &ltype);
998 int rclass = classify_type(degenerate(right), &rtype);
999 struct symbol *ctype;
1000 const char *typediff;
1002 if (expr->flags) {
1003 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
1004 expr->flags = 0;
1007 /* Type types? */
1008 if (is_type_type(ltype) && is_type_type(rtype))
1009 goto OK;
1011 if (is_safe_type(left->ctype) || is_safe_type(right->ctype))
1012 warning(expr->pos, "testing a 'safe expression'");
1014 /* number on number */
1015 if (lclass & rclass & TYPE_NUM) {
1016 ctype = usual_conversions(expr->op, expr->left, expr->right,
1017 lclass, rclass, ltype, rtype);
1018 expr->left = cast_to(expr->left, ctype);
1019 expr->right = cast_to(expr->right, ctype);
1020 if (ctype->ctype.modifiers & MOD_UNSIGNED)
1021 expr->op = modify_for_unsigned(expr->op);
1022 goto OK;
1025 /* at least one must be a pointer */
1026 if (!((lclass | rclass) & TYPE_PTR))
1027 return bad_expr_type(expr);
1029 /* equality comparisons can be with null pointer constants */
1030 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1031 int is_null1 = is_null_pointer_constant(left);
1032 int is_null2 = is_null_pointer_constant(right);
1033 if (is_null1 == 2)
1034 bad_null(left);
1035 if (is_null2 == 2)
1036 bad_null(right);
1037 if (is_null1 && is_null2) {
1038 int positive = expr->op == SPECIAL_EQUAL;
1039 expr->type = EXPR_VALUE;
1040 expr->value = positive;
1041 goto OK;
1043 if (is_null1 && (rclass & TYPE_PTR)) {
1044 left = cast_to(left, rtype);
1045 goto OK;
1047 if (is_null2 && (lclass & TYPE_PTR)) {
1048 right = cast_to(right, ltype);
1049 goto OK;
1052 /* both should be pointers */
1053 if (!(lclass & rclass & TYPE_PTR))
1054 return bad_expr_type(expr);
1055 expr->op = modify_for_unsigned(expr->op);
1057 lbase = examine_pointer_target(ltype);
1058 rbase = examine_pointer_target(rtype);
1060 /* they also have special treatment for pointers to void */
1061 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1062 if (ltype->ctype.as == rtype->ctype.as) {
1063 if (lbase == &void_ctype) {
1064 right = cast_to(right, ltype);
1065 goto OK;
1067 if (rbase == &void_ctype) {
1068 left = cast_to(left, rtype);
1069 goto OK;
1074 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1075 target_qualifiers(rtype),
1076 target_qualifiers(ltype));
1077 if (!typediff)
1078 goto OK;
1080 expression_error(expr, "incompatible types in comparison expression (%s)", typediff);
1081 return NULL;
1084 /* the result is int [6.5.8(6), 6.5.9(3)]*/
1085 expr->ctype = &int_ctype;
1086 return &int_ctype;
1090 * NOTE! The degenerate case of "x ? : y", where we don't
1091 * have a true case, this will possibly promote "x" to the
1092 * same type as "y", and thus _change_ the conditional
1093 * test in the expression. But since promotion is "safe"
1094 * for testing, that's OK.
1096 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1098 struct expression **true;
1099 struct symbol *ctype, *ltype, *rtype, *lbase, *rbase;
1100 int lclass, rclass;
1101 const char * typediff;
1102 int qual;
1104 if (!evaluate_conditional(expr->conditional, 0))
1105 return NULL;
1106 if (!evaluate_expression(expr->cond_false))
1107 return NULL;
1109 ctype = degenerate(expr->conditional);
1110 rtype = degenerate(expr->cond_false);
1112 true = &expr->conditional;
1113 ltype = ctype;
1114 if (expr->cond_true) {
1115 if (!evaluate_expression(expr->cond_true))
1116 return NULL;
1117 ltype = degenerate(expr->cond_true);
1118 true = &expr->cond_true;
1121 if (expr->flags) {
1122 int flags = expr->conditional->flags & Int_const_expr;
1123 flags &= (*true)->flags & expr->cond_false->flags;
1124 if (!flags)
1125 expr->flags = 0;
1128 lclass = classify_type(ltype, &ltype);
1129 rclass = classify_type(rtype, &rtype);
1130 if (lclass & rclass & TYPE_NUM) {
1131 ctype = usual_conversions('?', *true, expr->cond_false,
1132 lclass, rclass, ltype, rtype);
1133 *true = cast_to(*true, ctype);
1134 expr->cond_false = cast_to(expr->cond_false, ctype);
1135 goto out;
1138 if ((lclass | rclass) & TYPE_PTR) {
1139 int is_null1 = is_null_pointer_constant(*true);
1140 int is_null2 = is_null_pointer_constant(expr->cond_false);
1142 if (is_null1 && is_null2) {
1143 *true = cast_to(*true, &ptr_ctype);
1144 expr->cond_false = cast_to(expr->cond_false, &ptr_ctype);
1145 ctype = &ptr_ctype;
1146 goto out;
1148 if (is_null1 && (rclass & TYPE_PTR)) {
1149 if (is_null1 == 2)
1150 bad_null(*true);
1151 *true = cast_to(*true, rtype);
1152 ctype = rtype;
1153 goto out;
1155 if (is_null2 && (lclass & TYPE_PTR)) {
1156 if (is_null2 == 2)
1157 bad_null(expr->cond_false);
1158 expr->cond_false = cast_to(expr->cond_false, ltype);
1159 ctype = ltype;
1160 goto out;
1162 if (!(lclass & rclass & TYPE_PTR)) {
1163 typediff = "different types";
1164 goto Err;
1166 /* OK, it's pointer on pointer */
1167 if (ltype->ctype.as != rtype->ctype.as) {
1168 typediff = "different address spaces";
1169 goto Err;
1172 /* need to be lazier here */
1173 lbase = examine_pointer_target(ltype);
1174 rbase = examine_pointer_target(rtype);
1175 qual = target_qualifiers(ltype) | target_qualifiers(rtype);
1177 if (lbase == &void_ctype) {
1178 /* XXX: pointers to function should warn here */
1179 ctype = ltype;
1180 goto Qual;
1183 if (rbase == &void_ctype) {
1184 /* XXX: pointers to function should warn here */
1185 ctype = rtype;
1186 goto Qual;
1188 /* XXX: that should be pointer to composite */
1189 ctype = ltype;
1190 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1191 qual, qual);
1192 if (!typediff)
1193 goto Qual;
1194 goto Err;
1197 /* void on void, struct on same struct, union on same union */
1198 if (ltype == rtype) {
1199 ctype = ltype;
1200 goto out;
1202 typediff = "different base types";
1204 Err:
1205 expression_error(expr, "incompatible types in conditional expression (%s)", typediff);
1206 return NULL;
1208 out:
1209 expr->ctype = ctype;
1210 return ctype;
1212 Qual:
1213 if (qual & ~ctype->ctype.modifiers) {
1214 struct symbol *sym = alloc_symbol(ctype->pos, SYM_PTR);
1215 *sym = *ctype;
1216 sym->ctype.modifiers |= qual;
1217 ctype = sym;
1219 *true = cast_to(*true, ctype);
1220 expr->cond_false = cast_to(expr->cond_false, ctype);
1221 goto out;
1224 /* FP assignments can not do modulo or bit operations */
1225 static int compatible_float_op(int op)
1227 return op == SPECIAL_ADD_ASSIGN ||
1228 op == SPECIAL_SUB_ASSIGN ||
1229 op == SPECIAL_MUL_ASSIGN ||
1230 op == SPECIAL_DIV_ASSIGN;
1233 static int evaluate_assign_op(struct expression *expr)
1235 struct symbol *target = expr->left->ctype;
1236 struct symbol *source = expr->right->ctype;
1237 struct symbol *t, *s;
1238 int tclass = classify_type(target, &t);
1239 int sclass = classify_type(source, &s);
1240 int op = expr->op;
1242 if (tclass & sclass & TYPE_NUM) {
1243 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1244 expression_error(expr, "invalid assignment");
1245 return 0;
1247 if (tclass & TYPE_RESTRICT) {
1248 if (!restricted_binop(op, t)) {
1249 warning(expr->pos, "bad assignment (%s) to %s",
1250 show_special(op), show_typename(t));
1251 expr->right = cast_to(expr->right, target);
1252 return 0;
1254 /* allowed assignments unfoul */
1255 if (sclass & TYPE_FOULED && unfoul(s) == t)
1256 goto Cast;
1257 if (!restricted_value(expr->right, t))
1258 return 1;
1259 } else if (!(sclass & TYPE_RESTRICT))
1260 goto Cast;
1261 /* source and target would better be identical restricted */
1262 if (t == s)
1263 return 1;
1264 warning(expr->pos, "invalid assignment: %s", show_special(op));
1265 info(expr->pos, " left side has type %s", show_typename(t));
1266 info(expr->pos, " right side has type %s", show_typename(s));
1267 expr->right = cast_to(expr->right, target);
1268 return 0;
1270 if (tclass == TYPE_PTR && is_int(sclass)) {
1271 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1272 unrestrict(expr->right, sclass, &s);
1273 evaluate_ptr_add(expr, s);
1274 return 1;
1276 expression_error(expr, "invalid pointer assignment");
1277 return 0;
1280 expression_error(expr, "invalid assignment");
1281 return 0;
1283 Cast:
1284 expr->right = cast_to(expr->right, target);
1285 return 1;
1288 static int whitelist_pointers(struct symbol *t1, struct symbol *t2)
1290 if (t1 == t2)
1291 return 0; /* yes, 0 - we don't want a cast_to here */
1292 if (t1 == &void_ctype)
1293 return 1;
1294 if (t2 == &void_ctype)
1295 return 1;
1296 if (classify_type(t1, &t1) != TYPE_NUM)
1297 return 0;
1298 if (classify_type(t2, &t2) != TYPE_NUM)
1299 return 0;
1300 if (t1 == t2)
1301 return 1;
1302 if (t1->ctype.modifiers & t2->ctype.modifiers & MOD_CHAR)
1303 return 1;
1304 if ((t1->ctype.modifiers ^ t2->ctype.modifiers) & MOD_SIZE)
1305 return 0;
1306 return !Wtypesign;
1309 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1310 struct expression **rp, const char *where)
1312 const char *typediff;
1313 struct symbol *source = degenerate(*rp);
1314 struct symbol *t, *s;
1315 int tclass = classify_type(target, &t);
1316 int sclass = classify_type(source, &s);
1318 if (tclass & sclass & TYPE_NUM) {
1319 if (tclass & TYPE_RESTRICT) {
1320 /* allowed assignments unfoul */
1321 if (sclass & TYPE_FOULED && unfoul(s) == t)
1322 goto Cast;
1323 if (!restricted_value(*rp, target))
1324 return 1;
1325 if (s == t)
1326 return 1;
1327 } else if (!(sclass & TYPE_RESTRICT))
1328 goto Cast;
1329 typediff = "different base types";
1330 goto Err;
1333 if (tclass == TYPE_PTR) {
1334 unsigned long mod1, mod2;
1335 struct symbol *b1, *b2;
1336 // NULL pointer is always OK
1337 int is_null = is_null_pointer_constant(*rp);
1338 if (is_null) {
1339 if (is_null == 2)
1340 bad_null(*rp);
1341 goto Cast;
1343 if (!(sclass & TYPE_PTR)) {
1344 typediff = "different base types";
1345 goto Err;
1347 b1 = examine_pointer_target(t);
1348 b2 = examine_pointer_target(s);
1349 mod1 = target_qualifiers(t);
1350 mod2 = target_qualifiers(s);
1351 if (whitelist_pointers(b1, b2)) {
1353 * assignments to/from void * are OK, provided that
1354 * we do not remove qualifiers from pointed to [C]
1355 * or mix address spaces [sparse].
1357 if (t->ctype.as != s->ctype.as) {
1358 typediff = "different address spaces";
1359 goto Err;
1361 if (mod2 & ~mod1) {
1362 typediff = "different modifiers";
1363 goto Err;
1365 goto Cast;
1367 /* It's OK if the target is more volatile or const than the source */
1368 typediff = type_difference(&t->ctype, &s->ctype, 0, mod1);
1369 if (typediff)
1370 goto Err;
1371 return 1;
1374 if ((tclass & TYPE_COMPOUND) && s == t)
1375 return 1;
1377 if (tclass & TYPE_NUM) {
1378 /* XXX: need to turn into comparison with NULL */
1379 if (t == &bool_ctype && (sclass & TYPE_PTR))
1380 goto Cast;
1381 typediff = "different base types";
1382 goto Err;
1384 typediff = "invalid types";
1386 Err:
1387 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1388 info(expr->pos, " expected %s", show_typename(target));
1389 info(expr->pos, " got %s", show_typename(source));
1390 *rp = cast_to(*rp, target);
1391 return 0;
1392 Cast:
1393 *rp = cast_to(*rp, target);
1394 return 1;
1397 static void mark_assigned(struct expression *expr)
1399 struct symbol *sym;
1401 if (!expr)
1402 return;
1403 switch (expr->type) {
1404 case EXPR_SYMBOL:
1405 sym = expr->symbol;
1406 if (!sym)
1407 return;
1408 if (sym->type != SYM_NODE)
1409 return;
1410 sym->ctype.modifiers |= MOD_ASSIGNED;
1411 return;
1413 case EXPR_BINOP:
1414 mark_assigned(expr->left);
1415 mark_assigned(expr->right);
1416 return;
1417 case EXPR_CAST:
1418 case EXPR_FORCE_CAST:
1419 mark_assigned(expr->cast_expression);
1420 return;
1421 case EXPR_SLICE:
1422 mark_assigned(expr->base);
1423 return;
1424 default:
1425 /* Hmm? */
1426 return;
1430 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1432 if (type->ctype.modifiers & MOD_CONST)
1433 expression_error(left, "assignment to const expression");
1435 /* We know left is an lvalue, so it's a "preop-*" */
1436 mark_assigned(left->unop);
1439 static struct symbol *evaluate_assignment(struct expression *expr)
1441 struct expression *left = expr->left;
1442 struct expression *where = expr;
1443 struct symbol *ltype;
1445 if (!lvalue_expression(left)) {
1446 expression_error(expr, "not an lvalue");
1447 return NULL;
1450 ltype = left->ctype;
1452 if (expr->op != '=') {
1453 if (!evaluate_assign_op(expr))
1454 return NULL;
1455 } else {
1456 if (!compatible_assignment_types(where, ltype, &expr->right, "assignment"))
1457 return NULL;
1460 evaluate_assign_to(left, ltype);
1462 expr->ctype = ltype;
1463 return ltype;
1466 static void examine_fn_arguments(struct symbol *fn)
1468 struct symbol *s;
1470 FOR_EACH_PTR(fn->arguments, s) {
1471 struct symbol *arg = evaluate_symbol(s);
1472 /* Array/function arguments silently degenerate into pointers */
1473 if (arg) {
1474 struct symbol *ptr;
1475 switch(arg->type) {
1476 case SYM_ARRAY:
1477 case SYM_FN:
1478 ptr = alloc_symbol(s->pos, SYM_PTR);
1479 if (arg->type == SYM_ARRAY)
1480 ptr->ctype = arg->ctype;
1481 else
1482 ptr->ctype.base_type = arg;
1483 ptr->ctype.as |= s->ctype.as;
1484 ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1486 s->ctype.base_type = ptr;
1487 s->ctype.as = 0;
1488 s->ctype.modifiers &= ~MOD_PTRINHERIT;
1489 s->bit_size = 0;
1490 s->examined = 0;
1491 examine_symbol_type(s);
1492 break;
1493 default:
1494 /* nothing */
1495 break;
1498 } END_FOR_EACH_PTR(s);
1501 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1503 /* Take the modifiers of the pointer, and apply them to the member */
1504 mod |= sym->ctype.modifiers;
1505 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1506 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1507 *newsym = *sym;
1508 newsym->ctype.as = as;
1509 newsym->ctype.modifiers = mod;
1510 sym = newsym;
1512 return sym;
1515 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1517 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1518 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1520 node->ctype.base_type = ptr;
1521 ptr->bit_size = bits_in_pointer;
1522 ptr->ctype.alignment = pointer_alignment;
1524 node->bit_size = bits_in_pointer;
1525 node->ctype.alignment = pointer_alignment;
1527 access_symbol(sym);
1528 if (sym->ctype.modifiers & MOD_REGISTER) {
1529 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1530 sym->ctype.modifiers &= ~MOD_REGISTER;
1532 if (sym->type == SYM_NODE) {
1533 ptr->ctype.as |= sym->ctype.as;
1534 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1535 sym = sym->ctype.base_type;
1537 if (degenerate && sym->type == SYM_ARRAY) {
1538 ptr->ctype.as |= sym->ctype.as;
1539 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1540 sym = sym->ctype.base_type;
1542 ptr->ctype.base_type = sym;
1544 return node;
1547 /* Arrays degenerate into pointers on pointer arithmetic */
1548 static struct symbol *degenerate(struct expression *expr)
1550 struct symbol *ctype, *base;
1552 if (!expr)
1553 return NULL;
1554 ctype = expr->ctype;
1555 if (!ctype)
1556 return NULL;
1557 base = examine_symbol_type(ctype);
1558 if (ctype->type == SYM_NODE)
1559 base = ctype->ctype.base_type;
1561 * Arrays degenerate into pointers to the entries, while
1562 * functions degenerate into pointers to themselves.
1563 * If array was part of non-lvalue compound, we create a copy
1564 * of that compound first and then act as if we were dealing with
1565 * the corresponding field in there.
1567 switch (base->type) {
1568 case SYM_ARRAY:
1569 if (expr->type == EXPR_SLICE) {
1570 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1571 struct expression *e0, *e1, *e2, *e3, *e4;
1573 a->ctype.base_type = expr->base->ctype;
1574 a->bit_size = expr->base->ctype->bit_size;
1575 a->array_size = expr->base->ctype->array_size;
1577 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1578 e0->symbol = a;
1579 e0->ctype = &lazy_ptr_ctype;
1581 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1582 e1->unop = e0;
1583 e1->op = '*';
1584 e1->ctype = expr->base->ctype; /* XXX */
1586 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1587 e2->left = e1;
1588 e2->right = expr->base;
1589 e2->op = '=';
1590 e2->ctype = expr->base->ctype;
1592 if (expr->r_bitpos) {
1593 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1594 e3->op = '+';
1595 e3->left = e0;
1596 e3->right = alloc_const_expression(expr->pos,
1597 bits_to_bytes(expr->r_bitpos));
1598 e3->ctype = &lazy_ptr_ctype;
1599 } else {
1600 e3 = e0;
1603 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1604 e4->left = e2;
1605 e4->right = e3;
1606 e4->ctype = &lazy_ptr_ctype;
1608 expr->unop = e4;
1609 expr->type = EXPR_PREOP;
1610 expr->op = '*';
1612 case SYM_FN:
1613 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1614 expression_error(expr, "strange non-value function or array");
1615 return &bad_ctype;
1617 *expr = *expr->unop;
1618 ctype = create_pointer(expr, ctype, 1);
1619 expr->ctype = ctype;
1620 default:
1621 /* nothing */;
1623 return ctype;
1626 static struct symbol *evaluate_addressof(struct expression *expr)
1628 struct expression *op = expr->unop;
1629 struct symbol *ctype;
1631 if (op->op != '*' || op->type != EXPR_PREOP) {
1632 expression_error(expr, "not addressable");
1633 return NULL;
1635 ctype = op->ctype;
1636 *expr = *op->unop;
1637 expr->flags = 0;
1639 if (expr->type == EXPR_SYMBOL) {
1640 struct symbol *sym = expr->symbol;
1641 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1645 * symbol expression evaluation is lazy about the type
1646 * of the sub-expression, so we may have to generate
1647 * the type here if so..
1649 if (expr->ctype == &lazy_ptr_ctype) {
1650 ctype = create_pointer(expr, ctype, 0);
1651 expr->ctype = ctype;
1653 return expr->ctype;
1657 static struct symbol *evaluate_dereference(struct expression *expr)
1659 struct expression *op = expr->unop;
1660 struct symbol *ctype = op->ctype, *node, *target;
1662 /* Simplify: *&(expr) => (expr) */
1663 if (op->type == EXPR_PREOP && op->op == '&') {
1664 *expr = *op->unop;
1665 expr->flags = 0;
1666 return expr->ctype;
1669 /* Dereferencing a node drops all the node information. */
1670 if (ctype->type == SYM_NODE)
1671 ctype = ctype->ctype.base_type;
1673 node = alloc_symbol(expr->pos, SYM_NODE);
1674 target = ctype->ctype.base_type;
1676 switch (ctype->type) {
1677 default:
1678 expression_error(expr, "cannot dereference this type");
1679 return NULL;
1680 case SYM_PTR:
1681 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1682 merge_type(node, ctype);
1683 break;
1685 case SYM_ARRAY:
1686 if (!lvalue_expression(op)) {
1687 expression_error(op, "non-lvalue array??");
1688 return NULL;
1691 /* Do the implied "addressof" on the array */
1692 *op = *op->unop;
1695 * When an array is dereferenced, we need to pick
1696 * up the attributes of the original node too..
1698 merge_type(node, op->ctype);
1699 merge_type(node, ctype);
1700 break;
1703 node->bit_size = target->bit_size;
1704 node->array_size = target->array_size;
1706 expr->ctype = node;
1707 return node;
1711 * Unary post-ops: x++ and x--
1713 static struct symbol *evaluate_postop(struct expression *expr)
1715 struct expression *op = expr->unop;
1716 struct symbol *ctype = op->ctype;
1717 int class = classify_type(ctype, &ctype);
1718 int multiply = 0;
1720 if (!class || class & TYPE_COMPOUND) {
1721 expression_error(expr, "need scalar for ++/--");
1722 return NULL;
1724 if (!lvalue_expression(expr->unop)) {
1725 expression_error(expr, "need lvalue expression for ++/--");
1726 return NULL;
1729 if ((class & TYPE_RESTRICT) && restricted_unop(expr->op, &ctype))
1730 unrestrict(expr, class, &ctype);
1732 if (class & TYPE_NUM) {
1733 multiply = 1;
1734 } else if (class == TYPE_PTR) {
1735 struct symbol *target = examine_pointer_target(ctype);
1736 if (!is_function(target))
1737 multiply = bits_to_bytes(target->bit_size);
1740 if (multiply) {
1741 evaluate_assign_to(op, op->ctype);
1742 expr->op_value = multiply;
1743 expr->ctype = ctype;
1744 return ctype;
1747 expression_error(expr, "bad argument type for ++/--");
1748 return NULL;
1751 static struct symbol *evaluate_sign(struct expression *expr)
1753 struct symbol *ctype = expr->unop->ctype;
1754 int class = classify_type(ctype, &ctype);
1755 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1756 expr->flags = 0;
1757 /* should be an arithmetic type */
1758 if (!(class & TYPE_NUM))
1759 return bad_expr_type(expr);
1760 if (class & TYPE_RESTRICT)
1761 goto Restr;
1762 Normal:
1763 if (!(class & TYPE_FLOAT)) {
1764 ctype = integer_promotion(ctype);
1765 expr->unop = cast_to(expr->unop, ctype);
1766 } else if (expr->op != '~') {
1767 /* no conversions needed */
1768 } else {
1769 return bad_expr_type(expr);
1771 if (expr->op == '+')
1772 *expr = *expr->unop;
1773 expr->ctype = ctype;
1774 return ctype;
1775 Restr:
1776 if (restricted_unop(expr->op, &ctype))
1777 unrestrict(expr, class, &ctype);
1778 goto Normal;
1781 static struct symbol *evaluate_preop(struct expression *expr)
1783 struct symbol *ctype = expr->unop->ctype;
1785 switch (expr->op) {
1786 case '(':
1787 *expr = *expr->unop;
1788 return ctype;
1790 case '+':
1791 case '-':
1792 case '~':
1793 return evaluate_sign(expr);
1795 case '*':
1796 return evaluate_dereference(expr);
1798 case '&':
1799 return evaluate_addressof(expr);
1801 case SPECIAL_INCREMENT:
1802 case SPECIAL_DECREMENT:
1804 * From a type evaluation standpoint the preops are
1805 * the same as the postops
1807 return evaluate_postop(expr);
1809 case '!':
1810 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1811 expr->flags = 0;
1812 if (is_safe_type(ctype))
1813 warning(expr->pos, "testing a 'safe expression'");
1814 if (is_float_type(ctype)) {
1815 struct expression *arg = expr->unop;
1816 expr->type = EXPR_COMPARE;
1817 expr->op = SPECIAL_EQUAL;
1818 expr->left = arg;
1819 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1820 expr->right->ctype = ctype;
1821 expr->right->fvalue = 0;
1822 } else if (is_fouled_type(ctype)) {
1823 warning(expr->pos, "%s degrades to integer",
1824 show_typename(ctype->ctype.base_type));
1826 /* the result is int [6.5.3.3(5)]*/
1827 ctype = &int_ctype;
1828 break;
1830 default:
1831 break;
1833 expr->ctype = ctype;
1834 return ctype;
1837 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1839 struct ptr_list *head = (struct ptr_list *)_list;
1840 struct ptr_list *list = head;
1842 if (!head)
1843 return NULL;
1844 do {
1845 int i;
1846 for (i = 0; i < list->nr; i++) {
1847 struct symbol *sym = (struct symbol *) list->list[i];
1848 if (sym->ident) {
1849 if (sym->ident != ident)
1850 continue;
1851 *offset = sym->offset;
1852 return sym;
1853 } else {
1854 struct symbol *ctype = sym->ctype.base_type;
1855 struct symbol *sub;
1856 if (!ctype)
1857 continue;
1858 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1859 continue;
1860 sub = find_identifier(ident, ctype->symbol_list, offset);
1861 if (!sub)
1862 continue;
1863 *offset += sym->offset;
1864 return sub;
1867 } while ((list = list->next) != head);
1868 return NULL;
1871 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1873 struct expression *add;
1876 * Create a new add-expression
1878 * NOTE! Even if we just add zero, we need a new node
1879 * for the member pointer, since it has a different
1880 * type than the original pointer. We could make that
1881 * be just a cast, but the fact is, a node is a node,
1882 * so we might as well just do the "add zero" here.
1884 add = alloc_expression(expr->pos, EXPR_BINOP);
1885 add->op = '+';
1886 add->left = expr;
1887 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1888 add->right->ctype = &int_ctype;
1889 add->right->value = offset;
1892 * The ctype of the pointer will be lazily evaluated if
1893 * we ever take the address of this member dereference..
1895 add->ctype = &lazy_ptr_ctype;
1896 return add;
1899 /* structure/union dereference */
1900 static struct symbol *evaluate_member_dereference(struct expression *expr)
1902 int offset;
1903 struct symbol *ctype, *member;
1904 struct expression *deref = expr->deref, *add;
1905 struct ident *ident = expr->member;
1906 unsigned int mod;
1907 int address_space;
1909 if (!evaluate_expression(deref))
1910 return NULL;
1911 if (!ident) {
1912 expression_error(expr, "bad member name");
1913 return NULL;
1916 ctype = deref->ctype;
1917 examine_symbol_type(ctype);
1918 address_space = ctype->ctype.as;
1919 mod = ctype->ctype.modifiers;
1920 if (ctype->type == SYM_NODE) {
1921 ctype = ctype->ctype.base_type;
1922 address_space |= ctype->ctype.as;
1923 mod |= ctype->ctype.modifiers;
1925 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1926 expression_error(expr, "expected structure or union");
1927 return NULL;
1929 offset = 0;
1930 member = find_identifier(ident, ctype->symbol_list, &offset);
1931 if (!member) {
1932 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1933 const char *name = "<unnamed>";
1934 int namelen = 9;
1935 if (ctype->ident) {
1936 name = ctype->ident->name;
1937 namelen = ctype->ident->len;
1939 if (ctype->symbol_list)
1940 expression_error(expr, "no member '%s' in %s %.*s",
1941 show_ident(ident), type, namelen, name);
1942 else
1943 expression_error(expr, "using member '%s' in "
1944 "incomplete %s %.*s", show_ident(ident),
1945 type, namelen, name);
1946 return NULL;
1950 * The member needs to take on the address space and modifiers of
1951 * the "parent" type.
1953 member = convert_to_as_mod(member, address_space, mod);
1954 ctype = get_base_type(member);
1956 if (!lvalue_expression(deref)) {
1957 if (deref->type != EXPR_SLICE) {
1958 expr->base = deref;
1959 expr->r_bitpos = 0;
1960 } else {
1961 expr->base = deref->base;
1962 expr->r_bitpos = deref->r_bitpos;
1964 expr->r_bitpos += bytes_to_bits(offset);
1965 expr->type = EXPR_SLICE;
1966 expr->r_nrbits = member->bit_size;
1967 expr->r_bitpos += member->bit_offset;
1968 expr->ctype = member;
1969 return member;
1972 deref = deref->unop;
1973 expr->deref = deref;
1975 add = evaluate_offset(deref, offset);
1976 expr->type = EXPR_PREOP;
1977 expr->op = '*';
1978 expr->unop = add;
1980 expr->ctype = member;
1981 return member;
1984 static int is_promoted(struct expression *expr)
1986 while (1) {
1987 switch (expr->type) {
1988 case EXPR_BINOP:
1989 case EXPR_SELECT:
1990 case EXPR_CONDITIONAL:
1991 return 1;
1992 case EXPR_COMMA:
1993 expr = expr->right;
1994 continue;
1995 case EXPR_PREOP:
1996 switch (expr->op) {
1997 case '(':
1998 expr = expr->unop;
1999 continue;
2000 case '+':
2001 case '-':
2002 case '~':
2003 return 1;
2004 default:
2005 return 0;
2007 default:
2008 return 0;
2014 static struct symbol *evaluate_cast(struct expression *);
2016 static struct symbol *evaluate_type_information(struct expression *expr)
2018 struct symbol *sym = expr->cast_type;
2019 if (!sym) {
2020 sym = evaluate_expression(expr->cast_expression);
2021 if (!sym)
2022 return NULL;
2024 * Expressions of restricted types will possibly get
2025 * promoted - check that here
2027 if (is_restricted_type(sym)) {
2028 if (sym->bit_size < bits_in_int && is_promoted(expr))
2029 sym = &int_ctype;
2030 } else if (is_fouled_type(sym)) {
2031 sym = &int_ctype;
2034 examine_symbol_type(sym);
2035 if (is_bitfield_type(sym)) {
2036 expression_error(expr, "trying to examine bitfield type");
2037 return NULL;
2039 return sym;
2042 static struct symbol *evaluate_sizeof(struct expression *expr)
2044 struct symbol *type;
2045 int size;
2047 type = evaluate_type_information(expr);
2048 if (!type)
2049 return NULL;
2051 size = type->bit_size;
2053 if (size < 0 && is_void_type(type)) {
2054 warning(expr->pos, "expression using sizeof(void)");
2055 size = bits_in_char;
2058 if (size == 1 && is_bool_type(type)) {
2059 warning(expr->pos, "expression using sizeof bool");
2060 size = bits_in_char;
2063 if (is_function(type->ctype.base_type)) {
2064 warning(expr->pos, "expression using sizeof on a function");
2065 size = bits_in_char;
2068 if ((size < 0) || (size & (bits_in_char - 1)))
2069 expression_error(expr, "cannot size expression");
2071 expr->type = EXPR_VALUE;
2072 expr->value = bits_to_bytes(size);
2073 expr->taint = 0;
2074 expr->ctype = size_t_ctype;
2075 return size_t_ctype;
2078 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
2080 struct symbol *type;
2081 int size;
2083 type = evaluate_type_information(expr);
2084 if (!type)
2085 return NULL;
2087 if (type->type == SYM_NODE)
2088 type = type->ctype.base_type;
2089 if (!type)
2090 return NULL;
2091 switch (type->type) {
2092 case SYM_ARRAY:
2093 break;
2094 case SYM_PTR:
2095 type = get_base_type(type);
2096 if (type)
2097 break;
2098 default:
2099 expression_error(expr, "expected pointer expression");
2100 return NULL;
2102 size = type->bit_size;
2103 if (size & (bits_in_char-1))
2104 size = 0;
2105 expr->type = EXPR_VALUE;
2106 expr->value = bits_to_bytes(size);
2107 expr->taint = 0;
2108 expr->ctype = size_t_ctype;
2109 return size_t_ctype;
2112 static struct symbol *evaluate_alignof(struct expression *expr)
2114 struct symbol *type;
2116 type = evaluate_type_information(expr);
2117 if (!type)
2118 return NULL;
2120 expr->type = EXPR_VALUE;
2121 expr->value = type->ctype.alignment;
2122 expr->taint = 0;
2123 expr->ctype = size_t_ctype;
2124 return size_t_ctype;
2127 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
2129 struct expression *expr;
2130 struct symbol_list *argument_types = fn->arguments;
2131 struct symbol *argtype;
2132 int i = 1;
2134 PREPARE_PTR_LIST(argument_types, argtype);
2135 FOR_EACH_PTR (head, expr) {
2136 struct expression **p = THIS_ADDRESS(expr);
2137 struct symbol *ctype, *target;
2138 ctype = evaluate_expression(expr);
2140 if (!ctype)
2141 return 0;
2143 target = argtype;
2144 if (!target) {
2145 struct symbol *type;
2146 int class = classify_type(ctype, &type);
2147 if (is_int(class)) {
2148 *p = cast_to(expr, integer_promotion(type));
2149 } else if (class & TYPE_FLOAT) {
2150 unsigned long mod = type->ctype.modifiers;
2151 if (!(mod & (MOD_LONG_ALL)))
2152 *p = cast_to(expr, &double_ctype);
2153 } else if (class & TYPE_PTR) {
2154 if (expr->ctype == &null_ctype)
2155 *p = cast_to(expr, &ptr_ctype);
2156 else
2157 degenerate(expr);
2159 } else if (!target->forced_arg){
2160 static char where[30];
2161 examine_symbol_type(target);
2162 sprintf(where, "argument %d", i);
2163 compatible_assignment_types(expr, target, p, where);
2166 i++;
2167 NEXT_PTR_LIST(argtype);
2168 } END_FOR_EACH_PTR(expr);
2169 FINISH_PTR_LIST(argtype);
2170 return 1;
2173 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
2175 struct symbol *sym;
2177 FOR_EACH_PTR(ctype->symbol_list, sym) {
2178 if (sym->ident == ident)
2179 return sym;
2180 } END_FOR_EACH_PTR(sym);
2181 return NULL;
2184 static void convert_index(struct expression *e)
2186 struct expression *child = e->idx_expression;
2187 unsigned from = e->idx_from;
2188 unsigned to = e->idx_to + 1;
2189 e->type = EXPR_POS;
2190 e->init_offset = from * bits_to_bytes(e->ctype->bit_size);
2191 e->init_nr = to - from;
2192 e->init_expr = child;
2195 static void convert_ident(struct expression *e)
2197 struct expression *child = e->ident_expression;
2198 struct symbol *sym = e->field;
2199 e->type = EXPR_POS;
2200 e->init_offset = sym->offset;
2201 e->init_nr = 1;
2202 e->init_expr = child;
2205 static void convert_designators(struct expression *e)
2207 while (e) {
2208 if (e->type == EXPR_INDEX)
2209 convert_index(e);
2210 else if (e->type == EXPR_IDENTIFIER)
2211 convert_ident(e);
2212 else
2213 break;
2214 e = e->init_expr;
2218 static void excess(struct expression *e, const char *s)
2220 warning(e->pos, "excessive elements in %s initializer", s);
2224 * implicit designator for the first element
2226 static struct expression *first_subobject(struct symbol *ctype, int class,
2227 struct expression **v)
2229 struct expression *e = *v, *new;
2231 if (ctype->type == SYM_NODE)
2232 ctype = ctype->ctype.base_type;
2234 if (class & TYPE_PTR) { /* array */
2235 if (!ctype->bit_size)
2236 return NULL;
2237 new = alloc_expression(e->pos, EXPR_INDEX);
2238 new->idx_expression = e;
2239 new->ctype = ctype->ctype.base_type;
2240 } else {
2241 struct symbol *field, *p;
2242 PREPARE_PTR_LIST(ctype->symbol_list, p);
2243 while (p && !p->ident && is_bitfield_type(p))
2244 NEXT_PTR_LIST(p);
2245 field = p;
2246 FINISH_PTR_LIST(p);
2247 if (!field)
2248 return NULL;
2249 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2250 new->ident_expression = e;
2251 new->field = new->ctype = field;
2253 *v = new;
2254 return new;
2258 * sanity-check explicit designators; return the innermost one or NULL
2259 * in case of error. Assign types.
2261 static struct expression *check_designators(struct expression *e,
2262 struct symbol *ctype)
2264 struct expression *last = NULL;
2265 const char *err;
2266 while (1) {
2267 if (ctype->type == SYM_NODE)
2268 ctype = ctype->ctype.base_type;
2269 if (e->type == EXPR_INDEX) {
2270 struct symbol *type;
2271 if (ctype->type != SYM_ARRAY) {
2272 err = "array index in non-array";
2273 break;
2275 type = ctype->ctype.base_type;
2276 if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2277 unsigned offset = e->idx_to * type->bit_size;
2278 if (offset >= ctype->bit_size) {
2279 err = "index out of bounds in";
2280 break;
2283 e->ctype = ctype = type;
2284 ctype = type;
2285 last = e;
2286 if (!e->idx_expression) {
2287 err = "invalid";
2288 break;
2290 e = e->idx_expression;
2291 } else if (e->type == EXPR_IDENTIFIER) {
2292 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2293 err = "field name not in struct or union";
2294 break;
2296 ctype = find_struct_ident(ctype, e->expr_ident);
2297 if (!ctype) {
2298 err = "unknown field name in";
2299 break;
2301 e->field = e->ctype = ctype;
2302 last = e;
2303 if (!e->ident_expression) {
2304 err = "invalid";
2305 break;
2307 e = e->ident_expression;
2308 } else if (e->type == EXPR_POS) {
2309 err = "internal front-end error: EXPR_POS in";
2310 break;
2311 } else
2312 return last;
2314 expression_error(e, "%s initializer", err);
2315 return NULL;
2319 * choose the next subobject to initialize.
2321 * Get designators for next element, switch old ones to EXPR_POS.
2322 * Return the resulting expression or NULL if we'd run out of subobjects.
2323 * The innermost designator is returned in *v. Designators in old
2324 * are assumed to be already sanity-checked.
2326 static struct expression *next_designators(struct expression *old,
2327 struct symbol *ctype,
2328 struct expression *e, struct expression **v)
2330 struct expression *new = NULL;
2332 if (!old)
2333 return NULL;
2334 if (old->type == EXPR_INDEX) {
2335 struct expression *copy;
2336 unsigned n;
2338 copy = next_designators(old->idx_expression,
2339 old->ctype, e, v);
2340 if (!copy) {
2341 n = old->idx_to + 1;
2342 if (n * old->ctype->bit_size == ctype->bit_size) {
2343 convert_index(old);
2344 return NULL;
2346 copy = e;
2347 *v = new = alloc_expression(e->pos, EXPR_INDEX);
2348 } else {
2349 n = old->idx_to;
2350 new = alloc_expression(e->pos, EXPR_INDEX);
2353 new->idx_from = new->idx_to = n;
2354 new->idx_expression = copy;
2355 new->ctype = old->ctype;
2356 convert_index(old);
2357 } else if (old->type == EXPR_IDENTIFIER) {
2358 struct expression *copy;
2359 struct symbol *field;
2361 copy = next_designators(old->ident_expression,
2362 old->ctype, e, v);
2363 if (!copy) {
2364 field = old->field->next_subobject;
2365 if (!field) {
2366 convert_ident(old);
2367 return NULL;
2369 copy = e;
2370 *v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2371 } else {
2372 field = old->field;
2373 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2376 new->field = field;
2377 new->expr_ident = field->ident;
2378 new->ident_expression = copy;
2379 new->ctype = field;
2380 convert_ident(old);
2382 return new;
2385 static int handle_simple_initializer(struct expression **ep, int nested,
2386 int class, struct symbol *ctype);
2389 * deal with traversing subobjects [6.7.8(17,18,20)]
2391 static void handle_list_initializer(struct expression *expr,
2392 int class, struct symbol *ctype)
2394 struct expression *e, *last = NULL, *top = NULL, *next;
2395 int jumped = 0;
2397 FOR_EACH_PTR(expr->expr_list, e) {
2398 struct expression **v;
2399 struct symbol *type;
2400 int lclass;
2402 if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2403 struct symbol *struct_sym;
2404 if (!top) {
2405 top = e;
2406 last = first_subobject(ctype, class, &top);
2407 } else {
2408 last = next_designators(last, ctype, e, &top);
2410 if (!last) {
2411 excess(e, class & TYPE_PTR ? "array" :
2412 "struct or union");
2413 DELETE_CURRENT_PTR(e);
2414 continue;
2416 struct_sym = ctype->type == SYM_NODE ? ctype->ctype.base_type : ctype;
2417 if (Wdesignated_init && struct_sym->designated_init)
2418 warning(e->pos, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2419 ctype->ident ? "in initializer for " : "",
2420 ctype->ident ? ctype->ident->len : 0,
2421 ctype->ident ? ctype->ident->name : "",
2422 ctype->ident ? ": " : "",
2423 get_type_name(struct_sym->type),
2424 show_ident(struct_sym->ident));
2425 if (jumped) {
2426 warning(e->pos, "advancing past deep designator");
2427 jumped = 0;
2429 REPLACE_CURRENT_PTR(e, last);
2430 } else {
2431 next = check_designators(e, ctype);
2432 if (!next) {
2433 DELETE_CURRENT_PTR(e);
2434 continue;
2436 top = next;
2437 /* deeper than one designator? */
2438 jumped = top != e;
2439 convert_designators(last);
2440 last = e;
2443 found:
2444 lclass = classify_type(top->ctype, &type);
2445 if (top->type == EXPR_INDEX)
2446 v = &top->idx_expression;
2447 else
2448 v = &top->ident_expression;
2450 if (handle_simple_initializer(v, 1, lclass, top->ctype))
2451 continue;
2453 if (!(lclass & TYPE_COMPOUND)) {
2454 warning(e->pos, "bogus scalar initializer");
2455 DELETE_CURRENT_PTR(e);
2456 continue;
2459 next = first_subobject(type, lclass, v);
2460 if (next) {
2461 warning(e->pos, "missing braces around initializer");
2462 top = next;
2463 goto found;
2466 DELETE_CURRENT_PTR(e);
2467 excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2469 } END_FOR_EACH_PTR(e);
2471 convert_designators(last);
2472 expr->ctype = ctype;
2475 static int is_string_literal(struct expression **v)
2477 struct expression *e = *v;
2478 while (e && e->type == EXPR_PREOP && e->op == '(')
2479 e = e->unop;
2480 if (!e || e->type != EXPR_STRING)
2481 return 0;
2482 if (e != *v && Wparen_string)
2483 warning(e->pos,
2484 "array initialized from parenthesized string constant");
2485 *v = e;
2486 return 1;
2490 * We want a normal expression, possibly in one layer of braces. Warn
2491 * if the latter happens inside a list (it's legal, but likely to be
2492 * an effect of screwup). In case of anything not legal, we are definitely
2493 * having an effect of screwup, so just fail and let the caller warn.
2495 static struct expression *handle_scalar(struct expression *e, int nested)
2497 struct expression *v = NULL, *p;
2498 int count = 0;
2500 /* normal case */
2501 if (e->type != EXPR_INITIALIZER)
2502 return e;
2504 FOR_EACH_PTR(e->expr_list, p) {
2505 if (!v)
2506 v = p;
2507 count++;
2508 } END_FOR_EACH_PTR(p);
2509 if (count != 1)
2510 return NULL;
2511 switch(v->type) {
2512 case EXPR_INITIALIZER:
2513 case EXPR_INDEX:
2514 case EXPR_IDENTIFIER:
2515 return NULL;
2516 default:
2517 break;
2519 if (nested)
2520 warning(e->pos, "braces around scalar initializer");
2521 return v;
2525 * deal with the cases that don't care about subobjects:
2526 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2527 * character array <- string literal, possibly in braces [6.7.8(14)]
2528 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2529 * compound type <- initializer list in braces [6.7.8(16)]
2530 * The last one punts to handle_list_initializer() which, in turn will call
2531 * us for individual elements of the list.
2533 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2534 * the lack of support of wide char stuff in general.
2536 * One note: we need to take care not to evaluate a string literal until
2537 * we know that we *will* handle it right here. Otherwise we would screw
2538 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2539 * { "string", ...} - we need to preserve that string literal recognizable
2540 * until we dig into the inner struct.
2542 static int handle_simple_initializer(struct expression **ep, int nested,
2543 int class, struct symbol *ctype)
2545 int is_string = is_string_type(ctype);
2546 struct expression *e = *ep, *p;
2547 struct symbol *type;
2549 if (!e)
2550 return 0;
2552 /* scalar */
2553 if (!(class & TYPE_COMPOUND)) {
2554 e = handle_scalar(e, nested);
2555 if (!e)
2556 return 0;
2557 *ep = e;
2558 if (!evaluate_expression(e))
2559 return 1;
2560 compatible_assignment_types(e, ctype, ep, "initializer");
2561 return 1;
2565 * sublist; either a string, or we dig in; the latter will deal with
2566 * pathologies, so we don't need anything fancy here.
2568 if (e->type == EXPR_INITIALIZER) {
2569 if (is_string) {
2570 struct expression *v = NULL;
2571 int count = 0;
2573 FOR_EACH_PTR(e->expr_list, p) {
2574 if (!v)
2575 v = p;
2576 count++;
2577 } END_FOR_EACH_PTR(p);
2578 if (count == 1 && is_string_literal(&v)) {
2579 *ep = e = v;
2580 goto String;
2583 handle_list_initializer(e, class, ctype);
2584 return 1;
2587 /* string */
2588 if (is_string_literal(&e)) {
2589 /* either we are doing array of char, or we'll have to dig in */
2590 if (is_string) {
2591 *ep = e;
2592 goto String;
2594 return 0;
2596 /* struct or union can be initialized by compatible */
2597 if (class != TYPE_COMPOUND)
2598 return 0;
2599 type = evaluate_expression(e);
2600 if (!type)
2601 return 0;
2602 if (ctype->type == SYM_NODE)
2603 ctype = ctype->ctype.base_type;
2604 if (type->type == SYM_NODE)
2605 type = type->ctype.base_type;
2606 if (ctype == type)
2607 return 1;
2608 return 0;
2610 String:
2611 p = alloc_expression(e->pos, EXPR_STRING);
2612 *p = *e;
2613 type = evaluate_expression(p);
2614 if (ctype->bit_size != -1) {
2615 if (ctype->bit_size + bits_in_char < type->bit_size)
2616 warning(e->pos,
2617 "too long initializer-string for array of char");
2618 else if (Winit_cstring && ctype->bit_size + bits_in_char == type->bit_size) {
2619 warning(e->pos,
2620 "too long initializer-string for array of char(no space for nul char)");
2623 *ep = p;
2624 return 1;
2627 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2629 struct symbol *type;
2630 int class = classify_type(ctype, &type);
2631 if (!handle_simple_initializer(ep, 0, class, ctype))
2632 expression_error(*ep, "invalid initializer");
2635 static struct symbol *evaluate_cast(struct expression *expr)
2637 struct expression *target = expr->cast_expression;
2638 struct symbol *ctype;
2639 struct symbol *t1, *t2;
2640 int class1, class2;
2641 int as1 = 0, as2 = 0;
2643 if (!target)
2644 return NULL;
2647 * Special case: a cast can be followed by an
2648 * initializer, in which case we need to pass
2649 * the type value down to that initializer rather
2650 * than trying to evaluate it as an expression
2652 * A more complex case is when the initializer is
2653 * dereferenced as part of a post-fix expression.
2654 * We need to produce an expression that can be dereferenced.
2656 if (target->type == EXPR_INITIALIZER) {
2657 struct symbol *sym = expr->cast_type;
2658 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2660 sym->initializer = target;
2661 evaluate_symbol(sym);
2663 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2664 addr->symbol = sym;
2666 expr->type = EXPR_PREOP;
2667 expr->op = '*';
2668 expr->unop = addr;
2669 expr->ctype = sym;
2671 return sym;
2674 ctype = examine_symbol_type(expr->cast_type);
2675 expr->ctype = ctype;
2676 expr->cast_type = ctype;
2678 evaluate_expression(target);
2679 degenerate(target);
2681 class1 = classify_type(ctype, &t1);
2683 /* cast to non-integer type -> not an integer constant expression */
2684 if (!is_int(class1))
2685 expr->flags = 0;
2686 /* if argument turns out to be not an integer constant expression *and*
2687 it was not a floating literal to start with -> too bad */
2688 else if (expr->flags == Int_const_expr &&
2689 !(target->flags & Int_const_expr))
2690 expr->flags = 0;
2692 * You can always throw a value away by casting to
2693 * "void" - that's an implicit "force". Note that
2694 * the same is _not_ true of "void *".
2696 if (t1 == &void_ctype)
2697 goto out;
2699 if (class1 & (TYPE_COMPOUND | TYPE_FN))
2700 warning(expr->pos, "cast to non-scalar");
2702 t2 = target->ctype;
2703 if (!t2) {
2704 expression_error(expr, "cast from unknown type");
2705 goto out;
2707 class2 = classify_type(t2, &t2);
2709 if (class2 & TYPE_COMPOUND)
2710 warning(expr->pos, "cast from non-scalar");
2712 if (expr->type == EXPR_FORCE_CAST)
2713 goto out;
2715 /* allowed cast unfouls */
2716 if (class2 & TYPE_FOULED)
2717 t2 = unfoul(t2);
2719 if (t1 != t2) {
2720 if (class1 & TYPE_RESTRICT)
2721 warning(expr->pos, "cast to %s",
2722 show_typename(t1));
2723 if (class2 & TYPE_RESTRICT)
2724 warning(expr->pos, "cast from %s",
2725 show_typename(t2));
2728 if (t1 == &ulong_ctype)
2729 as1 = -1;
2730 else if (class1 == TYPE_PTR) {
2731 examine_pointer_target(t1);
2732 as1 = t1->ctype.as;
2735 if (t2 == &ulong_ctype)
2736 as2 = -1;
2737 else if (class2 == TYPE_PTR) {
2738 examine_pointer_target(t2);
2739 as2 = t2->ctype.as;
2742 if (!as1 && as2 > 0)
2743 warning(expr->pos, "cast removes address space of expression");
2744 if (as1 > 0 && as2 > 0 && as1 != as2)
2745 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2746 if (as1 > 0 && !as2 &&
2747 !is_null_pointer_constant(target) && Wcast_to_as)
2748 warning(expr->pos,
2749 "cast adds address space to expression (<asn:%d>)", as1);
2751 if (!(t1->ctype.modifiers & MOD_PTRINHERIT) && class1 == TYPE_PTR &&
2752 !as1 && (target->flags & Int_const_expr)) {
2753 if (t1->ctype.base_type == &void_ctype) {
2754 if (is_zero_constant(target)) {
2755 /* NULL */
2756 expr->type = EXPR_VALUE;
2757 expr->ctype = &null_ctype;
2758 expr->value = 0;
2759 return ctype;
2763 out:
2764 return ctype;
2768 * Evaluate a call expression with a symbol. This
2769 * should expand inline functions, and evaluate
2770 * builtins.
2772 static int evaluate_symbol_call(struct expression *expr)
2774 struct expression *fn = expr->fn;
2775 struct symbol *ctype = fn->ctype;
2777 if (fn->type != EXPR_PREOP)
2778 return 0;
2780 if (ctype->op && ctype->op->evaluate)
2781 return ctype->op->evaluate(expr);
2783 if (ctype->ctype.modifiers & MOD_INLINE) {
2784 int ret;
2785 struct symbol *curr = current_fn;
2787 if (ctype->definition)
2788 ctype = ctype->definition;
2790 current_fn = ctype->ctype.base_type;
2792 ret = inline_function(expr, ctype);
2794 /* restore the old function */
2795 current_fn = curr;
2796 return ret;
2799 return 0;
2802 static struct symbol *evaluate_call(struct expression *expr)
2804 int args, fnargs;
2805 struct symbol *ctype, *sym;
2806 struct expression *fn = expr->fn;
2807 struct expression_list *arglist = expr->args;
2809 if (!evaluate_expression(fn))
2810 return NULL;
2811 sym = ctype = fn->ctype;
2812 if (ctype->type == SYM_NODE)
2813 ctype = ctype->ctype.base_type;
2814 if (ctype->type == SYM_PTR)
2815 ctype = get_base_type(ctype);
2817 if (ctype->type != SYM_FN) {
2818 struct expression *arg;
2819 expression_error(expr, "not a function %s",
2820 show_ident(sym->ident));
2821 /* do typechecking in arguments */
2822 FOR_EACH_PTR (arglist, arg) {
2823 evaluate_expression(arg);
2824 } END_FOR_EACH_PTR(arg);
2825 return NULL;
2828 examine_fn_arguments(ctype);
2829 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
2830 sym->op && sym->op->args) {
2831 if (!sym->op->args(expr))
2832 return NULL;
2833 } else {
2834 if (!evaluate_arguments(sym, ctype, arglist))
2835 return NULL;
2836 args = expression_list_size(expr->args);
2837 fnargs = symbol_list_size(ctype->arguments);
2838 if (args < fnargs)
2839 expression_error(expr,
2840 "not enough arguments for function %s",
2841 show_ident(sym->ident));
2842 if (args > fnargs && !ctype->variadic)
2843 expression_error(expr,
2844 "too many arguments for function %s",
2845 show_ident(sym->ident));
2847 if (sym->type == SYM_NODE) {
2848 if (evaluate_symbol_call(expr))
2849 return expr->ctype;
2851 expr->ctype = ctype->ctype.base_type;
2852 return expr->ctype;
2855 static struct symbol *evaluate_offsetof(struct expression *expr)
2857 struct expression *e = expr->down;
2858 struct symbol *ctype = expr->in;
2859 int class;
2861 if (expr->op == '.') {
2862 struct symbol *field;
2863 int offset = 0;
2864 if (!ctype) {
2865 expression_error(expr, "expected structure or union");
2866 return NULL;
2868 examine_symbol_type(ctype);
2869 class = classify_type(ctype, &ctype);
2870 if (class != TYPE_COMPOUND) {
2871 expression_error(expr, "expected structure or union");
2872 return NULL;
2875 field = find_identifier(expr->ident, ctype->symbol_list, &offset);
2876 if (!field) {
2877 expression_error(expr, "unknown member");
2878 return NULL;
2880 ctype = field;
2881 expr->type = EXPR_VALUE;
2882 expr->flags = Int_const_expr;
2883 expr->value = offset;
2884 expr->taint = 0;
2885 expr->ctype = size_t_ctype;
2886 } else {
2887 if (!ctype) {
2888 expression_error(expr, "expected structure or union");
2889 return NULL;
2891 examine_symbol_type(ctype);
2892 class = classify_type(ctype, &ctype);
2893 if (class != (TYPE_COMPOUND | TYPE_PTR)) {
2894 expression_error(expr, "expected array");
2895 return NULL;
2897 ctype = ctype->ctype.base_type;
2898 if (!expr->index) {
2899 expr->type = EXPR_VALUE;
2900 expr->flags = Int_const_expr;
2901 expr->value = 0;
2902 expr->taint = 0;
2903 expr->ctype = size_t_ctype;
2904 } else {
2905 struct expression *idx = expr->index, *m;
2906 struct symbol *i_type = evaluate_expression(idx);
2907 int i_class = classify_type(i_type, &i_type);
2908 if (!is_int(i_class)) {
2909 expression_error(expr, "non-integer index");
2910 return NULL;
2912 unrestrict(idx, i_class, &i_type);
2913 idx = cast_to(idx, size_t_ctype);
2914 m = alloc_const_expression(expr->pos,
2915 bits_to_bytes(ctype->bit_size));
2916 m->ctype = size_t_ctype;
2917 m->flags = Int_const_expr;
2918 expr->type = EXPR_BINOP;
2919 expr->left = idx;
2920 expr->right = m;
2921 expr->op = '*';
2922 expr->ctype = size_t_ctype;
2923 expr->flags = m->flags & idx->flags & Int_const_expr;
2926 if (e) {
2927 struct expression *copy = __alloc_expression(0);
2928 *copy = *expr;
2929 if (e->type == EXPR_OFFSETOF)
2930 e->in = ctype;
2931 if (!evaluate_expression(e))
2932 return NULL;
2933 expr->type = EXPR_BINOP;
2934 expr->flags = e->flags & copy->flags & Int_const_expr;
2935 expr->op = '+';
2936 expr->ctype = size_t_ctype;
2937 expr->left = copy;
2938 expr->right = e;
2940 return size_t_ctype;
2943 struct symbol *evaluate_expression(struct expression *expr)
2945 if (!expr)
2946 return NULL;
2947 if (expr->ctype)
2948 return expr->ctype;
2950 switch (expr->type) {
2951 case EXPR_VALUE:
2952 case EXPR_FVALUE:
2953 expression_error(expr, "value expression without a type");
2954 return NULL;
2955 case EXPR_STRING:
2956 return evaluate_string(expr);
2957 case EXPR_SYMBOL:
2958 return evaluate_symbol_expression(expr);
2959 case EXPR_BINOP:
2960 if (!evaluate_expression(expr->left))
2961 return NULL;
2962 if (!evaluate_expression(expr->right))
2963 return NULL;
2964 return evaluate_binop(expr);
2965 case EXPR_LOGICAL:
2966 return evaluate_logical(expr);
2967 case EXPR_COMMA:
2968 evaluate_expression(expr->left);
2969 if (!evaluate_expression(expr->right))
2970 return NULL;
2971 return evaluate_comma(expr);
2972 case EXPR_COMPARE:
2973 if (!evaluate_expression(expr->left))
2974 return NULL;
2975 if (!evaluate_expression(expr->right))
2976 return NULL;
2977 return evaluate_compare(expr);
2978 case EXPR_ASSIGNMENT:
2979 if (!evaluate_expression(expr->left))
2980 return NULL;
2981 if (!evaluate_expression(expr->right))
2982 return NULL;
2983 return evaluate_assignment(expr);
2984 case EXPR_PREOP:
2985 if (!evaluate_expression(expr->unop))
2986 return NULL;
2987 return evaluate_preop(expr);
2988 case EXPR_POSTOP:
2989 if (!evaluate_expression(expr->unop))
2990 return NULL;
2991 return evaluate_postop(expr);
2992 case EXPR_CAST:
2993 case EXPR_FORCE_CAST:
2994 case EXPR_IMPLIED_CAST:
2995 return evaluate_cast(expr);
2996 case EXPR_SIZEOF:
2997 return evaluate_sizeof(expr);
2998 case EXPR_PTRSIZEOF:
2999 return evaluate_ptrsizeof(expr);
3000 case EXPR_ALIGNOF:
3001 return evaluate_alignof(expr);
3002 case EXPR_DEREF:
3003 return evaluate_member_dereference(expr);
3004 case EXPR_CALL:
3005 return evaluate_call(expr);
3006 case EXPR_SELECT:
3007 case EXPR_CONDITIONAL:
3008 return evaluate_conditional_expression(expr);
3009 case EXPR_STATEMENT:
3010 expr->ctype = evaluate_statement(expr->statement);
3011 return expr->ctype;
3013 case EXPR_LABEL:
3014 expr->ctype = &ptr_ctype;
3015 return &ptr_ctype;
3017 case EXPR_TYPE:
3018 /* Evaluate the type of the symbol .. */
3019 evaluate_symbol(expr->symbol);
3020 /* .. but the type of the _expression_ is a "type" */
3021 expr->ctype = &type_ctype;
3022 return &type_ctype;
3024 case EXPR_OFFSETOF:
3025 return evaluate_offsetof(expr);
3027 /* These can not exist as stand-alone expressions */
3028 case EXPR_INITIALIZER:
3029 case EXPR_IDENTIFIER:
3030 case EXPR_INDEX:
3031 case EXPR_POS:
3032 expression_error(expr, "internal front-end error: initializer in expression");
3033 return NULL;
3034 case EXPR_SLICE:
3035 expression_error(expr, "internal front-end error: SLICE re-evaluated");
3036 return NULL;
3038 return NULL;
3041 static void check_duplicates(struct symbol *sym)
3043 int declared = 0;
3044 struct symbol *next = sym;
3046 while ((next = next->same_symbol) != NULL) {
3047 const char *typediff;
3048 evaluate_symbol(next);
3049 declared++;
3050 typediff = type_difference(&sym->ctype, &next->ctype, 0, 0);
3051 if (typediff) {
3052 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
3053 show_ident(sym->ident),
3054 stream_name(next->pos.stream), next->pos.line, typediff);
3055 return;
3058 if (!declared) {
3059 unsigned long mod = sym->ctype.modifiers;
3060 if (mod & (MOD_STATIC | MOD_REGISTER))
3061 return;
3062 if (!(mod & MOD_TOPLEVEL))
3063 return;
3064 if (!Wdecl)
3065 return;
3066 if (sym->ident == &main_ident)
3067 return;
3068 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
3072 static struct symbol *evaluate_symbol(struct symbol *sym)
3074 struct symbol *base_type;
3076 if (!sym)
3077 return sym;
3078 if (sym->evaluated)
3079 return sym;
3080 sym->evaluated = 1;
3082 sym = examine_symbol_type(sym);
3083 base_type = get_base_type(sym);
3084 if (!base_type)
3085 return NULL;
3087 /* Evaluate the initializers */
3088 if (sym->initializer)
3089 evaluate_initializer(sym, &sym->initializer);
3091 /* And finally, evaluate the body of the symbol too */
3092 if (base_type->type == SYM_FN) {
3093 struct symbol *curr = current_fn;
3095 if (sym->definition && sym->definition != sym)
3096 return evaluate_symbol(sym->definition);
3098 current_fn = base_type;
3100 examine_fn_arguments(base_type);
3101 if (!base_type->stmt && base_type->inline_stmt)
3102 uninline(sym);
3103 if (base_type->stmt)
3104 evaluate_statement(base_type->stmt);
3106 current_fn = curr;
3109 return base_type;
3112 void evaluate_symbol_list(struct symbol_list *list)
3114 struct symbol *sym;
3116 FOR_EACH_PTR(list, sym) {
3117 evaluate_symbol(sym);
3118 check_duplicates(sym);
3119 } END_FOR_EACH_PTR(sym);
3122 static struct symbol *evaluate_return_expression(struct statement *stmt)
3124 struct expression *expr = stmt->expression;
3125 struct symbol *fntype;
3127 evaluate_expression(expr);
3128 fntype = current_fn->ctype.base_type;
3129 if (!fntype || fntype == &void_ctype) {
3130 if (expr && expr->ctype != &void_ctype)
3131 expression_error(expr, "return expression in %s function", fntype?"void":"typeless");
3132 if (expr && Wreturn_void)
3133 warning(stmt->pos, "returning void-valued expression");
3134 return NULL;
3137 if (!expr) {
3138 sparse_error(stmt->pos, "return with no return value");
3139 return NULL;
3141 if (!expr->ctype)
3142 return NULL;
3143 compatible_assignment_types(expr, fntype, &stmt->expression, "return expression");
3144 return NULL;
3147 static void evaluate_if_statement(struct statement *stmt)
3149 if (!stmt->if_conditional)
3150 return;
3152 evaluate_conditional(stmt->if_conditional, 0);
3153 evaluate_statement(stmt->if_true);
3154 evaluate_statement(stmt->if_false);
3157 static void evaluate_iterator(struct statement *stmt)
3159 evaluate_symbol_list(stmt->iterator_syms);
3160 evaluate_conditional(stmt->iterator_pre_condition, 1);
3161 evaluate_conditional(stmt->iterator_post_condition,1);
3162 evaluate_statement(stmt->iterator_pre_statement);
3163 evaluate_statement(stmt->iterator_statement);
3164 evaluate_statement(stmt->iterator_post_statement);
3167 static void verify_output_constraint(struct expression *expr, const char *constraint)
3169 switch (*constraint) {
3170 case '=': /* Assignment */
3171 case '+': /* Update */
3172 break;
3173 default:
3174 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
3178 static void verify_input_constraint(struct expression *expr, const char *constraint)
3180 switch (*constraint) {
3181 case '=': /* Assignment */
3182 case '+': /* Update */
3183 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
3187 static void evaluate_asm_statement(struct statement *stmt)
3189 struct expression *expr;
3190 struct symbol *sym;
3191 int state;
3193 expr = stmt->asm_string;
3194 if (!expr || expr->type != EXPR_STRING) {
3195 sparse_error(stmt->pos, "need constant string for inline asm");
3196 return;
3199 state = 0;
3200 FOR_EACH_PTR(stmt->asm_outputs, expr) {
3201 switch (state) {
3202 case 0: /* Identifier */
3203 state = 1;
3204 continue;
3206 case 1: /* Constraint */
3207 state = 2;
3208 if (!expr || expr->type != EXPR_STRING) {
3209 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
3210 *THIS_ADDRESS(expr) = NULL;
3211 continue;
3213 verify_output_constraint(expr, expr->string->data);
3214 continue;
3216 case 2: /* Expression */
3217 state = 0;
3218 if (!evaluate_expression(expr))
3219 return;
3220 if (!lvalue_expression(expr))
3221 warning(expr->pos, "asm output is not an lvalue");
3222 evaluate_assign_to(expr, expr->ctype);
3223 continue;
3225 } END_FOR_EACH_PTR(expr);
3227 state = 0;
3228 FOR_EACH_PTR(stmt->asm_inputs, expr) {
3229 switch (state) {
3230 case 0: /* Identifier */
3231 state = 1;
3232 continue;
3234 case 1: /* Constraint */
3235 state = 2;
3236 if (!expr || expr->type != EXPR_STRING) {
3237 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
3238 *THIS_ADDRESS(expr) = NULL;
3239 continue;
3241 verify_input_constraint(expr, expr->string->data);
3242 continue;
3244 case 2: /* Expression */
3245 state = 0;
3246 if (!evaluate_expression(expr))
3247 return;
3248 continue;
3250 } END_FOR_EACH_PTR(expr);
3252 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
3253 if (!expr) {
3254 sparse_error(stmt->pos, "bad asm clobbers");
3255 return;
3257 if (expr->type == EXPR_STRING)
3258 continue;
3259 expression_error(expr, "asm clobber is not a string");
3260 } END_FOR_EACH_PTR(expr);
3262 FOR_EACH_PTR(stmt->asm_labels, sym) {
3263 if (!sym || sym->type != SYM_LABEL) {
3264 sparse_error(stmt->pos, "bad asm label");
3265 return;
3267 } END_FOR_EACH_PTR(sym);
3270 static void evaluate_case_statement(struct statement *stmt)
3272 evaluate_expression(stmt->case_expression);
3273 evaluate_expression(stmt->case_to);
3274 evaluate_statement(stmt->case_statement);
3277 static void check_case_type(struct expression *switch_expr,
3278 struct expression *case_expr,
3279 struct expression **enumcase)
3281 struct symbol *switch_type, *case_type;
3282 int sclass, cclass;
3284 if (!case_expr)
3285 return;
3287 switch_type = switch_expr->ctype;
3288 case_type = evaluate_expression(case_expr);
3290 if (!switch_type || !case_type)
3291 goto Bad;
3292 if (enumcase) {
3293 if (*enumcase)
3294 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
3295 else if (is_enum_type(case_type))
3296 *enumcase = case_expr;
3299 sclass = classify_type(switch_type, &switch_type);
3300 cclass = classify_type(case_type, &case_type);
3302 /* both should be arithmetic */
3303 if (!(sclass & cclass & TYPE_NUM))
3304 goto Bad;
3306 /* neither should be floating */
3307 if ((sclass | cclass) & TYPE_FLOAT)
3308 goto Bad;
3310 /* if neither is restricted, we are OK */
3311 if (!((sclass | cclass) & TYPE_RESTRICT))
3312 return;
3314 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
3315 cclass, sclass, case_type, switch_type)) {
3316 unrestrict(case_expr, cclass, &case_type);
3317 unrestrict(switch_expr, sclass, &switch_type);
3319 return;
3321 Bad:
3322 expression_error(case_expr, "incompatible types for 'case' statement");
3325 static void evaluate_switch_statement(struct statement *stmt)
3327 struct symbol *sym;
3328 struct expression *enumcase = NULL;
3329 struct expression **enumcase_holder = &enumcase;
3330 struct expression *sel = stmt->switch_expression;
3332 evaluate_expression(sel);
3333 evaluate_statement(stmt->switch_statement);
3334 if (!sel)
3335 return;
3336 if (sel->ctype && is_enum_type(sel->ctype))
3337 enumcase_holder = NULL; /* Only check cases against switch */
3339 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
3340 struct statement *case_stmt = sym->stmt;
3341 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
3342 check_case_type(sel, case_stmt->case_to, enumcase_holder);
3343 } END_FOR_EACH_PTR(sym);
3346 static void evaluate_goto_statement(struct statement *stmt)
3348 struct symbol *label = stmt->goto_label;
3350 if (label && !label->stmt && !lookup_keyword(label->ident, NS_KEYWORD))
3351 sparse_error(stmt->pos, "label '%s' was not declared", show_ident(label->ident));
3353 evaluate_expression(stmt->goto_expression);
3356 struct symbol *evaluate_statement(struct statement *stmt)
3358 if (!stmt)
3359 return NULL;
3361 switch (stmt->type) {
3362 case STMT_DECLARATION: {
3363 struct symbol *s;
3364 FOR_EACH_PTR(stmt->declaration, s) {
3365 evaluate_symbol(s);
3366 } END_FOR_EACH_PTR(s);
3367 return NULL;
3370 case STMT_RETURN:
3371 return evaluate_return_expression(stmt);
3373 case STMT_EXPRESSION:
3374 if (!evaluate_expression(stmt->expression))
3375 return NULL;
3376 if (stmt->expression->ctype == &null_ctype)
3377 stmt->expression = cast_to(stmt->expression, &ptr_ctype);
3378 return degenerate(stmt->expression);
3380 case STMT_COMPOUND: {
3381 struct statement *s;
3382 struct symbol *type = NULL;
3384 /* Evaluate the return symbol in the compound statement */
3385 evaluate_symbol(stmt->ret);
3388 * Then, evaluate each statement, making the type of the
3389 * compound statement be the type of the last statement
3391 type = evaluate_statement(stmt->args);
3392 FOR_EACH_PTR(stmt->stmts, s) {
3393 type = evaluate_statement(s);
3394 } END_FOR_EACH_PTR(s);
3395 if (!type)
3396 type = &void_ctype;
3397 return type;
3399 case STMT_IF:
3400 evaluate_if_statement(stmt);
3401 return NULL;
3402 case STMT_ITERATOR:
3403 evaluate_iterator(stmt);
3404 return NULL;
3405 case STMT_SWITCH:
3406 evaluate_switch_statement(stmt);
3407 return NULL;
3408 case STMT_CASE:
3409 evaluate_case_statement(stmt);
3410 return NULL;
3411 case STMT_LABEL:
3412 return evaluate_statement(stmt->label_statement);
3413 case STMT_GOTO:
3414 evaluate_goto_statement(stmt);
3415 return NULL;
3416 case STMT_NONE:
3417 break;
3418 case STMT_ASM:
3419 evaluate_asm_statement(stmt);
3420 return NULL;
3421 case STMT_CONTEXT:
3422 evaluate_expression(stmt->expression);
3423 return NULL;
3424 case STMT_RANGE:
3425 evaluate_expression(stmt->range_expression);
3426 evaluate_expression(stmt->range_low);
3427 evaluate_expression(stmt->range_high);
3428 return NULL;
3430 return NULL;