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
| MOD_LONGLONG
))
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
.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
;
316 static int is_type_type(struct symbol
*type
)
318 return (type
->ctype
.modifiers
& MOD_TYPE
) != 0;
321 int is_ptr_type(struct symbol
*type
)
323 if (type
->type
== SYM_NODE
)
324 type
= type
->ctype
.base_type
;
325 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
328 static inline int is_float_type(struct symbol
*type
)
330 if (type
->type
== SYM_NODE
)
331 type
= type
->ctype
.base_type
;
332 return type
->ctype
.base_type
== &fp_type
;
335 static inline int is_byte_type(struct symbol
*type
)
337 return type
->bit_size
== bits_in_char
&& type
->type
!= SYM_BITFIELD
;
340 static inline int is_void_type(struct symbol
*type
)
342 if (type
->type
== SYM_NODE
)
343 type
= type
->ctype
.base_type
;
344 return type
== &void_ctype
;
358 static inline int classify_type(struct symbol
*type
, struct symbol
**base
)
360 static int type_class
[SYM_BAD
+ 1] = {
361 [SYM_PTR
] = TYPE_PTR
,
362 [SYM_FN
] = TYPE_PTR
| TYPE_FN
,
363 [SYM_ARRAY
] = TYPE_PTR
| TYPE_COMPOUND
,
364 [SYM_STRUCT
] = TYPE_COMPOUND
,
365 [SYM_UNION
] = TYPE_COMPOUND
,
366 [SYM_BITFIELD
] = TYPE_NUM
| TYPE_BITFIELD
,
367 [SYM_RESTRICT
] = TYPE_NUM
| TYPE_RESTRICT
,
368 [SYM_FOULED
] = TYPE_NUM
| TYPE_RESTRICT
| TYPE_FOULED
,
370 if (type
->type
== SYM_NODE
)
371 type
= type
->ctype
.base_type
;
372 if (type
->type
== SYM_TYPEOF
) {
373 type
= evaluate_expression(type
->initializer
);
374 if (type
->type
== SYM_NODE
)
375 type
= type
->ctype
.base_type
;
377 if (type
->type
== SYM_ENUM
)
378 type
= type
->ctype
.base_type
;
380 if (type
->type
== SYM_BASETYPE
) {
381 if (type
->ctype
.base_type
== &int_type
)
383 if (type
->ctype
.base_type
== &fp_type
)
384 return TYPE_NUM
| TYPE_FLOAT
;
386 return type_class
[type
->type
];
389 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
391 static inline int is_string_type(struct symbol
*type
)
393 if (type
->type
== SYM_NODE
)
394 type
= type
->ctype
.base_type
;
395 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
398 static struct symbol
*bad_expr_type(struct expression
*expr
)
400 sparse_error(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
401 switch (expr
->type
) {
404 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
405 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
409 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
416 return expr
->ctype
= &bad_ctype
;
419 static int restricted_value(struct expression
*v
, struct symbol
*type
)
421 if (v
->type
!= EXPR_VALUE
)
428 static int restricted_binop(int op
, struct symbol
*type
)
433 case SPECIAL_AND_ASSIGN
:
434 case SPECIAL_OR_ASSIGN
:
435 case SPECIAL_XOR_ASSIGN
:
436 return 1; /* unfoul */
440 return 2; /* keep fouled */
442 case SPECIAL_NOTEQUAL
:
443 return 3; /* warn if fouled */
449 static int restricted_unop(int op
, struct symbol
**type
)
452 if ((*type
)->bit_size
< bits_in_int
)
453 *type
= befoul(*type
);
460 /* type should be SYM_FOULED */
461 static inline struct symbol
*unfoul(struct symbol
*type
)
463 return type
->ctype
.base_type
;
466 static struct symbol
*restricted_binop_type(int op
,
467 struct expression
*left
,
468 struct expression
*right
,
469 int lclass
, int rclass
,
470 struct symbol
*ltype
,
471 struct symbol
*rtype
)
473 struct symbol
*ctype
= NULL
;
474 if (lclass
& TYPE_RESTRICT
) {
475 if (rclass
& TYPE_RESTRICT
) {
476 if (ltype
== rtype
) {
478 } else if (lclass
& TYPE_FOULED
) {
479 if (unfoul(ltype
) == rtype
)
481 } else if (rclass
& TYPE_FOULED
) {
482 if (unfoul(rtype
) == ltype
)
486 if (!restricted_value(right
, ltype
))
489 } else if (!restricted_value(left
, rtype
))
493 switch (restricted_binop(op
, ctype
)) {
495 if ((lclass
^ rclass
) & TYPE_FOULED
)
496 ctype
= unfoul(ctype
);
499 if (!(lclass
& rclass
& TYPE_FOULED
))
511 static inline void unrestrict(struct expression
*expr
,
512 int class, struct symbol
**ctype
)
514 if (class & TYPE_RESTRICT
) {
515 if (class & TYPE_FOULED
)
516 *ctype
= unfoul(*ctype
);
517 warning(expr
->pos
, "%s degrades to integer",
518 show_typename(*ctype
));
519 *ctype
= (*ctype
)->ctype
.base_type
; /* get to arithmetic type */
523 static struct symbol
*usual_conversions(int op
,
524 struct expression
*left
,
525 struct expression
*right
,
526 int lclass
, int rclass
,
527 struct symbol
*ltype
,
528 struct symbol
*rtype
)
530 struct symbol
*ctype
;
532 warn_for_different_enum_types(right
->pos
, left
->ctype
, right
->ctype
);
534 if ((lclass
| rclass
) & TYPE_RESTRICT
)
538 if (!(lclass
& TYPE_FLOAT
)) {
539 if (!(rclass
& TYPE_FLOAT
))
540 return bigger_int_type(ltype
, rtype
);
543 } else if (rclass
& TYPE_FLOAT
) {
544 unsigned long lmod
= ltype
->ctype
.modifiers
;
545 unsigned long rmod
= rtype
->ctype
.modifiers
;
546 if (rmod
& ~lmod
& (MOD_LONG
| MOD_LONGLONG
))
554 ctype
= restricted_binop_type(op
, left
, right
,
555 lclass
, rclass
, ltype
, rtype
);
559 unrestrict(left
, lclass
, <ype
);
560 unrestrict(right
, rclass
, &rtype
);
565 static inline int lvalue_expression(struct expression
*expr
)
567 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
570 static inline int is_function(struct symbol
*type
)
572 return type
&& type
->type
== SYM_FN
;
575 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*itype
)
577 struct expression
*index
= expr
->right
;
578 struct symbol
*ctype
, *base
;
581 classify_type(degenerate(expr
->left
), &ctype
);
582 base
= examine_pointer_target(ctype
);
585 expression_error(expr
, "missing type information");
588 if (is_function(base
)) {
589 expression_error(expr
, "arithmetics on pointers to functions");
593 /* Get the size of whatever the pointer points to */
594 multiply
= is_void_type(base
) ? 1 : bits_to_bytes(base
->bit_size
);
596 if (ctype
== &null_ctype
)
600 if (multiply
== 1 && itype
->bit_size
>= bits_in_pointer
)
603 if (index
->type
== EXPR_VALUE
) {
604 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
605 unsigned long long v
= index
->value
, mask
;
606 mask
= 1ULL << (itype
->bit_size
- 1);
612 mask
= 1ULL << (bits_in_pointer
- 1);
613 v
&= mask
| (mask
- 1);
615 val
->ctype
= ssize_t_ctype
;
620 if (itype
->bit_size
< bits_in_pointer
)
621 index
= cast_to(index
, ssize_t_ctype
);
624 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
625 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
627 val
->ctype
= ssize_t_ctype
;
628 val
->value
= multiply
;
631 mul
->ctype
= ssize_t_ctype
;
641 static void examine_fn_arguments(struct symbol
*fn
);
643 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
645 const char *type_difference(struct ctype
*c1
, struct ctype
*c2
,
646 unsigned long mod1
, unsigned long mod2
)
648 unsigned long as1
= c1
->as
, as2
= c2
->as
;
649 struct symbol
*t1
= c1
->base_type
;
650 struct symbol
*t2
= c2
->base_type
;
651 int move1
= 1, move2
= 1;
652 mod1
|= c1
->modifiers
;
653 mod2
|= c2
->modifiers
;
657 struct symbol
*base1
= t1
->ctype
.base_type
;
658 struct symbol
*base2
= t2
->ctype
.base_type
;
661 * FIXME! Collect alignment and context too here!
664 if (t1
&& t1
->type
!= SYM_PTR
) {
665 mod1
|= t1
->ctype
.modifiers
;
672 if (t2
&& t2
->type
!= SYM_PTR
) {
673 mod2
|= t2
->ctype
.modifiers
;
682 return "different types";
684 if (t1
->type
== SYM_NODE
|| t1
->type
== SYM_ENUM
) {
692 if (t2
->type
== SYM_NODE
|| t2
->type
== SYM_ENUM
) {
702 if (type
!= t2
->type
)
703 return "different base types";
707 sparse_error(t1
->pos
,
708 "internal error: bad type in derived(%d)",
712 return "different base types";
715 /* allow definition of incomplete structs and unions */
716 if (t1
->ident
== t2
->ident
)
718 return "different base types";
720 /* XXX: we ought to compare sizes */
723 if (Waddress_space
&& as1
!= as2
)
724 return "different address spaces";
725 /* MOD_SPECIFIER is due to idiocy in parse.c */
726 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SPECIFIER
)
727 return "different modifiers";
728 /* we could be lazier here */
729 base1
= examine_pointer_target(t1
);
730 base2
= examine_pointer_target(t2
);
731 mod1
= t1
->ctype
.modifiers
;
733 mod2
= t2
->ctype
.modifiers
;
737 struct symbol
*arg1
, *arg2
;
740 if (Waddress_space
&& as1
!= as2
)
741 return "different address spaces";
742 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
743 return "different modifiers";
744 mod1
= t1
->ctype
.modifiers
;
746 mod2
= t2
->ctype
.modifiers
;
749 if (base1
->variadic
!= base2
->variadic
)
750 return "incompatible variadic arguments";
751 examine_fn_arguments(t1
);
752 examine_fn_arguments(t2
);
753 PREPARE_PTR_LIST(t1
->arguments
, arg1
);
754 PREPARE_PTR_LIST(t2
->arguments
, arg2
);
761 return "different argument counts";
762 diffstr
= type_difference(&arg1
->ctype
,
766 static char argdiff
[80];
767 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diffstr
);
774 FINISH_PTR_LIST(arg2
);
775 FINISH_PTR_LIST(arg1
);
779 if (Waddress_space
&& as1
!= as2
)
780 return "different address spaces";
782 return "different base types";
783 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
787 return "different type sizes";
788 else if (diff
& ~MOD_SIGNEDNESS
)
789 return "different modifiers";
791 return "different signedness";
796 if (Waddress_space
&& as1
!= as2
)
797 return "different address spaces";
798 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
799 return "different modifiers";
803 static void bad_null(struct expression
*expr
)
805 if (Wnon_pointer_null
)
806 warning(expr
->pos
, "Using plain integer as NULL pointer");
809 static unsigned long target_qualifiers(struct symbol
*type
)
811 unsigned long mod
= type
->ctype
.modifiers
& MOD_IGN
;
812 if (type
->ctype
.base_type
&& type
->ctype
.base_type
->type
== SYM_ARRAY
)
817 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
)
819 const char *typediff
;
820 struct symbol
*ltype
, *rtype
;
821 struct expression
*l
= expr
->left
;
822 struct expression
*r
= expr
->right
;
823 struct symbol
*lbase
, *rbase
;
825 classify_type(degenerate(l
), <ype
);
826 classify_type(degenerate(r
), &rtype
);
828 lbase
= examine_pointer_target(ltype
);
829 rbase
= examine_pointer_target(rtype
);
830 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
831 target_qualifiers(rtype
),
832 target_qualifiers(ltype
));
834 expression_error(expr
, "subtraction of different types can't work (%s)", typediff
);
836 if (is_function(lbase
)) {
837 expression_error(expr
, "subtraction of functions? Share your drugs");
841 expr
->ctype
= ssize_t_ctype
;
842 if (lbase
->bit_size
> bits_in_char
) {
843 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
844 struct expression
*div
= expr
;
845 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
846 unsigned long value
= bits_to_bytes(lbase
->bit_size
);
848 val
->ctype
= size_t_ctype
;
851 if (value
& (value
-1)) {
852 if (Wptr_subtraction_blows
)
853 warning(expr
->pos
, "potentially expensive pointer subtraction");
857 sub
->ctype
= ssize_t_ctype
;
866 return ssize_t_ctype
;
869 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
871 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
873 struct symbol
*ctype
;
878 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
879 warning(expr
->pos
, "assignment expression in conditional");
881 ctype
= evaluate_expression(expr
);
883 if (is_safe_type(ctype
))
884 warning(expr
->pos
, "testing a 'safe expression'");
890 static struct symbol
*evaluate_logical(struct expression
*expr
)
892 if (!evaluate_conditional(expr
->left
, 0))
894 if (!evaluate_conditional(expr
->right
, 0))
897 expr
->ctype
= &bool_ctype
;
899 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
905 static struct symbol
*evaluate_binop(struct expression
*expr
)
907 struct symbol
*ltype
, *rtype
, *ctype
;
908 int lclass
= classify_type(expr
->left
->ctype
, <ype
);
909 int rclass
= classify_type(expr
->right
->ctype
, &rtype
);
913 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
917 /* number op number */
918 if (lclass
& rclass
& TYPE_NUM
) {
919 if ((lclass
| rclass
) & TYPE_FLOAT
) {
921 case '+': case '-': case '*': case '/':
924 return bad_expr_type(expr
);
928 if (op
== SPECIAL_LEFTSHIFT
|| op
== SPECIAL_RIGHTSHIFT
) {
929 // shifts do integer promotions, but that's it.
930 unrestrict(expr
->left
, lclass
, <ype
);
931 unrestrict(expr
->right
, rclass
, &rtype
);
932 ctype
= ltype
= integer_promotion(ltype
);
933 rtype
= integer_promotion(rtype
);
935 // The rest do usual conversions
936 const unsigned left_not
= expr
->left
->type
== EXPR_PREOP
937 && expr
->left
->op
== '!';
938 const unsigned right_not
= expr
->right
->type
== EXPR_PREOP
939 && expr
->right
->op
== '!';
940 if ((op
== '&' || op
== '|') && (left_not
|| right_not
))
941 warning(expr
->pos
, "dubious: %sx %c %sy",
944 right_not
? "!" : "");
946 ltype
= usual_conversions(op
, expr
->left
, expr
->right
,
947 lclass
, rclass
, ltype
, rtype
);
948 ctype
= rtype
= ltype
;
951 expr
->left
= cast_to(expr
->left
, ltype
);
952 expr
->right
= cast_to(expr
->right
, rtype
);
957 /* pointer (+|-) integer */
958 if (lclass
& TYPE_PTR
&& is_int(rclass
) && (op
== '+' || op
== '-')) {
959 unrestrict(expr
->right
, rclass
, &rtype
);
960 return evaluate_ptr_add(expr
, rtype
);
963 /* integer + pointer */
964 if (rclass
& TYPE_PTR
&& is_int(lclass
) && op
== '+') {
965 struct expression
*index
= expr
->left
;
966 unrestrict(index
, lclass
, <ype
);
967 expr
->left
= expr
->right
;
969 return evaluate_ptr_add(expr
, ltype
);
972 /* pointer - pointer */
973 if (lclass
& rclass
& TYPE_PTR
&& expr
->op
== '-')
974 return evaluate_ptr_sub(expr
);
976 return bad_expr_type(expr
);
979 static struct symbol
*evaluate_comma(struct expression
*expr
)
981 expr
->ctype
= degenerate(expr
->right
);
982 if (expr
->ctype
== &null_ctype
)
983 expr
->ctype
= &ptr_ctype
;
984 expr
->flags
&= expr
->left
->flags
& expr
->right
->flags
;
988 static int modify_for_unsigned(int op
)
991 op
= SPECIAL_UNSIGNED_LT
;
993 op
= SPECIAL_UNSIGNED_GT
;
994 else if (op
== SPECIAL_LTE
)
995 op
= SPECIAL_UNSIGNED_LTE
;
996 else if (op
== SPECIAL_GTE
)
997 op
= SPECIAL_UNSIGNED_GTE
;
1001 static inline int is_null_pointer_constant(struct expression
*e
)
1003 if (e
->ctype
== &null_ctype
)
1005 if (!(e
->flags
& Int_const_expr
))
1007 return is_zero_constant(e
) ? 2 : 0;
1010 static struct symbol
*evaluate_compare(struct expression
*expr
)
1012 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1013 struct symbol
*ltype
, *rtype
, *lbase
, *rbase
;
1014 int lclass
= classify_type(degenerate(left
), <ype
);
1015 int rclass
= classify_type(degenerate(right
), &rtype
);
1016 struct symbol
*ctype
;
1017 const char *typediff
;
1020 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
1025 if (is_type_type(ltype
) && is_type_type(rtype
))
1028 if (is_safe_type(left
->ctype
) || is_safe_type(right
->ctype
))
1029 warning(expr
->pos
, "testing a 'safe expression'");
1031 /* number on number */
1032 if (lclass
& rclass
& TYPE_NUM
) {
1033 ctype
= usual_conversions(expr
->op
, expr
->left
, expr
->right
,
1034 lclass
, rclass
, ltype
, rtype
);
1035 expr
->left
= cast_to(expr
->left
, ctype
);
1036 expr
->right
= cast_to(expr
->right
, ctype
);
1037 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
1038 expr
->op
= modify_for_unsigned(expr
->op
);
1042 /* at least one must be a pointer */
1043 if (!((lclass
| rclass
) & TYPE_PTR
))
1044 return bad_expr_type(expr
);
1046 /* equality comparisons can be with null pointer constants */
1047 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1048 int is_null1
= is_null_pointer_constant(left
);
1049 int is_null2
= is_null_pointer_constant(right
);
1054 if (is_null1
&& is_null2
) {
1055 int positive
= expr
->op
== SPECIAL_EQUAL
;
1056 expr
->type
= EXPR_VALUE
;
1057 expr
->value
= positive
;
1060 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1061 left
= cast_to(left
, rtype
);
1064 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1065 right
= cast_to(right
, ltype
);
1069 /* both should be pointers */
1070 if (!(lclass
& rclass
& TYPE_PTR
))
1071 return bad_expr_type(expr
);
1072 expr
->op
= modify_for_unsigned(expr
->op
);
1074 lbase
= examine_pointer_target(ltype
);
1075 rbase
= examine_pointer_target(rtype
);
1077 /* they also have special treatment for pointers to void */
1078 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1079 if (ltype
->ctype
.as
== rtype
->ctype
.as
) {
1080 if (lbase
== &void_ctype
) {
1081 right
= cast_to(right
, ltype
);
1084 if (rbase
== &void_ctype
) {
1085 left
= cast_to(left
, rtype
);
1091 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1092 target_qualifiers(rtype
),
1093 target_qualifiers(ltype
));
1097 expression_error(expr
, "incompatible types in comparison expression (%s)", typediff
);
1101 expr
->ctype
= &bool_ctype
;
1106 * NOTE! The degenerate case of "x ? : y", where we don't
1107 * have a true case, this will possibly promote "x" to the
1108 * same type as "y", and thus _change_ the conditional
1109 * test in the expression. But since promotion is "safe"
1110 * for testing, that's OK.
1112 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
1114 struct expression
**true;
1115 struct symbol
*ctype
, *ltype
, *rtype
, *lbase
, *rbase
;
1117 const char * typediff
;
1120 if (!evaluate_conditional(expr
->conditional
, 0))
1122 if (!evaluate_expression(expr
->cond_false
))
1125 ctype
= degenerate(expr
->conditional
);
1126 rtype
= degenerate(expr
->cond_false
);
1128 true = &expr
->conditional
;
1130 if (expr
->cond_true
) {
1131 if (!evaluate_expression(expr
->cond_true
))
1133 ltype
= degenerate(expr
->cond_true
);
1134 true = &expr
->cond_true
;
1138 int flags
= expr
->conditional
->flags
& Int_const_expr
;
1139 flags
&= (*true)->flags
& expr
->cond_false
->flags
;
1144 lclass
= classify_type(ltype
, <ype
);
1145 rclass
= classify_type(rtype
, &rtype
);
1146 if (lclass
& rclass
& TYPE_NUM
) {
1147 ctype
= usual_conversions('?', *true, expr
->cond_false
,
1148 lclass
, rclass
, ltype
, rtype
);
1149 *true = cast_to(*true, ctype
);
1150 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1154 if ((lclass
| rclass
) & TYPE_PTR
) {
1155 int is_null1
= is_null_pointer_constant(*true);
1156 int is_null2
= is_null_pointer_constant(expr
->cond_false
);
1158 if (is_null1
&& is_null2
) {
1159 *true = cast_to(*true, &ptr_ctype
);
1160 expr
->cond_false
= cast_to(expr
->cond_false
, &ptr_ctype
);
1164 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1167 *true = cast_to(*true, rtype
);
1171 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1173 bad_null(expr
->cond_false
);
1174 expr
->cond_false
= cast_to(expr
->cond_false
, ltype
);
1178 if (!(lclass
& rclass
& TYPE_PTR
)) {
1179 typediff
= "different types";
1182 /* OK, it's pointer on pointer */
1183 if (ltype
->ctype
.as
!= rtype
->ctype
.as
) {
1184 typediff
= "different address spaces";
1188 /* need to be lazier here */
1189 lbase
= examine_pointer_target(ltype
);
1190 rbase
= examine_pointer_target(rtype
);
1191 qual
= target_qualifiers(ltype
) | target_qualifiers(rtype
);
1193 if (lbase
== &void_ctype
) {
1194 /* XXX: pointers to function should warn here */
1199 if (rbase
== &void_ctype
) {
1200 /* XXX: pointers to function should warn here */
1204 /* XXX: that should be pointer to composite */
1206 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1213 /* void on void, struct on same struct, union on same union */
1214 if (ltype
== rtype
) {
1218 typediff
= "different base types";
1221 expression_error(expr
, "incompatible types in conditional expression (%s)", typediff
);
1225 expr
->ctype
= ctype
;
1229 if (qual
& ~ctype
->ctype
.modifiers
) {
1230 struct symbol
*sym
= alloc_symbol(ctype
->pos
, SYM_PTR
);
1232 sym
->ctype
.modifiers
|= qual
;
1235 *true = cast_to(*true, ctype
);
1236 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1240 /* FP assignments can not do modulo or bit operations */
1241 static int compatible_float_op(int op
)
1243 return op
== SPECIAL_ADD_ASSIGN
||
1244 op
== SPECIAL_SUB_ASSIGN
||
1245 op
== SPECIAL_MUL_ASSIGN
||
1246 op
== SPECIAL_DIV_ASSIGN
;
1249 static int evaluate_assign_op(struct expression
*expr
)
1251 struct symbol
*target
= expr
->left
->ctype
;
1252 struct symbol
*source
= expr
->right
->ctype
;
1253 struct symbol
*t
, *s
;
1254 int tclass
= classify_type(target
, &t
);
1255 int sclass
= classify_type(source
, &s
);
1258 if (tclass
& sclass
& TYPE_NUM
) {
1259 if (tclass
& TYPE_FLOAT
&& !compatible_float_op(op
)) {
1260 expression_error(expr
, "invalid assignment");
1263 if (tclass
& TYPE_RESTRICT
) {
1264 if (!restricted_binop(op
, t
)) {
1265 warning(expr
->pos
, "bad assignment (%s) to %s",
1266 show_special(op
), show_typename(t
));
1267 expr
->right
= cast_to(expr
->right
, target
);
1270 /* allowed assignments unfoul */
1271 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1273 if (!restricted_value(expr
->right
, t
))
1275 } else if (!(sclass
& TYPE_RESTRICT
))
1277 /* source and target would better be identical restricted */
1280 warning(expr
->pos
, "invalid assignment: %s", show_special(op
));
1281 info(expr
->pos
, " left side has type %s", show_typename(t
));
1282 info(expr
->pos
, " right side has type %s", show_typename(s
));
1283 expr
->right
= cast_to(expr
->right
, target
);
1286 if (tclass
== TYPE_PTR
&& is_int(sclass
)) {
1287 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1288 unrestrict(expr
->right
, sclass
, &s
);
1289 evaluate_ptr_add(expr
, s
);
1292 expression_error(expr
, "invalid pointer assignment");
1296 expression_error(expr
, "invalid assignment");
1300 expr
->right
= cast_to(expr
->right
, target
);
1304 static int whitelist_pointers(struct symbol
*t1
, struct symbol
*t2
)
1307 return 0; /* yes, 0 - we don't want a cast_to here */
1308 if (t1
== &void_ctype
)
1310 if (t2
== &void_ctype
)
1312 if (classify_type(t1
, &t1
) != TYPE_NUM
)
1314 if (classify_type(t2
, &t2
) != TYPE_NUM
)
1318 if (t1
->ctype
.modifiers
& t2
->ctype
.modifiers
& MOD_CHAR
)
1320 if ((t1
->ctype
.modifiers
^ t2
->ctype
.modifiers
) & MOD_SIZE
)
1325 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1326 struct expression
**rp
, const char *where
)
1328 const char *typediff
;
1329 struct symbol
*source
= degenerate(*rp
);
1330 struct symbol
*t
, *s
;
1331 int tclass
= classify_type(target
, &t
);
1332 int sclass
= classify_type(source
, &s
);
1334 if (tclass
& sclass
& TYPE_NUM
) {
1335 if (tclass
& TYPE_RESTRICT
) {
1336 /* allowed assignments unfoul */
1337 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1339 if (!restricted_value(*rp
, target
))
1343 } else if (!(sclass
& TYPE_RESTRICT
))
1345 typediff
= "different base types";
1349 if (tclass
== TYPE_PTR
) {
1350 unsigned long mod1
, mod2
;
1351 struct symbol
*b1
, *b2
;
1352 // NULL pointer is always OK
1353 int is_null
= is_null_pointer_constant(*rp
);
1359 if (!(sclass
& TYPE_PTR
)) {
1360 typediff
= "different base types";
1363 b1
= examine_pointer_target(t
);
1364 b2
= examine_pointer_target(s
);
1365 mod1
= target_qualifiers(t
);
1366 mod2
= target_qualifiers(s
);
1367 if (whitelist_pointers(b1
, b2
)) {
1369 * assignments to/from void * are OK, provided that
1370 * we do not remove qualifiers from pointed to [C]
1371 * or mix address spaces [sparse].
1373 if (t
->ctype
.as
!= s
->ctype
.as
) {
1374 typediff
= "different address spaces";
1378 typediff
= "different modifiers";
1383 /* It's OK if the target is more volatile or const than the source */
1384 typediff
= type_difference(&t
->ctype
, &s
->ctype
, 0, mod1
);
1390 if ((tclass
& TYPE_COMPOUND
) && s
== t
)
1393 if (tclass
& TYPE_NUM
) {
1394 /* XXX: need to turn into comparison with NULL */
1395 if (t
== &bool_ctype
&& (sclass
& TYPE_PTR
))
1397 typediff
= "different base types";
1400 typediff
= "invalid types";
1403 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1404 info(expr
->pos
, " expected %s", show_typename(target
));
1405 info(expr
->pos
, " got %s", show_typename(source
));
1406 *rp
= cast_to(*rp
, target
);
1409 *rp
= cast_to(*rp
, target
);
1413 static void mark_assigned(struct expression
*expr
)
1419 switch (expr
->type
) {
1424 if (sym
->type
!= SYM_NODE
)
1426 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1430 mark_assigned(expr
->left
);
1431 mark_assigned(expr
->right
);
1434 case EXPR_FORCE_CAST
:
1435 mark_assigned(expr
->cast_expression
);
1438 mark_assigned(expr
->base
);
1446 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1448 if (type
->ctype
.modifiers
& MOD_CONST
)
1449 expression_error(left
, "assignment to const expression");
1451 /* We know left is an lvalue, so it's a "preop-*" */
1452 mark_assigned(left
->unop
);
1455 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1457 struct expression
*left
= expr
->left
;
1458 struct expression
*where
= expr
;
1459 struct symbol
*ltype
;
1461 if (!lvalue_expression(left
)) {
1462 expression_error(expr
, "not an lvalue");
1466 ltype
= left
->ctype
;
1468 if (expr
->op
!= '=') {
1469 if (!evaluate_assign_op(expr
))
1472 if (!compatible_assignment_types(where
, ltype
, &expr
->right
, "assignment"))
1476 evaluate_assign_to(left
, ltype
);
1478 expr
->ctype
= ltype
;
1482 static void examine_fn_arguments(struct symbol
*fn
)
1486 FOR_EACH_PTR(fn
->arguments
, s
) {
1487 struct symbol
*arg
= evaluate_symbol(s
);
1488 /* Array/function arguments silently degenerate into pointers */
1494 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1495 if (arg
->type
== SYM_ARRAY
)
1496 ptr
->ctype
= arg
->ctype
;
1498 ptr
->ctype
.base_type
= arg
;
1499 ptr
->ctype
.as
|= s
->ctype
.as
;
1500 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
& MOD_PTRINHERIT
;
1502 s
->ctype
.base_type
= ptr
;
1504 s
->ctype
.modifiers
&= ~MOD_PTRINHERIT
;
1507 examine_symbol_type(s
);
1514 } END_FOR_EACH_PTR(s
);
1517 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1519 /* Take the modifiers of the pointer, and apply them to the member */
1520 mod
|= sym
->ctype
.modifiers
;
1521 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1522 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1524 newsym
->ctype
.as
= as
;
1525 newsym
->ctype
.modifiers
= mod
;
1531 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1533 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1534 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1536 node
->ctype
.base_type
= ptr
;
1537 ptr
->bit_size
= bits_in_pointer
;
1538 ptr
->ctype
.alignment
= pointer_alignment
;
1540 node
->bit_size
= bits_in_pointer
;
1541 node
->ctype
.alignment
= pointer_alignment
;
1544 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1545 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1546 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1548 if (sym
->type
== SYM_NODE
) {
1549 ptr
->ctype
.as
|= sym
->ctype
.as
;
1550 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1551 sym
= sym
->ctype
.base_type
;
1553 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1554 ptr
->ctype
.as
|= sym
->ctype
.as
;
1555 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1556 sym
= sym
->ctype
.base_type
;
1558 ptr
->ctype
.base_type
= sym
;
1563 /* Arrays degenerate into pointers on pointer arithmetic */
1564 static struct symbol
*degenerate(struct expression
*expr
)
1566 struct symbol
*ctype
, *base
;
1570 ctype
= expr
->ctype
;
1573 base
= examine_symbol_type(ctype
);
1574 if (ctype
->type
== SYM_NODE
)
1575 base
= ctype
->ctype
.base_type
;
1577 * Arrays degenerate into pointers to the entries, while
1578 * functions degenerate into pointers to themselves.
1579 * If array was part of non-lvalue compound, we create a copy
1580 * of that compound first and then act as if we were dealing with
1581 * the corresponding field in there.
1583 switch (base
->type
) {
1585 if (expr
->type
== EXPR_SLICE
) {
1586 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1587 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1589 a
->ctype
.base_type
= expr
->base
->ctype
;
1590 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1591 a
->array_size
= expr
->base
->ctype
->array_size
;
1593 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1595 e0
->ctype
= &lazy_ptr_ctype
;
1597 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1600 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1602 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1604 e2
->right
= expr
->base
;
1606 e2
->ctype
= expr
->base
->ctype
;
1608 if (expr
->r_bitpos
) {
1609 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1612 e3
->right
= alloc_const_expression(expr
->pos
,
1613 bits_to_bytes(expr
->r_bitpos
));
1614 e3
->ctype
= &lazy_ptr_ctype
;
1619 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1622 e4
->ctype
= &lazy_ptr_ctype
;
1625 expr
->type
= EXPR_PREOP
;
1629 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1630 expression_error(expr
, "strange non-value function or array");
1633 *expr
= *expr
->unop
;
1634 ctype
= create_pointer(expr
, ctype
, 1);
1635 expr
->ctype
= ctype
;
1642 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1644 struct expression
*op
= expr
->unop
;
1645 struct symbol
*ctype
;
1647 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1648 expression_error(expr
, "not addressable");
1655 if (expr
->type
== EXPR_SYMBOL
) {
1656 struct symbol
*sym
= expr
->symbol
;
1657 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1661 * symbol expression evaluation is lazy about the type
1662 * of the sub-expression, so we may have to generate
1663 * the type here if so..
1665 if (expr
->ctype
== &lazy_ptr_ctype
) {
1666 ctype
= create_pointer(expr
, ctype
, 0);
1667 expr
->ctype
= ctype
;
1673 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1675 struct expression
*op
= expr
->unop
;
1676 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1678 /* Simplify: *&(expr) => (expr) */
1679 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1685 /* Dereferencing a node drops all the node information. */
1686 if (ctype
->type
== SYM_NODE
)
1687 ctype
= ctype
->ctype
.base_type
;
1689 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1690 target
= ctype
->ctype
.base_type
;
1692 switch (ctype
->type
) {
1694 expression_error(expr
, "cannot dereference this type");
1697 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1698 merge_type(node
, ctype
);
1702 if (!lvalue_expression(op
)) {
1703 expression_error(op
, "non-lvalue array??");
1707 /* Do the implied "addressof" on the array */
1711 * When an array is dereferenced, we need to pick
1712 * up the attributes of the original node too..
1714 merge_type(node
, op
->ctype
);
1715 merge_type(node
, ctype
);
1719 node
->bit_size
= target
->bit_size
;
1720 node
->array_size
= target
->array_size
;
1727 * Unary post-ops: x++ and x--
1729 static struct symbol
*evaluate_postop(struct expression
*expr
)
1731 struct expression
*op
= expr
->unop
;
1732 struct symbol
*ctype
= op
->ctype
;
1733 int class = classify_type(op
->ctype
, &ctype
);
1736 if (!lvalue_expression(expr
->unop
)) {
1737 expression_error(expr
, "need lvalue expression for ++/--");
1741 if ((class & TYPE_RESTRICT
) && restricted_unop(expr
->op
, &ctype
))
1742 return bad_expr_type(expr
);
1744 if (class & TYPE_NUM
) {
1746 } else if (class == TYPE_PTR
) {
1747 struct symbol
*target
= examine_pointer_target(ctype
);
1748 if (!is_function(target
))
1749 multiply
= bits_to_bytes(target
->bit_size
);
1753 evaluate_assign_to(op
, op
->ctype
);
1754 expr
->op_value
= multiply
;
1755 expr
->ctype
= ctype
;
1759 expression_error(expr
, "bad argument type for ++/--");
1763 static struct symbol
*evaluate_sign(struct expression
*expr
)
1765 struct symbol
*ctype
= expr
->unop
->ctype
;
1766 int class = classify_type(ctype
, &ctype
);
1767 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1769 /* should be an arithmetic type */
1770 if (!(class & TYPE_NUM
))
1771 return bad_expr_type(expr
);
1772 if (!(class & (TYPE_FLOAT
|TYPE_RESTRICT
))) {
1773 struct symbol
*rtype
= integer_promotion(ctype
);
1774 expr
->unop
= cast_to(expr
->unop
, rtype
);
1776 } else if ((class & TYPE_FLOAT
) && expr
->op
!= '~') {
1777 /* no conversions needed */
1778 } else if ((class & TYPE_RESTRICT
) && !restricted_unop(expr
->op
, &ctype
)) {
1779 /* no conversions needed */
1781 return bad_expr_type(expr
);
1783 if (expr
->op
== '+')
1784 *expr
= *expr
->unop
;
1785 expr
->ctype
= ctype
;
1789 static struct symbol
*evaluate_preop(struct expression
*expr
)
1791 struct symbol
*ctype
= expr
->unop
->ctype
;
1795 *expr
= *expr
->unop
;
1801 return evaluate_sign(expr
);
1804 return evaluate_dereference(expr
);
1807 return evaluate_addressof(expr
);
1809 case SPECIAL_INCREMENT
:
1810 case SPECIAL_DECREMENT
:
1812 * From a type evaluation standpoint the preops are
1813 * the same as the postops
1815 return evaluate_postop(expr
);
1818 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1820 if (is_safe_type(ctype
))
1821 warning(expr
->pos
, "testing a 'safe expression'");
1822 if (is_float_type(ctype
)) {
1823 struct expression
*arg
= expr
->unop
;
1824 expr
->type
= EXPR_BINOP
;
1825 expr
->op
= SPECIAL_EQUAL
;
1827 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1828 expr
->right
->ctype
= ctype
;
1829 expr
->right
->fvalue
= 0;
1830 } else if (is_fouled_type(ctype
)) {
1831 warning(expr
->pos
, "%s degrades to integer",
1832 show_typename(ctype
->ctype
.base_type
));
1834 ctype
= &bool_ctype
;
1840 expr
->ctype
= ctype
;
1844 static struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1846 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1847 struct ptr_list
*list
= head
;
1853 for (i
= 0; i
< list
->nr
; i
++) {
1854 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1856 if (sym
->ident
!= ident
)
1858 *offset
= sym
->offset
;
1861 struct symbol
*ctype
= sym
->ctype
.base_type
;
1865 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1867 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1870 *offset
+= sym
->offset
;
1874 } while ((list
= list
->next
) != head
);
1878 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1880 struct expression
*add
;
1883 * Create a new add-expression
1885 * NOTE! Even if we just add zero, we need a new node
1886 * for the member pointer, since it has a different
1887 * type than the original pointer. We could make that
1888 * be just a cast, but the fact is, a node is a node,
1889 * so we might as well just do the "add zero" here.
1891 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1894 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1895 add
->right
->ctype
= &int_ctype
;
1896 add
->right
->value
= offset
;
1899 * The ctype of the pointer will be lazily evaluated if
1900 * we ever take the address of this member dereference..
1902 add
->ctype
= &lazy_ptr_ctype
;
1906 /* structure/union dereference */
1907 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1910 struct symbol
*ctype
, *member
;
1911 struct expression
*deref
= expr
->deref
, *add
;
1912 struct ident
*ident
= expr
->member
;
1916 if (!evaluate_expression(deref
))
1919 expression_error(expr
, "bad member name");
1923 ctype
= deref
->ctype
;
1924 examine_symbol_type(ctype
);
1925 address_space
= ctype
->ctype
.as
;
1926 mod
= ctype
->ctype
.modifiers
;
1927 if (ctype
->type
== SYM_NODE
) {
1928 ctype
= ctype
->ctype
.base_type
;
1929 address_space
|= ctype
->ctype
.as
;
1930 mod
|= ctype
->ctype
.modifiers
;
1932 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1933 expression_error(expr
, "expected structure or union");
1937 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1939 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1940 const char *name
= "<unnamed>";
1943 name
= ctype
->ident
->name
;
1944 namelen
= ctype
->ident
->len
;
1946 if (ctype
->symbol_list
)
1947 expression_error(expr
, "no member '%s' in %s %.*s",
1948 show_ident(ident
), type
, namelen
, name
);
1950 expression_error(expr
, "using member '%s' in "
1951 "incomplete %s %.*s", show_ident(ident
),
1952 type
, namelen
, name
);
1957 * The member needs to take on the address space and modifiers of
1958 * the "parent" type.
1960 member
= convert_to_as_mod(member
, address_space
, mod
);
1961 ctype
= get_base_type(member
);
1963 if (!lvalue_expression(deref
)) {
1964 if (deref
->type
!= EXPR_SLICE
) {
1968 expr
->base
= deref
->base
;
1969 expr
->r_bitpos
= deref
->r_bitpos
;
1971 expr
->r_bitpos
+= bytes_to_bits(offset
);
1972 expr
->type
= EXPR_SLICE
;
1973 expr
->r_nrbits
= member
->bit_size
;
1974 expr
->r_bitpos
+= member
->bit_offset
;
1975 expr
->ctype
= member
;
1979 deref
= deref
->unop
;
1980 expr
->deref
= deref
;
1982 add
= evaluate_offset(deref
, offset
);
1983 expr
->type
= EXPR_PREOP
;
1987 expr
->ctype
= member
;
1991 static int is_promoted(struct expression
*expr
)
1994 switch (expr
->type
) {
1997 case EXPR_CONDITIONAL
:
2021 static struct symbol
*evaluate_cast(struct expression
*);
2023 static struct symbol
*evaluate_type_information(struct expression
*expr
)
2025 struct symbol
*sym
= expr
->cast_type
;
2027 sym
= evaluate_expression(expr
->cast_expression
);
2031 * Expressions of restricted types will possibly get
2032 * promoted - check that here
2034 if (is_restricted_type(sym
)) {
2035 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
2037 } else if (is_fouled_type(sym
)) {
2041 examine_symbol_type(sym
);
2042 if (is_bitfield_type(sym
)) {
2043 expression_error(expr
, "trying to examine bitfield type");
2049 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
2051 struct symbol
*type
;
2054 type
= evaluate_type_information(expr
);
2058 size
= type
->bit_size
;
2060 if (size
< 0 && is_void_type(type
)) {
2061 warning(expr
->pos
, "expression using sizeof(void)");
2062 size
= bits_in_char
;
2065 if (is_function(type
->ctype
.base_type
)) {
2066 warning(expr
->pos
, "expression using sizeof on a function");
2067 size
= bits_in_char
;
2070 if ((size
< 0) || (size
& (bits_in_char
- 1)))
2071 expression_error(expr
, "cannot size expression");
2073 expr
->type
= EXPR_VALUE
;
2074 expr
->value
= bits_to_bytes(size
);
2076 expr
->ctype
= size_t_ctype
;
2077 return size_t_ctype
;
2080 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
2082 struct symbol
*type
;
2085 type
= evaluate_type_information(expr
);
2089 if (type
->type
== SYM_NODE
)
2090 type
= type
->ctype
.base_type
;
2093 switch (type
->type
) {
2097 type
= get_base_type(type
);
2101 expression_error(expr
, "expected pointer expression");
2104 size
= type
->bit_size
;
2105 if (size
& (bits_in_char
-1))
2107 expr
->type
= EXPR_VALUE
;
2108 expr
->value
= bits_to_bytes(size
);
2110 expr
->ctype
= size_t_ctype
;
2111 return size_t_ctype
;
2114 static struct symbol
*evaluate_alignof(struct expression
*expr
)
2116 struct symbol
*type
;
2118 type
= evaluate_type_information(expr
);
2122 expr
->type
= EXPR_VALUE
;
2123 expr
->value
= type
->ctype
.alignment
;
2125 expr
->ctype
= size_t_ctype
;
2126 return size_t_ctype
;
2129 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
2131 struct expression
*expr
;
2132 struct symbol_list
*argument_types
= fn
->arguments
;
2133 struct symbol
*argtype
;
2136 PREPARE_PTR_LIST(argument_types
, argtype
);
2137 FOR_EACH_PTR (head
, expr
) {
2138 struct expression
**p
= THIS_ADDRESS(expr
);
2139 struct symbol
*ctype
, *target
;
2140 ctype
= evaluate_expression(expr
);
2147 struct symbol
*type
;
2148 int class = classify_type(ctype
, &type
);
2149 if (is_int(class)) {
2150 *p
= cast_to(expr
, integer_promotion(type
));
2151 } else if (class & TYPE_FLOAT
) {
2152 unsigned long mod
= type
->ctype
.modifiers
;
2153 if (!(mod
& (MOD_LONG
|MOD_LONGLONG
)))
2154 *p
= cast_to(expr
, &double_ctype
);
2155 } else if (class & TYPE_PTR
) {
2156 if (expr
->ctype
== &null_ctype
)
2157 *p
= cast_to(expr
, &ptr_ctype
);
2162 static char where
[30];
2163 examine_symbol_type(target
);
2164 sprintf(where
, "argument %d", i
);
2165 compatible_assignment_types(expr
, target
, p
, where
);
2169 NEXT_PTR_LIST(argtype
);
2170 } END_FOR_EACH_PTR(expr
);
2171 FINISH_PTR_LIST(argtype
);
2175 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
2179 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
2180 if (sym
->ident
== ident
)
2182 } END_FOR_EACH_PTR(sym
);
2186 static void convert_index(struct expression
*e
)
2188 struct expression
*child
= e
->idx_expression
;
2189 unsigned from
= e
->idx_from
;
2190 unsigned to
= e
->idx_to
+ 1;
2192 e
->init_offset
= from
* bits_to_bytes(e
->ctype
->bit_size
);
2193 e
->init_nr
= to
- from
;
2194 e
->init_expr
= child
;
2197 static void convert_ident(struct expression
*e
)
2199 struct expression
*child
= e
->ident_expression
;
2200 struct symbol
*sym
= e
->field
;
2202 e
->init_offset
= sym
->offset
;
2204 e
->init_expr
= child
;
2207 static void convert_designators(struct expression
*e
)
2210 if (e
->type
== EXPR_INDEX
)
2212 else if (e
->type
== EXPR_IDENTIFIER
)
2220 static void excess(struct expression
*e
, const char *s
)
2222 warning(e
->pos
, "excessive elements in %s initializer", s
);
2226 * implicit designator for the first element
2228 static struct expression
*first_subobject(struct symbol
*ctype
, int class,
2229 struct expression
**v
)
2231 struct expression
*e
= *v
, *new;
2233 if (ctype
->type
== SYM_NODE
)
2234 ctype
= ctype
->ctype
.base_type
;
2236 if (class & TYPE_PTR
) { /* array */
2237 if (!ctype
->bit_size
)
2239 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2240 new->idx_expression
= e
;
2241 new->ctype
= ctype
->ctype
.base_type
;
2243 struct symbol
*field
, *p
;
2244 PREPARE_PTR_LIST(ctype
->symbol_list
, p
);
2245 while (p
&& !p
->ident
&& is_bitfield_type(p
))
2251 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2252 new->ident_expression
= e
;
2253 new->field
= new->ctype
= field
;
2260 * sanity-check explicit designators; return the innermost one or NULL
2261 * in case of error. Assign types.
2263 static struct expression
*check_designators(struct expression
*e
,
2264 struct symbol
*ctype
)
2266 struct expression
*last
= NULL
;
2269 if (ctype
->type
== SYM_NODE
)
2270 ctype
= ctype
->ctype
.base_type
;
2271 if (e
->type
== EXPR_INDEX
) {
2272 struct symbol
*type
;
2273 if (ctype
->type
!= SYM_ARRAY
) {
2274 err
= "array index in non-array";
2277 type
= ctype
->ctype
.base_type
;
2278 if (ctype
->bit_size
>= 0 && type
->bit_size
>= 0) {
2279 unsigned offset
= e
->idx_to
* type
->bit_size
;
2280 if (offset
>= ctype
->bit_size
) {
2281 err
= "index out of bounds in";
2285 e
->ctype
= ctype
= type
;
2288 if (!e
->idx_expression
) {
2292 e
= e
->idx_expression
;
2293 } else if (e
->type
== EXPR_IDENTIFIER
) {
2294 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2295 err
= "field name not in struct or union";
2298 ctype
= find_struct_ident(ctype
, e
->expr_ident
);
2300 err
= "unknown field name in";
2303 e
->field
= e
->ctype
= ctype
;
2305 if (!e
->ident_expression
) {
2309 e
= e
->ident_expression
;
2310 } else if (e
->type
== EXPR_POS
) {
2311 err
= "internal front-end error: EXPR_POS in";
2316 expression_error(e
, "%s initializer", err
);
2321 * choose the next subobject to initialize.
2323 * Get designators for next element, switch old ones to EXPR_POS.
2324 * Return the resulting expression or NULL if we'd run out of subobjects.
2325 * The innermost designator is returned in *v. Designators in old
2326 * are assumed to be already sanity-checked.
2328 static struct expression
*next_designators(struct expression
*old
,
2329 struct symbol
*ctype
,
2330 struct expression
*e
, struct expression
**v
)
2332 struct expression
*new = NULL
;
2336 if (old
->type
== EXPR_INDEX
) {
2337 struct expression
*copy
;
2340 copy
= next_designators(old
->idx_expression
,
2343 n
= old
->idx_to
+ 1;
2344 if (n
* old
->ctype
->bit_size
== ctype
->bit_size
) {
2349 *v
= new = alloc_expression(e
->pos
, EXPR_INDEX
);
2352 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2355 new->idx_from
= new->idx_to
= n
;
2356 new->idx_expression
= copy
;
2357 new->ctype
= old
->ctype
;
2359 } else if (old
->type
== EXPR_IDENTIFIER
) {
2360 struct expression
*copy
;
2361 struct symbol
*field
;
2363 copy
= next_designators(old
->ident_expression
,
2366 field
= old
->field
->next_subobject
;
2372 *v
= new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2375 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2379 new->expr_ident
= field
->ident
;
2380 new->ident_expression
= copy
;
2387 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2388 int class, struct symbol
*ctype
);
2391 * deal with traversing subobjects [6.7.8(17,18,20)]
2393 static void handle_list_initializer(struct expression
*expr
,
2394 int class, struct symbol
*ctype
)
2396 struct expression
*e
, *last
= NULL
, *top
= NULL
, *next
;
2399 FOR_EACH_PTR(expr
->expr_list
, e
) {
2400 struct expression
**v
;
2401 struct symbol
*type
;
2404 if (e
->type
!= EXPR_INDEX
&& e
->type
!= EXPR_IDENTIFIER
) {
2407 last
= first_subobject(ctype
, class, &top
);
2409 last
= next_designators(last
, ctype
, e
, &top
);
2412 excess(e
, class & TYPE_PTR
? "array" :
2414 DELETE_CURRENT_PTR(e
);
2418 warning(e
->pos
, "advancing past deep designator");
2421 REPLACE_CURRENT_PTR(e
, last
);
2423 next
= check_designators(e
, ctype
);
2425 DELETE_CURRENT_PTR(e
);
2429 /* deeper than one designator? */
2431 convert_designators(last
);
2436 lclass
= classify_type(top
->ctype
, &type
);
2437 if (top
->type
== EXPR_INDEX
)
2438 v
= &top
->idx_expression
;
2440 v
= &top
->ident_expression
;
2442 if (handle_simple_initializer(v
, 1, lclass
, top
->ctype
))
2445 if (!(lclass
& TYPE_COMPOUND
)) {
2446 warning(e
->pos
, "bogus scalar initializer");
2447 DELETE_CURRENT_PTR(e
);
2451 next
= first_subobject(type
, lclass
, v
);
2453 warning(e
->pos
, "missing braces around initializer");
2458 DELETE_CURRENT_PTR(e
);
2459 excess(e
, lclass
& TYPE_PTR
? "array" : "struct or union");
2461 } END_FOR_EACH_PTR(e
);
2463 convert_designators(last
);
2464 expr
->ctype
= ctype
;
2467 static int is_string_literal(struct expression
**v
)
2469 struct expression
*e
= *v
;
2470 while (e
&& e
->type
== EXPR_PREOP
&& e
->op
== '(')
2472 if (!e
|| e
->type
!= EXPR_STRING
)
2474 if (e
!= *v
&& Wparen_string
)
2476 "array initialized from parenthesized string constant");
2482 * We want a normal expression, possibly in one layer of braces. Warn
2483 * if the latter happens inside a list (it's legal, but likely to be
2484 * an effect of screwup). In case of anything not legal, we are definitely
2485 * having an effect of screwup, so just fail and let the caller warn.
2487 static struct expression
*handle_scalar(struct expression
*e
, int nested
)
2489 struct expression
*v
= NULL
, *p
;
2493 if (e
->type
!= EXPR_INITIALIZER
)
2496 FOR_EACH_PTR(e
->expr_list
, p
) {
2500 } END_FOR_EACH_PTR(p
);
2504 case EXPR_INITIALIZER
:
2506 case EXPR_IDENTIFIER
:
2512 warning(e
->pos
, "braces around scalar initializer");
2517 * deal with the cases that don't care about subobjects:
2518 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2519 * character array <- string literal, possibly in braces [6.7.8(14)]
2520 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2521 * compound type <- initializer list in braces [6.7.8(16)]
2522 * The last one punts to handle_list_initializer() which, in turn will call
2523 * us for individual elements of the list.
2525 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2526 * the lack of support of wide char stuff in general.
2528 * One note: we need to take care not to evaluate a string literal until
2529 * we know that we *will* handle it right here. Otherwise we would screw
2530 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2531 * { "string", ...} - we need to preserve that string literal recognizable
2532 * until we dig into the inner struct.
2534 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2535 int class, struct symbol
*ctype
)
2537 int is_string
= is_string_type(ctype
);
2538 struct expression
*e
= *ep
, *p
;
2539 struct symbol
*type
;
2545 if (!(class & TYPE_COMPOUND
)) {
2546 e
= handle_scalar(e
, nested
);
2550 if (!evaluate_expression(e
))
2552 compatible_assignment_types(e
, ctype
, ep
, "initializer");
2557 * sublist; either a string, or we dig in; the latter will deal with
2558 * pathologies, so we don't need anything fancy here.
2560 if (e
->type
== EXPR_INITIALIZER
) {
2562 struct expression
*v
= NULL
;
2565 FOR_EACH_PTR(e
->expr_list
, p
) {
2569 } END_FOR_EACH_PTR(p
);
2570 if (count
== 1 && is_string_literal(&v
)) {
2575 handle_list_initializer(e
, class, ctype
);
2580 if (is_string_literal(&e
)) {
2581 /* either we are doing array of char, or we'll have to dig in */
2588 /* struct or union can be initialized by compatible */
2589 if (class != TYPE_COMPOUND
)
2591 type
= evaluate_expression(e
);
2594 if (ctype
->type
== SYM_NODE
)
2595 ctype
= ctype
->ctype
.base_type
;
2596 if (type
->type
== SYM_NODE
)
2597 type
= type
->ctype
.base_type
;
2603 p
= alloc_expression(e
->pos
, EXPR_STRING
);
2605 type
= evaluate_expression(p
);
2606 if (ctype
->bit_size
!= -1 &&
2607 ctype
->bit_size
+ bits_in_char
< type
->bit_size
) {
2609 "too long initializer-string for array of char");
2615 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2617 struct symbol
*type
;
2618 int class = classify_type(ctype
, &type
);
2619 if (!handle_simple_initializer(ep
, 0, class, ctype
))
2620 expression_error(*ep
, "invalid initializer");
2623 static struct symbol
*evaluate_cast(struct expression
*expr
)
2625 struct expression
*target
= expr
->cast_expression
;
2626 struct symbol
*ctype
;
2627 struct symbol
*t1
, *t2
;
2629 int as1
= 0, as2
= 0;
2635 * Special case: a cast can be followed by an
2636 * initializer, in which case we need to pass
2637 * the type value down to that initializer rather
2638 * than trying to evaluate it as an expression
2640 * A more complex case is when the initializer is
2641 * dereferenced as part of a post-fix expression.
2642 * We need to produce an expression that can be dereferenced.
2644 if (target
->type
== EXPR_INITIALIZER
) {
2645 struct symbol
*sym
= expr
->cast_type
;
2646 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2648 sym
->initializer
= target
;
2649 evaluate_symbol(sym
);
2651 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2654 expr
->type
= EXPR_PREOP
;
2662 ctype
= examine_symbol_type(expr
->cast_type
);
2663 expr
->ctype
= ctype
;
2664 expr
->cast_type
= ctype
;
2666 evaluate_expression(target
);
2669 class1
= classify_type(ctype
, &t1
);
2671 /* cast to non-integer type -> not an integer constant expression */
2672 if (!is_int(class1
))
2674 /* if argument turns out to be not an integer constant expression *and*
2675 it was not a floating literal to start with -> too bad */
2676 else if (expr
->flags
== Int_const_expr
&&
2677 !(target
->flags
& Int_const_expr
))
2680 * You can always throw a value away by casting to
2681 * "void" - that's an implicit "force". Note that
2682 * the same is _not_ true of "void *".
2684 if (t1
== &void_ctype
)
2687 if (class1
& (TYPE_COMPOUND
| TYPE_FN
))
2688 warning(expr
->pos
, "cast to non-scalar");
2692 expression_error(expr
, "cast from unknown type");
2695 class2
= classify_type(t2
, &t2
);
2697 if (class2
& TYPE_COMPOUND
)
2698 warning(expr
->pos
, "cast from non-scalar");
2700 if (expr
->type
== EXPR_FORCE_CAST
)
2703 /* allowed cast unfouls */
2704 if (class2
& TYPE_FOULED
)
2708 if (class1
& TYPE_RESTRICT
)
2709 warning(expr
->pos
, "cast to %s",
2711 if (class2
& TYPE_RESTRICT
)
2712 warning(expr
->pos
, "cast from %s",
2716 if (t1
== &ulong_ctype
)
2718 else if (class1
== TYPE_PTR
) {
2719 examine_pointer_target(t1
);
2723 if (t2
== &ulong_ctype
)
2725 else if (class2
== TYPE_PTR
) {
2726 examine_pointer_target(t2
);
2730 if (!as1
&& as2
> 0)
2731 warning(expr
->pos
, "cast removes address space of expression");
2732 if (as1
> 0 && as2
> 0 && as1
!= as2
)
2733 warning(expr
->pos
, "cast between address spaces (<asn:%d>-><asn:%d>)", as2
, as1
);
2734 if (as1
> 0 && !as2
&&
2735 !is_null_pointer_constant(target
) && Wcast_to_as
)
2737 "cast adds address space to expression (<asn:%d>)", as1
);
2739 if (!(t1
->ctype
.modifiers
& MOD_PTRINHERIT
) && class1
== TYPE_PTR
&&
2740 !as1
&& (target
->flags
& Int_const_expr
)) {
2741 if (t1
->ctype
.base_type
== &void_ctype
) {
2742 if (is_zero_constant(target
)) {
2744 expr
->type
= EXPR_VALUE
;
2745 expr
->ctype
= &null_ctype
;
2756 * Evaluate a call expression with a symbol. This
2757 * should expand inline functions, and evaluate
2760 static int evaluate_symbol_call(struct expression
*expr
)
2762 struct expression
*fn
= expr
->fn
;
2763 struct symbol
*ctype
= fn
->ctype
;
2765 if (fn
->type
!= EXPR_PREOP
)
2768 if (ctype
->op
&& ctype
->op
->evaluate
)
2769 return ctype
->op
->evaluate(expr
);
2771 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2773 struct symbol
*curr
= current_fn
;
2774 current_fn
= ctype
->ctype
.base_type
;
2776 ret
= inline_function(expr
, ctype
);
2778 /* restore the old function */
2786 static struct symbol
*evaluate_call(struct expression
*expr
)
2789 struct symbol
*ctype
, *sym
;
2790 struct expression
*fn
= expr
->fn
;
2791 struct expression_list
*arglist
= expr
->args
;
2793 if (!evaluate_expression(fn
))
2795 sym
= ctype
= fn
->ctype
;
2796 if (ctype
->type
== SYM_NODE
)
2797 ctype
= ctype
->ctype
.base_type
;
2798 if (ctype
->type
== SYM_PTR
)
2799 ctype
= get_base_type(ctype
);
2801 if (ctype
->type
!= SYM_FN
) {
2802 struct expression
*arg
;
2803 expression_error(expr
, "not a function %s",
2804 show_ident(sym
->ident
));
2805 /* do typechecking in arguments */
2806 FOR_EACH_PTR (arglist
, arg
) {
2807 evaluate_expression(arg
);
2808 } END_FOR_EACH_PTR(arg
);
2812 examine_fn_arguments(ctype
);
2813 if (sym
->type
== SYM_NODE
&& fn
->type
== EXPR_PREOP
&&
2814 sym
->op
&& sym
->op
->args
) {
2815 if (!sym
->op
->args(expr
))
2818 if (!evaluate_arguments(sym
, ctype
, arglist
))
2820 args
= expression_list_size(expr
->args
);
2821 fnargs
= symbol_list_size(ctype
->arguments
);
2823 expression_error(expr
,
2824 "not enough arguments for function %s",
2825 show_ident(sym
->ident
));
2826 if (args
> fnargs
&& !ctype
->variadic
)
2827 expression_error(expr
,
2828 "too many arguments for function %s",
2829 show_ident(sym
->ident
));
2831 if (sym
->type
== SYM_NODE
) {
2832 if (evaluate_symbol_call(expr
))
2835 expr
->ctype
= ctype
->ctype
.base_type
;
2839 static struct symbol
*evaluate_offsetof(struct expression
*expr
)
2841 struct expression
*e
= expr
->down
;
2842 struct symbol
*ctype
= expr
->in
;
2845 if (expr
->op
== '.') {
2846 struct symbol
*field
;
2849 expression_error(expr
, "expected structure or union");
2852 examine_symbol_type(ctype
);
2853 class = classify_type(ctype
, &ctype
);
2854 if (class != TYPE_COMPOUND
) {
2855 expression_error(expr
, "expected structure or union");
2859 field
= find_identifier(expr
->ident
, ctype
->symbol_list
, &offset
);
2861 expression_error(expr
, "unknown member");
2865 expr
->type
= EXPR_VALUE
;
2866 expr
->flags
= Int_const_expr
;
2867 expr
->value
= offset
;
2869 expr
->ctype
= size_t_ctype
;
2872 expression_error(expr
, "expected structure or union");
2875 examine_symbol_type(ctype
);
2876 class = classify_type(ctype
, &ctype
);
2877 if (class != (TYPE_COMPOUND
| TYPE_PTR
)) {
2878 expression_error(expr
, "expected array");
2881 ctype
= ctype
->ctype
.base_type
;
2883 expr
->type
= EXPR_VALUE
;
2884 expr
->flags
= Int_const_expr
;
2887 expr
->ctype
= size_t_ctype
;
2889 struct expression
*idx
= expr
->index
, *m
;
2890 struct symbol
*i_type
= evaluate_expression(idx
);
2891 int i_class
= classify_type(i_type
, &i_type
);
2892 if (!is_int(i_class
)) {
2893 expression_error(expr
, "non-integer index");
2896 unrestrict(idx
, i_class
, &i_type
);
2897 idx
= cast_to(idx
, size_t_ctype
);
2898 m
= alloc_const_expression(expr
->pos
,
2899 bits_to_bytes(ctype
->bit_size
));
2900 m
->ctype
= size_t_ctype
;
2901 m
->flags
= Int_const_expr
;
2902 expr
->type
= EXPR_BINOP
;
2906 expr
->ctype
= size_t_ctype
;
2907 expr
->flags
= m
->flags
& idx
->flags
& Int_const_expr
;
2911 struct expression
*copy
= __alloc_expression(0);
2913 if (e
->type
== EXPR_OFFSETOF
)
2915 if (!evaluate_expression(e
))
2917 expr
->type
= EXPR_BINOP
;
2918 expr
->flags
= e
->flags
& copy
->flags
& Int_const_expr
;
2920 expr
->ctype
= size_t_ctype
;
2924 return size_t_ctype
;
2927 struct symbol
*evaluate_expression(struct expression
*expr
)
2934 switch (expr
->type
) {
2937 expression_error(expr
, "value expression without a type");
2940 return evaluate_string(expr
);
2942 return evaluate_symbol_expression(expr
);
2944 if (!evaluate_expression(expr
->left
))
2946 if (!evaluate_expression(expr
->right
))
2948 return evaluate_binop(expr
);
2950 return evaluate_logical(expr
);
2952 evaluate_expression(expr
->left
);
2953 if (!evaluate_expression(expr
->right
))
2955 return evaluate_comma(expr
);
2957 if (!evaluate_expression(expr
->left
))
2959 if (!evaluate_expression(expr
->right
))
2961 return evaluate_compare(expr
);
2962 case EXPR_ASSIGNMENT
:
2963 if (!evaluate_expression(expr
->left
))
2965 if (!evaluate_expression(expr
->right
))
2967 return evaluate_assignment(expr
);
2969 if (!evaluate_expression(expr
->unop
))
2971 return evaluate_preop(expr
);
2973 if (!evaluate_expression(expr
->unop
))
2975 return evaluate_postop(expr
);
2977 case EXPR_FORCE_CAST
:
2978 case EXPR_IMPLIED_CAST
:
2979 return evaluate_cast(expr
);
2981 return evaluate_sizeof(expr
);
2982 case EXPR_PTRSIZEOF
:
2983 return evaluate_ptrsizeof(expr
);
2985 return evaluate_alignof(expr
);
2987 return evaluate_member_dereference(expr
);
2989 return evaluate_call(expr
);
2991 case EXPR_CONDITIONAL
:
2992 return evaluate_conditional_expression(expr
);
2993 case EXPR_STATEMENT
:
2994 expr
->ctype
= evaluate_statement(expr
->statement
);
2998 expr
->ctype
= &ptr_ctype
;
3002 /* Evaluate the type of the symbol .. */
3003 evaluate_symbol(expr
->symbol
);
3004 /* .. but the type of the _expression_ is a "type" */
3005 expr
->ctype
= &type_ctype
;
3009 return evaluate_offsetof(expr
);
3011 /* These can not exist as stand-alone expressions */
3012 case EXPR_INITIALIZER
:
3013 case EXPR_IDENTIFIER
:
3016 expression_error(expr
, "internal front-end error: initializer in expression");
3019 expression_error(expr
, "internal front-end error: SLICE re-evaluated");
3025 static void check_duplicates(struct symbol
*sym
)
3028 struct symbol
*next
= sym
;
3030 while ((next
= next
->same_symbol
) != NULL
) {
3031 const char *typediff
;
3032 evaluate_symbol(next
);
3034 typediff
= type_difference(&sym
->ctype
, &next
->ctype
, 0, 0);
3036 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
3037 show_ident(sym
->ident
),
3038 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
3043 unsigned long mod
= sym
->ctype
.modifiers
;
3044 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
3046 if (!(mod
& MOD_TOPLEVEL
))
3050 if (sym
->ident
== &main_ident
)
3052 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
3056 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
3058 struct symbol
*base_type
;
3066 sym
= examine_symbol_type(sym
);
3067 base_type
= get_base_type(sym
);
3071 /* Evaluate the initializers */
3072 if (sym
->initializer
)
3073 evaluate_initializer(sym
, &sym
->initializer
);
3075 /* And finally, evaluate the body of the symbol too */
3076 if (base_type
->type
== SYM_FN
) {
3077 struct symbol
*curr
= current_fn
;
3079 current_fn
= base_type
;
3081 examine_fn_arguments(base_type
);
3082 if (!base_type
->stmt
&& base_type
->inline_stmt
)
3084 if (base_type
->stmt
)
3085 evaluate_statement(base_type
->stmt
);
3093 void evaluate_symbol_list(struct symbol_list
*list
)
3097 FOR_EACH_PTR(list
, sym
) {
3098 evaluate_symbol(sym
);
3099 check_duplicates(sym
);
3100 } END_FOR_EACH_PTR(sym
);
3103 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
3105 struct expression
*expr
= stmt
->expression
;
3106 struct symbol
*fntype
;
3108 evaluate_expression(expr
);
3109 fntype
= current_fn
->ctype
.base_type
;
3110 if (!fntype
|| fntype
== &void_ctype
) {
3111 if (expr
&& expr
->ctype
!= &void_ctype
)
3112 expression_error(expr
, "return expression in %s function", fntype
?"void":"typeless");
3113 if (expr
&& Wreturn_void
)
3114 warning(stmt
->pos
, "returning void-valued expression");
3119 sparse_error(stmt
->pos
, "return with no return value");
3124 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, "return expression");
3128 static void evaluate_if_statement(struct statement
*stmt
)
3130 if (!stmt
->if_conditional
)
3133 evaluate_conditional(stmt
->if_conditional
, 0);
3134 evaluate_statement(stmt
->if_true
);
3135 evaluate_statement(stmt
->if_false
);
3138 static void evaluate_iterator(struct statement
*stmt
)
3140 evaluate_symbol_list(stmt
->iterator_syms
);
3141 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
3142 evaluate_conditional(stmt
->iterator_post_condition
,1);
3143 evaluate_statement(stmt
->iterator_pre_statement
);
3144 evaluate_statement(stmt
->iterator_statement
);
3145 evaluate_statement(stmt
->iterator_post_statement
);
3148 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
3150 switch (*constraint
) {
3151 case '=': /* Assignment */
3152 case '+': /* Update */
3155 expression_error(expr
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
3159 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
3161 switch (*constraint
) {
3162 case '=': /* Assignment */
3163 case '+': /* Update */
3164 expression_error(expr
, "input constraint with assignment (\"%s\")", constraint
);
3168 static void evaluate_asm_statement(struct statement
*stmt
)
3170 struct expression
*expr
;
3173 expr
= stmt
->asm_string
;
3174 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3175 sparse_error(stmt
->pos
, "need constant string for inline asm");
3180 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
3181 struct ident
*ident
;
3184 case 0: /* Identifier */
3186 ident
= (struct ident
*)expr
;
3189 case 1: /* Constraint */
3191 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3192 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
3193 *THIS_ADDRESS(expr
) = NULL
;
3196 verify_output_constraint(expr
, expr
->string
->data
);
3199 case 2: /* Expression */
3201 if (!evaluate_expression(expr
))
3203 if (!lvalue_expression(expr
))
3204 warning(expr
->pos
, "asm output is not an lvalue");
3205 evaluate_assign_to(expr
, expr
->ctype
);
3208 } END_FOR_EACH_PTR(expr
);
3211 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
3212 struct ident
*ident
;
3215 case 0: /* Identifier */
3217 ident
= (struct ident
*)expr
;
3220 case 1: /* Constraint */
3222 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3223 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
3224 *THIS_ADDRESS(expr
) = NULL
;
3227 verify_input_constraint(expr
, expr
->string
->data
);
3230 case 2: /* Expression */
3232 if (!evaluate_expression(expr
))
3236 } END_FOR_EACH_PTR(expr
);
3238 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
3240 sparse_error(stmt
->pos
, "bad asm output");
3243 if (expr
->type
== EXPR_STRING
)
3245 expression_error(expr
, "asm clobber is not a string");
3246 } END_FOR_EACH_PTR(expr
);
3249 static void evaluate_case_statement(struct statement
*stmt
)
3251 evaluate_expression(stmt
->case_expression
);
3252 evaluate_expression(stmt
->case_to
);
3253 evaluate_statement(stmt
->case_statement
);
3256 static void check_case_type(struct expression
*switch_expr
,
3257 struct expression
*case_expr
,
3258 struct expression
**enumcase
)
3260 struct symbol
*switch_type
, *case_type
;
3266 switch_type
= switch_expr
->ctype
;
3267 case_type
= evaluate_expression(case_expr
);
3269 if (!switch_type
|| !case_type
)
3273 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
3274 else if (is_enum_type(case_type
))
3275 *enumcase
= case_expr
;
3278 sclass
= classify_type(switch_type
, &switch_type
);
3279 cclass
= classify_type(case_type
, &case_type
);
3281 /* both should be arithmetic */
3282 if (!(sclass
& cclass
& TYPE_NUM
))
3285 /* neither should be floating */
3286 if ((sclass
| cclass
) & TYPE_FLOAT
)
3289 /* if neither is restricted, we are OK */
3290 if (!((sclass
| cclass
) & TYPE_RESTRICT
))
3293 if (!restricted_binop_type(SPECIAL_EQUAL
, case_expr
, switch_expr
,
3294 cclass
, sclass
, case_type
, switch_type
)) {
3295 unrestrict(case_expr
, cclass
, &case_type
);
3296 unrestrict(switch_expr
, sclass
, &switch_type
);
3301 expression_error(case_expr
, "incompatible types for 'case' statement");
3304 static void evaluate_switch_statement(struct statement
*stmt
)
3307 struct expression
*enumcase
= NULL
;
3308 struct expression
**enumcase_holder
= &enumcase
;
3309 struct expression
*sel
= stmt
->switch_expression
;
3311 evaluate_expression(sel
);
3312 evaluate_statement(stmt
->switch_statement
);
3315 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
3316 enumcase_holder
= NULL
; /* Only check cases against switch */
3318 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
3319 struct statement
*case_stmt
= sym
->stmt
;
3320 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
3321 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
3322 } END_FOR_EACH_PTR(sym
);
3325 struct symbol
*evaluate_statement(struct statement
*stmt
)
3330 switch (stmt
->type
) {
3331 case STMT_DECLARATION
: {
3333 FOR_EACH_PTR(stmt
->declaration
, s
) {
3335 } END_FOR_EACH_PTR(s
);
3340 return evaluate_return_expression(stmt
);
3342 case STMT_EXPRESSION
:
3343 if (!evaluate_expression(stmt
->expression
))
3345 if (stmt
->expression
->ctype
== &null_ctype
)
3346 stmt
->expression
= cast_to(stmt
->expression
, &ptr_ctype
);
3347 return degenerate(stmt
->expression
);
3349 case STMT_COMPOUND
: {
3350 struct statement
*s
;
3351 struct symbol
*type
= NULL
;
3353 /* Evaluate the return symbol in the compound statement */
3354 evaluate_symbol(stmt
->ret
);
3357 * Then, evaluate each statement, making the type of the
3358 * compound statement be the type of the last statement
3360 type
= evaluate_statement(stmt
->args
);
3361 FOR_EACH_PTR(stmt
->stmts
, s
) {
3362 type
= evaluate_statement(s
);
3363 } END_FOR_EACH_PTR(s
);
3369 evaluate_if_statement(stmt
);
3372 evaluate_iterator(stmt
);
3375 evaluate_switch_statement(stmt
);
3378 evaluate_case_statement(stmt
);
3381 return evaluate_statement(stmt
->label_statement
);
3383 evaluate_expression(stmt
->goto_expression
);
3388 evaluate_asm_statement(stmt
);
3391 evaluate_expression(stmt
->expression
);
3394 evaluate_expression(stmt
->range_expression
);
3395 evaluate_expression(stmt
->range_low
);
3396 evaluate_expression(stmt
->range_high
);