4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
9 * Evaluate constant expressions.
27 #include "expression.h"
29 struct symbol
*current_fn
;
31 static struct symbol
*degenerate(struct expression
*expr
);
32 static struct symbol
*evaluate_symbol(struct symbol
*sym
);
34 static struct symbol
*evaluate_symbol_expression(struct expression
*expr
)
36 struct expression
*addr
;
37 struct symbol
*sym
= expr
->symbol
;
38 struct symbol
*base_type
;
41 expression_error(expr
, "undefined identifier '%s'", show_ident(expr
->symbol_name
));
45 examine_symbol_type(sym
);
47 base_type
= get_base_type(sym
);
49 expression_error(expr
, "identifier '%s' has no type", show_ident(expr
->symbol_name
));
53 addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
55 addr
->symbol_name
= expr
->symbol_name
;
56 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
57 expr
->type
= EXPR_PREOP
;
61 /* The type of a symbol is the symbol itself! */
66 static struct symbol
*evaluate_string(struct expression
*expr
)
68 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
69 struct symbol
*array
= alloc_symbol(expr
->pos
, SYM_ARRAY
);
70 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
71 struct expression
*initstr
= alloc_expression(expr
->pos
, EXPR_STRING
);
72 unsigned int length
= expr
->string
->length
;
74 sym
->array_size
= alloc_const_expression(expr
->pos
, length
);
75 sym
->bit_size
= bits_in_char
* length
;
76 sym
->ctype
.alignment
= 1;
78 sym
->ctype
.modifiers
= MOD_STATIC
;
79 sym
->ctype
.base_type
= array
;
80 sym
->initializer
= initstr
;
83 initstr
->string
= expr
->string
;
85 array
->array_size
= sym
->array_size
;
86 array
->bit_size
= bits_in_char
* length
;
87 array
->ctype
.alignment
= 1;
88 array
->ctype
.modifiers
= MOD_STATIC
;
89 array
->ctype
.base_type
= &char_ctype
;
92 addr
->ctype
= &lazy_ptr_ctype
;
94 expr
->type
= EXPR_PREOP
;
101 /* type has come from classify_type and is an integer type */
102 static inline struct symbol
*integer_promotion(struct symbol
*type
)
104 struct symbol
*orig_type
= type
;
105 unsigned long mod
= type
->ctype
.modifiers
;
106 int width
= type
->bit_size
;
109 * Bitfields always promote to the base type,
110 * even if the bitfield might be bigger than
113 if (type
->type
== SYM_BITFIELD
) {
114 type
= type
->ctype
.base_type
;
117 mod
= type
->ctype
.modifiers
;
118 if (width
< bits_in_int
)
121 /* If char/short has as many bits as int, it still gets "promoted" */
122 if (mod
& (MOD_CHAR
| MOD_SHORT
)) {
123 if (mod
& MOD_UNSIGNED
)
131 * integer part of usual arithmetic conversions:
132 * integer promotions are applied
133 * if left and right are identical, we are done
134 * if signedness is the same, convert one with lower rank
135 * unless unsigned argument has rank lower than signed one, convert the
137 * if signed argument is bigger than unsigned one, convert the unsigned.
138 * otherwise, convert signed.
140 * Leaving aside the integer promotions, that is equivalent to
141 * if identical, don't convert
142 * if left is bigger than right, convert right
143 * if right is bigger than left, convert right
144 * otherwise, if signedness is the same, convert one with lower rank
145 * otherwise convert the signed one.
147 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
149 unsigned long lmod
, rmod
;
151 left
= integer_promotion(left
);
152 right
= integer_promotion(right
);
157 if (left
->bit_size
> right
->bit_size
)
160 if (right
->bit_size
> left
->bit_size
)
163 lmod
= left
->ctype
.modifiers
;
164 rmod
= right
->ctype
.modifiers
;
165 if ((lmod
^ rmod
) & MOD_UNSIGNED
) {
166 if (lmod
& MOD_UNSIGNED
)
168 } else if ((lmod
& ~rmod
) & (MOD_LONG
| MOD_LONGLONG
))
176 static int same_cast_type(struct symbol
*orig
, struct symbol
*new)
178 return orig
->bit_size
== new->bit_size
&& orig
->bit_offset
== new->bit_offset
;
181 static struct symbol
*base_type(struct symbol
*node
, unsigned long *modp
, unsigned long *asp
)
183 unsigned long mod
, as
;
187 mod
|= node
->ctype
.modifiers
;
188 as
|= node
->ctype
.as
;
189 if (node
->type
== SYM_NODE
) {
190 node
= node
->ctype
.base_type
;
195 *modp
= mod
& ~MOD_IGNORE
;
200 static int is_same_type(struct expression
*expr
, struct symbol
*new)
202 struct symbol
*old
= expr
->ctype
;
203 unsigned long oldmod
, newmod
, oldas
, newas
;
205 old
= base_type(old
, &oldmod
, &oldas
);
206 new = base_type(new, &newmod
, &newas
);
208 /* Same base type, same address space? */
209 if (old
== new && oldas
== newas
) {
210 unsigned long difmod
;
212 /* Check the modifier bits. */
213 difmod
= (oldmod
^ newmod
) & ~MOD_NOCAST
;
215 /* Exact same type? */
220 * Not the same type, but differs only in "const".
221 * Don't warn about MOD_NOCAST.
223 if (difmod
== MOD_CONST
)
226 if ((oldmod
| newmod
) & MOD_NOCAST
) {
227 const char *tofrom
= "to/from";
228 if (!(newmod
& MOD_NOCAST
))
230 if (!(oldmod
& MOD_NOCAST
))
232 warning(expr
->pos
, "implicit cast %s nocast type", tofrom
);
238 warn_for_different_enum_types (struct position pos
,
239 struct symbol
*typea
,
240 struct symbol
*typeb
)
244 if (typea
->type
== SYM_NODE
)
245 typea
= typea
->ctype
.base_type
;
246 if (typeb
->type
== SYM_NODE
)
247 typeb
= typeb
->ctype
.base_type
;
252 if (typea
->type
== SYM_ENUM
&& typeb
->type
== SYM_ENUM
) {
253 warning(pos
, "mixing different enum types");
254 info(pos
, " %s versus", show_typename(typea
));
255 info(pos
, " %s", show_typename(typeb
));
260 * This gets called for implicit casts in assignments and
261 * integer promotion. We often want to try to move the
262 * cast down, because the ops involved may have been
263 * implicitly cast up, and we can get rid of the casts
266 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
268 struct expression
*expr
;
270 warn_for_different_enum_types (old
->pos
, old
->ctype
, type
);
272 if (old
->ctype
!= &null_ctype
&& is_same_type(old
, type
))
276 * See if we can simplify the op. Move the cast down.
280 if (old
->ctype
->bit_size
< type
->bit_size
)
282 if (old
->op
== '~') {
284 old
->unop
= cast_to(old
->unop
, type
);
289 case EXPR_IMPLIED_CAST
:
290 warn_for_different_enum_types(old
->pos
, old
->ctype
, type
);
292 if (old
->ctype
->bit_size
>= type
->bit_size
) {
293 struct expression
*orig
= old
->cast_expression
;
294 if (same_cast_type(orig
->ctype
, type
))
296 if (old
->ctype
->bit_offset
== type
->bit_offset
) {
298 old
->cast_type
= type
;
308 expr
= alloc_expression(old
->pos
, EXPR_IMPLIED_CAST
);
309 expr
->flags
= old
->flags
;
311 expr
->cast_type
= type
;
312 expr
->cast_expression
= old
;
316 static int is_type_type(struct symbol
*type
)
318 return (type
->ctype
.modifiers
& MOD_TYPE
) != 0;
321 int is_ptr_type(struct symbol
*type
)
323 if (type
->type
== SYM_NODE
)
324 type
= type
->ctype
.base_type
;
325 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
328 static inline int is_float_type(struct symbol
*type
)
330 if (type
->type
== SYM_NODE
)
331 type
= type
->ctype
.base_type
;
332 return type
->ctype
.base_type
== &fp_type
;
335 static inline int is_byte_type(struct symbol
*type
)
337 return type
->bit_size
== bits_in_char
&& type
->type
!= SYM_BITFIELD
;
350 static inline int classify_type(struct symbol
*type
, struct symbol
**base
)
352 static int type_class
[SYM_BAD
+ 1] = {
353 [SYM_PTR
] = TYPE_PTR
,
355 [SYM_ARRAY
] = TYPE_PTR
| TYPE_COMPOUND
,
356 [SYM_STRUCT
] = TYPE_COMPOUND
,
357 [SYM_UNION
] = TYPE_COMPOUND
,
358 [SYM_BITFIELD
] = TYPE_NUM
| TYPE_BITFIELD
,
359 [SYM_RESTRICT
] = TYPE_NUM
| TYPE_RESTRICT
,
360 [SYM_FOULED
] = TYPE_NUM
| TYPE_RESTRICT
| TYPE_FOULED
,
362 if (type
->type
== SYM_NODE
)
363 type
= type
->ctype
.base_type
;
364 if (type
->type
== SYM_ENUM
)
365 type
= type
->ctype
.base_type
;
367 if (type
->type
== SYM_BASETYPE
) {
368 if (type
->ctype
.base_type
== &int_type
)
370 if (type
->ctype
.base_type
== &fp_type
)
371 return TYPE_NUM
| TYPE_FLOAT
;
373 return type_class
[type
->type
];
376 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
378 static inline int is_string_type(struct symbol
*type
)
380 if (type
->type
== SYM_NODE
)
381 type
= type
->ctype
.base_type
;
382 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
385 static struct symbol
*bad_expr_type(struct expression
*expr
)
387 sparse_error(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
388 switch (expr
->type
) {
391 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
392 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
396 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
403 return expr
->ctype
= &bad_ctype
;
406 static int restricted_value(struct expression
*v
, struct symbol
*type
)
408 if (v
->type
!= EXPR_VALUE
)
415 static int restricted_binop(int op
, struct symbol
*type
)
420 case SPECIAL_AND_ASSIGN
:
421 case SPECIAL_OR_ASSIGN
:
422 case SPECIAL_XOR_ASSIGN
:
423 return 1; /* unfoul */
427 return 2; /* keep fouled */
429 case SPECIAL_NOTEQUAL
:
430 return 3; /* warn if fouled */
436 static int restricted_unop(int op
, struct symbol
**type
)
439 if ((*type
)->bit_size
< bits_in_int
)
440 *type
= befoul(*type
);
447 static struct symbol
*restricted_binop_type(int op
,
448 struct expression
*left
,
449 struct expression
*right
,
450 int lclass
, int rclass
,
451 struct symbol
*ltype
,
452 struct symbol
*rtype
)
454 struct symbol
*ctype
= NULL
;
455 if (lclass
& TYPE_RESTRICT
) {
456 if (rclass
& TYPE_RESTRICT
) {
457 if (ltype
== rtype
) {
459 } else if (lclass
& TYPE_FOULED
) {
460 if (ltype
->ctype
.base_type
== rtype
)
462 } else if (rclass
& TYPE_FOULED
) {
463 if (rtype
->ctype
.base_type
== ltype
)
467 if (!restricted_value(right
, ltype
))
470 } else if (!restricted_value(left
, rtype
))
474 switch (restricted_binop(op
, ctype
)) {
476 if ((lclass
^ rclass
) & TYPE_FOULED
)
477 ctype
= ctype
->ctype
.base_type
;
480 if (!(lclass
& rclass
& TYPE_FOULED
))
492 static inline void unrestrict(struct expression
*expr
,
493 int class, struct symbol
**ctype
)
495 if (class & TYPE_RESTRICT
) {
496 warning(expr
->pos
, "restricted degrades to integer");
497 if (class & TYPE_FOULED
) /* unfoul it first */
498 *ctype
= (*ctype
)->ctype
.base_type
;
499 *ctype
= (*ctype
)->ctype
.base_type
; /* get to arithmetic type */
503 static struct symbol
*usual_conversions(int op
,
504 struct expression
*left
,
505 struct expression
*right
,
506 int lclass
, int rclass
,
507 struct symbol
*ltype
,
508 struct symbol
*rtype
)
510 struct symbol
*ctype
;
512 warn_for_different_enum_types(right
->pos
, left
->ctype
, right
->ctype
);
514 if ((lclass
| rclass
) & TYPE_RESTRICT
)
518 if (!(lclass
& TYPE_FLOAT
)) {
519 if (!(rclass
& TYPE_FLOAT
))
520 return bigger_int_type(ltype
, rtype
);
523 } else if (rclass
& TYPE_FLOAT
) {
524 unsigned long lmod
= ltype
->ctype
.modifiers
;
525 unsigned long rmod
= rtype
->ctype
.modifiers
;
526 if (rmod
& ~lmod
& (MOD_LONG
| MOD_LONGLONG
))
534 ctype
= restricted_binop_type(op
, left
, right
,
535 lclass
, rclass
, ltype
, rtype
);
539 unrestrict(left
, lclass
, <ype
);
540 unrestrict(right
, rclass
, &rtype
);
545 static inline int lvalue_expression(struct expression
*expr
)
547 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
550 static int ptr_object_size(struct symbol
*ptr_type
)
552 if (ptr_type
->type
== SYM_NODE
)
553 ptr_type
= ptr_type
->ctype
.base_type
;
554 if (ptr_type
->type
== SYM_PTR
)
555 ptr_type
= get_base_type(ptr_type
);
556 return ptr_type
->bit_size
;
559 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*ctype
, struct symbol
*itype
)
561 struct expression
*index
= expr
->right
;
565 if (ctype
== &null_ctype
)
568 examine_symbol_type(ctype
);
570 if (!ctype
->ctype
.base_type
) {
571 expression_error(expr
, "missing type information");
575 /* Get the size of whatever the pointer points to */
576 bit_size
= ptr_object_size(ctype
);
577 multiply
= bit_size
>> 3;
581 if (multiply
== 1 && itype
->bit_size
>= bits_in_pointer
)
584 if (index
->type
== EXPR_VALUE
) {
585 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
586 unsigned long long v
= index
->value
, mask
;
587 mask
= 1ULL << (itype
->bit_size
- 1);
593 mask
= 1ULL << (bits_in_pointer
- 1);
594 v
&= mask
| (mask
- 1);
596 val
->ctype
= ssize_t_ctype
;
601 if (itype
->bit_size
< bits_in_pointer
)
602 index
= cast_to(index
, ssize_t_ctype
);
605 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
606 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
608 val
->ctype
= ssize_t_ctype
;
609 val
->value
= multiply
;
612 mul
->ctype
= ssize_t_ctype
;
622 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
623 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
626 unsigned long mod1
, mod2
, diff
;
627 unsigned long as1
, as2
;
629 struct symbol
*base1
, *base2
;
631 if (target
== source
)
633 if (!target
|| !source
)
634 return "different types";
636 * Peel of per-node information.
637 * FIXME! Check alignment and context too here!
639 mod1
= target
->ctype
.modifiers
;
640 as1
= target
->ctype
.as
;
641 mod2
= source
->ctype
.modifiers
;
642 as2
= source
->ctype
.as
;
643 if (target
->type
== SYM_NODE
) {
644 target
= target
->ctype
.base_type
;
647 if (target
->type
== SYM_PTR
) {
651 mod1
|= target
->ctype
.modifiers
;
652 as1
|= target
->ctype
.as
;
654 if (source
->type
== SYM_NODE
) {
655 source
= source
->ctype
.base_type
;
658 if (source
->type
== SYM_PTR
) {
662 mod2
|= source
->ctype
.modifiers
;
663 as2
|= source
->ctype
.as
;
665 if (target
->type
== SYM_ENUM
) {
666 target
= target
->ctype
.base_type
;
670 if (source
->type
== SYM_ENUM
) {
671 source
= source
->ctype
.base_type
;
676 if (target
== source
)
678 if (!target
|| !source
)
679 return "different types";
681 type1
= target
->type
;
682 base1
= target
->ctype
.base_type
;
684 type2
= source
->type
;
685 base2
= source
->ctype
.base_type
;
688 * Pointers to functions compare as the function itself
690 if (type1
== SYM_PTR
&& base1
) {
691 base1
= examine_symbol_type(base1
);
692 switch (base1
->type
) {
696 base1
= base1
->ctype
.base_type
;
701 if (type2
== SYM_PTR
&& base2
) {
702 base2
= examine_symbol_type(base2
);
703 switch (base2
->type
) {
707 base2
= base2
->ctype
.base_type
;
713 /* Arrays degenerate to pointers for type comparisons */
714 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
715 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
717 if (type1
!= type2
|| type1
== SYM_RESTRICT
)
718 return "different base types";
720 /* Must be same address space to be comparable */
721 if (Waddress_space
&& as1
!= as2
)
722 return "different address spaces";
724 /* Ignore differences in storage types or addressability */
725 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
726 diff
&= (mod1
& ~target_mod_ignore
) | (mod2
& ~source_mod_ignore
);
729 return "different type sizes";
730 if (diff
& ~MOD_SIGNEDNESS
)
731 return "different modifiers";
733 /* Differs in signedness only.. */
736 * Warn if both are explicitly signed ("unsigned" is obviously
737 * always explicit, and since we know one of them has to be
738 * unsigned, we check if the signed one was explicit).
740 if ((mod1
| mod2
) & MOD_EXPLICITLY_SIGNED
)
741 return "different explicit signedness";
744 * "char" matches both "unsigned char" and "signed char",
745 * so if the explicit test didn't trigger, then we should
746 * not warn about a char.
748 if (!(mod1
& MOD_CHAR
))
749 return "different signedness";
753 if (type1
== SYM_FN
) {
755 struct symbol
*arg1
, *arg2
;
756 if (base1
->variadic
!= base2
->variadic
)
757 return "incompatible variadic arguments";
758 PREPARE_PTR_LIST(target
->arguments
, arg1
);
759 PREPARE_PTR_LIST(source
->arguments
, arg2
);
763 diffstr
= type_difference(arg1
, arg2
, 0, 0);
765 static char argdiff
[80];
766 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diffstr
);
775 FINISH_PTR_LIST(arg2
);
776 FINISH_PTR_LIST(arg1
);
785 static void bad_null(struct expression
*expr
)
787 if (Wnon_pointer_null
)
788 warning(expr
->pos
, "Using plain integer as NULL pointer");
792 * Ignore differences in "volatile" and "const"ness when
793 * subtracting pointers
795 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
797 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
)
799 const char *typediff
;
800 struct symbol
*ctype
;
801 struct symbol
*ltype
, *rtype
;
802 struct expression
*l
= expr
->left
;
803 struct expression
*r
= expr
->right
;
805 ltype
= degenerate(l
);
806 rtype
= degenerate(r
);
809 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
811 expression_error(expr
, "subtraction of different types can't work (%s)", typediff
);
812 examine_symbol_type(ctype
);
814 /* Figure out the base type we point to */
815 if (ctype
->type
== SYM_NODE
)
816 ctype
= ctype
->ctype
.base_type
;
817 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
818 expression_error(expr
, "subtraction of functions? Share your drugs");
821 ctype
= get_base_type(ctype
);
823 expr
->ctype
= ssize_t_ctype
;
824 if (ctype
->bit_size
> bits_in_char
) {
825 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
826 struct expression
*div
= expr
;
827 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
828 unsigned long value
= ctype
->bit_size
>> 3;
830 val
->ctype
= size_t_ctype
;
833 if (value
& (value
-1)) {
834 if (Wptr_subtraction_blows
)
835 warning(expr
->pos
, "potentially expensive pointer subtraction");
839 sub
->ctype
= ssize_t_ctype
;
848 return ssize_t_ctype
;
851 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
853 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
855 struct symbol
*ctype
;
860 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
861 warning(expr
->pos
, "assignment expression in conditional");
863 ctype
= evaluate_expression(expr
);
865 if (is_safe_type(ctype
))
866 warning(expr
->pos
, "testing a 'safe expression'");
872 static struct symbol
*evaluate_logical(struct expression
*expr
)
874 if (!evaluate_conditional(expr
->left
, 0))
876 if (!evaluate_conditional(expr
->right
, 0))
879 expr
->ctype
= &bool_ctype
;
881 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
887 static struct symbol
*evaluate_binop(struct expression
*expr
)
889 struct symbol
*ltype
, *rtype
, *ctype
;
890 int lclass
= classify_type(expr
->left
->ctype
, <ype
);
891 int rclass
= classify_type(expr
->right
->ctype
, &rtype
);
895 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
899 /* number op number */
900 if (lclass
& rclass
& TYPE_NUM
) {
901 if ((lclass
| rclass
) & TYPE_FLOAT
) {
903 case '+': case '-': case '*': case '/':
906 return bad_expr_type(expr
);
910 if (op
== SPECIAL_LEFTSHIFT
|| op
== SPECIAL_RIGHTSHIFT
) {
911 // shifts do integer promotions, but that's it.
912 unrestrict(expr
->left
, lclass
, <ype
);
913 unrestrict(expr
->right
, rclass
, &rtype
);
914 ctype
= ltype
= integer_promotion(ltype
);
915 rtype
= integer_promotion(rtype
);
917 // The rest do usual conversions
918 ltype
= usual_conversions(op
, expr
->left
, expr
->right
,
919 lclass
, rclass
, ltype
, rtype
);
920 ctype
= rtype
= ltype
;
923 expr
->left
= cast_to(expr
->left
, ltype
);
924 expr
->right
= cast_to(expr
->right
, rtype
);
929 /* pointer (+|-) integer */
930 if (lclass
& TYPE_PTR
&& is_int(rclass
) && (op
== '+' || op
== '-')) {
931 unrestrict(expr
->right
, rclass
, &rtype
);
932 return evaluate_ptr_add(expr
, degenerate(expr
->left
), rtype
);
935 /* integer + pointer */
936 if (rclass
& TYPE_PTR
&& is_int(lclass
) && op
== '+') {
937 struct expression
*index
= expr
->left
;
938 unrestrict(index
, lclass
, <ype
);
939 expr
->left
= expr
->right
;
941 return evaluate_ptr_add(expr
, degenerate(expr
->left
), ltype
);
944 /* pointer - pointer */
945 if (lclass
& rclass
& TYPE_PTR
&& expr
->op
== '-')
946 return evaluate_ptr_sub(expr
);
948 return bad_expr_type(expr
);
951 static struct symbol
*evaluate_comma(struct expression
*expr
)
953 expr
->ctype
= expr
->right
->ctype
;
954 expr
->flags
&= expr
->left
->flags
& expr
->right
->flags
;
958 static int modify_for_unsigned(int op
)
961 op
= SPECIAL_UNSIGNED_LT
;
963 op
= SPECIAL_UNSIGNED_GT
;
964 else if (op
== SPECIAL_LTE
)
965 op
= SPECIAL_UNSIGNED_LTE
;
966 else if (op
== SPECIAL_GTE
)
967 op
= SPECIAL_UNSIGNED_GTE
;
971 static inline int is_null_pointer_constant(struct expression
*e
)
973 if (e
->ctype
== &null_ctype
)
975 if (!(e
->flags
& Int_const_expr
))
977 return is_zero_constant(e
) ? 2 : 0;
980 static struct symbol
*evaluate_compare(struct expression
*expr
)
982 struct expression
*left
= expr
->left
, *right
= expr
->right
;
983 struct symbol
*ltype
, *rtype
;
984 int lclass
= classify_type(degenerate(left
), <ype
);
985 int rclass
= classify_type(degenerate(right
), &rtype
);
986 struct symbol
*ctype
;
987 const char *typediff
;
990 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
995 if (is_type_type(ltype
) && is_type_type(rtype
))
998 if (is_safe_type(left
->ctype
) || is_safe_type(right
->ctype
))
999 warning(expr
->pos
, "testing a 'safe expression'");
1001 /* number on number */
1002 if (lclass
& rclass
& TYPE_NUM
) {
1003 ctype
= usual_conversions(expr
->op
, expr
->left
, expr
->right
,
1004 lclass
, rclass
, ltype
, rtype
);
1005 expr
->left
= cast_to(expr
->left
, ctype
);
1006 expr
->right
= cast_to(expr
->right
, ctype
);
1007 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
1008 expr
->op
= modify_for_unsigned(expr
->op
);
1012 /* at least one must be a pointer */
1013 if (!((lclass
| rclass
) & TYPE_PTR
))
1014 return bad_expr_type(expr
);
1016 /* equality comparisons can be with null pointer constants */
1017 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1018 int is_null1
= is_null_pointer_constant(left
);
1019 int is_null2
= is_null_pointer_constant(right
);
1024 if (is_null1
&& is_null2
) {
1025 int positive
= expr
->op
== SPECIAL_EQUAL
;
1026 expr
->type
= EXPR_VALUE
;
1027 expr
->value
= positive
;
1031 left
= cast_to(left
, rtype
);
1035 right
= cast_to(right
, ltype
);
1038 /* they also have special treatment for pointers to void */
1039 if (lclass
& rclass
& TYPE_PTR
) {
1040 if (get_base_type(ltype
) == &void_ctype
) {
1041 right
= cast_to(right
, ltype
);
1044 if (get_base_type(rtype
) == &void_ctype
) {
1045 left
= cast_to(left
, rtype
);
1050 /* both should be pointers */
1051 if (!(lclass
& rclass
& TYPE_PTR
))
1052 return bad_expr_type(expr
);
1054 expr
->op
= modify_for_unsigned(expr
->op
);
1055 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
1059 expression_error(expr
, "incompatible types in comparison expression (%s)", typediff
);
1063 expr
->ctype
= &bool_ctype
;
1068 * NOTE! The degenerate case of "x ? : y", where we don't
1069 * have a true case, this will possibly promote "x" to the
1070 * same type as "y", and thus _change_ the conditional
1071 * test in the expression. But since promotion is "safe"
1072 * for testing, that's OK.
1074 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
1076 struct expression
**true;
1077 struct symbol
*ctype
, *ltype
, *rtype
, *lbase
, *rbase
;
1079 const char * typediff
;
1082 if (!evaluate_conditional(expr
->conditional
, 0))
1084 if (!evaluate_expression(expr
->cond_false
))
1087 ctype
= degenerate(expr
->conditional
);
1088 rtype
= degenerate(expr
->cond_false
);
1090 true = &expr
->conditional
;
1092 if (expr
->cond_true
) {
1093 if (!evaluate_expression(expr
->cond_true
))
1095 ltype
= degenerate(expr
->cond_true
);
1096 true = &expr
->cond_true
;
1100 int flags
= expr
->conditional
->flags
& Int_const_expr
;
1101 flags
&= (*true)->flags
& expr
->cond_false
->flags
;
1106 lclass
= classify_type(ltype
, <ype
);
1107 rclass
= classify_type(rtype
, &rtype
);
1108 if (lclass
& rclass
& TYPE_NUM
) {
1109 ctype
= usual_conversions('?', *true, expr
->cond_false
,
1110 lclass
, rclass
, ltype
, rtype
);
1111 *true = cast_to(*true, ctype
);
1112 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1116 if ((lclass
| rclass
) & TYPE_PTR
) {
1117 int is_null1
= is_null_pointer_constant(*true);
1118 int is_null2
= is_null_pointer_constant(expr
->cond_false
);
1120 if (is_null1
&& is_null2
) {
1121 *true = cast_to(*true, &ptr_ctype
);
1122 expr
->cond_false
= cast_to(expr
->cond_false
, &ptr_ctype
);
1126 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1129 *true = cast_to(*true, rtype
);
1133 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1135 bad_null(expr
->cond_false
);
1136 expr
->cond_false
= cast_to(expr
->cond_false
, ltype
);
1140 if (!(lclass
& rclass
& TYPE_PTR
)) {
1141 typediff
= "different types";
1144 /* OK, it's pointer on pointer */
1145 if (ltype
->ctype
.as
!= rtype
->ctype
.as
) {
1146 typediff
= "different address spaces";
1150 /* need to be lazier here */
1151 lbase
= get_base_type(ltype
);
1152 rbase
= get_base_type(rtype
);
1153 qual
= ltype
->ctype
.modifiers
| rtype
->ctype
.modifiers
;
1154 qual
&= MOD_CONST
| MOD_VOLATILE
;
1156 if (lbase
== &void_ctype
) {
1157 /* XXX: pointers to function should warn here */
1162 if (rbase
== &void_ctype
) {
1163 /* XXX: pointers to function should warn here */
1167 /* XXX: that should be pointer to composite */
1169 typediff
= type_difference(lbase
, rbase
, MOD_IGN
, MOD_IGN
);
1175 /* void on void, struct on same struct, union on same union */
1176 if (ltype
== rtype
) {
1180 typediff
= "different base types";
1183 expression_error(expr
, "incompatible types in conditional expression (%s)", typediff
);
1187 expr
->ctype
= ctype
;
1191 if (qual
& ~ctype
->ctype
.modifiers
) {
1192 struct symbol
*sym
= alloc_symbol(ctype
->pos
, SYM_PTR
);
1194 sym
->ctype
.modifiers
|= qual
;
1197 *true = cast_to(*true, ctype
);
1198 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1202 /* FP assignments can not do modulo or bit operations */
1203 static int compatible_float_op(int op
)
1205 return op
== SPECIAL_ADD_ASSIGN
||
1206 op
== SPECIAL_SUB_ASSIGN
||
1207 op
== SPECIAL_MUL_ASSIGN
||
1208 op
== SPECIAL_DIV_ASSIGN
;
1211 static int evaluate_assign_op(struct expression
*expr
)
1213 struct symbol
*target
= expr
->left
->ctype
;
1214 struct symbol
*source
= expr
->right
->ctype
;
1215 struct symbol
*t
, *s
;
1216 int tclass
= classify_type(target
, &t
);
1217 int sclass
= classify_type(source
, &s
);
1220 if (tclass
& sclass
& TYPE_NUM
) {
1221 if (tclass
& TYPE_FLOAT
&& !compatible_float_op(op
)) {
1222 expression_error(expr
, "invalid assignment");
1225 if (tclass
& TYPE_RESTRICT
) {
1226 if (!restricted_binop(op
, t
)) {
1227 expression_error(expr
, "bad restricted assignment");
1230 /* allowed assignments unfoul */
1231 if (sclass
& TYPE_FOULED
&& s
->ctype
.base_type
== t
)
1233 if (!restricted_value(expr
->right
, t
))
1235 } else if (!(sclass
& TYPE_RESTRICT
))
1237 /* source and target would better be identical restricted */
1240 warning(expr
->pos
, "invalid restricted assignment");
1241 expr
->right
= cast_to(expr
->right
, target
);
1244 if (tclass
& TYPE_PTR
&& is_int(sclass
)) {
1245 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1246 unrestrict(expr
->right
, sclass
, &s
);
1247 evaluate_ptr_add(expr
, target
, s
);
1250 expression_error(expr
, "invalid pointer assignment");
1254 expression_error(expr
, "invalid assignment");
1258 expr
->right
= cast_to(expr
->right
, target
);
1262 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1263 struct expression
**rp
, const char *where
)
1265 const char *typediff
;
1266 struct symbol
*source
= degenerate(*rp
);
1267 struct symbol
*t
, *s
;
1268 int tclass
= classify_type(target
, &t
);
1269 int sclass
= classify_type(source
, &s
);
1271 if (tclass
& sclass
& TYPE_NUM
) {
1272 if (tclass
& TYPE_RESTRICT
) {
1273 /* allowed assignments unfoul */
1274 if (sclass
& TYPE_FOULED
&& s
->ctype
.base_type
== t
)
1276 if (!restricted_value(*rp
, target
))
1278 } else if (!(sclass
& TYPE_RESTRICT
))
1282 if (tclass
& TYPE_PTR
) {
1283 // NULL pointer is always OK
1284 int is_null
= is_null_pointer_constant(*rp
);
1290 if (sclass
& TYPE_PTR
&& t
->ctype
.as
== s
->ctype
.as
) {
1291 /* we should be more lazy here */
1292 int mod1
= t
->ctype
.modifiers
;
1293 int mod2
= s
->ctype
.modifiers
;
1294 s
= get_base_type(s
);
1295 t
= get_base_type(t
);
1298 * assignments to/from void * are OK, provided that
1299 * we do not remove qualifiers from pointed to [C]
1300 * or mix address spaces [sparse].
1302 if (!(mod2
& ~mod1
& (MOD_VOLATILE
| MOD_CONST
)))
1303 if (s
== &void_ctype
|| t
== &void_ctype
)
1308 /* It's OK if the target is more volatile or const than the source */
1309 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
1313 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1314 info(expr
->pos
, " expected %s", show_typename(target
));
1315 info(expr
->pos
, " got %s", show_typename(source
));
1316 *rp
= cast_to(*rp
, target
);
1319 *rp
= cast_to(*rp
, target
);
1323 static void mark_assigned(struct expression
*expr
)
1329 switch (expr
->type
) {
1334 if (sym
->type
!= SYM_NODE
)
1336 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1340 mark_assigned(expr
->left
);
1341 mark_assigned(expr
->right
);
1344 case EXPR_FORCE_CAST
:
1345 mark_assigned(expr
->cast_expression
);
1348 mark_assigned(expr
->base
);
1356 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1358 if (type
->ctype
.modifiers
& MOD_CONST
)
1359 expression_error(left
, "assignment to const expression");
1361 /* We know left is an lvalue, so it's a "preop-*" */
1362 mark_assigned(left
->unop
);
1365 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1367 struct expression
*left
= expr
->left
;
1368 struct expression
*where
= expr
;
1369 struct symbol
*ltype
;
1371 if (!lvalue_expression(left
)) {
1372 expression_error(expr
, "not an lvalue");
1376 ltype
= left
->ctype
;
1378 if (expr
->op
!= '=') {
1379 if (!evaluate_assign_op(expr
))
1382 if (!compatible_assignment_types(where
, ltype
, &expr
->right
, "assignment"))
1386 evaluate_assign_to(left
, ltype
);
1388 expr
->ctype
= ltype
;
1392 static void examine_fn_arguments(struct symbol
*fn
)
1396 FOR_EACH_PTR(fn
->arguments
, s
) {
1397 struct symbol
*arg
= evaluate_symbol(s
);
1398 /* Array/function arguments silently degenerate into pointers */
1404 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1405 if (arg
->type
== SYM_ARRAY
)
1406 ptr
->ctype
= arg
->ctype
;
1408 ptr
->ctype
.base_type
= arg
;
1409 ptr
->ctype
.as
|= s
->ctype
.as
;
1410 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
& MOD_PTRINHERIT
;
1412 s
->ctype
.base_type
= ptr
;
1414 s
->ctype
.modifiers
&= ~MOD_PTRINHERIT
;
1417 examine_symbol_type(s
);
1424 } END_FOR_EACH_PTR(s
);
1427 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1429 /* Take the modifiers of the pointer, and apply them to the member */
1430 mod
|= sym
->ctype
.modifiers
;
1431 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1432 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1434 newsym
->ctype
.as
= as
;
1435 newsym
->ctype
.modifiers
= mod
;
1441 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1443 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1444 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1446 node
->ctype
.base_type
= ptr
;
1447 ptr
->bit_size
= bits_in_pointer
;
1448 ptr
->ctype
.alignment
= pointer_alignment
;
1450 node
->bit_size
= bits_in_pointer
;
1451 node
->ctype
.alignment
= pointer_alignment
;
1454 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1455 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1456 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1458 if (sym
->type
== SYM_NODE
) {
1459 ptr
->ctype
.as
|= sym
->ctype
.as
;
1460 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1461 sym
= sym
->ctype
.base_type
;
1463 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1464 ptr
->ctype
.as
|= sym
->ctype
.as
;
1465 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1466 sym
= sym
->ctype
.base_type
;
1468 ptr
->ctype
.base_type
= sym
;
1473 /* Arrays degenerate into pointers on pointer arithmetic */
1474 static struct symbol
*degenerate(struct expression
*expr
)
1476 struct symbol
*ctype
, *base
;
1480 ctype
= expr
->ctype
;
1483 base
= examine_symbol_type(ctype
);
1484 if (ctype
->type
== SYM_NODE
)
1485 base
= ctype
->ctype
.base_type
;
1487 * Arrays degenerate into pointers to the entries, while
1488 * functions degenerate into pointers to themselves.
1489 * If array was part of non-lvalue compound, we create a copy
1490 * of that compound first and then act as if we were dealing with
1491 * the corresponding field in there.
1493 switch (base
->type
) {
1495 if (expr
->type
== EXPR_SLICE
) {
1496 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1497 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1499 a
->ctype
.base_type
= expr
->base
->ctype
;
1500 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1501 a
->array_size
= expr
->base
->ctype
->array_size
;
1503 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1505 e0
->ctype
= &lazy_ptr_ctype
;
1507 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1510 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1512 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1514 e2
->right
= expr
->base
;
1516 e2
->ctype
= expr
->base
->ctype
;
1518 if (expr
->r_bitpos
) {
1519 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1522 e3
->right
= alloc_const_expression(expr
->pos
,
1523 expr
->r_bitpos
>> 3);
1524 e3
->ctype
= &lazy_ptr_ctype
;
1529 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1532 e4
->ctype
= &lazy_ptr_ctype
;
1535 expr
->type
= EXPR_PREOP
;
1539 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1540 expression_error(expr
, "strange non-value function or array");
1543 *expr
= *expr
->unop
;
1544 ctype
= create_pointer(expr
, ctype
, 1);
1545 expr
->ctype
= ctype
;
1552 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1554 struct expression
*op
= expr
->unop
;
1555 struct symbol
*ctype
;
1557 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1558 expression_error(expr
, "not addressable");
1565 if (expr
->type
== EXPR_SYMBOL
) {
1566 struct symbol
*sym
= expr
->symbol
;
1567 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1571 * symbol expression evaluation is lazy about the type
1572 * of the sub-expression, so we may have to generate
1573 * the type here if so..
1575 if (expr
->ctype
== &lazy_ptr_ctype
) {
1576 ctype
= create_pointer(expr
, ctype
, 0);
1577 expr
->ctype
= ctype
;
1583 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1585 struct expression
*op
= expr
->unop
;
1586 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1588 /* Simplify: *&(expr) => (expr) */
1589 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1595 /* Dereferencing a node drops all the node information. */
1596 if (ctype
->type
== SYM_NODE
)
1597 ctype
= ctype
->ctype
.base_type
;
1599 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1600 target
= ctype
->ctype
.base_type
;
1602 switch (ctype
->type
) {
1604 expression_error(expr
, "cannot dereference this type");
1607 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1608 merge_type(node
, ctype
);
1612 if (!lvalue_expression(op
)) {
1613 expression_error(op
, "non-lvalue array??");
1617 /* Do the implied "addressof" on the array */
1621 * When an array is dereferenced, we need to pick
1622 * up the attributes of the original node too..
1624 merge_type(node
, op
->ctype
);
1625 merge_type(node
, ctype
);
1629 node
->bit_size
= target
->bit_size
;
1630 node
->array_size
= target
->array_size
;
1637 * Unary post-ops: x++ and x--
1639 static struct symbol
*evaluate_postop(struct expression
*expr
)
1641 struct expression
*op
= expr
->unop
;
1642 struct symbol
*ctype
= op
->ctype
;
1644 if (!lvalue_expression(expr
->unop
)) {
1645 expression_error(expr
, "need lvalue expression for ++/--");
1648 if (is_restricted_type(ctype
) && restricted_unop(expr
->op
, &ctype
)) {
1649 expression_error(expr
, "bad operation on restricted");
1651 } else if (is_fouled_type(ctype
) && restricted_unop(expr
->op
, &ctype
)) {
1652 expression_error(expr
, "bad operation on restricted");
1656 evaluate_assign_to(op
, ctype
);
1658 expr
->ctype
= ctype
;
1660 if (is_ptr_type(ctype
))
1661 expr
->op_value
= ptr_object_size(ctype
) >> 3;
1666 static struct symbol
*evaluate_sign(struct expression
*expr
)
1668 struct symbol
*ctype
= expr
->unop
->ctype
;
1669 int class = classify_type(ctype
, &ctype
);
1670 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1672 /* should be an arithmetic type */
1673 if (!(class & TYPE_NUM
))
1674 return bad_expr_type(expr
);
1675 if (!(class & (TYPE_FLOAT
|TYPE_RESTRICT
))) {
1676 struct symbol
*rtype
= integer_promotion(ctype
);
1677 expr
->unop
= cast_to(expr
->unop
, rtype
);
1679 } else if ((class & TYPE_FLOAT
) && expr
->op
!= '~') {
1680 /* no conversions needed */
1681 } else if ((class & TYPE_RESTRICT
) && !restricted_unop(expr
->op
, &ctype
)) {
1682 /* no conversions needed */
1684 return bad_expr_type(expr
);
1686 if (expr
->op
== '+')
1687 *expr
= *expr
->unop
;
1688 expr
->ctype
= ctype
;
1692 static struct symbol
*evaluate_preop(struct expression
*expr
)
1694 struct symbol
*ctype
= expr
->unop
->ctype
;
1698 *expr
= *expr
->unop
;
1704 return evaluate_sign(expr
);
1707 return evaluate_dereference(expr
);
1710 return evaluate_addressof(expr
);
1712 case SPECIAL_INCREMENT
:
1713 case SPECIAL_DECREMENT
:
1715 * From a type evaluation standpoint the preops are
1716 * the same as the postops
1718 return evaluate_postop(expr
);
1721 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1723 if (is_safe_type(ctype
))
1724 warning(expr
->pos
, "testing a 'safe expression'");
1725 if (is_float_type(ctype
)) {
1726 struct expression
*arg
= expr
->unop
;
1727 expr
->type
= EXPR_BINOP
;
1728 expr
->op
= SPECIAL_EQUAL
;
1730 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1731 expr
->right
->ctype
= ctype
;
1732 expr
->right
->fvalue
= 0;
1733 } else if (is_fouled_type(ctype
)) {
1734 warning(expr
->pos
, "restricted degrades to integer");
1736 ctype
= &bool_ctype
;
1742 expr
->ctype
= ctype
;
1746 static struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1748 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1749 struct ptr_list
*list
= head
;
1755 for (i
= 0; i
< list
->nr
; i
++) {
1756 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1758 if (sym
->ident
!= ident
)
1760 *offset
= sym
->offset
;
1763 struct symbol
*ctype
= sym
->ctype
.base_type
;
1767 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1769 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1772 *offset
+= sym
->offset
;
1776 } while ((list
= list
->next
) != head
);
1780 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1782 struct expression
*add
;
1785 * Create a new add-expression
1787 * NOTE! Even if we just add zero, we need a new node
1788 * for the member pointer, since it has a different
1789 * type than the original pointer. We could make that
1790 * be just a cast, but the fact is, a node is a node,
1791 * so we might as well just do the "add zero" here.
1793 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1796 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1797 add
->right
->ctype
= &int_ctype
;
1798 add
->right
->value
= offset
;
1801 * The ctype of the pointer will be lazily evaluated if
1802 * we ever take the address of this member dereference..
1804 add
->ctype
= &lazy_ptr_ctype
;
1808 /* structure/union dereference */
1809 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1812 struct symbol
*ctype
, *member
;
1813 struct expression
*deref
= expr
->deref
, *add
;
1814 struct ident
*ident
= expr
->member
;
1818 if (!evaluate_expression(deref
))
1821 expression_error(expr
, "bad member name");
1825 ctype
= deref
->ctype
;
1826 address_space
= ctype
->ctype
.as
;
1827 mod
= ctype
->ctype
.modifiers
;
1828 if (ctype
->type
== SYM_NODE
) {
1829 ctype
= ctype
->ctype
.base_type
;
1830 address_space
|= ctype
->ctype
.as
;
1831 mod
|= ctype
->ctype
.modifiers
;
1833 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1834 expression_error(expr
, "expected structure or union");
1837 examine_symbol_type(ctype
);
1839 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1841 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1842 const char *name
= "<unnamed>";
1845 name
= ctype
->ident
->name
;
1846 namelen
= ctype
->ident
->len
;
1848 if (ctype
->symbol_list
)
1849 expression_error(expr
, "no member '%s' in %s %.*s",
1850 show_ident(ident
), type
, namelen
, name
);
1852 expression_error(expr
, "using member '%s' in "
1853 "incomplete %s %.*s", show_ident(ident
),
1854 type
, namelen
, name
);
1859 * The member needs to take on the address space and modifiers of
1860 * the "parent" type.
1862 member
= convert_to_as_mod(member
, address_space
, mod
);
1863 ctype
= get_base_type(member
);
1865 if (!lvalue_expression(deref
)) {
1866 if (deref
->type
!= EXPR_SLICE
) {
1870 expr
->base
= deref
->base
;
1871 expr
->r_bitpos
= deref
->r_bitpos
;
1873 expr
->r_bitpos
+= offset
<< 3;
1874 expr
->type
= EXPR_SLICE
;
1875 expr
->r_nrbits
= member
->bit_size
;
1876 expr
->r_bitpos
+= member
->bit_offset
;
1877 expr
->ctype
= member
;
1881 deref
= deref
->unop
;
1882 expr
->deref
= deref
;
1884 add
= evaluate_offset(deref
, offset
);
1885 expr
->type
= EXPR_PREOP
;
1889 expr
->ctype
= member
;
1893 static int is_promoted(struct expression
*expr
)
1896 switch (expr
->type
) {
1899 case EXPR_CONDITIONAL
:
1923 static struct symbol
*evaluate_cast(struct expression
*);
1925 static struct symbol
*evaluate_type_information(struct expression
*expr
)
1927 struct symbol
*sym
= expr
->cast_type
;
1929 sym
= evaluate_expression(expr
->cast_expression
);
1933 * Expressions of restricted types will possibly get
1934 * promoted - check that here
1936 if (is_restricted_type(sym
)) {
1937 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
1939 } else if (is_fouled_type(sym
)) {
1943 examine_symbol_type(sym
);
1944 if (is_bitfield_type(sym
)) {
1945 expression_error(expr
, "trying to examine bitfield type");
1951 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1953 struct symbol
*type
;
1956 type
= evaluate_type_information(expr
);
1960 size
= type
->bit_size
;
1961 if ((size
< 0) || (size
& 7))
1962 expression_error(expr
, "cannot size expression");
1963 expr
->type
= EXPR_VALUE
;
1964 expr
->value
= size
>> 3;
1966 expr
->ctype
= size_t_ctype
;
1967 return size_t_ctype
;
1970 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
1972 struct symbol
*type
;
1975 type
= evaluate_type_information(expr
);
1979 if (type
->type
== SYM_NODE
)
1980 type
= type
->ctype
.base_type
;
1983 switch (type
->type
) {
1987 type
= get_base_type(type
);
1991 expression_error(expr
, "expected pointer expression");
1994 size
= type
->bit_size
;
1997 expr
->type
= EXPR_VALUE
;
1998 expr
->value
= size
>> 3;
2000 expr
->ctype
= size_t_ctype
;
2001 return size_t_ctype
;
2004 static struct symbol
*evaluate_alignof(struct expression
*expr
)
2006 struct symbol
*type
;
2008 type
= evaluate_type_information(expr
);
2012 expr
->type
= EXPR_VALUE
;
2013 expr
->value
= type
->ctype
.alignment
;
2015 expr
->ctype
= size_t_ctype
;
2016 return size_t_ctype
;
2019 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
2021 struct expression
*expr
;
2022 struct symbol_list
*argument_types
= fn
->arguments
;
2023 struct symbol
*argtype
;
2026 PREPARE_PTR_LIST(argument_types
, argtype
);
2027 FOR_EACH_PTR (head
, expr
) {
2028 struct expression
**p
= THIS_ADDRESS(expr
);
2029 struct symbol
*ctype
, *target
;
2030 ctype
= evaluate_expression(expr
);
2037 struct symbol
*type
;
2038 int class = classify_type(ctype
, &type
);
2039 if (is_int(class)) {
2040 *p
= cast_to(expr
, integer_promotion(type
));
2041 } else if (class & TYPE_FLOAT
) {
2042 unsigned long mod
= type
->ctype
.modifiers
;
2043 if (!(mod
& (MOD_LONG
|MOD_LONGLONG
)))
2044 *p
= cast_to(expr
, &double_ctype
);
2045 } else if (class & TYPE_PTR
) {
2046 if (expr
->ctype
== &null_ctype
)
2047 *p
= cast_to(expr
, &ptr_ctype
);
2052 static char where
[30];
2053 examine_symbol_type(target
);
2054 sprintf(where
, "argument %d", i
);
2055 compatible_assignment_types(expr
, target
, p
, where
);
2059 NEXT_PTR_LIST(argtype
);
2060 } END_FOR_EACH_PTR(expr
);
2061 FINISH_PTR_LIST(argtype
);
2065 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
2069 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
2070 if (sym
->ident
== ident
)
2072 } END_FOR_EACH_PTR(sym
);
2076 static void convert_index(struct expression
*e
)
2078 struct expression
*child
= e
->idx_expression
;
2079 unsigned from
= e
->idx_from
;
2080 unsigned to
= e
->idx_to
+ 1;
2082 e
->init_offset
= from
* (e
->ctype
->bit_size
>>3);
2083 e
->init_nr
= to
- from
;
2084 e
->init_expr
= child
;
2087 static void convert_ident(struct expression
*e
)
2089 struct expression
*child
= e
->ident_expression
;
2090 struct symbol
*sym
= e
->field
;
2092 e
->init_offset
= sym
->offset
;
2094 e
->init_expr
= child
;
2097 static void convert_designators(struct expression
*e
)
2100 if (e
->type
== EXPR_INDEX
)
2102 else if (e
->type
== EXPR_IDENTIFIER
)
2110 static void excess(struct expression
*e
, const char *s
)
2112 warning(e
->pos
, "excessive elements in %s initializer", s
);
2116 * implicit designator for the first element
2118 static struct expression
*first_subobject(struct symbol
*ctype
, int class,
2119 struct expression
**v
)
2121 struct expression
*e
= *v
, *new;
2123 if (ctype
->type
== SYM_NODE
)
2124 ctype
= ctype
->ctype
.base_type
;
2126 if (class & TYPE_PTR
) { /* array */
2127 if (!ctype
->bit_size
)
2129 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2130 new->idx_expression
= e
;
2131 new->ctype
= ctype
->ctype
.base_type
;
2133 struct symbol
*field
, *p
;
2134 PREPARE_PTR_LIST(ctype
->symbol_list
, p
);
2135 while (p
&& !p
->ident
&& is_bitfield_type(p
))
2141 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2142 new->ident_expression
= e
;
2143 new->field
= new->ctype
= field
;
2150 * sanity-check explicit designators; return the innermost one or NULL
2151 * in case of error. Assign types.
2153 static struct expression
*check_designators(struct expression
*e
,
2154 struct symbol
*ctype
)
2156 struct expression
*last
= NULL
;
2159 if (ctype
->type
== SYM_NODE
)
2160 ctype
= ctype
->ctype
.base_type
;
2161 if (e
->type
== EXPR_INDEX
) {
2162 struct symbol
*type
;
2163 if (ctype
->type
!= SYM_ARRAY
) {
2164 err
= "array index in non-array";
2167 type
= ctype
->ctype
.base_type
;
2168 if (ctype
->bit_size
>= 0 && type
->bit_size
>= 0) {
2169 unsigned offset
= e
->idx_to
* type
->bit_size
;
2170 if (offset
>= ctype
->bit_size
) {
2171 err
= "index out of bounds in";
2175 e
->ctype
= ctype
= type
;
2178 if (!e
->idx_expression
) {
2182 e
= e
->idx_expression
;
2183 } else if (e
->type
== EXPR_IDENTIFIER
) {
2184 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2185 err
= "field name not in struct or union";
2188 ctype
= find_struct_ident(ctype
, e
->expr_ident
);
2190 err
= "unknown field name in";
2193 e
->field
= e
->ctype
= ctype
;
2195 if (!e
->ident_expression
) {
2199 e
= e
->ident_expression
;
2200 } else if (e
->type
== EXPR_POS
) {
2201 err
= "internal front-end error: EXPR_POS in";
2206 expression_error(e
, "%s initializer", err
);
2211 * choose the next subobject to initialize.
2213 * Get designators for next element, switch old ones to EXPR_POS.
2214 * Return the resulting expression or NULL if we'd run out of subobjects.
2215 * The innermost designator is returned in *v. Designators in old
2216 * are assumed to be already sanity-checked.
2218 static struct expression
*next_designators(struct expression
*old
,
2219 struct symbol
*ctype
,
2220 struct expression
*e
, struct expression
**v
)
2222 struct expression
*new = NULL
;
2226 if (old
->type
== EXPR_INDEX
) {
2227 struct expression
*copy
;
2230 copy
= next_designators(old
->idx_expression
,
2233 n
= old
->idx_to
+ 1;
2234 if (n
* old
->ctype
->bit_size
== ctype
->bit_size
) {
2239 *v
= new = alloc_expression(e
->pos
, EXPR_INDEX
);
2242 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2245 new->idx_from
= new->idx_to
= n
;
2246 new->idx_expression
= copy
;
2247 new->ctype
= old
->ctype
;
2249 } else if (old
->type
== EXPR_IDENTIFIER
) {
2250 struct expression
*copy
;
2251 struct symbol
*field
;
2253 copy
= next_designators(old
->ident_expression
,
2256 field
= old
->field
->next_subobject
;
2262 *v
= new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2265 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2269 new->expr_ident
= field
->ident
;
2270 new->ident_expression
= copy
;
2277 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2278 int class, struct symbol
*ctype
);
2281 * deal with traversing subobjects [6.7.8(17,18,20)]
2283 static void handle_list_initializer(struct expression
*expr
,
2284 int class, struct symbol
*ctype
)
2286 struct expression
*e
, *last
= NULL
, *top
= NULL
, *next
;
2289 FOR_EACH_PTR(expr
->expr_list
, e
) {
2290 struct expression
**v
;
2291 struct symbol
*type
;
2294 if (e
->type
!= EXPR_INDEX
&& e
->type
!= EXPR_IDENTIFIER
) {
2297 last
= first_subobject(ctype
, class, &top
);
2299 last
= next_designators(last
, ctype
, e
, &top
);
2302 excess(e
, class & TYPE_PTR
? "array" :
2304 DELETE_CURRENT_PTR(e
);
2308 warning(e
->pos
, "advancing past deep designator");
2311 REPLACE_CURRENT_PTR(e
, last
);
2313 next
= check_designators(e
, ctype
);
2315 DELETE_CURRENT_PTR(e
);
2319 /* deeper than one designator? */
2321 convert_designators(last
);
2326 lclass
= classify_type(top
->ctype
, &type
);
2327 if (top
->type
== EXPR_INDEX
)
2328 v
= &top
->idx_expression
;
2330 v
= &top
->ident_expression
;
2332 if (handle_simple_initializer(v
, 1, lclass
, top
->ctype
))
2335 if (!(lclass
& TYPE_COMPOUND
)) {
2336 warning(e
->pos
, "bogus scalar initializer");
2337 DELETE_CURRENT_PTR(e
);
2341 next
= first_subobject(type
, lclass
, v
);
2343 warning(e
->pos
, "missing braces around initializer");
2348 DELETE_CURRENT_PTR(e
);
2349 excess(e
, lclass
& TYPE_PTR
? "array" : "struct or union");
2351 } END_FOR_EACH_PTR(e
);
2353 convert_designators(last
);
2354 expr
->ctype
= ctype
;
2357 static int is_string_literal(struct expression
**v
)
2359 struct expression
*e
= *v
;
2360 while (e
&& e
->type
== EXPR_PREOP
&& e
->op
== '(')
2362 if (!e
|| e
->type
!= EXPR_STRING
)
2364 if (e
!= *v
&& Wparen_string
)
2366 "array initialized from parenthesized string constant");
2372 * We want a normal expression, possibly in one layer of braces. Warn
2373 * if the latter happens inside a list (it's legal, but likely to be
2374 * an effect of screwup). In case of anything not legal, we are definitely
2375 * having an effect of screwup, so just fail and let the caller warn.
2377 static struct expression
*handle_scalar(struct expression
*e
, int nested
)
2379 struct expression
*v
= NULL
, *p
;
2383 if (e
->type
!= EXPR_INITIALIZER
)
2386 FOR_EACH_PTR(e
->expr_list
, p
) {
2390 } END_FOR_EACH_PTR(p
);
2394 case EXPR_INITIALIZER
:
2396 case EXPR_IDENTIFIER
:
2402 warning(e
->pos
, "braces around scalar initializer");
2407 * deal with the cases that don't care about subobjects:
2408 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2409 * character array <- string literal, possibly in braces [6.7.8(14)]
2410 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2411 * compound type <- initializer list in braces [6.7.8(16)]
2412 * The last one punts to handle_list_initializer() which, in turn will call
2413 * us for individual elements of the list.
2415 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2416 * the lack of support of wide char stuff in general.
2418 * One note: we need to take care not to evaluate a string literal until
2419 * we know that we *will* handle it right here. Otherwise we would screw
2420 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2421 * { "string", ...} - we need to preserve that string literal recognizable
2422 * until we dig into the inner struct.
2424 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2425 int class, struct symbol
*ctype
)
2427 int is_string
= is_string_type(ctype
);
2428 struct expression
*e
= *ep
, *p
;
2429 struct symbol
*type
;
2435 if (!(class & TYPE_COMPOUND
)) {
2436 e
= handle_scalar(e
, nested
);
2440 if (!evaluate_expression(e
))
2442 compatible_assignment_types(e
, ctype
, ep
, "initializer");
2447 * sublist; either a string, or we dig in; the latter will deal with
2448 * pathologies, so we don't need anything fancy here.
2450 if (e
->type
== EXPR_INITIALIZER
) {
2452 struct expression
*v
= NULL
;
2455 FOR_EACH_PTR(e
->expr_list
, p
) {
2459 } END_FOR_EACH_PTR(p
);
2460 if (count
== 1 && is_string_literal(&v
)) {
2465 handle_list_initializer(e
, class, ctype
);
2470 if (is_string_literal(&e
)) {
2471 /* either we are doing array of char, or we'll have to dig in */
2478 /* struct or union can be initialized by compatible */
2479 if (class != TYPE_COMPOUND
)
2481 type
= evaluate_expression(e
);
2484 if (ctype
->type
== SYM_NODE
)
2485 ctype
= ctype
->ctype
.base_type
;
2486 if (type
->type
== SYM_NODE
)
2487 type
= type
->ctype
.base_type
;
2493 p
= alloc_expression(e
->pos
, EXPR_STRING
);
2495 type
= evaluate_expression(p
);
2496 if (ctype
->bit_size
!= -1 &&
2497 ctype
->bit_size
+ bits_in_char
< type
->bit_size
) {
2499 "too long initializer-string for array of char");
2505 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2507 struct symbol
*type
;
2508 int class = classify_type(ctype
, &type
);
2509 if (!handle_simple_initializer(ep
, 0, class, ctype
))
2510 expression_error(*ep
, "invalid initializer");
2513 static struct symbol
*evaluate_cast(struct expression
*expr
)
2515 struct expression
*target
= expr
->cast_expression
;
2516 struct symbol
*ctype
;
2517 struct symbol
*t1
, *t2
;
2519 int as1
= 0, as2
= 0;
2525 * Special case: a cast can be followed by an
2526 * initializer, in which case we need to pass
2527 * the type value down to that initializer rather
2528 * than trying to evaluate it as an expression
2530 * A more complex case is when the initializer is
2531 * dereferenced as part of a post-fix expression.
2532 * We need to produce an expression that can be dereferenced.
2534 if (target
->type
== EXPR_INITIALIZER
) {
2535 struct symbol
*sym
= expr
->cast_type
;
2536 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2538 sym
->initializer
= target
;
2539 evaluate_symbol(sym
);
2541 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2544 expr
->type
= EXPR_PREOP
;
2552 ctype
= examine_symbol_type(expr
->cast_type
);
2553 expr
->ctype
= ctype
;
2554 expr
->cast_type
= ctype
;
2556 evaluate_expression(target
);
2559 class1
= classify_type(ctype
, &t1
);
2561 /* cast to non-integer type -> not an integer constant expression */
2562 if (!is_int(class1
))
2564 /* if argument turns out to be not an integer constant expression *and*
2565 it was not a floating literal to start with -> too bad */
2566 else if (expr
->flags
== Int_const_expr
&&
2567 !(target
->flags
& Int_const_expr
))
2570 * You can always throw a value away by casting to
2571 * "void" - that's an implicit "force". Note that
2572 * the same is _not_ true of "void *".
2574 if (t1
== &void_ctype
)
2577 if (class1
& TYPE_COMPOUND
)
2578 warning(expr
->pos
, "cast to non-scalar");
2580 if (class1
== TYPE_PTR
)
2585 expression_error(expr
, "cast from unknown type");
2588 class2
= classify_type(t2
, &t2
);
2590 if (class2
& TYPE_COMPOUND
)
2591 warning(expr
->pos
, "cast from non-scalar");
2593 if (expr
->type
== EXPR_FORCE_CAST
)
2596 /* allowed cast unfouls */
2597 if (class2
& TYPE_FOULED
)
2598 t2
= t2
->ctype
.base_type
;
2601 if (class1
& TYPE_RESTRICT
)
2602 warning(expr
->pos
, "cast to restricted type");
2603 if (class2
& TYPE_RESTRICT
)
2604 warning(expr
->pos
, "cast from restricted type");
2607 if (t1
== &ulong_ctype
)
2609 else if (class1
== TYPE_PTR
)
2612 if (t2
== &ulong_ctype
)
2614 else if (class2
== TYPE_PTR
)
2617 if (!as1
&& as2
> 0)
2618 warning(expr
->pos
, "cast removes address space of expression");
2619 if (as1
> 0 && as2
> 0 && as1
!= as2
)
2620 warning(expr
->pos
, "cast between address spaces (<asn:%d>-><asn:%d>)", as2
, as1
);
2621 if (as1
> 0 && !as2
&&
2622 !is_null_pointer_constant(target
) && Wcast_to_address_space
)
2624 "cast adds address space to expression (<asn:%d>)", as1
);
2626 if (!(t1
->ctype
.modifiers
& MOD_PTRINHERIT
) && class1
== TYPE_PTR
&&
2627 !as1
&& (target
->flags
& Int_const_expr
)) {
2628 if (t1
->ctype
.base_type
== &void_ctype
) {
2629 if (is_zero_constant(target
)) {
2631 expr
->type
= EXPR_VALUE
;
2632 expr
->ctype
= &null_ctype
;
2643 * Evaluate a call expression with a symbol. This
2644 * should expand inline functions, and evaluate
2647 static int evaluate_symbol_call(struct expression
*expr
)
2649 struct expression
*fn
= expr
->fn
;
2650 struct symbol
*ctype
= fn
->ctype
;
2652 if (fn
->type
!= EXPR_PREOP
)
2655 if (ctype
->op
&& ctype
->op
->evaluate
)
2656 return ctype
->op
->evaluate(expr
);
2658 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2660 struct symbol
*curr
= current_fn
;
2661 current_fn
= ctype
->ctype
.base_type
;
2663 ret
= inline_function(expr
, ctype
);
2665 /* restore the old function */
2673 static struct symbol
*evaluate_call(struct expression
*expr
)
2676 struct symbol
*ctype
, *sym
;
2677 struct expression
*fn
= expr
->fn
;
2678 struct expression_list
*arglist
= expr
->args
;
2680 if (!evaluate_expression(fn
))
2682 sym
= ctype
= fn
->ctype
;
2683 if (ctype
->type
== SYM_NODE
)
2684 ctype
= ctype
->ctype
.base_type
;
2685 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
2686 ctype
= get_base_type(ctype
);
2688 examine_fn_arguments(ctype
);
2689 if (sym
->type
== SYM_NODE
&& fn
->type
== EXPR_PREOP
&&
2690 sym
->op
&& sym
->op
->args
) {
2691 if (!sym
->op
->args(expr
))
2694 if (!evaluate_arguments(sym
, ctype
, arglist
))
2696 if (ctype
->type
!= SYM_FN
) {
2697 expression_error(expr
, "not a function %s",
2698 show_ident(sym
->ident
));
2701 args
= expression_list_size(expr
->args
);
2702 fnargs
= symbol_list_size(ctype
->arguments
);
2704 expression_error(expr
,
2705 "not enough arguments for function %s",
2706 show_ident(sym
->ident
));
2707 if (args
> fnargs
&& !ctype
->variadic
)
2708 expression_error(expr
,
2709 "too many arguments for function %s",
2710 show_ident(sym
->ident
));
2712 if (sym
->type
== SYM_NODE
) {
2713 if (evaluate_symbol_call(expr
))
2716 expr
->ctype
= ctype
->ctype
.base_type
;
2720 static struct symbol
*evaluate_offsetof(struct expression
*expr
)
2722 struct expression
*e
= expr
->down
;
2723 struct symbol
*ctype
= expr
->in
;
2726 if (expr
->op
== '.') {
2727 struct symbol
*field
;
2730 expression_error(expr
, "expected structure or union");
2733 examine_symbol_type(ctype
);
2734 class = classify_type(ctype
, &ctype
);
2735 if (class != TYPE_COMPOUND
) {
2736 expression_error(expr
, "expected structure or union");
2740 field
= find_identifier(expr
->ident
, ctype
->symbol_list
, &offset
);
2742 expression_error(expr
, "unknown member");
2746 expr
->type
= EXPR_VALUE
;
2747 expr
->flags
= Int_const_expr
;
2748 expr
->value
= offset
;
2750 expr
->ctype
= size_t_ctype
;
2753 expression_error(expr
, "expected structure or union");
2756 examine_symbol_type(ctype
);
2757 class = classify_type(ctype
, &ctype
);
2758 if (class != (TYPE_COMPOUND
| TYPE_PTR
)) {
2759 expression_error(expr
, "expected array");
2762 ctype
= ctype
->ctype
.base_type
;
2764 expr
->type
= EXPR_VALUE
;
2765 expr
->flags
= Int_const_expr
;
2768 expr
->ctype
= size_t_ctype
;
2770 struct expression
*idx
= expr
->index
, *m
;
2771 struct symbol
*i_type
= evaluate_expression(idx
);
2772 int i_class
= classify_type(i_type
, &i_type
);
2773 if (!is_int(i_class
)) {
2774 expression_error(expr
, "non-integer index");
2777 unrestrict(idx
, i_class
, &i_type
);
2778 idx
= cast_to(idx
, size_t_ctype
);
2779 m
= alloc_const_expression(expr
->pos
,
2780 ctype
->bit_size
>> 3);
2781 m
->ctype
= size_t_ctype
;
2782 m
->flags
= Int_const_expr
;
2783 expr
->type
= EXPR_BINOP
;
2787 expr
->ctype
= size_t_ctype
;
2788 expr
->flags
= m
->flags
& idx
->flags
& Int_const_expr
;
2792 struct expression
*copy
= __alloc_expression(0);
2794 if (e
->type
== EXPR_OFFSETOF
)
2796 if (!evaluate_expression(e
))
2798 expr
->type
= EXPR_BINOP
;
2799 expr
->flags
= e
->flags
& copy
->flags
& Int_const_expr
;
2801 expr
->ctype
= size_t_ctype
;
2805 return size_t_ctype
;
2808 struct symbol
*evaluate_expression(struct expression
*expr
)
2815 switch (expr
->type
) {
2818 expression_error(expr
, "value expression without a type");
2821 return evaluate_string(expr
);
2823 return evaluate_symbol_expression(expr
);
2825 if (!evaluate_expression(expr
->left
))
2827 if (!evaluate_expression(expr
->right
))
2829 return evaluate_binop(expr
);
2831 return evaluate_logical(expr
);
2833 evaluate_expression(expr
->left
);
2834 if (!evaluate_expression(expr
->right
))
2836 return evaluate_comma(expr
);
2838 if (!evaluate_expression(expr
->left
))
2840 if (!evaluate_expression(expr
->right
))
2842 return evaluate_compare(expr
);
2843 case EXPR_ASSIGNMENT
:
2844 if (!evaluate_expression(expr
->left
))
2846 if (!evaluate_expression(expr
->right
))
2848 return evaluate_assignment(expr
);
2850 if (!evaluate_expression(expr
->unop
))
2852 return evaluate_preop(expr
);
2854 if (!evaluate_expression(expr
->unop
))
2856 return evaluate_postop(expr
);
2858 case EXPR_FORCE_CAST
:
2859 case EXPR_IMPLIED_CAST
:
2860 return evaluate_cast(expr
);
2862 return evaluate_sizeof(expr
);
2863 case EXPR_PTRSIZEOF
:
2864 return evaluate_ptrsizeof(expr
);
2866 return evaluate_alignof(expr
);
2868 return evaluate_member_dereference(expr
);
2870 return evaluate_call(expr
);
2872 case EXPR_CONDITIONAL
:
2873 return evaluate_conditional_expression(expr
);
2874 case EXPR_STATEMENT
:
2875 expr
->ctype
= evaluate_statement(expr
->statement
);
2879 expr
->ctype
= &ptr_ctype
;
2883 /* Evaluate the type of the symbol .. */
2884 evaluate_symbol(expr
->symbol
);
2885 /* .. but the type of the _expression_ is a "type" */
2886 expr
->ctype
= &type_ctype
;
2890 return evaluate_offsetof(expr
);
2892 /* These can not exist as stand-alone expressions */
2893 case EXPR_INITIALIZER
:
2894 case EXPR_IDENTIFIER
:
2897 expression_error(expr
, "internal front-end error: initializer in expression");
2900 expression_error(expr
, "internal front-end error: SLICE re-evaluated");
2906 static void check_duplicates(struct symbol
*sym
)
2909 struct symbol
*next
= sym
;
2911 while ((next
= next
->same_symbol
) != NULL
) {
2912 const char *typediff
;
2913 evaluate_symbol(next
);
2915 typediff
= type_difference(sym
, next
, 0, 0);
2917 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2918 show_ident(sym
->ident
),
2919 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
2924 unsigned long mod
= sym
->ctype
.modifiers
;
2925 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
2927 if (!(mod
& MOD_TOPLEVEL
))
2931 if (sym
->ident
== &main_ident
)
2933 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
2937 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
2939 struct symbol
*base_type
;
2947 sym
= examine_symbol_type(sym
);
2948 base_type
= get_base_type(sym
);
2952 /* Evaluate the initializers */
2953 if (sym
->initializer
)
2954 evaluate_initializer(sym
, &sym
->initializer
);
2956 /* And finally, evaluate the body of the symbol too */
2957 if (base_type
->type
== SYM_FN
) {
2958 struct symbol
*curr
= current_fn
;
2960 current_fn
= base_type
;
2962 examine_fn_arguments(base_type
);
2963 if (!base_type
->stmt
&& base_type
->inline_stmt
)
2965 if (base_type
->stmt
)
2966 evaluate_statement(base_type
->stmt
);
2974 void evaluate_symbol_list(struct symbol_list
*list
)
2978 FOR_EACH_PTR(list
, sym
) {
2979 evaluate_symbol(sym
);
2980 check_duplicates(sym
);
2981 } END_FOR_EACH_PTR(sym
);
2984 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
2986 struct expression
*expr
= stmt
->expression
;
2987 struct symbol
*fntype
;
2989 evaluate_expression(expr
);
2990 fntype
= current_fn
->ctype
.base_type
;
2991 if (!fntype
|| fntype
== &void_ctype
) {
2992 if (expr
&& expr
->ctype
!= &void_ctype
)
2993 expression_error(expr
, "return expression in %s function", fntype
?"void":"typeless");
2994 if (expr
&& Wreturn_void
)
2995 warning(stmt
->pos
, "returning void-valued expression");
3000 sparse_error(stmt
->pos
, "return with no return value");
3005 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, "return expression");
3009 static void evaluate_if_statement(struct statement
*stmt
)
3011 if (!stmt
->if_conditional
)
3014 evaluate_conditional(stmt
->if_conditional
, 0);
3015 evaluate_statement(stmt
->if_true
);
3016 evaluate_statement(stmt
->if_false
);
3019 static void evaluate_iterator(struct statement
*stmt
)
3021 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
3022 evaluate_conditional(stmt
->iterator_post_condition
,1);
3023 evaluate_statement(stmt
->iterator_pre_statement
);
3024 evaluate_statement(stmt
->iterator_statement
);
3025 evaluate_statement(stmt
->iterator_post_statement
);
3028 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
3030 switch (*constraint
) {
3031 case '=': /* Assignment */
3032 case '+': /* Update */
3035 expression_error(expr
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
3039 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
3041 switch (*constraint
) {
3042 case '=': /* Assignment */
3043 case '+': /* Update */
3044 expression_error(expr
, "input constraint with assignment (\"%s\")", constraint
);
3048 static void evaluate_asm_statement(struct statement
*stmt
)
3050 struct expression
*expr
;
3053 expr
= stmt
->asm_string
;
3054 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3055 sparse_error(stmt
->pos
, "need constant string for inline asm");
3060 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
3061 struct ident
*ident
;
3064 case 0: /* Identifier */
3066 ident
= (struct ident
*)expr
;
3069 case 1: /* Constraint */
3071 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3072 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
3073 *THIS_ADDRESS(expr
) = NULL
;
3076 verify_output_constraint(expr
, expr
->string
->data
);
3079 case 2: /* Expression */
3081 if (!evaluate_expression(expr
))
3083 if (!lvalue_expression(expr
))
3084 warning(expr
->pos
, "asm output is not an lvalue");
3085 evaluate_assign_to(expr
, expr
->ctype
);
3088 } END_FOR_EACH_PTR(expr
);
3091 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
3092 struct ident
*ident
;
3095 case 0: /* Identifier */
3097 ident
= (struct ident
*)expr
;
3100 case 1: /* Constraint */
3102 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3103 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
3104 *THIS_ADDRESS(expr
) = NULL
;
3107 verify_input_constraint(expr
, expr
->string
->data
);
3110 case 2: /* Expression */
3112 if (!evaluate_expression(expr
))
3116 } END_FOR_EACH_PTR(expr
);
3118 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
3120 sparse_error(stmt
->pos
, "bad asm output");
3123 if (expr
->type
== EXPR_STRING
)
3125 expression_error(expr
, "asm clobber is not a string");
3126 } END_FOR_EACH_PTR(expr
);
3129 static void evaluate_case_statement(struct statement
*stmt
)
3131 evaluate_expression(stmt
->case_expression
);
3132 evaluate_expression(stmt
->case_to
);
3133 evaluate_statement(stmt
->case_statement
);
3136 static void check_case_type(struct expression
*switch_expr
,
3137 struct expression
*case_expr
,
3138 struct expression
**enumcase
)
3140 struct symbol
*switch_type
, *case_type
;
3146 switch_type
= switch_expr
->ctype
;
3147 case_type
= evaluate_expression(case_expr
);
3149 if (!switch_type
|| !case_type
)
3153 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
3154 else if (is_enum_type(case_type
))
3155 *enumcase
= case_expr
;
3158 sclass
= classify_type(switch_type
, &switch_type
);
3159 cclass
= classify_type(case_type
, &case_type
);
3161 /* both should be arithmetic */
3162 if (!(sclass
& cclass
& TYPE_NUM
))
3165 /* neither should be floating */
3166 if ((sclass
| cclass
) & TYPE_FLOAT
)
3169 /* if neither is restricted, we are OK */
3170 if (!((sclass
| cclass
) & TYPE_RESTRICT
))
3173 if (!restricted_binop_type(SPECIAL_EQUAL
, case_expr
, switch_expr
,
3174 cclass
, sclass
, case_type
, switch_type
))
3175 warning(case_expr
->pos
, "restricted degrades to integer");
3180 expression_error(case_expr
, "incompatible types for 'case' statement");
3183 static void evaluate_switch_statement(struct statement
*stmt
)
3186 struct expression
*enumcase
= NULL
;
3187 struct expression
**enumcase_holder
= &enumcase
;
3188 struct expression
*sel
= stmt
->switch_expression
;
3190 evaluate_expression(sel
);
3191 evaluate_statement(stmt
->switch_statement
);
3194 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
3195 enumcase_holder
= NULL
; /* Only check cases against switch */
3197 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
3198 struct statement
*case_stmt
= sym
->stmt
;
3199 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
3200 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
3201 } END_FOR_EACH_PTR(sym
);
3204 struct symbol
*evaluate_statement(struct statement
*stmt
)
3209 switch (stmt
->type
) {
3210 case STMT_DECLARATION
: {
3212 FOR_EACH_PTR(stmt
->declaration
, s
) {
3214 } END_FOR_EACH_PTR(s
);
3219 return evaluate_return_expression(stmt
);
3221 case STMT_EXPRESSION
:
3222 if (!evaluate_expression(stmt
->expression
))
3224 if (stmt
->expression
->ctype
== &null_ctype
)
3225 stmt
->expression
= cast_to(stmt
->expression
, &ptr_ctype
);
3226 return degenerate(stmt
->expression
);
3228 case STMT_COMPOUND
: {
3229 struct statement
*s
;
3230 struct symbol
*type
= NULL
;
3232 /* Evaluate the return symbol in the compound statement */
3233 evaluate_symbol(stmt
->ret
);
3236 * Then, evaluate each statement, making the type of the
3237 * compound statement be the type of the last statement
3239 type
= evaluate_statement(stmt
->args
);
3240 FOR_EACH_PTR(stmt
->stmts
, s
) {
3241 type
= evaluate_statement(s
);
3242 } END_FOR_EACH_PTR(s
);
3248 evaluate_if_statement(stmt
);
3251 evaluate_iterator(stmt
);
3254 evaluate_switch_statement(stmt
);
3257 evaluate_case_statement(stmt
);
3260 return evaluate_statement(stmt
->label_statement
);
3262 evaluate_expression(stmt
->goto_expression
);
3267 evaluate_asm_statement(stmt
);
3270 evaluate_expression(stmt
->expression
);
3273 evaluate_expression(stmt
->range_expression
);
3274 evaluate_expression(stmt
->range_low
);
3275 evaluate_expression(stmt
->range_high
);