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 (old
->ctype
!= &null_ctype
&& 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 if (ctype
== &null_ctype
)
573 examine_symbol_type(ctype
);
575 if (!ctype
->ctype
.base_type
) {
576 expression_error(expr
, "missing type information");
580 /* Get the size of whatever the pointer points to */
581 bit_size
= ptr_object_size(ctype
);
582 multiply
= bit_size
>> 3;
586 if (multiply
== 1 && itype
->bit_size
>= bits_in_pointer
)
589 if (index
->type
== EXPR_VALUE
) {
590 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
591 unsigned long long v
= index
->value
, mask
;
592 mask
= 1ULL << (itype
->bit_size
- 1);
598 mask
= 1ULL << (bits_in_pointer
- 1);
599 v
&= mask
| (mask
- 1);
601 val
->ctype
= ssize_t_ctype
;
606 if (itype
->bit_size
< bits_in_pointer
)
607 index
= cast_to(index
, ssize_t_ctype
);
610 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
611 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
613 val
->ctype
= ssize_t_ctype
;
614 val
->value
= multiply
;
617 mul
->ctype
= ssize_t_ctype
;
627 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
628 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
631 unsigned long mod1
, mod2
, diff
;
632 unsigned long as1
, as2
;
634 struct symbol
*base1
, *base2
;
636 if (target
== source
)
638 if (!target
|| !source
)
639 return "different types";
641 * Peel of per-node information.
642 * FIXME! Check alignment and context too here!
644 mod1
= target
->ctype
.modifiers
;
645 as1
= target
->ctype
.as
;
646 mod2
= source
->ctype
.modifiers
;
647 as2
= source
->ctype
.as
;
648 if (target
->type
== SYM_NODE
) {
649 target
= target
->ctype
.base_type
;
652 if (target
->type
== SYM_PTR
) {
656 mod1
|= target
->ctype
.modifiers
;
657 as1
|= target
->ctype
.as
;
659 if (source
->type
== SYM_NODE
) {
660 source
= source
->ctype
.base_type
;
663 if (source
->type
== SYM_PTR
) {
667 mod2
|= source
->ctype
.modifiers
;
668 as2
|= source
->ctype
.as
;
670 if (target
->type
== SYM_ENUM
) {
671 target
= target
->ctype
.base_type
;
675 if (source
->type
== SYM_ENUM
) {
676 source
= source
->ctype
.base_type
;
681 if (target
== source
)
683 if (!target
|| !source
)
684 return "different types";
686 type1
= target
->type
;
687 base1
= target
->ctype
.base_type
;
689 type2
= source
->type
;
690 base2
= source
->ctype
.base_type
;
693 * Pointers to functions compare as the function itself
695 if (type1
== SYM_PTR
&& base1
) {
696 base1
= examine_symbol_type(base1
);
697 switch (base1
->type
) {
701 base1
= base1
->ctype
.base_type
;
706 if (type2
== SYM_PTR
&& base2
) {
707 base2
= examine_symbol_type(base2
);
708 switch (base2
->type
) {
712 base2
= base2
->ctype
.base_type
;
718 /* Arrays degenerate to pointers for type comparisons */
719 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
720 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
722 if (type1
!= type2
|| type1
== SYM_RESTRICT
)
723 return "different base types";
725 /* Must be same address space to be comparable */
726 if (Waddress_space
&& as1
!= as2
)
727 return "different address spaces";
729 /* Ignore differences in storage types or addressability */
730 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
731 diff
&= (mod1
& ~target_mod_ignore
) | (mod2
& ~source_mod_ignore
);
734 return "different type sizes";
735 if (diff
& ~MOD_SIGNEDNESS
)
736 return "different modifiers";
738 /* Differs in signedness only.. */
741 * Warn if both are explicitly signed ("unsigned" is obviously
742 * always explicit, and since we know one of them has to be
743 * unsigned, we check if the signed one was explicit).
745 if ((mod1
| mod2
) & MOD_EXPLICITLY_SIGNED
)
746 return "different explicit signedness";
749 * "char" matches both "unsigned char" and "signed char",
750 * so if the explicit test didn't trigger, then we should
751 * not warn about a char.
753 if (!(mod1
& MOD_CHAR
))
754 return "different signedness";
758 if (type1
== SYM_FN
) {
760 struct symbol
*arg1
, *arg2
;
761 if (base1
->variadic
!= base2
->variadic
)
762 return "incompatible variadic arguments";
763 PREPARE_PTR_LIST(target
->arguments
, arg1
);
764 PREPARE_PTR_LIST(source
->arguments
, arg2
);
768 diffstr
= type_difference(arg1
, arg2
, 0, 0);
770 static char argdiff
[80];
771 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diffstr
);
780 FINISH_PTR_LIST(arg2
);
781 FINISH_PTR_LIST(arg1
);
790 static void bad_null(struct expression
*expr
)
792 if (Wnon_pointer_null
)
793 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
;
959 expr
->flags
&= expr
->left
->flags
& expr
->right
->flags
;
963 static int modify_for_unsigned(int op
)
966 op
= SPECIAL_UNSIGNED_LT
;
968 op
= SPECIAL_UNSIGNED_GT
;
969 else if (op
== SPECIAL_LTE
)
970 op
= SPECIAL_UNSIGNED_LTE
;
971 else if (op
== SPECIAL_GTE
)
972 op
= SPECIAL_UNSIGNED_GTE
;
976 static inline int is_null_pointer_constant(struct expression
*e
)
978 if (e
->ctype
== &null_ctype
)
980 if (!(e
->flags
& Int_const_expr
))
982 return is_zero_constant(e
) ? 2 : 0;
985 static struct symbol
*evaluate_compare(struct expression
*expr
)
987 struct expression
*left
= expr
->left
, *right
= expr
->right
;
988 struct symbol
*ltype
, *rtype
;
989 int lclass
= classify_type(degenerate(left
), <ype
);
990 int rclass
= classify_type(degenerate(right
), &rtype
);
991 struct symbol
*ctype
;
992 const char *typediff
;
995 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
1000 if (is_type_type(ltype
) && is_type_type(rtype
))
1003 if (is_safe_type(left
->ctype
) || is_safe_type(right
->ctype
))
1004 warning(expr
->pos
, "testing a 'safe expression'");
1006 /* number on number */
1007 if (lclass
& rclass
& TYPE_NUM
) {
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
);
1017 /* at least one must be a pointer */
1018 if (!((lclass
| rclass
) & TYPE_PTR
))
1019 return bad_expr_type(expr
);
1021 /* equality comparisons can be with null pointer constants */
1022 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1023 int is_null1
= is_null_pointer_constant(left
);
1024 int is_null2
= is_null_pointer_constant(right
);
1029 if (is_null1
&& is_null2
) {
1030 int positive
= expr
->op
== SPECIAL_EQUAL
;
1031 expr
->type
= EXPR_VALUE
;
1032 expr
->value
= positive
;
1036 left
= cast_to(left
, rtype
);
1040 right
= cast_to(right
, ltype
);
1043 /* they also have special treatment for pointers to void */
1044 if (lclass
& rclass
& TYPE_PTR
) {
1045 if (get_base_type(ltype
) == &void_ctype
) {
1046 right
= cast_to(right
, ltype
);
1049 if (get_base_type(rtype
) == &void_ctype
) {
1050 left
= cast_to(left
, rtype
);
1055 /* both should be pointers */
1056 if (!(lclass
& rclass
& TYPE_PTR
))
1057 return bad_expr_type(expr
);
1059 expr
->op
= modify_for_unsigned(expr
->op
);
1060 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
1064 expression_error(expr
, "incompatible types in comparison expression (%s)", typediff
);
1068 expr
->ctype
= &bool_ctype
;
1073 * NOTE! The degenerate case of "x ? : y", where we don't
1074 * have a true case, this will possibly promote "x" to the
1075 * same type as "y", and thus _change_ the conditional
1076 * test in the expression. But since promotion is "safe"
1077 * for testing, that's OK.
1079 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
1081 struct expression
**true;
1082 struct symbol
*ctype
, *ltype
, *rtype
, *lbase
, *rbase
;
1084 const char * typediff
;
1087 if (!evaluate_conditional(expr
->conditional
, 0))
1089 if (!evaluate_expression(expr
->cond_false
))
1092 ctype
= degenerate(expr
->conditional
);
1093 rtype
= degenerate(expr
->cond_false
);
1095 true = &expr
->conditional
;
1097 if (expr
->cond_true
) {
1098 if (!evaluate_expression(expr
->cond_true
))
1100 ltype
= degenerate(expr
->cond_true
);
1101 true = &expr
->cond_true
;
1105 int flags
= expr
->conditional
->flags
& Int_const_expr
;
1106 flags
&= (*true)->flags
& expr
->cond_false
->flags
;
1111 lclass
= classify_type(ltype
, <ype
);
1112 rclass
= classify_type(rtype
, &rtype
);
1113 if (lclass
& rclass
& TYPE_NUM
) {
1114 ctype
= usual_conversions('?', *true, expr
->cond_false
,
1115 lclass
, rclass
, ltype
, rtype
);
1116 *true = cast_to(*true, ctype
);
1117 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1121 if ((lclass
| rclass
) & TYPE_PTR
) {
1122 int is_null1
= is_null_pointer_constant(*true);
1123 int is_null2
= is_null_pointer_constant(expr
->cond_false
);
1125 if (is_null1
&& is_null2
) {
1126 *true = cast_to(*true, &ptr_ctype
);
1127 expr
->cond_false
= cast_to(expr
->cond_false
, &ptr_ctype
);
1131 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1134 *true = cast_to(*true, rtype
);
1138 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1140 bad_null(expr
->cond_false
);
1141 expr
->cond_false
= cast_to(expr
->cond_false
, ltype
);
1145 if (!(lclass
& rclass
& TYPE_PTR
)) {
1146 typediff
= "different types";
1149 /* OK, it's pointer on pointer */
1150 if (ltype
->ctype
.as
!= rtype
->ctype
.as
) {
1151 typediff
= "different address spaces";
1155 /* need to be lazier here */
1156 lbase
= get_base_type(ltype
);
1157 rbase
= get_base_type(rtype
);
1158 qual
= ltype
->ctype
.modifiers
| rtype
->ctype
.modifiers
;
1159 qual
&= MOD_CONST
| MOD_VOLATILE
;
1161 if (lbase
== &void_ctype
) {
1162 /* XXX: pointers to function should warn here */
1167 if (rbase
== &void_ctype
) {
1168 /* XXX: pointers to function should warn here */
1172 /* XXX: that should be pointer to composite */
1174 typediff
= type_difference(lbase
, rbase
, MOD_IGN
, MOD_IGN
);
1180 /* void on void, struct on same struct, union on same union */
1181 if (ltype
== rtype
) {
1185 typediff
= "different base types";
1188 expression_error(expr
, "incompatible types in conditional expression (%s)", typediff
);
1192 expr
->ctype
= ctype
;
1196 if (qual
& ~ctype
->ctype
.modifiers
) {
1197 struct symbol
*sym
= alloc_symbol(ctype
->pos
, SYM_PTR
);
1199 sym
->ctype
.modifiers
|= qual
;
1202 *true = cast_to(*true, ctype
);
1203 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1207 /* FP assignments can not do modulo or bit operations */
1208 static int compatible_float_op(int op
)
1210 return op
== SPECIAL_ADD_ASSIGN
||
1211 op
== SPECIAL_SUB_ASSIGN
||
1212 op
== SPECIAL_MUL_ASSIGN
||
1213 op
== SPECIAL_DIV_ASSIGN
;
1216 static int evaluate_assign_op(struct expression
*expr
)
1218 struct symbol
*target
= expr
->left
->ctype
;
1219 struct symbol
*source
= expr
->right
->ctype
;
1220 struct symbol
*t
, *s
;
1221 int tclass
= classify_type(target
, &t
);
1222 int sclass
= classify_type(source
, &s
);
1225 if (tclass
& sclass
& TYPE_NUM
) {
1226 if (tclass
& TYPE_FLOAT
&& !compatible_float_op(op
)) {
1227 expression_error(expr
, "invalid assignment");
1230 if (tclass
& TYPE_RESTRICT
) {
1231 if (!restricted_binop(op
, t
)) {
1232 expression_error(expr
, "bad restricted assignment");
1235 /* allowed assignments unfoul */
1236 if (sclass
& TYPE_FOULED
&& s
->ctype
.base_type
== t
)
1238 if (!restricted_value(expr
->right
, t
))
1240 } else if (!(sclass
& TYPE_RESTRICT
))
1242 /* source and target would better be identical restricted */
1245 warning(expr
->pos
, "invalid restricted assignment");
1246 expr
->right
= cast_to(expr
->right
, target
);
1249 if (tclass
& TYPE_PTR
&& is_int(sclass
)) {
1250 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1251 unrestrict(expr
->right
, sclass
, &s
);
1252 evaluate_ptr_add(expr
, target
, s
);
1255 expression_error(expr
, "invalid pointer assignment");
1259 expression_error(expr
, "invalid assignment");
1263 expr
->right
= cast_to(expr
->right
, target
);
1267 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1268 struct expression
**rp
, const char *where
)
1270 const char *typediff
;
1271 struct symbol
*source
= degenerate(*rp
);
1272 struct symbol
*t
, *s
;
1273 int tclass
= classify_type(target
, &t
);
1274 int sclass
= classify_type(source
, &s
);
1276 if (tclass
& sclass
& TYPE_NUM
) {
1277 if (tclass
& TYPE_RESTRICT
) {
1278 /* allowed assignments unfoul */
1279 if (sclass
& TYPE_FOULED
&& s
->ctype
.base_type
== t
)
1281 if (!restricted_value(*rp
, target
))
1283 } else if (!(sclass
& TYPE_RESTRICT
))
1287 if (tclass
& TYPE_PTR
) {
1288 // NULL pointer is always OK
1289 int is_null
= is_null_pointer_constant(*rp
);
1295 if (sclass
& TYPE_PTR
&& t
->ctype
.as
== s
->ctype
.as
) {
1296 /* we should be more lazy here */
1297 int mod1
= t
->ctype
.modifiers
;
1298 int mod2
= s
->ctype
.modifiers
;
1299 s
= get_base_type(s
);
1300 t
= get_base_type(t
);
1303 * assignments to/from void * are OK, provided that
1304 * we do not remove qualifiers from pointed to [C]
1305 * or mix address spaces [sparse].
1307 if (!(mod2
& ~mod1
& (MOD_VOLATILE
| MOD_CONST
)))
1308 if (s
== &void_ctype
|| t
== &void_ctype
)
1313 /* It's OK if the target is more volatile or const than the source */
1314 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
1318 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1319 info(expr
->pos
, " expected %s", show_typename(target
));
1320 info(expr
->pos
, " got %s", show_typename(source
));
1321 *rp
= cast_to(*rp
, target
);
1324 *rp
= cast_to(*rp
, target
);
1328 static void mark_assigned(struct expression
*expr
)
1334 switch (expr
->type
) {
1339 if (sym
->type
!= SYM_NODE
)
1341 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1345 mark_assigned(expr
->left
);
1346 mark_assigned(expr
->right
);
1349 case EXPR_FORCE_CAST
:
1350 mark_assigned(expr
->cast_expression
);
1353 mark_assigned(expr
->base
);
1361 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1363 if (type
->ctype
.modifiers
& MOD_CONST
)
1364 expression_error(left
, "assignment to const expression");
1366 /* We know left is an lvalue, so it's a "preop-*" */
1367 mark_assigned(left
->unop
);
1370 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1372 struct expression
*left
= expr
->left
;
1373 struct expression
*where
= expr
;
1374 struct symbol
*ltype
;
1376 if (!lvalue_expression(left
)) {
1377 expression_error(expr
, "not an lvalue");
1381 ltype
= left
->ctype
;
1383 if (expr
->op
!= '=') {
1384 if (!evaluate_assign_op(expr
))
1387 if (!compatible_assignment_types(where
, ltype
, &expr
->right
, "assignment"))
1391 evaluate_assign_to(left
, ltype
);
1393 expr
->ctype
= ltype
;
1397 static void examine_fn_arguments(struct symbol
*fn
)
1401 FOR_EACH_PTR(fn
->arguments
, s
) {
1402 struct symbol
*arg
= evaluate_symbol(s
);
1403 /* Array/function arguments silently degenerate into pointers */
1409 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1410 if (arg
->type
== SYM_ARRAY
)
1411 ptr
->ctype
= arg
->ctype
;
1413 ptr
->ctype
.base_type
= arg
;
1414 ptr
->ctype
.as
|= s
->ctype
.as
;
1415 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
& MOD_PTRINHERIT
;
1417 s
->ctype
.base_type
= ptr
;
1419 s
->ctype
.modifiers
&= ~MOD_PTRINHERIT
;
1422 examine_symbol_type(s
);
1429 } END_FOR_EACH_PTR(s
);
1432 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1434 /* Take the modifiers of the pointer, and apply them to the member */
1435 mod
|= sym
->ctype
.modifiers
;
1436 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1437 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1439 newsym
->ctype
.as
= as
;
1440 newsym
->ctype
.modifiers
= mod
;
1446 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1448 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1449 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1451 node
->ctype
.base_type
= ptr
;
1452 ptr
->bit_size
= bits_in_pointer
;
1453 ptr
->ctype
.alignment
= pointer_alignment
;
1455 node
->bit_size
= bits_in_pointer
;
1456 node
->ctype
.alignment
= pointer_alignment
;
1459 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1460 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1461 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1463 if (sym
->type
== SYM_NODE
) {
1464 ptr
->ctype
.as
|= sym
->ctype
.as
;
1465 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1466 sym
= sym
->ctype
.base_type
;
1468 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1469 ptr
->ctype
.as
|= sym
->ctype
.as
;
1470 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1471 sym
= sym
->ctype
.base_type
;
1473 ptr
->ctype
.base_type
= sym
;
1478 /* Arrays degenerate into pointers on pointer arithmetic */
1479 static struct symbol
*degenerate(struct expression
*expr
)
1481 struct symbol
*ctype
, *base
;
1485 ctype
= expr
->ctype
;
1488 base
= examine_symbol_type(ctype
);
1489 if (ctype
->type
== SYM_NODE
)
1490 base
= ctype
->ctype
.base_type
;
1492 * Arrays degenerate into pointers to the entries, while
1493 * functions degenerate into pointers to themselves.
1494 * If array was part of non-lvalue compound, we create a copy
1495 * of that compound first and then act as if we were dealing with
1496 * the corresponding field in there.
1498 switch (base
->type
) {
1500 if (expr
->type
== EXPR_SLICE
) {
1501 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1502 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1504 a
->ctype
.base_type
= expr
->base
->ctype
;
1505 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1506 a
->array_size
= expr
->base
->ctype
->array_size
;
1508 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1510 e0
->ctype
= &lazy_ptr_ctype
;
1512 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1515 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1517 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1519 e2
->right
= expr
->base
;
1521 e2
->ctype
= expr
->base
->ctype
;
1523 if (expr
->r_bitpos
) {
1524 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1527 e3
->right
= alloc_const_expression(expr
->pos
,
1528 expr
->r_bitpos
>> 3);
1529 e3
->ctype
= &lazy_ptr_ctype
;
1534 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1537 e4
->ctype
= &lazy_ptr_ctype
;
1540 expr
->type
= EXPR_PREOP
;
1544 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1545 expression_error(expr
, "strange non-value function or array");
1548 *expr
= *expr
->unop
;
1549 ctype
= create_pointer(expr
, ctype
, 1);
1550 expr
->ctype
= ctype
;
1557 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1559 struct expression
*op
= expr
->unop
;
1560 struct symbol
*ctype
;
1562 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1563 expression_error(expr
, "not addressable");
1570 if (expr
->type
== EXPR_SYMBOL
) {
1571 struct symbol
*sym
= expr
->symbol
;
1572 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1576 * symbol expression evaluation is lazy about the type
1577 * of the sub-expression, so we may have to generate
1578 * the type here if so..
1580 if (expr
->ctype
== &lazy_ptr_ctype
) {
1581 ctype
= create_pointer(expr
, ctype
, 0);
1582 expr
->ctype
= ctype
;
1588 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1590 struct expression
*op
= expr
->unop
;
1591 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1593 /* Simplify: *&(expr) => (expr) */
1594 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1600 /* Dereferencing a node drops all the node information. */
1601 if (ctype
->type
== SYM_NODE
)
1602 ctype
= ctype
->ctype
.base_type
;
1604 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1605 target
= ctype
->ctype
.base_type
;
1607 switch (ctype
->type
) {
1609 expression_error(expr
, "cannot dereference this type");
1612 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1613 merge_type(node
, ctype
);
1617 if (!lvalue_expression(op
)) {
1618 expression_error(op
, "non-lvalue array??");
1622 /* Do the implied "addressof" on the array */
1626 * When an array is dereferenced, we need to pick
1627 * up the attributes of the original node too..
1629 merge_type(node
, op
->ctype
);
1630 merge_type(node
, ctype
);
1634 node
->bit_size
= target
->bit_size
;
1635 node
->array_size
= target
->array_size
;
1642 * Unary post-ops: x++ and x--
1644 static struct symbol
*evaluate_postop(struct expression
*expr
)
1646 struct expression
*op
= expr
->unop
;
1647 struct symbol
*ctype
= op
->ctype
;
1649 if (!lvalue_expression(expr
->unop
)) {
1650 expression_error(expr
, "need lvalue expression for ++/--");
1653 if (is_restricted_type(ctype
) && restricted_unop(expr
->op
, &ctype
)) {
1654 expression_error(expr
, "bad operation on restricted");
1656 } else if (is_fouled_type(ctype
) && restricted_unop(expr
->op
, &ctype
)) {
1657 expression_error(expr
, "bad operation on restricted");
1661 evaluate_assign_to(op
, ctype
);
1663 expr
->ctype
= ctype
;
1665 if (is_ptr_type(ctype
))
1666 expr
->op_value
= ptr_object_size(ctype
) >> 3;
1671 static struct symbol
*evaluate_sign(struct expression
*expr
)
1673 struct symbol
*ctype
= expr
->unop
->ctype
;
1674 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1676 if (is_int_type(ctype
)) {
1677 struct symbol
*rtype
= rtype
= integer_promotion(ctype
);
1678 expr
->unop
= cast_to(expr
->unop
, rtype
);
1680 } else if (is_float_type(ctype
) && expr
->op
!= '~') {
1681 /* no conversions needed */
1682 } else if (is_restricted_type(ctype
) && !restricted_unop(expr
->op
, &ctype
)) {
1683 /* no conversions needed */
1684 } else if (is_fouled_type(ctype
) && !restricted_unop(expr
->op
, &ctype
)) {
1685 /* no conversions needed */
1687 return bad_expr_type(expr
);
1689 if (expr
->op
== '+')
1690 *expr
= *expr
->unop
;
1691 expr
->ctype
= ctype
;
1695 static struct symbol
*evaluate_preop(struct expression
*expr
)
1697 struct symbol
*ctype
= expr
->unop
->ctype
;
1701 *expr
= *expr
->unop
;
1707 return evaluate_sign(expr
);
1710 return evaluate_dereference(expr
);
1713 return evaluate_addressof(expr
);
1715 case SPECIAL_INCREMENT
:
1716 case SPECIAL_DECREMENT
:
1718 * From a type evaluation standpoint the preops are
1719 * the same as the postops
1721 return evaluate_postop(expr
);
1724 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1726 if (is_safe_type(ctype
))
1727 warning(expr
->pos
, "testing a 'safe expression'");
1728 if (is_float_type(ctype
)) {
1729 struct expression
*arg
= expr
->unop
;
1730 expr
->type
= EXPR_BINOP
;
1731 expr
->op
= SPECIAL_EQUAL
;
1733 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1734 expr
->right
->ctype
= ctype
;
1735 expr
->right
->fvalue
= 0;
1736 } else if (is_fouled_type(ctype
)) {
1737 warning(expr
->pos
, "restricted degrades to integer");
1739 ctype
= &bool_ctype
;
1745 expr
->ctype
= ctype
;
1749 static struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1751 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1752 struct ptr_list
*list
= head
;
1758 for (i
= 0; i
< list
->nr
; i
++) {
1759 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1761 if (sym
->ident
!= ident
)
1763 *offset
= sym
->offset
;
1766 struct symbol
*ctype
= sym
->ctype
.base_type
;
1770 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1772 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1775 *offset
+= sym
->offset
;
1779 } while ((list
= list
->next
) != head
);
1783 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1785 struct expression
*add
;
1788 * Create a new add-expression
1790 * NOTE! Even if we just add zero, we need a new node
1791 * for the member pointer, since it has a different
1792 * type than the original pointer. We could make that
1793 * be just a cast, but the fact is, a node is a node,
1794 * so we might as well just do the "add zero" here.
1796 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1799 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1800 add
->right
->ctype
= &int_ctype
;
1801 add
->right
->value
= offset
;
1804 * The ctype of the pointer will be lazily evaluated if
1805 * we ever take the address of this member dereference..
1807 add
->ctype
= &lazy_ptr_ctype
;
1811 /* structure/union dereference */
1812 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1815 struct symbol
*ctype
, *member
;
1816 struct expression
*deref
= expr
->deref
, *add
;
1817 struct ident
*ident
= expr
->member
;
1821 if (!evaluate_expression(deref
))
1824 expression_error(expr
, "bad member name");
1828 ctype
= deref
->ctype
;
1829 address_space
= ctype
->ctype
.as
;
1830 mod
= ctype
->ctype
.modifiers
;
1831 if (ctype
->type
== SYM_NODE
) {
1832 ctype
= ctype
->ctype
.base_type
;
1833 address_space
|= ctype
->ctype
.as
;
1834 mod
|= ctype
->ctype
.modifiers
;
1836 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1837 expression_error(expr
, "expected structure or union");
1840 examine_symbol_type(ctype
);
1842 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1844 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1845 const char *name
= "<unnamed>";
1848 name
= ctype
->ident
->name
;
1849 namelen
= ctype
->ident
->len
;
1851 if (ctype
->symbol_list
)
1852 expression_error(expr
, "no member '%s' in %s %.*s",
1853 show_ident(ident
), type
, namelen
, name
);
1855 expression_error(expr
, "using member '%s' in "
1856 "incomplete %s %.*s", show_ident(ident
),
1857 type
, namelen
, name
);
1862 * The member needs to take on the address space and modifiers of
1863 * the "parent" type.
1865 member
= convert_to_as_mod(member
, address_space
, mod
);
1866 ctype
= get_base_type(member
);
1868 if (!lvalue_expression(deref
)) {
1869 if (deref
->type
!= EXPR_SLICE
) {
1873 expr
->base
= deref
->base
;
1874 expr
->r_bitpos
= deref
->r_bitpos
;
1876 expr
->r_bitpos
+= offset
<< 3;
1877 expr
->type
= EXPR_SLICE
;
1878 expr
->r_nrbits
= member
->bit_size
;
1879 expr
->r_bitpos
+= member
->bit_offset
;
1880 expr
->ctype
= member
;
1884 deref
= deref
->unop
;
1885 expr
->deref
= deref
;
1887 add
= evaluate_offset(deref
, offset
);
1888 expr
->type
= EXPR_PREOP
;
1892 expr
->ctype
= member
;
1896 static int is_promoted(struct expression
*expr
)
1899 switch (expr
->type
) {
1902 case EXPR_CONDITIONAL
:
1926 static struct symbol
*evaluate_cast(struct expression
*);
1928 static struct symbol
*evaluate_type_information(struct expression
*expr
)
1930 struct symbol
*sym
= expr
->cast_type
;
1932 sym
= evaluate_expression(expr
->cast_expression
);
1936 * Expressions of restricted types will possibly get
1937 * promoted - check that here
1939 if (is_restricted_type(sym
)) {
1940 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
1942 } else if (is_fouled_type(sym
)) {
1946 examine_symbol_type(sym
);
1947 if (is_bitfield_type(sym
)) {
1948 expression_error(expr
, "trying to examine bitfield type");
1954 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1956 struct symbol
*type
;
1959 type
= evaluate_type_information(expr
);
1963 size
= type
->bit_size
;
1964 if ((size
< 0) || (size
& 7))
1965 expression_error(expr
, "cannot size expression");
1966 expr
->type
= EXPR_VALUE
;
1967 expr
->value
= size
>> 3;
1969 expr
->ctype
= size_t_ctype
;
1970 return size_t_ctype
;
1973 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
1975 struct symbol
*type
;
1978 type
= evaluate_type_information(expr
);
1982 if (type
->type
== SYM_NODE
)
1983 type
= type
->ctype
.base_type
;
1986 switch (type
->type
) {
1990 type
= get_base_type(type
);
1994 expression_error(expr
, "expected pointer expression");
1997 size
= type
->bit_size
;
2000 expr
->type
= EXPR_VALUE
;
2001 expr
->value
= size
>> 3;
2003 expr
->ctype
= size_t_ctype
;
2004 return size_t_ctype
;
2007 static struct symbol
*evaluate_alignof(struct expression
*expr
)
2009 struct symbol
*type
;
2011 type
= evaluate_type_information(expr
);
2015 expr
->type
= EXPR_VALUE
;
2016 expr
->value
= type
->ctype
.alignment
;
2018 expr
->ctype
= size_t_ctype
;
2019 return size_t_ctype
;
2022 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
2024 struct expression
*expr
;
2025 struct symbol_list
*argument_types
= fn
->arguments
;
2026 struct symbol
*argtype
;
2029 PREPARE_PTR_LIST(argument_types
, argtype
);
2030 FOR_EACH_PTR (head
, expr
) {
2031 struct expression
**p
= THIS_ADDRESS(expr
);
2032 struct symbol
*ctype
, *target
;
2033 ctype
= evaluate_expression(expr
);
2040 struct symbol
*type
;
2041 int class = classify_type(ctype
, &type
);
2042 if (is_int(class)) {
2043 *p
= cast_to(expr
, integer_promotion(type
));
2044 } else if (class & TYPE_FLOAT
) {
2045 unsigned long mod
= type
->ctype
.modifiers
;
2046 if (!(mod
& (MOD_LONG
|MOD_LONGLONG
)))
2047 *p
= cast_to(expr
, &double_ctype
);
2048 } else if (class & TYPE_PTR
) {
2049 if (expr
->ctype
== &null_ctype
)
2050 *p
= cast_to(expr
, &ptr_ctype
);
2055 static char where
[30];
2056 examine_symbol_type(target
);
2057 sprintf(where
, "argument %d", i
);
2058 compatible_assignment_types(expr
, target
, p
, where
);
2062 NEXT_PTR_LIST(argtype
);
2063 } END_FOR_EACH_PTR(expr
);
2064 FINISH_PTR_LIST(argtype
);
2068 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
2072 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
2073 if (sym
->ident
== ident
)
2075 } END_FOR_EACH_PTR(sym
);
2079 static void convert_index(struct expression
*e
)
2081 struct expression
*child
= e
->idx_expression
;
2082 unsigned from
= e
->idx_from
;
2083 unsigned to
= e
->idx_to
+ 1;
2085 e
->init_offset
= from
* (e
->ctype
->bit_size
>>3);
2086 e
->init_nr
= to
- from
;
2087 e
->init_expr
= child
;
2090 static void convert_ident(struct expression
*e
)
2092 struct expression
*child
= e
->ident_expression
;
2093 struct symbol
*sym
= e
->field
;
2095 e
->init_offset
= sym
->offset
;
2097 e
->init_expr
= child
;
2100 static void convert_designators(struct expression
*e
)
2103 if (e
->type
== EXPR_INDEX
)
2105 else if (e
->type
== EXPR_IDENTIFIER
)
2113 static void excess(struct expression
*e
, const char *s
)
2115 warning(e
->pos
, "excessive elements in %s initializer", s
);
2119 * implicit designator for the first element
2121 static struct expression
*first_subobject(struct symbol
*ctype
, int class,
2122 struct expression
**v
)
2124 struct expression
*e
= *v
, *new;
2126 if (ctype
->type
== SYM_NODE
)
2127 ctype
= ctype
->ctype
.base_type
;
2129 if (class & TYPE_PTR
) { /* array */
2130 if (!ctype
->bit_size
)
2132 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2133 new->idx_expression
= e
;
2134 new->ctype
= ctype
->ctype
.base_type
;
2136 struct symbol
*field
, *p
;
2137 PREPARE_PTR_LIST(ctype
->symbol_list
, p
);
2138 while (p
&& !p
->ident
&& is_bitfield_type(p
))
2144 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2145 new->ident_expression
= e
;
2146 new->field
= new->ctype
= field
;
2153 * sanity-check explicit designators; return the innermost one or NULL
2154 * in case of error. Assign types.
2156 static struct expression
*check_designators(struct expression
*e
,
2157 struct symbol
*ctype
)
2159 struct expression
*last
= NULL
;
2162 if (ctype
->type
== SYM_NODE
)
2163 ctype
= ctype
->ctype
.base_type
;
2164 if (e
->type
== EXPR_INDEX
) {
2165 struct symbol
*type
;
2166 if (ctype
->type
!= SYM_ARRAY
) {
2167 err
= "array index in non-array";
2170 type
= ctype
->ctype
.base_type
;
2171 if (ctype
->bit_size
>= 0 && type
->bit_size
>= 0) {
2172 unsigned offset
= e
->idx_to
* type
->bit_size
;
2173 if (offset
>= ctype
->bit_size
) {
2174 err
= "index out of bounds in";
2178 e
->ctype
= ctype
= type
;
2181 if (!e
->idx_expression
) {
2185 e
= e
->idx_expression
;
2186 } else if (e
->type
== EXPR_IDENTIFIER
) {
2187 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2188 err
= "field name not in struct or union";
2191 ctype
= find_struct_ident(ctype
, e
->expr_ident
);
2193 err
= "unknown field name in";
2196 e
->field
= e
->ctype
= ctype
;
2198 if (!e
->ident_expression
) {
2202 e
= e
->ident_expression
;
2203 } else if (e
->type
== EXPR_POS
) {
2204 err
= "internal front-end error: EXPR_POS in";
2209 expression_error(e
, "%s initializer", err
);
2214 * choose the next subobject to initialize.
2216 * Get designators for next element, switch old ones to EXPR_POS.
2217 * Return the resulting expression or NULL if we'd run out of subobjects.
2218 * The innermost designator is returned in *v. Designators in old
2219 * are assumed to be already sanity-checked.
2221 static struct expression
*next_designators(struct expression
*old
,
2222 struct symbol
*ctype
,
2223 struct expression
*e
, struct expression
**v
)
2225 struct expression
*new = NULL
;
2229 if (old
->type
== EXPR_INDEX
) {
2230 struct expression
*copy
;
2233 copy
= next_designators(old
->idx_expression
,
2236 n
= old
->idx_to
+ 1;
2237 if (n
* old
->ctype
->bit_size
== ctype
->bit_size
) {
2242 *v
= new = alloc_expression(e
->pos
, EXPR_INDEX
);
2245 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2248 new->idx_from
= new->idx_to
= n
;
2249 new->idx_expression
= copy
;
2250 new->ctype
= old
->ctype
;
2252 } else if (old
->type
== EXPR_IDENTIFIER
) {
2253 struct expression
*copy
;
2254 struct symbol
*field
;
2256 copy
= next_designators(old
->ident_expression
,
2259 field
= old
->field
->next_subobject
;
2265 *v
= new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2268 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2272 new->expr_ident
= field
->ident
;
2273 new->ident_expression
= copy
;
2280 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2281 int class, struct symbol
*ctype
);
2284 * deal with traversing subobjects [6.7.8(17,18,20)]
2286 static void handle_list_initializer(struct expression
*expr
,
2287 int class, struct symbol
*ctype
)
2289 struct expression
*e
, *last
= NULL
, *top
= NULL
, *next
;
2292 FOR_EACH_PTR(expr
->expr_list
, e
) {
2293 struct expression
**v
;
2294 struct symbol
*type
;
2297 if (e
->type
!= EXPR_INDEX
&& e
->type
!= EXPR_IDENTIFIER
) {
2300 last
= first_subobject(ctype
, class, &top
);
2302 last
= next_designators(last
, ctype
, e
, &top
);
2305 excess(e
, class & TYPE_PTR
? "array" :
2307 DELETE_CURRENT_PTR(e
);
2311 warning(e
->pos
, "advancing past deep designator");
2314 REPLACE_CURRENT_PTR(e
, last
);
2316 next
= check_designators(e
, ctype
);
2318 DELETE_CURRENT_PTR(e
);
2322 /* deeper than one designator? */
2324 convert_designators(last
);
2329 lclass
= classify_type(top
->ctype
, &type
);
2330 if (top
->type
== EXPR_INDEX
)
2331 v
= &top
->idx_expression
;
2333 v
= &top
->ident_expression
;
2335 if (handle_simple_initializer(v
, 1, lclass
, top
->ctype
))
2338 if (!(lclass
& TYPE_COMPOUND
)) {
2339 warning(e
->pos
, "bogus scalar initializer");
2340 DELETE_CURRENT_PTR(e
);
2344 next
= first_subobject(type
, lclass
, v
);
2346 warning(e
->pos
, "missing braces around initializer");
2351 DELETE_CURRENT_PTR(e
);
2352 excess(e
, lclass
& TYPE_PTR
? "array" : "struct or union");
2354 } END_FOR_EACH_PTR(e
);
2356 convert_designators(last
);
2357 expr
->ctype
= ctype
;
2360 static int is_string_literal(struct expression
**v
)
2362 struct expression
*e
= *v
;
2363 while (e
&& e
->type
== EXPR_PREOP
&& e
->op
== '(')
2365 if (!e
|| e
->type
!= EXPR_STRING
)
2367 if (e
!= *v
&& Wparen_string
)
2369 "array initialized from parenthesized string constant");
2375 * We want a normal expression, possibly in one layer of braces. Warn
2376 * if the latter happens inside a list (it's legal, but likely to be
2377 * an effect of screwup). In case of anything not legal, we are definitely
2378 * having an effect of screwup, so just fail and let the caller warn.
2380 static struct expression
*handle_scalar(struct expression
*e
, int nested
)
2382 struct expression
*v
= NULL
, *p
;
2386 if (e
->type
!= EXPR_INITIALIZER
)
2389 FOR_EACH_PTR(e
->expr_list
, p
) {
2393 } END_FOR_EACH_PTR(p
);
2397 case EXPR_INITIALIZER
:
2399 case EXPR_IDENTIFIER
:
2405 warning(e
->pos
, "braces around scalar initializer");
2410 * deal with the cases that don't care about subobjects:
2411 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2412 * character array <- string literal, possibly in braces [6.7.8(14)]
2413 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2414 * compound type <- initializer list in braces [6.7.8(16)]
2415 * The last one punts to handle_list_initializer() which, in turn will call
2416 * us for individual elements of the list.
2418 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2419 * the lack of support of wide char stuff in general.
2421 * One note: we need to take care not to evaluate a string literal until
2422 * we know that we *will* handle it right here. Otherwise we would screw
2423 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2424 * { "string", ...} - we need to preserve that string literal recognizable
2425 * until we dig into the inner struct.
2427 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2428 int class, struct symbol
*ctype
)
2430 int is_string
= is_string_type(ctype
);
2431 struct expression
*e
= *ep
, *p
;
2432 struct symbol
*type
;
2438 if (!(class & TYPE_COMPOUND
)) {
2439 e
= handle_scalar(e
, nested
);
2443 if (!evaluate_expression(e
))
2445 compatible_assignment_types(e
, ctype
, ep
, "initializer");
2450 * sublist; either a string, or we dig in; the latter will deal with
2451 * pathologies, so we don't need anything fancy here.
2453 if (e
->type
== EXPR_INITIALIZER
) {
2455 struct expression
*v
= NULL
;
2458 FOR_EACH_PTR(e
->expr_list
, p
) {
2462 } END_FOR_EACH_PTR(p
);
2463 if (count
== 1 && is_string_literal(&v
)) {
2468 handle_list_initializer(e
, class, ctype
);
2473 if (is_string_literal(&e
)) {
2474 /* either we are doing array of char, or we'll have to dig in */
2481 /* struct or union can be initialized by compatible */
2482 if (class != TYPE_COMPOUND
)
2484 type
= evaluate_expression(e
);
2487 if (ctype
->type
== SYM_NODE
)
2488 ctype
= ctype
->ctype
.base_type
;
2489 if (type
->type
== SYM_NODE
)
2490 type
= type
->ctype
.base_type
;
2496 p
= alloc_expression(e
->pos
, EXPR_STRING
);
2498 type
= evaluate_expression(p
);
2499 if (ctype
->bit_size
!= -1 &&
2500 ctype
->bit_size
+ bits_in_char
< type
->bit_size
) {
2502 "too long initializer-string for array of char");
2508 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2510 struct symbol
*type
;
2511 int class = classify_type(ctype
, &type
);
2512 if (!handle_simple_initializer(ep
, 0, class, ctype
))
2513 expression_error(*ep
, "invalid initializer");
2516 static struct symbol
*evaluate_cast(struct expression
*expr
)
2518 struct expression
*target
= expr
->cast_expression
;
2519 struct symbol
*ctype
;
2520 struct symbol
*t1
, *t2
;
2522 int as1
= 0, as2
= 0;
2528 * Special case: a cast can be followed by an
2529 * initializer, in which case we need to pass
2530 * the type value down to that initializer rather
2531 * than trying to evaluate it as an expression
2533 * A more complex case is when the initializer is
2534 * dereferenced as part of a post-fix expression.
2535 * We need to produce an expression that can be dereferenced.
2537 if (target
->type
== EXPR_INITIALIZER
) {
2538 struct symbol
*sym
= expr
->cast_type
;
2539 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2541 sym
->initializer
= target
;
2542 evaluate_symbol(sym
);
2544 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2547 expr
->type
= EXPR_PREOP
;
2555 ctype
= examine_symbol_type(expr
->cast_type
);
2556 expr
->ctype
= ctype
;
2557 expr
->cast_type
= ctype
;
2559 evaluate_expression(target
);
2562 class1
= classify_type(ctype
, &t1
);
2564 /* cast to non-integer type -> not an integer constant expression */
2565 if (!is_int(class1
))
2567 /* if argument turns out to be not an integer constant expression *and*
2568 it was not a floating literal to start with -> too bad */
2569 else if (expr
->flags
== Int_const_expr
&&
2570 !(target
->flags
& Int_const_expr
))
2573 * You can always throw a value away by casting to
2574 * "void" - that's an implicit "force". Note that
2575 * the same is _not_ true of "void *".
2577 if (t1
== &void_ctype
)
2580 if (class1
& TYPE_COMPOUND
)
2581 warning(expr
->pos
, "cast to non-scalar");
2583 if (class1
== TYPE_PTR
)
2588 expression_error(expr
, "cast from unknown type");
2591 class2
= classify_type(t2
, &t2
);
2593 if (class2
& TYPE_COMPOUND
)
2594 warning(expr
->pos
, "cast from non-scalar");
2596 if (expr
->type
== EXPR_FORCE_CAST
)
2599 /* allowed cast unfouls */
2600 if (class2
& TYPE_FOULED
)
2601 t2
= t2
->ctype
.base_type
;
2604 if (class1
& TYPE_RESTRICT
)
2605 warning(expr
->pos
, "cast to restricted type");
2606 if (class2
& TYPE_RESTRICT
)
2607 warning(expr
->pos
, "cast from restricted type");
2610 if (t1
== &ulong_ctype
)
2612 else if (class1
== TYPE_PTR
)
2615 if (t2
== &ulong_ctype
)
2617 else if (class2
== TYPE_PTR
)
2620 if (!as1
&& as2
> 0)
2621 warning(expr
->pos
, "cast removes address space of expression");
2622 if (as1
> 0 && as2
> 0 && as1
!= as2
)
2623 warning(expr
->pos
, "cast between address spaces (<asn:%d>-><asn:%d>)", as2
, as1
);
2624 if (as1
> 0 && !as2
&&
2625 !is_null_pointer_constant(target
) && Wcast_to_address_space
)
2627 "cast adds address space to expression (<asn:%d>)", as1
);
2629 if (!(t1
->ctype
.modifiers
& MOD_PTRINHERIT
) && class1
== TYPE_PTR
&&
2630 !as1
&& (target
->flags
& Int_const_expr
)) {
2631 if (t1
->ctype
.base_type
== &void_ctype
) {
2632 if (is_zero_constant(target
)) {
2634 expr
->type
= EXPR_VALUE
;
2635 expr
->ctype
= &null_ctype
;
2646 * Evaluate a call expression with a symbol. This
2647 * should expand inline functions, and evaluate
2650 static int evaluate_symbol_call(struct expression
*expr
)
2652 struct expression
*fn
= expr
->fn
;
2653 struct symbol
*ctype
= fn
->ctype
;
2655 if (fn
->type
!= EXPR_PREOP
)
2658 if (ctype
->op
&& ctype
->op
->evaluate
)
2659 return ctype
->op
->evaluate(expr
);
2661 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2663 struct symbol
*curr
= current_fn
;
2664 current_fn
= ctype
->ctype
.base_type
;
2666 ret
= inline_function(expr
, ctype
);
2668 /* restore the old function */
2676 static struct symbol
*evaluate_call(struct expression
*expr
)
2679 struct symbol
*ctype
, *sym
;
2680 struct expression
*fn
= expr
->fn
;
2681 struct expression_list
*arglist
= expr
->args
;
2683 if (!evaluate_expression(fn
))
2685 sym
= ctype
= fn
->ctype
;
2686 if (ctype
->type
== SYM_NODE
)
2687 ctype
= ctype
->ctype
.base_type
;
2688 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
2689 ctype
= get_base_type(ctype
);
2691 examine_fn_arguments(ctype
);
2692 if (sym
->type
== SYM_NODE
&& fn
->type
== EXPR_PREOP
&&
2693 sym
->op
&& sym
->op
->args
) {
2694 if (!sym
->op
->args(expr
))
2697 if (!evaluate_arguments(sym
, ctype
, arglist
))
2699 if (ctype
->type
!= SYM_FN
) {
2700 expression_error(expr
, "not a function %s",
2701 show_ident(sym
->ident
));
2704 args
= expression_list_size(expr
->args
);
2705 fnargs
= symbol_list_size(ctype
->arguments
);
2707 expression_error(expr
,
2708 "not enough arguments for function %s",
2709 show_ident(sym
->ident
));
2710 if (args
> fnargs
&& !ctype
->variadic
)
2711 expression_error(expr
,
2712 "too many arguments for function %s",
2713 show_ident(sym
->ident
));
2715 if (sym
->type
== SYM_NODE
) {
2716 if (evaluate_symbol_call(expr
))
2719 expr
->ctype
= ctype
->ctype
.base_type
;
2723 static struct symbol
*evaluate_offsetof(struct expression
*expr
)
2725 struct expression
*e
= expr
->down
;
2726 struct symbol
*ctype
= expr
->in
;
2729 if (expr
->op
== '.') {
2730 struct symbol
*field
;
2733 expression_error(expr
, "expected structure or union");
2736 examine_symbol_type(ctype
);
2737 class = classify_type(ctype
, &ctype
);
2738 if (class != TYPE_COMPOUND
) {
2739 expression_error(expr
, "expected structure or union");
2743 field
= find_identifier(expr
->ident
, ctype
->symbol_list
, &offset
);
2745 expression_error(expr
, "unknown member");
2749 expr
->type
= EXPR_VALUE
;
2750 expr
->flags
= Int_const_expr
;
2751 expr
->value
= offset
;
2753 expr
->ctype
= size_t_ctype
;
2756 expression_error(expr
, "expected structure or union");
2759 examine_symbol_type(ctype
);
2760 class = classify_type(ctype
, &ctype
);
2761 if (class != (TYPE_COMPOUND
| TYPE_PTR
)) {
2762 expression_error(expr
, "expected array");
2765 ctype
= ctype
->ctype
.base_type
;
2767 expr
->type
= EXPR_VALUE
;
2768 expr
->flags
= Int_const_expr
;
2771 expr
->ctype
= size_t_ctype
;
2773 struct expression
*idx
= expr
->index
, *m
;
2774 struct symbol
*i_type
= evaluate_expression(idx
);
2775 int i_class
= classify_type(i_type
, &i_type
);
2776 if (!is_int(i_class
)) {
2777 expression_error(expr
, "non-integer index");
2780 unrestrict(idx
, i_class
, &i_type
);
2781 idx
= cast_to(idx
, size_t_ctype
);
2782 m
= alloc_const_expression(expr
->pos
,
2783 ctype
->bit_size
>> 3);
2784 m
->ctype
= size_t_ctype
;
2785 m
->flags
= Int_const_expr
;
2786 expr
->type
= EXPR_BINOP
;
2790 expr
->ctype
= size_t_ctype
;
2791 expr
->flags
= m
->flags
& idx
->flags
& Int_const_expr
;
2795 struct expression
*copy
= __alloc_expression(0);
2797 if (e
->type
== EXPR_OFFSETOF
)
2799 if (!evaluate_expression(e
))
2801 expr
->type
= EXPR_BINOP
;
2802 expr
->flags
= e
->flags
& copy
->flags
& Int_const_expr
;
2804 expr
->ctype
= size_t_ctype
;
2808 return size_t_ctype
;
2811 struct symbol
*evaluate_expression(struct expression
*expr
)
2818 switch (expr
->type
) {
2821 expression_error(expr
, "value expression without a type");
2824 return evaluate_string(expr
);
2826 return evaluate_symbol_expression(expr
);
2828 if (!evaluate_expression(expr
->left
))
2830 if (!evaluate_expression(expr
->right
))
2832 return evaluate_binop(expr
);
2834 return evaluate_logical(expr
);
2836 evaluate_expression(expr
->left
);
2837 if (!evaluate_expression(expr
->right
))
2839 return evaluate_comma(expr
);
2841 if (!evaluate_expression(expr
->left
))
2843 if (!evaluate_expression(expr
->right
))
2845 return evaluate_compare(expr
);
2846 case EXPR_ASSIGNMENT
:
2847 if (!evaluate_expression(expr
->left
))
2849 if (!evaluate_expression(expr
->right
))
2851 return evaluate_assignment(expr
);
2853 if (!evaluate_expression(expr
->unop
))
2855 return evaluate_preop(expr
);
2857 if (!evaluate_expression(expr
->unop
))
2859 return evaluate_postop(expr
);
2861 case EXPR_FORCE_CAST
:
2862 case EXPR_IMPLIED_CAST
:
2863 return evaluate_cast(expr
);
2865 return evaluate_sizeof(expr
);
2866 case EXPR_PTRSIZEOF
:
2867 return evaluate_ptrsizeof(expr
);
2869 return evaluate_alignof(expr
);
2871 return evaluate_member_dereference(expr
);
2873 return evaluate_call(expr
);
2875 case EXPR_CONDITIONAL
:
2876 return evaluate_conditional_expression(expr
);
2877 case EXPR_STATEMENT
:
2878 expr
->ctype
= evaluate_statement(expr
->statement
);
2882 expr
->ctype
= &ptr_ctype
;
2886 /* Evaluate the type of the symbol .. */
2887 evaluate_symbol(expr
->symbol
);
2888 /* .. but the type of the _expression_ is a "type" */
2889 expr
->ctype
= &type_ctype
;
2893 return evaluate_offsetof(expr
);
2895 /* These can not exist as stand-alone expressions */
2896 case EXPR_INITIALIZER
:
2897 case EXPR_IDENTIFIER
:
2900 expression_error(expr
, "internal front-end error: initializer in expression");
2903 expression_error(expr
, "internal front-end error: SLICE re-evaluated");
2909 static void check_duplicates(struct symbol
*sym
)
2912 struct symbol
*next
= sym
;
2914 while ((next
= next
->same_symbol
) != NULL
) {
2915 const char *typediff
;
2916 evaluate_symbol(next
);
2918 typediff
= type_difference(sym
, next
, 0, 0);
2920 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2921 show_ident(sym
->ident
),
2922 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
2927 unsigned long mod
= sym
->ctype
.modifiers
;
2928 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
2930 if (!(mod
& MOD_TOPLEVEL
))
2934 if (sym
->ident
== &main_ident
)
2936 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
2940 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
2942 struct symbol
*base_type
;
2950 sym
= examine_symbol_type(sym
);
2951 base_type
= get_base_type(sym
);
2955 /* Evaluate the initializers */
2956 if (sym
->initializer
)
2957 evaluate_initializer(sym
, &sym
->initializer
);
2959 /* And finally, evaluate the body of the symbol too */
2960 if (base_type
->type
== SYM_FN
) {
2961 struct symbol
*curr
= current_fn
;
2963 current_fn
= base_type
;
2965 examine_fn_arguments(base_type
);
2966 if (!base_type
->stmt
&& base_type
->inline_stmt
)
2968 if (base_type
->stmt
)
2969 evaluate_statement(base_type
->stmt
);
2977 void evaluate_symbol_list(struct symbol_list
*list
)
2981 FOR_EACH_PTR(list
, sym
) {
2982 evaluate_symbol(sym
);
2983 check_duplicates(sym
);
2984 } END_FOR_EACH_PTR(sym
);
2987 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
2989 struct expression
*expr
= stmt
->expression
;
2990 struct symbol
*fntype
;
2992 evaluate_expression(expr
);
2993 fntype
= current_fn
->ctype
.base_type
;
2994 if (!fntype
|| fntype
== &void_ctype
) {
2995 if (expr
&& expr
->ctype
!= &void_ctype
)
2996 expression_error(expr
, "return expression in %s function", fntype
?"void":"typeless");
2997 if (expr
&& Wreturn_void
)
2998 warning(stmt
->pos
, "returning void-valued expression");
3003 sparse_error(stmt
->pos
, "return with no return value");
3008 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, "return expression");
3012 static void evaluate_if_statement(struct statement
*stmt
)
3014 if (!stmt
->if_conditional
)
3017 evaluate_conditional(stmt
->if_conditional
, 0);
3018 evaluate_statement(stmt
->if_true
);
3019 evaluate_statement(stmt
->if_false
);
3022 static void evaluate_iterator(struct statement
*stmt
)
3024 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
3025 evaluate_conditional(stmt
->iterator_post_condition
,1);
3026 evaluate_statement(stmt
->iterator_pre_statement
);
3027 evaluate_statement(stmt
->iterator_statement
);
3028 evaluate_statement(stmt
->iterator_post_statement
);
3031 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
3033 switch (*constraint
) {
3034 case '=': /* Assignment */
3035 case '+': /* Update */
3038 expression_error(expr
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
3042 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
3044 switch (*constraint
) {
3045 case '=': /* Assignment */
3046 case '+': /* Update */
3047 expression_error(expr
, "input constraint with assignment (\"%s\")", constraint
);
3051 static void evaluate_asm_statement(struct statement
*stmt
)
3053 struct expression
*expr
;
3056 expr
= stmt
->asm_string
;
3057 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3058 sparse_error(stmt
->pos
, "need constant string for inline asm");
3063 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
3064 struct ident
*ident
;
3067 case 0: /* Identifier */
3069 ident
= (struct ident
*)expr
;
3072 case 1: /* Constraint */
3074 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3075 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
3076 *THIS_ADDRESS(expr
) = NULL
;
3079 verify_output_constraint(expr
, expr
->string
->data
);
3082 case 2: /* Expression */
3084 if (!evaluate_expression(expr
))
3086 if (!lvalue_expression(expr
))
3087 warning(expr
->pos
, "asm output is not an lvalue");
3088 evaluate_assign_to(expr
, expr
->ctype
);
3091 } END_FOR_EACH_PTR(expr
);
3094 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
3095 struct ident
*ident
;
3098 case 0: /* Identifier */
3100 ident
= (struct ident
*)expr
;
3103 case 1: /* Constraint */
3105 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3106 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
3107 *THIS_ADDRESS(expr
) = NULL
;
3110 verify_input_constraint(expr
, expr
->string
->data
);
3113 case 2: /* Expression */
3115 if (!evaluate_expression(expr
))
3119 } END_FOR_EACH_PTR(expr
);
3121 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
3123 sparse_error(stmt
->pos
, "bad asm output");
3126 if (expr
->type
== EXPR_STRING
)
3128 expression_error(expr
, "asm clobber is not a string");
3129 } END_FOR_EACH_PTR(expr
);
3132 static void evaluate_case_statement(struct statement
*stmt
)
3134 evaluate_expression(stmt
->case_expression
);
3135 evaluate_expression(stmt
->case_to
);
3136 evaluate_statement(stmt
->case_statement
);
3139 static void check_case_type(struct expression
*switch_expr
,
3140 struct expression
*case_expr
,
3141 struct expression
**enumcase
)
3143 struct symbol
*switch_type
, *case_type
;
3149 switch_type
= switch_expr
->ctype
;
3150 case_type
= evaluate_expression(case_expr
);
3152 if (!switch_type
|| !case_type
)
3156 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
3157 else if (is_enum_type(case_type
))
3158 *enumcase
= case_expr
;
3161 sclass
= classify_type(switch_type
, &switch_type
);
3162 cclass
= classify_type(case_type
, &case_type
);
3164 /* both should be arithmetic */
3165 if (!(sclass
& cclass
& TYPE_NUM
))
3168 /* neither should be floating */
3169 if ((sclass
| cclass
) & TYPE_FLOAT
)
3172 /* if neither is restricted, we are OK */
3173 if (!((sclass
| cclass
) & TYPE_RESTRICT
))
3176 if (!restricted_binop_type(SPECIAL_EQUAL
, case_expr
, switch_expr
,
3177 cclass
, sclass
, case_type
, switch_type
))
3178 warning(case_expr
->pos
, "restricted degrades to integer");
3183 expression_error(case_expr
, "incompatible types for 'case' statement");
3186 static void evaluate_switch_statement(struct statement
*stmt
)
3189 struct expression
*enumcase
= NULL
;
3190 struct expression
**enumcase_holder
= &enumcase
;
3191 struct expression
*sel
= stmt
->switch_expression
;
3193 evaluate_expression(sel
);
3194 evaluate_statement(stmt
->switch_statement
);
3197 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
3198 enumcase_holder
= NULL
; /* Only check cases against switch */
3200 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
3201 struct statement
*case_stmt
= sym
->stmt
;
3202 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
3203 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
3204 } END_FOR_EACH_PTR(sym
);
3207 struct symbol
*evaluate_statement(struct statement
*stmt
)
3212 switch (stmt
->type
) {
3213 case STMT_DECLARATION
: {
3215 FOR_EACH_PTR(stmt
->declaration
, s
) {
3217 } END_FOR_EACH_PTR(s
);
3222 return evaluate_return_expression(stmt
);
3224 case STMT_EXPRESSION
:
3225 if (!evaluate_expression(stmt
->expression
))
3227 if (stmt
->expression
->ctype
== &null_ctype
)
3228 stmt
->expression
= cast_to(stmt
->expression
, &ptr_ctype
);
3229 return degenerate(stmt
->expression
);
3231 case STMT_COMPOUND
: {
3232 struct statement
*s
;
3233 struct symbol
*type
= NULL
;
3235 /* Evaluate the return symbol in the compound statement */
3236 evaluate_symbol(stmt
->ret
);
3239 * Then, evaluate each statement, making the type of the
3240 * compound statement be the type of the last statement
3242 type
= evaluate_statement(stmt
->args
);
3243 FOR_EACH_PTR(stmt
->stmts
, s
) {
3244 type
= evaluate_statement(s
);
3245 } END_FOR_EACH_PTR(s
);
3251 evaluate_if_statement(stmt
);
3254 evaluate_iterator(stmt
);
3257 evaluate_switch_statement(stmt
);
3260 evaluate_case_statement(stmt
);
3263 return evaluate_statement(stmt
->label_statement
);
3265 evaluate_expression(stmt
->goto_expression
);
3270 evaluate_asm_statement(stmt
);
3273 evaluate_expression(stmt
->expression
);
3276 evaluate_expression(stmt
->range_expression
);
3277 evaluate_expression(stmt
->range_low
);
3278 evaluate_expression(stmt
->range_high
);