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
)
246 if (typea
->type
== SYM_NODE
)
247 typea
= typea
->ctype
.base_type
;
248 if (typeb
->type
== SYM_NODE
)
249 typeb
= typeb
->ctype
.base_type
;
254 if (typea
->type
== SYM_ENUM
&& typeb
->type
== SYM_ENUM
)
255 warning(pos
, "mixing different enum types");
259 * This gets called for implicit casts in assignments and
260 * integer promotion. We often want to try to move the
261 * cast down, because the ops involved may have been
262 * implicitly cast up, and we can get rid of the casts
265 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
267 struct expression
*expr
;
269 warn_for_different_enum_types (old
->pos
, old
->ctype
, type
);
271 if (is_same_type(old
, type
))
275 * See if we can simplify the op. Move the cast down.
279 if (old
->ctype
->bit_size
< type
->bit_size
)
281 if (old
->op
== '~') {
283 old
->unop
= cast_to(old
->unop
, type
);
288 case EXPR_IMPLIED_CAST
:
289 warn_for_different_enum_types(old
->pos
, old
->ctype
, type
);
291 if (old
->ctype
->bit_size
>= type
->bit_size
) {
292 struct expression
*orig
= old
->cast_expression
;
293 if (same_cast_type(orig
->ctype
, type
))
295 if (old
->ctype
->bit_offset
== type
->bit_offset
) {
297 old
->cast_type
= type
;
307 expr
= alloc_expression(old
->pos
, EXPR_IMPLIED_CAST
);
309 expr
->cast_type
= type
;
310 expr
->cast_expression
= old
;
314 static int is_type_type(struct symbol
*type
)
316 return (type
->ctype
.modifiers
& MOD_TYPE
) != 0;
319 int is_ptr_type(struct symbol
*type
)
321 if (type
->type
== SYM_NODE
)
322 type
= type
->ctype
.base_type
;
323 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
326 static inline int is_float_type(struct symbol
*type
)
328 if (type
->type
== SYM_NODE
)
329 type
= type
->ctype
.base_type
;
330 return type
->ctype
.base_type
== &fp_type
;
333 static inline int is_byte_type(struct symbol
*type
)
335 return type
->bit_size
== bits_in_char
&& type
->type
!= SYM_BITFIELD
;
338 static inline int is_string_type(struct symbol
*type
)
340 if (type
->type
== SYM_NODE
)
341 type
= type
->ctype
.base_type
;
342 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
345 static struct symbol
*bad_expr_type(struct expression
*expr
)
347 sparse_error(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
348 switch (expr
->type
) {
351 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
352 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
356 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
362 return expr
->ctype
= &bad_ctype
;
365 static struct symbol
*compatible_float_binop(struct expression
**lp
, struct expression
**rp
)
367 struct expression
*left
= *lp
, *right
= *rp
;
368 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
370 if (ltype
->type
== SYM_NODE
)
371 ltype
= ltype
->ctype
.base_type
;
372 if (rtype
->type
== SYM_NODE
)
373 rtype
= rtype
->ctype
.base_type
;
374 if (is_float_type(ltype
)) {
375 if (is_int_type(rtype
))
377 if (is_float_type(rtype
)) {
378 unsigned long lmod
= ltype
->ctype
.modifiers
;
379 unsigned long rmod
= rtype
->ctype
.modifiers
;
380 lmod
&= MOD_LONG
| MOD_LONGLONG
;
381 rmod
&= MOD_LONG
| MOD_LONGLONG
;
391 if (!is_float_type(rtype
) || !is_int_type(ltype
))
394 *lp
= cast_to(left
, rtype
);
397 *rp
= cast_to(right
, ltype
);
401 static struct symbol
*compatible_integer_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_int_type(ltype
) && is_int_type(rtype
)) {
411 struct symbol
*ctype
= bigger_int_type(ltype
, rtype
);
413 *lp
= cast_to(left
, ctype
);
414 *rp
= cast_to(right
, ctype
);
420 static int restricted_value(struct expression
*v
, struct symbol
*type
)
422 if (v
->type
!= EXPR_VALUE
)
429 static int restricted_binop(int op
, struct symbol
*type
)
438 case SPECIAL_NOTEQUAL
:
439 case SPECIAL_AND_ASSIGN
:
440 case SPECIAL_OR_ASSIGN
:
441 case SPECIAL_XOR_ASSIGN
:
448 static int restricted_unop(int op
, struct symbol
*type
)
450 if (op
== '~' && type
->bit_size
>= bits_in_int
)
457 static struct symbol
*compatible_restricted_binop(int op
, struct expression
**lp
, struct expression
**rp
)
459 struct expression
*left
= *lp
, *right
= *rp
;
460 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
461 struct symbol
*type
= NULL
;
463 if (ltype
->type
== SYM_NODE
)
464 ltype
= ltype
->ctype
.base_type
;
465 if (rtype
->type
== SYM_NODE
)
466 rtype
= rtype
->ctype
.base_type
;
468 warn_for_different_enum_types(right
->pos
, ltype
, rtype
);
470 if (ltype
->type
== SYM_ENUM
)
471 ltype
= ltype
->ctype
.base_type
;
472 if (rtype
->type
== SYM_ENUM
)
473 rtype
= rtype
->ctype
.base_type
;
475 if (is_restricted_type(ltype
)) {
476 if (is_restricted_type(rtype
)) {
480 if (!restricted_value(right
, ltype
))
483 } else if (is_restricted_type(rtype
)) {
484 if (!restricted_value(left
, rtype
))
489 if (restricted_binop(op
, type
))
494 static struct symbol
*evaluate_arith(struct expression
*expr
, int float_ok
)
496 struct symbol
*ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
497 if (!ctype
&& float_ok
)
498 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
500 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
505 return bad_expr_type(expr
);
508 static inline int lvalue_expression(struct expression
*expr
)
510 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
513 static int ptr_object_size(struct symbol
*ptr_type
)
515 if (ptr_type
->type
== SYM_NODE
)
516 ptr_type
= ptr_type
->ctype
.base_type
;
517 if (ptr_type
->type
== SYM_PTR
)
518 ptr_type
= get_base_type(ptr_type
);
519 return ptr_type
->bit_size
;
522 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*ctype
, struct expression
**ip
)
524 struct expression
*i
= *ip
;
525 struct symbol
*ptr_type
= ctype
;
528 if (ptr_type
->type
== SYM_NODE
)
529 ptr_type
= ptr_type
->ctype
.base_type
;
531 if (!is_int_type(i
->ctype
))
532 return bad_expr_type(expr
);
534 examine_symbol_type(ctype
);
536 if (!ctype
->ctype
.base_type
) {
537 sparse_error(expr
->pos
, "missing type information");
541 /* Get the size of whatever the pointer points to */
542 bit_size
= ptr_object_size(ctype
);
544 if (bit_size
> bits_in_char
) {
545 int multiply
= bit_size
>> 3;
546 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
548 if (i
->type
== EXPR_VALUE
) {
549 val
->value
= i
->value
* multiply
;
550 val
->ctype
= size_t_ctype
;
553 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
555 val
->ctype
= size_t_ctype
;
556 val
->value
= bit_size
>> 3;
559 mul
->ctype
= size_t_ctype
;
571 static struct symbol
*evaluate_add(struct expression
*expr
)
573 struct expression
*left
= expr
->left
, *right
= expr
->right
;
574 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
576 if (is_ptr_type(ltype
))
577 return evaluate_ptr_add(expr
, degenerate(left
), &expr
->right
);
579 if (is_ptr_type(rtype
))
580 return evaluate_ptr_add(expr
, degenerate(right
), &expr
->left
);
582 return evaluate_arith(expr
, 1);
585 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
586 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
589 unsigned long mod1
, mod2
, diff
;
590 unsigned long as1
, as2
;
592 struct symbol
*base1
, *base2
;
594 if (target
== source
)
596 if (!target
|| !source
)
597 return "different types";
599 * Peel of per-node information.
600 * FIXME! Check alignment and context too here!
602 mod1
= target
->ctype
.modifiers
;
603 as1
= target
->ctype
.as
;
604 mod2
= source
->ctype
.modifiers
;
605 as2
= source
->ctype
.as
;
606 if (target
->type
== SYM_NODE
) {
607 target
= target
->ctype
.base_type
;
610 if (target
->type
== SYM_PTR
) {
614 mod1
|= target
->ctype
.modifiers
;
615 as1
|= target
->ctype
.as
;
617 if (source
->type
== SYM_NODE
) {
618 source
= source
->ctype
.base_type
;
621 if (source
->type
== SYM_PTR
) {
625 mod2
|= source
->ctype
.modifiers
;
626 as2
|= source
->ctype
.as
;
628 if (target
->type
== SYM_ENUM
) {
629 target
= target
->ctype
.base_type
;
633 if (source
->type
== SYM_ENUM
) {
634 source
= source
->ctype
.base_type
;
639 if (target
== source
)
641 if (!target
|| !source
)
642 return "different types";
644 type1
= target
->type
;
645 base1
= target
->ctype
.base_type
;
647 type2
= source
->type
;
648 base2
= source
->ctype
.base_type
;
651 * Pointers to functions compare as the function itself
653 if (type1
== SYM_PTR
&& base1
) {
654 base1
= examine_symbol_type(base1
);
655 switch (base1
->type
) {
659 base1
= base1
->ctype
.base_type
;
664 if (type2
== SYM_PTR
&& base2
) {
665 base2
= examine_symbol_type(base2
);
666 switch (base2
->type
) {
670 base2
= base2
->ctype
.base_type
;
676 /* Arrays degenerate to pointers for type comparisons */
677 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
678 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
680 if (type1
!= type2
|| type1
== SYM_RESTRICT
)
681 return "different base types";
683 /* Must be same address space to be comparable */
685 return "different address spaces";
687 /* Ignore differences in storage types or addressability */
688 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
689 diff
&= (mod1
& ~target_mod_ignore
) | (mod2
& ~source_mod_ignore
);
692 return "different type sizes";
693 if (diff
& ~MOD_SIGNEDNESS
)
694 return "different modifiers";
696 /* Differs in signedness only.. */
699 * Warn if both are explicitly signed ("unsigned" is obvously
700 * always explicit, and since we know one of them has to be
701 * unsigned, we check if the signed one was explicit).
703 if ((mod1
| mod2
) & MOD_EXPLICITLY_SIGNED
)
704 return "different explicit signedness";
707 * "char" matches both "unsigned char" and "signed char",
708 * so if the explicit test didn't trigger, then we should
709 * not warn about a char.
711 if (!(mod1
& MOD_CHAR
))
712 return "different signedness";
716 if (type1
== SYM_FN
) {
718 struct symbol
*arg1
, *arg2
;
719 if (base1
->variadic
!= base2
->variadic
)
720 return "incompatible variadic arguments";
721 PREPARE_PTR_LIST(target
->arguments
, arg1
);
722 PREPARE_PTR_LIST(source
->arguments
, arg2
);
726 diff
= type_difference(arg1
, arg2
, 0, 0);
728 static char argdiff
[80];
729 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
738 FINISH_PTR_LIST(arg2
);
739 FINISH_PTR_LIST(arg1
);
748 static int is_null_ptr(struct expression
*expr
)
750 if (expr
->type
!= EXPR_VALUE
|| expr
->value
)
752 if (!is_ptr_type(expr
->ctype
))
753 warning(expr
->pos
, "Using plain integer as NULL pointer");
757 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
759 /* NULL expression? Just return the type of the "other side" */
768 * Ignore differences in "volatile" and "const"ness when
769 * subtracting pointers
771 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
773 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
**rp
)
775 const char *typediff
;
776 struct symbol
*ctype
;
777 struct symbol
*ltype
, *rtype
;
778 struct expression
*r
= *rp
;
780 ltype
= degenerate(l
);
781 rtype
= degenerate(r
);
784 * If it is an integer subtract: the ptr add case will do the
787 if (!is_ptr_type(rtype
))
788 return evaluate_ptr_add(expr
, degenerate(l
), rp
);
791 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
793 ctype
= common_ptr_type(l
, r
);
795 sparse_error(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
799 examine_symbol_type(ctype
);
801 /* Figure out the base type we point to */
802 if (ctype
->type
== SYM_NODE
)
803 ctype
= ctype
->ctype
.base_type
;
804 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
805 sparse_error(expr
->pos
, "subtraction of functions? Share your drugs");
808 ctype
= get_base_type(ctype
);
810 expr
->ctype
= ssize_t_ctype
;
811 if (ctype
->bit_size
> bits_in_char
) {
812 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
813 struct expression
*div
= expr
;
814 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
815 unsigned long value
= ctype
->bit_size
>> 3;
817 val
->ctype
= size_t_ctype
;
820 if (value
& (value
-1)) {
821 if (Wptr_subtraction_blows
)
822 warning(expr
->pos
, "potentially expensive pointer subtraction");
826 sub
->ctype
= ssize_t_ctype
;
835 return ssize_t_ctype
;
838 static struct symbol
*evaluate_sub(struct expression
*expr
)
840 struct expression
*left
= expr
->left
;
841 struct symbol
*ltype
= left
->ctype
;
843 if (is_ptr_type(ltype
))
844 return evaluate_ptr_sub(expr
, left
, &expr
->right
);
846 return evaluate_arith(expr
, 1);
849 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
851 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
853 struct symbol
*ctype
;
858 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
859 warning(expr
->pos
, "assignment expression in conditional");
861 ctype
= evaluate_expression(expr
);
863 if (is_safe_type(ctype
))
864 warning(expr
->pos
, "testing a 'safe expression'");
870 static struct symbol
*evaluate_logical(struct expression
*expr
)
872 if (!evaluate_conditional(expr
->left
, 0))
874 if (!evaluate_conditional(expr
->right
, 0))
877 expr
->ctype
= &bool_ctype
;
881 static struct symbol
*evaluate_shift(struct expression
*expr
)
883 struct expression
*left
= expr
->left
, *right
= expr
->right
;
884 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
886 if (ltype
->type
== SYM_NODE
)
887 ltype
= ltype
->ctype
.base_type
;
888 if (rtype
->type
== SYM_NODE
)
889 rtype
= rtype
->ctype
.base_type
;
890 if (is_int_type(ltype
) && is_int_type(rtype
)) {
891 struct symbol
*ctype
= integer_promotion(ltype
);
892 expr
->left
= cast_to(expr
->left
, ctype
);
894 ctype
= integer_promotion(rtype
);
895 expr
->right
= cast_to(expr
->right
, ctype
);
898 return bad_expr_type(expr
);
901 static struct symbol
*evaluate_binop(struct expression
*expr
)
904 // addition can take ptr+int, fp and int
906 return evaluate_add(expr
);
908 // subtraction can take ptr-ptr, fp and int
910 return evaluate_sub(expr
);
912 // Arithmetic operations can take fp and int
914 return evaluate_arith(expr
, 1);
916 // shifts do integer promotions, but that's it.
917 case SPECIAL_LEFTSHIFT
: case SPECIAL_RIGHTSHIFT
:
918 return evaluate_shift(expr
);
920 // The rest are integer operations
921 // '%', '&', '^', '|'
923 return evaluate_arith(expr
, 0);
927 static struct symbol
*evaluate_comma(struct expression
*expr
)
929 expr
->ctype
= expr
->right
->ctype
;
933 static int modify_for_unsigned(int op
)
936 op
= SPECIAL_UNSIGNED_LT
;
938 op
= SPECIAL_UNSIGNED_GT
;
939 else if (op
== SPECIAL_LTE
)
940 op
= SPECIAL_UNSIGNED_LTE
;
941 else if (op
== SPECIAL_GTE
)
942 op
= SPECIAL_UNSIGNED_GTE
;
946 static struct symbol
*evaluate_compare(struct expression
*expr
)
948 struct expression
*left
= expr
->left
, *right
= expr
->right
;
949 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
950 struct symbol
*ctype
;
953 if (is_type_type(ltype
) && is_type_type(rtype
))
956 if (is_safe_type(ltype
) || is_safe_type(rtype
))
957 warning(expr
->pos
, "testing a 'safe expression'");
960 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
961 // FIXME! Check the types for compatibility
962 expr
->op
= modify_for_unsigned(expr
->op
);
966 ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
968 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
969 expr
->op
= modify_for_unsigned(expr
->op
);
973 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
977 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
979 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
980 expr
->op
= modify_for_unsigned(expr
->op
);
987 expr
->ctype
= &bool_ctype
;
992 * FIXME!! This should do casts, array degeneration etc..
994 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
996 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
998 if (ltype
->type
== SYM_NODE
)
999 ltype
= ltype
->ctype
.base_type
;
1001 if (rtype
->type
== SYM_NODE
)
1002 rtype
= rtype
->ctype
.base_type
;
1004 if (ltype
->type
== SYM_PTR
) {
1005 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
1009 if (rtype
->type
== SYM_PTR
) {
1010 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
1017 * NOTE! The degenerate case of "x ? : y", where we don't
1018 * have a true case, this will possibly promote "x" to the
1019 * same type as "y", and thus _change_ the conditional
1020 * test in the expression. But since promotion is "safe"
1021 * for testing, that's ok.
1023 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
1025 struct expression
**true;
1026 struct symbol
*ctype
, *ltype
, *rtype
;
1027 const char * typediff
;
1029 if (!evaluate_conditional(expr
->conditional
, 0))
1031 if (!evaluate_expression(expr
->cond_false
))
1034 ctype
= degenerate(expr
->conditional
);
1035 rtype
= degenerate(expr
->cond_false
);
1037 true = &expr
->conditional
;
1039 if (expr
->cond_true
) {
1040 if (!evaluate_expression(expr
->cond_true
))
1042 ltype
= degenerate(expr
->cond_true
);
1043 true = &expr
->cond_true
;
1046 ctype
= compatible_integer_binop(true, &expr
->cond_false
);
1049 ctype
= compatible_ptr_type(*true, expr
->cond_false
);
1052 ctype
= compatible_float_binop(true, &expr
->cond_false
);
1055 ctype
= compatible_restricted_binop('?', true, &expr
->cond_false
);
1059 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
1062 sparse_error(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
1066 expr
->ctype
= ctype
;
1070 /* FP assignments can not do modulo or bit operations */
1071 static int compatible_float_op(int op
)
1074 op
== SPECIAL_ADD_ASSIGN
||
1075 op
== SPECIAL_SUB_ASSIGN
||
1076 op
== SPECIAL_MUL_ASSIGN
||
1077 op
== SPECIAL_DIV_ASSIGN
;
1080 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1081 struct expression
**rp
, struct symbol
*source
, const char *where
, int op
)
1083 const char *typediff
;
1087 if (is_int_type(target
)) {
1088 if (is_int_type(source
))
1090 if (is_float_type(source
))
1092 } else if (is_float_type(target
)) {
1093 if (!compatible_float_op(op
)) {
1094 sparse_error(expr
->pos
, "invalid assignment");
1097 if (is_int_type(source
))
1099 if (is_float_type(source
))
1101 } else if (is_restricted_type(target
)) {
1102 if (restricted_binop(op
, target
)) {
1103 sparse_error(expr
->pos
, "bad restricted assignment");
1106 if (!restricted_value(*rp
, target
))
1108 } else if (is_ptr_type(target
)) {
1109 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1110 evaluate_ptr_add(expr
, target
, rp
);
1114 sparse_error(expr
->pos
, "invalid pointer assignment");
1117 } else if (op
!= '=') {
1118 sparse_error(expr
->pos
, "invalid assignment");
1122 /* It's ok if the target is more volatile or const than the source */
1123 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
1127 /* Pointer destination? */
1129 target_as
= t
->ctype
.as
;
1130 if (t
->type
== SYM_NODE
) {
1131 t
= t
->ctype
.base_type
;
1132 target_as
|= t
->ctype
.as
;
1134 if (t
->type
== SYM_PTR
|| t
->type
== SYM_FN
|| t
->type
== SYM_ARRAY
) {
1135 struct expression
*right
= *rp
;
1136 struct symbol
*s
= source
;
1139 // NULL pointer is always ok
1140 if (is_null_ptr(right
))
1143 /* "void *" matches anything as long as the address space is ok */
1144 source_as
= s
->ctype
.as
;
1145 if (s
->type
== SYM_NODE
) {
1146 s
= s
->ctype
.base_type
;
1147 source_as
|= s
->ctype
.as
;
1149 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
1150 s
= get_base_type(s
);
1151 t
= get_base_type(t
);
1152 if (s
== &void_ctype
|| t
== &void_ctype
)
1157 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1158 info(expr
->pos
, " expected %s", show_typename(target
));
1159 info(expr
->pos
, " got %s", show_typename(source
));
1160 *rp
= cast_to(*rp
, target
);
1163 *rp
= cast_to(*rp
, target
);
1167 static void mark_assigned(struct expression
*expr
)
1173 switch (expr
->type
) {
1178 if (sym
->type
!= SYM_NODE
)
1180 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1184 mark_assigned(expr
->left
);
1185 mark_assigned(expr
->right
);
1188 mark_assigned(expr
->cast_expression
);
1191 mark_assigned(expr
->base
);
1199 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1201 if (type
->ctype
.modifiers
& MOD_CONST
)
1202 sparse_error(left
->pos
, "assignment to const expression");
1204 /* We know left is an lvalue, so it's a "preop-*" */
1205 mark_assigned(left
->unop
);
1208 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1210 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1211 struct expression
*where
= expr
;
1212 struct symbol
*ltype
, *rtype
;
1214 if (!lvalue_expression(left
)) {
1215 sparse_error(expr
->pos
, "not an lvalue");
1219 ltype
= left
->ctype
;
1221 rtype
= degenerate(right
);
1223 if (!compatible_assignment_types(where
, ltype
, &where
->right
, rtype
, "assignment", expr
->op
))
1226 evaluate_assign_to(left
, ltype
);
1228 expr
->ctype
= ltype
;
1232 static void examine_fn_arguments(struct symbol
*fn
)
1236 FOR_EACH_PTR(fn
->arguments
, s
) {
1237 struct symbol
*arg
= evaluate_symbol(s
);
1238 /* Array/function arguments silently degenerate into pointers */
1244 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1245 if (arg
->type
== SYM_ARRAY
)
1246 ptr
->ctype
= arg
->ctype
;
1248 ptr
->ctype
.base_type
= arg
;
1249 ptr
->ctype
.as
|= s
->ctype
.as
;
1250 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1252 s
->ctype
.base_type
= ptr
;
1254 s
->ctype
.modifiers
= 0;
1257 examine_symbol_type(s
);
1264 } END_FOR_EACH_PTR(s
);
1267 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1269 /* Take the modifiers of the pointer, and apply them to the member */
1270 mod
|= sym
->ctype
.modifiers
;
1271 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1272 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1274 newsym
->ctype
.as
= as
;
1275 newsym
->ctype
.modifiers
= mod
;
1281 #define MOD_PTRINHERIT (MOD_VOLATILE | MOD_CONST | MOD_NODEREF | MOD_STORAGE)
1283 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1285 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1286 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1288 node
->ctype
.base_type
= ptr
;
1289 ptr
->bit_size
= bits_in_pointer
;
1290 ptr
->ctype
.alignment
= pointer_alignment
;
1292 node
->bit_size
= bits_in_pointer
;
1293 node
->ctype
.alignment
= pointer_alignment
;
1296 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1297 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1298 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1300 if (sym
->type
== SYM_NODE
) {
1301 ptr
->ctype
.as
|= sym
->ctype
.as
;
1302 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1303 sym
= sym
->ctype
.base_type
;
1305 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1306 ptr
->ctype
.as
|= sym
->ctype
.as
;
1307 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1308 sym
= sym
->ctype
.base_type
;
1310 ptr
->ctype
.base_type
= sym
;
1315 /* Arrays degenerate into pointers on pointer arithmetic */
1316 static struct symbol
*degenerate(struct expression
*expr
)
1318 struct symbol
*ctype
, *base
;
1322 ctype
= expr
->ctype
;
1325 base
= examine_symbol_type(ctype
);
1326 if (ctype
->type
== SYM_NODE
)
1327 base
= ctype
->ctype
.base_type
;
1329 * Arrays degenerate into pointers to the entries, while
1330 * functions degenerate into pointers to themselves.
1331 * If array was part of non-lvalue compound, we create a copy
1332 * of that compound first and then act as if we were dealing with
1333 * the corresponding field in there.
1335 switch (base
->type
) {
1337 if (expr
->type
== EXPR_SLICE
) {
1338 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1339 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1341 a
->ctype
.base_type
= expr
->base
->ctype
;
1342 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1343 a
->array_size
= expr
->base
->ctype
->array_size
;
1345 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1347 e0
->ctype
= &lazy_ptr_ctype
;
1349 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1352 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1354 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1356 e2
->right
= expr
->base
;
1358 e2
->ctype
= expr
->base
->ctype
;
1360 if (expr
->r_bitpos
) {
1361 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1364 e3
->right
= alloc_const_expression(expr
->pos
,
1365 expr
->r_bitpos
>> 3);
1366 e3
->ctype
= &lazy_ptr_ctype
;
1371 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1374 e4
->ctype
= &lazy_ptr_ctype
;
1377 expr
->type
= EXPR_PREOP
;
1381 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1382 sparse_error(expr
->pos
, "strange non-value function or array");
1385 *expr
= *expr
->unop
;
1386 ctype
= create_pointer(expr
, ctype
, 1);
1387 expr
->ctype
= ctype
;
1394 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1396 struct expression
*op
= expr
->unop
;
1397 struct symbol
*ctype
;
1399 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1400 sparse_error(expr
->pos
, "not addressable");
1406 if (expr
->type
== EXPR_SYMBOL
) {
1407 struct symbol
*sym
= expr
->symbol
;
1408 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1412 * symbol expression evaluation is lazy about the type
1413 * of the sub-expression, so we may have to generate
1414 * the type here if so..
1416 if (expr
->ctype
== &lazy_ptr_ctype
) {
1417 ctype
= create_pointer(expr
, ctype
, 0);
1418 expr
->ctype
= ctype
;
1424 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1426 struct expression
*op
= expr
->unop
;
1427 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1429 /* Simplify: *&(expr) => (expr) */
1430 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1435 /* Dereferencing a node drops all the node information. */
1436 if (ctype
->type
== SYM_NODE
)
1437 ctype
= ctype
->ctype
.base_type
;
1439 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1440 target
= ctype
->ctype
.base_type
;
1442 switch (ctype
->type
) {
1444 sparse_error(expr
->pos
, "cannot derefence this type");
1447 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1448 merge_type(node
, ctype
);
1452 if (!lvalue_expression(op
)) {
1453 sparse_error(op
->pos
, "non-lvalue array??");
1457 /* Do the implied "addressof" on the array */
1461 * When an array is dereferenced, we need to pick
1462 * up the attributes of the original node too..
1464 merge_type(node
, op
->ctype
);
1465 merge_type(node
, ctype
);
1469 node
->bit_size
= target
->bit_size
;
1470 node
->array_size
= target
->array_size
;
1477 * Unary post-ops: x++ and x--
1479 static struct symbol
*evaluate_postop(struct expression
*expr
)
1481 struct expression
*op
= expr
->unop
;
1482 struct symbol
*ctype
= op
->ctype
;
1484 if (!lvalue_expression(expr
->unop
)) {
1485 sparse_error(expr
->pos
, "need lvalue expression for ++/--");
1488 if (is_restricted_type(ctype
) && restricted_unop(expr
->op
, ctype
)) {
1489 sparse_error(expr
->pos
, "bad operation on restricted");
1493 evaluate_assign_to(op
, ctype
);
1495 expr
->ctype
= ctype
;
1497 if (is_ptr_type(ctype
))
1498 expr
->op_value
= ptr_object_size(ctype
) >> 3;
1503 static struct symbol
*evaluate_sign(struct expression
*expr
)
1505 struct symbol
*ctype
= expr
->unop
->ctype
;
1506 if (is_int_type(ctype
)) {
1507 struct symbol
*rtype
= rtype
= integer_promotion(ctype
);
1508 expr
->unop
= cast_to(expr
->unop
, rtype
);
1510 } else if (is_float_type(ctype
) && expr
->op
!= '~') {
1511 /* no conversions needed */
1512 } else if (is_restricted_type(ctype
) && !restricted_unop(expr
->op
, ctype
)) {
1513 /* no conversions needed */
1515 return bad_expr_type(expr
);
1517 if (expr
->op
== '+')
1518 *expr
= *expr
->unop
;
1519 expr
->ctype
= ctype
;
1523 static struct symbol
*evaluate_preop(struct expression
*expr
)
1525 struct symbol
*ctype
= expr
->unop
->ctype
;
1529 *expr
= *expr
->unop
;
1535 return evaluate_sign(expr
);
1538 return evaluate_dereference(expr
);
1541 return evaluate_addressof(expr
);
1543 case SPECIAL_INCREMENT
:
1544 case SPECIAL_DECREMENT
:
1546 * From a type evaluation standpoint the pre-ops are
1547 * the same as the postops
1549 return evaluate_postop(expr
);
1552 if (is_safe_type(ctype
))
1553 warning(expr
->pos
, "testing a 'safe expression'");
1554 if (is_float_type(ctype
)) {
1555 struct expression
*arg
= expr
->unop
;
1556 expr
->type
= EXPR_BINOP
;
1557 expr
->op
= SPECIAL_EQUAL
;
1559 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1560 expr
->right
->ctype
= ctype
;
1561 expr
->right
->fvalue
= 0;
1563 ctype
= &bool_ctype
;
1569 expr
->ctype
= ctype
;
1573 static struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1575 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1576 struct ptr_list
*list
= head
;
1582 for (i
= 0; i
< list
->nr
; i
++) {
1583 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1585 if (sym
->ident
!= ident
)
1587 *offset
= sym
->offset
;
1590 struct symbol
*ctype
= sym
->ctype
.base_type
;
1594 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1596 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1599 *offset
+= sym
->offset
;
1603 } while ((list
= list
->next
) != head
);
1607 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1609 struct expression
*add
;
1612 * Create a new add-expression
1614 * NOTE! Even if we just add zero, we need a new node
1615 * for the member pointer, since it has a different
1616 * type than the original pointer. We could make that
1617 * be just a cast, but the fact is, a node is a node,
1618 * so we might as well just do the "add zero" here.
1620 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1623 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1624 add
->right
->ctype
= &int_ctype
;
1625 add
->right
->value
= offset
;
1628 * The ctype of the pointer will be lazily evaluated if
1629 * we ever take the address of this member dereference..
1631 add
->ctype
= &lazy_ptr_ctype
;
1635 /* structure/union dereference */
1636 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1639 struct symbol
*ctype
, *member
;
1640 struct expression
*deref
= expr
->deref
, *add
;
1641 struct ident
*ident
= expr
->member
;
1645 if (!evaluate_expression(deref
))
1648 sparse_error(expr
->pos
, "bad member name");
1652 ctype
= deref
->ctype
;
1653 address_space
= ctype
->ctype
.as
;
1654 mod
= ctype
->ctype
.modifiers
;
1655 if (ctype
->type
== SYM_NODE
) {
1656 ctype
= ctype
->ctype
.base_type
;
1657 address_space
|= ctype
->ctype
.as
;
1658 mod
|= ctype
->ctype
.modifiers
;
1660 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1661 sparse_error(expr
->pos
, "expected structure or union");
1664 examine_symbol_type(ctype
);
1666 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1668 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1669 const char *name
= "<unnamed>";
1672 name
= ctype
->ident
->name
;
1673 namelen
= ctype
->ident
->len
;
1675 sparse_error(expr
->pos
, "no member '%s' in %s %.*s",
1676 show_ident(ident
), type
, namelen
, name
);
1681 * The member needs to take on the address space and modifiers of
1682 * the "parent" type.
1684 member
= convert_to_as_mod(member
, address_space
, mod
);
1685 ctype
= get_base_type(member
);
1687 if (!lvalue_expression(deref
)) {
1688 if (deref
->type
!= EXPR_SLICE
) {
1692 expr
->base
= deref
->base
;
1693 expr
->r_bitpos
= deref
->r_bitpos
;
1695 expr
->r_bitpos
+= offset
<< 3;
1696 expr
->type
= EXPR_SLICE
;
1697 expr
->r_nrbits
= member
->bit_size
;
1698 expr
->r_bitpos
+= member
->bit_offset
;
1699 expr
->ctype
= member
;
1703 deref
= deref
->unop
;
1704 expr
->deref
= deref
;
1706 add
= evaluate_offset(deref
, offset
);
1707 expr
->type
= EXPR_PREOP
;
1711 expr
->ctype
= member
;
1715 static int is_promoted(struct expression
*expr
)
1718 switch (expr
->type
) {
1721 case EXPR_CONDITIONAL
:
1745 static struct symbol
*evaluate_cast(struct expression
*);
1747 static struct symbol
*evaluate_type_information(struct expression
*expr
)
1749 struct symbol
*sym
= expr
->cast_type
;
1751 sym
= evaluate_expression(expr
->cast_expression
);
1755 * Expressions of restricted types will possibly get
1756 * promoted - check that here
1758 if (is_restricted_type(sym
)) {
1759 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
1763 examine_symbol_type(sym
);
1764 if (is_bitfield_type(sym
)) {
1765 sparse_error(expr
->pos
, "trying to examine bitfield type");
1771 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1773 struct symbol
*type
;
1776 type
= evaluate_type_information(expr
);
1780 size
= type
->bit_size
;
1781 if ((size
< 0) || (size
& 7))
1782 sparse_error(expr
->pos
, "cannot size expression");
1783 expr
->type
= EXPR_VALUE
;
1784 expr
->value
= size
>> 3;
1785 expr
->ctype
= size_t_ctype
;
1786 return size_t_ctype
;
1789 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
1791 struct symbol
*type
;
1794 type
= evaluate_type_information(expr
);
1798 if (type
->type
== SYM_NODE
)
1799 type
= type
->ctype
.base_type
;
1802 switch (type
->type
) {
1806 type
= get_base_type(type
);
1810 sparse_error(expr
->pos
, "expected pointer expression");
1813 size
= type
->bit_size
;
1816 expr
->type
= EXPR_VALUE
;
1817 expr
->value
= size
>> 3;
1818 expr
->ctype
= size_t_ctype
;
1819 return size_t_ctype
;
1822 static struct symbol
*evaluate_alignof(struct expression
*expr
)
1824 struct symbol
*type
;
1826 type
= evaluate_type_information(expr
);
1830 expr
->type
= EXPR_VALUE
;
1831 expr
->value
= type
->ctype
.alignment
;
1832 expr
->ctype
= size_t_ctype
;
1833 return size_t_ctype
;
1836 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1838 struct expression
*expr
;
1839 struct symbol_list
*argument_types
= fn
->arguments
;
1840 struct symbol
*argtype
;
1843 PREPARE_PTR_LIST(argument_types
, argtype
);
1844 FOR_EACH_PTR (head
, expr
) {
1845 struct expression
**p
= THIS_ADDRESS(expr
);
1846 struct symbol
*ctype
, *target
;
1847 ctype
= evaluate_expression(expr
);
1852 ctype
= degenerate(expr
);
1855 if (!target
&& ctype
->bit_size
< bits_in_int
)
1856 target
= &int_ctype
;
1858 static char where
[30];
1859 examine_symbol_type(target
);
1860 sprintf(where
, "argument %d", i
);
1861 compatible_assignment_types(expr
, target
, p
, ctype
, where
, '=');
1865 NEXT_PTR_LIST(argtype
);
1866 } END_FOR_EACH_PTR(expr
);
1867 FINISH_PTR_LIST(argtype
);
1871 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
);
1873 static int evaluate_one_array_initializer(struct symbol
*ctype
, struct expression
**ep
, int current
)
1875 struct expression
*entry
= *ep
;
1876 struct expression
**parent
, *reuse
= NULL
;
1877 unsigned long offset
;
1879 unsigned long from
, to
;
1880 int accept_string
= is_byte_type(ctype
);
1885 if (entry
->type
== EXPR_INDEX
) {
1886 from
= entry
->idx_from
;
1887 to
= entry
->idx_to
+1;
1888 parent
= &entry
->idx_expression
;
1890 entry
= entry
->idx_expression
;
1893 offset
= from
* (ctype
->bit_size
>>3);
1895 if (!reuse
) reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1896 reuse
->type
= EXPR_POS
;
1897 reuse
->ctype
= ctype
;
1898 reuse
->init_offset
= offset
;
1899 reuse
->init_nr
= to
- from
;
1900 reuse
->init_expr
= entry
;
1901 parent
= &reuse
->init_expr
;
1906 if (accept_string
&& entry
->type
== EXPR_STRING
) {
1907 sym
= evaluate_expression(entry
);
1908 to
= from
+ get_expression_value(sym
->array_size
);
1910 evaluate_initializer(ctype
, parent
);
1915 static void evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
)
1917 struct expression
*entry
;
1920 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1921 current
= evaluate_one_array_initializer(ctype
, THIS_ADDRESS(entry
), current
);
1922 } END_FOR_EACH_PTR(entry
);
1925 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1926 static void evaluate_scalar_initializer(struct symbol
*ctype
, struct expression
*expr
)
1928 if (expression_list_size(expr
->expr_list
) != 1) {
1929 sparse_error(expr
->pos
, "unexpected compound initializer");
1932 evaluate_array_initializer(ctype
, expr
);
1936 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
1940 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
1941 if (sym
->ident
== ident
)
1943 } END_FOR_EACH_PTR(sym
);
1947 static int evaluate_one_struct_initializer(struct symbol
*ctype
, struct expression
**ep
, struct symbol
*sym
)
1949 struct expression
*entry
= *ep
;
1950 struct expression
**parent
;
1951 struct expression
*reuse
= NULL
;
1952 unsigned long offset
;
1955 sparse_error(entry
->pos
, "unknown named initializer");
1959 if (entry
->type
== EXPR_IDENTIFIER
) {
1961 entry
= entry
->ident_expression
;
1965 offset
= sym
->offset
;
1968 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1969 reuse
->type
= EXPR_POS
;
1971 reuse
->init_offset
= offset
;
1973 reuse
->init_expr
= entry
;
1974 parent
= &reuse
->init_expr
;
1978 evaluate_initializer(sym
, parent
);
1982 static void evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
)
1984 struct expression
*entry
;
1987 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
1988 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1989 if (entry
->type
== EXPR_IDENTIFIER
) {
1990 struct ident
*ident
= entry
->expr_ident
;
1991 /* We special-case the "already right place" case */
1992 if (!sym
|| sym
->ident
!= ident
) {
1993 RESET_PTR_LIST(sym
);
1997 if (sym
->ident
== ident
)
2003 if (evaluate_one_struct_initializer(ctype
, THIS_ADDRESS(entry
), sym
))
2006 } END_FOR_EACH_PTR(entry
);
2007 FINISH_PTR_LIST(sym
);
2011 * Initializers are kind of like assignments. Except
2012 * they can be a hell of a lot more complex.
2014 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2016 struct expression
*expr
= *ep
;
2019 * Simple non-structure/array initializers are the simple
2020 * case, and look (and parse) largely like assignments.
2022 switch (expr
->type
) {
2024 int is_string
= expr
->type
== EXPR_STRING
;
2025 struct symbol
*rtype
= evaluate_expression(expr
);
2029 * char array[] = "string"
2030 * should _not_ degenerate.
2032 if (!is_string
|| !is_string_type(ctype
))
2033 rtype
= degenerate(expr
);
2034 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer", '=');
2039 case EXPR_INITIALIZER
:
2040 expr
->ctype
= ctype
;
2041 if (ctype
->type
== SYM_NODE
)
2042 ctype
= ctype
->ctype
.base_type
;
2044 switch (ctype
->type
) {
2047 evaluate_array_initializer(get_base_type(ctype
), expr
);
2050 evaluate_struct_or_union_initializer(ctype
, expr
, 0);
2053 evaluate_struct_or_union_initializer(ctype
, expr
, 1);
2056 evaluate_scalar_initializer(ctype
, expr
);
2060 case EXPR_IDENTIFIER
:
2061 if (ctype
->type
== SYM_NODE
)
2062 ctype
= ctype
->ctype
.base_type
;
2063 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2064 sparse_error(expr
->pos
, "expected structure or union for '%s' dereference", show_ident(expr
->expr_ident
));
2068 evaluate_one_struct_initializer(ctype
, ep
,
2069 find_struct_ident(ctype
, expr
->expr_ident
));
2073 if (ctype
->type
== SYM_NODE
)
2074 ctype
= ctype
->ctype
.base_type
;
2075 if (ctype
->type
!= SYM_ARRAY
) {
2076 sparse_error(expr
->pos
, "expected array");
2079 evaluate_one_array_initializer(ctype
->ctype
.base_type
, ep
, 0);
2084 * An EXPR_POS expression has already been evaluated, and we don't
2085 * need to do anything more
2091 static int get_as(struct symbol
*sym
)
2099 mod
= sym
->ctype
.modifiers
;
2100 if (sym
->type
== SYM_NODE
) {
2101 sym
= sym
->ctype
.base_type
;
2102 as
|= sym
->ctype
.as
;
2103 mod
|= sym
->ctype
.modifiers
;
2107 * At least for now, allow casting to a "unsigned long".
2108 * That's how we do things like pointer arithmetic and
2109 * store pointers to registers.
2111 if (sym
== &ulong_ctype
)
2114 if (sym
&& sym
->type
== SYM_PTR
) {
2115 sym
= get_base_type(sym
);
2116 as
|= sym
->ctype
.as
;
2117 mod
|= sym
->ctype
.modifiers
;
2119 if (mod
& MOD_FORCE
)
2124 static void cast_to_as(struct expression
*e
, int as
)
2126 struct expression
*v
= e
->cast_expression
;
2128 if (!Wcast_to_address_space
)
2131 /* cast from constant 0 to pointer is OK */
2132 if (v
->type
== EXPR_VALUE
&& is_int_type(v
->ctype
) && !v
->value
)
2135 warning(e
->pos
, "cast adds address space to expression (<asn:%d>)", as
);
2138 static struct symbol
*evaluate_cast(struct expression
*expr
)
2140 struct expression
*target
= expr
->cast_expression
;
2141 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
2142 struct symbol
*t1
, *t2
;
2143 enum type type1
, type2
;
2149 expr
->ctype
= ctype
;
2150 expr
->cast_type
= ctype
;
2153 * Special case: a cast can be followed by an
2154 * initializer, in which case we need to pass
2155 * the type value down to that initializer rather
2156 * than trying to evaluate it as an expression
2158 * A more complex case is when the initializer is
2159 * dereferenced as part of a post-fix expression.
2160 * We need to produce an expression that can be dereferenced.
2162 if (target
->type
== EXPR_INITIALIZER
) {
2163 struct symbol
*sym
= expr
->cast_type
;
2164 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2166 sym
->initializer
= expr
->cast_expression
;
2167 evaluate_symbol(sym
);
2169 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2172 expr
->type
= EXPR_PREOP
;
2180 evaluate_expression(target
);
2184 if (t1
->type
== SYM_NODE
)
2185 t1
= t1
->ctype
.base_type
;
2186 if (t1
->type
== SYM_ENUM
)
2187 t1
= t1
->ctype
.base_type
;
2190 * You can always throw a value away by casting to
2191 * "void" - that's an implicit "force". Note that
2192 * the same is _not_ true of "void *".
2194 if (t1
== &void_ctype
)
2198 if (type1
== SYM_ARRAY
|| type1
== SYM_UNION
|| type1
== SYM_STRUCT
)
2199 warning(expr
->pos
, "cast to non-scalar");
2203 sparse_error(expr
->pos
, "cast from unknown type");
2206 if (t2
->type
== SYM_NODE
)
2207 t2
= t2
->ctype
.base_type
;
2208 if (t2
->type
== SYM_ENUM
)
2209 t2
= t2
->ctype
.base_type
;
2212 if (type2
== SYM_ARRAY
|| type2
== SYM_UNION
|| type2
== SYM_STRUCT
)
2213 warning(expr
->pos
, "cast from non-scalar");
2215 if (!(ctype
->ctype
.modifiers
& MOD_FORCE
) && t1
!= t2
) {
2216 if (t1
->type
== SYM_RESTRICT
)
2217 warning(expr
->pos
, "cast to restricted type");
2218 if (t2
->type
== SYM_RESTRICT
)
2219 warning(expr
->pos
, "cast from restricted type");
2222 as1
= get_as(ctype
);
2223 as2
= get_as(target
->ctype
);
2224 if (!as1
&& as2
> 0)
2225 warning(expr
->pos
, "cast removes address space of expression");
2226 if (as1
> 0 && as2
> 0 && as1
!= as2
)
2227 warning(expr
->pos
, "cast between address spaces (<asn:%d>-><asn:%d>)", as2
, as1
);
2228 if (as1
> 0 && !as2
)
2229 cast_to_as(expr
, as1
);
2232 * Casts of constant values are special: they
2233 * can be NULL, and thus need to be simplified
2236 if (target
->type
== EXPR_VALUE
)
2237 cast_value(expr
, ctype
, target
, target
->ctype
);
2244 * Evaluate a call expression with a symbol. This
2245 * should expand inline functions, and evaluate
2248 static int evaluate_symbol_call(struct expression
*expr
)
2250 struct expression
*fn
= expr
->fn
;
2251 struct symbol
*ctype
= fn
->ctype
;
2253 if (fn
->type
!= EXPR_PREOP
)
2256 if (ctype
->op
&& ctype
->op
->evaluate
)
2257 return ctype
->op
->evaluate(expr
);
2259 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2261 struct symbol
*curr
= current_fn
;
2262 current_fn
= ctype
->ctype
.base_type
;
2263 examine_fn_arguments(current_fn
);
2265 ret
= inline_function(expr
, ctype
);
2267 /* restore the old function */
2275 static struct symbol
*evaluate_call(struct expression
*expr
)
2278 struct symbol
*ctype
, *sym
;
2279 struct expression
*fn
= expr
->fn
;
2280 struct expression_list
*arglist
= expr
->args
;
2282 if (!evaluate_expression(fn
))
2284 sym
= ctype
= fn
->ctype
;
2285 if (ctype
->type
== SYM_NODE
)
2286 ctype
= ctype
->ctype
.base_type
;
2287 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
2288 ctype
= get_base_type(ctype
);
2289 if (!evaluate_arguments(sym
, ctype
, arglist
))
2291 if (ctype
->type
!= SYM_FN
) {
2292 sparse_error(expr
->pos
, "not a function %s", show_ident(sym
->ident
));
2295 args
= expression_list_size(expr
->args
);
2296 fnargs
= symbol_list_size(ctype
->arguments
);
2298 sparse_error(expr
->pos
, "not enough arguments for function %s", show_ident(sym
->ident
));
2299 if (args
> fnargs
&& !ctype
->variadic
)
2300 sparse_error(expr
->pos
, "too many arguments for function %s", show_ident(sym
->ident
));
2301 if (sym
->type
== SYM_NODE
) {
2302 if (evaluate_symbol_call(expr
))
2305 expr
->ctype
= ctype
->ctype
.base_type
;
2309 struct symbol
*evaluate_expression(struct expression
*expr
)
2316 switch (expr
->type
) {
2319 sparse_error(expr
->pos
, "value expression without a type");
2322 return evaluate_string(expr
);
2324 return evaluate_symbol_expression(expr
);
2326 if (!evaluate_expression(expr
->left
))
2328 if (!evaluate_expression(expr
->right
))
2330 return evaluate_binop(expr
);
2332 return evaluate_logical(expr
);
2334 evaluate_expression(expr
->left
);
2335 if (!evaluate_expression(expr
->right
))
2337 return evaluate_comma(expr
);
2339 if (!evaluate_expression(expr
->left
))
2341 if (!evaluate_expression(expr
->right
))
2343 return evaluate_compare(expr
);
2344 case EXPR_ASSIGNMENT
:
2345 if (!evaluate_expression(expr
->left
))
2347 if (!evaluate_expression(expr
->right
))
2349 return evaluate_assignment(expr
);
2351 if (!evaluate_expression(expr
->unop
))
2353 return evaluate_preop(expr
);
2355 if (!evaluate_expression(expr
->unop
))
2357 return evaluate_postop(expr
);
2359 case EXPR_IMPLIED_CAST
:
2360 return evaluate_cast(expr
);
2362 return evaluate_sizeof(expr
);
2363 case EXPR_PTRSIZEOF
:
2364 return evaluate_ptrsizeof(expr
);
2366 return evaluate_alignof(expr
);
2368 return evaluate_member_dereference(expr
);
2370 return evaluate_call(expr
);
2372 case EXPR_CONDITIONAL
:
2373 return evaluate_conditional_expression(expr
);
2374 case EXPR_STATEMENT
:
2375 expr
->ctype
= evaluate_statement(expr
->statement
);
2379 expr
->ctype
= &ptr_ctype
;
2383 /* Evaluate the type of the symbol .. */
2384 evaluate_symbol(expr
->symbol
);
2385 /* .. but the type of the _expression_ is a "type" */
2386 expr
->ctype
= &type_ctype
;
2389 /* These can not exist as stand-alone expressions */
2390 case EXPR_INITIALIZER
:
2391 case EXPR_IDENTIFIER
:
2394 sparse_error(expr
->pos
, "internal front-end error: initializer in expression");
2397 sparse_error(expr
->pos
, "internal front-end error: SLICE re-evaluated");
2403 static void check_duplicates(struct symbol
*sym
)
2406 struct symbol
*next
= sym
;
2408 while ((next
= next
->same_symbol
) != NULL
) {
2409 const char *typediff
;
2410 evaluate_symbol(next
);
2412 typediff
= type_difference(sym
, next
, 0, 0);
2414 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2415 show_ident(sym
->ident
),
2416 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
2421 unsigned long mod
= sym
->ctype
.modifiers
;
2422 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
2424 if (!(mod
& MOD_TOPLEVEL
))
2428 if (sym
->ident
== &main_ident
)
2430 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
2434 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
2436 struct symbol
*base_type
;
2444 sym
= examine_symbol_type(sym
);
2445 base_type
= get_base_type(sym
);
2449 /* Evaluate the initializers */
2450 if (sym
->initializer
)
2451 evaluate_initializer(sym
, &sym
->initializer
);
2453 /* And finally, evaluate the body of the symbol too */
2454 if (base_type
->type
== SYM_FN
) {
2455 struct symbol
*curr
= current_fn
;
2457 current_fn
= base_type
;
2459 examine_fn_arguments(base_type
);
2460 if (!base_type
->stmt
&& base_type
->inline_stmt
)
2462 if (base_type
->stmt
)
2463 evaluate_statement(base_type
->stmt
);
2471 void evaluate_symbol_list(struct symbol_list
*list
)
2475 FOR_EACH_PTR(list
, sym
) {
2476 evaluate_symbol(sym
);
2477 check_duplicates(sym
);
2478 } END_FOR_EACH_PTR(sym
);
2481 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
2483 struct expression
*expr
= stmt
->expression
;
2484 struct symbol
*ctype
, *fntype
;
2486 evaluate_expression(expr
);
2487 ctype
= degenerate(expr
);
2488 fntype
= current_fn
->ctype
.base_type
;
2489 if (!fntype
|| fntype
== &void_ctype
) {
2490 if (expr
&& ctype
!= &void_ctype
)
2491 sparse_error(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
2496 sparse_error(stmt
->pos
, "return with no return value");
2501 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, ctype
, "return expression", '=');
2505 static void evaluate_if_statement(struct statement
*stmt
)
2507 if (!stmt
->if_conditional
)
2510 evaluate_conditional(stmt
->if_conditional
, 0);
2511 evaluate_statement(stmt
->if_true
);
2512 evaluate_statement(stmt
->if_false
);
2515 static void evaluate_iterator(struct statement
*stmt
)
2517 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
2518 evaluate_conditional(stmt
->iterator_post_condition
,1);
2519 evaluate_statement(stmt
->iterator_pre_statement
);
2520 evaluate_statement(stmt
->iterator_statement
);
2521 evaluate_statement(stmt
->iterator_post_statement
);
2524 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
2526 switch (*constraint
) {
2527 case '=': /* Assignment */
2528 case '+': /* Update */
2531 sparse_error(expr
->pos
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
2535 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
2537 switch (*constraint
) {
2538 case '=': /* Assignment */
2539 case '+': /* Update */
2540 sparse_error(expr
->pos
, "input constraint with assignment (\"%s\")", constraint
);
2544 static void evaluate_asm_statement(struct statement
*stmt
)
2546 struct expression
*expr
;
2549 expr
= stmt
->asm_string
;
2550 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2551 sparse_error(stmt
->pos
, "need constant string for inline asm");
2556 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
2557 struct ident
*ident
;
2560 case 0: /* Identifier */
2562 ident
= (struct ident
*)expr
;
2565 case 1: /* Constraint */
2567 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2568 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
2569 *THIS_ADDRESS(expr
) = NULL
;
2572 verify_output_constraint(expr
, expr
->string
->data
);
2575 case 2: /* Expression */
2577 if (!evaluate_expression(expr
))
2579 if (!lvalue_expression(expr
))
2580 warning(expr
->pos
, "asm output is not an lvalue");
2581 evaluate_assign_to(expr
, expr
->ctype
);
2584 } END_FOR_EACH_PTR(expr
);
2587 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
2588 struct ident
*ident
;
2591 case 0: /* Identifier */
2593 ident
= (struct ident
*)expr
;
2596 case 1: /* Constraint */
2598 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2599 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
2600 *THIS_ADDRESS(expr
) = NULL
;
2603 verify_input_constraint(expr
, expr
->string
->data
);
2606 case 2: /* Expression */
2608 if (!evaluate_expression(expr
))
2612 } END_FOR_EACH_PTR(expr
);
2614 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
2616 sparse_error(stmt
->pos
, "bad asm output");
2619 if (expr
->type
== EXPR_STRING
)
2621 sparse_error(expr
->pos
, "asm clobber is not a string");
2622 } END_FOR_EACH_PTR(expr
);
2625 static void evaluate_case_statement(struct statement
*stmt
)
2627 evaluate_expression(stmt
->case_expression
);
2628 evaluate_expression(stmt
->case_to
);
2629 evaluate_statement(stmt
->case_statement
);
2632 static void check_case_type(struct expression
*switch_expr
,
2633 struct expression
*case_expr
,
2634 struct expression
**enumcase
)
2636 struct symbol
*switch_type
, *case_type
;
2639 switch_type
= switch_expr
->ctype
;
2640 case_type
= evaluate_expression(case_expr
);
2642 if (case_type
&& switch_type
) {
2645 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
2646 else if (is_enum_type(case_type
))
2647 *enumcase
= case_expr
;
2650 /* Both integer types? */
2651 if (compatible_restricted_binop(SPECIAL_EQUAL
, &switch_expr
, &case_expr
))
2653 if (is_int_type(switch_type
) && is_int_type(case_type
))
2657 sparse_error(case_expr
->pos
, "incompatible types for 'case' statement");
2660 static void evaluate_switch_statement(struct statement
*stmt
)
2663 struct expression
*enumcase
= NULL
;
2664 struct expression
**enumcase_holder
;
2666 evaluate_expression(stmt
->switch_expression
);
2667 evaluate_statement(stmt
->switch_statement
);
2668 if (!stmt
->switch_expression
)
2670 enumcase_holder
= is_enum_type(stmt
->switch_expression
->ctype
)
2671 ? NULL
/* Only check cases against switch */
2674 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
2675 struct statement
*case_stmt
= sym
->stmt
;
2676 check_case_type(stmt
->switch_expression
, case_stmt
->case_expression
, enumcase_holder
);
2677 check_case_type(stmt
->switch_expression
, case_stmt
->case_to
, enumcase_holder
);
2678 } END_FOR_EACH_PTR(sym
);
2681 struct symbol
*evaluate_statement(struct statement
*stmt
)
2686 switch (stmt
->type
) {
2687 case STMT_DECLARATION
: {
2689 FOR_EACH_PTR(stmt
->declaration
, s
) {
2691 } END_FOR_EACH_PTR(s
);
2696 return evaluate_return_expression(stmt
);
2698 case STMT_EXPRESSION
:
2699 if (!evaluate_expression(stmt
->expression
))
2701 return degenerate(stmt
->expression
);
2703 case STMT_COMPOUND
: {
2704 struct statement
*s
;
2705 struct symbol
*type
= NULL
;
2707 /* Evaluate the return symbol in the compound statement */
2708 evaluate_symbol(stmt
->ret
);
2711 * Then, evaluate each statement, making the type of the
2712 * compound statement be the type of the last statement
2715 FOR_EACH_PTR(stmt
->stmts
, s
) {
2716 type
= evaluate_statement(s
);
2717 } END_FOR_EACH_PTR(s
);
2723 evaluate_if_statement(stmt
);
2726 evaluate_iterator(stmt
);
2729 evaluate_switch_statement(stmt
);
2732 evaluate_case_statement(stmt
);
2735 return evaluate_statement(stmt
->label_statement
);
2737 evaluate_expression(stmt
->goto_expression
);
2742 evaluate_asm_statement(stmt
);
2745 evaluate_expression(stmt
->expression
);
2748 evaluate_expression(stmt
->range_expression
);
2749 evaluate_expression(stmt
->range_low
);
2750 evaluate_expression(stmt
->range_high
);