4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
9 * Evaluate constant expressions.
27 #include "expression.h"
29 struct symbol
*current_fn
;
31 static struct symbol
*degenerate(struct expression
*expr
);
32 static struct symbol
*evaluate_symbol(struct symbol
*sym
);
34 static struct symbol
*evaluate_symbol_expression(struct expression
*expr
)
36 struct expression
*addr
;
37 struct symbol
*sym
= expr
->symbol
;
38 struct symbol
*base_type
;
41 sparse_error(expr
->pos
, "undefined identifier '%s'", show_ident(expr
->symbol_name
));
45 examine_symbol_type(sym
);
47 base_type
= get_base_type(sym
);
49 sparse_error(expr
->pos
, "identifier '%s' has no type", show_ident(expr
->symbol_name
));
53 addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
55 addr
->symbol_name
= expr
->symbol_name
;
56 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
57 expr
->type
= EXPR_PREOP
;
61 /* The type of a symbol is the symbol itself! */
66 static struct symbol
*evaluate_string(struct expression
*expr
)
68 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
69 struct symbol
*array
= alloc_symbol(expr
->pos
, SYM_ARRAY
);
70 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
71 struct expression
*initstr
= alloc_expression(expr
->pos
, EXPR_STRING
);
72 unsigned int length
= expr
->string
->length
;
74 sym
->array_size
= alloc_const_expression(expr
->pos
, length
);
75 sym
->bit_size
= bits_in_char
* length
;
76 sym
->ctype
.alignment
= 1;
77 sym
->ctype
.modifiers
= MOD_STATIC
;
78 sym
->ctype
.base_type
= array
;
79 sym
->initializer
= initstr
;
82 initstr
->string
= expr
->string
;
84 array
->array_size
= sym
->array_size
;
85 array
->bit_size
= bits_in_char
* length
;
86 array
->ctype
.alignment
= 1;
87 array
->ctype
.modifiers
= MOD_STATIC
;
88 array
->ctype
.base_type
= &char_ctype
;
91 addr
->ctype
= &lazy_ptr_ctype
;
93 expr
->type
= EXPR_PREOP
;
100 static inline struct symbol
*integer_promotion(struct symbol
*type
)
102 struct symbol
*orig_type
= type
;
103 unsigned long mod
= type
->ctype
.modifiers
;
106 if (type
->type
== SYM_NODE
)
107 type
= type
->ctype
.base_type
;
108 if (type
->type
== SYM_ENUM
)
109 type
= type
->ctype
.base_type
;
110 width
= type
->bit_size
;
113 * Bitfields always promote to the base type,
114 * even if the bitfield might be bigger than
117 if (type
->type
== SYM_BITFIELD
) {
118 type
= type
->ctype
.base_type
;
121 mod
= type
->ctype
.modifiers
;
122 if (width
< bits_in_int
)
125 /* If char/short has as many bits as int, it still gets "promoted" */
126 if (mod
& (MOD_CHAR
| MOD_SHORT
)) {
127 if (mod
& MOD_UNSIGNED
)
135 * integer part of usual arithmetic conversions:
136 * integer promotions are applied
137 * if left and right are identical, we are done
138 * if signedness is the same, convert one with lower rank
139 * unless unsigned argument has rank lower than signed one, convert the
141 * if signed argument is bigger than unsigned one, convert the unsigned.
142 * otherwise, convert signed.
144 * Leaving aside the integer promotions, that is equivalent to
145 * if identical, don't convert
146 * if left is bigger than right, convert right
147 * if right is bigger than left, convert right
148 * otherwise, if signedness is the same, convert one with lower rank
149 * otherwise convert the signed one.
151 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
153 unsigned long lmod
, rmod
;
155 left
= integer_promotion(left
);
156 right
= integer_promotion(right
);
161 if (left
->bit_size
> right
->bit_size
)
164 if (right
->bit_size
> left
->bit_size
)
167 lmod
= left
->ctype
.modifiers
;
168 rmod
= right
->ctype
.modifiers
;
169 if ((lmod
^ rmod
) & MOD_UNSIGNED
) {
170 if (lmod
& MOD_UNSIGNED
)
172 } else if ((lmod
& ~rmod
) & (MOD_LONG
| MOD_LONGLONG
))
180 static int same_cast_type(struct symbol
*orig
, struct symbol
*new)
182 return orig
->bit_size
== new->bit_size
&& orig
->bit_offset
== orig
->bit_offset
;
185 static struct symbol
*base_type(struct symbol
*node
, unsigned long *modp
, unsigned long *asp
)
187 unsigned long mod
, as
;
191 mod
|= node
->ctype
.modifiers
;
192 as
|= node
->ctype
.as
;
193 if (node
->type
== SYM_NODE
) {
194 node
= node
->ctype
.base_type
;
199 *modp
= mod
& ~MOD_IGNORE
;
204 static int is_same_type(struct expression
*expr
, struct symbol
*new)
206 struct symbol
*old
= expr
->ctype
;
207 unsigned long oldmod
, newmod
, oldas
, newas
;
209 old
= base_type(old
, &oldmod
, &oldas
);
210 new = base_type(new, &newmod
, &newas
);
212 /* Same base type, same address space? */
213 if (old
== new && oldas
== newas
) {
214 unsigned long difmod
;
216 /* Check the modifier bits. */
217 difmod
= (oldmod
^ newmod
) & ~MOD_NOCAST
;
219 /* Exact same type? */
224 * Not the same type, but differs only in "const".
225 * Don't warn about MOD_NOCAST.
227 if (difmod
== MOD_CONST
)
230 if ((oldmod
| newmod
) & MOD_NOCAST
) {
231 const char *tofrom
= "to/from";
232 if (!(newmod
& MOD_NOCAST
))
234 if (!(oldmod
& MOD_NOCAST
))
236 warning(expr
->pos
, "implicit cast %s nocast type", tofrom
);
242 warn_for_different_enum_types (struct position pos
,
243 struct symbol
*typea
,
244 struct symbol
*typeb
)
248 if (typea
->type
== SYM_NODE
)
249 typea
= typea
->ctype
.base_type
;
250 if (typeb
->type
== SYM_NODE
)
251 typeb
= typeb
->ctype
.base_type
;
256 if (typea
->type
== SYM_ENUM
&& typeb
->type
== SYM_ENUM
)
257 warning(pos
, "mixing different enum types");
261 * This gets called for implicit casts in assignments and
262 * integer promotion. We often want to try to move the
263 * cast down, because the ops involved may have been
264 * implicitly cast up, and we can get rid of the casts
267 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
269 struct expression
*expr
;
271 warn_for_different_enum_types (old
->pos
, old
->ctype
, type
);
273 if (is_same_type(old
, type
))
277 * See if we can simplify the op. Move the cast down.
281 if (old
->ctype
->bit_size
< type
->bit_size
)
283 if (old
->op
== '~') {
285 old
->unop
= cast_to(old
->unop
, type
);
290 case EXPR_IMPLIED_CAST
:
291 warn_for_different_enum_types(old
->pos
, old
->ctype
, type
);
293 if (old
->ctype
->bit_size
>= type
->bit_size
) {
294 struct expression
*orig
= old
->cast_expression
;
295 if (same_cast_type(orig
->ctype
, type
))
297 if (old
->ctype
->bit_offset
== type
->bit_offset
) {
299 old
->cast_type
= type
;
309 expr
= alloc_expression(old
->pos
, EXPR_IMPLIED_CAST
);
311 expr
->cast_type
= type
;
312 expr
->cast_expression
= old
;
316 static int is_type_type(struct symbol
*type
)
318 return (type
->ctype
.modifiers
& MOD_TYPE
) != 0;
321 int is_ptr_type(struct symbol
*type
)
323 if (type
->type
== SYM_NODE
)
324 type
= type
->ctype
.base_type
;
325 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
328 static inline int is_float_type(struct symbol
*type
)
330 if (type
->type
== SYM_NODE
)
331 type
= type
->ctype
.base_type
;
332 return type
->ctype
.base_type
== &fp_type
;
335 static inline int is_byte_type(struct symbol
*type
)
337 return type
->bit_size
== bits_in_char
&& type
->type
!= SYM_BITFIELD
;
349 static inline int classify_type(struct symbol
*type
, struct symbol
**base
)
351 static int type_class
[SYM_BAD
+ 1] = {
352 [SYM_PTR
] = TYPE_PTR
,
354 [SYM_ARRAY
] = TYPE_PTR
| TYPE_COMPOUND
,
355 [SYM_STRUCT
] = TYPE_COMPOUND
,
356 [SYM_UNION
] = TYPE_COMPOUND
,
357 [SYM_BITFIELD
] = TYPE_NUM
| TYPE_BITFIELD
,
358 [SYM_RESTRICT
] = TYPE_NUM
| TYPE_RESTRICT
,
360 if (type
->type
== SYM_NODE
)
361 type
= type
->ctype
.base_type
;
362 if (type
->type
== SYM_ENUM
)
363 type
= type
->ctype
.base_type
;
365 if (type
->type
== SYM_BASETYPE
) {
366 if (type
->ctype
.base_type
== &int_type
)
368 if (type
->ctype
.base_type
== &fp_type
)
369 return TYPE_NUM
| TYPE_FLOAT
;
371 return type_class
[type
->type
];
374 static inline int is_string_type(struct symbol
*type
)
376 if (type
->type
== SYM_NODE
)
377 type
= type
->ctype
.base_type
;
378 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
381 static struct symbol
*bad_expr_type(struct expression
*expr
)
383 sparse_error(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
384 switch (expr
->type
) {
387 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
388 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
392 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
398 return expr
->ctype
= &bad_ctype
;
401 static struct symbol
*compatible_float_binop(struct expression
**lp
, struct expression
**rp
)
403 struct expression
*left
= *lp
, *right
= *rp
;
404 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
406 if (ltype
->type
== SYM_NODE
)
407 ltype
= ltype
->ctype
.base_type
;
408 if (rtype
->type
== SYM_NODE
)
409 rtype
= rtype
->ctype
.base_type
;
410 if (is_float_type(ltype
)) {
411 if (is_int_type(rtype
))
413 if (is_float_type(rtype
)) {
414 unsigned long lmod
= ltype
->ctype
.modifiers
;
415 unsigned long rmod
= rtype
->ctype
.modifiers
;
416 lmod
&= MOD_LONG
| MOD_LONGLONG
;
417 rmod
&= MOD_LONG
| MOD_LONGLONG
;
427 if (!is_float_type(rtype
) || !is_int_type(ltype
))
430 *lp
= cast_to(left
, rtype
);
433 *rp
= cast_to(right
, ltype
);
437 static struct symbol
*compatible_integer_binop(struct expression
**lp
, struct expression
**rp
)
439 struct expression
*left
= *lp
, *right
= *rp
;
440 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
442 if (ltype
->type
== SYM_NODE
)
443 ltype
= ltype
->ctype
.base_type
;
444 if (rtype
->type
== SYM_NODE
)
445 rtype
= rtype
->ctype
.base_type
;
446 if (is_int_type(ltype
) && is_int_type(rtype
)) {
447 struct symbol
*ctype
= bigger_int_type(ltype
, rtype
);
449 *lp
= cast_to(left
, ctype
);
450 *rp
= cast_to(right
, ctype
);
456 static int restricted_value(struct expression
*v
, struct symbol
*type
)
458 if (v
->type
!= EXPR_VALUE
)
465 static int restricted_binop(int op
, struct symbol
*type
)
474 case SPECIAL_NOTEQUAL
:
475 case SPECIAL_AND_ASSIGN
:
476 case SPECIAL_OR_ASSIGN
:
477 case SPECIAL_XOR_ASSIGN
:
484 static int restricted_unop(int op
, struct symbol
*type
)
486 if (op
== '~' && type
->bit_size
>= bits_in_int
)
493 static struct symbol
*compatible_restricted_binop(int op
, struct expression
**lp
, struct expression
**rp
)
495 struct expression
*left
= *lp
, *right
= *rp
;
496 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
497 struct symbol
*type
= NULL
;
499 if (ltype
->type
== SYM_NODE
)
500 ltype
= ltype
->ctype
.base_type
;
501 if (rtype
->type
== SYM_NODE
)
502 rtype
= rtype
->ctype
.base_type
;
504 warn_for_different_enum_types(right
->pos
, ltype
, rtype
);
506 if (ltype
->type
== SYM_ENUM
)
507 ltype
= ltype
->ctype
.base_type
;
508 if (rtype
->type
== SYM_ENUM
)
509 rtype
= rtype
->ctype
.base_type
;
511 if (is_restricted_type(ltype
)) {
512 if (is_restricted_type(rtype
)) {
516 if (!restricted_value(right
, ltype
))
519 } else if (is_restricted_type(rtype
)) {
520 if (!restricted_value(left
, rtype
))
525 if (restricted_binop(op
, type
))
530 static struct symbol
*evaluate_arith(struct expression
*expr
, int float_ok
)
532 struct symbol
*ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
533 if (!ctype
&& float_ok
)
534 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
536 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
541 return bad_expr_type(expr
);
544 static inline int lvalue_expression(struct expression
*expr
)
546 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
549 static int ptr_object_size(struct symbol
*ptr_type
)
551 if (ptr_type
->type
== SYM_NODE
)
552 ptr_type
= ptr_type
->ctype
.base_type
;
553 if (ptr_type
->type
== SYM_PTR
)
554 ptr_type
= get_base_type(ptr_type
);
555 return ptr_type
->bit_size
;
558 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*ctype
, struct expression
**ip
)
560 struct expression
*i
= *ip
;
561 struct symbol
*ptr_type
= ctype
;
564 if (ptr_type
->type
== SYM_NODE
)
565 ptr_type
= ptr_type
->ctype
.base_type
;
567 if (!is_int_type(i
->ctype
))
568 return bad_expr_type(expr
);
570 examine_symbol_type(ctype
);
572 if (!ctype
->ctype
.base_type
) {
573 sparse_error(expr
->pos
, "missing type information");
577 /* Get the size of whatever the pointer points to */
578 bit_size
= ptr_object_size(ctype
);
580 if (bit_size
> bits_in_char
) {
581 int multiply
= bit_size
>> 3;
582 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
584 if (i
->type
== EXPR_VALUE
) {
585 val
->value
= i
->value
* multiply
;
586 val
->ctype
= size_t_ctype
;
589 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
591 val
->ctype
= size_t_ctype
;
592 val
->value
= bit_size
>> 3;
595 mul
->ctype
= size_t_ctype
;
607 static struct symbol
*evaluate_add(struct expression
*expr
)
609 struct expression
*left
= expr
->left
, *right
= expr
->right
;
610 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
612 if (is_ptr_type(ltype
))
613 return evaluate_ptr_add(expr
, degenerate(left
), &expr
->right
);
615 if (is_ptr_type(rtype
))
616 return evaluate_ptr_add(expr
, degenerate(right
), &expr
->left
);
618 return evaluate_arith(expr
, 1);
621 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
622 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
625 unsigned long mod1
, mod2
, diff
;
626 unsigned long as1
, as2
;
628 struct symbol
*base1
, *base2
;
630 if (target
== source
)
632 if (!target
|| !source
)
633 return "different types";
635 * Peel of per-node information.
636 * FIXME! Check alignment and context too here!
638 mod1
= target
->ctype
.modifiers
;
639 as1
= target
->ctype
.as
;
640 mod2
= source
->ctype
.modifiers
;
641 as2
= source
->ctype
.as
;
642 if (target
->type
== SYM_NODE
) {
643 target
= target
->ctype
.base_type
;
646 if (target
->type
== SYM_PTR
) {
650 mod1
|= target
->ctype
.modifiers
;
651 as1
|= target
->ctype
.as
;
653 if (source
->type
== SYM_NODE
) {
654 source
= source
->ctype
.base_type
;
657 if (source
->type
== SYM_PTR
) {
661 mod2
|= source
->ctype
.modifiers
;
662 as2
|= source
->ctype
.as
;
664 if (target
->type
== SYM_ENUM
) {
665 target
= target
->ctype
.base_type
;
669 if (source
->type
== SYM_ENUM
) {
670 source
= source
->ctype
.base_type
;
675 if (target
== source
)
677 if (!target
|| !source
)
678 return "different types";
680 type1
= target
->type
;
681 base1
= target
->ctype
.base_type
;
683 type2
= source
->type
;
684 base2
= source
->ctype
.base_type
;
687 * Pointers to functions compare as the function itself
689 if (type1
== SYM_PTR
&& base1
) {
690 base1
= examine_symbol_type(base1
);
691 switch (base1
->type
) {
695 base1
= base1
->ctype
.base_type
;
700 if (type2
== SYM_PTR
&& base2
) {
701 base2
= examine_symbol_type(base2
);
702 switch (base2
->type
) {
706 base2
= base2
->ctype
.base_type
;
712 /* Arrays degenerate to pointers for type comparisons */
713 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
714 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
716 if (type1
!= type2
|| type1
== SYM_RESTRICT
)
717 return "different base types";
719 /* Must be same address space to be comparable */
720 if (Waddress_space
&& as1
!= as2
)
721 return "different address spaces";
723 /* Ignore differences in storage types or addressability */
724 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
725 diff
&= (mod1
& ~target_mod_ignore
) | (mod2
& ~source_mod_ignore
);
728 return "different type sizes";
729 if (diff
& ~MOD_SIGNEDNESS
)
730 return "different modifiers";
732 /* Differs in signedness only.. */
735 * Warn if both are explicitly signed ("unsigned" is obvously
736 * always explicit, and since we know one of them has to be
737 * unsigned, we check if the signed one was explicit).
739 if ((mod1
| mod2
) & MOD_EXPLICITLY_SIGNED
)
740 return "different explicit signedness";
743 * "char" matches both "unsigned char" and "signed char",
744 * so if the explicit test didn't trigger, then we should
745 * not warn about a char.
747 if (!(mod1
& MOD_CHAR
))
748 return "different signedness";
752 if (type1
== SYM_FN
) {
754 struct symbol
*arg1
, *arg2
;
755 if (base1
->variadic
!= base2
->variadic
)
756 return "incompatible variadic arguments";
757 PREPARE_PTR_LIST(target
->arguments
, arg1
);
758 PREPARE_PTR_LIST(source
->arguments
, arg2
);
762 diff
= type_difference(arg1
, arg2
, 0, 0);
764 static char argdiff
[80];
765 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
774 FINISH_PTR_LIST(arg2
);
775 FINISH_PTR_LIST(arg1
);
784 static int is_null_ptr(struct expression
*expr
)
786 if (expr
->type
!= EXPR_VALUE
|| expr
->value
)
788 if (!is_ptr_type(expr
->ctype
))
789 warning(expr
->pos
, "Using plain integer as NULL pointer");
793 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
795 /* NULL expression? Just return the type of the "other side" */
804 * Ignore differences in "volatile" and "const"ness when
805 * subtracting pointers
807 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
809 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
**rp
)
811 const char *typediff
;
812 struct symbol
*ctype
;
813 struct symbol
*ltype
, *rtype
;
814 struct expression
*r
= *rp
;
816 ltype
= degenerate(l
);
817 rtype
= degenerate(r
);
820 * If it is an integer subtract: the ptr add case will do the
823 if (!is_ptr_type(rtype
))
824 return evaluate_ptr_add(expr
, degenerate(l
), rp
);
827 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
829 ctype
= common_ptr_type(l
, r
);
831 sparse_error(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
835 examine_symbol_type(ctype
);
837 /* Figure out the base type we point to */
838 if (ctype
->type
== SYM_NODE
)
839 ctype
= ctype
->ctype
.base_type
;
840 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
841 sparse_error(expr
->pos
, "subtraction of functions? Share your drugs");
844 ctype
= get_base_type(ctype
);
846 expr
->ctype
= ssize_t_ctype
;
847 if (ctype
->bit_size
> bits_in_char
) {
848 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
849 struct expression
*div
= expr
;
850 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
851 unsigned long value
= ctype
->bit_size
>> 3;
853 val
->ctype
= size_t_ctype
;
856 if (value
& (value
-1)) {
857 if (Wptr_subtraction_blows
)
858 warning(expr
->pos
, "potentially expensive pointer subtraction");
862 sub
->ctype
= ssize_t_ctype
;
871 return ssize_t_ctype
;
874 static struct symbol
*evaluate_sub(struct expression
*expr
)
876 struct expression
*left
= expr
->left
;
877 struct symbol
*ltype
= left
->ctype
;
879 if (is_ptr_type(ltype
))
880 return evaluate_ptr_sub(expr
, left
, &expr
->right
);
882 return evaluate_arith(expr
, 1);
885 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
887 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
889 struct symbol
*ctype
;
894 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
895 warning(expr
->pos
, "assignment expression in conditional");
897 ctype
= evaluate_expression(expr
);
899 if (is_safe_type(ctype
))
900 warning(expr
->pos
, "testing a 'safe expression'");
906 static struct symbol
*evaluate_logical(struct expression
*expr
)
908 if (!evaluate_conditional(expr
->left
, 0))
910 if (!evaluate_conditional(expr
->right
, 0))
913 expr
->ctype
= &bool_ctype
;
917 static struct symbol
*evaluate_shift(struct expression
*expr
)
919 struct expression
*left
= expr
->left
, *right
= expr
->right
;
920 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
922 if (ltype
->type
== SYM_NODE
)
923 ltype
= ltype
->ctype
.base_type
;
924 if (rtype
->type
== SYM_NODE
)
925 rtype
= rtype
->ctype
.base_type
;
926 if (is_int_type(ltype
) && is_int_type(rtype
)) {
927 struct symbol
*ctype
= integer_promotion(ltype
);
928 expr
->left
= cast_to(expr
->left
, ctype
);
930 ctype
= integer_promotion(rtype
);
931 expr
->right
= cast_to(expr
->right
, ctype
);
934 return bad_expr_type(expr
);
937 static struct symbol
*evaluate_binop(struct expression
*expr
)
940 // addition can take ptr+int, fp and int
942 return evaluate_add(expr
);
944 // subtraction can take ptr-ptr, fp and int
946 return evaluate_sub(expr
);
948 // Arithmetic operations can take fp and int
950 return evaluate_arith(expr
, 1);
952 // shifts do integer promotions, but that's it.
953 case SPECIAL_LEFTSHIFT
: case SPECIAL_RIGHTSHIFT
:
954 return evaluate_shift(expr
);
956 // The rest are integer operations
957 // '%', '&', '^', '|'
959 return evaluate_arith(expr
, 0);
963 static struct symbol
*evaluate_comma(struct expression
*expr
)
965 expr
->ctype
= expr
->right
->ctype
;
969 static int modify_for_unsigned(int op
)
972 op
= SPECIAL_UNSIGNED_LT
;
974 op
= SPECIAL_UNSIGNED_GT
;
975 else if (op
== SPECIAL_LTE
)
976 op
= SPECIAL_UNSIGNED_LTE
;
977 else if (op
== SPECIAL_GTE
)
978 op
= SPECIAL_UNSIGNED_GTE
;
982 static struct symbol
*evaluate_compare(struct expression
*expr
)
984 struct expression
*left
= expr
->left
, *right
= expr
->right
;
985 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
986 struct symbol
*ctype
;
989 if (is_type_type(ltype
) && is_type_type(rtype
))
992 if (is_safe_type(ltype
) || is_safe_type(rtype
))
993 warning(expr
->pos
, "testing a 'safe expression'");
996 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
997 // FIXME! Check the types for compatibility
998 expr
->op
= modify_for_unsigned(expr
->op
);
1002 ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
1004 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
1005 expr
->op
= modify_for_unsigned(expr
->op
);
1009 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
1013 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
1015 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
1016 expr
->op
= modify_for_unsigned(expr
->op
);
1020 bad_expr_type(expr
);
1023 expr
->ctype
= &bool_ctype
;
1028 * FIXME!! This should do casts, array degeneration etc..
1030 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
1032 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
1034 if (ltype
->type
== SYM_NODE
)
1035 ltype
= ltype
->ctype
.base_type
;
1037 if (rtype
->type
== SYM_NODE
)
1038 rtype
= rtype
->ctype
.base_type
;
1040 if (ltype
->type
== SYM_PTR
) {
1041 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
1045 if (rtype
->type
== SYM_PTR
) {
1046 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
1053 * NOTE! The degenerate case of "x ? : y", where we don't
1054 * have a true case, this will possibly promote "x" to the
1055 * same type as "y", and thus _change_ the conditional
1056 * test in the expression. But since promotion is "safe"
1057 * for testing, that's ok.
1059 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
1061 struct expression
**true;
1062 struct symbol
*ctype
, *ltype
, *rtype
;
1063 const char * typediff
;
1065 if (!evaluate_conditional(expr
->conditional
, 0))
1067 if (!evaluate_expression(expr
->cond_false
))
1070 ctype
= degenerate(expr
->conditional
);
1071 rtype
= degenerate(expr
->cond_false
);
1073 true = &expr
->conditional
;
1075 if (expr
->cond_true
) {
1076 if (!evaluate_expression(expr
->cond_true
))
1078 ltype
= degenerate(expr
->cond_true
);
1079 true = &expr
->cond_true
;
1082 ctype
= compatible_integer_binop(true, &expr
->cond_false
);
1085 ctype
= compatible_ptr_type(*true, expr
->cond_false
);
1088 ctype
= compatible_float_binop(true, &expr
->cond_false
);
1091 ctype
= compatible_restricted_binop('?', true, &expr
->cond_false
);
1095 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
1098 sparse_error(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
1102 expr
->ctype
= ctype
;
1106 /* FP assignments can not do modulo or bit operations */
1107 static int compatible_float_op(int op
)
1110 op
== SPECIAL_ADD_ASSIGN
||
1111 op
== SPECIAL_SUB_ASSIGN
||
1112 op
== SPECIAL_MUL_ASSIGN
||
1113 op
== SPECIAL_DIV_ASSIGN
;
1116 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1117 struct expression
**rp
, struct symbol
*source
, const char *where
, int op
)
1119 const char *typediff
;
1120 struct symbol
*t
, *s
;
1122 int tclass
= classify_type(target
, &t
);
1123 int sclass
= classify_type(source
, &s
);
1125 if (tclass
& sclass
& TYPE_NUM
) {
1126 if (tclass
& TYPE_FLOAT
&& !compatible_float_op(op
)) {
1127 sparse_error(expr
->pos
, "invalid assignment");
1130 if (tclass
& TYPE_RESTRICT
) {
1131 if (restricted_binop(op
, target
)) {
1132 sparse_error(expr
->pos
, "bad restricted assignment");
1135 if (!restricted_value(*rp
, target
))
1137 } else if (!(sclass
& TYPE_RESTRICT
))
1139 } else if (tclass
& TYPE_PTR
) {
1140 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1141 evaluate_ptr_add(expr
, target
, rp
);
1145 sparse_error(expr
->pos
, "invalid pointer assignment");
1148 } else if (op
!= '=') {
1149 sparse_error(expr
->pos
, "invalid assignment");
1153 /* It's ok if the target is more volatile or const than the source */
1154 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
1158 /* Pointer destination? */
1159 if (tclass
& TYPE_PTR
) {
1160 struct expression
*right
= *rp
;
1163 // NULL pointer is always ok
1164 if (is_null_ptr(right
))
1167 /* "void *" matches anything as long as the address space is ok */
1168 target_as
= t
->ctype
.as
| target
->ctype
.as
;
1169 source_as
= s
->ctype
.as
| source
->ctype
.as
;
1170 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
1171 s
= get_base_type(s
);
1172 t
= get_base_type(t
);
1173 if (s
== &void_ctype
|| t
== &void_ctype
)
1178 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1179 info(expr
->pos
, " expected %s", show_typename(target
));
1180 info(expr
->pos
, " got %s", show_typename(source
));
1181 *rp
= cast_to(*rp
, target
);
1184 *rp
= cast_to(*rp
, target
);
1188 static void mark_assigned(struct expression
*expr
)
1194 switch (expr
->type
) {
1199 if (sym
->type
!= SYM_NODE
)
1201 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1205 mark_assigned(expr
->left
);
1206 mark_assigned(expr
->right
);
1209 mark_assigned(expr
->cast_expression
);
1212 mark_assigned(expr
->base
);
1220 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1222 if (type
->ctype
.modifiers
& MOD_CONST
)
1223 sparse_error(left
->pos
, "assignment to const expression");
1225 /* We know left is an lvalue, so it's a "preop-*" */
1226 mark_assigned(left
->unop
);
1229 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1231 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1232 struct expression
*where
= expr
;
1233 struct symbol
*ltype
, *rtype
;
1235 if (!lvalue_expression(left
)) {
1236 sparse_error(expr
->pos
, "not an lvalue");
1240 ltype
= left
->ctype
;
1242 rtype
= degenerate(right
);
1244 if (!compatible_assignment_types(where
, ltype
, &where
->right
, rtype
, "assignment", expr
->op
))
1247 evaluate_assign_to(left
, ltype
);
1249 expr
->ctype
= ltype
;
1253 static void examine_fn_arguments(struct symbol
*fn
)
1257 FOR_EACH_PTR(fn
->arguments
, s
) {
1258 struct symbol
*arg
= evaluate_symbol(s
);
1259 /* Array/function arguments silently degenerate into pointers */
1265 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1266 if (arg
->type
== SYM_ARRAY
)
1267 ptr
->ctype
= arg
->ctype
;
1269 ptr
->ctype
.base_type
= arg
;
1270 ptr
->ctype
.as
|= s
->ctype
.as
;
1271 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1273 s
->ctype
.base_type
= ptr
;
1275 s
->ctype
.modifiers
= 0;
1278 examine_symbol_type(s
);
1285 } END_FOR_EACH_PTR(s
);
1288 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1290 /* Take the modifiers of the pointer, and apply them to the member */
1291 mod
|= sym
->ctype
.modifiers
;
1292 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1293 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1295 newsym
->ctype
.as
= as
;
1296 newsym
->ctype
.modifiers
= mod
;
1302 #define MOD_PTRINHERIT (MOD_VOLATILE | MOD_CONST | MOD_NODEREF | MOD_STORAGE)
1304 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1306 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1307 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1309 node
->ctype
.base_type
= ptr
;
1310 ptr
->bit_size
= bits_in_pointer
;
1311 ptr
->ctype
.alignment
= pointer_alignment
;
1313 node
->bit_size
= bits_in_pointer
;
1314 node
->ctype
.alignment
= pointer_alignment
;
1317 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1318 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1319 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1321 if (sym
->type
== SYM_NODE
) {
1322 ptr
->ctype
.as
|= sym
->ctype
.as
;
1323 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1324 sym
= sym
->ctype
.base_type
;
1326 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1327 ptr
->ctype
.as
|= sym
->ctype
.as
;
1328 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1329 sym
= sym
->ctype
.base_type
;
1331 ptr
->ctype
.base_type
= sym
;
1336 /* Arrays degenerate into pointers on pointer arithmetic */
1337 static struct symbol
*degenerate(struct expression
*expr
)
1339 struct symbol
*ctype
, *base
;
1343 ctype
= expr
->ctype
;
1346 base
= examine_symbol_type(ctype
);
1347 if (ctype
->type
== SYM_NODE
)
1348 base
= ctype
->ctype
.base_type
;
1350 * Arrays degenerate into pointers to the entries, while
1351 * functions degenerate into pointers to themselves.
1352 * If array was part of non-lvalue compound, we create a copy
1353 * of that compound first and then act as if we were dealing with
1354 * the corresponding field in there.
1356 switch (base
->type
) {
1358 if (expr
->type
== EXPR_SLICE
) {
1359 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1360 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1362 a
->ctype
.base_type
= expr
->base
->ctype
;
1363 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1364 a
->array_size
= expr
->base
->ctype
->array_size
;
1366 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1368 e0
->ctype
= &lazy_ptr_ctype
;
1370 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1373 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1375 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1377 e2
->right
= expr
->base
;
1379 e2
->ctype
= expr
->base
->ctype
;
1381 if (expr
->r_bitpos
) {
1382 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1385 e3
->right
= alloc_const_expression(expr
->pos
,
1386 expr
->r_bitpos
>> 3);
1387 e3
->ctype
= &lazy_ptr_ctype
;
1392 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1395 e4
->ctype
= &lazy_ptr_ctype
;
1398 expr
->type
= EXPR_PREOP
;
1402 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1403 sparse_error(expr
->pos
, "strange non-value function or array");
1406 *expr
= *expr
->unop
;
1407 ctype
= create_pointer(expr
, ctype
, 1);
1408 expr
->ctype
= ctype
;
1415 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1417 struct expression
*op
= expr
->unop
;
1418 struct symbol
*ctype
;
1420 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1421 sparse_error(expr
->pos
, "not addressable");
1427 if (expr
->type
== EXPR_SYMBOL
) {
1428 struct symbol
*sym
= expr
->symbol
;
1429 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1433 * symbol expression evaluation is lazy about the type
1434 * of the sub-expression, so we may have to generate
1435 * the type here if so..
1437 if (expr
->ctype
== &lazy_ptr_ctype
) {
1438 ctype
= create_pointer(expr
, ctype
, 0);
1439 expr
->ctype
= ctype
;
1445 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1447 struct expression
*op
= expr
->unop
;
1448 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1450 /* Simplify: *&(expr) => (expr) */
1451 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1456 /* Dereferencing a node drops all the node information. */
1457 if (ctype
->type
== SYM_NODE
)
1458 ctype
= ctype
->ctype
.base_type
;
1460 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1461 target
= ctype
->ctype
.base_type
;
1463 switch (ctype
->type
) {
1465 sparse_error(expr
->pos
, "cannot derefence this type");
1468 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1469 merge_type(node
, ctype
);
1473 if (!lvalue_expression(op
)) {
1474 sparse_error(op
->pos
, "non-lvalue array??");
1478 /* Do the implied "addressof" on the array */
1482 * When an array is dereferenced, we need to pick
1483 * up the attributes of the original node too..
1485 merge_type(node
, op
->ctype
);
1486 merge_type(node
, ctype
);
1490 node
->bit_size
= target
->bit_size
;
1491 node
->array_size
= target
->array_size
;
1498 * Unary post-ops: x++ and x--
1500 static struct symbol
*evaluate_postop(struct expression
*expr
)
1502 struct expression
*op
= expr
->unop
;
1503 struct symbol
*ctype
= op
->ctype
;
1505 if (!lvalue_expression(expr
->unop
)) {
1506 sparse_error(expr
->pos
, "need lvalue expression for ++/--");
1509 if (is_restricted_type(ctype
) && restricted_unop(expr
->op
, ctype
)) {
1510 sparse_error(expr
->pos
, "bad operation on restricted");
1514 evaluate_assign_to(op
, ctype
);
1516 expr
->ctype
= ctype
;
1518 if (is_ptr_type(ctype
))
1519 expr
->op_value
= ptr_object_size(ctype
) >> 3;
1524 static struct symbol
*evaluate_sign(struct expression
*expr
)
1526 struct symbol
*ctype
= expr
->unop
->ctype
;
1527 if (is_int_type(ctype
)) {
1528 struct symbol
*rtype
= rtype
= integer_promotion(ctype
);
1529 expr
->unop
= cast_to(expr
->unop
, rtype
);
1531 } else if (is_float_type(ctype
) && expr
->op
!= '~') {
1532 /* no conversions needed */
1533 } else if (is_restricted_type(ctype
) && !restricted_unop(expr
->op
, ctype
)) {
1534 /* no conversions needed */
1536 return bad_expr_type(expr
);
1538 if (expr
->op
== '+')
1539 *expr
= *expr
->unop
;
1540 expr
->ctype
= ctype
;
1544 static struct symbol
*evaluate_preop(struct expression
*expr
)
1546 struct symbol
*ctype
= expr
->unop
->ctype
;
1550 *expr
= *expr
->unop
;
1556 return evaluate_sign(expr
);
1559 return evaluate_dereference(expr
);
1562 return evaluate_addressof(expr
);
1564 case SPECIAL_INCREMENT
:
1565 case SPECIAL_DECREMENT
:
1567 * From a type evaluation standpoint the pre-ops are
1568 * the same as the postops
1570 return evaluate_postop(expr
);
1573 if (is_safe_type(ctype
))
1574 warning(expr
->pos
, "testing a 'safe expression'");
1575 if (is_float_type(ctype
)) {
1576 struct expression
*arg
= expr
->unop
;
1577 expr
->type
= EXPR_BINOP
;
1578 expr
->op
= SPECIAL_EQUAL
;
1580 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1581 expr
->right
->ctype
= ctype
;
1582 expr
->right
->fvalue
= 0;
1584 ctype
= &bool_ctype
;
1590 expr
->ctype
= ctype
;
1594 static struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1596 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1597 struct ptr_list
*list
= head
;
1603 for (i
= 0; i
< list
->nr
; i
++) {
1604 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1606 if (sym
->ident
!= ident
)
1608 *offset
= sym
->offset
;
1611 struct symbol
*ctype
= sym
->ctype
.base_type
;
1615 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1617 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1620 *offset
+= sym
->offset
;
1624 } while ((list
= list
->next
) != head
);
1628 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1630 struct expression
*add
;
1633 * Create a new add-expression
1635 * NOTE! Even if we just add zero, we need a new node
1636 * for the member pointer, since it has a different
1637 * type than the original pointer. We could make that
1638 * be just a cast, but the fact is, a node is a node,
1639 * so we might as well just do the "add zero" here.
1641 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1644 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1645 add
->right
->ctype
= &int_ctype
;
1646 add
->right
->value
= offset
;
1649 * The ctype of the pointer will be lazily evaluated if
1650 * we ever take the address of this member dereference..
1652 add
->ctype
= &lazy_ptr_ctype
;
1656 /* structure/union dereference */
1657 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1660 struct symbol
*ctype
, *member
;
1661 struct expression
*deref
= expr
->deref
, *add
;
1662 struct ident
*ident
= expr
->member
;
1666 if (!evaluate_expression(deref
))
1669 sparse_error(expr
->pos
, "bad member name");
1673 ctype
= deref
->ctype
;
1674 address_space
= ctype
->ctype
.as
;
1675 mod
= ctype
->ctype
.modifiers
;
1676 if (ctype
->type
== SYM_NODE
) {
1677 ctype
= ctype
->ctype
.base_type
;
1678 address_space
|= ctype
->ctype
.as
;
1679 mod
|= ctype
->ctype
.modifiers
;
1681 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1682 sparse_error(expr
->pos
, "expected structure or union");
1685 examine_symbol_type(ctype
);
1687 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1689 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1690 const char *name
= "<unnamed>";
1693 name
= ctype
->ident
->name
;
1694 namelen
= ctype
->ident
->len
;
1696 sparse_error(expr
->pos
, "no member '%s' in %s %.*s",
1697 show_ident(ident
), type
, namelen
, name
);
1702 * The member needs to take on the address space and modifiers of
1703 * the "parent" type.
1705 member
= convert_to_as_mod(member
, address_space
, mod
);
1706 ctype
= get_base_type(member
);
1708 if (!lvalue_expression(deref
)) {
1709 if (deref
->type
!= EXPR_SLICE
) {
1713 expr
->base
= deref
->base
;
1714 expr
->r_bitpos
= deref
->r_bitpos
;
1716 expr
->r_bitpos
+= offset
<< 3;
1717 expr
->type
= EXPR_SLICE
;
1718 expr
->r_nrbits
= member
->bit_size
;
1719 expr
->r_bitpos
+= member
->bit_offset
;
1720 expr
->ctype
= member
;
1724 deref
= deref
->unop
;
1725 expr
->deref
= deref
;
1727 add
= evaluate_offset(deref
, offset
);
1728 expr
->type
= EXPR_PREOP
;
1732 expr
->ctype
= member
;
1736 static int is_promoted(struct expression
*expr
)
1739 switch (expr
->type
) {
1742 case EXPR_CONDITIONAL
:
1766 static struct symbol
*evaluate_cast(struct expression
*);
1768 static struct symbol
*evaluate_type_information(struct expression
*expr
)
1770 struct symbol
*sym
= expr
->cast_type
;
1772 sym
= evaluate_expression(expr
->cast_expression
);
1776 * Expressions of restricted types will possibly get
1777 * promoted - check that here
1779 if (is_restricted_type(sym
)) {
1780 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
1784 examine_symbol_type(sym
);
1785 if (is_bitfield_type(sym
)) {
1786 sparse_error(expr
->pos
, "trying to examine bitfield type");
1792 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1794 struct symbol
*type
;
1797 type
= evaluate_type_information(expr
);
1801 size
= type
->bit_size
;
1802 if ((size
< 0) || (size
& 7))
1803 sparse_error(expr
->pos
, "cannot size expression");
1804 expr
->type
= EXPR_VALUE
;
1805 expr
->value
= size
>> 3;
1806 expr
->ctype
= size_t_ctype
;
1807 return size_t_ctype
;
1810 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
1812 struct symbol
*type
;
1815 type
= evaluate_type_information(expr
);
1819 if (type
->type
== SYM_NODE
)
1820 type
= type
->ctype
.base_type
;
1823 switch (type
->type
) {
1827 type
= get_base_type(type
);
1831 sparse_error(expr
->pos
, "expected pointer expression");
1834 size
= type
->bit_size
;
1837 expr
->type
= EXPR_VALUE
;
1838 expr
->value
= size
>> 3;
1839 expr
->ctype
= size_t_ctype
;
1840 return size_t_ctype
;
1843 static struct symbol
*evaluate_alignof(struct expression
*expr
)
1845 struct symbol
*type
;
1847 type
= evaluate_type_information(expr
);
1851 expr
->type
= EXPR_VALUE
;
1852 expr
->value
= type
->ctype
.alignment
;
1853 expr
->ctype
= size_t_ctype
;
1854 return size_t_ctype
;
1857 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1859 struct expression
*expr
;
1860 struct symbol_list
*argument_types
= fn
->arguments
;
1861 struct symbol
*argtype
;
1864 PREPARE_PTR_LIST(argument_types
, argtype
);
1865 FOR_EACH_PTR (head
, expr
) {
1866 struct expression
**p
= THIS_ADDRESS(expr
);
1867 struct symbol
*ctype
, *target
;
1868 ctype
= evaluate_expression(expr
);
1873 ctype
= degenerate(expr
);
1876 if (!target
&& ctype
->bit_size
< bits_in_int
)
1877 target
= &int_ctype
;
1879 static char where
[30];
1880 examine_symbol_type(target
);
1881 sprintf(where
, "argument %d", i
);
1882 compatible_assignment_types(expr
, target
, p
, ctype
, where
, '=');
1886 NEXT_PTR_LIST(argtype
);
1887 } END_FOR_EACH_PTR(expr
);
1888 FINISH_PTR_LIST(argtype
);
1892 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
);
1894 static int evaluate_one_array_initializer(struct symbol
*ctype
, struct expression
**ep
, int current
)
1896 struct expression
*entry
= *ep
;
1897 struct expression
**parent
, *reuse
= NULL
;
1898 unsigned long offset
;
1900 unsigned long from
, to
;
1901 int accept_string
= is_byte_type(ctype
);
1906 if (entry
->type
== EXPR_INDEX
) {
1907 from
= entry
->idx_from
;
1908 to
= entry
->idx_to
+1;
1909 parent
= &entry
->idx_expression
;
1911 entry
= entry
->idx_expression
;
1914 offset
= from
* (ctype
->bit_size
>>3);
1916 if (!reuse
) reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1917 reuse
->type
= EXPR_POS
;
1918 reuse
->ctype
= ctype
;
1919 reuse
->init_offset
= offset
;
1920 reuse
->init_nr
= to
- from
;
1921 reuse
->init_expr
= entry
;
1922 parent
= &reuse
->init_expr
;
1927 if (accept_string
&& entry
->type
== EXPR_STRING
) {
1928 sym
= evaluate_expression(entry
);
1929 to
= from
+ get_expression_value(sym
->array_size
);
1931 evaluate_initializer(ctype
, parent
);
1936 static void evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
)
1938 struct expression
*entry
;
1941 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1942 current
= evaluate_one_array_initializer(ctype
, THIS_ADDRESS(entry
), current
);
1943 } END_FOR_EACH_PTR(entry
);
1946 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1947 static void evaluate_scalar_initializer(struct symbol
*ctype
, struct expression
*expr
)
1949 if (expression_list_size(expr
->expr_list
) != 1) {
1950 sparse_error(expr
->pos
, "unexpected compound initializer");
1953 evaluate_array_initializer(ctype
, expr
);
1957 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
1961 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
1962 if (sym
->ident
== ident
)
1964 } END_FOR_EACH_PTR(sym
);
1968 static int evaluate_one_struct_initializer(struct symbol
*ctype
, struct expression
**ep
, struct symbol
*sym
)
1970 struct expression
*entry
= *ep
;
1971 struct expression
**parent
;
1972 struct expression
*reuse
= NULL
;
1973 unsigned long offset
;
1976 sparse_error(entry
->pos
, "unknown named initializer");
1980 if (entry
->type
== EXPR_IDENTIFIER
) {
1982 entry
= entry
->ident_expression
;
1986 offset
= sym
->offset
;
1989 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1990 reuse
->type
= EXPR_POS
;
1992 reuse
->init_offset
= offset
;
1994 reuse
->init_expr
= entry
;
1995 parent
= &reuse
->init_expr
;
1999 evaluate_initializer(sym
, parent
);
2003 static void evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
)
2005 struct expression
*entry
;
2008 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
2009 FOR_EACH_PTR(expr
->expr_list
, entry
) {
2010 if (entry
->type
== EXPR_IDENTIFIER
) {
2011 struct ident
*ident
= entry
->expr_ident
;
2012 /* We special-case the "already right place" case */
2013 if (!sym
|| sym
->ident
!= ident
) {
2014 RESET_PTR_LIST(sym
);
2018 if (sym
->ident
== ident
)
2024 if (evaluate_one_struct_initializer(ctype
, THIS_ADDRESS(entry
), sym
))
2027 } END_FOR_EACH_PTR(entry
);
2028 FINISH_PTR_LIST(sym
);
2032 * Initializers are kind of like assignments. Except
2033 * they can be a hell of a lot more complex.
2035 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2037 struct expression
*expr
= *ep
;
2040 * Simple non-structure/array initializers are the simple
2041 * case, and look (and parse) largely like assignments.
2043 switch (expr
->type
) {
2045 int is_string
= expr
->type
== EXPR_STRING
;
2046 struct symbol
*rtype
= evaluate_expression(expr
);
2050 * char array[] = "string"
2051 * should _not_ degenerate.
2053 if (!is_string
|| !is_string_type(ctype
))
2054 rtype
= degenerate(expr
);
2055 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer", '=');
2060 case EXPR_INITIALIZER
:
2061 expr
->ctype
= ctype
;
2062 if (ctype
->type
== SYM_NODE
)
2063 ctype
= ctype
->ctype
.base_type
;
2065 switch (ctype
->type
) {
2068 evaluate_array_initializer(get_base_type(ctype
), expr
);
2071 evaluate_struct_or_union_initializer(ctype
, expr
, 0);
2074 evaluate_struct_or_union_initializer(ctype
, expr
, 1);
2077 evaluate_scalar_initializer(ctype
, expr
);
2081 case EXPR_IDENTIFIER
:
2082 if (ctype
->type
== SYM_NODE
)
2083 ctype
= ctype
->ctype
.base_type
;
2084 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2085 sparse_error(expr
->pos
, "expected structure or union for '%s' dereference", show_ident(expr
->expr_ident
));
2089 evaluate_one_struct_initializer(ctype
, ep
,
2090 find_struct_ident(ctype
, expr
->expr_ident
));
2094 if (ctype
->type
== SYM_NODE
)
2095 ctype
= ctype
->ctype
.base_type
;
2096 if (ctype
->type
!= SYM_ARRAY
) {
2097 sparse_error(expr
->pos
, "expected array");
2100 evaluate_one_array_initializer(ctype
->ctype
.base_type
, ep
, 0);
2105 * An EXPR_POS expression has already been evaluated, and we don't
2106 * need to do anything more
2112 static int get_as(struct symbol
*sym
)
2120 mod
= sym
->ctype
.modifiers
;
2121 if (sym
->type
== SYM_NODE
) {
2122 sym
= sym
->ctype
.base_type
;
2123 as
|= sym
->ctype
.as
;
2124 mod
|= sym
->ctype
.modifiers
;
2128 * At least for now, allow casting to a "unsigned long".
2129 * That's how we do things like pointer arithmetic and
2130 * store pointers to registers.
2132 if (sym
== &ulong_ctype
)
2135 if (sym
&& sym
->type
== SYM_PTR
) {
2136 sym
= get_base_type(sym
);
2137 as
|= sym
->ctype
.as
;
2138 mod
|= sym
->ctype
.modifiers
;
2140 if (mod
& MOD_FORCE
)
2145 static void cast_to_as(struct expression
*e
, int as
)
2147 struct expression
*v
= e
->cast_expression
;
2148 struct symbol
*type
= v
->ctype
;
2150 if (!Wcast_to_address_space
)
2153 if (v
->type
!= EXPR_VALUE
|| v
->value
)
2156 /* cast from constant 0 to pointer is OK */
2157 if (is_int_type(type
))
2160 if (type
->type
== SYM_NODE
)
2161 type
= type
->ctype
.base_type
;
2163 if (type
->type
== SYM_PTR
&& type
->ctype
.base_type
== &void_ctype
)
2167 warning(e
->pos
, "cast adds address space to expression (<asn:%d>)", as
);
2170 static struct symbol
*evaluate_cast(struct expression
*expr
)
2172 struct expression
*target
= expr
->cast_expression
;
2173 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
2174 struct symbol
*t1
, *t2
;
2181 expr
->ctype
= ctype
;
2182 expr
->cast_type
= ctype
;
2185 * Special case: a cast can be followed by an
2186 * initializer, in which case we need to pass
2187 * the type value down to that initializer rather
2188 * than trying to evaluate it as an expression
2190 * A more complex case is when the initializer is
2191 * dereferenced as part of a post-fix expression.
2192 * We need to produce an expression that can be dereferenced.
2194 if (target
->type
== EXPR_INITIALIZER
) {
2195 struct symbol
*sym
= expr
->cast_type
;
2196 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2198 sym
->initializer
= expr
->cast_expression
;
2199 evaluate_symbol(sym
);
2201 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2204 expr
->type
= EXPR_PREOP
;
2212 evaluate_expression(target
);
2215 class1
= classify_type(ctype
, &t1
);
2217 * You can always throw a value away by casting to
2218 * "void" - that's an implicit "force". Note that
2219 * the same is _not_ true of "void *".
2221 if (t1
== &void_ctype
)
2224 if (class1
& TYPE_COMPOUND
)
2225 warning(expr
->pos
, "cast to non-scalar");
2229 sparse_error(expr
->pos
, "cast from unknown type");
2232 class2
= classify_type(t2
, &t2
);
2234 if (class2
& TYPE_COMPOUND
)
2235 warning(expr
->pos
, "cast from non-scalar");
2237 if (!(ctype
->ctype
.modifiers
& MOD_FORCE
) && t1
!= t2
) {
2238 if (class1
& TYPE_RESTRICT
)
2239 warning(expr
->pos
, "cast to restricted type");
2240 if (class2
& TYPE_RESTRICT
)
2241 warning(expr
->pos
, "cast from restricted type");
2244 as1
= get_as(ctype
);
2245 as2
= get_as(target
->ctype
);
2246 if (!as1
&& as2
> 0)
2247 warning(expr
->pos
, "cast removes address space of expression");
2248 if (as1
> 0 && as2
> 0 && as1
!= as2
)
2249 warning(expr
->pos
, "cast between address spaces (<asn:%d>-><asn:%d>)", as2
, as1
);
2250 if (as1
> 0 && !as2
)
2251 cast_to_as(expr
, as1
);
2254 * Casts of constant values are special: they
2255 * can be NULL, and thus need to be simplified
2258 if (target
->type
== EXPR_VALUE
)
2259 cast_value(expr
, ctype
, target
, target
->ctype
);
2266 * Evaluate a call expression with a symbol. This
2267 * should expand inline functions, and evaluate
2270 static int evaluate_symbol_call(struct expression
*expr
)
2272 struct expression
*fn
= expr
->fn
;
2273 struct symbol
*ctype
= fn
->ctype
;
2275 if (fn
->type
!= EXPR_PREOP
)
2278 if (ctype
->op
&& ctype
->op
->evaluate
)
2279 return ctype
->op
->evaluate(expr
);
2281 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2283 struct symbol
*curr
= current_fn
;
2284 current_fn
= ctype
->ctype
.base_type
;
2285 examine_fn_arguments(current_fn
);
2287 ret
= inline_function(expr
, ctype
);
2289 /* restore the old function */
2297 static struct symbol
*evaluate_call(struct expression
*expr
)
2300 struct symbol
*ctype
, *sym
;
2301 struct expression
*fn
= expr
->fn
;
2302 struct expression_list
*arglist
= expr
->args
;
2304 if (!evaluate_expression(fn
))
2306 sym
= ctype
= fn
->ctype
;
2307 if (ctype
->type
== SYM_NODE
)
2308 ctype
= ctype
->ctype
.base_type
;
2309 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
2310 ctype
= get_base_type(ctype
);
2312 if (sym
->type
== SYM_NODE
&& fn
->type
== EXPR_PREOP
&&
2313 sym
->op
&& sym
->op
->args
) {
2314 if (!sym
->op
->args(expr
))
2317 if (!evaluate_arguments(sym
, ctype
, arglist
))
2319 if (ctype
->type
!= SYM_FN
) {
2320 sparse_error(expr
->pos
, "not a function %s",
2321 show_ident(sym
->ident
));
2324 args
= expression_list_size(expr
->args
);
2325 fnargs
= symbol_list_size(ctype
->arguments
);
2327 sparse_error(expr
->pos
,
2328 "not enough arguments for function %s",
2329 show_ident(sym
->ident
));
2330 if (args
> fnargs
&& !ctype
->variadic
)
2331 sparse_error(expr
->pos
,
2332 "too many arguments for function %s",
2333 show_ident(sym
->ident
));
2335 if (sym
->type
== SYM_NODE
) {
2336 if (evaluate_symbol_call(expr
))
2339 expr
->ctype
= ctype
->ctype
.base_type
;
2343 struct symbol
*evaluate_expression(struct expression
*expr
)
2350 switch (expr
->type
) {
2353 sparse_error(expr
->pos
, "value expression without a type");
2356 return evaluate_string(expr
);
2358 return evaluate_symbol_expression(expr
);
2360 if (!evaluate_expression(expr
->left
))
2362 if (!evaluate_expression(expr
->right
))
2364 return evaluate_binop(expr
);
2366 return evaluate_logical(expr
);
2368 evaluate_expression(expr
->left
);
2369 if (!evaluate_expression(expr
->right
))
2371 return evaluate_comma(expr
);
2373 if (!evaluate_expression(expr
->left
))
2375 if (!evaluate_expression(expr
->right
))
2377 return evaluate_compare(expr
);
2378 case EXPR_ASSIGNMENT
:
2379 if (!evaluate_expression(expr
->left
))
2381 if (!evaluate_expression(expr
->right
))
2383 return evaluate_assignment(expr
);
2385 if (!evaluate_expression(expr
->unop
))
2387 return evaluate_preop(expr
);
2389 if (!evaluate_expression(expr
->unop
))
2391 return evaluate_postop(expr
);
2393 case EXPR_IMPLIED_CAST
:
2394 return evaluate_cast(expr
);
2396 return evaluate_sizeof(expr
);
2397 case EXPR_PTRSIZEOF
:
2398 return evaluate_ptrsizeof(expr
);
2400 return evaluate_alignof(expr
);
2402 return evaluate_member_dereference(expr
);
2404 return evaluate_call(expr
);
2406 case EXPR_CONDITIONAL
:
2407 return evaluate_conditional_expression(expr
);
2408 case EXPR_STATEMENT
:
2409 expr
->ctype
= evaluate_statement(expr
->statement
);
2413 expr
->ctype
= &ptr_ctype
;
2417 /* Evaluate the type of the symbol .. */
2418 evaluate_symbol(expr
->symbol
);
2419 /* .. but the type of the _expression_ is a "type" */
2420 expr
->ctype
= &type_ctype
;
2423 /* These can not exist as stand-alone expressions */
2424 case EXPR_INITIALIZER
:
2425 case EXPR_IDENTIFIER
:
2428 sparse_error(expr
->pos
, "internal front-end error: initializer in expression");
2431 sparse_error(expr
->pos
, "internal front-end error: SLICE re-evaluated");
2437 static void check_duplicates(struct symbol
*sym
)
2440 struct symbol
*next
= sym
;
2442 while ((next
= next
->same_symbol
) != NULL
) {
2443 const char *typediff
;
2444 evaluate_symbol(next
);
2446 typediff
= type_difference(sym
, next
, 0, 0);
2448 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2449 show_ident(sym
->ident
),
2450 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
2455 unsigned long mod
= sym
->ctype
.modifiers
;
2456 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
2458 if (!(mod
& MOD_TOPLEVEL
))
2462 if (sym
->ident
== &main_ident
)
2464 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
2468 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
2470 struct symbol
*base_type
;
2478 sym
= examine_symbol_type(sym
);
2479 base_type
= get_base_type(sym
);
2483 /* Evaluate the initializers */
2484 if (sym
->initializer
)
2485 evaluate_initializer(sym
, &sym
->initializer
);
2487 /* And finally, evaluate the body of the symbol too */
2488 if (base_type
->type
== SYM_FN
) {
2489 struct symbol
*curr
= current_fn
;
2491 current_fn
= base_type
;
2493 examine_fn_arguments(base_type
);
2494 if (!base_type
->stmt
&& base_type
->inline_stmt
)
2496 if (base_type
->stmt
)
2497 evaluate_statement(base_type
->stmt
);
2505 void evaluate_symbol_list(struct symbol_list
*list
)
2509 FOR_EACH_PTR(list
, sym
) {
2510 evaluate_symbol(sym
);
2511 check_duplicates(sym
);
2512 } END_FOR_EACH_PTR(sym
);
2515 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
2517 struct expression
*expr
= stmt
->expression
;
2518 struct symbol
*ctype
, *fntype
;
2520 evaluate_expression(expr
);
2521 ctype
= degenerate(expr
);
2522 fntype
= current_fn
->ctype
.base_type
;
2523 if (!fntype
|| fntype
== &void_ctype
) {
2524 if (expr
&& ctype
!= &void_ctype
)
2525 sparse_error(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
2530 sparse_error(stmt
->pos
, "return with no return value");
2535 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, ctype
, "return expression", '=');
2539 static void evaluate_if_statement(struct statement
*stmt
)
2541 if (!stmt
->if_conditional
)
2544 evaluate_conditional(stmt
->if_conditional
, 0);
2545 evaluate_statement(stmt
->if_true
);
2546 evaluate_statement(stmt
->if_false
);
2549 static void evaluate_iterator(struct statement
*stmt
)
2551 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
2552 evaluate_conditional(stmt
->iterator_post_condition
,1);
2553 evaluate_statement(stmt
->iterator_pre_statement
);
2554 evaluate_statement(stmt
->iterator_statement
);
2555 evaluate_statement(stmt
->iterator_post_statement
);
2558 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
2560 switch (*constraint
) {
2561 case '=': /* Assignment */
2562 case '+': /* Update */
2565 sparse_error(expr
->pos
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
2569 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
2571 switch (*constraint
) {
2572 case '=': /* Assignment */
2573 case '+': /* Update */
2574 sparse_error(expr
->pos
, "input constraint with assignment (\"%s\")", constraint
);
2578 static void evaluate_asm_statement(struct statement
*stmt
)
2580 struct expression
*expr
;
2583 expr
= stmt
->asm_string
;
2584 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2585 sparse_error(stmt
->pos
, "need constant string for inline asm");
2590 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
2591 struct ident
*ident
;
2594 case 0: /* Identifier */
2596 ident
= (struct ident
*)expr
;
2599 case 1: /* Constraint */
2601 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2602 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
2603 *THIS_ADDRESS(expr
) = NULL
;
2606 verify_output_constraint(expr
, expr
->string
->data
);
2609 case 2: /* Expression */
2611 if (!evaluate_expression(expr
))
2613 if (!lvalue_expression(expr
))
2614 warning(expr
->pos
, "asm output is not an lvalue");
2615 evaluate_assign_to(expr
, expr
->ctype
);
2618 } END_FOR_EACH_PTR(expr
);
2621 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
2622 struct ident
*ident
;
2625 case 0: /* Identifier */
2627 ident
= (struct ident
*)expr
;
2630 case 1: /* Constraint */
2632 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2633 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
2634 *THIS_ADDRESS(expr
) = NULL
;
2637 verify_input_constraint(expr
, expr
->string
->data
);
2640 case 2: /* Expression */
2642 if (!evaluate_expression(expr
))
2646 } END_FOR_EACH_PTR(expr
);
2648 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
2650 sparse_error(stmt
->pos
, "bad asm output");
2653 if (expr
->type
== EXPR_STRING
)
2655 sparse_error(expr
->pos
, "asm clobber is not a string");
2656 } END_FOR_EACH_PTR(expr
);
2659 static void evaluate_case_statement(struct statement
*stmt
)
2661 evaluate_expression(stmt
->case_expression
);
2662 evaluate_expression(stmt
->case_to
);
2663 evaluate_statement(stmt
->case_statement
);
2666 static void check_case_type(struct expression
*switch_expr
,
2667 struct expression
*case_expr
,
2668 struct expression
**enumcase
)
2670 struct symbol
*switch_type
, *case_type
;
2673 switch_type
= switch_expr
->ctype
;
2674 case_type
= evaluate_expression(case_expr
);
2676 if (case_type
&& switch_type
) {
2679 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
2680 else if (is_enum_type(case_type
))
2681 *enumcase
= case_expr
;
2684 /* Both integer types? */
2685 if (compatible_restricted_binop(SPECIAL_EQUAL
, &switch_expr
, &case_expr
))
2687 if (is_int_type(switch_type
) && is_int_type(case_type
))
2691 sparse_error(case_expr
->pos
, "incompatible types for 'case' statement");
2694 static void evaluate_switch_statement(struct statement
*stmt
)
2697 struct expression
*enumcase
= NULL
;
2698 struct expression
**enumcase_holder
= &enumcase
;
2699 struct expression
*sel
= stmt
->switch_expression
;
2701 evaluate_expression(sel
);
2702 evaluate_statement(stmt
->switch_statement
);
2705 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
2706 enumcase_holder
= NULL
; /* Only check cases against switch */
2708 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
2709 struct statement
*case_stmt
= sym
->stmt
;
2710 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
2711 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
2712 } END_FOR_EACH_PTR(sym
);
2715 struct symbol
*evaluate_statement(struct statement
*stmt
)
2720 switch (stmt
->type
) {
2721 case STMT_DECLARATION
: {
2723 FOR_EACH_PTR(stmt
->declaration
, s
) {
2725 } END_FOR_EACH_PTR(s
);
2730 return evaluate_return_expression(stmt
);
2732 case STMT_EXPRESSION
:
2733 if (!evaluate_expression(stmt
->expression
))
2735 return degenerate(stmt
->expression
);
2737 case STMT_COMPOUND
: {
2738 struct statement
*s
;
2739 struct symbol
*type
= NULL
;
2741 /* Evaluate the return symbol in the compound statement */
2742 evaluate_symbol(stmt
->ret
);
2745 * Then, evaluate each statement, making the type of the
2746 * compound statement be the type of the last statement
2749 FOR_EACH_PTR(stmt
->stmts
, s
) {
2750 type
= evaluate_statement(s
);
2751 } END_FOR_EACH_PTR(s
);
2757 evaluate_if_statement(stmt
);
2760 evaluate_iterator(stmt
);
2763 evaluate_switch_statement(stmt
);
2766 evaluate_case_statement(stmt
);
2769 return evaluate_statement(stmt
->label_statement
);
2771 evaluate_expression(stmt
->goto_expression
);
2776 evaluate_asm_statement(stmt
);
2779 evaluate_expression(stmt
->expression
);
2782 evaluate_expression(stmt
->range_expression
);
2783 evaluate_expression(stmt
->range_low
);
2784 evaluate_expression(stmt
->range_high
);