warn if testing the address of a function
[smatch.git] / evaluate.c
blobd8ec1c2e38b6177f8d1bd1f17450c19aca82e6f5
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'");
872 if (is_func_type(ctype)) {
873 if (Waddress)
874 warning(expr->pos, "the address of %s will always evaluate as true", "a function");
875 } else if (!is_scalar_type(ctype)) {
876 sparse_error(expr->pos, "incorrect type in conditional");
877 info(expr->pos, " got %s", show_typename(ctype));
878 ctype = NULL;
882 return ctype;
885 static struct symbol *evaluate_logical(struct expression *expr)
887 if (!evaluate_conditional(expr->left, 0))
888 return NULL;
889 if (!evaluate_conditional(expr->right, 0))
890 return NULL;
892 /* the result is int [6.5.13(3), 6.5.14(3)] */
893 expr->ctype = &int_ctype;
894 if (expr->flags) {
895 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
896 expr->flags = 0;
898 return &int_ctype;
901 static struct symbol *evaluate_binop(struct expression *expr)
903 struct symbol *ltype, *rtype, *ctype;
904 int lclass = classify_type(expr->left->ctype, &ltype);
905 int rclass = classify_type(expr->right->ctype, &rtype);
906 int op = expr->op;
908 if (expr->flags) {
909 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
910 expr->flags = 0;
913 /* number op number */
914 if (lclass & rclass & TYPE_NUM) {
915 if ((lclass | rclass) & TYPE_FLOAT) {
916 switch (op) {
917 case '+': case '-': case '*': case '/':
918 break;
919 default:
920 return bad_expr_type(expr);
924 if (op == SPECIAL_LEFTSHIFT || op == SPECIAL_RIGHTSHIFT) {
925 // shifts do integer promotions, but that's it.
926 unrestrict(expr->left, lclass, &ltype);
927 unrestrict(expr->right, rclass, &rtype);
928 ctype = ltype = integer_promotion(ltype);
929 rtype = integer_promotion(rtype);
930 } else {
931 // The rest do usual conversions
932 const unsigned left_not = expr->left->type == EXPR_PREOP
933 && expr->left->op == '!';
934 const unsigned right_not = expr->right->type == EXPR_PREOP
935 && expr->right->op == '!';
936 if ((op == '&' || op == '|') && (left_not || right_not))
937 warning(expr->pos, "dubious: %sx %c %sy",
938 left_not ? "!" : "",
940 right_not ? "!" : "");
942 ltype = usual_conversions(op, expr->left, expr->right,
943 lclass, rclass, ltype, rtype);
944 ctype = rtype = ltype;
947 expr->left = cast_to(expr->left, ltype);
948 expr->right = cast_to(expr->right, rtype);
949 expr->ctype = ctype;
950 return ctype;
953 /* pointer (+|-) integer */
954 if (lclass & TYPE_PTR && is_int(rclass) && (op == '+' || op == '-')) {
955 unrestrict(expr->right, rclass, &rtype);
956 return evaluate_ptr_add(expr, rtype);
959 /* integer + pointer */
960 if (rclass & TYPE_PTR && is_int(lclass) && op == '+') {
961 struct expression *index = expr->left;
962 unrestrict(index, lclass, &ltype);
963 expr->left = expr->right;
964 expr->right = index;
965 return evaluate_ptr_add(expr, ltype);
968 /* pointer - pointer */
969 if (lclass & rclass & TYPE_PTR && expr->op == '-')
970 return evaluate_ptr_sub(expr);
972 return bad_expr_type(expr);
975 static struct symbol *evaluate_comma(struct expression *expr)
977 expr->ctype = degenerate(expr->right);
978 if (expr->ctype == &null_ctype)
979 expr->ctype = &ptr_ctype;
980 expr->flags &= expr->left->flags & expr->right->flags;
981 return expr->ctype;
984 static int modify_for_unsigned(int op)
986 if (op == '<')
987 op = SPECIAL_UNSIGNED_LT;
988 else if (op == '>')
989 op = SPECIAL_UNSIGNED_GT;
990 else if (op == SPECIAL_LTE)
991 op = SPECIAL_UNSIGNED_LTE;
992 else if (op == SPECIAL_GTE)
993 op = SPECIAL_UNSIGNED_GTE;
994 return op;
997 static inline int is_null_pointer_constant(struct expression *e)
999 if (e->ctype == &null_ctype)
1000 return 1;
1001 if (!(e->flags & Int_const_expr))
1002 return 0;
1003 return is_zero_constant(e) ? 2 : 0;
1006 static struct symbol *evaluate_compare(struct expression *expr)
1008 struct expression *left = expr->left, *right = expr->right;
1009 struct symbol *ltype, *rtype, *lbase, *rbase;
1010 int lclass = classify_type(degenerate(left), &ltype);
1011 int rclass = classify_type(degenerate(right), &rtype);
1012 struct symbol *ctype;
1013 const char *typediff;
1015 if (expr->flags) {
1016 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
1017 expr->flags = 0;
1020 /* Type types? */
1021 if (is_type_type(ltype) && is_type_type(rtype))
1022 goto OK;
1024 if (is_safe_type(left->ctype) || is_safe_type(right->ctype))
1025 warning(expr->pos, "testing a 'safe expression'");
1027 /* number on number */
1028 if (lclass & rclass & TYPE_NUM) {
1029 ctype = usual_conversions(expr->op, expr->left, expr->right,
1030 lclass, rclass, ltype, rtype);
1031 expr->left = cast_to(expr->left, ctype);
1032 expr->right = cast_to(expr->right, ctype);
1033 if (ctype->ctype.modifiers & MOD_UNSIGNED)
1034 expr->op = modify_for_unsigned(expr->op);
1035 goto OK;
1038 /* at least one must be a pointer */
1039 if (!((lclass | rclass) & TYPE_PTR))
1040 return bad_expr_type(expr);
1042 /* equality comparisons can be with null pointer constants */
1043 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1044 int is_null1 = is_null_pointer_constant(left);
1045 int is_null2 = is_null_pointer_constant(right);
1046 if (is_null1 == 2)
1047 bad_null(left);
1048 if (is_null2 == 2)
1049 bad_null(right);
1050 if (is_null1 && is_null2) {
1051 int positive = expr->op == SPECIAL_EQUAL;
1052 expr->type = EXPR_VALUE;
1053 expr->value = positive;
1054 goto OK;
1056 if (is_null1 && (rclass & TYPE_PTR)) {
1057 left = cast_to(left, rtype);
1058 goto OK;
1060 if (is_null2 && (lclass & TYPE_PTR)) {
1061 right = cast_to(right, ltype);
1062 goto OK;
1065 /* both should be pointers */
1066 if (!(lclass & rclass & TYPE_PTR))
1067 return bad_expr_type(expr);
1068 expr->op = modify_for_unsigned(expr->op);
1070 lbase = examine_pointer_target(ltype);
1071 rbase = examine_pointer_target(rtype);
1073 /* they also have special treatment for pointers to void */
1074 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1075 if (ltype->ctype.as == rtype->ctype.as) {
1076 if (lbase == &void_ctype) {
1077 right = cast_to(right, ltype);
1078 goto OK;
1080 if (rbase == &void_ctype) {
1081 left = cast_to(left, rtype);
1082 goto OK;
1087 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1088 target_qualifiers(rtype),
1089 target_qualifiers(ltype));
1090 if (!typediff)
1091 goto OK;
1093 expression_error(expr, "incompatible types in comparison expression (%s)", typediff);
1094 return NULL;
1097 /* the result is int [6.5.8(6), 6.5.9(3)]*/
1098 expr->ctype = &int_ctype;
1099 return &int_ctype;
1103 * NOTE! The degenerate case of "x ? : y", where we don't
1104 * have a true case, this will possibly promote "x" to the
1105 * same type as "y", and thus _change_ the conditional
1106 * test in the expression. But since promotion is "safe"
1107 * for testing, that's OK.
1109 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1111 struct expression **true;
1112 struct symbol *ctype, *ltype, *rtype, *lbase, *rbase;
1113 int lclass, rclass;
1114 const char * typediff;
1115 int qual;
1117 if (!evaluate_conditional(expr->conditional, 0))
1118 return NULL;
1119 if (!evaluate_expression(expr->cond_false))
1120 return NULL;
1122 ctype = degenerate(expr->conditional);
1123 rtype = degenerate(expr->cond_false);
1125 true = &expr->conditional;
1126 ltype = ctype;
1127 if (expr->cond_true) {
1128 if (!evaluate_expression(expr->cond_true))
1129 return NULL;
1130 ltype = degenerate(expr->cond_true);
1131 true = &expr->cond_true;
1134 if (expr->flags) {
1135 int flags = expr->conditional->flags & Int_const_expr;
1136 flags &= (*true)->flags & expr->cond_false->flags;
1137 if (!flags)
1138 expr->flags = 0;
1141 lclass = classify_type(ltype, &ltype);
1142 rclass = classify_type(rtype, &rtype);
1143 if (lclass & rclass & TYPE_NUM) {
1144 ctype = usual_conversions('?', *true, expr->cond_false,
1145 lclass, rclass, ltype, rtype);
1146 *true = cast_to(*true, ctype);
1147 expr->cond_false = cast_to(expr->cond_false, ctype);
1148 goto out;
1151 if ((lclass | rclass) & TYPE_PTR) {
1152 int is_null1 = is_null_pointer_constant(*true);
1153 int is_null2 = is_null_pointer_constant(expr->cond_false);
1155 if (is_null1 && is_null2) {
1156 *true = cast_to(*true, &ptr_ctype);
1157 expr->cond_false = cast_to(expr->cond_false, &ptr_ctype);
1158 ctype = &ptr_ctype;
1159 goto out;
1161 if (is_null1 && (rclass & TYPE_PTR)) {
1162 if (is_null1 == 2)
1163 bad_null(*true);
1164 *true = cast_to(*true, rtype);
1165 ctype = rtype;
1166 goto out;
1168 if (is_null2 && (lclass & TYPE_PTR)) {
1169 if (is_null2 == 2)
1170 bad_null(expr->cond_false);
1171 expr->cond_false = cast_to(expr->cond_false, ltype);
1172 ctype = ltype;
1173 goto out;
1175 if (!(lclass & rclass & TYPE_PTR)) {
1176 typediff = "different types";
1177 goto Err;
1179 /* OK, it's pointer on pointer */
1180 if (ltype->ctype.as != rtype->ctype.as) {
1181 typediff = "different address spaces";
1182 goto Err;
1185 /* need to be lazier here */
1186 lbase = examine_pointer_target(ltype);
1187 rbase = examine_pointer_target(rtype);
1188 qual = target_qualifiers(ltype) | target_qualifiers(rtype);
1190 if (lbase == &void_ctype) {
1191 /* XXX: pointers to function should warn here */
1192 ctype = ltype;
1193 goto Qual;
1196 if (rbase == &void_ctype) {
1197 /* XXX: pointers to function should warn here */
1198 ctype = rtype;
1199 goto Qual;
1201 /* XXX: that should be pointer to composite */
1202 ctype = ltype;
1203 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1204 qual, qual);
1205 if (!typediff)
1206 goto Qual;
1207 goto Err;
1210 /* void on void, struct on same struct, union on same union */
1211 if (ltype == rtype) {
1212 ctype = ltype;
1213 goto out;
1215 typediff = "different base types";
1217 Err:
1218 expression_error(expr, "incompatible types in conditional expression (%s)", typediff);
1219 return NULL;
1221 out:
1222 expr->ctype = ctype;
1223 return ctype;
1225 Qual:
1226 if (qual & ~ctype->ctype.modifiers) {
1227 struct symbol *sym = alloc_symbol(ctype->pos, SYM_PTR);
1228 *sym = *ctype;
1229 sym->ctype.modifiers |= qual;
1230 ctype = sym;
1232 *true = cast_to(*true, ctype);
1233 expr->cond_false = cast_to(expr->cond_false, ctype);
1234 goto out;
1237 /* FP assignments can not do modulo or bit operations */
1238 static int compatible_float_op(int op)
1240 return op == SPECIAL_ADD_ASSIGN ||
1241 op == SPECIAL_SUB_ASSIGN ||
1242 op == SPECIAL_MUL_ASSIGN ||
1243 op == SPECIAL_DIV_ASSIGN;
1246 static int evaluate_assign_op(struct expression *expr)
1248 struct symbol *target = expr->left->ctype;
1249 struct symbol *source = expr->right->ctype;
1250 struct symbol *t, *s;
1251 int tclass = classify_type(target, &t);
1252 int sclass = classify_type(source, &s);
1253 int op = expr->op;
1255 if (tclass & sclass & TYPE_NUM) {
1256 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1257 expression_error(expr, "invalid assignment");
1258 return 0;
1260 if (tclass & TYPE_RESTRICT) {
1261 if (!restricted_binop(op, t)) {
1262 warning(expr->pos, "bad assignment (%s) to %s",
1263 show_special(op), show_typename(t));
1264 expr->right = cast_to(expr->right, target);
1265 return 0;
1267 /* allowed assignments unfoul */
1268 if (sclass & TYPE_FOULED && unfoul(s) == t)
1269 goto Cast;
1270 if (!restricted_value(expr->right, t))
1271 return 1;
1272 } else if (!(sclass & TYPE_RESTRICT))
1273 goto usual;
1274 /* source and target would better be identical restricted */
1275 if (t == s)
1276 return 1;
1277 warning(expr->pos, "invalid assignment: %s", show_special(op));
1278 info(expr->pos, " left side has type %s", show_typename(t));
1279 info(expr->pos, " right side has type %s", show_typename(s));
1280 expr->right = cast_to(expr->right, target);
1281 return 0;
1283 if (tclass == TYPE_PTR && is_int(sclass)) {
1284 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1285 unrestrict(expr->right, sclass, &s);
1286 evaluate_ptr_add(expr, s);
1287 return 1;
1289 expression_error(expr, "invalid pointer assignment");
1290 return 0;
1293 expression_error(expr, "invalid assignment");
1294 return 0;
1296 usual:
1297 target = usual_conversions(op, expr->left, expr->right,
1298 tclass, sclass, target, source);
1299 Cast:
1300 expr->right = cast_to(expr->right, target);
1301 return 1;
1304 static int whitelist_pointers(struct symbol *t1, struct symbol *t2)
1306 if (t1 == t2)
1307 return 0; /* yes, 0 - we don't want a cast_to here */
1308 if (t1 == &void_ctype)
1309 return 1;
1310 if (t2 == &void_ctype)
1311 return 1;
1312 if (classify_type(t1, &t1) != TYPE_NUM)
1313 return 0;
1314 if (classify_type(t2, &t2) != TYPE_NUM)
1315 return 0;
1316 if (t1 == t2)
1317 return 1;
1318 if (t1->ctype.modifiers & t2->ctype.modifiers & MOD_CHAR)
1319 return 1;
1320 if ((t1->ctype.modifiers ^ t2->ctype.modifiers) & MOD_SIZE)
1321 return 0;
1322 return !Wtypesign;
1325 static int check_assignment_types(struct symbol *target, struct expression **rp,
1326 const char **typediff)
1328 struct symbol *source = degenerate(*rp);
1329 struct symbol *t, *s;
1330 int tclass = classify_type(target, &t);
1331 int sclass = classify_type(source, &s);
1333 if (tclass & sclass & TYPE_NUM) {
1334 if (tclass & TYPE_RESTRICT) {
1335 /* allowed assignments unfoul */
1336 if (sclass & TYPE_FOULED && unfoul(s) == t)
1337 goto Cast;
1338 if (!restricted_value(*rp, target))
1339 return 1;
1340 if (s == t)
1341 return 1;
1342 } else if (!(sclass & TYPE_RESTRICT))
1343 goto Cast;
1344 *typediff = "different base types";
1345 return 0;
1348 if (tclass == TYPE_PTR) {
1349 unsigned long mod1, mod2;
1350 struct symbol *b1, *b2;
1351 // NULL pointer is always OK
1352 int is_null = is_null_pointer_constant(*rp);
1353 if (is_null) {
1354 if (is_null == 2)
1355 bad_null(*rp);
1356 goto Cast;
1358 if (!(sclass & TYPE_PTR)) {
1359 *typediff = "different base types";
1360 return 0;
1362 b1 = examine_pointer_target(t);
1363 b2 = examine_pointer_target(s);
1364 mod1 = target_qualifiers(t);
1365 mod2 = target_qualifiers(s);
1366 if (whitelist_pointers(b1, b2)) {
1368 * assignments to/from void * are OK, provided that
1369 * we do not remove qualifiers from pointed to [C]
1370 * or mix address spaces [sparse].
1372 if (t->ctype.as != s->ctype.as) {
1373 *typediff = "different address spaces";
1374 return 0;
1377 * If this is a function pointer assignment, it is
1378 * actually fine to assign a pointer to const data to
1379 * it, as a function pointer points to const data
1380 * implicitly, i.e., dereferencing it does not produce
1381 * an lvalue.
1383 if (b1->type == SYM_FN)
1384 mod1 |= MOD_CONST;
1385 if (mod2 & ~mod1) {
1386 *typediff = "different modifiers";
1387 return 0;
1389 goto Cast;
1391 /* It's OK if the target is more volatile or const than the source */
1392 *typediff = type_difference(&t->ctype, &s->ctype, 0, mod1);
1393 if (*typediff)
1394 return 0;
1395 return 1;
1398 if ((tclass & TYPE_COMPOUND) && s == t)
1399 return 1;
1401 if (tclass & TYPE_NUM) {
1402 /* XXX: need to turn into comparison with NULL */
1403 if (t == &bool_ctype && (sclass & TYPE_PTR))
1404 goto Cast;
1405 *typediff = "different base types";
1406 return 0;
1408 *typediff = "invalid types";
1409 return 0;
1411 Cast:
1412 *rp = cast_to(*rp, target);
1413 return 1;
1416 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1417 struct expression **rp, const char *where)
1419 const char *typediff;
1420 struct symbol *source = degenerate(*rp);
1422 if (!check_assignment_types(target, rp, &typediff)) {
1423 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1424 info(expr->pos, " expected %s", show_typename(target));
1425 info(expr->pos, " got %s", show_typename(source));
1426 *rp = cast_to(*rp, target);
1427 return 0;
1430 return 1;
1433 static int compatible_transparent_union(struct symbol *target,
1434 struct expression **rp)
1436 struct symbol *t, *member;
1437 classify_type(target, &t);
1438 if (t->type != SYM_UNION || !t->transparent_union)
1439 return 0;
1441 FOR_EACH_PTR(t->symbol_list, member) {
1442 const char *typediff;
1443 if (check_assignment_types(member, rp, &typediff))
1444 return 1;
1445 } END_FOR_EACH_PTR(member);
1447 return 0;
1450 static int compatible_argument_type(struct expression *expr, struct symbol *target,
1451 struct expression **rp, const char *where)
1453 if (compatible_transparent_union(target, rp))
1454 return 1;
1456 return compatible_assignment_types(expr, target, rp, where);
1459 static void mark_assigned(struct expression *expr)
1461 struct symbol *sym;
1463 if (!expr)
1464 return;
1465 switch (expr->type) {
1466 case EXPR_SYMBOL:
1467 sym = expr->symbol;
1468 if (!sym)
1469 return;
1470 if (sym->type != SYM_NODE)
1471 return;
1472 sym->ctype.modifiers |= MOD_ASSIGNED;
1473 return;
1475 case EXPR_BINOP:
1476 mark_assigned(expr->left);
1477 mark_assigned(expr->right);
1478 return;
1479 case EXPR_CAST:
1480 case EXPR_FORCE_CAST:
1481 mark_assigned(expr->cast_expression);
1482 return;
1483 case EXPR_SLICE:
1484 mark_assigned(expr->base);
1485 return;
1486 default:
1487 /* Hmm? */
1488 return;
1492 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1494 if (type->ctype.modifiers & MOD_CONST)
1495 expression_error(left, "assignment to const expression");
1497 /* We know left is an lvalue, so it's a "preop-*" */
1498 mark_assigned(left->unop);
1501 static struct symbol *evaluate_assignment(struct expression *expr)
1503 struct expression *left = expr->left;
1504 struct expression *where = expr;
1505 struct symbol *ltype;
1507 if (!lvalue_expression(left)) {
1508 expression_error(expr, "not an lvalue");
1509 return NULL;
1512 ltype = left->ctype;
1514 if (expr->op != '=') {
1515 if (!evaluate_assign_op(expr))
1516 return NULL;
1517 } else {
1518 if (!compatible_assignment_types(where, ltype, &expr->right, "assignment"))
1519 return NULL;
1522 evaluate_assign_to(left, ltype);
1524 expr->ctype = ltype;
1525 return ltype;
1528 static void examine_fn_arguments(struct symbol *fn)
1530 struct symbol *s;
1532 FOR_EACH_PTR(fn->arguments, s) {
1533 struct symbol *arg = evaluate_symbol(s);
1534 /* Array/function arguments silently degenerate into pointers */
1535 if (arg) {
1536 struct symbol *ptr;
1537 switch(arg->type) {
1538 case SYM_ARRAY:
1539 case SYM_FN:
1540 ptr = alloc_symbol(s->pos, SYM_PTR);
1541 if (arg->type == SYM_ARRAY)
1542 ptr->ctype = arg->ctype;
1543 else
1544 ptr->ctype.base_type = arg;
1545 ptr->ctype.as |= s->ctype.as;
1546 ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1548 s->ctype.base_type = ptr;
1549 s->ctype.as = 0;
1550 s->ctype.modifiers &= ~MOD_PTRINHERIT;
1551 s->bit_size = 0;
1552 s->examined = 0;
1553 examine_symbol_type(s);
1554 break;
1555 default:
1556 /* nothing */
1557 break;
1560 } END_FOR_EACH_PTR(s);
1563 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1565 /* Take the modifiers of the pointer, and apply them to the member */
1566 mod |= sym->ctype.modifiers;
1567 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1568 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1569 *newsym = *sym;
1570 newsym->ctype.as = as;
1571 newsym->ctype.modifiers = mod;
1572 sym = newsym;
1574 return sym;
1577 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1579 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1580 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1582 node->ctype.base_type = ptr;
1583 ptr->bit_size = bits_in_pointer;
1584 ptr->ctype.alignment = pointer_alignment;
1586 node->bit_size = bits_in_pointer;
1587 node->ctype.alignment = pointer_alignment;
1589 access_symbol(sym);
1590 if (sym->ctype.modifiers & MOD_REGISTER) {
1591 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1592 sym->ctype.modifiers &= ~MOD_REGISTER;
1594 if (sym->type == SYM_NODE) {
1595 ptr->ctype.as |= sym->ctype.as;
1596 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1597 sym = sym->ctype.base_type;
1599 if (degenerate && sym->type == SYM_ARRAY) {
1600 ptr->ctype.as |= sym->ctype.as;
1601 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1602 sym = sym->ctype.base_type;
1604 ptr->ctype.base_type = sym;
1606 return node;
1609 /* Arrays degenerate into pointers on pointer arithmetic */
1610 static struct symbol *degenerate(struct expression *expr)
1612 struct symbol *ctype, *base;
1614 if (!expr)
1615 return NULL;
1616 ctype = expr->ctype;
1617 if (!ctype)
1618 return NULL;
1619 base = examine_symbol_type(ctype);
1620 if (ctype->type == SYM_NODE)
1621 base = ctype->ctype.base_type;
1623 * Arrays degenerate into pointers to the entries, while
1624 * functions degenerate into pointers to themselves.
1625 * If array was part of non-lvalue compound, we create a copy
1626 * of that compound first and then act as if we were dealing with
1627 * the corresponding field in there.
1629 switch (base->type) {
1630 case SYM_ARRAY:
1631 if (expr->type == EXPR_SLICE) {
1632 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1633 struct expression *e0, *e1, *e2, *e3, *e4;
1635 a->ctype.base_type = expr->base->ctype;
1636 a->bit_size = expr->base->ctype->bit_size;
1637 a->array_size = expr->base->ctype->array_size;
1639 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1640 e0->symbol = a;
1641 e0->ctype = &lazy_ptr_ctype;
1643 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1644 e1->unop = e0;
1645 e1->op = '*';
1646 e1->ctype = expr->base->ctype; /* XXX */
1648 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1649 e2->left = e1;
1650 e2->right = expr->base;
1651 e2->op = '=';
1652 e2->ctype = expr->base->ctype;
1654 if (expr->r_bitpos) {
1655 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1656 e3->op = '+';
1657 e3->left = e0;
1658 e3->right = alloc_const_expression(expr->pos,
1659 bits_to_bytes(expr->r_bitpos));
1660 e3->ctype = &lazy_ptr_ctype;
1661 } else {
1662 e3 = e0;
1665 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1666 e4->left = e2;
1667 e4->right = e3;
1668 e4->ctype = &lazy_ptr_ctype;
1670 expr->unop = e4;
1671 expr->type = EXPR_PREOP;
1672 expr->op = '*';
1674 case SYM_FN:
1675 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1676 expression_error(expr, "strange non-value function or array");
1677 return &bad_ctype;
1679 *expr = *expr->unop;
1680 ctype = create_pointer(expr, ctype, 1);
1681 expr->ctype = ctype;
1682 default:
1683 /* nothing */;
1685 return ctype;
1688 static struct symbol *evaluate_addressof(struct expression *expr)
1690 struct expression *op = expr->unop;
1691 struct symbol *ctype;
1693 if (op->op != '*' || op->type != EXPR_PREOP) {
1694 expression_error(expr, "not addressable");
1695 return NULL;
1697 ctype = op->ctype;
1698 *expr = *op->unop;
1699 expr->flags = 0;
1701 if (expr->type == EXPR_SYMBOL) {
1702 struct symbol *sym = expr->symbol;
1703 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1707 * symbol expression evaluation is lazy about the type
1708 * of the sub-expression, so we may have to generate
1709 * the type here if so..
1711 if (expr->ctype == &lazy_ptr_ctype) {
1712 ctype = create_pointer(expr, ctype, 0);
1713 expr->ctype = ctype;
1715 return expr->ctype;
1719 static struct symbol *evaluate_dereference(struct expression *expr)
1721 struct expression *op = expr->unop;
1722 struct symbol *ctype = op->ctype, *node, *target;
1724 /* Simplify: *&(expr) => (expr) */
1725 if (op->type == EXPR_PREOP && op->op == '&') {
1726 *expr = *op->unop;
1727 expr->flags = 0;
1728 return expr->ctype;
1731 /* Dereferencing a node drops all the node information. */
1732 if (ctype->type == SYM_NODE)
1733 ctype = ctype->ctype.base_type;
1735 node = alloc_symbol(expr->pos, SYM_NODE);
1736 target = ctype->ctype.base_type;
1738 switch (ctype->type) {
1739 default:
1740 expression_error(expr, "cannot dereference this type");
1741 return NULL;
1742 case SYM_PTR:
1743 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1744 merge_type(node, ctype);
1745 break;
1747 case SYM_ARRAY:
1748 if (!lvalue_expression(op)) {
1749 expression_error(op, "non-lvalue array??");
1750 return NULL;
1753 /* Do the implied "addressof" on the array */
1754 *op = *op->unop;
1757 * When an array is dereferenced, we need to pick
1758 * up the attributes of the original node too..
1760 merge_type(node, op->ctype);
1761 merge_type(node, ctype);
1762 break;
1765 node->bit_size = target->bit_size;
1766 node->array_size = target->array_size;
1768 expr->ctype = node;
1769 return node;
1773 * Unary post-ops: x++ and x--
1775 static struct symbol *evaluate_postop(struct expression *expr)
1777 struct expression *op = expr->unop;
1778 struct symbol *ctype = op->ctype;
1779 int class = classify_type(ctype, &ctype);
1780 int multiply = 0;
1782 if (!class || class & TYPE_COMPOUND) {
1783 expression_error(expr, "need scalar for ++/--");
1784 return NULL;
1786 if (!lvalue_expression(expr->unop)) {
1787 expression_error(expr, "need lvalue expression for ++/--");
1788 return NULL;
1791 if ((class & TYPE_RESTRICT) && restricted_unop(expr->op, &ctype))
1792 unrestrict(expr, class, &ctype);
1794 if (class & TYPE_NUM) {
1795 multiply = 1;
1796 } else if (class == TYPE_PTR) {
1797 struct symbol *target = examine_pointer_target(ctype);
1798 if (!is_function(target))
1799 multiply = bits_to_bytes(target->bit_size);
1802 if (multiply) {
1803 evaluate_assign_to(op, op->ctype);
1804 expr->op_value = multiply;
1805 expr->ctype = ctype;
1806 return ctype;
1809 expression_error(expr, "bad argument type for ++/--");
1810 return NULL;
1813 static struct symbol *evaluate_sign(struct expression *expr)
1815 struct symbol *ctype = expr->unop->ctype;
1816 int class = classify_type(ctype, &ctype);
1817 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1818 expr->flags = 0;
1819 /* should be an arithmetic type */
1820 if (!(class & TYPE_NUM))
1821 return bad_expr_type(expr);
1822 if (class & TYPE_RESTRICT)
1823 goto Restr;
1824 Normal:
1825 if (!(class & TYPE_FLOAT)) {
1826 ctype = integer_promotion(ctype);
1827 expr->unop = cast_to(expr->unop, ctype);
1828 } else if (expr->op != '~') {
1829 /* no conversions needed */
1830 } else {
1831 return bad_expr_type(expr);
1833 if (expr->op == '+')
1834 *expr = *expr->unop;
1835 expr->ctype = ctype;
1836 return ctype;
1837 Restr:
1838 if (restricted_unop(expr->op, &ctype))
1839 unrestrict(expr, class, &ctype);
1840 goto Normal;
1843 static struct symbol *evaluate_preop(struct expression *expr)
1845 struct symbol *ctype = expr->unop->ctype;
1847 switch (expr->op) {
1848 case '(':
1849 *expr = *expr->unop;
1850 return ctype;
1852 case '+':
1853 case '-':
1854 case '~':
1855 return evaluate_sign(expr);
1857 case '*':
1858 return evaluate_dereference(expr);
1860 case '&':
1861 return evaluate_addressof(expr);
1863 case SPECIAL_INCREMENT:
1864 case SPECIAL_DECREMENT:
1866 * From a type evaluation standpoint the preops are
1867 * the same as the postops
1869 return evaluate_postop(expr);
1871 case '!':
1872 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1873 expr->flags = 0;
1874 if (is_safe_type(ctype))
1875 warning(expr->pos, "testing a 'safe expression'");
1876 if (is_float_type(ctype)) {
1877 struct expression *arg = expr->unop;
1878 expr->type = EXPR_COMPARE;
1879 expr->op = SPECIAL_EQUAL;
1880 expr->left = arg;
1881 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1882 expr->right->ctype = ctype;
1883 expr->right->fvalue = 0;
1884 } else if (is_fouled_type(ctype)) {
1885 warning(expr->pos, "%s degrades to integer",
1886 show_typename(ctype->ctype.base_type));
1888 /* the result is int [6.5.3.3(5)]*/
1889 ctype = &int_ctype;
1890 break;
1892 default:
1893 break;
1895 expr->ctype = ctype;
1896 return ctype;
1899 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1901 struct ptr_list *head = (struct ptr_list *)_list;
1902 struct ptr_list *list = head;
1904 if (!head)
1905 return NULL;
1906 do {
1907 int i;
1908 for (i = 0; i < list->nr; i++) {
1909 struct symbol *sym = (struct symbol *) list->list[i];
1910 if (sym->ident) {
1911 if (sym->ident != ident)
1912 continue;
1913 *offset = sym->offset;
1914 return sym;
1915 } else {
1916 struct symbol *ctype = sym->ctype.base_type;
1917 struct symbol *sub;
1918 if (!ctype)
1919 continue;
1920 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1921 continue;
1922 sub = find_identifier(ident, ctype->symbol_list, offset);
1923 if (!sub)
1924 continue;
1925 *offset += sym->offset;
1926 return sub;
1929 } while ((list = list->next) != head);
1930 return NULL;
1933 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1935 struct expression *add;
1938 * Create a new add-expression
1940 * NOTE! Even if we just add zero, we need a new node
1941 * for the member pointer, since it has a different
1942 * type than the original pointer. We could make that
1943 * be just a cast, but the fact is, a node is a node,
1944 * so we might as well just do the "add zero" here.
1946 add = alloc_expression(expr->pos, EXPR_BINOP);
1947 add->op = '+';
1948 add->left = expr;
1949 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1950 add->right->ctype = &int_ctype;
1951 add->right->value = offset;
1954 * The ctype of the pointer will be lazily evaluated if
1955 * we ever take the address of this member dereference..
1957 add->ctype = &lazy_ptr_ctype;
1958 return add;
1961 /* structure/union dereference */
1962 static struct symbol *evaluate_member_dereference(struct expression *expr)
1964 int offset;
1965 struct symbol *ctype, *member;
1966 struct expression *deref = expr->deref, *add;
1967 struct ident *ident = expr->member;
1968 unsigned int mod;
1969 int address_space;
1971 if (!evaluate_expression(deref))
1972 return NULL;
1973 if (!ident) {
1974 expression_error(expr, "bad member name");
1975 return NULL;
1978 ctype = deref->ctype;
1979 examine_symbol_type(ctype);
1980 address_space = ctype->ctype.as;
1981 mod = ctype->ctype.modifiers;
1982 if (ctype->type == SYM_NODE) {
1983 ctype = ctype->ctype.base_type;
1984 address_space |= ctype->ctype.as;
1985 mod |= ctype->ctype.modifiers;
1987 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1988 expression_error(expr, "expected structure or union");
1989 return NULL;
1991 offset = 0;
1992 member = find_identifier(ident, ctype->symbol_list, &offset);
1993 if (!member) {
1994 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1995 const char *name = "<unnamed>";
1996 int namelen = 9;
1997 if (ctype->ident) {
1998 name = ctype->ident->name;
1999 namelen = ctype->ident->len;
2001 if (ctype->symbol_list)
2002 expression_error(expr, "no member '%s' in %s %.*s",
2003 show_ident(ident), type, namelen, name);
2004 else
2005 expression_error(expr, "using member '%s' in "
2006 "incomplete %s %.*s", show_ident(ident),
2007 type, namelen, name);
2008 return NULL;
2012 * The member needs to take on the address space and modifiers of
2013 * the "parent" type.
2015 member = convert_to_as_mod(member, address_space, mod);
2016 ctype = get_base_type(member);
2018 if (!lvalue_expression(deref)) {
2019 if (deref->type != EXPR_SLICE) {
2020 expr->base = deref;
2021 expr->r_bitpos = 0;
2022 } else {
2023 expr->base = deref->base;
2024 expr->r_bitpos = deref->r_bitpos;
2026 expr->r_bitpos += bytes_to_bits(offset);
2027 expr->type = EXPR_SLICE;
2028 expr->r_nrbits = member->bit_size;
2029 expr->r_bitpos += member->bit_offset;
2030 expr->ctype = member;
2031 return member;
2034 deref = deref->unop;
2035 expr->deref = deref;
2037 add = evaluate_offset(deref, offset);
2038 expr->type = EXPR_PREOP;
2039 expr->op = '*';
2040 expr->unop = add;
2042 expr->ctype = member;
2043 return member;
2046 static int is_promoted(struct expression *expr)
2048 while (1) {
2049 switch (expr->type) {
2050 case EXPR_BINOP:
2051 case EXPR_SELECT:
2052 case EXPR_CONDITIONAL:
2053 return 1;
2054 case EXPR_COMMA:
2055 expr = expr->right;
2056 continue;
2057 case EXPR_PREOP:
2058 switch (expr->op) {
2059 case '(':
2060 expr = expr->unop;
2061 continue;
2062 case '+':
2063 case '-':
2064 case '~':
2065 return 1;
2066 default:
2067 return 0;
2069 default:
2070 return 0;
2076 static struct symbol *evaluate_cast(struct expression *);
2078 static struct symbol *evaluate_type_information(struct expression *expr)
2080 struct symbol *sym = expr->cast_type;
2081 if (!sym) {
2082 sym = evaluate_expression(expr->cast_expression);
2083 if (!sym)
2084 return NULL;
2086 * Expressions of restricted types will possibly get
2087 * promoted - check that here
2089 if (is_restricted_type(sym)) {
2090 if (sym->bit_size < bits_in_int && is_promoted(expr))
2091 sym = &int_ctype;
2092 } else if (is_fouled_type(sym)) {
2093 sym = &int_ctype;
2096 examine_symbol_type(sym);
2097 if (is_bitfield_type(sym)) {
2098 expression_error(expr, "trying to examine bitfield type");
2099 return NULL;
2101 return sym;
2104 static struct symbol *evaluate_sizeof(struct expression *expr)
2106 struct symbol *type;
2107 int size;
2109 type = evaluate_type_information(expr);
2110 if (!type)
2111 return NULL;
2113 size = type->bit_size;
2115 if (size < 0 && is_void_type(type)) {
2116 warning(expr->pos, "expression using sizeof(void)");
2117 size = bits_in_char;
2120 if (size == 1 && is_bool_type(type)) {
2121 if (Wsizeof_bool)
2122 warning(expr->pos, "expression using sizeof bool");
2123 size = bits_in_char;
2126 if (is_function(type->ctype.base_type)) {
2127 warning(expr->pos, "expression using sizeof on a function");
2128 size = bits_in_char;
2131 if ((size < 0) || (size & (bits_in_char - 1)))
2132 expression_error(expr, "cannot size expression");
2134 expr->type = EXPR_VALUE;
2135 expr->value = bits_to_bytes(size);
2136 expr->taint = 0;
2137 expr->ctype = size_t_ctype;
2138 return size_t_ctype;
2141 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
2143 struct symbol *type;
2144 int size;
2146 type = evaluate_type_information(expr);
2147 if (!type)
2148 return NULL;
2150 if (type->type == SYM_NODE)
2151 type = type->ctype.base_type;
2152 if (!type)
2153 return NULL;
2154 switch (type->type) {
2155 case SYM_ARRAY:
2156 break;
2157 case SYM_PTR:
2158 type = get_base_type(type);
2159 if (type)
2160 break;
2161 default:
2162 expression_error(expr, "expected pointer expression");
2163 return NULL;
2165 size = type->bit_size;
2166 if (size & (bits_in_char-1))
2167 size = 0;
2168 expr->type = EXPR_VALUE;
2169 expr->value = bits_to_bytes(size);
2170 expr->taint = 0;
2171 expr->ctype = size_t_ctype;
2172 return size_t_ctype;
2175 static struct symbol *evaluate_alignof(struct expression *expr)
2177 struct symbol *type;
2179 type = evaluate_type_information(expr);
2180 if (!type)
2181 return NULL;
2183 expr->type = EXPR_VALUE;
2184 expr->value = type->ctype.alignment;
2185 expr->taint = 0;
2186 expr->ctype = size_t_ctype;
2187 return size_t_ctype;
2190 static int evaluate_arguments(struct symbol *fn, struct expression_list *head)
2192 struct expression *expr;
2193 struct symbol_list *argument_types = fn->arguments;
2194 struct symbol *argtype;
2195 int i = 1;
2197 PREPARE_PTR_LIST(argument_types, argtype);
2198 FOR_EACH_PTR (head, expr) {
2199 struct expression **p = THIS_ADDRESS(expr);
2200 struct symbol *ctype, *target;
2201 ctype = evaluate_expression(expr);
2203 if (!ctype)
2204 return 0;
2206 target = argtype;
2207 if (!target) {
2208 struct symbol *type;
2209 int class = classify_type(ctype, &type);
2210 if (is_int(class)) {
2211 *p = cast_to(expr, integer_promotion(type));
2212 } else if (class & TYPE_FLOAT) {
2213 unsigned long mod = type->ctype.modifiers;
2214 if (!(mod & (MOD_LONG_ALL)))
2215 *p = cast_to(expr, &double_ctype);
2216 } else if (class & TYPE_PTR) {
2217 if (expr->ctype == &null_ctype)
2218 *p = cast_to(expr, &ptr_ctype);
2219 else
2220 degenerate(expr);
2222 } else if (!target->forced_arg){
2223 static char where[30];
2224 examine_symbol_type(target);
2225 sprintf(where, "argument %d", i);
2226 compatible_argument_type(expr, target, p, where);
2229 i++;
2230 NEXT_PTR_LIST(argtype);
2231 } END_FOR_EACH_PTR(expr);
2232 FINISH_PTR_LIST(argtype);
2233 return 1;
2236 static void convert_index(struct expression *e)
2238 struct expression *child = e->idx_expression;
2239 unsigned from = e->idx_from;
2240 unsigned to = e->idx_to + 1;
2241 e->type = EXPR_POS;
2242 e->init_offset = from * bits_to_bytes(e->ctype->bit_size);
2243 e->init_nr = to - from;
2244 e->init_expr = child;
2247 static void convert_ident(struct expression *e)
2249 struct expression *child = e->ident_expression;
2250 int offset = e->offset;
2252 e->type = EXPR_POS;
2253 e->init_offset = offset;
2254 e->init_nr = 1;
2255 e->init_expr = child;
2258 static void convert_designators(struct expression *e)
2260 while (e) {
2261 if (e->type == EXPR_INDEX)
2262 convert_index(e);
2263 else if (e->type == EXPR_IDENTIFIER)
2264 convert_ident(e);
2265 else
2266 break;
2267 e = e->init_expr;
2271 static void excess(struct expression *e, const char *s)
2273 warning(e->pos, "excessive elements in %s initializer", s);
2277 * implicit designator for the first element
2279 static struct expression *first_subobject(struct symbol *ctype, int class,
2280 struct expression **v)
2282 struct expression *e = *v, *new;
2284 if (ctype->type == SYM_NODE)
2285 ctype = ctype->ctype.base_type;
2287 if (class & TYPE_PTR) { /* array */
2288 if (!ctype->bit_size)
2289 return NULL;
2290 new = alloc_expression(e->pos, EXPR_INDEX);
2291 new->idx_expression = e;
2292 new->ctype = ctype->ctype.base_type;
2293 } else {
2294 struct symbol *field, *p;
2295 PREPARE_PTR_LIST(ctype->symbol_list, p);
2296 while (p && !p->ident && is_bitfield_type(p))
2297 NEXT_PTR_LIST(p);
2298 field = p;
2299 FINISH_PTR_LIST(p);
2300 if (!field)
2301 return NULL;
2302 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2303 new->ident_expression = e;
2304 new->field = new->ctype = field;
2305 new->offset = field->offset;
2307 *v = new;
2308 return new;
2312 * sanity-check explicit designators; return the innermost one or NULL
2313 * in case of error. Assign types.
2315 static struct expression *check_designators(struct expression *e,
2316 struct symbol *ctype)
2318 struct expression *last = NULL;
2319 const char *err;
2320 while (1) {
2321 if (ctype->type == SYM_NODE)
2322 ctype = ctype->ctype.base_type;
2323 if (e->type == EXPR_INDEX) {
2324 struct symbol *type;
2325 if (ctype->type != SYM_ARRAY) {
2326 err = "array index in non-array";
2327 break;
2329 type = ctype->ctype.base_type;
2330 if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2331 unsigned offset = array_element_offset(type->bit_size, e->idx_to);
2332 if (offset >= ctype->bit_size) {
2333 err = "index out of bounds in";
2334 break;
2337 e->ctype = ctype = type;
2338 ctype = type;
2339 last = e;
2340 if (!e->idx_expression) {
2341 err = "invalid";
2342 break;
2344 e = e->idx_expression;
2345 } else if (e->type == EXPR_IDENTIFIER) {
2346 int offset = 0;
2347 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2348 err = "field name not in struct or union";
2349 break;
2351 ctype = find_identifier(e->expr_ident, ctype->symbol_list, &offset);
2352 if (!ctype) {
2353 err = "unknown field name in";
2354 break;
2356 e->offset = offset;
2357 e->field = e->ctype = ctype;
2358 last = e;
2359 if (!e->ident_expression) {
2360 err = "invalid";
2361 break;
2363 e = e->ident_expression;
2364 } else if (e->type == EXPR_POS) {
2365 err = "internal front-end error: EXPR_POS in";
2366 break;
2367 } else
2368 return last;
2370 expression_error(e, "%s initializer", err);
2371 return NULL;
2375 * choose the next subobject to initialize.
2377 * Get designators for next element, switch old ones to EXPR_POS.
2378 * Return the resulting expression or NULL if we'd run out of subobjects.
2379 * The innermost designator is returned in *v. Designators in old
2380 * are assumed to be already sanity-checked.
2382 static struct expression *next_designators(struct expression *old,
2383 struct symbol *ctype,
2384 struct expression *e, struct expression **v)
2386 struct expression *new = NULL;
2388 if (!old)
2389 return NULL;
2390 if (old->type == EXPR_INDEX) {
2391 struct expression *copy;
2392 unsigned n;
2394 copy = next_designators(old->idx_expression,
2395 old->ctype, e, v);
2396 if (!copy) {
2397 n = old->idx_to + 1;
2398 if (array_element_offset(old->ctype->bit_size, n) == ctype->bit_size) {
2399 convert_index(old);
2400 return NULL;
2402 copy = e;
2403 *v = new = alloc_expression(e->pos, EXPR_INDEX);
2404 } else {
2405 n = old->idx_to;
2406 new = alloc_expression(e->pos, EXPR_INDEX);
2409 new->idx_from = new->idx_to = n;
2410 new->idx_expression = copy;
2411 new->ctype = old->ctype;
2412 convert_index(old);
2413 } else if (old->type == EXPR_IDENTIFIER) {
2414 struct expression *copy;
2415 struct symbol *field;
2416 int offset = 0;
2418 copy = next_designators(old->ident_expression,
2419 old->ctype, e, v);
2420 if (!copy) {
2421 field = old->field->next_subobject;
2422 if (!field) {
2423 convert_ident(old);
2424 return NULL;
2426 copy = e;
2427 *v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2429 * We can't necessarily trust "field->offset",
2430 * because the field might be in an anonymous
2431 * union, and the field offset is then the offset
2432 * within that union.
2434 * The "old->offset - old->field->offset"
2435 * would be the offset of such an anonymous
2436 * union.
2438 offset = old->offset - old->field->offset;
2439 } else {
2440 field = old->field;
2441 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2444 new->field = field;
2445 new->expr_ident = field->ident;
2446 new->ident_expression = copy;
2447 new->ctype = field;
2448 new->offset = field->offset + offset;
2449 convert_ident(old);
2451 return new;
2454 static int handle_simple_initializer(struct expression **ep, int nested,
2455 int class, struct symbol *ctype);
2458 * deal with traversing subobjects [6.7.8(17,18,20)]
2460 static void handle_list_initializer(struct expression *expr,
2461 int class, struct symbol *ctype)
2463 struct expression *e, *last = NULL, *top = NULL, *next;
2464 int jumped = 0;
2466 FOR_EACH_PTR(expr->expr_list, e) {
2467 struct expression **v;
2468 struct symbol *type;
2469 int lclass;
2471 if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2472 struct symbol *struct_sym;
2473 if (!top) {
2474 top = e;
2475 last = first_subobject(ctype, class, &top);
2476 } else {
2477 last = next_designators(last, ctype, e, &top);
2479 if (!last) {
2480 excess(e, class & TYPE_PTR ? "array" :
2481 "struct or union");
2482 DELETE_CURRENT_PTR(e);
2483 continue;
2485 struct_sym = ctype->type == SYM_NODE ? ctype->ctype.base_type : ctype;
2486 if (Wdesignated_init && struct_sym->designated_init)
2487 warning(e->pos, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2488 ctype->ident ? "in initializer for " : "",
2489 ctype->ident ? ctype->ident->len : 0,
2490 ctype->ident ? ctype->ident->name : "",
2491 ctype->ident ? ": " : "",
2492 get_type_name(struct_sym->type),
2493 show_ident(struct_sym->ident));
2494 if (jumped) {
2495 warning(e->pos, "advancing past deep designator");
2496 jumped = 0;
2498 REPLACE_CURRENT_PTR(e, last);
2499 } else {
2500 next = check_designators(e, ctype);
2501 if (!next) {
2502 DELETE_CURRENT_PTR(e);
2503 continue;
2505 top = next;
2506 /* deeper than one designator? */
2507 jumped = top != e;
2508 convert_designators(last);
2509 last = e;
2512 found:
2513 lclass = classify_type(top->ctype, &type);
2514 if (top->type == EXPR_INDEX)
2515 v = &top->idx_expression;
2516 else
2517 v = &top->ident_expression;
2519 if (handle_simple_initializer(v, 1, lclass, top->ctype))
2520 continue;
2522 if (!(lclass & TYPE_COMPOUND)) {
2523 warning(e->pos, "bogus scalar initializer");
2524 DELETE_CURRENT_PTR(e);
2525 continue;
2528 next = first_subobject(type, lclass, v);
2529 if (next) {
2530 warning(e->pos, "missing braces around initializer");
2531 top = next;
2532 goto found;
2535 DELETE_CURRENT_PTR(e);
2536 excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2538 } END_FOR_EACH_PTR(e);
2540 convert_designators(last);
2541 expr->ctype = ctype;
2544 static int is_string_literal(struct expression **v)
2546 struct expression *e = *v;
2547 while (e && e->type == EXPR_PREOP && e->op == '(')
2548 e = e->unop;
2549 if (!e || e->type != EXPR_STRING)
2550 return 0;
2551 if (e != *v && Wparen_string)
2552 warning(e->pos,
2553 "array initialized from parenthesized string constant");
2554 *v = e;
2555 return 1;
2559 * We want a normal expression, possibly in one layer of braces. Warn
2560 * if the latter happens inside a list (it's legal, but likely to be
2561 * an effect of screwup). In case of anything not legal, we are definitely
2562 * having an effect of screwup, so just fail and let the caller warn.
2564 static struct expression *handle_scalar(struct expression *e, int nested)
2566 struct expression *v = NULL, *p;
2567 int count = 0;
2569 /* normal case */
2570 if (e->type != EXPR_INITIALIZER)
2571 return e;
2573 FOR_EACH_PTR(e->expr_list, p) {
2574 if (!v)
2575 v = p;
2576 count++;
2577 } END_FOR_EACH_PTR(p);
2578 if (count != 1)
2579 return NULL;
2580 switch(v->type) {
2581 case EXPR_INITIALIZER:
2582 case EXPR_INDEX:
2583 case EXPR_IDENTIFIER:
2584 return NULL;
2585 default:
2586 break;
2588 if (nested)
2589 warning(e->pos, "braces around scalar initializer");
2590 return v;
2594 * deal with the cases that don't care about subobjects:
2595 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2596 * character array <- string literal, possibly in braces [6.7.8(14)]
2597 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2598 * compound type <- initializer list in braces [6.7.8(16)]
2599 * The last one punts to handle_list_initializer() which, in turn will call
2600 * us for individual elements of the list.
2602 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2603 * the lack of support of wide char stuff in general.
2605 * One note: we need to take care not to evaluate a string literal until
2606 * we know that we *will* handle it right here. Otherwise we would screw
2607 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2608 * { "string", ...} - we need to preserve that string literal recognizable
2609 * until we dig into the inner struct.
2611 static int handle_simple_initializer(struct expression **ep, int nested,
2612 int class, struct symbol *ctype)
2614 int is_string = is_string_type(ctype);
2615 struct expression *e = *ep, *p;
2616 struct symbol *type;
2618 if (!e)
2619 return 0;
2621 /* scalar */
2622 if (!(class & TYPE_COMPOUND)) {
2623 e = handle_scalar(e, nested);
2624 if (!e)
2625 return 0;
2626 *ep = e;
2627 if (!evaluate_expression(e))
2628 return 1;
2629 compatible_assignment_types(e, ctype, ep, "initializer");
2630 return 1;
2634 * sublist; either a string, or we dig in; the latter will deal with
2635 * pathologies, so we don't need anything fancy here.
2637 if (e->type == EXPR_INITIALIZER) {
2638 if (is_string) {
2639 struct expression *v = NULL;
2640 int count = 0;
2642 FOR_EACH_PTR(e->expr_list, p) {
2643 if (!v)
2644 v = p;
2645 count++;
2646 } END_FOR_EACH_PTR(p);
2647 if (count == 1 && is_string_literal(&v)) {
2648 *ep = e = v;
2649 goto String;
2652 handle_list_initializer(e, class, ctype);
2653 return 1;
2656 /* string */
2657 if (is_string_literal(&e)) {
2658 /* either we are doing array of char, or we'll have to dig in */
2659 if (is_string) {
2660 *ep = e;
2661 goto String;
2663 return 0;
2665 /* struct or union can be initialized by compatible */
2666 if (class != TYPE_COMPOUND)
2667 return 0;
2668 type = evaluate_expression(e);
2669 if (!type)
2670 return 0;
2671 if (ctype->type == SYM_NODE)
2672 ctype = ctype->ctype.base_type;
2673 if (type->type == SYM_NODE)
2674 type = type->ctype.base_type;
2675 if (ctype == type)
2676 return 1;
2677 return 0;
2679 String:
2680 p = alloc_expression(e->pos, EXPR_STRING);
2681 *p = *e;
2682 type = evaluate_expression(p);
2683 if (ctype->bit_size != -1) {
2684 if (ctype->bit_size + bits_in_char < type->bit_size)
2685 warning(e->pos,
2686 "too long initializer-string for array of char");
2687 else if (Winit_cstring && ctype->bit_size + bits_in_char == type->bit_size) {
2688 warning(e->pos,
2689 "too long initializer-string for array of char(no space for nul char)");
2692 *ep = p;
2693 return 1;
2696 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2698 struct symbol *type;
2699 int class = classify_type(ctype, &type);
2700 if (!handle_simple_initializer(ep, 0, class, ctype))
2701 expression_error(*ep, "invalid initializer");
2704 static struct symbol *cast_to_bool(struct expression *expr)
2706 struct expression *old = expr->cast_expression;
2707 struct expression *zero;
2708 struct symbol *otype;
2709 int oclass = classify_type(degenerate(old), &otype);
2710 struct symbol *ctype;
2712 if (oclass & TYPE_COMPOUND)
2713 return NULL;
2715 zero = alloc_const_expression(expr->pos, 0);
2716 expr->op = SPECIAL_NOTEQUAL;
2717 ctype = usual_conversions(expr->op, old, zero,
2718 oclass, TYPE_NUM, otype, zero->ctype);
2719 expr->type = EXPR_COMPARE;
2720 expr->left = cast_to(old, ctype);
2721 expr->right = cast_to(zero, ctype);
2723 return expr->ctype;
2726 static struct symbol *evaluate_cast(struct expression *expr)
2728 struct expression *target = expr->cast_expression;
2729 struct symbol *ctype;
2730 struct symbol *t1, *t2;
2731 int class1, class2;
2732 int as1 = 0, as2 = 0;
2734 if (!target)
2735 return NULL;
2738 * Special case: a cast can be followed by an
2739 * initializer, in which case we need to pass
2740 * the type value down to that initializer rather
2741 * than trying to evaluate it as an expression
2743 * A more complex case is when the initializer is
2744 * dereferenced as part of a post-fix expression.
2745 * We need to produce an expression that can be dereferenced.
2747 if (target->type == EXPR_INITIALIZER) {
2748 struct symbol *sym = expr->cast_type;
2749 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2751 sym->initializer = target;
2752 evaluate_symbol(sym);
2754 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2755 addr->symbol = sym;
2757 expr->type = EXPR_PREOP;
2758 expr->op = '*';
2759 expr->unop = addr;
2760 expr->ctype = sym;
2762 return sym;
2765 ctype = examine_symbol_type(expr->cast_type);
2766 expr->ctype = ctype;
2767 expr->cast_type = ctype;
2769 evaluate_expression(target);
2770 degenerate(target);
2772 class1 = classify_type(ctype, &t1);
2774 /* cast to non-integer type -> not an integer constant expression */
2775 if (!is_int(class1))
2776 expr->flags = 0;
2777 /* if argument turns out to be not an integer constant expression *and*
2778 it was not a floating literal to start with -> too bad */
2779 else if (expr->flags == Int_const_expr &&
2780 !(target->flags & Int_const_expr))
2781 expr->flags = 0;
2783 * You can always throw a value away by casting to
2784 * "void" - that's an implicit "force". Note that
2785 * the same is _not_ true of "void *".
2787 if (t1 == &void_ctype)
2788 goto out;
2790 if (class1 & (TYPE_COMPOUND | TYPE_FN))
2791 warning(expr->pos, "cast to non-scalar");
2793 t2 = target->ctype;
2794 if (!t2) {
2795 expression_error(expr, "cast from unknown type");
2796 goto out;
2798 class2 = classify_type(t2, &t2);
2800 if (class2 & TYPE_COMPOUND)
2801 warning(expr->pos, "cast from non-scalar");
2803 if (expr->type == EXPR_FORCE_CAST)
2804 goto out;
2806 /* allowed cast unfouls */
2807 if (class2 & TYPE_FOULED)
2808 t2 = unfoul(t2);
2810 if (t1 != t2) {
2811 if ((class1 & TYPE_RESTRICT) && restricted_value(target, t1))
2812 warning(expr->pos, "cast to %s",
2813 show_typename(t1));
2814 if (class2 & TYPE_RESTRICT)
2815 warning(expr->pos, "cast from %s",
2816 show_typename(t2));
2819 if (t1 == &ulong_ctype)
2820 as1 = -1;
2821 else if (class1 == TYPE_PTR) {
2822 examine_pointer_target(t1);
2823 as1 = t1->ctype.as;
2826 if (t2 == &ulong_ctype)
2827 as2 = -1;
2828 else if (class2 == TYPE_PTR) {
2829 examine_pointer_target(t2);
2830 as2 = t2->ctype.as;
2833 if (!as1 && as2 > 0)
2834 warning(expr->pos, "cast removes address space of expression");
2835 if (as1 > 0 && as2 > 0 && as1 != as2)
2836 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2837 if (as1 > 0 && !as2 &&
2838 !is_null_pointer_constant(target) && Wcast_to_as)
2839 warning(expr->pos,
2840 "cast adds address space to expression (<asn:%d>)", as1);
2842 if (!(t1->ctype.modifiers & MOD_PTRINHERIT) && class1 == TYPE_PTR &&
2843 !as1 && (target->flags & Int_const_expr)) {
2844 if (t1->ctype.base_type == &void_ctype) {
2845 if (is_zero_constant(target)) {
2846 /* NULL */
2847 expr->type = EXPR_VALUE;
2848 expr->ctype = &null_ctype;
2849 expr->value = 0;
2850 return ctype;
2855 if (t1 == &bool_ctype)
2856 cast_to_bool(expr);
2858 out:
2859 return ctype;
2863 * Evaluate a call expression with a symbol. This
2864 * should expand inline functions, and evaluate
2865 * builtins.
2867 static int evaluate_symbol_call(struct expression *expr)
2869 struct expression *fn = expr->fn;
2870 struct symbol *ctype = fn->ctype;
2872 if (fn->type != EXPR_PREOP)
2873 return 0;
2875 if (ctype->op && ctype->op->evaluate)
2876 return ctype->op->evaluate(expr);
2878 if (ctype->ctype.modifiers & MOD_INLINE) {
2879 int ret;
2880 struct symbol *curr = current_fn;
2882 if (ctype->definition)
2883 ctype = ctype->definition;
2885 current_fn = ctype->ctype.base_type;
2887 ret = inline_function(expr, ctype);
2889 /* restore the old function */
2890 current_fn = curr;
2891 return ret;
2894 return 0;
2897 static struct symbol *evaluate_call(struct expression *expr)
2899 int args, fnargs;
2900 struct symbol *ctype, *sym;
2901 struct expression *fn = expr->fn;
2902 struct expression_list *arglist = expr->args;
2904 if (!evaluate_expression(fn))
2905 return NULL;
2906 sym = ctype = fn->ctype;
2907 if (ctype->type == SYM_NODE)
2908 ctype = ctype->ctype.base_type;
2909 if (ctype->type == SYM_PTR)
2910 ctype = get_base_type(ctype);
2912 if (ctype->type != SYM_FN) {
2913 struct expression *arg;
2914 expression_error(expr, "not a function %s",
2915 show_ident(sym->ident));
2916 /* do typechecking in arguments */
2917 FOR_EACH_PTR (arglist, arg) {
2918 evaluate_expression(arg);
2919 } END_FOR_EACH_PTR(arg);
2920 return NULL;
2923 examine_fn_arguments(ctype);
2924 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
2925 sym->op && sym->op->args) {
2926 if (!sym->op->args(expr))
2927 return NULL;
2928 } else {
2929 if (!evaluate_arguments(ctype, arglist))
2930 return NULL;
2931 args = expression_list_size(expr->args);
2932 fnargs = symbol_list_size(ctype->arguments);
2933 if (args < fnargs)
2934 expression_error(expr,
2935 "not enough arguments for function %s",
2936 show_ident(sym->ident));
2937 if (args > fnargs && !ctype->variadic)
2938 expression_error(expr,
2939 "too many arguments for function %s",
2940 show_ident(sym->ident));
2942 if (sym->type == SYM_NODE) {
2943 if (evaluate_symbol_call(expr))
2944 return expr->ctype;
2946 expr->ctype = ctype->ctype.base_type;
2947 return expr->ctype;
2950 static struct symbol *evaluate_offsetof(struct expression *expr)
2952 struct expression *e = expr->down;
2953 struct symbol *ctype = expr->in;
2954 int class;
2956 if (expr->op == '.') {
2957 struct symbol *field;
2958 int offset = 0;
2959 if (!ctype) {
2960 expression_error(expr, "expected structure or union");
2961 return NULL;
2963 examine_symbol_type(ctype);
2964 class = classify_type(ctype, &ctype);
2965 if (class != TYPE_COMPOUND) {
2966 expression_error(expr, "expected structure or union");
2967 return NULL;
2970 field = find_identifier(expr->ident, ctype->symbol_list, &offset);
2971 if (!field) {
2972 expression_error(expr, "unknown member");
2973 return NULL;
2975 ctype = field;
2976 expr->type = EXPR_VALUE;
2977 expr->flags = Int_const_expr;
2978 expr->value = offset;
2979 expr->taint = 0;
2980 expr->ctype = size_t_ctype;
2981 } else {
2982 if (!ctype) {
2983 expression_error(expr, "expected structure or union");
2984 return NULL;
2986 examine_symbol_type(ctype);
2987 class = classify_type(ctype, &ctype);
2988 if (class != (TYPE_COMPOUND | TYPE_PTR)) {
2989 expression_error(expr, "expected array");
2990 return NULL;
2992 ctype = ctype->ctype.base_type;
2993 if (!expr->index) {
2994 expr->type = EXPR_VALUE;
2995 expr->flags = Int_const_expr;
2996 expr->value = 0;
2997 expr->taint = 0;
2998 expr->ctype = size_t_ctype;
2999 } else {
3000 struct expression *idx = expr->index, *m;
3001 struct symbol *i_type = evaluate_expression(idx);
3002 int i_class = classify_type(i_type, &i_type);
3003 if (!is_int(i_class)) {
3004 expression_error(expr, "non-integer index");
3005 return NULL;
3007 unrestrict(idx, i_class, &i_type);
3008 idx = cast_to(idx, size_t_ctype);
3009 m = alloc_const_expression(expr->pos,
3010 bits_to_bytes(ctype->bit_size));
3011 m->ctype = size_t_ctype;
3012 m->flags = Int_const_expr;
3013 expr->type = EXPR_BINOP;
3014 expr->left = idx;
3015 expr->right = m;
3016 expr->op = '*';
3017 expr->ctype = size_t_ctype;
3018 expr->flags = m->flags & idx->flags & Int_const_expr;
3021 if (e) {
3022 struct expression *copy = __alloc_expression(0);
3023 *copy = *expr;
3024 if (e->type == EXPR_OFFSETOF)
3025 e->in = ctype;
3026 if (!evaluate_expression(e))
3027 return NULL;
3028 expr->type = EXPR_BINOP;
3029 expr->flags = e->flags & copy->flags & Int_const_expr;
3030 expr->op = '+';
3031 expr->ctype = size_t_ctype;
3032 expr->left = copy;
3033 expr->right = e;
3035 return size_t_ctype;
3038 struct symbol *evaluate_expression(struct expression *expr)
3040 if (!expr)
3041 return NULL;
3042 if (expr->ctype)
3043 return expr->ctype;
3045 switch (expr->type) {
3046 case EXPR_VALUE:
3047 case EXPR_FVALUE:
3048 expression_error(expr, "value expression without a type");
3049 return NULL;
3050 case EXPR_STRING:
3051 return evaluate_string(expr);
3052 case EXPR_SYMBOL:
3053 return evaluate_symbol_expression(expr);
3054 case EXPR_BINOP:
3055 if (!evaluate_expression(expr->left))
3056 return NULL;
3057 if (!evaluate_expression(expr->right))
3058 return NULL;
3059 return evaluate_binop(expr);
3060 case EXPR_LOGICAL:
3061 return evaluate_logical(expr);
3062 case EXPR_COMMA:
3063 evaluate_expression(expr->left);
3064 if (!evaluate_expression(expr->right))
3065 return NULL;
3066 return evaluate_comma(expr);
3067 case EXPR_COMPARE:
3068 if (!evaluate_expression(expr->left))
3069 return NULL;
3070 if (!evaluate_expression(expr->right))
3071 return NULL;
3072 return evaluate_compare(expr);
3073 case EXPR_ASSIGNMENT:
3074 if (!evaluate_expression(expr->left))
3075 return NULL;
3076 if (!evaluate_expression(expr->right))
3077 return NULL;
3078 return evaluate_assignment(expr);
3079 case EXPR_PREOP:
3080 if (!evaluate_expression(expr->unop))
3081 return NULL;
3082 return evaluate_preop(expr);
3083 case EXPR_POSTOP:
3084 if (!evaluate_expression(expr->unop))
3085 return NULL;
3086 return evaluate_postop(expr);
3087 case EXPR_CAST:
3088 case EXPR_FORCE_CAST:
3089 case EXPR_IMPLIED_CAST:
3090 return evaluate_cast(expr);
3091 case EXPR_SIZEOF:
3092 return evaluate_sizeof(expr);
3093 case EXPR_PTRSIZEOF:
3094 return evaluate_ptrsizeof(expr);
3095 case EXPR_ALIGNOF:
3096 return evaluate_alignof(expr);
3097 case EXPR_DEREF:
3098 return evaluate_member_dereference(expr);
3099 case EXPR_CALL:
3100 return evaluate_call(expr);
3101 case EXPR_SELECT:
3102 case EXPR_CONDITIONAL:
3103 return evaluate_conditional_expression(expr);
3104 case EXPR_STATEMENT:
3105 expr->ctype = evaluate_statement(expr->statement);
3106 return expr->ctype;
3108 case EXPR_LABEL:
3109 expr->ctype = &ptr_ctype;
3110 return &ptr_ctype;
3112 case EXPR_TYPE:
3113 /* Evaluate the type of the symbol .. */
3114 evaluate_symbol(expr->symbol);
3115 /* .. but the type of the _expression_ is a "type" */
3116 expr->ctype = &type_ctype;
3117 return &type_ctype;
3119 case EXPR_OFFSETOF:
3120 return evaluate_offsetof(expr);
3122 /* These can not exist as stand-alone expressions */
3123 case EXPR_INITIALIZER:
3124 case EXPR_IDENTIFIER:
3125 case EXPR_INDEX:
3126 case EXPR_POS:
3127 expression_error(expr, "internal front-end error: initializer in expression");
3128 return NULL;
3129 case EXPR_SLICE:
3130 expression_error(expr, "internal front-end error: SLICE re-evaluated");
3131 return NULL;
3133 return NULL;
3136 static void check_duplicates(struct symbol *sym)
3138 int declared = 0;
3139 struct symbol *next = sym;
3140 int initialized = sym->initializer != NULL;
3142 while ((next = next->same_symbol) != NULL) {
3143 const char *typediff;
3144 evaluate_symbol(next);
3145 if (initialized && next->initializer) {
3146 sparse_error(sym->pos, "symbol '%s' has multiple initializers (originally initialized at %s:%d)",
3147 show_ident(sym->ident),
3148 stream_name(next->pos.stream), next->pos.line);
3149 /* Only warn once */
3150 initialized = 0;
3152 declared++;
3153 typediff = type_difference(&sym->ctype, &next->ctype, 0, 0);
3154 if (typediff) {
3155 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
3156 show_ident(sym->ident),
3157 stream_name(next->pos.stream), next->pos.line, typediff);
3158 return;
3161 if (!declared) {
3162 unsigned long mod = sym->ctype.modifiers;
3163 if (mod & (MOD_STATIC | MOD_REGISTER))
3164 return;
3165 if (!(mod & MOD_TOPLEVEL))
3166 return;
3167 if (!Wdecl)
3168 return;
3169 if (sym->ident == &main_ident)
3170 return;
3171 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
3175 static struct symbol *evaluate_symbol(struct symbol *sym)
3177 struct symbol *base_type;
3179 if (!sym)
3180 return sym;
3181 if (sym->evaluated)
3182 return sym;
3183 sym->evaluated = 1;
3185 sym = examine_symbol_type(sym);
3186 base_type = get_base_type(sym);
3187 if (!base_type)
3188 return NULL;
3190 /* Evaluate the initializers */
3191 if (sym->initializer)
3192 evaluate_initializer(sym, &sym->initializer);
3194 /* And finally, evaluate the body of the symbol too */
3195 if (base_type->type == SYM_FN) {
3196 struct symbol *curr = current_fn;
3198 if (sym->definition && sym->definition != sym)
3199 return evaluate_symbol(sym->definition);
3201 current_fn = base_type;
3203 examine_fn_arguments(base_type);
3204 if (!base_type->stmt && base_type->inline_stmt)
3205 uninline(sym);
3206 if (base_type->stmt)
3207 evaluate_statement(base_type->stmt);
3209 current_fn = curr;
3212 return base_type;
3215 void evaluate_symbol_list(struct symbol_list *list)
3217 struct symbol *sym;
3219 FOR_EACH_PTR(list, sym) {
3220 evaluate_symbol(sym);
3221 check_duplicates(sym);
3222 } END_FOR_EACH_PTR(sym);
3225 static struct symbol *evaluate_return_expression(struct statement *stmt)
3227 struct expression *expr = stmt->expression;
3228 struct symbol *fntype;
3230 evaluate_expression(expr);
3231 fntype = current_fn->ctype.base_type;
3232 if (!fntype || fntype == &void_ctype) {
3233 if (expr && expr->ctype != &void_ctype)
3234 expression_error(expr, "return expression in %s function", fntype?"void":"typeless");
3235 if (expr && Wreturn_void)
3236 warning(stmt->pos, "returning void-valued expression");
3237 return NULL;
3240 if (!expr) {
3241 sparse_error(stmt->pos, "return with no return value");
3242 return NULL;
3244 if (!expr->ctype)
3245 return NULL;
3246 compatible_assignment_types(expr, fntype, &stmt->expression, "return expression");
3247 return NULL;
3250 static void evaluate_if_statement(struct statement *stmt)
3252 if (!stmt->if_conditional)
3253 return;
3255 evaluate_conditional(stmt->if_conditional, 0);
3256 evaluate_statement(stmt->if_true);
3257 evaluate_statement(stmt->if_false);
3260 static void evaluate_iterator(struct statement *stmt)
3262 evaluate_symbol_list(stmt->iterator_syms);
3263 evaluate_conditional(stmt->iterator_pre_condition, 1);
3264 evaluate_conditional(stmt->iterator_post_condition,1);
3265 evaluate_statement(stmt->iterator_pre_statement);
3266 evaluate_statement(stmt->iterator_statement);
3267 evaluate_statement(stmt->iterator_post_statement);
3270 static void verify_output_constraint(struct expression *expr, const char *constraint)
3272 switch (*constraint) {
3273 case '=': /* Assignment */
3274 case '+': /* Update */
3275 break;
3276 default:
3277 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
3281 static void verify_input_constraint(struct expression *expr, const char *constraint)
3283 switch (*constraint) {
3284 case '=': /* Assignment */
3285 case '+': /* Update */
3286 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
3290 static void evaluate_asm_statement(struct statement *stmt)
3292 struct expression *expr;
3293 struct symbol *sym;
3294 int state;
3296 expr = stmt->asm_string;
3297 if (!expr || expr->type != EXPR_STRING) {
3298 sparse_error(stmt->pos, "need constant string for inline asm");
3299 return;
3302 state = 0;
3303 FOR_EACH_PTR(stmt->asm_outputs, expr) {
3304 switch (state) {
3305 case 0: /* Identifier */
3306 state = 1;
3307 continue;
3309 case 1: /* Constraint */
3310 state = 2;
3311 if (!expr || expr->type != EXPR_STRING) {
3312 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
3313 *THIS_ADDRESS(expr) = NULL;
3314 continue;
3316 verify_output_constraint(expr, expr->string->data);
3317 continue;
3319 case 2: /* Expression */
3320 state = 0;
3321 if (!evaluate_expression(expr))
3322 return;
3323 if (!lvalue_expression(expr))
3324 warning(expr->pos, "asm output is not an lvalue");
3325 evaluate_assign_to(expr, expr->ctype);
3326 continue;
3328 } END_FOR_EACH_PTR(expr);
3330 state = 0;
3331 FOR_EACH_PTR(stmt->asm_inputs, expr) {
3332 switch (state) {
3333 case 0: /* Identifier */
3334 state = 1;
3335 continue;
3337 case 1: /* Constraint */
3338 state = 2;
3339 if (!expr || expr->type != EXPR_STRING) {
3340 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
3341 *THIS_ADDRESS(expr) = NULL;
3342 continue;
3344 verify_input_constraint(expr, expr->string->data);
3345 continue;
3347 case 2: /* Expression */
3348 state = 0;
3349 if (!evaluate_expression(expr))
3350 return;
3351 continue;
3353 } END_FOR_EACH_PTR(expr);
3355 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
3356 if (!expr) {
3357 sparse_error(stmt->pos, "bad asm clobbers");
3358 return;
3360 if (expr->type == EXPR_STRING)
3361 continue;
3362 expression_error(expr, "asm clobber is not a string");
3363 } END_FOR_EACH_PTR(expr);
3365 FOR_EACH_PTR(stmt->asm_labels, sym) {
3366 if (!sym || sym->type != SYM_LABEL) {
3367 sparse_error(stmt->pos, "bad asm label");
3368 return;
3370 } END_FOR_EACH_PTR(sym);
3373 static void evaluate_case_statement(struct statement *stmt)
3375 evaluate_expression(stmt->case_expression);
3376 evaluate_expression(stmt->case_to);
3377 evaluate_statement(stmt->case_statement);
3380 static void check_case_type(struct expression *switch_expr,
3381 struct expression *case_expr,
3382 struct expression **enumcase)
3384 struct symbol *switch_type, *case_type;
3385 int sclass, cclass;
3387 if (!case_expr)
3388 return;
3390 switch_type = switch_expr->ctype;
3391 case_type = evaluate_expression(case_expr);
3393 if (!switch_type || !case_type)
3394 goto Bad;
3395 if (enumcase) {
3396 if (*enumcase)
3397 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
3398 else if (is_enum_type(case_type))
3399 *enumcase = case_expr;
3402 sclass = classify_type(switch_type, &switch_type);
3403 cclass = classify_type(case_type, &case_type);
3405 /* both should be arithmetic */
3406 if (!(sclass & cclass & TYPE_NUM))
3407 goto Bad;
3409 /* neither should be floating */
3410 if ((sclass | cclass) & TYPE_FLOAT)
3411 goto Bad;
3413 /* if neither is restricted, we are OK */
3414 if (!((sclass | cclass) & TYPE_RESTRICT))
3415 return;
3417 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
3418 cclass, sclass, case_type, switch_type)) {
3419 unrestrict(case_expr, cclass, &case_type);
3420 unrestrict(switch_expr, sclass, &switch_type);
3422 return;
3424 Bad:
3425 expression_error(case_expr, "incompatible types for 'case' statement");
3428 static void evaluate_switch_statement(struct statement *stmt)
3430 struct symbol *sym;
3431 struct expression *enumcase = NULL;
3432 struct expression **enumcase_holder = &enumcase;
3433 struct expression *sel = stmt->switch_expression;
3435 evaluate_expression(sel);
3436 evaluate_statement(stmt->switch_statement);
3437 if (!sel)
3438 return;
3439 if (sel->ctype && is_enum_type(sel->ctype))
3440 enumcase_holder = NULL; /* Only check cases against switch */
3442 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
3443 struct statement *case_stmt = sym->stmt;
3444 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
3445 check_case_type(sel, case_stmt->case_to, enumcase_holder);
3446 } END_FOR_EACH_PTR(sym);
3449 static void evaluate_goto_statement(struct statement *stmt)
3451 struct symbol *label = stmt->goto_label;
3453 if (label && !label->stmt && !lookup_keyword(label->ident, NS_KEYWORD))
3454 sparse_error(stmt->pos, "label '%s' was not declared", show_ident(label->ident));
3456 evaluate_expression(stmt->goto_expression);
3459 struct symbol *evaluate_statement(struct statement *stmt)
3461 if (!stmt)
3462 return NULL;
3464 switch (stmt->type) {
3465 case STMT_DECLARATION: {
3466 struct symbol *s;
3467 FOR_EACH_PTR(stmt->declaration, s) {
3468 evaluate_symbol(s);
3469 } END_FOR_EACH_PTR(s);
3470 return NULL;
3473 case STMT_RETURN:
3474 return evaluate_return_expression(stmt);
3476 case STMT_EXPRESSION:
3477 if (!evaluate_expression(stmt->expression))
3478 return NULL;
3479 if (stmt->expression->ctype == &null_ctype)
3480 stmt->expression = cast_to(stmt->expression, &ptr_ctype);
3481 return degenerate(stmt->expression);
3483 case STMT_COMPOUND: {
3484 struct statement *s;
3485 struct symbol *type = NULL;
3487 /* Evaluate the return symbol in the compound statement */
3488 evaluate_symbol(stmt->ret);
3491 * Then, evaluate each statement, making the type of the
3492 * compound statement be the type of the last statement
3494 type = evaluate_statement(stmt->args);
3495 FOR_EACH_PTR(stmt->stmts, s) {
3496 type = evaluate_statement(s);
3497 } END_FOR_EACH_PTR(s);
3498 if (!type)
3499 type = &void_ctype;
3500 return type;
3502 case STMT_IF:
3503 evaluate_if_statement(stmt);
3504 return NULL;
3505 case STMT_ITERATOR:
3506 evaluate_iterator(stmt);
3507 return NULL;
3508 case STMT_SWITCH:
3509 evaluate_switch_statement(stmt);
3510 return NULL;
3511 case STMT_CASE:
3512 evaluate_case_statement(stmt);
3513 return NULL;
3514 case STMT_LABEL:
3515 return evaluate_statement(stmt->label_statement);
3516 case STMT_GOTO:
3517 evaluate_goto_statement(stmt);
3518 return NULL;
3519 case STMT_NONE:
3520 break;
3521 case STMT_ASM:
3522 evaluate_asm_statement(stmt);
3523 return NULL;
3524 case STMT_CONTEXT:
3525 evaluate_expression(stmt->expression);
3526 return NULL;
3527 case STMT_RANGE:
3528 evaluate_expression(stmt->range_expression);
3529 evaluate_expression(stmt->range_low);
3530 evaluate_expression(stmt->range_high);
3531 return NULL;
3533 return NULL;