4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
9 * Evaluate constant expressions.
27 #include "expression.h"
29 struct symbol
*current_fn
;
31 static struct symbol
*degenerate(struct expression
*expr
);
32 static struct symbol
*evaluate_symbol(struct symbol
*sym
);
34 static struct symbol
*evaluate_symbol_expression(struct expression
*expr
)
36 struct expression
*addr
;
37 struct symbol
*sym
= expr
->symbol
;
38 struct symbol
*base_type
;
41 expression_error(expr
, "undefined identifier '%s'", show_ident(expr
->symbol_name
));
45 examine_symbol_type(sym
);
47 base_type
= get_base_type(sym
);
49 expression_error(expr
, "identifier '%s' has no type", show_ident(expr
->symbol_name
));
53 addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
55 addr
->symbol_name
= expr
->symbol_name
;
56 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
57 expr
->type
= EXPR_PREOP
;
61 /* The type of a symbol is the symbol itself! */
66 static struct symbol
*evaluate_string(struct expression
*expr
)
68 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
69 struct symbol
*array
= alloc_symbol(expr
->pos
, SYM_ARRAY
);
70 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
71 struct expression
*initstr
= alloc_expression(expr
->pos
, EXPR_STRING
);
72 unsigned int length
= expr
->string
->length
;
74 sym
->array_size
= alloc_const_expression(expr
->pos
, length
);
75 sym
->bit_size
= bits_in_char
* length
;
76 sym
->ctype
.alignment
= 1;
78 sym
->ctype
.modifiers
= MOD_STATIC
;
79 sym
->ctype
.base_type
= array
;
80 sym
->initializer
= initstr
;
83 initstr
->string
= expr
->string
;
85 array
->array_size
= sym
->array_size
;
86 array
->bit_size
= bits_in_char
* length
;
87 array
->ctype
.alignment
= 1;
88 array
->ctype
.modifiers
= MOD_STATIC
;
89 array
->ctype
.base_type
= &char_ctype
;
92 addr
->ctype
= &lazy_ptr_ctype
;
94 expr
->type
= EXPR_PREOP
;
101 /* 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
;
350 static inline int classify_type(struct symbol
*type
, struct symbol
**base
)
352 static int type_class
[SYM_BAD
+ 1] = {
353 [SYM_PTR
] = TYPE_PTR
,
355 [SYM_ARRAY
] = TYPE_PTR
| TYPE_COMPOUND
,
356 [SYM_STRUCT
] = TYPE_COMPOUND
,
357 [SYM_UNION
] = TYPE_COMPOUND
,
358 [SYM_BITFIELD
] = TYPE_NUM
| TYPE_BITFIELD
,
359 [SYM_RESTRICT
] = TYPE_NUM
| TYPE_RESTRICT
,
360 [SYM_FOULED
] = TYPE_NUM
| TYPE_RESTRICT
| TYPE_FOULED
,
362 if (type
->type
== SYM_NODE
)
363 type
= type
->ctype
.base_type
;
364 if (type
->type
== SYM_ENUM
)
365 type
= type
->ctype
.base_type
;
367 if (type
->type
== SYM_BASETYPE
) {
368 if (type
->ctype
.base_type
== &int_type
)
370 if (type
->ctype
.base_type
== &fp_type
)
371 return TYPE_NUM
| TYPE_FLOAT
;
373 return type_class
[type
->type
];
376 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
378 static inline int is_string_type(struct symbol
*type
)
380 if (type
->type
== SYM_NODE
)
381 type
= type
->ctype
.base_type
;
382 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
385 static struct symbol
*bad_expr_type(struct expression
*expr
)
387 sparse_error(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
388 switch (expr
->type
) {
391 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
392 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
396 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
403 return expr
->ctype
= &bad_ctype
;
406 static int restricted_value(struct expression
*v
, struct symbol
*type
)
408 if (v
->type
!= EXPR_VALUE
)
415 static int restricted_binop(int op
, struct symbol
*type
)
420 case SPECIAL_AND_ASSIGN
:
421 case SPECIAL_OR_ASSIGN
:
422 case SPECIAL_XOR_ASSIGN
:
423 return 1; /* unfoul */
427 return 2; /* keep fouled */
429 case SPECIAL_NOTEQUAL
:
430 return 3; /* warn if fouled */
436 static int restricted_unop(int op
, struct symbol
**type
)
439 if ((*type
)->bit_size
< bits_in_int
)
440 *type
= befoul(*type
);
447 static struct symbol
*restricted_binop_type(int op
,
448 struct expression
*left
,
449 struct expression
*right
,
450 int lclass
, int rclass
,
451 struct symbol
*ltype
,
452 struct symbol
*rtype
)
454 struct symbol
*ctype
= NULL
;
455 if (lclass
& TYPE_RESTRICT
) {
456 if (rclass
& TYPE_RESTRICT
) {
457 if (ltype
== rtype
) {
459 } else if (lclass
& TYPE_FOULED
) {
460 if (ltype
->ctype
.base_type
== rtype
)
462 } else if (rclass
& TYPE_FOULED
) {
463 if (rtype
->ctype
.base_type
== ltype
)
467 if (!restricted_value(right
, ltype
))
470 } else if (!restricted_value(left
, rtype
))
474 switch (restricted_binop(op
, ctype
)) {
476 if ((lclass
^ rclass
) & TYPE_FOULED
)
477 ctype
= ctype
->ctype
.base_type
;
480 if (!(lclass
& rclass
& TYPE_FOULED
))
492 static inline void unrestrict(struct expression
*expr
,
493 int class, struct symbol
**ctype
)
495 if (class & TYPE_RESTRICT
) {
496 warning(expr
->pos
, "restricted degrades to integer");
497 if (class & TYPE_FOULED
) /* unfoul it first */
498 *ctype
= (*ctype
)->ctype
.base_type
;
499 *ctype
= (*ctype
)->ctype
.base_type
; /* get to arithmetic type */
503 static struct symbol
*usual_conversions(int op
,
504 struct expression
*left
,
505 struct expression
*right
,
506 int lclass
, int rclass
,
507 struct symbol
*ltype
,
508 struct symbol
*rtype
)
510 struct symbol
*ctype
;
512 warn_for_different_enum_types(right
->pos
, left
->ctype
, right
->ctype
);
514 if ((lclass
| rclass
) & TYPE_RESTRICT
)
518 if (!(lclass
& TYPE_FLOAT
)) {
519 if (!(rclass
& TYPE_FLOAT
))
520 return bigger_int_type(ltype
, rtype
);
523 } else if (rclass
& TYPE_FLOAT
) {
524 unsigned long lmod
= ltype
->ctype
.modifiers
;
525 unsigned long rmod
= rtype
->ctype
.modifiers
;
526 if (rmod
& ~lmod
& (MOD_LONG
| MOD_LONGLONG
))
534 ctype
= restricted_binop_type(op
, left
, right
,
535 lclass
, rclass
, ltype
, rtype
);
539 unrestrict(left
, lclass
, <ype
);
540 unrestrict(right
, rclass
, &rtype
);
545 static inline int lvalue_expression(struct expression
*expr
)
547 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
550 static int ptr_object_size(struct symbol
*ptr_type
)
552 if (ptr_type
->type
== SYM_NODE
)
553 ptr_type
= ptr_type
->ctype
.base_type
;
554 if (ptr_type
->type
== SYM_PTR
)
555 ptr_type
= get_base_type(ptr_type
);
556 return ptr_type
->bit_size
;
559 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*ctype
, struct symbol
*itype
)
561 struct expression
*index
= expr
->right
;
565 if (ctype
== &null_ctype
)
568 examine_symbol_type(ctype
);
570 if (!ctype
->ctype
.base_type
) {
571 expression_error(expr
, "missing type information");
575 /* Get the size of whatever the pointer points to */
576 bit_size
= ptr_object_size(ctype
);
577 multiply
= bit_size
>> 3;
581 if (multiply
== 1 && itype
->bit_size
>= bits_in_pointer
)
584 if (index
->type
== EXPR_VALUE
) {
585 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
586 unsigned long long v
= index
->value
, mask
;
587 mask
= 1ULL << (itype
->bit_size
- 1);
593 mask
= 1ULL << (bits_in_pointer
- 1);
594 v
&= mask
| (mask
- 1);
596 val
->ctype
= ssize_t_ctype
;
601 if (itype
->bit_size
< bits_in_pointer
)
602 index
= cast_to(index
, ssize_t_ctype
);
605 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
606 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
608 val
->ctype
= ssize_t_ctype
;
609 val
->value
= multiply
;
612 mul
->ctype
= ssize_t_ctype
;
622 static void examine_fn_arguments(struct symbol
*fn
);
624 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
626 const char *type_difference(struct ctype
*c1
, struct ctype
*c2
,
627 unsigned long mod1
, unsigned long mod2
)
629 unsigned long as1
= c1
->as
, as2
= c2
->as
;
630 struct symbol
*t1
= c1
->base_type
;
631 struct symbol
*t2
= c2
->base_type
;
632 int move1
= 1, move2
= 1;
633 mod1
|= c1
->modifiers
;
634 mod2
|= c2
->modifiers
;
638 struct symbol
*base1
= t1
->ctype
.base_type
;
639 struct symbol
*base2
= t2
->ctype
.base_type
;
642 * FIXME! Collect alignment and context too here!
645 if (t1
&& t1
->type
!= SYM_PTR
) {
646 mod1
|= t1
->ctype
.modifiers
;
653 if (t2
&& t2
->type
!= SYM_PTR
) {
654 mod2
|= t2
->ctype
.modifiers
;
663 return "different types";
665 if (t1
->type
== SYM_NODE
|| t1
->type
== SYM_ENUM
) {
673 if (t2
->type
== SYM_NODE
|| t2
->type
== SYM_ENUM
) {
683 if (type
!= t2
->type
)
684 return "different base types";
688 sparse_error(t1
->pos
,
689 "internal error: bad type in derived(%d)",
695 return "different base types";
697 /* XXX: we ought to compare sizes */
700 if (Waddress_space
&& as1
!= as2
)
701 return "different address spaces";
702 /* MOD_SPECIFIER is due to idiocy in parse.c */
703 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SPECIFIER
)
704 return "different modifiers";
705 /* we could be lazier here */
706 base1
= examine_pointer_target(t1
);
707 base2
= examine_pointer_target(t2
);
708 mod1
= t1
->ctype
.modifiers
;
710 mod2
= t2
->ctype
.modifiers
;
714 struct symbol
*arg1
, *arg2
;
717 if (Waddress_space
&& as1
!= as2
)
718 return "different address spaces";
719 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
720 return "different modifiers";
721 mod1
= t1
->ctype
.modifiers
;
723 mod2
= t2
->ctype
.modifiers
;
726 if (base1
->variadic
!= base2
->variadic
)
727 return "incompatible variadic arguments";
728 examine_fn_arguments(t1
);
729 examine_fn_arguments(t2
);
730 PREPARE_PTR_LIST(t1
->arguments
, arg1
);
731 PREPARE_PTR_LIST(t2
->arguments
, arg2
);
738 return "different argument counts";
739 diffstr
= type_difference(&arg1
->ctype
,
743 static char argdiff
[80];
744 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diffstr
);
751 FINISH_PTR_LIST(arg2
);
752 FINISH_PTR_LIST(arg1
);
756 if (Waddress_space
&& as1
!= as2
)
757 return "different address spaces";
759 return "different base types";
760 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
764 return "different type sizes";
765 if (diff
& ~MOD_SIGNEDNESS
)
766 return "different modifiers";
768 /* Differs in signedness only.. */
771 * Warn if both are explicitly signed ("unsigned" is obviously
772 * always explicit, and since we know one of them has to be
773 * unsigned, we check if the signed one was explicit).
775 if ((mod1
| mod2
) & MOD_EXPLICITLY_SIGNED
)
776 return "different explicit signedness";
779 * "char" matches both "unsigned char" and "signed char",
780 * so if the explicit test didn't trigger, then we should
781 * not warn about a char.
783 if (!(mod1
& MOD_CHAR
))
784 return "different signedness";
791 if (Waddress_space
&& as1
!= as2
)
792 return "different address spaces";
793 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
794 return "different modifiers";
798 static void bad_null(struct expression
*expr
)
800 if (Wnon_pointer_null
)
801 warning(expr
->pos
, "Using plain integer as NULL pointer");
804 static unsigned long target_qualifiers(struct symbol
*type
)
806 unsigned long mod
= type
->ctype
.modifiers
& MOD_IGN
;
807 if (type
->ctype
.base_type
&& type
->ctype
.base_type
->type
== SYM_ARRAY
)
812 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
)
814 const char *typediff
;
815 struct symbol
*ltype
, *rtype
;
816 struct expression
*l
= expr
->left
;
817 struct expression
*r
= expr
->right
;
818 struct symbol
*lbase
, *rbase
;
820 classify_type(degenerate(l
), <ype
);
821 classify_type(degenerate(r
), &rtype
);
823 lbase
= examine_pointer_target(ltype
);
824 rbase
= examine_pointer_target(rtype
);
825 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
826 target_qualifiers(rtype
),
827 target_qualifiers(ltype
));
829 expression_error(expr
, "subtraction of different types can't work (%s)", typediff
);
831 if (lbase
->type
== SYM_FN
) {
832 expression_error(expr
, "subtraction of functions? Share your drugs");
836 expr
->ctype
= ssize_t_ctype
;
837 if (lbase
->bit_size
> bits_in_char
) {
838 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
839 struct expression
*div
= expr
;
840 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
841 unsigned long value
= lbase
->bit_size
>> 3;
843 val
->ctype
= size_t_ctype
;
846 if (value
& (value
-1)) {
847 if (Wptr_subtraction_blows
)
848 warning(expr
->pos
, "potentially expensive pointer subtraction");
852 sub
->ctype
= ssize_t_ctype
;
861 return ssize_t_ctype
;
864 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
866 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
868 struct symbol
*ctype
;
873 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
874 warning(expr
->pos
, "assignment expression in conditional");
876 ctype
= evaluate_expression(expr
);
878 if (is_safe_type(ctype
))
879 warning(expr
->pos
, "testing a 'safe expression'");
885 static struct symbol
*evaluate_logical(struct expression
*expr
)
887 if (!evaluate_conditional(expr
->left
, 0))
889 if (!evaluate_conditional(expr
->right
, 0))
892 expr
->ctype
= &bool_ctype
;
894 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
900 static struct symbol
*evaluate_binop(struct expression
*expr
)
902 struct symbol
*ltype
, *rtype
, *ctype
;
903 int lclass
= classify_type(expr
->left
->ctype
, <ype
);
904 int rclass
= classify_type(expr
->right
->ctype
, &rtype
);
908 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
912 /* number op number */
913 if (lclass
& rclass
& TYPE_NUM
) {
914 if ((lclass
| rclass
) & TYPE_FLOAT
) {
916 case '+': case '-': case '*': case '/':
919 return bad_expr_type(expr
);
923 if (op
== SPECIAL_LEFTSHIFT
|| op
== SPECIAL_RIGHTSHIFT
) {
924 // shifts do integer promotions, but that's it.
925 unrestrict(expr
->left
, lclass
, <ype
);
926 unrestrict(expr
->right
, rclass
, &rtype
);
927 ctype
= ltype
= integer_promotion(ltype
);
928 rtype
= integer_promotion(rtype
);
930 // The rest do usual conversions
931 ltype
= usual_conversions(op
, expr
->left
, expr
->right
,
932 lclass
, rclass
, ltype
, rtype
);
933 ctype
= rtype
= ltype
;
936 expr
->left
= cast_to(expr
->left
, ltype
);
937 expr
->right
= cast_to(expr
->right
, rtype
);
942 /* pointer (+|-) integer */
943 if (lclass
& TYPE_PTR
&& is_int(rclass
) && (op
== '+' || op
== '-')) {
944 unrestrict(expr
->right
, rclass
, &rtype
);
945 return evaluate_ptr_add(expr
, degenerate(expr
->left
), rtype
);
948 /* integer + pointer */
949 if (rclass
& TYPE_PTR
&& is_int(lclass
) && op
== '+') {
950 struct expression
*index
= expr
->left
;
951 unrestrict(index
, lclass
, <ype
);
952 expr
->left
= expr
->right
;
954 return evaluate_ptr_add(expr
, degenerate(expr
->left
), ltype
);
957 /* pointer - pointer */
958 if (lclass
& rclass
& TYPE_PTR
&& expr
->op
== '-')
959 return evaluate_ptr_sub(expr
);
961 return bad_expr_type(expr
);
964 static struct symbol
*evaluate_comma(struct expression
*expr
)
966 expr
->ctype
= expr
->right
->ctype
;
967 expr
->flags
&= expr
->left
->flags
& expr
->right
->flags
;
971 static int modify_for_unsigned(int op
)
974 op
= SPECIAL_UNSIGNED_LT
;
976 op
= SPECIAL_UNSIGNED_GT
;
977 else if (op
== SPECIAL_LTE
)
978 op
= SPECIAL_UNSIGNED_LTE
;
979 else if (op
== SPECIAL_GTE
)
980 op
= SPECIAL_UNSIGNED_GTE
;
984 static inline int is_null_pointer_constant(struct expression
*e
)
986 if (e
->ctype
== &null_ctype
)
988 if (!(e
->flags
& Int_const_expr
))
990 return is_zero_constant(e
) ? 2 : 0;
993 static struct symbol
*evaluate_compare(struct expression
*expr
)
995 struct expression
*left
= expr
->left
, *right
= expr
->right
;
996 struct symbol
*ltype
, *rtype
, *lbase
, *rbase
;
997 int lclass
= classify_type(degenerate(left
), <ype
);
998 int rclass
= classify_type(degenerate(right
), &rtype
);
999 struct symbol
*ctype
;
1000 const char *typediff
;
1003 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
1008 if (is_type_type(ltype
) && is_type_type(rtype
))
1011 if (is_safe_type(left
->ctype
) || is_safe_type(right
->ctype
))
1012 warning(expr
->pos
, "testing a 'safe expression'");
1014 /* number on number */
1015 if (lclass
& rclass
& TYPE_NUM
) {
1016 ctype
= usual_conversions(expr
->op
, expr
->left
, expr
->right
,
1017 lclass
, rclass
, ltype
, rtype
);
1018 expr
->left
= cast_to(expr
->left
, ctype
);
1019 expr
->right
= cast_to(expr
->right
, ctype
);
1020 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
1021 expr
->op
= modify_for_unsigned(expr
->op
);
1025 /* at least one must be a pointer */
1026 if (!((lclass
| rclass
) & TYPE_PTR
))
1027 return bad_expr_type(expr
);
1029 /* equality comparisons can be with null pointer constants */
1030 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1031 int is_null1
= is_null_pointer_constant(left
);
1032 int is_null2
= is_null_pointer_constant(right
);
1037 if (is_null1
&& is_null2
) {
1038 int positive
= expr
->op
== SPECIAL_EQUAL
;
1039 expr
->type
= EXPR_VALUE
;
1040 expr
->value
= positive
;
1044 left
= cast_to(left
, rtype
);
1048 right
= cast_to(right
, ltype
);
1052 /* both should be pointers */
1053 if (!(lclass
& rclass
& TYPE_PTR
))
1054 return bad_expr_type(expr
);
1055 expr
->op
= modify_for_unsigned(expr
->op
);
1057 lbase
= examine_pointer_target(ltype
);
1058 rbase
= examine_pointer_target(rtype
);
1060 /* they also have special treatment for pointers to void */
1061 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1062 if (ltype
->ctype
.as
== rtype
->ctype
.as
) {
1063 if (lbase
== &void_ctype
) {
1064 right
= cast_to(right
, ltype
);
1067 if (rbase
== &void_ctype
) {
1068 left
= cast_to(left
, rtype
);
1074 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1075 target_qualifiers(rtype
),
1076 target_qualifiers(ltype
));
1080 expression_error(expr
, "incompatible types in comparison expression (%s)", typediff
);
1084 expr
->ctype
= &bool_ctype
;
1089 * NOTE! The degenerate case of "x ? : y", where we don't
1090 * have a true case, this will possibly promote "x" to the
1091 * same type as "y", and thus _change_ the conditional
1092 * test in the expression. But since promotion is "safe"
1093 * for testing, that's OK.
1095 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
1097 struct expression
**true;
1098 struct symbol
*ctype
, *ltype
, *rtype
, *lbase
, *rbase
;
1100 const char * typediff
;
1103 if (!evaluate_conditional(expr
->conditional
, 0))
1105 if (!evaluate_expression(expr
->cond_false
))
1108 ctype
= degenerate(expr
->conditional
);
1109 rtype
= degenerate(expr
->cond_false
);
1111 true = &expr
->conditional
;
1113 if (expr
->cond_true
) {
1114 if (!evaluate_expression(expr
->cond_true
))
1116 ltype
= degenerate(expr
->cond_true
);
1117 true = &expr
->cond_true
;
1121 int flags
= expr
->conditional
->flags
& Int_const_expr
;
1122 flags
&= (*true)->flags
& expr
->cond_false
->flags
;
1127 lclass
= classify_type(ltype
, <ype
);
1128 rclass
= classify_type(rtype
, &rtype
);
1129 if (lclass
& rclass
& TYPE_NUM
) {
1130 ctype
= usual_conversions('?', *true, expr
->cond_false
,
1131 lclass
, rclass
, ltype
, rtype
);
1132 *true = cast_to(*true, ctype
);
1133 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1137 if ((lclass
| rclass
) & TYPE_PTR
) {
1138 int is_null1
= is_null_pointer_constant(*true);
1139 int is_null2
= is_null_pointer_constant(expr
->cond_false
);
1141 if (is_null1
&& is_null2
) {
1142 *true = cast_to(*true, &ptr_ctype
);
1143 expr
->cond_false
= cast_to(expr
->cond_false
, &ptr_ctype
);
1147 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1150 *true = cast_to(*true, rtype
);
1154 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1156 bad_null(expr
->cond_false
);
1157 expr
->cond_false
= cast_to(expr
->cond_false
, ltype
);
1161 if (!(lclass
& rclass
& TYPE_PTR
)) {
1162 typediff
= "different types";
1165 /* OK, it's pointer on pointer */
1166 if (ltype
->ctype
.as
!= rtype
->ctype
.as
) {
1167 typediff
= "different address spaces";
1171 /* need to be lazier here */
1172 lbase
= examine_pointer_target(ltype
);
1173 rbase
= examine_pointer_target(rtype
);
1174 qual
= target_qualifiers(ltype
) | target_qualifiers(rtype
);
1176 if (lbase
== &void_ctype
) {
1177 /* XXX: pointers to function should warn here */
1182 if (rbase
== &void_ctype
) {
1183 /* XXX: pointers to function should warn here */
1187 /* XXX: that should be pointer to composite */
1189 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1196 /* void on void, struct on same struct, union on same union */
1197 if (ltype
== rtype
) {
1201 typediff
= "different base types";
1204 expression_error(expr
, "incompatible types in conditional expression (%s)", typediff
);
1208 expr
->ctype
= ctype
;
1212 if (qual
& ~ctype
->ctype
.modifiers
) {
1213 struct symbol
*sym
= alloc_symbol(ctype
->pos
, SYM_PTR
);
1215 sym
->ctype
.modifiers
|= qual
;
1218 *true = cast_to(*true, ctype
);
1219 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1223 /* FP assignments can not do modulo or bit operations */
1224 static int compatible_float_op(int op
)
1226 return op
== SPECIAL_ADD_ASSIGN
||
1227 op
== SPECIAL_SUB_ASSIGN
||
1228 op
== SPECIAL_MUL_ASSIGN
||
1229 op
== SPECIAL_DIV_ASSIGN
;
1232 static int evaluate_assign_op(struct expression
*expr
)
1234 struct symbol
*target
= expr
->left
->ctype
;
1235 struct symbol
*source
= expr
->right
->ctype
;
1236 struct symbol
*t
, *s
;
1237 int tclass
= classify_type(target
, &t
);
1238 int sclass
= classify_type(source
, &s
);
1241 if (tclass
& sclass
& TYPE_NUM
) {
1242 if (tclass
& TYPE_FLOAT
&& !compatible_float_op(op
)) {
1243 expression_error(expr
, "invalid assignment");
1246 if (tclass
& TYPE_RESTRICT
) {
1247 if (!restricted_binop(op
, t
)) {
1248 expression_error(expr
, "bad restricted assignment");
1251 /* allowed assignments unfoul */
1252 if (sclass
& TYPE_FOULED
&& s
->ctype
.base_type
== t
)
1254 if (!restricted_value(expr
->right
, t
))
1256 } else if (!(sclass
& TYPE_RESTRICT
))
1258 /* source and target would better be identical restricted */
1261 warning(expr
->pos
, "invalid restricted assignment");
1262 expr
->right
= cast_to(expr
->right
, target
);
1265 if (tclass
& TYPE_PTR
&& is_int(sclass
)) {
1266 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1267 unrestrict(expr
->right
, sclass
, &s
);
1268 evaluate_ptr_add(expr
, target
, s
);
1271 expression_error(expr
, "invalid pointer assignment");
1275 expression_error(expr
, "invalid assignment");
1279 expr
->right
= cast_to(expr
->right
, target
);
1283 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1284 struct expression
**rp
, const char *where
)
1286 const char *typediff
;
1287 struct symbol
*source
= degenerate(*rp
);
1288 struct symbol
*t
, *s
;
1289 int tclass
= classify_type(target
, &t
);
1290 int sclass
= classify_type(source
, &s
);
1292 if (tclass
& sclass
& TYPE_NUM
) {
1293 if (tclass
& TYPE_RESTRICT
) {
1294 /* allowed assignments unfoul */
1295 if (sclass
& TYPE_FOULED
&& s
->ctype
.base_type
== t
)
1297 if (!restricted_value(*rp
, target
))
1301 } else if (!(sclass
& TYPE_RESTRICT
))
1303 typediff
= "different base types";
1307 if (tclass
== TYPE_PTR
) {
1308 unsigned long mod1
, mod2
;
1309 struct symbol
*b1
, *b2
;
1310 // NULL pointer is always OK
1311 int is_null
= is_null_pointer_constant(*rp
);
1317 if (!(sclass
& TYPE_PTR
)) {
1318 typediff
= "different base types";
1321 b1
= examine_pointer_target(t
);
1322 b2
= examine_pointer_target(s
);
1323 mod1
= target_qualifiers(t
);
1324 mod2
= target_qualifiers(s
);
1325 if (b1
== &void_ctype
|| b2
== &void_ctype
) {
1327 * assignments to/from void * are OK, provided that
1328 * we do not remove qualifiers from pointed to [C]
1329 * or mix address spaces [sparse].
1331 if (t
->ctype
.as
!= s
->ctype
.as
) {
1332 typediff
= "different address spaces";
1336 typediff
= "different modifiers";
1341 /* It's OK if the target is more volatile or const than the source */
1342 typediff
= type_difference(&t
->ctype
, &s
->ctype
, 0, mod1
);
1348 if ((tclass
& TYPE_COMPOUND
) && s
== t
)
1351 if (tclass
& TYPE_NUM
) {
1352 /* XXX: need to turn into comparison with NULL */
1353 if (t
== &bool_ctype
&& (sclass
& TYPE_PTR
))
1355 typediff
= "different base types";
1358 typediff
= "invalid types";
1361 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1362 info(expr
->pos
, " expected %s", show_typename(target
));
1363 info(expr
->pos
, " got %s", show_typename(source
));
1364 *rp
= cast_to(*rp
, target
);
1367 *rp
= cast_to(*rp
, target
);
1371 static void mark_assigned(struct expression
*expr
)
1377 switch (expr
->type
) {
1382 if (sym
->type
!= SYM_NODE
)
1384 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1388 mark_assigned(expr
->left
);
1389 mark_assigned(expr
->right
);
1392 case EXPR_FORCE_CAST
:
1393 mark_assigned(expr
->cast_expression
);
1396 mark_assigned(expr
->base
);
1404 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1406 if (type
->ctype
.modifiers
& MOD_CONST
)
1407 expression_error(left
, "assignment to const expression");
1409 /* We know left is an lvalue, so it's a "preop-*" */
1410 mark_assigned(left
->unop
);
1413 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1415 struct expression
*left
= expr
->left
;
1416 struct expression
*where
= expr
;
1417 struct symbol
*ltype
;
1419 if (!lvalue_expression(left
)) {
1420 expression_error(expr
, "not an lvalue");
1424 ltype
= left
->ctype
;
1426 if (expr
->op
!= '=') {
1427 if (!evaluate_assign_op(expr
))
1430 if (!compatible_assignment_types(where
, ltype
, &expr
->right
, "assignment"))
1434 evaluate_assign_to(left
, ltype
);
1436 expr
->ctype
= ltype
;
1440 static void examine_fn_arguments(struct symbol
*fn
)
1444 FOR_EACH_PTR(fn
->arguments
, s
) {
1445 struct symbol
*arg
= evaluate_symbol(s
);
1446 /* Array/function arguments silently degenerate into pointers */
1452 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1453 if (arg
->type
== SYM_ARRAY
)
1454 ptr
->ctype
= arg
->ctype
;
1456 ptr
->ctype
.base_type
= arg
;
1457 ptr
->ctype
.as
|= s
->ctype
.as
;
1458 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
& MOD_PTRINHERIT
;
1460 s
->ctype
.base_type
= ptr
;
1462 s
->ctype
.modifiers
&= ~MOD_PTRINHERIT
;
1465 examine_symbol_type(s
);
1472 } END_FOR_EACH_PTR(s
);
1475 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1477 /* Take the modifiers of the pointer, and apply them to the member */
1478 mod
|= sym
->ctype
.modifiers
;
1479 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1480 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1482 newsym
->ctype
.as
= as
;
1483 newsym
->ctype
.modifiers
= mod
;
1489 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1491 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1492 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1494 node
->ctype
.base_type
= ptr
;
1495 ptr
->bit_size
= bits_in_pointer
;
1496 ptr
->ctype
.alignment
= pointer_alignment
;
1498 node
->bit_size
= bits_in_pointer
;
1499 node
->ctype
.alignment
= pointer_alignment
;
1502 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1503 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1504 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1506 if (sym
->type
== SYM_NODE
) {
1507 ptr
->ctype
.as
|= sym
->ctype
.as
;
1508 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1509 sym
= sym
->ctype
.base_type
;
1511 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1512 ptr
->ctype
.as
|= sym
->ctype
.as
;
1513 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1514 sym
= sym
->ctype
.base_type
;
1516 ptr
->ctype
.base_type
= sym
;
1521 /* Arrays degenerate into pointers on pointer arithmetic */
1522 static struct symbol
*degenerate(struct expression
*expr
)
1524 struct symbol
*ctype
, *base
;
1528 ctype
= expr
->ctype
;
1531 base
= examine_symbol_type(ctype
);
1532 if (ctype
->type
== SYM_NODE
)
1533 base
= ctype
->ctype
.base_type
;
1535 * Arrays degenerate into pointers to the entries, while
1536 * functions degenerate into pointers to themselves.
1537 * If array was part of non-lvalue compound, we create a copy
1538 * of that compound first and then act as if we were dealing with
1539 * the corresponding field in there.
1541 switch (base
->type
) {
1543 if (expr
->type
== EXPR_SLICE
) {
1544 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1545 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1547 a
->ctype
.base_type
= expr
->base
->ctype
;
1548 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1549 a
->array_size
= expr
->base
->ctype
->array_size
;
1551 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1553 e0
->ctype
= &lazy_ptr_ctype
;
1555 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1558 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1560 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1562 e2
->right
= expr
->base
;
1564 e2
->ctype
= expr
->base
->ctype
;
1566 if (expr
->r_bitpos
) {
1567 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1570 e3
->right
= alloc_const_expression(expr
->pos
,
1571 expr
->r_bitpos
>> 3);
1572 e3
->ctype
= &lazy_ptr_ctype
;
1577 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1580 e4
->ctype
= &lazy_ptr_ctype
;
1583 expr
->type
= EXPR_PREOP
;
1587 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1588 expression_error(expr
, "strange non-value function or array");
1591 *expr
= *expr
->unop
;
1592 ctype
= create_pointer(expr
, ctype
, 1);
1593 expr
->ctype
= ctype
;
1600 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1602 struct expression
*op
= expr
->unop
;
1603 struct symbol
*ctype
;
1605 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1606 expression_error(expr
, "not addressable");
1613 if (expr
->type
== EXPR_SYMBOL
) {
1614 struct symbol
*sym
= expr
->symbol
;
1615 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1619 * symbol expression evaluation is lazy about the type
1620 * of the sub-expression, so we may have to generate
1621 * the type here if so..
1623 if (expr
->ctype
== &lazy_ptr_ctype
) {
1624 ctype
= create_pointer(expr
, ctype
, 0);
1625 expr
->ctype
= ctype
;
1631 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1633 struct expression
*op
= expr
->unop
;
1634 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1636 /* Simplify: *&(expr) => (expr) */
1637 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1643 /* Dereferencing a node drops all the node information. */
1644 if (ctype
->type
== SYM_NODE
)
1645 ctype
= ctype
->ctype
.base_type
;
1647 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1648 target
= ctype
->ctype
.base_type
;
1650 switch (ctype
->type
) {
1652 expression_error(expr
, "cannot dereference this type");
1655 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1656 merge_type(node
, ctype
);
1660 if (!lvalue_expression(op
)) {
1661 expression_error(op
, "non-lvalue array??");
1665 /* Do the implied "addressof" on the array */
1669 * When an array is dereferenced, we need to pick
1670 * up the attributes of the original node too..
1672 merge_type(node
, op
->ctype
);
1673 merge_type(node
, ctype
);
1677 node
->bit_size
= target
->bit_size
;
1678 node
->array_size
= target
->array_size
;
1685 * Unary post-ops: x++ and x--
1687 static struct symbol
*evaluate_postop(struct expression
*expr
)
1689 struct expression
*op
= expr
->unop
;
1690 struct symbol
*ctype
= op
->ctype
;
1692 if (!lvalue_expression(expr
->unop
)) {
1693 expression_error(expr
, "need lvalue expression for ++/--");
1696 if (is_restricted_type(ctype
) && restricted_unop(expr
->op
, &ctype
)) {
1697 expression_error(expr
, "bad operation on restricted");
1699 } else if (is_fouled_type(ctype
) && restricted_unop(expr
->op
, &ctype
)) {
1700 expression_error(expr
, "bad operation on restricted");
1704 evaluate_assign_to(op
, ctype
);
1706 expr
->ctype
= ctype
;
1708 if (is_ptr_type(ctype
))
1709 expr
->op_value
= ptr_object_size(ctype
) >> 3;
1714 static struct symbol
*evaluate_sign(struct expression
*expr
)
1716 struct symbol
*ctype
= expr
->unop
->ctype
;
1717 int class = classify_type(ctype
, &ctype
);
1718 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1720 /* should be an arithmetic type */
1721 if (!(class & TYPE_NUM
))
1722 return bad_expr_type(expr
);
1723 if (!(class & (TYPE_FLOAT
|TYPE_RESTRICT
))) {
1724 struct symbol
*rtype
= integer_promotion(ctype
);
1725 expr
->unop
= cast_to(expr
->unop
, rtype
);
1727 } else if ((class & TYPE_FLOAT
) && expr
->op
!= '~') {
1728 /* no conversions needed */
1729 } else if ((class & TYPE_RESTRICT
) && !restricted_unop(expr
->op
, &ctype
)) {
1730 /* no conversions needed */
1732 return bad_expr_type(expr
);
1734 if (expr
->op
== '+')
1735 *expr
= *expr
->unop
;
1736 expr
->ctype
= ctype
;
1740 static struct symbol
*evaluate_preop(struct expression
*expr
)
1742 struct symbol
*ctype
= expr
->unop
->ctype
;
1746 *expr
= *expr
->unop
;
1752 return evaluate_sign(expr
);
1755 return evaluate_dereference(expr
);
1758 return evaluate_addressof(expr
);
1760 case SPECIAL_INCREMENT
:
1761 case SPECIAL_DECREMENT
:
1763 * From a type evaluation standpoint the preops are
1764 * the same as the postops
1766 return evaluate_postop(expr
);
1769 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1771 if (is_safe_type(ctype
))
1772 warning(expr
->pos
, "testing a 'safe expression'");
1773 if (is_float_type(ctype
)) {
1774 struct expression
*arg
= expr
->unop
;
1775 expr
->type
= EXPR_BINOP
;
1776 expr
->op
= SPECIAL_EQUAL
;
1778 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1779 expr
->right
->ctype
= ctype
;
1780 expr
->right
->fvalue
= 0;
1781 } else if (is_fouled_type(ctype
)) {
1782 warning(expr
->pos
, "restricted degrades to integer");
1784 ctype
= &bool_ctype
;
1790 expr
->ctype
= ctype
;
1794 static struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1796 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1797 struct ptr_list
*list
= head
;
1803 for (i
= 0; i
< list
->nr
; i
++) {
1804 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1806 if (sym
->ident
!= ident
)
1808 *offset
= sym
->offset
;
1811 struct symbol
*ctype
= sym
->ctype
.base_type
;
1815 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1817 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1820 *offset
+= sym
->offset
;
1824 } while ((list
= list
->next
) != head
);
1828 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1830 struct expression
*add
;
1833 * Create a new add-expression
1835 * NOTE! Even if we just add zero, we need a new node
1836 * for the member pointer, since it has a different
1837 * type than the original pointer. We could make that
1838 * be just a cast, but the fact is, a node is a node,
1839 * so we might as well just do the "add zero" here.
1841 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1844 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1845 add
->right
->ctype
= &int_ctype
;
1846 add
->right
->value
= offset
;
1849 * The ctype of the pointer will be lazily evaluated if
1850 * we ever take the address of this member dereference..
1852 add
->ctype
= &lazy_ptr_ctype
;
1856 /* structure/union dereference */
1857 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1860 struct symbol
*ctype
, *member
;
1861 struct expression
*deref
= expr
->deref
, *add
;
1862 struct ident
*ident
= expr
->member
;
1866 if (!evaluate_expression(deref
))
1869 expression_error(expr
, "bad member name");
1873 ctype
= deref
->ctype
;
1874 address_space
= ctype
->ctype
.as
;
1875 mod
= ctype
->ctype
.modifiers
;
1876 if (ctype
->type
== SYM_NODE
) {
1877 ctype
= ctype
->ctype
.base_type
;
1878 address_space
|= ctype
->ctype
.as
;
1879 mod
|= ctype
->ctype
.modifiers
;
1881 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1882 expression_error(expr
, "expected structure or union");
1885 examine_symbol_type(ctype
);
1887 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1889 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1890 const char *name
= "<unnamed>";
1893 name
= ctype
->ident
->name
;
1894 namelen
= ctype
->ident
->len
;
1896 if (ctype
->symbol_list
)
1897 expression_error(expr
, "no member '%s' in %s %.*s",
1898 show_ident(ident
), type
, namelen
, name
);
1900 expression_error(expr
, "using member '%s' in "
1901 "incomplete %s %.*s", show_ident(ident
),
1902 type
, namelen
, name
);
1907 * The member needs to take on the address space and modifiers of
1908 * the "parent" type.
1910 member
= convert_to_as_mod(member
, address_space
, mod
);
1911 ctype
= get_base_type(member
);
1913 if (!lvalue_expression(deref
)) {
1914 if (deref
->type
!= EXPR_SLICE
) {
1918 expr
->base
= deref
->base
;
1919 expr
->r_bitpos
= deref
->r_bitpos
;
1921 expr
->r_bitpos
+= offset
<< 3;
1922 expr
->type
= EXPR_SLICE
;
1923 expr
->r_nrbits
= member
->bit_size
;
1924 expr
->r_bitpos
+= member
->bit_offset
;
1925 expr
->ctype
= member
;
1929 deref
= deref
->unop
;
1930 expr
->deref
= deref
;
1932 add
= evaluate_offset(deref
, offset
);
1933 expr
->type
= EXPR_PREOP
;
1937 expr
->ctype
= member
;
1941 static int is_promoted(struct expression
*expr
)
1944 switch (expr
->type
) {
1947 case EXPR_CONDITIONAL
:
1971 static struct symbol
*evaluate_cast(struct expression
*);
1973 static struct symbol
*evaluate_type_information(struct expression
*expr
)
1975 struct symbol
*sym
= expr
->cast_type
;
1977 sym
= evaluate_expression(expr
->cast_expression
);
1981 * Expressions of restricted types will possibly get
1982 * promoted - check that here
1984 if (is_restricted_type(sym
)) {
1985 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
1987 } else if (is_fouled_type(sym
)) {
1991 examine_symbol_type(sym
);
1992 if (is_bitfield_type(sym
)) {
1993 expression_error(expr
, "trying to examine bitfield type");
1999 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
2001 struct symbol
*type
;
2004 type
= evaluate_type_information(expr
);
2008 size
= type
->bit_size
;
2009 if ((size
< 0) || (size
& 7))
2010 expression_error(expr
, "cannot size expression");
2011 expr
->type
= EXPR_VALUE
;
2012 expr
->value
= size
>> 3;
2014 expr
->ctype
= size_t_ctype
;
2015 return size_t_ctype
;
2018 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
2020 struct symbol
*type
;
2023 type
= evaluate_type_information(expr
);
2027 if (type
->type
== SYM_NODE
)
2028 type
= type
->ctype
.base_type
;
2031 switch (type
->type
) {
2035 type
= get_base_type(type
);
2039 expression_error(expr
, "expected pointer expression");
2042 size
= type
->bit_size
;
2045 expr
->type
= EXPR_VALUE
;
2046 expr
->value
= size
>> 3;
2048 expr
->ctype
= size_t_ctype
;
2049 return size_t_ctype
;
2052 static struct symbol
*evaluate_alignof(struct expression
*expr
)
2054 struct symbol
*type
;
2056 type
= evaluate_type_information(expr
);
2060 expr
->type
= EXPR_VALUE
;
2061 expr
->value
= type
->ctype
.alignment
;
2063 expr
->ctype
= size_t_ctype
;
2064 return size_t_ctype
;
2067 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
2069 struct expression
*expr
;
2070 struct symbol_list
*argument_types
= fn
->arguments
;
2071 struct symbol
*argtype
;
2074 PREPARE_PTR_LIST(argument_types
, argtype
);
2075 FOR_EACH_PTR (head
, expr
) {
2076 struct expression
**p
= THIS_ADDRESS(expr
);
2077 struct symbol
*ctype
, *target
;
2078 ctype
= evaluate_expression(expr
);
2085 struct symbol
*type
;
2086 int class = classify_type(ctype
, &type
);
2087 if (is_int(class)) {
2088 *p
= cast_to(expr
, integer_promotion(type
));
2089 } else if (class & TYPE_FLOAT
) {
2090 unsigned long mod
= type
->ctype
.modifiers
;
2091 if (!(mod
& (MOD_LONG
|MOD_LONGLONG
)))
2092 *p
= cast_to(expr
, &double_ctype
);
2093 } else if (class & TYPE_PTR
) {
2094 if (expr
->ctype
== &null_ctype
)
2095 *p
= cast_to(expr
, &ptr_ctype
);
2100 static char where
[30];
2101 examine_symbol_type(target
);
2102 sprintf(where
, "argument %d", i
);
2103 compatible_assignment_types(expr
, target
, p
, where
);
2107 NEXT_PTR_LIST(argtype
);
2108 } END_FOR_EACH_PTR(expr
);
2109 FINISH_PTR_LIST(argtype
);
2113 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
2117 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
2118 if (sym
->ident
== ident
)
2120 } END_FOR_EACH_PTR(sym
);
2124 static void convert_index(struct expression
*e
)
2126 struct expression
*child
= e
->idx_expression
;
2127 unsigned from
= e
->idx_from
;
2128 unsigned to
= e
->idx_to
+ 1;
2130 e
->init_offset
= from
* (e
->ctype
->bit_size
>>3);
2131 e
->init_nr
= to
- from
;
2132 e
->init_expr
= child
;
2135 static void convert_ident(struct expression
*e
)
2137 struct expression
*child
= e
->ident_expression
;
2138 struct symbol
*sym
= e
->field
;
2140 e
->init_offset
= sym
->offset
;
2142 e
->init_expr
= child
;
2145 static void convert_designators(struct expression
*e
)
2148 if (e
->type
== EXPR_INDEX
)
2150 else if (e
->type
== EXPR_IDENTIFIER
)
2158 static void excess(struct expression
*e
, const char *s
)
2160 warning(e
->pos
, "excessive elements in %s initializer", s
);
2164 * implicit designator for the first element
2166 static struct expression
*first_subobject(struct symbol
*ctype
, int class,
2167 struct expression
**v
)
2169 struct expression
*e
= *v
, *new;
2171 if (ctype
->type
== SYM_NODE
)
2172 ctype
= ctype
->ctype
.base_type
;
2174 if (class & TYPE_PTR
) { /* array */
2175 if (!ctype
->bit_size
)
2177 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2178 new->idx_expression
= e
;
2179 new->ctype
= ctype
->ctype
.base_type
;
2181 struct symbol
*field
, *p
;
2182 PREPARE_PTR_LIST(ctype
->symbol_list
, p
);
2183 while (p
&& !p
->ident
&& is_bitfield_type(p
))
2189 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2190 new->ident_expression
= e
;
2191 new->field
= new->ctype
= field
;
2198 * sanity-check explicit designators; return the innermost one or NULL
2199 * in case of error. Assign types.
2201 static struct expression
*check_designators(struct expression
*e
,
2202 struct symbol
*ctype
)
2204 struct expression
*last
= NULL
;
2207 if (ctype
->type
== SYM_NODE
)
2208 ctype
= ctype
->ctype
.base_type
;
2209 if (e
->type
== EXPR_INDEX
) {
2210 struct symbol
*type
;
2211 if (ctype
->type
!= SYM_ARRAY
) {
2212 err
= "array index in non-array";
2215 type
= ctype
->ctype
.base_type
;
2216 if (ctype
->bit_size
>= 0 && type
->bit_size
>= 0) {
2217 unsigned offset
= e
->idx_to
* type
->bit_size
;
2218 if (offset
>= ctype
->bit_size
) {
2219 err
= "index out of bounds in";
2223 e
->ctype
= ctype
= type
;
2226 if (!e
->idx_expression
) {
2230 e
= e
->idx_expression
;
2231 } else if (e
->type
== EXPR_IDENTIFIER
) {
2232 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2233 err
= "field name not in struct or union";
2236 ctype
= find_struct_ident(ctype
, e
->expr_ident
);
2238 err
= "unknown field name in";
2241 e
->field
= e
->ctype
= ctype
;
2243 if (!e
->ident_expression
) {
2247 e
= e
->ident_expression
;
2248 } else if (e
->type
== EXPR_POS
) {
2249 err
= "internal front-end error: EXPR_POS in";
2254 expression_error(e
, "%s initializer", err
);
2259 * choose the next subobject to initialize.
2261 * Get designators for next element, switch old ones to EXPR_POS.
2262 * Return the resulting expression or NULL if we'd run out of subobjects.
2263 * The innermost designator is returned in *v. Designators in old
2264 * are assumed to be already sanity-checked.
2266 static struct expression
*next_designators(struct expression
*old
,
2267 struct symbol
*ctype
,
2268 struct expression
*e
, struct expression
**v
)
2270 struct expression
*new = NULL
;
2274 if (old
->type
== EXPR_INDEX
) {
2275 struct expression
*copy
;
2278 copy
= next_designators(old
->idx_expression
,
2281 n
= old
->idx_to
+ 1;
2282 if (n
* old
->ctype
->bit_size
== ctype
->bit_size
) {
2287 *v
= new = alloc_expression(e
->pos
, EXPR_INDEX
);
2290 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2293 new->idx_from
= new->idx_to
= n
;
2294 new->idx_expression
= copy
;
2295 new->ctype
= old
->ctype
;
2297 } else if (old
->type
== EXPR_IDENTIFIER
) {
2298 struct expression
*copy
;
2299 struct symbol
*field
;
2301 copy
= next_designators(old
->ident_expression
,
2304 field
= old
->field
->next_subobject
;
2310 *v
= new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2313 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2317 new->expr_ident
= field
->ident
;
2318 new->ident_expression
= copy
;
2325 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2326 int class, struct symbol
*ctype
);
2329 * deal with traversing subobjects [6.7.8(17,18,20)]
2331 static void handle_list_initializer(struct expression
*expr
,
2332 int class, struct symbol
*ctype
)
2334 struct expression
*e
, *last
= NULL
, *top
= NULL
, *next
;
2337 FOR_EACH_PTR(expr
->expr_list
, e
) {
2338 struct expression
**v
;
2339 struct symbol
*type
;
2342 if (e
->type
!= EXPR_INDEX
&& e
->type
!= EXPR_IDENTIFIER
) {
2345 last
= first_subobject(ctype
, class, &top
);
2347 last
= next_designators(last
, ctype
, e
, &top
);
2350 excess(e
, class & TYPE_PTR
? "array" :
2352 DELETE_CURRENT_PTR(e
);
2356 warning(e
->pos
, "advancing past deep designator");
2359 REPLACE_CURRENT_PTR(e
, last
);
2361 next
= check_designators(e
, ctype
);
2363 DELETE_CURRENT_PTR(e
);
2367 /* deeper than one designator? */
2369 convert_designators(last
);
2374 lclass
= classify_type(top
->ctype
, &type
);
2375 if (top
->type
== EXPR_INDEX
)
2376 v
= &top
->idx_expression
;
2378 v
= &top
->ident_expression
;
2380 if (handle_simple_initializer(v
, 1, lclass
, top
->ctype
))
2383 if (!(lclass
& TYPE_COMPOUND
)) {
2384 warning(e
->pos
, "bogus scalar initializer");
2385 DELETE_CURRENT_PTR(e
);
2389 next
= first_subobject(type
, lclass
, v
);
2391 warning(e
->pos
, "missing braces around initializer");
2396 DELETE_CURRENT_PTR(e
);
2397 excess(e
, lclass
& TYPE_PTR
? "array" : "struct or union");
2399 } END_FOR_EACH_PTR(e
);
2401 convert_designators(last
);
2402 expr
->ctype
= ctype
;
2405 static int is_string_literal(struct expression
**v
)
2407 struct expression
*e
= *v
;
2408 while (e
&& e
->type
== EXPR_PREOP
&& e
->op
== '(')
2410 if (!e
|| e
->type
!= EXPR_STRING
)
2412 if (e
!= *v
&& Wparen_string
)
2414 "array initialized from parenthesized string constant");
2420 * We want a normal expression, possibly in one layer of braces. Warn
2421 * if the latter happens inside a list (it's legal, but likely to be
2422 * an effect of screwup). In case of anything not legal, we are definitely
2423 * having an effect of screwup, so just fail and let the caller warn.
2425 static struct expression
*handle_scalar(struct expression
*e
, int nested
)
2427 struct expression
*v
= NULL
, *p
;
2431 if (e
->type
!= EXPR_INITIALIZER
)
2434 FOR_EACH_PTR(e
->expr_list
, p
) {
2438 } END_FOR_EACH_PTR(p
);
2442 case EXPR_INITIALIZER
:
2444 case EXPR_IDENTIFIER
:
2450 warning(e
->pos
, "braces around scalar initializer");
2455 * deal with the cases that don't care about subobjects:
2456 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2457 * character array <- string literal, possibly in braces [6.7.8(14)]
2458 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2459 * compound type <- initializer list in braces [6.7.8(16)]
2460 * The last one punts to handle_list_initializer() which, in turn will call
2461 * us for individual elements of the list.
2463 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2464 * the lack of support of wide char stuff in general.
2466 * One note: we need to take care not to evaluate a string literal until
2467 * we know that we *will* handle it right here. Otherwise we would screw
2468 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2469 * { "string", ...} - we need to preserve that string literal recognizable
2470 * until we dig into the inner struct.
2472 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2473 int class, struct symbol
*ctype
)
2475 int is_string
= is_string_type(ctype
);
2476 struct expression
*e
= *ep
, *p
;
2477 struct symbol
*type
;
2483 if (!(class & TYPE_COMPOUND
)) {
2484 e
= handle_scalar(e
, nested
);
2488 if (!evaluate_expression(e
))
2490 compatible_assignment_types(e
, ctype
, ep
, "initializer");
2495 * sublist; either a string, or we dig in; the latter will deal with
2496 * pathologies, so we don't need anything fancy here.
2498 if (e
->type
== EXPR_INITIALIZER
) {
2500 struct expression
*v
= NULL
;
2503 FOR_EACH_PTR(e
->expr_list
, p
) {
2507 } END_FOR_EACH_PTR(p
);
2508 if (count
== 1 && is_string_literal(&v
)) {
2513 handle_list_initializer(e
, class, ctype
);
2518 if (is_string_literal(&e
)) {
2519 /* either we are doing array of char, or we'll have to dig in */
2526 /* struct or union can be initialized by compatible */
2527 if (class != TYPE_COMPOUND
)
2529 type
= evaluate_expression(e
);
2532 if (ctype
->type
== SYM_NODE
)
2533 ctype
= ctype
->ctype
.base_type
;
2534 if (type
->type
== SYM_NODE
)
2535 type
= type
->ctype
.base_type
;
2541 p
= alloc_expression(e
->pos
, EXPR_STRING
);
2543 type
= evaluate_expression(p
);
2544 if (ctype
->bit_size
!= -1 &&
2545 ctype
->bit_size
+ bits_in_char
< type
->bit_size
) {
2547 "too long initializer-string for array of char");
2553 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2555 struct symbol
*type
;
2556 int class = classify_type(ctype
, &type
);
2557 if (!handle_simple_initializer(ep
, 0, class, ctype
))
2558 expression_error(*ep
, "invalid initializer");
2561 static struct symbol
*evaluate_cast(struct expression
*expr
)
2563 struct expression
*target
= expr
->cast_expression
;
2564 struct symbol
*ctype
;
2565 struct symbol
*t1
, *t2
;
2567 int as1
= 0, as2
= 0;
2573 * Special case: a cast can be followed by an
2574 * initializer, in which case we need to pass
2575 * the type value down to that initializer rather
2576 * than trying to evaluate it as an expression
2578 * A more complex case is when the initializer is
2579 * dereferenced as part of a post-fix expression.
2580 * We need to produce an expression that can be dereferenced.
2582 if (target
->type
== EXPR_INITIALIZER
) {
2583 struct symbol
*sym
= expr
->cast_type
;
2584 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2586 sym
->initializer
= target
;
2587 evaluate_symbol(sym
);
2589 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2592 expr
->type
= EXPR_PREOP
;
2600 ctype
= examine_symbol_type(expr
->cast_type
);
2601 expr
->ctype
= ctype
;
2602 expr
->cast_type
= ctype
;
2604 evaluate_expression(target
);
2607 class1
= classify_type(ctype
, &t1
);
2609 /* cast to non-integer type -> not an integer constant expression */
2610 if (!is_int(class1
))
2612 /* if argument turns out to be not an integer constant expression *and*
2613 it was not a floating literal to start with -> too bad */
2614 else if (expr
->flags
== Int_const_expr
&&
2615 !(target
->flags
& Int_const_expr
))
2618 * You can always throw a value away by casting to
2619 * "void" - that's an implicit "force". Note that
2620 * the same is _not_ true of "void *".
2622 if (t1
== &void_ctype
)
2625 if (class1
& TYPE_COMPOUND
)
2626 warning(expr
->pos
, "cast to non-scalar");
2630 expression_error(expr
, "cast from unknown type");
2633 class2
= classify_type(t2
, &t2
);
2635 if (class2
& TYPE_COMPOUND
)
2636 warning(expr
->pos
, "cast from non-scalar");
2638 if (expr
->type
== EXPR_FORCE_CAST
)
2641 /* allowed cast unfouls */
2642 if (class2
& TYPE_FOULED
)
2643 t2
= t2
->ctype
.base_type
;
2646 if (class1
& TYPE_RESTRICT
)
2647 warning(expr
->pos
, "cast to restricted type");
2648 if (class2
& TYPE_RESTRICT
)
2649 warning(expr
->pos
, "cast from restricted type");
2652 if (t1
== &ulong_ctype
)
2654 else if (class1
== TYPE_PTR
) {
2655 examine_pointer_target(t1
);
2659 if (t2
== &ulong_ctype
)
2661 else if (class2
== TYPE_PTR
) {
2662 examine_pointer_target(t2
);
2666 if (!as1
&& as2
> 0)
2667 warning(expr
->pos
, "cast removes address space of expression");
2668 if (as1
> 0 && as2
> 0 && as1
!= as2
)
2669 warning(expr
->pos
, "cast between address spaces (<asn:%d>-><asn:%d>)", as2
, as1
);
2670 if (as1
> 0 && !as2
&&
2671 !is_null_pointer_constant(target
) && Wcast_to_address_space
)
2673 "cast adds address space to expression (<asn:%d>)", as1
);
2675 if (!(t1
->ctype
.modifiers
& MOD_PTRINHERIT
) && class1
== TYPE_PTR
&&
2676 !as1
&& (target
->flags
& Int_const_expr
)) {
2677 if (t1
->ctype
.base_type
== &void_ctype
) {
2678 if (is_zero_constant(target
)) {
2680 expr
->type
= EXPR_VALUE
;
2681 expr
->ctype
= &null_ctype
;
2692 * Evaluate a call expression with a symbol. This
2693 * should expand inline functions, and evaluate
2696 static int evaluate_symbol_call(struct expression
*expr
)
2698 struct expression
*fn
= expr
->fn
;
2699 struct symbol
*ctype
= fn
->ctype
;
2701 if (fn
->type
!= EXPR_PREOP
)
2704 if (ctype
->op
&& ctype
->op
->evaluate
)
2705 return ctype
->op
->evaluate(expr
);
2707 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2709 struct symbol
*curr
= current_fn
;
2710 current_fn
= ctype
->ctype
.base_type
;
2712 ret
= inline_function(expr
, ctype
);
2714 /* restore the old function */
2722 static struct symbol
*evaluate_call(struct expression
*expr
)
2725 struct symbol
*ctype
, *sym
;
2726 struct expression
*fn
= expr
->fn
;
2727 struct expression_list
*arglist
= expr
->args
;
2729 if (!evaluate_expression(fn
))
2731 sym
= ctype
= fn
->ctype
;
2732 if (ctype
->type
== SYM_NODE
)
2733 ctype
= ctype
->ctype
.base_type
;
2734 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
2735 ctype
= get_base_type(ctype
);
2737 examine_fn_arguments(ctype
);
2738 if (sym
->type
== SYM_NODE
&& fn
->type
== EXPR_PREOP
&&
2739 sym
->op
&& sym
->op
->args
) {
2740 if (!sym
->op
->args(expr
))
2743 if (!evaluate_arguments(sym
, ctype
, arglist
))
2745 if (ctype
->type
!= SYM_FN
) {
2746 expression_error(expr
, "not a function %s",
2747 show_ident(sym
->ident
));
2750 args
= expression_list_size(expr
->args
);
2751 fnargs
= symbol_list_size(ctype
->arguments
);
2753 expression_error(expr
,
2754 "not enough arguments for function %s",
2755 show_ident(sym
->ident
));
2756 if (args
> fnargs
&& !ctype
->variadic
)
2757 expression_error(expr
,
2758 "too many arguments for function %s",
2759 show_ident(sym
->ident
));
2761 if (sym
->type
== SYM_NODE
) {
2762 if (evaluate_symbol_call(expr
))
2765 expr
->ctype
= ctype
->ctype
.base_type
;
2769 static struct symbol
*evaluate_offsetof(struct expression
*expr
)
2771 struct expression
*e
= expr
->down
;
2772 struct symbol
*ctype
= expr
->in
;
2775 if (expr
->op
== '.') {
2776 struct symbol
*field
;
2779 expression_error(expr
, "expected structure or union");
2782 examine_symbol_type(ctype
);
2783 class = classify_type(ctype
, &ctype
);
2784 if (class != TYPE_COMPOUND
) {
2785 expression_error(expr
, "expected structure or union");
2789 field
= find_identifier(expr
->ident
, ctype
->symbol_list
, &offset
);
2791 expression_error(expr
, "unknown member");
2795 expr
->type
= EXPR_VALUE
;
2796 expr
->flags
= Int_const_expr
;
2797 expr
->value
= offset
;
2799 expr
->ctype
= size_t_ctype
;
2802 expression_error(expr
, "expected structure or union");
2805 examine_symbol_type(ctype
);
2806 class = classify_type(ctype
, &ctype
);
2807 if (class != (TYPE_COMPOUND
| TYPE_PTR
)) {
2808 expression_error(expr
, "expected array");
2811 ctype
= ctype
->ctype
.base_type
;
2813 expr
->type
= EXPR_VALUE
;
2814 expr
->flags
= Int_const_expr
;
2817 expr
->ctype
= size_t_ctype
;
2819 struct expression
*idx
= expr
->index
, *m
;
2820 struct symbol
*i_type
= evaluate_expression(idx
);
2821 int i_class
= classify_type(i_type
, &i_type
);
2822 if (!is_int(i_class
)) {
2823 expression_error(expr
, "non-integer index");
2826 unrestrict(idx
, i_class
, &i_type
);
2827 idx
= cast_to(idx
, size_t_ctype
);
2828 m
= alloc_const_expression(expr
->pos
,
2829 ctype
->bit_size
>> 3);
2830 m
->ctype
= size_t_ctype
;
2831 m
->flags
= Int_const_expr
;
2832 expr
->type
= EXPR_BINOP
;
2836 expr
->ctype
= size_t_ctype
;
2837 expr
->flags
= m
->flags
& idx
->flags
& Int_const_expr
;
2841 struct expression
*copy
= __alloc_expression(0);
2843 if (e
->type
== EXPR_OFFSETOF
)
2845 if (!evaluate_expression(e
))
2847 expr
->type
= EXPR_BINOP
;
2848 expr
->flags
= e
->flags
& copy
->flags
& Int_const_expr
;
2850 expr
->ctype
= size_t_ctype
;
2854 return size_t_ctype
;
2857 struct symbol
*evaluate_expression(struct expression
*expr
)
2864 switch (expr
->type
) {
2867 expression_error(expr
, "value expression without a type");
2870 return evaluate_string(expr
);
2872 return evaluate_symbol_expression(expr
);
2874 if (!evaluate_expression(expr
->left
))
2876 if (!evaluate_expression(expr
->right
))
2878 return evaluate_binop(expr
);
2880 return evaluate_logical(expr
);
2882 evaluate_expression(expr
->left
);
2883 if (!evaluate_expression(expr
->right
))
2885 return evaluate_comma(expr
);
2887 if (!evaluate_expression(expr
->left
))
2889 if (!evaluate_expression(expr
->right
))
2891 return evaluate_compare(expr
);
2892 case EXPR_ASSIGNMENT
:
2893 if (!evaluate_expression(expr
->left
))
2895 if (!evaluate_expression(expr
->right
))
2897 return evaluate_assignment(expr
);
2899 if (!evaluate_expression(expr
->unop
))
2901 return evaluate_preop(expr
);
2903 if (!evaluate_expression(expr
->unop
))
2905 return evaluate_postop(expr
);
2907 case EXPR_FORCE_CAST
:
2908 case EXPR_IMPLIED_CAST
:
2909 return evaluate_cast(expr
);
2911 return evaluate_sizeof(expr
);
2912 case EXPR_PTRSIZEOF
:
2913 return evaluate_ptrsizeof(expr
);
2915 return evaluate_alignof(expr
);
2917 return evaluate_member_dereference(expr
);
2919 return evaluate_call(expr
);
2921 case EXPR_CONDITIONAL
:
2922 return evaluate_conditional_expression(expr
);
2923 case EXPR_STATEMENT
:
2924 expr
->ctype
= evaluate_statement(expr
->statement
);
2928 expr
->ctype
= &ptr_ctype
;
2932 /* Evaluate the type of the symbol .. */
2933 evaluate_symbol(expr
->symbol
);
2934 /* .. but the type of the _expression_ is a "type" */
2935 expr
->ctype
= &type_ctype
;
2939 return evaluate_offsetof(expr
);
2941 /* These can not exist as stand-alone expressions */
2942 case EXPR_INITIALIZER
:
2943 case EXPR_IDENTIFIER
:
2946 expression_error(expr
, "internal front-end error: initializer in expression");
2949 expression_error(expr
, "internal front-end error: SLICE re-evaluated");
2955 static void check_duplicates(struct symbol
*sym
)
2958 struct symbol
*next
= sym
;
2960 while ((next
= next
->same_symbol
) != NULL
) {
2961 const char *typediff
;
2962 evaluate_symbol(next
);
2964 typediff
= type_difference(&sym
->ctype
, &next
->ctype
, 0, 0);
2966 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2967 show_ident(sym
->ident
),
2968 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
2973 unsigned long mod
= sym
->ctype
.modifiers
;
2974 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
2976 if (!(mod
& MOD_TOPLEVEL
))
2980 if (sym
->ident
== &main_ident
)
2982 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
2986 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
2988 struct symbol
*base_type
;
2996 sym
= examine_symbol_type(sym
);
2997 base_type
= get_base_type(sym
);
3001 /* Evaluate the initializers */
3002 if (sym
->initializer
)
3003 evaluate_initializer(sym
, &sym
->initializer
);
3005 /* And finally, evaluate the body of the symbol too */
3006 if (base_type
->type
== SYM_FN
) {
3007 struct symbol
*curr
= current_fn
;
3009 current_fn
= base_type
;
3011 examine_fn_arguments(base_type
);
3012 if (!base_type
->stmt
&& base_type
->inline_stmt
)
3014 if (base_type
->stmt
)
3015 evaluate_statement(base_type
->stmt
);
3023 void evaluate_symbol_list(struct symbol_list
*list
)
3027 FOR_EACH_PTR(list
, sym
) {
3028 evaluate_symbol(sym
);
3029 check_duplicates(sym
);
3030 } END_FOR_EACH_PTR(sym
);
3033 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
3035 struct expression
*expr
= stmt
->expression
;
3036 struct symbol
*fntype
;
3038 evaluate_expression(expr
);
3039 fntype
= current_fn
->ctype
.base_type
;
3040 if (!fntype
|| fntype
== &void_ctype
) {
3041 if (expr
&& expr
->ctype
!= &void_ctype
)
3042 expression_error(expr
, "return expression in %s function", fntype
?"void":"typeless");
3043 if (expr
&& Wreturn_void
)
3044 warning(stmt
->pos
, "returning void-valued expression");
3049 sparse_error(stmt
->pos
, "return with no return value");
3054 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, "return expression");
3058 static void evaluate_if_statement(struct statement
*stmt
)
3060 if (!stmt
->if_conditional
)
3063 evaluate_conditional(stmt
->if_conditional
, 0);
3064 evaluate_statement(stmt
->if_true
);
3065 evaluate_statement(stmt
->if_false
);
3068 static void evaluate_iterator(struct statement
*stmt
)
3070 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
3071 evaluate_conditional(stmt
->iterator_post_condition
,1);
3072 evaluate_statement(stmt
->iterator_pre_statement
);
3073 evaluate_statement(stmt
->iterator_statement
);
3074 evaluate_statement(stmt
->iterator_post_statement
);
3077 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
3079 switch (*constraint
) {
3080 case '=': /* Assignment */
3081 case '+': /* Update */
3084 expression_error(expr
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
3088 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
3090 switch (*constraint
) {
3091 case '=': /* Assignment */
3092 case '+': /* Update */
3093 expression_error(expr
, "input constraint with assignment (\"%s\")", constraint
);
3097 static void evaluate_asm_statement(struct statement
*stmt
)
3099 struct expression
*expr
;
3102 expr
= stmt
->asm_string
;
3103 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3104 sparse_error(stmt
->pos
, "need constant string for inline asm");
3109 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
3110 struct ident
*ident
;
3113 case 0: /* Identifier */
3115 ident
= (struct ident
*)expr
;
3118 case 1: /* Constraint */
3120 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3121 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
3122 *THIS_ADDRESS(expr
) = NULL
;
3125 verify_output_constraint(expr
, expr
->string
->data
);
3128 case 2: /* Expression */
3130 if (!evaluate_expression(expr
))
3132 if (!lvalue_expression(expr
))
3133 warning(expr
->pos
, "asm output is not an lvalue");
3134 evaluate_assign_to(expr
, expr
->ctype
);
3137 } END_FOR_EACH_PTR(expr
);
3140 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
3141 struct ident
*ident
;
3144 case 0: /* Identifier */
3146 ident
= (struct ident
*)expr
;
3149 case 1: /* Constraint */
3151 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3152 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
3153 *THIS_ADDRESS(expr
) = NULL
;
3156 verify_input_constraint(expr
, expr
->string
->data
);
3159 case 2: /* Expression */
3161 if (!evaluate_expression(expr
))
3165 } END_FOR_EACH_PTR(expr
);
3167 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
3169 sparse_error(stmt
->pos
, "bad asm output");
3172 if (expr
->type
== EXPR_STRING
)
3174 expression_error(expr
, "asm clobber is not a string");
3175 } END_FOR_EACH_PTR(expr
);
3178 static void evaluate_case_statement(struct statement
*stmt
)
3180 evaluate_expression(stmt
->case_expression
);
3181 evaluate_expression(stmt
->case_to
);
3182 evaluate_statement(stmt
->case_statement
);
3185 static void check_case_type(struct expression
*switch_expr
,
3186 struct expression
*case_expr
,
3187 struct expression
**enumcase
)
3189 struct symbol
*switch_type
, *case_type
;
3195 switch_type
= switch_expr
->ctype
;
3196 case_type
= evaluate_expression(case_expr
);
3198 if (!switch_type
|| !case_type
)
3202 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
3203 else if (is_enum_type(case_type
))
3204 *enumcase
= case_expr
;
3207 sclass
= classify_type(switch_type
, &switch_type
);
3208 cclass
= classify_type(case_type
, &case_type
);
3210 /* both should be arithmetic */
3211 if (!(sclass
& cclass
& TYPE_NUM
))
3214 /* neither should be floating */
3215 if ((sclass
| cclass
) & TYPE_FLOAT
)
3218 /* if neither is restricted, we are OK */
3219 if (!((sclass
| cclass
) & TYPE_RESTRICT
))
3222 if (!restricted_binop_type(SPECIAL_EQUAL
, case_expr
, switch_expr
,
3223 cclass
, sclass
, case_type
, switch_type
))
3224 warning(case_expr
->pos
, "restricted degrades to integer");
3229 expression_error(case_expr
, "incompatible types for 'case' statement");
3232 static void evaluate_switch_statement(struct statement
*stmt
)
3235 struct expression
*enumcase
= NULL
;
3236 struct expression
**enumcase_holder
= &enumcase
;
3237 struct expression
*sel
= stmt
->switch_expression
;
3239 evaluate_expression(sel
);
3240 evaluate_statement(stmt
->switch_statement
);
3243 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
3244 enumcase_holder
= NULL
; /* Only check cases against switch */
3246 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
3247 struct statement
*case_stmt
= sym
->stmt
;
3248 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
3249 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
3250 } END_FOR_EACH_PTR(sym
);
3253 struct symbol
*evaluate_statement(struct statement
*stmt
)
3258 switch (stmt
->type
) {
3259 case STMT_DECLARATION
: {
3261 FOR_EACH_PTR(stmt
->declaration
, s
) {
3263 } END_FOR_EACH_PTR(s
);
3268 return evaluate_return_expression(stmt
);
3270 case STMT_EXPRESSION
:
3271 if (!evaluate_expression(stmt
->expression
))
3273 if (stmt
->expression
->ctype
== &null_ctype
)
3274 stmt
->expression
= cast_to(stmt
->expression
, &ptr_ctype
);
3275 return degenerate(stmt
->expression
);
3277 case STMT_COMPOUND
: {
3278 struct statement
*s
;
3279 struct symbol
*type
= NULL
;
3281 /* Evaluate the return symbol in the compound statement */
3282 evaluate_symbol(stmt
->ret
);
3285 * Then, evaluate each statement, making the type of the
3286 * compound statement be the type of the last statement
3288 type
= evaluate_statement(stmt
->args
);
3289 FOR_EACH_PTR(stmt
->stmts
, s
) {
3290 type
= evaluate_statement(s
);
3291 } END_FOR_EACH_PTR(s
);
3297 evaluate_if_statement(stmt
);
3300 evaluate_iterator(stmt
);
3303 evaluate_switch_statement(stmt
);
3306 evaluate_case_statement(stmt
);
3309 return evaluate_statement(stmt
->label_statement
);
3311 evaluate_expression(stmt
->goto_expression
);
3316 evaluate_asm_statement(stmt
);
3319 evaluate_expression(stmt
->expression
);
3322 evaluate_expression(stmt
->range_expression
);
3323 evaluate_expression(stmt
->range_low
);
3324 evaluate_expression(stmt
->range_high
);