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
);
314 expr
->flags
= old
->flags
;
316 expr
->cast_type
= type
;
317 expr
->cast_expression
= old
;
321 static int is_type_type(struct symbol
*type
)
323 return (type
->ctype
.modifiers
& MOD_TYPE
) != 0;
326 int is_ptr_type(struct symbol
*type
)
328 if (type
->type
== SYM_NODE
)
329 type
= type
->ctype
.base_type
;
330 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
333 static inline int is_float_type(struct symbol
*type
)
335 if (type
->type
== SYM_NODE
)
336 type
= type
->ctype
.base_type
;
337 return type
->ctype
.base_type
== &fp_type
;
340 static inline int is_byte_type(struct symbol
*type
)
342 return type
->bit_size
== bits_in_char
&& type
->type
!= SYM_BITFIELD
;
355 static inline int classify_type(struct symbol
*type
, struct symbol
**base
)
357 static int type_class
[SYM_BAD
+ 1] = {
358 [SYM_PTR
] = TYPE_PTR
,
360 [SYM_ARRAY
] = TYPE_PTR
| TYPE_COMPOUND
,
361 [SYM_STRUCT
] = TYPE_COMPOUND
,
362 [SYM_UNION
] = TYPE_COMPOUND
,
363 [SYM_BITFIELD
] = TYPE_NUM
| TYPE_BITFIELD
,
364 [SYM_RESTRICT
] = TYPE_NUM
| TYPE_RESTRICT
,
365 [SYM_FOULED
] = TYPE_NUM
| TYPE_RESTRICT
| TYPE_FOULED
,
367 if (type
->type
== SYM_NODE
)
368 type
= type
->ctype
.base_type
;
369 if (type
->type
== SYM_ENUM
)
370 type
= type
->ctype
.base_type
;
372 if (type
->type
== SYM_BASETYPE
) {
373 if (type
->ctype
.base_type
== &int_type
)
375 if (type
->ctype
.base_type
== &fp_type
)
376 return TYPE_NUM
| TYPE_FLOAT
;
378 return type_class
[type
->type
];
381 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
383 static inline int is_string_type(struct symbol
*type
)
385 if (type
->type
== SYM_NODE
)
386 type
= type
->ctype
.base_type
;
387 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
390 static struct symbol
*bad_expr_type(struct expression
*expr
)
392 sparse_error(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
393 switch (expr
->type
) {
396 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
397 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
401 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
408 return expr
->ctype
= &bad_ctype
;
411 static int restricted_value(struct expression
*v
, struct symbol
*type
)
413 if (v
->type
!= EXPR_VALUE
)
420 static int restricted_binop(int op
, struct symbol
*type
)
425 case SPECIAL_AND_ASSIGN
:
426 case SPECIAL_OR_ASSIGN
:
427 case SPECIAL_XOR_ASSIGN
:
428 return 1; /* unfoul */
432 return 2; /* keep fouled */
434 case SPECIAL_NOTEQUAL
:
435 return 3; /* warn if fouled */
441 static int restricted_unop(int op
, struct symbol
**type
)
444 if ((*type
)->bit_size
< bits_in_int
)
445 *type
= befoul(*type
);
452 static struct symbol
*restricted_binop_type(int op
,
453 struct expression
*left
,
454 struct expression
*right
,
455 int lclass
, int rclass
,
456 struct symbol
*ltype
,
457 struct symbol
*rtype
)
459 struct symbol
*ctype
= NULL
;
460 if (lclass
& TYPE_RESTRICT
) {
461 if (rclass
& TYPE_RESTRICT
) {
462 if (ltype
== rtype
) {
464 } else if (lclass
& TYPE_FOULED
) {
465 if (ltype
->ctype
.base_type
== rtype
)
467 } else if (rclass
& TYPE_FOULED
) {
468 if (rtype
->ctype
.base_type
== ltype
)
472 if (!restricted_value(right
, ltype
))
475 } else if (!restricted_value(left
, rtype
))
479 switch (restricted_binop(op
, ctype
)) {
481 if ((lclass
^ rclass
) & TYPE_FOULED
)
482 ctype
= ctype
->ctype
.base_type
;
485 if (!(lclass
& rclass
& TYPE_FOULED
))
497 static inline void unrestrict(struct expression
*expr
,
498 int class, struct symbol
**ctype
)
500 if (class & TYPE_RESTRICT
) {
501 warning(expr
->pos
, "restricted degrades to integer");
502 if (class & TYPE_FOULED
) /* unfoul it first */
503 *ctype
= (*ctype
)->ctype
.base_type
;
504 *ctype
= (*ctype
)->ctype
.base_type
; /* get to arithmetic type */
508 static struct symbol
*usual_conversions(int op
,
509 struct expression
*left
,
510 struct expression
*right
,
511 int lclass
, int rclass
,
512 struct symbol
*ltype
,
513 struct symbol
*rtype
)
515 struct symbol
*ctype
;
517 warn_for_different_enum_types(right
->pos
, left
->ctype
, right
->ctype
);
519 if ((lclass
| rclass
) & TYPE_RESTRICT
)
523 if (!(lclass
& TYPE_FLOAT
)) {
524 if (!(rclass
& TYPE_FLOAT
))
525 return bigger_int_type(ltype
, rtype
);
528 } else if (rclass
& TYPE_FLOAT
) {
529 unsigned long lmod
= ltype
->ctype
.modifiers
;
530 unsigned long rmod
= rtype
->ctype
.modifiers
;
531 if (rmod
& ~lmod
& (MOD_LONG
| MOD_LONGLONG
))
539 ctype
= restricted_binop_type(op
, left
, right
,
540 lclass
, rclass
, ltype
, rtype
);
544 unrestrict(left
, lclass
, <ype
);
545 unrestrict(right
, rclass
, &rtype
);
550 static inline int lvalue_expression(struct expression
*expr
)
552 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
555 static int ptr_object_size(struct symbol
*ptr_type
)
557 if (ptr_type
->type
== SYM_NODE
)
558 ptr_type
= ptr_type
->ctype
.base_type
;
559 if (ptr_type
->type
== SYM_PTR
)
560 ptr_type
= get_base_type(ptr_type
);
561 return ptr_type
->bit_size
;
564 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*ctype
, struct symbol
*itype
)
566 struct expression
*index
= expr
->right
;
570 examine_symbol_type(ctype
);
572 if (!ctype
->ctype
.base_type
) {
573 expression_error(expr
, "missing type information");
577 /* Get the size of whatever the pointer points to */
578 bit_size
= ptr_object_size(ctype
);
579 multiply
= bit_size
>> 3;
583 if (multiply
== 1 && itype
->bit_size
>= bits_in_pointer
)
586 if (index
->type
== EXPR_VALUE
) {
587 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
588 unsigned long long v
= index
->value
, mask
;
589 mask
= 1ULL << (itype
->bit_size
- 1);
595 mask
= 1ULL << (bits_in_pointer
- 1);
596 v
&= mask
| (mask
- 1);
598 val
->ctype
= ssize_t_ctype
;
603 if (itype
->bit_size
< bits_in_pointer
)
604 index
= cast_to(index
, ssize_t_ctype
);
607 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
608 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
610 val
->ctype
= ssize_t_ctype
;
611 val
->value
= multiply
;
614 mul
->ctype
= ssize_t_ctype
;
624 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
625 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
628 unsigned long mod1
, mod2
, diff
;
629 unsigned long as1
, as2
;
631 struct symbol
*base1
, *base2
;
633 if (target
== source
)
635 if (!target
|| !source
)
636 return "different types";
638 * Peel of per-node information.
639 * FIXME! Check alignment and context too here!
641 mod1
= target
->ctype
.modifiers
;
642 as1
= target
->ctype
.as
;
643 mod2
= source
->ctype
.modifiers
;
644 as2
= source
->ctype
.as
;
645 if (target
->type
== SYM_NODE
) {
646 target
= target
->ctype
.base_type
;
649 if (target
->type
== SYM_PTR
) {
653 mod1
|= target
->ctype
.modifiers
;
654 as1
|= target
->ctype
.as
;
656 if (source
->type
== SYM_NODE
) {
657 source
= source
->ctype
.base_type
;
660 if (source
->type
== SYM_PTR
) {
664 mod2
|= source
->ctype
.modifiers
;
665 as2
|= source
->ctype
.as
;
667 if (target
->type
== SYM_ENUM
) {
668 target
= target
->ctype
.base_type
;
672 if (source
->type
== SYM_ENUM
) {
673 source
= source
->ctype
.base_type
;
678 if (target
== source
)
680 if (!target
|| !source
)
681 return "different types";
683 type1
= target
->type
;
684 base1
= target
->ctype
.base_type
;
686 type2
= source
->type
;
687 base2
= source
->ctype
.base_type
;
690 * Pointers to functions compare as the function itself
692 if (type1
== SYM_PTR
&& base1
) {
693 base1
= examine_symbol_type(base1
);
694 switch (base1
->type
) {
698 base1
= base1
->ctype
.base_type
;
703 if (type2
== SYM_PTR
&& base2
) {
704 base2
= examine_symbol_type(base2
);
705 switch (base2
->type
) {
709 base2
= base2
->ctype
.base_type
;
715 /* Arrays degenerate to pointers for type comparisons */
716 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
717 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
719 if (type1
!= type2
|| type1
== SYM_RESTRICT
)
720 return "different base types";
722 /* Must be same address space to be comparable */
723 if (Waddress_space
&& as1
!= as2
)
724 return "different address spaces";
726 /* Ignore differences in storage types or addressability */
727 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
728 diff
&= (mod1
& ~target_mod_ignore
) | (mod2
& ~source_mod_ignore
);
731 return "different type sizes";
732 if (diff
& ~MOD_SIGNEDNESS
)
733 return "different modifiers";
735 /* Differs in signedness only.. */
738 * Warn if both are explicitly signed ("unsigned" is obviously
739 * always explicit, and since we know one of them has to be
740 * unsigned, we check if the signed one was explicit).
742 if ((mod1
| mod2
) & MOD_EXPLICITLY_SIGNED
)
743 return "different explicit signedness";
746 * "char" matches both "unsigned char" and "signed char",
747 * so if the explicit test didn't trigger, then we should
748 * not warn about a char.
750 if (!(mod1
& MOD_CHAR
))
751 return "different signedness";
755 if (type1
== SYM_FN
) {
757 struct symbol
*arg1
, *arg2
;
758 if (base1
->variadic
!= base2
->variadic
)
759 return "incompatible variadic arguments";
760 PREPARE_PTR_LIST(target
->arguments
, arg1
);
761 PREPARE_PTR_LIST(source
->arguments
, arg2
);
765 diffstr
= type_difference(arg1
, arg2
, 0, 0);
767 static char argdiff
[80];
768 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diffstr
);
777 FINISH_PTR_LIST(arg2
);
778 FINISH_PTR_LIST(arg1
);
787 static int is_null_ptr(struct expression
*expr
)
789 if (expr
->type
!= EXPR_VALUE
|| expr
->value
)
791 if (Wnon_pointer_null
&& !is_ptr_type(expr
->ctype
))
792 warning(expr
->pos
, "Using plain integer as NULL pointer");
797 * Ignore differences in "volatile" and "const"ness when
798 * subtracting pointers
800 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
802 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
)
804 const char *typediff
;
805 struct symbol
*ctype
;
806 struct symbol
*ltype
, *rtype
;
807 struct expression
*l
= expr
->left
;
808 struct expression
*r
= expr
->right
;
810 ltype
= degenerate(l
);
811 rtype
= degenerate(r
);
814 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
816 expression_error(expr
, "subtraction of different types can't work (%s)", typediff
);
817 examine_symbol_type(ctype
);
819 /* Figure out the base type we point to */
820 if (ctype
->type
== SYM_NODE
)
821 ctype
= ctype
->ctype
.base_type
;
822 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
823 expression_error(expr
, "subtraction of functions? Share your drugs");
826 ctype
= get_base_type(ctype
);
828 expr
->ctype
= ssize_t_ctype
;
829 if (ctype
->bit_size
> bits_in_char
) {
830 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
831 struct expression
*div
= expr
;
832 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
833 unsigned long value
= ctype
->bit_size
>> 3;
835 val
->ctype
= size_t_ctype
;
838 if (value
& (value
-1)) {
839 if (Wptr_subtraction_blows
)
840 warning(expr
->pos
, "potentially expensive pointer subtraction");
844 sub
->ctype
= ssize_t_ctype
;
853 return ssize_t_ctype
;
856 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
858 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
860 struct symbol
*ctype
;
865 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
866 warning(expr
->pos
, "assignment expression in conditional");
868 ctype
= evaluate_expression(expr
);
870 if (is_safe_type(ctype
))
871 warning(expr
->pos
, "testing a 'safe expression'");
877 static struct symbol
*evaluate_logical(struct expression
*expr
)
879 if (!evaluate_conditional(expr
->left
, 0))
881 if (!evaluate_conditional(expr
->right
, 0))
884 expr
->ctype
= &bool_ctype
;
886 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
892 static struct symbol
*evaluate_binop(struct expression
*expr
)
894 struct symbol
*ltype
, *rtype
, *ctype
;
895 int lclass
= classify_type(expr
->left
->ctype
, <ype
);
896 int rclass
= classify_type(expr
->right
->ctype
, &rtype
);
900 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
904 /* number op number */
905 if (lclass
& rclass
& TYPE_NUM
) {
906 if ((lclass
| rclass
) & TYPE_FLOAT
) {
908 case '+': case '-': case '*': case '/':
911 return bad_expr_type(expr
);
915 if (op
== SPECIAL_LEFTSHIFT
|| op
== SPECIAL_RIGHTSHIFT
) {
916 // shifts do integer promotions, but that's it.
917 unrestrict(expr
->left
, lclass
, <ype
);
918 unrestrict(expr
->right
, rclass
, &rtype
);
919 ctype
= ltype
= integer_promotion(ltype
);
920 rtype
= integer_promotion(rtype
);
922 // The rest do usual conversions
923 ltype
= usual_conversions(op
, expr
->left
, expr
->right
,
924 lclass
, rclass
, ltype
, rtype
);
925 ctype
= rtype
= ltype
;
928 expr
->left
= cast_to(expr
->left
, ltype
);
929 expr
->right
= cast_to(expr
->right
, rtype
);
934 /* pointer (+|-) integer */
935 if (lclass
& TYPE_PTR
&& is_int(rclass
) && (op
== '+' || op
== '-')) {
936 unrestrict(expr
->right
, rclass
, &rtype
);
937 return evaluate_ptr_add(expr
, degenerate(expr
->left
), rtype
);
940 /* integer + pointer */
941 if (rclass
& TYPE_PTR
&& is_int(lclass
) && op
== '+') {
942 struct expression
*index
= expr
->left
;
943 unrestrict(index
, lclass
, <ype
);
944 expr
->left
= expr
->right
;
946 return evaluate_ptr_add(expr
, degenerate(expr
->left
), ltype
);
949 /* pointer - pointer */
950 if (lclass
& rclass
& TYPE_PTR
&& expr
->op
== '-')
951 return evaluate_ptr_sub(expr
);
953 return bad_expr_type(expr
);
956 static struct symbol
*evaluate_comma(struct expression
*expr
)
958 expr
->ctype
= expr
->right
->ctype
;
962 static int modify_for_unsigned(int op
)
965 op
= SPECIAL_UNSIGNED_LT
;
967 op
= SPECIAL_UNSIGNED_GT
;
968 else if (op
== SPECIAL_LTE
)
969 op
= SPECIAL_UNSIGNED_LTE
;
970 else if (op
== SPECIAL_GTE
)
971 op
= SPECIAL_UNSIGNED_GTE
;
975 static struct symbol
*evaluate_compare(struct expression
*expr
)
977 struct expression
*left
= expr
->left
, *right
= expr
->right
;
978 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
979 struct symbol
*ctype
;
983 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
988 if (is_type_type(ltype
) && is_type_type(rtype
))
991 if (is_safe_type(ltype
) || is_safe_type(rtype
))
992 warning(expr
->pos
, "testing a 'safe expression'");
994 lclass
= classify_type(ltype
, <ype
);
995 rclass
= classify_type(rtype
, &rtype
);
998 if ((lclass
| rclass
) & TYPE_PTR
) {
999 // FIXME! Check the types for compatibility
1000 expr
->op
= modify_for_unsigned(expr
->op
);
1004 /* Both should be numbers */
1005 if (!(lclass
& rclass
& TYPE_NUM
))
1006 return bad_expr_type(expr
);
1008 ctype
= usual_conversions(expr
->op
, expr
->left
, expr
->right
,
1009 lclass
, rclass
, ltype
, rtype
);
1010 expr
->left
= cast_to(expr
->left
, ctype
);
1011 expr
->right
= cast_to(expr
->right
, ctype
);
1012 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
1013 expr
->op
= modify_for_unsigned(expr
->op
);
1016 expr
->ctype
= &bool_ctype
;
1021 * FIXME!! This should do casts, array degeneration etc..
1023 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
1025 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
1027 if (ltype
->type
== SYM_NODE
)
1028 ltype
= ltype
->ctype
.base_type
;
1030 if (rtype
->type
== SYM_NODE
)
1031 rtype
= rtype
->ctype
.base_type
;
1033 if (ltype
->type
== SYM_PTR
) {
1034 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
1038 if (rtype
->type
== SYM_PTR
) {
1039 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
1046 * NOTE! The degenerate case of "x ? : y", where we don't
1047 * have a true case, this will possibly promote "x" to the
1048 * same type as "y", and thus _change_ the conditional
1049 * test in the expression. But since promotion is "safe"
1050 * for testing, that's OK.
1052 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
1054 struct expression
**true;
1055 struct symbol
*ctype
, *ltype
, *rtype
;
1057 const char * typediff
;
1059 if (!evaluate_conditional(expr
->conditional
, 0))
1061 if (!evaluate_expression(expr
->cond_false
))
1064 ctype
= degenerate(expr
->conditional
);
1065 rtype
= degenerate(expr
->cond_false
);
1067 true = &expr
->conditional
;
1069 if (expr
->cond_true
) {
1070 if (!evaluate_expression(expr
->cond_true
))
1072 ltype
= degenerate(expr
->cond_true
);
1073 true = &expr
->cond_true
;
1077 int flags
= expr
->conditional
->flags
& Int_const_expr
;
1078 flags
&= (*true)->flags
& expr
->cond_false
->flags
;
1083 lclass
= classify_type(ltype
, <ype
);
1084 rclass
= classify_type(rtype
, &rtype
);
1085 if (lclass
& rclass
& TYPE_NUM
) {
1086 ctype
= usual_conversions('?', *true, expr
->cond_false
,
1087 lclass
, rclass
, ltype
, rtype
);
1088 *true = cast_to(*true, ctype
);
1089 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1092 ctype
= compatible_ptr_type(*true, expr
->cond_false
);
1096 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
1099 expression_error(expr
, "incompatible types in conditional expression (%s)", typediff
);
1103 expr
->ctype
= ctype
;
1107 /* FP assignments can not do modulo or bit operations */
1108 static int compatible_float_op(int op
)
1110 return op
== SPECIAL_ADD_ASSIGN
||
1111 op
== SPECIAL_SUB_ASSIGN
||
1112 op
== SPECIAL_MUL_ASSIGN
||
1113 op
== SPECIAL_DIV_ASSIGN
;
1116 static int evaluate_assign_op(struct expression
*expr
)
1118 struct symbol
*target
= expr
->left
->ctype
;
1119 struct symbol
*source
= expr
->right
->ctype
;
1120 struct symbol
*t
, *s
;
1121 int tclass
= classify_type(target
, &t
);
1122 int sclass
= classify_type(source
, &s
);
1125 if (tclass
& sclass
& TYPE_NUM
) {
1126 if (tclass
& TYPE_FLOAT
&& !compatible_float_op(op
)) {
1127 expression_error(expr
, "invalid assignment");
1130 if (tclass
& TYPE_RESTRICT
) {
1131 if (!restricted_binop(op
, t
)) {
1132 expression_error(expr
, "bad restricted assignment");
1135 /* allowed assignments unfoul */
1136 if (sclass
& TYPE_FOULED
&& s
->ctype
.base_type
== t
)
1138 if (!restricted_value(expr
->right
, t
))
1140 } else if (!(sclass
& TYPE_RESTRICT
))
1142 /* source and target would better be identical restricted */
1145 warning(expr
->pos
, "invalid restricted assignment");
1146 expr
->right
= cast_to(expr
->right
, target
);
1149 if (tclass
& TYPE_PTR
&& is_int(sclass
)) {
1150 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1151 unrestrict(expr
->right
, sclass
, &s
);
1152 evaluate_ptr_add(expr
, target
, s
);
1155 expression_error(expr
, "invalid pointer assignment");
1159 expression_error(expr
, "invalid assignment");
1163 expr
->right
= cast_to(expr
->right
, target
);
1167 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1168 struct expression
**rp
, const char *where
)
1170 const char *typediff
;
1171 struct symbol
*source
= degenerate(*rp
);
1172 struct symbol
*t
, *s
;
1173 int tclass
= classify_type(target
, &t
);
1174 int sclass
= classify_type(source
, &s
);
1176 if (tclass
& sclass
& TYPE_NUM
) {
1177 if (tclass
& TYPE_RESTRICT
) {
1178 /* allowed assignments unfoul */
1179 if (sclass
& TYPE_FOULED
&& s
->ctype
.base_type
== t
)
1181 if (!restricted_value(*rp
, target
))
1183 } else if (!(sclass
& TYPE_RESTRICT
))
1187 /* It's OK if the target is more volatile or const than the source */
1188 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
1192 /* Pointer destination? */
1193 if (tclass
& TYPE_PTR
) {
1194 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
;
1267 struct expression
*where
= expr
;
1268 struct symbol
*ltype
;
1270 if (!lvalue_expression(left
)) {
1271 expression_error(expr
, "not an lvalue");
1275 ltype
= left
->ctype
;
1277 if (expr
->op
!= '=') {
1278 if (!evaluate_assign_op(expr
))
1281 if (!compatible_assignment_types(where
, ltype
, &expr
->right
, "assignment"))
1285 evaluate_assign_to(left
, ltype
);
1287 expr
->ctype
= ltype
;
1291 static void examine_fn_arguments(struct symbol
*fn
)
1295 FOR_EACH_PTR(fn
->arguments
, s
) {
1296 struct symbol
*arg
= evaluate_symbol(s
);
1297 /* Array/function arguments silently degenerate into pointers */
1303 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1304 if (arg
->type
== SYM_ARRAY
)
1305 ptr
->ctype
= arg
->ctype
;
1307 ptr
->ctype
.base_type
= arg
;
1308 ptr
->ctype
.as
|= s
->ctype
.as
;
1309 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
& MOD_PTRINHERIT
;
1311 s
->ctype
.base_type
= ptr
;
1313 s
->ctype
.modifiers
&= ~MOD_PTRINHERIT
;
1316 examine_symbol_type(s
);
1323 } END_FOR_EACH_PTR(s
);
1326 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1328 /* Take the modifiers of the pointer, and apply them to the member */
1329 mod
|= sym
->ctype
.modifiers
;
1330 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1331 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1333 newsym
->ctype
.as
= as
;
1334 newsym
->ctype
.modifiers
= mod
;
1340 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1342 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1343 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1345 node
->ctype
.base_type
= ptr
;
1346 ptr
->bit_size
= bits_in_pointer
;
1347 ptr
->ctype
.alignment
= pointer_alignment
;
1349 node
->bit_size
= bits_in_pointer
;
1350 node
->ctype
.alignment
= pointer_alignment
;
1353 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1354 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1355 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1357 if (sym
->type
== SYM_NODE
) {
1358 ptr
->ctype
.as
|= sym
->ctype
.as
;
1359 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1360 sym
= sym
->ctype
.base_type
;
1362 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1363 ptr
->ctype
.as
|= sym
->ctype
.as
;
1364 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1365 sym
= sym
->ctype
.base_type
;
1367 ptr
->ctype
.base_type
= sym
;
1372 /* Arrays degenerate into pointers on pointer arithmetic */
1373 static struct symbol
*degenerate(struct expression
*expr
)
1375 struct symbol
*ctype
, *base
;
1379 ctype
= expr
->ctype
;
1382 base
= examine_symbol_type(ctype
);
1383 if (ctype
->type
== SYM_NODE
)
1384 base
= ctype
->ctype
.base_type
;
1386 * Arrays degenerate into pointers to the entries, while
1387 * functions degenerate into pointers to themselves.
1388 * If array was part of non-lvalue compound, we create a copy
1389 * of that compound first and then act as if we were dealing with
1390 * the corresponding field in there.
1392 switch (base
->type
) {
1394 if (expr
->type
== EXPR_SLICE
) {
1395 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1396 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1398 a
->ctype
.base_type
= expr
->base
->ctype
;
1399 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1400 a
->array_size
= expr
->base
->ctype
->array_size
;
1402 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1404 e0
->ctype
= &lazy_ptr_ctype
;
1406 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1409 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1411 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1413 e2
->right
= expr
->base
;
1415 e2
->ctype
= expr
->base
->ctype
;
1417 if (expr
->r_bitpos
) {
1418 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1421 e3
->right
= alloc_const_expression(expr
->pos
,
1422 expr
->r_bitpos
>> 3);
1423 e3
->ctype
= &lazy_ptr_ctype
;
1428 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1431 e4
->ctype
= &lazy_ptr_ctype
;
1434 expr
->type
= EXPR_PREOP
;
1438 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1439 expression_error(expr
, "strange non-value function or array");
1442 *expr
= *expr
->unop
;
1443 ctype
= create_pointer(expr
, ctype
, 1);
1444 expr
->ctype
= ctype
;
1451 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1453 struct expression
*op
= expr
->unop
;
1454 struct symbol
*ctype
;
1456 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1457 expression_error(expr
, "not addressable");
1464 if (expr
->type
== EXPR_SYMBOL
) {
1465 struct symbol
*sym
= expr
->symbol
;
1466 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1470 * symbol expression evaluation is lazy about the type
1471 * of the sub-expression, so we may have to generate
1472 * the type here if so..
1474 if (expr
->ctype
== &lazy_ptr_ctype
) {
1475 ctype
= create_pointer(expr
, ctype
, 0);
1476 expr
->ctype
= ctype
;
1482 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1484 struct expression
*op
= expr
->unop
;
1485 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1487 /* Simplify: *&(expr) => (expr) */
1488 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 (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1570 if (is_int_type(ctype
)) {
1571 struct symbol
*rtype
= rtype
= integer_promotion(ctype
);
1572 expr
->unop
= cast_to(expr
->unop
, rtype
);
1574 } else if (is_float_type(ctype
) && expr
->op
!= '~') {
1575 /* no conversions needed */
1576 } else if (is_restricted_type(ctype
) && !restricted_unop(expr
->op
, &ctype
)) {
1577 /* no conversions needed */
1578 } else if (is_fouled_type(ctype
) && !restricted_unop(expr
->op
, &ctype
)) {
1579 /* no conversions needed */
1581 return bad_expr_type(expr
);
1583 if (expr
->op
== '+')
1584 *expr
= *expr
->unop
;
1585 expr
->ctype
= ctype
;
1589 static struct symbol
*evaluate_preop(struct expression
*expr
)
1591 struct symbol
*ctype
= expr
->unop
->ctype
;
1595 *expr
= *expr
->unop
;
1601 return evaluate_sign(expr
);
1604 return evaluate_dereference(expr
);
1607 return evaluate_addressof(expr
);
1609 case SPECIAL_INCREMENT
:
1610 case SPECIAL_DECREMENT
:
1612 * From a type evaluation standpoint the preops are
1613 * the same as the postops
1615 return evaluate_postop(expr
);
1618 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1620 if (is_safe_type(ctype
))
1621 warning(expr
->pos
, "testing a 'safe expression'");
1622 if (is_float_type(ctype
)) {
1623 struct expression
*arg
= expr
->unop
;
1624 expr
->type
= EXPR_BINOP
;
1625 expr
->op
= SPECIAL_EQUAL
;
1627 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1628 expr
->right
->ctype
= ctype
;
1629 expr
->right
->fvalue
= 0;
1630 } else if (is_fouled_type(ctype
)) {
1631 warning(expr
->pos
, "restricted degrades to integer");
1633 ctype
= &bool_ctype
;
1639 expr
->ctype
= ctype
;
1643 static struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1645 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1646 struct ptr_list
*list
= head
;
1652 for (i
= 0; i
< list
->nr
; i
++) {
1653 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1655 if (sym
->ident
!= ident
)
1657 *offset
= sym
->offset
;
1660 struct symbol
*ctype
= sym
->ctype
.base_type
;
1664 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1666 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1669 *offset
+= sym
->offset
;
1673 } while ((list
= list
->next
) != head
);
1677 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1679 struct expression
*add
;
1682 * Create a new add-expression
1684 * NOTE! Even if we just add zero, we need a new node
1685 * for the member pointer, since it has a different
1686 * type than the original pointer. We could make that
1687 * be just a cast, but the fact is, a node is a node,
1688 * so we might as well just do the "add zero" here.
1690 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1693 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1694 add
->right
->ctype
= &int_ctype
;
1695 add
->right
->value
= offset
;
1698 * The ctype of the pointer will be lazily evaluated if
1699 * we ever take the address of this member dereference..
1701 add
->ctype
= &lazy_ptr_ctype
;
1705 /* structure/union dereference */
1706 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1709 struct symbol
*ctype
, *member
;
1710 struct expression
*deref
= expr
->deref
, *add
;
1711 struct ident
*ident
= expr
->member
;
1715 if (!evaluate_expression(deref
))
1718 expression_error(expr
, "bad member name");
1722 ctype
= deref
->ctype
;
1723 address_space
= ctype
->ctype
.as
;
1724 mod
= ctype
->ctype
.modifiers
;
1725 if (ctype
->type
== SYM_NODE
) {
1726 ctype
= ctype
->ctype
.base_type
;
1727 address_space
|= ctype
->ctype
.as
;
1728 mod
|= ctype
->ctype
.modifiers
;
1730 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1731 expression_error(expr
, "expected structure or union");
1734 examine_symbol_type(ctype
);
1736 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1738 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1739 const char *name
= "<unnamed>";
1742 name
= ctype
->ident
->name
;
1743 namelen
= ctype
->ident
->len
;
1745 if (ctype
->symbol_list
)
1746 expression_error(expr
, "no member '%s' in %s %.*s",
1747 show_ident(ident
), type
, namelen
, name
);
1749 expression_error(expr
, "using member '%s' in "
1750 "incomplete %s %.*s", show_ident(ident
),
1751 type
, namelen
, name
);
1756 * The member needs to take on the address space and modifiers of
1757 * the "parent" type.
1759 member
= convert_to_as_mod(member
, address_space
, mod
);
1760 ctype
= get_base_type(member
);
1762 if (!lvalue_expression(deref
)) {
1763 if (deref
->type
!= EXPR_SLICE
) {
1767 expr
->base
= deref
->base
;
1768 expr
->r_bitpos
= deref
->r_bitpos
;
1770 expr
->r_bitpos
+= offset
<< 3;
1771 expr
->type
= EXPR_SLICE
;
1772 expr
->r_nrbits
= member
->bit_size
;
1773 expr
->r_bitpos
+= member
->bit_offset
;
1774 expr
->ctype
= member
;
1778 deref
= deref
->unop
;
1779 expr
->deref
= deref
;
1781 add
= evaluate_offset(deref
, offset
);
1782 expr
->type
= EXPR_PREOP
;
1786 expr
->ctype
= member
;
1790 static int is_promoted(struct expression
*expr
)
1793 switch (expr
->type
) {
1796 case EXPR_CONDITIONAL
:
1820 static struct symbol
*evaluate_cast(struct expression
*);
1822 static struct symbol
*evaluate_type_information(struct expression
*expr
)
1824 struct symbol
*sym
= expr
->cast_type
;
1826 sym
= evaluate_expression(expr
->cast_expression
);
1830 * Expressions of restricted types will possibly get
1831 * promoted - check that here
1833 if (is_restricted_type(sym
)) {
1834 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
1836 } else if (is_fouled_type(sym
)) {
1840 examine_symbol_type(sym
);
1841 if (is_bitfield_type(sym
)) {
1842 expression_error(expr
, "trying to examine bitfield type");
1848 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1850 struct symbol
*type
;
1853 type
= evaluate_type_information(expr
);
1857 size
= type
->bit_size
;
1858 if ((size
< 0) || (size
& 7))
1859 expression_error(expr
, "cannot size expression");
1860 expr
->type
= EXPR_VALUE
;
1861 expr
->value
= size
>> 3;
1862 expr
->ctype
= size_t_ctype
;
1863 return size_t_ctype
;
1866 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
1868 struct symbol
*type
;
1871 type
= evaluate_type_information(expr
);
1875 if (type
->type
== SYM_NODE
)
1876 type
= type
->ctype
.base_type
;
1879 switch (type
->type
) {
1883 type
= get_base_type(type
);
1887 expression_error(expr
, "expected pointer expression");
1890 size
= type
->bit_size
;
1893 expr
->type
= EXPR_VALUE
;
1894 expr
->value
= size
>> 3;
1895 expr
->ctype
= size_t_ctype
;
1896 return size_t_ctype
;
1899 static struct symbol
*evaluate_alignof(struct expression
*expr
)
1901 struct symbol
*type
;
1903 type
= evaluate_type_information(expr
);
1907 expr
->type
= EXPR_VALUE
;
1908 expr
->value
= type
->ctype
.alignment
;
1909 expr
->ctype
= size_t_ctype
;
1910 return size_t_ctype
;
1913 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1915 struct expression
*expr
;
1916 struct symbol_list
*argument_types
= fn
->arguments
;
1917 struct symbol
*argtype
;
1920 PREPARE_PTR_LIST(argument_types
, argtype
);
1921 FOR_EACH_PTR (head
, expr
) {
1922 struct expression
**p
= THIS_ADDRESS(expr
);
1923 struct symbol
*ctype
, *target
;
1924 ctype
= evaluate_expression(expr
);
1931 struct symbol
*type
;
1932 int class = classify_type(ctype
, &type
);
1933 if (is_int(class)) {
1934 *p
= cast_to(expr
, integer_promotion(type
));
1935 } else if (class & TYPE_FLOAT
) {
1936 unsigned long mod
= type
->ctype
.modifiers
;
1937 if (!(mod
& (MOD_LONG
|MOD_LONGLONG
)))
1938 *p
= cast_to(expr
, &double_ctype
);
1939 } else if (class & TYPE_PTR
) {
1943 static char where
[30];
1944 examine_symbol_type(target
);
1945 sprintf(where
, "argument %d", i
);
1946 compatible_assignment_types(expr
, target
, p
, where
);
1950 NEXT_PTR_LIST(argtype
);
1951 } END_FOR_EACH_PTR(expr
);
1952 FINISH_PTR_LIST(argtype
);
1956 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
1960 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
1961 if (sym
->ident
== ident
)
1963 } END_FOR_EACH_PTR(sym
);
1967 static void convert_index(struct expression
*e
)
1969 struct expression
*child
= e
->idx_expression
;
1970 unsigned from
= e
->idx_from
;
1971 unsigned to
= e
->idx_to
+ 1;
1973 e
->init_offset
= from
* (e
->ctype
->bit_size
>>3);
1974 e
->init_nr
= to
- from
;
1975 e
->init_expr
= child
;
1978 static void convert_ident(struct expression
*e
)
1980 struct expression
*child
= e
->ident_expression
;
1981 struct symbol
*sym
= e
->field
;
1983 e
->init_offset
= sym
->offset
;
1985 e
->init_expr
= child
;
1988 static void convert_designators(struct expression
*e
)
1991 if (e
->type
== EXPR_INDEX
)
1993 else if (e
->type
== EXPR_IDENTIFIER
)
2001 static void excess(struct expression
*e
, const char *s
)
2003 warning(e
->pos
, "excessive elements in %s initializer", s
);
2007 * implicit designator for the first element
2009 static struct expression
*first_subobject(struct symbol
*ctype
, int class,
2010 struct expression
**v
)
2012 struct expression
*e
= *v
, *new;
2014 if (ctype
->type
== SYM_NODE
)
2015 ctype
= ctype
->ctype
.base_type
;
2017 if (class & TYPE_PTR
) { /* array */
2018 if (!ctype
->bit_size
)
2020 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2021 new->idx_expression
= e
;
2022 new->ctype
= ctype
->ctype
.base_type
;
2024 struct symbol
*field
, *p
;
2025 PREPARE_PTR_LIST(ctype
->symbol_list
, p
);
2026 while (p
&& !p
->ident
&& is_bitfield_type(p
))
2032 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2033 new->ident_expression
= e
;
2034 new->field
= new->ctype
= field
;
2041 * sanity-check explicit designators; return the innermost one or NULL
2042 * in case of error. Assign types.
2044 static struct expression
*check_designators(struct expression
*e
,
2045 struct symbol
*ctype
)
2047 struct expression
*last
= NULL
;
2050 if (ctype
->type
== SYM_NODE
)
2051 ctype
= ctype
->ctype
.base_type
;
2052 if (e
->type
== EXPR_INDEX
) {
2053 struct symbol
*type
;
2054 if (ctype
->type
!= SYM_ARRAY
) {
2055 err
= "array index in non-array";
2058 type
= ctype
->ctype
.base_type
;
2059 if (ctype
->bit_size
>= 0 && type
->bit_size
>= 0) {
2060 unsigned offset
= e
->idx_to
* type
->bit_size
;
2061 if (offset
>= ctype
->bit_size
) {
2062 err
= "index out of bounds in";
2066 e
->ctype
= ctype
= type
;
2069 if (!e
->idx_expression
) {
2073 e
= e
->idx_expression
;
2074 } else if (e
->type
== EXPR_IDENTIFIER
) {
2075 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2076 err
= "field name not in struct or union";
2079 ctype
= find_struct_ident(ctype
, e
->expr_ident
);
2081 err
= "unknown field name in";
2084 e
->field
= e
->ctype
= ctype
;
2086 if (!e
->ident_expression
) {
2090 e
= e
->ident_expression
;
2091 } else if (e
->type
== EXPR_POS
) {
2092 err
= "internal front-end error: EXPR_POS in";
2097 expression_error(e
, "%s initializer", err
);
2102 * choose the next subobject to initialize.
2104 * Get designators for next element, switch old ones to EXPR_POS.
2105 * Return the resulting expression or NULL if we'd run out of subobjects.
2106 * The innermost designator is returned in *v. Designators in old
2107 * are assumed to be already sanity-checked.
2109 static struct expression
*next_designators(struct expression
*old
,
2110 struct symbol
*ctype
,
2111 struct expression
*e
, struct expression
**v
)
2113 struct expression
*new = NULL
;
2117 if (old
->type
== EXPR_INDEX
) {
2118 struct expression
*copy
;
2121 copy
= next_designators(old
->idx_expression
,
2124 n
= old
->idx_to
+ 1;
2125 if (n
* old
->ctype
->bit_size
== ctype
->bit_size
) {
2130 *v
= new = alloc_expression(e
->pos
, EXPR_INDEX
);
2133 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2136 new->idx_from
= new->idx_to
= n
;
2137 new->idx_expression
= copy
;
2138 new->ctype
= old
->ctype
;
2140 } else if (old
->type
== EXPR_IDENTIFIER
) {
2141 struct expression
*copy
;
2142 struct symbol
*field
;
2144 copy
= next_designators(old
->ident_expression
,
2147 field
= old
->field
->next_subobject
;
2153 *v
= new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2156 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2160 new->expr_ident
= field
->ident
;
2161 new->ident_expression
= copy
;
2168 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2169 int class, struct symbol
*ctype
);
2172 * deal with traversing subobjects [6.7.8(17,18,20)]
2174 static void handle_list_initializer(struct expression
*expr
,
2175 int class, struct symbol
*ctype
)
2177 struct expression
*e
, *last
= NULL
, *top
= NULL
, *next
;
2180 FOR_EACH_PTR(expr
->expr_list
, e
) {
2181 struct expression
**v
;
2182 struct symbol
*type
;
2185 if (e
->type
!= EXPR_INDEX
&& e
->type
!= EXPR_IDENTIFIER
) {
2188 last
= first_subobject(ctype
, class, &top
);
2190 last
= next_designators(last
, ctype
, e
, &top
);
2193 excess(e
, class & TYPE_PTR
? "array" :
2195 DELETE_CURRENT_PTR(e
);
2199 warning(e
->pos
, "advancing past deep designator");
2202 REPLACE_CURRENT_PTR(e
, last
);
2204 next
= check_designators(e
, ctype
);
2206 DELETE_CURRENT_PTR(e
);
2210 /* deeper than one designator? */
2212 convert_designators(last
);
2217 lclass
= classify_type(top
->ctype
, &type
);
2218 if (top
->type
== EXPR_INDEX
)
2219 v
= &top
->idx_expression
;
2221 v
= &top
->ident_expression
;
2223 if (handle_simple_initializer(v
, 1, lclass
, top
->ctype
))
2226 if (!(lclass
& TYPE_COMPOUND
)) {
2227 warning(e
->pos
, "bogus scalar initializer");
2228 DELETE_CURRENT_PTR(e
);
2232 next
= first_subobject(type
, lclass
, v
);
2234 warning(e
->pos
, "missing braces around initializer");
2239 DELETE_CURRENT_PTR(e
);
2240 excess(e
, lclass
& TYPE_PTR
? "array" : "struct or union");
2242 } END_FOR_EACH_PTR(e
);
2244 convert_designators(last
);
2245 expr
->ctype
= ctype
;
2248 static int is_string_literal(struct expression
**v
)
2250 struct expression
*e
= *v
;
2251 while (e
&& e
->type
== EXPR_PREOP
&& e
->op
== '(')
2253 if (!e
|| e
->type
!= EXPR_STRING
)
2255 if (e
!= *v
&& Wparen_string
)
2257 "array initialized from parenthesized string constant");
2263 * We want a normal expression, possibly in one layer of braces. Warn
2264 * if the latter happens inside a list (it's legal, but likely to be
2265 * an effect of screwup). In case of anything not legal, we are definitely
2266 * having an effect of screwup, so just fail and let the caller warn.
2268 static struct expression
*handle_scalar(struct expression
*e
, int nested
)
2270 struct expression
*v
= NULL
, *p
;
2274 if (e
->type
!= EXPR_INITIALIZER
)
2277 FOR_EACH_PTR(e
->expr_list
, p
) {
2281 } END_FOR_EACH_PTR(p
);
2285 case EXPR_INITIALIZER
:
2287 case EXPR_IDENTIFIER
:
2293 warning(e
->pos
, "braces around scalar initializer");
2298 * deal with the cases that don't care about subobjects:
2299 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2300 * character array <- string literal, possibly in braces [6.7.8(14)]
2301 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2302 * compound type <- initializer list in braces [6.7.8(16)]
2303 * The last one punts to handle_list_initializer() which, in turn will call
2304 * us for individual elements of the list.
2306 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2307 * the lack of support of wide char stuff in general.
2309 * One note: we need to take care not to evaluate a string literal until
2310 * we know that we *will* handle it right here. Otherwise we would screw
2311 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2312 * { "string", ...} - we need to preserve that string literal recognizable
2313 * until we dig into the inner struct.
2315 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2316 int class, struct symbol
*ctype
)
2318 int is_string
= is_string_type(ctype
);
2319 struct expression
*e
= *ep
, *p
;
2320 struct symbol
*type
;
2326 if (!(class & TYPE_COMPOUND
)) {
2327 e
= handle_scalar(e
, nested
);
2331 if (!evaluate_expression(e
))
2333 compatible_assignment_types(e
, ctype
, ep
, "initializer");
2338 * sublist; either a string, or we dig in; the latter will deal with
2339 * pathologies, so we don't need anything fancy here.
2341 if (e
->type
== EXPR_INITIALIZER
) {
2343 struct expression
*v
= NULL
;
2346 FOR_EACH_PTR(e
->expr_list
, p
) {
2350 } END_FOR_EACH_PTR(p
);
2351 if (count
== 1 && is_string_literal(&v
)) {
2356 handle_list_initializer(e
, class, ctype
);
2361 if (is_string_literal(&e
)) {
2362 /* either we are doing array of char, or we'll have to dig in */
2369 /* struct or union can be initialized by compatible */
2370 if (class != TYPE_COMPOUND
)
2372 type
= evaluate_expression(e
);
2375 if (ctype
->type
== SYM_NODE
)
2376 ctype
= ctype
->ctype
.base_type
;
2377 if (type
->type
== SYM_NODE
)
2378 type
= type
->ctype
.base_type
;
2384 p
= alloc_expression(e
->pos
, EXPR_STRING
);
2386 type
= evaluate_expression(p
);
2387 if (ctype
->bit_size
!= -1 &&
2388 ctype
->bit_size
+ bits_in_char
< type
->bit_size
) {
2390 "too long initializer-string for array of char");
2396 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2398 struct symbol
*type
;
2399 int class = classify_type(ctype
, &type
);
2400 if (!handle_simple_initializer(ep
, 0, class, ctype
))
2401 expression_error(*ep
, "invalid initializer");
2404 static int get_as(struct symbol
*sym
)
2412 mod
= sym
->ctype
.modifiers
;
2413 if (sym
->type
== SYM_NODE
) {
2414 sym
= sym
->ctype
.base_type
;
2415 as
|= sym
->ctype
.as
;
2416 mod
|= sym
->ctype
.modifiers
;
2420 * At least for now, allow casting to a "unsigned long".
2421 * That's how we do things like pointer arithmetic and
2422 * store pointers to registers.
2424 if (sym
== &ulong_ctype
)
2427 if (sym
&& sym
->type
== SYM_PTR
) {
2428 sym
= get_base_type(sym
);
2429 as
|= sym
->ctype
.as
;
2430 mod
|= sym
->ctype
.modifiers
;
2432 if (mod
& MOD_FORCE
)
2437 static void cast_to_as(struct expression
*e
, int as
)
2439 struct expression
*v
= e
->cast_expression
;
2440 struct symbol
*type
= v
->ctype
;
2442 if (!Wcast_to_address_space
)
2445 if (v
->type
!= EXPR_VALUE
|| v
->value
)
2448 /* cast from constant 0 to pointer is OK */
2449 if (is_int_type(type
))
2452 if (type
->type
== SYM_NODE
)
2453 type
= type
->ctype
.base_type
;
2455 if (type
->type
== SYM_PTR
&& type
->ctype
.base_type
== &void_ctype
)
2459 warning(e
->pos
, "cast adds address space to expression (<asn:%d>)", as
);
2462 static struct symbol
*evaluate_cast(struct expression
*expr
)
2464 struct expression
*target
= expr
->cast_expression
;
2465 struct symbol
*ctype
;
2466 struct symbol
*t1
, *t2
;
2474 * Special case: a cast can be followed by an
2475 * initializer, in which case we need to pass
2476 * the type value down to that initializer rather
2477 * than trying to evaluate it as an expression
2479 * A more complex case is when the initializer is
2480 * dereferenced as part of a post-fix expression.
2481 * We need to produce an expression that can be dereferenced.
2483 if (target
->type
== EXPR_INITIALIZER
) {
2484 struct symbol
*sym
= expr
->cast_type
;
2485 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2487 sym
->initializer
= target
;
2488 evaluate_symbol(sym
);
2490 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2493 expr
->type
= EXPR_PREOP
;
2501 ctype
= examine_symbol_type(expr
->cast_type
);
2502 expr
->ctype
= ctype
;
2503 expr
->cast_type
= ctype
;
2505 evaluate_expression(target
);
2508 class1
= classify_type(ctype
, &t1
);
2510 /* cast to non-integer type -> not an integer constant expression */
2511 if (!is_int(class1
))
2513 /* if argument turns out to be not an integer constant expression *and*
2514 it was not a floating literal to start with -> too bad */
2515 else if (expr
->flags
== Int_const_expr
&&
2516 !(target
->flags
& Int_const_expr
))
2519 * You can always throw a value away by casting to
2520 * "void" - that's an implicit "force". Note that
2521 * the same is _not_ true of "void *".
2523 if (t1
== &void_ctype
)
2526 if (class1
& TYPE_COMPOUND
)
2527 warning(expr
->pos
, "cast to non-scalar");
2531 expression_error(expr
, "cast from unknown type");
2534 class2
= classify_type(t2
, &t2
);
2536 if (class2
& TYPE_COMPOUND
)
2537 warning(expr
->pos
, "cast from non-scalar");
2539 /* allowed cast unfouls */
2540 if (class2
& TYPE_FOULED
)
2541 t2
= t2
->ctype
.base_type
;
2543 if (!(ctype
->ctype
.modifiers
& MOD_FORCE
) && t1
!= t2
) {
2544 if (class1
& TYPE_RESTRICT
)
2545 warning(expr
->pos
, "cast to restricted type");
2546 if (class2
& TYPE_RESTRICT
)
2547 warning(expr
->pos
, "cast from restricted type");
2550 as1
= get_as(ctype
);
2551 as2
= get_as(target
->ctype
);
2552 if (!as1
&& as2
> 0)
2553 warning(expr
->pos
, "cast removes address space of expression");
2554 if (as1
> 0 && as2
> 0 && as1
!= as2
)
2555 warning(expr
->pos
, "cast between address spaces (<asn:%d>-><asn:%d>)", as2
, as1
);
2556 if (as1
> 0 && !as2
)
2557 cast_to_as(expr
, as1
);
2560 * Casts of constant values are special: they
2561 * can be NULL, and thus need to be simplified
2564 if (target
->type
== EXPR_VALUE
)
2565 cast_value(expr
, ctype
, target
, target
->ctype
);
2572 * Evaluate a call expression with a symbol. This
2573 * should expand inline functions, and evaluate
2576 static int evaluate_symbol_call(struct expression
*expr
)
2578 struct expression
*fn
= expr
->fn
;
2579 struct symbol
*ctype
= fn
->ctype
;
2581 if (fn
->type
!= EXPR_PREOP
)
2584 if (ctype
->op
&& ctype
->op
->evaluate
)
2585 return ctype
->op
->evaluate(expr
);
2587 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2589 struct symbol
*curr
= current_fn
;
2590 current_fn
= ctype
->ctype
.base_type
;
2592 ret
= inline_function(expr
, ctype
);
2594 /* restore the old function */
2602 static struct symbol
*evaluate_call(struct expression
*expr
)
2605 struct symbol
*ctype
, *sym
;
2606 struct expression
*fn
= expr
->fn
;
2607 struct expression_list
*arglist
= expr
->args
;
2609 if (!evaluate_expression(fn
))
2611 sym
= ctype
= fn
->ctype
;
2612 if (ctype
->type
== SYM_NODE
)
2613 ctype
= ctype
->ctype
.base_type
;
2614 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
2615 ctype
= get_base_type(ctype
);
2617 examine_fn_arguments(ctype
);
2618 if (sym
->type
== SYM_NODE
&& fn
->type
== EXPR_PREOP
&&
2619 sym
->op
&& sym
->op
->args
) {
2620 if (!sym
->op
->args(expr
))
2623 if (!evaluate_arguments(sym
, ctype
, arglist
))
2625 if (ctype
->type
!= SYM_FN
) {
2626 expression_error(expr
, "not a function %s",
2627 show_ident(sym
->ident
));
2630 args
= expression_list_size(expr
->args
);
2631 fnargs
= symbol_list_size(ctype
->arguments
);
2633 expression_error(expr
,
2634 "not enough arguments for function %s",
2635 show_ident(sym
->ident
));
2636 if (args
> fnargs
&& !ctype
->variadic
)
2637 expression_error(expr
,
2638 "too many arguments for function %s",
2639 show_ident(sym
->ident
));
2641 if (sym
->type
== SYM_NODE
) {
2642 if (evaluate_symbol_call(expr
))
2645 expr
->ctype
= ctype
->ctype
.base_type
;
2649 static struct symbol
*evaluate_offsetof(struct expression
*expr
)
2651 struct expression
*e
= expr
->down
;
2652 struct symbol
*ctype
= expr
->in
;
2655 if (expr
->op
== '.') {
2656 struct symbol
*field
;
2659 expression_error(expr
, "expected structure or union");
2662 examine_symbol_type(ctype
);
2663 class = classify_type(ctype
, &ctype
);
2664 if (class != TYPE_COMPOUND
) {
2665 expression_error(expr
, "expected structure or union");
2669 field
= find_identifier(expr
->ident
, ctype
->symbol_list
, &offset
);
2671 expression_error(expr
, "unknown member");
2675 expr
->type
= EXPR_VALUE
;
2676 expr
->flags
= Int_const_expr
;
2677 expr
->value
= offset
;
2678 expr
->ctype
= size_t_ctype
;
2681 expression_error(expr
, "expected structure or union");
2684 examine_symbol_type(ctype
);
2685 class = classify_type(ctype
, &ctype
);
2686 if (class != (TYPE_COMPOUND
| TYPE_PTR
)) {
2687 expression_error(expr
, "expected array");
2690 ctype
= ctype
->ctype
.base_type
;
2692 expr
->type
= EXPR_VALUE
;
2693 expr
->flags
= Int_const_expr
;
2695 expr
->ctype
= size_t_ctype
;
2697 struct expression
*idx
= expr
->index
, *m
;
2698 struct symbol
*i_type
= evaluate_expression(idx
);
2699 int i_class
= classify_type(i_type
, &i_type
);
2700 if (!is_int(i_class
)) {
2701 expression_error(expr
, "non-integer index");
2704 unrestrict(idx
, i_class
, &i_type
);
2705 idx
= cast_to(idx
, size_t_ctype
);
2706 m
= alloc_const_expression(expr
->pos
,
2707 ctype
->bit_size
>> 3);
2708 m
->ctype
= size_t_ctype
;
2709 m
->flags
= Int_const_expr
;
2710 expr
->type
= EXPR_BINOP
;
2714 expr
->ctype
= size_t_ctype
;
2715 expr
->flags
= m
->flags
& idx
->flags
& Int_const_expr
;
2719 struct expression
*copy
= __alloc_expression(0);
2721 if (e
->type
== EXPR_OFFSETOF
)
2723 if (!evaluate_expression(e
))
2725 expr
->type
= EXPR_BINOP
;
2726 expr
->flags
= e
->flags
& copy
->flags
& Int_const_expr
;
2728 expr
->ctype
= size_t_ctype
;
2732 return size_t_ctype
;
2735 struct symbol
*evaluate_expression(struct expression
*expr
)
2742 switch (expr
->type
) {
2745 expression_error(expr
, "value expression without a type");
2748 return evaluate_string(expr
);
2750 return evaluate_symbol_expression(expr
);
2752 if (!evaluate_expression(expr
->left
))
2754 if (!evaluate_expression(expr
->right
))
2756 return evaluate_binop(expr
);
2758 return evaluate_logical(expr
);
2760 evaluate_expression(expr
->left
);
2761 if (!evaluate_expression(expr
->right
))
2763 return evaluate_comma(expr
);
2765 if (!evaluate_expression(expr
->left
))
2767 if (!evaluate_expression(expr
->right
))
2769 return evaluate_compare(expr
);
2770 case EXPR_ASSIGNMENT
:
2771 if (!evaluate_expression(expr
->left
))
2773 if (!evaluate_expression(expr
->right
))
2775 return evaluate_assignment(expr
);
2777 if (!evaluate_expression(expr
->unop
))
2779 return evaluate_preop(expr
);
2781 if (!evaluate_expression(expr
->unop
))
2783 return evaluate_postop(expr
);
2785 case EXPR_IMPLIED_CAST
:
2786 return evaluate_cast(expr
);
2788 return evaluate_sizeof(expr
);
2789 case EXPR_PTRSIZEOF
:
2790 return evaluate_ptrsizeof(expr
);
2792 return evaluate_alignof(expr
);
2794 return evaluate_member_dereference(expr
);
2796 return evaluate_call(expr
);
2798 case EXPR_CONDITIONAL
:
2799 return evaluate_conditional_expression(expr
);
2800 case EXPR_STATEMENT
:
2801 expr
->ctype
= evaluate_statement(expr
->statement
);
2805 expr
->ctype
= &ptr_ctype
;
2809 /* Evaluate the type of the symbol .. */
2810 evaluate_symbol(expr
->symbol
);
2811 /* .. but the type of the _expression_ is a "type" */
2812 expr
->ctype
= &type_ctype
;
2816 return evaluate_offsetof(expr
);
2818 /* These can not exist as stand-alone expressions */
2819 case EXPR_INITIALIZER
:
2820 case EXPR_IDENTIFIER
:
2823 expression_error(expr
, "internal front-end error: initializer in expression");
2826 expression_error(expr
, "internal front-end error: SLICE re-evaluated");
2832 static void check_duplicates(struct symbol
*sym
)
2835 struct symbol
*next
= sym
;
2837 while ((next
= next
->same_symbol
) != NULL
) {
2838 const char *typediff
;
2839 evaluate_symbol(next
);
2841 typediff
= type_difference(sym
, next
, 0, 0);
2843 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2844 show_ident(sym
->ident
),
2845 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
2850 unsigned long mod
= sym
->ctype
.modifiers
;
2851 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
2853 if (!(mod
& MOD_TOPLEVEL
))
2857 if (sym
->ident
== &main_ident
)
2859 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
2863 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
2865 struct symbol
*base_type
;
2873 sym
= examine_symbol_type(sym
);
2874 base_type
= get_base_type(sym
);
2878 /* Evaluate the initializers */
2879 if (sym
->initializer
)
2880 evaluate_initializer(sym
, &sym
->initializer
);
2882 /* And finally, evaluate the body of the symbol too */
2883 if (base_type
->type
== SYM_FN
) {
2884 struct symbol
*curr
= current_fn
;
2886 current_fn
= base_type
;
2888 examine_fn_arguments(base_type
);
2889 if (!base_type
->stmt
&& base_type
->inline_stmt
)
2891 if (base_type
->stmt
)
2892 evaluate_statement(base_type
->stmt
);
2900 void evaluate_symbol_list(struct symbol_list
*list
)
2904 FOR_EACH_PTR(list
, sym
) {
2905 evaluate_symbol(sym
);
2906 check_duplicates(sym
);
2907 } END_FOR_EACH_PTR(sym
);
2910 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
2912 struct expression
*expr
= stmt
->expression
;
2913 struct symbol
*fntype
;
2915 evaluate_expression(expr
);
2916 fntype
= current_fn
->ctype
.base_type
;
2917 if (!fntype
|| fntype
== &void_ctype
) {
2918 if (expr
&& expr
->ctype
!= &void_ctype
)
2919 expression_error(expr
, "return expression in %s function", fntype
?"void":"typeless");
2920 if (expr
&& Wreturn_void
)
2921 warning(stmt
->pos
, "returning void-valued expression");
2926 sparse_error(stmt
->pos
, "return with no return value");
2931 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, "return expression");
2935 static void evaluate_if_statement(struct statement
*stmt
)
2937 if (!stmt
->if_conditional
)
2940 evaluate_conditional(stmt
->if_conditional
, 0);
2941 evaluate_statement(stmt
->if_true
);
2942 evaluate_statement(stmt
->if_false
);
2945 static void evaluate_iterator(struct statement
*stmt
)
2947 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
2948 evaluate_conditional(stmt
->iterator_post_condition
,1);
2949 evaluate_statement(stmt
->iterator_pre_statement
);
2950 evaluate_statement(stmt
->iterator_statement
);
2951 evaluate_statement(stmt
->iterator_post_statement
);
2954 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
2956 switch (*constraint
) {
2957 case '=': /* Assignment */
2958 case '+': /* Update */
2961 expression_error(expr
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
2965 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
2967 switch (*constraint
) {
2968 case '=': /* Assignment */
2969 case '+': /* Update */
2970 expression_error(expr
, "input constraint with assignment (\"%s\")", constraint
);
2974 static void evaluate_asm_statement(struct statement
*stmt
)
2976 struct expression
*expr
;
2979 expr
= stmt
->asm_string
;
2980 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2981 sparse_error(stmt
->pos
, "need constant string for inline asm");
2986 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
2987 struct ident
*ident
;
2990 case 0: /* Identifier */
2992 ident
= (struct ident
*)expr
;
2995 case 1: /* Constraint */
2997 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2998 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
2999 *THIS_ADDRESS(expr
) = NULL
;
3002 verify_output_constraint(expr
, expr
->string
->data
);
3005 case 2: /* Expression */
3007 if (!evaluate_expression(expr
))
3009 if (!lvalue_expression(expr
))
3010 warning(expr
->pos
, "asm output is not an lvalue");
3011 evaluate_assign_to(expr
, expr
->ctype
);
3014 } END_FOR_EACH_PTR(expr
);
3017 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
3018 struct ident
*ident
;
3021 case 0: /* Identifier */
3023 ident
= (struct ident
*)expr
;
3026 case 1: /* Constraint */
3028 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3029 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
3030 *THIS_ADDRESS(expr
) = NULL
;
3033 verify_input_constraint(expr
, expr
->string
->data
);
3036 case 2: /* Expression */
3038 if (!evaluate_expression(expr
))
3042 } END_FOR_EACH_PTR(expr
);
3044 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
3046 sparse_error(stmt
->pos
, "bad asm output");
3049 if (expr
->type
== EXPR_STRING
)
3051 expression_error(expr
, "asm clobber is not a string");
3052 } END_FOR_EACH_PTR(expr
);
3055 static void evaluate_case_statement(struct statement
*stmt
)
3057 evaluate_expression(stmt
->case_expression
);
3058 evaluate_expression(stmt
->case_to
);
3059 evaluate_statement(stmt
->case_statement
);
3062 static void check_case_type(struct expression
*switch_expr
,
3063 struct expression
*case_expr
,
3064 struct expression
**enumcase
)
3066 struct symbol
*switch_type
, *case_type
;
3072 switch_type
= switch_expr
->ctype
;
3073 case_type
= evaluate_expression(case_expr
);
3075 if (!switch_type
|| !case_type
)
3079 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
3080 else if (is_enum_type(case_type
))
3081 *enumcase
= case_expr
;
3084 sclass
= classify_type(switch_type
, &switch_type
);
3085 cclass
= classify_type(case_type
, &case_type
);
3087 /* both should be arithmetic */
3088 if (!(sclass
& cclass
& TYPE_NUM
))
3091 /* neither should be floating */
3092 if ((sclass
| cclass
) & TYPE_FLOAT
)
3095 /* if neither is restricted, we are OK */
3096 if (!((sclass
| cclass
) & TYPE_RESTRICT
))
3099 if (!restricted_binop_type(SPECIAL_EQUAL
, case_expr
, switch_expr
,
3100 cclass
, sclass
, case_type
, switch_type
))
3101 warning(case_expr
->pos
, "restricted degrades to integer");
3106 expression_error(case_expr
, "incompatible types for 'case' statement");
3109 static void evaluate_switch_statement(struct statement
*stmt
)
3112 struct expression
*enumcase
= NULL
;
3113 struct expression
**enumcase_holder
= &enumcase
;
3114 struct expression
*sel
= stmt
->switch_expression
;
3116 evaluate_expression(sel
);
3117 evaluate_statement(stmt
->switch_statement
);
3120 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
3121 enumcase_holder
= NULL
; /* Only check cases against switch */
3123 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
3124 struct statement
*case_stmt
= sym
->stmt
;
3125 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
3126 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
3127 } END_FOR_EACH_PTR(sym
);
3130 struct symbol
*evaluate_statement(struct statement
*stmt
)
3135 switch (stmt
->type
) {
3136 case STMT_DECLARATION
: {
3138 FOR_EACH_PTR(stmt
->declaration
, s
) {
3140 } END_FOR_EACH_PTR(s
);
3145 return evaluate_return_expression(stmt
);
3147 case STMT_EXPRESSION
:
3148 if (!evaluate_expression(stmt
->expression
))
3150 return degenerate(stmt
->expression
);
3152 case STMT_COMPOUND
: {
3153 struct statement
*s
;
3154 struct symbol
*type
= NULL
;
3156 /* Evaluate the return symbol in the compound statement */
3157 evaluate_symbol(stmt
->ret
);
3160 * Then, evaluate each statement, making the type of the
3161 * compound statement be the type of the last statement
3163 type
= evaluate_statement(stmt
->args
);
3164 FOR_EACH_PTR(stmt
->stmts
, s
) {
3165 type
= evaluate_statement(s
);
3166 } END_FOR_EACH_PTR(s
);
3172 evaluate_if_statement(stmt
);
3175 evaluate_iterator(stmt
);
3178 evaluate_switch_statement(stmt
);
3181 evaluate_case_statement(stmt
);
3184 return evaluate_statement(stmt
->label_statement
);
3186 evaluate_expression(stmt
->goto_expression
);
3191 evaluate_asm_statement(stmt
);
3194 evaluate_expression(stmt
->expression
);
3197 evaluate_expression(stmt
->range_expression
);
3198 evaluate_expression(stmt
->range_low
);
3199 evaluate_expression(stmt
->range_high
);