llvm: remove unneeded OP_COPY support
[smatch.git] / evaluate.c
blob010a4c3654353dce46c93c5cd00d535994a94763
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 usual;
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 usual:
1283 target = usual_conversions(op, expr->left, expr->right,
1284 tclass, sclass, target, source);
1285 Cast:
1286 expr->right = cast_to(expr->right, target);
1287 return 1;
1290 static int whitelist_pointers(struct symbol *t1, struct symbol *t2)
1292 if (t1 == t2)
1293 return 0; /* yes, 0 - we don't want a cast_to here */
1294 if (t1 == &void_ctype)
1295 return 1;
1296 if (t2 == &void_ctype)
1297 return 1;
1298 if (classify_type(t1, &t1) != TYPE_NUM)
1299 return 0;
1300 if (classify_type(t2, &t2) != TYPE_NUM)
1301 return 0;
1302 if (t1 == t2)
1303 return 1;
1304 if (t1->ctype.modifiers & t2->ctype.modifiers & MOD_CHAR)
1305 return 1;
1306 if ((t1->ctype.modifiers ^ t2->ctype.modifiers) & MOD_SIZE)
1307 return 0;
1308 return !Wtypesign;
1311 static int check_assignment_types(struct symbol *target, struct expression **rp,
1312 const char **typediff)
1314 struct symbol *source = degenerate(*rp);
1315 struct symbol *t, *s;
1316 int tclass = classify_type(target, &t);
1317 int sclass = classify_type(source, &s);
1319 if (tclass & sclass & TYPE_NUM) {
1320 if (tclass & TYPE_RESTRICT) {
1321 /* allowed assignments unfoul */
1322 if (sclass & TYPE_FOULED && unfoul(s) == t)
1323 goto Cast;
1324 if (!restricted_value(*rp, target))
1325 return 1;
1326 if (s == t)
1327 return 1;
1328 } else if (!(sclass & TYPE_RESTRICT))
1329 goto Cast;
1330 *typediff = "different base types";
1331 return 0;
1334 if (tclass == TYPE_PTR) {
1335 unsigned long mod1, mod2;
1336 struct symbol *b1, *b2;
1337 // NULL pointer is always OK
1338 int is_null = is_null_pointer_constant(*rp);
1339 if (is_null) {
1340 if (is_null == 2)
1341 bad_null(*rp);
1342 goto Cast;
1344 if (!(sclass & TYPE_PTR)) {
1345 *typediff = "different base types";
1346 return 0;
1348 b1 = examine_pointer_target(t);
1349 b2 = examine_pointer_target(s);
1350 mod1 = target_qualifiers(t);
1351 mod2 = target_qualifiers(s);
1352 if (whitelist_pointers(b1, b2)) {
1354 * assignments to/from void * are OK, provided that
1355 * we do not remove qualifiers from pointed to [C]
1356 * or mix address spaces [sparse].
1358 if (t->ctype.as != s->ctype.as) {
1359 *typediff = "different address spaces";
1360 return 0;
1363 * If this is a function pointer assignment, it is
1364 * actually fine to assign a pointer to const data to
1365 * it, as a function pointer points to const data
1366 * implicitly, i.e., dereferencing it does not produce
1367 * an lvalue.
1369 if (b1->type == SYM_FN)
1370 mod1 |= MOD_CONST;
1371 if (mod2 & ~mod1) {
1372 *typediff = "different modifiers";
1373 return 0;
1375 goto Cast;
1377 /* It's OK if the target is more volatile or const than the source */
1378 *typediff = type_difference(&t->ctype, &s->ctype, 0, mod1);
1379 if (*typediff)
1380 return 0;
1381 return 1;
1384 if ((tclass & TYPE_COMPOUND) && s == t)
1385 return 1;
1387 if (tclass & TYPE_NUM) {
1388 /* XXX: need to turn into comparison with NULL */
1389 if (t == &bool_ctype && (sclass & TYPE_PTR))
1390 goto Cast;
1391 *typediff = "different base types";
1392 return 0;
1394 *typediff = "invalid types";
1395 return 0;
1397 Cast:
1398 *rp = cast_to(*rp, target);
1399 return 1;
1402 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1403 struct expression **rp, const char *where)
1405 const char *typediff;
1406 struct symbol *source = degenerate(*rp);
1408 if (!check_assignment_types(target, rp, &typediff)) {
1409 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1410 info(expr->pos, " expected %s", show_typename(target));
1411 info(expr->pos, " got %s", show_typename(source));
1412 *rp = cast_to(*rp, target);
1413 return 0;
1416 return 1;
1419 static int compatible_transparent_union(struct symbol *target,
1420 struct expression **rp)
1422 struct symbol *t, *member;
1423 classify_type(target, &t);
1424 if (t->type != SYM_UNION || !t->transparent_union)
1425 return 0;
1427 FOR_EACH_PTR(t->symbol_list, member) {
1428 const char *typediff;
1429 if (check_assignment_types(member, rp, &typediff))
1430 return 1;
1431 } END_FOR_EACH_PTR(member);
1433 return 0;
1436 static int compatible_argument_type(struct expression *expr, struct symbol *target,
1437 struct expression **rp, const char *where)
1439 if (compatible_transparent_union(target, rp))
1440 return 1;
1442 return compatible_assignment_types(expr, target, rp, where);
1445 static void mark_assigned(struct expression *expr)
1447 struct symbol *sym;
1449 if (!expr)
1450 return;
1451 switch (expr->type) {
1452 case EXPR_SYMBOL:
1453 sym = expr->symbol;
1454 if (!sym)
1455 return;
1456 if (sym->type != SYM_NODE)
1457 return;
1458 sym->ctype.modifiers |= MOD_ASSIGNED;
1459 return;
1461 case EXPR_BINOP:
1462 mark_assigned(expr->left);
1463 mark_assigned(expr->right);
1464 return;
1465 case EXPR_CAST:
1466 case EXPR_FORCE_CAST:
1467 mark_assigned(expr->cast_expression);
1468 return;
1469 case EXPR_SLICE:
1470 mark_assigned(expr->base);
1471 return;
1472 default:
1473 /* Hmm? */
1474 return;
1478 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1480 if (type->ctype.modifiers & MOD_CONST)
1481 expression_error(left, "assignment to const expression");
1483 /* We know left is an lvalue, so it's a "preop-*" */
1484 mark_assigned(left->unop);
1487 static struct symbol *evaluate_assignment(struct expression *expr)
1489 struct expression *left = expr->left;
1490 struct expression *where = expr;
1491 struct symbol *ltype;
1493 if (!lvalue_expression(left)) {
1494 expression_error(expr, "not an lvalue");
1495 return NULL;
1498 ltype = left->ctype;
1500 if (expr->op != '=') {
1501 if (!evaluate_assign_op(expr))
1502 return NULL;
1503 } else {
1504 if (!compatible_assignment_types(where, ltype, &expr->right, "assignment"))
1505 return NULL;
1508 evaluate_assign_to(left, ltype);
1510 expr->ctype = ltype;
1511 return ltype;
1514 static void examine_fn_arguments(struct symbol *fn)
1516 struct symbol *s;
1518 FOR_EACH_PTR(fn->arguments, s) {
1519 struct symbol *arg = evaluate_symbol(s);
1520 /* Array/function arguments silently degenerate into pointers */
1521 if (arg) {
1522 struct symbol *ptr;
1523 switch(arg->type) {
1524 case SYM_ARRAY:
1525 case SYM_FN:
1526 ptr = alloc_symbol(s->pos, SYM_PTR);
1527 if (arg->type == SYM_ARRAY)
1528 ptr->ctype = arg->ctype;
1529 else
1530 ptr->ctype.base_type = arg;
1531 ptr->ctype.as |= s->ctype.as;
1532 ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1534 s->ctype.base_type = ptr;
1535 s->ctype.as = 0;
1536 s->ctype.modifiers &= ~MOD_PTRINHERIT;
1537 s->bit_size = 0;
1538 s->examined = 0;
1539 examine_symbol_type(s);
1540 break;
1541 default:
1542 /* nothing */
1543 break;
1546 } END_FOR_EACH_PTR(s);
1549 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1551 /* Take the modifiers of the pointer, and apply them to the member */
1552 mod |= sym->ctype.modifiers;
1553 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1554 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1555 *newsym = *sym;
1556 newsym->ctype.as = as;
1557 newsym->ctype.modifiers = mod;
1558 sym = newsym;
1560 return sym;
1563 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1565 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1566 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1568 node->ctype.base_type = ptr;
1569 ptr->bit_size = bits_in_pointer;
1570 ptr->ctype.alignment = pointer_alignment;
1572 node->bit_size = bits_in_pointer;
1573 node->ctype.alignment = pointer_alignment;
1575 access_symbol(sym);
1576 if (sym->ctype.modifiers & MOD_REGISTER) {
1577 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1578 sym->ctype.modifiers &= ~MOD_REGISTER;
1580 if (sym->type == SYM_NODE) {
1581 ptr->ctype.as |= sym->ctype.as;
1582 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1583 sym = sym->ctype.base_type;
1585 if (degenerate && sym->type == SYM_ARRAY) {
1586 ptr->ctype.as |= sym->ctype.as;
1587 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1588 sym = sym->ctype.base_type;
1590 ptr->ctype.base_type = sym;
1592 return node;
1595 /* Arrays degenerate into pointers on pointer arithmetic */
1596 static struct symbol *degenerate(struct expression *expr)
1598 struct symbol *ctype, *base;
1600 if (!expr)
1601 return NULL;
1602 ctype = expr->ctype;
1603 if (!ctype)
1604 return NULL;
1605 base = examine_symbol_type(ctype);
1606 if (ctype->type == SYM_NODE)
1607 base = ctype->ctype.base_type;
1609 * Arrays degenerate into pointers to the entries, while
1610 * functions degenerate into pointers to themselves.
1611 * If array was part of non-lvalue compound, we create a copy
1612 * of that compound first and then act as if we were dealing with
1613 * the corresponding field in there.
1615 switch (base->type) {
1616 case SYM_ARRAY:
1617 if (expr->type == EXPR_SLICE) {
1618 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1619 struct expression *e0, *e1, *e2, *e3, *e4;
1621 a->ctype.base_type = expr->base->ctype;
1622 a->bit_size = expr->base->ctype->bit_size;
1623 a->array_size = expr->base->ctype->array_size;
1625 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1626 e0->symbol = a;
1627 e0->ctype = &lazy_ptr_ctype;
1629 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1630 e1->unop = e0;
1631 e1->op = '*';
1632 e1->ctype = expr->base->ctype; /* XXX */
1634 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1635 e2->left = e1;
1636 e2->right = expr->base;
1637 e2->op = '=';
1638 e2->ctype = expr->base->ctype;
1640 if (expr->r_bitpos) {
1641 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1642 e3->op = '+';
1643 e3->left = e0;
1644 e3->right = alloc_const_expression(expr->pos,
1645 bits_to_bytes(expr->r_bitpos));
1646 e3->ctype = &lazy_ptr_ctype;
1647 } else {
1648 e3 = e0;
1651 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1652 e4->left = e2;
1653 e4->right = e3;
1654 e4->ctype = &lazy_ptr_ctype;
1656 expr->unop = e4;
1657 expr->type = EXPR_PREOP;
1658 expr->op = '*';
1660 case SYM_FN:
1661 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1662 expression_error(expr, "strange non-value function or array");
1663 return &bad_ctype;
1665 *expr = *expr->unop;
1666 ctype = create_pointer(expr, ctype, 1);
1667 expr->ctype = ctype;
1668 default:
1669 /* nothing */;
1671 return ctype;
1674 static struct symbol *evaluate_addressof(struct expression *expr)
1676 struct expression *op = expr->unop;
1677 struct symbol *ctype;
1679 if (op->op != '*' || op->type != EXPR_PREOP) {
1680 expression_error(expr, "not addressable");
1681 return NULL;
1683 ctype = op->ctype;
1684 *expr = *op->unop;
1685 expr->flags = 0;
1687 if (expr->type == EXPR_SYMBOL) {
1688 struct symbol *sym = expr->symbol;
1689 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1693 * symbol expression evaluation is lazy about the type
1694 * of the sub-expression, so we may have to generate
1695 * the type here if so..
1697 if (expr->ctype == &lazy_ptr_ctype) {
1698 ctype = create_pointer(expr, ctype, 0);
1699 expr->ctype = ctype;
1701 return expr->ctype;
1705 static struct symbol *evaluate_dereference(struct expression *expr)
1707 struct expression *op = expr->unop;
1708 struct symbol *ctype = op->ctype, *node, *target;
1710 /* Simplify: *&(expr) => (expr) */
1711 if (op->type == EXPR_PREOP && op->op == '&') {
1712 *expr = *op->unop;
1713 expr->flags = 0;
1714 return expr->ctype;
1717 /* Dereferencing a node drops all the node information. */
1718 if (ctype->type == SYM_NODE)
1719 ctype = ctype->ctype.base_type;
1721 node = alloc_symbol(expr->pos, SYM_NODE);
1722 target = ctype->ctype.base_type;
1724 switch (ctype->type) {
1725 default:
1726 expression_error(expr, "cannot dereference this type");
1727 return NULL;
1728 case SYM_PTR:
1729 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1730 merge_type(node, ctype);
1731 break;
1733 case SYM_ARRAY:
1734 if (!lvalue_expression(op)) {
1735 expression_error(op, "non-lvalue array??");
1736 return NULL;
1739 /* Do the implied "addressof" on the array */
1740 *op = *op->unop;
1743 * When an array is dereferenced, we need to pick
1744 * up the attributes of the original node too..
1746 merge_type(node, op->ctype);
1747 merge_type(node, ctype);
1748 break;
1751 node->bit_size = target->bit_size;
1752 node->array_size = target->array_size;
1754 expr->ctype = node;
1755 return node;
1759 * Unary post-ops: x++ and x--
1761 static struct symbol *evaluate_postop(struct expression *expr)
1763 struct expression *op = expr->unop;
1764 struct symbol *ctype = op->ctype;
1765 int class = classify_type(ctype, &ctype);
1766 int multiply = 0;
1768 if (!class || class & TYPE_COMPOUND) {
1769 expression_error(expr, "need scalar for ++/--");
1770 return NULL;
1772 if (!lvalue_expression(expr->unop)) {
1773 expression_error(expr, "need lvalue expression for ++/--");
1774 return NULL;
1777 if ((class & TYPE_RESTRICT) && restricted_unop(expr->op, &ctype))
1778 unrestrict(expr, class, &ctype);
1780 if (class & TYPE_NUM) {
1781 multiply = 1;
1782 } else if (class == TYPE_PTR) {
1783 struct symbol *target = examine_pointer_target(ctype);
1784 if (!is_function(target))
1785 multiply = bits_to_bytes(target->bit_size);
1788 if (multiply) {
1789 evaluate_assign_to(op, op->ctype);
1790 expr->op_value = multiply;
1791 expr->ctype = ctype;
1792 return ctype;
1795 expression_error(expr, "bad argument type for ++/--");
1796 return NULL;
1799 static struct symbol *evaluate_sign(struct expression *expr)
1801 struct symbol *ctype = expr->unop->ctype;
1802 int class = classify_type(ctype, &ctype);
1803 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1804 expr->flags = 0;
1805 /* should be an arithmetic type */
1806 if (!(class & TYPE_NUM))
1807 return bad_expr_type(expr);
1808 if (class & TYPE_RESTRICT)
1809 goto Restr;
1810 Normal:
1811 if (!(class & TYPE_FLOAT)) {
1812 ctype = integer_promotion(ctype);
1813 expr->unop = cast_to(expr->unop, ctype);
1814 } else if (expr->op != '~') {
1815 /* no conversions needed */
1816 } else {
1817 return bad_expr_type(expr);
1819 if (expr->op == '+')
1820 *expr = *expr->unop;
1821 expr->ctype = ctype;
1822 return ctype;
1823 Restr:
1824 if (restricted_unop(expr->op, &ctype))
1825 unrestrict(expr, class, &ctype);
1826 goto Normal;
1829 static struct symbol *evaluate_preop(struct expression *expr)
1831 struct symbol *ctype = expr->unop->ctype;
1833 switch (expr->op) {
1834 case '(':
1835 *expr = *expr->unop;
1836 return ctype;
1838 case '+':
1839 case '-':
1840 case '~':
1841 return evaluate_sign(expr);
1843 case '*':
1844 return evaluate_dereference(expr);
1846 case '&':
1847 return evaluate_addressof(expr);
1849 case SPECIAL_INCREMENT:
1850 case SPECIAL_DECREMENT:
1852 * From a type evaluation standpoint the preops are
1853 * the same as the postops
1855 return evaluate_postop(expr);
1857 case '!':
1858 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1859 expr->flags = 0;
1860 if (is_safe_type(ctype))
1861 warning(expr->pos, "testing a 'safe expression'");
1862 if (is_float_type(ctype)) {
1863 struct expression *arg = expr->unop;
1864 expr->type = EXPR_COMPARE;
1865 expr->op = SPECIAL_EQUAL;
1866 expr->left = arg;
1867 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1868 expr->right->ctype = ctype;
1869 expr->right->fvalue = 0;
1870 } else if (is_fouled_type(ctype)) {
1871 warning(expr->pos, "%s degrades to integer",
1872 show_typename(ctype->ctype.base_type));
1874 /* the result is int [6.5.3.3(5)]*/
1875 ctype = &int_ctype;
1876 break;
1878 default:
1879 break;
1881 expr->ctype = ctype;
1882 return ctype;
1885 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1887 struct ptr_list *head = (struct ptr_list *)_list;
1888 struct ptr_list *list = head;
1890 if (!head)
1891 return NULL;
1892 do {
1893 int i;
1894 for (i = 0; i < list->nr; i++) {
1895 struct symbol *sym = (struct symbol *) list->list[i];
1896 if (sym->ident) {
1897 if (sym->ident != ident)
1898 continue;
1899 *offset = sym->offset;
1900 return sym;
1901 } else {
1902 struct symbol *ctype = sym->ctype.base_type;
1903 struct symbol *sub;
1904 if (!ctype)
1905 continue;
1906 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1907 continue;
1908 sub = find_identifier(ident, ctype->symbol_list, offset);
1909 if (!sub)
1910 continue;
1911 *offset += sym->offset;
1912 return sub;
1915 } while ((list = list->next) != head);
1916 return NULL;
1919 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1921 struct expression *add;
1924 * Create a new add-expression
1926 * NOTE! Even if we just add zero, we need a new node
1927 * for the member pointer, since it has a different
1928 * type than the original pointer. We could make that
1929 * be just a cast, but the fact is, a node is a node,
1930 * so we might as well just do the "add zero" here.
1932 add = alloc_expression(expr->pos, EXPR_BINOP);
1933 add->op = '+';
1934 add->left = expr;
1935 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1936 add->right->ctype = &int_ctype;
1937 add->right->value = offset;
1940 * The ctype of the pointer will be lazily evaluated if
1941 * we ever take the address of this member dereference..
1943 add->ctype = &lazy_ptr_ctype;
1944 return add;
1947 /* structure/union dereference */
1948 static struct symbol *evaluate_member_dereference(struct expression *expr)
1950 int offset;
1951 struct symbol *ctype, *member;
1952 struct expression *deref = expr->deref, *add;
1953 struct ident *ident = expr->member;
1954 unsigned int mod;
1955 int address_space;
1957 if (!evaluate_expression(deref))
1958 return NULL;
1959 if (!ident) {
1960 expression_error(expr, "bad member name");
1961 return NULL;
1964 ctype = deref->ctype;
1965 examine_symbol_type(ctype);
1966 address_space = ctype->ctype.as;
1967 mod = ctype->ctype.modifiers;
1968 if (ctype->type == SYM_NODE) {
1969 ctype = ctype->ctype.base_type;
1970 address_space |= ctype->ctype.as;
1971 mod |= ctype->ctype.modifiers;
1973 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1974 expression_error(expr, "expected structure or union");
1975 return NULL;
1977 offset = 0;
1978 member = find_identifier(ident, ctype->symbol_list, &offset);
1979 if (!member) {
1980 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1981 const char *name = "<unnamed>";
1982 int namelen = 9;
1983 if (ctype->ident) {
1984 name = ctype->ident->name;
1985 namelen = ctype->ident->len;
1987 if (ctype->symbol_list)
1988 expression_error(expr, "no member '%s' in %s %.*s",
1989 show_ident(ident), type, namelen, name);
1990 else
1991 expression_error(expr, "using member '%s' in "
1992 "incomplete %s %.*s", show_ident(ident),
1993 type, namelen, name);
1994 return NULL;
1998 * The member needs to take on the address space and modifiers of
1999 * the "parent" type.
2001 member = convert_to_as_mod(member, address_space, mod);
2002 ctype = get_base_type(member);
2004 if (!lvalue_expression(deref)) {
2005 if (deref->type != EXPR_SLICE) {
2006 expr->base = deref;
2007 expr->r_bitpos = 0;
2008 } else {
2009 expr->base = deref->base;
2010 expr->r_bitpos = deref->r_bitpos;
2012 expr->r_bitpos += bytes_to_bits(offset);
2013 expr->type = EXPR_SLICE;
2014 expr->r_nrbits = member->bit_size;
2015 expr->r_bitpos += member->bit_offset;
2016 expr->ctype = member;
2017 return member;
2020 deref = deref->unop;
2021 expr->deref = deref;
2023 add = evaluate_offset(deref, offset);
2024 expr->type = EXPR_PREOP;
2025 expr->op = '*';
2026 expr->unop = add;
2028 expr->ctype = member;
2029 return member;
2032 static int is_promoted(struct expression *expr)
2034 while (1) {
2035 switch (expr->type) {
2036 case EXPR_BINOP:
2037 case EXPR_SELECT:
2038 case EXPR_CONDITIONAL:
2039 return 1;
2040 case EXPR_COMMA:
2041 expr = expr->right;
2042 continue;
2043 case EXPR_PREOP:
2044 switch (expr->op) {
2045 case '(':
2046 expr = expr->unop;
2047 continue;
2048 case '+':
2049 case '-':
2050 case '~':
2051 return 1;
2052 default:
2053 return 0;
2055 default:
2056 return 0;
2062 static struct symbol *evaluate_cast(struct expression *);
2064 static struct symbol *evaluate_type_information(struct expression *expr)
2066 struct symbol *sym = expr->cast_type;
2067 if (!sym) {
2068 sym = evaluate_expression(expr->cast_expression);
2069 if (!sym)
2070 return NULL;
2072 * Expressions of restricted types will possibly get
2073 * promoted - check that here
2075 if (is_restricted_type(sym)) {
2076 if (sym->bit_size < bits_in_int && is_promoted(expr))
2077 sym = &int_ctype;
2078 } else if (is_fouled_type(sym)) {
2079 sym = &int_ctype;
2082 examine_symbol_type(sym);
2083 if (is_bitfield_type(sym)) {
2084 expression_error(expr, "trying to examine bitfield type");
2085 return NULL;
2087 return sym;
2090 static struct symbol *evaluate_sizeof(struct expression *expr)
2092 struct symbol *type;
2093 int size;
2095 type = evaluate_type_information(expr);
2096 if (!type)
2097 return NULL;
2099 size = type->bit_size;
2101 if (size < 0 && is_void_type(type)) {
2102 warning(expr->pos, "expression using sizeof(void)");
2103 size = bits_in_char;
2106 if (size == 1 && is_bool_type(type)) {
2107 if (Wsizeof_bool)
2108 warning(expr->pos, "expression using sizeof bool");
2109 size = bits_in_char;
2112 if (is_function(type->ctype.base_type)) {
2113 warning(expr->pos, "expression using sizeof on a function");
2114 size = bits_in_char;
2117 if ((size < 0) || (size & (bits_in_char - 1)))
2118 expression_error(expr, "cannot size expression");
2120 expr->type = EXPR_VALUE;
2121 expr->value = bits_to_bytes(size);
2122 expr->taint = 0;
2123 expr->ctype = size_t_ctype;
2124 return size_t_ctype;
2127 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
2129 struct symbol *type;
2130 int size;
2132 type = evaluate_type_information(expr);
2133 if (!type)
2134 return NULL;
2136 if (type->type == SYM_NODE)
2137 type = type->ctype.base_type;
2138 if (!type)
2139 return NULL;
2140 switch (type->type) {
2141 case SYM_ARRAY:
2142 break;
2143 case SYM_PTR:
2144 type = get_base_type(type);
2145 if (type)
2146 break;
2147 default:
2148 expression_error(expr, "expected pointer expression");
2149 return NULL;
2151 size = type->bit_size;
2152 if (size & (bits_in_char-1))
2153 size = 0;
2154 expr->type = EXPR_VALUE;
2155 expr->value = bits_to_bytes(size);
2156 expr->taint = 0;
2157 expr->ctype = size_t_ctype;
2158 return size_t_ctype;
2161 static struct symbol *evaluate_alignof(struct expression *expr)
2163 struct symbol *type;
2165 type = evaluate_type_information(expr);
2166 if (!type)
2167 return NULL;
2169 expr->type = EXPR_VALUE;
2170 expr->value = type->ctype.alignment;
2171 expr->taint = 0;
2172 expr->ctype = size_t_ctype;
2173 return size_t_ctype;
2176 static int evaluate_arguments(struct symbol *fn, struct expression_list *head)
2178 struct expression *expr;
2179 struct symbol_list *argument_types = fn->arguments;
2180 struct symbol *argtype;
2181 int i = 1;
2183 PREPARE_PTR_LIST(argument_types, argtype);
2184 FOR_EACH_PTR (head, expr) {
2185 struct expression **p = THIS_ADDRESS(expr);
2186 struct symbol *ctype, *target;
2187 ctype = evaluate_expression(expr);
2189 if (!ctype)
2190 return 0;
2192 target = argtype;
2193 if (!target) {
2194 struct symbol *type;
2195 int class = classify_type(ctype, &type);
2196 if (is_int(class)) {
2197 *p = cast_to(expr, integer_promotion(type));
2198 } else if (class & TYPE_FLOAT) {
2199 unsigned long mod = type->ctype.modifiers;
2200 if (!(mod & (MOD_LONG_ALL)))
2201 *p = cast_to(expr, &double_ctype);
2202 } else if (class & TYPE_PTR) {
2203 if (expr->ctype == &null_ctype)
2204 *p = cast_to(expr, &ptr_ctype);
2205 else
2206 degenerate(expr);
2208 } else if (!target->forced_arg){
2209 static char where[30];
2210 examine_symbol_type(target);
2211 sprintf(where, "argument %d", i);
2212 compatible_argument_type(expr, target, p, where);
2215 i++;
2216 NEXT_PTR_LIST(argtype);
2217 } END_FOR_EACH_PTR(expr);
2218 FINISH_PTR_LIST(argtype);
2219 return 1;
2222 static void convert_index(struct expression *e)
2224 struct expression *child = e->idx_expression;
2225 unsigned from = e->idx_from;
2226 unsigned to = e->idx_to + 1;
2227 e->type = EXPR_POS;
2228 e->init_offset = from * bits_to_bytes(e->ctype->bit_size);
2229 e->init_nr = to - from;
2230 e->init_expr = child;
2233 static void convert_ident(struct expression *e)
2235 struct expression *child = e->ident_expression;
2236 int offset = e->offset;
2238 e->type = EXPR_POS;
2239 e->init_offset = offset;
2240 e->init_nr = 1;
2241 e->init_expr = child;
2244 static void convert_designators(struct expression *e)
2246 while (e) {
2247 if (e->type == EXPR_INDEX)
2248 convert_index(e);
2249 else if (e->type == EXPR_IDENTIFIER)
2250 convert_ident(e);
2251 else
2252 break;
2253 e = e->init_expr;
2257 static void excess(struct expression *e, const char *s)
2259 warning(e->pos, "excessive elements in %s initializer", s);
2263 * implicit designator for the first element
2265 static struct expression *first_subobject(struct symbol *ctype, int class,
2266 struct expression **v)
2268 struct expression *e = *v, *new;
2270 if (ctype->type == SYM_NODE)
2271 ctype = ctype->ctype.base_type;
2273 if (class & TYPE_PTR) { /* array */
2274 if (!ctype->bit_size)
2275 return NULL;
2276 new = alloc_expression(e->pos, EXPR_INDEX);
2277 new->idx_expression = e;
2278 new->ctype = ctype->ctype.base_type;
2279 } else {
2280 struct symbol *field, *p;
2281 PREPARE_PTR_LIST(ctype->symbol_list, p);
2282 while (p && !p->ident && is_bitfield_type(p))
2283 NEXT_PTR_LIST(p);
2284 field = p;
2285 FINISH_PTR_LIST(p);
2286 if (!field)
2287 return NULL;
2288 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2289 new->ident_expression = e;
2290 new->field = new->ctype = field;
2291 new->offset = field->offset;
2293 *v = new;
2294 return new;
2298 * sanity-check explicit designators; return the innermost one or NULL
2299 * in case of error. Assign types.
2301 static struct expression *check_designators(struct expression *e,
2302 struct symbol *ctype)
2304 struct expression *last = NULL;
2305 const char *err;
2306 while (1) {
2307 if (ctype->type == SYM_NODE)
2308 ctype = ctype->ctype.base_type;
2309 if (e->type == EXPR_INDEX) {
2310 struct symbol *type;
2311 if (ctype->type != SYM_ARRAY) {
2312 err = "array index in non-array";
2313 break;
2315 type = ctype->ctype.base_type;
2316 if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2317 unsigned offset = array_element_offset(type->bit_size, e->idx_to);
2318 if (offset >= ctype->bit_size) {
2319 err = "index out of bounds in";
2320 break;
2323 e->ctype = ctype = type;
2324 ctype = type;
2325 last = e;
2326 if (!e->idx_expression) {
2327 err = "invalid";
2328 break;
2330 e = e->idx_expression;
2331 } else if (e->type == EXPR_IDENTIFIER) {
2332 int offset = 0;
2333 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2334 err = "field name not in struct or union";
2335 break;
2337 ctype = find_identifier(e->expr_ident, ctype->symbol_list, &offset);
2338 if (!ctype) {
2339 err = "unknown field name in";
2340 break;
2342 e->offset = offset;
2343 e->field = e->ctype = ctype;
2344 last = e;
2345 if (!e->ident_expression) {
2346 err = "invalid";
2347 break;
2349 e = e->ident_expression;
2350 } else if (e->type == EXPR_POS) {
2351 err = "internal front-end error: EXPR_POS in";
2352 break;
2353 } else
2354 return last;
2356 expression_error(e, "%s initializer", err);
2357 return NULL;
2361 * choose the next subobject to initialize.
2363 * Get designators for next element, switch old ones to EXPR_POS.
2364 * Return the resulting expression or NULL if we'd run out of subobjects.
2365 * The innermost designator is returned in *v. Designators in old
2366 * are assumed to be already sanity-checked.
2368 static struct expression *next_designators(struct expression *old,
2369 struct symbol *ctype,
2370 struct expression *e, struct expression **v)
2372 struct expression *new = NULL;
2374 if (!old)
2375 return NULL;
2376 if (old->type == EXPR_INDEX) {
2377 struct expression *copy;
2378 unsigned n;
2380 copy = next_designators(old->idx_expression,
2381 old->ctype, e, v);
2382 if (!copy) {
2383 n = old->idx_to + 1;
2384 if (array_element_offset(old->ctype->bit_size, n) == ctype->bit_size) {
2385 convert_index(old);
2386 return NULL;
2388 copy = e;
2389 *v = new = alloc_expression(e->pos, EXPR_INDEX);
2390 } else {
2391 n = old->idx_to;
2392 new = alloc_expression(e->pos, EXPR_INDEX);
2395 new->idx_from = new->idx_to = n;
2396 new->idx_expression = copy;
2397 new->ctype = old->ctype;
2398 convert_index(old);
2399 } else if (old->type == EXPR_IDENTIFIER) {
2400 struct expression *copy;
2401 struct symbol *field;
2402 int offset = 0;
2404 copy = next_designators(old->ident_expression,
2405 old->ctype, e, v);
2406 if (!copy) {
2407 field = old->field->next_subobject;
2408 if (!field) {
2409 convert_ident(old);
2410 return NULL;
2412 copy = e;
2413 *v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2415 * We can't necessarily trust "field->offset",
2416 * because the field might be in an anonymous
2417 * union, and the field offset is then the offset
2418 * within that union.
2420 * The "old->offset - old->field->offset"
2421 * would be the offset of such an anonymous
2422 * union.
2424 offset = old->offset - old->field->offset;
2425 } else {
2426 field = old->field;
2427 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2430 new->field = field;
2431 new->expr_ident = field->ident;
2432 new->ident_expression = copy;
2433 new->ctype = field;
2434 new->offset = field->offset + offset;
2435 convert_ident(old);
2437 return new;
2440 static int handle_simple_initializer(struct expression **ep, int nested,
2441 int class, struct symbol *ctype);
2444 * deal with traversing subobjects [6.7.8(17,18,20)]
2446 static void handle_list_initializer(struct expression *expr,
2447 int class, struct symbol *ctype)
2449 struct expression *e, *last = NULL, *top = NULL, *next;
2450 int jumped = 0;
2452 FOR_EACH_PTR(expr->expr_list, e) {
2453 struct expression **v;
2454 struct symbol *type;
2455 int lclass;
2457 if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2458 struct symbol *struct_sym;
2459 if (!top) {
2460 top = e;
2461 last = first_subobject(ctype, class, &top);
2462 } else {
2463 last = next_designators(last, ctype, e, &top);
2465 if (!last) {
2466 excess(e, class & TYPE_PTR ? "array" :
2467 "struct or union");
2468 DELETE_CURRENT_PTR(e);
2469 continue;
2471 struct_sym = ctype->type == SYM_NODE ? ctype->ctype.base_type : ctype;
2472 if (Wdesignated_init && struct_sym->designated_init)
2473 warning(e->pos, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2474 ctype->ident ? "in initializer for " : "",
2475 ctype->ident ? ctype->ident->len : 0,
2476 ctype->ident ? ctype->ident->name : "",
2477 ctype->ident ? ": " : "",
2478 get_type_name(struct_sym->type),
2479 show_ident(struct_sym->ident));
2480 if (jumped) {
2481 warning(e->pos, "advancing past deep designator");
2482 jumped = 0;
2484 REPLACE_CURRENT_PTR(e, last);
2485 } else {
2486 next = check_designators(e, ctype);
2487 if (!next) {
2488 DELETE_CURRENT_PTR(e);
2489 continue;
2491 top = next;
2492 /* deeper than one designator? */
2493 jumped = top != e;
2494 convert_designators(last);
2495 last = e;
2498 found:
2499 lclass = classify_type(top->ctype, &type);
2500 if (top->type == EXPR_INDEX)
2501 v = &top->idx_expression;
2502 else
2503 v = &top->ident_expression;
2505 if (handle_simple_initializer(v, 1, lclass, top->ctype))
2506 continue;
2508 if (!(lclass & TYPE_COMPOUND)) {
2509 warning(e->pos, "bogus scalar initializer");
2510 DELETE_CURRENT_PTR(e);
2511 continue;
2514 next = first_subobject(type, lclass, v);
2515 if (next) {
2516 warning(e->pos, "missing braces around initializer");
2517 top = next;
2518 goto found;
2521 DELETE_CURRENT_PTR(e);
2522 excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2524 } END_FOR_EACH_PTR(e);
2526 convert_designators(last);
2527 expr->ctype = ctype;
2530 static int is_string_literal(struct expression **v)
2532 struct expression *e = *v;
2533 while (e && e->type == EXPR_PREOP && e->op == '(')
2534 e = e->unop;
2535 if (!e || e->type != EXPR_STRING)
2536 return 0;
2537 if (e != *v && Wparen_string)
2538 warning(e->pos,
2539 "array initialized from parenthesized string constant");
2540 *v = e;
2541 return 1;
2545 * We want a normal expression, possibly in one layer of braces. Warn
2546 * if the latter happens inside a list (it's legal, but likely to be
2547 * an effect of screwup). In case of anything not legal, we are definitely
2548 * having an effect of screwup, so just fail and let the caller warn.
2550 static struct expression *handle_scalar(struct expression *e, int nested)
2552 struct expression *v = NULL, *p;
2553 int count = 0;
2555 /* normal case */
2556 if (e->type != EXPR_INITIALIZER)
2557 return e;
2559 FOR_EACH_PTR(e->expr_list, p) {
2560 if (!v)
2561 v = p;
2562 count++;
2563 } END_FOR_EACH_PTR(p);
2564 if (count != 1)
2565 return NULL;
2566 switch(v->type) {
2567 case EXPR_INITIALIZER:
2568 case EXPR_INDEX:
2569 case EXPR_IDENTIFIER:
2570 return NULL;
2571 default:
2572 break;
2574 if (nested)
2575 warning(e->pos, "braces around scalar initializer");
2576 return v;
2580 * deal with the cases that don't care about subobjects:
2581 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2582 * character array <- string literal, possibly in braces [6.7.8(14)]
2583 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2584 * compound type <- initializer list in braces [6.7.8(16)]
2585 * The last one punts to handle_list_initializer() which, in turn will call
2586 * us for individual elements of the list.
2588 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2589 * the lack of support of wide char stuff in general.
2591 * One note: we need to take care not to evaluate a string literal until
2592 * we know that we *will* handle it right here. Otherwise we would screw
2593 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2594 * { "string", ...} - we need to preserve that string literal recognizable
2595 * until we dig into the inner struct.
2597 static int handle_simple_initializer(struct expression **ep, int nested,
2598 int class, struct symbol *ctype)
2600 int is_string = is_string_type(ctype);
2601 struct expression *e = *ep, *p;
2602 struct symbol *type;
2604 if (!e)
2605 return 0;
2607 /* scalar */
2608 if (!(class & TYPE_COMPOUND)) {
2609 e = handle_scalar(e, nested);
2610 if (!e)
2611 return 0;
2612 *ep = e;
2613 if (!evaluate_expression(e))
2614 return 1;
2615 compatible_assignment_types(e, ctype, ep, "initializer");
2616 return 1;
2620 * sublist; either a string, or we dig in; the latter will deal with
2621 * pathologies, so we don't need anything fancy here.
2623 if (e->type == EXPR_INITIALIZER) {
2624 if (is_string) {
2625 struct expression *v = NULL;
2626 int count = 0;
2628 FOR_EACH_PTR(e->expr_list, p) {
2629 if (!v)
2630 v = p;
2631 count++;
2632 } END_FOR_EACH_PTR(p);
2633 if (count == 1 && is_string_literal(&v)) {
2634 *ep = e = v;
2635 goto String;
2638 handle_list_initializer(e, class, ctype);
2639 return 1;
2642 /* string */
2643 if (is_string_literal(&e)) {
2644 /* either we are doing array of char, or we'll have to dig in */
2645 if (is_string) {
2646 *ep = e;
2647 goto String;
2649 return 0;
2651 /* struct or union can be initialized by compatible */
2652 if (class != TYPE_COMPOUND)
2653 return 0;
2654 type = evaluate_expression(e);
2655 if (!type)
2656 return 0;
2657 if (ctype->type == SYM_NODE)
2658 ctype = ctype->ctype.base_type;
2659 if (type->type == SYM_NODE)
2660 type = type->ctype.base_type;
2661 if (ctype == type)
2662 return 1;
2663 return 0;
2665 String:
2666 p = alloc_expression(e->pos, EXPR_STRING);
2667 *p = *e;
2668 type = evaluate_expression(p);
2669 if (ctype->bit_size != -1) {
2670 if (ctype->bit_size + bits_in_char < type->bit_size)
2671 warning(e->pos,
2672 "too long initializer-string for array of char");
2673 else if (Winit_cstring && ctype->bit_size + bits_in_char == type->bit_size) {
2674 warning(e->pos,
2675 "too long initializer-string for array of char(no space for nul char)");
2678 *ep = p;
2679 return 1;
2682 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2684 struct symbol *type;
2685 int class = classify_type(ctype, &type);
2686 if (!handle_simple_initializer(ep, 0, class, ctype))
2687 expression_error(*ep, "invalid initializer");
2690 static struct symbol *evaluate_cast(struct expression *expr)
2692 struct expression *target = expr->cast_expression;
2693 struct symbol *ctype;
2694 struct symbol *t1, *t2;
2695 int class1, class2;
2696 int as1 = 0, as2 = 0;
2698 if (!target)
2699 return NULL;
2702 * Special case: a cast can be followed by an
2703 * initializer, in which case we need to pass
2704 * the type value down to that initializer rather
2705 * than trying to evaluate it as an expression
2707 * A more complex case is when the initializer is
2708 * dereferenced as part of a post-fix expression.
2709 * We need to produce an expression that can be dereferenced.
2711 if (target->type == EXPR_INITIALIZER) {
2712 struct symbol *sym = expr->cast_type;
2713 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2715 sym->initializer = target;
2716 evaluate_symbol(sym);
2718 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2719 addr->symbol = sym;
2721 expr->type = EXPR_PREOP;
2722 expr->op = '*';
2723 expr->unop = addr;
2724 expr->ctype = sym;
2726 return sym;
2729 ctype = examine_symbol_type(expr->cast_type);
2730 expr->ctype = ctype;
2731 expr->cast_type = ctype;
2733 evaluate_expression(target);
2734 degenerate(target);
2736 class1 = classify_type(ctype, &t1);
2738 /* cast to non-integer type -> not an integer constant expression */
2739 if (!is_int(class1))
2740 expr->flags = 0;
2741 /* if argument turns out to be not an integer constant expression *and*
2742 it was not a floating literal to start with -> too bad */
2743 else if (expr->flags == Int_const_expr &&
2744 !(target->flags & Int_const_expr))
2745 expr->flags = 0;
2747 * You can always throw a value away by casting to
2748 * "void" - that's an implicit "force". Note that
2749 * the same is _not_ true of "void *".
2751 if (t1 == &void_ctype)
2752 goto out;
2754 if (class1 & (TYPE_COMPOUND | TYPE_FN))
2755 warning(expr->pos, "cast to non-scalar");
2757 t2 = target->ctype;
2758 if (!t2) {
2759 expression_error(expr, "cast from unknown type");
2760 goto out;
2762 class2 = classify_type(t2, &t2);
2764 if (class2 & TYPE_COMPOUND)
2765 warning(expr->pos, "cast from non-scalar");
2767 if (expr->type == EXPR_FORCE_CAST)
2768 goto out;
2770 /* allowed cast unfouls */
2771 if (class2 & TYPE_FOULED)
2772 t2 = unfoul(t2);
2774 if (t1 != t2) {
2775 if (class1 & TYPE_RESTRICT)
2776 warning(expr->pos, "cast to %s",
2777 show_typename(t1));
2778 if (class2 & TYPE_RESTRICT)
2779 warning(expr->pos, "cast from %s",
2780 show_typename(t2));
2783 if (t1 == &ulong_ctype)
2784 as1 = -1;
2785 else if (class1 == TYPE_PTR) {
2786 examine_pointer_target(t1);
2787 as1 = t1->ctype.as;
2790 if (t2 == &ulong_ctype)
2791 as2 = -1;
2792 else if (class2 == TYPE_PTR) {
2793 examine_pointer_target(t2);
2794 as2 = t2->ctype.as;
2797 if (!as1 && as2 > 0)
2798 warning(expr->pos, "cast removes address space of expression");
2799 if (as1 > 0 && as2 > 0 && as1 != as2)
2800 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2801 if (as1 > 0 && !as2 &&
2802 !is_null_pointer_constant(target) && Wcast_to_as)
2803 warning(expr->pos,
2804 "cast adds address space to expression (<asn:%d>)", as1);
2806 if (!(t1->ctype.modifiers & MOD_PTRINHERIT) && class1 == TYPE_PTR &&
2807 !as1 && (target->flags & Int_const_expr)) {
2808 if (t1->ctype.base_type == &void_ctype) {
2809 if (is_zero_constant(target)) {
2810 /* NULL */
2811 expr->type = EXPR_VALUE;
2812 expr->ctype = &null_ctype;
2813 expr->value = 0;
2814 return ctype;
2818 out:
2819 return ctype;
2823 * Evaluate a call expression with a symbol. This
2824 * should expand inline functions, and evaluate
2825 * builtins.
2827 static int evaluate_symbol_call(struct expression *expr)
2829 struct expression *fn = expr->fn;
2830 struct symbol *ctype = fn->ctype;
2832 if (fn->type != EXPR_PREOP)
2833 return 0;
2835 if (ctype->op && ctype->op->evaluate)
2836 return ctype->op->evaluate(expr);
2838 if (ctype->ctype.modifiers & MOD_INLINE) {
2839 int ret;
2840 struct symbol *curr = current_fn;
2842 if (ctype->definition)
2843 ctype = ctype->definition;
2845 current_fn = ctype->ctype.base_type;
2847 ret = inline_function(expr, ctype);
2849 /* restore the old function */
2850 current_fn = curr;
2851 return ret;
2854 return 0;
2857 static struct symbol *evaluate_call(struct expression *expr)
2859 int args, fnargs;
2860 struct symbol *ctype, *sym;
2861 struct expression *fn = expr->fn;
2862 struct expression_list *arglist = expr->args;
2864 if (!evaluate_expression(fn))
2865 return NULL;
2866 sym = ctype = fn->ctype;
2867 if (ctype->type == SYM_NODE)
2868 ctype = ctype->ctype.base_type;
2869 if (ctype->type == SYM_PTR)
2870 ctype = get_base_type(ctype);
2872 if (ctype->type != SYM_FN) {
2873 struct expression *arg;
2874 expression_error(expr, "not a function %s",
2875 show_ident(sym->ident));
2876 /* do typechecking in arguments */
2877 FOR_EACH_PTR (arglist, arg) {
2878 evaluate_expression(arg);
2879 } END_FOR_EACH_PTR(arg);
2880 return NULL;
2883 examine_fn_arguments(ctype);
2884 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
2885 sym->op && sym->op->args) {
2886 if (!sym->op->args(expr))
2887 return NULL;
2888 } else {
2889 if (!evaluate_arguments(ctype, arglist))
2890 return NULL;
2891 args = expression_list_size(expr->args);
2892 fnargs = symbol_list_size(ctype->arguments);
2893 if (args < fnargs)
2894 expression_error(expr,
2895 "not enough arguments for function %s",
2896 show_ident(sym->ident));
2897 if (args > fnargs && !ctype->variadic)
2898 expression_error(expr,
2899 "too many arguments for function %s",
2900 show_ident(sym->ident));
2902 if (sym->type == SYM_NODE) {
2903 if (evaluate_symbol_call(expr))
2904 return expr->ctype;
2906 expr->ctype = ctype->ctype.base_type;
2907 return expr->ctype;
2910 static struct symbol *evaluate_offsetof(struct expression *expr)
2912 struct expression *e = expr->down;
2913 struct symbol *ctype = expr->in;
2914 int class;
2916 if (expr->op == '.') {
2917 struct symbol *field;
2918 int offset = 0;
2919 if (!ctype) {
2920 expression_error(expr, "expected structure or union");
2921 return NULL;
2923 examine_symbol_type(ctype);
2924 class = classify_type(ctype, &ctype);
2925 if (class != TYPE_COMPOUND) {
2926 expression_error(expr, "expected structure or union");
2927 return NULL;
2930 field = find_identifier(expr->ident, ctype->symbol_list, &offset);
2931 if (!field) {
2932 expression_error(expr, "unknown member");
2933 return NULL;
2935 ctype = field;
2936 expr->type = EXPR_VALUE;
2937 expr->flags = Int_const_expr;
2938 expr->value = offset;
2939 expr->taint = 0;
2940 expr->ctype = size_t_ctype;
2941 } else {
2942 if (!ctype) {
2943 expression_error(expr, "expected structure or union");
2944 return NULL;
2946 examine_symbol_type(ctype);
2947 class = classify_type(ctype, &ctype);
2948 if (class != (TYPE_COMPOUND | TYPE_PTR)) {
2949 expression_error(expr, "expected array");
2950 return NULL;
2952 ctype = ctype->ctype.base_type;
2953 if (!expr->index) {
2954 expr->type = EXPR_VALUE;
2955 expr->flags = Int_const_expr;
2956 expr->value = 0;
2957 expr->taint = 0;
2958 expr->ctype = size_t_ctype;
2959 } else {
2960 struct expression *idx = expr->index, *m;
2961 struct symbol *i_type = evaluate_expression(idx);
2962 int i_class = classify_type(i_type, &i_type);
2963 if (!is_int(i_class)) {
2964 expression_error(expr, "non-integer index");
2965 return NULL;
2967 unrestrict(idx, i_class, &i_type);
2968 idx = cast_to(idx, size_t_ctype);
2969 m = alloc_const_expression(expr->pos,
2970 bits_to_bytes(ctype->bit_size));
2971 m->ctype = size_t_ctype;
2972 m->flags = Int_const_expr;
2973 expr->type = EXPR_BINOP;
2974 expr->left = idx;
2975 expr->right = m;
2976 expr->op = '*';
2977 expr->ctype = size_t_ctype;
2978 expr->flags = m->flags & idx->flags & Int_const_expr;
2981 if (e) {
2982 struct expression *copy = __alloc_expression(0);
2983 *copy = *expr;
2984 if (e->type == EXPR_OFFSETOF)
2985 e->in = ctype;
2986 if (!evaluate_expression(e))
2987 return NULL;
2988 expr->type = EXPR_BINOP;
2989 expr->flags = e->flags & copy->flags & Int_const_expr;
2990 expr->op = '+';
2991 expr->ctype = size_t_ctype;
2992 expr->left = copy;
2993 expr->right = e;
2995 return size_t_ctype;
2998 struct symbol *evaluate_expression(struct expression *expr)
3000 if (!expr)
3001 return NULL;
3002 if (expr->ctype)
3003 return expr->ctype;
3005 switch (expr->type) {
3006 case EXPR_VALUE:
3007 case EXPR_FVALUE:
3008 expression_error(expr, "value expression without a type");
3009 return NULL;
3010 case EXPR_STRING:
3011 return evaluate_string(expr);
3012 case EXPR_SYMBOL:
3013 return evaluate_symbol_expression(expr);
3014 case EXPR_BINOP:
3015 if (!evaluate_expression(expr->left))
3016 return NULL;
3017 if (!evaluate_expression(expr->right))
3018 return NULL;
3019 return evaluate_binop(expr);
3020 case EXPR_LOGICAL:
3021 return evaluate_logical(expr);
3022 case EXPR_COMMA:
3023 evaluate_expression(expr->left);
3024 if (!evaluate_expression(expr->right))
3025 return NULL;
3026 return evaluate_comma(expr);
3027 case EXPR_COMPARE:
3028 if (!evaluate_expression(expr->left))
3029 return NULL;
3030 if (!evaluate_expression(expr->right))
3031 return NULL;
3032 return evaluate_compare(expr);
3033 case EXPR_ASSIGNMENT:
3034 if (!evaluate_expression(expr->left))
3035 return NULL;
3036 if (!evaluate_expression(expr->right))
3037 return NULL;
3038 return evaluate_assignment(expr);
3039 case EXPR_PREOP:
3040 if (!evaluate_expression(expr->unop))
3041 return NULL;
3042 return evaluate_preop(expr);
3043 case EXPR_POSTOP:
3044 if (!evaluate_expression(expr->unop))
3045 return NULL;
3046 return evaluate_postop(expr);
3047 case EXPR_CAST:
3048 case EXPR_FORCE_CAST:
3049 case EXPR_IMPLIED_CAST:
3050 return evaluate_cast(expr);
3051 case EXPR_SIZEOF:
3052 return evaluate_sizeof(expr);
3053 case EXPR_PTRSIZEOF:
3054 return evaluate_ptrsizeof(expr);
3055 case EXPR_ALIGNOF:
3056 return evaluate_alignof(expr);
3057 case EXPR_DEREF:
3058 return evaluate_member_dereference(expr);
3059 case EXPR_CALL:
3060 return evaluate_call(expr);
3061 case EXPR_SELECT:
3062 case EXPR_CONDITIONAL:
3063 return evaluate_conditional_expression(expr);
3064 case EXPR_STATEMENT:
3065 expr->ctype = evaluate_statement(expr->statement);
3066 return expr->ctype;
3068 case EXPR_LABEL:
3069 expr->ctype = &ptr_ctype;
3070 return &ptr_ctype;
3072 case EXPR_TYPE:
3073 /* Evaluate the type of the symbol .. */
3074 evaluate_symbol(expr->symbol);
3075 /* .. but the type of the _expression_ is a "type" */
3076 expr->ctype = &type_ctype;
3077 return &type_ctype;
3079 case EXPR_OFFSETOF:
3080 return evaluate_offsetof(expr);
3082 /* These can not exist as stand-alone expressions */
3083 case EXPR_INITIALIZER:
3084 case EXPR_IDENTIFIER:
3085 case EXPR_INDEX:
3086 case EXPR_POS:
3087 expression_error(expr, "internal front-end error: initializer in expression");
3088 return NULL;
3089 case EXPR_SLICE:
3090 expression_error(expr, "internal front-end error: SLICE re-evaluated");
3091 return NULL;
3093 return NULL;
3096 static void check_duplicates(struct symbol *sym)
3098 int declared = 0;
3099 struct symbol *next = sym;
3100 int initialized = sym->initializer != NULL;
3102 while ((next = next->same_symbol) != NULL) {
3103 const char *typediff;
3104 evaluate_symbol(next);
3105 if (initialized && next->initializer) {
3106 sparse_error(sym->pos, "symbol '%s' has multiple initializers (originally initialized at %s:%d)",
3107 show_ident(sym->ident),
3108 stream_name(next->pos.stream), next->pos.line);
3109 /* Only warn once */
3110 initialized = 0;
3112 declared++;
3113 typediff = type_difference(&sym->ctype, &next->ctype, 0, 0);
3114 if (typediff) {
3115 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
3116 show_ident(sym->ident),
3117 stream_name(next->pos.stream), next->pos.line, typediff);
3118 return;
3121 if (!declared) {
3122 unsigned long mod = sym->ctype.modifiers;
3123 if (mod & (MOD_STATIC | MOD_REGISTER))
3124 return;
3125 if (!(mod & MOD_TOPLEVEL))
3126 return;
3127 if (!Wdecl)
3128 return;
3129 if (sym->ident == &main_ident)
3130 return;
3131 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
3135 static struct symbol *evaluate_symbol(struct symbol *sym)
3137 struct symbol *base_type;
3139 if (!sym)
3140 return sym;
3141 if (sym->evaluated)
3142 return sym;
3143 sym->evaluated = 1;
3145 sym = examine_symbol_type(sym);
3146 base_type = get_base_type(sym);
3147 if (!base_type)
3148 return NULL;
3150 /* Evaluate the initializers */
3151 if (sym->initializer)
3152 evaluate_initializer(sym, &sym->initializer);
3154 /* And finally, evaluate the body of the symbol too */
3155 if (base_type->type == SYM_FN) {
3156 struct symbol *curr = current_fn;
3158 if (sym->definition && sym->definition != sym)
3159 return evaluate_symbol(sym->definition);
3161 current_fn = base_type;
3163 examine_fn_arguments(base_type);
3164 if (!base_type->stmt && base_type->inline_stmt)
3165 uninline(sym);
3166 if (base_type->stmt)
3167 evaluate_statement(base_type->stmt);
3169 current_fn = curr;
3172 return base_type;
3175 void evaluate_symbol_list(struct symbol_list *list)
3177 struct symbol *sym;
3179 FOR_EACH_PTR(list, sym) {
3180 evaluate_symbol(sym);
3181 check_duplicates(sym);
3182 } END_FOR_EACH_PTR(sym);
3185 static struct symbol *evaluate_return_expression(struct statement *stmt)
3187 struct expression *expr = stmt->expression;
3188 struct symbol *fntype;
3190 evaluate_expression(expr);
3191 fntype = current_fn->ctype.base_type;
3192 if (!fntype || fntype == &void_ctype) {
3193 if (expr && expr->ctype != &void_ctype)
3194 expression_error(expr, "return expression in %s function", fntype?"void":"typeless");
3195 if (expr && Wreturn_void)
3196 warning(stmt->pos, "returning void-valued expression");
3197 return NULL;
3200 if (!expr) {
3201 sparse_error(stmt->pos, "return with no return value");
3202 return NULL;
3204 if (!expr->ctype)
3205 return NULL;
3206 compatible_assignment_types(expr, fntype, &stmt->expression, "return expression");
3207 return NULL;
3210 static void evaluate_if_statement(struct statement *stmt)
3212 if (!stmt->if_conditional)
3213 return;
3215 evaluate_conditional(stmt->if_conditional, 0);
3216 evaluate_statement(stmt->if_true);
3217 evaluate_statement(stmt->if_false);
3220 static void evaluate_iterator(struct statement *stmt)
3222 evaluate_symbol_list(stmt->iterator_syms);
3223 evaluate_conditional(stmt->iterator_pre_condition, 1);
3224 evaluate_conditional(stmt->iterator_post_condition,1);
3225 evaluate_statement(stmt->iterator_pre_statement);
3226 evaluate_statement(stmt->iterator_statement);
3227 evaluate_statement(stmt->iterator_post_statement);
3230 static void verify_output_constraint(struct expression *expr, const char *constraint)
3232 switch (*constraint) {
3233 case '=': /* Assignment */
3234 case '+': /* Update */
3235 break;
3236 default:
3237 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
3241 static void verify_input_constraint(struct expression *expr, const char *constraint)
3243 switch (*constraint) {
3244 case '=': /* Assignment */
3245 case '+': /* Update */
3246 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
3250 static void evaluate_asm_statement(struct statement *stmt)
3252 struct expression *expr;
3253 struct symbol *sym;
3254 int state;
3256 expr = stmt->asm_string;
3257 if (!expr || expr->type != EXPR_STRING) {
3258 sparse_error(stmt->pos, "need constant string for inline asm");
3259 return;
3262 state = 0;
3263 FOR_EACH_PTR(stmt->asm_outputs, expr) {
3264 switch (state) {
3265 case 0: /* Identifier */
3266 state = 1;
3267 continue;
3269 case 1: /* Constraint */
3270 state = 2;
3271 if (!expr || expr->type != EXPR_STRING) {
3272 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
3273 *THIS_ADDRESS(expr) = NULL;
3274 continue;
3276 verify_output_constraint(expr, expr->string->data);
3277 continue;
3279 case 2: /* Expression */
3280 state = 0;
3281 if (!evaluate_expression(expr))
3282 return;
3283 if (!lvalue_expression(expr))
3284 warning(expr->pos, "asm output is not an lvalue");
3285 evaluate_assign_to(expr, expr->ctype);
3286 continue;
3288 } END_FOR_EACH_PTR(expr);
3290 state = 0;
3291 FOR_EACH_PTR(stmt->asm_inputs, expr) {
3292 switch (state) {
3293 case 0: /* Identifier */
3294 state = 1;
3295 continue;
3297 case 1: /* Constraint */
3298 state = 2;
3299 if (!expr || expr->type != EXPR_STRING) {
3300 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
3301 *THIS_ADDRESS(expr) = NULL;
3302 continue;
3304 verify_input_constraint(expr, expr->string->data);
3305 continue;
3307 case 2: /* Expression */
3308 state = 0;
3309 if (!evaluate_expression(expr))
3310 return;
3311 continue;
3313 } END_FOR_EACH_PTR(expr);
3315 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
3316 if (!expr) {
3317 sparse_error(stmt->pos, "bad asm clobbers");
3318 return;
3320 if (expr->type == EXPR_STRING)
3321 continue;
3322 expression_error(expr, "asm clobber is not a string");
3323 } END_FOR_EACH_PTR(expr);
3325 FOR_EACH_PTR(stmt->asm_labels, sym) {
3326 if (!sym || sym->type != SYM_LABEL) {
3327 sparse_error(stmt->pos, "bad asm label");
3328 return;
3330 } END_FOR_EACH_PTR(sym);
3333 static void evaluate_case_statement(struct statement *stmt)
3335 evaluate_expression(stmt->case_expression);
3336 evaluate_expression(stmt->case_to);
3337 evaluate_statement(stmt->case_statement);
3340 static void check_case_type(struct expression *switch_expr,
3341 struct expression *case_expr,
3342 struct expression **enumcase)
3344 struct symbol *switch_type, *case_type;
3345 int sclass, cclass;
3347 if (!case_expr)
3348 return;
3350 switch_type = switch_expr->ctype;
3351 case_type = evaluate_expression(case_expr);
3353 if (!switch_type || !case_type)
3354 goto Bad;
3355 if (enumcase) {
3356 if (*enumcase)
3357 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
3358 else if (is_enum_type(case_type))
3359 *enumcase = case_expr;
3362 sclass = classify_type(switch_type, &switch_type);
3363 cclass = classify_type(case_type, &case_type);
3365 /* both should be arithmetic */
3366 if (!(sclass & cclass & TYPE_NUM))
3367 goto Bad;
3369 /* neither should be floating */
3370 if ((sclass | cclass) & TYPE_FLOAT)
3371 goto Bad;
3373 /* if neither is restricted, we are OK */
3374 if (!((sclass | cclass) & TYPE_RESTRICT))
3375 return;
3377 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
3378 cclass, sclass, case_type, switch_type)) {
3379 unrestrict(case_expr, cclass, &case_type);
3380 unrestrict(switch_expr, sclass, &switch_type);
3382 return;
3384 Bad:
3385 expression_error(case_expr, "incompatible types for 'case' statement");
3388 static void evaluate_switch_statement(struct statement *stmt)
3390 struct symbol *sym;
3391 struct expression *enumcase = NULL;
3392 struct expression **enumcase_holder = &enumcase;
3393 struct expression *sel = stmt->switch_expression;
3395 evaluate_expression(sel);
3396 evaluate_statement(stmt->switch_statement);
3397 if (!sel)
3398 return;
3399 if (sel->ctype && is_enum_type(sel->ctype))
3400 enumcase_holder = NULL; /* Only check cases against switch */
3402 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
3403 struct statement *case_stmt = sym->stmt;
3404 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
3405 check_case_type(sel, case_stmt->case_to, enumcase_holder);
3406 } END_FOR_EACH_PTR(sym);
3409 static void evaluate_goto_statement(struct statement *stmt)
3411 struct symbol *label = stmt->goto_label;
3413 if (label && !label->stmt && !lookup_keyword(label->ident, NS_KEYWORD))
3414 sparse_error(stmt->pos, "label '%s' was not declared", show_ident(label->ident));
3416 evaluate_expression(stmt->goto_expression);
3419 struct symbol *evaluate_statement(struct statement *stmt)
3421 if (!stmt)
3422 return NULL;
3424 switch (stmt->type) {
3425 case STMT_DECLARATION: {
3426 struct symbol *s;
3427 FOR_EACH_PTR(stmt->declaration, s) {
3428 evaluate_symbol(s);
3429 } END_FOR_EACH_PTR(s);
3430 return NULL;
3433 case STMT_RETURN:
3434 return evaluate_return_expression(stmt);
3436 case STMT_EXPRESSION:
3437 if (!evaluate_expression(stmt->expression))
3438 return NULL;
3439 if (stmt->expression->ctype == &null_ctype)
3440 stmt->expression = cast_to(stmt->expression, &ptr_ctype);
3441 return degenerate(stmt->expression);
3443 case STMT_COMPOUND: {
3444 struct statement *s;
3445 struct symbol *type = NULL;
3447 /* Evaluate the return symbol in the compound statement */
3448 evaluate_symbol(stmt->ret);
3451 * Then, evaluate each statement, making the type of the
3452 * compound statement be the type of the last statement
3454 type = evaluate_statement(stmt->args);
3455 FOR_EACH_PTR(stmt->stmts, s) {
3456 type = evaluate_statement(s);
3457 } END_FOR_EACH_PTR(s);
3458 if (!type)
3459 type = &void_ctype;
3460 return type;
3462 case STMT_IF:
3463 evaluate_if_statement(stmt);
3464 return NULL;
3465 case STMT_ITERATOR:
3466 evaluate_iterator(stmt);
3467 return NULL;
3468 case STMT_SWITCH:
3469 evaluate_switch_statement(stmt);
3470 return NULL;
3471 case STMT_CASE:
3472 evaluate_case_statement(stmt);
3473 return NULL;
3474 case STMT_LABEL:
3475 return evaluate_statement(stmt->label_statement);
3476 case STMT_GOTO:
3477 evaluate_goto_statement(stmt);
3478 return NULL;
3479 case STMT_NONE:
3480 break;
3481 case STMT_ASM:
3482 evaluate_asm_statement(stmt);
3483 return NULL;
3484 case STMT_CONTEXT:
3485 evaluate_expression(stmt->expression);
3486 return NULL;
3487 case STMT_RANGE:
3488 evaluate_expression(stmt->range_expression);
3489 evaluate_expression(stmt->range_low);
3490 evaluate_expression(stmt->range_high);
3491 return NULL;
3493 return NULL;