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 /* type should be SYM_FOULED */
448 static inline struct symbol
*unfoul(struct symbol
*type
)
450 return type
->ctype
.base_type
;
453 static struct symbol
*restricted_binop_type(int op
,
454 struct expression
*left
,
455 struct expression
*right
,
456 int lclass
, int rclass
,
457 struct symbol
*ltype
,
458 struct symbol
*rtype
)
460 struct symbol
*ctype
= NULL
;
461 if (lclass
& TYPE_RESTRICT
) {
462 if (rclass
& TYPE_RESTRICT
) {
463 if (ltype
== rtype
) {
465 } else if (lclass
& TYPE_FOULED
) {
466 if (unfoul(ltype
) == rtype
)
468 } else if (rclass
& TYPE_FOULED
) {
469 if (unfoul(rtype
) == ltype
)
473 if (!restricted_value(right
, ltype
))
476 } else if (!restricted_value(left
, rtype
))
480 switch (restricted_binop(op
, ctype
)) {
482 if ((lclass
^ rclass
) & TYPE_FOULED
)
483 ctype
= unfoul(ctype
);
486 if (!(lclass
& rclass
& TYPE_FOULED
))
498 static inline void unrestrict(struct expression
*expr
,
499 int class, struct symbol
**ctype
)
501 if (class & TYPE_RESTRICT
) {
502 warning(expr
->pos
, "restricted degrades to integer");
503 if (class & TYPE_FOULED
)
504 *ctype
= unfoul(*ctype
);
505 *ctype
= (*ctype
)->ctype
.base_type
; /* get to arithmetic type */
509 static struct symbol
*usual_conversions(int op
,
510 struct expression
*left
,
511 struct expression
*right
,
512 int lclass
, int rclass
,
513 struct symbol
*ltype
,
514 struct symbol
*rtype
)
516 struct symbol
*ctype
;
518 warn_for_different_enum_types(right
->pos
, left
->ctype
, right
->ctype
);
520 if ((lclass
| rclass
) & TYPE_RESTRICT
)
524 if (!(lclass
& TYPE_FLOAT
)) {
525 if (!(rclass
& TYPE_FLOAT
))
526 return bigger_int_type(ltype
, rtype
);
529 } else if (rclass
& TYPE_FLOAT
) {
530 unsigned long lmod
= ltype
->ctype
.modifiers
;
531 unsigned long rmod
= rtype
->ctype
.modifiers
;
532 if (rmod
& ~lmod
& (MOD_LONG
| MOD_LONGLONG
))
540 ctype
= restricted_binop_type(op
, left
, right
,
541 lclass
, rclass
, ltype
, rtype
);
545 unrestrict(left
, lclass
, <ype
);
546 unrestrict(right
, rclass
, &rtype
);
551 static inline int lvalue_expression(struct expression
*expr
)
553 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
556 static int ptr_object_size(struct symbol
*ptr_type
)
558 if (ptr_type
->type
== SYM_NODE
)
559 ptr_type
= ptr_type
->ctype
.base_type
;
560 if (ptr_type
->type
== SYM_PTR
)
561 ptr_type
= get_base_type(ptr_type
);
562 return ptr_type
->bit_size
;
565 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*ctype
, struct symbol
*itype
)
567 struct expression
*index
= expr
->right
;
571 if (ctype
== &null_ctype
)
574 examine_symbol_type(ctype
);
576 if (!ctype
->ctype
.base_type
) {
577 expression_error(expr
, "missing type information");
581 /* Get the size of whatever the pointer points to */
582 bit_size
= ptr_object_size(ctype
);
583 multiply
= bit_size
>> 3;
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 if (diff
& ~MOD_SIGNEDNESS
)
772 return "different modifiers";
774 /* Differs in signedness only.. */
777 * Warn if both are explicitly signed ("unsigned" is obviously
778 * always explicit, and since we know one of them has to be
779 * unsigned, we check if the signed one was explicit).
781 if ((mod1
| mod2
) & MOD_EXPLICITLY_SIGNED
)
782 return "different explicit signedness";
785 * "char" matches both "unsigned char" and "signed char",
786 * so if the explicit test didn't trigger, then we should
787 * not warn about a char.
789 if (!(mod1
& MOD_CHAR
))
790 return "different signedness";
797 if (Waddress_space
&& as1
!= as2
)
798 return "different address spaces";
799 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
800 return "different modifiers";
804 static void bad_null(struct expression
*expr
)
806 if (Wnon_pointer_null
)
807 warning(expr
->pos
, "Using plain integer as NULL pointer");
810 static unsigned long target_qualifiers(struct symbol
*type
)
812 unsigned long mod
= type
->ctype
.modifiers
& MOD_IGN
;
813 if (type
->ctype
.base_type
&& type
->ctype
.base_type
->type
== SYM_ARRAY
)
818 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
)
820 const char *typediff
;
821 struct symbol
*ltype
, *rtype
;
822 struct expression
*l
= expr
->left
;
823 struct expression
*r
= expr
->right
;
824 struct symbol
*lbase
, *rbase
;
826 classify_type(degenerate(l
), <ype
);
827 classify_type(degenerate(r
), &rtype
);
829 lbase
= examine_pointer_target(ltype
);
830 rbase
= examine_pointer_target(rtype
);
831 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
832 target_qualifiers(rtype
),
833 target_qualifiers(ltype
));
835 expression_error(expr
, "subtraction of different types can't work (%s)", typediff
);
837 if (lbase
->type
== SYM_FN
) {
838 expression_error(expr
, "subtraction of functions? Share your drugs");
842 expr
->ctype
= ssize_t_ctype
;
843 if (lbase
->bit_size
> bits_in_char
) {
844 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
845 struct expression
*div
= expr
;
846 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
847 unsigned long value
= lbase
->bit_size
>> 3;
849 val
->ctype
= size_t_ctype
;
852 if (value
& (value
-1)) {
853 if (Wptr_subtraction_blows
)
854 warning(expr
->pos
, "potentially expensive pointer subtraction");
858 sub
->ctype
= ssize_t_ctype
;
867 return ssize_t_ctype
;
870 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
872 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
874 struct symbol
*ctype
;
879 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
880 warning(expr
->pos
, "assignment expression in conditional");
882 ctype
= evaluate_expression(expr
);
884 if (is_safe_type(ctype
))
885 warning(expr
->pos
, "testing a 'safe expression'");
891 static struct symbol
*evaluate_logical(struct expression
*expr
)
893 if (!evaluate_conditional(expr
->left
, 0))
895 if (!evaluate_conditional(expr
->right
, 0))
898 expr
->ctype
= &bool_ctype
;
900 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
906 static struct symbol
*evaluate_binop(struct expression
*expr
)
908 struct symbol
*ltype
, *rtype
, *ctype
;
909 int lclass
= classify_type(expr
->left
->ctype
, <ype
);
910 int rclass
= classify_type(expr
->right
->ctype
, &rtype
);
914 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
918 /* number op number */
919 if (lclass
& rclass
& TYPE_NUM
) {
920 if ((lclass
| rclass
) & TYPE_FLOAT
) {
922 case '+': case '-': case '*': case '/':
925 return bad_expr_type(expr
);
929 if (op
== SPECIAL_LEFTSHIFT
|| op
== SPECIAL_RIGHTSHIFT
) {
930 // shifts do integer promotions, but that's it.
931 unrestrict(expr
->left
, lclass
, <ype
);
932 unrestrict(expr
->right
, rclass
, &rtype
);
933 ctype
= ltype
= integer_promotion(ltype
);
934 rtype
= integer_promotion(rtype
);
936 // The rest do usual conversions
937 ltype
= usual_conversions(op
, expr
->left
, expr
->right
,
938 lclass
, rclass
, ltype
, rtype
);
939 ctype
= rtype
= ltype
;
942 expr
->left
= cast_to(expr
->left
, ltype
);
943 expr
->right
= cast_to(expr
->right
, rtype
);
948 /* pointer (+|-) integer */
949 if (lclass
& TYPE_PTR
&& is_int(rclass
) && (op
== '+' || op
== '-')) {
950 unrestrict(expr
->right
, rclass
, &rtype
);
951 return evaluate_ptr_add(expr
, degenerate(expr
->left
), rtype
);
954 /* integer + pointer */
955 if (rclass
& TYPE_PTR
&& is_int(lclass
) && op
== '+') {
956 struct expression
*index
= expr
->left
;
957 unrestrict(index
, lclass
, <ype
);
958 expr
->left
= expr
->right
;
960 return evaluate_ptr_add(expr
, degenerate(expr
->left
), ltype
);
963 /* pointer - pointer */
964 if (lclass
& rclass
& TYPE_PTR
&& expr
->op
== '-')
965 return evaluate_ptr_sub(expr
);
967 return bad_expr_type(expr
);
970 static struct symbol
*evaluate_comma(struct expression
*expr
)
972 expr
->ctype
= degenerate(expr
->right
);
973 if (expr
->ctype
== &null_ctype
)
974 expr
->ctype
= &ptr_ctype
;
975 expr
->flags
&= expr
->left
->flags
& expr
->right
->flags
;
979 static int modify_for_unsigned(int op
)
982 op
= SPECIAL_UNSIGNED_LT
;
984 op
= SPECIAL_UNSIGNED_GT
;
985 else if (op
== SPECIAL_LTE
)
986 op
= SPECIAL_UNSIGNED_LTE
;
987 else if (op
== SPECIAL_GTE
)
988 op
= SPECIAL_UNSIGNED_GTE
;
992 static inline int is_null_pointer_constant(struct expression
*e
)
994 if (e
->ctype
== &null_ctype
)
996 if (!(e
->flags
& Int_const_expr
))
998 return is_zero_constant(e
) ? 2 : 0;
1001 static struct symbol
*evaluate_compare(struct expression
*expr
)
1003 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1004 struct symbol
*ltype
, *rtype
, *lbase
, *rbase
;
1005 int lclass
= classify_type(degenerate(left
), <ype
);
1006 int rclass
= classify_type(degenerate(right
), &rtype
);
1007 struct symbol
*ctype
;
1008 const char *typediff
;
1011 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
1016 if (is_type_type(ltype
) && is_type_type(rtype
))
1019 if (is_safe_type(left
->ctype
) || is_safe_type(right
->ctype
))
1020 warning(expr
->pos
, "testing a 'safe expression'");
1022 /* number on number */
1023 if (lclass
& rclass
& TYPE_NUM
) {
1024 ctype
= usual_conversions(expr
->op
, expr
->left
, expr
->right
,
1025 lclass
, rclass
, ltype
, rtype
);
1026 expr
->left
= cast_to(expr
->left
, ctype
);
1027 expr
->right
= cast_to(expr
->right
, ctype
);
1028 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
1029 expr
->op
= modify_for_unsigned(expr
->op
);
1033 /* at least one must be a pointer */
1034 if (!((lclass
| rclass
) & TYPE_PTR
))
1035 return bad_expr_type(expr
);
1037 /* equality comparisons can be with null pointer constants */
1038 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1039 int is_null1
= is_null_pointer_constant(left
);
1040 int is_null2
= is_null_pointer_constant(right
);
1045 if (is_null1
&& is_null2
) {
1046 int positive
= expr
->op
== SPECIAL_EQUAL
;
1047 expr
->type
= EXPR_VALUE
;
1048 expr
->value
= positive
;
1052 left
= cast_to(left
, rtype
);
1056 right
= cast_to(right
, ltype
);
1060 /* both should be pointers */
1061 if (!(lclass
& rclass
& TYPE_PTR
))
1062 return bad_expr_type(expr
);
1063 expr
->op
= modify_for_unsigned(expr
->op
);
1065 lbase
= examine_pointer_target(ltype
);
1066 rbase
= examine_pointer_target(rtype
);
1068 /* they also have special treatment for pointers to void */
1069 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1070 if (ltype
->ctype
.as
== rtype
->ctype
.as
) {
1071 if (lbase
== &void_ctype
) {
1072 right
= cast_to(right
, ltype
);
1075 if (rbase
== &void_ctype
) {
1076 left
= cast_to(left
, rtype
);
1082 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1083 target_qualifiers(rtype
),
1084 target_qualifiers(ltype
));
1088 expression_error(expr
, "incompatible types in comparison expression (%s)", typediff
);
1092 expr
->ctype
= &bool_ctype
;
1097 * NOTE! The degenerate case of "x ? : y", where we don't
1098 * have a true case, this will possibly promote "x" to the
1099 * same type as "y", and thus _change_ the conditional
1100 * test in the expression. But since promotion is "safe"
1101 * for testing, that's OK.
1103 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
1105 struct expression
**true;
1106 struct symbol
*ctype
, *ltype
, *rtype
, *lbase
, *rbase
;
1108 const char * typediff
;
1111 if (!evaluate_conditional(expr
->conditional
, 0))
1113 if (!evaluate_expression(expr
->cond_false
))
1116 ctype
= degenerate(expr
->conditional
);
1117 rtype
= degenerate(expr
->cond_false
);
1119 true = &expr
->conditional
;
1121 if (expr
->cond_true
) {
1122 if (!evaluate_expression(expr
->cond_true
))
1124 ltype
= degenerate(expr
->cond_true
);
1125 true = &expr
->cond_true
;
1129 int flags
= expr
->conditional
->flags
& Int_const_expr
;
1130 flags
&= (*true)->flags
& expr
->cond_false
->flags
;
1135 lclass
= classify_type(ltype
, <ype
);
1136 rclass
= classify_type(rtype
, &rtype
);
1137 if (lclass
& rclass
& TYPE_NUM
) {
1138 ctype
= usual_conversions('?', *true, expr
->cond_false
,
1139 lclass
, rclass
, ltype
, rtype
);
1140 *true = cast_to(*true, ctype
);
1141 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1145 if ((lclass
| rclass
) & TYPE_PTR
) {
1146 int is_null1
= is_null_pointer_constant(*true);
1147 int is_null2
= is_null_pointer_constant(expr
->cond_false
);
1149 if (is_null1
&& is_null2
) {
1150 *true = cast_to(*true, &ptr_ctype
);
1151 expr
->cond_false
= cast_to(expr
->cond_false
, &ptr_ctype
);
1155 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1158 *true = cast_to(*true, rtype
);
1162 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1164 bad_null(expr
->cond_false
);
1165 expr
->cond_false
= cast_to(expr
->cond_false
, ltype
);
1169 if (!(lclass
& rclass
& TYPE_PTR
)) {
1170 typediff
= "different types";
1173 /* OK, it's pointer on pointer */
1174 if (ltype
->ctype
.as
!= rtype
->ctype
.as
) {
1175 typediff
= "different address spaces";
1179 /* need to be lazier here */
1180 lbase
= examine_pointer_target(ltype
);
1181 rbase
= examine_pointer_target(rtype
);
1182 qual
= target_qualifiers(ltype
) | target_qualifiers(rtype
);
1184 if (lbase
== &void_ctype
) {
1185 /* XXX: pointers to function should warn here */
1190 if (rbase
== &void_ctype
) {
1191 /* XXX: pointers to function should warn here */
1195 /* XXX: that should be pointer to composite */
1197 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1204 /* void on void, struct on same struct, union on same union */
1205 if (ltype
== rtype
) {
1209 typediff
= "different base types";
1212 expression_error(expr
, "incompatible types in conditional expression (%s)", typediff
);
1216 expr
->ctype
= ctype
;
1220 if (qual
& ~ctype
->ctype
.modifiers
) {
1221 struct symbol
*sym
= alloc_symbol(ctype
->pos
, SYM_PTR
);
1223 sym
->ctype
.modifiers
|= qual
;
1226 *true = cast_to(*true, ctype
);
1227 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1231 /* FP assignments can not do modulo or bit operations */
1232 static int compatible_float_op(int op
)
1234 return op
== SPECIAL_ADD_ASSIGN
||
1235 op
== SPECIAL_SUB_ASSIGN
||
1236 op
== SPECIAL_MUL_ASSIGN
||
1237 op
== SPECIAL_DIV_ASSIGN
;
1240 static int evaluate_assign_op(struct expression
*expr
)
1242 struct symbol
*target
= expr
->left
->ctype
;
1243 struct symbol
*source
= expr
->right
->ctype
;
1244 struct symbol
*t
, *s
;
1245 int tclass
= classify_type(target
, &t
);
1246 int sclass
= classify_type(source
, &s
);
1249 if (tclass
& sclass
& TYPE_NUM
) {
1250 if (tclass
& TYPE_FLOAT
&& !compatible_float_op(op
)) {
1251 expression_error(expr
, "invalid assignment");
1254 if (tclass
& TYPE_RESTRICT
) {
1255 if (!restricted_binop(op
, t
)) {
1256 expression_error(expr
, "bad restricted assignment");
1259 /* allowed assignments unfoul */
1260 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1262 if (!restricted_value(expr
->right
, t
))
1264 } else if (!(sclass
& TYPE_RESTRICT
))
1266 /* source and target would better be identical restricted */
1269 warning(expr
->pos
, "invalid restricted assignment");
1270 expr
->right
= cast_to(expr
->right
, target
);
1273 if (tclass
& TYPE_PTR
&& is_int(sclass
)) {
1274 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1275 unrestrict(expr
->right
, sclass
, &s
);
1276 evaluate_ptr_add(expr
, target
, s
);
1279 expression_error(expr
, "invalid pointer assignment");
1283 expression_error(expr
, "invalid assignment");
1287 expr
->right
= cast_to(expr
->right
, target
);
1291 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1292 struct expression
**rp
, const char *where
)
1294 const char *typediff
;
1295 struct symbol
*source
= degenerate(*rp
);
1296 struct symbol
*t
, *s
;
1297 int tclass
= classify_type(target
, &t
);
1298 int sclass
= classify_type(source
, &s
);
1300 if (tclass
& sclass
& TYPE_NUM
) {
1301 if (tclass
& TYPE_RESTRICT
) {
1302 /* allowed assignments unfoul */
1303 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1305 if (!restricted_value(*rp
, target
))
1309 } else if (!(sclass
& TYPE_RESTRICT
))
1311 typediff
= "different base types";
1315 if (tclass
== TYPE_PTR
) {
1316 unsigned long mod1
, mod2
;
1317 struct symbol
*b1
, *b2
;
1318 // NULL pointer is always OK
1319 int is_null
= is_null_pointer_constant(*rp
);
1325 if (!(sclass
& TYPE_PTR
)) {
1326 typediff
= "different base types";
1329 b1
= examine_pointer_target(t
);
1330 b2
= examine_pointer_target(s
);
1331 mod1
= target_qualifiers(t
);
1332 mod2
= target_qualifiers(s
);
1333 if (b1
== &void_ctype
|| b2
== &void_ctype
) {
1335 * assignments to/from void * are OK, provided that
1336 * we do not remove qualifiers from pointed to [C]
1337 * or mix address spaces [sparse].
1339 if (t
->ctype
.as
!= s
->ctype
.as
) {
1340 typediff
= "different address spaces";
1344 typediff
= "different modifiers";
1349 /* It's OK if the target is more volatile or const than the source */
1350 typediff
= type_difference(&t
->ctype
, &s
->ctype
, 0, mod1
);
1356 if ((tclass
& TYPE_COMPOUND
) && s
== t
)
1359 if (tclass
& TYPE_NUM
) {
1360 /* XXX: need to turn into comparison with NULL */
1361 if (t
== &bool_ctype
&& (sclass
& TYPE_PTR
))
1363 typediff
= "different base types";
1366 typediff
= "invalid types";
1369 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1370 info(expr
->pos
, " expected %s", show_typename(target
));
1371 info(expr
->pos
, " got %s", show_typename(source
));
1372 *rp
= cast_to(*rp
, target
);
1375 *rp
= cast_to(*rp
, target
);
1379 static void mark_assigned(struct expression
*expr
)
1385 switch (expr
->type
) {
1390 if (sym
->type
!= SYM_NODE
)
1392 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1396 mark_assigned(expr
->left
);
1397 mark_assigned(expr
->right
);
1400 case EXPR_FORCE_CAST
:
1401 mark_assigned(expr
->cast_expression
);
1404 mark_assigned(expr
->base
);
1412 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1414 if (type
->ctype
.modifiers
& MOD_CONST
)
1415 expression_error(left
, "assignment to const expression");
1417 /* We know left is an lvalue, so it's a "preop-*" */
1418 mark_assigned(left
->unop
);
1421 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1423 struct expression
*left
= expr
->left
;
1424 struct expression
*where
= expr
;
1425 struct symbol
*ltype
;
1427 if (!lvalue_expression(left
)) {
1428 expression_error(expr
, "not an lvalue");
1432 ltype
= left
->ctype
;
1434 if (expr
->op
!= '=') {
1435 if (!evaluate_assign_op(expr
))
1438 if (!compatible_assignment_types(where
, ltype
, &expr
->right
, "assignment"))
1442 evaluate_assign_to(left
, ltype
);
1444 expr
->ctype
= ltype
;
1448 static void examine_fn_arguments(struct symbol
*fn
)
1452 FOR_EACH_PTR(fn
->arguments
, s
) {
1453 struct symbol
*arg
= evaluate_symbol(s
);
1454 /* Array/function arguments silently degenerate into pointers */
1460 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1461 if (arg
->type
== SYM_ARRAY
)
1462 ptr
->ctype
= arg
->ctype
;
1464 ptr
->ctype
.base_type
= arg
;
1465 ptr
->ctype
.as
|= s
->ctype
.as
;
1466 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
& MOD_PTRINHERIT
;
1468 s
->ctype
.base_type
= ptr
;
1470 s
->ctype
.modifiers
&= ~MOD_PTRINHERIT
;
1473 examine_symbol_type(s
);
1480 } END_FOR_EACH_PTR(s
);
1483 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1485 /* Take the modifiers of the pointer, and apply them to the member */
1486 mod
|= sym
->ctype
.modifiers
;
1487 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1488 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1490 newsym
->ctype
.as
= as
;
1491 newsym
->ctype
.modifiers
= mod
;
1497 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1499 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1500 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1502 node
->ctype
.base_type
= ptr
;
1503 ptr
->bit_size
= bits_in_pointer
;
1504 ptr
->ctype
.alignment
= pointer_alignment
;
1506 node
->bit_size
= bits_in_pointer
;
1507 node
->ctype
.alignment
= pointer_alignment
;
1510 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1511 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1512 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1514 if (sym
->type
== SYM_NODE
) {
1515 ptr
->ctype
.as
|= sym
->ctype
.as
;
1516 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1517 sym
= sym
->ctype
.base_type
;
1519 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1520 ptr
->ctype
.as
|= sym
->ctype
.as
;
1521 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1522 sym
= sym
->ctype
.base_type
;
1524 ptr
->ctype
.base_type
= sym
;
1529 /* Arrays degenerate into pointers on pointer arithmetic */
1530 static struct symbol
*degenerate(struct expression
*expr
)
1532 struct symbol
*ctype
, *base
;
1536 ctype
= expr
->ctype
;
1539 base
= examine_symbol_type(ctype
);
1540 if (ctype
->type
== SYM_NODE
)
1541 base
= ctype
->ctype
.base_type
;
1543 * Arrays degenerate into pointers to the entries, while
1544 * functions degenerate into pointers to themselves.
1545 * If array was part of non-lvalue compound, we create a copy
1546 * of that compound first and then act as if we were dealing with
1547 * the corresponding field in there.
1549 switch (base
->type
) {
1551 if (expr
->type
== EXPR_SLICE
) {
1552 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1553 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1555 a
->ctype
.base_type
= expr
->base
->ctype
;
1556 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1557 a
->array_size
= expr
->base
->ctype
->array_size
;
1559 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1561 e0
->ctype
= &lazy_ptr_ctype
;
1563 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1566 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1568 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1570 e2
->right
= expr
->base
;
1572 e2
->ctype
= expr
->base
->ctype
;
1574 if (expr
->r_bitpos
) {
1575 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1578 e3
->right
= alloc_const_expression(expr
->pos
,
1579 expr
->r_bitpos
>> 3);
1580 e3
->ctype
= &lazy_ptr_ctype
;
1585 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1588 e4
->ctype
= &lazy_ptr_ctype
;
1591 expr
->type
= EXPR_PREOP
;
1595 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1596 expression_error(expr
, "strange non-value function or array");
1599 *expr
= *expr
->unop
;
1600 ctype
= create_pointer(expr
, ctype
, 1);
1601 expr
->ctype
= ctype
;
1608 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1610 struct expression
*op
= expr
->unop
;
1611 struct symbol
*ctype
;
1613 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1614 expression_error(expr
, "not addressable");
1621 if (expr
->type
== EXPR_SYMBOL
) {
1622 struct symbol
*sym
= expr
->symbol
;
1623 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1627 * symbol expression evaluation is lazy about the type
1628 * of the sub-expression, so we may have to generate
1629 * the type here if so..
1631 if (expr
->ctype
== &lazy_ptr_ctype
) {
1632 ctype
= create_pointer(expr
, ctype
, 0);
1633 expr
->ctype
= ctype
;
1639 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1641 struct expression
*op
= expr
->unop
;
1642 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1644 /* Simplify: *&(expr) => (expr) */
1645 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1651 /* Dereferencing a node drops all the node information. */
1652 if (ctype
->type
== SYM_NODE
)
1653 ctype
= ctype
->ctype
.base_type
;
1655 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1656 target
= ctype
->ctype
.base_type
;
1658 switch (ctype
->type
) {
1660 expression_error(expr
, "cannot dereference this type");
1663 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1664 merge_type(node
, ctype
);
1668 if (!lvalue_expression(op
)) {
1669 expression_error(op
, "non-lvalue array??");
1673 /* Do the implied "addressof" on the array */
1677 * When an array is dereferenced, we need to pick
1678 * up the attributes of the original node too..
1680 merge_type(node
, op
->ctype
);
1681 merge_type(node
, ctype
);
1685 node
->bit_size
= target
->bit_size
;
1686 node
->array_size
= target
->array_size
;
1693 * Unary post-ops: x++ and x--
1695 static struct symbol
*evaluate_postop(struct expression
*expr
)
1697 struct expression
*op
= expr
->unop
;
1698 struct symbol
*ctype
= op
->ctype
;
1700 if (!lvalue_expression(expr
->unop
)) {
1701 expression_error(expr
, "need lvalue expression for ++/--");
1704 if (is_restricted_type(ctype
) && restricted_unop(expr
->op
, &ctype
)) {
1705 expression_error(expr
, "bad operation on restricted");
1707 } else if (is_fouled_type(ctype
) && restricted_unop(expr
->op
, &ctype
)) {
1708 expression_error(expr
, "bad operation on restricted");
1712 evaluate_assign_to(op
, ctype
);
1714 expr
->ctype
= ctype
;
1716 if (is_ptr_type(ctype
))
1717 expr
->op_value
= ptr_object_size(ctype
) >> 3;
1722 static struct symbol
*evaluate_sign(struct expression
*expr
)
1724 struct symbol
*ctype
= expr
->unop
->ctype
;
1725 int class = classify_type(ctype
, &ctype
);
1726 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1728 /* should be an arithmetic type */
1729 if (!(class & TYPE_NUM
))
1730 return bad_expr_type(expr
);
1731 if (!(class & (TYPE_FLOAT
|TYPE_RESTRICT
))) {
1732 struct symbol
*rtype
= integer_promotion(ctype
);
1733 expr
->unop
= cast_to(expr
->unop
, rtype
);
1735 } else if ((class & TYPE_FLOAT
) && expr
->op
!= '~') {
1736 /* no conversions needed */
1737 } else if ((class & TYPE_RESTRICT
) && !restricted_unop(expr
->op
, &ctype
)) {
1738 /* no conversions needed */
1740 return bad_expr_type(expr
);
1742 if (expr
->op
== '+')
1743 *expr
= *expr
->unop
;
1744 expr
->ctype
= ctype
;
1748 static struct symbol
*evaluate_preop(struct expression
*expr
)
1750 struct symbol
*ctype
= expr
->unop
->ctype
;
1754 *expr
= *expr
->unop
;
1760 return evaluate_sign(expr
);
1763 return evaluate_dereference(expr
);
1766 return evaluate_addressof(expr
);
1768 case SPECIAL_INCREMENT
:
1769 case SPECIAL_DECREMENT
:
1771 * From a type evaluation standpoint the preops are
1772 * the same as the postops
1774 return evaluate_postop(expr
);
1777 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1779 if (is_safe_type(ctype
))
1780 warning(expr
->pos
, "testing a 'safe expression'");
1781 if (is_float_type(ctype
)) {
1782 struct expression
*arg
= expr
->unop
;
1783 expr
->type
= EXPR_BINOP
;
1784 expr
->op
= SPECIAL_EQUAL
;
1786 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1787 expr
->right
->ctype
= ctype
;
1788 expr
->right
->fvalue
= 0;
1789 } else if (is_fouled_type(ctype
)) {
1790 warning(expr
->pos
, "restricted degrades to integer");
1792 ctype
= &bool_ctype
;
1798 expr
->ctype
= ctype
;
1802 static struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1804 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1805 struct ptr_list
*list
= head
;
1811 for (i
= 0; i
< list
->nr
; i
++) {
1812 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1814 if (sym
->ident
!= ident
)
1816 *offset
= sym
->offset
;
1819 struct symbol
*ctype
= sym
->ctype
.base_type
;
1823 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1825 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1828 *offset
+= sym
->offset
;
1832 } while ((list
= list
->next
) != head
);
1836 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1838 struct expression
*add
;
1841 * Create a new add-expression
1843 * NOTE! Even if we just add zero, we need a new node
1844 * for the member pointer, since it has a different
1845 * type than the original pointer. We could make that
1846 * be just a cast, but the fact is, a node is a node,
1847 * so we might as well just do the "add zero" here.
1849 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1852 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1853 add
->right
->ctype
= &int_ctype
;
1854 add
->right
->value
= offset
;
1857 * The ctype of the pointer will be lazily evaluated if
1858 * we ever take the address of this member dereference..
1860 add
->ctype
= &lazy_ptr_ctype
;
1864 /* structure/union dereference */
1865 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1868 struct symbol
*ctype
, *member
;
1869 struct expression
*deref
= expr
->deref
, *add
;
1870 struct ident
*ident
= expr
->member
;
1874 if (!evaluate_expression(deref
))
1877 expression_error(expr
, "bad member name");
1881 ctype
= deref
->ctype
;
1882 examine_symbol_type(ctype
);
1883 address_space
= ctype
->ctype
.as
;
1884 mod
= ctype
->ctype
.modifiers
;
1885 if (ctype
->type
== SYM_NODE
) {
1886 ctype
= ctype
->ctype
.base_type
;
1887 address_space
|= ctype
->ctype
.as
;
1888 mod
|= ctype
->ctype
.modifiers
;
1890 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1891 expression_error(expr
, "expected structure or union");
1895 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1897 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1898 const char *name
= "<unnamed>";
1901 name
= ctype
->ident
->name
;
1902 namelen
= ctype
->ident
->len
;
1904 if (ctype
->symbol_list
)
1905 expression_error(expr
, "no member '%s' in %s %.*s",
1906 show_ident(ident
), type
, namelen
, name
);
1908 expression_error(expr
, "using member '%s' in "
1909 "incomplete %s %.*s", show_ident(ident
),
1910 type
, namelen
, name
);
1915 * The member needs to take on the address space and modifiers of
1916 * the "parent" type.
1918 member
= convert_to_as_mod(member
, address_space
, mod
);
1919 ctype
= get_base_type(member
);
1921 if (!lvalue_expression(deref
)) {
1922 if (deref
->type
!= EXPR_SLICE
) {
1926 expr
->base
= deref
->base
;
1927 expr
->r_bitpos
= deref
->r_bitpos
;
1929 expr
->r_bitpos
+= offset
<< 3;
1930 expr
->type
= EXPR_SLICE
;
1931 expr
->r_nrbits
= member
->bit_size
;
1932 expr
->r_bitpos
+= member
->bit_offset
;
1933 expr
->ctype
= member
;
1937 deref
= deref
->unop
;
1938 expr
->deref
= deref
;
1940 add
= evaluate_offset(deref
, offset
);
1941 expr
->type
= EXPR_PREOP
;
1945 expr
->ctype
= member
;
1949 static int is_promoted(struct expression
*expr
)
1952 switch (expr
->type
) {
1955 case EXPR_CONDITIONAL
:
1979 static struct symbol
*evaluate_cast(struct expression
*);
1981 static struct symbol
*evaluate_type_information(struct expression
*expr
)
1983 struct symbol
*sym
= expr
->cast_type
;
1985 sym
= evaluate_expression(expr
->cast_expression
);
1989 * Expressions of restricted types will possibly get
1990 * promoted - check that here
1992 if (is_restricted_type(sym
)) {
1993 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
1995 } else if (is_fouled_type(sym
)) {
1999 examine_symbol_type(sym
);
2000 if (is_bitfield_type(sym
)) {
2001 expression_error(expr
, "trying to examine bitfield type");
2007 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
2009 struct symbol
*type
;
2012 type
= evaluate_type_information(expr
);
2016 size
= type
->bit_size
;
2017 if ((size
< 0) || (size
& 7))
2018 expression_error(expr
, "cannot size expression");
2019 expr
->type
= EXPR_VALUE
;
2020 expr
->value
= size
>> 3;
2022 expr
->ctype
= size_t_ctype
;
2023 return size_t_ctype
;
2026 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
2028 struct symbol
*type
;
2031 type
= evaluate_type_information(expr
);
2035 if (type
->type
== SYM_NODE
)
2036 type
= type
->ctype
.base_type
;
2039 switch (type
->type
) {
2043 type
= get_base_type(type
);
2047 expression_error(expr
, "expected pointer expression");
2050 size
= type
->bit_size
;
2053 expr
->type
= EXPR_VALUE
;
2054 expr
->value
= size
>> 3;
2056 expr
->ctype
= size_t_ctype
;
2057 return size_t_ctype
;
2060 static struct symbol
*evaluate_alignof(struct expression
*expr
)
2062 struct symbol
*type
;
2064 type
= evaluate_type_information(expr
);
2068 expr
->type
= EXPR_VALUE
;
2069 expr
->value
= type
->ctype
.alignment
;
2071 expr
->ctype
= size_t_ctype
;
2072 return size_t_ctype
;
2075 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
2077 struct expression
*expr
;
2078 struct symbol_list
*argument_types
= fn
->arguments
;
2079 struct symbol
*argtype
;
2082 PREPARE_PTR_LIST(argument_types
, argtype
);
2083 FOR_EACH_PTR (head
, expr
) {
2084 struct expression
**p
= THIS_ADDRESS(expr
);
2085 struct symbol
*ctype
, *target
;
2086 ctype
= evaluate_expression(expr
);
2093 struct symbol
*type
;
2094 int class = classify_type(ctype
, &type
);
2095 if (is_int(class)) {
2096 *p
= cast_to(expr
, integer_promotion(type
));
2097 } else if (class & TYPE_FLOAT
) {
2098 unsigned long mod
= type
->ctype
.modifiers
;
2099 if (!(mod
& (MOD_LONG
|MOD_LONGLONG
)))
2100 *p
= cast_to(expr
, &double_ctype
);
2101 } else if (class & TYPE_PTR
) {
2102 if (expr
->ctype
== &null_ctype
)
2103 *p
= cast_to(expr
, &ptr_ctype
);
2108 static char where
[30];
2109 examine_symbol_type(target
);
2110 sprintf(where
, "argument %d", i
);
2111 compatible_assignment_types(expr
, target
, p
, where
);
2115 NEXT_PTR_LIST(argtype
);
2116 } END_FOR_EACH_PTR(expr
);
2117 FINISH_PTR_LIST(argtype
);
2121 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
2125 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
2126 if (sym
->ident
== ident
)
2128 } END_FOR_EACH_PTR(sym
);
2132 static void convert_index(struct expression
*e
)
2134 struct expression
*child
= e
->idx_expression
;
2135 unsigned from
= e
->idx_from
;
2136 unsigned to
= e
->idx_to
+ 1;
2138 e
->init_offset
= from
* (e
->ctype
->bit_size
>>3);
2139 e
->init_nr
= to
- from
;
2140 e
->init_expr
= child
;
2143 static void convert_ident(struct expression
*e
)
2145 struct expression
*child
= e
->ident_expression
;
2146 struct symbol
*sym
= e
->field
;
2148 e
->init_offset
= sym
->offset
;
2150 e
->init_expr
= child
;
2153 static void convert_designators(struct expression
*e
)
2156 if (e
->type
== EXPR_INDEX
)
2158 else if (e
->type
== EXPR_IDENTIFIER
)
2166 static void excess(struct expression
*e
, const char *s
)
2168 warning(e
->pos
, "excessive elements in %s initializer", s
);
2172 * implicit designator for the first element
2174 static struct expression
*first_subobject(struct symbol
*ctype
, int class,
2175 struct expression
**v
)
2177 struct expression
*e
= *v
, *new;
2179 if (ctype
->type
== SYM_NODE
)
2180 ctype
= ctype
->ctype
.base_type
;
2182 if (class & TYPE_PTR
) { /* array */
2183 if (!ctype
->bit_size
)
2185 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2186 new->idx_expression
= e
;
2187 new->ctype
= ctype
->ctype
.base_type
;
2189 struct symbol
*field
, *p
;
2190 PREPARE_PTR_LIST(ctype
->symbol_list
, p
);
2191 while (p
&& !p
->ident
&& is_bitfield_type(p
))
2197 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2198 new->ident_expression
= e
;
2199 new->field
= new->ctype
= field
;
2206 * sanity-check explicit designators; return the innermost one or NULL
2207 * in case of error. Assign types.
2209 static struct expression
*check_designators(struct expression
*e
,
2210 struct symbol
*ctype
)
2212 struct expression
*last
= NULL
;
2215 if (ctype
->type
== SYM_NODE
)
2216 ctype
= ctype
->ctype
.base_type
;
2217 if (e
->type
== EXPR_INDEX
) {
2218 struct symbol
*type
;
2219 if (ctype
->type
!= SYM_ARRAY
) {
2220 err
= "array index in non-array";
2223 type
= ctype
->ctype
.base_type
;
2224 if (ctype
->bit_size
>= 0 && type
->bit_size
>= 0) {
2225 unsigned offset
= e
->idx_to
* type
->bit_size
;
2226 if (offset
>= ctype
->bit_size
) {
2227 err
= "index out of bounds in";
2231 e
->ctype
= ctype
= type
;
2234 if (!e
->idx_expression
) {
2238 e
= e
->idx_expression
;
2239 } else if (e
->type
== EXPR_IDENTIFIER
) {
2240 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2241 err
= "field name not in struct or union";
2244 ctype
= find_struct_ident(ctype
, e
->expr_ident
);
2246 err
= "unknown field name in";
2249 e
->field
= e
->ctype
= ctype
;
2251 if (!e
->ident_expression
) {
2255 e
= e
->ident_expression
;
2256 } else if (e
->type
== EXPR_POS
) {
2257 err
= "internal front-end error: EXPR_POS in";
2262 expression_error(e
, "%s initializer", err
);
2267 * choose the next subobject to initialize.
2269 * Get designators for next element, switch old ones to EXPR_POS.
2270 * Return the resulting expression or NULL if we'd run out of subobjects.
2271 * The innermost designator is returned in *v. Designators in old
2272 * are assumed to be already sanity-checked.
2274 static struct expression
*next_designators(struct expression
*old
,
2275 struct symbol
*ctype
,
2276 struct expression
*e
, struct expression
**v
)
2278 struct expression
*new = NULL
;
2282 if (old
->type
== EXPR_INDEX
) {
2283 struct expression
*copy
;
2286 copy
= next_designators(old
->idx_expression
,
2289 n
= old
->idx_to
+ 1;
2290 if (n
* old
->ctype
->bit_size
== ctype
->bit_size
) {
2295 *v
= new = alloc_expression(e
->pos
, EXPR_INDEX
);
2298 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2301 new->idx_from
= new->idx_to
= n
;
2302 new->idx_expression
= copy
;
2303 new->ctype
= old
->ctype
;
2305 } else if (old
->type
== EXPR_IDENTIFIER
) {
2306 struct expression
*copy
;
2307 struct symbol
*field
;
2309 copy
= next_designators(old
->ident_expression
,
2312 field
= old
->field
->next_subobject
;
2318 *v
= new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2321 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2325 new->expr_ident
= field
->ident
;
2326 new->ident_expression
= copy
;
2333 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2334 int class, struct symbol
*ctype
);
2337 * deal with traversing subobjects [6.7.8(17,18,20)]
2339 static void handle_list_initializer(struct expression
*expr
,
2340 int class, struct symbol
*ctype
)
2342 struct expression
*e
, *last
= NULL
, *top
= NULL
, *next
;
2345 FOR_EACH_PTR(expr
->expr_list
, e
) {
2346 struct expression
**v
;
2347 struct symbol
*type
;
2350 if (e
->type
!= EXPR_INDEX
&& e
->type
!= EXPR_IDENTIFIER
) {
2353 last
= first_subobject(ctype
, class, &top
);
2355 last
= next_designators(last
, ctype
, e
, &top
);
2358 excess(e
, class & TYPE_PTR
? "array" :
2360 DELETE_CURRENT_PTR(e
);
2364 warning(e
->pos
, "advancing past deep designator");
2367 REPLACE_CURRENT_PTR(e
, last
);
2369 next
= check_designators(e
, ctype
);
2371 DELETE_CURRENT_PTR(e
);
2375 /* deeper than one designator? */
2377 convert_designators(last
);
2382 lclass
= classify_type(top
->ctype
, &type
);
2383 if (top
->type
== EXPR_INDEX
)
2384 v
= &top
->idx_expression
;
2386 v
= &top
->ident_expression
;
2388 if (handle_simple_initializer(v
, 1, lclass
, top
->ctype
))
2391 if (!(lclass
& TYPE_COMPOUND
)) {
2392 warning(e
->pos
, "bogus scalar initializer");
2393 DELETE_CURRENT_PTR(e
);
2397 next
= first_subobject(type
, lclass
, v
);
2399 warning(e
->pos
, "missing braces around initializer");
2404 DELETE_CURRENT_PTR(e
);
2405 excess(e
, lclass
& TYPE_PTR
? "array" : "struct or union");
2407 } END_FOR_EACH_PTR(e
);
2409 convert_designators(last
);
2410 expr
->ctype
= ctype
;
2413 static int is_string_literal(struct expression
**v
)
2415 struct expression
*e
= *v
;
2416 while (e
&& e
->type
== EXPR_PREOP
&& e
->op
== '(')
2418 if (!e
|| e
->type
!= EXPR_STRING
)
2420 if (e
!= *v
&& Wparen_string
)
2422 "array initialized from parenthesized string constant");
2428 * We want a normal expression, possibly in one layer of braces. Warn
2429 * if the latter happens inside a list (it's legal, but likely to be
2430 * an effect of screwup). In case of anything not legal, we are definitely
2431 * having an effect of screwup, so just fail and let the caller warn.
2433 static struct expression
*handle_scalar(struct expression
*e
, int nested
)
2435 struct expression
*v
= NULL
, *p
;
2439 if (e
->type
!= EXPR_INITIALIZER
)
2442 FOR_EACH_PTR(e
->expr_list
, p
) {
2446 } END_FOR_EACH_PTR(p
);
2450 case EXPR_INITIALIZER
:
2452 case EXPR_IDENTIFIER
:
2458 warning(e
->pos
, "braces around scalar initializer");
2463 * deal with the cases that don't care about subobjects:
2464 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2465 * character array <- string literal, possibly in braces [6.7.8(14)]
2466 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2467 * compound type <- initializer list in braces [6.7.8(16)]
2468 * The last one punts to handle_list_initializer() which, in turn will call
2469 * us for individual elements of the list.
2471 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2472 * the lack of support of wide char stuff in general.
2474 * One note: we need to take care not to evaluate a string literal until
2475 * we know that we *will* handle it right here. Otherwise we would screw
2476 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2477 * { "string", ...} - we need to preserve that string literal recognizable
2478 * until we dig into the inner struct.
2480 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2481 int class, struct symbol
*ctype
)
2483 int is_string
= is_string_type(ctype
);
2484 struct expression
*e
= *ep
, *p
;
2485 struct symbol
*type
;
2491 if (!(class & TYPE_COMPOUND
)) {
2492 e
= handle_scalar(e
, nested
);
2496 if (!evaluate_expression(e
))
2498 compatible_assignment_types(e
, ctype
, ep
, "initializer");
2503 * sublist; either a string, or we dig in; the latter will deal with
2504 * pathologies, so we don't need anything fancy here.
2506 if (e
->type
== EXPR_INITIALIZER
) {
2508 struct expression
*v
= NULL
;
2511 FOR_EACH_PTR(e
->expr_list
, p
) {
2515 } END_FOR_EACH_PTR(p
);
2516 if (count
== 1 && is_string_literal(&v
)) {
2521 handle_list_initializer(e
, class, ctype
);
2526 if (is_string_literal(&e
)) {
2527 /* either we are doing array of char, or we'll have to dig in */
2534 /* struct or union can be initialized by compatible */
2535 if (class != TYPE_COMPOUND
)
2537 type
= evaluate_expression(e
);
2540 if (ctype
->type
== SYM_NODE
)
2541 ctype
= ctype
->ctype
.base_type
;
2542 if (type
->type
== SYM_NODE
)
2543 type
= type
->ctype
.base_type
;
2549 p
= alloc_expression(e
->pos
, EXPR_STRING
);
2551 type
= evaluate_expression(p
);
2552 if (ctype
->bit_size
!= -1 &&
2553 ctype
->bit_size
+ bits_in_char
< type
->bit_size
) {
2555 "too long initializer-string for array of char");
2561 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2563 struct symbol
*type
;
2564 int class = classify_type(ctype
, &type
);
2565 if (!handle_simple_initializer(ep
, 0, class, ctype
))
2566 expression_error(*ep
, "invalid initializer");
2569 static struct symbol
*evaluate_cast(struct expression
*expr
)
2571 struct expression
*target
= expr
->cast_expression
;
2572 struct symbol
*ctype
;
2573 struct symbol
*t1
, *t2
;
2575 int as1
= 0, as2
= 0;
2581 * Special case: a cast can be followed by an
2582 * initializer, in which case we need to pass
2583 * the type value down to that initializer rather
2584 * than trying to evaluate it as an expression
2586 * A more complex case is when the initializer is
2587 * dereferenced as part of a post-fix expression.
2588 * We need to produce an expression that can be dereferenced.
2590 if (target
->type
== EXPR_INITIALIZER
) {
2591 struct symbol
*sym
= expr
->cast_type
;
2592 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2594 sym
->initializer
= target
;
2595 evaluate_symbol(sym
);
2597 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2600 expr
->type
= EXPR_PREOP
;
2608 ctype
= examine_symbol_type(expr
->cast_type
);
2609 expr
->ctype
= ctype
;
2610 expr
->cast_type
= ctype
;
2612 evaluate_expression(target
);
2615 class1
= classify_type(ctype
, &t1
);
2617 /* cast to non-integer type -> not an integer constant expression */
2618 if (!is_int(class1
))
2620 /* if argument turns out to be not an integer constant expression *and*
2621 it was not a floating literal to start with -> too bad */
2622 else if (expr
->flags
== Int_const_expr
&&
2623 !(target
->flags
& Int_const_expr
))
2626 * You can always throw a value away by casting to
2627 * "void" - that's an implicit "force". Note that
2628 * the same is _not_ true of "void *".
2630 if (t1
== &void_ctype
)
2633 if (class1
& TYPE_COMPOUND
)
2634 warning(expr
->pos
, "cast to non-scalar");
2638 expression_error(expr
, "cast from unknown type");
2641 class2
= classify_type(t2
, &t2
);
2643 if (class2
& TYPE_COMPOUND
)
2644 warning(expr
->pos
, "cast from non-scalar");
2646 if (expr
->type
== EXPR_FORCE_CAST
)
2649 /* allowed cast unfouls */
2650 if (class2
& TYPE_FOULED
)
2654 if (class1
& TYPE_RESTRICT
)
2655 warning(expr
->pos
, "cast to restricted type");
2656 if (class2
& TYPE_RESTRICT
)
2657 warning(expr
->pos
, "cast from restricted type");
2660 if (t1
== &ulong_ctype
)
2662 else if (class1
== TYPE_PTR
) {
2663 examine_pointer_target(t1
);
2667 if (t2
== &ulong_ctype
)
2669 else if (class2
== TYPE_PTR
) {
2670 examine_pointer_target(t2
);
2674 if (!as1
&& as2
> 0)
2675 warning(expr
->pos
, "cast removes address space of expression");
2676 if (as1
> 0 && as2
> 0 && as1
!= as2
)
2677 warning(expr
->pos
, "cast between address spaces (<asn:%d>-><asn:%d>)", as2
, as1
);
2678 if (as1
> 0 && !as2
&&
2679 !is_null_pointer_constant(target
) && Wcast_to_address_space
)
2681 "cast adds address space to expression (<asn:%d>)", as1
);
2683 if (!(t1
->ctype
.modifiers
& MOD_PTRINHERIT
) && class1
== TYPE_PTR
&&
2684 !as1
&& (target
->flags
& Int_const_expr
)) {
2685 if (t1
->ctype
.base_type
== &void_ctype
) {
2686 if (is_zero_constant(target
)) {
2688 expr
->type
= EXPR_VALUE
;
2689 expr
->ctype
= &null_ctype
;
2700 * Evaluate a call expression with a symbol. This
2701 * should expand inline functions, and evaluate
2704 static int evaluate_symbol_call(struct expression
*expr
)
2706 struct expression
*fn
= expr
->fn
;
2707 struct symbol
*ctype
= fn
->ctype
;
2709 if (fn
->type
!= EXPR_PREOP
)
2712 if (ctype
->op
&& ctype
->op
->evaluate
)
2713 return ctype
->op
->evaluate(expr
);
2715 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2717 struct symbol
*curr
= current_fn
;
2718 current_fn
= ctype
->ctype
.base_type
;
2720 ret
= inline_function(expr
, ctype
);
2722 /* restore the old function */
2730 static struct symbol
*evaluate_call(struct expression
*expr
)
2733 struct symbol
*ctype
, *sym
;
2734 struct expression
*fn
= expr
->fn
;
2735 struct expression_list
*arglist
= expr
->args
;
2737 if (!evaluate_expression(fn
))
2739 sym
= ctype
= fn
->ctype
;
2740 if (ctype
->type
== SYM_NODE
)
2741 ctype
= ctype
->ctype
.base_type
;
2742 if (ctype
->type
== SYM_PTR
)
2743 ctype
= get_base_type(ctype
);
2745 if (ctype
->type
!= SYM_FN
) {
2746 struct expression
*arg
;
2747 expression_error(expr
, "not a function %s",
2748 show_ident(sym
->ident
));
2749 /* do typechecking in arguments */
2750 FOR_EACH_PTR (arglist
, arg
) {
2751 evaluate_expression(arg
);
2752 } END_FOR_EACH_PTR(arg
);
2756 examine_fn_arguments(ctype
);
2757 if (sym
->type
== SYM_NODE
&& fn
->type
== EXPR_PREOP
&&
2758 sym
->op
&& sym
->op
->args
) {
2759 if (!sym
->op
->args(expr
))
2762 if (!evaluate_arguments(sym
, ctype
, arglist
))
2764 args
= expression_list_size(expr
->args
);
2765 fnargs
= symbol_list_size(ctype
->arguments
);
2767 expression_error(expr
,
2768 "not enough arguments for function %s",
2769 show_ident(sym
->ident
));
2770 if (args
> fnargs
&& !ctype
->variadic
)
2771 expression_error(expr
,
2772 "too many arguments for function %s",
2773 show_ident(sym
->ident
));
2775 if (sym
->type
== SYM_NODE
) {
2776 if (evaluate_symbol_call(expr
))
2779 expr
->ctype
= ctype
->ctype
.base_type
;
2783 static struct symbol
*evaluate_offsetof(struct expression
*expr
)
2785 struct expression
*e
= expr
->down
;
2786 struct symbol
*ctype
= expr
->in
;
2789 if (expr
->op
== '.') {
2790 struct symbol
*field
;
2793 expression_error(expr
, "expected structure or union");
2796 examine_symbol_type(ctype
);
2797 class = classify_type(ctype
, &ctype
);
2798 if (class != TYPE_COMPOUND
) {
2799 expression_error(expr
, "expected structure or union");
2803 field
= find_identifier(expr
->ident
, ctype
->symbol_list
, &offset
);
2805 expression_error(expr
, "unknown member");
2809 expr
->type
= EXPR_VALUE
;
2810 expr
->flags
= Int_const_expr
;
2811 expr
->value
= offset
;
2813 expr
->ctype
= size_t_ctype
;
2816 expression_error(expr
, "expected structure or union");
2819 examine_symbol_type(ctype
);
2820 class = classify_type(ctype
, &ctype
);
2821 if (class != (TYPE_COMPOUND
| TYPE_PTR
)) {
2822 expression_error(expr
, "expected array");
2825 ctype
= ctype
->ctype
.base_type
;
2827 expr
->type
= EXPR_VALUE
;
2828 expr
->flags
= Int_const_expr
;
2831 expr
->ctype
= size_t_ctype
;
2833 struct expression
*idx
= expr
->index
, *m
;
2834 struct symbol
*i_type
= evaluate_expression(idx
);
2835 int i_class
= classify_type(i_type
, &i_type
);
2836 if (!is_int(i_class
)) {
2837 expression_error(expr
, "non-integer index");
2840 unrestrict(idx
, i_class
, &i_type
);
2841 idx
= cast_to(idx
, size_t_ctype
);
2842 m
= alloc_const_expression(expr
->pos
,
2843 ctype
->bit_size
>> 3);
2844 m
->ctype
= size_t_ctype
;
2845 m
->flags
= Int_const_expr
;
2846 expr
->type
= EXPR_BINOP
;
2850 expr
->ctype
= size_t_ctype
;
2851 expr
->flags
= m
->flags
& idx
->flags
& Int_const_expr
;
2855 struct expression
*copy
= __alloc_expression(0);
2857 if (e
->type
== EXPR_OFFSETOF
)
2859 if (!evaluate_expression(e
))
2861 expr
->type
= EXPR_BINOP
;
2862 expr
->flags
= e
->flags
& copy
->flags
& Int_const_expr
;
2864 expr
->ctype
= size_t_ctype
;
2868 return size_t_ctype
;
2871 struct symbol
*evaluate_expression(struct expression
*expr
)
2878 switch (expr
->type
) {
2881 expression_error(expr
, "value expression without a type");
2884 return evaluate_string(expr
);
2886 return evaluate_symbol_expression(expr
);
2888 if (!evaluate_expression(expr
->left
))
2890 if (!evaluate_expression(expr
->right
))
2892 return evaluate_binop(expr
);
2894 return evaluate_logical(expr
);
2896 evaluate_expression(expr
->left
);
2897 if (!evaluate_expression(expr
->right
))
2899 return evaluate_comma(expr
);
2901 if (!evaluate_expression(expr
->left
))
2903 if (!evaluate_expression(expr
->right
))
2905 return evaluate_compare(expr
);
2906 case EXPR_ASSIGNMENT
:
2907 if (!evaluate_expression(expr
->left
))
2909 if (!evaluate_expression(expr
->right
))
2911 return evaluate_assignment(expr
);
2913 if (!evaluate_expression(expr
->unop
))
2915 return evaluate_preop(expr
);
2917 if (!evaluate_expression(expr
->unop
))
2919 return evaluate_postop(expr
);
2921 case EXPR_FORCE_CAST
:
2922 case EXPR_IMPLIED_CAST
:
2923 return evaluate_cast(expr
);
2925 return evaluate_sizeof(expr
);
2926 case EXPR_PTRSIZEOF
:
2927 return evaluate_ptrsizeof(expr
);
2929 return evaluate_alignof(expr
);
2931 return evaluate_member_dereference(expr
);
2933 return evaluate_call(expr
);
2935 case EXPR_CONDITIONAL
:
2936 return evaluate_conditional_expression(expr
);
2937 case EXPR_STATEMENT
:
2938 expr
->ctype
= evaluate_statement(expr
->statement
);
2942 expr
->ctype
= &ptr_ctype
;
2946 /* Evaluate the type of the symbol .. */
2947 evaluate_symbol(expr
->symbol
);
2948 /* .. but the type of the _expression_ is a "type" */
2949 expr
->ctype
= &type_ctype
;
2953 return evaluate_offsetof(expr
);
2955 /* These can not exist as stand-alone expressions */
2956 case EXPR_INITIALIZER
:
2957 case EXPR_IDENTIFIER
:
2960 expression_error(expr
, "internal front-end error: initializer in expression");
2963 expression_error(expr
, "internal front-end error: SLICE re-evaluated");
2969 static void check_duplicates(struct symbol
*sym
)
2972 struct symbol
*next
= sym
;
2974 while ((next
= next
->same_symbol
) != NULL
) {
2975 const char *typediff
;
2976 evaluate_symbol(next
);
2978 typediff
= type_difference(&sym
->ctype
, &next
->ctype
, 0, 0);
2980 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2981 show_ident(sym
->ident
),
2982 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
2987 unsigned long mod
= sym
->ctype
.modifiers
;
2988 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
2990 if (!(mod
& MOD_TOPLEVEL
))
2994 if (sym
->ident
== &main_ident
)
2996 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
3000 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
3002 struct symbol
*base_type
;
3010 sym
= examine_symbol_type(sym
);
3011 base_type
= get_base_type(sym
);
3015 /* Evaluate the initializers */
3016 if (sym
->initializer
)
3017 evaluate_initializer(sym
, &sym
->initializer
);
3019 /* And finally, evaluate the body of the symbol too */
3020 if (base_type
->type
== SYM_FN
) {
3021 struct symbol
*curr
= current_fn
;
3023 current_fn
= base_type
;
3025 examine_fn_arguments(base_type
);
3026 if (!base_type
->stmt
&& base_type
->inline_stmt
)
3028 if (base_type
->stmt
)
3029 evaluate_statement(base_type
->stmt
);
3037 void evaluate_symbol_list(struct symbol_list
*list
)
3041 FOR_EACH_PTR(list
, sym
) {
3042 evaluate_symbol(sym
);
3043 check_duplicates(sym
);
3044 } END_FOR_EACH_PTR(sym
);
3047 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
3049 struct expression
*expr
= stmt
->expression
;
3050 struct symbol
*fntype
;
3052 evaluate_expression(expr
);
3053 fntype
= current_fn
->ctype
.base_type
;
3054 if (!fntype
|| fntype
== &void_ctype
) {
3055 if (expr
&& expr
->ctype
!= &void_ctype
)
3056 expression_error(expr
, "return expression in %s function", fntype
?"void":"typeless");
3057 if (expr
&& Wreturn_void
)
3058 warning(stmt
->pos
, "returning void-valued expression");
3063 sparse_error(stmt
->pos
, "return with no return value");
3068 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, "return expression");
3072 static void evaluate_if_statement(struct statement
*stmt
)
3074 if (!stmt
->if_conditional
)
3077 evaluate_conditional(stmt
->if_conditional
, 0);
3078 evaluate_statement(stmt
->if_true
);
3079 evaluate_statement(stmt
->if_false
);
3082 static void evaluate_iterator(struct statement
*stmt
)
3084 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
3085 evaluate_conditional(stmt
->iterator_post_condition
,1);
3086 evaluate_statement(stmt
->iterator_pre_statement
);
3087 evaluate_statement(stmt
->iterator_statement
);
3088 evaluate_statement(stmt
->iterator_post_statement
);
3091 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
3093 switch (*constraint
) {
3094 case '=': /* Assignment */
3095 case '+': /* Update */
3098 expression_error(expr
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
3102 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
3104 switch (*constraint
) {
3105 case '=': /* Assignment */
3106 case '+': /* Update */
3107 expression_error(expr
, "input constraint with assignment (\"%s\")", constraint
);
3111 static void evaluate_asm_statement(struct statement
*stmt
)
3113 struct expression
*expr
;
3116 expr
= stmt
->asm_string
;
3117 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3118 sparse_error(stmt
->pos
, "need constant string for inline asm");
3123 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
3124 struct ident
*ident
;
3127 case 0: /* Identifier */
3129 ident
= (struct ident
*)expr
;
3132 case 1: /* Constraint */
3134 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3135 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
3136 *THIS_ADDRESS(expr
) = NULL
;
3139 verify_output_constraint(expr
, expr
->string
->data
);
3142 case 2: /* Expression */
3144 if (!evaluate_expression(expr
))
3146 if (!lvalue_expression(expr
))
3147 warning(expr
->pos
, "asm output is not an lvalue");
3148 evaluate_assign_to(expr
, expr
->ctype
);
3151 } END_FOR_EACH_PTR(expr
);
3154 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
3155 struct ident
*ident
;
3158 case 0: /* Identifier */
3160 ident
= (struct ident
*)expr
;
3163 case 1: /* Constraint */
3165 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3166 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
3167 *THIS_ADDRESS(expr
) = NULL
;
3170 verify_input_constraint(expr
, expr
->string
->data
);
3173 case 2: /* Expression */
3175 if (!evaluate_expression(expr
))
3179 } END_FOR_EACH_PTR(expr
);
3181 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
3183 sparse_error(stmt
->pos
, "bad asm output");
3186 if (expr
->type
== EXPR_STRING
)
3188 expression_error(expr
, "asm clobber is not a string");
3189 } END_FOR_EACH_PTR(expr
);
3192 static void evaluate_case_statement(struct statement
*stmt
)
3194 evaluate_expression(stmt
->case_expression
);
3195 evaluate_expression(stmt
->case_to
);
3196 evaluate_statement(stmt
->case_statement
);
3199 static void check_case_type(struct expression
*switch_expr
,
3200 struct expression
*case_expr
,
3201 struct expression
**enumcase
)
3203 struct symbol
*switch_type
, *case_type
;
3209 switch_type
= switch_expr
->ctype
;
3210 case_type
= evaluate_expression(case_expr
);
3212 if (!switch_type
|| !case_type
)
3216 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
3217 else if (is_enum_type(case_type
))
3218 *enumcase
= case_expr
;
3221 sclass
= classify_type(switch_type
, &switch_type
);
3222 cclass
= classify_type(case_type
, &case_type
);
3224 /* both should be arithmetic */
3225 if (!(sclass
& cclass
& TYPE_NUM
))
3228 /* neither should be floating */
3229 if ((sclass
| cclass
) & TYPE_FLOAT
)
3232 /* if neither is restricted, we are OK */
3233 if (!((sclass
| cclass
) & TYPE_RESTRICT
))
3236 if (!restricted_binop_type(SPECIAL_EQUAL
, case_expr
, switch_expr
,
3237 cclass
, sclass
, case_type
, switch_type
))
3238 warning(case_expr
->pos
, "restricted degrades to integer");
3243 expression_error(case_expr
, "incompatible types for 'case' statement");
3246 static void evaluate_switch_statement(struct statement
*stmt
)
3249 struct expression
*enumcase
= NULL
;
3250 struct expression
**enumcase_holder
= &enumcase
;
3251 struct expression
*sel
= stmt
->switch_expression
;
3253 evaluate_expression(sel
);
3254 evaluate_statement(stmt
->switch_statement
);
3257 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
3258 enumcase_holder
= NULL
; /* Only check cases against switch */
3260 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
3261 struct statement
*case_stmt
= sym
->stmt
;
3262 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
3263 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
3264 } END_FOR_EACH_PTR(sym
);
3267 struct symbol
*evaluate_statement(struct statement
*stmt
)
3272 switch (stmt
->type
) {
3273 case STMT_DECLARATION
: {
3275 FOR_EACH_PTR(stmt
->declaration
, s
) {
3277 } END_FOR_EACH_PTR(s
);
3282 return evaluate_return_expression(stmt
);
3284 case STMT_EXPRESSION
:
3285 if (!evaluate_expression(stmt
->expression
))
3287 if (stmt
->expression
->ctype
== &null_ctype
)
3288 stmt
->expression
= cast_to(stmt
->expression
, &ptr_ctype
);
3289 return degenerate(stmt
->expression
);
3291 case STMT_COMPOUND
: {
3292 struct statement
*s
;
3293 struct symbol
*type
= NULL
;
3295 /* Evaluate the return symbol in the compound statement */
3296 evaluate_symbol(stmt
->ret
);
3299 * Then, evaluate each statement, making the type of the
3300 * compound statement be the type of the last statement
3302 type
= evaluate_statement(stmt
->args
);
3303 FOR_EACH_PTR(stmt
->stmts
, s
) {
3304 type
= evaluate_statement(s
);
3305 } END_FOR_EACH_PTR(s
);
3311 evaluate_if_statement(stmt
);
3314 evaluate_iterator(stmt
);
3317 evaluate_switch_statement(stmt
);
3320 evaluate_case_statement(stmt
);
3323 return evaluate_statement(stmt
->label_statement
);
3325 evaluate_expression(stmt
->goto_expression
);
3330 evaluate_asm_statement(stmt
);
3333 evaluate_expression(stmt
->expression
);
3336 evaluate_expression(stmt
->range_expression
);
3337 evaluate_expression(stmt
->range_low
);
3338 evaluate_expression(stmt
->range_high
);