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
))
527 ctype
= restricted_binop_type(op
, left
, right
,
528 lclass
, rclass
, ltype
, rtype
);
532 if (lclass
& TYPE_RESTRICT
) {
533 warning(left
->pos
, "restricted degrades to integer");
534 if (lclass
& TYPE_FOULED
)
535 ltype
= ltype
->ctype
.base_type
;
536 ltype
= ltype
->ctype
.base_type
;
538 if (rclass
& TYPE_RESTRICT
) {
539 warning(right
->pos
, "restricted degrades to integer");
540 if (rclass
& TYPE_FOULED
)
541 rtype
= rtype
->ctype
.base_type
;
542 rtype
= rtype
->ctype
.base_type
;
547 static struct symbol
*evaluate_arith(struct expression
*expr
, int float_ok
)
549 struct symbol
*ltype
, *rtype
;
550 int lclass
= classify_type(expr
->left
->ctype
, <ype
);
551 int rclass
= classify_type(expr
->right
->ctype
, &rtype
);
552 struct symbol
*ctype
;
554 if (!(lclass
& rclass
& TYPE_NUM
))
557 if (!float_ok
&& (lclass
| rclass
) & TYPE_FLOAT
)
560 ctype
= usual_conversions(expr
->op
, expr
->left
, expr
->right
,
561 lclass
, rclass
, ltype
, rtype
);
562 expr
->left
= cast_to(expr
->left
, ctype
);
563 expr
->right
= cast_to(expr
->right
, ctype
);
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
*itype
;
607 if (!want_int(&i
, &itype
))
608 return bad_expr_type(expr
);
610 examine_symbol_type(ctype
);
612 if (!ctype
->ctype
.base_type
) {
613 expression_error(expr
, "missing type information");
617 /* Get the size of whatever the pointer points to */
618 bit_size
= ptr_object_size(ctype
);
620 if (bit_size
> bits_in_char
) {
621 int multiply
= bit_size
>> 3;
622 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
624 if (i
->type
== EXPR_VALUE
) {
625 val
->value
= i
->value
* multiply
;
626 val
->ctype
= size_t_ctype
;
629 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
631 val
->ctype
= size_t_ctype
;
632 val
->value
= bit_size
>> 3;
635 mul
->ctype
= size_t_ctype
;
647 static struct symbol
*evaluate_add(struct expression
*expr
)
649 struct expression
*left
= expr
->left
, *right
= expr
->right
;
650 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
652 if (is_ptr_type(ltype
))
653 return evaluate_ptr_add(expr
, degenerate(left
), &expr
->right
);
655 if (is_ptr_type(rtype
))
656 return evaluate_ptr_add(expr
, degenerate(right
), &expr
->left
);
658 return evaluate_arith(expr
, 1);
661 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
662 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
665 unsigned long mod1
, mod2
, diff
;
666 unsigned long as1
, as2
;
668 struct symbol
*base1
, *base2
;
670 if (target
== source
)
672 if (!target
|| !source
)
673 return "different types";
675 * Peel of per-node information.
676 * FIXME! Check alignment and context too here!
678 mod1
= target
->ctype
.modifiers
;
679 as1
= target
->ctype
.as
;
680 mod2
= source
->ctype
.modifiers
;
681 as2
= source
->ctype
.as
;
682 if (target
->type
== SYM_NODE
) {
683 target
= target
->ctype
.base_type
;
686 if (target
->type
== SYM_PTR
) {
690 mod1
|= target
->ctype
.modifiers
;
691 as1
|= target
->ctype
.as
;
693 if (source
->type
== SYM_NODE
) {
694 source
= source
->ctype
.base_type
;
697 if (source
->type
== SYM_PTR
) {
701 mod2
|= source
->ctype
.modifiers
;
702 as2
|= source
->ctype
.as
;
704 if (target
->type
== SYM_ENUM
) {
705 target
= target
->ctype
.base_type
;
709 if (source
->type
== SYM_ENUM
) {
710 source
= source
->ctype
.base_type
;
715 if (target
== source
)
717 if (!target
|| !source
)
718 return "different types";
720 type1
= target
->type
;
721 base1
= target
->ctype
.base_type
;
723 type2
= source
->type
;
724 base2
= source
->ctype
.base_type
;
727 * Pointers to functions compare as the function itself
729 if (type1
== SYM_PTR
&& base1
) {
730 base1
= examine_symbol_type(base1
);
731 switch (base1
->type
) {
735 base1
= base1
->ctype
.base_type
;
740 if (type2
== SYM_PTR
&& base2
) {
741 base2
= examine_symbol_type(base2
);
742 switch (base2
->type
) {
746 base2
= base2
->ctype
.base_type
;
752 /* Arrays degenerate to pointers for type comparisons */
753 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
754 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
756 if (type1
!= type2
|| type1
== SYM_RESTRICT
)
757 return "different base types";
759 /* Must be same address space to be comparable */
760 if (Waddress_space
&& as1
!= as2
)
761 return "different address spaces";
763 /* Ignore differences in storage types or addressability */
764 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
765 diff
&= (mod1
& ~target_mod_ignore
) | (mod2
& ~source_mod_ignore
);
768 return "different type sizes";
769 if (diff
& ~MOD_SIGNEDNESS
)
770 return "different modifiers";
772 /* Differs in signedness only.. */
775 * Warn if both are explicitly signed ("unsigned" is obviously
776 * always explicit, and since we know one of them has to be
777 * unsigned, we check if the signed one was explicit).
779 if ((mod1
| mod2
) & MOD_EXPLICITLY_SIGNED
)
780 return "different explicit signedness";
783 * "char" matches both "unsigned char" and "signed char",
784 * so if the explicit test didn't trigger, then we should
785 * not warn about a char.
787 if (!(mod1
& MOD_CHAR
))
788 return "different signedness";
792 if (type1
== SYM_FN
) {
794 struct symbol
*arg1
, *arg2
;
795 if (base1
->variadic
!= base2
->variadic
)
796 return "incompatible variadic arguments";
797 PREPARE_PTR_LIST(target
->arguments
, arg1
);
798 PREPARE_PTR_LIST(source
->arguments
, arg2
);
802 diffstr
= type_difference(arg1
, arg2
, 0, 0);
804 static char argdiff
[80];
805 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diffstr
);
814 FINISH_PTR_LIST(arg2
);
815 FINISH_PTR_LIST(arg1
);
824 static int is_null_ptr(struct expression
*expr
)
826 if (expr
->type
!= EXPR_VALUE
|| expr
->value
)
828 if (Wnon_pointer_null
&& !is_ptr_type(expr
->ctype
))
829 warning(expr
->pos
, "Using plain integer as NULL pointer");
834 * Ignore differences in "volatile" and "const"ness when
835 * subtracting pointers
837 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
839 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
**rp
)
841 const char *typediff
;
842 struct symbol
*ctype
;
843 struct symbol
*ltype
, *rtype
;
844 struct expression
*r
= *rp
;
846 ltype
= degenerate(l
);
847 rtype
= degenerate(r
);
850 * If it is an integer subtract: the ptr add case will do the
853 if (!is_ptr_type(rtype
))
854 return evaluate_ptr_add(expr
, degenerate(l
), rp
);
857 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
859 expression_error(expr
, "subtraction of different types can't work (%s)", typediff
);
860 examine_symbol_type(ctype
);
862 /* Figure out the base type we point to */
863 if (ctype
->type
== SYM_NODE
)
864 ctype
= ctype
->ctype
.base_type
;
865 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
866 expression_error(expr
, "subtraction of functions? Share your drugs");
869 ctype
= get_base_type(ctype
);
871 expr
->ctype
= ssize_t_ctype
;
872 if (ctype
->bit_size
> bits_in_char
) {
873 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
874 struct expression
*div
= expr
;
875 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
876 unsigned long value
= ctype
->bit_size
>> 3;
878 val
->ctype
= size_t_ctype
;
881 if (value
& (value
-1)) {
882 if (Wptr_subtraction_blows
)
883 warning(expr
->pos
, "potentially expensive pointer subtraction");
887 sub
->ctype
= ssize_t_ctype
;
896 return ssize_t_ctype
;
899 static struct symbol
*evaluate_sub(struct expression
*expr
)
901 struct expression
*left
= expr
->left
;
902 struct symbol
*ltype
= left
->ctype
;
904 if (is_ptr_type(ltype
))
905 return evaluate_ptr_sub(expr
, left
, &expr
->right
);
907 return evaluate_arith(expr
, 1);
910 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
912 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
914 struct symbol
*ctype
;
919 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
920 warning(expr
->pos
, "assignment expression in conditional");
922 ctype
= evaluate_expression(expr
);
924 if (is_safe_type(ctype
))
925 warning(expr
->pos
, "testing a 'safe expression'");
931 static struct symbol
*evaluate_logical(struct expression
*expr
)
933 if (!evaluate_conditional(expr
->left
, 0))
935 if (!evaluate_conditional(expr
->right
, 0))
938 expr
->ctype
= &bool_ctype
;
942 static struct symbol
*evaluate_shift(struct expression
*expr
)
944 struct symbol
*ltype
, *rtype
;
946 if (want_int(&expr
->left
, <ype
) && want_int(&expr
->right
, &rtype
)) {
947 struct symbol
*ctype
= integer_promotion(ltype
);
948 expr
->left
= cast_to(expr
->left
, ctype
);
950 ctype
= integer_promotion(rtype
);
951 expr
->right
= cast_to(expr
->right
, ctype
);
954 return bad_expr_type(expr
);
957 static struct symbol
*evaluate_binop(struct expression
*expr
)
960 // addition can take ptr+int, fp and int
962 return evaluate_add(expr
);
964 // subtraction can take ptr-ptr, fp and int
966 return evaluate_sub(expr
);
968 // Arithmetic operations can take fp and int
970 return evaluate_arith(expr
, 1);
972 // shifts do integer promotions, but that's it.
973 case SPECIAL_LEFTSHIFT
: case SPECIAL_RIGHTSHIFT
:
974 return evaluate_shift(expr
);
976 // The rest are integer operations
977 // '%', '&', '^', '|'
979 return evaluate_arith(expr
, 0);
983 static struct symbol
*evaluate_comma(struct expression
*expr
)
985 expr
->ctype
= expr
->right
->ctype
;
989 static int modify_for_unsigned(int op
)
992 op
= SPECIAL_UNSIGNED_LT
;
994 op
= SPECIAL_UNSIGNED_GT
;
995 else if (op
== SPECIAL_LTE
)
996 op
= SPECIAL_UNSIGNED_LTE
;
997 else if (op
== SPECIAL_GTE
)
998 op
= SPECIAL_UNSIGNED_GTE
;
1002 static struct symbol
*evaluate_compare(struct expression
*expr
)
1004 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1005 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
1006 struct symbol
*ctype
;
1009 if (is_type_type(ltype
) && is_type_type(rtype
))
1012 if (is_safe_type(ltype
) || is_safe_type(rtype
))
1013 warning(expr
->pos
, "testing a 'safe expression'");
1015 /* Pointer types? */
1016 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
1017 // FIXME! Check the types for compatibility
1018 expr
->op
= modify_for_unsigned(expr
->op
);
1022 ctype
= evaluate_arith(expr
, 1);
1024 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
1025 expr
->op
= modify_for_unsigned(expr
->op
);
1028 expr
->ctype
= &bool_ctype
;
1033 * FIXME!! This should do casts, array degeneration etc..
1035 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
1037 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
1039 if (ltype
->type
== SYM_NODE
)
1040 ltype
= ltype
->ctype
.base_type
;
1042 if (rtype
->type
== SYM_NODE
)
1043 rtype
= rtype
->ctype
.base_type
;
1045 if (ltype
->type
== SYM_PTR
) {
1046 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
1050 if (rtype
->type
== SYM_PTR
) {
1051 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
1058 * NOTE! The degenerate case of "x ? : y", where we don't
1059 * have a true case, this will possibly promote "x" to the
1060 * same type as "y", and thus _change_ the conditional
1061 * test in the expression. But since promotion is "safe"
1062 * for testing, that's OK.
1064 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
1066 struct expression
**true;
1067 struct symbol
*ctype
, *ltype
, *rtype
;
1069 const char * typediff
;
1071 if (!evaluate_conditional(expr
->conditional
, 0))
1073 if (!evaluate_expression(expr
->cond_false
))
1076 ctype
= degenerate(expr
->conditional
);
1077 rtype
= degenerate(expr
->cond_false
);
1079 true = &expr
->conditional
;
1081 if (expr
->cond_true
) {
1082 if (!evaluate_expression(expr
->cond_true
))
1084 ltype
= degenerate(expr
->cond_true
);
1085 true = &expr
->cond_true
;
1088 lclass
= classify_type(ltype
, <ype
);
1089 rclass
= classify_type(rtype
, &rtype
);
1090 if (lclass
& rclass
& TYPE_NUM
) {
1091 ctype
= usual_conversions('?', *true, expr
->cond_false
,
1092 lclass
, rclass
, ltype
, rtype
);
1093 *true = cast_to(*true, ctype
);
1094 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1097 ctype
= compatible_ptr_type(*true, expr
->cond_false
);
1101 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
1104 expression_error(expr
, "incompatible types in conditional expression (%s)", typediff
);
1108 expr
->ctype
= ctype
;
1112 /* FP assignments can not do modulo or bit operations */
1113 static int compatible_float_op(int op
)
1115 return op
== SPECIAL_ADD_ASSIGN
||
1116 op
== SPECIAL_SUB_ASSIGN
||
1117 op
== SPECIAL_MUL_ASSIGN
||
1118 op
== SPECIAL_DIV_ASSIGN
;
1121 static int evaluate_assign_op(struct expression
*expr
, struct symbol
*target
,
1122 struct expression
**rp
, struct symbol
*source
, int op
)
1124 struct symbol
*t
, *s
;
1125 int tclass
= classify_type(target
, &t
);
1126 int sclass
= classify_type(source
, &s
);
1128 if (tclass
& sclass
& TYPE_NUM
) {
1129 if (tclass
& TYPE_FLOAT
&& !compatible_float_op(op
)) {
1130 expression_error(expr
, "invalid assignment");
1133 if (tclass
& TYPE_RESTRICT
) {
1134 if (!restricted_binop(op
, target
)) {
1135 expression_error(expr
, "bad restricted assignment");
1138 /* allowed assignments unfoul */
1139 if (sclass
& TYPE_FOULED
&& s
->ctype
.base_type
== t
)
1141 if (!restricted_value(*rp
, target
))
1143 } else if (!(sclass
& TYPE_RESTRICT
))
1145 /* source and target would better be identical restricted */
1148 warning(expr
->pos
, "invalid restricted assignment");
1149 *rp
= cast_to(*rp
, target
);
1151 } else if (tclass
& TYPE_PTR
) {
1152 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1153 evaluate_ptr_add(expr
, target
, rp
);
1156 expression_error(expr
, "invalid pointer assignment");
1159 expression_error(expr
, "invalid assignment");
1164 *rp
= cast_to(*rp
, target
);
1168 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1169 struct expression
**rp
, struct symbol
*source
, const char *where
)
1171 const char *typediff
;
1172 struct symbol
*t
, *s
;
1174 int tclass
= classify_type(target
, &t
);
1175 int sclass
= classify_type(source
, &s
);
1177 if (tclass
& sclass
& TYPE_NUM
) {
1178 if (tclass
& TYPE_RESTRICT
) {
1179 /* allowed assignments unfoul */
1180 if (sclass
& TYPE_FOULED
&& s
->ctype
.base_type
== t
)
1182 if (!restricted_value(*rp
, target
))
1184 } else if (!(sclass
& TYPE_RESTRICT
))
1188 /* It's OK if the target is more volatile or const than the source */
1189 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
1193 /* Pointer destination? */
1194 if (tclass
& TYPE_PTR
) {
1195 struct expression
*right
= *rp
;
1198 // NULL pointer is always OK
1199 if (is_null_ptr(right
))
1202 /* "void *" matches anything as long as the address space is OK */
1203 target_as
= t
->ctype
.as
| target
->ctype
.as
;
1204 source_as
= s
->ctype
.as
| source
->ctype
.as
;
1205 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
1206 s
= get_base_type(s
);
1207 t
= get_base_type(t
);
1208 if (s
== &void_ctype
|| t
== &void_ctype
)
1213 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1214 info(expr
->pos
, " expected %s", show_typename(target
));
1215 info(expr
->pos
, " got %s", show_typename(source
));
1216 *rp
= cast_to(*rp
, target
);
1219 *rp
= cast_to(*rp
, target
);
1223 static void mark_assigned(struct expression
*expr
)
1229 switch (expr
->type
) {
1234 if (sym
->type
!= SYM_NODE
)
1236 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1240 mark_assigned(expr
->left
);
1241 mark_assigned(expr
->right
);
1244 mark_assigned(expr
->cast_expression
);
1247 mark_assigned(expr
->base
);
1255 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1257 if (type
->ctype
.modifiers
& MOD_CONST
)
1258 expression_error(left
, "assignment to const expression");
1260 /* We know left is an lvalue, so it's a "preop-*" */
1261 mark_assigned(left
->unop
);
1264 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1266 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1267 struct expression
*where
= expr
;
1268 struct symbol
*ltype
, *rtype
;
1270 if (!lvalue_expression(left
)) {
1271 expression_error(expr
, "not an lvalue");
1275 ltype
= left
->ctype
;
1277 rtype
= degenerate(right
);
1279 if (expr
->op
!= '=') {
1280 if (!evaluate_assign_op(where
, ltype
, &where
->right
, rtype
, expr
->op
))
1283 if (!compatible_assignment_types(where
, ltype
, &where
->right
, rtype
, "assignment"))
1287 evaluate_assign_to(left
, ltype
);
1289 expr
->ctype
= ltype
;
1293 static void examine_fn_arguments(struct symbol
*fn
)
1297 FOR_EACH_PTR(fn
->arguments
, s
) {
1298 struct symbol
*arg
= evaluate_symbol(s
);
1299 /* Array/function arguments silently degenerate into pointers */
1305 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1306 if (arg
->type
== SYM_ARRAY
)
1307 ptr
->ctype
= arg
->ctype
;
1309 ptr
->ctype
.base_type
= arg
;
1310 ptr
->ctype
.as
|= s
->ctype
.as
;
1311 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
& MOD_PTRINHERIT
;
1313 s
->ctype
.base_type
= ptr
;
1315 s
->ctype
.modifiers
&= ~MOD_PTRINHERIT
;
1318 examine_symbol_type(s
);
1325 } END_FOR_EACH_PTR(s
);
1328 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1330 /* Take the modifiers of the pointer, and apply them to the member */
1331 mod
|= sym
->ctype
.modifiers
;
1332 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1333 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1335 newsym
->ctype
.as
= as
;
1336 newsym
->ctype
.modifiers
= mod
;
1342 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1344 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1345 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1347 node
->ctype
.base_type
= ptr
;
1348 ptr
->bit_size
= bits_in_pointer
;
1349 ptr
->ctype
.alignment
= pointer_alignment
;
1351 node
->bit_size
= bits_in_pointer
;
1352 node
->ctype
.alignment
= pointer_alignment
;
1355 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1356 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1357 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1359 if (sym
->type
== SYM_NODE
) {
1360 ptr
->ctype
.as
|= sym
->ctype
.as
;
1361 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1362 sym
= sym
->ctype
.base_type
;
1364 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1365 ptr
->ctype
.as
|= sym
->ctype
.as
;
1366 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1367 sym
= sym
->ctype
.base_type
;
1369 ptr
->ctype
.base_type
= sym
;
1374 /* Arrays degenerate into pointers on pointer arithmetic */
1375 static struct symbol
*degenerate(struct expression
*expr
)
1377 struct symbol
*ctype
, *base
;
1381 ctype
= expr
->ctype
;
1384 base
= examine_symbol_type(ctype
);
1385 if (ctype
->type
== SYM_NODE
)
1386 base
= ctype
->ctype
.base_type
;
1388 * Arrays degenerate into pointers to the entries, while
1389 * functions degenerate into pointers to themselves.
1390 * If array was part of non-lvalue compound, we create a copy
1391 * of that compound first and then act as if we were dealing with
1392 * the corresponding field in there.
1394 switch (base
->type
) {
1396 if (expr
->type
== EXPR_SLICE
) {
1397 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1398 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1400 a
->ctype
.base_type
= expr
->base
->ctype
;
1401 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1402 a
->array_size
= expr
->base
->ctype
->array_size
;
1404 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1406 e0
->ctype
= &lazy_ptr_ctype
;
1408 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1411 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1413 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1415 e2
->right
= expr
->base
;
1417 e2
->ctype
= expr
->base
->ctype
;
1419 if (expr
->r_bitpos
) {
1420 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1423 e3
->right
= alloc_const_expression(expr
->pos
,
1424 expr
->r_bitpos
>> 3);
1425 e3
->ctype
= &lazy_ptr_ctype
;
1430 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1433 e4
->ctype
= &lazy_ptr_ctype
;
1436 expr
->type
= EXPR_PREOP
;
1440 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1441 expression_error(expr
, "strange non-value function or array");
1444 *expr
= *expr
->unop
;
1445 ctype
= create_pointer(expr
, ctype
, 1);
1446 expr
->ctype
= ctype
;
1453 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1455 struct expression
*op
= expr
->unop
;
1456 struct symbol
*ctype
;
1458 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1459 expression_error(expr
, "not addressable");
1465 if (expr
->type
== EXPR_SYMBOL
) {
1466 struct symbol
*sym
= expr
->symbol
;
1467 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1471 * symbol expression evaluation is lazy about the type
1472 * of the sub-expression, so we may have to generate
1473 * the type here if so..
1475 if (expr
->ctype
== &lazy_ptr_ctype
) {
1476 ctype
= create_pointer(expr
, ctype
, 0);
1477 expr
->ctype
= ctype
;
1483 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1485 struct expression
*op
= expr
->unop
;
1486 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1488 /* Simplify: *&(expr) => (expr) */
1489 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1494 /* Dereferencing a node drops all the node information. */
1495 if (ctype
->type
== SYM_NODE
)
1496 ctype
= ctype
->ctype
.base_type
;
1498 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1499 target
= ctype
->ctype
.base_type
;
1501 switch (ctype
->type
) {
1503 expression_error(expr
, "cannot dereference this type");
1506 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1507 merge_type(node
, ctype
);
1511 if (!lvalue_expression(op
)) {
1512 expression_error(op
, "non-lvalue array??");
1516 /* Do the implied "addressof" on the array */
1520 * When an array is dereferenced, we need to pick
1521 * up the attributes of the original node too..
1523 merge_type(node
, op
->ctype
);
1524 merge_type(node
, ctype
);
1528 node
->bit_size
= target
->bit_size
;
1529 node
->array_size
= target
->array_size
;
1536 * Unary post-ops: x++ and x--
1538 static struct symbol
*evaluate_postop(struct expression
*expr
)
1540 struct expression
*op
= expr
->unop
;
1541 struct symbol
*ctype
= op
->ctype
;
1543 if (!lvalue_expression(expr
->unop
)) {
1544 expression_error(expr
, "need lvalue expression for ++/--");
1547 if (is_restricted_type(ctype
) && restricted_unop(expr
->op
, &ctype
)) {
1548 expression_error(expr
, "bad operation on restricted");
1550 } else if (is_fouled_type(ctype
) && restricted_unop(expr
->op
, &ctype
)) {
1551 expression_error(expr
, "bad operation on restricted");
1555 evaluate_assign_to(op
, ctype
);
1557 expr
->ctype
= ctype
;
1559 if (is_ptr_type(ctype
))
1560 expr
->op_value
= ptr_object_size(ctype
) >> 3;
1565 static struct symbol
*evaluate_sign(struct expression
*expr
)
1567 struct symbol
*ctype
= expr
->unop
->ctype
;
1568 if (is_int_type(ctype
)) {
1569 struct symbol
*rtype
= rtype
= integer_promotion(ctype
);
1570 expr
->unop
= cast_to(expr
->unop
, rtype
);
1572 } else if (is_float_type(ctype
) && expr
->op
!= '~') {
1573 /* no conversions needed */
1574 } else if (is_restricted_type(ctype
) && !restricted_unop(expr
->op
, &ctype
)) {
1575 /* no conversions needed */
1576 } else if (is_fouled_type(ctype
) && !restricted_unop(expr
->op
, &ctype
)) {
1577 /* no conversions needed */
1579 return bad_expr_type(expr
);
1581 if (expr
->op
== '+')
1582 *expr
= *expr
->unop
;
1583 expr
->ctype
= ctype
;
1587 static struct symbol
*evaluate_preop(struct expression
*expr
)
1589 struct symbol
*ctype
= expr
->unop
->ctype
;
1593 *expr
= *expr
->unop
;
1599 return evaluate_sign(expr
);
1602 return evaluate_dereference(expr
);
1605 return evaluate_addressof(expr
);
1607 case SPECIAL_INCREMENT
:
1608 case SPECIAL_DECREMENT
:
1610 * From a type evaluation standpoint the preops are
1611 * the same as the postops
1613 return evaluate_postop(expr
);
1616 if (is_safe_type(ctype
))
1617 warning(expr
->pos
, "testing a 'safe expression'");
1618 if (is_float_type(ctype
)) {
1619 struct expression
*arg
= expr
->unop
;
1620 expr
->type
= EXPR_BINOP
;
1621 expr
->op
= SPECIAL_EQUAL
;
1623 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1624 expr
->right
->ctype
= ctype
;
1625 expr
->right
->fvalue
= 0;
1626 } else if (is_fouled_type(ctype
)) {
1627 warning(expr
->pos
, "restricted degrades to integer");
1629 ctype
= &bool_ctype
;
1635 expr
->ctype
= ctype
;
1639 static struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1641 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1642 struct ptr_list
*list
= head
;
1648 for (i
= 0; i
< list
->nr
; i
++) {
1649 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1651 if (sym
->ident
!= ident
)
1653 *offset
= sym
->offset
;
1656 struct symbol
*ctype
= sym
->ctype
.base_type
;
1660 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1662 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1665 *offset
+= sym
->offset
;
1669 } while ((list
= list
->next
) != head
);
1673 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1675 struct expression
*add
;
1678 * Create a new add-expression
1680 * NOTE! Even if we just add zero, we need a new node
1681 * for the member pointer, since it has a different
1682 * type than the original pointer. We could make that
1683 * be just a cast, but the fact is, a node is a node,
1684 * so we might as well just do the "add zero" here.
1686 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1689 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1690 add
->right
->ctype
= &int_ctype
;
1691 add
->right
->value
= offset
;
1694 * The ctype of the pointer will be lazily evaluated if
1695 * we ever take the address of this member dereference..
1697 add
->ctype
= &lazy_ptr_ctype
;
1701 /* structure/union dereference */
1702 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1705 struct symbol
*ctype
, *member
;
1706 struct expression
*deref
= expr
->deref
, *add
;
1707 struct ident
*ident
= expr
->member
;
1711 if (!evaluate_expression(deref
))
1714 expression_error(expr
, "bad member name");
1718 ctype
= deref
->ctype
;
1719 address_space
= ctype
->ctype
.as
;
1720 mod
= ctype
->ctype
.modifiers
;
1721 if (ctype
->type
== SYM_NODE
) {
1722 ctype
= ctype
->ctype
.base_type
;
1723 address_space
|= ctype
->ctype
.as
;
1724 mod
|= ctype
->ctype
.modifiers
;
1726 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1727 expression_error(expr
, "expected structure or union");
1730 examine_symbol_type(ctype
);
1732 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1734 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1735 const char *name
= "<unnamed>";
1738 name
= ctype
->ident
->name
;
1739 namelen
= ctype
->ident
->len
;
1741 if (ctype
->symbol_list
)
1742 expression_error(expr
, "no member '%s' in %s %.*s",
1743 show_ident(ident
), type
, namelen
, name
);
1745 expression_error(expr
, "using member '%s' in "
1746 "incomplete %s %.*s", show_ident(ident
),
1747 type
, namelen
, name
);
1752 * The member needs to take on the address space and modifiers of
1753 * the "parent" type.
1755 member
= convert_to_as_mod(member
, address_space
, mod
);
1756 ctype
= get_base_type(member
);
1758 if (!lvalue_expression(deref
)) {
1759 if (deref
->type
!= EXPR_SLICE
) {
1763 expr
->base
= deref
->base
;
1764 expr
->r_bitpos
= deref
->r_bitpos
;
1766 expr
->r_bitpos
+= offset
<< 3;
1767 expr
->type
= EXPR_SLICE
;
1768 expr
->r_nrbits
= member
->bit_size
;
1769 expr
->r_bitpos
+= member
->bit_offset
;
1770 expr
->ctype
= member
;
1774 deref
= deref
->unop
;
1775 expr
->deref
= deref
;
1777 add
= evaluate_offset(deref
, offset
);
1778 expr
->type
= EXPR_PREOP
;
1782 expr
->ctype
= member
;
1786 static int is_promoted(struct expression
*expr
)
1789 switch (expr
->type
) {
1792 case EXPR_CONDITIONAL
:
1816 static struct symbol
*evaluate_cast(struct expression
*);
1818 static struct symbol
*evaluate_type_information(struct expression
*expr
)
1820 struct symbol
*sym
= expr
->cast_type
;
1822 sym
= evaluate_expression(expr
->cast_expression
);
1826 * Expressions of restricted types will possibly get
1827 * promoted - check that here
1829 if (is_restricted_type(sym
)) {
1830 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
1832 } else if (is_fouled_type(sym
)) {
1836 examine_symbol_type(sym
);
1837 if (is_bitfield_type(sym
)) {
1838 expression_error(expr
, "trying to examine bitfield type");
1844 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1846 struct symbol
*type
;
1849 type
= evaluate_type_information(expr
);
1853 size
= type
->bit_size
;
1854 if ((size
< 0) || (size
& 7))
1855 expression_error(expr
, "cannot size expression");
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_ptrsizeof(struct expression
*expr
)
1864 struct symbol
*type
;
1867 type
= evaluate_type_information(expr
);
1871 if (type
->type
== SYM_NODE
)
1872 type
= type
->ctype
.base_type
;
1875 switch (type
->type
) {
1879 type
= get_base_type(type
);
1883 expression_error(expr
, "expected pointer expression");
1886 size
= type
->bit_size
;
1889 expr
->type
= EXPR_VALUE
;
1890 expr
->value
= size
>> 3;
1891 expr
->ctype
= size_t_ctype
;
1892 return size_t_ctype
;
1895 static struct symbol
*evaluate_alignof(struct expression
*expr
)
1897 struct symbol
*type
;
1899 type
= evaluate_type_information(expr
);
1903 expr
->type
= EXPR_VALUE
;
1904 expr
->value
= type
->ctype
.alignment
;
1905 expr
->ctype
= size_t_ctype
;
1906 return size_t_ctype
;
1909 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1911 struct expression
*expr
;
1912 struct symbol_list
*argument_types
= fn
->arguments
;
1913 struct symbol
*argtype
;
1916 PREPARE_PTR_LIST(argument_types
, argtype
);
1917 FOR_EACH_PTR (head
, expr
) {
1918 struct expression
**p
= THIS_ADDRESS(expr
);
1919 struct symbol
*ctype
, *target
;
1920 ctype
= evaluate_expression(expr
);
1925 ctype
= degenerate(expr
);
1928 if (!target
&& ctype
->bit_size
< bits_in_int
)
1929 target
= &int_ctype
;
1931 static char where
[30];
1932 examine_symbol_type(target
);
1933 sprintf(where
, "argument %d", i
);
1934 compatible_assignment_types(expr
, target
, p
, ctype
, where
);
1938 NEXT_PTR_LIST(argtype
);
1939 } END_FOR_EACH_PTR(expr
);
1940 FINISH_PTR_LIST(argtype
);
1944 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
1948 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
1949 if (sym
->ident
== ident
)
1951 } END_FOR_EACH_PTR(sym
);
1955 static void convert_index(struct expression
*e
)
1957 struct expression
*child
= e
->idx_expression
;
1958 unsigned from
= e
->idx_from
;
1959 unsigned to
= e
->idx_to
+ 1;
1961 e
->init_offset
= from
* (e
->ctype
->bit_size
>>3);
1962 e
->init_nr
= to
- from
;
1963 e
->init_expr
= child
;
1966 static void convert_ident(struct expression
*e
)
1968 struct expression
*child
= e
->ident_expression
;
1969 struct symbol
*sym
= e
->field
;
1971 e
->init_offset
= sym
->offset
;
1973 e
->init_expr
= child
;
1976 static void convert_designators(struct expression
*e
)
1979 if (e
->type
== EXPR_INDEX
)
1981 else if (e
->type
== EXPR_IDENTIFIER
)
1989 static void excess(struct expression
*e
, const char *s
)
1991 warning(e
->pos
, "excessive elements in %s initializer", s
);
1995 * implicit designator for the first element
1997 static struct expression
*first_subobject(struct symbol
*ctype
, int class,
1998 struct expression
**v
)
2000 struct expression
*e
= *v
, *new;
2002 if (ctype
->type
== SYM_NODE
)
2003 ctype
= ctype
->ctype
.base_type
;
2005 if (class & TYPE_PTR
) { /* array */
2006 if (!ctype
->bit_size
)
2008 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2009 new->idx_expression
= e
;
2010 new->ctype
= ctype
->ctype
.base_type
;
2012 struct symbol
*field
, *p
;
2013 PREPARE_PTR_LIST(ctype
->symbol_list
, p
);
2014 while (p
&& !p
->ident
&& is_bitfield_type(p
))
2020 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2021 new->ident_expression
= e
;
2022 new->field
= new->ctype
= field
;
2029 * sanity-check explicit designators; return the innermost one or NULL
2030 * in case of error. Assign types.
2032 static struct expression
*check_designators(struct expression
*e
,
2033 struct symbol
*ctype
)
2035 struct expression
*last
= NULL
;
2038 if (ctype
->type
== SYM_NODE
)
2039 ctype
= ctype
->ctype
.base_type
;
2040 if (e
->type
== EXPR_INDEX
) {
2041 struct symbol
*type
;
2042 if (ctype
->type
!= SYM_ARRAY
) {
2043 err
= "array index in non-array";
2046 type
= ctype
->ctype
.base_type
;
2047 if (ctype
->bit_size
>= 0 && type
->bit_size
>= 0) {
2048 unsigned offset
= e
->idx_to
* type
->bit_size
;
2049 if (offset
>= ctype
->bit_size
) {
2050 err
= "index out of bounds in";
2054 e
->ctype
= ctype
= type
;
2057 if (!e
->idx_expression
) {
2061 e
= e
->idx_expression
;
2062 } else if (e
->type
== EXPR_IDENTIFIER
) {
2063 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2064 err
= "field name not in struct or union";
2067 ctype
= find_struct_ident(ctype
, e
->expr_ident
);
2069 err
= "unknown field name in";
2072 e
->field
= e
->ctype
= ctype
;
2074 if (!e
->ident_expression
) {
2078 e
= e
->ident_expression
;
2079 } else if (e
->type
== EXPR_POS
) {
2080 err
= "internal front-end error: EXPR_POS in";
2085 expression_error(e
, "%s initializer", err
);
2090 * choose the next subobject to initialize.
2092 * Get designators for next element, switch old ones to EXPR_POS.
2093 * Return the resulting expression or NULL if we'd run out of subobjects.
2094 * The innermost designator is returned in *v. Designators in old
2095 * are assumed to be already sanity-checked.
2097 static struct expression
*next_designators(struct expression
*old
,
2098 struct symbol
*ctype
,
2099 struct expression
*e
, struct expression
**v
)
2101 struct expression
*new = NULL
;
2105 if (old
->type
== EXPR_INDEX
) {
2106 struct expression
*copy
;
2109 copy
= next_designators(old
->idx_expression
,
2112 n
= old
->idx_to
+ 1;
2113 if (n
* old
->ctype
->bit_size
== ctype
->bit_size
) {
2118 *v
= new = alloc_expression(e
->pos
, EXPR_INDEX
);
2121 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2124 new->idx_from
= new->idx_to
= n
;
2125 new->idx_expression
= copy
;
2126 new->ctype
= old
->ctype
;
2128 } else if (old
->type
== EXPR_IDENTIFIER
) {
2129 struct expression
*copy
;
2130 struct symbol
*field
;
2132 copy
= next_designators(old
->ident_expression
,
2135 field
= old
->field
->next_subobject
;
2141 *v
= new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2144 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2148 new->expr_ident
= field
->ident
;
2149 new->ident_expression
= copy
;
2156 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2157 int class, struct symbol
*ctype
);
2160 * deal with traversing subobjects [6.7.8(17,18,20)]
2162 static void handle_list_initializer(struct expression
*expr
,
2163 int class, struct symbol
*ctype
)
2165 struct expression
*e
, *last
= NULL
, *top
= NULL
, *next
;
2168 FOR_EACH_PTR(expr
->expr_list
, e
) {
2169 struct expression
**v
;
2170 struct symbol
*type
;
2173 if (e
->type
!= EXPR_INDEX
&& e
->type
!= EXPR_IDENTIFIER
) {
2176 last
= first_subobject(ctype
, class, &top
);
2178 last
= next_designators(last
, ctype
, e
, &top
);
2181 excess(e
, class & TYPE_PTR
? "array" :
2183 DELETE_CURRENT_PTR(e
);
2187 warning(e
->pos
, "advancing past deep designator");
2190 REPLACE_CURRENT_PTR(e
, last
);
2192 next
= check_designators(e
, ctype
);
2194 DELETE_CURRENT_PTR(e
);
2198 /* deeper than one designator? */
2200 convert_designators(last
);
2205 lclass
= classify_type(top
->ctype
, &type
);
2206 if (top
->type
== EXPR_INDEX
)
2207 v
= &top
->idx_expression
;
2209 v
= &top
->ident_expression
;
2211 if (handle_simple_initializer(v
, 1, lclass
, top
->ctype
))
2214 if (!(lclass
& TYPE_COMPOUND
)) {
2215 warning(e
->pos
, "bogus scalar initializer");
2216 DELETE_CURRENT_PTR(e
);
2220 next
= first_subobject(type
, lclass
, v
);
2222 warning(e
->pos
, "missing braces around initializer");
2227 DELETE_CURRENT_PTR(e
);
2228 excess(e
, lclass
& TYPE_PTR
? "array" : "struct or union");
2230 } END_FOR_EACH_PTR(e
);
2232 convert_designators(last
);
2233 expr
->ctype
= ctype
;
2236 static int is_string_literal(struct expression
**v
)
2238 struct expression
*e
= *v
;
2239 while (e
&& e
->type
== EXPR_PREOP
&& e
->op
== '(')
2241 if (!e
|| e
->type
!= EXPR_STRING
)
2243 if (e
!= *v
&& Wparen_string
)
2245 "array initialized from parenthesized string constant");
2251 * We want a normal expression, possibly in one layer of braces. Warn
2252 * if the latter happens inside a list (it's legal, but likely to be
2253 * an effect of screwup). In case of anything not legal, we are definitely
2254 * having an effect of screwup, so just fail and let the caller warn.
2256 static struct expression
*handle_scalar(struct expression
*e
, int nested
)
2258 struct expression
*v
= NULL
, *p
;
2262 if (e
->type
!= EXPR_INITIALIZER
)
2265 FOR_EACH_PTR(e
->expr_list
, p
) {
2269 } END_FOR_EACH_PTR(p
);
2273 case EXPR_INITIALIZER
:
2275 case EXPR_IDENTIFIER
:
2281 warning(e
->pos
, "braces around scalar initializer");
2286 * deal with the cases that don't care about subobjects:
2287 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2288 * character array <- string literal, possibly in braces [6.7.8(14)]
2289 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2290 * compound type <- initializer list in braces [6.7.8(16)]
2291 * The last one punts to handle_list_initializer() which, in turn will call
2292 * us for individual elements of the list.
2294 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2295 * the lack of support of wide char stuff in general.
2297 * One note: we need to take care not to evaluate a string literal until
2298 * we know that we *will* handle it right here. Otherwise we would screw
2299 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2300 * { "string", ...} - we need to preserve that string literal recognizable
2301 * until we dig into the inner struct.
2303 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2304 int class, struct symbol
*ctype
)
2306 int is_string
= is_string_type(ctype
);
2307 struct expression
*e
= *ep
, *p
;
2308 struct symbol
*type
;
2314 if (!(class & TYPE_COMPOUND
)) {
2315 e
= handle_scalar(e
, nested
);
2319 type
= evaluate_expression(e
);
2322 compatible_assignment_types(e
, ctype
, ep
, degenerate(e
),
2328 * sublist; either a string, or we dig in; the latter will deal with
2329 * pathologies, so we don't need anything fancy here.
2331 if (e
->type
== EXPR_INITIALIZER
) {
2333 struct expression
*v
= NULL
;
2336 FOR_EACH_PTR(e
->expr_list
, p
) {
2340 } END_FOR_EACH_PTR(p
);
2341 if (count
== 1 && is_string_literal(&v
)) {
2346 handle_list_initializer(e
, class, ctype
);
2351 if (is_string_literal(&e
)) {
2352 /* either we are doing array of char, or we'll have to dig in */
2359 /* struct or union can be initialized by compatible */
2360 if (class != TYPE_COMPOUND
)
2362 type
= evaluate_expression(e
);
2365 if (ctype
->type
== SYM_NODE
)
2366 ctype
= ctype
->ctype
.base_type
;
2367 if (type
->type
== SYM_NODE
)
2368 type
= type
->ctype
.base_type
;
2374 p
= alloc_expression(e
->pos
, EXPR_STRING
);
2376 type
= evaluate_expression(p
);
2377 if (ctype
->bit_size
!= -1 &&
2378 ctype
->bit_size
+ bits_in_char
< type
->bit_size
) {
2380 "too long initializer-string for array of char");
2386 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2388 struct symbol
*type
;
2389 int class = classify_type(ctype
, &type
);
2390 if (!handle_simple_initializer(ep
, 0, class, ctype
))
2391 expression_error(*ep
, "invalid initializer");
2394 static int get_as(struct symbol
*sym
)
2402 mod
= sym
->ctype
.modifiers
;
2403 if (sym
->type
== SYM_NODE
) {
2404 sym
= sym
->ctype
.base_type
;
2405 as
|= sym
->ctype
.as
;
2406 mod
|= sym
->ctype
.modifiers
;
2410 * At least for now, allow casting to a "unsigned long".
2411 * That's how we do things like pointer arithmetic and
2412 * store pointers to registers.
2414 if (sym
== &ulong_ctype
)
2417 if (sym
&& sym
->type
== SYM_PTR
) {
2418 sym
= get_base_type(sym
);
2419 as
|= sym
->ctype
.as
;
2420 mod
|= sym
->ctype
.modifiers
;
2422 if (mod
& MOD_FORCE
)
2427 static void cast_to_as(struct expression
*e
, int as
)
2429 struct expression
*v
= e
->cast_expression
;
2430 struct symbol
*type
= v
->ctype
;
2432 if (!Wcast_to_address_space
)
2435 if (v
->type
!= EXPR_VALUE
|| v
->value
)
2438 /* cast from constant 0 to pointer is OK */
2439 if (is_int_type(type
))
2442 if (type
->type
== SYM_NODE
)
2443 type
= type
->ctype
.base_type
;
2445 if (type
->type
== SYM_PTR
&& type
->ctype
.base_type
== &void_ctype
)
2449 warning(e
->pos
, "cast adds address space to expression (<asn:%d>)", as
);
2452 static struct symbol
*evaluate_cast(struct expression
*expr
)
2454 struct expression
*target
= expr
->cast_expression
;
2455 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
2456 struct symbol
*t1
, *t2
;
2463 expr
->ctype
= ctype
;
2464 expr
->cast_type
= ctype
;
2467 * Special case: a cast can be followed by an
2468 * initializer, in which case we need to pass
2469 * the type value down to that initializer rather
2470 * than trying to evaluate it as an expression
2472 * A more complex case is when the initializer is
2473 * dereferenced as part of a post-fix expression.
2474 * We need to produce an expression that can be dereferenced.
2476 if (target
->type
== EXPR_INITIALIZER
) {
2477 struct symbol
*sym
= expr
->cast_type
;
2478 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2480 sym
->initializer
= expr
->cast_expression
;
2481 evaluate_symbol(sym
);
2483 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2486 expr
->type
= EXPR_PREOP
;
2494 evaluate_expression(target
);
2497 class1
= classify_type(ctype
, &t1
);
2499 * You can always throw a value away by casting to
2500 * "void" - that's an implicit "force". Note that
2501 * the same is _not_ true of "void *".
2503 if (t1
== &void_ctype
)
2506 if (class1
& TYPE_COMPOUND
)
2507 warning(expr
->pos
, "cast to non-scalar");
2511 expression_error(expr
, "cast from unknown type");
2514 class2
= classify_type(t2
, &t2
);
2516 if (class2
& TYPE_COMPOUND
)
2517 warning(expr
->pos
, "cast from non-scalar");
2519 /* allowed cast unfouls */
2520 if (class2
& TYPE_FOULED
)
2521 t2
= t2
->ctype
.base_type
;
2523 if (!(ctype
->ctype
.modifiers
& MOD_FORCE
) && t1
!= t2
) {
2524 if (class1
& TYPE_RESTRICT
)
2525 warning(expr
->pos
, "cast to restricted type");
2526 if (class2
& TYPE_RESTRICT
)
2527 warning(expr
->pos
, "cast from restricted type");
2530 as1
= get_as(ctype
);
2531 as2
= get_as(target
->ctype
);
2532 if (!as1
&& as2
> 0)
2533 warning(expr
->pos
, "cast removes address space of expression");
2534 if (as1
> 0 && as2
> 0 && as1
!= as2
)
2535 warning(expr
->pos
, "cast between address spaces (<asn:%d>-><asn:%d>)", as2
, as1
);
2536 if (as1
> 0 && !as2
)
2537 cast_to_as(expr
, as1
);
2540 * Casts of constant values are special: they
2541 * can be NULL, and thus need to be simplified
2544 if (target
->type
== EXPR_VALUE
)
2545 cast_value(expr
, ctype
, target
, target
->ctype
);
2552 * Evaluate a call expression with a symbol. This
2553 * should expand inline functions, and evaluate
2556 static int evaluate_symbol_call(struct expression
*expr
)
2558 struct expression
*fn
= expr
->fn
;
2559 struct symbol
*ctype
= fn
->ctype
;
2561 if (fn
->type
!= EXPR_PREOP
)
2564 if (ctype
->op
&& ctype
->op
->evaluate
)
2565 return ctype
->op
->evaluate(expr
);
2567 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2569 struct symbol
*curr
= current_fn
;
2570 current_fn
= ctype
->ctype
.base_type
;
2572 ret
= inline_function(expr
, ctype
);
2574 /* restore the old function */
2582 static struct symbol
*evaluate_call(struct expression
*expr
)
2585 struct symbol
*ctype
, *sym
;
2586 struct expression
*fn
= expr
->fn
;
2587 struct expression_list
*arglist
= expr
->args
;
2589 if (!evaluate_expression(fn
))
2591 sym
= ctype
= fn
->ctype
;
2592 if (ctype
->type
== SYM_NODE
)
2593 ctype
= ctype
->ctype
.base_type
;
2594 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
2595 ctype
= get_base_type(ctype
);
2597 examine_fn_arguments(ctype
);
2598 if (sym
->type
== SYM_NODE
&& fn
->type
== EXPR_PREOP
&&
2599 sym
->op
&& sym
->op
->args
) {
2600 if (!sym
->op
->args(expr
))
2603 if (!evaluate_arguments(sym
, ctype
, arglist
))
2605 if (ctype
->type
!= SYM_FN
) {
2606 expression_error(expr
, "not a function %s",
2607 show_ident(sym
->ident
));
2610 args
= expression_list_size(expr
->args
);
2611 fnargs
= symbol_list_size(ctype
->arguments
);
2613 expression_error(expr
,
2614 "not enough arguments for function %s",
2615 show_ident(sym
->ident
));
2616 if (args
> fnargs
&& !ctype
->variadic
)
2617 expression_error(expr
,
2618 "too many arguments for function %s",
2619 show_ident(sym
->ident
));
2621 if (sym
->type
== SYM_NODE
) {
2622 if (evaluate_symbol_call(expr
))
2625 expr
->ctype
= ctype
->ctype
.base_type
;
2629 struct symbol
*evaluate_expression(struct expression
*expr
)
2636 switch (expr
->type
) {
2639 expression_error(expr
, "value expression without a type");
2642 return evaluate_string(expr
);
2644 return evaluate_symbol_expression(expr
);
2646 if (!evaluate_expression(expr
->left
))
2648 if (!evaluate_expression(expr
->right
))
2650 return evaluate_binop(expr
);
2652 return evaluate_logical(expr
);
2654 evaluate_expression(expr
->left
);
2655 if (!evaluate_expression(expr
->right
))
2657 return evaluate_comma(expr
);
2659 if (!evaluate_expression(expr
->left
))
2661 if (!evaluate_expression(expr
->right
))
2663 return evaluate_compare(expr
);
2664 case EXPR_ASSIGNMENT
:
2665 if (!evaluate_expression(expr
->left
))
2667 if (!evaluate_expression(expr
->right
))
2669 return evaluate_assignment(expr
);
2671 if (!evaluate_expression(expr
->unop
))
2673 return evaluate_preop(expr
);
2675 if (!evaluate_expression(expr
->unop
))
2677 return evaluate_postop(expr
);
2679 case EXPR_IMPLIED_CAST
:
2680 return evaluate_cast(expr
);
2682 return evaluate_sizeof(expr
);
2683 case EXPR_PTRSIZEOF
:
2684 return evaluate_ptrsizeof(expr
);
2686 return evaluate_alignof(expr
);
2688 return evaluate_member_dereference(expr
);
2690 return evaluate_call(expr
);
2692 case EXPR_CONDITIONAL
:
2693 return evaluate_conditional_expression(expr
);
2694 case EXPR_STATEMENT
:
2695 expr
->ctype
= evaluate_statement(expr
->statement
);
2699 expr
->ctype
= &ptr_ctype
;
2703 /* Evaluate the type of the symbol .. */
2704 evaluate_symbol(expr
->symbol
);
2705 /* .. but the type of the _expression_ is a "type" */
2706 expr
->ctype
= &type_ctype
;
2709 /* These can not exist as stand-alone expressions */
2710 case EXPR_INITIALIZER
:
2711 case EXPR_IDENTIFIER
:
2714 expression_error(expr
, "internal front-end error: initializer in expression");
2717 expression_error(expr
, "internal front-end error: SLICE re-evaluated");
2723 static void check_duplicates(struct symbol
*sym
)
2726 struct symbol
*next
= sym
;
2728 while ((next
= next
->same_symbol
) != NULL
) {
2729 const char *typediff
;
2730 evaluate_symbol(next
);
2732 typediff
= type_difference(sym
, next
, 0, 0);
2734 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2735 show_ident(sym
->ident
),
2736 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
2741 unsigned long mod
= sym
->ctype
.modifiers
;
2742 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
2744 if (!(mod
& MOD_TOPLEVEL
))
2748 if (sym
->ident
== &main_ident
)
2750 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
2754 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
2756 struct symbol
*base_type
;
2764 sym
= examine_symbol_type(sym
);
2765 base_type
= get_base_type(sym
);
2769 /* Evaluate the initializers */
2770 if (sym
->initializer
)
2771 evaluate_initializer(sym
, &sym
->initializer
);
2773 /* And finally, evaluate the body of the symbol too */
2774 if (base_type
->type
== SYM_FN
) {
2775 struct symbol
*curr
= current_fn
;
2777 current_fn
= base_type
;
2779 examine_fn_arguments(base_type
);
2780 if (!base_type
->stmt
&& base_type
->inline_stmt
)
2782 if (base_type
->stmt
)
2783 evaluate_statement(base_type
->stmt
);
2791 void evaluate_symbol_list(struct symbol_list
*list
)
2795 FOR_EACH_PTR(list
, sym
) {
2796 evaluate_symbol(sym
);
2797 check_duplicates(sym
);
2798 } END_FOR_EACH_PTR(sym
);
2801 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
2803 struct expression
*expr
= stmt
->expression
;
2804 struct symbol
*ctype
, *fntype
;
2806 evaluate_expression(expr
);
2807 ctype
= degenerate(expr
);
2808 fntype
= current_fn
->ctype
.base_type
;
2809 if (!fntype
|| fntype
== &void_ctype
) {
2810 if (expr
&& ctype
!= &void_ctype
)
2811 expression_error(expr
, "return expression in %s function", fntype
?"void":"typeless");
2816 sparse_error(stmt
->pos
, "return with no return value");
2821 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, ctype
, "return expression");
2825 static void evaluate_if_statement(struct statement
*stmt
)
2827 if (!stmt
->if_conditional
)
2830 evaluate_conditional(stmt
->if_conditional
, 0);
2831 evaluate_statement(stmt
->if_true
);
2832 evaluate_statement(stmt
->if_false
);
2835 static void evaluate_iterator(struct statement
*stmt
)
2837 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
2838 evaluate_conditional(stmt
->iterator_post_condition
,1);
2839 evaluate_statement(stmt
->iterator_pre_statement
);
2840 evaluate_statement(stmt
->iterator_statement
);
2841 evaluate_statement(stmt
->iterator_post_statement
);
2844 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
2846 switch (*constraint
) {
2847 case '=': /* Assignment */
2848 case '+': /* Update */
2851 expression_error(expr
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
2855 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
2857 switch (*constraint
) {
2858 case '=': /* Assignment */
2859 case '+': /* Update */
2860 expression_error(expr
, "input constraint with assignment (\"%s\")", constraint
);
2864 static void evaluate_asm_statement(struct statement
*stmt
)
2866 struct expression
*expr
;
2869 expr
= stmt
->asm_string
;
2870 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2871 sparse_error(stmt
->pos
, "need constant string for inline asm");
2876 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
2877 struct ident
*ident
;
2880 case 0: /* Identifier */
2882 ident
= (struct ident
*)expr
;
2885 case 1: /* Constraint */
2887 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2888 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
2889 *THIS_ADDRESS(expr
) = NULL
;
2892 verify_output_constraint(expr
, expr
->string
->data
);
2895 case 2: /* Expression */
2897 if (!evaluate_expression(expr
))
2899 if (!lvalue_expression(expr
))
2900 warning(expr
->pos
, "asm output is not an lvalue");
2901 evaluate_assign_to(expr
, expr
->ctype
);
2904 } END_FOR_EACH_PTR(expr
);
2907 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
2908 struct ident
*ident
;
2911 case 0: /* Identifier */
2913 ident
= (struct ident
*)expr
;
2916 case 1: /* Constraint */
2918 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2919 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
2920 *THIS_ADDRESS(expr
) = NULL
;
2923 verify_input_constraint(expr
, expr
->string
->data
);
2926 case 2: /* Expression */
2928 if (!evaluate_expression(expr
))
2932 } END_FOR_EACH_PTR(expr
);
2934 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
2936 sparse_error(stmt
->pos
, "bad asm output");
2939 if (expr
->type
== EXPR_STRING
)
2941 expression_error(expr
, "asm clobber is not a string");
2942 } END_FOR_EACH_PTR(expr
);
2945 static void evaluate_case_statement(struct statement
*stmt
)
2947 evaluate_expression(stmt
->case_expression
);
2948 evaluate_expression(stmt
->case_to
);
2949 evaluate_statement(stmt
->case_statement
);
2952 static void check_case_type(struct expression
*switch_expr
,
2953 struct expression
*case_expr
,
2954 struct expression
**enumcase
)
2956 struct symbol
*switch_type
, *case_type
;
2962 switch_type
= switch_expr
->ctype
;
2963 case_type
= evaluate_expression(case_expr
);
2965 if (!switch_type
|| !case_type
)
2969 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
2970 else if (is_enum_type(case_type
))
2971 *enumcase
= case_expr
;
2974 sclass
= classify_type(switch_type
, &switch_type
);
2975 cclass
= classify_type(case_type
, &case_type
);
2977 /* both should be arithmetic */
2978 if (!(sclass
& cclass
& TYPE_NUM
))
2981 /* neither should be floating */
2982 if ((sclass
| cclass
) & TYPE_FLOAT
)
2985 /* if neither is restricted, we are OK */
2986 if (!((sclass
| cclass
) & TYPE_RESTRICT
))
2989 if (!restricted_binop_type(SPECIAL_EQUAL
, case_expr
, switch_expr
,
2990 cclass
, sclass
, case_type
, switch_type
))
2991 warning(case_expr
->pos
, "restricted degrades to integer");
2996 expression_error(case_expr
, "incompatible types for 'case' statement");
2999 static void evaluate_switch_statement(struct statement
*stmt
)
3002 struct expression
*enumcase
= NULL
;
3003 struct expression
**enumcase_holder
= &enumcase
;
3004 struct expression
*sel
= stmt
->switch_expression
;
3006 evaluate_expression(sel
);
3007 evaluate_statement(stmt
->switch_statement
);
3010 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
3011 enumcase_holder
= NULL
; /* Only check cases against switch */
3013 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
3014 struct statement
*case_stmt
= sym
->stmt
;
3015 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
3016 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
3017 } END_FOR_EACH_PTR(sym
);
3020 struct symbol
*evaluate_statement(struct statement
*stmt
)
3025 switch (stmt
->type
) {
3026 case STMT_DECLARATION
: {
3028 FOR_EACH_PTR(stmt
->declaration
, s
) {
3030 } END_FOR_EACH_PTR(s
);
3035 return evaluate_return_expression(stmt
);
3037 case STMT_EXPRESSION
:
3038 if (!evaluate_expression(stmt
->expression
))
3040 return degenerate(stmt
->expression
);
3042 case STMT_COMPOUND
: {
3043 struct statement
*s
;
3044 struct symbol
*type
= NULL
;
3046 /* Evaluate the return symbol in the compound statement */
3047 evaluate_symbol(stmt
->ret
);
3050 * Then, evaluate each statement, making the type of the
3051 * compound statement be the type of the last statement
3053 type
= evaluate_statement(stmt
->args
);
3054 FOR_EACH_PTR(stmt
->stmts
, s
) {
3055 type
= evaluate_statement(s
);
3056 } END_FOR_EACH_PTR(s
);
3062 evaluate_if_statement(stmt
);
3065 evaluate_iterator(stmt
);
3068 evaluate_switch_statement(stmt
);
3071 evaluate_case_statement(stmt
);
3074 return evaluate_statement(stmt
->label_statement
);
3076 evaluate_expression(stmt
->goto_expression
);
3081 evaluate_asm_statement(stmt
);
3084 evaluate_expression(stmt
->expression
);
3087 evaluate_expression(stmt
->range_expression
);
3088 evaluate_expression(stmt
->range_low
);
3089 evaluate_expression(stmt
->range_high
);