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
;
340 static inline int is_string_type(struct symbol
*type
)
342 if (type
->type
== SYM_NODE
)
343 type
= type
->ctype
.base_type
;
344 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
347 static struct symbol
*bad_expr_type(struct expression
*expr
)
349 sparse_error(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
350 switch (expr
->type
) {
353 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
354 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
358 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
364 return expr
->ctype
= &bad_ctype
;
367 static struct symbol
*compatible_float_binop(struct expression
**lp
, struct expression
**rp
)
369 struct expression
*left
= *lp
, *right
= *rp
;
370 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
372 if (ltype
->type
== SYM_NODE
)
373 ltype
= ltype
->ctype
.base_type
;
374 if (rtype
->type
== SYM_NODE
)
375 rtype
= rtype
->ctype
.base_type
;
376 if (is_float_type(ltype
)) {
377 if (is_int_type(rtype
))
379 if (is_float_type(rtype
)) {
380 unsigned long lmod
= ltype
->ctype
.modifiers
;
381 unsigned long rmod
= rtype
->ctype
.modifiers
;
382 lmod
&= MOD_LONG
| MOD_LONGLONG
;
383 rmod
&= MOD_LONG
| MOD_LONGLONG
;
393 if (!is_float_type(rtype
) || !is_int_type(ltype
))
396 *lp
= cast_to(left
, rtype
);
399 *rp
= cast_to(right
, ltype
);
403 static struct symbol
*compatible_integer_binop(struct expression
**lp
, struct expression
**rp
)
405 struct expression
*left
= *lp
, *right
= *rp
;
406 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
408 if (ltype
->type
== SYM_NODE
)
409 ltype
= ltype
->ctype
.base_type
;
410 if (rtype
->type
== SYM_NODE
)
411 rtype
= rtype
->ctype
.base_type
;
412 if (is_int_type(ltype
) && is_int_type(rtype
)) {
413 struct symbol
*ctype
= bigger_int_type(ltype
, rtype
);
415 *lp
= cast_to(left
, ctype
);
416 *rp
= cast_to(right
, ctype
);
422 static int restricted_value(struct expression
*v
, struct symbol
*type
)
424 if (v
->type
!= EXPR_VALUE
)
431 static int restricted_binop(int op
, struct symbol
*type
)
440 case SPECIAL_NOTEQUAL
:
441 case SPECIAL_AND_ASSIGN
:
442 case SPECIAL_OR_ASSIGN
:
443 case SPECIAL_XOR_ASSIGN
:
450 static int restricted_unop(int op
, struct symbol
*type
)
452 if (op
== '~' && type
->bit_size
>= bits_in_int
)
459 static struct symbol
*compatible_restricted_binop(int op
, struct expression
**lp
, struct expression
**rp
)
461 struct expression
*left
= *lp
, *right
= *rp
;
462 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
463 struct symbol
*type
= NULL
;
465 if (ltype
->type
== SYM_NODE
)
466 ltype
= ltype
->ctype
.base_type
;
467 if (rtype
->type
== SYM_NODE
)
468 rtype
= rtype
->ctype
.base_type
;
470 warn_for_different_enum_types(right
->pos
, ltype
, rtype
);
472 if (ltype
->type
== SYM_ENUM
)
473 ltype
= ltype
->ctype
.base_type
;
474 if (rtype
->type
== SYM_ENUM
)
475 rtype
= rtype
->ctype
.base_type
;
477 if (is_restricted_type(ltype
)) {
478 if (is_restricted_type(rtype
)) {
482 if (!restricted_value(right
, ltype
))
485 } else if (is_restricted_type(rtype
)) {
486 if (!restricted_value(left
, rtype
))
491 if (restricted_binop(op
, type
))
496 static struct symbol
*evaluate_arith(struct expression
*expr
, int float_ok
)
498 struct symbol
*ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
499 if (!ctype
&& float_ok
)
500 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
502 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
507 return bad_expr_type(expr
);
510 static inline int lvalue_expression(struct expression
*expr
)
512 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
515 static int ptr_object_size(struct symbol
*ptr_type
)
517 if (ptr_type
->type
== SYM_NODE
)
518 ptr_type
= ptr_type
->ctype
.base_type
;
519 if (ptr_type
->type
== SYM_PTR
)
520 ptr_type
= get_base_type(ptr_type
);
521 return ptr_type
->bit_size
;
524 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*ctype
, struct expression
**ip
)
526 struct expression
*i
= *ip
;
527 struct symbol
*ptr_type
= ctype
;
530 if (ptr_type
->type
== SYM_NODE
)
531 ptr_type
= ptr_type
->ctype
.base_type
;
533 if (!is_int_type(i
->ctype
))
534 return bad_expr_type(expr
);
536 examine_symbol_type(ctype
);
538 if (!ctype
->ctype
.base_type
) {
539 sparse_error(expr
->pos
, "missing type information");
543 /* Get the size of whatever the pointer points to */
544 bit_size
= ptr_object_size(ctype
);
546 if (bit_size
> bits_in_char
) {
547 int multiply
= bit_size
>> 3;
548 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
550 if (i
->type
== EXPR_VALUE
) {
551 val
->value
= i
->value
* multiply
;
552 val
->ctype
= size_t_ctype
;
555 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
557 val
->ctype
= size_t_ctype
;
558 val
->value
= bit_size
>> 3;
561 mul
->ctype
= size_t_ctype
;
573 static struct symbol
*evaluate_add(struct expression
*expr
)
575 struct expression
*left
= expr
->left
, *right
= expr
->right
;
576 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
578 if (is_ptr_type(ltype
))
579 return evaluate_ptr_add(expr
, degenerate(left
), &expr
->right
);
581 if (is_ptr_type(rtype
))
582 return evaluate_ptr_add(expr
, degenerate(right
), &expr
->left
);
584 return evaluate_arith(expr
, 1);
587 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
588 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
591 unsigned long mod1
, mod2
, diff
;
592 unsigned long as1
, as2
;
594 struct symbol
*base1
, *base2
;
596 if (target
== source
)
598 if (!target
|| !source
)
599 return "different types";
601 * Peel of per-node information.
602 * FIXME! Check alignment and context too here!
604 mod1
= target
->ctype
.modifiers
;
605 as1
= target
->ctype
.as
;
606 mod2
= source
->ctype
.modifiers
;
607 as2
= source
->ctype
.as
;
608 if (target
->type
== SYM_NODE
) {
609 target
= target
->ctype
.base_type
;
612 if (target
->type
== SYM_PTR
) {
616 mod1
|= target
->ctype
.modifiers
;
617 as1
|= target
->ctype
.as
;
619 if (source
->type
== SYM_NODE
) {
620 source
= source
->ctype
.base_type
;
623 if (source
->type
== SYM_PTR
) {
627 mod2
|= source
->ctype
.modifiers
;
628 as2
|= source
->ctype
.as
;
630 if (target
->type
== SYM_ENUM
) {
631 target
= target
->ctype
.base_type
;
635 if (source
->type
== SYM_ENUM
) {
636 source
= source
->ctype
.base_type
;
641 if (target
== source
)
643 if (!target
|| !source
)
644 return "different types";
646 type1
= target
->type
;
647 base1
= target
->ctype
.base_type
;
649 type2
= source
->type
;
650 base2
= source
->ctype
.base_type
;
653 * Pointers to functions compare as the function itself
655 if (type1
== SYM_PTR
&& base1
) {
656 base1
= examine_symbol_type(base1
);
657 switch (base1
->type
) {
661 base1
= base1
->ctype
.base_type
;
666 if (type2
== SYM_PTR
&& base2
) {
667 base2
= examine_symbol_type(base2
);
668 switch (base2
->type
) {
672 base2
= base2
->ctype
.base_type
;
678 /* Arrays degenerate to pointers for type comparisons */
679 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
680 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
682 if (type1
!= type2
|| type1
== SYM_RESTRICT
)
683 return "different base types";
685 /* Must be same address space to be comparable */
686 if (Waddress_space
&& as1
!= as2
)
687 return "different address spaces";
689 /* Ignore differences in storage types or addressability */
690 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
691 diff
&= (mod1
& ~target_mod_ignore
) | (mod2
& ~source_mod_ignore
);
694 return "different type sizes";
695 if (diff
& ~MOD_SIGNEDNESS
)
696 return "different modifiers";
698 /* Differs in signedness only.. */
701 * Warn if both are explicitly signed ("unsigned" is obvously
702 * always explicit, and since we know one of them has to be
703 * unsigned, we check if the signed one was explicit).
705 if ((mod1
| mod2
) & MOD_EXPLICITLY_SIGNED
)
706 return "different explicit signedness";
709 * "char" matches both "unsigned char" and "signed char",
710 * so if the explicit test didn't trigger, then we should
711 * not warn about a char.
713 if (!(mod1
& MOD_CHAR
))
714 return "different signedness";
718 if (type1
== SYM_FN
) {
720 struct symbol
*arg1
, *arg2
;
721 if (base1
->variadic
!= base2
->variadic
)
722 return "incompatible variadic arguments";
723 PREPARE_PTR_LIST(target
->arguments
, arg1
);
724 PREPARE_PTR_LIST(source
->arguments
, arg2
);
728 diff
= type_difference(arg1
, arg2
, 0, 0);
730 static char argdiff
[80];
731 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
740 FINISH_PTR_LIST(arg2
);
741 FINISH_PTR_LIST(arg1
);
750 static int is_null_ptr(struct expression
*expr
)
752 if (expr
->type
!= EXPR_VALUE
|| expr
->value
)
754 if (!is_ptr_type(expr
->ctype
))
755 warning(expr
->pos
, "Using plain integer as NULL pointer");
759 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
761 /* NULL expression? Just return the type of the "other side" */
770 * Ignore differences in "volatile" and "const"ness when
771 * subtracting pointers
773 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
775 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
**rp
)
777 const char *typediff
;
778 struct symbol
*ctype
;
779 struct symbol
*ltype
, *rtype
;
780 struct expression
*r
= *rp
;
782 ltype
= degenerate(l
);
783 rtype
= degenerate(r
);
786 * If it is an integer subtract: the ptr add case will do the
789 if (!is_ptr_type(rtype
))
790 return evaluate_ptr_add(expr
, degenerate(l
), rp
);
793 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
795 ctype
= common_ptr_type(l
, r
);
797 sparse_error(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
801 examine_symbol_type(ctype
);
803 /* Figure out the base type we point to */
804 if (ctype
->type
== SYM_NODE
)
805 ctype
= ctype
->ctype
.base_type
;
806 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
807 sparse_error(expr
->pos
, "subtraction of functions? Share your drugs");
810 ctype
= get_base_type(ctype
);
812 expr
->ctype
= ssize_t_ctype
;
813 if (ctype
->bit_size
> bits_in_char
) {
814 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
815 struct expression
*div
= expr
;
816 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
817 unsigned long value
= ctype
->bit_size
>> 3;
819 val
->ctype
= size_t_ctype
;
822 if (value
& (value
-1)) {
823 if (Wptr_subtraction_blows
)
824 warning(expr
->pos
, "potentially expensive pointer subtraction");
828 sub
->ctype
= ssize_t_ctype
;
837 return ssize_t_ctype
;
840 static struct symbol
*evaluate_sub(struct expression
*expr
)
842 struct expression
*left
= expr
->left
;
843 struct symbol
*ltype
= left
->ctype
;
845 if (is_ptr_type(ltype
))
846 return evaluate_ptr_sub(expr
, left
, &expr
->right
);
848 return evaluate_arith(expr
, 1);
851 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
853 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
855 struct symbol
*ctype
;
860 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
861 warning(expr
->pos
, "assignment expression in conditional");
863 ctype
= evaluate_expression(expr
);
865 if (is_safe_type(ctype
))
866 warning(expr
->pos
, "testing a 'safe expression'");
872 static struct symbol
*evaluate_logical(struct expression
*expr
)
874 if (!evaluate_conditional(expr
->left
, 0))
876 if (!evaluate_conditional(expr
->right
, 0))
879 expr
->ctype
= &bool_ctype
;
883 static struct symbol
*evaluate_shift(struct expression
*expr
)
885 struct expression
*left
= expr
->left
, *right
= expr
->right
;
886 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
888 if (ltype
->type
== SYM_NODE
)
889 ltype
= ltype
->ctype
.base_type
;
890 if (rtype
->type
== SYM_NODE
)
891 rtype
= rtype
->ctype
.base_type
;
892 if (is_int_type(ltype
) && is_int_type(rtype
)) {
893 struct symbol
*ctype
= integer_promotion(ltype
);
894 expr
->left
= cast_to(expr
->left
, ctype
);
896 ctype
= integer_promotion(rtype
);
897 expr
->right
= cast_to(expr
->right
, ctype
);
900 return bad_expr_type(expr
);
903 static struct symbol
*evaluate_binop(struct expression
*expr
)
906 // addition can take ptr+int, fp and int
908 return evaluate_add(expr
);
910 // subtraction can take ptr-ptr, fp and int
912 return evaluate_sub(expr
);
914 // Arithmetic operations can take fp and int
916 return evaluate_arith(expr
, 1);
918 // shifts do integer promotions, but that's it.
919 case SPECIAL_LEFTSHIFT
: case SPECIAL_RIGHTSHIFT
:
920 return evaluate_shift(expr
);
922 // The rest are integer operations
923 // '%', '&', '^', '|'
925 return evaluate_arith(expr
, 0);
929 static struct symbol
*evaluate_comma(struct expression
*expr
)
931 expr
->ctype
= expr
->right
->ctype
;
935 static int modify_for_unsigned(int op
)
938 op
= SPECIAL_UNSIGNED_LT
;
940 op
= SPECIAL_UNSIGNED_GT
;
941 else if (op
== SPECIAL_LTE
)
942 op
= SPECIAL_UNSIGNED_LTE
;
943 else if (op
== SPECIAL_GTE
)
944 op
= SPECIAL_UNSIGNED_GTE
;
948 static struct symbol
*evaluate_compare(struct expression
*expr
)
950 struct expression
*left
= expr
->left
, *right
= expr
->right
;
951 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
952 struct symbol
*ctype
;
955 if (is_type_type(ltype
) && is_type_type(rtype
))
958 if (is_safe_type(ltype
) || is_safe_type(rtype
))
959 warning(expr
->pos
, "testing a 'safe expression'");
962 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
963 // FIXME! Check the types for compatibility
964 expr
->op
= modify_for_unsigned(expr
->op
);
968 ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
970 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
971 expr
->op
= modify_for_unsigned(expr
->op
);
975 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
979 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
981 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
982 expr
->op
= modify_for_unsigned(expr
->op
);
989 expr
->ctype
= &bool_ctype
;
994 * FIXME!! This should do casts, array degeneration etc..
996 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
998 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
1000 if (ltype
->type
== SYM_NODE
)
1001 ltype
= ltype
->ctype
.base_type
;
1003 if (rtype
->type
== SYM_NODE
)
1004 rtype
= rtype
->ctype
.base_type
;
1006 if (ltype
->type
== SYM_PTR
) {
1007 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
1011 if (rtype
->type
== SYM_PTR
) {
1012 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
1019 * NOTE! The degenerate case of "x ? : y", where we don't
1020 * have a true case, this will possibly promote "x" to the
1021 * same type as "y", and thus _change_ the conditional
1022 * test in the expression. But since promotion is "safe"
1023 * for testing, that's ok.
1025 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
1027 struct expression
**true;
1028 struct symbol
*ctype
, *ltype
, *rtype
;
1029 const char * typediff
;
1031 if (!evaluate_conditional(expr
->conditional
, 0))
1033 if (!evaluate_expression(expr
->cond_false
))
1036 ctype
= degenerate(expr
->conditional
);
1037 rtype
= degenerate(expr
->cond_false
);
1039 true = &expr
->conditional
;
1041 if (expr
->cond_true
) {
1042 if (!evaluate_expression(expr
->cond_true
))
1044 ltype
= degenerate(expr
->cond_true
);
1045 true = &expr
->cond_true
;
1048 ctype
= compatible_integer_binop(true, &expr
->cond_false
);
1051 ctype
= compatible_ptr_type(*true, expr
->cond_false
);
1054 ctype
= compatible_float_binop(true, &expr
->cond_false
);
1057 ctype
= compatible_restricted_binop('?', true, &expr
->cond_false
);
1061 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
1064 sparse_error(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
1068 expr
->ctype
= ctype
;
1072 /* FP assignments can not do modulo or bit operations */
1073 static int compatible_float_op(int op
)
1076 op
== SPECIAL_ADD_ASSIGN
||
1077 op
== SPECIAL_SUB_ASSIGN
||
1078 op
== SPECIAL_MUL_ASSIGN
||
1079 op
== SPECIAL_DIV_ASSIGN
;
1082 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1083 struct expression
**rp
, struct symbol
*source
, const char *where
, int op
)
1085 const char *typediff
;
1089 if (is_int_type(target
)) {
1090 if (is_int_type(source
))
1092 if (is_float_type(source
))
1094 } else if (is_float_type(target
)) {
1095 if (!compatible_float_op(op
)) {
1096 sparse_error(expr
->pos
, "invalid assignment");
1099 if (is_int_type(source
))
1101 if (is_float_type(source
))
1103 } else if (is_restricted_type(target
)) {
1104 if (restricted_binop(op
, target
)) {
1105 sparse_error(expr
->pos
, "bad restricted assignment");
1108 if (!restricted_value(*rp
, target
))
1110 } else if (is_ptr_type(target
)) {
1111 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1112 evaluate_ptr_add(expr
, target
, rp
);
1116 sparse_error(expr
->pos
, "invalid pointer assignment");
1119 } else if (op
!= '=') {
1120 sparse_error(expr
->pos
, "invalid assignment");
1124 /* It's ok if the target is more volatile or const than the source */
1125 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
1129 /* Pointer destination? */
1131 target_as
= t
->ctype
.as
;
1132 if (t
->type
== SYM_NODE
) {
1133 t
= t
->ctype
.base_type
;
1134 target_as
|= t
->ctype
.as
;
1136 if (t
->type
== SYM_PTR
|| t
->type
== SYM_FN
|| t
->type
== SYM_ARRAY
) {
1137 struct expression
*right
= *rp
;
1138 struct symbol
*s
= source
;
1141 // NULL pointer is always ok
1142 if (is_null_ptr(right
))
1145 /* "void *" matches anything as long as the address space is ok */
1146 source_as
= s
->ctype
.as
;
1147 if (s
->type
== SYM_NODE
) {
1148 s
= s
->ctype
.base_type
;
1149 source_as
|= s
->ctype
.as
;
1151 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
1152 s
= get_base_type(s
);
1153 t
= get_base_type(t
);
1154 if (s
== &void_ctype
|| t
== &void_ctype
)
1159 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1160 info(expr
->pos
, " expected %s", show_typename(target
));
1161 info(expr
->pos
, " got %s", show_typename(source
));
1162 *rp
= cast_to(*rp
, target
);
1165 *rp
= cast_to(*rp
, target
);
1169 static void mark_assigned(struct expression
*expr
)
1175 switch (expr
->type
) {
1180 if (sym
->type
!= SYM_NODE
)
1182 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1186 mark_assigned(expr
->left
);
1187 mark_assigned(expr
->right
);
1190 mark_assigned(expr
->cast_expression
);
1193 mark_assigned(expr
->base
);
1201 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1203 if (type
->ctype
.modifiers
& MOD_CONST
)
1204 sparse_error(left
->pos
, "assignment to const expression");
1206 /* We know left is an lvalue, so it's a "preop-*" */
1207 mark_assigned(left
->unop
);
1210 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1212 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1213 struct expression
*where
= expr
;
1214 struct symbol
*ltype
, *rtype
;
1216 if (!lvalue_expression(left
)) {
1217 sparse_error(expr
->pos
, "not an lvalue");
1221 ltype
= left
->ctype
;
1223 rtype
= degenerate(right
);
1225 if (!compatible_assignment_types(where
, ltype
, &where
->right
, rtype
, "assignment", expr
->op
))
1228 evaluate_assign_to(left
, ltype
);
1230 expr
->ctype
= ltype
;
1234 static void examine_fn_arguments(struct symbol
*fn
)
1238 FOR_EACH_PTR(fn
->arguments
, s
) {
1239 struct symbol
*arg
= evaluate_symbol(s
);
1240 /* Array/function arguments silently degenerate into pointers */
1246 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1247 if (arg
->type
== SYM_ARRAY
)
1248 ptr
->ctype
= arg
->ctype
;
1250 ptr
->ctype
.base_type
= arg
;
1251 ptr
->ctype
.as
|= s
->ctype
.as
;
1252 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1254 s
->ctype
.base_type
= ptr
;
1256 s
->ctype
.modifiers
= 0;
1259 examine_symbol_type(s
);
1266 } END_FOR_EACH_PTR(s
);
1269 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1271 /* Take the modifiers of the pointer, and apply them to the member */
1272 mod
|= sym
->ctype
.modifiers
;
1273 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1274 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1276 newsym
->ctype
.as
= as
;
1277 newsym
->ctype
.modifiers
= mod
;
1283 #define MOD_PTRINHERIT (MOD_VOLATILE | MOD_CONST | MOD_NODEREF | MOD_STORAGE)
1285 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1287 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1288 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1290 node
->ctype
.base_type
= ptr
;
1291 ptr
->bit_size
= bits_in_pointer
;
1292 ptr
->ctype
.alignment
= pointer_alignment
;
1294 node
->bit_size
= bits_in_pointer
;
1295 node
->ctype
.alignment
= pointer_alignment
;
1298 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1299 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1300 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1302 if (sym
->type
== SYM_NODE
) {
1303 ptr
->ctype
.as
|= sym
->ctype
.as
;
1304 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1305 sym
= sym
->ctype
.base_type
;
1307 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1308 ptr
->ctype
.as
|= sym
->ctype
.as
;
1309 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1310 sym
= sym
->ctype
.base_type
;
1312 ptr
->ctype
.base_type
= sym
;
1317 /* Arrays degenerate into pointers on pointer arithmetic */
1318 static struct symbol
*degenerate(struct expression
*expr
)
1320 struct symbol
*ctype
, *base
;
1324 ctype
= expr
->ctype
;
1327 base
= examine_symbol_type(ctype
);
1328 if (ctype
->type
== SYM_NODE
)
1329 base
= ctype
->ctype
.base_type
;
1331 * Arrays degenerate into pointers to the entries, while
1332 * functions degenerate into pointers to themselves.
1333 * If array was part of non-lvalue compound, we create a copy
1334 * of that compound first and then act as if we were dealing with
1335 * the corresponding field in there.
1337 switch (base
->type
) {
1339 if (expr
->type
== EXPR_SLICE
) {
1340 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1341 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1343 a
->ctype
.base_type
= expr
->base
->ctype
;
1344 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1345 a
->array_size
= expr
->base
->ctype
->array_size
;
1347 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1349 e0
->ctype
= &lazy_ptr_ctype
;
1351 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1354 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1356 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1358 e2
->right
= expr
->base
;
1360 e2
->ctype
= expr
->base
->ctype
;
1362 if (expr
->r_bitpos
) {
1363 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1366 e3
->right
= alloc_const_expression(expr
->pos
,
1367 expr
->r_bitpos
>> 3);
1368 e3
->ctype
= &lazy_ptr_ctype
;
1373 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1376 e4
->ctype
= &lazy_ptr_ctype
;
1379 expr
->type
= EXPR_PREOP
;
1383 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1384 sparse_error(expr
->pos
, "strange non-value function or array");
1387 *expr
= *expr
->unop
;
1388 ctype
= create_pointer(expr
, ctype
, 1);
1389 expr
->ctype
= ctype
;
1396 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1398 struct expression
*op
= expr
->unop
;
1399 struct symbol
*ctype
;
1401 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1402 sparse_error(expr
->pos
, "not addressable");
1408 if (expr
->type
== EXPR_SYMBOL
) {
1409 struct symbol
*sym
= expr
->symbol
;
1410 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1414 * symbol expression evaluation is lazy about the type
1415 * of the sub-expression, so we may have to generate
1416 * the type here if so..
1418 if (expr
->ctype
== &lazy_ptr_ctype
) {
1419 ctype
= create_pointer(expr
, ctype
, 0);
1420 expr
->ctype
= ctype
;
1426 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1428 struct expression
*op
= expr
->unop
;
1429 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1431 /* Simplify: *&(expr) => (expr) */
1432 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1437 /* Dereferencing a node drops all the node information. */
1438 if (ctype
->type
== SYM_NODE
)
1439 ctype
= ctype
->ctype
.base_type
;
1441 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1442 target
= ctype
->ctype
.base_type
;
1444 switch (ctype
->type
) {
1446 sparse_error(expr
->pos
, "cannot derefence this type");
1449 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1450 merge_type(node
, ctype
);
1454 if (!lvalue_expression(op
)) {
1455 sparse_error(op
->pos
, "non-lvalue array??");
1459 /* Do the implied "addressof" on the array */
1463 * When an array is dereferenced, we need to pick
1464 * up the attributes of the original node too..
1466 merge_type(node
, op
->ctype
);
1467 merge_type(node
, ctype
);
1471 node
->bit_size
= target
->bit_size
;
1472 node
->array_size
= target
->array_size
;
1479 * Unary post-ops: x++ and x--
1481 static struct symbol
*evaluate_postop(struct expression
*expr
)
1483 struct expression
*op
= expr
->unop
;
1484 struct symbol
*ctype
= op
->ctype
;
1486 if (!lvalue_expression(expr
->unop
)) {
1487 sparse_error(expr
->pos
, "need lvalue expression for ++/--");
1490 if (is_restricted_type(ctype
) && restricted_unop(expr
->op
, ctype
)) {
1491 sparse_error(expr
->pos
, "bad operation on restricted");
1495 evaluate_assign_to(op
, ctype
);
1497 expr
->ctype
= ctype
;
1499 if (is_ptr_type(ctype
))
1500 expr
->op_value
= ptr_object_size(ctype
) >> 3;
1505 static struct symbol
*evaluate_sign(struct expression
*expr
)
1507 struct symbol
*ctype
= expr
->unop
->ctype
;
1508 if (is_int_type(ctype
)) {
1509 struct symbol
*rtype
= rtype
= integer_promotion(ctype
);
1510 expr
->unop
= cast_to(expr
->unop
, rtype
);
1512 } else if (is_float_type(ctype
) && expr
->op
!= '~') {
1513 /* no conversions needed */
1514 } else if (is_restricted_type(ctype
) && !restricted_unop(expr
->op
, ctype
)) {
1515 /* no conversions needed */
1517 return bad_expr_type(expr
);
1519 if (expr
->op
== '+')
1520 *expr
= *expr
->unop
;
1521 expr
->ctype
= ctype
;
1525 static struct symbol
*evaluate_preop(struct expression
*expr
)
1527 struct symbol
*ctype
= expr
->unop
->ctype
;
1531 *expr
= *expr
->unop
;
1537 return evaluate_sign(expr
);
1540 return evaluate_dereference(expr
);
1543 return evaluate_addressof(expr
);
1545 case SPECIAL_INCREMENT
:
1546 case SPECIAL_DECREMENT
:
1548 * From a type evaluation standpoint the pre-ops are
1549 * the same as the postops
1551 return evaluate_postop(expr
);
1554 if (is_safe_type(ctype
))
1555 warning(expr
->pos
, "testing a 'safe expression'");
1556 if (is_float_type(ctype
)) {
1557 struct expression
*arg
= expr
->unop
;
1558 expr
->type
= EXPR_BINOP
;
1559 expr
->op
= SPECIAL_EQUAL
;
1561 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1562 expr
->right
->ctype
= ctype
;
1563 expr
->right
->fvalue
= 0;
1565 ctype
= &bool_ctype
;
1571 expr
->ctype
= ctype
;
1575 static struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1577 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1578 struct ptr_list
*list
= head
;
1584 for (i
= 0; i
< list
->nr
; i
++) {
1585 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1587 if (sym
->ident
!= ident
)
1589 *offset
= sym
->offset
;
1592 struct symbol
*ctype
= sym
->ctype
.base_type
;
1596 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1598 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1601 *offset
+= sym
->offset
;
1605 } while ((list
= list
->next
) != head
);
1609 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1611 struct expression
*add
;
1614 * Create a new add-expression
1616 * NOTE! Even if we just add zero, we need a new node
1617 * for the member pointer, since it has a different
1618 * type than the original pointer. We could make that
1619 * be just a cast, but the fact is, a node is a node,
1620 * so we might as well just do the "add zero" here.
1622 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1625 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1626 add
->right
->ctype
= &int_ctype
;
1627 add
->right
->value
= offset
;
1630 * The ctype of the pointer will be lazily evaluated if
1631 * we ever take the address of this member dereference..
1633 add
->ctype
= &lazy_ptr_ctype
;
1637 /* structure/union dereference */
1638 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1641 struct symbol
*ctype
, *member
;
1642 struct expression
*deref
= expr
->deref
, *add
;
1643 struct ident
*ident
= expr
->member
;
1647 if (!evaluate_expression(deref
))
1650 sparse_error(expr
->pos
, "bad member name");
1654 ctype
= deref
->ctype
;
1655 address_space
= ctype
->ctype
.as
;
1656 mod
= ctype
->ctype
.modifiers
;
1657 if (ctype
->type
== SYM_NODE
) {
1658 ctype
= ctype
->ctype
.base_type
;
1659 address_space
|= ctype
->ctype
.as
;
1660 mod
|= ctype
->ctype
.modifiers
;
1662 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1663 sparse_error(expr
->pos
, "expected structure or union");
1666 examine_symbol_type(ctype
);
1668 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1670 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1671 const char *name
= "<unnamed>";
1674 name
= ctype
->ident
->name
;
1675 namelen
= ctype
->ident
->len
;
1677 sparse_error(expr
->pos
, "no member '%s' in %s %.*s",
1678 show_ident(ident
), type
, namelen
, name
);
1683 * The member needs to take on the address space and modifiers of
1684 * the "parent" type.
1686 member
= convert_to_as_mod(member
, address_space
, mod
);
1687 ctype
= get_base_type(member
);
1689 if (!lvalue_expression(deref
)) {
1690 if (deref
->type
!= EXPR_SLICE
) {
1694 expr
->base
= deref
->base
;
1695 expr
->r_bitpos
= deref
->r_bitpos
;
1697 expr
->r_bitpos
+= offset
<< 3;
1698 expr
->type
= EXPR_SLICE
;
1699 expr
->r_nrbits
= member
->bit_size
;
1700 expr
->r_bitpos
+= member
->bit_offset
;
1701 expr
->ctype
= member
;
1705 deref
= deref
->unop
;
1706 expr
->deref
= deref
;
1708 add
= evaluate_offset(deref
, offset
);
1709 expr
->type
= EXPR_PREOP
;
1713 expr
->ctype
= member
;
1717 static int is_promoted(struct expression
*expr
)
1720 switch (expr
->type
) {
1723 case EXPR_CONDITIONAL
:
1747 static struct symbol
*evaluate_cast(struct expression
*);
1749 static struct symbol
*evaluate_type_information(struct expression
*expr
)
1751 struct symbol
*sym
= expr
->cast_type
;
1753 sym
= evaluate_expression(expr
->cast_expression
);
1757 * Expressions of restricted types will possibly get
1758 * promoted - check that here
1760 if (is_restricted_type(sym
)) {
1761 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
1765 examine_symbol_type(sym
);
1766 if (is_bitfield_type(sym
)) {
1767 sparse_error(expr
->pos
, "trying to examine bitfield type");
1773 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1775 struct symbol
*type
;
1778 type
= evaluate_type_information(expr
);
1782 size
= type
->bit_size
;
1783 if ((size
< 0) || (size
& 7))
1784 sparse_error(expr
->pos
, "cannot size expression");
1785 expr
->type
= EXPR_VALUE
;
1786 expr
->value
= size
>> 3;
1787 expr
->ctype
= size_t_ctype
;
1788 return size_t_ctype
;
1791 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
1793 struct symbol
*type
;
1796 type
= evaluate_type_information(expr
);
1800 if (type
->type
== SYM_NODE
)
1801 type
= type
->ctype
.base_type
;
1804 switch (type
->type
) {
1808 type
= get_base_type(type
);
1812 sparse_error(expr
->pos
, "expected pointer expression");
1815 size
= type
->bit_size
;
1818 expr
->type
= EXPR_VALUE
;
1819 expr
->value
= size
>> 3;
1820 expr
->ctype
= size_t_ctype
;
1821 return size_t_ctype
;
1824 static struct symbol
*evaluate_alignof(struct expression
*expr
)
1826 struct symbol
*type
;
1828 type
= evaluate_type_information(expr
);
1832 expr
->type
= EXPR_VALUE
;
1833 expr
->value
= type
->ctype
.alignment
;
1834 expr
->ctype
= size_t_ctype
;
1835 return size_t_ctype
;
1838 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1840 struct expression
*expr
;
1841 struct symbol_list
*argument_types
= fn
->arguments
;
1842 struct symbol
*argtype
;
1845 PREPARE_PTR_LIST(argument_types
, argtype
);
1846 FOR_EACH_PTR (head
, expr
) {
1847 struct expression
**p
= THIS_ADDRESS(expr
);
1848 struct symbol
*ctype
, *target
;
1849 ctype
= evaluate_expression(expr
);
1854 ctype
= degenerate(expr
);
1857 if (!target
&& ctype
->bit_size
< bits_in_int
)
1858 target
= &int_ctype
;
1860 static char where
[30];
1861 examine_symbol_type(target
);
1862 sprintf(where
, "argument %d", i
);
1863 compatible_assignment_types(expr
, target
, p
, ctype
, where
, '=');
1867 NEXT_PTR_LIST(argtype
);
1868 } END_FOR_EACH_PTR(expr
);
1869 FINISH_PTR_LIST(argtype
);
1873 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
);
1875 static int evaluate_one_array_initializer(struct symbol
*ctype
, struct expression
**ep
, int current
)
1877 struct expression
*entry
= *ep
;
1878 struct expression
**parent
, *reuse
= NULL
;
1879 unsigned long offset
;
1881 unsigned long from
, to
;
1882 int accept_string
= is_byte_type(ctype
);
1887 if (entry
->type
== EXPR_INDEX
) {
1888 from
= entry
->idx_from
;
1889 to
= entry
->idx_to
+1;
1890 parent
= &entry
->idx_expression
;
1892 entry
= entry
->idx_expression
;
1895 offset
= from
* (ctype
->bit_size
>>3);
1897 if (!reuse
) reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1898 reuse
->type
= EXPR_POS
;
1899 reuse
->ctype
= ctype
;
1900 reuse
->init_offset
= offset
;
1901 reuse
->init_nr
= to
- from
;
1902 reuse
->init_expr
= entry
;
1903 parent
= &reuse
->init_expr
;
1908 if (accept_string
&& entry
->type
== EXPR_STRING
) {
1909 sym
= evaluate_expression(entry
);
1910 to
= from
+ get_expression_value(sym
->array_size
);
1912 evaluate_initializer(ctype
, parent
);
1917 static void evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
)
1919 struct expression
*entry
;
1922 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1923 current
= evaluate_one_array_initializer(ctype
, THIS_ADDRESS(entry
), current
);
1924 } END_FOR_EACH_PTR(entry
);
1927 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1928 static void evaluate_scalar_initializer(struct symbol
*ctype
, struct expression
*expr
)
1930 if (expression_list_size(expr
->expr_list
) != 1) {
1931 sparse_error(expr
->pos
, "unexpected compound initializer");
1934 evaluate_array_initializer(ctype
, expr
);
1938 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
1942 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
1943 if (sym
->ident
== ident
)
1945 } END_FOR_EACH_PTR(sym
);
1949 static int evaluate_one_struct_initializer(struct symbol
*ctype
, struct expression
**ep
, struct symbol
*sym
)
1951 struct expression
*entry
= *ep
;
1952 struct expression
**parent
;
1953 struct expression
*reuse
= NULL
;
1954 unsigned long offset
;
1957 sparse_error(entry
->pos
, "unknown named initializer");
1961 if (entry
->type
== EXPR_IDENTIFIER
) {
1963 entry
= entry
->ident_expression
;
1967 offset
= sym
->offset
;
1970 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1971 reuse
->type
= EXPR_POS
;
1973 reuse
->init_offset
= offset
;
1975 reuse
->init_expr
= entry
;
1976 parent
= &reuse
->init_expr
;
1980 evaluate_initializer(sym
, parent
);
1984 static void evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
)
1986 struct expression
*entry
;
1989 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
1990 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1991 if (entry
->type
== EXPR_IDENTIFIER
) {
1992 struct ident
*ident
= entry
->expr_ident
;
1993 /* We special-case the "already right place" case */
1994 if (!sym
|| sym
->ident
!= ident
) {
1995 RESET_PTR_LIST(sym
);
1999 if (sym
->ident
== ident
)
2005 if (evaluate_one_struct_initializer(ctype
, THIS_ADDRESS(entry
), sym
))
2008 } END_FOR_EACH_PTR(entry
);
2009 FINISH_PTR_LIST(sym
);
2013 * Initializers are kind of like assignments. Except
2014 * they can be a hell of a lot more complex.
2016 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2018 struct expression
*expr
= *ep
;
2021 * Simple non-structure/array initializers are the simple
2022 * case, and look (and parse) largely like assignments.
2024 switch (expr
->type
) {
2026 int is_string
= expr
->type
== EXPR_STRING
;
2027 struct symbol
*rtype
= evaluate_expression(expr
);
2031 * char array[] = "string"
2032 * should _not_ degenerate.
2034 if (!is_string
|| !is_string_type(ctype
))
2035 rtype
= degenerate(expr
);
2036 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer", '=');
2041 case EXPR_INITIALIZER
:
2042 expr
->ctype
= ctype
;
2043 if (ctype
->type
== SYM_NODE
)
2044 ctype
= ctype
->ctype
.base_type
;
2046 switch (ctype
->type
) {
2049 evaluate_array_initializer(get_base_type(ctype
), expr
);
2052 evaluate_struct_or_union_initializer(ctype
, expr
, 0);
2055 evaluate_struct_or_union_initializer(ctype
, expr
, 1);
2058 evaluate_scalar_initializer(ctype
, expr
);
2062 case EXPR_IDENTIFIER
:
2063 if (ctype
->type
== SYM_NODE
)
2064 ctype
= ctype
->ctype
.base_type
;
2065 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2066 sparse_error(expr
->pos
, "expected structure or union for '%s' dereference", show_ident(expr
->expr_ident
));
2070 evaluate_one_struct_initializer(ctype
, ep
,
2071 find_struct_ident(ctype
, expr
->expr_ident
));
2075 if (ctype
->type
== SYM_NODE
)
2076 ctype
= ctype
->ctype
.base_type
;
2077 if (ctype
->type
!= SYM_ARRAY
) {
2078 sparse_error(expr
->pos
, "expected array");
2081 evaluate_one_array_initializer(ctype
->ctype
.base_type
, ep
, 0);
2086 * An EXPR_POS expression has already been evaluated, and we don't
2087 * need to do anything more
2093 static int get_as(struct symbol
*sym
)
2101 mod
= sym
->ctype
.modifiers
;
2102 if (sym
->type
== SYM_NODE
) {
2103 sym
= sym
->ctype
.base_type
;
2104 as
|= sym
->ctype
.as
;
2105 mod
|= sym
->ctype
.modifiers
;
2109 * At least for now, allow casting to a "unsigned long".
2110 * That's how we do things like pointer arithmetic and
2111 * store pointers to registers.
2113 if (sym
== &ulong_ctype
)
2116 if (sym
&& sym
->type
== SYM_PTR
) {
2117 sym
= get_base_type(sym
);
2118 as
|= sym
->ctype
.as
;
2119 mod
|= sym
->ctype
.modifiers
;
2121 if (mod
& MOD_FORCE
)
2126 static void cast_to_as(struct expression
*e
, int as
)
2128 struct expression
*v
= e
->cast_expression
;
2130 if (!Wcast_to_address_space
)
2133 /* cast from constant 0 to pointer is OK */
2134 if (v
->type
== EXPR_VALUE
&& is_int_type(v
->ctype
) && !v
->value
)
2137 warning(e
->pos
, "cast adds address space to expression (<asn:%d>)", as
);
2140 static struct symbol
*evaluate_cast(struct expression
*expr
)
2142 struct expression
*target
= expr
->cast_expression
;
2143 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
2144 struct symbol
*t1
, *t2
;
2145 enum type type1
, type2
;
2151 expr
->ctype
= ctype
;
2152 expr
->cast_type
= ctype
;
2155 * Special case: a cast can be followed by an
2156 * initializer, in which case we need to pass
2157 * the type value down to that initializer rather
2158 * than trying to evaluate it as an expression
2160 * A more complex case is when the initializer is
2161 * dereferenced as part of a post-fix expression.
2162 * We need to produce an expression that can be dereferenced.
2164 if (target
->type
== EXPR_INITIALIZER
) {
2165 struct symbol
*sym
= expr
->cast_type
;
2166 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2168 sym
->initializer
= expr
->cast_expression
;
2169 evaluate_symbol(sym
);
2171 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2174 expr
->type
= EXPR_PREOP
;
2182 evaluate_expression(target
);
2186 if (t1
->type
== SYM_NODE
)
2187 t1
= t1
->ctype
.base_type
;
2188 if (t1
->type
== SYM_ENUM
)
2189 t1
= t1
->ctype
.base_type
;
2192 * You can always throw a value away by casting to
2193 * "void" - that's an implicit "force". Note that
2194 * the same is _not_ true of "void *".
2196 if (t1
== &void_ctype
)
2200 if (type1
== SYM_ARRAY
|| type1
== SYM_UNION
|| type1
== SYM_STRUCT
)
2201 warning(expr
->pos
, "cast to non-scalar");
2205 sparse_error(expr
->pos
, "cast from unknown type");
2208 if (t2
->type
== SYM_NODE
)
2209 t2
= t2
->ctype
.base_type
;
2210 if (t2
->type
== SYM_ENUM
)
2211 t2
= t2
->ctype
.base_type
;
2214 if (type2
== SYM_ARRAY
|| type2
== SYM_UNION
|| type2
== SYM_STRUCT
)
2215 warning(expr
->pos
, "cast from non-scalar");
2217 if (!(ctype
->ctype
.modifiers
& MOD_FORCE
) && t1
!= t2
) {
2218 if (t1
->type
== SYM_RESTRICT
)
2219 warning(expr
->pos
, "cast to restricted type");
2220 if (t2
->type
== SYM_RESTRICT
)
2221 warning(expr
->pos
, "cast from restricted type");
2224 as1
= get_as(ctype
);
2225 as2
= get_as(target
->ctype
);
2226 if (!as1
&& as2
> 0)
2227 warning(expr
->pos
, "cast removes address space of expression");
2228 if (as1
> 0 && as2
> 0 && as1
!= as2
)
2229 warning(expr
->pos
, "cast between address spaces (<asn:%d>-><asn:%d>)", as2
, as1
);
2230 if (as1
> 0 && !as2
)
2231 cast_to_as(expr
, as1
);
2234 * Casts of constant values are special: they
2235 * can be NULL, and thus need to be simplified
2238 if (target
->type
== EXPR_VALUE
)
2239 cast_value(expr
, ctype
, target
, target
->ctype
);
2246 * Evaluate a call expression with a symbol. This
2247 * should expand inline functions, and evaluate
2250 static int evaluate_symbol_call(struct expression
*expr
)
2252 struct expression
*fn
= expr
->fn
;
2253 struct symbol
*ctype
= fn
->ctype
;
2255 if (fn
->type
!= EXPR_PREOP
)
2258 if (ctype
->op
&& ctype
->op
->evaluate
)
2259 return ctype
->op
->evaluate(expr
);
2261 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2263 struct symbol
*curr
= current_fn
;
2264 current_fn
= ctype
->ctype
.base_type
;
2265 examine_fn_arguments(current_fn
);
2267 ret
= inline_function(expr
, ctype
);
2269 /* restore the old function */
2277 static struct symbol
*evaluate_call(struct expression
*expr
)
2280 struct symbol
*ctype
, *sym
;
2281 struct expression
*fn
= expr
->fn
;
2282 struct expression_list
*arglist
= expr
->args
;
2284 if (!evaluate_expression(fn
))
2286 sym
= ctype
= fn
->ctype
;
2287 if (ctype
->type
== SYM_NODE
)
2288 ctype
= ctype
->ctype
.base_type
;
2289 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
2290 ctype
= get_base_type(ctype
);
2291 if (!evaluate_arguments(sym
, ctype
, arglist
))
2293 if (ctype
->type
!= SYM_FN
) {
2294 sparse_error(expr
->pos
, "not a function %s", show_ident(sym
->ident
));
2297 args
= expression_list_size(expr
->args
);
2298 fnargs
= symbol_list_size(ctype
->arguments
);
2300 sparse_error(expr
->pos
, "not enough arguments for function %s", show_ident(sym
->ident
));
2301 if (args
> fnargs
&& !ctype
->variadic
)
2302 sparse_error(expr
->pos
, "too many arguments for function %s", show_ident(sym
->ident
));
2303 if (sym
->type
== SYM_NODE
) {
2304 if (evaluate_symbol_call(expr
))
2307 expr
->ctype
= ctype
->ctype
.base_type
;
2311 struct symbol
*evaluate_expression(struct expression
*expr
)
2318 switch (expr
->type
) {
2321 sparse_error(expr
->pos
, "value expression without a type");
2324 return evaluate_string(expr
);
2326 return evaluate_symbol_expression(expr
);
2328 if (!evaluate_expression(expr
->left
))
2330 if (!evaluate_expression(expr
->right
))
2332 return evaluate_binop(expr
);
2334 return evaluate_logical(expr
);
2336 evaluate_expression(expr
->left
);
2337 if (!evaluate_expression(expr
->right
))
2339 return evaluate_comma(expr
);
2341 if (!evaluate_expression(expr
->left
))
2343 if (!evaluate_expression(expr
->right
))
2345 return evaluate_compare(expr
);
2346 case EXPR_ASSIGNMENT
:
2347 if (!evaluate_expression(expr
->left
))
2349 if (!evaluate_expression(expr
->right
))
2351 return evaluate_assignment(expr
);
2353 if (!evaluate_expression(expr
->unop
))
2355 return evaluate_preop(expr
);
2357 if (!evaluate_expression(expr
->unop
))
2359 return evaluate_postop(expr
);
2361 case EXPR_IMPLIED_CAST
:
2362 return evaluate_cast(expr
);
2364 return evaluate_sizeof(expr
);
2365 case EXPR_PTRSIZEOF
:
2366 return evaluate_ptrsizeof(expr
);
2368 return evaluate_alignof(expr
);
2370 return evaluate_member_dereference(expr
);
2372 return evaluate_call(expr
);
2374 case EXPR_CONDITIONAL
:
2375 return evaluate_conditional_expression(expr
);
2376 case EXPR_STATEMENT
:
2377 expr
->ctype
= evaluate_statement(expr
->statement
);
2381 expr
->ctype
= &ptr_ctype
;
2385 /* Evaluate the type of the symbol .. */
2386 evaluate_symbol(expr
->symbol
);
2387 /* .. but the type of the _expression_ is a "type" */
2388 expr
->ctype
= &type_ctype
;
2391 /* These can not exist as stand-alone expressions */
2392 case EXPR_INITIALIZER
:
2393 case EXPR_IDENTIFIER
:
2396 sparse_error(expr
->pos
, "internal front-end error: initializer in expression");
2399 sparse_error(expr
->pos
, "internal front-end error: SLICE re-evaluated");
2405 static void check_duplicates(struct symbol
*sym
)
2408 struct symbol
*next
= sym
;
2410 while ((next
= next
->same_symbol
) != NULL
) {
2411 const char *typediff
;
2412 evaluate_symbol(next
);
2414 typediff
= type_difference(sym
, next
, 0, 0);
2416 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2417 show_ident(sym
->ident
),
2418 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
2423 unsigned long mod
= sym
->ctype
.modifiers
;
2424 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
2426 if (!(mod
& MOD_TOPLEVEL
))
2430 if (sym
->ident
== &main_ident
)
2432 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
2436 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
2438 struct symbol
*base_type
;
2446 sym
= examine_symbol_type(sym
);
2447 base_type
= get_base_type(sym
);
2451 /* Evaluate the initializers */
2452 if (sym
->initializer
)
2453 evaluate_initializer(sym
, &sym
->initializer
);
2455 /* And finally, evaluate the body of the symbol too */
2456 if (base_type
->type
== SYM_FN
) {
2457 struct symbol
*curr
= current_fn
;
2459 current_fn
= base_type
;
2461 examine_fn_arguments(base_type
);
2462 if (!base_type
->stmt
&& base_type
->inline_stmt
)
2464 if (base_type
->stmt
)
2465 evaluate_statement(base_type
->stmt
);
2473 void evaluate_symbol_list(struct symbol_list
*list
)
2477 FOR_EACH_PTR(list
, sym
) {
2478 evaluate_symbol(sym
);
2479 check_duplicates(sym
);
2480 } END_FOR_EACH_PTR(sym
);
2483 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
2485 struct expression
*expr
= stmt
->expression
;
2486 struct symbol
*ctype
, *fntype
;
2488 evaluate_expression(expr
);
2489 ctype
= degenerate(expr
);
2490 fntype
= current_fn
->ctype
.base_type
;
2491 if (!fntype
|| fntype
== &void_ctype
) {
2492 if (expr
&& ctype
!= &void_ctype
)
2493 sparse_error(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
2498 sparse_error(stmt
->pos
, "return with no return value");
2503 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, ctype
, "return expression", '=');
2507 static void evaluate_if_statement(struct statement
*stmt
)
2509 if (!stmt
->if_conditional
)
2512 evaluate_conditional(stmt
->if_conditional
, 0);
2513 evaluate_statement(stmt
->if_true
);
2514 evaluate_statement(stmt
->if_false
);
2517 static void evaluate_iterator(struct statement
*stmt
)
2519 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
2520 evaluate_conditional(stmt
->iterator_post_condition
,1);
2521 evaluate_statement(stmt
->iterator_pre_statement
);
2522 evaluate_statement(stmt
->iterator_statement
);
2523 evaluate_statement(stmt
->iterator_post_statement
);
2526 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
2528 switch (*constraint
) {
2529 case '=': /* Assignment */
2530 case '+': /* Update */
2533 sparse_error(expr
->pos
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
2537 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
2539 switch (*constraint
) {
2540 case '=': /* Assignment */
2541 case '+': /* Update */
2542 sparse_error(expr
->pos
, "input constraint with assignment (\"%s\")", constraint
);
2546 static void evaluate_asm_statement(struct statement
*stmt
)
2548 struct expression
*expr
;
2551 expr
= stmt
->asm_string
;
2552 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2553 sparse_error(stmt
->pos
, "need constant string for inline asm");
2558 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
2559 struct ident
*ident
;
2562 case 0: /* Identifier */
2564 ident
= (struct ident
*)expr
;
2567 case 1: /* Constraint */
2569 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2570 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
2571 *THIS_ADDRESS(expr
) = NULL
;
2574 verify_output_constraint(expr
, expr
->string
->data
);
2577 case 2: /* Expression */
2579 if (!evaluate_expression(expr
))
2581 if (!lvalue_expression(expr
))
2582 warning(expr
->pos
, "asm output is not an lvalue");
2583 evaluate_assign_to(expr
, expr
->ctype
);
2586 } END_FOR_EACH_PTR(expr
);
2589 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
2590 struct ident
*ident
;
2593 case 0: /* Identifier */
2595 ident
= (struct ident
*)expr
;
2598 case 1: /* Constraint */
2600 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2601 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
2602 *THIS_ADDRESS(expr
) = NULL
;
2605 verify_input_constraint(expr
, expr
->string
->data
);
2608 case 2: /* Expression */
2610 if (!evaluate_expression(expr
))
2614 } END_FOR_EACH_PTR(expr
);
2616 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
2618 sparse_error(stmt
->pos
, "bad asm output");
2621 if (expr
->type
== EXPR_STRING
)
2623 sparse_error(expr
->pos
, "asm clobber is not a string");
2624 } END_FOR_EACH_PTR(expr
);
2627 static void evaluate_case_statement(struct statement
*stmt
)
2629 evaluate_expression(stmt
->case_expression
);
2630 evaluate_expression(stmt
->case_to
);
2631 evaluate_statement(stmt
->case_statement
);
2634 static void check_case_type(struct expression
*switch_expr
,
2635 struct expression
*case_expr
,
2636 struct expression
**enumcase
)
2638 struct symbol
*switch_type
, *case_type
;
2641 switch_type
= switch_expr
->ctype
;
2642 case_type
= evaluate_expression(case_expr
);
2644 if (case_type
&& switch_type
) {
2647 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
2648 else if (is_enum_type(case_type
))
2649 *enumcase
= case_expr
;
2652 /* Both integer types? */
2653 if (compatible_restricted_binop(SPECIAL_EQUAL
, &switch_expr
, &case_expr
))
2655 if (is_int_type(switch_type
) && is_int_type(case_type
))
2659 sparse_error(case_expr
->pos
, "incompatible types for 'case' statement");
2662 static void evaluate_switch_statement(struct statement
*stmt
)
2665 struct expression
*enumcase
= NULL
;
2666 struct expression
**enumcase_holder
= &enumcase
;
2667 struct expression
*sel
= stmt
->switch_expression
;
2669 evaluate_expression(sel
);
2670 evaluate_statement(stmt
->switch_statement
);
2673 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
2674 enumcase_holder
= NULL
; /* Only check cases against switch */
2676 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
2677 struct statement
*case_stmt
= sym
->stmt
;
2678 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
2679 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
2680 } END_FOR_EACH_PTR(sym
);
2683 struct symbol
*evaluate_statement(struct statement
*stmt
)
2688 switch (stmt
->type
) {
2689 case STMT_DECLARATION
: {
2691 FOR_EACH_PTR(stmt
->declaration
, s
) {
2693 } END_FOR_EACH_PTR(s
);
2698 return evaluate_return_expression(stmt
);
2700 case STMT_EXPRESSION
:
2701 if (!evaluate_expression(stmt
->expression
))
2703 return degenerate(stmt
->expression
);
2705 case STMT_COMPOUND
: {
2706 struct statement
*s
;
2707 struct symbol
*type
= NULL
;
2709 /* Evaluate the return symbol in the compound statement */
2710 evaluate_symbol(stmt
->ret
);
2713 * Then, evaluate each statement, making the type of the
2714 * compound statement be the type of the last statement
2717 FOR_EACH_PTR(stmt
->stmts
, s
) {
2718 type
= evaluate_statement(s
);
2719 } END_FOR_EACH_PTR(s
);
2725 evaluate_if_statement(stmt
);
2728 evaluate_iterator(stmt
);
2731 evaluate_switch_statement(stmt
);
2734 evaluate_case_statement(stmt
);
2737 return evaluate_statement(stmt
->label_statement
);
2739 evaluate_expression(stmt
->goto_expression
);
2744 evaluate_asm_statement(stmt
);
2747 evaluate_expression(stmt
->expression
);
2750 evaluate_expression(stmt
->range_expression
);
2751 evaluate_expression(stmt
->range_low
);
2752 evaluate_expression(stmt
->range_high
);