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
;
350 static inline int classify_type(struct symbol
*type
, struct symbol
**base
)
352 static int type_class
[SYM_BAD
+ 1] = {
353 [SYM_PTR
] = TYPE_PTR
,
355 [SYM_ARRAY
] = TYPE_PTR
| TYPE_COMPOUND
,
356 [SYM_STRUCT
] = TYPE_COMPOUND
,
357 [SYM_UNION
] = TYPE_COMPOUND
,
358 [SYM_BITFIELD
] = TYPE_NUM
| TYPE_BITFIELD
,
359 [SYM_RESTRICT
] = TYPE_NUM
| TYPE_RESTRICT
,
360 [SYM_FOULED
] = TYPE_NUM
| TYPE_RESTRICT
| TYPE_FOULED
,
362 if (type
->type
== SYM_NODE
)
363 type
= type
->ctype
.base_type
;
364 if (type
->type
== SYM_ENUM
)
365 type
= type
->ctype
.base_type
;
367 if (type
->type
== SYM_BASETYPE
) {
368 if (type
->ctype
.base_type
== &int_type
)
370 if (type
->ctype
.base_type
== &fp_type
)
371 return TYPE_NUM
| TYPE_FLOAT
;
373 return type_class
[type
->type
];
376 static inline int is_string_type(struct symbol
*type
)
378 if (type
->type
== SYM_NODE
)
379 type
= type
->ctype
.base_type
;
380 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
383 static struct symbol
*bad_expr_type(struct expression
*expr
)
385 sparse_error(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
386 switch (expr
->type
) {
389 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
390 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
394 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
400 return expr
->ctype
= &bad_ctype
;
403 static int restricted_value(struct expression
*v
, struct symbol
*type
)
405 if (v
->type
!= EXPR_VALUE
)
412 static int restricted_binop(int op
, struct symbol
*type
)
417 case SPECIAL_AND_ASSIGN
:
418 case SPECIAL_OR_ASSIGN
:
419 case SPECIAL_XOR_ASSIGN
:
420 return 1; /* unfoul */
424 return 2; /* keep fouled */
426 case SPECIAL_NOTEQUAL
:
427 return 3; /* warn if fouled */
433 static int restricted_unop(int op
, struct symbol
**type
)
436 if ((*type
)->bit_size
< bits_in_int
)
437 *type
= befoul(*type
);
444 static struct symbol
*restricted_binop_type(int op
,
445 struct expression
*left
,
446 struct expression
*right
,
447 int lclass
, int rclass
,
448 struct symbol
*ltype
,
449 struct symbol
*rtype
)
451 struct symbol
*ctype
= NULL
;
452 if (lclass
& TYPE_RESTRICT
) {
453 if (rclass
& TYPE_RESTRICT
) {
454 if (ltype
== rtype
) {
456 } else if (lclass
& TYPE_FOULED
) {
457 if (ltype
->ctype
.base_type
== rtype
)
459 } else if (rclass
& TYPE_FOULED
) {
460 if (rtype
->ctype
.base_type
== ltype
)
464 if (!restricted_value(right
, ltype
))
467 } else if (!restricted_value(left
, rtype
))
471 switch (restricted_binop(op
, ctype
)) {
473 if ((lclass
^ rclass
) & TYPE_FOULED
)
474 ctype
= ctype
->ctype
.base_type
;
477 if (!(lclass
& rclass
& TYPE_FOULED
))
489 static struct symbol
*usual_conversions(int op
,
490 struct expression
**left
,
491 struct expression
**right
,
492 int lclass
, int rclass
,
493 struct symbol
*ltype
,
494 struct symbol
*rtype
)
496 struct symbol
*ctype
;
498 warn_for_different_enum_types((*right
)->pos
, (*left
)->ctype
, (*right
)->ctype
);
500 if ((lclass
| rclass
) & TYPE_RESTRICT
)
504 if (!(lclass
& TYPE_FLOAT
)) {
505 if (!(rclass
& TYPE_FLOAT
))
506 ctype
= bigger_int_type(ltype
, rtype
);
509 } else if (rclass
& TYPE_FLOAT
) {
510 unsigned long lmod
= ltype
->ctype
.modifiers
;
511 unsigned long rmod
= rtype
->ctype
.modifiers
;
512 if (rmod
& ~lmod
& (MOD_LONG
| MOD_LONGLONG
))
520 *left
= cast_to(*left
, ctype
);
521 *right
= cast_to(*right
, ctype
);
525 ctype
= restricted_binop_type(op
, *left
, *right
,
526 lclass
, rclass
, ltype
, rtype
);
530 if (lclass
& TYPE_RESTRICT
) {
531 warning((*left
)->pos
, "restricted degrades to integer");
532 ltype
= ltype
->ctype
.base_type
;
533 if (is_restricted_type(ltype
)) /* was fouled */
534 ltype
= ltype
->ctype
.base_type
;
536 if (rclass
& TYPE_RESTRICT
) {
537 warning((*right
)->pos
, "restricted degrades to integer");
538 rtype
= rtype
->ctype
.base_type
;
539 if (is_restricted_type(rtype
)) /* was fouled */
540 rtype
= rtype
->ctype
.base_type
;
545 static struct symbol
*evaluate_arith(struct expression
*expr
, int float_ok
)
547 struct symbol
*ltype
, *rtype
;
548 int lclass
= classify_type(expr
->left
->ctype
, <ype
);
549 int rclass
= classify_type(expr
->right
->ctype
, &rtype
);
550 struct symbol
*ctype
;
552 if (!(lclass
& rclass
& TYPE_NUM
))
555 if (!float_ok
&& (lclass
| rclass
) & TYPE_FLOAT
)
558 ctype
= usual_conversions(expr
->op
, &expr
->left
, &expr
->right
,
559 lclass
, rclass
, ltype
, rtype
);
564 return bad_expr_type(expr
);
567 static inline int lvalue_expression(struct expression
*expr
)
569 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
572 static int ptr_object_size(struct symbol
*ptr_type
)
574 if (ptr_type
->type
== SYM_NODE
)
575 ptr_type
= ptr_type
->ctype
.base_type
;
576 if (ptr_type
->type
== SYM_PTR
)
577 ptr_type
= get_base_type(ptr_type
);
578 return ptr_type
->bit_size
;
581 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*ctype
, struct expression
**ip
)
583 struct expression
*i
= *ip
;
584 struct symbol
*ptr_type
= ctype
;
587 if (ptr_type
->type
== SYM_NODE
)
588 ptr_type
= ptr_type
->ctype
.base_type
;
590 if (!is_int_type(i
->ctype
))
591 return bad_expr_type(expr
);
593 examine_symbol_type(ctype
);
595 if (!ctype
->ctype
.base_type
) {
596 sparse_error(expr
->pos
, "missing type information");
600 /* Get the size of whatever the pointer points to */
601 bit_size
= ptr_object_size(ctype
);
603 if (bit_size
> bits_in_char
) {
604 int multiply
= bit_size
>> 3;
605 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
607 if (i
->type
== EXPR_VALUE
) {
608 val
->value
= i
->value
* multiply
;
609 val
->ctype
= size_t_ctype
;
612 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
614 val
->ctype
= size_t_ctype
;
615 val
->value
= bit_size
>> 3;
618 mul
->ctype
= size_t_ctype
;
630 static struct symbol
*evaluate_add(struct expression
*expr
)
632 struct expression
*left
= expr
->left
, *right
= expr
->right
;
633 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
635 if (is_ptr_type(ltype
))
636 return evaluate_ptr_add(expr
, degenerate(left
), &expr
->right
);
638 if (is_ptr_type(rtype
))
639 return evaluate_ptr_add(expr
, degenerate(right
), &expr
->left
);
641 return evaluate_arith(expr
, 1);
644 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
645 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
648 unsigned long mod1
, mod2
, diff
;
649 unsigned long as1
, as2
;
651 struct symbol
*base1
, *base2
;
653 if (target
== source
)
655 if (!target
|| !source
)
656 return "different types";
658 * Peel of per-node information.
659 * FIXME! Check alignment and context too here!
661 mod1
= target
->ctype
.modifiers
;
662 as1
= target
->ctype
.as
;
663 mod2
= source
->ctype
.modifiers
;
664 as2
= source
->ctype
.as
;
665 if (target
->type
== SYM_NODE
) {
666 target
= target
->ctype
.base_type
;
669 if (target
->type
== SYM_PTR
) {
673 mod1
|= target
->ctype
.modifiers
;
674 as1
|= target
->ctype
.as
;
676 if (source
->type
== SYM_NODE
) {
677 source
= source
->ctype
.base_type
;
680 if (source
->type
== SYM_PTR
) {
684 mod2
|= source
->ctype
.modifiers
;
685 as2
|= source
->ctype
.as
;
687 if (target
->type
== SYM_ENUM
) {
688 target
= target
->ctype
.base_type
;
692 if (source
->type
== SYM_ENUM
) {
693 source
= source
->ctype
.base_type
;
698 if (target
== source
)
700 if (!target
|| !source
)
701 return "different types";
703 type1
= target
->type
;
704 base1
= target
->ctype
.base_type
;
706 type2
= source
->type
;
707 base2
= source
->ctype
.base_type
;
710 * Pointers to functions compare as the function itself
712 if (type1
== SYM_PTR
&& base1
) {
713 base1
= examine_symbol_type(base1
);
714 switch (base1
->type
) {
718 base1
= base1
->ctype
.base_type
;
723 if (type2
== SYM_PTR
&& base2
) {
724 base2
= examine_symbol_type(base2
);
725 switch (base2
->type
) {
729 base2
= base2
->ctype
.base_type
;
735 /* Arrays degenerate to pointers for type comparisons */
736 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
737 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
739 if (type1
!= type2
|| type1
== SYM_RESTRICT
)
740 return "different base types";
742 /* Must be same address space to be comparable */
743 if (Waddress_space
&& as1
!= as2
)
744 return "different address spaces";
746 /* Ignore differences in storage types or addressability */
747 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
748 diff
&= (mod1
& ~target_mod_ignore
) | (mod2
& ~source_mod_ignore
);
751 return "different type sizes";
752 if (diff
& ~MOD_SIGNEDNESS
)
753 return "different modifiers";
755 /* Differs in signedness only.. */
758 * Warn if both are explicitly signed ("unsigned" is obvously
759 * always explicit, and since we know one of them has to be
760 * unsigned, we check if the signed one was explicit).
762 if ((mod1
| mod2
) & MOD_EXPLICITLY_SIGNED
)
763 return "different explicit signedness";
766 * "char" matches both "unsigned char" and "signed char",
767 * so if the explicit test didn't trigger, then we should
768 * not warn about a char.
770 if (!(mod1
& MOD_CHAR
))
771 return "different signedness";
775 if (type1
== SYM_FN
) {
777 struct symbol
*arg1
, *arg2
;
778 if (base1
->variadic
!= base2
->variadic
)
779 return "incompatible variadic arguments";
780 PREPARE_PTR_LIST(target
->arguments
, arg1
);
781 PREPARE_PTR_LIST(source
->arguments
, arg2
);
785 diff
= type_difference(arg1
, arg2
, 0, 0);
787 static char argdiff
[80];
788 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
797 FINISH_PTR_LIST(arg2
);
798 FINISH_PTR_LIST(arg1
);
807 static int is_null_ptr(struct expression
*expr
)
809 if (expr
->type
!= EXPR_VALUE
|| expr
->value
)
811 if (!is_ptr_type(expr
->ctype
))
812 warning(expr
->pos
, "Using plain integer as NULL pointer");
816 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
818 /* NULL expression? Just return the type of the "other side" */
827 * Ignore differences in "volatile" and "const"ness when
828 * subtracting pointers
830 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
832 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
**rp
)
834 const char *typediff
;
835 struct symbol
*ctype
;
836 struct symbol
*ltype
, *rtype
;
837 struct expression
*r
= *rp
;
839 ltype
= degenerate(l
);
840 rtype
= degenerate(r
);
843 * If it is an integer subtract: the ptr add case will do the
846 if (!is_ptr_type(rtype
))
847 return evaluate_ptr_add(expr
, degenerate(l
), rp
);
850 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
852 ctype
= common_ptr_type(l
, r
);
854 sparse_error(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
858 examine_symbol_type(ctype
);
860 /* Figure out the base type we point to */
861 if (ctype
->type
== SYM_NODE
)
862 ctype
= ctype
->ctype
.base_type
;
863 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
864 sparse_error(expr
->pos
, "subtraction of functions? Share your drugs");
867 ctype
= get_base_type(ctype
);
869 expr
->ctype
= ssize_t_ctype
;
870 if (ctype
->bit_size
> bits_in_char
) {
871 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
872 struct expression
*div
= expr
;
873 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
874 unsigned long value
= ctype
->bit_size
>> 3;
876 val
->ctype
= size_t_ctype
;
879 if (value
& (value
-1)) {
880 if (Wptr_subtraction_blows
)
881 warning(expr
->pos
, "potentially expensive pointer subtraction");
885 sub
->ctype
= ssize_t_ctype
;
894 return ssize_t_ctype
;
897 static struct symbol
*evaluate_sub(struct expression
*expr
)
899 struct expression
*left
= expr
->left
;
900 struct symbol
*ltype
= left
->ctype
;
902 if (is_ptr_type(ltype
))
903 return evaluate_ptr_sub(expr
, left
, &expr
->right
);
905 return evaluate_arith(expr
, 1);
908 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
910 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
912 struct symbol
*ctype
;
917 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
918 warning(expr
->pos
, "assignment expression in conditional");
920 ctype
= evaluate_expression(expr
);
922 if (is_safe_type(ctype
))
923 warning(expr
->pos
, "testing a 'safe expression'");
929 static struct symbol
*evaluate_logical(struct expression
*expr
)
931 if (!evaluate_conditional(expr
->left
, 0))
933 if (!evaluate_conditional(expr
->right
, 0))
936 expr
->ctype
= &bool_ctype
;
940 static struct symbol
*evaluate_shift(struct expression
*expr
)
942 struct expression
*left
= expr
->left
, *right
= expr
->right
;
943 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
945 if (ltype
->type
== SYM_NODE
)
946 ltype
= ltype
->ctype
.base_type
;
947 if (rtype
->type
== SYM_NODE
)
948 rtype
= rtype
->ctype
.base_type
;
949 if (is_int_type(ltype
) && is_int_type(rtype
)) {
950 struct symbol
*ctype
= integer_promotion(ltype
);
951 expr
->left
= cast_to(expr
->left
, ctype
);
953 ctype
= integer_promotion(rtype
);
954 expr
->right
= cast_to(expr
->right
, ctype
);
957 return bad_expr_type(expr
);
960 static struct symbol
*evaluate_binop(struct expression
*expr
)
963 // addition can take ptr+int, fp and int
965 return evaluate_add(expr
);
967 // subtraction can take ptr-ptr, fp and int
969 return evaluate_sub(expr
);
971 // Arithmetic operations can take fp and int
973 return evaluate_arith(expr
, 1);
975 // shifts do integer promotions, but that's it.
976 case SPECIAL_LEFTSHIFT
: case SPECIAL_RIGHTSHIFT
:
977 return evaluate_shift(expr
);
979 // The rest are integer operations
980 // '%', '&', '^', '|'
982 return evaluate_arith(expr
, 0);
986 static struct symbol
*evaluate_comma(struct expression
*expr
)
988 expr
->ctype
= expr
->right
->ctype
;
992 static int modify_for_unsigned(int op
)
995 op
= SPECIAL_UNSIGNED_LT
;
997 op
= SPECIAL_UNSIGNED_GT
;
998 else if (op
== SPECIAL_LTE
)
999 op
= SPECIAL_UNSIGNED_LTE
;
1000 else if (op
== SPECIAL_GTE
)
1001 op
= SPECIAL_UNSIGNED_GTE
;
1005 static struct symbol
*evaluate_compare(struct expression
*expr
)
1007 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1008 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
1009 struct symbol
*ctype
;
1012 if (is_type_type(ltype
) && is_type_type(rtype
))
1015 if (is_safe_type(ltype
) || is_safe_type(rtype
))
1016 warning(expr
->pos
, "testing a 'safe expression'");
1018 /* Pointer types? */
1019 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
1020 // FIXME! Check the types for compatibility
1021 expr
->op
= modify_for_unsigned(expr
->op
);
1025 ctype
= evaluate_arith(expr
, 1);
1027 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
1028 expr
->op
= modify_for_unsigned(expr
->op
);
1031 expr
->ctype
= &bool_ctype
;
1036 * FIXME!! This should do casts, array degeneration etc..
1038 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
1040 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
1042 if (ltype
->type
== SYM_NODE
)
1043 ltype
= ltype
->ctype
.base_type
;
1045 if (rtype
->type
== SYM_NODE
)
1046 rtype
= rtype
->ctype
.base_type
;
1048 if (ltype
->type
== SYM_PTR
) {
1049 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
1053 if (rtype
->type
== SYM_PTR
) {
1054 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
1061 * NOTE! The degenerate case of "x ? : y", where we don't
1062 * have a true case, this will possibly promote "x" to the
1063 * same type as "y", and thus _change_ the conditional
1064 * test in the expression. But since promotion is "safe"
1065 * for testing, that's ok.
1067 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
1069 struct expression
**true;
1070 struct symbol
*ctype
, *ltype
, *rtype
;
1072 const char * typediff
;
1074 if (!evaluate_conditional(expr
->conditional
, 0))
1076 if (!evaluate_expression(expr
->cond_false
))
1079 ctype
= degenerate(expr
->conditional
);
1080 rtype
= degenerate(expr
->cond_false
);
1082 true = &expr
->conditional
;
1084 if (expr
->cond_true
) {
1085 if (!evaluate_expression(expr
->cond_true
))
1087 ltype
= degenerate(expr
->cond_true
);
1088 true = &expr
->cond_true
;
1091 lclass
= classify_type(ltype
, <ype
);
1092 rclass
= classify_type(rtype
, &rtype
);
1093 if (lclass
& rclass
& TYPE_NUM
) {
1094 ctype
= usual_conversions('?', true, &expr
->cond_false
,
1095 lclass
, rclass
, ltype
, rtype
);
1098 ctype
= compatible_ptr_type(*true, expr
->cond_false
);
1102 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
1105 sparse_error(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
1109 expr
->ctype
= ctype
;
1113 /* FP assignments can not do modulo or bit operations */
1114 static int compatible_float_op(int op
)
1117 op
== SPECIAL_ADD_ASSIGN
||
1118 op
== SPECIAL_SUB_ASSIGN
||
1119 op
== SPECIAL_MUL_ASSIGN
||
1120 op
== SPECIAL_DIV_ASSIGN
;
1123 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1124 struct expression
**rp
, struct symbol
*source
, const char *where
, int op
)
1126 const char *typediff
;
1127 struct symbol
*t
, *s
;
1129 int tclass
= classify_type(target
, &t
);
1130 int sclass
= classify_type(source
, &s
);
1132 if (tclass
& sclass
& TYPE_NUM
) {
1133 if (tclass
& TYPE_FLOAT
&& !compatible_float_op(op
)) {
1134 sparse_error(expr
->pos
, "invalid assignment");
1137 if (tclass
& TYPE_RESTRICT
) {
1138 if (!restricted_binop(op
, target
)) {
1139 sparse_error(expr
->pos
, "bad restricted assignment");
1142 /* allowed assignments unfoul */
1143 if (sclass
& TYPE_FOULED
&& s
->ctype
.base_type
== t
)
1145 if (!restricted_value(*rp
, target
))
1147 } else if (!(sclass
& TYPE_RESTRICT
))
1149 } else if (tclass
& TYPE_PTR
) {
1150 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1151 evaluate_ptr_add(expr
, target
, rp
);
1155 sparse_error(expr
->pos
, "invalid pointer assignment");
1158 } else if (op
!= '=') {
1159 sparse_error(expr
->pos
, "invalid assignment");
1163 /* It's ok if the target is more volatile or const than the source */
1164 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
1168 /* Pointer destination? */
1169 if (tclass
& TYPE_PTR
) {
1170 struct expression
*right
= *rp
;
1173 // NULL pointer is always ok
1174 if (is_null_ptr(right
))
1177 /* "void *" matches anything as long as the address space is ok */
1178 target_as
= t
->ctype
.as
| target
->ctype
.as
;
1179 source_as
= s
->ctype
.as
| source
->ctype
.as
;
1180 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
1181 s
= get_base_type(s
);
1182 t
= get_base_type(t
);
1183 if (s
== &void_ctype
|| t
== &void_ctype
)
1188 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1189 info(expr
->pos
, " expected %s", show_typename(target
));
1190 info(expr
->pos
, " got %s", show_typename(source
));
1191 *rp
= cast_to(*rp
, target
);
1194 *rp
= cast_to(*rp
, target
);
1198 static void mark_assigned(struct expression
*expr
)
1204 switch (expr
->type
) {
1209 if (sym
->type
!= SYM_NODE
)
1211 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1215 mark_assigned(expr
->left
);
1216 mark_assigned(expr
->right
);
1219 mark_assigned(expr
->cast_expression
);
1222 mark_assigned(expr
->base
);
1230 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1232 if (type
->ctype
.modifiers
& MOD_CONST
)
1233 sparse_error(left
->pos
, "assignment to const expression");
1235 /* We know left is an lvalue, so it's a "preop-*" */
1236 mark_assigned(left
->unop
);
1239 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1241 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1242 struct expression
*where
= expr
;
1243 struct symbol
*ltype
, *rtype
;
1245 if (!lvalue_expression(left
)) {
1246 sparse_error(expr
->pos
, "not an lvalue");
1250 ltype
= left
->ctype
;
1252 rtype
= degenerate(right
);
1254 if (!compatible_assignment_types(where
, ltype
, &where
->right
, rtype
, "assignment", expr
->op
))
1257 evaluate_assign_to(left
, ltype
);
1259 expr
->ctype
= ltype
;
1263 static void examine_fn_arguments(struct symbol
*fn
)
1267 FOR_EACH_PTR(fn
->arguments
, s
) {
1268 struct symbol
*arg
= evaluate_symbol(s
);
1269 /* Array/function arguments silently degenerate into pointers */
1275 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1276 if (arg
->type
== SYM_ARRAY
)
1277 ptr
->ctype
= arg
->ctype
;
1279 ptr
->ctype
.base_type
= arg
;
1280 ptr
->ctype
.as
|= s
->ctype
.as
;
1281 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1283 s
->ctype
.base_type
= ptr
;
1285 s
->ctype
.modifiers
= 0;
1288 examine_symbol_type(s
);
1295 } END_FOR_EACH_PTR(s
);
1298 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1300 /* Take the modifiers of the pointer, and apply them to the member */
1301 mod
|= sym
->ctype
.modifiers
;
1302 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1303 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1305 newsym
->ctype
.as
= as
;
1306 newsym
->ctype
.modifiers
= mod
;
1312 #define MOD_PTRINHERIT (MOD_VOLATILE | MOD_CONST | MOD_NODEREF | MOD_STORAGE)
1314 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1316 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1317 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1319 node
->ctype
.base_type
= ptr
;
1320 ptr
->bit_size
= bits_in_pointer
;
1321 ptr
->ctype
.alignment
= pointer_alignment
;
1323 node
->bit_size
= bits_in_pointer
;
1324 node
->ctype
.alignment
= pointer_alignment
;
1327 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1328 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1329 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1331 if (sym
->type
== SYM_NODE
) {
1332 ptr
->ctype
.as
|= sym
->ctype
.as
;
1333 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1334 sym
= sym
->ctype
.base_type
;
1336 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1337 ptr
->ctype
.as
|= sym
->ctype
.as
;
1338 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1339 sym
= sym
->ctype
.base_type
;
1341 ptr
->ctype
.base_type
= sym
;
1346 /* Arrays degenerate into pointers on pointer arithmetic */
1347 static struct symbol
*degenerate(struct expression
*expr
)
1349 struct symbol
*ctype
, *base
;
1353 ctype
= expr
->ctype
;
1356 base
= examine_symbol_type(ctype
);
1357 if (ctype
->type
== SYM_NODE
)
1358 base
= ctype
->ctype
.base_type
;
1360 * Arrays degenerate into pointers to the entries, while
1361 * functions degenerate into pointers to themselves.
1362 * If array was part of non-lvalue compound, we create a copy
1363 * of that compound first and then act as if we were dealing with
1364 * the corresponding field in there.
1366 switch (base
->type
) {
1368 if (expr
->type
== EXPR_SLICE
) {
1369 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1370 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1372 a
->ctype
.base_type
= expr
->base
->ctype
;
1373 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1374 a
->array_size
= expr
->base
->ctype
->array_size
;
1376 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1378 e0
->ctype
= &lazy_ptr_ctype
;
1380 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1383 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1385 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1387 e2
->right
= expr
->base
;
1389 e2
->ctype
= expr
->base
->ctype
;
1391 if (expr
->r_bitpos
) {
1392 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1395 e3
->right
= alloc_const_expression(expr
->pos
,
1396 expr
->r_bitpos
>> 3);
1397 e3
->ctype
= &lazy_ptr_ctype
;
1402 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1405 e4
->ctype
= &lazy_ptr_ctype
;
1408 expr
->type
= EXPR_PREOP
;
1412 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1413 sparse_error(expr
->pos
, "strange non-value function or array");
1416 *expr
= *expr
->unop
;
1417 ctype
= create_pointer(expr
, ctype
, 1);
1418 expr
->ctype
= ctype
;
1425 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1427 struct expression
*op
= expr
->unop
;
1428 struct symbol
*ctype
;
1430 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1431 sparse_error(expr
->pos
, "not addressable");
1437 if (expr
->type
== EXPR_SYMBOL
) {
1438 struct symbol
*sym
= expr
->symbol
;
1439 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1443 * symbol expression evaluation is lazy about the type
1444 * of the sub-expression, so we may have to generate
1445 * the type here if so..
1447 if (expr
->ctype
== &lazy_ptr_ctype
) {
1448 ctype
= create_pointer(expr
, ctype
, 0);
1449 expr
->ctype
= ctype
;
1455 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1457 struct expression
*op
= expr
->unop
;
1458 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1460 /* Simplify: *&(expr) => (expr) */
1461 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1466 /* Dereferencing a node drops all the node information. */
1467 if (ctype
->type
== SYM_NODE
)
1468 ctype
= ctype
->ctype
.base_type
;
1470 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1471 target
= ctype
->ctype
.base_type
;
1473 switch (ctype
->type
) {
1475 sparse_error(expr
->pos
, "cannot derefence this type");
1478 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1479 merge_type(node
, ctype
);
1483 if (!lvalue_expression(op
)) {
1484 sparse_error(op
->pos
, "non-lvalue array??");
1488 /* Do the implied "addressof" on the array */
1492 * When an array is dereferenced, we need to pick
1493 * up the attributes of the original node too..
1495 merge_type(node
, op
->ctype
);
1496 merge_type(node
, ctype
);
1500 node
->bit_size
= target
->bit_size
;
1501 node
->array_size
= target
->array_size
;
1508 * Unary post-ops: x++ and x--
1510 static struct symbol
*evaluate_postop(struct expression
*expr
)
1512 struct expression
*op
= expr
->unop
;
1513 struct symbol
*ctype
= op
->ctype
;
1515 if (!lvalue_expression(expr
->unop
)) {
1516 sparse_error(expr
->pos
, "need lvalue expression for ++/--");
1519 if (is_restricted_type(ctype
) && restricted_unop(expr
->op
, &ctype
)) {
1520 sparse_error(expr
->pos
, "bad operation on restricted");
1522 } else if (is_fouled_type(ctype
) && restricted_unop(expr
->op
, &ctype
)) {
1523 sparse_error(expr
->pos
, "bad operation on restricted");
1527 evaluate_assign_to(op
, ctype
);
1529 expr
->ctype
= ctype
;
1531 if (is_ptr_type(ctype
))
1532 expr
->op_value
= ptr_object_size(ctype
) >> 3;
1537 static struct symbol
*evaluate_sign(struct expression
*expr
)
1539 struct symbol
*ctype
= expr
->unop
->ctype
;
1540 if (is_int_type(ctype
)) {
1541 struct symbol
*rtype
= rtype
= integer_promotion(ctype
);
1542 expr
->unop
= cast_to(expr
->unop
, rtype
);
1544 } else if (is_float_type(ctype
) && expr
->op
!= '~') {
1545 /* no conversions needed */
1546 } else if (is_restricted_type(ctype
) && !restricted_unop(expr
->op
, &ctype
)) {
1547 /* no conversions needed */
1548 } else if (is_fouled_type(ctype
) && !restricted_unop(expr
->op
, &ctype
)) {
1549 /* no conversions needed */
1551 return bad_expr_type(expr
);
1553 if (expr
->op
== '+')
1554 *expr
= *expr
->unop
;
1555 expr
->ctype
= ctype
;
1559 static struct symbol
*evaluate_preop(struct expression
*expr
)
1561 struct symbol
*ctype
= expr
->unop
->ctype
;
1565 *expr
= *expr
->unop
;
1571 return evaluate_sign(expr
);
1574 return evaluate_dereference(expr
);
1577 return evaluate_addressof(expr
);
1579 case SPECIAL_INCREMENT
:
1580 case SPECIAL_DECREMENT
:
1582 * From a type evaluation standpoint the pre-ops are
1583 * the same as the postops
1585 return evaluate_postop(expr
);
1588 if (is_safe_type(ctype
))
1589 warning(expr
->pos
, "testing a 'safe expression'");
1590 if (is_float_type(ctype
)) {
1591 struct expression
*arg
= expr
->unop
;
1592 expr
->type
= EXPR_BINOP
;
1593 expr
->op
= SPECIAL_EQUAL
;
1595 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1596 expr
->right
->ctype
= ctype
;
1597 expr
->right
->fvalue
= 0;
1598 } else if (is_fouled_type(ctype
)) {
1599 warning(expr
->pos
, "restricted degrades to integer");
1601 ctype
= &bool_ctype
;
1607 expr
->ctype
= ctype
;
1611 static struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1613 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1614 struct ptr_list
*list
= head
;
1620 for (i
= 0; i
< list
->nr
; i
++) {
1621 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1623 if (sym
->ident
!= ident
)
1625 *offset
= sym
->offset
;
1628 struct symbol
*ctype
= sym
->ctype
.base_type
;
1632 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1634 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1637 *offset
+= sym
->offset
;
1641 } while ((list
= list
->next
) != head
);
1645 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1647 struct expression
*add
;
1650 * Create a new add-expression
1652 * NOTE! Even if we just add zero, we need a new node
1653 * for the member pointer, since it has a different
1654 * type than the original pointer. We could make that
1655 * be just a cast, but the fact is, a node is a node,
1656 * so we might as well just do the "add zero" here.
1658 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1661 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1662 add
->right
->ctype
= &int_ctype
;
1663 add
->right
->value
= offset
;
1666 * The ctype of the pointer will be lazily evaluated if
1667 * we ever take the address of this member dereference..
1669 add
->ctype
= &lazy_ptr_ctype
;
1673 /* structure/union dereference */
1674 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1677 struct symbol
*ctype
, *member
;
1678 struct expression
*deref
= expr
->deref
, *add
;
1679 struct ident
*ident
= expr
->member
;
1683 if (!evaluate_expression(deref
))
1686 sparse_error(expr
->pos
, "bad member name");
1690 ctype
= deref
->ctype
;
1691 address_space
= ctype
->ctype
.as
;
1692 mod
= ctype
->ctype
.modifiers
;
1693 if (ctype
->type
== SYM_NODE
) {
1694 ctype
= ctype
->ctype
.base_type
;
1695 address_space
|= ctype
->ctype
.as
;
1696 mod
|= ctype
->ctype
.modifiers
;
1698 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1699 sparse_error(expr
->pos
, "expected structure or union");
1702 examine_symbol_type(ctype
);
1704 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1706 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1707 const char *name
= "<unnamed>";
1710 name
= ctype
->ident
->name
;
1711 namelen
= ctype
->ident
->len
;
1713 sparse_error(expr
->pos
, "no member '%s' in %s %.*s",
1714 show_ident(ident
), type
, namelen
, name
);
1719 * The member needs to take on the address space and modifiers of
1720 * the "parent" type.
1722 member
= convert_to_as_mod(member
, address_space
, mod
);
1723 ctype
= get_base_type(member
);
1725 if (!lvalue_expression(deref
)) {
1726 if (deref
->type
!= EXPR_SLICE
) {
1730 expr
->base
= deref
->base
;
1731 expr
->r_bitpos
= deref
->r_bitpos
;
1733 expr
->r_bitpos
+= offset
<< 3;
1734 expr
->type
= EXPR_SLICE
;
1735 expr
->r_nrbits
= member
->bit_size
;
1736 expr
->r_bitpos
+= member
->bit_offset
;
1737 expr
->ctype
= member
;
1741 deref
= deref
->unop
;
1742 expr
->deref
= deref
;
1744 add
= evaluate_offset(deref
, offset
);
1745 expr
->type
= EXPR_PREOP
;
1749 expr
->ctype
= member
;
1753 static int is_promoted(struct expression
*expr
)
1756 switch (expr
->type
) {
1759 case EXPR_CONDITIONAL
:
1783 static struct symbol
*evaluate_cast(struct expression
*);
1785 static struct symbol
*evaluate_type_information(struct expression
*expr
)
1787 struct symbol
*sym
= expr
->cast_type
;
1789 sym
= evaluate_expression(expr
->cast_expression
);
1793 * Expressions of restricted types will possibly get
1794 * promoted - check that here
1796 if (is_restricted_type(sym
)) {
1797 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
1799 } else if (is_fouled_type(sym
)) {
1803 examine_symbol_type(sym
);
1804 if (is_bitfield_type(sym
)) {
1805 sparse_error(expr
->pos
, "trying to examine bitfield type");
1811 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1813 struct symbol
*type
;
1816 type
= evaluate_type_information(expr
);
1820 size
= type
->bit_size
;
1821 if ((size
< 0) || (size
& 7))
1822 sparse_error(expr
->pos
, "cannot size expression");
1823 expr
->type
= EXPR_VALUE
;
1824 expr
->value
= size
>> 3;
1825 expr
->ctype
= size_t_ctype
;
1826 return size_t_ctype
;
1829 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
1831 struct symbol
*type
;
1834 type
= evaluate_type_information(expr
);
1838 if (type
->type
== SYM_NODE
)
1839 type
= type
->ctype
.base_type
;
1842 switch (type
->type
) {
1846 type
= get_base_type(type
);
1850 sparse_error(expr
->pos
, "expected pointer expression");
1853 size
= type
->bit_size
;
1856 expr
->type
= EXPR_VALUE
;
1857 expr
->value
= size
>> 3;
1858 expr
->ctype
= size_t_ctype
;
1859 return size_t_ctype
;
1862 static struct symbol
*evaluate_alignof(struct expression
*expr
)
1864 struct symbol
*type
;
1866 type
= evaluate_type_information(expr
);
1870 expr
->type
= EXPR_VALUE
;
1871 expr
->value
= type
->ctype
.alignment
;
1872 expr
->ctype
= size_t_ctype
;
1873 return size_t_ctype
;
1876 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1878 struct expression
*expr
;
1879 struct symbol_list
*argument_types
= fn
->arguments
;
1880 struct symbol
*argtype
;
1883 PREPARE_PTR_LIST(argument_types
, argtype
);
1884 FOR_EACH_PTR (head
, expr
) {
1885 struct expression
**p
= THIS_ADDRESS(expr
);
1886 struct symbol
*ctype
, *target
;
1887 ctype
= evaluate_expression(expr
);
1892 ctype
= degenerate(expr
);
1895 if (!target
&& ctype
->bit_size
< bits_in_int
)
1896 target
= &int_ctype
;
1898 static char where
[30];
1899 examine_symbol_type(target
);
1900 sprintf(where
, "argument %d", i
);
1901 compatible_assignment_types(expr
, target
, p
, ctype
, where
, '=');
1905 NEXT_PTR_LIST(argtype
);
1906 } END_FOR_EACH_PTR(expr
);
1907 FINISH_PTR_LIST(argtype
);
1911 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
);
1913 static int evaluate_one_array_initializer(struct symbol
*ctype
, struct expression
**ep
, int current
)
1915 struct expression
*entry
= *ep
;
1916 struct expression
**parent
, *reuse
= NULL
;
1917 unsigned long offset
;
1919 unsigned long from
, to
;
1920 int accept_string
= is_byte_type(ctype
);
1925 if (entry
->type
== EXPR_INDEX
) {
1926 from
= entry
->idx_from
;
1927 to
= entry
->idx_to
+1;
1928 parent
= &entry
->idx_expression
;
1930 entry
= entry
->idx_expression
;
1933 offset
= from
* (ctype
->bit_size
>>3);
1935 if (!reuse
) reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1936 reuse
->type
= EXPR_POS
;
1937 reuse
->ctype
= ctype
;
1938 reuse
->init_offset
= offset
;
1939 reuse
->init_nr
= to
- from
;
1940 reuse
->init_expr
= entry
;
1941 parent
= &reuse
->init_expr
;
1946 if (accept_string
&& entry
->type
== EXPR_STRING
) {
1947 sym
= evaluate_expression(entry
);
1948 to
= from
+ get_expression_value(sym
->array_size
);
1950 evaluate_initializer(ctype
, parent
);
1955 static void evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
)
1957 struct expression
*entry
;
1960 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1961 current
= evaluate_one_array_initializer(ctype
, THIS_ADDRESS(entry
), current
);
1962 } END_FOR_EACH_PTR(entry
);
1965 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1966 static void evaluate_scalar_initializer(struct symbol
*ctype
, struct expression
*expr
)
1968 if (expression_list_size(expr
->expr_list
) != 1) {
1969 sparse_error(expr
->pos
, "unexpected compound initializer");
1972 evaluate_array_initializer(ctype
, expr
);
1976 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
1980 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
1981 if (sym
->ident
== ident
)
1983 } END_FOR_EACH_PTR(sym
);
1987 static int evaluate_one_struct_initializer(struct symbol
*ctype
, struct expression
**ep
, struct symbol
*sym
)
1989 struct expression
*entry
= *ep
;
1990 struct expression
**parent
;
1991 struct expression
*reuse
= NULL
;
1992 unsigned long offset
;
1995 sparse_error(entry
->pos
, "unknown named initializer");
1999 if (entry
->type
== EXPR_IDENTIFIER
) {
2001 entry
= entry
->ident_expression
;
2005 offset
= sym
->offset
;
2008 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
2009 reuse
->type
= EXPR_POS
;
2011 reuse
->init_offset
= offset
;
2013 reuse
->init_expr
= entry
;
2014 parent
= &reuse
->init_expr
;
2018 evaluate_initializer(sym
, parent
);
2022 static void evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
)
2024 struct expression
*entry
;
2027 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
2028 FOR_EACH_PTR(expr
->expr_list
, entry
) {
2029 if (entry
->type
== EXPR_IDENTIFIER
) {
2030 struct ident
*ident
= entry
->expr_ident
;
2031 /* We special-case the "already right place" case */
2032 if (!sym
|| sym
->ident
!= ident
) {
2033 RESET_PTR_LIST(sym
);
2037 if (sym
->ident
== ident
)
2043 if (evaluate_one_struct_initializer(ctype
, THIS_ADDRESS(entry
), sym
))
2046 } END_FOR_EACH_PTR(entry
);
2047 FINISH_PTR_LIST(sym
);
2051 * Initializers are kind of like assignments. Except
2052 * they can be a hell of a lot more complex.
2054 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2056 struct expression
*expr
= *ep
;
2059 * Simple non-structure/array initializers are the simple
2060 * case, and look (and parse) largely like assignments.
2062 switch (expr
->type
) {
2064 int is_string
= expr
->type
== EXPR_STRING
;
2065 struct symbol
*rtype
= evaluate_expression(expr
);
2069 * char array[] = "string"
2070 * should _not_ degenerate.
2072 if (!is_string
|| !is_string_type(ctype
))
2073 rtype
= degenerate(expr
);
2074 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer", '=');
2079 case EXPR_INITIALIZER
:
2080 expr
->ctype
= ctype
;
2081 if (ctype
->type
== SYM_NODE
)
2082 ctype
= ctype
->ctype
.base_type
;
2084 switch (ctype
->type
) {
2087 evaluate_array_initializer(get_base_type(ctype
), expr
);
2090 evaluate_struct_or_union_initializer(ctype
, expr
, 0);
2093 evaluate_struct_or_union_initializer(ctype
, expr
, 1);
2096 evaluate_scalar_initializer(ctype
, expr
);
2100 case EXPR_IDENTIFIER
:
2101 if (ctype
->type
== SYM_NODE
)
2102 ctype
= ctype
->ctype
.base_type
;
2103 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2104 sparse_error(expr
->pos
, "expected structure or union for '%s' dereference", show_ident(expr
->expr_ident
));
2108 evaluate_one_struct_initializer(ctype
, ep
,
2109 find_struct_ident(ctype
, expr
->expr_ident
));
2113 if (ctype
->type
== SYM_NODE
)
2114 ctype
= ctype
->ctype
.base_type
;
2115 if (ctype
->type
!= SYM_ARRAY
) {
2116 sparse_error(expr
->pos
, "expected array");
2119 evaluate_one_array_initializer(ctype
->ctype
.base_type
, ep
, 0);
2124 * An EXPR_POS expression has already been evaluated, and we don't
2125 * need to do anything more
2131 static int get_as(struct symbol
*sym
)
2139 mod
= sym
->ctype
.modifiers
;
2140 if (sym
->type
== SYM_NODE
) {
2141 sym
= sym
->ctype
.base_type
;
2142 as
|= sym
->ctype
.as
;
2143 mod
|= sym
->ctype
.modifiers
;
2147 * At least for now, allow casting to a "unsigned long".
2148 * That's how we do things like pointer arithmetic and
2149 * store pointers to registers.
2151 if (sym
== &ulong_ctype
)
2154 if (sym
&& sym
->type
== SYM_PTR
) {
2155 sym
= get_base_type(sym
);
2156 as
|= sym
->ctype
.as
;
2157 mod
|= sym
->ctype
.modifiers
;
2159 if (mod
& MOD_FORCE
)
2164 static void cast_to_as(struct expression
*e
, int as
)
2166 struct expression
*v
= e
->cast_expression
;
2167 struct symbol
*type
= v
->ctype
;
2169 if (!Wcast_to_address_space
)
2172 if (v
->type
!= EXPR_VALUE
|| v
->value
)
2175 /* cast from constant 0 to pointer is OK */
2176 if (is_int_type(type
))
2179 if (type
->type
== SYM_NODE
)
2180 type
= type
->ctype
.base_type
;
2182 if (type
->type
== SYM_PTR
&& type
->ctype
.base_type
== &void_ctype
)
2186 warning(e
->pos
, "cast adds address space to expression (<asn:%d>)", as
);
2189 static struct symbol
*evaluate_cast(struct expression
*expr
)
2191 struct expression
*target
= expr
->cast_expression
;
2192 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
2193 struct symbol
*t1
, *t2
;
2200 expr
->ctype
= ctype
;
2201 expr
->cast_type
= ctype
;
2204 * Special case: a cast can be followed by an
2205 * initializer, in which case we need to pass
2206 * the type value down to that initializer rather
2207 * than trying to evaluate it as an expression
2209 * A more complex case is when the initializer is
2210 * dereferenced as part of a post-fix expression.
2211 * We need to produce an expression that can be dereferenced.
2213 if (target
->type
== EXPR_INITIALIZER
) {
2214 struct symbol
*sym
= expr
->cast_type
;
2215 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2217 sym
->initializer
= expr
->cast_expression
;
2218 evaluate_symbol(sym
);
2220 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2223 expr
->type
= EXPR_PREOP
;
2231 evaluate_expression(target
);
2234 class1
= classify_type(ctype
, &t1
);
2236 * You can always throw a value away by casting to
2237 * "void" - that's an implicit "force". Note that
2238 * the same is _not_ true of "void *".
2240 if (t1
== &void_ctype
)
2243 if (class1
& TYPE_COMPOUND
)
2244 warning(expr
->pos
, "cast to non-scalar");
2248 sparse_error(expr
->pos
, "cast from unknown type");
2251 class2
= classify_type(t2
, &t2
);
2253 if (class2
& TYPE_COMPOUND
)
2254 warning(expr
->pos
, "cast from non-scalar");
2256 /* allowed cast unfouls */
2257 if (class2
& TYPE_FOULED
)
2258 t2
= t2
->ctype
.base_type
;
2260 if (!(ctype
->ctype
.modifiers
& MOD_FORCE
) && t1
!= t2
) {
2261 if (class1
& TYPE_RESTRICT
)
2262 warning(expr
->pos
, "cast to restricted type");
2263 if (class2
& TYPE_RESTRICT
)
2264 warning(expr
->pos
, "cast from restricted type");
2267 as1
= get_as(ctype
);
2268 as2
= get_as(target
->ctype
);
2269 if (!as1
&& as2
> 0)
2270 warning(expr
->pos
, "cast removes address space of expression");
2271 if (as1
> 0 && as2
> 0 && as1
!= as2
)
2272 warning(expr
->pos
, "cast between address spaces (<asn:%d>-><asn:%d>)", as2
, as1
);
2273 if (as1
> 0 && !as2
)
2274 cast_to_as(expr
, as1
);
2277 * Casts of constant values are special: they
2278 * can be NULL, and thus need to be simplified
2281 if (target
->type
== EXPR_VALUE
)
2282 cast_value(expr
, ctype
, target
, target
->ctype
);
2289 * Evaluate a call expression with a symbol. This
2290 * should expand inline functions, and evaluate
2293 static int evaluate_symbol_call(struct expression
*expr
)
2295 struct expression
*fn
= expr
->fn
;
2296 struct symbol
*ctype
= fn
->ctype
;
2298 if (fn
->type
!= EXPR_PREOP
)
2301 if (ctype
->op
&& ctype
->op
->evaluate
)
2302 return ctype
->op
->evaluate(expr
);
2304 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2306 struct symbol
*curr
= current_fn
;
2307 current_fn
= ctype
->ctype
.base_type
;
2308 examine_fn_arguments(current_fn
);
2310 ret
= inline_function(expr
, ctype
);
2312 /* restore the old function */
2320 static struct symbol
*evaluate_call(struct expression
*expr
)
2323 struct symbol
*ctype
, *sym
;
2324 struct expression
*fn
= expr
->fn
;
2325 struct expression_list
*arglist
= expr
->args
;
2327 if (!evaluate_expression(fn
))
2329 sym
= ctype
= fn
->ctype
;
2330 if (ctype
->type
== SYM_NODE
)
2331 ctype
= ctype
->ctype
.base_type
;
2332 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
2333 ctype
= get_base_type(ctype
);
2335 if (sym
->type
== SYM_NODE
&& fn
->type
== EXPR_PREOP
&&
2336 sym
->op
&& sym
->op
->args
) {
2337 if (!sym
->op
->args(expr
))
2340 if (!evaluate_arguments(sym
, ctype
, arglist
))
2342 if (ctype
->type
!= SYM_FN
) {
2343 sparse_error(expr
->pos
, "not a function %s",
2344 show_ident(sym
->ident
));
2347 args
= expression_list_size(expr
->args
);
2348 fnargs
= symbol_list_size(ctype
->arguments
);
2350 sparse_error(expr
->pos
,
2351 "not enough arguments for function %s",
2352 show_ident(sym
->ident
));
2353 if (args
> fnargs
&& !ctype
->variadic
)
2354 sparse_error(expr
->pos
,
2355 "too many arguments for function %s",
2356 show_ident(sym
->ident
));
2358 if (sym
->type
== SYM_NODE
) {
2359 if (evaluate_symbol_call(expr
))
2362 expr
->ctype
= ctype
->ctype
.base_type
;
2366 struct symbol
*evaluate_expression(struct expression
*expr
)
2373 switch (expr
->type
) {
2376 sparse_error(expr
->pos
, "value expression without a type");
2379 return evaluate_string(expr
);
2381 return evaluate_symbol_expression(expr
);
2383 if (!evaluate_expression(expr
->left
))
2385 if (!evaluate_expression(expr
->right
))
2387 return evaluate_binop(expr
);
2389 return evaluate_logical(expr
);
2391 evaluate_expression(expr
->left
);
2392 if (!evaluate_expression(expr
->right
))
2394 return evaluate_comma(expr
);
2396 if (!evaluate_expression(expr
->left
))
2398 if (!evaluate_expression(expr
->right
))
2400 return evaluate_compare(expr
);
2401 case EXPR_ASSIGNMENT
:
2402 if (!evaluate_expression(expr
->left
))
2404 if (!evaluate_expression(expr
->right
))
2406 return evaluate_assignment(expr
);
2408 if (!evaluate_expression(expr
->unop
))
2410 return evaluate_preop(expr
);
2412 if (!evaluate_expression(expr
->unop
))
2414 return evaluate_postop(expr
);
2416 case EXPR_IMPLIED_CAST
:
2417 return evaluate_cast(expr
);
2419 return evaluate_sizeof(expr
);
2420 case EXPR_PTRSIZEOF
:
2421 return evaluate_ptrsizeof(expr
);
2423 return evaluate_alignof(expr
);
2425 return evaluate_member_dereference(expr
);
2427 return evaluate_call(expr
);
2429 case EXPR_CONDITIONAL
:
2430 return evaluate_conditional_expression(expr
);
2431 case EXPR_STATEMENT
:
2432 expr
->ctype
= evaluate_statement(expr
->statement
);
2436 expr
->ctype
= &ptr_ctype
;
2440 /* Evaluate the type of the symbol .. */
2441 evaluate_symbol(expr
->symbol
);
2442 /* .. but the type of the _expression_ is a "type" */
2443 expr
->ctype
= &type_ctype
;
2446 /* These can not exist as stand-alone expressions */
2447 case EXPR_INITIALIZER
:
2448 case EXPR_IDENTIFIER
:
2451 sparse_error(expr
->pos
, "internal front-end error: initializer in expression");
2454 sparse_error(expr
->pos
, "internal front-end error: SLICE re-evaluated");
2460 static void check_duplicates(struct symbol
*sym
)
2463 struct symbol
*next
= sym
;
2465 while ((next
= next
->same_symbol
) != NULL
) {
2466 const char *typediff
;
2467 evaluate_symbol(next
);
2469 typediff
= type_difference(sym
, next
, 0, 0);
2471 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2472 show_ident(sym
->ident
),
2473 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
2478 unsigned long mod
= sym
->ctype
.modifiers
;
2479 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
2481 if (!(mod
& MOD_TOPLEVEL
))
2485 if (sym
->ident
== &main_ident
)
2487 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
2491 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
2493 struct symbol
*base_type
;
2501 sym
= examine_symbol_type(sym
);
2502 base_type
= get_base_type(sym
);
2506 /* Evaluate the initializers */
2507 if (sym
->initializer
)
2508 evaluate_initializer(sym
, &sym
->initializer
);
2510 /* And finally, evaluate the body of the symbol too */
2511 if (base_type
->type
== SYM_FN
) {
2512 struct symbol
*curr
= current_fn
;
2514 current_fn
= base_type
;
2516 examine_fn_arguments(base_type
);
2517 if (!base_type
->stmt
&& base_type
->inline_stmt
)
2519 if (base_type
->stmt
)
2520 evaluate_statement(base_type
->stmt
);
2528 void evaluate_symbol_list(struct symbol_list
*list
)
2532 FOR_EACH_PTR(list
, sym
) {
2533 evaluate_symbol(sym
);
2534 check_duplicates(sym
);
2535 } END_FOR_EACH_PTR(sym
);
2538 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
2540 struct expression
*expr
= stmt
->expression
;
2541 struct symbol
*ctype
, *fntype
;
2543 evaluate_expression(expr
);
2544 ctype
= degenerate(expr
);
2545 fntype
= current_fn
->ctype
.base_type
;
2546 if (!fntype
|| fntype
== &void_ctype
) {
2547 if (expr
&& ctype
!= &void_ctype
)
2548 sparse_error(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
2553 sparse_error(stmt
->pos
, "return with no return value");
2558 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, ctype
, "return expression", '=');
2562 static void evaluate_if_statement(struct statement
*stmt
)
2564 if (!stmt
->if_conditional
)
2567 evaluate_conditional(stmt
->if_conditional
, 0);
2568 evaluate_statement(stmt
->if_true
);
2569 evaluate_statement(stmt
->if_false
);
2572 static void evaluate_iterator(struct statement
*stmt
)
2574 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
2575 evaluate_conditional(stmt
->iterator_post_condition
,1);
2576 evaluate_statement(stmt
->iterator_pre_statement
);
2577 evaluate_statement(stmt
->iterator_statement
);
2578 evaluate_statement(stmt
->iterator_post_statement
);
2581 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
2583 switch (*constraint
) {
2584 case '=': /* Assignment */
2585 case '+': /* Update */
2588 sparse_error(expr
->pos
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
2592 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
2594 switch (*constraint
) {
2595 case '=': /* Assignment */
2596 case '+': /* Update */
2597 sparse_error(expr
->pos
, "input constraint with assignment (\"%s\")", constraint
);
2601 static void evaluate_asm_statement(struct statement
*stmt
)
2603 struct expression
*expr
;
2606 expr
= stmt
->asm_string
;
2607 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2608 sparse_error(stmt
->pos
, "need constant string for inline asm");
2613 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
2614 struct ident
*ident
;
2617 case 0: /* Identifier */
2619 ident
= (struct ident
*)expr
;
2622 case 1: /* Constraint */
2624 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2625 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
2626 *THIS_ADDRESS(expr
) = NULL
;
2629 verify_output_constraint(expr
, expr
->string
->data
);
2632 case 2: /* Expression */
2634 if (!evaluate_expression(expr
))
2636 if (!lvalue_expression(expr
))
2637 warning(expr
->pos
, "asm output is not an lvalue");
2638 evaluate_assign_to(expr
, expr
->ctype
);
2641 } END_FOR_EACH_PTR(expr
);
2644 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
2645 struct ident
*ident
;
2648 case 0: /* Identifier */
2650 ident
= (struct ident
*)expr
;
2653 case 1: /* Constraint */
2655 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2656 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
2657 *THIS_ADDRESS(expr
) = NULL
;
2660 verify_input_constraint(expr
, expr
->string
->data
);
2663 case 2: /* Expression */
2665 if (!evaluate_expression(expr
))
2669 } END_FOR_EACH_PTR(expr
);
2671 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
2673 sparse_error(stmt
->pos
, "bad asm output");
2676 if (expr
->type
== EXPR_STRING
)
2678 sparse_error(expr
->pos
, "asm clobber is not a string");
2679 } END_FOR_EACH_PTR(expr
);
2682 static void evaluate_case_statement(struct statement
*stmt
)
2684 evaluate_expression(stmt
->case_expression
);
2685 evaluate_expression(stmt
->case_to
);
2686 evaluate_statement(stmt
->case_statement
);
2689 static void check_case_type(struct expression
*switch_expr
,
2690 struct expression
*case_expr
,
2691 struct expression
**enumcase
)
2693 struct symbol
*switch_type
, *case_type
;
2699 switch_type
= switch_expr
->ctype
;
2700 case_type
= evaluate_expression(case_expr
);
2702 if (!switch_type
|| !case_type
)
2706 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
2707 else if (is_enum_type(case_type
))
2708 *enumcase
= case_expr
;
2711 sclass
= classify_type(switch_type
, &switch_type
);
2712 cclass
= classify_type(case_type
, &case_type
);
2714 /* both should be arithmetic */
2715 if (!(sclass
& cclass
& TYPE_NUM
))
2718 /* neither should be floating */
2719 if ((sclass
| cclass
) & TYPE_FLOAT
)
2722 /* if neither is restricted, we are OK */
2723 if (!((sclass
| cclass
) & TYPE_RESTRICT
))
2726 if (!restricted_binop_type(SPECIAL_EQUAL
, case_expr
, switch_expr
,
2727 cclass
, sclass
, case_type
, switch_type
))
2728 warning(case_expr
->pos
, "restricted degrades to integer");
2733 sparse_error(case_expr
->pos
, "incompatible types for 'case' statement");
2736 static void evaluate_switch_statement(struct statement
*stmt
)
2739 struct expression
*enumcase
= NULL
;
2740 struct expression
**enumcase_holder
= &enumcase
;
2741 struct expression
*sel
= stmt
->switch_expression
;
2743 evaluate_expression(sel
);
2744 evaluate_statement(stmt
->switch_statement
);
2747 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
2748 enumcase_holder
= NULL
; /* Only check cases against switch */
2750 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
2751 struct statement
*case_stmt
= sym
->stmt
;
2752 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
2753 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
2754 } END_FOR_EACH_PTR(sym
);
2757 struct symbol
*evaluate_statement(struct statement
*stmt
)
2762 switch (stmt
->type
) {
2763 case STMT_DECLARATION
: {
2765 FOR_EACH_PTR(stmt
->declaration
, s
) {
2767 } END_FOR_EACH_PTR(s
);
2772 return evaluate_return_expression(stmt
);
2774 case STMT_EXPRESSION
:
2775 if (!evaluate_expression(stmt
->expression
))
2777 return degenerate(stmt
->expression
);
2779 case STMT_COMPOUND
: {
2780 struct statement
*s
;
2781 struct symbol
*type
= NULL
;
2783 /* Evaluate the return symbol in the compound statement */
2784 evaluate_symbol(stmt
->ret
);
2787 * Then, evaluate each statement, making the type of the
2788 * compound statement be the type of the last statement
2791 FOR_EACH_PTR(stmt
->stmts
, s
) {
2792 type
= evaluate_statement(s
);
2793 } END_FOR_EACH_PTR(s
);
2799 evaluate_if_statement(stmt
);
2802 evaluate_iterator(stmt
);
2805 evaluate_switch_statement(stmt
);
2808 evaluate_case_statement(stmt
);
2811 return evaluate_statement(stmt
->label_statement
);
2813 evaluate_expression(stmt
->goto_expression
);
2818 evaluate_asm_statement(stmt
);
2821 evaluate_expression(stmt
->expression
);
2824 evaluate_expression(stmt
->range_expression
);
2825 evaluate_expression(stmt
->range_low
);
2826 evaluate_expression(stmt
->range_high
);