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
.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
);
343 if (type
->type
== SYM_NODE
)
344 type
= type
->ctype
.base_type
;
346 if (type
->type
== SYM_ENUM
)
347 type
= type
->ctype
.base_type
;
349 if (type
->type
== SYM_BASETYPE
) {
350 if (type
->ctype
.base_type
== &int_type
)
352 if (type
->ctype
.base_type
== &fp_type
)
353 return TYPE_NUM
| TYPE_FLOAT
;
355 return type_class
[type
->type
];
358 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
360 static inline int is_string_type(struct symbol
*type
)
362 if (type
->type
== SYM_NODE
)
363 type
= type
->ctype
.base_type
;
364 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
367 static struct symbol
*bad_expr_type(struct expression
*expr
)
369 sparse_error(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
370 switch (expr
->type
) {
373 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
374 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
378 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
385 return expr
->ctype
= &bad_ctype
;
388 static int restricted_value(struct expression
*v
, struct symbol
*type
)
390 if (v
->type
!= EXPR_VALUE
)
397 static int restricted_binop(int op
, struct symbol
*type
)
402 case SPECIAL_AND_ASSIGN
:
403 case SPECIAL_OR_ASSIGN
:
404 case SPECIAL_XOR_ASSIGN
:
405 return 1; /* unfoul */
409 return 2; /* keep fouled */
411 case SPECIAL_NOTEQUAL
:
412 return 3; /* warn if fouled */
418 static int restricted_unop(int op
, struct symbol
**type
)
421 if ((*type
)->bit_size
< bits_in_int
)
422 *type
= befoul(*type
);
429 /* type should be SYM_FOULED */
430 static inline struct symbol
*unfoul(struct symbol
*type
)
432 return type
->ctype
.base_type
;
435 static struct symbol
*restricted_binop_type(int op
,
436 struct expression
*left
,
437 struct expression
*right
,
438 int lclass
, int rclass
,
439 struct symbol
*ltype
,
440 struct symbol
*rtype
)
442 struct symbol
*ctype
= NULL
;
443 if (lclass
& TYPE_RESTRICT
) {
444 if (rclass
& TYPE_RESTRICT
) {
445 if (ltype
== rtype
) {
447 } else if (lclass
& TYPE_FOULED
) {
448 if (unfoul(ltype
) == rtype
)
450 } else if (rclass
& TYPE_FOULED
) {
451 if (unfoul(rtype
) == ltype
)
455 if (!restricted_value(right
, ltype
))
458 } else if (!restricted_value(left
, rtype
))
462 switch (restricted_binop(op
, ctype
)) {
464 if ((lclass
^ rclass
) & TYPE_FOULED
)
465 ctype
= unfoul(ctype
);
468 if (!(lclass
& rclass
& TYPE_FOULED
))
480 static inline void unrestrict(struct expression
*expr
,
481 int class, struct symbol
**ctype
)
483 if (class & TYPE_RESTRICT
) {
484 if (class & TYPE_FOULED
)
485 *ctype
= unfoul(*ctype
);
486 warning(expr
->pos
, "%s degrades to integer",
487 show_typename(*ctype
));
488 *ctype
= (*ctype
)->ctype
.base_type
; /* get to arithmetic type */
492 static struct symbol
*usual_conversions(int op
,
493 struct expression
*left
,
494 struct expression
*right
,
495 int lclass
, int rclass
,
496 struct symbol
*ltype
,
497 struct symbol
*rtype
)
499 struct symbol
*ctype
;
501 warn_for_different_enum_types(right
->pos
, left
->ctype
, right
->ctype
);
503 if ((lclass
| rclass
) & TYPE_RESTRICT
)
507 if (!(lclass
& TYPE_FLOAT
)) {
508 if (!(rclass
& TYPE_FLOAT
))
509 return bigger_int_type(ltype
, rtype
);
512 } else if (rclass
& TYPE_FLOAT
) {
513 unsigned long lmod
= ltype
->ctype
.modifiers
;
514 unsigned long rmod
= rtype
->ctype
.modifiers
;
515 if (rmod
& ~lmod
& (MOD_LONG_ALL
))
523 ctype
= restricted_binop_type(op
, left
, right
,
524 lclass
, rclass
, ltype
, rtype
);
528 unrestrict(left
, lclass
, <ype
);
529 unrestrict(right
, rclass
, &rtype
);
534 static inline int lvalue_expression(struct expression
*expr
)
536 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
539 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*itype
)
541 struct expression
*index
= expr
->right
;
542 struct symbol
*ctype
, *base
;
545 classify_type(degenerate(expr
->left
), &ctype
);
546 base
= examine_pointer_target(ctype
);
549 expression_error(expr
, "missing type information");
552 if (is_function(base
)) {
553 expression_error(expr
, "arithmetics on pointers to functions");
557 /* Get the size of whatever the pointer points to */
558 multiply
= is_void_type(base
) ? 1 : bits_to_bytes(base
->bit_size
);
560 if (ctype
== &null_ctype
)
564 if (multiply
== 1 && itype
->bit_size
>= bits_in_pointer
)
567 if (index
->type
== EXPR_VALUE
) {
568 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
569 unsigned long long v
= index
->value
, mask
;
570 mask
= 1ULL << (itype
->bit_size
- 1);
576 mask
= 1ULL << (bits_in_pointer
- 1);
577 v
&= mask
| (mask
- 1);
579 val
->ctype
= ssize_t_ctype
;
584 if (itype
->bit_size
< bits_in_pointer
)
585 index
= cast_to(index
, ssize_t_ctype
);
588 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
589 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
591 val
->ctype
= ssize_t_ctype
;
592 val
->value
= multiply
;
595 mul
->ctype
= ssize_t_ctype
;
605 static void examine_fn_arguments(struct symbol
*fn
);
607 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
609 const char *type_difference(struct ctype
*c1
, struct ctype
*c2
,
610 unsigned long mod1
, unsigned long mod2
)
612 unsigned long as1
= c1
->as
, as2
= c2
->as
;
613 struct symbol
*t1
= c1
->base_type
;
614 struct symbol
*t2
= c2
->base_type
;
615 int move1
= 1, move2
= 1;
616 mod1
|= c1
->modifiers
;
617 mod2
|= c2
->modifiers
;
621 struct symbol
*base1
= t1
->ctype
.base_type
;
622 struct symbol
*base2
= t2
->ctype
.base_type
;
625 * FIXME! Collect alignment and context too here!
628 if (t1
&& t1
->type
!= SYM_PTR
) {
629 mod1
|= t1
->ctype
.modifiers
;
636 if (t2
&& t2
->type
!= SYM_PTR
) {
637 mod2
|= t2
->ctype
.modifiers
;
646 return "different types";
648 if (t1
->type
== SYM_NODE
|| t1
->type
== SYM_ENUM
) {
656 if (t2
->type
== SYM_NODE
|| t2
->type
== SYM_ENUM
) {
666 if (type
!= t2
->type
)
667 return "different base types";
671 sparse_error(t1
->pos
,
672 "internal error: bad type in derived(%d)",
676 return "different base types";
679 /* allow definition of incomplete structs and unions */
680 if (t1
->ident
== t2
->ident
)
682 return "different base types";
684 /* XXX: we ought to compare sizes */
688 return "different address spaces";
689 /* MOD_SPECIFIER is due to idiocy in parse.c */
690 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SPECIFIER
)
691 return "different modifiers";
692 /* we could be lazier here */
693 base1
= examine_pointer_target(t1
);
694 base2
= examine_pointer_target(t2
);
695 mod1
= t1
->ctype
.modifiers
;
697 mod2
= t2
->ctype
.modifiers
;
701 struct symbol
*arg1
, *arg2
;
705 return "different address spaces";
706 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
707 return "different modifiers";
708 mod1
= t1
->ctype
.modifiers
;
710 mod2
= t2
->ctype
.modifiers
;
713 if (base1
->variadic
!= base2
->variadic
)
714 return "incompatible variadic arguments";
715 examine_fn_arguments(t1
);
716 examine_fn_arguments(t2
);
717 PREPARE_PTR_LIST(t1
->arguments
, arg1
);
718 PREPARE_PTR_LIST(t2
->arguments
, arg2
);
725 return "different argument counts";
726 diffstr
= type_difference(&arg1
->ctype
,
730 static char argdiff
[80];
731 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diffstr
);
738 FINISH_PTR_LIST(arg2
);
739 FINISH_PTR_LIST(arg1
);
744 return "different address spaces";
746 return "different base types";
747 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
751 return "different type sizes";
752 else if (diff
& ~MOD_SIGNEDNESS
)
753 return "different modifiers";
755 return "different signedness";
761 return "different address spaces";
762 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
763 return "different modifiers";
767 static void bad_null(struct expression
*expr
)
769 if (Wnon_pointer_null
)
770 warning(expr
->pos
, "Using plain integer as NULL pointer");
773 static unsigned long target_qualifiers(struct symbol
*type
)
775 unsigned long mod
= type
->ctype
.modifiers
& MOD_IGN
;
776 if (type
->ctype
.base_type
&& type
->ctype
.base_type
->type
== SYM_ARRAY
)
781 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
)
783 const char *typediff
;
784 struct symbol
*ltype
, *rtype
;
785 struct expression
*l
= expr
->left
;
786 struct expression
*r
= expr
->right
;
787 struct symbol
*lbase
, *rbase
;
789 classify_type(degenerate(l
), <ype
);
790 classify_type(degenerate(r
), &rtype
);
792 lbase
= examine_pointer_target(ltype
);
793 rbase
= examine_pointer_target(rtype
);
794 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
795 target_qualifiers(rtype
),
796 target_qualifiers(ltype
));
798 expression_error(expr
, "subtraction of different types can't work (%s)", typediff
);
800 if (is_function(lbase
)) {
801 expression_error(expr
, "subtraction of functions? Share your drugs");
805 expr
->ctype
= ssize_t_ctype
;
806 if (lbase
->bit_size
> bits_in_char
) {
807 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
808 struct expression
*div
= expr
;
809 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
810 unsigned long value
= bits_to_bytes(lbase
->bit_size
);
812 val
->ctype
= size_t_ctype
;
815 if (value
& (value
-1)) {
816 if (Wptr_subtraction_blows
)
817 warning(expr
->pos
, "potentially expensive pointer subtraction");
821 sub
->ctype
= ssize_t_ctype
;
830 return ssize_t_ctype
;
833 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
835 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
837 struct symbol
*ctype
;
842 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
843 warning(expr
->pos
, "assignment expression in conditional");
845 ctype
= evaluate_expression(expr
);
847 if (is_safe_type(ctype
))
848 warning(expr
->pos
, "testing a 'safe expression'");
854 static struct symbol
*evaluate_logical(struct expression
*expr
)
856 if (!evaluate_conditional(expr
->left
, 0))
858 if (!evaluate_conditional(expr
->right
, 0))
861 expr
->ctype
= &bool_ctype
;
863 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
869 static struct symbol
*evaluate_binop(struct expression
*expr
)
871 struct symbol
*ltype
, *rtype
, *ctype
;
872 int lclass
= classify_type(expr
->left
->ctype
, <ype
);
873 int rclass
= classify_type(expr
->right
->ctype
, &rtype
);
877 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
881 /* number op number */
882 if (lclass
& rclass
& TYPE_NUM
) {
883 if ((lclass
| rclass
) & TYPE_FLOAT
) {
885 case '+': case '-': case '*': case '/':
888 return bad_expr_type(expr
);
892 if (op
== SPECIAL_LEFTSHIFT
|| op
== SPECIAL_RIGHTSHIFT
) {
893 // shifts do integer promotions, but that's it.
894 unrestrict(expr
->left
, lclass
, <ype
);
895 unrestrict(expr
->right
, rclass
, &rtype
);
896 ctype
= ltype
= integer_promotion(ltype
);
897 rtype
= integer_promotion(rtype
);
899 // The rest do usual conversions
900 const unsigned left_not
= expr
->left
->type
== EXPR_PREOP
901 && expr
->left
->op
== '!';
902 const unsigned right_not
= expr
->right
->type
== EXPR_PREOP
903 && expr
->right
->op
== '!';
904 if ((op
== '&' || op
== '|') && (left_not
|| right_not
))
905 warning(expr
->pos
, "dubious: %sx %c %sy",
908 right_not
? "!" : "");
910 ltype
= usual_conversions(op
, expr
->left
, expr
->right
,
911 lclass
, rclass
, ltype
, rtype
);
912 ctype
= rtype
= ltype
;
915 expr
->left
= cast_to(expr
->left
, ltype
);
916 expr
->right
= cast_to(expr
->right
, rtype
);
921 /* pointer (+|-) integer */
922 if (lclass
& TYPE_PTR
&& is_int(rclass
) && (op
== '+' || op
== '-')) {
923 unrestrict(expr
->right
, rclass
, &rtype
);
924 return evaluate_ptr_add(expr
, rtype
);
927 /* integer + pointer */
928 if (rclass
& TYPE_PTR
&& is_int(lclass
) && op
== '+') {
929 struct expression
*index
= expr
->left
;
930 unrestrict(index
, lclass
, <ype
);
931 expr
->left
= expr
->right
;
933 return evaluate_ptr_add(expr
, ltype
);
936 /* pointer - pointer */
937 if (lclass
& rclass
& TYPE_PTR
&& expr
->op
== '-')
938 return evaluate_ptr_sub(expr
);
940 return bad_expr_type(expr
);
943 static struct symbol
*evaluate_comma(struct expression
*expr
)
945 expr
->ctype
= degenerate(expr
->right
);
946 if (expr
->ctype
== &null_ctype
)
947 expr
->ctype
= &ptr_ctype
;
948 expr
->flags
&= expr
->left
->flags
& expr
->right
->flags
;
952 static int modify_for_unsigned(int op
)
955 op
= SPECIAL_UNSIGNED_LT
;
957 op
= SPECIAL_UNSIGNED_GT
;
958 else if (op
== SPECIAL_LTE
)
959 op
= SPECIAL_UNSIGNED_LTE
;
960 else if (op
== SPECIAL_GTE
)
961 op
= SPECIAL_UNSIGNED_GTE
;
965 static inline int is_null_pointer_constant(struct expression
*e
)
967 if (e
->ctype
== &null_ctype
)
969 if (!(e
->flags
& Int_const_expr
))
971 return is_zero_constant(e
) ? 2 : 0;
974 static struct symbol
*evaluate_compare(struct expression
*expr
)
976 struct expression
*left
= expr
->left
, *right
= expr
->right
;
977 struct symbol
*ltype
, *rtype
, *lbase
, *rbase
;
978 int lclass
= classify_type(degenerate(left
), <ype
);
979 int rclass
= classify_type(degenerate(right
), &rtype
);
980 struct symbol
*ctype
;
981 const char *typediff
;
984 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
989 if (is_type_type(ltype
) && is_type_type(rtype
))
992 if (is_safe_type(left
->ctype
) || is_safe_type(right
->ctype
))
993 warning(expr
->pos
, "testing a 'safe expression'");
995 /* number on number */
996 if (lclass
& rclass
& TYPE_NUM
) {
997 ctype
= usual_conversions(expr
->op
, expr
->left
, expr
->right
,
998 lclass
, rclass
, ltype
, rtype
);
999 expr
->left
= cast_to(expr
->left
, ctype
);
1000 expr
->right
= cast_to(expr
->right
, ctype
);
1001 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
1002 expr
->op
= modify_for_unsigned(expr
->op
);
1006 /* at least one must be a pointer */
1007 if (!((lclass
| rclass
) & TYPE_PTR
))
1008 return bad_expr_type(expr
);
1010 /* equality comparisons can be with null pointer constants */
1011 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1012 int is_null1
= is_null_pointer_constant(left
);
1013 int is_null2
= is_null_pointer_constant(right
);
1018 if (is_null1
&& is_null2
) {
1019 int positive
= expr
->op
== SPECIAL_EQUAL
;
1020 expr
->type
= EXPR_VALUE
;
1021 expr
->value
= positive
;
1024 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1025 left
= cast_to(left
, rtype
);
1028 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1029 right
= cast_to(right
, ltype
);
1033 /* both should be pointers */
1034 if (!(lclass
& rclass
& TYPE_PTR
))
1035 return bad_expr_type(expr
);
1036 expr
->op
= modify_for_unsigned(expr
->op
);
1038 lbase
= examine_pointer_target(ltype
);
1039 rbase
= examine_pointer_target(rtype
);
1041 /* they also have special treatment for pointers to void */
1042 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1043 if (ltype
->ctype
.as
== rtype
->ctype
.as
) {
1044 if (lbase
== &void_ctype
) {
1045 right
= cast_to(right
, ltype
);
1048 if (rbase
== &void_ctype
) {
1049 left
= cast_to(left
, rtype
);
1055 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1056 target_qualifiers(rtype
),
1057 target_qualifiers(ltype
));
1061 expression_error(expr
, "incompatible types in comparison expression (%s)", typediff
);
1065 expr
->ctype
= &bool_ctype
;
1070 * NOTE! The degenerate case of "x ? : y", where we don't
1071 * have a true case, this will possibly promote "x" to the
1072 * same type as "y", and thus _change_ the conditional
1073 * test in the expression. But since promotion is "safe"
1074 * for testing, that's OK.
1076 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
1078 struct expression
**true;
1079 struct symbol
*ctype
, *ltype
, *rtype
, *lbase
, *rbase
;
1081 const char * typediff
;
1084 if (!evaluate_conditional(expr
->conditional
, 0))
1086 if (!evaluate_expression(expr
->cond_false
))
1089 ctype
= degenerate(expr
->conditional
);
1090 rtype
= degenerate(expr
->cond_false
);
1092 true = &expr
->conditional
;
1094 if (expr
->cond_true
) {
1095 if (!evaluate_expression(expr
->cond_true
))
1097 ltype
= degenerate(expr
->cond_true
);
1098 true = &expr
->cond_true
;
1102 int flags
= expr
->conditional
->flags
& Int_const_expr
;
1103 flags
&= (*true)->flags
& expr
->cond_false
->flags
;
1108 lclass
= classify_type(ltype
, <ype
);
1109 rclass
= classify_type(rtype
, &rtype
);
1110 if (lclass
& rclass
& TYPE_NUM
) {
1111 ctype
= usual_conversions('?', *true, expr
->cond_false
,
1112 lclass
, rclass
, ltype
, rtype
);
1113 *true = cast_to(*true, ctype
);
1114 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1118 if ((lclass
| rclass
) & TYPE_PTR
) {
1119 int is_null1
= is_null_pointer_constant(*true);
1120 int is_null2
= is_null_pointer_constant(expr
->cond_false
);
1122 if (is_null1
&& is_null2
) {
1123 *true = cast_to(*true, &ptr_ctype
);
1124 expr
->cond_false
= cast_to(expr
->cond_false
, &ptr_ctype
);
1128 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1131 *true = cast_to(*true, rtype
);
1135 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1137 bad_null(expr
->cond_false
);
1138 expr
->cond_false
= cast_to(expr
->cond_false
, ltype
);
1142 if (!(lclass
& rclass
& TYPE_PTR
)) {
1143 typediff
= "different types";
1146 /* OK, it's pointer on pointer */
1147 if (ltype
->ctype
.as
!= rtype
->ctype
.as
) {
1148 typediff
= "different address spaces";
1152 /* need to be lazier here */
1153 lbase
= examine_pointer_target(ltype
);
1154 rbase
= examine_pointer_target(rtype
);
1155 qual
= target_qualifiers(ltype
) | target_qualifiers(rtype
);
1157 if (lbase
== &void_ctype
) {
1158 /* XXX: pointers to function should warn here */
1163 if (rbase
== &void_ctype
) {
1164 /* XXX: pointers to function should warn here */
1168 /* XXX: that should be pointer to composite */
1170 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1177 /* void on void, struct on same struct, union on same union */
1178 if (ltype
== rtype
) {
1182 typediff
= "different base types";
1185 expression_error(expr
, "incompatible types in conditional expression (%s)", typediff
);
1189 expr
->ctype
= ctype
;
1193 if (qual
& ~ctype
->ctype
.modifiers
) {
1194 struct symbol
*sym
= alloc_symbol(ctype
->pos
, SYM_PTR
);
1196 sym
->ctype
.modifiers
|= qual
;
1199 *true = cast_to(*true, ctype
);
1200 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1204 /* FP assignments can not do modulo or bit operations */
1205 static int compatible_float_op(int op
)
1207 return op
== SPECIAL_ADD_ASSIGN
||
1208 op
== SPECIAL_SUB_ASSIGN
||
1209 op
== SPECIAL_MUL_ASSIGN
||
1210 op
== SPECIAL_DIV_ASSIGN
;
1213 static int evaluate_assign_op(struct expression
*expr
)
1215 struct symbol
*target
= expr
->left
->ctype
;
1216 struct symbol
*source
= expr
->right
->ctype
;
1217 struct symbol
*t
, *s
;
1218 int tclass
= classify_type(target
, &t
);
1219 int sclass
= classify_type(source
, &s
);
1222 if (tclass
& sclass
& TYPE_NUM
) {
1223 if (tclass
& TYPE_FLOAT
&& !compatible_float_op(op
)) {
1224 expression_error(expr
, "invalid assignment");
1227 if (tclass
& TYPE_RESTRICT
) {
1228 if (!restricted_binop(op
, t
)) {
1229 warning(expr
->pos
, "bad assignment (%s) to %s",
1230 show_special(op
), show_typename(t
));
1231 expr
->right
= cast_to(expr
->right
, target
);
1234 /* allowed assignments unfoul */
1235 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1237 if (!restricted_value(expr
->right
, t
))
1239 } else if (!(sclass
& TYPE_RESTRICT
))
1241 /* source and target would better be identical restricted */
1244 warning(expr
->pos
, "invalid assignment: %s", show_special(op
));
1245 info(expr
->pos
, " left side has type %s", show_typename(t
));
1246 info(expr
->pos
, " right side has type %s", show_typename(s
));
1247 expr
->right
= cast_to(expr
->right
, target
);
1250 if (tclass
== TYPE_PTR
&& is_int(sclass
)) {
1251 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1252 unrestrict(expr
->right
, sclass
, &s
);
1253 evaluate_ptr_add(expr
, s
);
1256 expression_error(expr
, "invalid pointer assignment");
1260 expression_error(expr
, "invalid assignment");
1264 expr
->right
= cast_to(expr
->right
, target
);
1268 static int whitelist_pointers(struct symbol
*t1
, struct symbol
*t2
)
1271 return 0; /* yes, 0 - we don't want a cast_to here */
1272 if (t1
== &void_ctype
)
1274 if (t2
== &void_ctype
)
1276 if (classify_type(t1
, &t1
) != TYPE_NUM
)
1278 if (classify_type(t2
, &t2
) != TYPE_NUM
)
1282 if (t1
->ctype
.modifiers
& t2
->ctype
.modifiers
& MOD_CHAR
)
1284 if ((t1
->ctype
.modifiers
^ t2
->ctype
.modifiers
) & MOD_SIZE
)
1289 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1290 struct expression
**rp
, const char *where
)
1292 const char *typediff
;
1293 struct symbol
*source
= degenerate(*rp
);
1294 struct symbol
*t
, *s
;
1295 int tclass
= classify_type(target
, &t
);
1296 int sclass
= classify_type(source
, &s
);
1298 if (tclass
& sclass
& TYPE_NUM
) {
1299 if (tclass
& TYPE_RESTRICT
) {
1300 /* allowed assignments unfoul */
1301 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1303 if (!restricted_value(*rp
, target
))
1307 } else if (!(sclass
& TYPE_RESTRICT
))
1309 typediff
= "different base types";
1313 if (tclass
== TYPE_PTR
) {
1314 unsigned long mod1
, mod2
;
1315 struct symbol
*b1
, *b2
;
1316 // NULL pointer is always OK
1317 int is_null
= is_null_pointer_constant(*rp
);
1323 if (!(sclass
& TYPE_PTR
)) {
1324 typediff
= "different base types";
1327 b1
= examine_pointer_target(t
);
1328 b2
= examine_pointer_target(s
);
1329 mod1
= target_qualifiers(t
);
1330 mod2
= target_qualifiers(s
);
1331 if (whitelist_pointers(b1
, b2
)) {
1333 * assignments to/from void * are OK, provided that
1334 * we do not remove qualifiers from pointed to [C]
1335 * or mix address spaces [sparse].
1337 if (t
->ctype
.as
!= s
->ctype
.as
) {
1338 typediff
= "different address spaces";
1342 typediff
= "different modifiers";
1347 /* It's OK if the target is more volatile or const than the source */
1348 typediff
= type_difference(&t
->ctype
, &s
->ctype
, 0, mod1
);
1354 if ((tclass
& TYPE_COMPOUND
) && s
== t
)
1357 if (tclass
& TYPE_NUM
) {
1358 /* XXX: need to turn into comparison with NULL */
1359 if (t
== &bool_ctype
&& (sclass
& TYPE_PTR
))
1361 typediff
= "different base types";
1364 typediff
= "invalid types";
1367 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1368 info(expr
->pos
, " expected %s", show_typename(target
));
1369 info(expr
->pos
, " got %s", show_typename(source
));
1370 *rp
= cast_to(*rp
, target
);
1373 *rp
= cast_to(*rp
, target
);
1377 static void mark_assigned(struct expression
*expr
)
1383 switch (expr
->type
) {
1388 if (sym
->type
!= SYM_NODE
)
1390 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1394 mark_assigned(expr
->left
);
1395 mark_assigned(expr
->right
);
1398 case EXPR_FORCE_CAST
:
1399 mark_assigned(expr
->cast_expression
);
1402 mark_assigned(expr
->base
);
1410 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1412 if (type
->ctype
.modifiers
& MOD_CONST
)
1413 expression_error(left
, "assignment to const expression");
1415 /* We know left is an lvalue, so it's a "preop-*" */
1416 mark_assigned(left
->unop
);
1419 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1421 struct expression
*left
= expr
->left
;
1422 struct expression
*where
= expr
;
1423 struct symbol
*ltype
;
1425 if (!lvalue_expression(left
)) {
1426 expression_error(expr
, "not an lvalue");
1430 ltype
= left
->ctype
;
1432 if (expr
->op
!= '=') {
1433 if (!evaluate_assign_op(expr
))
1436 if (!compatible_assignment_types(where
, ltype
, &expr
->right
, "assignment"))
1440 evaluate_assign_to(left
, ltype
);
1442 expr
->ctype
= ltype
;
1446 static void examine_fn_arguments(struct symbol
*fn
)
1450 FOR_EACH_PTR(fn
->arguments
, s
) {
1451 struct symbol
*arg
= evaluate_symbol(s
);
1452 /* Array/function arguments silently degenerate into pointers */
1458 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1459 if (arg
->type
== SYM_ARRAY
)
1460 ptr
->ctype
= arg
->ctype
;
1462 ptr
->ctype
.base_type
= arg
;
1463 ptr
->ctype
.as
|= s
->ctype
.as
;
1464 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
& MOD_PTRINHERIT
;
1466 s
->ctype
.base_type
= ptr
;
1468 s
->ctype
.modifiers
&= ~MOD_PTRINHERIT
;
1471 examine_symbol_type(s
);
1478 } END_FOR_EACH_PTR(s
);
1481 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1483 /* Take the modifiers of the pointer, and apply them to the member */
1484 mod
|= sym
->ctype
.modifiers
;
1485 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1486 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1488 newsym
->ctype
.as
= as
;
1489 newsym
->ctype
.modifiers
= mod
;
1495 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1497 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1498 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1500 node
->ctype
.base_type
= ptr
;
1501 ptr
->bit_size
= bits_in_pointer
;
1502 ptr
->ctype
.alignment
= pointer_alignment
;
1504 node
->bit_size
= bits_in_pointer
;
1505 node
->ctype
.alignment
= pointer_alignment
;
1508 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1509 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1510 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1512 if (sym
->type
== SYM_NODE
) {
1513 ptr
->ctype
.as
|= sym
->ctype
.as
;
1514 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1515 sym
= sym
->ctype
.base_type
;
1517 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1518 ptr
->ctype
.as
|= sym
->ctype
.as
;
1519 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1520 sym
= sym
->ctype
.base_type
;
1522 ptr
->ctype
.base_type
= sym
;
1527 /* Arrays degenerate into pointers on pointer arithmetic */
1528 static struct symbol
*degenerate(struct expression
*expr
)
1530 struct symbol
*ctype
, *base
;
1534 ctype
= expr
->ctype
;
1537 base
= examine_symbol_type(ctype
);
1538 if (ctype
->type
== SYM_NODE
)
1539 base
= ctype
->ctype
.base_type
;
1541 * Arrays degenerate into pointers to the entries, while
1542 * functions degenerate into pointers to themselves.
1543 * If array was part of non-lvalue compound, we create a copy
1544 * of that compound first and then act as if we were dealing with
1545 * the corresponding field in there.
1547 switch (base
->type
) {
1549 if (expr
->type
== EXPR_SLICE
) {
1550 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1551 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1553 a
->ctype
.base_type
= expr
->base
->ctype
;
1554 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1555 a
->array_size
= expr
->base
->ctype
->array_size
;
1557 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1559 e0
->ctype
= &lazy_ptr_ctype
;
1561 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1564 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1566 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1568 e2
->right
= expr
->base
;
1570 e2
->ctype
= expr
->base
->ctype
;
1572 if (expr
->r_bitpos
) {
1573 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1576 e3
->right
= alloc_const_expression(expr
->pos
,
1577 bits_to_bytes(expr
->r_bitpos
));
1578 e3
->ctype
= &lazy_ptr_ctype
;
1583 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1586 e4
->ctype
= &lazy_ptr_ctype
;
1589 expr
->type
= EXPR_PREOP
;
1593 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1594 expression_error(expr
, "strange non-value function or array");
1597 *expr
= *expr
->unop
;
1598 ctype
= create_pointer(expr
, ctype
, 1);
1599 expr
->ctype
= ctype
;
1606 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1608 struct expression
*op
= expr
->unop
;
1609 struct symbol
*ctype
;
1611 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1612 expression_error(expr
, "not addressable");
1619 if (expr
->type
== EXPR_SYMBOL
) {
1620 struct symbol
*sym
= expr
->symbol
;
1621 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1625 * symbol expression evaluation is lazy about the type
1626 * of the sub-expression, so we may have to generate
1627 * the type here if so..
1629 if (expr
->ctype
== &lazy_ptr_ctype
) {
1630 ctype
= create_pointer(expr
, ctype
, 0);
1631 expr
->ctype
= ctype
;
1637 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1639 struct expression
*op
= expr
->unop
;
1640 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1642 /* Simplify: *&(expr) => (expr) */
1643 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1649 /* Dereferencing a node drops all the node information. */
1650 if (ctype
->type
== SYM_NODE
)
1651 ctype
= ctype
->ctype
.base_type
;
1653 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1654 target
= ctype
->ctype
.base_type
;
1656 switch (ctype
->type
) {
1658 expression_error(expr
, "cannot dereference this type");
1661 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1662 merge_type(node
, ctype
);
1666 if (!lvalue_expression(op
)) {
1667 expression_error(op
, "non-lvalue array??");
1671 /* Do the implied "addressof" on the array */
1675 * When an array is dereferenced, we need to pick
1676 * up the attributes of the original node too..
1678 merge_type(node
, op
->ctype
);
1679 merge_type(node
, ctype
);
1683 node
->bit_size
= target
->bit_size
;
1684 node
->array_size
= target
->array_size
;
1691 * Unary post-ops: x++ and x--
1693 static struct symbol
*evaluate_postop(struct expression
*expr
)
1695 struct expression
*op
= expr
->unop
;
1696 struct symbol
*ctype
= op
->ctype
;
1697 int class = classify_type(op
->ctype
, &ctype
);
1700 if (!lvalue_expression(expr
->unop
)) {
1701 expression_error(expr
, "need lvalue expression for ++/--");
1705 if ((class & TYPE_RESTRICT
) && restricted_unop(expr
->op
, &ctype
))
1706 return bad_expr_type(expr
);
1708 if (class & TYPE_NUM
) {
1710 } else if (class == TYPE_PTR
) {
1711 struct symbol
*target
= examine_pointer_target(ctype
);
1712 if (!is_function(target
))
1713 multiply
= bits_to_bytes(target
->bit_size
);
1717 evaluate_assign_to(op
, op
->ctype
);
1718 expr
->op_value
= multiply
;
1719 expr
->ctype
= ctype
;
1723 expression_error(expr
, "bad argument type for ++/--");
1727 static struct symbol
*evaluate_sign(struct expression
*expr
)
1729 struct symbol
*ctype
= expr
->unop
->ctype
;
1730 int class = classify_type(ctype
, &ctype
);
1731 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1733 /* should be an arithmetic type */
1734 if (!(class & TYPE_NUM
))
1735 return bad_expr_type(expr
);
1736 if (!(class & (TYPE_FLOAT
|TYPE_RESTRICT
))) {
1737 struct symbol
*rtype
= integer_promotion(ctype
);
1738 expr
->unop
= cast_to(expr
->unop
, rtype
);
1740 } else if ((class & TYPE_FLOAT
) && expr
->op
!= '~') {
1741 /* no conversions needed */
1742 } else if ((class & TYPE_RESTRICT
) && !restricted_unop(expr
->op
, &ctype
)) {
1743 /* no conversions needed */
1745 return bad_expr_type(expr
);
1747 if (expr
->op
== '+')
1748 *expr
= *expr
->unop
;
1749 expr
->ctype
= ctype
;
1753 static struct symbol
*evaluate_preop(struct expression
*expr
)
1755 struct symbol
*ctype
= expr
->unop
->ctype
;
1759 *expr
= *expr
->unop
;
1765 return evaluate_sign(expr
);
1768 return evaluate_dereference(expr
);
1771 return evaluate_addressof(expr
);
1773 case SPECIAL_INCREMENT
:
1774 case SPECIAL_DECREMENT
:
1776 * From a type evaluation standpoint the preops are
1777 * the same as the postops
1779 return evaluate_postop(expr
);
1782 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1784 if (is_safe_type(ctype
))
1785 warning(expr
->pos
, "testing a 'safe expression'");
1786 if (is_float_type(ctype
)) {
1787 struct expression
*arg
= expr
->unop
;
1788 expr
->type
= EXPR_BINOP
;
1789 expr
->op
= SPECIAL_EQUAL
;
1791 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1792 expr
->right
->ctype
= ctype
;
1793 expr
->right
->fvalue
= 0;
1794 } else if (is_fouled_type(ctype
)) {
1795 warning(expr
->pos
, "%s degrades to integer",
1796 show_typename(ctype
->ctype
.base_type
));
1798 ctype
= &bool_ctype
;
1804 expr
->ctype
= ctype
;
1808 static struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1810 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1811 struct ptr_list
*list
= head
;
1817 for (i
= 0; i
< list
->nr
; i
++) {
1818 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1820 if (sym
->ident
!= ident
)
1822 *offset
= sym
->offset
;
1825 struct symbol
*ctype
= sym
->ctype
.base_type
;
1829 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1831 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1834 *offset
+= sym
->offset
;
1838 } while ((list
= list
->next
) != head
);
1842 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1844 struct expression
*add
;
1847 * Create a new add-expression
1849 * NOTE! Even if we just add zero, we need a new node
1850 * for the member pointer, since it has a different
1851 * type than the original pointer. We could make that
1852 * be just a cast, but the fact is, a node is a node,
1853 * so we might as well just do the "add zero" here.
1855 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1858 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1859 add
->right
->ctype
= &int_ctype
;
1860 add
->right
->value
= offset
;
1863 * The ctype of the pointer will be lazily evaluated if
1864 * we ever take the address of this member dereference..
1866 add
->ctype
= &lazy_ptr_ctype
;
1870 /* structure/union dereference */
1871 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1874 struct symbol
*ctype
, *member
;
1875 struct expression
*deref
= expr
->deref
, *add
;
1876 struct ident
*ident
= expr
->member
;
1880 if (!evaluate_expression(deref
))
1883 expression_error(expr
, "bad member name");
1887 ctype
= deref
->ctype
;
1888 examine_symbol_type(ctype
);
1889 address_space
= ctype
->ctype
.as
;
1890 mod
= ctype
->ctype
.modifiers
;
1891 if (ctype
->type
== SYM_NODE
) {
1892 ctype
= ctype
->ctype
.base_type
;
1893 address_space
|= ctype
->ctype
.as
;
1894 mod
|= ctype
->ctype
.modifiers
;
1896 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1897 expression_error(expr
, "expected structure or union");
1901 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1903 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1904 const char *name
= "<unnamed>";
1907 name
= ctype
->ident
->name
;
1908 namelen
= ctype
->ident
->len
;
1910 if (ctype
->symbol_list
)
1911 expression_error(expr
, "no member '%s' in %s %.*s",
1912 show_ident(ident
), type
, namelen
, name
);
1914 expression_error(expr
, "using member '%s' in "
1915 "incomplete %s %.*s", show_ident(ident
),
1916 type
, namelen
, name
);
1921 * The member needs to take on the address space and modifiers of
1922 * the "parent" type.
1924 member
= convert_to_as_mod(member
, address_space
, mod
);
1925 ctype
= get_base_type(member
);
1927 if (!lvalue_expression(deref
)) {
1928 if (deref
->type
!= EXPR_SLICE
) {
1932 expr
->base
= deref
->base
;
1933 expr
->r_bitpos
= deref
->r_bitpos
;
1935 expr
->r_bitpos
+= bytes_to_bits(offset
);
1936 expr
->type
= EXPR_SLICE
;
1937 expr
->r_nrbits
= member
->bit_size
;
1938 expr
->r_bitpos
+= member
->bit_offset
;
1939 expr
->ctype
= member
;
1943 deref
= deref
->unop
;
1944 expr
->deref
= deref
;
1946 add
= evaluate_offset(deref
, offset
);
1947 expr
->type
= EXPR_PREOP
;
1951 expr
->ctype
= member
;
1955 static int is_promoted(struct expression
*expr
)
1958 switch (expr
->type
) {
1961 case EXPR_CONDITIONAL
:
1985 static struct symbol
*evaluate_cast(struct expression
*);
1987 static struct symbol
*evaluate_type_information(struct expression
*expr
)
1989 struct symbol
*sym
= expr
->cast_type
;
1991 sym
= evaluate_expression(expr
->cast_expression
);
1995 * Expressions of restricted types will possibly get
1996 * promoted - check that here
1998 if (is_restricted_type(sym
)) {
1999 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
2001 } else if (is_fouled_type(sym
)) {
2005 examine_symbol_type(sym
);
2006 if (is_bitfield_type(sym
)) {
2007 expression_error(expr
, "trying to examine bitfield type");
2013 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
2015 struct symbol
*type
;
2018 type
= evaluate_type_information(expr
);
2022 size
= type
->bit_size
;
2024 if (size
< 0 && is_void_type(type
)) {
2025 warning(expr
->pos
, "expression using sizeof(void)");
2026 size
= bits_in_char
;
2029 if (is_function(type
->ctype
.base_type
)) {
2030 warning(expr
->pos
, "expression using sizeof on a function");
2031 size
= bits_in_char
;
2034 if ((size
< 0) || (size
& (bits_in_char
- 1)))
2035 expression_error(expr
, "cannot size expression");
2037 expr
->type
= EXPR_VALUE
;
2038 expr
->value
= bits_to_bytes(size
);
2040 expr
->ctype
= size_t_ctype
;
2041 return size_t_ctype
;
2044 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
2046 struct symbol
*type
;
2049 type
= evaluate_type_information(expr
);
2053 if (type
->type
== SYM_NODE
)
2054 type
= type
->ctype
.base_type
;
2057 switch (type
->type
) {
2061 type
= get_base_type(type
);
2065 expression_error(expr
, "expected pointer expression");
2068 size
= type
->bit_size
;
2069 if (size
& (bits_in_char
-1))
2071 expr
->type
= EXPR_VALUE
;
2072 expr
->value
= bits_to_bytes(size
);
2074 expr
->ctype
= size_t_ctype
;
2075 return size_t_ctype
;
2078 static struct symbol
*evaluate_alignof(struct expression
*expr
)
2080 struct symbol
*type
;
2082 type
= evaluate_type_information(expr
);
2086 expr
->type
= EXPR_VALUE
;
2087 expr
->value
= type
->ctype
.alignment
;
2089 expr
->ctype
= size_t_ctype
;
2090 return size_t_ctype
;
2093 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
2095 struct expression
*expr
;
2096 struct symbol_list
*argument_types
= fn
->arguments
;
2097 struct symbol
*argtype
;
2100 PREPARE_PTR_LIST(argument_types
, argtype
);
2101 FOR_EACH_PTR (head
, expr
) {
2102 struct expression
**p
= THIS_ADDRESS(expr
);
2103 struct symbol
*ctype
, *target
;
2104 ctype
= evaluate_expression(expr
);
2111 struct symbol
*type
;
2112 int class = classify_type(ctype
, &type
);
2113 if (is_int(class)) {
2114 *p
= cast_to(expr
, integer_promotion(type
));
2115 } else if (class & TYPE_FLOAT
) {
2116 unsigned long mod
= type
->ctype
.modifiers
;
2117 if (!(mod
& (MOD_LONG_ALL
)))
2118 *p
= cast_to(expr
, &double_ctype
);
2119 } else if (class & TYPE_PTR
) {
2120 if (expr
->ctype
== &null_ctype
)
2121 *p
= cast_to(expr
, &ptr_ctype
);
2126 static char where
[30];
2127 examine_symbol_type(target
);
2128 sprintf(where
, "argument %d", i
);
2129 compatible_assignment_types(expr
, target
, p
, where
);
2133 NEXT_PTR_LIST(argtype
);
2134 } END_FOR_EACH_PTR(expr
);
2135 FINISH_PTR_LIST(argtype
);
2139 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
2143 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
2144 if (sym
->ident
== ident
)
2146 } END_FOR_EACH_PTR(sym
);
2150 static void convert_index(struct expression
*e
)
2152 struct expression
*child
= e
->idx_expression
;
2153 unsigned from
= e
->idx_from
;
2154 unsigned to
= e
->idx_to
+ 1;
2156 e
->init_offset
= from
* bits_to_bytes(e
->ctype
->bit_size
);
2157 e
->init_nr
= to
- from
;
2158 e
->init_expr
= child
;
2161 static void convert_ident(struct expression
*e
)
2163 struct expression
*child
= e
->ident_expression
;
2164 struct symbol
*sym
= e
->field
;
2166 e
->init_offset
= sym
->offset
;
2168 e
->init_expr
= child
;
2171 static void convert_designators(struct expression
*e
)
2174 if (e
->type
== EXPR_INDEX
)
2176 else if (e
->type
== EXPR_IDENTIFIER
)
2184 static void excess(struct expression
*e
, const char *s
)
2186 warning(e
->pos
, "excessive elements in %s initializer", s
);
2190 * implicit designator for the first element
2192 static struct expression
*first_subobject(struct symbol
*ctype
, int class,
2193 struct expression
**v
)
2195 struct expression
*e
= *v
, *new;
2197 if (ctype
->type
== SYM_NODE
)
2198 ctype
= ctype
->ctype
.base_type
;
2200 if (class & TYPE_PTR
) { /* array */
2201 if (!ctype
->bit_size
)
2203 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2204 new->idx_expression
= e
;
2205 new->ctype
= ctype
->ctype
.base_type
;
2207 struct symbol
*field
, *p
;
2208 PREPARE_PTR_LIST(ctype
->symbol_list
, p
);
2209 while (p
&& !p
->ident
&& is_bitfield_type(p
))
2215 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2216 new->ident_expression
= e
;
2217 new->field
= new->ctype
= field
;
2224 * sanity-check explicit designators; return the innermost one or NULL
2225 * in case of error. Assign types.
2227 static struct expression
*check_designators(struct expression
*e
,
2228 struct symbol
*ctype
)
2230 struct expression
*last
= NULL
;
2233 if (ctype
->type
== SYM_NODE
)
2234 ctype
= ctype
->ctype
.base_type
;
2235 if (e
->type
== EXPR_INDEX
) {
2236 struct symbol
*type
;
2237 if (ctype
->type
!= SYM_ARRAY
) {
2238 err
= "array index in non-array";
2241 type
= ctype
->ctype
.base_type
;
2242 if (ctype
->bit_size
>= 0 && type
->bit_size
>= 0) {
2243 unsigned offset
= e
->idx_to
* type
->bit_size
;
2244 if (offset
>= ctype
->bit_size
) {
2245 err
= "index out of bounds in";
2249 e
->ctype
= ctype
= type
;
2252 if (!e
->idx_expression
) {
2256 e
= e
->idx_expression
;
2257 } else if (e
->type
== EXPR_IDENTIFIER
) {
2258 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2259 err
= "field name not in struct or union";
2262 ctype
= find_struct_ident(ctype
, e
->expr_ident
);
2264 err
= "unknown field name in";
2267 e
->field
= e
->ctype
= ctype
;
2269 if (!e
->ident_expression
) {
2273 e
= e
->ident_expression
;
2274 } else if (e
->type
== EXPR_POS
) {
2275 err
= "internal front-end error: EXPR_POS in";
2280 expression_error(e
, "%s initializer", err
);
2285 * choose the next subobject to initialize.
2287 * Get designators for next element, switch old ones to EXPR_POS.
2288 * Return the resulting expression or NULL if we'd run out of subobjects.
2289 * The innermost designator is returned in *v. Designators in old
2290 * are assumed to be already sanity-checked.
2292 static struct expression
*next_designators(struct expression
*old
,
2293 struct symbol
*ctype
,
2294 struct expression
*e
, struct expression
**v
)
2296 struct expression
*new = NULL
;
2300 if (old
->type
== EXPR_INDEX
) {
2301 struct expression
*copy
;
2304 copy
= next_designators(old
->idx_expression
,
2307 n
= old
->idx_to
+ 1;
2308 if (n
* old
->ctype
->bit_size
== ctype
->bit_size
) {
2313 *v
= new = alloc_expression(e
->pos
, EXPR_INDEX
);
2316 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2319 new->idx_from
= new->idx_to
= n
;
2320 new->idx_expression
= copy
;
2321 new->ctype
= old
->ctype
;
2323 } else if (old
->type
== EXPR_IDENTIFIER
) {
2324 struct expression
*copy
;
2325 struct symbol
*field
;
2327 copy
= next_designators(old
->ident_expression
,
2330 field
= old
->field
->next_subobject
;
2336 *v
= new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2339 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2343 new->expr_ident
= field
->ident
;
2344 new->ident_expression
= copy
;
2351 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2352 int class, struct symbol
*ctype
);
2355 * deal with traversing subobjects [6.7.8(17,18,20)]
2357 static void handle_list_initializer(struct expression
*expr
,
2358 int class, struct symbol
*ctype
)
2360 struct expression
*e
, *last
= NULL
, *top
= NULL
, *next
;
2363 FOR_EACH_PTR(expr
->expr_list
, e
) {
2364 struct expression
**v
;
2365 struct symbol
*type
;
2368 if (e
->type
!= EXPR_INDEX
&& e
->type
!= EXPR_IDENTIFIER
) {
2369 struct symbol
*struct_sym
;
2372 last
= first_subobject(ctype
, class, &top
);
2374 last
= next_designators(last
, ctype
, e
, &top
);
2377 excess(e
, class & TYPE_PTR
? "array" :
2379 DELETE_CURRENT_PTR(e
);
2382 struct_sym
= ctype
->type
== SYM_NODE
? ctype
->ctype
.base_type
: ctype
;
2383 if (Wdesignated_init
&& struct_sym
->designated_init
)
2384 warning(e
->pos
, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2385 ctype
->ident
? "in initializer for " : "",
2386 ctype
->ident
? ctype
->ident
->len
: 0,
2387 ctype
->ident
? ctype
->ident
->name
: "",
2388 ctype
->ident
? ": " : "",
2389 get_type_name(struct_sym
->type
),
2390 show_ident(struct_sym
->ident
));
2392 warning(e
->pos
, "advancing past deep designator");
2395 REPLACE_CURRENT_PTR(e
, last
);
2397 next
= check_designators(e
, ctype
);
2399 DELETE_CURRENT_PTR(e
);
2403 /* deeper than one designator? */
2405 convert_designators(last
);
2410 lclass
= classify_type(top
->ctype
, &type
);
2411 if (top
->type
== EXPR_INDEX
)
2412 v
= &top
->idx_expression
;
2414 v
= &top
->ident_expression
;
2416 if (handle_simple_initializer(v
, 1, lclass
, top
->ctype
))
2419 if (!(lclass
& TYPE_COMPOUND
)) {
2420 warning(e
->pos
, "bogus scalar initializer");
2421 DELETE_CURRENT_PTR(e
);
2425 next
= first_subobject(type
, lclass
, v
);
2427 warning(e
->pos
, "missing braces around initializer");
2432 DELETE_CURRENT_PTR(e
);
2433 excess(e
, lclass
& TYPE_PTR
? "array" : "struct or union");
2435 } END_FOR_EACH_PTR(e
);
2437 convert_designators(last
);
2438 expr
->ctype
= ctype
;
2441 static int is_string_literal(struct expression
**v
)
2443 struct expression
*e
= *v
;
2444 while (e
&& e
->type
== EXPR_PREOP
&& e
->op
== '(')
2446 if (!e
|| e
->type
!= EXPR_STRING
)
2448 if (e
!= *v
&& Wparen_string
)
2450 "array initialized from parenthesized string constant");
2456 * We want a normal expression, possibly in one layer of braces. Warn
2457 * if the latter happens inside a list (it's legal, but likely to be
2458 * an effect of screwup). In case of anything not legal, we are definitely
2459 * having an effect of screwup, so just fail and let the caller warn.
2461 static struct expression
*handle_scalar(struct expression
*e
, int nested
)
2463 struct expression
*v
= NULL
, *p
;
2467 if (e
->type
!= EXPR_INITIALIZER
)
2470 FOR_EACH_PTR(e
->expr_list
, p
) {
2474 } END_FOR_EACH_PTR(p
);
2478 case EXPR_INITIALIZER
:
2480 case EXPR_IDENTIFIER
:
2486 warning(e
->pos
, "braces around scalar initializer");
2491 * deal with the cases that don't care about subobjects:
2492 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2493 * character array <- string literal, possibly in braces [6.7.8(14)]
2494 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2495 * compound type <- initializer list in braces [6.7.8(16)]
2496 * The last one punts to handle_list_initializer() which, in turn will call
2497 * us for individual elements of the list.
2499 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2500 * the lack of support of wide char stuff in general.
2502 * One note: we need to take care not to evaluate a string literal until
2503 * we know that we *will* handle it right here. Otherwise we would screw
2504 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2505 * { "string", ...} - we need to preserve that string literal recognizable
2506 * until we dig into the inner struct.
2508 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2509 int class, struct symbol
*ctype
)
2511 int is_string
= is_string_type(ctype
);
2512 struct expression
*e
= *ep
, *p
;
2513 struct symbol
*type
;
2519 if (!(class & TYPE_COMPOUND
)) {
2520 e
= handle_scalar(e
, nested
);
2524 if (!evaluate_expression(e
))
2526 compatible_assignment_types(e
, ctype
, ep
, "initializer");
2531 * sublist; either a string, or we dig in; the latter will deal with
2532 * pathologies, so we don't need anything fancy here.
2534 if (e
->type
== EXPR_INITIALIZER
) {
2536 struct expression
*v
= NULL
;
2539 FOR_EACH_PTR(e
->expr_list
, p
) {
2543 } END_FOR_EACH_PTR(p
);
2544 if (count
== 1 && is_string_literal(&v
)) {
2549 handle_list_initializer(e
, class, ctype
);
2554 if (is_string_literal(&e
)) {
2555 /* either we are doing array of char, or we'll have to dig in */
2562 /* struct or union can be initialized by compatible */
2563 if (class != TYPE_COMPOUND
)
2565 type
= evaluate_expression(e
);
2568 if (ctype
->type
== SYM_NODE
)
2569 ctype
= ctype
->ctype
.base_type
;
2570 if (type
->type
== SYM_NODE
)
2571 type
= type
->ctype
.base_type
;
2577 p
= alloc_expression(e
->pos
, EXPR_STRING
);
2579 type
= evaluate_expression(p
);
2580 if (ctype
->bit_size
!= -1 &&
2581 ctype
->bit_size
+ bits_in_char
< type
->bit_size
) {
2583 "too long initializer-string for array of char");
2589 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2591 struct symbol
*type
;
2592 int class = classify_type(ctype
, &type
);
2593 if (!handle_simple_initializer(ep
, 0, class, ctype
))
2594 expression_error(*ep
, "invalid initializer");
2597 static struct symbol
*evaluate_cast(struct expression
*expr
)
2599 struct expression
*target
= expr
->cast_expression
;
2600 struct symbol
*ctype
;
2601 struct symbol
*t1
, *t2
;
2603 int as1
= 0, as2
= 0;
2609 * Special case: a cast can be followed by an
2610 * initializer, in which case we need to pass
2611 * the type value down to that initializer rather
2612 * than trying to evaluate it as an expression
2614 * A more complex case is when the initializer is
2615 * dereferenced as part of a post-fix expression.
2616 * We need to produce an expression that can be dereferenced.
2618 if (target
->type
== EXPR_INITIALIZER
) {
2619 struct symbol
*sym
= expr
->cast_type
;
2620 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2622 sym
->initializer
= target
;
2623 evaluate_symbol(sym
);
2625 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2628 expr
->type
= EXPR_PREOP
;
2636 ctype
= examine_symbol_type(expr
->cast_type
);
2637 expr
->ctype
= ctype
;
2638 expr
->cast_type
= ctype
;
2640 evaluate_expression(target
);
2643 class1
= classify_type(ctype
, &t1
);
2645 /* cast to non-integer type -> not an integer constant expression */
2646 if (!is_int(class1
))
2648 /* if argument turns out to be not an integer constant expression *and*
2649 it was not a floating literal to start with -> too bad */
2650 else if (expr
->flags
== Int_const_expr
&&
2651 !(target
->flags
& Int_const_expr
))
2654 * You can always throw a value away by casting to
2655 * "void" - that's an implicit "force". Note that
2656 * the same is _not_ true of "void *".
2658 if (t1
== &void_ctype
)
2661 if (class1
& (TYPE_COMPOUND
| TYPE_FN
))
2662 warning(expr
->pos
, "cast to non-scalar");
2666 expression_error(expr
, "cast from unknown type");
2669 class2
= classify_type(t2
, &t2
);
2671 if (class2
& TYPE_COMPOUND
)
2672 warning(expr
->pos
, "cast from non-scalar");
2674 if (expr
->type
== EXPR_FORCE_CAST
)
2677 /* allowed cast unfouls */
2678 if (class2
& TYPE_FOULED
)
2682 if (class1
& TYPE_RESTRICT
)
2683 warning(expr
->pos
, "cast to %s",
2685 if (class2
& TYPE_RESTRICT
)
2686 warning(expr
->pos
, "cast from %s",
2690 if (t1
== &ulong_ctype
)
2692 else if (class1
== TYPE_PTR
) {
2693 examine_pointer_target(t1
);
2697 if (t2
== &ulong_ctype
)
2699 else if (class2
== TYPE_PTR
) {
2700 examine_pointer_target(t2
);
2704 if (!as1
&& as2
> 0)
2705 warning(expr
->pos
, "cast removes address space of expression");
2706 if (as1
> 0 && as2
> 0 && as1
!= as2
)
2707 warning(expr
->pos
, "cast between address spaces (<asn:%d>-><asn:%d>)", as2
, as1
);
2708 if (as1
> 0 && !as2
&&
2709 !is_null_pointer_constant(target
) && Wcast_to_as
)
2711 "cast adds address space to expression (<asn:%d>)", as1
);
2713 if (!(t1
->ctype
.modifiers
& MOD_PTRINHERIT
) && class1
== TYPE_PTR
&&
2714 !as1
&& (target
->flags
& Int_const_expr
)) {
2715 if (t1
->ctype
.base_type
== &void_ctype
) {
2716 if (is_zero_constant(target
)) {
2718 expr
->type
= EXPR_VALUE
;
2719 expr
->ctype
= &null_ctype
;
2730 * Evaluate a call expression with a symbol. This
2731 * should expand inline functions, and evaluate
2734 static int evaluate_symbol_call(struct expression
*expr
)
2736 struct expression
*fn
= expr
->fn
;
2737 struct symbol
*ctype
= fn
->ctype
;
2739 if (fn
->type
!= EXPR_PREOP
)
2742 if (ctype
->op
&& ctype
->op
->evaluate
)
2743 return ctype
->op
->evaluate(expr
);
2745 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2747 struct symbol
*curr
= current_fn
;
2749 if (ctype
->definition
)
2750 ctype
= ctype
->definition
;
2752 current_fn
= ctype
->ctype
.base_type
;
2754 ret
= inline_function(expr
, ctype
);
2756 /* restore the old function */
2764 static struct symbol
*evaluate_call(struct expression
*expr
)
2767 struct symbol
*ctype
, *sym
;
2768 struct expression
*fn
= expr
->fn
;
2769 struct expression_list
*arglist
= expr
->args
;
2771 if (!evaluate_expression(fn
))
2773 sym
= ctype
= fn
->ctype
;
2774 if (ctype
->type
== SYM_NODE
)
2775 ctype
= ctype
->ctype
.base_type
;
2776 if (ctype
->type
== SYM_PTR
)
2777 ctype
= get_base_type(ctype
);
2779 if (ctype
->type
!= SYM_FN
) {
2780 struct expression
*arg
;
2781 expression_error(expr
, "not a function %s",
2782 show_ident(sym
->ident
));
2783 /* do typechecking in arguments */
2784 FOR_EACH_PTR (arglist
, arg
) {
2785 evaluate_expression(arg
);
2786 } END_FOR_EACH_PTR(arg
);
2790 examine_fn_arguments(ctype
);
2791 if (sym
->type
== SYM_NODE
&& fn
->type
== EXPR_PREOP
&&
2792 sym
->op
&& sym
->op
->args
) {
2793 if (!sym
->op
->args(expr
))
2796 if (!evaluate_arguments(sym
, ctype
, arglist
))
2798 args
= expression_list_size(expr
->args
);
2799 fnargs
= symbol_list_size(ctype
->arguments
);
2801 expression_error(expr
,
2802 "not enough arguments for function %s",
2803 show_ident(sym
->ident
));
2804 if (args
> fnargs
&& !ctype
->variadic
)
2805 expression_error(expr
,
2806 "too many arguments for function %s",
2807 show_ident(sym
->ident
));
2809 if (sym
->type
== SYM_NODE
) {
2810 if (evaluate_symbol_call(expr
))
2813 expr
->ctype
= ctype
->ctype
.base_type
;
2817 static struct symbol
*evaluate_offsetof(struct expression
*expr
)
2819 struct expression
*e
= expr
->down
;
2820 struct symbol
*ctype
= expr
->in
;
2823 if (expr
->op
== '.') {
2824 struct symbol
*field
;
2827 expression_error(expr
, "expected structure or union");
2830 examine_symbol_type(ctype
);
2831 class = classify_type(ctype
, &ctype
);
2832 if (class != TYPE_COMPOUND
) {
2833 expression_error(expr
, "expected structure or union");
2837 field
= find_identifier(expr
->ident
, ctype
->symbol_list
, &offset
);
2839 expression_error(expr
, "unknown member");
2843 expr
->type
= EXPR_VALUE
;
2844 expr
->flags
= Int_const_expr
;
2845 expr
->value
= offset
;
2847 expr
->ctype
= size_t_ctype
;
2850 expression_error(expr
, "expected structure or union");
2853 examine_symbol_type(ctype
);
2854 class = classify_type(ctype
, &ctype
);
2855 if (class != (TYPE_COMPOUND
| TYPE_PTR
)) {
2856 expression_error(expr
, "expected array");
2859 ctype
= ctype
->ctype
.base_type
;
2861 expr
->type
= EXPR_VALUE
;
2862 expr
->flags
= Int_const_expr
;
2865 expr
->ctype
= size_t_ctype
;
2867 struct expression
*idx
= expr
->index
, *m
;
2868 struct symbol
*i_type
= evaluate_expression(idx
);
2869 int i_class
= classify_type(i_type
, &i_type
);
2870 if (!is_int(i_class
)) {
2871 expression_error(expr
, "non-integer index");
2874 unrestrict(idx
, i_class
, &i_type
);
2875 idx
= cast_to(idx
, size_t_ctype
);
2876 m
= alloc_const_expression(expr
->pos
,
2877 bits_to_bytes(ctype
->bit_size
));
2878 m
->ctype
= size_t_ctype
;
2879 m
->flags
= Int_const_expr
;
2880 expr
->type
= EXPR_BINOP
;
2884 expr
->ctype
= size_t_ctype
;
2885 expr
->flags
= m
->flags
& idx
->flags
& Int_const_expr
;
2889 struct expression
*copy
= __alloc_expression(0);
2891 if (e
->type
== EXPR_OFFSETOF
)
2893 if (!evaluate_expression(e
))
2895 expr
->type
= EXPR_BINOP
;
2896 expr
->flags
= e
->flags
& copy
->flags
& Int_const_expr
;
2898 expr
->ctype
= size_t_ctype
;
2902 return size_t_ctype
;
2905 struct symbol
*evaluate_expression(struct expression
*expr
)
2912 switch (expr
->type
) {
2915 expression_error(expr
, "value expression without a type");
2918 return evaluate_string(expr
);
2920 return evaluate_symbol_expression(expr
);
2922 if (!evaluate_expression(expr
->left
))
2924 if (!evaluate_expression(expr
->right
))
2926 return evaluate_binop(expr
);
2928 return evaluate_logical(expr
);
2930 evaluate_expression(expr
->left
);
2931 if (!evaluate_expression(expr
->right
))
2933 return evaluate_comma(expr
);
2935 if (!evaluate_expression(expr
->left
))
2937 if (!evaluate_expression(expr
->right
))
2939 return evaluate_compare(expr
);
2940 case EXPR_ASSIGNMENT
:
2941 if (!evaluate_expression(expr
->left
))
2943 if (!evaluate_expression(expr
->right
))
2945 return evaluate_assignment(expr
);
2947 if (!evaluate_expression(expr
->unop
))
2949 return evaluate_preop(expr
);
2951 if (!evaluate_expression(expr
->unop
))
2953 return evaluate_postop(expr
);
2955 case EXPR_FORCE_CAST
:
2956 case EXPR_IMPLIED_CAST
:
2957 return evaluate_cast(expr
);
2959 return evaluate_sizeof(expr
);
2960 case EXPR_PTRSIZEOF
:
2961 return evaluate_ptrsizeof(expr
);
2963 return evaluate_alignof(expr
);
2965 return evaluate_member_dereference(expr
);
2967 return evaluate_call(expr
);
2969 case EXPR_CONDITIONAL
:
2970 return evaluate_conditional_expression(expr
);
2971 case EXPR_STATEMENT
:
2972 expr
->ctype
= evaluate_statement(expr
->statement
);
2976 expr
->ctype
= &ptr_ctype
;
2980 /* Evaluate the type of the symbol .. */
2981 evaluate_symbol(expr
->symbol
);
2982 /* .. but the type of the _expression_ is a "type" */
2983 expr
->ctype
= &type_ctype
;
2987 return evaluate_offsetof(expr
);
2989 /* These can not exist as stand-alone expressions */
2990 case EXPR_INITIALIZER
:
2991 case EXPR_IDENTIFIER
:
2994 expression_error(expr
, "internal front-end error: initializer in expression");
2997 expression_error(expr
, "internal front-end error: SLICE re-evaluated");
3003 static void check_duplicates(struct symbol
*sym
)
3006 struct symbol
*next
= sym
;
3008 while ((next
= next
->same_symbol
) != NULL
) {
3009 const char *typediff
;
3010 evaluate_symbol(next
);
3012 typediff
= type_difference(&sym
->ctype
, &next
->ctype
, 0, 0);
3014 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
3015 show_ident(sym
->ident
),
3016 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
3021 unsigned long mod
= sym
->ctype
.modifiers
;
3022 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
3024 if (!(mod
& MOD_TOPLEVEL
))
3028 if (sym
->ident
== &main_ident
)
3030 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
3034 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
3036 struct symbol
*base_type
;
3044 sym
= examine_symbol_type(sym
);
3045 base_type
= get_base_type(sym
);
3049 /* Evaluate the initializers */
3050 if (sym
->initializer
)
3051 evaluate_initializer(sym
, &sym
->initializer
);
3053 /* And finally, evaluate the body of the symbol too */
3054 if (base_type
->type
== SYM_FN
) {
3055 struct symbol
*curr
= current_fn
;
3057 if (sym
->definition
&& sym
->definition
!= sym
)
3058 return evaluate_symbol(sym
->definition
);
3060 current_fn
= base_type
;
3062 examine_fn_arguments(base_type
);
3063 if (!base_type
->stmt
&& base_type
->inline_stmt
)
3065 if (base_type
->stmt
)
3066 evaluate_statement(base_type
->stmt
);
3074 void evaluate_symbol_list(struct symbol_list
*list
)
3078 FOR_EACH_PTR(list
, sym
) {
3079 evaluate_symbol(sym
);
3080 check_duplicates(sym
);
3081 } END_FOR_EACH_PTR(sym
);
3084 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
3086 struct expression
*expr
= stmt
->expression
;
3087 struct symbol
*fntype
;
3089 evaluate_expression(expr
);
3090 fntype
= current_fn
->ctype
.base_type
;
3091 if (!fntype
|| fntype
== &void_ctype
) {
3092 if (expr
&& expr
->ctype
!= &void_ctype
)
3093 expression_error(expr
, "return expression in %s function", fntype
?"void":"typeless");
3094 if (expr
&& Wreturn_void
)
3095 warning(stmt
->pos
, "returning void-valued expression");
3100 sparse_error(stmt
->pos
, "return with no return value");
3105 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, "return expression");
3109 static void evaluate_if_statement(struct statement
*stmt
)
3111 if (!stmt
->if_conditional
)
3114 evaluate_conditional(stmt
->if_conditional
, 0);
3115 evaluate_statement(stmt
->if_true
);
3116 evaluate_statement(stmt
->if_false
);
3119 static void evaluate_iterator(struct statement
*stmt
)
3121 evaluate_symbol_list(stmt
->iterator_syms
);
3122 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
3123 evaluate_conditional(stmt
->iterator_post_condition
,1);
3124 evaluate_statement(stmt
->iterator_pre_statement
);
3125 evaluate_statement(stmt
->iterator_statement
);
3126 evaluate_statement(stmt
->iterator_post_statement
);
3129 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
3131 switch (*constraint
) {
3132 case '=': /* Assignment */
3133 case '+': /* Update */
3136 expression_error(expr
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
3140 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
3142 switch (*constraint
) {
3143 case '=': /* Assignment */
3144 case '+': /* Update */
3145 expression_error(expr
, "input constraint with assignment (\"%s\")", constraint
);
3149 static void evaluate_asm_statement(struct statement
*stmt
)
3151 struct expression
*expr
;
3154 expr
= stmt
->asm_string
;
3155 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3156 sparse_error(stmt
->pos
, "need constant string for inline asm");
3161 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
3162 struct ident
*ident
;
3165 case 0: /* Identifier */
3167 ident
= (struct ident
*)expr
;
3170 case 1: /* Constraint */
3172 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3173 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
3174 *THIS_ADDRESS(expr
) = NULL
;
3177 verify_output_constraint(expr
, expr
->string
->data
);
3180 case 2: /* Expression */
3182 if (!evaluate_expression(expr
))
3184 if (!lvalue_expression(expr
))
3185 warning(expr
->pos
, "asm output is not an lvalue");
3186 evaluate_assign_to(expr
, expr
->ctype
);
3189 } END_FOR_EACH_PTR(expr
);
3192 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
3193 struct ident
*ident
;
3196 case 0: /* Identifier */
3198 ident
= (struct ident
*)expr
;
3201 case 1: /* Constraint */
3203 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3204 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
3205 *THIS_ADDRESS(expr
) = NULL
;
3208 verify_input_constraint(expr
, expr
->string
->data
);
3211 case 2: /* Expression */
3213 if (!evaluate_expression(expr
))
3217 } END_FOR_EACH_PTR(expr
);
3219 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
3221 sparse_error(stmt
->pos
, "bad asm output");
3224 if (expr
->type
== EXPR_STRING
)
3226 expression_error(expr
, "asm clobber is not a string");
3227 } END_FOR_EACH_PTR(expr
);
3230 static void evaluate_case_statement(struct statement
*stmt
)
3232 evaluate_expression(stmt
->case_expression
);
3233 evaluate_expression(stmt
->case_to
);
3234 evaluate_statement(stmt
->case_statement
);
3237 static void check_case_type(struct expression
*switch_expr
,
3238 struct expression
*case_expr
,
3239 struct expression
**enumcase
)
3241 struct symbol
*switch_type
, *case_type
;
3247 switch_type
= switch_expr
->ctype
;
3248 case_type
= evaluate_expression(case_expr
);
3250 if (!switch_type
|| !case_type
)
3254 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
3255 else if (is_enum_type(case_type
))
3256 *enumcase
= case_expr
;
3259 sclass
= classify_type(switch_type
, &switch_type
);
3260 cclass
= classify_type(case_type
, &case_type
);
3262 /* both should be arithmetic */
3263 if (!(sclass
& cclass
& TYPE_NUM
))
3266 /* neither should be floating */
3267 if ((sclass
| cclass
) & TYPE_FLOAT
)
3270 /* if neither is restricted, we are OK */
3271 if (!((sclass
| cclass
) & TYPE_RESTRICT
))
3274 if (!restricted_binop_type(SPECIAL_EQUAL
, case_expr
, switch_expr
,
3275 cclass
, sclass
, case_type
, switch_type
)) {
3276 unrestrict(case_expr
, cclass
, &case_type
);
3277 unrestrict(switch_expr
, sclass
, &switch_type
);
3282 expression_error(case_expr
, "incompatible types for 'case' statement");
3285 static void evaluate_switch_statement(struct statement
*stmt
)
3288 struct expression
*enumcase
= NULL
;
3289 struct expression
**enumcase_holder
= &enumcase
;
3290 struct expression
*sel
= stmt
->switch_expression
;
3292 evaluate_expression(sel
);
3293 evaluate_statement(stmt
->switch_statement
);
3296 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
3297 enumcase_holder
= NULL
; /* Only check cases against switch */
3299 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
3300 struct statement
*case_stmt
= sym
->stmt
;
3301 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
3302 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
3303 } END_FOR_EACH_PTR(sym
);
3306 struct symbol
*evaluate_statement(struct statement
*stmt
)
3311 switch (stmt
->type
) {
3312 case STMT_DECLARATION
: {
3314 FOR_EACH_PTR(stmt
->declaration
, s
) {
3316 } END_FOR_EACH_PTR(s
);
3321 return evaluate_return_expression(stmt
);
3323 case STMT_EXPRESSION
:
3324 if (!evaluate_expression(stmt
->expression
))
3326 if (stmt
->expression
->ctype
== &null_ctype
)
3327 stmt
->expression
= cast_to(stmt
->expression
, &ptr_ctype
);
3328 return degenerate(stmt
->expression
);
3330 case STMT_COMPOUND
: {
3331 struct statement
*s
;
3332 struct symbol
*type
= NULL
;
3334 /* Evaluate the return symbol in the compound statement */
3335 evaluate_symbol(stmt
->ret
);
3338 * Then, evaluate each statement, making the type of the
3339 * compound statement be the type of the last statement
3341 type
= evaluate_statement(stmt
->args
);
3342 FOR_EACH_PTR(stmt
->stmts
, s
) {
3343 type
= evaluate_statement(s
);
3344 } END_FOR_EACH_PTR(s
);
3350 evaluate_if_statement(stmt
);
3353 evaluate_iterator(stmt
);
3356 evaluate_switch_statement(stmt
);
3359 evaluate_case_statement(stmt
);
3362 return evaluate_statement(stmt
->label_statement
);
3364 evaluate_expression(stmt
->goto_expression
);
3369 evaluate_asm_statement(stmt
);
3372 evaluate_expression(stmt
->expression
);
3375 evaluate_expression(stmt
->range_expression
);
3376 evaluate_expression(stmt
->range_low
);
3377 evaluate_expression(stmt
->range_high
);