fix value of label statement
[smatch.git] / evaluate.c
blobe2489ef63f981c843912c9dcd2a36c623ced426a
1 /*
2 * sparse/evaluate.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
25 * Evaluate constant expressions.
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <stddef.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <limits.h>
37 #include "lib.h"
38 #include "allocate.h"
39 #include "parse.h"
40 #include "token.h"
41 #include "symbol.h"
42 #include "target.h"
43 #include "expression.h"
45 struct symbol *current_fn;
47 static struct symbol *degenerate(struct expression *expr);
48 static struct symbol *evaluate_symbol(struct symbol *sym);
50 static struct symbol *evaluate_symbol_expression(struct expression *expr)
52 struct expression *addr;
53 struct symbol *sym = expr->symbol;
54 struct symbol *base_type;
56 if (!sym) {
57 expression_error(expr, "undefined identifier '%s'", show_ident(expr->symbol_name));
58 return NULL;
61 examine_symbol_type(sym);
63 base_type = get_base_type(sym);
64 if (!base_type) {
65 expression_error(expr, "identifier '%s' has no type", show_ident(expr->symbol_name));
66 return NULL;
69 addr = alloc_expression(expr->pos, EXPR_SYMBOL);
70 addr->symbol = sym;
71 addr->symbol_name = expr->symbol_name;
72 addr->ctype = &lazy_ptr_ctype; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
73 expr->type = EXPR_PREOP;
74 expr->op = '*';
75 expr->unop = addr;
77 /* The type of a symbol is the symbol itself! */
78 expr->ctype = sym;
79 return sym;
82 static struct symbol *evaluate_string(struct expression *expr)
84 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
85 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
86 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
87 struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
88 unsigned int length = expr->string->length;
90 sym->array_size = alloc_const_expression(expr->pos, length);
91 sym->bit_size = bytes_to_bits(length);
92 sym->ctype.alignment = 1;
93 sym->string = 1;
94 sym->ctype.modifiers = MOD_STATIC;
95 sym->ctype.base_type = array;
96 sym->initializer = initstr;
98 initstr->ctype = sym;
99 initstr->string = expr->string;
101 array->array_size = sym->array_size;
102 array->bit_size = bytes_to_bits(length);
103 array->ctype.alignment = 1;
104 array->ctype.modifiers = MOD_STATIC;
105 array->ctype.base_type = &char_ctype;
107 addr->symbol = sym;
108 addr->ctype = &lazy_ptr_ctype;
110 expr->type = EXPR_PREOP;
111 expr->op = '*';
112 expr->unop = addr;
113 expr->ctype = sym;
114 return sym;
117 /* type has come from classify_type and is an integer type */
118 static inline struct symbol *integer_promotion(struct symbol *type)
120 unsigned long mod = type->ctype.modifiers;
121 int width = type->bit_size;
124 * Bitfields always promote to the base type,
125 * even if the bitfield might be bigger than
126 * an "int".
128 if (type->type == SYM_BITFIELD) {
129 type = type->ctype.base_type;
131 mod = type->ctype.modifiers;
132 if (width < bits_in_int)
133 return &int_ctype;
135 /* If char/short has as many bits as int, it still gets "promoted" */
136 if (mod & (MOD_CHAR | MOD_SHORT)) {
137 if (mod & MOD_UNSIGNED)
138 return &uint_ctype;
139 return &int_ctype;
141 return type;
145 * integer part of usual arithmetic conversions:
146 * integer promotions are applied
147 * if left and right are identical, we are done
148 * if signedness is the same, convert one with lower rank
149 * unless unsigned argument has rank lower than signed one, convert the
150 * signed one.
151 * if signed argument is bigger than unsigned one, convert the unsigned.
152 * otherwise, convert signed.
154 * Leaving aside the integer promotions, that is equivalent to
155 * if identical, don't convert
156 * if left is bigger than right, convert right
157 * if right is bigger than left, convert right
158 * otherwise, if signedness is the same, convert one with lower rank
159 * otherwise convert the signed one.
161 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
163 unsigned long lmod, rmod;
165 left = integer_promotion(left);
166 right = integer_promotion(right);
168 if (left == right)
169 goto left;
171 if (left->bit_size > right->bit_size)
172 goto left;
174 if (right->bit_size > left->bit_size)
175 goto right;
177 lmod = left->ctype.modifiers;
178 rmod = right->ctype.modifiers;
179 if ((lmod ^ rmod) & MOD_UNSIGNED) {
180 if (lmod & MOD_UNSIGNED)
181 goto left;
182 } else if ((lmod & ~rmod) & (MOD_LONG_ALL))
183 goto left;
184 right:
185 left = right;
186 left:
187 return left;
190 static int same_cast_type(struct symbol *orig, struct symbol *new)
192 return orig->bit_size == new->bit_size &&
193 orig->bit_offset == new->bit_offset;
196 static struct symbol *base_type(struct symbol *node, unsigned long *modp, unsigned long *asp)
198 unsigned long mod, as;
200 mod = 0; as = 0;
201 while (node) {
202 mod |= node->ctype.modifiers;
203 as |= node->ctype.as;
204 if (node->type == SYM_NODE) {
205 node = node->ctype.base_type;
206 continue;
208 break;
210 *modp = mod & ~MOD_IGNORE;
211 *asp = as;
212 return node;
215 static int is_same_type(struct expression *expr, struct symbol *new)
217 struct symbol *old = expr->ctype;
218 unsigned long oldmod, newmod, oldas, newas;
220 old = base_type(old, &oldmod, &oldas);
221 new = base_type(new, &newmod, &newas);
223 /* Same base type, same address space? */
224 if (old == new && oldas == newas) {
225 unsigned long difmod;
227 /* Check the modifier bits. */
228 difmod = (oldmod ^ newmod) & ~MOD_NOCAST;
230 /* Exact same type? */
231 if (!difmod)
232 return 1;
235 * Not the same type, but differs only in "const".
236 * Don't warn about MOD_NOCAST.
238 if (difmod == MOD_CONST)
239 return 0;
241 if ((oldmod | newmod) & MOD_NOCAST) {
242 const char *tofrom = "to/from";
243 if (!(newmod & MOD_NOCAST))
244 tofrom = "from";
245 if (!(oldmod & MOD_NOCAST))
246 tofrom = "to";
247 warning(expr->pos, "implicit cast %s nocast type", tofrom);
249 return 0;
252 static void
253 warn_for_different_enum_types (struct position pos,
254 struct symbol *typea,
255 struct symbol *typeb)
257 if (!Wenum_mismatch)
258 return;
259 if (typea->type == SYM_NODE)
260 typea = typea->ctype.base_type;
261 if (typeb->type == SYM_NODE)
262 typeb = typeb->ctype.base_type;
264 if (typea == typeb)
265 return;
267 if (typea->type == SYM_ENUM && typeb->type == SYM_ENUM) {
268 warning(pos, "mixing different enum types");
269 info(pos, " %s versus", show_typename(typea));
270 info(pos, " %s", show_typename(typeb));
274 static struct symbol *cast_to_bool(struct expression *expr);
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;
331 if (is_bool_type(type))
332 cast_to_bool(expr);
334 return expr;
337 enum {
338 TYPE_NUM = 1,
339 TYPE_BITFIELD = 2,
340 TYPE_RESTRICT = 4,
341 TYPE_FLOAT = 8,
342 TYPE_PTR = 16,
343 TYPE_COMPOUND = 32,
344 TYPE_FOULED = 64,
345 TYPE_FN = 128,
348 static inline int classify_type(struct symbol *type, struct symbol **base)
350 static int type_class[SYM_BAD + 1] = {
351 [SYM_PTR] = TYPE_PTR,
352 [SYM_FN] = TYPE_PTR | TYPE_FN,
353 [SYM_ARRAY] = TYPE_PTR | TYPE_COMPOUND,
354 [SYM_STRUCT] = TYPE_COMPOUND,
355 [SYM_UNION] = TYPE_COMPOUND,
356 [SYM_BITFIELD] = TYPE_NUM | TYPE_BITFIELD,
357 [SYM_RESTRICT] = TYPE_NUM | TYPE_RESTRICT,
358 [SYM_FOULED] = TYPE_NUM | TYPE_RESTRICT | TYPE_FOULED,
360 if (type->type == SYM_NODE)
361 type = type->ctype.base_type;
362 if (type->type == SYM_TYPEOF) {
363 type = evaluate_expression(type->initializer);
364 if (!type)
365 type = &bad_ctype;
366 else if (type->type == SYM_NODE)
367 type = type->ctype.base_type;
369 if (type->type == SYM_ENUM)
370 type = type->ctype.base_type;
371 *base = type;
372 if (type->type == SYM_BASETYPE) {
373 if (type->ctype.base_type == &int_type)
374 return TYPE_NUM;
375 if (type->ctype.base_type == &fp_type)
376 return TYPE_NUM | TYPE_FLOAT;
378 return type_class[type->type];
381 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
383 static inline int is_string_type(struct symbol *type)
385 if (type->type == SYM_NODE)
386 type = type->ctype.base_type;
387 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
390 static struct symbol *bad_expr_type(struct expression *expr)
392 sparse_error(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
393 switch (expr->type) {
394 case EXPR_BINOP:
395 case EXPR_COMPARE:
396 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
397 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
398 break;
399 case EXPR_PREOP:
400 case EXPR_POSTOP:
401 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
402 break;
403 default:
404 break;
407 expr->flags = 0;
408 return expr->ctype = &bad_ctype;
411 static int restricted_value(struct expression *v, struct symbol *type)
413 if (v->type != EXPR_VALUE)
414 return 1;
415 if (v->value != 0)
416 return 1;
417 return 0;
420 static int restricted_binop(int op, struct symbol *type)
422 switch (op) {
423 case '&':
424 case '=':
425 case SPECIAL_AND_ASSIGN:
426 case SPECIAL_OR_ASSIGN:
427 case SPECIAL_XOR_ASSIGN:
428 return 1; /* unfoul */
429 case '|':
430 case '^':
431 case '?':
432 return 2; /* keep fouled */
433 case SPECIAL_EQUAL:
434 case SPECIAL_NOTEQUAL:
435 return 3; /* warn if fouled */
436 default:
437 return 0; /* warn */
441 static int restricted_unop(int op, struct symbol **type)
443 if (op == '~') {
444 if ((*type)->bit_size < bits_in_int)
445 *type = befoul(*type);
446 return 0;
447 } if (op == '+')
448 return 0;
449 return 1;
452 /* type should be SYM_FOULED */
453 static inline struct symbol *unfoul(struct symbol *type)
455 return type->ctype.base_type;
458 static struct symbol *restricted_binop_type(int op,
459 struct expression *left,
460 struct expression *right,
461 int lclass, int rclass,
462 struct symbol *ltype,
463 struct symbol *rtype)
465 struct symbol *ctype = NULL;
466 if (lclass & TYPE_RESTRICT) {
467 if (rclass & TYPE_RESTRICT) {
468 if (ltype == rtype) {
469 ctype = ltype;
470 } else if (lclass & TYPE_FOULED) {
471 if (unfoul(ltype) == rtype)
472 ctype = ltype;
473 } else if (rclass & TYPE_FOULED) {
474 if (unfoul(rtype) == ltype)
475 ctype = rtype;
477 } else {
478 if (!restricted_value(right, ltype))
479 ctype = ltype;
481 } else if (!restricted_value(left, rtype))
482 ctype = rtype;
484 if (ctype) {
485 switch (restricted_binop(op, ctype)) {
486 case 1:
487 if ((lclass ^ rclass) & TYPE_FOULED)
488 ctype = unfoul(ctype);
489 break;
490 case 3:
491 if (!(lclass & rclass & TYPE_FOULED))
492 break;
493 case 0:
494 ctype = NULL;
495 default:
496 break;
500 return ctype;
503 static inline void unrestrict(struct expression *expr,
504 int class, struct symbol **ctype)
506 if (class & TYPE_RESTRICT) {
507 if (class & TYPE_FOULED)
508 *ctype = unfoul(*ctype);
509 warning(expr->pos, "%s degrades to integer",
510 show_typename(*ctype));
511 *ctype = (*ctype)->ctype.base_type; /* get to arithmetic type */
515 static struct symbol *usual_conversions(int op,
516 struct expression *left,
517 struct expression *right,
518 int lclass, int rclass,
519 struct symbol *ltype,
520 struct symbol *rtype)
522 struct symbol *ctype;
524 warn_for_different_enum_types(right->pos, left->ctype, right->ctype);
526 if ((lclass | rclass) & TYPE_RESTRICT)
527 goto Restr;
529 Normal:
530 if (!(lclass & TYPE_FLOAT)) {
531 if (!(rclass & TYPE_FLOAT))
532 return bigger_int_type(ltype, rtype);
533 else
534 return rtype;
535 } else if (rclass & TYPE_FLOAT) {
536 unsigned long lmod = ltype->ctype.modifiers;
537 unsigned long rmod = rtype->ctype.modifiers;
538 if (rmod & ~lmod & (MOD_LONG_ALL))
539 return rtype;
540 else
541 return ltype;
542 } else
543 return ltype;
545 Restr:
546 ctype = restricted_binop_type(op, left, right,
547 lclass, rclass, ltype, rtype);
548 if (ctype)
549 return ctype;
551 unrestrict(left, lclass, &ltype);
552 unrestrict(right, rclass, &rtype);
554 goto Normal;
557 static inline int lvalue_expression(struct expression *expr)
559 return expr->type == EXPR_PREOP && expr->op == '*';
562 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *itype)
564 struct expression *index = expr->right;
565 struct symbol *ctype, *base;
566 int multiply;
568 classify_type(degenerate(expr->left), &ctype);
569 base = examine_pointer_target(ctype);
571 if (!base) {
572 expression_error(expr, "missing type information");
573 return NULL;
575 if (is_function(base)) {
576 expression_error(expr, "arithmetics on pointers to functions");
577 return NULL;
580 /* Get the size of whatever the pointer points to */
581 multiply = is_void_type(base) ? 1 : bits_to_bytes(base->bit_size);
583 if (ctype == &null_ctype)
584 ctype = &ptr_ctype;
585 expr->ctype = ctype;
587 if (multiply == 1 && itype->bit_size >= bits_in_pointer)
588 return ctype;
590 if (index->type == EXPR_VALUE) {
591 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
592 unsigned long long v = index->value, mask;
593 mask = 1ULL << (itype->bit_size - 1);
594 if (v & mask)
595 v |= -mask;
596 else
597 v &= mask - 1;
598 v *= multiply;
599 mask = 1ULL << (bits_in_pointer - 1);
600 v &= mask | (mask - 1);
601 val->value = v;
602 val->ctype = ssize_t_ctype;
603 expr->right = val;
604 return ctype;
607 if (itype->bit_size < bits_in_pointer)
608 index = cast_to(index, ssize_t_ctype);
610 if (multiply > 1) {
611 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
612 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
614 val->ctype = ssize_t_ctype;
615 val->value = multiply;
617 mul->op = '*';
618 mul->ctype = ssize_t_ctype;
619 mul->left = index;
620 mul->right = val;
621 index = mul;
624 expr->right = index;
625 return ctype;
628 static void examine_fn_arguments(struct symbol *fn);
630 #define MOD_IGN (MOD_VOLATILE | MOD_CONST | MOD_PURE)
632 const char *type_difference(struct ctype *c1, struct ctype *c2,
633 unsigned long mod1, unsigned long mod2)
635 unsigned long as1 = c1->as, as2 = c2->as;
636 struct symbol *t1 = c1->base_type;
637 struct symbol *t2 = c2->base_type;
638 int move1 = 1, move2 = 1;
639 mod1 |= c1->modifiers;
640 mod2 |= c2->modifiers;
641 for (;;) {
642 unsigned long diff;
643 int type;
644 struct symbol *base1 = t1->ctype.base_type;
645 struct symbol *base2 = t2->ctype.base_type;
648 * FIXME! Collect alignment and context too here!
650 if (move1) {
651 if (t1 && t1->type != SYM_PTR) {
652 mod1 |= t1->ctype.modifiers;
653 as1 |= t1->ctype.as;
655 move1 = 0;
658 if (move2) {
659 if (t2 && t2->type != SYM_PTR) {
660 mod2 |= t2->ctype.modifiers;
661 as2 |= t2->ctype.as;
663 move2 = 0;
666 if (t1 == t2)
667 break;
668 if (!t1 || !t2)
669 return "different types";
671 if (t1->type == SYM_NODE || t1->type == SYM_ENUM) {
672 t1 = base1;
673 move1 = 1;
674 if (!t1)
675 return "bad types";
676 continue;
679 if (t2->type == SYM_NODE || t2->type == SYM_ENUM) {
680 t2 = base2;
681 move2 = 1;
682 if (!t2)
683 return "bad types";
684 continue;
687 move1 = move2 = 1;
688 type = t1->type;
689 if (type != t2->type)
690 return "different base types";
692 switch (type) {
693 default:
694 sparse_error(t1->pos,
695 "internal error: bad type in derived(%d)",
696 type);
697 return "bad types";
698 case SYM_RESTRICT:
699 return "different base types";
700 case SYM_UNION:
701 case SYM_STRUCT:
702 /* allow definition of incomplete structs and unions */
703 if (t1->ident == t2->ident)
704 return NULL;
705 return "different base types";
706 case SYM_ARRAY:
707 /* XXX: we ought to compare sizes */
708 break;
709 case SYM_PTR:
710 if (as1 != as2)
711 return "different address spaces";
712 /* MOD_SPECIFIER is due to idiocy in parse.c */
713 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SPECIFIER)
714 return "different modifiers";
715 /* we could be lazier here */
716 base1 = examine_pointer_target(t1);
717 base2 = examine_pointer_target(t2);
718 mod1 = t1->ctype.modifiers;
719 as1 = t1->ctype.as;
720 mod2 = t2->ctype.modifiers;
721 as2 = t2->ctype.as;
722 break;
723 case SYM_FN: {
724 struct symbol *arg1, *arg2;
725 int i;
727 if (as1 != as2)
728 return "different address spaces";
729 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
730 return "different modifiers";
731 mod1 = t1->ctype.modifiers;
732 as1 = t1->ctype.as;
733 mod2 = t2->ctype.modifiers;
734 as2 = t2->ctype.as;
736 if (t1->variadic != t2->variadic)
737 return "incompatible variadic arguments";
738 examine_fn_arguments(t1);
739 examine_fn_arguments(t2);
740 PREPARE_PTR_LIST(t1->arguments, arg1);
741 PREPARE_PTR_LIST(t2->arguments, arg2);
742 i = 1;
743 for (;;) {
744 const char *diffstr;
745 if (!arg1 && !arg2)
746 break;
747 if (!arg1 || !arg2)
748 return "different argument counts";
749 diffstr = type_difference(&arg1->ctype,
750 &arg2->ctype,
751 MOD_IGN, MOD_IGN);
752 if (diffstr) {
753 static char argdiff[80];
754 sprintf(argdiff, "incompatible argument %d (%s)", i, diffstr);
755 return argdiff;
757 NEXT_PTR_LIST(arg1);
758 NEXT_PTR_LIST(arg2);
759 i++;
761 FINISH_PTR_LIST(arg2);
762 FINISH_PTR_LIST(arg1);
763 break;
765 case SYM_BASETYPE:
766 if (as1 != as2)
767 return "different address spaces";
768 if (base1 != base2)
769 return "different base types";
770 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
771 if (!diff)
772 return NULL;
773 if (diff & MOD_SIZE)
774 return "different type sizes";
775 else if (diff & ~MOD_SIGNEDNESS)
776 return "different modifiers";
777 else
778 return "different signedness";
780 t1 = base1;
781 t2 = base2;
783 if (as1 != as2)
784 return "different address spaces";
785 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
786 return "different modifiers";
787 return NULL;
790 static void bad_null(struct expression *expr)
792 if (Wnon_pointer_null)
793 warning(expr->pos, "Using plain integer as NULL pointer");
796 static unsigned long target_qualifiers(struct symbol *type)
798 unsigned long mod = type->ctype.modifiers & MOD_IGN;
799 if (type->ctype.base_type && type->ctype.base_type->type == SYM_ARRAY)
800 mod = 0;
801 return mod;
804 static struct symbol *evaluate_ptr_sub(struct expression *expr)
806 const char *typediff;
807 struct symbol *ltype, *rtype;
808 struct expression *l = expr->left;
809 struct expression *r = expr->right;
810 struct symbol *lbase;
812 classify_type(degenerate(l), &ltype);
813 classify_type(degenerate(r), &rtype);
815 lbase = examine_pointer_target(ltype);
816 examine_pointer_target(rtype);
817 typediff = type_difference(&ltype->ctype, &rtype->ctype,
818 target_qualifiers(rtype),
819 target_qualifiers(ltype));
820 if (typediff)
821 expression_error(expr, "subtraction of different types can't work (%s)", typediff);
823 if (is_function(lbase)) {
824 expression_error(expr, "subtraction of functions? Share your drugs");
825 return NULL;
828 expr->ctype = ssize_t_ctype;
829 if (lbase->bit_size > bits_in_char) {
830 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
831 struct expression *div = expr;
832 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
833 unsigned long value = bits_to_bytes(lbase->bit_size);
835 val->ctype = size_t_ctype;
836 val->value = value;
838 if (value & (value-1)) {
839 if (Wptr_subtraction_blows)
840 warning(expr->pos, "potentially expensive pointer subtraction");
843 sub->op = '-';
844 sub->ctype = ssize_t_ctype;
845 sub->left = l;
846 sub->right = r;
848 div->op = '/';
849 div->left = sub;
850 div->right = val;
853 return ssize_t_ctype;
856 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
858 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
860 struct symbol *ctype;
862 if (!expr)
863 return NULL;
865 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
866 warning(expr->pos, "assignment expression in conditional");
868 ctype = evaluate_expression(expr);
869 if (ctype) {
870 if (is_safe_type(ctype))
871 warning(expr->pos, "testing a 'safe expression'");
874 return ctype;
877 static struct symbol *evaluate_logical(struct expression *expr)
879 if (!evaluate_conditional(expr->left, 0))
880 return NULL;
881 if (!evaluate_conditional(expr->right, 0))
882 return NULL;
884 /* the result is int [6.5.13(3), 6.5.14(3)] */
885 expr->ctype = &int_ctype;
886 if (expr->flags) {
887 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
888 expr->flags = 0;
890 return &int_ctype;
893 static struct symbol *evaluate_binop(struct expression *expr)
895 struct symbol *ltype, *rtype, *ctype;
896 int lclass = classify_type(expr->left->ctype, &ltype);
897 int rclass = classify_type(expr->right->ctype, &rtype);
898 int op = expr->op;
900 if (expr->flags) {
901 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
902 expr->flags = 0;
905 /* number op number */
906 if (lclass & rclass & TYPE_NUM) {
907 if ((lclass | rclass) & TYPE_FLOAT) {
908 switch (op) {
909 case '+': case '-': case '*': case '/':
910 break;
911 default:
912 return bad_expr_type(expr);
916 if (op == SPECIAL_LEFTSHIFT || op == SPECIAL_RIGHTSHIFT) {
917 // shifts do integer promotions, but that's it.
918 unrestrict(expr->left, lclass, &ltype);
919 unrestrict(expr->right, rclass, &rtype);
920 ctype = ltype = integer_promotion(ltype);
921 rtype = integer_promotion(rtype);
922 } else {
923 // The rest do usual conversions
924 const unsigned left_not = expr->left->type == EXPR_PREOP
925 && expr->left->op == '!';
926 const unsigned right_not = expr->right->type == EXPR_PREOP
927 && expr->right->op == '!';
928 if ((op == '&' || op == '|') && (left_not || right_not))
929 warning(expr->pos, "dubious: %sx %c %sy",
930 left_not ? "!" : "",
932 right_not ? "!" : "");
934 ltype = usual_conversions(op, expr->left, expr->right,
935 lclass, rclass, ltype, rtype);
936 ctype = rtype = ltype;
939 expr->left = cast_to(expr->left, ltype);
940 expr->right = cast_to(expr->right, rtype);
941 expr->ctype = ctype;
942 return ctype;
945 /* pointer (+|-) integer */
946 if (lclass & TYPE_PTR && is_int(rclass) && (op == '+' || op == '-')) {
947 unrestrict(expr->right, rclass, &rtype);
948 return evaluate_ptr_add(expr, rtype);
951 /* integer + pointer */
952 if (rclass & TYPE_PTR && is_int(lclass) && op == '+') {
953 struct expression *index = expr->left;
954 unrestrict(index, lclass, &ltype);
955 expr->left = expr->right;
956 expr->right = index;
957 return evaluate_ptr_add(expr, ltype);
960 /* pointer - pointer */
961 if (lclass & rclass & TYPE_PTR && expr->op == '-')
962 return evaluate_ptr_sub(expr);
964 return bad_expr_type(expr);
967 static struct symbol *evaluate_comma(struct expression *expr)
969 expr->ctype = degenerate(expr->right);
970 if (expr->ctype == &null_ctype)
971 expr->ctype = &ptr_ctype;
972 expr->flags &= expr->left->flags & expr->right->flags;
973 return expr->ctype;
976 static int modify_for_unsigned(int op)
978 if (op == '<')
979 op = SPECIAL_UNSIGNED_LT;
980 else if (op == '>')
981 op = SPECIAL_UNSIGNED_GT;
982 else if (op == SPECIAL_LTE)
983 op = SPECIAL_UNSIGNED_LTE;
984 else if (op == SPECIAL_GTE)
985 op = SPECIAL_UNSIGNED_GTE;
986 return op;
989 static inline int is_null_pointer_constant(struct expression *e)
991 if (e->ctype == &null_ctype)
992 return 1;
993 if (!(e->flags & Int_const_expr))
994 return 0;
995 return is_zero_constant(e) ? 2 : 0;
998 static struct symbol *evaluate_compare(struct expression *expr)
1000 struct expression *left = expr->left, *right = expr->right;
1001 struct symbol *ltype, *rtype, *lbase, *rbase;
1002 int lclass = classify_type(degenerate(left), &ltype);
1003 int rclass = classify_type(degenerate(right), &rtype);
1004 struct symbol *ctype;
1005 const char *typediff;
1007 if (expr->flags) {
1008 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
1009 expr->flags = 0;
1012 /* Type types? */
1013 if (is_type_type(ltype) && is_type_type(rtype))
1014 goto OK;
1016 if (is_safe_type(left->ctype) || is_safe_type(right->ctype))
1017 warning(expr->pos, "testing a 'safe expression'");
1019 /* number on number */
1020 if (lclass & rclass & TYPE_NUM) {
1021 ctype = usual_conversions(expr->op, expr->left, expr->right,
1022 lclass, rclass, ltype, rtype);
1023 expr->left = cast_to(expr->left, ctype);
1024 expr->right = cast_to(expr->right, ctype);
1025 if (ctype->ctype.modifiers & MOD_UNSIGNED)
1026 expr->op = modify_for_unsigned(expr->op);
1027 goto OK;
1030 /* at least one must be a pointer */
1031 if (!((lclass | rclass) & TYPE_PTR))
1032 return bad_expr_type(expr);
1034 /* equality comparisons can be with null pointer constants */
1035 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1036 int is_null1 = is_null_pointer_constant(left);
1037 int is_null2 = is_null_pointer_constant(right);
1038 if (is_null1 == 2)
1039 bad_null(left);
1040 if (is_null2 == 2)
1041 bad_null(right);
1042 if (is_null1 && is_null2) {
1043 int positive = expr->op == SPECIAL_EQUAL;
1044 expr->type = EXPR_VALUE;
1045 expr->value = positive;
1046 goto OK;
1048 if (is_null1 && (rclass & TYPE_PTR)) {
1049 left = cast_to(left, rtype);
1050 goto OK;
1052 if (is_null2 && (lclass & TYPE_PTR)) {
1053 right = cast_to(right, ltype);
1054 goto OK;
1057 /* both should be pointers */
1058 if (!(lclass & rclass & TYPE_PTR))
1059 return bad_expr_type(expr);
1060 expr->op = modify_for_unsigned(expr->op);
1062 lbase = examine_pointer_target(ltype);
1063 rbase = examine_pointer_target(rtype);
1065 /* they also have special treatment for pointers to void */
1066 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1067 if (ltype->ctype.as == rtype->ctype.as) {
1068 if (lbase == &void_ctype) {
1069 right = cast_to(right, ltype);
1070 goto OK;
1072 if (rbase == &void_ctype) {
1073 left = cast_to(left, rtype);
1074 goto OK;
1079 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1080 target_qualifiers(rtype),
1081 target_qualifiers(ltype));
1082 if (!typediff)
1083 goto OK;
1085 expression_error(expr, "incompatible types in comparison expression (%s)", typediff);
1086 return NULL;
1089 /* the result is int [6.5.8(6), 6.5.9(3)]*/
1090 expr->ctype = &int_ctype;
1091 return &int_ctype;
1095 * NOTE! The degenerate case of "x ? : y", where we don't
1096 * have a true case, this will possibly promote "x" to the
1097 * same type as "y", and thus _change_ the conditional
1098 * test in the expression. But since promotion is "safe"
1099 * for testing, that's OK.
1101 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1103 struct expression **true;
1104 struct symbol *ctype, *ltype, *rtype, *lbase, *rbase;
1105 int lclass, rclass;
1106 const char * typediff;
1107 int qual;
1109 if (!evaluate_conditional(expr->conditional, 0))
1110 return NULL;
1111 if (!evaluate_expression(expr->cond_false))
1112 return NULL;
1114 ctype = degenerate(expr->conditional);
1115 rtype = degenerate(expr->cond_false);
1117 true = &expr->conditional;
1118 ltype = ctype;
1119 if (expr->cond_true) {
1120 if (!evaluate_expression(expr->cond_true))
1121 return NULL;
1122 ltype = degenerate(expr->cond_true);
1123 true = &expr->cond_true;
1126 if (expr->flags) {
1127 int flags = expr->conditional->flags & Int_const_expr;
1128 flags &= (*true)->flags & expr->cond_false->flags;
1129 if (!flags)
1130 expr->flags = 0;
1133 lclass = classify_type(ltype, &ltype);
1134 rclass = classify_type(rtype, &rtype);
1135 if (lclass & rclass & TYPE_NUM) {
1136 ctype = usual_conversions('?', *true, expr->cond_false,
1137 lclass, rclass, ltype, rtype);
1138 *true = cast_to(*true, ctype);
1139 expr->cond_false = cast_to(expr->cond_false, ctype);
1140 goto out;
1143 if ((lclass | rclass) & TYPE_PTR) {
1144 int is_null1 = is_null_pointer_constant(*true);
1145 int is_null2 = is_null_pointer_constant(expr->cond_false);
1147 if (is_null1 && is_null2) {
1148 *true = cast_to(*true, &ptr_ctype);
1149 expr->cond_false = cast_to(expr->cond_false, &ptr_ctype);
1150 ctype = &ptr_ctype;
1151 goto out;
1153 if (is_null1 && (rclass & TYPE_PTR)) {
1154 if (is_null1 == 2)
1155 bad_null(*true);
1156 *true = cast_to(*true, rtype);
1157 ctype = rtype;
1158 goto out;
1160 if (is_null2 && (lclass & TYPE_PTR)) {
1161 if (is_null2 == 2)
1162 bad_null(expr->cond_false);
1163 expr->cond_false = cast_to(expr->cond_false, ltype);
1164 ctype = ltype;
1165 goto out;
1167 if (!(lclass & rclass & TYPE_PTR)) {
1168 typediff = "different types";
1169 goto Err;
1171 /* OK, it's pointer on pointer */
1172 if (ltype->ctype.as != rtype->ctype.as) {
1173 typediff = "different address spaces";
1174 goto Err;
1177 /* need to be lazier here */
1178 lbase = examine_pointer_target(ltype);
1179 rbase = examine_pointer_target(rtype);
1180 qual = target_qualifiers(ltype) | target_qualifiers(rtype);
1182 if (lbase == &void_ctype) {
1183 /* XXX: pointers to function should warn here */
1184 ctype = ltype;
1185 goto Qual;
1188 if (rbase == &void_ctype) {
1189 /* XXX: pointers to function should warn here */
1190 ctype = rtype;
1191 goto Qual;
1193 /* XXX: that should be pointer to composite */
1194 ctype = ltype;
1195 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1196 qual, qual);
1197 if (!typediff)
1198 goto Qual;
1199 goto Err;
1202 /* void on void, struct on same struct, union on same union */
1203 if (ltype == rtype) {
1204 ctype = ltype;
1205 goto out;
1207 typediff = "different base types";
1209 Err:
1210 expression_error(expr, "incompatible types in conditional expression (%s)", typediff);
1211 return NULL;
1213 out:
1214 expr->ctype = ctype;
1215 return ctype;
1217 Qual:
1218 if (qual & ~ctype->ctype.modifiers) {
1219 struct symbol *sym = alloc_symbol(ctype->pos, SYM_PTR);
1220 *sym = *ctype;
1221 sym->ctype.modifiers |= qual;
1222 ctype = sym;
1224 *true = cast_to(*true, ctype);
1225 expr->cond_false = cast_to(expr->cond_false, ctype);
1226 goto out;
1229 /* FP assignments can not do modulo or bit operations */
1230 static int compatible_float_op(int op)
1232 return op == SPECIAL_ADD_ASSIGN ||
1233 op == SPECIAL_SUB_ASSIGN ||
1234 op == SPECIAL_MUL_ASSIGN ||
1235 op == SPECIAL_DIV_ASSIGN;
1238 static int evaluate_assign_op(struct expression *expr)
1240 struct symbol *target = expr->left->ctype;
1241 struct symbol *source = expr->right->ctype;
1242 struct symbol *t, *s;
1243 int tclass = classify_type(target, &t);
1244 int sclass = classify_type(source, &s);
1245 int op = expr->op;
1247 if (tclass & sclass & TYPE_NUM) {
1248 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1249 expression_error(expr, "invalid assignment");
1250 return 0;
1252 if (tclass & TYPE_RESTRICT) {
1253 if (!restricted_binop(op, t)) {
1254 warning(expr->pos, "bad assignment (%s) to %s",
1255 show_special(op), show_typename(t));
1256 expr->right = cast_to(expr->right, target);
1257 return 0;
1259 /* allowed assignments unfoul */
1260 if (sclass & TYPE_FOULED && unfoul(s) == t)
1261 goto Cast;
1262 if (!restricted_value(expr->right, t))
1263 return 1;
1264 } else if (!(sclass & TYPE_RESTRICT))
1265 goto usual;
1266 /* source and target would better be identical restricted */
1267 if (t == s)
1268 return 1;
1269 warning(expr->pos, "invalid assignment: %s", show_special(op));
1270 info(expr->pos, " left side has type %s", show_typename(t));
1271 info(expr->pos, " right side has type %s", show_typename(s));
1272 expr->right = cast_to(expr->right, target);
1273 return 0;
1275 if (tclass == TYPE_PTR && is_int(sclass)) {
1276 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1277 unrestrict(expr->right, sclass, &s);
1278 evaluate_ptr_add(expr, s);
1279 return 1;
1281 expression_error(expr, "invalid pointer assignment");
1282 return 0;
1285 expression_error(expr, "invalid assignment");
1286 return 0;
1288 usual:
1289 target = usual_conversions(op, expr->left, expr->right,
1290 tclass, sclass, target, source);
1291 Cast:
1292 expr->right = cast_to(expr->right, target);
1293 return 1;
1296 static int whitelist_pointers(struct symbol *t1, struct symbol *t2)
1298 if (t1 == t2)
1299 return 0; /* yes, 0 - we don't want a cast_to here */
1300 if (t1 == &void_ctype)
1301 return 1;
1302 if (t2 == &void_ctype)
1303 return 1;
1304 if (classify_type(t1, &t1) != TYPE_NUM)
1305 return 0;
1306 if (classify_type(t2, &t2) != TYPE_NUM)
1307 return 0;
1308 if (t1 == t2)
1309 return 1;
1310 if (t1->ctype.modifiers & t2->ctype.modifiers & MOD_CHAR)
1311 return 1;
1312 if ((t1->ctype.modifiers ^ t2->ctype.modifiers) & MOD_SIZE)
1313 return 0;
1314 return !Wtypesign;
1317 static int check_assignment_types(struct symbol *target, struct expression **rp,
1318 const char **typediff)
1320 struct symbol *source = degenerate(*rp);
1321 struct symbol *t, *s;
1322 int tclass = classify_type(target, &t);
1323 int sclass = classify_type(source, &s);
1325 if (tclass & sclass & TYPE_NUM) {
1326 if (tclass & TYPE_RESTRICT) {
1327 /* allowed assignments unfoul */
1328 if (sclass & TYPE_FOULED && unfoul(s) == t)
1329 goto Cast;
1330 if (!restricted_value(*rp, target))
1331 return 1;
1332 if (s == t)
1333 return 1;
1334 } else if (!(sclass & TYPE_RESTRICT))
1335 goto Cast;
1336 *typediff = "different base types";
1337 return 0;
1340 if (tclass == TYPE_PTR) {
1341 unsigned long mod1, mod2;
1342 struct symbol *b1, *b2;
1343 // NULL pointer is always OK
1344 int is_null = is_null_pointer_constant(*rp);
1345 if (is_null) {
1346 if (is_null == 2)
1347 bad_null(*rp);
1348 goto Cast;
1350 if (!(sclass & TYPE_PTR)) {
1351 *typediff = "different base types";
1352 return 0;
1354 b1 = examine_pointer_target(t);
1355 b2 = examine_pointer_target(s);
1356 mod1 = target_qualifiers(t);
1357 mod2 = target_qualifiers(s);
1358 if (whitelist_pointers(b1, b2)) {
1360 * assignments to/from void * are OK, provided that
1361 * we do not remove qualifiers from pointed to [C]
1362 * or mix address spaces [sparse].
1364 if (t->ctype.as != s->ctype.as) {
1365 *typediff = "different address spaces";
1366 return 0;
1369 * If this is a function pointer assignment, it is
1370 * actually fine to assign a pointer to const data to
1371 * it, as a function pointer points to const data
1372 * implicitly, i.e., dereferencing it does not produce
1373 * an lvalue.
1375 if (b1->type == SYM_FN)
1376 mod1 |= MOD_CONST;
1377 if (mod2 & ~mod1) {
1378 *typediff = "different modifiers";
1379 return 0;
1381 goto Cast;
1383 /* It's OK if the target is more volatile or const than the source */
1384 *typediff = type_difference(&t->ctype, &s->ctype, 0, mod1);
1385 if (*typediff)
1386 return 0;
1387 return 1;
1390 if ((tclass & TYPE_COMPOUND) && s == t)
1391 return 1;
1393 if (tclass & TYPE_NUM) {
1394 /* XXX: need to turn into comparison with NULL */
1395 if (t == &bool_ctype && (sclass & TYPE_PTR))
1396 goto Cast;
1397 *typediff = "different base types";
1398 return 0;
1400 *typediff = "invalid types";
1401 return 0;
1403 Cast:
1404 *rp = cast_to(*rp, target);
1405 return 1;
1408 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1409 struct expression **rp, const char *where)
1411 const char *typediff;
1412 struct symbol *source = degenerate(*rp);
1414 if (!check_assignment_types(target, rp, &typediff)) {
1415 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1416 info(expr->pos, " expected %s", show_typename(target));
1417 info(expr->pos, " got %s", show_typename(source));
1418 *rp = cast_to(*rp, target);
1419 return 0;
1422 return 1;
1425 static int compatible_transparent_union(struct symbol *target,
1426 struct expression **rp)
1428 struct symbol *t, *member;
1429 classify_type(target, &t);
1430 if (t->type != SYM_UNION || !t->transparent_union)
1431 return 0;
1433 FOR_EACH_PTR(t->symbol_list, member) {
1434 const char *typediff;
1435 if (check_assignment_types(member, rp, &typediff))
1436 return 1;
1437 } END_FOR_EACH_PTR(member);
1439 return 0;
1442 static int compatible_argument_type(struct expression *expr, struct symbol *target,
1443 struct expression **rp, const char *where)
1445 if (compatible_transparent_union(target, rp))
1446 return 1;
1448 return compatible_assignment_types(expr, target, rp, where);
1451 static void mark_assigned(struct expression *expr)
1453 struct symbol *sym;
1455 if (!expr)
1456 return;
1457 switch (expr->type) {
1458 case EXPR_SYMBOL:
1459 sym = expr->symbol;
1460 if (!sym)
1461 return;
1462 if (sym->type != SYM_NODE)
1463 return;
1464 sym->ctype.modifiers |= MOD_ASSIGNED;
1465 return;
1467 case EXPR_BINOP:
1468 mark_assigned(expr->left);
1469 mark_assigned(expr->right);
1470 return;
1471 case EXPR_CAST:
1472 case EXPR_FORCE_CAST:
1473 mark_assigned(expr->cast_expression);
1474 return;
1475 case EXPR_SLICE:
1476 mark_assigned(expr->base);
1477 return;
1478 default:
1479 /* Hmm? */
1480 return;
1484 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1486 if (type->ctype.modifiers & MOD_CONST)
1487 expression_error(left, "assignment to const expression");
1489 /* We know left is an lvalue, so it's a "preop-*" */
1490 mark_assigned(left->unop);
1493 static struct symbol *evaluate_assignment(struct expression *expr)
1495 struct expression *left = expr->left;
1496 struct expression *where = expr;
1497 struct symbol *ltype;
1499 if (!lvalue_expression(left)) {
1500 expression_error(expr, "not an lvalue");
1501 return NULL;
1504 ltype = left->ctype;
1506 if (expr->op != '=') {
1507 if (!evaluate_assign_op(expr))
1508 return NULL;
1509 } else {
1510 if (!compatible_assignment_types(where, ltype, &expr->right, "assignment"))
1511 return NULL;
1514 evaluate_assign_to(left, ltype);
1516 expr->ctype = ltype;
1517 return ltype;
1520 static void examine_fn_arguments(struct symbol *fn)
1522 struct symbol *s;
1524 FOR_EACH_PTR(fn->arguments, s) {
1525 struct symbol *arg = evaluate_symbol(s);
1526 /* Array/function arguments silently degenerate into pointers */
1527 if (arg) {
1528 struct symbol *ptr;
1529 switch(arg->type) {
1530 case SYM_ARRAY:
1531 case SYM_FN:
1532 ptr = alloc_symbol(s->pos, SYM_PTR);
1533 if (arg->type == SYM_ARRAY)
1534 ptr->ctype = arg->ctype;
1535 else
1536 ptr->ctype.base_type = arg;
1537 ptr->ctype.as |= s->ctype.as;
1538 ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1540 s->ctype.base_type = ptr;
1541 s->ctype.as = 0;
1542 s->ctype.modifiers &= ~MOD_PTRINHERIT;
1543 s->bit_size = 0;
1544 s->examined = 0;
1545 examine_symbol_type(s);
1546 break;
1547 default:
1548 /* nothing */
1549 break;
1552 } END_FOR_EACH_PTR(s);
1555 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1557 /* Take the modifiers of the pointer, and apply them to the member */
1558 mod |= sym->ctype.modifiers;
1559 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1560 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1561 *newsym = *sym;
1562 newsym->ctype.as = as;
1563 newsym->ctype.modifiers = mod;
1564 sym = newsym;
1566 return sym;
1569 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1571 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1572 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1574 node->ctype.base_type = ptr;
1575 ptr->bit_size = bits_in_pointer;
1576 ptr->ctype.alignment = pointer_alignment;
1578 node->bit_size = bits_in_pointer;
1579 node->ctype.alignment = pointer_alignment;
1581 access_symbol(sym);
1582 if (sym->ctype.modifiers & MOD_REGISTER) {
1583 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1584 sym->ctype.modifiers &= ~MOD_REGISTER;
1586 if (sym->type == SYM_NODE) {
1587 ptr->ctype.as |= sym->ctype.as;
1588 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1589 sym = sym->ctype.base_type;
1591 if (degenerate && sym->type == SYM_ARRAY) {
1592 ptr->ctype.as |= sym->ctype.as;
1593 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1594 sym = sym->ctype.base_type;
1596 ptr->ctype.base_type = sym;
1598 return node;
1601 /* Arrays degenerate into pointers on pointer arithmetic */
1602 static struct symbol *degenerate(struct expression *expr)
1604 struct symbol *ctype, *base;
1606 if (!expr)
1607 return NULL;
1608 ctype = expr->ctype;
1609 if (!ctype)
1610 return NULL;
1611 base = examine_symbol_type(ctype);
1612 if (ctype->type == SYM_NODE)
1613 base = ctype->ctype.base_type;
1615 * Arrays degenerate into pointers to the entries, while
1616 * functions degenerate into pointers to themselves.
1617 * If array was part of non-lvalue compound, we create a copy
1618 * of that compound first and then act as if we were dealing with
1619 * the corresponding field in there.
1621 switch (base->type) {
1622 case SYM_ARRAY:
1623 if (expr->type == EXPR_SLICE) {
1624 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1625 struct expression *e0, *e1, *e2, *e3, *e4;
1627 a->ctype.base_type = expr->base->ctype;
1628 a->bit_size = expr->base->ctype->bit_size;
1629 a->array_size = expr->base->ctype->array_size;
1631 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1632 e0->symbol = a;
1633 e0->ctype = &lazy_ptr_ctype;
1635 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1636 e1->unop = e0;
1637 e1->op = '*';
1638 e1->ctype = expr->base->ctype; /* XXX */
1640 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1641 e2->left = e1;
1642 e2->right = expr->base;
1643 e2->op = '=';
1644 e2->ctype = expr->base->ctype;
1646 if (expr->r_bitpos) {
1647 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1648 e3->op = '+';
1649 e3->left = e0;
1650 e3->right = alloc_const_expression(expr->pos,
1651 bits_to_bytes(expr->r_bitpos));
1652 e3->ctype = &lazy_ptr_ctype;
1653 } else {
1654 e3 = e0;
1657 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1658 e4->left = e2;
1659 e4->right = e3;
1660 e4->ctype = &lazy_ptr_ctype;
1662 expr->unop = e4;
1663 expr->type = EXPR_PREOP;
1664 expr->op = '*';
1666 case SYM_FN:
1667 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1668 expression_error(expr, "strange non-value function or array");
1669 return &bad_ctype;
1671 *expr = *expr->unop;
1672 ctype = create_pointer(expr, ctype, 1);
1673 expr->ctype = ctype;
1674 default:
1675 /* nothing */;
1677 return ctype;
1680 static struct symbol *evaluate_addressof(struct expression *expr)
1682 struct expression *op = expr->unop;
1683 struct symbol *ctype;
1685 if (op->op != '*' || op->type != EXPR_PREOP) {
1686 expression_error(expr, "not addressable");
1687 return NULL;
1689 ctype = op->ctype;
1690 *expr = *op->unop;
1691 expr->flags = 0;
1693 if (expr->type == EXPR_SYMBOL) {
1694 struct symbol *sym = expr->symbol;
1695 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1699 * symbol expression evaluation is lazy about the type
1700 * of the sub-expression, so we may have to generate
1701 * the type here if so..
1703 if (expr->ctype == &lazy_ptr_ctype) {
1704 ctype = create_pointer(expr, ctype, 0);
1705 expr->ctype = ctype;
1707 return expr->ctype;
1711 static struct symbol *evaluate_dereference(struct expression *expr)
1713 struct expression *op = expr->unop;
1714 struct symbol *ctype = op->ctype, *node, *target;
1716 /* Simplify: *&(expr) => (expr) */
1717 if (op->type == EXPR_PREOP && op->op == '&') {
1718 *expr = *op->unop;
1719 expr->flags = 0;
1720 return expr->ctype;
1723 /* Dereferencing a node drops all the node information. */
1724 if (ctype->type == SYM_NODE)
1725 ctype = ctype->ctype.base_type;
1727 node = alloc_symbol(expr->pos, SYM_NODE);
1728 target = ctype->ctype.base_type;
1730 switch (ctype->type) {
1731 default:
1732 expression_error(expr, "cannot dereference this type");
1733 return NULL;
1734 case SYM_PTR:
1735 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1736 merge_type(node, ctype);
1737 break;
1739 case SYM_ARRAY:
1740 if (!lvalue_expression(op)) {
1741 expression_error(op, "non-lvalue array??");
1742 return NULL;
1745 /* Do the implied "addressof" on the array */
1746 *op = *op->unop;
1749 * When an array is dereferenced, we need to pick
1750 * up the attributes of the original node too..
1752 merge_type(node, op->ctype);
1753 merge_type(node, ctype);
1754 break;
1757 node->bit_size = target->bit_size;
1758 node->array_size = target->array_size;
1760 expr->ctype = node;
1761 return node;
1765 * Unary post-ops: x++ and x--
1767 static struct symbol *evaluate_postop(struct expression *expr)
1769 struct expression *op = expr->unop;
1770 struct symbol *ctype = op->ctype;
1771 int class = classify_type(ctype, &ctype);
1772 int multiply = 0;
1774 if (!class || class & TYPE_COMPOUND) {
1775 expression_error(expr, "need scalar for ++/--");
1776 return NULL;
1778 if (!lvalue_expression(expr->unop)) {
1779 expression_error(expr, "need lvalue expression for ++/--");
1780 return NULL;
1783 if ((class & TYPE_RESTRICT) && restricted_unop(expr->op, &ctype))
1784 unrestrict(expr, class, &ctype);
1786 if (class & TYPE_NUM) {
1787 multiply = 1;
1788 } else if (class == TYPE_PTR) {
1789 struct symbol *target = examine_pointer_target(ctype);
1790 if (!is_function(target))
1791 multiply = bits_to_bytes(target->bit_size);
1794 if (multiply) {
1795 evaluate_assign_to(op, op->ctype);
1796 expr->op_value = multiply;
1797 expr->ctype = ctype;
1798 return ctype;
1801 expression_error(expr, "bad argument type for ++/--");
1802 return NULL;
1805 static struct symbol *evaluate_sign(struct expression *expr)
1807 struct symbol *ctype = expr->unop->ctype;
1808 int class = classify_type(ctype, &ctype);
1809 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1810 expr->flags = 0;
1811 /* should be an arithmetic type */
1812 if (!(class & TYPE_NUM))
1813 return bad_expr_type(expr);
1814 if (class & TYPE_RESTRICT)
1815 goto Restr;
1816 Normal:
1817 if (!(class & TYPE_FLOAT)) {
1818 ctype = integer_promotion(ctype);
1819 expr->unop = cast_to(expr->unop, ctype);
1820 } else if (expr->op != '~') {
1821 /* no conversions needed */
1822 } else {
1823 return bad_expr_type(expr);
1825 if (expr->op == '+')
1826 *expr = *expr->unop;
1827 expr->ctype = ctype;
1828 return ctype;
1829 Restr:
1830 if (restricted_unop(expr->op, &ctype))
1831 unrestrict(expr, class, &ctype);
1832 goto Normal;
1835 static struct symbol *evaluate_preop(struct expression *expr)
1837 struct symbol *ctype = expr->unop->ctype;
1839 switch (expr->op) {
1840 case '(':
1841 *expr = *expr->unop;
1842 return ctype;
1844 case '+':
1845 case '-':
1846 case '~':
1847 return evaluate_sign(expr);
1849 case '*':
1850 return evaluate_dereference(expr);
1852 case '&':
1853 return evaluate_addressof(expr);
1855 case SPECIAL_INCREMENT:
1856 case SPECIAL_DECREMENT:
1858 * From a type evaluation standpoint the preops are
1859 * the same as the postops
1861 return evaluate_postop(expr);
1863 case '!':
1864 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1865 expr->flags = 0;
1866 if (is_safe_type(ctype))
1867 warning(expr->pos, "testing a 'safe expression'");
1868 if (is_float_type(ctype)) {
1869 struct expression *arg = expr->unop;
1870 expr->type = EXPR_COMPARE;
1871 expr->op = SPECIAL_EQUAL;
1872 expr->left = arg;
1873 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1874 expr->right->ctype = ctype;
1875 expr->right->fvalue = 0;
1876 } else if (is_fouled_type(ctype)) {
1877 warning(expr->pos, "%s degrades to integer",
1878 show_typename(ctype->ctype.base_type));
1880 /* the result is int [6.5.3.3(5)]*/
1881 ctype = &int_ctype;
1882 break;
1884 default:
1885 break;
1887 expr->ctype = ctype;
1888 return ctype;
1891 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1893 struct ptr_list *head = (struct ptr_list *)_list;
1894 struct ptr_list *list = head;
1896 if (!head)
1897 return NULL;
1898 do {
1899 int i;
1900 for (i = 0; i < list->nr; i++) {
1901 struct symbol *sym = (struct symbol *) list->list[i];
1902 if (sym->ident) {
1903 if (sym->ident != ident)
1904 continue;
1905 *offset = sym->offset;
1906 return sym;
1907 } else {
1908 struct symbol *ctype = sym->ctype.base_type;
1909 struct symbol *sub;
1910 if (!ctype)
1911 continue;
1912 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1913 continue;
1914 sub = find_identifier(ident, ctype->symbol_list, offset);
1915 if (!sub)
1916 continue;
1917 *offset += sym->offset;
1918 return sub;
1921 } while ((list = list->next) != head);
1922 return NULL;
1925 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1927 struct expression *add;
1930 * Create a new add-expression
1932 * NOTE! Even if we just add zero, we need a new node
1933 * for the member pointer, since it has a different
1934 * type than the original pointer. We could make that
1935 * be just a cast, but the fact is, a node is a node,
1936 * so we might as well just do the "add zero" here.
1938 add = alloc_expression(expr->pos, EXPR_BINOP);
1939 add->op = '+';
1940 add->left = expr;
1941 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1942 add->right->ctype = &int_ctype;
1943 add->right->value = offset;
1946 * The ctype of the pointer will be lazily evaluated if
1947 * we ever take the address of this member dereference..
1949 add->ctype = &lazy_ptr_ctype;
1950 return add;
1953 /* structure/union dereference */
1954 static struct symbol *evaluate_member_dereference(struct expression *expr)
1956 int offset;
1957 struct symbol *ctype, *member;
1958 struct expression *deref = expr->deref, *add;
1959 struct ident *ident = expr->member;
1960 unsigned int mod;
1961 int address_space;
1963 if (!evaluate_expression(deref))
1964 return NULL;
1965 if (!ident) {
1966 expression_error(expr, "bad member name");
1967 return NULL;
1970 ctype = deref->ctype;
1971 examine_symbol_type(ctype);
1972 address_space = ctype->ctype.as;
1973 mod = ctype->ctype.modifiers;
1974 if (ctype->type == SYM_NODE) {
1975 ctype = ctype->ctype.base_type;
1976 address_space |= ctype->ctype.as;
1977 mod |= ctype->ctype.modifiers;
1979 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1980 expression_error(expr, "expected structure or union");
1981 return NULL;
1983 offset = 0;
1984 member = find_identifier(ident, ctype->symbol_list, &offset);
1985 if (!member) {
1986 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1987 const char *name = "<unnamed>";
1988 int namelen = 9;
1989 if (ctype->ident) {
1990 name = ctype->ident->name;
1991 namelen = ctype->ident->len;
1993 if (ctype->symbol_list)
1994 expression_error(expr, "no member '%s' in %s %.*s",
1995 show_ident(ident), type, namelen, name);
1996 else
1997 expression_error(expr, "using member '%s' in "
1998 "incomplete %s %.*s", show_ident(ident),
1999 type, namelen, name);
2000 return NULL;
2004 * The member needs to take on the address space and modifiers of
2005 * the "parent" type.
2007 member = convert_to_as_mod(member, address_space, mod);
2008 ctype = get_base_type(member);
2010 if (!lvalue_expression(deref)) {
2011 if (deref->type != EXPR_SLICE) {
2012 expr->base = deref;
2013 expr->r_bitpos = 0;
2014 } else {
2015 expr->base = deref->base;
2016 expr->r_bitpos = deref->r_bitpos;
2018 expr->r_bitpos += bytes_to_bits(offset);
2019 expr->type = EXPR_SLICE;
2020 expr->r_nrbits = member->bit_size;
2021 expr->r_bitpos += member->bit_offset;
2022 expr->ctype = member;
2023 return member;
2026 deref = deref->unop;
2027 expr->deref = deref;
2029 add = evaluate_offset(deref, offset);
2030 expr->type = EXPR_PREOP;
2031 expr->op = '*';
2032 expr->unop = add;
2034 expr->ctype = member;
2035 return member;
2038 static int is_promoted(struct expression *expr)
2040 while (1) {
2041 switch (expr->type) {
2042 case EXPR_BINOP:
2043 case EXPR_SELECT:
2044 case EXPR_CONDITIONAL:
2045 return 1;
2046 case EXPR_COMMA:
2047 expr = expr->right;
2048 continue;
2049 case EXPR_PREOP:
2050 switch (expr->op) {
2051 case '(':
2052 expr = expr->unop;
2053 continue;
2054 case '+':
2055 case '-':
2056 case '~':
2057 return 1;
2058 default:
2059 return 0;
2061 default:
2062 return 0;
2068 static struct symbol *evaluate_cast(struct expression *);
2070 static struct symbol *evaluate_type_information(struct expression *expr)
2072 struct symbol *sym = expr->cast_type;
2073 if (!sym) {
2074 sym = evaluate_expression(expr->cast_expression);
2075 if (!sym)
2076 return NULL;
2078 * Expressions of restricted types will possibly get
2079 * promoted - check that here
2081 if (is_restricted_type(sym)) {
2082 if (sym->bit_size < bits_in_int && is_promoted(expr))
2083 sym = &int_ctype;
2084 } else if (is_fouled_type(sym)) {
2085 sym = &int_ctype;
2088 examine_symbol_type(sym);
2089 if (is_bitfield_type(sym)) {
2090 expression_error(expr, "trying to examine bitfield type");
2091 return NULL;
2093 return sym;
2096 static struct symbol *evaluate_sizeof(struct expression *expr)
2098 struct symbol *type;
2099 int size;
2101 type = evaluate_type_information(expr);
2102 if (!type)
2103 return NULL;
2105 size = type->bit_size;
2107 if (size < 0 && is_void_type(type)) {
2108 warning(expr->pos, "expression using sizeof(void)");
2109 size = bits_in_char;
2112 if (size == 1 && is_bool_type(type)) {
2113 if (Wsizeof_bool)
2114 warning(expr->pos, "expression using sizeof bool");
2115 size = bits_in_char;
2118 if (is_function(type->ctype.base_type)) {
2119 warning(expr->pos, "expression using sizeof on a function");
2120 size = bits_in_char;
2123 if ((size < 0) || (size & (bits_in_char - 1)))
2124 expression_error(expr, "cannot size expression");
2126 expr->type = EXPR_VALUE;
2127 expr->value = bits_to_bytes(size);
2128 expr->taint = 0;
2129 expr->ctype = size_t_ctype;
2130 return size_t_ctype;
2133 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
2135 struct symbol *type;
2136 int size;
2138 type = evaluate_type_information(expr);
2139 if (!type)
2140 return NULL;
2142 if (type->type == SYM_NODE)
2143 type = type->ctype.base_type;
2144 if (!type)
2145 return NULL;
2146 switch (type->type) {
2147 case SYM_ARRAY:
2148 break;
2149 case SYM_PTR:
2150 type = get_base_type(type);
2151 if (type)
2152 break;
2153 default:
2154 expression_error(expr, "expected pointer expression");
2155 return NULL;
2157 size = type->bit_size;
2158 if (size & (bits_in_char-1))
2159 size = 0;
2160 expr->type = EXPR_VALUE;
2161 expr->value = bits_to_bytes(size);
2162 expr->taint = 0;
2163 expr->ctype = size_t_ctype;
2164 return size_t_ctype;
2167 static struct symbol *evaluate_alignof(struct expression *expr)
2169 struct symbol *type;
2171 type = evaluate_type_information(expr);
2172 if (!type)
2173 return NULL;
2175 expr->type = EXPR_VALUE;
2176 expr->value = type->ctype.alignment;
2177 expr->taint = 0;
2178 expr->ctype = size_t_ctype;
2179 return size_t_ctype;
2182 static int evaluate_arguments(struct symbol *fn, struct expression_list *head)
2184 struct expression *expr;
2185 struct symbol_list *argument_types = fn->arguments;
2186 struct symbol *argtype;
2187 int i = 1;
2189 PREPARE_PTR_LIST(argument_types, argtype);
2190 FOR_EACH_PTR (head, expr) {
2191 struct expression **p = THIS_ADDRESS(expr);
2192 struct symbol *ctype, *target;
2193 ctype = evaluate_expression(expr);
2195 if (!ctype)
2196 return 0;
2198 target = argtype;
2199 if (!target) {
2200 struct symbol *type;
2201 int class = classify_type(ctype, &type);
2202 if (is_int(class)) {
2203 *p = cast_to(expr, integer_promotion(type));
2204 } else if (class & TYPE_FLOAT) {
2205 unsigned long mod = type->ctype.modifiers;
2206 if (!(mod & (MOD_LONG_ALL)))
2207 *p = cast_to(expr, &double_ctype);
2208 } else if (class & TYPE_PTR) {
2209 if (expr->ctype == &null_ctype)
2210 *p = cast_to(expr, &ptr_ctype);
2211 else
2212 degenerate(expr);
2214 } else if (!target->forced_arg){
2215 static char where[30];
2216 examine_symbol_type(target);
2217 sprintf(where, "argument %d", i);
2218 compatible_argument_type(expr, target, p, where);
2221 i++;
2222 NEXT_PTR_LIST(argtype);
2223 } END_FOR_EACH_PTR(expr);
2224 FINISH_PTR_LIST(argtype);
2225 return 1;
2228 static void convert_index(struct expression *e)
2230 struct expression *child = e->idx_expression;
2231 unsigned from = e->idx_from;
2232 unsigned to = e->idx_to + 1;
2233 e->type = EXPR_POS;
2234 e->init_offset = from * bits_to_bytes(e->ctype->bit_size);
2235 e->init_nr = to - from;
2236 e->init_expr = child;
2239 static void convert_ident(struct expression *e)
2241 struct expression *child = e->ident_expression;
2242 int offset = e->offset;
2244 e->type = EXPR_POS;
2245 e->init_offset = offset;
2246 e->init_nr = 1;
2247 e->init_expr = child;
2250 static void convert_designators(struct expression *e)
2252 while (e) {
2253 if (e->type == EXPR_INDEX)
2254 convert_index(e);
2255 else if (e->type == EXPR_IDENTIFIER)
2256 convert_ident(e);
2257 else
2258 break;
2259 e = e->init_expr;
2263 static void excess(struct expression *e, const char *s)
2265 warning(e->pos, "excessive elements in %s initializer", s);
2269 * implicit designator for the first element
2271 static struct expression *first_subobject(struct symbol *ctype, int class,
2272 struct expression **v)
2274 struct expression *e = *v, *new;
2276 if (ctype->type == SYM_NODE)
2277 ctype = ctype->ctype.base_type;
2279 if (class & TYPE_PTR) { /* array */
2280 if (!ctype->bit_size)
2281 return NULL;
2282 new = alloc_expression(e->pos, EXPR_INDEX);
2283 new->idx_expression = e;
2284 new->ctype = ctype->ctype.base_type;
2285 } else {
2286 struct symbol *field, *p;
2287 PREPARE_PTR_LIST(ctype->symbol_list, p);
2288 while (p && !p->ident && is_bitfield_type(p))
2289 NEXT_PTR_LIST(p);
2290 field = p;
2291 FINISH_PTR_LIST(p);
2292 if (!field)
2293 return NULL;
2294 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2295 new->ident_expression = e;
2296 new->field = new->ctype = field;
2297 new->offset = field->offset;
2299 *v = new;
2300 return new;
2304 * sanity-check explicit designators; return the innermost one or NULL
2305 * in case of error. Assign types.
2307 static struct expression *check_designators(struct expression *e,
2308 struct symbol *ctype)
2310 struct expression *last = NULL;
2311 const char *err;
2312 while (1) {
2313 if (ctype->type == SYM_NODE)
2314 ctype = ctype->ctype.base_type;
2315 if (e->type == EXPR_INDEX) {
2316 struct symbol *type;
2317 if (ctype->type != SYM_ARRAY) {
2318 err = "array index in non-array";
2319 break;
2321 type = ctype->ctype.base_type;
2322 if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2323 unsigned offset = array_element_offset(type->bit_size, e->idx_to);
2324 if (offset >= ctype->bit_size) {
2325 err = "index out of bounds in";
2326 break;
2329 e->ctype = ctype = type;
2330 ctype = type;
2331 last = e;
2332 if (!e->idx_expression) {
2333 err = "invalid";
2334 break;
2336 e = e->idx_expression;
2337 } else if (e->type == EXPR_IDENTIFIER) {
2338 int offset = 0;
2339 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2340 err = "field name not in struct or union";
2341 break;
2343 ctype = find_identifier(e->expr_ident, ctype->symbol_list, &offset);
2344 if (!ctype) {
2345 err = "unknown field name in";
2346 break;
2348 e->offset = offset;
2349 e->field = e->ctype = ctype;
2350 last = e;
2351 if (!e->ident_expression) {
2352 err = "invalid";
2353 break;
2355 e = e->ident_expression;
2356 } else if (e->type == EXPR_POS) {
2357 err = "internal front-end error: EXPR_POS in";
2358 break;
2359 } else
2360 return last;
2362 expression_error(e, "%s initializer", err);
2363 return NULL;
2367 * choose the next subobject to initialize.
2369 * Get designators for next element, switch old ones to EXPR_POS.
2370 * Return the resulting expression or NULL if we'd run out of subobjects.
2371 * The innermost designator is returned in *v. Designators in old
2372 * are assumed to be already sanity-checked.
2374 static struct expression *next_designators(struct expression *old,
2375 struct symbol *ctype,
2376 struct expression *e, struct expression **v)
2378 struct expression *new = NULL;
2380 if (!old)
2381 return NULL;
2382 if (old->type == EXPR_INDEX) {
2383 struct expression *copy;
2384 unsigned n;
2386 copy = next_designators(old->idx_expression,
2387 old->ctype, e, v);
2388 if (!copy) {
2389 n = old->idx_to + 1;
2390 if (array_element_offset(old->ctype->bit_size, n) == ctype->bit_size) {
2391 convert_index(old);
2392 return NULL;
2394 copy = e;
2395 *v = new = alloc_expression(e->pos, EXPR_INDEX);
2396 } else {
2397 n = old->idx_to;
2398 new = alloc_expression(e->pos, EXPR_INDEX);
2401 new->idx_from = new->idx_to = n;
2402 new->idx_expression = copy;
2403 new->ctype = old->ctype;
2404 convert_index(old);
2405 } else if (old->type == EXPR_IDENTIFIER) {
2406 struct expression *copy;
2407 struct symbol *field;
2408 int offset = 0;
2410 copy = next_designators(old->ident_expression,
2411 old->ctype, e, v);
2412 if (!copy) {
2413 field = old->field->next_subobject;
2414 if (!field) {
2415 convert_ident(old);
2416 return NULL;
2418 copy = e;
2419 *v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2421 * We can't necessarily trust "field->offset",
2422 * because the field might be in an anonymous
2423 * union, and the field offset is then the offset
2424 * within that union.
2426 * The "old->offset - old->field->offset"
2427 * would be the offset of such an anonymous
2428 * union.
2430 offset = old->offset - old->field->offset;
2431 } else {
2432 field = old->field;
2433 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2436 new->field = field;
2437 new->expr_ident = field->ident;
2438 new->ident_expression = copy;
2439 new->ctype = field;
2440 new->offset = field->offset + offset;
2441 convert_ident(old);
2443 return new;
2446 static int handle_simple_initializer(struct expression **ep, int nested,
2447 int class, struct symbol *ctype);
2450 * deal with traversing subobjects [6.7.8(17,18,20)]
2452 static void handle_list_initializer(struct expression *expr,
2453 int class, struct symbol *ctype)
2455 struct expression *e, *last = NULL, *top = NULL, *next;
2456 int jumped = 0;
2458 FOR_EACH_PTR(expr->expr_list, e) {
2459 struct expression **v;
2460 struct symbol *type;
2461 int lclass;
2463 if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2464 struct symbol *struct_sym;
2465 if (!top) {
2466 top = e;
2467 last = first_subobject(ctype, class, &top);
2468 } else {
2469 last = next_designators(last, ctype, e, &top);
2471 if (!last) {
2472 excess(e, class & TYPE_PTR ? "array" :
2473 "struct or union");
2474 DELETE_CURRENT_PTR(e);
2475 continue;
2477 struct_sym = ctype->type == SYM_NODE ? ctype->ctype.base_type : ctype;
2478 if (Wdesignated_init && struct_sym->designated_init)
2479 warning(e->pos, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2480 ctype->ident ? "in initializer for " : "",
2481 ctype->ident ? ctype->ident->len : 0,
2482 ctype->ident ? ctype->ident->name : "",
2483 ctype->ident ? ": " : "",
2484 get_type_name(struct_sym->type),
2485 show_ident(struct_sym->ident));
2486 if (jumped) {
2487 warning(e->pos, "advancing past deep designator");
2488 jumped = 0;
2490 REPLACE_CURRENT_PTR(e, last);
2491 } else {
2492 next = check_designators(e, ctype);
2493 if (!next) {
2494 DELETE_CURRENT_PTR(e);
2495 continue;
2497 top = next;
2498 /* deeper than one designator? */
2499 jumped = top != e;
2500 convert_designators(last);
2501 last = e;
2504 found:
2505 lclass = classify_type(top->ctype, &type);
2506 if (top->type == EXPR_INDEX)
2507 v = &top->idx_expression;
2508 else
2509 v = &top->ident_expression;
2511 if (handle_simple_initializer(v, 1, lclass, top->ctype))
2512 continue;
2514 if (!(lclass & TYPE_COMPOUND)) {
2515 warning(e->pos, "bogus scalar initializer");
2516 DELETE_CURRENT_PTR(e);
2517 continue;
2520 next = first_subobject(type, lclass, v);
2521 if (next) {
2522 warning(e->pos, "missing braces around initializer");
2523 top = next;
2524 goto found;
2527 DELETE_CURRENT_PTR(e);
2528 excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2530 } END_FOR_EACH_PTR(e);
2532 convert_designators(last);
2533 expr->ctype = ctype;
2536 static int is_string_literal(struct expression **v)
2538 struct expression *e = *v;
2539 while (e && e->type == EXPR_PREOP && e->op == '(')
2540 e = e->unop;
2541 if (!e || e->type != EXPR_STRING)
2542 return 0;
2543 if (e != *v && Wparen_string)
2544 warning(e->pos,
2545 "array initialized from parenthesized string constant");
2546 *v = e;
2547 return 1;
2551 * We want a normal expression, possibly in one layer of braces. Warn
2552 * if the latter happens inside a list (it's legal, but likely to be
2553 * an effect of screwup). In case of anything not legal, we are definitely
2554 * having an effect of screwup, so just fail and let the caller warn.
2556 static struct expression *handle_scalar(struct expression *e, int nested)
2558 struct expression *v = NULL, *p;
2559 int count = 0;
2561 /* normal case */
2562 if (e->type != EXPR_INITIALIZER)
2563 return e;
2565 FOR_EACH_PTR(e->expr_list, p) {
2566 if (!v)
2567 v = p;
2568 count++;
2569 } END_FOR_EACH_PTR(p);
2570 if (count != 1)
2571 return NULL;
2572 switch(v->type) {
2573 case EXPR_INITIALIZER:
2574 case EXPR_INDEX:
2575 case EXPR_IDENTIFIER:
2576 return NULL;
2577 default:
2578 break;
2580 if (nested)
2581 warning(e->pos, "braces around scalar initializer");
2582 return v;
2586 * deal with the cases that don't care about subobjects:
2587 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2588 * character array <- string literal, possibly in braces [6.7.8(14)]
2589 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2590 * compound type <- initializer list in braces [6.7.8(16)]
2591 * The last one punts to handle_list_initializer() which, in turn will call
2592 * us for individual elements of the list.
2594 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2595 * the lack of support of wide char stuff in general.
2597 * One note: we need to take care not to evaluate a string literal until
2598 * we know that we *will* handle it right here. Otherwise we would screw
2599 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2600 * { "string", ...} - we need to preserve that string literal recognizable
2601 * until we dig into the inner struct.
2603 static int handle_simple_initializer(struct expression **ep, int nested,
2604 int class, struct symbol *ctype)
2606 int is_string = is_string_type(ctype);
2607 struct expression *e = *ep, *p;
2608 struct symbol *type;
2610 if (!e)
2611 return 0;
2613 /* scalar */
2614 if (!(class & TYPE_COMPOUND)) {
2615 e = handle_scalar(e, nested);
2616 if (!e)
2617 return 0;
2618 *ep = e;
2619 if (!evaluate_expression(e))
2620 return 1;
2621 compatible_assignment_types(e, ctype, ep, "initializer");
2622 return 1;
2626 * sublist; either a string, or we dig in; the latter will deal with
2627 * pathologies, so we don't need anything fancy here.
2629 if (e->type == EXPR_INITIALIZER) {
2630 if (is_string) {
2631 struct expression *v = NULL;
2632 int count = 0;
2634 FOR_EACH_PTR(e->expr_list, p) {
2635 if (!v)
2636 v = p;
2637 count++;
2638 } END_FOR_EACH_PTR(p);
2639 if (count == 1 && is_string_literal(&v)) {
2640 *ep = e = v;
2641 goto String;
2644 handle_list_initializer(e, class, ctype);
2645 return 1;
2648 /* string */
2649 if (is_string_literal(&e)) {
2650 /* either we are doing array of char, or we'll have to dig in */
2651 if (is_string) {
2652 *ep = e;
2653 goto String;
2655 return 0;
2657 /* struct or union can be initialized by compatible */
2658 if (class != TYPE_COMPOUND)
2659 return 0;
2660 type = evaluate_expression(e);
2661 if (!type)
2662 return 0;
2663 if (ctype->type == SYM_NODE)
2664 ctype = ctype->ctype.base_type;
2665 if (type->type == SYM_NODE)
2666 type = type->ctype.base_type;
2667 if (ctype == type)
2668 return 1;
2669 return 0;
2671 String:
2672 p = alloc_expression(e->pos, EXPR_STRING);
2673 *p = *e;
2674 type = evaluate_expression(p);
2675 if (ctype->bit_size != -1) {
2676 if (ctype->bit_size + bits_in_char < type->bit_size)
2677 warning(e->pos,
2678 "too long initializer-string for array of char");
2679 else if (Winit_cstring && ctype->bit_size + bits_in_char == type->bit_size) {
2680 warning(e->pos,
2681 "too long initializer-string for array of char(no space for nul char)");
2684 *ep = p;
2685 return 1;
2688 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2690 struct symbol *type;
2691 int class = classify_type(ctype, &type);
2692 if (!handle_simple_initializer(ep, 0, class, ctype))
2693 expression_error(*ep, "invalid initializer");
2696 static struct symbol *cast_to_bool(struct expression *expr)
2698 struct expression *old = expr->cast_expression;
2699 struct expression *zero;
2700 struct symbol *otype;
2701 int oclass = classify_type(degenerate(old), &otype);
2702 struct symbol *ctype;
2704 if (oclass & TYPE_COMPOUND)
2705 return NULL;
2707 zero = alloc_const_expression(expr->pos, 0);
2708 expr->op = SPECIAL_NOTEQUAL;
2709 ctype = usual_conversions(expr->op, old, zero,
2710 oclass, TYPE_NUM, otype, zero->ctype);
2711 expr->type = EXPR_COMPARE;
2712 expr->left = cast_to(old, ctype);
2713 expr->right = cast_to(zero, ctype);
2715 return expr->ctype;
2718 static struct symbol *evaluate_cast(struct expression *expr)
2720 struct expression *target = expr->cast_expression;
2721 struct symbol *ctype;
2722 struct symbol *t1, *t2;
2723 int class1, class2;
2724 int as1 = 0, as2 = 0;
2726 if (!target)
2727 return NULL;
2730 * Special case: a cast can be followed by an
2731 * initializer, in which case we need to pass
2732 * the type value down to that initializer rather
2733 * than trying to evaluate it as an expression
2735 * A more complex case is when the initializer is
2736 * dereferenced as part of a post-fix expression.
2737 * We need to produce an expression that can be dereferenced.
2739 if (target->type == EXPR_INITIALIZER) {
2740 struct symbol *sym = expr->cast_type;
2741 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2743 sym->initializer = target;
2744 evaluate_symbol(sym);
2746 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2747 addr->symbol = sym;
2749 expr->type = EXPR_PREOP;
2750 expr->op = '*';
2751 expr->unop = addr;
2752 expr->ctype = sym;
2754 return sym;
2757 ctype = examine_symbol_type(expr->cast_type);
2758 expr->ctype = ctype;
2759 expr->cast_type = ctype;
2761 evaluate_expression(target);
2762 degenerate(target);
2764 class1 = classify_type(ctype, &t1);
2766 /* cast to non-integer type -> not an integer constant expression */
2767 if (!is_int(class1))
2768 expr->flags = 0;
2769 /* if argument turns out to be not an integer constant expression *and*
2770 it was not a floating literal to start with -> too bad */
2771 else if (expr->flags == Int_const_expr &&
2772 !(target->flags & Int_const_expr))
2773 expr->flags = 0;
2775 * You can always throw a value away by casting to
2776 * "void" - that's an implicit "force". Note that
2777 * the same is _not_ true of "void *".
2779 if (t1 == &void_ctype)
2780 goto out;
2782 if (class1 & (TYPE_COMPOUND | TYPE_FN))
2783 warning(expr->pos, "cast to non-scalar");
2785 t2 = target->ctype;
2786 if (!t2) {
2787 expression_error(expr, "cast from unknown type");
2788 goto out;
2790 class2 = classify_type(t2, &t2);
2792 if (class2 & TYPE_COMPOUND)
2793 warning(expr->pos, "cast from non-scalar");
2795 if (expr->type == EXPR_FORCE_CAST)
2796 goto out;
2798 /* allowed cast unfouls */
2799 if (class2 & TYPE_FOULED)
2800 t2 = unfoul(t2);
2802 if (t1 != t2) {
2803 if (class1 & TYPE_RESTRICT)
2804 warning(expr->pos, "cast to %s",
2805 show_typename(t1));
2806 if (class2 & TYPE_RESTRICT)
2807 warning(expr->pos, "cast from %s",
2808 show_typename(t2));
2811 if (t1 == &ulong_ctype)
2812 as1 = -1;
2813 else if (class1 == TYPE_PTR) {
2814 examine_pointer_target(t1);
2815 as1 = t1->ctype.as;
2818 if (t2 == &ulong_ctype)
2819 as2 = -1;
2820 else if (class2 == TYPE_PTR) {
2821 examine_pointer_target(t2);
2822 as2 = t2->ctype.as;
2825 if (!as1 && as2 > 0)
2826 warning(expr->pos, "cast removes address space of expression");
2827 if (as1 > 0 && as2 > 0 && as1 != as2)
2828 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2829 if (as1 > 0 && !as2 &&
2830 !is_null_pointer_constant(target) && Wcast_to_as)
2831 warning(expr->pos,
2832 "cast adds address space to expression (<asn:%d>)", as1);
2834 if (!(t1->ctype.modifiers & MOD_PTRINHERIT) && class1 == TYPE_PTR &&
2835 !as1 && (target->flags & Int_const_expr)) {
2836 if (t1->ctype.base_type == &void_ctype) {
2837 if (is_zero_constant(target)) {
2838 /* NULL */
2839 expr->type = EXPR_VALUE;
2840 expr->ctype = &null_ctype;
2841 expr->value = 0;
2842 return ctype;
2847 if (t1 == &bool_ctype)
2848 cast_to_bool(expr);
2850 out:
2851 return ctype;
2855 * Evaluate a call expression with a symbol. This
2856 * should expand inline functions, and evaluate
2857 * builtins.
2859 static int evaluate_symbol_call(struct expression *expr)
2861 struct expression *fn = expr->fn;
2862 struct symbol *ctype = fn->ctype;
2864 if (fn->type != EXPR_PREOP)
2865 return 0;
2867 if (ctype->op && ctype->op->evaluate)
2868 return ctype->op->evaluate(expr);
2870 if (ctype->ctype.modifiers & MOD_INLINE) {
2871 int ret;
2872 struct symbol *curr = current_fn;
2874 if (ctype->definition)
2875 ctype = ctype->definition;
2877 current_fn = ctype->ctype.base_type;
2879 ret = inline_function(expr, ctype);
2881 /* restore the old function */
2882 current_fn = curr;
2883 return ret;
2886 return 0;
2889 static struct symbol *evaluate_call(struct expression *expr)
2891 int args, fnargs;
2892 struct symbol *ctype, *sym;
2893 struct expression *fn = expr->fn;
2894 struct expression_list *arglist = expr->args;
2896 if (!evaluate_expression(fn))
2897 return NULL;
2898 sym = ctype = fn->ctype;
2899 if (ctype->type == SYM_NODE)
2900 ctype = ctype->ctype.base_type;
2901 if (ctype->type == SYM_PTR)
2902 ctype = get_base_type(ctype);
2904 if (ctype->type != SYM_FN) {
2905 struct expression *arg;
2906 expression_error(expr, "not a function %s",
2907 show_ident(sym->ident));
2908 /* do typechecking in arguments */
2909 FOR_EACH_PTR (arglist, arg) {
2910 evaluate_expression(arg);
2911 } END_FOR_EACH_PTR(arg);
2912 return NULL;
2915 examine_fn_arguments(ctype);
2916 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
2917 sym->op && sym->op->args) {
2918 if (!sym->op->args(expr))
2919 return NULL;
2920 } else {
2921 if (!evaluate_arguments(ctype, arglist))
2922 return NULL;
2923 args = expression_list_size(expr->args);
2924 fnargs = symbol_list_size(ctype->arguments);
2925 if (args < fnargs)
2926 expression_error(expr,
2927 "not enough arguments for function %s",
2928 show_ident(sym->ident));
2929 if (args > fnargs && !ctype->variadic)
2930 expression_error(expr,
2931 "too many arguments for function %s",
2932 show_ident(sym->ident));
2934 if (sym->type == SYM_NODE) {
2935 if (evaluate_symbol_call(expr))
2936 return expr->ctype;
2938 expr->ctype = ctype->ctype.base_type;
2939 return expr->ctype;
2942 static struct symbol *evaluate_offsetof(struct expression *expr)
2944 struct expression *e = expr->down;
2945 struct symbol *ctype = expr->in;
2946 int class;
2948 if (expr->op == '.') {
2949 struct symbol *field;
2950 int offset = 0;
2951 if (!ctype) {
2952 expression_error(expr, "expected structure or union");
2953 return NULL;
2955 examine_symbol_type(ctype);
2956 class = classify_type(ctype, &ctype);
2957 if (class != TYPE_COMPOUND) {
2958 expression_error(expr, "expected structure or union");
2959 return NULL;
2962 field = find_identifier(expr->ident, ctype->symbol_list, &offset);
2963 if (!field) {
2964 expression_error(expr, "unknown member");
2965 return NULL;
2967 ctype = field;
2968 expr->type = EXPR_VALUE;
2969 expr->flags = Int_const_expr;
2970 expr->value = offset;
2971 expr->taint = 0;
2972 expr->ctype = size_t_ctype;
2973 } else {
2974 if (!ctype) {
2975 expression_error(expr, "expected structure or union");
2976 return NULL;
2978 examine_symbol_type(ctype);
2979 class = classify_type(ctype, &ctype);
2980 if (class != (TYPE_COMPOUND | TYPE_PTR)) {
2981 expression_error(expr, "expected array");
2982 return NULL;
2984 ctype = ctype->ctype.base_type;
2985 if (!expr->index) {
2986 expr->type = EXPR_VALUE;
2987 expr->flags = Int_const_expr;
2988 expr->value = 0;
2989 expr->taint = 0;
2990 expr->ctype = size_t_ctype;
2991 } else {
2992 struct expression *idx = expr->index, *m;
2993 struct symbol *i_type = evaluate_expression(idx);
2994 int i_class = classify_type(i_type, &i_type);
2995 if (!is_int(i_class)) {
2996 expression_error(expr, "non-integer index");
2997 return NULL;
2999 unrestrict(idx, i_class, &i_type);
3000 idx = cast_to(idx, size_t_ctype);
3001 m = alloc_const_expression(expr->pos,
3002 bits_to_bytes(ctype->bit_size));
3003 m->ctype = size_t_ctype;
3004 m->flags = Int_const_expr;
3005 expr->type = EXPR_BINOP;
3006 expr->left = idx;
3007 expr->right = m;
3008 expr->op = '*';
3009 expr->ctype = size_t_ctype;
3010 expr->flags = m->flags & idx->flags & Int_const_expr;
3013 if (e) {
3014 struct expression *copy = __alloc_expression(0);
3015 *copy = *expr;
3016 if (e->type == EXPR_OFFSETOF)
3017 e->in = ctype;
3018 if (!evaluate_expression(e))
3019 return NULL;
3020 expr->type = EXPR_BINOP;
3021 expr->flags = e->flags & copy->flags & Int_const_expr;
3022 expr->op = '+';
3023 expr->ctype = size_t_ctype;
3024 expr->left = copy;
3025 expr->right = e;
3027 return size_t_ctype;
3030 struct symbol *evaluate_expression(struct expression *expr)
3032 if (!expr)
3033 return NULL;
3034 if (expr->ctype)
3035 return expr->ctype;
3037 switch (expr->type) {
3038 case EXPR_VALUE:
3039 case EXPR_FVALUE:
3040 expression_error(expr, "value expression without a type");
3041 return NULL;
3042 case EXPR_STRING:
3043 return evaluate_string(expr);
3044 case EXPR_SYMBOL:
3045 return evaluate_symbol_expression(expr);
3046 case EXPR_BINOP:
3047 if (!evaluate_expression(expr->left))
3048 return NULL;
3049 if (!evaluate_expression(expr->right))
3050 return NULL;
3051 return evaluate_binop(expr);
3052 case EXPR_LOGICAL:
3053 return evaluate_logical(expr);
3054 case EXPR_COMMA:
3055 evaluate_expression(expr->left);
3056 if (!evaluate_expression(expr->right))
3057 return NULL;
3058 return evaluate_comma(expr);
3059 case EXPR_COMPARE:
3060 if (!evaluate_expression(expr->left))
3061 return NULL;
3062 if (!evaluate_expression(expr->right))
3063 return NULL;
3064 return evaluate_compare(expr);
3065 case EXPR_ASSIGNMENT:
3066 if (!evaluate_expression(expr->left))
3067 return NULL;
3068 if (!evaluate_expression(expr->right))
3069 return NULL;
3070 return evaluate_assignment(expr);
3071 case EXPR_PREOP:
3072 if (!evaluate_expression(expr->unop))
3073 return NULL;
3074 return evaluate_preop(expr);
3075 case EXPR_POSTOP:
3076 if (!evaluate_expression(expr->unop))
3077 return NULL;
3078 return evaluate_postop(expr);
3079 case EXPR_CAST:
3080 case EXPR_FORCE_CAST:
3081 case EXPR_IMPLIED_CAST:
3082 return evaluate_cast(expr);
3083 case EXPR_SIZEOF:
3084 return evaluate_sizeof(expr);
3085 case EXPR_PTRSIZEOF:
3086 return evaluate_ptrsizeof(expr);
3087 case EXPR_ALIGNOF:
3088 return evaluate_alignof(expr);
3089 case EXPR_DEREF:
3090 return evaluate_member_dereference(expr);
3091 case EXPR_CALL:
3092 return evaluate_call(expr);
3093 case EXPR_SELECT:
3094 case EXPR_CONDITIONAL:
3095 return evaluate_conditional_expression(expr);
3096 case EXPR_STATEMENT:
3097 expr->ctype = evaluate_statement(expr->statement);
3098 return expr->ctype;
3100 case EXPR_LABEL:
3101 expr->ctype = &ptr_ctype;
3102 return &ptr_ctype;
3104 case EXPR_TYPE:
3105 /* Evaluate the type of the symbol .. */
3106 evaluate_symbol(expr->symbol);
3107 /* .. but the type of the _expression_ is a "type" */
3108 expr->ctype = &type_ctype;
3109 return &type_ctype;
3111 case EXPR_OFFSETOF:
3112 return evaluate_offsetof(expr);
3114 /* These can not exist as stand-alone expressions */
3115 case EXPR_INITIALIZER:
3116 case EXPR_IDENTIFIER:
3117 case EXPR_INDEX:
3118 case EXPR_POS:
3119 expression_error(expr, "internal front-end error: initializer in expression");
3120 return NULL;
3121 case EXPR_SLICE:
3122 expression_error(expr, "internal front-end error: SLICE re-evaluated");
3123 return NULL;
3125 return NULL;
3128 static void check_duplicates(struct symbol *sym)
3130 int declared = 0;
3131 struct symbol *next = sym;
3132 int initialized = sym->initializer != NULL;
3134 while ((next = next->same_symbol) != NULL) {
3135 const char *typediff;
3136 evaluate_symbol(next);
3137 if (initialized && next->initializer) {
3138 sparse_error(sym->pos, "symbol '%s' has multiple initializers (originally initialized at %s:%d)",
3139 show_ident(sym->ident),
3140 stream_name(next->pos.stream), next->pos.line);
3141 /* Only warn once */
3142 initialized = 0;
3144 declared++;
3145 typediff = type_difference(&sym->ctype, &next->ctype, 0, 0);
3146 if (typediff) {
3147 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
3148 show_ident(sym->ident),
3149 stream_name(next->pos.stream), next->pos.line, typediff);
3150 return;
3153 if (!declared) {
3154 unsigned long mod = sym->ctype.modifiers;
3155 if (mod & (MOD_STATIC | MOD_REGISTER))
3156 return;
3157 if (!(mod & MOD_TOPLEVEL))
3158 return;
3159 if (!Wdecl)
3160 return;
3161 if (sym->ident == &main_ident)
3162 return;
3163 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
3167 static struct symbol *evaluate_symbol(struct symbol *sym)
3169 struct symbol *base_type;
3171 if (!sym)
3172 return sym;
3173 if (sym->evaluated)
3174 return sym;
3175 sym->evaluated = 1;
3177 sym = examine_symbol_type(sym);
3178 base_type = get_base_type(sym);
3179 if (!base_type)
3180 return NULL;
3182 /* Evaluate the initializers */
3183 if (sym->initializer)
3184 evaluate_initializer(sym, &sym->initializer);
3186 /* And finally, evaluate the body of the symbol too */
3187 if (base_type->type == SYM_FN) {
3188 struct symbol *curr = current_fn;
3190 if (sym->definition && sym->definition != sym)
3191 return evaluate_symbol(sym->definition);
3193 current_fn = base_type;
3195 examine_fn_arguments(base_type);
3196 if (!base_type->stmt && base_type->inline_stmt)
3197 uninline(sym);
3198 if (base_type->stmt)
3199 evaluate_statement(base_type->stmt);
3201 current_fn = curr;
3204 return base_type;
3207 void evaluate_symbol_list(struct symbol_list *list)
3209 struct symbol *sym;
3211 FOR_EACH_PTR(list, sym) {
3212 evaluate_symbol(sym);
3213 check_duplicates(sym);
3214 } END_FOR_EACH_PTR(sym);
3217 static struct symbol *evaluate_return_expression(struct statement *stmt)
3219 struct expression *expr = stmt->expression;
3220 struct symbol *fntype;
3222 evaluate_expression(expr);
3223 fntype = current_fn->ctype.base_type;
3224 if (!fntype || fntype == &void_ctype) {
3225 if (expr && expr->ctype != &void_ctype)
3226 expression_error(expr, "return expression in %s function", fntype?"void":"typeless");
3227 if (expr && Wreturn_void)
3228 warning(stmt->pos, "returning void-valued expression");
3229 return NULL;
3232 if (!expr) {
3233 sparse_error(stmt->pos, "return with no return value");
3234 return NULL;
3236 if (!expr->ctype)
3237 return NULL;
3238 compatible_assignment_types(expr, fntype, &stmt->expression, "return expression");
3239 return NULL;
3242 static void evaluate_if_statement(struct statement *stmt)
3244 if (!stmt->if_conditional)
3245 return;
3247 evaluate_conditional(stmt->if_conditional, 0);
3248 evaluate_statement(stmt->if_true);
3249 evaluate_statement(stmt->if_false);
3252 static void evaluate_iterator(struct statement *stmt)
3254 evaluate_symbol_list(stmt->iterator_syms);
3255 evaluate_conditional(stmt->iterator_pre_condition, 1);
3256 evaluate_conditional(stmt->iterator_post_condition,1);
3257 evaluate_statement(stmt->iterator_pre_statement);
3258 evaluate_statement(stmt->iterator_statement);
3259 evaluate_statement(stmt->iterator_post_statement);
3262 static void verify_output_constraint(struct expression *expr, const char *constraint)
3264 switch (*constraint) {
3265 case '=': /* Assignment */
3266 case '+': /* Update */
3267 break;
3268 default:
3269 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
3273 static void verify_input_constraint(struct expression *expr, const char *constraint)
3275 switch (*constraint) {
3276 case '=': /* Assignment */
3277 case '+': /* Update */
3278 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
3282 static void evaluate_asm_statement(struct statement *stmt)
3284 struct expression *expr;
3285 struct symbol *sym;
3286 int state;
3288 expr = stmt->asm_string;
3289 if (!expr || expr->type != EXPR_STRING) {
3290 sparse_error(stmt->pos, "need constant string for inline asm");
3291 return;
3294 state = 0;
3295 FOR_EACH_PTR(stmt->asm_outputs, expr) {
3296 switch (state) {
3297 case 0: /* Identifier */
3298 state = 1;
3299 continue;
3301 case 1: /* Constraint */
3302 state = 2;
3303 if (!expr || expr->type != EXPR_STRING) {
3304 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
3305 *THIS_ADDRESS(expr) = NULL;
3306 continue;
3308 verify_output_constraint(expr, expr->string->data);
3309 continue;
3311 case 2: /* Expression */
3312 state = 0;
3313 if (!evaluate_expression(expr))
3314 return;
3315 if (!lvalue_expression(expr))
3316 warning(expr->pos, "asm output is not an lvalue");
3317 evaluate_assign_to(expr, expr->ctype);
3318 continue;
3320 } END_FOR_EACH_PTR(expr);
3322 state = 0;
3323 FOR_EACH_PTR(stmt->asm_inputs, expr) {
3324 switch (state) {
3325 case 0: /* Identifier */
3326 state = 1;
3327 continue;
3329 case 1: /* Constraint */
3330 state = 2;
3331 if (!expr || expr->type != EXPR_STRING) {
3332 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
3333 *THIS_ADDRESS(expr) = NULL;
3334 continue;
3336 verify_input_constraint(expr, expr->string->data);
3337 continue;
3339 case 2: /* Expression */
3340 state = 0;
3341 if (!evaluate_expression(expr))
3342 return;
3343 continue;
3345 } END_FOR_EACH_PTR(expr);
3347 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
3348 if (!expr) {
3349 sparse_error(stmt->pos, "bad asm clobbers");
3350 return;
3352 if (expr->type == EXPR_STRING)
3353 continue;
3354 expression_error(expr, "asm clobber is not a string");
3355 } END_FOR_EACH_PTR(expr);
3357 FOR_EACH_PTR(stmt->asm_labels, sym) {
3358 if (!sym || sym->type != SYM_LABEL) {
3359 sparse_error(stmt->pos, "bad asm label");
3360 return;
3362 } END_FOR_EACH_PTR(sym);
3365 static void evaluate_case_statement(struct statement *stmt)
3367 evaluate_expression(stmt->case_expression);
3368 evaluate_expression(stmt->case_to);
3369 evaluate_statement(stmt->case_statement);
3372 static void check_case_type(struct expression *switch_expr,
3373 struct expression *case_expr,
3374 struct expression **enumcase)
3376 struct symbol *switch_type, *case_type;
3377 int sclass, cclass;
3379 if (!case_expr)
3380 return;
3382 switch_type = switch_expr->ctype;
3383 case_type = evaluate_expression(case_expr);
3385 if (!switch_type || !case_type)
3386 goto Bad;
3387 if (enumcase) {
3388 if (*enumcase)
3389 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
3390 else if (is_enum_type(case_type))
3391 *enumcase = case_expr;
3394 sclass = classify_type(switch_type, &switch_type);
3395 cclass = classify_type(case_type, &case_type);
3397 /* both should be arithmetic */
3398 if (!(sclass & cclass & TYPE_NUM))
3399 goto Bad;
3401 /* neither should be floating */
3402 if ((sclass | cclass) & TYPE_FLOAT)
3403 goto Bad;
3405 /* if neither is restricted, we are OK */
3406 if (!((sclass | cclass) & TYPE_RESTRICT))
3407 return;
3409 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
3410 cclass, sclass, case_type, switch_type)) {
3411 unrestrict(case_expr, cclass, &case_type);
3412 unrestrict(switch_expr, sclass, &switch_type);
3414 return;
3416 Bad:
3417 expression_error(case_expr, "incompatible types for 'case' statement");
3420 static void evaluate_switch_statement(struct statement *stmt)
3422 struct symbol *sym;
3423 struct expression *enumcase = NULL;
3424 struct expression **enumcase_holder = &enumcase;
3425 struct expression *sel = stmt->switch_expression;
3427 evaluate_expression(sel);
3428 evaluate_statement(stmt->switch_statement);
3429 if (!sel)
3430 return;
3431 if (sel->ctype && is_enum_type(sel->ctype))
3432 enumcase_holder = NULL; /* Only check cases against switch */
3434 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
3435 struct statement *case_stmt = sym->stmt;
3436 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
3437 check_case_type(sel, case_stmt->case_to, enumcase_holder);
3438 } END_FOR_EACH_PTR(sym);
3441 static void evaluate_goto_statement(struct statement *stmt)
3443 struct symbol *label = stmt->goto_label;
3445 if (label && !label->stmt && !lookup_keyword(label->ident, NS_KEYWORD))
3446 sparse_error(stmt->pos, "label '%s' was not declared", show_ident(label->ident));
3448 evaluate_expression(stmt->goto_expression);
3451 struct symbol *evaluate_statement(struct statement *stmt)
3453 if (!stmt)
3454 return NULL;
3456 switch (stmt->type) {
3457 case STMT_DECLARATION: {
3458 struct symbol *s;
3459 FOR_EACH_PTR(stmt->declaration, s) {
3460 evaluate_symbol(s);
3461 } END_FOR_EACH_PTR(s);
3462 return NULL;
3465 case STMT_RETURN:
3466 return evaluate_return_expression(stmt);
3468 case STMT_EXPRESSION:
3469 if (!evaluate_expression(stmt->expression))
3470 return NULL;
3471 if (stmt->expression->ctype == &null_ctype)
3472 stmt->expression = cast_to(stmt->expression, &ptr_ctype);
3473 return degenerate(stmt->expression);
3475 case STMT_COMPOUND: {
3476 struct statement *s;
3477 struct symbol *type = NULL;
3479 /* Evaluate the return symbol in the compound statement */
3480 evaluate_symbol(stmt->ret);
3483 * Then, evaluate each statement, making the type of the
3484 * compound statement be the type of the last statement
3486 type = evaluate_statement(stmt->args);
3487 FOR_EACH_PTR(stmt->stmts, s) {
3488 type = evaluate_statement(s);
3489 } END_FOR_EACH_PTR(s);
3490 if (!type)
3491 type = &void_ctype;
3492 return type;
3494 case STMT_IF:
3495 evaluate_if_statement(stmt);
3496 return NULL;
3497 case STMT_ITERATOR:
3498 evaluate_iterator(stmt);
3499 return NULL;
3500 case STMT_SWITCH:
3501 evaluate_switch_statement(stmt);
3502 return NULL;
3503 case STMT_CASE:
3504 evaluate_case_statement(stmt);
3505 return NULL;
3506 case STMT_LABEL:
3507 return evaluate_statement(stmt->label_statement);
3508 case STMT_GOTO:
3509 evaluate_goto_statement(stmt);
3510 return NULL;
3511 case STMT_NONE:
3512 break;
3513 case STMT_ASM:
3514 evaluate_asm_statement(stmt);
3515 return NULL;
3516 case STMT_CONTEXT:
3517 evaluate_expression(stmt->expression);
3518 return NULL;
3519 case STMT_RANGE:
3520 evaluate_expression(stmt->range_expression);
3521 evaluate_expression(stmt->range_low);
3522 evaluate_expression(stmt->range_high);
3523 return NULL;
3525 return NULL;