math: array parameters can be NULL
[smatch.git] / evaluate.c
blob4facb1fd9b05aa6b91266d403e1984c9a819adbe
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 "evaluate.h"
38 #include "lib.h"
39 #include "allocate.h"
40 #include "parse.h"
41 #include "token.h"
42 #include "symbol.h"
43 #include "target.h"
44 #include "expression.h"
46 struct symbol *current_fn;
48 struct ident bad_address_space = { .len = 6, .name = "bad AS", };
50 static struct symbol *degenerate(struct expression *expr);
51 static struct symbol *evaluate_symbol(struct symbol *sym);
53 static inline int valid_expr_type(struct expression *expr)
55 return expr && valid_type(expr->ctype);
58 static inline int valid_subexpr_type(struct expression *expr)
60 return valid_expr_type(expr->left)
61 && valid_expr_type(expr->right);
64 static struct symbol *evaluate_symbol_expression(struct expression *expr)
66 struct expression *addr;
67 struct symbol *sym = expr->symbol;
68 struct symbol *base_type;
70 if (!sym) {
71 expression_error(expr, "undefined identifier '%s'", show_ident(expr->symbol_name));
72 return NULL;
75 examine_symbol_type(sym);
77 base_type = get_base_type(sym);
78 if (!base_type) {
79 expression_error(expr, "identifier '%s' has no type", show_ident(expr->symbol_name));
80 return NULL;
83 addr = alloc_expression(expr->pos, EXPR_SYMBOL);
84 addr->symbol = sym;
85 addr->symbol_name = expr->symbol_name;
86 addr->ctype = &lazy_ptr_ctype; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
87 addr->flags = expr->flags;
88 expr->type = EXPR_PREOP;
89 expr->op = '*';
90 expr->unop = addr;
91 expr->flags = CEF_NONE;
93 /* The type of a symbol is the symbol itself! */
94 expr->ctype = sym;
95 return sym;
98 static struct symbol *evaluate_string(struct expression *expr)
100 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
101 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
102 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
103 struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
104 unsigned int length = expr->string->length;
106 sym->array_size = alloc_const_expression(expr->pos, length);
107 sym->bit_size = bytes_to_bits(length);
108 sym->ctype.alignment = 1;
109 sym->string = 1;
110 sym->ctype.modifiers = MOD_STATIC;
111 sym->ctype.base_type = array;
112 sym->initializer = initstr;
113 sym->examined = 1;
114 sym->evaluated = 1;
116 initstr->ctype = sym;
117 initstr->string = expr->string;
119 array->array_size = sym->array_size;
120 array->bit_size = bytes_to_bits(length);
121 array->ctype.alignment = 1;
122 array->ctype.modifiers = MOD_STATIC;
123 array->ctype.base_type = &char_ctype;
124 array->examined = 1;
125 array->evaluated = 1;
127 addr->symbol = sym;
128 addr->ctype = &lazy_ptr_ctype;
129 addr->flags = CEF_ADDR;
131 expr->type = EXPR_PREOP;
132 expr->op = '*';
133 expr->unop = addr;
134 expr->ctype = sym;
135 return sym;
138 /* type has come from classify_type and is an integer type */
139 static inline struct symbol *integer_promotion(struct symbol *type)
141 unsigned long mod = type->ctype.modifiers;
142 int width = type->bit_size;
145 * Bitfields always promote to the base type,
146 * even if the bitfield might be bigger than
147 * an "int".
149 if (type->type == SYM_BITFIELD) {
150 type = type->ctype.base_type;
152 mod = type->ctype.modifiers;
153 if (width < bits_in_int)
154 return &int_ctype;
156 /* If char/short has as many bits as int, it still gets "promoted" */
157 if (type->rank < 0) {
158 if (mod & MOD_UNSIGNED)
159 return &uint_ctype;
160 return &int_ctype;
162 return type;
166 * integer part of usual arithmetic conversions:
167 * integer promotions are applied
168 * if left and right are identical, we are done
169 * if signedness is the same, convert one with lower rank
170 * unless unsigned argument has rank lower than signed one, convert the
171 * signed one.
172 * if signed argument is bigger than unsigned one, convert the unsigned.
173 * otherwise, convert signed.
175 * Leaving aside the integer promotions, that is equivalent to
176 * if identical, don't convert
177 * if left is bigger than right, convert right
178 * if right is bigger than left, convert right
179 * otherwise, if signedness is the same, convert one with lower rank
180 * otherwise convert the signed one.
182 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
184 unsigned long lmod, rmod;
186 left = integer_promotion(left);
187 right = integer_promotion(right);
189 if (left == right)
190 goto left;
192 if (left->bit_size > right->bit_size)
193 goto left;
195 if (right->bit_size > left->bit_size)
196 goto right;
198 lmod = left->ctype.modifiers;
199 rmod = right->ctype.modifiers;
200 if ((lmod ^ rmod) & MOD_UNSIGNED) {
201 if (lmod & MOD_UNSIGNED)
202 goto left;
203 } else if (left->rank > right->rank)
204 goto left;
205 right:
206 left = right;
207 left:
208 return left;
211 static int same_cast_type(struct symbol *orig, struct symbol *new)
213 return orig->bit_size == new->bit_size &&
214 orig->bit_offset == new->bit_offset;
217 static struct symbol *base_type(struct symbol *node, unsigned long *modp, struct ident **asp)
219 unsigned long mod = 0;
220 struct ident *as = NULL;
222 while (node) {
223 mod |= node->ctype.modifiers;
224 combine_address_space(node->pos, &as, node->ctype.as);
225 if (node->type == SYM_NODE) {
226 node = node->ctype.base_type;
227 continue;
229 break;
231 *modp = mod & ~MOD_IGNORE;
232 *asp = as;
233 return node;
236 static int is_same_type(struct expression *expr, struct symbol *new)
238 struct symbol *old = expr->ctype;
239 unsigned long oldmod, newmod;
240 struct ident *oldas, *newas;
242 old = base_type(old, &oldmod, &oldas);
243 new = base_type(new, &newmod, &newas);
245 /* Same base type, same address space? */
246 if (old == new && oldas == newas) {
247 unsigned long difmod;
249 /* Check the modifier bits. */
250 difmod = (oldmod ^ newmod) & ~MOD_NOCAST;
252 /* Exact same type? */
253 if (!difmod)
254 return 1;
257 * Not the same type, but differs only in "const".
258 * Don't warn about MOD_NOCAST.
260 if (difmod == MOD_CONST)
261 return 0;
263 if ((oldmod | newmod) & MOD_NOCAST) {
264 const char *tofrom = "to/from";
265 if (!(newmod & MOD_NOCAST))
266 tofrom = "from";
267 if (!(oldmod & MOD_NOCAST))
268 tofrom = "to";
269 warning(expr->pos, "implicit cast %s nocast type", tofrom);
271 return 0;
274 static void
275 warn_for_different_enum_types (struct position pos,
276 struct symbol *typea,
277 struct symbol *typeb)
279 if (!Wenum_mismatch)
280 return;
281 if (typea->type == SYM_NODE)
282 typea = typea->ctype.base_type;
283 if (typeb->type == SYM_NODE)
284 typeb = typeb->ctype.base_type;
286 if (typea == typeb)
287 return;
289 if (typea->type == SYM_ENUM && typeb->type == SYM_ENUM) {
290 warning(pos, "mixing different enum types:");
291 info(pos, " %s", show_typename(typea));
292 info(pos, " %s", show_typename(typeb));
296 static int cast_flags(struct expression *expr, struct expression *target);
297 static struct symbol *cast_to_bool(struct expression *expr);
300 * This gets called for implicit casts in assignments and
301 * integer promotion. We often want to try to move the
302 * cast down, because the ops involved may have been
303 * implicitly cast up, and we can get rid of the casts
304 * early.
306 static struct expression * cast_to(struct expression *old, struct symbol *type)
308 struct expression *expr;
310 warn_for_different_enum_types (old->pos, old->ctype, type);
312 if (old->ctype != &null_ctype && is_same_type(old, type))
313 return old;
316 * See if we can simplify the op. Move the cast down.
318 switch (old->type) {
319 case EXPR_PREOP:
320 if (old->ctype->bit_size < type->bit_size)
321 break;
322 if (old->op == '~') {
323 old->ctype = type;
324 old->unop = cast_to(old->unop, type);
325 return old;
327 break;
329 case EXPR_IMPLIED_CAST:
330 warn_for_different_enum_types(old->pos, old->ctype, type);
332 if (old->ctype->bit_size >= type->bit_size) {
333 struct expression *orig = old->cast_expression;
334 if (same_cast_type(orig->ctype, type))
335 return orig;
336 if (old->ctype->bit_offset == type->bit_offset) {
337 old->ctype = type;
338 old->cast_type = type;
339 return old;
342 break;
344 default:
345 /* nothing */;
348 expr = alloc_expression(old->pos, EXPR_IMPLIED_CAST);
349 expr->ctype = type;
350 expr->cast_type = type;
351 expr->cast_expression = old;
352 expr->flags = cast_flags(expr, old);
354 if (is_bool_type(type))
355 cast_to_bool(expr);
357 return expr;
360 enum {
361 TYPE_NUM = 1,
362 TYPE_BITFIELD = 2,
363 TYPE_RESTRICT = 4,
364 TYPE_FLOAT = 8,
365 TYPE_PTR = 16,
366 TYPE_COMPOUND = 32,
367 TYPE_FOULED = 64,
368 TYPE_FN = 128,
371 static inline int classify_type(struct symbol *type, struct symbol **base)
373 static int type_class[SYM_BAD + 1] = {
374 [SYM_PTR] = TYPE_PTR,
375 [SYM_FN] = TYPE_PTR | TYPE_FN,
376 [SYM_ARRAY] = TYPE_PTR | TYPE_COMPOUND,
377 [SYM_STRUCT] = TYPE_COMPOUND,
378 [SYM_UNION] = TYPE_COMPOUND,
379 [SYM_BITFIELD] = TYPE_NUM | TYPE_BITFIELD,
380 [SYM_RESTRICT] = TYPE_NUM | TYPE_RESTRICT,
381 [SYM_FOULED] = TYPE_NUM | TYPE_RESTRICT | TYPE_FOULED,
383 if (type->type == SYM_NODE)
384 type = type->ctype.base_type;
385 if (type->type == SYM_TYPEOF) {
386 type = examine_symbol_type(type);
387 if (type->type == SYM_NODE)
388 type = type->ctype.base_type;
390 if (type->type == SYM_ENUM)
391 type = type->ctype.base_type;
392 *base = type;
393 if (type->type == SYM_BASETYPE) {
394 if (type->ctype.base_type == &int_type)
395 return TYPE_NUM;
396 if (type->ctype.base_type == &fp_type)
397 return TYPE_NUM | TYPE_FLOAT;
399 return type_class[type->type];
402 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
404 static inline int is_string_type(struct symbol *type)
406 if (type->type == SYM_NODE)
407 type = type->ctype.base_type;
408 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
411 static struct symbol *bad_expr_type(struct expression *expr)
413 switch (expr->type) {
414 case EXPR_BINOP:
415 case EXPR_COMPARE:
416 if (!valid_subexpr_type(expr))
417 break;
418 sparse_error(expr->pos, "incompatible types for operation (%s):", show_special(expr->op));
419 info(expr->pos, " %s", show_typename(expr->left->ctype));
420 info(expr->pos, " %s", show_typename(expr->right->ctype));
421 break;
422 case EXPR_PREOP:
423 case EXPR_POSTOP:
424 if (!valid_expr_type(expr->unop))
425 break;
426 sparse_error(expr->pos, "incompatible type for operation (%s):", show_special(expr->op));
427 info(expr->pos, " %s", show_typename(expr->unop->ctype));
428 break;
429 default:
430 break;
433 expr->flags = CEF_NONE;
434 return expr->ctype = &bad_ctype;
437 static int restricted_value(struct expression *v, struct symbol *type)
439 if (v->type != EXPR_VALUE)
440 return 1;
441 if (v->value != 0)
442 return 1;
443 return 0;
446 static int restricted_binop(int op, struct symbol *type)
448 switch (op) {
449 case '&':
450 case '=':
451 case SPECIAL_AND_ASSIGN:
452 case SPECIAL_OR_ASSIGN:
453 case SPECIAL_XOR_ASSIGN:
454 return 1; /* unfoul */
455 case '|':
456 case '^':
457 case '?':
458 return 2; /* keep fouled */
459 case SPECIAL_EQUAL:
460 case SPECIAL_NOTEQUAL:
461 return 3; /* warn if fouled */
462 default:
463 return 0; /* warn */
467 static int restricted_unop(int op, struct symbol **type)
469 if (op == '~') {
470 if ((*type)->bit_size < bits_in_int)
471 *type = befoul(*type);
472 return 0;
473 } if (op == '+')
474 return 0;
475 return 1;
478 /* type should be SYM_FOULED */
479 static inline struct symbol *unfoul(struct symbol *type)
481 return type->ctype.base_type;
484 static struct symbol *restricted_binop_type(int op,
485 struct expression *left,
486 struct expression *right,
487 int lclass, int rclass,
488 struct symbol *ltype,
489 struct symbol *rtype)
491 struct symbol *ctype = NULL;
492 if (lclass & TYPE_RESTRICT) {
493 if (rclass & TYPE_RESTRICT) {
494 if (ltype == rtype) {
495 ctype = ltype;
496 } else if (lclass & TYPE_FOULED) {
497 if (unfoul(ltype) == rtype)
498 ctype = ltype;
499 } else if (rclass & TYPE_FOULED) {
500 if (unfoul(rtype) == ltype)
501 ctype = rtype;
503 } else {
504 if (!restricted_value(right, ltype))
505 ctype = ltype;
507 } else if (!restricted_value(left, rtype))
508 ctype = rtype;
510 if (ctype) {
511 switch (restricted_binop(op, ctype)) {
512 case 1:
513 if ((lclass ^ rclass) & TYPE_FOULED)
514 ctype = unfoul(ctype);
515 break;
516 case 3:
517 if (!(lclass & rclass & TYPE_FOULED))
518 break;
519 case 0:
520 ctype = NULL;
521 default:
522 break;
526 return ctype;
529 static inline void unrestrict(struct expression *expr,
530 int class, struct symbol **ctype)
532 if (class & TYPE_RESTRICT) {
533 if (class & TYPE_FOULED)
534 *ctype = unfoul(*ctype);
535 warning(expr->pos, "%s degrades to integer",
536 show_typename(*ctype));
537 *ctype = (*ctype)->ctype.base_type; /* get to arithmetic type */
541 static struct symbol *usual_conversions(int op,
542 struct expression *left,
543 struct expression *right,
544 int lclass, int rclass,
545 struct symbol *ltype,
546 struct symbol *rtype)
548 struct symbol *ctype;
550 warn_for_different_enum_types(right->pos, left->ctype, right->ctype);
552 if ((lclass | rclass) & TYPE_RESTRICT)
553 goto Restr;
555 Normal:
556 if (!(lclass & TYPE_FLOAT)) {
557 if (!(rclass & TYPE_FLOAT))
558 return bigger_int_type(ltype, rtype);
559 else
560 return rtype;
561 } else if (rclass & TYPE_FLOAT) {
562 if (rtype->rank > ltype->rank)
563 return rtype;
564 else
565 return ltype;
566 } else
567 return ltype;
569 Restr:
570 ctype = restricted_binop_type(op, left, right,
571 lclass, rclass, ltype, rtype);
572 if (ctype)
573 return ctype;
575 unrestrict(left, lclass, &ltype);
576 unrestrict(right, rclass, &rtype);
578 goto Normal;
581 static inline int lvalue_expression(struct expression *expr)
583 return expr->type == EXPR_PREOP && expr->op == '*';
586 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *itype)
588 struct expression *index = expr->right;
589 struct symbol *ctype, *base;
590 int multiply;
592 classify_type(degenerate(expr->left), &ctype);
593 base = examine_pointer_target(ctype);
596 * An address constant +/- an integer constant expression
597 * yields an address constant again [6.6(7)].
599 if ((expr->left->flags & CEF_ADDR) && (expr->right->flags & CEF_ICE))
600 expr->flags = CEF_ADDR;
602 if (!base) {
603 expression_error(expr, "missing type information");
604 return NULL;
606 if (is_function(base)) {
607 expression_error(expr, "arithmetics on pointers to functions");
608 return NULL;
611 /* Get the size of whatever the pointer points to */
612 multiply = is_void_type(base) ? 1 : bits_to_bytes(base->bit_size);
614 if (ctype == &null_ctype)
615 ctype = &ptr_ctype;
616 expr->ctype = ctype;
618 if (multiply == 1 && itype->bit_size >= bits_in_pointer)
619 return ctype;
621 if (index->type == EXPR_VALUE) {
622 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
623 unsigned long long v = index->value, mask;
624 mask = 1ULL << (itype->bit_size - 1);
625 if (v & mask)
626 v |= -mask;
627 else
628 v &= mask - 1;
629 v *= multiply;
630 mask = 1ULL << (bits_in_pointer - 1);
631 v &= mask | (mask - 1);
632 val->value = v;
633 val->ctype = ssize_t_ctype;
634 expr->right = val;
635 return ctype;
638 if (itype->bit_size < bits_in_pointer)
639 index = cast_to(index, ssize_t_ctype);
641 if (multiply > 1) {
642 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
643 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
645 val->ctype = ssize_t_ctype;
646 val->value = multiply;
648 mul->op = '*';
649 mul->ctype = ssize_t_ctype;
650 mul->left = index;
651 mul->right = val;
652 index = mul;
655 expr->right = index;
656 return ctype;
659 static void examine_fn_arguments(struct symbol *fn);
661 #define MOD_IGN (MOD_QUALIFIER | MOD_FUN_ATTR)
663 const char *type_difference(struct ctype *c1, struct ctype *c2,
664 unsigned long mod1, unsigned long mod2)
666 struct ident *as1 = c1->as, *as2 = c2->as;
667 struct symbol *t1 = c1->base_type;
668 struct symbol *t2 = c2->base_type;
669 int move1 = 1, move2 = 1;
670 mod1 |= c1->modifiers;
671 mod2 |= c2->modifiers;
672 for (;;) {
673 unsigned long diff;
674 int type;
675 struct symbol *base1 = t1->ctype.base_type;
676 struct symbol *base2 = t2->ctype.base_type;
679 * FIXME! Collect alignment and context too here!
681 if (move1) {
682 if (t1 && t1->type != SYM_PTR) {
683 mod1 |= t1->ctype.modifiers;
684 combine_address_space(t1->pos, &as1, t1->ctype.as);
686 move1 = 0;
689 if (move2) {
690 if (t2 && t2->type != SYM_PTR) {
691 mod2 |= t2->ctype.modifiers;
692 combine_address_space(t2->pos, &as2, t2->ctype.as);
694 move2 = 0;
697 if (t1 == t2)
698 break;
699 if (!t1 || !t2)
700 return "different types";
702 if (t1->type == SYM_NODE || t1->type == SYM_ENUM) {
703 t1 = base1;
704 move1 = 1;
705 if (!t1)
706 return "bad types";
707 continue;
710 if (t2->type == SYM_NODE || t2->type == SYM_ENUM) {
711 t2 = base2;
712 move2 = 1;
713 if (!t2)
714 return "bad types";
715 continue;
718 move1 = move2 = 1;
719 type = t1->type;
720 if (type != t2->type)
721 return "different base types";
723 switch (type) {
724 default:
725 sparse_error(t1->pos,
726 "internal error: bad type in derived(%d)",
727 type);
728 return "bad types";
729 case SYM_RESTRICT:
730 return "different base types";
731 case SYM_UNION:
732 case SYM_STRUCT:
733 /* allow definition of incomplete structs and unions */
734 if (t1->ident == t2->ident)
735 return NULL;
736 return "different base types";
737 case SYM_ARRAY:
738 /* XXX: we ought to compare sizes */
739 break;
740 case SYM_PTR:
741 if (as1 != as2)
742 return "different address spaces";
743 /* MOD_SPECIFIER is due to idiocy in parse.c */
744 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SPECIFIER)
745 return "different modifiers";
746 /* we could be lazier here */
747 base1 = examine_pointer_target(t1);
748 base2 = examine_pointer_target(t2);
749 mod1 = t1->ctype.modifiers;
750 as1 = t1->ctype.as;
751 mod2 = t2->ctype.modifiers;
752 as2 = t2->ctype.as;
753 break;
754 case SYM_FN: {
755 struct symbol *arg1, *arg2;
756 int i;
758 if (as1 != as2)
759 return "different address spaces";
760 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
761 return "different modifiers";
762 mod1 = t1->ctype.modifiers;
763 as1 = t1->ctype.as;
764 mod2 = t2->ctype.modifiers;
765 as2 = t2->ctype.as;
767 if (t1->variadic != t2->variadic)
768 return "incompatible variadic arguments";
769 examine_fn_arguments(t1);
770 examine_fn_arguments(t2);
771 PREPARE_PTR_LIST(t1->arguments, arg1);
772 PREPARE_PTR_LIST(t2->arguments, arg2);
773 i = 1;
774 for (;;) {
775 const char *diffstr;
776 if (!arg1 && !arg2)
777 break;
778 if (!arg1 || !arg2)
779 return "different argument counts";
780 diffstr = type_difference(&arg1->ctype,
781 &arg2->ctype,
782 MOD_IGN, MOD_IGN);
783 if (diffstr) {
784 static char argdiff[80];
785 sprintf(argdiff, "incompatible argument %d (%s)", i, diffstr);
786 return argdiff;
788 NEXT_PTR_LIST(arg1);
789 NEXT_PTR_LIST(arg2);
790 i++;
792 FINISH_PTR_LIST(arg2);
793 FINISH_PTR_LIST(arg1);
794 break;
796 case SYM_BASETYPE:
797 if (as1 != as2)
798 return "different address spaces";
799 if (base1 != base2)
800 return "different base types";
801 if (t1->rank != t2->rank)
802 return "different type sizes";
803 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
804 if (!diff)
805 return NULL;
806 else if (diff & ~MOD_SIGNEDNESS)
807 return "different modifiers";
808 else
809 return "different signedness";
811 t1 = base1;
812 t2 = base2;
814 if (as1 != as2)
815 return "different address spaces";
816 if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
817 return "different modifiers";
818 return NULL;
821 static void bad_null(struct expression *expr)
823 if (Wnon_pointer_null)
824 warning(expr->pos, "Using plain integer as NULL pointer");
827 static unsigned long target_qualifiers(struct symbol *type)
829 unsigned long mod = type->ctype.modifiers & MOD_IGN;
830 if (type->ctype.base_type && type->ctype.base_type->type == SYM_ARRAY)
831 mod = 0;
832 return mod;
835 static struct symbol *evaluate_ptr_sub(struct expression *expr)
837 const char *typediff;
838 struct symbol *ltype, *rtype;
839 struct expression *l = expr->left;
840 struct expression *r = expr->right;
841 struct symbol *lbase;
843 classify_type(degenerate(l), &ltype);
844 classify_type(degenerate(r), &rtype);
846 lbase = examine_pointer_target(ltype);
847 examine_pointer_target(rtype);
848 typediff = type_difference(&ltype->ctype, &rtype->ctype,
849 target_qualifiers(rtype),
850 target_qualifiers(ltype));
851 if (typediff)
852 expression_error(expr, "subtraction of different types can't work (%s)", typediff);
854 if (is_function(lbase)) {
855 expression_error(expr, "subtraction of functions? Share your drugs");
856 return NULL;
859 expr->ctype = ssize_t_ctype;
860 if (lbase->bit_size > bits_in_char) {
861 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
862 struct expression *div = expr;
863 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
864 unsigned long value = bits_to_bytes(lbase->bit_size);
866 val->ctype = size_t_ctype;
867 val->value = value;
869 if (value & (value-1)) {
870 if (Wptr_subtraction_blows) {
871 warning(expr->pos, "potentially expensive pointer subtraction");
872 info(expr->pos, " '%s' has a non-power-of-2 size: %lu", show_typename(lbase), value);
876 sub->op = '-';
877 sub->ctype = ssize_t_ctype;
878 sub->left = l;
879 sub->right = r;
881 div->op = '/';
882 div->left = sub;
883 div->right = val;
886 return ssize_t_ctype;
889 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
891 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
893 struct symbol *ctype;
895 if (!expr)
896 return NULL;
898 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
899 warning(expr->pos, "assignment expression in conditional");
901 ctype = evaluate_expression(expr);
902 if (!valid_type(ctype))
903 return NULL;
904 if (is_safe_type(ctype))
905 warning(expr->pos, "testing a 'safe expression'");
906 if (is_func_type(ctype)) {
907 if (Waddress)
908 warning(expr->pos, "the address of %s will always evaluate as true", "a function");
909 } else if (is_array_type(ctype)) {
910 if (Waddress)
911 warning(expr->pos, "the address of %s will always evaluate as true", "an array");
912 } else if (!is_scalar_type(ctype)) {
913 sparse_error(expr->pos, "non-scalar type in conditional:");
914 info(expr->pos, " %s", show_typename(ctype));
915 return NULL;
918 ctype = degenerate(expr);
919 return ctype;
922 static struct symbol *evaluate_logical(struct expression *expr)
924 if (!evaluate_conditional(expr->left, 0))
925 return NULL;
926 if (!evaluate_conditional(expr->right, 0))
927 return NULL;
929 /* the result is int [6.5.13(3), 6.5.14(3)] */
930 expr->ctype = &int_ctype;
931 expr->flags = expr->left->flags & expr->right->flags;
932 expr->flags &= ~(CEF_CONST_MASK | CEF_ADDR);
933 return &int_ctype;
936 static struct symbol *evaluate_binop(struct expression *expr)
938 struct symbol *ltype, *rtype, *ctype;
939 int lclass = classify_type(expr->left->ctype, &ltype);
940 int rclass = classify_type(expr->right->ctype, &rtype);
941 int op = expr->op;
943 /* number op number */
944 if (lclass & rclass & TYPE_NUM) {
945 expr->flags = expr->left->flags & expr->right->flags;
946 expr->flags &= ~CEF_CONST_MASK;
948 if ((lclass | rclass) & TYPE_FLOAT) {
949 switch (op) {
950 case '+': case '-': case '*': case '/':
951 break;
952 default:
953 return bad_expr_type(expr);
957 if (op == SPECIAL_LEFTSHIFT || op == SPECIAL_RIGHTSHIFT) {
958 // shifts do integer promotions, but that's it.
959 unrestrict(expr->left, lclass, &ltype);
960 unrestrict(expr->right, rclass, &rtype);
961 ctype = ltype = integer_promotion(ltype);
962 rtype = integer_promotion(rtype);
963 } else {
964 // The rest do usual conversions
965 const unsigned left_not = expr->left->type == EXPR_PREOP
966 && expr->left->op == '!';
967 const unsigned right_not = expr->right->type == EXPR_PREOP
968 && expr->right->op == '!';
969 if ((op == '&' || op == '|') && (left_not || right_not))
970 warning(expr->pos, "dubious: %sx %c %sy",
971 left_not ? "!" : "",
973 right_not ? "!" : "");
975 ltype = usual_conversions(op, expr->left, expr->right,
976 lclass, rclass, ltype, rtype);
977 ctype = rtype = ltype;
980 expr->left = cast_to(expr->left, ltype);
981 expr->right = cast_to(expr->right, rtype);
982 expr->ctype = ctype;
983 return ctype;
986 /* pointer (+|-) integer */
987 if (lclass & TYPE_PTR && is_int(rclass) && (op == '+' || op == '-')) {
988 unrestrict(expr->right, rclass, &rtype);
989 return evaluate_ptr_add(expr, rtype);
992 /* integer + pointer */
993 if (rclass & TYPE_PTR && is_int(lclass) && op == '+') {
994 struct expression *index = expr->left;
995 unrestrict(index, lclass, &ltype);
996 expr->left = expr->right;
997 expr->right = index;
998 return evaluate_ptr_add(expr, ltype);
1001 /* pointer - pointer */
1002 if (lclass & rclass & TYPE_PTR && expr->op == '-')
1003 return evaluate_ptr_sub(expr);
1005 return bad_expr_type(expr);
1008 static struct symbol *evaluate_comma(struct expression *expr)
1010 expr->ctype = degenerate(expr->right);
1011 if (expr->ctype == &null_ctype)
1012 expr->ctype = &ptr_ctype;
1013 expr->flags &= expr->left->flags & expr->right->flags;
1014 return expr->ctype;
1017 static int modify_for_unsigned(int op)
1019 if (op == '<')
1020 op = SPECIAL_UNSIGNED_LT;
1021 else if (op == '>')
1022 op = SPECIAL_UNSIGNED_GT;
1023 else if (op == SPECIAL_LTE)
1024 op = SPECIAL_UNSIGNED_LTE;
1025 else if (op == SPECIAL_GTE)
1026 op = SPECIAL_UNSIGNED_GTE;
1027 return op;
1030 enum null_constant_type {
1031 NON_NULL,
1032 NULL_PTR,
1033 NULL_ZERO,
1036 static inline int is_null_pointer_constant(struct expression *e)
1038 if (e->ctype == &null_ctype)
1039 return NULL_PTR;
1040 if (!(e->flags & CEF_ICE))
1041 return NON_NULL;
1042 return is_zero_constant(e) ? NULL_ZERO : NON_NULL;
1045 static struct symbol *evaluate_compare(struct expression *expr)
1047 struct expression *left = expr->left, *right = expr->right;
1048 struct symbol *ltype, *rtype, *lbase, *rbase;
1049 int lclass = classify_type(degenerate(left), &ltype);
1050 int rclass = classify_type(degenerate(right), &rtype);
1051 struct symbol *ctype;
1052 const char *typediff;
1054 /* Type types? */
1055 if (is_type_type(ltype) && is_type_type(rtype)) {
1057 * __builtin_types_compatible_p() yields an integer
1058 * constant expression
1060 expr->flags = CEF_SET_ICE;
1061 goto OK;
1064 if (is_safe_type(left->ctype) || is_safe_type(right->ctype))
1065 warning(expr->pos, "testing a 'safe expression'");
1067 expr->flags = left->flags & right->flags & ~CEF_CONST_MASK & ~CEF_ADDR;
1069 /* number on number */
1070 if (lclass & rclass & TYPE_NUM) {
1071 ctype = usual_conversions(expr->op, expr->left, expr->right,
1072 lclass, rclass, ltype, rtype);
1073 expr->left = cast_to(expr->left, ctype);
1074 expr->right = cast_to(expr->right, ctype);
1075 if (ctype->ctype.modifiers & MOD_UNSIGNED)
1076 expr->op = modify_for_unsigned(expr->op);
1077 goto OK;
1080 /* at least one must be a pointer */
1081 if (!((lclass | rclass) & TYPE_PTR))
1082 return bad_expr_type(expr);
1084 /* equality comparisons can be with null pointer constants */
1085 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1086 int is_null1 = is_null_pointer_constant(left);
1087 int is_null2 = is_null_pointer_constant(right);
1088 if (is_null1 == NULL_ZERO)
1089 bad_null(left);
1090 if (is_null2 == NULL_ZERO)
1091 bad_null(right);
1092 if (is_null1 && is_null2) {
1093 int positive = expr->op == SPECIAL_EQUAL;
1094 expr->type = EXPR_VALUE;
1095 expr->value = positive;
1096 goto OK;
1098 if (is_null1 && (rclass & TYPE_PTR)) {
1099 expr->left = cast_to(left, rtype);
1100 goto OK;
1102 if (is_null2 && (lclass & TYPE_PTR)) {
1103 expr->right = cast_to(right, ltype);
1104 goto OK;
1107 /* both should be pointers */
1108 if (!(lclass & rclass & TYPE_PTR))
1109 return bad_expr_type(expr);
1110 expr->op = modify_for_unsigned(expr->op);
1112 lbase = examine_pointer_target(ltype);
1113 rbase = examine_pointer_target(rtype);
1115 /* they also have special treatment for pointers to void */
1116 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1117 if (ltype->ctype.as == rtype->ctype.as) {
1118 if (lbase == &void_ctype) {
1119 expr->right = cast_to(right, ltype);
1120 goto OK;
1122 if (rbase == &void_ctype) {
1123 expr->left = cast_to(left, rtype);
1124 goto OK;
1129 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1130 target_qualifiers(rtype),
1131 target_qualifiers(ltype));
1132 if (!typediff)
1133 goto OK;
1135 expression_error(expr, "incompatible types in comparison expression (%s):", typediff);
1136 info(expr->pos, " %s", show_typename(ltype));
1137 info(expr->pos, " %s", show_typename(rtype));
1138 return NULL;
1141 /* the result is int [6.5.8(6), 6.5.9(3)]*/
1142 expr->ctype = &int_ctype;
1143 return &int_ctype;
1147 * NOTE! The degenerate case of "x ? : y", where we don't
1148 * have a true case, this will possibly promote "x" to the
1149 * same type as "y", and thus _change_ the conditional
1150 * test in the expression. But since promotion is "safe"
1151 * for testing, that's OK.
1153 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1155 struct expression **cond;
1156 struct symbol *ctype, *ltype, *rtype, *lbase, *rbase;
1157 int lclass, rclass;
1158 const char * typediff;
1159 int qual;
1161 if (!evaluate_conditional(expr->conditional, 0))
1162 return NULL;
1163 if (!evaluate_expression(expr->cond_false))
1164 return NULL;
1166 ctype = degenerate(expr->conditional);
1167 rtype = degenerate(expr->cond_false);
1169 cond = &expr->conditional;
1170 ltype = ctype;
1171 if (expr->cond_true) {
1172 if (!evaluate_expression(expr->cond_true))
1173 return NULL;
1174 ltype = degenerate(expr->cond_true);
1175 cond = &expr->cond_true;
1178 expr->flags = (expr->conditional->flags & (*cond)->flags &
1179 expr->cond_false->flags & ~CEF_CONST_MASK);
1181 * In the standard, it is defined that an integer constant expression
1182 * shall only have operands that are themselves constant [6.6(6)].
1183 * While this definition is very clear for expressions that need all
1184 * their operands to be evaluated, for conditional expressions with a
1185 * constant condition things are much less obvious.
1186 * So, as an extension, do the same as GCC seems to do:
1187 * Consider a conditional expression with a constant condition
1188 * as having the same constantness as the argument corresponding
1189 * to the truth value (including in the case of address constants
1190 * which are defined more stricly [6.6(9)]).
1192 if (expr->conditional->flags & (CEF_ACE | CEF_ADDR)) {
1193 int is_true = expr_truth_value(expr->conditional);
1194 struct expression *arg = is_true ? *cond : expr->cond_false;
1195 expr->flags = arg->flags & ~CEF_CONST_MASK;
1198 lclass = classify_type(ltype, &ltype);
1199 rclass = classify_type(rtype, &rtype);
1200 if (lclass & rclass & TYPE_NUM) {
1201 ctype = usual_conversions('?', *cond, expr->cond_false,
1202 lclass, rclass, ltype, rtype);
1203 *cond = cast_to(*cond, ctype);
1204 expr->cond_false = cast_to(expr->cond_false, ctype);
1205 goto out;
1208 if ((lclass | rclass) & TYPE_PTR) {
1209 int is_null1 = is_null_pointer_constant(*cond);
1210 int is_null2 = is_null_pointer_constant(expr->cond_false);
1212 if (is_null1 && is_null2) {
1213 *cond = cast_to(*cond, &ptr_ctype);
1214 expr->cond_false = cast_to(expr->cond_false, &ptr_ctype);
1215 ctype = &ptr_ctype;
1216 goto out;
1218 if (is_null1 && (rclass & TYPE_PTR)) {
1219 if (is_null1 == NULL_ZERO)
1220 bad_null(*cond);
1221 *cond = cast_to(*cond, rtype);
1222 ctype = rtype;
1223 goto out;
1225 if (is_null2 && (lclass & TYPE_PTR)) {
1226 if (is_null2 == NULL_ZERO)
1227 bad_null(expr->cond_false);
1228 expr->cond_false = cast_to(expr->cond_false, ltype);
1229 ctype = ltype;
1230 goto out;
1232 if (!(lclass & rclass & TYPE_PTR)) {
1233 typediff = "different types";
1234 goto Err;
1236 /* OK, it's pointer on pointer */
1237 if (ltype->ctype.as != rtype->ctype.as) {
1238 typediff = "different address spaces";
1239 goto Err;
1242 /* need to be lazier here */
1243 lbase = examine_pointer_target(ltype);
1244 rbase = examine_pointer_target(rtype);
1245 qual = target_qualifiers(ltype) | target_qualifiers(rtype);
1247 if (lbase == &void_ctype) {
1248 /* XXX: pointers to function should warn here */
1249 ctype = ltype;
1250 goto Qual;
1253 if (rbase == &void_ctype) {
1254 /* XXX: pointers to function should warn here */
1255 ctype = rtype;
1256 goto Qual;
1258 /* XXX: that should be pointer to composite */
1259 ctype = ltype;
1260 typediff = type_difference(&ltype->ctype, &rtype->ctype,
1261 qual, qual);
1262 if (!typediff)
1263 goto Qual;
1264 goto Err;
1267 /* void on void, struct on same struct, union on same union */
1268 if (ltype == rtype) {
1269 ctype = ltype;
1270 goto out;
1272 typediff = "different base types";
1274 Err:
1275 expression_error(expr, "incompatible types in conditional expression (%s):", typediff);
1276 info(expr->pos, " %s", show_typename(ltype));
1277 info(expr->pos, " %s", show_typename(rtype));
1279 * if the condition is constant, the type is in fact known
1280 * so use it, as gcc & clang do.
1282 switch (expr_truth_value(expr->conditional)) {
1283 case 1: expr->ctype = ltype;
1284 break;
1285 case 0: expr->ctype = rtype;
1286 break;
1287 default:
1288 break;
1290 return NULL;
1292 out:
1293 expr->ctype = ctype;
1294 return ctype;
1296 Qual:
1297 if (qual & ~ctype->ctype.modifiers) {
1298 struct symbol *sym = alloc_symbol(ctype->pos, SYM_PTR);
1299 *sym = *ctype;
1300 sym->ctype.modifiers |= qual;
1301 ctype = sym;
1303 *cond = cast_to(*cond, ctype);
1304 expr->cond_false = cast_to(expr->cond_false, ctype);
1305 goto out;
1308 /* FP assignments can not do modulo or bit operations */
1309 static int compatible_float_op(int op)
1311 return op == SPECIAL_ADD_ASSIGN ||
1312 op == SPECIAL_SUB_ASSIGN ||
1313 op == SPECIAL_MUL_ASSIGN ||
1314 op == SPECIAL_DIV_ASSIGN;
1317 static int evaluate_assign_op(struct expression *expr)
1319 struct symbol *target = expr->left->ctype;
1320 struct symbol *source = expr->right->ctype;
1321 struct symbol *t, *s;
1322 int tclass = classify_type(target, &t);
1323 int sclass = classify_type(source, &s);
1324 int op = expr->op;
1326 if (tclass & sclass & TYPE_NUM) {
1327 if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1328 expression_error(expr, "invalid assignment");
1329 return 0;
1331 if (tclass & TYPE_RESTRICT) {
1332 if (!restricted_binop(op, t)) {
1333 warning(expr->pos, "bad assignment (%s) to %s",
1334 show_special(op), show_typename(t));
1335 expr->right = cast_to(expr->right, target);
1336 return 0;
1338 /* allowed assignments unfoul */
1339 if (sclass & TYPE_FOULED && unfoul(s) == t)
1340 goto Cast;
1341 if (!restricted_value(expr->right, t))
1342 return 1;
1343 } else if (op == SPECIAL_SHR_ASSIGN || op == SPECIAL_SHL_ASSIGN) {
1344 // shifts do integer promotions, but that's it.
1345 unrestrict(expr->right, sclass, &s);
1346 target = integer_promotion(s);
1347 goto Cast;
1348 } else if (!(sclass & TYPE_RESTRICT))
1349 goto usual;
1350 /* source and target would better be identical restricted */
1351 if (t == s)
1352 return 1;
1353 warning(expr->pos, "invalid assignment: %s", show_special(op));
1354 info(expr->pos, " left side has type %s", show_typename(t));
1355 info(expr->pos, " right side has type %s", show_typename(s));
1356 expr->right = cast_to(expr->right, target);
1357 return 0;
1359 if (tclass == TYPE_PTR && is_int(sclass)) {
1360 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1361 unrestrict(expr->right, sclass, &s);
1362 evaluate_ptr_add(expr, s);
1363 return 1;
1365 expression_error(expr, "invalid pointer assignment");
1366 return 0;
1369 expression_error(expr, "invalid assignment");
1370 return 0;
1372 usual:
1373 target = usual_conversions(op, expr->left, expr->right,
1374 tclass, sclass, target, source);
1375 Cast:
1376 expr->right = cast_to(expr->right, target);
1377 return 1;
1380 static int whitelist_pointers(struct symbol *t1, struct symbol *t2)
1382 if (t1 == t2)
1383 return 0; /* yes, 0 - we don't want a cast_to here */
1384 if (t1 == &void_ctype)
1385 return 1;
1386 if (t2 == &void_ctype)
1387 return 1;
1388 if (classify_type(t1, &t1) != TYPE_NUM)
1389 return 0;
1390 if (classify_type(t2, &t2) != TYPE_NUM)
1391 return 0;
1392 if (t1 == t2)
1393 return 1;
1394 if (t1->rank == -2 && t2->rank == -2)
1395 return 1;
1396 if (t1->rank != t2->rank)
1397 return 0;
1398 return !Wtypesign;
1401 static int check_assignment_types(struct symbol *target, struct expression **rp,
1402 const char **typediff)
1404 struct symbol *source = degenerate(*rp);
1405 struct symbol *t, *s;
1406 int tclass = classify_type(target, &t);
1407 int sclass = classify_type(source, &s);
1409 if (tclass & sclass & TYPE_NUM) {
1410 if (tclass & TYPE_RESTRICT) {
1411 /* allowed assignments unfoul */
1412 if (sclass & TYPE_FOULED && unfoul(s) == t)
1413 goto Cast;
1414 if (!restricted_value(*rp, target))
1415 return 1;
1416 if (s == t)
1417 return 1;
1418 } else if (!(sclass & TYPE_RESTRICT))
1419 goto Cast;
1420 if (t == &bool_ctype) {
1421 if (is_fouled_type(s))
1422 warning((*rp)->pos, "%s degrades to integer",
1423 show_typename(s->ctype.base_type));
1424 goto Cast;
1426 *typediff = "different base types";
1427 return 0;
1430 if (tclass == TYPE_PTR) {
1431 unsigned long mod1, mod2;
1432 unsigned long modl, modr;
1433 struct symbol *b1, *b2;
1434 // NULL pointer is always OK
1435 int is_null = is_null_pointer_constant(*rp);
1436 if (is_null) {
1437 if (is_null == NULL_ZERO)
1438 bad_null(*rp);
1439 goto Cast;
1441 if (!(sclass & TYPE_PTR)) {
1442 *typediff = "different base types";
1443 return 0;
1445 b1 = examine_pointer_target(t);
1446 b2 = examine_pointer_target(s);
1447 mod1 = target_qualifiers(t);
1448 mod2 = target_qualifiers(s);
1449 if (whitelist_pointers(b1, b2)) {
1451 * assignments to/from void * are OK, provided that
1452 * we do not remove qualifiers from pointed to [C]
1453 * or mix address spaces [sparse].
1455 if (t->ctype.as != s->ctype.as) {
1456 *typediff = "different address spaces";
1457 return 0;
1460 * If this is a function pointer assignment, it is
1461 * actually fine to assign a pointer to const data to
1462 * it, as a function pointer points to const data
1463 * implicitly, i.e., dereferencing it does not produce
1464 * an lvalue.
1466 if (b1->type == SYM_FN)
1467 mod1 |= MOD_CONST;
1468 if (mod2 & ~mod1 & ~MOD_FUN_ATTR) {
1469 *typediff = "different modifiers";
1470 return 0;
1472 goto Cast;
1474 /* It's OK if the target is more volatile or const than the source */
1475 /* It's OK if the source is more pure/noreturn than the target */
1476 modr = mod1 & ~MOD_REV_QUAL;
1477 modl = mod2 & MOD_REV_QUAL;
1478 *typediff = type_difference(&t->ctype, &s->ctype, modl, modr);
1479 if (*typediff)
1480 return 0;
1481 return 1;
1484 if ((tclass & TYPE_COMPOUND) && s == t)
1485 return 1;
1487 if (tclass & TYPE_NUM) {
1488 /* XXX: need to turn into comparison with NULL */
1489 if (t == &bool_ctype && (sclass & TYPE_PTR))
1490 goto Cast;
1491 *typediff = "different base types";
1492 return 0;
1494 *typediff = "invalid types";
1495 return 0;
1497 Cast:
1498 *rp = cast_to(*rp, target);
1499 return 1;
1502 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1503 struct expression **rp, const char *where)
1505 const char *typediff;
1507 if (!check_assignment_types(target, rp, &typediff)) {
1508 struct symbol *source = *rp ? (*rp)->ctype : NULL;
1509 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1510 info(expr->pos, " expected %s", show_typename(target));
1511 info(expr->pos, " got %s", show_typename(source));
1512 *rp = cast_to(*rp, target);
1513 return 0;
1516 return 1;
1519 static int compatible_transparent_union(struct symbol *target,
1520 struct expression **rp)
1522 struct symbol *t, *member;
1523 classify_type(target, &t);
1524 if (t->type != SYM_UNION || !t->transparent_union)
1525 return 0;
1527 FOR_EACH_PTR(t->symbol_list, member) {
1528 const char *typediff;
1529 if (check_assignment_types(member, rp, &typediff))
1530 return 1;
1531 } END_FOR_EACH_PTR(member);
1533 return 0;
1536 static int compatible_argument_type(struct expression *expr, struct symbol *target,
1537 struct expression **rp, const char *where)
1539 if (compatible_transparent_union(target, rp))
1540 return 1;
1542 return compatible_assignment_types(expr, target, rp, where);
1545 static void mark_addressable(struct expression *expr)
1547 while (expr->type == EXPR_BINOP && expr->op == '+')
1548 expr = expr->left;
1549 if (expr->type == EXPR_SYMBOL) {
1550 struct symbol *sym = expr->symbol;
1551 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1555 static void mark_assigned(struct expression *expr)
1557 struct symbol *sym;
1559 if (!expr)
1560 return;
1561 switch (expr->type) {
1562 case EXPR_SYMBOL:
1563 sym = expr->symbol;
1564 if (!sym)
1565 return;
1566 if (sym->type != SYM_NODE)
1567 return;
1568 sym->ctype.modifiers |= MOD_ASSIGNED;
1569 return;
1571 case EXPR_BINOP:
1572 mark_assigned(expr->left);
1573 mark_assigned(expr->right);
1574 return;
1575 case EXPR_CAST:
1576 case EXPR_FORCE_CAST:
1577 mark_assigned(expr->cast_expression);
1578 return;
1579 case EXPR_SLICE:
1580 mark_assigned(expr->base);
1581 return;
1582 default:
1583 /* Hmm? */
1584 return;
1588 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1590 if (type->ctype.modifiers & MOD_CONST)
1591 expression_error(left, "assignment to const expression");
1593 /* We know left is an lvalue, so it's a "preop-*" */
1594 mark_assigned(left->unop);
1597 static struct symbol *evaluate_assignment(struct expression *expr)
1599 struct expression *left = expr->left;
1600 struct symbol *ltype;
1602 if (!lvalue_expression(left)) {
1603 expression_error(expr, "not an lvalue");
1604 return NULL;
1607 ltype = left->ctype;
1609 if (expr->op != '=') {
1610 if (!evaluate_assign_op(expr))
1611 return NULL;
1612 } else {
1613 if (!compatible_assignment_types(expr, ltype, &expr->right, "assignment"))
1614 return NULL;
1617 evaluate_assign_to(left, ltype);
1619 expr->ctype = ltype;
1620 return ltype;
1623 static void examine_fn_arguments(struct symbol *fn)
1625 struct symbol *s;
1627 FOR_EACH_PTR(fn->arguments, s) {
1628 struct symbol *arg = evaluate_symbol(s);
1629 /* Array/function arguments silently degenerate into pointers */
1630 if (arg) {
1631 struct symbol *ptr;
1632 switch(arg->type) {
1633 case SYM_ARRAY:
1634 case SYM_FN:
1635 ptr = alloc_symbol(s->pos, SYM_PTR);
1636 if (arg->type == SYM_ARRAY)
1637 ptr->ctype = arg->ctype;
1638 else
1639 ptr->ctype.base_type = arg;
1640 combine_address_space(s->pos, &ptr->ctype.as, s->ctype.as);
1641 ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1643 s->ctype.base_type = ptr;
1644 s->ctype.as = NULL;
1645 s->ctype.modifiers &= ~MOD_PTRINHERIT;
1646 s->bit_size = 0;
1647 s->examined = 0;
1648 examine_symbol_type(s);
1649 break;
1650 default:
1651 /* nothing */
1652 break;
1655 } END_FOR_EACH_PTR(s);
1658 static struct symbol *convert_to_as_mod(struct symbol *sym, struct ident *as, int mod)
1660 /* Take the modifiers of the pointer, and apply them to the member */
1661 mod |= sym->ctype.modifiers;
1662 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1663 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1664 *newsym = *sym;
1665 newsym->ctype.as = as;
1666 newsym->ctype.modifiers = mod;
1667 sym = newsym;
1669 return sym;
1672 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1674 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1675 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1677 node->ctype.base_type = ptr;
1678 ptr->bit_size = bits_in_pointer;
1679 ptr->ctype.alignment = pointer_alignment;
1681 node->bit_size = bits_in_pointer;
1682 node->ctype.alignment = pointer_alignment;
1684 access_symbol(sym);
1685 if (sym->ctype.modifiers & MOD_REGISTER) {
1686 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1687 sym->ctype.modifiers &= ~MOD_REGISTER;
1689 if (sym->type == SYM_NODE) {
1690 combine_address_space(sym->pos, &ptr->ctype.as, sym->ctype.as);
1691 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1692 sym = sym->ctype.base_type;
1694 if (degenerate && sym->type == SYM_ARRAY) {
1695 combine_address_space(sym->pos, &ptr->ctype.as, sym->ctype.as);
1696 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1697 sym = sym->ctype.base_type;
1699 ptr->ctype.base_type = sym;
1701 return node;
1704 /* Arrays degenerate into pointers on pointer arithmetic */
1705 static struct symbol *degenerate(struct expression *expr)
1707 struct symbol *ctype, *base;
1709 if (!expr)
1710 return NULL;
1711 ctype = expr->ctype;
1712 if (!ctype)
1713 return NULL;
1714 base = examine_symbol_type(ctype);
1715 if (ctype->type == SYM_NODE)
1716 base = ctype->ctype.base_type;
1718 * Arrays degenerate into pointers to the entries, while
1719 * functions degenerate into pointers to themselves.
1720 * If array was part of non-lvalue compound, we create a copy
1721 * of that compound first and then act as if we were dealing with
1722 * the corresponding field in there.
1724 switch (base->type) {
1725 case SYM_ARRAY:
1726 if (expr->type == EXPR_SLICE) {
1727 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1728 struct expression *e0, *e1, *e2, *e3, *e4;
1730 a->ctype.base_type = expr->base->ctype;
1731 a->bit_size = expr->base->ctype->bit_size;
1732 a->array_size = expr->base->ctype->array_size;
1734 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1735 e0->symbol = a;
1736 e0->ctype = &lazy_ptr_ctype;
1738 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1739 e1->unop = e0;
1740 e1->op = '*';
1741 e1->ctype = expr->base->ctype; /* XXX */
1743 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1744 e2->left = e1;
1745 e2->right = expr->base;
1746 e2->op = '=';
1747 e2->ctype = expr->base->ctype;
1749 if (expr->r_bitpos) {
1750 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1751 e3->op = '+';
1752 e3->left = e0;
1753 e3->right = alloc_const_expression(expr->pos,
1754 bits_to_bytes(expr->r_bitpos));
1755 e3->ctype = &lazy_ptr_ctype;
1756 } else {
1757 e3 = e0;
1760 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1761 e4->left = e2;
1762 e4->right = e3;
1763 e4->ctype = &lazy_ptr_ctype;
1765 expr->unop = e4;
1766 expr->type = EXPR_PREOP;
1767 expr->op = '*';
1769 case SYM_FN:
1770 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1771 expression_error(expr, "strange non-value function or array");
1772 return &bad_ctype;
1774 *expr = *expr->unop;
1775 ctype = create_pointer(expr, ctype, 1);
1776 expr->ctype = ctype;
1777 mark_addressable(expr);
1778 default:
1779 /* nothing */;
1781 return ctype;
1784 static struct symbol *evaluate_addressof(struct expression *expr)
1786 struct expression *op = expr->unop;
1787 struct symbol *ctype;
1789 if (op->op != '*' || op->type != EXPR_PREOP) {
1790 expression_error(expr, "not addressable");
1791 return NULL;
1793 ctype = op->ctype;
1794 *expr = *op->unop;
1796 mark_addressable(expr);
1799 * symbol expression evaluation is lazy about the type
1800 * of the sub-expression, so we may have to generate
1801 * the type here if so..
1803 if (expr->ctype == &lazy_ptr_ctype) {
1804 ctype = create_pointer(expr, ctype, 0);
1805 expr->ctype = ctype;
1807 return expr->ctype;
1811 static struct symbol *evaluate_dereference(struct expression *expr)
1813 struct expression *op = expr->unop;
1814 struct symbol *ctype = op->ctype, *node, *target;
1816 /* Simplify: *&(expr) => (expr) */
1817 if (op->type == EXPR_PREOP && op->op == '&') {
1818 *expr = *op->unop;
1819 expr->flags = CEF_NONE;
1820 return expr->ctype;
1823 examine_symbol_type(ctype);
1825 /* Dereferencing a node drops all the node information. */
1826 if (ctype->type == SYM_NODE)
1827 ctype = ctype->ctype.base_type;
1829 target = ctype->ctype.base_type;
1831 switch (ctype->type) {
1832 default:
1833 expression_error(expr, "cannot dereference this type");
1834 return NULL;
1835 case SYM_FN:
1836 *expr = *op;
1837 return expr->ctype;
1838 case SYM_PTR:
1839 examine_symbol_type(target);
1840 node = alloc_symbol(expr->pos, SYM_NODE);
1841 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1842 merge_type(node, ctype);
1843 break;
1845 case SYM_ARRAY:
1846 if (!lvalue_expression(op)) {
1847 expression_error(op, "non-lvalue array??");
1848 return NULL;
1851 /* Do the implied "addressof" on the array */
1852 *op = *op->unop;
1855 * When an array is dereferenced, we need to pick
1856 * up the attributes of the original node too..
1858 node = alloc_symbol(expr->pos, SYM_NODE);
1859 merge_type(node, op->ctype);
1860 merge_type(node, ctype);
1861 break;
1864 node->bit_size = target->bit_size;
1865 node->array_size = target->array_size;
1867 expr->ctype = node;
1868 return node;
1872 * Unary post-ops: x++ and x--
1874 static struct symbol *evaluate_postop(struct expression *expr)
1876 struct expression *op = expr->unop;
1877 struct symbol *ctype = op->ctype;
1878 int class = classify_type(ctype, &ctype);
1879 int multiply = 0;
1881 if (!class || class & TYPE_COMPOUND) {
1882 expression_error(expr, "need scalar for ++/--");
1883 return NULL;
1885 if (!lvalue_expression(expr->unop)) {
1886 expression_error(expr, "need lvalue expression for ++/--");
1887 return NULL;
1890 if ((class & TYPE_RESTRICT) && restricted_unop(expr->op, &ctype))
1891 unrestrict(expr, class, &ctype);
1893 if (class & TYPE_NUM) {
1894 multiply = 1;
1895 } else if (class == TYPE_PTR) {
1896 struct symbol *target = examine_pointer_target(ctype);
1897 if (!is_function(target))
1898 multiply = bits_to_bytes(target->bit_size);
1901 if (multiply) {
1902 evaluate_assign_to(op, op->ctype);
1903 expr->op_value = multiply;
1904 expr->ctype = ctype;
1905 return ctype;
1908 expression_error(expr, "bad argument type for ++/--");
1909 return NULL;
1912 static struct symbol *evaluate_sign(struct expression *expr)
1914 struct symbol *ctype = expr->unop->ctype;
1915 int class = classify_type(ctype, &ctype);
1916 unsigned char flags = expr->unop->flags & ~CEF_CONST_MASK;
1918 /* should be an arithmetic type */
1919 if (!(class & TYPE_NUM))
1920 return bad_expr_type(expr);
1921 if (class & TYPE_RESTRICT)
1922 goto Restr;
1923 Normal:
1924 if (!(class & TYPE_FLOAT)) {
1925 ctype = integer_promotion(ctype);
1926 expr->unop = cast_to(expr->unop, ctype);
1927 } else if (expr->op != '~') {
1928 /* no conversions needed */
1929 } else {
1930 return bad_expr_type(expr);
1932 if (expr->op == '+')
1933 *expr = *expr->unop;
1934 expr->flags = flags;
1935 expr->ctype = ctype;
1936 return ctype;
1937 Restr:
1938 if (restricted_unop(expr->op, &ctype))
1939 unrestrict(expr, class, &ctype);
1940 goto Normal;
1943 static struct symbol *evaluate_preop(struct expression *expr)
1945 struct symbol *ctype = expr->unop->ctype;
1947 switch (expr->op) {
1948 case '(':
1949 *expr = *expr->unop;
1950 return ctype;
1952 case '+':
1953 case '-':
1954 case '~':
1955 return evaluate_sign(expr);
1957 case '*':
1958 return evaluate_dereference(expr);
1960 case '&':
1961 return evaluate_addressof(expr);
1963 case SPECIAL_INCREMENT:
1964 case SPECIAL_DECREMENT:
1966 * From a type evaluation standpoint the preops are
1967 * the same as the postops
1969 return evaluate_postop(expr);
1971 case '!':
1972 ctype = degenerate(expr->unop);
1973 expr->flags = expr->unop->flags & ~CEF_CONST_MASK;
1975 * A logical negation never yields an address constant
1976 * [6.6(9)].
1978 expr->flags &= ~CEF_ADDR;
1980 if (is_safe_type(ctype))
1981 warning(expr->pos, "testing a 'safe expression'");
1982 if (is_float_type(ctype)) {
1983 struct expression *arg = expr->unop;
1984 expr->type = EXPR_COMPARE;
1985 expr->op = SPECIAL_EQUAL;
1986 expr->left = arg;
1987 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1988 expr->right->ctype = ctype;
1989 expr->right->fvalue = 0;
1990 } else if (is_fouled_type(ctype)) {
1991 warning(expr->pos, "%s degrades to integer",
1992 show_typename(ctype->ctype.base_type));
1994 /* the result is int [6.5.3.3(5)]*/
1995 ctype = &int_ctype;
1996 break;
1998 default:
1999 break;
2001 expr->ctype = ctype;
2002 return ctype;
2005 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
2007 struct ptr_list *head = (struct ptr_list *)_list;
2008 struct ptr_list *list = head;
2010 if (!head)
2011 return NULL;
2012 do {
2013 int i;
2014 for (i = 0; i < list->nr; i++) {
2015 struct symbol *sym = (struct symbol *) list->list[i];
2016 if (sym->ident) {
2017 if (sym->ident != ident)
2018 continue;
2019 *offset = sym->offset;
2020 return sym;
2021 } else {
2022 struct symbol *ctype = sym->ctype.base_type;
2023 struct symbol *sub;
2024 if (!ctype)
2025 continue;
2026 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
2027 continue;
2028 sub = find_identifier(ident, ctype->symbol_list, offset);
2029 if (!sub)
2030 continue;
2031 *offset += sym->offset;
2032 return sub;
2035 } while ((list = list->next) != head);
2036 return NULL;
2039 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
2041 struct expression *add;
2044 * Create a new add-expression
2046 * NOTE! Even if we just add zero, we need a new node
2047 * for the member pointer, since it has a different
2048 * type than the original pointer. We could make that
2049 * be just a cast, but the fact is, a node is a node,
2050 * so we might as well just do the "add zero" here.
2052 add = alloc_expression(expr->pos, EXPR_BINOP);
2053 add->op = '+';
2054 add->left = expr;
2055 add->right = alloc_expression(expr->pos, EXPR_VALUE);
2056 add->right->ctype = &int_ctype;
2057 add->right->value = offset;
2060 * The ctype of the pointer will be lazily evaluated if
2061 * we ever take the address of this member dereference..
2063 add->ctype = &lazy_ptr_ctype;
2065 * The resulting address of a member access through an address
2066 * constant is an address constant again [6.6(9)].
2068 add->flags = expr->flags;
2070 return add;
2073 /* structure/union dereference */
2074 static struct symbol *evaluate_member_dereference(struct expression *expr)
2076 int offset;
2077 struct symbol *ctype, *member;
2078 struct expression *deref = expr->deref, *add;
2079 struct ident *ident = expr->member;
2080 struct ident *address_space;
2081 unsigned int mod;
2083 if (!evaluate_expression(deref))
2084 return NULL;
2085 if (!ident) {
2086 expression_error(expr, "bad member name");
2087 return NULL;
2090 ctype = deref->ctype;
2091 examine_symbol_type(ctype);
2092 address_space = ctype->ctype.as;
2093 mod = ctype->ctype.modifiers;
2094 if (ctype->type == SYM_NODE) {
2095 ctype = ctype->ctype.base_type;
2096 combine_address_space(deref->pos, &address_space, ctype->ctype.as);
2097 mod |= ctype->ctype.modifiers;
2099 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
2100 expression_error(expr, "expected structure or union");
2101 return NULL;
2103 offset = 0;
2104 member = find_identifier(ident, ctype->symbol_list, &offset);
2105 if (!member) {
2106 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
2107 const char *name = "<unnamed>";
2108 int namelen = 9;
2109 if (ctype->ident) {
2110 name = ctype->ident->name;
2111 namelen = ctype->ident->len;
2113 if (ctype->symbol_list)
2114 expression_error(expr, "no member '%s' in %s %.*s",
2115 show_ident(ident), type, namelen, name);
2116 else
2117 expression_error(expr, "using member '%s' in "
2118 "incomplete %s %.*s", show_ident(ident),
2119 type, namelen, name);
2120 return NULL;
2124 * The member needs to take on the address space and modifiers of
2125 * the "parent" type.
2127 member = convert_to_as_mod(member, address_space, mod);
2128 ctype = get_base_type(member);
2130 if (!lvalue_expression(deref)) {
2131 if (deref->type != EXPR_SLICE) {
2132 expr->base = deref;
2133 expr->r_bitpos = 0;
2134 } else {
2135 expr->base = deref->base;
2136 expr->r_bitpos = deref->r_bitpos;
2138 expr->r_bitpos += bytes_to_bits(offset);
2139 expr->type = EXPR_SLICE;
2140 expr->r_nrbits = member->bit_size;
2141 expr->r_bitpos += member->bit_offset;
2142 expr->ctype = member;
2143 return member;
2146 deref = deref->unop;
2147 expr->deref = deref;
2149 add = evaluate_offset(deref, offset);
2150 expr->type = EXPR_PREOP;
2151 expr->op = '*';
2152 expr->unop = add;
2154 expr->ctype = member;
2155 return member;
2158 static int is_promoted(struct expression *expr)
2160 while (1) {
2161 switch (expr->type) {
2162 case EXPR_BINOP:
2163 case EXPR_SELECT:
2164 case EXPR_CONDITIONAL:
2165 return 1;
2166 case EXPR_COMMA:
2167 expr = expr->right;
2168 continue;
2169 case EXPR_PREOP:
2170 switch (expr->op) {
2171 case '(':
2172 expr = expr->unop;
2173 continue;
2174 case '+':
2175 case '-':
2176 case '~':
2177 return 1;
2178 default:
2179 return 0;
2181 default:
2182 return 0;
2188 static struct symbol *evaluate_cast(struct expression *);
2190 static struct symbol *evaluate_type_information(struct expression *expr)
2192 struct symbol *sym = expr->cast_type;
2193 if (!sym) {
2194 sym = evaluate_expression(expr->cast_expression);
2195 if (!sym)
2196 return NULL;
2198 * Expressions of restricted types will possibly get
2199 * promoted - check that here
2201 if (is_restricted_type(sym)) {
2202 if (sym->bit_size < bits_in_int && is_promoted(expr))
2203 sym = &int_ctype;
2204 } else if (is_fouled_type(sym)) {
2205 sym = &int_ctype;
2208 examine_symbol_type(sym);
2209 if (is_bitfield_type(sym)) {
2210 expression_error(expr, "trying to examine bitfield type");
2211 return NULL;
2213 return sym;
2216 static struct symbol *evaluate_sizeof(struct expression *expr)
2218 struct symbol *type;
2219 int size;
2221 type = evaluate_type_information(expr);
2222 if (!type)
2223 return NULL;
2225 size = type->bit_size;
2227 if (size < 0 && is_void_type(type)) {
2228 if (Wpointer_arith)
2229 warning(expr->pos, "expression using sizeof(void)");
2230 size = bits_in_char;
2233 if (is_bool_type(type)) {
2234 if (Wsizeof_bool)
2235 warning(expr->pos, "expression using sizeof _Bool");
2236 size = bits_to_bytes(bits_in_bool) * bits_in_char;
2239 if (is_function(type->ctype.base_type)) {
2240 if (Wpointer_arith)
2241 warning(expr->pos, "expression using sizeof on a function");
2242 size = bits_in_char;
2245 if (is_array_type(type) && size < 0) { // VLA, 1-dimension only
2246 struct expression *base, *size;
2247 struct symbol *base_type;
2249 if (type->type == SYM_NODE)
2250 type = type->ctype.base_type; // strip the SYM_NODE
2251 base_type = get_base_type(type);
2252 if (!base_type)
2253 goto error;
2254 if (base_type->bit_size <= 0) {
2255 base = alloc_expression(expr->pos, EXPR_SIZEOF);
2256 base->cast_type = base_type;
2257 if (!evaluate_sizeof(base))
2258 goto error;
2259 } else {
2260 base = alloc_expression(expr->pos, EXPR_VALUE);
2261 base->value = bits_to_bytes(base_type->bit_size);
2262 base->ctype = size_t_ctype;
2264 size = alloc_expression(expr->pos, EXPR_CAST);
2265 size->cast_type = size_t_ctype;
2266 size->cast_expression = type->array_size;
2267 if (!evaluate_expression(size))
2268 goto error;
2269 expr->left = size;
2270 expr->right = base;
2271 expr->type = EXPR_BINOP;
2272 expr->op = '*';
2273 return expr->ctype = size_t_ctype;
2276 error:
2277 if ((size < 0) || (size & (bits_in_char - 1)))
2278 expression_error(expr, "cannot size expression");
2280 expr->type = EXPR_VALUE;
2281 expr->value = bits_to_bytes(size);
2282 expr->taint = 0;
2283 expr->ctype = size_t_ctype;
2284 return size_t_ctype;
2287 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
2289 struct symbol *type;
2290 int size;
2292 type = evaluate_type_information(expr);
2293 if (!type)
2294 return NULL;
2296 if (type->type == SYM_NODE)
2297 type = type->ctype.base_type;
2298 if (!type)
2299 return NULL;
2300 switch (type->type) {
2301 case SYM_ARRAY:
2302 break;
2303 case SYM_PTR:
2304 type = get_base_type(type);
2305 if (type)
2306 break;
2307 default:
2308 expression_error(expr, "expected pointer expression");
2309 return NULL;
2311 size = type->bit_size;
2312 if (size & (bits_in_char-1))
2313 size = 0;
2314 expr->type = EXPR_VALUE;
2315 expr->value = bits_to_bytes(size);
2316 expr->taint = 0;
2317 expr->ctype = size_t_ctype;
2318 return size_t_ctype;
2321 static struct symbol *evaluate_alignof(struct expression *expr)
2323 struct symbol *type;
2325 type = evaluate_type_information(expr);
2326 if (!type)
2327 return NULL;
2329 expr->type = EXPR_VALUE;
2330 expr->value = type->ctype.alignment;
2331 expr->taint = 0;
2332 expr->ctype = size_t_ctype;
2333 return size_t_ctype;
2336 static int evaluate_arguments(struct symbol *fn, struct expression_list *head)
2338 struct expression *expr;
2339 struct symbol_list *argument_types = fn->arguments;
2340 struct symbol *argtype;
2341 int i = 1;
2343 PREPARE_PTR_LIST(argument_types, argtype);
2344 FOR_EACH_PTR (head, expr) {
2345 struct expression **p = THIS_ADDRESS(expr);
2346 struct symbol *ctype, *target;
2347 ctype = evaluate_expression(expr);
2349 if (!ctype)
2350 return 0;
2352 target = argtype;
2353 if (!target) {
2354 struct symbol *type;
2355 int class = classify_type(ctype, &type);
2356 if (is_int(class)) {
2357 *p = cast_to(expr, integer_promotion(type));
2358 } else if (class & TYPE_FLOAT) {
2359 if (type->rank < 0)
2360 *p = cast_to(expr, &double_ctype);
2361 } else if (class & TYPE_PTR) {
2362 if (expr->ctype == &null_ctype)
2363 *p = cast_to(expr, &ptr_ctype);
2364 else
2365 degenerate(expr);
2367 } else if (!target->forced_arg){
2368 static char where[30];
2369 examine_symbol_type(target);
2370 sprintf(where, "argument %d", i);
2371 compatible_argument_type(expr, target, p, where);
2374 i++;
2375 NEXT_PTR_LIST(argtype);
2376 } END_FOR_EACH_PTR(expr);
2377 FINISH_PTR_LIST(argtype);
2378 return 1;
2381 static void convert_index(struct expression *e)
2383 struct expression *child = e->idx_expression;
2384 unsigned from = e->idx_from;
2385 unsigned to = e->idx_to + 1;
2386 e->type = EXPR_POS;
2387 e->init_offset = from * bits_to_bytes(e->ctype->bit_size);
2388 e->init_nr = to - from;
2389 e->init_expr = child;
2392 static void convert_ident(struct expression *e)
2394 struct expression *child = e->ident_expression;
2395 int offset = e->offset;
2397 e->type = EXPR_POS;
2398 e->init_offset = offset;
2399 e->init_nr = 1;
2400 e->init_expr = child;
2403 static void convert_designators(struct expression *e)
2405 while (e) {
2406 if (e->type == EXPR_INDEX)
2407 convert_index(e);
2408 else if (e->type == EXPR_IDENTIFIER)
2409 convert_ident(e);
2410 else
2411 break;
2412 e = e->init_expr;
2416 static void excess(struct expression *e, const char *s)
2418 warning(e->pos, "excessive elements in %s initializer", s);
2422 * implicit designator for the first element
2424 static struct expression *first_subobject(struct symbol *ctype, int class,
2425 struct expression **v)
2427 struct expression *e = *v, *new;
2429 if (ctype->type == SYM_NODE)
2430 ctype = ctype->ctype.base_type;
2432 if (class & TYPE_PTR) { /* array */
2433 if (!ctype->bit_size)
2434 return NULL;
2435 new = alloc_expression(e->pos, EXPR_INDEX);
2436 new->idx_expression = e;
2437 new->ctype = ctype->ctype.base_type;
2438 } else {
2439 struct symbol *field, *p;
2440 PREPARE_PTR_LIST(ctype->symbol_list, p);
2441 while (p && !p->ident && is_bitfield_type(p))
2442 NEXT_PTR_LIST(p);
2443 field = p;
2444 FINISH_PTR_LIST(p);
2445 if (!field)
2446 return NULL;
2447 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2448 new->ident_expression = e;
2449 new->field = new->ctype = field;
2450 new->offset = field->offset;
2452 *v = new;
2453 return new;
2457 * sanity-check explicit designators; return the innermost one or NULL
2458 * in case of error. Assign types.
2460 static struct expression *check_designators(struct expression *e,
2461 struct symbol *ctype)
2463 struct expression *last = NULL;
2464 const char *err;
2465 while (1) {
2466 if (ctype->type == SYM_NODE)
2467 ctype = ctype->ctype.base_type;
2468 if (e->type == EXPR_INDEX) {
2469 struct symbol *type;
2470 if (ctype->type != SYM_ARRAY) {
2471 err = "array index in non-array";
2472 break;
2474 type = ctype->ctype.base_type;
2475 if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2476 unsigned offset = array_element_offset(type->bit_size, e->idx_to);
2477 if (offset >= ctype->bit_size) {
2478 err = "index out of bounds in";
2479 break;
2482 e->ctype = ctype = type;
2483 ctype = type;
2484 last = e;
2485 if (!e->idx_expression) {
2486 err = "invalid";
2487 break;
2489 e = e->idx_expression;
2490 } else if (e->type == EXPR_IDENTIFIER) {
2491 int offset = 0;
2492 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2493 err = "field name not in struct or union";
2494 break;
2496 ctype = find_identifier(e->expr_ident, ctype->symbol_list, &offset);
2497 if (!ctype) {
2498 err = "unknown field name in";
2499 break;
2501 e->offset = offset;
2502 e->field = e->ctype = ctype;
2503 last = e;
2504 if (!e->ident_expression) {
2505 err = "invalid";
2506 break;
2508 e = e->ident_expression;
2509 } else if (e->type == EXPR_POS) {
2510 err = "internal front-end error: EXPR_POS in";
2511 break;
2512 } else
2513 return last;
2515 expression_error(e, "%s initializer", err);
2516 return NULL;
2520 * choose the next subobject to initialize.
2522 * Get designators for next element, switch old ones to EXPR_POS.
2523 * Return the resulting expression or NULL if we'd run out of subobjects.
2524 * The innermost designator is returned in *v. Designators in old
2525 * are assumed to be already sanity-checked.
2527 static struct expression *next_designators(struct expression *old,
2528 struct symbol *ctype,
2529 struct expression *e, struct expression **v)
2531 struct expression *new = NULL;
2533 if (!old)
2534 return NULL;
2535 if (old->type == EXPR_INDEX) {
2536 struct expression *copy;
2537 unsigned n;
2539 copy = next_designators(old->idx_expression,
2540 old->ctype, e, v);
2541 if (!copy) {
2542 n = old->idx_to + 1;
2543 if (array_element_offset(old->ctype->bit_size, n) == ctype->bit_size) {
2544 convert_index(old);
2545 return NULL;
2547 copy = e;
2548 *v = new = alloc_expression(e->pos, EXPR_INDEX);
2549 } else {
2550 n = old->idx_to;
2551 new = alloc_expression(e->pos, EXPR_INDEX);
2554 new->idx_from = new->idx_to = n;
2555 new->idx_expression = copy;
2556 new->ctype = old->ctype;
2557 convert_index(old);
2558 } else if (old->type == EXPR_IDENTIFIER) {
2559 struct expression *copy;
2560 struct symbol *field;
2561 int offset = 0;
2563 copy = next_designators(old->ident_expression,
2564 old->ctype, e, v);
2565 if (!copy) {
2566 field = old->field->next_subobject;
2567 if (!field) {
2568 convert_ident(old);
2569 return NULL;
2571 copy = e;
2572 *v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2574 * We can't necessarily trust "field->offset",
2575 * because the field might be in an anonymous
2576 * union, and the field offset is then the offset
2577 * within that union.
2579 * The "old->offset - old->field->offset"
2580 * would be the offset of such an anonymous
2581 * union.
2583 offset = old->offset - old->field->offset;
2584 } else {
2585 field = old->field;
2586 new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2589 new->field = field;
2590 new->expr_ident = field->ident;
2591 new->ident_expression = copy;
2592 new->ctype = field;
2593 new->offset = field->offset + offset;
2594 convert_ident(old);
2596 return new;
2599 static int handle_initializer(struct expression **ep, int nested,
2600 int class, struct symbol *ctype, unsigned long mods);
2603 * deal with traversing subobjects [6.7.8(17,18,20)]
2605 static void handle_list_initializer(struct expression *expr,
2606 int class, struct symbol *ctype, unsigned long mods)
2608 struct expression *e, *last = NULL, *top = NULL, *next;
2609 int jumped = 0;
2611 if (expr->zero_init)
2612 free_ptr_list(&expr->expr_list);
2614 FOR_EACH_PTR(expr->expr_list, e) {
2615 struct expression **v;
2616 struct symbol *type;
2617 int lclass;
2619 if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2620 struct symbol *struct_sym;
2621 if (!top) {
2622 top = e;
2623 last = first_subobject(ctype, class, &top);
2624 } else {
2625 last = next_designators(last, ctype, e, &top);
2627 if (!last) {
2628 excess(e, class & TYPE_PTR ? "array" :
2629 "struct or union");
2630 DELETE_CURRENT_PTR(e);
2631 continue;
2633 struct_sym = ctype->type == SYM_NODE ? ctype->ctype.base_type : ctype;
2634 if (Wdesignated_init && struct_sym->designated_init)
2635 warning(e->pos, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2636 ctype->ident ? "in initializer for " : "",
2637 ctype->ident ? ctype->ident->len : 0,
2638 ctype->ident ? ctype->ident->name : "",
2639 ctype->ident ? ": " : "",
2640 get_type_name(struct_sym->type),
2641 show_ident(struct_sym->ident));
2642 if (jumped) {
2643 warning(e->pos, "advancing past deep designator");
2644 jumped = 0;
2646 REPLACE_CURRENT_PTR(e, last);
2647 } else {
2648 next = check_designators(e, ctype);
2649 if (!next) {
2650 DELETE_CURRENT_PTR(e);
2651 continue;
2653 top = next;
2654 /* deeper than one designator? */
2655 jumped = top != e;
2656 convert_designators(last);
2657 last = e;
2660 found:
2661 lclass = classify_type(top->ctype, &type);
2662 if (top->type == EXPR_INDEX)
2663 v = &top->idx_expression;
2664 else
2665 v = &top->ident_expression;
2667 mods |= ctype->ctype.modifiers & MOD_STORAGE;
2668 if (handle_initializer(v, 1, lclass, top->ctype, mods))
2669 continue;
2671 if (!(lclass & TYPE_COMPOUND)) {
2672 warning(e->pos, "bogus scalar initializer");
2673 DELETE_CURRENT_PTR(e);
2674 continue;
2677 next = first_subobject(type, lclass, v);
2678 if (next) {
2679 warning(e->pos, "missing braces around initializer");
2680 top = next;
2681 goto found;
2684 DELETE_CURRENT_PTR(e);
2685 excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2687 } END_FOR_EACH_PTR(e);
2689 convert_designators(last);
2690 expr->ctype = ctype;
2693 static int is_string_literal(struct expression **v)
2695 struct expression *e = *v;
2696 while (e && e->type == EXPR_PREOP && e->op == '(')
2697 e = e->unop;
2698 if (!e || e->type != EXPR_STRING)
2699 return 0;
2700 if (e != *v && Wparen_string)
2701 warning(e->pos,
2702 "array initialized from parenthesized string constant");
2703 *v = e;
2704 return 1;
2708 * We want a normal expression, possibly in one layer of braces. Warn
2709 * if the latter happens inside a list (it's legal, but likely to be
2710 * an effect of screwup). In case of anything not legal, we are definitely
2711 * having an effect of screwup, so just fail and let the caller warn.
2713 static struct expression *handle_scalar(struct expression *e, int nested)
2715 struct expression *v = NULL, *p;
2716 int count = 0;
2718 /* normal case */
2719 if (e->type != EXPR_INITIALIZER)
2720 return e;
2722 FOR_EACH_PTR(e->expr_list, p) {
2723 if (!v)
2724 v = p;
2725 count++;
2726 } END_FOR_EACH_PTR(p);
2727 if (count != 1)
2728 return NULL;
2729 switch(v->type) {
2730 case EXPR_INITIALIZER:
2731 case EXPR_INDEX:
2732 case EXPR_IDENTIFIER:
2733 return NULL;
2734 default:
2735 break;
2737 if (nested)
2738 warning(e->pos, "braces around scalar initializer");
2739 return v;
2743 * deal with the cases that don't care about subobjects:
2744 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2745 * character array <- string literal, possibly in braces [6.7.8(14)]
2746 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2747 * compound type <- initializer list in braces [6.7.8(16)]
2748 * The last one punts to handle_list_initializer() which, in turn will call
2749 * us for individual elements of the list.
2751 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2752 * the lack of support of wide char stuff in general.
2754 * One note: we need to take care not to evaluate a string literal until
2755 * we know that we *will* handle it right here. Otherwise we would screw
2756 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2757 * { "string", ...} - we need to preserve that string literal recognizable
2758 * until we dig into the inner struct.
2760 static int handle_initializer(struct expression **ep, int nested,
2761 int class, struct symbol *ctype, unsigned long mods)
2763 int is_string = is_string_type(ctype);
2764 struct expression *e = *ep, *p;
2765 struct symbol *type;
2767 if (!e)
2768 return 0;
2770 /* scalar */
2771 if (!(class & TYPE_COMPOUND)) {
2772 e = handle_scalar(e, nested);
2773 if (!e)
2774 return 0;
2775 *ep = e;
2776 if (!evaluate_expression(e))
2777 return 1;
2778 compatible_assignment_types(e, ctype, ep, "initializer");
2780 * Initializers for static storage duration objects
2781 * shall be constant expressions or a string literal [6.7.8(4)].
2783 mods |= ctype->ctype.modifiers;
2784 mods &= (MOD_TOPLEVEL | MOD_STATIC);
2785 if (mods && !(e->flags & (CEF_ACE | CEF_ADDR)))
2786 if (Wconstexpr_not_const)
2787 warning(e->pos, "non-constant initializer for static object");
2789 return 1;
2793 * sublist; either a string, or we dig in; the latter will deal with
2794 * pathologies, so we don't need anything fancy here.
2796 if (e->type == EXPR_INITIALIZER) {
2797 if (is_string) {
2798 struct expression *v = NULL;
2799 int count = 0;
2801 FOR_EACH_PTR(e->expr_list, p) {
2802 if (!v)
2803 v = p;
2804 count++;
2805 } END_FOR_EACH_PTR(p);
2806 if (count == 1 && is_string_literal(&v)) {
2807 *ep = e = v;
2808 goto String;
2811 handle_list_initializer(e, class, ctype, mods);
2812 return 1;
2815 /* string */
2816 if (is_string_literal(&e)) {
2817 /* either we are doing array of char, or we'll have to dig in */
2818 if (is_string) {
2819 *ep = e;
2820 goto String;
2822 return 0;
2824 /* struct or union can be initialized by compatible */
2825 if (class != TYPE_COMPOUND)
2826 return 0;
2827 type = evaluate_expression(e);
2828 if (!type)
2829 return 0;
2830 if (ctype->type == SYM_NODE)
2831 ctype = ctype->ctype.base_type;
2832 if (type->type == SYM_NODE)
2833 type = type->ctype.base_type;
2834 if (ctype == type)
2835 return 1;
2836 return 0;
2838 String:
2839 p = alloc_expression(e->pos, EXPR_STRING);
2840 *p = *e;
2841 type = evaluate_expression(p);
2842 if (ctype->bit_size != -1) {
2843 if (ctype->bit_size + bits_in_char < type->bit_size)
2844 warning(e->pos,
2845 "too long initializer-string for array of char");
2846 else if (Winit_cstring && ctype->bit_size + bits_in_char == type->bit_size) {
2847 warning(e->pos,
2848 "too long initializer-string for array of char(no space for nul char)");
2851 *ep = p;
2852 return 1;
2855 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2857 struct symbol *type;
2858 int class = classify_type(ctype, &type);
2859 if (!handle_initializer(ep, 0, class, ctype, 0))
2860 expression_error(*ep, "invalid initializer");
2863 static struct symbol *cast_to_bool(struct expression *expr)
2865 struct expression *old = expr->cast_expression;
2866 struct expression *zero;
2867 struct symbol *otype;
2868 int oclass = classify_type(degenerate(old), &otype);
2869 struct symbol *ctype;
2871 if (oclass & TYPE_COMPOUND)
2872 return NULL;
2874 zero = alloc_const_expression(expr->pos, 0);
2875 expr->op = SPECIAL_NOTEQUAL;
2876 ctype = usual_conversions(expr->op, old, zero,
2877 oclass, TYPE_NUM, otype, zero->ctype);
2878 expr->type = EXPR_COMPARE;
2879 expr->left = cast_to(old, ctype);
2880 expr->right = cast_to(zero, ctype);
2882 return expr->ctype;
2885 static int cast_flags(struct expression *expr, struct expression *old)
2887 struct symbol *t;
2888 int class;
2889 int flags = CEF_NONE;
2891 class = classify_type(expr->ctype, &t);
2892 if (class & TYPE_NUM) {
2893 flags = old->flags & ~CEF_CONST_MASK;
2895 * Casts to numeric types never result in address
2896 * constants [6.6(9)].
2898 flags &= ~CEF_ADDR;
2901 * As an extension, treat address constants cast to
2902 * integer type as an arithmetic constant.
2904 if (old->flags & CEF_ADDR)
2905 flags = CEF_ACE;
2908 * Cast to float type -> not an integer constant
2909 * expression [6.6(6)].
2911 if (class & TYPE_FLOAT)
2912 flags &= ~CEF_CLR_ICE;
2914 * Casts of float literals to integer type results in
2915 * a constant integer expression [6.6(6)].
2917 else if (old->flags & CEF_FLOAT)
2918 flags = CEF_SET_ICE;
2919 } else if (class & TYPE_PTR) {
2921 * Casts of integer literals to pointer type yield
2922 * address constants [6.6(9)].
2924 * As an extension, treat address constants cast to a
2925 * different pointer type as address constants again.
2927 * As another extension, treat integer constant
2928 * expressions (in contrast to literals) cast to
2929 * pointer type as address constants.
2931 if (old->flags & (CEF_ICE | CEF_ADDR))
2932 flags = CEF_ADDR;
2935 return flags;
2938 static struct symbol *evaluate_cast(struct expression *expr)
2940 struct expression *source = expr->cast_expression;
2941 struct symbol *ctype;
2942 struct symbol *ttype, *stype;
2943 int tclass, sclass;
2944 struct ident *tas = NULL, *sas = NULL;
2946 if (!source)
2947 return NULL;
2950 * Special case: a cast can be followed by an
2951 * initializer, in which case we need to pass
2952 * the type value down to that initializer rather
2953 * than trying to evaluate it as an expression
2954 * (cfr. compound literals: C99 & C11 6.5.2.5).
2956 * A more complex case is when the initializer is
2957 * dereferenced as part of a post-fix expression.
2958 * We need to produce an expression that can be dereferenced.
2960 if (source->type == EXPR_INITIALIZER) {
2961 struct symbol *sym = expr->cast_type;
2962 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2964 sym->initializer = source;
2965 evaluate_symbol(sym);
2967 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2968 addr->symbol = sym;
2969 if (sym->ctype.modifiers & MOD_TOPLEVEL)
2970 addr->flags |= CEF_ADDR;
2972 expr->type = EXPR_PREOP;
2973 expr->op = '*';
2974 expr->unop = addr;
2975 expr->ctype = sym;
2977 return sym;
2980 ctype = examine_symbol_type(expr->cast_type);
2981 expr->ctype = ctype;
2982 expr->cast_type = ctype;
2984 evaluate_expression(source);
2985 degenerate(source);
2987 tclass = classify_type(ctype, &ttype);
2989 expr->flags = cast_flags(expr, source);
2992 * You can always throw a value away by casting to
2993 * "void" - that's an implicit "force". Note that
2994 * the same is _not_ true of "void *".
2996 if (ttype == &void_ctype)
2997 goto out;
2999 stype = source->ctype;
3000 if (!stype) {
3001 expression_error(expr, "cast from unknown type");
3002 goto out;
3004 sclass = classify_type(stype, &stype);
3006 if (expr->type == EXPR_FORCE_CAST)
3007 goto out;
3009 if (tclass & (TYPE_COMPOUND | TYPE_FN))
3010 warning(expr->pos, "cast to non-scalar");
3012 if (sclass & TYPE_COMPOUND)
3013 warning(expr->pos, "cast from non-scalar");
3015 /* allowed cast unfouls */
3016 if (sclass & TYPE_FOULED)
3017 stype = unfoul(stype);
3019 if (ttype != stype) {
3020 if ((tclass & TYPE_RESTRICT) && restricted_value(source, ttype))
3021 warning(expr->pos, "cast to %s",
3022 show_typename(ttype));
3023 if (sclass & TYPE_RESTRICT) {
3024 if (ttype == &bool_ctype) {
3025 if (sclass & TYPE_FOULED)
3026 warning(expr->pos, "%s degrades to integer",
3027 show_typename(stype));
3028 } else {
3029 warning(expr->pos, "cast from %s",
3030 show_typename(stype));
3035 if ((ttype == &ulong_ctype || ttype == uintptr_ctype) && !Wcast_from_as)
3036 tas = &bad_address_space;
3037 else if (tclass == TYPE_PTR) {
3038 examine_pointer_target(ttype);
3039 tas = ttype->ctype.as;
3042 if ((stype == &ulong_ctype || stype == uintptr_ctype))
3043 sas = &bad_address_space;
3044 else if (sclass == TYPE_PTR) {
3045 examine_pointer_target(stype);
3046 sas = stype->ctype.as;
3049 if (!tas && valid_as(sas))
3050 warning(expr->pos, "cast removes address space '%s' of expression", show_as(sas));
3051 if (valid_as(tas) && valid_as(sas) && tas != sas)
3052 warning(expr->pos, "cast between address spaces (%s -> %s)", show_as(sas), show_as(tas));
3053 if (valid_as(tas) && !sas &&
3054 !is_null_pointer_constant(source) && Wcast_to_as)
3055 warning(expr->pos,
3056 "cast adds address space '%s' to expression", show_as(tas));
3058 if (!(ttype->ctype.modifiers & MOD_PTRINHERIT) && tclass == TYPE_PTR &&
3059 !tas && (source->flags & CEF_ICE)) {
3060 if (ttype->ctype.base_type == &void_ctype) {
3061 if (is_zero_constant(source)) {
3062 /* NULL */
3063 expr->type = EXPR_VALUE;
3064 expr->ctype = &null_ctype;
3065 expr->value = 0;
3066 return expr->ctype;
3071 if (ttype == &bool_ctype)
3072 cast_to_bool(expr);
3074 // checks pointers to restricted
3075 while (Wbitwise_pointer && tclass == TYPE_PTR && sclass == TYPE_PTR) {
3076 tclass = classify_type(ttype->ctype.base_type, &ttype);
3077 sclass = classify_type(stype->ctype.base_type, &stype);
3078 if (ttype == stype)
3079 break;
3080 if (!ttype || !stype)
3081 break;
3082 if (ttype == &void_ctype || stype == &void_ctype)
3083 break;
3084 if (tclass & TYPE_RESTRICT) {
3085 warning(expr->pos, "cast to %s", show_typename(ctype));
3086 break;
3088 if (sclass & TYPE_RESTRICT) {
3089 warning(expr->pos, "cast from %s", show_typename(source->ctype));
3090 break;
3093 out:
3094 return ctype;
3098 * Evaluate a call expression with a symbol. This
3099 * should expand inline functions, and evaluate
3100 * builtins.
3102 static int evaluate_symbol_call(struct expression *expr)
3104 struct expression *fn = expr->fn;
3105 struct symbol *ctype = fn->ctype;
3107 if (fn->type != EXPR_PREOP)
3108 return 0;
3110 if (ctype->op && ctype->op->evaluate)
3111 return ctype->op->evaluate(expr);
3113 return 0;
3116 static struct symbol *evaluate_call(struct expression *expr)
3118 int args, fnargs;
3119 struct symbol *ctype, *sym;
3120 struct expression *fn = expr->fn;
3121 struct expression_list *arglist = expr->args;
3123 if (!evaluate_expression(fn))
3124 return NULL;
3125 sym = ctype = fn->ctype;
3126 if (ctype->type == SYM_NODE)
3127 ctype = ctype->ctype.base_type;
3128 if (ctype->type == SYM_PTR)
3129 ctype = get_base_type(ctype);
3131 if (ctype->type != SYM_FN) {
3132 struct expression *arg;
3134 if (fn->ctype == &bad_ctype)
3135 return NULL;
3137 expression_error(expr, "not a function %s",
3138 show_ident(sym->ident));
3139 /* do typechecking in arguments */
3140 FOR_EACH_PTR (arglist, arg) {
3141 evaluate_expression(arg);
3142 } END_FOR_EACH_PTR(arg);
3143 return NULL;
3146 examine_fn_arguments(ctype);
3147 if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
3148 sym->op && sym->op->args) {
3149 if (!sym->op->args(expr))
3150 return NULL;
3151 } else {
3152 if (!evaluate_arguments(ctype, arglist))
3153 return NULL;
3154 args = expression_list_size(expr->args);
3155 fnargs = symbol_list_size(ctype->arguments);
3156 if (args < fnargs) {
3157 expression_error(expr,
3158 "not enough arguments for function %s",
3159 show_ident(sym->ident));
3160 return NULL;
3162 if (args > fnargs && !ctype->variadic)
3163 expression_error(expr,
3164 "too many arguments for function %s",
3165 show_ident(sym->ident));
3167 expr->ctype = ctype->ctype.base_type;
3168 if (sym->type == SYM_NODE) {
3169 if (evaluate_symbol_call(expr))
3170 return expr->ctype;
3172 return expr->ctype;
3175 static struct symbol *evaluate_offsetof(struct expression *expr)
3177 struct expression *e = expr->down;
3178 struct symbol *ctype = expr->in;
3179 int class;
3181 if (expr->op == '.') {
3182 struct symbol *field;
3183 int offset = 0;
3184 if (!ctype) {
3185 expression_error(expr, "expected structure or union");
3186 return NULL;
3188 examine_symbol_type(ctype);
3189 class = classify_type(ctype, &ctype);
3190 if (class != TYPE_COMPOUND) {
3191 expression_error(expr, "expected structure or union");
3192 return NULL;
3195 field = find_identifier(expr->ident, ctype->symbol_list, &offset);
3196 if (!field) {
3197 expression_error(expr, "unknown member");
3198 return NULL;
3200 ctype = field;
3201 expr->type = EXPR_VALUE;
3202 expr->flags = CEF_SET_ICE;
3203 expr->value = offset;
3204 expr->taint = 0;
3205 expr->ctype = size_t_ctype;
3206 } else {
3207 if (!ctype) {
3208 expression_error(expr, "expected structure or union");
3209 return NULL;
3211 examine_symbol_type(ctype);
3212 class = classify_type(ctype, &ctype);
3213 if (class != (TYPE_COMPOUND | TYPE_PTR)) {
3214 expression_error(expr, "expected array");
3215 return NULL;
3217 ctype = ctype->ctype.base_type;
3218 if (!expr->index) {
3219 expr->type = EXPR_VALUE;
3220 expr->flags = CEF_SET_ICE;
3221 expr->value = 0;
3222 expr->taint = 0;
3223 expr->ctype = size_t_ctype;
3224 } else {
3225 struct expression *idx = expr->index, *m;
3226 struct symbol *i_type = evaluate_expression(idx);
3227 unsigned old_idx_flags;
3228 int i_class = classify_type(i_type, &i_type);
3230 if (!is_int(i_class)) {
3231 expression_error(expr, "non-integer index");
3232 return NULL;
3234 unrestrict(idx, i_class, &i_type);
3235 old_idx_flags = idx->flags;
3236 idx = cast_to(idx, size_t_ctype);
3237 idx->flags = old_idx_flags;
3238 m = alloc_const_expression(expr->pos,
3239 bits_to_bytes(ctype->bit_size));
3240 m->ctype = size_t_ctype;
3241 m->flags = CEF_SET_INT;
3242 expr->type = EXPR_BINOP;
3243 expr->left = idx;
3244 expr->right = m;
3245 expr->op = '*';
3246 expr->ctype = size_t_ctype;
3247 expr->flags = m->flags & idx->flags & ~CEF_CONST_MASK;
3250 if (e) {
3251 struct expression *copy = __alloc_expression(0);
3252 *copy = *expr;
3253 if (e->type == EXPR_OFFSETOF)
3254 e->in = ctype;
3255 if (!evaluate_expression(e))
3256 return NULL;
3257 expr->type = EXPR_BINOP;
3258 expr->flags = e->flags & copy->flags & ~CEF_CONST_MASK;
3259 expr->op = '+';
3260 expr->ctype = size_t_ctype;
3261 expr->left = copy;
3262 expr->right = e;
3264 return size_t_ctype;
3267 static void check_label_declaration(struct position pos, struct symbol *label)
3269 switch (label->namespace) {
3270 case NS_LABEL:
3271 if (label->stmt)
3272 break;
3273 sparse_error(pos, "label '%s' was not declared", show_ident(label->ident));
3274 /* fallthrough */
3275 case NS_NONE:
3276 current_fn->bogus_linear = 1;
3277 default:
3278 break;
3282 static int type_selection(struct symbol *ctrl, struct symbol *type)
3284 struct ctype c = { .base_type = ctrl };
3285 struct ctype t = { .base_type = type };
3287 return !type_difference(&c, &t, 0, 0);
3290 static struct symbol *evaluate_generic_selection(struct expression *expr)
3292 struct type_expression *map;
3293 struct expression *res;
3294 struct symbol source;
3295 struct symbol *ctrl;
3297 if (!evaluate_expression(expr->control))
3298 return NULL;
3299 if (!(ctrl = degenerate(expr->control)))
3300 return NULL;
3302 source = *ctrl;
3303 source.ctype.modifiers &= ~(MOD_QUALIFIER|MOD_ATOMIC);
3304 for (map = expr->map; map; map = map->next) {
3305 struct symbol *stype = map->type;
3306 struct symbol *base;
3308 if (!evaluate_symbol(stype))
3309 continue;
3311 base = stype->ctype.base_type;
3312 if (base->type == SYM_ARRAY && base->array_size) {
3313 get_expression_value_silent(base->array_size);
3314 if (base->array_size->type == EXPR_VALUE)
3315 continue;
3316 sparse_error(stype->pos, "variable length array type in generic selection");
3317 continue;
3319 if (is_func_type(stype)) {
3320 sparse_error(stype->pos, "function type in generic selection");
3321 continue;
3323 if (stype->bit_size <= 0 || is_void_type(stype)) {
3324 sparse_error(stype->pos, "incomplete type in generic selection");
3325 continue;
3327 if (!type_selection(&source, stype))
3328 continue;
3330 res = map->expr;
3331 goto found;
3333 res = expr->def;
3334 if (!res) {
3335 sparse_error(expr->pos, "no generic selection for '%s'", show_typename(ctrl));
3336 return NULL;
3339 found:
3340 *expr = *res;
3341 return evaluate_expression(expr);
3344 struct symbol *evaluate_expression(struct expression *expr)
3346 if (!expr)
3347 return NULL;
3348 if (expr->ctype)
3349 return expr->ctype;
3351 switch (expr->type) {
3352 case EXPR_VALUE:
3353 case EXPR_FVALUE:
3354 expression_error(expr, "value expression without a type");
3355 return NULL;
3356 case EXPR_STRING:
3357 return evaluate_string(expr);
3358 case EXPR_SYMBOL:
3359 return evaluate_symbol_expression(expr);
3360 case EXPR_BINOP:
3361 evaluate_expression(expr->left);
3362 evaluate_expression(expr->right);
3363 if (!valid_subexpr_type(expr))
3364 return NULL;
3365 return evaluate_binop(expr);
3366 case EXPR_LOGICAL:
3367 return evaluate_logical(expr);
3368 case EXPR_COMMA:
3369 evaluate_expression(expr->left);
3370 if (!evaluate_expression(expr->right))
3371 return NULL;
3372 return evaluate_comma(expr);
3373 case EXPR_COMPARE:
3374 evaluate_expression(expr->left);
3375 evaluate_expression(expr->right);
3376 if (!valid_subexpr_type(expr))
3377 return NULL;
3378 return evaluate_compare(expr);
3379 case EXPR_ASSIGNMENT:
3380 evaluate_expression(expr->left);
3381 evaluate_expression(expr->right);
3382 if (!valid_subexpr_type(expr))
3383 return NULL;
3384 return evaluate_assignment(expr);
3385 case EXPR_PREOP:
3386 if (!evaluate_expression(expr->unop))
3387 return NULL;
3388 return evaluate_preop(expr);
3389 case EXPR_POSTOP:
3390 if (!evaluate_expression(expr->unop))
3391 return NULL;
3392 return evaluate_postop(expr);
3393 case EXPR_CAST:
3394 case EXPR_FORCE_CAST:
3395 case EXPR_IMPLIED_CAST:
3396 return evaluate_cast(expr);
3397 case EXPR_SIZEOF:
3398 return evaluate_sizeof(expr);
3399 case EXPR_PTRSIZEOF:
3400 return evaluate_ptrsizeof(expr);
3401 case EXPR_ALIGNOF:
3402 return evaluate_alignof(expr);
3403 case EXPR_DEREF:
3404 return evaluate_member_dereference(expr);
3405 case EXPR_CALL:
3406 return evaluate_call(expr);
3407 case EXPR_SELECT:
3408 case EXPR_CONDITIONAL:
3409 return evaluate_conditional_expression(expr);
3410 case EXPR_STATEMENT:
3411 expr->ctype = evaluate_statement(expr->statement);
3412 return expr->ctype;
3414 case EXPR_LABEL:
3415 expr->ctype = &ptr_ctype;
3416 check_label_declaration(expr->pos, expr->label_symbol);
3417 return &ptr_ctype;
3419 case EXPR_TYPE:
3420 /* Evaluate the type of the symbol .. */
3421 evaluate_symbol(expr->symbol);
3422 /* .. but the type of the _expression_ is a "type" */
3423 expr->ctype = &type_ctype;
3424 return &type_ctype;
3426 case EXPR_OFFSETOF:
3427 return evaluate_offsetof(expr);
3429 case EXPR_GENERIC:
3430 return evaluate_generic_selection(expr);
3432 /* These can not exist as stand-alone expressions */
3433 case EXPR_INITIALIZER:
3434 case EXPR_IDENTIFIER:
3435 case EXPR_INDEX:
3436 case EXPR_POS:
3437 expression_error(expr, "internal front-end error: initializer in expression");
3438 return NULL;
3439 case EXPR_SLICE:
3440 expression_error(expr, "internal front-end error: SLICE re-evaluated");
3441 return NULL;
3443 return NULL;
3446 void check_duplicates(struct symbol *sym)
3448 int declared = 0;
3449 struct symbol *next = sym;
3450 int initialized = sym->initializer != NULL;
3452 while ((next = next->same_symbol) != NULL) {
3453 const char *typediff;
3454 evaluate_symbol(next);
3455 if (initialized && next->initializer) {
3456 sparse_error(sym->pos, "symbol '%s' has multiple initializers (originally initialized at %s:%d)",
3457 show_ident(sym->ident),
3458 stream_name(next->pos.stream), next->pos.line);
3459 /* Only warn once */
3460 initialized = 0;
3462 declared++;
3463 typediff = type_difference(&sym->ctype, &next->ctype, 0, 0);
3464 if (typediff) {
3465 sparse_error(sym->pos, "symbol '%s' redeclared with different type (%s):",
3466 show_ident(sym->ident), typediff);
3467 info(sym->pos, " %s", show_typename(sym));
3468 info(next->pos, "note: previously declared as:");
3469 info(next->pos, " %s", show_typename(next));
3470 return;
3473 if (!declared) {
3474 unsigned long mod = sym->ctype.modifiers;
3475 if (mod & (MOD_STATIC | MOD_REGISTER | MOD_EXT_VISIBLE))
3476 return;
3477 if (!(mod & MOD_TOPLEVEL))
3478 return;
3479 if (!Wdecl)
3480 return;
3481 if (sym->ident == &main_ident)
3482 return;
3483 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
3487 static struct symbol *evaluate_symbol(struct symbol *sym)
3489 struct symbol *base_type;
3491 if (!sym)
3492 return sym;
3493 if (sym->evaluated)
3494 return sym;
3495 sym->evaluated = 1;
3497 sym = examine_symbol_type(sym);
3498 base_type = get_base_type(sym);
3499 if (!base_type)
3500 return NULL;
3502 /* Evaluate the initializers */
3503 if (sym->initializer)
3504 evaluate_initializer(sym, &sym->initializer);
3506 /* And finally, evaluate the body of the symbol too */
3507 if (base_type->type == SYM_FN) {
3508 struct symbol *curr = current_fn;
3510 if (sym->definition && sym->definition != sym)
3511 return evaluate_symbol(sym->definition);
3513 current_fn = sym;
3515 examine_fn_arguments(base_type);
3516 if (!base_type->stmt && base_type->inline_stmt)
3517 uninline(sym);
3518 if (base_type->stmt)
3519 evaluate_statement(base_type->stmt);
3521 current_fn = curr;
3524 return base_type;
3527 void evaluate_symbol_list(struct symbol_list *list)
3529 struct symbol *sym;
3531 FOR_EACH_PTR(list, sym) {
3532 has_error &= ~ERROR_CURR_PHASE;
3533 evaluate_symbol(sym);
3534 check_duplicates(sym);
3535 } END_FOR_EACH_PTR(sym);
3538 static struct symbol *evaluate_return_expression(struct statement *stmt)
3540 struct expression *expr = stmt->expression;
3541 struct symbol *fntype, *rettype;
3543 evaluate_expression(expr);
3544 fntype = current_fn->ctype.base_type;
3545 rettype = fntype->ctype.base_type;
3546 if (!rettype || rettype == &void_ctype) {
3547 if (expr && expr->ctype != &void_ctype)
3548 expression_error(expr, "return expression in %s function", rettype?"void":"typeless");
3549 if (expr && Wreturn_void)
3550 warning(stmt->pos, "returning void-valued expression");
3551 return NULL;
3554 if (!expr) {
3555 sparse_error(stmt->pos, "return with no return value");
3556 return NULL;
3558 if (!expr->ctype)
3559 return NULL;
3560 compatible_assignment_types(expr, rettype, &stmt->expression, "return expression");
3561 return NULL;
3564 static void evaluate_if_statement(struct statement *stmt)
3566 if (!stmt->if_conditional)
3567 return;
3569 evaluate_conditional(stmt->if_conditional, 0);
3570 evaluate_statement(stmt->if_true);
3571 evaluate_statement(stmt->if_false);
3574 static void evaluate_iterator(struct statement *stmt)
3576 evaluate_symbol_list(stmt->iterator_syms);
3577 evaluate_conditional(stmt->iterator_pre_condition, 1);
3578 evaluate_conditional(stmt->iterator_post_condition,1);
3579 evaluate_statement(stmt->iterator_pre_statement);
3580 evaluate_statement(stmt->iterator_statement);
3581 evaluate_statement(stmt->iterator_post_statement);
3585 static void parse_asm_constraint(struct asm_operand *op)
3587 struct expression *constraint = op->constraint;
3588 const char *str = constraint->string->data;
3589 int c;
3591 switch (str[0]) {
3592 case '\0':
3593 sparse_error(constraint->pos, "invalid ASM constraint (\"\")");
3594 break;
3595 case '+':
3596 op->is_modify = true;
3597 /* fall-through */
3598 case '=':
3599 op->is_assign = true;
3600 str++;
3601 break;
3604 while ((c = *str++)) {
3605 switch (c) {
3606 case '=':
3607 case '+':
3608 sparse_error(constraint->pos, "invalid ASM constraint '%c'", c);
3609 break;
3611 case '&':
3612 op->is_earlyclobber = true;
3613 break;
3614 case '%':
3615 op->is_commutative = true;
3616 break;
3617 case 'r':
3618 op->is_register = true;
3619 break;
3621 case 'm':
3622 case 'o':
3623 case 'V':
3624 case 'Q':
3625 op->is_memory = true;
3626 break;
3628 case '<':
3629 case '>':
3630 // FIXME: ignored for now
3631 break;
3633 case ',':
3634 // FIXME: multiple alternative constraints
3635 break;
3637 case '0' ... '9':
3638 // FIXME: numeric matching constraint?
3639 break;
3640 case '[':
3641 // FIXME: symbolic matching constraint
3642 return;
3644 default:
3645 if (arch_target->asm_constraint)
3646 str = arch_target->asm_constraint(op, c, str);
3648 // FIXME: multi-letter constraints
3649 break;
3653 // FIXME: how to deal with multi-constraint?
3654 if (op->is_register)
3655 op->is_memory = 0;
3658 static void verify_output_constraint(struct asm_operand *op)
3660 struct expression *expr = op->constraint;
3661 const char *constraint = expr->string->data;
3663 if (!op->is_assign)
3664 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
3667 static void verify_input_constraint(struct asm_operand *op)
3669 struct expression *expr = op->constraint;
3670 const char *constraint = expr->string->data;
3672 if (op->is_assign)
3673 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
3676 static void evaluate_asm_memop(struct asm_operand *op)
3678 if (op->is_memory) {
3679 struct expression *expr = op->expr;
3680 struct expression *addr;
3682 // implicit addressof
3683 addr = alloc_expression(expr->pos, EXPR_PREOP);
3684 addr->op = '&';
3685 addr->unop = expr;
3687 evaluate_addressof(addr);
3688 op->expr = addr;
3689 } else {
3690 evaluate_expression(op->expr);
3691 degenerate(op->expr);
3695 static void evaluate_asm_statement(struct statement *stmt)
3697 struct expression *expr;
3698 struct asm_operand *op;
3699 struct symbol *sym;
3701 if (!stmt->asm_string)
3702 return;
3704 FOR_EACH_PTR(stmt->asm_outputs, op) {
3705 /* Identifier */
3707 /* Constraint */
3708 if (op->constraint) {
3709 parse_asm_constraint(op);
3710 verify_output_constraint(op);
3713 /* Expression */
3714 expr = op->expr;
3715 if (!evaluate_expression(expr))
3716 return;
3717 if (!lvalue_expression(expr))
3718 warning(expr->pos, "asm output is not an lvalue");
3719 evaluate_assign_to(expr, expr->ctype);
3720 evaluate_asm_memop(op);
3721 } END_FOR_EACH_PTR(op);
3723 FOR_EACH_PTR(stmt->asm_inputs, op) {
3724 /* Identifier */
3726 /* Constraint */
3727 if (op->constraint) {
3728 parse_asm_constraint(op);
3729 verify_input_constraint(op);
3732 /* Expression */
3733 if (!evaluate_expression(op->expr))
3734 return;
3735 evaluate_asm_memop(op);
3736 } END_FOR_EACH_PTR(op);
3738 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
3739 if (!expr) {
3740 sparse_error(stmt->pos, "bad asm clobbers");
3741 return;
3743 if (expr->type == EXPR_STRING)
3744 continue;
3745 expression_error(expr, "asm clobber is not a string");
3746 } END_FOR_EACH_PTR(expr);
3748 FOR_EACH_PTR(stmt->asm_labels, sym) {
3749 if (!sym || sym->type != SYM_LABEL) {
3750 sparse_error(stmt->pos, "bad asm label");
3751 return;
3753 } END_FOR_EACH_PTR(sym);
3756 static void evaluate_case_statement(struct statement *stmt)
3758 evaluate_expression(stmt->case_expression);
3759 evaluate_expression(stmt->case_to);
3760 evaluate_statement(stmt->case_statement);
3763 static void check_case_type(struct expression *switch_expr,
3764 struct expression *case_expr,
3765 struct expression **enumcase)
3767 struct symbol *switch_type, *case_type;
3768 int sclass, cclass;
3770 if (!case_expr)
3771 return;
3773 switch_type = switch_expr->ctype;
3774 case_type = evaluate_expression(case_expr);
3776 if (!switch_type || !case_type)
3777 goto Bad;
3778 if (enumcase) {
3779 if (*enumcase)
3780 warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
3781 else if (is_enum_type(case_type))
3782 *enumcase = case_expr;
3785 sclass = classify_type(switch_type, &switch_type);
3786 cclass = classify_type(case_type, &case_type);
3788 /* both should be arithmetic */
3789 if (!(sclass & cclass & TYPE_NUM))
3790 goto Bad;
3792 /* neither should be floating */
3793 if ((sclass | cclass) & TYPE_FLOAT)
3794 goto Bad;
3796 /* if neither is restricted, we are OK */
3797 if (!((sclass | cclass) & TYPE_RESTRICT))
3798 return;
3800 if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
3801 cclass, sclass, case_type, switch_type)) {
3802 unrestrict(case_expr, cclass, &case_type);
3803 unrestrict(switch_expr, sclass, &switch_type);
3805 return;
3807 Bad:
3808 expression_error(case_expr, "incompatible types for 'case' statement");
3811 static void evaluate_switch_statement(struct statement *stmt)
3813 struct symbol *sym;
3814 struct expression *enumcase = NULL;
3815 struct expression **enumcase_holder = &enumcase;
3816 struct expression *sel = stmt->switch_expression;
3818 evaluate_expression(sel);
3819 evaluate_statement(stmt->switch_statement);
3820 if (!sel)
3821 return;
3822 if (sel->ctype && is_enum_type(sel->ctype))
3823 enumcase_holder = NULL; /* Only check cases against switch */
3825 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
3826 struct statement *case_stmt = sym->stmt;
3827 check_case_type(sel, case_stmt->case_expression, enumcase_holder);
3828 check_case_type(sel, case_stmt->case_to, enumcase_holder);
3829 } END_FOR_EACH_PTR(sym);
3832 static void evaluate_goto_statement(struct statement *stmt)
3834 struct symbol *label = stmt->goto_label;
3836 if (!label) {
3837 // no label associated, may be a computed goto
3838 evaluate_expression(stmt->goto_expression);
3839 return;
3842 check_label_declaration(stmt->pos, label);
3845 struct symbol *evaluate_statement(struct statement *stmt)
3847 if (!stmt)
3848 return NULL;
3850 switch (stmt->type) {
3851 case STMT_DECLARATION: {
3852 struct symbol *s;
3853 FOR_EACH_PTR(stmt->declaration, s) {
3854 evaluate_symbol(s);
3855 } END_FOR_EACH_PTR(s);
3856 return NULL;
3859 case STMT_RETURN:
3860 return evaluate_return_expression(stmt);
3862 case STMT_EXPRESSION:
3863 if (!evaluate_expression(stmt->expression))
3864 return NULL;
3865 if (stmt->expression->ctype == &null_ctype)
3866 stmt->expression = cast_to(stmt->expression, &ptr_ctype);
3867 return degenerate(stmt->expression);
3869 case STMT_COMPOUND: {
3870 struct statement *s;
3871 struct symbol *type = NULL;
3873 /* Evaluate the return symbol in the compound statement */
3874 evaluate_symbol(stmt->ret);
3877 * Then, evaluate each statement, making the type of the
3878 * compound statement be the type of the last statement
3880 type = evaluate_statement(stmt->args);
3881 FOR_EACH_PTR(stmt->stmts, s) {
3882 type = evaluate_statement(s);
3883 } END_FOR_EACH_PTR(s);
3884 if (!type)
3885 type = &void_ctype;
3886 return type;
3888 case STMT_IF:
3889 evaluate_if_statement(stmt);
3890 return NULL;
3891 case STMT_ITERATOR:
3892 evaluate_iterator(stmt);
3893 return NULL;
3894 case STMT_SWITCH:
3895 evaluate_switch_statement(stmt);
3896 return NULL;
3897 case STMT_CASE:
3898 evaluate_case_statement(stmt);
3899 return NULL;
3900 case STMT_LABEL:
3901 return evaluate_statement(stmt->label_statement);
3902 case STMT_GOTO:
3903 evaluate_goto_statement(stmt);
3904 return NULL;
3905 case STMT_NONE:
3906 break;
3907 case STMT_ASM:
3908 evaluate_asm_statement(stmt);
3909 return NULL;
3910 case STMT_CONTEXT:
3911 evaluate_expression(stmt->expression);
3912 return NULL;
3913 case STMT_RANGE:
3914 evaluate_expression(stmt->range_expression);
3915 evaluate_expression(stmt->range_low);
3916 evaluate_expression(stmt->range_high);
3917 return NULL;
3919 return NULL;