4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * Evaluate constant expressions.
43 #include "expression.h"
45 struct symbol
*current_fn
;
47 static struct symbol
*degenerate(struct expression
*expr
);
48 static struct symbol
*evaluate_symbol(struct symbol
*sym
);
50 static struct symbol
*evaluate_symbol_expression(struct expression
*expr
)
52 struct expression
*addr
;
53 struct symbol
*sym
= expr
->symbol
;
54 struct symbol
*base_type
;
57 expression_error(expr
, "undefined identifier '%s'", show_ident(expr
->symbol_name
));
61 examine_symbol_type(sym
);
63 base_type
= get_base_type(sym
);
65 expression_error(expr
, "identifier '%s' has no type", show_ident(expr
->symbol_name
));
69 addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
71 addr
->symbol_name
= expr
->symbol_name
;
72 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
73 expr
->type
= EXPR_PREOP
;
77 /* The type of a symbol is the symbol itself! */
82 static struct symbol
*evaluate_string(struct expression
*expr
)
84 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
85 struct symbol
*array
= alloc_symbol(expr
->pos
, SYM_ARRAY
);
86 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
87 struct expression
*initstr
= alloc_expression(expr
->pos
, EXPR_STRING
);
88 unsigned int length
= expr
->string
->length
;
90 sym
->array_size
= alloc_const_expression(expr
->pos
, length
);
91 sym
->bit_size
= bytes_to_bits(length
);
92 sym
->ctype
.alignment
= 1;
94 sym
->ctype
.modifiers
= MOD_STATIC
;
95 sym
->ctype
.base_type
= array
;
96 sym
->initializer
= initstr
;
99 initstr
->string
= expr
->string
;
101 array
->array_size
= sym
->array_size
;
102 array
->bit_size
= bytes_to_bits(length
);
103 array
->ctype
.alignment
= 1;
104 array
->ctype
.modifiers
= MOD_STATIC
;
105 array
->ctype
.base_type
= &char_ctype
;
108 addr
->ctype
= &lazy_ptr_ctype
;
110 expr
->type
= EXPR_PREOP
;
117 /* type has come from classify_type and is an integer type */
118 static inline struct symbol
*integer_promotion(struct symbol
*type
)
120 struct symbol
*orig_type
= type
;
121 unsigned long mod
= type
->ctype
.modifiers
;
122 int width
= type
->bit_size
;
125 * Bitfields always promote to the base type,
126 * even if the bitfield might be bigger than
129 if (type
->type
== SYM_BITFIELD
) {
130 type
= type
->ctype
.base_type
;
133 mod
= type
->ctype
.modifiers
;
134 if (width
< bits_in_int
)
137 /* If char/short has as many bits as int, it still gets "promoted" */
138 if (mod
& (MOD_CHAR
| MOD_SHORT
)) {
139 if (mod
& MOD_UNSIGNED
)
147 * integer part of usual arithmetic conversions:
148 * integer promotions are applied
149 * if left and right are identical, we are done
150 * if signedness is the same, convert one with lower rank
151 * unless unsigned argument has rank lower than signed one, convert the
153 * if signed argument is bigger than unsigned one, convert the unsigned.
154 * otherwise, convert signed.
156 * Leaving aside the integer promotions, that is equivalent to
157 * if identical, don't convert
158 * if left is bigger than right, convert right
159 * if right is bigger than left, convert right
160 * otherwise, if signedness is the same, convert one with lower rank
161 * otherwise convert the signed one.
163 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
165 unsigned long lmod
, rmod
;
167 left
= integer_promotion(left
);
168 right
= integer_promotion(right
);
173 if (left
->bit_size
> right
->bit_size
)
176 if (right
->bit_size
> left
->bit_size
)
179 lmod
= left
->ctype
.modifiers
;
180 rmod
= right
->ctype
.modifiers
;
181 if ((lmod
^ rmod
) & MOD_UNSIGNED
) {
182 if (lmod
& MOD_UNSIGNED
)
184 } else if ((lmod
& ~rmod
) & (MOD_LONG_ALL
))
192 static int same_cast_type(struct symbol
*orig
, struct symbol
*new)
194 return orig
->bit_size
== new->bit_size
&&
195 orig
->bit_offset
== new->bit_offset
;
198 static struct symbol
*base_type(struct symbol
*node
, unsigned long *modp
, unsigned long *asp
)
200 unsigned long mod
, as
;
204 mod
|= node
->ctype
.modifiers
;
205 as
|= node
->ctype
.as
;
206 if (node
->type
== SYM_NODE
) {
207 node
= node
->ctype
.base_type
;
212 *modp
= mod
& ~MOD_IGNORE
;
217 static int is_same_type(struct expression
*expr
, struct symbol
*new)
219 struct symbol
*old
= expr
->ctype
;
220 unsigned long oldmod
, newmod
, oldas
, newas
;
222 old
= base_type(old
, &oldmod
, &oldas
);
223 new = base_type(new, &newmod
, &newas
);
225 /* Same base type, same address space? */
226 if (old
== new && oldas
== newas
) {
227 unsigned long difmod
;
229 /* Check the modifier bits. */
230 difmod
= (oldmod
^ newmod
) & ~MOD_NOCAST
;
232 /* Exact same type? */
237 * Not the same type, but differs only in "const".
238 * Don't warn about MOD_NOCAST.
240 if (difmod
== MOD_CONST
)
243 if ((oldmod
| newmod
) & MOD_NOCAST
) {
244 const char *tofrom
= "to/from";
245 if (!(newmod
& MOD_NOCAST
))
247 if (!(oldmod
& MOD_NOCAST
))
249 warning(expr
->pos
, "implicit cast %s nocast type", tofrom
);
255 warn_for_different_enum_types (struct position pos
,
256 struct symbol
*typea
,
257 struct symbol
*typeb
)
261 if (typea
->type
== SYM_NODE
)
262 typea
= typea
->ctype
.base_type
;
263 if (typeb
->type
== SYM_NODE
)
264 typeb
= typeb
->ctype
.base_type
;
269 if (typea
->type
== SYM_ENUM
&& typeb
->type
== SYM_ENUM
) {
270 warning(pos
, "mixing different enum types");
271 info(pos
, " %s versus", show_typename(typea
));
272 info(pos
, " %s", show_typename(typeb
));
277 * This gets called for implicit casts in assignments and
278 * integer promotion. We often want to try to move the
279 * cast down, because the ops involved may have been
280 * implicitly cast up, and we can get rid of the casts
283 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
285 struct expression
*expr
;
287 warn_for_different_enum_types (old
->pos
, old
->ctype
, type
);
289 if (old
->ctype
!= &null_ctype
&& is_same_type(old
, type
))
293 * See if we can simplify the op. Move the cast down.
297 if (old
->ctype
->bit_size
< type
->bit_size
)
299 if (old
->op
== '~') {
301 old
->unop
= cast_to(old
->unop
, type
);
306 case EXPR_IMPLIED_CAST
:
307 warn_for_different_enum_types(old
->pos
, old
->ctype
, type
);
309 if (old
->ctype
->bit_size
>= type
->bit_size
) {
310 struct expression
*orig
= old
->cast_expression
;
311 if (same_cast_type(orig
->ctype
, type
))
313 if (old
->ctype
->bit_offset
== type
->bit_offset
) {
315 old
->cast_type
= type
;
325 expr
= alloc_expression(old
->pos
, EXPR_IMPLIED_CAST
);
326 expr
->flags
= old
->flags
;
328 expr
->cast_type
= type
;
329 expr
->cast_expression
= old
;
344 static inline int classify_type(struct symbol
*type
, struct symbol
**base
)
346 static int type_class
[SYM_BAD
+ 1] = {
347 [SYM_PTR
] = TYPE_PTR
,
348 [SYM_FN
] = TYPE_PTR
| TYPE_FN
,
349 [SYM_ARRAY
] = TYPE_PTR
| TYPE_COMPOUND
,
350 [SYM_STRUCT
] = TYPE_COMPOUND
,
351 [SYM_UNION
] = TYPE_COMPOUND
,
352 [SYM_BITFIELD
] = TYPE_NUM
| TYPE_BITFIELD
,
353 [SYM_RESTRICT
] = TYPE_NUM
| TYPE_RESTRICT
,
354 [SYM_FOULED
] = TYPE_NUM
| TYPE_RESTRICT
| TYPE_FOULED
,
356 if (type
->type
== SYM_NODE
)
357 type
= type
->ctype
.base_type
;
358 if (type
->type
== SYM_TYPEOF
) {
359 type
= evaluate_expression(type
->initializer
);
362 else if (type
->type
== SYM_NODE
)
363 type
= type
->ctype
.base_type
;
365 if (type
->type
== SYM_ENUM
)
366 type
= type
->ctype
.base_type
;
368 if (type
->type
== SYM_BASETYPE
) {
369 if (type
->ctype
.base_type
== &int_type
)
371 if (type
->ctype
.base_type
== &fp_type
)
372 return TYPE_NUM
| TYPE_FLOAT
;
374 return type_class
[type
->type
];
377 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
379 static inline int is_string_type(struct symbol
*type
)
381 if (type
->type
== SYM_NODE
)
382 type
= type
->ctype
.base_type
;
383 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
386 static struct symbol
*bad_expr_type(struct expression
*expr
)
388 sparse_error(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
389 switch (expr
->type
) {
392 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
393 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
397 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
404 return expr
->ctype
= &bad_ctype
;
407 static int restricted_value(struct expression
*v
, struct symbol
*type
)
409 if (v
->type
!= EXPR_VALUE
)
416 static int restricted_binop(int op
, struct symbol
*type
)
421 case SPECIAL_AND_ASSIGN
:
422 case SPECIAL_OR_ASSIGN
:
423 case SPECIAL_XOR_ASSIGN
:
424 return 1; /* unfoul */
428 return 2; /* keep fouled */
430 case SPECIAL_NOTEQUAL
:
431 return 3; /* warn if fouled */
437 static int restricted_unop(int op
, struct symbol
**type
)
440 if ((*type
)->bit_size
< bits_in_int
)
441 *type
= befoul(*type
);
448 /* type should be SYM_FOULED */
449 static inline struct symbol
*unfoul(struct symbol
*type
)
451 return type
->ctype
.base_type
;
454 static struct symbol
*restricted_binop_type(int op
,
455 struct expression
*left
,
456 struct expression
*right
,
457 int lclass
, int rclass
,
458 struct symbol
*ltype
,
459 struct symbol
*rtype
)
461 struct symbol
*ctype
= NULL
;
462 if (lclass
& TYPE_RESTRICT
) {
463 if (rclass
& TYPE_RESTRICT
) {
464 if (ltype
== rtype
) {
466 } else if (lclass
& TYPE_FOULED
) {
467 if (unfoul(ltype
) == rtype
)
469 } else if (rclass
& TYPE_FOULED
) {
470 if (unfoul(rtype
) == ltype
)
474 if (!restricted_value(right
, ltype
))
477 } else if (!restricted_value(left
, rtype
))
481 switch (restricted_binop(op
, ctype
)) {
483 if ((lclass
^ rclass
) & TYPE_FOULED
)
484 ctype
= unfoul(ctype
);
487 if (!(lclass
& rclass
& TYPE_FOULED
))
499 static inline void unrestrict(struct expression
*expr
,
500 int class, struct symbol
**ctype
)
502 if (class & TYPE_RESTRICT
) {
503 if (class & TYPE_FOULED
)
504 *ctype
= unfoul(*ctype
);
505 warning(expr
->pos
, "%s degrades to integer",
506 show_typename(*ctype
));
507 *ctype
= (*ctype
)->ctype
.base_type
; /* get to arithmetic type */
511 static struct symbol
*usual_conversions(int op
,
512 struct expression
*left
,
513 struct expression
*right
,
514 int lclass
, int rclass
,
515 struct symbol
*ltype
,
516 struct symbol
*rtype
)
518 struct symbol
*ctype
;
520 warn_for_different_enum_types(right
->pos
, left
->ctype
, right
->ctype
);
522 if ((lclass
| rclass
) & TYPE_RESTRICT
)
526 if (!(lclass
& TYPE_FLOAT
)) {
527 if (!(rclass
& TYPE_FLOAT
))
528 return bigger_int_type(ltype
, rtype
);
531 } else if (rclass
& TYPE_FLOAT
) {
532 unsigned long lmod
= ltype
->ctype
.modifiers
;
533 unsigned long rmod
= rtype
->ctype
.modifiers
;
534 if (rmod
& ~lmod
& (MOD_LONG_ALL
))
542 ctype
= restricted_binop_type(op
, left
, right
,
543 lclass
, rclass
, ltype
, rtype
);
547 unrestrict(left
, lclass
, <ype
);
548 unrestrict(right
, rclass
, &rtype
);
553 static inline int lvalue_expression(struct expression
*expr
)
555 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
558 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*itype
)
560 struct expression
*index
= expr
->right
;
561 struct symbol
*ctype
, *base
;
564 classify_type(degenerate(expr
->left
), &ctype
);
565 base
= examine_pointer_target(ctype
);
568 expression_error(expr
, "missing type information");
571 if (is_function(base
)) {
572 expression_error(expr
, "arithmetics on pointers to functions");
576 /* Get the size of whatever the pointer points to */
577 multiply
= is_void_type(base
) ? 1 : bits_to_bytes(base
->bit_size
);
579 if (ctype
== &null_ctype
)
583 if (multiply
== 1 && itype
->bit_size
>= bits_in_pointer
)
586 if (index
->type
== EXPR_VALUE
) {
587 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
588 unsigned long long v
= index
->value
, mask
;
589 mask
= 1ULL << (itype
->bit_size
- 1);
595 mask
= 1ULL << (bits_in_pointer
- 1);
596 v
&= mask
| (mask
- 1);
598 val
->ctype
= ssize_t_ctype
;
603 if (itype
->bit_size
< bits_in_pointer
)
604 index
= cast_to(index
, ssize_t_ctype
);
607 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
608 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
610 val
->ctype
= ssize_t_ctype
;
611 val
->value
= multiply
;
614 mul
->ctype
= ssize_t_ctype
;
624 static void examine_fn_arguments(struct symbol
*fn
);
626 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
628 const char *type_difference(struct ctype
*c1
, struct ctype
*c2
,
629 unsigned long mod1
, unsigned long mod2
)
631 unsigned long as1
= c1
->as
, as2
= c2
->as
;
632 struct symbol
*t1
= c1
->base_type
;
633 struct symbol
*t2
= c2
->base_type
;
634 int move1
= 1, move2
= 1;
635 mod1
|= c1
->modifiers
;
636 mod2
|= c2
->modifiers
;
640 struct symbol
*base1
= t1
->ctype
.base_type
;
641 struct symbol
*base2
= t2
->ctype
.base_type
;
644 * FIXME! Collect alignment and context too here!
647 if (t1
&& t1
->type
!= SYM_PTR
) {
648 mod1
|= t1
->ctype
.modifiers
;
655 if (t2
&& t2
->type
!= SYM_PTR
) {
656 mod2
|= t2
->ctype
.modifiers
;
665 return "different types";
667 if (t1
->type
== SYM_NODE
|| t1
->type
== SYM_ENUM
) {
675 if (t2
->type
== SYM_NODE
|| t2
->type
== SYM_ENUM
) {
685 if (type
!= t2
->type
)
686 return "different base types";
690 sparse_error(t1
->pos
,
691 "internal error: bad type in derived(%d)",
695 return "different base types";
698 /* allow definition of incomplete structs and unions */
699 if (t1
->ident
== t2
->ident
)
701 return "different base types";
703 /* XXX: we ought to compare sizes */
707 return "different address spaces";
708 /* MOD_SPECIFIER is due to idiocy in parse.c */
709 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SPECIFIER
)
710 return "different modifiers";
711 /* we could be lazier here */
712 base1
= examine_pointer_target(t1
);
713 base2
= examine_pointer_target(t2
);
714 mod1
= t1
->ctype
.modifiers
;
716 mod2
= t2
->ctype
.modifiers
;
720 struct symbol
*arg1
, *arg2
;
724 return "different address spaces";
725 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
726 return "different modifiers";
727 mod1
= t1
->ctype
.modifiers
;
729 mod2
= t2
->ctype
.modifiers
;
732 if (base1
->variadic
!= base2
->variadic
)
733 return "incompatible variadic arguments";
734 examine_fn_arguments(t1
);
735 examine_fn_arguments(t2
);
736 PREPARE_PTR_LIST(t1
->arguments
, arg1
);
737 PREPARE_PTR_LIST(t2
->arguments
, arg2
);
744 return "different argument counts";
745 diffstr
= type_difference(&arg1
->ctype
,
749 static char argdiff
[80];
750 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diffstr
);
757 FINISH_PTR_LIST(arg2
);
758 FINISH_PTR_LIST(arg1
);
763 return "different address spaces";
765 return "different base types";
766 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
770 return "different type sizes";
771 else if (diff
& ~MOD_SIGNEDNESS
)
772 return "different modifiers";
774 return "different signedness";
780 return "different address spaces";
781 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
782 return "different modifiers";
786 static void bad_null(struct expression
*expr
)
788 if (Wnon_pointer_null
)
789 warning(expr
->pos
, "Using plain integer as NULL pointer");
792 static unsigned long target_qualifiers(struct symbol
*type
)
794 unsigned long mod
= type
->ctype
.modifiers
& MOD_IGN
;
795 if (type
->ctype
.base_type
&& type
->ctype
.base_type
->type
== SYM_ARRAY
)
800 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
)
802 const char *typediff
;
803 struct symbol
*ltype
, *rtype
;
804 struct expression
*l
= expr
->left
;
805 struct expression
*r
= expr
->right
;
806 struct symbol
*lbase
;
808 classify_type(degenerate(l
), <ype
);
809 classify_type(degenerate(r
), &rtype
);
811 lbase
= examine_pointer_target(ltype
);
812 examine_pointer_target(rtype
);
813 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
814 target_qualifiers(rtype
),
815 target_qualifiers(ltype
));
817 expression_error(expr
, "subtraction of different types can't work (%s)", typediff
);
819 if (is_function(lbase
)) {
820 expression_error(expr
, "subtraction of functions? Share your drugs");
824 expr
->ctype
= ssize_t_ctype
;
825 if (lbase
->bit_size
> bits_in_char
) {
826 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
827 struct expression
*div
= expr
;
828 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
829 unsigned long value
= bits_to_bytes(lbase
->bit_size
);
831 val
->ctype
= size_t_ctype
;
834 if (value
& (value
-1)) {
835 if (Wptr_subtraction_blows
)
836 warning(expr
->pos
, "potentially expensive pointer subtraction");
840 sub
->ctype
= ssize_t_ctype
;
849 return ssize_t_ctype
;
852 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
854 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
856 struct symbol
*ctype
;
861 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
862 warning(expr
->pos
, "assignment expression in conditional");
864 ctype
= evaluate_expression(expr
);
866 if (is_safe_type(ctype
))
867 warning(expr
->pos
, "testing a 'safe expression'");
873 static struct symbol
*evaluate_logical(struct expression
*expr
)
875 if (!evaluate_conditional(expr
->left
, 0))
877 if (!evaluate_conditional(expr
->right
, 0))
880 /* the result is int [6.5.13(3), 6.5.14(3)] */
881 expr
->ctype
= &int_ctype
;
883 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
889 static struct symbol
*evaluate_binop(struct expression
*expr
)
891 struct symbol
*ltype
, *rtype
, *ctype
;
892 int lclass
= classify_type(expr
->left
->ctype
, <ype
);
893 int rclass
= classify_type(expr
->right
->ctype
, &rtype
);
897 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
901 /* number op number */
902 if (lclass
& rclass
& TYPE_NUM
) {
903 if ((lclass
| rclass
) & TYPE_FLOAT
) {
905 case '+': case '-': case '*': case '/':
908 return bad_expr_type(expr
);
912 if (op
== SPECIAL_LEFTSHIFT
|| op
== SPECIAL_RIGHTSHIFT
) {
913 // shifts do integer promotions, but that's it.
914 unrestrict(expr
->left
, lclass
, <ype
);
915 unrestrict(expr
->right
, rclass
, &rtype
);
916 ctype
= ltype
= integer_promotion(ltype
);
917 rtype
= integer_promotion(rtype
);
919 // The rest do usual conversions
920 const unsigned left_not
= expr
->left
->type
== EXPR_PREOP
921 && expr
->left
->op
== '!';
922 const unsigned right_not
= expr
->right
->type
== EXPR_PREOP
923 && expr
->right
->op
== '!';
924 if ((op
== '&' || op
== '|') && (left_not
|| right_not
))
925 warning(expr
->pos
, "dubious: %sx %c %sy",
928 right_not
? "!" : "");
930 ltype
= usual_conversions(op
, expr
->left
, expr
->right
,
931 lclass
, rclass
, ltype
, rtype
);
932 ctype
= rtype
= ltype
;
935 expr
->left
= cast_to(expr
->left
, ltype
);
936 expr
->right
= cast_to(expr
->right
, rtype
);
941 /* pointer (+|-) integer */
942 if (lclass
& TYPE_PTR
&& is_int(rclass
) && (op
== '+' || op
== '-')) {
943 unrestrict(expr
->right
, rclass
, &rtype
);
944 return evaluate_ptr_add(expr
, rtype
);
947 /* integer + pointer */
948 if (rclass
& TYPE_PTR
&& is_int(lclass
) && op
== '+') {
949 struct expression
*index
= expr
->left
;
950 unrestrict(index
, lclass
, <ype
);
951 expr
->left
= expr
->right
;
953 return evaluate_ptr_add(expr
, ltype
);
956 /* pointer - pointer */
957 if (lclass
& rclass
& TYPE_PTR
&& expr
->op
== '-')
958 return evaluate_ptr_sub(expr
);
960 return bad_expr_type(expr
);
963 static struct symbol
*evaluate_comma(struct expression
*expr
)
965 expr
->ctype
= degenerate(expr
->right
);
966 if (expr
->ctype
== &null_ctype
)
967 expr
->ctype
= &ptr_ctype
;
968 expr
->flags
&= expr
->left
->flags
& expr
->right
->flags
;
972 static int modify_for_unsigned(int op
)
975 op
= SPECIAL_UNSIGNED_LT
;
977 op
= SPECIAL_UNSIGNED_GT
;
978 else if (op
== SPECIAL_LTE
)
979 op
= SPECIAL_UNSIGNED_LTE
;
980 else if (op
== SPECIAL_GTE
)
981 op
= SPECIAL_UNSIGNED_GTE
;
985 static inline int is_null_pointer_constant(struct expression
*e
)
987 if (e
->ctype
== &null_ctype
)
989 if (!(e
->flags
& Int_const_expr
))
991 return is_zero_constant(e
) ? 2 : 0;
994 static struct symbol
*evaluate_compare(struct expression
*expr
)
996 struct expression
*left
= expr
->left
, *right
= expr
->right
;
997 struct symbol
*ltype
, *rtype
, *lbase
, *rbase
;
998 int lclass
= classify_type(degenerate(left
), <ype
);
999 int rclass
= classify_type(degenerate(right
), &rtype
);
1000 struct symbol
*ctype
;
1001 const char *typediff
;
1004 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
1009 if (is_type_type(ltype
) && is_type_type(rtype
))
1012 if (is_safe_type(left
->ctype
) || is_safe_type(right
->ctype
))
1013 warning(expr
->pos
, "testing a 'safe expression'");
1015 /* number on number */
1016 if (lclass
& rclass
& TYPE_NUM
) {
1017 ctype
= usual_conversions(expr
->op
, expr
->left
, expr
->right
,
1018 lclass
, rclass
, ltype
, rtype
);
1019 expr
->left
= cast_to(expr
->left
, ctype
);
1020 expr
->right
= cast_to(expr
->right
, ctype
);
1021 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
1022 expr
->op
= modify_for_unsigned(expr
->op
);
1026 /* at least one must be a pointer */
1027 if (!((lclass
| rclass
) & TYPE_PTR
))
1028 return bad_expr_type(expr
);
1030 /* equality comparisons can be with null pointer constants */
1031 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1032 int is_null1
= is_null_pointer_constant(left
);
1033 int is_null2
= is_null_pointer_constant(right
);
1038 if (is_null1
&& is_null2
) {
1039 int positive
= expr
->op
== SPECIAL_EQUAL
;
1040 expr
->type
= EXPR_VALUE
;
1041 expr
->value
= positive
;
1044 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1045 left
= cast_to(left
, rtype
);
1048 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1049 right
= cast_to(right
, ltype
);
1053 /* both should be pointers */
1054 if (!(lclass
& rclass
& TYPE_PTR
))
1055 return bad_expr_type(expr
);
1056 expr
->op
= modify_for_unsigned(expr
->op
);
1058 lbase
= examine_pointer_target(ltype
);
1059 rbase
= examine_pointer_target(rtype
);
1061 /* they also have special treatment for pointers to void */
1062 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1063 if (ltype
->ctype
.as
== rtype
->ctype
.as
) {
1064 if (lbase
== &void_ctype
) {
1065 right
= cast_to(right
, ltype
);
1068 if (rbase
== &void_ctype
) {
1069 left
= cast_to(left
, rtype
);
1075 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1076 target_qualifiers(rtype
),
1077 target_qualifiers(ltype
));
1081 expression_error(expr
, "incompatible types in comparison expression (%s)", typediff
);
1085 /* the result is int [6.5.8(6), 6.5.9(3)]*/
1086 expr
->ctype
= &int_ctype
;
1091 * NOTE! The degenerate case of "x ? : y", where we don't
1092 * have a true case, this will possibly promote "x" to the
1093 * same type as "y", and thus _change_ the conditional
1094 * test in the expression. But since promotion is "safe"
1095 * for testing, that's OK.
1097 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
1099 struct expression
**true;
1100 struct symbol
*ctype
, *ltype
, *rtype
, *lbase
, *rbase
;
1102 const char * typediff
;
1105 if (!evaluate_conditional(expr
->conditional
, 0))
1107 if (!evaluate_expression(expr
->cond_false
))
1110 ctype
= degenerate(expr
->conditional
);
1111 rtype
= degenerate(expr
->cond_false
);
1113 true = &expr
->conditional
;
1115 if (expr
->cond_true
) {
1116 if (!evaluate_expression(expr
->cond_true
))
1118 ltype
= degenerate(expr
->cond_true
);
1119 true = &expr
->cond_true
;
1123 int flags
= expr
->conditional
->flags
& Int_const_expr
;
1124 flags
&= (*true)->flags
& expr
->cond_false
->flags
;
1129 lclass
= classify_type(ltype
, <ype
);
1130 rclass
= classify_type(rtype
, &rtype
);
1131 if (lclass
& rclass
& TYPE_NUM
) {
1132 ctype
= usual_conversions('?', *true, expr
->cond_false
,
1133 lclass
, rclass
, ltype
, rtype
);
1134 *true = cast_to(*true, ctype
);
1135 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1139 if ((lclass
| rclass
) & TYPE_PTR
) {
1140 int is_null1
= is_null_pointer_constant(*true);
1141 int is_null2
= is_null_pointer_constant(expr
->cond_false
);
1143 if (is_null1
&& is_null2
) {
1144 *true = cast_to(*true, &ptr_ctype
);
1145 expr
->cond_false
= cast_to(expr
->cond_false
, &ptr_ctype
);
1149 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1152 *true = cast_to(*true, rtype
);
1156 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1158 bad_null(expr
->cond_false
);
1159 expr
->cond_false
= cast_to(expr
->cond_false
, ltype
);
1163 if (!(lclass
& rclass
& TYPE_PTR
)) {
1164 typediff
= "different types";
1167 /* OK, it's pointer on pointer */
1168 if (ltype
->ctype
.as
!= rtype
->ctype
.as
) {
1169 typediff
= "different address spaces";
1173 /* need to be lazier here */
1174 lbase
= examine_pointer_target(ltype
);
1175 rbase
= examine_pointer_target(rtype
);
1176 qual
= target_qualifiers(ltype
) | target_qualifiers(rtype
);
1178 if (lbase
== &void_ctype
) {
1179 /* XXX: pointers to function should warn here */
1184 if (rbase
== &void_ctype
) {
1185 /* XXX: pointers to function should warn here */
1189 /* XXX: that should be pointer to composite */
1191 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1198 /* void on void, struct on same struct, union on same union */
1199 if (ltype
== rtype
) {
1203 typediff
= "different base types";
1206 expression_error(expr
, "incompatible types in conditional expression (%s)", typediff
);
1210 expr
->ctype
= ctype
;
1214 if (qual
& ~ctype
->ctype
.modifiers
) {
1215 struct symbol
*sym
= alloc_symbol(ctype
->pos
, SYM_PTR
);
1217 sym
->ctype
.modifiers
|= qual
;
1220 *true = cast_to(*true, ctype
);
1221 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1225 /* FP assignments can not do modulo or bit operations */
1226 static int compatible_float_op(int op
)
1228 return op
== SPECIAL_ADD_ASSIGN
||
1229 op
== SPECIAL_SUB_ASSIGN
||
1230 op
== SPECIAL_MUL_ASSIGN
||
1231 op
== SPECIAL_DIV_ASSIGN
;
1234 static int evaluate_assign_op(struct expression
*expr
)
1236 struct symbol
*target
= expr
->left
->ctype
;
1237 struct symbol
*source
= expr
->right
->ctype
;
1238 struct symbol
*t
, *s
;
1239 int tclass
= classify_type(target
, &t
);
1240 int sclass
= classify_type(source
, &s
);
1243 if (tclass
& sclass
& TYPE_NUM
) {
1244 if (tclass
& TYPE_FLOAT
&& !compatible_float_op(op
)) {
1245 expression_error(expr
, "invalid assignment");
1248 if (tclass
& TYPE_RESTRICT
) {
1249 if (!restricted_binop(op
, t
)) {
1250 warning(expr
->pos
, "bad assignment (%s) to %s",
1251 show_special(op
), show_typename(t
));
1252 expr
->right
= cast_to(expr
->right
, target
);
1255 /* allowed assignments unfoul */
1256 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1258 if (!restricted_value(expr
->right
, t
))
1260 } else if (!(sclass
& TYPE_RESTRICT
))
1262 /* source and target would better be identical restricted */
1265 warning(expr
->pos
, "invalid assignment: %s", show_special(op
));
1266 info(expr
->pos
, " left side has type %s", show_typename(t
));
1267 info(expr
->pos
, " right side has type %s", show_typename(s
));
1268 expr
->right
= cast_to(expr
->right
, target
);
1271 if (tclass
== TYPE_PTR
&& is_int(sclass
)) {
1272 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1273 unrestrict(expr
->right
, sclass
, &s
);
1274 evaluate_ptr_add(expr
, s
);
1277 expression_error(expr
, "invalid pointer assignment");
1281 expression_error(expr
, "invalid assignment");
1285 expr
->right
= cast_to(expr
->right
, target
);
1289 static int whitelist_pointers(struct symbol
*t1
, struct symbol
*t2
)
1292 return 0; /* yes, 0 - we don't want a cast_to here */
1293 if (t1
== &void_ctype
)
1295 if (t2
== &void_ctype
)
1297 if (classify_type(t1
, &t1
) != TYPE_NUM
)
1299 if (classify_type(t2
, &t2
) != TYPE_NUM
)
1303 if (t1
->ctype
.modifiers
& t2
->ctype
.modifiers
& MOD_CHAR
)
1305 if ((t1
->ctype
.modifiers
^ t2
->ctype
.modifiers
) & MOD_SIZE
)
1310 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1311 struct expression
**rp
, const char *where
)
1313 const char *typediff
;
1314 struct symbol
*source
= degenerate(*rp
);
1315 struct symbol
*t
, *s
;
1316 int tclass
= classify_type(target
, &t
);
1317 int sclass
= classify_type(source
, &s
);
1319 if (tclass
& sclass
& TYPE_NUM
) {
1320 if (tclass
& TYPE_RESTRICT
) {
1321 /* allowed assignments unfoul */
1322 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1324 if (!restricted_value(*rp
, target
))
1328 } else if (!(sclass
& TYPE_RESTRICT
))
1330 typediff
= "different base types";
1334 if (tclass
== TYPE_PTR
) {
1335 unsigned long mod1
, mod2
;
1336 struct symbol
*b1
, *b2
;
1337 // NULL pointer is always OK
1338 int is_null
= is_null_pointer_constant(*rp
);
1344 if (!(sclass
& TYPE_PTR
)) {
1345 typediff
= "different base types";
1348 b1
= examine_pointer_target(t
);
1349 b2
= examine_pointer_target(s
);
1350 mod1
= target_qualifiers(t
);
1351 mod2
= target_qualifiers(s
);
1352 if (whitelist_pointers(b1
, b2
)) {
1354 * assignments to/from void * are OK, provided that
1355 * we do not remove qualifiers from pointed to [C]
1356 * or mix address spaces [sparse].
1358 if (t
->ctype
.as
!= s
->ctype
.as
) {
1359 typediff
= "different address spaces";
1363 typediff
= "different modifiers";
1368 /* It's OK if the target is more volatile or const than the source */
1369 typediff
= type_difference(&t
->ctype
, &s
->ctype
, 0, mod1
);
1375 if ((tclass
& TYPE_COMPOUND
) && s
== t
)
1378 if (tclass
& TYPE_NUM
) {
1379 /* XXX: need to turn into comparison with NULL */
1380 if (t
== &bool_ctype
&& (sclass
& TYPE_PTR
))
1382 typediff
= "different base types";
1385 typediff
= "invalid types";
1388 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1389 info(expr
->pos
, " expected %s", show_typename(target
));
1390 info(expr
->pos
, " got %s", show_typename(source
));
1391 *rp
= cast_to(*rp
, target
);
1394 *rp
= cast_to(*rp
, target
);
1398 static void mark_assigned(struct expression
*expr
)
1404 switch (expr
->type
) {
1409 if (sym
->type
!= SYM_NODE
)
1411 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1415 mark_assigned(expr
->left
);
1416 mark_assigned(expr
->right
);
1419 case EXPR_FORCE_CAST
:
1420 mark_assigned(expr
->cast_expression
);
1423 mark_assigned(expr
->base
);
1431 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1433 if (type
->ctype
.modifiers
& MOD_CONST
)
1434 expression_error(left
, "assignment to const expression");
1436 /* We know left is an lvalue, so it's a "preop-*" */
1437 mark_assigned(left
->unop
);
1440 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1442 struct expression
*left
= expr
->left
;
1443 struct expression
*where
= expr
;
1444 struct symbol
*ltype
;
1446 if (!lvalue_expression(left
)) {
1447 expression_error(expr
, "not an lvalue");
1451 ltype
= left
->ctype
;
1453 if (expr
->op
!= '=') {
1454 if (!evaluate_assign_op(expr
))
1457 if (!compatible_assignment_types(where
, ltype
, &expr
->right
, "assignment"))
1461 evaluate_assign_to(left
, ltype
);
1463 expr
->ctype
= ltype
;
1467 static void examine_fn_arguments(struct symbol
*fn
)
1471 FOR_EACH_PTR(fn
->arguments
, s
) {
1472 struct symbol
*arg
= evaluate_symbol(s
);
1473 /* Array/function arguments silently degenerate into pointers */
1479 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1480 if (arg
->type
== SYM_ARRAY
)
1481 ptr
->ctype
= arg
->ctype
;
1483 ptr
->ctype
.base_type
= arg
;
1484 ptr
->ctype
.as
|= s
->ctype
.as
;
1485 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
& MOD_PTRINHERIT
;
1487 s
->ctype
.base_type
= ptr
;
1489 s
->ctype
.modifiers
&= ~MOD_PTRINHERIT
;
1492 examine_symbol_type(s
);
1499 } END_FOR_EACH_PTR(s
);
1502 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1504 /* Take the modifiers of the pointer, and apply them to the member */
1505 mod
|= sym
->ctype
.modifiers
;
1506 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1507 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1509 newsym
->ctype
.as
= as
;
1510 newsym
->ctype
.modifiers
= mod
;
1516 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1518 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1519 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1521 node
->ctype
.base_type
= ptr
;
1522 ptr
->bit_size
= bits_in_pointer
;
1523 ptr
->ctype
.alignment
= pointer_alignment
;
1525 node
->bit_size
= bits_in_pointer
;
1526 node
->ctype
.alignment
= pointer_alignment
;
1529 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1530 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1531 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1533 if (sym
->type
== SYM_NODE
) {
1534 ptr
->ctype
.as
|= sym
->ctype
.as
;
1535 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1536 sym
= sym
->ctype
.base_type
;
1538 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1539 ptr
->ctype
.as
|= sym
->ctype
.as
;
1540 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1541 sym
= sym
->ctype
.base_type
;
1543 ptr
->ctype
.base_type
= sym
;
1548 /* Arrays degenerate into pointers on pointer arithmetic */
1549 static struct symbol
*degenerate(struct expression
*expr
)
1551 struct symbol
*ctype
, *base
;
1555 ctype
= expr
->ctype
;
1558 base
= examine_symbol_type(ctype
);
1559 if (ctype
->type
== SYM_NODE
)
1560 base
= ctype
->ctype
.base_type
;
1562 * Arrays degenerate into pointers to the entries, while
1563 * functions degenerate into pointers to themselves.
1564 * If array was part of non-lvalue compound, we create a copy
1565 * of that compound first and then act as if we were dealing with
1566 * the corresponding field in there.
1568 switch (base
->type
) {
1570 if (expr
->type
== EXPR_SLICE
) {
1571 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1572 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1574 a
->ctype
.base_type
= expr
->base
->ctype
;
1575 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1576 a
->array_size
= expr
->base
->ctype
->array_size
;
1578 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1580 e0
->ctype
= &lazy_ptr_ctype
;
1582 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1585 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1587 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1589 e2
->right
= expr
->base
;
1591 e2
->ctype
= expr
->base
->ctype
;
1593 if (expr
->r_bitpos
) {
1594 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1597 e3
->right
= alloc_const_expression(expr
->pos
,
1598 bits_to_bytes(expr
->r_bitpos
));
1599 e3
->ctype
= &lazy_ptr_ctype
;
1604 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1607 e4
->ctype
= &lazy_ptr_ctype
;
1610 expr
->type
= EXPR_PREOP
;
1614 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1615 expression_error(expr
, "strange non-value function or array");
1618 *expr
= *expr
->unop
;
1619 ctype
= create_pointer(expr
, ctype
, 1);
1620 expr
->ctype
= ctype
;
1627 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1629 struct expression
*op
= expr
->unop
;
1630 struct symbol
*ctype
;
1632 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1633 expression_error(expr
, "not addressable");
1640 if (expr
->type
== EXPR_SYMBOL
) {
1641 struct symbol
*sym
= expr
->symbol
;
1642 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1646 * symbol expression evaluation is lazy about the type
1647 * of the sub-expression, so we may have to generate
1648 * the type here if so..
1650 if (expr
->ctype
== &lazy_ptr_ctype
) {
1651 ctype
= create_pointer(expr
, ctype
, 0);
1652 expr
->ctype
= ctype
;
1658 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1660 struct expression
*op
= expr
->unop
;
1661 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1663 /* Simplify: *&(expr) => (expr) */
1664 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1670 /* Dereferencing a node drops all the node information. */
1671 if (ctype
->type
== SYM_NODE
)
1672 ctype
= ctype
->ctype
.base_type
;
1674 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1675 target
= ctype
->ctype
.base_type
;
1677 switch (ctype
->type
) {
1679 expression_error(expr
, "cannot dereference this type");
1682 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1683 merge_type(node
, ctype
);
1687 if (!lvalue_expression(op
)) {
1688 expression_error(op
, "non-lvalue array??");
1692 /* Do the implied "addressof" on the array */
1696 * When an array is dereferenced, we need to pick
1697 * up the attributes of the original node too..
1699 merge_type(node
, op
->ctype
);
1700 merge_type(node
, ctype
);
1704 node
->bit_size
= target
->bit_size
;
1705 node
->array_size
= target
->array_size
;
1712 * Unary post-ops: x++ and x--
1714 static struct symbol
*evaluate_postop(struct expression
*expr
)
1716 struct expression
*op
= expr
->unop
;
1717 struct symbol
*ctype
= op
->ctype
;
1718 int class = classify_type(ctype
, &ctype
);
1721 if (!class || class & TYPE_COMPOUND
) {
1722 expression_error(expr
, "need scalar for ++/--");
1725 if (!lvalue_expression(expr
->unop
)) {
1726 expression_error(expr
, "need lvalue expression for ++/--");
1730 if ((class & TYPE_RESTRICT
) && restricted_unop(expr
->op
, &ctype
))
1731 unrestrict(expr
, class, &ctype
);
1733 if (class & TYPE_NUM
) {
1735 } else if (class == TYPE_PTR
) {
1736 struct symbol
*target
= examine_pointer_target(ctype
);
1737 if (!is_function(target
))
1738 multiply
= bits_to_bytes(target
->bit_size
);
1742 evaluate_assign_to(op
, op
->ctype
);
1743 expr
->op_value
= multiply
;
1744 expr
->ctype
= ctype
;
1748 expression_error(expr
, "bad argument type for ++/--");
1752 static struct symbol
*evaluate_sign(struct expression
*expr
)
1754 struct symbol
*ctype
= expr
->unop
->ctype
;
1755 int class = classify_type(ctype
, &ctype
);
1756 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1758 /* should be an arithmetic type */
1759 if (!(class & TYPE_NUM
))
1760 return bad_expr_type(expr
);
1761 if (class & TYPE_RESTRICT
)
1764 if (!(class & TYPE_FLOAT
)) {
1765 ctype
= integer_promotion(ctype
);
1766 expr
->unop
= cast_to(expr
->unop
, ctype
);
1767 } else if (expr
->op
!= '~') {
1768 /* no conversions needed */
1770 return bad_expr_type(expr
);
1772 if (expr
->op
== '+')
1773 *expr
= *expr
->unop
;
1774 expr
->ctype
= ctype
;
1777 if (restricted_unop(expr
->op
, &ctype
))
1778 unrestrict(expr
, class, &ctype
);
1782 static struct symbol
*evaluate_preop(struct expression
*expr
)
1784 struct symbol
*ctype
= expr
->unop
->ctype
;
1788 *expr
= *expr
->unop
;
1794 return evaluate_sign(expr
);
1797 return evaluate_dereference(expr
);
1800 return evaluate_addressof(expr
);
1802 case SPECIAL_INCREMENT
:
1803 case SPECIAL_DECREMENT
:
1805 * From a type evaluation standpoint the preops are
1806 * the same as the postops
1808 return evaluate_postop(expr
);
1811 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1813 if (is_safe_type(ctype
))
1814 warning(expr
->pos
, "testing a 'safe expression'");
1815 if (is_float_type(ctype
)) {
1816 struct expression
*arg
= expr
->unop
;
1817 expr
->type
= EXPR_COMPARE
;
1818 expr
->op
= SPECIAL_EQUAL
;
1820 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1821 expr
->right
->ctype
= ctype
;
1822 expr
->right
->fvalue
= 0;
1823 } else if (is_fouled_type(ctype
)) {
1824 warning(expr
->pos
, "%s degrades to integer",
1825 show_typename(ctype
->ctype
.base_type
));
1827 /* the result is int [6.5.3.3(5)]*/
1834 expr
->ctype
= ctype
;
1838 static struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1840 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1841 struct ptr_list
*list
= head
;
1847 for (i
= 0; i
< list
->nr
; i
++) {
1848 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1850 if (sym
->ident
!= ident
)
1852 *offset
= sym
->offset
;
1855 struct symbol
*ctype
= sym
->ctype
.base_type
;
1859 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1861 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1864 *offset
+= sym
->offset
;
1868 } while ((list
= list
->next
) != head
);
1872 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1874 struct expression
*add
;
1877 * Create a new add-expression
1879 * NOTE! Even if we just add zero, we need a new node
1880 * for the member pointer, since it has a different
1881 * type than the original pointer. We could make that
1882 * be just a cast, but the fact is, a node is a node,
1883 * so we might as well just do the "add zero" here.
1885 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1888 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1889 add
->right
->ctype
= &int_ctype
;
1890 add
->right
->value
= offset
;
1893 * The ctype of the pointer will be lazily evaluated if
1894 * we ever take the address of this member dereference..
1896 add
->ctype
= &lazy_ptr_ctype
;
1900 /* structure/union dereference */
1901 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1904 struct symbol
*ctype
, *member
;
1905 struct expression
*deref
= expr
->deref
, *add
;
1906 struct ident
*ident
= expr
->member
;
1910 if (!evaluate_expression(deref
))
1913 expression_error(expr
, "bad member name");
1917 ctype
= deref
->ctype
;
1918 examine_symbol_type(ctype
);
1919 address_space
= ctype
->ctype
.as
;
1920 mod
= ctype
->ctype
.modifiers
;
1921 if (ctype
->type
== SYM_NODE
) {
1922 ctype
= ctype
->ctype
.base_type
;
1923 address_space
|= ctype
->ctype
.as
;
1924 mod
|= ctype
->ctype
.modifiers
;
1926 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1927 expression_error(expr
, "expected structure or union");
1931 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1933 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1934 const char *name
= "<unnamed>";
1937 name
= ctype
->ident
->name
;
1938 namelen
= ctype
->ident
->len
;
1940 if (ctype
->symbol_list
)
1941 expression_error(expr
, "no member '%s' in %s %.*s",
1942 show_ident(ident
), type
, namelen
, name
);
1944 expression_error(expr
, "using member '%s' in "
1945 "incomplete %s %.*s", show_ident(ident
),
1946 type
, namelen
, name
);
1951 * The member needs to take on the address space and modifiers of
1952 * the "parent" type.
1954 member
= convert_to_as_mod(member
, address_space
, mod
);
1955 ctype
= get_base_type(member
);
1957 if (!lvalue_expression(deref
)) {
1958 if (deref
->type
!= EXPR_SLICE
) {
1962 expr
->base
= deref
->base
;
1963 expr
->r_bitpos
= deref
->r_bitpos
;
1965 expr
->r_bitpos
+= bytes_to_bits(offset
);
1966 expr
->type
= EXPR_SLICE
;
1967 expr
->r_nrbits
= member
->bit_size
;
1968 expr
->r_bitpos
+= member
->bit_offset
;
1969 expr
->ctype
= member
;
1973 deref
= deref
->unop
;
1974 expr
->deref
= deref
;
1976 add
= evaluate_offset(deref
, offset
);
1977 expr
->type
= EXPR_PREOP
;
1981 expr
->ctype
= member
;
1985 static int is_promoted(struct expression
*expr
)
1988 switch (expr
->type
) {
1991 case EXPR_CONDITIONAL
:
2015 static struct symbol
*evaluate_cast(struct expression
*);
2017 static struct symbol
*evaluate_type_information(struct expression
*expr
)
2019 struct symbol
*sym
= expr
->cast_type
;
2021 sym
= evaluate_expression(expr
->cast_expression
);
2025 * Expressions of restricted types will possibly get
2026 * promoted - check that here
2028 if (is_restricted_type(sym
)) {
2029 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
2031 } else if (is_fouled_type(sym
)) {
2035 examine_symbol_type(sym
);
2036 if (is_bitfield_type(sym
)) {
2037 expression_error(expr
, "trying to examine bitfield type");
2043 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
2045 struct symbol
*type
;
2048 type
= evaluate_type_information(expr
);
2052 size
= type
->bit_size
;
2054 if (size
< 0 && is_void_type(type
)) {
2055 warning(expr
->pos
, "expression using sizeof(void)");
2056 size
= bits_in_char
;
2059 if (size
== 1 && is_bool_type(type
)) {
2060 warning(expr
->pos
, "expression using sizeof bool");
2061 size
= bits_in_char
;
2064 if (is_function(type
->ctype
.base_type
)) {
2065 warning(expr
->pos
, "expression using sizeof on a function");
2066 size
= bits_in_char
;
2069 if ((size
< 0) || (size
& (bits_in_char
- 1)))
2070 expression_error(expr
, "cannot size expression");
2072 expr
->type
= EXPR_VALUE
;
2073 expr
->value
= bits_to_bytes(size
);
2075 expr
->ctype
= size_t_ctype
;
2076 return size_t_ctype
;
2079 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
2081 struct symbol
*type
;
2084 type
= evaluate_type_information(expr
);
2088 if (type
->type
== SYM_NODE
)
2089 type
= type
->ctype
.base_type
;
2092 switch (type
->type
) {
2096 type
= get_base_type(type
);
2100 expression_error(expr
, "expected pointer expression");
2103 size
= type
->bit_size
;
2104 if (size
& (bits_in_char
-1))
2106 expr
->type
= EXPR_VALUE
;
2107 expr
->value
= bits_to_bytes(size
);
2109 expr
->ctype
= size_t_ctype
;
2110 return size_t_ctype
;
2113 static struct symbol
*evaluate_alignof(struct expression
*expr
)
2115 struct symbol
*type
;
2117 type
= evaluate_type_information(expr
);
2121 expr
->type
= EXPR_VALUE
;
2122 expr
->value
= type
->ctype
.alignment
;
2124 expr
->ctype
= size_t_ctype
;
2125 return size_t_ctype
;
2128 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
2130 struct expression
*expr
;
2131 struct symbol_list
*argument_types
= fn
->arguments
;
2132 struct symbol
*argtype
;
2135 PREPARE_PTR_LIST(argument_types
, argtype
);
2136 FOR_EACH_PTR (head
, expr
) {
2137 struct expression
**p
= THIS_ADDRESS(expr
);
2138 struct symbol
*ctype
, *target
;
2139 ctype
= evaluate_expression(expr
);
2146 struct symbol
*type
;
2147 int class = classify_type(ctype
, &type
);
2148 if (is_int(class)) {
2149 *p
= cast_to(expr
, integer_promotion(type
));
2150 } else if (class & TYPE_FLOAT
) {
2151 unsigned long mod
= type
->ctype
.modifiers
;
2152 if (!(mod
& (MOD_LONG_ALL
)))
2153 *p
= cast_to(expr
, &double_ctype
);
2154 } else if (class & TYPE_PTR
) {
2155 if (expr
->ctype
== &null_ctype
)
2156 *p
= cast_to(expr
, &ptr_ctype
);
2160 } else if (!target
->forced_arg
){
2161 static char where
[30];
2162 examine_symbol_type(target
);
2163 sprintf(where
, "argument %d", i
);
2164 compatible_assignment_types(expr
, target
, p
, where
);
2168 NEXT_PTR_LIST(argtype
);
2169 } END_FOR_EACH_PTR(expr
);
2170 FINISH_PTR_LIST(argtype
);
2174 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
2178 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
2179 if (sym
->ident
== ident
)
2181 } END_FOR_EACH_PTR(sym
);
2185 static void convert_index(struct expression
*e
)
2187 struct expression
*child
= e
->idx_expression
;
2188 unsigned from
= e
->idx_from
;
2189 unsigned to
= e
->idx_to
+ 1;
2191 e
->init_offset
= from
* bits_to_bytes(e
->ctype
->bit_size
);
2192 e
->init_nr
= to
- from
;
2193 e
->init_expr
= child
;
2196 static void convert_ident(struct expression
*e
)
2198 struct expression
*child
= e
->ident_expression
;
2199 struct symbol
*sym
= e
->field
;
2201 e
->init_offset
= sym
->offset
;
2203 e
->init_expr
= child
;
2206 static void convert_designators(struct expression
*e
)
2209 if (e
->type
== EXPR_INDEX
)
2211 else if (e
->type
== EXPR_IDENTIFIER
)
2219 static void excess(struct expression
*e
, const char *s
)
2221 warning(e
->pos
, "excessive elements in %s initializer", s
);
2225 * implicit designator for the first element
2227 static struct expression
*first_subobject(struct symbol
*ctype
, int class,
2228 struct expression
**v
)
2230 struct expression
*e
= *v
, *new;
2232 if (ctype
->type
== SYM_NODE
)
2233 ctype
= ctype
->ctype
.base_type
;
2235 if (class & TYPE_PTR
) { /* array */
2236 if (!ctype
->bit_size
)
2238 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2239 new->idx_expression
= e
;
2240 new->ctype
= ctype
->ctype
.base_type
;
2242 struct symbol
*field
, *p
;
2243 PREPARE_PTR_LIST(ctype
->symbol_list
, p
);
2244 while (p
&& !p
->ident
&& is_bitfield_type(p
))
2250 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2251 new->ident_expression
= e
;
2252 new->field
= new->ctype
= field
;
2259 * sanity-check explicit designators; return the innermost one or NULL
2260 * in case of error. Assign types.
2262 static struct expression
*check_designators(struct expression
*e
,
2263 struct symbol
*ctype
)
2265 struct expression
*last
= NULL
;
2268 if (ctype
->type
== SYM_NODE
)
2269 ctype
= ctype
->ctype
.base_type
;
2270 if (e
->type
== EXPR_INDEX
) {
2271 struct symbol
*type
;
2272 if (ctype
->type
!= SYM_ARRAY
) {
2273 err
= "array index in non-array";
2276 type
= ctype
->ctype
.base_type
;
2277 if (ctype
->bit_size
>= 0 && type
->bit_size
>= 0) {
2278 unsigned offset
= e
->idx_to
* type
->bit_size
;
2279 if (offset
>= ctype
->bit_size
) {
2280 err
= "index out of bounds in";
2284 e
->ctype
= ctype
= type
;
2287 if (!e
->idx_expression
) {
2291 e
= e
->idx_expression
;
2292 } else if (e
->type
== EXPR_IDENTIFIER
) {
2293 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2294 err
= "field name not in struct or union";
2297 ctype
= find_struct_ident(ctype
, e
->expr_ident
);
2299 err
= "unknown field name in";
2302 e
->field
= e
->ctype
= ctype
;
2304 if (!e
->ident_expression
) {
2308 e
= e
->ident_expression
;
2309 } else if (e
->type
== EXPR_POS
) {
2310 err
= "internal front-end error: EXPR_POS in";
2315 expression_error(e
, "%s initializer", err
);
2320 * choose the next subobject to initialize.
2322 * Get designators for next element, switch old ones to EXPR_POS.
2323 * Return the resulting expression or NULL if we'd run out of subobjects.
2324 * The innermost designator is returned in *v. Designators in old
2325 * are assumed to be already sanity-checked.
2327 static struct expression
*next_designators(struct expression
*old
,
2328 struct symbol
*ctype
,
2329 struct expression
*e
, struct expression
**v
)
2331 struct expression
*new = NULL
;
2335 if (old
->type
== EXPR_INDEX
) {
2336 struct expression
*copy
;
2339 copy
= next_designators(old
->idx_expression
,
2342 n
= old
->idx_to
+ 1;
2343 if (n
* old
->ctype
->bit_size
== ctype
->bit_size
) {
2348 *v
= new = alloc_expression(e
->pos
, EXPR_INDEX
);
2351 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2354 new->idx_from
= new->idx_to
= n
;
2355 new->idx_expression
= copy
;
2356 new->ctype
= old
->ctype
;
2358 } else if (old
->type
== EXPR_IDENTIFIER
) {
2359 struct expression
*copy
;
2360 struct symbol
*field
;
2362 copy
= next_designators(old
->ident_expression
,
2365 field
= old
->field
->next_subobject
;
2371 *v
= new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2374 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2378 new->expr_ident
= field
->ident
;
2379 new->ident_expression
= copy
;
2386 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2387 int class, struct symbol
*ctype
);
2390 * deal with traversing subobjects [6.7.8(17,18,20)]
2392 static void handle_list_initializer(struct expression
*expr
,
2393 int class, struct symbol
*ctype
)
2395 struct expression
*e
, *last
= NULL
, *top
= NULL
, *next
;
2398 FOR_EACH_PTR(expr
->expr_list
, e
) {
2399 struct expression
**v
;
2400 struct symbol
*type
;
2403 if (e
->type
!= EXPR_INDEX
&& e
->type
!= EXPR_IDENTIFIER
) {
2404 struct symbol
*struct_sym
;
2407 last
= first_subobject(ctype
, class, &top
);
2409 last
= next_designators(last
, ctype
, e
, &top
);
2412 excess(e
, class & TYPE_PTR
? "array" :
2414 DELETE_CURRENT_PTR(e
);
2417 struct_sym
= ctype
->type
== SYM_NODE
? ctype
->ctype
.base_type
: ctype
;
2418 if (Wdesignated_init
&& struct_sym
->designated_init
)
2419 warning(e
->pos
, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2420 ctype
->ident
? "in initializer for " : "",
2421 ctype
->ident
? ctype
->ident
->len
: 0,
2422 ctype
->ident
? ctype
->ident
->name
: "",
2423 ctype
->ident
? ": " : "",
2424 get_type_name(struct_sym
->type
),
2425 show_ident(struct_sym
->ident
));
2427 warning(e
->pos
, "advancing past deep designator");
2430 REPLACE_CURRENT_PTR(e
, last
);
2432 next
= check_designators(e
, ctype
);
2434 DELETE_CURRENT_PTR(e
);
2438 /* deeper than one designator? */
2440 convert_designators(last
);
2445 lclass
= classify_type(top
->ctype
, &type
);
2446 if (top
->type
== EXPR_INDEX
)
2447 v
= &top
->idx_expression
;
2449 v
= &top
->ident_expression
;
2451 if (handle_simple_initializer(v
, 1, lclass
, top
->ctype
))
2454 if (!(lclass
& TYPE_COMPOUND
)) {
2455 warning(e
->pos
, "bogus scalar initializer");
2456 DELETE_CURRENT_PTR(e
);
2460 next
= first_subobject(type
, lclass
, v
);
2462 warning(e
->pos
, "missing braces around initializer");
2467 DELETE_CURRENT_PTR(e
);
2468 excess(e
, lclass
& TYPE_PTR
? "array" : "struct or union");
2470 } END_FOR_EACH_PTR(e
);
2472 convert_designators(last
);
2473 expr
->ctype
= ctype
;
2476 static int is_string_literal(struct expression
**v
)
2478 struct expression
*e
= *v
;
2479 while (e
&& e
->type
== EXPR_PREOP
&& e
->op
== '(')
2481 if (!e
|| e
->type
!= EXPR_STRING
)
2483 if (e
!= *v
&& Wparen_string
)
2485 "array initialized from parenthesized string constant");
2491 * We want a normal expression, possibly in one layer of braces. Warn
2492 * if the latter happens inside a list (it's legal, but likely to be
2493 * an effect of screwup). In case of anything not legal, we are definitely
2494 * having an effect of screwup, so just fail and let the caller warn.
2496 static struct expression
*handle_scalar(struct expression
*e
, int nested
)
2498 struct expression
*v
= NULL
, *p
;
2502 if (e
->type
!= EXPR_INITIALIZER
)
2505 FOR_EACH_PTR(e
->expr_list
, p
) {
2509 } END_FOR_EACH_PTR(p
);
2513 case EXPR_INITIALIZER
:
2515 case EXPR_IDENTIFIER
:
2521 warning(e
->pos
, "braces around scalar initializer");
2526 * deal with the cases that don't care about subobjects:
2527 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2528 * character array <- string literal, possibly in braces [6.7.8(14)]
2529 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2530 * compound type <- initializer list in braces [6.7.8(16)]
2531 * The last one punts to handle_list_initializer() which, in turn will call
2532 * us for individual elements of the list.
2534 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2535 * the lack of support of wide char stuff in general.
2537 * One note: we need to take care not to evaluate a string literal until
2538 * we know that we *will* handle it right here. Otherwise we would screw
2539 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2540 * { "string", ...} - we need to preserve that string literal recognizable
2541 * until we dig into the inner struct.
2543 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2544 int class, struct symbol
*ctype
)
2546 int is_string
= is_string_type(ctype
);
2547 struct expression
*e
= *ep
, *p
;
2548 struct symbol
*type
;
2554 if (!(class & TYPE_COMPOUND
)) {
2555 e
= handle_scalar(e
, nested
);
2559 if (!evaluate_expression(e
))
2561 compatible_assignment_types(e
, ctype
, ep
, "initializer");
2566 * sublist; either a string, or we dig in; the latter will deal with
2567 * pathologies, so we don't need anything fancy here.
2569 if (e
->type
== EXPR_INITIALIZER
) {
2571 struct expression
*v
= NULL
;
2574 FOR_EACH_PTR(e
->expr_list
, p
) {
2578 } END_FOR_EACH_PTR(p
);
2579 if (count
== 1 && is_string_literal(&v
)) {
2584 handle_list_initializer(e
, class, ctype
);
2589 if (is_string_literal(&e
)) {
2590 /* either we are doing array of char, or we'll have to dig in */
2597 /* struct or union can be initialized by compatible */
2598 if (class != TYPE_COMPOUND
)
2600 type
= evaluate_expression(e
);
2603 if (ctype
->type
== SYM_NODE
)
2604 ctype
= ctype
->ctype
.base_type
;
2605 if (type
->type
== SYM_NODE
)
2606 type
= type
->ctype
.base_type
;
2612 p
= alloc_expression(e
->pos
, EXPR_STRING
);
2614 type
= evaluate_expression(p
);
2615 if (ctype
->bit_size
!= -1) {
2616 if (ctype
->bit_size
+ bits_in_char
< type
->bit_size
)
2618 "too long initializer-string for array of char");
2619 else if (Winit_cstring
&& ctype
->bit_size
+ bits_in_char
== type
->bit_size
) {
2621 "too long initializer-string for array of char(no space for nul char)");
2628 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2630 struct symbol
*type
;
2631 int class = classify_type(ctype
, &type
);
2632 if (!handle_simple_initializer(ep
, 0, class, ctype
))
2633 expression_error(*ep
, "invalid initializer");
2636 static struct symbol
*evaluate_cast(struct expression
*expr
)
2638 struct expression
*target
= expr
->cast_expression
;
2639 struct symbol
*ctype
;
2640 struct symbol
*t1
, *t2
;
2642 int as1
= 0, as2
= 0;
2648 * Special case: a cast can be followed by an
2649 * initializer, in which case we need to pass
2650 * the type value down to that initializer rather
2651 * than trying to evaluate it as an expression
2653 * A more complex case is when the initializer is
2654 * dereferenced as part of a post-fix expression.
2655 * We need to produce an expression that can be dereferenced.
2657 if (target
->type
== EXPR_INITIALIZER
) {
2658 struct symbol
*sym
= expr
->cast_type
;
2659 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2661 sym
->initializer
= target
;
2662 evaluate_symbol(sym
);
2664 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2667 expr
->type
= EXPR_PREOP
;
2675 ctype
= examine_symbol_type(expr
->cast_type
);
2676 expr
->ctype
= ctype
;
2677 expr
->cast_type
= ctype
;
2679 evaluate_expression(target
);
2682 class1
= classify_type(ctype
, &t1
);
2684 /* cast to non-integer type -> not an integer constant expression */
2685 if (!is_int(class1
))
2687 /* if argument turns out to be not an integer constant expression *and*
2688 it was not a floating literal to start with -> too bad */
2689 else if (expr
->flags
== Int_const_expr
&&
2690 !(target
->flags
& Int_const_expr
))
2693 * You can always throw a value away by casting to
2694 * "void" - that's an implicit "force". Note that
2695 * the same is _not_ true of "void *".
2697 if (t1
== &void_ctype
)
2700 if (class1
& (TYPE_COMPOUND
| TYPE_FN
))
2701 warning(expr
->pos
, "cast to non-scalar");
2705 expression_error(expr
, "cast from unknown type");
2708 class2
= classify_type(t2
, &t2
);
2710 if (class2
& TYPE_COMPOUND
)
2711 warning(expr
->pos
, "cast from non-scalar");
2713 if (expr
->type
== EXPR_FORCE_CAST
)
2716 /* allowed cast unfouls */
2717 if (class2
& TYPE_FOULED
)
2721 if (class1
& TYPE_RESTRICT
)
2722 warning(expr
->pos
, "cast to %s",
2724 if (class2
& TYPE_RESTRICT
)
2725 warning(expr
->pos
, "cast from %s",
2729 if (t1
== &ulong_ctype
)
2731 else if (class1
== TYPE_PTR
) {
2732 examine_pointer_target(t1
);
2736 if (t2
== &ulong_ctype
)
2738 else if (class2
== TYPE_PTR
) {
2739 examine_pointer_target(t2
);
2743 if (!as1
&& as2
> 0)
2744 warning(expr
->pos
, "cast removes address space of expression");
2745 if (as1
> 0 && as2
> 0 && as1
!= as2
)
2746 warning(expr
->pos
, "cast between address spaces (<asn:%d>-><asn:%d>)", as2
, as1
);
2747 if (as1
> 0 && !as2
&&
2748 !is_null_pointer_constant(target
) && Wcast_to_as
)
2750 "cast adds address space to expression (<asn:%d>)", as1
);
2752 if (!(t1
->ctype
.modifiers
& MOD_PTRINHERIT
) && class1
== TYPE_PTR
&&
2753 !as1
&& (target
->flags
& Int_const_expr
)) {
2754 if (t1
->ctype
.base_type
== &void_ctype
) {
2755 if (is_zero_constant(target
)) {
2757 expr
->type
= EXPR_VALUE
;
2758 expr
->ctype
= &null_ctype
;
2769 * Evaluate a call expression with a symbol. This
2770 * should expand inline functions, and evaluate
2773 static int evaluate_symbol_call(struct expression
*expr
)
2775 struct expression
*fn
= expr
->fn
;
2776 struct symbol
*ctype
= fn
->ctype
;
2778 if (fn
->type
!= EXPR_PREOP
)
2781 if (ctype
->op
&& ctype
->op
->evaluate
)
2782 return ctype
->op
->evaluate(expr
);
2784 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2786 struct symbol
*curr
= current_fn
;
2788 if (ctype
->definition
)
2789 ctype
= ctype
->definition
;
2791 current_fn
= ctype
->ctype
.base_type
;
2793 ret
= inline_function(expr
, ctype
);
2795 /* restore the old function */
2803 static struct symbol
*evaluate_call(struct expression
*expr
)
2806 struct symbol
*ctype
, *sym
;
2807 struct expression
*fn
= expr
->fn
;
2808 struct expression_list
*arglist
= expr
->args
;
2810 if (!evaluate_expression(fn
))
2812 sym
= ctype
= fn
->ctype
;
2813 if (ctype
->type
== SYM_NODE
)
2814 ctype
= ctype
->ctype
.base_type
;
2815 if (ctype
->type
== SYM_PTR
)
2816 ctype
= get_base_type(ctype
);
2818 if (ctype
->type
!= SYM_FN
) {
2819 struct expression
*arg
;
2820 expression_error(expr
, "not a function %s",
2821 show_ident(sym
->ident
));
2822 /* do typechecking in arguments */
2823 FOR_EACH_PTR (arglist
, arg
) {
2824 evaluate_expression(arg
);
2825 } END_FOR_EACH_PTR(arg
);
2829 examine_fn_arguments(ctype
);
2830 if (sym
->type
== SYM_NODE
&& fn
->type
== EXPR_PREOP
&&
2831 sym
->op
&& sym
->op
->args
) {
2832 if (!sym
->op
->args(expr
))
2835 if (!evaluate_arguments(sym
, ctype
, arglist
))
2837 args
= expression_list_size(expr
->args
);
2838 fnargs
= symbol_list_size(ctype
->arguments
);
2840 expression_error(expr
,
2841 "not enough arguments for function %s",
2842 show_ident(sym
->ident
));
2843 if (args
> fnargs
&& !ctype
->variadic
)
2844 expression_error(expr
,
2845 "too many arguments for function %s",
2846 show_ident(sym
->ident
));
2848 if (sym
->type
== SYM_NODE
) {
2849 if (evaluate_symbol_call(expr
))
2852 expr
->ctype
= ctype
->ctype
.base_type
;
2856 static struct symbol
*evaluate_offsetof(struct expression
*expr
)
2858 struct expression
*e
= expr
->down
;
2859 struct symbol
*ctype
= expr
->in
;
2862 if (expr
->op
== '.') {
2863 struct symbol
*field
;
2866 expression_error(expr
, "expected structure or union");
2869 examine_symbol_type(ctype
);
2870 class = classify_type(ctype
, &ctype
);
2871 if (class != TYPE_COMPOUND
) {
2872 expression_error(expr
, "expected structure or union");
2876 field
= find_identifier(expr
->ident
, ctype
->symbol_list
, &offset
);
2878 expression_error(expr
, "unknown member");
2882 expr
->type
= EXPR_VALUE
;
2883 expr
->flags
= Int_const_expr
;
2884 expr
->value
= offset
;
2886 expr
->ctype
= size_t_ctype
;
2889 expression_error(expr
, "expected structure or union");
2892 examine_symbol_type(ctype
);
2893 class = classify_type(ctype
, &ctype
);
2894 if (class != (TYPE_COMPOUND
| TYPE_PTR
)) {
2895 expression_error(expr
, "expected array");
2898 ctype
= ctype
->ctype
.base_type
;
2900 expr
->type
= EXPR_VALUE
;
2901 expr
->flags
= Int_const_expr
;
2904 expr
->ctype
= size_t_ctype
;
2906 struct expression
*idx
= expr
->index
, *m
;
2907 struct symbol
*i_type
= evaluate_expression(idx
);
2908 int i_class
= classify_type(i_type
, &i_type
);
2909 if (!is_int(i_class
)) {
2910 expression_error(expr
, "non-integer index");
2913 unrestrict(idx
, i_class
, &i_type
);
2914 idx
= cast_to(idx
, size_t_ctype
);
2915 m
= alloc_const_expression(expr
->pos
,
2916 bits_to_bytes(ctype
->bit_size
));
2917 m
->ctype
= size_t_ctype
;
2918 m
->flags
= Int_const_expr
;
2919 expr
->type
= EXPR_BINOP
;
2923 expr
->ctype
= size_t_ctype
;
2924 expr
->flags
= m
->flags
& idx
->flags
& Int_const_expr
;
2928 struct expression
*copy
= __alloc_expression(0);
2930 if (e
->type
== EXPR_OFFSETOF
)
2932 if (!evaluate_expression(e
))
2934 expr
->type
= EXPR_BINOP
;
2935 expr
->flags
= e
->flags
& copy
->flags
& Int_const_expr
;
2937 expr
->ctype
= size_t_ctype
;
2941 return size_t_ctype
;
2944 struct symbol
*evaluate_expression(struct expression
*expr
)
2951 switch (expr
->type
) {
2954 expression_error(expr
, "value expression without a type");
2957 return evaluate_string(expr
);
2959 return evaluate_symbol_expression(expr
);
2961 if (!evaluate_expression(expr
->left
))
2963 if (!evaluate_expression(expr
->right
))
2965 return evaluate_binop(expr
);
2967 return evaluate_logical(expr
);
2969 evaluate_expression(expr
->left
);
2970 if (!evaluate_expression(expr
->right
))
2972 return evaluate_comma(expr
);
2974 if (!evaluate_expression(expr
->left
))
2976 if (!evaluate_expression(expr
->right
))
2978 return evaluate_compare(expr
);
2979 case EXPR_ASSIGNMENT
:
2980 if (!evaluate_expression(expr
->left
))
2982 if (!evaluate_expression(expr
->right
))
2984 return evaluate_assignment(expr
);
2986 if (!evaluate_expression(expr
->unop
))
2988 return evaluate_preop(expr
);
2990 if (!evaluate_expression(expr
->unop
))
2992 return evaluate_postop(expr
);
2994 case EXPR_FORCE_CAST
:
2995 case EXPR_IMPLIED_CAST
:
2996 return evaluate_cast(expr
);
2998 return evaluate_sizeof(expr
);
2999 case EXPR_PTRSIZEOF
:
3000 return evaluate_ptrsizeof(expr
);
3002 return evaluate_alignof(expr
);
3004 return evaluate_member_dereference(expr
);
3006 return evaluate_call(expr
);
3008 case EXPR_CONDITIONAL
:
3009 return evaluate_conditional_expression(expr
);
3010 case EXPR_STATEMENT
:
3011 expr
->ctype
= evaluate_statement(expr
->statement
);
3015 expr
->ctype
= &ptr_ctype
;
3019 /* Evaluate the type of the symbol .. */
3020 evaluate_symbol(expr
->symbol
);
3021 /* .. but the type of the _expression_ is a "type" */
3022 expr
->ctype
= &type_ctype
;
3026 return evaluate_offsetof(expr
);
3028 /* These can not exist as stand-alone expressions */
3029 case EXPR_INITIALIZER
:
3030 case EXPR_IDENTIFIER
:
3033 expression_error(expr
, "internal front-end error: initializer in expression");
3036 expression_error(expr
, "internal front-end error: SLICE re-evaluated");
3042 static void check_duplicates(struct symbol
*sym
)
3045 struct symbol
*next
= sym
;
3047 while ((next
= next
->same_symbol
) != NULL
) {
3048 const char *typediff
;
3049 evaluate_symbol(next
);
3051 typediff
= type_difference(&sym
->ctype
, &next
->ctype
, 0, 0);
3053 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
3054 show_ident(sym
->ident
),
3055 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
3060 unsigned long mod
= sym
->ctype
.modifiers
;
3061 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
3063 if (!(mod
& MOD_TOPLEVEL
))
3067 if (sym
->ident
== &main_ident
)
3069 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
3073 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
3075 struct symbol
*base_type
;
3083 sym
= examine_symbol_type(sym
);
3084 base_type
= get_base_type(sym
);
3088 /* Evaluate the initializers */
3089 if (sym
->initializer
)
3090 evaluate_initializer(sym
, &sym
->initializer
);
3092 /* And finally, evaluate the body of the symbol too */
3093 if (base_type
->type
== SYM_FN
) {
3094 struct symbol
*curr
= current_fn
;
3096 if (sym
->definition
&& sym
->definition
!= sym
)
3097 return evaluate_symbol(sym
->definition
);
3099 current_fn
= base_type
;
3101 examine_fn_arguments(base_type
);
3102 if (!base_type
->stmt
&& base_type
->inline_stmt
)
3104 if (base_type
->stmt
)
3105 evaluate_statement(base_type
->stmt
);
3113 void evaluate_symbol_list(struct symbol_list
*list
)
3117 FOR_EACH_PTR(list
, sym
) {
3118 evaluate_symbol(sym
);
3119 check_duplicates(sym
);
3120 } END_FOR_EACH_PTR(sym
);
3123 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
3125 struct expression
*expr
= stmt
->expression
;
3126 struct symbol
*fntype
;
3128 evaluate_expression(expr
);
3129 fntype
= current_fn
->ctype
.base_type
;
3130 if (!fntype
|| fntype
== &void_ctype
) {
3131 if (expr
&& expr
->ctype
!= &void_ctype
)
3132 expression_error(expr
, "return expression in %s function", fntype
?"void":"typeless");
3133 if (expr
&& Wreturn_void
)
3134 warning(stmt
->pos
, "returning void-valued expression");
3139 sparse_error(stmt
->pos
, "return with no return value");
3144 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, "return expression");
3148 static void evaluate_if_statement(struct statement
*stmt
)
3150 if (!stmt
->if_conditional
)
3153 evaluate_conditional(stmt
->if_conditional
, 0);
3154 evaluate_statement(stmt
->if_true
);
3155 evaluate_statement(stmt
->if_false
);
3158 static void evaluate_iterator(struct statement
*stmt
)
3160 evaluate_symbol_list(stmt
->iterator_syms
);
3161 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
3162 evaluate_conditional(stmt
->iterator_post_condition
,1);
3163 evaluate_statement(stmt
->iterator_pre_statement
);
3164 evaluate_statement(stmt
->iterator_statement
);
3165 evaluate_statement(stmt
->iterator_post_statement
);
3168 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
3170 switch (*constraint
) {
3171 case '=': /* Assignment */
3172 case '+': /* Update */
3175 expression_error(expr
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
3179 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
3181 switch (*constraint
) {
3182 case '=': /* Assignment */
3183 case '+': /* Update */
3184 expression_error(expr
, "input constraint with assignment (\"%s\")", constraint
);
3188 static void evaluate_asm_statement(struct statement
*stmt
)
3190 struct expression
*expr
;
3194 expr
= stmt
->asm_string
;
3195 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3196 sparse_error(stmt
->pos
, "need constant string for inline asm");
3201 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
3203 case 0: /* Identifier */
3207 case 1: /* Constraint */
3209 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3210 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
3211 *THIS_ADDRESS(expr
) = NULL
;
3214 verify_output_constraint(expr
, expr
->string
->data
);
3217 case 2: /* Expression */
3219 if (!evaluate_expression(expr
))
3221 if (!lvalue_expression(expr
))
3222 warning(expr
->pos
, "asm output is not an lvalue");
3223 evaluate_assign_to(expr
, expr
->ctype
);
3226 } END_FOR_EACH_PTR(expr
);
3229 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
3231 case 0: /* Identifier */
3235 case 1: /* Constraint */
3237 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3238 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
3239 *THIS_ADDRESS(expr
) = NULL
;
3242 verify_input_constraint(expr
, expr
->string
->data
);
3245 case 2: /* Expression */
3247 if (!evaluate_expression(expr
))
3251 } END_FOR_EACH_PTR(expr
);
3253 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
3255 sparse_error(stmt
->pos
, "bad asm clobbers");
3258 if (expr
->type
== EXPR_STRING
)
3260 expression_error(expr
, "asm clobber is not a string");
3261 } END_FOR_EACH_PTR(expr
);
3263 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
3264 if (!sym
|| sym
->type
!= SYM_LABEL
) {
3265 sparse_error(stmt
->pos
, "bad asm label");
3268 } END_FOR_EACH_PTR(sym
);
3271 static void evaluate_case_statement(struct statement
*stmt
)
3273 evaluate_expression(stmt
->case_expression
);
3274 evaluate_expression(stmt
->case_to
);
3275 evaluate_statement(stmt
->case_statement
);
3278 static void check_case_type(struct expression
*switch_expr
,
3279 struct expression
*case_expr
,
3280 struct expression
**enumcase
)
3282 struct symbol
*switch_type
, *case_type
;
3288 switch_type
= switch_expr
->ctype
;
3289 case_type
= evaluate_expression(case_expr
);
3291 if (!switch_type
|| !case_type
)
3295 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
3296 else if (is_enum_type(case_type
))
3297 *enumcase
= case_expr
;
3300 sclass
= classify_type(switch_type
, &switch_type
);
3301 cclass
= classify_type(case_type
, &case_type
);
3303 /* both should be arithmetic */
3304 if (!(sclass
& cclass
& TYPE_NUM
))
3307 /* neither should be floating */
3308 if ((sclass
| cclass
) & TYPE_FLOAT
)
3311 /* if neither is restricted, we are OK */
3312 if (!((sclass
| cclass
) & TYPE_RESTRICT
))
3315 if (!restricted_binop_type(SPECIAL_EQUAL
, case_expr
, switch_expr
,
3316 cclass
, sclass
, case_type
, switch_type
)) {
3317 unrestrict(case_expr
, cclass
, &case_type
);
3318 unrestrict(switch_expr
, sclass
, &switch_type
);
3323 expression_error(case_expr
, "incompatible types for 'case' statement");
3326 static void evaluate_switch_statement(struct statement
*stmt
)
3329 struct expression
*enumcase
= NULL
;
3330 struct expression
**enumcase_holder
= &enumcase
;
3331 struct expression
*sel
= stmt
->switch_expression
;
3333 evaluate_expression(sel
);
3334 evaluate_statement(stmt
->switch_statement
);
3337 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
3338 enumcase_holder
= NULL
; /* Only check cases against switch */
3340 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
3341 struct statement
*case_stmt
= sym
->stmt
;
3342 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
3343 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
3344 } END_FOR_EACH_PTR(sym
);
3347 static void evaluate_goto_statement(struct statement
*stmt
)
3349 struct symbol
*label
= stmt
->goto_label
;
3351 if (label
&& !label
->stmt
&& !lookup_keyword(label
->ident
, NS_KEYWORD
))
3352 sparse_error(stmt
->pos
, "label '%s' was not declared", show_ident(label
->ident
));
3354 evaluate_expression(stmt
->goto_expression
);
3357 struct symbol
*evaluate_statement(struct statement
*stmt
)
3362 switch (stmt
->type
) {
3363 case STMT_DECLARATION
: {
3365 FOR_EACH_PTR(stmt
->declaration
, s
) {
3367 } END_FOR_EACH_PTR(s
);
3372 return evaluate_return_expression(stmt
);
3374 case STMT_EXPRESSION
:
3375 if (!evaluate_expression(stmt
->expression
))
3377 if (stmt
->expression
->ctype
== &null_ctype
)
3378 stmt
->expression
= cast_to(stmt
->expression
, &ptr_ctype
);
3379 return degenerate(stmt
->expression
);
3381 case STMT_COMPOUND
: {
3382 struct statement
*s
;
3383 struct symbol
*type
= NULL
;
3385 /* Evaluate the return symbol in the compound statement */
3386 evaluate_symbol(stmt
->ret
);
3389 * Then, evaluate each statement, making the type of the
3390 * compound statement be the type of the last statement
3392 type
= evaluate_statement(stmt
->args
);
3393 FOR_EACH_PTR(stmt
->stmts
, s
) {
3394 type
= evaluate_statement(s
);
3395 } END_FOR_EACH_PTR(s
);
3401 evaluate_if_statement(stmt
);
3404 evaluate_iterator(stmt
);
3407 evaluate_switch_statement(stmt
);
3410 evaluate_case_statement(stmt
);
3413 return evaluate_statement(stmt
->label_statement
);
3415 evaluate_goto_statement(stmt
);
3420 evaluate_asm_statement(stmt
);
3423 evaluate_expression(stmt
->expression
);
3426 evaluate_expression(stmt
->range_expression
);
3427 evaluate_expression(stmt
->range_low
);
3428 evaluate_expression(stmt
->range_high
);