sval: handle LEFTSHIFTs in sval_binop_overflows()
[smatch.git] / evaluate.c
blob153373084fb2ef0d3e448a4c3c38b1cf85beb1d7
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.attribute->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->attribute->as, as2 = c2->attribute->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;
638 for (;;) {
639 unsigned long diff;
640 int type;
641 struct symbol *base1 = t1->ctype.base_type;
642 struct symbol *base2 = t2->ctype.base_type;
645 * FIXME! Collect alignment and context too here!
647 if (move1) {
648 if (t1 && t1->type != SYM_PTR) {
649 mod1 |= t1->ctype.modifiers;
650 as1 |= t1->ctype.attribute->as;
652 move1 = 0;
655 if (move2) {
656 if (t2 && t2->type != SYM_PTR) {
657 mod2 |= t2->ctype.modifiers;
658 as2 |= t2->ctype.attribute->as;
660 move2 = 0;
663 if (t1 == t2)
664 break;
665 if (!t1 || !t2)
666 return "different types";
668 if (t1->type == SYM_NODE || t1->type == SYM_ENUM) {
669 t1 = base1;
670 move1 = 1;
671 if (!t1)
672 return "bad types";
673 continue;
676 if (t2->type == SYM_NODE || t2->type == SYM_ENUM) {
677 t2 = base2;
678 move2 = 1;
679 if (!t2)
680 return "bad types";
681 continue;
684 move1 = move2 = 1;
685 type = t1->type;
686 if (type != t2->type)
687 return "different base types";
689 switch (type) {
690 default:
691 sparse_error(t1->pos,
692 "internal error: bad type in derived(%d)",
693 type);
694 return "bad types";
695 case SYM_RESTRICT:
696 return "different base types";
697 case SYM_UNION:
698 case SYM_STRUCT:
699 /* allow definition of incomplete structs and unions */
700 if (t1->ident == t2->ident)
701 return NULL;
702 return "different base types";
703 case SYM_ARRAY:
704 /* XXX: we ought to compare sizes */
705 break;
706 case SYM_PTR:
707 if (as1 != as2)
708 return "different address spaces";
709 /* MOD_SPECIFIER is due to idiocy in parse.c */
710 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SPECIFIER)
711 return "different modifiers";
712 /* we could be lazier here */
713 base1 = examine_pointer_target(t1);
714 base2 = examine_pointer_target(t2);
715 mod1 = t1->ctype.modifiers;
716 as1 = t1->ctype.attribute->as;
717 mod2 = t2->ctype.modifiers;
718 as2 = t2->ctype.attribute->as;
719 break;
720 case SYM_FN: {
721 struct symbol *arg1, *arg2;
722 int i;
724 if (as1 != as2)
725 return "different address spaces";
726 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
727 return "different modifiers";
728 mod1 = t1->ctype.modifiers;
729 as1 = t1->ctype.attribute->as;
730 mod2 = t2->ctype.modifiers;
731 as2 = t2->ctype.attribute->as;
733 if (base1->variadic != base2->variadic)
734 return "incompatible variadic arguments";
735 examine_fn_arguments(t1);
736 examine_fn_arguments(t2);
737 PREPARE_PTR_LIST(t1->arguments, arg1);
738 PREPARE_PTR_LIST(t2->arguments, arg2);
739 i = 1;
740 for (;;) {
741 const char *diffstr;
742 if (!arg1 && !arg2)
743 break;
744 if (!arg1 || !arg2)
745 return "different argument counts";
746 diffstr = type_difference(&arg1->ctype,
747 &arg2->ctype,
748 MOD_IGN, MOD_IGN);
749 if (diffstr) {
750 static char argdiff[80];
751 sprintf(argdiff, "incompatible argument %d (%s)", i, diffstr);
752 return argdiff;
754 NEXT_PTR_LIST(arg1);
755 NEXT_PTR_LIST(arg2);
756 i++;
758 FINISH_PTR_LIST(arg2);
759 FINISH_PTR_LIST(arg1);
760 break;
762 case SYM_BASETYPE:
763 if (as1 != as2)
764 return "different address spaces";
765 if (base1 != base2)
766 return "different base types";
767 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
768 if (!diff)
769 return NULL;
770 if (diff & MOD_SIZE)
771 return "different type sizes";
772 else if (diff & ~MOD_SIGNEDNESS)
773 return "different modifiers";
774 else
775 return "different signedness";
777 t1 = base1;
778 t2 = base2;
780 if (as1 != as2)
781 return "different address spaces";
782 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
783 return "different modifiers";
784 return NULL;
787 static void bad_null(struct expression *expr)
789 if (Wnon_pointer_null)
790 warning(expr->pos, "Using plain integer as NULL pointer");
793 static unsigned long target_qualifiers(struct symbol *type)
795 unsigned long mod = type->ctype.modifiers & MOD_IGN;
796 if (type->ctype.base_type && type->ctype.base_type->type == SYM_ARRAY)
797 mod = 0;
798 return mod;
801 static struct symbol *evaluate_ptr_sub(struct expression *expr)
803 const char *typediff;
804 struct symbol *ltype, *rtype;
805 struct expression *l = expr->left;
806 struct expression *r = expr->right;
807 struct symbol *lbase;
809 classify_type(degenerate(l), &ltype);
810 classify_type(degenerate(r), &rtype);
812 lbase = examine_pointer_target(ltype);
813 examine_pointer_target(rtype);
814 typediff = type_difference(&ltype->ctype, &rtype->ctype,
815 target_qualifiers(rtype),
816 target_qualifiers(ltype));
817 if (typediff)
818 expression_error(expr, "subtraction of different types can't work (%s)", typediff);
820 if (is_function(lbase)) {
821 expression_error(expr, "subtraction of functions? Share your drugs");
822 return NULL;
825 expr->ctype = ssize_t_ctype;
826 if (lbase->bit_size > bits_in_char) {
827 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
828 struct expression *div = expr;
829 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
830 unsigned long value = bits_to_bytes(lbase->bit_size);
832 val->ctype = size_t_ctype;
833 val->value = value;
835 if (value & (value-1)) {
836 if (Wptr_subtraction_blows)
837 warning(expr->pos, "potentially expensive pointer subtraction");
840 sub->op = '-';
841 sub->ctype = ssize_t_ctype;
842 sub->left = l;
843 sub->right = r;
845 div->op = '/';
846 div->left = sub;
847 div->right = val;
850 return ssize_t_ctype;
853 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
855 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
857 struct symbol *ctype;
859 if (!expr)
860 return NULL;
862 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
863 warning(expr->pos, "assignment expression in conditional");
865 ctype = evaluate_expression(expr);
866 if (ctype) {
867 if (is_safe_type(ctype))
868 warning(expr->pos, "testing a 'safe expression'");
871 return ctype;
874 static struct symbol *evaluate_logical(struct expression *expr)
876 if (!evaluate_conditional(expr->left, 0))
877 return NULL;
878 if (!evaluate_conditional(expr->right, 0))
879 return NULL;
881 /* the result is int [6.5.13(3), 6.5.14(3)] */
882 expr->ctype = &int_ctype;
883 if (expr->flags) {
884 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
885 expr->flags = 0;
887 return &int_ctype;
890 static struct symbol *evaluate_binop(struct expression *expr)
892 struct symbol *ltype, *rtype, *ctype;
893 int lclass = classify_type(expr->left->ctype, &ltype);
894 int rclass = classify_type(expr->right->ctype, &rtype);
895 int op = expr->op;
897 if (expr->flags) {
898 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
899 expr->flags = 0;
902 /* number op number */
903 if (lclass & rclass & TYPE_NUM) {
904 if ((lclass | rclass) & TYPE_FLOAT) {
905 switch (op) {
906 case '+': case '-': case '*': case '/':
907 break;
908 default:
909 return bad_expr_type(expr);
913 if (op == SPECIAL_LEFTSHIFT || op == SPECIAL_RIGHTSHIFT) {
914 // shifts do integer promotions, but that's it.
915 unrestrict(expr->left, lclass, &ltype);
916 unrestrict(expr->right, rclass, &rtype);
917 ctype = ltype = integer_promotion(ltype);
918 rtype = integer_promotion(rtype);
919 } else {
920 // The rest do usual conversions
921 const unsigned left_not = expr->left->type == EXPR_PREOP
922 && expr->left->op == '!';
923 const unsigned right_not = expr->right->type == EXPR_PREOP
924 && expr->right->op == '!';
925 if ((op == '&' || op == '|') && (left_not || right_not))
926 warning(expr->pos, "dubious: %sx %c %sy",
927 left_not ? "!" : "",
929 right_not ? "!" : "");
931 ltype = usual_conversions(op, expr->left, expr->right,
932 lclass, rclass, ltype, rtype);
933 ctype = rtype = ltype;
936 expr->left = cast_to(expr->left, ltype);
937 expr->right = cast_to(expr->right, rtype);
938 expr->ctype = ctype;
939 return ctype;
942 /* pointer (+|-) integer */
943 if (lclass & TYPE_PTR && is_int(rclass) && (op == '+' || op == '-')) {
944 unrestrict(expr->right, rclass, &rtype);
945 return evaluate_ptr_add(expr, rtype);
948 /* integer + pointer */
949 if (rclass & TYPE_PTR && is_int(lclass) && op == '+') {
950 struct expression *index = expr->left;
951 unrestrict(index, lclass, &ltype);
952 expr->left = expr->right;
953 expr->right = index;
954 return evaluate_ptr_add(expr, ltype);
957 /* pointer - pointer */
958 if (lclass & rclass & TYPE_PTR && expr->op == '-')
959 return evaluate_ptr_sub(expr);
961 return bad_expr_type(expr);
964 static struct symbol *evaluate_comma(struct expression *expr)
966 expr->ctype = degenerate(expr->right);
967 if (expr->ctype == &null_ctype)
968 expr->ctype = &ptr_ctype;
969 expr->flags &= expr->left->flags & expr->right->flags;
970 return expr->ctype;
973 static int modify_for_unsigned(int op)
975 if (op == '<')
976 op = SPECIAL_UNSIGNED_LT;
977 else if (op == '>')
978 op = SPECIAL_UNSIGNED_GT;
979 else if (op == SPECIAL_LTE)
980 op = SPECIAL_UNSIGNED_LTE;
981 else if (op == SPECIAL_GTE)
982 op = SPECIAL_UNSIGNED_GTE;
983 return op;
986 static inline int is_null_pointer_constant(struct expression *e)
988 if (e->ctype == &null_ctype)
989 return 1;
990 if (!(e->flags & Int_const_expr))
991 return 0;
992 return is_zero_constant(e) ? 2 : 0;
995 static struct symbol *evaluate_compare(struct expression *expr)
997 struct expression *left = expr->left, *right = expr->right;
998 struct symbol *ltype, *rtype, *lbase, *rbase;
999 int lclass = classify_type(degenerate(left), &ltype);
1000 int rclass = classify_type(degenerate(right), &rtype);
1001 struct symbol *ctype;
1002 const char *typediff;
1004 if (expr->flags) {
1005 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
1006 expr->flags = 0;
1009 /* Type types? */
1010 if (is_type_type(ltype) && is_type_type(rtype))
1011 goto OK;
1013 if (is_safe_type(left->ctype) || is_safe_type(right->ctype))
1014 warning(expr->pos, "testing a 'safe expression'");
1016 /* number on number */
1017 if (lclass & rclass & TYPE_NUM) {
1018 ctype = usual_conversions(expr->op, expr->left, expr->right,
1019 lclass, rclass, ltype, rtype);
1020 expr->left = cast_to(expr->left, ctype);
1021 expr->right = cast_to(expr->right, ctype);
1022 if (ctype->ctype.modifiers & MOD_UNSIGNED)
1023 expr->op = modify_for_unsigned(expr->op);
1024 goto OK;
1027 /* at least one must be a pointer */
1028 if (!((lclass | rclass) & TYPE_PTR))
1029 return bad_expr_type(expr);
1031 /* equality comparisons can be with null pointer constants */
1032 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1033 int is_null1 = is_null_pointer_constant(left);
1034 int is_null2 = is_null_pointer_constant(right);
1035 if (is_null1 == 2)
1036 bad_null(left);
1037 if (is_null2 == 2)
1038 bad_null(right);
1039 if (is_null1 && is_null2) {
1040 int positive = expr->op == SPECIAL_EQUAL;
1041 expr->type = EXPR_VALUE;
1042 expr->value = positive;
1043 goto OK;
1045 if (is_null1 && (rclass & TYPE_PTR)) {
1046 left = cast_to(left, rtype);
1047 goto OK;
1049 if (is_null2 && (lclass & TYPE_PTR)) {
1050 right = cast_to(right, ltype);
1051 goto OK;
1054 /* both should be pointers */
1055 if (!(lclass & rclass & TYPE_PTR))
1056 return bad_expr_type(expr);
1057 expr->op = modify_for_unsigned(expr->op);
1059 lbase = examine_pointer_target(ltype);
1060 rbase = examine_pointer_target(rtype);
1062 /* they also have special treatment for pointers to void */
1063 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1064 if (ltype->ctype.attribute->as == rtype->ctype.attribute->as) {
1065 if (lbase == &void_ctype) {
1066 right = cast_to(right, ltype);
1067 goto OK;
1069 if (rbase == &void_ctype) {
1070 left = cast_to(left, rtype);
1071 goto OK;
1076 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1077 target_qualifiers(rtype),
1078 target_qualifiers(ltype));
1079 if (!typediff)
1080 goto OK;
1082 expression_error(expr, "incompatible types in comparison expression (%s)", typediff);
1083 return NULL;
1086 /* the result is int [6.5.8(6), 6.5.9(3)]*/
1087 expr->ctype = &int_ctype;
1088 return &int_ctype;
1092 * NOTE! The degenerate case of "x ? : y", where we don't
1093 * have a true case, this will possibly promote "x" to the
1094 * same type as "y", and thus _change_ the conditional
1095 * test in the expression. But since promotion is "safe"
1096 * for testing, that's OK.
1098 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1100 struct expression **true;
1101 struct symbol *ctype, *ltype, *rtype, *lbase, *rbase;
1102 int lclass, rclass;
1103 const char * typediff;
1104 int qual;
1106 if (!evaluate_conditional(expr->conditional, 0))
1107 return NULL;
1108 if (!evaluate_expression(expr->cond_false))
1109 return NULL;
1111 ctype = degenerate(expr->conditional);
1112 rtype = degenerate(expr->cond_false);
1114 true = &expr->conditional;
1115 ltype = ctype;
1116 if (expr->cond_true) {
1117 if (!evaluate_expression(expr->cond_true))
1118 return NULL;
1119 ltype = degenerate(expr->cond_true);
1120 true = &expr->cond_true;
1123 if (expr->flags) {
1124 int flags = expr->conditional->flags & Int_const_expr;
1125 flags &= (*true)->flags & expr->cond_false->flags;
1126 if (!flags)
1127 expr->flags = 0;
1130 lclass = classify_type(ltype, &ltype);
1131 rclass = classify_type(rtype, &rtype);
1132 if (lclass & rclass & TYPE_NUM) {
1133 ctype = usual_conversions('?', *true, expr->cond_false,
1134 lclass, rclass, ltype, rtype);
1135 *true = cast_to(*true, ctype);
1136 expr->cond_false = cast_to(expr->cond_false, ctype);
1137 goto out;
1140 if ((lclass | rclass) & TYPE_PTR) {
1141 int is_null1 = is_null_pointer_constant(*true);
1142 int is_null2 = is_null_pointer_constant(expr->cond_false);
1144 if (is_null1 && is_null2) {
1145 *true = cast_to(*true, &ptr_ctype);
1146 expr->cond_false = cast_to(expr->cond_false, &ptr_ctype);
1147 ctype = &ptr_ctype;
1148 goto out;
1150 if (is_null1 && (rclass & TYPE_PTR)) {
1151 if (is_null1 == 2)
1152 bad_null(*true);
1153 *true = cast_to(*true, rtype);
1154 ctype = rtype;
1155 goto out;
1157 if (is_null2 && (lclass & TYPE_PTR)) {
1158 if (is_null2 == 2)
1159 bad_null(expr->cond_false);
1160 expr->cond_false = cast_to(expr->cond_false, ltype);
1161 ctype = ltype;
1162 goto out;
1164 if (!(lclass & rclass & TYPE_PTR)) {
1165 typediff = "different types";
1166 goto Err;
1168 /* OK, it's pointer on pointer */
1169 if (ltype->ctype.attribute->as != rtype->ctype.attribute->as) {
1170 typediff = "different address spaces";
1171 goto Err;
1174 /* need to be lazier here */
1175 lbase = examine_pointer_target(ltype);
1176 rbase = examine_pointer_target(rtype);
1177 qual = target_qualifiers(ltype) | target_qualifiers(rtype);
1179 if (lbase == &void_ctype) {
1180 /* XXX: pointers to function should warn here */
1181 ctype = ltype;
1182 goto Qual;
1185 if (rbase == &void_ctype) {
1186 /* XXX: pointers to function should warn here */
1187 ctype = rtype;
1188 goto Qual;
1190 /* XXX: that should be pointer to composite */
1191 ctype = ltype;
1192 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1193 qual, qual);
1194 if (!typediff)
1195 goto Qual;
1196 goto Err;
1199 /* void on void, struct on same struct, union on same union */
1200 if (ltype == rtype) {
1201 ctype = ltype;
1202 goto out;
1204 typediff = "different base types";
1206 Err:
1207 expression_error(expr, "incompatible types in conditional expression (%s)", typediff);
1208 return NULL;
1210 out:
1211 expr->ctype = ctype;
1212 return ctype;
1214 Qual:
1215 if (qual & ~ctype->ctype.modifiers) {
1216 struct symbol *sym = alloc_symbol(ctype->pos, SYM_PTR);
1217 *sym = *ctype;
1218 sym->ctype.modifiers |= qual;
1219 ctype = sym;
1221 *true = cast_to(*true, ctype);
1222 expr->cond_false = cast_to(expr->cond_false, ctype);
1223 goto out;
1226 /* FP assignments can not do modulo or bit operations */
1227 static int compatible_float_op(int op)
1229 return op == SPECIAL_ADD_ASSIGN ||
1230 op == SPECIAL_SUB_ASSIGN ||
1231 op == SPECIAL_MUL_ASSIGN ||
1232 op == SPECIAL_DIV_ASSIGN;
1235 static int evaluate_assign_op(struct expression *expr)
1237 struct symbol *target = expr->left->ctype;
1238 struct symbol *source = expr->right->ctype;
1239 struct symbol *t, *s;
1240 int tclass = classify_type(target, &t);
1241 int sclass = classify_type(source, &s);
1242 int op = expr->op;
1244 if (tclass & sclass & TYPE_NUM) {
1245 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1246 expression_error(expr, "invalid assignment");
1247 return 0;
1249 if (tclass & TYPE_RESTRICT) {
1250 if (!restricted_binop(op, t)) {
1251 warning(expr->pos, "bad assignment (%s) to %s",
1252 show_special(op), show_typename(t));
1253 expr->right = cast_to(expr->right, target);
1254 return 0;
1256 /* allowed assignments unfoul */
1257 if (sclass & TYPE_FOULED && unfoul(s) == t)
1258 goto Cast;
1259 if (!restricted_value(expr->right, t))
1260 return 1;
1261 } else if (!(sclass & TYPE_RESTRICT))
1262 goto Cast;
1263 /* source and target would better be identical restricted */
1264 if (t == s)
1265 return 1;
1266 warning(expr->pos, "invalid assignment: %s", show_special(op));
1267 info(expr->pos, " left side has type %s", show_typename(t));
1268 info(expr->pos, " right side has type %s", show_typename(s));
1269 expr->right = cast_to(expr->right, target);
1270 return 0;
1272 if (tclass == TYPE_PTR && is_int(sclass)) {
1273 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1274 unrestrict(expr->right, sclass, &s);
1275 evaluate_ptr_add(expr, s);
1276 return 1;
1278 expression_error(expr, "invalid pointer assignment");
1279 return 0;
1282 expression_error(expr, "invalid assignment");
1283 return 0;
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.attribute->as != s->ctype.attribute->as) {
1359 *typediff = "different address spaces";
1360 return 0;
1362 if (mod2 & ~mod1) {
1363 *typediff = "different modifiers";
1364 return 0;
1366 goto Cast;
1368 /* It's OK if the target is more volatile or const than the source */
1369 *typediff = type_difference(&t->ctype, &s->ctype, 0, mod1);
1370 if (*typediff)
1371 return 0;
1372 return 1;
1375 if ((tclass & TYPE_COMPOUND) && s == t)
1376 return 1;
1378 if (tclass & TYPE_NUM) {
1379 /* XXX: need to turn into comparison with NULL */
1380 if (t == &bool_ctype && (sclass & TYPE_PTR))
1381 goto Cast;
1382 *typediff = "different base types";
1383 return 0;
1385 *typediff = "invalid types";
1386 return 0;
1388 Cast:
1389 *rp = cast_to(*rp, target);
1390 return 1;
1393 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1394 struct expression **rp, const char *where)
1396 const char *typediff;
1397 struct symbol *source = degenerate(*rp);
1399 if (!check_assignment_types(target, rp, &typediff)) {
1400 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1401 info(expr->pos, " expected %s", show_typename(target));
1402 info(expr->pos, " got %s", show_typename(source));
1403 *rp = cast_to(*rp, target);
1404 return 0;
1407 return 1;
1410 static int compatible_transparent_union(struct symbol *target,
1411 struct expression **rp)
1413 struct symbol *t, *member;
1414 classify_type(target, &t);
1415 if (t->type != SYM_UNION || !t->transparent_union)
1416 return 0;
1418 FOR_EACH_PTR(t->symbol_list, member) {
1419 const char *typediff;
1420 if (check_assignment_types(member, rp, &typediff))
1421 return 1;
1422 } END_FOR_EACH_PTR(member);
1424 return 0;
1427 static int compatible_argument_type(struct expression *expr, struct symbol *target,
1428 struct expression **rp, const char *where)
1430 if (compatible_transparent_union(target, rp))
1431 return 1;
1433 return compatible_assignment_types(expr, target, rp, where);
1436 static void mark_assigned(struct expression *expr)
1438 struct symbol *sym;
1440 if (!expr)
1441 return;
1442 switch (expr->type) {
1443 case EXPR_SYMBOL:
1444 sym = expr->symbol;
1445 if (!sym)
1446 return;
1447 if (sym->type != SYM_NODE)
1448 return;
1449 sym->ctype.modifiers |= MOD_ASSIGNED;
1450 return;
1452 case EXPR_BINOP:
1453 mark_assigned(expr->left);
1454 mark_assigned(expr->right);
1455 return;
1456 case EXPR_CAST:
1457 case EXPR_FORCE_CAST:
1458 mark_assigned(expr->cast_expression);
1459 return;
1460 case EXPR_SLICE:
1461 mark_assigned(expr->base);
1462 return;
1463 default:
1464 /* Hmm? */
1465 return;
1469 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1471 if (type->ctype.modifiers & MOD_CONST)
1472 expression_error(left, "assignment to const expression");
1474 /* We know left is an lvalue, so it's a "preop-*" */
1475 mark_assigned(left->unop);
1478 static struct symbol *evaluate_assignment(struct expression *expr)
1480 struct expression *left = expr->left;
1481 struct expression *where = expr;
1482 struct symbol *ltype;
1484 if (!lvalue_expression(left)) {
1485 expression_error(expr, "not an lvalue");
1486 return NULL;
1489 ltype = left->ctype;
1491 if (expr->op != '=') {
1492 if (!evaluate_assign_op(expr))
1493 return NULL;
1494 } else {
1495 if (!compatible_assignment_types(where, ltype, &expr->right, "assignment"))
1496 return NULL;
1499 evaluate_assign_to(left, ltype);
1501 expr->ctype = ltype;
1502 return ltype;
1505 static void examine_fn_arguments(struct symbol *fn)
1507 struct symbol *s;
1509 FOR_EACH_PTR(fn->arguments, s) {
1510 struct symbol *arg = evaluate_symbol(s);
1511 /* Array/function arguments silently degenerate into pointers */
1512 if (arg) {
1513 struct symbol *ptr;
1514 switch(arg->type) {
1515 case SYM_ARRAY:
1516 case SYM_FN:
1517 ptr = alloc_symbol(s->pos, SYM_PTR);
1518 if (arg->type == SYM_ARRAY)
1519 ptr->ctype = arg->ctype;
1520 else
1521 ptr->ctype.base_type = arg;
1522 merge_attr(&ptr->ctype, &s->ctype);
1523 ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1525 s->ctype.base_type = ptr;
1526 s->ctype.attribute = &null_attr;
1527 s->ctype.modifiers &= ~MOD_PTRINHERIT;
1528 s->bit_size = 0;
1529 s->examined = 0;
1530 examine_symbol_type(s);
1531 break;
1532 default:
1533 /* nothing */
1534 break;
1537 } END_FOR_EACH_PTR(s);
1540 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1542 /* Take the modifiers of the pointer, and apply them to the member */
1543 mod |= sym->ctype.modifiers;
1544 if (sym->ctype.attribute->as != as || sym->ctype.modifiers != mod) {
1545 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1546 *newsym = *sym;
1547 attr_set_as(&newsym->ctype, as);
1548 newsym->ctype.modifiers = mod;
1549 sym = newsym;
1551 return sym;
1554 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1556 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1557 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1559 node->ctype.base_type = ptr;
1560 ptr->bit_size = bits_in_pointer;
1561 ptr->ctype.alignment = pointer_alignment;
1563 node->bit_size = bits_in_pointer;
1564 node->ctype.alignment = pointer_alignment;
1566 access_symbol(sym);
1567 if (sym->ctype.modifiers & MOD_REGISTER) {
1568 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1569 sym->ctype.modifiers &= ~MOD_REGISTER;
1571 if (sym->type == SYM_NODE) {
1572 merge_attr(&ptr->ctype, &sym->ctype);
1573 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1574 sym = sym->ctype.base_type;
1576 if (degenerate && sym->type == SYM_ARRAY) {
1577 merge_attr(&ptr->ctype, &sym->ctype);
1578 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1579 sym = sym->ctype.base_type;
1581 ptr->ctype.base_type = sym;
1583 return node;
1586 /* Arrays degenerate into pointers on pointer arithmetic */
1587 static struct symbol *degenerate(struct expression *expr)
1589 struct symbol *ctype, *base;
1591 if (!expr)
1592 return NULL;
1593 ctype = expr->ctype;
1594 if (!ctype)
1595 return NULL;
1596 base = examine_symbol_type(ctype);
1597 if (ctype->type == SYM_NODE)
1598 base = ctype->ctype.base_type;
1600 * Arrays degenerate into pointers to the entries, while
1601 * functions degenerate into pointers to themselves.
1602 * If array was part of non-lvalue compound, we create a copy
1603 * of that compound first and then act as if we were dealing with
1604 * the corresponding field in there.
1606 switch (base->type) {
1607 case SYM_ARRAY:
1608 if (expr->type == EXPR_SLICE) {
1609 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1610 struct expression *e0, *e1, *e2, *e3, *e4;
1612 a->ctype.base_type = expr->base->ctype;
1613 a->bit_size = expr->base->ctype->bit_size;
1614 a->array_size = expr->base->ctype->array_size;
1616 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1617 e0->symbol = a;
1618 e0->ctype = &lazy_ptr_ctype;
1620 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1621 e1->unop = e0;
1622 e1->op = '*';
1623 e1->ctype = expr->base->ctype; /* XXX */
1625 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1626 e2->left = e1;
1627 e2->right = expr->base;
1628 e2->op = '=';
1629 e2->ctype = expr->base->ctype;
1631 if (expr->r_bitpos) {
1632 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1633 e3->op = '+';
1634 e3->left = e0;
1635 e3->right = alloc_const_expression(expr->pos,
1636 bits_to_bytes(expr->r_bitpos));
1637 e3->ctype = &lazy_ptr_ctype;
1638 } else {
1639 e3 = e0;
1642 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1643 e4->left = e2;
1644 e4->right = e3;
1645 e4->ctype = &lazy_ptr_ctype;
1647 expr->unop = e4;
1648 expr->type = EXPR_PREOP;
1649 expr->op = '*';
1651 case SYM_FN:
1652 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1653 expression_error(expr, "strange non-value function or array");
1654 return &bad_ctype;
1656 *expr = *expr->unop;
1657 ctype = create_pointer(expr, ctype, 1);
1658 expr->ctype = ctype;
1659 default:
1660 /* nothing */;
1662 return ctype;
1665 static struct symbol *evaluate_addressof(struct expression *expr)
1667 struct expression *op = expr->unop;
1668 struct symbol *ctype;
1670 if (op->op != '*' || op->type != EXPR_PREOP) {
1671 expression_error(expr, "not addressable");
1672 return NULL;
1674 ctype = op->ctype;
1675 *expr = *op->unop;
1676 expr->flags = 0;
1678 if (expr->type == EXPR_SYMBOL) {
1679 struct symbol *sym = expr->symbol;
1680 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1684 * symbol expression evaluation is lazy about the type
1685 * of the sub-expression, so we may have to generate
1686 * the type here if so..
1688 if (expr->ctype == &lazy_ptr_ctype) {
1689 ctype = create_pointer(expr, ctype, 0);
1690 expr->ctype = ctype;
1692 return expr->ctype;
1696 static struct symbol *evaluate_dereference(struct expression *expr)
1698 struct expression *op = expr->unop;
1699 struct symbol *ctype = op->ctype, *node, *target;
1701 /* Simplify: *&(expr) => (expr) */
1702 if (op->type == EXPR_PREOP && op->op == '&') {
1703 *expr = *op->unop;
1704 expr->flags = 0;
1705 return expr->ctype;
1708 /* Dereferencing a node drops all the node information. */
1709 if (ctype->type == SYM_NODE)
1710 ctype = ctype->ctype.base_type;
1712 node = alloc_symbol(expr->pos, SYM_NODE);
1713 target = ctype->ctype.base_type;
1715 switch (ctype->type) {
1716 default:
1717 expression_error(expr, "cannot dereference this type");
1718 return NULL;
1719 case SYM_PTR:
1720 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1721 merge_type(node, ctype);
1722 break;
1724 case SYM_ARRAY:
1725 if (!lvalue_expression(op)) {
1726 expression_error(op, "non-lvalue array??");
1727 return NULL;
1730 /* Do the implied "addressof" on the array */
1731 *op = *op->unop;
1734 * When an array is dereferenced, we need to pick
1735 * up the attributes of the original node too..
1737 merge_type(node, op->ctype);
1738 merge_type(node, ctype);
1739 break;
1742 node->bit_size = target->bit_size;
1743 node->array_size = target->array_size;
1745 expr->ctype = node;
1746 return node;
1750 * Unary post-ops: x++ and x--
1752 static struct symbol *evaluate_postop(struct expression *expr)
1754 struct expression *op = expr->unop;
1755 struct symbol *ctype = op->ctype;
1756 int class = classify_type(ctype, &ctype);
1757 int multiply = 0;
1759 if (!class || class & TYPE_COMPOUND) {
1760 expression_error(expr, "need scalar for ++/--");
1761 return NULL;
1763 if (!lvalue_expression(expr->unop)) {
1764 expression_error(expr, "need lvalue expression for ++/--");
1765 return NULL;
1768 if ((class & TYPE_RESTRICT) && restricted_unop(expr->op, &ctype))
1769 unrestrict(expr, class, &ctype);
1771 if (class & TYPE_NUM) {
1772 multiply = 1;
1773 } else if (class == TYPE_PTR) {
1774 struct symbol *target = examine_pointer_target(ctype);
1775 if (!is_function(target))
1776 multiply = bits_to_bytes(target->bit_size);
1779 if (multiply) {
1780 evaluate_assign_to(op, op->ctype);
1781 expr->op_value = multiply;
1782 expr->ctype = ctype;
1783 return ctype;
1786 expression_error(expr, "bad argument type for ++/--");
1787 return NULL;
1790 static struct symbol *evaluate_sign(struct expression *expr)
1792 struct symbol *ctype = expr->unop->ctype;
1793 int class = classify_type(ctype, &ctype);
1794 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1795 expr->flags = 0;
1796 /* should be an arithmetic type */
1797 if (!(class & TYPE_NUM))
1798 return bad_expr_type(expr);
1799 if (class & TYPE_RESTRICT)
1800 goto Restr;
1801 Normal:
1802 if (!(class & TYPE_FLOAT)) {
1803 ctype = integer_promotion(ctype);
1804 expr->unop = cast_to(expr->unop, ctype);
1805 } else if (expr->op != '~') {
1806 /* no conversions needed */
1807 } else {
1808 return bad_expr_type(expr);
1810 if (expr->op == '+')
1811 *expr = *expr->unop;
1812 expr->ctype = ctype;
1813 return ctype;
1814 Restr:
1815 if (restricted_unop(expr->op, &ctype))
1816 unrestrict(expr, class, &ctype);
1817 goto Normal;
1820 static struct symbol *evaluate_preop(struct expression *expr)
1822 struct symbol *ctype = expr->unop->ctype;
1824 switch (expr->op) {
1825 case '(':
1826 *expr = *expr->unop;
1827 return ctype;
1829 case '+':
1830 case '-':
1831 case '~':
1832 return evaluate_sign(expr);
1834 case '*':
1835 return evaluate_dereference(expr);
1837 case '&':
1838 return evaluate_addressof(expr);
1840 case SPECIAL_INCREMENT:
1841 case SPECIAL_DECREMENT:
1843 * From a type evaluation standpoint the preops are
1844 * the same as the postops
1846 return evaluate_postop(expr);
1848 case '!':
1849 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1850 expr->flags = 0;
1851 if (is_safe_type(ctype))
1852 warning(expr->pos, "testing a 'safe expression'");
1853 if (is_float_type(ctype)) {
1854 struct expression *arg = expr->unop;
1855 expr->type = EXPR_COMPARE;
1856 expr->op = SPECIAL_EQUAL;
1857 expr->left = arg;
1858 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1859 expr->right->ctype = ctype;
1860 expr->right->fvalue = 0;
1861 } else if (is_fouled_type(ctype)) {
1862 warning(expr->pos, "%s degrades to integer",
1863 show_typename(ctype->ctype.base_type));
1865 /* the result is int [6.5.3.3(5)]*/
1866 ctype = &int_ctype;
1867 break;
1869 default:
1870 break;
1872 expr->ctype = ctype;
1873 return ctype;
1876 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1878 struct ptr_list *head = (struct ptr_list *)_list;
1879 struct ptr_list *list = head;
1881 if (!head)
1882 return NULL;
1883 do {
1884 int i;
1885 for (i = 0; i < list->nr; i++) {
1886 struct symbol *sym = (struct symbol *) list->list[i];
1887 if (sym->ident) {
1888 if (sym->ident != ident)
1889 continue;
1890 *offset = sym->offset;
1891 return sym;
1892 } else {
1893 struct symbol *ctype = sym->ctype.base_type;
1894 struct symbol *sub;
1895 if (!ctype)
1896 continue;
1897 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1898 continue;
1899 sub = find_identifier(ident, ctype->symbol_list, offset);
1900 if (!sub)
1901 continue;
1902 *offset += sym->offset;
1903 return sub;
1906 } while ((list = list->next) != head);
1907 return NULL;
1910 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1912 struct expression *add;
1915 * Create a new add-expression
1917 * NOTE! Even if we just add zero, we need a new node
1918 * for the member pointer, since it has a different
1919 * type than the original pointer. We could make that
1920 * be just a cast, but the fact is, a node is a node,
1921 * so we might as well just do the "add zero" here.
1923 add = alloc_expression(expr->pos, EXPR_BINOP);
1924 add->op = '+';
1925 add->left = expr;
1926 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1927 add->right->ctype = &int_ctype;
1928 add->right->value = offset;
1931 * The ctype of the pointer will be lazily evaluated if
1932 * we ever take the address of this member dereference..
1934 add->ctype = &lazy_ptr_ctype;
1935 return add;
1938 /* structure/union dereference */
1939 static struct symbol *evaluate_member_dereference(struct expression *expr)
1941 int offset;
1942 struct symbol *ctype, *member;
1943 struct expression *deref = expr->deref, *add;
1944 struct ident *ident = expr->member;
1945 unsigned int mod;
1946 int address_space;
1948 if (!evaluate_expression(deref))
1949 return NULL;
1950 if (!ident) {
1951 expression_error(expr, "bad member name");
1952 return NULL;
1955 ctype = deref->ctype;
1956 examine_symbol_type(ctype);
1957 address_space = ctype->ctype.attribute->as;
1958 mod = ctype->ctype.modifiers;
1959 if (ctype->type == SYM_NODE) {
1960 ctype = ctype->ctype.base_type;
1961 address_space |= ctype->ctype.attribute->as;
1962 mod |= ctype->ctype.modifiers;
1964 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1965 expression_error(expr, "expected structure or union");
1966 return NULL;
1968 offset = 0;
1969 member = find_identifier(ident, ctype->symbol_list, &offset);
1970 if (!member) {
1971 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1972 const char *name = "<unnamed>";
1973 int namelen = 9;
1974 if (ctype->ident) {
1975 name = ctype->ident->name;
1976 namelen = ctype->ident->len;
1978 if (ctype->symbol_list)
1979 expression_error(expr, "no member '%s' in %s %.*s",
1980 show_ident(ident), type, namelen, name);
1981 else
1982 expression_error(expr, "using member '%s' in "
1983 "incomplete %s %.*s", show_ident(ident),
1984 type, namelen, name);
1985 return NULL;
1989 * The member needs to take on the address space and modifiers of
1990 * the "parent" type.
1992 member = convert_to_as_mod(member, address_space, mod);
1993 ctype = get_base_type(member);
1995 if (!lvalue_expression(deref)) {
1996 if (deref->type != EXPR_SLICE) {
1997 expr->base = deref;
1998 expr->r_bitpos = 0;
1999 } else {
2000 expr->base = deref->base;
2001 expr->r_bitpos = deref->r_bitpos;
2003 expr->r_bitpos += bytes_to_bits(offset);
2004 expr->type = EXPR_SLICE;
2005 expr->r_nrbits = member->bit_size;
2006 expr->r_bitpos += member->bit_offset;
2007 expr->ctype = member;
2008 return member;
2011 deref = deref->unop;
2012 expr->deref = deref;
2014 add = evaluate_offset(deref, offset);
2015 expr->type = EXPR_PREOP;
2016 expr->op = '*';
2017 expr->unop = add;
2019 expr->ctype = member;
2020 return member;
2023 static int is_promoted(struct expression *expr)
2025 while (1) {
2026 switch (expr->type) {
2027 case EXPR_BINOP:
2028 case EXPR_SELECT:
2029 case EXPR_CONDITIONAL:
2030 return 1;
2031 case EXPR_COMMA:
2032 expr = expr->right;
2033 continue;
2034 case EXPR_PREOP:
2035 switch (expr->op) {
2036 case '(':
2037 expr = expr->unop;
2038 continue;
2039 case '+':
2040 case '-':
2041 case '~':
2042 return 1;
2043 default:
2044 return 0;
2046 default:
2047 return 0;
2053 static struct symbol *evaluate_cast(struct expression *);
2055 static struct symbol *evaluate_type_information(struct expression *expr)
2057 struct symbol *sym = expr->cast_type;
2058 if (!sym) {
2059 sym = evaluate_expression(expr->cast_expression);
2060 if (!sym)
2061 return NULL;
2063 * Expressions of restricted types will possibly get
2064 * promoted - check that here
2066 if (is_restricted_type(sym)) {
2067 if (sym->bit_size < bits_in_int && is_promoted(expr))
2068 sym = &int_ctype;
2069 } else if (is_fouled_type(sym)) {
2070 sym = &int_ctype;
2073 examine_symbol_type(sym);
2074 if (is_bitfield_type(sym)) {
2075 expression_error(expr, "trying to examine bitfield type");
2076 return NULL;
2078 return sym;
2081 static struct symbol *evaluate_sizeof(struct expression *expr)
2083 struct symbol *type;
2084 int size;
2086 type = evaluate_type_information(expr);
2087 if (!type)
2088 return NULL;
2090 size = type->bit_size;
2092 if (size < 0 && is_void_type(type)) {
2093 warning(expr->pos, "expression using sizeof(void)");
2094 size = bits_in_char;
2097 if (size == 1 && is_bool_type(type)) {
2098 if (Wsizeof_bool)
2099 warning(expr->pos, "expression using sizeof bool");
2100 size = bits_in_char;
2103 if (is_function(type->ctype.base_type)) {
2104 warning(expr->pos, "expression using sizeof on a function");
2105 size = bits_in_char;
2108 if ((size < 0) || (size & (bits_in_char - 1)))
2109 expression_error(expr, "cannot size expression");
2111 expr->type = EXPR_VALUE;
2112 expr->value = bits_to_bytes(size);
2113 expr->taint = 0;
2114 expr->ctype = size_t_ctype;
2115 return size_t_ctype;
2118 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
2120 struct symbol *type;
2121 int size;
2123 type = evaluate_type_information(expr);
2124 if (!type)
2125 return NULL;
2127 if (type->type == SYM_NODE)
2128 type = type->ctype.base_type;
2129 if (!type)
2130 return NULL;
2131 switch (type->type) {
2132 case SYM_ARRAY:
2133 break;
2134 case SYM_PTR:
2135 type = get_base_type(type);
2136 if (type)
2137 break;
2138 default:
2139 expression_error(expr, "expected pointer expression");
2140 return NULL;
2142 size = type->bit_size;
2143 if (size & (bits_in_char-1))
2144 size = 0;
2145 expr->type = EXPR_VALUE;
2146 expr->value = bits_to_bytes(size);
2147 expr->taint = 0;
2148 expr->ctype = size_t_ctype;
2149 return size_t_ctype;
2152 static struct symbol *evaluate_alignof(struct expression *expr)
2154 struct symbol *type;
2156 type = evaluate_type_information(expr);
2157 if (!type)
2158 return NULL;
2160 expr->type = EXPR_VALUE;
2161 expr->value = type->ctype.alignment;
2162 expr->taint = 0;
2163 expr->ctype = size_t_ctype;
2164 return size_t_ctype;
2167 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
2169 struct expression *expr;
2170 struct symbol_list *argument_types = fn->arguments;
2171 struct symbol *argtype;
2172 int i = 1;
2174 PREPARE_PTR_LIST(argument_types, argtype);
2175 FOR_EACH_PTR (head, expr) {
2176 struct expression **p = THIS_ADDRESS(expr);
2177 struct symbol *ctype, *target;
2178 ctype = evaluate_expression(expr);
2180 if (!ctype)
2181 return 0;
2183 target = argtype;
2184 if (!target) {
2185 struct symbol *type;
2186 int class = classify_type(ctype, &type);
2187 if (is_int(class)) {
2188 *p = cast_to(expr, integer_promotion(type));
2189 } else if (class & TYPE_FLOAT) {
2190 unsigned long mod = type->ctype.modifiers;
2191 if (!(mod & (MOD_LONG_ALL)))
2192 *p = cast_to(expr, &double_ctype);
2193 } else if (class & TYPE_PTR) {
2194 if (expr->ctype == &null_ctype)
2195 *p = cast_to(expr, &ptr_ctype);
2196 else
2197 degenerate(expr);
2199 } else if (!target->forced_arg){
2200 static char where[30];
2201 examine_symbol_type(target);
2202 sprintf(where, "argument %d", i);
2203 compatible_argument_type(expr, target, p, where);
2206 i++;
2207 NEXT_PTR_LIST(argtype);
2208 } END_FOR_EACH_PTR(expr);
2209 FINISH_PTR_LIST(argtype);
2210 return 1;
2213 static void convert_index(struct expression *e)
2215 struct expression *child = e->idx_expression;
2216 unsigned from = e->idx_from;
2217 unsigned to = e->idx_to + 1;
2218 e->type = EXPR_POS;
2219 e->init_offset = from * bits_to_bytes(e->ctype->bit_size);
2220 e->init_nr = to - from;
2221 e->init_expr = child;
2224 static void convert_ident(struct expression *e)
2226 struct expression *child = e->ident_expression;
2227 int offset = e->offset;
2229 e->type = EXPR_POS;
2230 e->init_offset = offset;
2231 e->init_nr = 1;
2232 e->init_expr = child;
2235 static void convert_designators(struct expression *e)
2237 while (e) {
2238 if (e->type == EXPR_INDEX)
2239 convert_index(e);
2240 else if (e->type == EXPR_IDENTIFIER)
2241 convert_ident(e);
2242 else
2243 break;
2244 e = e->init_expr;
2248 static void excess(struct expression *e, const char *s)
2250 warning(e->pos, "excessive elements in %s initializer", s);
2254 * implicit designator for the first element
2256 static struct expression *first_subobject(struct symbol *ctype, int class,
2257 struct expression **v)
2259 struct expression *e = *v, *new;
2261 if (ctype->type == SYM_NODE)
2262 ctype = ctype->ctype.base_type;
2264 if (class & TYPE_PTR) { /* array */
2265 if (!ctype->bit_size)
2266 return NULL;
2267 new = alloc_expression(e->pos, EXPR_INDEX);
2268 new->idx_expression = e;
2269 new->ctype = ctype->ctype.base_type;
2270 } else {
2271 struct symbol *field, *p;
2272 PREPARE_PTR_LIST(ctype->symbol_list, p);
2273 while (p && !p->ident && is_bitfield_type(p))
2274 NEXT_PTR_LIST(p);
2275 field = p;
2276 FINISH_PTR_LIST(p);
2277 if (!field)
2278 return NULL;
2279 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2280 new->ident_expression = e;
2281 new->field = new->ctype = field;
2282 new->offset = field->offset;
2284 *v = new;
2285 return new;
2289 * sanity-check explicit designators; return the innermost one or NULL
2290 * in case of error. Assign types.
2292 static struct expression *check_designators(struct expression *e,
2293 struct symbol *ctype)
2295 struct expression *last = NULL;
2296 const char *err;
2297 while (1) {
2298 if (ctype->type == SYM_NODE)
2299 ctype = ctype->ctype.base_type;
2300 if (e->type == EXPR_INDEX) {
2301 struct symbol *type;
2302 if (ctype->type != SYM_ARRAY) {
2303 err = "array index in non-array";
2304 break;
2306 type = ctype->ctype.base_type;
2307 if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2308 unsigned offset = array_element_offset(type->bit_size, e->idx_to);
2309 if (offset >= ctype->bit_size) {
2310 err = "index out of bounds in";
2311 break;
2314 e->ctype = ctype = type;
2315 ctype = type;
2316 last = e;
2317 if (!e->idx_expression) {
2318 err = "invalid";
2319 break;
2321 e = e->idx_expression;
2322 } else if (e->type == EXPR_IDENTIFIER) {
2323 int offset = 0;
2324 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2325 err = "field name not in struct or union";
2326 break;
2328 ctype = find_identifier(e->expr_ident, ctype->symbol_list, &offset);
2329 if (!ctype) {
2330 err = "unknown field name in";
2331 break;
2333 e->offset = offset;
2334 e->field = e->ctype = ctype;
2335 last = e;
2336 if (!e->ident_expression) {
2337 err = "invalid";
2338 break;
2340 e = e->ident_expression;
2341 } else if (e->type == EXPR_POS) {
2342 err = "internal front-end error: EXPR_POS in";
2343 break;
2344 } else
2345 return last;
2347 expression_error(e, "%s initializer", err);
2348 return NULL;
2352 * choose the next subobject to initialize.
2354 * Get designators for next element, switch old ones to EXPR_POS.
2355 * Return the resulting expression or NULL if we'd run out of subobjects.
2356 * The innermost designator is returned in *v. Designators in old
2357 * are assumed to be already sanity-checked.
2359 static struct expression *next_designators(struct expression *old,
2360 struct symbol *ctype,
2361 struct expression *e, struct expression **v)
2363 struct expression *new = NULL;
2365 if (!old)
2366 return NULL;
2367 if (old->type == EXPR_INDEX) {
2368 struct expression *copy;
2369 unsigned n;
2371 copy = next_designators(old->idx_expression,
2372 old->ctype, e, v);
2373 if (!copy) {
2374 n = old->idx_to + 1;
2375 if (n * old->ctype->bit_size == ctype->bit_size) {
2376 convert_index(old);
2377 return NULL;
2379 copy = e;
2380 *v = new = alloc_expression(e->pos, EXPR_INDEX);
2381 } else {
2382 n = old->idx_to;
2383 new = alloc_expression(e->pos, EXPR_INDEX);
2386 new->idx_from = new->idx_to = n;
2387 new->idx_expression = copy;
2388 new->ctype = old->ctype;
2389 convert_index(old);
2390 } else if (old->type == EXPR_IDENTIFIER) {
2391 struct expression *copy;
2392 struct symbol *field;
2393 int offset = 0;
2395 copy = next_designators(old->ident_expression,
2396 old->ctype, e, v);
2397 if (!copy) {
2398 field = old->field->next_subobject;
2399 if (!field) {
2400 convert_ident(old);
2401 return NULL;
2403 copy = e;
2404 *v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2406 * We can't necessarily trust "field->offset",
2407 * because the field might be in an anonymous
2408 * union, and the field offset is then the offset
2409 * within that union.
2411 * The "old->offset - old->field->offset"
2412 * would be the offset of such an anonymous
2413 * union.
2415 offset = old->offset - old->field->offset;
2416 } else {
2417 field = old->field;
2418 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2421 new->field = field;
2422 new->expr_ident = field->ident;
2423 new->ident_expression = copy;
2424 new->ctype = field;
2425 new->offset = field->offset + offset;
2426 convert_ident(old);
2428 return new;
2431 static int handle_simple_initializer(struct expression **ep, int nested,
2432 int class, struct symbol *ctype);
2435 * deal with traversing subobjects [6.7.8(17,18,20)]
2437 static void handle_list_initializer(struct expression *expr,
2438 int class, struct symbol *ctype)
2440 struct expression *e, *last = NULL, *top = NULL, *next;
2441 int jumped = 0;
2443 FOR_EACH_PTR(expr->expr_list, e) {
2444 struct expression **v;
2445 struct symbol *type;
2446 int lclass;
2448 if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2449 struct symbol *struct_sym;
2450 if (!top) {
2451 top = e;
2452 last = first_subobject(ctype, class, &top);
2453 } else {
2454 last = next_designators(last, ctype, e, &top);
2456 if (!last) {
2457 excess(e, class & TYPE_PTR ? "array" :
2458 "struct or union");
2459 DELETE_CURRENT_PTR(e);
2460 continue;
2462 struct_sym = ctype->type == SYM_NODE ? ctype->ctype.base_type : ctype;
2463 if (Wdesignated_init && struct_sym->designated_init)
2464 warning(e->pos, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2465 ctype->ident ? "in initializer for " : "",
2466 ctype->ident ? ctype->ident->len : 0,
2467 ctype->ident ? ctype->ident->name : "",
2468 ctype->ident ? ": " : "",
2469 get_type_name(struct_sym->type),
2470 show_ident(struct_sym->ident));
2471 if (jumped) {
2472 warning(e->pos, "advancing past deep designator");
2473 jumped = 0;
2475 REPLACE_CURRENT_PTR(e, last);
2476 } else {
2477 next = check_designators(e, ctype);
2478 if (!next) {
2479 DELETE_CURRENT_PTR(e);
2480 continue;
2482 top = next;
2483 /* deeper than one designator? */
2484 jumped = top != e;
2485 convert_designators(last);
2486 last = e;
2489 found:
2490 lclass = classify_type(top->ctype, &type);
2491 if (top->type == EXPR_INDEX)
2492 v = &top->idx_expression;
2493 else
2494 v = &top->ident_expression;
2496 if (handle_simple_initializer(v, 1, lclass, top->ctype))
2497 continue;
2499 if (!(lclass & TYPE_COMPOUND)) {
2500 warning(e->pos, "bogus scalar initializer");
2501 DELETE_CURRENT_PTR(e);
2502 continue;
2505 next = first_subobject(type, lclass, v);
2506 if (next) {
2507 warning(e->pos, "missing braces around initializer");
2508 top = next;
2509 goto found;
2512 DELETE_CURRENT_PTR(e);
2513 excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2515 } END_FOR_EACH_PTR(e);
2517 convert_designators(last);
2518 expr->ctype = ctype;
2521 static int is_string_literal(struct expression **v)
2523 struct expression *e = *v;
2524 while (e && e->type == EXPR_PREOP && e->op == '(')
2525 e = e->unop;
2526 if (!e || e->type != EXPR_STRING)
2527 return 0;
2528 if (e != *v && Wparen_string)
2529 warning(e->pos,
2530 "array initialized from parenthesized string constant");
2531 *v = e;
2532 return 1;
2536 * We want a normal expression, possibly in one layer of braces. Warn
2537 * if the latter happens inside a list (it's legal, but likely to be
2538 * an effect of screwup). In case of anything not legal, we are definitely
2539 * having an effect of screwup, so just fail and let the caller warn.
2541 static struct expression *handle_scalar(struct expression *e, int nested)
2543 struct expression *v = NULL, *p;
2544 int count = 0;
2546 /* normal case */
2547 if (e->type != EXPR_INITIALIZER)
2548 return e;
2550 FOR_EACH_PTR(e->expr_list, p) {
2551 if (!v)
2552 v = p;
2553 count++;
2554 } END_FOR_EACH_PTR(p);
2555 if (count != 1)
2556 return NULL;
2557 switch(v->type) {
2558 case EXPR_INITIALIZER:
2559 case EXPR_INDEX:
2560 case EXPR_IDENTIFIER:
2561 return NULL;
2562 default:
2563 break;
2565 if (nested)
2566 warning(e->pos, "braces around scalar initializer");
2567 return v;
2571 * deal with the cases that don't care about subobjects:
2572 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2573 * character array <- string literal, possibly in braces [6.7.8(14)]
2574 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2575 * compound type <- initializer list in braces [6.7.8(16)]
2576 * The last one punts to handle_list_initializer() which, in turn will call
2577 * us for individual elements of the list.
2579 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2580 * the lack of support of wide char stuff in general.
2582 * One note: we need to take care not to evaluate a string literal until
2583 * we know that we *will* handle it right here. Otherwise we would screw
2584 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2585 * { "string", ...} - we need to preserve that string literal recognizable
2586 * until we dig into the inner struct.
2588 static int handle_simple_initializer(struct expression **ep, int nested,
2589 int class, struct symbol *ctype)
2591 int is_string = is_string_type(ctype);
2592 struct expression *e = *ep, *p;
2593 struct symbol *type;
2595 if (!e)
2596 return 0;
2598 /* scalar */
2599 if (!(class & TYPE_COMPOUND)) {
2600 e = handle_scalar(e, nested);
2601 if (!e)
2602 return 0;
2603 *ep = e;
2604 if (!evaluate_expression(e))
2605 return 1;
2606 compatible_assignment_types(e, ctype, ep, "initializer");
2607 return 1;
2611 * sublist; either a string, or we dig in; the latter will deal with
2612 * pathologies, so we don't need anything fancy here.
2614 if (e->type == EXPR_INITIALIZER) {
2615 if (is_string) {
2616 struct expression *v = NULL;
2617 int count = 0;
2619 FOR_EACH_PTR(e->expr_list, p) {
2620 if (!v)
2621 v = p;
2622 count++;
2623 } END_FOR_EACH_PTR(p);
2624 if (count == 1 && is_string_literal(&v)) {
2625 *ep = e = v;
2626 goto String;
2629 handle_list_initializer(e, class, ctype);
2630 return 1;
2633 /* string */
2634 if (is_string_literal(&e)) {
2635 /* either we are doing array of char, or we'll have to dig in */
2636 if (is_string) {
2637 *ep = e;
2638 goto String;
2640 return 0;
2642 /* struct or union can be initialized by compatible */
2643 if (class != TYPE_COMPOUND)
2644 return 0;
2645 type = evaluate_expression(e);
2646 if (!type)
2647 return 0;
2648 if (ctype->type == SYM_NODE)
2649 ctype = ctype->ctype.base_type;
2650 if (type->type == SYM_NODE)
2651 type = type->ctype.base_type;
2652 if (ctype == type)
2653 return 1;
2654 return 0;
2656 String:
2657 p = alloc_expression(e->pos, EXPR_STRING);
2658 *p = *e;
2659 type = evaluate_expression(p);
2660 if (ctype->bit_size != -1) {
2661 if (ctype->bit_size + bits_in_char < type->bit_size)
2662 warning(e->pos,
2663 "too long initializer-string for array of char");
2664 else if (Winit_cstring && ctype->bit_size + bits_in_char == type->bit_size) {
2665 warning(e->pos,
2666 "too long initializer-string for array of char(no space for nul char)");
2669 *ep = p;
2670 return 1;
2673 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2675 struct symbol *type;
2676 int class = classify_type(ctype, &type);
2677 if (!handle_simple_initializer(ep, 0, class, ctype))
2678 expression_error(*ep, "invalid initializer");
2681 static struct symbol *evaluate_cast(struct expression *expr)
2683 struct expression *target = expr->cast_expression;
2684 struct symbol *ctype;
2685 struct symbol *t1, *t2;
2686 int class1, class2;
2687 int as1 = 0, as2 = 0;
2689 if (!target)
2690 return NULL;
2693 * Special case: a cast can be followed by an
2694 * initializer, in which case we need to pass
2695 * the type value down to that initializer rather
2696 * than trying to evaluate it as an expression
2698 * A more complex case is when the initializer is
2699 * dereferenced as part of a post-fix expression.
2700 * We need to produce an expression that can be dereferenced.
2702 if (target->type == EXPR_INITIALIZER) {
2703 struct symbol *sym = expr->cast_type;
2704 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2706 sym->initializer = target;
2707 evaluate_symbol(sym);
2709 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2710 addr->symbol = sym;
2712 expr->type = EXPR_PREOP;
2713 expr->op = '*';
2714 expr->unop = addr;
2715 expr->ctype = sym;
2717 return sym;
2720 ctype = examine_symbol_type(expr->cast_type);
2721 expr->ctype = ctype;
2722 expr->cast_type = ctype;
2724 evaluate_expression(target);
2725 degenerate(target);
2727 class1 = classify_type(ctype, &t1);
2729 /* cast to non-integer type -> not an integer constant expression */
2730 if (!is_int(class1))
2731 expr->flags = 0;
2732 /* if argument turns out to be not an integer constant expression *and*
2733 it was not a floating literal to start with -> too bad */
2734 else if (expr->flags == Int_const_expr &&
2735 !(target->flags & Int_const_expr))
2736 expr->flags = 0;
2738 * You can always throw a value away by casting to
2739 * "void" - that's an implicit "force". Note that
2740 * the same is _not_ true of "void *".
2742 if (t1 == &void_ctype)
2743 goto out;
2745 if (class1 & (TYPE_COMPOUND | TYPE_FN))
2746 warning(expr->pos, "cast to non-scalar");
2748 t2 = target->ctype;
2749 if (!t2) {
2750 expression_error(expr, "cast from unknown type");
2751 goto out;
2753 class2 = classify_type(t2, &t2);
2755 if (class2 & TYPE_COMPOUND)
2756 warning(expr->pos, "cast from non-scalar");
2758 if (expr->type == EXPR_FORCE_CAST)
2759 goto out;
2761 /* allowed cast unfouls */
2762 if (class2 & TYPE_FOULED)
2763 t2 = unfoul(t2);
2765 if (t1 != t2) {
2766 if (class1 & TYPE_RESTRICT)
2767 warning(expr->pos, "cast to %s",
2768 show_typename(t1));
2769 if (class2 & TYPE_RESTRICT)
2770 warning(expr->pos, "cast from %s",
2771 show_typename(t2));
2774 if (t1 == &ulong_ctype)
2775 as1 = -1;
2776 else if (class1 == TYPE_PTR) {
2777 examine_pointer_target(t1);
2778 as1 = t1->ctype.attribute->as;
2781 if (t2 == &ulong_ctype)
2782 as2 = -1;
2783 else if (class2 == TYPE_PTR) {
2784 examine_pointer_target(t2);
2785 as2 = t2->ctype.attribute->as;
2788 if (!as1 && as2 > 0)
2789 warning(expr->pos, "cast removes address space of expression");
2790 if (as1 > 0 && as2 > 0 && as1 != as2)
2791 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2792 if (as1 > 0 && !as2 &&
2793 !is_null_pointer_constant(target) && Wcast_to_as)
2794 warning(expr->pos,
2795 "cast adds address space to expression (<asn:%d>)", as1);
2797 if (!(t1->ctype.modifiers & MOD_PTRINHERIT) && class1 == TYPE_PTR &&
2798 !as1 && (target->flags & Int_const_expr)) {
2799 if (t1->ctype.base_type == &void_ctype) {
2800 if (is_zero_constant(target)) {
2801 /* NULL */
2802 expr->type = EXPR_VALUE;
2803 expr->ctype = &null_ctype;
2804 expr->value = 0;
2805 return ctype;
2809 out:
2810 return ctype;
2814 * Evaluate a call expression with a symbol. This
2815 * should expand inline functions, and evaluate
2816 * builtins.
2818 static int evaluate_symbol_call(struct expression *expr)
2820 struct expression *fn = expr->fn;
2821 struct symbol *ctype = fn->ctype;
2823 if (fn->type != EXPR_PREOP)
2824 return 0;
2826 if (ctype->op && ctype->op->evaluate)
2827 return ctype->op->evaluate(expr);
2829 if (ctype->ctype.modifiers & MOD_INLINE) {
2830 int ret;
2831 struct symbol *curr = current_fn;
2833 if (ctype->definition)
2834 ctype = ctype->definition;
2836 current_fn = ctype->ctype.base_type;
2838 ret = inline_function(expr, ctype);
2840 /* restore the old function */
2841 current_fn = curr;
2842 return ret;
2845 return 0;
2848 static struct symbol *evaluate_call(struct expression *expr)
2850 int args, fnargs;
2851 struct symbol *ctype, *sym;
2852 struct expression *fn = expr->fn;
2853 struct expression_list *arglist = expr->args;
2855 if (!evaluate_expression(fn))
2856 return NULL;
2857 sym = ctype = fn->ctype;
2858 if (ctype->type == SYM_NODE)
2859 ctype = ctype->ctype.base_type;
2860 if (ctype->type == SYM_PTR)
2861 ctype = get_base_type(ctype);
2863 if (ctype->type != SYM_FN) {
2864 struct expression *arg;
2865 expression_error(expr, "not a function %s",
2866 show_ident(sym->ident));
2867 /* do typechecking in arguments */
2868 FOR_EACH_PTR (arglist, arg) {
2869 evaluate_expression(arg);
2870 } END_FOR_EACH_PTR(arg);
2871 return NULL;
2874 examine_fn_arguments(ctype);
2875 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
2876 sym->op && sym->op->args) {
2877 if (!sym->op->args(expr))
2878 return NULL;
2879 } else {
2880 if (!evaluate_arguments(sym, ctype, arglist))
2881 return NULL;
2882 args = expression_list_size(expr->args);
2883 fnargs = symbol_list_size(ctype->arguments);
2884 if (args < fnargs)
2885 expression_error(expr,
2886 "not enough arguments for function %s",
2887 show_ident(sym->ident));
2888 if (args > fnargs && !ctype->variadic)
2889 expression_error(expr,
2890 "too many arguments for function %s",
2891 show_ident(sym->ident));
2893 if (sym->type == SYM_NODE) {
2894 if (evaluate_symbol_call(expr))
2895 return expr->ctype;
2897 expr->ctype = ctype->ctype.base_type;
2898 return expr->ctype;
2901 static struct symbol *evaluate_offsetof(struct expression *expr)
2903 struct expression *e = expr->down;
2904 struct symbol *ctype = expr->in;
2905 int class;
2907 if (expr->op == '.') {
2908 struct symbol *field;
2909 int offset = 0;
2910 if (!ctype) {
2911 expression_error(expr, "expected structure or union");
2912 return NULL;
2914 examine_symbol_type(ctype);
2915 class = classify_type(ctype, &ctype);
2916 if (class != TYPE_COMPOUND) {
2917 expression_error(expr, "expected structure or union");
2918 return NULL;
2921 field = find_identifier(expr->ident, ctype->symbol_list, &offset);
2922 if (!field) {
2923 expression_error(expr, "unknown member");
2924 return NULL;
2926 ctype = field;
2927 expr->type = EXPR_VALUE;
2928 expr->flags = Int_const_expr;
2929 expr->value = offset;
2930 expr->taint = 0;
2931 expr->ctype = size_t_ctype;
2932 } else {
2933 if (!ctype) {
2934 expression_error(expr, "expected structure or union");
2935 return NULL;
2937 examine_symbol_type(ctype);
2938 class = classify_type(ctype, &ctype);
2939 if (class != (TYPE_COMPOUND | TYPE_PTR)) {
2940 expression_error(expr, "expected array");
2941 return NULL;
2943 ctype = ctype->ctype.base_type;
2944 if (!expr->index) {
2945 expr->type = EXPR_VALUE;
2946 expr->flags = Int_const_expr;
2947 expr->value = 0;
2948 expr->taint = 0;
2949 expr->ctype = size_t_ctype;
2950 } else {
2951 struct expression *idx = expr->index, *m;
2952 struct symbol *i_type = evaluate_expression(idx);
2953 int i_class = classify_type(i_type, &i_type);
2954 if (!is_int(i_class)) {
2955 expression_error(expr, "non-integer index");
2956 return NULL;
2958 unrestrict(idx, i_class, &i_type);
2959 idx = cast_to(idx, size_t_ctype);
2960 m = alloc_const_expression(expr->pos,
2961 bits_to_bytes(ctype->bit_size));
2962 m->ctype = size_t_ctype;
2963 m->flags = Int_const_expr;
2964 expr->type = EXPR_BINOP;
2965 expr->left = idx;
2966 expr->right = m;
2967 expr->op = '*';
2968 expr->ctype = size_t_ctype;
2969 expr->flags = m->flags & idx->flags & Int_const_expr;
2972 if (e) {
2973 struct expression *copy = __alloc_expression(0);
2974 *copy = *expr;
2975 if (e->type == EXPR_OFFSETOF)
2976 e->in = ctype;
2977 if (!evaluate_expression(e))
2978 return NULL;
2979 expr->type = EXPR_BINOP;
2980 expr->flags = e->flags & copy->flags & Int_const_expr;
2981 expr->op = '+';
2982 expr->ctype = size_t_ctype;
2983 expr->left = copy;
2984 expr->right = e;
2986 return size_t_ctype;
2989 struct symbol *evaluate_expression(struct expression *expr)
2991 if (!expr)
2992 return NULL;
2993 if (expr->ctype)
2994 return expr->ctype;
2996 switch (expr->type) {
2997 case EXPR_VALUE:
2998 case EXPR_FVALUE:
2999 expression_error(expr, "value expression without a type");
3000 return NULL;
3001 case EXPR_STRING:
3002 return evaluate_string(expr);
3003 case EXPR_SYMBOL:
3004 return evaluate_symbol_expression(expr);
3005 case EXPR_BINOP:
3006 if (!evaluate_expression(expr->left))
3007 return NULL;
3008 if (!evaluate_expression(expr->right))
3009 return NULL;
3010 return evaluate_binop(expr);
3011 case EXPR_LOGICAL:
3012 return evaluate_logical(expr);
3013 case EXPR_COMMA:
3014 evaluate_expression(expr->left);
3015 if (!evaluate_expression(expr->right))
3016 return NULL;
3017 return evaluate_comma(expr);
3018 case EXPR_COMPARE:
3019 if (!evaluate_expression(expr->left))
3020 return NULL;
3021 if (!evaluate_expression(expr->right))
3022 return NULL;
3023 return evaluate_compare(expr);
3024 case EXPR_ASSIGNMENT:
3025 if (!evaluate_expression(expr->left))
3026 return NULL;
3027 if (!evaluate_expression(expr->right))
3028 return NULL;
3029 return evaluate_assignment(expr);
3030 case EXPR_PREOP:
3031 if (!evaluate_expression(expr->unop))
3032 return NULL;
3033 return evaluate_preop(expr);
3034 case EXPR_POSTOP:
3035 if (!evaluate_expression(expr->unop))
3036 return NULL;
3037 return evaluate_postop(expr);
3038 case EXPR_CAST:
3039 case EXPR_FORCE_CAST:
3040 case EXPR_IMPLIED_CAST:
3041 return evaluate_cast(expr);
3042 case EXPR_SIZEOF:
3043 return evaluate_sizeof(expr);
3044 case EXPR_PTRSIZEOF:
3045 return evaluate_ptrsizeof(expr);
3046 case EXPR_ALIGNOF:
3047 return evaluate_alignof(expr);
3048 case EXPR_DEREF:
3049 return evaluate_member_dereference(expr);
3050 case EXPR_CALL:
3051 return evaluate_call(expr);
3052 case EXPR_SELECT:
3053 case EXPR_CONDITIONAL:
3054 return evaluate_conditional_expression(expr);
3055 case EXPR_STATEMENT:
3056 expr->ctype = evaluate_statement(expr->statement);
3057 return expr->ctype;
3059 case EXPR_LABEL:
3060 expr->ctype = &ptr_ctype;
3061 return &ptr_ctype;
3063 case EXPR_TYPE:
3064 /* Evaluate the type of the symbol .. */
3065 evaluate_symbol(expr->symbol);
3066 /* .. but the type of the _expression_ is a "type" */
3067 expr->ctype = &type_ctype;
3068 return &type_ctype;
3070 case EXPR_OFFSETOF:
3071 return evaluate_offsetof(expr);
3073 /* These can not exist as stand-alone expressions */
3074 case EXPR_INITIALIZER:
3075 case EXPR_IDENTIFIER:
3076 case EXPR_INDEX:
3077 case EXPR_POS:
3078 expression_error(expr, "internal front-end error: initializer in expression");
3079 return NULL;
3080 case EXPR_SLICE:
3081 expression_error(expr, "internal front-end error: SLICE re-evaluated");
3082 return NULL;
3084 return NULL;
3087 static void check_duplicates(struct symbol *sym)
3089 int declared = 0;
3090 struct symbol *next = sym;
3091 int initialized = sym->initializer != NULL;
3093 while ((next = next->same_symbol) != NULL) {
3094 const char *typediff;
3095 evaluate_symbol(next);
3096 if (initialized && next->initializer) {
3097 sparse_error(sym->pos, "symbol '%s' has multiple initializers (originally initialized at %s:%d)",
3098 show_ident(sym->ident),
3099 stream_name(next->pos.stream), next->pos.line);
3100 /* Only warn once */
3101 initialized = 0;
3103 declared++;
3104 typediff = type_difference(&sym->ctype, &next->ctype, 0, 0);
3105 if (typediff) {
3106 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
3107 show_ident(sym->ident),
3108 stream_name(next->pos.stream), next->pos.line, typediff);
3109 return;
3112 if (!declared) {
3113 unsigned long mod = sym->ctype.modifiers;
3114 if (mod & (MOD_STATIC | MOD_REGISTER))
3115 return;
3116 if (!(mod & MOD_TOPLEVEL))
3117 return;
3118 if (!Wdecl)
3119 return;
3120 if (sym->ident == &main_ident)
3121 return;
3122 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
3126 static struct symbol *evaluate_symbol(struct symbol *sym)
3128 struct symbol *base_type;
3130 if (!sym)
3131 return sym;
3132 if (sym->evaluated)
3133 return sym;
3134 sym->evaluated = 1;
3136 sym = examine_symbol_type(sym);
3137 base_type = get_base_type(sym);
3138 if (!base_type)
3139 return NULL;
3141 /* Evaluate the initializers */
3142 if (sym->initializer)
3143 evaluate_initializer(sym, &sym->initializer);
3145 /* And finally, evaluate the body of the symbol too */
3146 if (base_type->type == SYM_FN) {
3147 struct symbol *curr = current_fn;
3149 if (sym->definition && sym->definition != sym)
3150 return evaluate_symbol(sym->definition);
3152 current_fn = base_type;
3154 examine_fn_arguments(base_type);
3155 if (!base_type->stmt && base_type->inline_stmt)
3156 uninline(sym);
3157 if (base_type->stmt)
3158 evaluate_statement(base_type->stmt);
3160 current_fn = curr;
3163 return base_type;
3166 void evaluate_symbol_list(struct symbol_list *list)
3168 struct symbol *sym;
3170 FOR_EACH_PTR(list, sym) {
3171 evaluate_symbol(sym);
3172 check_duplicates(sym);
3173 } END_FOR_EACH_PTR(sym);
3176 static struct symbol *evaluate_return_expression(struct statement *stmt)
3178 struct expression *expr = stmt->expression;
3179 struct symbol *fntype;
3181 evaluate_expression(expr);
3182 fntype = current_fn->ctype.base_type;
3183 if (!fntype || fntype == &void_ctype) {
3184 if (expr && expr->ctype != &void_ctype)
3185 expression_error(expr, "return expression in %s function", fntype?"void":"typeless");
3186 if (expr && Wreturn_void)
3187 warning(stmt->pos, "returning void-valued expression");
3188 return NULL;
3191 if (!expr) {
3192 sparse_error(stmt->pos, "return with no return value");
3193 return NULL;
3195 if (!expr->ctype)
3196 return NULL;
3197 compatible_assignment_types(expr, fntype, &stmt->expression, "return expression");
3198 return NULL;
3201 static void evaluate_if_statement(struct statement *stmt)
3203 if (!stmt->if_conditional)
3204 return;
3206 evaluate_conditional(stmt->if_conditional, 0);
3207 evaluate_statement(stmt->if_true);
3208 evaluate_statement(stmt->if_false);
3211 static void evaluate_iterator(struct statement *stmt)
3213 evaluate_symbol_list(stmt->iterator_syms);
3214 evaluate_conditional(stmt->iterator_pre_condition, 1);
3215 evaluate_conditional(stmt->iterator_post_condition,1);
3216 evaluate_statement(stmt->iterator_pre_statement);
3217 evaluate_statement(stmt->iterator_statement);
3218 evaluate_statement(stmt->iterator_post_statement);
3221 static void verify_output_constraint(struct expression *expr, const char *constraint)
3223 switch (*constraint) {
3224 case '=': /* Assignment */
3225 case '+': /* Update */
3226 break;
3227 default:
3228 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
3232 static void verify_input_constraint(struct expression *expr, const char *constraint)
3234 switch (*constraint) {
3235 case '=': /* Assignment */
3236 case '+': /* Update */
3237 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
3241 static void evaluate_asm_statement(struct statement *stmt)
3243 struct expression *expr;
3244 struct symbol *sym;
3245 int state;
3247 expr = stmt->asm_string;
3248 if (!expr || expr->type != EXPR_STRING) {
3249 sparse_error(stmt->pos, "need constant string for inline asm");
3250 return;
3253 state = 0;
3254 FOR_EACH_PTR(stmt->asm_outputs, expr) {
3255 switch (state) {
3256 case 0: /* Identifier */
3257 state = 1;
3258 continue;
3260 case 1: /* Constraint */
3261 state = 2;
3262 if (!expr || expr->type != EXPR_STRING) {
3263 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
3264 *THIS_ADDRESS(expr) = NULL;
3265 continue;
3267 verify_output_constraint(expr, expr->string->data);
3268 continue;
3270 case 2: /* Expression */
3271 state = 0;
3272 if (!evaluate_expression(expr))
3273 return;
3274 if (!lvalue_expression(expr))
3275 warning(expr->pos, "asm output is not an lvalue");
3276 evaluate_assign_to(expr, expr->ctype);
3277 continue;
3279 } END_FOR_EACH_PTR(expr);
3281 state = 0;
3282 FOR_EACH_PTR(stmt->asm_inputs, expr) {
3283 switch (state) {
3284 case 0: /* Identifier */
3285 state = 1;
3286 continue;
3288 case 1: /* Constraint */
3289 state = 2;
3290 if (!expr || expr->type != EXPR_STRING) {
3291 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
3292 *THIS_ADDRESS(expr) = NULL;
3293 continue;
3295 verify_input_constraint(expr, expr->string->data);
3296 continue;
3298 case 2: /* Expression */
3299 state = 0;
3300 if (!evaluate_expression(expr))
3301 return;
3302 continue;
3304 } END_FOR_EACH_PTR(expr);
3306 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
3307 if (!expr) {
3308 sparse_error(stmt->pos, "bad asm clobbers");
3309 return;
3311 if (expr->type == EXPR_STRING)
3312 continue;
3313 expression_error(expr, "asm clobber is not a string");
3314 } END_FOR_EACH_PTR(expr);
3316 FOR_EACH_PTR(stmt->asm_labels, sym) {
3317 if (!sym || sym->type != SYM_LABEL) {
3318 sparse_error(stmt->pos, "bad asm label");
3319 return;
3321 } END_FOR_EACH_PTR(sym);
3324 static void evaluate_case_statement(struct statement *stmt)
3326 evaluate_expression(stmt->case_expression);
3327 evaluate_expression(stmt->case_to);
3328 evaluate_statement(stmt->case_statement);
3331 static void check_case_type(struct expression *switch_expr,
3332 struct expression *case_expr,
3333 struct expression **enumcase)
3335 struct symbol *switch_type, *case_type;
3336 int sclass, cclass;
3338 if (!case_expr)
3339 return;
3341 switch_type = switch_expr->ctype;
3342 case_type = evaluate_expression(case_expr);
3344 if (!switch_type || !case_type)
3345 goto Bad;
3346 if (enumcase) {
3347 if (*enumcase)
3348 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
3349 else if (is_enum_type(case_type))
3350 *enumcase = case_expr;
3353 sclass = classify_type(switch_type, &switch_type);
3354 cclass = classify_type(case_type, &case_type);
3356 /* both should be arithmetic */
3357 if (!(sclass & cclass & TYPE_NUM))
3358 goto Bad;
3360 /* neither should be floating */
3361 if ((sclass | cclass) & TYPE_FLOAT)
3362 goto Bad;
3364 /* if neither is restricted, we are OK */
3365 if (!((sclass | cclass) & TYPE_RESTRICT))
3366 return;
3368 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
3369 cclass, sclass, case_type, switch_type)) {
3370 unrestrict(case_expr, cclass, &case_type);
3371 unrestrict(switch_expr, sclass, &switch_type);
3373 return;
3375 Bad:
3376 expression_error(case_expr, "incompatible types for 'case' statement");
3379 static void evaluate_switch_statement(struct statement *stmt)
3381 struct symbol *sym;
3382 struct expression *enumcase = NULL;
3383 struct expression **enumcase_holder = &enumcase;
3384 struct expression *sel = stmt->switch_expression;
3386 evaluate_expression(sel);
3387 evaluate_statement(stmt->switch_statement);
3388 if (!sel)
3389 return;
3390 if (sel->ctype && is_enum_type(sel->ctype))
3391 enumcase_holder = NULL; /* Only check cases against switch */
3393 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
3394 struct statement *case_stmt = sym->stmt;
3395 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
3396 check_case_type(sel, case_stmt->case_to, enumcase_holder);
3397 } END_FOR_EACH_PTR(sym);
3400 static void evaluate_goto_statement(struct statement *stmt)
3402 struct symbol *label = stmt->goto_label;
3404 if (label && !label->stmt)
3405 sparse_error(stmt->pos, "label '%s' was not declared", show_ident(label->ident));
3407 evaluate_expression(stmt->goto_expression);
3410 struct symbol *evaluate_statement(struct statement *stmt)
3412 if (!stmt)
3413 return NULL;
3415 switch (stmt->type) {
3416 case STMT_DECLARATION: {
3417 struct symbol *s;
3418 FOR_EACH_PTR(stmt->declaration, s) {
3419 evaluate_symbol(s);
3420 } END_FOR_EACH_PTR(s);
3421 return NULL;
3424 case STMT_RETURN:
3425 return evaluate_return_expression(stmt);
3427 case STMT_EXPRESSION:
3428 if (!evaluate_expression(stmt->expression))
3429 return NULL;
3430 if (stmt->expression->ctype == &null_ctype)
3431 stmt->expression = cast_to(stmt->expression, &ptr_ctype);
3432 return degenerate(stmt->expression);
3434 case STMT_COMPOUND: {
3435 struct statement *s;
3436 struct symbol *type = NULL;
3438 /* Evaluate the return symbol in the compound statement */
3439 evaluate_symbol(stmt->ret);
3442 * Then, evaluate each statement, making the type of the
3443 * compound statement be the type of the last statement
3445 type = evaluate_statement(stmt->args);
3446 FOR_EACH_PTR(stmt->stmts, s) {
3447 type = evaluate_statement(s);
3448 } END_FOR_EACH_PTR(s);
3449 if (!type)
3450 type = &void_ctype;
3451 return type;
3453 case STMT_IF:
3454 evaluate_if_statement(stmt);
3455 return NULL;
3456 case STMT_ITERATOR:
3457 evaluate_iterator(stmt);
3458 return NULL;
3459 case STMT_SWITCH:
3460 evaluate_switch_statement(stmt);
3461 return NULL;
3462 case STMT_CASE:
3463 evaluate_case_statement(stmt);
3464 return NULL;
3465 case STMT_LABEL:
3466 return evaluate_statement(stmt->label_statement);
3467 case STMT_GOTO:
3468 evaluate_goto_statement(stmt);
3469 return NULL;
3470 case STMT_NONE:
3471 break;
3472 case STMT_ASM:
3473 evaluate_asm_statement(stmt);
3474 return NULL;
3475 case STMT_CONTEXT:
3476 evaluate_expression(stmt->expression);
3477 return NULL;
3478 case STMT_RANGE:
3479 evaluate_expression(stmt->range_expression);
3480 evaluate_expression(stmt->range_low);
3481 evaluate_expression(stmt->range_high);
3482 return NULL;
3484 return NULL;