make ptrlist walking against robust against empty blocks
[smatch.git] / evaluate.c
blob6d44e0d9c365223f170cb7e86daa16f495240cdb
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 unsigned long mod = type->ctype.modifiers;
121 int width = type->bit_size;
124 * Bitfields always promote to the base type,
125 * even if the bitfield might be bigger than
126 * an "int".
128 if (type->type == SYM_BITFIELD) {
129 type = type->ctype.base_type;
131 mod = type->ctype.modifiers;
132 if (width < bits_in_int)
133 return &int_ctype;
135 /* If char/short has as many bits as int, it still gets "promoted" */
136 if (mod & (MOD_CHAR | MOD_SHORT)) {
137 if (mod & MOD_UNSIGNED)
138 return &uint_ctype;
139 return &int_ctype;
141 return type;
145 * integer part of usual arithmetic conversions:
146 * integer promotions are applied
147 * if left and right are identical, we are done
148 * if signedness is the same, convert one with lower rank
149 * unless unsigned argument has rank lower than signed one, convert the
150 * signed one.
151 * if signed argument is bigger than unsigned one, convert the unsigned.
152 * otherwise, convert signed.
154 * Leaving aside the integer promotions, that is equivalent to
155 * if identical, don't convert
156 * if left is bigger than right, convert right
157 * if right is bigger than left, convert right
158 * otherwise, if signedness is the same, convert one with lower rank
159 * otherwise convert the signed one.
161 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
163 unsigned long lmod, rmod;
165 left = integer_promotion(left);
166 right = integer_promotion(right);
168 if (left == right)
169 goto left;
171 if (left->bit_size > right->bit_size)
172 goto left;
174 if (right->bit_size > left->bit_size)
175 goto right;
177 lmod = left->ctype.modifiers;
178 rmod = right->ctype.modifiers;
179 if ((lmod ^ rmod) & MOD_UNSIGNED) {
180 if (lmod & MOD_UNSIGNED)
181 goto left;
182 } else if ((lmod & ~rmod) & (MOD_LONG_ALL))
183 goto left;
184 right:
185 left = right;
186 left:
187 return left;
190 static int same_cast_type(struct symbol *orig, struct symbol *new)
192 return orig->bit_size == new->bit_size &&
193 orig->bit_offset == new->bit_offset;
196 static struct symbol *base_type(struct symbol *node, unsigned long *modp, unsigned long *asp)
198 unsigned long mod, as;
200 mod = 0; as = 0;
201 while (node) {
202 mod |= node->ctype.modifiers;
203 as |= node->ctype.as;
204 if (node->type == SYM_NODE) {
205 node = node->ctype.base_type;
206 continue;
208 break;
210 *modp = mod & ~MOD_IGNORE;
211 *asp = as;
212 return node;
215 static int is_same_type(struct expression *expr, struct symbol *new)
217 struct symbol *old = expr->ctype;
218 unsigned long oldmod, newmod, oldas, newas;
220 old = base_type(old, &oldmod, &oldas);
221 new = base_type(new, &newmod, &newas);
223 /* Same base type, same address space? */
224 if (old == new && oldas == newas) {
225 unsigned long difmod;
227 /* Check the modifier bits. */
228 difmod = (oldmod ^ newmod) & ~MOD_NOCAST;
230 /* Exact same type? */
231 if (!difmod)
232 return 1;
235 * Not the same type, but differs only in "const".
236 * Don't warn about MOD_NOCAST.
238 if (difmod == MOD_CONST)
239 return 0;
241 if ((oldmod | newmod) & MOD_NOCAST) {
242 const char *tofrom = "to/from";
243 if (!(newmod & MOD_NOCAST))
244 tofrom = "from";
245 if (!(oldmod & MOD_NOCAST))
246 tofrom = "to";
247 warning(expr->pos, "implicit cast %s nocast type", tofrom);
249 return 0;
252 static void
253 warn_for_different_enum_types (struct position pos,
254 struct symbol *typea,
255 struct symbol *typeb)
257 if (!Wenum_mismatch)
258 return;
259 if (typea->type == SYM_NODE)
260 typea = typea->ctype.base_type;
261 if (typeb->type == SYM_NODE)
262 typeb = typeb->ctype.base_type;
264 if (typea == typeb)
265 return;
267 if (typea->type == SYM_ENUM && typeb->type == SYM_ENUM) {
268 warning(pos, "mixing different enum types");
269 info(pos, " %s versus", show_typename(typea));
270 info(pos, " %s", show_typename(typeb));
275 * This gets called for implicit casts in assignments and
276 * integer promotion. We often want to try to move the
277 * cast down, because the ops involved may have been
278 * implicitly cast up, and we can get rid of the casts
279 * early.
281 static struct expression * cast_to(struct expression *old, struct symbol *type)
283 struct expression *expr;
285 warn_for_different_enum_types (old->pos, old->ctype, type);
287 if (old->ctype != &null_ctype && is_same_type(old, type))
288 return old;
291 * See if we can simplify the op. Move the cast down.
293 switch (old->type) {
294 case EXPR_PREOP:
295 if (old->ctype->bit_size < type->bit_size)
296 break;
297 if (old->op == '~') {
298 old->ctype = type;
299 old->unop = cast_to(old->unop, type);
300 return old;
302 break;
304 case EXPR_IMPLIED_CAST:
305 warn_for_different_enum_types(old->pos, old->ctype, type);
307 if (old->ctype->bit_size >= type->bit_size) {
308 struct expression *orig = old->cast_expression;
309 if (same_cast_type(orig->ctype, type))
310 return orig;
311 if (old->ctype->bit_offset == type->bit_offset) {
312 old->ctype = type;
313 old->cast_type = type;
314 return old;
317 break;
319 default:
320 /* nothing */;
323 expr = alloc_expression(old->pos, EXPR_IMPLIED_CAST);
324 expr->flags = old->flags;
325 expr->ctype = type;
326 expr->cast_type = type;
327 expr->cast_expression = old;
328 return expr;
331 enum {
332 TYPE_NUM = 1,
333 TYPE_BITFIELD = 2,
334 TYPE_RESTRICT = 4,
335 TYPE_FLOAT = 8,
336 TYPE_PTR = 16,
337 TYPE_COMPOUND = 32,
338 TYPE_FOULED = 64,
339 TYPE_FN = 128,
342 static inline int classify_type(struct symbol *type, struct symbol **base)
344 static int type_class[SYM_BAD + 1] = {
345 [SYM_PTR] = TYPE_PTR,
346 [SYM_FN] = TYPE_PTR | TYPE_FN,
347 [SYM_ARRAY] = TYPE_PTR | TYPE_COMPOUND,
348 [SYM_STRUCT] = TYPE_COMPOUND,
349 [SYM_UNION] = TYPE_COMPOUND,
350 [SYM_BITFIELD] = TYPE_NUM | TYPE_BITFIELD,
351 [SYM_RESTRICT] = TYPE_NUM | TYPE_RESTRICT,
352 [SYM_FOULED] = TYPE_NUM | TYPE_RESTRICT | TYPE_FOULED,
354 if (type->type == SYM_NODE)
355 type = type->ctype.base_type;
356 if (type->type == SYM_TYPEOF) {
357 type = evaluate_expression(type->initializer);
358 if (!type)
359 type = &bad_ctype;
360 else if (type->type == SYM_NODE)
361 type = type->ctype.base_type;
363 if (type->type == SYM_ENUM)
364 type = type->ctype.base_type;
365 *base = type;
366 if (type->type == SYM_BASETYPE) {
367 if (type->ctype.base_type == &int_type)
368 return TYPE_NUM;
369 if (type->ctype.base_type == &fp_type)
370 return TYPE_NUM | TYPE_FLOAT;
372 return type_class[type->type];
375 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
377 static inline int is_string_type(struct symbol *type)
379 if (type->type == SYM_NODE)
380 type = type->ctype.base_type;
381 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
384 static struct symbol *bad_expr_type(struct expression *expr)
386 sparse_error(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
387 switch (expr->type) {
388 case EXPR_BINOP:
389 case EXPR_COMPARE:
390 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
391 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
392 break;
393 case EXPR_PREOP:
394 case EXPR_POSTOP:
395 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
396 break;
397 default:
398 break;
401 expr->flags = 0;
402 return expr->ctype = &bad_ctype;
405 static int restricted_value(struct expression *v, struct symbol *type)
407 if (v->type != EXPR_VALUE)
408 return 1;
409 if (v->value != 0)
410 return 1;
411 return 0;
414 static int restricted_binop(int op, struct symbol *type)
416 switch (op) {
417 case '&':
418 case '=':
419 case SPECIAL_AND_ASSIGN:
420 case SPECIAL_OR_ASSIGN:
421 case SPECIAL_XOR_ASSIGN:
422 return 1; /* unfoul */
423 case '|':
424 case '^':
425 case '?':
426 return 2; /* keep fouled */
427 case SPECIAL_EQUAL:
428 case SPECIAL_NOTEQUAL:
429 return 3; /* warn if fouled */
430 default:
431 return 0; /* warn */
435 static int restricted_unop(int op, struct symbol **type)
437 if (op == '~') {
438 if ((*type)->bit_size < bits_in_int)
439 *type = befoul(*type);
440 return 0;
441 } if (op == '+')
442 return 0;
443 return 1;
446 /* type should be SYM_FOULED */
447 static inline struct symbol *unfoul(struct symbol *type)
449 return type->ctype.base_type;
452 static struct symbol *restricted_binop_type(int op,
453 struct expression *left,
454 struct expression *right,
455 int lclass, int rclass,
456 struct symbol *ltype,
457 struct symbol *rtype)
459 struct symbol *ctype = NULL;
460 if (lclass & TYPE_RESTRICT) {
461 if (rclass & TYPE_RESTRICT) {
462 if (ltype == rtype) {
463 ctype = ltype;
464 } else if (lclass & TYPE_FOULED) {
465 if (unfoul(ltype) == rtype)
466 ctype = ltype;
467 } else if (rclass & TYPE_FOULED) {
468 if (unfoul(rtype) == ltype)
469 ctype = rtype;
471 } else {
472 if (!restricted_value(right, ltype))
473 ctype = ltype;
475 } else if (!restricted_value(left, rtype))
476 ctype = rtype;
478 if (ctype) {
479 switch (restricted_binop(op, ctype)) {
480 case 1:
481 if ((lclass ^ rclass) & TYPE_FOULED)
482 ctype = unfoul(ctype);
483 break;
484 case 3:
485 if (!(lclass & rclass & TYPE_FOULED))
486 break;
487 case 0:
488 ctype = NULL;
489 default:
490 break;
494 return ctype;
497 static inline void unrestrict(struct expression *expr,
498 int class, struct symbol **ctype)
500 if (class & TYPE_RESTRICT) {
501 if (class & TYPE_FOULED)
502 *ctype = unfoul(*ctype);
503 warning(expr->pos, "%s degrades to integer",
504 show_typename(*ctype));
505 *ctype = (*ctype)->ctype.base_type; /* get to arithmetic type */
509 static struct symbol *usual_conversions(int op,
510 struct expression *left,
511 struct expression *right,
512 int lclass, int rclass,
513 struct symbol *ltype,
514 struct symbol *rtype)
516 struct symbol *ctype;
518 warn_for_different_enum_types(right->pos, left->ctype, right->ctype);
520 if ((lclass | rclass) & TYPE_RESTRICT)
521 goto Restr;
523 Normal:
524 if (!(lclass & TYPE_FLOAT)) {
525 if (!(rclass & TYPE_FLOAT))
526 return bigger_int_type(ltype, rtype);
527 else
528 return rtype;
529 } else if (rclass & TYPE_FLOAT) {
530 unsigned long lmod = ltype->ctype.modifiers;
531 unsigned long rmod = rtype->ctype.modifiers;
532 if (rmod & ~lmod & (MOD_LONG_ALL))
533 return rtype;
534 else
535 return ltype;
536 } else
537 return ltype;
539 Restr:
540 ctype = restricted_binop_type(op, left, right,
541 lclass, rclass, ltype, rtype);
542 if (ctype)
543 return ctype;
545 unrestrict(left, lclass, &ltype);
546 unrestrict(right, rclass, &rtype);
548 goto Normal;
551 static inline int lvalue_expression(struct expression *expr)
553 return expr->type == EXPR_PREOP && expr->op == '*';
556 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *itype)
558 struct expression *index = expr->right;
559 struct symbol *ctype, *base;
560 int multiply;
562 classify_type(degenerate(expr->left), &ctype);
563 base = examine_pointer_target(ctype);
565 if (!base) {
566 expression_error(expr, "missing type information");
567 return NULL;
569 if (is_function(base)) {
570 expression_error(expr, "arithmetics on pointers to functions");
571 return NULL;
574 /* Get the size of whatever the pointer points to */
575 multiply = is_void_type(base) ? 1 : bits_to_bytes(base->bit_size);
577 if (ctype == &null_ctype)
578 ctype = &ptr_ctype;
579 expr->ctype = ctype;
581 if (multiply == 1 && itype->bit_size >= bits_in_pointer)
582 return ctype;
584 if (index->type == EXPR_VALUE) {
585 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
586 unsigned long long v = index->value, mask;
587 mask = 1ULL << (itype->bit_size - 1);
588 if (v & mask)
589 v |= -mask;
590 else
591 v &= mask - 1;
592 v *= multiply;
593 mask = 1ULL << (bits_in_pointer - 1);
594 v &= mask | (mask - 1);
595 val->value = v;
596 val->ctype = ssize_t_ctype;
597 expr->right = val;
598 return ctype;
601 if (itype->bit_size < bits_in_pointer)
602 index = cast_to(index, ssize_t_ctype);
604 if (multiply > 1) {
605 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
606 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
608 val->ctype = ssize_t_ctype;
609 val->value = multiply;
611 mul->op = '*';
612 mul->ctype = ssize_t_ctype;
613 mul->left = index;
614 mul->right = val;
615 index = mul;
618 expr->right = index;
619 return ctype;
622 static void examine_fn_arguments(struct symbol *fn);
624 #define MOD_IGN (MOD_VOLATILE | MOD_CONST | MOD_PURE)
626 const char *type_difference(struct ctype *c1, struct ctype *c2,
627 unsigned long mod1, unsigned long mod2)
629 unsigned long as1 = c1->as, as2 = c2->as;
630 struct symbol *t1 = c1->base_type;
631 struct symbol *t2 = c2->base_type;
632 int move1 = 1, move2 = 1;
633 mod1 |= c1->modifiers;
634 mod2 |= c2->modifiers;
635 for (;;) {
636 unsigned long diff;
637 int type;
638 struct symbol *base1 = t1->ctype.base_type;
639 struct symbol *base2 = t2->ctype.base_type;
642 * FIXME! Collect alignment and context too here!
644 if (move1) {
645 if (t1 && t1->type != SYM_PTR) {
646 mod1 |= t1->ctype.modifiers;
647 as1 |= t1->ctype.as;
649 move1 = 0;
652 if (move2) {
653 if (t2 && t2->type != SYM_PTR) {
654 mod2 |= t2->ctype.modifiers;
655 as2 |= t2->ctype.as;
657 move2 = 0;
660 if (t1 == t2)
661 break;
662 if (!t1 || !t2)
663 return "different types";
665 if (t1->type == SYM_NODE || t1->type == SYM_ENUM) {
666 t1 = base1;
667 move1 = 1;
668 if (!t1)
669 return "bad types";
670 continue;
673 if (t2->type == SYM_NODE || t2->type == SYM_ENUM) {
674 t2 = base2;
675 move2 = 1;
676 if (!t2)
677 return "bad types";
678 continue;
681 move1 = move2 = 1;
682 type = t1->type;
683 if (type != t2->type)
684 return "different base types";
686 switch (type) {
687 default:
688 sparse_error(t1->pos,
689 "internal error: bad type in derived(%d)",
690 type);
691 return "bad types";
692 case SYM_RESTRICT:
693 return "different base types";
694 case SYM_UNION:
695 case SYM_STRUCT:
696 /* allow definition of incomplete structs and unions */
697 if (t1->ident == t2->ident)
698 return NULL;
699 return "different base types";
700 case SYM_ARRAY:
701 /* XXX: we ought to compare sizes */
702 break;
703 case SYM_PTR:
704 if (as1 != as2)
705 return "different address spaces";
706 /* MOD_SPECIFIER is due to idiocy in parse.c */
707 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SPECIFIER)
708 return "different modifiers";
709 /* we could be lazier here */
710 base1 = examine_pointer_target(t1);
711 base2 = examine_pointer_target(t2);
712 mod1 = t1->ctype.modifiers;
713 as1 = t1->ctype.as;
714 mod2 = t2->ctype.modifiers;
715 as2 = t2->ctype.as;
716 break;
717 case SYM_FN: {
718 struct symbol *arg1, *arg2;
719 int i;
721 if (as1 != as2)
722 return "different address spaces";
723 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
724 return "different modifiers";
725 mod1 = t1->ctype.modifiers;
726 as1 = t1->ctype.as;
727 mod2 = t2->ctype.modifiers;
728 as2 = t2->ctype.as;
730 if (t1->variadic != t2->variadic)
731 return "incompatible variadic arguments";
732 examine_fn_arguments(t1);
733 examine_fn_arguments(t2);
734 PREPARE_PTR_LIST(t1->arguments, arg1);
735 PREPARE_PTR_LIST(t2->arguments, arg2);
736 i = 1;
737 for (;;) {
738 const char *diffstr;
739 if (!arg1 && !arg2)
740 break;
741 if (!arg1 || !arg2)
742 return "different argument counts";
743 diffstr = type_difference(&arg1->ctype,
744 &arg2->ctype,
745 MOD_IGN, MOD_IGN);
746 if (diffstr) {
747 static char argdiff[80];
748 sprintf(argdiff, "incompatible argument %d (%s)", i, diffstr);
749 return argdiff;
751 NEXT_PTR_LIST(arg1);
752 NEXT_PTR_LIST(arg2);
753 i++;
755 FINISH_PTR_LIST(arg2);
756 FINISH_PTR_LIST(arg1);
757 break;
759 case SYM_BASETYPE:
760 if (as1 != as2)
761 return "different address spaces";
762 if (base1 != base2)
763 return "different base types";
764 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
765 if (!diff)
766 return NULL;
767 if (diff & MOD_SIZE)
768 return "different type sizes";
769 else if (diff & ~MOD_SIGNEDNESS)
770 return "different modifiers";
771 else
772 return "different signedness";
774 t1 = base1;
775 t2 = base2;
777 if (as1 != as2)
778 return "different address spaces";
779 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
780 return "different modifiers";
781 return NULL;
784 static void bad_null(struct expression *expr)
786 if (Wnon_pointer_null)
787 warning(expr->pos, "Using plain integer as NULL pointer");
790 static unsigned long target_qualifiers(struct symbol *type)
792 unsigned long mod = type->ctype.modifiers & MOD_IGN;
793 if (type->ctype.base_type && type->ctype.base_type->type == SYM_ARRAY)
794 mod = 0;
795 return mod;
798 static struct symbol *evaluate_ptr_sub(struct expression *expr)
800 const char *typediff;
801 struct symbol *ltype, *rtype;
802 struct expression *l = expr->left;
803 struct expression *r = expr->right;
804 struct symbol *lbase;
806 classify_type(degenerate(l), &ltype);
807 classify_type(degenerate(r), &rtype);
809 lbase = examine_pointer_target(ltype);
810 examine_pointer_target(rtype);
811 typediff = type_difference(&ltype->ctype, &rtype->ctype,
812 target_qualifiers(rtype),
813 target_qualifiers(ltype));
814 if (typediff)
815 expression_error(expr, "subtraction of different types can't work (%s)", typediff);
817 if (is_function(lbase)) {
818 expression_error(expr, "subtraction of functions? Share your drugs");
819 return NULL;
822 expr->ctype = ssize_t_ctype;
823 if (lbase->bit_size > bits_in_char) {
824 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
825 struct expression *div = expr;
826 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
827 unsigned long value = bits_to_bytes(lbase->bit_size);
829 val->ctype = size_t_ctype;
830 val->value = value;
832 if (value & (value-1)) {
833 if (Wptr_subtraction_blows)
834 warning(expr->pos, "potentially expensive pointer subtraction");
837 sub->op = '-';
838 sub->ctype = ssize_t_ctype;
839 sub->left = l;
840 sub->right = r;
842 div->op = '/';
843 div->left = sub;
844 div->right = val;
847 return ssize_t_ctype;
850 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
852 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
854 struct symbol *ctype;
856 if (!expr)
857 return NULL;
859 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
860 warning(expr->pos, "assignment expression in conditional");
862 ctype = evaluate_expression(expr);
863 if (ctype) {
864 if (is_safe_type(ctype))
865 warning(expr->pos, "testing a 'safe expression'");
868 return ctype;
871 static struct symbol *evaluate_logical(struct expression *expr)
873 if (!evaluate_conditional(expr->left, 0))
874 return NULL;
875 if (!evaluate_conditional(expr->right, 0))
876 return NULL;
878 /* the result is int [6.5.13(3), 6.5.14(3)] */
879 expr->ctype = &int_ctype;
880 if (expr->flags) {
881 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
882 expr->flags = 0;
884 return &int_ctype;
887 static struct symbol *evaluate_binop(struct expression *expr)
889 struct symbol *ltype, *rtype, *ctype;
890 int lclass = classify_type(expr->left->ctype, &ltype);
891 int rclass = classify_type(expr->right->ctype, &rtype);
892 int op = expr->op;
894 if (expr->flags) {
895 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
896 expr->flags = 0;
899 /* number op number */
900 if (lclass & rclass & TYPE_NUM) {
901 if ((lclass | rclass) & TYPE_FLOAT) {
902 switch (op) {
903 case '+': case '-': case '*': case '/':
904 break;
905 default:
906 return bad_expr_type(expr);
910 if (op == SPECIAL_LEFTSHIFT || op == SPECIAL_RIGHTSHIFT) {
911 // shifts do integer promotions, but that's it.
912 unrestrict(expr->left, lclass, &ltype);
913 unrestrict(expr->right, rclass, &rtype);
914 ctype = ltype = integer_promotion(ltype);
915 rtype = integer_promotion(rtype);
916 } else {
917 // The rest do usual conversions
918 const unsigned left_not = expr->left->type == EXPR_PREOP
919 && expr->left->op == '!';
920 const unsigned right_not = expr->right->type == EXPR_PREOP
921 && expr->right->op == '!';
922 if ((op == '&' || op == '|') && (left_not || right_not))
923 warning(expr->pos, "dubious: %sx %c %sy",
924 left_not ? "!" : "",
926 right_not ? "!" : "");
928 ltype = usual_conversions(op, expr->left, expr->right,
929 lclass, rclass, ltype, rtype);
930 ctype = rtype = ltype;
933 expr->left = cast_to(expr->left, ltype);
934 expr->right = cast_to(expr->right, rtype);
935 expr->ctype = ctype;
936 return ctype;
939 /* pointer (+|-) integer */
940 if (lclass & TYPE_PTR && is_int(rclass) && (op == '+' || op == '-')) {
941 unrestrict(expr->right, rclass, &rtype);
942 return evaluate_ptr_add(expr, rtype);
945 /* integer + pointer */
946 if (rclass & TYPE_PTR && is_int(lclass) && op == '+') {
947 struct expression *index = expr->left;
948 unrestrict(index, lclass, &ltype);
949 expr->left = expr->right;
950 expr->right = index;
951 return evaluate_ptr_add(expr, ltype);
954 /* pointer - pointer */
955 if (lclass & rclass & TYPE_PTR && expr->op == '-')
956 return evaluate_ptr_sub(expr);
958 return bad_expr_type(expr);
961 static struct symbol *evaluate_comma(struct expression *expr)
963 expr->ctype = degenerate(expr->right);
964 if (expr->ctype == &null_ctype)
965 expr->ctype = &ptr_ctype;
966 expr->flags &= expr->left->flags & expr->right->flags;
967 return expr->ctype;
970 static int modify_for_unsigned(int op)
972 if (op == '<')
973 op = SPECIAL_UNSIGNED_LT;
974 else if (op == '>')
975 op = SPECIAL_UNSIGNED_GT;
976 else if (op == SPECIAL_LTE)
977 op = SPECIAL_UNSIGNED_LTE;
978 else if (op == SPECIAL_GTE)
979 op = SPECIAL_UNSIGNED_GTE;
980 return op;
983 static inline int is_null_pointer_constant(struct expression *e)
985 if (e->ctype == &null_ctype)
986 return 1;
987 if (!(e->flags & Int_const_expr))
988 return 0;
989 return is_zero_constant(e) ? 2 : 0;
992 static struct symbol *evaluate_compare(struct expression *expr)
994 struct expression *left = expr->left, *right = expr->right;
995 struct symbol *ltype, *rtype, *lbase, *rbase;
996 int lclass = classify_type(degenerate(left), &ltype);
997 int rclass = classify_type(degenerate(right), &rtype);
998 struct symbol *ctype;
999 const char *typediff;
1001 if (expr->flags) {
1002 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
1003 expr->flags = 0;
1006 /* Type types? */
1007 if (is_type_type(ltype) && is_type_type(rtype))
1008 goto OK;
1010 if (is_safe_type(left->ctype) || is_safe_type(right->ctype))
1011 warning(expr->pos, "testing a 'safe expression'");
1013 /* number on number */
1014 if (lclass & rclass & TYPE_NUM) {
1015 ctype = usual_conversions(expr->op, expr->left, expr->right,
1016 lclass, rclass, ltype, rtype);
1017 expr->left = cast_to(expr->left, ctype);
1018 expr->right = cast_to(expr->right, ctype);
1019 if (ctype->ctype.modifiers & MOD_UNSIGNED)
1020 expr->op = modify_for_unsigned(expr->op);
1021 goto OK;
1024 /* at least one must be a pointer */
1025 if (!((lclass | rclass) & TYPE_PTR))
1026 return bad_expr_type(expr);
1028 /* equality comparisons can be with null pointer constants */
1029 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1030 int is_null1 = is_null_pointer_constant(left);
1031 int is_null2 = is_null_pointer_constant(right);
1032 if (is_null1 == 2)
1033 bad_null(left);
1034 if (is_null2 == 2)
1035 bad_null(right);
1036 if (is_null1 && is_null2) {
1037 int positive = expr->op == SPECIAL_EQUAL;
1038 expr->type = EXPR_VALUE;
1039 expr->value = positive;
1040 goto OK;
1042 if (is_null1 && (rclass & TYPE_PTR)) {
1043 left = cast_to(left, rtype);
1044 goto OK;
1046 if (is_null2 && (lclass & TYPE_PTR)) {
1047 right = cast_to(right, ltype);
1048 goto OK;
1051 /* both should be pointers */
1052 if (!(lclass & rclass & TYPE_PTR))
1053 return bad_expr_type(expr);
1054 expr->op = modify_for_unsigned(expr->op);
1056 lbase = examine_pointer_target(ltype);
1057 rbase = examine_pointer_target(rtype);
1059 /* they also have special treatment for pointers to void */
1060 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1061 if (ltype->ctype.as == rtype->ctype.as) {
1062 if (lbase == &void_ctype) {
1063 right = cast_to(right, ltype);
1064 goto OK;
1066 if (rbase == &void_ctype) {
1067 left = cast_to(left, rtype);
1068 goto OK;
1073 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1074 target_qualifiers(rtype),
1075 target_qualifiers(ltype));
1076 if (!typediff)
1077 goto OK;
1079 expression_error(expr, "incompatible types in comparison expression (%s)", typediff);
1080 return NULL;
1083 /* the result is int [6.5.8(6), 6.5.9(3)]*/
1084 expr->ctype = &int_ctype;
1085 return &int_ctype;
1089 * NOTE! The degenerate case of "x ? : y", where we don't
1090 * have a true case, this will possibly promote "x" to the
1091 * same type as "y", and thus _change_ the conditional
1092 * test in the expression. But since promotion is "safe"
1093 * for testing, that's OK.
1095 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1097 struct expression **true;
1098 struct symbol *ctype, *ltype, *rtype, *lbase, *rbase;
1099 int lclass, rclass;
1100 const char * typediff;
1101 int qual;
1103 if (!evaluate_conditional(expr->conditional, 0))
1104 return NULL;
1105 if (!evaluate_expression(expr->cond_false))
1106 return NULL;
1108 ctype = degenerate(expr->conditional);
1109 rtype = degenerate(expr->cond_false);
1111 true = &expr->conditional;
1112 ltype = ctype;
1113 if (expr->cond_true) {
1114 if (!evaluate_expression(expr->cond_true))
1115 return NULL;
1116 ltype = degenerate(expr->cond_true);
1117 true = &expr->cond_true;
1120 if (expr->flags) {
1121 int flags = expr->conditional->flags & Int_const_expr;
1122 flags &= (*true)->flags & expr->cond_false->flags;
1123 if (!flags)
1124 expr->flags = 0;
1127 lclass = classify_type(ltype, &ltype);
1128 rclass = classify_type(rtype, &rtype);
1129 if (lclass & rclass & TYPE_NUM) {
1130 ctype = usual_conversions('?', *true, expr->cond_false,
1131 lclass, rclass, ltype, rtype);
1132 *true = cast_to(*true, ctype);
1133 expr->cond_false = cast_to(expr->cond_false, ctype);
1134 goto out;
1137 if ((lclass | rclass) & TYPE_PTR) {
1138 int is_null1 = is_null_pointer_constant(*true);
1139 int is_null2 = is_null_pointer_constant(expr->cond_false);
1141 if (is_null1 && is_null2) {
1142 *true = cast_to(*true, &ptr_ctype);
1143 expr->cond_false = cast_to(expr->cond_false, &ptr_ctype);
1144 ctype = &ptr_ctype;
1145 goto out;
1147 if (is_null1 && (rclass & TYPE_PTR)) {
1148 if (is_null1 == 2)
1149 bad_null(*true);
1150 *true = cast_to(*true, rtype);
1151 ctype = rtype;
1152 goto out;
1154 if (is_null2 && (lclass & TYPE_PTR)) {
1155 if (is_null2 == 2)
1156 bad_null(expr->cond_false);
1157 expr->cond_false = cast_to(expr->cond_false, ltype);
1158 ctype = ltype;
1159 goto out;
1161 if (!(lclass & rclass & TYPE_PTR)) {
1162 typediff = "different types";
1163 goto Err;
1165 /* OK, it's pointer on pointer */
1166 if (ltype->ctype.as != rtype->ctype.as) {
1167 typediff = "different address spaces";
1168 goto Err;
1171 /* need to be lazier here */
1172 lbase = examine_pointer_target(ltype);
1173 rbase = examine_pointer_target(rtype);
1174 qual = target_qualifiers(ltype) | target_qualifiers(rtype);
1176 if (lbase == &void_ctype) {
1177 /* XXX: pointers to function should warn here */
1178 ctype = ltype;
1179 goto Qual;
1182 if (rbase == &void_ctype) {
1183 /* XXX: pointers to function should warn here */
1184 ctype = rtype;
1185 goto Qual;
1187 /* XXX: that should be pointer to composite */
1188 ctype = ltype;
1189 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1190 qual, qual);
1191 if (!typediff)
1192 goto Qual;
1193 goto Err;
1196 /* void on void, struct on same struct, union on same union */
1197 if (ltype == rtype) {
1198 ctype = ltype;
1199 goto out;
1201 typediff = "different base types";
1203 Err:
1204 expression_error(expr, "incompatible types in conditional expression (%s)", typediff);
1205 return NULL;
1207 out:
1208 expr->ctype = ctype;
1209 return ctype;
1211 Qual:
1212 if (qual & ~ctype->ctype.modifiers) {
1213 struct symbol *sym = alloc_symbol(ctype->pos, SYM_PTR);
1214 *sym = *ctype;
1215 sym->ctype.modifiers |= qual;
1216 ctype = sym;
1218 *true = cast_to(*true, ctype);
1219 expr->cond_false = cast_to(expr->cond_false, ctype);
1220 goto out;
1223 /* FP assignments can not do modulo or bit operations */
1224 static int compatible_float_op(int op)
1226 return op == SPECIAL_ADD_ASSIGN ||
1227 op == SPECIAL_SUB_ASSIGN ||
1228 op == SPECIAL_MUL_ASSIGN ||
1229 op == SPECIAL_DIV_ASSIGN;
1232 static int evaluate_assign_op(struct expression *expr)
1234 struct symbol *target = expr->left->ctype;
1235 struct symbol *source = expr->right->ctype;
1236 struct symbol *t, *s;
1237 int tclass = classify_type(target, &t);
1238 int sclass = classify_type(source, &s);
1239 int op = expr->op;
1241 if (tclass & sclass & TYPE_NUM) {
1242 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1243 expression_error(expr, "invalid assignment");
1244 return 0;
1246 if (tclass & TYPE_RESTRICT) {
1247 if (!restricted_binop(op, t)) {
1248 warning(expr->pos, "bad assignment (%s) to %s",
1249 show_special(op), show_typename(t));
1250 expr->right = cast_to(expr->right, target);
1251 return 0;
1253 /* allowed assignments unfoul */
1254 if (sclass & TYPE_FOULED && unfoul(s) == t)
1255 goto Cast;
1256 if (!restricted_value(expr->right, t))
1257 return 1;
1258 } else if (!(sclass & TYPE_RESTRICT))
1259 goto Cast;
1260 /* source and target would better be identical restricted */
1261 if (t == s)
1262 return 1;
1263 warning(expr->pos, "invalid assignment: %s", show_special(op));
1264 info(expr->pos, " left side has type %s", show_typename(t));
1265 info(expr->pos, " right side has type %s", show_typename(s));
1266 expr->right = cast_to(expr->right, target);
1267 return 0;
1269 if (tclass == TYPE_PTR && is_int(sclass)) {
1270 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1271 unrestrict(expr->right, sclass, &s);
1272 evaluate_ptr_add(expr, s);
1273 return 1;
1275 expression_error(expr, "invalid pointer assignment");
1276 return 0;
1279 expression_error(expr, "invalid assignment");
1280 return 0;
1282 Cast:
1283 expr->right = cast_to(expr->right, target);
1284 return 1;
1287 static int whitelist_pointers(struct symbol *t1, struct symbol *t2)
1289 if (t1 == t2)
1290 return 0; /* yes, 0 - we don't want a cast_to here */
1291 if (t1 == &void_ctype)
1292 return 1;
1293 if (t2 == &void_ctype)
1294 return 1;
1295 if (classify_type(t1, &t1) != TYPE_NUM)
1296 return 0;
1297 if (classify_type(t2, &t2) != TYPE_NUM)
1298 return 0;
1299 if (t1 == t2)
1300 return 1;
1301 if (t1->ctype.modifiers & t2->ctype.modifiers & MOD_CHAR)
1302 return 1;
1303 if ((t1->ctype.modifiers ^ t2->ctype.modifiers) & MOD_SIZE)
1304 return 0;
1305 return !Wtypesign;
1308 static int check_assignment_types(struct symbol *target, struct expression **rp,
1309 const char **typediff)
1311 struct symbol *source = degenerate(*rp);
1312 struct symbol *t, *s;
1313 int tclass = classify_type(target, &t);
1314 int sclass = classify_type(source, &s);
1316 if (tclass & sclass & TYPE_NUM) {
1317 if (tclass & TYPE_RESTRICT) {
1318 /* allowed assignments unfoul */
1319 if (sclass & TYPE_FOULED && unfoul(s) == t)
1320 goto Cast;
1321 if (!restricted_value(*rp, target))
1322 return 1;
1323 if (s == t)
1324 return 1;
1325 } else if (!(sclass & TYPE_RESTRICT))
1326 goto Cast;
1327 *typediff = "different base types";
1328 return 0;
1331 if (tclass == TYPE_PTR) {
1332 unsigned long mod1, mod2;
1333 struct symbol *b1, *b2;
1334 // NULL pointer is always OK
1335 int is_null = is_null_pointer_constant(*rp);
1336 if (is_null) {
1337 if (is_null == 2)
1338 bad_null(*rp);
1339 goto Cast;
1341 if (!(sclass & TYPE_PTR)) {
1342 *typediff = "different base types";
1343 return 0;
1345 b1 = examine_pointer_target(t);
1346 b2 = examine_pointer_target(s);
1347 mod1 = target_qualifiers(t);
1348 mod2 = target_qualifiers(s);
1349 if (whitelist_pointers(b1, b2)) {
1351 * assignments to/from void * are OK, provided that
1352 * we do not remove qualifiers from pointed to [C]
1353 * or mix address spaces [sparse].
1355 if (t->ctype.as != s->ctype.as) {
1356 *typediff = "different address spaces";
1357 return 0;
1360 * If this is a function pointer assignment, it is
1361 * actually fine to assign a pointer to const data to
1362 * it, as a function pointer points to const data
1363 * implicitly, i.e., dereferencing it does not produce
1364 * an lvalue.
1366 if (b1->type == SYM_FN)
1367 mod1 |= MOD_CONST;
1368 if (mod2 & ~mod1) {
1369 *typediff = "different modifiers";
1370 return 0;
1372 goto Cast;
1374 /* It's OK if the target is more volatile or const than the source */
1375 *typediff = type_difference(&t->ctype, &s->ctype, 0, mod1);
1376 if (*typediff)
1377 return 0;
1378 return 1;
1381 if ((tclass & TYPE_COMPOUND) && s == t)
1382 return 1;
1384 if (tclass & TYPE_NUM) {
1385 /* XXX: need to turn into comparison with NULL */
1386 if (t == &bool_ctype && (sclass & TYPE_PTR))
1387 goto Cast;
1388 *typediff = "different base types";
1389 return 0;
1391 *typediff = "invalid types";
1392 return 0;
1394 Cast:
1395 *rp = cast_to(*rp, target);
1396 return 1;
1399 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1400 struct expression **rp, const char *where)
1402 const char *typediff;
1403 struct symbol *source = degenerate(*rp);
1405 if (!check_assignment_types(target, rp, &typediff)) {
1406 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1407 info(expr->pos, " expected %s", show_typename(target));
1408 info(expr->pos, " got %s", show_typename(source));
1409 *rp = cast_to(*rp, target);
1410 return 0;
1413 return 1;
1416 static int compatible_transparent_union(struct symbol *target,
1417 struct expression **rp)
1419 struct symbol *t, *member;
1420 classify_type(target, &t);
1421 if (t->type != SYM_UNION || !t->transparent_union)
1422 return 0;
1424 FOR_EACH_PTR(t->symbol_list, member) {
1425 const char *typediff;
1426 if (check_assignment_types(member, rp, &typediff))
1427 return 1;
1428 } END_FOR_EACH_PTR(member);
1430 return 0;
1433 static int compatible_argument_type(struct expression *expr, struct symbol *target,
1434 struct expression **rp, const char *where)
1436 if (compatible_transparent_union(target, rp))
1437 return 1;
1439 return compatible_assignment_types(expr, target, rp, where);
1442 static void mark_assigned(struct expression *expr)
1444 struct symbol *sym;
1446 if (!expr)
1447 return;
1448 switch (expr->type) {
1449 case EXPR_SYMBOL:
1450 sym = expr->symbol;
1451 if (!sym)
1452 return;
1453 if (sym->type != SYM_NODE)
1454 return;
1455 sym->ctype.modifiers |= MOD_ASSIGNED;
1456 return;
1458 case EXPR_BINOP:
1459 mark_assigned(expr->left);
1460 mark_assigned(expr->right);
1461 return;
1462 case EXPR_CAST:
1463 case EXPR_FORCE_CAST:
1464 mark_assigned(expr->cast_expression);
1465 return;
1466 case EXPR_SLICE:
1467 mark_assigned(expr->base);
1468 return;
1469 default:
1470 /* Hmm? */
1471 return;
1475 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1477 if (type->ctype.modifiers & MOD_CONST)
1478 expression_error(left, "assignment to const expression");
1480 /* We know left is an lvalue, so it's a "preop-*" */
1481 mark_assigned(left->unop);
1484 static struct symbol *evaluate_assignment(struct expression *expr)
1486 struct expression *left = expr->left;
1487 struct expression *where = expr;
1488 struct symbol *ltype;
1490 if (!lvalue_expression(left)) {
1491 expression_error(expr, "not an lvalue");
1492 return NULL;
1495 ltype = left->ctype;
1497 if (expr->op != '=') {
1498 if (!evaluate_assign_op(expr))
1499 return NULL;
1500 } else {
1501 if (!compatible_assignment_types(where, ltype, &expr->right, "assignment"))
1502 return NULL;
1505 evaluate_assign_to(left, ltype);
1507 expr->ctype = ltype;
1508 return ltype;
1511 static void examine_fn_arguments(struct symbol *fn)
1513 struct symbol *s;
1515 FOR_EACH_PTR(fn->arguments, s) {
1516 struct symbol *arg = evaluate_symbol(s);
1517 /* Array/function arguments silently degenerate into pointers */
1518 if (arg) {
1519 struct symbol *ptr;
1520 switch(arg->type) {
1521 case SYM_ARRAY:
1522 case SYM_FN:
1523 ptr = alloc_symbol(s->pos, SYM_PTR);
1524 if (arg->type == SYM_ARRAY)
1525 ptr->ctype = arg->ctype;
1526 else
1527 ptr->ctype.base_type = arg;
1528 ptr->ctype.as |= s->ctype.as;
1529 ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1531 s->ctype.base_type = ptr;
1532 s->ctype.as = 0;
1533 s->ctype.modifiers &= ~MOD_PTRINHERIT;
1534 s->bit_size = 0;
1535 s->examined = 0;
1536 examine_symbol_type(s);
1537 break;
1538 default:
1539 /* nothing */
1540 break;
1543 } END_FOR_EACH_PTR(s);
1546 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1548 /* Take the modifiers of the pointer, and apply them to the member */
1549 mod |= sym->ctype.modifiers;
1550 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1551 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1552 *newsym = *sym;
1553 newsym->ctype.as = as;
1554 newsym->ctype.modifiers = mod;
1555 sym = newsym;
1557 return sym;
1560 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1562 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1563 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1565 node->ctype.base_type = ptr;
1566 ptr->bit_size = bits_in_pointer;
1567 ptr->ctype.alignment = pointer_alignment;
1569 node->bit_size = bits_in_pointer;
1570 node->ctype.alignment = pointer_alignment;
1572 access_symbol(sym);
1573 if (sym->ctype.modifiers & MOD_REGISTER) {
1574 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1575 sym->ctype.modifiers &= ~MOD_REGISTER;
1577 if (sym->type == SYM_NODE) {
1578 ptr->ctype.as |= sym->ctype.as;
1579 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1580 sym = sym->ctype.base_type;
1582 if (degenerate && sym->type == SYM_ARRAY) {
1583 ptr->ctype.as |= sym->ctype.as;
1584 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1585 sym = sym->ctype.base_type;
1587 ptr->ctype.base_type = sym;
1589 return node;
1592 /* Arrays degenerate into pointers on pointer arithmetic */
1593 static struct symbol *degenerate(struct expression *expr)
1595 struct symbol *ctype, *base;
1597 if (!expr)
1598 return NULL;
1599 ctype = expr->ctype;
1600 if (!ctype)
1601 return NULL;
1602 base = examine_symbol_type(ctype);
1603 if (ctype->type == SYM_NODE)
1604 base = ctype->ctype.base_type;
1606 * Arrays degenerate into pointers to the entries, while
1607 * functions degenerate into pointers to themselves.
1608 * If array was part of non-lvalue compound, we create a copy
1609 * of that compound first and then act as if we were dealing with
1610 * the corresponding field in there.
1612 switch (base->type) {
1613 case SYM_ARRAY:
1614 if (expr->type == EXPR_SLICE) {
1615 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1616 struct expression *e0, *e1, *e2, *e3, *e4;
1618 a->ctype.base_type = expr->base->ctype;
1619 a->bit_size = expr->base->ctype->bit_size;
1620 a->array_size = expr->base->ctype->array_size;
1622 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1623 e0->symbol = a;
1624 e0->ctype = &lazy_ptr_ctype;
1626 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1627 e1->unop = e0;
1628 e1->op = '*';
1629 e1->ctype = expr->base->ctype; /* XXX */
1631 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1632 e2->left = e1;
1633 e2->right = expr->base;
1634 e2->op = '=';
1635 e2->ctype = expr->base->ctype;
1637 if (expr->r_bitpos) {
1638 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1639 e3->op = '+';
1640 e3->left = e0;
1641 e3->right = alloc_const_expression(expr->pos,
1642 bits_to_bytes(expr->r_bitpos));
1643 e3->ctype = &lazy_ptr_ctype;
1644 } else {
1645 e3 = e0;
1648 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1649 e4->left = e2;
1650 e4->right = e3;
1651 e4->ctype = &lazy_ptr_ctype;
1653 expr->unop = e4;
1654 expr->type = EXPR_PREOP;
1655 expr->op = '*';
1657 case SYM_FN:
1658 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1659 expression_error(expr, "strange non-value function or array");
1660 return &bad_ctype;
1662 *expr = *expr->unop;
1663 ctype = create_pointer(expr, ctype, 1);
1664 expr->ctype = ctype;
1665 default:
1666 /* nothing */;
1668 return ctype;
1671 static struct symbol *evaluate_addressof(struct expression *expr)
1673 struct expression *op = expr->unop;
1674 struct symbol *ctype;
1676 if (op->op != '*' || op->type != EXPR_PREOP) {
1677 expression_error(expr, "not addressable");
1678 return NULL;
1680 ctype = op->ctype;
1681 *expr = *op->unop;
1682 expr->flags = 0;
1684 if (expr->type == EXPR_SYMBOL) {
1685 struct symbol *sym = expr->symbol;
1686 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1690 * symbol expression evaluation is lazy about the type
1691 * of the sub-expression, so we may have to generate
1692 * the type here if so..
1694 if (expr->ctype == &lazy_ptr_ctype) {
1695 ctype = create_pointer(expr, ctype, 0);
1696 expr->ctype = ctype;
1698 return expr->ctype;
1702 static struct symbol *evaluate_dereference(struct expression *expr)
1704 struct expression *op = expr->unop;
1705 struct symbol *ctype = op->ctype, *node, *target;
1707 /* Simplify: *&(expr) => (expr) */
1708 if (op->type == EXPR_PREOP && op->op == '&') {
1709 *expr = *op->unop;
1710 expr->flags = 0;
1711 return expr->ctype;
1714 /* Dereferencing a node drops all the node information. */
1715 if (ctype->type == SYM_NODE)
1716 ctype = ctype->ctype.base_type;
1718 node = alloc_symbol(expr->pos, SYM_NODE);
1719 target = ctype->ctype.base_type;
1721 switch (ctype->type) {
1722 default:
1723 expression_error(expr, "cannot dereference this type");
1724 return NULL;
1725 case SYM_PTR:
1726 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1727 merge_type(node, ctype);
1728 break;
1730 case SYM_ARRAY:
1731 if (!lvalue_expression(op)) {
1732 expression_error(op, "non-lvalue array??");
1733 return NULL;
1736 /* Do the implied "addressof" on the array */
1737 *op = *op->unop;
1740 * When an array is dereferenced, we need to pick
1741 * up the attributes of the original node too..
1743 merge_type(node, op->ctype);
1744 merge_type(node, ctype);
1745 break;
1748 node->bit_size = target->bit_size;
1749 node->array_size = target->array_size;
1751 expr->ctype = node;
1752 return node;
1756 * Unary post-ops: x++ and x--
1758 static struct symbol *evaluate_postop(struct expression *expr)
1760 struct expression *op = expr->unop;
1761 struct symbol *ctype = op->ctype;
1762 int class = classify_type(ctype, &ctype);
1763 int multiply = 0;
1765 if (!class || class & TYPE_COMPOUND) {
1766 expression_error(expr, "need scalar for ++/--");
1767 return NULL;
1769 if (!lvalue_expression(expr->unop)) {
1770 expression_error(expr, "need lvalue expression for ++/--");
1771 return NULL;
1774 if ((class & TYPE_RESTRICT) && restricted_unop(expr->op, &ctype))
1775 unrestrict(expr, class, &ctype);
1777 if (class & TYPE_NUM) {
1778 multiply = 1;
1779 } else if (class == TYPE_PTR) {
1780 struct symbol *target = examine_pointer_target(ctype);
1781 if (!is_function(target))
1782 multiply = bits_to_bytes(target->bit_size);
1785 if (multiply) {
1786 evaluate_assign_to(op, op->ctype);
1787 expr->op_value = multiply;
1788 expr->ctype = ctype;
1789 return ctype;
1792 expression_error(expr, "bad argument type for ++/--");
1793 return NULL;
1796 static struct symbol *evaluate_sign(struct expression *expr)
1798 struct symbol *ctype = expr->unop->ctype;
1799 int class = classify_type(ctype, &ctype);
1800 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1801 expr->flags = 0;
1802 /* should be an arithmetic type */
1803 if (!(class & TYPE_NUM))
1804 return bad_expr_type(expr);
1805 if (class & TYPE_RESTRICT)
1806 goto Restr;
1807 Normal:
1808 if (!(class & TYPE_FLOAT)) {
1809 ctype = integer_promotion(ctype);
1810 expr->unop = cast_to(expr->unop, ctype);
1811 } else if (expr->op != '~') {
1812 /* no conversions needed */
1813 } else {
1814 return bad_expr_type(expr);
1816 if (expr->op == '+')
1817 *expr = *expr->unop;
1818 expr->ctype = ctype;
1819 return ctype;
1820 Restr:
1821 if (restricted_unop(expr->op, &ctype))
1822 unrestrict(expr, class, &ctype);
1823 goto Normal;
1826 static struct symbol *evaluate_preop(struct expression *expr)
1828 struct symbol *ctype = expr->unop->ctype;
1830 switch (expr->op) {
1831 case '(':
1832 *expr = *expr->unop;
1833 return ctype;
1835 case '+':
1836 case '-':
1837 case '~':
1838 return evaluate_sign(expr);
1840 case '*':
1841 return evaluate_dereference(expr);
1843 case '&':
1844 return evaluate_addressof(expr);
1846 case SPECIAL_INCREMENT:
1847 case SPECIAL_DECREMENT:
1849 * From a type evaluation standpoint the preops are
1850 * the same as the postops
1852 return evaluate_postop(expr);
1854 case '!':
1855 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1856 expr->flags = 0;
1857 if (is_safe_type(ctype))
1858 warning(expr->pos, "testing a 'safe expression'");
1859 if (is_float_type(ctype)) {
1860 struct expression *arg = expr->unop;
1861 expr->type = EXPR_COMPARE;
1862 expr->op = SPECIAL_EQUAL;
1863 expr->left = arg;
1864 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1865 expr->right->ctype = ctype;
1866 expr->right->fvalue = 0;
1867 } else if (is_fouled_type(ctype)) {
1868 warning(expr->pos, "%s degrades to integer",
1869 show_typename(ctype->ctype.base_type));
1871 /* the result is int [6.5.3.3(5)]*/
1872 ctype = &int_ctype;
1873 break;
1875 default:
1876 break;
1878 expr->ctype = ctype;
1879 return ctype;
1882 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1884 struct ptr_list *head = (struct ptr_list *)_list;
1885 struct ptr_list *list = head;
1887 if (!head)
1888 return NULL;
1889 do {
1890 int i;
1891 for (i = 0; i < list->nr; i++) {
1892 struct symbol *sym = (struct symbol *) list->list[i];
1893 if (sym->ident) {
1894 if (sym->ident != ident)
1895 continue;
1896 *offset = sym->offset;
1897 return sym;
1898 } else {
1899 struct symbol *ctype = sym->ctype.base_type;
1900 struct symbol *sub;
1901 if (!ctype)
1902 continue;
1903 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1904 continue;
1905 sub = find_identifier(ident, ctype->symbol_list, offset);
1906 if (!sub)
1907 continue;
1908 *offset += sym->offset;
1909 return sub;
1912 } while ((list = list->next) != head);
1913 return NULL;
1916 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1918 struct expression *add;
1921 * Create a new add-expression
1923 * NOTE! Even if we just add zero, we need a new node
1924 * for the member pointer, since it has a different
1925 * type than the original pointer. We could make that
1926 * be just a cast, but the fact is, a node is a node,
1927 * so we might as well just do the "add zero" here.
1929 add = alloc_expression(expr->pos, EXPR_BINOP);
1930 add->op = '+';
1931 add->left = expr;
1932 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1933 add->right->ctype = &int_ctype;
1934 add->right->value = offset;
1937 * The ctype of the pointer will be lazily evaluated if
1938 * we ever take the address of this member dereference..
1940 add->ctype = &lazy_ptr_ctype;
1941 return add;
1944 /* structure/union dereference */
1945 static struct symbol *evaluate_member_dereference(struct expression *expr)
1947 int offset;
1948 struct symbol *ctype, *member;
1949 struct expression *deref = expr->deref, *add;
1950 struct ident *ident = expr->member;
1951 unsigned int mod;
1952 int address_space;
1954 if (!evaluate_expression(deref))
1955 return NULL;
1956 if (!ident) {
1957 expression_error(expr, "bad member name");
1958 return NULL;
1961 ctype = deref->ctype;
1962 examine_symbol_type(ctype);
1963 address_space = ctype->ctype.as;
1964 mod = ctype->ctype.modifiers;
1965 if (ctype->type == SYM_NODE) {
1966 ctype = ctype->ctype.base_type;
1967 address_space |= ctype->ctype.as;
1968 mod |= ctype->ctype.modifiers;
1970 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1971 expression_error(expr, "expected structure or union");
1972 return NULL;
1974 offset = 0;
1975 member = find_identifier(ident, ctype->symbol_list, &offset);
1976 if (!member) {
1977 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1978 const char *name = "<unnamed>";
1979 int namelen = 9;
1980 if (ctype->ident) {
1981 name = ctype->ident->name;
1982 namelen = ctype->ident->len;
1984 if (ctype->symbol_list)
1985 expression_error(expr, "no member '%s' in %s %.*s",
1986 show_ident(ident), type, namelen, name);
1987 else
1988 expression_error(expr, "using member '%s' in "
1989 "incomplete %s %.*s", show_ident(ident),
1990 type, namelen, name);
1991 return NULL;
1995 * The member needs to take on the address space and modifiers of
1996 * the "parent" type.
1998 member = convert_to_as_mod(member, address_space, mod);
1999 ctype = get_base_type(member);
2001 if (!lvalue_expression(deref)) {
2002 if (deref->type != EXPR_SLICE) {
2003 expr->base = deref;
2004 expr->r_bitpos = 0;
2005 } else {
2006 expr->base = deref->base;
2007 expr->r_bitpos = deref->r_bitpos;
2009 expr->r_bitpos += bytes_to_bits(offset);
2010 expr->type = EXPR_SLICE;
2011 expr->r_nrbits = member->bit_size;
2012 expr->r_bitpos += member->bit_offset;
2013 expr->ctype = member;
2014 return member;
2017 deref = deref->unop;
2018 expr->deref = deref;
2020 add = evaluate_offset(deref, offset);
2021 expr->type = EXPR_PREOP;
2022 expr->op = '*';
2023 expr->unop = add;
2025 expr->ctype = member;
2026 return member;
2029 static int is_promoted(struct expression *expr)
2031 while (1) {
2032 switch (expr->type) {
2033 case EXPR_BINOP:
2034 case EXPR_SELECT:
2035 case EXPR_CONDITIONAL:
2036 return 1;
2037 case EXPR_COMMA:
2038 expr = expr->right;
2039 continue;
2040 case EXPR_PREOP:
2041 switch (expr->op) {
2042 case '(':
2043 expr = expr->unop;
2044 continue;
2045 case '+':
2046 case '-':
2047 case '~':
2048 return 1;
2049 default:
2050 return 0;
2052 default:
2053 return 0;
2059 static struct symbol *evaluate_cast(struct expression *);
2061 static struct symbol *evaluate_type_information(struct expression *expr)
2063 struct symbol *sym = expr->cast_type;
2064 if (!sym) {
2065 sym = evaluate_expression(expr->cast_expression);
2066 if (!sym)
2067 return NULL;
2069 * Expressions of restricted types will possibly get
2070 * promoted - check that here
2072 if (is_restricted_type(sym)) {
2073 if (sym->bit_size < bits_in_int && is_promoted(expr))
2074 sym = &int_ctype;
2075 } else if (is_fouled_type(sym)) {
2076 sym = &int_ctype;
2079 examine_symbol_type(sym);
2080 if (is_bitfield_type(sym)) {
2081 expression_error(expr, "trying to examine bitfield type");
2082 return NULL;
2084 return sym;
2087 static struct symbol *evaluate_sizeof(struct expression *expr)
2089 struct symbol *type;
2090 int size;
2092 type = evaluate_type_information(expr);
2093 if (!type)
2094 return NULL;
2096 size = type->bit_size;
2098 if (size < 0 && is_void_type(type)) {
2099 warning(expr->pos, "expression using sizeof(void)");
2100 size = bits_in_char;
2103 if (size == 1 && is_bool_type(type)) {
2104 if (Wsizeof_bool)
2105 warning(expr->pos, "expression using sizeof bool");
2106 size = bits_in_char;
2109 if (is_function(type->ctype.base_type)) {
2110 warning(expr->pos, "expression using sizeof on a function");
2111 size = bits_in_char;
2114 if ((size < 0) || (size & (bits_in_char - 1)))
2115 expression_error(expr, "cannot size expression");
2117 expr->type = EXPR_VALUE;
2118 expr->value = bits_to_bytes(size);
2119 expr->taint = 0;
2120 expr->ctype = size_t_ctype;
2121 return size_t_ctype;
2124 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
2126 struct symbol *type;
2127 int size;
2129 type = evaluate_type_information(expr);
2130 if (!type)
2131 return NULL;
2133 if (type->type == SYM_NODE)
2134 type = type->ctype.base_type;
2135 if (!type)
2136 return NULL;
2137 switch (type->type) {
2138 case SYM_ARRAY:
2139 break;
2140 case SYM_PTR:
2141 type = get_base_type(type);
2142 if (type)
2143 break;
2144 default:
2145 expression_error(expr, "expected pointer expression");
2146 return NULL;
2148 size = type->bit_size;
2149 if (size & (bits_in_char-1))
2150 size = 0;
2151 expr->type = EXPR_VALUE;
2152 expr->value = bits_to_bytes(size);
2153 expr->taint = 0;
2154 expr->ctype = size_t_ctype;
2155 return size_t_ctype;
2158 static struct symbol *evaluate_alignof(struct expression *expr)
2160 struct symbol *type;
2162 type = evaluate_type_information(expr);
2163 if (!type)
2164 return NULL;
2166 expr->type = EXPR_VALUE;
2167 expr->value = type->ctype.alignment;
2168 expr->taint = 0;
2169 expr->ctype = size_t_ctype;
2170 return size_t_ctype;
2173 static int evaluate_arguments(struct symbol *fn, struct expression_list *head)
2175 struct expression *expr;
2176 struct symbol_list *argument_types = fn->arguments;
2177 struct symbol *argtype;
2178 int i = 1;
2180 PREPARE_PTR_LIST(argument_types, argtype);
2181 FOR_EACH_PTR (head, expr) {
2182 struct expression **p = THIS_ADDRESS(expr);
2183 struct symbol *ctype, *target;
2184 ctype = evaluate_expression(expr);
2186 if (!ctype)
2187 return 0;
2189 target = argtype;
2190 if (!target) {
2191 struct symbol *type;
2192 int class = classify_type(ctype, &type);
2193 if (is_int(class)) {
2194 *p = cast_to(expr, integer_promotion(type));
2195 } else if (class & TYPE_FLOAT) {
2196 unsigned long mod = type->ctype.modifiers;
2197 if (!(mod & (MOD_LONG_ALL)))
2198 *p = cast_to(expr, &double_ctype);
2199 } else if (class & TYPE_PTR) {
2200 if (expr->ctype == &null_ctype)
2201 *p = cast_to(expr, &ptr_ctype);
2202 else
2203 degenerate(expr);
2205 } else if (!target->forced_arg){
2206 static char where[30];
2207 examine_symbol_type(target);
2208 sprintf(where, "argument %d", i);
2209 compatible_argument_type(expr, target, p, where);
2212 i++;
2213 NEXT_PTR_LIST(argtype);
2214 } END_FOR_EACH_PTR(expr);
2215 FINISH_PTR_LIST(argtype);
2216 return 1;
2219 static void convert_index(struct expression *e)
2221 struct expression *child = e->idx_expression;
2222 unsigned from = e->idx_from;
2223 unsigned to = e->idx_to + 1;
2224 e->type = EXPR_POS;
2225 e->init_offset = from * bits_to_bytes(e->ctype->bit_size);
2226 e->init_nr = to - from;
2227 e->init_expr = child;
2230 static void convert_ident(struct expression *e)
2232 struct expression *child = e->ident_expression;
2233 int offset = e->offset;
2235 e->type = EXPR_POS;
2236 e->init_offset = offset;
2237 e->init_nr = 1;
2238 e->init_expr = child;
2241 static void convert_designators(struct expression *e)
2243 while (e) {
2244 if (e->type == EXPR_INDEX)
2245 convert_index(e);
2246 else if (e->type == EXPR_IDENTIFIER)
2247 convert_ident(e);
2248 else
2249 break;
2250 e = e->init_expr;
2254 static void excess(struct expression *e, const char *s)
2256 warning(e->pos, "excessive elements in %s initializer", s);
2260 * implicit designator for the first element
2262 static struct expression *first_subobject(struct symbol *ctype, int class,
2263 struct expression **v)
2265 struct expression *e = *v, *new;
2267 if (ctype->type == SYM_NODE)
2268 ctype = ctype->ctype.base_type;
2270 if (class & TYPE_PTR) { /* array */
2271 if (!ctype->bit_size)
2272 return NULL;
2273 new = alloc_expression(e->pos, EXPR_INDEX);
2274 new->idx_expression = e;
2275 new->ctype = ctype->ctype.base_type;
2276 } else {
2277 struct symbol *field, *p;
2278 PREPARE_PTR_LIST(ctype->symbol_list, p);
2279 while (p && !p->ident && is_bitfield_type(p))
2280 NEXT_PTR_LIST(p);
2281 field = p;
2282 FINISH_PTR_LIST(p);
2283 if (!field)
2284 return NULL;
2285 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2286 new->ident_expression = e;
2287 new->field = new->ctype = field;
2288 new->offset = field->offset;
2290 *v = new;
2291 return new;
2295 * sanity-check explicit designators; return the innermost one or NULL
2296 * in case of error. Assign types.
2298 static struct expression *check_designators(struct expression *e,
2299 struct symbol *ctype)
2301 struct expression *last = NULL;
2302 const char *err;
2303 while (1) {
2304 if (ctype->type == SYM_NODE)
2305 ctype = ctype->ctype.base_type;
2306 if (e->type == EXPR_INDEX) {
2307 struct symbol *type;
2308 if (ctype->type != SYM_ARRAY) {
2309 err = "array index in non-array";
2310 break;
2312 type = ctype->ctype.base_type;
2313 if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2314 unsigned offset = array_element_offset(type->bit_size, e->idx_to);
2315 if (offset >= ctype->bit_size) {
2316 err = "index out of bounds in";
2317 break;
2320 e->ctype = ctype = type;
2321 ctype = type;
2322 last = e;
2323 if (!e->idx_expression) {
2324 err = "invalid";
2325 break;
2327 e = e->idx_expression;
2328 } else if (e->type == EXPR_IDENTIFIER) {
2329 int offset = 0;
2330 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2331 err = "field name not in struct or union";
2332 break;
2334 ctype = find_identifier(e->expr_ident, ctype->symbol_list, &offset);
2335 if (!ctype) {
2336 err = "unknown field name in";
2337 break;
2339 e->offset = offset;
2340 e->field = e->ctype = ctype;
2341 last = e;
2342 if (!e->ident_expression) {
2343 err = "invalid";
2344 break;
2346 e = e->ident_expression;
2347 } else if (e->type == EXPR_POS) {
2348 err = "internal front-end error: EXPR_POS in";
2349 break;
2350 } else
2351 return last;
2353 expression_error(e, "%s initializer", err);
2354 return NULL;
2358 * choose the next subobject to initialize.
2360 * Get designators for next element, switch old ones to EXPR_POS.
2361 * Return the resulting expression or NULL if we'd run out of subobjects.
2362 * The innermost designator is returned in *v. Designators in old
2363 * are assumed to be already sanity-checked.
2365 static struct expression *next_designators(struct expression *old,
2366 struct symbol *ctype,
2367 struct expression *e, struct expression **v)
2369 struct expression *new = NULL;
2371 if (!old)
2372 return NULL;
2373 if (old->type == EXPR_INDEX) {
2374 struct expression *copy;
2375 unsigned n;
2377 copy = next_designators(old->idx_expression,
2378 old->ctype, e, v);
2379 if (!copy) {
2380 n = old->idx_to + 1;
2381 if (array_element_offset(old->ctype->bit_size, n) == ctype->bit_size) {
2382 convert_index(old);
2383 return NULL;
2385 copy = e;
2386 *v = new = alloc_expression(e->pos, EXPR_INDEX);
2387 } else {
2388 n = old->idx_to;
2389 new = alloc_expression(e->pos, EXPR_INDEX);
2392 new->idx_from = new->idx_to = n;
2393 new->idx_expression = copy;
2394 new->ctype = old->ctype;
2395 convert_index(old);
2396 } else if (old->type == EXPR_IDENTIFIER) {
2397 struct expression *copy;
2398 struct symbol *field;
2399 int offset = 0;
2401 copy = next_designators(old->ident_expression,
2402 old->ctype, e, v);
2403 if (!copy) {
2404 field = old->field->next_subobject;
2405 if (!field) {
2406 convert_ident(old);
2407 return NULL;
2409 copy = e;
2410 *v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2412 * We can't necessarily trust "field->offset",
2413 * because the field might be in an anonymous
2414 * union, and the field offset is then the offset
2415 * within that union.
2417 * The "old->offset - old->field->offset"
2418 * would be the offset of such an anonymous
2419 * union.
2421 offset = old->offset - old->field->offset;
2422 } else {
2423 field = old->field;
2424 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2427 new->field = field;
2428 new->expr_ident = field->ident;
2429 new->ident_expression = copy;
2430 new->ctype = field;
2431 new->offset = field->offset + offset;
2432 convert_ident(old);
2434 return new;
2437 static int handle_simple_initializer(struct expression **ep, int nested,
2438 int class, struct symbol *ctype);
2441 * deal with traversing subobjects [6.7.8(17,18,20)]
2443 static void handle_list_initializer(struct expression *expr,
2444 int class, struct symbol *ctype)
2446 struct expression *e, *last = NULL, *top = NULL, *next;
2447 int jumped = 0;
2449 FOR_EACH_PTR(expr->expr_list, e) {
2450 struct expression **v;
2451 struct symbol *type;
2452 int lclass;
2454 if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2455 struct symbol *struct_sym;
2456 if (!top) {
2457 top = e;
2458 last = first_subobject(ctype, class, &top);
2459 } else {
2460 last = next_designators(last, ctype, e, &top);
2462 if (!last) {
2463 excess(e, class & TYPE_PTR ? "array" :
2464 "struct or union");
2465 DELETE_CURRENT_PTR(e);
2466 continue;
2468 struct_sym = ctype->type == SYM_NODE ? ctype->ctype.base_type : ctype;
2469 if (Wdesignated_init && struct_sym->designated_init)
2470 warning(e->pos, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2471 ctype->ident ? "in initializer for " : "",
2472 ctype->ident ? ctype->ident->len : 0,
2473 ctype->ident ? ctype->ident->name : "",
2474 ctype->ident ? ": " : "",
2475 get_type_name(struct_sym->type),
2476 show_ident(struct_sym->ident));
2477 if (jumped) {
2478 warning(e->pos, "advancing past deep designator");
2479 jumped = 0;
2481 REPLACE_CURRENT_PTR(e, last);
2482 } else {
2483 next = check_designators(e, ctype);
2484 if (!next) {
2485 DELETE_CURRENT_PTR(e);
2486 continue;
2488 top = next;
2489 /* deeper than one designator? */
2490 jumped = top != e;
2491 convert_designators(last);
2492 last = e;
2495 found:
2496 lclass = classify_type(top->ctype, &type);
2497 if (top->type == EXPR_INDEX)
2498 v = &top->idx_expression;
2499 else
2500 v = &top->ident_expression;
2502 if (handle_simple_initializer(v, 1, lclass, top->ctype))
2503 continue;
2505 if (!(lclass & TYPE_COMPOUND)) {
2506 warning(e->pos, "bogus scalar initializer");
2507 DELETE_CURRENT_PTR(e);
2508 continue;
2511 next = first_subobject(type, lclass, v);
2512 if (next) {
2513 warning(e->pos, "missing braces around initializer");
2514 top = next;
2515 goto found;
2518 DELETE_CURRENT_PTR(e);
2519 excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2521 } END_FOR_EACH_PTR(e);
2523 convert_designators(last);
2524 expr->ctype = ctype;
2527 static int is_string_literal(struct expression **v)
2529 struct expression *e = *v;
2530 while (e && e->type == EXPR_PREOP && e->op == '(')
2531 e = e->unop;
2532 if (!e || e->type != EXPR_STRING)
2533 return 0;
2534 if (e != *v && Wparen_string)
2535 warning(e->pos,
2536 "array initialized from parenthesized string constant");
2537 *v = e;
2538 return 1;
2542 * We want a normal expression, possibly in one layer of braces. Warn
2543 * if the latter happens inside a list (it's legal, but likely to be
2544 * an effect of screwup). In case of anything not legal, we are definitely
2545 * having an effect of screwup, so just fail and let the caller warn.
2547 static struct expression *handle_scalar(struct expression *e, int nested)
2549 struct expression *v = NULL, *p;
2550 int count = 0;
2552 /* normal case */
2553 if (e->type != EXPR_INITIALIZER)
2554 return e;
2556 FOR_EACH_PTR(e->expr_list, p) {
2557 if (!v)
2558 v = p;
2559 count++;
2560 } END_FOR_EACH_PTR(p);
2561 if (count != 1)
2562 return NULL;
2563 switch(v->type) {
2564 case EXPR_INITIALIZER:
2565 case EXPR_INDEX:
2566 case EXPR_IDENTIFIER:
2567 return NULL;
2568 default:
2569 break;
2571 if (nested)
2572 warning(e->pos, "braces around scalar initializer");
2573 return v;
2577 * deal with the cases that don't care about subobjects:
2578 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2579 * character array <- string literal, possibly in braces [6.7.8(14)]
2580 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2581 * compound type <- initializer list in braces [6.7.8(16)]
2582 * The last one punts to handle_list_initializer() which, in turn will call
2583 * us for individual elements of the list.
2585 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2586 * the lack of support of wide char stuff in general.
2588 * One note: we need to take care not to evaluate a string literal until
2589 * we know that we *will* handle it right here. Otherwise we would screw
2590 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2591 * { "string", ...} - we need to preserve that string literal recognizable
2592 * until we dig into the inner struct.
2594 static int handle_simple_initializer(struct expression **ep, int nested,
2595 int class, struct symbol *ctype)
2597 int is_string = is_string_type(ctype);
2598 struct expression *e = *ep, *p;
2599 struct symbol *type;
2601 if (!e)
2602 return 0;
2604 /* scalar */
2605 if (!(class & TYPE_COMPOUND)) {
2606 e = handle_scalar(e, nested);
2607 if (!e)
2608 return 0;
2609 *ep = e;
2610 if (!evaluate_expression(e))
2611 return 1;
2612 compatible_assignment_types(e, ctype, ep, "initializer");
2613 return 1;
2617 * sublist; either a string, or we dig in; the latter will deal with
2618 * pathologies, so we don't need anything fancy here.
2620 if (e->type == EXPR_INITIALIZER) {
2621 if (is_string) {
2622 struct expression *v = NULL;
2623 int count = 0;
2625 FOR_EACH_PTR(e->expr_list, p) {
2626 if (!v)
2627 v = p;
2628 count++;
2629 } END_FOR_EACH_PTR(p);
2630 if (count == 1 && is_string_literal(&v)) {
2631 *ep = e = v;
2632 goto String;
2635 handle_list_initializer(e, class, ctype);
2636 return 1;
2639 /* string */
2640 if (is_string_literal(&e)) {
2641 /* either we are doing array of char, or we'll have to dig in */
2642 if (is_string) {
2643 *ep = e;
2644 goto String;
2646 return 0;
2648 /* struct or union can be initialized by compatible */
2649 if (class != TYPE_COMPOUND)
2650 return 0;
2651 type = evaluate_expression(e);
2652 if (!type)
2653 return 0;
2654 if (ctype->type == SYM_NODE)
2655 ctype = ctype->ctype.base_type;
2656 if (type->type == SYM_NODE)
2657 type = type->ctype.base_type;
2658 if (ctype == type)
2659 return 1;
2660 return 0;
2662 String:
2663 p = alloc_expression(e->pos, EXPR_STRING);
2664 *p = *e;
2665 type = evaluate_expression(p);
2666 if (ctype->bit_size != -1) {
2667 if (ctype->bit_size + bits_in_char < type->bit_size)
2668 warning(e->pos,
2669 "too long initializer-string for array of char");
2670 else if (Winit_cstring && ctype->bit_size + bits_in_char == type->bit_size) {
2671 warning(e->pos,
2672 "too long initializer-string for array of char(no space for nul char)");
2675 *ep = p;
2676 return 1;
2679 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2681 struct symbol *type;
2682 int class = classify_type(ctype, &type);
2683 if (!handle_simple_initializer(ep, 0, class, ctype))
2684 expression_error(*ep, "invalid initializer");
2687 static struct symbol *evaluate_cast(struct expression *expr)
2689 struct expression *target = expr->cast_expression;
2690 struct symbol *ctype;
2691 struct symbol *t1, *t2;
2692 int class1, class2;
2693 int as1 = 0, as2 = 0;
2695 if (!target)
2696 return NULL;
2699 * Special case: a cast can be followed by an
2700 * initializer, in which case we need to pass
2701 * the type value down to that initializer rather
2702 * than trying to evaluate it as an expression
2704 * A more complex case is when the initializer is
2705 * dereferenced as part of a post-fix expression.
2706 * We need to produce an expression that can be dereferenced.
2708 if (target->type == EXPR_INITIALIZER) {
2709 struct symbol *sym = expr->cast_type;
2710 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2712 sym->initializer = target;
2713 evaluate_symbol(sym);
2715 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2716 addr->symbol = sym;
2718 expr->type = EXPR_PREOP;
2719 expr->op = '*';
2720 expr->unop = addr;
2721 expr->ctype = sym;
2723 return sym;
2726 ctype = examine_symbol_type(expr->cast_type);
2727 expr->ctype = ctype;
2728 expr->cast_type = ctype;
2730 evaluate_expression(target);
2731 degenerate(target);
2733 class1 = classify_type(ctype, &t1);
2735 /* cast to non-integer type -> not an integer constant expression */
2736 if (!is_int(class1))
2737 expr->flags = 0;
2738 /* if argument turns out to be not an integer constant expression *and*
2739 it was not a floating literal to start with -> too bad */
2740 else if (expr->flags == Int_const_expr &&
2741 !(target->flags & Int_const_expr))
2742 expr->flags = 0;
2744 * You can always throw a value away by casting to
2745 * "void" - that's an implicit "force". Note that
2746 * the same is _not_ true of "void *".
2748 if (t1 == &void_ctype)
2749 goto out;
2751 if (class1 & (TYPE_COMPOUND | TYPE_FN))
2752 warning(expr->pos, "cast to non-scalar");
2754 t2 = target->ctype;
2755 if (!t2) {
2756 expression_error(expr, "cast from unknown type");
2757 goto out;
2759 class2 = classify_type(t2, &t2);
2761 if (class2 & TYPE_COMPOUND)
2762 warning(expr->pos, "cast from non-scalar");
2764 if (expr->type == EXPR_FORCE_CAST)
2765 goto out;
2767 /* allowed cast unfouls */
2768 if (class2 & TYPE_FOULED)
2769 t2 = unfoul(t2);
2771 if (t1 != t2) {
2772 if (class1 & TYPE_RESTRICT)
2773 warning(expr->pos, "cast to %s",
2774 show_typename(t1));
2775 if (class2 & TYPE_RESTRICT)
2776 warning(expr->pos, "cast from %s",
2777 show_typename(t2));
2780 if (t1 == &ulong_ctype)
2781 as1 = -1;
2782 else if (class1 == TYPE_PTR) {
2783 examine_pointer_target(t1);
2784 as1 = t1->ctype.as;
2787 if (t2 == &ulong_ctype)
2788 as2 = -1;
2789 else if (class2 == TYPE_PTR) {
2790 examine_pointer_target(t2);
2791 as2 = t2->ctype.as;
2794 if (!as1 && as2 > 0)
2795 warning(expr->pos, "cast removes address space of expression");
2796 if (as1 > 0 && as2 > 0 && as1 != as2)
2797 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2798 if (as1 > 0 && !as2 &&
2799 !is_null_pointer_constant(target) && Wcast_to_as)
2800 warning(expr->pos,
2801 "cast adds address space to expression (<asn:%d>)", as1);
2803 if (!(t1->ctype.modifiers & MOD_PTRINHERIT) && class1 == TYPE_PTR &&
2804 !as1 && (target->flags & Int_const_expr)) {
2805 if (t1->ctype.base_type == &void_ctype) {
2806 if (is_zero_constant(target)) {
2807 /* NULL */
2808 expr->type = EXPR_VALUE;
2809 expr->ctype = &null_ctype;
2810 expr->value = 0;
2811 return ctype;
2815 out:
2816 return ctype;
2820 * Evaluate a call expression with a symbol. This
2821 * should expand inline functions, and evaluate
2822 * builtins.
2824 static int evaluate_symbol_call(struct expression *expr)
2826 struct expression *fn = expr->fn;
2827 struct symbol *ctype = fn->ctype;
2829 if (fn->type != EXPR_PREOP)
2830 return 0;
2832 if (ctype->op && ctype->op->evaluate)
2833 return ctype->op->evaluate(expr);
2835 if (ctype->ctype.modifiers & MOD_INLINE) {
2836 int ret;
2837 struct symbol *curr = current_fn;
2839 if (ctype->definition)
2840 ctype = ctype->definition;
2842 current_fn = ctype->ctype.base_type;
2844 ret = inline_function(expr, ctype);
2846 /* restore the old function */
2847 current_fn = curr;
2848 return ret;
2851 return 0;
2854 static struct symbol *evaluate_call(struct expression *expr)
2856 int args, fnargs;
2857 struct symbol *ctype, *sym;
2858 struct expression *fn = expr->fn;
2859 struct expression_list *arglist = expr->args;
2861 if (!evaluate_expression(fn))
2862 return NULL;
2863 sym = ctype = fn->ctype;
2864 if (ctype->type == SYM_NODE)
2865 ctype = ctype->ctype.base_type;
2866 if (ctype->type == SYM_PTR)
2867 ctype = get_base_type(ctype);
2869 if (ctype->type != SYM_FN) {
2870 struct expression *arg;
2871 expression_error(expr, "not a function %s",
2872 show_ident(sym->ident));
2873 /* do typechecking in arguments */
2874 FOR_EACH_PTR (arglist, arg) {
2875 evaluate_expression(arg);
2876 } END_FOR_EACH_PTR(arg);
2877 return NULL;
2880 examine_fn_arguments(ctype);
2881 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
2882 sym->op && sym->op->args) {
2883 if (!sym->op->args(expr))
2884 return NULL;
2885 } else {
2886 if (!evaluate_arguments(ctype, arglist))
2887 return NULL;
2888 args = expression_list_size(expr->args);
2889 fnargs = symbol_list_size(ctype->arguments);
2890 if (args < fnargs)
2891 expression_error(expr,
2892 "not enough arguments for function %s",
2893 show_ident(sym->ident));
2894 if (args > fnargs && !ctype->variadic)
2895 expression_error(expr,
2896 "too many arguments for function %s",
2897 show_ident(sym->ident));
2899 if (sym->type == SYM_NODE) {
2900 if (evaluate_symbol_call(expr))
2901 return expr->ctype;
2903 expr->ctype = ctype->ctype.base_type;
2904 return expr->ctype;
2907 static struct symbol *evaluate_offsetof(struct expression *expr)
2909 struct expression *e = expr->down;
2910 struct symbol *ctype = expr->in;
2911 int class;
2913 if (expr->op == '.') {
2914 struct symbol *field;
2915 int offset = 0;
2916 if (!ctype) {
2917 expression_error(expr, "expected structure or union");
2918 return NULL;
2920 examine_symbol_type(ctype);
2921 class = classify_type(ctype, &ctype);
2922 if (class != TYPE_COMPOUND) {
2923 expression_error(expr, "expected structure or union");
2924 return NULL;
2927 field = find_identifier(expr->ident, ctype->symbol_list, &offset);
2928 if (!field) {
2929 expression_error(expr, "unknown member");
2930 return NULL;
2932 ctype = field;
2933 expr->type = EXPR_VALUE;
2934 expr->flags = Int_const_expr;
2935 expr->value = offset;
2936 expr->taint = 0;
2937 expr->ctype = size_t_ctype;
2938 } else {
2939 if (!ctype) {
2940 expression_error(expr, "expected structure or union");
2941 return NULL;
2943 examine_symbol_type(ctype);
2944 class = classify_type(ctype, &ctype);
2945 if (class != (TYPE_COMPOUND | TYPE_PTR)) {
2946 expression_error(expr, "expected array");
2947 return NULL;
2949 ctype = ctype->ctype.base_type;
2950 if (!expr->index) {
2951 expr->type = EXPR_VALUE;
2952 expr->flags = Int_const_expr;
2953 expr->value = 0;
2954 expr->taint = 0;
2955 expr->ctype = size_t_ctype;
2956 } else {
2957 struct expression *idx = expr->index, *m;
2958 struct symbol *i_type = evaluate_expression(idx);
2959 int i_class = classify_type(i_type, &i_type);
2960 if (!is_int(i_class)) {
2961 expression_error(expr, "non-integer index");
2962 return NULL;
2964 unrestrict(idx, i_class, &i_type);
2965 idx = cast_to(idx, size_t_ctype);
2966 m = alloc_const_expression(expr->pos,
2967 bits_to_bytes(ctype->bit_size));
2968 m->ctype = size_t_ctype;
2969 m->flags = Int_const_expr;
2970 expr->type = EXPR_BINOP;
2971 expr->left = idx;
2972 expr->right = m;
2973 expr->op = '*';
2974 expr->ctype = size_t_ctype;
2975 expr->flags = m->flags & idx->flags & Int_const_expr;
2978 if (e) {
2979 struct expression *copy = __alloc_expression(0);
2980 *copy = *expr;
2981 if (e->type == EXPR_OFFSETOF)
2982 e->in = ctype;
2983 if (!evaluate_expression(e))
2984 return NULL;
2985 expr->type = EXPR_BINOP;
2986 expr->flags = e->flags & copy->flags & Int_const_expr;
2987 expr->op = '+';
2988 expr->ctype = size_t_ctype;
2989 expr->left = copy;
2990 expr->right = e;
2992 return size_t_ctype;
2995 struct symbol *evaluate_expression(struct expression *expr)
2997 if (!expr)
2998 return NULL;
2999 if (expr->ctype)
3000 return expr->ctype;
3002 switch (expr->type) {
3003 case EXPR_VALUE:
3004 case EXPR_FVALUE:
3005 expression_error(expr, "value expression without a type");
3006 return NULL;
3007 case EXPR_STRING:
3008 return evaluate_string(expr);
3009 case EXPR_SYMBOL:
3010 return evaluate_symbol_expression(expr);
3011 case EXPR_BINOP:
3012 if (!evaluate_expression(expr->left))
3013 return NULL;
3014 if (!evaluate_expression(expr->right))
3015 return NULL;
3016 return evaluate_binop(expr);
3017 case EXPR_LOGICAL:
3018 return evaluate_logical(expr);
3019 case EXPR_COMMA:
3020 evaluate_expression(expr->left);
3021 if (!evaluate_expression(expr->right))
3022 return NULL;
3023 return evaluate_comma(expr);
3024 case EXPR_COMPARE:
3025 if (!evaluate_expression(expr->left))
3026 return NULL;
3027 if (!evaluate_expression(expr->right))
3028 return NULL;
3029 return evaluate_compare(expr);
3030 case EXPR_ASSIGNMENT:
3031 if (!evaluate_expression(expr->left))
3032 return NULL;
3033 if (!evaluate_expression(expr->right))
3034 return NULL;
3035 return evaluate_assignment(expr);
3036 case EXPR_PREOP:
3037 if (!evaluate_expression(expr->unop))
3038 return NULL;
3039 return evaluate_preop(expr);
3040 case EXPR_POSTOP:
3041 if (!evaluate_expression(expr->unop))
3042 return NULL;
3043 return evaluate_postop(expr);
3044 case EXPR_CAST:
3045 case EXPR_FORCE_CAST:
3046 case EXPR_IMPLIED_CAST:
3047 return evaluate_cast(expr);
3048 case EXPR_SIZEOF:
3049 return evaluate_sizeof(expr);
3050 case EXPR_PTRSIZEOF:
3051 return evaluate_ptrsizeof(expr);
3052 case EXPR_ALIGNOF:
3053 return evaluate_alignof(expr);
3054 case EXPR_DEREF:
3055 return evaluate_member_dereference(expr);
3056 case EXPR_CALL:
3057 return evaluate_call(expr);
3058 case EXPR_SELECT:
3059 case EXPR_CONDITIONAL:
3060 return evaluate_conditional_expression(expr);
3061 case EXPR_STATEMENT:
3062 expr->ctype = evaluate_statement(expr->statement);
3063 return expr->ctype;
3065 case EXPR_LABEL:
3066 expr->ctype = &ptr_ctype;
3067 return &ptr_ctype;
3069 case EXPR_TYPE:
3070 /* Evaluate the type of the symbol .. */
3071 evaluate_symbol(expr->symbol);
3072 /* .. but the type of the _expression_ is a "type" */
3073 expr->ctype = &type_ctype;
3074 return &type_ctype;
3076 case EXPR_OFFSETOF:
3077 return evaluate_offsetof(expr);
3079 /* These can not exist as stand-alone expressions */
3080 case EXPR_INITIALIZER:
3081 case EXPR_IDENTIFIER:
3082 case EXPR_INDEX:
3083 case EXPR_POS:
3084 expression_error(expr, "internal front-end error: initializer in expression");
3085 return NULL;
3086 case EXPR_SLICE:
3087 expression_error(expr, "internal front-end error: SLICE re-evaluated");
3088 return NULL;
3090 return NULL;
3093 static void check_duplicates(struct symbol *sym)
3095 int declared = 0;
3096 struct symbol *next = sym;
3097 int initialized = sym->initializer != NULL;
3099 while ((next = next->same_symbol) != NULL) {
3100 const char *typediff;
3101 evaluate_symbol(next);
3102 if (initialized && next->initializer) {
3103 sparse_error(sym->pos, "symbol '%s' has multiple initializers (originally initialized at %s:%d)",
3104 show_ident(sym->ident),
3105 stream_name(next->pos.stream), next->pos.line);
3106 /* Only warn once */
3107 initialized = 0;
3109 declared++;
3110 typediff = type_difference(&sym->ctype, &next->ctype, 0, 0);
3111 if (typediff) {
3112 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
3113 show_ident(sym->ident),
3114 stream_name(next->pos.stream), next->pos.line, typediff);
3115 return;
3118 if (!declared) {
3119 unsigned long mod = sym->ctype.modifiers;
3120 if (mod & (MOD_STATIC | MOD_REGISTER))
3121 return;
3122 if (!(mod & MOD_TOPLEVEL))
3123 return;
3124 if (!Wdecl)
3125 return;
3126 if (sym->ident == &main_ident)
3127 return;
3128 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
3132 static struct symbol *evaluate_symbol(struct symbol *sym)
3134 struct symbol *base_type;
3136 if (!sym)
3137 return sym;
3138 if (sym->evaluated)
3139 return sym;
3140 sym->evaluated = 1;
3142 sym = examine_symbol_type(sym);
3143 base_type = get_base_type(sym);
3144 if (!base_type)
3145 return NULL;
3147 /* Evaluate the initializers */
3148 if (sym->initializer)
3149 evaluate_initializer(sym, &sym->initializer);
3151 /* And finally, evaluate the body of the symbol too */
3152 if (base_type->type == SYM_FN) {
3153 struct symbol *curr = current_fn;
3155 if (sym->definition && sym->definition != sym)
3156 return evaluate_symbol(sym->definition);
3158 current_fn = base_type;
3160 examine_fn_arguments(base_type);
3161 if (!base_type->stmt && base_type->inline_stmt)
3162 uninline(sym);
3163 if (base_type->stmt)
3164 evaluate_statement(base_type->stmt);
3166 current_fn = curr;
3169 return base_type;
3172 void evaluate_symbol_list(struct symbol_list *list)
3174 struct symbol *sym;
3176 FOR_EACH_PTR(list, sym) {
3177 evaluate_symbol(sym);
3178 check_duplicates(sym);
3179 } END_FOR_EACH_PTR(sym);
3182 static struct symbol *evaluate_return_expression(struct statement *stmt)
3184 struct expression *expr = stmt->expression;
3185 struct symbol *fntype;
3187 evaluate_expression(expr);
3188 fntype = current_fn->ctype.base_type;
3189 if (!fntype || fntype == &void_ctype) {
3190 if (expr && expr->ctype != &void_ctype)
3191 expression_error(expr, "return expression in %s function", fntype?"void":"typeless");
3192 if (expr && Wreturn_void)
3193 warning(stmt->pos, "returning void-valued expression");
3194 return NULL;
3197 if (!expr) {
3198 sparse_error(stmt->pos, "return with no return value");
3199 return NULL;
3201 if (!expr->ctype)
3202 return NULL;
3203 compatible_assignment_types(expr, fntype, &stmt->expression, "return expression");
3204 return NULL;
3207 static void evaluate_if_statement(struct statement *stmt)
3209 if (!stmt->if_conditional)
3210 return;
3212 evaluate_conditional(stmt->if_conditional, 0);
3213 evaluate_statement(stmt->if_true);
3214 evaluate_statement(stmt->if_false);
3217 static void evaluate_iterator(struct statement *stmt)
3219 evaluate_symbol_list(stmt->iterator_syms);
3220 evaluate_conditional(stmt->iterator_pre_condition, 1);
3221 evaluate_conditional(stmt->iterator_post_condition,1);
3222 evaluate_statement(stmt->iterator_pre_statement);
3223 evaluate_statement(stmt->iterator_statement);
3224 evaluate_statement(stmt->iterator_post_statement);
3227 static void verify_output_constraint(struct expression *expr, const char *constraint)
3229 switch (*constraint) {
3230 case '=': /* Assignment */
3231 case '+': /* Update */
3232 break;
3233 default:
3234 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
3238 static void verify_input_constraint(struct expression *expr, const char *constraint)
3240 switch (*constraint) {
3241 case '=': /* Assignment */
3242 case '+': /* Update */
3243 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
3247 static void evaluate_asm_statement(struct statement *stmt)
3249 struct expression *expr;
3250 struct symbol *sym;
3251 int state;
3253 expr = stmt->asm_string;
3254 if (!expr || expr->type != EXPR_STRING) {
3255 sparse_error(stmt->pos, "need constant string for inline asm");
3256 return;
3259 state = 0;
3260 FOR_EACH_PTR(stmt->asm_outputs, expr) {
3261 switch (state) {
3262 case 0: /* Identifier */
3263 state = 1;
3264 continue;
3266 case 1: /* Constraint */
3267 state = 2;
3268 if (!expr || expr->type != EXPR_STRING) {
3269 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
3270 *THIS_ADDRESS(expr) = NULL;
3271 continue;
3273 verify_output_constraint(expr, expr->string->data);
3274 continue;
3276 case 2: /* Expression */
3277 state = 0;
3278 if (!evaluate_expression(expr))
3279 return;
3280 if (!lvalue_expression(expr))
3281 warning(expr->pos, "asm output is not an lvalue");
3282 evaluate_assign_to(expr, expr->ctype);
3283 continue;
3285 } END_FOR_EACH_PTR(expr);
3287 state = 0;
3288 FOR_EACH_PTR(stmt->asm_inputs, expr) {
3289 switch (state) {
3290 case 0: /* Identifier */
3291 state = 1;
3292 continue;
3294 case 1: /* Constraint */
3295 state = 2;
3296 if (!expr || expr->type != EXPR_STRING) {
3297 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
3298 *THIS_ADDRESS(expr) = NULL;
3299 continue;
3301 verify_input_constraint(expr, expr->string->data);
3302 continue;
3304 case 2: /* Expression */
3305 state = 0;
3306 if (!evaluate_expression(expr))
3307 return;
3308 continue;
3310 } END_FOR_EACH_PTR(expr);
3312 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
3313 if (!expr) {
3314 sparse_error(stmt->pos, "bad asm clobbers");
3315 return;
3317 if (expr->type == EXPR_STRING)
3318 continue;
3319 expression_error(expr, "asm clobber is not a string");
3320 } END_FOR_EACH_PTR(expr);
3322 FOR_EACH_PTR(stmt->asm_labels, sym) {
3323 if (!sym || sym->type != SYM_LABEL) {
3324 sparse_error(stmt->pos, "bad asm label");
3325 return;
3327 } END_FOR_EACH_PTR(sym);
3330 static void evaluate_case_statement(struct statement *stmt)
3332 evaluate_expression(stmt->case_expression);
3333 evaluate_expression(stmt->case_to);
3334 evaluate_statement(stmt->case_statement);
3337 static void check_case_type(struct expression *switch_expr,
3338 struct expression *case_expr,
3339 struct expression **enumcase)
3341 struct symbol *switch_type, *case_type;
3342 int sclass, cclass;
3344 if (!case_expr)
3345 return;
3347 switch_type = switch_expr->ctype;
3348 case_type = evaluate_expression(case_expr);
3350 if (!switch_type || !case_type)
3351 goto Bad;
3352 if (enumcase) {
3353 if (*enumcase)
3354 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
3355 else if (is_enum_type(case_type))
3356 *enumcase = case_expr;
3359 sclass = classify_type(switch_type, &switch_type);
3360 cclass = classify_type(case_type, &case_type);
3362 /* both should be arithmetic */
3363 if (!(sclass & cclass & TYPE_NUM))
3364 goto Bad;
3366 /* neither should be floating */
3367 if ((sclass | cclass) & TYPE_FLOAT)
3368 goto Bad;
3370 /* if neither is restricted, we are OK */
3371 if (!((sclass | cclass) & TYPE_RESTRICT))
3372 return;
3374 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
3375 cclass, sclass, case_type, switch_type)) {
3376 unrestrict(case_expr, cclass, &case_type);
3377 unrestrict(switch_expr, sclass, &switch_type);
3379 return;
3381 Bad:
3382 expression_error(case_expr, "incompatible types for 'case' statement");
3385 static void evaluate_switch_statement(struct statement *stmt)
3387 struct symbol *sym;
3388 struct expression *enumcase = NULL;
3389 struct expression **enumcase_holder = &enumcase;
3390 struct expression *sel = stmt->switch_expression;
3392 evaluate_expression(sel);
3393 evaluate_statement(stmt->switch_statement);
3394 if (!sel)
3395 return;
3396 if (sel->ctype && is_enum_type(sel->ctype))
3397 enumcase_holder = NULL; /* Only check cases against switch */
3399 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
3400 struct statement *case_stmt = sym->stmt;
3401 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
3402 check_case_type(sel, case_stmt->case_to, enumcase_holder);
3403 } END_FOR_EACH_PTR(sym);
3406 static void evaluate_goto_statement(struct statement *stmt)
3408 struct symbol *label = stmt->goto_label;
3410 if (label && !label->stmt && !lookup_keyword(label->ident, NS_KEYWORD))
3411 sparse_error(stmt->pos, "label '%s' was not declared", show_ident(label->ident));
3413 evaluate_expression(stmt->goto_expression);
3416 struct symbol *evaluate_statement(struct statement *stmt)
3418 if (!stmt)
3419 return NULL;
3421 switch (stmt->type) {
3422 case STMT_DECLARATION: {
3423 struct symbol *s;
3424 FOR_EACH_PTR(stmt->declaration, s) {
3425 evaluate_symbol(s);
3426 } END_FOR_EACH_PTR(s);
3427 return NULL;
3430 case STMT_RETURN:
3431 return evaluate_return_expression(stmt);
3433 case STMT_EXPRESSION:
3434 if (!evaluate_expression(stmt->expression))
3435 return NULL;
3436 if (stmt->expression->ctype == &null_ctype)
3437 stmt->expression = cast_to(stmt->expression, &ptr_ctype);
3438 return degenerate(stmt->expression);
3440 case STMT_COMPOUND: {
3441 struct statement *s;
3442 struct symbol *type = NULL;
3444 /* Evaluate the return symbol in the compound statement */
3445 evaluate_symbol(stmt->ret);
3448 * Then, evaluate each statement, making the type of the
3449 * compound statement be the type of the last statement
3451 type = evaluate_statement(stmt->args);
3452 FOR_EACH_PTR(stmt->stmts, s) {
3453 type = evaluate_statement(s);
3454 } END_FOR_EACH_PTR(s);
3455 if (!type)
3456 type = &void_ctype;
3457 return type;
3459 case STMT_IF:
3460 evaluate_if_statement(stmt);
3461 return NULL;
3462 case STMT_ITERATOR:
3463 evaluate_iterator(stmt);
3464 return NULL;
3465 case STMT_SWITCH:
3466 evaluate_switch_statement(stmt);
3467 return NULL;
3468 case STMT_CASE:
3469 evaluate_case_statement(stmt);
3470 return NULL;
3471 case STMT_LABEL:
3472 return evaluate_statement(stmt->label_statement);
3473 case STMT_GOTO:
3474 evaluate_goto_statement(stmt);
3475 return NULL;
3476 case STMT_NONE:
3477 break;
3478 case STMT_ASM:
3479 evaluate_asm_statement(stmt);
3480 return NULL;
3481 case STMT_CONTEXT:
3482 evaluate_expression(stmt->expression);
3483 return NULL;
3484 case STMT_RANGE:
3485 evaluate_expression(stmt->range_expression);
3486 evaluate_expression(stmt->range_low);
3487 evaluate_expression(stmt->range_high);
3488 return NULL;
3490 return NULL;