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
;
351 static inline int classify_type(struct symbol
*type
, struct symbol
**base
)
353 static int type_class
[SYM_BAD
+ 1] = {
354 [SYM_PTR
] = TYPE_PTR
,
355 [SYM_FN
] = TYPE_PTR
| TYPE_FN
,
356 [SYM_ARRAY
] = TYPE_PTR
| TYPE_COMPOUND
,
357 [SYM_STRUCT
] = TYPE_COMPOUND
,
358 [SYM_UNION
] = TYPE_COMPOUND
,
359 [SYM_BITFIELD
] = TYPE_NUM
| TYPE_BITFIELD
,
360 [SYM_RESTRICT
] = TYPE_NUM
| TYPE_RESTRICT
,
361 [SYM_FOULED
] = TYPE_NUM
| TYPE_RESTRICT
| TYPE_FOULED
,
363 if (type
->type
== SYM_NODE
)
364 type
= type
->ctype
.base_type
;
365 if (type
->type
== SYM_ENUM
)
366 type
= type
->ctype
.base_type
;
368 if (type
->type
== SYM_BASETYPE
) {
369 if (type
->ctype
.base_type
== &int_type
)
371 if (type
->ctype
.base_type
== &fp_type
)
372 return TYPE_NUM
| TYPE_FLOAT
;
374 return type_class
[type
->type
];
377 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
379 static inline int is_string_type(struct symbol
*type
)
381 if (type
->type
== SYM_NODE
)
382 type
= type
->ctype
.base_type
;
383 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
386 static struct symbol
*bad_expr_type(struct expression
*expr
)
388 sparse_error(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
389 switch (expr
->type
) {
392 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
393 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
397 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
404 return expr
->ctype
= &bad_ctype
;
407 static int restricted_value(struct expression
*v
, struct symbol
*type
)
409 if (v
->type
!= EXPR_VALUE
)
416 static int restricted_binop(int op
, struct symbol
*type
)
421 case SPECIAL_AND_ASSIGN
:
422 case SPECIAL_OR_ASSIGN
:
423 case SPECIAL_XOR_ASSIGN
:
424 return 1; /* unfoul */
428 return 2; /* keep fouled */
430 case SPECIAL_NOTEQUAL
:
431 return 3; /* warn if fouled */
437 static int restricted_unop(int op
, struct symbol
**type
)
440 if ((*type
)->bit_size
< bits_in_int
)
441 *type
= befoul(*type
);
448 /* type should be SYM_FOULED */
449 static inline struct symbol
*unfoul(struct symbol
*type
)
451 return type
->ctype
.base_type
;
454 static struct symbol
*restricted_binop_type(int op
,
455 struct expression
*left
,
456 struct expression
*right
,
457 int lclass
, int rclass
,
458 struct symbol
*ltype
,
459 struct symbol
*rtype
)
461 struct symbol
*ctype
= NULL
;
462 if (lclass
& TYPE_RESTRICT
) {
463 if (rclass
& TYPE_RESTRICT
) {
464 if (ltype
== rtype
) {
466 } else if (lclass
& TYPE_FOULED
) {
467 if (unfoul(ltype
) == rtype
)
469 } else if (rclass
& TYPE_FOULED
) {
470 if (unfoul(rtype
) == ltype
)
474 if (!restricted_value(right
, ltype
))
477 } else if (!restricted_value(left
, rtype
))
481 switch (restricted_binop(op
, ctype
)) {
483 if ((lclass
^ rclass
) & TYPE_FOULED
)
484 ctype
= unfoul(ctype
);
487 if (!(lclass
& rclass
& TYPE_FOULED
))
499 static inline void unrestrict(struct expression
*expr
,
500 int class, struct symbol
**ctype
)
502 if (class & TYPE_RESTRICT
) {
503 warning(expr
->pos
, "restricted degrades to integer");
504 if (class & TYPE_FOULED
)
505 *ctype
= unfoul(*ctype
);
506 *ctype
= (*ctype
)->ctype
.base_type
; /* get to arithmetic type */
510 static struct symbol
*usual_conversions(int op
,
511 struct expression
*left
,
512 struct expression
*right
,
513 int lclass
, int rclass
,
514 struct symbol
*ltype
,
515 struct symbol
*rtype
)
517 struct symbol
*ctype
;
519 warn_for_different_enum_types(right
->pos
, left
->ctype
, right
->ctype
);
521 if ((lclass
| rclass
) & TYPE_RESTRICT
)
525 if (!(lclass
& TYPE_FLOAT
)) {
526 if (!(rclass
& TYPE_FLOAT
))
527 return bigger_int_type(ltype
, rtype
);
530 } else if (rclass
& TYPE_FLOAT
) {
531 unsigned long lmod
= ltype
->ctype
.modifiers
;
532 unsigned long rmod
= rtype
->ctype
.modifiers
;
533 if (rmod
& ~lmod
& (MOD_LONG
| MOD_LONGLONG
))
541 ctype
= restricted_binop_type(op
, left
, right
,
542 lclass
, rclass
, ltype
, rtype
);
546 unrestrict(left
, lclass
, <ype
);
547 unrestrict(right
, rclass
, &rtype
);
552 static inline int lvalue_expression(struct expression
*expr
)
554 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
557 static inline int is_function(struct symbol
*type
)
559 return type
&& type
->type
== SYM_FN
;
562 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*itype
)
564 struct expression
*index
= expr
->right
;
565 struct symbol
*ctype
, *base
;
568 classify_type(degenerate(expr
->left
), &ctype
);
569 base
= examine_pointer_target(ctype
);
572 expression_error(expr
, "missing type information");
575 if (is_function(base
)) {
576 expression_error(expr
, "arithmetics on pointers to functions");
580 /* Get the size of whatever the pointer points to */
581 multiply
= base
->bit_size
>> 3;
583 if (ctype
== &null_ctype
)
587 if (multiply
== 1 && itype
->bit_size
>= bits_in_pointer
)
590 if (index
->type
== EXPR_VALUE
) {
591 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
592 unsigned long long v
= index
->value
, mask
;
593 mask
= 1ULL << (itype
->bit_size
- 1);
599 mask
= 1ULL << (bits_in_pointer
- 1);
600 v
&= mask
| (mask
- 1);
602 val
->ctype
= ssize_t_ctype
;
607 if (itype
->bit_size
< bits_in_pointer
)
608 index
= cast_to(index
, ssize_t_ctype
);
611 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
612 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
614 val
->ctype
= ssize_t_ctype
;
615 val
->value
= multiply
;
618 mul
->ctype
= ssize_t_ctype
;
628 static void examine_fn_arguments(struct symbol
*fn
);
630 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
632 const char *type_difference(struct ctype
*c1
, struct ctype
*c2
,
633 unsigned long mod1
, unsigned long mod2
)
635 unsigned long as1
= c1
->as
, as2
= c2
->as
;
636 struct symbol
*t1
= c1
->base_type
;
637 struct symbol
*t2
= c2
->base_type
;
638 int move1
= 1, move2
= 1;
639 mod1
|= c1
->modifiers
;
640 mod2
|= c2
->modifiers
;
644 struct symbol
*base1
= t1
->ctype
.base_type
;
645 struct symbol
*base2
= t2
->ctype
.base_type
;
648 * FIXME! Collect alignment and context too here!
651 if (t1
&& t1
->type
!= SYM_PTR
) {
652 mod1
|= t1
->ctype
.modifiers
;
659 if (t2
&& t2
->type
!= SYM_PTR
) {
660 mod2
|= t2
->ctype
.modifiers
;
669 return "different types";
671 if (t1
->type
== SYM_NODE
|| t1
->type
== SYM_ENUM
) {
679 if (t2
->type
== SYM_NODE
|| t2
->type
== SYM_ENUM
) {
689 if (type
!= t2
->type
)
690 return "different base types";
694 sparse_error(t1
->pos
,
695 "internal error: bad type in derived(%d)",
701 return "different base types";
703 /* XXX: we ought to compare sizes */
706 if (Waddress_space
&& as1
!= as2
)
707 return "different address spaces";
708 /* MOD_SPECIFIER is due to idiocy in parse.c */
709 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SPECIFIER
)
710 return "different modifiers";
711 /* we could be lazier here */
712 base1
= examine_pointer_target(t1
);
713 base2
= examine_pointer_target(t2
);
714 mod1
= t1
->ctype
.modifiers
;
716 mod2
= t2
->ctype
.modifiers
;
720 struct symbol
*arg1
, *arg2
;
723 if (Waddress_space
&& as1
!= as2
)
724 return "different address spaces";
725 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
726 return "different modifiers";
727 mod1
= t1
->ctype
.modifiers
;
729 mod2
= t2
->ctype
.modifiers
;
732 if (base1
->variadic
!= base2
->variadic
)
733 return "incompatible variadic arguments";
734 examine_fn_arguments(t1
);
735 examine_fn_arguments(t2
);
736 PREPARE_PTR_LIST(t1
->arguments
, arg1
);
737 PREPARE_PTR_LIST(t2
->arguments
, arg2
);
744 return "different argument counts";
745 diffstr
= type_difference(&arg1
->ctype
,
749 static char argdiff
[80];
750 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diffstr
);
757 FINISH_PTR_LIST(arg2
);
758 FINISH_PTR_LIST(arg1
);
762 if (Waddress_space
&& as1
!= as2
)
763 return "different address spaces";
765 return "different base types";
766 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
770 return "different type sizes";
771 else if (diff
& ~MOD_SIGNEDNESS
)
772 return "different modifiers";
774 return "different signedness";
779 if (Waddress_space
&& as1
!= as2
)
780 return "different address spaces";
781 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
782 return "different modifiers";
786 static void bad_null(struct expression
*expr
)
788 if (Wnon_pointer_null
)
789 warning(expr
->pos
, "Using plain integer as NULL pointer");
792 static unsigned long target_qualifiers(struct symbol
*type
)
794 unsigned long mod
= type
->ctype
.modifiers
& MOD_IGN
;
795 if (type
->ctype
.base_type
&& type
->ctype
.base_type
->type
== SYM_ARRAY
)
800 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
)
802 const char *typediff
;
803 struct symbol
*ltype
, *rtype
;
804 struct expression
*l
= expr
->left
;
805 struct expression
*r
= expr
->right
;
806 struct symbol
*lbase
, *rbase
;
808 classify_type(degenerate(l
), <ype
);
809 classify_type(degenerate(r
), &rtype
);
811 lbase
= examine_pointer_target(ltype
);
812 rbase
= examine_pointer_target(rtype
);
813 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
814 target_qualifiers(rtype
),
815 target_qualifiers(ltype
));
817 expression_error(expr
, "subtraction of different types can't work (%s)", typediff
);
819 if (is_function(lbase
)) {
820 expression_error(expr
, "subtraction of functions? Share your drugs");
824 expr
->ctype
= ssize_t_ctype
;
825 if (lbase
->bit_size
> bits_in_char
) {
826 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
827 struct expression
*div
= expr
;
828 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
829 unsigned long value
= lbase
->bit_size
>> 3;
831 val
->ctype
= size_t_ctype
;
834 if (value
& (value
-1)) {
835 if (Wptr_subtraction_blows
)
836 warning(expr
->pos
, "potentially expensive pointer subtraction");
840 sub
->ctype
= ssize_t_ctype
;
849 return ssize_t_ctype
;
852 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
854 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
856 struct symbol
*ctype
;
861 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
862 warning(expr
->pos
, "assignment expression in conditional");
864 ctype
= evaluate_expression(expr
);
866 if (is_safe_type(ctype
))
867 warning(expr
->pos
, "testing a 'safe expression'");
873 static struct symbol
*evaluate_logical(struct expression
*expr
)
875 if (!evaluate_conditional(expr
->left
, 0))
877 if (!evaluate_conditional(expr
->right
, 0))
880 expr
->ctype
= &bool_ctype
;
882 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
888 static struct symbol
*evaluate_binop(struct expression
*expr
)
890 struct symbol
*ltype
, *rtype
, *ctype
;
891 int lclass
= classify_type(expr
->left
->ctype
, <ype
);
892 int rclass
= classify_type(expr
->right
->ctype
, &rtype
);
896 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
900 /* number op number */
901 if (lclass
& rclass
& TYPE_NUM
) {
902 if ((lclass
| rclass
) & TYPE_FLOAT
) {
904 case '+': case '-': case '*': case '/':
907 return bad_expr_type(expr
);
911 if (op
== SPECIAL_LEFTSHIFT
|| op
== SPECIAL_RIGHTSHIFT
) {
912 // shifts do integer promotions, but that's it.
913 unrestrict(expr
->left
, lclass
, <ype
);
914 unrestrict(expr
->right
, rclass
, &rtype
);
915 ctype
= ltype
= integer_promotion(ltype
);
916 rtype
= integer_promotion(rtype
);
918 // The rest do usual conversions
919 ltype
= usual_conversions(op
, expr
->left
, expr
->right
,
920 lclass
, rclass
, ltype
, rtype
);
921 ctype
= rtype
= ltype
;
924 expr
->left
= cast_to(expr
->left
, ltype
);
925 expr
->right
= cast_to(expr
->right
, rtype
);
930 /* pointer (+|-) integer */
931 if (lclass
& TYPE_PTR
&& is_int(rclass
) && (op
== '+' || op
== '-')) {
932 unrestrict(expr
->right
, rclass
, &rtype
);
933 return evaluate_ptr_add(expr
, rtype
);
936 /* integer + pointer */
937 if (rclass
& TYPE_PTR
&& is_int(lclass
) && op
== '+') {
938 struct expression
*index
= expr
->left
;
939 unrestrict(index
, lclass
, <ype
);
940 expr
->left
= expr
->right
;
942 return evaluate_ptr_add(expr
, ltype
);
945 /* pointer - pointer */
946 if (lclass
& rclass
& TYPE_PTR
&& expr
->op
== '-')
947 return evaluate_ptr_sub(expr
);
949 return bad_expr_type(expr
);
952 static struct symbol
*evaluate_comma(struct expression
*expr
)
954 expr
->ctype
= degenerate(expr
->right
);
955 if (expr
->ctype
== &null_ctype
)
956 expr
->ctype
= &ptr_ctype
;
957 expr
->flags
&= expr
->left
->flags
& expr
->right
->flags
;
961 static int modify_for_unsigned(int op
)
964 op
= SPECIAL_UNSIGNED_LT
;
966 op
= SPECIAL_UNSIGNED_GT
;
967 else if (op
== SPECIAL_LTE
)
968 op
= SPECIAL_UNSIGNED_LTE
;
969 else if (op
== SPECIAL_GTE
)
970 op
= SPECIAL_UNSIGNED_GTE
;
974 static inline int is_null_pointer_constant(struct expression
*e
)
976 if (e
->ctype
== &null_ctype
)
978 if (!(e
->flags
& Int_const_expr
))
980 return is_zero_constant(e
) ? 2 : 0;
983 static struct symbol
*evaluate_compare(struct expression
*expr
)
985 struct expression
*left
= expr
->left
, *right
= expr
->right
;
986 struct symbol
*ltype
, *rtype
, *lbase
, *rbase
;
987 int lclass
= classify_type(degenerate(left
), <ype
);
988 int rclass
= classify_type(degenerate(right
), &rtype
);
989 struct symbol
*ctype
;
990 const char *typediff
;
993 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
998 if (is_type_type(ltype
) && is_type_type(rtype
))
1001 if (is_safe_type(left
->ctype
) || is_safe_type(right
->ctype
))
1002 warning(expr
->pos
, "testing a 'safe expression'");
1004 /* number on number */
1005 if (lclass
& rclass
& TYPE_NUM
) {
1006 ctype
= usual_conversions(expr
->op
, expr
->left
, expr
->right
,
1007 lclass
, rclass
, ltype
, rtype
);
1008 expr
->left
= cast_to(expr
->left
, ctype
);
1009 expr
->right
= cast_to(expr
->right
, ctype
);
1010 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
1011 expr
->op
= modify_for_unsigned(expr
->op
);
1015 /* at least one must be a pointer */
1016 if (!((lclass
| rclass
) & TYPE_PTR
))
1017 return bad_expr_type(expr
);
1019 /* equality comparisons can be with null pointer constants */
1020 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1021 int is_null1
= is_null_pointer_constant(left
);
1022 int is_null2
= is_null_pointer_constant(right
);
1027 if (is_null1
&& is_null2
) {
1028 int positive
= expr
->op
== SPECIAL_EQUAL
;
1029 expr
->type
= EXPR_VALUE
;
1030 expr
->value
= positive
;
1033 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1034 left
= cast_to(left
, rtype
);
1037 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1038 right
= cast_to(right
, ltype
);
1042 /* both should be pointers */
1043 if (!(lclass
& rclass
& TYPE_PTR
))
1044 return bad_expr_type(expr
);
1045 expr
->op
= modify_for_unsigned(expr
->op
);
1047 lbase
= examine_pointer_target(ltype
);
1048 rbase
= examine_pointer_target(rtype
);
1050 /* they also have special treatment for pointers to void */
1051 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1052 if (ltype
->ctype
.as
== rtype
->ctype
.as
) {
1053 if (lbase
== &void_ctype
) {
1054 right
= cast_to(right
, ltype
);
1057 if (rbase
== &void_ctype
) {
1058 left
= cast_to(left
, rtype
);
1064 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1065 target_qualifiers(rtype
),
1066 target_qualifiers(ltype
));
1070 expression_error(expr
, "incompatible types in comparison expression (%s)", typediff
);
1074 expr
->ctype
= &bool_ctype
;
1079 * NOTE! The degenerate case of "x ? : y", where we don't
1080 * have a true case, this will possibly promote "x" to the
1081 * same type as "y", and thus _change_ the conditional
1082 * test in the expression. But since promotion is "safe"
1083 * for testing, that's OK.
1085 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
1087 struct expression
**true;
1088 struct symbol
*ctype
, *ltype
, *rtype
, *lbase
, *rbase
;
1090 const char * typediff
;
1093 if (!evaluate_conditional(expr
->conditional
, 0))
1095 if (!evaluate_expression(expr
->cond_false
))
1098 ctype
= degenerate(expr
->conditional
);
1099 rtype
= degenerate(expr
->cond_false
);
1101 true = &expr
->conditional
;
1103 if (expr
->cond_true
) {
1104 if (!evaluate_expression(expr
->cond_true
))
1106 ltype
= degenerate(expr
->cond_true
);
1107 true = &expr
->cond_true
;
1111 int flags
= expr
->conditional
->flags
& Int_const_expr
;
1112 flags
&= (*true)->flags
& expr
->cond_false
->flags
;
1117 lclass
= classify_type(ltype
, <ype
);
1118 rclass
= classify_type(rtype
, &rtype
);
1119 if (lclass
& rclass
& TYPE_NUM
) {
1120 ctype
= usual_conversions('?', *true, expr
->cond_false
,
1121 lclass
, rclass
, ltype
, rtype
);
1122 *true = cast_to(*true, ctype
);
1123 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1127 if ((lclass
| rclass
) & TYPE_PTR
) {
1128 int is_null1
= is_null_pointer_constant(*true);
1129 int is_null2
= is_null_pointer_constant(expr
->cond_false
);
1131 if (is_null1
&& is_null2
) {
1132 *true = cast_to(*true, &ptr_ctype
);
1133 expr
->cond_false
= cast_to(expr
->cond_false
, &ptr_ctype
);
1137 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1140 *true = cast_to(*true, rtype
);
1144 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1146 bad_null(expr
->cond_false
);
1147 expr
->cond_false
= cast_to(expr
->cond_false
, ltype
);
1151 if (!(lclass
& rclass
& TYPE_PTR
)) {
1152 typediff
= "different types";
1155 /* OK, it's pointer on pointer */
1156 if (ltype
->ctype
.as
!= rtype
->ctype
.as
) {
1157 typediff
= "different address spaces";
1161 /* need to be lazier here */
1162 lbase
= examine_pointer_target(ltype
);
1163 rbase
= examine_pointer_target(rtype
);
1164 qual
= target_qualifiers(ltype
) | target_qualifiers(rtype
);
1166 if (lbase
== &void_ctype
) {
1167 /* XXX: pointers to function should warn here */
1172 if (rbase
== &void_ctype
) {
1173 /* XXX: pointers to function should warn here */
1177 /* XXX: that should be pointer to composite */
1179 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1186 /* void on void, struct on same struct, union on same union */
1187 if (ltype
== rtype
) {
1191 typediff
= "different base types";
1194 expression_error(expr
, "incompatible types in conditional expression (%s)", typediff
);
1198 expr
->ctype
= ctype
;
1202 if (qual
& ~ctype
->ctype
.modifiers
) {
1203 struct symbol
*sym
= alloc_symbol(ctype
->pos
, SYM_PTR
);
1205 sym
->ctype
.modifiers
|= qual
;
1208 *true = cast_to(*true, ctype
);
1209 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1213 /* FP assignments can not do modulo or bit operations */
1214 static int compatible_float_op(int op
)
1216 return op
== SPECIAL_ADD_ASSIGN
||
1217 op
== SPECIAL_SUB_ASSIGN
||
1218 op
== SPECIAL_MUL_ASSIGN
||
1219 op
== SPECIAL_DIV_ASSIGN
;
1222 static int evaluate_assign_op(struct expression
*expr
)
1224 struct symbol
*target
= expr
->left
->ctype
;
1225 struct symbol
*source
= expr
->right
->ctype
;
1226 struct symbol
*t
, *s
;
1227 int tclass
= classify_type(target
, &t
);
1228 int sclass
= classify_type(source
, &s
);
1231 if (tclass
& sclass
& TYPE_NUM
) {
1232 if (tclass
& TYPE_FLOAT
&& !compatible_float_op(op
)) {
1233 expression_error(expr
, "invalid assignment");
1236 if (tclass
& TYPE_RESTRICT
) {
1237 if (!restricted_binop(op
, t
)) {
1238 expression_error(expr
, "bad restricted assignment");
1241 /* allowed assignments unfoul */
1242 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1244 if (!restricted_value(expr
->right
, t
))
1246 } else if (!(sclass
& TYPE_RESTRICT
))
1248 /* source and target would better be identical restricted */
1251 warning(expr
->pos
, "invalid restricted assignment");
1252 expr
->right
= cast_to(expr
->right
, target
);
1255 if (tclass
== TYPE_PTR
&& is_int(sclass
)) {
1256 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1257 unrestrict(expr
->right
, sclass
, &s
);
1258 evaluate_ptr_add(expr
, s
);
1261 expression_error(expr
, "invalid pointer assignment");
1265 expression_error(expr
, "invalid assignment");
1269 expr
->right
= cast_to(expr
->right
, target
);
1273 static int whitelist_pointers(struct symbol
*t1
, struct symbol
*t2
)
1276 return 0; /* yes, 0 - we don't want a cast_to here */
1277 if (t1
== &void_ctype
)
1279 if (t2
== &void_ctype
)
1281 if (classify_type(t1
, &t1
) != TYPE_NUM
)
1283 if (classify_type(t2
, &t2
) != TYPE_NUM
)
1287 if (t1
->ctype
.modifiers
& t2
->ctype
.modifiers
& MOD_CHAR
)
1289 if ((t1
->ctype
.modifiers
^ t2
->ctype
.modifiers
) & MOD_SIZE
)
1294 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1295 struct expression
**rp
, const char *where
)
1297 const char *typediff
;
1298 struct symbol
*source
= degenerate(*rp
);
1299 struct symbol
*t
, *s
;
1300 int tclass
= classify_type(target
, &t
);
1301 int sclass
= classify_type(source
, &s
);
1303 if (tclass
& sclass
& TYPE_NUM
) {
1304 if (tclass
& TYPE_RESTRICT
) {
1305 /* allowed assignments unfoul */
1306 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1308 if (!restricted_value(*rp
, target
))
1312 } else if (!(sclass
& TYPE_RESTRICT
))
1314 typediff
= "different base types";
1318 if (tclass
== TYPE_PTR
) {
1319 unsigned long mod1
, mod2
;
1320 struct symbol
*b1
, *b2
;
1321 // NULL pointer is always OK
1322 int is_null
= is_null_pointer_constant(*rp
);
1328 if (!(sclass
& TYPE_PTR
)) {
1329 typediff
= "different base types";
1332 b1
= examine_pointer_target(t
);
1333 b2
= examine_pointer_target(s
);
1334 mod1
= target_qualifiers(t
);
1335 mod2
= target_qualifiers(s
);
1336 if (whitelist_pointers(b1
, b2
)) {
1338 * assignments to/from void * are OK, provided that
1339 * we do not remove qualifiers from pointed to [C]
1340 * or mix address spaces [sparse].
1342 if (t
->ctype
.as
!= s
->ctype
.as
) {
1343 typediff
= "different address spaces";
1347 typediff
= "different modifiers";
1352 /* It's OK if the target is more volatile or const than the source */
1353 typediff
= type_difference(&t
->ctype
, &s
->ctype
, 0, mod1
);
1359 if ((tclass
& TYPE_COMPOUND
) && s
== t
)
1362 if (tclass
& TYPE_NUM
) {
1363 /* XXX: need to turn into comparison with NULL */
1364 if (t
== &bool_ctype
&& (sclass
& TYPE_PTR
))
1366 typediff
= "different base types";
1369 typediff
= "invalid types";
1372 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1373 info(expr
->pos
, " expected %s", show_typename(target
));
1374 info(expr
->pos
, " got %s", show_typename(source
));
1375 *rp
= cast_to(*rp
, target
);
1378 *rp
= cast_to(*rp
, target
);
1382 static void mark_assigned(struct expression
*expr
)
1388 switch (expr
->type
) {
1393 if (sym
->type
!= SYM_NODE
)
1395 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1399 mark_assigned(expr
->left
);
1400 mark_assigned(expr
->right
);
1403 case EXPR_FORCE_CAST
:
1404 mark_assigned(expr
->cast_expression
);
1407 mark_assigned(expr
->base
);
1415 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1417 if (type
->ctype
.modifiers
& MOD_CONST
)
1418 expression_error(left
, "assignment to const expression");
1420 /* We know left is an lvalue, so it's a "preop-*" */
1421 mark_assigned(left
->unop
);
1424 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1426 struct expression
*left
= expr
->left
;
1427 struct expression
*where
= expr
;
1428 struct symbol
*ltype
;
1430 if (!lvalue_expression(left
)) {
1431 expression_error(expr
, "not an lvalue");
1435 ltype
= left
->ctype
;
1437 if (expr
->op
!= '=') {
1438 if (!evaluate_assign_op(expr
))
1441 if (!compatible_assignment_types(where
, ltype
, &expr
->right
, "assignment"))
1445 evaluate_assign_to(left
, ltype
);
1447 expr
->ctype
= ltype
;
1451 static void examine_fn_arguments(struct symbol
*fn
)
1455 FOR_EACH_PTR(fn
->arguments
, s
) {
1456 struct symbol
*arg
= evaluate_symbol(s
);
1457 /* Array/function arguments silently degenerate into pointers */
1463 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1464 if (arg
->type
== SYM_ARRAY
)
1465 ptr
->ctype
= arg
->ctype
;
1467 ptr
->ctype
.base_type
= arg
;
1468 ptr
->ctype
.as
|= s
->ctype
.as
;
1469 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
& MOD_PTRINHERIT
;
1471 s
->ctype
.base_type
= ptr
;
1473 s
->ctype
.modifiers
&= ~MOD_PTRINHERIT
;
1476 examine_symbol_type(s
);
1483 } END_FOR_EACH_PTR(s
);
1486 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1488 /* Take the modifiers of the pointer, and apply them to the member */
1489 mod
|= sym
->ctype
.modifiers
;
1490 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1491 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1493 newsym
->ctype
.as
= as
;
1494 newsym
->ctype
.modifiers
= mod
;
1500 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1502 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1503 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1505 node
->ctype
.base_type
= ptr
;
1506 ptr
->bit_size
= bits_in_pointer
;
1507 ptr
->ctype
.alignment
= pointer_alignment
;
1509 node
->bit_size
= bits_in_pointer
;
1510 node
->ctype
.alignment
= pointer_alignment
;
1513 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1514 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1515 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1517 if (sym
->type
== SYM_NODE
) {
1518 ptr
->ctype
.as
|= sym
->ctype
.as
;
1519 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1520 sym
= sym
->ctype
.base_type
;
1522 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1523 ptr
->ctype
.as
|= sym
->ctype
.as
;
1524 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1525 sym
= sym
->ctype
.base_type
;
1527 ptr
->ctype
.base_type
= sym
;
1532 /* Arrays degenerate into pointers on pointer arithmetic */
1533 static struct symbol
*degenerate(struct expression
*expr
)
1535 struct symbol
*ctype
, *base
;
1539 ctype
= expr
->ctype
;
1542 base
= examine_symbol_type(ctype
);
1543 if (ctype
->type
== SYM_NODE
)
1544 base
= ctype
->ctype
.base_type
;
1546 * Arrays degenerate into pointers to the entries, while
1547 * functions degenerate into pointers to themselves.
1548 * If array was part of non-lvalue compound, we create a copy
1549 * of that compound first and then act as if we were dealing with
1550 * the corresponding field in there.
1552 switch (base
->type
) {
1554 if (expr
->type
== EXPR_SLICE
) {
1555 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1556 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1558 a
->ctype
.base_type
= expr
->base
->ctype
;
1559 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1560 a
->array_size
= expr
->base
->ctype
->array_size
;
1562 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1564 e0
->ctype
= &lazy_ptr_ctype
;
1566 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1569 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1571 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1573 e2
->right
= expr
->base
;
1575 e2
->ctype
= expr
->base
->ctype
;
1577 if (expr
->r_bitpos
) {
1578 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1581 e3
->right
= alloc_const_expression(expr
->pos
,
1582 expr
->r_bitpos
>> 3);
1583 e3
->ctype
= &lazy_ptr_ctype
;
1588 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1591 e4
->ctype
= &lazy_ptr_ctype
;
1594 expr
->type
= EXPR_PREOP
;
1598 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1599 expression_error(expr
, "strange non-value function or array");
1602 *expr
= *expr
->unop
;
1603 ctype
= create_pointer(expr
, ctype
, 1);
1604 expr
->ctype
= ctype
;
1611 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1613 struct expression
*op
= expr
->unop
;
1614 struct symbol
*ctype
;
1616 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1617 expression_error(expr
, "not addressable");
1624 if (expr
->type
== EXPR_SYMBOL
) {
1625 struct symbol
*sym
= expr
->symbol
;
1626 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1630 * symbol expression evaluation is lazy about the type
1631 * of the sub-expression, so we may have to generate
1632 * the type here if so..
1634 if (expr
->ctype
== &lazy_ptr_ctype
) {
1635 ctype
= create_pointer(expr
, ctype
, 0);
1636 expr
->ctype
= ctype
;
1642 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1644 struct expression
*op
= expr
->unop
;
1645 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1647 /* Simplify: *&(expr) => (expr) */
1648 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1654 /* Dereferencing a node drops all the node information. */
1655 if (ctype
->type
== SYM_NODE
)
1656 ctype
= ctype
->ctype
.base_type
;
1658 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1659 target
= ctype
->ctype
.base_type
;
1661 switch (ctype
->type
) {
1663 expression_error(expr
, "cannot dereference this type");
1666 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1667 merge_type(node
, ctype
);
1671 if (!lvalue_expression(op
)) {
1672 expression_error(op
, "non-lvalue array??");
1676 /* Do the implied "addressof" on the array */
1680 * When an array is dereferenced, we need to pick
1681 * up the attributes of the original node too..
1683 merge_type(node
, op
->ctype
);
1684 merge_type(node
, ctype
);
1688 node
->bit_size
= target
->bit_size
;
1689 node
->array_size
= target
->array_size
;
1696 * Unary post-ops: x++ and x--
1698 static struct symbol
*evaluate_postop(struct expression
*expr
)
1700 struct expression
*op
= expr
->unop
;
1701 struct symbol
*ctype
= op
->ctype
;
1702 int class = classify_type(op
->ctype
, &ctype
);
1705 if (!lvalue_expression(expr
->unop
)) {
1706 expression_error(expr
, "need lvalue expression for ++/--");
1710 if ((class & TYPE_RESTRICT
) && restricted_unop(expr
->op
, &ctype
)) {
1711 expression_error(expr
, "bad operation on restricted");
1715 if (class & TYPE_NUM
) {
1717 } else if (class == TYPE_PTR
) {
1718 struct symbol
*target
= examine_pointer_target(ctype
);
1719 if (!is_function(target
))
1720 multiply
= target
->bit_size
>> 3;
1724 evaluate_assign_to(op
, op
->ctype
);
1725 expr
->op_value
= multiply
;
1726 expr
->ctype
= ctype
;
1730 expression_error(expr
, "bad argument type for ++/--");
1734 static struct symbol
*evaluate_sign(struct expression
*expr
)
1736 struct symbol
*ctype
= expr
->unop
->ctype
;
1737 int class = classify_type(ctype
, &ctype
);
1738 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1740 /* should be an arithmetic type */
1741 if (!(class & TYPE_NUM
))
1742 return bad_expr_type(expr
);
1743 if (!(class & (TYPE_FLOAT
|TYPE_RESTRICT
))) {
1744 struct symbol
*rtype
= integer_promotion(ctype
);
1745 expr
->unop
= cast_to(expr
->unop
, rtype
);
1747 } else if ((class & TYPE_FLOAT
) && expr
->op
!= '~') {
1748 /* no conversions needed */
1749 } else if ((class & TYPE_RESTRICT
) && !restricted_unop(expr
->op
, &ctype
)) {
1750 /* no conversions needed */
1752 return bad_expr_type(expr
);
1754 if (expr
->op
== '+')
1755 *expr
= *expr
->unop
;
1756 expr
->ctype
= ctype
;
1760 static struct symbol
*evaluate_preop(struct expression
*expr
)
1762 struct symbol
*ctype
= expr
->unop
->ctype
;
1766 *expr
= *expr
->unop
;
1772 return evaluate_sign(expr
);
1775 return evaluate_dereference(expr
);
1778 return evaluate_addressof(expr
);
1780 case SPECIAL_INCREMENT
:
1781 case SPECIAL_DECREMENT
:
1783 * From a type evaluation standpoint the preops are
1784 * the same as the postops
1786 return evaluate_postop(expr
);
1789 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1791 if (is_safe_type(ctype
))
1792 warning(expr
->pos
, "testing a 'safe expression'");
1793 if (is_float_type(ctype
)) {
1794 struct expression
*arg
= expr
->unop
;
1795 expr
->type
= EXPR_BINOP
;
1796 expr
->op
= SPECIAL_EQUAL
;
1798 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1799 expr
->right
->ctype
= ctype
;
1800 expr
->right
->fvalue
= 0;
1801 } else if (is_fouled_type(ctype
)) {
1802 warning(expr
->pos
, "restricted degrades to integer");
1804 ctype
= &bool_ctype
;
1810 expr
->ctype
= ctype
;
1814 static struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1816 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1817 struct ptr_list
*list
= head
;
1823 for (i
= 0; i
< list
->nr
; i
++) {
1824 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1826 if (sym
->ident
!= ident
)
1828 *offset
= sym
->offset
;
1831 struct symbol
*ctype
= sym
->ctype
.base_type
;
1835 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1837 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1840 *offset
+= sym
->offset
;
1844 } while ((list
= list
->next
) != head
);
1848 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1850 struct expression
*add
;
1853 * Create a new add-expression
1855 * NOTE! Even if we just add zero, we need a new node
1856 * for the member pointer, since it has a different
1857 * type than the original pointer. We could make that
1858 * be just a cast, but the fact is, a node is a node,
1859 * so we might as well just do the "add zero" here.
1861 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1864 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1865 add
->right
->ctype
= &int_ctype
;
1866 add
->right
->value
= offset
;
1869 * The ctype of the pointer will be lazily evaluated if
1870 * we ever take the address of this member dereference..
1872 add
->ctype
= &lazy_ptr_ctype
;
1876 /* structure/union dereference */
1877 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1880 struct symbol
*ctype
, *member
;
1881 struct expression
*deref
= expr
->deref
, *add
;
1882 struct ident
*ident
= expr
->member
;
1886 if (!evaluate_expression(deref
))
1889 expression_error(expr
, "bad member name");
1893 ctype
= deref
->ctype
;
1894 examine_symbol_type(ctype
);
1895 address_space
= ctype
->ctype
.as
;
1896 mod
= ctype
->ctype
.modifiers
;
1897 if (ctype
->type
== SYM_NODE
) {
1898 ctype
= ctype
->ctype
.base_type
;
1899 address_space
|= ctype
->ctype
.as
;
1900 mod
|= ctype
->ctype
.modifiers
;
1902 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1903 expression_error(expr
, "expected structure or union");
1907 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1909 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1910 const char *name
= "<unnamed>";
1913 name
= ctype
->ident
->name
;
1914 namelen
= ctype
->ident
->len
;
1916 if (ctype
->symbol_list
)
1917 expression_error(expr
, "no member '%s' in %s %.*s",
1918 show_ident(ident
), type
, namelen
, name
);
1920 expression_error(expr
, "using member '%s' in "
1921 "incomplete %s %.*s", show_ident(ident
),
1922 type
, namelen
, name
);
1927 * The member needs to take on the address space and modifiers of
1928 * the "parent" type.
1930 member
= convert_to_as_mod(member
, address_space
, mod
);
1931 ctype
= get_base_type(member
);
1933 if (!lvalue_expression(deref
)) {
1934 if (deref
->type
!= EXPR_SLICE
) {
1938 expr
->base
= deref
->base
;
1939 expr
->r_bitpos
= deref
->r_bitpos
;
1941 expr
->r_bitpos
+= offset
<< 3;
1942 expr
->type
= EXPR_SLICE
;
1943 expr
->r_nrbits
= member
->bit_size
;
1944 expr
->r_bitpos
+= member
->bit_offset
;
1945 expr
->ctype
= member
;
1949 deref
= deref
->unop
;
1950 expr
->deref
= deref
;
1952 add
= evaluate_offset(deref
, offset
);
1953 expr
->type
= EXPR_PREOP
;
1957 expr
->ctype
= member
;
1961 static int is_promoted(struct expression
*expr
)
1964 switch (expr
->type
) {
1967 case EXPR_CONDITIONAL
:
1991 static struct symbol
*evaluate_cast(struct expression
*);
1993 static struct symbol
*evaluate_type_information(struct expression
*expr
)
1995 struct symbol
*sym
= expr
->cast_type
;
1997 sym
= evaluate_expression(expr
->cast_expression
);
2001 * Expressions of restricted types will possibly get
2002 * promoted - check that here
2004 if (is_restricted_type(sym
)) {
2005 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
2007 } else if (is_fouled_type(sym
)) {
2011 examine_symbol_type(sym
);
2012 if (is_bitfield_type(sym
)) {
2013 expression_error(expr
, "trying to examine bitfield type");
2019 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
2021 struct symbol
*type
;
2024 type
= evaluate_type_information(expr
);
2028 size
= type
->bit_size
;
2029 if ((size
< 0) || (size
& 7))
2030 expression_error(expr
, "cannot size expression");
2031 expr
->type
= EXPR_VALUE
;
2032 expr
->value
= size
>> 3;
2034 expr
->ctype
= size_t_ctype
;
2035 return size_t_ctype
;
2038 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
2040 struct symbol
*type
;
2043 type
= evaluate_type_information(expr
);
2047 if (type
->type
== SYM_NODE
)
2048 type
= type
->ctype
.base_type
;
2051 switch (type
->type
) {
2055 type
= get_base_type(type
);
2059 expression_error(expr
, "expected pointer expression");
2062 size
= type
->bit_size
;
2065 expr
->type
= EXPR_VALUE
;
2066 expr
->value
= size
>> 3;
2068 expr
->ctype
= size_t_ctype
;
2069 return size_t_ctype
;
2072 static struct symbol
*evaluate_alignof(struct expression
*expr
)
2074 struct symbol
*type
;
2076 type
= evaluate_type_information(expr
);
2080 expr
->type
= EXPR_VALUE
;
2081 expr
->value
= type
->ctype
.alignment
;
2083 expr
->ctype
= size_t_ctype
;
2084 return size_t_ctype
;
2087 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
2089 struct expression
*expr
;
2090 struct symbol_list
*argument_types
= fn
->arguments
;
2091 struct symbol
*argtype
;
2094 PREPARE_PTR_LIST(argument_types
, argtype
);
2095 FOR_EACH_PTR (head
, expr
) {
2096 struct expression
**p
= THIS_ADDRESS(expr
);
2097 struct symbol
*ctype
, *target
;
2098 ctype
= evaluate_expression(expr
);
2105 struct symbol
*type
;
2106 int class = classify_type(ctype
, &type
);
2107 if (is_int(class)) {
2108 *p
= cast_to(expr
, integer_promotion(type
));
2109 } else if (class & TYPE_FLOAT
) {
2110 unsigned long mod
= type
->ctype
.modifiers
;
2111 if (!(mod
& (MOD_LONG
|MOD_LONGLONG
)))
2112 *p
= cast_to(expr
, &double_ctype
);
2113 } else if (class & TYPE_PTR
) {
2114 if (expr
->ctype
== &null_ctype
)
2115 *p
= cast_to(expr
, &ptr_ctype
);
2120 static char where
[30];
2121 examine_symbol_type(target
);
2122 sprintf(where
, "argument %d", i
);
2123 compatible_assignment_types(expr
, target
, p
, where
);
2127 NEXT_PTR_LIST(argtype
);
2128 } END_FOR_EACH_PTR(expr
);
2129 FINISH_PTR_LIST(argtype
);
2133 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
2137 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
2138 if (sym
->ident
== ident
)
2140 } END_FOR_EACH_PTR(sym
);
2144 static void convert_index(struct expression
*e
)
2146 struct expression
*child
= e
->idx_expression
;
2147 unsigned from
= e
->idx_from
;
2148 unsigned to
= e
->idx_to
+ 1;
2150 e
->init_offset
= from
* (e
->ctype
->bit_size
>>3);
2151 e
->init_nr
= to
- from
;
2152 e
->init_expr
= child
;
2155 static void convert_ident(struct expression
*e
)
2157 struct expression
*child
= e
->ident_expression
;
2158 struct symbol
*sym
= e
->field
;
2160 e
->init_offset
= sym
->offset
;
2162 e
->init_expr
= child
;
2165 static void convert_designators(struct expression
*e
)
2168 if (e
->type
== EXPR_INDEX
)
2170 else if (e
->type
== EXPR_IDENTIFIER
)
2178 static void excess(struct expression
*e
, const char *s
)
2180 warning(e
->pos
, "excessive elements in %s initializer", s
);
2184 * implicit designator for the first element
2186 static struct expression
*first_subobject(struct symbol
*ctype
, int class,
2187 struct expression
**v
)
2189 struct expression
*e
= *v
, *new;
2191 if (ctype
->type
== SYM_NODE
)
2192 ctype
= ctype
->ctype
.base_type
;
2194 if (class & TYPE_PTR
) { /* array */
2195 if (!ctype
->bit_size
)
2197 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2198 new->idx_expression
= e
;
2199 new->ctype
= ctype
->ctype
.base_type
;
2201 struct symbol
*field
, *p
;
2202 PREPARE_PTR_LIST(ctype
->symbol_list
, p
);
2203 while (p
&& !p
->ident
&& is_bitfield_type(p
))
2209 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2210 new->ident_expression
= e
;
2211 new->field
= new->ctype
= field
;
2218 * sanity-check explicit designators; return the innermost one or NULL
2219 * in case of error. Assign types.
2221 static struct expression
*check_designators(struct expression
*e
,
2222 struct symbol
*ctype
)
2224 struct expression
*last
= NULL
;
2227 if (ctype
->type
== SYM_NODE
)
2228 ctype
= ctype
->ctype
.base_type
;
2229 if (e
->type
== EXPR_INDEX
) {
2230 struct symbol
*type
;
2231 if (ctype
->type
!= SYM_ARRAY
) {
2232 err
= "array index in non-array";
2235 type
= ctype
->ctype
.base_type
;
2236 if (ctype
->bit_size
>= 0 && type
->bit_size
>= 0) {
2237 unsigned offset
= e
->idx_to
* type
->bit_size
;
2238 if (offset
>= ctype
->bit_size
) {
2239 err
= "index out of bounds in";
2243 e
->ctype
= ctype
= type
;
2246 if (!e
->idx_expression
) {
2250 e
= e
->idx_expression
;
2251 } else if (e
->type
== EXPR_IDENTIFIER
) {
2252 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2253 err
= "field name not in struct or union";
2256 ctype
= find_struct_ident(ctype
, e
->expr_ident
);
2258 err
= "unknown field name in";
2261 e
->field
= e
->ctype
= ctype
;
2263 if (!e
->ident_expression
) {
2267 e
= e
->ident_expression
;
2268 } else if (e
->type
== EXPR_POS
) {
2269 err
= "internal front-end error: EXPR_POS in";
2274 expression_error(e
, "%s initializer", err
);
2279 * choose the next subobject to initialize.
2281 * Get designators for next element, switch old ones to EXPR_POS.
2282 * Return the resulting expression or NULL if we'd run out of subobjects.
2283 * The innermost designator is returned in *v. Designators in old
2284 * are assumed to be already sanity-checked.
2286 static struct expression
*next_designators(struct expression
*old
,
2287 struct symbol
*ctype
,
2288 struct expression
*e
, struct expression
**v
)
2290 struct expression
*new = NULL
;
2294 if (old
->type
== EXPR_INDEX
) {
2295 struct expression
*copy
;
2298 copy
= next_designators(old
->idx_expression
,
2301 n
= old
->idx_to
+ 1;
2302 if (n
* old
->ctype
->bit_size
== ctype
->bit_size
) {
2307 *v
= new = alloc_expression(e
->pos
, EXPR_INDEX
);
2310 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2313 new->idx_from
= new->idx_to
= n
;
2314 new->idx_expression
= copy
;
2315 new->ctype
= old
->ctype
;
2317 } else if (old
->type
== EXPR_IDENTIFIER
) {
2318 struct expression
*copy
;
2319 struct symbol
*field
;
2321 copy
= next_designators(old
->ident_expression
,
2324 field
= old
->field
->next_subobject
;
2330 *v
= new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2333 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2337 new->expr_ident
= field
->ident
;
2338 new->ident_expression
= copy
;
2345 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2346 int class, struct symbol
*ctype
);
2349 * deal with traversing subobjects [6.7.8(17,18,20)]
2351 static void handle_list_initializer(struct expression
*expr
,
2352 int class, struct symbol
*ctype
)
2354 struct expression
*e
, *last
= NULL
, *top
= NULL
, *next
;
2357 FOR_EACH_PTR(expr
->expr_list
, e
) {
2358 struct expression
**v
;
2359 struct symbol
*type
;
2362 if (e
->type
!= EXPR_INDEX
&& e
->type
!= EXPR_IDENTIFIER
) {
2365 last
= first_subobject(ctype
, class, &top
);
2367 last
= next_designators(last
, ctype
, e
, &top
);
2370 excess(e
, class & TYPE_PTR
? "array" :
2372 DELETE_CURRENT_PTR(e
);
2376 warning(e
->pos
, "advancing past deep designator");
2379 REPLACE_CURRENT_PTR(e
, last
);
2381 next
= check_designators(e
, ctype
);
2383 DELETE_CURRENT_PTR(e
);
2387 /* deeper than one designator? */
2389 convert_designators(last
);
2394 lclass
= classify_type(top
->ctype
, &type
);
2395 if (top
->type
== EXPR_INDEX
)
2396 v
= &top
->idx_expression
;
2398 v
= &top
->ident_expression
;
2400 if (handle_simple_initializer(v
, 1, lclass
, top
->ctype
))
2403 if (!(lclass
& TYPE_COMPOUND
)) {
2404 warning(e
->pos
, "bogus scalar initializer");
2405 DELETE_CURRENT_PTR(e
);
2409 next
= first_subobject(type
, lclass
, v
);
2411 warning(e
->pos
, "missing braces around initializer");
2416 DELETE_CURRENT_PTR(e
);
2417 excess(e
, lclass
& TYPE_PTR
? "array" : "struct or union");
2419 } END_FOR_EACH_PTR(e
);
2421 convert_designators(last
);
2422 expr
->ctype
= ctype
;
2425 static int is_string_literal(struct expression
**v
)
2427 struct expression
*e
= *v
;
2428 while (e
&& e
->type
== EXPR_PREOP
&& e
->op
== '(')
2430 if (!e
|| e
->type
!= EXPR_STRING
)
2432 if (e
!= *v
&& Wparen_string
)
2434 "array initialized from parenthesized string constant");
2440 * We want a normal expression, possibly in one layer of braces. Warn
2441 * if the latter happens inside a list (it's legal, but likely to be
2442 * an effect of screwup). In case of anything not legal, we are definitely
2443 * having an effect of screwup, so just fail and let the caller warn.
2445 static struct expression
*handle_scalar(struct expression
*e
, int nested
)
2447 struct expression
*v
= NULL
, *p
;
2451 if (e
->type
!= EXPR_INITIALIZER
)
2454 FOR_EACH_PTR(e
->expr_list
, p
) {
2458 } END_FOR_EACH_PTR(p
);
2462 case EXPR_INITIALIZER
:
2464 case EXPR_IDENTIFIER
:
2470 warning(e
->pos
, "braces around scalar initializer");
2475 * deal with the cases that don't care about subobjects:
2476 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2477 * character array <- string literal, possibly in braces [6.7.8(14)]
2478 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2479 * compound type <- initializer list in braces [6.7.8(16)]
2480 * The last one punts to handle_list_initializer() which, in turn will call
2481 * us for individual elements of the list.
2483 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2484 * the lack of support of wide char stuff in general.
2486 * One note: we need to take care not to evaluate a string literal until
2487 * we know that we *will* handle it right here. Otherwise we would screw
2488 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2489 * { "string", ...} - we need to preserve that string literal recognizable
2490 * until we dig into the inner struct.
2492 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2493 int class, struct symbol
*ctype
)
2495 int is_string
= is_string_type(ctype
);
2496 struct expression
*e
= *ep
, *p
;
2497 struct symbol
*type
;
2503 if (!(class & TYPE_COMPOUND
)) {
2504 e
= handle_scalar(e
, nested
);
2508 if (!evaluate_expression(e
))
2510 compatible_assignment_types(e
, ctype
, ep
, "initializer");
2515 * sublist; either a string, or we dig in; the latter will deal with
2516 * pathologies, so we don't need anything fancy here.
2518 if (e
->type
== EXPR_INITIALIZER
) {
2520 struct expression
*v
= NULL
;
2523 FOR_EACH_PTR(e
->expr_list
, p
) {
2527 } END_FOR_EACH_PTR(p
);
2528 if (count
== 1 && is_string_literal(&v
)) {
2533 handle_list_initializer(e
, class, ctype
);
2538 if (is_string_literal(&e
)) {
2539 /* either we are doing array of char, or we'll have to dig in */
2546 /* struct or union can be initialized by compatible */
2547 if (class != TYPE_COMPOUND
)
2549 type
= evaluate_expression(e
);
2552 if (ctype
->type
== SYM_NODE
)
2553 ctype
= ctype
->ctype
.base_type
;
2554 if (type
->type
== SYM_NODE
)
2555 type
= type
->ctype
.base_type
;
2561 p
= alloc_expression(e
->pos
, EXPR_STRING
);
2563 type
= evaluate_expression(p
);
2564 if (ctype
->bit_size
!= -1 &&
2565 ctype
->bit_size
+ bits_in_char
< type
->bit_size
) {
2567 "too long initializer-string for array of char");
2573 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2575 struct symbol
*type
;
2576 int class = classify_type(ctype
, &type
);
2577 if (!handle_simple_initializer(ep
, 0, class, ctype
))
2578 expression_error(*ep
, "invalid initializer");
2581 static struct symbol
*evaluate_cast(struct expression
*expr
)
2583 struct expression
*target
= expr
->cast_expression
;
2584 struct symbol
*ctype
;
2585 struct symbol
*t1
, *t2
;
2587 int as1
= 0, as2
= 0;
2593 * Special case: a cast can be followed by an
2594 * initializer, in which case we need to pass
2595 * the type value down to that initializer rather
2596 * than trying to evaluate it as an expression
2598 * A more complex case is when the initializer is
2599 * dereferenced as part of a post-fix expression.
2600 * We need to produce an expression that can be dereferenced.
2602 if (target
->type
== EXPR_INITIALIZER
) {
2603 struct symbol
*sym
= expr
->cast_type
;
2604 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2606 sym
->initializer
= target
;
2607 evaluate_symbol(sym
);
2609 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2612 expr
->type
= EXPR_PREOP
;
2620 ctype
= examine_symbol_type(expr
->cast_type
);
2621 expr
->ctype
= ctype
;
2622 expr
->cast_type
= ctype
;
2624 evaluate_expression(target
);
2627 class1
= classify_type(ctype
, &t1
);
2629 /* cast to non-integer type -> not an integer constant expression */
2630 if (!is_int(class1
))
2632 /* if argument turns out to be not an integer constant expression *and*
2633 it was not a floating literal to start with -> too bad */
2634 else if (expr
->flags
== Int_const_expr
&&
2635 !(target
->flags
& Int_const_expr
))
2638 * You can always throw a value away by casting to
2639 * "void" - that's an implicit "force". Note that
2640 * the same is _not_ true of "void *".
2642 if (t1
== &void_ctype
)
2645 if (class1
& (TYPE_COMPOUND
| TYPE_FN
))
2646 warning(expr
->pos
, "cast to non-scalar");
2650 expression_error(expr
, "cast from unknown type");
2653 class2
= classify_type(t2
, &t2
);
2655 if (class2
& TYPE_COMPOUND
)
2656 warning(expr
->pos
, "cast from non-scalar");
2658 if (expr
->type
== EXPR_FORCE_CAST
)
2661 /* allowed cast unfouls */
2662 if (class2
& TYPE_FOULED
)
2666 if (class1
& TYPE_RESTRICT
)
2667 warning(expr
->pos
, "cast to restricted type");
2668 if (class2
& TYPE_RESTRICT
)
2669 warning(expr
->pos
, "cast from restricted type");
2672 if (t1
== &ulong_ctype
)
2674 else if (class1
== TYPE_PTR
) {
2675 examine_pointer_target(t1
);
2679 if (t2
== &ulong_ctype
)
2681 else if (class2
== TYPE_PTR
) {
2682 examine_pointer_target(t2
);
2686 if (!as1
&& as2
> 0)
2687 warning(expr
->pos
, "cast removes address space of expression");
2688 if (as1
> 0 && as2
> 0 && as1
!= as2
)
2689 warning(expr
->pos
, "cast between address spaces (<asn:%d>-><asn:%d>)", as2
, as1
);
2690 if (as1
> 0 && !as2
&&
2691 !is_null_pointer_constant(target
) && Wcast_to_as
)
2693 "cast adds address space to expression (<asn:%d>)", as1
);
2695 if (!(t1
->ctype
.modifiers
& MOD_PTRINHERIT
) && class1
== TYPE_PTR
&&
2696 !as1
&& (target
->flags
& Int_const_expr
)) {
2697 if (t1
->ctype
.base_type
== &void_ctype
) {
2698 if (is_zero_constant(target
)) {
2700 expr
->type
= EXPR_VALUE
;
2701 expr
->ctype
= &null_ctype
;
2712 * Evaluate a call expression with a symbol. This
2713 * should expand inline functions, and evaluate
2716 static int evaluate_symbol_call(struct expression
*expr
)
2718 struct expression
*fn
= expr
->fn
;
2719 struct symbol
*ctype
= fn
->ctype
;
2721 if (fn
->type
!= EXPR_PREOP
)
2724 if (ctype
->op
&& ctype
->op
->evaluate
)
2725 return ctype
->op
->evaluate(expr
);
2727 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2729 struct symbol
*curr
= current_fn
;
2730 current_fn
= ctype
->ctype
.base_type
;
2732 ret
= inline_function(expr
, ctype
);
2734 /* restore the old function */
2742 static struct symbol
*evaluate_call(struct expression
*expr
)
2745 struct symbol
*ctype
, *sym
;
2746 struct expression
*fn
= expr
->fn
;
2747 struct expression_list
*arglist
= expr
->args
;
2749 if (!evaluate_expression(fn
))
2751 sym
= ctype
= fn
->ctype
;
2752 if (ctype
->type
== SYM_NODE
)
2753 ctype
= ctype
->ctype
.base_type
;
2754 if (ctype
->type
== SYM_PTR
)
2755 ctype
= get_base_type(ctype
);
2757 if (ctype
->type
!= SYM_FN
) {
2758 struct expression
*arg
;
2759 expression_error(expr
, "not a function %s",
2760 show_ident(sym
->ident
));
2761 /* do typechecking in arguments */
2762 FOR_EACH_PTR (arglist
, arg
) {
2763 evaluate_expression(arg
);
2764 } END_FOR_EACH_PTR(arg
);
2768 examine_fn_arguments(ctype
);
2769 if (sym
->type
== SYM_NODE
&& fn
->type
== EXPR_PREOP
&&
2770 sym
->op
&& sym
->op
->args
) {
2771 if (!sym
->op
->args(expr
))
2774 if (!evaluate_arguments(sym
, ctype
, arglist
))
2776 args
= expression_list_size(expr
->args
);
2777 fnargs
= symbol_list_size(ctype
->arguments
);
2779 expression_error(expr
,
2780 "not enough arguments for function %s",
2781 show_ident(sym
->ident
));
2782 if (args
> fnargs
&& !ctype
->variadic
)
2783 expression_error(expr
,
2784 "too many arguments for function %s",
2785 show_ident(sym
->ident
));
2787 if (sym
->type
== SYM_NODE
) {
2788 if (evaluate_symbol_call(expr
))
2791 expr
->ctype
= ctype
->ctype
.base_type
;
2795 static struct symbol
*evaluate_offsetof(struct expression
*expr
)
2797 struct expression
*e
= expr
->down
;
2798 struct symbol
*ctype
= expr
->in
;
2801 if (expr
->op
== '.') {
2802 struct symbol
*field
;
2805 expression_error(expr
, "expected structure or union");
2808 examine_symbol_type(ctype
);
2809 class = classify_type(ctype
, &ctype
);
2810 if (class != TYPE_COMPOUND
) {
2811 expression_error(expr
, "expected structure or union");
2815 field
= find_identifier(expr
->ident
, ctype
->symbol_list
, &offset
);
2817 expression_error(expr
, "unknown member");
2821 expr
->type
= EXPR_VALUE
;
2822 expr
->flags
= Int_const_expr
;
2823 expr
->value
= offset
;
2825 expr
->ctype
= size_t_ctype
;
2828 expression_error(expr
, "expected structure or union");
2831 examine_symbol_type(ctype
);
2832 class = classify_type(ctype
, &ctype
);
2833 if (class != (TYPE_COMPOUND
| TYPE_PTR
)) {
2834 expression_error(expr
, "expected array");
2837 ctype
= ctype
->ctype
.base_type
;
2839 expr
->type
= EXPR_VALUE
;
2840 expr
->flags
= Int_const_expr
;
2843 expr
->ctype
= size_t_ctype
;
2845 struct expression
*idx
= expr
->index
, *m
;
2846 struct symbol
*i_type
= evaluate_expression(idx
);
2847 int i_class
= classify_type(i_type
, &i_type
);
2848 if (!is_int(i_class
)) {
2849 expression_error(expr
, "non-integer index");
2852 unrestrict(idx
, i_class
, &i_type
);
2853 idx
= cast_to(idx
, size_t_ctype
);
2854 m
= alloc_const_expression(expr
->pos
,
2855 ctype
->bit_size
>> 3);
2856 m
->ctype
= size_t_ctype
;
2857 m
->flags
= Int_const_expr
;
2858 expr
->type
= EXPR_BINOP
;
2862 expr
->ctype
= size_t_ctype
;
2863 expr
->flags
= m
->flags
& idx
->flags
& Int_const_expr
;
2867 struct expression
*copy
= __alloc_expression(0);
2869 if (e
->type
== EXPR_OFFSETOF
)
2871 if (!evaluate_expression(e
))
2873 expr
->type
= EXPR_BINOP
;
2874 expr
->flags
= e
->flags
& copy
->flags
& Int_const_expr
;
2876 expr
->ctype
= size_t_ctype
;
2880 return size_t_ctype
;
2883 struct symbol
*evaluate_expression(struct expression
*expr
)
2890 switch (expr
->type
) {
2893 expression_error(expr
, "value expression without a type");
2896 return evaluate_string(expr
);
2898 return evaluate_symbol_expression(expr
);
2900 if (!evaluate_expression(expr
->left
))
2902 if (!evaluate_expression(expr
->right
))
2904 return evaluate_binop(expr
);
2906 return evaluate_logical(expr
);
2908 evaluate_expression(expr
->left
);
2909 if (!evaluate_expression(expr
->right
))
2911 return evaluate_comma(expr
);
2913 if (!evaluate_expression(expr
->left
))
2915 if (!evaluate_expression(expr
->right
))
2917 return evaluate_compare(expr
);
2918 case EXPR_ASSIGNMENT
:
2919 if (!evaluate_expression(expr
->left
))
2921 if (!evaluate_expression(expr
->right
))
2923 return evaluate_assignment(expr
);
2925 if (!evaluate_expression(expr
->unop
))
2927 return evaluate_preop(expr
);
2929 if (!evaluate_expression(expr
->unop
))
2931 return evaluate_postop(expr
);
2933 case EXPR_FORCE_CAST
:
2934 case EXPR_IMPLIED_CAST
:
2935 return evaluate_cast(expr
);
2937 return evaluate_sizeof(expr
);
2938 case EXPR_PTRSIZEOF
:
2939 return evaluate_ptrsizeof(expr
);
2941 return evaluate_alignof(expr
);
2943 return evaluate_member_dereference(expr
);
2945 return evaluate_call(expr
);
2947 case EXPR_CONDITIONAL
:
2948 return evaluate_conditional_expression(expr
);
2949 case EXPR_STATEMENT
:
2950 expr
->ctype
= evaluate_statement(expr
->statement
);
2954 expr
->ctype
= &ptr_ctype
;
2958 /* Evaluate the type of the symbol .. */
2959 evaluate_symbol(expr
->symbol
);
2960 /* .. but the type of the _expression_ is a "type" */
2961 expr
->ctype
= &type_ctype
;
2965 return evaluate_offsetof(expr
);
2967 /* These can not exist as stand-alone expressions */
2968 case EXPR_INITIALIZER
:
2969 case EXPR_IDENTIFIER
:
2972 expression_error(expr
, "internal front-end error: initializer in expression");
2975 expression_error(expr
, "internal front-end error: SLICE re-evaluated");
2981 static void check_duplicates(struct symbol
*sym
)
2984 struct symbol
*next
= sym
;
2986 while ((next
= next
->same_symbol
) != NULL
) {
2987 const char *typediff
;
2988 evaluate_symbol(next
);
2990 typediff
= type_difference(&sym
->ctype
, &next
->ctype
, 0, 0);
2992 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2993 show_ident(sym
->ident
),
2994 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
2999 unsigned long mod
= sym
->ctype
.modifiers
;
3000 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
3002 if (!(mod
& MOD_TOPLEVEL
))
3006 if (sym
->ident
== &main_ident
)
3008 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
3012 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
3014 struct symbol
*base_type
;
3022 sym
= examine_symbol_type(sym
);
3023 base_type
= get_base_type(sym
);
3027 /* Evaluate the initializers */
3028 if (sym
->initializer
)
3029 evaluate_initializer(sym
, &sym
->initializer
);
3031 /* And finally, evaluate the body of the symbol too */
3032 if (base_type
->type
== SYM_FN
) {
3033 struct symbol
*curr
= current_fn
;
3035 current_fn
= base_type
;
3037 examine_fn_arguments(base_type
);
3038 if (!base_type
->stmt
&& base_type
->inline_stmt
)
3040 if (base_type
->stmt
)
3041 evaluate_statement(base_type
->stmt
);
3049 void evaluate_symbol_list(struct symbol_list
*list
)
3053 FOR_EACH_PTR(list
, sym
) {
3054 evaluate_symbol(sym
);
3055 check_duplicates(sym
);
3056 } END_FOR_EACH_PTR(sym
);
3059 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
3061 struct expression
*expr
= stmt
->expression
;
3062 struct symbol
*fntype
;
3064 evaluate_expression(expr
);
3065 fntype
= current_fn
->ctype
.base_type
;
3066 if (!fntype
|| fntype
== &void_ctype
) {
3067 if (expr
&& expr
->ctype
!= &void_ctype
)
3068 expression_error(expr
, "return expression in %s function", fntype
?"void":"typeless");
3069 if (expr
&& Wreturn_void
)
3070 warning(stmt
->pos
, "returning void-valued expression");
3075 sparse_error(stmt
->pos
, "return with no return value");
3080 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, "return expression");
3084 static void evaluate_if_statement(struct statement
*stmt
)
3086 if (!stmt
->if_conditional
)
3089 evaluate_conditional(stmt
->if_conditional
, 0);
3090 evaluate_statement(stmt
->if_true
);
3091 evaluate_statement(stmt
->if_false
);
3094 static void evaluate_iterator(struct statement
*stmt
)
3096 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
3097 evaluate_conditional(stmt
->iterator_post_condition
,1);
3098 evaluate_statement(stmt
->iterator_pre_statement
);
3099 evaluate_statement(stmt
->iterator_statement
);
3100 evaluate_statement(stmt
->iterator_post_statement
);
3103 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
3105 switch (*constraint
) {
3106 case '=': /* Assignment */
3107 case '+': /* Update */
3110 expression_error(expr
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
3114 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
3116 switch (*constraint
) {
3117 case '=': /* Assignment */
3118 case '+': /* Update */
3119 expression_error(expr
, "input constraint with assignment (\"%s\")", constraint
);
3123 static void evaluate_asm_statement(struct statement
*stmt
)
3125 struct expression
*expr
;
3128 expr
= stmt
->asm_string
;
3129 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3130 sparse_error(stmt
->pos
, "need constant string for inline asm");
3135 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
3136 struct ident
*ident
;
3139 case 0: /* Identifier */
3141 ident
= (struct ident
*)expr
;
3144 case 1: /* Constraint */
3146 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3147 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
3148 *THIS_ADDRESS(expr
) = NULL
;
3151 verify_output_constraint(expr
, expr
->string
->data
);
3154 case 2: /* Expression */
3156 if (!evaluate_expression(expr
))
3158 if (!lvalue_expression(expr
))
3159 warning(expr
->pos
, "asm output is not an lvalue");
3160 evaluate_assign_to(expr
, expr
->ctype
);
3163 } END_FOR_EACH_PTR(expr
);
3166 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
3167 struct ident
*ident
;
3170 case 0: /* Identifier */
3172 ident
= (struct ident
*)expr
;
3175 case 1: /* Constraint */
3177 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3178 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
3179 *THIS_ADDRESS(expr
) = NULL
;
3182 verify_input_constraint(expr
, expr
->string
->data
);
3185 case 2: /* Expression */
3187 if (!evaluate_expression(expr
))
3191 } END_FOR_EACH_PTR(expr
);
3193 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
3195 sparse_error(stmt
->pos
, "bad asm output");
3198 if (expr
->type
== EXPR_STRING
)
3200 expression_error(expr
, "asm clobber is not a string");
3201 } END_FOR_EACH_PTR(expr
);
3204 static void evaluate_case_statement(struct statement
*stmt
)
3206 evaluate_expression(stmt
->case_expression
);
3207 evaluate_expression(stmt
->case_to
);
3208 evaluate_statement(stmt
->case_statement
);
3211 static void check_case_type(struct expression
*switch_expr
,
3212 struct expression
*case_expr
,
3213 struct expression
**enumcase
)
3215 struct symbol
*switch_type
, *case_type
;
3221 switch_type
= switch_expr
->ctype
;
3222 case_type
= evaluate_expression(case_expr
);
3224 if (!switch_type
|| !case_type
)
3228 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
3229 else if (is_enum_type(case_type
))
3230 *enumcase
= case_expr
;
3233 sclass
= classify_type(switch_type
, &switch_type
);
3234 cclass
= classify_type(case_type
, &case_type
);
3236 /* both should be arithmetic */
3237 if (!(sclass
& cclass
& TYPE_NUM
))
3240 /* neither should be floating */
3241 if ((sclass
| cclass
) & TYPE_FLOAT
)
3244 /* if neither is restricted, we are OK */
3245 if (!((sclass
| cclass
) & TYPE_RESTRICT
))
3248 if (!restricted_binop_type(SPECIAL_EQUAL
, case_expr
, switch_expr
,
3249 cclass
, sclass
, case_type
, switch_type
))
3250 warning(case_expr
->pos
, "restricted degrades to integer");
3255 expression_error(case_expr
, "incompatible types for 'case' statement");
3258 static void evaluate_switch_statement(struct statement
*stmt
)
3261 struct expression
*enumcase
= NULL
;
3262 struct expression
**enumcase_holder
= &enumcase
;
3263 struct expression
*sel
= stmt
->switch_expression
;
3265 evaluate_expression(sel
);
3266 evaluate_statement(stmt
->switch_statement
);
3269 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
3270 enumcase_holder
= NULL
; /* Only check cases against switch */
3272 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
3273 struct statement
*case_stmt
= sym
->stmt
;
3274 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
3275 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
3276 } END_FOR_EACH_PTR(sym
);
3279 struct symbol
*evaluate_statement(struct statement
*stmt
)
3284 switch (stmt
->type
) {
3285 case STMT_DECLARATION
: {
3287 FOR_EACH_PTR(stmt
->declaration
, s
) {
3289 } END_FOR_EACH_PTR(s
);
3294 return evaluate_return_expression(stmt
);
3296 case STMT_EXPRESSION
:
3297 if (!evaluate_expression(stmt
->expression
))
3299 if (stmt
->expression
->ctype
== &null_ctype
)
3300 stmt
->expression
= cast_to(stmt
->expression
, &ptr_ctype
);
3301 return degenerate(stmt
->expression
);
3303 case STMT_COMPOUND
: {
3304 struct statement
*s
;
3305 struct symbol
*type
= NULL
;
3307 /* Evaluate the return symbol in the compound statement */
3308 evaluate_symbol(stmt
->ret
);
3311 * Then, evaluate each statement, making the type of the
3312 * compound statement be the type of the last statement
3314 type
= evaluate_statement(stmt
->args
);
3315 FOR_EACH_PTR(stmt
->stmts
, s
) {
3316 type
= evaluate_statement(s
);
3317 } END_FOR_EACH_PTR(s
);
3323 evaluate_if_statement(stmt
);
3326 evaluate_iterator(stmt
);
3329 evaluate_switch_statement(stmt
);
3332 evaluate_case_statement(stmt
);
3335 return evaluate_statement(stmt
->label_statement
);
3337 evaluate_expression(stmt
->goto_expression
);
3342 evaluate_asm_statement(stmt
);
3345 evaluate_expression(stmt
->expression
);
3348 evaluate_expression(stmt
->range_expression
);
3349 evaluate_expression(stmt
->range_low
);
3350 evaluate_expression(stmt
->range_high
);