fix size of loaded bitfields
[smatch.git] / evaluate.c
blob47eeaef2eeea3d8de6b63b55d50ea4e101c7d573
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_scalar_type(ctype)) {
873 sparse_error(expr->pos, "incorrect type in conditional");
874 info(expr->pos, " got %s", show_typename(ctype));
875 ctype = NULL;
879 return ctype;
882 static struct symbol *evaluate_logical(struct expression *expr)
884 if (!evaluate_conditional(expr->left, 0))
885 return NULL;
886 if (!evaluate_conditional(expr->right, 0))
887 return NULL;
889 /* the result is int [6.5.13(3), 6.5.14(3)] */
890 expr->ctype = &int_ctype;
891 if (expr->flags) {
892 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
893 expr->flags = 0;
895 return &int_ctype;
898 static struct symbol *evaluate_binop(struct expression *expr)
900 struct symbol *ltype, *rtype, *ctype;
901 int lclass = classify_type(expr->left->ctype, &ltype);
902 int rclass = classify_type(expr->right->ctype, &rtype);
903 int op = expr->op;
905 if (expr->flags) {
906 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
907 expr->flags = 0;
910 /* number op number */
911 if (lclass & rclass & TYPE_NUM) {
912 if ((lclass | rclass) & TYPE_FLOAT) {
913 switch (op) {
914 case '+': case '-': case '*': case '/':
915 break;
916 default:
917 return bad_expr_type(expr);
921 if (op == SPECIAL_LEFTSHIFT || op == SPECIAL_RIGHTSHIFT) {
922 // shifts do integer promotions, but that's it.
923 unrestrict(expr->left, lclass, &ltype);
924 unrestrict(expr->right, rclass, &rtype);
925 ctype = ltype = integer_promotion(ltype);
926 rtype = integer_promotion(rtype);
927 } else {
928 // The rest do usual conversions
929 const unsigned left_not = expr->left->type == EXPR_PREOP
930 && expr->left->op == '!';
931 const unsigned right_not = expr->right->type == EXPR_PREOP
932 && expr->right->op == '!';
933 if ((op == '&' || op == '|') && (left_not || right_not))
934 warning(expr->pos, "dubious: %sx %c %sy",
935 left_not ? "!" : "",
937 right_not ? "!" : "");
939 ltype = usual_conversions(op, expr->left, expr->right,
940 lclass, rclass, ltype, rtype);
941 ctype = rtype = ltype;
944 expr->left = cast_to(expr->left, ltype);
945 expr->right = cast_to(expr->right, rtype);
946 expr->ctype = ctype;
947 return ctype;
950 /* pointer (+|-) integer */
951 if (lclass & TYPE_PTR && is_int(rclass) && (op == '+' || op == '-')) {
952 unrestrict(expr->right, rclass, &rtype);
953 return evaluate_ptr_add(expr, rtype);
956 /* integer + pointer */
957 if (rclass & TYPE_PTR && is_int(lclass) && op == '+') {
958 struct expression *index = expr->left;
959 unrestrict(index, lclass, &ltype);
960 expr->left = expr->right;
961 expr->right = index;
962 return evaluate_ptr_add(expr, ltype);
965 /* pointer - pointer */
966 if (lclass & rclass & TYPE_PTR && expr->op == '-')
967 return evaluate_ptr_sub(expr);
969 return bad_expr_type(expr);
972 static struct symbol *evaluate_comma(struct expression *expr)
974 expr->ctype = degenerate(expr->right);
975 if (expr->ctype == &null_ctype)
976 expr->ctype = &ptr_ctype;
977 expr->flags &= expr->left->flags & expr->right->flags;
978 return expr->ctype;
981 static int modify_for_unsigned(int op)
983 if (op == '<')
984 op = SPECIAL_UNSIGNED_LT;
985 else if (op == '>')
986 op = SPECIAL_UNSIGNED_GT;
987 else if (op == SPECIAL_LTE)
988 op = SPECIAL_UNSIGNED_LTE;
989 else if (op == SPECIAL_GTE)
990 op = SPECIAL_UNSIGNED_GTE;
991 return op;
994 static inline int is_null_pointer_constant(struct expression *e)
996 if (e->ctype == &null_ctype)
997 return 1;
998 if (!(e->flags & Int_const_expr))
999 return 0;
1000 return is_zero_constant(e) ? 2 : 0;
1003 static struct symbol *evaluate_compare(struct expression *expr)
1005 struct expression *left = expr->left, *right = expr->right;
1006 struct symbol *ltype, *rtype, *lbase, *rbase;
1007 int lclass = classify_type(degenerate(left), &ltype);
1008 int rclass = classify_type(degenerate(right), &rtype);
1009 struct symbol *ctype;
1010 const char *typediff;
1012 if (expr->flags) {
1013 if (!(expr->left->flags & expr->right->flags & Int_const_expr))
1014 expr->flags = 0;
1017 /* Type types? */
1018 if (is_type_type(ltype) && is_type_type(rtype))
1019 goto OK;
1021 if (is_safe_type(left->ctype) || is_safe_type(right->ctype))
1022 warning(expr->pos, "testing a 'safe expression'");
1024 /* number on number */
1025 if (lclass & rclass & TYPE_NUM) {
1026 ctype = usual_conversions(expr->op, expr->left, expr->right,
1027 lclass, rclass, ltype, rtype);
1028 expr->left = cast_to(expr->left, ctype);
1029 expr->right = cast_to(expr->right, ctype);
1030 if (ctype->ctype.modifiers & MOD_UNSIGNED)
1031 expr->op = modify_for_unsigned(expr->op);
1032 goto OK;
1035 /* at least one must be a pointer */
1036 if (!((lclass | rclass) & TYPE_PTR))
1037 return bad_expr_type(expr);
1039 /* equality comparisons can be with null pointer constants */
1040 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1041 int is_null1 = is_null_pointer_constant(left);
1042 int is_null2 = is_null_pointer_constant(right);
1043 if (is_null1 == 2)
1044 bad_null(left);
1045 if (is_null2 == 2)
1046 bad_null(right);
1047 if (is_null1 && is_null2) {
1048 int positive = expr->op == SPECIAL_EQUAL;
1049 expr->type = EXPR_VALUE;
1050 expr->value = positive;
1051 goto OK;
1053 if (is_null1 && (rclass & TYPE_PTR)) {
1054 left = cast_to(left, rtype);
1055 goto OK;
1057 if (is_null2 && (lclass & TYPE_PTR)) {
1058 right = cast_to(right, ltype);
1059 goto OK;
1062 /* both should be pointers */
1063 if (!(lclass & rclass & TYPE_PTR))
1064 return bad_expr_type(expr);
1065 expr->op = modify_for_unsigned(expr->op);
1067 lbase = examine_pointer_target(ltype);
1068 rbase = examine_pointer_target(rtype);
1070 /* they also have special treatment for pointers to void */
1071 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1072 if (ltype->ctype.as == rtype->ctype.as) {
1073 if (lbase == &void_ctype) {
1074 right = cast_to(right, ltype);
1075 goto OK;
1077 if (rbase == &void_ctype) {
1078 left = cast_to(left, rtype);
1079 goto OK;
1084 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1085 target_qualifiers(rtype),
1086 target_qualifiers(ltype));
1087 if (!typediff)
1088 goto OK;
1090 expression_error(expr, "incompatible types in comparison expression (%s)", typediff);
1091 return NULL;
1094 /* the result is int [6.5.8(6), 6.5.9(3)]*/
1095 expr->ctype = &int_ctype;
1096 return &int_ctype;
1100 * NOTE! The degenerate case of "x ? : y", where we don't
1101 * have a true case, this will possibly promote "x" to the
1102 * same type as "y", and thus _change_ the conditional
1103 * test in the expression. But since promotion is "safe"
1104 * for testing, that's OK.
1106 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1108 struct expression **true;
1109 struct symbol *ctype, *ltype, *rtype, *lbase, *rbase;
1110 int lclass, rclass;
1111 const char * typediff;
1112 int qual;
1114 if (!evaluate_conditional(expr->conditional, 0))
1115 return NULL;
1116 if (!evaluate_expression(expr->cond_false))
1117 return NULL;
1119 ctype = degenerate(expr->conditional);
1120 rtype = degenerate(expr->cond_false);
1122 true = &expr->conditional;
1123 ltype = ctype;
1124 if (expr->cond_true) {
1125 if (!evaluate_expression(expr->cond_true))
1126 return NULL;
1127 ltype = degenerate(expr->cond_true);
1128 true = &expr->cond_true;
1131 if (expr->flags) {
1132 int flags = expr->conditional->flags & Int_const_expr;
1133 flags &= (*true)->flags & expr->cond_false->flags;
1134 if (!flags)
1135 expr->flags = 0;
1138 lclass = classify_type(ltype, &ltype);
1139 rclass = classify_type(rtype, &rtype);
1140 if (lclass & rclass & TYPE_NUM) {
1141 ctype = usual_conversions('?', *true, expr->cond_false,
1142 lclass, rclass, ltype, rtype);
1143 *true = cast_to(*true, ctype);
1144 expr->cond_false = cast_to(expr->cond_false, ctype);
1145 goto out;
1148 if ((lclass | rclass) & TYPE_PTR) {
1149 int is_null1 = is_null_pointer_constant(*true);
1150 int is_null2 = is_null_pointer_constant(expr->cond_false);
1152 if (is_null1 && is_null2) {
1153 *true = cast_to(*true, &ptr_ctype);
1154 expr->cond_false = cast_to(expr->cond_false, &ptr_ctype);
1155 ctype = &ptr_ctype;
1156 goto out;
1158 if (is_null1 && (rclass & TYPE_PTR)) {
1159 if (is_null1 == 2)
1160 bad_null(*true);
1161 *true = cast_to(*true, rtype);
1162 ctype = rtype;
1163 goto out;
1165 if (is_null2 && (lclass & TYPE_PTR)) {
1166 if (is_null2 == 2)
1167 bad_null(expr->cond_false);
1168 expr->cond_false = cast_to(expr->cond_false, ltype);
1169 ctype = ltype;
1170 goto out;
1172 if (!(lclass & rclass & TYPE_PTR)) {
1173 typediff = "different types";
1174 goto Err;
1176 /* OK, it's pointer on pointer */
1177 if (ltype->ctype.as != rtype->ctype.as) {
1178 typediff = "different address spaces";
1179 goto Err;
1182 /* need to be lazier here */
1183 lbase = examine_pointer_target(ltype);
1184 rbase = examine_pointer_target(rtype);
1185 qual = target_qualifiers(ltype) | target_qualifiers(rtype);
1187 if (lbase == &void_ctype) {
1188 /* XXX: pointers to function should warn here */
1189 ctype = ltype;
1190 goto Qual;
1193 if (rbase == &void_ctype) {
1194 /* XXX: pointers to function should warn here */
1195 ctype = rtype;
1196 goto Qual;
1198 /* XXX: that should be pointer to composite */
1199 ctype = ltype;
1200 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1201 qual, qual);
1202 if (!typediff)
1203 goto Qual;
1204 goto Err;
1207 /* void on void, struct on same struct, union on same union */
1208 if (ltype == rtype) {
1209 ctype = ltype;
1210 goto out;
1212 typediff = "different base types";
1214 Err:
1215 expression_error(expr, "incompatible types in conditional expression (%s)", typediff);
1216 return NULL;
1218 out:
1219 expr->ctype = ctype;
1220 return ctype;
1222 Qual:
1223 if (qual & ~ctype->ctype.modifiers) {
1224 struct symbol *sym = alloc_symbol(ctype->pos, SYM_PTR);
1225 *sym = *ctype;
1226 sym->ctype.modifiers |= qual;
1227 ctype = sym;
1229 *true = cast_to(*true, ctype);
1230 expr->cond_false = cast_to(expr->cond_false, ctype);
1231 goto out;
1234 /* FP assignments can not do modulo or bit operations */
1235 static int compatible_float_op(int op)
1237 return op == SPECIAL_ADD_ASSIGN ||
1238 op == SPECIAL_SUB_ASSIGN ||
1239 op == SPECIAL_MUL_ASSIGN ||
1240 op == SPECIAL_DIV_ASSIGN;
1243 static int evaluate_assign_op(struct expression *expr)
1245 struct symbol *target = expr->left->ctype;
1246 struct symbol *source = expr->right->ctype;
1247 struct symbol *t, *s;
1248 int tclass = classify_type(target, &t);
1249 int sclass = classify_type(source, &s);
1250 int op = expr->op;
1252 if (tclass & sclass & TYPE_NUM) {
1253 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1254 expression_error(expr, "invalid assignment");
1255 return 0;
1257 if (tclass & TYPE_RESTRICT) {
1258 if (!restricted_binop(op, t)) {
1259 warning(expr->pos, "bad assignment (%s) to %s",
1260 show_special(op), show_typename(t));
1261 expr->right = cast_to(expr->right, target);
1262 return 0;
1264 /* allowed assignments unfoul */
1265 if (sclass & TYPE_FOULED && unfoul(s) == t)
1266 goto Cast;
1267 if (!restricted_value(expr->right, t))
1268 return 1;
1269 } else if (!(sclass & TYPE_RESTRICT))
1270 goto usual;
1271 /* source and target would better be identical restricted */
1272 if (t == s)
1273 return 1;
1274 warning(expr->pos, "invalid assignment: %s", show_special(op));
1275 info(expr->pos, " left side has type %s", show_typename(t));
1276 info(expr->pos, " right side has type %s", show_typename(s));
1277 expr->right = cast_to(expr->right, target);
1278 return 0;
1280 if (tclass == TYPE_PTR && is_int(sclass)) {
1281 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1282 unrestrict(expr->right, sclass, &s);
1283 evaluate_ptr_add(expr, s);
1284 return 1;
1286 expression_error(expr, "invalid pointer assignment");
1287 return 0;
1290 expression_error(expr, "invalid assignment");
1291 return 0;
1293 usual:
1294 target = usual_conversions(op, expr->left, expr->right,
1295 tclass, sclass, target, source);
1296 Cast:
1297 expr->right = cast_to(expr->right, target);
1298 return 1;
1301 static int whitelist_pointers(struct symbol *t1, struct symbol *t2)
1303 if (t1 == t2)
1304 return 0; /* yes, 0 - we don't want a cast_to here */
1305 if (t1 == &void_ctype)
1306 return 1;
1307 if (t2 == &void_ctype)
1308 return 1;
1309 if (classify_type(t1, &t1) != TYPE_NUM)
1310 return 0;
1311 if (classify_type(t2, &t2) != TYPE_NUM)
1312 return 0;
1313 if (t1 == t2)
1314 return 1;
1315 if (t1->ctype.modifiers & t2->ctype.modifiers & MOD_CHAR)
1316 return 1;
1317 if ((t1->ctype.modifiers ^ t2->ctype.modifiers) & MOD_SIZE)
1318 return 0;
1319 return !Wtypesign;
1322 static int check_assignment_types(struct symbol *target, struct expression **rp,
1323 const char **typediff)
1325 struct symbol *source = degenerate(*rp);
1326 struct symbol *t, *s;
1327 int tclass = classify_type(target, &t);
1328 int sclass = classify_type(source, &s);
1330 if (tclass & sclass & TYPE_NUM) {
1331 if (tclass & TYPE_RESTRICT) {
1332 /* allowed assignments unfoul */
1333 if (sclass & TYPE_FOULED && unfoul(s) == t)
1334 goto Cast;
1335 if (!restricted_value(*rp, target))
1336 return 1;
1337 if (s == t)
1338 return 1;
1339 } else if (!(sclass & TYPE_RESTRICT))
1340 goto Cast;
1341 *typediff = "different base types";
1342 return 0;
1345 if (tclass == TYPE_PTR) {
1346 unsigned long mod1, mod2;
1347 struct symbol *b1, *b2;
1348 // NULL pointer is always OK
1349 int is_null = is_null_pointer_constant(*rp);
1350 if (is_null) {
1351 if (is_null == 2)
1352 bad_null(*rp);
1353 goto Cast;
1355 if (!(sclass & TYPE_PTR)) {
1356 *typediff = "different base types";
1357 return 0;
1359 b1 = examine_pointer_target(t);
1360 b2 = examine_pointer_target(s);
1361 mod1 = target_qualifiers(t);
1362 mod2 = target_qualifiers(s);
1363 if (whitelist_pointers(b1, b2)) {
1365 * assignments to/from void * are OK, provided that
1366 * we do not remove qualifiers from pointed to [C]
1367 * or mix address spaces [sparse].
1369 if (t->ctype.as != s->ctype.as) {
1370 *typediff = "different address spaces";
1371 return 0;
1374 * If this is a function pointer assignment, it is
1375 * actually fine to assign a pointer to const data to
1376 * it, as a function pointer points to const data
1377 * implicitly, i.e., dereferencing it does not produce
1378 * an lvalue.
1380 if (b1->type == SYM_FN)
1381 mod1 |= MOD_CONST;
1382 if (mod2 & ~mod1) {
1383 *typediff = "different modifiers";
1384 return 0;
1386 goto Cast;
1388 /* It's OK if the target is more volatile or const than the source */
1389 *typediff = type_difference(&t->ctype, &s->ctype, 0, mod1);
1390 if (*typediff)
1391 return 0;
1392 return 1;
1395 if ((tclass & TYPE_COMPOUND) && s == t)
1396 return 1;
1398 if (tclass & TYPE_NUM) {
1399 /* XXX: need to turn into comparison with NULL */
1400 if (t == &bool_ctype && (sclass & TYPE_PTR))
1401 goto Cast;
1402 *typediff = "different base types";
1403 return 0;
1405 *typediff = "invalid types";
1406 return 0;
1408 Cast:
1409 *rp = cast_to(*rp, target);
1410 return 1;
1413 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1414 struct expression **rp, const char *where)
1416 const char *typediff;
1417 struct symbol *source = degenerate(*rp);
1419 if (!check_assignment_types(target, rp, &typediff)) {
1420 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1421 info(expr->pos, " expected %s", show_typename(target));
1422 info(expr->pos, " got %s", show_typename(source));
1423 *rp = cast_to(*rp, target);
1424 return 0;
1427 return 1;
1430 static int compatible_transparent_union(struct symbol *target,
1431 struct expression **rp)
1433 struct symbol *t, *member;
1434 classify_type(target, &t);
1435 if (t->type != SYM_UNION || !t->transparent_union)
1436 return 0;
1438 FOR_EACH_PTR(t->symbol_list, member) {
1439 const char *typediff;
1440 if (check_assignment_types(member, rp, &typediff))
1441 return 1;
1442 } END_FOR_EACH_PTR(member);
1444 return 0;
1447 static int compatible_argument_type(struct expression *expr, struct symbol *target,
1448 struct expression **rp, const char *where)
1450 if (compatible_transparent_union(target, rp))
1451 return 1;
1453 return compatible_assignment_types(expr, target, rp, where);
1456 static void mark_assigned(struct expression *expr)
1458 struct symbol *sym;
1460 if (!expr)
1461 return;
1462 switch (expr->type) {
1463 case EXPR_SYMBOL:
1464 sym = expr->symbol;
1465 if (!sym)
1466 return;
1467 if (sym->type != SYM_NODE)
1468 return;
1469 sym->ctype.modifiers |= MOD_ASSIGNED;
1470 return;
1472 case EXPR_BINOP:
1473 mark_assigned(expr->left);
1474 mark_assigned(expr->right);
1475 return;
1476 case EXPR_CAST:
1477 case EXPR_FORCE_CAST:
1478 mark_assigned(expr->cast_expression);
1479 return;
1480 case EXPR_SLICE:
1481 mark_assigned(expr->base);
1482 return;
1483 default:
1484 /* Hmm? */
1485 return;
1489 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1491 if (type->ctype.modifiers & MOD_CONST)
1492 expression_error(left, "assignment to const expression");
1494 /* We know left is an lvalue, so it's a "preop-*" */
1495 mark_assigned(left->unop);
1498 static struct symbol *evaluate_assignment(struct expression *expr)
1500 struct expression *left = expr->left;
1501 struct expression *where = expr;
1502 struct symbol *ltype;
1504 if (!lvalue_expression(left)) {
1505 expression_error(expr, "not an lvalue");
1506 return NULL;
1509 ltype = left->ctype;
1511 if (expr->op != '=') {
1512 if (!evaluate_assign_op(expr))
1513 return NULL;
1514 } else {
1515 if (!compatible_assignment_types(where, ltype, &expr->right, "assignment"))
1516 return NULL;
1519 evaluate_assign_to(left, ltype);
1521 expr->ctype = ltype;
1522 return ltype;
1525 static void examine_fn_arguments(struct symbol *fn)
1527 struct symbol *s;
1529 FOR_EACH_PTR(fn->arguments, s) {
1530 struct symbol *arg = evaluate_symbol(s);
1531 /* Array/function arguments silently degenerate into pointers */
1532 if (arg) {
1533 struct symbol *ptr;
1534 switch(arg->type) {
1535 case SYM_ARRAY:
1536 case SYM_FN:
1537 ptr = alloc_symbol(s->pos, SYM_PTR);
1538 if (arg->type == SYM_ARRAY)
1539 ptr->ctype = arg->ctype;
1540 else
1541 ptr->ctype.base_type = arg;
1542 ptr->ctype.as |= s->ctype.as;
1543 ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1545 s->ctype.base_type = ptr;
1546 s->ctype.as = 0;
1547 s->ctype.modifiers &= ~MOD_PTRINHERIT;
1548 s->bit_size = 0;
1549 s->examined = 0;
1550 examine_symbol_type(s);
1551 break;
1552 default:
1553 /* nothing */
1554 break;
1557 } END_FOR_EACH_PTR(s);
1560 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1562 /* Take the modifiers of the pointer, and apply them to the member */
1563 mod |= sym->ctype.modifiers;
1564 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1565 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1566 *newsym = *sym;
1567 newsym->ctype.as = as;
1568 newsym->ctype.modifiers = mod;
1569 sym = newsym;
1571 return sym;
1574 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1576 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1577 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1579 node->ctype.base_type = ptr;
1580 ptr->bit_size = bits_in_pointer;
1581 ptr->ctype.alignment = pointer_alignment;
1583 node->bit_size = bits_in_pointer;
1584 node->ctype.alignment = pointer_alignment;
1586 access_symbol(sym);
1587 if (sym->ctype.modifiers & MOD_REGISTER) {
1588 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1589 sym->ctype.modifiers &= ~MOD_REGISTER;
1591 if (sym->type == SYM_NODE) {
1592 ptr->ctype.as |= sym->ctype.as;
1593 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1594 sym = sym->ctype.base_type;
1596 if (degenerate && sym->type == SYM_ARRAY) {
1597 ptr->ctype.as |= sym->ctype.as;
1598 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1599 sym = sym->ctype.base_type;
1601 ptr->ctype.base_type = sym;
1603 return node;
1606 /* Arrays degenerate into pointers on pointer arithmetic */
1607 static struct symbol *degenerate(struct expression *expr)
1609 struct symbol *ctype, *base;
1611 if (!expr)
1612 return NULL;
1613 ctype = expr->ctype;
1614 if (!ctype)
1615 return NULL;
1616 base = examine_symbol_type(ctype);
1617 if (ctype->type == SYM_NODE)
1618 base = ctype->ctype.base_type;
1620 * Arrays degenerate into pointers to the entries, while
1621 * functions degenerate into pointers to themselves.
1622 * If array was part of non-lvalue compound, we create a copy
1623 * of that compound first and then act as if we were dealing with
1624 * the corresponding field in there.
1626 switch (base->type) {
1627 case SYM_ARRAY:
1628 if (expr->type == EXPR_SLICE) {
1629 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1630 struct expression *e0, *e1, *e2, *e3, *e4;
1632 a->ctype.base_type = expr->base->ctype;
1633 a->bit_size = expr->base->ctype->bit_size;
1634 a->array_size = expr->base->ctype->array_size;
1636 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1637 e0->symbol = a;
1638 e0->ctype = &lazy_ptr_ctype;
1640 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1641 e1->unop = e0;
1642 e1->op = '*';
1643 e1->ctype = expr->base->ctype; /* XXX */
1645 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1646 e2->left = e1;
1647 e2->right = expr->base;
1648 e2->op = '=';
1649 e2->ctype = expr->base->ctype;
1651 if (expr->r_bitpos) {
1652 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1653 e3->op = '+';
1654 e3->left = e0;
1655 e3->right = alloc_const_expression(expr->pos,
1656 bits_to_bytes(expr->r_bitpos));
1657 e3->ctype = &lazy_ptr_ctype;
1658 } else {
1659 e3 = e0;
1662 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1663 e4->left = e2;
1664 e4->right = e3;
1665 e4->ctype = &lazy_ptr_ctype;
1667 expr->unop = e4;
1668 expr->type = EXPR_PREOP;
1669 expr->op = '*';
1671 case SYM_FN:
1672 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1673 expression_error(expr, "strange non-value function or array");
1674 return &bad_ctype;
1676 *expr = *expr->unop;
1677 ctype = create_pointer(expr, ctype, 1);
1678 expr->ctype = ctype;
1679 default:
1680 /* nothing */;
1682 return ctype;
1685 static struct symbol *evaluate_addressof(struct expression *expr)
1687 struct expression *op = expr->unop;
1688 struct symbol *ctype;
1690 if (op->op != '*' || op->type != EXPR_PREOP) {
1691 expression_error(expr, "not addressable");
1692 return NULL;
1694 ctype = op->ctype;
1695 *expr = *op->unop;
1696 expr->flags = 0;
1698 if (expr->type == EXPR_SYMBOL) {
1699 struct symbol *sym = expr->symbol;
1700 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1704 * symbol expression evaluation is lazy about the type
1705 * of the sub-expression, so we may have to generate
1706 * the type here if so..
1708 if (expr->ctype == &lazy_ptr_ctype) {
1709 ctype = create_pointer(expr, ctype, 0);
1710 expr->ctype = ctype;
1712 return expr->ctype;
1716 static struct symbol *evaluate_dereference(struct expression *expr)
1718 struct expression *op = expr->unop;
1719 struct symbol *ctype = op->ctype, *node, *target;
1721 /* Simplify: *&(expr) => (expr) */
1722 if (op->type == EXPR_PREOP && op->op == '&') {
1723 *expr = *op->unop;
1724 expr->flags = 0;
1725 return expr->ctype;
1728 /* Dereferencing a node drops all the node information. */
1729 if (ctype->type == SYM_NODE)
1730 ctype = ctype->ctype.base_type;
1732 node = alloc_symbol(expr->pos, SYM_NODE);
1733 target = ctype->ctype.base_type;
1735 switch (ctype->type) {
1736 default:
1737 expression_error(expr, "cannot dereference this type");
1738 return NULL;
1739 case SYM_PTR:
1740 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1741 merge_type(node, ctype);
1742 break;
1744 case SYM_ARRAY:
1745 if (!lvalue_expression(op)) {
1746 expression_error(op, "non-lvalue array??");
1747 return NULL;
1750 /* Do the implied "addressof" on the array */
1751 *op = *op->unop;
1754 * When an array is dereferenced, we need to pick
1755 * up the attributes of the original node too..
1757 merge_type(node, op->ctype);
1758 merge_type(node, ctype);
1759 break;
1762 node->bit_size = target->bit_size;
1763 node->array_size = target->array_size;
1765 expr->ctype = node;
1766 return node;
1770 * Unary post-ops: x++ and x--
1772 static struct symbol *evaluate_postop(struct expression *expr)
1774 struct expression *op = expr->unop;
1775 struct symbol *ctype = op->ctype;
1776 int class = classify_type(ctype, &ctype);
1777 int multiply = 0;
1779 if (!class || class & TYPE_COMPOUND) {
1780 expression_error(expr, "need scalar for ++/--");
1781 return NULL;
1783 if (!lvalue_expression(expr->unop)) {
1784 expression_error(expr, "need lvalue expression for ++/--");
1785 return NULL;
1788 if ((class & TYPE_RESTRICT) && restricted_unop(expr->op, &ctype))
1789 unrestrict(expr, class, &ctype);
1791 if (class & TYPE_NUM) {
1792 multiply = 1;
1793 } else if (class == TYPE_PTR) {
1794 struct symbol *target = examine_pointer_target(ctype);
1795 if (!is_function(target))
1796 multiply = bits_to_bytes(target->bit_size);
1799 if (multiply) {
1800 evaluate_assign_to(op, op->ctype);
1801 expr->op_value = multiply;
1802 expr->ctype = ctype;
1803 return ctype;
1806 expression_error(expr, "bad argument type for ++/--");
1807 return NULL;
1810 static struct symbol *evaluate_sign(struct expression *expr)
1812 struct symbol *ctype = expr->unop->ctype;
1813 int class = classify_type(ctype, &ctype);
1814 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1815 expr->flags = 0;
1816 /* should be an arithmetic type */
1817 if (!(class & TYPE_NUM))
1818 return bad_expr_type(expr);
1819 if (class & TYPE_RESTRICT)
1820 goto Restr;
1821 Normal:
1822 if (!(class & TYPE_FLOAT)) {
1823 ctype = integer_promotion(ctype);
1824 expr->unop = cast_to(expr->unop, ctype);
1825 } else if (expr->op != '~') {
1826 /* no conversions needed */
1827 } else {
1828 return bad_expr_type(expr);
1830 if (expr->op == '+')
1831 *expr = *expr->unop;
1832 expr->ctype = ctype;
1833 return ctype;
1834 Restr:
1835 if (restricted_unop(expr->op, &ctype))
1836 unrestrict(expr, class, &ctype);
1837 goto Normal;
1840 static struct symbol *evaluate_preop(struct expression *expr)
1842 struct symbol *ctype = expr->unop->ctype;
1844 switch (expr->op) {
1845 case '(':
1846 *expr = *expr->unop;
1847 return ctype;
1849 case '+':
1850 case '-':
1851 case '~':
1852 return evaluate_sign(expr);
1854 case '*':
1855 return evaluate_dereference(expr);
1857 case '&':
1858 return evaluate_addressof(expr);
1860 case SPECIAL_INCREMENT:
1861 case SPECIAL_DECREMENT:
1863 * From a type evaluation standpoint the preops are
1864 * the same as the postops
1866 return evaluate_postop(expr);
1868 case '!':
1869 if (expr->flags && !(expr->unop->flags & Int_const_expr))
1870 expr->flags = 0;
1871 if (is_safe_type(ctype))
1872 warning(expr->pos, "testing a 'safe expression'");
1873 if (is_float_type(ctype)) {
1874 struct expression *arg = expr->unop;
1875 expr->type = EXPR_COMPARE;
1876 expr->op = SPECIAL_EQUAL;
1877 expr->left = arg;
1878 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1879 expr->right->ctype = ctype;
1880 expr->right->fvalue = 0;
1881 } else if (is_fouled_type(ctype)) {
1882 warning(expr->pos, "%s degrades to integer",
1883 show_typename(ctype->ctype.base_type));
1885 /* the result is int [6.5.3.3(5)]*/
1886 ctype = &int_ctype;
1887 break;
1889 default:
1890 break;
1892 expr->ctype = ctype;
1893 return ctype;
1896 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1898 struct ptr_list *head = (struct ptr_list *)_list;
1899 struct ptr_list *list = head;
1901 if (!head)
1902 return NULL;
1903 do {
1904 int i;
1905 for (i = 0; i < list->nr; i++) {
1906 struct symbol *sym = (struct symbol *) list->list[i];
1907 if (sym->ident) {
1908 if (sym->ident != ident)
1909 continue;
1910 *offset = sym->offset;
1911 return sym;
1912 } else {
1913 struct symbol *ctype = sym->ctype.base_type;
1914 struct symbol *sub;
1915 if (!ctype)
1916 continue;
1917 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1918 continue;
1919 sub = find_identifier(ident, ctype->symbol_list, offset);
1920 if (!sub)
1921 continue;
1922 *offset += sym->offset;
1923 return sub;
1926 } while ((list = list->next) != head);
1927 return NULL;
1930 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1932 struct expression *add;
1935 * Create a new add-expression
1937 * NOTE! Even if we just add zero, we need a new node
1938 * for the member pointer, since it has a different
1939 * type than the original pointer. We could make that
1940 * be just a cast, but the fact is, a node is a node,
1941 * so we might as well just do the "add zero" here.
1943 add = alloc_expression(expr->pos, EXPR_BINOP);
1944 add->op = '+';
1945 add->left = expr;
1946 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1947 add->right->ctype = &int_ctype;
1948 add->right->value = offset;
1951 * The ctype of the pointer will be lazily evaluated if
1952 * we ever take the address of this member dereference..
1954 add->ctype = &lazy_ptr_ctype;
1955 return add;
1958 /* structure/union dereference */
1959 static struct symbol *evaluate_member_dereference(struct expression *expr)
1961 int offset;
1962 struct symbol *ctype, *member;
1963 struct expression *deref = expr->deref, *add;
1964 struct ident *ident = expr->member;
1965 unsigned int mod;
1966 int address_space;
1968 if (!evaluate_expression(deref))
1969 return NULL;
1970 if (!ident) {
1971 expression_error(expr, "bad member name");
1972 return NULL;
1975 ctype = deref->ctype;
1976 examine_symbol_type(ctype);
1977 address_space = ctype->ctype.as;
1978 mod = ctype->ctype.modifiers;
1979 if (ctype->type == SYM_NODE) {
1980 ctype = ctype->ctype.base_type;
1981 address_space |= ctype->ctype.as;
1982 mod |= ctype->ctype.modifiers;
1984 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1985 expression_error(expr, "expected structure or union");
1986 return NULL;
1988 offset = 0;
1989 member = find_identifier(ident, ctype->symbol_list, &offset);
1990 if (!member) {
1991 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1992 const char *name = "<unnamed>";
1993 int namelen = 9;
1994 if (ctype->ident) {
1995 name = ctype->ident->name;
1996 namelen = ctype->ident->len;
1998 if (ctype->symbol_list)
1999 expression_error(expr, "no member '%s' in %s %.*s",
2000 show_ident(ident), type, namelen, name);
2001 else
2002 expression_error(expr, "using member '%s' in "
2003 "incomplete %s %.*s", show_ident(ident),
2004 type, namelen, name);
2005 return NULL;
2009 * The member needs to take on the address space and modifiers of
2010 * the "parent" type.
2012 member = convert_to_as_mod(member, address_space, mod);
2013 ctype = get_base_type(member);
2015 if (!lvalue_expression(deref)) {
2016 if (deref->type != EXPR_SLICE) {
2017 expr->base = deref;
2018 expr->r_bitpos = 0;
2019 } else {
2020 expr->base = deref->base;
2021 expr->r_bitpos = deref->r_bitpos;
2023 expr->r_bitpos += bytes_to_bits(offset);
2024 expr->type = EXPR_SLICE;
2025 expr->r_nrbits = member->bit_size;
2026 expr->r_bitpos += member->bit_offset;
2027 expr->ctype = member;
2028 return member;
2031 deref = deref->unop;
2032 expr->deref = deref;
2034 add = evaluate_offset(deref, offset);
2035 expr->type = EXPR_PREOP;
2036 expr->op = '*';
2037 expr->unop = add;
2039 expr->ctype = member;
2040 return member;
2043 static int is_promoted(struct expression *expr)
2045 while (1) {
2046 switch (expr->type) {
2047 case EXPR_BINOP:
2048 case EXPR_SELECT:
2049 case EXPR_CONDITIONAL:
2050 return 1;
2051 case EXPR_COMMA:
2052 expr = expr->right;
2053 continue;
2054 case EXPR_PREOP:
2055 switch (expr->op) {
2056 case '(':
2057 expr = expr->unop;
2058 continue;
2059 case '+':
2060 case '-':
2061 case '~':
2062 return 1;
2063 default:
2064 return 0;
2066 default:
2067 return 0;
2073 static struct symbol *evaluate_cast(struct expression *);
2075 static struct symbol *evaluate_type_information(struct expression *expr)
2077 struct symbol *sym = expr->cast_type;
2078 if (!sym) {
2079 sym = evaluate_expression(expr->cast_expression);
2080 if (!sym)
2081 return NULL;
2083 * Expressions of restricted types will possibly get
2084 * promoted - check that here
2086 if (is_restricted_type(sym)) {
2087 if (sym->bit_size < bits_in_int && is_promoted(expr))
2088 sym = &int_ctype;
2089 } else if (is_fouled_type(sym)) {
2090 sym = &int_ctype;
2093 examine_symbol_type(sym);
2094 if (is_bitfield_type(sym)) {
2095 expression_error(expr, "trying to examine bitfield type");
2096 return NULL;
2098 return sym;
2101 static struct symbol *evaluate_sizeof(struct expression *expr)
2103 struct symbol *type;
2104 int size;
2106 type = evaluate_type_information(expr);
2107 if (!type)
2108 return NULL;
2110 size = type->bit_size;
2112 if (size < 0 && is_void_type(type)) {
2113 warning(expr->pos, "expression using sizeof(void)");
2114 size = bits_in_char;
2117 if (size == 1 && is_bool_type(type)) {
2118 if (Wsizeof_bool)
2119 warning(expr->pos, "expression using sizeof bool");
2120 size = bits_in_char;
2123 if (is_function(type->ctype.base_type)) {
2124 warning(expr->pos, "expression using sizeof on a function");
2125 size = bits_in_char;
2128 if ((size < 0) || (size & (bits_in_char - 1)))
2129 expression_error(expr, "cannot size expression");
2131 expr->type = EXPR_VALUE;
2132 expr->value = bits_to_bytes(size);
2133 expr->taint = 0;
2134 expr->ctype = size_t_ctype;
2135 return size_t_ctype;
2138 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
2140 struct symbol *type;
2141 int size;
2143 type = evaluate_type_information(expr);
2144 if (!type)
2145 return NULL;
2147 if (type->type == SYM_NODE)
2148 type = type->ctype.base_type;
2149 if (!type)
2150 return NULL;
2151 switch (type->type) {
2152 case SYM_ARRAY:
2153 break;
2154 case SYM_PTR:
2155 type = get_base_type(type);
2156 if (type)
2157 break;
2158 default:
2159 expression_error(expr, "expected pointer expression");
2160 return NULL;
2162 size = type->bit_size;
2163 if (size & (bits_in_char-1))
2164 size = 0;
2165 expr->type = EXPR_VALUE;
2166 expr->value = bits_to_bytes(size);
2167 expr->taint = 0;
2168 expr->ctype = size_t_ctype;
2169 return size_t_ctype;
2172 static struct symbol *evaluate_alignof(struct expression *expr)
2174 struct symbol *type;
2176 type = evaluate_type_information(expr);
2177 if (!type)
2178 return NULL;
2180 expr->type = EXPR_VALUE;
2181 expr->value = type->ctype.alignment;
2182 expr->taint = 0;
2183 expr->ctype = size_t_ctype;
2184 return size_t_ctype;
2187 static int evaluate_arguments(struct symbol *fn, struct expression_list *head)
2189 struct expression *expr;
2190 struct symbol_list *argument_types = fn->arguments;
2191 struct symbol *argtype;
2192 int i = 1;
2194 PREPARE_PTR_LIST(argument_types, argtype);
2195 FOR_EACH_PTR (head, expr) {
2196 struct expression **p = THIS_ADDRESS(expr);
2197 struct symbol *ctype, *target;
2198 ctype = evaluate_expression(expr);
2200 if (!ctype)
2201 return 0;
2203 target = argtype;
2204 if (!target) {
2205 struct symbol *type;
2206 int class = classify_type(ctype, &type);
2207 if (is_int(class)) {
2208 *p = cast_to(expr, integer_promotion(type));
2209 } else if (class & TYPE_FLOAT) {
2210 unsigned long mod = type->ctype.modifiers;
2211 if (!(mod & (MOD_LONG_ALL)))
2212 *p = cast_to(expr, &double_ctype);
2213 } else if (class & TYPE_PTR) {
2214 if (expr->ctype == &null_ctype)
2215 *p = cast_to(expr, &ptr_ctype);
2216 else
2217 degenerate(expr);
2219 } else if (!target->forced_arg){
2220 static char where[30];
2221 examine_symbol_type(target);
2222 sprintf(where, "argument %d", i);
2223 compatible_argument_type(expr, target, p, where);
2226 i++;
2227 NEXT_PTR_LIST(argtype);
2228 } END_FOR_EACH_PTR(expr);
2229 FINISH_PTR_LIST(argtype);
2230 return 1;
2233 static void convert_index(struct expression *e)
2235 struct expression *child = e->idx_expression;
2236 unsigned from = e->idx_from;
2237 unsigned to = e->idx_to + 1;
2238 e->type = EXPR_POS;
2239 e->init_offset = from * bits_to_bytes(e->ctype->bit_size);
2240 e->init_nr = to - from;
2241 e->init_expr = child;
2244 static void convert_ident(struct expression *e)
2246 struct expression *child = e->ident_expression;
2247 int offset = e->offset;
2249 e->type = EXPR_POS;
2250 e->init_offset = offset;
2251 e->init_nr = 1;
2252 e->init_expr = child;
2255 static void convert_designators(struct expression *e)
2257 while (e) {
2258 if (e->type == EXPR_INDEX)
2259 convert_index(e);
2260 else if (e->type == EXPR_IDENTIFIER)
2261 convert_ident(e);
2262 else
2263 break;
2264 e = e->init_expr;
2268 static void excess(struct expression *e, const char *s)
2270 warning(e->pos, "excessive elements in %s initializer", s);
2274 * implicit designator for the first element
2276 static struct expression *first_subobject(struct symbol *ctype, int class,
2277 struct expression **v)
2279 struct expression *e = *v, *new;
2281 if (ctype->type == SYM_NODE)
2282 ctype = ctype->ctype.base_type;
2284 if (class & TYPE_PTR) { /* array */
2285 if (!ctype->bit_size)
2286 return NULL;
2287 new = alloc_expression(e->pos, EXPR_INDEX);
2288 new->idx_expression = e;
2289 new->ctype = ctype->ctype.base_type;
2290 } else {
2291 struct symbol *field, *p;
2292 PREPARE_PTR_LIST(ctype->symbol_list, p);
2293 while (p && !p->ident && is_bitfield_type(p))
2294 NEXT_PTR_LIST(p);
2295 field = p;
2296 FINISH_PTR_LIST(p);
2297 if (!field)
2298 return NULL;
2299 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2300 new->ident_expression = e;
2301 new->field = new->ctype = field;
2302 new->offset = field->offset;
2304 *v = new;
2305 return new;
2309 * sanity-check explicit designators; return the innermost one or NULL
2310 * in case of error. Assign types.
2312 static struct expression *check_designators(struct expression *e,
2313 struct symbol *ctype)
2315 struct expression *last = NULL;
2316 const char *err;
2317 while (1) {
2318 if (ctype->type == SYM_NODE)
2319 ctype = ctype->ctype.base_type;
2320 if (e->type == EXPR_INDEX) {
2321 struct symbol *type;
2322 if (ctype->type != SYM_ARRAY) {
2323 err = "array index in non-array";
2324 break;
2326 type = ctype->ctype.base_type;
2327 if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2328 unsigned offset = array_element_offset(type->bit_size, e->idx_to);
2329 if (offset >= ctype->bit_size) {
2330 err = "index out of bounds in";
2331 break;
2334 e->ctype = ctype = type;
2335 ctype = type;
2336 last = e;
2337 if (!e->idx_expression) {
2338 err = "invalid";
2339 break;
2341 e = e->idx_expression;
2342 } else if (e->type == EXPR_IDENTIFIER) {
2343 int offset = 0;
2344 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2345 err = "field name not in struct or union";
2346 break;
2348 ctype = find_identifier(e->expr_ident, ctype->symbol_list, &offset);
2349 if (!ctype) {
2350 err = "unknown field name in";
2351 break;
2353 e->offset = offset;
2354 e->field = e->ctype = ctype;
2355 last = e;
2356 if (!e->ident_expression) {
2357 err = "invalid";
2358 break;
2360 e = e->ident_expression;
2361 } else if (e->type == EXPR_POS) {
2362 err = "internal front-end error: EXPR_POS in";
2363 break;
2364 } else
2365 return last;
2367 expression_error(e, "%s initializer", err);
2368 return NULL;
2372 * choose the next subobject to initialize.
2374 * Get designators for next element, switch old ones to EXPR_POS.
2375 * Return the resulting expression or NULL if we'd run out of subobjects.
2376 * The innermost designator is returned in *v. Designators in old
2377 * are assumed to be already sanity-checked.
2379 static struct expression *next_designators(struct expression *old,
2380 struct symbol *ctype,
2381 struct expression *e, struct expression **v)
2383 struct expression *new = NULL;
2385 if (!old)
2386 return NULL;
2387 if (old->type == EXPR_INDEX) {
2388 struct expression *copy;
2389 unsigned n;
2391 copy = next_designators(old->idx_expression,
2392 old->ctype, e, v);
2393 if (!copy) {
2394 n = old->idx_to + 1;
2395 if (array_element_offset(old->ctype->bit_size, n) == ctype->bit_size) {
2396 convert_index(old);
2397 return NULL;
2399 copy = e;
2400 *v = new = alloc_expression(e->pos, EXPR_INDEX);
2401 } else {
2402 n = old->idx_to;
2403 new = alloc_expression(e->pos, EXPR_INDEX);
2406 new->idx_from = new->idx_to = n;
2407 new->idx_expression = copy;
2408 new->ctype = old->ctype;
2409 convert_index(old);
2410 } else if (old->type == EXPR_IDENTIFIER) {
2411 struct expression *copy;
2412 struct symbol *field;
2413 int offset = 0;
2415 copy = next_designators(old->ident_expression,
2416 old->ctype, e, v);
2417 if (!copy) {
2418 field = old->field->next_subobject;
2419 if (!field) {
2420 convert_ident(old);
2421 return NULL;
2423 copy = e;
2424 *v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2426 * We can't necessarily trust "field->offset",
2427 * because the field might be in an anonymous
2428 * union, and the field offset is then the offset
2429 * within that union.
2431 * The "old->offset - old->field->offset"
2432 * would be the offset of such an anonymous
2433 * union.
2435 offset = old->offset - old->field->offset;
2436 } else {
2437 field = old->field;
2438 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2441 new->field = field;
2442 new->expr_ident = field->ident;
2443 new->ident_expression = copy;
2444 new->ctype = field;
2445 new->offset = field->offset + offset;
2446 convert_ident(old);
2448 return new;
2451 static int handle_simple_initializer(struct expression **ep, int nested,
2452 int class, struct symbol *ctype);
2455 * deal with traversing subobjects [6.7.8(17,18,20)]
2457 static void handle_list_initializer(struct expression *expr,
2458 int class, struct symbol *ctype)
2460 struct expression *e, *last = NULL, *top = NULL, *next;
2461 int jumped = 0;
2463 FOR_EACH_PTR(expr->expr_list, e) {
2464 struct expression **v;
2465 struct symbol *type;
2466 int lclass;
2468 if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2469 struct symbol *struct_sym;
2470 if (!top) {
2471 top = e;
2472 last = first_subobject(ctype, class, &top);
2473 } else {
2474 last = next_designators(last, ctype, e, &top);
2476 if (!last) {
2477 excess(e, class & TYPE_PTR ? "array" :
2478 "struct or union");
2479 DELETE_CURRENT_PTR(e);
2480 continue;
2482 struct_sym = ctype->type == SYM_NODE ? ctype->ctype.base_type : ctype;
2483 if (Wdesignated_init && struct_sym->designated_init)
2484 warning(e->pos, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2485 ctype->ident ? "in initializer for " : "",
2486 ctype->ident ? ctype->ident->len : 0,
2487 ctype->ident ? ctype->ident->name : "",
2488 ctype->ident ? ": " : "",
2489 get_type_name(struct_sym->type),
2490 show_ident(struct_sym->ident));
2491 if (jumped) {
2492 warning(e->pos, "advancing past deep designator");
2493 jumped = 0;
2495 REPLACE_CURRENT_PTR(e, last);
2496 } else {
2497 next = check_designators(e, ctype);
2498 if (!next) {
2499 DELETE_CURRENT_PTR(e);
2500 continue;
2502 top = next;
2503 /* deeper than one designator? */
2504 jumped = top != e;
2505 convert_designators(last);
2506 last = e;
2509 found:
2510 lclass = classify_type(top->ctype, &type);
2511 if (top->type == EXPR_INDEX)
2512 v = &top->idx_expression;
2513 else
2514 v = &top->ident_expression;
2516 if (handle_simple_initializer(v, 1, lclass, top->ctype))
2517 continue;
2519 if (!(lclass & TYPE_COMPOUND)) {
2520 warning(e->pos, "bogus scalar initializer");
2521 DELETE_CURRENT_PTR(e);
2522 continue;
2525 next = first_subobject(type, lclass, v);
2526 if (next) {
2527 warning(e->pos, "missing braces around initializer");
2528 top = next;
2529 goto found;
2532 DELETE_CURRENT_PTR(e);
2533 excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2535 } END_FOR_EACH_PTR(e);
2537 convert_designators(last);
2538 expr->ctype = ctype;
2541 static int is_string_literal(struct expression **v)
2543 struct expression *e = *v;
2544 while (e && e->type == EXPR_PREOP && e->op == '(')
2545 e = e->unop;
2546 if (!e || e->type != EXPR_STRING)
2547 return 0;
2548 if (e != *v && Wparen_string)
2549 warning(e->pos,
2550 "array initialized from parenthesized string constant");
2551 *v = e;
2552 return 1;
2556 * We want a normal expression, possibly in one layer of braces. Warn
2557 * if the latter happens inside a list (it's legal, but likely to be
2558 * an effect of screwup). In case of anything not legal, we are definitely
2559 * having an effect of screwup, so just fail and let the caller warn.
2561 static struct expression *handle_scalar(struct expression *e, int nested)
2563 struct expression *v = NULL, *p;
2564 int count = 0;
2566 /* normal case */
2567 if (e->type != EXPR_INITIALIZER)
2568 return e;
2570 FOR_EACH_PTR(e->expr_list, p) {
2571 if (!v)
2572 v = p;
2573 count++;
2574 } END_FOR_EACH_PTR(p);
2575 if (count != 1)
2576 return NULL;
2577 switch(v->type) {
2578 case EXPR_INITIALIZER:
2579 case EXPR_INDEX:
2580 case EXPR_IDENTIFIER:
2581 return NULL;
2582 default:
2583 break;
2585 if (nested)
2586 warning(e->pos, "braces around scalar initializer");
2587 return v;
2591 * deal with the cases that don't care about subobjects:
2592 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2593 * character array <- string literal, possibly in braces [6.7.8(14)]
2594 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2595 * compound type <- initializer list in braces [6.7.8(16)]
2596 * The last one punts to handle_list_initializer() which, in turn will call
2597 * us for individual elements of the list.
2599 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2600 * the lack of support of wide char stuff in general.
2602 * One note: we need to take care not to evaluate a string literal until
2603 * we know that we *will* handle it right here. Otherwise we would screw
2604 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2605 * { "string", ...} - we need to preserve that string literal recognizable
2606 * until we dig into the inner struct.
2608 static int handle_simple_initializer(struct expression **ep, int nested,
2609 int class, struct symbol *ctype)
2611 int is_string = is_string_type(ctype);
2612 struct expression *e = *ep, *p;
2613 struct symbol *type;
2615 if (!e)
2616 return 0;
2618 /* scalar */
2619 if (!(class & TYPE_COMPOUND)) {
2620 e = handle_scalar(e, nested);
2621 if (!e)
2622 return 0;
2623 *ep = e;
2624 if (!evaluate_expression(e))
2625 return 1;
2626 compatible_assignment_types(e, ctype, ep, "initializer");
2627 return 1;
2631 * sublist; either a string, or we dig in; the latter will deal with
2632 * pathologies, so we don't need anything fancy here.
2634 if (e->type == EXPR_INITIALIZER) {
2635 if (is_string) {
2636 struct expression *v = NULL;
2637 int count = 0;
2639 FOR_EACH_PTR(e->expr_list, p) {
2640 if (!v)
2641 v = p;
2642 count++;
2643 } END_FOR_EACH_PTR(p);
2644 if (count == 1 && is_string_literal(&v)) {
2645 *ep = e = v;
2646 goto String;
2649 handle_list_initializer(e, class, ctype);
2650 return 1;
2653 /* string */
2654 if (is_string_literal(&e)) {
2655 /* either we are doing array of char, or we'll have to dig in */
2656 if (is_string) {
2657 *ep = e;
2658 goto String;
2660 return 0;
2662 /* struct or union can be initialized by compatible */
2663 if (class != TYPE_COMPOUND)
2664 return 0;
2665 type = evaluate_expression(e);
2666 if (!type)
2667 return 0;
2668 if (ctype->type == SYM_NODE)
2669 ctype = ctype->ctype.base_type;
2670 if (type->type == SYM_NODE)
2671 type = type->ctype.base_type;
2672 if (ctype == type)
2673 return 1;
2674 return 0;
2676 String:
2677 p = alloc_expression(e->pos, EXPR_STRING);
2678 *p = *e;
2679 type = evaluate_expression(p);
2680 if (ctype->bit_size != -1) {
2681 if (ctype->bit_size + bits_in_char < type->bit_size)
2682 warning(e->pos,
2683 "too long initializer-string for array of char");
2684 else if (Winit_cstring && ctype->bit_size + bits_in_char == type->bit_size) {
2685 warning(e->pos,
2686 "too long initializer-string for array of char(no space for nul char)");
2689 *ep = p;
2690 return 1;
2693 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2695 struct symbol *type;
2696 int class = classify_type(ctype, &type);
2697 if (!handle_simple_initializer(ep, 0, class, ctype))
2698 expression_error(*ep, "invalid initializer");
2701 static struct symbol *cast_to_bool(struct expression *expr)
2703 struct expression *old = expr->cast_expression;
2704 struct expression *zero;
2705 struct symbol *otype;
2706 int oclass = classify_type(degenerate(old), &otype);
2707 struct symbol *ctype;
2709 if (oclass & TYPE_COMPOUND)
2710 return NULL;
2712 zero = alloc_const_expression(expr->pos, 0);
2713 expr->op = SPECIAL_NOTEQUAL;
2714 ctype = usual_conversions(expr->op, old, zero,
2715 oclass, TYPE_NUM, otype, zero->ctype);
2716 expr->type = EXPR_COMPARE;
2717 expr->left = cast_to(old, ctype);
2718 expr->right = cast_to(zero, ctype);
2720 return expr->ctype;
2723 static struct symbol *evaluate_cast(struct expression *expr)
2725 struct expression *target = expr->cast_expression;
2726 struct symbol *ctype;
2727 struct symbol *t1, *t2;
2728 int class1, class2;
2729 int as1 = 0, as2 = 0;
2731 if (!target)
2732 return NULL;
2735 * Special case: a cast can be followed by an
2736 * initializer, in which case we need to pass
2737 * the type value down to that initializer rather
2738 * than trying to evaluate it as an expression
2740 * A more complex case is when the initializer is
2741 * dereferenced as part of a post-fix expression.
2742 * We need to produce an expression that can be dereferenced.
2744 if (target->type == EXPR_INITIALIZER) {
2745 struct symbol *sym = expr->cast_type;
2746 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2748 sym->initializer = target;
2749 evaluate_symbol(sym);
2751 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2752 addr->symbol = sym;
2754 expr->type = EXPR_PREOP;
2755 expr->op = '*';
2756 expr->unop = addr;
2757 expr->ctype = sym;
2759 return sym;
2762 ctype = examine_symbol_type(expr->cast_type);
2763 expr->ctype = ctype;
2764 expr->cast_type = ctype;
2766 evaluate_expression(target);
2767 degenerate(target);
2769 class1 = classify_type(ctype, &t1);
2771 /* cast to non-integer type -> not an integer constant expression */
2772 if (!is_int(class1))
2773 expr->flags = 0;
2774 /* if argument turns out to be not an integer constant expression *and*
2775 it was not a floating literal to start with -> too bad */
2776 else if (expr->flags == Int_const_expr &&
2777 !(target->flags & Int_const_expr))
2778 expr->flags = 0;
2780 * You can always throw a value away by casting to
2781 * "void" - that's an implicit "force". Note that
2782 * the same is _not_ true of "void *".
2784 if (t1 == &void_ctype)
2785 goto out;
2787 if (class1 & (TYPE_COMPOUND | TYPE_FN))
2788 warning(expr->pos, "cast to non-scalar");
2790 t2 = target->ctype;
2791 if (!t2) {
2792 expression_error(expr, "cast from unknown type");
2793 goto out;
2795 class2 = classify_type(t2, &t2);
2797 if (class2 & TYPE_COMPOUND)
2798 warning(expr->pos, "cast from non-scalar");
2800 if (expr->type == EXPR_FORCE_CAST)
2801 goto out;
2803 /* allowed cast unfouls */
2804 if (class2 & TYPE_FOULED)
2805 t2 = unfoul(t2);
2807 if (t1 != t2) {
2808 if ((class1 & TYPE_RESTRICT) && restricted_value(target, t1))
2809 warning(expr->pos, "cast to %s",
2810 show_typename(t1));
2811 if (class2 & TYPE_RESTRICT)
2812 warning(expr->pos, "cast from %s",
2813 show_typename(t2));
2816 if (t1 == &ulong_ctype)
2817 as1 = -1;
2818 else if (class1 == TYPE_PTR) {
2819 examine_pointer_target(t1);
2820 as1 = t1->ctype.as;
2823 if (t2 == &ulong_ctype)
2824 as2 = -1;
2825 else if (class2 == TYPE_PTR) {
2826 examine_pointer_target(t2);
2827 as2 = t2->ctype.as;
2830 if (!as1 && as2 > 0)
2831 warning(expr->pos, "cast removes address space of expression");
2832 if (as1 > 0 && as2 > 0 && as1 != as2)
2833 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2834 if (as1 > 0 && !as2 &&
2835 !is_null_pointer_constant(target) && Wcast_to_as)
2836 warning(expr->pos,
2837 "cast adds address space to expression (<asn:%d>)", as1);
2839 if (!(t1->ctype.modifiers & MOD_PTRINHERIT) && class1 == TYPE_PTR &&
2840 !as1 && (target->flags & Int_const_expr)) {
2841 if (t1->ctype.base_type == &void_ctype) {
2842 if (is_zero_constant(target)) {
2843 /* NULL */
2844 expr->type = EXPR_VALUE;
2845 expr->ctype = &null_ctype;
2846 expr->value = 0;
2847 return ctype;
2852 if (t1 == &bool_ctype)
2853 cast_to_bool(expr);
2855 out:
2856 return ctype;
2860 * Evaluate a call expression with a symbol. This
2861 * should expand inline functions, and evaluate
2862 * builtins.
2864 static int evaluate_symbol_call(struct expression *expr)
2866 struct expression *fn = expr->fn;
2867 struct symbol *ctype = fn->ctype;
2869 if (fn->type != EXPR_PREOP)
2870 return 0;
2872 if (ctype->op && ctype->op->evaluate)
2873 return ctype->op->evaluate(expr);
2875 if (ctype->ctype.modifiers & MOD_INLINE) {
2876 int ret;
2877 struct symbol *curr = current_fn;
2879 if (ctype->definition)
2880 ctype = ctype->definition;
2882 current_fn = ctype->ctype.base_type;
2884 ret = inline_function(expr, ctype);
2886 /* restore the old function */
2887 current_fn = curr;
2888 return ret;
2891 return 0;
2894 static struct symbol *evaluate_call(struct expression *expr)
2896 int args, fnargs;
2897 struct symbol *ctype, *sym;
2898 struct expression *fn = expr->fn;
2899 struct expression_list *arglist = expr->args;
2901 if (!evaluate_expression(fn))
2902 return NULL;
2903 sym = ctype = fn->ctype;
2904 if (ctype->type == SYM_NODE)
2905 ctype = ctype->ctype.base_type;
2906 if (ctype->type == SYM_PTR)
2907 ctype = get_base_type(ctype);
2909 if (ctype->type != SYM_FN) {
2910 struct expression *arg;
2911 expression_error(expr, "not a function %s",
2912 show_ident(sym->ident));
2913 /* do typechecking in arguments */
2914 FOR_EACH_PTR (arglist, arg) {
2915 evaluate_expression(arg);
2916 } END_FOR_EACH_PTR(arg);
2917 return NULL;
2920 examine_fn_arguments(ctype);
2921 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
2922 sym->op && sym->op->args) {
2923 if (!sym->op->args(expr))
2924 return NULL;
2925 } else {
2926 if (!evaluate_arguments(ctype, arglist))
2927 return NULL;
2928 args = expression_list_size(expr->args);
2929 fnargs = symbol_list_size(ctype->arguments);
2930 if (args < fnargs)
2931 expression_error(expr,
2932 "not enough arguments for function %s",
2933 show_ident(sym->ident));
2934 if (args > fnargs && !ctype->variadic)
2935 expression_error(expr,
2936 "too many arguments for function %s",
2937 show_ident(sym->ident));
2939 if (sym->type == SYM_NODE) {
2940 if (evaluate_symbol_call(expr))
2941 return expr->ctype;
2943 expr->ctype = ctype->ctype.base_type;
2944 return expr->ctype;
2947 static struct symbol *evaluate_offsetof(struct expression *expr)
2949 struct expression *e = expr->down;
2950 struct symbol *ctype = expr->in;
2951 int class;
2953 if (expr->op == '.') {
2954 struct symbol *field;
2955 int offset = 0;
2956 if (!ctype) {
2957 expression_error(expr, "expected structure or union");
2958 return NULL;
2960 examine_symbol_type(ctype);
2961 class = classify_type(ctype, &ctype);
2962 if (class != TYPE_COMPOUND) {
2963 expression_error(expr, "expected structure or union");
2964 return NULL;
2967 field = find_identifier(expr->ident, ctype->symbol_list, &offset);
2968 if (!field) {
2969 expression_error(expr, "unknown member");
2970 return NULL;
2972 ctype = field;
2973 expr->type = EXPR_VALUE;
2974 expr->flags = Int_const_expr;
2975 expr->value = offset;
2976 expr->taint = 0;
2977 expr->ctype = size_t_ctype;
2978 } else {
2979 if (!ctype) {
2980 expression_error(expr, "expected structure or union");
2981 return NULL;
2983 examine_symbol_type(ctype);
2984 class = classify_type(ctype, &ctype);
2985 if (class != (TYPE_COMPOUND | TYPE_PTR)) {
2986 expression_error(expr, "expected array");
2987 return NULL;
2989 ctype = ctype->ctype.base_type;
2990 if (!expr->index) {
2991 expr->type = EXPR_VALUE;
2992 expr->flags = Int_const_expr;
2993 expr->value = 0;
2994 expr->taint = 0;
2995 expr->ctype = size_t_ctype;
2996 } else {
2997 struct expression *idx = expr->index, *m;
2998 struct symbol *i_type = evaluate_expression(idx);
2999 int i_class = classify_type(i_type, &i_type);
3000 if (!is_int(i_class)) {
3001 expression_error(expr, "non-integer index");
3002 return NULL;
3004 unrestrict(idx, i_class, &i_type);
3005 idx = cast_to(idx, size_t_ctype);
3006 m = alloc_const_expression(expr->pos,
3007 bits_to_bytes(ctype->bit_size));
3008 m->ctype = size_t_ctype;
3009 m->flags = Int_const_expr;
3010 expr->type = EXPR_BINOP;
3011 expr->left = idx;
3012 expr->right = m;
3013 expr->op = '*';
3014 expr->ctype = size_t_ctype;
3015 expr->flags = m->flags & idx->flags & Int_const_expr;
3018 if (e) {
3019 struct expression *copy = __alloc_expression(0);
3020 *copy = *expr;
3021 if (e->type == EXPR_OFFSETOF)
3022 e->in = ctype;
3023 if (!evaluate_expression(e))
3024 return NULL;
3025 expr->type = EXPR_BINOP;
3026 expr->flags = e->flags & copy->flags & Int_const_expr;
3027 expr->op = '+';
3028 expr->ctype = size_t_ctype;
3029 expr->left = copy;
3030 expr->right = e;
3032 return size_t_ctype;
3035 struct symbol *evaluate_expression(struct expression *expr)
3037 if (!expr)
3038 return NULL;
3039 if (expr->ctype)
3040 return expr->ctype;
3042 switch (expr->type) {
3043 case EXPR_VALUE:
3044 case EXPR_FVALUE:
3045 expression_error(expr, "value expression without a type");
3046 return NULL;
3047 case EXPR_STRING:
3048 return evaluate_string(expr);
3049 case EXPR_SYMBOL:
3050 return evaluate_symbol_expression(expr);
3051 case EXPR_BINOP:
3052 if (!evaluate_expression(expr->left))
3053 return NULL;
3054 if (!evaluate_expression(expr->right))
3055 return NULL;
3056 return evaluate_binop(expr);
3057 case EXPR_LOGICAL:
3058 return evaluate_logical(expr);
3059 case EXPR_COMMA:
3060 evaluate_expression(expr->left);
3061 if (!evaluate_expression(expr->right))
3062 return NULL;
3063 return evaluate_comma(expr);
3064 case EXPR_COMPARE:
3065 if (!evaluate_expression(expr->left))
3066 return NULL;
3067 if (!evaluate_expression(expr->right))
3068 return NULL;
3069 return evaluate_compare(expr);
3070 case EXPR_ASSIGNMENT:
3071 if (!evaluate_expression(expr->left))
3072 return NULL;
3073 if (!evaluate_expression(expr->right))
3074 return NULL;
3075 return evaluate_assignment(expr);
3076 case EXPR_PREOP:
3077 if (!evaluate_expression(expr->unop))
3078 return NULL;
3079 return evaluate_preop(expr);
3080 case EXPR_POSTOP:
3081 if (!evaluate_expression(expr->unop))
3082 return NULL;
3083 return evaluate_postop(expr);
3084 case EXPR_CAST:
3085 case EXPR_FORCE_CAST:
3086 case EXPR_IMPLIED_CAST:
3087 return evaluate_cast(expr);
3088 case EXPR_SIZEOF:
3089 return evaluate_sizeof(expr);
3090 case EXPR_PTRSIZEOF:
3091 return evaluate_ptrsizeof(expr);
3092 case EXPR_ALIGNOF:
3093 return evaluate_alignof(expr);
3094 case EXPR_DEREF:
3095 return evaluate_member_dereference(expr);
3096 case EXPR_CALL:
3097 return evaluate_call(expr);
3098 case EXPR_SELECT:
3099 case EXPR_CONDITIONAL:
3100 return evaluate_conditional_expression(expr);
3101 case EXPR_STATEMENT:
3102 expr->ctype = evaluate_statement(expr->statement);
3103 return expr->ctype;
3105 case EXPR_LABEL:
3106 expr->ctype = &ptr_ctype;
3107 return &ptr_ctype;
3109 case EXPR_TYPE:
3110 /* Evaluate the type of the symbol .. */
3111 evaluate_symbol(expr->symbol);
3112 /* .. but the type of the _expression_ is a "type" */
3113 expr->ctype = &type_ctype;
3114 return &type_ctype;
3116 case EXPR_OFFSETOF:
3117 return evaluate_offsetof(expr);
3119 /* These can not exist as stand-alone expressions */
3120 case EXPR_INITIALIZER:
3121 case EXPR_IDENTIFIER:
3122 case EXPR_INDEX:
3123 case EXPR_POS:
3124 expression_error(expr, "internal front-end error: initializer in expression");
3125 return NULL;
3126 case EXPR_SLICE:
3127 expression_error(expr, "internal front-end error: SLICE re-evaluated");
3128 return NULL;
3130 return NULL;
3133 static void check_duplicates(struct symbol *sym)
3135 int declared = 0;
3136 struct symbol *next = sym;
3137 int initialized = sym->initializer != NULL;
3139 while ((next = next->same_symbol) != NULL) {
3140 const char *typediff;
3141 evaluate_symbol(next);
3142 if (initialized && next->initializer) {
3143 sparse_error(sym->pos, "symbol '%s' has multiple initializers (originally initialized at %s:%d)",
3144 show_ident(sym->ident),
3145 stream_name(next->pos.stream), next->pos.line);
3146 /* Only warn once */
3147 initialized = 0;
3149 declared++;
3150 typediff = type_difference(&sym->ctype, &next->ctype, 0, 0);
3151 if (typediff) {
3152 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
3153 show_ident(sym->ident),
3154 stream_name(next->pos.stream), next->pos.line, typediff);
3155 return;
3158 if (!declared) {
3159 unsigned long mod = sym->ctype.modifiers;
3160 if (mod & (MOD_STATIC | MOD_REGISTER))
3161 return;
3162 if (!(mod & MOD_TOPLEVEL))
3163 return;
3164 if (!Wdecl)
3165 return;
3166 if (sym->ident == &main_ident)
3167 return;
3168 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
3172 static struct symbol *evaluate_symbol(struct symbol *sym)
3174 struct symbol *base_type;
3176 if (!sym)
3177 return sym;
3178 if (sym->evaluated)
3179 return sym;
3180 sym->evaluated = 1;
3182 sym = examine_symbol_type(sym);
3183 base_type = get_base_type(sym);
3184 if (!base_type)
3185 return NULL;
3187 /* Evaluate the initializers */
3188 if (sym->initializer)
3189 evaluate_initializer(sym, &sym->initializer);
3191 /* And finally, evaluate the body of the symbol too */
3192 if (base_type->type == SYM_FN) {
3193 struct symbol *curr = current_fn;
3195 if (sym->definition && sym->definition != sym)
3196 return evaluate_symbol(sym->definition);
3198 current_fn = base_type;
3200 examine_fn_arguments(base_type);
3201 if (!base_type->stmt && base_type->inline_stmt)
3202 uninline(sym);
3203 if (base_type->stmt)
3204 evaluate_statement(base_type->stmt);
3206 current_fn = curr;
3209 return base_type;
3212 void evaluate_symbol_list(struct symbol_list *list)
3214 struct symbol *sym;
3216 FOR_EACH_PTR(list, sym) {
3217 evaluate_symbol(sym);
3218 check_duplicates(sym);
3219 } END_FOR_EACH_PTR(sym);
3222 static struct symbol *evaluate_return_expression(struct statement *stmt)
3224 struct expression *expr = stmt->expression;
3225 struct symbol *fntype;
3227 evaluate_expression(expr);
3228 fntype = current_fn->ctype.base_type;
3229 if (!fntype || fntype == &void_ctype) {
3230 if (expr && expr->ctype != &void_ctype)
3231 expression_error(expr, "return expression in %s function", fntype?"void":"typeless");
3232 if (expr && Wreturn_void)
3233 warning(stmt->pos, "returning void-valued expression");
3234 return NULL;
3237 if (!expr) {
3238 sparse_error(stmt->pos, "return with no return value");
3239 return NULL;
3241 if (!expr->ctype)
3242 return NULL;
3243 compatible_assignment_types(expr, fntype, &stmt->expression, "return expression");
3244 return NULL;
3247 static void evaluate_if_statement(struct statement *stmt)
3249 if (!stmt->if_conditional)
3250 return;
3252 evaluate_conditional(stmt->if_conditional, 0);
3253 evaluate_statement(stmt->if_true);
3254 evaluate_statement(stmt->if_false);
3257 static void evaluate_iterator(struct statement *stmt)
3259 evaluate_symbol_list(stmt->iterator_syms);
3260 evaluate_conditional(stmt->iterator_pre_condition, 1);
3261 evaluate_conditional(stmt->iterator_post_condition,1);
3262 evaluate_statement(stmt->iterator_pre_statement);
3263 evaluate_statement(stmt->iterator_statement);
3264 evaluate_statement(stmt->iterator_post_statement);
3267 static void verify_output_constraint(struct expression *expr, const char *constraint)
3269 switch (*constraint) {
3270 case '=': /* Assignment */
3271 case '+': /* Update */
3272 break;
3273 default:
3274 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
3278 static void verify_input_constraint(struct expression *expr, const char *constraint)
3280 switch (*constraint) {
3281 case '=': /* Assignment */
3282 case '+': /* Update */
3283 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
3287 static void evaluate_asm_statement(struct statement *stmt)
3289 struct expression *expr;
3290 struct symbol *sym;
3291 int state;
3293 expr = stmt->asm_string;
3294 if (!expr || expr->type != EXPR_STRING) {
3295 sparse_error(stmt->pos, "need constant string for inline asm");
3296 return;
3299 state = 0;
3300 FOR_EACH_PTR(stmt->asm_outputs, expr) {
3301 switch (state) {
3302 case 0: /* Identifier */
3303 state = 1;
3304 continue;
3306 case 1: /* Constraint */
3307 state = 2;
3308 if (!expr || expr->type != EXPR_STRING) {
3309 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
3310 *THIS_ADDRESS(expr) = NULL;
3311 continue;
3313 verify_output_constraint(expr, expr->string->data);
3314 continue;
3316 case 2: /* Expression */
3317 state = 0;
3318 if (!evaluate_expression(expr))
3319 return;
3320 if (!lvalue_expression(expr))
3321 warning(expr->pos, "asm output is not an lvalue");
3322 evaluate_assign_to(expr, expr->ctype);
3323 continue;
3325 } END_FOR_EACH_PTR(expr);
3327 state = 0;
3328 FOR_EACH_PTR(stmt->asm_inputs, expr) {
3329 switch (state) {
3330 case 0: /* Identifier */
3331 state = 1;
3332 continue;
3334 case 1: /* Constraint */
3335 state = 2;
3336 if (!expr || expr->type != EXPR_STRING) {
3337 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
3338 *THIS_ADDRESS(expr) = NULL;
3339 continue;
3341 verify_input_constraint(expr, expr->string->data);
3342 continue;
3344 case 2: /* Expression */
3345 state = 0;
3346 if (!evaluate_expression(expr))
3347 return;
3348 continue;
3350 } END_FOR_EACH_PTR(expr);
3352 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
3353 if (!expr) {
3354 sparse_error(stmt->pos, "bad asm clobbers");
3355 return;
3357 if (expr->type == EXPR_STRING)
3358 continue;
3359 expression_error(expr, "asm clobber is not a string");
3360 } END_FOR_EACH_PTR(expr);
3362 FOR_EACH_PTR(stmt->asm_labels, sym) {
3363 if (!sym || sym->type != SYM_LABEL) {
3364 sparse_error(stmt->pos, "bad asm label");
3365 return;
3367 } END_FOR_EACH_PTR(sym);
3370 static void evaluate_case_statement(struct statement *stmt)
3372 evaluate_expression(stmt->case_expression);
3373 evaluate_expression(stmt->case_to);
3374 evaluate_statement(stmt->case_statement);
3377 static void check_case_type(struct expression *switch_expr,
3378 struct expression *case_expr,
3379 struct expression **enumcase)
3381 struct symbol *switch_type, *case_type;
3382 int sclass, cclass;
3384 if (!case_expr)
3385 return;
3387 switch_type = switch_expr->ctype;
3388 case_type = evaluate_expression(case_expr);
3390 if (!switch_type || !case_type)
3391 goto Bad;
3392 if (enumcase) {
3393 if (*enumcase)
3394 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
3395 else if (is_enum_type(case_type))
3396 *enumcase = case_expr;
3399 sclass = classify_type(switch_type, &switch_type);
3400 cclass = classify_type(case_type, &case_type);
3402 /* both should be arithmetic */
3403 if (!(sclass & cclass & TYPE_NUM))
3404 goto Bad;
3406 /* neither should be floating */
3407 if ((sclass | cclass) & TYPE_FLOAT)
3408 goto Bad;
3410 /* if neither is restricted, we are OK */
3411 if (!((sclass | cclass) & TYPE_RESTRICT))
3412 return;
3414 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
3415 cclass, sclass, case_type, switch_type)) {
3416 unrestrict(case_expr, cclass, &case_type);
3417 unrestrict(switch_expr, sclass, &switch_type);
3419 return;
3421 Bad:
3422 expression_error(case_expr, "incompatible types for 'case' statement");
3425 static void evaluate_switch_statement(struct statement *stmt)
3427 struct symbol *sym;
3428 struct expression *enumcase = NULL;
3429 struct expression **enumcase_holder = &enumcase;
3430 struct expression *sel = stmt->switch_expression;
3432 evaluate_expression(sel);
3433 evaluate_statement(stmt->switch_statement);
3434 if (!sel)
3435 return;
3436 if (sel->ctype && is_enum_type(sel->ctype))
3437 enumcase_holder = NULL; /* Only check cases against switch */
3439 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
3440 struct statement *case_stmt = sym->stmt;
3441 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
3442 check_case_type(sel, case_stmt->case_to, enumcase_holder);
3443 } END_FOR_EACH_PTR(sym);
3446 static void evaluate_goto_statement(struct statement *stmt)
3448 struct symbol *label = stmt->goto_label;
3450 if (label && !label->stmt && !lookup_keyword(label->ident, NS_KEYWORD))
3451 sparse_error(stmt->pos, "label '%s' was not declared", show_ident(label->ident));
3453 evaluate_expression(stmt->goto_expression);
3456 struct symbol *evaluate_statement(struct statement *stmt)
3458 if (!stmt)
3459 return NULL;
3461 switch (stmt->type) {
3462 case STMT_DECLARATION: {
3463 struct symbol *s;
3464 FOR_EACH_PTR(stmt->declaration, s) {
3465 evaluate_symbol(s);
3466 } END_FOR_EACH_PTR(s);
3467 return NULL;
3470 case STMT_RETURN:
3471 return evaluate_return_expression(stmt);
3473 case STMT_EXPRESSION:
3474 if (!evaluate_expression(stmt->expression))
3475 return NULL;
3476 if (stmt->expression->ctype == &null_ctype)
3477 stmt->expression = cast_to(stmt->expression, &ptr_ctype);
3478 return degenerate(stmt->expression);
3480 case STMT_COMPOUND: {
3481 struct statement *s;
3482 struct symbol *type = NULL;
3484 /* Evaluate the return symbol in the compound statement */
3485 evaluate_symbol(stmt->ret);
3488 * Then, evaluate each statement, making the type of the
3489 * compound statement be the type of the last statement
3491 type = evaluate_statement(stmt->args);
3492 FOR_EACH_PTR(stmt->stmts, s) {
3493 type = evaluate_statement(s);
3494 } END_FOR_EACH_PTR(s);
3495 if (!type)
3496 type = &void_ctype;
3497 return type;
3499 case STMT_IF:
3500 evaluate_if_statement(stmt);
3501 return NULL;
3502 case STMT_ITERATOR:
3503 evaluate_iterator(stmt);
3504 return NULL;
3505 case STMT_SWITCH:
3506 evaluate_switch_statement(stmt);
3507 return NULL;
3508 case STMT_CASE:
3509 evaluate_case_statement(stmt);
3510 return NULL;
3511 case STMT_LABEL:
3512 return evaluate_statement(stmt->label_statement);
3513 case STMT_GOTO:
3514 evaluate_goto_statement(stmt);
3515 return NULL;
3516 case STMT_NONE:
3517 break;
3518 case STMT_ASM:
3519 evaluate_asm_statement(stmt);
3520 return NULL;
3521 case STMT_CONTEXT:
3522 evaluate_expression(stmt->expression);
3523 return NULL;
3524 case STMT_RANGE:
3525 evaluate_expression(stmt->range_expression);
3526 evaluate_expression(stmt->range_low);
3527 evaluate_expression(stmt->range_high);
3528 return NULL;
3530 return NULL;