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 expression_error(expr
, "undefined identifier '%s'", show_ident(expr
->symbol_name
));
45 examine_symbol_type(sym
);
47 base_type
= get_base_type(sym
);
49 expression_error(expr
, "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;
78 sym
->ctype
.modifiers
= MOD_STATIC
;
79 sym
->ctype
.base_type
= array
;
80 sym
->initializer
= initstr
;
83 initstr
->string
= expr
->string
;
85 array
->array_size
= sym
->array_size
;
86 array
->bit_size
= bits_in_char
* length
;
87 array
->ctype
.alignment
= 1;
88 array
->ctype
.modifiers
= MOD_STATIC
;
89 array
->ctype
.base_type
= &char_ctype
;
92 addr
->ctype
= &lazy_ptr_ctype
;
94 expr
->type
= EXPR_PREOP
;
101 static inline struct symbol
*integer_promotion(struct symbol
*type
)
103 struct symbol
*orig_type
= type
;
104 unsigned long mod
= type
->ctype
.modifiers
;
107 if (type
->type
== SYM_NODE
)
108 type
= type
->ctype
.base_type
;
109 if (type
->type
== SYM_ENUM
)
110 type
= type
->ctype
.base_type
;
111 width
= type
->bit_size
;
114 * Bitfields always promote to the base type,
115 * even if the bitfield might be bigger than
118 if (type
->type
== SYM_BITFIELD
) {
119 type
= type
->ctype
.base_type
;
122 mod
= type
->ctype
.modifiers
;
123 if (width
< bits_in_int
)
126 /* If char/short has as many bits as int, it still gets "promoted" */
127 if (mod
& (MOD_CHAR
| MOD_SHORT
)) {
128 if (mod
& MOD_UNSIGNED
)
136 * integer part of usual arithmetic conversions:
137 * integer promotions are applied
138 * if left and right are identical, we are done
139 * if signedness is the same, convert one with lower rank
140 * unless unsigned argument has rank lower than signed one, convert the
142 * if signed argument is bigger than unsigned one, convert the unsigned.
143 * otherwise, convert signed.
145 * Leaving aside the integer promotions, that is equivalent to
146 * if identical, don't convert
147 * if left is bigger than right, convert right
148 * if right is bigger than left, convert right
149 * otherwise, if signedness is the same, convert one with lower rank
150 * otherwise convert the signed one.
152 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
154 unsigned long lmod
, rmod
;
156 left
= integer_promotion(left
);
157 right
= integer_promotion(right
);
162 if (left
->bit_size
> right
->bit_size
)
165 if (right
->bit_size
> left
->bit_size
)
168 lmod
= left
->ctype
.modifiers
;
169 rmod
= right
->ctype
.modifiers
;
170 if ((lmod
^ rmod
) & MOD_UNSIGNED
) {
171 if (lmod
& MOD_UNSIGNED
)
173 } else if ((lmod
& ~rmod
) & (MOD_LONG
| MOD_LONGLONG
))
181 static int same_cast_type(struct symbol
*orig
, struct symbol
*new)
183 return orig
->bit_size
== new->bit_size
&& orig
->bit_offset
== new->bit_offset
;
186 static struct symbol
*base_type(struct symbol
*node
, unsigned long *modp
, unsigned long *asp
)
188 unsigned long mod
, as
;
192 mod
|= node
->ctype
.modifiers
;
193 as
|= node
->ctype
.as
;
194 if (node
->type
== SYM_NODE
) {
195 node
= node
->ctype
.base_type
;
200 *modp
= mod
& ~MOD_IGNORE
;
205 static int is_same_type(struct expression
*expr
, struct symbol
*new)
207 struct symbol
*old
= expr
->ctype
;
208 unsigned long oldmod
, newmod
, oldas
, newas
;
210 old
= base_type(old
, &oldmod
, &oldas
);
211 new = base_type(new, &newmod
, &newas
);
213 /* Same base type, same address space? */
214 if (old
== new && oldas
== newas
) {
215 unsigned long difmod
;
217 /* Check the modifier bits. */
218 difmod
= (oldmod
^ newmod
) & ~MOD_NOCAST
;
220 /* Exact same type? */
225 * Not the same type, but differs only in "const".
226 * Don't warn about MOD_NOCAST.
228 if (difmod
== MOD_CONST
)
231 if ((oldmod
| newmod
) & MOD_NOCAST
) {
232 const char *tofrom
= "to/from";
233 if (!(newmod
& MOD_NOCAST
))
235 if (!(oldmod
& MOD_NOCAST
))
237 warning(expr
->pos
, "implicit cast %s nocast type", tofrom
);
243 warn_for_different_enum_types (struct position pos
,
244 struct symbol
*typea
,
245 struct symbol
*typeb
)
249 if (typea
->type
== SYM_NODE
)
250 typea
= typea
->ctype
.base_type
;
251 if (typeb
->type
== SYM_NODE
)
252 typeb
= typeb
->ctype
.base_type
;
257 if (typea
->type
== SYM_ENUM
&& typeb
->type
== SYM_ENUM
) {
258 warning(pos
, "mixing different enum types");
259 info(pos
, " %s versus", show_typename(typea
));
260 info(pos
, " %s", show_typename(typeb
));
265 * This gets called for implicit casts in assignments and
266 * integer promotion. We often want to try to move the
267 * cast down, because the ops involved may have been
268 * implicitly cast up, and we can get rid of the casts
271 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
273 struct expression
*expr
;
275 warn_for_different_enum_types (old
->pos
, old
->ctype
, type
);
277 if (is_same_type(old
, type
))
281 * See if we can simplify the op. Move the cast down.
285 if (old
->ctype
->bit_size
< type
->bit_size
)
287 if (old
->op
== '~') {
289 old
->unop
= cast_to(old
->unop
, type
);
294 case EXPR_IMPLIED_CAST
:
295 warn_for_different_enum_types(old
->pos
, old
->ctype
, type
);
297 if (old
->ctype
->bit_size
>= type
->bit_size
) {
298 struct expression
*orig
= old
->cast_expression
;
299 if (same_cast_type(orig
->ctype
, type
))
301 if (old
->ctype
->bit_offset
== type
->bit_offset
) {
303 old
->cast_type
= type
;
313 expr
= alloc_expression(old
->pos
, EXPR_IMPLIED_CAST
);
315 expr
->cast_type
= type
;
316 expr
->cast_expression
= old
;
320 static int is_type_type(struct symbol
*type
)
322 return (type
->ctype
.modifiers
& MOD_TYPE
) != 0;
325 int is_ptr_type(struct symbol
*type
)
327 if (type
->type
== SYM_NODE
)
328 type
= type
->ctype
.base_type
;
329 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
332 static inline int is_float_type(struct symbol
*type
)
334 if (type
->type
== SYM_NODE
)
335 type
= type
->ctype
.base_type
;
336 return type
->ctype
.base_type
== &fp_type
;
339 static inline int is_byte_type(struct symbol
*type
)
341 return type
->bit_size
== bits_in_char
&& type
->type
!= SYM_BITFIELD
;
354 static inline int classify_type(struct symbol
*type
, struct symbol
**base
)
356 static int type_class
[SYM_BAD
+ 1] = {
357 [SYM_PTR
] = TYPE_PTR
,
359 [SYM_ARRAY
] = TYPE_PTR
| TYPE_COMPOUND
,
360 [SYM_STRUCT
] = TYPE_COMPOUND
,
361 [SYM_UNION
] = TYPE_COMPOUND
,
362 [SYM_BITFIELD
] = TYPE_NUM
| TYPE_BITFIELD
,
363 [SYM_RESTRICT
] = TYPE_NUM
| TYPE_RESTRICT
,
364 [SYM_FOULED
] = TYPE_NUM
| TYPE_RESTRICT
| TYPE_FOULED
,
366 if (type
->type
== SYM_NODE
)
367 type
= type
->ctype
.base_type
;
368 if (type
->type
== SYM_ENUM
)
369 type
= type
->ctype
.base_type
;
371 if (type
->type
== SYM_BASETYPE
) {
372 if (type
->ctype
.base_type
== &int_type
)
374 if (type
->ctype
.base_type
== &fp_type
)
375 return TYPE_NUM
| TYPE_FLOAT
;
377 return type_class
[type
->type
];
380 static inline int is_string_type(struct symbol
*type
)
382 if (type
->type
== SYM_NODE
)
383 type
= type
->ctype
.base_type
;
384 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
387 static struct symbol
*bad_expr_type(struct expression
*expr
)
389 sparse_error(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
390 switch (expr
->type
) {
393 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
394 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
398 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
404 return expr
->ctype
= &bad_ctype
;
407 static int restricted_value(struct expression
*v
, struct symbol
*type
)
409 if (v
->type
!= EXPR_VALUE
)
416 static int restricted_binop(int op
, struct symbol
*type
)
421 case SPECIAL_AND_ASSIGN
:
422 case SPECIAL_OR_ASSIGN
:
423 case SPECIAL_XOR_ASSIGN
:
424 return 1; /* unfoul */
428 return 2; /* keep fouled */
430 case SPECIAL_NOTEQUAL
:
431 return 3; /* warn if fouled */
437 static int restricted_unop(int op
, struct symbol
**type
)
440 if ((*type
)->bit_size
< bits_in_int
)
441 *type
= befoul(*type
);
448 static struct symbol
*restricted_binop_type(int op
,
449 struct expression
*left
,
450 struct expression
*right
,
451 int lclass
, int rclass
,
452 struct symbol
*ltype
,
453 struct symbol
*rtype
)
455 struct symbol
*ctype
= NULL
;
456 if (lclass
& TYPE_RESTRICT
) {
457 if (rclass
& TYPE_RESTRICT
) {
458 if (ltype
== rtype
) {
460 } else if (lclass
& TYPE_FOULED
) {
461 if (ltype
->ctype
.base_type
== rtype
)
463 } else if (rclass
& TYPE_FOULED
) {
464 if (rtype
->ctype
.base_type
== ltype
)
468 if (!restricted_value(right
, ltype
))
471 } else if (!restricted_value(left
, rtype
))
475 switch (restricted_binop(op
, ctype
)) {
477 if ((lclass
^ rclass
) & TYPE_FOULED
)
478 ctype
= ctype
->ctype
.base_type
;
481 if (!(lclass
& rclass
& TYPE_FOULED
))
493 static struct symbol
*usual_conversions(int op
,
494 struct expression
**left
,
495 struct expression
**right
,
496 int lclass
, int rclass
,
497 struct symbol
*ltype
,
498 struct symbol
*rtype
)
500 struct symbol
*ctype
;
502 warn_for_different_enum_types((*right
)->pos
, (*left
)->ctype
, (*right
)->ctype
);
504 if ((lclass
| rclass
) & TYPE_RESTRICT
)
508 if (!(lclass
& TYPE_FLOAT
)) {
509 if (!(rclass
& TYPE_FLOAT
))
510 ctype
= bigger_int_type(ltype
, rtype
);
513 } else if (rclass
& TYPE_FLOAT
) {
514 unsigned long lmod
= ltype
->ctype
.modifiers
;
515 unsigned long rmod
= rtype
->ctype
.modifiers
;
516 if (rmod
& ~lmod
& (MOD_LONG
| MOD_LONGLONG
))
524 *left
= cast_to(*left
, ctype
);
525 *right
= cast_to(*right
, ctype
);
529 ctype
= restricted_binop_type(op
, *left
, *right
,
530 lclass
, rclass
, ltype
, rtype
);
534 if (lclass
& TYPE_RESTRICT
) {
535 warning((*left
)->pos
, "restricted degrades to integer");
536 ltype
= ltype
->ctype
.base_type
;
537 if (is_restricted_type(ltype
)) /* was fouled */
538 ltype
= ltype
->ctype
.base_type
;
540 if (rclass
& TYPE_RESTRICT
) {
541 warning((*right
)->pos
, "restricted degrades to integer");
542 rtype
= rtype
->ctype
.base_type
;
543 if (is_restricted_type(rtype
)) /* was fouled */
544 rtype
= rtype
->ctype
.base_type
;
549 static struct symbol
*evaluate_arith(struct expression
*expr
, int float_ok
)
551 struct symbol
*ltype
, *rtype
;
552 int lclass
= classify_type(expr
->left
->ctype
, <ype
);
553 int rclass
= classify_type(expr
->right
->ctype
, &rtype
);
554 struct symbol
*ctype
;
556 if (!(lclass
& rclass
& TYPE_NUM
))
559 if (!float_ok
&& (lclass
| rclass
) & TYPE_FLOAT
)
562 ctype
= usual_conversions(expr
->op
, &expr
->left
, &expr
->right
,
563 lclass
, rclass
, ltype
, rtype
);
568 return bad_expr_type(expr
);
571 static inline int lvalue_expression(struct expression
*expr
)
573 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
576 static int ptr_object_size(struct symbol
*ptr_type
)
578 if (ptr_type
->type
== SYM_NODE
)
579 ptr_type
= ptr_type
->ctype
.base_type
;
580 if (ptr_type
->type
== SYM_PTR
)
581 ptr_type
= get_base_type(ptr_type
);
582 return ptr_type
->bit_size
;
585 static inline int want_int(struct expression
**expr
, struct symbol
**ctype
)
587 int class = classify_type((*expr
)->ctype
, ctype
);
589 if (!(class & TYPE_NUM
))
591 if (!(class & TYPE_RESTRICT
))
593 warning((*expr
)->pos
, "restricted degrades to integer");
594 if (class & TYPE_FOULED
) /* unfoul it first */
595 (*ctype
) = (*ctype
)->ctype
.base_type
;
596 (*ctype
) = (*ctype
)->ctype
.base_type
; /* get to arithmetic type */
597 *expr
= cast_to(*expr
, *ctype
);
601 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*ctype
, struct expression
**ip
)
603 struct expression
*i
= *ip
;
604 struct symbol
*ptr_type
= ctype
, *itype
;
607 if (ptr_type
->type
== SYM_NODE
)
608 ptr_type
= ptr_type
->ctype
.base_type
;
610 if (!want_int(&i
, &itype
))
611 return bad_expr_type(expr
);
613 examine_symbol_type(ctype
);
615 if (!ctype
->ctype
.base_type
) {
616 expression_error(expr
, "missing type information");
620 /* Get the size of whatever the pointer points to */
621 bit_size
= ptr_object_size(ctype
);
623 if (bit_size
> bits_in_char
) {
624 int multiply
= bit_size
>> 3;
625 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
627 if (i
->type
== EXPR_VALUE
) {
628 val
->value
= i
->value
* multiply
;
629 val
->ctype
= size_t_ctype
;
632 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
634 val
->ctype
= size_t_ctype
;
635 val
->value
= bit_size
>> 3;
638 mul
->ctype
= size_t_ctype
;
650 static struct symbol
*evaluate_add(struct expression
*expr
)
652 struct expression
*left
= expr
->left
, *right
= expr
->right
;
653 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
655 if (is_ptr_type(ltype
))
656 return evaluate_ptr_add(expr
, degenerate(left
), &expr
->right
);
658 if (is_ptr_type(rtype
))
659 return evaluate_ptr_add(expr
, degenerate(right
), &expr
->left
);
661 return evaluate_arith(expr
, 1);
664 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
665 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
668 unsigned long mod1
, mod2
, diff
;
669 unsigned long as1
, as2
;
671 struct symbol
*base1
, *base2
;
673 if (target
== source
)
675 if (!target
|| !source
)
676 return "different types";
678 * Peel of per-node information.
679 * FIXME! Check alignment and context too here!
681 mod1
= target
->ctype
.modifiers
;
682 as1
= target
->ctype
.as
;
683 mod2
= source
->ctype
.modifiers
;
684 as2
= source
->ctype
.as
;
685 if (target
->type
== SYM_NODE
) {
686 target
= target
->ctype
.base_type
;
689 if (target
->type
== SYM_PTR
) {
693 mod1
|= target
->ctype
.modifiers
;
694 as1
|= target
->ctype
.as
;
696 if (source
->type
== SYM_NODE
) {
697 source
= source
->ctype
.base_type
;
700 if (source
->type
== SYM_PTR
) {
704 mod2
|= source
->ctype
.modifiers
;
705 as2
|= source
->ctype
.as
;
707 if (target
->type
== SYM_ENUM
) {
708 target
= target
->ctype
.base_type
;
712 if (source
->type
== SYM_ENUM
) {
713 source
= source
->ctype
.base_type
;
718 if (target
== source
)
720 if (!target
|| !source
)
721 return "different types";
723 type1
= target
->type
;
724 base1
= target
->ctype
.base_type
;
726 type2
= source
->type
;
727 base2
= source
->ctype
.base_type
;
730 * Pointers to functions compare as the function itself
732 if (type1
== SYM_PTR
&& base1
) {
733 base1
= examine_symbol_type(base1
);
734 switch (base1
->type
) {
738 base1
= base1
->ctype
.base_type
;
743 if (type2
== SYM_PTR
&& base2
) {
744 base2
= examine_symbol_type(base2
);
745 switch (base2
->type
) {
749 base2
= base2
->ctype
.base_type
;
755 /* Arrays degenerate to pointers for type comparisons */
756 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
757 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
759 if (type1
!= type2
|| type1
== SYM_RESTRICT
)
760 return "different base types";
762 /* Must be same address space to be comparable */
763 if (Waddress_space
&& as1
!= as2
)
764 return "different address spaces";
766 /* Ignore differences in storage types or addressability */
767 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
768 diff
&= (mod1
& ~target_mod_ignore
) | (mod2
& ~source_mod_ignore
);
771 return "different type sizes";
772 if (diff
& ~MOD_SIGNEDNESS
)
773 return "different modifiers";
775 /* Differs in signedness only.. */
778 * Warn if both are explicitly signed ("unsigned" is obviously
779 * always explicit, and since we know one of them has to be
780 * unsigned, we check if the signed one was explicit).
782 if ((mod1
| mod2
) & MOD_EXPLICITLY_SIGNED
)
783 return "different explicit signedness";
786 * "char" matches both "unsigned char" and "signed char",
787 * so if the explicit test didn't trigger, then we should
788 * not warn about a char.
790 if (!(mod1
& MOD_CHAR
))
791 return "different signedness";
795 if (type1
== SYM_FN
) {
797 struct symbol
*arg1
, *arg2
;
798 if (base1
->variadic
!= base2
->variadic
)
799 return "incompatible variadic arguments";
800 PREPARE_PTR_LIST(target
->arguments
, arg1
);
801 PREPARE_PTR_LIST(source
->arguments
, arg2
);
805 diffstr
= type_difference(arg1
, arg2
, 0, 0);
807 static char argdiff
[80];
808 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diffstr
);
817 FINISH_PTR_LIST(arg2
);
818 FINISH_PTR_LIST(arg1
);
827 static int is_null_ptr(struct expression
*expr
)
829 if (expr
->type
!= EXPR_VALUE
|| expr
->value
)
831 if (Wnon_pointer_null
&& !is_ptr_type(expr
->ctype
))
832 warning(expr
->pos
, "Using plain integer as NULL pointer");
836 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
838 /* NULL expression? Just return the type of the "other side" */
847 * Ignore differences in "volatile" and "const"ness when
848 * subtracting pointers
850 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
852 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
**rp
)
854 const char *typediff
;
855 struct symbol
*ctype
;
856 struct symbol
*ltype
, *rtype
;
857 struct expression
*r
= *rp
;
859 ltype
= degenerate(l
);
860 rtype
= degenerate(r
);
863 * If it is an integer subtract: the ptr add case will do the
866 if (!is_ptr_type(rtype
))
867 return evaluate_ptr_add(expr
, degenerate(l
), rp
);
870 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
872 ctype
= common_ptr_type(l
, r
);
874 expression_error(expr
, "subtraction of different types can't work (%s)", typediff
);
878 examine_symbol_type(ctype
);
880 /* Figure out the base type we point to */
881 if (ctype
->type
== SYM_NODE
)
882 ctype
= ctype
->ctype
.base_type
;
883 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
884 expression_error(expr
, "subtraction of functions? Share your drugs");
887 ctype
= get_base_type(ctype
);
889 expr
->ctype
= ssize_t_ctype
;
890 if (ctype
->bit_size
> bits_in_char
) {
891 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
892 struct expression
*div
= expr
;
893 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
894 unsigned long value
= ctype
->bit_size
>> 3;
896 val
->ctype
= size_t_ctype
;
899 if (value
& (value
-1)) {
900 if (Wptr_subtraction_blows
)
901 warning(expr
->pos
, "potentially expensive pointer subtraction");
905 sub
->ctype
= ssize_t_ctype
;
914 return ssize_t_ctype
;
917 static struct symbol
*evaluate_sub(struct expression
*expr
)
919 struct expression
*left
= expr
->left
;
920 struct symbol
*ltype
= left
->ctype
;
922 if (is_ptr_type(ltype
))
923 return evaluate_ptr_sub(expr
, left
, &expr
->right
);
925 return evaluate_arith(expr
, 1);
928 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
930 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
932 struct symbol
*ctype
;
937 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
938 warning(expr
->pos
, "assignment expression in conditional");
940 ctype
= evaluate_expression(expr
);
942 if (is_safe_type(ctype
))
943 warning(expr
->pos
, "testing a 'safe expression'");
949 static struct symbol
*evaluate_logical(struct expression
*expr
)
951 if (!evaluate_conditional(expr
->left
, 0))
953 if (!evaluate_conditional(expr
->right
, 0))
956 expr
->ctype
= &bool_ctype
;
960 static struct symbol
*evaluate_shift(struct expression
*expr
)
962 struct symbol
*ltype
, *rtype
;
964 if (want_int(&expr
->left
, <ype
) && want_int(&expr
->right
, &rtype
)) {
965 struct symbol
*ctype
= integer_promotion(ltype
);
966 expr
->left
= cast_to(expr
->left
, ctype
);
968 ctype
= integer_promotion(rtype
);
969 expr
->right
= cast_to(expr
->right
, ctype
);
972 return bad_expr_type(expr
);
975 static struct symbol
*evaluate_binop(struct expression
*expr
)
978 // addition can take ptr+int, fp and int
980 return evaluate_add(expr
);
982 // subtraction can take ptr-ptr, fp and int
984 return evaluate_sub(expr
);
986 // Arithmetic operations can take fp and int
988 return evaluate_arith(expr
, 1);
990 // shifts do integer promotions, but that's it.
991 case SPECIAL_LEFTSHIFT
: case SPECIAL_RIGHTSHIFT
:
992 return evaluate_shift(expr
);
994 // The rest are integer operations
995 // '%', '&', '^', '|'
997 return evaluate_arith(expr
, 0);
1001 static struct symbol
*evaluate_comma(struct expression
*expr
)
1003 expr
->ctype
= expr
->right
->ctype
;
1007 static int modify_for_unsigned(int op
)
1010 op
= SPECIAL_UNSIGNED_LT
;
1012 op
= SPECIAL_UNSIGNED_GT
;
1013 else if (op
== SPECIAL_LTE
)
1014 op
= SPECIAL_UNSIGNED_LTE
;
1015 else if (op
== SPECIAL_GTE
)
1016 op
= SPECIAL_UNSIGNED_GTE
;
1020 static struct symbol
*evaluate_compare(struct expression
*expr
)
1022 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1023 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
1024 struct symbol
*ctype
;
1027 if (is_type_type(ltype
) && is_type_type(rtype
))
1030 if (is_safe_type(ltype
) || is_safe_type(rtype
))
1031 warning(expr
->pos
, "testing a 'safe expression'");
1033 /* Pointer types? */
1034 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
1035 // FIXME! Check the types for compatibility
1036 expr
->op
= modify_for_unsigned(expr
->op
);
1040 ctype
= evaluate_arith(expr
, 1);
1042 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
1043 expr
->op
= modify_for_unsigned(expr
->op
);
1046 expr
->ctype
= &bool_ctype
;
1051 * FIXME!! This should do casts, array degeneration etc..
1053 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
1055 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
1057 if (ltype
->type
== SYM_NODE
)
1058 ltype
= ltype
->ctype
.base_type
;
1060 if (rtype
->type
== SYM_NODE
)
1061 rtype
= rtype
->ctype
.base_type
;
1063 if (ltype
->type
== SYM_PTR
) {
1064 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
1068 if (rtype
->type
== SYM_PTR
) {
1069 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
1076 * NOTE! The degenerate case of "x ? : y", where we don't
1077 * have a true case, this will possibly promote "x" to the
1078 * same type as "y", and thus _change_ the conditional
1079 * test in the expression. But since promotion is "safe"
1080 * for testing, that's OK.
1082 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
1084 struct expression
**true;
1085 struct symbol
*ctype
, *ltype
, *rtype
;
1087 const char * typediff
;
1089 if (!evaluate_conditional(expr
->conditional
, 0))
1091 if (!evaluate_expression(expr
->cond_false
))
1094 ctype
= degenerate(expr
->conditional
);
1095 rtype
= degenerate(expr
->cond_false
);
1097 true = &expr
->conditional
;
1099 if (expr
->cond_true
) {
1100 if (!evaluate_expression(expr
->cond_true
))
1102 ltype
= degenerate(expr
->cond_true
);
1103 true = &expr
->cond_true
;
1106 lclass
= classify_type(ltype
, <ype
);
1107 rclass
= classify_type(rtype
, &rtype
);
1108 if (lclass
& rclass
& TYPE_NUM
) {
1109 ctype
= usual_conversions('?', true, &expr
->cond_false
,
1110 lclass
, rclass
, ltype
, rtype
);
1113 ctype
= compatible_ptr_type(*true, expr
->cond_false
);
1117 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
1120 expression_error(expr
, "incompatible types in conditional expression (%s)", typediff
);
1124 expr
->ctype
= ctype
;
1128 /* FP assignments can not do modulo or bit operations */
1129 static int compatible_float_op(int op
)
1132 op
== SPECIAL_ADD_ASSIGN
||
1133 op
== SPECIAL_SUB_ASSIGN
||
1134 op
== SPECIAL_MUL_ASSIGN
||
1135 op
== SPECIAL_DIV_ASSIGN
;
1138 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1139 struct expression
**rp
, struct symbol
*source
, const char *where
, int op
)
1141 const char *typediff
;
1142 struct symbol
*t
, *s
;
1144 int tclass
= classify_type(target
, &t
);
1145 int sclass
= classify_type(source
, &s
);
1147 if (tclass
& sclass
& TYPE_NUM
) {
1148 if (tclass
& TYPE_FLOAT
&& !compatible_float_op(op
)) {
1149 expression_error(expr
, "invalid assignment");
1152 if (tclass
& TYPE_RESTRICT
) {
1153 if (!restricted_binop(op
, target
)) {
1154 expression_error(expr
, "bad restricted assignment");
1157 /* allowed assignments unfoul */
1158 if (sclass
& TYPE_FOULED
&& s
->ctype
.base_type
== t
)
1160 if (!restricted_value(*rp
, target
))
1162 } else if (!(sclass
& TYPE_RESTRICT
))
1164 } else if (tclass
& TYPE_PTR
) {
1165 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1166 evaluate_ptr_add(expr
, target
, rp
);
1170 expression_error(expr
, "invalid pointer assignment");
1173 } else if (op
!= '=') {
1174 expression_error(expr
, "invalid assignment");
1178 /* It's OK if the target is more volatile or const than the source */
1179 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
1183 /* Pointer destination? */
1184 if (tclass
& TYPE_PTR
) {
1185 struct expression
*right
= *rp
;
1188 // NULL pointer is always OK
1189 if (is_null_ptr(right
))
1192 /* "void *" matches anything as long as the address space is OK */
1193 target_as
= t
->ctype
.as
| target
->ctype
.as
;
1194 source_as
= s
->ctype
.as
| source
->ctype
.as
;
1195 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
1196 s
= get_base_type(s
);
1197 t
= get_base_type(t
);
1198 if (s
== &void_ctype
|| t
== &void_ctype
)
1203 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1204 info(expr
->pos
, " expected %s", show_typename(target
));
1205 info(expr
->pos
, " got %s", show_typename(source
));
1206 *rp
= cast_to(*rp
, target
);
1209 *rp
= cast_to(*rp
, target
);
1213 static void mark_assigned(struct expression
*expr
)
1219 switch (expr
->type
) {
1224 if (sym
->type
!= SYM_NODE
)
1226 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1230 mark_assigned(expr
->left
);
1231 mark_assigned(expr
->right
);
1234 mark_assigned(expr
->cast_expression
);
1237 mark_assigned(expr
->base
);
1245 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1247 if (type
->ctype
.modifiers
& MOD_CONST
)
1248 expression_error(left
, "assignment to const expression");
1250 /* We know left is an lvalue, so it's a "preop-*" */
1251 mark_assigned(left
->unop
);
1254 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1256 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1257 struct expression
*where
= expr
;
1258 struct symbol
*ltype
, *rtype
;
1260 if (!lvalue_expression(left
)) {
1261 expression_error(expr
, "not an lvalue");
1265 ltype
= left
->ctype
;
1267 rtype
= degenerate(right
);
1269 if (!compatible_assignment_types(where
, ltype
, &where
->right
, rtype
, "assignment", expr
->op
))
1272 evaluate_assign_to(left
, ltype
);
1274 expr
->ctype
= ltype
;
1278 static void examine_fn_arguments(struct symbol
*fn
)
1282 FOR_EACH_PTR(fn
->arguments
, s
) {
1283 struct symbol
*arg
= evaluate_symbol(s
);
1284 /* Array/function arguments silently degenerate into pointers */
1290 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1291 if (arg
->type
== SYM_ARRAY
)
1292 ptr
->ctype
= arg
->ctype
;
1294 ptr
->ctype
.base_type
= arg
;
1295 ptr
->ctype
.as
|= s
->ctype
.as
;
1296 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
& MOD_PTRINHERIT
;
1298 s
->ctype
.base_type
= ptr
;
1300 s
->ctype
.modifiers
&= ~MOD_PTRINHERIT
;
1303 examine_symbol_type(s
);
1310 } END_FOR_EACH_PTR(s
);
1313 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1315 /* Take the modifiers of the pointer, and apply them to the member */
1316 mod
|= sym
->ctype
.modifiers
;
1317 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1318 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1320 newsym
->ctype
.as
= as
;
1321 newsym
->ctype
.modifiers
= mod
;
1327 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1329 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1330 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1332 node
->ctype
.base_type
= ptr
;
1333 ptr
->bit_size
= bits_in_pointer
;
1334 ptr
->ctype
.alignment
= pointer_alignment
;
1336 node
->bit_size
= bits_in_pointer
;
1337 node
->ctype
.alignment
= pointer_alignment
;
1340 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1341 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1342 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1344 if (sym
->type
== SYM_NODE
) {
1345 ptr
->ctype
.as
|= sym
->ctype
.as
;
1346 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1347 sym
= sym
->ctype
.base_type
;
1349 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1350 ptr
->ctype
.as
|= sym
->ctype
.as
;
1351 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1352 sym
= sym
->ctype
.base_type
;
1354 ptr
->ctype
.base_type
= sym
;
1359 /* Arrays degenerate into pointers on pointer arithmetic */
1360 static struct symbol
*degenerate(struct expression
*expr
)
1362 struct symbol
*ctype
, *base
;
1366 ctype
= expr
->ctype
;
1369 base
= examine_symbol_type(ctype
);
1370 if (ctype
->type
== SYM_NODE
)
1371 base
= ctype
->ctype
.base_type
;
1373 * Arrays degenerate into pointers to the entries, while
1374 * functions degenerate into pointers to themselves.
1375 * If array was part of non-lvalue compound, we create a copy
1376 * of that compound first and then act as if we were dealing with
1377 * the corresponding field in there.
1379 switch (base
->type
) {
1381 if (expr
->type
== EXPR_SLICE
) {
1382 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1383 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1385 a
->ctype
.base_type
= expr
->base
->ctype
;
1386 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1387 a
->array_size
= expr
->base
->ctype
->array_size
;
1389 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1391 e0
->ctype
= &lazy_ptr_ctype
;
1393 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1396 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1398 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1400 e2
->right
= expr
->base
;
1402 e2
->ctype
= expr
->base
->ctype
;
1404 if (expr
->r_bitpos
) {
1405 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1408 e3
->right
= alloc_const_expression(expr
->pos
,
1409 expr
->r_bitpos
>> 3);
1410 e3
->ctype
= &lazy_ptr_ctype
;
1415 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1418 e4
->ctype
= &lazy_ptr_ctype
;
1421 expr
->type
= EXPR_PREOP
;
1425 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1426 expression_error(expr
, "strange non-value function or array");
1429 *expr
= *expr
->unop
;
1430 ctype
= create_pointer(expr
, ctype
, 1);
1431 expr
->ctype
= ctype
;
1438 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1440 struct expression
*op
= expr
->unop
;
1441 struct symbol
*ctype
;
1443 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1444 expression_error(expr
, "not addressable");
1450 if (expr
->type
== EXPR_SYMBOL
) {
1451 struct symbol
*sym
= expr
->symbol
;
1452 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1456 * symbol expression evaluation is lazy about the type
1457 * of the sub-expression, so we may have to generate
1458 * the type here if so..
1460 if (expr
->ctype
== &lazy_ptr_ctype
) {
1461 ctype
= create_pointer(expr
, ctype
, 0);
1462 expr
->ctype
= ctype
;
1468 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1470 struct expression
*op
= expr
->unop
;
1471 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1473 /* Simplify: *&(expr) => (expr) */
1474 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1479 /* Dereferencing a node drops all the node information. */
1480 if (ctype
->type
== SYM_NODE
)
1481 ctype
= ctype
->ctype
.base_type
;
1483 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1484 target
= ctype
->ctype
.base_type
;
1486 switch (ctype
->type
) {
1488 expression_error(expr
, "cannot dereference this type");
1491 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1492 merge_type(node
, ctype
);
1496 if (!lvalue_expression(op
)) {
1497 expression_error(op
, "non-lvalue array??");
1501 /* Do the implied "addressof" on the array */
1505 * When an array is dereferenced, we need to pick
1506 * up the attributes of the original node too..
1508 merge_type(node
, op
->ctype
);
1509 merge_type(node
, ctype
);
1513 node
->bit_size
= target
->bit_size
;
1514 node
->array_size
= target
->array_size
;
1521 * Unary post-ops: x++ and x--
1523 static struct symbol
*evaluate_postop(struct expression
*expr
)
1525 struct expression
*op
= expr
->unop
;
1526 struct symbol
*ctype
= op
->ctype
;
1528 if (!lvalue_expression(expr
->unop
)) {
1529 expression_error(expr
, "need lvalue expression for ++/--");
1532 if (is_restricted_type(ctype
) && restricted_unop(expr
->op
, &ctype
)) {
1533 expression_error(expr
, "bad operation on restricted");
1535 } else if (is_fouled_type(ctype
) && restricted_unop(expr
->op
, &ctype
)) {
1536 expression_error(expr
, "bad operation on restricted");
1540 evaluate_assign_to(op
, ctype
);
1542 expr
->ctype
= ctype
;
1544 if (is_ptr_type(ctype
))
1545 expr
->op_value
= ptr_object_size(ctype
) >> 3;
1550 static struct symbol
*evaluate_sign(struct expression
*expr
)
1552 struct symbol
*ctype
= expr
->unop
->ctype
;
1553 if (is_int_type(ctype
)) {
1554 struct symbol
*rtype
= rtype
= integer_promotion(ctype
);
1555 expr
->unop
= cast_to(expr
->unop
, rtype
);
1557 } else if (is_float_type(ctype
) && expr
->op
!= '~') {
1558 /* no conversions needed */
1559 } else if (is_restricted_type(ctype
) && !restricted_unop(expr
->op
, &ctype
)) {
1560 /* no conversions needed */
1561 } else if (is_fouled_type(ctype
) && !restricted_unop(expr
->op
, &ctype
)) {
1562 /* no conversions needed */
1564 return bad_expr_type(expr
);
1566 if (expr
->op
== '+')
1567 *expr
= *expr
->unop
;
1568 expr
->ctype
= ctype
;
1572 static struct symbol
*evaluate_preop(struct expression
*expr
)
1574 struct symbol
*ctype
= expr
->unop
->ctype
;
1578 *expr
= *expr
->unop
;
1584 return evaluate_sign(expr
);
1587 return evaluate_dereference(expr
);
1590 return evaluate_addressof(expr
);
1592 case SPECIAL_INCREMENT
:
1593 case SPECIAL_DECREMENT
:
1595 * From a type evaluation standpoint the preops are
1596 * the same as the postops
1598 return evaluate_postop(expr
);
1601 if (is_safe_type(ctype
))
1602 warning(expr
->pos
, "testing a 'safe expression'");
1603 if (is_float_type(ctype
)) {
1604 struct expression
*arg
= expr
->unop
;
1605 expr
->type
= EXPR_BINOP
;
1606 expr
->op
= SPECIAL_EQUAL
;
1608 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1609 expr
->right
->ctype
= ctype
;
1610 expr
->right
->fvalue
= 0;
1611 } else if (is_fouled_type(ctype
)) {
1612 warning(expr
->pos
, "restricted degrades to integer");
1614 ctype
= &bool_ctype
;
1620 expr
->ctype
= ctype
;
1624 static struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1626 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1627 struct ptr_list
*list
= head
;
1633 for (i
= 0; i
< list
->nr
; i
++) {
1634 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1636 if (sym
->ident
!= ident
)
1638 *offset
= sym
->offset
;
1641 struct symbol
*ctype
= sym
->ctype
.base_type
;
1645 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1647 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1650 *offset
+= sym
->offset
;
1654 } while ((list
= list
->next
) != head
);
1658 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1660 struct expression
*add
;
1663 * Create a new add-expression
1665 * NOTE! Even if we just add zero, we need a new node
1666 * for the member pointer, since it has a different
1667 * type than the original pointer. We could make that
1668 * be just a cast, but the fact is, a node is a node,
1669 * so we might as well just do the "add zero" here.
1671 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1674 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1675 add
->right
->ctype
= &int_ctype
;
1676 add
->right
->value
= offset
;
1679 * The ctype of the pointer will be lazily evaluated if
1680 * we ever take the address of this member dereference..
1682 add
->ctype
= &lazy_ptr_ctype
;
1686 /* structure/union dereference */
1687 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1690 struct symbol
*ctype
, *member
;
1691 struct expression
*deref
= expr
->deref
, *add
;
1692 struct ident
*ident
= expr
->member
;
1696 if (!evaluate_expression(deref
))
1699 expression_error(expr
, "bad member name");
1703 ctype
= deref
->ctype
;
1704 address_space
= ctype
->ctype
.as
;
1705 mod
= ctype
->ctype
.modifiers
;
1706 if (ctype
->type
== SYM_NODE
) {
1707 ctype
= ctype
->ctype
.base_type
;
1708 address_space
|= ctype
->ctype
.as
;
1709 mod
|= ctype
->ctype
.modifiers
;
1711 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1712 expression_error(expr
, "expected structure or union");
1715 examine_symbol_type(ctype
);
1717 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1719 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1720 const char *name
= "<unnamed>";
1723 name
= ctype
->ident
->name
;
1724 namelen
= ctype
->ident
->len
;
1726 if (ctype
->symbol_list
)
1727 expression_error(expr
, "no member '%s' in %s %.*s",
1728 show_ident(ident
), type
, namelen
, name
);
1730 expression_error(expr
, "using member '%s' in "
1731 "incomplete %s %.*s", show_ident(ident
),
1732 type
, namelen
, name
);
1737 * The member needs to take on the address space and modifiers of
1738 * the "parent" type.
1740 member
= convert_to_as_mod(member
, address_space
, mod
);
1741 ctype
= get_base_type(member
);
1743 if (!lvalue_expression(deref
)) {
1744 if (deref
->type
!= EXPR_SLICE
) {
1748 expr
->base
= deref
->base
;
1749 expr
->r_bitpos
= deref
->r_bitpos
;
1751 expr
->r_bitpos
+= offset
<< 3;
1752 expr
->type
= EXPR_SLICE
;
1753 expr
->r_nrbits
= member
->bit_size
;
1754 expr
->r_bitpos
+= member
->bit_offset
;
1755 expr
->ctype
= member
;
1759 deref
= deref
->unop
;
1760 expr
->deref
= deref
;
1762 add
= evaluate_offset(deref
, offset
);
1763 expr
->type
= EXPR_PREOP
;
1767 expr
->ctype
= member
;
1771 static int is_promoted(struct expression
*expr
)
1774 switch (expr
->type
) {
1777 case EXPR_CONDITIONAL
:
1801 static struct symbol
*evaluate_cast(struct expression
*);
1803 static struct symbol
*evaluate_type_information(struct expression
*expr
)
1805 struct symbol
*sym
= expr
->cast_type
;
1807 sym
= evaluate_expression(expr
->cast_expression
);
1811 * Expressions of restricted types will possibly get
1812 * promoted - check that here
1814 if (is_restricted_type(sym
)) {
1815 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
1817 } else if (is_fouled_type(sym
)) {
1821 examine_symbol_type(sym
);
1822 if (is_bitfield_type(sym
)) {
1823 expression_error(expr
, "trying to examine bitfield type");
1829 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1831 struct symbol
*type
;
1834 type
= evaluate_type_information(expr
);
1838 size
= type
->bit_size
;
1839 if ((size
< 0) || (size
& 7))
1840 expression_error(expr
, "cannot size expression");
1841 expr
->type
= EXPR_VALUE
;
1842 expr
->value
= size
>> 3;
1843 expr
->ctype
= size_t_ctype
;
1844 return size_t_ctype
;
1847 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
1849 struct symbol
*type
;
1852 type
= evaluate_type_information(expr
);
1856 if (type
->type
== SYM_NODE
)
1857 type
= type
->ctype
.base_type
;
1860 switch (type
->type
) {
1864 type
= get_base_type(type
);
1868 expression_error(expr
, "expected pointer expression");
1871 size
= type
->bit_size
;
1874 expr
->type
= EXPR_VALUE
;
1875 expr
->value
= size
>> 3;
1876 expr
->ctype
= size_t_ctype
;
1877 return size_t_ctype
;
1880 static struct symbol
*evaluate_alignof(struct expression
*expr
)
1882 struct symbol
*type
;
1884 type
= evaluate_type_information(expr
);
1888 expr
->type
= EXPR_VALUE
;
1889 expr
->value
= type
->ctype
.alignment
;
1890 expr
->ctype
= size_t_ctype
;
1891 return size_t_ctype
;
1894 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1896 struct expression
*expr
;
1897 struct symbol_list
*argument_types
= fn
->arguments
;
1898 struct symbol
*argtype
;
1901 PREPARE_PTR_LIST(argument_types
, argtype
);
1902 FOR_EACH_PTR (head
, expr
) {
1903 struct expression
**p
= THIS_ADDRESS(expr
);
1904 struct symbol
*ctype
, *target
;
1905 ctype
= evaluate_expression(expr
);
1910 ctype
= degenerate(expr
);
1913 if (!target
&& ctype
->bit_size
< bits_in_int
)
1914 target
= &int_ctype
;
1916 static char where
[30];
1917 examine_symbol_type(target
);
1918 sprintf(where
, "argument %d", i
);
1919 compatible_assignment_types(expr
, target
, p
, ctype
, where
, '=');
1923 NEXT_PTR_LIST(argtype
);
1924 } END_FOR_EACH_PTR(expr
);
1925 FINISH_PTR_LIST(argtype
);
1929 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
1933 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
1934 if (sym
->ident
== ident
)
1936 } END_FOR_EACH_PTR(sym
);
1940 static void convert_index(struct expression
*e
)
1942 struct expression
*child
= e
->idx_expression
;
1943 unsigned from
= e
->idx_from
;
1944 unsigned to
= e
->idx_to
+ 1;
1946 e
->init_offset
= from
* (e
->ctype
->bit_size
>>3);
1947 e
->init_nr
= to
- from
;
1948 e
->init_expr
= child
;
1951 static void convert_ident(struct expression
*e
)
1953 struct expression
*child
= e
->ident_expression
;
1954 struct symbol
*sym
= e
->field
;
1956 e
->init_offset
= sym
->offset
;
1958 e
->init_expr
= child
;
1961 static void convert_designators(struct expression
*e
)
1964 if (e
->type
== EXPR_INDEX
)
1966 else if (e
->type
== EXPR_IDENTIFIER
)
1974 static void excess(struct expression
*e
, const char *s
)
1976 warning(e
->pos
, "excessive elements in %s initializer", s
);
1980 * implicit designator for the first element
1982 static struct expression
*first_subobject(struct symbol
*ctype
, int class,
1983 struct expression
**v
)
1985 struct expression
*e
= *v
, *new;
1987 if (ctype
->type
== SYM_NODE
)
1988 ctype
= ctype
->ctype
.base_type
;
1990 if (class & TYPE_PTR
) { /* array */
1991 if (!ctype
->bit_size
)
1993 new = alloc_expression(e
->pos
, EXPR_INDEX
);
1994 new->idx_expression
= e
;
1995 new->ctype
= ctype
->ctype
.base_type
;
1997 struct symbol
*field
, *p
;
1998 PREPARE_PTR_LIST(ctype
->symbol_list
, p
);
1999 while (p
&& !p
->ident
&& is_bitfield_type(p
))
2005 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2006 new->ident_expression
= e
;
2007 new->field
= new->ctype
= field
;
2014 * sanity-check explicit designators; return the innermost one or NULL
2015 * in case of error. Assign types.
2017 static struct expression
*check_designators(struct expression
*e
,
2018 struct symbol
*ctype
)
2020 struct expression
*last
= NULL
;
2023 if (ctype
->type
== SYM_NODE
)
2024 ctype
= ctype
->ctype
.base_type
;
2025 if (e
->type
== EXPR_INDEX
) {
2026 struct symbol
*type
;
2027 if (ctype
->type
!= SYM_ARRAY
) {
2028 err
= "array index in non-array";
2031 type
= ctype
->ctype
.base_type
;
2032 if (ctype
->bit_size
>= 0 && type
->bit_size
>= 0) {
2033 unsigned offset
= e
->idx_to
* type
->bit_size
;
2034 if (offset
>= ctype
->bit_size
) {
2035 err
= "index out of bounds in";
2039 e
->ctype
= ctype
= type
;
2042 e
= e
->idx_expression
;
2043 } else if (e
->type
== EXPR_IDENTIFIER
) {
2044 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2045 err
= "field name not in struct or union";
2048 ctype
= find_struct_ident(ctype
, e
->expr_ident
);
2050 err
= "unknown field name in";
2053 e
->field
= e
->ctype
= ctype
;
2055 e
= e
->ident_expression
;
2056 } else if (e
->type
== EXPR_POS
) {
2057 err
= "internal front-end error: EXPR_POS in";
2062 expression_error(e
, "%s initializer", err
);
2067 * choose the next subobject to initialize.
2069 * Get designators for next element, switch old ones to EXPR_POS.
2070 * Return the resulting expression or NULL if we'd run out of subobjects.
2071 * The innermost designator is returned in *v. Designators in old
2072 * are assumed to be already sanity-checked.
2074 static struct expression
*next_designators(struct expression
*old
,
2075 struct symbol
*ctype
,
2076 struct expression
*e
, struct expression
**v
)
2078 struct expression
*new = NULL
;
2082 if (old
->type
== EXPR_INDEX
) {
2083 struct expression
*copy
;
2086 copy
= next_designators(old
->idx_expression
,
2089 n
= old
->idx_to
+ 1;
2090 if (n
* old
->ctype
->bit_size
== ctype
->bit_size
) {
2095 *v
= new = alloc_expression(e
->pos
, EXPR_INDEX
);
2098 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2101 new->idx_from
= new->idx_to
= n
;
2102 new->idx_expression
= copy
;
2103 new->ctype
= old
->ctype
;
2105 } else if (old
->type
== EXPR_IDENTIFIER
) {
2106 struct expression
*copy
;
2107 struct symbol
*field
;
2109 copy
= next_designators(old
->ident_expression
,
2112 field
= old
->field
->next_subobject
;
2118 *v
= new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2121 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2125 new->expr_ident
= field
->ident
;
2126 new->ident_expression
= copy
;
2133 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2134 int class, struct symbol
*ctype
);
2137 * deal with traversing subobjects [6.7.8(17,18,20)]
2139 static void handle_list_initializer(struct expression
*expr
,
2140 int class, struct symbol
*ctype
)
2142 struct expression
*e
, *last
= NULL
, *top
= NULL
, *next
;
2145 FOR_EACH_PTR(expr
->expr_list
, e
) {
2146 struct expression
**v
;
2147 struct symbol
*type
;
2150 if (e
->type
!= EXPR_INDEX
&& e
->type
!= EXPR_IDENTIFIER
) {
2153 last
= first_subobject(ctype
, class, &top
);
2155 last
= next_designators(last
, ctype
, e
, &top
);
2158 excess(e
, class & TYPE_PTR
? "array" :
2160 DELETE_CURRENT_PTR(e
);
2164 warning(e
->pos
, "advancing past deep designator");
2167 REPLACE_CURRENT_PTR(e
, last
);
2169 next
= check_designators(e
, ctype
);
2171 DELETE_CURRENT_PTR(e
);
2175 /* deeper than one designator? */
2177 convert_designators(last
);
2182 lclass
= classify_type(top
->ctype
, &type
);
2183 if (top
->type
== EXPR_INDEX
)
2184 v
= &top
->idx_expression
;
2186 v
= &top
->ident_expression
;
2188 if (handle_simple_initializer(v
, 1, lclass
, top
->ctype
))
2191 if (!(lclass
& TYPE_COMPOUND
)) {
2192 warning(e
->pos
, "bogus scalar initializer");
2193 DELETE_CURRENT_PTR(e
);
2197 next
= first_subobject(type
, lclass
, v
);
2199 warning(e
->pos
, "missing braces around initializer");
2204 DELETE_CURRENT_PTR(e
);
2205 excess(e
, lclass
& TYPE_PTR
? "array" : "struct or union");
2207 } END_FOR_EACH_PTR(e
);
2209 convert_designators(last
);
2210 expr
->ctype
= ctype
;
2213 static int is_string_literal(struct expression
**v
)
2215 struct expression
*e
= *v
;
2216 while (e
->type
== EXPR_PREOP
&& e
->op
== '(')
2218 if (e
->type
!= EXPR_STRING
)
2220 if (e
!= *v
&& Wparen_string
)
2222 "array initialized from parenthesized string constant");
2228 * We want a normal expression, possibly in one layer of braces. Warn
2229 * if the latter happens inside a list (it's legal, but likely to be
2230 * an effect of screwup). In case of anything not legal, we are definitely
2231 * having an effect of screwup, so just fail and let the caller warn.
2233 static struct expression
*handle_scalar(struct expression
*e
, int nested
)
2235 struct expression
*v
= NULL
, *p
;
2239 if (e
->type
!= EXPR_INITIALIZER
)
2242 FOR_EACH_PTR(e
->expr_list
, p
) {
2246 } END_FOR_EACH_PTR(p
);
2250 case EXPR_INITIALIZER
:
2252 case EXPR_IDENTIFIER
:
2258 warning(e
->pos
, "braces around scalar initializer");
2263 * deal with the cases that don't care about subobjects:
2264 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2265 * character array <- string literal, possibly in braces [6.7.8(14)]
2266 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2267 * compound type <- initializer list in braces [6.7.8(16)]
2268 * The last one punts to handle_list_initializer() which, in turn will call
2269 * us for individual elements of the list.
2271 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2272 * the lack of support of wide char stuff in general.
2274 * One note: we need to take care not to evaluate a string literal until
2275 * we know that we *will* handle it right here. Otherwise we would screw
2276 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2277 * { "string", ...} - we need to preserve that string literal recognizable
2278 * until we dig into the inner struct.
2280 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2281 int class, struct symbol
*ctype
)
2283 int is_string
= is_string_type(ctype
);
2284 struct expression
*e
= *ep
, *p
;
2285 struct symbol
*type
;
2288 if (!(class & TYPE_COMPOUND
)) {
2289 e
= handle_scalar(e
, nested
);
2293 type
= evaluate_expression(e
);
2296 compatible_assignment_types(e
, ctype
, ep
, degenerate(e
),
2297 "initializer", '=');
2302 * sublist; either a string, or we dig in; the latter will deal with
2303 * pathologies, so we don't need anything fancy here.
2305 if (e
->type
== EXPR_INITIALIZER
) {
2307 struct expression
*v
= NULL
;
2310 FOR_EACH_PTR(e
->expr_list
, p
) {
2314 } END_FOR_EACH_PTR(p
);
2315 if (count
== 1 && is_string_literal(&v
)) {
2320 handle_list_initializer(e
, class, ctype
);
2325 if (is_string_literal(&e
)) {
2326 /* either we are doing array of char, or we'll have to dig in */
2333 /* struct or union can be initialized by compatible */
2334 if (class != TYPE_COMPOUND
)
2336 type
= evaluate_expression(e
);
2339 if (ctype
->type
== SYM_NODE
)
2340 ctype
= ctype
->ctype
.base_type
;
2341 if (type
->type
== SYM_NODE
)
2342 type
= type
->ctype
.base_type
;
2348 p
= alloc_expression(e
->pos
, EXPR_STRING
);
2350 type
= evaluate_expression(p
);
2351 if (ctype
->bit_size
!= -1 &&
2352 ctype
->bit_size
+ bits_in_char
< type
->bit_size
) {
2354 "too long initializer-string for array of char");
2360 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2362 struct symbol
*type
;
2363 int class = classify_type(ctype
, &type
);
2364 if (!handle_simple_initializer(ep
, 0, class, ctype
))
2365 expression_error(*ep
, "invalid initializer");
2368 static int get_as(struct symbol
*sym
)
2376 mod
= sym
->ctype
.modifiers
;
2377 if (sym
->type
== SYM_NODE
) {
2378 sym
= sym
->ctype
.base_type
;
2379 as
|= sym
->ctype
.as
;
2380 mod
|= sym
->ctype
.modifiers
;
2384 * At least for now, allow casting to a "unsigned long".
2385 * That's how we do things like pointer arithmetic and
2386 * store pointers to registers.
2388 if (sym
== &ulong_ctype
)
2391 if (sym
&& sym
->type
== SYM_PTR
) {
2392 sym
= get_base_type(sym
);
2393 as
|= sym
->ctype
.as
;
2394 mod
|= sym
->ctype
.modifiers
;
2396 if (mod
& MOD_FORCE
)
2401 static void cast_to_as(struct expression
*e
, int as
)
2403 struct expression
*v
= e
->cast_expression
;
2404 struct symbol
*type
= v
->ctype
;
2406 if (!Wcast_to_address_space
)
2409 if (v
->type
!= EXPR_VALUE
|| v
->value
)
2412 /* cast from constant 0 to pointer is OK */
2413 if (is_int_type(type
))
2416 if (type
->type
== SYM_NODE
)
2417 type
= type
->ctype
.base_type
;
2419 if (type
->type
== SYM_PTR
&& type
->ctype
.base_type
== &void_ctype
)
2423 warning(e
->pos
, "cast adds address space to expression (<asn:%d>)", as
);
2426 static struct symbol
*evaluate_cast(struct expression
*expr
)
2428 struct expression
*target
= expr
->cast_expression
;
2429 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
2430 struct symbol
*t1
, *t2
;
2437 expr
->ctype
= ctype
;
2438 expr
->cast_type
= ctype
;
2441 * Special case: a cast can be followed by an
2442 * initializer, in which case we need to pass
2443 * the type value down to that initializer rather
2444 * than trying to evaluate it as an expression
2446 * A more complex case is when the initializer is
2447 * dereferenced as part of a post-fix expression.
2448 * We need to produce an expression that can be dereferenced.
2450 if (target
->type
== EXPR_INITIALIZER
) {
2451 struct symbol
*sym
= expr
->cast_type
;
2452 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2454 sym
->initializer
= expr
->cast_expression
;
2455 evaluate_symbol(sym
);
2457 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2460 expr
->type
= EXPR_PREOP
;
2468 evaluate_expression(target
);
2471 class1
= classify_type(ctype
, &t1
);
2473 * You can always throw a value away by casting to
2474 * "void" - that's an implicit "force". Note that
2475 * the same is _not_ true of "void *".
2477 if (t1
== &void_ctype
)
2480 if (class1
& TYPE_COMPOUND
)
2481 warning(expr
->pos
, "cast to non-scalar");
2485 expression_error(expr
, "cast from unknown type");
2488 class2
= classify_type(t2
, &t2
);
2490 if (class2
& TYPE_COMPOUND
)
2491 warning(expr
->pos
, "cast from non-scalar");
2493 /* allowed cast unfouls */
2494 if (class2
& TYPE_FOULED
)
2495 t2
= t2
->ctype
.base_type
;
2497 if (!(ctype
->ctype
.modifiers
& MOD_FORCE
) && t1
!= t2
) {
2498 if (class1
& TYPE_RESTRICT
)
2499 warning(expr
->pos
, "cast to restricted type");
2500 if (class2
& TYPE_RESTRICT
)
2501 warning(expr
->pos
, "cast from restricted type");
2504 as1
= get_as(ctype
);
2505 as2
= get_as(target
->ctype
);
2506 if (!as1
&& as2
> 0)
2507 warning(expr
->pos
, "cast removes address space of expression");
2508 if (as1
> 0 && as2
> 0 && as1
!= as2
)
2509 warning(expr
->pos
, "cast between address spaces (<asn:%d>-><asn:%d>)", as2
, as1
);
2510 if (as1
> 0 && !as2
)
2511 cast_to_as(expr
, as1
);
2514 * Casts of constant values are special: they
2515 * can be NULL, and thus need to be simplified
2518 if (target
->type
== EXPR_VALUE
)
2519 cast_value(expr
, ctype
, target
, target
->ctype
);
2526 * Evaluate a call expression with a symbol. This
2527 * should expand inline functions, and evaluate
2530 static int evaluate_symbol_call(struct expression
*expr
)
2532 struct expression
*fn
= expr
->fn
;
2533 struct symbol
*ctype
= fn
->ctype
;
2535 if (fn
->type
!= EXPR_PREOP
)
2538 if (ctype
->op
&& ctype
->op
->evaluate
)
2539 return ctype
->op
->evaluate(expr
);
2541 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2543 struct symbol
*curr
= current_fn
;
2544 current_fn
= ctype
->ctype
.base_type
;
2546 ret
= inline_function(expr
, ctype
);
2548 /* restore the old function */
2556 static struct symbol
*evaluate_call(struct expression
*expr
)
2559 struct symbol
*ctype
, *sym
;
2560 struct expression
*fn
= expr
->fn
;
2561 struct expression_list
*arglist
= expr
->args
;
2563 if (!evaluate_expression(fn
))
2565 sym
= ctype
= fn
->ctype
;
2566 if (ctype
->type
== SYM_NODE
)
2567 ctype
= ctype
->ctype
.base_type
;
2568 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
2569 ctype
= get_base_type(ctype
);
2571 examine_fn_arguments(ctype
);
2572 if (sym
->type
== SYM_NODE
&& fn
->type
== EXPR_PREOP
&&
2573 sym
->op
&& sym
->op
->args
) {
2574 if (!sym
->op
->args(expr
))
2577 if (!evaluate_arguments(sym
, ctype
, arglist
))
2579 if (ctype
->type
!= SYM_FN
) {
2580 expression_error(expr
, "not a function %s",
2581 show_ident(sym
->ident
));
2584 args
= expression_list_size(expr
->args
);
2585 fnargs
= symbol_list_size(ctype
->arguments
);
2587 expression_error(expr
,
2588 "not enough arguments for function %s",
2589 show_ident(sym
->ident
));
2590 if (args
> fnargs
&& !ctype
->variadic
)
2591 expression_error(expr
,
2592 "too many arguments for function %s",
2593 show_ident(sym
->ident
));
2595 if (sym
->type
== SYM_NODE
) {
2596 if (evaluate_symbol_call(expr
))
2599 expr
->ctype
= ctype
->ctype
.base_type
;
2603 struct symbol
*evaluate_expression(struct expression
*expr
)
2610 switch (expr
->type
) {
2613 expression_error(expr
, "value expression without a type");
2616 return evaluate_string(expr
);
2618 return evaluate_symbol_expression(expr
);
2620 if (!evaluate_expression(expr
->left
))
2622 if (!evaluate_expression(expr
->right
))
2624 return evaluate_binop(expr
);
2626 return evaluate_logical(expr
);
2628 evaluate_expression(expr
->left
);
2629 if (!evaluate_expression(expr
->right
))
2631 return evaluate_comma(expr
);
2633 if (!evaluate_expression(expr
->left
))
2635 if (!evaluate_expression(expr
->right
))
2637 return evaluate_compare(expr
);
2638 case EXPR_ASSIGNMENT
:
2639 if (!evaluate_expression(expr
->left
))
2641 if (!evaluate_expression(expr
->right
))
2643 return evaluate_assignment(expr
);
2645 if (!evaluate_expression(expr
->unop
))
2647 return evaluate_preop(expr
);
2649 if (!evaluate_expression(expr
->unop
))
2651 return evaluate_postop(expr
);
2653 case EXPR_IMPLIED_CAST
:
2654 return evaluate_cast(expr
);
2656 return evaluate_sizeof(expr
);
2657 case EXPR_PTRSIZEOF
:
2658 return evaluate_ptrsizeof(expr
);
2660 return evaluate_alignof(expr
);
2662 return evaluate_member_dereference(expr
);
2664 return evaluate_call(expr
);
2666 case EXPR_CONDITIONAL
:
2667 return evaluate_conditional_expression(expr
);
2668 case EXPR_STATEMENT
:
2669 expr
->ctype
= evaluate_statement(expr
->statement
);
2673 expr
->ctype
= &ptr_ctype
;
2677 /* Evaluate the type of the symbol .. */
2678 evaluate_symbol(expr
->symbol
);
2679 /* .. but the type of the _expression_ is a "type" */
2680 expr
->ctype
= &type_ctype
;
2683 /* These can not exist as stand-alone expressions */
2684 case EXPR_INITIALIZER
:
2685 case EXPR_IDENTIFIER
:
2688 expression_error(expr
, "internal front-end error: initializer in expression");
2691 expression_error(expr
, "internal front-end error: SLICE re-evaluated");
2697 static void check_duplicates(struct symbol
*sym
)
2700 struct symbol
*next
= sym
;
2702 while ((next
= next
->same_symbol
) != NULL
) {
2703 const char *typediff
;
2704 evaluate_symbol(next
);
2706 typediff
= type_difference(sym
, next
, 0, 0);
2708 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2709 show_ident(sym
->ident
),
2710 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
2715 unsigned long mod
= sym
->ctype
.modifiers
;
2716 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
2718 if (!(mod
& MOD_TOPLEVEL
))
2722 if (sym
->ident
== &main_ident
)
2724 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
2728 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
2730 struct symbol
*base_type
;
2738 sym
= examine_symbol_type(sym
);
2739 base_type
= get_base_type(sym
);
2743 /* Evaluate the initializers */
2744 if (sym
->initializer
)
2745 evaluate_initializer(sym
, &sym
->initializer
);
2747 /* And finally, evaluate the body of the symbol too */
2748 if (base_type
->type
== SYM_FN
) {
2749 struct symbol
*curr
= current_fn
;
2751 current_fn
= base_type
;
2753 examine_fn_arguments(base_type
);
2754 if (!base_type
->stmt
&& base_type
->inline_stmt
)
2756 if (base_type
->stmt
)
2757 evaluate_statement(base_type
->stmt
);
2765 void evaluate_symbol_list(struct symbol_list
*list
)
2769 FOR_EACH_PTR(list
, sym
) {
2770 evaluate_symbol(sym
);
2771 check_duplicates(sym
);
2772 } END_FOR_EACH_PTR(sym
);
2775 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
2777 struct expression
*expr
= stmt
->expression
;
2778 struct symbol
*ctype
, *fntype
;
2780 evaluate_expression(expr
);
2781 ctype
= degenerate(expr
);
2782 fntype
= current_fn
->ctype
.base_type
;
2783 if (!fntype
|| fntype
== &void_ctype
) {
2784 if (expr
&& ctype
!= &void_ctype
)
2785 expression_error(expr
, "return expression in %s function", fntype
?"void":"typeless");
2790 sparse_error(stmt
->pos
, "return with no return value");
2795 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, ctype
, "return expression", '=');
2799 static void evaluate_if_statement(struct statement
*stmt
)
2801 if (!stmt
->if_conditional
)
2804 evaluate_conditional(stmt
->if_conditional
, 0);
2805 evaluate_statement(stmt
->if_true
);
2806 evaluate_statement(stmt
->if_false
);
2809 static void evaluate_iterator(struct statement
*stmt
)
2811 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
2812 evaluate_conditional(stmt
->iterator_post_condition
,1);
2813 evaluate_statement(stmt
->iterator_pre_statement
);
2814 evaluate_statement(stmt
->iterator_statement
);
2815 evaluate_statement(stmt
->iterator_post_statement
);
2818 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
2820 switch (*constraint
) {
2821 case '=': /* Assignment */
2822 case '+': /* Update */
2825 expression_error(expr
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
2829 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
2831 switch (*constraint
) {
2832 case '=': /* Assignment */
2833 case '+': /* Update */
2834 expression_error(expr
, "input constraint with assignment (\"%s\")", constraint
);
2838 static void evaluate_asm_statement(struct statement
*stmt
)
2840 struct expression
*expr
;
2843 expr
= stmt
->asm_string
;
2844 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2845 sparse_error(stmt
->pos
, "need constant string for inline asm");
2850 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
2851 struct ident
*ident
;
2854 case 0: /* Identifier */
2856 ident
= (struct ident
*)expr
;
2859 case 1: /* Constraint */
2861 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2862 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
2863 *THIS_ADDRESS(expr
) = NULL
;
2866 verify_output_constraint(expr
, expr
->string
->data
);
2869 case 2: /* Expression */
2871 if (!evaluate_expression(expr
))
2873 if (!lvalue_expression(expr
))
2874 warning(expr
->pos
, "asm output is not an lvalue");
2875 evaluate_assign_to(expr
, expr
->ctype
);
2878 } END_FOR_EACH_PTR(expr
);
2881 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
2882 struct ident
*ident
;
2885 case 0: /* Identifier */
2887 ident
= (struct ident
*)expr
;
2890 case 1: /* Constraint */
2892 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2893 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
2894 *THIS_ADDRESS(expr
) = NULL
;
2897 verify_input_constraint(expr
, expr
->string
->data
);
2900 case 2: /* Expression */
2902 if (!evaluate_expression(expr
))
2906 } END_FOR_EACH_PTR(expr
);
2908 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
2910 sparse_error(stmt
->pos
, "bad asm output");
2913 if (expr
->type
== EXPR_STRING
)
2915 expression_error(expr
, "asm clobber is not a string");
2916 } END_FOR_EACH_PTR(expr
);
2919 static void evaluate_case_statement(struct statement
*stmt
)
2921 evaluate_expression(stmt
->case_expression
);
2922 evaluate_expression(stmt
->case_to
);
2923 evaluate_statement(stmt
->case_statement
);
2926 static void check_case_type(struct expression
*switch_expr
,
2927 struct expression
*case_expr
,
2928 struct expression
**enumcase
)
2930 struct symbol
*switch_type
, *case_type
;
2936 switch_type
= switch_expr
->ctype
;
2937 case_type
= evaluate_expression(case_expr
);
2939 if (!switch_type
|| !case_type
)
2943 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
2944 else if (is_enum_type(case_type
))
2945 *enumcase
= case_expr
;
2948 sclass
= classify_type(switch_type
, &switch_type
);
2949 cclass
= classify_type(case_type
, &case_type
);
2951 /* both should be arithmetic */
2952 if (!(sclass
& cclass
& TYPE_NUM
))
2955 /* neither should be floating */
2956 if ((sclass
| cclass
) & TYPE_FLOAT
)
2959 /* if neither is restricted, we are OK */
2960 if (!((sclass
| cclass
) & TYPE_RESTRICT
))
2963 if (!restricted_binop_type(SPECIAL_EQUAL
, case_expr
, switch_expr
,
2964 cclass
, sclass
, case_type
, switch_type
))
2965 warning(case_expr
->pos
, "restricted degrades to integer");
2970 expression_error(case_expr
, "incompatible types for 'case' statement");
2973 static void evaluate_switch_statement(struct statement
*stmt
)
2976 struct expression
*enumcase
= NULL
;
2977 struct expression
**enumcase_holder
= &enumcase
;
2978 struct expression
*sel
= stmt
->switch_expression
;
2980 evaluate_expression(sel
);
2981 evaluate_statement(stmt
->switch_statement
);
2984 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
2985 enumcase_holder
= NULL
; /* Only check cases against switch */
2987 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
2988 struct statement
*case_stmt
= sym
->stmt
;
2989 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
2990 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
2991 } END_FOR_EACH_PTR(sym
);
2994 struct symbol
*evaluate_statement(struct statement
*stmt
)
2999 switch (stmt
->type
) {
3000 case STMT_DECLARATION
: {
3002 FOR_EACH_PTR(stmt
->declaration
, s
) {
3004 } END_FOR_EACH_PTR(s
);
3009 return evaluate_return_expression(stmt
);
3011 case STMT_EXPRESSION
:
3012 if (!evaluate_expression(stmt
->expression
))
3014 return degenerate(stmt
->expression
);
3016 case STMT_COMPOUND
: {
3017 struct statement
*s
;
3018 struct symbol
*type
= NULL
;
3020 /* Evaluate the return symbol in the compound statement */
3021 evaluate_symbol(stmt
->ret
);
3024 * Then, evaluate each statement, making the type of the
3025 * compound statement be the type of the last statement
3027 type
= evaluate_statement(stmt
->args
);
3028 FOR_EACH_PTR(stmt
->stmts
, s
) {
3029 type
= evaluate_statement(s
);
3030 } END_FOR_EACH_PTR(s
);
3036 evaluate_if_statement(stmt
);
3039 evaluate_iterator(stmt
);
3042 evaluate_switch_statement(stmt
);
3045 evaluate_case_statement(stmt
);
3048 return evaluate_statement(stmt
->label_statement
);
3050 evaluate_expression(stmt
->goto_expression
);
3055 evaluate_asm_statement(stmt
);
3058 evaluate_expression(stmt
->expression
);
3061 evaluate_expression(stmt
->range_expression
);
3062 evaluate_expression(stmt
->range_low
);
3063 evaluate_expression(stmt
->range_high
);