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");
258 info(pos
, " %s versus", show_typename(typea
));
259 info(pos
, " %s", show_typename(typeb
));
264 * This gets called for implicit casts in assignments and
265 * integer promotion. We often want to try to move the
266 * cast down, because the ops involved may have been
267 * implicitly cast up, and we can get rid of the casts
270 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
272 struct expression
*expr
;
274 warn_for_different_enum_types (old
->pos
, old
->ctype
, type
);
276 if (is_same_type(old
, type
))
280 * See if we can simplify the op. Move the cast down.
284 if (old
->ctype
->bit_size
< type
->bit_size
)
286 if (old
->op
== '~') {
288 old
->unop
= cast_to(old
->unop
, type
);
293 case EXPR_IMPLIED_CAST
:
294 warn_for_different_enum_types(old
->pos
, old
->ctype
, type
);
296 if (old
->ctype
->bit_size
>= type
->bit_size
) {
297 struct expression
*orig
= old
->cast_expression
;
298 if (same_cast_type(orig
->ctype
, type
))
300 if (old
->ctype
->bit_offset
== type
->bit_offset
) {
302 old
->cast_type
= type
;
312 expr
= alloc_expression(old
->pos
, EXPR_IMPLIED_CAST
);
314 expr
->cast_type
= type
;
315 expr
->cast_expression
= old
;
319 static int is_type_type(struct symbol
*type
)
321 return (type
->ctype
.modifiers
& MOD_TYPE
) != 0;
324 int is_ptr_type(struct symbol
*type
)
326 if (type
->type
== SYM_NODE
)
327 type
= type
->ctype
.base_type
;
328 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
331 static inline int is_float_type(struct symbol
*type
)
333 if (type
->type
== SYM_NODE
)
334 type
= type
->ctype
.base_type
;
335 return type
->ctype
.base_type
== &fp_type
;
338 static inline int is_byte_type(struct symbol
*type
)
340 return type
->bit_size
== bits_in_char
&& type
->type
!= SYM_BITFIELD
;
353 static inline int classify_type(struct symbol
*type
, struct symbol
**base
)
355 static int type_class
[SYM_BAD
+ 1] = {
356 [SYM_PTR
] = TYPE_PTR
,
358 [SYM_ARRAY
] = TYPE_PTR
| TYPE_COMPOUND
,
359 [SYM_STRUCT
] = TYPE_COMPOUND
,
360 [SYM_UNION
] = TYPE_COMPOUND
,
361 [SYM_BITFIELD
] = TYPE_NUM
| TYPE_BITFIELD
,
362 [SYM_RESTRICT
] = TYPE_NUM
| TYPE_RESTRICT
,
363 [SYM_FOULED
] = TYPE_NUM
| TYPE_RESTRICT
| TYPE_FOULED
,
365 if (type
->type
== SYM_NODE
)
366 type
= type
->ctype
.base_type
;
367 if (type
->type
== SYM_ENUM
)
368 type
= type
->ctype
.base_type
;
370 if (type
->type
== SYM_BASETYPE
) {
371 if (type
->ctype
.base_type
== &int_type
)
373 if (type
->ctype
.base_type
== &fp_type
)
374 return TYPE_NUM
| TYPE_FLOAT
;
376 return type_class
[type
->type
];
379 static inline int is_string_type(struct symbol
*type
)
381 if (type
->type
== SYM_NODE
)
382 type
= type
->ctype
.base_type
;
383 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
386 static struct symbol
*bad_expr_type(struct expression
*expr
)
388 sparse_error(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
389 switch (expr
->type
) {
392 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
393 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
397 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
403 return expr
->ctype
= &bad_ctype
;
406 static int restricted_value(struct expression
*v
, struct symbol
*type
)
408 if (v
->type
!= EXPR_VALUE
)
415 static int restricted_binop(int op
, struct symbol
*type
)
420 case SPECIAL_AND_ASSIGN
:
421 case SPECIAL_OR_ASSIGN
:
422 case SPECIAL_XOR_ASSIGN
:
423 return 1; /* unfoul */
427 return 2; /* keep fouled */
429 case SPECIAL_NOTEQUAL
:
430 return 3; /* warn if fouled */
436 static int restricted_unop(int op
, struct symbol
**type
)
439 if ((*type
)->bit_size
< bits_in_int
)
440 *type
= befoul(*type
);
447 static struct symbol
*restricted_binop_type(int op
,
448 struct expression
*left
,
449 struct expression
*right
,
450 int lclass
, int rclass
,
451 struct symbol
*ltype
,
452 struct symbol
*rtype
)
454 struct symbol
*ctype
= NULL
;
455 if (lclass
& TYPE_RESTRICT
) {
456 if (rclass
& TYPE_RESTRICT
) {
457 if (ltype
== rtype
) {
459 } else if (lclass
& TYPE_FOULED
) {
460 if (ltype
->ctype
.base_type
== rtype
)
462 } else if (rclass
& TYPE_FOULED
) {
463 if (rtype
->ctype
.base_type
== ltype
)
467 if (!restricted_value(right
, ltype
))
470 } else if (!restricted_value(left
, rtype
))
474 switch (restricted_binop(op
, ctype
)) {
476 if ((lclass
^ rclass
) & TYPE_FOULED
)
477 ctype
= ctype
->ctype
.base_type
;
480 if (!(lclass
& rclass
& TYPE_FOULED
))
492 static struct symbol
*usual_conversions(int op
,
493 struct expression
**left
,
494 struct expression
**right
,
495 int lclass
, int rclass
,
496 struct symbol
*ltype
,
497 struct symbol
*rtype
)
499 struct symbol
*ctype
;
501 warn_for_different_enum_types((*right
)->pos
, (*left
)->ctype
, (*right
)->ctype
);
503 if ((lclass
| rclass
) & TYPE_RESTRICT
)
507 if (!(lclass
& TYPE_FLOAT
)) {
508 if (!(rclass
& TYPE_FLOAT
))
509 ctype
= bigger_int_type(ltype
, rtype
);
512 } else if (rclass
& TYPE_FLOAT
) {
513 unsigned long lmod
= ltype
->ctype
.modifiers
;
514 unsigned long rmod
= rtype
->ctype
.modifiers
;
515 if (rmod
& ~lmod
& (MOD_LONG
| MOD_LONGLONG
))
523 *left
= cast_to(*left
, ctype
);
524 *right
= cast_to(*right
, ctype
);
528 ctype
= restricted_binop_type(op
, *left
, *right
,
529 lclass
, rclass
, ltype
, rtype
);
533 if (lclass
& TYPE_RESTRICT
) {
534 warning((*left
)->pos
, "restricted degrades to integer");
535 ltype
= ltype
->ctype
.base_type
;
536 if (is_restricted_type(ltype
)) /* was fouled */
537 ltype
= ltype
->ctype
.base_type
;
539 if (rclass
& TYPE_RESTRICT
) {
540 warning((*right
)->pos
, "restricted degrades to integer");
541 rtype
= rtype
->ctype
.base_type
;
542 if (is_restricted_type(rtype
)) /* was fouled */
543 rtype
= rtype
->ctype
.base_type
;
548 static struct symbol
*evaluate_arith(struct expression
*expr
, int float_ok
)
550 struct symbol
*ltype
, *rtype
;
551 int lclass
= classify_type(expr
->left
->ctype
, <ype
);
552 int rclass
= classify_type(expr
->right
->ctype
, &rtype
);
553 struct symbol
*ctype
;
555 if (!(lclass
& rclass
& TYPE_NUM
))
558 if (!float_ok
&& (lclass
| rclass
) & TYPE_FLOAT
)
561 ctype
= usual_conversions(expr
->op
, &expr
->left
, &expr
->right
,
562 lclass
, rclass
, ltype
, rtype
);
567 return bad_expr_type(expr
);
570 static inline int lvalue_expression(struct expression
*expr
)
572 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
575 static int ptr_object_size(struct symbol
*ptr_type
)
577 if (ptr_type
->type
== SYM_NODE
)
578 ptr_type
= ptr_type
->ctype
.base_type
;
579 if (ptr_type
->type
== SYM_PTR
)
580 ptr_type
= get_base_type(ptr_type
);
581 return ptr_type
->bit_size
;
584 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*ctype
, struct expression
**ip
)
586 struct expression
*i
= *ip
;
587 struct symbol
*ptr_type
= ctype
;
590 if (ptr_type
->type
== SYM_NODE
)
591 ptr_type
= ptr_type
->ctype
.base_type
;
593 if (!is_int_type(i
->ctype
))
594 return bad_expr_type(expr
);
596 examine_symbol_type(ctype
);
598 if (!ctype
->ctype
.base_type
) {
599 sparse_error(expr
->pos
, "missing type information");
603 /* Get the size of whatever the pointer points to */
604 bit_size
= ptr_object_size(ctype
);
606 if (bit_size
> bits_in_char
) {
607 int multiply
= bit_size
>> 3;
608 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
610 if (i
->type
== EXPR_VALUE
) {
611 val
->value
= i
->value
* multiply
;
612 val
->ctype
= size_t_ctype
;
615 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
617 val
->ctype
= size_t_ctype
;
618 val
->value
= bit_size
>> 3;
621 mul
->ctype
= size_t_ctype
;
633 static struct symbol
*evaluate_add(struct expression
*expr
)
635 struct expression
*left
= expr
->left
, *right
= expr
->right
;
636 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
638 if (is_ptr_type(ltype
))
639 return evaluate_ptr_add(expr
, degenerate(left
), &expr
->right
);
641 if (is_ptr_type(rtype
))
642 return evaluate_ptr_add(expr
, degenerate(right
), &expr
->left
);
644 return evaluate_arith(expr
, 1);
647 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
648 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
651 unsigned long mod1
, mod2
, diff
;
652 unsigned long as1
, as2
;
654 struct symbol
*base1
, *base2
;
656 if (target
== source
)
658 if (!target
|| !source
)
659 return "different types";
661 * Peel of per-node information.
662 * FIXME! Check alignment and context too here!
664 mod1
= target
->ctype
.modifiers
;
665 as1
= target
->ctype
.as
;
666 mod2
= source
->ctype
.modifiers
;
667 as2
= source
->ctype
.as
;
668 if (target
->type
== SYM_NODE
) {
669 target
= target
->ctype
.base_type
;
672 if (target
->type
== SYM_PTR
) {
676 mod1
|= target
->ctype
.modifiers
;
677 as1
|= target
->ctype
.as
;
679 if (source
->type
== SYM_NODE
) {
680 source
= source
->ctype
.base_type
;
683 if (source
->type
== SYM_PTR
) {
687 mod2
|= source
->ctype
.modifiers
;
688 as2
|= source
->ctype
.as
;
690 if (target
->type
== SYM_ENUM
) {
691 target
= target
->ctype
.base_type
;
695 if (source
->type
== SYM_ENUM
) {
696 source
= source
->ctype
.base_type
;
701 if (target
== source
)
703 if (!target
|| !source
)
704 return "different types";
706 type1
= target
->type
;
707 base1
= target
->ctype
.base_type
;
709 type2
= source
->type
;
710 base2
= source
->ctype
.base_type
;
713 * Pointers to functions compare as the function itself
715 if (type1
== SYM_PTR
&& base1
) {
716 base1
= examine_symbol_type(base1
);
717 switch (base1
->type
) {
721 base1
= base1
->ctype
.base_type
;
726 if (type2
== SYM_PTR
&& base2
) {
727 base2
= examine_symbol_type(base2
);
728 switch (base2
->type
) {
732 base2
= base2
->ctype
.base_type
;
738 /* Arrays degenerate to pointers for type comparisons */
739 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
740 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
742 if (type1
!= type2
|| type1
== SYM_RESTRICT
)
743 return "different base types";
745 /* Must be same address space to be comparable */
746 if (Waddress_space
&& as1
!= as2
)
747 return "different address spaces";
749 /* Ignore differences in storage types or addressability */
750 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
751 diff
&= (mod1
& ~target_mod_ignore
) | (mod2
& ~source_mod_ignore
);
754 return "different type sizes";
755 if (diff
& ~MOD_SIGNEDNESS
)
756 return "different modifiers";
758 /* Differs in signedness only.. */
761 * Warn if both are explicitly signed ("unsigned" is obvously
762 * always explicit, and since we know one of them has to be
763 * unsigned, we check if the signed one was explicit).
765 if ((mod1
| mod2
) & MOD_EXPLICITLY_SIGNED
)
766 return "different explicit signedness";
769 * "char" matches both "unsigned char" and "signed char",
770 * so if the explicit test didn't trigger, then we should
771 * not warn about a char.
773 if (!(mod1
& MOD_CHAR
))
774 return "different signedness";
778 if (type1
== SYM_FN
) {
780 struct symbol
*arg1
, *arg2
;
781 if (base1
->variadic
!= base2
->variadic
)
782 return "incompatible variadic arguments";
783 PREPARE_PTR_LIST(target
->arguments
, arg1
);
784 PREPARE_PTR_LIST(source
->arguments
, arg2
);
788 diff
= type_difference(arg1
, arg2
, 0, 0);
790 static char argdiff
[80];
791 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
800 FINISH_PTR_LIST(arg2
);
801 FINISH_PTR_LIST(arg1
);
810 static int is_null_ptr(struct expression
*expr
)
812 if (expr
->type
!= EXPR_VALUE
|| expr
->value
)
814 if (!is_ptr_type(expr
->ctype
))
815 warning(expr
->pos
, "Using plain integer as NULL pointer");
819 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
821 /* NULL expression? Just return the type of the "other side" */
830 * Ignore differences in "volatile" and "const"ness when
831 * subtracting pointers
833 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
835 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
**rp
)
837 const char *typediff
;
838 struct symbol
*ctype
;
839 struct symbol
*ltype
, *rtype
;
840 struct expression
*r
= *rp
;
842 ltype
= degenerate(l
);
843 rtype
= degenerate(r
);
846 * If it is an integer subtract: the ptr add case will do the
849 if (!is_ptr_type(rtype
))
850 return evaluate_ptr_add(expr
, degenerate(l
), rp
);
853 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
855 ctype
= common_ptr_type(l
, r
);
857 sparse_error(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
861 examine_symbol_type(ctype
);
863 /* Figure out the base type we point to */
864 if (ctype
->type
== SYM_NODE
)
865 ctype
= ctype
->ctype
.base_type
;
866 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
867 sparse_error(expr
->pos
, "subtraction of functions? Share your drugs");
870 ctype
= get_base_type(ctype
);
872 expr
->ctype
= ssize_t_ctype
;
873 if (ctype
->bit_size
> bits_in_char
) {
874 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
875 struct expression
*div
= expr
;
876 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
877 unsigned long value
= ctype
->bit_size
>> 3;
879 val
->ctype
= size_t_ctype
;
882 if (value
& (value
-1)) {
883 if (Wptr_subtraction_blows
)
884 warning(expr
->pos
, "potentially expensive pointer subtraction");
888 sub
->ctype
= ssize_t_ctype
;
897 return ssize_t_ctype
;
900 static struct symbol
*evaluate_sub(struct expression
*expr
)
902 struct expression
*left
= expr
->left
;
903 struct symbol
*ltype
= left
->ctype
;
905 if (is_ptr_type(ltype
))
906 return evaluate_ptr_sub(expr
, left
, &expr
->right
);
908 return evaluate_arith(expr
, 1);
911 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
913 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
915 struct symbol
*ctype
;
920 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
921 warning(expr
->pos
, "assignment expression in conditional");
923 ctype
= evaluate_expression(expr
);
925 if (is_safe_type(ctype
))
926 warning(expr
->pos
, "testing a 'safe expression'");
932 static struct symbol
*evaluate_logical(struct expression
*expr
)
934 if (!evaluate_conditional(expr
->left
, 0))
936 if (!evaluate_conditional(expr
->right
, 0))
939 expr
->ctype
= &bool_ctype
;
943 static struct symbol
*evaluate_shift(struct expression
*expr
)
945 struct expression
*left
= expr
->left
, *right
= expr
->right
;
946 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
948 if (ltype
->type
== SYM_NODE
)
949 ltype
= ltype
->ctype
.base_type
;
950 if (rtype
->type
== SYM_NODE
)
951 rtype
= rtype
->ctype
.base_type
;
952 if (is_int_type(ltype
) && is_int_type(rtype
)) {
953 struct symbol
*ctype
= integer_promotion(ltype
);
954 expr
->left
= cast_to(expr
->left
, ctype
);
956 ctype
= integer_promotion(rtype
);
957 expr
->right
= cast_to(expr
->right
, ctype
);
960 return bad_expr_type(expr
);
963 static struct symbol
*evaluate_binop(struct expression
*expr
)
966 // addition can take ptr+int, fp and int
968 return evaluate_add(expr
);
970 // subtraction can take ptr-ptr, fp and int
972 return evaluate_sub(expr
);
974 // Arithmetic operations can take fp and int
976 return evaluate_arith(expr
, 1);
978 // shifts do integer promotions, but that's it.
979 case SPECIAL_LEFTSHIFT
: case SPECIAL_RIGHTSHIFT
:
980 return evaluate_shift(expr
);
982 // The rest are integer operations
983 // '%', '&', '^', '|'
985 return evaluate_arith(expr
, 0);
989 static struct symbol
*evaluate_comma(struct expression
*expr
)
991 expr
->ctype
= expr
->right
->ctype
;
995 static int modify_for_unsigned(int op
)
998 op
= SPECIAL_UNSIGNED_LT
;
1000 op
= SPECIAL_UNSIGNED_GT
;
1001 else if (op
== SPECIAL_LTE
)
1002 op
= SPECIAL_UNSIGNED_LTE
;
1003 else if (op
== SPECIAL_GTE
)
1004 op
= SPECIAL_UNSIGNED_GTE
;
1008 static struct symbol
*evaluate_compare(struct expression
*expr
)
1010 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1011 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
1012 struct symbol
*ctype
;
1015 if (is_type_type(ltype
) && is_type_type(rtype
))
1018 if (is_safe_type(ltype
) || is_safe_type(rtype
))
1019 warning(expr
->pos
, "testing a 'safe expression'");
1021 /* Pointer types? */
1022 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
1023 // FIXME! Check the types for compatibility
1024 expr
->op
= modify_for_unsigned(expr
->op
);
1028 ctype
= evaluate_arith(expr
, 1);
1030 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
1031 expr
->op
= modify_for_unsigned(expr
->op
);
1034 expr
->ctype
= &bool_ctype
;
1039 * FIXME!! This should do casts, array degeneration etc..
1041 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
1043 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
1045 if (ltype
->type
== SYM_NODE
)
1046 ltype
= ltype
->ctype
.base_type
;
1048 if (rtype
->type
== SYM_NODE
)
1049 rtype
= rtype
->ctype
.base_type
;
1051 if (ltype
->type
== SYM_PTR
) {
1052 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
1056 if (rtype
->type
== SYM_PTR
) {
1057 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
1064 * NOTE! The degenerate case of "x ? : y", where we don't
1065 * have a true case, this will possibly promote "x" to the
1066 * same type as "y", and thus _change_ the conditional
1067 * test in the expression. But since promotion is "safe"
1068 * for testing, that's ok.
1070 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
1072 struct expression
**true;
1073 struct symbol
*ctype
, *ltype
, *rtype
;
1075 const char * typediff
;
1077 if (!evaluate_conditional(expr
->conditional
, 0))
1079 if (!evaluate_expression(expr
->cond_false
))
1082 ctype
= degenerate(expr
->conditional
);
1083 rtype
= degenerate(expr
->cond_false
);
1085 true = &expr
->conditional
;
1087 if (expr
->cond_true
) {
1088 if (!evaluate_expression(expr
->cond_true
))
1090 ltype
= degenerate(expr
->cond_true
);
1091 true = &expr
->cond_true
;
1094 lclass
= classify_type(ltype
, <ype
);
1095 rclass
= classify_type(rtype
, &rtype
);
1096 if (lclass
& rclass
& TYPE_NUM
) {
1097 ctype
= usual_conversions('?', true, &expr
->cond_false
,
1098 lclass
, rclass
, ltype
, rtype
);
1101 ctype
= compatible_ptr_type(*true, expr
->cond_false
);
1105 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
1108 sparse_error(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
1112 expr
->ctype
= ctype
;
1116 /* FP assignments can not do modulo or bit operations */
1117 static int compatible_float_op(int op
)
1120 op
== SPECIAL_ADD_ASSIGN
||
1121 op
== SPECIAL_SUB_ASSIGN
||
1122 op
== SPECIAL_MUL_ASSIGN
||
1123 op
== SPECIAL_DIV_ASSIGN
;
1126 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1127 struct expression
**rp
, struct symbol
*source
, const char *where
, int op
)
1129 const char *typediff
;
1130 struct symbol
*t
, *s
;
1132 int tclass
= classify_type(target
, &t
);
1133 int sclass
= classify_type(source
, &s
);
1135 if (tclass
& sclass
& TYPE_NUM
) {
1136 if (tclass
& TYPE_FLOAT
&& !compatible_float_op(op
)) {
1137 sparse_error(expr
->pos
, "invalid assignment");
1140 if (tclass
& TYPE_RESTRICT
) {
1141 if (!restricted_binop(op
, target
)) {
1142 sparse_error(expr
->pos
, "bad restricted assignment");
1145 /* allowed assignments unfoul */
1146 if (sclass
& TYPE_FOULED
&& s
->ctype
.base_type
== t
)
1148 if (!restricted_value(*rp
, target
))
1150 } else if (!(sclass
& TYPE_RESTRICT
))
1152 } else if (tclass
& TYPE_PTR
) {
1153 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1154 evaluate_ptr_add(expr
, target
, rp
);
1158 sparse_error(expr
->pos
, "invalid pointer assignment");
1161 } else if (op
!= '=') {
1162 sparse_error(expr
->pos
, "invalid assignment");
1166 /* It's ok if the target is more volatile or const than the source */
1167 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
1171 /* Pointer destination? */
1172 if (tclass
& TYPE_PTR
) {
1173 struct expression
*right
= *rp
;
1176 // NULL pointer is always ok
1177 if (is_null_ptr(right
))
1180 /* "void *" matches anything as long as the address space is ok */
1181 target_as
= t
->ctype
.as
| target
->ctype
.as
;
1182 source_as
= s
->ctype
.as
| source
->ctype
.as
;
1183 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
1184 s
= get_base_type(s
);
1185 t
= get_base_type(t
);
1186 if (s
== &void_ctype
|| t
== &void_ctype
)
1191 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1192 info(expr
->pos
, " expected %s", show_typename(target
));
1193 info(expr
->pos
, " got %s", show_typename(source
));
1194 *rp
= cast_to(*rp
, target
);
1197 *rp
= cast_to(*rp
, target
);
1201 static void mark_assigned(struct expression
*expr
)
1207 switch (expr
->type
) {
1212 if (sym
->type
!= SYM_NODE
)
1214 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1218 mark_assigned(expr
->left
);
1219 mark_assigned(expr
->right
);
1222 mark_assigned(expr
->cast_expression
);
1225 mark_assigned(expr
->base
);
1233 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1235 if (type
->ctype
.modifiers
& MOD_CONST
)
1236 sparse_error(left
->pos
, "assignment to const expression");
1238 /* We know left is an lvalue, so it's a "preop-*" */
1239 mark_assigned(left
->unop
);
1242 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1244 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1245 struct expression
*where
= expr
;
1246 struct symbol
*ltype
, *rtype
;
1248 if (!lvalue_expression(left
)) {
1249 sparse_error(expr
->pos
, "not an lvalue");
1253 ltype
= left
->ctype
;
1255 rtype
= degenerate(right
);
1257 if (!compatible_assignment_types(where
, ltype
, &where
->right
, rtype
, "assignment", expr
->op
))
1260 evaluate_assign_to(left
, ltype
);
1262 expr
->ctype
= ltype
;
1266 static void examine_fn_arguments(struct symbol
*fn
)
1270 FOR_EACH_PTR(fn
->arguments
, s
) {
1271 struct symbol
*arg
= evaluate_symbol(s
);
1272 /* Array/function arguments silently degenerate into pointers */
1278 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1279 if (arg
->type
== SYM_ARRAY
)
1280 ptr
->ctype
= arg
->ctype
;
1282 ptr
->ctype
.base_type
= arg
;
1283 ptr
->ctype
.as
|= s
->ctype
.as
;
1284 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1286 s
->ctype
.base_type
= ptr
;
1288 s
->ctype
.modifiers
= 0;
1291 examine_symbol_type(s
);
1298 } END_FOR_EACH_PTR(s
);
1301 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1303 /* Take the modifiers of the pointer, and apply them to the member */
1304 mod
|= sym
->ctype
.modifiers
;
1305 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1306 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1308 newsym
->ctype
.as
= as
;
1309 newsym
->ctype
.modifiers
= mod
;
1315 #define MOD_PTRINHERIT (MOD_VOLATILE | MOD_CONST | MOD_NODEREF | MOD_STORAGE)
1317 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1319 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1320 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1322 node
->ctype
.base_type
= ptr
;
1323 ptr
->bit_size
= bits_in_pointer
;
1324 ptr
->ctype
.alignment
= pointer_alignment
;
1326 node
->bit_size
= bits_in_pointer
;
1327 node
->ctype
.alignment
= pointer_alignment
;
1330 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1331 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1332 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1334 if (sym
->type
== SYM_NODE
) {
1335 ptr
->ctype
.as
|= sym
->ctype
.as
;
1336 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1337 sym
= sym
->ctype
.base_type
;
1339 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1340 ptr
->ctype
.as
|= sym
->ctype
.as
;
1341 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1342 sym
= sym
->ctype
.base_type
;
1344 ptr
->ctype
.base_type
= sym
;
1349 /* Arrays degenerate into pointers on pointer arithmetic */
1350 static struct symbol
*degenerate(struct expression
*expr
)
1352 struct symbol
*ctype
, *base
;
1356 ctype
= expr
->ctype
;
1359 base
= examine_symbol_type(ctype
);
1360 if (ctype
->type
== SYM_NODE
)
1361 base
= ctype
->ctype
.base_type
;
1363 * Arrays degenerate into pointers to the entries, while
1364 * functions degenerate into pointers to themselves.
1365 * If array was part of non-lvalue compound, we create a copy
1366 * of that compound first and then act as if we were dealing with
1367 * the corresponding field in there.
1369 switch (base
->type
) {
1371 if (expr
->type
== EXPR_SLICE
) {
1372 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1373 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1375 a
->ctype
.base_type
= expr
->base
->ctype
;
1376 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1377 a
->array_size
= expr
->base
->ctype
->array_size
;
1379 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1381 e0
->ctype
= &lazy_ptr_ctype
;
1383 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1386 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1388 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1390 e2
->right
= expr
->base
;
1392 e2
->ctype
= expr
->base
->ctype
;
1394 if (expr
->r_bitpos
) {
1395 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1398 e3
->right
= alloc_const_expression(expr
->pos
,
1399 expr
->r_bitpos
>> 3);
1400 e3
->ctype
= &lazy_ptr_ctype
;
1405 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1408 e4
->ctype
= &lazy_ptr_ctype
;
1411 expr
->type
= EXPR_PREOP
;
1415 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1416 sparse_error(expr
->pos
, "strange non-value function or array");
1419 *expr
= *expr
->unop
;
1420 ctype
= create_pointer(expr
, ctype
, 1);
1421 expr
->ctype
= ctype
;
1428 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1430 struct expression
*op
= expr
->unop
;
1431 struct symbol
*ctype
;
1433 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1434 sparse_error(expr
->pos
, "not addressable");
1440 if (expr
->type
== EXPR_SYMBOL
) {
1441 struct symbol
*sym
= expr
->symbol
;
1442 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1446 * symbol expression evaluation is lazy about the type
1447 * of the sub-expression, so we may have to generate
1448 * the type here if so..
1450 if (expr
->ctype
== &lazy_ptr_ctype
) {
1451 ctype
= create_pointer(expr
, ctype
, 0);
1452 expr
->ctype
= ctype
;
1458 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1460 struct expression
*op
= expr
->unop
;
1461 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1463 /* Simplify: *&(expr) => (expr) */
1464 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1469 /* Dereferencing a node drops all the node information. */
1470 if (ctype
->type
== SYM_NODE
)
1471 ctype
= ctype
->ctype
.base_type
;
1473 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1474 target
= ctype
->ctype
.base_type
;
1476 switch (ctype
->type
) {
1478 sparse_error(expr
->pos
, "cannot dereference this type");
1481 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1482 merge_type(node
, ctype
);
1486 if (!lvalue_expression(op
)) {
1487 sparse_error(op
->pos
, "non-lvalue array??");
1491 /* Do the implied "addressof" on the array */
1495 * When an array is dereferenced, we need to pick
1496 * up the attributes of the original node too..
1498 merge_type(node
, op
->ctype
);
1499 merge_type(node
, ctype
);
1503 node
->bit_size
= target
->bit_size
;
1504 node
->array_size
= target
->array_size
;
1511 * Unary post-ops: x++ and x--
1513 static struct symbol
*evaluate_postop(struct expression
*expr
)
1515 struct expression
*op
= expr
->unop
;
1516 struct symbol
*ctype
= op
->ctype
;
1518 if (!lvalue_expression(expr
->unop
)) {
1519 sparse_error(expr
->pos
, "need lvalue expression for ++/--");
1522 if (is_restricted_type(ctype
) && restricted_unop(expr
->op
, &ctype
)) {
1523 sparse_error(expr
->pos
, "bad operation on restricted");
1525 } else if (is_fouled_type(ctype
) && restricted_unop(expr
->op
, &ctype
)) {
1526 sparse_error(expr
->pos
, "bad operation on restricted");
1530 evaluate_assign_to(op
, ctype
);
1532 expr
->ctype
= ctype
;
1534 if (is_ptr_type(ctype
))
1535 expr
->op_value
= ptr_object_size(ctype
) >> 3;
1540 static struct symbol
*evaluate_sign(struct expression
*expr
)
1542 struct symbol
*ctype
= expr
->unop
->ctype
;
1543 if (is_int_type(ctype
)) {
1544 struct symbol
*rtype
= rtype
= integer_promotion(ctype
);
1545 expr
->unop
= cast_to(expr
->unop
, rtype
);
1547 } else if (is_float_type(ctype
) && expr
->op
!= '~') {
1548 /* no conversions needed */
1549 } else if (is_restricted_type(ctype
) && !restricted_unop(expr
->op
, &ctype
)) {
1550 /* no conversions needed */
1551 } else if (is_fouled_type(ctype
) && !restricted_unop(expr
->op
, &ctype
)) {
1552 /* no conversions needed */
1554 return bad_expr_type(expr
);
1556 if (expr
->op
== '+')
1557 *expr
= *expr
->unop
;
1558 expr
->ctype
= ctype
;
1562 static struct symbol
*evaluate_preop(struct expression
*expr
)
1564 struct symbol
*ctype
= expr
->unop
->ctype
;
1568 *expr
= *expr
->unop
;
1574 return evaluate_sign(expr
);
1577 return evaluate_dereference(expr
);
1580 return evaluate_addressof(expr
);
1582 case SPECIAL_INCREMENT
:
1583 case SPECIAL_DECREMENT
:
1585 * From a type evaluation standpoint the pre-ops are
1586 * the same as the postops
1588 return evaluate_postop(expr
);
1591 if (is_safe_type(ctype
))
1592 warning(expr
->pos
, "testing a 'safe expression'");
1593 if (is_float_type(ctype
)) {
1594 struct expression
*arg
= expr
->unop
;
1595 expr
->type
= EXPR_BINOP
;
1596 expr
->op
= SPECIAL_EQUAL
;
1598 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1599 expr
->right
->ctype
= ctype
;
1600 expr
->right
->fvalue
= 0;
1601 } else if (is_fouled_type(ctype
)) {
1602 warning(expr
->pos
, "restricted degrades to integer");
1604 ctype
= &bool_ctype
;
1610 expr
->ctype
= ctype
;
1614 static struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1616 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1617 struct ptr_list
*list
= head
;
1623 for (i
= 0; i
< list
->nr
; i
++) {
1624 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1626 if (sym
->ident
!= ident
)
1628 *offset
= sym
->offset
;
1631 struct symbol
*ctype
= sym
->ctype
.base_type
;
1635 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1637 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1640 *offset
+= sym
->offset
;
1644 } while ((list
= list
->next
) != head
);
1648 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1650 struct expression
*add
;
1653 * Create a new add-expression
1655 * NOTE! Even if we just add zero, we need a new node
1656 * for the member pointer, since it has a different
1657 * type than the original pointer. We could make that
1658 * be just a cast, but the fact is, a node is a node,
1659 * so we might as well just do the "add zero" here.
1661 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1664 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1665 add
->right
->ctype
= &int_ctype
;
1666 add
->right
->value
= offset
;
1669 * The ctype of the pointer will be lazily evaluated if
1670 * we ever take the address of this member dereference..
1672 add
->ctype
= &lazy_ptr_ctype
;
1676 /* structure/union dereference */
1677 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1680 struct symbol
*ctype
, *member
;
1681 struct expression
*deref
= expr
->deref
, *add
;
1682 struct ident
*ident
= expr
->member
;
1686 if (!evaluate_expression(deref
))
1689 sparse_error(expr
->pos
, "bad member name");
1693 ctype
= deref
->ctype
;
1694 address_space
= ctype
->ctype
.as
;
1695 mod
= ctype
->ctype
.modifiers
;
1696 if (ctype
->type
== SYM_NODE
) {
1697 ctype
= ctype
->ctype
.base_type
;
1698 address_space
|= ctype
->ctype
.as
;
1699 mod
|= ctype
->ctype
.modifiers
;
1701 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1702 sparse_error(expr
->pos
, "expected structure or union");
1705 examine_symbol_type(ctype
);
1707 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1709 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1710 const char *name
= "<unnamed>";
1713 name
= ctype
->ident
->name
;
1714 namelen
= ctype
->ident
->len
;
1716 sparse_error(expr
->pos
, "no member '%s' in %s %.*s",
1717 show_ident(ident
), type
, namelen
, name
);
1722 * The member needs to take on the address space and modifiers of
1723 * the "parent" type.
1725 member
= convert_to_as_mod(member
, address_space
, mod
);
1726 ctype
= get_base_type(member
);
1728 if (!lvalue_expression(deref
)) {
1729 if (deref
->type
!= EXPR_SLICE
) {
1733 expr
->base
= deref
->base
;
1734 expr
->r_bitpos
= deref
->r_bitpos
;
1736 expr
->r_bitpos
+= offset
<< 3;
1737 expr
->type
= EXPR_SLICE
;
1738 expr
->r_nrbits
= member
->bit_size
;
1739 expr
->r_bitpos
+= member
->bit_offset
;
1740 expr
->ctype
= member
;
1744 deref
= deref
->unop
;
1745 expr
->deref
= deref
;
1747 add
= evaluate_offset(deref
, offset
);
1748 expr
->type
= EXPR_PREOP
;
1752 expr
->ctype
= member
;
1756 static int is_promoted(struct expression
*expr
)
1759 switch (expr
->type
) {
1762 case EXPR_CONDITIONAL
:
1786 static struct symbol
*evaluate_cast(struct expression
*);
1788 static struct symbol
*evaluate_type_information(struct expression
*expr
)
1790 struct symbol
*sym
= expr
->cast_type
;
1792 sym
= evaluate_expression(expr
->cast_expression
);
1796 * Expressions of restricted types will possibly get
1797 * promoted - check that here
1799 if (is_restricted_type(sym
)) {
1800 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
1802 } else if (is_fouled_type(sym
)) {
1806 examine_symbol_type(sym
);
1807 if (is_bitfield_type(sym
)) {
1808 sparse_error(expr
->pos
, "trying to examine bitfield type");
1814 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1816 struct symbol
*type
;
1819 type
= evaluate_type_information(expr
);
1823 size
= type
->bit_size
;
1824 if ((size
< 0) || (size
& 7))
1825 sparse_error(expr
->pos
, "cannot size expression");
1826 expr
->type
= EXPR_VALUE
;
1827 expr
->value
= size
>> 3;
1828 expr
->ctype
= size_t_ctype
;
1829 return size_t_ctype
;
1832 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
1834 struct symbol
*type
;
1837 type
= evaluate_type_information(expr
);
1841 if (type
->type
== SYM_NODE
)
1842 type
= type
->ctype
.base_type
;
1845 switch (type
->type
) {
1849 type
= get_base_type(type
);
1853 sparse_error(expr
->pos
, "expected pointer expression");
1856 size
= type
->bit_size
;
1859 expr
->type
= EXPR_VALUE
;
1860 expr
->value
= size
>> 3;
1861 expr
->ctype
= size_t_ctype
;
1862 return size_t_ctype
;
1865 static struct symbol
*evaluate_alignof(struct expression
*expr
)
1867 struct symbol
*type
;
1869 type
= evaluate_type_information(expr
);
1873 expr
->type
= EXPR_VALUE
;
1874 expr
->value
= type
->ctype
.alignment
;
1875 expr
->ctype
= size_t_ctype
;
1876 return size_t_ctype
;
1879 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1881 struct expression
*expr
;
1882 struct symbol_list
*argument_types
= fn
->arguments
;
1883 struct symbol
*argtype
;
1886 PREPARE_PTR_LIST(argument_types
, argtype
);
1887 FOR_EACH_PTR (head
, expr
) {
1888 struct expression
**p
= THIS_ADDRESS(expr
);
1889 struct symbol
*ctype
, *target
;
1890 ctype
= evaluate_expression(expr
);
1895 ctype
= degenerate(expr
);
1898 if (!target
&& ctype
->bit_size
< bits_in_int
)
1899 target
= &int_ctype
;
1901 static char where
[30];
1902 examine_symbol_type(target
);
1903 sprintf(where
, "argument %d", i
);
1904 compatible_assignment_types(expr
, target
, p
, ctype
, where
, '=');
1908 NEXT_PTR_LIST(argtype
);
1909 } END_FOR_EACH_PTR(expr
);
1910 FINISH_PTR_LIST(argtype
);
1914 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
);
1916 static int evaluate_one_array_initializer(struct symbol
*ctype
, struct expression
**ep
, int current
)
1918 struct expression
*entry
= *ep
;
1919 struct expression
**parent
, *reuse
= NULL
;
1920 unsigned long offset
;
1922 unsigned long from
, to
;
1923 int accept_string
= is_byte_type(ctype
);
1928 if (entry
->type
== EXPR_INDEX
) {
1929 from
= entry
->idx_from
;
1930 to
= entry
->idx_to
+1;
1931 parent
= &entry
->idx_expression
;
1933 entry
= entry
->idx_expression
;
1936 offset
= from
* (ctype
->bit_size
>>3);
1938 if (!reuse
) reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1939 reuse
->type
= EXPR_POS
;
1940 reuse
->ctype
= ctype
;
1941 reuse
->init_offset
= offset
;
1942 reuse
->init_nr
= to
- from
;
1943 reuse
->init_expr
= entry
;
1944 parent
= &reuse
->init_expr
;
1949 if (accept_string
&& entry
->type
== EXPR_STRING
) {
1950 sym
= evaluate_expression(entry
);
1951 to
= from
+ get_expression_value(sym
->array_size
);
1953 evaluate_initializer(ctype
, parent
);
1958 static void evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
)
1960 struct expression
*entry
;
1963 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1964 current
= evaluate_one_array_initializer(ctype
, THIS_ADDRESS(entry
), current
);
1965 } END_FOR_EACH_PTR(entry
);
1968 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1969 static void evaluate_scalar_initializer(struct symbol
*ctype
, struct expression
*expr
)
1971 if (expression_list_size(expr
->expr_list
) != 1) {
1972 sparse_error(expr
->pos
, "unexpected compound initializer");
1975 evaluate_array_initializer(ctype
, expr
);
1979 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
1983 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
1984 if (sym
->ident
== ident
)
1986 } END_FOR_EACH_PTR(sym
);
1990 static int evaluate_one_struct_initializer(struct symbol
*ctype
, struct expression
**ep
, struct symbol
*sym
)
1992 struct expression
*entry
= *ep
;
1993 struct expression
**parent
;
1994 struct expression
*reuse
= NULL
;
1995 unsigned long offset
;
1998 sparse_error(entry
->pos
, "unknown named initializer");
2002 if (entry
->type
== EXPR_IDENTIFIER
) {
2004 entry
= entry
->ident_expression
;
2008 offset
= sym
->offset
;
2011 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
2012 reuse
->type
= EXPR_POS
;
2014 reuse
->init_offset
= offset
;
2016 reuse
->init_expr
= entry
;
2017 parent
= &reuse
->init_expr
;
2021 evaluate_initializer(sym
, parent
);
2025 static void evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
)
2027 struct expression
*entry
;
2030 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
2031 FOR_EACH_PTR(expr
->expr_list
, entry
) {
2032 if (entry
->type
== EXPR_IDENTIFIER
) {
2033 struct ident
*ident
= entry
->expr_ident
;
2034 /* We special-case the "already right place" case */
2035 if (!sym
|| sym
->ident
!= ident
) {
2036 RESET_PTR_LIST(sym
);
2040 if (sym
->ident
== ident
)
2046 if (evaluate_one_struct_initializer(ctype
, THIS_ADDRESS(entry
), sym
))
2049 } END_FOR_EACH_PTR(entry
);
2050 FINISH_PTR_LIST(sym
);
2054 * Initializers are kind of like assignments. Except
2055 * they can be a hell of a lot more complex.
2057 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2059 struct expression
*expr
= *ep
;
2062 * Simple non-structure/array initializers are the simple
2063 * case, and look (and parse) largely like assignments.
2065 switch (expr
->type
) {
2067 int is_string
= expr
->type
== EXPR_STRING
;
2068 struct symbol
*rtype
= evaluate_expression(expr
);
2072 * char array[] = "string"
2073 * should _not_ degenerate.
2075 if (!is_string
|| !is_string_type(ctype
))
2076 rtype
= degenerate(expr
);
2077 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer", '=');
2082 case EXPR_INITIALIZER
:
2083 expr
->ctype
= ctype
;
2084 if (ctype
->type
== SYM_NODE
)
2085 ctype
= ctype
->ctype
.base_type
;
2087 switch (ctype
->type
) {
2090 evaluate_array_initializer(get_base_type(ctype
), expr
);
2093 evaluate_struct_or_union_initializer(ctype
, expr
, 0);
2096 evaluate_struct_or_union_initializer(ctype
, expr
, 1);
2099 evaluate_scalar_initializer(ctype
, expr
);
2103 case EXPR_IDENTIFIER
:
2104 if (ctype
->type
== SYM_NODE
)
2105 ctype
= ctype
->ctype
.base_type
;
2106 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2107 sparse_error(expr
->pos
, "expected structure or union for '%s' dereference", show_ident(expr
->expr_ident
));
2111 evaluate_one_struct_initializer(ctype
, ep
,
2112 find_struct_ident(ctype
, expr
->expr_ident
));
2116 if (ctype
->type
== SYM_NODE
)
2117 ctype
= ctype
->ctype
.base_type
;
2118 if (ctype
->type
!= SYM_ARRAY
) {
2119 sparse_error(expr
->pos
, "expected array");
2122 evaluate_one_array_initializer(ctype
->ctype
.base_type
, ep
, 0);
2127 * An EXPR_POS expression has already been evaluated, and we don't
2128 * need to do anything more
2134 static int get_as(struct symbol
*sym
)
2142 mod
= sym
->ctype
.modifiers
;
2143 if (sym
->type
== SYM_NODE
) {
2144 sym
= sym
->ctype
.base_type
;
2145 as
|= sym
->ctype
.as
;
2146 mod
|= sym
->ctype
.modifiers
;
2150 * At least for now, allow casting to a "unsigned long".
2151 * That's how we do things like pointer arithmetic and
2152 * store pointers to registers.
2154 if (sym
== &ulong_ctype
)
2157 if (sym
&& sym
->type
== SYM_PTR
) {
2158 sym
= get_base_type(sym
);
2159 as
|= sym
->ctype
.as
;
2160 mod
|= sym
->ctype
.modifiers
;
2162 if (mod
& MOD_FORCE
)
2167 static void cast_to_as(struct expression
*e
, int as
)
2169 struct expression
*v
= e
->cast_expression
;
2170 struct symbol
*type
= v
->ctype
;
2172 if (!Wcast_to_address_space
)
2175 if (v
->type
!= EXPR_VALUE
|| v
->value
)
2178 /* cast from constant 0 to pointer is OK */
2179 if (is_int_type(type
))
2182 if (type
->type
== SYM_NODE
)
2183 type
= type
->ctype
.base_type
;
2185 if (type
->type
== SYM_PTR
&& type
->ctype
.base_type
== &void_ctype
)
2189 warning(e
->pos
, "cast adds address space to expression (<asn:%d>)", as
);
2192 static struct symbol
*evaluate_cast(struct expression
*expr
)
2194 struct expression
*target
= expr
->cast_expression
;
2195 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
2196 struct symbol
*t1
, *t2
;
2203 expr
->ctype
= ctype
;
2204 expr
->cast_type
= ctype
;
2207 * Special case: a cast can be followed by an
2208 * initializer, in which case we need to pass
2209 * the type value down to that initializer rather
2210 * than trying to evaluate it as an expression
2212 * A more complex case is when the initializer is
2213 * dereferenced as part of a post-fix expression.
2214 * We need to produce an expression that can be dereferenced.
2216 if (target
->type
== EXPR_INITIALIZER
) {
2217 struct symbol
*sym
= expr
->cast_type
;
2218 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2220 sym
->initializer
= expr
->cast_expression
;
2221 evaluate_symbol(sym
);
2223 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2226 expr
->type
= EXPR_PREOP
;
2234 evaluate_expression(target
);
2237 class1
= classify_type(ctype
, &t1
);
2239 * You can always throw a value away by casting to
2240 * "void" - that's an implicit "force". Note that
2241 * the same is _not_ true of "void *".
2243 if (t1
== &void_ctype
)
2246 if (class1
& TYPE_COMPOUND
)
2247 warning(expr
->pos
, "cast to non-scalar");
2251 sparse_error(expr
->pos
, "cast from unknown type");
2254 class2
= classify_type(t2
, &t2
);
2256 if (class2
& TYPE_COMPOUND
)
2257 warning(expr
->pos
, "cast from non-scalar");
2259 /* allowed cast unfouls */
2260 if (class2
& TYPE_FOULED
)
2261 t2
= t2
->ctype
.base_type
;
2263 if (!(ctype
->ctype
.modifiers
& MOD_FORCE
) && t1
!= t2
) {
2264 if (class1
& TYPE_RESTRICT
)
2265 warning(expr
->pos
, "cast to restricted type");
2266 if (class2
& TYPE_RESTRICT
)
2267 warning(expr
->pos
, "cast from restricted type");
2270 as1
= get_as(ctype
);
2271 as2
= get_as(target
->ctype
);
2272 if (!as1
&& as2
> 0)
2273 warning(expr
->pos
, "cast removes address space of expression");
2274 if (as1
> 0 && as2
> 0 && as1
!= as2
)
2275 warning(expr
->pos
, "cast between address spaces (<asn:%d>-><asn:%d>)", as2
, as1
);
2276 if (as1
> 0 && !as2
)
2277 cast_to_as(expr
, as1
);
2280 * Casts of constant values are special: they
2281 * can be NULL, and thus need to be simplified
2284 if (target
->type
== EXPR_VALUE
)
2285 cast_value(expr
, ctype
, target
, target
->ctype
);
2292 * Evaluate a call expression with a symbol. This
2293 * should expand inline functions, and evaluate
2296 static int evaluate_symbol_call(struct expression
*expr
)
2298 struct expression
*fn
= expr
->fn
;
2299 struct symbol
*ctype
= fn
->ctype
;
2301 if (fn
->type
!= EXPR_PREOP
)
2304 if (ctype
->op
&& ctype
->op
->evaluate
)
2305 return ctype
->op
->evaluate(expr
);
2307 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2309 struct symbol
*curr
= current_fn
;
2310 current_fn
= ctype
->ctype
.base_type
;
2311 examine_fn_arguments(current_fn
);
2313 ret
= inline_function(expr
, ctype
);
2315 /* restore the old function */
2323 static struct symbol
*evaluate_call(struct expression
*expr
)
2326 struct symbol
*ctype
, *sym
;
2327 struct expression
*fn
= expr
->fn
;
2328 struct expression_list
*arglist
= expr
->args
;
2330 if (!evaluate_expression(fn
))
2332 sym
= ctype
= fn
->ctype
;
2333 if (ctype
->type
== SYM_NODE
)
2334 ctype
= ctype
->ctype
.base_type
;
2335 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
2336 ctype
= get_base_type(ctype
);
2338 if (sym
->type
== SYM_NODE
&& fn
->type
== EXPR_PREOP
&&
2339 sym
->op
&& sym
->op
->args
) {
2340 if (!sym
->op
->args(expr
))
2343 if (!evaluate_arguments(sym
, ctype
, arglist
))
2345 if (ctype
->type
!= SYM_FN
) {
2346 sparse_error(expr
->pos
, "not a function %s",
2347 show_ident(sym
->ident
));
2350 args
= expression_list_size(expr
->args
);
2351 fnargs
= symbol_list_size(ctype
->arguments
);
2353 sparse_error(expr
->pos
,
2354 "not enough arguments for function %s",
2355 show_ident(sym
->ident
));
2356 if (args
> fnargs
&& !ctype
->variadic
)
2357 sparse_error(expr
->pos
,
2358 "too many arguments for function %s",
2359 show_ident(sym
->ident
));
2361 if (sym
->type
== SYM_NODE
) {
2362 if (evaluate_symbol_call(expr
))
2365 expr
->ctype
= ctype
->ctype
.base_type
;
2369 struct symbol
*evaluate_expression(struct expression
*expr
)
2376 switch (expr
->type
) {
2379 sparse_error(expr
->pos
, "value expression without a type");
2382 return evaluate_string(expr
);
2384 return evaluate_symbol_expression(expr
);
2386 if (!evaluate_expression(expr
->left
))
2388 if (!evaluate_expression(expr
->right
))
2390 return evaluate_binop(expr
);
2392 return evaluate_logical(expr
);
2394 evaluate_expression(expr
->left
);
2395 if (!evaluate_expression(expr
->right
))
2397 return evaluate_comma(expr
);
2399 if (!evaluate_expression(expr
->left
))
2401 if (!evaluate_expression(expr
->right
))
2403 return evaluate_compare(expr
);
2404 case EXPR_ASSIGNMENT
:
2405 if (!evaluate_expression(expr
->left
))
2407 if (!evaluate_expression(expr
->right
))
2409 return evaluate_assignment(expr
);
2411 if (!evaluate_expression(expr
->unop
))
2413 return evaluate_preop(expr
);
2415 if (!evaluate_expression(expr
->unop
))
2417 return evaluate_postop(expr
);
2419 case EXPR_IMPLIED_CAST
:
2420 return evaluate_cast(expr
);
2422 return evaluate_sizeof(expr
);
2423 case EXPR_PTRSIZEOF
:
2424 return evaluate_ptrsizeof(expr
);
2426 return evaluate_alignof(expr
);
2428 return evaluate_member_dereference(expr
);
2430 return evaluate_call(expr
);
2432 case EXPR_CONDITIONAL
:
2433 return evaluate_conditional_expression(expr
);
2434 case EXPR_STATEMENT
:
2435 expr
->ctype
= evaluate_statement(expr
->statement
);
2439 expr
->ctype
= &ptr_ctype
;
2443 /* Evaluate the type of the symbol .. */
2444 evaluate_symbol(expr
->symbol
);
2445 /* .. but the type of the _expression_ is a "type" */
2446 expr
->ctype
= &type_ctype
;
2449 /* These can not exist as stand-alone expressions */
2450 case EXPR_INITIALIZER
:
2451 case EXPR_IDENTIFIER
:
2454 sparse_error(expr
->pos
, "internal front-end error: initializer in expression");
2457 sparse_error(expr
->pos
, "internal front-end error: SLICE re-evaluated");
2463 static void check_duplicates(struct symbol
*sym
)
2466 struct symbol
*next
= sym
;
2468 while ((next
= next
->same_symbol
) != NULL
) {
2469 const char *typediff
;
2470 evaluate_symbol(next
);
2472 typediff
= type_difference(sym
, next
, 0, 0);
2474 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2475 show_ident(sym
->ident
),
2476 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
2481 unsigned long mod
= sym
->ctype
.modifiers
;
2482 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
2484 if (!(mod
& MOD_TOPLEVEL
))
2488 if (sym
->ident
== &main_ident
)
2490 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
2494 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
2496 struct symbol
*base_type
;
2504 sym
= examine_symbol_type(sym
);
2505 base_type
= get_base_type(sym
);
2509 /* Evaluate the initializers */
2510 if (sym
->initializer
)
2511 evaluate_initializer(sym
, &sym
->initializer
);
2513 /* And finally, evaluate the body of the symbol too */
2514 if (base_type
->type
== SYM_FN
) {
2515 struct symbol
*curr
= current_fn
;
2517 current_fn
= base_type
;
2519 examine_fn_arguments(base_type
);
2520 if (!base_type
->stmt
&& base_type
->inline_stmt
)
2522 if (base_type
->stmt
)
2523 evaluate_statement(base_type
->stmt
);
2531 void evaluate_symbol_list(struct symbol_list
*list
)
2535 FOR_EACH_PTR(list
, sym
) {
2536 evaluate_symbol(sym
);
2537 check_duplicates(sym
);
2538 } END_FOR_EACH_PTR(sym
);
2541 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
2543 struct expression
*expr
= stmt
->expression
;
2544 struct symbol
*ctype
, *fntype
;
2546 evaluate_expression(expr
);
2547 ctype
= degenerate(expr
);
2548 fntype
= current_fn
->ctype
.base_type
;
2549 if (!fntype
|| fntype
== &void_ctype
) {
2550 if (expr
&& ctype
!= &void_ctype
)
2551 sparse_error(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
2556 sparse_error(stmt
->pos
, "return with no return value");
2561 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, ctype
, "return expression", '=');
2565 static void evaluate_if_statement(struct statement
*stmt
)
2567 if (!stmt
->if_conditional
)
2570 evaluate_conditional(stmt
->if_conditional
, 0);
2571 evaluate_statement(stmt
->if_true
);
2572 evaluate_statement(stmt
->if_false
);
2575 static void evaluate_iterator(struct statement
*stmt
)
2577 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
2578 evaluate_conditional(stmt
->iterator_post_condition
,1);
2579 evaluate_statement(stmt
->iterator_pre_statement
);
2580 evaluate_statement(stmt
->iterator_statement
);
2581 evaluate_statement(stmt
->iterator_post_statement
);
2584 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
2586 switch (*constraint
) {
2587 case '=': /* Assignment */
2588 case '+': /* Update */
2591 sparse_error(expr
->pos
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
2595 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
2597 switch (*constraint
) {
2598 case '=': /* Assignment */
2599 case '+': /* Update */
2600 sparse_error(expr
->pos
, "input constraint with assignment (\"%s\")", constraint
);
2604 static void evaluate_asm_statement(struct statement
*stmt
)
2606 struct expression
*expr
;
2609 expr
= stmt
->asm_string
;
2610 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2611 sparse_error(stmt
->pos
, "need constant string for inline asm");
2616 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
2617 struct ident
*ident
;
2620 case 0: /* Identifier */
2622 ident
= (struct ident
*)expr
;
2625 case 1: /* Constraint */
2627 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2628 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
2629 *THIS_ADDRESS(expr
) = NULL
;
2632 verify_output_constraint(expr
, expr
->string
->data
);
2635 case 2: /* Expression */
2637 if (!evaluate_expression(expr
))
2639 if (!lvalue_expression(expr
))
2640 warning(expr
->pos
, "asm output is not an lvalue");
2641 evaluate_assign_to(expr
, expr
->ctype
);
2644 } END_FOR_EACH_PTR(expr
);
2647 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
2648 struct ident
*ident
;
2651 case 0: /* Identifier */
2653 ident
= (struct ident
*)expr
;
2656 case 1: /* Constraint */
2658 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2659 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
2660 *THIS_ADDRESS(expr
) = NULL
;
2663 verify_input_constraint(expr
, expr
->string
->data
);
2666 case 2: /* Expression */
2668 if (!evaluate_expression(expr
))
2672 } END_FOR_EACH_PTR(expr
);
2674 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
2676 sparse_error(stmt
->pos
, "bad asm output");
2679 if (expr
->type
== EXPR_STRING
)
2681 sparse_error(expr
->pos
, "asm clobber is not a string");
2682 } END_FOR_EACH_PTR(expr
);
2685 static void evaluate_case_statement(struct statement
*stmt
)
2687 evaluate_expression(stmt
->case_expression
);
2688 evaluate_expression(stmt
->case_to
);
2689 evaluate_statement(stmt
->case_statement
);
2692 static void check_case_type(struct expression
*switch_expr
,
2693 struct expression
*case_expr
,
2694 struct expression
**enumcase
)
2696 struct symbol
*switch_type
, *case_type
;
2702 switch_type
= switch_expr
->ctype
;
2703 case_type
= evaluate_expression(case_expr
);
2705 if (!switch_type
|| !case_type
)
2709 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
2710 else if (is_enum_type(case_type
))
2711 *enumcase
= case_expr
;
2714 sclass
= classify_type(switch_type
, &switch_type
);
2715 cclass
= classify_type(case_type
, &case_type
);
2717 /* both should be arithmetic */
2718 if (!(sclass
& cclass
& TYPE_NUM
))
2721 /* neither should be floating */
2722 if ((sclass
| cclass
) & TYPE_FLOAT
)
2725 /* if neither is restricted, we are OK */
2726 if (!((sclass
| cclass
) & TYPE_RESTRICT
))
2729 if (!restricted_binop_type(SPECIAL_EQUAL
, case_expr
, switch_expr
,
2730 cclass
, sclass
, case_type
, switch_type
))
2731 warning(case_expr
->pos
, "restricted degrades to integer");
2736 sparse_error(case_expr
->pos
, "incompatible types for 'case' statement");
2739 static void evaluate_switch_statement(struct statement
*stmt
)
2742 struct expression
*enumcase
= NULL
;
2743 struct expression
**enumcase_holder
= &enumcase
;
2744 struct expression
*sel
= stmt
->switch_expression
;
2746 evaluate_expression(sel
);
2747 evaluate_statement(stmt
->switch_statement
);
2750 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
2751 enumcase_holder
= NULL
; /* Only check cases against switch */
2753 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
2754 struct statement
*case_stmt
= sym
->stmt
;
2755 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
2756 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
2757 } END_FOR_EACH_PTR(sym
);
2760 struct symbol
*evaluate_statement(struct statement
*stmt
)
2765 switch (stmt
->type
) {
2766 case STMT_DECLARATION
: {
2768 FOR_EACH_PTR(stmt
->declaration
, s
) {
2770 } END_FOR_EACH_PTR(s
);
2775 return evaluate_return_expression(stmt
);
2777 case STMT_EXPRESSION
:
2778 if (!evaluate_expression(stmt
->expression
))
2780 return degenerate(stmt
->expression
);
2782 case STMT_COMPOUND
: {
2783 struct statement
*s
;
2784 struct symbol
*type
= NULL
;
2786 /* Evaluate the return symbol in the compound statement */
2787 evaluate_symbol(stmt
->ret
);
2790 * Then, evaluate each statement, making the type of the
2791 * compound statement be the type of the last statement
2794 FOR_EACH_PTR(stmt
->stmts
, s
) {
2795 type
= evaluate_statement(s
);
2796 } END_FOR_EACH_PTR(s
);
2802 evaluate_if_statement(stmt
);
2805 evaluate_iterator(stmt
);
2808 evaluate_switch_statement(stmt
);
2811 evaluate_case_statement(stmt
);
2814 return evaluate_statement(stmt
->label_statement
);
2816 evaluate_expression(stmt
->goto_expression
);
2821 evaluate_asm_statement(stmt
);
2824 evaluate_expression(stmt
->expression
);
2827 evaluate_expression(stmt
->range_expression
);
2828 evaluate_expression(stmt
->range_low
);
2829 evaluate_expression(stmt
->range_high
);