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
= bytes_to_bits(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
= bytes_to_bits(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 /* type has come from classify_type and is an integer type */
102 static inline struct symbol
*integer_promotion(struct symbol
*type
)
104 struct symbol
*orig_type
= type
;
105 unsigned long mod
= type
->ctype
.modifiers
;
106 int width
= type
->bit_size
;
109 * Bitfields always promote to the base type,
110 * even if the bitfield might be bigger than
113 if (type
->type
== SYM_BITFIELD
) {
114 type
= type
->ctype
.base_type
;
117 mod
= type
->ctype
.modifiers
;
118 if (width
< bits_in_int
)
121 /* If char/short has as many bits as int, it still gets "promoted" */
122 if (mod
& (MOD_CHAR
| MOD_SHORT
)) {
123 if (mod
& MOD_UNSIGNED
)
131 * integer part of usual arithmetic conversions:
132 * integer promotions are applied
133 * if left and right are identical, we are done
134 * if signedness is the same, convert one with lower rank
135 * unless unsigned argument has rank lower than signed one, convert the
137 * if signed argument is bigger than unsigned one, convert the unsigned.
138 * otherwise, convert signed.
140 * Leaving aside the integer promotions, that is equivalent to
141 * if identical, don't convert
142 * if left is bigger than right, convert right
143 * if right is bigger than left, convert right
144 * otherwise, if signedness is the same, convert one with lower rank
145 * otherwise convert the signed one.
147 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
149 unsigned long lmod
, rmod
;
151 left
= integer_promotion(left
);
152 right
= integer_promotion(right
);
157 if (left
->bit_size
> right
->bit_size
)
160 if (right
->bit_size
> left
->bit_size
)
163 lmod
= left
->ctype
.modifiers
;
164 rmod
= right
->ctype
.modifiers
;
165 if ((lmod
^ rmod
) & MOD_UNSIGNED
) {
166 if (lmod
& MOD_UNSIGNED
)
168 } else if ((lmod
& ~rmod
) & (MOD_LONG_ALL
))
176 static int same_cast_type(struct symbol
*orig
, struct symbol
*new)
178 return orig
->bit_size
== new->bit_size
&& orig
->bit_offset
== new->bit_offset
;
181 static struct symbol
*base_type(struct symbol
*node
, unsigned long *modp
, unsigned long *asp
)
183 unsigned long mod
, as
;
187 mod
|= node
->ctype
.modifiers
;
188 as
|= node
->ctype
.attribute
->as
;
189 if (node
->type
== SYM_NODE
) {
190 node
= node
->ctype
.base_type
;
195 *modp
= mod
& ~MOD_IGNORE
;
200 static int is_same_type(struct expression
*expr
, struct symbol
*new)
202 struct symbol
*old
= expr
->ctype
;
203 unsigned long oldmod
, newmod
, oldas
, newas
;
205 old
= base_type(old
, &oldmod
, &oldas
);
206 new = base_type(new, &newmod
, &newas
);
208 /* Same base type, same address space? */
209 if (old
== new && oldas
== newas
) {
210 unsigned long difmod
;
212 /* Check the modifier bits. */
213 difmod
= (oldmod
^ newmod
) & ~MOD_NOCAST
;
215 /* Exact same type? */
220 * Not the same type, but differs only in "const".
221 * Don't warn about MOD_NOCAST.
223 if (difmod
== MOD_CONST
)
226 if ((oldmod
| newmod
) & MOD_NOCAST
) {
227 const char *tofrom
= "to/from";
228 if (!(newmod
& MOD_NOCAST
))
230 if (!(oldmod
& MOD_NOCAST
))
232 warning(expr
->pos
, "implicit cast %s nocast type", tofrom
);
238 warn_for_different_enum_types (struct position pos
,
239 struct symbol
*typea
,
240 struct symbol
*typeb
)
244 if (typea
->type
== SYM_NODE
)
245 typea
= typea
->ctype
.base_type
;
246 if (typeb
->type
== SYM_NODE
)
247 typeb
= typeb
->ctype
.base_type
;
252 if (typea
->type
== SYM_ENUM
&& typeb
->type
== SYM_ENUM
) {
253 warning(pos
, "mixing different enum types");
254 info(pos
, " %s versus", show_typename(typea
));
255 info(pos
, " %s", show_typename(typeb
));
260 * This gets called for implicit casts in assignments and
261 * integer promotion. We often want to try to move the
262 * cast down, because the ops involved may have been
263 * implicitly cast up, and we can get rid of the casts
266 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
268 struct expression
*expr
;
270 warn_for_different_enum_types (old
->pos
, old
->ctype
, type
);
272 if (old
->ctype
!= &null_ctype
&& is_same_type(old
, type
))
276 * See if we can simplify the op. Move the cast down.
280 if (old
->ctype
->bit_size
< type
->bit_size
)
282 if (old
->op
== '~') {
284 old
->unop
= cast_to(old
->unop
, type
);
289 case EXPR_IMPLIED_CAST
:
290 warn_for_different_enum_types(old
->pos
, old
->ctype
, type
);
292 if (old
->ctype
->bit_size
>= type
->bit_size
) {
293 struct expression
*orig
= old
->cast_expression
;
294 if (same_cast_type(orig
->ctype
, type
))
296 if (old
->ctype
->bit_offset
== type
->bit_offset
) {
298 old
->cast_type
= type
;
308 expr
= alloc_expression(old
->pos
, EXPR_IMPLIED_CAST
);
309 expr
->flags
= old
->flags
;
311 expr
->cast_type
= type
;
312 expr
->cast_expression
= old
;
327 static inline int classify_type(struct symbol
*type
, struct symbol
**base
)
329 static int type_class
[SYM_BAD
+ 1] = {
330 [SYM_PTR
] = TYPE_PTR
,
331 [SYM_FN
] = TYPE_PTR
| TYPE_FN
,
332 [SYM_ARRAY
] = TYPE_PTR
| TYPE_COMPOUND
,
333 [SYM_STRUCT
] = TYPE_COMPOUND
,
334 [SYM_UNION
] = TYPE_COMPOUND
,
335 [SYM_BITFIELD
] = TYPE_NUM
| TYPE_BITFIELD
,
336 [SYM_RESTRICT
] = TYPE_NUM
| TYPE_RESTRICT
,
337 [SYM_FOULED
] = TYPE_NUM
| TYPE_RESTRICT
| TYPE_FOULED
,
339 if (type
->type
== SYM_NODE
)
340 type
= type
->ctype
.base_type
;
341 if (type
->type
== SYM_TYPEOF
) {
342 type
= evaluate_expression(type
->initializer
);
345 else if (type
->type
== SYM_NODE
)
346 type
= type
->ctype
.base_type
;
348 if (type
->type
== SYM_ENUM
)
349 type
= type
->ctype
.base_type
;
351 if (type
->type
== SYM_BASETYPE
) {
352 if (type
->ctype
.base_type
== &int_type
)
354 if (type
->ctype
.base_type
== &fp_type
)
355 return TYPE_NUM
| TYPE_FLOAT
;
357 return type_class
[type
->type
];
360 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
362 static inline int is_string_type(struct symbol
*type
)
364 if (type
->type
== SYM_NODE
)
365 type
= type
->ctype
.base_type
;
366 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
369 static struct symbol
*bad_expr_type(struct expression
*expr
)
371 sparse_error(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
372 switch (expr
->type
) {
375 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
376 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
380 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
387 return expr
->ctype
= &bad_ctype
;
390 static int restricted_value(struct expression
*v
, struct symbol
*type
)
392 if (v
->type
!= EXPR_VALUE
)
399 static int restricted_binop(int op
, struct symbol
*type
)
404 case SPECIAL_AND_ASSIGN
:
405 case SPECIAL_OR_ASSIGN
:
406 case SPECIAL_XOR_ASSIGN
:
407 return 1; /* unfoul */
411 return 2; /* keep fouled */
413 case SPECIAL_NOTEQUAL
:
414 return 3; /* warn if fouled */
420 static int restricted_unop(int op
, struct symbol
**type
)
423 if ((*type
)->bit_size
< bits_in_int
)
424 *type
= befoul(*type
);
431 /* type should be SYM_FOULED */
432 static inline struct symbol
*unfoul(struct symbol
*type
)
434 return type
->ctype
.base_type
;
437 static struct symbol
*restricted_binop_type(int op
,
438 struct expression
*left
,
439 struct expression
*right
,
440 int lclass
, int rclass
,
441 struct symbol
*ltype
,
442 struct symbol
*rtype
)
444 struct symbol
*ctype
= NULL
;
445 if (lclass
& TYPE_RESTRICT
) {
446 if (rclass
& TYPE_RESTRICT
) {
447 if (ltype
== rtype
) {
449 } else if (lclass
& TYPE_FOULED
) {
450 if (unfoul(ltype
) == rtype
)
452 } else if (rclass
& TYPE_FOULED
) {
453 if (unfoul(rtype
) == ltype
)
457 if (!restricted_value(right
, ltype
))
460 } else if (!restricted_value(left
, rtype
))
464 switch (restricted_binop(op
, ctype
)) {
466 if ((lclass
^ rclass
) & TYPE_FOULED
)
467 ctype
= unfoul(ctype
);
470 if (!(lclass
& rclass
& TYPE_FOULED
))
482 static inline void unrestrict(struct expression
*expr
,
483 int class, struct symbol
**ctype
)
485 if (class & TYPE_RESTRICT
) {
486 if (class & TYPE_FOULED
)
487 *ctype
= unfoul(*ctype
);
488 warning(expr
->pos
, "%s degrades to integer",
489 show_typename(*ctype
));
490 *ctype
= (*ctype
)->ctype
.base_type
; /* get to arithmetic type */
494 static struct symbol
*usual_conversions(int op
,
495 struct expression
*left
,
496 struct expression
*right
,
497 int lclass
, int rclass
,
498 struct symbol
*ltype
,
499 struct symbol
*rtype
)
501 struct symbol
*ctype
;
503 warn_for_different_enum_types(right
->pos
, left
->ctype
, right
->ctype
);
505 if ((lclass
| rclass
) & TYPE_RESTRICT
)
509 if (!(lclass
& TYPE_FLOAT
)) {
510 if (!(rclass
& TYPE_FLOAT
))
511 return bigger_int_type(ltype
, rtype
);
514 } else if (rclass
& TYPE_FLOAT
) {
515 unsigned long lmod
= ltype
->ctype
.modifiers
;
516 unsigned long rmod
= rtype
->ctype
.modifiers
;
517 if (rmod
& ~lmod
& (MOD_LONG_ALL
))
525 ctype
= restricted_binop_type(op
, left
, right
,
526 lclass
, rclass
, ltype
, rtype
);
530 unrestrict(left
, lclass
, <ype
);
531 unrestrict(right
, rclass
, &rtype
);
536 static inline int lvalue_expression(struct expression
*expr
)
538 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
541 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*itype
)
543 struct expression
*index
= expr
->right
;
544 struct symbol
*ctype
, *base
;
547 classify_type(degenerate(expr
->left
), &ctype
);
548 base
= examine_pointer_target(ctype
);
551 expression_error(expr
, "missing type information");
554 if (is_function(base
)) {
555 expression_error(expr
, "arithmetics on pointers to functions");
559 /* Get the size of whatever the pointer points to */
560 multiply
= is_void_type(base
) ? 1 : bits_to_bytes(base
->bit_size
);
562 if (ctype
== &null_ctype
)
566 if (multiply
== 1 && itype
->bit_size
>= bits_in_pointer
)
569 if (index
->type
== EXPR_VALUE
) {
570 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
571 unsigned long long v
= index
->value
, mask
;
572 mask
= 1ULL << (itype
->bit_size
- 1);
578 mask
= 1ULL << (bits_in_pointer
- 1);
579 v
&= mask
| (mask
- 1);
581 val
->ctype
= ssize_t_ctype
;
586 if (itype
->bit_size
< bits_in_pointer
)
587 index
= cast_to(index
, ssize_t_ctype
);
590 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
591 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
593 val
->ctype
= ssize_t_ctype
;
594 val
->value
= multiply
;
597 mul
->ctype
= ssize_t_ctype
;
607 static void examine_fn_arguments(struct symbol
*fn
);
609 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
611 const char *type_difference(struct ctype
*c1
, struct ctype
*c2
,
612 unsigned long mod1
, unsigned long mod2
)
614 unsigned long as1
= c1
->attribute
->as
, as2
= c2
->attribute
->as
;
615 struct symbol
*t1
= c1
->base_type
;
616 struct symbol
*t2
= c2
->base_type
;
617 int move1
= 1, move2
= 1;
618 mod1
|= c1
->modifiers
;
619 mod2
|= c2
->modifiers
;
624 struct symbol
*base1
= t1
->ctype
.base_type
;
625 struct symbol
*base2
= t2
->ctype
.base_type
;
628 * FIXME! Collect alignment and context too here!
631 if (t1
&& t1
->type
!= SYM_PTR
) {
632 mod1
|= t1
->ctype
.modifiers
;
633 as1
|= t1
->ctype
.attribute
->as
;
639 if (t2
&& t2
->type
!= SYM_PTR
) {
640 mod2
|= t2
->ctype
.modifiers
;
641 as2
|= t2
->ctype
.attribute
->as
;
649 return "different types";
651 if (t1
->type
== SYM_NODE
|| t1
->type
== SYM_ENUM
) {
659 if (t2
->type
== SYM_NODE
|| t2
->type
== SYM_ENUM
) {
669 if (type
!= t2
->type
)
670 return "different base types";
674 sparse_error(t1
->pos
,
675 "internal error: bad type in derived(%d)",
679 return "different base types";
682 /* allow definition of incomplete structs and unions */
683 if (t1
->ident
== t2
->ident
)
685 return "different base types";
687 /* XXX: we ought to compare sizes */
691 return "different address spaces";
692 /* MOD_SPECIFIER is due to idiocy in parse.c */
693 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SPECIFIER
)
694 return "different modifiers";
695 /* we could be lazier here */
696 base1
= examine_pointer_target(t1
);
697 base2
= examine_pointer_target(t2
);
698 mod1
= t1
->ctype
.modifiers
;
699 as1
= t1
->ctype
.attribute
->as
;
700 mod2
= t2
->ctype
.modifiers
;
701 as2
= t2
->ctype
.attribute
->as
;
704 struct symbol
*arg1
, *arg2
;
708 return "different address spaces";
709 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
710 return "different modifiers";
711 mod1
= t1
->ctype
.modifiers
;
712 as1
= t1
->ctype
.attribute
->as
;
713 mod2
= t2
->ctype
.modifiers
;
714 as2
= t2
->ctype
.attribute
->as
;
716 if (base1
->variadic
!= base2
->variadic
)
717 return "incompatible variadic arguments";
718 examine_fn_arguments(t1
);
719 examine_fn_arguments(t2
);
720 PREPARE_PTR_LIST(t1
->arguments
, arg1
);
721 PREPARE_PTR_LIST(t2
->arguments
, arg2
);
728 return "different argument counts";
729 diffstr
= type_difference(&arg1
->ctype
,
733 static char argdiff
[80];
734 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diffstr
);
741 FINISH_PTR_LIST(arg2
);
742 FINISH_PTR_LIST(arg1
);
747 return "different address spaces";
749 return "different base types";
750 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
754 return "different type sizes";
755 else if (diff
& ~MOD_SIGNEDNESS
)
756 return "different modifiers";
758 return "different signedness";
764 return "different address spaces";
765 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
766 return "different modifiers";
770 static void bad_null(struct expression
*expr
)
772 if (Wnon_pointer_null
)
773 warning(expr
->pos
, "Using plain integer as NULL pointer");
776 static unsigned long target_qualifiers(struct symbol
*type
)
778 unsigned long mod
= type
->ctype
.modifiers
& MOD_IGN
;
779 if (type
->ctype
.base_type
&& type
->ctype
.base_type
->type
== SYM_ARRAY
)
784 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
)
786 const char *typediff
;
787 struct symbol
*ltype
, *rtype
;
788 struct expression
*l
= expr
->left
;
789 struct expression
*r
= expr
->right
;
790 struct symbol
*lbase
;
792 classify_type(degenerate(l
), <ype
);
793 classify_type(degenerate(r
), &rtype
);
795 lbase
= examine_pointer_target(ltype
);
796 examine_pointer_target(rtype
);
797 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
798 target_qualifiers(rtype
),
799 target_qualifiers(ltype
));
801 expression_error(expr
, "subtraction of different types can't work (%s)", typediff
);
803 if (is_function(lbase
)) {
804 expression_error(expr
, "subtraction of functions? Share your drugs");
808 expr
->ctype
= ssize_t_ctype
;
809 if (lbase
->bit_size
> bits_in_char
) {
810 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
811 struct expression
*div
= expr
;
812 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
813 unsigned long value
= bits_to_bytes(lbase
->bit_size
);
815 val
->ctype
= size_t_ctype
;
818 if (value
& (value
-1)) {
819 if (Wptr_subtraction_blows
)
820 warning(expr
->pos
, "potentially expensive pointer subtraction");
824 sub
->ctype
= ssize_t_ctype
;
833 return ssize_t_ctype
;
836 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
838 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
840 struct symbol
*ctype
;
845 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
846 warning(expr
->pos
, "assignment expression in conditional");
848 ctype
= evaluate_expression(expr
);
850 if (is_safe_type(ctype
))
851 warning(expr
->pos
, "testing a 'safe expression'");
857 static struct symbol
*evaluate_logical(struct expression
*expr
)
859 if (!evaluate_conditional(expr
->left
, 0))
861 if (!evaluate_conditional(expr
->right
, 0))
864 expr
->ctype
= &bool_ctype
;
866 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
872 static struct symbol
*evaluate_binop(struct expression
*expr
)
874 struct symbol
*ltype
, *rtype
, *ctype
;
875 int lclass
= classify_type(expr
->left
->ctype
, <ype
);
876 int rclass
= classify_type(expr
->right
->ctype
, &rtype
);
880 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
884 /* number op number */
885 if (lclass
& rclass
& TYPE_NUM
) {
886 if ((lclass
| rclass
) & TYPE_FLOAT
) {
888 case '+': case '-': case '*': case '/':
891 return bad_expr_type(expr
);
895 if (op
== SPECIAL_LEFTSHIFT
|| op
== SPECIAL_RIGHTSHIFT
) {
896 // shifts do integer promotions, but that's it.
897 unrestrict(expr
->left
, lclass
, <ype
);
898 unrestrict(expr
->right
, rclass
, &rtype
);
899 ctype
= ltype
= integer_promotion(ltype
);
900 rtype
= integer_promotion(rtype
);
902 // The rest do usual conversions
903 const unsigned left_not
= expr
->left
->type
== EXPR_PREOP
904 && expr
->left
->op
== '!';
905 const unsigned right_not
= expr
->right
->type
== EXPR_PREOP
906 && expr
->right
->op
== '!';
907 if ((op
== '&' || op
== '|') && (left_not
|| right_not
))
908 warning(expr
->pos
, "dubious: %sx %c %sy",
911 right_not
? "!" : "");
913 ltype
= usual_conversions(op
, expr
->left
, expr
->right
,
914 lclass
, rclass
, ltype
, rtype
);
915 ctype
= rtype
= ltype
;
918 expr
->left
= cast_to(expr
->left
, ltype
);
919 expr
->right
= cast_to(expr
->right
, rtype
);
924 /* pointer (+|-) integer */
925 if (lclass
& TYPE_PTR
&& is_int(rclass
) && (op
== '+' || op
== '-')) {
926 unrestrict(expr
->right
, rclass
, &rtype
);
927 return evaluate_ptr_add(expr
, rtype
);
930 /* integer + pointer */
931 if (rclass
& TYPE_PTR
&& is_int(lclass
) && op
== '+') {
932 struct expression
*index
= expr
->left
;
933 unrestrict(index
, lclass
, <ype
);
934 expr
->left
= expr
->right
;
936 return evaluate_ptr_add(expr
, ltype
);
939 /* pointer - pointer */
940 if (lclass
& rclass
& TYPE_PTR
&& expr
->op
== '-')
941 return evaluate_ptr_sub(expr
);
943 return bad_expr_type(expr
);
946 static struct symbol
*evaluate_comma(struct expression
*expr
)
948 expr
->ctype
= degenerate(expr
->right
);
949 if (expr
->ctype
== &null_ctype
)
950 expr
->ctype
= &ptr_ctype
;
951 expr
->flags
&= expr
->left
->flags
& expr
->right
->flags
;
955 static int modify_for_unsigned(int op
)
958 op
= SPECIAL_UNSIGNED_LT
;
960 op
= SPECIAL_UNSIGNED_GT
;
961 else if (op
== SPECIAL_LTE
)
962 op
= SPECIAL_UNSIGNED_LTE
;
963 else if (op
== SPECIAL_GTE
)
964 op
= SPECIAL_UNSIGNED_GTE
;
968 static inline int is_null_pointer_constant(struct expression
*e
)
970 if (e
->ctype
== &null_ctype
)
972 if (!(e
->flags
& Int_const_expr
))
974 return is_zero_constant(e
) ? 2 : 0;
977 static struct symbol
*evaluate_compare(struct expression
*expr
)
979 struct expression
*left
= expr
->left
, *right
= expr
->right
;
980 struct symbol
*ltype
, *rtype
, *lbase
, *rbase
;
981 int lclass
= classify_type(degenerate(left
), <ype
);
982 int rclass
= classify_type(degenerate(right
), &rtype
);
983 struct symbol
*ctype
;
984 const char *typediff
;
987 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
992 if (is_type_type(ltype
) && is_type_type(rtype
))
995 if (is_safe_type(left
->ctype
) || is_safe_type(right
->ctype
))
996 warning(expr
->pos
, "testing a 'safe expression'");
998 /* number on number */
999 if (lclass
& rclass
& TYPE_NUM
) {
1000 ctype
= usual_conversions(expr
->op
, expr
->left
, expr
->right
,
1001 lclass
, rclass
, ltype
, rtype
);
1002 expr
->left
= cast_to(expr
->left
, ctype
);
1003 expr
->right
= cast_to(expr
->right
, ctype
);
1004 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
1005 expr
->op
= modify_for_unsigned(expr
->op
);
1009 /* at least one must be a pointer */
1010 if (!((lclass
| rclass
) & TYPE_PTR
))
1011 return bad_expr_type(expr
);
1013 /* equality comparisons can be with null pointer constants */
1014 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1015 int is_null1
= is_null_pointer_constant(left
);
1016 int is_null2
= is_null_pointer_constant(right
);
1021 if (is_null1
&& is_null2
) {
1022 int positive
= expr
->op
== SPECIAL_EQUAL
;
1023 expr
->type
= EXPR_VALUE
;
1024 expr
->value
= positive
;
1027 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1028 left
= cast_to(left
, rtype
);
1031 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1032 right
= cast_to(right
, ltype
);
1036 /* both should be pointers */
1037 if (!(lclass
& rclass
& TYPE_PTR
))
1038 return bad_expr_type(expr
);
1039 expr
->op
= modify_for_unsigned(expr
->op
);
1041 lbase
= examine_pointer_target(ltype
);
1042 rbase
= examine_pointer_target(rtype
);
1044 /* they also have special treatment for pointers to void */
1045 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1046 if (ltype
->ctype
.attribute
->as
== rtype
->ctype
.attribute
->as
) {
1047 if (lbase
== &void_ctype
) {
1048 right
= cast_to(right
, ltype
);
1051 if (rbase
== &void_ctype
) {
1052 left
= cast_to(left
, rtype
);
1058 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1059 target_qualifiers(rtype
),
1060 target_qualifiers(ltype
));
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
.attribute
->as
!= rtype
->ctype
.attribute
->as
) {
1151 typediff
= "different address spaces";
1155 /* need to be lazier here */
1156 lbase
= examine_pointer_target(ltype
);
1157 rbase
= examine_pointer_target(rtype
);
1158 qual
= target_qualifiers(ltype
) | target_qualifiers(rtype
);
1160 if (lbase
== &void_ctype
) {
1161 /* XXX: pointers to function should warn here */
1166 if (rbase
== &void_ctype
) {
1167 /* XXX: pointers to function should warn here */
1171 /* XXX: that should be pointer to composite */
1173 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
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 warning(expr
->pos
, "bad assignment (%s) to %s",
1233 show_special(op
), show_typename(t
));
1234 expr
->right
= cast_to(expr
->right
, target
);
1237 /* allowed assignments unfoul */
1238 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1240 if (!restricted_value(expr
->right
, t
))
1242 } else if (!(sclass
& TYPE_RESTRICT
))
1244 /* source and target would better be identical restricted */
1247 warning(expr
->pos
, "invalid assignment: %s", show_special(op
));
1248 info(expr
->pos
, " left side has type %s", show_typename(t
));
1249 info(expr
->pos
, " right side has type %s", show_typename(s
));
1250 expr
->right
= cast_to(expr
->right
, target
);
1253 if (tclass
== TYPE_PTR
&& is_int(sclass
)) {
1254 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1255 unrestrict(expr
->right
, sclass
, &s
);
1256 evaluate_ptr_add(expr
, s
);
1259 expression_error(expr
, "invalid pointer assignment");
1263 expression_error(expr
, "invalid assignment");
1267 expr
->right
= cast_to(expr
->right
, target
);
1271 static int whitelist_pointers(struct symbol
*t1
, struct symbol
*t2
)
1274 return 0; /* yes, 0 - we don't want a cast_to here */
1275 if (t1
== &void_ctype
)
1277 if (t2
== &void_ctype
)
1279 if (classify_type(t1
, &t1
) != TYPE_NUM
)
1281 if (classify_type(t2
, &t2
) != TYPE_NUM
)
1285 if (t1
->ctype
.modifiers
& t2
->ctype
.modifiers
& MOD_CHAR
)
1287 if ((t1
->ctype
.modifiers
^ t2
->ctype
.modifiers
) & MOD_SIZE
)
1292 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1293 struct expression
**rp
, const char *where
)
1295 const char *typediff
;
1296 struct symbol
*source
= degenerate(*rp
);
1297 struct symbol
*t
, *s
;
1298 int tclass
= classify_type(target
, &t
);
1299 int sclass
= classify_type(source
, &s
);
1301 if (tclass
& sclass
& TYPE_NUM
) {
1302 if (tclass
& TYPE_RESTRICT
) {
1303 /* allowed assignments unfoul */
1304 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1306 if (!restricted_value(*rp
, target
))
1310 } else if (!(sclass
& TYPE_RESTRICT
))
1312 typediff
= "different base types";
1316 if (tclass
== TYPE_PTR
) {
1317 unsigned long mod1
, mod2
;
1318 struct symbol
*b1
, *b2
;
1319 // NULL pointer is always OK
1320 int is_null
= is_null_pointer_constant(*rp
);
1326 if (!(sclass
& TYPE_PTR
)) {
1327 typediff
= "different base types";
1330 b1
= examine_pointer_target(t
);
1331 b2
= examine_pointer_target(s
);
1332 mod1
= target_qualifiers(t
);
1333 mod2
= target_qualifiers(s
);
1334 if (whitelist_pointers(b1
, b2
)) {
1336 * assignments to/from void * are OK, provided that
1337 * we do not remove qualifiers from pointed to [C]
1338 * or mix address spaces [sparse].
1340 if (t
->ctype
.attribute
->as
!= s
->ctype
.attribute
->as
) {
1341 typediff
= "different address spaces";
1345 typediff
= "different modifiers";
1350 /* It's OK if the target is more volatile or const than the source */
1351 typediff
= type_difference(&t
->ctype
, &s
->ctype
, 0, mod1
);
1357 if ((tclass
& TYPE_COMPOUND
) && s
== t
)
1360 if (tclass
& TYPE_NUM
) {
1361 /* XXX: need to turn into comparison with NULL */
1362 if (t
== &bool_ctype
&& (sclass
& TYPE_PTR
))
1364 typediff
= "different base types";
1367 typediff
= "invalid types";
1370 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1371 info(expr
->pos
, " expected %s", show_typename(target
));
1372 info(expr
->pos
, " got %s", show_typename(source
));
1373 *rp
= cast_to(*rp
, target
);
1376 *rp
= cast_to(*rp
, target
);
1380 static void mark_assigned(struct expression
*expr
)
1386 switch (expr
->type
) {
1391 if (sym
->type
!= SYM_NODE
)
1393 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1397 mark_assigned(expr
->left
);
1398 mark_assigned(expr
->right
);
1401 case EXPR_FORCE_CAST
:
1402 mark_assigned(expr
->cast_expression
);
1405 mark_assigned(expr
->base
);
1413 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1415 if (type
->ctype
.modifiers
& MOD_CONST
)
1416 expression_error(left
, "assignment to const expression");
1418 /* We know left is an lvalue, so it's a "preop-*" */
1419 mark_assigned(left
->unop
);
1422 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1424 struct expression
*left
= expr
->left
;
1425 struct expression
*where
= expr
;
1426 struct symbol
*ltype
;
1428 if (!lvalue_expression(left
)) {
1429 expression_error(expr
, "not an lvalue");
1433 ltype
= left
->ctype
;
1435 if (expr
->op
!= '=') {
1436 if (!evaluate_assign_op(expr
))
1439 if (!compatible_assignment_types(where
, ltype
, &expr
->right
, "assignment"))
1443 evaluate_assign_to(left
, ltype
);
1445 expr
->ctype
= ltype
;
1449 static void examine_fn_arguments(struct symbol
*fn
)
1453 FOR_EACH_PTR(fn
->arguments
, s
) {
1454 struct symbol
*arg
= evaluate_symbol(s
);
1455 /* Array/function arguments silently degenerate into pointers */
1461 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1462 if (arg
->type
== SYM_ARRAY
)
1463 ptr
->ctype
= arg
->ctype
;
1465 ptr
->ctype
.base_type
= arg
;
1466 merge_attr(&ptr
->ctype
, &s
->ctype
);
1467 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
& MOD_PTRINHERIT
;
1469 s
->ctype
.base_type
= ptr
;
1470 s
->ctype
.attribute
= &null_attr
;
1471 s
->ctype
.modifiers
&= ~MOD_PTRINHERIT
;
1474 examine_symbol_type(s
);
1481 } END_FOR_EACH_PTR(s
);
1484 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1486 /* Take the modifiers of the pointer, and apply them to the member */
1487 mod
|= sym
->ctype
.modifiers
;
1488 if (sym
->ctype
.attribute
->as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1489 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1491 attr_set_as(&newsym
->ctype
, as
);
1492 newsym
->ctype
.modifiers
= mod
;
1498 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1500 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1501 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1503 node
->ctype
.base_type
= ptr
;
1504 ptr
->bit_size
= bits_in_pointer
;
1505 ptr
->ctype
.alignment
= pointer_alignment
;
1507 node
->bit_size
= bits_in_pointer
;
1508 node
->ctype
.alignment
= pointer_alignment
;
1511 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1512 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1513 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1515 if (sym
->type
== SYM_NODE
) {
1516 merge_attr(&ptr
->ctype
, &sym
->ctype
);
1517 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1518 sym
= sym
->ctype
.base_type
;
1520 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1521 merge_attr(&ptr
->ctype
, &sym
->ctype
);
1522 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1523 sym
= sym
->ctype
.base_type
;
1525 ptr
->ctype
.base_type
= sym
;
1530 /* Arrays degenerate into pointers on pointer arithmetic */
1531 static struct symbol
*degenerate(struct expression
*expr
)
1533 struct symbol
*ctype
, *base
;
1537 ctype
= expr
->ctype
;
1540 base
= examine_symbol_type(ctype
);
1541 if (ctype
->type
== SYM_NODE
)
1542 base
= ctype
->ctype
.base_type
;
1544 * Arrays degenerate into pointers to the entries, while
1545 * functions degenerate into pointers to themselves.
1546 * If array was part of non-lvalue compound, we create a copy
1547 * of that compound first and then act as if we were dealing with
1548 * the corresponding field in there.
1550 switch (base
->type
) {
1552 if (expr
->type
== EXPR_SLICE
) {
1553 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1554 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1556 a
->ctype
.base_type
= expr
->base
->ctype
;
1557 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1558 a
->array_size
= expr
->base
->ctype
->array_size
;
1560 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1562 e0
->ctype
= &lazy_ptr_ctype
;
1564 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1567 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1569 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1571 e2
->right
= expr
->base
;
1573 e2
->ctype
= expr
->base
->ctype
;
1575 if (expr
->r_bitpos
) {
1576 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1579 e3
->right
= alloc_const_expression(expr
->pos
,
1580 bits_to_bytes(expr
->r_bitpos
));
1581 e3
->ctype
= &lazy_ptr_ctype
;
1586 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1589 e4
->ctype
= &lazy_ptr_ctype
;
1592 expr
->type
= EXPR_PREOP
;
1596 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1597 expression_error(expr
, "strange non-value function or array");
1600 *expr
= *expr
->unop
;
1601 ctype
= create_pointer(expr
, ctype
, 1);
1602 expr
->ctype
= ctype
;
1609 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1611 struct expression
*op
= expr
->unop
;
1612 struct symbol
*ctype
;
1614 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1615 expression_error(expr
, "not addressable");
1622 if (expr
->type
== EXPR_SYMBOL
) {
1623 struct symbol
*sym
= expr
->symbol
;
1624 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1628 * symbol expression evaluation is lazy about the type
1629 * of the sub-expression, so we may have to generate
1630 * the type here if so..
1632 if (expr
->ctype
== &lazy_ptr_ctype
) {
1633 ctype
= create_pointer(expr
, ctype
, 0);
1634 expr
->ctype
= ctype
;
1640 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1642 struct expression
*op
= expr
->unop
;
1643 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1645 /* Simplify: *&(expr) => (expr) */
1646 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1652 /* Dereferencing a node drops all the node information. */
1653 if (ctype
->type
== SYM_NODE
)
1654 ctype
= ctype
->ctype
.base_type
;
1656 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1657 target
= ctype
->ctype
.base_type
;
1659 switch (ctype
->type
) {
1661 expression_error(expr
, "cannot dereference this type");
1664 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1665 merge_type(node
, ctype
);
1669 if (!lvalue_expression(op
)) {
1670 expression_error(op
, "non-lvalue array??");
1674 /* Do the implied "addressof" on the array */
1678 * When an array is dereferenced, we need to pick
1679 * up the attributes of the original node too..
1681 merge_type(node
, op
->ctype
);
1682 merge_type(node
, ctype
);
1686 node
->bit_size
= target
->bit_size
;
1687 node
->array_size
= target
->array_size
;
1694 * Unary post-ops: x++ and x--
1696 static struct symbol
*evaluate_postop(struct expression
*expr
)
1698 struct expression
*op
= expr
->unop
;
1699 struct symbol
*ctype
= op
->ctype
;
1700 int class = classify_type(ctype
, &ctype
);
1703 if (!class || class & TYPE_COMPOUND
) {
1704 expression_error(expr
, "need scalar for ++/--");
1707 if (!lvalue_expression(expr
->unop
)) {
1708 expression_error(expr
, "need lvalue expression for ++/--");
1712 if ((class & TYPE_RESTRICT
) && restricted_unop(expr
->op
, &ctype
))
1713 unrestrict(expr
, class, &ctype
);
1715 if (class & TYPE_NUM
) {
1717 } else if (class == TYPE_PTR
) {
1718 struct symbol
*target
= examine_pointer_target(ctype
);
1719 if (!is_function(target
))
1720 multiply
= bits_to_bytes(target
->bit_size
);
1724 evaluate_assign_to(op
, op
->ctype
);
1725 expr
->op_value
= multiply
;
1726 expr
->ctype
= ctype
;
1730 expression_error(expr
, "bad argument type for ++/--");
1734 static struct symbol
*evaluate_sign(struct expression
*expr
)
1736 struct symbol
*ctype
= expr
->unop
->ctype
;
1737 int class = classify_type(ctype
, &ctype
);
1738 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1740 /* should be an arithmetic type */
1741 if (!(class & TYPE_NUM
))
1742 return bad_expr_type(expr
);
1743 if (class & TYPE_RESTRICT
)
1746 if (!(class & TYPE_FLOAT
)) {
1747 ctype
= integer_promotion(ctype
);
1748 expr
->unop
= cast_to(expr
->unop
, ctype
);
1749 } else if (expr
->op
!= '~') {
1750 /* no conversions needed */
1752 return bad_expr_type(expr
);
1754 if (expr
->op
== '+')
1755 *expr
= *expr
->unop
;
1756 expr
->ctype
= ctype
;
1759 if (restricted_unop(expr
->op
, &ctype
))
1760 unrestrict(expr
, class, &ctype
);
1764 static struct symbol
*evaluate_preop(struct expression
*expr
)
1766 struct symbol
*ctype
= expr
->unop
->ctype
;
1770 *expr
= *expr
->unop
;
1776 return evaluate_sign(expr
);
1779 return evaluate_dereference(expr
);
1782 return evaluate_addressof(expr
);
1784 case SPECIAL_INCREMENT
:
1785 case SPECIAL_DECREMENT
:
1787 * From a type evaluation standpoint the preops are
1788 * the same as the postops
1790 return evaluate_postop(expr
);
1793 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1795 if (is_safe_type(ctype
))
1796 warning(expr
->pos
, "testing a 'safe expression'");
1797 if (is_float_type(ctype
)) {
1798 struct expression
*arg
= expr
->unop
;
1799 expr
->type
= EXPR_BINOP
;
1800 expr
->op
= SPECIAL_EQUAL
;
1802 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1803 expr
->right
->ctype
= ctype
;
1804 expr
->right
->fvalue
= 0;
1805 } else if (is_fouled_type(ctype
)) {
1806 warning(expr
->pos
, "%s degrades to integer",
1807 show_typename(ctype
->ctype
.base_type
));
1809 ctype
= &bool_ctype
;
1815 expr
->ctype
= ctype
;
1819 static struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1821 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1822 struct ptr_list
*list
= head
;
1828 for (i
= 0; i
< list
->nr
; i
++) {
1829 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1831 if (sym
->ident
!= ident
)
1833 *offset
= sym
->offset
;
1836 struct symbol
*ctype
= sym
->ctype
.base_type
;
1840 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1842 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1845 *offset
+= sym
->offset
;
1849 } while ((list
= list
->next
) != head
);
1853 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1855 struct expression
*add
;
1858 * Create a new add-expression
1860 * NOTE! Even if we just add zero, we need a new node
1861 * for the member pointer, since it has a different
1862 * type than the original pointer. We could make that
1863 * be just a cast, but the fact is, a node is a node,
1864 * so we might as well just do the "add zero" here.
1866 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1869 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1870 add
->right
->ctype
= &int_ctype
;
1871 add
->right
->value
= offset
;
1874 * The ctype of the pointer will be lazily evaluated if
1875 * we ever take the address of this member dereference..
1877 add
->ctype
= &lazy_ptr_ctype
;
1881 /* structure/union dereference */
1882 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1885 struct symbol
*ctype
, *member
;
1886 struct expression
*deref
= expr
->deref
, *add
;
1887 struct ident
*ident
= expr
->member
;
1891 if (!evaluate_expression(deref
))
1894 expression_error(expr
, "bad member name");
1898 ctype
= deref
->ctype
;
1899 examine_symbol_type(ctype
);
1900 address_space
= ctype
->ctype
.attribute
->as
;
1901 mod
= ctype
->ctype
.modifiers
;
1902 if (ctype
->type
== SYM_NODE
) {
1903 ctype
= ctype
->ctype
.base_type
;
1904 address_space
|= ctype
->ctype
.attribute
->as
;
1905 mod
|= ctype
->ctype
.modifiers
;
1907 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1908 expression_error(expr
, "expected structure or union");
1912 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1914 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1915 const char *name
= "<unnamed>";
1918 name
= ctype
->ident
->name
;
1919 namelen
= ctype
->ident
->len
;
1921 if (ctype
->symbol_list
)
1922 expression_error(expr
, "no member '%s' in %s %.*s",
1923 show_ident(ident
), type
, namelen
, name
);
1925 expression_error(expr
, "using member '%s' in "
1926 "incomplete %s %.*s", show_ident(ident
),
1927 type
, namelen
, name
);
1932 * The member needs to take on the address space and modifiers of
1933 * the "parent" type.
1935 member
= convert_to_as_mod(member
, address_space
, mod
);
1936 ctype
= get_base_type(member
);
1938 if (!lvalue_expression(deref
)) {
1939 if (deref
->type
!= EXPR_SLICE
) {
1943 expr
->base
= deref
->base
;
1944 expr
->r_bitpos
= deref
->r_bitpos
;
1946 expr
->r_bitpos
+= bytes_to_bits(offset
);
1947 expr
->type
= EXPR_SLICE
;
1948 expr
->r_nrbits
= member
->bit_size
;
1949 expr
->r_bitpos
+= member
->bit_offset
;
1950 expr
->ctype
= member
;
1954 deref
= deref
->unop
;
1955 expr
->deref
= deref
;
1957 add
= evaluate_offset(deref
, offset
);
1958 expr
->type
= EXPR_PREOP
;
1962 expr
->ctype
= member
;
1966 static int is_promoted(struct expression
*expr
)
1969 switch (expr
->type
) {
1972 case EXPR_CONDITIONAL
:
1996 static struct symbol
*evaluate_cast(struct expression
*);
1998 static struct symbol
*evaluate_type_information(struct expression
*expr
)
2000 struct symbol
*sym
= expr
->cast_type
;
2002 sym
= evaluate_expression(expr
->cast_expression
);
2006 * Expressions of restricted types will possibly get
2007 * promoted - check that here
2009 if (is_restricted_type(sym
)) {
2010 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
2012 } else if (is_fouled_type(sym
)) {
2016 examine_symbol_type(sym
);
2017 if (is_bitfield_type(sym
)) {
2018 expression_error(expr
, "trying to examine bitfield type");
2024 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
2026 struct symbol
*type
;
2029 type
= evaluate_type_information(expr
);
2033 size
= type
->bit_size
;
2035 if (size
< 0 && is_void_type(type
)) {
2036 warning(expr
->pos
, "expression using sizeof(void)");
2037 size
= bits_in_char
;
2040 if (size
== 1 && is_bool_type(type
)) {
2041 warning(expr
->pos
, "expression using sizeof bool");
2042 size
= bits_in_char
;
2045 if (is_function(type
->ctype
.base_type
)) {
2046 warning(expr
->pos
, "expression using sizeof on a function");
2047 size
= bits_in_char
;
2050 if ((size
< 0) || (size
& (bits_in_char
- 1)))
2051 expression_error(expr
, "cannot size expression");
2053 expr
->type
= EXPR_VALUE
;
2054 expr
->value
= bits_to_bytes(size
);
2056 expr
->ctype
= size_t_ctype
;
2057 return size_t_ctype
;
2060 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
2062 struct symbol
*type
;
2065 type
= evaluate_type_information(expr
);
2069 if (type
->type
== SYM_NODE
)
2070 type
= type
->ctype
.base_type
;
2073 switch (type
->type
) {
2077 type
= get_base_type(type
);
2081 expression_error(expr
, "expected pointer expression");
2084 size
= type
->bit_size
;
2085 if (size
& (bits_in_char
-1))
2087 expr
->type
= EXPR_VALUE
;
2088 expr
->value
= bits_to_bytes(size
);
2090 expr
->ctype
= size_t_ctype
;
2091 return size_t_ctype
;
2094 static struct symbol
*evaluate_alignof(struct expression
*expr
)
2096 struct symbol
*type
;
2098 type
= evaluate_type_information(expr
);
2102 expr
->type
= EXPR_VALUE
;
2103 expr
->value
= type
->ctype
.alignment
;
2105 expr
->ctype
= size_t_ctype
;
2106 return size_t_ctype
;
2109 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
2111 struct expression
*expr
;
2112 struct symbol_list
*argument_types
= fn
->arguments
;
2113 struct symbol
*argtype
;
2116 PREPARE_PTR_LIST(argument_types
, argtype
);
2117 FOR_EACH_PTR (head
, expr
) {
2118 struct expression
**p
= THIS_ADDRESS(expr
);
2119 struct symbol
*ctype
, *target
;
2120 ctype
= evaluate_expression(expr
);
2127 struct symbol
*type
;
2128 int class = classify_type(ctype
, &type
);
2129 if (is_int(class)) {
2130 *p
= cast_to(expr
, integer_promotion(type
));
2131 } else if (class & TYPE_FLOAT
) {
2132 unsigned long mod
= type
->ctype
.modifiers
;
2133 if (!(mod
& (MOD_LONG_ALL
)))
2134 *p
= cast_to(expr
, &double_ctype
);
2135 } else if (class & TYPE_PTR
) {
2136 if (expr
->ctype
== &null_ctype
)
2137 *p
= cast_to(expr
, &ptr_ctype
);
2142 static char where
[30];
2143 examine_symbol_type(target
);
2144 sprintf(where
, "argument %d", i
);
2145 compatible_assignment_types(expr
, target
, p
, where
);
2149 NEXT_PTR_LIST(argtype
);
2150 } END_FOR_EACH_PTR(expr
);
2151 FINISH_PTR_LIST(argtype
);
2155 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
2159 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
2160 if (sym
->ident
== ident
)
2162 } END_FOR_EACH_PTR(sym
);
2166 static void convert_index(struct expression
*e
)
2168 struct expression
*child
= e
->idx_expression
;
2169 unsigned from
= e
->idx_from
;
2170 unsigned to
= e
->idx_to
+ 1;
2172 e
->init_offset
= from
* bits_to_bytes(e
->ctype
->bit_size
);
2173 e
->init_nr
= to
- from
;
2174 e
->init_expr
= child
;
2177 static void convert_ident(struct expression
*e
)
2179 struct expression
*child
= e
->ident_expression
;
2180 struct symbol
*sym
= e
->field
;
2182 e
->init_offset
= sym
->offset
;
2184 e
->init_expr
= child
;
2187 static void convert_designators(struct expression
*e
)
2190 if (e
->type
== EXPR_INDEX
)
2192 else if (e
->type
== EXPR_IDENTIFIER
)
2200 static void excess(struct expression
*e
, const char *s
)
2202 warning(e
->pos
, "excessive elements in %s initializer", s
);
2206 * implicit designator for the first element
2208 static struct expression
*first_subobject(struct symbol
*ctype
, int class,
2209 struct expression
**v
)
2211 struct expression
*e
= *v
, *new;
2213 if (ctype
->type
== SYM_NODE
)
2214 ctype
= ctype
->ctype
.base_type
;
2216 if (class & TYPE_PTR
) { /* array */
2217 if (!ctype
->bit_size
)
2219 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2220 new->idx_expression
= e
;
2221 new->ctype
= ctype
->ctype
.base_type
;
2223 struct symbol
*field
, *p
;
2224 PREPARE_PTR_LIST(ctype
->symbol_list
, p
);
2225 while (p
&& !p
->ident
&& is_bitfield_type(p
))
2231 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2232 new->ident_expression
= e
;
2233 new->field
= new->ctype
= field
;
2240 * sanity-check explicit designators; return the innermost one or NULL
2241 * in case of error. Assign types.
2243 static struct expression
*check_designators(struct expression
*e
,
2244 struct symbol
*ctype
)
2246 struct expression
*last
= NULL
;
2249 if (ctype
->type
== SYM_NODE
)
2250 ctype
= ctype
->ctype
.base_type
;
2251 if (e
->type
== EXPR_INDEX
) {
2252 struct symbol
*type
;
2253 if (ctype
->type
!= SYM_ARRAY
) {
2254 err
= "array index in non-array";
2257 type
= ctype
->ctype
.base_type
;
2258 if (ctype
->bit_size
>= 0 && type
->bit_size
>= 0) {
2259 unsigned offset
= e
->idx_to
* type
->bit_size
;
2260 if (offset
>= ctype
->bit_size
) {
2261 err
= "index out of bounds in";
2265 e
->ctype
= ctype
= type
;
2268 if (!e
->idx_expression
) {
2272 e
= e
->idx_expression
;
2273 } else if (e
->type
== EXPR_IDENTIFIER
) {
2274 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2275 err
= "field name not in struct or union";
2278 ctype
= find_struct_ident(ctype
, e
->expr_ident
);
2280 err
= "unknown field name in";
2283 e
->field
= e
->ctype
= ctype
;
2285 if (!e
->ident_expression
) {
2289 e
= e
->ident_expression
;
2290 } else if (e
->type
== EXPR_POS
) {
2291 err
= "internal front-end error: EXPR_POS in";
2296 expression_error(e
, "%s initializer", err
);
2301 * choose the next subobject to initialize.
2303 * Get designators for next element, switch old ones to EXPR_POS.
2304 * Return the resulting expression or NULL if we'd run out of subobjects.
2305 * The innermost designator is returned in *v. Designators in old
2306 * are assumed to be already sanity-checked.
2308 static struct expression
*next_designators(struct expression
*old
,
2309 struct symbol
*ctype
,
2310 struct expression
*e
, struct expression
**v
)
2312 struct expression
*new = NULL
;
2316 if (old
->type
== EXPR_INDEX
) {
2317 struct expression
*copy
;
2320 copy
= next_designators(old
->idx_expression
,
2323 n
= old
->idx_to
+ 1;
2324 if (n
* old
->ctype
->bit_size
== ctype
->bit_size
) {
2329 *v
= new = alloc_expression(e
->pos
, EXPR_INDEX
);
2332 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2335 new->idx_from
= new->idx_to
= n
;
2336 new->idx_expression
= copy
;
2337 new->ctype
= old
->ctype
;
2339 } else if (old
->type
== EXPR_IDENTIFIER
) {
2340 struct expression
*copy
;
2341 struct symbol
*field
;
2343 copy
= next_designators(old
->ident_expression
,
2346 field
= old
->field
->next_subobject
;
2352 *v
= new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2355 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2359 new->expr_ident
= field
->ident
;
2360 new->ident_expression
= copy
;
2367 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2368 int class, struct symbol
*ctype
);
2371 * deal with traversing subobjects [6.7.8(17,18,20)]
2373 static void handle_list_initializer(struct expression
*expr
,
2374 int class, struct symbol
*ctype
)
2376 struct expression
*e
, *last
= NULL
, *top
= NULL
, *next
;
2379 FOR_EACH_PTR(expr
->expr_list
, e
) {
2380 struct expression
**v
;
2381 struct symbol
*type
;
2384 if (e
->type
!= EXPR_INDEX
&& e
->type
!= EXPR_IDENTIFIER
) {
2385 struct symbol
*struct_sym
;
2388 last
= first_subobject(ctype
, class, &top
);
2390 last
= next_designators(last
, ctype
, e
, &top
);
2393 excess(e
, class & TYPE_PTR
? "array" :
2395 DELETE_CURRENT_PTR(e
);
2398 struct_sym
= ctype
->type
== SYM_NODE
? ctype
->ctype
.base_type
: ctype
;
2399 if (Wdesignated_init
&& struct_sym
->designated_init
)
2400 warning(e
->pos
, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2401 ctype
->ident
? "in initializer for " : "",
2402 ctype
->ident
? ctype
->ident
->len
: 0,
2403 ctype
->ident
? ctype
->ident
->name
: "",
2404 ctype
->ident
? ": " : "",
2405 get_type_name(struct_sym
->type
),
2406 show_ident(struct_sym
->ident
));
2408 warning(e
->pos
, "advancing past deep designator");
2411 REPLACE_CURRENT_PTR(e
, last
);
2413 next
= check_designators(e
, ctype
);
2415 DELETE_CURRENT_PTR(e
);
2419 /* deeper than one designator? */
2421 convert_designators(last
);
2426 lclass
= classify_type(top
->ctype
, &type
);
2427 if (top
->type
== EXPR_INDEX
)
2428 v
= &top
->idx_expression
;
2430 v
= &top
->ident_expression
;
2432 if (handle_simple_initializer(v
, 1, lclass
, top
->ctype
))
2435 if (!(lclass
& TYPE_COMPOUND
)) {
2436 warning(e
->pos
, "bogus scalar initializer");
2437 DELETE_CURRENT_PTR(e
);
2441 next
= first_subobject(type
, lclass
, v
);
2443 warning(e
->pos
, "missing braces around initializer");
2448 DELETE_CURRENT_PTR(e
);
2449 excess(e
, lclass
& TYPE_PTR
? "array" : "struct or union");
2451 } END_FOR_EACH_PTR(e
);
2453 convert_designators(last
);
2454 expr
->ctype
= ctype
;
2457 static int is_string_literal(struct expression
**v
)
2459 struct expression
*e
= *v
;
2460 while (e
&& e
->type
== EXPR_PREOP
&& e
->op
== '(')
2462 if (!e
|| e
->type
!= EXPR_STRING
)
2464 if (e
!= *v
&& Wparen_string
)
2466 "array initialized from parenthesized string constant");
2472 * We want a normal expression, possibly in one layer of braces. Warn
2473 * if the latter happens inside a list (it's legal, but likely to be
2474 * an effect of screwup). In case of anything not legal, we are definitely
2475 * having an effect of screwup, so just fail and let the caller warn.
2477 static struct expression
*handle_scalar(struct expression
*e
, int nested
)
2479 struct expression
*v
= NULL
, *p
;
2483 if (e
->type
!= EXPR_INITIALIZER
)
2486 FOR_EACH_PTR(e
->expr_list
, p
) {
2490 } END_FOR_EACH_PTR(p
);
2494 case EXPR_INITIALIZER
:
2496 case EXPR_IDENTIFIER
:
2502 warning(e
->pos
, "braces around scalar initializer");
2507 * deal with the cases that don't care about subobjects:
2508 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2509 * character array <- string literal, possibly in braces [6.7.8(14)]
2510 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2511 * compound type <- initializer list in braces [6.7.8(16)]
2512 * The last one punts to handle_list_initializer() which, in turn will call
2513 * us for individual elements of the list.
2515 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2516 * the lack of support of wide char stuff in general.
2518 * One note: we need to take care not to evaluate a string literal until
2519 * we know that we *will* handle it right here. Otherwise we would screw
2520 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2521 * { "string", ...} - we need to preserve that string literal recognizable
2522 * until we dig into the inner struct.
2524 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2525 int class, struct symbol
*ctype
)
2527 int is_string
= is_string_type(ctype
);
2528 struct expression
*e
= *ep
, *p
;
2529 struct symbol
*type
;
2535 if (!(class & TYPE_COMPOUND
)) {
2536 e
= handle_scalar(e
, nested
);
2540 if (!evaluate_expression(e
))
2542 compatible_assignment_types(e
, ctype
, ep
, "initializer");
2547 * sublist; either a string, or we dig in; the latter will deal with
2548 * pathologies, so we don't need anything fancy here.
2550 if (e
->type
== EXPR_INITIALIZER
) {
2552 struct expression
*v
= NULL
;
2555 FOR_EACH_PTR(e
->expr_list
, p
) {
2559 } END_FOR_EACH_PTR(p
);
2560 if (count
== 1 && is_string_literal(&v
)) {
2565 handle_list_initializer(e
, class, ctype
);
2570 if (is_string_literal(&e
)) {
2571 /* either we are doing array of char, or we'll have to dig in */
2578 /* struct or union can be initialized by compatible */
2579 if (class != TYPE_COMPOUND
)
2581 type
= evaluate_expression(e
);
2584 if (ctype
->type
== SYM_NODE
)
2585 ctype
= ctype
->ctype
.base_type
;
2586 if (type
->type
== SYM_NODE
)
2587 type
= type
->ctype
.base_type
;
2593 p
= alloc_expression(e
->pos
, EXPR_STRING
);
2595 type
= evaluate_expression(p
);
2596 if (ctype
->bit_size
!= -1 &&
2597 ctype
->bit_size
+ bits_in_char
< type
->bit_size
) {
2599 "too long initializer-string for array of char");
2605 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2607 struct symbol
*type
;
2608 int class = classify_type(ctype
, &type
);
2609 if (!handle_simple_initializer(ep
, 0, class, ctype
))
2610 expression_error(*ep
, "invalid initializer");
2613 static struct symbol
*evaluate_cast(struct expression
*expr
)
2615 struct expression
*target
= expr
->cast_expression
;
2616 struct symbol
*ctype
;
2617 struct symbol
*t1
, *t2
;
2619 int as1
= 0, as2
= 0;
2625 * Special case: a cast can be followed by an
2626 * initializer, in which case we need to pass
2627 * the type value down to that initializer rather
2628 * than trying to evaluate it as an expression
2630 * A more complex case is when the initializer is
2631 * dereferenced as part of a post-fix expression.
2632 * We need to produce an expression that can be dereferenced.
2634 if (target
->type
== EXPR_INITIALIZER
) {
2635 struct symbol
*sym
= expr
->cast_type
;
2636 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2638 sym
->initializer
= target
;
2639 evaluate_symbol(sym
);
2641 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2644 expr
->type
= EXPR_PREOP
;
2652 ctype
= examine_symbol_type(expr
->cast_type
);
2653 expr
->ctype
= ctype
;
2654 expr
->cast_type
= ctype
;
2656 evaluate_expression(target
);
2659 class1
= classify_type(ctype
, &t1
);
2661 /* cast to non-integer type -> not an integer constant expression */
2662 if (!is_int(class1
))
2664 /* if argument turns out to be not an integer constant expression *and*
2665 it was not a floating literal to start with -> too bad */
2666 else if (expr
->flags
== Int_const_expr
&&
2667 !(target
->flags
& Int_const_expr
))
2670 * You can always throw a value away by casting to
2671 * "void" - that's an implicit "force". Note that
2672 * the same is _not_ true of "void *".
2674 if (t1
== &void_ctype
)
2677 if (class1
& (TYPE_COMPOUND
| TYPE_FN
))
2678 warning(expr
->pos
, "cast to non-scalar");
2682 expression_error(expr
, "cast from unknown type");
2685 class2
= classify_type(t2
, &t2
);
2687 if (class2
& TYPE_COMPOUND
)
2688 warning(expr
->pos
, "cast from non-scalar");
2690 if (expr
->type
== EXPR_FORCE_CAST
)
2693 /* allowed cast unfouls */
2694 if (class2
& TYPE_FOULED
)
2698 if (class1
& TYPE_RESTRICT
)
2699 warning(expr
->pos
, "cast to %s",
2701 if (class2
& TYPE_RESTRICT
)
2702 warning(expr
->pos
, "cast from %s",
2706 if (t1
== &ulong_ctype
)
2708 else if (class1
== TYPE_PTR
) {
2709 examine_pointer_target(t1
);
2710 as1
= t1
->ctype
.attribute
->as
;
2713 if (t2
== &ulong_ctype
)
2715 else if (class2
== TYPE_PTR
) {
2716 examine_pointer_target(t2
);
2717 as2
= t2
->ctype
.attribute
->as
;
2720 if (!as1
&& as2
> 0)
2721 warning(expr
->pos
, "cast removes address space of expression");
2722 if (as1
> 0 && as2
> 0 && as1
!= as2
)
2723 warning(expr
->pos
, "cast between address spaces (<asn:%d>-><asn:%d>)", as2
, as1
);
2724 if (as1
> 0 && !as2
&&
2725 !is_null_pointer_constant(target
) && Wcast_to_as
)
2727 "cast adds address space to expression (<asn:%d>)", as1
);
2729 if (!(t1
->ctype
.modifiers
& MOD_PTRINHERIT
) && class1
== TYPE_PTR
&&
2730 !as1
&& (target
->flags
& Int_const_expr
)) {
2731 if (t1
->ctype
.base_type
== &void_ctype
) {
2732 if (is_zero_constant(target
)) {
2734 expr
->type
= EXPR_VALUE
;
2735 expr
->ctype
= &null_ctype
;
2746 * Evaluate a call expression with a symbol. This
2747 * should expand inline functions, and evaluate
2750 static int evaluate_symbol_call(struct expression
*expr
)
2752 struct expression
*fn
= expr
->fn
;
2753 struct symbol
*ctype
= fn
->ctype
;
2755 if (fn
->type
!= EXPR_PREOP
)
2758 if (ctype
->op
&& ctype
->op
->evaluate
)
2759 return ctype
->op
->evaluate(expr
);
2761 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2763 struct symbol
*curr
= current_fn
;
2765 if (ctype
->definition
)
2766 ctype
= ctype
->definition
;
2768 current_fn
= ctype
->ctype
.base_type
;
2770 ret
= inline_function(expr
, ctype
);
2772 /* restore the old function */
2780 static struct symbol
*evaluate_call(struct expression
*expr
)
2783 struct symbol
*ctype
, *sym
;
2784 struct expression
*fn
= expr
->fn
;
2785 struct expression_list
*arglist
= expr
->args
;
2787 if (!evaluate_expression(fn
))
2789 sym
= ctype
= fn
->ctype
;
2790 if (ctype
->type
== SYM_NODE
)
2791 ctype
= ctype
->ctype
.base_type
;
2792 if (ctype
->type
== SYM_PTR
)
2793 ctype
= get_base_type(ctype
);
2795 if (ctype
->type
!= SYM_FN
) {
2796 struct expression
*arg
;
2797 expression_error(expr
, "not a function %s",
2798 show_ident(sym
->ident
));
2799 /* do typechecking in arguments */
2800 FOR_EACH_PTR (arglist
, arg
) {
2801 evaluate_expression(arg
);
2802 } END_FOR_EACH_PTR(arg
);
2806 examine_fn_arguments(ctype
);
2807 if (sym
->type
== SYM_NODE
&& fn
->type
== EXPR_PREOP
&&
2808 sym
->op
&& sym
->op
->args
) {
2809 if (!sym
->op
->args(expr
))
2812 if (!evaluate_arguments(sym
, ctype
, arglist
))
2814 args
= expression_list_size(expr
->args
);
2815 fnargs
= symbol_list_size(ctype
->arguments
);
2817 expression_error(expr
,
2818 "not enough arguments for function %s",
2819 show_ident(sym
->ident
));
2820 if (args
> fnargs
&& !ctype
->variadic
)
2821 expression_error(expr
,
2822 "too many arguments for function %s",
2823 show_ident(sym
->ident
));
2825 if (sym
->type
== SYM_NODE
) {
2826 if (evaluate_symbol_call(expr
))
2829 expr
->ctype
= ctype
->ctype
.base_type
;
2833 static struct symbol
*evaluate_offsetof(struct expression
*expr
)
2835 struct expression
*e
= expr
->down
;
2836 struct symbol
*ctype
= expr
->in
;
2839 if (expr
->op
== '.') {
2840 struct symbol
*field
;
2843 expression_error(expr
, "expected structure or union");
2846 examine_symbol_type(ctype
);
2847 class = classify_type(ctype
, &ctype
);
2848 if (class != TYPE_COMPOUND
) {
2849 expression_error(expr
, "expected structure or union");
2853 field
= find_identifier(expr
->ident
, ctype
->symbol_list
, &offset
);
2855 expression_error(expr
, "unknown member");
2859 expr
->type
= EXPR_VALUE
;
2860 expr
->flags
= Int_const_expr
;
2861 expr
->value
= offset
;
2863 expr
->ctype
= size_t_ctype
;
2866 expression_error(expr
, "expected structure or union");
2869 examine_symbol_type(ctype
);
2870 class = classify_type(ctype
, &ctype
);
2871 if (class != (TYPE_COMPOUND
| TYPE_PTR
)) {
2872 expression_error(expr
, "expected array");
2875 ctype
= ctype
->ctype
.base_type
;
2877 expr
->type
= EXPR_VALUE
;
2878 expr
->flags
= Int_const_expr
;
2881 expr
->ctype
= size_t_ctype
;
2883 struct expression
*idx
= expr
->index
, *m
;
2884 struct symbol
*i_type
= evaluate_expression(idx
);
2885 int i_class
= classify_type(i_type
, &i_type
);
2886 if (!is_int(i_class
)) {
2887 expression_error(expr
, "non-integer index");
2890 unrestrict(idx
, i_class
, &i_type
);
2891 idx
= cast_to(idx
, size_t_ctype
);
2892 m
= alloc_const_expression(expr
->pos
,
2893 bits_to_bytes(ctype
->bit_size
));
2894 m
->ctype
= size_t_ctype
;
2895 m
->flags
= Int_const_expr
;
2896 expr
->type
= EXPR_BINOP
;
2900 expr
->ctype
= size_t_ctype
;
2901 expr
->flags
= m
->flags
& idx
->flags
& Int_const_expr
;
2905 struct expression
*copy
= __alloc_expression(0);
2907 if (e
->type
== EXPR_OFFSETOF
)
2909 if (!evaluate_expression(e
))
2911 expr
->type
= EXPR_BINOP
;
2912 expr
->flags
= e
->flags
& copy
->flags
& Int_const_expr
;
2914 expr
->ctype
= size_t_ctype
;
2918 return size_t_ctype
;
2921 struct symbol
*evaluate_expression(struct expression
*expr
)
2928 switch (expr
->type
) {
2931 expression_error(expr
, "value expression without a type");
2934 return evaluate_string(expr
);
2936 return evaluate_symbol_expression(expr
);
2938 if (!evaluate_expression(expr
->left
))
2940 if (!evaluate_expression(expr
->right
))
2942 return evaluate_binop(expr
);
2944 return evaluate_logical(expr
);
2946 evaluate_expression(expr
->left
);
2947 if (!evaluate_expression(expr
->right
))
2949 return evaluate_comma(expr
);
2951 if (!evaluate_expression(expr
->left
))
2953 if (!evaluate_expression(expr
->right
))
2955 return evaluate_compare(expr
);
2956 case EXPR_ASSIGNMENT
:
2957 if (!evaluate_expression(expr
->left
))
2959 if (!evaluate_expression(expr
->right
))
2961 return evaluate_assignment(expr
);
2963 if (!evaluate_expression(expr
->unop
))
2965 return evaluate_preop(expr
);
2967 if (!evaluate_expression(expr
->unop
))
2969 return evaluate_postop(expr
);
2971 case EXPR_FORCE_CAST
:
2972 case EXPR_IMPLIED_CAST
:
2973 return evaluate_cast(expr
);
2975 return evaluate_sizeof(expr
);
2976 case EXPR_PTRSIZEOF
:
2977 return evaluate_ptrsizeof(expr
);
2979 return evaluate_alignof(expr
);
2981 return evaluate_member_dereference(expr
);
2983 return evaluate_call(expr
);
2985 case EXPR_CONDITIONAL
:
2986 return evaluate_conditional_expression(expr
);
2987 case EXPR_STATEMENT
:
2988 expr
->ctype
= evaluate_statement(expr
->statement
);
2992 expr
->ctype
= &ptr_ctype
;
2996 /* Evaluate the type of the symbol .. */
2997 evaluate_symbol(expr
->symbol
);
2998 /* .. but the type of the _expression_ is a "type" */
2999 expr
->ctype
= &type_ctype
;
3003 return evaluate_offsetof(expr
);
3005 /* These can not exist as stand-alone expressions */
3006 case EXPR_INITIALIZER
:
3007 case EXPR_IDENTIFIER
:
3010 expression_error(expr
, "internal front-end error: initializer in expression");
3013 expression_error(expr
, "internal front-end error: SLICE re-evaluated");
3019 static void check_duplicates(struct symbol
*sym
)
3022 struct symbol
*next
= sym
;
3024 while ((next
= next
->same_symbol
) != NULL
) {
3025 const char *typediff
;
3026 evaluate_symbol(next
);
3028 typediff
= type_difference(&sym
->ctype
, &next
->ctype
, 0, 0);
3030 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
3031 show_ident(sym
->ident
),
3032 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
3037 unsigned long mod
= sym
->ctype
.modifiers
;
3038 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
3040 if (!(mod
& MOD_TOPLEVEL
))
3044 if (sym
->ident
== &main_ident
)
3046 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
3050 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
3052 struct symbol
*base_type
;
3060 sym
= examine_symbol_type(sym
);
3061 base_type
= get_base_type(sym
);
3065 /* Evaluate the initializers */
3066 if (sym
->initializer
)
3067 evaluate_initializer(sym
, &sym
->initializer
);
3069 /* And finally, evaluate the body of the symbol too */
3070 if (base_type
->type
== SYM_FN
) {
3071 struct symbol
*curr
= current_fn
;
3073 if (sym
->definition
&& sym
->definition
!= sym
)
3074 return evaluate_symbol(sym
->definition
);
3076 current_fn
= base_type
;
3078 examine_fn_arguments(base_type
);
3079 if (!base_type
->stmt
&& base_type
->inline_stmt
)
3081 if (base_type
->stmt
)
3082 evaluate_statement(base_type
->stmt
);
3090 void evaluate_symbol_list(struct symbol_list
*list
)
3094 FOR_EACH_PTR(list
, sym
) {
3095 evaluate_symbol(sym
);
3096 check_duplicates(sym
);
3097 } END_FOR_EACH_PTR(sym
);
3100 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
3102 struct expression
*expr
= stmt
->expression
;
3103 struct symbol
*fntype
;
3105 evaluate_expression(expr
);
3106 fntype
= current_fn
->ctype
.base_type
;
3107 if (!fntype
|| fntype
== &void_ctype
) {
3108 if (expr
&& expr
->ctype
!= &void_ctype
)
3109 expression_error(expr
, "return expression in %s function", fntype
?"void":"typeless");
3110 if (expr
&& Wreturn_void
)
3111 warning(stmt
->pos
, "returning void-valued expression");
3116 sparse_error(stmt
->pos
, "return with no return value");
3121 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, "return expression");
3125 static void evaluate_if_statement(struct statement
*stmt
)
3127 if (!stmt
->if_conditional
)
3130 evaluate_conditional(stmt
->if_conditional
, 0);
3131 evaluate_statement(stmt
->if_true
);
3132 evaluate_statement(stmt
->if_false
);
3135 static void evaluate_iterator(struct statement
*stmt
)
3137 evaluate_symbol_list(stmt
->iterator_syms
);
3138 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
3139 evaluate_conditional(stmt
->iterator_post_condition
,1);
3140 evaluate_statement(stmt
->iterator_pre_statement
);
3141 evaluate_statement(stmt
->iterator_statement
);
3142 evaluate_statement(stmt
->iterator_post_statement
);
3145 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
3147 switch (*constraint
) {
3148 case '=': /* Assignment */
3149 case '+': /* Update */
3152 expression_error(expr
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
3156 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
3158 switch (*constraint
) {
3159 case '=': /* Assignment */
3160 case '+': /* Update */
3161 expression_error(expr
, "input constraint with assignment (\"%s\")", constraint
);
3165 static void evaluate_asm_statement(struct statement
*stmt
)
3167 struct expression
*expr
;
3171 expr
= stmt
->asm_string
;
3172 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3173 sparse_error(stmt
->pos
, "need constant string for inline asm");
3178 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
3180 case 0: /* Identifier */
3184 case 1: /* Constraint */
3186 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3187 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
3188 *THIS_ADDRESS(expr
) = NULL
;
3191 verify_output_constraint(expr
, expr
->string
->data
);
3194 case 2: /* Expression */
3196 if (!evaluate_expression(expr
))
3198 if (!lvalue_expression(expr
))
3199 warning(expr
->pos
, "asm output is not an lvalue");
3200 evaluate_assign_to(expr
, expr
->ctype
);
3203 } END_FOR_EACH_PTR(expr
);
3206 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
3208 case 0: /* Identifier */
3212 case 1: /* Constraint */
3214 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3215 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
3216 *THIS_ADDRESS(expr
) = NULL
;
3219 verify_input_constraint(expr
, expr
->string
->data
);
3222 case 2: /* Expression */
3224 if (!evaluate_expression(expr
))
3228 } END_FOR_EACH_PTR(expr
);
3230 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
3232 sparse_error(stmt
->pos
, "bad asm clobbers");
3235 if (expr
->type
== EXPR_STRING
)
3237 expression_error(expr
, "asm clobber is not a string");
3238 } END_FOR_EACH_PTR(expr
);
3240 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
3241 if (!sym
|| sym
->type
!= SYM_LABEL
) {
3242 sparse_error(stmt
->pos
, "bad asm label");
3245 } END_FOR_EACH_PTR(sym
);
3248 static void evaluate_case_statement(struct statement
*stmt
)
3250 evaluate_expression(stmt
->case_expression
);
3251 evaluate_expression(stmt
->case_to
);
3252 evaluate_statement(stmt
->case_statement
);
3255 static void check_case_type(struct expression
*switch_expr
,
3256 struct expression
*case_expr
,
3257 struct expression
**enumcase
)
3259 struct symbol
*switch_type
, *case_type
;
3265 switch_type
= switch_expr
->ctype
;
3266 case_type
= evaluate_expression(case_expr
);
3268 if (!switch_type
|| !case_type
)
3272 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
3273 else if (is_enum_type(case_type
))
3274 *enumcase
= case_expr
;
3277 sclass
= classify_type(switch_type
, &switch_type
);
3278 cclass
= classify_type(case_type
, &case_type
);
3280 /* both should be arithmetic */
3281 if (!(sclass
& cclass
& TYPE_NUM
))
3284 /* neither should be floating */
3285 if ((sclass
| cclass
) & TYPE_FLOAT
)
3288 /* if neither is restricted, we are OK */
3289 if (!((sclass
| cclass
) & TYPE_RESTRICT
))
3292 if (!restricted_binop_type(SPECIAL_EQUAL
, case_expr
, switch_expr
,
3293 cclass
, sclass
, case_type
, switch_type
)) {
3294 unrestrict(case_expr
, cclass
, &case_type
);
3295 unrestrict(switch_expr
, sclass
, &switch_type
);
3300 expression_error(case_expr
, "incompatible types for 'case' statement");
3303 static void evaluate_switch_statement(struct statement
*stmt
)
3306 struct expression
*enumcase
= NULL
;
3307 struct expression
**enumcase_holder
= &enumcase
;
3308 struct expression
*sel
= stmt
->switch_expression
;
3310 evaluate_expression(sel
);
3311 evaluate_statement(stmt
->switch_statement
);
3314 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
3315 enumcase_holder
= NULL
; /* Only check cases against switch */
3317 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
3318 struct statement
*case_stmt
= sym
->stmt
;
3319 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
3320 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
3321 } END_FOR_EACH_PTR(sym
);
3324 static void evaluate_goto_statement(struct statement
*stmt
)
3326 struct symbol
*label
= stmt
->goto_label
;
3328 if (label
&& !label
->stmt
)
3329 sparse_error(stmt
->pos
, "label '%s' was not declared", show_ident(label
->ident
));
3331 evaluate_expression(stmt
->goto_expression
);
3334 struct symbol
*evaluate_statement(struct statement
*stmt
)
3339 switch (stmt
->type
) {
3340 case STMT_DECLARATION
: {
3342 FOR_EACH_PTR(stmt
->declaration
, s
) {
3344 } END_FOR_EACH_PTR(s
);
3349 return evaluate_return_expression(stmt
);
3351 case STMT_EXPRESSION
:
3352 if (!evaluate_expression(stmt
->expression
))
3354 if (stmt
->expression
->ctype
== &null_ctype
)
3355 stmt
->expression
= cast_to(stmt
->expression
, &ptr_ctype
);
3356 return degenerate(stmt
->expression
);
3358 case STMT_COMPOUND
: {
3359 struct statement
*s
;
3360 struct symbol
*type
= NULL
;
3362 /* Evaluate the return symbol in the compound statement */
3363 evaluate_symbol(stmt
->ret
);
3366 * Then, evaluate each statement, making the type of the
3367 * compound statement be the type of the last statement
3369 type
= evaluate_statement(stmt
->args
);
3370 FOR_EACH_PTR(stmt
->stmts
, s
) {
3371 type
= evaluate_statement(s
);
3372 } END_FOR_EACH_PTR(s
);
3378 evaluate_if_statement(stmt
);
3381 evaluate_iterator(stmt
);
3384 evaluate_switch_statement(stmt
);
3387 evaluate_case_statement(stmt
);
3390 return evaluate_statement(stmt
->label_statement
);
3392 evaluate_goto_statement(stmt
);
3397 evaluate_asm_statement(stmt
);
3400 evaluate_expression(stmt
->expression
);
3403 evaluate_expression(stmt
->range_expression
);
3404 evaluate_expression(stmt
->range_low
);
3405 evaluate_expression(stmt
->range_high
);