4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
9 * Evaluate constant expressions.
27 #include "expression.h"
29 struct symbol
*current_fn
;
31 static struct symbol
*degenerate(struct expression
*expr
);
32 static struct symbol
*evaluate_symbol(struct symbol
*sym
);
34 static struct symbol
*evaluate_symbol_expression(struct expression
*expr
)
36 struct expression
*addr
;
37 struct symbol
*sym
= expr
->symbol
;
38 struct symbol
*base_type
;
41 expression_error(expr
, "undefined identifier '%s'", show_ident(expr
->symbol_name
));
45 examine_symbol_type(sym
);
47 base_type
= get_base_type(sym
);
49 expression_error(expr
, "identifier '%s' has no type", show_ident(expr
->symbol_name
));
53 addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
55 addr
->symbol_name
= expr
->symbol_name
;
56 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
57 expr
->type
= EXPR_PREOP
;
61 /* The type of a symbol is the symbol itself! */
66 static struct symbol
*evaluate_string(struct expression
*expr
)
68 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
69 struct symbol
*array
= alloc_symbol(expr
->pos
, SYM_ARRAY
);
70 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
71 struct expression
*initstr
= alloc_expression(expr
->pos
, EXPR_STRING
);
72 unsigned int length
= expr
->string
->length
;
74 sym
->array_size
= alloc_const_expression(expr
->pos
, length
);
75 sym
->bit_size
= bits_in_char
* length
;
76 sym
->ctype
.alignment
= 1;
78 sym
->ctype
.modifiers
= MOD_STATIC
;
79 sym
->ctype
.base_type
= array
;
80 sym
->initializer
= initstr
;
83 initstr
->string
= expr
->string
;
85 array
->array_size
= sym
->array_size
;
86 array
->bit_size
= bits_in_char
* length
;
87 array
->ctype
.alignment
= 1;
88 array
->ctype
.modifiers
= MOD_STATIC
;
89 array
->ctype
.base_type
= &char_ctype
;
92 addr
->ctype
= &lazy_ptr_ctype
;
94 expr
->type
= EXPR_PREOP
;
101 /* type has come from classify_type and is an integer type */
102 static inline struct symbol
*integer_promotion(struct symbol
*type
)
104 struct symbol
*orig_type
= type
;
105 unsigned long mod
= type
->ctype
.modifiers
;
106 int width
= type
->bit_size
;
109 * Bitfields always promote to the base type,
110 * even if the bitfield might be bigger than
113 if (type
->type
== SYM_BITFIELD
) {
114 type
= type
->ctype
.base_type
;
117 mod
= type
->ctype
.modifiers
;
118 if (width
< bits_in_int
)
121 /* If char/short has as many bits as int, it still gets "promoted" */
122 if (mod
& (MOD_CHAR
| MOD_SHORT
)) {
123 if (mod
& MOD_UNSIGNED
)
131 * integer part of usual arithmetic conversions:
132 * integer promotions are applied
133 * if left and right are identical, we are done
134 * if signedness is the same, convert one with lower rank
135 * unless unsigned argument has rank lower than signed one, convert the
137 * if signed argument is bigger than unsigned one, convert the unsigned.
138 * otherwise, convert signed.
140 * Leaving aside the integer promotions, that is equivalent to
141 * if identical, don't convert
142 * if left is bigger than right, convert right
143 * if right is bigger than left, convert right
144 * otherwise, if signedness is the same, convert one with lower rank
145 * otherwise convert the signed one.
147 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
149 unsigned long lmod
, rmod
;
151 left
= integer_promotion(left
);
152 right
= integer_promotion(right
);
157 if (left
->bit_size
> right
->bit_size
)
160 if (right
->bit_size
> left
->bit_size
)
163 lmod
= left
->ctype
.modifiers
;
164 rmod
= right
->ctype
.modifiers
;
165 if ((lmod
^ rmod
) & MOD_UNSIGNED
) {
166 if (lmod
& MOD_UNSIGNED
)
168 } else if ((lmod
& ~rmod
) & (MOD_LONG
| MOD_LONGLONG
))
176 static int same_cast_type(struct symbol
*orig
, struct symbol
*new)
178 return orig
->bit_size
== new->bit_size
&& orig
->bit_offset
== new->bit_offset
;
181 static struct symbol
*base_type(struct symbol
*node
, unsigned long *modp
, unsigned long *asp
)
183 unsigned long mod
, as
;
187 mod
|= node
->ctype
.modifiers
;
188 as
|= node
->ctype
.as
;
189 if (node
->type
== SYM_NODE
) {
190 node
= node
->ctype
.base_type
;
195 *modp
= mod
& ~MOD_IGNORE
;
200 static int is_same_type(struct expression
*expr
, struct symbol
*new)
202 struct symbol
*old
= expr
->ctype
;
203 unsigned long oldmod
, newmod
, oldas
, newas
;
205 old
= base_type(old
, &oldmod
, &oldas
);
206 new = base_type(new, &newmod
, &newas
);
208 /* Same base type, same address space? */
209 if (old
== new && oldas
== newas
) {
210 unsigned long difmod
;
212 /* Check the modifier bits. */
213 difmod
= (oldmod
^ newmod
) & ~MOD_NOCAST
;
215 /* Exact same type? */
220 * Not the same type, but differs only in "const".
221 * Don't warn about MOD_NOCAST.
223 if (difmod
== MOD_CONST
)
226 if ((oldmod
| newmod
) & MOD_NOCAST
) {
227 const char *tofrom
= "to/from";
228 if (!(newmod
& MOD_NOCAST
))
230 if (!(oldmod
& MOD_NOCAST
))
232 warning(expr
->pos
, "implicit cast %s nocast type", tofrom
);
238 warn_for_different_enum_types (struct position pos
,
239 struct symbol
*typea
,
240 struct symbol
*typeb
)
244 if (typea
->type
== SYM_NODE
)
245 typea
= typea
->ctype
.base_type
;
246 if (typeb
->type
== SYM_NODE
)
247 typeb
= typeb
->ctype
.base_type
;
252 if (typea
->type
== SYM_ENUM
&& typeb
->type
== SYM_ENUM
) {
253 warning(pos
, "mixing different enum types");
254 info(pos
, " %s versus", show_typename(typea
));
255 info(pos
, " %s", show_typename(typeb
));
260 * This gets called for implicit casts in assignments and
261 * integer promotion. We often want to try to move the
262 * cast down, because the ops involved may have been
263 * implicitly cast up, and we can get rid of the casts
266 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
268 struct expression
*expr
;
270 warn_for_different_enum_types (old
->pos
, old
->ctype
, type
);
272 if (old
->ctype
!= &null_ctype
&& is_same_type(old
, type
))
276 * See if we can simplify the op. Move the cast down.
280 if (old
->ctype
->bit_size
< type
->bit_size
)
282 if (old
->op
== '~') {
284 old
->unop
= cast_to(old
->unop
, type
);
289 case EXPR_IMPLIED_CAST
:
290 warn_for_different_enum_types(old
->pos
, old
->ctype
, type
);
292 if (old
->ctype
->bit_size
>= type
->bit_size
) {
293 struct expression
*orig
= old
->cast_expression
;
294 if (same_cast_type(orig
->ctype
, type
))
296 if (old
->ctype
->bit_offset
== type
->bit_offset
) {
298 old
->cast_type
= type
;
308 expr
= alloc_expression(old
->pos
, EXPR_IMPLIED_CAST
);
309 expr
->flags
= old
->flags
;
311 expr
->cast_type
= type
;
312 expr
->cast_expression
= old
;
316 static int is_type_type(struct symbol
*type
)
318 return (type
->ctype
.modifiers
& MOD_TYPE
) != 0;
321 int is_ptr_type(struct symbol
*type
)
323 if (type
->type
== SYM_NODE
)
324 type
= type
->ctype
.base_type
;
325 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
328 static inline int is_float_type(struct symbol
*type
)
330 if (type
->type
== SYM_NODE
)
331 type
= type
->ctype
.base_type
;
332 return type
->ctype
.base_type
== &fp_type
;
335 static inline int is_byte_type(struct symbol
*type
)
337 return type
->bit_size
== bits_in_char
&& type
->type
!= SYM_BITFIELD
;
350 static inline int classify_type(struct symbol
*type
, struct symbol
**base
)
352 static int type_class
[SYM_BAD
+ 1] = {
353 [SYM_PTR
] = TYPE_PTR
,
355 [SYM_ARRAY
] = TYPE_PTR
| TYPE_COMPOUND
,
356 [SYM_STRUCT
] = TYPE_COMPOUND
,
357 [SYM_UNION
] = TYPE_COMPOUND
,
358 [SYM_BITFIELD
] = TYPE_NUM
| TYPE_BITFIELD
,
359 [SYM_RESTRICT
] = TYPE_NUM
| TYPE_RESTRICT
,
360 [SYM_FOULED
] = TYPE_NUM
| TYPE_RESTRICT
| TYPE_FOULED
,
362 if (type
->type
== SYM_NODE
)
363 type
= type
->ctype
.base_type
;
364 if (type
->type
== SYM_ENUM
)
365 type
= type
->ctype
.base_type
;
367 if (type
->type
== SYM_BASETYPE
) {
368 if (type
->ctype
.base_type
== &int_type
)
370 if (type
->ctype
.base_type
== &fp_type
)
371 return TYPE_NUM
| TYPE_FLOAT
;
373 return type_class
[type
->type
];
376 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
378 static inline int is_string_type(struct symbol
*type
)
380 if (type
->type
== SYM_NODE
)
381 type
= type
->ctype
.base_type
;
382 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
385 static struct symbol
*bad_expr_type(struct expression
*expr
)
387 sparse_error(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
388 switch (expr
->type
) {
391 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
392 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
396 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
403 return expr
->ctype
= &bad_ctype
;
406 static int restricted_value(struct expression
*v
, struct symbol
*type
)
408 if (v
->type
!= EXPR_VALUE
)
415 static int restricted_binop(int op
, struct symbol
*type
)
420 case SPECIAL_AND_ASSIGN
:
421 case SPECIAL_OR_ASSIGN
:
422 case SPECIAL_XOR_ASSIGN
:
423 return 1; /* unfoul */
427 return 2; /* keep fouled */
429 case SPECIAL_NOTEQUAL
:
430 return 3; /* warn if fouled */
436 static int restricted_unop(int op
, struct symbol
**type
)
439 if ((*type
)->bit_size
< bits_in_int
)
440 *type
= befoul(*type
);
447 static struct symbol
*restricted_binop_type(int op
,
448 struct expression
*left
,
449 struct expression
*right
,
450 int lclass
, int rclass
,
451 struct symbol
*ltype
,
452 struct symbol
*rtype
)
454 struct symbol
*ctype
= NULL
;
455 if (lclass
& TYPE_RESTRICT
) {
456 if (rclass
& TYPE_RESTRICT
) {
457 if (ltype
== rtype
) {
459 } else if (lclass
& TYPE_FOULED
) {
460 if (ltype
->ctype
.base_type
== rtype
)
462 } else if (rclass
& TYPE_FOULED
) {
463 if (rtype
->ctype
.base_type
== ltype
)
467 if (!restricted_value(right
, ltype
))
470 } else if (!restricted_value(left
, rtype
))
474 switch (restricted_binop(op
, ctype
)) {
476 if ((lclass
^ rclass
) & TYPE_FOULED
)
477 ctype
= ctype
->ctype
.base_type
;
480 if (!(lclass
& rclass
& TYPE_FOULED
))
492 static inline void unrestrict(struct expression
*expr
,
493 int class, struct symbol
**ctype
)
495 if (class & TYPE_RESTRICT
) {
496 warning(expr
->pos
, "restricted degrades to integer");
497 if (class & TYPE_FOULED
) /* unfoul it first */
498 *ctype
= (*ctype
)->ctype
.base_type
;
499 *ctype
= (*ctype
)->ctype
.base_type
; /* get to arithmetic type */
503 static struct symbol
*usual_conversions(int op
,
504 struct expression
*left
,
505 struct expression
*right
,
506 int lclass
, int rclass
,
507 struct symbol
*ltype
,
508 struct symbol
*rtype
)
510 struct symbol
*ctype
;
512 warn_for_different_enum_types(right
->pos
, left
->ctype
, right
->ctype
);
514 if ((lclass
| rclass
) & TYPE_RESTRICT
)
518 if (!(lclass
& TYPE_FLOAT
)) {
519 if (!(rclass
& TYPE_FLOAT
))
520 return bigger_int_type(ltype
, rtype
);
523 } else if (rclass
& TYPE_FLOAT
) {
524 unsigned long lmod
= ltype
->ctype
.modifiers
;
525 unsigned long rmod
= rtype
->ctype
.modifiers
;
526 if (rmod
& ~lmod
& (MOD_LONG
| MOD_LONGLONG
))
534 ctype
= restricted_binop_type(op
, left
, right
,
535 lclass
, rclass
, ltype
, rtype
);
539 unrestrict(left
, lclass
, <ype
);
540 unrestrict(right
, rclass
, &rtype
);
545 static inline int lvalue_expression(struct expression
*expr
)
547 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
550 static int ptr_object_size(struct symbol
*ptr_type
)
552 if (ptr_type
->type
== SYM_NODE
)
553 ptr_type
= ptr_type
->ctype
.base_type
;
554 if (ptr_type
->type
== SYM_PTR
)
555 ptr_type
= get_base_type(ptr_type
);
556 return ptr_type
->bit_size
;
559 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*ctype
, struct symbol
*itype
)
561 struct expression
*index
= expr
->right
;
565 if (ctype
== &null_ctype
)
568 examine_symbol_type(ctype
);
570 if (!ctype
->ctype
.base_type
) {
571 expression_error(expr
, "missing type information");
575 /* Get the size of whatever the pointer points to */
576 bit_size
= ptr_object_size(ctype
);
577 multiply
= bit_size
>> 3;
581 if (multiply
== 1 && itype
->bit_size
>= bits_in_pointer
)
584 if (index
->type
== EXPR_VALUE
) {
585 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
586 unsigned long long v
= index
->value
, mask
;
587 mask
= 1ULL << (itype
->bit_size
- 1);
593 mask
= 1ULL << (bits_in_pointer
- 1);
594 v
&= mask
| (mask
- 1);
596 val
->ctype
= ssize_t_ctype
;
601 if (itype
->bit_size
< bits_in_pointer
)
602 index
= cast_to(index
, ssize_t_ctype
);
605 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
606 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
608 val
->ctype
= ssize_t_ctype
;
609 val
->value
= multiply
;
612 mul
->ctype
= ssize_t_ctype
;
622 static void examine_fn_arguments(struct symbol
*fn
);
624 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
625 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
628 unsigned long mod1
, mod2
, diff
;
629 unsigned long as1
, as2
;
631 struct symbol
*base1
, *base2
;
633 if (target
== source
)
635 if (!target
|| !source
)
636 return "different types";
638 * Peel of per-node information.
639 * FIXME! Check alignment and context too here!
641 mod1
= target
->ctype
.modifiers
;
642 as1
= target
->ctype
.as
;
643 mod2
= source
->ctype
.modifiers
;
644 as2
= source
->ctype
.as
;
645 if (target
->type
== SYM_NODE
) {
646 target
= target
->ctype
.base_type
;
649 if (target
->type
== SYM_PTR
) {
653 mod1
|= target
->ctype
.modifiers
;
654 as1
|= target
->ctype
.as
;
656 if (source
->type
== SYM_NODE
) {
657 source
= source
->ctype
.base_type
;
660 if (source
->type
== SYM_PTR
) {
664 mod2
|= source
->ctype
.modifiers
;
665 as2
|= source
->ctype
.as
;
667 if (target
->type
== SYM_ENUM
) {
668 target
= target
->ctype
.base_type
;
672 if (source
->type
== SYM_ENUM
) {
673 source
= source
->ctype
.base_type
;
678 if (target
== source
)
680 if (!target
|| !source
)
681 return "different types";
683 type1
= target
->type
;
684 base1
= target
->ctype
.base_type
;
686 type2
= source
->type
;
687 base2
= source
->ctype
.base_type
;
690 * Pointers to functions compare as the function itself
692 if (type1
== SYM_PTR
&& base1
)
693 base1
= examine_symbol_type(base1
);
695 if (type2
== SYM_PTR
&& base2
)
696 base2
= examine_symbol_type(base2
);
698 if (type1
!= type2
|| type1
== SYM_RESTRICT
||
699 type1
== SYM_UNION
|| type1
== SYM_STRUCT
)
700 return "different base types";
702 /* Must be same address space to be comparable */
703 if (Waddress_space
&& as1
!= as2
)
704 return "different address spaces";
706 /* Ignore differences in storage types or addressability */
707 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
708 diff
&= (mod1
& ~target_mod_ignore
) | (mod2
& ~source_mod_ignore
);
711 return "different type sizes";
712 if (diff
& ~MOD_SIGNEDNESS
)
713 return "different modifiers";
715 /* Differs in signedness only.. */
718 * Warn if both are explicitly signed ("unsigned" is obviously
719 * always explicit, and since we know one of them has to be
720 * unsigned, we check if the signed one was explicit).
722 if ((mod1
| mod2
) & MOD_EXPLICITLY_SIGNED
)
723 return "different explicit signedness";
726 * "char" matches both "unsigned char" and "signed char",
727 * so if the explicit test didn't trigger, then we should
728 * not warn about a char.
730 if (!(mod1
& MOD_CHAR
))
731 return "different signedness";
735 if (type1
== SYM_FN
) {
737 struct symbol
*arg1
, *arg2
;
738 if (base1
->variadic
!= base2
->variadic
)
739 return "incompatible variadic arguments";
740 examine_fn_arguments(target
);
741 examine_fn_arguments(source
);
742 PREPARE_PTR_LIST(target
->arguments
, arg1
);
743 PREPARE_PTR_LIST(source
->arguments
, arg2
);
747 diffstr
= type_difference(arg1
, arg2
, 0, 0);
749 static char argdiff
[80];
750 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diffstr
);
759 FINISH_PTR_LIST(arg2
);
760 FINISH_PTR_LIST(arg1
);
769 static void bad_null(struct expression
*expr
)
771 if (Wnon_pointer_null
)
772 warning(expr
->pos
, "Using plain integer as NULL pointer");
776 * Ignore differences in "volatile" and "const"ness when
777 * subtracting pointers
779 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
781 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
)
783 const char *typediff
;
784 struct symbol
*ctype
;
785 struct symbol
*ltype
, *rtype
;
786 struct expression
*l
= expr
->left
;
787 struct expression
*r
= expr
->right
;
788 struct symbol
*lbase
, *rbase
;
790 classify_type(degenerate(l
), <ype
);
791 classify_type(degenerate(r
), &rtype
);
793 lbase
= get_base_type(ltype
);
794 rbase
= get_base_type(rtype
);
795 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
797 expression_error(expr
, "subtraction of different types can't work (%s)", typediff
);
800 /* Figure out the base type we point to */
801 if (ctype
->type
== SYM_NODE
)
802 ctype
= ctype
->ctype
.base_type
;
803 if (ctype
->type
== SYM_FN
) {
804 expression_error(expr
, "subtraction of functions? Share your drugs");
808 expr
->ctype
= ssize_t_ctype
;
809 if (ctype
->bit_size
> bits_in_char
) {
810 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
811 struct expression
*div
= expr
;
812 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
813 unsigned long value
= ctype
->bit_size
>> 3;
815 val
->ctype
= size_t_ctype
;
818 if (value
& (value
-1)) {
819 if (Wptr_subtraction_blows
)
820 warning(expr
->pos
, "potentially expensive pointer subtraction");
824 sub
->ctype
= ssize_t_ctype
;
833 return ssize_t_ctype
;
836 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
838 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
840 struct symbol
*ctype
;
845 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
846 warning(expr
->pos
, "assignment expression in conditional");
848 ctype
= evaluate_expression(expr
);
850 if (is_safe_type(ctype
))
851 warning(expr
->pos
, "testing a 'safe expression'");
857 static struct symbol
*evaluate_logical(struct expression
*expr
)
859 if (!evaluate_conditional(expr
->left
, 0))
861 if (!evaluate_conditional(expr
->right
, 0))
864 expr
->ctype
= &bool_ctype
;
866 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
872 static struct symbol
*evaluate_binop(struct expression
*expr
)
874 struct symbol
*ltype
, *rtype
, *ctype
;
875 int lclass
= classify_type(expr
->left
->ctype
, <ype
);
876 int rclass
= classify_type(expr
->right
->ctype
, &rtype
);
880 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
884 /* number op number */
885 if (lclass
& rclass
& TYPE_NUM
) {
886 if ((lclass
| rclass
) & TYPE_FLOAT
) {
888 case '+': case '-': case '*': case '/':
891 return bad_expr_type(expr
);
895 if (op
== SPECIAL_LEFTSHIFT
|| op
== SPECIAL_RIGHTSHIFT
) {
896 // shifts do integer promotions, but that's it.
897 unrestrict(expr
->left
, lclass
, <ype
);
898 unrestrict(expr
->right
, rclass
, &rtype
);
899 ctype
= ltype
= integer_promotion(ltype
);
900 rtype
= integer_promotion(rtype
);
902 // The rest do usual conversions
903 ltype
= usual_conversions(op
, expr
->left
, expr
->right
,
904 lclass
, rclass
, ltype
, rtype
);
905 ctype
= rtype
= ltype
;
908 expr
->left
= cast_to(expr
->left
, ltype
);
909 expr
->right
= cast_to(expr
->right
, rtype
);
914 /* pointer (+|-) integer */
915 if (lclass
& TYPE_PTR
&& is_int(rclass
) && (op
== '+' || op
== '-')) {
916 unrestrict(expr
->right
, rclass
, &rtype
);
917 return evaluate_ptr_add(expr
, degenerate(expr
->left
), rtype
);
920 /* integer + pointer */
921 if (rclass
& TYPE_PTR
&& is_int(lclass
) && op
== '+') {
922 struct expression
*index
= expr
->left
;
923 unrestrict(index
, lclass
, <ype
);
924 expr
->left
= expr
->right
;
926 return evaluate_ptr_add(expr
, degenerate(expr
->left
), ltype
);
929 /* pointer - pointer */
930 if (lclass
& rclass
& TYPE_PTR
&& expr
->op
== '-')
931 return evaluate_ptr_sub(expr
);
933 return bad_expr_type(expr
);
936 static struct symbol
*evaluate_comma(struct expression
*expr
)
938 expr
->ctype
= expr
->right
->ctype
;
939 expr
->flags
&= expr
->left
->flags
& expr
->right
->flags
;
943 static int modify_for_unsigned(int op
)
946 op
= SPECIAL_UNSIGNED_LT
;
948 op
= SPECIAL_UNSIGNED_GT
;
949 else if (op
== SPECIAL_LTE
)
950 op
= SPECIAL_UNSIGNED_LTE
;
951 else if (op
== SPECIAL_GTE
)
952 op
= SPECIAL_UNSIGNED_GTE
;
956 static inline int is_null_pointer_constant(struct expression
*e
)
958 if (e
->ctype
== &null_ctype
)
960 if (!(e
->flags
& Int_const_expr
))
962 return is_zero_constant(e
) ? 2 : 0;
965 static struct symbol
*evaluate_compare(struct expression
*expr
)
967 struct expression
*left
= expr
->left
, *right
= expr
->right
;
968 struct symbol
*ltype
, *rtype
;
969 int lclass
= classify_type(degenerate(left
), <ype
);
970 int rclass
= classify_type(degenerate(right
), &rtype
);
971 struct symbol
*ctype
;
972 const char *typediff
;
975 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
980 if (is_type_type(ltype
) && is_type_type(rtype
))
983 if (is_safe_type(left
->ctype
) || is_safe_type(right
->ctype
))
984 warning(expr
->pos
, "testing a 'safe expression'");
986 /* number on number */
987 if (lclass
& rclass
& TYPE_NUM
) {
988 ctype
= usual_conversions(expr
->op
, expr
->left
, expr
->right
,
989 lclass
, rclass
, ltype
, rtype
);
990 expr
->left
= cast_to(expr
->left
, ctype
);
991 expr
->right
= cast_to(expr
->right
, ctype
);
992 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
993 expr
->op
= modify_for_unsigned(expr
->op
);
997 /* at least one must be a pointer */
998 if (!((lclass
| rclass
) & TYPE_PTR
))
999 return bad_expr_type(expr
);
1001 /* equality comparisons can be with null pointer constants */
1002 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1003 int is_null1
= is_null_pointer_constant(left
);
1004 int is_null2
= is_null_pointer_constant(right
);
1009 if (is_null1
&& is_null2
) {
1010 int positive
= expr
->op
== SPECIAL_EQUAL
;
1011 expr
->type
= EXPR_VALUE
;
1012 expr
->value
= positive
;
1016 left
= cast_to(left
, rtype
);
1020 right
= cast_to(right
, ltype
);
1023 /* they also have special treatment for pointers to void */
1024 if (lclass
& rclass
& TYPE_PTR
) {
1025 if (get_base_type(ltype
) == &void_ctype
) {
1026 right
= cast_to(right
, ltype
);
1029 if (get_base_type(rtype
) == &void_ctype
) {
1030 left
= cast_to(left
, rtype
);
1035 /* both should be pointers */
1036 if (!(lclass
& rclass
& TYPE_PTR
))
1037 return bad_expr_type(expr
);
1039 expr
->op
= modify_for_unsigned(expr
->op
);
1040 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
1044 expression_error(expr
, "incompatible types in comparison expression (%s)", typediff
);
1048 expr
->ctype
= &bool_ctype
;
1053 * NOTE! The degenerate case of "x ? : y", where we don't
1054 * have a true case, this will possibly promote "x" to the
1055 * same type as "y", and thus _change_ the conditional
1056 * test in the expression. But since promotion is "safe"
1057 * for testing, that's OK.
1059 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
1061 struct expression
**true;
1062 struct symbol
*ctype
, *ltype
, *rtype
, *lbase
, *rbase
;
1064 const char * typediff
;
1067 if (!evaluate_conditional(expr
->conditional
, 0))
1069 if (!evaluate_expression(expr
->cond_false
))
1072 ctype
= degenerate(expr
->conditional
);
1073 rtype
= degenerate(expr
->cond_false
);
1075 true = &expr
->conditional
;
1077 if (expr
->cond_true
) {
1078 if (!evaluate_expression(expr
->cond_true
))
1080 ltype
= degenerate(expr
->cond_true
);
1081 true = &expr
->cond_true
;
1085 int flags
= expr
->conditional
->flags
& Int_const_expr
;
1086 flags
&= (*true)->flags
& expr
->cond_false
->flags
;
1091 lclass
= classify_type(ltype
, <ype
);
1092 rclass
= classify_type(rtype
, &rtype
);
1093 if (lclass
& rclass
& TYPE_NUM
) {
1094 ctype
= usual_conversions('?', *true, expr
->cond_false
,
1095 lclass
, rclass
, ltype
, rtype
);
1096 *true = cast_to(*true, ctype
);
1097 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1101 if ((lclass
| rclass
) & TYPE_PTR
) {
1102 int is_null1
= is_null_pointer_constant(*true);
1103 int is_null2
= is_null_pointer_constant(expr
->cond_false
);
1105 if (is_null1
&& is_null2
) {
1106 *true = cast_to(*true, &ptr_ctype
);
1107 expr
->cond_false
= cast_to(expr
->cond_false
, &ptr_ctype
);
1111 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1114 *true = cast_to(*true, rtype
);
1118 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1120 bad_null(expr
->cond_false
);
1121 expr
->cond_false
= cast_to(expr
->cond_false
, ltype
);
1125 if (!(lclass
& rclass
& TYPE_PTR
)) {
1126 typediff
= "different types";
1129 /* OK, it's pointer on pointer */
1130 if (ltype
->ctype
.as
!= rtype
->ctype
.as
) {
1131 typediff
= "different address spaces";
1135 /* need to be lazier here */
1136 lbase
= get_base_type(ltype
);
1137 rbase
= get_base_type(rtype
);
1138 qual
= ltype
->ctype
.modifiers
| rtype
->ctype
.modifiers
;
1139 qual
&= MOD_CONST
| MOD_VOLATILE
;
1141 if (lbase
== &void_ctype
) {
1142 /* XXX: pointers to function should warn here */
1147 if (rbase
== &void_ctype
) {
1148 /* XXX: pointers to function should warn here */
1152 /* XXX: that should be pointer to composite */
1154 typediff
= type_difference(lbase
, rbase
, MOD_IGN
, MOD_IGN
);
1160 /* void on void, struct on same struct, union on same union */
1161 if (ltype
== rtype
) {
1165 typediff
= "different base types";
1168 expression_error(expr
, "incompatible types in conditional expression (%s)", typediff
);
1172 expr
->ctype
= ctype
;
1176 if (qual
& ~ctype
->ctype
.modifiers
) {
1177 struct symbol
*sym
= alloc_symbol(ctype
->pos
, SYM_PTR
);
1179 sym
->ctype
.modifiers
|= qual
;
1182 *true = cast_to(*true, ctype
);
1183 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1187 /* FP assignments can not do modulo or bit operations */
1188 static int compatible_float_op(int op
)
1190 return op
== SPECIAL_ADD_ASSIGN
||
1191 op
== SPECIAL_SUB_ASSIGN
||
1192 op
== SPECIAL_MUL_ASSIGN
||
1193 op
== SPECIAL_DIV_ASSIGN
;
1196 static int evaluate_assign_op(struct expression
*expr
)
1198 struct symbol
*target
= expr
->left
->ctype
;
1199 struct symbol
*source
= expr
->right
->ctype
;
1200 struct symbol
*t
, *s
;
1201 int tclass
= classify_type(target
, &t
);
1202 int sclass
= classify_type(source
, &s
);
1205 if (tclass
& sclass
& TYPE_NUM
) {
1206 if (tclass
& TYPE_FLOAT
&& !compatible_float_op(op
)) {
1207 expression_error(expr
, "invalid assignment");
1210 if (tclass
& TYPE_RESTRICT
) {
1211 if (!restricted_binop(op
, t
)) {
1212 expression_error(expr
, "bad restricted assignment");
1215 /* allowed assignments unfoul */
1216 if (sclass
& TYPE_FOULED
&& s
->ctype
.base_type
== t
)
1218 if (!restricted_value(expr
->right
, t
))
1220 } else if (!(sclass
& TYPE_RESTRICT
))
1222 /* source and target would better be identical restricted */
1225 warning(expr
->pos
, "invalid restricted assignment");
1226 expr
->right
= cast_to(expr
->right
, target
);
1229 if (tclass
& TYPE_PTR
&& is_int(sclass
)) {
1230 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1231 unrestrict(expr
->right
, sclass
, &s
);
1232 evaluate_ptr_add(expr
, target
, s
);
1235 expression_error(expr
, "invalid pointer assignment");
1239 expression_error(expr
, "invalid assignment");
1243 expr
->right
= cast_to(expr
->right
, target
);
1247 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1248 struct expression
**rp
, const char *where
)
1250 const char *typediff
;
1251 struct symbol
*source
= degenerate(*rp
);
1252 struct symbol
*t
, *s
;
1253 int tclass
= classify_type(target
, &t
);
1254 int sclass
= classify_type(source
, &s
);
1256 if (tclass
& sclass
& TYPE_NUM
) {
1257 if (tclass
& TYPE_RESTRICT
) {
1258 /* allowed assignments unfoul */
1259 if (sclass
& TYPE_FOULED
&& s
->ctype
.base_type
== t
)
1261 if (!restricted_value(*rp
, target
))
1265 } else if (!(sclass
& TYPE_RESTRICT
))
1267 typediff
= "different base types";
1271 if (tclass
== TYPE_PTR
) {
1272 unsigned long mod1
, mod2
;
1273 struct symbol
*b1
, *b2
;
1274 // NULL pointer is always OK
1275 int is_null
= is_null_pointer_constant(*rp
);
1281 if (!(sclass
& TYPE_PTR
)) {
1282 typediff
= "different base types";
1285 /* we should be more lazy here */
1286 mod1
= t
->ctype
.modifiers
;
1287 mod2
= s
->ctype
.modifiers
;
1288 b1
= get_base_type(t
);
1289 b2
= get_base_type(s
);
1290 if (b1
== &void_ctype
|| b2
== &void_ctype
) {
1292 * assignments to/from void * are OK, provided that
1293 * we do not remove qualifiers from pointed to [C]
1294 * or mix address spaces [sparse].
1296 if (t
->ctype
.as
!= s
->ctype
.as
) {
1297 typediff
= "different address spaces";
1300 if (mod2
& ~mod1
& MOD_IGN
) {
1301 typediff
= "different modifiers";
1306 /* It's OK if the target is more volatile or const than the source */
1307 typediff
= type_difference(target
, source
,
1308 MOD_VOLATILE
| MOD_CONST
, 0);
1314 if ((tclass
& TYPE_COMPOUND
) && s
== t
)
1317 if (tclass
& TYPE_NUM
) {
1318 /* XXX: need to turn into comparison with NULL */
1319 if (t
== &bool_ctype
&& (sclass
& TYPE_PTR
))
1321 typediff
= "different base types";
1324 typediff
= "invalid types";
1327 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1328 info(expr
->pos
, " expected %s", show_typename(target
));
1329 info(expr
->pos
, " got %s", show_typename(source
));
1330 *rp
= cast_to(*rp
, target
);
1333 *rp
= cast_to(*rp
, target
);
1337 static void mark_assigned(struct expression
*expr
)
1343 switch (expr
->type
) {
1348 if (sym
->type
!= SYM_NODE
)
1350 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1354 mark_assigned(expr
->left
);
1355 mark_assigned(expr
->right
);
1358 case EXPR_FORCE_CAST
:
1359 mark_assigned(expr
->cast_expression
);
1362 mark_assigned(expr
->base
);
1370 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1372 if (type
->ctype
.modifiers
& MOD_CONST
)
1373 expression_error(left
, "assignment to const expression");
1375 /* We know left is an lvalue, so it's a "preop-*" */
1376 mark_assigned(left
->unop
);
1379 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1381 struct expression
*left
= expr
->left
;
1382 struct expression
*where
= expr
;
1383 struct symbol
*ltype
;
1385 if (!lvalue_expression(left
)) {
1386 expression_error(expr
, "not an lvalue");
1390 ltype
= left
->ctype
;
1392 if (expr
->op
!= '=') {
1393 if (!evaluate_assign_op(expr
))
1396 if (!compatible_assignment_types(where
, ltype
, &expr
->right
, "assignment"))
1400 evaluate_assign_to(left
, ltype
);
1402 expr
->ctype
= ltype
;
1406 static void examine_fn_arguments(struct symbol
*fn
)
1410 FOR_EACH_PTR(fn
->arguments
, s
) {
1411 struct symbol
*arg
= evaluate_symbol(s
);
1412 /* Array/function arguments silently degenerate into pointers */
1418 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1419 if (arg
->type
== SYM_ARRAY
)
1420 ptr
->ctype
= arg
->ctype
;
1422 ptr
->ctype
.base_type
= arg
;
1423 ptr
->ctype
.as
|= s
->ctype
.as
;
1424 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
& MOD_PTRINHERIT
;
1426 s
->ctype
.base_type
= ptr
;
1428 s
->ctype
.modifiers
&= ~MOD_PTRINHERIT
;
1431 examine_symbol_type(s
);
1438 } END_FOR_EACH_PTR(s
);
1441 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1443 /* Take the modifiers of the pointer, and apply them to the member */
1444 mod
|= sym
->ctype
.modifiers
;
1445 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1446 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1448 newsym
->ctype
.as
= as
;
1449 newsym
->ctype
.modifiers
= mod
;
1455 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1457 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1458 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1460 node
->ctype
.base_type
= ptr
;
1461 ptr
->bit_size
= bits_in_pointer
;
1462 ptr
->ctype
.alignment
= pointer_alignment
;
1464 node
->bit_size
= bits_in_pointer
;
1465 node
->ctype
.alignment
= pointer_alignment
;
1468 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1469 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1470 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1472 if (sym
->type
== SYM_NODE
) {
1473 ptr
->ctype
.as
|= sym
->ctype
.as
;
1474 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1475 sym
= sym
->ctype
.base_type
;
1477 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1478 ptr
->ctype
.as
|= sym
->ctype
.as
;
1479 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1480 sym
= sym
->ctype
.base_type
;
1482 ptr
->ctype
.base_type
= sym
;
1487 /* Arrays degenerate into pointers on pointer arithmetic */
1488 static struct symbol
*degenerate(struct expression
*expr
)
1490 struct symbol
*ctype
, *base
;
1494 ctype
= expr
->ctype
;
1497 base
= examine_symbol_type(ctype
);
1498 if (ctype
->type
== SYM_NODE
)
1499 base
= ctype
->ctype
.base_type
;
1501 * Arrays degenerate into pointers to the entries, while
1502 * functions degenerate into pointers to themselves.
1503 * If array was part of non-lvalue compound, we create a copy
1504 * of that compound first and then act as if we were dealing with
1505 * the corresponding field in there.
1507 switch (base
->type
) {
1509 if (expr
->type
== EXPR_SLICE
) {
1510 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1511 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1513 a
->ctype
.base_type
= expr
->base
->ctype
;
1514 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1515 a
->array_size
= expr
->base
->ctype
->array_size
;
1517 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1519 e0
->ctype
= &lazy_ptr_ctype
;
1521 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1524 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1526 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1528 e2
->right
= expr
->base
;
1530 e2
->ctype
= expr
->base
->ctype
;
1532 if (expr
->r_bitpos
) {
1533 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1536 e3
->right
= alloc_const_expression(expr
->pos
,
1537 expr
->r_bitpos
>> 3);
1538 e3
->ctype
= &lazy_ptr_ctype
;
1543 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1546 e4
->ctype
= &lazy_ptr_ctype
;
1549 expr
->type
= EXPR_PREOP
;
1553 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1554 expression_error(expr
, "strange non-value function or array");
1557 *expr
= *expr
->unop
;
1558 ctype
= create_pointer(expr
, ctype
, 1);
1559 expr
->ctype
= ctype
;
1566 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1568 struct expression
*op
= expr
->unop
;
1569 struct symbol
*ctype
;
1571 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1572 expression_error(expr
, "not addressable");
1579 if (expr
->type
== EXPR_SYMBOL
) {
1580 struct symbol
*sym
= expr
->symbol
;
1581 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1585 * symbol expression evaluation is lazy about the type
1586 * of the sub-expression, so we may have to generate
1587 * the type here if so..
1589 if (expr
->ctype
== &lazy_ptr_ctype
) {
1590 ctype
= create_pointer(expr
, ctype
, 0);
1591 expr
->ctype
= ctype
;
1597 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1599 struct expression
*op
= expr
->unop
;
1600 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1602 /* Simplify: *&(expr) => (expr) */
1603 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1609 /* Dereferencing a node drops all the node information. */
1610 if (ctype
->type
== SYM_NODE
)
1611 ctype
= ctype
->ctype
.base_type
;
1613 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1614 target
= ctype
->ctype
.base_type
;
1616 switch (ctype
->type
) {
1618 expression_error(expr
, "cannot dereference this type");
1621 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1622 merge_type(node
, ctype
);
1626 if (!lvalue_expression(op
)) {
1627 expression_error(op
, "non-lvalue array??");
1631 /* Do the implied "addressof" on the array */
1635 * When an array is dereferenced, we need to pick
1636 * up the attributes of the original node too..
1638 merge_type(node
, op
->ctype
);
1639 merge_type(node
, ctype
);
1643 node
->bit_size
= target
->bit_size
;
1644 node
->array_size
= target
->array_size
;
1651 * Unary post-ops: x++ and x--
1653 static struct symbol
*evaluate_postop(struct expression
*expr
)
1655 struct expression
*op
= expr
->unop
;
1656 struct symbol
*ctype
= op
->ctype
;
1658 if (!lvalue_expression(expr
->unop
)) {
1659 expression_error(expr
, "need lvalue expression for ++/--");
1662 if (is_restricted_type(ctype
) && restricted_unop(expr
->op
, &ctype
)) {
1663 expression_error(expr
, "bad operation on restricted");
1665 } else if (is_fouled_type(ctype
) && restricted_unop(expr
->op
, &ctype
)) {
1666 expression_error(expr
, "bad operation on restricted");
1670 evaluate_assign_to(op
, ctype
);
1672 expr
->ctype
= ctype
;
1674 if (is_ptr_type(ctype
))
1675 expr
->op_value
= ptr_object_size(ctype
) >> 3;
1680 static struct symbol
*evaluate_sign(struct expression
*expr
)
1682 struct symbol
*ctype
= expr
->unop
->ctype
;
1683 int class = classify_type(ctype
, &ctype
);
1684 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1686 /* should be an arithmetic type */
1687 if (!(class & TYPE_NUM
))
1688 return bad_expr_type(expr
);
1689 if (!(class & (TYPE_FLOAT
|TYPE_RESTRICT
))) {
1690 struct symbol
*rtype
= integer_promotion(ctype
);
1691 expr
->unop
= cast_to(expr
->unop
, rtype
);
1693 } else if ((class & TYPE_FLOAT
) && expr
->op
!= '~') {
1694 /* no conversions needed */
1695 } else if ((class & TYPE_RESTRICT
) && !restricted_unop(expr
->op
, &ctype
)) {
1696 /* no conversions needed */
1698 return bad_expr_type(expr
);
1700 if (expr
->op
== '+')
1701 *expr
= *expr
->unop
;
1702 expr
->ctype
= ctype
;
1706 static struct symbol
*evaluate_preop(struct expression
*expr
)
1708 struct symbol
*ctype
= expr
->unop
->ctype
;
1712 *expr
= *expr
->unop
;
1718 return evaluate_sign(expr
);
1721 return evaluate_dereference(expr
);
1724 return evaluate_addressof(expr
);
1726 case SPECIAL_INCREMENT
:
1727 case SPECIAL_DECREMENT
:
1729 * From a type evaluation standpoint the preops are
1730 * the same as the postops
1732 return evaluate_postop(expr
);
1735 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1737 if (is_safe_type(ctype
))
1738 warning(expr
->pos
, "testing a 'safe expression'");
1739 if (is_float_type(ctype
)) {
1740 struct expression
*arg
= expr
->unop
;
1741 expr
->type
= EXPR_BINOP
;
1742 expr
->op
= SPECIAL_EQUAL
;
1744 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1745 expr
->right
->ctype
= ctype
;
1746 expr
->right
->fvalue
= 0;
1747 } else if (is_fouled_type(ctype
)) {
1748 warning(expr
->pos
, "restricted degrades to integer");
1750 ctype
= &bool_ctype
;
1756 expr
->ctype
= ctype
;
1760 static struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1762 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1763 struct ptr_list
*list
= head
;
1769 for (i
= 0; i
< list
->nr
; i
++) {
1770 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1772 if (sym
->ident
!= ident
)
1774 *offset
= sym
->offset
;
1777 struct symbol
*ctype
= sym
->ctype
.base_type
;
1781 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1783 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1786 *offset
+= sym
->offset
;
1790 } while ((list
= list
->next
) != head
);
1794 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1796 struct expression
*add
;
1799 * Create a new add-expression
1801 * NOTE! Even if we just add zero, we need a new node
1802 * for the member pointer, since it has a different
1803 * type than the original pointer. We could make that
1804 * be just a cast, but the fact is, a node is a node,
1805 * so we might as well just do the "add zero" here.
1807 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1810 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1811 add
->right
->ctype
= &int_ctype
;
1812 add
->right
->value
= offset
;
1815 * The ctype of the pointer will be lazily evaluated if
1816 * we ever take the address of this member dereference..
1818 add
->ctype
= &lazy_ptr_ctype
;
1822 /* structure/union dereference */
1823 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1826 struct symbol
*ctype
, *member
;
1827 struct expression
*deref
= expr
->deref
, *add
;
1828 struct ident
*ident
= expr
->member
;
1832 if (!evaluate_expression(deref
))
1835 expression_error(expr
, "bad member name");
1839 ctype
= deref
->ctype
;
1840 address_space
= ctype
->ctype
.as
;
1841 mod
= ctype
->ctype
.modifiers
;
1842 if (ctype
->type
== SYM_NODE
) {
1843 ctype
= ctype
->ctype
.base_type
;
1844 address_space
|= ctype
->ctype
.as
;
1845 mod
|= ctype
->ctype
.modifiers
;
1847 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1848 expression_error(expr
, "expected structure or union");
1851 examine_symbol_type(ctype
);
1853 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1855 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1856 const char *name
= "<unnamed>";
1859 name
= ctype
->ident
->name
;
1860 namelen
= ctype
->ident
->len
;
1862 if (ctype
->symbol_list
)
1863 expression_error(expr
, "no member '%s' in %s %.*s",
1864 show_ident(ident
), type
, namelen
, name
);
1866 expression_error(expr
, "using member '%s' in "
1867 "incomplete %s %.*s", show_ident(ident
),
1868 type
, namelen
, name
);
1873 * The member needs to take on the address space and modifiers of
1874 * the "parent" type.
1876 member
= convert_to_as_mod(member
, address_space
, mod
);
1877 ctype
= get_base_type(member
);
1879 if (!lvalue_expression(deref
)) {
1880 if (deref
->type
!= EXPR_SLICE
) {
1884 expr
->base
= deref
->base
;
1885 expr
->r_bitpos
= deref
->r_bitpos
;
1887 expr
->r_bitpos
+= offset
<< 3;
1888 expr
->type
= EXPR_SLICE
;
1889 expr
->r_nrbits
= member
->bit_size
;
1890 expr
->r_bitpos
+= member
->bit_offset
;
1891 expr
->ctype
= member
;
1895 deref
= deref
->unop
;
1896 expr
->deref
= deref
;
1898 add
= evaluate_offset(deref
, offset
);
1899 expr
->type
= EXPR_PREOP
;
1903 expr
->ctype
= member
;
1907 static int is_promoted(struct expression
*expr
)
1910 switch (expr
->type
) {
1913 case EXPR_CONDITIONAL
:
1937 static struct symbol
*evaluate_cast(struct expression
*);
1939 static struct symbol
*evaluate_type_information(struct expression
*expr
)
1941 struct symbol
*sym
= expr
->cast_type
;
1943 sym
= evaluate_expression(expr
->cast_expression
);
1947 * Expressions of restricted types will possibly get
1948 * promoted - check that here
1950 if (is_restricted_type(sym
)) {
1951 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
1953 } else if (is_fouled_type(sym
)) {
1957 examine_symbol_type(sym
);
1958 if (is_bitfield_type(sym
)) {
1959 expression_error(expr
, "trying to examine bitfield type");
1965 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1967 struct symbol
*type
;
1970 type
= evaluate_type_information(expr
);
1974 size
= type
->bit_size
;
1975 if ((size
< 0) || (size
& 7))
1976 expression_error(expr
, "cannot size expression");
1977 expr
->type
= EXPR_VALUE
;
1978 expr
->value
= size
>> 3;
1980 expr
->ctype
= size_t_ctype
;
1981 return size_t_ctype
;
1984 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
1986 struct symbol
*type
;
1989 type
= evaluate_type_information(expr
);
1993 if (type
->type
== SYM_NODE
)
1994 type
= type
->ctype
.base_type
;
1997 switch (type
->type
) {
2001 type
= get_base_type(type
);
2005 expression_error(expr
, "expected pointer expression");
2008 size
= type
->bit_size
;
2011 expr
->type
= EXPR_VALUE
;
2012 expr
->value
= size
>> 3;
2014 expr
->ctype
= size_t_ctype
;
2015 return size_t_ctype
;
2018 static struct symbol
*evaluate_alignof(struct expression
*expr
)
2020 struct symbol
*type
;
2022 type
= evaluate_type_information(expr
);
2026 expr
->type
= EXPR_VALUE
;
2027 expr
->value
= type
->ctype
.alignment
;
2029 expr
->ctype
= size_t_ctype
;
2030 return size_t_ctype
;
2033 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
2035 struct expression
*expr
;
2036 struct symbol_list
*argument_types
= fn
->arguments
;
2037 struct symbol
*argtype
;
2040 PREPARE_PTR_LIST(argument_types
, argtype
);
2041 FOR_EACH_PTR (head
, expr
) {
2042 struct expression
**p
= THIS_ADDRESS(expr
);
2043 struct symbol
*ctype
, *target
;
2044 ctype
= evaluate_expression(expr
);
2051 struct symbol
*type
;
2052 int class = classify_type(ctype
, &type
);
2053 if (is_int(class)) {
2054 *p
= cast_to(expr
, integer_promotion(type
));
2055 } else if (class & TYPE_FLOAT
) {
2056 unsigned long mod
= type
->ctype
.modifiers
;
2057 if (!(mod
& (MOD_LONG
|MOD_LONGLONG
)))
2058 *p
= cast_to(expr
, &double_ctype
);
2059 } else if (class & TYPE_PTR
) {
2060 if (expr
->ctype
== &null_ctype
)
2061 *p
= cast_to(expr
, &ptr_ctype
);
2066 static char where
[30];
2067 examine_symbol_type(target
);
2068 sprintf(where
, "argument %d", i
);
2069 compatible_assignment_types(expr
, target
, p
, where
);
2073 NEXT_PTR_LIST(argtype
);
2074 } END_FOR_EACH_PTR(expr
);
2075 FINISH_PTR_LIST(argtype
);
2079 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
2083 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
2084 if (sym
->ident
== ident
)
2086 } END_FOR_EACH_PTR(sym
);
2090 static void convert_index(struct expression
*e
)
2092 struct expression
*child
= e
->idx_expression
;
2093 unsigned from
= e
->idx_from
;
2094 unsigned to
= e
->idx_to
+ 1;
2096 e
->init_offset
= from
* (e
->ctype
->bit_size
>>3);
2097 e
->init_nr
= to
- from
;
2098 e
->init_expr
= child
;
2101 static void convert_ident(struct expression
*e
)
2103 struct expression
*child
= e
->ident_expression
;
2104 struct symbol
*sym
= e
->field
;
2106 e
->init_offset
= sym
->offset
;
2108 e
->init_expr
= child
;
2111 static void convert_designators(struct expression
*e
)
2114 if (e
->type
== EXPR_INDEX
)
2116 else if (e
->type
== EXPR_IDENTIFIER
)
2124 static void excess(struct expression
*e
, const char *s
)
2126 warning(e
->pos
, "excessive elements in %s initializer", s
);
2130 * implicit designator for the first element
2132 static struct expression
*first_subobject(struct symbol
*ctype
, int class,
2133 struct expression
**v
)
2135 struct expression
*e
= *v
, *new;
2137 if (ctype
->type
== SYM_NODE
)
2138 ctype
= ctype
->ctype
.base_type
;
2140 if (class & TYPE_PTR
) { /* array */
2141 if (!ctype
->bit_size
)
2143 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2144 new->idx_expression
= e
;
2145 new->ctype
= ctype
->ctype
.base_type
;
2147 struct symbol
*field
, *p
;
2148 PREPARE_PTR_LIST(ctype
->symbol_list
, p
);
2149 while (p
&& !p
->ident
&& is_bitfield_type(p
))
2155 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2156 new->ident_expression
= e
;
2157 new->field
= new->ctype
= field
;
2164 * sanity-check explicit designators; return the innermost one or NULL
2165 * in case of error. Assign types.
2167 static struct expression
*check_designators(struct expression
*e
,
2168 struct symbol
*ctype
)
2170 struct expression
*last
= NULL
;
2173 if (ctype
->type
== SYM_NODE
)
2174 ctype
= ctype
->ctype
.base_type
;
2175 if (e
->type
== EXPR_INDEX
) {
2176 struct symbol
*type
;
2177 if (ctype
->type
!= SYM_ARRAY
) {
2178 err
= "array index in non-array";
2181 type
= ctype
->ctype
.base_type
;
2182 if (ctype
->bit_size
>= 0 && type
->bit_size
>= 0) {
2183 unsigned offset
= e
->idx_to
* type
->bit_size
;
2184 if (offset
>= ctype
->bit_size
) {
2185 err
= "index out of bounds in";
2189 e
->ctype
= ctype
= type
;
2192 if (!e
->idx_expression
) {
2196 e
= e
->idx_expression
;
2197 } else if (e
->type
== EXPR_IDENTIFIER
) {
2198 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2199 err
= "field name not in struct or union";
2202 ctype
= find_struct_ident(ctype
, e
->expr_ident
);
2204 err
= "unknown field name in";
2207 e
->field
= e
->ctype
= ctype
;
2209 if (!e
->ident_expression
) {
2213 e
= e
->ident_expression
;
2214 } else if (e
->type
== EXPR_POS
) {
2215 err
= "internal front-end error: EXPR_POS in";
2220 expression_error(e
, "%s initializer", err
);
2225 * choose the next subobject to initialize.
2227 * Get designators for next element, switch old ones to EXPR_POS.
2228 * Return the resulting expression or NULL if we'd run out of subobjects.
2229 * The innermost designator is returned in *v. Designators in old
2230 * are assumed to be already sanity-checked.
2232 static struct expression
*next_designators(struct expression
*old
,
2233 struct symbol
*ctype
,
2234 struct expression
*e
, struct expression
**v
)
2236 struct expression
*new = NULL
;
2240 if (old
->type
== EXPR_INDEX
) {
2241 struct expression
*copy
;
2244 copy
= next_designators(old
->idx_expression
,
2247 n
= old
->idx_to
+ 1;
2248 if (n
* old
->ctype
->bit_size
== ctype
->bit_size
) {
2253 *v
= new = alloc_expression(e
->pos
, EXPR_INDEX
);
2256 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2259 new->idx_from
= new->idx_to
= n
;
2260 new->idx_expression
= copy
;
2261 new->ctype
= old
->ctype
;
2263 } else if (old
->type
== EXPR_IDENTIFIER
) {
2264 struct expression
*copy
;
2265 struct symbol
*field
;
2267 copy
= next_designators(old
->ident_expression
,
2270 field
= old
->field
->next_subobject
;
2276 *v
= new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2279 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2283 new->expr_ident
= field
->ident
;
2284 new->ident_expression
= copy
;
2291 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2292 int class, struct symbol
*ctype
);
2295 * deal with traversing subobjects [6.7.8(17,18,20)]
2297 static void handle_list_initializer(struct expression
*expr
,
2298 int class, struct symbol
*ctype
)
2300 struct expression
*e
, *last
= NULL
, *top
= NULL
, *next
;
2303 FOR_EACH_PTR(expr
->expr_list
, e
) {
2304 struct expression
**v
;
2305 struct symbol
*type
;
2308 if (e
->type
!= EXPR_INDEX
&& e
->type
!= EXPR_IDENTIFIER
) {
2311 last
= first_subobject(ctype
, class, &top
);
2313 last
= next_designators(last
, ctype
, e
, &top
);
2316 excess(e
, class & TYPE_PTR
? "array" :
2318 DELETE_CURRENT_PTR(e
);
2322 warning(e
->pos
, "advancing past deep designator");
2325 REPLACE_CURRENT_PTR(e
, last
);
2327 next
= check_designators(e
, ctype
);
2329 DELETE_CURRENT_PTR(e
);
2333 /* deeper than one designator? */
2335 convert_designators(last
);
2340 lclass
= classify_type(top
->ctype
, &type
);
2341 if (top
->type
== EXPR_INDEX
)
2342 v
= &top
->idx_expression
;
2344 v
= &top
->ident_expression
;
2346 if (handle_simple_initializer(v
, 1, lclass
, top
->ctype
))
2349 if (!(lclass
& TYPE_COMPOUND
)) {
2350 warning(e
->pos
, "bogus scalar initializer");
2351 DELETE_CURRENT_PTR(e
);
2355 next
= first_subobject(type
, lclass
, v
);
2357 warning(e
->pos
, "missing braces around initializer");
2362 DELETE_CURRENT_PTR(e
);
2363 excess(e
, lclass
& TYPE_PTR
? "array" : "struct or union");
2365 } END_FOR_EACH_PTR(e
);
2367 convert_designators(last
);
2368 expr
->ctype
= ctype
;
2371 static int is_string_literal(struct expression
**v
)
2373 struct expression
*e
= *v
;
2374 while (e
&& e
->type
== EXPR_PREOP
&& e
->op
== '(')
2376 if (!e
|| e
->type
!= EXPR_STRING
)
2378 if (e
!= *v
&& Wparen_string
)
2380 "array initialized from parenthesized string constant");
2386 * We want a normal expression, possibly in one layer of braces. Warn
2387 * if the latter happens inside a list (it's legal, but likely to be
2388 * an effect of screwup). In case of anything not legal, we are definitely
2389 * having an effect of screwup, so just fail and let the caller warn.
2391 static struct expression
*handle_scalar(struct expression
*e
, int nested
)
2393 struct expression
*v
= NULL
, *p
;
2397 if (e
->type
!= EXPR_INITIALIZER
)
2400 FOR_EACH_PTR(e
->expr_list
, p
) {
2404 } END_FOR_EACH_PTR(p
);
2408 case EXPR_INITIALIZER
:
2410 case EXPR_IDENTIFIER
:
2416 warning(e
->pos
, "braces around scalar initializer");
2421 * deal with the cases that don't care about subobjects:
2422 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2423 * character array <- string literal, possibly in braces [6.7.8(14)]
2424 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2425 * compound type <- initializer list in braces [6.7.8(16)]
2426 * The last one punts to handle_list_initializer() which, in turn will call
2427 * us for individual elements of the list.
2429 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2430 * the lack of support of wide char stuff in general.
2432 * One note: we need to take care not to evaluate a string literal until
2433 * we know that we *will* handle it right here. Otherwise we would screw
2434 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2435 * { "string", ...} - we need to preserve that string literal recognizable
2436 * until we dig into the inner struct.
2438 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2439 int class, struct symbol
*ctype
)
2441 int is_string
= is_string_type(ctype
);
2442 struct expression
*e
= *ep
, *p
;
2443 struct symbol
*type
;
2449 if (!(class & TYPE_COMPOUND
)) {
2450 e
= handle_scalar(e
, nested
);
2454 if (!evaluate_expression(e
))
2456 compatible_assignment_types(e
, ctype
, ep
, "initializer");
2461 * sublist; either a string, or we dig in; the latter will deal with
2462 * pathologies, so we don't need anything fancy here.
2464 if (e
->type
== EXPR_INITIALIZER
) {
2466 struct expression
*v
= NULL
;
2469 FOR_EACH_PTR(e
->expr_list
, p
) {
2473 } END_FOR_EACH_PTR(p
);
2474 if (count
== 1 && is_string_literal(&v
)) {
2479 handle_list_initializer(e
, class, ctype
);
2484 if (is_string_literal(&e
)) {
2485 /* either we are doing array of char, or we'll have to dig in */
2492 /* struct or union can be initialized by compatible */
2493 if (class != TYPE_COMPOUND
)
2495 type
= evaluate_expression(e
);
2498 if (ctype
->type
== SYM_NODE
)
2499 ctype
= ctype
->ctype
.base_type
;
2500 if (type
->type
== SYM_NODE
)
2501 type
= type
->ctype
.base_type
;
2507 p
= alloc_expression(e
->pos
, EXPR_STRING
);
2509 type
= evaluate_expression(p
);
2510 if (ctype
->bit_size
!= -1 &&
2511 ctype
->bit_size
+ bits_in_char
< type
->bit_size
) {
2513 "too long initializer-string for array of char");
2519 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2521 struct symbol
*type
;
2522 int class = classify_type(ctype
, &type
);
2523 if (!handle_simple_initializer(ep
, 0, class, ctype
))
2524 expression_error(*ep
, "invalid initializer");
2527 static struct symbol
*evaluate_cast(struct expression
*expr
)
2529 struct expression
*target
= expr
->cast_expression
;
2530 struct symbol
*ctype
;
2531 struct symbol
*t1
, *t2
;
2533 int as1
= 0, as2
= 0;
2539 * Special case: a cast can be followed by an
2540 * initializer, in which case we need to pass
2541 * the type value down to that initializer rather
2542 * than trying to evaluate it as an expression
2544 * A more complex case is when the initializer is
2545 * dereferenced as part of a post-fix expression.
2546 * We need to produce an expression that can be dereferenced.
2548 if (target
->type
== EXPR_INITIALIZER
) {
2549 struct symbol
*sym
= expr
->cast_type
;
2550 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2552 sym
->initializer
= target
;
2553 evaluate_symbol(sym
);
2555 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2558 expr
->type
= EXPR_PREOP
;
2566 ctype
= examine_symbol_type(expr
->cast_type
);
2567 expr
->ctype
= ctype
;
2568 expr
->cast_type
= ctype
;
2570 evaluate_expression(target
);
2573 class1
= classify_type(ctype
, &t1
);
2575 /* cast to non-integer type -> not an integer constant expression */
2576 if (!is_int(class1
))
2578 /* if argument turns out to be not an integer constant expression *and*
2579 it was not a floating literal to start with -> too bad */
2580 else if (expr
->flags
== Int_const_expr
&&
2581 !(target
->flags
& Int_const_expr
))
2584 * You can always throw a value away by casting to
2585 * "void" - that's an implicit "force". Note that
2586 * the same is _not_ true of "void *".
2588 if (t1
== &void_ctype
)
2591 if (class1
& TYPE_COMPOUND
)
2592 warning(expr
->pos
, "cast to non-scalar");
2594 if (class1
== TYPE_PTR
)
2599 expression_error(expr
, "cast from unknown type");
2602 class2
= classify_type(t2
, &t2
);
2604 if (class2
& TYPE_COMPOUND
)
2605 warning(expr
->pos
, "cast from non-scalar");
2607 if (expr
->type
== EXPR_FORCE_CAST
)
2610 /* allowed cast unfouls */
2611 if (class2
& TYPE_FOULED
)
2612 t2
= t2
->ctype
.base_type
;
2615 if (class1
& TYPE_RESTRICT
)
2616 warning(expr
->pos
, "cast to restricted type");
2617 if (class2
& TYPE_RESTRICT
)
2618 warning(expr
->pos
, "cast from restricted type");
2621 if (t1
== &ulong_ctype
)
2623 else if (class1
== TYPE_PTR
)
2626 if (t2
== &ulong_ctype
)
2628 else if (class2
== TYPE_PTR
)
2631 if (!as1
&& as2
> 0)
2632 warning(expr
->pos
, "cast removes address space of expression");
2633 if (as1
> 0 && as2
> 0 && as1
!= as2
)
2634 warning(expr
->pos
, "cast between address spaces (<asn:%d>-><asn:%d>)", as2
, as1
);
2635 if (as1
> 0 && !as2
&&
2636 !is_null_pointer_constant(target
) && Wcast_to_address_space
)
2638 "cast adds address space to expression (<asn:%d>)", as1
);
2640 if (!(t1
->ctype
.modifiers
& MOD_PTRINHERIT
) && class1
== TYPE_PTR
&&
2641 !as1
&& (target
->flags
& Int_const_expr
)) {
2642 if (t1
->ctype
.base_type
== &void_ctype
) {
2643 if (is_zero_constant(target
)) {
2645 expr
->type
= EXPR_VALUE
;
2646 expr
->ctype
= &null_ctype
;
2657 * Evaluate a call expression with a symbol. This
2658 * should expand inline functions, and evaluate
2661 static int evaluate_symbol_call(struct expression
*expr
)
2663 struct expression
*fn
= expr
->fn
;
2664 struct symbol
*ctype
= fn
->ctype
;
2666 if (fn
->type
!= EXPR_PREOP
)
2669 if (ctype
->op
&& ctype
->op
->evaluate
)
2670 return ctype
->op
->evaluate(expr
);
2672 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2674 struct symbol
*curr
= current_fn
;
2675 current_fn
= ctype
->ctype
.base_type
;
2677 ret
= inline_function(expr
, ctype
);
2679 /* restore the old function */
2687 static struct symbol
*evaluate_call(struct expression
*expr
)
2690 struct symbol
*ctype
, *sym
;
2691 struct expression
*fn
= expr
->fn
;
2692 struct expression_list
*arglist
= expr
->args
;
2694 if (!evaluate_expression(fn
))
2696 sym
= ctype
= fn
->ctype
;
2697 if (ctype
->type
== SYM_NODE
)
2698 ctype
= ctype
->ctype
.base_type
;
2699 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
2700 ctype
= get_base_type(ctype
);
2702 examine_fn_arguments(ctype
);
2703 if (sym
->type
== SYM_NODE
&& fn
->type
== EXPR_PREOP
&&
2704 sym
->op
&& sym
->op
->args
) {
2705 if (!sym
->op
->args(expr
))
2708 if (!evaluate_arguments(sym
, ctype
, arglist
))
2710 if (ctype
->type
!= SYM_FN
) {
2711 expression_error(expr
, "not a function %s",
2712 show_ident(sym
->ident
));
2715 args
= expression_list_size(expr
->args
);
2716 fnargs
= symbol_list_size(ctype
->arguments
);
2718 expression_error(expr
,
2719 "not enough arguments for function %s",
2720 show_ident(sym
->ident
));
2721 if (args
> fnargs
&& !ctype
->variadic
)
2722 expression_error(expr
,
2723 "too many arguments for function %s",
2724 show_ident(sym
->ident
));
2726 if (sym
->type
== SYM_NODE
) {
2727 if (evaluate_symbol_call(expr
))
2730 expr
->ctype
= ctype
->ctype
.base_type
;
2734 static struct symbol
*evaluate_offsetof(struct expression
*expr
)
2736 struct expression
*e
= expr
->down
;
2737 struct symbol
*ctype
= expr
->in
;
2740 if (expr
->op
== '.') {
2741 struct symbol
*field
;
2744 expression_error(expr
, "expected structure or union");
2747 examine_symbol_type(ctype
);
2748 class = classify_type(ctype
, &ctype
);
2749 if (class != TYPE_COMPOUND
) {
2750 expression_error(expr
, "expected structure or union");
2754 field
= find_identifier(expr
->ident
, ctype
->symbol_list
, &offset
);
2756 expression_error(expr
, "unknown member");
2760 expr
->type
= EXPR_VALUE
;
2761 expr
->flags
= Int_const_expr
;
2762 expr
->value
= offset
;
2764 expr
->ctype
= size_t_ctype
;
2767 expression_error(expr
, "expected structure or union");
2770 examine_symbol_type(ctype
);
2771 class = classify_type(ctype
, &ctype
);
2772 if (class != (TYPE_COMPOUND
| TYPE_PTR
)) {
2773 expression_error(expr
, "expected array");
2776 ctype
= ctype
->ctype
.base_type
;
2778 expr
->type
= EXPR_VALUE
;
2779 expr
->flags
= Int_const_expr
;
2782 expr
->ctype
= size_t_ctype
;
2784 struct expression
*idx
= expr
->index
, *m
;
2785 struct symbol
*i_type
= evaluate_expression(idx
);
2786 int i_class
= classify_type(i_type
, &i_type
);
2787 if (!is_int(i_class
)) {
2788 expression_error(expr
, "non-integer index");
2791 unrestrict(idx
, i_class
, &i_type
);
2792 idx
= cast_to(idx
, size_t_ctype
);
2793 m
= alloc_const_expression(expr
->pos
,
2794 ctype
->bit_size
>> 3);
2795 m
->ctype
= size_t_ctype
;
2796 m
->flags
= Int_const_expr
;
2797 expr
->type
= EXPR_BINOP
;
2801 expr
->ctype
= size_t_ctype
;
2802 expr
->flags
= m
->flags
& idx
->flags
& Int_const_expr
;
2806 struct expression
*copy
= __alloc_expression(0);
2808 if (e
->type
== EXPR_OFFSETOF
)
2810 if (!evaluate_expression(e
))
2812 expr
->type
= EXPR_BINOP
;
2813 expr
->flags
= e
->flags
& copy
->flags
& Int_const_expr
;
2815 expr
->ctype
= size_t_ctype
;
2819 return size_t_ctype
;
2822 struct symbol
*evaluate_expression(struct expression
*expr
)
2829 switch (expr
->type
) {
2832 expression_error(expr
, "value expression without a type");
2835 return evaluate_string(expr
);
2837 return evaluate_symbol_expression(expr
);
2839 if (!evaluate_expression(expr
->left
))
2841 if (!evaluate_expression(expr
->right
))
2843 return evaluate_binop(expr
);
2845 return evaluate_logical(expr
);
2847 evaluate_expression(expr
->left
);
2848 if (!evaluate_expression(expr
->right
))
2850 return evaluate_comma(expr
);
2852 if (!evaluate_expression(expr
->left
))
2854 if (!evaluate_expression(expr
->right
))
2856 return evaluate_compare(expr
);
2857 case EXPR_ASSIGNMENT
:
2858 if (!evaluate_expression(expr
->left
))
2860 if (!evaluate_expression(expr
->right
))
2862 return evaluate_assignment(expr
);
2864 if (!evaluate_expression(expr
->unop
))
2866 return evaluate_preop(expr
);
2868 if (!evaluate_expression(expr
->unop
))
2870 return evaluate_postop(expr
);
2872 case EXPR_FORCE_CAST
:
2873 case EXPR_IMPLIED_CAST
:
2874 return evaluate_cast(expr
);
2876 return evaluate_sizeof(expr
);
2877 case EXPR_PTRSIZEOF
:
2878 return evaluate_ptrsizeof(expr
);
2880 return evaluate_alignof(expr
);
2882 return evaluate_member_dereference(expr
);
2884 return evaluate_call(expr
);
2886 case EXPR_CONDITIONAL
:
2887 return evaluate_conditional_expression(expr
);
2888 case EXPR_STATEMENT
:
2889 expr
->ctype
= evaluate_statement(expr
->statement
);
2893 expr
->ctype
= &ptr_ctype
;
2897 /* Evaluate the type of the symbol .. */
2898 evaluate_symbol(expr
->symbol
);
2899 /* .. but the type of the _expression_ is a "type" */
2900 expr
->ctype
= &type_ctype
;
2904 return evaluate_offsetof(expr
);
2906 /* These can not exist as stand-alone expressions */
2907 case EXPR_INITIALIZER
:
2908 case EXPR_IDENTIFIER
:
2911 expression_error(expr
, "internal front-end error: initializer in expression");
2914 expression_error(expr
, "internal front-end error: SLICE re-evaluated");
2920 static void check_duplicates(struct symbol
*sym
)
2923 struct symbol
*next
= sym
;
2925 while ((next
= next
->same_symbol
) != NULL
) {
2926 const char *typediff
;
2927 evaluate_symbol(next
);
2929 typediff
= type_difference(sym
, next
, 0, 0);
2931 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2932 show_ident(sym
->ident
),
2933 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
2938 unsigned long mod
= sym
->ctype
.modifiers
;
2939 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
2941 if (!(mod
& MOD_TOPLEVEL
))
2945 if (sym
->ident
== &main_ident
)
2947 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
2951 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
2953 struct symbol
*base_type
;
2961 sym
= examine_symbol_type(sym
);
2962 base_type
= get_base_type(sym
);
2966 /* Evaluate the initializers */
2967 if (sym
->initializer
)
2968 evaluate_initializer(sym
, &sym
->initializer
);
2970 /* And finally, evaluate the body of the symbol too */
2971 if (base_type
->type
== SYM_FN
) {
2972 struct symbol
*curr
= current_fn
;
2974 current_fn
= base_type
;
2976 examine_fn_arguments(base_type
);
2977 if (!base_type
->stmt
&& base_type
->inline_stmt
)
2979 if (base_type
->stmt
)
2980 evaluate_statement(base_type
->stmt
);
2988 void evaluate_symbol_list(struct symbol_list
*list
)
2992 FOR_EACH_PTR(list
, sym
) {
2993 evaluate_symbol(sym
);
2994 check_duplicates(sym
);
2995 } END_FOR_EACH_PTR(sym
);
2998 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
3000 struct expression
*expr
= stmt
->expression
;
3001 struct symbol
*fntype
;
3003 evaluate_expression(expr
);
3004 fntype
= current_fn
->ctype
.base_type
;
3005 if (!fntype
|| fntype
== &void_ctype
) {
3006 if (expr
&& expr
->ctype
!= &void_ctype
)
3007 expression_error(expr
, "return expression in %s function", fntype
?"void":"typeless");
3008 if (expr
&& Wreturn_void
)
3009 warning(stmt
->pos
, "returning void-valued expression");
3014 sparse_error(stmt
->pos
, "return with no return value");
3019 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, "return expression");
3023 static void evaluate_if_statement(struct statement
*stmt
)
3025 if (!stmt
->if_conditional
)
3028 evaluate_conditional(stmt
->if_conditional
, 0);
3029 evaluate_statement(stmt
->if_true
);
3030 evaluate_statement(stmt
->if_false
);
3033 static void evaluate_iterator(struct statement
*stmt
)
3035 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
3036 evaluate_conditional(stmt
->iterator_post_condition
,1);
3037 evaluate_statement(stmt
->iterator_pre_statement
);
3038 evaluate_statement(stmt
->iterator_statement
);
3039 evaluate_statement(stmt
->iterator_post_statement
);
3042 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
3044 switch (*constraint
) {
3045 case '=': /* Assignment */
3046 case '+': /* Update */
3049 expression_error(expr
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
3053 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
3055 switch (*constraint
) {
3056 case '=': /* Assignment */
3057 case '+': /* Update */
3058 expression_error(expr
, "input constraint with assignment (\"%s\")", constraint
);
3062 static void evaluate_asm_statement(struct statement
*stmt
)
3064 struct expression
*expr
;
3067 expr
= stmt
->asm_string
;
3068 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3069 sparse_error(stmt
->pos
, "need constant string for inline asm");
3074 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
3075 struct ident
*ident
;
3078 case 0: /* Identifier */
3080 ident
= (struct ident
*)expr
;
3083 case 1: /* Constraint */
3085 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3086 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
3087 *THIS_ADDRESS(expr
) = NULL
;
3090 verify_output_constraint(expr
, expr
->string
->data
);
3093 case 2: /* Expression */
3095 if (!evaluate_expression(expr
))
3097 if (!lvalue_expression(expr
))
3098 warning(expr
->pos
, "asm output is not an lvalue");
3099 evaluate_assign_to(expr
, expr
->ctype
);
3102 } END_FOR_EACH_PTR(expr
);
3105 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
3106 struct ident
*ident
;
3109 case 0: /* Identifier */
3111 ident
= (struct ident
*)expr
;
3114 case 1: /* Constraint */
3116 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3117 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
3118 *THIS_ADDRESS(expr
) = NULL
;
3121 verify_input_constraint(expr
, expr
->string
->data
);
3124 case 2: /* Expression */
3126 if (!evaluate_expression(expr
))
3130 } END_FOR_EACH_PTR(expr
);
3132 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
3134 sparse_error(stmt
->pos
, "bad asm output");
3137 if (expr
->type
== EXPR_STRING
)
3139 expression_error(expr
, "asm clobber is not a string");
3140 } END_FOR_EACH_PTR(expr
);
3143 static void evaluate_case_statement(struct statement
*stmt
)
3145 evaluate_expression(stmt
->case_expression
);
3146 evaluate_expression(stmt
->case_to
);
3147 evaluate_statement(stmt
->case_statement
);
3150 static void check_case_type(struct expression
*switch_expr
,
3151 struct expression
*case_expr
,
3152 struct expression
**enumcase
)
3154 struct symbol
*switch_type
, *case_type
;
3160 switch_type
= switch_expr
->ctype
;
3161 case_type
= evaluate_expression(case_expr
);
3163 if (!switch_type
|| !case_type
)
3167 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
3168 else if (is_enum_type(case_type
))
3169 *enumcase
= case_expr
;
3172 sclass
= classify_type(switch_type
, &switch_type
);
3173 cclass
= classify_type(case_type
, &case_type
);
3175 /* both should be arithmetic */
3176 if (!(sclass
& cclass
& TYPE_NUM
))
3179 /* neither should be floating */
3180 if ((sclass
| cclass
) & TYPE_FLOAT
)
3183 /* if neither is restricted, we are OK */
3184 if (!((sclass
| cclass
) & TYPE_RESTRICT
))
3187 if (!restricted_binop_type(SPECIAL_EQUAL
, case_expr
, switch_expr
,
3188 cclass
, sclass
, case_type
, switch_type
))
3189 warning(case_expr
->pos
, "restricted degrades to integer");
3194 expression_error(case_expr
, "incompatible types for 'case' statement");
3197 static void evaluate_switch_statement(struct statement
*stmt
)
3200 struct expression
*enumcase
= NULL
;
3201 struct expression
**enumcase_holder
= &enumcase
;
3202 struct expression
*sel
= stmt
->switch_expression
;
3204 evaluate_expression(sel
);
3205 evaluate_statement(stmt
->switch_statement
);
3208 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
3209 enumcase_holder
= NULL
; /* Only check cases against switch */
3211 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
3212 struct statement
*case_stmt
= sym
->stmt
;
3213 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
3214 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
3215 } END_FOR_EACH_PTR(sym
);
3218 struct symbol
*evaluate_statement(struct statement
*stmt
)
3223 switch (stmt
->type
) {
3224 case STMT_DECLARATION
: {
3226 FOR_EACH_PTR(stmt
->declaration
, s
) {
3228 } END_FOR_EACH_PTR(s
);
3233 return evaluate_return_expression(stmt
);
3235 case STMT_EXPRESSION
:
3236 if (!evaluate_expression(stmt
->expression
))
3238 if (stmt
->expression
->ctype
== &null_ctype
)
3239 stmt
->expression
= cast_to(stmt
->expression
, &ptr_ctype
);
3240 return degenerate(stmt
->expression
);
3242 case STMT_COMPOUND
: {
3243 struct statement
*s
;
3244 struct symbol
*type
= NULL
;
3246 /* Evaluate the return symbol in the compound statement */
3247 evaluate_symbol(stmt
->ret
);
3250 * Then, evaluate each statement, making the type of the
3251 * compound statement be the type of the last statement
3253 type
= evaluate_statement(stmt
->args
);
3254 FOR_EACH_PTR(stmt
->stmts
, s
) {
3255 type
= evaluate_statement(s
);
3256 } END_FOR_EACH_PTR(s
);
3262 evaluate_if_statement(stmt
);
3265 evaluate_iterator(stmt
);
3268 evaluate_switch_statement(stmt
);
3271 evaluate_case_statement(stmt
);
3274 return evaluate_statement(stmt
->label_statement
);
3276 evaluate_expression(stmt
->goto_expression
);
3281 evaluate_asm_statement(stmt
);
3284 evaluate_expression(stmt
->expression
);
3287 evaluate_expression(stmt
->range_expression
);
3288 evaluate_expression(stmt
->range_low
);
3289 evaluate_expression(stmt
->range_high
);