Support GCC's transparent unions
[smatch.git] / evaluate.c
blob98fc52b83236c66ab8d6f3423c8ac5107005d877
1 /*
2 * sparse/evaluate.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
25 * Evaluate constant expressions.
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <stddef.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <limits.h>
37 #include "lib.h"
38 #include "allocate.h"
39 #include "parse.h"
40 #include "token.h"
41 #include "symbol.h"
42 #include "target.h"
43 #include "expression.h"
45 struct symbol *current_fn;
47 static struct symbol *degenerate(struct expression *expr);
48 static struct symbol *evaluate_symbol(struct symbol *sym);
50 static struct symbol *evaluate_symbol_expression(struct expression *expr)
52 struct expression *addr;
53 struct symbol *sym = expr->symbol;
54 struct symbol *base_type;
56 if (!sym) {
57 expression_error(expr, "undefined identifier '%s'", show_ident(expr->symbol_name));
58 return NULL;
61 examine_symbol_type(sym);
63 base_type = get_base_type(sym);
64 if (!base_type) {
65 expression_error(expr, "identifier '%s' has no type", show_ident(expr->symbol_name));
66 return NULL;
69 addr = alloc_expression(expr->pos, EXPR_SYMBOL);
70 addr->symbol = sym;
71 addr->symbol_name = expr->symbol_name;
72 addr->ctype = &lazy_ptr_ctype; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
73 expr->type = EXPR_PREOP;
74 expr->op = '*';
75 expr->unop = addr;
77 /* The type of a symbol is the symbol itself! */
78 expr->ctype = sym;
79 return sym;
82 static struct symbol *evaluate_string(struct expression *expr)
84 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
85 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
86 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
87 struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
88 unsigned int length = expr->string->length;
90 sym->array_size = alloc_const_expression(expr->pos, length);
91 sym->bit_size = bytes_to_bits(length);
92 sym->ctype.alignment = 1;
93 sym->string = 1;
94 sym->ctype.modifiers = MOD_STATIC;
95 sym->ctype.base_type = array;
96 sym->initializer = initstr;
98 initstr->ctype = sym;
99 initstr->string = expr->string;
101 array->array_size = sym->array_size;
102 array->bit_size = bytes_to_bits(length);
103 array->ctype.alignment = 1;
104 array->ctype.modifiers = MOD_STATIC;
105 array->ctype.base_type = &char_ctype;
107 addr->symbol = sym;
108 addr->ctype = &lazy_ptr_ctype;
110 expr->type = EXPR_PREOP;
111 expr->op = '*';
112 expr->unop = addr;
113 expr->ctype = sym;
114 return sym;
117 /* type has come from classify_type and is an integer type */
118 static inline struct symbol *integer_promotion(struct symbol *type)
120 struct symbol *orig_type = type;
121 unsigned long mod = type->ctype.modifiers;
122 int width = type->bit_size;
125 * Bitfields always promote to the base type,
126 * even if the bitfield might be bigger than
127 * an "int".
129 if (type->type == SYM_BITFIELD) {
130 type = type->ctype.base_type;
131 orig_type = type;
133 mod = type->ctype.modifiers;
134 if (width < bits_in_int)
135 return &int_ctype;
137 /* If char/short has as many bits as int, it still gets "promoted" */
138 if (mod & (MOD_CHAR | MOD_SHORT)) {
139 if (mod & MOD_UNSIGNED)
140 return &uint_ctype;
141 return &int_ctype;
143 return orig_type;
147 * integer part of usual arithmetic conversions:
148 * integer promotions are applied
149 * if left and right are identical, we are done
150 * if signedness is the same, convert one with lower rank
151 * unless unsigned argument has rank lower than signed one, convert the
152 * signed one.
153 * if signed argument is bigger than unsigned one, convert the unsigned.
154 * otherwise, convert signed.
156 * Leaving aside the integer promotions, that is equivalent to
157 * if identical, don't convert
158 * if left is bigger than right, convert right
159 * if right is bigger than left, convert right
160 * otherwise, if signedness is the same, convert one with lower rank
161 * otherwise convert the signed one.
163 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
165 unsigned long lmod, rmod;
167 left = integer_promotion(left);
168 right = integer_promotion(right);
170 if (left == right)
171 goto left;
173 if (left->bit_size > right->bit_size)
174 goto left;
176 if (right->bit_size > left->bit_size)
177 goto right;
179 lmod = left->ctype.modifiers;
180 rmod = right->ctype.modifiers;
181 if ((lmod ^ rmod) & MOD_UNSIGNED) {
182 if (lmod & MOD_UNSIGNED)
183 goto left;
184 } else if ((lmod & ~rmod) & (MOD_LONG_ALL))
185 goto left;
186 right:
187 left = right;
188 left:
189 return left;
192 static int same_cast_type(struct symbol *orig, struct symbol *new)
194 return orig->bit_size == new->bit_size &&
195 orig->bit_offset == new->bit_offset;
198 static struct symbol *base_type(struct symbol *node, unsigned long *modp, unsigned long *asp)
200 unsigned long mod, as;
202 mod = 0; as = 0;
203 while (node) {
204 mod |= node->ctype.modifiers;
205 as |= node->ctype.as;
206 if (node->type == SYM_NODE) {
207 node = node->ctype.base_type;
208 continue;
210 break;
212 *modp = mod & ~MOD_IGNORE;
213 *asp = as;
214 return node;
217 static int is_same_type(struct expression *expr, struct symbol *new)
219 struct symbol *old = expr->ctype;
220 unsigned long oldmod, newmod, oldas, newas;
222 old = base_type(old, &oldmod, &oldas);
223 new = base_type(new, &newmod, &newas);
225 /* Same base type, same address space? */
226 if (old == new && oldas == newas) {
227 unsigned long difmod;
229 /* Check the modifier bits. */
230 difmod = (oldmod ^ newmod) & ~MOD_NOCAST;
232 /* Exact same type? */
233 if (!difmod)
234 return 1;
237 * Not the same type, but differs only in "const".
238 * Don't warn about MOD_NOCAST.
240 if (difmod == MOD_CONST)
241 return 0;
243 if ((oldmod | newmod) & MOD_NOCAST) {
244 const char *tofrom = "to/from";
245 if (!(newmod & MOD_NOCAST))
246 tofrom = "from";
247 if (!(oldmod & MOD_NOCAST))
248 tofrom = "to";
249 warning(expr->pos, "implicit cast %s nocast type", tofrom);
251 return 0;
254 static void
255 warn_for_different_enum_types (struct position pos,
256 struct symbol *typea,
257 struct symbol *typeb)
259 if (!Wenum_mismatch)
260 return;
261 if (typea->type == SYM_NODE)
262 typea = typea->ctype.base_type;
263 if (typeb->type == SYM_NODE)
264 typeb = typeb->ctype.base_type;
266 if (typea == typeb)
267 return;
269 if (typea->type == SYM_ENUM && typeb->type == SYM_ENUM) {
270 warning(pos, "mixing different enum types");
271 info(pos, " %s versus", show_typename(typea));
272 info(pos, " %s", show_typename(typeb));
277 * This gets called for implicit casts in assignments and
278 * integer promotion. We often want to try to move the
279 * cast down, because the ops involved may have been
280 * implicitly cast up, and we can get rid of the casts
281 * early.
283 static struct expression * cast_to(struct expression *old, struct symbol *type)
285 struct expression *expr;
287 warn_for_different_enum_types (old->pos, old->ctype, type);
289 if (old->ctype != &null_ctype && is_same_type(old, type))
290 return old;
293 * See if we can simplify the op. Move the cast down.
295 switch (old->type) {
296 case EXPR_PREOP:
297 if (old->ctype->bit_size < type->bit_size)
298 break;
299 if (old->op == '~') {
300 old->ctype = type;
301 old->unop = cast_to(old->unop, type);
302 return old;
304 break;
306 case EXPR_IMPLIED_CAST:
307 warn_for_different_enum_types(old->pos, old->ctype, type);
309 if (old->ctype->bit_size >= type->bit_size) {
310 struct expression *orig = old->cast_expression;
311 if (same_cast_type(orig->ctype, type))
312 return orig;
313 if (old->ctype->bit_offset == type->bit_offset) {
314 old->ctype = type;
315 old->cast_type = type;
316 return old;
319 break;
321 default:
322 /* nothing */;
325 expr = alloc_expression(old->pos, EXPR_IMPLIED_CAST);
326 expr->flags = old->flags;
327 expr->ctype = type;
328 expr->cast_type = type;
329 expr->cast_expression = old;
330 return expr;
333 enum {
334 TYPE_NUM = 1,
335 TYPE_BITFIELD = 2,
336 TYPE_RESTRICT = 4,
337 TYPE_FLOAT = 8,
338 TYPE_PTR = 16,
339 TYPE_COMPOUND = 32,
340 TYPE_FOULED = 64,
341 TYPE_FN = 128,
344 static inline int classify_type(struct symbol *type, struct symbol **base)
346 static int type_class[SYM_BAD + 1] = {
347 [SYM_PTR] = TYPE_PTR,
348 [SYM_FN] = TYPE_PTR | TYPE_FN,
349 [SYM_ARRAY] = TYPE_PTR | TYPE_COMPOUND,
350 [SYM_STRUCT] = TYPE_COMPOUND,
351 [SYM_UNION] = TYPE_COMPOUND,
352 [SYM_BITFIELD] = TYPE_NUM | TYPE_BITFIELD,
353 [SYM_RESTRICT] = TYPE_NUM | TYPE_RESTRICT,
354 [SYM_FOULED] = TYPE_NUM | TYPE_RESTRICT | TYPE_FOULED,
356 if (type->type == SYM_NODE)
357 type = type->ctype.base_type;
358 if (type->type == SYM_TYPEOF) {
359 type = evaluate_expression(type->initializer);
360 if (!type)
361 type = &bad_ctype;
362 else if (type->type == SYM_NODE)
363 type = type->ctype.base_type;
365 if (type->type == SYM_ENUM)
366 type = type->ctype.base_type;
367 *base = type;
368 if (type->type == SYM_BASETYPE) {
369 if (type->ctype.base_type == &int_type)
370 return TYPE_NUM;
371 if (type->ctype.base_type == &fp_type)
372 return TYPE_NUM | TYPE_FLOAT;
374 return type_class[type->type];
377 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
379 static inline int is_string_type(struct symbol *type)
381 if (type->type == SYM_NODE)
382 type = type->ctype.base_type;
383 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
386 static struct symbol *bad_expr_type(struct expression *expr)
388 sparse_error(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
389 switch (expr->type) {
390 case EXPR_BINOP:
391 case EXPR_COMPARE:
392 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
393 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
394 break;
395 case EXPR_PREOP:
396 case EXPR_POSTOP:
397 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
398 break;
399 default:
400 break;
403 expr->flags = 0;
404 return expr->ctype = &bad_ctype;
407 static int restricted_value(struct expression *v, struct symbol *type)
409 if (v->type != EXPR_VALUE)
410 return 1;
411 if (v->value != 0)
412 return 1;
413 return 0;
416 static int restricted_binop(int op, struct symbol *type)
418 switch (op) {
419 case '&':
420 case '=':
421 case SPECIAL_AND_ASSIGN:
422 case SPECIAL_OR_ASSIGN:
423 case SPECIAL_XOR_ASSIGN:
424 return 1; /* unfoul */
425 case '|':
426 case '^':
427 case '?':
428 return 2; /* keep fouled */
429 case SPECIAL_EQUAL:
430 case SPECIAL_NOTEQUAL:
431 return 3; /* warn if fouled */
432 default:
433 return 0; /* warn */
437 static int restricted_unop(int op, struct symbol **type)
439 if (op == '~') {
440 if ((*type)->bit_size < bits_in_int)
441 *type = befoul(*type);
442 return 0;
443 } if (op == '+')
444 return 0;
445 return 1;
448 /* type should be SYM_FOULED */
449 static inline struct symbol *unfoul(struct symbol *type)
451 return type->ctype.base_type;
454 static struct symbol *restricted_binop_type(int op,
455 struct expression *left,
456 struct expression *right,
457 int lclass, int rclass,
458 struct symbol *ltype,
459 struct symbol *rtype)
461 struct symbol *ctype = NULL;
462 if (lclass & TYPE_RESTRICT) {
463 if (rclass & TYPE_RESTRICT) {
464 if (ltype == rtype) {
465 ctype = ltype;
466 } else if (lclass & TYPE_FOULED) {
467 if (unfoul(ltype) == rtype)
468 ctype = ltype;
469 } else if (rclass & TYPE_FOULED) {
470 if (unfoul(rtype) == ltype)
471 ctype = rtype;
473 } else {
474 if (!restricted_value(right, ltype))
475 ctype = ltype;
477 } else if (!restricted_value(left, rtype))
478 ctype = rtype;
480 if (ctype) {
481 switch (restricted_binop(op, ctype)) {
482 case 1:
483 if ((lclass ^ rclass) & TYPE_FOULED)
484 ctype = unfoul(ctype);
485 break;
486 case 3:
487 if (!(lclass & rclass & TYPE_FOULED))
488 break;
489 case 0:
490 ctype = NULL;
491 default:
492 break;
496 return ctype;
499 static inline void unrestrict(struct expression *expr,
500 int class, struct symbol **ctype)
502 if (class & TYPE_RESTRICT) {
503 if (class & TYPE_FOULED)
504 *ctype = unfoul(*ctype);
505 warning(expr->pos, "%s degrades to integer",
506 show_typename(*ctype));
507 *ctype = (*ctype)->ctype.base_type; /* get to arithmetic type */
511 static struct symbol *usual_conversions(int op,
512 struct expression *left,
513 struct expression *right,
514 int lclass, int rclass,
515 struct symbol *ltype,
516 struct symbol *rtype)
518 struct symbol *ctype;
520 warn_for_different_enum_types(right->pos, left->ctype, right->ctype);
522 if ((lclass | rclass) & TYPE_RESTRICT)
523 goto Restr;
525 Normal:
526 if (!(lclass & TYPE_FLOAT)) {
527 if (!(rclass & TYPE_FLOAT))
528 return bigger_int_type(ltype, rtype);
529 else
530 return rtype;
531 } else if (rclass & TYPE_FLOAT) {
532 unsigned long lmod = ltype->ctype.modifiers;
533 unsigned long rmod = rtype->ctype.modifiers;
534 if (rmod & ~lmod & (MOD_LONG_ALL))
535 return rtype;
536 else
537 return ltype;
538 } else
539 return ltype;
541 Restr:
542 ctype = restricted_binop_type(op, left, right,
543 lclass, rclass, ltype, rtype);
544 if (ctype)
545 return ctype;
547 unrestrict(left, lclass, &ltype);
548 unrestrict(right, rclass, &rtype);
550 goto Normal;
553 static inline int lvalue_expression(struct expression *expr)
555 return expr->type == EXPR_PREOP && expr->op == '*';
558 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *itype)
560 struct expression *index = expr->right;
561 struct symbol *ctype, *base;
562 int multiply;
564 classify_type(degenerate(expr->left), &ctype);
565 base = examine_pointer_target(ctype);
567 if (!base) {
568 expression_error(expr, "missing type information");
569 return NULL;
571 if (is_function(base)) {
572 expression_error(expr, "arithmetics on pointers to functions");
573 return NULL;
576 /* Get the size of whatever the pointer points to */
577 multiply = is_void_type(base) ? 1 : bits_to_bytes(base->bit_size);
579 if (ctype == &null_ctype)
580 ctype = &ptr_ctype;
581 expr->ctype = ctype;
583 if (multiply == 1 && itype->bit_size >= bits_in_pointer)
584 return ctype;
586 if (index->type == EXPR_VALUE) {
587 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
588 unsigned long long v = index->value, mask;
589 mask = 1ULL << (itype->bit_size - 1);
590 if (v & mask)
591 v |= -mask;
592 else
593 v &= mask - 1;
594 v *= multiply;
595 mask = 1ULL << (bits_in_pointer - 1);
596 v &= mask | (mask - 1);
597 val->value = v;
598 val->ctype = ssize_t_ctype;
599 expr->right = val;
600 return ctype;
603 if (itype->bit_size < bits_in_pointer)
604 index = cast_to(index, ssize_t_ctype);
606 if (multiply > 1) {
607 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
608 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
610 val->ctype = ssize_t_ctype;
611 val->value = multiply;
613 mul->op = '*';
614 mul->ctype = ssize_t_ctype;
615 mul->left = index;
616 mul->right = val;
617 index = mul;
620 expr->right = index;
621 return ctype;
624 static void examine_fn_arguments(struct symbol *fn);
626 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
628 const char *type_difference(struct ctype *c1, struct ctype *c2,
629 unsigned long mod1, unsigned long mod2)
631 unsigned long as1 = c1->as, as2 = c2->as;
632 struct symbol *t1 = c1->base_type;
633 struct symbol *t2 = c2->base_type;
634 int move1 = 1, move2 = 1;
635 mod1 |= c1->modifiers;
636 mod2 |= c2->modifiers;
637 for (;;) {
638 unsigned long diff;
639 int type;
640 struct symbol *base1 = t1->ctype.base_type;
641 struct symbol *base2 = t2->ctype.base_type;
644 * FIXME! Collect alignment and context too here!
646 if (move1) {
647 if (t1 && t1->type != SYM_PTR) {
648 mod1 |= t1->ctype.modifiers;
649 as1 |= t1->ctype.as;
651 move1 = 0;
654 if (move2) {
655 if (t2 && t2->type != SYM_PTR) {
656 mod2 |= t2->ctype.modifiers;
657 as2 |= t2->ctype.as;
659 move2 = 0;
662 if (t1 == t2)
663 break;
664 if (!t1 || !t2)
665 return "different types";
667 if (t1->type == SYM_NODE || t1->type == SYM_ENUM) {
668 t1 = base1;
669 move1 = 1;
670 if (!t1)
671 return "bad types";
672 continue;
675 if (t2->type == SYM_NODE || t2->type == SYM_ENUM) {
676 t2 = base2;
677 move2 = 1;
678 if (!t2)
679 return "bad types";
680 continue;
683 move1 = move2 = 1;
684 type = t1->type;
685 if (type != t2->type)
686 return "different base types";
688 switch (type) {
689 default:
690 sparse_error(t1->pos,
691 "internal error: bad type in derived(%d)",
692 type);
693 return "bad types";
694 case SYM_RESTRICT:
695 return "different base types";
696 case SYM_UNION:
697 case SYM_STRUCT:
698 /* allow definition of incomplete structs and unions */
699 if (t1->ident == t2->ident)
700 return NULL;
701 return "different base types";
702 case SYM_ARRAY:
703 /* XXX: we ought to compare sizes */
704 break;
705 case SYM_PTR:
706 if (as1 != as2)
707 return "different address spaces";
708 /* MOD_SPECIFIER is due to idiocy in parse.c */
709 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SPECIFIER)
710 return "different modifiers";
711 /* we could be lazier here */
712 base1 = examine_pointer_target(t1);
713 base2 = examine_pointer_target(t2);
714 mod1 = t1->ctype.modifiers;
715 as1 = t1->ctype.as;
716 mod2 = t2->ctype.modifiers;
717 as2 = t2->ctype.as;
718 break;
719 case SYM_FN: {
720 struct symbol *arg1, *arg2;
721 int i;
723 if (as1 != as2)
724 return "different address spaces";
725 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
726 return "different modifiers";
727 mod1 = t1->ctype.modifiers;
728 as1 = t1->ctype.as;
729 mod2 = t2->ctype.modifiers;
730 as2 = t2->ctype.as;
732 if (base1->variadic != base2->variadic)
733 return "incompatible variadic arguments";
734 examine_fn_arguments(t1);
735 examine_fn_arguments(t2);
736 PREPARE_PTR_LIST(t1->arguments, arg1);
737 PREPARE_PTR_LIST(t2->arguments, arg2);
738 i = 1;
739 for (;;) {
740 const char *diffstr;
741 if (!arg1 && !arg2)
742 break;
743 if (!arg1 || !arg2)
744 return "different argument counts";
745 diffstr = type_difference(&arg1->ctype,
746 &arg2->ctype,
747 MOD_IGN, MOD_IGN);
748 if (diffstr) {
749 static char argdiff[80];
750 sprintf(argdiff, "incompatible argument %d (%s)", i, diffstr);
751 return argdiff;
753 NEXT_PTR_LIST(arg1);
754 NEXT_PTR_LIST(arg2);
755 i++;
757 FINISH_PTR_LIST(arg2);
758 FINISH_PTR_LIST(arg1);
759 break;
761 case SYM_BASETYPE:
762 if (as1 != as2)
763 return "different address spaces";
764 if (base1 != base2)
765 return "different base types";
766 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
767 if (!diff)
768 return NULL;
769 if (diff & MOD_SIZE)
770 return "different type sizes";
771 else if (diff & ~MOD_SIGNEDNESS)
772 return "different modifiers";
773 else
774 return "different signedness";
776 t1 = base1;
777 t2 = base2;
779 if (as1 != as2)
780 return "different address spaces";
781 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
782 return "different modifiers";
783 return NULL;
786 static void bad_null(struct expression *expr)
788 if (Wnon_pointer_null)
789 warning(expr->pos, "Using plain integer as NULL pointer");
792 static unsigned long target_qualifiers(struct symbol *type)
794 unsigned long mod = type->ctype.modifiers & MOD_IGN;
795 if (type->ctype.base_type && type->ctype.base_type->type == SYM_ARRAY)
796 mod = 0;
797 return mod;
800 static struct symbol *evaluate_ptr_sub(struct expression *expr)
802 const char *typediff;
803 struct symbol *ltype, *rtype;
804 struct expression *l = expr->left;
805 struct expression *r = expr->right;
806 struct symbol *lbase;
808 classify_type(degenerate(l), &ltype);
809 classify_type(degenerate(r), &rtype);
811 lbase = examine_pointer_target(ltype);
812 examine_pointer_target(rtype);
813 typediff = type_difference(&ltype->ctype, &rtype->ctype,
814 target_qualifiers(rtype),
815 target_qualifiers(ltype));
816 if (typediff)
817 expression_error(expr, "subtraction of different types can't work (%s)", typediff);
819 if (is_function(lbase)) {
820 expression_error(expr, "subtraction of functions? Share your drugs");
821 return NULL;
824 expr->ctype = ssize_t_ctype;
825 if (lbase->bit_size > bits_in_char) {
826 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
827 struct expression *div = expr;
828 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
829 unsigned long value = bits_to_bytes(lbase->bit_size);
831 val->ctype = size_t_ctype;
832 val->value = value;
834 if (value & (value-1)) {
835 if (Wptr_subtraction_blows)
836 warning(expr->pos, "potentially expensive pointer subtraction");
839 sub->op = '-';
840 sub->ctype = ssize_t_ctype;
841 sub->left = l;
842 sub->right = r;
844 div->op = '/';
845 div->left = sub;
846 div->right = val;
849 return ssize_t_ctype;
852 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
854 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
856 struct symbol *ctype;
858 if (!expr)
859 return NULL;
861 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
862 warning(expr->pos, "assignment expression in conditional");
864 ctype = evaluate_expression(expr);
865 if (ctype) {
866 if (is_safe_type(ctype))
867 warning(expr->pos, "testing a 'safe expression'");
870 return ctype;
873 static struct symbol *evaluate_logical(struct expression *expr)
875 if (!evaluate_conditional(expr->left, 0))
876 return NULL;
877 if (!evaluate_conditional(expr->right, 0))
878 return NULL;
880 /* the result is int [6.5.13(3), 6.5.14(3)] */
881 expr->ctype = &int_ctype;
882 if (expr->flags) {
883 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
884 expr->flags = 0;
886 return &int_ctype;
889 static struct symbol *evaluate_binop(struct expression *expr)
891 struct symbol *ltype, *rtype, *ctype;
892 int lclass = classify_type(expr->left->ctype, &ltype);
893 int rclass = classify_type(expr->right->ctype, &rtype);
894 int op = expr->op;
896 if (expr->flags) {
897 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
898 expr->flags = 0;
901 /* number op number */
902 if (lclass & rclass & TYPE_NUM) {
903 if ((lclass | rclass) & TYPE_FLOAT) {
904 switch (op) {
905 case '+': case '-': case '*': case '/':
906 break;
907 default:
908 return bad_expr_type(expr);
912 if (op == SPECIAL_LEFTSHIFT || op == SPECIAL_RIGHTSHIFT) {
913 // shifts do integer promotions, but that's it.
914 unrestrict(expr->left, lclass, &ltype);
915 unrestrict(expr->right, rclass, &rtype);
916 ctype = ltype = integer_promotion(ltype);
917 rtype = integer_promotion(rtype);
918 } else {
919 // The rest do usual conversions
920 const unsigned left_not = expr->left->type == EXPR_PREOP
921 && expr->left->op == '!';
922 const unsigned right_not = expr->right->type == EXPR_PREOP
923 && expr->right->op == '!';
924 if ((op == '&' || op == '|') && (left_not || right_not))
925 warning(expr->pos, "dubious: %sx %c %sy",
926 left_not ? "!" : "",
928 right_not ? "!" : "");
930 ltype = usual_conversions(op, expr->left, expr->right,
931 lclass, rclass, ltype, rtype);
932 ctype = rtype = ltype;
935 expr->left = cast_to(expr->left, ltype);
936 expr->right = cast_to(expr->right, rtype);
937 expr->ctype = ctype;
938 return ctype;
941 /* pointer (+|-) integer */
942 if (lclass & TYPE_PTR && is_int(rclass) && (op == '+' || op == '-')) {
943 unrestrict(expr->right, rclass, &rtype);
944 return evaluate_ptr_add(expr, rtype);
947 /* integer + pointer */
948 if (rclass & TYPE_PTR && is_int(lclass) && op == '+') {
949 struct expression *index = expr->left;
950 unrestrict(index, lclass, &ltype);
951 expr->left = expr->right;
952 expr->right = index;
953 return evaluate_ptr_add(expr, ltype);
956 /* pointer - pointer */
957 if (lclass & rclass & TYPE_PTR && expr->op == '-')
958 return evaluate_ptr_sub(expr);
960 return bad_expr_type(expr);
963 static struct symbol *evaluate_comma(struct expression *expr)
965 expr->ctype = degenerate(expr->right);
966 if (expr->ctype == &null_ctype)
967 expr->ctype = &ptr_ctype;
968 expr->flags &= expr->left->flags & expr->right->flags;
969 return expr->ctype;
972 static int modify_for_unsigned(int op)
974 if (op == '<')
975 op = SPECIAL_UNSIGNED_LT;
976 else if (op == '>')
977 op = SPECIAL_UNSIGNED_GT;
978 else if (op == SPECIAL_LTE)
979 op = SPECIAL_UNSIGNED_LTE;
980 else if (op == SPECIAL_GTE)
981 op = SPECIAL_UNSIGNED_GTE;
982 return op;
985 static inline int is_null_pointer_constant(struct expression *e)
987 if (e->ctype == &null_ctype)
988 return 1;
989 if (!(e->flags & Int_const_expr))
990 return 0;
991 return is_zero_constant(e) ? 2 : 0;
994 static struct symbol *evaluate_compare(struct expression *expr)
996 struct expression *left = expr->left, *right = expr->right;
997 struct symbol *ltype, *rtype, *lbase, *rbase;
998 int lclass = classify_type(degenerate(left), &ltype);
999 int rclass = classify_type(degenerate(right), &rtype);
1000 struct symbol *ctype;
1001 const char *typediff;
1003 if (expr->flags) {
1004 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
1005 expr->flags = 0;
1008 /* Type types? */
1009 if (is_type_type(ltype) && is_type_type(rtype))
1010 goto OK;
1012 if (is_safe_type(left->ctype) || is_safe_type(right->ctype))
1013 warning(expr->pos, "testing a 'safe expression'");
1015 /* number on number */
1016 if (lclass & rclass & TYPE_NUM) {
1017 ctype = usual_conversions(expr->op, expr->left, expr->right,
1018 lclass, rclass, ltype, rtype);
1019 expr->left = cast_to(expr->left, ctype);
1020 expr->right = cast_to(expr->right, ctype);
1021 if (ctype->ctype.modifiers & MOD_UNSIGNED)
1022 expr->op = modify_for_unsigned(expr->op);
1023 goto OK;
1026 /* at least one must be a pointer */
1027 if (!((lclass | rclass) & TYPE_PTR))
1028 return bad_expr_type(expr);
1030 /* equality comparisons can be with null pointer constants */
1031 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1032 int is_null1 = is_null_pointer_constant(left);
1033 int is_null2 = is_null_pointer_constant(right);
1034 if (is_null1 == 2)
1035 bad_null(left);
1036 if (is_null2 == 2)
1037 bad_null(right);
1038 if (is_null1 && is_null2) {
1039 int positive = expr->op == SPECIAL_EQUAL;
1040 expr->type = EXPR_VALUE;
1041 expr->value = positive;
1042 goto OK;
1044 if (is_null1 && (rclass & TYPE_PTR)) {
1045 left = cast_to(left, rtype);
1046 goto OK;
1048 if (is_null2 && (lclass & TYPE_PTR)) {
1049 right = cast_to(right, ltype);
1050 goto OK;
1053 /* both should be pointers */
1054 if (!(lclass & rclass & TYPE_PTR))
1055 return bad_expr_type(expr);
1056 expr->op = modify_for_unsigned(expr->op);
1058 lbase = examine_pointer_target(ltype);
1059 rbase = examine_pointer_target(rtype);
1061 /* they also have special treatment for pointers to void */
1062 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1063 if (ltype->ctype.as == rtype->ctype.as) {
1064 if (lbase == &void_ctype) {
1065 right = cast_to(right, ltype);
1066 goto OK;
1068 if (rbase == &void_ctype) {
1069 left = cast_to(left, rtype);
1070 goto OK;
1075 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1076 target_qualifiers(rtype),
1077 target_qualifiers(ltype));
1078 if (!typediff)
1079 goto OK;
1081 expression_error(expr, "incompatible types in comparison expression (%s)", typediff);
1082 return NULL;
1085 /* the result is int [6.5.8(6), 6.5.9(3)]*/
1086 expr->ctype = &int_ctype;
1087 return &int_ctype;
1091 * NOTE! The degenerate case of "x ? : y", where we don't
1092 * have a true case, this will possibly promote "x" to the
1093 * same type as "y", and thus _change_ the conditional
1094 * test in the expression. But since promotion is "safe"
1095 * for testing, that's OK.
1097 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1099 struct expression **true;
1100 struct symbol *ctype, *ltype, *rtype, *lbase, *rbase;
1101 int lclass, rclass;
1102 const char * typediff;
1103 int qual;
1105 if (!evaluate_conditional(expr->conditional, 0))
1106 return NULL;
1107 if (!evaluate_expression(expr->cond_false))
1108 return NULL;
1110 ctype = degenerate(expr->conditional);
1111 rtype = degenerate(expr->cond_false);
1113 true = &expr->conditional;
1114 ltype = ctype;
1115 if (expr->cond_true) {
1116 if (!evaluate_expression(expr->cond_true))
1117 return NULL;
1118 ltype = degenerate(expr->cond_true);
1119 true = &expr->cond_true;
1122 if (expr->flags) {
1123 int flags = expr->conditional->flags & Int_const_expr;
1124 flags &= (*true)->flags & expr->cond_false->flags;
1125 if (!flags)
1126 expr->flags = 0;
1129 lclass = classify_type(ltype, &ltype);
1130 rclass = classify_type(rtype, &rtype);
1131 if (lclass & rclass & TYPE_NUM) {
1132 ctype = usual_conversions('?', *true, expr->cond_false,
1133 lclass, rclass, ltype, rtype);
1134 *true = cast_to(*true, ctype);
1135 expr->cond_false = cast_to(expr->cond_false, ctype);
1136 goto out;
1139 if ((lclass | rclass) & TYPE_PTR) {
1140 int is_null1 = is_null_pointer_constant(*true);
1141 int is_null2 = is_null_pointer_constant(expr->cond_false);
1143 if (is_null1 && is_null2) {
1144 *true = cast_to(*true, &ptr_ctype);
1145 expr->cond_false = cast_to(expr->cond_false, &ptr_ctype);
1146 ctype = &ptr_ctype;
1147 goto out;
1149 if (is_null1 && (rclass & TYPE_PTR)) {
1150 if (is_null1 == 2)
1151 bad_null(*true);
1152 *true = cast_to(*true, rtype);
1153 ctype = rtype;
1154 goto out;
1156 if (is_null2 && (lclass & TYPE_PTR)) {
1157 if (is_null2 == 2)
1158 bad_null(expr->cond_false);
1159 expr->cond_false = cast_to(expr->cond_false, ltype);
1160 ctype = ltype;
1161 goto out;
1163 if (!(lclass & rclass & TYPE_PTR)) {
1164 typediff = "different types";
1165 goto Err;
1167 /* OK, it's pointer on pointer */
1168 if (ltype->ctype.as != rtype->ctype.as) {
1169 typediff = "different address spaces";
1170 goto Err;
1173 /* need to be lazier here */
1174 lbase = examine_pointer_target(ltype);
1175 rbase = examine_pointer_target(rtype);
1176 qual = target_qualifiers(ltype) | target_qualifiers(rtype);
1178 if (lbase == &void_ctype) {
1179 /* XXX: pointers to function should warn here */
1180 ctype = ltype;
1181 goto Qual;
1184 if (rbase == &void_ctype) {
1185 /* XXX: pointers to function should warn here */
1186 ctype = rtype;
1187 goto Qual;
1189 /* XXX: that should be pointer to composite */
1190 ctype = ltype;
1191 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1192 qual, qual);
1193 if (!typediff)
1194 goto Qual;
1195 goto Err;
1198 /* void on void, struct on same struct, union on same union */
1199 if (ltype == rtype) {
1200 ctype = ltype;
1201 goto out;
1203 typediff = "different base types";
1205 Err:
1206 expression_error(expr, "incompatible types in conditional expression (%s)", typediff);
1207 return NULL;
1209 out:
1210 expr->ctype = ctype;
1211 return ctype;
1213 Qual:
1214 if (qual & ~ctype->ctype.modifiers) {
1215 struct symbol *sym = alloc_symbol(ctype->pos, SYM_PTR);
1216 *sym = *ctype;
1217 sym->ctype.modifiers |= qual;
1218 ctype = sym;
1220 *true = cast_to(*true, ctype);
1221 expr->cond_false = cast_to(expr->cond_false, ctype);
1222 goto out;
1225 /* FP assignments can not do modulo or bit operations */
1226 static int compatible_float_op(int op)
1228 return op == SPECIAL_ADD_ASSIGN ||
1229 op == SPECIAL_SUB_ASSIGN ||
1230 op == SPECIAL_MUL_ASSIGN ||
1231 op == SPECIAL_DIV_ASSIGN;
1234 static int evaluate_assign_op(struct expression *expr)
1236 struct symbol *target = expr->left->ctype;
1237 struct symbol *source = expr->right->ctype;
1238 struct symbol *t, *s;
1239 int tclass = classify_type(target, &t);
1240 int sclass = classify_type(source, &s);
1241 int op = expr->op;
1243 if (tclass & sclass & TYPE_NUM) {
1244 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1245 expression_error(expr, "invalid assignment");
1246 return 0;
1248 if (tclass & TYPE_RESTRICT) {
1249 if (!restricted_binop(op, t)) {
1250 warning(expr->pos, "bad assignment (%s) to %s",
1251 show_special(op), show_typename(t));
1252 expr->right = cast_to(expr->right, target);
1253 return 0;
1255 /* allowed assignments unfoul */
1256 if (sclass & TYPE_FOULED && unfoul(s) == t)
1257 goto Cast;
1258 if (!restricted_value(expr->right, t))
1259 return 1;
1260 } else if (!(sclass & TYPE_RESTRICT))
1261 goto Cast;
1262 /* source and target would better be identical restricted */
1263 if (t == s)
1264 return 1;
1265 warning(expr->pos, "invalid assignment: %s", show_special(op));
1266 info(expr->pos, " left side has type %s", show_typename(t));
1267 info(expr->pos, " right side has type %s", show_typename(s));
1268 expr->right = cast_to(expr->right, target);
1269 return 0;
1271 if (tclass == TYPE_PTR && is_int(sclass)) {
1272 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1273 unrestrict(expr->right, sclass, &s);
1274 evaluate_ptr_add(expr, s);
1275 return 1;
1277 expression_error(expr, "invalid pointer assignment");
1278 return 0;
1281 expression_error(expr, "invalid assignment");
1282 return 0;
1284 Cast:
1285 expr->right = cast_to(expr->right, target);
1286 return 1;
1289 static int whitelist_pointers(struct symbol *t1, struct symbol *t2)
1291 if (t1 == t2)
1292 return 0; /* yes, 0 - we don't want a cast_to here */
1293 if (t1 == &void_ctype)
1294 return 1;
1295 if (t2 == &void_ctype)
1296 return 1;
1297 if (classify_type(t1, &t1) != TYPE_NUM)
1298 return 0;
1299 if (classify_type(t2, &t2) != TYPE_NUM)
1300 return 0;
1301 if (t1 == t2)
1302 return 1;
1303 if (t1->ctype.modifiers & t2->ctype.modifiers & MOD_CHAR)
1304 return 1;
1305 if ((t1->ctype.modifiers ^ t2->ctype.modifiers) & MOD_SIZE)
1306 return 0;
1307 return !Wtypesign;
1310 static int check_assignment_types(struct symbol *target, struct expression **rp,
1311 const char **typediff)
1313 struct symbol *source = degenerate(*rp);
1314 struct symbol *t, *s;
1315 int tclass = classify_type(target, &t);
1316 int sclass = classify_type(source, &s);
1318 if (tclass & sclass & TYPE_NUM) {
1319 if (tclass & TYPE_RESTRICT) {
1320 /* allowed assignments unfoul */
1321 if (sclass & TYPE_FOULED && unfoul(s) == t)
1322 goto Cast;
1323 if (!restricted_value(*rp, target))
1324 return 1;
1325 if (s == t)
1326 return 1;
1327 } else if (!(sclass & TYPE_RESTRICT))
1328 goto Cast;
1329 *typediff = "different base types";
1330 return 0;
1333 if (tclass == TYPE_PTR) {
1334 unsigned long mod1, mod2;
1335 struct symbol *b1, *b2;
1336 // NULL pointer is always OK
1337 int is_null = is_null_pointer_constant(*rp);
1338 if (is_null) {
1339 if (is_null == 2)
1340 bad_null(*rp);
1341 goto Cast;
1343 if (!(sclass & TYPE_PTR)) {
1344 *typediff = "different base types";
1345 return 0;
1347 b1 = examine_pointer_target(t);
1348 b2 = examine_pointer_target(s);
1349 mod1 = target_qualifiers(t);
1350 mod2 = target_qualifiers(s);
1351 if (whitelist_pointers(b1, b2)) {
1353 * assignments to/from void * are OK, provided that
1354 * we do not remove qualifiers from pointed to [C]
1355 * or mix address spaces [sparse].
1357 if (t->ctype.as != s->ctype.as) {
1358 *typediff = "different address spaces";
1359 return 0;
1361 if (mod2 & ~mod1) {
1362 *typediff = "different modifiers";
1363 return 0;
1365 goto Cast;
1367 /* It's OK if the target is more volatile or const than the source */
1368 *typediff = type_difference(&t->ctype, &s->ctype, 0, mod1);
1369 if (*typediff)
1370 return 0;
1371 return 1;
1374 if ((tclass & TYPE_COMPOUND) && s == t)
1375 return 1;
1377 if (tclass & TYPE_NUM) {
1378 /* XXX: need to turn into comparison with NULL */
1379 if (t == &bool_ctype && (sclass & TYPE_PTR))
1380 goto Cast;
1381 *typediff = "different base types";
1382 return 0;
1384 *typediff = "invalid types";
1385 return 0;
1387 Cast:
1388 *rp = cast_to(*rp, target);
1389 return 1;
1392 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1393 struct expression **rp, const char *where)
1395 const char *typediff;
1396 struct symbol *source = degenerate(*rp);
1398 if (!check_assignment_types(target, rp, &typediff)) {
1399 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1400 info(expr->pos, " expected %s", show_typename(target));
1401 info(expr->pos, " got %s", show_typename(source));
1402 *rp = cast_to(*rp, target);
1403 return 0;
1406 return 1;
1409 static int compatible_transparent_union(struct symbol *target,
1410 struct expression **rp)
1412 struct symbol *t, *member;
1413 classify_type(target, &t);
1414 if (t->type != SYM_UNION || !t->transparent_union)
1415 return 0;
1417 FOR_EACH_PTR(t->symbol_list, member) {
1418 const char *typediff;
1419 if (check_assignment_types(member, rp, &typediff))
1420 return 1;
1421 } END_FOR_EACH_PTR(member);
1423 return 0;
1426 static int compatible_argument_type(struct expression *expr, struct symbol *target,
1427 struct expression **rp, const char *where)
1429 if (compatible_transparent_union(target, rp))
1430 return 1;
1432 return compatible_assignment_types(expr, target, rp, where);
1435 static void mark_assigned(struct expression *expr)
1437 struct symbol *sym;
1439 if (!expr)
1440 return;
1441 switch (expr->type) {
1442 case EXPR_SYMBOL:
1443 sym = expr->symbol;
1444 if (!sym)
1445 return;
1446 if (sym->type != SYM_NODE)
1447 return;
1448 sym->ctype.modifiers |= MOD_ASSIGNED;
1449 return;
1451 case EXPR_BINOP:
1452 mark_assigned(expr->left);
1453 mark_assigned(expr->right);
1454 return;
1455 case EXPR_CAST:
1456 case EXPR_FORCE_CAST:
1457 mark_assigned(expr->cast_expression);
1458 return;
1459 case EXPR_SLICE:
1460 mark_assigned(expr->base);
1461 return;
1462 default:
1463 /* Hmm? */
1464 return;
1468 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1470 if (type->ctype.modifiers & MOD_CONST)
1471 expression_error(left, "assignment to const expression");
1473 /* We know left is an lvalue, so it's a "preop-*" */
1474 mark_assigned(left->unop);
1477 static struct symbol *evaluate_assignment(struct expression *expr)
1479 struct expression *left = expr->left;
1480 struct expression *where = expr;
1481 struct symbol *ltype;
1483 if (!lvalue_expression(left)) {
1484 expression_error(expr, "not an lvalue");
1485 return NULL;
1488 ltype = left->ctype;
1490 if (expr->op != '=') {
1491 if (!evaluate_assign_op(expr))
1492 return NULL;
1493 } else {
1494 if (!compatible_assignment_types(where, ltype, &expr->right, "assignment"))
1495 return NULL;
1498 evaluate_assign_to(left, ltype);
1500 expr->ctype = ltype;
1501 return ltype;
1504 static void examine_fn_arguments(struct symbol *fn)
1506 struct symbol *s;
1508 FOR_EACH_PTR(fn->arguments, s) {
1509 struct symbol *arg = evaluate_symbol(s);
1510 /* Array/function arguments silently degenerate into pointers */
1511 if (arg) {
1512 struct symbol *ptr;
1513 switch(arg->type) {
1514 case SYM_ARRAY:
1515 case SYM_FN:
1516 ptr = alloc_symbol(s->pos, SYM_PTR);
1517 if (arg->type == SYM_ARRAY)
1518 ptr->ctype = arg->ctype;
1519 else
1520 ptr->ctype.base_type = arg;
1521 ptr->ctype.as |= s->ctype.as;
1522 ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1524 s->ctype.base_type = ptr;
1525 s->ctype.as = 0;
1526 s->ctype.modifiers &= ~MOD_PTRINHERIT;
1527 s->bit_size = 0;
1528 s->examined = 0;
1529 examine_symbol_type(s);
1530 break;
1531 default:
1532 /* nothing */
1533 break;
1536 } END_FOR_EACH_PTR(s);
1539 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1541 /* Take the modifiers of the pointer, and apply them to the member */
1542 mod |= sym->ctype.modifiers;
1543 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1544 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1545 *newsym = *sym;
1546 newsym->ctype.as = as;
1547 newsym->ctype.modifiers = mod;
1548 sym = newsym;
1550 return sym;
1553 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1555 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1556 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1558 node->ctype.base_type = ptr;
1559 ptr->bit_size = bits_in_pointer;
1560 ptr->ctype.alignment = pointer_alignment;
1562 node->bit_size = bits_in_pointer;
1563 node->ctype.alignment = pointer_alignment;
1565 access_symbol(sym);
1566 if (sym->ctype.modifiers & MOD_REGISTER) {
1567 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1568 sym->ctype.modifiers &= ~MOD_REGISTER;
1570 if (sym->type == SYM_NODE) {
1571 ptr->ctype.as |= sym->ctype.as;
1572 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1573 sym = sym->ctype.base_type;
1575 if (degenerate && sym->type == SYM_ARRAY) {
1576 ptr->ctype.as |= sym->ctype.as;
1577 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1578 sym = sym->ctype.base_type;
1580 ptr->ctype.base_type = sym;
1582 return node;
1585 /* Arrays degenerate into pointers on pointer arithmetic */
1586 static struct symbol *degenerate(struct expression *expr)
1588 struct symbol *ctype, *base;
1590 if (!expr)
1591 return NULL;
1592 ctype = expr->ctype;
1593 if (!ctype)
1594 return NULL;
1595 base = examine_symbol_type(ctype);
1596 if (ctype->type == SYM_NODE)
1597 base = ctype->ctype.base_type;
1599 * Arrays degenerate into pointers to the entries, while
1600 * functions degenerate into pointers to themselves.
1601 * If array was part of non-lvalue compound, we create a copy
1602 * of that compound first and then act as if we were dealing with
1603 * the corresponding field in there.
1605 switch (base->type) {
1606 case SYM_ARRAY:
1607 if (expr->type == EXPR_SLICE) {
1608 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1609 struct expression *e0, *e1, *e2, *e3, *e4;
1611 a->ctype.base_type = expr->base->ctype;
1612 a->bit_size = expr->base->ctype->bit_size;
1613 a->array_size = expr->base->ctype->array_size;
1615 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1616 e0->symbol = a;
1617 e0->ctype = &lazy_ptr_ctype;
1619 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1620 e1->unop = e0;
1621 e1->op = '*';
1622 e1->ctype = expr->base->ctype; /* XXX */
1624 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1625 e2->left = e1;
1626 e2->right = expr->base;
1627 e2->op = '=';
1628 e2->ctype = expr->base->ctype;
1630 if (expr->r_bitpos) {
1631 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1632 e3->op = '+';
1633 e3->left = e0;
1634 e3->right = alloc_const_expression(expr->pos,
1635 bits_to_bytes(expr->r_bitpos));
1636 e3->ctype = &lazy_ptr_ctype;
1637 } else {
1638 e3 = e0;
1641 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1642 e4->left = e2;
1643 e4->right = e3;
1644 e4->ctype = &lazy_ptr_ctype;
1646 expr->unop = e4;
1647 expr->type = EXPR_PREOP;
1648 expr->op = '*';
1650 case SYM_FN:
1651 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1652 expression_error(expr, "strange non-value function or array");
1653 return &bad_ctype;
1655 *expr = *expr->unop;
1656 ctype = create_pointer(expr, ctype, 1);
1657 expr->ctype = ctype;
1658 default:
1659 /* nothing */;
1661 return ctype;
1664 static struct symbol *evaluate_addressof(struct expression *expr)
1666 struct expression *op = expr->unop;
1667 struct symbol *ctype;
1669 if (op->op != '*' || op->type != EXPR_PREOP) {
1670 expression_error(expr, "not addressable");
1671 return NULL;
1673 ctype = op->ctype;
1674 *expr = *op->unop;
1675 expr->flags = 0;
1677 if (expr->type == EXPR_SYMBOL) {
1678 struct symbol *sym = expr->symbol;
1679 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1683 * symbol expression evaluation is lazy about the type
1684 * of the sub-expression, so we may have to generate
1685 * the type here if so..
1687 if (expr->ctype == &lazy_ptr_ctype) {
1688 ctype = create_pointer(expr, ctype, 0);
1689 expr->ctype = ctype;
1691 return expr->ctype;
1695 static struct symbol *evaluate_dereference(struct expression *expr)
1697 struct expression *op = expr->unop;
1698 struct symbol *ctype = op->ctype, *node, *target;
1700 /* Simplify: *&(expr) => (expr) */
1701 if (op->type == EXPR_PREOP && op->op == '&') {
1702 *expr = *op->unop;
1703 expr->flags = 0;
1704 return expr->ctype;
1707 /* Dereferencing a node drops all the node information. */
1708 if (ctype->type == SYM_NODE)
1709 ctype = ctype->ctype.base_type;
1711 node = alloc_symbol(expr->pos, SYM_NODE);
1712 target = ctype->ctype.base_type;
1714 switch (ctype->type) {
1715 default:
1716 expression_error(expr, "cannot dereference this type");
1717 return NULL;
1718 case SYM_PTR:
1719 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1720 merge_type(node, ctype);
1721 break;
1723 case SYM_ARRAY:
1724 if (!lvalue_expression(op)) {
1725 expression_error(op, "non-lvalue array??");
1726 return NULL;
1729 /* Do the implied "addressof" on the array */
1730 *op = *op->unop;
1733 * When an array is dereferenced, we need to pick
1734 * up the attributes of the original node too..
1736 merge_type(node, op->ctype);
1737 merge_type(node, ctype);
1738 break;
1741 node->bit_size = target->bit_size;
1742 node->array_size = target->array_size;
1744 expr->ctype = node;
1745 return node;
1749 * Unary post-ops: x++ and x--
1751 static struct symbol *evaluate_postop(struct expression *expr)
1753 struct expression *op = expr->unop;
1754 struct symbol *ctype = op->ctype;
1755 int class = classify_type(ctype, &ctype);
1756 int multiply = 0;
1758 if (!class || class & TYPE_COMPOUND) {
1759 expression_error(expr, "need scalar for ++/--");
1760 return NULL;
1762 if (!lvalue_expression(expr->unop)) {
1763 expression_error(expr, "need lvalue expression for ++/--");
1764 return NULL;
1767 if ((class & TYPE_RESTRICT) && restricted_unop(expr->op, &ctype))
1768 unrestrict(expr, class, &ctype);
1770 if (class & TYPE_NUM) {
1771 multiply = 1;
1772 } else if (class == TYPE_PTR) {
1773 struct symbol *target = examine_pointer_target(ctype);
1774 if (!is_function(target))
1775 multiply = bits_to_bytes(target->bit_size);
1778 if (multiply) {
1779 evaluate_assign_to(op, op->ctype);
1780 expr->op_value = multiply;
1781 expr->ctype = ctype;
1782 return ctype;
1785 expression_error(expr, "bad argument type for ++/--");
1786 return NULL;
1789 static struct symbol *evaluate_sign(struct expression *expr)
1791 struct symbol *ctype = expr->unop->ctype;
1792 int class = classify_type(ctype, &ctype);
1793 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1794 expr->flags = 0;
1795 /* should be an arithmetic type */
1796 if (!(class & TYPE_NUM))
1797 return bad_expr_type(expr);
1798 if (class & TYPE_RESTRICT)
1799 goto Restr;
1800 Normal:
1801 if (!(class & TYPE_FLOAT)) {
1802 ctype = integer_promotion(ctype);
1803 expr->unop = cast_to(expr->unop, ctype);
1804 } else if (expr->op != '~') {
1805 /* no conversions needed */
1806 } else {
1807 return bad_expr_type(expr);
1809 if (expr->op == '+')
1810 *expr = *expr->unop;
1811 expr->ctype = ctype;
1812 return ctype;
1813 Restr:
1814 if (restricted_unop(expr->op, &ctype))
1815 unrestrict(expr, class, &ctype);
1816 goto Normal;
1819 static struct symbol *evaluate_preop(struct expression *expr)
1821 struct symbol *ctype = expr->unop->ctype;
1823 switch (expr->op) {
1824 case '(':
1825 *expr = *expr->unop;
1826 return ctype;
1828 case '+':
1829 case '-':
1830 case '~':
1831 return evaluate_sign(expr);
1833 case '*':
1834 return evaluate_dereference(expr);
1836 case '&':
1837 return evaluate_addressof(expr);
1839 case SPECIAL_INCREMENT:
1840 case SPECIAL_DECREMENT:
1842 * From a type evaluation standpoint the preops are
1843 * the same as the postops
1845 return evaluate_postop(expr);
1847 case '!':
1848 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1849 expr->flags = 0;
1850 if (is_safe_type(ctype))
1851 warning(expr->pos, "testing a 'safe expression'");
1852 if (is_float_type(ctype)) {
1853 struct expression *arg = expr->unop;
1854 expr->type = EXPR_COMPARE;
1855 expr->op = SPECIAL_EQUAL;
1856 expr->left = arg;
1857 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1858 expr->right->ctype = ctype;
1859 expr->right->fvalue = 0;
1860 } else if (is_fouled_type(ctype)) {
1861 warning(expr->pos, "%s degrades to integer",
1862 show_typename(ctype->ctype.base_type));
1864 /* the result is int [6.5.3.3(5)]*/
1865 ctype = &int_ctype;
1866 break;
1868 default:
1869 break;
1871 expr->ctype = ctype;
1872 return ctype;
1875 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1877 struct ptr_list *head = (struct ptr_list *)_list;
1878 struct ptr_list *list = head;
1880 if (!head)
1881 return NULL;
1882 do {
1883 int i;
1884 for (i = 0; i < list->nr; i++) {
1885 struct symbol *sym = (struct symbol *) list->list[i];
1886 if (sym->ident) {
1887 if (sym->ident != ident)
1888 continue;
1889 *offset = sym->offset;
1890 return sym;
1891 } else {
1892 struct symbol *ctype = sym->ctype.base_type;
1893 struct symbol *sub;
1894 if (!ctype)
1895 continue;
1896 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1897 continue;
1898 sub = find_identifier(ident, ctype->symbol_list, offset);
1899 if (!sub)
1900 continue;
1901 *offset += sym->offset;
1902 return sub;
1905 } while ((list = list->next) != head);
1906 return NULL;
1909 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1911 struct expression *add;
1914 * Create a new add-expression
1916 * NOTE! Even if we just add zero, we need a new node
1917 * for the member pointer, since it has a different
1918 * type than the original pointer. We could make that
1919 * be just a cast, but the fact is, a node is a node,
1920 * so we might as well just do the "add zero" here.
1922 add = alloc_expression(expr->pos, EXPR_BINOP);
1923 add->op = '+';
1924 add->left = expr;
1925 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1926 add->right->ctype = &int_ctype;
1927 add->right->value = offset;
1930 * The ctype of the pointer will be lazily evaluated if
1931 * we ever take the address of this member dereference..
1933 add->ctype = &lazy_ptr_ctype;
1934 return add;
1937 /* structure/union dereference */
1938 static struct symbol *evaluate_member_dereference(struct expression *expr)
1940 int offset;
1941 struct symbol *ctype, *member;
1942 struct expression *deref = expr->deref, *add;
1943 struct ident *ident = expr->member;
1944 unsigned int mod;
1945 int address_space;
1947 if (!evaluate_expression(deref))
1948 return NULL;
1949 if (!ident) {
1950 expression_error(expr, "bad member name");
1951 return NULL;
1954 ctype = deref->ctype;
1955 examine_symbol_type(ctype);
1956 address_space = ctype->ctype.as;
1957 mod = ctype->ctype.modifiers;
1958 if (ctype->type == SYM_NODE) {
1959 ctype = ctype->ctype.base_type;
1960 address_space |= ctype->ctype.as;
1961 mod |= ctype->ctype.modifiers;
1963 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1964 expression_error(expr, "expected structure or union");
1965 return NULL;
1967 offset = 0;
1968 member = find_identifier(ident, ctype->symbol_list, &offset);
1969 if (!member) {
1970 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1971 const char *name = "<unnamed>";
1972 int namelen = 9;
1973 if (ctype->ident) {
1974 name = ctype->ident->name;
1975 namelen = ctype->ident->len;
1977 if (ctype->symbol_list)
1978 expression_error(expr, "no member '%s' in %s %.*s",
1979 show_ident(ident), type, namelen, name);
1980 else
1981 expression_error(expr, "using member '%s' in "
1982 "incomplete %s %.*s", show_ident(ident),
1983 type, namelen, name);
1984 return NULL;
1988 * The member needs to take on the address space and modifiers of
1989 * the "parent" type.
1991 member = convert_to_as_mod(member, address_space, mod);
1992 ctype = get_base_type(member);
1994 if (!lvalue_expression(deref)) {
1995 if (deref->type != EXPR_SLICE) {
1996 expr->base = deref;
1997 expr->r_bitpos = 0;
1998 } else {
1999 expr->base = deref->base;
2000 expr->r_bitpos = deref->r_bitpos;
2002 expr->r_bitpos += bytes_to_bits(offset);
2003 expr->type = EXPR_SLICE;
2004 expr->r_nrbits = member->bit_size;
2005 expr->r_bitpos += member->bit_offset;
2006 expr->ctype = member;
2007 return member;
2010 deref = deref->unop;
2011 expr->deref = deref;
2013 add = evaluate_offset(deref, offset);
2014 expr->type = EXPR_PREOP;
2015 expr->op = '*';
2016 expr->unop = add;
2018 expr->ctype = member;
2019 return member;
2022 static int is_promoted(struct expression *expr)
2024 while (1) {
2025 switch (expr->type) {
2026 case EXPR_BINOP:
2027 case EXPR_SELECT:
2028 case EXPR_CONDITIONAL:
2029 return 1;
2030 case EXPR_COMMA:
2031 expr = expr->right;
2032 continue;
2033 case EXPR_PREOP:
2034 switch (expr->op) {
2035 case '(':
2036 expr = expr->unop;
2037 continue;
2038 case '+':
2039 case '-':
2040 case '~':
2041 return 1;
2042 default:
2043 return 0;
2045 default:
2046 return 0;
2052 static struct symbol *evaluate_cast(struct expression *);
2054 static struct symbol *evaluate_type_information(struct expression *expr)
2056 struct symbol *sym = expr->cast_type;
2057 if (!sym) {
2058 sym = evaluate_expression(expr->cast_expression);
2059 if (!sym)
2060 return NULL;
2062 * Expressions of restricted types will possibly get
2063 * promoted - check that here
2065 if (is_restricted_type(sym)) {
2066 if (sym->bit_size < bits_in_int && is_promoted(expr))
2067 sym = &int_ctype;
2068 } else if (is_fouled_type(sym)) {
2069 sym = &int_ctype;
2072 examine_symbol_type(sym);
2073 if (is_bitfield_type(sym)) {
2074 expression_error(expr, "trying to examine bitfield type");
2075 return NULL;
2077 return sym;
2080 static struct symbol *evaluate_sizeof(struct expression *expr)
2082 struct symbol *type;
2083 int size;
2085 type = evaluate_type_information(expr);
2086 if (!type)
2087 return NULL;
2089 size = type->bit_size;
2091 if (size < 0 && is_void_type(type)) {
2092 warning(expr->pos, "expression using sizeof(void)");
2093 size = bits_in_char;
2096 if (size == 1 && is_bool_type(type)) {
2097 if (Wsizeof_bool)
2098 warning(expr->pos, "expression using sizeof bool");
2099 size = bits_in_char;
2102 if (is_function(type->ctype.base_type)) {
2103 warning(expr->pos, "expression using sizeof on a function");
2104 size = bits_in_char;
2107 if ((size < 0) || (size & (bits_in_char - 1)))
2108 expression_error(expr, "cannot size expression");
2110 expr->type = EXPR_VALUE;
2111 expr->value = bits_to_bytes(size);
2112 expr->taint = 0;
2113 expr->ctype = size_t_ctype;
2114 return size_t_ctype;
2117 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
2119 struct symbol *type;
2120 int size;
2122 type = evaluate_type_information(expr);
2123 if (!type)
2124 return NULL;
2126 if (type->type == SYM_NODE)
2127 type = type->ctype.base_type;
2128 if (!type)
2129 return NULL;
2130 switch (type->type) {
2131 case SYM_ARRAY:
2132 break;
2133 case SYM_PTR:
2134 type = get_base_type(type);
2135 if (type)
2136 break;
2137 default:
2138 expression_error(expr, "expected pointer expression");
2139 return NULL;
2141 size = type->bit_size;
2142 if (size & (bits_in_char-1))
2143 size = 0;
2144 expr->type = EXPR_VALUE;
2145 expr->value = bits_to_bytes(size);
2146 expr->taint = 0;
2147 expr->ctype = size_t_ctype;
2148 return size_t_ctype;
2151 static struct symbol *evaluate_alignof(struct expression *expr)
2153 struct symbol *type;
2155 type = evaluate_type_information(expr);
2156 if (!type)
2157 return NULL;
2159 expr->type = EXPR_VALUE;
2160 expr->value = type->ctype.alignment;
2161 expr->taint = 0;
2162 expr->ctype = size_t_ctype;
2163 return size_t_ctype;
2166 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
2168 struct expression *expr;
2169 struct symbol_list *argument_types = fn->arguments;
2170 struct symbol *argtype;
2171 int i = 1;
2173 PREPARE_PTR_LIST(argument_types, argtype);
2174 FOR_EACH_PTR (head, expr) {
2175 struct expression **p = THIS_ADDRESS(expr);
2176 struct symbol *ctype, *target;
2177 ctype = evaluate_expression(expr);
2179 if (!ctype)
2180 return 0;
2182 target = argtype;
2183 if (!target) {
2184 struct symbol *type;
2185 int class = classify_type(ctype, &type);
2186 if (is_int(class)) {
2187 *p = cast_to(expr, integer_promotion(type));
2188 } else if (class & TYPE_FLOAT) {
2189 unsigned long mod = type->ctype.modifiers;
2190 if (!(mod & (MOD_LONG_ALL)))
2191 *p = cast_to(expr, &double_ctype);
2192 } else if (class & TYPE_PTR) {
2193 if (expr->ctype == &null_ctype)
2194 *p = cast_to(expr, &ptr_ctype);
2195 else
2196 degenerate(expr);
2198 } else if (!target->forced_arg){
2199 static char where[30];
2200 examine_symbol_type(target);
2201 sprintf(where, "argument %d", i);
2202 compatible_argument_type(expr, target, p, where);
2205 i++;
2206 NEXT_PTR_LIST(argtype);
2207 } END_FOR_EACH_PTR(expr);
2208 FINISH_PTR_LIST(argtype);
2209 return 1;
2212 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
2214 struct symbol *sym;
2216 FOR_EACH_PTR(ctype->symbol_list, sym) {
2217 if (sym->ident == ident)
2218 return sym;
2219 } END_FOR_EACH_PTR(sym);
2220 return NULL;
2223 static void convert_index(struct expression *e)
2225 struct expression *child = e->idx_expression;
2226 unsigned from = e->idx_from;
2227 unsigned to = e->idx_to + 1;
2228 e->type = EXPR_POS;
2229 e->init_offset = from * bits_to_bytes(e->ctype->bit_size);
2230 e->init_nr = to - from;
2231 e->init_expr = child;
2234 static void convert_ident(struct expression *e)
2236 struct expression *child = e->ident_expression;
2237 struct symbol *sym = e->field;
2238 e->type = EXPR_POS;
2239 e->init_offset = sym->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;
2292 *v = new;
2293 return new;
2297 * sanity-check explicit designators; return the innermost one or NULL
2298 * in case of error. Assign types.
2300 static struct expression *check_designators(struct expression *e,
2301 struct symbol *ctype)
2303 struct expression *last = NULL;
2304 const char *err;
2305 while (1) {
2306 if (ctype->type == SYM_NODE)
2307 ctype = ctype->ctype.base_type;
2308 if (e->type == EXPR_INDEX) {
2309 struct symbol *type;
2310 if (ctype->type != SYM_ARRAY) {
2311 err = "array index in non-array";
2312 break;
2314 type = ctype->ctype.base_type;
2315 if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2316 unsigned offset = e->idx_to * type->bit_size;
2317 if (offset >= ctype->bit_size) {
2318 err = "index out of bounds in";
2319 break;
2322 e->ctype = ctype = type;
2323 ctype = type;
2324 last = e;
2325 if (!e->idx_expression) {
2326 err = "invalid";
2327 break;
2329 e = e->idx_expression;
2330 } else if (e->type == EXPR_IDENTIFIER) {
2331 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2332 err = "field name not in struct or union";
2333 break;
2335 ctype = find_struct_ident(ctype, e->expr_ident);
2336 if (!ctype) {
2337 err = "unknown field name in";
2338 break;
2340 e->field = e->ctype = ctype;
2341 last = e;
2342 if (!e->ident_expression) {
2343 err = "invalid";
2344 break;
2346 e = e->ident_expression;
2347 } else if (e->type == EXPR_POS) {
2348 err = "internal front-end error: EXPR_POS in";
2349 break;
2350 } else
2351 return last;
2353 expression_error(e, "%s initializer", err);
2354 return NULL;
2358 * choose the next subobject to initialize.
2360 * Get designators for next element, switch old ones to EXPR_POS.
2361 * Return the resulting expression or NULL if we'd run out of subobjects.
2362 * The innermost designator is returned in *v. Designators in old
2363 * are assumed to be already sanity-checked.
2365 static struct expression *next_designators(struct expression *old,
2366 struct symbol *ctype,
2367 struct expression *e, struct expression **v)
2369 struct expression *new = NULL;
2371 if (!old)
2372 return NULL;
2373 if (old->type == EXPR_INDEX) {
2374 struct expression *copy;
2375 unsigned n;
2377 copy = next_designators(old->idx_expression,
2378 old->ctype, e, v);
2379 if (!copy) {
2380 n = old->idx_to + 1;
2381 if (n * old->ctype->bit_size == ctype->bit_size) {
2382 convert_index(old);
2383 return NULL;
2385 copy = e;
2386 *v = new = alloc_expression(e->pos, EXPR_INDEX);
2387 } else {
2388 n = old->idx_to;
2389 new = alloc_expression(e->pos, EXPR_INDEX);
2392 new->idx_from = new->idx_to = n;
2393 new->idx_expression = copy;
2394 new->ctype = old->ctype;
2395 convert_index(old);
2396 } else if (old->type == EXPR_IDENTIFIER) {
2397 struct expression *copy;
2398 struct symbol *field;
2400 copy = next_designators(old->ident_expression,
2401 old->ctype, e, v);
2402 if (!copy) {
2403 field = old->field->next_subobject;
2404 if (!field) {
2405 convert_ident(old);
2406 return NULL;
2408 copy = e;
2409 *v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2410 } else {
2411 field = old->field;
2412 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2415 new->field = field;
2416 new->expr_ident = field->ident;
2417 new->ident_expression = copy;
2418 new->ctype = field;
2419 convert_ident(old);
2421 return new;
2424 static int handle_simple_initializer(struct expression **ep, int nested,
2425 int class, struct symbol *ctype);
2428 * deal with traversing subobjects [6.7.8(17,18,20)]
2430 static void handle_list_initializer(struct expression *expr,
2431 int class, struct symbol *ctype)
2433 struct expression *e, *last = NULL, *top = NULL, *next;
2434 int jumped = 0;
2436 FOR_EACH_PTR(expr->expr_list, e) {
2437 struct expression **v;
2438 struct symbol *type;
2439 int lclass;
2441 if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2442 struct symbol *struct_sym;
2443 if (!top) {
2444 top = e;
2445 last = first_subobject(ctype, class, &top);
2446 } else {
2447 last = next_designators(last, ctype, e, &top);
2449 if (!last) {
2450 excess(e, class & TYPE_PTR ? "array" :
2451 "struct or union");
2452 DELETE_CURRENT_PTR(e);
2453 continue;
2455 struct_sym = ctype->type == SYM_NODE ? ctype->ctype.base_type : ctype;
2456 if (Wdesignated_init && struct_sym->designated_init)
2457 warning(e->pos, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2458 ctype->ident ? "in initializer for " : "",
2459 ctype->ident ? ctype->ident->len : 0,
2460 ctype->ident ? ctype->ident->name : "",
2461 ctype->ident ? ": " : "",
2462 get_type_name(struct_sym->type),
2463 show_ident(struct_sym->ident));
2464 if (jumped) {
2465 warning(e->pos, "advancing past deep designator");
2466 jumped = 0;
2468 REPLACE_CURRENT_PTR(e, last);
2469 } else {
2470 next = check_designators(e, ctype);
2471 if (!next) {
2472 DELETE_CURRENT_PTR(e);
2473 continue;
2475 top = next;
2476 /* deeper than one designator? */
2477 jumped = top != e;
2478 convert_designators(last);
2479 last = e;
2482 found:
2483 lclass = classify_type(top->ctype, &type);
2484 if (top->type == EXPR_INDEX)
2485 v = &top->idx_expression;
2486 else
2487 v = &top->ident_expression;
2489 if (handle_simple_initializer(v, 1, lclass, top->ctype))
2490 continue;
2492 if (!(lclass & TYPE_COMPOUND)) {
2493 warning(e->pos, "bogus scalar initializer");
2494 DELETE_CURRENT_PTR(e);
2495 continue;
2498 next = first_subobject(type, lclass, v);
2499 if (next) {
2500 warning(e->pos, "missing braces around initializer");
2501 top = next;
2502 goto found;
2505 DELETE_CURRENT_PTR(e);
2506 excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2508 } END_FOR_EACH_PTR(e);
2510 convert_designators(last);
2511 expr->ctype = ctype;
2514 static int is_string_literal(struct expression **v)
2516 struct expression *e = *v;
2517 while (e && e->type == EXPR_PREOP && e->op == '(')
2518 e = e->unop;
2519 if (!e || e->type != EXPR_STRING)
2520 return 0;
2521 if (e != *v && Wparen_string)
2522 warning(e->pos,
2523 "array initialized from parenthesized string constant");
2524 *v = e;
2525 return 1;
2529 * We want a normal expression, possibly in one layer of braces. Warn
2530 * if the latter happens inside a list (it's legal, but likely to be
2531 * an effect of screwup). In case of anything not legal, we are definitely
2532 * having an effect of screwup, so just fail and let the caller warn.
2534 static struct expression *handle_scalar(struct expression *e, int nested)
2536 struct expression *v = NULL, *p;
2537 int count = 0;
2539 /* normal case */
2540 if (e->type != EXPR_INITIALIZER)
2541 return e;
2543 FOR_EACH_PTR(e->expr_list, p) {
2544 if (!v)
2545 v = p;
2546 count++;
2547 } END_FOR_EACH_PTR(p);
2548 if (count != 1)
2549 return NULL;
2550 switch(v->type) {
2551 case EXPR_INITIALIZER:
2552 case EXPR_INDEX:
2553 case EXPR_IDENTIFIER:
2554 return NULL;
2555 default:
2556 break;
2558 if (nested)
2559 warning(e->pos, "braces around scalar initializer");
2560 return v;
2564 * deal with the cases that don't care about subobjects:
2565 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2566 * character array <- string literal, possibly in braces [6.7.8(14)]
2567 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2568 * compound type <- initializer list in braces [6.7.8(16)]
2569 * The last one punts to handle_list_initializer() which, in turn will call
2570 * us for individual elements of the list.
2572 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2573 * the lack of support of wide char stuff in general.
2575 * One note: we need to take care not to evaluate a string literal until
2576 * we know that we *will* handle it right here. Otherwise we would screw
2577 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2578 * { "string", ...} - we need to preserve that string literal recognizable
2579 * until we dig into the inner struct.
2581 static int handle_simple_initializer(struct expression **ep, int nested,
2582 int class, struct symbol *ctype)
2584 int is_string = is_string_type(ctype);
2585 struct expression *e = *ep, *p;
2586 struct symbol *type;
2588 if (!e)
2589 return 0;
2591 /* scalar */
2592 if (!(class & TYPE_COMPOUND)) {
2593 e = handle_scalar(e, nested);
2594 if (!e)
2595 return 0;
2596 *ep = e;
2597 if (!evaluate_expression(e))
2598 return 1;
2599 compatible_assignment_types(e, ctype, ep, "initializer");
2600 return 1;
2604 * sublist; either a string, or we dig in; the latter will deal with
2605 * pathologies, so we don't need anything fancy here.
2607 if (e->type == EXPR_INITIALIZER) {
2608 if (is_string) {
2609 struct expression *v = NULL;
2610 int count = 0;
2612 FOR_EACH_PTR(e->expr_list, p) {
2613 if (!v)
2614 v = p;
2615 count++;
2616 } END_FOR_EACH_PTR(p);
2617 if (count == 1 && is_string_literal(&v)) {
2618 *ep = e = v;
2619 goto String;
2622 handle_list_initializer(e, class, ctype);
2623 return 1;
2626 /* string */
2627 if (is_string_literal(&e)) {
2628 /* either we are doing array of char, or we'll have to dig in */
2629 if (is_string) {
2630 *ep = e;
2631 goto String;
2633 return 0;
2635 /* struct or union can be initialized by compatible */
2636 if (class != TYPE_COMPOUND)
2637 return 0;
2638 type = evaluate_expression(e);
2639 if (!type)
2640 return 0;
2641 if (ctype->type == SYM_NODE)
2642 ctype = ctype->ctype.base_type;
2643 if (type->type == SYM_NODE)
2644 type = type->ctype.base_type;
2645 if (ctype == type)
2646 return 1;
2647 return 0;
2649 String:
2650 p = alloc_expression(e->pos, EXPR_STRING);
2651 *p = *e;
2652 type = evaluate_expression(p);
2653 if (ctype->bit_size != -1) {
2654 if (ctype->bit_size + bits_in_char < type->bit_size)
2655 warning(e->pos,
2656 "too long initializer-string for array of char");
2657 else if (Winit_cstring && ctype->bit_size + bits_in_char == type->bit_size) {
2658 warning(e->pos,
2659 "too long initializer-string for array of char(no space for nul char)");
2662 *ep = p;
2663 return 1;
2666 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2668 struct symbol *type;
2669 int class = classify_type(ctype, &type);
2670 if (!handle_simple_initializer(ep, 0, class, ctype))
2671 expression_error(*ep, "invalid initializer");
2674 static struct symbol *evaluate_cast(struct expression *expr)
2676 struct expression *target = expr->cast_expression;
2677 struct symbol *ctype;
2678 struct symbol *t1, *t2;
2679 int class1, class2;
2680 int as1 = 0, as2 = 0;
2682 if (!target)
2683 return NULL;
2686 * Special case: a cast can be followed by an
2687 * initializer, in which case we need to pass
2688 * the type value down to that initializer rather
2689 * than trying to evaluate it as an expression
2691 * A more complex case is when the initializer is
2692 * dereferenced as part of a post-fix expression.
2693 * We need to produce an expression that can be dereferenced.
2695 if (target->type == EXPR_INITIALIZER) {
2696 struct symbol *sym = expr->cast_type;
2697 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2699 sym->initializer = target;
2700 evaluate_symbol(sym);
2702 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2703 addr->symbol = sym;
2705 expr->type = EXPR_PREOP;
2706 expr->op = '*';
2707 expr->unop = addr;
2708 expr->ctype = sym;
2710 return sym;
2713 ctype = examine_symbol_type(expr->cast_type);
2714 expr->ctype = ctype;
2715 expr->cast_type = ctype;
2717 evaluate_expression(target);
2718 degenerate(target);
2720 class1 = classify_type(ctype, &t1);
2722 /* cast to non-integer type -> not an integer constant expression */
2723 if (!is_int(class1))
2724 expr->flags = 0;
2725 /* if argument turns out to be not an integer constant expression *and*
2726 it was not a floating literal to start with -> too bad */
2727 else if (expr->flags == Int_const_expr &&
2728 !(target->flags & Int_const_expr))
2729 expr->flags = 0;
2731 * You can always throw a value away by casting to
2732 * "void" - that's an implicit "force". Note that
2733 * the same is _not_ true of "void *".
2735 if (t1 == &void_ctype)
2736 goto out;
2738 if (class1 & (TYPE_COMPOUND | TYPE_FN))
2739 warning(expr->pos, "cast to non-scalar");
2741 t2 = target->ctype;
2742 if (!t2) {
2743 expression_error(expr, "cast from unknown type");
2744 goto out;
2746 class2 = classify_type(t2, &t2);
2748 if (class2 & TYPE_COMPOUND)
2749 warning(expr->pos, "cast from non-scalar");
2751 if (expr->type == EXPR_FORCE_CAST)
2752 goto out;
2754 /* allowed cast unfouls */
2755 if (class2 & TYPE_FOULED)
2756 t2 = unfoul(t2);
2758 if (t1 != t2) {
2759 if (class1 & TYPE_RESTRICT)
2760 warning(expr->pos, "cast to %s",
2761 show_typename(t1));
2762 if (class2 & TYPE_RESTRICT)
2763 warning(expr->pos, "cast from %s",
2764 show_typename(t2));
2767 if (t1 == &ulong_ctype)
2768 as1 = -1;
2769 else if (class1 == TYPE_PTR) {
2770 examine_pointer_target(t1);
2771 as1 = t1->ctype.as;
2774 if (t2 == &ulong_ctype)
2775 as2 = -1;
2776 else if (class2 == TYPE_PTR) {
2777 examine_pointer_target(t2);
2778 as2 = t2->ctype.as;
2781 if (!as1 && as2 > 0)
2782 warning(expr->pos, "cast removes address space of expression");
2783 if (as1 > 0 && as2 > 0 && as1 != as2)
2784 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2785 if (as1 > 0 && !as2 &&
2786 !is_null_pointer_constant(target) && Wcast_to_as)
2787 warning(expr->pos,
2788 "cast adds address space to expression (<asn:%d>)", as1);
2790 if (!(t1->ctype.modifiers & MOD_PTRINHERIT) && class1 == TYPE_PTR &&
2791 !as1 && (target->flags & Int_const_expr)) {
2792 if (t1->ctype.base_type == &void_ctype) {
2793 if (is_zero_constant(target)) {
2794 /* NULL */
2795 expr->type = EXPR_VALUE;
2796 expr->ctype = &null_ctype;
2797 expr->value = 0;
2798 return ctype;
2802 out:
2803 return ctype;
2807 * Evaluate a call expression with a symbol. This
2808 * should expand inline functions, and evaluate
2809 * builtins.
2811 static int evaluate_symbol_call(struct expression *expr)
2813 struct expression *fn = expr->fn;
2814 struct symbol *ctype = fn->ctype;
2816 if (fn->type != EXPR_PREOP)
2817 return 0;
2819 if (ctype->op && ctype->op->evaluate)
2820 return ctype->op->evaluate(expr);
2822 if (ctype->ctype.modifiers & MOD_INLINE) {
2823 int ret;
2824 struct symbol *curr = current_fn;
2826 if (ctype->definition)
2827 ctype = ctype->definition;
2829 current_fn = ctype->ctype.base_type;
2831 ret = inline_function(expr, ctype);
2833 /* restore the old function */
2834 current_fn = curr;
2835 return ret;
2838 return 0;
2841 static struct symbol *evaluate_call(struct expression *expr)
2843 int args, fnargs;
2844 struct symbol *ctype, *sym;
2845 struct expression *fn = expr->fn;
2846 struct expression_list *arglist = expr->args;
2848 if (!evaluate_expression(fn))
2849 return NULL;
2850 sym = ctype = fn->ctype;
2851 if (ctype->type == SYM_NODE)
2852 ctype = ctype->ctype.base_type;
2853 if (ctype->type == SYM_PTR)
2854 ctype = get_base_type(ctype);
2856 if (ctype->type != SYM_FN) {
2857 struct expression *arg;
2858 expression_error(expr, "not a function %s",
2859 show_ident(sym->ident));
2860 /* do typechecking in arguments */
2861 FOR_EACH_PTR (arglist, arg) {
2862 evaluate_expression(arg);
2863 } END_FOR_EACH_PTR(arg);
2864 return NULL;
2867 examine_fn_arguments(ctype);
2868 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
2869 sym->op && sym->op->args) {
2870 if (!sym->op->args(expr))
2871 return NULL;
2872 } else {
2873 if (!evaluate_arguments(sym, ctype, arglist))
2874 return NULL;
2875 args = expression_list_size(expr->args);
2876 fnargs = symbol_list_size(ctype->arguments);
2877 if (args < fnargs)
2878 expression_error(expr,
2879 "not enough arguments for function %s",
2880 show_ident(sym->ident));
2881 if (args > fnargs && !ctype->variadic)
2882 expression_error(expr,
2883 "too many arguments for function %s",
2884 show_ident(sym->ident));
2886 if (sym->type == SYM_NODE) {
2887 if (evaluate_symbol_call(expr))
2888 return expr->ctype;
2890 expr->ctype = ctype->ctype.base_type;
2891 return expr->ctype;
2894 static struct symbol *evaluate_offsetof(struct expression *expr)
2896 struct expression *e = expr->down;
2897 struct symbol *ctype = expr->in;
2898 int class;
2900 if (expr->op == '.') {
2901 struct symbol *field;
2902 int offset = 0;
2903 if (!ctype) {
2904 expression_error(expr, "expected structure or union");
2905 return NULL;
2907 examine_symbol_type(ctype);
2908 class = classify_type(ctype, &ctype);
2909 if (class != TYPE_COMPOUND) {
2910 expression_error(expr, "expected structure or union");
2911 return NULL;
2914 field = find_identifier(expr->ident, ctype->symbol_list, &offset);
2915 if (!field) {
2916 expression_error(expr, "unknown member");
2917 return NULL;
2919 ctype = field;
2920 expr->type = EXPR_VALUE;
2921 expr->flags = Int_const_expr;
2922 expr->value = offset;
2923 expr->taint = 0;
2924 expr->ctype = size_t_ctype;
2925 } else {
2926 if (!ctype) {
2927 expression_error(expr, "expected structure or union");
2928 return NULL;
2930 examine_symbol_type(ctype);
2931 class = classify_type(ctype, &ctype);
2932 if (class != (TYPE_COMPOUND | TYPE_PTR)) {
2933 expression_error(expr, "expected array");
2934 return NULL;
2936 ctype = ctype->ctype.base_type;
2937 if (!expr->index) {
2938 expr->type = EXPR_VALUE;
2939 expr->flags = Int_const_expr;
2940 expr->value = 0;
2941 expr->taint = 0;
2942 expr->ctype = size_t_ctype;
2943 } else {
2944 struct expression *idx = expr->index, *m;
2945 struct symbol *i_type = evaluate_expression(idx);
2946 int i_class = classify_type(i_type, &i_type);
2947 if (!is_int(i_class)) {
2948 expression_error(expr, "non-integer index");
2949 return NULL;
2951 unrestrict(idx, i_class, &i_type);
2952 idx = cast_to(idx, size_t_ctype);
2953 m = alloc_const_expression(expr->pos,
2954 bits_to_bytes(ctype->bit_size));
2955 m->ctype = size_t_ctype;
2956 m->flags = Int_const_expr;
2957 expr->type = EXPR_BINOP;
2958 expr->left = idx;
2959 expr->right = m;
2960 expr->op = '*';
2961 expr->ctype = size_t_ctype;
2962 expr->flags = m->flags & idx->flags & Int_const_expr;
2965 if (e) {
2966 struct expression *copy = __alloc_expression(0);
2967 *copy = *expr;
2968 if (e->type == EXPR_OFFSETOF)
2969 e->in = ctype;
2970 if (!evaluate_expression(e))
2971 return NULL;
2972 expr->type = EXPR_BINOP;
2973 expr->flags = e->flags & copy->flags & Int_const_expr;
2974 expr->op = '+';
2975 expr->ctype = size_t_ctype;
2976 expr->left = copy;
2977 expr->right = e;
2979 return size_t_ctype;
2982 struct symbol *evaluate_expression(struct expression *expr)
2984 if (!expr)
2985 return NULL;
2986 if (expr->ctype)
2987 return expr->ctype;
2989 switch (expr->type) {
2990 case EXPR_VALUE:
2991 case EXPR_FVALUE:
2992 expression_error(expr, "value expression without a type");
2993 return NULL;
2994 case EXPR_STRING:
2995 return evaluate_string(expr);
2996 case EXPR_SYMBOL:
2997 return evaluate_symbol_expression(expr);
2998 case EXPR_BINOP:
2999 if (!evaluate_expression(expr->left))
3000 return NULL;
3001 if (!evaluate_expression(expr->right))
3002 return NULL;
3003 return evaluate_binop(expr);
3004 case EXPR_LOGICAL:
3005 return evaluate_logical(expr);
3006 case EXPR_COMMA:
3007 evaluate_expression(expr->left);
3008 if (!evaluate_expression(expr->right))
3009 return NULL;
3010 return evaluate_comma(expr);
3011 case EXPR_COMPARE:
3012 if (!evaluate_expression(expr->left))
3013 return NULL;
3014 if (!evaluate_expression(expr->right))
3015 return NULL;
3016 return evaluate_compare(expr);
3017 case EXPR_ASSIGNMENT:
3018 if (!evaluate_expression(expr->left))
3019 return NULL;
3020 if (!evaluate_expression(expr->right))
3021 return NULL;
3022 return evaluate_assignment(expr);
3023 case EXPR_PREOP:
3024 if (!evaluate_expression(expr->unop))
3025 return NULL;
3026 return evaluate_preop(expr);
3027 case EXPR_POSTOP:
3028 if (!evaluate_expression(expr->unop))
3029 return NULL;
3030 return evaluate_postop(expr);
3031 case EXPR_CAST:
3032 case EXPR_FORCE_CAST:
3033 case EXPR_IMPLIED_CAST:
3034 return evaluate_cast(expr);
3035 case EXPR_SIZEOF:
3036 return evaluate_sizeof(expr);
3037 case EXPR_PTRSIZEOF:
3038 return evaluate_ptrsizeof(expr);
3039 case EXPR_ALIGNOF:
3040 return evaluate_alignof(expr);
3041 case EXPR_DEREF:
3042 return evaluate_member_dereference(expr);
3043 case EXPR_CALL:
3044 return evaluate_call(expr);
3045 case EXPR_SELECT:
3046 case EXPR_CONDITIONAL:
3047 return evaluate_conditional_expression(expr);
3048 case EXPR_STATEMENT:
3049 expr->ctype = evaluate_statement(expr->statement);
3050 return expr->ctype;
3052 case EXPR_LABEL:
3053 expr->ctype = &ptr_ctype;
3054 return &ptr_ctype;
3056 case EXPR_TYPE:
3057 /* Evaluate the type of the symbol .. */
3058 evaluate_symbol(expr->symbol);
3059 /* .. but the type of the _expression_ is a "type" */
3060 expr->ctype = &type_ctype;
3061 return &type_ctype;
3063 case EXPR_OFFSETOF:
3064 return evaluate_offsetof(expr);
3066 /* These can not exist as stand-alone expressions */
3067 case EXPR_INITIALIZER:
3068 case EXPR_IDENTIFIER:
3069 case EXPR_INDEX:
3070 case EXPR_POS:
3071 expression_error(expr, "internal front-end error: initializer in expression");
3072 return NULL;
3073 case EXPR_SLICE:
3074 expression_error(expr, "internal front-end error: SLICE re-evaluated");
3075 return NULL;
3077 return NULL;
3080 static void check_duplicates(struct symbol *sym)
3082 int declared = 0;
3083 struct symbol *next = sym;
3085 while ((next = next->same_symbol) != NULL) {
3086 const char *typediff;
3087 evaluate_symbol(next);
3088 declared++;
3089 typediff = type_difference(&sym->ctype, &next->ctype, 0, 0);
3090 if (typediff) {
3091 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
3092 show_ident(sym->ident),
3093 stream_name(next->pos.stream), next->pos.line, typediff);
3094 return;
3097 if (!declared) {
3098 unsigned long mod = sym->ctype.modifiers;
3099 if (mod & (MOD_STATIC | MOD_REGISTER))
3100 return;
3101 if (!(mod & MOD_TOPLEVEL))
3102 return;
3103 if (!Wdecl)
3104 return;
3105 if (sym->ident == &main_ident)
3106 return;
3107 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
3111 static struct symbol *evaluate_symbol(struct symbol *sym)
3113 struct symbol *base_type;
3115 if (!sym)
3116 return sym;
3117 if (sym->evaluated)
3118 return sym;
3119 sym->evaluated = 1;
3121 sym = examine_symbol_type(sym);
3122 base_type = get_base_type(sym);
3123 if (!base_type)
3124 return NULL;
3126 /* Evaluate the initializers */
3127 if (sym->initializer)
3128 evaluate_initializer(sym, &sym->initializer);
3130 /* And finally, evaluate the body of the symbol too */
3131 if (base_type->type == SYM_FN) {
3132 struct symbol *curr = current_fn;
3134 if (sym->definition && sym->definition != sym)
3135 return evaluate_symbol(sym->definition);
3137 current_fn = base_type;
3139 examine_fn_arguments(base_type);
3140 if (!base_type->stmt && base_type->inline_stmt)
3141 uninline(sym);
3142 if (base_type->stmt)
3143 evaluate_statement(base_type->stmt);
3145 current_fn = curr;
3148 return base_type;
3151 void evaluate_symbol_list(struct symbol_list *list)
3153 struct symbol *sym;
3155 FOR_EACH_PTR(list, sym) {
3156 evaluate_symbol(sym);
3157 check_duplicates(sym);
3158 } END_FOR_EACH_PTR(sym);
3161 static struct symbol *evaluate_return_expression(struct statement *stmt)
3163 struct expression *expr = stmt->expression;
3164 struct symbol *fntype;
3166 evaluate_expression(expr);
3167 fntype = current_fn->ctype.base_type;
3168 if (!fntype || fntype == &void_ctype) {
3169 if (expr && expr->ctype != &void_ctype)
3170 expression_error(expr, "return expression in %s function", fntype?"void":"typeless");
3171 if (expr && Wreturn_void)
3172 warning(stmt->pos, "returning void-valued expression");
3173 return NULL;
3176 if (!expr) {
3177 sparse_error(stmt->pos, "return with no return value");
3178 return NULL;
3180 if (!expr->ctype)
3181 return NULL;
3182 compatible_assignment_types(expr, fntype, &stmt->expression, "return expression");
3183 return NULL;
3186 static void evaluate_if_statement(struct statement *stmt)
3188 if (!stmt->if_conditional)
3189 return;
3191 evaluate_conditional(stmt->if_conditional, 0);
3192 evaluate_statement(stmt->if_true);
3193 evaluate_statement(stmt->if_false);
3196 static void evaluate_iterator(struct statement *stmt)
3198 evaluate_symbol_list(stmt->iterator_syms);
3199 evaluate_conditional(stmt->iterator_pre_condition, 1);
3200 evaluate_conditional(stmt->iterator_post_condition,1);
3201 evaluate_statement(stmt->iterator_pre_statement);
3202 evaluate_statement(stmt->iterator_statement);
3203 evaluate_statement(stmt->iterator_post_statement);
3206 static void verify_output_constraint(struct expression *expr, const char *constraint)
3208 switch (*constraint) {
3209 case '=': /* Assignment */
3210 case '+': /* Update */
3211 break;
3212 default:
3213 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
3217 static void verify_input_constraint(struct expression *expr, const char *constraint)
3219 switch (*constraint) {
3220 case '=': /* Assignment */
3221 case '+': /* Update */
3222 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
3226 static void evaluate_asm_statement(struct statement *stmt)
3228 struct expression *expr;
3229 struct symbol *sym;
3230 int state;
3232 expr = stmt->asm_string;
3233 if (!expr || expr->type != EXPR_STRING) {
3234 sparse_error(stmt->pos, "need constant string for inline asm");
3235 return;
3238 state = 0;
3239 FOR_EACH_PTR(stmt->asm_outputs, expr) {
3240 switch (state) {
3241 case 0: /* Identifier */
3242 state = 1;
3243 continue;
3245 case 1: /* Constraint */
3246 state = 2;
3247 if (!expr || expr->type != EXPR_STRING) {
3248 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
3249 *THIS_ADDRESS(expr) = NULL;
3250 continue;
3252 verify_output_constraint(expr, expr->string->data);
3253 continue;
3255 case 2: /* Expression */
3256 state = 0;
3257 if (!evaluate_expression(expr))
3258 return;
3259 if (!lvalue_expression(expr))
3260 warning(expr->pos, "asm output is not an lvalue");
3261 evaluate_assign_to(expr, expr->ctype);
3262 continue;
3264 } END_FOR_EACH_PTR(expr);
3266 state = 0;
3267 FOR_EACH_PTR(stmt->asm_inputs, expr) {
3268 switch (state) {
3269 case 0: /* Identifier */
3270 state = 1;
3271 continue;
3273 case 1: /* Constraint */
3274 state = 2;
3275 if (!expr || expr->type != EXPR_STRING) {
3276 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
3277 *THIS_ADDRESS(expr) = NULL;
3278 continue;
3280 verify_input_constraint(expr, expr->string->data);
3281 continue;
3283 case 2: /* Expression */
3284 state = 0;
3285 if (!evaluate_expression(expr))
3286 return;
3287 continue;
3289 } END_FOR_EACH_PTR(expr);
3291 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
3292 if (!expr) {
3293 sparse_error(stmt->pos, "bad asm clobbers");
3294 return;
3296 if (expr->type == EXPR_STRING)
3297 continue;
3298 expression_error(expr, "asm clobber is not a string");
3299 } END_FOR_EACH_PTR(expr);
3301 FOR_EACH_PTR(stmt->asm_labels, sym) {
3302 if (!sym || sym->type != SYM_LABEL) {
3303 sparse_error(stmt->pos, "bad asm label");
3304 return;
3306 } END_FOR_EACH_PTR(sym);
3309 static void evaluate_case_statement(struct statement *stmt)
3311 evaluate_expression(stmt->case_expression);
3312 evaluate_expression(stmt->case_to);
3313 evaluate_statement(stmt->case_statement);
3316 static void check_case_type(struct expression *switch_expr,
3317 struct expression *case_expr,
3318 struct expression **enumcase)
3320 struct symbol *switch_type, *case_type;
3321 int sclass, cclass;
3323 if (!case_expr)
3324 return;
3326 switch_type = switch_expr->ctype;
3327 case_type = evaluate_expression(case_expr);
3329 if (!switch_type || !case_type)
3330 goto Bad;
3331 if (enumcase) {
3332 if (*enumcase)
3333 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
3334 else if (is_enum_type(case_type))
3335 *enumcase = case_expr;
3338 sclass = classify_type(switch_type, &switch_type);
3339 cclass = classify_type(case_type, &case_type);
3341 /* both should be arithmetic */
3342 if (!(sclass & cclass & TYPE_NUM))
3343 goto Bad;
3345 /* neither should be floating */
3346 if ((sclass | cclass) & TYPE_FLOAT)
3347 goto Bad;
3349 /* if neither is restricted, we are OK */
3350 if (!((sclass | cclass) & TYPE_RESTRICT))
3351 return;
3353 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
3354 cclass, sclass, case_type, switch_type)) {
3355 unrestrict(case_expr, cclass, &case_type);
3356 unrestrict(switch_expr, sclass, &switch_type);
3358 return;
3360 Bad:
3361 expression_error(case_expr, "incompatible types for 'case' statement");
3364 static void evaluate_switch_statement(struct statement *stmt)
3366 struct symbol *sym;
3367 struct expression *enumcase = NULL;
3368 struct expression **enumcase_holder = &enumcase;
3369 struct expression *sel = stmt->switch_expression;
3371 evaluate_expression(sel);
3372 evaluate_statement(stmt->switch_statement);
3373 if (!sel)
3374 return;
3375 if (sel->ctype && is_enum_type(sel->ctype))
3376 enumcase_holder = NULL; /* Only check cases against switch */
3378 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
3379 struct statement *case_stmt = sym->stmt;
3380 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
3381 check_case_type(sel, case_stmt->case_to, enumcase_holder);
3382 } END_FOR_EACH_PTR(sym);
3385 static void evaluate_goto_statement(struct statement *stmt)
3387 struct symbol *label = stmt->goto_label;
3389 if (label && !label->stmt && !lookup_keyword(label->ident, NS_KEYWORD))
3390 sparse_error(stmt->pos, "label '%s' was not declared", show_ident(label->ident));
3392 evaluate_expression(stmt->goto_expression);
3395 struct symbol *evaluate_statement(struct statement *stmt)
3397 if (!stmt)
3398 return NULL;
3400 switch (stmt->type) {
3401 case STMT_DECLARATION: {
3402 struct symbol *s;
3403 FOR_EACH_PTR(stmt->declaration, s) {
3404 evaluate_symbol(s);
3405 } END_FOR_EACH_PTR(s);
3406 return NULL;
3409 case STMT_RETURN:
3410 return evaluate_return_expression(stmt);
3412 case STMT_EXPRESSION:
3413 if (!evaluate_expression(stmt->expression))
3414 return NULL;
3415 if (stmt->expression->ctype == &null_ctype)
3416 stmt->expression = cast_to(stmt->expression, &ptr_ctype);
3417 return degenerate(stmt->expression);
3419 case STMT_COMPOUND: {
3420 struct statement *s;
3421 struct symbol *type = NULL;
3423 /* Evaluate the return symbol in the compound statement */
3424 evaluate_symbol(stmt->ret);
3427 * Then, evaluate each statement, making the type of the
3428 * compound statement be the type of the last statement
3430 type = evaluate_statement(stmt->args);
3431 FOR_EACH_PTR(stmt->stmts, s) {
3432 type = evaluate_statement(s);
3433 } END_FOR_EACH_PTR(s);
3434 if (!type)
3435 type = &void_ctype;
3436 return type;
3438 case STMT_IF:
3439 evaluate_if_statement(stmt);
3440 return NULL;
3441 case STMT_ITERATOR:
3442 evaluate_iterator(stmt);
3443 return NULL;
3444 case STMT_SWITCH:
3445 evaluate_switch_statement(stmt);
3446 return NULL;
3447 case STMT_CASE:
3448 evaluate_case_statement(stmt);
3449 return NULL;
3450 case STMT_LABEL:
3451 return evaluate_statement(stmt->label_statement);
3452 case STMT_GOTO:
3453 evaluate_goto_statement(stmt);
3454 return NULL;
3455 case STMT_NONE:
3456 break;
3457 case STMT_ASM:
3458 evaluate_asm_statement(stmt);
3459 return NULL;
3460 case STMT_CONTEXT:
3461 evaluate_expression(stmt->expression);
3462 return NULL;
3463 case STMT_RANGE:
3464 evaluate_expression(stmt->range_expression);
3465 evaluate_expression(stmt->range_low);
3466 evaluate_expression(stmt->range_high);
3467 return NULL;
3469 return NULL;